Browse Source

Various optimizations and refactoring

Panagiotis Christopoulos Charitos 8 years ago
parent
commit
99eee58cac

+ 1 - 1
programs/Ssao.ankiprog

@@ -124,7 +124,7 @@ void main(void)
 	float projRadius = length(projSphereLimit2 - ndc);
 
 	// Find a random point around the current NDC. Make sure that the sides fall inside the screen.
-#if 1
+#if 0
 	vec2 startXY = -in_uv; // range [0,-1]
 	startXY += randFactors.xy; // for the left side it's [0,1] for the center [0,0], right [-1,0]
 	vec2 finalDiskPoint = ndc + startXY * projRadius;

+ 3 - 3
src/anki/renderer/Ssao.cpp

@@ -27,7 +27,7 @@ Error SsaoMain::init(const ConfigSet& config)
 	ShaderProgramResourceConstantValueInitList<6> consts(m_prog);
 	consts.add("NOISE_MAP_SIZE", U32(m_noiseTex->getWidth()))
 		.add("FB_SIZE", UVec2(m_ssao->m_width, m_ssao->m_height))
-		.add("RADIUS", 2.0f)
+		.add("RADIUS", 3.0f)
 		.add("BIAS", 0.0f)
 		.add("STRENGTH", 2.0f)
 		.add("HISTORY_FEEDBACK", 1.0f / 4.0f);
@@ -93,7 +93,7 @@ Error SsaoHBlur::init(const ConfigSet& config)
 	ANKI_CHECK(m_r->getResourceManager().loadResource("programs/DepthAwareBlur.ankiprog", m_prog));
 
 	ShaderProgramResourceMutationInitList<3> mutators(m_prog);
-	mutators.add("HORIZONTAL", 1).add("KERNEL_SIZE", 7).add("COLOR_COMPONENTS", 1);
+	mutators.add("HORIZONTAL", 1).add("KERNEL_SIZE", 9).add("COLOR_COMPONENTS", 1);
 	ShaderProgramResourceConstantValueInitList<1> consts(m_prog);
 	consts.add("TEXTURE_SIZE", UVec2(m_ssao->m_width, m_ssao->m_height));
 
@@ -140,7 +140,7 @@ Error SsaoVBlur::init(const ConfigSet& config)
 	ANKI_CHECK(m_r->getResourceManager().loadResource("programs/DepthAwareBlur.ankiprog", m_prog));
 
 	ShaderProgramResourceMutationInitList<3> mutators(m_prog);
-	mutators.add("HORIZONTAL", 0).add("KERNEL_SIZE", 7).add("COLOR_COMPONENTS", 1);
+	mutators.add("HORIZONTAL", 0).add("KERNEL_SIZE", 9).add("COLOR_COMPONENTS", 1);
 	ShaderProgramResourceConstantValueInitList<1> consts(m_prog);
 	consts.add("TEXTURE_SIZE", UVec2(m_ssao->m_width, m_ssao->m_height));
 

+ 6 - 6
src/anki/scene/Light.cpp

@@ -26,7 +26,7 @@ public:
 		updated = false;
 		Light& lnode = static_cast<Light&>(node);
 
-		MoveComponent& move = node.getComponent<MoveComponent>();
+		const MoveComponent& move = node.getComponentAt<MoveComponent>(0);
 		if(move.getTimestamp() == node.getGlobalTimestamp())
 		{
 			// Move updated
@@ -51,7 +51,7 @@ public:
 		updated = false;
 		Light& lnode = static_cast<Light&>(node);
 
-		LightComponent& light = node.getComponent<LightComponent>();
+		LightComponent& light = node.getComponentAt<LightComponent>(getIndex() - 1);
 		if(light.getTimestamp() == node.getGlobalTimestamp())
 		{
 			// Shape updated
@@ -112,7 +112,7 @@ void Light::frameUpdateCommon()
 	(void)err;
 }
 
-void Light::onMoveUpdateCommon(MoveComponent& move)
+void Light::onMoveUpdateCommon(const MoveComponent& move)
 {
 	// Update the spatial
 	SpatialComponent& sp = getComponent<SpatialComponent>();
@@ -154,7 +154,7 @@ Error Light::loadLensFlare(const CString& filename)
 	Error err = flareComp->init(filename);
 	if(err)
 	{
-		getSceneAllocator().deleteInstance(flareComp);
+		ANKI_ASSERT(!"TODO: Remove component");
 		return err;
 	}
 
@@ -176,7 +176,7 @@ Error PointLight::init()
 	return Light::init(LightComponentType::POINT, &m_sphereW);
 }
 
-void PointLight::onMoveUpdate(MoveComponent& move)
+void PointLight::onMoveUpdate(const MoveComponent& move)
 {
 	onMoveUpdateCommon(move);
 
@@ -268,7 +268,7 @@ Error SpotLight::init()
 	return ErrorCode::NONE;
 }
 
-void SpotLight::onMoveUpdate(MoveComponent& move)
+void SpotLight::onMoveUpdate(const MoveComponent& move)
 {
 	// Update the frustums
 	Error err = iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& fr) -> Error {

+ 4 - 4
src/anki/scene/Light.h

@@ -31,14 +31,14 @@ public:
 
 protected:
 	/// Called when moved
-	void onMoveUpdateCommon(MoveComponent& move);
+	void onMoveUpdateCommon(const MoveComponent& move);
 
 	/// One of the frustums got updated
 	void onShapeUpdateCommon(LightComponent& light);
 
 	void frameUpdateCommon();
 
-	virtual void onMoveUpdate(MoveComponent& move) = 0;
+	virtual void onMoveUpdate(const MoveComponent& move) = 0;
 
 	virtual void onShapeUpdate(LightComponent& light) = 0;
 
@@ -69,7 +69,7 @@ private:
 	Sphere m_sphereW = Sphere(Vec4(0.0), 1.0);
 	DynamicArray<ShadowCombo> m_shadowData;
 
-	void onMoveUpdate(MoveComponent& move) override;
+	void onMoveUpdate(const MoveComponent& move) override;
 	void onShapeUpdate(LightComponent& light) override;
 };
 
@@ -86,7 +86,7 @@ public:
 private:
 	PerspectiveFrustum m_frustum;
 
-	void onMoveUpdate(MoveComponent& move) override;
+	void onMoveUpdate(const MoveComponent& move) override;
 	void onShapeUpdate(LightComponent& light) override;
 };
 /// @}

+ 8 - 8
src/anki/scene/ModelNode.cpp

@@ -102,15 +102,15 @@ void ModelPatchNode::drawCallback(RenderQueueDrawContext& ctx, WeakArray<const v
 
 	// Uniforms
 	Array<Mat4, MAX_INSTANCES> trfs;
-	trfs[0] = Mat4(self.getParent()->getComponent<MoveComponent>().getWorldTransform());
+	trfs[0] = Mat4(self.getParent()->getComponentAt<MoveComponent>(0).getWorldTransform());
 	for(U i = 1; i < userData.getSize(); ++i)
 	{
 		const ModelPatchNode& self2 = *static_cast<const ModelPatchNode*>(userData[i]);
-		trfs[i] = Mat4(self2.getParent()->getComponent<MoveComponent>().getWorldTransform());
+		trfs[i] = Mat4(self2.getParent()->getComponentAt<MoveComponent>(0).getWorldTransform());
 	}
 
 	StagingGpuMemoryToken token;
-	self.getComponent<RenderComponent>().allocateAndSetupUniforms(
+	self.getComponentAt<RenderComponent>(1).allocateAndSetupUniforms(
 		ctx, WeakArray<const Mat4>(&trfs[0], userData.getSize()), *ctx.m_stagingGpuAllocator, token);
 	cmdb->bindUniformBuffer(0, 0, token.m_buffer, token.m_offset, token.m_range);
 
@@ -124,10 +124,10 @@ void ModelPatchNode::drawCallback(RenderQueueDrawContext& ctx, WeakArray<const v
 }
 
 /// Feedback component.
-class ModelMoveFeedbackComponent : public SceneComponent
+class ModelNode::MoveFeedbackComponent : public SceneComponent
 {
 public:
-	ModelMoveFeedbackComponent(SceneNode* node)
+	MoveFeedbackComponent(SceneNode* node)
 		: SceneComponent(SceneComponentType::NONE, node)
 	{
 	}
@@ -136,7 +136,7 @@ public:
 	{
 		updated = false;
 
-		MoveComponent& move = node.getComponent<MoveComponent>();
+		const MoveComponent& move = node.getComponentAt<MoveComponent>(0);
 		if(move.getTimestamp() == node.getGlobalTimestamp())
 		{
 			ModelNode& mnode = static_cast<ModelNode&>(node);
@@ -180,12 +180,12 @@ Error ModelNode::init(const CString& modelFname)
 	newComponent<MoveComponent>(this);
 
 	// Feedback component
-	newComponent<ModelMoveFeedbackComponent>(this);
+	newComponent<MoveFeedbackComponent>(this);
 
 	return ErrorCode::NONE;
 }
 
-void ModelNode::onMoveComponentUpdate(MoveComponent& move)
+void ModelNode::onMoveComponentUpdate(const MoveComponent& move)
 {
 	// Inform the children about the moves
 	for(ModelPatchNode* child : m_modelPatches)

+ 3 - 2
src/anki/scene/ModelNode.h

@@ -55,7 +55,6 @@ private:
 class ModelNode : public SceneNode
 {
 	friend class ModelPatchNode;
-	friend class ModelMoveFeedbackComponent;
 
 public:
 	ModelNode(SceneGraph* scene, CString name);
@@ -70,10 +69,12 @@ public:
 	}
 
 private:
+	class MoveFeedbackComponent;
+
 	ModelResourcePtr m_model; ///< The resource
 	DynamicArray<ModelPatchNode*> m_modelPatches;
 
-	void onMoveComponentUpdate(MoveComponent& move);
+	void onMoveComponentUpdate(const MoveComponent& move);
 };
 /// @}
 

+ 1 - 0
src/anki/scene/SceneComponent.cpp

@@ -14,6 +14,7 @@ SceneComponent::SceneComponent(SceneComponentType type, SceneNode* node)
 	: m_node(node)
 	, m_type(type)
 	, m_uuid(node->getSceneGraph().getNewUuid())
+	, m_idx(node->getComponentCount())
 {
 	if(m_type != SceneComponentType::NONE)
 	{

+ 7 - 0
src/anki/scene/SceneComponent.h

@@ -96,6 +96,12 @@ public:
 		return *m_node;
 	}
 
+	/// The position in the owner SceneNode.
+	U getIndex() const
+	{
+		return m_idx;
+	}
+
 	SceneAllocator<U8> getAllocator() const;
 
 	SceneFrameAllocator<U8> getFrameAllocator() const;
@@ -111,6 +117,7 @@ protected:
 private:
 	SceneComponentType m_type;
 	U64 m_uuid;
+	U32 m_idx;
 };
 
 /// Multiple lists of all types of components.

+ 1 - 1
src/anki/scene/SceneNode.cpp

@@ -24,7 +24,7 @@ SceneNode::~SceneNode()
 	auto alloc = getSceneAllocator();
 
 	auto it = m_components.begin();
-	auto end = m_components.begin() + m_componentsCount;
+	auto end = m_components.begin() + m_componentCount;
 	for(; it != end; ++it)
 	{
 		SceneComponent* comp = *it;

+ 30 - 12
src/anki/scene/SceneNode.h

@@ -57,11 +57,6 @@ public:
 		return m_uuid;
 	}
 
-	U32 getComponentsCount() const
-	{
-		return m_componentsCount;
-	}
-
 	Bool getMarkedForDeletion() const
 	{
 		return m_flags.get(Flag::MARKED_FOR_DELETION);
@@ -121,7 +116,7 @@ public:
 	{
 		Error err = ErrorCode::NONE;
 		auto it = m_components.getBegin();
-		auto end = it + m_componentsCount;
+		auto end = it + m_componentCount;
 		for(; !err && it != end; ++it)
 		{
 			err = func(*(*it));
@@ -136,7 +131,7 @@ public:
 	{
 		Error err = ErrorCode::NONE;
 		auto it = m_components.getBegin();
-		auto end = it + m_componentsCount;
+		auto end = it + m_componentCount;
 		for(; !err && it != end; ++it)
 		{
 			auto* comp = *it;
@@ -153,7 +148,7 @@ public:
 	template<typename Component>
 	Component* tryGetComponent()
 	{
-		U count = m_componentsCount;
+		U count = m_componentCount;
 		while(count-- != 0)
 		{
 			SceneComponent* comp = m_components[count];
@@ -169,7 +164,7 @@ public:
 	template<typename Component>
 	const Component* tryGetComponent() const
 	{
-		U count = m_componentsCount;
+		U count = m_componentCount;
 		while(count-- != 0)
 		{
 			const SceneComponent* comp = m_components[count];
@@ -199,6 +194,29 @@ public:
 		return *out;
 	}
 
+	/// Get the nth component.
+	template<typename Component>
+	Component& getComponentAt(U idx)
+	{
+		ANKI_ASSERT(idx < m_componentCount);
+		ANKI_ASSERT(m_components[idx]->getType() == Component::CLASS_TYPE);
+		return *static_cast<Component*>(m_components[idx]);
+	}
+
+	/// Get the nth component.
+	template<typename Component>
+	const Component& getComponentAt(U idx) const
+	{
+		ANKI_ASSERT(idx < m_componentCount);
+		ANKI_ASSERT(m_components[idx]->getType() == Component::CLASS_TYPE);
+		return *static_cast<const Component*>(m_components[idx]);
+	}
+
+	U getComponentCount() const
+	{
+		return m_componentCount;
+	}
+
 protected:
 	/// Create and append a component to the components container. The SceneNode will not take ownership.
 	template<typename TComponent, typename... TArgs>
@@ -206,14 +224,14 @@ protected:
 	{
 		TComponent* comp = getSceneAllocator().newInstance<TComponent>(std::forward<TArgs>(args)...);
 
-		if(m_components.getSize() <= m_componentsCount)
+		if(m_components.getSize() <= m_componentCount)
 		{
 			// Not enough room
 			const U extra = 2;
 			m_components.resize(getSceneAllocator(), max<PtrSize>(m_components.getSize() + extra, 1));
 		}
 
-		m_components[m_componentsCount++] = comp;
+		m_components[m_componentCount++] = comp;
 		return comp;
 	}
 
@@ -229,7 +247,7 @@ private:
 	SceneGraph* m_scene = nullptr;
 
 	DynamicArray<SceneComponent*> m_components;
-	U8 m_componentsCount = 0;
+	U8 m_componentCount = 0;
 
 	String m_name; ///< A unique name
 	BitMask<Flag> m_flags;

+ 10 - 8
src/anki/scene/Visibility.cpp

@@ -150,6 +150,7 @@ void VisibilityContext::submitNewWork(FrustumComponent& frc, RenderQueue& rqueue
 	combine->m_frc = &frc;
 	combine->m_results = &rqueue;
 	combine->m_tests = tests;
+	combine->m_swRast = r;
 
 	ThreadHiveTask combineTask;
 	combineTask.m_callback = CombineResultsTask::callback;
@@ -202,16 +203,12 @@ void RasterizeTrianglesTask::rasterize()
 {
 	ANKI_TRACE_SCOPED_EVENT(SCENE_VISIBILITY_RASTERIZE);
 
-	PtrSize start, end;
-	ThreadPoolTask::choseStartEnd(m_taskIdx, m_taskCount, m_gatherTask->m_vertCount / 3, start, end);
+	const U totalVertCount = m_gatherTask->m_vertCount;
 
-	if(start != end)
+	U32 idx;
+	while((idx = m_gatherTask->m_rasterizedVertCount.fetchAdd(3)) < totalVertCount)
 	{
-		const Vec3* first = &m_gatherTask->m_verts[start * 3];
-		U count = (end - start) * 3;
-		ANKI_ASSERT(count <= m_gatherTask->m_vertCount);
-
-		m_gatherTask->m_r.draw(&first[0][0], count, sizeof(Vec3), false);
+		m_gatherTask->m_r.draw(&m_gatherTask->m_verts[idx][0], 3, sizeof(Vec3), false);
 	}
 }
 
@@ -524,6 +521,11 @@ void CombineResultsTask::combine()
 	std::sort(m_results->m_forwardShadingRenderables.getBegin(),
 		m_results->m_forwardShadingRenderables.getEnd(),
 		RevDistanceSortFunctor<RenderableQueueElement>());
+
+	if(m_swRast)
+	{
+		m_swRast->~SoftwareRasterizer();
+	}
 }
 
 template<typename T>

+ 5 - 1
src/anki/scene/VisibilityInternal.h

@@ -111,7 +111,9 @@ public:
 	DynamicArray<Vec3> m_verts;
 	U32 m_vertCount;
 
-	SoftwareRasterizer m_r; // TODO This will never be destroyed
+	SoftwareRasterizer m_r;
+
+	Atomic<U32> m_rasterizedVertCount = {0}; ///< That will be used by the RasterizeTrianglesTask.
 
 	/// Thread hive task.
 	static void callback(void* ud, U32 threadId, ThreadHive& hive)
@@ -210,6 +212,8 @@ public:
 
 	WeakPtr<RenderQueue> m_results; ///< Where to store the results.
 
+	SoftwareRasterizer* m_swRast = nullptr; ///< For cleanup.
+
 	/// Thread hive task.
 	static void callback(void* ud, U32 threadId, ThreadHive& hive)
 	{