/[pcsx2_0.9.7]/trunk/plugins/zzogl-pg/opengl/ZZoglCreate.cpp
ViewVC logotype

Contents of /trunk/plugins/zzogl-pg/opengl/ZZoglCreate.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (show annotations) (download)
Thu Dec 23 12:02:12 2010 UTC (9 years, 2 months ago) by william
File size: 21951 byte(s)
re-commit (had local access denied errors when committing)
1 /* ZZ Open GL graphics plugin
2 * Copyright (c)2009-2010 zeydlitz@gmail.com, arcum42@gmail.com
3 * Based on Zerofrog's ZeroGS KOSMOS (c)2005-2008
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 // Create and Destroy function. They would be called once per session.
21
22 //------------------ Includes
23 #include "GS.h"
24 #include "Mem.h"
25 #include "GLWin.h"
26 #include "ZZoglShaders.h"
27
28 #include "targets.h"
29 #include "rasterfont.h" // simple font
30 #include "ZZoglDrawing.h"
31 #include "ZZoglVB.h"
32
33 // This include for windows resource file with Shaders
34 #ifdef _WIN32
35 # include "Win32.h"
36 #endif
37
38 //------------------ Defines
39
40 #ifdef _WIN32
41 #define GL_LOADFN(name) { \
42 if( (*(void**)&name = (void*)wglGetProcAddress(#name)) == NULL ) { \
43 ZZLog::Error_Log("Failed to find %s, exiting.", #name); \
44 } \
45 }
46 #else
47 // let GLEW take care of it
48 #define GL_LOADFN(name)
49 #endif
50
51 #define GL_BLEND_RGB(src, dst) { \
52 s_srcrgb = src; \
53 s_dstrgb = dst; \
54 zgsBlendFuncSeparateEXT(s_srcrgb, s_dstrgb, s_srcalpha, s_dstalpha); \
55 }
56
57 #define GL_BLEND_ALPHA(src, dst) { \
58 s_srcalpha = src; \
59 s_dstalpha = dst; \
60 zgsBlendFuncSeparateEXT(s_srcrgb, s_dstrgb, s_srcalpha, s_dstalpha); \
61 }
62
63 #define GL_BLEND_ALL(srcrgb, dstrgb, srcalpha, dstalpha) { \
64 s_srcrgb = srcrgb; \
65 s_dstrgb = dstrgb; \
66 s_srcalpha = srcalpha; \
67 s_dstalpha = dstalpha; \
68 zgsBlendFuncSeparateEXT(s_srcrgb, s_dstrgb, s_srcalpha, s_dstalpha); \
69 }
70
71 #define GL_BLEND_SET() zgsBlendFuncSeparateEXT(s_srcrgb, s_dstrgb, s_srcalpha, s_dstalpha)
72 #define VB_NUMBUFFERS 512
73
74 // ----------------- Types
75 typedef void (APIENTRYP _PFNSWAPINTERVAL)(int);
76
77 map<string, GLbyte> mapGLExtensions;
78
79 extern bool LoadEffects();
80 extern bool ZZshLoadExtraEffects();
81 extern FRAGMENTSHADER* ZZshLoadShadeEffect(int type, int texfilter, int fog, int testaem, int exactcolor, const clampInfo& clamp, int context, bool* pbFailed);
82
83 GLuint vboRect = 0;
84 vector<GLuint> g_vboBuffers; // VBOs for all drawing commands
85 int g_nCurVBOIndex = 0;
86
87 inline bool CreateImportantCheck();
88 inline void CreateOtherCheck();
89 inline bool CreateOpenShadersFile();
90
91 void ZZGSStateReset();
92
93 //------------------ Dummies
94 #ifdef _WIN32
95 void __stdcall glBlendFuncSeparateDummy(GLenum e1, GLenum e2, GLenum e3, GLenum e4)
96 #else
97 void APIENTRY glBlendFuncSeparateDummy(GLenum e1, GLenum e2, GLenum e3, GLenum e4)
98 #endif
99 {
100 glBlendFunc(e1, e2);
101 }
102
103 #ifdef _WIN32
104 void __stdcall glBlendEquationSeparateDummy(GLenum e1, GLenum e2)
105 #else
106 void APIENTRY glBlendEquationSeparateDummy(GLenum e1, GLenum e2)
107 #endif
108 {
109 glBlendEquation(e1);
110 }
111
112 #ifdef _WIN32
113 extern HINSTANCE hInst;
114 void (__stdcall *zgsBlendEquationSeparateEXT)(GLenum, GLenum) = NULL;
115 void (__stdcall *zgsBlendFuncSeparateEXT)(GLenum, GLenum, GLenum, GLenum) = NULL;
116 #else
117 void (APIENTRY *zgsBlendEquationSeparateEXT)(GLenum, GLenum) = NULL;
118 void (APIENTRY *zgsBlendFuncSeparateEXT)(GLenum, GLenum, GLenum, GLenum) = NULL;
119 #endif
120
121 //------------------ variables
122 ////////////////////////////
123 // State parameters
124
125 extern u8* s_lpShaderResources;
126
127 // String's for shader file in developer mode
128 #ifdef ZEROGS_DEVBUILD
129 char* EFFECT_NAME = "";
130 char* EFFECT_DIR = "";
131 #endif
132
133 /////////////////////
134 // graphics resources
135 GLenum s_srcrgb, s_dstrgb, s_srcalpha, s_dstalpha; // set by zgsBlendFuncSeparateEXT
136 u32 s_stencilfunc, s_stencilref, s_stencilmask;
137 GLenum s_drawbuffers[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT };
138
139 GLenum g_internalFloatFmt = GL_ALPHA_FLOAT32_ATI;
140 GLenum g_internalRGBAFloatFmt = GL_RGBA_FLOAT32_ATI;
141 GLenum g_internalRGBAFloat16Fmt = GL_RGBA_FLOAT16_ATI;
142
143 u32 ptexLogo = 0;
144 int nLogoWidth, nLogoHeight;
145 u32 s_ptexInterlace = 0; // holds interlace fields
146
147 //------------------ Global Variables
148 int GPU_TEXWIDTH = 512;
149 float g_fiGPU_TEXWIDTH = 1/512.0f;
150 int g_MaxTexWidth = 4096, g_MaxTexHeight = 4096;
151 u32 s_uFramebuffer = 0;
152
153 RasterFont* font_p = NULL;
154 float g_fBlockMult = 1;
155 //int s_nFullscreen = 0;
156
157 u32 ptexBlocks = 0, ptexConv16to32 = 0; // holds information on block tiling
158 u32 ptexBilinearBlocks = 0;
159 u32 ptexConv32to16 = 0;
160 int g_nDepthBias = 0;
161
162 extern void Delete_Avi_Capture();
163 extern void ZZDestroy();
164 extern void SetAA(int mode);
165
166 //------------------ Code
167
168 ///< returns true if the the opengl extension is supported
169 bool IsGLExt(const char* szTargetExtension)
170 {
171 return mapGLExtensions.find(string(szTargetExtension)) != mapGLExtensions.end();
172 }
173
174 // Function asks about different OGL extensions, that are required to setup accordingly. Return false if checks failed
175 inline bool CreateImportantCheck()
176 {
177 bool bSuccess = true;
178 #ifndef _WIN32
179 int const glew_ok = glewInit();
180
181 if (glew_ok != GLEW_OK)
182 {
183 ZZLog::Error_Log("glewInit() is not ok!");
184 bSuccess = false;
185 }
186
187 #endif
188
189 if (!IsGLExt("GL_EXT_framebuffer_object"))
190 {
191 ZZLog::Error_Log("*********\nZZogl: ERROR: Need GL_EXT_framebuffer_object for multiple render targets\nZZogl: *********");
192 bSuccess = false;
193 }
194
195 if (!IsGLExt("GL_EXT_secondary_color"))
196 {
197 ZZLog::Error_Log("*********\nZZogl: OGL WARNING: Need GL_EXT_secondary_color\nZZogl: *********");
198 bSuccess = false;
199 }
200
201 bSuccess &= ZZshCheckProfilesSupport();
202
203 return bSuccess;
204 }
205
206 // This is a check for less important open gl extensions.
207 inline void CreateOtherCheck()
208 {
209 if (!IsGLExt("GL_EXT_blend_equation_separate") || glBlendEquationSeparateEXT == NULL)
210 {
211 ZZLog::Error_Log("*********\nZZogl: OGL WARNING: Need GL_EXT_blend_equation_separate\nZZogl: *********");
212 zgsBlendEquationSeparateEXT = glBlendEquationSeparateDummy;
213 }
214 else
215 zgsBlendEquationSeparateEXT = glBlendEquationSeparateEXT;
216
217 if (!IsGLExt("GL_EXT_blend_func_separate") || glBlendFuncSeparateEXT == NULL)
218 {
219 ZZLog::Error_Log("*********\nZZogl: OGL WARNING: Need GL_EXT_blend_func_separate\nZZogl: *********");
220 zgsBlendFuncSeparateEXT = glBlendFuncSeparateDummy;
221 }
222 else
223 zgsBlendFuncSeparateEXT = glBlendFuncSeparateEXT;
224
225 if (!IsGLExt("GL_ARB_draw_buffers") && !IsGLExt("GL_ATI_draw_buffers"))
226 {
227 ZZLog::Error_Log("*********\nZZogl: OGL WARNING: multiple render targets not supported, some effects might look bad\nZZogl: *********");
228 conf.mrtdepth = 0;
229 }
230
231 if (IsGLExt("GL_ARB_draw_buffers"))
232 glDrawBuffers = (PFNGLDRAWBUFFERSPROC)wglGetProcAddress("glDrawBuffers");
233 else if (IsGLExt("GL_ATI_draw_buffers"))
234 glDrawBuffers = (PFNGLDRAWBUFFERSPROC)wglGetProcAddress("glDrawBuffersATI");
235
236
237 if (!IsGLExt("GL_ARB_multitexture"))
238 ZZLog::Error_Log("No multitexturing.");
239 else
240 ZZLog::Error_Log("Using multitexturing.");
241
242 GLint Max_Texture_Size_NV = 0;
243
244 GLint Max_Texture_Size_2d = 0;
245
246 glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_NV, &Max_Texture_Size_NV);
247
248 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &Max_Texture_Size_2d);
249
250 ZZLog::Error_Log("Maximum texture size is %d for Tex_2d and %d for Tex_NV.", Max_Texture_Size_2d, Max_Texture_Size_NV);
251
252 if (Max_Texture_Size_NV < 1024)
253 ZZLog::Error_Log("Could not properly make bitmasks, so some textures will be missed.");
254
255 #ifdef _WIN32
256 if (IsGLExt("WGL_EXT_swap_control") || IsGLExt("EXT_swap_control"))
257 wglSwapIntervalEXT(0);
258
259 #else
260 if (IsGLExt("GLX_SGI_swap_control"))
261 {
262 _PFNSWAPINTERVAL swapinterval = (_PFNSWAPINTERVAL)wglGetProcAddress("glXSwapInterval");
263
264 if (!swapinterval)
265 swapinterval = (_PFNSWAPINTERVAL)wglGetProcAddress("glXSwapIntervalSGI");
266
267 if (!swapinterval)
268 swapinterval = (_PFNSWAPINTERVAL)wglGetProcAddress("glXSwapIntervalEXT");
269
270 if (swapinterval)
271 swapinterval(0);
272 else
273 ZZLog::Error_Log("No support for SwapInterval (framerate clamped to monitor refresh rate),");
274 }
275
276 #endif
277 }
278
279
280 #ifdef _WIN32
281 __forceinline bool LoadShadersFromRes()
282 {
283 HRSRC hShaderSrc = FindResource(hInst, MAKEINTRESOURCE(IDR_SHADERS), RT_RCDATA);
284 assert(hShaderSrc != NULL);
285 HGLOBAL hShaderGlob = LoadResource(hInst, hShaderSrc);
286 assert(hShaderGlob != NULL);
287 s_lpShaderResources = (u8*)LockResource(hShaderGlob);
288 return true;
289 }
290 #else
291
292 __forceinline bool LoadShadersFromDat()
293 {
294 FILE* fres = fopen("ps2hw.dat", "rb");
295
296 if (fres == NULL)
297 {
298 fres = fopen("plugins/ps2hw.dat", "rb");
299
300 if (fres == NULL)
301 {
302 ZZLog::Error_Log("Cannot find ps2hw.dat in working directory. Exiting.");
303 return false;
304 }
305 }
306
307 fseek(fres, 0, SEEK_END);
308
309 size_t s = ftell(fres);
310 s_lpShaderResources = new u8[s+1];
311 fseek(fres, 0, SEEK_SET);
312 fread(s_lpShaderResources, s, 1, fres);
313 s_lpShaderResources[s] = 0;
314
315 return true;
316 }
317
318 #ifdef DEVBUILD
319 __forceinline bool LoadShadersFromFX()
320 {
321 // test if ps2hw.fx exists
322 char tempstr[255];
323 char curwd[255];
324 getcwd(curwd, ArraySize(curwd));
325
326 strcpy(tempstr, "/plugins/");
327 sprintf(EFFECT_NAME, "%sps2hw.fx", tempstr);
328 FILE* f = fopen(EFFECT_NAME, "r");
329
330 if (f == NULL)
331 {
332
333 strcpy(tempstr, "../../plugins/zzogl-pg/opengl/");
334 sprintf(EFFECT_NAME, "%sps2hw.fx", tempstr);
335 f = fopen(EFFECT_NAME, "r");
336
337 if (f == NULL)
338 {
339 ZZLog::Error_Log("Failed to find %s, try compiling a non-devbuild.", EFFECT_NAME);
340 return false;
341 }
342 }
343
344 fclose(f);
345
346 sprintf(EFFECT_DIR, "%s/%s", curwd, tempstr);
347 sprintf(EFFECT_NAME, "%sps2hw.fx", EFFECT_DIR);
348
349 return true;
350 }
351 #endif
352 #endif
353
354
355 // open shader file according to build target
356
357 inline bool CreateOpenShadersFile()
358 {
359 #ifndef DEVBUILD
360 # ifdef _WIN32
361 return LoadShadersFromRes();
362 # else // not _WIN32
363 return LoadShadersFromDat();
364 # endif // _WIN32
365 #else // defined(ZEROGS_DEVBUILD)
366 # ifndef _WIN32 // NOT WINDOWS
367 return LoadShadersFromFX();
368
369 // No else clause?
370 #endif
371 #endif // !defined(ZEROGS_DEVBUILD)
372 }
373
374 // Read all extensions name and fill mapGLExtensions
375 inline bool CreateFillExtensionsMap()
376 {
377 string temp("");
378 int max_ext = 0;
379 glGetIntegerv(GL_NUM_EXTENSIONS, &max_ext);
380
381 PFNGLGETSTRINGIPROC glGetStringi = 0;
382 glGetStringi = (PFNGLGETSTRINGIPROC)wglGetProcAddress("glGetStringi");
383
384 for (GLint i = 0; i < max_ext; i++)
385 {
386 string extension((const char*)glGetStringi(GL_EXTENSIONS, i));
387 mapGLExtensions[extension];
388
389 temp = temp + extension;
390 if (i != (max_ext - 1)) temp += ", ";
391 }
392
393 // Write the extension list to the log, but only write it to the screen on a debug build.
394 #ifndef _DEBUG
395 ZZLog::Log("%d supported OpenGL Extensions: %s\n", max_ext, temp.c_str());
396 #endif
397 ZZLog::Debug_Log("%d supported OpenGL Extensions: %s\n", max_ext, temp.c_str());
398
399 return true;
400 }
401
402 void LoadglFunctions()
403 {
404 GL_LOADFN(glIsRenderbufferEXT);
405 GL_LOADFN(glBindRenderbufferEXT);
406 GL_LOADFN(glDeleteRenderbuffersEXT);
407 GL_LOADFN(glGenRenderbuffersEXT);
408 GL_LOADFN(glRenderbufferStorageEXT);
409 GL_LOADFN(glGetRenderbufferParameterivEXT);
410 GL_LOADFN(glIsFramebufferEXT);
411 GL_LOADFN(glBindFramebufferEXT);
412 GL_LOADFN(glDeleteFramebuffersEXT);
413 GL_LOADFN(glGenFramebuffersEXT);
414 GL_LOADFN(glCheckFramebufferStatusEXT);
415 GL_LOADFN(glFramebufferTexture1DEXT);
416 GL_LOADFN(glFramebufferTexture2DEXT);
417 GL_LOADFN(glFramebufferTexture3DEXT);
418 GL_LOADFN(glFramebufferRenderbufferEXT);
419 GL_LOADFN(glGetFramebufferAttachmentParameterivEXT);
420 GL_LOADFN(glGenerateMipmapEXT);
421 }
422
423 inline bool TryBlockFormat(GLint fmt, const GLvoid* vBlockData) {
424 g_internalFloatFmt = fmt;
425 glTexImage2D(GL_TEXTURE_2D, 0, g_internalFloatFmt, BLOCK_TEXWIDTH, BLOCK_TEXHEIGHT, 0, GL_ALPHA, GL_FLOAT, vBlockData);
426 return (glGetError() == GL_NO_ERROR);
427 }
428
429 inline bool TryBlinearFormat(GLint fmt32, GLint fmt16, const GLvoid* vBilinearData) {
430 g_internalRGBAFloatFmt = fmt32;
431 g_internalRGBAFloat16Fmt = fmt16;
432 glTexImage2D(GL_TEXTURE_2D, 0, g_internalRGBAFloatFmt, BLOCK_TEXWIDTH, BLOCK_TEXHEIGHT, 0, GL_RGBA, GL_FLOAT, vBilinearData);
433 return (glGetError() == GL_NO_ERROR);
434 }
435
436
437 bool ZZCreate(int _width, int _height)
438 {
439 GLenum err = GL_NO_ERROR;
440 bool bSuccess = true;
441
442 ZZDestroy();
443 ZZGSStateReset();
444
445 if (!GLWin.DisplayWindow(_width, _height)) return false;
446
447 conf.mrtdepth = 0; // for now
448
449 if (!CreateFillExtensionsMap()) return false;
450 if (!CreateImportantCheck()) return false;
451
452 CreateOtherCheck();
453
454 // check the max texture width and height
455 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &g_MaxTexWidth);
456 // Limit the texture size supported to 8192. We do not need bigger texture.
457 // Besides the following assertion is false when texture are too big.
458 // ZZoglFlush.cpp:2349: assert(fblockstride >= 1.0f)
459 //g_MaxTexWidth = min(8192, g_MaxTexWidth);
460
461 g_MaxTexHeight = g_MaxTexWidth / 4;
462 GPU_TEXWIDTH = min (g_MaxTexWidth/8, 1024);
463 g_fiGPU_TEXWIDTH = 1.0f / GPU_TEXWIDTH;
464
465 if (!CreateOpenShadersFile()) return false;
466
467 GL_REPORT_ERROR();
468
469 if (err != GL_NO_ERROR) bSuccess = false;
470
471 s_srcrgb = s_dstrgb = s_srcalpha = s_dstalpha = GL_ONE;
472
473 LoadglFunctions();
474
475 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
476 GL_REPORT_ERROR();
477
478 if (err != GL_NO_ERROR) bSuccess = false;
479
480 glGenFramebuffersEXT(1, &s_uFramebuffer);
481
482 if (s_uFramebuffer == 0)
483 {
484 ZZLog::Error_Log("Failed to create the renderbuffer.");
485 }
486
487 GL_REPORT_ERRORD();
488
489 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, s_uFramebuffer);
490
491 DrawBuffers(s_drawbuffers);
492
493 GL_REPORT_ERROR();
494
495 if (err != GL_NO_ERROR) bSuccess = false;
496
497 font_p = new RasterFont();
498 GL_REPORT_ERROR();
499
500 if (err != GL_NO_ERROR) bSuccess = false;
501
502 // init draw fns
503 //init_drawfn();
504 if (ZZKick != NULL) delete ZZKick;
505 ZZKick = new Kick;
506
507 SetAA(conf.aa);
508
509 GSsetGameCRC(g_LastCRC, conf.settings()._u32);
510
511 GL_STENCILFUNC(GL_ALWAYS, 0, 0);
512
513 //s_bWriteDepth = true;
514
515 GL_BLEND_ALL(GL_ONE, GL_ONE, GL_ONE, GL_ONE);
516 glViewport(0, 0, GLWin.backbuffer.w, GLWin.backbuffer.h); // Reset The Current Viewport
517
518 glMatrixMode(GL_PROJECTION);
519 glLoadIdentity();
520
521 glMatrixMode(GL_MODELVIEW);
522 glLoadIdentity();
523
524 glShadeModel(GL_SMOOTH);
525 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
526 glClearDepth(1.0f);
527
528 glEnable(GL_DEPTH_TEST);
529 glDisable(GL_LIGHTING);
530
531 glDepthFunc(GL_LEQUAL);
532 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
533 glGenTextures(1, &ptexLogo);
534 glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexLogo);
535
536 #ifdef _WIN32
537 HRSRC hBitmapSrc = FindResource(hInst, MAKEINTRESOURCE(IDB_ZEROGSLOGO), RT_BITMAP);
538 assert(hBitmapSrc != NULL);
539
540 HGLOBAL hBitmapGlob = LoadResource(hInst, hBitmapSrc);
541 assert(hBitmapGlob != NULL);
542
543 PBITMAPINFO pinfo = (PBITMAPINFO)LockResource(hBitmapGlob);
544
545 GLenum tempFmt = (pinfo->bmiHeader.biBitCount == 32) ? GL_RGBA : GL_RGB;
546 TextureRect(GL_RGBA, pinfo->bmiHeader.biWidth, pinfo->bmiHeader.biHeight, tempFmt, GL_UNSIGNED_BYTE, (u8*)pinfo + pinfo->bmiHeader.biSize);
547
548 nLogoWidth = pinfo->bmiHeader.biWidth;
549 nLogoHeight = pinfo->bmiHeader.biHeight;
550
551 setRectFilters(GL_LINEAR);
552
553 #else
554 #endif
555
556 GL_REPORT_ERROR();
557
558 g_nCurVBOIndex = 0;
559
560 g_vboBuffers.resize(VB_NUMBUFFERS);
561 glGenBuffers((GLsizei)g_vboBuffers.size(), &g_vboBuffers[0]);
562
563 for (int i = 0; i < (int)g_vboBuffers.size(); ++i)
564 {
565 glBindBuffer(GL_ARRAY_BUFFER, g_vboBuffers[i]);
566 glBufferData(GL_ARRAY_BUFFER, 0x100*sizeof(VertexGPU), NULL, GL_STREAM_DRAW);
567 }
568
569 GL_REPORT_ERROR();
570 if (err != GL_NO_ERROR) bSuccess = false;
571
572 // create the blocks texture
573 g_fBlockMult = 1;
574 bool do_not_use_billinear = false;
575
576 vector<char> vBlockData, vBilinearData;
577 BLOCK::FillBlocks(vBlockData, vBilinearData, 1);
578
579 glGenTextures(1, &ptexBlocks);
580 glBindTexture(GL_TEXTURE_2D, ptexBlocks);
581
582 if (TryBlockFormat(GL_RGBA32F, &vBlockData[0]))
583 ZZLog::Error_Log("Use GL_RGBA32F for blockdata.");
584 else if (TryBlockFormat(GL_ALPHA_FLOAT32_ATI, &vBlockData[0]))
585 ZZLog::Error_Log("Use ATI_texture_float for blockdata.");
586 else if (TryBlockFormat(GL_ALPHA32F_ARB, &vBlockData[0]))
587 ZZLog::Error_Log("Use ARB_texture_float for blockdata.");
588 else
589 { // This case is bad. But for really old cards it could be nice.
590 g_fBlockMult = 65535.0f*(float)g_fiGPU_TEXWIDTH;
591 BLOCK::FillBlocks(vBlockData, vBilinearData, 0);
592 g_internalFloatFmt = GL_ALPHA16;
593
594 // We store block data on u16 rather float numbers. It's not so preciese, but ALPHA16 is OpenGL 2.0 standart
595 // and use only 16 bit. Old zerogs use red channel, but it does not work.
596
597 glTexImage2D(GL_TEXTURE_2D, 0, g_internalFloatFmt, BLOCK_TEXWIDTH, BLOCK_TEXHEIGHT, 0, GL_ALPHA, GL_UNSIGNED_SHORT, &vBlockData[0]);
598 if( glGetError() != GL_NO_ERROR ) {
599 ZZLog::Error_Log("ZZogl ERROR: could not fill blocks");
600 return false;
601 }
602
603 do_not_use_billinear = true;
604 conf.bilinear = 0;
605 ZZLog::Error_Log("Using non-bilinear fill, quallity is outdated!");
606 }
607
608 setTex2DFilters(GL_NEAREST);
609 setTex2DWrap(GL_REPEAT);
610
611 if (!do_not_use_billinear)
612 {
613 // fill in the bilinear blocks (main variant).
614 glGenTextures(1, &ptexBilinearBlocks);
615 glBindTexture(GL_TEXTURE_2D, ptexBilinearBlocks);
616
617 if (TryBlinearFormat(GL_RGBA32F, GL_RGBA16F, &vBilinearData[0]))
618 ZZLog::Error_Log("Fill bilinear blocks OK.!");
619 else if (TryBlinearFormat(GL_RGBA_FLOAT32_ATI, GL_RGBA_FLOAT16_ATI, &vBilinearData[0]))
620 ZZLog::Error_Log("Fill bilinear blocks with ATI_texture_float.");
621 else if (TryBlinearFormat(GL_FLOAT_RGBA32_NV, GL_FLOAT_RGBA16_NV, &vBilinearData[0]))
622 ZZLog::Error_Log("ZZogl Fill bilinear blocks with NVidia_float.");
623 else
624 ZZLog::Error_Log("Fill bilinear blocks failed.");
625
626 setTex2DFilters(GL_NEAREST);
627 setTex2DWrap(GL_REPEAT);
628 }
629
630 float fpri = 1;
631
632 glPrioritizeTextures(1, &ptexBlocks, &fpri);
633
634 if (ptexBilinearBlocks != 0) glPrioritizeTextures(1, &ptexBilinearBlocks, &fpri);
635
636 GL_REPORT_ERROR();
637
638 // fill a simple rect
639 glGenBuffers(1, &vboRect);
640 glBindBuffer(GL_ARRAY_BUFFER, vboRect);
641
642 vector<VertexGPU> verts(4);
643
644 VertexGPU* pvert = &verts[0];
645
646 pvert->set_xyzst(-0x7fff, 0x7fff, 0, 0, 0);
647 pvert++;
648
649 pvert->set_xyzst(0x7fff, 0x7fff, 0, 1, 0);
650 pvert++;
651
652 pvert->set_xyzst(-0x7fff, -0x7fff, 0, 0, 1);
653 pvert++;
654
655 pvert->set_xyzst(0x7fff, -0x7fff, 0, 1, 1);
656 pvert++;
657
658 glBufferDataARB(GL_ARRAY_BUFFER, 4*sizeof(VertexGPU), &verts[0], GL_STATIC_DRAW);
659
660 // setup the default vertex declaration
661 glEnableClientState(GL_VERTEX_ARRAY);
662 glClientActiveTexture(GL_TEXTURE0);
663 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
664 glEnableClientState(GL_SECONDARY_COLOR_ARRAY);
665 glEnableClientState(GL_COLOR_ARRAY);
666 GL_REPORT_ERROR();
667
668 // some cards don't support this
669 // glClientActiveTexture(GL_TEXTURE0);
670 // glEnableClientState(GL_TEXTURE_COORD_ARRAY);
671 // glTexCoordPointer(4, GL_UNSIGNED_BYTE, sizeof(VertexGPU), (void*)12);
672
673 // create the conversion textures
674 glGenTextures(1, &ptexConv16to32);
675 glBindTexture(GL_TEXTURE_2D, ptexConv16to32);
676
677 vector<u32> conv16to32data(256*256);
678
679 for (int i = 0; i < 256*256; ++i)
680 {
681 u32 tempcol = RGBA16to32(i);
682 // have to flip r and b
683 conv16to32data[i] = (tempcol & 0xff00ff00) | ((tempcol & 0xff) << 16) | ((tempcol & 0xff0000) >> 16);
684 }
685
686 Texture2D(4, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, &conv16to32data[0]);
687
688 setTex2DFilters(GL_NEAREST);
689 setTex2DWrap(GL_CLAMP);
690
691 GL_REPORT_ERROR();
692
693 if (err != GL_NO_ERROR) bSuccess = false;
694
695 vector<u32> conv32to16data(32*32*32);
696
697 glGenTextures(1, &ptexConv32to16);
698
699 glBindTexture(GL_TEXTURE_3D, ptexConv32to16);
700
701 u32* dst = &conv32to16data[0];
702
703 for (int i = 0; i < 32; ++i)
704 {
705 for (int j = 0; j < 32; ++j)
706 {
707 for (int k = 0; k < 32; ++k)
708 {
709 u32 col = (i << 10) | (j << 5) | k;
710 *dst++ = ((col & 0xff) << 16) | (col & 0xff00);
711 }
712 }
713 }
714
715 Texture3D(4, 32, 32, 32, GL_RGBA, GL_UNSIGNED_BYTE, &conv32to16data[0]);
716 setTex3DFilters(GL_NEAREST);
717 setTex3DWrap(GL_CLAMP);
718 GL_REPORT_ERROR();
719
720 if (err != GL_NO_ERROR) bSuccess = false;
721
722 if (!ZZshStartUsingShaders()) bSuccess = false;
723
724 GL_REPORT_ERROR();
725
726 if (err != GL_NO_ERROR) bSuccess = false;
727
728 glDisable(GL_STENCIL_TEST);
729 glEnable(GL_SCISSOR_TEST);
730
731 GL_BLEND_ALPHA(GL_ONE, GL_ZERO);
732
733 glBlendColorEXT(0, 0, 0, 0.5f);
734
735 glDisable(GL_CULL_FACE);
736
737 // points
738 // This was changed in SetAA - should we be changing it back?
739 glPointSize(1.0f);
740
741 g_nDepthBias = 0;
742
743 glEnable(GL_POLYGON_OFFSET_FILL);
744 glEnable(GL_POLYGON_OFFSET_LINE);
745
746 glPolygonOffset(0, 1);
747
748 vb[0].Init(VB_BUFFERSIZE);
749 vb[1].Init(VB_BUFFERSIZE);
750
751 g_vsprog = g_psprog = 0;
752
753 if (glGetError() == GL_NO_ERROR)
754 {
755 return bSuccess;
756 }
757 else
758 {
759 ZZLog::Debug_Log("In final init!");
760 return false;
761 }
762 }
763
764 void ZZDestroy()
765 {
766 Delete_Avi_Capture();
767
768 g_MemTargs.Destroy();
769
770 s_RTs.Destroy();
771 s_DepthRTs.Destroy();
772 s_BitwiseTextures.Destroy();
773
774 SAFE_RELEASE_TEX(s_ptexInterlace);
775 SAFE_RELEASE_TEX(ptexBlocks);
776 SAFE_RELEASE_TEX(ptexBilinearBlocks);
777 SAFE_RELEASE_TEX(ptexConv16to32);
778 SAFE_RELEASE_TEX(ptexConv32to16);
779
780 vb[0].Destroy();
781 vb[1].Destroy();
782
783 if (g_vboBuffers.size() > 0)
784 {
785 glDeleteBuffers((GLsizei)g_vboBuffers.size(), &g_vboBuffers[0]);
786 g_vboBuffers.clear();
787 }
788
789 g_nCurVBOIndex = 0;
790
791 if (pvs != NULL)
792 {
793 for (int i = 0; i < ArraySize(pvs); ++i)
794 {
795 SAFE_RELEASE_PROG(pvs[i]);
796 }
797 }
798
799 if (ppsRegular != NULL)
800 {
801 for (int i = 0; i < ArraySize(ppsRegular); ++i)
802 {
803 SAFE_RELEASE_PROG(ppsRegular[i].prog);
804 }
805 }
806
807 if (ppsTexture != NULL)
808 {
809 for (int i = 0; i < ArraySize(ppsTexture); ++i)
810 {
811 SAFE_RELEASE_PROG(ppsTexture[i].prog);
812 }
813 }
814
815 SAFE_RELEASE_PROG(pvsBitBlt.prog);
816
817 SAFE_RELEASE_PROG(ppsBitBlt[0].prog);
818 SAFE_RELEASE_PROG(ppsBitBlt[1].prog);
819 SAFE_RELEASE_PROG(ppsBitBltDepth.prog);
820 SAFE_RELEASE_PROG(ppsCRTCTarg[0].prog);
821 SAFE_RELEASE_PROG(ppsCRTCTarg[1].prog);
822 SAFE_RELEASE_PROG(ppsCRTC[0].prog);
823 SAFE_RELEASE_PROG(ppsCRTC[1].prog);
824 SAFE_RELEASE_PROG(ppsCRTC24[0].prog);
825 SAFE_RELEASE_PROG(ppsCRTC24[1].prog);
826 SAFE_RELEASE_PROG(ppsOne.prog);
827
828 safe_delete(font_p);
829
830 GLWin.ReleaseContext();
831
832 mapGLExtensions.clear();
833 }
834

  ViewVC Help
Powered by ViewVC 1.1.22