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

#include "Core/Headers/Kernel.h"
#include "Core/Headers/ParamHandler.h"
#include "Core/Resources/Headers/ResourceCache.h"
#include "Hardware/Video/Shaders/Headers/Shader.h"
#include "Hardware/Video/Headers/GFXDevice.h"

ShaderManager::ShaderManager() : _nullShader(nullptr), _activeKernel(nullptr), _init(false)
{
}

ShaderManager::~ShaderManager()
{
}

void ShaderManager::Destroy() {
    GFX_DEVICE.deInitShaders();
    RemoveResource(_imShader);
    RemoveResource(_nullShader);
}

bool ShaderManager::init(Kernel* const activeKernel){
    if(_init){
        ERROR_FN(Locale::get("WARNING_SHADER_MANAGER_DOUBLE_INIT"));
    }
    _activeKernel = activeKernel;
    _init = GFX_DEVICE.initShaders();
    ResourceDescriptor immediateModeShader("ImmediateModeEmulation");
    immediateModeShader.setThreadedLoading(false);
    _imShader = CreateResource<ShaderProgram>(immediateModeShader);
    return _init;
}

void ShaderManager::unregisterShaderProgram(const std::string& name){
    ShaderProgramMap::iterator it = _shaderPrograms.find(name);
    if(it != _shaderPrograms.end()){
        _shaderPrograms.erase(it);
    }else{
        ERROR_FN(Locale::get("ERROR_SHADER_REMOVE_NOT_FOUND"),name.c_str());
    }
}

void ShaderManager::registerShaderProgram(const std::string& name, ShaderProgram* const shaderProgram){
    if(_shaderPrograms.find(name) != _shaderPrograms.end()){
        SAFE_UPDATE(_shaderPrograms[name], shaderProgram);
    }else{
        _shaderPrograms.insert(std::make_pair(name,shaderProgram));
    }
}

U8 ShaderManager::update(const U64 deltaTime){
    FOR_EACH(ShaderProgramMap::value_type& it, _shaderPrograms){
        std::string name = it.first;
        if(it.second->update(deltaTime) == 0)
            return 0;//+ Error
    }
    return 1;
}

U8 ShaderManager::idle(){
    if(_recompileQueue.empty())
        return 0;

    _recompileQueue.top()->recompile(true,true,true,true,true);
    _recompileQueue.pop();
    return 1;
}

void ShaderManager::refresh(){
    FOR_EACH(ShaderProgramMap::value_type& it, _shaderPrograms){
        it.second->refresh();
    }
}

void ShaderManager::refreshSceneData(){
   FOR_EACH(ShaderProgramMap::value_type& it, _shaderPrograms){
        it.second->setSceneDataDirty();
    }
}

char* ShaderManager::shaderFileRead(const std::string &atomName, const std::string& location){
    AtomMap::iterator it = _atoms.find(atomName);
    if(it != _atoms.end()){
        return (char*)(it->second);
    }

    if(location.empty()){
        return nullptr;
    }

    std::string file = location+"/"+atomName;
    FILE *fp = nullptr;
    fopen_s(&fp,file.c_str(),"r");

    char *content = nullptr;

    if (fp != nullptr) {
      fseek(fp, 0, SEEK_END);
      I32 count = ftell(fp);
      rewind(fp);

            if (count > 0) {
                content = New char[count+1];
                count = (I32)(fread(content,sizeof(char),count,fp));
                content[count] = '\0';
            }
            fclose(fp);
        _atoms.insert(std::make_pair(atomName,content));
    }

    return content;
}

I8  ShaderManager::shaderFileWrite(char *atomName, const char *s){
    FILE *fp = nullptr;
    I8 status = 0;

    if (atomName != nullptr) {
        fopen_s(&fp,atomName,"w");

        if (fp != nullptr) {
            if (fwrite(s,sizeof(char),strlen(s),fp) == strlen(s))
                status = 1;
            fclose(fp);
        }
    }
    return(status);
}

void ShaderManager::removeShader(Shader* s){
    std::string name = s->getName();
    ShaderMap::iterator it = _shaderNameMap.find(name);
    if(it != _shaderNameMap.end()){
        if(s->SubRef()){
            SAFE_DELETE(it->second);
            _shaderNameMap.erase(name);
        }
    }
}

Shader* ShaderManager::findShader(const std::string& name,const bool recompile){
    ShaderMap::iterator it = _shaderNameMap.find(name);
    if(it != _shaderNameMap.end()){
        if(!recompile){
            it->second->AddRef();//<We don't need a ref count increase if we just recompile the shader
            D_PRINT_FN(Locale::get("SHADER_MANAGER_GET_SHADER_INC"),name.c_str(), it->second->getRefCount());
        }
        return it->second;
    }
    return nullptr;
}

Shader* ShaderManager::loadShader(const std::string& name, const std::string& source,const ShaderType& type,const bool recompile){
    Shader* shader = findShader(name,recompile);

    if(!recompile){
        if(shader != nullptr) return shader;

        shader = GFX_DEVICE.newShader(name, type);
    }

    if(!shader->load(source)){
        SAFE_DELETE(shader);
    }else{
        if(recompile) _shaderNameMap[name] = shader;
        else _shaderNameMap.insert(std::make_pair(name,shader));
    }

    return shader;
}

bool ShaderManager::recompileShaderProgram(const std::string& name) {
    bool state = false;
    FOR_EACH(ShaderProgramMap::value_type& it, _shaderPrograms){
        const std::string& shaderName = it.second->getName();
        if(shaderName.find(name) != std::string::npos || shaderName.compare(name) == 0){
            _recompileQueue.push(it.second);
            state = true;
        }
    }
    if(!state) ERROR_FN(Locale::get("ERROR_SHADER_RECOMPILE_NOT_FOUND"),name.c_str());
    return state;
}

bool ShaderManager::unbind(){
    if(!_nullShader){
        _nullShader = CreateResource<ShaderProgram>(ResourceDescriptor("NULL_SHADER"));
        //the null shader should never be nullptr!!!!
        assert(_nullShader != nullptr); //LoL -Ionut
    }
    //Should use program 0 and set previous shader ID to 0 as well
    _nullShader->bind();
    return true;
}

Commits for Divide-Framework/trunk/Source Code/Managers/ShaderManager.cpp

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]]

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]]

264 Diff Diff 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]]

263 Diff Diff k1ngp1n picture k1ngp1n Fri 09 May, 2014 16:43:53 +0000

[Ionut] [[BR]]
- Fullscreen quads are now rendered via Geometry Shaders [[BR]]
- Implemented shader subroutines [[BR]]
- Improved CVSM’s quality [[BR]]
- Separated shadow only light properties from general light properties in separate UBOs to improve update performance [[BR]]
- Removed shader mask and instead try to load all available shaders from a given file [[BR]]
- Added a method (hackish) of changing projection properties at runtime (e.g. z planes) [[BR]]
- Fixed a comparison bug in RenderStateBlock class (forgot to clean it at last commit) [[BR]]
- Fixed clipping plane upload to shader programs [[BR]]

259 Diff Diff k1ngp1n picture k1ngp1n Sun 27 Apr, 2014 18:24:52 +0000

[Ionut][[BR]]
- Implemented Exponential Stable Cascaded Shadow Maps for directional lights (still buggy, but basically working) [[BR]]
— CSM settings (resolution, number of splits, etc) are part of the scene render state [[BR]]
- SGN can skip rendering for the current frame if they are not ready (e.g. OnDraw can now return false or materials can fail to bind) [[BR]]
- Frustum class is now using the Plane class [[BR]]
- Removed Math SIMD implementation as the compiler generated code will always be faster and safer [[BR]]
- Added a “STUBBED” macro for outputing to the error stream of important messages (instead of using #pragma message) [[BR]]
- Added “freeze time” logic to pause/resume updates with or without freezing the GUI too [[BR]]
- Added “processGUI” method the every Scene class to separate time-sensitive GUI updates from game logic loop [[BR]]
- GLSL <-> Vertex Buffer bind locations are automatically set for every vertex shader [[BR]]
- Added basic support for compute shaders [[BR]]
- Removed most OS define checks to make porting to a new platform easier [[BR]]
- Texture transparency check per-pixel is now using OpenMP [[BR]]
- Texture samplers can now set a border color for CLAMP_TO_BORDER wrap mode [[BR]]
- Removed “Object” suffix from GFX Classes (pixel buffer, vertex buffer, frame buffer, etc) [[BR]]
- Stop processing scene input when application looses focus [[BR]]
- SceneGraphNode class is now responsible for updating SGNComponents on draw and calling its _node’s OnDraw method [[BR]]
- UBOs can now list all of the active uniforms inside a block with info such as type and offset [[BR]]
- Removed deprecated ‘GLCheck’ macro [[BR]]
- A lot of bug fixes and performance improvements in various places [[BR]]

256 Diff Diff k1ngp1n picture k1ngp1n Fri 11 Apr, 2014 17:23:24 +0000

[Ionut] [[BR]]
- Performance optimizations: [[BR]]
— Call SceneGraph::Update only once per frame (instead of once per draw call, as it was stupidly done now) [[BR]]
— Improve Shader programs' uniform system to allow better caching systems [[BR]]
— Use better caching system for Shader programs [[BR]]
— Use LOD for particle emitters (reduce count with distance for now) [[BR]]
- Every TrackedObject gets a GUID now [[BR]]
- Cleanup in SceneGraphNode class and SceneNode class (with derived classes as well) [[BR]]
- Some const-correctness fixes [[BR]]

251 Diff Diff k1ngp1n picture k1ngp1n Wed 02 Apr, 2014 20:38:18 +0000

[Ionut] [[BR]]

  • Divide is now a 64bit statically linked application * [[BR]]

- Updated to a Visual Studio 2013 solution for better C++11 support [[BR]]
— Recompiled all projects with platform toolset v120 [[BR]]
- Recompiled all projects as x64 – MT / MTd (from x86 – MD / MDd) [[BR]]
- Updated PhysX to 3.3 [[BR]]
- Updated GLFW to 3.0.4 [[BR]]

250 k1ngp1n picture k1ngp1n Wed 02 Apr, 2014 12:36:36 +0000

[Ionut] [[BR]]
- More C++11 work [[BR]]
- Renamed “for_each” macro to “FOR_EACH” to avoid name conflicts with the C++11 function [[BR]]