Просмотр исходного кода

Scene rework. WONT COMPILE just yet

Panagiotis Christopoulos Charitos 12 лет назад
Родитель
Сommit
a5296c19c8

+ 4 - 0
include/anki/scene/Path.h

@@ -7,6 +7,8 @@
 
 namespace anki {
 
+#if 0
+
 /// XXX
 class PathPoint
 {
@@ -66,6 +68,8 @@ private:
 	F32 distance;
 };
 
+#endif
+
 } // end namespace anki
 
 #endif

+ 15 - 9
include/anki/scene/RenderComponent.h

@@ -171,6 +171,18 @@ public:
 
 	~RenderComponent();
 
+	/// @name Accessors
+	/// @{
+	Variables::iterator getVariablesBegin()
+	{
+		return vars.begin();
+	}
+	Variables::iterator getVariablesEnd()
+	{
+		return vars.end();
+	}
+	/// @}
+
 	/// Get information for rendering.
 	/// Given an array of submeshes that are visible return the correct indices
 	/// offsets and counts
@@ -189,17 +201,11 @@ public:
 		return nullptr;
 	}
 
-	/// @name Accessors
-	/// @{
-	Variables::iterator getVariablesBegin()
-	{
-		return vars.begin();
-	}
-	Variables::iterator getVariablesEnd()
+	Bool getCastsShadow()
 	{
-		return vars.end();
+		const Material& mtl = getMaterial();
+		return mtl.getShadow() && !mtl.isBlendingEnabled();
 	}
-	/// @}
 
 	/// Iterate variables using a lambda
 	template<typename Func>

+ 1 - 1
include/anki/scene/Visibility.h

@@ -41,7 +41,7 @@ struct VisibleNode
 	U32 spatialsCount;
 
 	VisibleNode()
-		: node(nullptr), subSpatialIndicesCount(0)
+		: node(nullptr), spatialsCount(0)
 	{}
 
 	VisibleNode(const VisibleNode& other)

+ 3 - 4
src/renderer/Bs.cpp

@@ -25,14 +25,13 @@ void Bs::run()
 	drawer.prepareDraw();
 	SceneGraph& scene = r->getSceneGraph();
 	VisibilityTestResults& vi =
-		scene.getActiveCamera().getFrustumComponent()->
-		getVisibilityTestResults();
+		scene.getActiveCamera().getVisibilityTestResults();
 
 	for(auto it : vi.renderables)
 	{
 		drawer.render(scene.getActiveCamera(), RenderableDrawer::RS_BLEND,
-			COLOR_PASS, *it.node, it.subSpatialIndices, 
-			it.subSpatialIndicesCount);
+			COLOR_PASS, *it.node, &it.spatialIndices[0], 
+			it.spatialsCount);
 	}
 
 	GlStateSingleton::get().setDepthMaskEnabled(true);

+ 1 - 1
src/renderer/Renderer.cpp

@@ -163,7 +163,7 @@ void Renderer::render(SceneGraph& scene_)
 
 	// Calc a few vars
 	//
-	U32 camUpdateTimestamp = cam.getFrustumComponent()->getTimestamp();
+	Timestamp camUpdateTimestamp = cam.FrustumComponent::getTimestamp();
 	if(planesUpdateTimestamp < scene->getActiveCameraChangeTimestamp()
 		|| planesUpdateTimestamp < camUpdateTimestamp
 		|| planesUpdateTimestamp == 1)

+ 4 - 0
src/scene/Path.cpp

@@ -2,6 +2,8 @@
 
 namespace anki {
 
+#if 0
+
 //==============================================================================
 Path::Path(
 	const char* name, SceneGraph* scene, 
@@ -70,4 +72,6 @@ Path::Path(
 	}
 }
 
+#endif
+
 } // end namespace anki

+ 37 - 85
src/scene/SceneGraph.cpp

@@ -15,86 +15,47 @@ namespace anki {
 //==============================================================================
 
 //==============================================================================
-struct UpdateMoveComponentsJob: ThreadpoolTask
+struct UpdateSceneNodesJob: ThreadpoolTask
 {
 	SceneGraph* scene = nullptr;
+	F32 prevUpdateTime;
+	F32 crntTime;
+	Barrier* barrier;
+	
 
 	void operator()(ThreadId threadId, U threadsCount)
 	{
-		/*U64 start, end;
 		ANKI_ASSERT(scene);
+		U64 start, end;
 		choseStartEnd(
 			threadId, threadsCount, scene->getSceneNodesCount(), start, end);
 
-		scene->iterateSceneNodes(start, end, [](SceneNode& sn)
+		// First update the move components
+		scene->iterateSceneNodes(start, end, [&](SceneNode& node)
 		{
-			MoveComponent* m = sn.getMoveComponent();
-			if(m)
+			MoveComponent* move = node.tryGetComponent<MoveComponent>();
+
+			if(move)
 			{
-				m->update();
+				move->updateReal(node, prevUpdateTime, crntTime,
+					SceneComponent::ASYNC_UPDATE);
 			}
-		});*/
-	}
-};
-
-//==============================================================================
-static void updateSceneNode(SceneNode& sn, F32 prevUpdateTime,
-	F32 crntTime, SectorGroup& sectorGroup)
-{
-	// Movable
-	MoveComponent* m = sn.getMoveComponent();
-	if(m)
-	{
-		m->reset();
-		m->updateReal(sn, prevUpdateTime, crntTime);
-	}
-
-	// Do some spatial stuff
-	SpatialComponent* sp = sn.getSpatialComponent();
-	if(sp)
-	{
-		sp->reset();
-		sp->updateReal(sn, prevUpdateTime, crntTime);
-	}
-
-	// Do some frustumable stuff
-	FrustumComponent* fr = sn.getFrustumComponent();
-	if(fr)
-	{
-		fr->reset();
-		fr->updateReal(sn, prevUpdateTime, crntTime);
-	}
-
-	// Do some renderable stuff
-	RenderComponent* r = sn.getRenderComponent();
-	if(r)
-	{
-		r->reset();
-		r->updateReal(sn, prevUpdateTime, crntTime);
-	}
-
-	// Update the node
-	sn.frameUpdate(prevUpdateTime, crntTime, getGlobTimestamp());
-}
+		});
 
-//==============================================================================
-struct UpdateSceneNodesJob: ThreadpoolTask
-{
-	SceneGraph* scene = nullptr;
-	F32 prevUpdateTime;
-	F32 crntTime;
-	SectorGroup* sectorGroup;
+		barrier->wait();
 
-	void operator()(ThreadId threadId, U threadsCount)
-	{
-		ANKI_ASSERT(scene);
-		U64 start, end;
-		choseStartEnd(
-			threadId, threadsCount, scene->getSceneNodesCount(), start, end);
-
-		scene->iterateSceneNodes(start, end, [&](SceneNode& sn)
+		// Update the rest of the components
+		auto moveComponentTypeId = SceneComponent::getTypeIdOf<MoveComponent>();
+		scene->iterateSceneNodes(start, end, [&](SceneNode& node)
 		{
-			updateSceneNode(sn, prevUpdateTime, crntTime, *sectorGroup);	
+			node.iterateComponents([&](SceneComponent& comp)
+			{
+				if(comp.getTypeId() != moveComponentTypeId)
+				{
+					comp.updateReal(node, prevUpdateTime, crntTime, 
+						SceneComponent::ASYNC_UPDATE);
+				}
+			});
 		});
 	}
 };
@@ -243,13 +204,14 @@ void SceneGraph::update(F32 prevUpdateTime, F32 crntTime, Renderer& renderer)
 	deleteNodesMarkedForDeletion();
 
 	// Sync updates
-	iterateSceneNodes([&](SceneNode& sn)
+	iterateSceneNodes([&](SceneNode& node)
 	{
-		RigidBody* body = sn.getRigidBody();
-		if(body)
+		node.iterateComponents([&](SceneComponent& comp)
 		{
-			body->syncUpdate(sn, prevUpdateTime, crntTime);
-		}
+			comp.reset();
+			comp.updateReal(node, prevUpdateTime, crntTime, 
+				SceneComponent::SYNC_UPDATE);
+		});	
 	});
 
 	Threadpool& threadPool = ThreadpoolSingleton::get();
@@ -261,12 +223,6 @@ void SceneGraph::update(F32 prevUpdateTime, F32 crntTime, Renderer& renderer)
 	events.updateAllEvents(prevUpdateTime, crntTime);
 
 	// Then the rest
-#if 0
-	for(SceneNode* n : nodes)
-	{
-		updateSceneNode(*n, prevUpdateTime, crntTime, sectorGroup);
-	}
-#else
 	Array<UpdateSceneNodesJob, Threadpool::MAX_THREADS> jobs2;
 
 	for(U i = 0; i < threadPool.getThreadsCount(); i++)
@@ -276,19 +232,14 @@ void SceneGraph::update(F32 prevUpdateTime, F32 crntTime, Renderer& renderer)
 		job.scene = this;
 		job.prevUpdateTime = prevUpdateTime;
 		job.crntTime = crntTime;
-		job.sectorGroup = &sectorGroup;
 
 		threadPool.assignNewTask(i, &job);
 	}
 
 	threadPool.waitForAllThreadsToFinish();
-#endif
 
 	doVisibilityTests(*mainCam, *this, renderer);
 
-	/*sectorGroup.doVisibilityTests(*mainCam,
-		VisibilityTest(VT_RENDERABLES | VT_LIGHTS), &r);*/
-
 	ANKI_COUNTER_STOP_TIMER_INC(C_SCENE_UPDATE_TIME);
 }
 
@@ -327,7 +278,7 @@ void SceneGraph::load(const char* filename)
 			}
 
 			ModelNode* node;
-			newSceneNode(node, name.c_str(), el.getText(), instancesCount);
+			newSceneNode(node, name.c_str(), el.getText());
 
 			// <transform>
 			el = mdlNodeEl.getChildElement("transform");
@@ -338,12 +289,13 @@ void SceneGraph::load(const char* filename)
 				if(i == 0)
 				{
 					node->setLocalTransform(Transform(el.getMat4()));
-					node->setInstanceLocalTransform(
-						i, Transform(el.getMat4()));
+					//node->setInstanceLocalTransform(
+					//	i, Transform(el.getMat4()));
 				}
 				else
 				{
-					node->setInstanceLocalTransform(i, Transform(el.getMat4()));
+					// TODO
+					//node->setInstanceLocalTransform(i, Transform(el.getMat4()));
 				}
 
 				// Advance

+ 19 - 18
src/scene/Visibility.cpp

@@ -21,13 +21,14 @@ struct VisibilityTestTask: ThreadpoolTask
 	void test(SceneNode& testedNode, Bool isLight, 
 		ThreadId threadId, U threadsCount)
 	{
-		ANKI_ASSERT(isLight == testedNode.tryGetComponent<LightComponent>());
+		ANKI_ASSERT(isLight == 
+			(testedNode.tryGetComponent<LightComponent>() != nullptr));
 
 		VisibilityTestResults* visible = 
 			frameAlloc.newInstance<VisibilityTestResults>(frameAlloc);
 
 		FrustumComponent& testedFr = 
-			testedNode->getComponent<FrustumComponent>();
+			testedNode.getComponent<FrustumComponent>();
 
 		// Chose the test range and a few other things
 		U64 start, end;
@@ -76,7 +77,7 @@ struct VisibilityTestTask: ThreadpoolTask
 					// Inside
 					sps[count++] = SpatialTemp{&sp, i};
 
-					sp->enableBits(isLight 
+					sp.enableBits(isLight 
 						? SpatialComponent::SF_VISIBLE_LIGHT 
 						: SpatialComponent::SF_VISIBLE_CAMERA);
 				}
@@ -90,7 +91,7 @@ struct VisibilityTestTask: ThreadpoolTask
 			}
 
 			// Sort spatials
-			Vec3 origin = frustumable.getFrustumOrigin();
+			Vec3 origin = testedFr.getFrustumOrigin();
 			std::sort(sps.begin(), sps.begin() + count, 
 				[&](const SpatialTemp& a, const SpatialTemp& b) -> Bool
 			{
@@ -114,7 +115,7 @@ struct VisibilityTestTask: ThreadpoolTask
 			RenderComponent* r = node.tryGetComponent<RenderComponent>();
 			if(isLight)
 			{
-				if(r && r->castsShadow())
+				if(r && r->getCastsShadow())
 				{
 					visible->renderables.push_back(visibleNode);
 				}
@@ -130,12 +131,13 @@ struct VisibilityTestTask: ThreadpoolTask
 					LightComponent* l = node.tryGetComponent<LightComponent>();
 					if(l)
 					{
-						visible->lights.push_back(
-							VisibleNode(&node, nullptr, 0));
+						Light* light = staticCast<Light*>(&node);
 
-						if(l->getShadowEnabled() && fr)
+						visible->lights.push_back(visibleNode);
+
+						if(light->getShadowEnabled() && fr)
 						{
-							test(node, true);
+							test(node, true, 0, 0);
 						}
 					}
 				}
@@ -146,7 +148,7 @@ struct VisibilityTestTask: ThreadpoolTask
 	/// Do the tests
 	void operator()(ThreadId threadId, U threadsCount)
 	{
-		test(*frustumableSn, threadId, threadsCount);
+		test(*frustumableSn, false, threadId, threadsCount);
 	}
 };
 
@@ -154,8 +156,7 @@ struct VisibilityTestTask: ThreadpoolTask
 void doVisibilityTests(SceneNode& fsn, SceneGraph& scene, 
 	Renderer& r)
 {
-	FrustumComponent* fr = fsn.getFrustumComponent();
-	ANKI_ASSERT(fr);
+	FrustumComponent& fr = fsn.getComponent<FrustumComponent>();
 
 	//
 	// Do the tests in parallel
@@ -184,8 +185,8 @@ void doVisibilityTests(SceneNode& fsn, SceneGraph& scene,
 	U32 lightsSize = 0;
 	for(U i = 0; i < threadPool.getThreadsCount(); i++)
 	{
-		renderablesSize += jobs[i].visible->renderables.size();
-		lightsSize += jobs[i].visible->lights.size();
+		renderablesSize += jobs[i].cameraVisible->renderables.size();
+		lightsSize += jobs[i].cameraVisible->lights.size();
 	}
 
 	// Allocate
@@ -203,7 +204,7 @@ void doVisibilityTests(SceneNode& fsn, SceneGraph& scene,
 	lightsSize = 0;
 	for(U i = 0; i < threadPool.getThreadsCount(); i++)
 	{
-		const VisibilityTestResults& from = *jobs[i].visible;
+		const VisibilityTestResults& from = *jobs[i].cameraVisible;
 
 		memcpy(&visible->renderables[renderablesSize],
 			&from.renderables[0],
@@ -219,7 +220,7 @@ void doVisibilityTests(SceneNode& fsn, SceneGraph& scene,
 	}
 
 	// Set the frustumable
-	fr->setVisibilityTestResults(visible);
+	fr.setVisibilityTestResults(visible);
 
 	//
 	// Sort
@@ -229,7 +230,7 @@ void doVisibilityTests(SceneNode& fsn, SceneGraph& scene,
 	DistanceSortJob dsjob;
 	dsjob.nodes = visible->lights.begin();
 	dsjob.nodesCount = visible->lights.size();
-	dsjob.origin = fr->getFrustumOrigin();
+	dsjob.origin = fr.getFrustumOrigin();
 	threadPool.assignNewTask(0, &dsjob);
 
 	// The rest of the jobs are dummy
@@ -241,7 +242,7 @@ void doVisibilityTests(SceneNode& fsn, SceneGraph& scene,
 
 	// Sort the renderables in the main thread
 	DistanceSortFunctor dsfunc;
-	dsfunc.origin = fr->getFrustumOrigin();
+	dsfunc.origin = fr.getFrustumOrigin();
 	std::sort(visible->renderables.begin(), visible->renderables.end(), dsfunc);
 
 	threadPool.waitForAllThreadsToFinish();