-
Notifications
You must be signed in to change notification settings - Fork 2
/
xgraphics.c
444 lines (392 loc) · 11.3 KB
/
xgraphics.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
/**
* This file belongs to the 'xlab' game engine.
* Copyright 2009 xfacter
* Copyright 2016 wickles
* This work is licensed under the LGPLv3
* subject to all terms as reproduced in the included LICENSE file.
*/
#include <pspgu.h>
#include <pspgum.h>
#include <pspdisplay.h>
#include "xmem.h"
#include "xmath.h"
#include "xgraphics.h"
#if X_SCREEN_PSM == GU_PSM_5650 || X_SCREEN_PSM == GU_PSM_5551 || X_SCREEN_PSM == GU_PSM_4444
#define X_PIXEL_BYTES 2
#elif X_SCREEN_PSM == GU_PSM_8888
#define X_PIXEL_BYTES 4
#else
#error X_SCREEN_PSM must be equal to GU_PSM_5650, GU_PSM_5551, GU_PSM_4444, or GU_PSM_8888.
#endif
#define X_WHICH_MATRIX (x_states & X_PSEUDO_AA ? x_which_buf : 0)
#define X_WHICH_DITHER (x_states & X_DITHER_SMOOTH ? x_which_buf : 0)
static u32 __attribute__((aligned(16))) x_dlist0[X_LIST_KB*1024/sizeof(u32)];
#ifdef X_DLIST_DOUBLE
static u32 __attribute__((aligned(16))) x_dlist1[X_LIST_KB*1024/sizeof(u32)];
static int x_which_list = 0;
static u32 __attribute__((aligned(16))) x_call_list[32];
#endif
static void* x_draw_buf[2] = {0, 0};
static int x_which_buf = 0;
static void* x_depth_buf = 0;
static int x_states = 0;
static int x_inortho = 0;
static ScePspFMatrix4 x_perspect_mtx[2];
static ScePspFMatrix4 x_ortho_mtx[2];
static int x_saved_states;
static int x_dither_matrix[2][16] =
{
{ 0, 8, 0, 8,
8, 0, 8, 0,
0, 8, 0, 8,
8, 0, 8, 0 },
{ 8, 8, 8, 8,
0, 8, 0, 8,
8, 8, 8, 8,
0, 8, 0, 8 }
};
static int x_initialized = 0;
void xGuInit()
{
if (x_initialized) return;
x_initialized = 1;
#ifdef X_DLIST_DOUBLE
x_which_list = 0;
#endif
sceGuInit();
#ifndef X_DLIST_DOUBLE
sceGuStart(GU_DIRECT, x_dlist0);
#else
sceGuStart(GU_CALL, (x_which_list == 0 ? x_dlist0 : x_dlist1));
#endif
x_which_buf = 0;
x_draw_buf[0] = X_VREL(x_valloc(X_PIXEL_BYTES*X_SCREEN_STRIDE*X_SCREEN_HEIGHT));
x_draw_buf[1] = X_VREL(x_valloc(X_PIXEL_BYTES*X_SCREEN_STRIDE*X_SCREEN_HEIGHT));
sceGuDrawBuffer(X_SCREEN_PSM, x_draw_buf[0], X_SCREEN_STRIDE);
sceGuDispBuffer(X_SCREEN_WIDTH, X_SCREEN_HEIGHT, x_draw_buf[1], X_SCREEN_STRIDE);
sceGuOffset(2048 - (X_SCREEN_WIDTH/2), 2048 - (X_SCREEN_HEIGHT/2));
sceGuViewport(2048, 2048, X_SCREEN_WIDTH, X_SCREEN_HEIGHT);
sceGuScissor(0, 0, X_SCREEN_WIDTH, X_SCREEN_HEIGHT);
sceGuEnable(GU_SCISSOR_TEST);
#ifdef X_GRAPHICS_3D
x_depth_buf = X_VREL(x_valloc(2*X_SCREEN_STRIDE*X_SCREEN_HEIGHT));
sceGuDepthBuffer(x_depth_buf, X_SCREEN_STRIDE);
sceGuDepthRange(65535, 0);
sceGuDepthFunc(GU_GEQUAL);
sceGuClearDepth(0);
sceGuEnable(GU_DEPTH_TEST);
sceGuDepthMask(GU_FALSE);
sceGuEnable(GU_CLIP_PLANES);
sceGuEnable(GU_CULL_FACE);
sceGuShadeModel(GU_SMOOTH);
#else
sceGuDisable(GU_DEPTH_TEST);
sceGuDepthMask(GU_TRUE);
sceGuDisable(GU_CLIP_PLANES);
sceGuDisable(GU_CULL_FACE);
sceGuthadeModel(GU_FLAT);
sceGuEnable(GU_BLEND);
sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
#endif
sceGuTexWrap(GU_CLAMP, GU_CLAMP);
sceGuTexFilter(GU_NEAREST,GU_NEAREST);
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
sceGuTexEnvColor(0xFFFFFFFF);
sceGuColor(0xFFFFFFFF);
sceGuAmbientColor(0xFFFFFFFF);
sceGuTexOffset(0.0f, 0.0f);
sceGuTexScale(1.0f, 1.0f);
gumLoadIdentity(&x_ortho_mtx[0]);
gumOrtho(&x_ortho_mtx[0], 0.0f, 480.0f, 272.0f, 0.0f, -1000.0f, 1000.0f);
gumLoadIdentity(&x_ortho_mtx[1]);
ScePspFVector3 displace = {-1.0f/X_SCREEN_WIDTH, 1.0f/X_SCREEN_HEIGHT, 0.0f};
gumTranslate(&x_ortho_mtx[1], &displace);
gumMultMatrix(&x_ortho_mtx[1], &x_ortho_mtx[1], &x_ortho_mtx[0]);
xGuPerspective(75.0f, 0.5f, 1000.0f);
#ifndef X_GRAPHICS_3D
xGuSetOrtho();
#endif
sceGuDisplay(GU_TRUE);
}
void xGuEnd()
{
if (!x_initialized) return;
x_free(X_VABS(x_draw_buf[0]));
x_draw_buf[0] = 0;
x_free(X_VABS(x_draw_buf[1]));
x_draw_buf[1] = 0;
if (x_depth_buf)
{
x_free(X_VABS(x_depth_buf));
x_depth_buf = 0;
}
sceGuTerm();
}
int xGuFrameEnd()
{
/* end current frame */
int size = sceGuFinish();
sceGuSync(0, 0);
if (x_states & X_WAIT_VBLANK || x_states & X_PSEUDO_AA)
{
sceDisplayWaitVblankStart();
}
x_which_buf = (sceGuSwapBuffers() == x_draw_buf[0] ? 0 : 1);
#ifdef X_DLIST_DOUBLE
sceGuStart(GU_DIRECT, x_call_list);
sceGuCallList((x_which_list == 0 ? x_dlist0 : x_dlist1));
sceGuFinish();
x_which_list ^= 1;
#endif
if (x_states & X_PSEUDO_AA)
{
sceGumMatrixMode(GU_PROJECTION);
if (!x_inortho) sceGumLoadMatrix(&x_perspect_mtx[X_WHICH_MATRIX]);
else sceGumLoadMatrix(&x_ortho_mtx[X_WHICH_MATRIX]);
sceGumMatrixMode(GU_MODEL);
x_inortho = 0;
}
if (x_states & X_DITHER_SMOOTH)
{
sceGuSetDither((ScePspIMatrix4*)(&x_dither_matrix[X_WHICH_DITHER]));
}
/* begin next frame */
#ifndef X_DLIST_DOUBLE
sceGuStart(GU_DIRECT, x_dlist0);
#else
sceGuStart(GU_CALL, (x_which_list == 0 ? x_dlist0 : x_dlist1));
#endif
#ifdef X_GRAPHICS_3D
sceGuClear(GU_DEPTH_BUFFER_BIT);
#endif
sceGumMatrixMode(GU_VIEW);
sceGumLoadIdentity();
sceGumMatrixMode(GU_MODEL);
sceGumLoadIdentity();
return size;
}
void xGuClear(u32 color)
{
sceGuClearColor(color);
sceGuClear(GU_COLOR_BUFFER_BIT|GU_FAST_CLEAR_BIT);
}
void xGuPerspective(float fovy, float near, float far)
{
gumLoadIdentity(&x_perspect_mtx[0]);
gumPerspective(&x_perspect_mtx[0], fovy, 16.0f/9.0f, near, far);
gumLoadIdentity(&x_perspect_mtx[1]);
ScePspFVector3 displace = {-1.0f/X_SCREEN_WIDTH, 1.0f/X_SCREEN_HEIGHT, 0.0f};
gumTranslate(&x_perspect_mtx[1], &displace);
gumMultMatrix(&x_perspect_mtx[1], &x_perspect_mtx[1], &x_perspect_mtx[0]);
xGuSetPerspective();
}
int xGuSetPerspective()
{
//if (!x_inortho) return 0;
sceGumMatrixMode(GU_PROJECTION);
sceGumLoadMatrix(&x_perspect_mtx[X_WHICH_MATRIX]);
sceGumMatrixMode(GU_VIEW);
sceGumLoadIdentity();
sceGumMatrixMode(GU_MODEL);
sceGumLoadIdentity();
sceGuFrontFace(GU_CW);
x_inortho = 0;
return 1;
}
int xGuSetOrtho()
{
//if (x_inortho) return 0;
sceGumMatrixMode(GU_PROJECTION);
sceGumLoadMatrix(&x_ortho_mtx[X_WHICH_MATRIX]);
sceGumMatrixMode(GU_VIEW);
sceGumLoadIdentity();
sceGumMatrixMode(GU_MODEL);
sceGumLoadIdentity();
sceGuFrontFace(GU_CCW);
x_inortho = 1;
return 1;
}
void xGuEnable(int states)
{
if (states & ~0x1ffffff)
{
x_states |= states & ~0x1ffffff;
if (states & X_DITHER_SMOOTH) states |= X_DITHER;
}
states &= 0x1ffffff;
if (states) sceGuSetAllStatus(sceGuGetAllStatus() | states);
}
void xGuDisable(int states)
{
if (states & ~0x1ffffff)
{
x_states &= ~(states & ~0x1ffffff);
if (states & X_DITHER_SMOOTH) states |= X_DITHER;
}
states &= 0x1ffffff;
if (states) sceGuSetAllStatus(sceGuGetAllStatus() & ~states);
}
void xGuRenderToTarget(int psm, int width, int height, int tbw, void* tbp)
{
sceGuDrawBufferList(psm, tbp, tbw);
sceGuOffset(2048 - (width/2), 2048 - (height/2));
sceGuViewport(2048, 2048, width, height);
sceGuScissor(0, 0, width, height);
}
void xGuRenderToScreen()
{
sceGuDrawBufferList(X_SCREEN_PSM, x_draw_buf[x_which_buf], X_SCREEN_STRIDE);
sceGuOffset(2048 - (X_SCREEN_WIDTH/2), 2048 - (X_SCREEN_HEIGHT/2));
sceGuViewport(2048, 2048, X_SCREEN_WIDTH, X_SCREEN_HEIGHT);
sceGuScissor(0, 0, X_SCREEN_WIDTH, X_SCREEN_HEIGHT);
}
void xGuTexFilter(int filter)
{
switch (filter)
{
case X_BILINEAR:
sceGuTexFilter(GU_LINEAR, GU_LINEAR);
break;
case X_TRILINEAR:
sceGuTexFilter(GU_LINEAR_MIPMAP_LINEAR, GU_LINEAR_MIPMAP_LINEAR);
break;
default:
sceGuTexFilter(GU_NEAREST, GU_NEAREST);
break;
}
}
inline void xGuTexMode(int tfx, int alpha)
{
sceGuTexFunc(tfx, (alpha ? GU_TCC_RGBA : GU_TCC_RGB));
}
inline void xGumLoadIdentity()
{
sceGumLoadIdentity();
}
inline void xGumTranslate(float x, float y, float z)
{
ScePspFVector3 trans = {x,y,z};
sceGumTranslate(&trans);
}
inline void xGumRotateX(float angle)
{
sceGumRotateX(angle);
}
inline void xGumRotateY(float angle)
{
sceGumRotateY(angle);
}
inline void xGumRotateZ(float angle)
{
sceGumRotateZ(angle);
}
inline void xGumScale(float x, float y, float z)
{
ScePspFVector3 scale = {x,y,z};
sceGumScale(&scale);
}
inline void xGuSaveStates()
{
x_saved_states = x_states|sceGuGetAllStatus();
}
inline void xGuLoadStates()
{
sceGuSetAllStatus(x_saved_states & 0x1ffffff);
xGuEnable(x_saved_states & ~0x1ffffff);
}
inline void* xGuDrawPtr(int uncached, int abs)
{
u32 ptr = (u32)x_draw_buf[x_which_buf];
if (uncached) ptr |= X_MEM_NO_CACHE;
if (abs) ptr |= X_MEM_VRAM;
return (void*)ptr;
}
inline void* xGuDispPtr(int uncached, int abs)
{
u32 ptr = (u32)x_draw_buf[x_which_buf^1];
if (uncached) ptr |= X_MEM_NO_CACHE;
if (abs) ptr |= X_MEM_VRAM;
return (void*)ptr;
}
inline void* xGuDepthPtr(int uncached, int abs)
{
u32 ptr = (u32)x_depth_buf;
if (uncached) ptr |= X_MEM_NO_CACHE;
if (abs) ptr |= X_MEM_VRAM;
return (void*)ptr;
}
inline void* xGuStridePtr(int uncached, int abs)
{
u32 ptr = (u32)x_draw_buf[0] + X_SCREEN_WIDTH*X_PIXEL_BYTES;
if (uncached) ptr |= X_MEM_NO_CACHE;
if (abs) ptr |= X_MEM_VRAM;
return (void*)ptr;
}
#define TEX_DEBUG_PSM (GU_PSM_8888)
#define TEX_DEBUG_SCALE (8.0f)
#define TEX_DEBUG_WIDTH (2)
#define TEX_DEBUG_HEIGHT (2)
static u32 __attribute__((aligned(16))) debug_texture[4] = {
0xffff00ff, 0xff000000,
0xff000000, 0xffff00ff
};
void xGuSetDebugTex()
{
sceGuTexWrap(GU_REPEAT, GU_REPEAT);
sceGuTexMode(TEX_DEBUG_PSM, 0, 0, 0);
sceGuTexScale(TEX_DEBUG_SCALE, TEX_DEBUG_SCALE);
sceGuTexImage(0, TEX_DEBUG_WIDTH, TEX_DEBUG_HEIGHT, TEX_DEBUG_WIDTH, debug_texture);
}
#define TEX_SLICE (64)
void xGuDrawTex(int x, int y, int w, int h, int tx, int ty, int tw, int th)
{
float cur_u = (float)tx;
float ustep = (float)TEX_SLICE*tw/w;
int slice_width = 0;
float tex_step = 0;
int i;
for (i = x; i < x + w; i += TEX_SLICE)
{
slice_width = (i + TEX_SLICE > x + w ? x + w - i : TEX_SLICE);
tex_step = (cur_u + ustep > tx + tw ? tx + tw - cur_u : ustep);
TVertex2D* vertices = (TVertex2D*)sceGuGetMemory(2*sizeof(TVertex2D));
vertices[0].u = (s16)cur_u;
vertices[0].v = ty + th;
vertices[0].x = i;
vertices[0].y = y;
vertices[0].z = 0;
cur_u += tex_step;
vertices[1].u = (s16)cur_u;
vertices[1].v = ty;
vertices[1].x = i + slice_width;
vertices[1].y = y + h;
vertices[1].z = 0;
sceGuDrawArray(GU_SPRITES, TVertex2D_vtype|GU_TRANSFORM_2D, 2, 0, vertices);
}
}
void xGuDrawTexf(float x, float y, float w, float h, float tx, float ty, float tw, float th)
{
float cur_u = tx;
float ustep = TEX_SLICE*tw/w;
float slice_width = 0;
float tex_step = 0;
int i;
for (i = x; i < x + w; i += TEX_SLICE)
{
slice_width = (i + TEX_SLICE > x + w ? x + w - i : TEX_SLICE);
tex_step = (cur_u + ustep > tx + tw ? tx + tw - cur_u : ustep);
TVertexF* vertices = (TVertexF*)sceGuGetMemory(2*sizeof(TVertexF));
vertices[0].u = cur_u;
vertices[0].v = ty + th;
vertices[0].x = i;
vertices[0].y = y;
vertices[0].z = 0;
cur_u += tex_step;
vertices[1].u = cur_u;
vertices[1].v = ty;
vertices[1].x = i + slice_width;
vertices[1].y = y + h;
vertices[1].z = 0;
sceGuDrawArray(GU_SPRITES, TVertexF_vtype|GU_TRANSFORM_2D, 2, 0, vertices);
}
}