Debian Codegraph @

Source/Core/VideoCommon/XFMemory.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 // X.h defines None to be 0, which causes problems with some of the enums
7 #undef None
8
9 #include <array>
10
11 #include "Common/BitField.h"
12 #include "Common/CommonTypes.h"
13 #include "Common/EnumFormatter.h"
14 #include "VideoCommon/CPMemory.h"
15
16 constexpr size_t NUM_XF_COLOR_CHANNELS = 2;
17
18 // Lighting
19
20 // Projection
21 enum class TexSize : u32
22 {
23 ST = 0,
24 STQ = 1
25 };
26 template <>
27 struct fmt::formatter<TexSize> : EnumFormatter<TexSize::STQ>
28 {
29 constexpr formatter() : EnumFormatter({"ST (2x4 matrix)", "STQ (3x4 matrix)"}) {}
30 };
31
32 // Input form
33 enum class TexInputForm : u32
34 {
35 AB11 = 0,
36 ABC1 = 1
37 };
38 template <>
39 struct fmt::formatter<TexInputForm> : EnumFormatter<TexInputForm::ABC1>
40 {
41 constexpr formatter() : EnumFormatter({"AB11", "ABC1"}) {}
42 };
43
44 enum class NormalCount : u32
45 {
46 None = 0,
47 Normal = 1,
48 NormalTangentBinormal = 2,
49 // Hardware testing indicates that this beahves the same as NormalTangentBinormal.
50 // Call of Duty: Black Ops uses this in some cases; see https://bugs.dolphin-emu.org/issues/13070
51 Invalid = 3,
52 };
53 template <>
54 struct fmt::formatter<NormalCount> : EnumFormatter<NormalCount::Invalid>
55 {
56 static constexpr array_type names = {
57 "None",
58 "Normal only",
59 "Normal, tangent, and binormal",
60 "Invalid (Normal, tangent, and binormal)",
61 };
62 constexpr formatter() : EnumFormatter(names) {}
63 };
64
65 // Texture generation type
66 enum class TexGenType : u32
67 {
68 Regular = 0,
69 EmbossMap = 1, // Used when bump mapping
70 Color0 = 2,
71 Color1 = 3
72 };
73 template <>
74 struct fmt::formatter<TexGenType> : EnumFormatter<TexGenType::Color1>
75 {
76 static constexpr array_type names = {
77 "Regular",
78 "Emboss map (used when bump mapping)",
79 "Color channel 0",
80 "Color channel 1",
81 };
82 constexpr formatter() : EnumFormatter(names) {}
83 };
84
85 // Source row
86 enum class SourceRow : u32
87 {
88 Geom = 0, // Input is abc
89 Normal = 1, // Input is abc
90 Colors = 2,
91 BinormalT = 3, // Input is abc
92 BinormalB = 4, // Input is abc
93 Tex0 = 5,
94 Tex1 = 6,
95 Tex2 = 7,
96 Tex3 = 8,
97 Tex4 = 9,
98 Tex5 = 10,
99 Tex6 = 11,
100 Tex7 = 12
101 };
102 template <>
103 struct fmt::formatter<SourceRow> : EnumFormatter<SourceRow::Tex7>
104 {
105 static constexpr array_type names = {
106 "Geometry (input is ABC1)",
107 "Normal (input is ABC1)",
108 "Colors",
109 "Binormal T (input is ABC1)",
110 "Binormal B (input is ABC1)",
111 "Tex 0",
112 "Tex 1",
113 "Tex 2",
114 "Tex 3",
115 "Tex 4",
116 "Tex 5",
117 "Tex 6",
118 "Tex 7",
119 };
120 constexpr formatter() : EnumFormatter(names) {}
121 };
122
123 enum class MatSource : u32
124 {
125 MatColorRegister = 0,
126 Vertex = 1,
127 };
128 template <>
129 struct fmt::formatter<MatSource> : EnumFormatter<MatSource::Vertex>
130 {
131 constexpr formatter() : EnumFormatter({"Material color register", "Vertex color"}) {}
132 };
133
134 enum class AmbSource : u32
135 {
136 AmbColorRegister = 0,
137 Vertex = 1,
138 };
139 template <>
140 struct fmt::formatter<AmbSource> : EnumFormatter<AmbSource::Vertex>
141 {
142 constexpr formatter() : EnumFormatter({"Ambient color register", "Vertex color"}) {}
143 };
144
145 // Light diffuse attenuation function
146 enum class DiffuseFunc : u32
147 {
148 None = 0,
149 Sign = 1,
150 Clamp = 2
151 };
152 template <>
153 struct fmt::formatter<DiffuseFunc> : EnumFormatter<DiffuseFunc::Clamp>
154 {
155 constexpr formatter() : EnumFormatter({"None", "Sign", "Clamp"}) {}
156 };
157
158 // Light attenuation function
159 enum class AttenuationFunc : u32
160 {
161 None = 0, // No attenuation
162 Spec = 1, // Point light attenuation
163 Dir = 2, // Directional light attenuation
164 Spot = 3 // Spot light attenuation
165 };
166 template <>
167 struct fmt::formatter<AttenuationFunc> : EnumFormatter<AttenuationFunc::Spot>
168 {
169 static constexpr array_type names = {
170 "No attenuation",
171 "Point light attenuation",
172 "Directional light attenuation",
173 "Spot light attenuation",
174 };
175 constexpr formatter() : EnumFormatter(names) {}
176 };
177
178 // Projection type
179 enum class ProjectionType : u32
180 {
181 Perspective = 0,
182 Orthographic = 1
183 };
184 template <>
185 struct fmt::formatter<ProjectionType> : EnumFormatter<ProjectionType::Orthographic>
186 {
187 constexpr formatter() : EnumFormatter({"Perspective", "Orthographic"}) {}
188 };
189
190 // Registers and register ranges
191 enum
192 {
193 XFMEM_POSMATRICES = 0x000,
194 XFMEM_POSMATRICES_END = 0x100,
195 XFMEM_NORMALMATRICES = 0x400,
196 XFMEM_NORMALMATRICES_END = 0x460,
197 XFMEM_POSTMATRICES = 0x500,
198 XFMEM_POSTMATRICES_END = 0x600,
199 XFMEM_LIGHTS = 0x600,
200 XFMEM_LIGHTS_END = 0x680,
201 XFMEM_REGISTERS_START = 0x1000,
202 XFMEM_ERROR = 0x1000,
203 XFMEM_DIAG = 0x1001,
204 XFMEM_STATE0 = 0x1002,
205 XFMEM_STATE1 = 0x1003,
206 XFMEM_CLOCK = 0x1004,
207 XFMEM_CLIPDISABLE = 0x1005,
208 XFMEM_SETGPMETRIC = 0x1006, // Perf0 according to YAGCD
209 XFMEM_UNKNOWN_1007 = 0x1007, // Perf1 according to YAGCD
210 XFMEM_VTXSPECS = 0x1008,
211 XFMEM_SETNUMCHAN = 0x1009,
212 XFMEM_SETCHAN0_AMBCOLOR = 0x100a,
213 XFMEM_SETCHAN1_AMBCOLOR = 0x100b,
214 XFMEM_SETCHAN0_MATCOLOR = 0x100c,
215 XFMEM_SETCHAN1_MATCOLOR = 0x100d,
216 XFMEM_SETCHAN0_COLOR = 0x100e,
217 XFMEM_SETCHAN1_COLOR = 0x100f,
218 XFMEM_SETCHAN0_ALPHA = 0x1010,
219 XFMEM_SETCHAN1_ALPHA = 0x1011,
220 XFMEM_DUALTEX = 0x1012,
221 XFMEM_UNKNOWN_GROUP_1_START = 0x1013,
222 XFMEM_UNKNOWN_GROUP_1_END = 0x1017,
223 XFMEM_SETMATRIXINDA = 0x1018,
224 XFMEM_SETMATRIXINDB = 0x1019,
225 XFMEM_SETVIEWPORT = 0x101a,
226 XFMEM_SETPROJECTION = 0x1020,
227 // XFMEM_SETPROJECTIONB = 0x1021,
228 // XFMEM_SETPROJECTIONC = 0x1022,
229 // XFMEM_SETPROJECTIOND = 0x1023,
230 // XFMEM_SETPROJECTIONE = 0x1024,
231 // XFMEM_SETPROJECTIONF = 0x1025,
232 // XFMEM_SETPROJECTIONORTHO = 0x1026,
233 XFMEM_UNKNOWN_GROUP_2_START = 0x1027,
234 XFMEM_UNKNOWN_GROUP_2_END = 0x103e,
235 XFMEM_SETNUMTEXGENS = 0x103f,
236 XFMEM_SETTEXMTXINFO = 0x1040,
237 XFMEM_UNKNOWN_GROUP_3_START = 0x1048,
238 XFMEM_UNKNOWN_GROUP_3_END = 0x104f,
239 XFMEM_SETPOSTMTXINFO = 0x1050,
240 XFMEM_REGISTERS_END = 0x1058,
241 };
242
243 union LitChannel
244 {
245 BitField<0, 1, MatSource> matsource;
246 BitField<1, 1, bool, u32> enablelighting;
247 BitField<2, 4, u32> lightMask0_3;
248 BitField<6, 1, AmbSource> ambsource;
249 BitField<7, 2, DiffuseFunc> diffusefunc;
250 BitField<9, 2, AttenuationFunc> attnfunc;
251 BitField<11, 4, u32> lightMask4_7;
252 u32 hex;
253
254 unsigned int GetFullLightMask() const
255 {
256 return enablelighting ? (lightMask0_3 | (lightMask4_7 << 4)) : 0;
257 }
258 };
259 template <>
260 struct fmt::formatter<LitChannel>
261 {
262 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
263 template <typename FormatContext>
264 auto format(const LitChannel& chan, FormatContext& ctx) const
265 {
266 return fmt::format_to(
267 ctx.out(),
268 "Material source: {0}\nEnable lighting: {1}\nLight mask: {2:x} ({2:08b})\n"
269 "Ambient source: {3}\nDiffuse function: {4}\nAttenuation function: {5}",
270 chan.matsource, chan.enablelighting ? "Yes" : "No", chan.GetFullLightMask(), chan.ambsource,
271 chan.diffusefunc, chan.attnfunc);
272 }
273 };
274
275 union ClipDisable
276 {
277 BitField<0, 1, bool, u32> disable_clipping_detection;
278 BitField<1, 1, bool, u32> disable_trivial_rejection;
279 BitField<2, 1, bool, u32> disable_cpoly_clipping_acceleration;
280 u32 hex;
281 };
282 template <>
283 struct fmt::formatter<ClipDisable>
284 {
285 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
286 template <typename FormatContext>
287 auto format(const ClipDisable& cd, FormatContext& ctx) const
288 {
289 return fmt::format_to(ctx.out(),
290 "Disable clipping detection: {}\n"
291 "Disable trivial rejection: {}\n"
292 "Disable cpoly clipping acceleration: {}",
293 cd.disable_clipping_detection ? "Yes" : "No",
294 cd.disable_trivial_rejection ? "Yes" : "No",
295 cd.disable_cpoly_clipping_acceleration ? "Yes" : "No");
296 }
297 };
298
299 union INVTXSPEC
300 {
301 BitField<0, 2, u32> numcolors;
302 BitField<2, 2, NormalCount> numnormals;
303 BitField<4, 4, u32> numtextures;
304 u32 hex;
305 };
306 template <>
307 struct fmt::formatter<INVTXSPEC>
308 {
309 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
310 template <typename FormatContext>
311 auto format(const INVTXSPEC& spec, FormatContext& ctx) const
312 {
313 return fmt::format_to(ctx.out(), "Num colors: {}\nNum normals: {}\nNum textures: {}",
314 spec.numcolors, spec.numnormals, spec.numtextures);
315 }
316 };
317
318 union TexMtxInfo
319 {
320 BitField<0, 1, u32> unknown;
321 BitField<1, 1, TexSize> projection;
322 BitField<2, 1, TexInputForm> inputform;
323 BitField<3, 1, u32> unknown2;
324 BitField<4, 3, TexGenType> texgentype;
325 BitField<7, 5, SourceRow> sourcerow;
326 BitField<12, 3, u32> embosssourceshift; // what generated texcoord to use
327 BitField<15, 3, u32> embosslightshift; // light index that is used
328 u32 hex;
329 };
330 template <>
331 struct fmt::formatter<TexMtxInfo>
332 {
333 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
334 template <typename FormatContext>
335 auto format(const TexMtxInfo& i, FormatContext& ctx) const
336 {
337 return fmt::format_to(ctx.out(),
338 "Projection: {}\nInput form: {}\nTex gen type: {}\n"
339 "Source row: {}\nEmboss source shift: {}\nEmboss light shift: {}",
340 i.projection, i.inputform, i.texgentype, i.sourcerow, i.embosssourceshift,
341 i.embosslightshift);
342 }
343 };
344
345 union PostMtxInfo
346 {
347 BitField<0, 6, u32> index; // base row of dual transform matrix
348 BitField<6, 2, u32> unused; //
349 BitField<8, 1, bool, u32> normalize; // normalize before send operation
350 u32 hex;
351 };
352
353 template <>
354 struct fmt::formatter<PostMtxInfo>
355 {
356 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
357 template <typename FormatContext>
358 auto format(const PostMtxInfo& i, FormatContext& ctx) const
359 {
360 return fmt::format_to(ctx.out(), "Index: {}\nNormalize before send operation: {}", i.index,
361 i.normalize ? "Yes" : "No");
362 }
363 };
364
365 union NumColorChannel
366 {
367 BitField<0, 2, u32> numColorChans;
368 u32 hex;
369 };
370
371 union NumTexGen
372 {
373 BitField<0, 4, u32> numTexGens;
374 u32 hex;
375 };
376
377 union DualTexInfo
378 {
379 BitField<0, 1, bool, u32> enabled;
380 u32 hex;
381 };
382
383 struct Light
384 {
385 u32 useless[3];
386 u8 color[4];
387 float cosatt[3]; // cos attenuation
388 float distatt[3]; // dist attenuation
389
390 union
391 {
392 struct
393 {
394 float dpos[3];
395 float ddir[3]; // specular lights only
396 };
397
398 struct
399 {
400 float sdir[3];
401 float shalfangle[3]; // specular lights only
402 };
403 };
404 };
405
406 struct Viewport
407 {
408 float wd;
409 float ht;
410 float zRange;
411 float xOrig;
412 float yOrig;
413 float farZ;
414 };
415
416 struct Projection
417 {
418 using Raw = std::array<float, 6>;
419
420 Raw rawProjection;
421 ProjectionType type;
422 };
423
424 struct alignas(16) XFMemory
425 {
426 float posMatrices[256]; // 0x0000 - 0x00ff
427 u32 unk0[768]; // 0x0100 - 0x03ff
428 float normalMatrices[96]; // 0x0400 - 0x045f
429 u32 unk1[160]; // 0x0460 - 0x04ff
430 float postMatrices[256]; // 0x0500 - 0x05ff
431 Light lights[8]; // 0x0600 - 0x067f
432 u32 unk2[2432]; // 0x0680 - 0x0fff
433 u32 error; // 0x1000
434 u32 diag; // 0x1001
435 u32 state0; // 0x1002
436 u32 state1; // 0x1003
437 u32 xfClock; // 0x1004
438 ClipDisable clipDisable; // 0x1005
439 u32 perf0; // 0x1006
440 u32 perf1; // 0x1007
441 INVTXSPEC invtxspec; // 0x1008
442 NumColorChannel numChan; // 0x1009
443 u32 ambColor[NUM_XF_COLOR_CHANNELS]; // 0x100a, 0x100b
444 u32 matColor[NUM_XF_COLOR_CHANNELS]; // 0x100c, 0x100d
445 LitChannel color[NUM_XF_COLOR_CHANNELS]; // 0x100e, 0x100f
446 LitChannel alpha[NUM_XF_COLOR_CHANNELS]; // 0x1010, 0x1011
447 DualTexInfo dualTexTrans; // 0x1012
448 u32 unk3; // 0x1013
449 u32 unk4; // 0x1014
450 u32 unk5; // 0x1015
451 u32 unk6; // 0x1016
452 u32 unk7; // 0x1017
453 TMatrixIndexA MatrixIndexA; // 0x1018
454 TMatrixIndexB MatrixIndexB; // 0x1019
455 Viewport viewport; // 0x101a - 0x101f
456 Projection projection; // 0x1020 - 0x1026
457 u32 unk8[24]; // 0x1027 - 0x103e
458 NumTexGen numTexGen; // 0x103f
459 TexMtxInfo texMtxInfo[8]; // 0x1040 - 0x1047
460 u32 unk9[8]; // 0x1048 - 0x104f
461 PostMtxInfo postMtxInfo[8]; // 0x1050 - 0x1057
462 };
463 static_assert(sizeof(XFMemory) == sizeof(u32) * XFMEM_REGISTERS_END);
464
465 extern XFMemory xfmem;
466
467 void LoadXFReg(u16 base_address, u8 transfer_size, const u8* data);
468 void LoadIndexedXF(CPArray array, u32 index, u16 address, u8 size);
469 void PreprocessIndexedXF(CPArray array, u32 index, u16 address, u8 size);