Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libmupen64plus/mupen64plus-video-z64/src/rgl_debugger.cpp
2 views
1
/*
2
* z64
3
*
4
* Copyright (C) 2007 ziggy
5
*
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License along
17
* with this program; if not, write to the Free Software Foundation, Inc.,
18
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
**/
21
22
#include "rdp.h"
23
#include "rgl.h"
24
25
static const char *saRGBText[] =
26
{
27
"PREV", "TEXEL0", "TEXEL1", "PRIM",
28
"SHADE", "ENV", "NOISE", "1",
29
"0", "0", "0", "0",
30
"0", "0", "0", "0"
31
};
32
33
static const char *mRGBText[] =
34
{
35
"PREV", "TEXEL0", "TEXEL1", "PRIM",
36
"SHADE", "ENV", "SCALE", "PREV_ALPHA",
37
"TEXEL0_ALPHA", "TEXEL1_ALPHA", "PRIM_ALPHA", "SHADE_ALPHA",
38
"ENV_ALPHA", "LOD_FRACTION", "PRIM_LOD_FRAC", "K5",
39
"0", "0", "0", "0",
40
"0", "0", "0", "0",
41
"0", "0", "0", "0",
42
"0", "0", "0", "0"
43
};
44
45
static const char *aRGBText[] =
46
{
47
"PREV", "TEXEL0", "TEXEL1", "PRIM",
48
"SHADE", "ENV", "1", "0",
49
};
50
51
static const char *saAText[] =
52
{
53
"PREV", "TEXEL0", "TEXEL1", "PRIM",
54
"SHADE", "ENV", "1", "0",
55
};
56
57
static const char *sbAText[] =
58
{
59
"PREV", "TEXEL0", "TEXEL1", "PRIM",
60
"SHADE", "ENV", "1", "0",
61
};
62
63
static const char *mAText[] =
64
{
65
"LOD_FRACTION", "TEXEL0", "TEXEL1", "PRIM",
66
"SHADE", "ENV", "PRIM_LOD_FRAC", "0",
67
};
68
69
static const char *aAText[] =
70
{
71
"PREV", "TEXEL0", "TEXEL1", "PRIM",
72
"SHADE", "ENV", "1", "0",
73
};
74
75
const static char * bRGBText[] = { "PREV", "FRAG", "BLEND", "FOG" };
76
const static char * bAText[2][4] = { {"PREVA", "FOGA", "SHADEA", "0"},
77
{"(1.0-ALPHA)", "FRAGA", "1", "0"}};
78
79
char * rglCombiner2String(rdpState_t & state)
80
{
81
rdpOtherModes_t om = state.otherModes;
82
int cycle = RDP_GETOM_CYCLE_TYPE(om);
83
static char res[256];
84
char * p = res;
85
if (cycle < 2) {
86
p += sprintf(
87
p,
88
"c = [ (%s - %s) * %s + %s | (%s - %s) * %s + %s ]\n",
89
saRGBText[RDP_GETCM_SUB_A_RGB0(state.combineModes)],
90
saRGBText[RDP_GETCM_SUB_B_RGB0(state.combineModes)],
91
mRGBText[RDP_GETCM_MUL_RGB0(state.combineModes)],
92
aRGBText[RDP_GETCM_ADD_RGB0(state.combineModes)],
93
saAText[RDP_GETCM_SUB_A_A0(state.combineModes)],
94
sbAText[RDP_GETCM_SUB_B_A0(state.combineModes)],
95
mAText[RDP_GETCM_MUL_A0(state.combineModes)],
96
aAText[RDP_GETCM_ADD_A0(state.combineModes)]);
97
}
98
if (cycle == 1) {
99
p += sprintf(
100
p,
101
"c = [ (%s - %s) * %s + %s | (%s - %s) * %s + %s ]\n",
102
saRGBText[RDP_GETCM_SUB_A_RGB1(state.combineModes)],
103
saRGBText[RDP_GETCM_SUB_B_RGB1(state.combineModes)],
104
mRGBText[RDP_GETCM_MUL_RGB1(state.combineModes)],
105
aRGBText[RDP_GETCM_ADD_RGB1(state.combineModes)],
106
saAText[RDP_GETCM_SUB_A_A1(state.combineModes)],
107
sbAText[RDP_GETCM_SUB_B_A1(state.combineModes)],
108
mAText[RDP_GETCM_MUL_A1(state.combineModes)],
109
aAText[RDP_GETCM_ADD_A1(state.combineModes)]);
110
}
111
if (cycle < 2) {
112
p += sprintf(
113
p,
114
"%s*%s + %s*%s\n"
115
,bAText[0][RDP_GETOM_BLEND_M1B_0(state.otherModes)],
116
bRGBText[RDP_GETOM_BLEND_M1A_0(state.otherModes)],
117
bAText[1][RDP_GETOM_BLEND_M2B_0(state.otherModes)],
118
bRGBText[RDP_GETOM_BLEND_M2A_0(state.otherModes)]
119
);
120
}
121
if (cycle == 1) {
122
p += sprintf(
123
p,
124
"%s*%s + %s*%s\n"
125
,bAText[0][RDP_GETOM_BLEND_M1B_1(state.otherModes)],
126
bRGBText[RDP_GETOM_BLEND_M1A_1(state.otherModes)],
127
bAText[1][RDP_GETOM_BLEND_M2B_1(state.otherModes)],
128
bRGBText[RDP_GETOM_BLEND_M2A_1(state.otherModes)]
129
);
130
}
131
return res;
132
}
133
134
#ifdef RDP_DEBUG
135
136
#include <SDL.h>
137
//#include <IL/il.h>
138
#include <assert.h>
139
140
#include <FTGLTextureFont.h>
141
142
#define FONT "LucidaTypewriterRegular.ttf"
143
#define SMALLFONT "LucidaTypewriterRegular.ttf"
144
//#define SMALLFONT "/usr/share/fonts/corefonts/arial.ttf"
145
#define FS 12
146
#define SMALLFS 12
147
148
static FTFont * font;
149
static FTFont * smallfont;
150
static FTFont * curfont;
151
152
static int fbindex;
153
static int chunkindex, stripindex;
154
static int mx, my;
155
static float scalex, scaley;
156
static rglShader_t * alphaShader;
157
158
static int lines[0x10000], nblines;
159
static char dasm[512];
160
161
void gglPrint(int x, int y, const char * text)
162
{
163
glPushAttrib(GL_ALL_ATTRIB_BITS);
164
glPushMatrix();
165
glTranslatef(x, y, 0);
166
167
glEnable( GL_TEXTURE_2D);
168
glDisable( GL_DEPTH_TEST);
169
//glRasterPos2i( x , y);
170
curfont->Render(text);
171
172
glPopMatrix();
173
glPopAttrib();
174
175
//printf("%s\n", text);
176
}
177
178
void gglPrintf(int x, int y, const char * s, ...)
179
{
180
char buf[1024];
181
va_list ap;
182
va_start(ap, s);
183
vsprintf(buf, s, ap);
184
va_end(ap);
185
gglPrint(x, y, buf);
186
}
187
188
void rglDisplayTrace(int x, int y, int start, int lines)
189
{
190
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
191
rglUseShader(0);
192
curfont = smallfont;
193
start = ::lines[start];
194
while (lines-->0 && start <= rdpTracePos) {
195
glColor4f(0,0,0, 0.5);
196
glEnable(GL_BLEND);
197
glDisable(GL_TEXTURE_2D);
198
glBegin(GL_TRIANGLE_STRIP);
199
glVertex2f(x, y);
200
glVertex2f(x+2*screen_width*3/4, y);
201
glVertex2f(x, y-(SMALLFS+2));
202
glVertex2f(x+2*screen_width*3/4, y-(SMALLFS+2));
203
glEnd();
204
205
glColor4f(1,1,0.5,1);
206
glDisable(GL_BLEND);
207
start += rdp_dasm(rdpTraceBuf, start, start+256, dasm)/4;
208
gglPrintf(x, y-(SMALLFS), "%4x %s", start, dasm);
209
y -= (SMALLFS+2);
210
}
211
curfont = font;
212
// glDisable(GL_BLEND);
213
}
214
215
void rglDisplayColor(uint32_t color, int x , int y, const char * name, int sixteen = 0)
216
{
217
float r, g, b, a;
218
if (sixteen) {
219
r = RDP_GETC16_R(color)/ 31.0f;
220
g = RDP_GETC16_G(color)/ 31.0f;
221
b = RDP_GETC16_B(color)/ 31.0f;
222
a = RDP_GETC16_A(color)/ 1.0f;
223
} else {
224
r = RDP_GETC32_R(color)/255.0f;
225
g = RDP_GETC32_G(color)/255.0f;
226
b = RDP_GETC32_B(color)/255.0f;
227
a = RDP_GETC32_A(color)/255.0f;
228
}
229
y -= FS+2;
230
glColor4f(r, g, b, 1);
231
glDisable(GL_TEXTURE_2D);
232
glBegin(GL_TRIANGLE_STRIP);
233
glVertex2f(x, y);
234
glVertex2f(x+128, y);
235
glVertex2f(x, y-16);
236
glVertex2f(x+128, y-16);
237
glEnd();
238
239
glEnable(GL_TEXTURE_2D);
240
glColor4f(1,1,1,1);
241
gglPrintf(x, y+2, "%5s %08x", name, color);
242
243
}
244
245
void rglDisplayChunkInfo(rglRenderChunk_t & chunk)
246
{
247
int x = 0, y = screen_height;
248
int i;
249
rdpState_t & state = chunk.rdpState;
250
rdpOtherModes_t om = state.otherModes;
251
rdpCombineModes_t cm = state.combineModes;
252
int cycle = RDP_GETOM_CYCLE_TYPE(om);
253
254
rglDisplayColor(chunk.rdpState.primColor, x, y, "prim");
255
y -= 16+FS+10;
256
rglDisplayColor(chunk.rdpState.blendColor, x, y, "blend");
257
y -= 16+FS+10;
258
rglDisplayColor(chunk.rdpState.envColor, x, y, "env");
259
y -= 16+FS+10;
260
rglDisplayColor(chunk.rdpState.fogColor, x, y, "fog");
261
y -= 16+FS+10;
262
rglDisplayColor(chunk.rdpState.fillColor, x, y, "fill", 1);
263
y -= 16+FS+10;
264
265
y += 5*(16+FS+10);
266
x += 128+20;
267
268
glColor4f(1,1,1,1);
269
int oldy = y;
270
for (i=0; i<8; i++) {
271
int j;
272
int oldx = x;
273
if (!(chunk.flags & (1<<i))) continue;
274
rglTile_t & tile = chunk.tiles[i];
275
int w = tile.w, h = tile.h;
276
if (w > 64) w = 64;
277
if (h > 64) h = 64;
278
gglPrintf(x, y-h-FS, "#%d %dx%d %x", i, tile.w, tile.h, tile.hiresBuffer? 0 : tile.tex->crc);
279
gglPrintf(x, y-h-2*FS, "fmt %s-%d %d %d", rdpImageFormats[tile.format], tile.size, tile.line, tile.hiresBuffer? tile.hiresBuffer-rBuffers : -1);
280
gglPrintf(x, y-h-3*FS, "clip %dx%d %dx%d", tile.sl, tile.tl, tile.sh, tile.th);
281
gglPrintf(x, y-h-4*FS, "mask %dx%d shift %dx%d", tile.mask_s, tile.mask_t, tile.shift_s, tile.shift_t);
282
gglPrintf(x, y-h-5*FS, "%d %d %d %d pal %d", tile.cs, tile.ms, tile.ct, tile.ms, tile.palette);
283
glEnable(GL_TEXTURE_2D);
284
if (tile.hiresBuffer)
285
glBindTexture(GL_TEXTURE_2D, tile.hiresBuffer->texid);
286
else
287
glBindTexture(GL_TEXTURE_2D, tile.tex->id);
288
for (j=0; j<2; j++) {
289
glBegin(GL_TRIANGLE_STRIP);
290
glTexCoord2f(0, 0); glVertex2f(x, y);
291
glTexCoord2f(0, 1); glVertex2f(x, y-h);
292
glTexCoord2f(1, 0); glVertex2f(x+w, y);
293
glTexCoord2f(1, 1); glVertex2f(x+w, y-h);
294
glEnd();
295
rglUseShader(alphaShader);
296
x += w+2;
297
}
298
rglUseShader(0);
299
// if ((tile.w+2)*2 < 256)
300
// x += 256 - (tile.w+2)*2;
301
x = oldx;
302
y -= h + 5*FS + 5;
303
}
304
305
y = oldy;
306
x = 128+210;
307
308
y -= FS;
309
gglPrintf(x, y, "cycle %d persp %d detail %d sharpen %d tex_lod %d en_tlut %d tlut_type %d clipm %d",
310
RDP_GETOM_CYCLE_TYPE(om),
311
RDP_GETOM_PERSP_TEX_EN(om),
312
RDP_GETOM_DETAIL_TEX_EN(om),
313
RDP_GETOM_SHARPEN_TEX_EN(om),
314
RDP_GETOM_TEX_LOD_EN(om),
315
RDP_GETOM_EN_TLUT(om),
316
RDP_GETOM_TLUT_TYPE(om),
317
chunk.rdpState.clipMode);
318
319
y -= FS;
320
gglPrintf(x, y, "sample_type %d mid %d lerp0 %d lerp1 %d convert1 %d key_en %d rgb_dith_sel %d",
321
RDP_GETOM_SAMPLE_TYPE(om),
322
RDP_GETOM_MID_TEXEL(om),
323
RDP_GETOM_BI_LERP0(om),
324
RDP_GETOM_BI_LERP1(om),
325
RDP_GETOM_CONVERT_ONE(om),
326
RDP_GETOM_KEY_EN(om),
327
RDP_GETOM_RGB_DITHER_SEL(om));
328
329
y -= FS;
330
gglPrintf(x, y, "A_dith_sel %d force_blend %d A_cvg_sel %d cvgXA %d Zmode %d cvg_dest %d col_on %d",
331
RDP_GETOM_ALPHA_DITHER_SEL(om),
332
RDP_GETOM_FORCE_BLEND(om),
333
RDP_GETOM_ALPHA_CVG_SELECT(om),
334
RDP_GETOM_CVG_TIMES_ALPHA(om),
335
RDP_GETOM_Z_MODE(om),
336
RDP_GETOM_CVG_DEST(om),
337
RDP_GETOM_COLOR_ON_CVG(om));
338
339
y -= FS;
340
gglPrintf(x, y, "img_read %d Zupdate %d Zcmp_sel %d antialias %d Zsource %d dith_A_en %d A_cmp %d",
341
RDP_GETOM_IMAGE_READ_EN(om),
342
RDP_GETOM_Z_UPDATE_EN(om),
343
RDP_GETOM_Z_COMPARE_EN(om),
344
RDP_GETOM_ANTIALIAS_EN(om),
345
RDP_GETOM_Z_SOURCE_SEL(om),
346
RDP_GETOM_DITHER_ALPHA_EN(om),
347
RDP_GETOM_ALPHA_COMPARE_EN(om));
348
349
y -= 2*FS;
350
351
if (cycle < 2) {
352
gglPrintf(x, y,
353
"c = [ (%s - %s) * %s + %s | (%s - %s) * %s + %s ];",
354
saRGBText[RDP_GETCM_SUB_A_RGB0(state.combineModes)],
355
saRGBText[RDP_GETCM_SUB_B_RGB0(state.combineModes)],
356
mRGBText[RDP_GETCM_MUL_RGB0(state.combineModes)],
357
aRGBText[RDP_GETCM_ADD_RGB0(state.combineModes)],
358
saAText[RDP_GETCM_SUB_A_A0(state.combineModes)],
359
sbAText[RDP_GETCM_SUB_B_A0(state.combineModes)],
360
mAText[RDP_GETCM_MUL_A0(state.combineModes)],
361
aAText[RDP_GETCM_ADD_A0(state.combineModes)]);
362
363
y -= FS;
364
}
365
if (cycle == 1) {
366
//if (cycle < 2) {
367
gglPrintf(x, y,
368
"c = [ (%s - %s) * %s + %s | (%s - %s) * %s + %s ];",
369
saRGBText[RDP_GETCM_SUB_A_RGB1(state.combineModes)],
370
saRGBText[RDP_GETCM_SUB_B_RGB1(state.combineModes)],
371
mRGBText[RDP_GETCM_MUL_RGB1(state.combineModes)],
372
aRGBText[RDP_GETCM_ADD_RGB1(state.combineModes)],
373
saAText[RDP_GETCM_SUB_A_A1(state.combineModes)],
374
sbAText[RDP_GETCM_SUB_B_A1(state.combineModes)],
375
mAText[RDP_GETCM_MUL_A1(state.combineModes)],
376
aAText[RDP_GETCM_ADD_A1(state.combineModes)]);
377
378
y -= FS;
379
}
380
if (cycle < 2) {
381
gglPrintf(x, y,
382
"%s*%s + %s*%s"
383
,bAText[0][RDP_GETOM_BLEND_M1B_0(state.otherModes)],
384
bRGBText[RDP_GETOM_BLEND_M1A_0(state.otherModes)],
385
bAText[1][RDP_GETOM_BLEND_M2B_0(state.otherModes)],
386
bRGBText[RDP_GETOM_BLEND_M2A_0(state.otherModes)]
387
);
388
389
y -= FS;
390
}
391
if (cycle == 1) {
392
//if (cycle < 2) {
393
gglPrintf(x, y,
394
"%s*%s + %s*%s"
395
,bAText[0][RDP_GETOM_BLEND_M1B_1(state.otherModes)],
396
bRGBText[RDP_GETOM_BLEND_M1A_1(state.otherModes)],
397
bAText[1][RDP_GETOM_BLEND_M2B_1(state.otherModes)],
398
bRGBText[RDP_GETOM_BLEND_M2A_1(state.otherModes)]
399
);
400
401
y -= FS;
402
}
403
404
if (chunk.nbStrips) {
405
y -= FS;
406
rglStrip_t & strip = chunk.strips[chunkindex >= 0? stripindex:0];
407
408
int i;
409
for (i=0; i<strip.nbVtxs; i++) {
410
rglVertex_t vtx = strip.vtxs[i];
411
int oldx;
412
gglPrintf(x, y, "%g %g %g %g", vtx.x, vtx.y, vtx.z, vtx.w);
413
x += 256;
414
if (strip.flags & RGL_STRIP_SHADE) {
415
gglPrintf(x, y, "%d %d %d %d", vtx.r, vtx.g, vtx.b, vtx.a);
416
x += 200;
417
}
418
if (strip.flags & (RGL_STRIP_TEX1|RGL_STRIP_TEX2)) {
419
gglPrintf(x, y, "%g %g", vtx.s, vtx.t);
420
x += 192;
421
}
422
y -= FS;
423
x = oldx;
424
}
425
}
426
427
// LOG("missing om %x %x (%x %x)\n",
428
// chunk.rdpState.otherModes.w1&RDP_OM_MISSING1,
429
// chunk.rdpState.otherModes.w2&RDP_OM_MISSING2,
430
// RDP_OM_MISSING1,
431
// RDP_OM_MISSING2);
432
// LOG("missing cm %x %x\n",
433
// chunk.rdpState.combineModes.w1&~(RDP_COMBINE_MASK11|RDP_COMBINE_MASK21),
434
// chunk.rdpState.combineModes.w2&~(RDP_COMBINE_MASK12|RDP_COMBINE_MASK22));
435
}
436
437
void rglDisplayFramebuffer(rglRenderBuffer_t & buffer, int alpha)
438
{
439
int i;
440
441
if (alpha)
442
rglUseShader(alphaShader);
443
else
444
rglUseShader(rglCopyShader);
445
glBindTexture(GL_TEXTURE_2D, buffer.texid);
446
glEnable(GL_TEXTURE_2D);
447
glDisable(GL_DEPTH_TEST);
448
glDisable(GL_BLEND);
449
glColor4ub(255, 255, 255, 255);
450
glBegin(GL_TRIANGLE_STRIP);
451
glTexCoord2f(1, 1); glVertex2f(1, 0);
452
glTexCoord2f(0, 1); glVertex2f(0, 0);
453
glTexCoord2f(1, 0); glVertex2f(1, 1);
454
glTexCoord2f(0, 0); glVertex2f(0, 1);
455
glEnd();
456
}
457
458
void rglDisplayFlat(rglRenderChunk_t & chunk)
459
{
460
int j;
461
rglRenderBuffer_t & buffer = *chunk.renderBuffer;
462
463
glPushAttrib(GL_ALL_ATTRIB_BITS);
464
//glEnable(GL_SCISSOR_TEST);
465
rglUseShader(0);
466
glDisable(GL_TEXTURE_2D);
467
glDisable(GL_CULL_FACE);
468
469
// glScissor((chunk.rdpState.clip.xh >>2)*buffer.realWidth/buffer.width,
470
// (chunk.rdpState.clip.yh >>2)*buffer.realHeight/buffer.height,
471
// (chunk.rdpState.clip.xl-chunk.rdpState.clip.xh >>2)*buffer.realWidth/buffer.width,
472
// (chunk.rdpState.clip.yl-chunk.rdpState.clip.yh >>2)*buffer.realHeight/buffer.height);
473
474
475
for (j=0; j<chunk.nbStrips; j++) {
476
rglStrip_t & strip = chunk.strips[j];
477
int k;
478
479
if (chunkindex >= 0 && j == stripindex) {
480
glPushAttrib(GL_ALL_ATTRIB_BITS);
481
glColor4ub(255, 255, 128, 255);
482
}
483
glBegin(GL_TRIANGLE_STRIP);
484
for (k=0; k<strip.nbVtxs; k++) {
485
glVertex2f((strip.vtxs[k].x/(buffer.width)),
486
1-(strip.vtxs[k].y/(buffer.height)));
487
}
488
glEnd();
489
if (chunkindex >= 0 && j == stripindex)
490
glPopAttrib();
491
}
492
493
glPopAttrib();
494
}
495
496
int rglFindStrip(rglRenderChunk_t & chunk, float mx, float my)
497
{
498
int j;
499
rglRenderBuffer_t & buffer = *chunk.renderBuffer;
500
for (j=chunk.nbStrips-1; j>=0; j--) {
501
rglStrip_t & strip = chunk.strips[j];
502
int k;
503
struct { float x, y; } s[3];
504
505
for (k=0; k<strip.nbVtxs; k++) {
506
s[0] = s[1];
507
s[1] = s[2];
508
s[2].x = strip.vtxs[k].x/(buffer.width);
509
s[2].y = 1 - strip.vtxs[k].y/(buffer.height);
510
if (k >= 2) {
511
float last = 0;
512
int i;
513
for (i=0; i<3; i++) {
514
float dx1 = s[(i+1)%3].x - s[i].x;
515
float dy1 = s[(i+1)%3].y - s[i].y;
516
float dx2 = mx - s[i].x;
517
float dy2 = my - s[i].y;
518
dx1 = dx1*dy2-dx2*dy1;
519
if (dx1 == 0) goto next;
520
if (last*dx1 < 0)
521
goto next;
522
last = dx1;
523
}
524
stripindex = j;
525
return j;
526
next:;
527
}
528
}
529
}
530
return -1;
531
}
532
533
void rglDisplayFlat(rglRenderBuffer_t & buffer)
534
{
535
int i;
536
for (i=0; i<nbChunks; i++) {
537
rglRenderChunk_t & chunk = chunks[i];
538
if (chunk.renderBuffer != &buffer) continue;
539
rglDisplayFlat(chunk);
540
}
541
}
542
543
int rglFindChunk(rglRenderBuffer_t & buffer, float mx, float my)
544
{
545
int i;
546
if (chunkindex <= 0)
547
i = nbChunks-1;
548
else
549
i = chunkindex-1;
550
for (; i>=0; i--) {
551
rglRenderChunk_t & chunk = chunks[i];
552
if (chunk.renderBuffer != &buffer) continue;
553
if (rglFindStrip(chunk, mx, my) >= 0)
554
return i;
555
}
556
return -1;
557
}
558
559
void rglShowCursor(int state)
560
{
561
#ifdef WIN32
562
#else
563
SDL_ShowCursor(state);
564
#endif
565
}
566
567
#ifndef WIN32
568
static int keys[512];
569
#define MOUSEBUT 511
570
#else
571
# define MOUSEBUT VK_LBUTTON
572
# define SDLK_ESCAPE VK_ESCAPE
573
# define SDLK_KP_PLUS VK_ADD
574
# define SDLK_KP_MINUS VK_SUBTRACT
575
# define SDLK_TAB VK_TAB
576
# define SDLK_UP VK_UP
577
# define SDLK_DOWN VK_DOWN
578
# define SDLK_PAGEUP VK_PRIOR
579
# define SDLK_PAGEDOWN VK_NEXT
580
#endif
581
582
int rglCheckKey(int key)
583
{
584
#ifdef WIN32
585
return GetAsyncKeyState (key) & 1;
586
#else
587
if (key >= 'A' && key <= 'Z') key += 'a' - 'A';
588
int res = keys[key];
589
keys[key] = 0;
590
return res;
591
#endif
592
}
593
594
void rglDebugger()
595
{
596
SDL_Event event;
597
int paused = 1;
598
int i, j;
599
int traceX = 1;
600
int tracepos = 0;
601
int tracepage = (screen_height*3/4)/(SMALLFS+2);
602
int oldchunkindex = -1;
603
604
fbindex = 0;
605
chunkindex = -1;
606
607
void rglInitDebugger();
608
rglInitDebugger();
609
610
rglShowCursor(SDL_ENABLE);
611
612
glActiveTextureARB(GL_TEXTURE1_ARB);
613
glDisable(GL_TEXTURE_2D);
614
glActiveTextureARB(GL_TEXTURE2_ARB);
615
glDisable(GL_TEXTURE_2D);
616
glActiveTextureARB(GL_TEXTURE0_ARB);
617
glDrawBuffer(GL_BACK);
618
619
for (i=nblines=0; i<=rdpTracePos; i += rdp_dasm(rdpTraceBuf, i, i+256, dasm)/4, nblines++)
620
lines[nblines] = i;
621
622
if (nbChunks > 1)
623
// skip chunk 0 as it's usually depth clear
624
fbindex = chunks[1].renderBuffer - rBuffers;
625
626
while (paused) {
627
#ifndef WIN32
628
int res = SDL_WaitEvent(&event);
629
while (res) {
630
switch (event.type) {
631
case SDL_MOUSEBUTTONDOWN:
632
keys[MOUSEBUT] = 1;
633
break;
634
case SDL_MOUSEBUTTONUP:
635
keys[MOUSEBUT] = 0;
636
break;
637
case SDL_KEYDOWN:
638
if (event.key.keysym.sym < MOUSEBUT)
639
keys[event.key.keysym.sym] = 1;
640
break;
641
case SDL_KEYUP:
642
if (event.key.keysym.sym < MOUSEBUT)
643
keys[event.key.keysym.sym] = 0;
644
break;
645
}
646
res = SDL_PollEvent(&event);
647
}
648
#endif
649
rglRenderBuffer_t & buffer = rBuffers[fbindex];
650
scalex = buffer.realWidth; scaley = buffer.realHeight;
651
652
if (rBuffers[fbindex].fbid) {
653
if (buffer.realWidth > scalex*3/4 ||
654
buffer.realHeight > scaley*3/4) {
655
scalex = scalex*3/4;
656
scaley = scaley*3/4;
657
}
658
}
659
660
if (rglCheckKey(MOUSEBUT)) {
661
if (buffer.fbid) {
662
#ifdef WIN32
663
POINT pt;
664
GetCursorPos(&pt);
665
mx = pt.x;
666
my = pt.y;
667
#else
668
SDL_GetMouseState(&mx, &my);
669
#endif
670
int old = chunkindex;
671
if (old >= 0)
672
chunkindex++;
673
chunkindex = rglFindChunk(buffer, float(mx)/scalex, float(screen_height - my)/scaley);
674
if (old >= 0 && chunkindex == old) {
675
} else {
676
chunkindex = -1;
677
}
678
chunkindex = rglFindChunk(buffer, float(mx)/scalex, float(screen_height - my)/scaley);
679
if (chunkindex >= 0 && nbChunks)
680
printf("%s\n", chunks[chunkindex].shader->fsrc);
681
}
682
}
683
if (rglCheckKey('P') || rglCheckKey(SDLK_ESCAPE))
684
paused = 0;
685
if (rglCheckKey(SDLK_KP_PLUS)) {
686
if (fbindex < MAX_RENDER_BUFFERS-1/* &&
687
rBuffers[fbindex+1].fbid*/)
688
fbindex++;
689
chunkindex = -1;
690
}
691
if (rglCheckKey(SDLK_KP_MINUS)) {
692
if (fbindex > 0/* &&
693
rBuffers[fbindex-1].fbid*/)
694
fbindex--;
695
chunkindex = -1;
696
}
697
if (rglCheckKey(SDLK_TAB))
698
traceX = !traceX;
699
if (rglCheckKey(SDLK_UP))
700
tracepos--;
701
if (rglCheckKey(SDLK_DOWN))
702
tracepos++;
703
if (rglCheckKey(SDLK_PAGEUP))
704
tracepos -= tracepage/2;
705
if (rglCheckKey(SDLK_PAGEDOWN))
706
tracepos += tracepage/2;
707
if (tracepos < 0)
708
tracepos = 0;
709
if (tracepos > nblines-tracepage/2)
710
tracepos = nblines-tracepage/2;
711
712
//rglRenderChunks();
713
714
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
715
glDrawBuffer(GL_BACK);
716
glDisable(GL_SCISSOR_TEST);
717
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
718
glActiveTextureARB(GL_TEXTURE1_ARB);
719
glDisable(GL_TEXTURE_2D);
720
glActiveTextureARB(GL_TEXTURE0_ARB);
721
glDisable(GL_ALPHA_TEST);
722
723
glClearColor(0, 0, 0, 0);
724
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
725
glClear(GL_COLOR_BUFFER_BIT);
726
727
if (buffer.fbid) {
728
//glViewport(0, 0, scalex*screen_width/buffer.realWidth, scaley*screen_height/buffer.realHeight);
729
glViewport(0, 0, scalex, scaley);
730
rglDisplayFramebuffer(buffer, 0);
731
glViewport(scalex, 0, scalex, scaley);
732
rglDisplayFramebuffer(buffer, 1);
733
734
glViewport(0, 0, scalex, scaley);
735
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
736
glEnable(GL_BLEND);
737
//glBlendFunc( GL_ONE, GL_ONE );
738
glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_DST_COLOR);
739
if (chunkindex < 0) {
740
//glColor4f(0.1, 0, 0.1, 0.5);
741
glColor4f(0.6, 0, 0.6, 0.5);
742
rglDisplayFlat(buffer);
743
} else {
744
glColor4f(0.6, 0, 0.6, 0.5);
745
rglDisplayFlat(chunks[chunkindex]);
746
}
747
}
748
749
{
750
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
751
glDisable(GL_BLEND);
752
753
754
glMatrixMode( GL_PROJECTION);
755
glPushMatrix();
756
glLoadIdentity();
757
gluOrtho2D(0, screen_width, 0, screen_height);
758
glMatrixMode(GL_MODELVIEW);
759
glPushMatrix();
760
glLoadIdentity();
761
762
glViewport(0, 0, screen_width, screen_height);
763
rglUseShader(0);
764
765
glColor3f(1,0.5,0.5);
766
gglPrintf(0, 0, "Fb #%d at $%x --> %x (%dx%d fmt %s-%d) upto %d %s", fbindex,
767
buffer.addressStart, buffer.addressStop,
768
buffer.width, buffer.height,
769
(buffer.flags & RGL_RB_DEPTH)? "Z":rdpImageFormats[buffer.format], buffer.size,
770
buffer.chunkId,
771
(buffer.flags & RGL_RB_ERASED)? "ERASED":"");
772
773
if (chunkindex >= 0) {
774
gglPrintf(0, FS, "Chunk #%d", chunkindex);
775
776
rglDisplayChunkInfo(chunks[chunkindex]);
777
}
778
779
if (oldchunkindex != chunkindex) {
780
oldchunkindex = chunkindex;
781
if (chunkindex >= 0)
782
for (i=0; i<nblines; i++)
783
if (lines[i] == chunks[chunkindex].tracePos)
784
tracepos = i;
785
}
786
rglDisplayTrace(traceX*scalex, screen_height*3/4, tracepos, tracepage);
787
788
glMatrixMode( GL_PROJECTION);
789
glPopMatrix();
790
glMatrixMode(GL_MODELVIEW);
791
glPopMatrix();
792
}
793
794
rglSwapBuffers();
795
}
796
797
rglShowCursor(SDL_DISABLE);
798
}
799
800
void rglCloseDebugger()
801
{
802
if (font) {
803
delete font;
804
font = 0;
805
}
806
if (smallfont) {
807
delete smallfont;
808
smallfont = 0;
809
}
810
if (alphaShader) {
811
rglDeleteShader(alphaShader);
812
alphaShader = 0;
813
}
814
}
815
816
void rglInitDebugger()
817
{
818
if (!font) {
819
char s[1024];
820
extern char rgl_cwd[512];
821
sprintf(s, "%s/"FONT, rgl_cwd);
822
curfont = font = new FTGLTextureFont(s);
823
sprintf(s, "%s/"SMALLFONT, rgl_cwd);
824
smallfont = new FTGLTextureFont(s);
825
if (!font || !smallfont) {
826
LOGERROR("Couldn't load font '%s'\n", s);
827
return;
828
}
829
font->FaceSize(FS);
830
smallfont->FaceSize(SMALLFS);
831
}
832
833
if (!alphaShader) {
834
alphaShader = rglCreateShader(
835
"void main() \n"
836
"{ \n"
837
" gl_Position = ftransform(); \n"
838
" gl_FrontColor = gl_Color; \n"
839
" gl_TexCoord[0] = gl_MultiTexCoord0; \n"
840
"} \n"
841
,
842
"uniform sampler2D texture0; \n"
843
" \n"
844
"void main() \n"
845
"{ \n"
846
" gl_FragColor = gl_Color * texture2D(texture0, vec2(gl_TexCoord[0])).a; \n"
847
"} \n"
848
);
849
}
850
}
851
852
void rdpBacktrace()
853
{
854
int i=0;
855
while (i <= rdpTracePos) {
856
i += rdp_dasm(rdpTraceBuf, i, i+256, dasm)/4;
857
printf("%4x %s\n", i, dasm);
858
}
859
}
860
861
#endif
862
863