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
#include "stdafx.h"

#include "Headers/glShader.h"

#include "Platform/File/Headers/FileManagement.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/Video/OpenGL/Headers/GLWrapper.h"
#include "Platform/Video/OpenGL/Headers/glResources.h"

#include "Core/Headers/Console.h"
#include "Core/Headers/StringHelper.h"
#include "Utility/Headers/Localization.h"

namespace Divide {

SharedLock glShader::_shaderNameLock;
glShader::ShaderMap glShader::_shaderNameMap;
stringImpl glShader::shaderAtomLocationPrefix[to_base(ShaderType::COUNT) + 1];

IMPLEMENT_CUSTOM_ALLOCATOR(glShader, 0, 0);

void glShader::initStaticData() {
    stringImpl locPrefix(Paths::g_assetsLocation + Paths::g_shadersLocation + Paths::Shaders::GLSL::g_parentShaderLoc);

    shaderAtomLocationPrefix[to_base(ShaderType::FRAGMENT)] = locPrefix + Paths::Shaders::GLSL::g_fragAtomLoc;
    shaderAtomLocationPrefix[to_base(ShaderType::VERTEX)] = locPrefix + Paths::Shaders::GLSL::g_vertAtomLoc;
    shaderAtomLocationPrefix[to_base(ShaderType::GEOMETRY)] = locPrefix + Paths::Shaders::GLSL::g_geomAtomLoc;
    shaderAtomLocationPrefix[to_base(ShaderType::TESSELATION_CTRL)] = locPrefix + Paths::Shaders::GLSL::g_tescAtomLoc;
    shaderAtomLocationPrefix[to_base(ShaderType::TESSELATION_EVAL)] = locPrefix + Paths::Shaders::GLSL::g_teseAtomLoc;
    shaderAtomLocationPrefix[to_base(ShaderType::COMPUTE)] = locPrefix + Paths::Shaders::GLSL::g_compAtomLoc;
    shaderAtomLocationPrefix[to_base(ShaderType::COUNT)] = locPrefix + Paths::Shaders::GLSL::g_comnAtomLoc;
}

void glShader::destroyStaticData() {

}

glShader::glShader(GFXDevice& context,
                   const stringImpl& name,
                   const ShaderType& type,
                   const bool optimise)
    : TrackedObject(),
      GraphicsResource(context, GraphicsResource::Type::SHADER, getGUID()),
      glObject(glObjectType::TYPE_SHADER, context),
     _skipIncludes(false),
     _shader(std::numeric_limits<U32>::max()),
     _name(name),
     _type(type)
{
    _compiled = false;

    switch (type) {
        default:
            Console::errorfn(Locale::get(_ID("ERROR_GLSL_UNKNOWN_ShaderType")), type);
            break;
        case ShaderType::VERTEX:
            _shader = glCreateShader(GL_VERTEX_SHADER);
            break;
        case ShaderType::FRAGMENT:
            _shader = glCreateShader(GL_FRAGMENT_SHADER);
            break;
        case ShaderType::GEOMETRY:
            _shader = glCreateShader(GL_GEOMETRY_SHADER);
            break;
        case ShaderType::TESSELATION_CTRL:
            _shader = glCreateShader(GL_TESS_CONTROL_SHADER);
            break;
        case ShaderType::TESSELATION_EVAL:
            _shader = glCreateShader(GL_TESS_EVALUATION_SHADER);
            break;
        case ShaderType::COMPUTE:
            _shader = glCreateShader(GL_COMPUTE_SHADER);
            break;
    };
}

glShader::~glShader() {
    Console::d_printfn(Locale::get(_ID("SHADER_DELETE")), getName().c_str());
    glDeleteShader(_shader);
}

bool glShader::load(const stringImpl& source, U32 lineOffset) {
    if (source.empty()) {
        Console::errorfn(Locale::get(_ID("ERROR_GLSL_NOT_FOUND")), getName().c_str());
        return false;
    }

    _usedAtoms.clear();
    stringImpl parsedSource = _skipIncludes ? source
                                            : preprocessIncludes(getName(), source, 0, _usedAtoms);

    Util::ReplaceStringInPlace(parsedSource, "//__LINE_OFFSET_",
                               Util::StringFormat("#line %d", lineOffset));

    const char* src = parsedSource.c_str();

    GLsizei sourceLength = (GLsizei)parsedSource.length();
    glShaderSource(_shader, 1, &src, &sourceLength);

    if (!_skipIncludes) {
        ShaderProgram::shaderFileWrite(Paths::g_cacheLocation + Paths::Shaders::g_cacheLocationText + getName(), src);
    }

    _compiled = false;
    return true;
}

bool glShader::compile() {
    if (_compiled) {
        return true;
    }

    glCompileShader(_shader);
    _compiled = true;
    if (Config::ENABLE_GPU_VALIDATION) {
        glObjectLabel(GL_SHADER, _shader, -1, getName().c_str());
    }
    return validate();
}

bool glShader::validate() {
    if (Config::ENABLE_GPU_VALIDATION) {
        GLint length = 0, status = 0;

        glGetShaderiv(_shader, GL_COMPILE_STATUS, &status);
        glGetShaderiv(_shader, GL_INFO_LOG_LENGTH, &length);
        if (length <= 1) {
            return true;
        }
        vectorImpl<char> shaderLog(length);
        glGetShaderInfoLog(_shader, length, NULL, &shaderLog[0]);
        shaderLog.push_back('\n');
        if (status == 0) {
            Console::errorfn(Locale::get(_ID("GLSL_VALIDATING_SHADER")), _shader, _name.c_str(), &shaderLog[0]);
            return false;
        } else {
            Console::d_printfn(Locale::get(_ID("GLSL_VALIDATING_SHADER")), _shader, _name.c_str(), &shaderLog[0]);
            return true;
        }
    }

    return true;
}

stringImpl glShader::preprocessIncludes(const stringImpl& name,
                                        const stringImpl& source,
                                        GLint level,
                                        vectorImpl<stringImpl>& foundAtoms) {
    if (level > 32) {
        Console::errorfn(Locale::get(_ID("ERROR_GLSL_INCLUD_LIMIT")));
    }

    size_t line_number = 1;
    std::smatch matches;

    stringImpl output, line;
    stringImpl include_file, include_string;

    istringstreamImpl input(source);

    while (std::getline(input, line)) {
        if (std::regex_search(line, matches, Paths::g_includePattern)) {
            include_file = Util::Trim(matches[1].str().c_str());
            foundAtoms.push_back(include_file);

            ShaderType typeIndex = ShaderType::COUNT;
            // switch will throw warnings due to promotion to int
            U64 extHash = _ID_RT(Util::GetTrailingCharacters(include_file, 4));
            if (extHash == _ID_RT(Paths::Shaders::GLSL::g_fragAtomExt)) {
                typeIndex = ShaderType::FRAGMENT;
            } else if (extHash == _ID_RT(Paths::Shaders::GLSL::g_vertAtomExt)){
                typeIndex = ShaderType::VERTEX;
            } else if (extHash == _ID_RT(Paths::Shaders::GLSL::g_geomAtomExt)) {
                typeIndex = ShaderType::GEOMETRY;
            } else if (extHash == _ID_RT(Paths::Shaders::GLSL::g_tescAtomExt)) {
                typeIndex = ShaderType::TESSELATION_CTRL;
            } else if (extHash == _ID_RT(Paths::Shaders::GLSL::g_teseAtomExt)) {
                typeIndex = ShaderType::TESSELATION_EVAL;
            } else if (extHash == _ID_RT(Paths::Shaders::GLSL::g_compAtomExt)) {
                typeIndex = ShaderType::COMPUTE;
            } else if (extHash == _ID_RT(Paths::Shaders::GLSL::g_comnAtomExt)) {
                typeIndex = ShaderType::COUNT;
            } else {
                DIVIDE_UNEXPECTED_CALL("Invalid shader include type");
            }

            include_string = ShaderProgram::shaderFileRead(include_file, shaderAtomLocationPrefix[to_U32(typeIndex)]);
            if (include_string.empty()) {
                Console::errorfn(Locale::get(_ID("ERROR_GLSL_NO_INCLUDE_FILE")),
                                 name.c_str(),
                                 line_number,
                                 include_file.c_str());
            }

            output.append(preprocessIncludes(name, include_string, level + 1, foundAtoms));
        } else {
            output.append(line);
        }
        output.append("\n");
        ++line_number;
    }

    return output;
}

// ============================ static data =========================== //
/// Remove a shader entity. The shader is deleted only if it isn't referenced by a program
void glShader::removeShader(glShader* s) {
    // Keep a copy of it's name
    stringImpl name(s->getName());
    // Try to find it
    U64 nameHash = _ID_RT(name);
    UpgradableReadLock ur_lock(_shaderNameLock);
    ShaderMap::iterator it = _shaderNameMap.find(nameHash);
    if (it != std::end(_shaderNameMap)) {
        // Subtract one reference from it.
        if (s->SubRef()) {
            // If the new reference count is 0, delete the shader
            MemoryManager::DELETE(it->second);
            UpgradeToWriteLock w_lock(ur_lock);
            _shaderNameMap.erase(nameHash);
        }
    }
}

/// Return a new shader reference
glShader* glShader::getShader(const stringImpl& name) {
    // Try to find the shader
    ReadLock r_lock(_shaderNameLock);
    ShaderMap::iterator it = _shaderNameMap.find(_ID_RT(name));
    if (it != std::end(_shaderNameMap)) {
        return it->second;
    }

    return nullptr;
}

/// Load a shader by name, source code and stage
glShader* glShader::loadShader(GFXDevice& context,
                               const stringImpl& name,
                               const stringImpl& source,
                               const ShaderType& type,
                               const bool parseCode,
                               U32 lineOffset) {
    // See if we have the shader already loaded
    glShader* shader = getShader(name);
    
    bool newShader = false;
    // If we do, and don't need a recompile, just return it
    if (shader == nullptr) {
        // If we can't find it, we create a new one
        shader = MemoryManager_NEW glShader(context, name, type, false);
        newShader = true;
    }

    shader->skipIncludes(!parseCode);
    // At this stage, we have a valid Shader object, so load the source code
    if (!shader->load(source, lineOffset)) {
        // If loading the source code failed, delete it
        if (newShader) {
            MemoryManager::DELETE(shader);
        }
    } else {
        if (newShader) {
            U64 nameHash = _ID_RT(name);
            // If we loaded the source code successfully,  register it
            WriteLock w_lock(_shaderNameLock);
            hashAlg::insert(_shaderNameMap, nameHash, shader);
        }
    }

    return shader;
}
};

Commits for Divide-Framework/trunk/Source Code/Platform/Video/OpenGL/Shaders/glShader.cpp

Diff revisions: vs.
Revision Author Commited Message
1018 Diff Diff IonutCava picture IonutCava Wed 17 Jan, 2018 16:45:26 +0000

[Ionut]
- Rework caching mechanism:
— Shaders, Geometry and Terrain (and Materials soon) are cached in the cache folder in their respective sub-folders

996 Diff Diff IonutCava picture IonutCava Tue 12 Dec, 2017 17:16:54 +0000

[Ionut]
- Update and improve SGN creation and destruction
- Fix cleanup issues
- Fix GPU object memory leaks
- Fix duplicate GUID values being generated
- Misc cleanups and optimizations

955 Diff Diff IonutCava picture IonutCava Sun 29 Oct, 2017 20:45:04 +0000

[Ionut]
- Modify the CEGUI opengl renderer to interact with the state in GLWrapper better
- Performance optimizations

923 Diff Diff IonutCava picture IonutCava Tue 01 Aug, 2017 22:23:24 +0000

[Ionut]
- Add (hack) new GL 4.6 KHR_NO_ERROR flag for non-GPU validated builds
- Disable GPU debug messages by default. Re-enabled only via a command line argument: “--enableGPUMessageGroups”.
- Cleanup GL texture and sampler binding
- Rework hash map insert and emplace
- Update SDL to 2.0.5

909 Diff Diff IonutCava picture IonutCava Thu 29 Jun, 2017 23:57:18 +0000

[Ionut]
- Rework RenderTarget class
— RTAttachment now deal with textures directly
— RT size needs to be specified upfront
- Application class is no longer a Singleton but passed around in the PlatformContext

897 Diff Diff IonutCava picture IonutCava Thu 22 Jun, 2017 18:26:20 +0000

[IonutCava]
- Fix multi-context GL object creation
- Add missing file

893 Diff Diff IonutCava picture IonutCava Sun 18 Jun, 2017 17:33:07 +0000

[Ionut]
- Initial implementation of a PipelineStateObject (holds shader program, rasterizer state, etc)
- Rework PCH implementation a bit because VS2017 no longer has a /ZM option

890 Diff Diff IonutCava picture IonutCava Sun 14 May, 2017 20:54:59 +0000

[Ionut]
- Add pre-compiled header support
- Initial code for per-stage tessellation computation

879 Diff Diff IonutCava picture IonutCava Sun 19 Mar, 2017 19:29:46 +0000

[IonutCava]
- GLSL updates:
— Interface block correction to support tessellation shaders automatically
— Fixed line counting issues with the preprocessor

872 IonutCava picture IonutCava Mon 06 Mar, 2017 23:23:20 +0000

[IonutCava]
- Optimise static casts