DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
mglib.cpp
1#pragma helper_mask_gpr 0x30
2#pragma helper_mask_fpr 0x1000
3#pragma name_counter 250
4
5#include "mglib.hpp"
6#include "types.h"
7
8#include <eekernel.h>
9#include <libdma.h>
10#include <libpkt.h>
11#include <sifdev.h>
12
13#include <cmath>
14#include <cstdio>
15#include <cstring>
16
17#include "collision.hpp"
18#include "dataalloc.hpp"
19#include "dataset.hpp"
20#include "frame.hpp"
21#include "mathutil.hpp"
22#include "rect.hpp"
23#include "texture.hpp"
24#include "vutext.hpp"
25
26int DBuffID;
27int mgWaitVSync;
28sceDmaChan *DmaCH1;
29sceDmaChan *DmaCH2;
30sceDmaChan *DmaCH8;
31sceVif1Packet *Vif1Packet;
32int VSyncField__2;
33int mgClearBackFlag;
34#ifdef PAL
36int mgAdjustX;
38int mgAdjustY;
39#endif
40sceGsTex1 mgTEX1Env;
41sceGsTest mgPixelTest;
42sceGsZbuf mgZBuffer;
43sceGsAlpha mgAlpha;
44sceGsTexa mgTexa;
45#ifdef PAL
47int mgTopVRAM;
49int mgZBufferAdr;
50#endif
51
52MG_PICKZ mgPickZBuff[16];
53sceGifTag GiftagAD;
54sceGsDBuff mgDBuff;
55sceVu0FVECTOR mgBackColor;
56
57/* The window rectangle. Its four words are written back to front at static-initialisation time,
58 which CRect_i_'s own constructor does not do -- every rectangle handed to a drawing call has
59 them written front to back -- so the storage is declared through a type that writes them in
60 that order, and postprocess_object.py gives it the name the rest of the game reaches it by. */
62public:
63 s32 x;
64 s32 y;
65 s32 width;
66 s32 height;
68 CWindowRect() {
69 height = 0;
70 width = 0;
71 y = 0;
72 x = 0;
73 }
74} __attribute__((aligned(16)));
75
76CWindowRect mgWindowRectStore;
77RenderInfo mgRenderInfo;
78/* The one texture manager the game has. It is built here rather than beside the textures because
79 this is the unit that brings up the graphics. */
80CTextureManager TexManager;
81
82sceVu0FVECTOR mgZeroVector;
83sceVu0FVECTOR mgUnitVector;
84sceVu0FVECTOR mgUnitVector2;
85sceVu0FVECTOR mgZeroVector2;
86sceVu0FMATRIX mgUnitMatrix;
87sceVu0FMATRIX mgZeroMatrix;
88static sceVif1Packet vifpacket[2];
89sceGsStoreImage gs_simage;
90static int packetbuf[2];
91
92static int vcount;
93static int over_vsync;
94static int (*VSyncCallBack2)(int);
95static int call_back_active;
96#ifndef PORT
97static int VSyncCallBack(int id) {
98 iFlushCache(0);
99 call_back_active = 1;
100 /* Bit 13 of the GS status register is the field being displayed, and what everything downstream
101 offsets by is the other one. */
102 VSyncField__2 = !(bool) ((*(volatile unsigned long *) 0x12001000 >> 13) & 1);
103
104 if (VSyncCallBack2) {
105 VSyncCallBack2(id);
106 }
107
108 vcount++;
109 call_back_active = 0;
110 /* Ordering the stores and re-enabling interrupts are both things the compiler has no way to
111 spell, and a handler that returned without the second would be a handler that runs once. */
112 asm {
113 sync
114 ei
115 }
116 iFlushCache(0);
117 return 0;
118}
119#endif
120int MGGetVSyncCount() {
121 return vcount;
122}
123
124void MGInit() {
125 DBuffID = 0;
126 mgWaitVSync = 0;
127 Vif1Packet = &vifpacket[0];
128
129 DmaCH1 = sceDmaGetChan(1);
130 DmaCH2 = sceDmaGetChan(2);
131 DmaCH8 = sceDmaGetChan(8);
132 /* The frame is a source chain rather than a flat transfer, so channel 1 has to read the tags
133 it is handed instead of treating them as data. */
134 DmaCH1->chcr.TTE = 1;
135
136 *(u_long128 *) &GiftagAD = 0;
137 GiftagAD.EOP = 1;
138 GiftagAD.NREG = 1;
139 GiftagAD.REGS0 = SCE_GIF_PACKED_AD;
140
141#ifdef PAL
142 sceGsResetGraph(0, SCE_GS_INTERLACE, SCE_GS_PAL, SCE_GS_FIELD);
143 mgTopVRAM = 0x1E00;
144 mgZBufferAdr = 0x1400;
145#else
146 sceGsResetGraph(0, SCE_GS_INTERLACE, SCE_GS_NTSC, SCE_GS_FIELD);
147#endif
148 sceGsSetDefDBuff(&mgDBuff, SCE_GS_PSMCT32, 640, SCREEN_HALF_HEIGHT, SCE_GS_ZGEQUAL, SCE_GS_PSMZ24, 0);
149#ifdef PAL
150 mgDBuff.disp0.display.DY = 88;
151 mgDBuff.disp1.display.DY = 88;
152#endif
153
154 mgWindowRect.x = 0;
155 mgWindowRect.y = 0;
156 mgWindowRect.width = 640;
157 mgWindowRect.height = SCREEN_HALF_HEIGHT;
158#ifdef PAL
159 MGAdjustScreen(0, 0);
160#endif
161
162 mgBackColor[0] = 0.0f;
163 mgBackColor[1] = 0.0f;
164 mgBackColor[2] = 0.0f;
165 mgBackColor[3] = 128.0f;
166 mgClearBackFlag = 1;
167
168 mgDBuff.clear0.rgbaq.R = (int) mgBackColor[0];
169 mgDBuff.clear0.rgbaq.G = (int) mgBackColor[1];
170 mgDBuff.clear0.rgbaq.B = (int) mgBackColor[2];
171 mgDBuff.clear0.rgbaq.A = (int) mgBackColor[3];
172 mgDBuff.clear1.rgbaq.R = (int) mgBackColor[0];
173 mgDBuff.clear1.rgbaq.G = (int) mgBackColor[1];
174 mgDBuff.clear1.rgbaq.B = (int) mgBackColor[2];
175 mgDBuff.clear1.rgbaq.A = (int) mgBackColor[3];
176
177 mgTEX1Env.LCM = 1;
178 mgTEX1Env.MXL = 0;
179 mgTEX1Env.MMAG = 1;
180 mgTEX1Env.MMIN = 1;
181 mgTEX1Env.MTBA = 1;
182 mgTEX1Env.L = 0;
183 mgTEX1Env.K = 0;
184
185 /* The two registers the game edits per draw start from whatever the SDK's own drawing
186 environment put in them, so they are copied out of it rather than built from nothing. */
187 mgPixelTest = mgDBuff.draw0.test1;
188 mgPixelTest.bits.ate = 1;
189 mgPixelTest.bits.atst = 5;
190 mgPixelTest.bits.aref = 0;
191 mgPixelTest.bits.afail = 0;
192
193 mgZBuffer = mgDBuff.draw0.zbuf1;
194
195 mgAlpha.bits.a = 0;
196 mgAlpha.bits.b = 1;
197 mgAlpha.bits.c = 0;
198 mgAlpha.bits.d = 1;
199 mgAlpha.bits.fix = 0;
200
201 mgTexa.TA0 = 0;
202 mgTexa.TA1 = 0;
203 mgTexa.AEM = 1;
204
205 mgZeroVector[0] = 0.0f;
206 mgZeroVector[1] = 0.0f;
207 mgZeroVector[2] = 0.0f;
208 mgZeroVector[3] = 0.0f;
209 mgUnitVector[0] = 1.0f;
210 mgUnitVector[1] = 1.0f;
211 mgUnitVector[2] = 1.0f;
212 mgUnitVector[3] = 1.0f;
213 sceVu0CopyVector(mgZeroVector2, mgZeroVector);
214 sceVu0CopyVector(mgUnitVector2, mgUnitVector);
215 /* The second pair are the same vectors as points rather than directions, which is the whole of
216 the difference between them. */
217 mgZeroVector2[3] = 1.0f;
218 mgUnitVector2[3] = 0.0f;
219
220 sceVu0UnitMatrix(mgUnitMatrix);
221 sceVu0UnitMatrix(mgZeroMatrix);
222 mgZeroMatrix[0][0] = 0.0f;
223 mgZeroMatrix[1][1] = 0.0f;
224 mgZeroMatrix[2][2] = 0.0f;
225 mgZeroMatrix[3][3] = 0.0f;
226
227 CreateSinTable();
228
229 sceGsSyncVCallback(VSyncCallBack);
230 VSyncCallBack2 = 0;
231 vcount = 0;
232}
233
234/* Waiting out a handler that is already running is what keeps the pointer from changing under it. */
235void MGInitVSyncCallBack(int (*callback)(int)) {
236 while (call_back_active)
237 ;
238
239 VSyncCallBack2 = callback;
240}
241
242/* Meant to lift each buffer to a quadword boundary and unable to: the remainder is taken over four
243 bytes rather than sixteen, so it is zero for every address the caller can pass and the step never
244 runs. */
245void MGInitVif1Packet(u_long128 *buffer0, u_long128 *buffer1) {
246 packetbuf[0] = (int) buffer0;
247 packetbuf[1] = (int) buffer1;
248
249 if (packetbuf[0] % 4) {
250 packetbuf[0] += (4 - packetbuf[0] % 4) * 4;
251 }
252
253 if (packetbuf[1] % 4) {
254 packetbuf[1] += (4 - packetbuf[1] % 4) * 4;
255 }
256
257 sceVif1PkInit(&vifpacket[0], (u_int *) packetbuf[0]);
258 sceVif1PkInit(&vifpacket[1], (u_int *) packetbuf[1]);
259 sceVif1PkReset(&vifpacket[0]);
260 sceVif1PkReset(&vifpacket[1]);
261}
262
263/* The frame the capture is part-way through: it decides which of the two read-back buffers a field
264 lands in, and whether this call is the one that opens and closes the file. */
265static int movie_count;
266
267/* One screenshot, written where the host filesystem can take it: the two fields are read back out
268 of the frame buffer separately, packed from 32-bit colour down to the 24 a TGA holds, and
269 written row by row from the bottom up, which is the order that format stores. */
270void StoreImage() {
271 int fd;
272 int i;
273 int j;
274 int k;
275 char *mark;
276 sceVif1Packet *packet;
277 u_char *field0;
278 u_char *field1;
279 static int image_num = 0;
280 static u_char tga_head[18] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 2, 192, 1, 24, 0};
281 char name[128];
282 char directory[128];
283
284 movie_count = 1;
285
286 packet = &vifpacket[DBuffID];
287
288 printf("%d %d %d\n", image_num, DBuffID, VSyncField__2);
289
290 if (movie_count % 2) {
291 strcpy(directory, "host0:y:/ps2/capture/");
292 sprintf(name, "%si%5d.tga", directory, image_num++);
293
294 /* The width the number is padded to is what makes the names sort, and a space is not a
295 character a file name can carry. */
296 for (mark = name; *mark; mark++) {
297 if (*mark == ' ') {
298 *mark = '0';
299 }
300 }
301
302 fd = sceOpen(name, SCE_WRONLY | SCE_CREAT | SCE_TRUNC);
303 }
304
305 sceGsSetHalfOffset(&mgDBuff.draw0, 2048, 2048, 0);
306 sceGsSetHalfOffset(&mgDBuff.draw1, 2048, 2048, 0);
307 FlushCache(0);
308 sceGsSwapDBuff(&mgDBuff, DBuffID);
309 FlushCache(0);
310 sceGsSyncPath(0, 0);
311 DmaCH1->chcr.TTE = 1;
312 sceDmaSend(DmaCH1, packet->pBase);
313 sceGsSyncPath(0, 0);
314
315 sceGsSetDefStoreImage(&gs_simage, !DBuffID * 2240, 10, 0, 0, 0, 640, SCREEN_HALF_HEIGHT);
316 FlushCache(0);
317
318 if (movie_count % 2) {
319 sceGsExecStoreImage(&gs_simage, (u_long128 *) 0x02100000);
320 } else {
321 sceGsExecStoreImage(&gs_simage, (u_long128 *) 0x02300000);
322 }
323
324 sceGsSyncPath(0, 0);
325
326 sceGsSetHalfOffset(&mgDBuff.draw0, 2048, 2048, 1);
327 sceGsSetHalfOffset(&mgDBuff.draw1, 2048, 2048, 1);
328 FlushCache(0);
329 sceGsSyncPath(0, 0);
330 sceGsSwapDBuff(&mgDBuff, DBuffID);
331 FlushCache(0);
332 sceGsSyncPath(0, 0);
333 DmaCH1->chcr.TTE = 1;
334 sceDmaSend(DmaCH1, packet->pBase);
335 sceGsSyncPath(0, 0);
336
337 sceGsSetDefStoreImage(&gs_simage, !DBuffID * 2240, 10, 0, 0, 0, 640, SCREEN_HALF_HEIGHT);
338 FlushCache(0);
339
340 if (movie_count % 2) {
341 sceGsExecStoreImage(&gs_simage, (u_long128 *) 0x02200000);
342 } else {
343 sceGsExecStoreImage(&gs_simage, (u_long128 *) 0x02400000);
344 }
345
346 sceGsSyncPath(0, 0);
347
348 if (movie_count % 2) {
349 sceWrite(fd, tga_head, 18);
350
351 for (i = 0; i < SCREEN_HALF_HEIGHT; i++) {
352 field0 = (u_char *) 0x02100000 + ((((SCREEN_HALF_HEIGHT - 1) - i) * 640 >> 2) << 4);
353 field1 = (u_char *) 0x02200000 + ((((SCREEN_HALF_HEIGHT - 1) - i) * 640 >> 2) << 4);
354
355 /* Packed down in place and over the row it came from: three bytes out for every four
356 in, and the two colour ends swapped, which is the order a TGA stores. */
357 for (j = 0, k = 0; k < 2560; j += 3, k += 4) {
358 u_char red = field0[k];
359 field0[k] = field0[k + 2];
360 field0[k + 2] = red;
361 red = field1[k];
362 field1[k] = field1[k + 2];
363 field1[k + 2] = red;
364
365 field0[j] = field0[k];
366 field0[j + 1] = field0[k + 1];
367 field0[j + 2] = field0[k + 2];
368 field1[j] = field1[k];
369 field1[j + 1] = field1[k + 1];
370 field1[j + 2] = field1[k + 2];
371 }
372
373 sceWrite(fd, field1, 1920);
374 sceWrite(fd, field0, 1920);
375 }
376 }
377
378 if (movie_count % 2) {
379 sceClose(fd);
380 }
381
382 movie_count++;
383}
384
385static int h_count;
386
387void MGBeginFrame() {
388 /* Timer 0's count at the top of the frame, which MGEndFrame subtracts from the count at the
389 bottom to say how long the frame took. */
390 h_count = *(volatile u_int *) 0x10000000;
391
392 mgDBuff.clear0.rgbaq.R = (int) mgBackColor[0];
393 mgDBuff.clear0.rgbaq.G = (int) mgBackColor[1];
394 mgDBuff.clear0.rgbaq.B = (int) mgBackColor[2];
395 mgDBuff.clear0.rgbaq.A = (int) mgBackColor[3];
396 mgDBuff.clear1.rgbaq.R = (int) mgBackColor[0];
397 mgDBuff.clear1.rgbaq.G = (int) mgBackColor[1];
398 mgDBuff.clear1.rgbaq.B = (int) mgBackColor[2];
399 mgDBuff.clear1.rgbaq.A = (int) mgBackColor[3];
400
401 Vif1Packet = &vifpacket[DBuffID];
402 sceVif1PkReset(Vif1Packet);
403
404 if (DBuffID) {
405 ActiveData = &ActiveData0;
406 } else {
407 ActiveData = &ActiveData1;
408 }
409
410 ActiveData->used = 0;
411 WorkBuffer->used = 0;
412
413 *(u_long128 *) &GiftagAD = 0;
414 GiftagAD.EOP = 1;
415 GiftagAD.NREG = 1;
416 GiftagAD.REGS0 = SCE_GIF_PACKED_AD;
417
418 mgZeroVector[0] = 0.0f;
419 mgZeroVector[1] = 0.0f;
420 mgZeroVector[2] = 0.0f;
421 mgZeroVector[3] = 0.0f;
422 mgUnitVector[0] = 1.0f;
423 mgUnitVector[1] = 1.0f;
424 mgUnitVector[2] = 1.0f;
425 mgUnitVector[3] = 1.0f;
426 sceVu0CopyVector(mgZeroVector2, mgZeroVector);
427 sceVu0CopyVector(mgUnitVector2, mgUnitVector);
428 mgZeroVector2[3] = 1.0f;
429 mgUnitVector2[3] = 0.0f;
430
431 sceVu0UnitMatrix(mgUnitMatrix);
432 sceVu0UnitMatrix(mgZeroMatrix);
433 mgZeroMatrix[0][0] = 0.0f;
434 mgZeroMatrix[1][1] = 0.0f;
435 mgZeroMatrix[2][2] = 0.0f;
436 mgZeroMatrix[3][3] = 0.0f;
437
438 if (mgClearBackFlag) {
439 MGClearScreen(mgDBuff.clear0.rgbaq.R, mgDBuff.clear0.rgbaq.G, mgDBuff.clear0.rgbaq.B, mgDBuff.clear0.rgbaq.A);
440 } else {
441 MGClearZBuffer(0);
442 }
443}
444
445static void WaitVSync(int count) {
446 int i, j;
447
448 while (1) {
449 int now = MGGetVSyncCount();
450
451 /* The gap between polls has to cost something and do nothing, which is what a body that
452 negates its own counter twice is: the count is the whole of the effect. */
453 for (i = 0; i < 10; i++) {
454 i = -i;
455 i = -i;
456 }
457
458 if (count != now) {
459 return;
460 }
461
462 for (j = 0; j < 10; j++) {
463 j = -j;
464 j = -j;
465 }
466 }
467}
468
469void MGEndFrame() {
470 int i;
471 int nearest;
472 int j;
473 static int count = 1;
474 static long old_vcount = 0;
475
476 if (count == 0) {
477 printf("CPU %4.1f%%,", 100.0f * ((u_int) (*(volatile u_int *) 0x10000000 - h_count) / 262.0f));
478 }
479
480 sceVif1PkEnd(Vif1Packet, 0);
481 sceVif1PkTerminate(Vif1Packet);
482
483 if (sceGsSyncPath(0, 0) < 0) {
484 printf("******\n");
485 printf("base = %x,cuur = %x\n", Vif1Packet->pBase, Vif1Packet->pCurrent);
486 printf("abuff = %d\n", ActiveData->limit - ActiveData->used);
487 Exit(-1);
488 }
489
490 /* Every picking request the frame collected, answered by reading the depth the frame just
491 drew: an eight-by-eight block of the Z buffer around the point, and the nearest sample in
492 it. A point too close to an edge for that block has no answer and gets -1. */
493 for (i = 0; i < 16; i++) {
494 if (mgPickZBuff[i].enable) {
495 if (mgPickZBuff[i].x < 4 || mgPickZBuff[i].x > 636) {
496 mgPickZBuff[i].z = -1;
497 } else if (mgPickZBuff[i].y < 4 || mgPickZBuff[i].y > SCREEN_HEIGHT - 4) {
498 mgPickZBuff[i].z = -1;
499 } else {
500 WorkBuffer->used = 0;
501 int *block = (int *) WorkBuffer->Alloc(2048);
502
503 FlushCache(0);
504 sceGsSetDefStoreImage(&gs_simage, (mgZBuffer.bits.zbp << 11) >> 6, 10, 48, mgPickZBuff[i].x - 4, (mgPickZBuff[i].y >> 1) - 4, 8, 8);
505 FlushCache(0);
506 sceGsExecStoreImage(&gs_simage, (u_long128 *) block);
507 sceGsSyncPath(0, 0);
508
509 nearest = block[0];
510 nearest &= 0xffffff;
511
512 for (j = 0; j < 64; j++) {
513 int depth = block[j] & 0xffffff;
514
515 if (depth < nearest) {
516 nearest = depth;
517 }
518 }
519
520 mgPickZBuff[i].z = nearest;
521 }
522 }
523 }
524
525 DmaCH1->chcr.TTE = 1;
526
527 u_int flip = *(volatile u_int *) 0x10000000;
528
529 /* A sync passed between the last flip and this one, so the field parity the handler recorded
530 describes a field this frame is no longer in and everything keyed on it drops out. */
531 over_vsync = 0;
532
533 if (old_vcount != vcount) {
534 over_vsync = 1;
535 }
536
537 if (mgWaitVSync) {
538 old_vcount = vcount;
539 }
540
541 WaitVSync(old_vcount);
542 old_vcount = vcount;
543
544 static int capture = 0;
545 static int capture_start = 0;
546
547 if (capture && VSyncField__2) {
548 StoreImage();
549 capture = 0;
550 }
551
552 if (count == 0) {
553 printf("FREE %4.1f%%\n", 100.0f * ((u_int) (*(volatile u_int *) 0x10000000 - flip) / 262.0f));
554 }
555
556 count++;
557
558 if (count > 60) {
559 count = 0;
560 }
561
562#ifdef PAL
563 /* The display position is shifted by the screen adjustment only while the swap sends it. */
564 int display_x = mgDBuff.disp0.display.DX;
565 int display_y = mgDBuff.disp0.display.DY;
566#endif
567 sceGsSetHalfOffset(DBuffID ? &mgDBuff.draw1 : &mgDBuff.draw0, 2048, 2048, (short) VSyncField__2 * !over_vsync);
568#ifdef PAL
569 mgDBuff.disp0.display.DX = display_x + mgAdjustX;
570 mgDBuff.disp1.display.DX = display_x + mgAdjustX;
571 mgDBuff.disp0.display.DY = display_y + mgAdjustY;
572 mgDBuff.disp1.display.DY = display_y + mgAdjustY;
573#endif
574 FlushCache(0);
575 sceGsSwapDBuff(&mgDBuff, DBuffID);
576 sceDmaSync(DmaCH2, 0, 0);
577#ifdef PAL
578 mgDBuff.disp0.display.DX = display_x;
579 mgDBuff.disp1.display.DX = display_x;
580 mgDBuff.disp0.display.DY = display_y;
581 mgDBuff.disp1.display.DY = display_y;
582#endif
583
584 static int f = 0;
585 f++;
586
587 DmaCH1 = sceDmaGetChan(1);
588 DmaCH1->chcr.TTE = 1;
589 FlushCache(0);
590 sceDmaSend(DmaCH1, Vif1Packet->pBase);
591 DBuffID = !DBuffID;
592}
593
594void MGFlipWaitVSync(int wait) {
595 mgWaitVSync = wait;
596}
597
598#ifdef PAL
599void MGAdjustScreen(int x, int y) {
600 if (x > 32) {
601 x = 0;
602 }
603
604 if (x < -32) {
605 x = 0;
606 }
607
608 if (y > 32) {
609 y = 0;
610 }
611
612 if (y < -32) {
613 y = 0;
614 }
615
616 /* The display moves in whole pixel pairs. */
617 mgAdjustX = (x >> 1) << 1;
618 mgAdjustY = (y >> 1) << 1;
619}
620#endif
621
622/* Everything a frame is drawn against, recomputed from the projection scale and the two clip planes.
623 The depth it sets up runs from 16700000 at the near plane down to 1 at the far one, which is what
624 both z solves below are for. */
625void MGSetRenderInfo(float scale, float near_z, float far_z) {
626 float two_near;
627 sceVu0FMATRIX perspective;
628 float w = 1.0f;
629
630 float z_range = 16699999;
631
632 float near_far = near_z * far_z;
633 float z_scale = near_far * z_range / (far_z - near_z);
634 float z_offset = -(16700000 * near_z - w * far_z) / (far_z - near_z);
635
636 mgRenderInfo.projection = scale;
637 mgRenderInfo.scale[0] = scale;
638 mgRenderInfo.scale[1] = scale;
639 mgRenderInfo.scale[2] = z_scale;
640 mgRenderInfo.offset[0] = 2048.0f;
641 mgRenderInfo.offset[1] = 2048.0f;
642 mgRenderInfo.offset[2] = z_offset;
643 mgRenderInfo.near[0] = 0.0f;
644 mgRenderInfo.near[1] = 0.0f;
645 mgRenderInfo.near[2] = near_z;
646 mgRenderInfo.far[0] = 4095.9f;
647 mgRenderInfo.far[1] = 4095.9f;
648 mgRenderInfo.far[2] = far_z;
649
650 mgRenderInfo.clip_min[0] = 0.0f;
651 mgRenderInfo.clip_min[1] = 0.0f;
652 mgRenderInfo.clip_min[2] = 0.0f;
653 mgRenderInfo.clip_min[3] = mgRenderInfo.near[2];
654 mgRenderInfo.clip_max[0] = 4095.9f;
655 mgRenderInfo.clip_max[1] = 4095.9f;
656 mgRenderInfo.clip_max[2] = 0.0f;
657 mgRenderInfo.clip_max[3] = mgRenderInfo.far[2];
658
659 sceVu0UnitMatrix(perspective);
660
661 float near_clip = mgRenderInfo.near[2];
662 float far_clip = mgRenderInfo.far[2];
663 float projection = mgRenderInfo.scale[0];
664 float half_height = 2047 * near_clip / projection;
665
666 two_near = 2.0f * near_clip;
667 perspective[0][0] = two_near / (half_height + half_height);
668 perspective[1][1] = perspective[0][0];
669 perspective[2][2] = (far_clip + near_clip) / (far_clip - near_clip);
670 perspective[3][2] = -2.0f * (far_clip * near_clip) / (far_clip - near_clip);
671 perspective[2][3] = 1.0f;
672 perspective[3][3] = 0.0f;
673 sceVu0CopyMatrix(mgRenderInfo.perspective, perspective);
674
675 float z_max = 16700000;
676
677 sceVu0UnitMatrix(mgRenderInfo.viewport);
678 mgRenderInfo.viewport[0][0] = half_height * (projection * w) / near_clip;
679 mgRenderInfo.viewport[1][1] = mgRenderInfo.viewport[0][0];
680 mgRenderInfo.viewport[2][2] = (-z_max + w) / 2.0f;
681 mgRenderInfo.viewport[3][2] = 8350000.5f;
682 mgRenderInfo.viewport[3][0] = 2048.0f;
683 mgRenderInfo.viewport[3][1] = 2048.0f;
684 mgRenderInfo.viewport[3][3] = w;
685}
686
687void MGSetProjection(float scale) {
688 MGSetRenderInfo(scale, mgRenderInfo.near[2], mgRenderInfo.far[2]);
689}
690
691float MGGetProjection() {
692 return mgRenderInfo.projection;
693}
694
695/* The scissor the whole frame is drawn inside, sent down the frame's own packet rather than kept
696 in the draw environment: the rectangle is an origin and an extent, so the far edges are the near
697 ones plus the width and the height. */
698void MGSetWindowRect() {
699 sceGsScissor scissor;
700 sceVif1Packet *packet;
701
702 mgWindowRect.x = 0;
703 mgWindowRect.y = 0;
704 mgWindowRect.width = 640;
705 mgWindowRect.height = SCREEN_HALF_HEIGHT;
706
707 scissor.SCAX0 = mgWindowRect.x >> 1;
708 scissor.SCAX1 = mgWindowRect.x + mgWindowRect.width;
709 scissor.SCAY0 = mgWindowRect.y;
710 scissor.SCAY1 = mgWindowRect.y + mgWindowRect.height;
711
712 packet = GetVif1Packet();
713 sceVif1PkCnt(packet, 0);
714 sceVif1PkOpenDirectCode(packet, 0);
715 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
716 sceVif1PkAddGsAD(packet, SCE_GS_SCISSOR_1, *(u_long *) &scissor);
717 sceVif1PkCloseGifTag(packet);
718 sceVif1PkCloseDirectCode(packet);
719}
720
721void MGSetWindowRect(CRect_i_ rect) {
722 sceGsScissor scissor;
723 sceVif1Packet *packet;
724
725 mgWindowRect = rect;
726
727 scissor.SCAX0 = mgWindowRect.x >> 1;
728 scissor.SCAX1 = mgWindowRect.x + mgWindowRect.width;
729 scissor.SCAY0 = mgWindowRect.y;
730 scissor.SCAY1 = mgWindowRect.y + mgWindowRect.height;
731
732 packet = GetVif1Packet();
733 sceVif1PkCnt(packet, 0);
734 sceVif1PkOpenDirectCode(packet, 0);
735 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
736 sceVif1PkAddGsAD(packet, SCE_GS_SCISSOR_1, *(u_long *) &scissor);
737 sceVif1PkCloseGifTag(packet);
738 sceVif1PkCloseDirectCode(packet);
739}
740
741void MGSetPLight(sceVu0FMATRIX direction, sceVu0FMATRIX color) {
742 sceVu0CopyMatrix(mgRenderInfo.light_direction, direction);
743 sceVu0CopyMatrix(mgRenderInfo.light_color, color);
744 mgRenderInfo.light_color[0][3] = 0.0f;
745 mgRenderInfo.light_color[1][3] = 0.0f;
746 mgRenderInfo.light_color[2][3] = 0.0f;
747 mgRenderInfo.light_color[3][3] = 0.0f;
748 mgRenderInfo.light_direction[3][0] = 0.0f;
749 mgRenderInfo.light_direction[3][1] = 0.0f;
750 mgRenderInfo.light_direction[3][2] = 0.0f;
751 mgRenderInfo.light_direction[3][3] = 0.0f;
752}
753
754void MGGetPLight(sceVu0FMATRIX direction, sceVu0FMATRIX color) {
755 sceVu0CopyMatrix(direction, mgRenderInfo.light_direction);
756 sceVu0CopyMatrix(color, mgRenderInfo.light_color);
757}
758
759void MGSetAmbient(float *ambient) {
760 sceVu0CopyVector(mgRenderInfo.ambient, ambient);
761}
762
763void MGGetAmbient(float *ambient) {
764 sceVu0CopyVector(ambient, mgRenderInfo.ambient);
765}
766
767/* The two overloads below are the only callers and both pass the same two scales, so the vertical
768 squeeze is the engine's rather than any caller's. */
769static void MGSetViewMatrix_sub(sceVu0FMATRIX view, float x_scale, float y_scale) {
770 sceVu0FMATRIX scale;
771 sceVu0FMATRIX screen;
772 sceVu0FVECTOR direction;
773 sceVu0FVECTOR flat;
774
775 sceVu0CopyMatrix(mgRenderInfo.view, view);
776 mgRenderInfo.position[0] = view[3][0];
777 mgRenderInfo.position[1] = view[3][1];
778 mgRenderInfo.position[2] = view[3][2];
779 mgRenderInfo.position[3] = 0.0f;
780
781 sceVu0UnitMatrix(scale);
782 scale[0][0] = x_scale;
783#ifdef PAL
784 /* The taller PAL frame takes a fixed vertical squeeze in place of the caller's. */
785 scale[1][1] = 0.5f;
786#else
787 scale[1][1] = y_scale;
788#endif
789 MulMatrix(mgRenderInfo.view_scaled, scale, view);
790
791 sceVu0UnitMatrix(screen);
792 screen[1][1] = mgRenderInfo.scale[0];
793 screen[0][0] = mgRenderInfo.scale[0];
794 screen[2][2] = mgRenderInfo.offset[2];
795 screen[3][3] = 0.0f;
796 screen[2][1] = 2048.0f;
797 screen[2][0] = 2048.0f;
798 screen[2][3] = 1.0f;
799 screen[3][2] = mgRenderInfo.scale[2];
800 MulMatrix(mgRenderInfo.view_screen, screen, mgRenderInfo.view_scaled);
801 sceVu0CopyMatrix(mgRenderInfo.screen, screen);
802
803 direction[0] = -mgRenderInfo.view[0][2];
804 direction[1] = -mgRenderInfo.view[1][2];
805 direction[2] = -mgRenderInfo.view[2][2];
806 direction[3] = 0.0f;
807 flat[0] = direction[0];
808 flat[1] = 0.0f;
809 flat[2] = direction[2];
810 flat[3] = 0.0f;
811 sceVu0Normalize(flat, flat);
812 mgRenderInfo.yaw = atan2f(flat[0], flat[2]);
813 mgRenderInfo.pitch = -atan2f(direction[1], sqrtf(direction[0] * direction[0] + direction[2] * direction[2]));
814 /* Both angles are already stored by here, so the direction is normalised into a local that
815 goes out of scope with the call and nothing ever reads. */
816 direction[3] = 0.0f;
817 sceVu0Normalize(direction, direction);
818}
819
820void MGSetViewMatrix(sceVu0FMATRIX view) {
821 MGSetViewMatrix_sub(view, 1.0f, 0.47f);
822}
823
824void MGSetViewMatrix(sceVu0FMATRIX view, float *position) {
825 MGSetViewMatrix(view);
826 sceVu0CopyVector(mgRenderInfo.view_position, position);
827 mgRenderInfo.position[0] = position[0];
828 mgRenderInfo.position[1] = position[1];
829 mgRenderInfo.position[2] = position[2];
830 mgRenderInfo.position[3] = 0.0f;
831}
832
833void MGSetFogParm(float near_z, float far_z, u_char r, u_char g, u_char b, float far_fog, float near_fog) {
834 mgRenderInfo.fog_far = far_fog;
835 mgRenderInfo.fog_near = near_fog;
836 mgRenderInfo.fog_a = ((far_fog + near_fog) + (far_fog - near_fog) * (far_z + near_z) / (far_z - near_z)) / 2.0f;
837 mgRenderInfo.fog_b = -far_z * near_z * (far_fog - near_fog) / (far_z - near_z);
838 mgRenderInfo.fog_red = r;
839 mgRenderInfo.fog_green = g;
840 mgRenderInfo.fog_blue = b;
841}
842
843/* A colour every component of which is negative is the request not to clear at all; anything else
844 clears, and each component is clamped into the eight bits the clear is written through. */
845void MGSetBGColor(float red, float green, float blue, float alpha) {
846 if (red < 0.0f && green < 0.0f && blue < 0.0f && alpha < 0.0f) {
847 mgClearBackFlag = 0;
848 } else {
849 mgClearBackFlag = 1;
850 }
851
852 if (red < 0.0f) {
853 red = 0.0f;
854 }
855
856 if (green < 0.0f) {
857 green = 0.0f;
858 }
859
860 if (blue < 0.0f) {
861 blue = 0.0f;
862 }
863
864 if (alpha < 0.0f) {
865 alpha = 0.0f;
866 }
867
868 if (red > 255.0f) {
869 red = 255.0f;
870 }
871
872 if (green > 255.0f) {
873 green = 255.0f;
874 }
875
876 if (blue > 255.0f) {
877 blue = 255.0f;
878 }
879
880 if (alpha > 255.0f) {
881 alpha = 255.0f;
882 }
883
884 mgBackColor[0] = red;
885 mgBackColor[1] = green;
886 mgBackColor[2] = blue;
887 mgBackColor[3] = alpha;
888}
889
890void MGSetBGColor(float *color) {
891 MGSetBGColor(color[0], color[1], color[2], color[3]);
892}
893
894void MGGetBGColor(float *color) {
895 sceVu0CopyVector(color, mgBackColor);
896}
897
898void MGScisioringForce(int force) {
899 mgRenderInfo.scissoring = force;
900}
901
902sceVif1Packet *GetVif1Packet() {
903 return Vif1Packet;
904}
905
906/* A point through the camera and onto the screen, in floats rather than on the Vector Unit: the
907 view is applied, the perspective divide is done by hand, and the screen scale and offset the
908 render info carries turn the result into the 12.4 fixed point the GS takes. The answer says
909 whether the point is on screen, and a point behind the near plane is answered no and clamped to
910 the plane rather than divided by. */
911int MGRotTransPers(int *screen, float *position, int fog) {
912 sceVu0FVECTOR point;
913 int visible = true;
914 float w;
915
916 sceVu0ApplyMatrix(point, mgRenderInfo.view_scaled, position);
917
918 if (point[2] < 1.0f) {
919 visible = false;
920 point[2] = 1.0f;
921 }
922
923 w = 1.0f / point[2];
924
925 point[0] *= w;
926 point[1] *= w;
927 point[0] *= mgRenderInfo.scale[0];
928 point[1] *= mgRenderInfo.scale[1];
929 point[2] = w * mgRenderInfo.scale[2];
930 point[0] += mgRenderInfo.offset[0];
931 point[1] += mgRenderInfo.offset[1];
932 point[2] += mgRenderInfo.offset[2];
933
934 screen[0] = (int) (16.0f * point[0]);
935 screen[1] = (int) (16.0f * point[1]);
936 screen[2] = (int) point[2];
937
938 if (fog) {
939 float density = mgRenderInfo.fog_a + mgRenderInfo.fog_b * w;
940
941 if (mgRenderInfo.fog_far > density) {
942 density = mgRenderInfo.fog_far;
943 }
944
945 if (mgRenderInfo.fog_near < density) {
946 density = mgRenderInfo.fog_near;
947 }
948
949 screen[3] = (int) density;
950 }
951
952 if (point[0] < 0.0f || point[1] < 0.0f || point[0] > 4095 || point[1] > 4095) {
953 visible = false;
954 }
955
956 return visible;
957}
958
959/* The same transform for something drawn flat against the screen: the vertical squeeze
960 MGSetViewMatrix_sub bakes into the view is undone, the result is left in whole pixels rather than
961 scaled into 12.4, and the pair is moved off the screen centre the offset put it at. */
962int MGRotTransPers2D(int *screen, float *position, int fog) {
963 sceVu0FVECTOR point;
964 int visible = true;
965 float w;
966
967 sceVu0ApplyMatrix(point, mgRenderInfo.view_scaled, position);
968
969 if (point[2] < 1.0f) {
970 visible = false;
971 point[2] = 1.0f;
972 }
973
974 w = 1.0f / point[2];
975
976 point[0] *= w;
977 point[1] *= 2.0f * w;
978 point[0] *= mgRenderInfo.scale[0];
979 point[1] *= mgRenderInfo.scale[1];
980 point[2] = w * mgRenderInfo.scale[2];
981 point[0] += mgRenderInfo.offset[0];
982 point[1] += mgRenderInfo.offset[1];
983 point[2] += mgRenderInfo.offset[2];
984
985 screen[0] = (int) point[0];
986 screen[1] = (int) point[1];
987 screen[2] = (int) point[2];
988
989 if (fog) {
990 float density = mgRenderInfo.fog_a + mgRenderInfo.fog_b * w;
991
992 if (mgRenderInfo.fog_far > density) {
993 density = mgRenderInfo.fog_far;
994 }
995
996 if (mgRenderInfo.fog_near < density) {
997 density = mgRenderInfo.fog_near;
998 }
999
1000 screen[3] = (int) density;
1001 }
1002
1003 if (point[0] < 0.0f || point[1] < 0.0f || point[0] > 4095 || point[1] > 4095) {
1004 visible = false;
1005 }
1006
1007 screen[0] -= 1728;
1008 screen[1] -= 2048 - SCREEN_HALF_HEIGHT;
1009 return visible;
1010}
1011#ifndef PORT
1012/* Both of a screen-facing sprite's opposite corners out of a single transform: the half-width and
1013 half-height are scaled by the render info once and then by the same Q the perspective divide
1014 produced, so the second corner costs an add rather than a second pass through the matrix. The
1015 matrix is the one that already carries the screen scale and offset, which is what leaves the
1016 result ready for vftoi4 without the arithmetic MGRotTransPers does by hand. Whether the sprite is
1017 on screen is read off the Vector Unit's sticky status flags rather than compared as floats: four
1018 subtractions leave a bit set for any field that came out zero or negative, testing both corners
1019 against 0 and against 4096.0 and, through w, against the eye. */
1020int MGRotTransPers3DSprite(register int *top_left, register int *bottom_right, register float *position, float width, float height, int fog) {
1021 sceVu0FVECTOR half;
1022 register float *half_size;
1023 register float *view;
1024 register int visible = false;
1025
1026 half[0] = 0.5f * width * mgRenderInfo.scale[0];
1027 half[1] = 0.5f * height * mgRenderInfo.scale[1];
1028
1029 if (!fog) {
1030 view = &mgRenderInfo.view_screen[0][0];
1031 half_size = half;
1032
1033 asm {
1034 lqc2 vf1, 0(view)
1035 lqc2 vf2, 16(view)
1036 lqc2 vf3, 32(view)
1037 lqc2 vf4, 48(view)
1038 lqc2 vf5, 0(position)
1039 lqc2 vf6, 0(half_size)
1040 vmulax ACC, vf1, vf5
1041 vmadday ACC, vf2, vf5
1042 vmaddaz ACC, vf3, vf5
1043 vmaddw vf10, vf4, vf5
1044 vdiv Q, vf0w, vf10w
1045 vwaitq
1046 vmulq.xyz vf10, vf10, Q
1047 vmulq.xy vf6, vf6, Q
1048 vmove vf11, vf10
1049 vmove vf12, vf10
1050 vsub.xy vf11, vf11, vf6
1051 vadd.xy vf12, vf12, vf6
1052 vsub vf20, vf0, vf0
1053 ori visible, $0, 0x4580
1054 dsll visible, visible, 16
1055 dsll visible, visible, 16
1056 ori visible, visible, 0x4580
1057 dsll visible, visible, 16
1058 qmtc2 visible, vf21
1059 ctc2 $0, $16
1060 vsub.xyw vf25, vf11, vf20
1061 vsub.xy vf25, vf21, vf11
1062 vsub.xyw vf25, vf12, vf20
1063 vsub.xy vf25, vf21, vf12
1064 vnop
1065 vnop
1066 vnop
1067 vnop
1068 vnop
1069 cfc2 visible, $16
1070 andi visible, visible, 0xc0
1071 vftoi4.xy vf11, vf11
1072 vftoi0.z vf11, vf11
1073 vftoi4.xy vf12, vf12
1074 vftoi0.z vf12, vf12
1075 sqc2 vf11, 0(top_left)
1076 sqc2 vf12, 0(bottom_right)
1077 sltu visible, $0, visible
1078 xori visible, visible, 0x1
1079 andi visible, visible, 0xff
1080 }
1081 } else {
1082 sceVu0FVECTOR fog_parm = {0.0f, 0.0f, 0.0f, 0.0f};
1083 register float *fog_range;
1084 register float *fog_view;
1085
1086 fog_parm[0] = mgRenderInfo.fog_a;
1087 fog_parm[1] = mgRenderInfo.fog_near;
1088 fog_parm[2] = mgRenderInfo.fog_far;
1089 fog_parm[3] = mgRenderInfo.fog_b;
1090
1091 fog_view = &mgRenderInfo.view_screen[0][0];
1092 half_size = half;
1093 fog_range = fog_parm;
1094
1095 asm {
1096 lqc2 vf1, 0(fog_view)
1097 lqc2 vf2, 16(fog_view)
1098 lqc2 vf3, 32(fog_view)
1099 lqc2 vf4, 48(fog_view)
1100 lqc2 vf5, 0(position)
1101 lqc2 vf6, 0(half_size)
1102 lqc2 vf7, 0(fog_range)
1103 vmulax ACC, vf1, vf5
1104 vmadday ACC, vf2, vf5
1105 vmaddaz ACC, vf3, vf5
1106 vmaddw vf10, vf4, vf5
1107 vdiv Q, vf0w, vf10w
1108 vwaitq
1109 vmulq.xyz vf10, vf10, Q
1110 vmulq.xy vf6, vf6, Q
1111 vmulax.w ACC, vf0, vf7
1112 vmaddq.w vf31, vf7, Q
1113 vminiy.w vf31, vf31, vf7
1114 vmaxz.w vf31, vf31, vf7
1115 vmove vf11, vf10
1116 vmove vf12, vf10
1117 vsub.xy vf11, vf11, vf6
1118 vadd.xy vf12, vf12, vf6
1119 vsub vf20, vf0, vf0
1120 ori visible, $0, 0x4580
1121 dsll visible, visible, 16
1122 dsll visible, visible, 16
1123 ori visible, visible, 0x4580
1124 dsll visible, visible, 16
1125 qmtc2 visible, vf21
1126 ctc2 $0, $16
1127 vsub.xyw vf25, vf11, vf20
1128 vsub.xy vf25, vf21, vf11
1129 vsub.xyw vf25, vf12, vf20
1130 vsub.xy vf25, vf21, vf12
1131 vnop
1132 vnop
1133 vnop
1134 vnop
1135 vnop
1136 cfc2 visible, $16
1137 andi visible, visible, 0xc0
1138 vftoi4.xy vf11, vf11
1139 vftoi0.z vf11, vf11
1140 vftoi0.w vf11, vf31
1141 vftoi4.xy vf12, vf12
1142 vftoi0.z vf12, vf12
1143 vftoi0.w vf12, vf31
1144 sqc2 vf11, 0(top_left)
1145 sqc2 vf12, 0(bottom_right)
1146 sltu visible, $0, visible
1147 xori visible, visible, 0x1
1148 andi visible, visible, 0xff
1149 }
1150 }
1151
1152 if (visible == 0) {
1153 return 0;
1154 }
1155
1156 return visible;
1157}
1158
1159/* One vertex colour from one normal, on the Vector Unit in macro mode: the normal through the three
1160 light directions, clamped up at zero so a light behind the surface contributes nothing, then the
1161 four light colours accumulated onto the ambient and clamped down at 255. The caller's own buffer
1162 is where the ceiling comes from — 255 is written into it first and read straight back as a
1163 quadword, so the clamp costs no constant of its own. */
1164void MGCalcColor(register float *color, register float *normal) {
1165 register float *intensity;
1166 register float *direction;
1167 register float *ambient;
1168
1169 color[0] = 255.0f;
1170
1171 direction = &mgRenderInfo.light_direction[0][0];
1172 intensity = &mgRenderInfo.light_color[0][0];
1173 ambient = mgRenderInfo.ambient;
1174
1175 asm {
1176 lqc2 vf20, 0(color)
1177 lqc2 vf10, 0(normal)
1178 lqc2 vf1, 0(direction)
1179 lqc2 vf2, 16(direction)
1180 lqc2 vf3, 32(direction)
1181 lqc2 vf5, 0(intensity)
1182 lqc2 vf6, 16(intensity)
1183 lqc2 vf7, 32(intensity)
1184 lqc2 vf8, 48(intensity)
1185 lqc2 vf9, 0(ambient)
1186 vmulax ACC, vf1, vf10
1187 vmadday ACC, vf2, vf10
1188 vmaddz vf11, vf3, vf10
1189 vnop
1190 vnop
1191 vnop
1192 vmaxx vf12, vf11, vf0
1193 vnop
1194 vnop
1195 vmulaw ACC, vf9, vf0
1196 vmaddax ACC, vf5, vf12
1197 vmadday ACC, vf6, vf12
1198 vmaddaz ACC, vf7, vf12
1199 vmaddw vf13, vf8, vf12
1200 vnop
1201 vnop
1202 vnop
1203 vminix vf14, vf13, vf20
1204 vnop
1205 vnop
1206 vnop
1207 sqc2 vf14, 0(color)
1208 }
1209}
1210#endif
1211/* Where one point falls outside the screen, as six bits rather than a yes or no: near, far, and one
1212 per side. The half-widths are the guard band's and not the window's, so a point off the edge is
1213 still inside until it is off by half a screen. A point at or behind the eye keeps its bits and
1214 has every side test reversed, which is what makes the caller's running AND mean the same thing
1215 for a corner in front and a corner behind. */
1216int MGClipVertex(float *position) {
1217 sceVu0FVECTOR point;
1218 int outside = 0;
1219 float w;
1220 float offset;
1221 float half_width = 320.0f;
1222 float half_height = SCREEN_QUARTER_HEIGHT_F;
1223
1224 sceVu0ApplyMatrix(point, mgRenderInfo.view_screen, position);
1225
1226 if (0.0f == point[3]) {
1227 point[3] = 1.0f;
1228 }
1229
1230 w = 1.0f / point[3];
1231 point[0] *= w;
1232 point[1] *= w;
1233
1234 if (point[3] < mgRenderInfo.near[2]) {
1235 outside |= 0x20;
1236 }
1237
1238 if (point[3] > mgRenderInfo.far[2]) {
1239 outside |= 0x10;
1240 }
1241
1242 if (point[3] > 0.0f) {
1243 offset = point[1] - mgRenderInfo.offset[1];
1244
1245 if (offset < -half_height) {
1246 outside |= 0x8;
1247 }
1248
1249 if (offset > half_height) {
1250 outside |= 0x4;
1251 }
1252
1253 offset = point[0] - mgRenderInfo.offset[0];
1254
1255 if (offset < -half_width) {
1256 outside |= 0x2;
1257 }
1258
1259 if (offset > half_width) {
1260 outside |= 0x1;
1261 }
1262 } else {
1263 offset = point[1] - mgRenderInfo.offset[1];
1264
1265 if (offset > -half_height) {
1266 outside |= 0x8;
1267 }
1268
1269 if (offset < half_height) {
1270 outside |= 0x4;
1271 }
1272
1273 offset = point[0] - mgRenderInfo.offset[0];
1274
1275 if (offset > -half_width) {
1276 outside |= 0x2;
1277 }
1278
1279 if (offset < half_width) {
1280 outside |= 0x1;
1281 }
1282 }
1283
1284 return outside;
1285}
1286
1287/* A bound rejected only when every one of its eight corners is outside the same side, which is what
1288 the running AND of the six bits says. Answering zero is answering "keep it": a corner that is
1289 inside everything has no bits at all and ends the walk on the spot. */
1290int MGClipBox(CBoxVu0 *box) {
1291 sceVu0FVECTOR corner[8];
1292 /* Only two of these are ever filled. The frame leaves twelve to sixteen bytes for the array,
1293 which is what rules two out; three and four are the same bytes and this is the tidier. */
1294 float *extreme[4];
1295 int i;
1296 int outside;
1297
1298 extreme[0] = box->min;
1299 extreme[1] = box->max;
1300 outside = 63;
1301
1302 for (i = 0; i < 8; i++) {
1303 corner[i][3] = 1.0f;
1304 corner[i][0] = extreme[(i & 1) != 0][0];
1305 corner[i][1] = extreme[(i & 2) != 0][1];
1306 corner[i][2] = extreme[(i & 4) != 0][2];
1307
1308 int bits = MGClipVertex(corner[i]);
1309
1310 if (!bits) {
1311 return 0;
1312 }
1313
1314 outside &= bits;
1315
1316 if (!outside) {
1317 return 0;
1318 }
1319 }
1320
1321 return 1;
1322}
1323
1324void MGDraw(CFrame *frame) {
1325 if (frame) {
1326 sceVif1PkTerminate(Vif1Packet);
1327 sceVif1PkReserve(Vif1Packet, frame->DrawVu1(Vif1Packet->pCurrent, &mgRenderInfo));
1328 }
1329}
1330
1331/* Each of the four takes the register it names down the frame's packet, and a null argument means
1332 the module's own copy of that register rather than nothing to send. */
1333void MGSetGsTEST(sceGsTest *test) {
1334 sceVif1Packet *packet;
1335
1336 packet = GetVif1Packet();
1337 sceVif1PkCnt(packet, 0);
1338 sceVif1PkOpenDirectCode(packet, 0);
1339 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
1340
1341 if (test) {
1342 sceVif1PkAddGsAD(packet, SCE_GS_TEST_1, *(u_long *) test);
1343 } else {
1344 sceVif1PkAddGsAD(packet, SCE_GS_TEST_1, *(u_long *) &mgPixelTest);
1345 }
1346
1347 sceVif1PkCloseGifTag(packet);
1348 sceVif1PkCloseDirectCode(packet);
1349}
1350
1351void MGSetGsZBUF(sceGsZbuf *zbuf) {
1352 sceVif1Packet *packet;
1353
1354 packet = GetVif1Packet();
1355 sceVif1PkCnt(packet, 0);
1356 sceVif1PkOpenDirectCode(packet, 0);
1357 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
1358
1359 if (zbuf) {
1360 sceVif1PkAddGsAD(packet, SCE_GS_ZBUF_1, *(u_long *) zbuf);
1361 } else {
1362 sceVif1PkAddGsAD(packet, SCE_GS_ZBUF_1, *(u_long *) &mgZBuffer);
1363 }
1364
1365 sceVif1PkCloseGifTag(packet);
1366 sceVif1PkCloseDirectCode(packet);
1367}
1368
1369void MGSetGsALPHA(sceGsAlpha *alpha) {
1370 sceVif1Packet *packet;
1371
1372 packet = GetVif1Packet();
1373 sceVif1PkCnt(packet, 0);
1374 sceVif1PkOpenDirectCode(packet, 0);
1375 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
1376
1377 if (alpha) {
1378 sceVif1PkAddGsAD(packet, SCE_GS_ALPHA_1, *(u_long *) alpha);
1379 } else {
1380 sceVif1PkAddGsAD(packet, SCE_GS_ALPHA_1, *(u_long *) &mgAlpha);
1381 }
1382
1383 sceVif1PkCloseGifTag(packet);
1384 sceVif1PkCloseDirectCode(packet);
1385}
1386
1387void MGSetGsTEXA(sceGsTexa *texa) {
1388 sceVif1Packet *packet;
1389
1390 packet = GetVif1Packet();
1391 sceVif1PkCnt(packet, 0);
1392 sceVif1PkOpenDirectCode(packet, 0);
1393 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
1394
1395 if (texa) {
1396 sceVif1PkAddGsAD(packet, SCE_GS_TEXA, *(u_long *) texa);
1397 } else {
1398 sceVif1PkAddGsAD(packet, SCE_GS_TEXA, *(u_long *) &mgTexa);
1399 }
1400
1401 sceVif1PkCloseGifTag(packet);
1402 sceVif1PkCloseDirectCode(packet);
1403}
1404
1405/* The frame buffer read back as a texture, which is what every effect that samples the picture it
1406 is drawing over needs: a frame buffer page is 32 texture blocks, and the size is the largest the
1407 register can name rather than the buffer's own. */
1408void MGGetFBuffTex(sceGsTex0 *tex0) {
1409 sceGsFrame *frame = DBuffID ? &mgDBuff.draw1.frame1 : &mgDBuff.draw0.frame1;
1410
1411 *(u_long *) tex0 = SCE_GS_SET_TEX0(frame->FBP << 5, frame->FBW, frame->PSM, 10, 8, 0, 0, 0, 0, 0, 0, 0);
1412}
1413
1414void MGGetFBuffBackTex(sceGsTex0 *tex0) {
1415 sceGsFrame *frame = DBuffID ? &mgDBuff.draw0.frame1 : &mgDBuff.draw1.frame1;
1416
1417 *(u_long *) tex0 = SCE_GS_SET_TEX0(frame->FBP << 5, frame->FBW, frame->PSM, 10, 8, 0, 0, 0, 0, 0, 0, 0);
1418}
1419
1420/* A rectangle moved from one place in GS local memory to another, which is the transfer the GS
1421 does entirely on its own side: four registers down the frame's packet and no pixels through the
1422 bus. The second packet is a TEXFLUSH on its own, because the destination may be a texture the
1423 cache still holds the old contents of. */
1424void MGMoveImage(sceGsTex0 *src, const CRect_i_ &rect, sceGsTex0 *dst, int dst_x, int dst_y, int direction) {
1425 sceVif1Packet *packet;
1426
1427 packet = Vif1Packet;
1428 sceVif1PkCnt(packet, 0);
1429 sceVif1PkOpenDirectCode(packet, 0);
1430 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
1431 sceVif1PkAddGsAD(packet, SCE_GS_BITBLTBUF, SCE_GS_SET_BITBLTBUF(src->TBP0, src->TBW, src->PSM, dst->TBP0, dst->TBW, dst->PSM));
1432 sceVif1PkAddGsAD(packet, SCE_GS_TRXPOS, SCE_GS_SET_TRXPOS(rect.x, rect.y, dst_x, dst_y, direction));
1433 sceVif1PkAddGsAD(packet, SCE_GS_TRXREG, SCE_GS_SET_TRXREG(rect.width, rect.height));
1434 sceVif1PkAddGsAD(packet, SCE_GS_TRXDIR, SCE_GS_SET_TRXDIR(SCE_GS_LOCAL_LOCAL));
1435 sceVif1PkCloseGifTag(packet);
1436 sceVif1PkCloseDirectCode(packet);
1437
1438 sceVif1PkCnt(packet, 0);
1439 sceVif1PkOpenDirectCode(packet, 0);
1440 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
1441 sceVif1PkAddGsAD(packet, SCE_GS_TEXFLUSH, 0);
1442 sceVif1PkCloseGifTag(packet);
1443 sceVif1PkCloseDirectCode(packet);
1444}
1445
1446/* The same move as MGMoveImage where the two rectangles are not the same size, which the GS has no
1447 transfer for: this draws instead. One textured sprite carries the source rectangle onto the
1448 destination, so the scale is the sampler's, and every register it needs is set and put back
1449 inside the one packet. The destination is reached by pointing FRAME_1 at it, which is why the
1450 frame buffer's own texture is read before anything is written and FRAME_1 restored from it at
1451 the end. The two offsets stay statics rather than becoming literals because they were meant to
1452 be adjusted, and dyy carries the interlace correction on top of its own value. */
1453void MGStretchMoveImage(sceGsTex0 *src, const CRect_i_ &src_rect, sceGsTex0 *dst, const CRect_i_ &dst_rect) {
1454 sceVif1Packet *packet;
1455 sceGsTex0 frame_tex;
1456 sceGsTest test;
1457 sceGsTexa texa;
1458 sceGsTex1 tex1;
1459 sceGsAlpha alpha;
1460 sceGsZbuf zbuf;
1461 int dy;
1462
1463 packet = GetVif1Packet();
1464 MGGetFBuffTex(&frame_tex);
1465
1466 sceVif1PkCnt(packet, 0);
1467 sceVif1PkOpenDirectCode(packet, 0);
1468 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
1469
1470 test = mgPixelTest;
1471 test.bits.ate = 0;
1472 test.bits.date = 0;
1473 test.bits.zte = 1;
1474 test.bits.ztst = SCE_GS_ALWAYS;
1475
1476 texa = mgTexa;
1477 texa.AEM = 1;
1478 texa.TA0 = 128;
1479
1480 tex1 = mgTEX1Env;
1481 tex1.MMIN = 1;
1482
1483 alpha = mgAlpha;
1484 alpha.bits.a = 2;
1485 alpha.bits.b = 2;
1486 alpha.bits.c = 2;
1487 alpha.bits.d = 0;
1488
1489 zbuf = mgZBuffer;
1490 zbuf.bits.zmsk = 1;
1491
1492 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEXFLUSH, 0);
1493 sceVif1PkAddGsAD(packet, SCE_GS_TEST_1, *(u_long *) &test);
1494 sceVif1PkAddGsAD(packet, SCE_GS_TEX0_1, *(u_long *) src);
1495 sceVif1PkAddGsAD(packet, SCE_GS_TEX1_1, *(u_long *) &tex1);
1496 sceVif1PkAddGsAD(packet, SCE_GS_TEXA, *(u_long *) &texa);
1497 sceVif1PkAddGsAD(packet, SCE_GS_ALPHA_1, *(u_long *) &alpha);
1498 sceVif1PkAddGsAD(packet, SCE_GS_ZBUF_1, *(u_long *) &zbuf);
1499
1500 static int dyy = -10;
1501 static int dx = -8;
1502
1503 if (VSyncField__2) {
1504 dy = dyy;
1505 } else {
1506 dy = dyy + 16;
1507 }
1508
1509 if (over_vsync) {
1510 dy = 0;
1511 }
1512
1513 sceVif1PkAddGsAD(packet, SCE_GS_FRAME_1, SCE_GS_SET_FRAME(dst->TBP0 >> 5, dst->TBW, dst->PSM, 0));
1514 sceVif1PkAddGsAD(packet, SCE_GS_PRIM, SCE_GS_SET_PRIM(SCE_GS_PRIM_SPRITE, 0, 1, 0, 1, 0, 1, 0, 0));
1515 sceVif1PkAddGsAD(packet, SCE_GS_RGBAQ, SCE_GS_SET_RGBAQ(128, 128, 128, 128, 0));
1516
1517 sceVif1PkAddGsAD(packet, SCE_GS_UV, SCE_GS_SET_UV(src_rect.x, src_rect.y));
1518 sceVif1PkAddGsAD(packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(dst_rect.x + 27648 + dx, dst_rect.y + GS_Y_OFFSET + dy, 0, 0));
1519 sceVif1PkAddGsAD(packet, SCE_GS_UV, SCE_GS_SET_UV(src_rect.x + src_rect.width, src_rect.y + src_rect.height));
1520 sceVif1PkAddGsAD(packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(dst_rect.x + 27648 + dst_rect.width + dx, dst_rect.y + dst_rect.height + GS_Y_OFFSET + dy, 0, 0));
1521
1522 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEXFLUSH, 0);
1523 sceVif1PkAddGsAD(packet, SCE_GS_FRAME_1, SCE_GS_SET_FRAME(frame_tex.TBP0 >> 5, frame_tex.TBW, frame_tex.PSM, 0));
1524 sceVif1PkAddGsAD(packet, SCE_GS_TEST_1, *(u_long *) &mgPixelTest);
1525 sceVif1PkAddGsAD(packet, SCE_GS_TEST_1, *(u_long *) &mgPixelTest);
1526 sceVif1PkAddGsAD(packet, SCE_GS_ALPHA_1, *(u_long *) &mgAlpha);
1527 sceVif1PkAddGsAD(packet, SCE_GS_ZBUF_1, *(u_long *) &mgZBuffer);
1528 sceVif1PkCloseGifTag(packet);
1529 sceVif1PkCloseDirectCode(packet);
1530}
1531
1532/* Copies both interlaced fields into the destination, one 640-by-1 line at a time. */
1533void MGMoveFrameBuffImage(sceGsTex0 *dst, int x, int y, int direction) {
1534 sceGsTex0 tex[2];
1535 CRect_i_ even;
1536 CRect_i_ odd;
1537 int i;
1538
1539 MGGetFBuffTex(&tex[0]);
1540 MGGetFBuffBackTex(&tex[1]);
1541
1542 for (i = 0; i < SCREEN_HALF_HEIGHT; i++) {
1543 even.x = 0;
1544 even.y = i;
1545 even.width = 640;
1546 even.height = 1;
1547 MGMoveImage(&tex[!VSyncField__2], even, dst, 0, i * 2, 0);
1548
1549 odd.x = 0;
1550 odd.y = i;
1551 odd.width = 640;
1552 odd.height = 1;
1553 MGMoveImage(&tex[VSyncField__2], odd, dst, 0, i * 2 + 1, 0);
1554 }
1555}
1556
1557void MGFillBox(const CRect_i_ &rect, unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
1558 float q = 1.0f;
1559 sceGsTest test;
1560 sceGsZbuf zbuf;
1561
1562 sceVif1PkCnt(Vif1Packet, 0);
1563 sceVif1PkOpenDirectCode(Vif1Packet, 0);
1564 sceVif1PkOpenGifTag(Vif1Packet, *(u_long128 *) &GiftagAD);
1565 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEXFLUSH, 0);
1566 sceVif1PkAddGsAD(Vif1Packet, 0x14, 1);
1567 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_PRIM, 0x146);
1568
1569 test = mgPixelTest;
1570 test.bits.ate = 1;
1571 test.bits.aref = 0;
1572 test.bits.atst = 1;
1573 test.bits.zte = 1;
1574 test.bits.ztst = 1;
1575 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEST_1, *(u_long *) &test);
1576
1577 zbuf = mgZBuffer;
1578 zbuf.bits.zmsk = 1;
1579 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_ZBUF_1, *(u_long *) &zbuf);
1580
1581 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_RGBAQ, SCE_GS_SET_RGBAQ(r, g, b, a, *(u_int *) &q));
1582 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(rect.x + 0x6C00, rect.y + GS_Y_OFFSET, 0, 0));
1583 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(rect.x + 0x6C00 + rect.width, rect.y + GS_Y_OFFSET + rect.height, 0, 0));
1584 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEXFLUSH, 0);
1585 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEST_1, *(u_long *) &mgPixelTest);
1586 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_ZBUF_1, *(u_long *) &mgZBuffer);
1587 sceVif1PkCloseGifTag(Vif1Packet);
1588 sceVif1PkCloseDirectCode(Vif1Packet);
1589}
1590
1591/* Writes depth over the 640-by-224 field while leaving colour unchanged. */
1592void MGClearZBuffer(int mode) {
1593 sceGsTest test;
1594 sceGsZbuf zbuf;
1595 sceGsAlpha alpha;
1596
1597 sceVif1PkCnt(Vif1Packet, 0);
1598 sceVif1PkOpenDirectCode(Vif1Packet, 0);
1599 sceVif1PkOpenGifTag(Vif1Packet, *(u_long128 *) &GiftagAD);
1600
1601 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEX1_1, 1);
1602 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_PRIM, SCE_GS_SET_PRIM(4, 0, 0, 0, 1, 0, 1, 0, 0));
1603
1604 test = mgPixelTest;
1605 test.bits.ate = 0;
1606 test.bits.zte = 1;
1607 test.bits.ztst = SCE_GS_ALWAYS;
1608 test.bits.date = 0;
1609 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEST_1, *(u_long *) &test);
1610
1611 zbuf = mgZBuffer;
1612 zbuf.bits.zmsk = 0;
1613 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_ZBUF_1, *(u_long *) &zbuf);
1614
1615 alpha = mgAlpha;
1616 alpha.bits.a = 2;
1617 alpha.bits.b = 2;
1618 alpha.bits.c = 2;
1619 alpha.bits.d = 1;
1620 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_ALPHA_1, *(u_long *) &alpha);
1621
1622 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_RGBAQ, SCE_GS_SET_RGBAQ(128, 128, 128, 128, 0));
1623 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(27648, GS_Y_OFFSET, mode, 0));
1624 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(27648 + 640 * 16, GS_Y_OFFSET, mode, 0));
1625 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(27648, GS_Y_OFFSET + SCREEN_HALF_HEIGHT * 16, mode, 0));
1626 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(27648 + 640 * 16, GS_Y_OFFSET + SCREEN_HALF_HEIGHT * 16, mode, 0));
1627
1628 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEXFLUSH, 0);
1629 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEST_1, *(u_long *) &mgPixelTest);
1630 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_ZBUF_1, *(u_long *) &mgZBuffer);
1631 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_ALPHA_1, *(u_long *) &mgAlpha);
1632
1633 sceVif1PkCloseGifTag(Vif1Packet);
1634 sceVif1PkCloseDirectCode(Vif1Packet);
1635}
1636
1637/* Writes colour and zero depth over the 640-by-224 field in 16-pixel-wide strips. */
1638void MGClearScreen(u_char r, u_char g, u_char b, u_char a) {
1639 sceGsTest test;
1640 sceGsZbuf zbuf;
1641 sceGsAlpha alpha;
1642 int x;
1643
1644 sceVif1PkCnt(Vif1Packet, 0);
1645 sceVif1PkOpenDirectCode(Vif1Packet, 0);
1646 sceVif1PkOpenGifTag(Vif1Packet, *(u_long128 *) &GiftagAD);
1647 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEXFLUSH, 0);
1648 sceVif1PkCloseGifTag(Vif1Packet);
1649 sceVif1PkCloseDirectCode(Vif1Packet);
1650
1651 sceVif1PkCnt(Vif1Packet, 0);
1652 sceVif1PkOpenDirectCode(Vif1Packet, 0);
1653 sceVif1PkOpenGifTag(Vif1Packet, *(u_long128 *) &GiftagAD);
1654
1655 test = mgPixelTest;
1656 test.bits.ate = 0;
1657 test.bits.zte = 1;
1658 test.bits.ztst = SCE_GS_ALWAYS;
1659 test.bits.date = 0;
1660 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEST_1, *(u_long *) &test);
1661
1662 zbuf = mgZBuffer;
1663 zbuf.bits.zmsk = 0;
1664 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_ZBUF_1, *(u_long *) &zbuf);
1665
1666 alpha = mgAlpha;
1667 alpha.bits.a = 2;
1668 alpha.bits.b = 2;
1669 alpha.bits.c = 2;
1670 alpha.bits.d = 0;
1671 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_ALPHA_1, *(u_long *) &alpha);
1672
1673 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEX1_1, 1);
1674 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_PRIM, SCE_GS_SET_PRIM(SCE_GS_PRIM_SPRITE, 0, 0, 0, 1, 0, 1, 0, 0));
1675 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_RGBAQ, SCE_GS_SET_RGBAQ(r, g, b, a, 0));
1676
1677 for (x = 0; x < 640 * 16; x += 16 * 16) {
1678 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(27648 + x, GS_Y_OFFSET, 0, 0));
1679 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(27648 + x + 16 * 16, GS_Y_OFFSET + SCREEN_HALF_HEIGHT * 16, 0, 0));
1680 }
1681
1682 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEXFLUSH, 0);
1683 sceVif1PkCloseGifTag(Vif1Packet);
1684 sceVif1PkCloseDirectCode(Vif1Packet);
1685
1686 sceVif1PkCnt(Vif1Packet, 0);
1687 sceVif1PkOpenDirectCode(Vif1Packet, 0);
1688 sceVif1PkOpenGifTag(Vif1Packet, *(u_long128 *) &GiftagAD);
1689 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEST_1, *(u_long *) &mgPixelTest);
1690 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_ZBUF_1, *(u_long *) &mgZBuffer);
1691 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_ALPHA_1, *(u_long *) &mgAlpha);
1692 sceVif1PkCloseGifTag(Vif1Packet);
1693 sceVif1PkCloseDirectCode(Vif1Packet);
1694}
1695
1696/* Draws a model's shadow with the fast shadow microprogram. */
1697void MGDrawShadowFast(CFrame *frame, float *position, float *normal) {
1698 sceGsZbuf zbuf;
1699 sceGsTest test;
1700
1701 if (!frame) {
1702 return;
1703 }
1704
1705 sceVu0CopyVector(mgRenderInfo.shadow_point, position);
1706 sceVu0CopyVector(mgRenderInfo.shadow_normal, normal);
1707
1708 zbuf = mgZBuffer;
1709 test = mgPixelTest;
1710 zbuf.bits.zmsk = 1;
1711 MGSetGsZBUF(&zbuf);
1712
1713 test.bits.ate = 0;
1714 test.bits.date = 0;
1715 MGSetGsTEST(&test);
1716
1717 sceVif1PkCall(Vif1Packet, (u_long128 *) Vu_shadow, 0);
1718 mgRenderInfo.shadow_pass = 1;
1719 MGDraw(frame);
1720 mgRenderInfo.shadow_pass = 0;
1721 sceVif1PkCall(Vif1Packet, (u_long128 *) Vu_prog0f, 0);
1722}
1723
1724/* Draws a model's shadow with the second fast shadow microprogram. */
1725void MGDrawShadowFast2(CFrame *frame, float *position, float *normal) {
1726 sceGsZbuf zbuf;
1727 sceGsTest test;
1728
1729 if (!frame) {
1730 return;
1731 }
1732
1733 sceVu0CopyVector(mgRenderInfo.shadow_point, position);
1734 sceVu0CopyVector(mgRenderInfo.shadow_normal, normal);
1735
1736 zbuf = mgZBuffer;
1737 test = mgPixelTest;
1738 zbuf.bits.zmsk = 1;
1739 MGSetGsZBUF(&zbuf);
1740
1741 test.bits.ate = 0;
1742 test.bits.date = 0;
1743 MGSetGsTEST(&test);
1744
1745 sceVif1PkCall(Vif1Packet, (u_long128 *) Vu_shadow3, 0);
1746 mgRenderInfo.shadow_pass = 1;
1747 MGDraw(frame);
1748 mgRenderInfo.shadow_pass = 0;
1749 sceVif1PkCall(Vif1Packet, (u_long128 *) Vu_prog0f, 0);
1750}
1751
1752/* Draws a model's shadow while preserving the perspective and viewport matrices. */
1753void MGDrawShadow(CFrame *frame, float *position, float *normal) {
1754 sceVu0FMATRIX perspective;
1755 sceVu0FMATRIX viewport;
1756 sceGsZbuf zbuf;
1757 sceGsTest test;
1758
1759 if (!frame) {
1760 return;
1761 }
1762
1763 sceVu0CopyVector(mgRenderInfo.shadow_point, position);
1764 sceVu0CopyVector(mgRenderInfo.shadow_normal, normal);
1765
1766 zbuf = mgZBuffer;
1767 test = mgPixelTest;
1768 zbuf.bits.zmsk = 1;
1769 MGSetGsZBUF(&zbuf);
1770
1771 test.bits.ate = 0;
1772 test.bits.date = 0;
1773 MGSetGsTEST(&test);
1774
1775 sceVif1PkCall(Vif1Packet, (u_long128 *) Vu_shadow2, 0);
1776 mgRenderInfo.shadow_pass = 2;
1777 sceVu0CopyMatrix(perspective, mgRenderInfo.perspective);
1778 sceVu0CopyMatrix(viewport, mgRenderInfo.viewport);
1779 MGDraw(frame);
1780 sceVu0CopyMatrix(mgRenderInfo.perspective, perspective);
1781 sceVu0CopyMatrix(mgRenderInfo.viewport, viewport);
1782 mgRenderInfo.shadow_pass = 0;
1783 sceVif1PkCall(Vif1Packet, (u_long128 *) Vu_prog0f, 0);
1784}
1785
1786/* Draws the shade pass for one model. */
1787void MGDrawShade(CFrame *frame) {
1788 sceGsZbuf zbuf;
1789 sceGsTest test;
1790 sceGsAlpha alpha;
1791
1792 if (!frame) {
1793 return;
1794 }
1795
1796 zbuf = mgZBuffer;
1797 test = mgPixelTest;
1798 zbuf.bits.zmsk = 1;
1799 MGSetGsZBUF(&zbuf);
1800
1801 test.bits.ate = 0;
1802 test.bits.date = 0;
1803 MGSetGsTEST(&test);
1804
1805 alpha = mgAlpha;
1806 alpha.bits.a = 2;
1807 alpha.bits.b = 2;
1808 alpha.bits.c = 2;
1809 alpha.bits.d = 0;
1810 MGSetGsALPHA(&alpha);
1811
1812 mgRenderInfo.shadow_pass = 8;
1813 MGDraw(frame);
1814 mgRenderInfo.shadow_pass = 0;
1815}
1816
1817static sceGsTex0 Shadow_SaveFrameBuff;
1818static sceGsTex0 Shadow_WorkTex;
1819
1820void MGBeginDrawShadow(sceGsTex0 tex0) {
1821 sceVif1Packet *packet;
1822 sceGsTest test;
1823 sceGsZbuf zbuf;
1824
1825 test = mgPixelTest;
1826 test.bits.ate = 1;
1827 test.bits.aref = 0;
1828 test.bits.atst = SCE_GS_ALWAYS;
1829 test.bits.zte = 1;
1830 test.bits.ztst = SCE_GS_ALWAYS;
1831
1832 zbuf = mgZBuffer;
1833 zbuf.bits.zmsk = 1;
1834
1835 tex0.PSM = SCE_GS_PSMCT24;
1836
1837 packet = GetVif1Packet();
1838 MGGetFBuffTex(&Shadow_SaveFrameBuff);
1839
1840 sceVif1PkCnt(packet, 0);
1841 sceVif1PkOpenDirectCode(packet, 0);
1842 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
1843
1844 sceVif1PkAddGsAD(packet, SCE_GS_TEXFLUSH, 0);
1845 sceVif1PkAddGsAD(packet, SCE_GS_FRAME_1, SCE_GS_SET_FRAME(tex0.TBP0 >> 5, tex0.TBW, tex0.PSM, 0));
1846 sceVif1PkAddGsAD(packet, SCE_GS_ALPHA_1, *(u_long *) &mgAlpha);
1847 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEST_1, *(u_long *) &test);
1848 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_ZBUF_1, *(u_long *) &zbuf);
1849 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEX1_1, 1);
1850 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_PRIM, SCE_GS_SET_PRIM(SCE_GS_PRIM_SPRITE, 0, 0, 0, 1, 0, 1, 0, 0));
1851 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_RGBAQ, SCE_GS_SET_RGBAQ(0, 0, 0, 128, 0));
1852 sceVif1PkAddGsAD(packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(27648, GS_Y_OFFSET, 0, 0));
1853 sceVif1PkAddGsAD(packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(27648 + 640 * 16 - 1, GS_Y_OFFSET + SCREEN_HALF_HEIGHT * 16 - 1, 0, 0));
1854 sceVif1PkAddGsAD(packet, SCE_GS_TEXFLUSH, 0);
1855
1856 sceVif1PkCloseGifTag(packet);
1857 sceVif1PkCloseDirectCode(packet);
1858
1859 Shadow_WorkTex = tex0;
1860}
1861
1862void MGEndDrawShadow(u_char alpha) {
1863 sceVif1Packet *packet;
1864 sceGsAlpha blend;
1865 sceGsTest test;
1866 sceGsZbuf zbuf;
1867 sceGsTexa texa;
1868 float q;
1869
1870 packet = GetVif1Packet();
1871
1872 blend = mgAlpha;
1873 blend.bits.a = 2;
1874 blend.bits.b = 1;
1875 blend.bits.c = 0;
1876 blend.bits.d = 1;
1877 blend.bits.fix = 64;
1878
1879 test = mgPixelTest;
1880 test.bits.ate = 0;
1881 test.bits.date = 0;
1882 test.bits.datm = 1;
1883 test.bits.zte = 1;
1884 test.bits.ztst = SCE_GS_ALWAYS;
1885
1886 zbuf = mgZBuffer;
1887 zbuf.bits.zmsk = 1;
1888
1889 texa = mgTexa;
1890
1891 sceVif1PkCnt(packet, 0);
1892 sceVif1PkOpenDirectCode(packet, 0);
1893 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
1894
1895 sceVif1PkAddGsAD(packet, SCE_GS_TEXFLUSH, 0);
1896 sceVif1PkAddGsAD(packet, SCE_GS_FRAME_1, SCE_GS_SET_FRAME(Shadow_SaveFrameBuff.TBP0 >> 5, Shadow_SaveFrameBuff.TBW, Shadow_SaveFrameBuff.PSM, 0));
1897
1898 texa.AEM = 1;
1899 texa.TA0 = alpha;
1900 q = 1.0f;
1901
1902 sceVif1PkAddGsAD(packet, SCE_GS_TEXA, *(u_long *) &texa);
1903 sceVif1PkAddGsAD(packet, SCE_GS_ALPHA_1, *(u_long *) &blend);
1904 sceVif1PkAddGsAD(packet, SCE_GS_TEST_1, *(u_long *) &test);
1905 sceVif1PkAddGsAD(packet, SCE_GS_ZBUF_1, *(u_long *) &zbuf);
1906 sceVif1PkAddGsAD(packet, SCE_GS_TEX1_1, 1);
1907 sceVif1PkAddGsAD(packet, SCE_GS_PRIM, SCE_GS_SET_PRIM(SCE_GS_PRIM_SPRITE, 0, 1, 0, 1, 0, 1, 0, 0));
1908 sceVif1PkAddGsAD(packet, SCE_GS_RGBAQ, SCE_GS_SET_RGBAQ(128, 128, 128, 128, *(u_int *) &q));
1909 sceVif1PkAddGsAD(packet, SCE_GS_TEX0_1, *(u_long *) &Shadow_WorkTex);
1910 sceVif1PkAddGsAD(packet, SCE_GS_UV, SCE_GS_SET_UV(0, 0));
1911 sceVif1PkAddGsAD(packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(27648, GS_Y_OFFSET, 0, 0));
1912 sceVif1PkAddGsAD(packet, SCE_GS_UV, SCE_GS_SET_UV(640 * 16, SCREEN_HALF_HEIGHT * 16));
1913 sceVif1PkAddGsAD(packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(27648 + 640 * 16 - 1, GS_Y_OFFSET + SCREEN_HALF_HEIGHT * 16 - 1, 0, 0));
1914 sceVif1PkAddGsAD(packet, SCE_GS_TEXFLUSH, 0);
1915 sceVif1PkAddGsAD(packet, SCE_GS_ALPHA_1, *(u_long *) &mgAlpha);
1916 sceVif1PkAddGsAD(packet, SCE_GS_TEST_1, *(u_long *) &mgPixelTest);
1917 sceVif1PkAddGsAD(packet, SCE_GS_ZBUF_1, *(u_long *) &mgZBuffer);
1918
1919 sceVif1PkCloseGifTag(packet);
1920 sceVif1PkCloseDirectCode(packet);
1921}
sceVu0FVECTOR min
Definition collision.hpp:29
sceVu0FVECTOR max
Definition collision.hpp:28
s32 x
Definition rect.hpp:18
s32 width
Definition rect.hpp:20
s32 height
Definition rect.hpp:21
s32 y
Definition rect.hpp:19
s32 height
Definition mglib.cpp:66
s32 width
Definition mglib.cpp:65
s32 z
Definition mglib.hpp:27
s32 x
Definition mglib.hpp:25
s32 y
Definition mglib.hpp:26
sceVu0FMATRIX light_color
sceVu0FVECTOR scale
sceVu0FMATRIX light_direction
sceVu0FMATRIX perspective
sceVu0FVECTOR near
float fog_far
sceVu0FVECTOR far
sceVu0FMATRIX view
sceVu0FVECTOR clip_min
sceVu0FVECTOR view_position
u_char fog_blue
sceVu0FMATRIX view_screen
sceVu0FMATRIX screen
u_char fog_red
u_char fog_green
float fog_near
sceVu0FVECTOR shadow_point
sceVu0FVECTOR position
float projection
sceVu0FMATRIX viewport
sceVu0FMATRIX view_scaled
sceVu0FVECTOR clip_max
sceVu0FVECTOR ambient
sceVu0FVECTOR shadow_normal
sceVu0FVECTOR offset