-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgl-pvr.c
277 lines (210 loc) · 7.35 KB
/
gl-pvr.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
/* KallistiGL for KallistiOS ##version##
libgl/gl-pvr.c
Copyright (C) 2013-2014 Josh Pearson
Copyright (C) 2016, 2023 Lawrence Sebald
Vertex Buffer Routines for interfacing the Dreamcast's SH4 CPU and PowerVR GPU.
What we are doing here is using a Vertex Buffer in main RAM to store the
submitted data, untill the user finishes the scene by either calling
glSwapBuffer() to submit the buffer to the PVR for render to the screen OR
glSwapBufferToTexture() to submit the buffer to the PVR for render to texture.
This solution means the client can switch between transparent and opaque polygon
sumbission at any time, in no particular order, with no penalty in speed.
The size of the Vertex Buffer can be controlled by setting some params on gl-pvr.h:
GL_PVR_VERTEX_BUF_SIZE controls size of Vertex Buffer in the PVR VRAM
GL_KOS_MAX_VERTS conrols the number of vertices per list in the Vertex Buffer in SH4 RAM
*/
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dc/sq.h>
#include "gl.h"
#include "gl-api.h"
#include "gl-sh4.h"
#include "gl-pvr.h"
/* Vertex Buffer Functions *************************************************************************/
#ifdef GL_KOS_USE_MALLOC
static pvr_cmd_t *GL_VBUF[2] __attribute__((aligned(32))); /* Dynamic Vertex Buffer */
static pvr_cmd_t *GL_CBUF; /* Dynamic Clip Buffer */
static glTexCoord *GL_UVBUF; /* Dynamic Multi-Texture UV Buffer */
#else
static pvr_cmd_t GL_VBUF[2][GL_KOS_MAX_VERTS] __attribute__((aligned(32))); /* Static Vertex Buffer */
static pvr_cmd_t GL_CBUF[GL_KOS_MAX_VERTS / 2]; /* Static Clip Buffer */
static glTexCoord GL_UVBUF[GL_KOS_MAX_VERTS / 2]; /* Static Multi-Texture UV Buffer */
#endif
static GLuint GL_VERTS[2] = {0, 0},
GL_CVERTS = 0,
GL_UVVERTS = 0,
GL_LIST = GL_KOS_LIST_OP;
#define GL_KOS_MAX_MULTITEXTURE_OBJECTS 512
static GL_MULTITEX_OBJECT GL_MTOBJS[GL_KOS_MAX_MULTITEXTURE_OBJECTS];
static GLuint GL_MTOBJECTS = 0;
#define pvr_list_submit(src, n) pvr_sq_load(NULL, (src), ((n) << 5), PVR_DMA_TA)
#define pvr_hdr_submit(src) pvr_sq_load(NULL, (src), 32, PVR_DMA_TA)
inline void _glKosPushMultiTexObject(GL_TEXTURE_OBJECT *tex,
pvr_vertex_t *src,
GLuint count) {
_glKosCompileHdrMT(&GL_MTOBJS[GL_MTOBJECTS].hdr, tex);
GL_MTOBJS[GL_MTOBJECTS].src = src;
GL_MTOBJS[GL_MTOBJECTS++].count = count;
}
static inline void _glKosResetMultiTexObject() {
GL_MTOBJECTS = 0;
}
inline void *_glKosMultiUVBufAddress() {
return &GL_UVBUF[0];
}
inline void *_glKosMultiUVBufPointer() {
return &GL_UVBUF[GL_UVVERTS];
}
inline void _glKosMultiUVBufIncrement() {
++GL_UVVERTS;
}
inline void _glKosMultiUVBufAdd(GLuint count) {
GL_UVVERTS += count;
}
inline void _glKosMultiUVBufReset() {
GL_UVVERTS = 0;
}
inline void *_glKosClipBufAddress() {
return &GL_CBUF[0];
}
inline void *_glKosClipBufPointer() {
return &GL_CBUF[GL_CVERTS];
}
inline void _glKosClipBufIncrement() {
++GL_CVERTS;
}
void _glKosClipBufAdd(GLuint count) {
GL_CVERTS += count;
}
inline void _glKosClipBufReset() {
GL_CVERTS = 0;
}
inline void _glKosVertexBufSwitchOP() {
GL_LIST = GL_KOS_LIST_OP;
}
inline void _glKosVertexBufSwitchTR() {
GL_LIST = GL_KOS_LIST_TR;
}
inline void *_glKosVertexBufAddress(GLubyte list) {
return &GL_VBUF[list][0];
}
inline void *_glKosVertexBufPointer() {
return &GL_VBUF[GL_LIST][GL_VERTS[GL_LIST]];
}
inline void _glKosVertexBufIncrement() {
++GL_VERTS[GL_LIST];
}
inline void *_glKosTRVertexBufPointer() {
return &GL_VBUF[GL_KOS_LIST_TR][GL_VERTS[GL_KOS_LIST_TR]];
}
inline void _glKosTRVertexBufIncrement() {
++GL_VERTS[GL_KOS_LIST_TR];
}
inline void _glKosVertexBufAdd(GLuint count) {
GL_VERTS[GL_LIST] += count;
}
inline void _glKosTRVertexBufAdd(GLuint count) {
GL_VERTS[GL_KOS_LIST_TR] += count;
}
inline void _glKosVertexBufDecrement() {
--GL_VERTS[GL_LIST];
}
inline void _glKosVertexBufReset() {
GL_VERTS[0] = GL_VERTS[1] = 0;
}
static inline GLuint _glKosVertexBufCount(GLubyte list) {
return GL_VERTS[list];
}
GLubyte _glKosList() {
return GL_LIST;
}
inline void _glKosVertexBufCopy(void *dst, void *src, GLuint count) {
memcpy(dst, src, count * 0x20);
}
static inline void glutSwapBuffer() {
#ifndef GL_KOS_USE_DMA
#if 0
QACR0 = QACRTA;
QACR1 = QACRTA;
#endif
#endif
pvr_list_begin(PVR_LIST_OP_POLY);
#ifdef GL_KOS_USE_DMA
pvr_dma_transfer(_glKosVertexBufAddress(GL_KOS_LIST_OP), 0,
_glKosVertexBufCount(GL_KOS_LIST_OP) * 32,
PVR_DMA_TA, 1, NULL, 0);
#else
pvr_list_submit(_glKosVertexBufAddress(GL_KOS_LIST_OP), _glKosVertexBufCount(GL_KOS_LIST_OP));
#endif
pvr_list_finish();
pvr_list_begin(PVR_LIST_TR_POLY);
#ifdef GL_KOS_USE_DMA
pvr_dma_transfer(_glKosVertexBufAddress(GL_KOS_LIST_TR), 0,
_glKosVertexBufCount(GL_KOS_LIST_TR) * 32,
PVR_DMA_TA, 1, NULL, 0);
#else
pvr_list_submit(_glKosVertexBufAddress(GL_KOS_LIST_TR), _glKosVertexBufCount(GL_KOS_LIST_TR));
#endif
/* Multi-Texture Pass - Modify U/V coords of submitted vertices */
GLuint i, v;
glTexCoord *mt = _glKosMultiUVBufAddress();
for(i = 0; i < GL_MTOBJECTS; i++) {
//copy vertex uv
for(v = 0; v < GL_MTOBJS[i].count; v ++) {
GL_MTOBJS[i].src[v].u = mt->u;
GL_MTOBJS[i].src[v].v = mt->v;
++mt;
}
// submit vertex data to PVR
#ifdef GL_KOS_USE_DMA
pvr_hdr_submit((GLuint *)&GL_MTOBJS[i].hdr);
pvr_dma_transfer(GL_MTOBJS[i].src, 0,
GL_MTOBJS[i].count * 32, PVR_DMA_TA, 1, NULL, 0);
#else
pvr_list_submit((pvr_poly_hdr_t *)&GL_MTOBJS[i].hdr, 1);
pvr_list_submit((pvr_vertex_t *)GL_MTOBJS[i].src, GL_MTOBJS[i].count);
#endif
}
_glKosResetMultiTexObject(); /* End Multi-Texture Pass */
pvr_list_finish();
pvr_scene_finish();
}
void glutSwapBuffers() {
pvr_wait_ready();
if(_glKosGetFBO()) {
GLsizei w = _glKosGetFBOWidth(_glKosGetFBO());
GLsizei h = _glKosGetFBOHeight(_glKosGetFBO());
pvr_scene_begin_txr(_glKosGetFBOData(_glKosGetFBO()), &w, &h);
}
else
pvr_scene_begin();
glutSwapBuffer();
_glKosVertexBufReset();
_glKosMultiUVBufReset();
}
void glutCopyBufferToTexture(void *dst, GLsizei *x, GLsizei *y) {
if(_glKosGetFBO()) {
pvr_wait_ready();
pvr_scene_begin_txr(dst, x, y);
glutSwapBuffer();
}
}
int _glKosInitPVR() {
pvr_init_params_t params = {
/* Enable opaque and translucent polygons with size 32 and 32 */
{ PVR_BINSIZE_32, PVR_BINSIZE_0, PVR_BINSIZE_32, PVR_BINSIZE_0, PVR_BINSIZE_0 },
GL_PVR_VERTEX_BUF_SIZE, /* Vertex buffer size */
0, /* No DMA */
0 /* No FSAA */
};
pvr_init(¶ms);
#ifdef GL_KOS_USE_MALLOC
GL_VBUF[0] = memalign(0x20, GL_KOS_MAX_VERTS * sizeof(pvr_cmd_t));
GL_VBUF[1] = memalign(0x20, GL_KOS_MAX_VERTS * sizeof(pvr_cmd_t));
GL_CBUF = malloc((GL_KOS_MAX_VERTS / 2) * sizeof(pvr_cmd_t));
GL_UVBUF = malloc(GL_KOS_MAX_VERTS * sizeof(glTexCoord));
#endif
return 1;
}