Example #1
0
static VertexLoaderBase* RefreshLoader(int vtx_attr_group, bool preprocess = false)
{
  CPState* state = preprocess ? &g_preprocess_cp_state : &g_main_cp_state;
  state->last_id = vtx_attr_group;

  VertexLoaderBase* loader;
  if (state->attr_dirty[vtx_attr_group])
  {
    // We are not allowed to create a native vertex format on preprocessing as this is on the wrong
    // thread
    bool check_for_native_format = !preprocess;

    VertexLoaderUID uid(state->vtx_desc, state->vtx_attr[vtx_attr_group]);
    std::lock_guard<std::mutex> lk(s_vertex_loader_map_lock);
    VertexLoaderMap::iterator iter = s_vertex_loader_map.find(uid);
    if (iter != s_vertex_loader_map.end())
    {
      loader = iter->second.get();
      check_for_native_format &= !loader->m_native_vertex_format;
    }
    else
    {
      s_vertex_loader_map[uid] =
          VertexLoaderBase::CreateVertexLoader(state->vtx_desc, state->vtx_attr[vtx_attr_group]);
      loader = s_vertex_loader_map[uid].get();
      INCSTAT(stats.numVertexLoaders);
    }
    if (check_for_native_format)
    {
      // search for a cached native vertex format
      const PortableVertexDeclaration& format = loader->m_native_vtx_decl;
      std::unique_ptr<NativeVertexFormat>& native = s_native_vertex_map[format];
      if (!native)
      {
        native.reset(g_vertex_manager->CreateNativeVertexFormat(format));
      }
      loader->m_native_vertex_format = native.get();
    }
    state->vertex_loaders[vtx_attr_group] = loader;
    state->attr_dirty[vtx_attr_group] = false;
  }
  else
  {
    loader = state->vertex_loaders[vtx_attr_group];
  }

  // Lookup pointers for any vertex arrays.
  if (!preprocess)
    UpdateVertexArrayPointers();

  return loader;
}
bool ConvertVertices(VertexLoaderParameters &parameters, u32 &readsize, u32 &writesize)
{
	if (parameters.needloaderrefresh)
	{
		UpdateLoader(parameters);
	}
	auto loader = g_main_cp_state.vertex_loaders[parameters.vtx_attr_group];
	if (!loader->EnvironmentIsSupported())
	{
		loader = loader->GetFallback();
	}
	readsize = parameters.count * loader->m_VertexSize;
	if (parameters.buf_size < readsize)
		return false;
	if (parameters.skip_draw)
	{
		return true;
	}
	// Lookup pointers for any vertex arrays.
	UpdateVertexArrayPointers();
	NativeVertexFormat *nativefmt = loader->m_native_vertex_format;
	// Flush if our vertex format is different from the currently set.
	if (s_current_vtx_fmt != nullptr && s_current_vtx_fmt != nativefmt)
	{
		VertexManagerBase::Flush();
	}
	s_current_vtx_fmt = nativefmt;
	g_current_components = loader->m_native_components;
	VertexManagerBase::PrepareForAdditionalData(parameters.primitive, parameters.count, loader->m_native_stride);
	parameters.destination = VertexManagerBase::s_pCurBufferPointer;		
	s32 finalcount = loader->RunVertices(parameters);
	writesize = loader->m_native_stride * finalcount;
	IndexGenerator::AddIndices(parameters.primitive, finalcount);
	ADDSTAT(stats.thisFrame.numPrims, finalcount);
	INCSTAT(stats.thisFrame.numPrimitiveJoins);
	return true;
}