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

#include "Headers/ShaderProgram.h"

#include "Managers/Headers/SceneManager.h"
#include "Core/Headers/Kernel.h"
#include "Core/Headers/Application.h"
#include "Core/Headers/StringHelper.h"
#include "Geometry/Material/Headers/Material.h"
#include "Rendering/Lighting/ShadowMapping/Headers/ShadowMap.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/File/Headers/FileUpdateMonitor.h"
#include "Platform/File/Headers/FileWatcherManager.h"
#include "Platform/Video/Buffers/ShaderBuffer/Headers/ShaderBuffer.h"

namespace Divide {
    bool ShaderProgram::s_useShaderTextCache = false;

namespace {
    UpdateListener s_fileWatcherListener([](const char* atomName, FileUpdateEvent evt) {
        ShaderProgram::onAtomChange(atomName, evt);
    });
};

ShaderProgram_ptr ShaderProgram::_imShader;
ShaderProgram_ptr ShaderProgram::_nullShader;
ShaderProgram::AtomMap ShaderProgram::_atoms;
ShaderProgram::ShaderQueue ShaderProgram::_recompileQueue;
ShaderProgram::ShaderProgramMap ShaderProgram::_shaderPrograms;

SharedLock ShaderProgram::_programLock;

I64 ShaderProgram::s_shaderFileWatcherID = -1;

ShaderProgram::ShaderProgram(GFXDevice& context, size_t descriptorHash, const stringImpl& name, const stringImpl& resourceName, const stringImpl& resourceLocation, bool asyncLoad)
    : CachedResource(ResourceType::GPU_OBJECT, descriptorHash, name, resourceName, resourceLocation),
      GraphicsResource(context, GraphicsResource::Type::SHADER_PROGRAM, getGUID()),
      _asyncLoad(asyncLoad),
      _shouldRecompile(false)
{
    _linked = false;
    // Override in concrete implementations with appropriate invalid values
    _shaderProgramID = 0;
}

ShaderProgram::~ShaderProgram()
{
    Console::d_printfn(Locale::get(_ID("SHADER_PROGRAM_REMOVE")), getName().c_str());
}

bool ShaderProgram::load(const DELEGATE_CBK<void, CachedResource_wptr>& onLoadCallback) {
    registerShaderProgram(std::dynamic_pointer_cast<ShaderProgram>(shared_from_this()));

    return CachedResource::load(onLoadCallback);
}

bool ShaderProgram::unload() {
    // Unregister the program from the manager
    ReadLock r_lock(_programLock);
    if (!_shaderPrograms.empty()) {
        unregisterShaderProgram(getDescriptorHash());
    }

    return true;
}

/// Called once per frame. Update common values used across programs
bool ShaderProgram::update(const U64 deltaTimeUS) {
    if (_shouldRecompile) {
        recompile();
    }

    return true;
}

/// Add a define to the shader. The defined must not have been added previously
void ShaderProgram::addShaderDefine(const stringImpl& define) {
    // Find the string in the list of program defines
    vectorImpl<stringImpl>::iterator it =
        std::find(std::begin(_definesList), std::end(_definesList), define);
    // If we can't find it, we add it
    if (it == std::end(_definesList)) {
        _definesList.push_back(define);
        _shouldRecompile = getState() == ResourceState::RES_LOADED;
    } else {
        // If we did find it, we'll show an error message in debug builds about
        // double add
        Console::d_errorfn(Locale::get(_ID("ERROR_INVALID_DEFINE_ADD")),
                           define.c_str(), getName().c_str());
    }
}

/// Remove a define from the shader. The defined must have been added previously
void ShaderProgram::removeShaderDefine(const stringImpl& define) {
    // Find the string in the list of program defines
    vectorImpl<stringImpl>::iterator it =
        std::find(std::begin(_definesList), std::end(_definesList), define);
    // If we find it, we remove it
    if (it != std::end(_definesList)) {
        _definesList.erase(it);
        _shouldRecompile = getState() == ResourceState::RES_LOADED;
    } else {
        // If we did not find it, we'll show an error message in debug builds
        Console::d_errorfn(Locale::get(_ID("ERROR_INVALID_DEFINE_DELETE")),
                           define.c_str(), getName().c_str());
    }
}

/// Rebuild the specified shader stages from source code
bool ShaderProgram::recompile() {
    if (getState() != ResourceState::RES_LOADED) {
        return true;
    }
    _usedAtoms.clear();
    if (recompileInternal()) {
        _shouldRecompile = false;
        return true;
    }

    return false;
}

void ShaderProgram::registerAtomFile(const stringImpl& atomFile) {
    _usedAtoms.push_back(atomFile);
}

//================================ static methods ========================================
void ShaderProgram::idle() {
    // If we don't have any shaders queued for recompilation, return early
    if (!_recompileQueue.empty()) {
        // Else, recompile the top program from the queue
        if (!_recompileQueue.top()->recompile()) {
            // error
        }
        _recompileQueue.pop();
    }
}

/// Calling this will force a recompilation of all shader stages for the program
/// that matches the name specified
bool ShaderProgram::recompileShaderProgram(const stringImpl& name) {
    bool state = false;
    ReadLock r_lock(_programLock);

    // Find the shader program
    for (ShaderProgramMap::value_type& it : _shaderPrograms) {
        const stringImpl& shaderName = it.second->getName();
        // Check if the name matches any of the program's name components    
        if (shaderName.find(name) != stringImpl::npos || shaderName.compare(name) == 0) {
            // We process every partial match. So add it to the recompilation queue
            _recompileQueue.push(it.second);
            // Mark as found
            state = true;
        }
    }
    // If no shaders were found, show an error
    if (!state) {
        Console::errorfn(Locale::get(_ID("ERROR_RECOMPILE_NOT_FOUND")),  name.c_str());
    }

    return state;
}

/// Open the file found at 'location' matching 'atomName' and return it's source code
const stringImpl& ShaderProgram::shaderFileRead(const stringImpl& atomName, const stringImpl& location) {
    U64 atomNameHash = _ID_RT(atomName);
    // See if the atom was previously loaded and still in cache
    AtomMap::iterator it = _atoms.find(atomNameHash);
    // If that's the case, return the code from cache
    if (it != std::cend(_atoms)) {
        return it->second;
    }

    // If we forgot to specify an atom location, we have nothing to return
    assert(!location.empty());

    // Open the atom file and add the code to the atom cache for future reference
    stringImpl output;
    readFile(location + "/" + atomName, output, FileType::TEXT);
    std::pair<AtomMap::iterator, bool> result = hashAlg::insert(_atoms, atomNameHash, output);

    assert(result.second);

    // Return the source code
    return result.first->second;
}

void ShaderProgram::shaderFileRead(const stringImpl& filePath,
                                   bool buildVariant,
                                   stringImpl& sourceCodeOut) {
    if (buildVariant) {
        stringImpl variant = filePath;
        if (Config::Build::IS_DEBUG_BUILD) {
            variant.append(".debug");
        } else if (Config::Build::IS_PROFILE_BUILD) {
            variant.append(".profile");
        } else {
            variant.append(".release");
        }
        readFile(variant, sourceCodeOut, FileType::TEXT);
    } else {
        readFile(filePath, sourceCodeOut, FileType::TEXT);
    }
}

/// Dump the source code 's' of atom file 'atomName' to file
void ShaderProgram::shaderFileWrite(const stringImpl& atomName, const char* sourceCode) {
    stringImpl variant = atomName;
    if (Config::Build::IS_DEBUG_BUILD) {
        variant.append(".debug");
    } else if (Config::Build::IS_PROFILE_BUILD) {
        variant.append(".profile");
    } else {
        variant.append(".release");
    }

    writeFile(variant, (bufferPtr)sourceCode, strlen(sourceCode), FileType::TEXT);
}

void ShaderProgram::onStartup(GFXDevice& context, ResourceCache& parentCache) {
    if (!Config::Build::IS_SHIPPING_BUILD) {
        FileWatcher& watcher = FileWatcherManager::allocateWatcher();
        s_shaderFileWatcherID = watcher.getGUID();
        s_fileWatcherListener.addIgnoredEndCharacter('~');
        s_fileWatcherListener.addIgnoredExtension("tmp");

        vectorImpl<stringImpl> atomLocations = getAllAtomLocations();
        for (const stringImpl& loc : atomLocations) {
            createDirectories(loc.c_str());
            watcher().addWatch(loc, &s_fileWatcherListener);
        }
    }

    // Create an immediate mode rendering shader that simulates the fixed function pipeline
    ResourceDescriptor immediateModeShader("ImmediateModeEmulation");
    immediateModeShader.setThreadedLoading(false);
    _imShader = CreateResource<ShaderProgram>(parentCache, immediateModeShader);
    assert(_imShader != nullptr);

    // Create a null shader (basically telling the API to not use any shaders when bound)
    _nullShader = CreateResource<ShaderProgram>(parentCache, ResourceDescriptor("NULL"));
    // The null shader should never be nullptr!!!!
    assert(_nullShader != nullptr);  // LoL -Ionut
}

void ShaderProgram::onShutdown() {
    // Make sure we unload all shaders
    _shaderPrograms.clear();
    _nullShader.reset();
    _imShader.reset();
    while (!_recompileQueue.empty()) {
        _recompileQueue.pop();
    }
    FileWatcherManager::deallocateWatcher(s_shaderFileWatcherID);
    s_shaderFileWatcherID = -1;
}

bool ShaderProgram::updateAll(const U64 deltaTimeUS) {
    ReadLock r_lock(_programLock);
    // Pass the update call to all registered programs
    for (ShaderProgramMap::value_type& it : _shaderPrograms) {
        if (!it.second->update(deltaTimeUS)) {
            // If an update call fails, stop updating
            return false;
        }
    }
    return true;
}

/// Whenever a new program is created, it's registered with the manager
void ShaderProgram::registerShaderProgram(const ShaderProgram_ptr& shaderProgram) {
    size_t shaderHash = shaderProgram->getDescriptorHash();
    unregisterShaderProgram(shaderHash);

    WriteLock w_lock(_programLock);
    hashAlg::insert(_shaderPrograms, shaderHash, shaderProgram);
}

/// Unloading/Deleting a program will unregister it from the manager
bool ShaderProgram::unregisterShaderProgram(size_t shaderHash) {
    UpgradableReadLock ur_lock(_programLock);
    ShaderProgramMap::const_iterator it = _shaderPrograms.find(shaderHash);
    if (it != std::cend(_shaderPrograms)) {
        UpgradeToWriteLock w_lock(ur_lock);
        _shaderPrograms.erase(it);
        return true;
    }

    return false;
}

const ShaderProgram_ptr& ShaderProgram::defaultShader() {
    return _imShader;
}

const ShaderProgram_ptr& ShaderProgram::nullShader() {
    return _nullShader;
}

void ShaderProgram::rebuildAllShaders() {
    for (ShaderProgramMap::value_type& shader : _shaderPrograms) {
        _recompileQueue.push(shader.second);
    }
}

void ShaderProgram::onAtomChange(const char* atomName, FileUpdateEvent evt) {
    if (evt == FileUpdateEvent::DELETE) {
        // Do nothing if the specified file is "deleted"
        // We do not want to break running programs
        return;
    }
    // ADD and MODIFY events should get processed as usual

    // Clear the atom from the cache
    AtomMap::iterator it = _atoms.find(_ID_RT(atomName));
    if (it != std::cend(_atoms)) {
        it = _atoms.erase(it);
    }

    //Get list of shader programs that use the atom and rebuild all shaders in list;
    for (ShaderProgramMap::value_type& program : _shaderPrograms) {
        for (const stringImpl& atom : program.second->_usedAtoms) {
            if (Util::CompareIgnoreCase(atom, atomName)) {
                _recompileQueue.push(program.second);
                break;
            }
        }
    }
}

vectorImpl<stringImpl> ShaderProgram::getAllAtomLocations() {
    static vectorImpl<stringImpl> atomLocations;
        if (atomLocations.empty()) {
        // General
        atomLocations.emplace_back(Paths::g_assetsLocation +
                                   Paths::g_shadersLocation);
        // GLSL
        atomLocations.emplace_back(Paths::g_assetsLocation +
                                   Paths::g_shadersLocation +
                                   Paths::Shaders::GLSL::g_parentShaderLoc);

        atomLocations.emplace_back(Paths::g_assetsLocation +
                                   Paths::g_shadersLocation +
                                   Paths::Shaders::GLSL::g_parentShaderLoc +
                                   Paths::Shaders::GLSL::g_comnAtomLoc);

        atomLocations.emplace_back(Paths::g_assetsLocation +
                                   Paths::g_shadersLocation +
                                   Paths::Shaders::GLSL::g_parentShaderLoc +
                                   Paths::Shaders::GLSL::g_compAtomLoc);

        atomLocations.emplace_back(Paths::g_assetsLocation +
                                   Paths::g_shadersLocation +
                                   Paths::Shaders::GLSL::g_parentShaderLoc +
                                   Paths::Shaders::GLSL::g_fragAtomLoc);

        atomLocations.emplace_back(Paths::g_assetsLocation +
                                   Paths::g_shadersLocation +
                                   Paths::Shaders::GLSL::g_parentShaderLoc +
                                   Paths::Shaders::GLSL::g_geomAtomLoc);

        atomLocations.emplace_back(Paths::g_assetsLocation +
                                   Paths::g_shadersLocation +
                                   Paths::Shaders::GLSL::g_parentShaderLoc +
                                   Paths::Shaders::GLSL::g_tescAtomLoc);

        atomLocations.emplace_back(Paths::g_assetsLocation +
                                   Paths::g_shadersLocation +
                                   Paths::Shaders::GLSL::g_parentShaderLoc +
                                   Paths::Shaders::GLSL::g_teseAtomLoc);

        atomLocations.emplace_back(Paths::g_assetsLocation +
                                   Paths::g_shadersLocation +
                                   Paths::Shaders::GLSL::g_parentShaderLoc +
                                   Paths::Shaders::GLSL::g_vertAtomLoc);
        // HLSL
        atomLocations.emplace_back(Paths::g_assetsLocation +
                                   Paths::g_shadersLocation +
                                   Paths::Shaders::HLSL::g_parentShaderLoc);

    }

    return atomLocations;
}

};

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

Diff revisions: vs.
Revision Author Commited Message
1012 Diff Diff IonutCava picture IonutCava Mon 15 Jan, 2018 17:21:32 +0000

[Ionut]
- Update boost to v1.66
- Performance optimizations

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

968 Diff Diff IonutCava picture IonutCava Tue 07 Nov, 2017 17:32:31 +0000

[Ionut]
- Consistent naming between timing variables

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

935 Diff Diff IonutCava picture IonutCava Tue 15 Aug, 2017 21:10:25 +0000

[IonutCava]
- Added debug scope (GL’s debug group) commands
- More bug fixes and optimizations

930 Diff Diff IonutCava picture IonutCava Sun 13 Aug, 2017 20:33:31 +0000

[Ionut]
- Fix a bug with Push Constant to Uniform conversion
- Add more push constant types
- Fix RENDER_INDIRECT param for draw commands
- Cleanup Sky, Terrain and Vegetation a bit

929 Diff Diff IonutCava picture IonutCava Sat 12 Aug, 2017 17:34:44 +0000

[Ionut]
- More work on finishing up new PushConstant / Command buffer rendering system:
— Everything compiles and runs but with graphical and performance issues

928 Diff Diff IonutCava picture IonutCava Fri 11 Aug, 2017 16:20:37 +0000

[Ionut]
- Started porting Shader uniforms to more generic PushConstants (Part 1/2: does not compile yet)
— Implemented in OpenGL via Uniform emulation

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

898 IonutCava picture IonutCava Thu 22 Jun, 2017 22:05:34 +0000

[IonutCava]
- Use vectorImpl instead of vector in the few remaining places it wasn’t used
- Force GLIM to use the submitRenderCommand / GenericDrawCommand system
- Add basic framework for a “pushConstants” type buffer that will replace Uniform() calls
- Add some code to allow hardware query rewrite in the future