Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
#include "Headers/glShaderProgram.h"

#include "Hardware/Video/OpenGL/glsw/Headers/glsw.h"

#include "Core/Headers/ParamHandler.h"
#include "Hardware/Video/Headers/GFXDevice.h"
#include "Hardware/Video/Shaders/Headers/Shader.h"
#include "Hardware/Video/Shaders/Headers/ShaderManager.h"

namespace Divide {

stringImpl glShaderProgram::_lastPathPrefix;
stringImpl glShaderProgram::_lastPathSuffix;

glShaderProgram::glShaderProgram(const bool optimise) : ShaderProgram(optimise),
                                                        _loadedFromBinary(false),
                                                        _validated(false),
                                                        _shaderProgramIDTemp(0)
{
	_validationQueued = false; 
    // each API has it's own invalid id. This is OpenGL's
    _shaderProgramId = GLUtil::_invalidObjectID;
    // some basic translation tables for shade types
    _shaderStageTable[VERTEX_SHADER]           = GL_VERTEX_SHADER;
    _shaderStageTable[FRAGMENT_SHADER]         = GL_FRAGMENT_SHADER;
    _shaderStageTable[GEOMETRY_SHADER]         = GL_GEOMETRY_SHADER;
    _shaderStageTable[TESSELATION_CTRL_SHADER] = GL_TESS_CONTROL_SHADER;
    _shaderStageTable[TESSELATION_EVAL_SHADER] = GL_TESS_EVALUATION_SHADER;
    _shaderStageTable[COMPUTE_SHADER]          = GL_COMPUTE_SHADER;
    // pointers to all of our shader stages
    _shaderStage[VERTEX_SHADER]           = nullptr;
    _shaderStage[FRAGMENT_SHADER]         = nullptr;
    _shaderStage[GEOMETRY_SHADER]         = nullptr;
    _shaderStage[TESSELATION_CTRL_SHADER] = nullptr;
    _shaderStage[TESSELATION_EVAL_SHADER] = nullptr;
    _shaderStage[COMPUTE_SHADER]          = nullptr;
}

glShaderProgram::~glShaderProgram()
{
    // remove shader stages
    for (ShaderIdMap::value_type& it : _shaderIdMap ) {
        detachShader( it.second );
    }
    // delete shader program
    if ( _shaderProgramId > 0 && _shaderProgramId != GLUtil::_invalidObjectID ) {
        glDeleteProgram( _shaderProgramId );
    }
}

/// Basic OpenGL shader program validation (both in debug and in release)
void glShaderProgram::validateInternal() {
    GLint status = 0;
    glValidateProgram(_shaderProgramId);
    glGetProgramiv(_shaderProgramId, GL_VALIDATE_STATUS, &status);

    // we print errors in debug and in release, but everything else only in debug
    // the validation log is only retrieved if we request it. (i.e. in release, if the shader is validated, it isn't retrieved)
    if (status == GL_FALSE){
        ERROR_FN(Locale::get("GLSL_VALIDATING_PROGRAM"), getName().c_str(), getLog().c_str());
    } else{
        D_PRINT_FN(Locale::get("GLSL_VALIDATING_PROGRAM"), getName().c_str(), getLog().c_str());
    }
    _validated = true;
}

/// Called once per frame. Used to update internal state
bool glShaderProgram::update(const U64 deltaTime){
    // If we haven't validated the program but used it at lease once ...
    if(_validationQueued){
        // Call the internal validation function
        validateInternal();
        // We dump the shader binary only if it wasn't loaded from one
        if (!_loadedFromBinary && GFX_DEVICE.getGPUVendor() == GPU_VENDOR_NVIDIA) {
            STUBBED("GLSL binary dump/load is only enabled for nVidia GPUS. "
                    "Catalyst 14.x destroys uniforms on shader dump, for whatever reason. - Ionut")
            // Get the size of the binary code
            GLint binaryLength = 0;
            glGetProgramiv(_shaderProgramId, GL_PROGRAM_BINARY_LENGTH, &binaryLength);
            // allocate a big enough buffer to hold it
            void* binary = (void*)malloc(binaryLength);
            DIVIDE_ASSERT(binary != NULL, 
                          "glShaderProgram error: could not allocate memory for the program binary!");
            // and fill the buffer with the binary code
            glGetProgramBinary(_shaderProgramId, binaryLength, NULL, &_binaryFormat, binary);
            // dump the buffer to file
            stringImpl outFileName("shaderCache/Binary/"+getName()+".bin");
            FILE* outFile = fopen(outFileName.c_str(), "wb");
            if (outFile != NULL) {
                fwrite(binary, binaryLength, 1, outFile);
                fclose(outFile);
            }
            // dump the format to a separate file (highly non-optimised. Should dump formats to a database instead)
            outFileName += ".fmt";
            outFile = fopen(outFileName.c_str(), "wb");
            if (outFile != NULL) {
                fwrite((void*)&_binaryFormat, sizeof(GLenum), 1, outFile);
                fclose(outFile);
            }
            // delete our local code buffer
            free(binary);
        }
        // clear validation queue flag
        _validationQueued = false;
    }

    // pass the update responsibility back to the parent class  
    return ShaderProgram::update(deltaTime);
}

/// Retrieve the program's validation log if we need it
stringImpl glShaderProgram::getLog() const {
    // We default to a simple OK message if the log is empty (hopefully, that means validation was successful, nVidia ... )
    stringImpl validationBuffer("[OK]");
    // Query the size of the log
    GLint length = 0;
    glGetProgramiv(_shaderProgramIDTemp, GL_INFO_LOG_LENGTH, &length);
    // If we actually have something in the validation log
    if (length > 1) {
        // Delete our OK string, and start on a new line
        validationBuffer = "\n -- ";
        // This little trick avoids a "NEW/DELETE" set of calls and still gives us a linear array of char's
        vectorImpl<char> shaderProgramLog(length);
        glGetProgramInfoLog(_shaderProgramIDTemp, length, NULL, &shaderProgramLog[0]);
        // Append the program's log to the output message
        validationBuffer.append(&shaderProgramLog[0]);
        // To avoid overflowing the output buffers (both CEGUI and Console), limit the maximum output size
        if (validationBuffer.size() > 4096 * 16) {
            // On some systems, the program's disassembly is printed, and that can get quite large
            validationBuffer.resize(static_cast<stringAlg::stringSize>(4096 * 
                                                                       16 - 
                                                                       strlen(Locale::get("GLSL_LINK_PROGRAM_LOG")) - 
                                                                       10));
            // Use the simple "truncate and inform user" system (a.k.a. add dots and delete the rest)
            validationBuffer.append(" ... ");
        }
    }
    // Return the final message, whatever it may contain
    return validationBuffer;
}

/// Remove a shader stage from this program
void glShaderProgram::detachShader(Shader* const shader) {
    DIVIDE_ASSERT(_threadedLoadComplete, 
                  "glShaderProgram error: tried to detach a shader from a program that didn't finish loading!");
    glDetachShader(_shaderProgramId, shader->getShaderId());
    shader->removeParentProgram(this);
}

/// Add a new shader stage to this program
void glShaderProgram::attachShader(Shader* const shader, const bool refresh){
    if (!shader) {
        return;
    }

    GLuint shaderId = shader->getShaderId();
    // If refresh == true, than we are re-attaching an existing shader (possibly after re-compilation)
    if (refresh) {
        // Find the previous iteration (and print an error if not found)
        ShaderIdMap::iterator it = _shaderIdMap.find(shaderId);
        if(it != _shaderIdMap.end()){
            // Update the internal pointer
            _shaderIdMap[shaderId] = shader;
            // and detach the shader
            detachShader(shader);
        }else{
            ERROR_FN(Locale::get("ERROR_SHADER_RECOMPILE_NOT_FOUND_ATOM"),shader->getName().c_str());
        }
    }else{
        // If refresh == false, we are adding a new stage
        hashAlg::emplace(_shaderIdMap, shaderId, shader);
    }

    // Attach the shader
    glAttachShader(_shaderProgramIDTemp, shaderId);
    // And register the program with the shader
    shader->addParentProgram(this);
    // Clear the 'linked' flag. Program must be re-linked before usage
    _linked = false;
}

/// This should be called in the loading thread, but some issues are still present, and it's not recommended (yet)
void glShaderProgram::threadedLoad(const stringImpl& name) {
    // Loading from binary gives us a linked program ready for usage.
    if (!_loadedFromBinary) {
        // If this wasn't loaded from binary, we need a new API specific object 
        // If we try to refresh the program, we already have a handle
        if (_shaderProgramId == GLUtil::_invalidObjectID) {
            _shaderProgramIDTemp = glCreateProgram();
        }
        // For every possible stage that the program might use
        for (U8 i = 0; i < ShaderType_PLACEHOLDER; ++i) {
            // Get the shader pointer for that stage
            Shader* shader = _shaderStage[i];
            // If a shader exists for said stage
            if (shader) {
                // Validate it
                shader->validate();
                // Attach it 
                attachShader(shader, _refreshStage[i]);
                // Clear the refresh flag for this stage
                _refreshStage[i] = false;
            }
        }
        // Link the program
        link();
    }
    // This was once an atomic swap. Might still be in the future
    _shaderProgramId = _shaderProgramIDTemp;
    // Clear texture<->binding map
    _textureSlots.clear();
    // Pass the rest of the loading steps to the parent class
    ShaderProgram::generateHWResource(name);
}

/// Linking a shader program also sets up all pre-link properties for the shader (varying locations, attrib bindings, etc)
void glShaderProgram::link(){
#   ifdef NDEBUG
    // Loading from binary is optional, but it using it does require sending the driver a hint to give us access to it later
    if (Config::USE_SHADER_BINARY) {
        glProgramParameteri(_shaderProgramIDTemp, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
    }
#   endif
    D_PRINT_FN(Locale::get("GLSL_LINK_PROGRAM"), getName().c_str(), _shaderProgramIDTemp);

    // If we require specific outputs from the shader for transform feedback, we need to set them up before linking
    if (_outputCount > 0) {
        // This isn't as optimised as it should/could be, but it works
        vectorImpl<const char* > vars;
        for (U32 i = 0; i < _outputCount; ++i) {
            vars.push_back(strdup(("outData" + Util::toString(i)).c_str()));
        }
        // Only separate attributes are supported for now. Interleaved not top prio
        glTransformFeedbackVaryings(_shaderProgramIDTemp, _outputCount, vars.data(), GL_SEPARATE_ATTRIBS);
    }
    // Link the program
    glLinkProgram(_shaderProgramIDTemp);
    // And check the result
    GLint linkStatus = 0;
    glGetProgramiv(_shaderProgramIDTemp, GL_LINK_STATUS, &linkStatus);
    // If linking failed, show an error, else print the result in debug builds. Same getLog() method is used
    if (linkStatus == GL_FALSE) {
        ERROR_FN(Locale::get("GLSL_LINK_PROGRAM_LOG"), getName().c_str(), getLog().c_str());
    } else {
        D_PRINT_FN(Locale::get("GLSL_LINK_PROGRAM_LOG"), getName().c_str(), getLog().c_str());
        // The linked flag is set to true only if linking succeeded
        _linked = true;
    }
}

/// Creation of a new shader program. Pass in a shader token and use glsw to load the corresponding effects
bool glShaderProgram::generateHWResource(const stringImpl& name) {
    _name = name;
    
    // NULL_SHADER shader means use shaderProgram(0), so bypass the normal loading routine
    if (name.compare("NULL_SHADER") == 0){
        _validationQueued = false;
        _shaderProgramId = 0;
        _threadedLoadComplete = HardwareResource::generateHWResource(name);
        return true;
    }

    // Reset the linked status of the program
    _linked = _loadedFromBinary = false;

    // Check if we need to refresh an existing program, or create a new one
    bool refresh = false;
    for (U8 i = 0; i < ShaderType_PLACEHOLDER; ++i) {
        if (_refreshStage[i]) {
            refresh = true;
            break;
        }
    }

#   ifdef NDEBUG
    // Load the program from the binary file, if available and allowed, to avoid linking.
    if (Config::USE_SHADER_BINARY && !refresh && GFX_DEVICE.getGPUVendor() == GPU_VENDOR_NVIDIA) {
        // Only available for new programs
        assert(_shaderProgramIDTemp == 0);
        stringImpl fileName("shaderCache/Binary/" + _name + ".bin");
        // Load the program's binary format from file
        FILE* inFile = fopen((fileName + ".fmt").c_str(), "wb");
        if (inFile) {
            fread(&_binaryFormat, sizeof(GLenum), 1, inFile);
            fclose(inFile);
            // If we loaded the binary format successfully, load the binary
            inFile = fopen(fileName.c_str(), "rb");
        } else {
            // If the binary format load failed, we don't need to load the binary code as it's useless without a proper format
            inFile = nullptr;
        }
        if (inFile) {
            // Jump to the end of the file
            fseek(inFile, 0, SEEK_END);
            // And get the file's content size
            GLint binaryLength = (GLint)ftell(inFile);
            // Allocate a sufficiently large local buffer to hold the contents
            void* binary = (void*)malloc(binaryLength);
            // Jump back to the start of the file
            fseek(inFile, 0, SEEK_SET);
            // Read the contents from the file and save them locally 
            fread(binary, binaryLength, 1, inFile);
            // Close the file
            fclose(inFile);
            // Allocate a new handle
            _shaderProgramIDTemp = glCreateProgram();
            // Load binary code on the GPU
            glProgramBinary(_shaderProgramIDTemp, _binaryFormat, binary, binaryLength);
            // Delete the local binary code buffer
            free(binary);
            // Check if the program linked successfully on load
            GLint success = 0;
            glGetProgramiv(_shaderProgramIDTemp, GL_LINK_STATUS, &success);
            // If it loaded properly set all appropriate flags (this also prevents low level access to the program's shaders)
            if (success == GL_TRUE) {
                _loadedFromBinary = _linked = true;
                _threadedLoading = false;
            }
        }
    }
#   endif
    // The program wasn't loaded from binary, so process shaders
    if (!_loadedFromBinary) {
		bool updatePath = false;
        // Use the specified shader path
		if (_lastPathPrefix.compare(getResourceLocation() + "GLSL/") != 0) {
			_lastPathPrefix = getResourceLocation() + "GLSL/";
			updatePath = true;
		}
		if (_lastPathSuffix.compare(".glsl") != 0) {
			_lastPathSuffix = ".glsl";
			updatePath = true;
		}
		if (updatePath){
			glswSetPath(_lastPathPrefix.c_str(), _lastPathSuffix.c_str());
		}
        // Mirror initial shader defines to match line count
        GLint initialOffset = 20;
        if (GFX_DEVICE.getGPUVendor() == GPU_VENDOR_NVIDIA) { //nVidia specific
            initialOffset += 6;
        }
        // Get all of the preprocessor defines and add them to the general shader header for this program
        stringImpl shaderSourceHeader;
        for (U8 i = 0; i < _definesList.size(); ++i) {
            // Placeholders are ignored
            if (_definesList[i].compare("DEFINE_PLACEHOLDER") == 0) {
                continue;
            }
            // We manually add define dressing
            shaderSourceHeader.append("#define " + _definesList[i] + "\n" );
            // We also take in consideration any line count offset that this causes
            initialOffset++;
        }
        // Now that we have an offset for the general header, we need to move to per-stage offsets
        GLint lineCountOffset[ShaderType_PLACEHOLDER];
        // Every stage has it's own uniform specific header
        stringImpl shaderSourceUniforms[ShaderType_PLACEHOLDER];
        for (U8 i = 0; i < ShaderType_PLACEHOLDER; ++i) {
            // We start off from the general offset
            lineCountOffset[i] = initialOffset;
            // And add every custom uniform specified (yet again, we add all the needed dressing)
            for (U8 j = 0; j < _customUniforms[i].size(); ++j) {
                shaderSourceUniforms[i].append("uniform " + _customUniforms[i][j] + ";\n");
                lineCountOffset[i]++;
            }
            // We also add a custom include to all shaders that contains GPU data buffers. We need to account for its size as well
            lineCountOffset[i] += 42;
        }
        // GLSW directives are accounted here
        lineCountOffset[VERTEX_SHADER]   += 18;
        lineCountOffset[FRAGMENT_SHADER] += 10;

        // Split the shader name to get the effect file name and the effect properties
        // The effect file name is the part up until the first period or comma symbol
        stringImpl shaderName = name.substr(0, name.find_first_of(".,"));
        // We also differentiate between general properties, and vertex properties
        stringImpl shaderProperties, vertexProperties;
        // Get the position of the first "," symbol. Must be added at the end of the program's name!!
        stringAlg::stringSize propPositionVertex = name.find_first_of(",");
        // Get the position of the first "." symbol
        stringAlg::stringSize propPosition = name.find_first_of(".");
        // If we have effect properties, we extract them from the name 
        // (starting from the first "." symbol to the first "," symbol)
        if (propPosition != stringImpl::npos) {
            shaderProperties = "." + name.substr(propPosition + 1, propPositionVertex - propPosition - 1);
        }
        // Vertex properties start off identically to the rest of the stages' names
        vertexProperties += shaderProperties;
        // But we also add the shader specific properties
        if (propPositionVertex != stringImpl::npos) {
            vertexProperties += "." + name.substr(propPositionVertex + 1);
        }

        // Create an appropriate name for every shader stage
        stringImpl shaderCompileName[ShaderType_PLACEHOLDER];
        shaderCompileName[VERTEX_SHADER]           = shaderName + ".Vertex"        + vertexProperties;
        shaderCompileName[FRAGMENT_SHADER]         = shaderName + ".Fragment"      + shaderProperties;
        shaderCompileName[GEOMETRY_SHADER]         = shaderName + ".Geometry"      + shaderProperties;
        shaderCompileName[TESSELATION_CTRL_SHADER] = shaderName + ".TessellationC" + shaderProperties;
        shaderCompileName[TESSELATION_EVAL_SHADER] = shaderName + ".TessellationE" + shaderProperties;
        shaderCompileName[COMPUTE_SHADER]          = shaderName + ".Compute"       + shaderProperties;
    
        // For every stage
        for (U8 i = 0; i < ShaderType_PLACEHOLDER; ++i) {
            // Brute force conversion to an enum
            ShaderType type = (ShaderType)(i);

            // If we request a refresh for the current stage, we need to have a pointer for the stage's shader already
            if (!_refreshStage[type]) {
                // Else, we ask the shader manager to see if it was previously loaded elsewhere
                _shaderStage[type] = ShaderManager::getInstance().getShader(shaderCompileName[type], refresh);
            }

            // If this is the first time this shader is loaded ...
            if (!_shaderStage[type]){
                // Use GLSW to read the appropriate part of the effect file based on the specified stage and properties
                const char* sourceCode = glswGetShader(shaderCompileName[type].c_str(), 
                                                       lineCountOffset[type], 
                                                       _refreshStage[type]);
                // GLSW may fail for various reasons (not a valid effect stage, invalid name, etc)
                if (sourceCode) {
                    // If reading was successful, grab the entire code in a string
                    stringImpl codeString(sourceCode);
                    // And replace in place with our program's headers created earlier
                    Util::replaceStringInPlace(codeString, "//__CUSTOM_DEFINES__",  shaderSourceHeader);
                    Util::replaceStringInPlace(codeString, "//__CUSTOM_UNIFORMS__", shaderSourceUniforms[type]);
                    // Load our shader from the final string and save it in the manager in case a new Shader Program needs it
                    _shaderStage[type] = ShaderManager::getInstance().loadShader(shaderCompileName[type],
                                                                                 codeString, 
                                                                                 type, 
                                                                                 _refreshStage[type]);
                }
            }
            // Show a message, in debug, if we don't have a shader for this stage
            if (!_shaderStage[type]) { 
                D_PRINT_FN(Locale::get("WARN_GLSL_SHADER_LOAD"), shaderCompileName[type].c_str())
            } else {
                // Try to compile the shader (it doesn't double compile shaders, so it's safe to call it multiple types)
                if (!_shaderStage[type]->compile()) {
                    ERROR_FN(Locale::get("ERROR_GLSL_SHADER_COMPILE"), _shaderStage[type]->getShaderId());
                }
            }
        }   
    }

    // try to link the program in a separate thread
    return GFX_DEVICE.loadInContext(/*_threadedLoading && !_loadedFromBinary ? GFX_LOADING_CONTEXT : */
                                                                               GFX_RENDERING_CONTEXT, 
                                    DELEGATE_BIND(&glShaderProgram::threadedLoad, this, name));
}
    
/// Check every possible combination of flags to make sure this program can be used for rendering
bool glShaderProgram::isValid() const {
    return isHWInitComplete() && 
           _linked &&
           _shaderProgramId != 0 &&
           _shaderProgramId != GLUtil::_invalidObjectID;
}

/// Cache uniform/attribute locations for shader programs
/// When we call this function, we check our name<->address map to see if we queried the location before
/// If we didn't, ask the GPU to give us the variables address and save it for later use
GLint glShaderProgram::cachedLoc(const stringImpl& name, const bool uniform) {
    // If the shader can't be used for rendering, just return an invalid address
    if (!isValid()) {
        return -1;
    }

    DIVIDE_ASSERT(_threadedLoadComplete, 
                  "glShaderProgram error: tried to query a shader program before threaded load completed!");

    // Check the cache for the location
    ShaderVarMap::const_iterator it = _shaderVars.find(name);
    if (it != _shaderVars.end()) {
    	return it->second;
    }

    // Cache miss. Query OpenGL for the location
    GLint location = uniform ? glGetUniformLocation(_shaderProgramId, name.c_str()) :
                               glGetAttribLocation(_shaderProgramId, name.c_str());

    // Save it for later reference
    hashAlg::emplace(_shaderVars, name, location);

    // Return the location
    return location;
}

/// Bind this shader program
bool glShaderProgram::bind() {
    // Prevent double bind    
    if (_bound) {
        return true;
    }
    // If the shader isn't ready or failed to link, stop here
    if (!isValid()) {
        return false;
    }
    // Set this program as the currently active one
    GL_API::setActiveProgram(this);
    // Pass the rest of the binding responsibilities to the parent class
    return ShaderProgram::bind();
}

/// Unbinding this program, unless forced, just clears the _bound flag
void glShaderProgram::unbind(bool resetActiveProgram) {
    // Prevent double unbind
    if (!_bound) {
        return;
    }
    // After using the shader at least once, validate the shader if needed
    if (!_validated) {
        _validationQueued = isValid();
    }
    // If forced to do so, we can clear OpenGL's active program object
    if (resetActiveProgram) {
        GL_API::setActiveProgram(nullptr);
    }
    // Pass the rest of the unbind responsibilities to the parent class
    ShaderProgram::unbind(resetActiveProgram);
}

/// This is used to set all of the subroutine indices for the specified shader stage for this program
void glShaderProgram::SetSubroutines(ShaderType type, const vectorImpl<U32>& indices) const {
    // The shader must be bound before calling this!
    DIVIDE_ASSERT(_bound && isValid(), "glShaderProgram error: tried to set subroutines on an unbound or unlinked program!");
    // Validate data and send to GPU
    if (!indices.empty() && indices[0] != GLUtil::_invalidObjectID) {
        glUniformSubroutinesuiv(_shaderStageTable[type], (GLsizei)indices.size(), indices.data());
    }
    
}
/// This works exactly like SetSubroutines, but for a single index. 
/// If the shader has multiple subroutine uniforms, this will reset the rest!!!
void glShaderProgram::SetSubroutine(ShaderType type, U32 index) const {
    DIVIDE_ASSERT(_bound && isValid(), "glShaderProgram error: tried to set subroutines on an unbound or unlinked program!");

    if (index != GLUtil::_invalidObjectID) {
        U32 value[] = {index};
        glUniformSubroutinesuiv(_shaderStageTable[type], 1, value);
    }
}

/// Returns the number of subroutine uniforms for the specified shader stage
U32 glShaderProgram::GetSubroutineUniformCount(ShaderType type) const {
    DIVIDE_ASSERT(isValid(), "glShaderProgram error: tried to query subroutines on an invalid program!");
    
    I32 subroutineCount = 0;
    glGetProgramStageiv(_shaderProgramId, _shaderStageTable[type], GL_ACTIVE_SUBROUTINE_UNIFORMS, &subroutineCount);

    return std::max(subroutineCount, 0);
}
/// Get the uniform location of the specified subroutine uniform for the specified stage. Not cached!
U32 glShaderProgram::GetSubroutineUniformLocation(ShaderType type, const stringImpl& name) const {
    DIVIDE_ASSERT(isValid(), "glShaderProgram error: tried to query subroutines on an invalid program!");

    return glGetSubroutineUniformLocation(_shaderProgramId, _shaderStageTable[type], name.c_str());
}

/// Get the index of the specified subroutine name for the specified stage. Not cached!
U32 glShaderProgram::GetSubroutineIndex(ShaderType type, const stringImpl& name) const {
    DIVIDE_ASSERT(isValid(), "glShaderProgram error: tried to query subroutines on an invalid program!");

    return glGetSubroutineIndex(_shaderProgramId, _shaderStageTable[type], name.c_str());
}

/// Set an attribute value
void glShaderProgram::Attribute(I32 location, GLdouble value) const {
    if (location == -1) {
        return;
    }

    glVertexAttrib1d(location,value);
}

/// Set an attribute value
void glShaderProgram::Attribute(I32 location, GLfloat value) const {
    if (location == -1) {
        return;
    }

    glVertexAttrib1f(location,value);
}

/// Set an attribute value
void glShaderProgram::Attribute(I32 location, const vec2<GLfloat>& value) const {
    if (location == -1) {
        return;
    }

    glVertexAttrib2fv(location,value);
}

/// Set an attribute value
void glShaderProgram::Attribute(I32 location, const vec3<GLfloat>& value) const {
    if (location == -1) {
        return;
    }

    glVertexAttrib3fv(location,value);
}

/// Set an attribute value
void glShaderProgram::Attribute(I32 location, const vec4<GLfloat>& value) const {
    if (location == -1) {
        return;
    }

    glVertexAttrib4fv(location,value);
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, GLuint value) const {
    if (location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniform1ui(_shaderProgramId, location, value);
    } else {
        glUniform1ui(location, value);
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, GLint value) const {
    if (location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniform1i(_shaderProgramId, location, value);
    } else {
        glUniform1i(location, value);
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, GLfloat value) const {
    if (location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniform1f(_shaderProgramId, location, value);
    } else {
        glUniform1f(location, value);
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, const vec2<GLfloat>& value) const {
    if (location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniform2fv(_shaderProgramId, location, 1, value);
    } else {
        glUniform2fv(location, 1, value);
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, const vec2<GLint>& value) const {
    if (location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniform2iv(_shaderProgramId, location, 1, value);
    } else {
        glUniform2iv(location, 1, value);
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, const vec2<GLushort>& value) const {
    if (location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniform2iv(_shaderProgramId, location, 1, vec2<I32>(value.x, value.y));
    } else {
        glUniform2iv(location, 1, vec2<I32>(value.x, value.y));
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, const vec3<GLfloat>& value) const {
    if (location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniform3fv(_shaderProgramId, location, 1, value);
    } else {
        glUniform3fv(location, 1, value);
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, const vec4<GLfloat>& value) const {
    if (location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniform4fv(_shaderProgramId, location, 1, value);
    } else {
        glUniform4fv(location, 1, value);
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, const mat3<GLfloat>& value, bool rowMajor) const {
    if (location == -1) {
        return;
    }

    if (!_bound) { 
        glProgramUniformMatrix3fv(_shaderProgramId, location, 1, rowMajor, value.mat);
    } else {
        glUniformMatrix3fv(location, 1, rowMajor, value);
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, const mat4<GLfloat>& value, bool rowMajor) const {
   if (location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniformMatrix4fv(_shaderProgramId, location, 1, rowMajor, value.mat);
    } else {
        glUniformMatrix4fv(location, 1, rowMajor, value.mat);
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, const vectorImpl<GLint >& values) const {
    if (values.empty()) {
        return;
    }

    if (!_bound) {
        glProgramUniform1iv(_shaderProgramId, location, (GLsizei)values.size(), values.data());
    } else {
        glUniform1iv(location, (GLsizei)values.size(), values.data());
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, const vectorImpl<GLfloat >& values) const {
    if (values.empty() || location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniform1fv(_shaderProgramId, location, (GLsizei)values.size(), values.data());
    } else {
        glUniform1fv(location, (GLsizei)values.size(), values.data());
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, const vectorImpl<vec2<GLfloat> >& values) const {
    if (values.empty() || location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniform2fv(_shaderProgramId, location, (GLsizei)values.size(), values.front());
    } else {
        glUniform2fv(location, (GLsizei)values.size(), values.front());
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, const vectorImpl<vec3<GLfloat> >& values) const {
    if (values.empty() || location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniform3fv(_shaderProgramId, location, (GLsizei)values.size(), values.front());
    } else {
        glUniform3fv(location, (GLsizei)values.size(), values.front());
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, const vectorImpl<vec4<GLfloat> >& values) const {
    if (values.empty() || location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniform4fv(_shaderProgramId, location, (GLsizei)values.size(), values.front());
    } else {
        glUniform4fv(location, (GLsizei)values.size(), values.front());
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, const vectorImpl<mat3<F32> >& values, bool rowMajor) const {
    if (values.empty() || location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniformMatrix3fv(_shaderProgramId, location, (GLsizei)values.size(), rowMajor, values.front());
    } else {
        glUniformMatrix3fv(location, (GLsizei)values.size(), rowMajor, values.front());
    }
}

/// Set an uniform value
void glShaderProgram::Uniform(GLint location, const vectorImpl<mat4<GLfloat> >& values, bool rowMajor) const {
    if (values.empty() || location == -1) {
        return;
    }

    if (!_bound) {
        glProgramUniformMatrix4fv(_shaderProgramId, location, (GLsizei)values.size(), rowMajor, values.front());
    } else {
        glUniformMatrix4fv(location, (GLsizei)values.size(), rowMajor, values.front());
    }
}

/// Bind a sampler to specific texture unit, checking double bind to avoid programming errors in debug
void glShaderProgram::UniformTexture(GLint location, GLushort slot) {
    if (location == -1 || !checkSlotUsage(location, slot)) {
        return;
    }

    if(!_bound) {
        glProgramUniform1i(_shaderProgramId, location, slot);
    } else {
        glUniform1i(location, slot);
    }
}

/// If a slot is assigned to multiple locations, prevent binding it and print an error. Only in Debug builds!
bool glShaderProgram::checkSlotUsage(GLint location, GLushort slot) {
#ifdef _DEBUG
    const TextureSlotMap::const_iterator& it = _textureSlots.find(slot);
    if (it == _textureSlots.end() || it->second == location){
        _textureSlots[slot] = location;
        return true;
    }
    ERROR_FN(Locale::get("ERROR_SHADER_TEXTURE_MULTI_BIND"));
    return false;
#else
    return true;
#endif
}

};

Commits for Divide-Framework/trunk/Source Code/Hardware/Video/OpenGL/Shaders/glShaderProgram.cpp

Diff revisions: vs.
Revision Author Commited Message
331 Diff Diff IonutCava picture IonutCava Sat 06 Dec, 2014 20:53:45 +0000

[Ionut]
- Limited line length to 132 characters to improve readability and diff-comparisons
- Refactored memory allocation/deallocation functions
- Fixed a few compatibility issues with HashMap.h
- Fixed a bug in GPU Skinning shaders (cast a float to int)

330 Diff Diff IonutCava picture IonutCava Fri 10 Oct, 2014 17:19:11 +0000

[Ionut]
- New rendering algorithm :
— Perform second pass for visible nodes gathering all render commands and uploading them to an indirect draw buffer
— Render geometry with indirect draw commands
— Removed per-node range binding of the node buffer. Instead, bind the entire buffer once and index data in shaders
— Use “baseInstance” parameter from IndirectDrawCommand as a node buffer index
— Prefer DSA extensions for updating buffer data where applicable
- Moved all rendering specific code from SceneGraphNode to a new RenderingComponent
- Optimized “toString” utility function

326 Diff Diff IonutCava picture IonutCava Tue 30 Sep, 2014 21:11:32 +0000

[Ionut]
- Fixed more memory leaks
- Simplified Task interface and timing System
- Improved compatibility between Boost, STL and EASTL with random combinations of vectors, strings and maps
- Simplified Light class by removing the “slot” member
- Random optimizations

325 Diff Diff IonutCava picture IonutCava Wed 17 Sep, 2014 20:06:13 +0000

[Ionut]
- Reworked transform system:
— Parent transform chain system moved from Transform class to the PhysicsComponent
— PhysicsComponent now returns all transform values needed both global and local (still needs heavy optimization and caching)

- Reworked SceneGraph node management:
— Proper ref counting of SceneNodes and proper removing of resource (ResourceCache is now empty on destruction, as it should be)
— Removed parent transform tracking as that’s the PhysicsComponent’s responsibility
— Only nodes loaded via the ResourceCache are allowed to be added to the SceneGraph (added proper loaders for Impostors, Sky, etc)

- Optimized some of the math classes (mat4, mat3)

322 Diff Diff IonutCava picture IonutCava Sat 06 Sep, 2014 20:33:47 +0000

[Ionut]
- Refactored most of Boost related code to be C++11 based
— Boost is only used for ThreadPool, Mutex handling, XML parsing and networking (ASIO)
— Function binds, threads, regex, NonCopyable base, Hashing algorithms and more are now using C++11 libraries
- Replaced all FOR_EACH calls with standard, range, “for” calls

321 Diff Diff IonutCava picture IonutCava Wed 03 Sep, 2014 22:05:15 +0000

[Ionut]
- Added support (and enabled by default) for the EASTL library (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html / https://github.com/paulhodge/EASTL)
— Strings, vectors and hash_maps can be combined from EASTL, STL and Boost (not all combinations work. e.g. EASTL strings with STL/Boost containers due to the lack of a proper hash function)

319 Diff Diff IonutCava picture IonutCava Sat 30 Aug, 2014 21:37:57 +0000

[Ionut]
- Performance tweaks based on profiling data

318 Diff Diff IonutCava picture IonutCava Sat 30 Aug, 2014 17:35:53 +0000

[Ionut]
- Wrapped the entire code in a “Divide” namespace
- VertexBuffers now call “shrink_to_fit” on all internal data storage
- Improved some vector performance by preferring “emplace_back” instead of “push_back” + proepr usage of reserve / resize
- Wrapped OIS specific types and classes in Divide::Input namespace
- Added the messageBox.layout file (forgot it in the previous few commits)

302 Diff Diff IonutCava picture IonutCava Wed 06 Aug, 2014 22:09:55 +0000

[Ionut]
- GenericVertexData buffer size (for read/write buffering) is now configurable instead of a fixed 3x and also applies to non-persistently mapped buffers
- Moved OpenGL enum tables to Divide::GLUtil::GL_ENUM_TABLE
- More code cleanups (glResources, glError, glVertexArray, glGenericVertexData, glIMPrimitive)

301 IonutCava picture IonutCava Tue 05 Aug, 2014 20:55:30 +0000

[Ionut]
- Code cleanups and comments (ShaderManager, ShaderProgram, Texture)
- Fixed some singleton destruction order between ResourceCache and ShaderManager to properly unload remaining shader programs