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

#include "Headers/RenderQueue.h"

#include "Core/Headers/Kernel.h"
#include "Core/Headers/Console.h"
#include "Core/Headers/TaskPool.h"
#include "Core/Headers/Application.h"
#include "Utility/Headers/Localization.h"
#include "Graphs/Headers/SceneGraphNode.h"
#include "Geometry/Shapes/Headers/Object3D.h"
#include "Geometry/Material/Headers/Material.h"

namespace Divide {

RenderQueue::RenderQueue(GFXDevice& context)
    : _context(context)
{
    _renderBins.fill(nullptr);
    _activeBins.reserve(to_base(RenderBinType::COUNT));
}

RenderQueue::~RenderQueue()
{
    for (RenderBin* bin : _renderBins) {
        MemoryManager::DELETE(bin);
    }
    _renderBins.fill(nullptr);
    _activeBins.clear();
}

U16 RenderQueue::getRenderQueueStackSize() const {
    U16 temp = 0;
    for (RenderBin* bin : _activeBins) {
        temp += bin->getBinSize();
    }
    return temp;
}

RenderingOrder::List RenderQueue::getSortOrder(RenderBinType rbType) {
    RenderingOrder::List sortOrder = RenderingOrder::List::COUNT;
    switch (rbType) {
        case RenderBinType::RBT_TRANSPARENT:
        case RenderBinType::RBT_OPAQUE: {
            // Opaque items should be rendered front to back in depth passes for early-Z reasons
            sortOrder = !_context.isDepthStage() ? RenderingOrder::List::BY_STATE
                                                 : RenderingOrder::List::FRONT_TO_BACK;
        } break;
        case RenderBinType::RBT_SKY: {
            sortOrder = RenderingOrder::List::NONE;
        } break;
        case RenderBinType::RBT_IMPOSTOR:
        case RenderBinType::RBT_TERRAIN:
        case RenderBinType::RBT_DECAL: {
            // No need to sort decals in depth passes as they're small on screen and processed post-opaque pass
            sortOrder = !_context.isDepthStage() ? RenderingOrder::List::FRONT_TO_BACK
                                                 : RenderingOrder::List::NONE;
        } break;
        case RenderBinType::RBT_TRANSLUCENT: {
            // Translucent items should be rendered by material in depth passes to avoid useless material switches
            // Small cost for bypassing early-Z checks, but translucent items should be in the minority on the screen anyway
            // and the Opaque pass should have finished by now
            sortOrder = !_context.isDepthStage() ? RenderingOrder::List::BACK_TO_FRONT
                                                 : RenderingOrder::List::BY_STATE;
        } break;
        default: {
            Console::errorfn(Locale::get(_ID("ERROR_INVALID_RENDER_BIN_CREATION")));
        } break;
    };
    
    return sortOrder;
}

RenderBin* RenderQueue::getOrCreateBin(RenderBinType rbType) {
    RenderBin* temp = getBin(rbType);
    if (temp != nullptr) {
        return temp;
    }

    temp = MemoryManager_NEW RenderBin(_context, rbType);
    // Bins are sorted by their type
    _renderBins[rbType._to_integral()] = temp;
    
    _activeBins.resize(0);
    std::back_insert_iterator<vectorImpl<RenderBin*>> back_it(_activeBins);
    auto const usedPredicate = [](RenderBin* ptr) { return ptr != nullptr; };
    std::copy_if(std::begin(_renderBins), std::end(_renderBins), back_it, usedPredicate);

    return temp;
}

RenderBin* RenderQueue::getBinForNode(const std::shared_ptr<SceneNode>& node,
                                      const Material_ptr& matInstance) {
    assert(node != nullptr);
    switch (node->getType()) {
        case SceneNodeType::TYPE_LIGHT: return getOrCreateBin(RenderBinType::RBT_IMPOSTOR);
        case SceneNodeType::TYPE_PARTICLE_EMITTER: return getOrCreateBin(RenderBinType::RBT_TRANSLUCENT);
        case SceneNodeType::TYPE_VEGETATION_GRASS: return getOrCreateBin(RenderBinType::RBT_TRANSPARENT);
        case SceneNodeType::TYPE_SKY: return getOrCreateBin(RenderBinType::RBT_SKY);

        // Water is also opaque as refraction and reflection are separate textures
        case SceneNodeType::TYPE_WATER:
        case SceneNodeType::TYPE_OBJECT3D: {
        case SceneNodeType::TYPE_VEGETATION_TREES:
            if (node->getType() == SceneNodeType::TYPE_OBJECT3D) {
                Object3D::ObjectType type = static_cast<Object3D*>(node.get())->getObjectType();
                switch (type) {
                    case Object3D::ObjectType::TERRAIN: return getOrCreateBin(RenderBinType::RBT_TERRAIN);
                    case Object3D::ObjectType::DECAL: return getOrCreateBin(RenderBinType::RBT_DECAL);
                }
            }
            // Check if the object has a material with transparency/translucency
            if (matInstance) {
                // Add it to the appropriate bin if so ...
                if (matInstance->hasTransparency()) {
                    if (matInstance->hasTranslucency()) {
                        return getOrCreateBin(RenderBinType::RBT_TRANSLUCENT);
                    }
                    return getOrCreateBin(RenderBinType::RBT_TRANSPARENT);
                }
            }
            //... else add it to the general geometry bin
            return getOrCreateBin(RenderBinType::RBT_OPAQUE);
        }
    }
    return nullptr;
}

void RenderQueue::addNodeToQueue(const SceneGraphNode& sgn, const RenderStagePass& stage, const vec3<F32>& eyePos) {
    static Material_ptr defaultMat;

    RenderingComponent* const renderingCmp = sgn.get<RenderingComponent>();
    RenderBin* rb = getBinForNode(sgn.getNode(),
                                  renderingCmp
                                    ? renderingCmp->getMaterialInstance()
                                    : defaultMat);
    if (rb) {
        rb->addNodeToBin(sgn, stage, eyePos);
    }
}

void RenderQueue::populateRenderQueues(const RenderStagePass& renderStagePass) {
    for (RenderBin* renderBin : _activeBins) {
        if (!renderBin->empty()) {
            renderBin->populateRenderQueue(renderStagePass);
        }
    }
}

void RenderQueue::postRender(const SceneRenderState& renderState, const RenderStagePass& renderStagePass, GFX::CommandBuffer& bufferInOut) {
    for (RenderBin* renderBin : _activeBins) {
        renderBin->postRender(renderState, renderStagePass, bufferInOut);
    }
}

void RenderQueue::sort() {
    // How many elements should a renderbin contain before we decide that sorting 
    // should happen on a separate thread
    static const U16 threadBias = 16;

    TaskPool& pool = _context.parent().taskPool();
    TaskHandle sortTask = CreateTask(pool, DELEGATE_CBK<void, const Task&>());
    for (RenderBin* renderBin : _activeBins) {
        if (!renderBin->empty()) {
            RenderingOrder::List sortOrder = getSortOrder(renderBin->getType());

            if (renderBin->getBinSize() > threadBias) {
                Task* child = sortTask.addChildTask(CreateTask(pool,
                                                               [renderBin, sortOrder](const Task& parentTask) {
                    renderBin->sort(sortOrder, parentTask);
                }));

                child->startTask(Task::TaskPriority::HIGH);
            } else {
                renderBin->sort(sortOrder);
            }
        }
    }
    sortTask.startTask(Task::TaskPriority::MAX).wait();
}

void RenderQueue::refresh() {
    for (RenderBin* renderBin : _activeBins) {
        renderBin->refresh();
    }
}

const RenderQueue::SortedQueues& RenderQueue::getSortedQueues() {
    for (RenderBin* renderBin : _activeBins) {
        vectorImpl<SceneGraphNode*>& nodes = _sortedQueues[renderBin->getType()._to_integral()];
        renderBin->getSortedNodes(nodes);
    }

    return _sortedQueues;
}

};

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

Diff revisions: vs.
Revision Author Commited Message
1012 Diff Diff IonutCava picture IonutCava Mon 15 Jan, 2018 17:21:32 +0000

[Ionut]
- Update boost to v1.66
- Performance optimizations

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

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)

909 Diff Diff IonutCava picture IonutCava Thu 29 Jun, 2017 23:57:18 +0000

[Ionut]
- Rework RenderTarget class
— RTAttachment now deal with textures directly
— RT size needs to be specified upfront
- Application class is no longer a Singleton but passed around in the PlatformContext

906 Diff Diff IonutCava picture IonutCava Mon 26 Jun, 2017 20:58:19 +0000

[Ionut]
- Fix small issues with the previous commit

905 Diff Diff IonutCava picture IonutCava Mon 26 Jun, 2017 15:56:21 +0000

[Ionut]
Untested code

- Rework RenderBin/RenderPass/RenderQueue system to allow per-bin type rendering decision
— Added to allow implementation of Weighted, Blended Order-Independent Transparency later on
- Better detection of transparency and translucency in textures and materials. Used to separate Alpha-Discard based geometry from OIT translucent geometry

899 Diff Diff IonutCava picture IonutCava Fri 23 Jun, 2017 15:37:44 +0000

[IonutCava]
- Move sRGB conversion and fog calculation to post-processing stages (fog in postProcessing using the depth buffer and sRGB in the presentToScreen step)
- Split the bloom.glsl file into multiple shaders: bloom, tonemap and luminanceCalc for ease of use purposes
- Rework RenderBin sorting: sort translucent/transparent nodes by material in depth passes
- Sort order is no longer part of RenderBin for better control

890 Diff Diff IonutCava picture IonutCava Sun 14 May, 2017 20:54:59 +0000

[Ionut]
- Add pre-compiled header support
- Initial code for per-stage tessellation computation

872 Diff Diff IonutCava picture IonutCava Mon 06 Mar, 2017 23:23:20 +0000

[IonutCava]
- Optimise static casts

867 IonutCava picture IonutCava Mon 20 Feb, 2017 17:01:12 +0000

[IonutCava]
- Rename to_XNN and to_const_XNN functions to reflect the target data type they are converting to.
- Cleanup Angle namespace and remove all “inDegrees” parameters from rotation functions. Use aliases to differentiate between Degree and Radian parameters.
- Profile guided optimizations.