Debian Codegraph @

Source/Core/VideoCommon/OpcodeDecoding.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 <concepts>
7
8 #include "Common/Assert.h"
9 #include "Common/CommonTypes.h"
10 #include "Common/EnumFormatter.h"
11 #include "Common/Inline.h"
12 #include "Common/Swap.h"
13 #include "VideoCommon/CPMemory.h"
14 #include "VideoCommon/VertexLoaderBase.h"
15
16 struct CPState;
17 class DataReader;
18
19 namespace OpcodeDecoder
20 {
21 // Global flag to signal if FifoRecorder is active.
22 extern bool g_record_fifo_data;
23
24 enum class Opcode
25 {
26 GX_NOP = 0x00,
27
28 GX_LOAD_BP_REG = 0x61,
29 GX_LOAD_CP_REG = 0x08,
30 GX_LOAD_XF_REG = 0x10,
31 GX_LOAD_INDX_A = 0x20,
32 GX_LOAD_INDX_B = 0x28,
33 GX_LOAD_INDX_C = 0x30,
34 GX_LOAD_INDX_D = 0x38,
35
36 GX_CMD_CALL_DL = 0x40,
37 GX_CMD_UNKNOWN_METRICS = 0x44,
38 GX_CMD_INVL_VC = 0x48,
39
40 GX_PRIMITIVE_START = 0x80,
41 GX_PRIMITIVE_END = 0xbf,
42 };
43
44 constexpr u8 GX_PRIMITIVE_MASK = 0x78;
45 constexpr u32 GX_PRIMITIVE_SHIFT = 3;
46 constexpr u8 GX_VAT_MASK = 0x07;
47
48 // These values are the values extracted using GX_PRIMITIVE_MASK
49 // and GX_PRIMITIVE_SHIFT.
50 // GX_DRAW_QUADS_2 behaves the same way as GX_DRAW_QUADS.
51 enum class Primitive : u8
52 {
53 GX_DRAW_QUADS = 0x0, // 0x80
54 GX_DRAW_QUADS_2 = 0x1, // 0x88
55 GX_DRAW_TRIANGLES = 0x2, // 0x90
56 GX_DRAW_TRIANGLE_STRIP = 0x3, // 0x98
57 GX_DRAW_TRIANGLE_FAN = 0x4, // 0xA0
58 GX_DRAW_LINES = 0x5, // 0xA8
59 GX_DRAW_LINE_STRIP = 0x6, // 0xB0
60 GX_DRAW_POINTS = 0x7 // 0xB8
61 };
62
63 // Interface for the Run and RunCommand functions below.
64 // The functions themselves are templates so that the compiler generates separate versions for each
65 // callback (with the callback functions inlined), so the callback doesn't actually need to be
66 // publicly inherited.
67 // Compilers don't generate warnings for failed inlining with virtual functions, so this define
68 // allows disabling the use of virtual functions to generate those warnings. However, this means
69 // that missing functions will generate errors on their use in RunCommand, instead of in the
70 // subclass, which can be confusing.
71 #define OPCODE_CALLBACK_USE_INHERITANCE
72
73 #ifdef OPCODE_CALLBACK_USE_INHERITANCE
74 #define OPCODE_CALLBACK(sig) DOLPHIN_FORCE_INLINE sig override
75 #define OPCODE_CALLBACK_NOINLINE(sig) sig override
76 #else
77 #define OPCODE_CALLBACK(sig) DOLPHIN_FORCE_INLINE sig
78 #define OPCODE_CALLBACK_NOINLINE(sig) sig
79 #endif
80 class Callback
81 {
82 #ifdef OPCODE_CALLBACK_USE_INHERITANCE
83 public:
84 virtual ~Callback() = default;
85
86 // Called on any XF command.
87 virtual void OnXF(u16 address, u8 count, const u8* data) = 0;
88 // Called on any CP command.
89 // Subclasses should update the CP state with GetCPState().LoadCPReg(command, value) so that
90 // primitive commands decode properly.
91 virtual void OnCP(u8 command, u32 value) = 0;
92 // Called on any BP command.
93 virtual void OnBP(u8 command, u32 value) = 0;
94 // Called on any indexed XF load command.
95 virtual void OnIndexedLoad(CPArray array, u32 index, u16 address, u8 size) = 0;
96 // Called on any primitive command.
97 virtual void OnPrimitiveCommand(OpcodeDecoder::Primitive primitive, u8 vat, u32 vertex_size,
98 u16 num_vertices, const u8* vertex_data) = 0;
99 // Called on a display list.
100 virtual void OnDisplayList(u32 address, u32 size) = 0;
101 // Called on any NOP commands (which are all merged into a single call).
102 virtual void OnNop(u32 count) = 0;
103 // Called on an unknown opcode, or an opcode that is known but not implemented.
104 // data[0] is opcode.
105 virtual void OnUnknown(u8 opcode, const u8* data) = 0;
106
107 // Called on ANY command. The first byte of data is the opcode. Size will be at least 1.
108 // This function is called after one of the above functions is called.
109 virtual void OnCommand(const u8* data, u32 size) = 0;
110
111 // Get the current CP state. Needed for vertex decoding; will also be mutated for CP commands.
112 virtual CPState& GetCPState() = 0;
113
114 virtual u32 GetVertexSize(u8 vat)
115 {
116 const CPState& cpmem = GetCPState();
117 return VertexLoaderBase::GetVertexSize(cpmem.vtx_desc, cpmem.vtx_attr[vat]);
118 }
119 #endif
120 };
121
122 namespace detail
123 {
124 // Main logic; split so that the main RunCommand can call OnCommand with the returned size.
125 static DOLPHIN_FORCE_INLINE u32 RunCommand(const u8* data, u32 available,
126 std::derived_from<Callback> auto& callback)
127 {
128 if (available < 1)
129 return 0;
130
131 const Opcode cmd = static_cast<Opcode>(data[0]);
132
133 switch (cmd)
134 {
135 case Opcode::GX_NOP:
136 {
137 u32 count = 1;
138 while (count < available && static_cast<Opcode>(data[count]) == Opcode::GX_NOP)
139 count++;
140 callback.OnNop(count);
141 return count;
142 }
143
144 case Opcode::GX_LOAD_CP_REG:
145 {
146 if (available < 6)
147 return 0;
148
149 const u8 cmd2 = data[1];
150 const u32 value = Common::swap32(&data[2]);
151
152 callback.OnCP(cmd2, value);
153
154 return 6;
155 }
156
157 case Opcode::GX_LOAD_XF_REG:
158 {
159 if (available < 5)
160 return 0;
161
162 const u32 cmd2 = Common::swap32(&data[1]);
163 const u16 base_address = cmd2 & 0xffff;
164
165 const u16 stream_size_temp = cmd2 >> 16;
166 ASSERT_MSG(VIDEO, stream_size_temp < 16, "cmd2 = 0x{:08X}", cmd2);
167 const u8 stream_size = (stream_size_temp & 0xf) + 1;
168
169 if (available < u32(5 + stream_size * 4))
170 return 0;
171
172 callback.OnXF(base_address, stream_size, &data[5]);
173
174 return 5 + stream_size * 4;
175 }
176
177 case Opcode::GX_LOAD_INDX_A: // Used for position matrices
178 case Opcode::GX_LOAD_INDX_B: // Used for normal matrices
179 case Opcode::GX_LOAD_INDX_C: // Used for postmatrices
180 case Opcode::GX_LOAD_INDX_D: // Used for lights
181 {
182 if (available < 5)
183 return 0;
184
185 const u32 value = Common::swap32(&data[1]);
186
187 const u32 index = value >> 16;
188 const u16 address = value & 0xFFF; // TODO: check mask
189 const u8 size = ((value >> 12) & 0xF) + 1;
190
191 // Map the command byte to its ref array.
192 // GX_LOAD_INDX_A (32 = 8*4) . CPArray::XF_A (4+8 = 12)
193 // GX_LOAD_INDX_B (40 = 8*5) . CPArray::XF_B (5+8 = 13)
194 // GX_LOAD_INDX_C (48 = 8*6) . CPArray::XF_C (6+8 = 14)
195 // GX_LOAD_INDX_D (56 = 8*7) . CPArray::XF_D (7+8 = 15)
196 const auto ref_array = static_cast<CPArray>((static_cast<u8>(cmd) / 8) + 8);
197
198 callback.OnIndexedLoad(ref_array, index, address, size);
199 return 5;
200 }
201
202 case Opcode::GX_CMD_CALL_DL:
203 {
204 if (available < 9)
205 return 0;
206
207 const u32 address = Common::swap32(&data[1]);
208 const u32 size = Common::swap32(&data[5]);
209
210 // Force 32-byte alignment for both the address and the size.
211 callback.OnDisplayList(address & ~31, size & ~31);
212 return 9;
213 }
214
215 case Opcode::GX_LOAD_BP_REG:
216 {
217 if (available < 5)
218 return 0;
219
220 const u8 cmd2 = data[1];
221 const u32 value = Common::swap24(&data[2]);
222
223 callback.OnBP(cmd2, value);
224
225 return 5;
226 }
227
228 default:
229 if (cmd >= Opcode::GX_PRIMITIVE_START && cmd <= Opcode::GX_PRIMITIVE_END)
230 {
231 if (available < 3)
232 return 0;
233
234 const u8 cmdbyte = static_cast<u8>(cmd);
235 const OpcodeDecoder::Primitive primitive = static_cast<OpcodeDecoder::Primitive>(
236 (cmdbyte & OpcodeDecoder::GX_PRIMITIVE_MASK) >> OpcodeDecoder::GX_PRIMITIVE_SHIFT);
237 const u8 vat = cmdbyte & OpcodeDecoder::GX_VAT_MASK;
238
239 const u32 vertex_size = callback.GetVertexSize(vat);
240 const u16 num_vertices = Common::swap16(&data[1]);
241
242 if (available < 3 + num_vertices * vertex_size)
243 return 0;
244
245 callback.OnPrimitiveCommand(primitive, vat, vertex_size, num_vertices, &data[3]);
246
247 return 3 + num_vertices * vertex_size;
248 }
249 }
250
251 callback.OnUnknown(static_cast<u8>(cmd), data);
252 return 1;
253 }
254 } // namespace detail
255
256 DOLPHIN_FORCE_INLINE u32 RunCommand(const u8* data, u32 available,
257 std::derived_from<Callback> auto& callback)
258 {
259 const u32 size = detail::RunCommand(data, available, callback);
260 if (size > 0)
261 {
262 callback.OnCommand(data, size);
263 }
264 return size;
265 }
266
267 DOLPHIN_FORCE_INLINE u32 Run(const u8* data, u32 available,
268 std::derived_from<Callback> auto& callback)
269 {
270 u32 size = 0;
271 while (size < available)
272 {
273 const u32 command_size = RunCommand(&data[size], available - size, callback);
274 if (command_size == 0)
275 break;
276 size += command_size;
277 }
278 return size;
279 }
280
281 template <bool is_preprocess = false>
282 u8* RunFifo(DataReader src, u32* cycles);
283
284 } // namespace OpcodeDecoder
285
286 template <>
287 struct fmt::formatter<OpcodeDecoder::Primitive>
288 : EnumFormatter<OpcodeDecoder::Primitive::GX_DRAW_POINTS>
289 {
290 static constexpr array_type names = {
291 "GX_DRAW_QUADS", "GX_DRAW_QUADS_2 (nonstandard)",
292 "GX_DRAW_TRIANGLES", "GX_DRAW_TRIANGLE_STRIP",
293 "GX_DRAW_TRIANGLE_FAN", "GX_DRAW_LINES",
294 "GX_DRAW_LINE_STRIP", "GX_DRAW_POINTS",
295 };
296 constexpr formatter() : EnumFormatter(names) {}
297 };