Debian Codegraph @

code/renderergl2/tr_shade.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 // tr_shade.c
23
24 #include "tr_local.h"
25
26 /*
27
28 THIS ENTIRE FILE IS BACK END
29
30 This file deals with applying shaders to surface data in the tess struct.
31 */
32
33
34 /*
35 ==================
36 R_DrawElements
37
38 ==================
39 */
40
41 void R_DrawElements( int numIndexes, int firstIndex )
42 {
43 if (tess.useCacheVao)
44 {
45 VaoCache_DrawElements(numIndexes, firstIndex);
46 }
47 else
48 {
49 qglDrawElements(GL_TRIANGLES, numIndexes, GL_INDEX_TYPE, BUFFER_OFFSET(firstIndex * sizeof(glIndex_t)));
50 }
51 }
52
53
54 /*
55 =============================================================
56
57 SURFACE SHADERS
58
59 =============================================================
60 */
61
62 shaderCommands_t tess;
63
64
65 /*
66 =================
67 R_BindAnimatedImageToTMU
68
69 =================
70 */
71 static void R_BindAnimatedImageToTMU( textureBundle_t *bundle, int tmu ) {
72 int64_t index;
73
74 if ( bundle->isVideoMap ) {
75 ri.CIN_RunCinematic(bundle->videoMapHandle);
76 ri.CIN_UploadCinematic(bundle->videoMapHandle);
77 GL_BindToTMU(tr.scratchImage[bundle->videoMapHandle], tmu);
78 return;
79 }
80
81 if ( bundle->numImageAnimations <= 1 ) {
82 GL_BindToTMU( bundle->image[0], tmu);
83 return;
84 }
85
86 // it is necessary to do this messy calc to make sure animations line up
87 // exactly with waveforms of the same frequency
88 index = tess.shaderTime * bundle->imageAnimationSpeed * FUNCTABLE_SIZE;
89 index >>= FUNCTABLE_SIZE2;
90
91 if ( index < 0 ) {
92 index = 0; // may happen with shader time offsets
93 }
94
95 // Windows x86 doesn't load renderer DLL with 64 bit modulus
96 //index %= bundle->numImageAnimations;
97 while ( index >= bundle->numImageAnimations ) {
98 index -= bundle->numImageAnimations;
99 }
100
101 GL_BindToTMU( bundle->image[ index ], tmu );
102 }
103
104
105 /*
106 ================
107 DrawTris
108
109 Draws triangle outlines for debugging
110 ================
111 */
112 static void DrawTris (shaderCommands_t *input) {
113 GL_BindToTMU( tr.whiteImage, TB_COLORMAP );
114
115 GL_State( GLS_POLYMODE_LINE | GLS_DEPTHMASK_TRUE );
116 qglDepthRange( 0, 0 );
117
118 {
119 shaderProgram_t *sp = &tr.textureColorShader;
120 vec4_t color;
121
122 GLSL_BindProgram(sp);
123
124 GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
125 VectorSet4(color, 1, 1, 1, 1);
126 GLSL_SetUniformVec4(sp, UNIFORM_COLOR, color);
127 GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 0);
128
129 R_DrawElements(input->numIndexes, input->firstIndex);
130 }
131
132 qglDepthRange( 0, 1 );
133 }
134
135
136 /*
137 ================
138 DrawNormals
139
140 Draws vertex normals for debugging
141 ================
142 */
143 static void DrawNormals (shaderCommands_t *input) {
144 if (!input) return;
145
146 const int vertexCount = input->numVertexes;
147 if (vertexCount <= 0) return;
148 if (!input->shader || input->shader->isSky) return; // do not draw over sky/clouds
149 if (input->shader->numDeforms > 0) return; // skip vertex-deformed surfaces
150
151 // Skip animated entities that are interpolating; otherwise normals will appear to "swim".
152 if (backEnd.currentEntity != &tr.worldEntity) {
153 const trRefEntity_t* refEnt = backEnd.currentEntity;
154 if (refEnt && refEnt->e.backlerp > 0) return;
155 }
156
157 const float lineLength = 6.0f;
158
159 // xyz is vec4_t (x,y,z,1)
160 const float* positionsXYZW = (const float*)input->xyz;
161 const int16_t* packedNormals = (const int16_t*)input->normal;
162 const float* floatNormals = (const float*)input->normal;
163
164 const vao_t* boundVao = glState.currentVao;
165 qboolean usePacked = qfalse;
166
167 if (boundVao) {
168 const vaoAttrib_t* a = &boundVao->attribs[ATTR_INDEX_NORMAL];
169 usePacked = (a->type == GL_SHORT && a->normalized);
170 }
171 else {
172 const float sx = packedNormals[0] * (1.0f / 32767.0f);
173 const float sy = packedNormals[1] * (1.0f / 32767.0f);
174 const float sz = packedNormals[2] * (1.0f / 32767.0f);
175
176 const float lp = sx * sx + sy * sy + sz * sz;
177 const float lf = floatNormals[0] * floatNormals[0]
178 + floatNormals[1] * floatNormals[1]
179 + floatNormals[2] * floatNormals[2];
180
181 usePacked = fabsf(lp - 1.0f) <= fabsf(lf - 1.0f);
182 }
183
184 const GLsizeiptr floatsPerVertex = 6;
185 const GLsizeiptr maxFloatCount = floatsPerVertex * (GLsizeiptr)vertexCount;
186 const GLsizeiptr maxByteCount = (GLsizeiptr)sizeof(float) * maxFloatCount;
187
188 float* lineVertices = (float*)ri.Hunk_AllocateTempMemory((int)maxByteCount);
189 float* writePtr = lineVertices;
190
191 // Build the line list
192 for (int i = 0; i < vertexCount; ++i, positionsXYZW += 4) {
193 vec3_t n;
194
195 if (usePacked) {
196 R_VaoUnpackNormal(n, (int16_t*)packedNormals);
197 packedNormals += 4;
198 }
199 else {
200 n[0] = floatNormals[0];
201 n[1] = floatNormals[1];
202 n[2] = floatNormals[2];
203 floatNormals += 4;
204 }
205
206 // endpoints in object/world space
207 const float startX = positionsXYZW[0];
208 const float startY = positionsXYZW[1];
209 const float startZ = positionsXYZW[2];
210
211 const float endX = startX + n[0] * lineLength;
212 const float endY = startY + n[1] * lineLength;
213 const float endZ = startZ + n[2] * lineLength;
214
215
216 // emit line
217 *writePtr++ = startX; *writePtr++ = startY; *writePtr++ = startZ;
218 *writePtr++ = endX; *writePtr++ = endY; *writePtr++ = endZ;
219 }
220
221 const GLsizei numVerts = (GLsizei)((writePtr - lineVertices) / 3);
222 if (numVerts == 0) {
223 ri.Hunk_FreeTempMemory(lineVertices);
224 return;
225 }
226
227 // save bindings
228 GLint prevProg = 0, prevVao = 0, prevArrayBuf = 0;
229 qglGetIntegerv(GL_CURRENT_PROGRAM, &prevProg);
230 qglGetIntegerv(GL_VERTEX_ARRAY_BINDING, &prevVao);
231 qglGetIntegerv(GL_ARRAY_BUFFER_BINDING, &prevArrayBuf);
232
233 GL_BindToTMU(tr.whiteImage, TB_COLORMAP);
234 GL_State(GLS_POLYMODE_LINE);
235 GLboolean prevDepthMask;
236 qglGetBooleanv(GL_DEPTH_WRITEMASK, &prevDepthMask);
237 qglDepthMask(GL_FALSE);
238
239
240 R_BindNullVao();
241
242 // temp VAO/VBO
243 GLuint vao = 0, vbo = 0;
244 qglGenVertexArrays(1, &vao);
245 qglBindVertexArray(vao);
246
247 qglGenBuffers(1, &vbo);
248 qglBindBuffer(GL_ARRAY_BUFFER, vbo);
249 qglBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * numVerts, lineVertices, GL_STREAM_DRAW);
250
251 shaderProgram_t* program = &tr.textureColorShader;
252 GLSL_BindProgram(program);
253 GLSL_SetUniformMat4(program, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
254
255 vec4_t white;
256 VectorSet4(white, 1, 1, 1, 1);
257 GLSL_SetUniformVec4(program, UNIFORM_COLOR, white);
258
259 // position at location 0 in this backend
260 qglEnableVertexAttribArray(ATTR_INDEX_POSITION);
261 qglVertexAttribPointer(ATTR_INDEX_POSITION, 3, GL_FLOAT, GL_FALSE, 0, (const void*)0);
262
263 qglDrawArrays(GL_LINES, 0, numVerts);
264
265 qglDisableVertexAttribArray(ATTR_INDEX_POSITION);
266
267 // restore
268 qglDepthMask(prevDepthMask);
269 qglBindBuffer(GL_ARRAY_BUFFER, prevArrayBuf);
270 qglBindVertexArray(prevVao);
271 qglDeleteBuffers(1, &vbo);
272 qglDeleteVertexArrays(1, &vao);
273
274 if (prevProg) qglUseProgram((GLuint)prevProg);
275 else qglUseProgram(0);
276
277 ri.Hunk_FreeTempMemory(lineVertices);
278 }
279
280 /*
281 ==============
282 RB_BeginSurface
283
284 We must set some things up before beginning any tesselation,
285 because a surface may be forced to perform a RB_End due
286 to overflow.
287 ==============
288 */
289 void RB_BeginSurface( shader_t *shader, int fogNum, int cubemapIndex ) {
290
291 shader_t *state = (shader->remappedShader) ? shader->remappedShader : shader;
292
293 tess.numIndexes = 0;
294 tess.firstIndex = 0;
295 tess.numVertexes = 0;
296 tess.shader = state;
297 tess.fogNum = fogNum;
298 tess.cubemapIndex = cubemapIndex;
299 tess.dlightBits = 0; // will be OR'd in by surface functions
300 tess.pshadowBits = 0; // will be OR'd in by surface functions
301 tess.xstages = state->stages;
302 tess.numPasses = state->numUnfoggedPasses;
303 tess.currentStageIteratorFunc = state->optimalStageIteratorFunc;
304 tess.useInternalVao = qtrue;
305 tess.useCacheVao = qfalse;
306
307 tess.shaderTime = backEnd.refdef.floatTime - tess.shader->timeOffset;
308 if (tess.shader->clampTime && tess.shaderTime >= tess.shader->clampTime) {
309 tess.shaderTime = tess.shader->clampTime;
310 }
311
312 if (backEnd.viewParms.flags & VPF_SHADOWMAP)
313 {
314 tess.currentStageIteratorFunc = RB_StageIteratorGeneric;
315 }
316 }
317
318
319
320 extern float EvalWaveForm( const waveForm_t *wf );
321 extern float EvalWaveFormClamped( const waveForm_t *wf );
322
323
324 static void ComputeTexMods( shaderStage_t *pStage, int bundleNum, vec4_t outMatrix[8])
325 {
326 int tm;
327 float matrix[6];
328 float tmpmatrix[6];
329 float currentmatrix[6];
330 float turb[2];
331 textureBundle_t *bundle = &pStage->bundle[bundleNum];
332 qboolean hasTurb = qfalse;
333
334 currentmatrix[0] = 1.0f; currentmatrix[2] = 0.0f; currentmatrix[4] = 0.0f;
335 currentmatrix[1] = 0.0f; currentmatrix[3] = 1.0f; currentmatrix[5] = 0.0f;
336
337 for ( tm = 0; tm < bundle->numTexMods ; tm++ ) {
338 switch ( bundle->texMods[tm].type )
339 {
340
341 case TMOD_NONE:
342 matrix[0] = 1.0f; matrix[2] = 0.0f; matrix[4] = 0.0f;
343 matrix[1] = 0.0f; matrix[3] = 1.0f; matrix[5] = 0.0f;
344 break;
345
346 case TMOD_TURBULENT:
347 RB_CalcTurbulentFactors(&bundle->texMods[tm].wave, &turb[0], &turb[1]);
348 break;
349
350 case TMOD_ENTITY_TRANSLATE:
351 RB_CalcScrollTexMatrix( backEnd.currentEntity->e.shaderTexCoord, matrix );
352 break;
353
354 case TMOD_SCROLL:
355 RB_CalcScrollTexMatrix( bundle->texMods[tm].scroll,
356 matrix );
357 break;
358
359 case TMOD_SCALE:
360 RB_CalcScaleTexMatrix( bundle->texMods[tm].scale,
361 matrix );
362 break;
363
364 case TMOD_STRETCH:
365 RB_CalcStretchTexMatrix( &bundle->texMods[tm].wave,
366 matrix );
367 break;
368
369 case TMOD_TRANSFORM:
370 RB_CalcTransformTexMatrix( &bundle->texMods[tm],
371 matrix );
372 break;
373
374 case TMOD_ROTATE:
375 RB_CalcRotateTexMatrix( bundle->texMods[tm].rotateSpeed,
376 matrix );
377 break;
378
379 default:
380 ri.Error( ERR_DROP, "ERROR: unknown texmod '%d' in shader '%s'", bundle->texMods[tm].type, tess.shader->name );
381 break;
382 }
383
384 switch ( bundle->texMods[tm].type )
385 {
386 case TMOD_TURBULENT:
387 outMatrix[tm*2+0][0] = 1; outMatrix[tm*2+0][1] = 0; outMatrix[tm*2+0][2] = 0;
388 outMatrix[tm*2+1][0] = 0; outMatrix[tm*2+1][1] = 1; outMatrix[tm*2+1][2] = 0;
389
390 outMatrix[tm*2+0][3] = turb[0];
391 outMatrix[tm*2+1][3] = turb[1];
392
393 hasTurb = qtrue;
394 break;
395
396 case TMOD_NONE:
397 case TMOD_ENTITY_TRANSLATE:
398 case TMOD_SCROLL:
399 case TMOD_SCALE:
400 case TMOD_STRETCH:
401 case TMOD_TRANSFORM:
402 case TMOD_ROTATE:
403 default:
404 outMatrix[tm*2+0][0] = matrix[0]; outMatrix[tm*2+0][1] = matrix[2]; outMatrix[tm*2+0][2] = matrix[4];
405 outMatrix[tm*2+1][0] = matrix[1]; outMatrix[tm*2+1][1] = matrix[3]; outMatrix[tm*2+1][2] = matrix[5];
406
407 outMatrix[tm*2+0][3] = 0;
408 outMatrix[tm*2+1][3] = 0;
409
410 tmpmatrix[0] = matrix[0] * currentmatrix[0] + matrix[2] * currentmatrix[1];
411 tmpmatrix[1] = matrix[1] * currentmatrix[0] + matrix[3] * currentmatrix[1];
412
413 tmpmatrix[2] = matrix[0] * currentmatrix[2] + matrix[2] * currentmatrix[3];
414 tmpmatrix[3] = matrix[1] * currentmatrix[2] + matrix[3] * currentmatrix[3];
415
416 tmpmatrix[4] = matrix[0] * currentmatrix[4] + matrix[2] * currentmatrix[5] + matrix[4];
417 tmpmatrix[5] = matrix[1] * currentmatrix[4] + matrix[3] * currentmatrix[5] + matrix[5];
418
419 currentmatrix[0] = tmpmatrix[0];
420 currentmatrix[1] = tmpmatrix[1];
421 currentmatrix[2] = tmpmatrix[2];
422 currentmatrix[3] = tmpmatrix[3];
423 currentmatrix[4] = tmpmatrix[4];
424 currentmatrix[5] = tmpmatrix[5];
425 break;
426 }
427 }
428
429 // if turb isn't used, only one matrix is needed
430 if ( !hasTurb ) {
431 tm = 0;
432
433 outMatrix[tm*2+0][0] = currentmatrix[0]; outMatrix[tm*2+0][1] = currentmatrix[2]; outMatrix[tm*2+0][2] = currentmatrix[4];
434 outMatrix[tm*2+1][0] = currentmatrix[1]; outMatrix[tm*2+1][1] = currentmatrix[3]; outMatrix[tm*2+1][2] = currentmatrix[5];
435
436 outMatrix[tm*2+0][3] = 0;
437 outMatrix[tm*2+1][3] = 0;
438 tm++;
439 }
440
441 for ( ; tm < TR_MAX_TEXMODS ; tm++ ) {
442 outMatrix[tm*2+0][0] = 1; outMatrix[tm*2+0][1] = 0; outMatrix[tm*2+0][2] = 0;
443 outMatrix[tm*2+1][0] = 0; outMatrix[tm*2+1][1] = 1; outMatrix[tm*2+1][2] = 0;
444
445 outMatrix[tm*2+0][3] = 0;
446 outMatrix[tm*2+1][3] = 0;
447 }
448 }
449
450
451 static void ComputeDeformValues(int *deformGen, vec5_t deformParams)
452 {
453 // u_DeformGen
454 *deformGen = DGEN_NONE;
455 if(!ShaderRequiresCPUDeforms(tess.shader))
456 {
457 deformStage_t *ds;
458
459 // only support the first one
460 ds = &tess.shader->deforms[0];
461
462 switch (ds->deformation)
463 {
464 case DEFORM_WAVE:
465 *deformGen = ds->deformationWave.func;
466
467 deformParams[0] = ds->deformationWave.base;
468 deformParams[1] = ds->deformationWave.amplitude;
469 deformParams[2] = ds->deformationWave.phase;
470 deformParams[3] = ds->deformationWave.frequency;
471 deformParams[4] = ds->deformationSpread;
472 break;
473
474 case DEFORM_BULGE:
475 *deformGen = DGEN_BULGE;
476
477 deformParams[0] = 0;
478 deformParams[1] = ds->bulgeHeight; // amplitude
479 deformParams[2] = ds->bulgeWidth; // phase
480 deformParams[3] = ds->bulgeSpeed; // frequency
481 deformParams[4] = 0;
482 break;
483
484 default:
485 break;
486 }
487 }
488 }
489
490
491 static void ProjectDlightTexture( void ) {
492 int l;
493 vec3_t origin;
494 float scale;
495 float radius;
496 int deformGen;
497 vec5_t deformParams;
498
499 if ( !backEnd.refdef.num_dlights ) {
500 return;
501 }
502
503 ComputeDeformValues(&deformGen, deformParams);
504
505 for ( l = 0 ; l < backEnd.refdef.num_dlights ; l++ ) {
506 dlight_t *dl;
507 shaderProgram_t *sp;
508 vec4_t vector;
509
510 if ( !( tess.dlightBits & ( 1 << l ) ) ) {
511 continue; // this surface definitely doesn't have any of this light
512 }
513
514 dl = &backEnd.refdef.dlights[l];
515 VectorCopy( dl->transformed, origin );
516 radius = dl->radius;
517 scale = 1.0f / radius;
518
519 sp = &tr.dlightShader[deformGen == DGEN_NONE ? 0 : 1];
520
521 backEnd.pc.c_dlightDraws++;
522
523 GLSL_BindProgram(sp);
524
525 GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
526
527 GLSL_SetUniformFloat(sp, UNIFORM_VERTEXLERP, glState.vertexAttribsInterpolation);
528
529 GLSL_SetUniformInt(sp, UNIFORM_DEFORMGEN, deformGen);
530 if (deformGen != DGEN_NONE)
531 {
532 GLSL_SetUniformFloat5(sp, UNIFORM_DEFORMPARAMS, deformParams);
533 GLSL_SetUniformFloat(sp, UNIFORM_TIME, tess.shaderTime);
534 }
535
536 vector[0] = dl->color[0];
537 vector[1] = dl->color[1];
538 vector[2] = dl->color[2];
539 vector[3] = 1.0f;
540 GLSL_SetUniformVec4(sp, UNIFORM_COLOR, vector);
541
542 vector[0] = origin[0];
543 vector[1] = origin[1];
544 vector[2] = origin[2];
545 vector[3] = scale;
546 GLSL_SetUniformVec4(sp, UNIFORM_DLIGHTINFO, vector);
547
548 GL_BindToTMU( tr.dlightImage, TB_COLORMAP );
549
550 // include GLS_DEPTHFUNC_EQUAL so alpha tested surfaces don't add light
551 // where they aren't rendered
552 if ( dl->additive ) {
553 GL_State( GLS_ATEST_GT_0 | GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL );
554 }
555 else {
556 GL_State( GLS_ATEST_GT_0 | GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL );
557 }
558
559 GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 1);
560
561 R_DrawElements(tess.numIndexes, tess.firstIndex);
562
563 backEnd.pc.c_totalIndexes += tess.numIndexes;
564 backEnd.pc.c_dlightIndexes += tess.numIndexes;
565 backEnd.pc.c_dlightVertexes += tess.numVertexes;
566 }
567 }
568
569
570 static void ComputeShaderColors( shaderStage_t *pStage, vec4_t baseColor, vec4_t vertColor, int blend )
571 {
572 qboolean isBlend = ((blend & GLS_SRCBLEND_BITS) == GLS_SRCBLEND_DST_COLOR)
573 || ((blend & GLS_SRCBLEND_BITS) == GLS_SRCBLEND_ONE_MINUS_DST_COLOR)
574 || ((blend & GLS_DSTBLEND_BITS) == GLS_DSTBLEND_SRC_COLOR)
575 || ((blend & GLS_DSTBLEND_BITS) == GLS_DSTBLEND_ONE_MINUS_SRC_COLOR);
576
577 qboolean is2DDraw = backEnd.currentEntity == &backEnd.entity2D;
578
579 float overbright = (isBlend || is2DDraw) ? 1.0f : (float)(1 << tr.overbrightBits);
580
581 fog_t *fog;
582
583 baseColor[0] =
584 baseColor[1] =
585 baseColor[2] =
586 baseColor[3] = 1.0f;
587
588 vertColor[0] =
589 vertColor[1] =
590 vertColor[2] =
591 vertColor[3] = 0.0f;
592
593 //
594 // rgbGen
595 //
596 switch ( pStage->rgbGen )
597 {
598 case CGEN_EXACT_VERTEX:
599 case CGEN_EXACT_VERTEX_LIT:
600 baseColor[0] =
601 baseColor[1] =
602 baseColor[2] =
603 baseColor[3] = 0.0f;
604
605 vertColor[0] =
606 vertColor[1] =
607 vertColor[2] = overbright;
608 vertColor[3] = 1.0f;
609 break;
610 case CGEN_CONST:
611 baseColor[0] = pStage->constantColor[0] / 255.0f;
612 baseColor[1] = pStage->constantColor[1] / 255.0f;
613 baseColor[2] = pStage->constantColor[2] / 255.0f;
614 baseColor[3] = pStage->constantColor[3] / 255.0f;
615 break;
616 case CGEN_VERTEX:
617 case CGEN_VERTEX_LIT:
618 baseColor[0] =
619 baseColor[1] =
620 baseColor[2] =
621 baseColor[3] = 0.0f;
622
623 vertColor[0] =
624 vertColor[1] =
625 vertColor[2] =
626 vertColor[3] = 1.0f;
627 break;
628 case CGEN_ONE_MINUS_VERTEX:
629 baseColor[0] =
630 baseColor[1] =
631 baseColor[2] = 1.0f;
632
633 vertColor[0] =
634 vertColor[1] =
635 vertColor[2] = -1.0f;
636 break;
637 case CGEN_FOG:
638 fog = tr.world->fogs + tess.fogNum;
639
640 baseColor[0] = ((unsigned char *)(&fog->colorInt))[0] / 255.0f;
641 baseColor[1] = ((unsigned char *)(&fog->colorInt))[1] / 255.0f;
642 baseColor[2] = ((unsigned char *)(&fog->colorInt))[2] / 255.0f;
643 baseColor[3] = ((unsigned char *)(&fog->colorInt))[3] / 255.0f;
644 break;
645 case CGEN_WAVEFORM:
646 baseColor[0] =
647 baseColor[1] =
648 baseColor[2] = RB_CalcWaveColorSingle( &pStage->rgbWave );
649 break;
650 case CGEN_ENTITY:
651 if (backEnd.currentEntity)
652 {
653 baseColor[0] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[0] / 255.0f;
654 baseColor[1] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[1] / 255.0f;
655 baseColor[2] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[2] / 255.0f;
656 baseColor[3] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f;
657 }
658 break;
659 case CGEN_ONE_MINUS_ENTITY:
660 if (backEnd.currentEntity)
661 {
662 baseColor[0] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[0] / 255.0f;
663 baseColor[1] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[1] / 255.0f;
664 baseColor[2] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[2] / 255.0f;
665 baseColor[3] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f;
666 }
667 break;
668 case CGEN_IDENTITY:
669 case CGEN_LIGHTING_DIFFUSE:
670 baseColor[0] =
671 baseColor[1] =
672 baseColor[2] = overbright;
673 break;
674 case CGEN_IDENTITY_LIGHTING:
675 case CGEN_BAD:
676 break;
677 }
678
679 //
680 // alphaGen
681 //
682 switch ( pStage->alphaGen )
683 {
684 case AGEN_SKIP:
685 break;
686 case AGEN_CONST:
687 baseColor[3] = pStage->constantColor[3] / 255.0f;
688 vertColor[3] = 0.0f;
689 break;
690 case AGEN_WAVEFORM:
691 baseColor[3] = RB_CalcWaveAlphaSingle( &pStage->alphaWave );
692 vertColor[3] = 0.0f;
693 break;
694 case AGEN_ENTITY:
695 if (backEnd.currentEntity)
696 {
697 baseColor[3] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f;
698 }
699 vertColor[3] = 0.0f;
700 break;
701 case AGEN_ONE_MINUS_ENTITY:
702 if (backEnd.currentEntity)
703 {
704 baseColor[3] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f;
705 }
706 vertColor[3] = 0.0f;
707 break;
708 case AGEN_VERTEX:
709 baseColor[3] = 0.0f;
710 vertColor[3] = 1.0f;
711 break;
712 case AGEN_ONE_MINUS_VERTEX:
713 baseColor[3] = 1.0f;
714 vertColor[3] = -1.0f;
715 break;
716 case AGEN_IDENTITY:
717 case AGEN_LIGHTING_SPECULAR:
718 case AGEN_PORTAL:
719 // Done entirely in vertex program
720 baseColor[3] = 1.0f;
721 vertColor[3] = 0.0f;
722 break;
723 }
724
725 }
726
727
728 static void ComputeFogValues(vec4_t fogDistanceVector, vec4_t fogDepthVector, float *eyeT)
729 {
730 // from RB_CalcFogTexCoords()
731 fog_t *fog;
732 vec3_t local;
733
734 if (!tess.fogNum)
735 return;
736
737 fog = tr.world->fogs + tess.fogNum;
738
739 VectorSubtract( backEnd.or.origin, backEnd.viewParms.or.origin, local );
740 fogDistanceVector[0] = -backEnd.or.modelMatrix[2];
741 fogDistanceVector[1] = -backEnd.or.modelMatrix[6];
742 fogDistanceVector[2] = -backEnd.or.modelMatrix[10];
743 fogDistanceVector[3] = DotProduct( local, backEnd.viewParms.or.axis[0] );
744
745 // scale the fog vectors based on the fog's thickness
746 VectorScale4(fogDistanceVector, fog->tcScale, fogDistanceVector);
747
748 // rotate the gradient vector for this orientation
749 if ( fog->hasSurface ) {
750 fogDepthVector[0] = fog->surface[0] * backEnd.or.axis[0][0] +
751 fog->surface[1] * backEnd.or.axis[0][1] + fog->surface[2] * backEnd.or.axis[0][2];
752 fogDepthVector[1] = fog->surface[0] * backEnd.or.axis[1][0] +
753 fog->surface[1] * backEnd.or.axis[1][1] + fog->surface[2] * backEnd.or.axis[1][2];
754 fogDepthVector[2] = fog->surface[0] * backEnd.or.axis[2][0] +
755 fog->surface[1] * backEnd.or.axis[2][1] + fog->surface[2] * backEnd.or.axis[2][2];
756 fogDepthVector[3] = -fog->surface[3] + DotProduct( backEnd.or.origin, fog->surface );
757
758 *eyeT = DotProduct( backEnd.or.viewOrigin, fogDepthVector ) + fogDepthVector[3];
759 } else {
760 *eyeT = 1; // non-surface fog always has eye inside
761 }
762 }
763
764
765 static void ComputeFogColorMask( shaderStage_t *pStage, vec4_t fogColorMask )
766 {
767 switch(pStage->adjustColorsForFog)
768 {
769 case ACFF_MODULATE_RGB:
770 fogColorMask[0] =
771 fogColorMask[1] =
772 fogColorMask[2] = 1.0f;
773 fogColorMask[3] = 0.0f;
774 break;
775 case ACFF_MODULATE_ALPHA:
776 fogColorMask[0] =
777 fogColorMask[1] =
778 fogColorMask[2] = 0.0f;
779 fogColorMask[3] = 1.0f;
780 break;
781 case ACFF_MODULATE_RGBA:
782 fogColorMask[0] =
783 fogColorMask[1] =
784 fogColorMask[2] =
785 fogColorMask[3] = 1.0f;
786 break;
787 default:
788 fogColorMask[0] =
789 fogColorMask[1] =
790 fogColorMask[2] =
791 fogColorMask[3] = 0.0f;
792 break;
793 }
794 }
795
796
797 static void ForwardDlight( void ) {
798 int l;
799 //vec3_t origin;
800 //float scale;
801 float radius;
802
803 int deformGen;
804 vec5_t deformParams;
805
806 vec4_t fogDistanceVector, fogDepthVector = {0, 0, 0, 0};
807 float eyeT = 0;
808
809 shaderCommands_t *input = &tess;
810 shaderStage_t *pStage = tess.xstages[0];
811
812 if ( !backEnd.refdef.num_dlights ) {
813 return;
814 }
815
816 ComputeDeformValues(&deformGen, deformParams);
817
818 ComputeFogValues(fogDistanceVector, fogDepthVector, &eyeT);
819
820 for ( l = 0 ; l < backEnd.refdef.num_dlights ; l++ ) {
821 dlight_t *dl;
822 shaderProgram_t *sp;
823 vec4_t vector;
824 vec4_t texMatrix[8];
825
826 if ( !( tess.dlightBits & ( 1 << l ) ) ) {
827 continue; // this surface definitely doesn't have any of this light
828 }
829
830 dl = &backEnd.refdef.dlights[l];
831 //VectorCopy( dl->transformed, origin );
832 radius = dl->radius;
833 //scale = 1.0f / radius;
834
835 //if (pStage->glslShaderGroup == tr.lightallShader)
836 {
837 int index = pStage->glslShaderIndex;
838
839 index &= ~LIGHTDEF_LIGHTTYPE_MASK;
840 index |= LIGHTDEF_USE_LIGHT_VECTOR;
841
842 sp = &tr.lightallShader[index];
843 }
844
845 backEnd.pc.c_lightallDraws++;
846
847 GLSL_BindProgram(sp);
848
849 GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
850 GLSL_SetUniformVec3(sp, UNIFORM_VIEWORIGIN, backEnd.viewParms.or.origin);
851 GLSL_SetUniformVec3(sp, UNIFORM_LOCALVIEWORIGIN, backEnd.or.viewOrigin);
852
853 GLSL_SetUniformFloat(sp, UNIFORM_VERTEXLERP, glState.vertexAttribsInterpolation);
854
855 GLSL_SetUniformInt(sp, UNIFORM_DEFORMGEN, deformGen);
856 if (deformGen != DGEN_NONE)
857 {
858 GLSL_SetUniformFloat5(sp, UNIFORM_DEFORMPARAMS, deformParams);
859 GLSL_SetUniformFloat(sp, UNIFORM_TIME, tess.shaderTime);
860 }
861
862 if ( input->fogNum ) {
863 vec4_t fogColorMask;
864
865 GLSL_SetUniformVec4(sp, UNIFORM_FOGDISTANCE, fogDistanceVector);
866 GLSL_SetUniformVec4(sp, UNIFORM_FOGDEPTH, fogDepthVector);
867 GLSL_SetUniformFloat(sp, UNIFORM_FOGEYET, eyeT);
868
869 ComputeFogColorMask(pStage, fogColorMask);
870
871 GLSL_SetUniformVec4(sp, UNIFORM_FOGCOLORMASK, fogColorMask);
872 }
873
874 {
875 vec4_t baseColor;
876 vec4_t vertColor;
877
878 ComputeShaderColors(pStage, baseColor, vertColor, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE);
879
880 GLSL_SetUniformVec4(sp, UNIFORM_BASECOLOR, baseColor);
881 GLSL_SetUniformVec4(sp, UNIFORM_VERTCOLOR, vertColor);
882 }
883
884 if (pStage->alphaGen == AGEN_PORTAL)
885 {
886 GLSL_SetUniformFloat(sp, UNIFORM_PORTALRANGE, tess.shader->portalRange);
887 }
888
889 GLSL_SetUniformInt(sp, UNIFORM_COLORGEN, pStage->rgbGen);
890 GLSL_SetUniformInt(sp, UNIFORM_ALPHAGEN, pStage->alphaGen);
891
892 GLSL_SetUniformVec3(sp, UNIFORM_DIRECTEDLIGHT, dl->color);
893
894 VectorSet(vector, 0, 0, 0);
895 GLSL_SetUniformVec3(sp, UNIFORM_AMBIENTLIGHT, vector);
896
897 VectorCopy(dl->origin, vector);
898 vector[3] = 1.0f;
899 GLSL_SetUniformVec4(sp, UNIFORM_LIGHTORIGIN, vector);
900
901 GLSL_SetUniformFloat(sp, UNIFORM_LIGHTRADIUS, radius);
902
903 GLSL_SetUniformVec4(sp, UNIFORM_NORMALSCALE, pStage->normalScale);
904 GLSL_SetUniformVec4(sp, UNIFORM_SPECULARSCALE, pStage->specularScale);
905
906 // include GLS_DEPTHFUNC_EQUAL so alpha tested surfaces don't add light
907 // where they aren't rendered
908 GL_State( GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL );
909 GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 0);
910
911 GLSL_SetUniformMat4(sp, UNIFORM_MODELMATRIX, backEnd.or.transformMatrix);
912
913 if (pStage->bundle[TB_DIFFUSEMAP].image[0])
914 R_BindAnimatedImageToTMU( &pStage->bundle[TB_DIFFUSEMAP], TB_DIFFUSEMAP);
915
916 // bind textures that are sampled and used in the glsl shader, and
917 // bind whiteImage to textures that are sampled but zeroed in the glsl shader
918 //
919 // alternatives:
920 // - use the last bound texture
921 // -> costs more to sample a higher res texture then throw out the result
922 // - disable texture sampling in glsl shader with #ifdefs, as before
923 // -> increases the number of shaders that must be compiled
924 //
925
926 if (pStage->bundle[TB_NORMALMAP].image[0])
927 {
928 R_BindAnimatedImageToTMU( &pStage->bundle[TB_NORMALMAP], TB_NORMALMAP);
929 }
930 else if (r_normalMapping->integer)
931 GL_BindToTMU( tr.whiteImage, TB_NORMALMAP );
932
933 if (pStage->bundle[TB_SPECULARMAP].image[0])
934 {
935 R_BindAnimatedImageToTMU( &pStage->bundle[TB_SPECULARMAP], TB_SPECULARMAP);
936 }
937 else if (r_specularMapping->integer)
938 GL_BindToTMU( tr.whiteImage, TB_SPECULARMAP );
939
940 {
941 vec4_t enableTextures;
942
943 VectorSet4(enableTextures, 0.0f, 0.0f, 0.0f, 0.0f);
944 GLSL_SetUniformVec4(sp, UNIFORM_ENABLETEXTURES, enableTextures);
945 }
946
947 if (r_dlightMode->integer >= 2)
948 GL_BindToTMU(tr.shadowCubemaps[l], TB_SHADOWMAP);
949
950 ComputeTexMods( pStage, TB_DIFFUSEMAP, texMatrix );
951 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX0, texMatrix[0]);
952 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX1, texMatrix[1]);
953 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX2, texMatrix[2]);
954 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX3, texMatrix[3]);
955 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX4, texMatrix[4]);
956 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX5, texMatrix[5]);
957 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX6, texMatrix[6]);
958 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX7, texMatrix[7]);
959
960 GLSL_SetUniformInt(sp, UNIFORM_TCGEN0, pStage->bundle[0].tcGen);
961
962 //
963 // draw
964 //
965
966 R_DrawElements(input->numIndexes, input->firstIndex);
967
968 backEnd.pc.c_totalIndexes += tess.numIndexes;
969 backEnd.pc.c_dlightIndexes += tess.numIndexes;
970 backEnd.pc.c_dlightVertexes += tess.numVertexes;
971 }
972 }
973
974
975 static void ProjectPshadowVBOGLSL( void ) {
976 int l;
977 vec3_t origin;
978 float radius;
979
980 int deformGen;
981 vec5_t deformParams;
982
983 shaderCommands_t *input = &tess;
984
985 if ( !backEnd.refdef.num_pshadows ) {
986 return;
987 }
988
989 ComputeDeformValues(&deformGen, deformParams);
990
991 for ( l = 0 ; l < backEnd.refdef.num_pshadows ; l++ ) {
992 pshadow_t *ps;
993 shaderProgram_t *sp;
994 vec4_t vector;
995
996 if ( !( tess.pshadowBits & ( 1 << l ) ) ) {
997 continue; // this surface definitely doesn't have any of this shadow
998 }
999
1000 ps = &backEnd.refdef.pshadows[l];
1001 VectorCopy( ps->lightOrigin, origin );
1002 radius = ps->lightRadius;
1003
1004 sp = &tr.pshadowShader;
1005
1006 GLSL_BindProgram(sp);
1007
1008 GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
1009
1010 VectorCopy(origin, vector);
1011 vector[3] = 1.0f;
1012 GLSL_SetUniformVec4(sp, UNIFORM_LIGHTORIGIN, vector);
1013
1014 VectorScale(ps->lightViewAxis[0], 1.0f / ps->viewRadius, vector);
1015 GLSL_SetUniformVec3(sp, UNIFORM_LIGHTFORWARD, vector);
1016
1017 VectorScale(ps->lightViewAxis[1], 1.0f / ps->viewRadius, vector);
1018 GLSL_SetUniformVec3(sp, UNIFORM_LIGHTRIGHT, vector);
1019
1020 VectorScale(ps->lightViewAxis[2], 1.0f / ps->viewRadius, vector);
1021 GLSL_SetUniformVec3(sp, UNIFORM_LIGHTUP, vector);
1022
1023 GLSL_SetUniformFloat(sp, UNIFORM_LIGHTRADIUS, radius);
1024
1025 // include GLS_DEPTHFUNC_EQUAL so alpha tested surfaces don't add light
1026 // where they aren't rendered
1027 GL_State( GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA | GLS_DEPTHFUNC_EQUAL );
1028 GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 0);
1029
1030 GL_BindToTMU( tr.pshadowMaps[l], TB_DIFFUSEMAP );
1031
1032 //
1033 // draw
1034 //
1035
1036 R_DrawElements(input->numIndexes, input->firstIndex);
1037
1038 backEnd.pc.c_totalIndexes += tess.numIndexes;
1039 //backEnd.pc.c_dlightIndexes += tess.numIndexes;
1040 }
1041 }
1042
1043
1044
1045 /*
1046 ===================
1047 RB_FogPass
1048
1049 Blends a fog texture on top of everything else
1050 ===================
1051 */
1052 static void RB_FogPass( void ) {
1053 fog_t *fog;
1054 vec4_t color;
1055 vec4_t fogDistanceVector, fogDepthVector = {0, 0, 0, 0};
1056 float eyeT = 0;
1057 shaderProgram_t *sp;
1058
1059 int deformGen;
1060 vec5_t deformParams;
1061
1062 ComputeDeformValues(&deformGen, deformParams);
1063
1064 {
1065 int index = 0;
1066
1067 if (deformGen != DGEN_NONE)
1068 index |= FOGDEF_USE_DEFORM_VERTEXES;
1069
1070 if (glState.vertexAnimation)
1071 index |= FOGDEF_USE_VERTEX_ANIMATION;
1072 else if (glState.boneAnimation)
1073 index |= FOGDEF_USE_BONE_ANIMATION;
1074
1075 sp = &tr.fogShader[index];
1076 }
1077
1078 backEnd.pc.c_fogDraws++;
1079
1080 GLSL_BindProgram(sp);
1081
1082 fog = tr.world->fogs + tess.fogNum;
1083
1084 GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
1085
1086 GLSL_SetUniformFloat(sp, UNIFORM_VERTEXLERP, glState.vertexAttribsInterpolation);
1087
1088 if (glState.boneAnimation)
1089 {
1090 GLSL_SetUniformMat4BoneMatrix(sp, UNIFORM_BONEMATRIX, glState.boneMatrix, glState.boneAnimation);
1091 }
1092
1093 GLSL_SetUniformInt(sp, UNIFORM_DEFORMGEN, deformGen);
1094 if (deformGen != DGEN_NONE)
1095 {
1096 GLSL_SetUniformFloat5(sp, UNIFORM_DEFORMPARAMS, deformParams);
1097 GLSL_SetUniformFloat(sp, UNIFORM_TIME, tess.shaderTime);
1098 }
1099
1100 color[0] = ((unsigned char *)(&fog->colorInt))[0] / 255.0f;
1101 color[1] = ((unsigned char *)(&fog->colorInt))[1] / 255.0f;
1102 color[2] = ((unsigned char *)(&fog->colorInt))[2] / 255.0f;
1103 color[3] = ((unsigned char *)(&fog->colorInt))[3] / 255.0f;
1104 GLSL_SetUniformVec4(sp, UNIFORM_COLOR, color);
1105
1106 ComputeFogValues(fogDistanceVector, fogDepthVector, &eyeT);
1107
1108 GLSL_SetUniformVec4(sp, UNIFORM_FOGDISTANCE, fogDistanceVector);
1109 GLSL_SetUniformVec4(sp, UNIFORM_FOGDEPTH, fogDepthVector);
1110 GLSL_SetUniformFloat(sp, UNIFORM_FOGEYET, eyeT);
1111
1112 if ( tess.shader->fogPass == FP_EQUAL ) {
1113 GL_State( GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA | GLS_DEPTHFUNC_EQUAL );
1114 } else {
1115 GL_State( GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA );
1116 }
1117 GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 0);
1118
1119 R_DrawElements(tess.numIndexes, tess.firstIndex);
1120 }
1121
1122
1123 static unsigned int RB_CalcShaderVertexAttribs( shaderCommands_t *input )
1124 {
1125 unsigned int vertexAttribs = input->shader->vertexAttribs;
1126
1127 if(glState.vertexAnimation)
1128 {
1129 vertexAttribs |= ATTR_POSITION2;
1130 if (vertexAttribs & ATTR_NORMAL)
1131 {
1132 vertexAttribs |= ATTR_NORMAL2;
1133 vertexAttribs |= ATTR_TANGENT2;
1134 }
1135 }
1136
1137 return vertexAttribs;
1138 }
1139
1140 static void RB_IterateStagesGeneric( shaderCommands_t *input )
1141 {
1142 int stage;
1143
1144 vec4_t fogDistanceVector, fogDepthVector = {0, 0, 0, 0};
1145 float eyeT = 0;
1146
1147 int deformGen;
1148 vec5_t deformParams;
1149
1150 qboolean renderToCubemap = tr.renderCubeFbo && glState.currentFBO == tr.renderCubeFbo;
1151
1152 ComputeDeformValues(&deformGen, deformParams);
1153
1154 ComputeFogValues(fogDistanceVector, fogDepthVector, &eyeT);
1155
1156 for ( stage = 0; stage < MAX_SHADER_STAGES; stage++ )
1157 {
1158 shaderStage_t *pStage = input->xstages[stage];
1159 shaderProgram_t *sp;
1160 vec4_t texMatrix[8];
1161
1162 if ( !pStage )
1163 {
1164 break;
1165 }
1166
1167 if (backEnd.depthFill)
1168 {
1169 if (pStage->glslShaderGroup == tr.lightallShader)
1170 {
1171 int index = 0;
1172
1173 if (backEnd.currentEntity && backEnd.currentEntity != &tr.worldEntity)
1174 {
1175 if (glState.boneAnimation)
1176 {
1177 index |= LIGHTDEF_ENTITY_BONE_ANIMATION;
1178 }
1179 else
1180 {
1181 index |= LIGHTDEF_ENTITY_VERTEX_ANIMATION;
1182 }
1183 }
1184
1185 if (pStage->stateBits & GLS_ATEST_BITS)
1186 {
1187 index |= LIGHTDEF_USE_TCGEN_AND_TCMOD;
1188 }
1189
1190 sp = &pStage->glslShaderGroup[index];
1191 }
1192 else
1193 {
1194 int shaderAttribs = 0;
1195
1196 if (tess.shader->numDeforms && !ShaderRequiresCPUDeforms(tess.shader))
1197 {
1198 shaderAttribs |= GENERICDEF_USE_DEFORM_VERTEXES;
1199 }
1200
1201 if (glState.vertexAnimation)
1202 {
1203 shaderAttribs |= GENERICDEF_USE_VERTEX_ANIMATION;
1204 }
1205 else if (glState.boneAnimation)
1206 {
1207 shaderAttribs |= GENERICDEF_USE_BONE_ANIMATION;
1208 }
1209
1210 if (pStage->stateBits & GLS_ATEST_BITS)
1211 {
1212 shaderAttribs |= GENERICDEF_USE_TCGEN_AND_TCMOD;
1213 }
1214
1215 sp = &tr.genericShader[shaderAttribs];
1216 }
1217 }
1218 else if (pStage->glslShaderGroup == tr.lightallShader)
1219 {
1220 int index = pStage->glslShaderIndex;
1221
1222 if (backEnd.currentEntity && backEnd.currentEntity != &tr.worldEntity)
1223 {
1224 if (glState.boneAnimation)
1225 {
1226 index |= LIGHTDEF_ENTITY_BONE_ANIMATION;
1227 }
1228 else
1229 {
1230 index |= LIGHTDEF_ENTITY_VERTEX_ANIMATION;
1231 }
1232 }
1233
1234 if (r_sunlightMode->integer && (backEnd.viewParms.flags & VPF_USESUNLIGHT) && (index & LIGHTDEF_LIGHTTYPE_MASK))
1235 {
1236 index |= LIGHTDEF_USE_SHADOWMAP;
1237 }
1238
1239 if (r_lightmap->integer && ((index & LIGHTDEF_LIGHTTYPE_MASK) == LIGHTDEF_USE_LIGHTMAP))
1240 {
1241 index = LIGHTDEF_USE_TCGEN_AND_TCMOD;
1242 }
1243
1244 sp = &pStage->glslShaderGroup[index];
1245
1246 backEnd.pc.c_lightallDraws++;
1247 }
1248 else
1249 {
1250 sp = GLSL_GetGenericShaderProgram(stage);
1251
1252 backEnd.pc.c_genericDraws++;
1253 }
1254
1255 GLSL_BindProgram(sp);
1256
1257 GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
1258 GLSL_SetUniformVec3(sp, UNIFORM_VIEWORIGIN, backEnd.viewParms.or.origin);
1259 GLSL_SetUniformVec3(sp, UNIFORM_LOCALVIEWORIGIN, backEnd.or.viewOrigin);
1260
1261 GLSL_SetUniformFloat(sp, UNIFORM_VERTEXLERP, glState.vertexAttribsInterpolation);
1262
1263 if (glState.boneAnimation)
1264 {
1265 GLSL_SetUniformMat4BoneMatrix(sp, UNIFORM_BONEMATRIX, glState.boneMatrix, glState.boneAnimation);
1266 }
1267
1268 GLSL_SetUniformInt(sp, UNIFORM_DEFORMGEN, deformGen);
1269 if (deformGen != DGEN_NONE)
1270 {
1271 GLSL_SetUniformFloat5(sp, UNIFORM_DEFORMPARAMS, deformParams);
1272 GLSL_SetUniformFloat(sp, UNIFORM_TIME, tess.shaderTime);
1273 }
1274
1275 if ( input->fogNum ) {
1276 GLSL_SetUniformVec4(sp, UNIFORM_FOGDISTANCE, fogDistanceVector);
1277 GLSL_SetUniformVec4(sp, UNIFORM_FOGDEPTH, fogDepthVector);
1278 GLSL_SetUniformFloat(sp, UNIFORM_FOGEYET, eyeT);
1279 }
1280
1281 GL_State( pStage->stateBits );
1282 if ((pStage->stateBits & GLS_ATEST_BITS) == GLS_ATEST_GT_0)
1283 {
1284 GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 1);
1285 }
1286 else if ((pStage->stateBits & GLS_ATEST_BITS) == GLS_ATEST_LT_80)
1287 {
1288 GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 2);
1289 }
1290 else if ((pStage->stateBits & GLS_ATEST_BITS) == GLS_ATEST_GE_80)
1291 {
1292 GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 3);
1293 }
1294 else
1295 {
1296 GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 0);
1297 }
1298
1299
1300 {
1301 vec4_t baseColor;
1302 vec4_t vertColor;
1303
1304 ComputeShaderColors(pStage, baseColor, vertColor, pStage->stateBits);
1305
1306 GLSL_SetUniformVec4(sp, UNIFORM_BASECOLOR, baseColor);
1307 GLSL_SetUniformVec4(sp, UNIFORM_VERTCOLOR, vertColor);
1308 }
1309
1310 if (pStage->rgbGen == CGEN_LIGHTING_DIFFUSE)
1311 {
1312 vec4_t vec;
1313
1314 VectorScale(backEnd.currentEntity->ambientLight, 1.0f / 255.0f, vec);
1315 GLSL_SetUniformVec3(sp, UNIFORM_AMBIENTLIGHT, vec);
1316
1317 VectorScale(backEnd.currentEntity->directedLight, 1.0f / 255.0f, vec);
1318 GLSL_SetUniformVec3(sp, UNIFORM_DIRECTEDLIGHT, vec);
1319
1320 VectorCopy(backEnd.currentEntity->lightDir, vec);
1321 vec[3] = 0.0f;
1322 GLSL_SetUniformVec4(sp, UNIFORM_LIGHTORIGIN, vec);
1323 GLSL_SetUniformVec3(sp, UNIFORM_MODELLIGHTDIR, backEnd.currentEntity->modelLightDir);
1324
1325 GLSL_SetUniformFloat(sp, UNIFORM_LIGHTRADIUS, 0.0f);
1326 }
1327
1328 if (pStage->alphaGen == AGEN_PORTAL)
1329 {
1330 GLSL_SetUniformFloat(sp, UNIFORM_PORTALRANGE, tess.shader->portalRange);
1331 }
1332
1333 GLSL_SetUniformInt(sp, UNIFORM_COLORGEN, pStage->rgbGen);
1334 GLSL_SetUniformInt(sp, UNIFORM_ALPHAGEN, pStage->alphaGen);
1335
1336 if ( input->fogNum )
1337 {
1338 vec4_t fogColorMask;
1339
1340 ComputeFogColorMask(pStage, fogColorMask);
1341
1342 GLSL_SetUniformVec4(sp, UNIFORM_FOGCOLORMASK, fogColorMask);
1343 }
1344
1345 if (r_lightmap->integer)
1346 {
1347 vec4_t st[2];
1348 VectorSet4(st[0], 1.0f, 0.0f, 0.0f, 0.0f);
1349 VectorSet4(st[1], 0.0f, 1.0f, 0.0f, 0.0f);
1350 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX0, st[0]);
1351 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX1, st[1]);
1352 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX2, st[0]);
1353 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX3, st[1]);
1354 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX4, st[0]);
1355 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX5, st[1]);
1356 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX6, st[0]);
1357 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX7, st[1]);
1358
1359 GLSL_SetUniformInt(sp, UNIFORM_TCGEN0, TCGEN_LIGHTMAP);
1360 }
1361 else
1362 {
1363 ComputeTexMods(pStage, TB_DIFFUSEMAP, texMatrix);
1364 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX0, texMatrix[0]);
1365 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX1, texMatrix[1]);
1366 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX2, texMatrix[2]);
1367 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX3, texMatrix[3]);
1368 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX4, texMatrix[4]);
1369 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX5, texMatrix[5]);
1370 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX6, texMatrix[6]);
1371 GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX7, texMatrix[7]);
1372
1373 GLSL_SetUniformInt(sp, UNIFORM_TCGEN0, pStage->bundle[0].tcGen);
1374 if (pStage->bundle[0].tcGen == TCGEN_VECTOR)
1375 {
1376 vec3_t vec;
1377
1378 VectorCopy(pStage->bundle[0].tcGenVectors[0], vec);
1379 GLSL_SetUniformVec3(sp, UNIFORM_TCGEN0VECTOR0, vec);
1380 VectorCopy(pStage->bundle[0].tcGenVectors[1], vec);
1381 GLSL_SetUniformVec3(sp, UNIFORM_TCGEN0VECTOR1, vec);
1382 }
1383 }
1384
1385 GLSL_SetUniformMat4(sp, UNIFORM_MODELMATRIX, backEnd.or.transformMatrix);
1386
1387 GLSL_SetUniformVec4(sp, UNIFORM_NORMALSCALE, pStage->normalScale);
1388
1389 {
1390 vec4_t specularScale;
1391 Vector4Copy(pStage->specularScale, specularScale);
1392
1393 if (renderToCubemap)
1394 {
1395 // force specular to nonmetal if rendering cubemaps
1396 if (r_pbr->integer)
1397 specularScale[1] = 0.0f;
1398 }
1399
1400 GLSL_SetUniformVec4(sp, UNIFORM_SPECULARSCALE, specularScale);
1401 }
1402
1403 //GLSL_SetUniformFloat(sp, UNIFORM_MAPLIGHTSCALE, backEnd.refdef.mapLightScale);
1404
1405 //
1406 // do multitexture
1407 //
1408 if ( backEnd.depthFill )
1409 {
1410 if (!(pStage->stateBits & GLS_ATEST_BITS))
1411 GL_BindToTMU( tr.whiteImage, TB_COLORMAP );
1412 else if ( pStage->bundle[TB_COLORMAP].image[0] != 0 )
1413 R_BindAnimatedImageToTMU( &pStage->bundle[TB_COLORMAP], TB_COLORMAP );
1414 }
1415 else if ( pStage->glslShaderGroup == tr.lightallShader )
1416 {
1417 int i;
1418 vec4_t enableTextures;
1419
1420 if (r_sunlightMode->integer && (backEnd.viewParms.flags & VPF_USESUNLIGHT) && (pStage->glslShaderIndex & LIGHTDEF_LIGHTTYPE_MASK))
1421 {
1422 // FIXME: screenShadowImage is NULL if no framebuffers
1423 if (tr.screenShadowImage)
1424 GL_BindToTMU(tr.screenShadowImage, TB_SHADOWMAP);
1425 GLSL_SetUniformVec3(sp, UNIFORM_PRIMARYLIGHTAMBIENT, backEnd.refdef.sunAmbCol);
1426 if (r_pbr->integer)
1427 {
1428 vec3_t color;
1429
1430 color[0] = backEnd.refdef.sunCol[0] * backEnd.refdef.sunCol[0];
1431 color[1] = backEnd.refdef.sunCol[1] * backEnd.refdef.sunCol[1];
1432 color[2] = backEnd.refdef.sunCol[2] * backEnd.refdef.sunCol[2];
1433 GLSL_SetUniformVec3(sp, UNIFORM_PRIMARYLIGHTCOLOR, color);
1434 }
1435 else
1436 {
1437 GLSL_SetUniformVec3(sp, UNIFORM_PRIMARYLIGHTCOLOR, backEnd.refdef.sunCol);
1438 }
1439 GLSL_SetUniformVec4(sp, UNIFORM_PRIMARYLIGHTORIGIN, backEnd.refdef.sunDir);
1440 }
1441
1442 VectorSet4(enableTextures, 0, 0, 0, 0);
1443 if ((r_lightmap->integer == 1 || r_lightmap->integer == 2) && pStage->bundle[TB_LIGHTMAP].image[0])
1444 {
1445 for (i = 0; i < NUM_TEXTURE_BUNDLES; i++)
1446 {
1447 if (i == TB_COLORMAP)
1448 R_BindAnimatedImageToTMU( &pStage->bundle[TB_LIGHTMAP], i);
1449 else
1450 GL_BindToTMU( tr.whiteImage, i );
1451 }
1452 }
1453 else if (r_lightmap->integer == 3 && pStage->bundle[TB_DELUXEMAP].image[0])
1454 {
1455 for (i = 0; i < NUM_TEXTURE_BUNDLES; i++)
1456 {
1457 if (i == TB_COLORMAP)
1458 R_BindAnimatedImageToTMU( &pStage->bundle[TB_DELUXEMAP], i);
1459 else
1460 GL_BindToTMU( tr.whiteImage, i );
1461 }
1462 }
1463 else
1464 {
1465 qboolean light = (pStage->glslShaderIndex & LIGHTDEF_LIGHTTYPE_MASK) != 0;
1466 qboolean fastLight = !(r_normalMapping->integer || r_specularMapping->integer);
1467
1468 if (pStage->bundle[TB_DIFFUSEMAP].image[0])
1469 R_BindAnimatedImageToTMU( &pStage->bundle[TB_DIFFUSEMAP], TB_DIFFUSEMAP);
1470
1471 if (pStage->bundle[TB_LIGHTMAP].image[0])
1472 R_BindAnimatedImageToTMU( &pStage->bundle[TB_LIGHTMAP], TB_LIGHTMAP);
1473
1474 // bind textures that are sampled and used in the glsl shader, and
1475 // bind whiteImage to textures that are sampled but zeroed in the glsl shader
1476 //
1477 // alternatives:
1478 // - use the last bound texture
1479 // -> costs more to sample a higher res texture then throw out the result
1480 // - disable texture sampling in glsl shader with #ifdefs, as before
1481 // -> increases the number of shaders that must be compiled
1482 //
1483 if (light && !fastLight)
1484 {
1485 if (pStage->bundle[TB_NORMALMAP].image[0])
1486 {
1487 R_BindAnimatedImageToTMU( &pStage->bundle[TB_NORMALMAP], TB_NORMALMAP);
1488 enableTextures[0] = 1.0f;
1489 }
1490 else if (r_normalMapping->integer)
1491 GL_BindToTMU( tr.whiteImage, TB_NORMALMAP );
1492
1493 if (pStage->bundle[TB_DELUXEMAP].image[0])
1494 {
1495 R_BindAnimatedImageToTMU( &pStage->bundle[TB_DELUXEMAP], TB_DELUXEMAP);
1496 enableTextures[1] = 1.0f;
1497 }
1498 else if (r_deluxeMapping->integer)
1499 GL_BindToTMU( tr.whiteImage, TB_DELUXEMAP );
1500
1501 if (pStage->bundle[TB_SPECULARMAP].image[0])
1502 {
1503 R_BindAnimatedImageToTMU( &pStage->bundle[TB_SPECULARMAP], TB_SPECULARMAP);
1504 enableTextures[2] = 1.0f;
1505 }
1506 else if (r_specularMapping->integer)
1507 GL_BindToTMU( tr.whiteImage, TB_SPECULARMAP );
1508 }
1509
1510 enableTextures[3] = (r_cubeMapping->integer && !(tr.viewParms.flags & VPF_NOCUBEMAPS) && input->cubemapIndex) ? 1.0f : 0.0f;
1511 }
1512
1513 GLSL_SetUniformVec4(sp, UNIFORM_ENABLETEXTURES, enableTextures);
1514 }
1515 else if ( pStage->bundle[1].image[0] != 0 )
1516 {
1517 R_BindAnimatedImageToTMU( &pStage->bundle[0], 0 );
1518 R_BindAnimatedImageToTMU( &pStage->bundle[1], 1 );
1519 }
1520 else
1521 {
1522 //
1523 // set state
1524 //
1525 R_BindAnimatedImageToTMU( &pStage->bundle[0], 0 );
1526 }
1527
1528 //
1529 // testing cube map
1530 //
1531 if (!(tr.viewParms.flags & VPF_NOCUBEMAPS) && input->cubemapIndex && r_cubeMapping->integer)
1532 {
1533 vec4_t vec;
1534 cubemap_t *cubemap = &tr.cubemaps[input->cubemapIndex - 1];
1535
1536 // FIXME: cubemap image could be NULL if cubemap isn't renderer or loaded
1537 if (cubemap->image)
1538 GL_BindToTMU( cubemap->image, TB_CUBEMAP);
1539
1540 VectorSubtract(cubemap->origin, backEnd.viewParms.or.origin, vec);
1541 vec[3] = 1.0f;
1542
1543 VectorScale4(vec, 1.0f / cubemap->parallaxRadius, vec);
1544
1545 GLSL_SetUniformVec4(sp, UNIFORM_CUBEMAPINFO, vec);
1546 }
1547
1548 //
1549 // draw
1550 //
1551 R_DrawElements(input->numIndexes, input->firstIndex);
1552
1553 // allow skipping out to show just lightmaps during development
1554 if ( r_lightmap->integer && ( pStage->bundle[0].isLightmap || pStage->bundle[1].isLightmap ) )
1555 {
1556 break;
1557 }
1558
1559 if (backEnd.depthFill)
1560 break;
1561 }
1562 }
1563
1564
1565 static void RB_RenderShadowmap( shaderCommands_t *input )
1566 {
1567 int deformGen;
1568 vec5_t deformParams;
1569
1570 ComputeDeformValues(&deformGen, deformParams);
1571
1572 {
1573 shaderProgram_t *sp = &tr.shadowmapShader[0];
1574
1575 if (glState.vertexAnimation)
1576 {
1577 sp = &tr.shadowmapShader[SHADOWMAPDEF_USE_VERTEX_ANIMATION];
1578 }
1579 else if (glState.boneAnimation)
1580 {
1581 sp = &tr.shadowmapShader[SHADOWMAPDEF_USE_BONE_ANIMATION];
1582 }
1583
1584 vec4_t vector;
1585
1586 GLSL_BindProgram(sp);
1587
1588 GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
1589
1590 GLSL_SetUniformMat4(sp, UNIFORM_MODELMATRIX, backEnd.or.transformMatrix);
1591
1592 GLSL_SetUniformFloat(sp, UNIFORM_VERTEXLERP, glState.vertexAttribsInterpolation);
1593
1594 if (glState.boneAnimation)
1595 {
1596 GLSL_SetUniformMat4BoneMatrix(sp, UNIFORM_BONEMATRIX, glState.boneMatrix, glState.boneAnimation);
1597 }
1598
1599 GLSL_SetUniformInt(sp, UNIFORM_DEFORMGEN, deformGen);
1600 if (deformGen != DGEN_NONE)
1601 {
1602 GLSL_SetUniformFloat5(sp, UNIFORM_DEFORMPARAMS, deformParams);
1603 GLSL_SetUniformFloat(sp, UNIFORM_TIME, tess.shaderTime);
1604 }
1605
1606 VectorCopy(backEnd.viewParms.or.origin, vector);
1607 vector[3] = 1.0f;
1608 GLSL_SetUniformVec4(sp, UNIFORM_LIGHTORIGIN, vector);
1609 GLSL_SetUniformFloat(sp, UNIFORM_LIGHTRADIUS, backEnd.viewParms.zFar);
1610
1611 GL_State( 0 );
1612 GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 0);
1613
1614 //
1615 // do multitexture
1616 //
1617 //if ( pStage->glslShaderGroup )
1618 {
1619 //
1620 // draw
1621 //
1622
1623 R_DrawElements(input->numIndexes, input->firstIndex);
1624 }
1625 }
1626 }
1627
1628
1629
1630 /*
1631 ** RB_StageIteratorGeneric
1632 */
1633 void RB_StageIteratorGeneric( void )
1634 {
1635 shaderCommands_t *input;
1636 unsigned int vertexAttribs = 0;
1637
1638 input = &tess;
1639
1640 if (!input->numVertexes || !input->numIndexes)
1641 {
1642 return;
1643 }
1644
1645 if (tess.useInternalVao)
1646 {
1647 RB_DeformTessGeometry();
1648 }
1649
1650 vertexAttribs = RB_CalcShaderVertexAttribs( input );
1651
1652 if (tess.useInternalVao)
1653 {
1654 RB_UpdateTessVao(vertexAttribs);
1655 }
1656 else
1657 {
1658 backEnd.pc.c_staticVaoDraws++;
1659 }
1660
1661 //
1662 // log this call
1663 //
1664 if ( r_logFile->integer )
1665 {
1666 // don't just call LogComment, or we will get
1667 // a call to va() every frame!
1668 GLimp_LogComment( va("--- RB_StageIteratorGeneric( %s ) ---\n", tess.shader->name) );
1669 }
1670
1671 //
1672 // set face culling appropriately
1673 //
1674 if (input->shader->cullType == CT_TWO_SIDED)
1675 {
1676 GL_Cull( CT_TWO_SIDED );
1677 }
1678 else
1679 {
1680 qboolean cullFront = (input->shader->cullType == CT_FRONT_SIDED);
1681
1682 if ( backEnd.viewParms.flags & VPF_DEPTHSHADOW )
1683 cullFront = !cullFront;
1684
1685 if ( backEnd.viewParms.isMirror )
1686 cullFront = !cullFront;
1687
1688 if ( backEnd.currentEntity && backEnd.currentEntity->mirrored )
1689 cullFront = !cullFront;
1690
1691 if (cullFront)
1692 GL_Cull( CT_FRONT_SIDED );
1693 else
1694 GL_Cull( CT_BACK_SIDED );
1695 }
1696
1697 // set polygon offset if necessary
1698 if ( input->shader->polygonOffset )
1699 {
1700 qglEnable( GL_POLYGON_OFFSET_FILL );
1701 }
1702
1703 //
1704 // render depth if in depthfill mode
1705 //
1706 if (backEnd.depthFill)
1707 {
1708 RB_IterateStagesGeneric( input );
1709
1710 //
1711 // reset polygon offset
1712 //
1713 if ( input->shader->polygonOffset )
1714 {
1715 qglDisable( GL_POLYGON_OFFSET_FILL );
1716 }
1717
1718 return;
1719 }
1720
1721 //
1722 // render shadowmap if in shadowmap mode
1723 //
1724 if (backEnd.viewParms.flags & VPF_SHADOWMAP)
1725 {
1726 if ( input->shader->sort == SS_OPAQUE )
1727 {
1728 RB_RenderShadowmap( input );
1729 }
1730 //
1731 // reset polygon offset
1732 //
1733 if ( input->shader->polygonOffset )
1734 {
1735 qglDisable( GL_POLYGON_OFFSET_FILL );
1736 }
1737
1738 return;
1739 }
1740
1741 //
1742 //
1743 // call shader function
1744 //
1745 RB_IterateStagesGeneric( input );
1746
1747 //
1748 // pshadows!
1749 //
1750 if (glRefConfig.framebufferObject && r_shadows->integer == 4 && tess.pshadowBits
1751 && tess.shader->sort <= SS_OPAQUE && !(tess.shader->surfaceFlags & (SURF_NODLIGHT | SURF_SKY) ) ) {
1752 ProjectPshadowVBOGLSL();
1753 }
1754
1755
1756 //
1757 // now do any dynamic lighting needed
1758 //
1759 if ( tess.dlightBits && tess.shader->sort <= SS_OPAQUE && r_lightmap->integer == 0
1760 && !(tess.shader->surfaceFlags & (SURF_NODLIGHT | SURF_SKY) ) ) {
1761 if (tess.shader->numUnfoggedPasses == 1 && tess.xstages[0]->glslShaderGroup == tr.lightallShader
1762 && (tess.xstages[0]->glslShaderIndex & LIGHTDEF_LIGHTTYPE_MASK) && r_dlightMode->integer)
1763 {
1764 ForwardDlight();
1765 }
1766 else
1767 {
1768 ProjectDlightTexture();
1769 }
1770 }
1771
1772 //
1773 // now do fog
1774 //
1775 if ( tess.fogNum && tess.shader->fogPass ) {
1776 RB_FogPass();
1777 }
1778
1779 //
1780 // reset polygon offset
1781 //
1782 if ( input->shader->polygonOffset )
1783 {
1784 qglDisable( GL_POLYGON_OFFSET_FILL );
1785 }
1786 }
1787
1788 /*
1789 ** RB_EndSurface
1790 */
1791 void RB_EndSurface( void ) {
1792 shaderCommands_t *input;
1793
1794 input = &tess;
1795
1796 if (input->numIndexes == 0 || input->numVertexes == 0) {
1797 return;
1798 }
1799
1800 if (input->indexes[SHADER_MAX_INDEXES-1] != 0) {
1801 ri.Error (ERR_DROP, "RB_EndSurface() - SHADER_MAX_INDEXES hit");
1802 }
1803 if (input->xyz[SHADER_MAX_VERTEXES-1][0] != 0) {
1804 ri.Error (ERR_DROP, "RB_EndSurface() - SHADER_MAX_VERTEXES hit");
1805 }
1806
1807 if ( tess.shader == tr.shadowShader ) {
1808 RB_ShadowTessEnd();
1809 return;
1810 }
1811
1812 // for debugging of sort order issues, stop rendering after a given sort value
1813 if ( r_debugSort->integer && r_debugSort->integer < tess.shader->sort ) {
1814 return;
1815 }
1816
1817 if (tess.useCacheVao)
1818 {
1819 // upload indexes now
1820 VaoCache_Commit();
1821 }
1822
1823 //
1824 // update performance counters
1825 //
1826 backEnd.pc.c_shaders++;
1827 backEnd.pc.c_vertexes += tess.numVertexes;
1828 backEnd.pc.c_indexes += tess.numIndexes;
1829 backEnd.pc.c_totalIndexes += tess.numIndexes * tess.numPasses;
1830
1831 //
1832 // call off to shader specific tess end function
1833 //
1834 tess.currentStageIteratorFunc();
1835
1836 //
1837 // draw debugging stuff
1838 //
1839 if ( r_showtris->integer ) {
1840 DrawTris (input);
1841 }
1842 if ( r_shownormals->integer ) {
1843 DrawNormals (input);
1844 }
1845 // clear shader so we can tell we don't have any unclosed surfaces
1846 tess.numIndexes = 0;
1847 tess.numVertexes = 0;
1848 tess.firstIndex = 0;
1849 tess.useCacheVao = qfalse;
1850 tess.useInternalVao = qfalse;
1851
1852 GLimp_LogComment( "----------\n" );
1853 }