/
tr_shade.cpp
3050 lines (2400 loc) · 83.4 KB
/
tr_shade.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2006-2011 Robert Beckebans <trebor_7@users.sourceforge.net>
This file is part of Daemon source code.
Daemon source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Daemon source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Daemon source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// tr_shade.c
#include "tr_local.h"
#include "gl_shader.h"
/*
=================================================================================
THIS ENTIRE FILE IS BACK END!
This file deals with applying shaders to surface data in the tess struct.
=================================================================================
*/
static void GLSL_InitGPUShadersOrError()
{
// make sure the render thread is stopped
R_SyncRenderThread();
GL_CheckErrors();
// single texture rendering
gl_shaderManager.GenerateBuiltinHeaders();
// single texture rendering
gl_shaderManager.load( gl_genericShader );
// standard light mapping
gl_shaderManager.load( gl_lightMappingShader );
// omni-directional specular bump mapping ( Doom3 style )
gl_shaderManager.load( gl_forwardLightingShader_omniXYZ );
// projective lighting ( Doom3 style )
gl_shaderManager.load( gl_forwardLightingShader_projXYZ );
// directional sun lighting ( Doom3 style )
gl_shaderManager.load( gl_forwardLightingShader_directionalSun );
#if !defined( GLSL_COMPILE_STARTUP_ONLY )
gl_shaderManager.load( gl_depthToColorShader );
#endif // #if !defined(GLSL_COMPILE_STARTUP_ONLY)
// shadowmap distance compression
gl_shaderManager.load( gl_shadowFillShader );
// bumped cubemap reflection for abitrary polygons ( EMBM )
gl_shaderManager.load( gl_reflectionShader );
// skybox drawing for abitrary polygons
gl_shaderManager.load( gl_skyboxShader );
// Q3A volumetric fog
gl_shaderManager.load( gl_fogQuake3Shader );
// global fog post process effect
gl_shaderManager.load( gl_fogGlobalShader );
// heatHaze post process effect
gl_shaderManager.load( gl_heatHazeShader );
// screen post process effect
gl_shaderManager.load( gl_screenShader );
// portal process effect
gl_shaderManager.load( gl_portalShader );
// LDR bright pass filter
gl_shaderManager.load( gl_contrastShader );
// camera post process effect
gl_shaderManager.load( gl_cameraEffectsShader );
// gaussian blur
gl_shaderManager.load( gl_blurXShader );
gl_shaderManager.load( gl_blurYShader );
// debug utils
gl_shaderManager.load( gl_debugShadowMapShader );
gl_shaderManager.load( gl_liquidShader );
#if !defined( GLSL_COMPILE_STARTUP_ONLY )
gl_shaderManager.load( gl_volumetricFogShader );
#endif // #if !defined(GLSL_COMPILE_STARTUP_ONLY)
gl_shaderManager.load( gl_motionblurShader );
if (GLEW_ARB_texture_gather)
{
if (r_ssao->integer)
{
gl_shaderManager.load(gl_ssaoShader);
}
}
else
{
Log::Warn("SSAO not used because GL_ARB_texture_gather is not available.");
}
gl_shaderManager.load( gl_depthtile1Shader );
gl_shaderManager.load( gl_depthtile2Shader );
gl_shaderManager.load( gl_lighttileShader );
gl_shaderManager.load( gl_fxaaShader );
if ( !r_lazyShaders->integer )
{
gl_shaderManager.buildAll();
}
}
void GLSL_InitGPUShaders()
{
/*
Without a shaderpath option, the shader debugging cycle is like this:
1. Change shader file(s).
2. Run script to convert shader files into c++, storing them in shaders.cpp
3. Recompile app to pickup the new shaders.cpp changes.
4. Run the app and get to the point required to check work.
5. If the change failed or succeeded but you want to make more changes restart at step 1.
Alternatively, if set shaderpath "c:/unvanquished/main" is used, the cycle is:
1. Change shader file(s) - don't run the buildshaders script unless samples.cpp is missing.
2. Start the app, the app will load the shader files directly.
If there is a problem the app will revert to the last working changes
in samples.cpp, so need to restart the app.
3. Fix the problem shader files
4. Do /glsl_restart at the app console to reload them. Repeat from step 3 as needed.
Note that unv will respond by listing the files it thinks are different.
If this matches your expectations then it's not an error.
Note foward slashes (like those used in windows pathnames are processed
as escape characters by the Unvanquished command processor,
so use two forward slashes in that case.
*/
auto shaderPath = GetShaderPath();
if (shaderPath.empty())
shaderKind = ShaderKind::BuiltIn;
else
shaderKind = ShaderKind::External;
bool externalFailed = false;
if (shaderKind == ShaderKind::External)
{
try
{
Log::Warn("Loading external shaders.");
GLSL_InitGPUShadersOrError();
Log::Warn("External shaders in use.");
}
catch (const ShaderException& e)
{
Log::Warn("External shaders failed: %s", e.what());
Log::Warn("Attempting to use built in shaders instead.");
shaderKind = ShaderKind::BuiltIn;
externalFailed = true;
}
}
if (shaderKind == ShaderKind::BuiltIn)
{
// Let the user know if we are transitioning from external to
// built-in shaders. We won't alert them if we were already using
// built-in shaders as this is the normal case.
try
{
GLSL_InitGPUShadersOrError();
}
catch (const ShaderException&e)
{
Sys::Error("Built-in shaders failed: %s", e.what()); // Fatal.
};
if (externalFailed)
Log::Warn("Now using built-in shaders because external shaders failed.");
}
}
void GLSL_ShutdownGPUShaders()
{
R_SyncRenderThread();
gl_shaderManager.freeAll();
gl_genericShader = nullptr;
gl_lightMappingShader = nullptr;
gl_forwardLightingShader_omniXYZ = nullptr;
gl_forwardLightingShader_projXYZ = nullptr;
gl_forwardLightingShader_directionalSun = nullptr;
gl_depthToColorShader = nullptr;
gl_shadowFillShader = nullptr;
gl_lightVolumeShader_omni = nullptr;
gl_reflectionShader = nullptr;
gl_skyboxShader = nullptr;
gl_fogQuake3Shader = nullptr;
gl_fogGlobalShader = nullptr;
gl_heatHazeShader = nullptr;
gl_screenShader = nullptr;
gl_portalShader = nullptr;
gl_contrastShader = nullptr;
gl_cameraEffectsShader = nullptr;
gl_blurXShader = nullptr;
gl_blurYShader = nullptr;
gl_debugShadowMapShader = nullptr;
gl_liquidShader = nullptr;
gl_volumetricFogShader = nullptr;
gl_motionblurShader = nullptr;
gl_ssaoShader = nullptr;
gl_depthtile1Shader = nullptr;
gl_depthtile2Shader = nullptr;
gl_lighttileShader = nullptr;
gl_fxaaShader = nullptr;
GL_BindNullProgram();
}
void GLSL_FinishGPUShaders()
{
R_SyncRenderThread();
gl_shaderManager.buildAll();
}
/*
==================
Tess_DrawElements
==================
*/
void Tess_DrawElements()
{
int i;
if ( ( tess.numIndexes == 0 || tess.numVertexes == 0 ) && tess.multiDrawPrimitives == 0 )
{
return;
}
// move tess data through the GPU, finally
if ( glState.currentVBO && glState.currentIBO )
{
if ( tess.multiDrawPrimitives )
{
glMultiDrawElements( GL_TRIANGLES, tess.multiDrawCounts, GL_INDEX_TYPE, ( const GLvoid ** ) tess.multiDrawIndexes, tess.multiDrawPrimitives );
backEnd.pc.c_multiDrawElements++;
backEnd.pc.c_multiDrawPrimitives += tess.multiDrawPrimitives;
backEnd.pc.c_vboVertexes += tess.numVertexes;
for ( i = 0; i < tess.multiDrawPrimitives; i++ )
{
backEnd.pc.c_multiVboIndexes += tess.multiDrawCounts[ i ];
backEnd.pc.c_indexes += tess.multiDrawCounts[ i ];
}
}
else
{
uintptr_t base = 0;
if( glState.currentIBO == tess.ibo ) {
base = tess.indexBase * sizeof( glIndex_t );
}
glDrawRangeElements( GL_TRIANGLES, 0, tess.numVertexes, tess.numIndexes, GL_INDEX_TYPE, BUFFER_OFFSET( base ) );
backEnd.pc.c_drawElements++;
backEnd.pc.c_vboVertexes += tess.numVertexes;
backEnd.pc.c_vboIndexes += tess.numIndexes;
backEnd.pc.c_indexes += tess.numIndexes;
backEnd.pc.c_vertexes += tess.numVertexes;
}
}
else
{
glDrawElements( GL_TRIANGLES, tess.numIndexes, GL_INDEX_TYPE, tess.indexes );
backEnd.pc.c_drawElements++;
backEnd.pc.c_indexes += tess.numIndexes;
backEnd.pc.c_vertexes += tess.numVertexes;
}
}
/*
==================
Tess_DrawArrays
==================
*/
void Tess_DrawArrays( GLenum elementType )
{
if ( tess.numVertexes == 0 )
{
return;
}
// move tess data through the GPU, finally
glDrawArrays( elementType, 0, tess.numVertexes );
backEnd.pc.c_drawElements++;
backEnd.pc.c_indexes += tess.numIndexes;
backEnd.pc.c_vertexes += tess.numVertexes;
if ( glState.currentVBO )
{
backEnd.pc.c_vboVertexes += tess.numVertexes;
backEnd.pc.c_vboIndexes += tess.numIndexes;
}
}
/*
=============================================================
SURFACE SHADERS
=============================================================
*/
ALIGNED( 16, shaderCommands_t tess );
/*
================
DrawTris
Draws triangle outlines for debugging
================
*/
static void DrawTris()
{
int deform = 0;
GLimp_LogComment( "--- DrawTris ---\n" );
tess.vboVertexSprite = tess.surfaceShader->autoSpriteMode != 0;
gl_genericShader->SetVertexSkinning( glConfig2.vboVertexSkinningAvailable && tess.vboVertexSkinning );
gl_genericShader->SetVertexAnimation( tess.vboVertexAnimation );
gl_genericShader->SetVertexSprite( tess.vboVertexSprite );
gl_genericShader->SetTCGenEnvironment( false );
gl_genericShader->SetTCGenLightmap( false );
gl_genericShader->SetDepthFade( false );
gl_genericShader->SetAlphaTesting( false );
if( tess.surfaceShader->stages[0] ) {
deform = tess.surfaceShader->stages[0]->deformIndex;
}
gl_genericShader->BindProgram( deform );
GL_State( GLS_POLYMODE_LINE | GLS_DEPTHMASK_TRUE );
//gl_genericShader->SetUniform_AlphaTest( GLS_ATEST_NONE );
if ( r_showBatches->integer || r_showLightBatches->integer )
{
gl_genericShader->SetUniform_Color( Color::Color::Indexed( backEnd.pc.c_batches % 8 ) );
}
else if ( glState.currentVBO == tess.vbo )
{
gl_genericShader->SetUniform_Color( Color::Red );
}
else if ( glState.currentVBO )
{
gl_genericShader->SetUniform_Color( Color::Blue );
}
else
{
gl_genericShader->SetUniform_Color( Color::White );
}
gl_genericShader->SetUniform_ColorModulate( colorGen_t::CGEN_CONST, alphaGen_t::AGEN_CONST );
gl_genericShader->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
gl_genericShader->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[ glState.stackIndex ] );
if ( glConfig2.vboVertexSkinningAvailable && tess.vboVertexSkinning )
{
gl_genericShader->SetUniform_Bones( tess.numBones, tess.bones );
}
// u_DeformGen
gl_genericShader->SetUniform_Time( backEnd.refdef.floatTime - backEnd.currentEntity->e.shaderTime );
// bind u_ColorMap
GL_BindToTMU( 0, tr.whiteImage );
gl_genericShader->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_COLORMAP ] );
gl_genericShader->SetRequiredVertexPointers();
glDepthRange( 0, 0 );
Tess_DrawElements();
glDepthRange( 0, 1 );
}
/*
==============
Tess_Begin
We must set some things up before beginning any tesselation,
because a surface may be forced to perform a Tess_End due
to overflow.
==============
*/
// *INDENT-OFF*
void Tess_Begin( void ( *stageIteratorFunc )(),
void ( *stageIteratorFunc2 )(),
shader_t *surfaceShader, shader_t *lightShader,
bool skipTangentSpaces,
bool skipVBO,
int lightmapNum,
int fogNum,
bool bspSurface )
{
shader_t *state;
tess.numIndexes = 0;
tess.numVertexes = 0;
tess.attribsSet = 0;
tess.multiDrawPrimitives = 0;
// materials are optional
if ( surfaceShader != nullptr )
{
state = ( surfaceShader->remappedShader ) ? surfaceShader->remappedShader : surfaceShader;
tess.surfaceShader = state;
tess.surfaceStages = state->stages;
tess.numSurfaceStages = state->numStages;
Tess_MapVBOs( false );
}
else
{
state = nullptr;
tess.numSurfaceStages = 0;
tess.surfaceShader = nullptr;
tess.surfaceStages = nullptr;
Tess_MapVBOs( false );
}
bool isSky = ( state != nullptr && state->isSky != false );
tess.lightShader = lightShader;
tess.stageIteratorFunc = stageIteratorFunc;
tess.stageIteratorFunc2 = stageIteratorFunc2;
if ( !tess.stageIteratorFunc )
{
Sys::Error( "tess.stageIteratorFunc == NULL" );
}
if ( tess.stageIteratorFunc == &Tess_StageIteratorGeneric )
{
if ( isSky )
{
tess.stageIteratorFunc = &Tess_StageIteratorSky;
tess.stageIteratorFunc2 = &Tess_StageIteratorGeneric;
}
}
else if ( tess.stageIteratorFunc == &Tess_StageIteratorDepthFill )
{
if ( isSky )
{
tess.stageIteratorFunc = &Tess_StageIteratorSky;
tess.stageIteratorFunc2 = &Tess_StageIteratorDepthFill;
}
}
tess.skipTangentSpaces = skipTangentSpaces;
tess.skipVBO = skipVBO;
tess.lightmapNum = lightmapNum;
tess.fogNum = fogNum;
tess.bspSurface = bspSurface;
if ( r_logFile->integer )
{
// don't just call LogComment, or we will get
// a call to va() every frame!
GLimp_LogComment( va( "--- Tess_Begin( surfaceShader = %s, lightShader = %s, skipTangentSpaces = %i, lightmapNum = %i, fogNum = %i) ---\n", tess.surfaceShader->name, tess.lightShader ? tess.lightShader->name : nullptr, tess.skipTangentSpaces, tess.lightmapNum, tess.fogNum ) );
}
}
void SetNormalScale( shaderStage_t *pStage, vec3_t normalScale )
{
float normalIntensity = RB_EvalExpression( &pStage->normalIntensityExp, 1.0 );
for ( int i = 0; i < 3; i++ )
{
normalScale[ i ] = pStage->normalScale[ i ];
// Normal intensity is only applied on X and Y.
// This behaviour is inherited.
if ( i < 2 )
{
normalScale[ i ] *= normalIntensity;
}
}
/* Note: the GLSL code disables normal map scaling when normal Z scale is
equal to zero.
It means normal map scaling is disabled when r_normalScale is set to zero.
This is cool enough to be kept as a feature.
Normal Z component equal to zero would be wrong anyway.
r_normalScale is only applied on Z.
This behaviour is inherited.
*/
normalScale[ 2 ] *= r_normalScale->value;
}
// *INDENT-ON*
static void Render_generic( int stage )
{
shaderStage_t *pStage;
colorGen_t rgbGen;
alphaGen_t alphaGen;
bool needDepthMap = false;
bool hasDepthFade = false;
GLimp_LogComment( "--- Render_generic ---\n" );
pStage = tess.surfaceStages[ stage ];
GL_State( pStage->stateBits );
hasDepthFade = pStage->hasDepthFade && !tess.surfaceShader->autoSpriteMode;
needDepthMap = pStage->hasDepthFade || tess.surfaceShader->autoSpriteMode;
tess.vboVertexSprite = tess.surfaceShader->autoSpriteMode != 0;
uint32_t alphaTestBits = pStage->stateBits & GLS_ATEST_BITS;
// choose right shader program ----------------------------------
gl_genericShader->SetVertexSkinning( glConfig2.vboVertexSkinningAvailable && tess.vboVertexSkinning );
gl_genericShader->SetVertexAnimation( tess.vboVertexAnimation );
gl_genericShader->SetTCGenEnvironment( pStage->tcGen_Environment );
gl_genericShader->SetTCGenLightmap( pStage->tcGen_Lightmap );
gl_genericShader->SetDepthFade( hasDepthFade );
gl_genericShader->SetVertexSprite( tess.vboVertexSprite );
gl_genericShader->SetAlphaTesting(alphaTestBits != 0);
gl_genericShader->BindProgram( pStage->deformIndex );
// end choose right shader program ------------------------------
// set uniforms
if ( pStage->tcGen_Environment || tess.vboVertexSprite )
{
// calculate the environment texcoords in object space
gl_genericShader->SetUniform_ViewOrigin( backEnd.orientation.viewOrigin );
gl_genericShader->SetUniform_ViewUp( backEnd.orientation.axis[ 2 ] );
}
// u_AlphaTest
if (alphaTestBits != 0)
{
gl_genericShader->SetUniform_AlphaTest(pStage->stateBits);
}
// u_ColorGen
switch ( pStage->rgbGen )
{
case colorGen_t::CGEN_VERTEX:
case colorGen_t::CGEN_ONE_MINUS_VERTEX:
rgbGen = pStage->rgbGen;
break;
default:
rgbGen = colorGen_t::CGEN_CONST;
break;
}
// u_AlphaGen
switch ( pStage->alphaGen )
{
case alphaGen_t::AGEN_VERTEX:
case alphaGen_t::AGEN_ONE_MINUS_VERTEX:
alphaGen = pStage->alphaGen;
break;
default:
alphaGen = alphaGen_t::AGEN_CONST;
break;
}
// u_ColorModulate
gl_genericShader->SetUniform_ColorModulate( rgbGen, alphaGen );
// u_Color
gl_genericShader->SetUniform_Color( tess.svars.color );
gl_genericShader->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
gl_genericShader->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[ glState.stackIndex ] );
// u_Bones
if ( glConfig2.vboVertexSkinningAvailable && tess.vboVertexSkinning )
{
gl_genericShader->SetUniform_Bones( tess.numBones, tess.bones );
}
// u_VertexInterpolation
if ( tess.vboVertexAnimation )
{
gl_genericShader->SetUniform_VertexInterpolation( glState.vertexAttribsInterpolation );
}
// u_DeformGen
gl_genericShader->SetUniform_Time( backEnd.refdef.floatTime - backEnd.currentEntity->e.shaderTime );
// bind u_ColorMap
GL_SelectTexture( 0 );
BindAnimatedImage( &pStage->bundle[ TB_COLORMAP ] );
gl_genericShader->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_COLORMAP ] );
if ( hasDepthFade )
{
gl_genericShader->SetUniform_DepthScale( pStage->depthFadeValue );
}
if ( needDepthMap )
{
GL_BindToTMU( 1, tr.currentDepthImage );
}
GL_BindToTMU( 8, tr.lighttileRenderImage );
gl_genericShader->SetRequiredVertexPointers();
Tess_DrawElements();
GL_CheckErrors();
}
/*
=================
GetLightMap
=================
*/
static image_t* GetLightMap()
{
if ( !tr.lightmaps.currentElements )
{
return tr.whiteImage;
}
else if ( tr.fatLightmap )
{
return tr.fatLightmap;
}
else if ( tess.lightmapNum >= 0 && tess.lightmapNum < tr.lightmaps.currentElements )
{
return ( image_t * ) Com_GrowListElement( &tr.lightmaps, tess.lightmapNum );
}
else
{
return tr.whiteImage;
}
}
/*
=================
GetDeluxeMap
=================
*/
static image_t* GetDeluxeMap()
{
if ( !tr.deluxemaps.currentElements )
{
return tr.blackImage;
}
else if ( tess.lightmapNum >= 0 && tess.lightmapNum < tr.deluxemaps.currentElements )
{
return ( image_t * ) Com_GrowListElement( &tr.deluxemaps, tess.lightmapNum );
}
else
{
return tr.blackImage;
}
}
static void Render_lightMapping( int stage )
{
GLimp_LogComment( "--- Render_lightMapping ---\n" );
shaderStage_t *pStage = tess.surfaceStages[ stage ];
bool enableLightMapping = !r_vertexLighting->integer \
&& tess.bspSurface \
&& tess.lightmapNum >= 0 && tess.lightmapNum <= tr.lightmaps.currentElements;
bool enableDeluxeMapping = pStage->enableDeluxeMapping \
&& tess.bspSurface \
&& tr.worldDeluxeMapping;
bool noLightMap = !pStage->implicitLightmap
&& (tess.surfaceShader->surfaceFlags & SURF_NOLIGHTMAP)
&& !(tess.numSurfaceStages > 0 && tess.surfaceStages[0]->rgbGen == colorGen_t::CGEN_VERTEX);
uint32_t stateBits = pStage->stateBits;
if ( enableLightMapping && r_showLightMaps->integer )
{
stateBits &= ~( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS | GLS_ATEST_BITS );
}
GL_State( stateBits );
// u_ColorModulate
colorGen_t colorGen;
alphaGen_t alphaGen;
switch ( pStage->rgbGen )
{
case colorGen_t::CGEN_VERTEX:
case colorGen_t::CGEN_ONE_MINUS_VERTEX:
colorGen = pStage->rgbGen;
break;
default:
colorGen = colorGen_t::CGEN_CONST;
break;
}
switch ( pStage->alphaGen )
{
case alphaGen_t::AGEN_VERTEX:
case alphaGen_t::AGEN_ONE_MINUS_VERTEX:
alphaGen = pStage->alphaGen;
break;
default:
alphaGen = alphaGen_t::AGEN_CONST;
break;
}
// u_LightMap, u_DeluxeMap
image_t *lightmap;
image_t *deluxemap;
if ( noLightMap )
{
lightmap = tr.whiteImage;
enableLightMapping = true;
}
else if ( enableLightMapping )
{
lightmap = GetLightMap();
}
else if ( tr.lightGrid1Image )
{
// Store lightGrid1 as lightmap,
// the GLSL code will know to deal with it.
lightmap = tr.lightGrid1Image;
}
else
{
lightmap = tr.whiteImage;
enableLightMapping = true;
}
if ( enableDeluxeMapping )
{
deluxemap = GetDeluxeMap();
}
else if ( tr.lightGrid2Image )
{
// Store lightGrid2 as deluxemap,
// the GLSL code will know to deal with it.
deluxemap = tr.lightGrid2Image;
}
else
{
deluxemap = tr.blackImage;
enableDeluxeMapping = true;
}
// choose right shader program ----------------------------------
tess.vboVertexSprite = false;
gl_lightMappingShader->SetVertexSkinning( glConfig2.vboVertexSkinningAvailable && tess.vboVertexSkinning );
gl_lightMappingShader->SetVertexAnimation( tess.vboVertexAnimation );
gl_lightMappingShader->SetBspSurface( tess.bspSurface );
gl_lightMappingShader->SetLightMapping( enableLightMapping );
gl_lightMappingShader->SetDeluxeMapping( enableDeluxeMapping );
gl_lightMappingShader->SetHeightMapInNormalMap( pStage->isHeightMapInNormalMap );
gl_lightMappingShader->SetReliefMapping( pStage->enableReliefMapping );
gl_lightMappingShader->SetReflectiveSpecular( pStage->enableNormalMapping && tr.cubeHashTable != nullptr );
gl_lightMappingShader->SetPhysicalShading( pStage->isMaterialPhysical );
gl_lightMappingShader->BindProgram( pStage->deformIndex );
// end choose right shader program ------------------------------
// now we are ready to set the shader program uniforms
vec3_t viewOrigin;
if ( tess.bspSurface )
{
VectorCopy( backEnd.orientation.viewOrigin, viewOrigin ); // in world space
}
else
{
VectorCopy( backEnd.viewParms.orientation.origin, viewOrigin ); // in world space
if ( glConfig2.vboVertexSkinningAvailable && tess.vboVertexSkinning )
{
gl_lightMappingShader->SetUniform_Bones( tess.numBones, tess.bones );
}
// u_VertexInterpolation
if ( tess.vboVertexAnimation )
{
gl_lightMappingShader->SetUniform_VertexInterpolation( glState.vertexAttribsInterpolation );
}
gl_lightMappingShader->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
}
// u_ViewOrigin
gl_lightMappingShader->SetUniform_ViewOrigin( viewOrigin );
gl_lightMappingShader->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[ glState.stackIndex ] );
if( backEnd.refdef.numShaderLights > 0 ) {
gl_lightMappingShader->SetUniform_numLights( backEnd.refdef.numLights );
if( glConfig2.uniformBufferObjectAvailable ) {
gl_lightMappingShader->SetUniformBlock_Lights( tr.dlightUBO );
} else {
GL_BindToTMU( BIND_LIGHTS, tr.dlightImage );
}
}
// bind u_LightTiles
GL_BindToTMU( BIND_LIGHTTILES, tr.lighttileRenderImage );
// u_DeformGen
gl_lightMappingShader->SetUniform_Time( backEnd.refdef.floatTime - backEnd.currentEntity->e.shaderTime );
// u_ColorModulate
gl_lightMappingShader->SetUniform_ColorModulate( colorGen, alphaGen );
// u_Color
gl_lightMappingShader->SetUniform_Color( tess.svars.color );
if ( tess.bspSurface && !enableLightMapping )
{
gl_lightMappingShader->SetUniform_LightWrapAround( RB_EvalExpression( &pStage->wrapAroundLightingExp, 0 ) );
}
// u_AlphaTest
gl_lightMappingShader->SetUniform_AlphaTest( pStage->stateBits );
// bind u_HeightMap
if ( pStage->enableReliefMapping )
{
float depthScale;
float reliefDepthScale;
depthScale = RB_EvalExpression( &pStage->depthScaleExp, r_reliefDepthScale->value );
reliefDepthScale = tess.surfaceShader->reliefDepthScale;
depthScale *= reliefDepthScale == 0 ? 1 : reliefDepthScale;
gl_lightMappingShader->SetUniform_ReliefDepthScale( depthScale );
gl_lightMappingShader->SetUniform_ReliefOffsetBias( tess.surfaceShader->reliefOffsetBias );
// FIXME: if there is both, embedded heightmap in normalmap is used instead of standalone heightmap
if ( !pStage->isHeightMapInNormalMap )
{
GL_BindToTMU( BIND_HEIGHTMAP, pStage->bundle[ TB_HEIGHTMAP ].image[ 0 ] );
}
}
// bind u_DiffuseMap
if ( pStage->type == stageType_t::ST_LIGHTMAP )
{
// standalone lightmap stage: paint shadows over a white texture
GL_BindToTMU( BIND_DIFFUSEMAP, tr.whiteImage );
}
else
{
GL_BindToTMU( BIND_DIFFUSEMAP, pStage->bundle[ TB_DIFFUSEMAP ].image[ 0 ] );
gl_lightMappingShader->SetUniform_TextureMatrix( tess.svars.texMatrices[ TB_DIFFUSEMAP ] );
}
// bind u_NormalMap
GL_BindToTMU( BIND_NORMALMAP, pStage->bundle[ TB_NORMALMAP ].image[ 0 ] );
// bind u_NormalScale
if ( pStage->enableNormalMapping )
{
vec3_t normalScale;
SetNormalScale( pStage, normalScale );
gl_lightMappingShader->SetUniform_NormalScale( normalScale );
}
// bind u_MaterialMap
GL_BindToTMU( BIND_MATERIALMAP, pStage->bundle[ TB_MATERIALMAP ].image[ 0 ] );
if ( pStage->enableSpecularMapping )
{
float specExpMin = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value );
float specExpMax = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value );
gl_lightMappingShader->SetUniform_SpecularExponent( specExpMin, specExpMax );
}
// specular reflection
if ( tr.cubeHashTable != nullptr )
{
cubemapProbe_t *cubeProbeNearest;
cubemapProbe_t *cubeProbeSecondNearest;
image_t *cubeMap0 = nullptr;
image_t *cubeMap1 = nullptr;
float interpolation = 0.0;
bool isWorldEntity = backEnd.currentEntity == &tr.worldEntity;
if ( backEnd.currentEntity && !isWorldEntity )
{
R_FindTwoNearestCubeMaps( backEnd.currentEntity->e.origin, &cubeProbeNearest, &cubeProbeSecondNearest );
}
else
{
// FIXME position
R_FindTwoNearestCubeMaps( backEnd.viewParms.orientation.origin, &cubeProbeNearest, &cubeProbeSecondNearest );
}
if ( cubeProbeNearest == nullptr && cubeProbeSecondNearest == nullptr )
{
GLimp_LogComment( "cubeProbeNearest && cubeProbeSecondNearest == NULL\n" );
cubeMap0 = tr.whiteCubeImage;
cubeMap1 = tr.whiteCubeImage;
}
else if ( cubeProbeNearest == nullptr )
{
GLimp_LogComment( "cubeProbeNearest == NULL\n" );
cubeMap0 = cubeProbeSecondNearest->cubemap;
}
else if ( cubeProbeSecondNearest == nullptr )
{
GLimp_LogComment( "cubeProbeSecondNearest == NULL\n" );
cubeMap0 = cubeProbeNearest->cubemap;
}
else
{
float cubeProbeNearestDistance, cubeProbeSecondNearestDistance;
if ( backEnd.currentEntity && !isWorldEntity )
{
cubeProbeNearestDistance = Distance( backEnd.currentEntity->e.origin, cubeProbeNearest->origin );
cubeProbeSecondNearestDistance = Distance( backEnd.currentEntity->e.origin, cubeProbeSecondNearest->origin );
}
else
{
// FIXME position
cubeProbeNearestDistance = Distance( backEnd.viewParms.orientation.origin, cubeProbeNearest->origin );
cubeProbeSecondNearestDistance = Distance( backEnd.viewParms.orientation.origin, cubeProbeSecondNearest->origin );