src/ds/gx.c (view raw)
1/* Copyright (c) 2013-2017 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include <mgba/internal/ds/gx.h>
7
8#include <mgba/internal/ds/ds.h>
9#include <mgba/internal/ds/io.h>
10
11mLOG_DEFINE_CATEGORY(DS_GX, "DS GX", "ds.gx");
12
13#define DS_GX_FIFO_SIZE 256
14#define DS_GX_PIPE_SIZE 4
15
16static void DSGXDummyRendererInit(struct DSGXRenderer* renderer);
17static void DSGXDummyRendererReset(struct DSGXRenderer* renderer);
18static void DSGXDummyRendererDeinit(struct DSGXRenderer* renderer);
19static void DSGXDummyRendererInvalidateTex(struct DSGXRenderer* renderer, int slot);
20static void DSGXDummyRendererSetRAM(struct DSGXRenderer* renderer, struct DSGXVertex* verts, struct DSGXPolygon* polys, unsigned polyCount, bool wSort);
21static void DSGXDummyRendererDrawScanline(struct DSGXRenderer* renderer, int y);
22static void DSGXDummyRendererGetScanline(struct DSGXRenderer* renderer, int y, const color_t** output);
23
24static void DSGXWriteFIFO(struct DSGX* gx, struct DSGXEntry entry);
25
26static bool _boxTestVertex(struct DSGX* gx, struct DSGXVertex* vertex);
27
28static const int32_t _gxCommandCycleBase[DS_GX_CMD_MAX] = {
29 [DS_GX_CMD_NOP] = 0,
30 [DS_GX_CMD_MTX_MODE] = 2,
31 [DS_GX_CMD_MTX_PUSH] = 34,
32 [DS_GX_CMD_MTX_POP] = 72,
33 [DS_GX_CMD_MTX_STORE] = 34,
34 [DS_GX_CMD_MTX_RESTORE] = 72,
35 [DS_GX_CMD_MTX_IDENTITY] = 38,
36 [DS_GX_CMD_MTX_LOAD_4x4] = 68,
37 [DS_GX_CMD_MTX_LOAD_4x3] = 60,
38 [DS_GX_CMD_MTX_MULT_4x4] = 70,
39 [DS_GX_CMD_MTX_MULT_4x3] = 62,
40 [DS_GX_CMD_MTX_MULT_3x3] = 56,
41 [DS_GX_CMD_MTX_SCALE] = 44,
42 [DS_GX_CMD_MTX_TRANS] = 44,
43 [DS_GX_CMD_COLOR] = 2,
44 [DS_GX_CMD_NORMAL] = 18,
45 [DS_GX_CMD_TEXCOORD] = 2,
46 [DS_GX_CMD_VTX_16] = 18,
47 [DS_GX_CMD_VTX_10] = 16,
48 [DS_GX_CMD_VTX_XY] = 16,
49 [DS_GX_CMD_VTX_XZ] = 16,
50 [DS_GX_CMD_VTX_YZ] = 16,
51 [DS_GX_CMD_VTX_DIFF] = 16,
52 [DS_GX_CMD_POLYGON_ATTR] = 2,
53 [DS_GX_CMD_TEXIMAGE_PARAM] = 2,
54 [DS_GX_CMD_PLTT_BASE] = 2,
55 [DS_GX_CMD_DIF_AMB] = 8,
56 [DS_GX_CMD_SPE_EMI] = 8,
57 [DS_GX_CMD_LIGHT_VECTOR] = 12,
58 [DS_GX_CMD_LIGHT_COLOR] = 2,
59 [DS_GX_CMD_SHININESS] = 64,
60 [DS_GX_CMD_BEGIN_VTXS] = 2,
61 [DS_GX_CMD_END_VTXS] = 2,
62 [DS_GX_CMD_SWAP_BUFFERS] = 784,
63 [DS_GX_CMD_VIEWPORT] = 2,
64 [DS_GX_CMD_BOX_TEST] = 206,
65 [DS_GX_CMD_POS_TEST] = 18,
66 [DS_GX_CMD_VEC_TEST] = 10,
67};
68
69static const int32_t _gxCommandParams[DS_GX_CMD_MAX] = {
70 [DS_GX_CMD_MTX_MODE] = 1,
71 [DS_GX_CMD_MTX_POP] = 1,
72 [DS_GX_CMD_MTX_STORE] = 1,
73 [DS_GX_CMD_MTX_RESTORE] = 1,
74 [DS_GX_CMD_MTX_LOAD_4x4] = 16,
75 [DS_GX_CMD_MTX_LOAD_4x3] = 12,
76 [DS_GX_CMD_MTX_MULT_4x4] = 16,
77 [DS_GX_CMD_MTX_MULT_4x3] = 12,
78 [DS_GX_CMD_MTX_MULT_3x3] = 9,
79 [DS_GX_CMD_MTX_SCALE] = 3,
80 [DS_GX_CMD_MTX_TRANS] = 3,
81 [DS_GX_CMD_COLOR] = 1,
82 [DS_GX_CMD_NORMAL] = 1,
83 [DS_GX_CMD_TEXCOORD] = 1,
84 [DS_GX_CMD_VTX_16] = 2,
85 [DS_GX_CMD_VTX_10] = 1,
86 [DS_GX_CMD_VTX_XY] = 1,
87 [DS_GX_CMD_VTX_XZ] = 1,
88 [DS_GX_CMD_VTX_YZ] = 1,
89 [DS_GX_CMD_VTX_DIFF] = 1,
90 [DS_GX_CMD_POLYGON_ATTR] = 1,
91 [DS_GX_CMD_TEXIMAGE_PARAM] = 1,
92 [DS_GX_CMD_PLTT_BASE] = 1,
93 [DS_GX_CMD_DIF_AMB] = 1,
94 [DS_GX_CMD_SPE_EMI] = 1,
95 [DS_GX_CMD_LIGHT_VECTOR] = 1,
96 [DS_GX_CMD_LIGHT_COLOR] = 1,
97 [DS_GX_CMD_SHININESS] = 32,
98 [DS_GX_CMD_BEGIN_VTXS] = 1,
99 [DS_GX_CMD_SWAP_BUFFERS] = 1,
100 [DS_GX_CMD_VIEWPORT] = 1,
101 [DS_GX_CMD_BOX_TEST] = 3,
102 [DS_GX_CMD_POS_TEST] = 2,
103 [DS_GX_CMD_VEC_TEST] = 1,
104};
105
106static struct DSGXRenderer dummyRenderer = {
107 .init = DSGXDummyRendererInit,
108 .reset = DSGXDummyRendererReset,
109 .deinit = DSGXDummyRendererDeinit,
110 .invalidateTex = DSGXDummyRendererInvalidateTex,
111 .setRAM = DSGXDummyRendererSetRAM,
112 .drawScanline = DSGXDummyRendererDrawScanline,
113 .getScanline = DSGXDummyRendererGetScanline,
114};
115
116static void _pullPipe(struct DSGX* gx) {
117 if (CircleBufferSize(&gx->fifo) >= sizeof(struct DSGXEntry)) {
118 struct DSGXEntry entry = { 0 };
119 CircleBufferRead(&gx->fifo, &entry, sizeof(entry));
120 CircleBufferWrite(&gx->pipe, &entry, sizeof(entry));
121 }
122 if (CircleBufferSize(&gx->fifo) >= sizeof(struct DSGXEntry)) {
123 struct DSGXEntry entry = { 0 };
124 CircleBufferRead(&gx->fifo, &entry, sizeof(entry));
125 CircleBufferWrite(&gx->pipe, &entry, sizeof(entry));
126 }
127}
128
129static void _updateClipMatrix(struct DSGX* gx) {
130 DSGXMtxMultiply(&gx->clipMatrix, &gx->posMatrix, &gx->projMatrix);
131 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_00 >> 1] = gx->clipMatrix.m[0];
132 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_01 >> 1] = gx->clipMatrix.m[0] >> 16;
133 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_02 >> 1] = gx->clipMatrix.m[1];
134 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_03 >> 1] = gx->clipMatrix.m[1] >> 16;
135 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_04 >> 1] = gx->clipMatrix.m[2];
136 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_05 >> 1] = gx->clipMatrix.m[2] >> 16;
137 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_06 >> 1] = gx->clipMatrix.m[3];
138 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_07 >> 1] = gx->clipMatrix.m[3] >> 16;
139 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_08 >> 1] = gx->clipMatrix.m[4];
140 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_09 >> 1] = gx->clipMatrix.m[4] >> 16;
141 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0A >> 1] = gx->clipMatrix.m[5];
142 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0B >> 1] = gx->clipMatrix.m[5] >> 16;
143 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0C >> 1] = gx->clipMatrix.m[6];
144 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0D >> 1] = gx->clipMatrix.m[6] >> 16;
145 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0E >> 1] = gx->clipMatrix.m[7];
146 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0F >> 1] = gx->clipMatrix.m[7] >> 16;
147 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_10 >> 1] = gx->clipMatrix.m[8];
148 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_11 >> 1] = gx->clipMatrix.m[8] >> 16;
149 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_12 >> 1] = gx->clipMatrix.m[9];
150 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_13 >> 1] = gx->clipMatrix.m[9] >> 16;
151 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_14 >> 1] = gx->clipMatrix.m[10];
152 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_15 >> 1] = gx->clipMatrix.m[10] >> 16;
153 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_16 >> 1] = gx->clipMatrix.m[11];
154 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_17 >> 1] = gx->clipMatrix.m[11] >> 16;
155 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_18 >> 1] = gx->clipMatrix.m[12];
156 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_19 >> 1] = gx->clipMatrix.m[12] >> 16;
157 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1A >> 1] = gx->clipMatrix.m[13];
158 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1B >> 1] = gx->clipMatrix.m[13] >> 16;
159 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1C >> 1] = gx->clipMatrix.m[14];
160 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1D >> 1] = gx->clipMatrix.m[14] >> 16;
161 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1E >> 1] = gx->clipMatrix.m[15];
162 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1F >> 1] = gx->clipMatrix.m[15] >> 16;
163}
164
165static inline int32_t _lerp(int32_t x0, int32_t x1, int32_t q, int64_t r) {
166 int64_t x = x1 - x0;
167 x *= q;
168 x /= r;
169 x += x0;
170 return x;
171}
172
173static int _cohenSutherlandCode(const struct DSGXVertex* v) {
174 int code = 0;
175 if (v->viewCoord[0] < -v->viewCoord[3]) {
176 code |= 1 << 0;
177 } else if (v->viewCoord[0] > v->viewCoord[3]) {
178 code |= 2 << 0;
179 }
180 if (v->viewCoord[1] < -v->viewCoord[3]) {
181 code |= 1 << 2;
182 } else if (v->viewCoord[1] > v->viewCoord[3]) {
183 code |= 2 << 2;
184 }
185 if (v->viewCoord[2] < -v->viewCoord[3]) {
186 code |= 1 << 4;
187 } else if (v->viewCoord[2] > v->viewCoord[3]) {
188 code |= 2 << 4;
189 }
190 return code;
191}
192
193static bool _lerpVertex(const struct DSGXVertex* v0, const struct DSGXVertex* v1, struct DSGXVertex* out, int32_t q, int64_t r) {
194 if (!r) {
195 return false;
196 }
197 int cr0 = (v0->color) & 0x1F;
198 int cg0 = (v0->color >> 5) & 0x1F;
199 int cb0 = (v0->color >> 10) & 0x1F;
200 int cr1 = (v1->color) & 0x1F;
201 int cg1 = (v1->color >> 5) & 0x1F;
202 int cb1 = (v1->color >> 10) & 0x1F;
203 cr0 = _lerp(cr0, cr1, q, r) & 0x1F;
204 cg0 = _lerp(cg0, cg1, q, r) & 0x1F;
205 cb0 = _lerp(cb0, cb1, q, r) & 0x1F;
206 out->color = cr0 | (cg0 << 5) | (cb0 << 10);
207
208 out->viewCoord[0] = _lerp(v0->viewCoord[0], v1->viewCoord[0], q, r);
209 out->viewCoord[1] = _lerp(v0->viewCoord[1], v1->viewCoord[1], q, r);
210 out->viewCoord[2] = _lerp(v0->viewCoord[2], v1->viewCoord[2], q, r);
211 out->viewCoord[3] = _lerp(v0->viewCoord[3], v1->viewCoord[3], q, r);
212
213 out->vs = _lerp(v0->vs, v1->vs, q, r);
214 out->vt = _lerp(v0->vt, v1->vt, q, r);
215 return true;
216}
217
218static bool _lerpVertexP(const struct DSGXVertex* v0, const struct DSGXVertex* v1, struct DSGXVertex* out, int plane, int sign) {
219 int32_t q = v0->viewCoord[3] - sign * v0->viewCoord[plane];
220 int64_t r = q - (v1->viewCoord[3] - sign * v1->viewCoord[plane]);
221 return _lerpVertex(v0, v1, out, q, r);
222}
223
224static bool _clipPolygon(struct DSGX* gx, struct DSGXPolygon* poly) {
225 int nOffscreen = 0;
226 int offscreenVerts[10] = { 0, 0, 0, 0 };
227 unsigned oldVerts[4];
228 int v;
229
230 if (!DSGXPolygonAttrsIsBackFace(poly->polyParams) || !DSGXPolygonAttrsIsFrontFace(poly->polyParams)) {
231 // Calculate normal direction and camera dot product average
232 int64_t nx = 0;
233 int64_t ny = 0;
234 int64_t nz = 0;
235 int64_t dot = 0;
236 for (v = 0; v < poly->verts; ++v) {
237 struct DSGXVertex* v0 = &gx->pendingVertices[poly->vertIds[v]];
238 struct DSGXVertex* v1;
239 struct DSGXVertex* v2;
240 if (v < poly->verts - 2) {
241 v1 = &gx->pendingVertices[poly->vertIds[v + 1]];
242 v2 = &gx->pendingVertices[poly->vertIds[v + 2]];
243 } else if (v < poly->verts - 1) {
244 v1 = &gx->pendingVertices[poly->vertIds[v + 1]];
245 v2 = &gx->pendingVertices[poly->vertIds[v + 2 - poly->verts]];
246 } else {
247 v1 = &gx->pendingVertices[poly->vertIds[v + 1 - poly->verts]];
248 v2 = &gx->pendingVertices[poly->vertIds[v + 2 - poly->verts]];
249 }
250 nx = ((int64_t) v0->viewCoord[1] * v2->viewCoord[3] - (int64_t) v0->viewCoord[3] * v2->viewCoord[1]) >> 24;
251 ny = ((int64_t) v0->viewCoord[3] * v2->viewCoord[0] - (int64_t) v0->viewCoord[0] * v2->viewCoord[3]) >> 24;
252 nz = ((int64_t) v0->viewCoord[0] * v2->viewCoord[1] - (int64_t) v0->viewCoord[1] * v2->viewCoord[0]) >> 24;
253 dot += nx * v1->viewCoord[0] + ny * v1->viewCoord[1] + nz * v1->viewCoord[3];
254 }
255 if (!DSGXPolygonAttrsIsBackFace(poly->polyParams) && dot < 0) {
256 return false;
257 }
258 if (!DSGXPolygonAttrsIsFrontFace(poly->polyParams) && dot > 0) {
259 return false;
260 }
261 }
262
263 // Collect offscreen vertices
264 for (v = 0; v < poly->verts; ++v) {
265 offscreenVerts[v] = _cohenSutherlandCode(&gx->pendingVertices[poly->vertIds[v]]);
266 oldVerts[v] = poly->vertIds[v];
267 if (offscreenVerts[v]) {
268 ++nOffscreen;
269 }
270 }
271
272 struct DSGXVertex* vbuf = gx->vertexBuffer[gx->bufferIndex];
273
274 if (!nOffscreen) {
275 for (v = 0; v < poly->verts; ++v) {
276 if (gx->vertexIndex == DS_GX_VERTEX_BUFFER_SIZE) {
277 return false;
278 }
279 int vertexId = oldVerts[v];
280 if (gx->pendingVertexIds[vertexId] >= 0) {
281 poly->vertIds[v] = gx->pendingVertexIds[vertexId];
282 } else {
283 vbuf[gx->vertexIndex] = gx->pendingVertices[vertexId];
284 gx->pendingVertexIds[vertexId] = gx->vertexIndex;
285 poly->vertIds[v] = gx->vertexIndex;
286 ++gx->vertexIndex;
287 }
288 }
289 return true;
290 }
291
292 struct DSGXVertex inList[10];
293 struct DSGXVertex outList[10];
294 int outOffscreenVerts[10] = { 0, 0, 0, 0 };
295 for (v = 0; v < poly->verts; ++v) {
296 inList[v] = gx->pendingVertices[oldVerts[v]];
297 }
298
299 int newV;
300
301 int plane;
302 for (plane = 5; plane >= 0; --plane) {
303 newV = 0;
304 for (v = 0; v < poly->verts; ++v) {
305 if (!(offscreenVerts[v] & (1 << plane))) {
306 outList[newV] = inList[v];
307 outOffscreenVerts[newV] = offscreenVerts[v];
308 ++newV;
309 } else {
310 struct DSGXVertex* in = &inList[v];
311 struct DSGXVertex* in2;
312 struct DSGXVertex* out;
313 int iv;
314
315 if (v > 0) {
316 iv = v - 1;
317 } else {
318 iv = poly->verts - 1;
319 }
320 if (!(offscreenVerts[iv] & (1 << plane))) {
321 in2 = &inList[iv];
322 out = &outList[newV];
323 if (_lerpVertexP(in, in2, out, plane >> 1, -1 + (plane & 1) * 2)) {
324 outOffscreenVerts[newV] = _cohenSutherlandCode(out);
325 ++newV;
326 }
327 }
328
329 if (v < poly->verts - 1) {
330 iv = v + 1;
331 } else {
332 iv = 0;
333 }
334 if (!(offscreenVerts[iv] & (1 << plane))) {
335 in2 = &inList[iv];
336 out = &outList[newV];
337 if (_lerpVertexP(in, in2, out, plane >> 1, -1 + (plane & 1) * 2)) {
338 outOffscreenVerts[newV] = _cohenSutherlandCode(out);
339 ++newV;
340 }
341 }
342 }
343 }
344 poly->verts = newV;
345 memcpy(inList, outList, newV * sizeof(*inList));
346 memcpy(offscreenVerts, outOffscreenVerts, newV * sizeof(*offscreenVerts));
347 }
348
349 for (v = 0; v < poly->verts; ++v) {
350 if (gx->vertexIndex == DS_GX_VERTEX_BUFFER_SIZE) {
351 return false;
352 }
353 // TODO: merge strips
354 vbuf[gx->vertexIndex] = inList[v];
355 poly->vertIds[v] = gx->vertexIndex;
356 ++gx->vertexIndex;
357 }
358
359 return newV > 2;
360}
361
362static int32_t _dotViewport(struct DSGXVertex* vertex, int32_t* col) {
363 int64_t a;
364 int64_t b;
365 int64_t sum;
366 a = col[0];
367 b = vertex->coord[0];
368 sum = a * b;
369 a = col[4];
370 b = vertex->coord[1];
371 sum += a * b;
372 a = col[8];
373 b = vertex->coord[2];
374 sum += a * b;
375 a = col[12];
376 b = MTX_ONE;
377 sum += a * b;
378 return sum >> 8LL;
379}
380
381static int16_t _dotTexture(struct DSGXVertex* vertex, int mode, int32_t* col) {
382 int64_t a;
383 int64_t b;
384 int64_t sum;
385 switch (mode) {
386 case 1:
387 a = col[0];
388 b = vertex->s << 8;
389 sum = a * b;
390 a = col[4];
391 b = vertex->t << 8;
392 sum += a * b;
393 a = col[8];
394 b = MTX_ONE >> 4;
395 sum += a * b;
396 a = col[12];
397 b = MTX_ONE >> 4;
398 sum += a * b;
399 break;
400 case 2:
401 return 0;
402 case 3:
403 a = col[0];
404 b = vertex->viewCoord[0] << 8;
405 sum = a * b;
406 a = col[4];
407 b = vertex->viewCoord[1] << 8;
408 sum += a * b;
409 a = col[8];
410 b = vertex->viewCoord[2] << 8;
411 sum += a * b;
412 a = col[12];
413 b = MTX_ONE;
414 sum += a * b;
415 }
416 return sum >> 20;
417}
418
419static int32_t _dotFrac(int16_t x, int16_t y, int16_t z, int32_t* col) {
420 int64_t a;
421 int64_t b;
422 int64_t sum;
423 a = col[0];
424 b = x;
425 sum = a * b;
426 a = col[4];
427 b = y;
428 sum += a * b;
429 a = col[8];
430 b = z;
431 sum += a * b;
432 return sum >> 12;
433}
434
435static int16_t _dot3(int32_t x0, int32_t y0, int32_t z0, int32_t x1, int32_t y1, int32_t z1) {
436 int32_t a = x0 * x1;
437 a += y0 * y1;
438 a += z0 * z1;
439 a >>= 12;
440 return a;
441}
442
443static void _emitVertex(struct DSGX* gx, uint16_t x, uint16_t y, uint16_t z) {
444 if (gx->vertexMode < 0 || gx->vertexIndex == DS_GX_VERTEX_BUFFER_SIZE || gx->polygonIndex == DS_GX_POLYGON_BUFFER_SIZE) {
445 return;
446 }
447 gx->currentVertex.coord[0] = x;
448 gx->currentVertex.coord[1] = y;
449 gx->currentVertex.coord[2] = z;
450 gx->currentVertex.viewCoord[0] = _dotViewport(&gx->currentVertex, &gx->clipMatrix.m[0]);
451 gx->currentVertex.viewCoord[1] = _dotViewport(&gx->currentVertex, &gx->clipMatrix.m[1]);
452 gx->currentVertex.viewCoord[2] = _dotViewport(&gx->currentVertex, &gx->clipMatrix.m[2]);
453 gx->currentVertex.viewCoord[3] = _dotViewport(&gx->currentVertex, &gx->clipMatrix.m[3]);
454
455 if (DSGXTexParamsGetCoordTfMode(gx->currentPoly.texParams) == 0) {
456 gx->currentVertex.vs = gx->currentVertex.s;
457 gx->currentVertex.vt = gx->currentVertex.t;
458 } else if (DSGXTexParamsGetCoordTfMode(gx->currentPoly.texParams) == 3) {
459 int32_t m12 = gx->texMatrix.m[12];
460 int32_t m13 = gx->texMatrix.m[13];
461 gx->texMatrix.m[12] = gx->currentVertex.s;
462 gx->texMatrix.m[13] = gx->currentVertex.t;
463 gx->currentVertex.vs = _dotTexture(&gx->currentVertex, 3, &gx->texMatrix.m[0]);
464 gx->currentVertex.vt = _dotTexture(&gx->currentVertex, 3, &gx->texMatrix.m[1]);
465 gx->texMatrix.m[12] = m12;
466 gx->texMatrix.m[13] = m13;
467 }
468
469 gx->pendingVertices[gx->pendingVertexIndex] = gx->currentVertex;
470 gx->currentPoly.vertIds[gx->currentPoly.verts] = gx->pendingVertexIndex;
471 gx->pendingVertexIndex = (gx->pendingVertexIndex + 1) & 3;
472
473 ++gx->currentPoly.verts;
474 int totalVertices;
475 switch (gx->vertexMode) {
476 case 0:
477 case 2:
478 totalVertices = 3;
479 break;
480 case 1:
481 case 3:
482 totalVertices = 4;
483 break;
484 }
485 if (gx->currentPoly.verts == totalVertices) {
486 struct DSGXPolygon* pbuf = gx->polygonBuffer[gx->bufferIndex];
487
488 pbuf[gx->polygonIndex] = gx->currentPoly;
489 switch (gx->vertexMode) {
490 case 0:
491 case 1:
492 gx->currentPoly.verts = 0;
493 break;
494 case 2:
495 // Reverse winding if needed
496 if (gx->reverseWinding) {
497 pbuf[gx->polygonIndex].vertIds[1] = gx->currentPoly.vertIds[2];
498 pbuf[gx->polygonIndex].vertIds[2] = gx->currentPoly.vertIds[1];
499 }
500 gx->reverseWinding = !gx->reverseWinding;
501 gx->currentPoly.vertIds[0] = gx->currentPoly.vertIds[1];
502 gx->currentPoly.vertIds[1] = gx->currentPoly.vertIds[2];
503 gx->currentPoly.verts = 2;
504 break;
505 case 3:
506 gx->currentPoly.vertIds[0] = gx->currentPoly.vertIds[2];
507 gx->currentPoly.vertIds[1] = gx->currentPoly.vertIds[3];
508 // Ensure quads don't cross over
509 pbuf[gx->polygonIndex].vertIds[2] = gx->currentPoly.vertIds[3];
510 pbuf[gx->polygonIndex].vertIds[3] = gx->currentPoly.vertIds[2];
511 gx->currentPoly.verts = 2;
512 break;
513 }
514
515 if (_clipPolygon(gx, &pbuf[gx->polygonIndex])) {
516 ++gx->polygonIndex;
517 }
518 if (gx->vertexMode < 2) {
519 memset(gx->pendingVertexIds, -1, sizeof(gx->pendingVertexIds));
520 } else {
521 gx->pendingVertexIds[gx->pendingVertexIndex] = -1;
522 gx->pendingVertexIds[(gx->pendingVertexIndex + 1) & 3] = -1;
523 }
524 }
525}
526
527static void _flushOutstanding(struct DSGX* gx) {
528 if (gx->p->cpuBlocked & DS_CPU_BLOCK_GX) {
529 gx->p->cpuBlocked &= ~DS_CPU_BLOCK_GX;
530 DSGXWriteFIFO(gx, gx->outstandingEntry);
531 gx->outstandingEntry.command = 0;
532 }
533 while (gx->outstandingCommand[0] && !gx->outstandingParams[0]) {
534 DSGXWriteFIFO(gx, (struct DSGXEntry) { 0 });
535 if (CircleBufferSize(&gx->fifo) == (DS_GX_FIFO_SIZE * sizeof(struct DSGXEntry))) {
536 return;
537 }
538 }
539}
540
541static bool _boxTestVertex(struct DSGX* gx, struct DSGXVertex* vertex) {
542 int32_t vx = _dotViewport(vertex, &gx->clipMatrix.m[0]);
543 int32_t vy = _dotViewport(vertex, &gx->clipMatrix.m[1]);
544 int32_t vz = _dotViewport(vertex, &gx->clipMatrix.m[2]);
545 int32_t vw = _dotViewport(vertex, &gx->clipMatrix.m[3]);
546
547 vx = (vx + vw) * (int64_t) (gx->viewportWidth << 12) / (vw * 2) + (gx->viewportX1 << 12);
548 vy = (vy + vw) * (int64_t) (gx->viewportHeight << 12) / (vw * 2) + (gx->viewportY1 << 12);
549 vx >>= 12;
550 vy >>= 12;
551
552 if (vx < gx->viewportX1) {
553 return false;
554 }
555 if (vx > gx->viewportX2) {
556 return false;
557 }
558 if (vy < gx->viewportY1) {
559 return false;
560 }
561 if (vy > gx->viewportY2) {
562 return false;
563 }
564 if (vz < -vw) {
565 return false;
566 }
567 if (vz > vw) {
568 return false;
569 }
570 return true;
571}
572
573static bool _boxTest(struct DSGX* gx) {
574 int16_t x = gx->activeEntries[0].params[0];
575 x |= gx->activeEntries[0].params[1] << 8;
576 int16_t y = gx->activeEntries[0].params[2];
577 y |= gx->activeEntries[0].params[3] << 8;
578 int16_t z = gx->activeEntries[1].params[0];
579 z |= gx->activeEntries[1].params[1] << 8;
580 int16_t w = gx->activeEntries[1].params[2];
581 w |= gx->activeEntries[1].params[3] << 8;
582 int16_t h = gx->activeEntries[2].params[0];
583 h |= gx->activeEntries[2].params[1] << 8;
584 int16_t d = gx->activeEntries[2].params[2];
585 d |= gx->activeEntries[2].params[3] << 8;
586
587 struct DSGXVertex vertex = {
588 .coord = { x, y, z }
589 };
590 if (_boxTestVertex(gx, &vertex)) {
591 return true;
592 }
593
594 vertex.coord[0] += w;
595 if (_boxTestVertex(gx, &vertex)) {
596 return true;
597 }
598
599 vertex.coord[0] = x;
600 vertex.coord[1] += h;
601 if (_boxTestVertex(gx, &vertex)) {
602 return true;
603 }
604
605 vertex.coord[0] += w;
606 if (_boxTestVertex(gx, &vertex)) {
607 return true;
608 }
609
610 vertex.coord[0] = x;
611 vertex.coord[1] = y;
612 vertex.coord[2] += d;
613 if (_boxTestVertex(gx, &vertex)) {
614 return true;
615 }
616
617 vertex.coord[0] += w;
618 if (_boxTestVertex(gx, &vertex)) {
619 return true;
620 }
621
622 vertex.coord[0] = x;
623 vertex.coord[1] += h;
624 if (_boxTestVertex(gx, &vertex)) {
625 return true;
626 }
627
628 vertex.coord[0] += w;
629 if (_boxTestVertex(gx, &vertex)) {
630 return true;
631 }
632
633 return false;
634}
635
636static void _fifoRun(struct mTiming* timing, void* context, uint32_t cyclesLate) {
637 struct DSGX* gx = context;
638 uint32_t cycles;
639 bool first = true;
640 while (!gx->swapBuffers) {
641 if (CircleBufferSize(&gx->pipe) <= 2 * sizeof(struct DSGXEntry)) {
642 _pullPipe(gx);
643 }
644
645 if (!CircleBufferSize(&gx->pipe)) {
646 cycles = 0;
647 break;
648 }
649
650 DSRegGXSTAT gxstat = gx->p->memory.io9[DS9_REG_GXSTAT_LO >> 1];
651 int projMatrixPointer = DSRegGXSTATGetProjMatrixStackLevel(gxstat);
652
653 struct DSGXEntry entry = { 0 };
654 CircleBufferDump(&gx->pipe, (int8_t*) &entry.command, 1);
655 cycles = _gxCommandCycleBase[entry.command];
656
657 if (first) {
658 first = false;
659 } else if (!gx->activeParams && cycles > cyclesLate) {
660 break;
661 }
662 CircleBufferRead(&gx->pipe, &entry, sizeof(entry));
663
664 if (gx->activeParams) {
665 int index = _gxCommandParams[entry.command] - gx->activeParams;
666 gx->activeEntries[index] = entry;
667 --gx->activeParams;
668 } else {
669 gx->activeParams = _gxCommandParams[entry.command];
670 if (gx->activeParams) {
671 --gx->activeParams;
672 }
673 if (gx->activeParams) {
674 gx->activeEntries[0] = entry;
675 }
676 }
677
678 if (gx->activeParams) {
679 continue;
680 }
681
682 switch (entry.command) {
683 case DS_GX_CMD_MTX_MODE:
684 if (entry.params[0] < 4) {
685 gx->mtxMode = entry.params[0];
686 } else {
687 mLOG(DS_GX, GAME_ERROR, "Invalid GX MTX_MODE %02X", entry.params[0]);
688 }
689 break;
690 case DS_GX_CMD_MTX_PUSH:
691 switch (gx->mtxMode) {
692 case 0:
693 memcpy(&gx->projMatrixStack, &gx->projMatrix, sizeof(gx->projMatrix));
694 ++projMatrixPointer;
695 break;
696 case 2:
697 memcpy(&gx->vecMatrixStack[gx->pvMatrixPointer & 0x1F], &gx->vecMatrix, sizeof(gx->vecMatrix));
698 // Fall through
699 case 1:
700 memcpy(&gx->posMatrixStack[gx->pvMatrixPointer & 0x1F], &gx->posMatrix, sizeof(gx->posMatrix));
701 ++gx->pvMatrixPointer;
702 break;
703 case 3:
704 mLOG(DS_GX, STUB, "Unimplemented GX MTX_PUSH mode");
705 break;
706 }
707 break;
708 case DS_GX_CMD_MTX_POP: {
709 int8_t offset = entry.params[0];
710 offset <<= 2;
711 offset >>= 2;
712 switch (gx->mtxMode) {
713 case 0:
714 projMatrixPointer -= offset;
715 memcpy(&gx->projMatrix, &gx->projMatrixStack, sizeof(gx->projMatrix));
716 break;
717 case 1:
718 gx->pvMatrixPointer -= offset;
719 memcpy(&gx->posMatrix, &gx->posMatrixStack[gx->pvMatrixPointer & 0x1F], sizeof(gx->posMatrix));
720 break;
721 case 2:
722 gx->pvMatrixPointer -= offset;
723 memcpy(&gx->vecMatrix, &gx->vecMatrixStack[gx->pvMatrixPointer & 0x1F], sizeof(gx->vecMatrix));
724 memcpy(&gx->posMatrix, &gx->posMatrixStack[gx->pvMatrixPointer & 0x1F], sizeof(gx->posMatrix));
725 break;
726 case 3:
727 mLOG(DS_GX, STUB, "Unimplemented GX MTX_POP mode");
728 break;
729 }
730 _updateClipMatrix(gx);
731 break;
732 }
733 case DS_GX_CMD_MTX_STORE: {
734 int8_t offset = entry.params[0] & 0x1F;
735 // TODO: overflow
736 switch (gx->mtxMode) {
737 case 0:
738 memcpy(&gx->projMatrixStack, &gx->projMatrix, sizeof(gx->projMatrixStack));
739 break;
740 case 2:
741 memcpy(&gx->vecMatrixStack[offset], &gx->vecMatrix, sizeof(gx->vecMatrix));
742 // Fall through
743 case 1:
744 memcpy(&gx->posMatrixStack[offset], &gx->posMatrix, sizeof(gx->posMatrix));
745 break;
746 case 3:
747 mLOG(DS_GX, STUB, "Unimplemented GX MTX_STORE mode");
748 break;
749 }
750 break;
751 }
752 case DS_GX_CMD_MTX_RESTORE: {
753 int8_t offset = entry.params[0] & 0x1F;
754 // TODO: overflow
755 switch (gx->mtxMode) {
756 case 0:
757 memcpy(&gx->projMatrix, &gx->projMatrixStack, sizeof(gx->projMatrix));
758 break;
759 case 2:
760 memcpy(&gx->vecMatrix, &gx->vecMatrixStack[offset], sizeof(gx->vecMatrix));
761 // Fall through
762 case 1:
763 memcpy(&gx->posMatrix, &gx->posMatrixStack[offset], sizeof(gx->posMatrix));
764 break;
765 case 3:
766 mLOG(DS_GX, STUB, "Unimplemented GX MTX_RESTORE mode");
767 break;
768 }
769 _updateClipMatrix(gx);
770 break;
771 }
772 case DS_GX_CMD_MTX_IDENTITY:
773 switch (gx->mtxMode) {
774 case 0:
775 DSGXMtxIdentity(&gx->projMatrix);
776 break;
777 case 2:
778 DSGXMtxIdentity(&gx->vecMatrix);
779 // Fall through
780 case 1:
781 DSGXMtxIdentity(&gx->posMatrix);
782 break;
783 case 3:
784 DSGXMtxIdentity(&gx->texMatrix);
785 break;
786 }
787 _updateClipMatrix(gx);
788 break;
789 case DS_GX_CMD_MTX_LOAD_4x4: {
790 struct DSGXMatrix m;
791 int i;
792 for (i = 0; i < 16; ++i) {
793 m.m[i] = gx->activeEntries[i].params[0];
794 m.m[i] |= gx->activeEntries[i].params[1] << 8;
795 m.m[i] |= gx->activeEntries[i].params[2] << 16;
796 m.m[i] |= gx->activeEntries[i].params[3] << 24;
797 }
798 switch (gx->mtxMode) {
799 case 0:
800 memcpy(&gx->projMatrix, &m, sizeof(gx->projMatrix));
801 break;
802 case 2:
803 memcpy(&gx->vecMatrix, &m, sizeof(gx->vecMatrix));
804 // Fall through
805 case 1:
806 memcpy(&gx->posMatrix, &m, sizeof(gx->posMatrix));
807 break;
808 case 3:
809 memcpy(&gx->texMatrix, &m, sizeof(gx->texMatrix));
810 break;
811 }
812 _updateClipMatrix(gx);
813 break;
814 }
815 case DS_GX_CMD_MTX_LOAD_4x3: {
816 struct DSGXMatrix m;
817 int i, j;
818 for (j = 0; j < 4; ++j) {
819 for (i = 0; i < 3; ++i) {
820 m.m[i + j * 4] = gx->activeEntries[i + j * 3].params[0];
821 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[1] << 8;
822 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[2] << 16;
823 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[3] << 24;
824 }
825 m.m[j * 4 + 3] = 0;
826 }
827 m.m[15] = MTX_ONE;
828 switch (gx->mtxMode) {
829 case 0:
830 memcpy(&gx->projMatrix, &m, sizeof(gx->projMatrix));
831 break;
832 case 2:
833 memcpy(&gx->vecMatrix, &m, sizeof(gx->vecMatrix));
834 // Fall through
835 case 1:
836 memcpy(&gx->posMatrix, &m, sizeof(gx->posMatrix));
837 break;
838 case 3:
839 memcpy(&gx->texMatrix, &m, sizeof(gx->texMatrix));
840 break;
841 }
842 _updateClipMatrix(gx);
843 break;
844 }
845 case DS_GX_CMD_MTX_MULT_4x4: {
846 struct DSGXMatrix m;
847 int i;
848 for (i = 0; i < 16; ++i) {
849 m.m[i] = gx->activeEntries[i].params[0];
850 m.m[i] |= gx->activeEntries[i].params[1] << 8;
851 m.m[i] |= gx->activeEntries[i].params[2] << 16;
852 m.m[i] |= gx->activeEntries[i].params[3] << 24;
853 }
854 switch (gx->mtxMode) {
855 case 0:
856 DSGXMtxMultiply(&gx->projMatrix, &m, &gx->projMatrix);
857 break;
858 case 2:
859 DSGXMtxMultiply(&gx->vecMatrix, &m, &gx->vecMatrix);
860 // Fall through
861 case 1:
862 DSGXMtxMultiply(&gx->posMatrix, &m, &gx->posMatrix);
863 break;
864 case 3:
865 DSGXMtxMultiply(&gx->texMatrix, &m, &gx->texMatrix);
866 break;
867 }
868 _updateClipMatrix(gx);
869 break;
870 }
871 case DS_GX_CMD_MTX_MULT_4x3: {
872 struct DSGXMatrix m;
873 int i, j;
874 for (j = 0; j < 4; ++j) {
875 for (i = 0; i < 3; ++i) {
876 m.m[i + j * 4] = gx->activeEntries[i + j * 3].params[0];
877 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[1] << 8;
878 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[2] << 16;
879 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[3] << 24;
880 }
881 m.m[j * 4 + 3] = 0;
882 }
883 m.m[15] = MTX_ONE;
884 switch (gx->mtxMode) {
885 case 0:
886 DSGXMtxMultiply(&gx->projMatrix, &m, &gx->projMatrix);
887 break;
888 case 2:
889 DSGXMtxMultiply(&gx->vecMatrix, &m, &gx->vecMatrix);
890 // Fall through
891 case 1:
892 DSGXMtxMultiply(&gx->posMatrix, &m, &gx->posMatrix);
893 break;
894 case 3:
895 DSGXMtxMultiply(&gx->texMatrix, &m, &gx->texMatrix);
896 break;
897 }
898 _updateClipMatrix(gx);
899 break;
900 }
901 case DS_GX_CMD_MTX_MULT_3x3: {
902 struct DSGXMatrix m;
903 int i, j;
904 for (j = 0; j < 3; ++j) {
905 for (i = 0; i < 3; ++i) {
906 m.m[i + j * 4] = gx->activeEntries[i + j * 3].params[0];
907 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[1] << 8;
908 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[2] << 16;
909 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[3] << 24;
910 }
911 m.m[j * 4 + 3] = 0;
912 }
913 m.m[12] = 0;
914 m.m[13] = 0;
915 m.m[14] = 0;
916 m.m[15] = MTX_ONE;
917 switch (gx->mtxMode) {
918 case 0:
919 DSGXMtxMultiply(&gx->projMatrix, &m, &gx->projMatrix);
920 break;
921 case 2:
922 DSGXMtxMultiply(&gx->vecMatrix, &m, &gx->vecMatrix);
923 // Fall through
924 case 1:
925 DSGXMtxMultiply(&gx->posMatrix, &m, &gx->posMatrix);
926 break;
927 case 3:
928 DSGXMtxMultiply(&gx->texMatrix, &m, &gx->texMatrix);
929 break;
930 }
931 _updateClipMatrix(gx);
932 break;
933 }
934 case DS_GX_CMD_MTX_TRANS: {
935 int32_t m[3];
936 m[0] = gx->activeEntries[0].params[0];
937 m[0] |= gx->activeEntries[0].params[1] << 8;
938 m[0] |= gx->activeEntries[0].params[2] << 16;
939 m[0] |= gx->activeEntries[0].params[3] << 24;
940 m[1] = gx->activeEntries[1].params[0];
941 m[1] |= gx->activeEntries[1].params[1] << 8;
942 m[1] |= gx->activeEntries[1].params[2] << 16;
943 m[1] |= gx->activeEntries[1].params[3] << 24;
944 m[2] = gx->activeEntries[2].params[0];
945 m[2] |= gx->activeEntries[2].params[1] << 8;
946 m[2] |= gx->activeEntries[2].params[2] << 16;
947 m[2] |= gx->activeEntries[2].params[3] << 24;
948 switch (gx->mtxMode) {
949 case 0:
950 DSGXMtxTranslate(&gx->projMatrix, m);
951 break;
952 case 2:
953 DSGXMtxTranslate(&gx->vecMatrix, m);
954 // Fall through
955 case 1:
956 DSGXMtxTranslate(&gx->posMatrix, m);
957 break;
958 case 3:
959 DSGXMtxTranslate(&gx->texMatrix, m);
960 break;
961 }
962 _updateClipMatrix(gx);
963 break;
964 }
965 case DS_GX_CMD_MTX_SCALE: {
966 int32_t m[3];
967 m[0] = gx->activeEntries[0].params[0];
968 m[0] |= gx->activeEntries[0].params[1] << 8;
969 m[0] |= gx->activeEntries[0].params[2] << 16;
970 m[0] |= gx->activeEntries[0].params[3] << 24;
971 m[1] = gx->activeEntries[1].params[0];
972 m[1] |= gx->activeEntries[1].params[1] << 8;
973 m[1] |= gx->activeEntries[1].params[2] << 16;
974 m[1] |= gx->activeEntries[1].params[3] << 24;
975 m[2] = gx->activeEntries[2].params[0];
976 m[2] |= gx->activeEntries[2].params[1] << 8;
977 m[2] |= gx->activeEntries[2].params[2] << 16;
978 m[2] |= gx->activeEntries[2].params[3] << 24;
979 switch (gx->mtxMode) {
980 case 0:
981 DSGXMtxScale(&gx->projMatrix, m);
982 break;
983 case 1:
984 case 2:
985 DSGXMtxScale(&gx->posMatrix, m);
986 break;
987 case 3:
988 DSGXMtxScale(&gx->texMatrix, m);
989 break;
990 }
991 _updateClipMatrix(gx);
992 break;
993 }
994 case DS_GX_CMD_COLOR:
995 gx->currentVertex.color = entry.params[0];
996 gx->currentVertex.color |= entry.params[1] << 8;
997 break;
998 case DS_GX_CMD_NORMAL: {
999 int32_t xyz = entry.params[0];
1000 xyz |= entry.params[1] << 8;
1001 xyz |= entry.params[2] << 16;
1002 xyz |= entry.params[3] << 24;
1003 int16_t x = (xyz << 6) & 0xFFC0;
1004 int16_t y = (xyz >> 4) & 0xFFC0;
1005 int16_t z = (xyz >> 14) & 0xFFC0;
1006 x >>= 3;
1007 y >>= 3;
1008 z >>= 3;
1009 if (DSGXTexParamsGetCoordTfMode(gx->currentPoly.texParams) == 2) {
1010 gx->currentVertex.vs = _dotFrac(x, y, z, &gx->texMatrix.m[0]) + gx->currentVertex.s;
1011 gx->currentVertex.vt = _dotFrac(x, y, z, &gx->texMatrix.m[1]) + gx->currentVertex.t;
1012 }
1013 int16_t nx = _dotFrac(x, y, z, &gx->vecMatrix.m[0]);
1014 int16_t ny = _dotFrac(x, y, z, &gx->vecMatrix.m[1]);
1015 int16_t nz = _dotFrac(x, y, z, &gx->vecMatrix.m[2]);
1016 int r = gx->emit & 0x1F;
1017 int g = (gx->emit >> 5) & 0x1F;
1018 int b = (gx->emit >> 10) & 0x1F;
1019 int i;
1020 for (i = 0; i < 4; ++i) {
1021 if (!(DSGXPolygonAttrsGetLights(gx->currentPoly.polyParams) & (1 << i))) {
1022 continue;
1023 }
1024 struct DSGXLight* light = &gx->lights[i];
1025 int diffuse = -_dot3(light->x, light->y, light->z, nx, ny, nz);
1026 if (diffuse < 0) {
1027 diffuse = 0;
1028 }
1029 int specular = -_dot3(-light->x >> 1, -light->y >> 1, (0x1000 - light->z) >> 1, nx, ny, nz);
1030 if (specular < 0) {
1031 specular = 0;
1032 } else {
1033 specular = 2 * specular * specular - (1 << 10);
1034 }
1035 unsigned lr = (light->color) & 0x1F;
1036 unsigned lg = (light->color >> 5) & 0x1F;
1037 unsigned lb = (light->color >> 10) & 0x1F;
1038 unsigned xr, xg, xb;
1039 xr = gx->specular & 0x1F;
1040 xg = (gx->specular >> 5) & 0x1F;
1041 xb = (gx->specular >> 10) & 0x1F;
1042 r += (specular * xr * lr) >> 17;
1043 g += (specular * xg * lg) >> 17;
1044 b += (specular * xb * lb) >> 17;
1045 xr = gx->diffuse & 0x1F;
1046 xg = (gx->diffuse >> 5) & 0x1F;
1047 xb = (gx->diffuse >> 10) & 0x1F;
1048 r += (diffuse * xr * lr) >> 17;
1049 g += (diffuse * xg * lg) >> 17;
1050 b += (diffuse * xb * lb) >> 17;
1051 xr = gx->ambient & 0x1F;
1052 xg = (gx->ambient >> 5) & 0x1F;
1053 xb = (gx->ambient >> 10) & 0x1F;
1054 r += (xr * lr) >> 5;
1055 g += (xg * lg) >> 5;
1056 b += (xb * lb) >> 5;
1057 }
1058 if (r < 0) {
1059 r = 0;
1060 } else if (r > 0x1F) {
1061 r = 0x1F;
1062 }
1063 if (g < 0) {
1064 g = 0;
1065 } else if (g > 0x1F) {
1066 g = 0x1F;
1067 }
1068 if (b < 0) {
1069 b = 0;
1070 } else if (b > 0x1F) {
1071 b = 0x1F;
1072 }
1073 gx->currentVertex.color = r | (g << 5) | (b << 10);
1074 break;
1075 }
1076 case DS_GX_CMD_TEXCOORD:
1077 gx->currentVertex.s = entry.params[0];
1078 gx->currentVertex.s |= entry.params[1] << 8;
1079 gx->currentVertex.t = entry.params[2];
1080 gx->currentVertex.t |= entry.params[3] << 8;
1081 if (DSGXTexParamsGetCoordTfMode(gx->currentPoly.texParams) == 1) {
1082 gx->currentVertex.vs = _dotTexture(&gx->currentVertex, 1, &gx->texMatrix.m[0]);
1083 gx->currentVertex.vt = _dotTexture(&gx->currentVertex, 1, &gx->texMatrix.m[1]);
1084 }
1085 break;
1086 case DS_GX_CMD_VTX_16: {
1087 int16_t x = gx->activeEntries[0].params[0];
1088 x |= gx->activeEntries[0].params[1] << 8;
1089 int16_t y = gx->activeEntries[0].params[2];
1090 y |= gx->activeEntries[0].params[3] << 8;
1091 int16_t z = gx->activeEntries[1].params[0];
1092 z |= gx->activeEntries[1].params[1] << 8;
1093 _emitVertex(gx, x, y, z);
1094 break;
1095 }
1096 case DS_GX_CMD_VTX_10: {
1097 int32_t xyz = entry.params[0];
1098 xyz |= entry.params[1] << 8;
1099 xyz |= entry.params[2] << 16;
1100 xyz |= entry.params[3] << 24;
1101 int16_t x = (xyz << 6) & 0xFFC0;
1102 int16_t y = (xyz >> 4) & 0xFFC0;
1103 int16_t z = (xyz >> 14) & 0xFFC0;
1104 _emitVertex(gx, x, y, z);
1105 break;
1106 }
1107 case DS_GX_CMD_VTX_XY: {
1108 int16_t x = entry.params[0];
1109 x |= entry.params[1] << 8;
1110 int16_t y = entry.params[2];
1111 y |= entry.params[3] << 8;
1112 _emitVertex(gx, x, y, gx->currentVertex.coord[2]);
1113 break;
1114 }
1115 case DS_GX_CMD_VTX_XZ: {
1116 int16_t x = entry.params[0];
1117 x |= entry.params[1] << 8;
1118 int16_t z = entry.params[2];
1119 z |= entry.params[3] << 8;
1120 _emitVertex(gx, x, gx->currentVertex.coord[1], z);
1121 break;
1122 }
1123 case DS_GX_CMD_VTX_YZ: {
1124 int16_t y = entry.params[0];
1125 y |= entry.params[1] << 8;
1126 int16_t z = entry.params[2];
1127 z |= entry.params[3] << 8;
1128 _emitVertex(gx, gx->currentVertex.coord[0], y, z);
1129 break;
1130 }
1131 case DS_GX_CMD_VTX_DIFF: {
1132 int32_t xyz = entry.params[0];
1133 xyz |= entry.params[1] << 8;
1134 xyz |= entry.params[2] << 16;
1135 xyz |= entry.params[3] << 24;
1136 int16_t x = (xyz << 6) & 0xFFC0;
1137 int16_t y = (xyz >> 4) & 0xFFC0;
1138 int16_t z = (xyz >> 14) & 0xFFC0;
1139 _emitVertex(gx, gx->currentVertex.coord[0] + (x >> 6),
1140 gx->currentVertex.coord[1] + (y >> 6),
1141 gx->currentVertex.coord[2] + (z >> 6));
1142 break;
1143 }
1144 case DS_GX_CMD_DIF_AMB:
1145 gx->diffuse = entry.params[0];
1146 gx->diffuse |= entry.params[1] << 8;
1147 if (gx->diffuse & 0x8000) {
1148 gx->currentVertex.color = gx->diffuse;
1149 }
1150 gx->ambient = entry.params[2];
1151 gx->ambient |= entry.params[3] << 8;
1152 break;
1153 case DS_GX_CMD_SPE_EMI:
1154 gx->specular = entry.params[0];
1155 gx->specular |= entry.params[1] << 8;
1156 gx->emit = entry.params[2];
1157 gx->emit |= entry.params[3] << 8;
1158 break;
1159 case DS_GX_CMD_LIGHT_VECTOR: {
1160 uint32_t xyz = entry.params[0];
1161 xyz |= entry.params[1] << 8;
1162 xyz |= entry.params[2] << 16;
1163 xyz |= entry.params[3] << 24;
1164 struct DSGXLight* light = &gx->lights[xyz >> 30];
1165 int16_t x = (xyz << 6) & 0xFFC0;
1166 int16_t y = (xyz >> 4) & 0xFFC0;
1167 int16_t z = (xyz >> 14) & 0xFFC0;
1168 x >>= 3;
1169 y >>= 3;
1170 z >>= 3;
1171 light->x = _dotFrac(x, y, z, &gx->vecMatrix.m[0]);
1172 light->y = _dotFrac(x, y, z, &gx->vecMatrix.m[1]);
1173 light->z = _dotFrac(x, y, z, &gx->vecMatrix.m[2]);
1174 break;
1175 }
1176 case DS_GX_CMD_LIGHT_COLOR: {
1177 struct DSGXLight* light = &gx->lights[entry.params[3] >> 6];
1178 light->color = entry.params[0];
1179 light->color |= entry.params[1] << 8;
1180 break;
1181 }
1182 case DS_GX_CMD_POLYGON_ATTR:
1183 gx->nextPoly.polyParams = entry.params[0];
1184 gx->nextPoly.polyParams |= entry.params[1] << 8;
1185 gx->nextPoly.polyParams |= entry.params[2] << 16;
1186 gx->nextPoly.polyParams |= entry.params[3] << 24;
1187 break;
1188 case DS_GX_CMD_TEXIMAGE_PARAM:
1189 gx->nextPoly.texParams = entry.params[0];
1190 gx->nextPoly.texParams |= entry.params[1] << 8;
1191 gx->nextPoly.texParams |= entry.params[2] << 16;
1192 gx->nextPoly.texParams |= entry.params[3] << 24;
1193 gx->currentPoly.texParams = gx->nextPoly.texParams;
1194 break;
1195 case DS_GX_CMD_PLTT_BASE:
1196 gx->nextPoly.palBase = entry.params[0];
1197 gx->nextPoly.palBase |= entry.params[1] << 8;
1198 gx->nextPoly.palBase |= entry.params[2] << 16;
1199 gx->nextPoly.palBase |= entry.params[3] << 24;
1200 break;
1201 case DS_GX_CMD_BEGIN_VTXS:
1202 gx->vertexMode = entry.params[0] & 3;
1203 gx->currentPoly = gx->nextPoly;
1204 gx->reverseWinding = false;
1205 memset(gx->pendingVertexIds, -1, sizeof(gx->pendingVertexIds));
1206 break;
1207 case DS_GX_CMD_END_VTXS:
1208 gx->vertexMode = -1;
1209 break;
1210 case DS_GX_CMD_SWAP_BUFFERS:
1211 gx->swapBuffers = true;
1212 gx->wSort = entry.params[0] & 2;
1213 memset(&gx->currentVertex, 0, sizeof(gx->currentVertex));
1214 memset(&gx->nextPoly, 0, sizeof(gx-> nextPoly));
1215 gx->currentVertex.color = 0x7FFF;
1216 break;
1217 case DS_GX_CMD_VIEWPORT:
1218 gx->viewportX1 = (uint8_t) entry.params[0];
1219 gx->viewportY1 = (uint8_t) entry.params[1];
1220 gx->viewportX2 = (uint8_t) entry.params[2];
1221 gx->viewportY2 = (uint8_t) entry.params[3];
1222 gx->viewportWidth = gx->viewportX2 - gx->viewportX1 + 1;
1223 gx->viewportHeight = gx->viewportY2 - gx->viewportY1 + 1;
1224 gx->renderer->viewportX = gx->viewportX1;
1225 gx->renderer->viewportY = gx->viewportY1;
1226 gx->renderer->viewportWidth = gx->viewportWidth;
1227 gx->renderer->viewportHeight = gx->viewportHeight;
1228 break;
1229 case DS_GX_CMD_BOX_TEST:
1230 gxstat = DSRegGXSTATClearTestBusy(gxstat);
1231 gxstat = DSRegGXSTATTestFillBoxTestResult(gxstat, _boxTest(gx));
1232 break;
1233 default:
1234 mLOG(DS_GX, STUB, "Unimplemented GX command %02X:%02X %02X %02X %02X", entry.command, entry.params[0], entry.params[1], entry.params[2], entry.params[3]);
1235 break;
1236 }
1237
1238 gxstat = DSRegGXSTATSetPVMatrixStackLevel(gxstat, gx->pvMatrixPointer);
1239 gxstat = DSRegGXSTATSetProjMatrixStackLevel(gxstat, projMatrixPointer);
1240 gxstat = DSRegGXSTATTestFillMatrixStackError(gxstat, projMatrixPointer || gx->pvMatrixPointer >= 0x1F);
1241 gx->p->memory.io9[DS9_REG_GXSTAT_LO >> 1] = gxstat;
1242
1243 if (cyclesLate >= cycles) {
1244 cyclesLate -= cycles;
1245 } else {
1246 break;
1247 }
1248 }
1249 if (cycles && !gx->swapBuffers) {
1250 mTimingSchedule(timing, &gx->fifoEvent, cycles - cyclesLate);
1251 }
1252 if (CircleBufferSize(&gx->fifo) < (DS_GX_FIFO_SIZE * sizeof(struct DSGXEntry))) {
1253 _flushOutstanding(gx);
1254 }
1255 DSGXUpdateGXSTAT(gx);
1256}
1257
1258void DSGXInit(struct DSGX* gx) {
1259 gx->renderer = &dummyRenderer;
1260 CircleBufferInit(&gx->fifo, sizeof(struct DSGXEntry) * DS_GX_FIFO_SIZE);
1261 CircleBufferInit(&gx->pipe, sizeof(struct DSGXEntry) * DS_GX_PIPE_SIZE);
1262 gx->vertexBuffer[0] = malloc(sizeof(struct DSGXVertex) * DS_GX_VERTEX_BUFFER_SIZE);
1263 gx->vertexBuffer[1] = malloc(sizeof(struct DSGXVertex) * DS_GX_VERTEX_BUFFER_SIZE);
1264 gx->polygonBuffer[0] = malloc(sizeof(struct DSGXPolygon) * DS_GX_POLYGON_BUFFER_SIZE);
1265 gx->polygonBuffer[1] = malloc(sizeof(struct DSGXPolygon) * DS_GX_POLYGON_BUFFER_SIZE);
1266 gx->fifoEvent.name = "DS GX FIFO";
1267 gx->fifoEvent.priority = 0xC;
1268 gx->fifoEvent.context = gx;
1269 gx->fifoEvent.callback = _fifoRun;
1270}
1271
1272void DSGXDeinit(struct DSGX* gx) {
1273 DSGXAssociateRenderer(gx, &dummyRenderer);
1274 CircleBufferDeinit(&gx->fifo);
1275 CircleBufferDeinit(&gx->pipe);
1276 free(gx->vertexBuffer[0]);
1277 free(gx->vertexBuffer[1]);
1278 free(gx->polygonBuffer[0]);
1279 free(gx->polygonBuffer[1]);
1280}
1281
1282void DSGXReset(struct DSGX* gx) {
1283 CircleBufferClear(&gx->fifo);
1284 CircleBufferClear(&gx->pipe);
1285 DSGXMtxIdentity(&gx->projMatrix);
1286 DSGXMtxIdentity(&gx->texMatrix);
1287 DSGXMtxIdentity(&gx->posMatrix);
1288 DSGXMtxIdentity(&gx->vecMatrix);
1289
1290 DSGXMtxIdentity(&gx->clipMatrix);
1291 DSGXMtxIdentity(&gx->projMatrixStack);
1292 DSGXMtxIdentity(&gx->texMatrixStack);
1293 int i;
1294 for (i = 0; i < 32; ++i) {
1295 DSGXMtxIdentity(&gx->posMatrixStack[i]);
1296 DSGXMtxIdentity(&gx->vecMatrixStack[i]);
1297 }
1298 gx->swapBuffers = false;
1299 gx->bufferIndex = 0;
1300 gx->vertexIndex = 0;
1301 gx->polygonIndex = 0;
1302 gx->mtxMode = 0;
1303 gx->pvMatrixPointer = 0;
1304 gx->vertexMode = -1;
1305
1306 gx->viewportX1 = 0;
1307 gx->viewportY1 = 0;
1308 gx->viewportX2 = DS_VIDEO_HORIZONTAL_PIXELS - 1;
1309 gx->viewportY2 = DS_VIDEO_VERTICAL_PIXELS - 1;
1310 gx->viewportWidth = gx->viewportX2 - gx->viewportX1 + 1;
1311 gx->viewportHeight = gx->viewportY2 - gx->viewportY1 + 1;
1312
1313 memset(gx->outstandingParams, 0, sizeof(gx->outstandingParams));
1314 memset(gx->outstandingCommand, 0, sizeof(gx->outstandingCommand));
1315 memset(&gx->outstandingEntry, 0, sizeof(gx->outstandingEntry));
1316 gx->activeParams = 0;
1317 memset(&gx->currentVertex, 0, sizeof(gx->currentVertex));
1318 memset(&gx->nextPoly, 0, sizeof(gx-> nextPoly));
1319 gx->currentVertex.color = 0x7FFF;
1320 gx->dmaSource = -1;
1321}
1322
1323void DSGXAssociateRenderer(struct DSGX* gx, struct DSGXRenderer* renderer) {
1324 gx->renderer->deinit(gx->renderer);
1325 gx->renderer = renderer;
1326 memcpy(gx->renderer->tex, gx->tex, sizeof(gx->renderer->tex));
1327 memcpy(gx->renderer->texPal, gx->texPal, sizeof(gx->renderer->texPal));
1328 gx->renderer->init(gx->renderer);
1329}
1330
1331void DSGXUpdateGXSTAT(struct DSGX* gx) {
1332 uint32_t value = gx->p->memory.io9[DS9_REG_GXSTAT_HI >> 1] << 16;
1333 value = DSRegGXSTATIsDoIRQ(value);
1334
1335 size_t entries = CircleBufferSize(&gx->fifo) / sizeof(struct DSGXEntry);
1336 // XXX
1337 if (gx->swapBuffers) {
1338 entries++;
1339 }
1340 value = DSRegGXSTATSetFIFOEntries(value, entries);
1341 value = DSRegGXSTATSetFIFOLtHalf(value, entries < (DS_GX_FIFO_SIZE / 2));
1342 value = DSRegGXSTATSetFIFOEmpty(value, entries == 0);
1343
1344 if ((DSRegGXSTATGetDoIRQ(value) == 1 && entries < (DS_GX_FIFO_SIZE / 2)) ||
1345 (DSRegGXSTATGetDoIRQ(value) == 2 && entries == 0)) {
1346 DSRaiseIRQ(gx->p->ds9.cpu, gx->p->ds9.memory.io, DS_IRQ_GEOM_FIFO);
1347 }
1348
1349 value = DSRegGXSTATSetBusy(value, mTimingIsScheduled(&gx->p->ds9.timing, &gx->fifoEvent) || gx->swapBuffers);
1350
1351 gx->p->memory.io9[DS9_REG_GXSTAT_HI >> 1] = value >> 16;
1352
1353 struct GBADMA* dma = NULL;
1354 if (gx->dmaSource >= 0) {
1355 dma = &gx->p->ds9.memory.dma[gx->dmaSource];
1356 if (GBADMARegisterGetTiming9(dma->reg) != DS_DMA_TIMING_GEOM_FIFO) {
1357 gx->dmaSource = -1;
1358 } else if (GBADMARegisterIsEnable(dma->reg) && entries < (DS_GX_FIFO_SIZE / 2) && !dma->nextCount) {
1359 dma->nextCount = dma->count;
1360 dma->when = mTimingCurrentTime(&gx->p->ds9.timing);
1361 DSDMAUpdate(&gx->p->ds9);
1362 }
1363 }
1364}
1365
1366static void DSGXUnpackCommand(struct DSGX* gx, uint32_t command) {
1367 gx->outstandingCommand[0] = command;
1368 gx->outstandingCommand[1] = command >> 8;
1369 gx->outstandingCommand[2] = command >> 16;
1370 gx->outstandingCommand[3] = command >> 24;
1371 if (gx->outstandingCommand[0] >= DS_GX_CMD_MAX) {
1372 gx->outstandingCommand[0] = 0;
1373 }
1374 if (gx->outstandingCommand[1] >= DS_GX_CMD_MAX) {
1375 gx->outstandingCommand[1] = 0;
1376 }
1377 if (gx->outstandingCommand[2] >= DS_GX_CMD_MAX) {
1378 gx->outstandingCommand[2] = 0;
1379 }
1380 if (gx->outstandingCommand[3] >= DS_GX_CMD_MAX) {
1381 gx->outstandingCommand[3] = 0;
1382 }
1383 gx->outstandingParams[0] = _gxCommandParams[gx->outstandingCommand[0]];
1384 gx->outstandingParams[1] = _gxCommandParams[gx->outstandingCommand[1]];
1385 gx->outstandingParams[2] = _gxCommandParams[gx->outstandingCommand[2]];
1386 gx->outstandingParams[3] = _gxCommandParams[gx->outstandingCommand[3]];
1387 _flushOutstanding(gx);
1388 DSGXUpdateGXSTAT(gx);
1389}
1390
1391static void DSGXWriteFIFO(struct DSGX* gx, struct DSGXEntry entry) {
1392 if (CircleBufferSize(&gx->fifo) == (DS_GX_FIFO_SIZE * sizeof(entry))) {
1393 mLOG(DS_GX, INFO, "FIFO full");
1394 if (gx->p->cpuBlocked & DS_CPU_BLOCK_GX) {
1395 // Can happen from STM
1396 mTimingDeschedule(&gx->p->ds9.timing, &gx->fifoEvent);
1397 _fifoRun(&gx->p->ds9.timing, gx, 0);
1398 }
1399 gx->p->cpuBlocked |= DS_CPU_BLOCK_GX;
1400 gx->outstandingEntry = entry;
1401 gx->p->ds9.cpu->nextEvent = 0;
1402 return;
1403 }
1404 if (gx->outstandingCommand[0]) {
1405 entry.command = gx->outstandingCommand[0];
1406 if (gx->outstandingParams[0]) {
1407 --gx->outstandingParams[0];
1408 }
1409 if (!gx->outstandingParams[0]) {
1410 // TODO: improve this
1411 memmove(&gx->outstandingParams[0], &gx->outstandingParams[1], sizeof(gx->outstandingParams[0]) * 3);
1412 memmove(&gx->outstandingCommand[0], &gx->outstandingCommand[1], sizeof(gx->outstandingCommand[0]) * 3);
1413 gx->outstandingParams[3] = 0;
1414 gx->outstandingCommand[3] = 0;
1415 }
1416 } else {
1417 gx->outstandingParams[0] = _gxCommandParams[entry.command];
1418 if (gx->outstandingParams[0]) {
1419 --gx->outstandingParams[0];
1420 }
1421 if (gx->outstandingParams[0]) {
1422 gx->outstandingCommand[0] = entry.command;
1423 }
1424 }
1425 uint32_t cycles = _gxCommandCycleBase[entry.command];
1426 if (!cycles) {
1427 return;
1428 }
1429 if (CircleBufferSize(&gx->fifo) == 0 && CircleBufferSize(&gx->pipe) < (DS_GX_PIPE_SIZE * sizeof(entry))) {
1430 CircleBufferWrite(&gx->pipe, &entry, sizeof(entry));
1431 } else if (CircleBufferSize(&gx->fifo) < (DS_GX_FIFO_SIZE * sizeof(entry))) {
1432 CircleBufferWrite(&gx->fifo, &entry, sizeof(entry));
1433 }
1434 if (entry.command == DS_GX_CMD_BOX_TEST) {
1435 DSRegGXSTAT gxstat = gx->p->memory.io9[DS9_REG_GXSTAT_LO >> 1];
1436 gxstat = DSRegGXSTATFillTestBusy(gxstat);
1437 gxstat = DSRegGXSTATClearBoxTestResult(gxstat);
1438 gx->p->memory.io9[DS9_REG_GXSTAT_LO >> 1] = gxstat;
1439 }
1440 if (!gx->swapBuffers && !mTimingIsScheduled(&gx->p->ds9.timing, &gx->fifoEvent)) {
1441 mTimingSchedule(&gx->p->ds9.timing, &gx->fifoEvent, cycles);
1442 }
1443
1444 _flushOutstanding(gx);
1445}
1446
1447uint16_t DSGXWriteRegister(struct DSGX* gx, uint32_t address, uint16_t value) {
1448 uint16_t oldValue = gx->p->memory.io9[address >> 1];
1449 switch (address) {
1450 case DS9_REG_DISP3DCNT:
1451 mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%04X", address, value);
1452 break;
1453 case DS9_REG_GXSTAT_LO:
1454 value = DSRegGXSTATIsMatrixStackError(value);
1455 if (value) {
1456 oldValue = DSRegGXSTATClearMatrixStackError(oldValue);
1457 oldValue = DSRegGXSTATClearProjMatrixStackLevel(oldValue);
1458 }
1459 value = oldValue;
1460 break;
1461 case DS9_REG_GXSTAT_HI:
1462 value = DSRegGXSTATIsDoIRQ(value << 16) >> 16;
1463 gx->p->memory.io9[address >> 1] = value;
1464 DSGXUpdateGXSTAT(gx);
1465 value = gx->p->memory.io9[address >> 1];
1466 break;
1467 default:
1468 if (address < DS9_REG_GXFIFO_00) {
1469 mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%04X", address, value);
1470 } else if (address <= DS9_REG_GXFIFO_1F) {
1471 mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%04X", address, value);
1472 } else if (address < DS9_REG_GXSTAT_LO) {
1473 struct DSGXEntry entry = {
1474 .command = (address & 0x1FC) >> 2,
1475 .params = {
1476 value,
1477 value >> 8,
1478 }
1479 };
1480 if (entry.command < DS_GX_CMD_MAX) {
1481 DSGXWriteFIFO(gx, entry);
1482 }
1483 } else {
1484 mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%04X", address, value);
1485 }
1486 break;
1487 }
1488 return value;
1489}
1490
1491uint32_t DSGXWriteRegister32(struct DSGX* gx, uint32_t address, uint32_t value) {
1492 switch (address) {
1493 case DS9_REG_DISP3DCNT:
1494 mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%08X", address, value);
1495 break;
1496 case DS9_REG_GXSTAT_LO:
1497 value = (value & 0xFFFF0000) | DSGXWriteRegister(gx, DS9_REG_GXSTAT_LO, value);
1498 value = (value & 0x0000FFFF) | (DSGXWriteRegister(gx, DS9_REG_GXSTAT_HI, value >> 16) << 16);
1499 break;
1500 default:
1501 if (address < DS9_REG_GXFIFO_00) {
1502 mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%08X", address, value);
1503 } else if (address <= DS9_REG_GXFIFO_1F) {
1504 if (gx->outstandingParams[0]) {
1505 struct DSGXEntry entry = {
1506 .command = gx->outstandingCommand[0],
1507 .params = {
1508 value,
1509 value >> 8,
1510 value >> 16,
1511 value >> 24
1512 }
1513 };
1514 DSGXWriteFIFO(gx, entry);
1515 } else {
1516 DSGXUnpackCommand(gx, value);
1517 }
1518 } else if (address < DS9_REG_GXSTAT_LO) {
1519 struct DSGXEntry entry = {
1520 .command = (address & 0x1FC) >> 2,
1521 .params = {
1522 value,
1523 value >> 8,
1524 value >> 16,
1525 value >> 24
1526 }
1527 };
1528 DSGXWriteFIFO(gx, entry);
1529 } else {
1530 mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%08X", address, value);
1531 }
1532 break;
1533 }
1534 return value;
1535}
1536
1537void DSGXFlush(struct DSGX* gx) {
1538 if (gx->swapBuffers) {
1539 gx->renderer->setRAM(gx->renderer, gx->vertexBuffer[gx->bufferIndex], gx->polygonBuffer[gx->bufferIndex], gx->polygonIndex, gx->wSort);
1540 gx->swapBuffers = false;
1541 gx->bufferIndex ^= 1;
1542 gx->vertexIndex = 0;
1543 gx->pendingVertexIndex = 0;
1544 gx->polygonIndex = 0;
1545 if (CircleBufferSize(&gx->fifo)) {
1546 mTimingSchedule(&gx->p->ds9.timing, &gx->fifoEvent, 0);
1547 }
1548 }
1549
1550 DSGXUpdateGXSTAT(gx);
1551}
1552
1553void DSGXScheduleDMA(struct DSCommon* dscore, int number, struct GBADMA* info) {
1554 UNUSED(info);
1555 dscore->p->gx.dmaSource = number;
1556}
1557
1558static void DSGXDummyRendererInit(struct DSGXRenderer* renderer) {
1559 UNUSED(renderer);
1560 // Nothing to do
1561}
1562
1563static void DSGXDummyRendererReset(struct DSGXRenderer* renderer) {
1564 UNUSED(renderer);
1565 // Nothing to do
1566}
1567
1568static void DSGXDummyRendererDeinit(struct DSGXRenderer* renderer) {
1569 UNUSED(renderer);
1570 // Nothing to do
1571}
1572
1573static void DSGXDummyRendererInvalidateTex(struct DSGXRenderer* renderer, int slot) {
1574 UNUSED(renderer);
1575 UNUSED(slot);
1576 // Nothing to do
1577}
1578
1579static void DSGXDummyRendererSetRAM(struct DSGXRenderer* renderer, struct DSGXVertex* verts, struct DSGXPolygon* polys, unsigned polyCount, bool wSort) {
1580 UNUSED(renderer);
1581 UNUSED(verts);
1582 UNUSED(polys);
1583 UNUSED(polyCount);
1584 UNUSED(wSort);
1585 // Nothing to do
1586}
1587
1588static void DSGXDummyRendererDrawScanline(struct DSGXRenderer* renderer, int y) {
1589 UNUSED(renderer);
1590 UNUSED(y);
1591 // Nothing to do
1592}
1593
1594static void DSGXDummyRendererGetScanline(struct DSGXRenderer* renderer, int y, const color_t** output) {
1595 UNUSED(renderer);
1596 UNUSED(y);
1597 *output = NULL;
1598 // Nothing to do
1599}