Debian Codegraph @

Source/Core/VideoCommon/VertexLoaderManager.h

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <array>
7 #include <memory>
8 #include <string>
9 #include <unordered_map>
10
11 #include "Common/CommonTypes.h"
12 #include "Common/EnumMap.h"
13 #include "VideoCommon/CPMemory.h"
14
15 class NativeVertexFormat;
16 struct PortableVertexDeclaration;
17
18 namespace OpcodeDecoder
19 {
20 enum class Primitive : u8;
21 }
22
23 namespace VertexLoaderManager
24 {
25 using NativeVertexFormatMap =
26 std::unordered_map<PortableVertexDeclaration, std::unique_ptr<NativeVertexFormat>>;
27
28 void Init();
29 void Clear();
30
31 void MarkAllDirty();
32
33 // Creates or obtains a pointer to a VertexFormat representing decl.
34 // If this results in a VertexFormat being created, if the game later uses a matching vertex
35 // declaration, the one that was previously created will be used.
36 NativeVertexFormat* GetOrCreateMatchingFormat(const PortableVertexDeclaration& decl);
37
38 // For vertex ubershaders, all attributes need to be present, even when the vertex
39 // format does not contain them. This function returns a vertex format with dummy
40 // offsets set to the unused attributes.
41 NativeVertexFormat* GetUberVertexFormat(const PortableVertexDeclaration& decl);
42
43 // Returns -1 if buf_size is insufficient, else the amount of bytes consumed
44 template <bool IsPreprocess = false>
45 int RunVertices(int vtx_attr_group, OpcodeDecoder::Primitive primitive, int count, const u8* src);
46
47 namespace detail
48 {
49 // This will look for an existing loader in the global hashmap or create a new one if there is none.
50 // It should not be used directly because RefreshLoaders() has another cache for fast lookups.
51 template <bool IsPreprocess = false>
52 VertexLoaderBase* GetOrCreateLoader(int vtx_attr_group);
53 } // namespace detail
54
55 NativeVertexFormat* GetCurrentVertexFormat();
56
57 // Resolved pointers to array bases. Used by vertex loaders.
58 extern Common::EnumMap<u8*, CPArray::TexCoord7> cached_arraybases;
59 void UpdateVertexArrayPointers();
60
61 // Position cache for zfreeze (3 vertices, 4 floats each to allow SIMD overwrite).
62 // These arrays are in reverse order.
63 extern std::array<std::array<float, 4>, 3> position_cache;
64 extern std::array<u32, 3> position_matrix_index_cache;
65 // Needed for the game "LIT", which has text that has lighting enabled, but doesn't have normal
66 // vectors. The normals from the last drawn object are used instead.
67 // See https://bugs.dolphin-emu.org/issues/13635
68 extern std::array<float, 4> normal_cache;
69 // Store the tangent and binormal vectors for games that use emboss texgens when the vertex format
70 // doesn't include them (e.g. RS2 and RS3). These too are 4 floats each for SIMD overwrites.
71 extern std::array<float, 4> tangent_cache;
72 extern std::array<float, 4> binormal_cache;
73
74 // VB_HAS_X. Bitmask telling what vertex components are present.
75 extern u32 g_current_components;
76
77 extern BitSet8 g_main_vat_dirty;
78 extern BitSet8 g_preprocess_vat_dirty;
79 extern bool g_bases_dirty; // Main only
80 extern std::array<VertexLoaderBase*, CP_NUM_VAT_REG> g_main_vertex_loaders;
81 extern std::array<VertexLoaderBase*, CP_NUM_VAT_REG> g_preprocess_vertex_loaders;
82 extern bool g_needs_cp_xf_consistency_check;
83
84 template <bool IsPreprocess = false>
85 VertexLoaderBase* RefreshLoader(int vtx_attr_group)
86 {
87 constexpr const BitSet8& attr_dirty = IsPreprocess ? g_preprocess_vat_dirty : g_main_vat_dirty;
88 constexpr const auto& vertex_loaders =
89 IsPreprocess ? g_preprocess_vertex_loaders : g_main_vertex_loaders;
90
91 VertexLoaderBase* loader;
92 if (!attr_dirty[vtx_attr_group]) [[likely]]
93 {
94 loader = vertex_loaders[vtx_attr_group];
95 }
96 else [[unlikely]]
97 {
98 loader = detail::GetOrCreateLoader<IsPreprocess>(vtx_attr_group);
99 }
100
101 // Lookup pointers for any vertex arrays.
102 if constexpr (!IsPreprocess)
103 UpdateVertexArrayPointers();
104
105 return loader;
106 }
107
108 } // namespace VertexLoaderManager