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
#include "Headers/glShader.h"
#include "Platform/Video/OpenGL/Headers/GLWrapper.h"
#include "Platform/Video/OpenGL/Headers/glResources.h"

#include "Core/Headers/ParamHandler.h"
#include <regex>

namespace Divide {

namespace {
    // these must match the last 4 characters of the atom file
    ULL fragAtomExt = _ID("frag");
    ULL vertAtomExt = _ID("vert");
    ULL geomAtomExt = _ID("geom");
    ULL tescAtomExt = _ID("tesc");
    ULL teseAtomExt = _ID("tese");
    ULL compAtomExt = _ID(".cpt");
    ULL comnAtomExt = _ID(".cmn");
    // Shader subfolder name that contains shader files for OpenGL
    const char* parentShaderLoc = "GLSL";
    // Atom folder names in parent shader folder
    const char* fragAtomLoc = "fragmentAtoms";
    const char* vertAtomLoc = "vertexAtoms";
    const char* geomAtomLoc = "geometryAtoms";
    const char* tescAtomLoc = "tessellationCAtoms";
    const char* teseAtomLoc = "tessellationEAtoms";
    const char* compAtomLoc = "computeAtoms";
    const char* comnAtomLoc = "common";
    // include command regex pattern
    const std::regex includePattern("^[ ]*#[ ]*include[ ]+[\"<](.*)[\">].*");

};

const char* glShader::CACHE_LOCATION_TEXT = "shaderCache/Text/";
const char* glShader::CACHE_LOCATION_BIN = "shaderCache/Binary/";

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

IMPLEMENT_CUSTOM_ALLOCATOR(glShader, 0, 0);
glShader::glShader(GFXDevice& context,
                   const stringImpl& name,
                   const ShaderType& type,
                   const bool optimise)
    : GraphicsResource(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;
    };

    if (shaderAtomLocationPrefix[to_const_uint(ShaderType::VERTEX)].empty()) {
        ParamHandler& par = ParamHandler::instance();
        stringImpl locPrefix = par.getParam<stringImpl>(_ID("assetsLocation"), "assets") +
                               "/" +
                               par.getParam<stringImpl>(_ID("shaderLocation"), "shaders") +
                               "/" + 
                               parentShaderLoc +
                               "/";

        shaderAtomLocationPrefix[to_const_uint(ShaderType::FRAGMENT)] = locPrefix + fragAtomLoc;
        shaderAtomLocationPrefix[to_const_uint(ShaderType::VERTEX)] = locPrefix + vertAtomLoc;
        shaderAtomLocationPrefix[to_const_uint(ShaderType::GEOMETRY)] = locPrefix + geomAtomLoc;
        shaderAtomLocationPrefix[to_const_uint(ShaderType::TESSELATION_CTRL)] = locPrefix + tescAtomLoc;
        shaderAtomLocationPrefix[to_const_uint(ShaderType::TESSELATION_EVAL)] = locPrefix + teseAtomLoc;
        shaderAtomLocationPrefix[to_const_uint(ShaderType::COMPUTE)] = locPrefix + compAtomLoc;
        shaderAtomLocationPrefix[to_const_uint(ShaderType::COUNT)] = locPrefix + comnAtomLoc;
    }
}

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

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

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

    const char* src = parsedSource.c_str();

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

    if (!_skipIncludes) {
        ShaderProgram::shaderFileWrite(glShader::CACHE_LOCATION_TEXT + getName(), src);
    }

    return true;
}

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

    glCompileShader(_shader);
    validate();

    _compiled = true;

    return _compiled;
}

bool glShader::validate() {
#if defined(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")), _name.c_str(),
                         &shaderLog[0]);
        return true;
    } else {
        Console::d_printfn(Locale::get(_ID("GLSL_VALIDATING_SHADER")), _name.c_str(),
                           &shaderLog[0]);
        return false;
    }
#else
    return true;
#endif
}

stringImpl glShader::preprocessIncludes(const stringImpl& source,
                                        const stringImpl& filename,
                                        I32 level /*= 0 */) {
    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, includePattern)) {
            include_file = matches[1].str().c_str();

            I32 index = -1;
            // switch will throw warnings due to promotion to int
            ULL extHash = _ID_RT(Util::GetTrailingCharacters(include_file, 4));
            if (extHash == fragAtomExt) {
                index = to_const_int(ShaderType::FRAGMENT);
            } else if (extHash == vertAtomExt) {
                index = to_const_int(ShaderType::VERTEX); 
            } else if (extHash == geomAtomExt) {
                index = to_const_int(ShaderType::GEOMETRY);
            } else if (extHash == tescAtomExt) {
                index = to_const_int(ShaderType::TESSELATION_CTRL);
            } else if (extHash == teseAtomExt) {
                index = to_const_int(ShaderType::TESSELATION_EVAL);
            } else if (extHash == compAtomExt) {
                index = to_const_int(ShaderType::COMPUTE);
            } else if (extHash == comnAtomExt) {
                index = to_const_int(ShaderType::COUNT);
            }

            assert(index != -1);

            include_string = ShaderProgram::shaderFileRead(include_file, shaderAtomLocationPrefix[index]);

            if (include_string.empty()) {
                Console::errorfn(Locale::get(_ID("ERROR_GLSL_NO_INCLUDE_FILE")),
                                 getName().c_str(),
                                 line_number,
                                 include_file.c_str());
            }
            output.append(preprocessIncludes(include_string, include_file, level + 1));
        } 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
    ULL 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, const bool recompile) {
    // Try to find the shader
    ReadLock r_lock(_shaderNameLock);
    ShaderMap::iterator it = _shaderNameMap.find(_ID_RT(name));
    if (it != std::end(_shaderNameMap)) {
        if (!recompile) {
            // We don't need a ref count increase if we just recompile the shader
            it->second->AddRef();
            Console::d_printfn(Locale::get(_ID("SHADER_MANAGER_GET_INC")),
                name.c_str(), it->second->GetRef());
        }
        return it->second;
    }

    return nullptr;
}

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

    shader->skipIncludes(!parseCode);
    // At this stage, we have a valid Shader object, so load the source code
    if (!shader->load(source)) {
        // If loading the source code failed, delete it
        MemoryManager::DELETE(shader);
    } else {
        ULL nameHash = _ID_RT(name);
        // If we loaded the source code successfully, either update it (if we
        // recompiled) or register it
        WriteLock w_lock(_shaderNameLock);
        if (recompile) {
            _shaderNameMap[nameHash] = shader;
        } else {
            hashAlg::emplace(_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
727 Diff Diff IonutCava picture IonutCava Tue 14 Jun, 2016 16:01:38 +0000

[IonutCava]
- Remove node and cmd buffers from GFXDevice and add them as a new BufferData struct to RenderPass class.
— Each render pass holds its own buffers
- Improvements / fixes to the CSM code
- Added a global toggle to enable/disable custom memory allocators

718 Diff Diff IonutCava picture IonutCava Thu 02 Jun, 2016 16:02:48 +0000

[IonutCava]
- Code cleanup:
— Rename Framebuffer to RenderTarget. Only OpenGL uses the Framebuffer nomenclature.
— Remove base Shader class as only OpenGL will have separate shaders (and eventually move to pipeline objects) as Direct3D uses FX files and may end up with different structure
— Remove drawBox3D, drawSphere3D and drawLines from GFXDevice class and add them as member functions to the IMPrimitive class (renamed to fromXYZ)
— Pull some elements from SceneManager and move them to standalone classes (W.I.P.)

716 Diff Diff IonutCava picture IonutCava Tue 31 May, 2016 16:08:29 +0000

[IonutCava]
- Multithreaded scene load/unload fixes:
— Added postLoadMainThread call for scenes for thread sensitive loading steps
— GUI element adding is a thread sensitive loading step (a fast one) because CEGUI doesn’t handle threading
- Added REALTIME_WITH_CALLBACK task priority for tasks that need an immediate callback when they complete instead of using the flushCallbackQueue system.
- Some work on shadow mapping for getting it to work again
- ShaderPrograms that fail to load can cause a LockManager infinite wait. Delete the lockManager if the shader didn’t load to avoid waiting on commands that never get called.

710 Diff Diff IonutCava picture IonutCava Fri 20 May, 2016 16:24:40 +0000

[IonutCava]
- Code cleanup
- Initial work on Scene loading and unloading with and without unloading assets:
— Need to change AIManager from a Singleton to a per-scene component

709 Diff Diff IonutCava picture IonutCava Thu 19 May, 2016 16:21:46 +0000

[IonutCava]
- Massive rewrite of the resource system:
— ResourceCache now uses a map of weak pointers and passes shared pointers to the call point of CreateResource
— RemoveResource is no longer needed, but resource based smart pointers use a custom deleter that calls unload()
— Keep a shared_ptr of the resource in the entity that needs it and pass shared_ptr references from getter methods
-— All of this removed the need for calls to RemoveResource, REGISTER_TRACKED_DEPENDENCY and UNREGISTER_TRACKED_DEPENDENCY reducing the number of mutex locks and atomic exchanges
- Singleton cleanup:
— Removed ShaderManager singleton and merged it’s data and responsibilities in static methods in both ShaderProgram and Shader classes.
Explanation: Due to the complex interdependency chains in the system, copying, updating, tracking the cache in a thread safe way became a lot more slower than a straight forward smart pointer based implementation. (e.g. scene graph nodes have 3d objects and material instances, 3d objects have materials, materials have textures and shaders, etc)

671 Diff Diff IonutCava picture IonutCava Fri 08 Apr, 2016 16:26:49 +0000

[Ionut]
- Custom memory allocation system part 1:
— Add http://www.codeproject.com/Articles/1089905/A-Custom-STL-std-allocator-Replacement-Improves-Pe
— Add custom allocators to strings and vectors
— Add custom allocators to a few test classes
— No support for aligned memory yet, so vec4<F32> and mat4<F32> vectors use the old allocation system
- Replace more std::async call with our custom task system

670 Diff Diff IonutCava picture IonutCava Wed 06 Apr, 2016 16:21:35 +0000

[IonutCava]
- getInstance() renamed to instance() for simplicity (should really go away altogether, but that’s a different issue)
- some particle emitter threading updates

667 Diff Diff IonutCava picture IonutCava Fri 01 Apr, 2016 16:12:16 +0000

[IonutCava]
- replaced more strings with _ID equivalent
- streamlined shader include preprocessing
- improved intersection test performance for the Octree system at the cost of some memory

633 Diff Diff IonutCava picture IonutCava Wed 27 Jan, 2016 16:54:12 +0000

[IonutCava]
- Improve load times by using ShaderCache files to avoid re-parsing atoms multiple times
- Z_PRE_PASS and multi-threaded asset loading is disabled automatically for AMD only (until driver issues are resolved)

591 IonutCava picture IonutCava Wed 02 Dec, 2015 17:17:23 +0000

[IonutCava]
- More work on the ForwardPlus renderer
- Singleton cleanup
- First steps for reducing scope of GFXDevice class