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
|
/***************************************************************************
* Copyright (C) 2016 by iCatButler *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
***************************************************************************/
/**************************************************************************
* pgxp_gpu.c
* PGXP - Parallel/Precision Geometry Xform Pipeline
*
* Created on: 25 Mar 2016
* Author: iCatButler
***************************************************************************/
#include "pgxp_gpu.h"
#include "stdafx.h"
#include "externals.h"
#include <math.h>
typedef struct
{
float x;
float y;
float z;
unsigned int valid;
unsigned int count;
unsigned int value;
} PGXP_vertex;
/////////////////////////////////
//// Blade_Arma's Vertex Cache (CatBlade?)
/////////////////////////////////
const unsigned int mode_init = 0;
const unsigned int mode_write = 1;
const unsigned int mode_read = 2;
PGXP_vertex vertexCache[0x800 * 2][0x800 * 2];
unsigned int baseID = 0;
unsigned int lastID = 0;
unsigned int cacheMode = 0;
unsigned int IsSessionID(unsigned int vertID)
{
// No wrapping
if (lastID >= baseID)
return (vertID >= baseID);
// If vertID is >= baseID it is pre-wrap and in session
if (vertID >= baseID)
return 1;
// vertID is < baseID, If it is <= lastID it is post-wrap and in session
if (vertID <= lastID)
return 1;
return 0;
}
void CALLBACK GPUpgxpCacheVertex(short sx, short sy, const unsigned char* _pVertex)
{
const PGXP_vertex* pNewVertex = (const PGXP_vertex*)_pVertex;
PGXP_vertex* pOldVertex = NULL;
if (!pNewVertex)
return;
//if (bGteAccuracy)
{
if (cacheMode != mode_write)
{
// Initialise cache on first use
if (cacheMode == mode_init)
memset(vertexCache, 0x00, sizeof(vertexCache));
// First vertex of write session (frame?)
cacheMode = mode_write;
baseID = pNewVertex->count;
}
lastID = pNewVertex->count;
if (sx >= -0x800 && sx <= 0x7ff &&
sy >= -0x800 && sy <= 0x7ff)
{
pOldVertex = &vertexCache[sy + 0x800][sx + 0x800];
// To avoid ambiguity there can only be one valid entry per-session
if (IsSessionID(pOldVertex->count) && (pOldVertex->value == pNewVertex->value))
{
// check to ensure this isn't identical
if ((fabsf(pOldVertex->x - pNewVertex->x) > 0.1f) ||
(fabsf(pOldVertex->y - pNewVertex->y) > 0.1f) ||
(fabsf(pOldVertex->z - pNewVertex->z) > 0.1f))
{
pOldVertex->valid = 5;
return;
}
}
// Write vertex into cache
*pOldVertex = *pNewVertex;
}
}
}
PGXP_vertex* PGXP_GetCachedVertex(short sx, short sy)
{
//if (bGteAccuracy)
{
if (cacheMode != mode_read)
{
// Initialise cache on first use
if (cacheMode == mode_init)
memset(vertexCache, 0x00, sizeof(vertexCache));
// First vertex of read session (frame?)
cacheMode = mode_read;
}
if (sx >= -0x800 && sx <= 0x7ff &&
sy >= -0x800 && sy <= 0x7ff)
{
// Return pointer to cache entry
return &vertexCache[sy + 0x800][sx + 0x800];
}
}
return NULL;
}
/////////////////////////////////
//// PGXP Implementation
/////////////////////////////////
const unsigned int primStrideTable[] = { 1, 2, 1, 2, 2, 3, 2, 3, 0 };
const unsigned int primCountTable[] = { 3, 3, 4, 4, 3, 3, 4, 4, 0 };
PGXP_vertex* PGXP_Mem = NULL; // pointer to parallel memory
unsigned int currentAddr = 0; // address of current DMA
unsigned int numVertices = 0; // iCB: Used for glVertex3fv fix
unsigned int vertexIdx = 0;
// Set current DMA address and pointer to parallel memory
void CALLBACK GPUpgxpMemory(unsigned int addr, unsigned char* pVRAM)
{
if (pVRAM)
PGXP_Mem = (PGXP_vertex*)(pVRAM);
currentAddr = addr;
}
// Set current DMA address
void PGXP_SetAddress(unsigned int addr)
{
currentAddr = addr;
}
void PGXP_SetMatrix(float left, float right, float bottom, float top, float zNear, float zFar)
{
GLfloat m[16];
for (unsigned int i = 0; i < 16; ++i)
m[i] = 0.f;
//if ((right-left) != 0)
//{
// m[0] = 2 / (right - left);
// m[12] = -((right + left) / (right - left));
//}
//if ((top-bottom) != 0)
//{
// m[5] = 2 / (top - bottom);
// m[13] = -((top + bottom) / (top - bottom));
//}
//m[10] = -2 / (zFar - zNear);
//m[14] = -((zFar + zNear) / (zFar - zNear));
//m[15] = 1;
if ((right-left) != 0)
{
m[0] = 2 / (right - left);
m[8] = -((right + left) / (right - left));
}
if ((top-bottom) != 0)
{
m[5] = 2 / (top - bottom);
m[9] = -((top + bottom) / (top - bottom));
}
m[10] = -2 / (zFar - zNear);
m[14] = -((zFar + zNear) / (zFar - zNear));
m[11] = 1;
glLoadMatrixf(m);
//glOrtho(left, right, bottom, top, zNear, zFar);
}
// Wrap glVertex3fv/glVertex4fv
void PGXP_glVertexfv(GLfloat* pV)
{
// If there are PGXP vertices expected
if (1)//(vertexIdx < numVertices)
{
float temp[4];
memcpy(temp, pV, sizeof(float) * 4);
//pre-multiply each element by w (to negate perspective divide)
for (unsigned int i = 0; i < 3; i++)
temp[i] *= temp[3];
//pass complete vertex to OpenGL
glVertex4fv(temp);
vertexIdx++;
//pV[3] = 1.f;
}
else
{
glVertex3fv(pV);
}
}
// Get parallel vertex values
int PGXP_GetVertices(unsigned int* addr, void* pOutput, int xOffs, int yOffs)
{
unsigned int primCmd = ((*addr >> 24) & 0xff); // primitive command
unsigned int primIdx = min((primCmd - 0x20) >> 2, 8); // index to primitive lookup
OGLVertex* pVertex = (OGLVertex*)pOutput; // pointer to output vertices
unsigned int stride = primStrideTable[primIdx]; // stride between vertices
unsigned int count = primCountTable[primIdx]; // number of vertices
PGXP_vertex* primStart = NULL; // pointer to first vertex
char invalidVert = 0; // Number of vertices without valid PGXP values
short* pPrimData = (short*)addr; // primitive data for cache lookups
PGXP_vertex* pCacheVert = NULL;
if (PGXP_Mem == NULL)
return 0;
// Reset vertex count
numVertices = count;
vertexIdx = 0;
// Offset to start of primitive
primStart = &PGXP_Mem[currentAddr + 1];
pPrimData += 2;
// Find any invalid vertices
for (unsigned i = 0; i < count; ++i)
{
if(!primStart[stride * i].valid)
invalidVert++;
}
for (unsigned i = 0; i < count; ++i)
{
if (primStart[stride * i].valid)
{
pVertex[i].x = (primStart[stride * i].x + xOffs);
pVertex[i].y = (primStart[stride * i].y + yOffs);
pVertex[i].z = 0.95f;
pVertex[i].w = primStart[stride * i].z;
pVertex[i].PGXP_flag = 1;
}
else
{
// Default to low precision vertex data
pVertex[i].PGXP_flag = 2;
// Look in cache for valid vertex
pCacheVert = PGXP_GetCachedVertex(pPrimData[stride * i * 2], pPrimData[(stride * i * 2) + 1]);
if (pCacheVert)
{
if (IsSessionID(pCacheVert->count))
{
if (pCacheVert->valid == 1)
{
pVertex[i].x = (pCacheVert->x + xOffs);
pVertex[i].y = (pCacheVert->y + yOffs);
pVertex[i].z = 0.95f;
pVertex[i].w = pCacheVert->z;
pVertex[i].PGXP_flag = 3;
// reduce number of invalid vertices
invalidVert--;
}
else if(pCacheVert->valid > 1)
pVertex[i].PGXP_flag = 4;
}
}
}
}
// If there are any invalid vertices set all w values to 1
// iCB: Could use plane equation to find w for single invalid vertex in a quad
if (invalidVert > 0)
for (unsigned i = 0; i < count; ++i)
pVertex[i].w = 1;
return 1;
}
/////////////////////////////////
//// Visual Debugging Functions
/////////////////////////////////
unsigned int PGXP_vDebug = 0;
const char red[4] = { 255, 0, 0, 255 };
const char blue[4] = { 0, 0, 255, 255 };
const char green[4] = { 0, 255, 0, 255 };
const char yellow[4] = { 255, 255, 0, 255 };
const char magenta[4] = { 255, 0, 255, 255 };
const char black[4] = { 0, 0, 0, 255 };
void CALLBACK GPUtoggleDebug(void)
{
if (PGXP_vDebug)
PGXP_vDebug = 0;
else
PGXP_vDebug = 1;
}
const char* PGXP_colour(unsigned int flag)
{
switch (flag)
{
case 0:
return yellow;
case 1:
return blue;
case 2:
return red;
case 3:
return green;
case 4:
return magenta;
default:
return black;
}
}
void PGXP_DrawDebugTriQuad(OGLVertex* vertex1, OGLVertex* vertex2, OGLVertex* vertex3, OGLVertex* vertex4)
{
GLboolean bTexture = glIsEnabled(GL_TEXTURE_2D);
GLfloat fColour[4];
glGetFloatv(GL_CURRENT_COLOR, fColour);
glDisable(GL_TEXTURE_2D);
glBegin(GL_TRIANGLE_STRIP);
glColor4ubv(PGXP_colour(vertex1->PGXP_flag));
PGXP_glVertexfv(&vertex1->x);
glColor4ubv(PGXP_colour(vertex2->PGXP_flag));
PGXP_glVertexfv(&vertex2->x);
glColor4ubv(PGXP_colour(vertex3->PGXP_flag));
PGXP_glVertexfv(&vertex3->x);
glColor4ubv(PGXP_colour(vertex4->PGXP_flag));
PGXP_glVertexfv(&vertex4->x);
glEnd();
glPolygonMode(GL_FRONT, GL_LINE);
glPolygonMode(GL_BACK, GL_LINE);
glBegin(GL_TRIANGLE_STRIP);
glColor4ubv(black);
PGXP_glVertexfv(&vertex1->x);
PGXP_glVertexfv(&vertex2->x);
PGXP_glVertexfv(&vertex3->x);
PGXP_glVertexfv(&vertex4->x);
glColor4fv(fColour);
glEnd();
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_FILL);
if(bTexture == GL_TRUE)
glEnable(GL_TEXTURE_2D);
}
void PGXP_DrawDebugTri(OGLVertex* vertex1, OGLVertex* vertex2, OGLVertex* vertex3)
{
GLboolean bTexture = glIsEnabled(GL_TEXTURE_2D);
GLfloat fColour[4];
glGetFloatv(GL_CURRENT_COLOR, fColour);
glDisable(GL_TEXTURE_2D);
glBegin(GL_TRIANGLES);
glColor4ubv(PGXP_colour(vertex1->PGXP_flag));
PGXP_glVertexfv(&vertex1->x);
glColor4ubv(PGXP_colour(vertex2->PGXP_flag));
PGXP_glVertexfv(&vertex2->x);
glColor4ubv(PGXP_colour(vertex3->PGXP_flag));
PGXP_glVertexfv(&vertex3->x);
glEnd();
glPolygonMode(GL_FRONT, GL_LINE);
glPolygonMode(GL_BACK, GL_LINE);
glBegin(GL_TRIANGLE_STRIP);
glColor4ubv(black);
PGXP_glVertexfv(&vertex1->x);
PGXP_glVertexfv(&vertex2->x);
PGXP_glVertexfv(&vertex3->x);
glColor4fv(fColour);
glEnd();
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_FILL);
if (bTexture == GL_TRUE)
glEnable(GL_TEXTURE_2D);
}
void PGXP_DrawDebugQuad(OGLVertex* vertex1, OGLVertex* vertex2, OGLVertex* vertex3, OGLVertex* vertex4)
{
GLboolean bTexture = glIsEnabled(GL_TEXTURE_2D);
GLfloat fColour[4];
glGetFloatv(GL_CURRENT_COLOR, fColour);
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glColor4ubv(PGXP_colour(vertex1->PGXP_flag));
PGXP_glVertexfv(&vertex1->x);
glColor4ubv(PGXP_colour(vertex2->PGXP_flag));
PGXP_glVertexfv(&vertex2->x);
glColor4ubv(PGXP_colour(vertex3->PGXP_flag));
PGXP_glVertexfv(&vertex3->x);
glColor4ubv(PGXP_colour(vertex4->PGXP_flag));
PGXP_glVertexfv(&vertex4->x);
glEnd();
glPolygonMode(GL_FRONT, GL_LINE);
glPolygonMode(GL_BACK, GL_LINE);
glBegin(GL_TRIANGLE_STRIP);
glColor4ubv(black);
PGXP_glVertexfv(&vertex1->x);
PGXP_glVertexfv(&vertex2->x);
PGXP_glVertexfv(&vertex3->x);
PGXP_glVertexfv(&vertex4->x);
glColor4fv(fColour);
glEnd();
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_FILL);
if (bTexture == GL_TRUE)
glEnable(GL_TEXTURE_2D);
}
|