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
/*
   Copyright (c) 2014 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_HANDLER_H_
#define _SHADER_HANDLER_H_

#include "Utility/Headers/Vector.h"
#include "Core/Resources/Headers/HardwareResource.h"
#include "Hardware/Video/Headers/RenderAPIEnums.h"
#include "Hardware/Video/Shaders/Headers/Shader.h"

class Shader;
class Camera;
class Material;
struct GenericDrawCommand;
enum MATRIX_MODE;

class ShaderProgram : public HardwareResource {
public:
    virtual ~ShaderProgram();

    virtual bool bind();
    virtual void unbind(bool resetActiveProgram = true);
    virtual U8   update(const U64 deltaTime);

    void UpdateDrawCommand(U8 LoD);

    ///Attributes
    inline void Attribute(const std::string& ext, D32 value) { Attribute(cachedLoc(ext,false), value); }
    inline void Attribute(const std::string& ext, F32 value) { Attribute(cachedLoc(ext, false), value); }
    inline void Attribute(const std::string& ext, const vec2<F32>& value) { Attribute(cachedLoc(ext, false), value); }
    inline void Attribute(const std::string& ext, const vec3<F32>& value) { Attribute(cachedLoc(ext, false), value); }
    inline void Attribute(const std::string& ext, const vec4<F32>& value) { Attribute(cachedLoc(ext, false), value); }
    ///Uniforms (update constant buffer for D3D. Use index as location in buffer)
    inline void Uniform(const std::string& ext, U32 value) { Uniform(cachedLoc(ext), value); }
    inline void Uniform(const std::string& ext, I32 value) { Uniform(cachedLoc(ext), value); }
    inline void Uniform(const std::string& ext, F32 value) { Uniform(cachedLoc(ext), value); }
    inline void Uniform(const std::string& ext, bool value) { Uniform(cachedLoc(ext), value); }
    inline void Uniform(const std::string& ext, const vec2<F32>& value) { Uniform(cachedLoc(ext), value); }
    inline void Uniform(const std::string& ext, const vec2<I32>& value) { Uniform(cachedLoc(ext), value); }
    inline void Uniform(const std::string& ext, const vec2<U16>& value) { Uniform(cachedLoc(ext), value); }
    inline void Uniform(const std::string& ext, const vec3<F32>& value) { Uniform(cachedLoc(ext), value); }
    inline void Uniform(const std::string& ext, const vec4<F32>& value) { Uniform(cachedLoc(ext), value); }
    inline void Uniform(const std::string& ext, const mat3<F32>& value, bool rowMajor = false) { Uniform(cachedLoc(ext), value, rowMajor); }
    inline void Uniform(const std::string& ext, const mat4<F32>& value, bool rowMajor = false) { Uniform(cachedLoc(ext), value, rowMajor); }
    inline void Uniform(const std::string& ext, const vectorImpl<I32 >& values) { Uniform(cachedLoc(ext), values); }
    inline void Uniform(const std::string& ext, const vectorImpl<F32 >& values) { Uniform(cachedLoc(ext), values); }
    inline void Uniform(const std::string& ext, const vectorImpl<vec2<F32> >& values) { Uniform(cachedLoc(ext), values); }
    inline void Uniform(const std::string& ext, const vectorImpl<vec3<F32> >& values) { Uniform(cachedLoc(ext), values); }
    inline void Uniform(const std::string& ext, const vectorImpl<vec4<F32> >& values) { Uniform(cachedLoc(ext), values); }
    inline void Uniform(const std::string& ext, const vectorImpl<mat3<F32> >& values, bool rowMajor = false) { Uniform(cachedLoc(ext), values, rowMajor); }
    inline void Uniform(const std::string& ext, const vectorImpl<mat4<F32> >& values, bool rowMajor = false) { Uniform(cachedLoc(ext), values, rowMajor); }
    ///Uniform Texture
    inline void UniformTexture(const std::string& ext, U16 slot) { UniformTexture(cachedLoc(ext), slot); }
    ///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 std::string& name) const = 0;
    virtual U32  GetSubroutineUniformIndex(ShaderType type, const std::string& name) const = 0;
    virtual U32  GetSubroutineUniformCount(ShaderType type) const = 0;
    ///Attribute+Uniform+UniformTexture implementation
    virtual void Attribute(I32 location, D32 value) const = 0;
    virtual void Attribute(I32 location, F32 value) const = 0;
    virtual void Attribute(I32 location, const vec2<F32>& value) const = 0;
    virtual void Attribute(I32 location, const vec3<F32>& value) const = 0;
    virtual void Attribute(I32 location, const vec4<F32>& value) const = 0;
    virtual void Uniform(I32 location, U32 value) const = 0;
    virtual void Uniform(I32 location, I32 value) const = 0;
    virtual void Uniform(I32 location, F32 value) const = 0;
    virtual void Uniform(I32 location, const vec2<F32>& value) const = 0;
    virtual void Uniform(I32 location, const vec2<I32>& value) const = 0;
    virtual void Uniform(I32 location, const vec2<U16>& value) const = 0;
    virtual void Uniform(I32 location, const vec3<F32>& value) const = 0;
    virtual void Uniform(I32 location, const vec4<F32>& value) const = 0;
    virtual void Uniform(I32 location, const mat3<F32>& value, bool rowMajor = false) const = 0;
    virtual void Uniform(I32 location, const mat4<F32>& value, bool rowMajor = false) const = 0;
    virtual void Uniform(I32 location, const vectorImpl<I32 >& values) const = 0;
    virtual void Uniform(I32 location, const vectorImpl<F32 >& values) const = 0;
    virtual void Uniform(I32 location, const vectorImpl<vec2<F32> >& values) const = 0;
    virtual void Uniform(I32 location, const vectorImpl<vec3<F32> >& values) const = 0;
    virtual void Uniform(I32 location, const vectorImpl<vec4<F32> >& values) const = 0;
    virtual void Uniform(I32 location, const vectorImpl<mat3<F32> >& values, bool rowMajor = false) const = 0;
    virtual void Uniform(I32 location, const vectorImpl<mat4<F32> >& values, bool rowMajor = false) const = 0;
    virtual void UniformTexture(I32, U16 slot) = 0;

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

    inline  void SetOutputCount(U8 count) { _outputCount = std::min(std::max(count, (U8)1), (U8)4); }
    virtual void attachShader(Shader* const shader,const bool refresh = false) = 0;
    virtual void detachShader(Shader* const shader) = 0;
    ///ShaderProgram object id (i.e.: for OGL _shaderProgramId = glCreateProgram())
    inline U32  getId()   const { return _shaderProgramId; }
    ///Currently active
    inline bool isBound() const {return _bound;}

    //calling recompile will re-create the marked shaders from source files and update them in the ShaderManager if needed
           void recompile(const bool vertex, const bool fragment, const bool geometry = false, const bool tessellation = false, const bool compute = false);
    //calling refresh will force an update on default shader uniforms
           void refresh() { _dirty = true;}
    //add global shader defines
    inline void addShaderDefine(const std::string& define) {_definesList.push_back(define);}
           void removeShaderDefine(const std::string& define);
    //add either fragment or vertex uniforms (without the "uniform" word. e.g. addShaderUniform("vec3 eyePos", VERTEX_SHADER);)
           void addShaderUniform(const std::string& uniform, const ShaderType& type);
           void removeUniform(const std::string& uniform, const ShaderType& type);
    //flush stored uniform locations
    virtual void flushLocCache() = 0;

    inline size_t getFunctionCount(ShaderType shader, U8 LoD){
        return _functionIndex[shader][LoD].size();
    }

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

    inline void setFunctionCount(ShaderType shader, size_t 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){
        if(_functionIndex[shader][LoD].empty()) return;

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

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

    inline U32 addFunctionIndex(ShaderType type, U32 index){
        _availableFunctionIndex[type].push_back(index);
        return U32(_availableFunctionIndex[type].size() - 1);
    }

protected:
    friend class ShaderManager;
    vectorImpl<Shader* > getShaders(const ShaderType& type) const;
    inline void setSceneDataDirty() { _sceneDataDirty = true; }
    I32 operator==(const ShaderProgram &_v) { return this->getGUID() == _v.getGUID(); }
    I32 operator!=(const ShaderProgram &_v) { return !(*this == _v); }

protected:

    ShaderProgram(const bool optimise = false);

    virtual I32 cachedLoc(const std::string& name, const bool uniform = true) = 0;
    virtual void validate() = 0;
    template<typename T>
    friend class ImplResourceLoader;
    virtual bool generateHWResource(const std::string& name);
    void updateMatrices();
    const vectorImpl<U32>& getAvailableFunctions(ShaderType type) const;

protected:
    bool _refreshStage[ShaderType_PLACEHOLDER];
    bool _optimise;
    bool _dirty;
    bool _wasBound;
    boost::atomic_bool _bound;
    boost::atomic_bool _linked;
    U32 _shaderProgramId; //<not thread-safe. Make sure assignment is protected with a mutex or something
    U64 _elapsedTime;
    F32 _elapsedTimeMS;
    I32 _maxCombinedTextureUnits;
    U8  _outputCount;
    ///A list of preprocessor defines
    vectorImpl<std::string > _definesList;
    ///A list of custom shader uniforms
    vectorImpl<std::string > _customUniforms[ShaderType_PLACEHOLDER];
    ///ID<->shaders pair
    typedef Unordered_map<U32, Shader* > ShaderIdMap;
    ShaderIdMap _shaderIdMap;
    vectorImpl<U32> _lodVertLight;
    vectorImpl<U32> _lodFragLight;

private:
    Camera* _activeCamera;
    bool _sceneDataDirty;
    ///Various uniform/attribute locations
    I32 _timeLoc;
    I32 _enableFogLoc;
    I32 _lightAmbientLoc;
    I32 _invScreenDimension;
    I32 _fogColorLoc;
    I32 _fogDensityLoc;
    U8  _prevLOD;

    vectorImpl<U32> _functionIndex[ShaderType_PLACEHOLDER][Config::SCENE_NODE_LOD];
    vectorImpl<U32> _availableFunctionIndex[ShaderType_PLACEHOLDER];
};

#endif

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

Diff revisions: vs.
Revision Author Commited Message
286 Diff Diff IonutCava picture IonutCava Fri 18 Jul, 2014 16:32:03 +0000

[Ionut] [[BR]]

    • very buggy commit ** [[BR]]

- New rendering pipeline: [[BR]]
— Bumped OpenGL version to 4.4 [[BR]]
— Command based rendering. GFXDevice dispatches rendering commands to their respective buffers [[BR]]
— State changing is exclusively controlled by the GFXDevice class [[BR]]
— Material properties and transform matrices are stored in shader storage buffers that are updated after culling and uploaded once per RenderPass. Objects use drawIDs to index data in shaders [[BR]]
— Removed all custom matrices and other, per shader, bind data [[BR]]
— Removed per node light data. Light culling and shader buffers will replace the old light management system [[BR]]
— Framebuffers now use the Texture class for attachments [[BR]]
— Sampler objects now work like RenderStateBlocks. GL_API handles them and stores them in a hash indexed map [[BR]]
— Removed “prepareMaterial” calls. Pre-draw material setup is now only limited to texture binding [[BR]]
— Moved immediate mode emulation and primitive rendering from API level to GFXDevice level [[BR]]
— Framebuffer rendering must be completed with a call to end() to ensure proper MSAA resolve (asserts, otherwise) [[BR]]
- Fixed hash code generation for RenderStateBlock (and now, glSamplerObject) using boost::hash_combine [[BR]]
- Temporarily disabled XML caching for materials [[BR]]

283 Diff Diff IonutCava picture IonutCava Tue 01 Jul, 2014 18:16:16 +0000

[Ionut][[BR]]
- Upload Viewport, camera position and zPlanes data to the gpu using the _matricesBuffer instead of ShaderProgram’s “uploadNodeMatrices” method [[BR]]
- Removed most matrices constructed with the worldMatrix (worldView, worldViewProjection, etc) to allow better batching of transforms. [[BR]]
— Both changes will allow the elimination of the “uploadNodeMatrices” and “setLoD” methods [[BR]]

280 Diff Diff IonutCava picture IonutCava Sat 28 Jun, 2014 19:11:37 +0000

[Ionut] [[BR]]
- Improve ClipPlane management (bunch equations into the matrix shader buffer) [[BR]]
- Immediate mode emulation primitives set their own state based on specified hash instead of relying on state functions to be defined [[BR]]
- updateStates() function is now called right before the actual draw call to facilitate future batching work [[BR]]
- added tracked bool values (similar to those in ShaderInfo) to SGN class to fix selection bug. [[BR]]
— will probably create a “stateTracker” class to allow multiple classes to use tracked variables without all of the code duplication [[BR]]

278 Diff Diff IonutCava picture IonutCava Thu 19 Jun, 2014 18:09:59 +0000

[Ionut][[BR]]
- Preliminary work on generic shader function selection infrastructure [[BR]]

277 Diff Diff IonutCava picture IonutCava Tue 17 Jun, 2014 19:27:29 +0000

[Ionut] [[BR]]
- Change PostFX shader to be subroutine-based (instead of using if-else statements) [[BR]]

273 Diff Diff IonutCava picture IonutCava Wed 11 Jun, 2014 20:34:00 +0000

[Ionut] [[BR]]
- Improved batching part2: [[BR]]
— Moved all bone transformation matrices upload from uniform functions in SceneNode to Shader Storage Buffers in AnimationController class with updates controlled by SceneGraphNode’s AnimationComponent [[BR]]
-— No double-buffering or efficient mapping of data yet. Just raw buffer updates [[BR]]
— Replaced all RenderStateBlock* pointers with I64 hash values [[BR]]
-— Will be used later in draw commands as per-shader state hash [[BR]]
— Moved getShaderProgram from Material to Material::ShaderInfo [[BR]]
-— Improved redundant uniform checks [[BR]]
— Improved texture unit changing in case of redundant texture bind case (i.e. skip changing the texture unit) [[BR]]
[[BR]]
- Improved float and double comparison functions using algorithms described in http://randomascii.wordpress.com/2012/02/13/dont-store-that-in-a-float/ [[BR]]
- Added a new utility function, DIVIDE_ASSERT, to more easily bind an assert check with a specific message [[BR]]
- Added missing files from Forward+ renderer’s initial code [[BR]]

272 Diff Diff IonutCava picture IonutCava Wed 11 Jun, 2014 11:30:51 +0000

[Ionut] [[BR]]
- Added initial code for Forward+ rendering (grid based light culling) [[BR]]
- Simplified Light class [[BR]]
- Moved more light uniform data to ShaderBuffers [[BR]]
- Double buffered grass rendering [[BR]]
- Added a better attribute interface to GenericVertexData [[BR]]
- Fixed screenshot capture [[BR]]
- Fixed texture double-bind checks [[BR]]

268 Diff Diff IonutCava picture IonutCava Wed 04 Jun, 2014 10:20:27 +0000

[IonutCava] [[BR]]
- Terrain and vegetation updates: [[BR]]
— Use standard phong lighting model [[BR]]
— Separate grass patches per chunk [[BR]]
— Reduce number of required texture bind points for terrain by adding both textures and normal maps in a single texture array [[BR]]
— Draw terrain with 2 drawcalls (using glMultiDrawElements twice, once for LoD == 0 and once for LoD > 0) [[BR]]
[[BR]]
- Buffer management updates: [[BR]]
— Unified buffer binding system [[BR]]
— Made Uniform buffer objects visible to all classes and moved lightUBO to LightManager, matricesUBO to GFXDevice etc [[BR]]
— Added initial support for Shader Buffer Objects [[BR]]
— Moved viewport management to GFXDevice class [[BR]]
[[BR]]
- Rendering improvements: [[BR]]
— Added Hi-Z occlusion culling to instanceCull shader (using Hi-Z depth buffer constructed in GFXDevice class) [[BR]]
— Resolved issues with shadow maps overriding texture bind locations [[[BR]]
— Improved shader and shader program loading [[BR]]
— Improved material transparency support [[BR]]
— Added buffer lock manager class from “Approaching Zero Driver Overhead in OpenGL (Presented by NVIDIA)” speech at GDC14 [[BR]]

265 Diff Diff k1ngp1n picture k1ngp1n Tue 27 May, 2014 19:47:56 +0000

[Ionut] [[BR]]
- reworked uniform buffer binding [[BR]]
- modified framebuffer mipmap update [[BR]]

264 k1ngp1n picture k1ngp1n Tue 27 May, 2014 13:04:38 +0000

[Ionut] [[BR]]
- New camera system: [[BR]]
— Stack based (push/pop active camera) [[BR]]
— Per camera frustum [[BR]]
— Per camera projection [[BR]]
— Shadow mapping now uses the new camera system (each light has a shadow camera) [[BR]]
[[BR]]
- New terrain, water and vegetation system: [[BR]]
— Direct import of terrain from L3DT both as 8-bit grayscale and 16-bit raw formats [[BR]]
— Multiple alpha maps for texture splatting support [[BR]]
— Per texture detail map support [[BR]]
— Transform feedback based visibility culling for vegetation [[BR]]
— Alpha-map based vegetation placing on terrain [[BR]]
— Water now uses a refraction map instead of alpha blending for better refraction effects [[BR]]
[[BR]]
- SceneGraph improvements: [[BR]]
— Better bounding box updates from child to parent [[BR]]
— Better onDraw / postDraw implementation [[BR]]
[[BR]]
- Performance improvements: [[BR]]
— Batch 2D rendering tasks together as much as possible[[BR]]
— Improve renderStateBlock hash generation for better sorting [[BR]]
— Move most state related gfx changes from API implementation to the GFXDevice class [[BR]]
— Move viewport stack to the GFXDevice class [[BR]]
— Added various performance counters (frame duration with round-robin query system, draw call count, etc) [[BR]]
— Fixed and improved clip plane management [[BR]]
— Delete client-side texture data after GPU upload [[BR]]
[[BR]]
- New features: [[BR]]
— Added callback support to the Task class so that a specific task may call a function in the main thread upon completion [[BR]]
— Added mouse-wheel zoom support to Orbit/ThirdPerson cameras [[BR]]
— Added 2D Texture Array support (e.g. used for terrain textures) [[BR]]