-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathgl_shader.h
More file actions
3530 lines (3001 loc) · 78.3 KB
/
gl_shader.h
File metadata and controls
3530 lines (3001 loc) · 78.3 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
/*
===========================================================================
Copyright (C) 2010-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
===========================================================================
*/
#ifndef GL_SHADER_H
#define GL_SHADER_H
#include "tr_local.h"
#include "BufferBind.h"
#include "GLUtils.h"
#include <stdexcept>
// *INDENT-OFF*
static const unsigned int MAX_SHADER_MACROS = 10;
static const unsigned int GL_SHADER_VERSION = 6;
class ShaderException : public std::runtime_error
{
public:
ShaderException(const char* msg) : std::runtime_error(msg) { }
};
enum class ShaderKind
{
Unknown,
BuiltIn,
External
};
// Header for saved shader binaries
struct GLBinaryHeader {
uint32_t version;
uint32_t checkSum; // checksum of shader source this was built from
uint32_t driverVersionHash; // detect if the graphics driver was different
GLuint type;
uint32_t macro; // Bitmask of macros the shader uses ( may or may not be enabled )
GLenum binaryFormat; // argument to glProgramBinary
uint32_t binaryLength; // argument to glProgramBinary
};
class GLUniform;
class GLShader;
class GLUniformBlock;
class GLCompileMacro;
class GLShaderManager;
// represents a piece of GLSL code that can be copied verbatim into
// GLShaders, like a .h file in C++
struct GLHeader {
std::string name;
std::string text;
GLHeader() :
name(),
text() {
}
GLHeader( const std::string& newName, const std::string& newText ) :
name( newName ),
text( newText ) {
}
};
struct ShaderEntry {
std::string name;
uint32_t macro;
GLuint type;
bool operator==( const ShaderEntry& other ) const {
return name == other.name && macro == other.macro && type == other.type;
}
bool operator!=( const ShaderEntry& other ) const {
return !( *this == other );
}
};
struct ShaderDescriptor {
std::string name;
std::string macros;
uint32_t macro;
GLenum type;
bool main = false;
GLuint id = 0;
std::string shaderSource;
};
static const uint32_t MAX_SHADER_PROGRAM_SHADERS = 16;
struct ShaderProgramDescriptor {
GLuint id = 0;
bool hasMain = false;
GLuint type;
uint32_t macro = 0;
GLuint shaders[MAX_SHADER_PROGRAM_SHADERS] {};
ShaderEntry shaderNames[MAX_SHADER_PROGRAM_SHADERS] {};
std::string mainShader;
uint32_t shaderCount = 0;
GLint* uniformLocations;
GLuint* uniformBlockIndexes = nullptr;
uint32_t* uniformStorage;
uint32_t checkSum;
void AttachShader( ShaderDescriptor* descriptor ) {
if ( shaderCount == MAX_SHADER_PROGRAM_SHADERS ) {
Log::Warn( "Tried to attach too many shaders to program: skipping shader %s %s", descriptor->name, descriptor->macros );
return;
}
if ( !shaderCount ) {
type = descriptor->type;
} else if ( type != descriptor->type ) {
type = 0;
}
if ( descriptor->main ) {
if ( hasMain && mainShader != descriptor->name ) {
Log::Warn( "More than one shader specified as main, current: %s, new: %s, using current",
mainShader, descriptor->name );
} else {
mainShader = descriptor->name;
hasMain = true;
}
}
shaders[shaderCount] = descriptor->id;
shaderNames[shaderCount].name = descriptor->name;
shaderNames[shaderCount].macro = descriptor->macro;
shaderNames[shaderCount].type = descriptor->type;
macro |= descriptor->macro;
shaderCount++;
};
};
class GLShader {
friend class GLShaderManager;
friend class GLDeformStage;
public:
const std::string _name;
private:
GLShader( const GLShader & ) = delete;
GLShader &operator = ( const GLShader & ) = delete;
const uint32_t _vertexAttribsRequired;
const bool _useMaterialSystem;
std::string vertexShaderName;
std::string fragmentShaderName;
std::string computeShaderName;
const bool hasVertexShader;
const bool hasFragmentShader;
const bool hasComputeShader;
GLuint std140Size = 0;
const bool worldShader;
const bool pushSkip;
protected:
int _activeMacros = 0;
int _deformIndex = 0;
ShaderProgramDescriptor* currentProgram;
uint32_t _vertexAttribs = 0; // can be set by uniforms
std::vector<ShaderProgramDescriptor> shaderPrograms;
std::vector<bool> shaderProgramsToBuild;
std::vector<int> vertexShaderDescriptors;
std::vector<int> fragmentShaderDescriptors;
std::vector<int> computeShaderDescriptors;
size_t _uniformStorageSize;
std::vector<GLUniform*> _uniforms;
std::vector<GLUniform*> _pushUniforms;
std::vector<GLUniform*> _materialSystemUniforms;
std::vector<GLUniformBlock*> _uniformBlocks;
std::vector<GLCompileMacro*> _compileMacros;
GLShader( const std::string& name, uint32_t vertexAttribsRequired,
const bool useMaterialSystem,
const std::string newVertexShaderName, const std::string newFragmentShaderName,
const bool newPushSkip = false ) :
_name( name ),
_vertexAttribsRequired( vertexAttribsRequired ),
_useMaterialSystem( useMaterialSystem ),
vertexShaderName( newVertexShaderName),
fragmentShaderName( newFragmentShaderName ),
hasVertexShader( true ),
hasFragmentShader( true ),
hasComputeShader( false ),
worldShader( false ),
pushSkip( newPushSkip ) {
}
GLShader( const std::string& name,
const bool useMaterialSystem,
const std::string newComputeShaderName, const bool newWorldShader = false ) :
_name( name ),
_vertexAttribsRequired( 0 ),
_useMaterialSystem( useMaterialSystem ),
computeShaderName( newComputeShaderName ),
hasVertexShader( false ),
hasFragmentShader( false ),
hasComputeShader( true ),
worldShader( newWorldShader ),
pushSkip( false ) {
}
public:
virtual ~GLShader() {
}
void RegisterUniform( GLUniform* uniform );
void RegisterUniformBlock( GLUniformBlock *uniformBlock ) {
_uniformBlocks.push_back( uniformBlock );
}
void RegisterCompileMacro( GLCompileMacro *compileMacro ) {
if ( _compileMacros.size() >= MAX_SHADER_MACROS )
{
Sys::Drop( "Can't register more than %u compile macros for a single shader", MAX_SHADER_MACROS );
}
_compileMacros.push_back( compileMacro );
}
size_t GetNumOfCompiledMacros() const {
return _compileMacros.size();
}
GLint GetUniformLocation( const GLchar *uniformName ) const;
ShaderProgramDescriptor* GetProgram() const {
return currentProgram;
}
protected:
void PostProcessUniforms();
uint32_t GetUniqueCompileMacros( size_t permutation, const int type ) const;
bool GetCompileMacrosString( size_t permutation, std::string &compileMacrosOut, const int type ) const;
virtual void SetShaderProgramUniforms( ShaderProgramDescriptor* /*shaderProgram*/ ) { };
int SelectProgram();
public:
enum Mode {
MATERIAL,
PUSH
};
uint32_t* uniformStorage = nullptr;
bool uniformsUpdated = true;
void MarkProgramForBuilding();
GLuint GetProgram( const bool buildOneShader );
void BindProgram();
void DispatchCompute( const GLuint globalWorkgroupX, const GLuint globalWorkgroupY, const GLuint globalWorkgroupZ );
void DispatchComputeIndirect( const GLintptr indirectBuffer );
void SetRequiredVertexPointers();
bool IsMacroSet( int bit ) {
return ( _activeMacros & bit ) != 0;
}
void AddMacroBit( int bit ) {
_activeMacros |= bit;
}
void DelMacroBit( int bit ) {
_activeMacros &= ~bit;
}
bool IsVertexAttribSet( int bit ) {
return ( _vertexAttribs & bit ) != 0;
}
void AddVertexAttribBit( int bit ) {
_vertexAttribs |= bit;
}
void DelVertexAttribBit( int bit ) {
_vertexAttribs &= ~bit;
}
GLuint GetSTD140Size() const {
return std140Size;
}
bool UseMaterialSystem() const {
return _useMaterialSystem;
}
void WriteUniformsToBuffer( uint32_t* buffer, const Mode mode, const int filter = -1 );
};
class GLUniform {
public:
enum UpdateType {
CONST, // Set once at map load
FRAME, // Set at the start of a frame
PUSH, // Set based on the surface/shader/etc.
// Set based on the surface/shader/etc. If the material system is enabled, will go into the materials UBO instead
MATERIAL_OR_PUSH,
// Set based on the surface/shader/etc. If the material system is enabled, will go into the texbundles buffer instead
TEXDATA_OR_PUSH,
LEGACY, // These won't actually go into the buffer, it's only to skip uniforms used as fallbacks for older devices
SKIP
};
const std::string _name;
const std::string _type;
// In multiples of 4 bytes
// FIXME: the uniform structs are actually std140 so it would be more relevant to provide std140 info
const GLuint _std430BaseSize;
GLuint _std430Size; // includes padding that depends on the other uniforms in the struct
const GLuint _std430Alignment;
const GLuint _bufferSize;
GLuint _nextUniformOffset;
const UpdateType _updateType;
const int _components;
const bool _isTexture;
protected:
GLShader* _shader;
size_t _locationIndex;
size_t _uniformStorageOffset;
GLUniform( GLShader* shader, const char* name, const char* type, const GLuint std430Size, const GLuint std430Alignment,
const UpdateType updateType, const int components = 0,
const bool isTexture = false ) :
_name( name ),
_type( type ),
_std430BaseSize( std430Size ),
_std430Size( std430Size ),
_std430Alignment( std430Alignment ),
_bufferSize( components ? components * 4 : std430Size ),
_nextUniformOffset( components ? components * 4 : std430Size ),
_updateType( updateType ),
_components( components ),
_isTexture( isTexture ),
_shader( shader ) {
_shader->RegisterUniform( this );
}
// Returns true if the arg is different than cached value and the memory used for this uniform is not part of the material buffer or push buffer
bool CacheValue( const void* value ) {
uint32_t* currentValue;
const bool bufferUniform = ( _shader->UseMaterialSystem() && _updateType == MATERIAL_OR_PUSH )
|| ( glConfig.pushBufferAvailable && _updateType <= FRAME );
if ( bufferUniform ) {
currentValue = _shader->uniformStorage + _uniformStorageOffset;
} else {
ShaderProgramDescriptor* p = _shader->GetProgram();
DAEMON_ASSERT_EQ( p, glState.currentProgram );
currentValue = p->uniformStorage + _uniformStorageOffset;
}
const bool updated = memcmp( currentValue, value, _bufferSize * sizeof( uint32_t ) );
if ( updated ) {
memcpy( currentValue, value, _bufferSize * sizeof( uint32_t ) );
_shader->uniformsUpdated = true;
}
return updated && !bufferUniform;
}
public:
virtual ~GLUniform() = default;
void SetLocationIndex( size_t index ) {
_locationIndex = index;
}
void SetUniformStorageOffset( size_t index ) {
_uniformStorageOffset = index;
}
// This should return a pointer to the memory right after the one this uniform wrote to
uint32_t* WriteToBuffer( uint32_t* buffer ) {
uint32_t* currentValue;
const bool bufferUniform = ( _shader->UseMaterialSystem() && _updateType == MATERIAL_OR_PUSH )
|| ( glConfig.pushBufferAvailable && _updateType <= FRAME );
if ( bufferUniform ) {
currentValue = _shader->uniformStorage + _uniformStorageOffset;
} else {
return buffer;
}
memcpy( buffer, currentValue, _bufferSize * sizeof( uint32_t ) );
return buffer + _nextUniformOffset;
}
void UpdateShaderProgramUniformLocation( ShaderProgramDescriptor* shaderProgram ) {
shaderProgram->uniformLocations[_locationIndex] = glGetUniformLocation( shaderProgram->id, _name.c_str() );
}
};
class GLShaderManager {
std::queue<GLShader*> _shaderBuildQueue;
std::vector<std::unique_ptr<GLShader>> _shaders;
uint32_t deformShaderCount = 0;
std::unordered_map<std::string, int> _deformShaderLookup;
unsigned int _driverVersionHash; // For cache invalidation if hardware changes
public:
GLHeader GLVersionDeclaration;
GLHeader GLComputeVersionDeclaration;
GLHeader GLCompatHeader;
GLHeader GLVertexHeader;
GLHeader GLFragmentHeader;
GLHeader GLComputeHeader;
GLHeader GLWorldHeader;
GLHeader GLEngineConstants;
std::string globalUniformBlock;
GLShaderManager() {}
~GLShaderManager();
void InitDriverInfo();
void GenerateBuiltinHeaders();
void GenerateWorldHeaders();
static GLuint SortUniforms( std::vector<GLUniform*>& uniforms );
static std::vector<GLUniform*> ProcessUniforms( const GLUniform::UpdateType minType, const GLUniform::UpdateType maxType,
const bool skipTextures, std::vector<GLUniform*>& uniforms, GLuint& structSize );
template<class T>
void LoadShader( T*& shader ) {
if ( !deformShaderCount ) {
Q_UNUSED( GetDeformShaderIndex( nullptr, 0 ) );
initTime = 0;
initCount = 0;
}
shader = new T();
shader->PostProcessUniforms();
_shaders.emplace_back( shader );
_shaderBuildQueue.push( shader );
}
void InitShaders() {
for ( const std::unique_ptr<GLShader>& shader : _shaders ) {
if ( !shader.get()->worldShader ) {
InitShader( shader.get() );
}
}
}
void InitWorldShaders() {
for ( const std::unique_ptr<GLShader>& shader : _shaders ) {
if ( shader.get()->worldShader ) {
InitShader( shader.get() );
}
}
}
int GetDeformShaderIndex( deformStage_t *deforms, int numDeforms );
bool BuildPermutation( GLShader* shader, int index, const bool buildOneShader );
void BuildAll( const bool buildOnlyMarked );
void FreeAll();
void PostProcessGlobalUniforms();
void BindBuffers();
private:
struct InfoLogEntry {
int line;
int character;
std::string token;
std::string error;
};
std::vector<ShaderDescriptor> shaderDescriptors;
std::vector<ShaderProgramDescriptor> shaderProgramDescriptors;
int compileTime;
uint32_t compileCount;
int linkTime;
uint32_t linkCount;
int initTime;
uint32_t initCount;
int cacheLoadTime;
uint32_t cacheLoadCount;
int cacheSaveTime;
uint32_t cacheSaveCount;
void BuildShader( ShaderDescriptor* descriptor );
void BuildShaderProgram( ShaderProgramDescriptor* descriptor );
std::string GetDeformShaderName( const int index );
ShaderProgramDescriptor* FindShaderProgram( std::vector<ShaderEntry>& shaders, const std::string& mainShader );
bool LoadShaderBinary( const std::vector<ShaderEntry>& shaders, const std::string& mainShader,
ShaderProgramDescriptor* out );
void SaveShaderBinary( ShaderProgramDescriptor* descriptor );
uint32_t GenerateUniformStructDefinesText(
const std::vector<GLUniform*>& uniforms, const std::string& definesName, const uint32_t offset,
std::string& uniformStruct, std::string& uniformDefines );
std::string RemoveUniformsFromShaderText( const std::string& shaderText, const std::vector<GLUniform*>& uniforms );
std::string ShaderPostProcess( GLShader *shader, const std::string& shaderText, const uint32_t offset );
std::string BuildDeformShaderText( const std::string& steps );
std::string ProcessInserts( const std::string& shaderText ) const;
void LinkProgram( GLuint program ) const;
void BindAttribLocations( GLuint program ) const;
void PrintShaderSource( Str::StringRef programName, GLuint object, std::vector<InfoLogEntry>& infoLogLines ) const;
std::vector<InfoLogEntry> ParseInfoLog( const std::string& infoLog ) const;
std::string GetInfoLog( GLuint object ) const;
std::string BuildShaderText( const std::string& mainShaderText, const std::vector<GLHeader*>& headers, const std::string& macros );
ShaderDescriptor* FindShader( const std::string& name, const std::string& mainText,
const GLenum type, const std::vector<GLHeader*>& headers,
const uint32_t macro = 0, const std::string& compileMacros = "", const bool main = false );
void InitShader( GLShader* shader );
void UpdateShaderProgramUniformLocations( GLShader* shader, ShaderProgramDescriptor* shaderProgram ) const;
};
class GLUniformSampler : protected GLUniform {
protected:
GLUniformSampler( GLShader* shader, const char* name, const char* type, const UpdateType updateType ) :
GLUniform( shader, name, type, glConfig.bindlessTexturesAvailable ? 2 : 1,
glConfig.bindlessTexturesAvailable ? 2 : 1, updateType, 0, true ) {
}
inline GLint GetLocation() {
ShaderProgramDescriptor* p = _shader->GetProgram();
if ( !_shader->UseMaterialSystem() ) {
DAEMON_ASSERT_EQ( p, glState.currentProgram );
}
return p->uniformLocations[_locationIndex];
}
public:
void SetValueBindless( GLuint64 value ) {
if ( !glConfig.usingBindlessTextures ) {
return;
}
if ( !CacheValue( &value ) ) {
return;
}
glUniformHandleui64ARB( GetLocation(), value );
}
};
class GLUniformSampler2D : protected GLUniformSampler {
protected:
GLUniformSampler2D( GLShader* shader, const char* name, const UpdateType updateType ) :
GLUniformSampler( shader, name, "sampler2D", updateType ) {
}
};
class GLUniformSampler3D : protected GLUniformSampler {
protected:
GLUniformSampler3D( GLShader* shader, const char* name, const UpdateType updateType ) :
GLUniformSampler( shader, name, "sampler3D", updateType ) {
}
};
class GLUniformUSampler3D : protected GLUniformSampler {
protected:
GLUniformUSampler3D( GLShader* shader, const char* name, const UpdateType updateType ) :
GLUniformSampler( shader, name, "usampler3D", updateType ) {
}
};
class GLUniformSamplerCube : protected GLUniformSampler {
protected:
GLUniformSamplerCube( GLShader* shader, const char* name, const UpdateType updateType ) :
GLUniformSampler( shader, name, "samplerCube", updateType ) {
}
};
class GLUniform1i : protected GLUniform
{
protected:
GLUniform1i( GLShader *shader, const char *name, const UpdateType updateType ) :
GLUniform( shader, name, "int", 1, 1, updateType ) {
}
inline void SetValue( int value )
{
if ( !CacheValue( &value ) ) {
return;
}
ShaderProgramDescriptor* p = _shader->GetProgram();
glUniform1i( p->uniformLocations[ _locationIndex ], value );
}
};
class GLUniform1ui : protected GLUniform {
protected:
GLUniform1ui( GLShader* shader, const char* name, const UpdateType updateType ) :
GLUniform( shader, name, "uint", 1, 1, updateType ) {
}
inline void SetValue( uint value ) {
if ( !CacheValue( &value ) ) {
return;
}
ShaderProgramDescriptor* p = _shader->GetProgram();
glUniform1ui( p->uniformLocations[_locationIndex], value );
}
};
class GLUniform1Bool : protected GLUniform {
protected:
// GLSL std430 bool is always 4 bytes, which might not correspond to C++ bool
GLUniform1Bool( GLShader* shader, const char* name, const UpdateType updateType ) :
GLUniform( shader, name, "bool", 1, 1, updateType ) {
}
inline void SetValue( int value ) {
if ( !CacheValue( &value ) ) {
return;
}
ShaderProgramDescriptor* p = _shader->GetProgram();
glUniform1i( p->uniformLocations[_locationIndex], value );
}
};
class GLUniform1f : protected GLUniform
{
protected:
GLUniform1f( GLShader *shader, const char *name, const UpdateType updateType ) :
GLUniform( shader, name, "float", 1, 1, updateType ) {
}
inline void SetValue( float value )
{
if ( !CacheValue( &value ) ) {
return;
}
ShaderProgramDescriptor* p = _shader->GetProgram();
glUniform1f( p->uniformLocations[ _locationIndex ], value );
}
};
class GLUniform1fv : protected GLUniform
{
protected:
GLUniform1fv( GLShader *shader, const char *name, const int size, const UpdateType updateType ) :
GLUniform( shader, name, "float", 1, 1, updateType, size ) {
}
inline void SetValue( int numFloats, float *f )
{
if ( !CacheValue( f ) ) {
return;
}
ShaderProgramDescriptor* p = _shader->GetProgram();
glUniform1fv( p->uniformLocations[_locationIndex], numFloats, f );
}
};
class GLUniform2f : protected GLUniform
{
protected:
GLUniform2f( GLShader *shader, const char *name, const UpdateType updateType ) :
GLUniform( shader, name, "vec2", 2, 2, updateType ) {
}
inline void SetValue( const vec2_t v )
{
if ( !CacheValue( ( void* ) v ) ) {
return;
}
ShaderProgramDescriptor* p = _shader->GetProgram();
glUniform2f( p->uniformLocations[ _locationIndex ], v[ 0 ], v[ 1 ] );
}
};
class GLUniform3f : protected GLUniform
{
protected:
GLUniform3f( GLShader *shader, const char *name, const UpdateType updateType ) :
GLUniform( shader, name, "vec3", 3, 4, updateType ) {
}
inline void SetValue( const vec3_t v )
{
if ( !CacheValue( ( void* ) v ) ) {
return;
}
ShaderProgramDescriptor* p = _shader->GetProgram();
glUniform3f( p->uniformLocations[ _locationIndex ], v[ 0 ], v[ 1 ], v[ 2 ] );
}
};
class GLUniform4f : protected GLUniform
{
protected:
GLUniform4f( GLShader *shader, const char *name, const UpdateType updateType ) :
GLUniform( shader, name, "vec4", 4, 4, updateType ) {
}
inline void SetValue( const vec4_t v )
{
if ( !CacheValue( ( void* ) v ) ) {
return;
}
ShaderProgramDescriptor* p = _shader->GetProgram();
glUniform4f( p->uniformLocations[ _locationIndex ], v[ 0 ], v[ 1 ], v[ 2 ], v[ 3 ] );
}
};
class GLUniform4fv : protected GLUniform
{
protected:
GLUniform4fv( GLShader *shader, const char *name, const int size, const UpdateType updateType ) :
GLUniform( shader, name, "vec4", 4, 4, updateType, size ) {
}
inline void SetValue( int numV, vec4_t *v )
{
if ( !CacheValue( v ) ) {
return;
}
ShaderProgramDescriptor* p = _shader->GetProgram();
glUniform4fv( p->uniformLocations[ _locationIndex ], numV, &v[ 0 ][ 0 ] );
}
};
class GLUniformMatrix4f : protected GLUniform
{
protected:
GLUniformMatrix4f( GLShader *shader, const char *name, const UpdateType updateType ) :
GLUniform( shader, name, "mat4", 16, 4, updateType ) {
}
inline void SetValue( GLboolean transpose, const matrix_t m )
{
if ( !CacheValue( ( void* ) m ) ) {
return;
}
ShaderProgramDescriptor* p = _shader->GetProgram();
glUniformMatrix4fv( p->uniformLocations[ _locationIndex ], 1, transpose, m );
}
};
class GLUniformMatrix32f : protected GLUniform {
protected:
GLUniformMatrix32f( GLShader* shader, const char* name, const UpdateType updateType ) :
GLUniform( shader, name, "mat3x2", 12, 4, updateType ) {
}
inline void SetValue( GLboolean transpose, const vec_t* m ) {
vec_t value[12] {};
memcpy( value, m, 6 * sizeof( vec_t ) );
if ( !CacheValue( value ) ) {
return;
}
ShaderProgramDescriptor* p = _shader->GetProgram();
glUniformMatrix3x2fv( p->uniformLocations[_locationIndex], 1, transpose, m );
}
};
class GLUniformMatrix4fv : protected GLUniform
{
protected:
GLUniformMatrix4fv( GLShader *shader, const char *name, const int size, const UpdateType updateType ) :
GLUniform( shader, name, "mat4", 16, 4, updateType, size ) {
}
inline void SetValue( int numMatrices, GLboolean transpose, const matrix_t *m )
{
if ( !CacheValue( ( void* ) m ) ) {
return;
}
ShaderProgramDescriptor* p = _shader->GetProgram();
glUniformMatrix4fv( p->uniformLocations[ _locationIndex ], numMatrices, transpose, &m[ 0 ][ 0 ] );
}
};
class GLUniformMatrix34fv : protected GLUniform
{
protected:
GLUniformMatrix34fv( GLShader *shader, const char *name, const int size, const UpdateType updateType ) :
GLUniform( shader, name, "mat3x4", 12, 4, updateType, size )
{
}
inline void SetValue( int numMatrices, GLboolean transpose, const float *m )
{
if ( !CacheValue( ( void* ) m ) ) {
return;
}
ShaderProgramDescriptor* p = _shader->GetProgram();
glUniformMatrix3x4fv( p->uniformLocations[_locationIndex], numMatrices, transpose, m );
}
};
class GLUniformBlock
{
protected:
GLShader *_shader;
const std::string _name;
size_t _locationIndex; // Only valid if GL_ARB_shading_language_420pack is not available
const GLuint _bindingPoint; // Only valid if GL_ARB_shading_language_420pack is available
GLUniformBlock( GLShader *shader, const char *name, const GLuint bindingPoint ) :
_shader( shader ),
_name( name ),
_bindingPoint( bindingPoint )
{
_shader->RegisterUniformBlock( this );
}
public:
void SetLocationIndex( size_t index )
{
_locationIndex = index;
}
void UpdateShaderProgramUniformBlockIndex( ShaderProgramDescriptor* shaderProgram )
{
shaderProgram->uniformBlockIndexes[_locationIndex] = glGetUniformBlockIndex( shaderProgram->id, _name.c_str() );
}
void SetBuffer( GLuint buffer ) {
if ( glConfig.shadingLanguage420PackAvailable ) {
return;
}
ShaderProgramDescriptor *p = _shader->GetProgram();
GLuint blockIndex = p->uniformBlockIndexes[_locationIndex];
DAEMON_ASSERT_EQ( p, glState.currentProgram );
if( blockIndex != GL_INVALID_INDEX ) {
glBindBufferBase( GL_UNIFORM_BUFFER, blockIndex, buffer );
}
}
};
class GLCompileMacro
{
private:
int _bit;
protected:
GLShader *_shader;
GLCompileMacro( GLShader *shader ) :
_shader( shader )
{
_bit = BIT( _shader->GetNumOfCompiledMacros() );
_shader->RegisterCompileMacro( this );
}
// RB: This is not good oo design, but it can be a workaround and its cost is more or less only a virtual function call.
// It also works regardless of RTTI is enabled or not.
enum EGLCompileMacro : unsigned
{
OUTSIDE_FOG,
USE_BSP_SURFACE,
USE_VERTEX_SKINNING,
USE_VERTEX_ANIMATION,
USE_TCGEN_ENVIRONMENT,
USE_TCGEN_LIGHTMAP,
USE_DELUXE_MAPPING,
USE_GRID_LIGHTING,
USE_GRID_DELUXE_MAPPING,
USE_HEIGHTMAP_IN_NORMALMAP,
USE_RELIEF_MAPPING,
USE_REFLECTIVE_SPECULAR,
LIGHT_DIRECTIONAL,
USE_DEPTH_FADE,
USE_PHYSICAL_MAPPING,
};
public:
enum ShaderType {
VERTEX = BIT( 0 ),
FRAGMENT = BIT( 1 ),
COMPUTE = BIT( 2 )
};
virtual const char *GetName() const = 0;
virtual EGLCompileMacro GetType() const = 0;
virtual int GetShaderTypes() const = 0;
virtual bool HasConflictingMacros( size_t, const std::vector<GLCompileMacro*>& ) const
{
return false;
}
virtual uint32_t GetRequiredVertexAttributes() const
{
return 0;
}
void SetMacro( bool enable )
{
int bit = GetBit();
if ( enable && !_shader->IsMacroSet( bit ) )
{
_shader->AddMacroBit( bit );
}
else if ( !enable && _shader->IsMacroSet( bit ) )
{
_shader->DelMacroBit( bit );
}
// else do nothing because already enabled/disabled
}
public:
int GetBit() const
{
return _bit;
}
virtual ~GLCompileMacro() = default;
};
class GLCompileMacro_OUTSIDE_FOG :
GLCompileMacro
{
public:
GLCompileMacro_OUTSIDE_FOG( GLShader *shader ) :
GLCompileMacro( shader )
{
}
const char *GetName() const override
{
return "OUTSIDE_FOG";
}
EGLCompileMacro GetType() const override
{
return EGLCompileMacro::OUTSIDE_FOG;