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
/*
   Copyright (c) 2017 DIVIDE-Studio
   Copyright (c) 2009 Ionut Cava

   This file is part of DIVIDE Framework.

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software
   and associated documentation files (the "Software"), to deal in the Software
   without restriction,
   including without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
   PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
   IN CONNECTION WITH THE SOFTWARE
   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 */

#ifndef _SHADER_PROGRAM_H_
#define _SHADER_PROGRAM_H_

#include "config.h"

#include "Core/Resources/Headers/Resource.h"
#include "Platform/Video/Headers/GraphicsResource.h"
#include "Platform/Video/Headers/RenderAPIEnums.h"

#include <stack>

namespace FW {
    class FileWatcher;
};

namespace Divide {

class Camera;
class Material;
class ShaderBuffer;
class GenericDrawCommand;

FWD_DECLARE_MANAGED_CLASS(ShaderProgram);

class NOINITVTABLE ShaderProgram : public Resource, 
                                   public GraphicsResource,
                                   public std::enable_shared_from_this<ShaderProgram> {
   public:
    typedef hashMapImpl<U64, ShaderProgram_ptr> ShaderProgramMap;
    typedef hashMapImpl<U64, stringImpl> AtomMap;
    typedef std::stack<ShaderProgram_ptr, vectorImpl<ShaderProgram_ptr> > ShaderQueue;
    /// A list of built-in sampler slots. Use these if possible
    enum class TextureUsage : U32 {
        UNIT0 = 0,
        UNIT1 = 1,
        NORMALMAP = 2,
        OPACITY = 3,
        SPECULAR = 4,
        PROJECTION = 5,
        DEPTH = 6,
        DEPTH_PREV = 7,
        REFLECTION = 8,
        REFRACTION = 9,
        COUNT,

        GLOSS = SPECULAR,
        ROUGHNESS = GLOSS
    };

    enum class MemoryBarrierType : U32 {
        BUFFER = 0,
        TEXTURE = 1,
        RENDER_TARGET = 2,
        TRANSFORM_FEEDBACK = 3,
        COUNTER = 4,
        QUERY = 5,
        SHADER_BUFFER = 6,
        ALL = 7,
        COUNT
    };

    explicit ShaderProgram(GFXDevice& context,
                           const stringImpl& name,
                           const stringImpl& resourceName,
                           const stringImpl& resourceLocation,
                           bool asyncLoad);
    virtual ~ShaderProgram();

    virtual bool bind() = 0;
    /// Currently active
    virtual bool isBound() const = 0;
    /// Is the shader ready for drawing?
    virtual bool isValid() const = 0;
    virtual bool update(const U64 deltaTime);
    virtual bool load() override;
    virtual bool unload() override;

    /// Uniforms (update constant buffer for D3D. Use index as location in
    /// buffer)
    virtual void Uniform(const char* ext, U32 value) = 0;
    virtual void Uniform(const char* ext, I32 value) = 0;
    virtual void Uniform(const char* ext, F32 value) = 0;
    virtual void Uniform(const char* ext, const vec2<F32>& value) = 0;
    virtual void Uniform(const char* ext, const vec2<I32>& value) = 0;
    virtual void Uniform(const char* ext, const vec3<F32>& value) = 0;
    virtual void Uniform(const char* ext, const vec3<I32>& value) = 0;
    virtual void Uniform(const char* ext, const vec4<F32>& value) = 0;
    virtual void Uniform(const char* ext, const vec4<I32>& value) = 0;
    virtual void Uniform(const char* ext, const mat3<F32>& value, bool rowMajor = false) = 0;
    virtual void Uniform(const char* ext, const mat4<F32>& value, bool rowMajor = false) = 0;
    virtual void Uniform(const char* ext, const vectorImpl<I32>& values) = 0;
    virtual void Uniform(const char* ext, const vectorImpl<F32>& values) = 0;
    virtual void Uniform(const char* ext, const vectorImpl<vec2<F32> >& values) = 0;
    virtual void Uniform(const char* ext, const vectorImpl<vec3<F32> >& values) = 0;
    virtual void Uniform(const char* ext, const vectorImplBest<vec4<F32> >& values) = 0;
    virtual void Uniform(const char* ext, const vectorImpl<mat3<F32> >& values, bool rowMajor = false) = 0;
    virtual void Uniform(const char* ext, const vectorImplBest<mat4<F32> >& values, bool rowMajor = false) = 0;

    inline void Uniform(const char* ext, TextureUsage slot) {
        Uniform(ext, to_int(slot));
    }
        
    inline void Uniform(const char* ext, bool value) {
        Uniform(ext, value ? 1 : 0);
    }

    virtual I32 getUniformLocation(const char* name) = 0;
    /// Subroutine
    virtual void SetSubroutines(ShaderType type, const vectorImpl<U32>& indices) const = 0;
    virtual void SetSubroutine(ShaderType type, U32 index) const = 0;
    virtual U32 GetSubroutineIndex(ShaderType type, const char* name) const = 0;
    virtual U32 GetSubroutineUniformLocation(ShaderType type, const char* name) const = 0;
    virtual U32 GetSubroutineUniformCount(ShaderType type) const = 0;
    /// Uniform+UniformTexture implementation
    virtual void Uniform(I32 location, U32 value) = 0;
    virtual void Uniform(I32 location, I32 value) = 0;
    virtual void Uniform(I32 location, F32 value) = 0;
    virtual void Uniform(I32 location, const vec2<F32>& value) = 0;
    virtual void Uniform(I32 location, const vec2<I32>& value) = 0;
    virtual void Uniform(I32 location, const vec3<F32>& value) = 0;
    virtual void Uniform(I32 location, const vec3<I32>& value) = 0;
    virtual void Uniform(I32 location, const vec4<F32>& value) = 0;
    virtual void Uniform(I32 location, const vec4<I32>& value) = 0;
    virtual void Uniform(I32 location,
                         const mat3<F32>& value,
                         bool rowMajor = false) = 0;
    virtual void Uniform(I32 location,
                         const mat4<F32>& value,
                         bool rowMajor = false) = 0;
    virtual void Uniform(I32 location, const vectorImpl<I32>& values) = 0;
    virtual void Uniform(I32 location, const vectorImpl<F32>& values) = 0;
    virtual void Uniform(I32 location,
                         const vectorImpl<vec2<F32> >& values) = 0;
    virtual void Uniform(I32 location,
                         const vectorImpl<vec3<F32> >& values) = 0;
    virtual void Uniform(I32 location,
                         const vectorImplBest<vec4<F32> >& values) = 0;
    virtual void Uniform(I32 location,
                         const vectorImpl<mat3<F32> >& values,
                         bool rowMajor = false) = 0;
    virtual void Uniform(I32 location,
                         const vectorImplBest<mat4<F32> >& values,
                         bool rowMajor = false) = 0;

    virtual void DispatchCompute(U32 xGroups, U32 yGroups, U32 zGroups) = 0;

    virtual void SetMemoryBarrier(MemoryBarrierType type) = 0;

    inline void Uniform(I32 location, TextureUsage slot) {
        Uniform(location, to_ubyte(slot));
    }

    inline void Uniform(I32 location, bool value) {
        Uniform(location, value ? 1 : 0);
    }

    /// ShaderProgram object id (i.e.: for OGL _shaderProgramID =
    /// glCreateProgram())
    inline U32 getID() const { return _shaderProgramID; }
    ///  Calling recompile will re-create the marked shaders from source files
    ///  and update them in the ShaderManager if needed
    bool recompile();
    /// Add a define to the shader. The defined must not have been added
    /// previously
    void addShaderDefine(const stringImpl& define);
    /// Remove a define from the shader. The defined must have been added
    /// previously
    void removeShaderDefine(const stringImpl& define);

    /** ------ BEGIN EXPERIMENTAL CODE ----- **/
    inline vectorAlg::vecSize getFunctionCount(ShaderType shader, U8 LoD) {
        return _functionIndex[to_uint(shader)][LoD].size();
    }

    inline void setFunctionCount(ShaderType shader,
                                 U8 LoD,
                                 vectorAlg::vecSize count) {
        _functionIndex[to_uint(shader)][LoD].resize(count, 0);
    }

    inline void setFunctionCount(ShaderType shader, vectorAlg::vecSize count) {
        for (U8 i = 0; i < Config::SCENE_NODE_LOD; ++i) {
            setFunctionCount(shader, i, count);
        }
    }

    inline void setFunctionIndex(ShaderType shader,
                                 U8 LoD,
                                 U32 index,
                                 U32 functionEntry) {
        U32 shaderTypeValue = to_uint(shader);

        if (_functionIndex[shaderTypeValue][LoD].empty()) {
            return;
        }

        DIVIDE_ASSERT(index < _functionIndex[shaderTypeValue][LoD].size(),
                      "ShaderProgram error: Invalid function index specified "
                      "for update!");
        if (_availableFunctionIndex[shaderTypeValue].empty()) {
            return;
        }

        DIVIDE_ASSERT(
            functionEntry < _availableFunctionIndex[shaderTypeValue].size(),
            "ShaderProgram error: Specified function entry does not exist!");
        _functionIndex[shaderTypeValue][LoD][index] =
            _availableFunctionIndex[shaderTypeValue][functionEntry];
    }

    inline U32 addFunctionIndex(ShaderType shader, U32 index) {
        U32 shaderTypeValue = to_uint(shader);

        _availableFunctionIndex[shaderTypeValue].push_back(index);
        return U32(_availableFunctionIndex[shaderTypeValue].size() - 1);
    }
    /** ------ END EXPERIMENTAL CODE ----- **/

    //==================== static methods ===============================//
    static void idle();
    static void onStartup(ResourceCache& parentCache);
    static void onShutdown();
    static bool updateAll(const U64 deltaTime);
    /// Queue a shaderProgram recompile request
    static bool recompileShaderProgram(const stringImpl& name);
    static const stringImpl& shaderFileRead(const stringImpl& atomName, const stringImpl& location);
    static void shaderFileRead(const stringImpl& filePath, bool buildVariant, stringImpl& sourceCodeOut);
    static void shaderFileWrite(const stringImpl& atomName, const char* sourceCode);
    /// Remove a shaderProgram from the program cache
    static bool unregisterShaderProgram(const stringImpl& name);
    /// Add a shaderProgram to the program cache
    static void registerShaderProgram(const stringImpl& name, const ShaderProgram_ptr& shaderProgram);
    /// Bind the null shader
    static bool unbind();
    /// Return a default shader used for general purpose rendering
    static const ShaderProgram_ptr& defaultShader();

    static void onAtomChange(const stringImpl& atomName);

    static void rebuildAllShaders();

    static vectorImpl<stringImpl> getAllAtomLocations();

   protected:
       virtual bool recompileInternal() = 0;

       void registerAtomFile(const stringImpl& atomFile);
   protected:
    /// Shaders loaded from files are kept as atoms
    static AtomMap _atoms;
    /// Used to render geometry without valid materials.
    /// Should emulate the basic fixed pipeline functions (no lights, just colour and texture)
    static ShaderProgram_ptr _imShader;
    /// Pointer to a shader that we will perform operations on
    static ShaderProgram_ptr _nullShader;
    /// Only 1 shader program per frame should be recompiled to avoid a lot of stuttering
    static ShaderQueue _recompileQueue;
    /// Shader program cache
    static ShaderProgramMap _shaderPrograms;

    static SharedLock _programLock;

   protected:
    template <typename T>
    friend class ImplResourceLoader;

    bool _asyncLoad;
    std::atomic_bool _linked;
    U32 _shaderProgramID;  //<not thread-safe. Make sure assignment is protected
    // with a mutex or something
    /// A list of preprocessor defines
    vectorImpl<stringImpl> _definesList;
    /// A list of atoms used by this program. (All stages are added toghether)
    vectorImpl<stringImpl> _usedAtoms;

   private:
    std::array<std::array<vectorImpl<U32>, Config::SCENE_NODE_LOD>, to_const_uint(ShaderType::COUNT)> _functionIndex;
    std::array<vectorImpl<U32>, to_const_uint(ShaderType::COUNT)>  _availableFunctionIndex;
    
    static std::unique_ptr<FW::FileWatcher> s_shaderFileWatcher;
};

};  // namespace Divide
#endif //_SHADER_PROGRAM_H_

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

Diff revisions: vs.
Revision Author Commited Message
842 Diff Diff IonutCava picture IonutCava Wed 01 Feb, 2017 17:25:15 +0000

[IonutCava]
- Start to implement scripting support via ChaiScript: http://chaiscript.com/
- Cleanup DELEGATE_CBK alias

839 Diff Diff IonutCava picture IonutCava Tue 31 Jan, 2017 17:28:13 +0000

[IonutCava]
- Add simplefilewatcher libary: https://github.com/jameswynn/simplefilewatcher
- Use simplefilewatcher to automatically recompile shaders after save without restarting the app!
— Shaders can now be edited on the fly with automatic rebuild if they used one of the modified atoms.

836 Diff Diff IonutCava picture IonutCava Fri 27 Jan, 2017 14:59:56 +0000

[IonutCava]
- Update copyright notice

833 Diff Diff IonutCava picture IonutCava Thu 26 Jan, 2017 22:04:33 +0000

[IonutCava]
- Fix most build errors. Divide-Server does not build for now.

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.

776 Diff Diff IonutCava picture IonutCava Thu 06 Oct, 2016 15:57:56 +0000

[IonutCava]
- Improve the CommandBuffer system to include the notion of a RenderPass /RenderSubPass with output render target info stored in them (not used yet)

770 IonutCava picture IonutCava Tue 20 Sep, 2016 16:12:27 +0000

[IonutCava]
- Prepare velocity map calculation (later used for temporal AA and motion blur):
— Add a previous depth buffer render target that blits the main depth buffer at the end of the frame
— Add a new render target to the screen FBO: velocity map
- Store normals as RG instead of RGB and pack/unpack in shaders. (compute >>>> bandwith)
- Rename Forward+ renderer to something more descriptive

(Requires deletion of all parsed & cached data)