-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathgsCore.c
607 lines (486 loc) · 13.8 KB
/
gsCore.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// ____ ___ | / _____ _____
// | __ | |___/ | |
// |___| ___| | \ __|__ | gsKit Open Source Project.
// ----------------------------------------------------------------------
// Copyright 2004 - Chris "Neovanglist" Gilbert <[email protected]>
// Licenced under Academic Free License version 2.0
// Review gsKit README & LICENSE files for further details.
//
// gsCore.c - Core functionality implimentations. (Allocators, RenderStates, etc)
//
// Parts taken from emoon's BreakPoint Demo Library
//
#include "gsKit.h"
#include "gsInline.h"
#include <kernel.h>
#include <malloc.h>
u32 gsKit_vram_alloc(GSGLOBAL *gsGlobal, u32 size, u8 type)
{
u32 CurrentPointer = gsGlobal->CurrentPointer;
#ifdef GSKIT_DEBUG
printf("CurrentPointer Before:\t0x%08X\n", gsGlobal->CurrentPointer);
#endif
if(type == GSKIT_ALLOC_SYSBUFFER)
size = (-GS_VRAM_BLOCKSIZE_8K)&(size+GS_VRAM_BLOCKSIZE_8K-1);
else
size = (-GS_VRAM_BLOCKSIZE_256)&(size+GS_VRAM_BLOCKSIZE_256-1);
if((gsGlobal->CurrentPointer + size)>= 4194304)
{
printf("ERROR: Not enough VRAM for this allocation!\n");
return GSKIT_ALLOC_ERROR;
}
else
{
gsGlobal->CurrentPointer += size;
// Re-initialize the texture manager
// NOTE: this is here for compatibility, it's better not to use
// gsKit_vram_alloc, and use gsKit_TexManager_bind instead.
gsKit_TexManager_init(gsGlobal);
#ifdef GSKIT_DEBUG
printf("CurrentPointer After:\t0x%08X\n", gsGlobal->CurrentPointer);
#endif
return CurrentPointer;
}
}
void gsKit_vram_clear(GSGLOBAL *gsGlobal)
{
gsGlobal->CurrentPointer = gsGlobal->TexturePointer;
// Re-initialize the texture manager
// NOTE: this is here for compatibility, it's better not to use
// gsKit_vram_alloc, and use gsKit_TexManager_bind instead.
gsKit_TexManager_init(gsGlobal);
}
void gsKit_sync_flip(GSGLOBAL *gsGlobal)
{
if(!gsGlobal->FirstFrame)
{
gsKit_vsync_wait();
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);
}
void gsKit_setactive(GSGLOBAL *gsGlobal)
{
u64 *p_data;
u64 *p_store;
p_data = p_store = (u64 *)gsGlobal->dma_misc;
*p_data++ = GIF_TAG( 4, 1, 0, 0, 0, 1 );
*p_data++ = GIF_AD;
// Context 1
*p_data++ = GS_SETREG_SCISSOR_1( 0, gsGlobal->Width - 1, 0, gsGlobal->Height - 1 );
*p_data++ = GS_SCISSOR_1;
*p_data++ = GS_SETREG_FRAME_1( gsGlobal->ScreenBuffer[gsGlobal->ActiveBuffer & 1] / 8192,
gsGlobal->Width / 64, gsGlobal->PSM, 0 );
*p_data++ = GS_FRAME_1;
// Context 2
*p_data++ = GS_SETREG_SCISSOR_1( 0, gsGlobal->Width - 1, 0, gsGlobal->Height - 1 );
*p_data++ = GS_SCISSOR_2;
*p_data++ = GS_SETREG_FRAME_1( gsGlobal->ScreenBuffer[gsGlobal->ActiveBuffer & 1] / 8192,
gsGlobal->Width / 64, gsGlobal->PSM, 0 );
*p_data++ = GS_FRAME_2;
dmaKit_wait_fast();
dmaKit_send_ucab(DMA_CHANNEL_GIF, p_store, 5);
}
void gsKit_finish(void)
{
while(!(GS_CSR_FINISH));
}
void gsKit_lock_buffer(GSGLOBAL *gsGlobal)
{
gsGlobal->LockBuffer = GS_SETTING_ON;
}
void gsKit_unlock_buffer(GSGLOBAL *gsGlobal)
{
gsGlobal->LockBuffer = GS_SETTING_OFF;
}
int gsKit_lock_status(GSGLOBAL *gsGlobal)
{
return gsGlobal->LockBuffer;
}
void gsKit_display_buffer(GSGLOBAL *gsGlobal)
{
GS_SET_DISPFB2( gsGlobal->ScreenBuffer[gsGlobal->ActiveBuffer & 1] / 8192,
gsGlobal->Width / 64, gsGlobal->PSM, 0, 0 );
}
void gsKit_switch_context(GSGLOBAL *gsGlobal)
{
gsGlobal->ActiveBuffer ^= 1;
gsGlobal->PrimContext ^= 1;
gsKit_setactive(gsGlobal);
}
void gsKit_vsync_wait(void)
{
*GS_CSR = *GS_CSR & 8;
while(!(*GS_CSR & 8));
}
void gsKit_vsync_nowait(void)
{
*GS_CSR = *GS_CSR & 8;
}
void gsKit_get_field(GSGLOBAL *gsGlobal)
{
gsGlobal->EvenOrOdd=((GSREG*)GS_CSR)->FIELD;
}
void gsKit_hsync_wait(void)
{
*GS_CSR = *GS_CSR & 4;
while(!(*GS_CSR & 4));
}
int gsKit_add_vsync_handler(int (*vsync_callback)())
{
int callback_id;
DIntr();
callback_id = AddIntcHandler(INTC_VBLANK_S, vsync_callback, 0);
EnableIntc(INTC_VBLANK_S);
// Unmask VSync interrupt
GsPutIMR(GsGetIMR() & ~0x0800);
EIntr();
return callback_id;
}
void gsKit_remove_vsync_handler(int callback_id)
{
DIntr();
// Mask VSync interrupt
GsPutIMR(GsGetIMR() | 0x0800);
DisableIntc(INTC_VBLANK_S);
RemoveIntcHandler(INTC_VBLANK_S, callback_id);
EIntr();
}
int gsKit_add_hsync_handler(int (*hsync_callback)())
{
int callback_id;
DIntr();
callback_id = AddIntcHandler(INTC_GS, hsync_callback, 0);
EnableIntc(INTC_GS);
// Unmask HSync interrupt
GsPutIMR(GsGetIMR() & ~0x0400);
EIntr();
return callback_id;
}
void gsKit_remove_hsync_handler(int callback_id)
{
DIntr();
// Mask HSync interrupt
GsPutIMR(GsGetIMR() | 0x0400);
DisableIntc(INTC_GS);
RemoveIntcHandler(INTC_GS, callback_id);
EIntr();
}
void gsKit_clear(GSGLOBAL *gsGlobal, u64 color)
{
u8 PrevZState = gsGlobal->Test->ZTST;
gsKit_set_test(gsGlobal, GS_ZTEST_OFF);
u8 strips = gsGlobal->Width / 64;
u8 remain = gsGlobal->Width % 64;
u32 pos = 0;
strips++;
while(strips-- > 0)
{
gsKit_prim_sprite(gsGlobal, pos, 0, pos + 64, gsGlobal->Height, 0, color);
pos += 64;
}
if(remain > 0)
{
gsKit_prim_sprite(gsGlobal, pos, 0, remain + pos, gsGlobal->Height, 0, color);
}
gsGlobal->Test->ZTST = PrevZState;
gsKit_set_test(gsGlobal, 0);
}
void gsKit_set_test(GSGLOBAL *gsGlobal, u8 Preset)
{
u64 *p_data;
u64 *p_store;
if(Preset == GS_ZTEST_OFF)
gsGlobal->Test->ZTST = 1;
else if (Preset == GS_ZTEST_ON)
gsGlobal->Test->ZTST = 2;
else if (Preset == GS_ATEST_OFF)
gsGlobal->Test->ATE = 0;
else if (Preset == GS_ATEST_ON)
gsGlobal->Test->ATE = 1;
else if (Preset == GS_D_ATEST_OFF)
gsGlobal->Test->DATE = 0;
else if (Preset == GS_D_ATEST_ON)
gsGlobal->Test->DATE = 1;
p_data = p_store = gsKit_heap_alloc(gsGlobal, 1, 16, GIF_AD);
*p_data++ = GIF_TAG_AD(1);
*p_data++ = GIF_AD;
*p_data++ = GS_SETREG_TEST( gsGlobal->Test->ATE, gsGlobal->Test->ATST,
gsGlobal->Test->AREF, gsGlobal->Test->AFAIL,
gsGlobal->Test->DATE, gsGlobal->Test->DATM,
gsGlobal->Test->ZTE, gsGlobal->Test->ZTST );
*p_data++ = GS_TEST_1+gsGlobal->PrimContext;
}
void gsKit_set_clamp(GSGLOBAL *gsGlobal, u8 Preset)
{
u64 *p_data;
u64 *p_store;
if(Preset == GS_CMODE_REPEAT)
{
gsGlobal->Clamp->WMS = GS_CMODE_REPEAT;
gsGlobal->Clamp->WMT = GS_CMODE_REPEAT;
}
else if(Preset == GS_CMODE_CLAMP)
{
gsGlobal->Clamp->WMS = GS_CMODE_CLAMP;
gsGlobal->Clamp->WMT = GS_CMODE_CLAMP;
}
else if(Preset == GS_CMODE_REGION_CLAMP)
{
gsGlobal->Clamp->WMS = GS_CMODE_REGION_CLAMP;
gsGlobal->Clamp->WMT = GS_CMODE_REGION_CLAMP;
}
else if(Preset == GS_CMODE_REGION_REPEAT)
{
gsGlobal->Clamp->WMS = GS_CMODE_REGION_REPEAT;
gsGlobal->Clamp->WMT = GS_CMODE_REGION_REPEAT;
}
p_data = p_store = gsKit_heap_alloc(gsGlobal, 1, 16, GIF_AD);
*p_data++ = GIF_TAG_AD(1);
*p_data++ = GIF_AD;
*p_data++ = GS_SETREG_CLAMP(gsGlobal->Clamp->WMS, gsGlobal->Clamp->WMT,
gsGlobal->Clamp->MINU, gsGlobal->Clamp->MAXU,
gsGlobal->Clamp->MINV, gsGlobal->Clamp->MAXV);
*p_data++ = GS_CLAMP_1+gsGlobal->PrimContext;
}
void gsKit_set_primalpha(GSGLOBAL *gsGlobal, u64 AlphaMode, u8 PerPixel)
{
u64 *p_data;
u64 *p_store;
gsGlobal->PrimAlpha = AlphaMode;
gsGlobal->PABE = PerPixel;
p_data = p_store = gsKit_heap_alloc(gsGlobal, 2, 32, GIF_AD);
*p_data++ = GIF_TAG_AD(2);
*p_data++ = GIF_AD;
*p_data++ = gsGlobal->PABE;
*p_data++ = GS_PABE;
*p_data++ = gsGlobal->PrimAlpha;
*p_data++ = GS_ALPHA_1+gsGlobal->PrimContext;
}
void gsKit_set_texfilter(GSGLOBAL *gsGlobal, u8 FilterMode)
{
u64 *p_data;
u64 *p_store;
p_data = p_store = gsKit_heap_alloc(gsGlobal, 1, 16, GIF_AD);
*p_data++ = GIF_TAG_AD(1);
*p_data++ = GIF_AD;
*p_data++ = GS_SETREG_TEX1(0, 0, FilterMode, FilterMode, 0, 0, 0);
*p_data++ = GS_TEX1_1+gsGlobal->PrimContext;
}
void gsKit_set_dither_matrix(GSGLOBAL *gsGlobal)
{
u64 *p_data;
u64 *p_store;
p_data = p_store = gsKit_heap_alloc(gsGlobal, 1 ,16, GIF_AD);
*p_data++ = GIF_TAG_AD(1);
*p_data++ = GIF_AD;
*p_data++ = GS_SETREG_DIMX( gsGlobal->DitherMatrix[0], gsGlobal->DitherMatrix[1], gsGlobal->DitherMatrix[2], gsGlobal->DitherMatrix[3],
gsGlobal->DitherMatrix[4], gsGlobal->DitherMatrix[5], gsGlobal->DitherMatrix[6], gsGlobal->DitherMatrix[7],
gsGlobal->DitherMatrix[8], gsGlobal->DitherMatrix[9], gsGlobal->DitherMatrix[10], gsGlobal->DitherMatrix[11],
gsGlobal->DitherMatrix[12], gsGlobal->DitherMatrix[13], gsGlobal->DitherMatrix[14], gsGlobal->DitherMatrix[15]);
*p_data++ = GS_DIMX;
}
void gsKit_set_dither(GSGLOBAL *gsGlobal)
{
u64 *p_data;
u64 *p_store;
p_data = p_store = gsKit_heap_alloc(gsGlobal, 1, 16, GIF_AD);
*p_data++ = GIF_TAG_AD(1);
*p_data++ = GIF_AD;
if(gsGlobal->Dithering == GS_SETTING_ON)
*p_data++ = 1;
else
*p_data++ = 0;
*p_data++ = GS_DTHE;
}
// taken from libgraph
void gsKit_set_drawfield(GSGLOBAL *gsGlobal, u8 field)
{
u64 *p_data;
u64 *p_store;
p_data = p_store = gsKit_heap_alloc(gsGlobal, 1 ,16, GIF_AD);
*p_data++ = GIF_TAG_AD(1);
*p_data++ = GIF_AD;
if (field == GS_FIELD_NORMAL) { //Draw both
*p_data++ = 0;;
}
else if (field == GS_FIELD_ODD) { // Draw only odd
*p_data++ = 2;
}
else if (field == GS_FIELD_EVEN) { // Draw only even
*p_data++ = 3;
}
*p_data++ = GS_SCANMSK;
}
/*
// taken from libgraph
void gsKit_set_drawfield(GSGLOBAL *gsGlobal, u8 field)
{
u64 *p_data;
u64 *p_store;
(u32)p_data = (u32)p_store = gsGlobal->dma_misc;
*p_data++ = GIF_TAG( 1, 1, 0, 0, 0, 1);
*p_data++ = GIF_AD;
if (field == GS_FIELD_NORMAL) { //Draw both
*p_data++ = GIF_TAG_SCANMSK(0);
}
else if (field == GS_FIELD_ODD) { // Draw only odd
*p_data++ = GIF_TAG_SCANMSK(2);
}
else if (field == GS_FIELD_EVEN) { // Draw only even
*p_data++ = GIF_TAG_SCANMSK(3);
}
*p_data++ = GS_SCANMSK;
dmaKit_wait_fast();
dmaKit_send_ucab(DMA_CHANNEL_GIF, p_store, 2);
}
*/
GSQUEUE gsKit_set_finish(GSGLOBAL *gsGlobal)
{
u64 *p_data;
u64 *p_store;
GSQUEUE oldQueue = *gsGlobal->CurQueue;
p_data = p_store = gsKit_heap_alloc(gsGlobal, 1, 16, GIF_AD);
*p_data++ = GIF_TAG_AD(1);
*p_data++ = GIF_AD;
*p_data++ = 0;
*p_data++ = GS_FINISH;
return oldQueue;
}
void gsKit_queue_exec_real(GSGLOBAL *gsGlobal, GSQUEUE *Queue)
{
if(Queue->tag_size == 0)
return;
// This superstrange oldQueue crap is because Persistent drawbuffers need to be "backed up"
// or else they will balloon in size due to appending the finish token.
// So we back up the current *state* (NOT DATA) of them here and restore it afterward.
GSQUEUE oldQueue = gsKit_set_finish(gsGlobal);
*(u64 *)Queue->dma_tag = DMA_TAG(Queue->tag_size, 0, DMA_END, 0, 0, 0);
if(Queue->last_type != GIF_AD)
{
*(u64 *)Queue->last_tag = ((u64)Queue->same_obj | *(u64 *)Queue->last_tag);
}
if(!gsGlobal->FirstFrame)
gsKit_finish();
GS_SETREG_CSR_FINISH(1);
dmaKit_wait_fast();
dmaKit_send_chain_ucab(DMA_CHANNEL_GIF, Queue->pool[Queue->dbuf]);
if(Queue->mode != GS_PERSISTENT)
{
Queue->dbuf ^= 1;
Queue->dma_tag = Queue->pool[Queue->dbuf];
Queue->pool_cur = Queue->dma_tag + 16;
Queue->last_type = GIF_RESERVED;
Queue->last_tag = Queue->pool_cur;
Queue->tag_size = 0;
}
else
{
*Queue = oldQueue;
dmaKit_wait_fast();
}
}
void gsKit_queue_reset(GSQUEUE *Queue)
{
if(Queue->mode == GS_ONESHOT)
{
Queue->dbuf ^= 1;
}
Queue->dma_tag = Queue->pool[Queue->dbuf];
Queue->pool_cur = Queue->dma_tag + 16;
Queue->last_type = GIF_RESERVED;
Queue->last_tag = Queue->pool_cur;
Queue->tag_size = 0;
}
void gsKit_queue_exec(GSGLOBAL *gsGlobal)
{
GSQUEUE *CurQueue = gsGlobal->CurQueue;
if(gsGlobal->DrawOrder == GS_PER_OS)
{
gsGlobal->CurQueue = gsGlobal->Per_Queue;
gsKit_queue_exec_real(gsGlobal, gsGlobal->Per_Queue);
gsGlobal->CurQueue = gsGlobal->Os_Queue;
gsKit_queue_exec_real(gsGlobal, gsGlobal->Os_Queue);
}
else
{
gsGlobal->CurQueue = gsGlobal->Os_Queue;
gsKit_queue_exec_real(gsGlobal, gsGlobal->Os_Queue);
gsGlobal->CurQueue = gsGlobal->Per_Queue;
gsKit_queue_exec_real(gsGlobal, gsGlobal->Per_Queue);
}
gsGlobal->CurQueue = CurQueue;
gsGlobal->FirstFrame = GS_SETTING_OFF;
}
void *gsKit_alloc_ucab(int size)
{
// Allocate aligned memory
u8 *p = memalign(64, size);
// Flush the data cache
SyncDCache(p, p+size-1);
// Convert normal to UCAB memory
p = (u8*)((u32)p | 0x30000000);
// Return memory to UCAB area
return p;
}
void gsKit_free_ucab(void *p)
{
// Convert UCAB to normal memory
p = (u64 *)((u32)p & ~0x30000000);
// Free normal memory
free(p);
}
void gsKit_queue_init(GSGLOBAL *gsGlobal, GSQUEUE *Queue, u8 mode, int size)
{
// Init pool 0
Queue->pool[0] = gsKit_alloc_ucab(size);
Queue->pool_max[0] = (u64 *)((u32)Queue->pool[0] + size);
if (mode == GS_ONESHOT)
{
// Init pool 1
Queue->pool[1] = gsKit_alloc_ucab(size);
Queue->pool_max[1] = (u64 *)((u32)Queue->pool[1] + size);
}
Queue->dma_tag = Queue->pool[0];
Queue->pool_cur = (u64 *)((u32)Queue->pool[0] + 16);
Queue->dbuf = 0;
Queue->tag_size = 0;
Queue->last_tag = Queue->pool_cur;
Queue->last_type = GIF_RESERVED;
Queue->mode = mode;
}
void gsKit_queue_free(GSGLOBAL *gsGlobal, GSQUEUE *Queue)
{
if (Queue == NULL)
return;
if (Queue->pool[0] != NULL)
{
gsKit_free_ucab(Queue->pool[0]);
Queue->pool[0] = NULL;
}
if (Queue->pool[1] != NULL)
{
gsKit_free_ucab(Queue->pool[1]);
Queue->pool[1] = NULL;
}
}
void gsKit_queue_set(GSGLOBAL *gsGlobal, GSQUEUE *Queue)
{
gsGlobal->CurQueue = Queue;
}
void gsKit_mode_switch(GSGLOBAL *gsGlobal, u8 mode)
{
if(mode == GS_PERSISTENT)
{
gsKit_queue_set(gsGlobal, gsGlobal->Per_Queue);
}
else
{
gsKit_queue_set(gsGlobal, gsGlobal->Os_Queue);
}
}