Debian Codegraph @

code/renderergl1/tr_shader.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 #include "tr_local.h"
23
24 // tr_shader.c -- this file deals with the parsing and definition of shaders
25
26 static char *s_shaderText;
27
28 // the shader is parsed into these global variables, then copied into
29 // dynamically allocated memory if it is valid.
30 static shaderStage_t stages[MAX_SHADER_STAGES];
31 static shader_t shader;
32 static texModInfo_t texMods[MAX_SHADER_STAGES][TR_MAX_TEXMODS];
33
34 #define FILE_HASH_SIZE 1024
35 static shader_t* hashTable[FILE_HASH_SIZE];
36
37 #define MAX_SHADERTEXT_HASH 2048
38 static char **shaderTextHashTable[MAX_SHADERTEXT_HASH];
39
40 /*
41 ================
42 return a hash value for the filename
43 ================
44 */
45 #ifdef __GNUCC__
46 #warning TODO: check if long is ok here
47 #endif
48 static long generateHashValue( const char *fname, const int size ) {
49 int i;
50 long hash;
51 char letter;
52
53 hash = 0;
54 i = 0;
55 while (fname[i] != '\0') {
56 letter = tolower(fname[i]);
57 if (letter =='.') break; // don't include extension
58 if (letter =='\\') letter = '/'; // damn path names
59 if (letter == PATH_SEP) letter = '/'; // damn path names
60 hash+=(long)(letter)*(i+119);
61 i++;
62 }
63 hash = (hash ^ (hash >> 10) ^ (hash >> 20));
64 hash &= (size-1);
65 return hash;
66 }
67
68 void R_RemapShader(const char *shaderName, const char *newShaderName, const char *timeOffset) {
69 char strippedName[MAX_QPATH];
70 int hash;
71 shader_t *sh, *sh2;
72 qhandle_t h;
73
74 sh = R_FindShaderByName( shaderName );
75 if (sh == NULL || sh == tr.defaultShader) {
76 h = RE_RegisterShaderLightMap(shaderName, 0);
77 sh = R_GetShaderByHandle(h);
78 }
79 if (sh == NULL || sh == tr.defaultShader) {
80 ri.Printf( PRINT_WARNING, "WARNING: R_RemapShader: shader %s not found\n", shaderName );
81 return;
82 }
83
84 sh2 = R_FindShaderByName( newShaderName );
85 if (sh2 == NULL || sh2 == tr.defaultShader) {
86 h = RE_RegisterShaderLightMap(newShaderName, 0);
87 sh2 = R_GetShaderByHandle(h);
88 }
89
90 if (sh2 == NULL || sh2 == tr.defaultShader) {
91 ri.Printf( PRINT_WARNING, "WARNING: R_RemapShader: new shader %s not found\n", newShaderName );
92 return;
93 }
94
95 // remap all the shaders with the given name
96 // even tho they might have different lightmaps
97 COM_StripExtension(shaderName, strippedName, sizeof(strippedName));
98 hash = generateHashValue(strippedName, FILE_HASH_SIZE);
99 for (sh = hashTable[hash]; sh; sh = sh->next) {
100 if (Q_stricmp(sh->name, strippedName) == 0) {
101 if (sh != sh2) {
102 sh->remappedShader = sh2;
103 } else {
104 sh->remappedShader = NULL;
105 }
106 }
107 }
108 if (timeOffset) {
109 sh2->timeOffset = atof(timeOffset);
110 }
111 }
112
113 /*
114 ===============
115 ParseVector
116 ===============
117 */
118 static qboolean ParseVector( char **text, int count, float *v ) {
119 char *token;
120 int i;
121
122 // FIXME: spaces are currently required after parens, should change parseext...
123 token = COM_ParseExt( text, qfalse );
124 if ( strcmp( token, "(" ) ) {
125 ri.Printf( PRINT_WARNING, "WARNING: missing parenthesis in shader '%s'\n", shader.name );
126 return qfalse;
127 }
128
129 for ( i = 0 ; i < count ; i++ ) {
130 token = COM_ParseExt( text, qfalse );
131 if ( !token[0] ) {
132 ri.Printf( PRINT_WARNING, "WARNING: missing vector element in shader '%s'\n", shader.name );
133 return qfalse;
134 }
135 v[i] = atof( token );
136 }
137
138 token = COM_ParseExt( text, qfalse );
139 if ( strcmp( token, ")" ) ) {
140 ri.Printf( PRINT_WARNING, "WARNING: missing parenthesis in shader '%s'\n", shader.name );
141 return qfalse;
142 }
143
144 return qtrue;
145 }
146
147
148 /*
149 ===============
150 NameToAFunc
151 ===============
152 */
153 static unsigned NameToAFunc( const char *funcname )
154 {
155 if ( !Q_stricmp( funcname, "GT0" ) )
156 {
157 return GLS_ATEST_GT_0;
158 }
159 else if ( !Q_stricmp( funcname, "LT128" ) )
160 {
161 return GLS_ATEST_LT_80;
162 }
163 else if ( !Q_stricmp( funcname, "GE128" ) )
164 {
165 return GLS_ATEST_GE_80;
166 }
167
168 ri.Printf( PRINT_WARNING, "WARNING: invalid alphaFunc name '%s' in shader '%s'\n", funcname, shader.name );
169 return 0;
170 }
171
172
173 /*
174 ===============
175 NameToSrcBlendMode
176 ===============
177 */
178 static int NameToSrcBlendMode( const char *name )
179 {
180 if ( !Q_stricmp( name, "GL_ONE" ) )
181 {
182 return GLS_SRCBLEND_ONE;
183 }
184 else if ( !Q_stricmp( name, "GL_ZERO" ) )
185 {
186 return GLS_SRCBLEND_ZERO;
187 }
188 else if ( !Q_stricmp( name, "GL_DST_COLOR" ) )
189 {
190 return GLS_SRCBLEND_DST_COLOR;
191 }
192 else if ( !Q_stricmp( name, "GL_ONE_MINUS_DST_COLOR" ) )
193 {
194 return GLS_SRCBLEND_ONE_MINUS_DST_COLOR;
195 }
196 else if ( !Q_stricmp( name, "GL_SRC_ALPHA" ) )
197 {
198 return GLS_SRCBLEND_SRC_ALPHA;
199 }
200 else if ( !Q_stricmp( name, "GL_ONE_MINUS_SRC_ALPHA" ) )
201 {
202 return GLS_SRCBLEND_ONE_MINUS_SRC_ALPHA;
203 }
204 else if ( !Q_stricmp( name, "GL_DST_ALPHA" ) )
205 {
206 return GLS_SRCBLEND_DST_ALPHA;
207 }
208 else if ( !Q_stricmp( name, "GL_ONE_MINUS_DST_ALPHA" ) )
209 {
210 return GLS_SRCBLEND_ONE_MINUS_DST_ALPHA;
211 }
212 else if ( !Q_stricmp( name, "GL_SRC_ALPHA_SATURATE" ) )
213 {
214 return GLS_SRCBLEND_ALPHA_SATURATE;
215 }
216
217 ri.Printf( PRINT_WARNING, "WARNING: unknown blend mode '%s' in shader '%s', substituting GL_ONE\n", name, shader.name );
218 return GLS_SRCBLEND_ONE;
219 }
220
221 /*
222 ===============
223 NameToDstBlendMode
224 ===============
225 */
226 static int NameToDstBlendMode( const char *name )
227 {
228 if ( !Q_stricmp( name, "GL_ONE" ) )
229 {
230 return GLS_DSTBLEND_ONE;
231 }
232 else if ( !Q_stricmp( name, "GL_ZERO" ) )
233 {
234 return GLS_DSTBLEND_ZERO;
235 }
236 else if ( !Q_stricmp( name, "GL_SRC_ALPHA" ) )
237 {
238 return GLS_DSTBLEND_SRC_ALPHA;
239 }
240 else if ( !Q_stricmp( name, "GL_ONE_MINUS_SRC_ALPHA" ) )
241 {
242 return GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA;
243 }
244 else if ( !Q_stricmp( name, "GL_DST_ALPHA" ) )
245 {
246 return GLS_DSTBLEND_DST_ALPHA;
247 }
248 else if ( !Q_stricmp( name, "GL_ONE_MINUS_DST_ALPHA" ) )
249 {
250 return GLS_DSTBLEND_ONE_MINUS_DST_ALPHA;
251 }
252 else if ( !Q_stricmp( name, "GL_SRC_COLOR" ) )
253 {
254 return GLS_DSTBLEND_SRC_COLOR;
255 }
256 else if ( !Q_stricmp( name, "GL_ONE_MINUS_SRC_COLOR" ) )
257 {
258 return GLS_DSTBLEND_ONE_MINUS_SRC_COLOR;
259 }
260
261 ri.Printf( PRINT_WARNING, "WARNING: unknown blend mode '%s' in shader '%s', substituting GL_ONE\n", name, shader.name );
262 return GLS_DSTBLEND_ONE;
263 }
264
265 /*
266 ===============
267 NameToGenFunc
268 ===============
269 */
270 static genFunc_t NameToGenFunc( const char *funcname )
271 {
272 if ( !Q_stricmp( funcname, "sin" ) )
273 {
274 return GF_SIN;
275 }
276 else if ( !Q_stricmp( funcname, "square" ) )
277 {
278 return GF_SQUARE;
279 }
280 else if ( !Q_stricmp( funcname, "triangle" ) )
281 {
282 return GF_TRIANGLE;
283 }
284 else if ( !Q_stricmp( funcname, "sawtooth" ) )
285 {
286 return GF_SAWTOOTH;
287 }
288 else if ( !Q_stricmp( funcname, "inversesawtooth" ) )
289 {
290 return GF_INVERSE_SAWTOOTH;
291 }
292 else if ( !Q_stricmp( funcname, "noise" ) )
293 {
294 return GF_NOISE;
295 }
296
297 ri.Printf( PRINT_WARNING, "WARNING: invalid genfunc name '%s' in shader '%s'\n", funcname, shader.name );
298 return GF_SIN;
299 }
300
301
302 /*
303 ===================
304 ParseWaveForm
305 ===================
306 */
307 static void ParseWaveForm( char **text, waveForm_t *wave )
308 {
309 char *token;
310
311 token = COM_ParseExt( text, qfalse );
312 if ( token[0] == 0 )
313 {
314 ri.Printf( PRINT_WARNING, "WARNING: missing waveform parm in shader '%s'\n", shader.name );
315 return;
316 }
317 wave->func = NameToGenFunc( token );
318
319 // BASE, AMP, PHASE, FREQ
320 token = COM_ParseExt( text, qfalse );
321 if ( token[0] == 0 )
322 {
323 ri.Printf( PRINT_WARNING, "WARNING: missing waveform parm in shader '%s'\n", shader.name );
324 return;
325 }
326 wave->base = atof( token );
327
328 token = COM_ParseExt( text, qfalse );
329 if ( token[0] == 0 )
330 {
331 ri.Printf( PRINT_WARNING, "WARNING: missing waveform parm in shader '%s'\n", shader.name );
332 return;
333 }
334 wave->amplitude = atof( token );
335
336 token = COM_ParseExt( text, qfalse );
337 if ( token[0] == 0 )
338 {
339 ri.Printf( PRINT_WARNING, "WARNING: missing waveform parm in shader '%s'\n", shader.name );
340 return;
341 }
342 wave->phase = atof( token );
343
344 token = COM_ParseExt( text, qfalse );
345 if ( token[0] == 0 )
346 {
347 ri.Printf( PRINT_WARNING, "WARNING: missing waveform parm in shader '%s'\n", shader.name );
348 return;
349 }
350 wave->frequency = atof( token );
351 }
352
353
354 /*
355 ===================
356 ParseTexMod
357 ===================
358 */
359 static void ParseTexMod( char *_text, shaderStage_t *stage )
360 {
361 const char *token;
362 char **text = &_text;
363 texModInfo_t *tmi;
364
365 if ( stage->bundle[0].numTexMods == TR_MAX_TEXMODS ) {
366 ri.Error( ERR_DROP, "ERROR: too many tcMod stages in shader '%s'", shader.name );
367 return;
368 }
369
370 tmi = &stage->bundle[0].texMods[stage->bundle[0].numTexMods];
371 stage->bundle[0].numTexMods++;
372
373 token = COM_ParseExt( text, qfalse );
374
375 //
376 // turb
377 //
378 if ( !Q_stricmp( token, "turb" ) )
379 {
380 token = COM_ParseExt( text, qfalse );
381 if ( token[0] == 0 )
382 {
383 ri.Printf( PRINT_WARNING, "WARNING: missing tcMod turb parms in shader '%s'\n", shader.name );
384 return;
385 }
386 tmi->wave.base = atof( token );
387 token = COM_ParseExt( text, qfalse );
388 if ( token[0] == 0 )
389 {
390 ri.Printf( PRINT_WARNING, "WARNING: missing tcMod turb in shader '%s'\n", shader.name );
391 return;
392 }
393 tmi->wave.amplitude = atof( token );
394 token = COM_ParseExt( text, qfalse );
395 if ( token[0] == 0 )
396 {
397 ri.Printf( PRINT_WARNING, "WARNING: missing tcMod turb in shader '%s'\n", shader.name );
398 return;
399 }
400 tmi->wave.phase = atof( token );
401 token = COM_ParseExt( text, qfalse );
402 if ( token[0] == 0 )
403 {
404 ri.Printf( PRINT_WARNING, "WARNING: missing tcMod turb in shader '%s'\n", shader.name );
405 return;
406 }
407 tmi->wave.frequency = atof( token );
408
409 tmi->type = TMOD_TURBULENT;
410 }
411 //
412 // scale
413 //
414 else if ( !Q_stricmp( token, "scale" ) )
415 {
416 token = COM_ParseExt( text, qfalse );
417 if ( token[0] == 0 )
418 {
419 ri.Printf( PRINT_WARNING, "WARNING: missing scale parms in shader '%s'\n", shader.name );
420 return;
421 }
422 tmi->scale[0] = atof( token );
423
424 token = COM_ParseExt( text, qfalse );
425 if ( token[0] == 0 )
426 {
427 ri.Printf( PRINT_WARNING, "WARNING: missing scale parms in shader '%s'\n", shader.name );
428 return;
429 }
430 tmi->scale[1] = atof( token );
431 tmi->type = TMOD_SCALE;
432 }
433 //
434 // scroll
435 //
436 else if ( !Q_stricmp( token, "scroll" ) )
437 {
438 token = COM_ParseExt( text, qfalse );
439 if ( token[0] == 0 )
440 {
441 ri.Printf( PRINT_WARNING, "WARNING: missing scale scroll parms in shader '%s'\n", shader.name );
442 return;
443 }
444 tmi->scroll[0] = atof( token );
445 token = COM_ParseExt( text, qfalse );
446 if ( token[0] == 0 )
447 {
448 ri.Printf( PRINT_WARNING, "WARNING: missing scale scroll parms in shader '%s'\n", shader.name );
449 return;
450 }
451 tmi->scroll[1] = atof( token );
452 tmi->type = TMOD_SCROLL;
453 }
454 //
455 // stretch
456 //
457 else if ( !Q_stricmp( token, "stretch" ) )
458 {
459 token = COM_ParseExt( text, qfalse );
460 if ( token[0] == 0 )
461 {
462 ri.Printf( PRINT_WARNING, "WARNING: missing stretch parms in shader '%s'\n", shader.name );
463 return;
464 }
465 tmi->wave.func = NameToGenFunc( token );
466
467 token = COM_ParseExt( text, qfalse );
468 if ( token[0] == 0 )
469 {
470 ri.Printf( PRINT_WARNING, "WARNING: missing stretch parms in shader '%s'\n", shader.name );
471 return;
472 }
473 tmi->wave.base = atof( token );
474
475 token = COM_ParseExt( text, qfalse );
476 if ( token[0] == 0 )
477 {
478 ri.Printf( PRINT_WARNING, "WARNING: missing stretch parms in shader '%s'\n", shader.name );
479 return;
480 }
481 tmi->wave.amplitude = atof( token );
482
483 token = COM_ParseExt( text, qfalse );
484 if ( token[0] == 0 )
485 {
486 ri.Printf( PRINT_WARNING, "WARNING: missing stretch parms in shader '%s'\n", shader.name );
487 return;
488 }
489 tmi->wave.phase = atof( token );
490
491 token = COM_ParseExt( text, qfalse );
492 if ( token[0] == 0 )
493 {
494 ri.Printf( PRINT_WARNING, "WARNING: missing stretch parms in shader '%s'\n", shader.name );
495 return;
496 }
497 tmi->wave.frequency = atof( token );
498
499 tmi->type = TMOD_STRETCH;
500 }
501 //
502 // transform
503 //
504 else if ( !Q_stricmp( token, "transform" ) )
505 {
506 token = COM_ParseExt( text, qfalse );
507 if ( token[0] == 0 )
508 {
509 ri.Printf( PRINT_WARNING, "WARNING: missing transform parms in shader '%s'\n", shader.name );
510 return;
511 }
512 tmi->matrix[0][0] = atof( token );
513
514 token = COM_ParseExt( text, qfalse );
515 if ( token[0] == 0 )
516 {
517 ri.Printf( PRINT_WARNING, "WARNING: missing transform parms in shader '%s'\n", shader.name );
518 return;
519 }
520 tmi->matrix[0][1] = atof( token );
521
522 token = COM_ParseExt( text, qfalse );
523 if ( token[0] == 0 )
524 {
525 ri.Printf( PRINT_WARNING, "WARNING: missing transform parms in shader '%s'\n", shader.name );
526 return;
527 }
528 tmi->matrix[1][0] = atof( token );
529
530 token = COM_ParseExt( text, qfalse );
531 if ( token[0] == 0 )
532 {
533 ri.Printf( PRINT_WARNING, "WARNING: missing transform parms in shader '%s'\n", shader.name );
534 return;
535 }
536 tmi->matrix[1][1] = atof( token );
537
538 token = COM_ParseExt( text, qfalse );
539 if ( token[0] == 0 )
540 {
541 ri.Printf( PRINT_WARNING, "WARNING: missing transform parms in shader '%s'\n", shader.name );
542 return;
543 }
544 tmi->translate[0] = atof( token );
545
546 token = COM_ParseExt( text, qfalse );
547 if ( token[0] == 0 )
548 {
549 ri.Printf( PRINT_WARNING, "WARNING: missing transform parms in shader '%s'\n", shader.name );
550 return;
551 }
552 tmi->translate[1] = atof( token );
553
554 tmi->type = TMOD_TRANSFORM;
555 }
556 //
557 // rotate
558 //
559 else if ( !Q_stricmp( token, "rotate" ) )
560 {
561 token = COM_ParseExt( text, qfalse );
562 if ( token[0] == 0 )
563 {
564 ri.Printf( PRINT_WARNING, "WARNING: missing tcMod rotate parms in shader '%s'\n", shader.name );
565 return;
566 }
567 tmi->rotateSpeed = atof( token );
568 tmi->type = TMOD_ROTATE;
569 }
570 //
571 // entityTranslate
572 //
573 else if ( !Q_stricmp( token, "entityTranslate" ) )
574 {
575 tmi->type = TMOD_ENTITY_TRANSLATE;
576 }
577 else
578 {
579 ri.Printf( PRINT_WARNING, "WARNING: unknown tcMod '%s' in shader '%s'\n", token, shader.name );
580 }
581 }
582
583
584 /*
585 ===================
586 ParseStage
587 ===================
588 */
589 static qboolean ParseStage( shaderStage_t *stage, char **text )
590 {
591 char *token;
592 int depthMaskBits = GLS_DEPTHMASK_TRUE, blendSrcBits = 0, blendDstBits = 0, atestBits = 0, depthFuncBits = 0;
593 qboolean depthMaskExplicit = qfalse;
594
595 stage->active = qtrue;
596
597 while ( 1 )
598 {
599 token = COM_ParseExt( text, qtrue );
600 if ( !token[0] )
601 {
602 ri.Printf( PRINT_WARNING, "WARNING: no matching '}' found\n" );
603 return qfalse;
604 }
605
606 if ( token[0] == '}' )
607 {
608 break;
609 }
610 //
611 // map <name>
612 //
613 else if ( !Q_stricmp( token, "map" ) )
614 {
615 token = COM_ParseExt( text, qfalse );
616 if ( !token[0] )
617 {
618 ri.Printf( PRINT_WARNING, "WARNING: missing parameter for 'map' keyword in shader '%s'\n", shader.name );
619 return qfalse;
620 }
621
622 if ( !Q_stricmp( token, "$whiteimage" ) )
623 {
624 stage->bundle[0].image[0] = tr.whiteImage;
625 continue;
626 }
627 else if ( !Q_stricmp( token, "$lightmap" ) )
628 {
629 stage->bundle[0].isLightmap = qtrue;
630 if ( shader.lightmapIndex < 0 || !tr.lightmaps ) {
631 stage->bundle[0].image[0] = tr.whiteImage;
632 } else {
633 stage->bundle[0].image[0] = tr.lightmaps[shader.lightmapIndex];
634 }
635 continue;
636 }
637 else
638 {
639 imgType_t type = IMGTYPE_COLORALPHA;
640 imgFlags_t flags = IMGFLAG_NONE;
641
642 if (!shader.noMipMaps)
643 flags |= IMGFLAG_MIPMAP;
644
645 if (!shader.noPicMip)
646 flags |= IMGFLAG_PICMIP;
647
648 stage->bundle[0].image[0] = R_FindImageFile( token, type, flags );
649
650 if ( !stage->bundle[0].image[0] )
651 {
652 ri.Printf( PRINT_WARNING, "WARNING: R_FindImageFile could not find '%s' in shader '%s'\n", token, shader.name );
653 return qfalse;
654 }
655 }
656 }
657 //
658 // clampmap <name>
659 //
660 else if ( !Q_stricmp( token, "clampmap" ) )
661 {
662 imgType_t type = IMGTYPE_COLORALPHA;
663 imgFlags_t flags = IMGFLAG_CLAMPTOEDGE;
664
665 token = COM_ParseExt( text, qfalse );
666 if ( !token[0] )
667 {
668 ri.Printf( PRINT_WARNING, "WARNING: missing parameter for 'clampmap' keyword in shader '%s'\n", shader.name );
669 return qfalse;
670 }
671
672 if (!shader.noMipMaps)
673 flags |= IMGFLAG_MIPMAP;
674
675 if (!shader.noPicMip)
676 flags |= IMGFLAG_PICMIP;
677
678 stage->bundle[0].image[0] = R_FindImageFile( token, type, flags );
679 if ( !stage->bundle[0].image[0] )
680 {
681 ri.Printf( PRINT_WARNING, "WARNING: R_FindImageFile could not find '%s' in shader '%s'\n", token, shader.name );
682 return qfalse;
683 }
684 }
685 //
686 // animMap <frequency> <image1> .... <imageN>
687 //
688 else if ( !Q_stricmp( token, "animMap" ) )
689 {
690 int totalImages = 0;
691
692 token = COM_ParseExt( text, qfalse );
693 if ( !token[0] )
694 {
695 ri.Printf( PRINT_WARNING, "WARNING: missing parameter for 'animMap' keyword in shader '%s'\n", shader.name );
696 return qfalse;
697 }
698 stage->bundle[0].imageAnimationSpeed = atof( token );
699
700 // parse up to MAX_IMAGE_ANIMATIONS animations
701 while ( 1 ) {
702 int num;
703
704 token = COM_ParseExt( text, qfalse );
705 if ( !token[0] ) {
706 break;
707 }
708 num = stage->bundle[0].numImageAnimations;
709 if ( num < MAX_IMAGE_ANIMATIONS ) {
710 imgFlags_t flags = IMGFLAG_NONE;
711
712 if (!shader.noMipMaps)
713 flags |= IMGFLAG_MIPMAP;
714
715 if (!shader.noPicMip)
716 flags |= IMGFLAG_PICMIP;
717
718 stage->bundle[0].image[num] = R_FindImageFile( token, IMGTYPE_COLORALPHA, flags );
719 if ( !stage->bundle[0].image[num] )
720 {
721 ri.Printf( PRINT_WARNING, "WARNING: R_FindImageFile could not find '%s' in shader '%s'\n", token, shader.name );
722 return qfalse;
723 }
724 stage->bundle[0].numImageAnimations++;
725 }
726 totalImages++;
727 }
728
729 if ( totalImages > MAX_IMAGE_ANIMATIONS ) {
730 ri.Printf( PRINT_WARNING, "WARNING: ignoring excess images for 'animMap' (found %d, max is %d) in shader '%s'\n",
731 totalImages, MAX_IMAGE_ANIMATIONS, shader.name );
732 }
733 }
734 else if ( !Q_stricmp( token, "videoMap" ) )
735 {
736 token = COM_ParseExt( text, qfalse );
737 if ( !token[0] )
738 {
739 ri.Printf( PRINT_WARNING, "WARNING: missing parameter for 'videoMap' keyword in shader '%s'\n", shader.name );
740 return qfalse;
741 }
742 stage->bundle[0].videoMapHandle = ri.CIN_PlayCinematic( token, 0, 0, 256, 256, (CIN_loop | CIN_silent | CIN_shader));
743 if (stage->bundle[0].videoMapHandle != -1) {
744 stage->bundle[0].isVideoMap = qtrue;
745 stage->bundle[0].image[0] = tr.scratchImage[stage->bundle[0].videoMapHandle];
746 } else {
747 ri.Printf( PRINT_WARNING, "WARNING: could not load '%s' for 'videoMap' keyword in shader '%s'\n", token, shader.name );
748 }
749 }
750 //
751 // alphafunc <func>
752 //
753 else if ( !Q_stricmp( token, "alphaFunc" ) )
754 {
755 token = COM_ParseExt( text, qfalse );
756 if ( !token[0] )
757 {
758 ri.Printf( PRINT_WARNING, "WARNING: missing parameter for 'alphaFunc' keyword in shader '%s'\n", shader.name );
759 return qfalse;
760 }
761
762 atestBits = NameToAFunc( token );
763 }
764 //
765 // depthFunc <func>
766 //
767 else if ( !Q_stricmp( token, "depthfunc" ) )
768 {
769 token = COM_ParseExt( text, qfalse );
770
771 if ( !token[0] )
772 {
773 ri.Printf( PRINT_WARNING, "WARNING: missing parameter for 'depthfunc' keyword in shader '%s'\n", shader.name );
774 return qfalse;
775 }
776
777 if ( !Q_stricmp( token, "lequal" ) )
778 {
779 depthFuncBits = 0;
780 }
781 else if ( !Q_stricmp( token, "equal" ) )
782 {
783 depthFuncBits = GLS_DEPTHFUNC_EQUAL;
784 }
785 else
786 {
787 ri.Printf( PRINT_WARNING, "WARNING: unknown depthfunc '%s' in shader '%s'\n", token, shader.name );
788 continue;
789 }
790 }
791 //
792 // detail
793 //
794 else if ( !Q_stricmp( token, "detail" ) )
795 {
796 stage->isDetail = qtrue;
797 }
798 //
799 // blendfunc <srcFactor> <dstFactor>
800 // or blendfunc <add|filter|blend>
801 //
802 else if ( !Q_stricmp( token, "blendfunc" ) )
803 {
804 token = COM_ParseExt( text, qfalse );
805 if ( token[0] == 0 )
806 {
807 ri.Printf( PRINT_WARNING, "WARNING: missing parm for blendFunc in shader '%s'\n", shader.name );
808 continue;
809 }
810 // check for "simple" blends first
811 if ( !Q_stricmp( token, "add" ) ) {
812 blendSrcBits = GLS_SRCBLEND_ONE;
813 blendDstBits = GLS_DSTBLEND_ONE;
814 } else if ( !Q_stricmp( token, "filter" ) ) {
815 blendSrcBits = GLS_SRCBLEND_DST_COLOR;
816 blendDstBits = GLS_DSTBLEND_ZERO;
817 } else if ( !Q_stricmp( token, "blend" ) ) {
818 blendSrcBits = GLS_SRCBLEND_SRC_ALPHA;
819 blendDstBits = GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA;
820 } else {
821 // complex double blends
822 blendSrcBits = NameToSrcBlendMode( token );
823
824 token = COM_ParseExt( text, qfalse );
825 if ( token[0] == 0 )
826 {
827 ri.Printf( PRINT_WARNING, "WARNING: missing parm for blendFunc in shader '%s'\n", shader.name );
828 continue;
829 }
830 blendDstBits = NameToDstBlendMode( token );
831 }
832
833 // clear depth mask for blended surfaces
834 if ( !depthMaskExplicit )
835 {
836 depthMaskBits = 0;
837 }
838 }
839 //
840 // rgbGen
841 //
842 else if ( !Q_stricmp( token, "rgbGen" ) )
843 {
844 token = COM_ParseExt( text, qfalse );
845 if ( token[0] == 0 )
846 {
847 ri.Printf( PRINT_WARNING, "WARNING: missing parameters for rgbGen in shader '%s'\n", shader.name );
848 continue;
849 }
850
851 if ( !Q_stricmp( token, "wave" ) )
852 {
853 ParseWaveForm( text, &stage->rgbWave );
854 stage->rgbGen = CGEN_WAVEFORM;
855 }
856 else if ( !Q_stricmp( token, "const" ) )
857 {
858 vec3_t color;
859
860 VectorClear( color );
861
862 ParseVector( text, 3, color );
863 stage->constantColor[0] = 255 * color[0];
864 stage->constantColor[1] = 255 * color[1];
865 stage->constantColor[2] = 255 * color[2];
866
867 stage->rgbGen = CGEN_CONST;
868 }
869 else if ( !Q_stricmp( token, "identity" ) )
870 {
871 stage->rgbGen = CGEN_IDENTITY;
872 }
873 else if ( !Q_stricmp( token, "identityLighting" ) )
874 {
875 stage->rgbGen = CGEN_IDENTITY_LIGHTING;
876 }
877 else if ( !Q_stricmp( token, "entity" ) )
878 {
879 stage->rgbGen = CGEN_ENTITY;
880 }
881 else if ( !Q_stricmp( token, "oneMinusEntity" ) )
882 {
883 stage->rgbGen = CGEN_ONE_MINUS_ENTITY;
884 }
885 else if ( !Q_stricmp( token, "vertex" ) )
886 {
887 stage->rgbGen = CGEN_VERTEX;
888 if ( stage->alphaGen == 0 ) {
889 stage->alphaGen = AGEN_VERTEX;
890 }
891 }
892 else if ( !Q_stricmp( token, "exactVertex" ) )
893 {
894 stage->rgbGen = CGEN_EXACT_VERTEX;
895 }
896 else if ( !Q_stricmp( token, "lightingDiffuse" ) )
897 {
898 stage->rgbGen = CGEN_LIGHTING_DIFFUSE;
899 }
900 else if ( !Q_stricmp( token, "oneMinusVertex" ) )
901 {
902 stage->rgbGen = CGEN_ONE_MINUS_VERTEX;
903 }
904 else
905 {
906 ri.Printf( PRINT_WARNING, "WARNING: unknown rgbGen parameter '%s' in shader '%s'\n", token, shader.name );
907 continue;
908 }
909 }
910 //
911 // alphaGen
912 //
913 else if ( !Q_stricmp( token, "alphaGen" ) )
914 {
915 token = COM_ParseExt( text, qfalse );
916 if ( token[0] == 0 )
917 {
918 ri.Printf( PRINT_WARNING, "WARNING: missing parameters for alphaGen in shader '%s'\n", shader.name );
919 continue;
920 }
921
922 if ( !Q_stricmp( token, "wave" ) )
923 {
924 ParseWaveForm( text, &stage->alphaWave );
925 stage->alphaGen = AGEN_WAVEFORM;
926 }
927 else if ( !Q_stricmp( token, "const" ) )
928 {
929 token = COM_ParseExt( text, qfalse );
930 stage->constantColor[3] = 255 * atof( token );
931 stage->alphaGen = AGEN_CONST;
932 }
933 else if ( !Q_stricmp( token, "identity" ) )
934 {
935 stage->alphaGen = AGEN_IDENTITY;
936 }
937 else if ( !Q_stricmp( token, "entity" ) )
938 {
939 stage->alphaGen = AGEN_ENTITY;
940 }
941 else if ( !Q_stricmp( token, "oneMinusEntity" ) )
942 {
943 stage->alphaGen = AGEN_ONE_MINUS_ENTITY;
944 }
945 else if ( !Q_stricmp( token, "vertex" ) )
946 {
947 stage->alphaGen = AGEN_VERTEX;
948 }
949 else if ( !Q_stricmp( token, "lightingSpecular" ) )
950 {
951 stage->alphaGen = AGEN_LIGHTING_SPECULAR;
952 }
953 else if ( !Q_stricmp( token, "oneMinusVertex" ) )
954 {
955 stage->alphaGen = AGEN_ONE_MINUS_VERTEX;
956 }
957 else if ( !Q_stricmp( token, "portal" ) )
958 {
959 stage->alphaGen = AGEN_PORTAL;
960 token = COM_ParseExt( text, qfalse );
961 if ( token[0] == 0 )
962 {
963 shader.portalRange = 256;
964 ri.Printf( PRINT_WARNING, "WARNING: missing range parameter for alphaGen portal in shader '%s', defaulting to 256\n", shader.name );
965 }
966 else
967 {
968 shader.portalRange = atof( token );
969 }
970 }
971 else
972 {
973 ri.Printf( PRINT_WARNING, "WARNING: unknown alphaGen parameter '%s' in shader '%s'\n", token, shader.name );
974 continue;
975 }
976 }
977 //
978 // tcGen <function>
979 //
980 else if ( !Q_stricmp(token, "texgen") || !Q_stricmp( token, "tcGen" ) )
981 {
982 token = COM_ParseExt( text, qfalse );
983 if ( token[0] == 0 )
984 {
985 ri.Printf( PRINT_WARNING, "WARNING: missing texgen parm in shader '%s'\n", shader.name );
986 continue;
987 }
988
989 if ( !Q_stricmp( token, "environment" ) )
990 {
991 stage->bundle[0].tcGen = TCGEN_ENVIRONMENT_MAPPED;
992 }
993 else if ( !Q_stricmp( token, "lightmap" ) )
994 {
995 stage->bundle[0].tcGen = TCGEN_LIGHTMAP;
996 }
997 else if ( !Q_stricmp( token, "texture" ) || !Q_stricmp( token, "base" ) )
998 {
999 stage->bundle[0].tcGen = TCGEN_TEXTURE;
1000 }
1001 else if ( !Q_stricmp( token, "vector" ) )
1002 {
1003 ParseVector( text, 3, stage->bundle[0].tcGenVectors[0] );
1004 ParseVector( text, 3, stage->bundle[0].tcGenVectors[1] );
1005
1006 stage->bundle[0].tcGen = TCGEN_VECTOR;
1007 }
1008 else
1009 {
1010 ri.Printf( PRINT_WARNING, "WARNING: unknown texgen parm in shader '%s'\n", shader.name );
1011 }
1012 }
1013 //
1014 // tcMod <type> <...>
1015 //
1016 else if ( !Q_stricmp( token, "tcMod" ) )
1017 {
1018 char buffer[1024] = "";
1019
1020 while ( 1 )
1021 {
1022 token = COM_ParseExt( text, qfalse );
1023 if ( token[0] == 0 )
1024 break;
1025 Q_strcat( buffer, sizeof (buffer), token );
1026 Q_strcat( buffer, sizeof (buffer), " " );
1027 }
1028
1029 ParseTexMod( buffer, stage );
1030
1031 continue;
1032 }
1033 //
1034 // depthmask
1035 //
1036 else if ( !Q_stricmp( token, "depthwrite" ) )
1037 {
1038 depthMaskBits = GLS_DEPTHMASK_TRUE;
1039 depthMaskExplicit = qtrue;
1040
1041 continue;
1042 }
1043 else
1044 {
1045 ri.Printf( PRINT_WARNING, "WARNING: unknown parameter '%s' in shader '%s'\n", token, shader.name );
1046 return qfalse;
1047 }
1048 }
1049
1050 //
1051 // if cgen isn't explicitly specified, use either identity or identitylighting
1052 //
1053 if ( stage->rgbGen == CGEN_BAD ) {
1054 if ( blendSrcBits == 0 ||
1055 blendSrcBits == GLS_SRCBLEND_ONE ||
1056 blendSrcBits == GLS_SRCBLEND_SRC_ALPHA ) {
1057 stage->rgbGen = CGEN_IDENTITY_LIGHTING;
1058 } else {
1059 stage->rgbGen = CGEN_IDENTITY;
1060 }
1061 }
1062
1063
1064 //
1065 // implicitly assume that a GL_ONE GL_ZERO blend mask disables blending
1066 //
1067 if ( ( blendSrcBits == GLS_SRCBLEND_ONE ) &&
1068 ( blendDstBits == GLS_DSTBLEND_ZERO ) )
1069 {
1070 blendDstBits = blendSrcBits = 0;
1071 depthMaskBits = GLS_DEPTHMASK_TRUE;
1072 }
1073
1074 // decide which agens we can skip
1075 if ( stage->alphaGen == AGEN_IDENTITY ) {
1076 if ( stage->rgbGen == CGEN_IDENTITY
1077 || stage->rgbGen == CGEN_LIGHTING_DIFFUSE ) {
1078 stage->alphaGen = AGEN_SKIP;
1079 }
1080 }
1081
1082 //
1083 // compute state bits
1084 //
1085 stage->stateBits = depthMaskBits |
1086 blendSrcBits | blendDstBits |
1087 atestBits |
1088 depthFuncBits;
1089
1090 return qtrue;
1091 }
1092
1093 /*
1094 ===============
1095 ParseDeform
1096
1097 deformVertexes wave <spread> <waveform> <base> <amplitude> <phase> <frequency>
1098 deformVertexes normal <frequency> <amplitude>
1099 deformVertexes move <vector> <waveform> <base> <amplitude> <phase> <frequency>
1100 deformVertexes bulge <bulgeWidth> <bulgeHeight> <bulgeSpeed>
1101 deformVertexes projectionShadow
1102 deformVertexes autoSprite
1103 deformVertexes autoSprite2
1104 deformVertexes text[0-7]
1105 ===============
1106 */
1107 static void ParseDeform( char **text ) {
1108 char *token;
1109 deformStage_t *ds;
1110
1111 token = COM_ParseExt( text, qfalse );
1112 if ( token[0] == 0 )
1113 {
1114 ri.Printf( PRINT_WARNING, "WARNING: missing deform parm in shader '%s'\n", shader.name );
1115 return;
1116 }
1117
1118 if ( shader.numDeforms == MAX_SHADER_DEFORMS ) {
1119 ri.Printf( PRINT_WARNING, "WARNING: MAX_SHADER_DEFORMS in '%s'\n", shader.name );
1120 return;
1121 }
1122
1123 ds = &shader.deforms[ shader.numDeforms ];
1124 shader.numDeforms++;
1125
1126 if ( !Q_stricmp( token, "projectionShadow" ) ) {
1127 ds->deformation = DEFORM_PROJECTION_SHADOW;
1128 return;
1129 }
1130
1131 if ( !Q_stricmp( token, "autosprite" ) ) {
1132 ds->deformation = DEFORM_AUTOSPRITE;
1133 return;
1134 }
1135
1136 if ( !Q_stricmp( token, "autosprite2" ) ) {
1137 ds->deformation = DEFORM_AUTOSPRITE2;
1138 return;
1139 }
1140
1141 if ( !Q_stricmpn( token, "text", 4 ) ) {
1142 int n;
1143
1144 n = token[4] - '0';
1145 if ( n < 0 || n > 7 ) {
1146 n = 0;
1147 }
1148 ds->deformation = DEFORM_TEXT0 + n;
1149 return;
1150 }
1151
1152 if ( !Q_stricmp( token, "bulge" ) ) {
1153 token = COM_ParseExt( text, qfalse );
1154 if ( token[0] == 0 )
1155 {
1156 ri.Printf( PRINT_WARNING, "WARNING: missing deformVertexes bulge parm in shader '%s'\n", shader.name );
1157 return;
1158 }
1159 ds->bulgeWidth = atof( token );
1160
1161 token = COM_ParseExt( text, qfalse );
1162 if ( token[0] == 0 )
1163 {
1164 ri.Printf( PRINT_WARNING, "WARNING: missing deformVertexes bulge parm in shader '%s'\n", shader.name );
1165 return;
1166 }
1167 ds->bulgeHeight = atof( token );
1168
1169 token = COM_ParseExt( text, qfalse );
1170 if ( token[0] == 0 )
1171 {
1172 ri.Printf( PRINT_WARNING, "WARNING: missing deformVertexes bulge parm in shader '%s'\n", shader.name );
1173 return;
1174 }
1175 ds->bulgeSpeed = atof( token );
1176
1177 ds->deformation = DEFORM_BULGE;
1178 return;
1179 }
1180
1181 if ( !Q_stricmp( token, "wave" ) )
1182 {
1183 token = COM_ParseExt( text, qfalse );
1184 if ( token[0] == 0 )
1185 {
1186 ri.Printf( PRINT_WARNING, "WARNING: missing deformVertexes parm in shader '%s'\n", shader.name );
1187 return;
1188 }
1189
1190 if ( atof( token ) != 0 )
1191 {
1192 ds->deformationSpread = 1.0f / atof( token );
1193 }
1194 else
1195 {
1196 ds->deformationSpread = 100.0f;
1197 ri.Printf( PRINT_WARNING, "WARNING: illegal div value of 0 in deformVertexes command for shader '%s'\n", shader.name );
1198 }
1199
1200 ParseWaveForm( text, &ds->deformationWave );
1201 ds->deformation = DEFORM_WAVE;
1202 return;
1203 }
1204
1205 if ( !Q_stricmp( token, "normal" ) )
1206 {
1207 token = COM_ParseExt( text, qfalse );
1208 if ( token[0] == 0 )
1209 {
1210 ri.Printf( PRINT_WARNING, "WARNING: missing deformVertexes parm in shader '%s'\n", shader.name );
1211 return;
1212 }
1213 ds->deformationWave.amplitude = atof( token );
1214
1215 token = COM_ParseExt( text, qfalse );
1216 if ( token[0] == 0 )
1217 {
1218 ri.Printf( PRINT_WARNING, "WARNING: missing deformVertexes parm in shader '%s'\n", shader.name );
1219 return;
1220 }
1221 ds->deformationWave.frequency = atof( token );
1222
1223 ds->deformation = DEFORM_NORMALS;
1224 return;
1225 }
1226
1227 if ( !Q_stricmp( token, "move" ) ) {
1228 int i;
1229
1230 for ( i = 0 ; i < 3 ; i++ ) {
1231 token = COM_ParseExt( text, qfalse );
1232 if ( token[0] == 0 ) {
1233 ri.Printf( PRINT_WARNING, "WARNING: missing deformVertexes parm in shader '%s'\n", shader.name );
1234 return;
1235 }
1236 ds->moveVector[i] = atof( token );
1237 }
1238
1239 ParseWaveForm( text, &ds->deformationWave );
1240 ds->deformation = DEFORM_MOVE;
1241 return;
1242 }
1243
1244 ri.Printf( PRINT_WARNING, "WARNING: unknown deformVertexes subtype '%s' found in shader '%s'\n", token, shader.name );
1245 }
1246
1247
1248 /*
1249 ===============
1250 ParseSkyParms
1251
1252 skyParms <outerbox> <cloudheight> <innerbox>
1253 ===============
1254 */
1255 static void ParseSkyParms( char **text ) {
1256 char *token;
1257 static char *suf[6] = {"rt", "bk", "lf", "ft", "up", "dn"};
1258 char pathname[MAX_QPATH];
1259 int i;
1260 imgFlags_t imgFlags = IMGFLAG_MIPMAP | IMGFLAG_PICMIP;
1261
1262 // outerbox
1263 token = COM_ParseExt( text, qfalse );
1264 if ( token[0] == 0 ) {
1265 ri.Printf( PRINT_WARNING, "WARNING: 'skyParms' missing parameter in shader '%s'\n", shader.name );
1266 return;
1267 }
1268 if ( strcmp( token, "-" ) ) {
1269 for (i=0 ; i<6 ; i++) {
1270 Com_sprintf( pathname, sizeof(pathname), "%s_%s.tga"
1271 , token, suf[i] );
1272 shader.sky.outerbox[i] = R_FindImageFile( ( char * ) pathname, IMGTYPE_COLORALPHA, imgFlags | IMGFLAG_CLAMPTOEDGE );
1273
1274 if ( !shader.sky.outerbox[i] ) {
1275 shader.sky.outerbox[i] = tr.defaultImage;
1276 }
1277 }
1278 }
1279
1280 // cloudheight
1281 token = COM_ParseExt( text, qfalse );
1282 if ( token[0] == 0 ) {
1283 ri.Printf( PRINT_WARNING, "WARNING: 'skyParms' missing parameter in shader '%s'\n", shader.name );
1284 return;
1285 }
1286 shader.sky.cloudHeight = atof( token );
1287 if ( !shader.sky.cloudHeight ) {
1288 shader.sky.cloudHeight = 512;
1289 }
1290 R_InitSkyTexCoords( shader.sky.cloudHeight );
1291
1292
1293 // innerbox
1294 token = COM_ParseExt( text, qfalse );
1295 if ( token[0] == 0 ) {
1296 ri.Printf( PRINT_WARNING, "WARNING: 'skyParms' missing parameter in shader '%s'\n", shader.name );
1297 return;
1298 }
1299 if ( strcmp( token, "-" ) ) {
1300 for (i=0 ; i<6 ; i++) {
1301 Com_sprintf( pathname, sizeof(pathname), "%s_%s.tga"
1302 , token, suf[i] );
1303 shader.sky.innerbox[i] = R_FindImageFile( ( char * ) pathname, IMGTYPE_COLORALPHA, imgFlags );
1304 if ( !shader.sky.innerbox[i] ) {
1305 shader.sky.innerbox[i] = tr.defaultImage;
1306 }
1307 }
1308 }
1309
1310 shader.isSky = qtrue;
1311 }
1312
1313
1314 /*
1315 =================
1316 ParseSort
1317 =================
1318 */
1319 void ParseSort( char **text ) {
1320 char *token;
1321
1322 token = COM_ParseExt( text, qfalse );
1323 if ( token[0] == 0 ) {
1324 ri.Printf( PRINT_WARNING, "WARNING: missing sort parameter in shader '%s'\n", shader.name );
1325 return;
1326 }
1327
1328 if ( !Q_stricmp( token, "portal" ) ) {
1329 shader.sort = SS_PORTAL;
1330 } else if ( !Q_stricmp( token, "sky" ) ) {
1331 shader.sort = SS_ENVIRONMENT;
1332 } else if ( !Q_stricmp( token, "opaque" ) ) {
1333 shader.sort = SS_OPAQUE;
1334 }else if ( !Q_stricmp( token, "decal" ) ) {
1335 shader.sort = SS_DECAL;
1336 } else if ( !Q_stricmp( token, "seeThrough" ) ) {
1337 shader.sort = SS_SEE_THROUGH;
1338 } else if ( !Q_stricmp( token, "banner" ) ) {
1339 shader.sort = SS_BANNER;
1340 } else if ( !Q_stricmp( token, "additive" ) ) {
1341 shader.sort = SS_BLEND1;
1342 } else if ( !Q_stricmp( token, "nearest" ) ) {
1343 shader.sort = SS_NEAREST;
1344 } else if ( !Q_stricmp( token, "underwater" ) ) {
1345 shader.sort = SS_UNDERWATER;
1346 } else {
1347 shader.sort = atof( token );
1348 }
1349 }
1350
1351
1352
1353 // this table is also present in q3map
1354
1355 typedef struct {
1356 char *name;
1357 int clearSolid, surfaceFlags, contents;
1358 } infoParm_t;
1359
1360 infoParm_t infoParms[] = {
1361 // server relevant contents
1362 {"water", 1, 0, CONTENTS_WATER },
1363 {"slime", 1, 0, CONTENTS_SLIME }, // mildly damaging
1364 {"lava", 1, 0, CONTENTS_LAVA }, // very damaging
1365 {"playerclip", 1, 0, CONTENTS_PLAYERCLIP },
1366 {"monsterclip", 1, 0, CONTENTS_MONSTERCLIP },
1367 {"nodrop", 1, 0, CONTENTS_NODROP }, // don't drop items or leave bodies (death fog, lava, etc)
1368 {"nonsolid", 1, SURF_NONSOLID, 0}, // clears the solid flag
1369
1370 // utility relevant attributes
1371 {"origin", 1, 0, CONTENTS_ORIGIN }, // center of rotating brushes
1372 {"trans", 0, 0, CONTENTS_TRANSLUCENT }, // don't eat contained surfaces
1373 {"detail", 0, 0, CONTENTS_DETAIL }, // don't include in structural bsp
1374 {"structural", 0, 0, CONTENTS_STRUCTURAL }, // force into structural bsp even if trnas
1375 {"areaportal", 1, 0, CONTENTS_AREAPORTAL }, // divides areas
1376 {"clusterportal", 1,0, CONTENTS_CLUSTERPORTAL }, // for bots
1377 {"donotenter", 1, 0, CONTENTS_DONOTENTER }, // for bots
1378
1379 {"fog", 1, 0, CONTENTS_FOG}, // carves surfaces entering
1380 {"sky", 0, SURF_SKY, 0 }, // emit light from an environment map
1381 {"lightfilter", 0, SURF_LIGHTFILTER, 0 }, // filter light going through it
1382 {"alphashadow", 0, SURF_ALPHASHADOW, 0 }, // test light on a per-pixel basis
1383 {"hint", 0, SURF_HINT, 0 }, // use as a primary splitter
1384
1385 // server attributes
1386 {"slick", 0, SURF_SLICK, 0 },
1387 {"noimpact", 0, SURF_NOIMPACT, 0 }, // don't make impact explosions or marks
1388 {"nomarks", 0, SURF_NOMARKS, 0 }, // don't make impact marks, but still explode
1389 {"ladder", 0, SURF_LADDER, 0 },
1390 {"nodamage", 0, SURF_NODAMAGE, 0 },
1391 {"metalsteps", 0, SURF_METALSTEPS,0 },
1392 {"flesh", 0, SURF_FLESH, 0 },
1393 {"nosteps", 0, SURF_NOSTEPS, 0 },
1394
1395 // drawsurf attributes
1396 {"nodraw", 0, SURF_NODRAW, 0 }, // don't generate a drawsurface (or a lightmap)
1397 {"pointlight", 0, SURF_POINTLIGHT, 0 }, // sample lighting at vertexes
1398 {"nolightmap", 0, SURF_NOLIGHTMAP,0 }, // don't generate a lightmap
1399 {"nodlight", 0, SURF_NODLIGHT, 0 }, // don't ever add dynamic lights
1400 {"dust", 0, SURF_DUST, 0} // leave a dust trail when walking on this surface
1401 };
1402
1403
1404 /*
1405 ===============
1406 ParseSurfaceParm
1407
1408 surfaceparm <name>
1409 ===============
1410 */
1411 static void ParseSurfaceParm( char **text ) {
1412 char *token;
1413 int numInfoParms = ARRAY_LEN( infoParms );
1414 int i;
1415
1416 token = COM_ParseExt( text, qfalse );
1417 for ( i = 0 ; i < numInfoParms ; i++ ) {
1418 if ( !Q_stricmp( token, infoParms[i].name ) ) {
1419 shader.surfaceFlags |= infoParms[i].surfaceFlags;
1420 shader.contentFlags |= infoParms[i].contents;
1421 #if 0
1422 if ( infoParms[i].clearSolid ) {
1423 si->contents &= ~CONTENTS_SOLID;
1424 }
1425 #endif
1426 break;
1427 }
1428 }
1429 }
1430
1431 /*
1432 =================
1433 ParseShader
1434
1435 The current text pointer is at the explicit text definition of the
1436 shader. Parse it into the global shader variable. Later functions
1437 will optimize it.
1438 =================
1439 */
1440 static qboolean ParseShader( char **text )
1441 {
1442 char *token;
1443 int s;
1444
1445 s = 0;
1446
1447 token = COM_ParseExt( text, qtrue );
1448 if ( token[0] != '{' )
1449 {
1450 ri.Printf( PRINT_WARNING, "WARNING: expecting '{', found '%s' instead in shader '%s'\n", token, shader.name );
1451 return qfalse;
1452 }
1453
1454 while ( 1 )
1455 {
1456 token = COM_ParseExt( text, qtrue );
1457 if ( !token[0] )
1458 {
1459 ri.Printf( PRINT_WARNING, "WARNING: no concluding '}' in shader %s\n", shader.name );
1460 return qfalse;
1461 }
1462
1463 // end of shader definition
1464 if ( token[0] == '}' )
1465 {
1466 break;
1467 }
1468 // stage definition
1469 else if ( token[0] == '{' )
1470 {
1471 if ( s >= MAX_SHADER_STAGES ) {
1472 ri.Printf( PRINT_WARNING, "WARNING: too many stages in shader %s (max is %i)\n", shader.name, MAX_SHADER_STAGES );
1473 return qfalse;
1474 }
1475
1476 if ( !ParseStage( &stages[s], text ) )
1477 {
1478 return qfalse;
1479 }
1480 stages[s].active = qtrue;
1481 s++;
1482
1483 continue;
1484 }
1485 // skip stuff that only the QuakeEdRadient needs
1486 else if ( !Q_stricmpn( token, "qer", 3 ) ) {
1487 SkipRestOfLine( text );
1488 continue;
1489 }
1490 // sun parms
1491 else if ( !Q_stricmp( token, "q3map_sun" ) || !Q_stricmp( token, "q3map_sunExt" ) ) {
1492 float a, b;
1493
1494 token = COM_ParseExt( text, qfalse );
1495 tr.sunLight[0] = atof( token );
1496 token = COM_ParseExt( text, qfalse );
1497 tr.sunLight[1] = atof( token );
1498 token = COM_ParseExt( text, qfalse );
1499 tr.sunLight[2] = atof( token );
1500
1501 VectorNormalize( tr.sunLight );
1502
1503 token = COM_ParseExt( text, qfalse );
1504 a = atof( token );
1505 VectorScale( tr.sunLight, a, tr.sunLight);
1506
1507 token = COM_ParseExt( text, qfalse );
1508 a = atof( token );
1509 a = a / 180 * M_PI;
1510
1511 token = COM_ParseExt( text, qfalse );
1512 b = atof( token );
1513 b = b / 180 * M_PI;
1514
1515 tr.sunDirection[0] = cos( a ) * cos( b );
1516 tr.sunDirection[1] = sin( a ) * cos( b );
1517 tr.sunDirection[2] = sin( b );
1518
1519 SkipRestOfLine( text );
1520 continue;
1521 }
1522 else if ( !Q_stricmp( token, "deformVertexes" ) ) {
1523 ParseDeform( text );
1524 continue;
1525 }
1526 else if ( !Q_stricmp( token, "tesssize" ) ) {
1527 SkipRestOfLine( text );
1528 continue;
1529 }
1530 else if ( !Q_stricmp( token, "clampTime" ) ) {
1531 token = COM_ParseExt( text, qfalse );
1532 if (token[0]) {
1533 shader.clampTime = atof(token);
1534 }
1535 }
1536 // skip stuff that only the q3map needs
1537 else if ( !Q_stricmpn( token, "q3map", 5 ) ) {
1538 SkipRestOfLine( text );
1539 continue;
1540 }
1541 // skip stuff that only q3map or the server needs
1542 else if ( !Q_stricmp( token, "surfaceParm" ) ) {
1543 ParseSurfaceParm( text );
1544 continue;
1545 }
1546 // no mip maps
1547 else if ( !Q_stricmp( token, "nomipmaps" ) )
1548 {
1549 shader.noMipMaps = qtrue;
1550 shader.noPicMip = qtrue;
1551 continue;
1552 }
1553 // no picmip adjustment
1554 else if ( !Q_stricmp( token, "nopicmip" ) )
1555 {
1556 shader.noPicMip = qtrue;
1557 continue;
1558 }
1559 // polygonOffset
1560 else if ( !Q_stricmp( token, "polygonOffset" ) )
1561 {
1562 shader.polygonOffset = qtrue;
1563 continue;
1564 }
1565 // entityMergable, allowing sprite surfaces from multiple entities
1566 // to be merged into one batch. This is a savings for smoke
1567 // puffs and blood, but can't be used for anything where the
1568 // shader calcs (not the surface function) reference the entity color or scroll
1569 else if ( !Q_stricmp( token, "entityMergable" ) )
1570 {
1571 shader.entityMergable = qtrue;
1572 continue;
1573 }
1574 // fogParms
1575 else if ( !Q_stricmp( token, "fogParms" ) )
1576 {
1577 if ( !ParseVector( text, 3, shader.fogParms.color ) ) {
1578 return qfalse;
1579 }
1580
1581 if ( r_greyscale->integer )
1582 {
1583 float luminance;
1584
1585 luminance = LUMA( shader.fogParms.color[0], shader.fogParms.color[1], shader.fogParms.color[2] );
1586 VectorSet( shader.fogParms.color, luminance, luminance, luminance );
1587 }
1588 else if ( r_greyscale->value )
1589 {
1590 float luminance;
1591
1592 luminance = LUMA( shader.fogParms.color[0], shader.fogParms.color[1], shader.fogParms.color[2] );
1593 shader.fogParms.color[0] = LERP( shader.fogParms.color[0], luminance, r_greyscale->value );
1594 shader.fogParms.color[1] = LERP( shader.fogParms.color[1], luminance, r_greyscale->value );
1595 shader.fogParms.color[2] = LERP( shader.fogParms.color[2], luminance, r_greyscale->value );
1596 }
1597
1598 token = COM_ParseExt( text, qfalse );
1599 if ( !token[0] )
1600 {
1601 ri.Printf( PRINT_WARNING, "WARNING: missing parm for 'fogParms' keyword in shader '%s'\n", shader.name );
1602 continue;
1603 }
1604 shader.fogParms.depthForOpaque = atof( token );
1605
1606 // skip any old gradient directions
1607 SkipRestOfLine( text );
1608 continue;
1609 }
1610 // portal
1611 else if ( !Q_stricmp(token, "portal") )
1612 {
1613 shader.sort = SS_PORTAL;
1614 continue;
1615 }
1616 // skyparms <cloudheight> <outerbox> <innerbox>
1617 else if ( !Q_stricmp( token, "skyparms" ) )
1618 {
1619 ParseSkyParms( text );
1620 continue;
1621 }
1622 // light <value> determines flaring in q3map, not needed here
1623 else if ( !Q_stricmp(token, "light") )
1624 {
1625 (void)COM_ParseExt( text, qfalse );
1626 continue;
1627 }
1628 // cull <face>
1629 else if ( !Q_stricmp( token, "cull") )
1630 {
1631 token = COM_ParseExt( text, qfalse );
1632 if ( token[0] == 0 )
1633 {
1634 ri.Printf( PRINT_WARNING, "WARNING: missing cull parms in shader '%s'\n", shader.name );
1635 continue;
1636 }
1637
1638 if ( !Q_stricmp( token, "none" ) || !Q_stricmp( token, "twosided" ) || !Q_stricmp( token, "disable" ) )
1639 {
1640 shader.cullType = CT_TWO_SIDED;
1641 }
1642 else if ( !Q_stricmp( token, "back" ) || !Q_stricmp( token, "backside" ) || !Q_stricmp( token, "backsided" ) )
1643 {
1644 shader.cullType = CT_BACK_SIDED;
1645 }
1646 else
1647 {
1648 ri.Printf( PRINT_WARNING, "WARNING: invalid cull parm '%s' in shader '%s'\n", token, shader.name );
1649 }
1650 continue;
1651 }
1652 // sort
1653 else if ( !Q_stricmp( token, "sort" ) )
1654 {
1655 ParseSort( text );
1656 continue;
1657 }
1658 else
1659 {
1660 ri.Printf( PRINT_WARNING, "WARNING: unknown general shader parameter '%s' in '%s'\n", token, shader.name );
1661 return qfalse;
1662 }
1663 }
1664
1665 //
1666 // ignore shaders that don't have any stages, unless it is a sky or fog
1667 //
1668 if ( s == 0 && !shader.isSky && !(shader.contentFlags & CONTENTS_FOG ) ) {
1669 return qfalse;
1670 }
1671
1672 shader.explicitlyDefined = qtrue;
1673
1674 return qtrue;
1675 }
1676
1677 /*
1678 ========================================================================================
1679
1680 SHADER OPTIMIZATION AND FOGGING
1681
1682 ========================================================================================
1683 */
1684
1685 /*
1686 ===================
1687 ComputeStageIteratorFunc
1688
1689 See if we can use on of the simple fastpath stage functions,
1690 otherwise set to the generic stage function
1691 ===================
1692 */
1693 static void ComputeStageIteratorFunc( void )
1694 {
1695 shader.optimalStageIteratorFunc = RB_StageIteratorGeneric;
1696
1697 //
1698 // see if this should go into the sky path
1699 //
1700 if ( shader.isSky )
1701 {
1702 shader.optimalStageIteratorFunc = RB_StageIteratorSky;
1703 return;
1704 }
1705
1706 if ( r_ignoreFastPath->integer )
1707 {
1708 return;
1709 }
1710
1711 //
1712 // see if this can go into the vertex lit fast path
1713 //
1714 if ( shader.numUnfoggedPasses == 1 )
1715 {
1716 if ( stages[0].rgbGen == CGEN_LIGHTING_DIFFUSE )
1717 {
1718 if ( stages[0].alphaGen == AGEN_IDENTITY )
1719 {
1720 if ( stages[0].bundle[0].tcGen == TCGEN_TEXTURE )
1721 {
1722 if ( !shader.polygonOffset )
1723 {
1724 if ( !shader.multitextureEnv )
1725 {
1726 if ( !shader.numDeforms )
1727 {
1728 shader.optimalStageIteratorFunc = RB_StageIteratorVertexLitTexture;
1729 return;
1730 }
1731 }
1732 }
1733 }
1734 }
1735 }
1736 }
1737
1738 //
1739 // see if this can go into an optimized LM, multitextured path
1740 //
1741 if ( shader.numUnfoggedPasses == 1 )
1742 {
1743 if ( ( stages[0].rgbGen == CGEN_IDENTITY ) && ( stages[0].alphaGen == AGEN_IDENTITY ) )
1744 {
1745 if ( stages[0].bundle[0].tcGen == TCGEN_TEXTURE &&
1746 stages[0].bundle[1].tcGen == TCGEN_LIGHTMAP )
1747 {
1748 if ( !shader.polygonOffset )
1749 {
1750 if ( !shader.numDeforms )
1751 {
1752 if ( shader.multitextureEnv )
1753 {
1754 shader.optimalStageIteratorFunc = RB_StageIteratorLightmappedMultitexture;
1755 }
1756 }
1757 }
1758 }
1759 }
1760 }
1761 }
1762
1763 typedef struct {
1764 int blendA;
1765 int blendB;
1766
1767 int multitextureEnv;
1768 int multitextureBlend;
1769 } collapse_t;
1770
1771 static collapse_t collapse[] = {
1772 { 0, GLS_DSTBLEND_SRC_COLOR | GLS_SRCBLEND_ZERO,
1773 GL_MODULATE, 0 },
1774
1775 { 0, GLS_DSTBLEND_ZERO | GLS_SRCBLEND_DST_COLOR,
1776 GL_MODULATE, 0 },
1777
1778 { GLS_DSTBLEND_ZERO | GLS_SRCBLEND_DST_COLOR, GLS_DSTBLEND_ZERO | GLS_SRCBLEND_DST_COLOR,
1779 GL_MODULATE, GLS_DSTBLEND_ZERO | GLS_SRCBLEND_DST_COLOR },
1780
1781 { GLS_DSTBLEND_SRC_COLOR | GLS_SRCBLEND_ZERO, GLS_DSTBLEND_ZERO | GLS_SRCBLEND_DST_COLOR,
1782 GL_MODULATE, GLS_DSTBLEND_ZERO | GLS_SRCBLEND_DST_COLOR },
1783
1784 { GLS_DSTBLEND_ZERO | GLS_SRCBLEND_DST_COLOR, GLS_DSTBLEND_SRC_COLOR | GLS_SRCBLEND_ZERO,
1785 GL_MODULATE, GLS_DSTBLEND_ZERO | GLS_SRCBLEND_DST_COLOR },
1786
1787 { GLS_DSTBLEND_SRC_COLOR | GLS_SRCBLEND_ZERO, GLS_DSTBLEND_SRC_COLOR | GLS_SRCBLEND_ZERO,
1788 GL_MODULATE, GLS_DSTBLEND_ZERO | GLS_SRCBLEND_DST_COLOR },
1789
1790 { 0, GLS_DSTBLEND_ONE | GLS_SRCBLEND_ONE,
1791 GL_ADD, 0 },
1792
1793 { GLS_DSTBLEND_ONE | GLS_SRCBLEND_ONE, GLS_DSTBLEND_ONE | GLS_SRCBLEND_ONE,
1794 GL_ADD, GLS_DSTBLEND_ONE | GLS_SRCBLEND_ONE },
1795 #if 0
1796 { 0, GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA | GLS_SRCBLEND_SRC_ALPHA,
1797 GL_DECAL, 0 },
1798 #endif
1799 { -1 }
1800 };
1801
1802 /*
1803 ================
1804 CollapseMultitexture
1805
1806 Attempt to combine two stages into a single multitexture stage
1807 FIXME: I think modulated add + modulated add collapses incorrectly
1808 =================
1809 */
1810 static qboolean CollapseMultitexture( void ) {
1811 int abits, bbits;
1812 int i;
1813 textureBundle_t tmpBundle;
1814
1815 if ( !qglActiveTextureARB ) {
1816 return qfalse;
1817 }
1818
1819 // make sure both stages are active
1820 if ( !stages[0].active || !stages[1].active ) {
1821 return qfalse;
1822 }
1823
1824 // on voodoo2, don't combine different tmus
1825 if ( glConfig.driverType == GLDRV_VOODOO ) {
1826 if ( stages[0].bundle[0].image[0]->TMU ==
1827 stages[1].bundle[0].image[0]->TMU ) {
1828 return qfalse;
1829 }
1830 }
1831
1832 abits = stages[0].stateBits;
1833 bbits = stages[1].stateBits;
1834
1835 // make sure that both stages have identical state other than blend modes
1836 if ( ( abits & ~( GLS_DSTBLEND_BITS | GLS_SRCBLEND_BITS | GLS_DEPTHMASK_TRUE ) ) !=
1837 ( bbits & ~( GLS_DSTBLEND_BITS | GLS_SRCBLEND_BITS | GLS_DEPTHMASK_TRUE ) ) ) {
1838 return qfalse;
1839 }
1840
1841 abits &= ( GLS_DSTBLEND_BITS | GLS_SRCBLEND_BITS );
1842 bbits &= ( GLS_DSTBLEND_BITS | GLS_SRCBLEND_BITS );
1843
1844 // search for a valid multitexture blend function
1845 for ( i = 0; collapse[i].blendA != -1 ; i++ ) {
1846 if ( abits == collapse[i].blendA
1847 && bbits == collapse[i].blendB ) {
1848 break;
1849 }
1850 }
1851
1852 // nothing found
1853 if ( collapse[i].blendA == -1 ) {
1854 return qfalse;
1855 }
1856
1857 // GL_ADD is a separate extension
1858 if ( collapse[i].multitextureEnv == GL_ADD && !glConfig.textureEnvAddAvailable ) {
1859 return qfalse;
1860 }
1861
1862 // make sure waveforms have identical parameters
1863 if ( ( stages[0].rgbGen != stages[1].rgbGen ) ||
1864 ( stages[0].alphaGen != stages[1].alphaGen ) ) {
1865 return qfalse;
1866 }
1867
1868 // an add collapse can only have identity colors
1869 if ( collapse[i].multitextureEnv == GL_ADD && stages[0].rgbGen != CGEN_IDENTITY ) {
1870 return qfalse;
1871 }
1872
1873 if ( stages[0].rgbGen == CGEN_WAVEFORM )
1874 {
1875 if ( memcmp( &stages[0].rgbWave,
1876 &stages[1].rgbWave,
1877 sizeof( stages[0].rgbWave ) ) )
1878 {
1879 return qfalse;
1880 }
1881 }
1882 if ( stages[0].alphaGen == AGEN_WAVEFORM )
1883 {
1884 if ( memcmp( &stages[0].alphaWave,
1885 &stages[1].alphaWave,
1886 sizeof( stages[0].alphaWave ) ) )
1887 {
1888 return qfalse;
1889 }
1890 }
1891
1892
1893 // make sure that lightmaps are in bundle 1 for 3dfx
1894 if ( stages[0].bundle[0].isLightmap )
1895 {
1896 tmpBundle = stages[0].bundle[0];
1897 stages[0].bundle[0] = stages[1].bundle[0];
1898 stages[0].bundle[1] = tmpBundle;
1899 }
1900 else
1901 {
1902 stages[0].bundle[1] = stages[1].bundle[0];
1903 }
1904
1905 // set the new blend state bits
1906 shader.multitextureEnv = collapse[i].multitextureEnv;
1907 stages[0].stateBits &= ~( GLS_DSTBLEND_BITS | GLS_SRCBLEND_BITS );
1908 stages[0].stateBits |= collapse[i].multitextureBlend;
1909
1910 //
1911 // move down subsequent shaders
1912 //
1913 memmove( &stages[1], &stages[2], sizeof( stages[0] ) * ( MAX_SHADER_STAGES - 2 ) );
1914 Com_Memset( &stages[MAX_SHADER_STAGES-1], 0, sizeof( stages[0] ) );
1915
1916 return qtrue;
1917 }
1918
1919 /*
1920 =============
1921
1922 FixRenderCommandList
1923 https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=493
1924 Arnout: this is a nasty issue. Shaders can be registered after drawsurfaces are generated
1925 but before the frame is rendered. This will, for the duration of one frame, cause drawsurfaces
1926 to be rendered with bad shaders. To fix this, need to go through all render commands and fix
1927 sortedIndex.
1928 ==============
1929 */
1930 static void FixRenderCommandList( int newShader ) {
1931 renderCommandList_t *cmdList = &backEndData->commands;
1932
1933 if( cmdList ) {
1934 const void *curCmd = cmdList->cmds;
1935
1936 while ( 1 ) {
1937 curCmd = PADP(curCmd, sizeof(void *));
1938
1939 switch ( *(const int *)curCmd ) {
1940 case RC_SET_COLOR:
1941 {
1942 const setColorCommand_t *sc_cmd = (const setColorCommand_t *)curCmd;
1943 curCmd = (const void *)(sc_cmd + 1);
1944 break;
1945 }
1946 case RC_STRETCH_PIC:
1947 {
1948 const stretchPicCommand_t *sp_cmd = (const stretchPicCommand_t *)curCmd;
1949 curCmd = (const void *)(sp_cmd + 1);
1950 break;
1951 }
1952 case RC_DRAW_SURFS:
1953 {
1954 int i;
1955 drawSurf_t *drawSurf;
1956 shader_t *pShader;
1957 int fogNum;
1958 int entityNum;
1959 int dlightMap;
1960 int sortedIndex;
1961 const drawSurfsCommand_t *ds_cmd = (const drawSurfsCommand_t *)curCmd;
1962
1963 for( i = 0, drawSurf = ds_cmd->drawSurfs; i < ds_cmd->numDrawSurfs; i++, drawSurf++ ) {
1964 R_DecomposeSort( drawSurf->sort, &entityNum, &pShader, &fogNum, &dlightMap );
1965 sortedIndex = (( drawSurf->sort >> QSORT_SHADERNUM_SHIFT ) & (MAX_SHADERS-1));
1966 if( sortedIndex >= newShader ) {
1967 sortedIndex++;
1968 drawSurf->sort = (sortedIndex << QSORT_SHADERNUM_SHIFT) | entityNum | ( fogNum << QSORT_FOGNUM_SHIFT ) | (int)dlightMap;
1969 }
1970 }
1971 curCmd = (const void *)(ds_cmd + 1);
1972 break;
1973 }
1974 case RC_DRAW_BUFFER:
1975 {
1976 const drawBufferCommand_t *db_cmd = (const drawBufferCommand_t *)curCmd;
1977 curCmd = (const void *)(db_cmd + 1);
1978 break;
1979 }
1980 case RC_SWAP_BUFFERS:
1981 {
1982 const swapBuffersCommand_t *sb_cmd = (const swapBuffersCommand_t *)curCmd;
1983 curCmd = (const void *)(sb_cmd + 1);
1984 break;
1985 }
1986 case RC_END_OF_LIST:
1987 default:
1988 return;
1989 }
1990 }
1991 }
1992 }
1993
1994 /*
1995 ==============
1996 SortNewShader
1997
1998 Positions the most recently created shader in the tr.sortedShaders[]
1999 array so that the shader->sort key is sorted relative to the other
2000 shaders.
2001
2002 Sets shader->sortedIndex
2003 ==============
2004 */
2005 static void SortNewShader( void ) {
2006 int i;
2007 float sort;
2008 shader_t *newShader;
2009
2010 newShader = tr.shaders[ tr.numShaders - 1 ];
2011 sort = newShader->sort;
2012
2013 for ( i = tr.numShaders - 2 ; i >= 0 ; i-- ) {
2014 if ( tr.sortedShaders[ i ]->sort <= sort ) {
2015 break;
2016 }
2017 tr.sortedShaders[i+1] = tr.sortedShaders[i];
2018 tr.sortedShaders[i+1]->sortedIndex++;
2019 }
2020
2021 // Arnout: fix rendercommandlist
2022 // https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=493
2023 FixRenderCommandList( i+1 );
2024
2025 newShader->sortedIndex = i+1;
2026 tr.sortedShaders[i+1] = newShader;
2027 }
2028
2029
2030 /*
2031 ====================
2032 GeneratePermanentShader
2033 ====================
2034 */
2035 static shader_t *GeneratePermanentShader( void ) {
2036 shader_t *newShader;
2037 int i, b;
2038 int size, hash;
2039
2040 if ( tr.numShaders == MAX_SHADERS ) {
2041 ri.Printf( PRINT_WARNING, "WARNING: GeneratePermanentShader - MAX_SHADERS hit\n");
2042 return tr.defaultShader;
2043 }
2044
2045 newShader = ri.Hunk_Alloc( sizeof( shader_t ), h_low );
2046
2047 *newShader = shader;
2048
2049 if ( shader.sort <= SS_OPAQUE ) {
2050 newShader->fogPass = FP_EQUAL;
2051 } else if ( shader.contentFlags & CONTENTS_FOG ) {
2052 newShader->fogPass = FP_LE;
2053 }
2054
2055 tr.shaders[ tr.numShaders ] = newShader;
2056 newShader->index = tr.numShaders;
2057
2058 tr.sortedShaders[ tr.numShaders ] = newShader;
2059 newShader->sortedIndex = tr.numShaders;
2060
2061 tr.numShaders++;
2062
2063 for ( i = 0 ; i < newShader->numUnfoggedPasses ; i++ ) {
2064 if ( !stages[i].active ) {
2065 break;
2066 }
2067 newShader->stages[i] = ri.Hunk_Alloc( sizeof( stages[i] ), h_low );
2068 *newShader->stages[i] = stages[i];
2069
2070 for ( b = 0 ; b < NUM_TEXTURE_BUNDLES ; b++ ) {
2071 size = newShader->stages[i]->bundle[b].numTexMods * sizeof( texModInfo_t );
2072 newShader->stages[i]->bundle[b].texMods = ri.Hunk_Alloc( size, h_low );
2073 Com_Memcpy( newShader->stages[i]->bundle[b].texMods, stages[i].bundle[b].texMods, size );
2074 }
2075 }
2076
2077 SortNewShader();
2078
2079 hash = generateHashValue(newShader->name, FILE_HASH_SIZE);
2080 newShader->next = hashTable[hash];
2081 hashTable[hash] = newShader;
2082
2083 return newShader;
2084 }
2085
2086 /*
2087 =================
2088 VertexLightingCollapse
2089
2090 If vertex lighting is enabled, only render a single
2091 pass, trying to guess which is the correct one to best approximate
2092 what it is supposed to look like.
2093 =================
2094 */
2095 static void VertexLightingCollapse( void ) {
2096 int stage;
2097 shaderStage_t *bestStage;
2098 int bestImageRank;
2099 int rank;
2100
2101 // if we aren't opaque, just use the first pass
2102 if ( shader.sort == SS_OPAQUE ) {
2103
2104 // pick the best texture for the single pass
2105 bestStage = &stages[0];
2106 bestImageRank = -999999;
2107
2108 for ( stage = 0; stage < MAX_SHADER_STAGES; stage++ ) {
2109 shaderStage_t *pStage = &stages[stage];
2110
2111 if ( !pStage->active ) {
2112 break;
2113 }
2114 rank = 0;
2115
2116 if ( pStage->bundle[0].isLightmap ) {
2117 rank -= 100;
2118 }
2119 if ( pStage->bundle[0].tcGen != TCGEN_TEXTURE ) {
2120 rank -= 5;
2121 }
2122 if ( pStage->bundle[0].numTexMods ) {
2123 rank -= 5;
2124 }
2125 if ( pStage->rgbGen != CGEN_IDENTITY && pStage->rgbGen != CGEN_IDENTITY_LIGHTING ) {
2126 rank -= 3;
2127 }
2128
2129 if ( rank > bestImageRank ) {
2130 bestImageRank = rank;
2131 bestStage = pStage;
2132 }
2133 }
2134
2135 stages[0].bundle[0] = bestStage->bundle[0];
2136 stages[0].stateBits &= ~( GLS_DSTBLEND_BITS | GLS_SRCBLEND_BITS );
2137 stages[0].stateBits |= GLS_DEPTHMASK_TRUE;
2138 if ( shader.lightmapIndex == LIGHTMAP_NONE ) {
2139 stages[0].rgbGen = CGEN_LIGHTING_DIFFUSE;
2140 } else {
2141 stages[0].rgbGen = CGEN_EXACT_VERTEX;
2142 }
2143 stages[0].alphaGen = AGEN_SKIP;
2144 } else {
2145 // don't use a lightmap (tesla coils)
2146 if ( stages[0].bundle[0].isLightmap ) {
2147 stages[0] = stages[1];
2148 }
2149
2150 // if we were in a cross-fade cgen, hack it to normal
2151 if ( stages[0].rgbGen == CGEN_ONE_MINUS_ENTITY || stages[1].rgbGen == CGEN_ONE_MINUS_ENTITY ) {
2152 stages[0].rgbGen = CGEN_IDENTITY_LIGHTING;
2153 }
2154 if ( ( stages[0].rgbGen == CGEN_WAVEFORM && stages[0].rgbWave.func == GF_SAWTOOTH )
2155 && ( stages[1].rgbGen == CGEN_WAVEFORM && stages[1].rgbWave.func == GF_INVERSE_SAWTOOTH ) ) {
2156 stages[0].rgbGen = CGEN_IDENTITY_LIGHTING;
2157 }
2158 if ( ( stages[0].rgbGen == CGEN_WAVEFORM && stages[0].rgbWave.func == GF_INVERSE_SAWTOOTH )
2159 && ( stages[1].rgbGen == CGEN_WAVEFORM && stages[1].rgbWave.func == GF_SAWTOOTH ) ) {
2160 stages[0].rgbGen = CGEN_IDENTITY_LIGHTING;
2161 }
2162 }
2163
2164 for ( stage = 1; stage < MAX_SHADER_STAGES; stage++ ) {
2165 shaderStage_t *pStage = &stages[stage];
2166
2167 if ( !pStage->active ) {
2168 break;
2169 }
2170
2171 Com_Memset( pStage, 0, sizeof( *pStage ) );
2172 }
2173 }
2174
2175 /*
2176 ===============
2177 InitShader
2178 ===============
2179 */
2180 static void InitShader( const char *name, int lightmapIndex ) {
2181 int i;
2182
2183 // clear the global shader
2184 Com_Memset( &shader, 0, sizeof( shader ) );
2185 Com_Memset( &stages, 0, sizeof( stages ) );
2186
2187 Q_strncpyz( shader.name, name, sizeof( shader.name ) );
2188 shader.lightmapIndex = lightmapIndex;
2189
2190 for ( i = 0 ; i < MAX_SHADER_STAGES ; i++ ) {
2191 stages[i].bundle[0].texMods = texMods[i];
2192 }
2193 }
2194
2195 /*
2196 =========================
2197 FinishShader
2198
2199 Returns a freshly allocated shader with all the needed info
2200 from the current global working shader
2201 =========================
2202 */
2203 static shader_t *FinishShader( void ) {
2204 int stage;
2205 qboolean hasLightmapStage;
2206 qboolean vertexLightmap;
2207
2208 hasLightmapStage = qfalse;
2209 vertexLightmap = qfalse;
2210
2211 //
2212 // set sky stuff appropriate
2213 //
2214 if ( shader.isSky ) {
2215 shader.sort = SS_ENVIRONMENT;
2216 }
2217
2218 //
2219 // set polygon offset
2220 //
2221 if ( shader.polygonOffset && !shader.sort ) {
2222 shader.sort = SS_DECAL;
2223 }
2224
2225 //
2226 // set appropriate stage information
2227 //
2228 for ( stage = 0; stage < MAX_SHADER_STAGES; ) {
2229 shaderStage_t *pStage = &stages[stage];
2230
2231 if ( !pStage->active ) {
2232 break;
2233 }
2234
2235 // check for a missing texture
2236 if ( !pStage->bundle[0].image[0] ) {
2237 ri.Printf( PRINT_WARNING, "Shader %s has a stage with no image\n", shader.name );
2238 pStage->active = qfalse;
2239 stage++;
2240 continue;
2241 }
2242
2243 //
2244 // ditch this stage if it's detail and detail textures are disabled
2245 //
2246 if ( pStage->isDetail && !r_detailTextures->integer )
2247 {
2248 int index;
2249
2250 for(index = stage + 1; index < MAX_SHADER_STAGES; index++)
2251 {
2252 if(!stages[index].active)
2253 break;
2254 }
2255
2256 if(index < MAX_SHADER_STAGES)
2257 memmove(pStage, pStage + 1, sizeof(*pStage) * (index - stage));
2258 else
2259 {
2260 if(stage + 1 < MAX_SHADER_STAGES)
2261 memmove(pStage, pStage + 1, sizeof(*pStage) * (index - stage - 1));
2262
2263 Com_Memset(&stages[index - 1], 0, sizeof(*stages));
2264 }
2265
2266 continue;
2267 }
2268
2269 //
2270 // default texture coordinate generation
2271 //
2272 if ( pStage->bundle[0].isLightmap ) {
2273 if ( pStage->bundle[0].tcGen == TCGEN_BAD ) {
2274 pStage->bundle[0].tcGen = TCGEN_LIGHTMAP;
2275 }
2276 hasLightmapStage = qtrue;
2277 } else {
2278 if ( pStage->bundle[0].tcGen == TCGEN_BAD ) {
2279 pStage->bundle[0].tcGen = TCGEN_TEXTURE;
2280 }
2281 }
2282
2283
2284 // not a true lightmap but we want to leave existing
2285 // behaviour in place and not print out a warning
2286 //if (pStage->rgbGen == CGEN_VERTEX) {
2287 // vertexLightmap = qtrue;
2288 //}
2289
2290
2291
2292 //
2293 // determine sort order and fog color adjustment
2294 //
2295 if ( ( pStage->stateBits & ( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS ) ) &&
2296 ( stages[0].stateBits & ( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS ) ) ) {
2297 int blendSrcBits = pStage->stateBits & GLS_SRCBLEND_BITS;
2298 int blendDstBits = pStage->stateBits & GLS_DSTBLEND_BITS;
2299
2300 // fog color adjustment only works for blend modes that have a contribution
2301 // that aproaches 0 as the modulate values aproach 0 --
2302 // GL_ONE, GL_ONE
2303 // GL_ZERO, GL_ONE_MINUS_SRC_COLOR
2304 // GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
2305
2306 // modulate, additive
2307 if ( ( ( blendSrcBits == GLS_SRCBLEND_ONE ) && ( blendDstBits == GLS_DSTBLEND_ONE ) ) ||
2308 ( ( blendSrcBits == GLS_SRCBLEND_ZERO ) && ( blendDstBits == GLS_DSTBLEND_ONE_MINUS_SRC_COLOR ) ) ) {
2309 pStage->adjustColorsForFog = ACFF_MODULATE_RGB;
2310 }
2311 // strict blend
2312 else if ( ( blendSrcBits == GLS_SRCBLEND_SRC_ALPHA ) && ( blendDstBits == GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA ) )
2313 {
2314 pStage->adjustColorsForFog = ACFF_MODULATE_ALPHA;
2315 }
2316 // premultiplied alpha
2317 else if ( ( blendSrcBits == GLS_SRCBLEND_ONE ) && ( blendDstBits == GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA ) )
2318 {
2319 pStage->adjustColorsForFog = ACFF_MODULATE_RGBA;
2320 } else {
2321 // we can't adjust this one correctly, so it won't be exactly correct in fog
2322 }
2323
2324 // don't screw with sort order if this is a portal or environment
2325 if ( !shader.sort ) {
2326 // see through item, like a grill or grate
2327 if ( pStage->stateBits & GLS_DEPTHMASK_TRUE ) {
2328 shader.sort = SS_SEE_THROUGH;
2329 } else {
2330 shader.sort = SS_BLEND0;
2331 }
2332 }
2333 }
2334
2335 stage++;
2336 }
2337
2338 // there are times when you will need to manually apply a sort to
2339 // opaque alpha tested shaders that have later blend passes
2340 if ( !shader.sort ) {
2341 shader.sort = SS_OPAQUE;
2342 }
2343
2344 //
2345 // if we are in r_vertexLight mode, never use a lightmap texture
2346 //
2347 if ( stage > 1 && ( (r_vertexLight->integer && !r_uiFullScreen->integer) || glConfig.hardwareType == GLHW_PERMEDIA2 ) ) {
2348 VertexLightingCollapse();
2349 stage = 1;
2350 hasLightmapStage = qfalse;
2351 }
2352
2353 //
2354 // look for multitexture potential
2355 //
2356 if ( stage > 1 && CollapseMultitexture() ) {
2357 stage--;
2358 }
2359
2360 if ( shader.lightmapIndex >= 0 && !hasLightmapStage ) {
2361 if (vertexLightmap) {
2362 ri.Printf( PRINT_DEVELOPER, "WARNING: shader '%s' has VERTEX forced lightmap!\n", shader.name );
2363 } else {
2364 ri.Printf( PRINT_DEVELOPER, "WARNING: shader '%s' has lightmap but no lightmap stage!\n", shader.name );
2365 shader.lightmapIndex = LIGHTMAP_NONE;
2366 }
2367 }
2368
2369
2370 //
2371 // compute number of passes
2372 //
2373 shader.numUnfoggedPasses = stage;
2374
2375 // fogonly shaders don't have any normal passes
2376 if (stage == 0 && !shader.isSky)
2377 shader.sort = SS_FOG;
2378
2379 // determine which stage iterator function is appropriate
2380 ComputeStageIteratorFunc();
2381
2382 return GeneratePermanentShader();
2383 }
2384
2385 //========================================================================================
2386
2387 /*
2388 ====================
2389 FindShaderInShaderText
2390
2391 Scans the combined text description of all the shader files for
2392 the given shader name.
2393
2394 return NULL if not found
2395
2396 If found, it will return a valid shader
2397 =====================
2398 */
2399 static char *FindShaderInShaderText( const char *shadername ) {
2400
2401 char *token, *p;
2402
2403 int i, hash;
2404
2405 hash = generateHashValue(shadername, MAX_SHADERTEXT_HASH);
2406
2407 if(shaderTextHashTable[hash])
2408 {
2409 for (i = 0; shaderTextHashTable[hash][i]; i++)
2410 {
2411 p = shaderTextHashTable[hash][i];
2412 token = COM_ParseExt(&p, qtrue);
2413
2414 if(!Q_stricmp(token, shadername))
2415 return p;
2416 }
2417 }
2418
2419 p = s_shaderText;
2420
2421 if ( !p ) {
2422 return NULL;
2423 }
2424
2425 // look for label
2426 while ( 1 ) {
2427 token = COM_ParseExt( &p, qtrue );
2428 if ( token[0] == 0 ) {
2429 break;
2430 }
2431
2432 if ( !Q_stricmp( token, shadername ) ) {
2433 return p;
2434 }
2435 else {
2436 // skip the definition
2437 SkipBracedSection( &p, 0 );
2438 }
2439 }
2440
2441 return NULL;
2442 }
2443
2444
2445 /*
2446 ==================
2447 R_FindShaderByName
2448
2449 Will always return a valid shader, but it might be the
2450 default shader if the real one can't be found.
2451 ==================
2452 */
2453 shader_t *R_FindShaderByName( const char *name ) {
2454 char strippedName[MAX_QPATH];
2455 int hash;
2456 shader_t *sh;
2457
2458 if ( (name==NULL) || (name[0] == 0) ) {
2459 return tr.defaultShader;
2460 }
2461
2462 COM_StripExtension(name, strippedName, sizeof(strippedName));
2463
2464 hash = generateHashValue(strippedName, FILE_HASH_SIZE);
2465
2466 //
2467 // see if the shader is already loaded
2468 //
2469 for (sh=hashTable[hash]; sh; sh=sh->next) {
2470 // NOTE: if there was no shader or image available with the name strippedName
2471 // then a default shader is created with lightmapIndex == LIGHTMAP_NONE, so we
2472 // have to check all default shaders otherwise for every call to R_FindShader
2473 // with that same strippedName a new default shader is created.
2474 if (Q_stricmp(sh->name, strippedName) == 0) {
2475 // match found
2476 return sh;
2477 }
2478 }
2479
2480 return tr.defaultShader;
2481 }
2482
2483
2484 /*
2485 ===============
2486 R_FindShader
2487
2488 Will always return a valid shader, but it might be the
2489 default shader if the real one can't be found.
2490
2491 In the interest of not requiring an explicit shader text entry to
2492 be defined for every single image used in the game, three default
2493 shader behaviors can be auto-created for any image:
2494
2495 If lightmapIndex == LIGHTMAP_NONE, then the image will have
2496 dynamic diffuse lighting applied to it, as appropriate for most
2497 entity skin surfaces.
2498
2499 If lightmapIndex == LIGHTMAP_2D, then the image will be used
2500 for 2D rendering unless an explicit shader is found
2501
2502 If lightmapIndex == LIGHTMAP_BY_VERTEX, then the image will use
2503 the vertex rgba modulate values, as appropriate for misc_model
2504 pre-lit surfaces.
2505
2506 Other lightmapIndex values will have a lightmap stage created
2507 and src*dest blending applied with the texture, as appropriate for
2508 most world construction surfaces.
2509
2510 ===============
2511 */
2512 shader_t *R_FindShader( const char *name, int lightmapIndex, qboolean mipRawImage ) {
2513 char strippedName[MAX_QPATH];
2514 int hash;
2515 char *shaderText;
2516 image_t *image;
2517 shader_t *sh;
2518
2519 if ( name[0] == 0 ) {
2520 return tr.defaultShader;
2521 }
2522
2523 // use (fullbright) vertex lighting if the bsp file doesn't have
2524 // lightmaps
2525 if ( lightmapIndex >= 0 && lightmapIndex >= tr.numLightmaps ) {
2526 lightmapIndex = LIGHTMAP_BY_VERTEX;
2527 } else if ( lightmapIndex < LIGHTMAP_2D ) {
2528 // negative lightmap indexes cause stray pointers (think tr.lightmaps[lightmapIndex])
2529 ri.Printf( PRINT_WARNING, "WARNING: shader '%s' has invalid lightmap index of %d\n", name, lightmapIndex );
2530 lightmapIndex = LIGHTMAP_BY_VERTEX;
2531 }
2532
2533 COM_StripExtension(name, strippedName, sizeof(strippedName));
2534
2535 hash = generateHashValue(strippedName, FILE_HASH_SIZE);
2536
2537 //
2538 // see if the shader is already loaded
2539 //
2540 for (sh = hashTable[hash]; sh; sh = sh->next) {
2541 // NOTE: if there was no shader or image available with the name strippedName
2542 // then a default shader is created with lightmapIndex == LIGHTMAP_NONE, so we
2543 // have to check all default shaders otherwise for every call to R_FindShader
2544 // with that same strippedName a new default shader is created.
2545 if ( (sh->lightmapIndex == lightmapIndex || sh->defaultShader) &&
2546 !Q_stricmp(sh->name, strippedName)) {
2547 // match found
2548 return sh;
2549 }
2550 }
2551
2552 InitShader( strippedName, lightmapIndex );
2553
2554 // FIXME: set these "need" values appropriately
2555 shader.needsNormal = qtrue;
2556 shader.needsST1 = qtrue;
2557 shader.needsST2 = qtrue;
2558 shader.needsColor = qtrue;
2559
2560 //
2561 // attempt to define shader from an explicit parameter file
2562 //
2563 shaderText = FindShaderInShaderText( strippedName );
2564 if ( shaderText ) {
2565 // enable this when building a pak file to get a global list
2566 // of all explicit shaders
2567 if ( r_printShaders->integer ) {
2568 ri.Printf( PRINT_ALL, "*SHADER* %s\n", name );
2569 }
2570
2571 if ( !ParseShader( &shaderText ) ) {
2572 // had errors, so use default shader
2573 shader.defaultShader = qtrue;
2574 }
2575 sh = FinishShader();
2576 return sh;
2577 }
2578
2579
2580 //
2581 // if not defined in the in-memory shader descriptions,
2582 // look for a single supported image file
2583 //
2584 {
2585 imgFlags_t flags;
2586
2587 flags = IMGFLAG_NONE;
2588
2589 if (mipRawImage)
2590 {
2591 flags |= IMGFLAG_MIPMAP | IMGFLAG_PICMIP;
2592 }
2593 else
2594 {
2595 flags |= IMGFLAG_CLAMPTOEDGE;
2596 }
2597
2598 image = R_FindImageFile( name, IMGTYPE_COLORALPHA, flags );
2599 if ( !image ) {
2600 ri.Printf( PRINT_DEVELOPER, "Couldn't find image file for shader %s\n", name );
2601 shader.defaultShader = qtrue;
2602 return FinishShader();
2603 }
2604 }
2605
2606 //
2607 // create the default shading commands
2608 //
2609 if ( shader.lightmapIndex == LIGHTMAP_NONE ) {
2610 // dynamic colors at vertexes
2611 stages[0].bundle[0].image[0] = image;
2612 stages[0].active = qtrue;
2613 stages[0].rgbGen = CGEN_LIGHTING_DIFFUSE;
2614 stages[0].stateBits = GLS_DEFAULT;
2615 } else if ( shader.lightmapIndex == LIGHTMAP_BY_VERTEX ) {
2616 // explicit colors at vertexes
2617 stages[0].bundle[0].image[0] = image;
2618 stages[0].active = qtrue;
2619 stages[0].rgbGen = CGEN_EXACT_VERTEX;
2620 stages[0].alphaGen = AGEN_SKIP;
2621 stages[0].stateBits = GLS_DEFAULT;
2622 } else if ( shader.lightmapIndex == LIGHTMAP_2D ) {
2623 // GUI elements
2624 stages[0].bundle[0].image[0] = image;
2625 stages[0].active = qtrue;
2626 stages[0].rgbGen = CGEN_VERTEX;
2627 stages[0].alphaGen = AGEN_VERTEX;
2628 stages[0].stateBits = GLS_DEPTHTEST_DISABLE |
2629 GLS_SRCBLEND_SRC_ALPHA |
2630 GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA;
2631 } else if ( shader.lightmapIndex == LIGHTMAP_WHITEIMAGE ) {
2632 // fullbright level
2633 stages[0].bundle[0].image[0] = tr.whiteImage;
2634 stages[0].active = qtrue;
2635 stages[0].rgbGen = CGEN_IDENTITY_LIGHTING;
2636 stages[0].stateBits = GLS_DEFAULT;
2637
2638 stages[1].bundle[0].image[0] = image;
2639 stages[1].active = qtrue;
2640 stages[1].rgbGen = CGEN_IDENTITY;
2641 stages[1].stateBits |= GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ZERO;
2642 } else {
2643 // two pass lightmap
2644 stages[0].bundle[0].image[0] = tr.lightmaps[shader.lightmapIndex];
2645 stages[0].bundle[0].isLightmap = qtrue;
2646 stages[0].active = qtrue;
2647 stages[0].rgbGen = CGEN_IDENTITY; // lightmaps are scaled on creation
2648 // for identitylight
2649 stages[0].stateBits = GLS_DEFAULT;
2650
2651 stages[1].bundle[0].image[0] = image;
2652 stages[1].active = qtrue;
2653 stages[1].rgbGen = CGEN_IDENTITY;
2654 stages[1].stateBits |= GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ZERO;
2655 }
2656
2657 return FinishShader();
2658 }
2659
2660
2661 qhandle_t RE_RegisterShaderFromImage(const char *name, int lightmapIndex, image_t *image, qboolean mipRawImage) {
2662 int hash;
2663 shader_t *sh;
2664
2665 hash = generateHashValue(name, FILE_HASH_SIZE);
2666
2667 // probably not necessary since this function
2668 // only gets called from tr_font.c with lightmapIndex == LIGHTMAP_2D
2669 // but better safe than sorry.
2670 if ( lightmapIndex >= tr.numLightmaps ) {
2671 lightmapIndex = LIGHTMAP_WHITEIMAGE;
2672 }
2673
2674 //
2675 // see if the shader is already loaded
2676 //
2677 for (sh=hashTable[hash]; sh; sh=sh->next) {
2678 // NOTE: if there was no shader or image available with the name strippedName
2679 // then a default shader is created with lightmapIndex == LIGHTMAP_NONE, so we
2680 // have to check all default shaders otherwise for every call to R_FindShader
2681 // with that same strippedName a new default shader is created.
2682 if ( (sh->lightmapIndex == lightmapIndex || sh->defaultShader) &&
2683 // index by name
2684 !Q_stricmp(sh->name, name)) {
2685 // match found
2686 return sh->index;
2687 }
2688 }
2689
2690 InitShader( name, lightmapIndex );
2691
2692 // FIXME: set these "need" values appropriately
2693 shader.needsNormal = qtrue;
2694 shader.needsST1 = qtrue;
2695 shader.needsST2 = qtrue;
2696 shader.needsColor = qtrue;
2697
2698 //
2699 // create the default shading commands
2700 //
2701 if ( shader.lightmapIndex == LIGHTMAP_NONE ) {
2702 // dynamic colors at vertexes
2703 stages[0].bundle[0].image[0] = image;
2704 stages[0].active = qtrue;
2705 stages[0].rgbGen = CGEN_LIGHTING_DIFFUSE;
2706 stages[0].stateBits = GLS_DEFAULT;
2707 } else if ( shader.lightmapIndex == LIGHTMAP_BY_VERTEX ) {
2708 // explicit colors at vertexes
2709 stages[0].bundle[0].image[0] = image;
2710 stages[0].active = qtrue;
2711 stages[0].rgbGen = CGEN_EXACT_VERTEX;
2712 stages[0].alphaGen = AGEN_SKIP;
2713 stages[0].stateBits = GLS_DEFAULT;
2714 } else if ( shader.lightmapIndex == LIGHTMAP_2D ) {
2715 // GUI elements
2716 stages[0].bundle[0].image[0] = image;
2717 stages[0].active = qtrue;
2718 stages[0].rgbGen = CGEN_VERTEX;
2719 stages[0].alphaGen = AGEN_VERTEX;
2720 stages[0].stateBits = GLS_DEPTHTEST_DISABLE |
2721 GLS_SRCBLEND_SRC_ALPHA |
2722 GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA;
2723 } else if ( shader.lightmapIndex == LIGHTMAP_WHITEIMAGE ) {
2724 // fullbright level
2725 stages[0].bundle[0].image[0] = tr.whiteImage;
2726 stages[0].active = qtrue;
2727 stages[0].rgbGen = CGEN_IDENTITY_LIGHTING;
2728 stages[0].stateBits = GLS_DEFAULT;
2729
2730 stages[1].bundle[0].image[0] = image;
2731 stages[1].active = qtrue;
2732 stages[1].rgbGen = CGEN_IDENTITY;
2733 stages[1].stateBits |= GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ZERO;
2734 } else {
2735 // two pass lightmap
2736 stages[0].bundle[0].image[0] = tr.lightmaps[shader.lightmapIndex];
2737 stages[0].bundle[0].isLightmap = qtrue;
2738 stages[0].active = qtrue;
2739 stages[0].rgbGen = CGEN_IDENTITY; // lightmaps are scaled on creation
2740 // for identitylight
2741 stages[0].stateBits = GLS_DEFAULT;
2742
2743 stages[1].bundle[0].image[0] = image;
2744 stages[1].active = qtrue;
2745 stages[1].rgbGen = CGEN_IDENTITY;
2746 stages[1].stateBits |= GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ZERO;
2747 }
2748
2749 sh = FinishShader();
2750 return sh->index;
2751 }
2752
2753
2754 /*
2755 ====================
2756 RE_RegisterShader
2757
2758 This is the exported shader entry point for the rest of the system
2759 It will always return an index that will be valid.
2760
2761 This should really only be used for explicit shaders, because there is no
2762 way to ask for different implicit lighting modes (vertex, lightmap, etc)
2763 ====================
2764 */
2765 qhandle_t RE_RegisterShaderLightMap( const char *name, int lightmapIndex ) {
2766 shader_t *sh;
2767
2768 if ( strlen( name ) >= MAX_QPATH ) {
2769 ri.Printf( PRINT_ALL, "Shader name exceeds MAX_QPATH\n" );
2770 return 0;
2771 }
2772
2773 sh = R_FindShader( name, lightmapIndex, qtrue );
2774
2775 // we want to return 0 if the shader failed to
2776 // load for some reason, but R_FindShader should
2777 // still keep a name allocated for it, so if
2778 // something calls RE_RegisterShader again with
2779 // the same name, we don't try looking for it again
2780 if ( sh->defaultShader ) {
2781 return 0;
2782 }
2783
2784 return sh->index;
2785 }
2786
2787
2788 /*
2789 ====================
2790 RE_RegisterShader
2791
2792 This is the exported shader entry point for the rest of the system
2793 It will always return an index that will be valid.
2794
2795 This should really only be used for explicit shaders, because there is no
2796 way to ask for different implicit lighting modes (vertex, lightmap, etc)
2797 ====================
2798 */
2799 qhandle_t RE_RegisterShader( const char *name ) {
2800 shader_t *sh;
2801
2802 if ( strlen( name ) >= MAX_QPATH ) {
2803 ri.Printf( PRINT_ALL, "Shader name exceeds MAX_QPATH\n" );
2804 return 0;
2805 }
2806
2807 sh = R_FindShader( name, LIGHTMAP_2D, qtrue );
2808
2809 // we want to return 0 if the shader failed to
2810 // load for some reason, but R_FindShader should
2811 // still keep a name allocated for it, so if
2812 // something calls RE_RegisterShader again with
2813 // the same name, we don't try looking for it again
2814 if ( sh->defaultShader ) {
2815 return 0;
2816 }
2817
2818 return sh->index;
2819 }
2820
2821
2822 /*
2823 ====================
2824 RE_RegisterShaderNoMip
2825
2826 For menu graphics that should never be picmiped
2827 ====================
2828 */
2829 qhandle_t RE_RegisterShaderNoMip( const char *name ) {
2830 shader_t *sh;
2831
2832 if ( strlen( name ) >= MAX_QPATH ) {
2833 ri.Printf( PRINT_ALL, "Shader name exceeds MAX_QPATH\n" );
2834 return 0;
2835 }
2836
2837 sh = R_FindShader( name, LIGHTMAP_2D, qfalse );
2838
2839 // we want to return 0 if the shader failed to
2840 // load for some reason, but R_FindShader should
2841 // still keep a name allocated for it, so if
2842 // something calls RE_RegisterShader again with
2843 // the same name, we don't try looking for it again
2844 if ( sh->defaultShader ) {
2845 return 0;
2846 }
2847
2848 return sh->index;
2849 }
2850
2851 /*
2852 ====================
2853 R_GetShaderByHandle
2854
2855 When a handle is passed in by another module, this range checks
2856 it and returns a valid (possibly default) shader_t to be used internally.
2857 ====================
2858 */
2859 shader_t *R_GetShaderByHandle( qhandle_t hShader ) {
2860 if ( hShader < 0 ) {
2861 ri.Printf( PRINT_WARNING, "R_GetShaderByHandle: out of range hShader '%d'\n", hShader );
2862 return tr.defaultShader;
2863 }
2864 if ( hShader >= tr.numShaders ) {
2865 ri.Printf( PRINT_WARNING, "R_GetShaderByHandle: out of range hShader '%d'\n", hShader );
2866 return tr.defaultShader;
2867 }
2868 return tr.shaders[hShader];
2869 }
2870
2871 /*
2872 ===============
2873 R_ShaderList_f
2874
2875 Dump information on all valid shaders to the console
2876 A second parameter will cause it to print in sorted order
2877 ===============
2878 */
2879 void R_ShaderList_f (void) {
2880 int i;
2881 int count;
2882 shader_t *pShader;
2883
2884 ri.Printf (PRINT_ALL, "-----------------------\n");
2885
2886 count = 0;
2887 for ( i = 0 ; i < tr.numShaders ; i++ ) {
2888 if ( ri.Cmd_Argc() > 1 ) {
2889 pShader = tr.sortedShaders[i];
2890 } else {
2891 pShader = tr.shaders[i];
2892 }
2893
2894 ri.Printf( PRINT_ALL, "%i ", pShader->numUnfoggedPasses );
2895
2896 if (pShader->lightmapIndex >= 0 ) {
2897 ri.Printf (PRINT_ALL, "L ");
2898 } else {
2899 ri.Printf (PRINT_ALL, " ");
2900 }
2901 if ( pShader->multitextureEnv == GL_ADD ) {
2902 ri.Printf( PRINT_ALL, "MT(a) " );
2903 } else if ( pShader->multitextureEnv == GL_MODULATE ) {
2904 ri.Printf( PRINT_ALL, "MT(m) " );
2905 } else if ( pShader->multitextureEnv == GL_DECAL ) {
2906 ri.Printf( PRINT_ALL, "MT(d) " );
2907 } else {
2908 ri.Printf( PRINT_ALL, " " );
2909 }
2910 if ( pShader->explicitlyDefined ) {
2911 ri.Printf( PRINT_ALL, "E " );
2912 } else {
2913 ri.Printf( PRINT_ALL, " " );
2914 }
2915
2916 if ( pShader->optimalStageIteratorFunc == RB_StageIteratorGeneric ) {
2917 ri.Printf( PRINT_ALL, "gen " );
2918 } else if ( pShader->optimalStageIteratorFunc == RB_StageIteratorSky ) {
2919 ri.Printf( PRINT_ALL, "sky " );
2920 } else if ( pShader->optimalStageIteratorFunc == RB_StageIteratorLightmappedMultitexture ) {
2921 ri.Printf( PRINT_ALL, "lmmt" );
2922 } else if ( pShader->optimalStageIteratorFunc == RB_StageIteratorVertexLitTexture ) {
2923 ri.Printf( PRINT_ALL, "vlt " );
2924 } else {
2925 ri.Printf( PRINT_ALL, " " );
2926 }
2927
2928 if ( pShader->defaultShader ) {
2929 ri.Printf (PRINT_ALL, ": %s (DEFAULTED)\n", pShader->name);
2930 } else {
2931 ri.Printf (PRINT_ALL, ": %s\n", pShader->name);
2932 }
2933 count++;
2934 }
2935 ri.Printf (PRINT_ALL, "%i total shaders\n", count);
2936 ri.Printf (PRINT_ALL, "------------------\n");
2937 }
2938
2939 /*
2940 ====================
2941 ScanAndLoadShaderFiles
2942
2943 Finds and loads all .shader files, combining them into
2944 a single large text block that can be scanned for shader names
2945 =====================
2946 */
2947 #define MAX_SHADER_FILES 4096
2948 static void ScanAndLoadShaderFiles( void )
2949 {
2950 char **shaderFiles;
2951 char *buffers[MAX_SHADER_FILES] = {0};
2952 char *p;
2953 int numShaderFiles;
2954 int i;
2955 char *oldp, *token, *hashMem, *textEnd;
2956 int shaderTextHashTableSizes[MAX_SHADERTEXT_HASH], hash, size;
2957 char shaderName[MAX_QPATH];
2958 int shaderLine;
2959
2960 long sum = 0, summand;
2961 // scan for shader files
2962 shaderFiles = ri.FS_ListFiles( "scripts", ".shader", &numShaderFiles );
2963
2964 if ( !shaderFiles || !numShaderFiles )
2965 {
2966 ri.Printf( PRINT_WARNING, "WARNING: no shader files found\n" );
2967 return;
2968 }
2969
2970 if ( numShaderFiles > MAX_SHADER_FILES ) {
2971 numShaderFiles = MAX_SHADER_FILES;
2972 }
2973
2974 // load and parse shader files
2975 for ( i = 0; i < numShaderFiles; i++ )
2976 {
2977 char filename[MAX_QPATH];
2978
2979 Com_sprintf( filename, sizeof( filename ), "scripts/%s", shaderFiles[i] );
2980 ri.Printf( PRINT_DEVELOPER, "...loading '%s'\n", filename );
2981 summand = ri.FS_ReadFile( filename, (void **)&buffers[i] );
2982
2983 if ( !buffers[i] )
2984 ri.Error( ERR_DROP, "Couldn't load %s", filename );
2985
2986 // Do a simple check on the shader structure in that file to make sure one bad shader file cannot fuck up all other shaders.
2987 p = buffers[i];
2988 COM_BeginParseSession(filename);
2989 while(1)
2990 {
2991 token = COM_ParseExt(&p, qtrue);
2992
2993 if(!*token)
2994 break;
2995
2996 Q_strncpyz(shaderName, token, sizeof(shaderName));
2997 shaderLine = COM_GetCurrentParseLine();
2998
2999 token = COM_ParseExt(&p, qtrue);
3000 if(token[0] != '{' || token[1] != '\0')
3001 {
3002 ri.Printf(PRINT_WARNING, "WARNING: Ignoring shader file %s. Shader \"%s\" on line %d missing opening brace",
3003 filename, shaderName, shaderLine);
3004 if (token[0])
3005 {
3006 ri.Printf(PRINT_WARNING, " (found \"%s\" on line %d)", token, COM_GetCurrentParseLine());
3007 }
3008 ri.Printf(PRINT_WARNING, ".\n");
3009 ri.FS_FreeFile(buffers[i]);
3010 buffers[i] = NULL;
3011 break;
3012 }
3013
3014 if(!SkipBracedSection(&p, 1))
3015 {
3016 ri.Printf(PRINT_WARNING, "WARNING: Ignoring shader file %s. Shader \"%s\" on line %d missing closing brace.\n",
3017 filename, shaderName, shaderLine);
3018 ri.FS_FreeFile(buffers[i]);
3019 buffers[i] = NULL;
3020 break;
3021 }
3022 }
3023
3024
3025 if (buffers[i])
3026 sum += summand;
3027 }
3028
3029 // build single large buffer
3030 s_shaderText = ri.Hunk_Alloc( sum + numShaderFiles*2, h_low );
3031 s_shaderText[ 0 ] = '\0';
3032 textEnd = s_shaderText;
3033
3034 // free in reverse order, so the temp files are all dumped
3035 for ( i = numShaderFiles - 1; i >= 0 ; i-- )
3036 {
3037 if ( !buffers[i] )
3038 continue;
3039
3040 strcat( textEnd, buffers[i] );
3041 strcat( textEnd, "\n" );
3042 textEnd += strlen( textEnd );
3043 ri.FS_FreeFile( buffers[i] );
3044 }
3045
3046 COM_Compress( s_shaderText );
3047
3048 // free up memory
3049 ri.FS_FreeFileList( shaderFiles );
3050
3051 Com_Memset(shaderTextHashTableSizes, 0, sizeof(shaderTextHashTableSizes));
3052 size = 0;
3053
3054 p = s_shaderText;
3055 // look for shader names
3056 while ( 1 ) {
3057 token = COM_ParseExt( &p, qtrue );
3058 if ( token[0] == 0 ) {
3059 break;
3060 }
3061
3062 hash = generateHashValue(token, MAX_SHADERTEXT_HASH);
3063 shaderTextHashTableSizes[hash]++;
3064 size++;
3065 SkipBracedSection(&p, 0);
3066 }
3067
3068 size += MAX_SHADERTEXT_HASH;
3069
3070 hashMem = ri.Hunk_Alloc( size * sizeof(char *), h_low );
3071
3072 for (i = 0; i < MAX_SHADERTEXT_HASH; i++) {
3073 shaderTextHashTable[i] = (char **) hashMem;
3074 hashMem = ((char *) hashMem) + ((shaderTextHashTableSizes[i] + 1) * sizeof(char *));
3075 }
3076
3077 Com_Memset(shaderTextHashTableSizes, 0, sizeof(shaderTextHashTableSizes));
3078
3079 p = s_shaderText;
3080 // look for shader names
3081 while ( 1 ) {
3082 oldp = p;
3083 token = COM_ParseExt( &p, qtrue );
3084 if ( token[0] == 0 ) {
3085 break;
3086 }
3087
3088 hash = generateHashValue(token, MAX_SHADERTEXT_HASH);
3089 shaderTextHashTable[hash][shaderTextHashTableSizes[hash]++] = oldp;
3090
3091 SkipBracedSection(&p, 0);
3092 }
3093
3094 return;
3095
3096 }
3097
3098
3099 /*
3100 ====================
3101 CreateInternalShaders
3102 ====================
3103 */
3104 static void CreateInternalShaders( void ) {
3105 tr.numShaders = 0;
3106
3107 // init the default shader
3108 InitShader( "<default>", LIGHTMAP_NONE );
3109 stages[0].bundle[0].image[0] = tr.defaultImage;
3110 stages[0].active = qtrue;
3111 stages[0].stateBits = GLS_DEFAULT;
3112 tr.defaultShader = FinishShader();
3113
3114 // shadow shader is just a marker
3115 Q_strncpyz( shader.name, "<stencil shadow>", sizeof( shader.name ) );
3116 shader.sort = SS_STENCIL_SHADOW;
3117 tr.shadowShader = FinishShader();
3118 }
3119
3120 static void CreateExternalShaders( void ) {
3121 tr.projectionShadowShader = R_FindShader( "projectionShadow", LIGHTMAP_NONE, qtrue );
3122 tr.flareShader = R_FindShader( "flareShader", LIGHTMAP_NONE, qtrue );
3123
3124 // Hack to make fogging work correctly on flares. Fog colors are calculated
3125 // in tr_flare.c already.
3126 if(!tr.flareShader->defaultShader)
3127 {
3128 int index;
3129
3130 for(index = 0; index < tr.flareShader->numUnfoggedPasses; index++)
3131 {
3132 tr.flareShader->stages[index]->adjustColorsForFog = ACFF_NONE;
3133 tr.flareShader->stages[index]->stateBits |= GLS_DEPTHTEST_DISABLE;
3134 }
3135 }
3136
3137 tr.sunShader = R_FindShader( "sun", LIGHTMAP_NONE, qtrue );
3138 }
3139
3140 /*
3141 ==================
3142 R_InitShaders
3143 ==================
3144 */
3145 void R_InitShaders( void ) {
3146 ri.Printf( PRINT_ALL, "Initializing Shaders\n" );
3147
3148 Com_Memset(hashTable, 0, sizeof(hashTable));
3149
3150 CreateInternalShaders();
3151
3152 ScanAndLoadShaderFiles();
3153
3154 CreateExternalShaders();
3155 }