This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
forked from Jay-Jay-OPL/OPL-Daily-Builds
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrenderman.c
490 lines (406 loc) · 14.5 KB
/
renderman.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
Copyright 2010, Volca
Licenced under Academic Free License version 3.0
Review OpenUsbLd README & LICENSE files for further details.
*/
#include <stdio.h>
#include <kernel.h>
#include "include/renderman.h"
#include "include/ioman.h"
#include "include/opl.h"
// Allocateable space in vram, as indicated in GsKit's code
#define __VRAM_SIZE 4194304
GSGLOBAL *gsGlobal;
s32 guiThreadID;
static int order;
static short int vmode = -1;
static u8 hires = 0;
static u8 guiWakeupCount;
static int vsync_id = -1;
#define NUM_RM_VMODES 12
#define RM_VMODE_AUTO 0
// RM Vmode -> GS Vmode conversion table
struct rm_mode
{
char mode;
char hsync; // In KHz
short int width;
short int height;
short int passes;
short int VCK;
short int interlace;
short int field;
short int aratio;
short int PAR1; // Pixel Aspect Ratio 1 (For video modes with non-square pixels, like PAL/NTSC)
short int PAR2; // Pixel Aspect Ratio 2 (For video modes with non-square pixels, like PAL/NTSC)
};
// clang-format off
static struct rm_mode rm_mode_table[NUM_RM_VMODES] = {
// 24 bit color mode with black borders
{-1, 16, 640, -1, 1, 4, GS_INTERLACED, GS_FIELD, RM_ARATIO_4_3, -1, 15}, // AUTO
{GS_MODE_PAL, 16, 640, 512, 1, 4, GS_INTERLACED, GS_FIELD, RM_ARATIO_4_3, 16, 15}, // PAL@50Hz
{GS_MODE_NTSC, 16, 640, 448, 1, 4, GS_INTERLACED, GS_FIELD, RM_ARATIO_4_3, 14, 15}, // NTSC@60Hz
{GS_MODE_DTV_480P, 31, 640, 448, 1, 2, GS_NONINTERLACED, GS_FRAME, RM_ARATIO_4_3, 14, 15}, // DTV480P@60Hz
{GS_MODE_DTV_576P, 31, 640, 512, 1, 2, GS_NONINTERLACED, GS_FRAME, RM_ARATIO_4_3, 16, 15}, // DTV576P@50Hz
{GS_MODE_VGA_640_60, 31, 640, 480, 1, 2, GS_NONINTERLACED, GS_FRAME, RM_ARATIO_4_3, 1, 1}, // VGA640x480@60Hz
// 24 bit color mode full screen, multi-pass (2 passes, HIRES)
{GS_MODE_PAL, 16, 704, 576, 2, 4, GS_INTERLACED, GS_FIELD, RM_ARATIO_4_3, 12, 11}, // PAL@50Hz
{GS_MODE_NTSC, 16, 704, 480, 2, 4, GS_INTERLACED, GS_FIELD, RM_ARATIO_4_3, 10, 11}, // NTSC@60Hz
{GS_MODE_DTV_480P, 31, 704, 480, 2, 2, GS_NONINTERLACED, GS_FRAME, RM_ARATIO_4_3, 10, 11}, // DTV480P@60Hz
{GS_MODE_DTV_576P, 31, 704, 576, 2, 2, GS_NONINTERLACED, GS_FRAME, RM_ARATIO_4_3, 12, 11}, // DTV576P@50Hz
// 16 bit color mode full screen, multi-pass (3 passes, HIRES)
{GS_MODE_DTV_720P, 31, 1280, 720, 3, 1, GS_NONINTERLACED, GS_FRAME, RM_ARATIO_16_9, 1, 1}, // HDTV720P@60Hz
{GS_MODE_DTV_1080I, 31, 1920, 1080, 3, 1, GS_INTERLACED, GS_FRAME, RM_ARATIO_16_9, 1, 1}, // HDTV1080I@60Hz
};
// clang-format on
// Display Aspect Ratio
static int iAspectWidth = 4;
static enum rm_aratio DAR = RM_ARATIO_4_3;
// Display dimensions after overscan compensation
static int iDisplayWidth;
static int iDisplayHeight;
static int iDisplayXOff;
static int iDisplayYOff;
// Transposition values including overscan compensation
static float fRenderXOff = 0.0f;
static float fRenderYOff = 0.0f;
const u64 gColWhite = GS_SETREG_RGBA(0xFF, 0xFF, 0xFF, 0x80); // Alpha 0x80 -> solid white
const u64 gColBlack = GS_SETREG_RGBA(0x00, 0x00, 0x00, 0x80); // Alpha 0x80 -> solid black
const u64 gColDarker = GS_SETREG_RGBA(0x00, 0x00, 0x00, 0x60); // Alpha 0x60 -> transparent overlay color
const u64 gColFocus = GS_SETREG_RGBA(0xFF, 0xFF, 0xFF, 0x50); // Alpha 0x50 -> transparent overlay color
const u64 gDefaultCol = GS_SETREG_RGBA(0x80, 0x80, 0x80, 0x80); // Special color for texture multiplication
const u64 gDefaultAlpha = GS_SETREG_ALPHA(0, 1, 0, 1, 0);
void rmInvalidateTexture(GSTEXTURE *txt)
{
gsKit_TexManager_invalidate(gsGlobal, txt);
}
void rmUnloadTexture(GSTEXTURE *txt)
{
gsKit_TexManager_free(gsGlobal, txt);
}
void rmStartFrame(void)
{
if (hires == 0)
gsKit_clear(gsGlobal, gColBlack);
order = 0;
}
void rmEndFrame(void)
{
if (hires) {
gsKit_hires_sync(gsGlobal);
gsKit_hires_flip(gsGlobal);
} else {
gsKit_set_finish(gsGlobal);
gsKit_queue_exec(gsGlobal);
// Wait for draw ops to finish
gsKit_finish();
if (!gsGlobal->FirstFrame) {
SleepThread();
guiWakeupCount = 0;
if (gsGlobal->DoubleBuffering == GS_SETTING_ON) {
GS_SET_DISPFB2(gsGlobal->ScreenBuffer[gsGlobal->ActiveBuffer & 1] / 8192,
gsGlobal->Width / 64, gsGlobal->PSM, 0, 0);
gsGlobal->ActiveBuffer ^= 1;
}
}
gsKit_setactive(gsGlobal);
}
gsKit_TexManager_nextFrame(gsGlobal);
}
static int rmOnVSync(void)
{
if (guiWakeupCount == 0) {
guiWakeupCount = 1;
iWakeupThread(guiThreadID);
}
ExitHandler();
return 0;
}
void rmInit()
{
short int mode = gsKit_check_rom();
rm_mode_table[RM_VMODE_AUTO].mode = mode;
rm_mode_table[RM_VMODE_AUTO].height = (mode == GS_MODE_PAL) ? 512 : 448;
rm_mode_table[RM_VMODE_AUTO].PAR1 = (mode == GS_MODE_PAL) ? 16 : 14;
dmaKit_init(D_CTRL_RELE_OFF, D_CTRL_MFD_OFF, D_CTRL_STS_UNSPEC,
D_CTRL_STD_OFF, D_CTRL_RCYC_8, 1 << DMA_CHANNEL_GIF);
// Initialize the DMAC
dmaKit_chan_init(DMA_CHANNEL_GIF);
rmSetMode(1);
order = 0;
guiWakeupCount = 0;
guiThreadID = GetThreadId();
}
int rmSetMode(int force)
{
if (gVMode < RM_VMODE_AUTO || gVMode >= NUM_RM_VMODES)
gVMode = RM_VMODE_AUTO;
// we don't want to set the vmode without a reason...
int changed = (vmode != gVMode || force);
if (changed) {
// Cleanup previous gsKit instance
if (vmode >= 0)
rmEnd();
vmode = gVMode;
hires = (rm_mode_table[vmode].passes > 1) ? 1 : 0;
if (hires) {
gsGlobal = gsKit_hires_init_global();
} else {
gsGlobal = gsKit_init_global();
vsync_id = gsKit_add_vsync_handler(&rmOnVSync);
}
gsGlobal->Mode = rm_mode_table[vmode].mode;
gsGlobal->Width = rm_mode_table[vmode].width;
gsGlobal->Height = rm_mode_table[vmode].height;
gsGlobal->Interlace = rm_mode_table[vmode].interlace;
gsGlobal->Field = rm_mode_table[vmode].field;
gsGlobal->PSM = GS_PSM_CT24;
// Higher resolution use too much VRAM
// so automatically switch back to 16bit color depth
if ((gsGlobal->Width * gsGlobal->Height) > (704 * 576))
gsGlobal->PSM = GS_PSM_CT16S;
gsGlobal->PSMZ = GS_PSMZ_16S;
gsGlobal->ZBuffering = GS_SETTING_OFF;
gsGlobal->PrimAlphaEnable = GS_SETTING_ON;
gsGlobal->DoubleBuffering = GS_SETTING_ON;
gsGlobal->Dithering = GS_SETTING_ON;
// Do not draw pixels if they are fully transparent
// gsGlobal->Test->ATE = GS_SETTING_ON;
gsGlobal->Test->ATST = 7; // NOTEQUAL to AREF passes
gsGlobal->Test->AREF = 0x00;
gsGlobal->Test->AFAIL = 0; // KEEP
if ((gsGlobal->Interlace == GS_INTERLACED) && (gsGlobal->Field == GS_FRAME))
gsGlobal->Height /= 2;
// Coordinate space ranges from 0 to 4096 pixels
// Center the buffer in the coordinate space
gsGlobal->OffsetX = ((4096 - gsGlobal->Width) / 2) * 16;
gsGlobal->OffsetY = ((4096 - gsGlobal->Height) / 2) * 16;
if (hires) {
gsKit_hires_init_screen(gsGlobal, rm_mode_table[vmode].passes);
} else {
gsKit_init_screen(gsGlobal);
gsKit_mode_switch(gsGlobal, GS_ONESHOT);
}
gsKit_set_test(gsGlobal, GS_ZTEST_OFF);
gsKit_set_primalpha(gsGlobal, gDefaultAlpha, 0);
// reset the contents of the screen to avoid garbage being displayed
if (hires) {
gsKit_hires_sync(gsGlobal);
gsKit_hires_flip(gsGlobal);
} else {
gsKit_clear(gsGlobal, gColBlack);
gsKit_sync_flip(gsGlobal);
}
LOG("RENDERMAN New vmode: %d, %d x %d\n", vmode, gsGlobal->Width, gsGlobal->Height);
}
rmSetDisplayOffset(gXOff, gYOff);
rmSetOverscan(gOverscan);
rmSetAspectRatio((gWideScreen == 0) ? RM_ARATIO_4_3 : RM_ARATIO_16_9);
return changed;
}
void rmGetScreenExtentsNative(int *w, int *h)
{
*w = iDisplayWidth;
*h = iDisplayHeight;
}
void rmGetScreenExtents(int *w, int *h)
{
// Emulate 640x480 (square pixel VGA)
*w = 640;
*h = 480;
}
void rmEnd(void)
{
if (hires) {
gsKit_hires_deinit_global(gsGlobal);
} else {
gsKit_deinit_global(gsGlobal);
gsKit_remove_vsync_handler(vsync_id);
}
vmode = -1;
}
#define X_SCALE(x) (((x)*iDisplayWidth) / 640)
#define Y_SCALE(y) (((y)*iDisplayHeight) / 480)
/** If txt is null, don't use DIM_UNDEF size */
static void rmSetupQuad(GSTEXTURE *txt, int x, int y, short aligned, int w, int h, short scaled, u64 color, rm_quad_t *q)
{
if (w == DIM_UNDEF)
w = txt->Width;
if (h == DIM_UNDEF)
h = txt->Height;
// Legacy scaling
x = X_SCALE(x);
y = Y_SCALE(y);
if (scaled & SCALING_RATIO)
w = X_SCALE(w * iAspectWidth) >> 2;
else
w = X_SCALE(w);
h = Y_SCALE(h);
// Align LEFT/HCENTER/RIGHT
if (aligned & ALIGN_HCENTER)
q->ul.x = x - (w >> 1);
else if (aligned & ALIGN_RIGHT)
q->ul.x = x - w;
else
q->ul.x = x;
q->br.x = q->ul.x + w;
// Align TOP/VCENTER/BOTTOM
if (aligned & ALIGN_VCENTER)
q->ul.y = y - (h >> 1);
else if (aligned & ALIGN_BOTTOM)
q->ul.y = y - h;
else
q->ul.y = y;
q->br.y = q->ul.y + h;
q->color = color;
if (txt) {
q->txt = txt;
q->ul.u = 0;
q->ul.v = 0;
q->br.u = txt->Width;
q->br.v = txt->Height;
}
}
void rmDrawQuad(rm_quad_t *q)
{
if ((q->txt->PSM == GS_PSM_CT32) || (q->txt->Clut && q->txt->ClutPSM == GS_PSM_CT32)) {
gsGlobal->PrimAlphaEnable = GS_SETTING_ON;
gsKit_set_test(gsGlobal, GS_ATEST_ON);
} else {
gsGlobal->PrimAlphaEnable = GS_SETTING_OFF;
gsKit_set_test(gsGlobal, GS_ATEST_OFF);
}
gsKit_TexManager_bind(gsGlobal, q->txt);
gsKit_prim_sprite_texture(gsGlobal, q->txt,
q->ul.x + fRenderXOff, q->ul.y + fRenderYOff,
q->ul.u, q->ul.v,
q->br.x + fRenderXOff, q->br.y + fRenderYOff,
q->br.u, q->br.v, order, q->color);
order++;
}
void rmDrawPixmap(GSTEXTURE *txt, int x, int y, short aligned, int w, int h, short scaled, u64 color)
{
rm_quad_t quad;
rmSetupQuad(txt, x, y, aligned, w, h, scaled, color, &quad);
rmDrawQuad(&quad);
}
void rmDrawOverlayPixmap(GSTEXTURE *overlay, int x, int y, short aligned, int w, int h, short scaled, u64 color,
GSTEXTURE *inlay, int ulx, int uly, int urx, int ury, int blx, int bly, int brx, int bry)
{
rm_quad_t quad;
rmSetupQuad(overlay, x, y, aligned, w, h, scaled, color, &quad);
ulx = X_SCALE(ulx * iAspectWidth) >> 2;
urx = X_SCALE(urx * iAspectWidth) >> 2;
blx = X_SCALE(blx * iAspectWidth) >> 2;
brx = X_SCALE(brx * iAspectWidth) >> 2;
uly = Y_SCALE(uly);
ury = Y_SCALE(ury);
bly = Y_SCALE(bly);
bry = Y_SCALE(bry);
if ((inlay->PSM == GS_PSM_CT32) || (inlay->Clut && inlay->ClutPSM == GS_PSM_CT32))
gsGlobal->PrimAlphaEnable = GS_SETTING_ON;
else
gsGlobal->PrimAlphaEnable = GS_SETTING_OFF;
gsKit_TexManager_bind(gsGlobal, inlay);
gsKit_prim_quad_texture(gsGlobal, inlay,
quad.ul.x + ulx + fRenderXOff, quad.ul.y + uly + fRenderYOff,
0.0f, 0.0f,
quad.ul.x + urx + fRenderXOff, quad.ul.y + ury + fRenderYOff,
inlay->Width, 0.0f,
quad.ul.x + blx + fRenderXOff, quad.ul.y + bly + fRenderYOff,
0.0f, inlay->Height,
quad.ul.x + brx + fRenderXOff, quad.ul.y + bry + fRenderYOff,
inlay->Width, inlay->Height, order, gDefaultCol);
order++;
rmDrawQuad(&quad);
}
void rmDrawRect(int x, int y, int w, int h, u64 color)
{
float fx = X_SCALE(x) + fRenderXOff;
float fy = Y_SCALE(y) + fRenderYOff;
float fw = X_SCALE(w);
float fh = Y_SCALE(h);
gsGlobal->PrimAlphaEnable = GS_SETTING_ON;
gsKit_prim_sprite(gsGlobal, fx, fy, fx + fw, fy + fh, order, color);
order++;
}
void rmDrawLine(int x1, int y1, int x2, int y2, u64 color)
{
float fx1 = X_SCALE(x1) + fRenderXOff;
float fy1 = Y_SCALE(y1) + fRenderYOff;
float fx2 = X_SCALE(x2) + fRenderXOff;
float fy2 = Y_SCALE(y2) + fRenderYOff;
gsGlobal->PrimAlphaEnable = GS_SETTING_ON;
gsKit_prim_line(gsGlobal, fx1, fy1, fx2, fy2, order, color);
order++;
}
void rmSetDisplayOffset(int x, int y)
{
gsKit_set_display_offset(gsGlobal, x * rm_mode_table[vmode].VCK, y);
}
void rmSetAspectRatio(enum rm_aratio dar)
{
DAR = dar;
switch (DAR) {
case RM_ARATIO_4_3:
iAspectWidth = 4; // width = width * 4 / 4
break;
case RM_ARATIO_16_9:
iAspectWidth = 3; // width = width * 3 / 4
break;
};
}
int rmWideScale(int x)
{
return (x * iAspectWidth) >> 2;
}
// Get the pixel aspect ratio (how wide or narrow are the pixels?)
float rmGetPAR()
{
float fPAR = (float)rm_mode_table[vmode].PAR2 / (float)rm_mode_table[vmode].PAR1;
// In anamorphic mode the pixels are stretched to 16:9
if ((DAR == RM_ARATIO_16_9) && (rm_mode_table[vmode].aratio == RM_ARATIO_4_3))
fPAR *= 0.75f;
// In interlaced frame mode, the pixel are (virtually) twice as high
if ((gsGlobal->Interlace == GS_INTERLACED) && (gsGlobal->Field == GS_FRAME))
fPAR *= 2.0f;
return fPAR;
}
// Get interfaced frame mode
int rmGetInterlacedFrameMode()
{
if ((gsGlobal->Interlace == GS_INTERLACED) && (gsGlobal->Field == GS_FRAME))
return 1;
return 0;
}
int rmScaleX(int x)
{
return X_SCALE(x);
}
int rmScaleY(int y)
{
return Y_SCALE(y);
}
int rmUnScaleX(int x)
{
return (x * 640) / iDisplayWidth;
}
int rmUnScaleY(int y)
{
return (y * 480) / iDisplayHeight;
}
void rmSetOverscan(int overscan)
{
iDisplayXOff = (gsGlobal->Width * overscan) / (2 * 1000);
iDisplayYOff = (gsGlobal->Height * overscan) / (2 * 1000);
iDisplayWidth = gsGlobal->Width - (2 * iDisplayXOff);
iDisplayHeight = gsGlobal->Height - (2 * iDisplayYOff);
fRenderXOff = (float)iDisplayXOff - 0.5f;
fRenderYOff = (float)iDisplayYOff - 0.5f;
if (rmGetInterlacedFrameMode() == 1)
fRenderYOff += 0.25f;
}
unsigned char rmGetHsync(void)
{
return rm_mode_table[vmode].hsync;
}