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

Contents of /trunk/plugins/zzogl-pg/opengl/ZZoglShaders.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: 28080 byte(s)
re-commit (had local access denied errors when committing)
1 /* ZZ Open GL graphics plugin
2 * Copyright (c)2009 zeydlitz@gmail.com
3 * Based on Zerofrog's ZeroGS KOSMOS (c)2005-2006
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 //#ifdef NVIDIA_CG_API // This code is only for NVIDIA cg-toolkit API
21 // ZZogl Shader manipulation functions.
22
23 //------------------- Includes
24 #include "Util.h"
25 #include "ZZoglShaders.h"
26 #include "zpipe.h"
27 #include <math.h>
28 #include <map>
29
30 #ifdef _WIN32
31 # include "Win32.h"
32 extern HINSTANCE hInst;
33 #endif
34
35 // ----------------- Defines
36
37 #define TEXWRAP_REPEAT 0
38 #define TEXWRAP_CLAMP 1
39 #define TEXWRAP_REGION_REPEAT 2
40 #define TEXWRAP_REPEAT_CLAMP 3
41
42 #define SH_WRITEDEPTH 0x2000 // depth is written
43 #define SH_CONTEXT1 0x1000 // context1 is used
44 #define SH_REGULARVS 0x8000
45 #define SH_TEXTUREVS 0x8001
46 #define SH_REGULARFOGVS 0x8002
47 #define SH_TEXTUREFOGVS 0x8003
48 #define SH_REGULARPS 0x8004
49 #define SH_REGULARFOGPS 0x8005
50 #define SH_BITBLTVS 0x8006
51 #define SH_BITBLTPS 0x8007
52 #define SH_BITBLTDEPTHPS 0x8009
53 #define SH_CRTCTARGPS 0x800a
54 #define SH_CRTCPS 0x800b
55 #define SH_CRTC24PS 0x800c
56 #define SH_ZEROPS 0x800e
57 #define SH_BASETEXTUREPS 0x800f
58 #define SH_BITBLTAAPS 0x8010
59 #define SH_CRTCTARGINTERPS 0x8012
60 #define SH_CRTCINTERPS 0x8013
61 #define SH_CRTC24INTERPS 0x8014
62 #define SH_BITBLTDEPTHMRTPS 0x8016
63 #define SH_CONVERT16TO32PS 0x8020
64 #define SH_CONVERT32TO16PS 0x8021
65 #define SH_CRTC_NEARESTPS 0x8022
66 #define SH_CRTCINTER_NEARESTPS 0x8023
67
68 //------------------ Constants
69
70 // Used in a logarithmic Z-test, as (1-o(1))/log(MAX_U32).
71 const float g_filog32 = 0.999f / (32.0f * logf(2.0f));
72
73 #ifdef _DEBUG
74 const static char* g_pTexTypes[] = { "32", "tex32", "clut32", "tex32to16", "tex16to8h" };
75 #endif
76 const char* g_pShaders[4] = { "full", "reduced", "accurate", "accurate-reduced" };
77
78 // ----------------- Global Variables
79
80 ZZshContext g_cgcontext;
81 ZZshProfile cgvProf, cgfProf;
82 int g_nPixelShaderVer = 0; // default
83 u8* s_lpShaderResources = NULL;
84 ZZshProgram pvs[16] = {NULL};
85 ZZshProgram g_vsprog = 0, g_psprog = 0; // 2 -- ZZ
86 ZZshParameter g_vparamPosXY[2] = {0}, g_fparamFogColor = 0;
87
88 #ifdef DEVBUILD
89 char* EFFECT_NAME; // All this variables used for testing and set manually
90 char* EFFECT_DIR;
91 #endif
92
93 bool g_bCRTCBilinear = true;
94
95 float4 g_vdepth, vlogz;
96 FRAGMENTSHADER ppsBitBlt[2], ppsBitBltDepth, ppsOne;
97 FRAGMENTSHADER ppsBaseTexture, ppsConvert16to32, ppsConvert32to16;
98 FRAGMENTSHADER ppsRegular[4], ppsTexture[NUM_SHADERS];
99 FRAGMENTSHADER ppsCRTC[2], ppsCRTC24[2], ppsCRTCTarg[2];
100 VERTEXSHADER pvsBitBlt;
101
102 extern u32 ptexBlocks; // holds information on block tiling. It's texture number in OpenGL -- if 0 than such texture
103 extern u32 ptexConv16to32; // does not exists. This textures should be created on start and released on finish.
104 extern u32 ptexBilinearBlocks;
105 extern u32 ptexConv32to16;
106
107 inline bool LoadEffects();
108 extern bool s_bWriteDepth;
109
110 struct SHADERHEADER
111 {
112 unsigned int index, offset, size; // if highest bit of index is set, pixel shader
113 };
114 map<int, SHADERHEADER*> mapShaderResources;
115
116 // Debug variable, store name of the function that call the shader.
117 const char* ShaderCallerName = "";
118 const char* ShaderHandleName = "";
119
120 //------------------ Code
121
122 inline int GET_SHADER_INDEX(int type, int texfilter, int texwrap, int fog, int writedepth, int testaem, int exactcolor, int context, int ps) {
123 return type + texfilter*NUM_TYPES + NUM_FILTERS*NUM_TYPES*texwrap + NUM_TEXWRAPS*NUM_FILTERS*NUM_TYPES*(fog+2*writedepth+4*testaem+8*exactcolor+16*context+32*ps) ;
124 }
125
126 bool ZZshCheckProfilesSupport() {
127 // load the effect, find the best profiles (if any)
128 if (cgGLIsProfileSupported(CG_PROFILE_ARBVP1) != CG_TRUE) {
129 ZZLog::Error_Log("arbvp1 not supported.");
130 return false;
131 }
132 if (cgGLIsProfileSupported(CG_PROFILE_ARBFP1) != CG_TRUE) {
133 ZZLog::Error_Log("arbfp1 not supported.");
134 return false;
135 }
136 return true;
137 }
138
139 // Error handler. Setup in ZZogl_Create once.
140 void HandleCgError(ZZshContext ctx, ZZshError err, void* appdata)
141 {
142 ZZLog::Error_Log("%s->%s: %s\n", ShaderCallerName, ShaderHandleName, cgGetErrorString(err));
143 const char* listing = cgGetLastListing(g_cgcontext);
144 if (listing != NULL)
145 ZZLog::Debug_Log(" last listing: %s\n", listing);
146 }
147
148 bool ZZshStartUsingShaders() {
149 cgSetErrorHandler(HandleCgError, NULL);
150 g_cgcontext = cgCreateContext();
151
152 cgvProf = CG_PROFILE_ARBVP1;
153 cgfProf = CG_PROFILE_ARBFP1;
154 cgGLEnableProfile(cgvProf);
155 cgGLEnableProfile(cgfProf);
156 cgGLSetOptimalOptions(cgvProf);
157 cgGLSetOptimalOptions(cgfProf);
158
159 cgGLSetManageTextureParameters(g_cgcontext, CG_FALSE);
160 //cgSetAutoCompile(g_cgcontext, CG_COMPILE_IMMEDIATE);
161
162 g_fparamFogColor = cgCreateParameter(g_cgcontext, CG_FLOAT4);
163 g_vparamPosXY[0] = cgCreateParameter(g_cgcontext, CG_FLOAT4);
164 g_vparamPosXY[1] = cgCreateParameter(g_cgcontext, CG_FLOAT4);
165
166
167 ZZLog::GS_Log("Creating effects.");
168 B_G(LoadEffects(), return false);
169
170 // create a sample shader
171 clampInfo temp;
172 memset(&temp, 0, sizeof(temp));
173 temp.wms = 3; temp.wmt = 3;
174
175 g_nPixelShaderVer = 0;//SHADER_ACCURATE;
176 // test
177 bool bFailed;
178 FRAGMENTSHADER* pfrag = ZZshLoadShadeEffect(0, 1, 1, 1, 1, temp, 0, &bFailed);
179 if( bFailed || pfrag == NULL ) {
180 g_nPixelShaderVer = SHADER_ACCURATE|SHADER_REDUCED;
181
182 pfrag = ZZshLoadShadeEffect(0, 0, 1, 1, 0, temp, 0, &bFailed);
183 if( pfrag != NULL )
184 cgGLLoadProgram(pfrag->prog);
185 if( bFailed || pfrag == NULL || cgGetError() != CG_NO_ERROR ) {
186 g_nPixelShaderVer = SHADER_REDUCED;
187 ZZLog::Error_Log("Basic shader test failed.");
188 }
189 }
190
191 if (g_nPixelShaderVer & SHADER_REDUCED)
192 conf.bilinear = 0;
193
194 ZZLog::GS_Log("Creating extra effects.");
195 B_G(ZZshLoadExtraEffects(), return false);
196
197 ZZLog::GS_Log("using %s shaders\n", g_pShaders[g_nPixelShaderVer]);
198 return true;
199 }
200
201 // open shader file according to build target
202 bool ZZshCreateOpenShadersFile() {
203 #ifndef DEVBUILD
204 # ifdef _WIN32
205 HRSRC hShaderSrc = FindResource(hInst, MAKEINTRESOURCE(IDR_SHADERS), RT_RCDATA);
206 assert( hShaderSrc != NULL );
207 HGLOBAL hShaderGlob = LoadResource(hInst, hShaderSrc);
208 assert( hShaderGlob != NULL );
209 s_lpShaderResources = (u8*)LockResource(hShaderGlob);
210 # else // not _WIN32
211 FILE* fres = fopen("ps2hw.dat", "rb");
212 if( fres == NULL ) {
213 fres = fopen("plugins/ps2hw.dat", "rb");
214 if( fres == NULL ) {
215 ZZLog::Error_Log("Cannot find ps2hw.dat in working directory. Exiting.");
216 return false;
217 }
218 }
219 fseek(fres, 0, SEEK_END);
220 size_t s = ftell(fres);
221 s_lpShaderResources = new u8[s+1];
222 fseek(fres, 0, SEEK_SET);
223 fread(s_lpShaderResources, s, 1, fres);
224 s_lpShaderResources[s] = 0;
225 # endif // _WIN32
226 #else // NOT RELEASE_TO_PUBLIC
227 # ifndef _WIN32 // NOT WINDOWS
228 // test if ps2hw.fx exists
229 char tempstr[255];
230 char curwd[255];
231 getcwd(curwd, ArraySize(curwd));
232
233 strcpy(tempstr, "/plugins/");
234 sprintf(EFFECT_NAME, "%sps2hw.fx", tempstr);
235 FILE* f = fopen(EFFECT_NAME, "r");
236 if( f == NULL ) {
237
238 strcpy(tempstr, "../../plugins/zzogl-pg/opengl/");
239 sprintf(EFFECT_NAME, "%sps2hw.fx", tempstr);
240 f = fopen(EFFECT_NAME, "r");
241
242 if( f == NULL ) {
243 ZZLog::Error_Log("Failed to find %s, try compiling a non-devbuild\n", EFFECT_NAME);
244 return false;
245 }
246 }
247 fclose(f);
248
249 sprintf(EFFECT_DIR, "%s/%s", curwd, tempstr);
250 sprintf(EFFECT_NAME, "%sps2hw.fx", EFFECT_DIR);
251 #endif
252 #endif // RELEASE_TO_PUBLIC
253 return true;
254 }
255
256 // Disable CG
257 void ZZshGLDisableProfile() {
258 cgGLDisableProfile(cgvProf);
259 cgGLDisableProfile(cgfProf);
260 }
261 //Enable CG
262 void ZZshGLEnableProfile() {
263 cgGLEnableProfile(cgvProf);
264 cgGLEnableProfile(cgfProf);
265 }
266
267 // This is helper of cgGLSetParameter4fv, made for debug purpose.
268 // Name could be any string. We must use it on compilation time, because erroneus handler does not
269 // return name
270 void ZZshSetParameter4fv(ZZshParameter param, const float* v, const char* name) {
271 ShaderHandleName = name;
272 cgGLSetParameter4fv(param, v);
273 }
274
275 void ZZshSetParameter4fv(ZZshProgram prog, ZZshParameter param, const float* v, const char* name) {
276 ShaderHandleName = name;
277 cgGLSetParameter4fv(param, v);
278 }
279
280 // The same stuff, but also with retry of param, name should be USED name of param for prog.
281 void ZZshSetParameter4fvWithRetry(ZZshParameter* param, ZZshProgram prog, const float* v, const char* name) {
282 if (param != NULL)
283 ZZshSetParameter4fv(prog, param[0], v, name);
284 else
285 ZZshSetParameter4fv(prog, cgGetNamedParameter(prog, name), v, name);
286 }
287
288 void ZZshGLSetTextureParameter(ZZshParameter param, GLuint texobj, const char* name) {
289 ShaderHandleName = name;
290 cgGLSetTextureParameter(param, texobj);
291 cgGLEnableTextureParameter(param);
292 }
293
294 // The same function for texture, also to cgGLEnable
295 void ZZshGLSetTextureParameter(ZZshProgram prog, ZZshParameter param, GLuint texobj, const char* name) {
296 ShaderHandleName = name;
297 cgGLSetTextureParameter(param, texobj);
298 cgGLEnableTextureParameter(param);
299 }
300
301 // Used sometimes for color 1.
302 void ZZshDefaultOneColor( FRAGMENTSHADER ptr ) {
303 ShaderHandleName = "Set Default One color";
304 float4 v = float4 ( 1, 1, 1, 1 );
305 ZZshSetParameter4fv( ptr.prog, ptr.sOneColor, v, "DefaultOne");
306 }
307
308 #define SET_UNIFORMPARAM(var, name) { \
309 p = cgGetNamedParameter(pf->prog, name); \
310 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE ) \
311 pf->var = p; \
312 } \
313
314 void ZZshSetVertexShader(ZZshProgram prog) {
315 if ((prog) != g_vsprog) {
316 cgGLBindProgram(prog);
317 g_vsprog = prog;
318 }
319 }
320
321 void ZZshSetPixelShader(ZZshProgram prog) {
322 if ((prog) != g_psprog) {
323 cgGLBindProgram(prog);
324 g_psprog = prog;
325 }
326 }
327
328 void SetupFragmentProgramParameters(FRAGMENTSHADER* pf, int context, int type)
329 {
330 // uniform parameters
331 ZZshParameter p;
332
333 p = cgGetNamedParameter(pf->prog, "g_fFogColor");
334 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE ) {
335 cgConnectParameter(g_fparamFogColor, p);
336 }
337
338 SET_UNIFORMPARAM(sOneColor, "g_fOneColor");
339 SET_UNIFORMPARAM(sBitBltZ, "g_fBitBltZ");
340 SET_UNIFORMPARAM(sInvTexDims, "g_fInvTexDims");
341 SET_UNIFORMPARAM(fTexAlpha2, "fTexAlpha2");
342 SET_UNIFORMPARAM(fTexOffset, "g_fTexOffset");
343 SET_UNIFORMPARAM(fTexDims, "g_fTexDims");
344 SET_UNIFORMPARAM(fTexBlock, "g_fTexBlock");
345 SET_UNIFORMPARAM(fClampExts, "g_fClampExts");
346 SET_UNIFORMPARAM(fTexWrapMode, "TexWrapMode");
347 SET_UNIFORMPARAM(fRealTexDims, "g_fRealTexDims");
348 SET_UNIFORMPARAM(fTestBlack, "g_fTestBlack");
349 SET_UNIFORMPARAM(fPageOffset, "g_fPageOffset");
350 SET_UNIFORMPARAM(fTexAlpha, "fTexAlpha");
351
352 // textures
353 p = cgGetNamedParameter(pf->prog, "g_sBlocks");
354 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE ) {
355 cgGLSetTextureParameter(p, ptexBlocks);
356 cgGLEnableTextureParameter(p);
357 }
358
359 // cg parameter usage is wrong, so do it manually
360 if( type == 3 ) {
361 p = cgGetNamedParameter(pf->prog, "g_sConv16to32");
362 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE ) {
363 cgGLSetTextureParameter(p, ptexConv16to32);
364 cgGLEnableTextureParameter(p);
365 }
366 }
367 else if( type == 4 ) {
368 p = cgGetNamedParameter(pf->prog, "g_sConv32to16");
369 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE ) {
370 cgGLSetTextureParameter(p, ptexConv32to16);
371 cgGLEnableTextureParameter(p);
372 }
373 }
374 else {
375 p = cgGetNamedParameter(pf->prog, "g_sBilinearBlocks");
376 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE ) {
377 cgGLSetTextureParameter(p, ptexBilinearBlocks);
378 cgGLEnableTextureParameter(p);
379 }
380 }
381
382 p = cgGetNamedParameter(pf->prog, "g_sMemory");
383 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE ) {
384 //cgGLEnableTextureParameter(p);
385 pf->sMemory = p;
386 }
387 p = cgGetNamedParameter(pf->prog, "g_sSrcFinal");
388 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE ) {
389 //cgGLEnableTextureParameter(p);
390 pf->sFinal = p;
391 }
392 p = cgGetNamedParameter(pf->prog, "g_sBitwiseANDX");
393 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE ) {
394 //cgGLEnableTextureParameter(p);
395 pf->sBitwiseANDX = p;
396 }
397 p = cgGetNamedParameter(pf->prog, "g_sBitwiseANDY");
398 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE ) {
399 //cgGLEnableTextureParameter(p);
400 pf->sBitwiseANDY = p;
401 }
402 p = cgGetNamedParameter(pf->prog, "g_sCLUT");
403 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE ) {
404 //cgGLEnableTextureParameter(p);
405 pf->sCLUT = p;
406 }
407 p = cgGetNamedParameter(pf->prog, "g_sInterlace");
408 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE ) {
409 //cgGLEnableTextureParameter(p);
410 pf->sInterlace = p;
411 }
412
413 // set global shader constants
414 p = cgGetNamedParameter(pf->prog, "g_fExactColor");
415 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE ) {
416 cgGLSetParameter4fv(p, float4(0.5f, (conf.settings().exact_color)?0.9f/256.0f:0.5f/256.0f, 0,1/255.0f));
417 }
418
419 p = cgGetNamedParameter(pf->prog, "g_fBilinear");
420 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE )
421 cgGLSetParameter4fv(p, float4(-0.2f, -0.65f, 0.9f, 1.0f / 32767.0f ));
422
423 p = cgGetNamedParameter(pf->prog, "g_fZBias");
424 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE )
425 cgGLSetParameter4fv(p, float4(1.0f/256.0f, 1.0004f, 1, 0.5f));
426
427 p = cgGetNamedParameter(pf->prog, "g_fc0");
428 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE )
429 cgGLSetParameter4fv(p, float4(0,1, 0.001f, 0.5f));
430
431 p = cgGetNamedParameter(pf->prog, "g_fMult");
432 if( p != NULL && cgIsParameterUsed(p, pf->prog) == CG_TRUE )
433 cgGLSetParameter4fv(p, float4(1/1024.0f, 0.2f/1024.0f, 1/128.0f, 1/512.0f));
434 }
435
436 void SetupVertexProgramParameters(ZZshProgram prog, int context)
437 {
438 ZZshParameter p;
439
440 p = cgGetNamedParameter(prog, "g_fPosXY");
441 if( p != NULL && cgIsParameterUsed(p, prog) == CG_TRUE )
442 cgConnectParameter(g_vparamPosXY[context], p);
443
444 // Set Z-test, log or no log;
445 if (conf.settings().no_logz) {
446 g_vdepth = float4( 255.0 /256.0f, 255.0/65536.0f, 255.0f/(65535.0f*256.0f), 1.0f/(65536.0f*65536.0f));
447 vlogz = float4( 1.0f, 0.0f, 0.0f, 0.0f);
448 }
449 else {
450 g_vdepth = float4( 256.0f*65536.0f, 65536.0f, 256.0f, 65536.0f*65536.0f);
451 vlogz = float4( 0.0f, 1.0f, 0.0f, 0.0f);
452 }
453
454 p = cgGetNamedParameter(prog, "g_fZ");
455 if( p != NULL && cgIsParameterUsed(p, prog) == CG_TRUE ) {
456 cgGLSetParameter4fv(p, g_vdepth);
457
458 p = cgGetNamedParameter(prog, "g_fZMin"); // Switch to flat-z when needed
459 if( p != NULL && cgIsParameterUsed(p, prog) == CG_TRUE ) {
460 //ZZLog::Error_Log("Use flat-z\n");
461 cgGLSetParameter4fv(p, vlogz);
462 }
463 else
464 ZZLog::Error_Log("Shader file version is outdated! Only log-Z is possible.");
465 }
466
467 float4 vnorm = float4(g_filog32, 0, 0,0);
468 p = cgGetNamedParameter(prog, "g_fZNorm");
469 if( p != NULL && cgIsParameterUsed(p, prog) == CG_TRUE )
470 cgGLSetParameter4fv(p, vnorm);
471
472 p = cgGetNamedParameter(prog, "g_fBilinear");
473 if( p != NULL && cgIsParameterUsed(p, prog) == CG_TRUE )
474 cgGLSetParameter4fv(p, float4(-0.2f, -0.65f, 0.9f, 1.0f / 32767.0f ));
475
476 p = cgGetNamedParameter(prog, "g_fZBias");
477 if( p != NULL && cgIsParameterUsed(p, prog) == CG_TRUE )
478 cgGLSetParameter4fv(p, float4(1.0f/256.0f, 1.0004f, 1, 0.5f));
479
480 p = cgGetNamedParameter(prog, "g_fc0");
481 if( p != NULL && cgIsParameterUsed(p, prog) == CG_TRUE )
482 cgGLSetParameter4fv(p, float4(0,1, 0.001f, 0.5f));
483 }
484
485 #ifndef DEVBUILD
486
487 #define LOAD_VS(Index, prog) { \
488 assert( mapShaderResources.find(Index) != mapShaderResources.end() ); \
489 header = mapShaderResources[Index]; \
490 assert( (header) != NULL && (header)->index == (Index) ); \
491 prog = cgCreateProgram(g_cgcontext, CG_OBJECT, (char*)(s_lpShaderResources + (header)->offset), cgvProf, NULL, NULL); \
492 if( !cgIsProgram(prog) ) { \
493 ZZLog::Error_Log("Failed to load vs %d: \n%s", Index, cgGetLastListing(g_cgcontext)); \
494 return false; \
495 } \
496 cgGLLoadProgram(prog); \
497 if( cgGetError() != CG_NO_ERROR ) ZZLog::Error_Log("Failed to load program %d.", Index); \
498 SetupVertexProgramParameters(prog, !!(Index&SH_CONTEXT1)); \
499 } \
500
501 #define LOAD_PS(Index, fragment) { \
502 bLoadSuccess = true; \
503 assert( mapShaderResources.find(Index) != mapShaderResources.end() ); \
504 header = mapShaderResources[Index]; \
505 fragment.prog = cgCreateProgram(g_cgcontext, CG_OBJECT, (char*)(s_lpShaderResources + (header)->offset), cgfProf, NULL, NULL); \
506 if( !cgIsProgram(fragment.prog) ) { \
507 ZZLog::Error_Log("Failed to load ps %d: \n%s", Index, cgGetLastListing(g_cgcontext)); \
508 return false; \
509 } \
510 cgGLLoadProgram(fragment.prog); \
511 if( cgGetError() != CG_NO_ERROR ) { \
512 ZZLog::Error_Log("failed to load program %d.", Index); \
513 bLoadSuccess = false; \
514 } \
515 SetupFragmentProgramParameters(&fragment, !!(Index&SH_CONTEXT1), 0); \
516 } \
517
518 inline bool LoadEffects()
519 {
520 assert( s_lpShaderResources != NULL );
521
522 // process the header
523 u32 num = *(u32*)s_lpShaderResources;
524 int compressed_size = *(int*)(s_lpShaderResources+4);
525 int real_size = *(int*)(s_lpShaderResources+8);
526 int out;
527
528 char* pbuffer = (char*)malloc(real_size);
529 inf((char*)s_lpShaderResources+12, &pbuffer[0], compressed_size, real_size, &out);
530 assert(out == real_size);
531
532 s_lpShaderResources = (u8*)pbuffer;
533 SHADERHEADER* header = (SHADERHEADER*)s_lpShaderResources;
534
535 mapShaderResources.clear();
536 while(num-- > 0 ) {
537 mapShaderResources[header->index] = header;
538 ++header;
539 }
540
541 // clear the textures
542 for(u16 i = 0; i < ArraySize(ppsTexture); ++i) {
543 SAFE_RELEASE_PROG(ppsTexture[i].prog);
544 ppsTexture[i].prog = NULL;
545 }
546 #ifndef _DEBUG
547 memset(ppsTexture, 0, sizeof(ppsTexture));
548 #endif
549
550 return true;
551 }
552
553 // called
554 bool ZZshLoadExtraEffects()
555 {
556 SHADERHEADER* header;
557 bool bLoadSuccess = true;
558
559 const int vsshaders[4] = { SH_REGULARVS, SH_TEXTUREVS, SH_REGULARFOGVS, SH_TEXTUREFOGVS };
560
561 for(int i = 0; i < 4; ++i) {
562 LOAD_VS(vsshaders[i], pvs[2*i]);
563 LOAD_VS((vsshaders[i] | SH_CONTEXT1), pvs[2*i+1]);
564 //if( conf.mrtdepth ) {
565 LOAD_VS((vsshaders[i] | SH_WRITEDEPTH), pvs[2*i+8]);
566 LOAD_VS((vsshaders[i] | SH_WRITEDEPTH | SH_CONTEXT1), pvs[2*i+8+1]);
567 // }
568 // else {
569 // pvs[2*i+8] = pvs[2*i+8+1] = NULL;
570 // }
571 }
572
573 LOAD_VS(SH_BITBLTVS, pvsBitBlt.prog);
574 pvsBitBlt.sBitBltPos = cgGetNamedParameter(pvsBitBlt.prog, "g_fBitBltPos");
575 pvsBitBlt.sBitBltTex = cgGetNamedParameter(pvsBitBlt.prog, "g_fBitBltTex");
576 pvsBitBlt.fBitBltTrans = cgGetNamedParameter(pvsBitBlt.prog, "g_fBitBltTrans");
577
578 LOAD_PS(SH_REGULARPS, ppsRegular[0]);
579 LOAD_PS(SH_REGULARFOGPS, ppsRegular[1]);
580
581 if( conf.mrtdepth ) {
582 LOAD_PS(SH_REGULARPS, ppsRegular[2]);
583 if( !bLoadSuccess )
584 conf.mrtdepth = 0;
585 LOAD_PS(SH_REGULARFOGPS, ppsRegular[3]);
586 if( !bLoadSuccess )
587 conf.mrtdepth = 0;
588 }
589
590 LOAD_PS(SH_BITBLTPS, ppsBitBlt[0]);
591 LOAD_PS(SH_BITBLTAAPS, ppsBitBlt[1]);
592 if( !bLoadSuccess ) {
593 ZZLog::Error_Log("Failed to load BitBltAAPS, using BitBltPS.");
594 LOAD_PS(SH_BITBLTPS, ppsBitBlt[1]);
595 }
596 LOAD_PS(SH_BITBLTDEPTHPS, ppsBitBltDepth);
597 LOAD_PS(SH_CRTCTARGPS, ppsCRTCTarg[0]);
598 LOAD_PS(SH_CRTCTARGINTERPS, ppsCRTCTarg[1]);
599
600 g_bCRTCBilinear = true;
601 LOAD_PS(SH_CRTCPS, ppsCRTC[0]);
602 if( !bLoadSuccess ) {
603 // switch to simpler
604 g_bCRTCBilinear = false;
605 LOAD_PS(SH_CRTC_NEARESTPS, ppsCRTC[0]);
606 LOAD_PS(SH_CRTCINTER_NEARESTPS, ppsCRTC[0]);
607 }
608 else {
609 LOAD_PS(SH_CRTCINTERPS, ppsCRTC[1]);
610 }
611
612 if( !bLoadSuccess )
613 ZZLog::Error_Log("Failed to create CRTC shaders.");
614
615 LOAD_PS(SH_CRTC24PS, ppsCRTC24[0]);
616 LOAD_PS(SH_CRTC24INTERPS, ppsCRTC24[1]);
617 LOAD_PS(SH_ZEROPS, ppsOne);
618 LOAD_PS(SH_BASETEXTUREPS, ppsBaseTexture);
619 LOAD_PS(SH_CONVERT16TO32PS, ppsConvert16to32);
620 LOAD_PS(SH_CONVERT32TO16PS, ppsConvert32to16);
621
622 return true;
623 }
624
625 FRAGMENTSHADER* ZZshLoadShadeEffect(int type, int texfilter, int fog, int testaem, int exactcolor, const clampInfo& clamp, int context, bool* pbFailed)
626 {
627 int texwrap;
628 assert( texfilter < NUM_FILTERS );
629
630 if(g_nPixelShaderVer&SHADER_REDUCED)
631 texfilter = 0;
632 assert(!(g_nPixelShaderVer&SHADER_REDUCED) || !exactcolor);
633
634 if( clamp.wms == clamp.wmt ) {
635 switch( clamp.wms ) {
636 case 0: texwrap = TEXWRAP_REPEAT; break;
637 case 1: texwrap = TEXWRAP_CLAMP; break;
638 case 2: texwrap = TEXWRAP_CLAMP; break;
639 default: texwrap = TEXWRAP_REGION_REPEAT; break;
640 }
641 }
642 else if( clamp.wms==3||clamp.wmt==3)
643 texwrap = TEXWRAP_REGION_REPEAT;
644 else
645 texwrap = TEXWRAP_REPEAT_CLAMP;
646
647 int index = GET_SHADER_INDEX(type, texfilter, texwrap, fog, s_bWriteDepth, testaem, exactcolor, context, 0);
648
649 assert( index < ArraySize(ppsTexture) );
650 FRAGMENTSHADER* pf = ppsTexture+index;
651
652 if( pbFailed != NULL ) *pbFailed = false;
653
654 if( pf->prog != NULL )
655 return pf;
656
657 if( (g_nPixelShaderVer & SHADER_ACCURATE) && mapShaderResources.find(index+NUM_SHADERS*SHADER_ACCURATE) != mapShaderResources.end() )
658 index += NUM_SHADERS*SHADER_ACCURATE;
659
660 assert( mapShaderResources.find(index) != mapShaderResources.end() );
661 SHADERHEADER* header = mapShaderResources[index];
662 if( header == NULL )
663 ZZLog::Error_Log("%d %d", index, g_nPixelShaderVer);
664 assert( header != NULL );
665
666 //DEBUG_LOG("shader:\n%s\n", (char*)(s_lpShaderResources + (header)->offset));
667 pf->prog = cgCreateProgram(g_cgcontext, CG_OBJECT, (char*)(s_lpShaderResources + (header)->offset), cgfProf, NULL, NULL);
668 if( pf->prog != NULL && cgIsProgram(pf->prog) && cgGetError() == CG_NO_ERROR ) {
669 SetupFragmentProgramParameters(pf, context, type);
670 cgGLLoadProgram(pf->prog);
671 if( cgGetError() != CG_NO_ERROR ) {
672 // cgGLLoadProgram(pf->prog);
673 // if( cgGetError() != CG_NO_ERROR ) {
674 ZZLog::Error_Log("Failed to load shader %d,%d,%d,%d.", type, fog, texfilter, 4*clamp.wms+clamp.wmt);
675 if( pbFailed != NULL ) *pbFailed = true;
676 return pf;
677 // }
678 }
679 return pf;
680 }
681
682 ZZLog::Error_Log("Failed to create shader %d,%d,%d,%d", type, fog, texfilter, 4*clamp.wms+clamp.wmt);
683 if( pbFailed != NULL ) *pbFailed = true;
684
685 return NULL;
686 }
687
688 #else // not RELEASE_TO_PUBLIC
689
690 #define LOAD_VS(name, prog, shaderver) { \
691 prog = cgCreateProgramFromFile(g_cgcontext, CG_SOURCE, EFFECT_NAME, shaderver, name, args); \
692 if( !cgIsProgram(prog) ) { \
693 ZZLog::Error_Log("Failed to load vs %s: \n%s", name, cgGetLastListing(g_cgcontext)); \
694 return false; \
695 } \
696 cgGLLoadProgram(prog); \
697 if( cgGetError() != CG_NO_ERROR ) ZZLog::Error_Log("failed to load program %s", name); \
698 SetupVertexProgramParameters(prog, args[0]==context1); \
699 } \
700
701 #ifdef _DEBUG
702 #define SET_PSFILENAME(frag, name) frag.filename = name
703 #else
704 #define SET_PSFILENAME(frag, name)
705 #endif
706
707 #define LOAD_PS(name, fragment, shaderver) { \
708 bLoadSuccess = true; \
709 fragment.prog = cgCreateProgramFromFile(g_cgcontext, CG_SOURCE, EFFECT_NAME, shaderver, name, args); \
710 if( !cgIsProgram(fragment.prog) ) { \
711 ZZLog::Error_Log("Failed to load ps %s: \n%s", name, cgGetLastListing(g_cgcontext)); \
712 return false; \
713 } \
714 cgGLLoadProgram(fragment.prog); \
715 if( cgGetError() != CG_NO_ERROR ) { \
716 ZZLog::Error_Log("failed to load program %s", name); \
717 bLoadSuccess = false; \
718 } \
719 SetupFragmentProgramParameters(&fragment, args[0]==context1, 0); \
720 SET_PSFILENAME(fragment, name); \
721 } \
722
723 inline bool LoadEffects()
724 {
725 // clear the textures
726 for(int i = 0; i < ArraySize(ppsTexture); ++i) {
727 SAFE_RELEASE_PROG(ppsTexture[i].prog);
728 }
729
730 #ifndef _DEBUG
731 memset(ppsTexture, 0, sizeof(ppsTexture));
732 #endif
733
734 return true;
735 }
736
737 bool ZZshLoadExtraEffects()
738 {
739 const char* args[] = { NULL , NULL, NULL, NULL };
740 char context0[255], context1[255];
741 sprintf(context0, "-I%sctx0", EFFECT_DIR);
742 sprintf(context1, "-I%sctx1", EFFECT_DIR);
743 char* write_depth = "-DWRITE_DEPTH";
744 bool bLoadSuccess = true;
745
746 const char* pvsshaders[4] = { "RegularVS", "TextureVS", "RegularFogVS", "TextureFogVS" };
747
748 for(int i = 0; i < 4; ++i) {
749 args[0] = context0;
750 args[1] = NULL;
751 LOAD_VS(pvsshaders[i], pvs[2*i], cgvProf);
752 args[0] = context1;
753 LOAD_VS(pvsshaders[i], pvs[2*i+1], cgvProf);
754
755 //if( conf.mrtdepth ) {
756 args[0] = context0;
757 args[1] = write_depth;
758 LOAD_VS(pvsshaders[i], pvs[2*i+8], cgvProf);
759 args[0] = context1;
760 LOAD_VS(pvsshaders[i], pvs[2*i+8+1], cgvProf);
761 // }
762 // else {
763 // pvs[2*i+8] = pvs[2*i+8+1] = NULL;
764 // }
765 }
766
767 args[0] = context0;
768 args[1] = NULL;
769 LOAD_VS("BitBltVS", pvsBitBlt.prog, cgvProf);
770 pvsBitBlt.sBitBltPos = cgGetNamedParameter(pvsBitBlt.prog, "g_fBitBltPos");
771 pvsBitBlt.sBitBltTex = cgGetNamedParameter(pvsBitBlt.prog, "g_fBitBltTex");
772 pvsBitBlt.fBitBltTrans = cgGetNamedParameter(pvsBitBlt.prog, "g_fBitBltTrans");
773
774 LOAD_PS("RegularPS", ppsRegular[0], cgfProf);
775 LOAD_PS("RegularFogPS", ppsRegular[1], cgfProf);
776
777 if( conf.mrtdepth ) {
778 args[0] = context0;
779 args[1] = write_depth;
780 LOAD_PS("RegularPS", ppsRegular[2], cgfProf);
781 if( !bLoadSuccess )
782 conf.mrtdepth = 0;
783 LOAD_PS("RegularFogPS", ppsRegular[3], cgfProf);
784 if( !bLoadSuccess )
785 conf.mrtdepth = 0;
786 }
787
788 LOAD_PS("BitBltPS", ppsBitBlt[0], cgfProf);
789 LOAD_PS("BitBltAAPS", ppsBitBlt[1], cgfProf);
790 if( !bLoadSuccess ) {
791 ZZLog::Error_Log("Failed to load BitBltAAPS, using BitBltPS.");
792 LOAD_PS("BitBltPS", ppsBitBlt[1], cgfProf);
793 }
794
795 LOAD_PS("BitBltDepthPS", ppsBitBltDepth, cgfProf);
796 LOAD_PS("CRTCTargPS", ppsCRTCTarg[0], cgfProf);
797 LOAD_PS("CRTCTargInterPS", ppsCRTCTarg[1], cgfProf);
798
799 g_bCRTCBilinear = true;
800 LOAD_PS("CRTCPS", ppsCRTC[0], cgfProf);
801 if( !bLoadSuccess ) {
802 // switch to simpler
803 g_bCRTCBilinear = false;
804 LOAD_PS("CRTCPS_Nearest", ppsCRTC[0], cgfProf);
805 LOAD_PS("CRTCInterPS_Nearest", ppsCRTC[0], cgfProf);
806 }
807 else {
808 LOAD_PS("CRTCInterPS", ppsCRTC[1], cgfProf);
809 }
810
811 if( !bLoadSuccess )
812 ZZLog::Error_Log("Failed to create CRTC shaders.");
813
814 LOAD_PS("CRTC24PS", ppsCRTC24[0], cgfProf); LOAD_PS("CRTC24InterPS", ppsCRTC24[1], cgfProf);
815 LOAD_PS("ZeroPS", ppsOne, cgfProf);
816 LOAD_PS("BaseTexturePS", ppsBaseTexture, cgfProf);
817 LOAD_PS("Convert16to32PS", ppsConvert16to32, cgfProf);
818 LOAD_PS("Convert32to16PS", ppsConvert32to16, cgfProf);
819
820 // if( !conf.mrtdepth ) {
821 // ZZLog::Error_Log("Disabling MRT depth writing,");
822 // s_bWriteDepth = FALSE;
823 // }
824
825 return true;
826 }
827
828 FRAGMENTSHADER* ZZshLoadShadeEffect(int type, int texfilter, int fog, int testaem, int exactcolor, const clampInfo& clamp, int context, bool* pbFailed)
829 {
830 int texwrap;
831
832 assert( texfilter < NUM_FILTERS );
833 //assert( g_nPixelShaderVer == SHADER_30 );
834 if( clamp.wms == clamp.wmt ) {
835 switch( clamp.wms ) {
836 case 0: texwrap = TEXWRAP_REPEAT; break;
837 case 1: texwrap = TEXWRAP_CLAMP; break;
838 case 2: texwrap = TEXWRAP_CLAMP; break;
839 default:
840 texwrap = TEXWRAP_REGION_REPEAT; break;
841 }
842 }
843 else if( clamp.wms==3||clamp.wmt==3)
844 texwrap = TEXWRAP_REGION_REPEAT;
845 else
846 texwrap = TEXWRAP_REPEAT_CLAMP;
847
848 int index = GET_SHADER_INDEX(type, texfilter, texwrap, fog, s_bWriteDepth, testaem, exactcolor, context, 0);
849
850 if( pbFailed != NULL ) *pbFailed = false;
851
852 FRAGMENTSHADER* pf = ppsTexture+index;
853
854 if( pf->prog != NULL )
855 return pf;
856
857 pf->prog = LoadShaderFromType(EFFECT_DIR, EFFECT_NAME, type, texfilter, texwrap, fog, s_bWriteDepth, testaem, exactcolor, g_nPixelShaderVer, context);
858
859 if( pf->prog != NULL ) {
860 #ifdef _DEBUG
861 char str[255];
862 sprintf(str, "Texture%s%d_%sPS", fog?"Fog":"", texfilter, g_pTexTypes[type]);
863 pf->filename = str;
864 #endif
865 SetupFragmentProgramParameters(pf, context, type);
866 cgGLLoadProgram(pf->prog);
867 if( cgGetError() != CG_NO_ERROR ) {
868 // try again
869 // cgGLLoadProgram(pf->prog);
870 // if( cgGetError() != CG_NO_ERROR ) {
871 ZZLog::Error_Log("Failed to load shader %d,%d,%d,%d", type, fog, texfilter, 4*clamp.wms+clamp.wmt);
872 if( pbFailed != NULL ) *pbFailed = true;
873 //assert(0);
874 // NULL makes things crash
875 return pf;
876 // }
877 }
878 return pf;
879 }
880
881 ZZLog::Error_Log("Failed to create shader %d,%d,%d,%d", type, fog, texfilter, 4*clamp.wms+clamp.wmt);
882 if( pbFailed != NULL ) *pbFailed = true;
883
884 return NULL;
885 }
886
887 #endif // RELEASE_TO_PUBLIC
888
889 //#endif // NVIDIA_CG_API

  ViewVC Help
Powered by ViewVC 1.1.22