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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "stdafx.h"

#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/Configuration.h"
#include "Core/Headers/PlatformContext.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/Video/Textures/Headers/Texture.h"
#include "Core/Resources/Headers/ResourceCache.h"
#include "Managers/Headers/RenderPassManager.h"
#include "Rendering/Camera/Headers/Camera.h"
#include "Rendering/Lighting/Headers/Light.h"
#include "Rendering/Lighting/Headers/LightPool.h"
#include "Geometry/Shapes/Headers/Predefined/Quad3D.h"
#include "Managers/Headers/RenderPassManager.h"
#include "Platform/Video/Shaders/Headers/ShaderProgram.h"
#include "Platform/Video/Buffers/PixelBuffer/Headers/PixelBuffer.h"

namespace Divide {

DeferredShadingRenderer::DeferredShadingRenderer(PlatformContext& context, ResourceCache& cache)
    : Renderer(context, cache, RendererType::RENDERER_DEFERRED_SHADING), _cachedLightCount(0)
{

    WindowManager& winManager = context.app().windowManager();

    _lightTexture = _context.gfx().newPB();
    ResourceDescriptor deferred("DeferredShadingPass2");
    deferred.setThreadedLoading(false);
    _deferredShader = CreateResource<ShaderProgram>(cache, deferred);

    ResourceDescriptor deferredPreview("deferredPreview");
    deferredPreview.setThreadedLoading(false);
    _previewDeferredShader = CreateResource<ShaderProgram>(cache, deferredPreview);
    SamplerDescriptor gBufferSampler;
    gBufferSampler.setWrapMode(TextureWrap::CLAMP_TO_EDGE);
    gBufferSampler.setFilters(TextureFilter::NEAREST);

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

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

    TextureDescriptor depthDescriptor(TextureType::TEXTURE_2D,
                                      GFXImageFormat::DEPTH_COMPONENT,
                                      GFXDataFormat::UNSIGNED_INT);

    SamplerDescriptor depthSampler;
    depthSampler.setFilters(TextureFilter::NEAREST);
    depthSampler.setWrapMode(TextureWrap::CLAMP_TO_EDGE);
    depthDescriptor.setSampler(depthSampler);

    vectorImpl<RTAttachmentDescriptor> att = {
        { depthDescriptor, RTAttachmentType::Depth },
    };

    RenderTargetDescriptor desc = {};
    desc._name = "Deferred";
    desc._resolution = winManager.getActiveWindow().getDimensions();
    desc._attachmentCount = to_U8(att.size());
    desc._attachments = att.data();

    for (U8 i = 0; i < 4; ++i) {
        att.push_back(RTAttachmentDescriptor{ gBuffer[i], RTAttachmentType::Colour, i, (i == 0 ? DefaultColours::BLACK : DefaultColours::WHITE) });
    }

    _deferredBuffer = _context.gfx().renderTargetPool().allocateRT(desc);

    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>(cache, mrt));
    _renderQuads.push_back(CreateResource<Quad3D>(cache, mrt2));
    _renderQuads.push_back(CreateResource<Quad3D>(cache, mrt3));
    _renderQuads.push_back(CreateResource<Quad3D>(cache, mrt4));
    _renderQuads.push_back(CreateResource<Quad3D>(cache, mrtPreviewSmall));

    STUBBED("Shadow maps are currently disabled for Deferred Rendering! -Ionut")
    _context.gfx().shadowDetailLevel(RenderDetailLevel::OFF);

    RenderTarget& screenRT = _context.gfx().renderTargetPool().renderTarget(RenderTargetID(RenderTargetUsage::SCREEN));

    U16 width = screenRT.getWidth();
    U16 height = screenRT.getHeight();

    updateResolution(width, height);

    GUI& gui = _context.gui();
    gui.addText(_ID("PositionData"),                 // Unique ID
                      vec2<I32>(60, 60),             // Position
                      Font::DIVIDE_DEFAULT,          // Font
                      vec4<U8>(0, 64, 255, 255),     // Colour
                      "POSITION DATA");              // Text
    gui.addText(_ID("NormalData"),                   // Unique ID
                      vec2<I32>(60 + width / 2, 60), // Position
                      Font::DIVIDE_DEFAULT,          // Font
                      vec4<U8>(0, 64, 255, 255),     // Colour
                      "NORMAL DATA");                // Text
    gui.addText(_ID("FinalImage"),                    // Unique ID
                      vec2<I32>(60, 60 + height / 2), // Position
                      Font::DIVIDE_DEFAULT,           // Font
                      vec4<U8>(0, 64, 255, 255),      // Colour
                      "FINAL IMAGE");                 // Text
    gui.addText(_ID("LightTexture"),                               // Unique ID
                      vec2<I32>(60 + width / 2, 60 + height / 2),  // Position
                      Font::DIVIDE_DEFAULT,                        // Font
                      vec4<U8>(0, 64, 255, 255),                   // Colour
                      "LIGHT TEXTURE");                            // Text
}

DeferredShadingRenderer::~DeferredShadingRenderer()
{
    Console::printfn(Locale::get(_ID("DEFERRED_RT_DELETE")));
    _renderQuads.clear();
    _context.gfx().renderTargetPool().deallocateRT(_deferredBuffer);
}

void DeferredShadingRenderer::preRender(RenderTarget& target,
                                        LightPool& lightPool,
                                        GFX::CommandBuffer& bufferInOut) {
    Renderer::preRender(target, lightPool, bufferInOut);

    Light::LightList& lights = lightPool.getLights(LightType::POINT);

    if (lights.size() != _cachedLightCount) {
        _cachedLightCount = (U16)lights.size();
        _lightTexture->create(2, _cachedLightCount);
    }

    GFX::BeginPixelBufferCommand beginPixelBufferCmd;
    beginPixelBufferCmd._buffer = _lightTexture;
    beginPixelBufferCmd._command = [&lights](bufferPtr data) -> void {
        U8 index = 0;
        F32* pixels = (F32*)data;
        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 Colour
                pixels[index + 3] = lights[i]->getDiffuseColour().r;
                pixels[index + 4] = lights[i]->getDiffuseColour().g;
                pixels[index + 5] = lights[i]->getDiffuseColour().b;
                index += 6;
            }
        }
    };
    GFX::BeginPixelBuffer(bufferInOut, beginPixelBufferCmd);

    GFX::EndPixelBufferCommand endPixelBufferCmd;
    GFX::EndPixelBuffer(bufferInOut, endPixelBufferCmd);
}

void DeferredShadingRenderer::render(const DELEGATE_CBK<void, GFX::CommandBuffer&>& renderCallback,
                                     const SceneRenderState& sceneRenderState,
                                     GFX::CommandBuffer& bufferInOut) {
    firstPass(renderCallback, sceneRenderState, bufferInOut);
    secondPass(sceneRenderState, bufferInOut);
}

void DeferredShadingRenderer::firstPass(const DELEGATE_CBK<void, GFX::CommandBuffer&>& renderCallback,
                                        const SceneRenderState& sceneRenderState,
                                        GFX::CommandBuffer& bufferInOut) {
    // Pass 1
    // Draw the geometry, saving parameters into the buffer
    GFX::BeginRenderPassCommand beginRenderPassCmd;
    beginRenderPassCmd._target = _deferredBuffer._targetID;
    GFX::BeginRenderPass(bufferInOut, beginRenderPassCmd);

        renderCallback(bufferInOut);

    GFX::EndRenderPassCommand endRenderPassCmd;
    GFX::EndRenderPass(bufferInOut, endRenderPassCmd);
}

void DeferredShadingRenderer::secondPass(const SceneRenderState& sceneRenderState,
                                         GFX::CommandBuffer& bufferInOut) {
    // Pass 2
    // Draw a 2D fullscreen quad with lighting shader applied and all generated
    // textures bound to that shader
    GFX::SetCameraCommand setCameraCommand;
    setCameraCommand._camera = Camera::utilityCamera(Camera::UtilityCamera::_2D);
    GFX::SetCamera(bufferInOut, setCameraCommand);

    TextureData texData0 = _deferredBuffer._rt->getAttachment(RTAttachmentType::Colour, 0).texture()->getData();
    TextureData texData1 = _deferredBuffer._rt->getAttachment(RTAttachmentType::Colour, 1).texture()->getData();
    TextureData texData2 = _deferredBuffer._rt->getAttachment(RTAttachmentType::Colour, 2).texture()->getData();
    TextureData texData3 = _deferredBuffer._rt->getAttachment(RTAttachmentType::Colour, 3).texture()->getData();
    TextureData lightData = _lightTexture->getData();
    
    GFX::BindDescriptorSetsCommand descriptorSetCmd;
    descriptorSetCmd._set._textureData.addTexture(texData0, 0);
    descriptorSetCmd._set._textureData.addTexture(texData1, 1);
    descriptorSetCmd._set._textureData.addTexture(texData2, 2);
    descriptorSetCmd._set._textureData.addTexture(texData3, 3);
    descriptorSetCmd._set._textureData.addTexture(lightData, 4);
    GFX::BindDescriptorSets(bufferInOut, descriptorSetCmd);

    PipelineDescriptor pipelineDescriptor;
    pipelineDescriptor._stateHash = _context.gfx().getDefaultStateBlock(true);
    pipelineDescriptor._shaderProgram = _previewDeferredShader;

    Pipeline pipeline = _context.gfx().newPipeline(pipelineDescriptor);
    GFX::BindPipelineCommand pipelineCmd;
    pipelineCmd._pipeline = pipeline;
    GFX::BindPipeline(bufferInOut, pipelineCmd);

    GFX::SendPushConstantsCommand pushConstantsCommand;

    GenericDrawCommand cmd;
    if (_debugView) {
        const RenderStagePass& stagePass = _context.gfx().getRenderStage();

        pushConstantsCommand._constants.set("texDiffuse0", PushConstantType::UINT, 4);
        GFX::SendPushConstants(bufferInOut, pushConstantsCommand);
        cmd.sourceBuffer(_renderQuads[1]->getGeometryVB());
        {
            GFX::DrawCommand drawCmd;
            drawCmd._drawCommands.push_back(cmd);
            GFX::AddDrawCommands(bufferInOut, drawCmd);
        }
        pushConstantsCommand._constants.set("texDiffuse0", PushConstantType::UINT, 1);
        GFX::SendPushConstants(bufferInOut, pushConstantsCommand);
        cmd.sourceBuffer(_renderQuads[2]->getGeometryVB());
        {
            GFX::DrawCommand drawCmd;
            drawCmd._drawCommands.push_back(cmd);
            GFX::AddDrawCommands(bufferInOut, drawCmd);
        }
        pushConstantsCommand._constants.set("texDiffuse0", PushConstantType::UINT, 2);
        GFX::SendPushConstants(bufferInOut, pushConstantsCommand);
        cmd.sourceBuffer(_renderQuads[3]->getGeometryVB());
        {
            GFX::DrawCommand drawCmd;
            drawCmd._drawCommands.push_back(cmd);
            GFX::AddDrawCommands(bufferInOut, drawCmd);
        }
    }

    pipelineDescriptor._shaderProgram = _deferredShader;
    pipeline = _context.gfx().newPipeline(pipelineDescriptor);
    pipelineCmd._pipeline = pipeline;
    GFX::BindPipeline(bufferInOut, pipelineCmd);

    pushConstantsCommand._constants.set("lightCount", PushConstantType::INT, (I32)_cachedLightCount);
    GFX::SendPushConstants(bufferInOut, pushConstantsCommand);
    cmd.sourceBuffer(_renderQuads[_debugView ? 4 : 0]->getGeometryVB());
    {
        GFX::DrawCommand drawCmd;
        drawCmd._drawCommands.push_back(cmd);
        GFX::AddDrawCommands(bufferInOut, drawCmd);
    }
    GUI& gui = _context.gui();
    GUIElement* guiElement = gui.getGUIElement(0, _ID("FinalImage"));
    if (guiElement) {
        guiElement->setVisible(_debugView);
    }
    guiElement = gui.getGUIElement(0, _ID("LightTexture"));
    if (guiElement) {
        guiElement->setVisible(_debugView);
    }
    guiElement = gui.getGUIElement(0, _ID("PositionData"));
    if (guiElement) {
        guiElement->setVisible(_debugView);
    }
    guiElement = gui.getGUIElement(0, _ID("NormalData"));
    if (guiElement) {
        guiElement->setVisible(_debugView);
    }
}

void DeferredShadingRenderer::updateResolution(U16 width, U16 height) {
    _deferredBuffer._rt->resize(width, height);

    F32 widthF = to_F32(width);
    F32 heightF = to_F32(height);

    _renderQuads[0]->setDimensions(vec4<F32>(0.0f, 0.0f, widthF, heightF));

    _renderQuads[1]->setDimensions(vec4<F32>(widthF / 2, 0.0f, widthF, heightF / 2));

    _renderQuads[2]->setCorner(Quad3D::CornerLocation::TOP_LEFT,
                               vec3<F32>(0.0f, heightF, 0.0f));
    _renderQuads[2]->setCorner(Quad3D::CornerLocation::TOP_RIGHT,
                               vec3<F32>(widthF / 2, heightF, 0U));
    _renderQuads[2]->setCorner(Quad3D::CornerLocation::BOTTOM_LEFT,
                               vec3<F32>(0.0f, heightF / 2, 0.0f));
    _renderQuads[2]->setCorner(Quad3D::CornerLocation::BOTTOM_RIGHT,
                               vec3<F32>(widthF / 2, heightF / 2, 0U));

    _renderQuads[3]->setCorner(Quad3D::CornerLocation::TOP_LEFT,
                               vec3<F32>(widthF / 2, heightF, 0.0f));
    _renderQuads[3]->setCorner(Quad3D::CornerLocation::TOP_RIGHT,
                               vec3<F32>(widthF, heightF, 0.0f));
    _renderQuads[3]->setCorner(Quad3D::CornerLocation::BOTTOM_LEFT,
                               vec3<F32>(widthF / 2, heightF / 2, 0.0f));
    _renderQuads[3]->setCorner(Quad3D::CornerLocation::BOTTOM_RIGHT,
                               vec3<F32>(widthF, heightF / 2, 0.0f));
    // 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.0f, 0.0f, widthF / 2, heightF / 2));
}
};

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

Diff revisions: vs.
Revision Author Commited Message
949 Diff Diff IonutCava picture IonutCava Thu 19 Oct, 2017 21:48:37 +0000

[Ionut]
- More command buffer refactoring

948 Diff Diff IonutCava picture IonutCava Tue 17 Oct, 2017 22:41:09 +0000

[Ionut]
- Move rendering data caching outside of CommandBuffer and into RenderPackage for better control.

947 Diff Diff IonutCava picture IonutCava Sun 08 Oct, 2017 22:36:54 +0000

[Ionut]
- Refactor command buffer pooling
- Remove all static commands in an attempt to fix rendering issues. Profile and rework later.

938 Diff Diff IonutCava picture IonutCava Wed 16 Aug, 2017 21:35:55 +0000

[Ionut]
- Separate texture binding slot from TextureData
- Binding slot is now a property of the descriptor set
- Remove bind() calls from texture’s public interface
ToDo
- Add image layer binding options (addTextureLayer(data, binding, layer, ...)

937 Diff Diff IonutCava picture IonutCava Wed 16 Aug, 2017 19:14:24 +0000

[Ionut]
- Add PixelBuffer commands using lambdas for buffer updates
- Use Attorneys for glPixelBuffer and glFramebuffer begin/end calls

935 Diff Diff IonutCava picture IonutCava Tue 15 Aug, 2017 21:10:25 +0000

[IonutCava]
- Added debug scope (GL’s debug group) commands
- More bug fixes and optimizations

933 Diff Diff IonutCava picture IonutCava Tue 15 Aug, 2017 00:02:05 +0000

[Ionut]
- CommandBuffer Part 2/3: code compiles and splash screen renders and application doesn’t crash (still has drawing issues)

929 Diff Diff IonutCava picture IonutCava Sat 12 Aug, 2017 17:34:44 +0000

[Ionut]
- More work on finishing up new PushConstant / Command buffer rendering system:
— Everything compiles and runs but with graphical and performance issues

924 Diff Diff IonutCava picture IonutCava Wed 02 Aug, 2017 20:29:19 +0000

[Ionut]
- Rework RenderTarget binding. Now access is only available via the GFXRTPool
- Split RenderPassManager::doCustomPass into multiple steps for easier debugging and cleaner code
- Other small code cleanups

912 IonutCava picture IonutCava Sun 02 Jul, 2017 23:42:39 +0000

[Ionut]
- Add experimental Weighted Blended Order Independed Transparency (ref: http://casual-effects.blogspot.co.uk/2015/03/implemented-weighted-blended-order.html)
— Add per drawbuffer blend
— All translucent renderbin items go via the new OIT 2-step rendering: accumulation and composition
- Make sure we have proper blend enabled for text rendering
- Add a primitive form of PushConstants (unused yet. Emulated via Uniform calls)
- Fix bug with XMLParser not using case insensitive key lookups