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
#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"

namespace Divide {

DeferredShadingRenderer::DeferredShadingRenderer() : Renderer(RENDERER_DEFERRED_SHADING),
                                                    _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::processVisibleNodes(const vectorImpl<SceneGraphNode* >& visibleNodes, const GFXDevice::GPUBlock& gpuBlock) {
    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();
}

void DeferredShadingRenderer::render(const DELEGATE_CBK& renderCallback, const SceneRenderState& sceneRenderState) {

    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.stateHash(GFX_DEVICE.getDefaultStateBlock(true));
    cmd.drawID(GFX_DEVICE.getDrawID(0));
    cmd.shaderProgram(_previewDeferredShader);
    if(_debugView){
        _previewDeferredShader->bind();
        _previewDeferredShader->UniformTexture("texDiffuse0",4);
        if(_renderQuads[1]->onDraw(nullptr, GFX_DEVICE.getRenderStage()))
            GFX_DEVICE.submitRenderCommand(_renderQuads[1]->getGeometryVB(), cmd);
        _previewDeferredShader->UniformTexture("texDiffuse0",1);
        if(_renderQuads[2]->onDraw(nullptr, GFX_DEVICE.getRenderStage()))
            GFX_DEVICE.submitRenderCommand(_renderQuads[2]->getGeometryVB(), cmd);
        _previewDeferredShader->UniformTexture("texDiffuse0",2);
        if(_renderQuads[3]->onDraw(nullptr, GFX_DEVICE.getRenderStage()))
            GFX_DEVICE.submitRenderCommand(_renderQuads[3]->getGeometryVB(), cmd);
    }
    _deferredShader->bind();
        _deferredShader->Uniform("lightCount",(I32)_cachedLightCount);

    cmd.shaderProgram(_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));
}

};

Commits for Divide-Framework/trunk/Source Code/Rendering/DeferredShadingRenderer.cpp

Diff revisions: vs.
Revision Author Commited Message
318 Diff Diff IonutCava picture IonutCava Sat 30 Aug, 2014 17:35:53 +0000

[Ionut]
- Wrapped the entire code in a “Divide” namespace
- VertexBuffers now call “shrink_to_fit” on all internal data storage
- Improved some vector performance by preferring “emplace_back” instead of “push_back” + proepr usage of reserve / resize
- Wrapped OIS specific types and classes in Divide::Input namespace
- Added the messageBox.layout file (forgot it in the previous few commits)

307 Diff Diff IonutCava picture IonutCava Tue 12 Aug, 2014 19:45:12 +0000

[Ionut]
- More editor work:
— Per object skeleton rendering, bounding box rendering and shadow map toggling
— Full screen depth preview toggle
— Text entry based transforms

296 Diff Diff k1ngp1n picture k1ngp1n Sun 03 Aug, 2014 14:54:21 +0000

[Ionut]
- nVidia compatibility fixes (GUI, shadows, post-processing)
- Texture bind slots are now part of ShaderProgram class
- Renamed ForwardRenderer class to ForwardPlusRenderer and moved ligh grid creation to it
- More code cleanups and comments

287 Diff Diff IonutCava picture IonutCava Tue 22 Jul, 2014 16:11:05 +0000

[Ionut]
- Unified matrix transforms and node info and made it a vertex-shader only buffer
- World matrix queries have to be issued from the SceneGraphNode instead of the SGN’s Transform class to allow future interpolation algorithms to be more easily implemented
- Moved all lighting calculations per pixel to save varying and register space (caused compilation issues both on nVidia and AMD)
- Stored GPU buffer info (view, projection, viewport etc) into a special struct to avoid multiple redundant buffer uploads and to remove redundant client side matrix caches
- Some work on shader buffer locking system
- Cleaned up MathClasses.h and Transform.h
- Updated ASSIMP to 3.1.1

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