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

#include "Headers/SceneGraph.h"
#include "Core/Headers/Console.h"
#include "Utility/Headers/Localization.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Managers/Headers/SceneManager.h"
#include "Managers/Headers/FrameListenerManager.h"
#include "Rendering/RenderPass/Headers/RenderQueue.h"
#include "Geometry/Material/Headers/Material.h"

namespace Divide {

namespace {
    U32 ignoredNodeType = to_base(SceneNodeType::TYPE_ROOT) |
                          to_base(SceneNodeType::TYPE_LIGHT) |
                          to_base(SceneNodeType::TYPE_PARTICLE_EMITTER) |
                          to_base(SceneNodeType::TYPE_TRIGGER) |
                          to_base(SceneNodeType::TYPE_SKY) |
                          to_base(SceneNodeType::TYPE_VEGETATION_GRASS);
};

SceneGraph::SceneGraph(Scene& parentScene)
    : FrameListener(),
      SceneComponent(parentScene),
     _loadComplete(false),
     _octreeChanged(false),
     _rootNode(new SceneRoot(parentScene.resourceCache(), 1234))
{
    static const U32 rootMask = to_base(SGNComponent::ComponentType::PHYSICS) |
                                to_base(SGNComponent::ComponentType::BOUNDS);

    REGISTER_FRAME_LISTENER(this, 1);
    _root = std::make_shared<SceneGraphNode>(*this, PhysicsGroup::GROUP_IGNORE, _rootNode, "ROOT", rootMask);
    _root->get<BoundsComponent>()->lockBBTransforms(true);
    Attorney::SceneNodeSceneGraph::postLoad(*_rootNode, *_root);
    onNodeAdd(*_root);
    _allNodes.push_back(_root);

    U32 octreeNodeMask = to_base(SceneNodeType::TYPE_ROOT) |
                         to_base(SceneNodeType::TYPE_LIGHT) |
                         to_base(SceneNodeType::TYPE_SKY) |
                         to_base(SceneNodeType::TYPE_VEGETATION_GRASS);

    _octree.reset(new Octree(octreeNodeMask));
    _octreeUpdating = false;
}

SceneGraph::~SceneGraph()
{ 
    _octree.reset();
    _allNodes.clear();
    UNREGISTER_FRAME_LISTENER(this);
    Console::d_printfn(Locale::get(_ID("DELETE_SCENEGRAPH")));
    // Should recursively delete the entire scene graph
    unload();
}

void SceneGraph::unload()
{
    U32 childCount = 0;
    stringImpl childNameCpy;
    while((childCount = _root->getChildCount()) != 0) {
        SceneGraphNode& child = _root->getChild(childCount - 1);
        childNameCpy = child.getName();

        if (!_root->removeNode(child)) {
            Console::errorfn(Locale::get(_ID("ERROR_SCENE_GRAPH_REMOVE_NODE"), childNameCpy.c_str()));
        }
    }
}

bool SceneGraph::frameStarted(const FrameEvent& evt) {
    return true;
}

bool SceneGraph::frameEnded(const FrameEvent& evt) {
    return true;
}

void SceneGraph::unregisterNode(I64 guid, SceneGraphNode::UsageContext usage) {
}

void SceneGraph::onNodeDestroy(SceneGraphNode& oldNode) {
    I64 guid = oldNode.getGUID();

    vectorImpl<SceneGraphNode_wptr>& nodesByType = _nodesByType[to_base(oldNode.getNode()->getType())];

    nodesByType.erase(std::remove_if(std::begin(nodesByType), std::end(nodesByType), 
                                     [guid](SceneGraphNode_wptr node)-> bool
                                     {
                                         SceneGraphNode_ptr nodePtr = node.lock();
                                         return nodePtr && nodePtr->getGUID() == guid;
                                     }),
                      std::end(nodesByType));

    if (_loadComplete) {
        unregisterNode(guid, oldNode.usageContext());
    }

    Attorney::SceneGraph::onNodeDestroy(_parentScene, oldNode);

    _allNodes.erase(std::remove_if(std::begin(_allNodes), std::end(_allNodes),
                                   [guid](SceneGraphNode_wptr node)-> bool 
                                   {
                                       SceneGraphNode_ptr nodePtr = node.lock();
                                       return nodePtr && nodePtr->getGUID() == guid;
                                   }),
                    std::end(_allNodes));
}

void SceneGraph::onNodeAdd(SceneGraphNode& newNode) {
    SceneGraphNode_ptr newNodePtr(newNode.shared_from_this());

    _allNodes.push_back(newNodePtr);
    _nodesByType[to_base(newNodePtr->getNode()->getType())].push_back(newNodePtr);
    
    if (_loadComplete) {
        WAIT_FOR_CONDITION(!_octreeUpdating);
        _octreeChanged = _octree->addNode(newNode.shared_from_this());
    }
}

void SceneGraph::onNodeTransform(SceneGraphNode& node) {
    if (_loadComplete) {
        node.setUpdateFlag(SceneGraphNode::UpdateFlag::SPATIAL_PARTITION);
    }
}

void SceneGraph::idle()
{
    if (!_pendingRemovalNodes.empty()) {
        for (SceneGraphNode_wptr node : _pendingRemovalNodes) {
            removeNode(node, false);
        }
      
        _pendingRemovalNodes.clear();
    }
}

void SceneGraph::removeNodesByType(SceneNodeType nodeType) {
    getRoot().removeNodesByType(nodeType);
}

void SceneGraph::removeNode(SceneGraphNode_wptr node, bool deferrRemoval) {
    SceneGraphNode_ptr sgn = node.lock();
    if (!sgn) {
        return;
    }
    if (deferrRemoval) {
        _pendingRemovalNodes.push_back(node);
    } else {
        SceneGraphNode_ptr parent = sgn->getParent().lock();
        if (parent) {
            parent->removeNode(*sgn, false);
        }

        assert(sgn.unique());
        sgn.reset();
    }
}

void SceneGraph::sceneUpdate(const U64 deltaTime, SceneState& sceneState) {
    // Gather all nodes in order
    _orderedNodeList.resize(0);
    _root->getOrderedNodeList(_orderedNodeList);
    for (SceneGraphNode* node : _orderedNodeList) {
        node->sceneUpdate(deltaTime, sceneState);
        node->sgnUpdate(deltaTime, sceneState);
    }

    // Split updates into 2 passes to allow for better parallelism:
    // - Start threaded tasks in the sceneUpdate pass
    // - Wait on the results in the sgnUpdate pass
    // - Allows, for example, to recompute bounding boxes in parallel
    // First pass
    //_root->sceneUpdate(deltaTime, sceneState);
    // Second pass
    //_root->sgnUpdate(deltaTime, sceneState);

    if (_loadComplete) {
        CreateTask(parentScene().platformContext(),
            [this, deltaTime](const Task& parentTask) mutable
            {
                _octreeUpdating = true;
                if (_octreeChanged) {
                    _octree->updateTree();
                }
                _octree->update(deltaTime);
            },
            [this]() mutable
            {
                _octreeUpdating = false;
            })._task->startTask(Task::TaskPriority::REALTIME_WITH_CALLBACK);
    }
}

void SceneGraph::onCameraUpdate(const Camera& camera) {
    static const U32 s_nodesPerThread = 64;

    I64 guid = camera.getGUID();
    const vec3<F32>& eye = camera.getEye(); 
    const mat4<F32>& world = camera.getWorldMatrix();

    U32 childCount = _root->getChildCount();
    auto updateCamera = [this, &guid, &eye, &world](const Task& parentTask, U32 start, U32 end) {
        auto perChildCull = [this, &guid, &eye, &world](SceneGraphNode& child, I32 i) {
            child.onCameraUpdate(guid, eye, world);
        };

        _root->forEachChild(perChildCull, start, end);
    };

    parallel_for(parentScene().platformContext(), updateCamera, childCount, s_nodesPerThread, Task::TaskPriority::MAX);
}

void SceneGraph::onCameraChange(const Camera& camera) {
    _root->onCameraChange(camera);
}

void SceneGraph::onNetworkSend(U32 frameCount) {
    _root->onNetworkSend(frameCount);
}

void SceneGraph::intersect(const Ray& ray, F32 start, F32 end, vectorImpl<SceneGraphNode_cwptr>& selectionHits) const {
    _root->intersect(ray, start, end, selectionHits);

    /*if (_loadComplete) {
        WAIT_FOR_CONDITION(!_octreeUpdating);
        U32 filter = to_base(SceneNodeType::TYPE_OBJECT3D);
        SceneGraphNode_ptr collision = _octree->nearestIntersection(ray, start, end, filter)._intersectedObject1.lock();
        if (collision) {
            Console::d_printfn("Octree ray cast [ %s ]", collision->getName().c_str());
        }
    }*/
}

void SceneGraph::postLoad() {
    _octree->addNodes(_allNodes);
    _octreeChanged = true;
    _loadComplete = true;
}


const vectorImpl<SceneGraphNode_wptr>& SceneGraph::getNodesByType(SceneNodeType type) const {
    return _nodesByType[to_base(type)];
}

};

Commits for Divide-Framework/trunk/Source Code/Graphs/SceneGraph.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

925 Diff Diff IonutCava picture IonutCava Fri 04 Aug, 2017 13:10:43 +0000

[Ionut]
- Cleanup commandIndex and commandOffset updates between GFXDevice and RenderingComponent
- Fix some shader code rot
- Fix a bug in ParticleEmitter creation
- Disable OIT passes for now (really buggy)

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

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

886 Diff Diff IonutCava picture IonutCava Wed 29 Mar, 2017 23:36:51 +0000

[Ionut]
- Initial code for multiple water block support (position + length/width/depth)

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

[IonutCava]
- Optimise static casts

867 Diff Diff 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.

863 Diff Diff IonutCava picture IonutCava Wed 15 Feb, 2017 16:27:26 +0000

[IonutCava]
- Split Resource class into Resource and CachedResource
— CachedResource is the only resource held in a ResourceCache and loaded from a ResourceDescriptor
— CachedResource is mapped by its creation descriptor’s hash value, instead of the resource name
-— Multiple cached resources can now exist with the same name if the have different properties, for example.

857 Diff Diff IonutCava picture IonutCava Sun 12 Feb, 2017 15:19:19 +0000

[IonutCava]
- Resource load callback system updates

856 IonutCava picture IonutCava Fri 10 Feb, 2017 17:19:06 +0000

[IonutCava]
- Attempt to improve resource creation mechanism by adding a callback for when the resource finishes loading
— This is useful for multi-threaded loading: shaders, textures, terrain, etc