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

#include "Managers/Headers/SceneManager.h"
#include "Core/Headers/Kernel.h"
#include "Core/Headers/Application.h"
#include "Geometry/Material/Headers/Material.h"
#include "Rendering/Lighting/ShadowMapping/Headers/ShadowMap.h"
#include "Platform/Video/Headers/GFXDevice.h"

namespace Divide {

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

ShaderProgram::ShaderProgram(GFXDevice& context, const stringImpl& name, const stringImpl& resourceName, const stringImpl& resourceLocation, bool asyncLoad)
    : Resource(ResourceType::GPU_OBJECT, name, resourceName, resourceLocation),
      GraphicsResource(context, getGUID()),
      _asyncLoad(asyncLoad)
{
    _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() {
    registerShaderProgram(getName(), shared_from_this());
    return Resource::load();
}

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

    return true;
}

/// Called once per frame. Update common values used across programs
bool ShaderProgram::update(const U64 deltaTime) {
    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);
    } 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);
    } 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;
    }

    // Remember bind state
    bool wasBound = isBound();
    if (wasBound) {
        ShaderProgram::unbind();
    }
    bool state = recompileInternal();

    // Restore bind state
    if (wasBound) {
        bind();
    }

    return state;
}

//================================ 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::emplace(_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, sourceCode, FileType::TEXT);
}

void ShaderProgram::onStartup(ResourceCache& parentCache) {
    // 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();
    }
}

bool ShaderProgram::updateAll(const U64 deltaTime) {
    ReadLock r_lock(_programLock);
    // Pass the update call to all registered programs
    for (ShaderProgramMap::value_type& it : _shaderPrograms) {
        if (!it.second->update(deltaTime)) {
            // 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 stringImpl& name, const ShaderProgram_ptr& shaderProgram) {
    unregisterShaderProgram(name);

    WriteLock w_lock(_programLock);
    hashAlg::emplace(_shaderPrograms, _ID_RT(name), shaderProgram);
}

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

    return false;
}

/// Bind the NULL shader which should have the same effect as using no shaders at all
bool ShaderProgram::unbind() {
    return _nullShader->bind();
}

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


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

};

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

Diff revisions: vs.
Revision Author Commited Message
832 Diff Diff IonutCava picture IonutCava Thu 26 Jan, 2017 17:23:11 +0000

[IonutCava]
- More path related work
- Initial code to centralize file I/O (does not compile yet)

831 Diff Diff IonutCava picture IonutCava Wed 25 Jan, 2017 23:52:34 +0000

[IonutCava]
- More path related updates

830 Diff Diff IonutCava picture IonutCava Wed 25 Jan, 2017 17:29:44 +0000

[IonutCava]
- Add a new Platform element: File
— Move all predefined paths to FileManagement System
- Split MathHelper into MathHelper and StringHelper

829 Diff Diff IonutCava picture IonutCava Tue 24 Jan, 2017 17:28:39 +0000

[IonutCava]
- Separate resourceLocation for resources into resourceLocation and resourceName.
- More XML config loading refactoring

807 Diff Diff IonutCava picture IonutCava Mon 09 Jan, 2017 17:06:26 +0000

[IonutCava]
- Split PlatformDefines.h into 2. Move data type defines to PlatformDataTypes.h
- Continue to improve velocity map calculation
- Remove “destroy()” calls from most classes. Attempt to move as many classes to the RAII system.

804 Diff Diff IonutCava picture IonutCava Thu 01 Dec, 2016 17:20:59 +0000

[IonutCava]
- Singleton elimination update Part I: get it to compile
— The following classes are no longer Singletons: GFXDevice, GL_API, DX_API, SFXWrapper, FmodWrapper, SDLWrapper, ALWrapper, PXDevice, InputInterface, RenderPassManager, SceneManager and ResourceManager;
— Refactor system to a Context / Component based implementation (Pass relevant context to objects: e.g. GFXDevice object to Textures, GUI to GUIElements, etc)
— Make devices and managers components of the kernel
— Allow multiple Resource caches to co-exist. This may prove useful for later when a more fragmented memory model is need (per frame / per scene / global caches / etc)

  • next steps: part II – cleanup/refactor new code, part III – optimise code, part IV – remove remaining Singletons (e.g. Application, ParamHandler, FrameListenerManager, Recast, PostFX and DebugInterface)
795 Diff Diff IonutCava picture IonutCava Wed 23 Nov, 2016 17:14:33 +0000

[IonutCava]
- Remove ULL. Use U64 instead
- Various small bug fixes and typos.

788 Diff Diff IonutCava picture IonutCava Fri 21 Oct, 2016 16:11:37 +0000

[IonutCava]
- Added support for the Arena Allocator by Mamasha Knows (http://www.codeproject.com/Articles/44850/Arena-Allocator-DTOR-and-Embedded-Preallocated-Buf)
— Used for GFX Related objects: Textures, shaders, etc

768 Diff Diff IonutCava picture IonutCava Fri 09 Sep, 2016 14:38:51 +0000

[IonutCava]
- Refactored framework entry points (init, step, shutdown) to allow the addition of hot code reloading in the future

760 IonutCava picture IonutCava Tue 09 Aug, 2016 16:30:12 +0000

[IonutCava]
- Complete rewrite of RenderPass system:
— Allow every rendering pass (reflection, environment, etc) to use the same rendering system: occlusion culling, light culling, etc
— Define render passes by a param based token similar to modern API systems (DX12/Vulkan)
— W.I.P.: Depth rendering slightly wrong; Environment Mapping ,reflection and refraction rendering currently disabled
- Add compile time variables for DEBUG/PROFILE/RELEASE builds to validate all code for every build