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
#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 "Platform/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 R8G8B8A8, 32bit format for diffuse
    gBuffer[0] = TextureDescriptor(TEXTURE_2D, RGBA8, UNSIGNED_BYTE);
    //Position R32G32B32A32, HDR 128bit format for position data
    gBuffer[1] = TextureDescriptor(TEXTURE_2D, RGBA32F, FLOAT_32); 
    //Normals R16G16B16A16, 64bit format for normals
    gBuffer[2] = TextureDescriptor(TEXTURE_2D, RGBA16F, FLOAT_32); 
    //Blend (for transparent objects - unused for now) R8G8B8A8, 32bit format for blend
    gBuffer[3] = TextureDescriptor(TEXTURE_2D, RGBA8, UNSIGNED_BYTE); 

    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();
    MemoryManager::DELETE( _deferredBuffer );
    if(_deferredShader != nullptr){
        RemoveResource(_deferredShader);
    }
    RemoveResource(_previewDeferredShader);
    MemoryManager::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 with lighting shader applied 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.shaderProgram(_previewDeferredShader);
    if(_debugView){
        _previewDeferredShader->bind();
        _previewDeferredShader->UniformTexture("texDiffuse0",4);
        if (_renderQuads[1]->onDraw(nullptr, GFX_DEVICE.getRenderStage())) {
            cmd.sourceBuffer(_renderQuads[1]->getGeometryVB());
            GFX_DEVICE.submitRenderCommand(cmd);
        }
        _previewDeferredShader->UniformTexture("texDiffuse0",1);
        if (_renderQuads[2]->onDraw(nullptr, GFX_DEVICE.getRenderStage())){
            cmd.sourceBuffer(_renderQuads[2]->getGeometryVB());
            GFX_DEVICE.submitRenderCommand(cmd);
        }
        _previewDeferredShader->UniformTexture("texDiffuse0",2);
        if (_renderQuads[3]->onDraw(nullptr, GFX_DEVICE.getRenderStage())) {
            cmd.sourceBuffer(_renderQuads[3]->getGeometryVB());
            GFX_DEVICE.submitRenderCommand(cmd);
        }
    }
    _deferredShader->bind();
    _deferredShader->Uniform("lightCount",(I32)_cachedLightCount);

    cmd.shaderProgram(_deferredShader);
    if (_renderQuads[_debugView ? 4 : 0]->onDraw(nullptr, GFX_DEVICE.getRenderStage())) {
        cmd.sourceBuffer(_renderQuads[_debugView ? 4 : 0]->getGeometryVB());
        GFX_DEVICE.submitRenderCommand(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
337 Diff Diff IonutCava picture IonutCava Wed 17 Dec, 2014 17:25:16 +0000

[Ionut]
- Replaced all tabs with 4 spaces (some spacing may be off now. no way to check all the files.)
- Moved implementation details for ParamHandler, BoundingBox, BoundingSphere and ProfileTimer to proper .inl and .cpp files. (more to follow)
- Update all forward declared enums with the required storage type (as requested by the C++11 standard)

334 Diff Diff IonutCava picture IonutCava Wed 10 Dec, 2014 17:21:22 +0000

[Ionut]
- Reorganized the Hardware project and renamed to "Platform"
- Reorganized the Server project

331 Diff Diff IonutCava picture IonutCava Sat 06 Dec, 2014 20:53:45 +0000

[Ionut]
- Limited line length to 132 characters to improve readability and diff-comparisons
- Refactored memory allocation/deallocation functions
- Fixed a few compatibility issues with HashMap.h
- Fixed a bug in GPU Skinning shaders (cast a float to int)

330 Diff Diff IonutCava picture IonutCava Fri 10 Oct, 2014 17:19:11 +0000

[Ionut]
- New rendering algorithm :
— Perform second pass for visible nodes gathering all render commands and uploading them to an indirect draw buffer
— Render geometry with indirect draw commands
— Removed per-node range binding of the node buffer. Instead, bind the entire buffer once and index data in shaders
— Use “baseInstance” parameter from IndirectDrawCommand as a node buffer index
— Prefer DSA extensions for updating buffer data where applicable
- Moved all rendering specific code from SceneGraphNode to a new RenderingComponent
- Optimized “toString” utility function

325 Diff Diff IonutCava picture IonutCava Wed 17 Sep, 2014 20:06:13 +0000

[Ionut]
- Reworked transform system:
— Parent transform chain system moved from Transform class to the PhysicsComponent
— PhysicsComponent now returns all transform values needed both global and local (still needs heavy optimization and caching)

- Reworked SceneGraph node management:
— Proper ref counting of SceneNodes and proper removing of resource (ResourceCache is now empty on destruction, as it should be)
— Removed parent transform tracking as that’s the PhysicsComponent’s responsibility
— Only nodes loaded via the ResourceCache are allowed to be added to the SceneGraph (added proper loaders for Impostors, Sky, etc)

- Optimized some of the math classes (mat4, mat3)

322 Diff Diff IonutCava picture IonutCava Sat 06 Sep, 2014 20:33:47 +0000

[Ionut]
- Refactored most of Boost related code to be C++11 based
— Boost is only used for ThreadPool, Mutex handling, XML parsing and networking (ASIO)
— Function binds, threads, regex, NonCopyable base, Hashing algorithms and more are now using C++11 libraries
- Replaced all FOR_EACH calls with standard, range, “for” calls

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