Debian Codegraph @

Source/Core/VideoBackends/Software/SWVertexLoader.cpp

raw symbols SCIP
1 // Copyright 2009 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/Software/SWVertexLoader.h"
5
6 #include <cstddef>
7 #include <limits>
8
9 #include "Common/Assert.h"
10 #include "Common/CommonTypes.h"
11
12 #include "Core/System.h"
13
14 #include "VideoBackends/Software/NativeVertexFormat.h"
15 #include "VideoBackends/Software/Rasterizer.h"
16 #include "VideoBackends/Software/Tev.h"
17 #include "VideoBackends/Software/TransformUnit.h"
18
19 #include "VideoCommon/BoundingBox.h"
20 #include "VideoCommon/CPMemory.h"
21 #include "VideoCommon/DataReader.h"
22 #include "VideoCommon/IndexGenerator.h"
23 #include "VideoCommon/OpcodeDecoding.h"
24 #include "VideoCommon/Statistics.h"
25 #include "VideoCommon/VertexLoaderManager.h"
26 #include "VideoCommon/VertexShaderManager.h"
27 #include "VideoCommon/VideoConfig.h"
28 #include "VideoCommon/XFMemory.h"
29
30 SWVertexLoader::SWVertexLoader() = default;
31
32 SWVertexLoader::~SWVertexLoader() = default;
33
34 DataReader SWVertexLoader::PrepareForAdditionalData(OpcodeDecoder::Primitive primitive, u32 count,
35 u32 stride, bool cullall)
36 {
37 // The software renderer needs cullall to be false for zfreeze to work
38 return VertexManagerBase::PrepareForAdditionalData(primitive, count, stride, false);
39 }
40
41 void SWVertexLoader::DrawCurrentBatch(u32 base_index, u32 num_indices, u32 base_vertex)
42 {
43 using OpcodeDecoder::Primitive;
44 Primitive primitive_type = Primitive::GX_DRAW_QUADS;
45 switch (m_current_primitive_type)
46 {
47 case PrimitiveType::Points:
48 primitive_type = Primitive::GX_DRAW_POINTS;
49 break;
50 case PrimitiveType::Lines:
51 primitive_type = Primitive::GX_DRAW_LINES;
52 break;
53 case PrimitiveType::Triangles:
54 primitive_type = Primitive::GX_DRAW_TRIANGLES;
55 break;
56 case PrimitiveType::TriangleStrip:
57 primitive_type = Primitive::GX_DRAW_TRIANGLE_STRIP;
58 break;
59 }
60
61 // Flush bounding box here because software overrides the base function
62 if (g_bounding_box->IsEnabled())
63 g_bounding_box->Flush();
64
65 m_setup_unit.Init(primitive_type);
66 Rasterizer::SetTevKonstColors();
67
68 for (u32 i = 0; i < m_index_generator.GetIndexLen(); i++)
69 {
70 const u16 index = m_cpu_index_buffer[i];
71 memset(static_cast<void*>(&m_vertex), 0, sizeof(m_vertex));
72
73 // parse the videocommon format to our own struct format (m_vertex)
74 SetFormat();
75 ParseVertex(VertexLoaderManager::GetCurrentVertexFormat()->GetVertexDeclaration(), index);
76
77 // transform this vertex so that it can be used for rasterization (outVertex)
78 OutputVertexData* outVertex = m_setup_unit.GetVertex();
79 TransformUnit::TransformPosition(&m_vertex, outVertex);
80 TransformUnit::TransformNormal(&m_vertex, outVertex);
81 TransformUnit::TransformColor(&m_vertex, outVertex);
82 TransformUnit::TransformTexCoord(&m_vertex, outVertex);
83
84 // assemble and rasterize the primitive
85 m_setup_unit.SetupVertex();
86
87 INCSTAT(g_stats.this_frame.num_vertices_loaded);
88 }
89
90 INCSTAT(g_stats.this_frame.num_drawn_objects);
91 }
92
93 void SWVertexLoader::SetFormat()
94 {
95 m_vertex.posMtx = xfmem.MatrixIndexA.PosNormalMtxIdx;
96 m_vertex.texMtx[0] = xfmem.MatrixIndexA.Tex0MtxIdx;
97 m_vertex.texMtx[1] = xfmem.MatrixIndexA.Tex1MtxIdx;
98 m_vertex.texMtx[2] = xfmem.MatrixIndexA.Tex2MtxIdx;
99 m_vertex.texMtx[3] = xfmem.MatrixIndexA.Tex3MtxIdx;
100 m_vertex.texMtx[4] = xfmem.MatrixIndexB.Tex4MtxIdx;
101 m_vertex.texMtx[5] = xfmem.MatrixIndexB.Tex5MtxIdx;
102 m_vertex.texMtx[6] = xfmem.MatrixIndexB.Tex6MtxIdx;
103 m_vertex.texMtx[7] = xfmem.MatrixIndexB.Tex7MtxIdx;
104 }
105
106 template <typename T, typename I>
107 static T ReadNormalized(I value)
108 {
109 T casted = (T)value;
110 if (!std::numeric_limits<T>::is_integer && std::numeric_limits<I>::is_integer)
111 {
112 // normalize if non-float is converted to a float
113 casted *= (T)(1.0 / std::numeric_limits<I>::max());
114 }
115 return casted;
116 }
117
118 template <typename T, bool swap = false>
119 static void ReadVertexAttribute(T* dst, DataReader src, const AttributeFormat& format,
120 int base_component, int components, bool reverse)
121 {
122 if (format.enable)
123 {
124 src.Skip(format.offset);
125 src.Skip(base_component * GetElementSize(format.type));
126
127 int i;
128 for (i = 0; i < std::min(format.components - base_component, components); i++)
129 {
130 int i_dst = reverse ? components - i - 1 : i;
131 switch (format.type)
132 {
133 case ComponentFormat::UByte:
134 dst[i_dst] = ReadNormalized<T, u8>(src.Read<u8, swap>());
135 break;
136 case ComponentFormat::Byte:
137 dst[i_dst] = ReadNormalized<T, s8>(src.Read<s8, swap>());
138 break;
139 case ComponentFormat::UShort:
140 dst[i_dst] = ReadNormalized<T, u16>(src.Read<u16, swap>());
141 break;
142 case ComponentFormat::Short:
143 dst[i_dst] = ReadNormalized<T, s16>(src.Read<s16, swap>());
144 break;
145 case ComponentFormat::Float:
146 case ComponentFormat::InvalidFloat5:
147 case ComponentFormat::InvalidFloat6:
148 case ComponentFormat::InvalidFloat7:
149 dst[i_dst] = ReadNormalized<T, float>(src.Read<float, swap>());
150 break;
151 }
152
153 ASSERT_MSG(VIDEO, !format.integer || (format.type < ComponentFormat::Float),
154 "only non-float values are allowed to be streamed as integer");
155 }
156 for (; i < components; i++)
157 {
158 int i_dst = reverse ? components - i - 1 : i;
159 dst[i_dst] = i == 3;
160 }
161 }
162 }
163
164 static void ParseColorAttributes(InputVertexData* dst, DataReader& src,
165 const PortableVertexDeclaration& vdec)
166 {
167 const auto set_default_color = [](std::array<u8, 4>& color) {
168 color[Tev::ALP_C] = g_ActiveConfig.iMissingColorValue & 0xFF;
169 color[Tev::BLU_C] = (g_ActiveConfig.iMissingColorValue >> 8) & 0xFF;
170 color[Tev::GRN_C] = (g_ActiveConfig.iMissingColorValue >> 16) & 0xFF;
171 color[Tev::RED_C] = (g_ActiveConfig.iMissingColorValue >> 24) & 0xFF;
172 };
173
174 if (vdec.colors[0].enable)
175 {
176 // Use color0 for channel 0, and color1 for channel 1 if both colors 0 and 1 are present.
177 ReadVertexAttribute<u8>(dst->color[0].data(), src, vdec.colors[0], 0, 4, true);
178 if (vdec.colors[1].enable)
179 ReadVertexAttribute<u8>(dst->color[1].data(), src, vdec.colors[1], 0, 4, true);
180 else
181 set_default_color(dst->color[1]);
182 }
183 else
184 {
185 // If only one of the color attributes is enabled, it is directed to color 0.
186 if (vdec.colors[1].enable)
187 ReadVertexAttribute<u8>(dst->color[0].data(), src, vdec.colors[1], 0, 4, true);
188 else
189 set_default_color(dst->color[0]);
190 set_default_color(dst->color[1]);
191 }
192 }
193
194 void SWVertexLoader::ParseVertex(const PortableVertexDeclaration& vdec, int index)
195 {
196 DataReader src(m_cpu_vertex_buffer.data(),
197 m_cpu_vertex_buffer.data() + m_cpu_vertex_buffer.size());
198 src.Skip(index * vdec.stride);
199
200 ReadVertexAttribute<float>(&m_vertex.position.x, src, vdec.position, 0, 3, false);
201
202 for (std::size_t i = 0; i < m_vertex.normal.size(); i++)
203 {
204 ReadVertexAttribute<float>(&m_vertex.normal[i].x, src, vdec.normals[i], 0, 3, false);
205 }
206 if (!vdec.normals[0].enable)
207 {
208 auto& system = Core::System::GetInstance();
209 auto& vertex_shader_manager = system.GetVertexShaderManager();
210 m_vertex.normal[0].x = vertex_shader_manager.constants.cached_normal[0];
211 m_vertex.normal[0].y = vertex_shader_manager.constants.cached_normal[1];
212 m_vertex.normal[0].z = vertex_shader_manager.constants.cached_normal[2];
213 }
214 if (!vdec.normals[1].enable)
215 {
216 auto& system = Core::System::GetInstance();
217 auto& vertex_shader_manager = system.GetVertexShaderManager();
218 m_vertex.normal[1].x = vertex_shader_manager.constants.cached_tangent[0];
219 m_vertex.normal[1].y = vertex_shader_manager.constants.cached_tangent[1];
220 m_vertex.normal[1].z = vertex_shader_manager.constants.cached_tangent[2];
221 }
222 if (!vdec.normals[2].enable)
223 {
224 auto& system = Core::System::GetInstance();
225 auto& vertex_shader_manager = system.GetVertexShaderManager();
226 m_vertex.normal[2].x = vertex_shader_manager.constants.cached_binormal[0];
227 m_vertex.normal[2].y = vertex_shader_manager.constants.cached_binormal[1];
228 m_vertex.normal[2].z = vertex_shader_manager.constants.cached_binormal[2];
229 }
230
231 ParseColorAttributes(&m_vertex, src, vdec);
232
233 for (std::size_t i = 0; i < m_vertex.texCoords.size(); i++)
234 {
235 ReadVertexAttribute<float>(&m_vertex.texCoords[i].x, src, vdec.texcoords[i], 0, 2, false);
236
237 // the texmtr is stored as third component of the texCoord
238 if (vdec.texcoords[i].components >= 3)
239 {
240 ReadVertexAttribute<u8>(&m_vertex.texMtx[i], src, vdec.texcoords[i], 2, 1, false);
241 }
242 }
243
244 ReadVertexAttribute<u8>(&m_vertex.posMtx, src, vdec.posmtx, 0, 1, false);
245 }