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

#include "GUI/Headers/GUI.h"
#include "GUI/Headers/GUIText.h"
#include "Scenes/Headers/SceneState.h"
#include "Core/Headers/Application.h"
#include "Core/Headers/ParamHandler.h"
#include "Hardware/Video/Headers/GFXDevice.h"
#include "Core/Resources/Headers/ResourceCache.h"
#include "Managers/Headers/LightManager.h"
#include "Managers/Headers/RenderPassManager.h"
#include "Rendering/Camera/Headers/Camera.h"
#include "Rendering/Lighting/Headers/Light.h"
#include "Geometry/Shapes/Headers/Predefined/Quad3D.h"
#include "Managers/Headers/RenderPassManager.h"

DeferredShadingRenderer::DeferredShadingRenderer() : Renderer(RENDERER_DEFERRED_SHADING),
                                                    _debugView(false),
                                                    _cachedLightCount(0)
{
    _lightTexture = GFX_DEVICE.newPB();
    ResourceDescriptor deferred("DeferredShadingPass2");
    deferred.setThreadedLoading(false);
    _deferredShader = CreateResource<ShaderProgram>(deferred);
    _deferredBuffer = GFX_DEVICE.newFB();

    ResourceDescriptor deferredPreview("deferredPreview");
    deferredPreview.setThreadedLoading(false);
    _previewDeferredShader = CreateResource<ShaderProgram>(deferredPreview);
    SamplerDescriptor gBufferSampler;
    gBufferSampler.setWrapMode(TEXTURE_CLAMP_TO_EDGE);
    gBufferSampler.setFilters(TEXTURE_FILTER_NEAREST);
    gBufferSampler.toggleMipMaps(false);

    TextureDescriptor gBuffer[4]; /// 4 Gbuffer elements (mipmaps are ignored for deferredBuffers)
    //Albedo
    gBuffer[0] = TextureDescriptor(TEXTURE_2D, RGBA8, UNSIGNED_BYTE);  //R8G8B8A8, 32bit format for diffuse
    //Position
    gBuffer[1] = TextureDescriptor(TEXTURE_2D, RGBA32F, FLOAT_32); //R32G32B32A32, HDR 128bit format for position data
    //Normals
    gBuffer[2] = TextureDescriptor(TEXTURE_2D, RGBA16F, FLOAT_32); //R16G16B16A16, 64bit format for normals
    //Blend (for transparent objects - unused for now)
    gBuffer[3] = TextureDescriptor(TEXTURE_2D, RGBA8, UNSIGNED_BYTE); //R8G8B8A8, 32bit format for blend

    for(U8 i = 0; i < 4; i++) gBuffer[i].setSampler(gBufferSampler);

    _deferredBuffer->AddAttachment(gBuffer[0],TextureDescriptor::Color0);
    _deferredBuffer->AddAttachment(gBuffer[1],TextureDescriptor::Color1);
    _deferredBuffer->AddAttachment(gBuffer[2],TextureDescriptor::Color2);
    _deferredBuffer->AddAttachment(gBuffer[3],TextureDescriptor::Color3);
    _deferredBuffer->toggleDepthBuffer(true);
    _deferredBuffer->setClearColor(DefaultColors::BLACK());
    ResourceDescriptor mrtPreviewSmall("MRT RenderQuad SmallPreview");
    mrtPreviewSmall.setFlag(true); //no default material
    ResourceDescriptor mrt("MRT RenderQuad");
    mrt.setFlag(true); //no default material
    ResourceDescriptor mrt2("MRT RenderQuad2");
    mrt2.setFlag(true); //no default material
    ResourceDescriptor mrt3("MRT RenderQuad3");
    mrt3.setFlag(true); //no default material
    ResourceDescriptor mrt4("MRT RenderQuad4");
    mrt4.setFlag(true); //no default material
    _renderQuads.push_back(CreateResource<Quad3D>(mrt));
    _renderQuads.push_back(CreateResource<Quad3D>(mrt2));
    _renderQuads.push_back(CreateResource<Quad3D>(mrt3));
    _renderQuads.push_back(CreateResource<Quad3D>(mrt4));
    _renderQuads.push_back(CreateResource<Quad3D>(mrtPreviewSmall));

    ParamHandler& par = ParamHandler::getInstance();
    STUBBED("Shadow maps are currently disabled for Deferred Rendering! -Ionut")
    par.setParam("rendering.enableShadows",false);
    F32 width  = Application::getInstance().getResolution().width;
    F32 height = Application::getInstance().getResolution().height;
    
    updateResolution(width, height);

    GUI& gui = GUI::getInstance();
    gui.addText("PositionData",           //Unique ID
                vec2<I32>(60,60),          //Position
                Font::DIVIDE_DEFAULT,		    //Font
                vec3<F32>(0.0f,0.2f, 1.0f),  //Color
                "POSITION DATA",0);    //Text and arguments
    gui.addText("NormalData",           //Unique ID
                vec2<I32>(60+width/2,60),          //Position
                Font::DIVIDE_DEFAULT,		    //Font
                vec3<F32>(0.0f,0.2f, 1.0f),  //Color
                "NORMAL DATA",0);    //Text and arguments
    gui.addText("FinalImage",           //Unique ID
                vec2<I32>(60,60+height/2),  //Position
                Font::DIVIDE_DEFAULT,		    //Font
                vec3<F32>(0.0f,0.2f, 1.0f),  //Color
                "FINAL IMAGE",0);    //Text and arguments
    gui.addText("LightTexture",           //Unique ID
            vec2<I32>(60+width/2,60+height/2),          //Position
            Font::DIVIDE_DEFAULT,		    //Font
            vec3<F32>(0.0f,0.2f, 1.0f),  //Color
            "LIGHT TEXTURE",0);    //Text and arguments
}

DeferredShadingRenderer::~DeferredShadingRenderer()
{
    PRINT_FN(Locale::get("DEFERRED_RT_DELETE"));
    RemoveResource(_renderQuads[0]);
    RemoveResource(_renderQuads[1]);
    RemoveResource(_renderQuads[2]);
    RemoveResource(_renderQuads[3]);
    RemoveResource(_renderQuads[4]);
    _renderQuads.clear();
    SAFE_DELETE(_deferredBuffer);
    if(_deferredShader != nullptr){
        RemoveResource(_deferredShader);
    }
    RemoveResource(_previewDeferredShader);
    SAFE_DELETE(_lightTexture);
}

void DeferredShadingRenderer::render(const DELEGATE_CBK& renderCallback, const SceneRenderState& sceneRenderState) {
    GFX_DEVICE.setRenderStage(DEFERRED_STAGE);

    Light::LightMap& lights = LightManager::getInstance().getLights();
    if(lights.size() != _cachedLightCount){
        _cachedLightCount = (U16)lights.size();
        _lightTexture->Create(2,_cachedLightCount);
    }
    U8 index = 0;
    F32* pixels = (F32*)_lightTexture->Begin();
    for(U8 row=0; row<3; row++){
        for(U8 col=0; col < lights.size()/3; col++){
            U8 i = row*10+col;
            //Light Position
            pixels[index + 0]  = lights[i]->getPosition().x;
            pixels[index + 1]  = lights[i]->getPosition().y;
            pixels[index + 2]  = lights[i]->getPosition().z;
            //Light Color
            pixels[index + 3]  = lights[i]->getDiffuseColor().r;
            pixels[index + 4]  = lights[i]->getDiffuseColor().g;
            pixels[index + 5]  = lights[i]->getDiffuseColor().b;
            index += 6;
        }
    }
    _lightTexture->End();
    firstPass(renderCallback, sceneRenderState);
    secondPass(sceneRenderState);
}

void DeferredShadingRenderer::firstPass(const DELEGATE_CBK& renderCallback, const SceneRenderState& sceneRenderState){
    //Pass 1
    //Draw the geometry, saving parameters into the buffer
    _deferredBuffer->Begin(Framebuffer::defaultPolicy());
        renderCallback();
    _deferredBuffer->End();
}

void DeferredShadingRenderer::secondPass(const SceneRenderState& sceneRenderState){
    //Pass 2
    //Draw a 2D fullscreen quad that has the lighting shader applied to it and all generated textures bound to that shader
    GFX_DEVICE.toggle2D(true);

    _deferredBuffer->Bind(0, TextureDescriptor::Color0);
    _deferredBuffer->Bind(1, TextureDescriptor::Color1);
    _deferredBuffer->Bind(2, TextureDescriptor::Color2);
    _deferredBuffer->Bind(3, TextureDescriptor::Color3);
    _lightTexture->Bind(4);

    GenericDrawCommand cmd;
    cmd.setStateHash(GFX_DEVICE.getDefaultStateBlock(true));
    cmd.setDrawIDs(GFX_DEVICE.getDrawIDs(0));
    cmd.setShaderProgram(_previewDeferredShader);
    if(_debugView){
        _previewDeferredShader->bind();
        _previewDeferredShader->UniformTexture("tex",4);
        if(_renderQuads[1]->onDraw(nullptr, GFX_DEVICE.getRenderStage()))
            GFX_DEVICE.submitRenderCommand(_renderQuads[1]->getGeometryVB(), cmd);
        _previewDeferredShader->UniformTexture("tex",1);
        if(_renderQuads[2]->onDraw(nullptr, GFX_DEVICE.getRenderStage()))
            GFX_DEVICE.submitRenderCommand(_renderQuads[2]->getGeometryVB(), cmd);
        _previewDeferredShader->UniformTexture("tex",2);
        if(_renderQuads[3]->onDraw(nullptr, GFX_DEVICE.getRenderStage()))
            GFX_DEVICE.submitRenderCommand(_renderQuads[3]->getGeometryVB(), cmd);
    }
    _deferredShader->bind();
        _deferredShader->Uniform("lightCount",(I32)_cachedLightCount);

    cmd.setShaderProgram(_deferredShader);
    if(_renderQuads[ _debugView ? 4 : 0]->onDraw(nullptr, GFX_DEVICE.getRenderStage()))
        GFX_DEVICE.submitRenderCommand(_renderQuads[ _debugView ? 4 : 0]->getGeometryVB(), cmd);

    GFX_DEVICE.toggle2D(false);
    GUI& gui = GUI::getInstance();
    GUIElement* guiElement = gui.getGuiElement("FinalImage");
    if(guiElement){
        guiElement->setVisible(_debugView);
    }
    guiElement = gui.getGuiElement("LightTexture");
    if(guiElement){
        guiElement->setVisible(_debugView);
    }
    guiElement = gui.getGuiElement("PositionData");
    if(guiElement){
        guiElement->setVisible(_debugView);
    }
    guiElement = gui.getGuiElement("NormalData");
    if(guiElement){
        guiElement->setVisible(_debugView);
    }
}

void DeferredShadingRenderer::updateResolution(U16 width, U16 height){
    _deferredBuffer->Create(width, height);

    _renderQuads[0]->setDimensions(vec4<F32>(0, 0, width, height));
    _renderQuads[1]->setDimensions(vec4<F32>(width / 2, 0, width, height / 2));
    _renderQuads[2]->setCorner(Quad3D::TOP_LEFT, vec3<F32>(0, height, 0));
    _renderQuads[2]->setCorner(Quad3D::TOP_RIGHT, vec3<F32>(width / 2, height, 0));
    _renderQuads[2]->setCorner(Quad3D::BOTTOM_LEFT, vec3<F32>(0, height / 2, 0));
    _renderQuads[2]->setCorner(Quad3D::BOTTOM_RIGHT, vec3<F32>(width / 2, height / 2, 0));
    _renderQuads[3]->setCorner(Quad3D::TOP_LEFT, vec3<F32>(width / 2, height, 0));
    _renderQuads[3]->setCorner(Quad3D::TOP_RIGHT, vec3<F32>(width, height, 0));
    _renderQuads[3]->setCorner(Quad3D::BOTTOM_LEFT, vec3<F32>(width / 2, height / 2, 0));
    _renderQuads[3]->setCorner(Quad3D::BOTTOM_RIGHT, vec3<F32>(width, height / 2, 0));
    //Using a separate, smaller render quad for debug view because it's faster than resizing a quad back and forth -Ionut
    _renderQuads[4]->setDimensions(vec4<F32>(0, 0, width / 2, height / 2));
}

void DeferredShadingRenderer::toggleDebugView(){
    _debugView = !_debugView;
}

Commits for Divide-Framework/trunk/Source Code/Rendering/DeferredShadingRenderer.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]]

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

269 Diff Diff IonutCava picture IonutCava Wed 04 Jun, 2014 12:56:45 +0000

[Ionut] [[BR]]
- All texture objects now properly allocate storage using glTextureStorage*D(w-w/o Multisampled) [[BR]]
- Removed useless “format” element from TextureDescriptor. Format is deduced from the internal format specified (RGBA8 uses RGBA. RG32F uses RG) [[BR]]
- Use GLM only with radians [[BR]]

267 Diff Diff k1ngp1n picture k1ngp1n Wed 28 May, 2014 11:08:12 +0000

[Ionut] [[BR]]
- Improved texture handling: [[BR]]
— No more texture unbind (except when clearing the frame state) [[BR]]
— Moved texture bind checking to the GL_API class [[BR]]
- Fixed a crash related to vegetation loading due to compiler optimizations [[BR]]
- removed all “releaseMaterial” functions as they are no longer needed [[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]]

260 Diff Diff k1ngp1n picture k1ngp1n Sat 03 May, 2014 19:33:41 +0000

[Ionut] [[BR]]
- More work on CSM. Reverted back to VSM to allow easier blurring and filtering [[BR]]
- Rewrote FrameBuffer classes to fix MSAA and memory leaks that occurred on resize (double creation of textures) [[BR]]
— Removed FBType. FrameBuffer types depend only on attachment types and if it’s MSAA enabled or not [[BR]]
- Rewrote RenderState classes to be API agnostic [[BR]]
- Bug fixes in math classes [[BR]]
- Changed “resolution change” logic [[BR]]
- Simplified some options in config.xml [[BR]]
- Improved per-light shadow options [[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]]

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

249 k1ngp1n picture k1ngp1n Mon 31 Mar, 2014 20:42:31 +0000

[Ionut] [[BR]]
- Migrated to platform toolset v100 [[BR]]
- Updated boost to 1.55 [[BR]]
- Moved from “NULL” to “nullptr” [[BR]]
- DevIL is now linked dynamically [[BR]]