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
327
328
329
330
331
332
333
334
335
#include "stdafx.h"

#include "config.h"

#include "Headers/GFXDevice.h"

#include "GUI/Headers/GUI.h"
#include "GUI/Headers/GUIText.h"
#include "GUI/Headers/GUIFlash.h"

#include "Rendering/Headers/Renderer.h"

#include "Managers/Headers/SceneManager.h"
#include "Managers/Headers/RenderPassManager.h"

#include "Core/Headers/PlatformContext.h"
#include "Core/Time/Headers/ProfileTimer.h"

#include "Platform/Video/Headers/IMPrimitive.h"
#include "Platform/Video/Textures/Headers/Texture.h"
#include "Platform/Video/Shaders/Headers/ShaderProgram.h"
#include "Platform/Video/Buffers/ShaderBuffer/Headers/ShaderBuffer.h"

namespace Divide {

void GFXDevice::uploadGPUBlock() {
    if (_gpuBlock._needsUpload) {
        // We flush the entire buffer on update to inform the GPU that we don't
        // need the previous data. Might avoid some driver sync
        _gfxDataBuffer->writeData(&_gpuBlock._data);
        _gfxDataBuffer->bind(ShaderBufferLocation::GPU_BLOCK);
        _gpuBlock._needsUpload = false;
        _api->updateClipPlanes();
    }
}

void GFXDevice::renderQueueToSubPasses(RenderBinType queueType, GFX::CommandBuffer& subPassCmd) {
    RenderPackageQueue& renderQueue = *_renderQueues[queueType._to_integral()].get();

    assert(renderQueue.locked() == false);
    if (!renderQueue.empty()) {
        U32 queueSize = renderQueue.size();
        for (U32 idx = 0; idx < queueSize; ++idx) {
            subPassCmd.add(renderQueue.getPackage(idx).commands());
        }
        renderQueue.clear();
    }
}

void GFXDevice::flushCommandBuffer(GFX::CommandBuffer& commandBuffer) {
    _api->flushCommandBuffer(commandBuffer);
}

void GFXDevice::flushAndClearCommandBuffer(GFX::CommandBuffer& commandBuffer) {
    flushCommandBuffer(commandBuffer);
    commandBuffer.clear();
}

void GFXDevice::lockQueue(RenderBinType type) {
    _renderQueues[type._to_integral()]->lock();
}

void GFXDevice::unlockQueue(RenderBinType type) {
    _renderQueues[type._to_integral()]->unlock();
}

U32 GFXDevice::renderQueueSize(RenderBinType queueType) {
    U32 queueIndex = queueType._to_integral();
    assert(_renderQueues[queueIndex]->locked() == false);

    return _renderQueues[queueIndex]->size();
}

void GFXDevice::addToRenderQueue(RenderBinType queueType, const RenderPackage& package) {
    if (!package.isRenderable()) {
        return;
    }

    U32 queueIndex = queueType._to_integral();

    assert(_renderQueues[queueIndex]->locked() == true);

    _renderQueues[queueIndex]->push_back(package);
}

/// Prepare the list of visible nodes for rendering
GFXDevice::NodeData& GFXDevice::processVisibleNode(const SceneGraphNode& node, U32 dataIndex, bool isOcclusionCullable) {
    NodeData& dataOut = _matricesData[dataIndex];

    RenderingComponent* const renderable = node.get<RenderingComponent>();
    AnimationComponent* const animComp   = node.get<AnimationComponent>();
    PhysicsComponent*   const transform  = node.get<PhysicsComponent>();

    // Extract transform data (if available)
    // (Nodes without transforms are considered as using identity matrices)
    if (transform) {
        // ... get the node's world matrix properly interpolated
        dataOut._worldMatrix.set(transform->getWorldMatrix(getFrameInterpolationFactor()));

        dataOut._normalMatrixWV.set(dataOut._worldMatrix);
        if (!transform->isUniformScaled()) {
            // Non-uniform scaling requires an inverseTranspose to negate
            // scaling contribution but preserve rotation
            dataOut._normalMatrixWV.setRow(3, 0.0f, 0.0f, 0.0f, 1.0f);
            dataOut._normalMatrixWV.inverseTranspose();
            dataOut._normalMatrixWV.mat[15] = 0.0f;
        }
        dataOut._normalMatrixWV.setRow(3, 0.0f, 0.0f, 0.0f, 0.0f);

        // Calculate the normal matrix (world * view)
        dataOut._normalMatrixWV *= getMatrix(MATRIX::VIEW);
    }

    // Since the normal matrix is 3x3, we can use the extra row and column to store additional data
    dataOut._normalMatrixWV.element(0, 3) = to_F32(animComp ? animComp->boneCount() : 0);
    dataOut._normalMatrixWV.setRow(3, node.get<BoundsComponent>()->getBoundingSphere().asVec4());
    // Get the material property matrix (alpha test, texture count, texture operation, etc.)
    renderable->getRenderingProperties(dataOut._properties, dataOut._normalMatrixWV.element(1, 3), dataOut._normalMatrixWV.element(2, 3));
    // Get the colour matrix (diffuse, specular, etc.)
    renderable->getMaterialColourMatrix(dataOut._colourMatrix);

    //set properties.w to -1 to skip occlusion culling for the node
    dataOut._properties.w = isOcclusionCullable ? 1.0f : -1.0f;

    return dataOut;
}

void GFXDevice::buildDrawCommands(const RenderQueue::SortedQueues& sortedNodes,
                                  SceneRenderState& sceneRenderState,
                                  RenderPass::BufferData& bufferData,
                                  bool refreshNodeData)
{
    Time::ScopedTimer timer(_commandBuildTimer);
    // If there aren't any nodes visible in the current pass, don't update anything (but clear the render queue

    RenderStagePass currentStage = getRenderStage();
    if (refreshNodeData) {
        bufferData._lastCommandCount = 0;
        bufferData._lasNodeCount = 0;
    }

    if (currentStage.stage() == RenderStage::SHADOW) {
        Light* shadowLight = LightPool::currentShadowCastingLight();
        assert(shadowLight != nullptr);
        if (!COMPARE(_gpuBlock._data._renderProperties.x, shadowLight->getShadowProperties()._arrayOffset.x)) {
            _gpuBlock._data._renderProperties.x = to_F32(shadowLight->getShadowProperties()._arrayOffset.x);
            _gpuBlock._needsUpload = true;
        }
        U8 shadowPasses = shadowLight->getLightType() == LightType::DIRECTIONAL
                                                       ? shadowLight->getShadowMapInfo()->numLayers()
                                                       : 1;
        if (!COMPARE(_gpuBlock._data._renderProperties.y, to_F32(shadowPasses))) {
            _gpuBlock._data._renderProperties.y = to_F32(shadowPasses);
            _gpuBlock._needsUpload = true;
        }
    }

    U32 nodeCount = 0;
    U32 cmdCount = 0;

    for (const vectorImpl<SceneGraphNode*>& queue : sortedNodes) {
        std::for_each(std::begin(queue), std::end(queue),
            [&](SceneGraphNode* node) -> void
            {
                RenderingComponent& renderable = *node->get<RenderingComponent>();
                Attorney::RenderingCompGFXDevice::prepareDrawPackage(renderable, sceneRenderState, currentStage);
            });

        std::for_each(std::begin(queue), std::end(queue),
            [&](SceneGraphNode* node) -> void
            {
                RenderingComponent& renderable = *node->get<RenderingComponent>();

                const RenderPackage& pkg = Attorney::RenderingCompGFXDevice::getDrawPackage(renderable, currentStage);
                if (pkg.isRenderable()) {
                    if (refreshNodeData) {
                        Attorney::RenderingCompGFXDevice::setDrawIDs(renderable, currentStage, cmdCount, nodeCount);

                        processVisibleNode(*node, nodeCount, pkg.isOcclusionCullable());

                        for (I32 cmdIdx = 0; cmdIdx < pkg.drawCommandCount(); ++cmdIdx) {
                            const GFX::DrawCommand& cmd = pkg.drawCommand(cmdIdx);
                            for (const GenericDrawCommand& drawCmd : cmd._drawCommands) {
                                for (U32 i = 0; i < drawCmd.drawCount(); ++i) {
                                    _drawCommandsCache[cmdCount++].set(drawCmd.cmd());
                                }
                            }
                        }
                    }
                    nodeCount++;
                }
            });
    }

    if (refreshNodeData) {
        bufferData._lastCommandCount = cmdCount;
        bufferData._lasNodeCount = nodeCount;

        assert(cmdCount >= nodeCount);
        // If the buffer update required is large enough, just replace the entire thing
        if (nodeCount > Config::MAX_VISIBLE_NODES / 2) {
            bufferData._renderData->writeData(_matricesData.data());
        } else {
            // Otherwise, just update the needed range to save bandwidth
            bufferData._renderData->writeData(0, nodeCount, _matricesData.data());
        }

        ShaderBuffer& cmdBuffer = *bufferData._cmdBuffer;
        cmdBuffer.writeData(_drawCommandsCache.data());
        _api->registerCommandBuffer(cmdBuffer);

        // This forces a sync for each buffer to make sure all data is properly uploaded in VRAM
        bufferData._renderData->bind(ShaderBufferLocation::NODE_INFO);
    }
}

void GFXDevice::occlusionCull(const RenderPass::BufferData& bufferData,
                              const Texture_ptr& depthBuffer,
                              GFX::CommandBuffer& bufferInOut) {
    static const U32 GROUP_SIZE_AABB = 64;

    GFX::BindPipelineCommand bindPipelineCmd;
    PipelineDescriptor pipelineDescriptor;
    pipelineDescriptor._shaderProgram = _HIZCullProgram;
    bindPipelineCmd._pipeline = newPipeline(pipelineDescriptor);
    GFX::BindPipeline(bufferInOut, bindPipelineCmd);

    ShaderBufferBinding shaderBuffer;
    shaderBuffer._binding = ShaderBufferLocation::GPU_COMMANDS;
    shaderBuffer._buffer = bufferData._cmdBuffer;
    shaderBuffer._range.set(0, bufferData._cmdBuffer->getPrimitiveCount());
    shaderBuffer._atomicCounter.first = true;
    
    GFX::BindDescriptorSetsCommand bindDescriptorSetsCmd;
    bindDescriptorSetsCmd._set._shaderBuffers.push_back(shaderBuffer);
    bindDescriptorSetsCmd._set._textureData.addTexture(depthBuffer->getData(),
                                                       to_U8(ShaderProgram::TextureUsage::DEPTH));
    GFX::BindDescriptorSets(bufferInOut, bindDescriptorSetsCmd);
    
    U32 cmdCount = bufferData._lastCommandCount;

    GFX::SendPushConstantsCommand sendPushConstantsCmd;
    PushConstant constant;
    constant._type = PushConstantType::UINT;
    constant._binding = "dvd_numEntities";
    constant._values = { cmdCount };
    sendPushConstantsCmd._constants = PushConstants(constant);
    GFX::SendPushConstants(bufferInOut, sendPushConstantsCmd);

    GFX::DispatchComputeCommand computeCmd;
    computeCmd._params._barrierType = MemoryBarrierType::COUNTER;
    computeCmd._params._groupSize = vec3<U32>((cmdCount + GROUP_SIZE_AABB - 1) / GROUP_SIZE_AABB, 1, 1);
    GFX::AddComputeCommand(bufferInOut, computeCmd);
}

U32 GFXDevice::getLastCullCount() const {
    const RenderPass::BufferData& bufferData = parent().renderPassManager().getBufferData(RenderStage::DISPLAY, 0);

    U32 cullCount = bufferData._cmdBuffer->getAtomicCounter();
    if (cullCount > 0) {
        bufferData._cmdBuffer->resetAtomicCounter();
    }
    return cullCount;
}

void GFXDevice::drawText(const TextElementBatch& batch, GFX::CommandBuffer& bufferInOut) {
    GFX::BindPipelineCommand bindPipelineCmd;
    bindPipelineCmd._pipeline = _textRenderPipeline;

    GFX::SendPushConstantsCommand pushConstantsCommand;
    pushConstantsCommand._constants = _textRenderConstants;

    GFX::DrawTextCommand drawTextCommand;
    drawTextCommand._batch = batch;

    GFX::SetCameraCommand setCameraCommand;
    setCameraCommand._camera = Camera::utilityCamera(Camera::UtilityCamera::_2D);
    GFX::SetCamera(bufferInOut, setCameraCommand);
    GFX::BindPipeline(bufferInOut, bindPipelineCmd);
    GFX::SendPushConstants(bufferInOut, pushConstantsCommand);
    GFX::AddDrawTextCommand(bufferInOut, drawTextCommand);
}

void GFXDevice::drawText(const TextElementBatch& batch) {
    GFX::ScopedCommandBuffer sBuffer(GFX::allocateScopedCommandBuffer());

    drawText(batch, sBuffer());
    flushCommandBuffer(sBuffer());
}

void GFXDevice::flushDisplay(const vec4<I32>& targetViewport) {
    GFX::ScopedCommandBuffer sBuffer(GFX::allocateScopedCommandBuffer());
    GFX::CommandBuffer& buffer = sBuffer();

    PipelineDescriptor pipelineDescriptor;
    pipelineDescriptor._stateHash = get2DStateBlock();
    pipelineDescriptor._shaderProgram = _displayShader;

    GenericDrawCommand triangleCmd;
    triangleCmd.primitiveType(PrimitiveType::TRIANGLES);
    triangleCmd.drawCount(1);

    GFX::BindPipelineCommand bindPipelineCmd;
    bindPipelineCmd._pipeline = newPipeline(pipelineDescriptor);
    GFX::BindPipeline(buffer, bindPipelineCmd);

    RenderTarget& screen = _rtPool->renderTarget(RenderTargetID(RenderTargetUsage::SCREEN));
    TextureData texData = screen.getAttachment(RTAttachmentType::Colour, to_U8(ScreenTargets::ALBEDO)).texture()->getData();

    GFX::BindDescriptorSetsCommand bindDescriptorSetsCmd;
    bindDescriptorSetsCmd._set._textureData.addTexture(texData, to_U8(ShaderProgram::TextureUsage::UNIT0));
    GFX::BindDescriptorSets(buffer, bindDescriptorSetsCmd);
    
    GFX::SetViewportCommand viewportCommand;
    viewportCommand._viewport.set(targetViewport);
    GFX::SetViewPort(buffer, viewportCommand);

    // Blit render target to screen
    GFX::DrawCommand drawCmd;
    drawCmd._drawCommands.push_back(triangleCmd);
    GFX::AddDrawCommands(buffer, drawCmd);

    GFX::SetCameraCommand setCameraCommand;
    setCameraCommand._camera = Camera::utilityCamera(Camera::UtilityCamera::_2D);
    GFX::SetCamera(buffer, setCameraCommand);
    flushCommandBuffer(buffer);
    
    // Render all 2D debug info and call API specific flush function
    ReadLock r_lock(_2DRenderQueueLock);
    for (std::pair<U32, GUID2DCbk>& callbackFunction : _2dRenderQueue) {
        callbackFunction.second.second();
    }
}

};

Commits for Divide-Framework/trunk/Source Code/Platform/Video/GFXDeviceDraw.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.

946 Diff Diff IonutCava picture IonutCava Sun 01 Oct, 2017 21:25:10 +0000

[Ionut]
- Attempt to fix text rendering / flashing issue (improved but not fixed yet)

943 Diff Diff IonutCava picture IonutCava Tue 12 Sep, 2017 11:38:52 +0000

[IonutCava]
- Rework ThreadPool/Task system for clearer parent<->child relationship between tasks
— Disable Prio pool since it doesn’t work properly with the newest boost libraries
— Modify the C++11 threadpool to remove the boost dependency
- Reduce memory usage by allocating command buffers on use instead of on creation

940 Diff Diff IonutCava picture IonutCava Fri 18 Aug, 2017 16:18:44 +0000

[Ionut]
- Generate GFX::CommandBuffers only from GFX::CommandBufferPools.
— We shouldn’t be allocating any new GFX::CommandBuffers at runtime now.
— ToDo: Add a similar system for Pipelines
- Add 2 command buffer pools to GFXDevice: a primary one used for rendering and a large, secondary one used for command generation

939 Diff Diff IonutCava picture IonutCava Thu 17 Aug, 2017 21:20:58 +0000

[Ionut]
- Actually process the drawText command
- Remove Eclipse project files since Visual Studio Code is a much better alternative

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, ...)

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)

932 IonutCava picture IonutCava Mon 14 Aug, 2017 16:17:59 +0000

[Ionut]
- Transition every GPU call to the new GFX::CommandBuffer system.
- Part 1/3: Infrastructure. Does not compile yet; (ToDo: part 2/3: transition commands to new infrastructure. part 3/3: debug and optimize)

Currently supported commands:
BEGIN_RENDER_PASS
END_RENDER_PASS
BEGIN_RENDER_SUB_PASS
END_RENDER_SUB_PASS
SET_VIEWPORT
SET_SCISSOR
BIND_PIPELINE
BIND_DESCRIPTOR_SETS
SEND_PUSH_CONSTANTS
DRAW_COMMANDS
DRAW_TEXT
DISPATCH_COMPUTE