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
|
#include <math.h>
#include "sh4.h"
#include "sh4_math.h"
#define CLIP_DEBUG 0
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define SQ_BASE_ADDRESS (void*) 0xe0000000
GL_FORCE_INLINE float _glFastInvert(float x) {
return MATH_fsrra(x * x);
}
GL_FORCE_INLINE void _glPerspectiveDivideVertex(Vertex* vertex) {
TRACE();
const float f = _glFastInvert(vertex->w);
/* Convert to NDC and apply viewport */
vertex->xyz[0] = (vertex->xyz[0] * f * VIEWPORT.hwidth) + VIEWPORT.x_plus_hwidth;
vertex->xyz[1] = (vertex->xyz[1] * f * VIEWPORT.hheight) + VIEWPORT.y_plus_hheight;
/* Orthographic projections need to use invZ otherwise we lose
the depth information. As w == 1, and clip-space range is -w to +w
we add 1.0 to the Z to bring it into range. We add a little extra to
avoid a divide by zero.
*/
if(vertex->w == 1.0f) {
vertex->xyz[2] = _glFastInvert(1.0001f + vertex->xyz[2]);
} else {
vertex->xyz[2] = f;
}
}
volatile uint32_t *sq = SQ_BASE_ADDRESS;
static inline void _glFlushBuffer() {
TRACE();
/* Wait for both store queues to complete */
sq = (uint32_t*) 0xe0000000;
sq[0] = sq[8] = 0;
}
static inline void _glPushHeaderOrVertex(Vertex* v) {
TRACE();
uint32_t* s = (uint32_t*) v;
sq[0] = *(s++);
sq[1] = *(s++);
sq[2] = *(s++);
sq[3] = *(s++);
sq[4] = *(s++);
sq[5] = *(s++);
sq[6] = *(s++);
sq[7] = *(s++);
__asm__("pref @%0" : : "r"(sq));
sq += 8;
}
static void _glClipEdge(const Vertex* const v1, const Vertex* const v2, Vertex* vout) {
const float d0 = v1->w + v1->xyz[2];
const float d1 = v2->w + v2->xyz[2];
const float t = (fabs(d0) * MATH_fsrra((d1 - d0) * (d1 - d0))) + 0.000001f;
const float invt = 1.0f - t;
vout->xyz[0] = invt * v1->xyz[0] + t * v2->xyz[0];
vout->xyz[1] = invt * v1->xyz[1] + t * v2->xyz[1];
vout->xyz[2] = invt * v1->xyz[2] + t * v2->xyz[2];
vout->uv[0] = invt * v1->uv[0] + t * v2->uv[0];
vout->uv[1] = invt * v1->uv[1] + t * v2->uv[1];
vout->w = invt * v1->w + t * v2->w;
vout->bgra[0] = invt * v1->bgra[0] + t * v2->bgra[0];
vout->bgra[1] = invt * v1->bgra[1] + t * v2->bgra[1];
vout->bgra[2] = invt * v1->bgra[2] + t * v2->bgra[2];
vout->bgra[3] = invt * v1->bgra[3] + t * v2->bgra[3];
}
#define SPAN_SORT_CFG 0x005F8030
static volatile uint32_t* PVR_LMMODE0 = (uint32_t*) 0xA05F6884;
static volatile uint32_t* PVR_LMMODE1 = (uint32_t*) 0xA05F6888;
static volatile uint32_t* QACR = (uint32_t*) 0xFF000038;
#define V0_VIS (1 << 0)
#define V1_VIS (1 << 1)
#define V2_VIS (1 << 2)
#define V3_VIS (1 << 3)
// https://casual-effects.com/research/McGuire2011Clipping/clip.glsl
static void SubmitClipped(Vertex* v0, Vertex* v1, Vertex* v2, Vertex* v3, uint8_t visible_mask) {
Vertex __attribute__((aligned(32))) scratch[4];
Vertex* a = &scratch[0];
Vertex* b = &scratch[1];
switch(visible_mask) {
case V0_VIS:
{
// v0
// / |
// / |
// .....A....B...
// / |
// v3--v2---v1
_glClipEdge(v3, v0, a);
a->flags = PVR_CMD_VERTEX_EOL;
_glClipEdge(v0, v1, b);
b->flags = PVR_CMD_VERTEX;
_glPerspectiveDivideVertex(v0);
_glPushHeaderOrVertex(v0);
_glPerspectiveDivideVertex(b);
_glPushHeaderOrVertex(b);
_glPerspectiveDivideVertex(a);
_glPushHeaderOrVertex(a);
}
break;
case V1_VIS:
{
// v1
// / |
// / |
// ....A.....B...
// / |
// v0--v3---v2
_glClipEdge(v0, v1, a);
a->flags = PVR_CMD_VERTEX;
_glClipEdge(v1, v2, b);
b->flags = PVR_CMD_VERTEX_EOL;
_glPerspectiveDivideVertex(a);
_glPushHeaderOrVertex(a);
_glPerspectiveDivideVertex(v1);
_glPushHeaderOrVertex(v1);
_glPerspectiveDivideVertex(b);
_glPushHeaderOrVertex(b);
} break;
case V2_VIS:
{
// v2
// / |
// / |
// ....A.....B...
// / |
// v1--v0---v3
_glClipEdge(v1, v2, a);
a->flags = PVR_CMD_VERTEX;
_glClipEdge(v2, v3, b);
b->flags = PVR_CMD_VERTEX_EOL;
_glPerspectiveDivideVertex(a);
_glPushHeaderOrVertex(a);
_glPerspectiveDivideVertex(v2);
_glPushHeaderOrVertex(v2);
_glPerspectiveDivideVertex(b);
_glPushHeaderOrVertex(b);
} break;
case V3_VIS:
{
// v3
// / |
// / |
// ....A.....B...
// / |
// v2--v1---v0
_glClipEdge(v2, v3, a);
a->flags = PVR_CMD_VERTEX;
_glClipEdge(v3, v0, b);
b->flags = PVR_CMD_VERTEX;
_glPerspectiveDivideVertex(b);
_glPushHeaderOrVertex(b);
_glPerspectiveDivideVertex(a);
_glPushHeaderOrVertex(a);
_glPerspectiveDivideVertex(v3);
_glPushHeaderOrVertex(v3);
}
break;
case V0_VIS | V1_VIS:
{
// v0-----------v1
// \ |
// ....B..........A...
// \ |
// v3-----v2
_glClipEdge(v1, v2, a);
a->flags = PVR_CMD_VERTEX;
_glClipEdge(v3, v0, b);
b->flags = PVR_CMD_VERTEX_EOL;
_glPerspectiveDivideVertex(v1);
_glPushHeaderOrVertex(v1);
_glPerspectiveDivideVertex(a);
_glPushHeaderOrVertex(a);
_glPerspectiveDivideVertex(v0);
_glPushHeaderOrVertex(v0);
_glPerspectiveDivideVertex(b);
_glPushHeaderOrVertex(b);
} break;
// case V0_VIS | V2_VIS: degenerate case that should never happen
case V0_VIS | V3_VIS:
{
// v3-----------v0
// \ |
// ....B..........A...
// \ |
// v2-----v1
_glClipEdge(v0, v1, a);
a->flags = PVR_CMD_VERTEX;
_glClipEdge(v2, v3, b);
b->flags = PVR_CMD_VERTEX;
_glPerspectiveDivideVertex(a);
_glPushHeaderOrVertex(a);
_glPerspectiveDivideVertex(b);
_glPushHeaderOrVertex(b);
_glPerspectiveDivideVertex(v0);
_glPushHeaderOrVertex(v0);
_glPerspectiveDivideVertex(v3);
_glPushHeaderOrVertex(v3);
} break;
case V1_VIS | V2_VIS:
{
// v1-----------v2
// \ |
// ....B..........A...
// \ |
// v0-----v3
_glClipEdge(v2, v3, a);
a->flags = PVR_CMD_VERTEX_EOL;
_glClipEdge(v0, v1, b);
b->flags = PVR_CMD_VERTEX;
_glPerspectiveDivideVertex(v1);
_glPushHeaderOrVertex(v1);
_glPerspectiveDivideVertex(v2);
_glPushHeaderOrVertex(v2);
_glPerspectiveDivideVertex(b);
_glPushHeaderOrVertex(b);
_glPerspectiveDivideVertex(a);
_glPushHeaderOrVertex(a);
} break;
// case V1_VIS | V3_VIS: degenerate case that should never happen
case V2_VIS | V3_VIS:
{
// v2-----------v3
// \ |
// ....B..........A...
// \ |
// v1-----v0
_glClipEdge(v3, v0, a);
a->flags = PVR_CMD_VERTEX;
_glClipEdge(v1, v2, b);
b->flags = PVR_CMD_VERTEX;
_glPerspectiveDivideVertex(b);
_glPushHeaderOrVertex(b);
_glPerspectiveDivideVertex(v2);
_glPushHeaderOrVertex(v2);
_glPerspectiveDivideVertex(a);
_glPushHeaderOrVertex(a);
_glPerspectiveDivideVertex(v3);
_glPushHeaderOrVertex(v3);
} break;
case V0_VIS | V1_VIS | V2_VIS:
{
// --v1--
// v0-- --v2
// \ |
// .....B.....A...
// \ |
// v3
// v1,v2,v0 v2,v0,A v0,A,B
_glClipEdge(v2, v3, a);
a->flags = PVR_CMD_VERTEX;
_glClipEdge(v3, v0, b);
b->flags = PVR_CMD_VERTEX_EOL;
_glPerspectiveDivideVertex(v1);
_glPushHeaderOrVertex(v1);
_glPerspectiveDivideVertex(v2);
_glPushHeaderOrVertex(v2);
_glPerspectiveDivideVertex(v0);
_glPushHeaderOrVertex(v0);
_glPerspectiveDivideVertex(a);
_glPushHeaderOrVertex(a);
_glPerspectiveDivideVertex(b);
_glPushHeaderOrVertex(b);
} break;
case V0_VIS | V1_VIS | V3_VIS:
{
// --v0--
// v3-- --v1
// \ |
// .....B.....A...
// \ |
// v2
// v0,v1,v3 v1,v3,A v3,A,B
_glClipEdge(v1, v2, a);
a->flags = PVR_CMD_VERTEX;
_glClipEdge(v2, v3, b);
b->flags = PVR_CMD_VERTEX_EOL;
v3->flags = PVR_CMD_VERTEX;
_glPerspectiveDivideVertex(v0);
_glPushHeaderOrVertex(v0);
_glPerspectiveDivideVertex(v1);
_glPushHeaderOrVertex(v1);
_glPerspectiveDivideVertex(v3);
_glPushHeaderOrVertex(v3);
_glPerspectiveDivideVertex(a);
_glPushHeaderOrVertex(a);
_glPerspectiveDivideVertex(b);
_glPushHeaderOrVertex(b);
} break;
case V0_VIS | V2_VIS | V3_VIS:
{
// --v3--
// v2-- --v0
// \ |
// .....B.....A...
// \ |
// v1
// v3,v0,v2 v0,v2,A v2,A,B
_glClipEdge(v0, v1, a);
a->flags = PVR_CMD_VERTEX;
_glClipEdge(v1, v2, b);
b->flags = PVR_CMD_VERTEX_EOL;
v3->flags = PVR_CMD_VERTEX;
_glPerspectiveDivideVertex(v3);
_glPushHeaderOrVertex(v3);
_glPerspectiveDivideVertex(v0);
_glPushHeaderOrVertex(v0);
_glPerspectiveDivideVertex(v2);
_glPushHeaderOrVertex(v2);
_glPerspectiveDivideVertex(a);
_glPushHeaderOrVertex(a);
_glPerspectiveDivideVertex(b);
_glPushHeaderOrVertex(b);
} break;
case V1_VIS | V2_VIS | V3_VIS:
{
// --v2--
// v1-- --v3
// \ |
// .....B.....A...
// \ |
// v0
// v2,v3,v1 v3,v1,A v1,A,B
_glClipEdge(v3, v0, a);
a->flags = PVR_CMD_VERTEX;
_glClipEdge(v0, v1, b);
b->flags = PVR_CMD_VERTEX_EOL;
v3->flags = PVR_CMD_VERTEX;
_glPerspectiveDivideVertex(v2);
_glPushHeaderOrVertex(v2);
_glPerspectiveDivideVertex(v3);
_glPushHeaderOrVertex(v3);
_glPerspectiveDivideVertex(v1);
_glPushHeaderOrVertex(v1);
_glPerspectiveDivideVertex(a);
_glPushHeaderOrVertex(a);
_glPerspectiveDivideVertex(b);
_glPushHeaderOrVertex(b);
} break;
}
}
void SceneListSubmit(Vertex* v3, int n) {
TRACE();
/* You need at least a header, and 3 vertices to render anything */
if(n < 4) return;
PVR_SET(SPAN_SORT_CFG, 0x0);
//Set PVR DMA registers
*PVR_LMMODE0 = 0;
*PVR_LMMODE1 = 0;
//Set QACR registers
QACR[1] = QACR[0] = 0x11;
#if CLIP_DEBUG
Vertex* vertex = (Vertex*) src;
for(int i = 0; i < n; ++i) {
fprintf(stderr, "{%f, %f, %f, %f}, // %x (%x)\n", vertex[i].xyz[0], vertex[i].xyz[1], vertex[i].xyz[2], vertex[i].w, vertex[i].flags, &vertex[i]);
}
fprintf(stderr, "----\n");
#endif
uint8_t visible_mask = 0;
sq = SQ_BASE_ADDRESS;
for(int i = 0; i < n; ++i, ++v3) {
PREFETCH(v3 + 1);
switch(v3->flags & 0xFF000000) {
case PVR_CMD_VERTEX_EOL:
break;
case PVR_CMD_VERTEX:
continue;
default:
_glPushHeaderOrVertex(v3);
continue;
};
// Quads [0, 1, 2, 3] -> Triangles [{0, 1, 2} {2, 3, 0}]
Vertex* const v0 = v3 - 3;
Vertex* const v1 = v3 - 2;
Vertex* const v2 = v3 - 1;
visible_mask = v3->flags & 0xFF;
v3->flags &= ~0xFF;
// Stats gathering found that when testing a 64x64x64 sized world, at most
// ~400-500 triangles needed clipping
// ~13% of the triangles in a frame needed clipping (percentage increased when less triangles overall)
// Based on this, the decision was made to optimise for rendering quads there
// were either entirely visible or entirely culled, at the expensive at making
// partially visible quads a bit slower due to needing to be split into two triangles first
// Performance measuring indicated that overall FPS improved from this change
// to switching to try to process 1 quad instead of 2 triangles though
switch(visible_mask) {
case V0_VIS | V1_VIS | V2_VIS | V3_VIS: // All vertices visible
{
// Triangle strip: {1,2,0} {2,0,3}
_glPerspectiveDivideVertex(v1);
_glPushHeaderOrVertex(v1);
_glPerspectiveDivideVertex(v2);
_glPushHeaderOrVertex(v2);
_glPerspectiveDivideVertex(v0);
_glPushHeaderOrVertex(v0);
_glPerspectiveDivideVertex(v3);
_glPushHeaderOrVertex(v3);
}
break;
default: // Some vertices visible
SubmitClipped(v0, v1, v2, v3, visible_mask);
break;
}
}
_glFlushBuffer();
}
|