فهرست منبع

Some visibility refactoring

Panagiotis Christopoulos Charitos 10 سال پیش
والد
کامیت
c35512f100

+ 26 - 120
include/anki/scene/Visibility.h

@@ -74,7 +74,21 @@ public:
 	}
 };
 
-/// Its actually a container for visible entities. It should be per frame
+/// The group of nodes that a VisibilityTestResults holds.
+enum class VisibilityGroupType
+{
+	RENDERABLES,
+	LIGHTS,
+	FLARES,
+	REFLECTION_PROBES,
+	REFLECTION_PROXIES,
+
+	TYPE_COUNT,
+	FIRST = RENDERABLES
+};
+ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(VisibilityGroupType, inline)
+
+/// It's actually a container for visible entities. It should be per frame.
 class VisibilityTestResults
 {
 public:
@@ -91,104 +105,24 @@ public:
 
 	void prepareMerge();
 
-	VisibleNode* getRenderablesBegin()
-	{
-		return getBegin(RENDERABLES);
-	}
-
-	VisibleNode* getRenderablesEnd()
-	{
-		return getEnd(RENDERABLES);
-	}
-
-	VisibleNode* getLightsBegin()
-	{
-		return getBegin(LIGHTS);
-	}
-
-	VisibleNode* getLightsEnd()
-	{
-		return getEnd(LIGHTS);
-	}
-
-	VisibleNode* getLensFlaresBegin()
-	{
-		return getBegin(FLARES);
-	}
-
-	VisibleNode* getLensFlaresEnd()
-	{
-		return getEnd(FLARES);
-	}
-
-	VisibleNode* getReflectionProbesBegin()
-	{
-		return getBegin(REFLECTION_PROBES);
-	}
-
-	VisibleNode* getReflectionProbesEnd()
-	{
-		return getEnd(REFLECTION_PROBES);
-	}
-
-	VisibleNode* getReflectionProxiesBegin()
-	{
-		return getBegin(REFLECTION_PROXIES);
-	}
-
-	VisibleNode* getReflectionProxiesEnd()
-	{
-		return getEnd(REFLECTION_PROXIES);
-	}
-
-	U32 getRenderablesCount() const
-	{
-		return getCount(RENDERABLES);
-	}
-
-	U32 getLightsCount() const
-	{
-		return getCount(LIGHTS);
-	}
-
-	U32 getLensFlaresCount() const
-	{
-		return getCount(FLARES);
-	}
-
-	U32 getReflectionProbeCount() const
+	VisibleNode* getBegin(VisibilityGroupType type)
 	{
-		return getCount(REFLECTION_PROBES);
-	}
-
-	U32 getReflectionProxyCount() const
-	{
-		return getCount(REFLECTION_PROXIES);
-	}
-
-	void moveBackRenderable(SceneFrameAllocator<U8> alloc, VisibleNode& x)
-	{
-		moveBack(alloc, RENDERABLES, x);
-	}
-
-	void moveBackLight(SceneFrameAllocator<U8> alloc, VisibleNode& x)
-	{
-		moveBack(alloc, LIGHTS, x);
+		return (getCount(type)) ? &m_groups[type].m_nodes[0] : nullptr;
 	}
 
-	void moveBackLensFlare(SceneFrameAllocator<U8> alloc, VisibleNode& x)
+	VisibleNode* getEnd(VisibilityGroupType type)
 	{
-		moveBack(alloc, FLARES, x);
+		return (getCount(type)) ? (&m_groups[type].m_nodes[0] + getCount(type))
+								: nullptr;
 	}
 
-	void moveBackReflectionProbe(SceneFrameAllocator<U8> alloc, VisibleNode& x)
-	{
-		moveBack(alloc, REFLECTION_PROBES, x);
-	}
+	void moveBack(SceneFrameAllocator<U8> alloc,
+		VisibilityGroupType type,
+		VisibleNode& x);
 
-	void moveBackReflectionProxy(SceneFrameAllocator<U8> alloc, VisibleNode& x)
+	U32 getCount(VisibilityGroupType type) const
 	{
-		moveBack(alloc, REFLECTION_PROXIES, x);
+		return m_groups[type].m_count;
 	}
 
 	Timestamp getShapeUpdateTimestamp() const
@@ -207,16 +141,6 @@ public:
 private:
 	using Container = DArray<VisibleNode>;
 
-	enum GroupType
-	{
-		RENDERABLES,
-		LIGHTS,
-		FLARES,
-		REFLECTION_PROBES,
-		REFLECTION_PROXIES,
-		TYPE_COUNT
-	};
-
 	class Group
 	{
 	public:
@@ -224,27 +148,9 @@ private:
 		U32 m_count = 0;
 	};
 
-	Array<Group, TYPE_COUNT> m_groups;
+	Array<Group, U(VisibilityGroupType::TYPE_COUNT)> m_groups;
 
 	Timestamp m_shapeUpdateTimestamp = 0;
-
-	U32 getCount(GroupType type) const
-	{
-		return m_groups[type].m_count;
-	}
-
-	VisibleNode* getBegin(GroupType type)
-	{
-		return (getCount(type)) ? &m_groups[type].m_nodes[0] : nullptr;
-	}
-
-	VisibleNode* getEnd(GroupType type)
-	{
-		return (getCount(type)) ? (&m_groups[type].m_nodes[0] + getCount(type))
-								: nullptr;
-	}
-
-	void moveBack(SceneFrameAllocator<U8> alloc, GroupType, VisibleNode& x);
 };
 
 /// Do visibility tests bypassing portals

+ 6 - 3
src/renderer/Drawer.cpp

@@ -208,7 +208,8 @@ public:
 		VisibilityTestResults& vis = m_ctx.m_frc->getVisibilityTestResults();
 
 		PtrSize start, end;
-		U problemSize = vis.getRenderablesEnd() - vis.getRenderablesBegin();
+		U problemSize = vis.getEnd(VisibilityGroupType::RENDERABLES)
+			- vis.getBegin(VisibilityGroupType::RENDERABLES);
 		choseStartEnd(threadId, threadsCount, problemSize, start, end);
 
 		// Set the state of the command buffer
@@ -226,11 +227,13 @@ public:
 
 		for(U i = start; i < end; ++i)
 		{
-			m_ctx.m_visibleNode = vis.getRenderablesBegin() + i;
+			m_ctx.m_visibleNode =
+				vis.getBegin(VisibilityGroupType::RENDERABLES) + i;
 
 			if(i + 1 < end)
 			{
-				m_ctx.m_nextVisibleNode = vis.getRenderablesBegin() + i + 1;
+				m_ctx.m_nextVisibleNode =
+					vis.getBegin(VisibilityGroupType::RENDERABLES) + i + 1;
 			}
 			else
 			{

+ 4 - 2
src/renderer/Fs.cpp

@@ -81,8 +81,10 @@ Error Fs::run(CommandBufferPtr& cmdb)
 	cmdb->bindResourceGroup(m_globalResources, 1, &dyn);
 
 	SArray<CommandBufferPtr> cmdbs(&cmdb, 1);
-	ANKI_CHECK(m_r->getSceneDrawer().render(
-		camFr, RenderingStage::BLEND, Pass::MS_FS, cmdbs,
+	ANKI_CHECK(m_r->getSceneDrawer().render(camFr,
+		RenderingStage::BLEND,
+		Pass::MS_FS,
+		cmdbs,
 		UVec2(m_r->getWidth() / 2, m_r->getHeight() / 2)));
 
 	return ErrorCode::NONE;

+ 14 - 8
src/renderer/Ir.cpp

@@ -296,7 +296,8 @@ Error Ir::run(CommandBufferPtr cmdb)
 	FrustumComponent& frc = m_r->getActiveFrustumComponent();
 	VisibilityTestResults& visRez = frc.getVisibilityTestResults();
 
-	if(visRez.getReflectionProbeCount() > m_cubemapArrSize)
+	if(visRez.getCount(VisibilityGroupType::REFLECTION_PROBES)
+		> m_cubemapArrSize)
 	{
 		ANKI_LOGW("Increase the ir.cubemapTextureArraySize");
 	}
@@ -320,7 +321,8 @@ Error Ir::run(CommandBufferPtr cmdb)
 
 	// Probes GPU mem
 	void* data = getGrManager().allocateFrameHostVisibleMemory(
-		sizeof(IrShaderReflectionProbe) * visRez.getReflectionProbeCount()
+		sizeof(IrShaderReflectionProbe)
+				* visRez.getCount(VisibilityGroupType::REFLECTION_PROBES)
 			+ sizeof(Mat3x4)
 			+ sizeof(Vec4),
 		BufferUsage::STORAGE,
@@ -338,11 +340,13 @@ Error Ir::run(CommandBufferPtr cmdb)
 
 	SArray<IrShaderReflectionProbe> probes(
 		reinterpret_cast<IrShaderReflectionProbe*>(nearClusterDivisor + 1),
-		visRez.getReflectionProbeCount());
+		visRez.getCount(VisibilityGroupType::REFLECTION_PROBES));
 
 	// Render some of the probes
-	const VisibleNode* it = visRez.getReflectionProbesBegin();
-	const VisibleNode* end = visRez.getReflectionProbesEnd();
+	const VisibleNode* it =
+		visRez.getBegin(VisibilityGroupType::REFLECTION_PROBES);
+	const VisibleNode* end =
+		visRez.getEnd(VisibilityGroupType::REFLECTION_PROBES);
 
 	U probeIdx = 0;
 	while(it != end)
@@ -353,7 +357,8 @@ Error Ir::run(CommandBufferPtr cmdb)
 		++it;
 		++probeIdx;
 	}
-	ANKI_ASSERT(probeIdx == visRez.getReflectionProbeCount());
+	ANKI_ASSERT(
+		probeIdx == visRez.getCount(VisibilityGroupType::REFLECTION_PROBES));
 
 	//
 	// Start the jobs that can run in parallel
@@ -387,7 +392,7 @@ void Ir::binProbes(U32 threadId, PtrSize threadsCount, IrRunContext& ctx)
 	PtrSize start, end;
 	ThreadPool::Task::choseStartEnd(threadId,
 		threadsCount,
-		ctx.m_visRez->getReflectionProbeCount(),
+		ctx.m_visRez->getCount(VisibilityGroupType::REFLECTION_PROBES),
 		start,
 		end);
 
@@ -400,7 +405,8 @@ void Ir::binProbes(U32 threadId, PtrSize threadsCount, IrRunContext& ctx)
 
 	for(auto i = start; i < end; i++)
 	{
-		VisibleNode* vnode = ctx.m_visRez->getReflectionProbesBegin() + i;
+		VisibleNode* vnode =
+			ctx.m_visRez->getBegin(VisibilityGroupType::REFLECTION_PROBES) + i;
 		SceneNode& node = *vnode->m_node;
 
 		task.m_node = &node;

+ 4 - 4
src/renderer/Is.cpp

@@ -357,8 +357,8 @@ Error Is::lightPass(CommandBufferPtr& cmdb)
 	U spotCastersCount = 0;
 	U omniCastersCount = 0;
 
-	auto it = vi.getLightsBegin();
-	auto lend = vi.getLightsEnd();
+	auto it = vi.getBegin(VisibilityGroupType::LIGHTS);
+	auto lend = vi.getEnd(VisibilityGroupType::LIGHTS);
 	for(; it != lend; ++it)
 	{
 		SceneNode* node = (*it).m_node;
@@ -443,8 +443,8 @@ Error Is::lightPass(CommandBufferPtr& cmdb)
 		m_sLightsToken.markUnused();
 	}
 
-	taskData.m_lightsBegin = vi.getLightsBegin();
-	taskData.m_lightsEnd = vi.getLightsEnd();
+	taskData.m_lightsBegin = vi.getBegin(VisibilityGroupType::LIGHTS);
+	taskData.m_lightsEnd = vi.getEnd(VisibilityGroupType::LIGHTS);
 
 	taskData.m_is = this;
 

+ 9 - 7
src/renderer/Lf.cpp

@@ -160,12 +160,13 @@ void Lf::runOcclusionTests(CommandBufferPtr& cmdb)
 	FrustumComponent& camFr = m_r->getActiveFrustumComponent();
 	VisibilityTestResults& vi = camFr.getVisibilityTestResults();
 
-	if(vi.getLensFlaresCount() > m_maxFlares)
+	if(vi.getCount(VisibilityGroupType::FLARES) > m_maxFlares)
 	{
 		ANKI_LOGW("Visible flares exceed the limit. Increase lf.maxFlares");
 	}
 
-	U totalCount = min<U>(vi.getLensFlaresCount(), m_maxFlares);
+	U totalCount =
+		min<U>(vi.getCount(VisibilityGroupType::FLARES), m_maxFlares);
 	if(totalCount > 0)
 	{
 		// Setup MVP UBO
@@ -190,8 +191,8 @@ void Lf::runOcclusionTests(CommandBufferPtr& cmdb)
 		cmdb->bindResourceGroup(m_occlusionRcGroup, 0, &dyn);
 
 		// Iterate lens flare
-		auto it = vi.getLensFlaresBegin();
-		auto end = vi.getLensFlaresBegin() + totalCount;
+		auto it = vi.getBegin(VisibilityGroupType::FLARES);
+		auto end = vi.getBegin(VisibilityGroupType::FLARES) + totalCount;
 		for(; it != end; ++it)
 		{
 			LensFlareComponent& lf =
@@ -221,15 +222,16 @@ void Lf::run(CommandBufferPtr& cmdb)
 	FrustumComponent& camFr = m_r->getActiveFrustumComponent();
 	VisibilityTestResults& vi = camFr.getVisibilityTestResults();
 
-	U totalCount = min<U>(vi.getLensFlaresCount(), m_maxFlares);
+	U totalCount =
+		min<U>(vi.getCount(VisibilityGroupType::FLARES), m_maxFlares);
 	if(totalCount > 0)
 	{
 		// Set common rendering state
 		cmdb->bindPipeline(m_realPpline);
 
 		// Iterate lens flare
-		auto it = vi.getLensFlaresBegin();
-		auto end = vi.getLensFlaresBegin() + totalCount;
+		auto it = vi.getBegin(VisibilityGroupType::FLARES);
+		auto end = vi.getBegin(VisibilityGroupType::FLARES) + totalCount;
 		for(; it != end; ++it)
 		{
 			LensFlareComponent& lf =

+ 4 - 2
src/renderer/Ms.cpp

@@ -137,8 +137,10 @@ Error Ms::run(CommandBufferPtr& cmdb)
 	FrustumComponent& frc = m_r->getActiveFrustumComponent();
 	SArray<CommandBufferPtr> cmdbs(
 		&m_secondLevelCmdbs[0], m_secondLevelCmdbs.getSize());
-	ANKI_CHECK(m_r->getSceneDrawer().render(
-		frc, RenderingStage::MATERIAL, Pass::MS_FS, cmdbs, 
+	ANKI_CHECK(m_r->getSceneDrawer().render(frc,
+		RenderingStage::MATERIAL,
+		Pass::MS_FS,
+		cmdbs,
 		UVec2(m_r->getWidth(), m_r->getHeight())));
 
 	for(U i = 0; i < m_secondLevelCmdbs.getSize(); ++i)

+ 8 - 4
src/renderer/Sm.cpp

@@ -218,8 +218,10 @@ Error Sm::doSpotLight(SceneNode& light, CommandBufferPtr& cmdBuff)
 
 	FrustumComponent& fr = light.getComponent<FrustumComponent>();
 	SArray<CommandBufferPtr> cmdbs(&cmdBuff, 1);
-	ANKI_CHECK(m_r->getSceneDrawer().render(
-		fr, RenderingStage::MATERIAL, Pass::SM, cmdbs,
+	ANKI_CHECK(m_r->getSceneDrawer().render(fr,
+		RenderingStage::MATERIAL,
+		Pass::SM,
+		cmdbs,
 		UVec2(m_resolution, m_resolution)));
 
 	ANKI_TRACE_INC_COUNTER(RENDERER_SHADOW_PASSES, 1);
@@ -245,8 +247,10 @@ Error Sm::doOmniLight(SceneNode& light, CommandBufferPtr& cmdBuff)
 			cmdBuff->bindFramebuffer(sm->m_fb[frCount]);
 
 			SArray<CommandBufferPtr> cmdbs(&cmdBuff, 1);
-			ANKI_CHECK(m_r->getSceneDrawer().render(
-				fr, RenderingStage::MATERIAL, Pass::SM, cmdbs, 
+			ANKI_CHECK(m_r->getSceneDrawer().render(fr,
+				RenderingStage::MATERIAL,
+				Pass::SM,
+				cmdbs,
 				UVec2(m_resolution, m_resolution)));
 
 			++frCount;

+ 3 - 2
src/scene/FrustumComponent.cpp

@@ -30,8 +30,9 @@ void FrustumComponent::setVisibilityTestResults(VisibilityTestResults* visible)
 	ANKI_ASSERT(m_visible == nullptr);
 	m_visible = visible;
 
-	m_stats.m_renderablesCount = visible->getRenderablesCount();
-	m_stats.m_lightsCount = visible->getLightsCount();
+	m_stats.m_renderablesCount =
+		visible->getCount(VisibilityGroupType::RENDERABLES);
+	m_stats.m_lightsCount = visible->getCount(VisibilityGroupType::LIGHTS);
 }
 
 //==============================================================================

+ 33 - 20
src/scene/Visibility.cpp

@@ -331,7 +331,8 @@ void VisibilityTestTask::test(
 			if(wantsRenderComponents
 				|| (wantsShadowCasters && rc->getCastsShadow()))
 			{
-				visible->moveBackRenderable(alloc, visibleNode);
+				visible->moveBack(
+					alloc, VisibilityGroupType::RENDERABLES, visibleNode);
 
 				if(wantsShadowCasters)
 				{
@@ -342,22 +343,24 @@ void VisibilityTestTask::test(
 
 		if(lc && wantsLightComponents)
 		{
-			visible->moveBackLight(alloc, visibleNode);
+			visible->moveBack(alloc, VisibilityGroupType::LIGHTS, visibleNode);
 		}
 
 		if(lfc && wantsFlareComponents)
 		{
-			visible->moveBackLensFlare(alloc, visibleNode);
+			visible->moveBack(alloc, VisibilityGroupType::FLARES, visibleNode);
 		}
 
 		if(reflc && wantsReflectionProbes)
 		{
-			visible->moveBackReflectionProbe(alloc, visibleNode);
+			visible->moveBack(
+				alloc, VisibilityGroupType::REFLECTION_PROBES, visibleNode);
 		}
 
 		if(proxyc && wantsReflectionProxies)
 		{
-			visible->moveBackReflectionProxy(alloc, visibleNode);
+			visible->moveBack(
+				alloc, VisibilityGroupType::REFLECTION_PROXIES, visibleNode);
 		}
 
 		// Add more frustums to the list
@@ -433,11 +436,12 @@ void VisibilityTestTask::combineTestResults(
 
 	// Sort some of the arrays
 	DistanceSortFunctor comp;
-	std::sort(
-		visible->getRenderablesBegin(), visible->getRenderablesEnd(), comp);
+	std::sort(visible->getBegin(VisibilityGroupType::RENDERABLES),
+		visible->getEnd(VisibilityGroupType::RENDERABLES),
+		comp);
 
-	std::sort(visible->getReflectionProbesBegin(),
-		visible->getReflectionProbesEnd(),
+	std::sort(visible->getBegin(VisibilityGroupType::REFLECTION_PROBES),
+		visible->getEnd(VisibilityGroupType::REFLECTION_PROBES),
 		comp);
 }
 
@@ -452,18 +456,21 @@ void VisibilityTestResults::create(SceneFrameAllocator<U8> alloc,
 	U32 lensFlaresReservedSize,
 	U32 reflectionProbesReservedSize)
 {
-	m_groups[RENDERABLES].m_nodes.create(alloc, renderablesReservedSize);
-	m_groups[LIGHTS].m_nodes.create(alloc, lightsReservedSize);
-	m_groups[FLARES].m_nodes.create(alloc, lensFlaresReservedSize);
-	m_groups[REFLECTION_PROBES].m_nodes.create(
+	m_groups[VisibilityGroupType::RENDERABLES].m_nodes.create(
+		alloc, renderablesReservedSize);
+	m_groups[VisibilityGroupType::LIGHTS].m_nodes.create(
+		alloc, lightsReservedSize);
+	m_groups[VisibilityGroupType::FLARES].m_nodes.create(
+		alloc, lensFlaresReservedSize);
+	m_groups[VisibilityGroupType::REFLECTION_PROBES].m_nodes.create(
 		alloc, reflectionProbesReservedSize);
-	m_groups[REFLECTION_PROXIES].m_nodes.create(
+	m_groups[VisibilityGroupType::REFLECTION_PROXIES].m_nodes.create(
 		alloc, reflectionProbesReservedSize);
 }
 
 //==============================================================================
 void VisibilityTestResults::moveBack(
-	SceneFrameAllocator<U8> alloc, GroupType type, VisibleNode& x)
+	SceneFrameAllocator<U8> alloc, VisibilityGroupType type, VisibleNode& x)
 {
 	Group& group = m_groups[type];
 	if(group.m_count + 1 > group.m_nodes.getSize())
@@ -485,20 +492,24 @@ void VisibilityTestResults::combineWith(
 
 	// Count the visible scene nodes to optimize the allocation of the
 	// final result
-	Array<U, TYPE_COUNT> counts;
+	Array<U, U(VisibilityGroupType::TYPE_COUNT)> counts;
 	memset(&counts[0], 0, sizeof(counts));
-	for(U i = 0; i < results.getSize(); i++)
+	for(U i = 0; i < results.getSize(); ++i)
 	{
 		VisibilityTestResults& rez = *results[i];
 
-		for(U t = 0; t < TYPE_COUNT; ++t)
+		for(VisibilityGroupType t = VisibilityGroupType::FIRST;
+			t < VisibilityGroupType::TYPE_COUNT;
+			++t)
 		{
 			counts[t] += rez.m_groups[t].m_count;
 		}
 	}
 
 	// Allocate
-	for(U t = 0; t < TYPE_COUNT; ++t)
+	for(VisibilityGroupType t = VisibilityGroupType::FIRST;
+		t < VisibilityGroupType::TYPE_COUNT;
+		++t)
 	{
 		if(counts[t] > 0)
 		{
@@ -513,7 +524,9 @@ void VisibilityTestResults::combineWith(
 	{
 		VisibilityTestResults& rez = *results[i];
 
-		for(U t = 0; t < TYPE_COUNT; ++t)
+		for(VisibilityGroupType t = VisibilityGroupType::FIRST;
+			t < VisibilityGroupType::TYPE_COUNT;
+			++t)
 		{
 			U copyCount = rez.m_groups[t].m_count;
 			if(copyCount > 0)