-
Notifications
You must be signed in to change notification settings - Fork 475
/
OGLglitchmain.cpp
2051 lines (1838 loc) · 73.8 KB
/
OGLglitchmain.cpp
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* *
* Project64-video - A Nintendo 64 gfx plugin. *
* http://www.pj64-emu.com/ *
* Copyright (C) 2017 Project64. All rights reserved. *
* Copyright (C) 2003-2009 Sergey 'Gonetz' Lipski *
* Copyright (C) 2002 Dave2001 *
* *
* License: *
* GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html *
* version 2 of the License, or (at your option) any later version. *
* *
****************************************************************************/
#include <Project64-video/Renderer/Renderer.h>
#define SAVE_CBUFFER
#ifdef _WIN32
#include <windows.h>
#else
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#endif // _WIN32
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <math.h>
#include "glide.h"
#include "g3ext.h"
#include "glitchmain.h"
#include <Project64-video/trace.h>
#include <Project64-video/Settings.h>
#include <Common/Util.h>
/*
* `GetSystemSetting` and `FindSystemSettingId` from Project64 debugger
* used only in g_Notify->DisplayError when OpenGL extension loading fails on WGL
*/
#include <Settings/Settings.h>
int screen_width, screen_height;
static inline void opt_glCopyTexImage2D(GLenum target,
GLint level,
GLint internalFormat,
GLint x,
GLint y,
GLsizei width,
GLsizei height,
GLint border)
{
int w, h, fmt;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
//printf("copyteximage %dx%d fmt %x oldfmt %x\n", width, height, internalFormat, fmt);
if (w == width && h == height && fmt == internalFormat) {
if (x + width >= screen_width) {
width = screen_width - x;
//printf("resizing w --> %d\n", width);
}
if (y + height >= screen_height + g_viewport_offset) {
height = screen_height + g_viewport_offset - y;
//printf("resizing h --> %d\n", height);
}
glCopyTexSubImage2D(target, level, 0, 0, x, y, width, height);
}
else {
//printf("copyteximage %dx%d fmt %x old %dx%d oldfmt %x\n", width, height, internalFormat, w, h, fmt);
// glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, internalFormat, GL_UNSIGNED_BYTE, 0);
// glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
// printf("--> %dx%d newfmt %x\n", width, height, fmt);
glCopyTexImage2D(target, level, internalFormat, x, y, width, height, border);
}
grDisplayGLError("opt_glCopyTexImage2D");
}
#define glCopyTexImage2D opt_glCopyTexImage2D
#ifdef _WIN32
/*
* Some post-1.1 OpenGL functions can fail to be loaded through GL extensions
* when running primitive OpenGL contexts on Microsoft Windows, specifically.
*
* As of the Project64 Glide64 version, Glitch64 now assigns these GL
* functions to dummy functions to prevent access violations, while also
* displaying error information showing the missing OpenGL support.
*/
PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
PFNGLBLENDFUNCSEPARATEEXTPROC glBlendFuncSeparateEXT;
PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB;
PFNGLFOGCOORDFPROC glFogCoordfEXT;
void APIENTRY dummy_glActiveTexture(GLenum/*texture*/)
{ /* GLX render opcode 197, req. OpenGL 1.3 (1.2 w/ ARB_multitexture) */
g_Notify->DisplayError("glActiveTexture");
}
void APIENTRY dummy_glMultiTexCoord2f(GLenum/*target*/, GLfloat/*s*/, GLfloat/*t*/)
{ /* GLX render opcode 203, req. OpenGL 1.3 (1.2 w/ ARB_multitexture) */
g_Notify->DisplayError("glMultiTexCoord2f");
}
void APIENTRY dummy_glFogCoordf(GLfloat/*coord*/)
{ /* GLX render opcode 4124, req. OpenGL 1.4 (1.1 w/ EXT_fog_coord) */
g_Notify->DisplayError("glFogCoordf");
}
void APIENTRY dummy_glBlendFuncSeparate(GLenum, GLenum, GLenum, GLenum)
{ /* GLX render opcode 4134, req. OpenGL 1.0 w/ EXT_blend_func_separate */
g_Notify->DisplayError("glBlendFuncSeparate");
}
PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB;
const char * APIENTRY dummy_wglGetExtensionsString(HDC)
{
g_Notify->DisplayError("wglGetExtensionsString");
return NULL;
}
PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT;
PFNGLFRAMEBUFFERTEXTURE2DEXTPROC glFramebufferTexture2DEXT;
PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT;
PFNGLBINDRENDERBUFFEREXTPROC glBindRenderbufferEXT = NULL;
PFNGLDELETERENDERBUFFERSEXTPROC glDeleteRenderbuffersEXT = NULL;
PFNGLGENRENDERBUFFERSEXTPROC glGenRenderbuffersEXT = NULL;
PFNGLRENDERBUFFERSTORAGEEXTPROC glRenderbufferStorageEXT = NULL;
PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC glFramebufferRenderbufferEXT = NULL;
PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC glCheckFramebufferStatusEXT;
PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT;
void APIENTRY dummy_glGenRenderbuffers(GLsizei/*n*/, GLuint* /*renderbuffers*/)
{ /* GLX vendor opcode 1423, req. OpenGL 1.2 w/ EXT_framebuffer_object */
g_Notify->DisplayError("glGenRenderbuffers");
}
void APIENTRY dummy_glGenFramebuffers(GLsizei/*n*/, GLuint* /*framebuffers*/)
{ /* GLX vendor opcode 1426, req. OpenGL 1.2 w/ EXT_framebuffer_object */
g_Notify->DisplayError("glGenFramebuffers");
}
GLenum APIENTRY dummy_glCheckFramebufferStatus(GLenum/*target*/)
{ /* GLX vendor opcode 1427, req. OpenGL 1.2 w/ EXT_framebuffer_object */
g_Notify->DisplayError("glCheckFramebufferStatus");
return 0x00008CDD; /* GL_FRAMEBUFFER_UNSUPPORTED */
}
void APIENTRY dummy_glBindRenderbuffer(GLenum/*target*/, GLuint/*renderbuffer*/)
{ /* GLX render opcode 4316, req. OpenGL 1.2 w/ EXT_framebuffer_object */
g_Notify->DisplayError("glBindRenderbuffer");
}
void APIENTRY dummy_glDeleteRenderbuffers(GLsizei/*n*/, const GLuint* /*renderbuffers*/)
{ /* GLX render opcode 4317, req. OpenGL 1.2 w/ EXT_framebuffer_object */
g_Notify->DisplayError("glDeleteRenderbuffers");
}
void APIENTRY dummy_glRenderbufferStorage(GLenum, GLenum, GLsizei, GLsizei)
{ /* GLX render opcode 4318, req. OpenGL 1.2 w/ EXT_framebuffer_object */
g_Notify->DisplayError("glRenderbufferStorage");
}
void APIENTRY dummy_glBindFramebuffer(GLenum/*target*/, GLuint/*framebuffer*/)
{ /* GLX render opcode 4319, req. OpenGL 1.2 w/ EXT_framebuffer_object */
g_Notify->DisplayError("glBindFramebuffer");
}
void APIENTRY dummy_glDeleteFramebuffers(GLsizei/*n*/, const GLuint* /*framebuffers*/)
{ /* GLX render opcode 4320, req. OpenGL 1.2 w/ EXT_framebuffer_object */
g_Notify->DisplayError("glDeleteFramebuffers");
}
void APIENTRY dummy_glFramebufferTexture2D(GLenum, GLenum, GLenum, GLuint, GLint)
{ /* GLX render opcode 4322, req. OpenGL 1.2 w/ EXT_framebuffer_object */
g_Notify->DisplayError("glFramebufferTexture2D");
}
void APIENTRY dummy_glFramebufferRenderbuffer(GLenum, GLenum, GLenum, GLuint)
{ /* GLX render opcode 4324, req. OpenGL 1.2 w/ EXT_framebuffer_object */
g_Notify->DisplayError("glFramebufferRenderbuffer");
}
PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
PFNGLUNIFORM1IARBPROC glUniform1iARB;
PFNGLUNIFORM4IARBPROC glUniform4iARB;
PFNGLUNIFORM4FARBPROC glUniform4fARB;
PFNGLUNIFORM1FARBPROC glUniform1fARB;
PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
PFNGLSECONDARYCOLOR3FPROC glSecondaryColor3f;
void APIENTRY dummy_glSecondaryColor3f(GLfloat/*red*/, GLfloat/*green*/, GLfloat/*blue*/)
{ /* GLX render opcode 4129, req. OpenGL 1.4 (1.1 w/ EXT_secondary_color) */
g_Notify->DisplayError("glSecondaryColor3f");
}
GLuint APIENTRY dummy_glCreateShader(GLenum/*type*/)
{ /* GLX render opcode ?, req. OpenGL 2.0 (1.2 w/ ARB_shader_objects) */
g_Notify->DisplayError("glCreateShader");
return ((GLuint)(NULL));
}
void APIENTRY dummy_glShaderSource(GLuint, GLsizei, const GLchar **, GLint *)
{ /* GLX render opcode ?, req. OpenGL 2.0 (1.2 w/ ARB_shader_objects) */
g_Notify->DisplayError("glShaderSource");
}
void APIENTRY dummy_glCompileShader(GLuint/*shader*/)
{ /* GLX render opcode ?, req. OpenGL 2.0 (1.2 w/ ARB_shader_objects) */
g_Notify->DisplayError("glCompileShader");
}
GLuint APIENTRY dummy_glCreateProgram(void)
{ /* GLX render opcode ?, req. OpenGL 2.0 (1.2 w/ ARB_shader_objects) */
g_Notify->DisplayError("glCreateProgram");
return ((GLuint)(NULL));
}
void APIENTRY dummy_glAttachObject(GLhandleARB, GLhandleARB)
{ /* GLX render opcode ?, req. OpenGL 2.0 (1.2 w/ ARB_shader_objects) */
g_Notify->DisplayError("glAttachObject");
}
void APIENTRY dummy_glLinkProgram(GLuint/*program*/)
{ /* GLX render opcode ?, req. OpenGL 2.0 (1.2 w/ ARB_shader_objects) */
g_Notify->DisplayError("glLinkProgram");
}
void APIENTRY dummy_glUseProgram(GLuint/*program*/)
{ /* GLX render opcode ?, req. OpenGL 2.0 (1.2 w/ ARB_shader_objects) */
g_Notify->DisplayError("glUseProgram");
}
GLint APIENTRY dummy_glGetUniformLocation(GLuint/*program*/, GLchar* /*name*/)
{ /* GLX single opcode ?, req. OpenGL 2.0 (1.2 w/ ARB_shader_objects) */
g_Notify->DisplayError("glGetUniformLocation");
return -1;
}
void APIENTRY dummy_glUniform1i(GLint/*location*/, GLint/*v0*/)
{ /* GLX render opcode ?, req. OpenGL 2.0 (1.2 w/ ARB_shader_objects) */
g_Notify->DisplayError("glUniform1i");
}
void APIENTRY dummy_glUniform4i(GLint/*location*/, GLint, GLint, GLint, GLint)
{ /* GLX render opcode ?, req. OpenGL 2.0 (1.2 w/ ARB_shader_objects) */
g_Notify->DisplayError("glUniform4i");
}
void APIENTRY dummy_glUniform1f(GLint/*location*/, GLfloat/*v0*/)
{ /* GLX render opcode ?, req. OpenGL 2.0 (1.2 w/ ARB_shader_objects) */
g_Notify->DisplayError("glUniform1f");
}
void APIENTRY dummy_glUniform4f(GLint/*location*/, GLfloat, GLfloat, GLfloat, GLfloat)
{ /* GLX render opcode ?, req. OpenGL 2.0 (1.2 w/ ARB_shader_objects) */
g_Notify->DisplayError("glUniform4f");
}
void APIENTRY dummy_glDeleteObject(GLhandleARB/*obj*/)
{ /* GLX render opcode ?, req. OpenGL 1.2 w/ ARB_shader_objects */
g_Notify->DisplayError("glDeleteObject");
}
void APIENTRY dummy_glGetInfoLog(GLhandleARB, GLsizei, GLsizei *, GLcharARB *)
{ /* GLX single opcode ?, req. OpenGL 1.2 w/ ARB_shader_objects */
g_Notify->DisplayError("glGetInfoLog");
}
void APIENTRY dummy_glGetObjectParameteriv(GLhandleARB, GLenum, GLint *)
{ /* GLX single opcode ?, req. OpenGL 1.2 w/ ARB_shader_objects */
g_Notify->DisplayError("glGetObjectParameteriv");
}
// FXT1,DXT1,DXT5 support - Hiroshi Morii <koolsmoky(at)users.sourceforge.net>
// NOTE: Glide64 + GlideHQ use the following formats
// GL_COMPRESSED_RGB_S3TC_DXT1_EXT
// GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
// GL_COMPRESSED_RGB_FXT1_3DFX
// GL_COMPRESSED_RGBA_FXT1_3DFX
PFNGLCOMPRESSEDTEXIMAGE2DPROC glCompressedTexImage2DARB;
void APIENTRY dummy_glCompressedTexImage2D(GLenum, GLint, GLenum, GLsizei, GLsizei, GLint, GLsizei, const GLvoid *)
{ /* GLX render opcode 215, req. OpenGL 1.3 (1.2 w/ ARB_texture_compression) */
g_Notify->DisplayError("glCompressedTexImage2D");
}
#endif // _WIN32
typedef struct
{
unsigned int address;
int width;
int height;
unsigned int fbid;
unsigned int zbid;
unsigned int texid;
int buff_clear;
} fb;
int nbAuxBuffers, current_buffer;
int g_width, widtho, heighto, g_height;
int saved_width, saved_height;
int blend_func_separate_support;
int npot_support;
int fog_coord_support;
int render_to_texture = 0;
int texture_unit;
int use_fbo;
int buffer_cleared;
// ZIGGY
// to allocate a new static texture name, take the value (free_texture++)
int free_texture;
int default_texture; // the infamous "32*1024*1024" is now configurable
int current_texture;
int depth_texture, color_texture;
int glsl_support = 1;
int viewport_width, viewport_height, g_viewport_offset = 0, nvidia_viewport_hack = 0;
int save_w, save_h;
int lfb_color_fmt;
float invtex[2];
#ifdef _WIN32
static HDC hDC = NULL;
static HGLRC hGLRC = NULL;
static HWND hToolBar = NULL;
#endif // _WIN32
static int savedWidtho, savedHeighto;
static int savedWidth, savedHeight;
unsigned int pBufferAddress;
static int pBufferFmt;
static int pBufferWidth, pBufferHeight;
static fb fbs[100];
static int nb_fb = 0;
static unsigned int curBufferAddr = 0;
struct TMU_USAGE { unsigned long min, max; } tmu_usage[2] = {
{ 0x0FFFFFFFul, 0x00000000ul },
{ 0x0FFFFFFFul, 0x00000000ul },
};
struct texbuf_t {
uint32_t start, end;
int fmt;
};
#define NB_TEXBUFS 128 // MUST be a power of two
static texbuf_t texbufs[NB_TEXBUFS];
static int texbuf_i;
unsigned short frameBuffer[2048 * 2048];
unsigned short depthBuffer[2048 * 2048];
//#define VOODOO1
void gfxClipWindow(uint32_t minx, uint32_t miny, uint32_t maxx, uint32_t maxy)
{
WriteTrace(TraceGlitch, TraceDebug, "minx = %d, miny: %d maxy: %d", minx, miny, maxy);
if (use_fbo && render_to_texture)
{
if (int(minx) < 0) minx = 0;
if (int(miny) < 0) miny = 0;
if (maxx < minx) maxx = minx;
if (maxy < miny) maxy = miny;
glScissor(minx, miny, maxx - minx, maxy - miny);
glEnable(GL_SCISSOR_TEST);
grDisplayGLError("gfxClipWindow :: use_fbo");
return;
}
if (!use_fbo) {
int th = g_height;
if (th > screen_height)
th = screen_height;
maxy = th - maxy;
miny = th - miny;
uint32_t tmp = maxy; maxy = miny; miny = tmp;
if ((FxI32)maxx > g_width) maxx = g_width;
if ((FxI32)maxy > g_height) maxy = g_height;
if (int(minx) < 0) minx = 0;
if (int(miny) < 0) miny = 0;
if (maxx < minx) maxx = minx;
if (maxy < miny) maxy = miny;
glScissor(minx, miny + g_viewport_offset, maxx - minx, maxy - miny);
//printf("gl scissor %d %d %d %d\n", minx, miny, maxx, maxy);
}
else
{
glScissor(minx, (g_viewport_offset)+g_height - maxy, maxx - minx, maxy - miny);
}
glEnable(GL_SCISSOR_TEST);
grDisplayGLError("gfxClipWindow");
}
void gfxColorMask(bool rgb, bool a)
{
WriteTrace(TraceGlitch, TraceDebug, "rgb = %d, a: %d", rgb, a);
glColorMask((GLboolean)rgb, (GLboolean)rgb, (GLboolean)rgb, (GLboolean)a);
grDisplayGLError("gfxColorMask");
}
int isExtensionSupported(const char *extension)
{
const GLubyte *extensions = NULL;
const GLubyte *start;
GLubyte *where, *terminator;
where = (GLubyte *)strchr(extension, ' ');
if (where || *extension == '\0')
return 0;
extensions = glGetString(GL_EXTENSIONS);
start = extensions;
for (;;)
{
where = (GLubyte *)strstr((const char *)start, extension);
if (!where)
break;
terminator = where + strlen(extension);
if (where == start || *(where - 1) == ' ')
if (*terminator == ' ' || *terminator == '\0')
return 1;
start = terminator;
}
return 0;
}
#ifdef _WIN32
int isWglExtensionSupported(const char *extension)
{
const GLubyte *extensions = NULL;
const GLubyte *start;
GLubyte *where, *terminator;
where = (GLubyte *)strchr(extension, ' ');
if (where || *extension == '\0')
return 0;
extensions = (GLubyte*)wglGetExtensionsStringARB(wglGetCurrentDC());
start = extensions;
for (;;)
{
where = (GLubyte *)strstr((const char *)start, extension);
if (!where)
break;
terminator = where + strlen(extension);
if (where == start || *(where - 1) == ' ')
if (*terminator == ' ' || *terminator == '\0')
return 1;
start = terminator;
}
return 0;
}
#endif // _WIN32
#define GrPixelFormat_t int
#ifdef _WIN32
# include <fcntl.h>
# ifndef ATTACH_PARENT_PROCESS
# define ATTACH_PARENT_PROCESS ((uint32_t)-1)
# endif
extern HWND g_hwnd_win;
#endif
bool gfxSstWinOpen(gfxColorFormat_t color_format, gfxOriginLocation_t origin_location, int nColBuffers, int nAuxBuffers)
{
static int show_warning = 1;
// ZIGGY
// allocate static texture names
// the initial value should be big enough to support the maximal resolution
free_texture = 32 * 2048 * 2048;
default_texture = free_texture++;
color_texture = free_texture++;
depth_texture = free_texture++;
#ifdef _WIN32
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_GENERIC_ACCELERATED | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.cColorBits = 32;
pfd.cDepthBits = 24;
pfd.cAuxBuffers = 1;
int pfm;
#else
fputs("ERROR: No GLX yet to start GL on [Free]BSD, Linux etc.\n", stderr);
#endif // _WIN32
WriteTrace(TraceGlitch, TraceDebug, "color_format: %d, origin_location: %d, nColBuffers: %d, nAuxBuffers: %d", color_format, origin_location, nColBuffers, nAuxBuffers);
#ifdef _WIN32
TMU_SIZE = ((g_settings->wrpVRAM() * 1024 * 1024) - g_width * g_height * 4 * 3) / 2;
// save screen resolution for hwfbe, after resolution enumeration
screen_width = g_width;
screen_height = g_height;
if ((hDC = GetDC(g_hwnd_win)) == NULL)
{
WriteTrace(TraceGlitch, TraceWarning, "GetDC on main window failed");
return FXFALSE;
}
if ((pfm = ChoosePixelFormat(hDC, &pfd)) == 0) {
//printf("disabling auxiliary buffers\n");
pfd.cAuxBuffers = 0;
pfm = ChoosePixelFormat(hDC, &pfd);
}
if (pfm == 0)
{
WriteTrace(TraceGlitch, TraceWarning, "ChoosePixelFormat failed");
return FXFALSE;
}
if (SetPixelFormat(hDC, pfm, &pfd) == 0)
{
WriteTrace(TraceGlitch, TraceWarning, "SetPixelFormat failed");
return FXFALSE;
}
if ((hGLRC = wglCreateContext(hDC)) == 0)
{
WriteTrace(TraceGlitch, TraceWarning, "wglCreateContext failed!");
gfxSstWinClose();
return FXFALSE;
}
HGLRC CurrenthGLRC = wglGetCurrentContext();
if (CurrenthGLRC == NULL || CurrenthGLRC == hGLRC)
{
if (!wglMakeCurrent(hDC, hGLRC))
{
WriteTrace(TraceGlitch, TraceWarning, "wglMakeCurrent failed!");
gfxSstWinClose();
return FXFALSE;
}
}
#endif // _WIN32
lfb_color_fmt = color_format;
if (origin_location != GFX_ORIGIN_UPPER_LEFT) WriteTrace(TraceGlitch, TraceWarning, "origin must be in upper left corner");
if (nColBuffers != 2) WriteTrace(TraceGlitch, TraceWarning, "number of color buffer is not 2");
if (nAuxBuffers != 1) WriteTrace(TraceGlitch, TraceWarning, "number of auxiliary buffer is not 1");
if (isExtensionSupported("GL_ARB_texture_env_combine") == 0 &&
isExtensionSupported("GL_EXT_texture_env_combine") == 0 &&
show_warning)
WriteTrace(TraceGlitch, TraceWarning, "Your video card doesn't support GL_ARB_texture_env_combine extension");
if (isExtensionSupported("GL_ARB_multitexture") == 0 && show_warning)
WriteTrace(TraceGlitch, TraceWarning, "Your video card doesn't support GL_ARB_multitexture extension");
if (isExtensionSupported("GL_ARB_texture_mirrored_repeat") == 0 && show_warning)
WriteTrace(TraceGlitch, TraceWarning, "Your video card doesn't support GL_ARB_texture_mirrored_repeat extension");
show_warning = 0;
#ifdef _WIN32
glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC)wglGetProcAddress("glActiveTextureARB");
glMultiTexCoord2fARB = (PFNGLMULTITEXCOORD2FARBPROC)wglGetProcAddress("glMultiTexCoord2fARB");
if (glActiveTextureARB == NULL)
glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC)dummy_glActiveTexture;
if (glMultiTexCoord2fARB == NULL)
glMultiTexCoord2fARB = (PFNGLMULTITEXCOORD2FARBPROC)dummy_glMultiTexCoord2f;
#endif // _WIN32
nbTextureUnits = 0;
glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, (GLint *)&nbTextureUnits);
if (nbTextureUnits == 1) WriteTrace(TraceGlitch, TraceWarning, "You need a video card that has at least 2 texture units");
nbAuxBuffers = 0;
glGetIntegerv(GL_AUX_BUFFERS, &nbAuxBuffers);
if (nbAuxBuffers > 0)
printf("Congratulations, you have %d auxilliary buffers, we'll use them wisely !\n", nbAuxBuffers);
#ifdef VOODOO1
nbTextureUnits = 2;
#endif
if (isExtensionSupported("GL_EXT_blend_func_separate") == 0)
blend_func_separate_support = 0;
else
blend_func_separate_support = 1;
if (isExtensionSupported("GL_EXT_packed_pixels") == 0)
packed_pixels_support = 0;
else {
printf("packed pixels extension used\n");
packed_pixels_support = 1;
}
if (isExtensionSupported("GL_ARB_texture_non_power_of_two") == 0)
npot_support = 0;
else {
printf("NPOT extension used\n");
npot_support = 1;
}
#ifdef _WIN32
glBlendFuncSeparateEXT = (PFNGLBLENDFUNCSEPARATEEXTPROC)wglGetProcAddress("glBlendFuncSeparateEXT");
if (glBlendFuncSeparateEXT == NULL)
glBlendFuncSeparateEXT = (PFNGLBLENDFUNCSEPARATEEXTPROC)dummy_glBlendFuncSeparate;
#endif // _WIN32
if (isExtensionSupported("GL_EXT_fog_coord") == 0)
fog_coord_support = 0;
else
fog_coord_support = 1;
#ifdef _WIN32
glFogCoordfEXT = (PFNGLFOGCOORDFPROC)wglGetProcAddress("glFogCoordfEXT");
if (glFogCoordfEXT == NULL)
glFogCoordfEXT = (PFNGLFOGCOORDFPROC)dummy_glFogCoordf;
#endif // _WIN32
#ifdef _WIN32
wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
if (wglGetExtensionsStringARB == NULL)
wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)dummy_wglGetExtensionsString;
#endif // _WIN32
#ifdef _WIN32
glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)wglGetProcAddress("glBindFramebufferEXT");
glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)wglGetProcAddress("glFramebufferTexture2DEXT");
glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)wglGetProcAddress("glGenFramebuffersEXT");
glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)wglGetProcAddress("glCheckFramebufferStatusEXT");
glDeleteFramebuffersEXT = (PFNGLDELETEFRAMEBUFFERSEXTPROC)wglGetProcAddress("glDeleteFramebuffersEXT");
if (glBindFramebufferEXT == NULL)
glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)dummy_glBindFramebuffer;
if (glFramebufferTexture2DEXT == NULL)
glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)dummy_glFramebufferTexture2D;
if (glGenFramebuffersEXT == NULL)
glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)dummy_glGenFramebuffers;
if (glCheckFramebufferStatusEXT == NULL)
glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)dummy_glCheckFramebufferStatus;
if (glDeleteFramebuffersEXT == NULL)
glDeleteFramebuffersEXT = (PFNGLDELETEFRAMEBUFFERSEXTPROC)dummy_glDeleteFramebuffers;
glBindRenderbufferEXT = (PFNGLBINDRENDERBUFFEREXTPROC)wglGetProcAddress("glBindRenderbufferEXT");
glDeleteRenderbuffersEXT = (PFNGLDELETERENDERBUFFERSEXTPROC)wglGetProcAddress("glDeleteRenderbuffersEXT");
glGenRenderbuffersEXT = (PFNGLGENRENDERBUFFERSEXTPROC)wglGetProcAddress("glGenRenderbuffersEXT");
glRenderbufferStorageEXT = (PFNGLRENDERBUFFERSTORAGEEXTPROC)wglGetProcAddress("glRenderbufferStorageEXT");
glFramebufferRenderbufferEXT = (PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC)wglGetProcAddress("glFramebufferRenderbufferEXT");
if (glBindRenderbufferEXT == NULL)
glBindRenderbufferEXT = (PFNGLBINDRENDERBUFFEREXTPROC)dummy_glBindRenderbuffer;
if (glDeleteRenderbuffersEXT == NULL)
glDeleteRenderbuffersEXT = (PFNGLDELETERENDERBUFFERSEXTPROC)dummy_glDeleteRenderbuffers;
if (glGenRenderbuffersEXT == NULL)
glGenRenderbuffersEXT = (PFNGLGENRENDERBUFFERSEXTPROC)dummy_glGenRenderbuffers;
if (glRenderbufferStorageEXT == NULL)
glRenderbufferStorageEXT = (PFNGLRENDERBUFFERSTORAGEEXTPROC)dummy_glRenderbufferStorage;
if (glFramebufferRenderbufferEXT == NULL)
glFramebufferRenderbufferEXT = (PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC)dummy_glFramebufferRenderbuffer;
#endif // _WIN32
use_fbo = g_settings->wrpFBO() && glFramebufferRenderbufferEXT;
printf("use_fbo %d\n", use_fbo);
if (isExtensionSupported("GL_ARB_shading_language_100") &&
isExtensionSupported("GL_ARB_shader_objects") &&
isExtensionSupported("GL_ARB_fragment_shader") &&
isExtensionSupported("GL_ARB_vertex_shader"))
{
#ifdef _WIN32
glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC)wglGetProcAddress("glCreateShaderObjectARB");
glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC)wglGetProcAddress("glShaderSourceARB");
glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC)wglGetProcAddress("glCompileShaderARB");
glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC)wglGetProcAddress("glCreateProgramObjectARB");
glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC)wglGetProcAddress("glAttachObjectARB");
glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC)wglGetProcAddress("glLinkProgramARB");
glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC)wglGetProcAddress("glUseProgramObjectARB");
glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC)wglGetProcAddress("glGetUniformLocationARB");
glUniform1iARB = (PFNGLUNIFORM1IARBPROC)wglGetProcAddress("glUniform1iARB");
glUniform4iARB = (PFNGLUNIFORM4IARBPROC)wglGetProcAddress("glUniform4iARB");
glUniform4fARB = (PFNGLUNIFORM4FARBPROC)wglGetProcAddress("glUniform4fARB");
glUniform1fARB = (PFNGLUNIFORM1FARBPROC)wglGetProcAddress("glUniform1fARB");
glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC)wglGetProcAddress("glDeleteObjectARB");
glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC)wglGetProcAddress("glGetInfoLogARB");
glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC)wglGetProcAddress("glGetObjectParameterivARB");
glSecondaryColor3f = (PFNGLSECONDARYCOLOR3FPROC)wglGetProcAddress("glSecondaryColor3f");
#endif // _WIN32
}
if (isExtensionSupported("GL_EXT_texture_compression_s3tc") == 0 && show_warning)
WriteTrace(TraceGlitch, TraceWarning, "Your video card doesn't support GL_EXT_texture_compression_s3tc extension");
if (isExtensionSupported("GL_3DFX_texture_compression_FXT1") == 0 && show_warning)
WriteTrace(TraceGlitch, TraceWarning, "Your video card doesn't support GL_3DFX_texture_compression_FXT1 extension");
#ifdef _WIN32
glCompressedTexImage2DARB = (PFNGLCOMPRESSEDTEXIMAGE2DPROC)wglGetProcAddress("glCompressedTexImage2DARB");
if (glCreateShaderObjectARB == NULL)
glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC)dummy_glCreateShader;
if (glShaderSourceARB == NULL)
glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC)dummy_glShaderSource;
if (glCompileShaderARB == NULL)
glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC)dummy_glCompileShader;
if (glCreateProgramObjectARB == NULL)
glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC)dummy_glCreateProgram;
if (glAttachObjectARB == NULL)
glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC)dummy_glAttachObject;
if (glLinkProgramARB == NULL)
glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC)dummy_glLinkProgram;
if (glUseProgramObjectARB == NULL)
glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC)dummy_glUseProgram;
if (glGetUniformLocationARB == NULL)
glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC)dummy_glGetUniformLocation;
if (glUniform1iARB == NULL)
glUniform1iARB = (PFNGLUNIFORM1IARBPROC)dummy_glUniform1i;
if (glUniform4iARB == NULL)
glUniform4iARB = (PFNGLUNIFORM4IARBPROC)dummy_glUniform4i;
if (glUniform4fARB == NULL)
glUniform4fARB = (PFNGLUNIFORM4FARBPROC)dummy_glUniform4f;
if (glUniform1fARB == NULL)
glUniform1fARB = (PFNGLUNIFORM1FARBPROC)dummy_glUniform1f;
if (glDeleteObjectARB == NULL)
glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC)dummy_glDeleteObject;
if (glGetInfoLogARB == NULL)
glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC)dummy_glGetInfoLog;
if (glGetObjectParameterivARB == NULL)
glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC)dummy_glGetObjectParameteriv;
if (glSecondaryColor3f == NULL)
glSecondaryColor3f = (PFNGLSECONDARYCOLOR3FPROC)dummy_glSecondaryColor3f;
if (glCompressedTexImage2DARB == NULL)
glCompressedTexImage2DARB = (PFNGLCOMPRESSEDTEXIMAGE2DPROC)dummy_glCompressedTexImage2D;
#endif
#ifndef ANDROID
glViewport(0, g_viewport_offset, g_width, g_height);
viewport_width = g_width;
viewport_height = g_height;
nvidia_viewport_hack = 1;
#else
glViewport(0, g_viewport_offset, width, height);
viewport_width = width;
viewport_height = height;
#endif // _WIN32
// void do_benchmarks();
// do_benchmarks();
// VP try to resolve z precision issues
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, 1 - zscale);
glScalef(1, 1, zscale);
widtho = g_width / 2;
heighto = g_height / 2;
pBufferWidth = pBufferHeight = -1;
current_buffer = GL_BACK;
texture_unit = GL_TEXTURE0_ARB;
{
int i;
for (i = 0; i < NB_TEXBUFS; i++)
texbufs[i].start = texbufs[i].end = 0xffffffff;
}
if (!use_fbo && nbAuxBuffers == 0) {
// create the framebuffer saving texture
int w = g_width, h = g_height;
glBindTexture(GL_TEXTURE_2D, color_texture);
if (!npot_support) {
w = h = 1;
while (w < g_width) w *= 2;
while (h < g_height) h *= 2;
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);
save_w = save_h = 0;
}
void FindBestDepthBias();
FindBestDepthBias();
init_geometry();
init_textures();
init_combiner();
// Aniso filter check
if (g_settings->wrpAnisotropic())
{
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &largest_supported_anisotropy);
}
// ATI hack - certain texture formats are slow on ATI?
// Hmm, perhaps the internal format need to be specified explicitly...
{
GLint ifmt;
glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, NULL);
glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &ifmt);
if (ifmt != GL_RGB5_A1) {
WriteTrace(TraceGlitch, TraceWarning, "ATI SUCKS %x\n", ifmt);
ati_sucks = 1;
}
else
ati_sucks = 0;
}
grDisplayGLError("gfxSstWinOpen");
return 1;
}
bool gfxSstWinClose()
{
WriteTrace(TraceGlitch, TraceDebug, "-");
int i;
#ifndef WIN32
int clear_texbuff = use_fbo;
#endif
for (i = 0; i < 2; i++) {
tmu_usage[i].min = 0x0FFFFFFFul;
tmu_usage[i].max = 0x00000000ul;
invtex[i] = 0;
}
free_combiners();
#ifndef WIN32
try // I don't know why, but opengl can be killed before this function call when emulator is closed (Gonetz).
// ZIGGY : I found the problem : it is a function pointer, when the extension isn't supported , it is then zero, so just need to check the pointer prior to do the call.
{
if (use_fbo && glBindFramebufferEXT)
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
catch (...)
{
clear_texbuff = 0;
}
if (clear_texbuff)
{
for (i = 0; i < nb_fb; i++)
{
glDeleteTextures(1, &(fbs[i].texid));
glDeleteFramebuffersEXT(1, &(fbs[i].fbid));
glDeleteRenderbuffersEXT(1, &(fbs[i].zbid));
}
}
#endif
nb_fb = 0;
free_textures();
#ifndef WIN32
// ZIGGY for some reasons, Pj64 doesn't like remove_tex on exit
remove_tex(0, 0xfffffff);
#endif
//*/
#ifdef _WIN32
if (hGLRC)
{
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hGLRC);
hGLRC = NULL;
}
ExitFullScreen();
#else
//SDL_QuitSubSystem(SDL_INIT_VIDEO);
//sleep(2);
//m_pScreen = NULL;
#endif
return FXTRUE;
}
void gfxTextureBufferExt(gfxChipID_t tmu, uint32_t startAddress, gfxLOD_t lodmin, gfxLOD_t lodmax, gfxAspectRatio_t aspect, gfxTextureFormat_t fmt, uint32_t evenOdd)
{
int i;
static int fbs_init = 0;
WriteTrace(TraceGlitch, TraceDebug, "tmu: %d startAddress: %d lodmin: %d lodmax: %d aspect: %d fmt: %d evenOdd: %d", tmu, startAddress, lodmin, lodmax, aspect, fmt, evenOdd);
if (lodmin != lodmax) WriteTrace(TraceGlitch, TraceWarning, "gfxTextureBufferExt : loading more than one LOD");
if (!use_fbo) {
if (!render_to_texture) { //initialization
return;
}
render_to_texture = 2;
if (aspect < 0)
{
pBufferHeight = 1 << lodmin;
pBufferWidth = pBufferHeight >> -aspect;
}
else
{
pBufferWidth = 1 << lodmin;
pBufferHeight = pBufferWidth >> aspect;
}
if (curBufferAddr && startAddress + 1 != curBufferAddr)
updateTexture();
#ifdef SAVE_CBUFFER
//printf("saving %dx%d\n", pBufferWidth, pBufferHeight);
// save color buffer
if (nbAuxBuffers > 0) {
glDrawBuffer(GL_AUX0);
current_buffer = GL_AUX0;
}
else {
int tw, th;
if (pBufferWidth < screen_width)
tw = pBufferWidth;
else
tw = screen_width;
if (pBufferHeight < screen_height)
th = pBufferHeight;
else
th = screen_height;
glReadBuffer(GL_BACK);
glActiveTextureARB(texture_unit);
glBindTexture(GL_TEXTURE_2D, color_texture);
// save incrementally the framebuffer
if (save_w) {
if (tw > save_w && th > save_h) {
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, save_h,
0, g_viewport_offset + save_h, tw, th - save_h);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, save_w, 0,
save_w, g_viewport_offset, tw - save_w, save_h);
save_w = tw;
save_h = th;
}
else if (tw > save_w) {
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, save_w, 0,
save_w, g_viewport_offset, tw - save_w, save_h);
save_w = tw;
}
else if (th > save_h) {
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, save_h,
0, g_viewport_offset + save_h, save_w, th - save_h);
save_h = th;
}
}
else {
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
0, g_viewport_offset, tw, th);
save_w = tw;
save_h = th;
}
glBindTexture(GL_TEXTURE_2D, default_texture);
}
#endif
if (startAddress + 1 != curBufferAddr ||
(curBufferAddr == 0L && nbAuxBuffers == 0))
buffer_cleared = 0;
curBufferAddr = pBufferAddress = startAddress + 1;
pBufferFmt = fmt;
int rtmu = startAddress < gfxTexMinAddress(GFX_TMU1) ? 0 : 1;
int size = pBufferWidth*pBufferHeight * 2; //grTexFormatSize(fmt);
if (tmu_usage[rtmu].min > pBufferAddress + 0)
tmu_usage[rtmu].min = pBufferAddress + 0;
if (tmu_usage[rtmu].max < pBufferAddress + size)
tmu_usage[rtmu].max = pBufferAddress + size;
// printf("tmu %d usage now %gMb - %gMb\n",
// rtmu, tmu_usage[rtmu].min/1024.0f, tmu_usage[rtmu].max/1024.0f);
g_width = pBufferWidth;
g_height = pBufferHeight;
widtho = g_width / 2;
heighto = g_height / 2;
// this could be improved, but might be enough as long as the set of
// texture buffer addresses stay small
for (i = (texbuf_i - 1)&(NB_TEXBUFS - 1); i != texbuf_i; i = (i - 1)&(NB_TEXBUFS - 1))
if (texbufs[i].start == pBufferAddress)
break;
texbufs[i].start = pBufferAddress;
texbufs[i].end = pBufferAddress + size;
texbufs[i].fmt = fmt;
if (i == texbuf_i)
texbuf_i = (texbuf_i + 1)&(NB_TEXBUFS - 1);
//printf("texbuf %x fmt %x\n", pBufferAddress, fmt);
// ZIGGY it speeds things up to not delete the buffers
// a better thing would be to delete them *sometimes*
// remove_tex(pBufferAddress+1, pBufferAddress + size);
add_tex(pBufferAddress);
//printf("viewport %dx%d\n", width, height);
if (g_height > screen_height) {
glViewport(0, g_viewport_offset + screen_height - g_height, g_width, g_height);
}
else
glViewport(0, g_viewport_offset, g_width, g_height);
glScissor(0, g_viewport_offset, g_width, g_height);
grDisplayGLError("gfxTextureBufferExt :: A");
}
else {
if (!render_to_texture) //initialization
{
if (!fbs_init)
{
for (i = 0; i < 100; i++) fbs[i].address = 0;
fbs_init = 1;