Debian Codegraph @

Source/Core/VideoCommon/VertexLoader.cpp

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/VertexLoader.h"
5
6 #include "Common/Assert.h"
7 #include "Common/CommonTypes.h"
8
9 #include "VideoCommon/VertexLoaderManager.h"
10 #include "VideoCommon/VertexLoaderUtils.h"
11 #include "VideoCommon/VertexLoader_Color.h"
12 #include "VideoCommon/VertexLoader_Normal.h"
13 #include "VideoCommon/VertexLoader_Position.h"
14 #include "VideoCommon/VertexLoader_TextCoord.h"
15 #include "VideoCommon/VideoCommon.h"
16
17 // This pointer is used as the source/dst for all fixed function loader calls
18 const u8* g_video_buffer_read_ptr;
19 u8* g_vertex_manager_write_ptr;
20
21 static void PosMtx_ReadDirect_UByte(VertexLoader* loader)
22 {
23 u32 posmtx = DataRead<u8>() & 0x3f;
24 if (loader->m_remaining < 3)
25 VertexLoaderManager::position_matrix_index_cache[loader->m_remaining] = posmtx;
26 DataWrite<u32>(posmtx);
27 PRIM_LOG("posmtx: {}, ", posmtx);
28 }
29
30 static void TexMtx_ReadDirect_UByte(VertexLoader* loader)
31 {
32 loader->m_curtexmtx[loader->m_texmtxread] = DataRead<u8>() & 0x3f;
33
34 PRIM_LOG("texmtx{}: {}, ", loader->m_texmtxread, loader->m_curtexmtx[loader->m_texmtxread]);
35 loader->m_texmtxread++;
36 }
37
38 static void TexMtx_Write_Float(VertexLoader* loader)
39 {
40 DataWrite(float(loader->m_curtexmtx[loader->m_texmtxwrite++]));
41 }
42
43 static void TexMtx_Write_Float2(VertexLoader* loader)
44 {
45 DataWrite(0.f);
46 DataWrite(float(loader->m_curtexmtx[loader->m_texmtxwrite++]));
47 }
48
49 static void TexMtx_Write_Float3(VertexLoader* loader)
50 {
51 DataWrite(0.f);
52 DataWrite(0.f);
53 DataWrite(float(loader->m_curtexmtx[loader->m_texmtxwrite++]));
54 }
55
56 static void SkipVertex(VertexLoader* loader)
57 {
58 if (loader->m_vertexSkip)
59 {
60 // reset the output buffer
61 g_vertex_manager_write_ptr -= loader->m_native_vtx_decl.stride;
62
63 loader->m_skippedVertices++;
64 }
65 }
66
67 VertexLoader::VertexLoader(const TVtxDesc& vtx_desc, const VAT& vtx_attr)
68 : VertexLoaderBase(vtx_desc, vtx_attr)
69 {
70 CompileVertexTranslator();
71
72 // generate frac factors
73 m_posScale = 1.0f / (1U << m_VtxAttr.g0.PosFrac);
74 for (u32 i = 0; i < 8; i++)
75 m_tcScale[i] = 1.0f / (1U << m_VtxAttr.GetTexFrac(i));
76 }
77
78 void VertexLoader::CompileVertexTranslator()
79 {
80 // Position in pc vertex format.
81 int nat_offset = 0;
82
83 // Position Matrix Index
84 if (m_VtxDesc.low.PosMatIdx)
85 {
86 WriteCall(PosMtx_ReadDirect_UByte);
87 m_native_vtx_decl.posmtx.components = 4;
88 m_native_vtx_decl.posmtx.enable = true;
89 m_native_vtx_decl.posmtx.offset = nat_offset;
90 m_native_vtx_decl.posmtx.type = ComponentFormat::UByte;
91 m_native_vtx_decl.posmtx.integer = true;
92 nat_offset += 4;
93 }
94
95 for (auto texmtxidx : m_VtxDesc.low.TexMatIdx)
96 {
97 if (texmtxidx)
98 WriteCall(TexMtx_ReadDirect_UByte);
99 }
100
101 // Write vertex position loader
102 WriteCall(VertexLoader_Position::GetFunction(m_VtxDesc.low.Position, m_VtxAttr.g0.PosFormat,
103 m_VtxAttr.g0.PosElements));
104
105 int pos_elements = m_VtxAttr.g0.PosElements == CoordComponentCount::XY ? 2 : 3;
106 m_native_vtx_decl.position.components = pos_elements;
107 m_native_vtx_decl.position.enable = true;
108 m_native_vtx_decl.position.offset = nat_offset;
109 m_native_vtx_decl.position.type = ComponentFormat::Float;
110 m_native_vtx_decl.position.integer = false;
111 nat_offset += pos_elements * sizeof(float);
112
113 // Normals
114 if (m_VtxDesc.low.Normal != VertexComponentFormat::NotPresent)
115 {
116 TPipelineFunction pFunc =
117 VertexLoader_Normal::GetFunction(m_VtxDesc.low.Normal, m_VtxAttr.g0.NormalFormat,
118 m_VtxAttr.g0.NormalElements, m_VtxAttr.g0.NormalIndex3);
119
120 if (pFunc == nullptr)
121 {
122 PanicAlertFmt("VertexLoader_Normal::GetFunction({} {} {} {}) returned zero!",
123 m_VtxDesc.low.Normal, m_VtxAttr.g0.NormalFormat, m_VtxAttr.g0.NormalElements,
124 m_VtxAttr.g0.NormalIndex3);
125 }
126 WriteCall(pFunc);
127
128 for (int i = 0; i < (m_VtxAttr.g0.NormalElements == NormalComponentCount::NTB ? 3 : 1); i++)
129 {
130 m_native_vtx_decl.normals[i].components = 3;
131 m_native_vtx_decl.normals[i].enable = true;
132 m_native_vtx_decl.normals[i].offset = nat_offset;
133 m_native_vtx_decl.normals[i].type = ComponentFormat::Float;
134 m_native_vtx_decl.normals[i].integer = false;
135 nat_offset += 12;
136 }
137 }
138
139 for (size_t i = 0; i < m_VtxDesc.low.Color.Size(); i++)
140 {
141 m_native_vtx_decl.colors[i].components = 4;
142 m_native_vtx_decl.colors[i].type = ComponentFormat::UByte;
143 m_native_vtx_decl.colors[i].integer = false;
144
145 TPipelineFunction pFunc =
146 VertexLoader_Color::GetFunction(m_VtxDesc.low.Color[i], m_VtxAttr.GetColorFormat(i));
147
148 if (pFunc != nullptr)
149 {
150 WriteCall(pFunc);
151 }
152 else
153 {
154 ASSERT(m_VtxDesc.low.Color[i] == VertexComponentFormat::NotPresent);
155 // Keep colIndex in sync if color 0 is absent but color 1 is present
156 if (i == 0 && m_VtxDesc.low.Color[1] != VertexComponentFormat::NotPresent)
157 WriteCall(VertexLoader_Color::GetDummyFunction());
158 }
159
160 if (m_VtxDesc.low.Color[i] != VertexComponentFormat::NotPresent)
161 {
162 m_native_vtx_decl.colors[i].offset = nat_offset;
163 m_native_vtx_decl.colors[i].enable = true;
164 nat_offset += 4;
165 }
166 }
167
168 // Texture matrix indices (remove if corresponding texture coordinate isn't enabled)
169 for (size_t i = 0; i < m_VtxDesc.high.TexCoord.Size(); i++)
170 {
171 m_native_vtx_decl.texcoords[i].offset = nat_offset;
172 m_native_vtx_decl.texcoords[i].type = ComponentFormat::Float;
173 m_native_vtx_decl.texcoords[i].integer = false;
174
175 const auto tc = m_VtxDesc.high.TexCoord[i].Value();
176 const auto format = m_VtxAttr.GetTexFormat(i);
177 const auto elements = m_VtxAttr.GetTexElements(i);
178
179 if (tc != VertexComponentFormat::NotPresent)
180 {
181 ASSERT_MSG(VIDEO, VertexComponentFormat::Direct <= tc && tc <= VertexComponentFormat::Index16,
182 "Invalid texture coordinates!\n(tc = {})", tc);
183 ASSERT_MSG(VIDEO, ComponentFormat::UByte <= format && format <= ComponentFormat::Float,
184 "Invalid texture coordinates format!\n(format = {})", format);
185 ASSERT_MSG(VIDEO, elements == TexComponentCount::S || elements == TexComponentCount::ST,
186 "Invalid number of texture coordinates elements!\n(elements = {})", elements);
187
188 WriteCall(VertexLoader_TextCoord::GetFunction(tc, format, elements));
189 }
190
191 if (m_VtxDesc.low.TexMatIdx[i])
192 {
193 m_native_vtx_decl.texcoords[i].enable = true;
194 m_native_vtx_decl.texcoords[i].components = 3;
195 nat_offset += 12;
196 if (tc != VertexComponentFormat::NotPresent)
197 {
198 // if texmtx is included, texcoord will always be 3 floats, z will be the texmtx index
199 WriteCall(elements == TexComponentCount::ST ? TexMtx_Write_Float : TexMtx_Write_Float2);
200 }
201 else
202 {
203 WriteCall(TexMtx_Write_Float3);
204 }
205 }
206 else
207 {
208 if (tc != VertexComponentFormat::NotPresent)
209 {
210 m_native_vtx_decl.texcoords[i].enable = true;
211 m_native_vtx_decl.texcoords[i].components = elements == TexComponentCount::ST ? 2 : 1;
212 nat_offset += 4 * (elements == TexComponentCount::ST ? 2 : 1);
213 }
214 }
215
216 if (tc == VertexComponentFormat::NotPresent)
217 {
218 // if there's more tex coords later, have to write a dummy call
219 bool has_more = false;
220 for (size_t j = i + 1; j < m_VtxDesc.high.TexCoord.Size(); ++j)
221 {
222 if (m_VtxDesc.high.TexCoord[j] != VertexComponentFormat::NotPresent)
223 {
224 has_more = true;
225 // Keep tcIndex in sync so that the correct array is used later
226 WriteCall(VertexLoader_TextCoord::GetDummyFunction());
227 break;
228 }
229 else if (m_VtxDesc.low.TexMatIdx[j])
230 {
231 has_more = true;
232 }
233 }
234 if (!has_more)
235 {
236 // no more tex coords and tex matrices, so exit loop
237 break;
238 }
239 }
240 }
241
242 // indexed position formats may skip the vertex
243 if (IsIndexed(m_VtxDesc.low.Position))
244 {
245 WriteCall(SkipVertex);
246 }
247
248 m_native_vtx_decl.stride = nat_offset;
249 }
250
251 void VertexLoader::WriteCall(TPipelineFunction func)
252 {
253 m_PipelineStages.push_back(func);
254 }
255
256 int VertexLoader::RunVertices(const u8* src, u8* dst, int count)
257 {
258 g_vertex_manager_write_ptr = dst;
259 g_video_buffer_read_ptr = src;
260
261 m_numLoadedVertices += count;
262 m_skippedVertices = 0;
263
264 for (m_remaining = count - 1; m_remaining >= 0; m_remaining--)
265 {
266 m_tcIndex = 0;
267 m_colIndex = 0;
268 m_texmtxwrite = m_texmtxread = 0;
269 for (TPipelineFunction& func : m_PipelineStages)
270 func(this);
271 PRIM_LOG("\n");
272 }
273
274 return count - m_skippedVertices;
275 }