Files
dolphin/Source/Core/VideoCommon/VertexLoaderManager.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

301 lines
9.4 KiB
C++
Raw Normal View History

// Copyright 2008 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "VideoCommon/VertexLoaderManager.h"
#include <algorithm>
#include <iterator>
2014-06-05 17:55:21 +02:00
#include <memory>
2014-08-24 23:53:28 -04:00
#include <mutex>
2016-01-17 16:54:31 -05:00
#include <string>
#include <unordered_map>
2014-08-15 16:17:06 +02:00
#include <utility>
#include <vector>
2016-01-17 16:54:31 -05:00
#include "Common/CommonTypes.h"
2021-06-20 13:47:57 -07:00
#include "Common/EnumMap.h"
2021-02-28 13:53:32 -08:00
#include "Common/Logging/Log.h"
2014-02-17 05:18:15 -05:00
#include "Core/HW/Memmap.h"
#include "VideoCommon/BPMemory.h"
2021-02-28 13:53:32 -08:00
#include "VideoCommon/CPMemory.h"
#include "VideoCommon/DataReader.h"
#include "VideoCommon/IndexGenerator.h"
2016-01-17 16:54:31 -05:00
#include "VideoCommon/NativeVertexFormat.h"
#include "VideoCommon/RenderBase.h"
2014-02-17 05:18:15 -05:00
#include "VideoCommon/Statistics.h"
#include "VideoCommon/VertexLoaderBase.h"
#include "VideoCommon/VertexManagerBase.h"
2014-02-17 05:18:15 -05:00
#include "VideoCommon/VertexShaderManager.h"
namespace VertexLoaderManager
{
2015-06-01 19:58:27 +02:00
float position_cache[3][4];
// The counter added to the address of the array is 1, 2, or 3, but never zero.
// So only index 1 - 3 are used.
u32 position_matrix_index[4];
2015-06-01 19:58:27 +02:00
static NativeVertexFormatMap s_native_vertex_map;
static NativeVertexFormat* s_current_vtx_fmt;
u32 g_current_components;
typedef std::unordered_map<VertexLoaderUID, std::unique_ptr<VertexLoaderBase>> VertexLoaderMap;
2014-08-24 23:53:28 -04:00
static std::mutex s_vertex_loader_map_lock;
static VertexLoaderMap s_vertex_loader_map;
// TODO - change into array of pointers. Keep a map of all seen so far.
2021-06-20 13:47:57 -07:00
Common::EnumMap<u8*, CPArray::TexCoord7> cached_arraybases;
BitSet8 g_main_vat_dirty;
BitSet8 g_preprocess_vat_dirty;
bool g_bases_dirty; // Main only
u8 g_current_vat; // Main only
std::array<VertexLoaderBase*, CP_NUM_VAT_REG> g_main_vertex_loaders;
std::array<VertexLoaderBase*, CP_NUM_VAT_REG> g_preprocess_vertex_loaders;
void Init()
{
MarkAllDirty();
for (auto& map_entry : g_main_vertex_loaders)
map_entry = nullptr;
for (auto& map_entry : g_preprocess_vertex_loaders)
2014-08-24 23:53:28 -04:00
map_entry = nullptr;
SETSTAT(g_stats.num_vertex_loaders, 0);
}
void Clear()
{
2014-08-24 23:53:28 -04:00
std::lock_guard<std::mutex> lk(s_vertex_loader_map_lock);
s_vertex_loader_map.clear();
s_native_vertex_map.clear();
}
void UpdateVertexArrayPointers()
{
2015-05-30 03:58:27 +12:00
// Anything to update?
if (!g_bases_dirty)
2015-05-30 03:58:27 +12:00
return;
// Some games such as Burnout 2 can put invalid addresses into
// the array base registers. (see issue 8591)
// But the vertex arrays with invalid addresses aren't actually enabled.
// Note: Only array bases 0 through 11 are used by the Vertex loaders.
// 12 through 15 are used for loading data into xfmem.
2021-02-08 15:22:10 -08:00
// We also only update the array base if the vertex description states we are going to use it.
if (IsIndexed(g_main_cp_state.vtx_desc.low.Position))
2021-06-20 13:47:57 -07:00
cached_arraybases[CPArray::Position] =
Memory::GetPointer(g_main_cp_state.array_bases[CPArray::Position]);
2021-02-08 15:22:10 -08:00
if (IsIndexed(g_main_cp_state.vtx_desc.low.Normal))
2021-06-20 13:47:57 -07:00
cached_arraybases[CPArray::Normal] =
Memory::GetPointer(g_main_cp_state.array_bases[CPArray::Normal]);
2021-02-08 15:22:10 -08:00
2021-06-20 13:47:57 -07:00
for (u8 i = 0; i < g_main_cp_state.vtx_desc.low.Color.Size(); i++)
{
2021-02-08 15:22:10 -08:00
if (IsIndexed(g_main_cp_state.vtx_desc.low.Color[i]))
2021-06-20 13:47:57 -07:00
cached_arraybases[CPArray::Color0 + i] =
Memory::GetPointer(g_main_cp_state.array_bases[CPArray::Color0 + i]);
2021-02-08 15:22:10 -08:00
}
2021-06-20 13:47:57 -07:00
for (u8 i = 0; i < g_main_cp_state.vtx_desc.high.TexCoord.Size(); i++)
2021-02-08 15:22:10 -08:00
{
if (IsIndexed(g_main_cp_state.vtx_desc.high.TexCoord[i]))
2021-06-20 13:47:57 -07:00
cached_arraybases[CPArray::TexCoord0 + i] =
Memory::GetPointer(g_main_cp_state.array_bases[CPArray::TexCoord0 + i]);
}
2015-05-30 03:58:27 +12:00
g_bases_dirty = false;
}
namespace
{
struct entry
{
std::string text;
u64 num_verts;
bool operator<(const entry& other) const { return num_verts > other.num_verts; }
};
2019-05-05 23:48:12 +00:00
} // namespace
void MarkAllDirty()
{
g_main_vat_dirty = BitSet8::AllTrue(8);
g_preprocess_vat_dirty = BitSet8::AllTrue(8);
}
NativeVertexFormat* GetOrCreateMatchingFormat(const PortableVertexDeclaration& decl)
{
auto iter = s_native_vertex_map.find(decl);
if (iter == s_native_vertex_map.end())
{
std::unique_ptr<NativeVertexFormat> fmt = g_renderer->CreateNativeVertexFormat(decl);
auto ipair = s_native_vertex_map.emplace(decl, std::move(fmt));
iter = ipair.first;
}
return iter->second.get();
}
NativeVertexFormat* GetUberVertexFormat(const PortableVertexDeclaration& decl)
{
// The padding in the structs can cause the memcmp() in the map to create duplicates.
// Avoid this by initializing the padding to zero.
PortableVertexDeclaration new_decl;
std::memset(&new_decl, 0, sizeof(new_decl));
new_decl.stride = decl.stride;
2021-06-26 12:48:28 -07:00
auto MakeDummyAttribute = [](AttributeFormat& attr, ComponentFormat type, int components,
bool integer) {
attr.type = type;
attr.components = components;
attr.offset = 0;
attr.enable = true;
attr.integer = integer;
};
auto CopyAttribute = [](AttributeFormat& attr, const AttributeFormat& src) {
attr.type = src.type;
attr.components = src.components;
attr.offset = src.offset;
attr.enable = src.enable;
attr.integer = src.integer;
};
if (decl.position.enable)
CopyAttribute(new_decl.position, decl.position);
else
2021-06-26 12:48:28 -07:00
MakeDummyAttribute(new_decl.position, ComponentFormat::Float, 1, false);
for (size_t i = 0; i < std::size(new_decl.normals); i++)
{
if (decl.normals[i].enable)
CopyAttribute(new_decl.normals[i], decl.normals[i]);
else
2021-06-26 12:48:28 -07:00
MakeDummyAttribute(new_decl.normals[i], ComponentFormat::Float, 1, false);
}
for (size_t i = 0; i < std::size(new_decl.colors); i++)
{
if (decl.colors[i].enable)
CopyAttribute(new_decl.colors[i], decl.colors[i]);
else
2021-06-26 12:48:28 -07:00
MakeDummyAttribute(new_decl.colors[i], ComponentFormat::UByte, 4, false);
}
for (size_t i = 0; i < std::size(new_decl.texcoords); i++)
{
if (decl.texcoords[i].enable)
CopyAttribute(new_decl.texcoords[i], decl.texcoords[i]);
else
2021-06-26 12:48:28 -07:00
MakeDummyAttribute(new_decl.texcoords[i], ComponentFormat::Float, 1, false);
}
if (decl.posmtx.enable)
CopyAttribute(new_decl.posmtx, decl.posmtx);
else
2021-06-26 12:48:28 -07:00
MakeDummyAttribute(new_decl.posmtx, ComponentFormat::UByte, 1, true);
return GetOrCreateMatchingFormat(new_decl);
}
2015-01-02 21:05:36 +01:00
static VertexLoaderBase* RefreshLoader(int vtx_attr_group, bool preprocess = false)
2014-08-15 16:17:06 +02:00
{
2015-01-02 21:05:36 +01:00
CPState* state = preprocess ? &g_preprocess_cp_state : &g_main_cp_state;
BitSet8& attr_dirty = preprocess ? g_preprocess_vat_dirty : g_main_vat_dirty;
auto& vertex_loaders = preprocess ? g_main_vertex_loaders : g_preprocess_vertex_loaders;
g_current_vat = vtx_attr_group;
2015-01-02 21:05:36 +01:00
VertexLoaderBase* loader;
if (attr_dirty[vtx_attr_group])
2014-08-15 16:17:06 +02:00
{
2015-01-02 21:05:36 +01:00
// 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]);
2014-08-24 23:53:28 -04:00
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())
2014-08-15 16:17:06 +02:00
{
2014-08-24 23:53:28 -04:00
loader = iter->second.get();
2015-01-02 21:05:36 +01:00
check_for_native_format &= !loader->m_native_vertex_format;
2014-08-15 16:17:06 +02:00
}
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(g_stats.num_vertex_loaders);
2015-01-02 21:05:36 +01:00
}
if (check_for_native_format)
{
// search for a cached native vertex format
2014-12-13 00:23:54 +01:00
const PortableVertexDeclaration& format = loader->m_native_vtx_decl;
2015-01-08 10:44:16 +01:00
std::unique_ptr<NativeVertexFormat>& native = s_native_vertex_map[format];
if (!native)
native = g_renderer->CreateNativeVertexFormat(format);
loader->m_native_vertex_format = native.get();
2014-08-15 16:17:06 +02:00
}
vertex_loaders[vtx_attr_group] = loader;
attr_dirty[vtx_attr_group] = false;
2014-08-24 23:53:28 -04:00
}
else
{
loader = vertex_loaders[vtx_attr_group];
2014-08-15 16:17:06 +02:00
}
2015-05-29 18:25:19 +12:00
// Lookup pointers for any vertex arrays.
if (!preprocess)
UpdateVertexArrayPointers();
2015-05-29 18:25:19 +12:00
2014-08-24 23:53:28 -04:00
return loader;
2014-08-15 16:17:06 +02:00
}
int RunVertices(int vtx_attr_group, OpcodeDecoder::Primitive primitive, int count, DataReader src,
bool is_preprocess)
{
if (!count)
2014-12-09 08:35:04 +01:00
return 0;
VertexLoaderBase* loader = RefreshLoader(vtx_attr_group, is_preprocess);
int size = count * loader->m_vertex_size;
2014-12-09 08:35:04 +01:00
if ((int)src.size() < size)
return -1;
2016-10-07 19:55:47 -07:00
if (is_preprocess)
2014-12-09 08:35:04 +01:00
return size;
// If the native vertex format changed, force a flush.
if (loader->m_native_vertex_format != s_current_vtx_fmt ||
loader->m_native_components != g_current_components)
{
g_vertex_manager->Flush();
}
s_current_vtx_fmt = loader->m_native_vertex_format;
g_current_components = loader->m_native_components;
2017-07-20 15:25:27 +10:00
VertexShaderManager::SetVertexFormat(loader->m_native_components);
// if cull mode is CULL_ALL, tell VertexManager to skip triangles and quads.
// They still need to go through vertex loading, because we need to calculate a zfreeze refrence
// slope.
bool cullall = (bpmem.genMode.cullmode == CullMode::All &&
primitive < OpcodeDecoder::Primitive::GX_DRAW_LINES);
DataReader dst = g_vertex_manager->PrepareForAdditionalData(
primitive, count, loader->m_native_vtx_decl.stride, cullall);
count = loader->RunVertices(src, dst, count);
g_vertex_manager->AddIndices(primitive, count);
g_vertex_manager->FlushData(count, loader->m_native_vtx_decl.stride);
2014-12-09 08:35:04 +01:00
ADDSTAT(g_stats.this_frame.num_prims, count);
INCSTAT(g_stats.this_frame.num_primitive_joins);
2014-12-09 08:35:04 +01:00
return size;
}
NativeVertexFormat* GetCurrentVertexFormat()
2014-06-05 17:55:21 +02:00
{
return s_current_vtx_fmt;
2014-06-05 17:55:21 +02:00
}
2019-05-05 23:48:12 +00:00
} // namespace VertexLoaderManager