-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathMaterial.cpp
More file actions
2319 lines (1825 loc) · 83.7 KB
/
Material.cpp
File metadata and controls
2319 lines (1825 loc) · 83.7 KB
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
/*
===========================================================================
Daemon BSD Source Code
Copyright (c) 2024 Daemon Developers
All rights reserved.
This file is part of the Daemon BSD Source Code (Daemon Source Code).
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Daemon developers nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
===========================================================================
*/
// Material.cpp
#include "tr_local.h"
#include "Material.h"
#include "ShadeCommon.h"
#include "GeometryCache.h"
GLUBO materialsUBO( "materials", Util::ordinal( BufferBind::MATERIALS ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
GLBuffer texDataBuffer( "texData", Util::ordinal( BufferBind::TEX_DATA ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLUBO lightMapDataUBO( "lightMapData", Util::ordinal( BufferBind::LIGHTMAP_DATA ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLSSBO surfaceDescriptorsSSBO( "surfaceDescriptors", Util::ordinal( BufferBind::SURFACE_DESCRIPTORS ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
GLSSBO surfaceCommandsSSBO( "surfaceCommands", Util::ordinal( BufferBind::SURFACE_COMMANDS ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLBuffer culledCommandsBuffer( "culledCommands", Util::ordinal( BufferBind::CULLED_COMMANDS ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLUBO surfaceBatchesUBO( "surfaceBatches", Util::ordinal( BufferBind::SURFACE_BATCHES ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
GLBuffer atomicCommandCountersBuffer( "atomicCommandCounters", Util::ordinal( BufferBind::COMMAND_COUNTERS_ATOMIC ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLSSBO portalSurfacesSSBO( "portalSurfaces", Util::ordinal( BufferBind::PORTAL_SURFACES ), GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT, 0 );
GLSSBO debugSSBO( "debug", Util::ordinal( BufferBind::DEBUG ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
PortalView portalStack[MAX_VIEWS];
MaterialSystem materialSystem;
static void ComputeDynamics( shaderStage_t* pStage ) {
// TODO: Move color and texMatrices stuff to a compute shader
pStage->colorDynamic = false;
switch ( pStage->rgbGen ) {
case colorGen_t::CGEN_IDENTITY_LIGHTING:
case colorGen_t::CGEN_IDENTITY:
case colorGen_t::CGEN_ONE_MINUS_VERTEX:
case colorGen_t::CGEN_VERTEX:
case colorGen_t::CGEN_CONST:
default:
break;
case colorGen_t::CGEN_ENTITY:
case colorGen_t::CGEN_ONE_MINUS_ENTITY:
{
// TODO: Move this to some entity buffer once this is extended past BSP surfaces
if ( backEnd.currentEntity ) {
//
} else {
//
}
break;
}
case colorGen_t::CGEN_WAVEFORM:
case colorGen_t::CGEN_CUSTOM_RGB:
case colorGen_t::CGEN_CUSTOM_RGBs:
{
pStage->colorDynamic = true;
break;
}
}
switch ( pStage->alphaGen ) {
default:
case alphaGen_t::AGEN_IDENTITY:
case alphaGen_t::AGEN_ONE_MINUS_VERTEX:
case alphaGen_t::AGEN_VERTEX:
case alphaGen_t::AGEN_CONST: {
case alphaGen_t::AGEN_ENTITY:
case alphaGen_t::AGEN_ONE_MINUS_ENTITY:
// TODO: Move this to some entity buffer once this is extended past BSP surfaces
/* if ( backEnd.currentEntity ) {
} else {
} */
break;
}
case alphaGen_t::AGEN_WAVEFORM:
case alphaGen_t::AGEN_CUSTOM:
{
pStage->colorDynamic = true;
break;
}
}
// Can we move this to a compute shader too?
// Doesn't seem to be used much if at all, so probably not worth the effort to do that
pStage->dynamic = pStage->dynamic || pStage->ifExp.numOps;
pStage->dynamic = pStage->dynamic || pStage->alphaExp.numOps || pStage->alphaTestExp.numOps;
pStage->dynamic = pStage->dynamic || pStage->rgbExp.numOps || pStage->redExp.numOps || pStage->greenExp.numOps || pStage->blueExp.numOps;
pStage->dynamic = pStage->dynamic || pStage->deformMagnitudeExp.numOps;
pStage->dynamic = pStage->dynamic || pStage->depthScaleExp.numOps
|| pStage->fogDensityExp.numOps || pStage->fresnelBiasExp.numOps || pStage->fresnelPowerExp.numOps
|| pStage->fresnelScaleExp.numOps || pStage->normalIntensityExp.numOps || pStage->refractionIndexExp.numOps;
pStage->dynamic = pStage->dynamic || pStage->colorDynamic;
}
// UpdateSurface*() functions will actually write the uniform values to the SSBO
// Mirrors parts of the Render_*() functions in tr_shade.cpp
void UpdateSurfaceDataNONE( uint32_t*, shaderStage_t*, bool, bool, bool ) {
ASSERT_UNREACHABLE();
}
void UpdateSurfaceDataNOP( uint32_t*, shaderStage_t*, bool, bool, bool ) {
}
void UpdateSurfaceDataGeneric3D( uint32_t* materials, shaderStage_t* pStage, bool mayUseVertexOverbright, bool, bool ) {
// shader_t* shader = pStage->shader;
materials += pStage->bufferOffset;
// u_AlphaThreshold
gl_genericShaderMaterial->SetUniform_AlphaTest( pStage->stateBits );
// u_ColorModulate
colorGen_t rgbGen = SetRgbGen( pStage );
alphaGen_t alphaGen = SetAlphaGen( pStage );
const bool styleLightMap = pStage->type == stageType_t::ST_STYLELIGHTMAP || pStage->type == stageType_t::ST_STYLECOLORMAP;
gl_genericShaderMaterial->SetUniform_ColorModulateColorGen_Uint( rgbGen, alphaGen, mayUseVertexOverbright, styleLightMap );
Tess_ComputeColor( pStage );
gl_genericShaderMaterial->SetUniform_Color_Uint( tess.svars.color );
bool hasDepthFade = pStage->hasDepthFade;
if ( hasDepthFade ) {
gl_genericShaderMaterial->SetUniform_DepthScale( pStage->depthFadeValue );
}
gl_genericShaderMaterial->WriteUniformsToBuffer( materials );
}
void UpdateSurfaceDataLightMapping( uint32_t* materials, shaderStage_t* pStage, bool, bool vertexLit, bool fullbright ) {
shader_t* shader = pStage->shader;
materials += pStage->bufferOffset;
// u_ColorModulate
colorGen_t rgbGen = SetRgbGen( pStage );
alphaGen_t alphaGen = SetAlphaGen( pStage );
Tess_ComputeColor( pStage );
// HACK: This only has effect on vertex-lit surfaces
if ( vertexLit ) {
SetVertexLightingSettings( lightMode_t::VERTEX, rgbGen );
}
// u_ColorModulate
gl_lightMappingShaderMaterial->SetUniform_ColorModulateColorGen_Uint( rgbGen, alphaGen, false, !fullbright );
// u_Color
gl_lightMappingShaderMaterial->SetUniform_Color_Uint( tess.svars.color );
// u_AlphaThreshold
gl_lightMappingShaderMaterial->SetUniform_AlphaTest( pStage->stateBits );
// HeightMap
if ( pStage->enableReliefMapping ) {
float depthScale = RB_EvalExpression( &pStage->depthScaleExp, r_reliefDepthScale->value );
depthScale *= shader->reliefDepthScale;
gl_lightMappingShaderMaterial->SetUniform_ReliefDepthScale( depthScale );
gl_lightMappingShaderMaterial->SetUniform_ReliefOffsetBias( shader->reliefOffsetBias );
}
// bind u_NormalScale
if ( pStage->enableNormalMapping ) {
vec3_t normalScale;
SetNormalScale( pStage, normalScale );
gl_lightMappingShaderMaterial->SetUniform_NormalScale( normalScale );
}
if ( pStage->enableSpecularMapping ) {
float specExpMin = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value );
float specExpMax = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value );
gl_lightMappingShaderMaterial->SetUniform_SpecularExponent( specExpMin, specExpMax );
}
gl_lightMappingShaderMaterial->WriteUniformsToBuffer( materials );
}
void UpdateSurfaceDataReflection( uint32_t* materials, shaderStage_t* pStage, bool, bool, bool ) {
shader_t* shader = pStage->shader;
materials += pStage->bufferOffset;
// bind u_ColorMap
vec3_t position;
if ( backEnd.currentEntity && ( backEnd.currentEntity != &tr.worldEntity ) ) {
VectorCopy( backEnd.currentEntity->e.origin, position );
} else {
// FIXME position
VectorCopy( backEnd.viewParms.orientation.origin, position );
}
cubemapProbe_t* probes[ 1 ];
vec4_t trilerp;
R_GetNearestCubeMaps( position, probes, trilerp, 1 );
gl_reflectionShaderMaterial->SetUniform_ColorMapCubeBindless(
GL_BindToTMU( 0, probes[0]->cubemap )
);
if ( pStage->enableNormalMapping ) {
vec3_t normalScale;
SetNormalScale( pStage, normalScale );
gl_reflectionShaderMaterial->SetUniform_NormalScale( normalScale );
}
// u_depthScale u_reliefOffsetBias
if ( pStage->enableReliefMapping ) {
float depthScale = RB_EvalExpression( &pStage->depthScaleExp, r_reliefDepthScale->value );
float reliefDepthScale = shader->reliefDepthScale;
depthScale *= reliefDepthScale == 0 ? 1 : reliefDepthScale;
gl_reflectionShaderMaterial->SetUniform_ReliefDepthScale( depthScale );
gl_reflectionShaderMaterial->SetUniform_ReliefOffsetBias( shader->reliefOffsetBias );
}
gl_reflectionShaderMaterial->WriteUniformsToBuffer( materials );
}
void UpdateSurfaceDataSkybox( uint32_t* materials, shaderStage_t* pStage, bool, bool, bool ) {
// shader_t* shader = pStage->shader;
materials += pStage->bufferOffset;
// u_AlphaThreshold
gl_skyboxShaderMaterial->SetUniform_AlphaTest( GLS_ATEST_NONE );
gl_skyboxShaderMaterial->WriteUniformsToBuffer( materials );
}
void UpdateSurfaceDataScreen( uint32_t* materials, shaderStage_t* pStage, bool, bool, bool ) {
// shader_t* shader = pStage->shader;
materials += pStage->bufferOffset;
// bind u_CurrentMap
/* FIXME: This is currently unused, but u_CurrentMap was made global for other shaders,
this seems to be the only material system shader that might need it to not be global */
gl_screenShaderMaterial->SetUniform_CurrentMapBindless( BindAnimatedImage( 0, &pStage->bundle[TB_COLORMAP] ) );
gl_screenShaderMaterial->WriteUniformsToBuffer( materials );
}
void UpdateSurfaceDataHeatHaze( uint32_t* materials, shaderStage_t* pStage, bool, bool, bool ) {
// shader_t* shader = pStage->shader;
materials += pStage->bufferOffset;
float deformMagnitude = RB_EvalExpression( &pStage->deformMagnitudeExp, 1.0 );
gl_heatHazeShaderMaterial->SetUniform_DeformMagnitude( deformMagnitude );
if ( pStage->enableNormalMapping ) {
vec3_t normalScale;
SetNormalScale( pStage, normalScale );
// bind u_NormalScale
gl_heatHazeShaderMaterial->SetUniform_NormalScale( normalScale );
}
gl_heatHazeShaderMaterial->WriteUniformsToBuffer( materials );
}
void UpdateSurfaceDataLiquid( uint32_t* materials, shaderStage_t* pStage, bool, bool, bool ) {
// shader_t* shader = pStage->shader;
materials += pStage->bufferOffset;
float fogDensity = RB_EvalExpression( &pStage->fogDensityExp, 0.001 );
vec4_t fogColor;
Tess_ComputeColor( pStage );
VectorCopy( tess.svars.color.ToArray(), fogColor );
gl_liquidShaderMaterial->SetUniform_RefractionIndex( RB_EvalExpression( &pStage->refractionIndexExp, 1.0 ) );
gl_liquidShaderMaterial->SetUniform_FresnelPower( RB_EvalExpression( &pStage->fresnelPowerExp, 2.0 ) );
gl_liquidShaderMaterial->SetUniform_FresnelScale( RB_EvalExpression( &pStage->fresnelScaleExp, 1.0 ) );
gl_liquidShaderMaterial->SetUniform_FresnelBias( RB_EvalExpression( &pStage->fresnelBiasExp, 0.05 ) );
gl_liquidShaderMaterial->SetUniform_FogDensity( fogDensity );
gl_liquidShaderMaterial->SetUniform_FogColor( fogColor );
// NOTE: specular component is computed by shader.
// FIXME: physical mapping is not implemented.
if ( pStage->enableSpecularMapping ) {
float specMin = RB_EvalExpression( &pStage->specularExponentMin, r_specularExponentMin->value );
float specMax = RB_EvalExpression( &pStage->specularExponentMax, r_specularExponentMax->value );
gl_liquidShaderMaterial->SetUniform_SpecularExponent( specMin, specMax );
}
// bind u_CurrentMap
gl_liquidShaderMaterial->SetUniform_CurrentMapBindless( GL_BindToTMU( 0, tr.currentRenderImage[backEnd.currentMainFBO] ) );
// bind u_HeightMap u_depthScale u_reliefOffsetBias
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_liquidShaderMaterial->SetUniform_ReliefDepthScale( depthScale );
gl_liquidShaderMaterial->SetUniform_ReliefOffsetBias( tess.surfaceShader->reliefOffsetBias );
}
// bind u_NormalScale
if ( pStage->enableNormalMapping ) {
vec3_t normalScale;
// FIXME: NormalIntensity default was 0.5
SetNormalScale( pStage, normalScale );
gl_liquidShaderMaterial->SetUniform_NormalScale( normalScale );
}
gl_liquidShaderMaterial->WriteUniformsToBuffer( materials );
}
void UpdateSurfaceDataFog( uint32_t* materials, shaderStage_t* pStage, bool, bool, bool ) {
// shader_t* shader = pStage->shader;
materials += pStage->bufferOffset;
gl_fogQuake3ShaderMaterial->WriteUniformsToBuffer( materials );
}
/*
* Buffer layout:
* // Static surfaces data:
* // Stage0:
* uniform0_0
* uniform0_1
* ..
* uniform0_x
* optional_struct_padding
* // Stage1:
* ..
* // Stage_y:
* uniform0_0
* uniform0_1
* ..
* uniform0_x
* optional_struct_padding
* ..
* // Dynamic surfaces data:
* // Same as the static layout
*/
// Buffer is separated into static and dynamic parts so we can just update the whole dynamic range at once
// This will generate the actual buffer with per-stage values AFTER materials are generated
void MaterialSystem::GenerateWorldMaterialsBuffer() {
Log::Debug( "Generating materials buffer" );
// Sort by padded size to avoid extra padding
std::sort( materialStages.begin(), materialStages.end(),
[&]( const shaderStage_t* lhs, const shaderStage_t* rhs ) {
if ( !lhs->dynamic && rhs->dynamic ) {
return true;
}
if ( !rhs->dynamic && lhs->dynamic ) {
return false;
}
return lhs->paddedSize < rhs->paddedSize;
} );
uint32_t offset = 0;
dynamicStagesOffset = 0;
bool dynamicStagesOffsetSet = false;
// Compute data size for stages
for ( shaderStage_t* pStage : materialStages ) {
const uint32_t paddedSize = pStage->paddedSize;
const uint32_t padding = !paddedSize || offset % paddedSize == 0 ? 0 : paddedSize - ( offset % paddedSize );
offset += padding;
// Make sure padding is taken into account for dynamicStagesOffset
if ( pStage->dynamic ) {
if ( !dynamicStagesOffsetSet ) {
dynamicStagesOffset = offset;
dynamicStagesOffsetSet = true;
}
}
pStage->materialOffset = paddedSize ? offset / paddedSize : 0;
pStage->bufferOffset = offset;
offset += paddedSize * pStage->variantOffset;
}
dynamicStagesSize = dynamicStagesOffsetSet ? offset - dynamicStagesOffset : 0;
totalStageSize = offset;
// 4 bytes per component
materialsUBO.BufferData( offset, nullptr, GL_DYNAMIC_DRAW );
uint32_t* materialsData = materialsUBO.MapBufferRange( offset );
GenerateMaterialsBuffer( materialStages, offset, materialsData );
for ( uint32_t materialPackID = 0; materialPackID < 3; materialPackID++ ) {
for ( Material& material : materialPacks[materialPackID].materials ) {
for ( drawSurf_t* drawSurf : material.drawSurfs ) {
uint32_t stage = 0;
for ( shaderStage_t* pStage = drawSurf->shader->stages; pStage < drawSurf->shader->lastStage; pStage++ ) {
if ( drawSurf->materialIDs[stage] != material.id || drawSurf->materialPackIDs[stage] != materialPackID ) {
stage++;
continue;
}
// We need some of the values from the remapped stage, but material/materialPack ID has to come from pStage
shaderStage_t* remappedStage = pStage->materialRemappedStage ? pStage->materialRemappedStage : pStage;
const uint32_t SSBOOffset =
remappedStage->materialOffset + remappedStage->variantOffsets[drawSurf->shaderVariant[stage]];
tess.currentDrawSurf = drawSurf;
tess.currentSSBOOffset = SSBOOffset;
tess.materialID = drawSurf->materialIDs[stage];
tess.materialPackID = drawSurf->materialPackIDs[stage];
Tess_Begin( Tess_StageIteratorDummy, nullptr, nullptr, false, -1, 0 );
rb_surfaceTable[Util::ordinal( *drawSurf->surface )]( drawSurf->surface );
Tess_DrawElements();
Tess_Clear();
drawSurf->drawCommandIDs[stage] = lastCommandID;
stage++;
}
}
}
}
for ( shaderStage_t* pStage : materialStages ) {
if ( pStage->dynamic ) {
pStage->bufferOffset -= dynamicStagesOffset;
}
}
materialsUBO.UnmapBuffer();
}
void MaterialSystem::GenerateMaterialsBuffer( std::vector<shaderStage_t*>& stages, const uint32_t size, uint32_t* materialsData ) {
// Shader uniforms are set to 0 if they're not specified, so make sure we do that here too
memset( materialsData, 0, size * sizeof( uint32_t ) );
for ( shaderStage_t* pStage : stages ) {
/* Stage variants are essentially copies of the same stage with slightly different values that
normally come from a drawSurf_t */
uint32_t variants = 0;
for ( int i = 0; i < ShaderStageVariant::ALL && variants < pStage->variantOffset; i++ ) {
if ( pStage->variantOffsets[i] != -1 ) {
const bool mayUseVertexOverbright = i & ShaderStageVariant::VERTEX_OVERBRIGHT;
const bool vertexLit = i & ShaderStageVariant::VERTEX_LIT;
const bool fullbright = i & ShaderStageVariant::FULLBRIGHT;
const uint32_t variantOffset = pStage->variantOffsets[i] * pStage->paddedSize;
pStage->bufferOffset += variantOffset;
pStage->surfaceDataUpdater( materialsData, pStage, mayUseVertexOverbright, vertexLit, fullbright );
pStage->bufferOffset -= variantOffset;
variants++;
}
}
}
}
void MaterialSystem::GenerateTexturesBuffer( std::vector<TextureData>& textures, TexBundle* textureBundles ) {
for ( TextureData& textureData : textures ) {
for ( int i = 0; i < MAX_TEXTURE_BUNDLES; i++ ) {
if ( textureData.texBundlesOverride[i] ) {
textureBundles->textures[i] = textureData.texBundlesOverride[i]->texture->bindlessTextureHandle;
continue;
}
const textureBundle_t* bundle = textureData.texBundles[i];
if ( bundle && bundle->image[0] ) {
if ( generatingWorldCommandBuffer ) {
textureBundles->textures[i] = bundle->image[0]->texture->bindlessTextureHandle;
} else {
textureBundles->textures[i] = BindAnimatedImage( 0, bundle );
}
}
}
const int bundle = textureData.textureMatrixBundle;
RB_CalcTexMatrix( textureData.texBundles[bundle], tess.svars.texMatrices[bundle] );
/* We only actually need these 6 components to get the correct texture transformation,
the other ones are unused */
textureBundles->textureMatrix[0] = tess.svars.texMatrices[bundle][0];
textureBundles->textureMatrix[1] = tess.svars.texMatrices[bundle][1];
textureBundles->textureMatrix[2] = tess.svars.texMatrices[bundle][4];
textureBundles->textureMatrix[3] = tess.svars.texMatrices[bundle][5];
textureBundles->textureMatrix[4] = tess.svars.texMatrices[bundle][12];
textureBundles->textureMatrix[5] = tess.svars.texMatrices[bundle][13];
textureBundles++;
}
}
// This generates the buffers with indirect rendering commands etc.
void MaterialSystem::GenerateWorldCommandBuffer() {
Log::Debug( "Generating world command buffer" );
totalBatchCount = 0;
uint32_t batchOffset = 0;
uint32_t globalID = 0;
for ( MaterialPack& pack : materialPacks ) {
for ( Material& material : pack.materials ) {
material.surfaceCommandBatchOffset = batchOffset;
const uint32_t cmdCount = material.drawCommands.size();
const uint32_t batchCount = cmdCount % SURFACE_COMMANDS_PER_BATCH == 0 ? cmdCount / SURFACE_COMMANDS_PER_BATCH
: cmdCount / SURFACE_COMMANDS_PER_BATCH + 1;
material.surfaceCommandBatchOffset = batchOffset;
material.surfaceCommandBatchCount = batchCount;
batchOffset += batchCount;
material.globalID = globalID;
totalBatchCount += batchCount;
globalID++;
}
}
Log::Debug( "Total batch count: %u", totalBatchCount );
surfaceDescriptorsCount = totalDrawSurfs;
descriptorSize = BOUNDING_SPHERE_SIZE + maxStages;
surfaceDescriptorsSSBO.BufferData( surfaceDescriptorsCount * descriptorSize, nullptr, GL_STATIC_DRAW );
uint32_t* surfaceDescriptors = surfaceDescriptorsSSBO.MapBufferRange( surfaceDescriptorsCount * descriptorSize );
texDataBufferType = glConfig2.maxUniformBlockSize >= MIN_MATERIAL_UBO_SIZE ? GL_UNIFORM_BUFFER : GL_SHADER_STORAGE_BUFFER;
texDataBuffer.BufferStorage( ( texData.size() + dynamicTexData.size() ) * TEX_BUNDLE_SIZE, 1, nullptr );
texDataBuffer.MapAll();
TexBundle* textureBundles = ( TexBundle* ) texDataBuffer.GetData();
memset( textureBundles, 0, ( texData.size() + dynamicTexData.size() ) * TEX_BUNDLE_SIZE * sizeof( uint32_t ) );
GenerateTexturesBuffer( texData, textureBundles );
textureBundles += texData.size();
GenerateTexturesBuffer( dynamicTexData, textureBundles );
dynamicTexDataOffset = texData.size() * TEX_BUNDLE_SIZE;
dynamicTexDataSize = dynamicTexData.size() * TEX_BUNDLE_SIZE;
texDataBuffer.FlushAll();
texDataBuffer.UnmapBuffer();
lightMapDataUBO.BufferStorage( MAX_LIGHTMAPS * LIGHTMAP_SIZE, 1, nullptr );
lightMapDataUBO.MapAll();
uint64_t* lightMapData = ( uint64_t* ) lightMapDataUBO.GetData();
memset( lightMapData, 0, MAX_LIGHTMAPS * LIGHTMAP_SIZE * sizeof( uint32_t ) );
for ( uint32_t i = 0; i < tr.lightmaps.size(); i++ ) {
if ( !tr.lightmaps[i]->texture->hasBindlessHandle ) {
tr.lightmaps[i]->texture->GenBindlessHandle();
}
lightMapData[i * 2] = tr.lightmaps[i]->texture->bindlessTextureHandle;
}
for ( uint32_t i = 0; i < tr.deluxemaps.size(); i++ ) {
if ( !tr.deluxemaps[i]->texture->hasBindlessHandle ) {
tr.deluxemaps[i]->texture->GenBindlessHandle();
}
lightMapData[i * 2 + 1] = tr.deluxemaps[i]->texture->bindlessTextureHandle;
}
ASSERT_LE( tr.lightmaps.size(), 256 ); // Engine supports up to 256 lightmaps currently, so we use 8 bits to address them
if ( tr.lightmaps.size() == 256 ) {
/* It's very unlikely that this would actually happen, but put the warn here just in case
If needed, another bit can be added to the lightmap address in rendering commands, but that would mean
that its hex representation would no longer be easily "parsable" by just looking at it in a frame debugger */
Log::Warn( "Material system only supports up to 255 lightmaps, got 256" );
} else {
if ( !tr.whiteImage->texture->hasBindlessHandle ) {
tr.whiteImage->texture->GenBindlessHandle();
}
if ( !tr.blackImage->texture->hasBindlessHandle ) {
tr.blackImage->texture->GenBindlessHandle();
}
// Use lightmap 255 for drawSurfs that use a full white image for their lightmap
lightMapData[255 * 2] = tr.whiteImage->texture->bindlessTextureHandle;
lightMapData[255 * 2 + 1] = tr.blackImage->texture->bindlessTextureHandle;
}
lightMapDataUBO.FlushAll();
lightMapDataUBO.UnmapBuffer();
surfaceCommandsCount = totalBatchCount * SURFACE_COMMANDS_PER_BATCH;
surfaceCommandsSSBO.BufferStorage( surfaceCommandsCount * SURFACE_COMMAND_SIZE * MAX_VIEWFRAMES, 1, nullptr );
surfaceCommandsSSBO.MapAll();
SurfaceCommand* surfaceCommands = ( SurfaceCommand* ) surfaceCommandsSSBO.GetData();
memset( surfaceCommands, 0, surfaceCommandsCount * sizeof( SurfaceCommand ) * MAX_VIEWFRAMES );
culledCommandsBuffer.BufferStorage( surfaceCommandsCount * INDIRECT_COMMAND_SIZE * MAX_VIEWFRAMES, 1, nullptr );
culledCommandsBuffer.MapAll();
GLIndirectCommand* culledCommands = ( GLIndirectCommand* ) culledCommandsBuffer.GetData();
memset( culledCommands, 0, surfaceCommandsCount * sizeof( GLIndirectCommand ) * MAX_VIEWFRAMES );
culledCommandsBuffer.FlushAll();
surfaceBatchesUBO.BufferData( MAX_SURFACE_COMMAND_BATCHES * SURFACE_COMMAND_BATCH_SIZE, nullptr, GL_STATIC_DRAW );
SurfaceCommandBatch* surfaceCommandBatches =
( SurfaceCommandBatch* ) surfaceBatchesUBO.MapBufferRange( MAX_SURFACE_COMMAND_BATCHES * SURFACE_COMMAND_BATCH_SIZE );
// memset( (void*) surfaceCommandBatches, 0, MAX_SURFACE_COMMAND_BATCHES * SURFACE_COMMAND_BATCH_SIZE );
// Fuck off gcc
for ( int i = 0; i < MAX_SURFACE_COMMAND_BATCHES; i++ ) {
surfaceCommandBatches[i] = {};
}
uint32_t id = 0;
uint32_t matID = 0;
for ( MaterialPack& pack : materialPacks ) {
for ( Material& mat : pack.materials ) {
for ( uint32_t i = 0; i < mat.surfaceCommandBatchCount; i++ ) {
surfaceCommandBatches[id].materialIDs[0] = matID;
surfaceCommandBatches[id].materialIDs[1] = mat.surfaceCommandBatchOffset;
id++;
}
matID++;
}
}
atomicCommandCountersBuffer.BufferStorage( MAX_COMMAND_COUNTERS * MAX_VIEWS, MAX_FRAMES, nullptr );
atomicCommandCountersBuffer.MapAll();
uint32_t* atomicCommandCounters = ( uint32_t* ) atomicCommandCountersBuffer.GetData();
memset( atomicCommandCounters, 0, MAX_COMMAND_COUNTERS * MAX_VIEWFRAMES * sizeof( uint32_t ) );
/* For use in debugging compute shaders
Intended for use with Nsight Graphics to format the output */
if ( r_materialDebug.Get() ) {
const uint32_t debugSize = surfaceCommandsCount * 20;
debugSSBO.BufferData( debugSize, nullptr, GL_STATIC_DRAW );
uint32_t* debugBuffer = debugSSBO.MapBufferRange( debugSize );
memset( debugBuffer, 0, debugSize * sizeof( uint32_t ) );
debugSSBO.UnmapBuffer();
}
for ( int i = 0; i < tr.refdef.numDrawSurfs; i++ ) {
const drawSurf_t* drawSurf = &tr.refdef.drawSurfs[i];
if ( drawSurf->entity != &tr.worldEntity ) {
continue;
}
shader_t* shader = drawSurf->shader;
if ( !shader ) {
continue;
}
shader = shader->remappedShader ? shader->remappedShader : shader;
if ( shader->isSky || shader->isPortal || shader->autoSpriteMode ) {
continue;
}
// Don't add SF_SKIP surfaces
if ( *drawSurf->surface == surfaceType_t::SF_SKIP ) {
continue;
}
// Depth prepass surfaces are added as stages to the main surface instead
if ( drawSurf->materialSystemSkip ) {
continue;
}
SurfaceDescriptor surface;
VectorCopy( ( ( srfGeneric_t* ) drawSurf->surface )->origin, surface.boundingSphere.origin );
surface.boundingSphere.radius = ( ( srfGeneric_t* ) drawSurf->surface )->radius;
const bool depthPrePass = drawSurf->depthSurface != nullptr;
if ( depthPrePass ) {
const drawSurf_t* depthDrawSurf = drawSurf->depthSurface;
const Material* material = &materialPacks[depthDrawSurf->materialPackIDs[0]]
.materials[depthDrawSurf->materialIDs[0]];
uint cmdID = material->surfaceCommandBatchOffset * SURFACE_COMMANDS_PER_BATCH + depthDrawSurf->drawCommandIDs[0];
// Add 1 because cmd 0 == no-command
surface.surfaceCommandIDs[0] = cmdID + 1;
SurfaceCommand surfaceCommand;
surfaceCommand.enabled = 0;
surfaceCommand.drawCommand = material->drawCommands[depthDrawSurf->drawCommandIDs[0]].cmd;
// We still need the textures for alpha-tested depth pre-pass surface commands
surfaceCommand.drawCommand.baseInstance |= depthDrawSurf->texDataDynamic[0]
? ( depthDrawSurf->texDataIDs[0] + texData.size() ) << TEX_BUNDLE_BITS
: depthDrawSurf->texDataIDs[0] << TEX_BUNDLE_BITS;
surfaceCommands[cmdID] = surfaceCommand;
}
uint32_t stage = 0;
for ( shaderStage_t* pStage = drawSurf->shader->stages; pStage < drawSurf->shader->lastStage; pStage++ ) {
const Material* material = &materialPacks[drawSurf->materialPackIDs[stage]].materials[drawSurf->materialIDs[stage]];
uint32_t cmdID = material->surfaceCommandBatchOffset * SURFACE_COMMANDS_PER_BATCH + drawSurf->drawCommandIDs[stage];
// Add 1 because cmd 0 == no-command
surface.surfaceCommandIDs[stage + ( depthPrePass ? 1 : 0 )] = cmdID + 1;
SurfaceCommand surfaceCommand;
surfaceCommand.enabled = 0;
surfaceCommand.drawCommand = material->drawCommands[drawSurf->drawCommandIDs[stage]].cmd;
surfaceCommand.drawCommand.baseInstance |= drawSurf->texDataDynamic[stage]
? ( drawSurf->texDataIDs[stage] + texData.size() ) << TEX_BUNDLE_BITS
: drawSurf->texDataIDs[stage] << TEX_BUNDLE_BITS;
surfaceCommand.drawCommand.baseInstance |= ( HasLightMap( drawSurf ) ? GetLightMapNum( drawSurf ) : 255 ) << LIGHTMAP_BITS;
surfaceCommands[cmdID] = surfaceCommand;
stage++;
}
if ( drawSurf->fogSurface ) {
const drawSurf_t* fogDrawSurf = drawSurf->fogSurface;
const Material* material = &materialPacks[fogDrawSurf->materialPackIDs[0]]
.materials[fogDrawSurf->materialIDs[0]];
uint cmdID = material->surfaceCommandBatchOffset * SURFACE_COMMANDS_PER_BATCH + fogDrawSurf->drawCommandIDs[0];
// Add 1 because cmd 0 == no-command
surface.surfaceCommandIDs[stage + ( depthPrePass ? 1 : 0 )] = cmdID + 1;
SurfaceCommand surfaceCommand;
surfaceCommand.enabled = 0;
surfaceCommand.drawCommand = material->drawCommands[fogDrawSurf->drawCommandIDs[0]].cmd;
surfaceCommands[cmdID] = surfaceCommand;
}
memcpy( surfaceDescriptors, &surface, descriptorSize * sizeof( uint32_t ) );
surfaceDescriptors += descriptorSize;
}
for ( int i = 0; i < MAX_VIEWFRAMES; i++ ) {
memcpy( surfaceCommands + surfaceCommandsCount * i, surfaceCommands, surfaceCommandsCount * sizeof( SurfaceCommand ) );
}
surfaceDescriptorsSSBO.UnmapBuffer();
surfaceCommandsSSBO.UnmapBuffer();
culledCommandsBuffer.UnmapBuffer();
atomicCommandCountersBuffer.UnmapBuffer();
surfaceBatchesUBO.UnmapBuffer();
GL_CheckErrors();
}
void MaterialSystem::GenerateDepthImages( const int width, const int height, imageParams_t imageParms ) {
imageParms.bits ^= ( IF_NOPICMIP | IF_PACKED_DEPTH24_STENCIL8 );
imageParms.bits |= IF_ONECOMP32F;
depthImageLevels = log2f( std::max( width, height ) ) + 1;
depthImage = R_CreateImage( "_depthImage", nullptr, width, height, depthImageLevels, imageParms );
GL_Bind( depthImage );
int mipmapWidth = width;
int mipmapHeight = height;
for ( int i = 0; i < depthImageLevels; i++ ) {
glTexImage2D( GL_TEXTURE_2D, i, GL_R32F, mipmapWidth, mipmapHeight, 0, GL_RED, GL_FLOAT, nullptr );
mipmapWidth = mipmapWidth > 1 ? mipmapWidth >> 1 : 1;
mipmapHeight = mipmapHeight > 1 ? mipmapHeight >> 1 : 1;
}
}
void BindShaderNONE( Material* ) {
ASSERT_UNREACHABLE();
}
void BindShaderNOP( Material* ) {
}
void BindShaderGeneric3D( Material* material ) {
// Select shader permutation.
gl_genericShaderMaterial->SetTCGenEnvironment( material->tcGenEnvironment );
gl_genericShaderMaterial->SetTCGenLightmap( material->tcGen_Lightmap );
gl_genericShaderMaterial->SetDepthFade( material->hasDepthFade );
// Bind shader program.
gl_genericShaderMaterial->BindProgram( material->deformIndex );
// Set shader uniforms.
if ( material->tcGenEnvironment ) {
gl_genericShaderMaterial->SetUniform_ViewOrigin( backEnd.orientation.viewOrigin );
gl_genericShaderMaterial->SetUniform_ViewUp( backEnd.orientation.axis[2] );
}
gl_genericShaderMaterial->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
gl_genericShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
gl_genericShaderMaterial->SetUniform_DepthMapBindless( GL_BindToTMU( 1, tr.currentDepthImage ) );
// u_DeformGen
gl_genericShaderMaterial->SetUniform_Time( backEnd.refdef.floatTime - backEnd.currentEntity->e.shaderTime );
if ( r_profilerRenderSubGroups.Get() ) {
gl_genericShaderMaterial->SetUniform_ProfilerZero();
gl_genericShaderMaterial->SetUniform_ProfilerRenderSubGroups( GetShaderProfilerRenderSubGroupsMode( material->stateBits ) );
}
}
void BindShaderLightMapping( Material* material ) {
// Select shader permutation.
gl_lightMappingShaderMaterial->SetBspSurface( material->bspSurface );
gl_lightMappingShaderMaterial->SetDeluxeMapping( material->enableDeluxeMapping );
gl_lightMappingShaderMaterial->SetGridLighting( material->enableGridLighting );
gl_lightMappingShaderMaterial->SetGridDeluxeMapping( material->enableGridDeluxeMapping );
gl_lightMappingShaderMaterial->SetHeightMapInNormalMap( material->hasHeightMapInNormalMap );
gl_lightMappingShaderMaterial->SetReliefMapping( material->enableReliefMapping );
/* Reflective specular setting is different here than in ProcessMaterialLightMapping(),
because we don't have cubemaps built yet at this point, but for the purposes of the material ordering there's no difference */
gl_lightMappingShaderMaterial->SetReflectiveSpecular( glConfig2.reflectionMapping && material->enableSpecularMapping && !( tr.refdef.rdflags & RDF_NOCUBEMAP ) );
gl_lightMappingShaderMaterial->SetPhysicalShading( material->enablePhysicalMapping );
// Bind shader program.
gl_lightMappingShaderMaterial->BindProgram( material->deformIndex );
// Set shader uniforms.
if ( tr.world ) {
gl_lightMappingShaderMaterial->SetUniform_LightGridOrigin( tr.world->lightGridGLOrigin );
gl_lightMappingShaderMaterial->SetUniform_LightGridScale( tr.world->lightGridGLScale );
}
// FIXME: else
// bind u_LightGrid1
if ( material->enableGridLighting ) {
gl_lightMappingShaderMaterial->SetUniform_LightGrid1Bindless( GL_BindToTMU( BIND_LIGHTMAP, tr.lightGrid1Image ) );
}
// bind u_LightGrid2
if ( material->enableGridDeluxeMapping ) {
gl_lightMappingShaderMaterial->SetUniform_LightGrid2Bindless( GL_BindToTMU( BIND_DELUXEMAP, tr.lightGrid2Image ) );
}
if ( glConfig2.realtimeLighting ) {
gl_lightMappingShaderMaterial->SetUniformBlock_Lights( tr.dlightUBO );
// bind u_LightTiles
if ( r_realtimeLightingRenderer.Get() == Util::ordinal( realtimeLightingRenderer_t::TILED ) ) {
gl_lightMappingShaderMaterial->SetUniform_LightTilesBindless(
GL_BindToTMU( BIND_LIGHTTILES, tr.lighttileRenderImage )
);
}
}
gl_lightMappingShaderMaterial->SetUniform_ViewOrigin( backEnd.orientation.viewOrigin );
gl_lightMappingShaderMaterial->SetUniform_numLights( backEnd.refdef.numLights );
gl_lightMappingShaderMaterial->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
gl_lightMappingShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
// u_DeformGen
gl_lightMappingShaderMaterial->SetUniform_Time( backEnd.refdef.floatTime - backEnd.currentEntity->e.shaderTime );
// TODO: Move this to a per-entity buffer
if ( glConfig2.reflectionMapping && !( tr.refdef.rdflags & RDF_NOCUBEMAP ) ) {
bool isWorldEntity = backEnd.currentEntity == &tr.worldEntity;
vec3_t position;
if ( backEnd.currentEntity && !isWorldEntity ) {
VectorCopy( backEnd.currentEntity->e.origin, position );
return;
} else {
// FIXME position
VectorCopy( backEnd.orientation.viewOrigin, position );
}
cubemapProbe_t* probes[2];
vec4_t trilerp;
// TODO: Add a code path that would assign a cubemap to each tile for the tiled renderer
R_GetNearestCubeMaps( position, probes, trilerp, 2 );
const cubemapProbe_t* cubeProbeNearest = probes[0];
const cubemapProbe_t* cubeProbeSecondNearest = probes[1];
const float interpolation = 1.0 - trilerp[0];
GLIMP_LOGCOMMENT( "Probe 0 distance = %f, probe 1 distance = %f, interpolation = %f",
Distance( position, probes[0]->origin ), Distance( position, probes[1]->origin ), interpolation );
// bind u_EnvironmentMap0
gl_lightMappingShaderMaterial->SetUniform_EnvironmentMap0Bindless(
GL_BindToTMU( BIND_ENVIRONMENTMAP0, cubeProbeNearest->cubemap )
);
// bind u_EnvironmentMap1
gl_lightMappingShaderMaterial->SetUniform_EnvironmentMap1Bindless(
GL_BindToTMU( BIND_ENVIRONMENTMAP1, cubeProbeSecondNearest->cubemap )
);
// bind u_EnvironmentInterpolation
gl_lightMappingShaderMaterial->SetUniform_EnvironmentInterpolation( interpolation );
}
if ( r_profilerRenderSubGroups.Get() ) {
gl_lightMappingShaderMaterial->SetUniform_ProfilerZero();
gl_lightMappingShaderMaterial->SetUniform_ProfilerRenderSubGroups( GetShaderProfilerRenderSubGroupsMode( material->stateBits ) );
}
}
void BindShaderReflection( Material* material ) {
// Select shader permutation.
gl_reflectionShaderMaterial->SetHeightMapInNormalMap( material->hasHeightMapInNormalMap );
gl_reflectionShaderMaterial->SetReliefMapping( material->enableReliefMapping );
// Bind shader program.
gl_reflectionShaderMaterial->BindProgram( material->deformIndex );
// Set shader uniforms.
gl_reflectionShaderMaterial->SetUniform_ViewOrigin( backEnd.viewParms.orientation.origin );
gl_reflectionShaderMaterial->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
gl_reflectionShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
}
void BindShaderSkybox( Material* material ) {
// Bind shader program.
gl_skyboxShaderMaterial->BindProgram( material->deformIndex );
// Set shader uniforms.
gl_skyboxShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
}
void BindShaderScreen( Material* material ) {
// Bind shader program.
gl_screenShaderMaterial->BindProgram( material->deformIndex );
// Set shader uniforms.
gl_screenShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
}
void BindShaderHeatHaze( Material* material ) {
// Bind shader program.
gl_heatHazeShaderMaterial->BindProgram( material->deformIndex );
// Set shader uniforms.
gl_heatHazeShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
gl_heatHazeShaderMaterial->SetUniform_ModelViewMatrixTranspose( glState.modelViewMatrix[glState.stackIndex] );
gl_heatHazeShaderMaterial->SetUniform_ProjectionMatrixTranspose( glState.projectionMatrix[glState.stackIndex] );
gl_heatHazeShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
// bind u_CurrentMap
gl_heatHazeShaderMaterial->SetUniform_CurrentMapBindless(
GL_BindToTMU( 1, tr.currentRenderImage[backEnd.currentMainFBO] )
);
gl_heatHazeShaderMaterial->SetUniform_DeformEnable( true );
// draw to background image
R_BindFBO( tr.mainFBO[1 - backEnd.currentMainFBO] );
}
void BindShaderLiquid( Material* material ) {
// Select shader permutation.
gl_liquidShaderMaterial->SetHeightMapInNormalMap( material->hasHeightMapInNormalMap );
gl_liquidShaderMaterial->SetReliefMapping( material->enableReliefMapping );
gl_liquidShaderMaterial->SetGridDeluxeMapping( material->enableGridDeluxeMapping );
gl_liquidShaderMaterial->SetGridLighting( material->enableGridLighting );
// Bind shader program.
gl_liquidShaderMaterial->BindProgram( material->deformIndex );
// Set shader uniforms.
gl_liquidShaderMaterial->SetUniform_ViewOrigin( backEnd.viewParms.orientation.origin );
gl_liquidShaderMaterial->SetUniform_UnprojectMatrix( backEnd.viewParms.unprojectionMatrix );
gl_liquidShaderMaterial->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
gl_liquidShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );