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
#include "stdafx.h"

#include "Headers/GFXDevice.h"

#include "GUI\Headers\GUI.h"
#include "Core/Headers/Kernel.h"
#include "Core/Headers/Application.h"
#include "Core/Headers/ParamHandler.h"
#include "Scenes/Headers/SceneState.h"
#include "Utility/Headers/TextLabel.h"
#include "Core/Time/Headers/ApplicationTimer.h"
#include "Core/Resources/Headers/ResourceCache.h"
#include "Rendering/Camera/Headers/Camera.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/RenderTarget/Headers/RenderTarget.h"

namespace Divide {

void GFXDevice::renderDebugViews(GFX::CommandBuffer& bufferInOut) {
    static DebugView* HiZPtr;
    static size_t labelStyleHash = TextLabelStyle(Font::DROID_SERIF_BOLD, UColour(255), 96).getHash();

    // As this is touched once per frame, we'll only enable it in debug builds
    if (Config::Build::IS_DEBUG_BUILD) {
        // Early out if we didn't request the preview
        if (!ParamHandler::instance().getParam<bool>(_ID("rendering.previewDebugViews"), false)) {
            return;
        }

        // Lazy-load preview shader
        if (!_previewDepthMapShader) {
            // The LinearDepth variant converts the depth values to linear values
            // between the 2 scene z-planes
            ResourceDescriptor fbPreview("fbPreview.LinearDepth.ScenePlanes");
            fbPreview.setPropertyList("USE_SCENE_ZPLANES");
            _previewDepthMapShader = CreateResource<ShaderProgram>(parent().resourceCache(), fbPreview);
            assert(_previewDepthMapShader != nullptr);

            DebugView_ptr HiZ = std::make_shared<DebugView>();
            HiZ->_shader = _previewDepthMapShader;
            HiZ->_texture = renderTargetPool().renderTarget(RenderTargetID(RenderTargetUsage::SCREEN)).getAttachment(RTAttachmentType::Depth, 0).texture();
            HiZ->_name = "Hierarchical-Z";
            HiZ->_shaderData.set("lodLevel", PushConstantType::FLOAT, to_F32(HiZ->_texture->getMaxMipLevel() - 1));

            DebugView_ptr DepthPreview = std::make_shared<DebugView>();
            DepthPreview->_shader = _previewDepthMapShader;
            DepthPreview->_texture = renderTargetPool().renderTarget(RenderTargetID(RenderTargetUsage::SCREEN)).getAttachment(RTAttachmentType::Depth, 0).texture();
            DepthPreview->_shaderData.set("Depth Buffer", PushConstantType::FLOAT, 0.0f);

            DebugView_ptr NormalPreview = std::make_shared<DebugView>();
            NormalPreview->_shader = _renderTargetDraw;
            NormalPreview->_texture = renderTargetPool().renderTarget(RenderTargetID(RenderTargetUsage::SCREEN)).getAttachment(RTAttachmentType::Colour, to_U8(ScreenTargets::NORMALS)).texture();
            NormalPreview->_name = "Normals";
            NormalPreview->_shaderData.set("linearSpace", PushConstantType::UINT, 0u);
            NormalPreview->_shaderData.set("unpack2Channel", PushConstantType::UINT, 1u);

            DebugView_ptr VelocityPreview = std::make_shared<DebugView>();
            VelocityPreview->_shader = _renderTargetDraw;
            VelocityPreview->_texture = renderTargetPool().renderTarget(RenderTargetID(RenderTargetUsage::SCREEN)).getAttachment(RTAttachmentType::Colour, to_U8(ScreenTargets::VELOCITY)).texture();
            VelocityPreview->_name = "Velocity Map";
            VelocityPreview->_shaderData.set("linearSpace", PushConstantType::UINT, 0u);
            VelocityPreview->_shaderData.set("unpack2Channel", PushConstantType::UINT, 0u);

            DebugView_ptr AlphaAccumulation = std::make_shared<DebugView>();
            AlphaAccumulation->_shader = _renderTargetDraw;
            AlphaAccumulation->_texture = renderTargetPool().renderTarget(RenderTargetID(RenderTargetUsage::OIT)).getAttachment(RTAttachmentType::Colour, to_U8(ScreenTargets::ALBEDO)).texture();
            AlphaAccumulation->_name = "Alpha Accumulation";
            AlphaAccumulation->_shaderData.set("linearSpace", PushConstantType::UINT, 1u);
            AlphaAccumulation->_shaderData.set("unpack2Channel", PushConstantType::UINT, 0u);

            addDebugView(HiZ);
            addDebugView(DepthPreview);
            addDebugView(NormalPreview);
            addDebugView(VelocityPreview);
            addDebugView(AlphaAccumulation);
            HiZPtr = HiZ.get();
        }

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

        if (HiZPtr) {
            //HiZ preview
            I32 LoDLevel = 0;
            if (Config::USE_HIZ_CULLING && Config::USE_Z_PRE_PASS) {
                LoDLevel = to_I32(std::ceil(Time::ElapsedMilliseconds() / 750.0f)) %
                    (screenRT.getAttachment(RTAttachmentType::Depth, 0).texture()->getMaxMipLevel() - 1);
            }
            HiZPtr->_shaderData.set("lodLevel", PushConstantType::FLOAT, to_F32(LoDLevel));
        }

        constexpr I32 maxViewportColumnCount = 10;
        I32 viewCount = to_I32(_debugViews.size());
        I32 columnCount = std::min(viewCount, maxViewportColumnCount);
        I32 rowCount = viewCount / maxViewportColumnCount;
        if (viewCount % maxViewportColumnCount > 0) {
            rowCount++;
        }

        U16 screenWidth = std::max(screenRT.getWidth(), to_U16(1280));
        U16 screenHeight = std::max(screenRT.getHeight(), to_U16(720));
        F32 aspectRatio = to_F32(screenWidth) / screenHeight;

        I32 viewportWidth = screenWidth / maxViewportColumnCount;
        I32 viewportHeight = to_I32(viewportWidth / aspectRatio);
        Rect<I32> viewport(screenWidth - viewportWidth, 0, viewportWidth, viewportHeight);

        PipelineDescriptor pipelineDesc;
        pipelineDesc._stateHash = _defaultStateBlockHash;

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

        vectorImplFast <std::pair<stringImpl, Rect<I32>>> labelStack;

        Rect<I32> crtViewport = getCurrentViewport();
        GFX::SetViewportCommand setViewport;
        GFX::SendPushConstantsCommand pushConstants;
        GFX::BindPipelineCommand bindPipeline;
        GFX::BindDescriptorSetsCommand bindDescriptorSets;
        GFX::DrawCommand drawCommand;
        drawCommand._drawCommands.push_back(triangleCmd);

        I32 viewIndex = 0;
        for (U8 i = 0; i < rowCount; ++i) {
            for (U8 j = 0; j < columnCount; ++j) {
                DebugView& view = *_debugViews[viewIndex];
                pipelineDesc._shaderProgram = view._shader;

                bindPipeline._pipeline = &newPipeline(pipelineDesc);
                GFX::BindPipeline(bufferInOut, bindPipeline);

                pushConstants._constants = view._shaderData;
                GFX::SendPushConstants(bufferInOut, pushConstants);

                setViewport._viewport.set(viewport);
                GFX::SetViewPort(bufferInOut, setViewport);

                bindDescriptorSets._set._textureData.clear();
                bindDescriptorSets._set._textureData.addTexture(view._texture->getData(),
                                                                view._textureBindSlot);
                GFX::BindDescriptorSets(bufferInOut, bindDescriptorSets);

                GFX::AddDrawCommands(bufferInOut, drawCommand);

                if (!view._name.empty()) {
                    labelStack.emplace_back(view._name, viewport);
                }

                viewport.x -= viewportWidth;
                if (viewIndex++ == viewCount) {
                    return;
                }
            }
            viewport.y += viewportHeight;
        }

        TextElement text(labelStyleHash, RelativePosition2D(RelativeValue(0.1f, 0.0f), RelativeValue(0.1f, 0.0f)));
        for (const std::pair<stringImpl, Rect<I32>>& entry : labelStack) {
            // Draw labels at the end to reduce number of state changes
            setViewport._viewport.set(entry.second);
            GFX::SetViewPort(bufferInOut, setViewport);

            text._position.d_y.d_offset = entry.second.sizeY - 10.0f;
            text.text(entry.first);
            drawText(text, bufferInOut);
        }
    }
}


void GFXDevice::addDebugView(const std::shared_ptr<DebugView>& view) {
    _debugViews.push_back(view);
    if (_debugViews.back()->_sortIndex == -1) {
        _debugViews.back()->_sortIndex = to_I16(_debugViews.size());
    }
    std::sort(std::begin(_debugViews),
              std::end(_debugViews),
              [](const std::shared_ptr<DebugView>& a, const std::shared_ptr<DebugView>& b)-> bool {
                  return a->_sortIndex < b->_sortIndex;
               });
}

void GFXDevice::drawDebugFrustum(GFX::CommandBuffer& bufferInOut) {
    if (_debugFrustum != nullptr) {
        vectorImpl<vec3<F32>> corners;
        _debugFrustum->getCornersViewSpace(corners);

        vectorImpl<Line> lines;
        for (U8 i = 0; i < 4; ++i) {
            // Draw Near Plane
            lines.emplace_back(corners[i], corners[(i + 1) % 4], DefaultColours::RED_U8);
            // Draw Far Plane
            lines.emplace_back(corners[i + 4], corners[(i + 4 + 1) % 4], DefaultColours::RED_U8);
            // Connect Near Plane with Far Plane
            lines.emplace_back(corners[i], corners[(i + 4) % 8], DefaultColours::GREEN_U8);
        }

        _debugFrustumPrimitive->fromLines(lines);
        bufferInOut.add(_debugFrustumPrimitive->toCommandBuffer());
    }
}

/// Render all of our immediate mode primitives. This isn't very optimised and
/// most are recreated per frame!
void GFXDevice::debugDraw(const SceneRenderState& sceneRenderState, const Camera& activeCamera, GFX::CommandBuffer& bufferInOut) {
    if (Config::Build::IS_DEBUG_BUILD) {
        drawDebugFrustum(bufferInOut);

        // Debug axis form the axis arrow gizmo in the corner of the screen
        // This is togglable, so check if it's actually requested
        if (BitCompare(to_U32(sceneRenderState.gizmoState()),
                       to_base(SceneRenderState::GizmoState::SCENE_GIZMO))) {

            // Apply the inverse view matrix so that it cancels out in the shader
            // Submit the draw command, rendering it in a tiny viewport in the lower
            // right corner
            U16 windowWidth = renderTargetPool().renderTarget(RenderTargetID(RenderTargetUsage::SCREEN)).getWidth();
            _axisGizmo->fromLines(_axisLines, Rect<I32>(windowWidth - 120, 8, 128, 128));
        
            // We need to transform the gizmo so that it always remains axis aligned
            // Create a world matrix using a look at function with the eye position
            // backed up from the camera's view direction
            _axisGizmo->worldMatrix(mat4<F32>(-activeCamera.getForwardDir() * 2,
                                               VECTOR3_ZERO,
                                               activeCamera.getUpDir()) * getMatrix(MATRIX::VIEW_INV));
            bufferInOut.add(_axisGizmo->toCommandBuffer());
        }
    }
}

};

Commits for Divide-Framework/trunk/Source Code/Platform/Video/GFXDeviceDebug.cpp

Diff revisions: vs.
Revision Author Commited Message
1004 Diff Diff IonutCava picture IonutCava Wed 20 Dec, 2017 17:16:32 +0000

[Ionut]
- More GUI cleanup (added multiple GUIButton events)
- Typedef vec4<U8> and vec4<F32> to UColour and FColour respectively for easier to read code.

988 Diff Diff IonutCava picture IonutCava Thu 30 Nov, 2017 23:08:51 +0000

[Ionut]
- Add a Rect class (basic wrapper around a vec4)
- More work on MouseEvent system

979 Diff Diff IonutCava picture IonutCava Tue 21 Nov, 2017 23:24:57 +0000

[Ionut]
- W.I.P. Rework GUI element positioning and scaling
- Update ImGUI and IMGUI_ADDONS

966 Diff Diff IonutCava picture IonutCava Mon 06 Nov, 2017 17:10:01 +0000

[Ionut]
- Remove old “add2DRenderFunction” and use command buffers instead
- Allow frame to blit to the screen or a render target of choice

963 Diff Diff IonutCava picture IonutCava Sun 05 Nov, 2017 18:31:51 +0000

[Ionut]
- More multi-window fixes
- Profile guided performance improvements

953 Diff Diff IonutCava picture IonutCava Sat 28 Oct, 2017 21:57:10 +0000

[Ionut]
- Fix blinking issue by caching and hashing pipelines to that we don’t overwrite pipelines used by other elements

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)

942 Diff Diff IonutCava picture IonutCava Wed 06 Sep, 2017 20:45:37 +0000

[Ionut]
- Add more DLLs required by CEGUI
- Move CommandBuffer allocation from GFXDevice to GFX namespace

940 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