| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624 |
- // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <AnKi/Scene/SceneGraph.h>
- #include <AnKi/Scene/RenderStateBucket.h>
- #include <AnKi/Physics/PhysicsWorld.h>
- #include <AnKi/Resource/ResourceManager.h>
- #include <AnKi/Util/CVarSet.h>
- #include <AnKi/Core/StatsSet.h>
- #include <AnKi/Util/Tracer.h>
- #include <AnKi/Util/HighRezTimer.h>
- #include <AnKi/Core/App.h>
- #include <AnKi/Scene/StatsUiNode.h>
- #include <AnKi/Scene/DeveloperConsoleUiNode.h>
- #include <AnKi/Scene/EditorUiNode.h>
- #include <AnKi/Scene/Components/BodyComponent.h>
- #include <AnKi/Scene/Components/CameraComponent.h>
- #include <AnKi/Scene/Components/DecalComponent.h>
- #include <AnKi/Scene/Components/FogDensityComponent.h>
- #include <AnKi/Scene/Components/GlobalIlluminationProbeComponent.h>
- #include <AnKi/Scene/Components/JointComponent.h>
- #include <AnKi/Scene/Components/LensFlareComponent.h>
- #include <AnKi/Scene/Components/LightComponent.h>
- #include <AnKi/Scene/Components/MoveComponent.h>
- #include <AnKi/Scene/Components/ParticleEmitter2Component.h>
- #include <AnKi/Scene/Components/PlayerControllerComponent.h>
- #include <AnKi/Scene/Components/ReflectionProbeComponent.h>
- #include <AnKi/Scene/Components/ScriptComponent.h>
- #include <AnKi/Scene/Components/SkinComponent.h>
- #include <AnKi/Scene/Components/SkyboxComponent.h>
- #include <AnKi/Scene/Components/TriggerComponent.h>
- #include <AnKi/Scene/Components/UiComponent.h>
- #include <AnKi/Scene/Components/MeshComponent.h>
- #include <AnKi/Scene/Components/MaterialComponent.h>
- namespace anki {
- ANKI_SVAR(SceneUpdateTime, StatCategory::kTime, "All scene update", StatFlag::kMilisecond | StatFlag::kShowAverage | StatFlag::kMainThreadUpdates)
- ANKI_SVAR(SceneComponentsUpdated, StatCategory::kScene, "Scene components updated per frame", StatFlag::kZeroEveryFrame)
- ANKI_SVAR(SceneNodesUpdated, StatCategory::kScene, "Scene nodes updated per frame", StatFlag::kZeroEveryFrame)
- constexpr U32 kUpdateNodeBatchSize = 10;
- class SceneGraph::UpdateSceneNodesCtx
- {
- public:
- class PerThread
- {
- public:
- DynamicArray<SceneNode*, MemoryPoolPtrWrapper<StackMemoryPool>> m_nodesForDeletion;
- LightComponent* m_dirLightComponent = nullptr;
- SkyboxComponent* m_skyboxComponent = nullptr;
- Vec3 m_sceneMin = Vec3(kMaxF32);
- Vec3 m_sceneMax = Vec3(kMinF32);
- Bool m_multipleDirLights = false;
- Bool m_multipleSkyboxes = false;
- PerThread()
- : m_nodesForDeletion(&SceneGraph::getSingleton().m_framePool)
- {
- }
- };
- IntrusiveList<SceneNode>::Iterator m_crntNode;
- SpinLock m_crntNodeLock;
- Second m_prevUpdateTime = 0.0;
- Second m_crntTime = 0.0;
- DynamicArray<PerThread, MemoryPoolPtrWrapper<StackMemoryPool>> m_perThread;
- Bool m_forceUpdateSceneBounds = false;
- UpdateSceneNodesCtx(U32 threadCount)
- : m_perThread(&SceneGraph::getSingleton().m_framePool)
- {
- m_perThread.resize(threadCount);
- }
- };
- SceneGraph::SceneGraph()
- {
- }
- SceneGraph::~SceneGraph()
- {
- while(!m_nodesForRegistration.isEmpty())
- {
- deleteInstance(SceneMemoryPool::getSingleton(), m_nodesForRegistration.popBack());
- }
- while(!m_nodes.isEmpty())
- {
- deleteInstance(SceneMemoryPool::getSingleton(), m_nodes.popBack());
- }
- #define ANKI_CAT_TYPE(arrayName, gpuSceneType, id, cvarName) GpuSceneArrays::arrayName::freeSingleton();
- #include <AnKi/Scene/GpuSceneArrays.def.h>
- RenderStateBucketContainer::freeSingleton();
- }
- Error SceneGraph::init(AllocAlignedCallback allocCallback, void* allocCallbackData)
- {
- SceneMemoryPool::allocateSingleton(allocCallback, allocCallbackData);
- m_framePool.init(allocCallback, allocCallbackData, 1_MB, 2.0, 0, true, "SceneGraphFramePool");
- #define ANKI_CAT_TYPE(arrayName, gpuSceneType, id, cvarName) GpuSceneArrays::arrayName::allocateSingleton(U32(cvarName));
- #include <AnKi/Scene/GpuSceneArrays.def.h>
- // Init the default main camera
- m_defaultMainCam = newSceneNode<SceneNode>("_MainCamera");
- CameraComponent* camc = m_defaultMainCam->newComponent<CameraComponent>();
- camc->setPerspective(0.1f, 1000.0f, toRad(60.0f), (1080.0f / 1920.0f) * toRad(60.0f));
- m_mainCam = m_defaultMainCam;
- RenderStateBucketContainer::allocateSingleton();
- // Construct a few common nodes
- if(g_cvarCoreDisplayStats > 0)
- {
- StatsUiNode* statsNode = newSceneNode<StatsUiNode>("_StatsUi");
- statsNode->setFpsOnly(g_cvarCoreDisplayStats == 1);
- }
- newSceneNode<DeveloperConsoleUiNode>("_DevConsole");
- EditorUiNode* editorNode = newSceneNode<EditorUiNode>("_Editor");
- editorNode->getFirstComponentOfType<UiComponent>().setEnabled(false);
- m_editorUi = editorNode;
- return Error::kNone;
- }
- SceneNode& SceneGraph::findSceneNode(const CString& name)
- {
- SceneNode* node = tryFindSceneNode(name);
- ANKI_ASSERT(node);
- return *node;
- }
- SceneNode* SceneGraph::tryFindSceneNode(const CString& name)
- {
- // Search registered nodes
- auto it = m_nodesDict.find(name);
- if(it != m_nodesDict.getEnd())
- {
- return *it;
- }
- // Didn't found it, search those up for registration
- LockGuard lock(m_nodesForRegistrationMtx);
- for(SceneNode& node : m_nodesForRegistration)
- {
- if(node.getName() == name)
- {
- return &node;
- }
- }
- return nullptr;
- }
- void SceneGraph::update(Second prevUpdateTime, Second crntTime)
- {
- ANKI_ASSERT(m_mainCam);
- ANKI_TRACE_SCOPED_EVENT(SceneUpdate);
- const Second startUpdateTime = HighRezTimer::getCurrentTime();
- // Reset the framepool
- m_framePool.reset();
- // Register new nodes
- while(!m_nodesForRegistration.isEmpty())
- {
- SceneNode* node = m_nodesForRegistration.popFront();
- // Add to dict if it has a name
- if(node->getName() != "Unnamed")
- {
- if(m_nodesDict.find(node->getName()) != m_nodesDict.getEnd())
- {
- ANKI_SCENE_LOGE("Node with the same name already exists. New node will not be searchable: %s", node->getName().cstr());
- }
- else
- {
- m_nodesDict.emplace(node->getName(), node);
- }
- }
- // Add to list
- m_nodes.pushBack(node);
- ++m_nodesCount;
- }
- // Re-index renamed nodes
- for(auto& pair : m_nodesRenamed)
- {
- SceneNode& node = *pair.first;
- CString oldName = pair.second;
- auto it = m_nodesDict.find(oldName);
- if(it != m_nodesDict.getEnd())
- {
- m_nodesDict.erase(it);
- }
- else
- {
- ANKI_ASSERT(oldName == "Unnamed" && "Didn't found the SceneNode and its name wasn't unnamed");
- }
- if(node.getName() != "Unnamed")
- {
- if(m_nodesDict.find(node.getName()) != m_nodesDict.getEnd())
- {
- ANKI_SCENE_LOGE("Node with the same name already exists. New node will not be searchable: %s", node.getName().cstr());
- }
- else
- {
- m_nodesDict.emplace(node.getName(), &node);
- }
- }
- }
- m_nodesRenamed.destroy();
- // Update physics
- PhysicsWorld::getSingleton().update(crntTime - prevUpdateTime);
- // Before the update wake some threads with dummy work
- for(U i = 0; i < 2; i++)
- {
- CoreThreadJobManager::getSingleton().dispatchTask([]([[maybe_unused]] U32 tid) {});
- }
- // Update events
- {
- ANKI_TRACE_SCOPED_EVENT(EventsUpdate);
- m_events.updateAllEvents(prevUpdateTime, crntTime);
- }
- // Update events and scene nodes
- UpdateSceneNodesCtx updateCtx(CoreThreadJobManager::getSingleton().getThreadCount());
- {
- ANKI_TRACE_SCOPED_EVENT(SceneNodesUpdate);
- updateCtx.m_crntNode = m_nodes.getBegin();
- updateCtx.m_prevUpdateTime = prevUpdateTime;
- updateCtx.m_crntTime = crntTime;
- updateCtx.m_forceUpdateSceneBounds = (m_frame % kForceSetSceneBoundsFrameCount) == 0;
- for(U i = 0; i < CoreThreadJobManager::getSingleton().getThreadCount(); i++)
- {
- CoreThreadJobManager::getSingleton().dispatchTask([this, &updateCtx](U32 tid) {
- updateNodes(tid, updateCtx);
- });
- }
- CoreThreadJobManager::getSingleton().waitForAllTasksToFinish();
- }
- // Update scene bounds
- {
- Vec3 sceneMin = Vec3(kMaxF32);
- Vec3 sceneMax = Vec3(kMinF32);
- for(U32 tid = 0; tid < updateCtx.m_perThread.getSize(); ++tid)
- {
- const UpdateSceneNodesCtx::PerThread& thread = updateCtx.m_perThread[tid];
- sceneMin = sceneMin.min(thread.m_sceneMin);
- sceneMax = sceneMax.max(thread.m_sceneMax);
- }
- if(sceneMin.x() != kMaxF32)
- {
- if(updateCtx.m_forceUpdateSceneBounds)
- {
- m_sceneMin = sceneMin;
- m_sceneMax = sceneMax;
- }
- else
- {
- m_sceneMin = m_sceneMin.min(sceneMin);
- m_sceneMax = m_sceneMax.max(sceneMax);
- }
- }
- }
- // Set some unique components
- {
- m_activeDirLight = nullptr;
- m_activeSkybox = nullptr;
- Bool bMultipleDirLights = false;
- Bool bMultipleSkyboxes = false;
- for(U32 tid = 0; tid < updateCtx.m_perThread.getSize(); ++tid)
- {
- const UpdateSceneNodesCtx::PerThread& thread = updateCtx.m_perThread[tid];
- bMultipleDirLights = bMultipleDirLights || thread.m_multipleDirLights;
- bMultipleSkyboxes = bMultipleSkyboxes || thread.m_multipleSkyboxes;
- if(thread.m_dirLightComponent)
- {
- if(m_activeDirLight)
- {
- bMultipleDirLights = true;
- if(thread.m_dirLightComponent->getUuid() < m_activeDirLight->getUuid())
- {
- m_activeDirLight = thread.m_dirLightComponent;
- }
- }
- else
- {
- m_activeDirLight = thread.m_dirLightComponent;
- }
- }
- if(thread.m_skyboxComponent)
- {
- if(m_activeSkybox)
- {
- bMultipleSkyboxes = true;
- if(thread.m_skyboxComponent->getUuid() < m_activeSkybox->getUuid())
- {
- m_activeSkybox = thread.m_skyboxComponent;
- }
- }
- else
- {
- m_activeSkybox = thread.m_skyboxComponent;
- }
- }
- }
- if(bMultipleDirLights)
- {
- ANKI_SCENE_LOGW("There are multiple dir lights in the scene. Choosing one to be active");
- }
- if(bMultipleSkyboxes)
- {
- ANKI_SCENE_LOGW("There are multiple skyboxes in the scene. Choosing one to be active");
- }
- }
- // Cleanup
- for(U32 tid = 0; tid < updateCtx.m_perThread.getSize(); ++tid)
- {
- const auto& thread = updateCtx.m_perThread[tid];
- for(SceneNode* node : thread.m_nodesForDeletion)
- {
- // Remove from the graph
- m_nodes.erase(node);
- ANKI_ASSERT(m_nodesCount > 0);
- --m_nodesCount;
- if(m_mainCam != m_defaultMainCam && m_mainCam == node)
- {
- m_mainCam = m_defaultMainCam;
- }
- // Remove from dict
- if(node->getName() != "Unnamed")
- {
- auto it = m_nodesDict.find(node->getName());
- ANKI_ASSERT(it != m_nodesDict.getEnd());
- if(*it == node)
- {
- m_nodesDict.erase(it);
- }
- }
- deleteInstance(SceneMemoryPool::getSingleton(), node);
- }
- }
- // Misc
- #define ANKI_CAT_TYPE(arrayName, gpuSceneType, id, cvarName) GpuSceneArrays::arrayName::getSingleton().flush();
- #include <AnKi/Scene/GpuSceneArrays.def.h>
- g_svarSceneUpdateTime.set((HighRezTimer::getCurrentTime() - startUpdateTime) * 1000.0);
- ++m_frame;
- }
- void SceneGraph::updateNode(U32 tid, SceneNode& node, UpdateSceneNodesCtx& ctx)
- {
- ANKI_TRACE_INC_COUNTER(SceneNodeUpdated, 1);
- UpdateSceneNodesCtx::PerThread& thread = ctx.m_perThread[tid];
- // Components update
- SceneComponentUpdateInfo componentUpdateInfo(ctx.m_prevUpdateTime, ctx.m_crntTime, ctx.m_forceUpdateSceneBounds, m_checkForResourceUpdates);
- componentUpdateInfo.m_framePool = &m_framePool;
- U32 sceneComponentUpdatedCount = 0;
- node.iterateComponents([&](SceneComponent& comp) {
- componentUpdateInfo.m_node = &node;
- Bool updated = false;
- comp.update(componentUpdateInfo, updated);
- if(updated)
- {
- ANKI_TRACE_INC_COUNTER(SceneComponentUpdated, 1);
- comp.setTimestamp(GlobalFrameIndex::getSingleton().m_value);
- ++sceneComponentUpdatedCount;
- }
- if(comp.getType() == SceneComponentType::kLight)
- {
- LightComponent& lc = static_cast<LightComponent&>(comp);
- if(lc.getLightComponentType() == LightComponentType::kDirectional)
- {
- if(thread.m_dirLightComponent)
- {
- thread.m_multipleDirLights = true;
- // Try to choose the same dir light in a deterministic way
- if(lc.getUuid() < thread.m_dirLightComponent->getUuid())
- {
- ctx.m_perThread[tid].m_dirLightComponent = &lc;
- }
- }
- else
- {
- ctx.m_perThread[tid].m_dirLightComponent = &lc;
- }
- }
- }
- else if(comp.getType() == SceneComponentType::kSkybox)
- {
- SkyboxComponent& skyc = static_cast<SkyboxComponent&>(comp);
- if(thread.m_skyboxComponent)
- {
- thread.m_multipleSkyboxes = true;
- // Try to choose the same skybox in a deterministic way
- if(skyc.getUuid() < thread.m_skyboxComponent->getUuid())
- {
- ctx.m_perThread[tid].m_skyboxComponent = &skyc;
- }
- }
- else
- {
- ctx.m_perThread[tid].m_skyboxComponent = &skyc;
- }
- }
- });
- // Frame update
- {
- if(sceneComponentUpdatedCount)
- {
- node.setComponentMaxTimestamp(GlobalFrameIndex::getSingleton().m_value);
- g_svarSceneComponentsUpdated.increment(sceneComponentUpdatedCount);
- g_svarSceneNodesUpdated.increment(1);
- }
- else
- {
- // No components or nothing updated, don't change the timestamp
- }
- node.frameUpdate(ctx.m_prevUpdateTime, ctx.m_crntTime);
- }
- // Update children
- node.visitChildrenMaxDepth(0, [&](SceneNode& child) {
- updateNode(tid, child, ctx);
- return FunctorContinue::kContinue;
- });
- ctx.m_perThread[tid].m_sceneMin = ctx.m_perThread[tid].m_sceneMin.min(componentUpdateInfo.m_sceneMin);
- ctx.m_perThread[tid].m_sceneMax = ctx.m_perThread[tid].m_sceneMax.max(componentUpdateInfo.m_sceneMax);
- }
- void SceneGraph::updateNodes(U32 tid, UpdateSceneNodesCtx& ctx)
- {
- ANKI_TRACE_SCOPED_EVENT(SceneNodeUpdate);
- IntrusiveList<SceneNode>::ConstIterator end = m_nodes.getEnd();
- Bool quit = false;
- while(!quit)
- {
- // Fetch a batch of scene nodes that don't have parent
- Array<SceneNode*, kUpdateNodeBatchSize> batch;
- U batchSize = 0;
- {
- LockGuard<SpinLock> lock(ctx.m_crntNodeLock);
- while(1)
- {
- if(batchSize == batch.getSize())
- {
- break;
- }
- if(ctx.m_crntNode == end)
- {
- quit = true;
- break;
- }
- SceneNode& node = *ctx.m_crntNode;
- if(node.isMarkedForDeletion())
- {
- ctx.m_perThread[tid].m_nodesForDeletion.emplaceBack(&node);
- }
- else if(node.getParent() == nullptr)
- {
- batch[batchSize++] = &node;
- }
- else
- {
- // Ignore
- }
- ++ctx.m_crntNode;
- }
- }
- // Process nodes
- for(U i = 0; i < batchSize; ++i)
- {
- updateNode(tid, *batch[i], ctx);
- }
- }
- }
- const SceneNode& SceneGraph::getActiveCameraNode() const
- {
- ANKI_ASSERT(m_mainCam);
- if(ANKI_EXPECT(m_mainCam->hasComponent<CameraComponent>()))
- {
- return *m_mainCam;
- }
- else
- {
- return *m_defaultMainCam;
- }
- }
- void SceneGraph::setActiveCameraNode(SceneNode* cam)
- {
- if(cam)
- {
- m_mainCam = cam;
- }
- else
- {
- m_mainCam = m_defaultMainCam;
- }
- }
- void SceneGraph::sceneNodeChangedName(SceneNode& node, CString oldName)
- {
- LockGuard lock(m_nodesForRegistrationMtx);
- m_nodesRenamed.emplaceBack(std::pair(&node, oldName));
- }
- Error SceneGraph::saveToTextFile(CString filename)
- {
- File file;
- ANKI_CHECK(file.open(filename, FileOpenFlag::kWrite));
- TextSceneSerializer serializer(&file, true);
- U32 version = kSceneBinaryVersion;
- ANKI_SERIALIZE(version, 1);
- #define ANKI_DEFINE_SCENE_COMPONENT(name, weight, sceneNodeCanHaveMany, icon, serializable) \
- if(serializable) \
- { \
- U32 name##Count = m_componentArrays.get##name##s().getSize(); \
- ANKI_SERIALIZE(name##Count, 1); \
- for(SceneComponent & comp : m_componentArrays.get##name##s()) \
- { \
- U32 uuid = comp.getUuid(); \
- ANKI_SERIALIZE(uuid, 1); \
- ANKI_CHECK(comp.serialize(serializer)); \
- } \
- }
- #include <AnKi/Scene/Components/SceneComponentClasses.def.h>
- // Scene nodes
- Error err = Error::kNone;
- visitNodes([&](SceneNode& node) {
- if(node.getParent() != nullptr)
- {
- // Skip non-root nodes
- return FunctorContinue::kContinue;
- }
- node.visitThisAndChildren([&](SceneNode& node) {
- err = node.serializeCommon(serializer);
- if(err)
- {
- return FunctorContinue::kStop;
- }
- return FunctorContinue::kContinue;
- });
- if(err)
- {
- return FunctorContinue::kStop;
- }
- return FunctorContinue::kContinue;
- });
- if(err)
- {
- return err;
- }
- return Error::kNone;
- }
- } // end namespace anki
|