Browse Source

Minor refactoring

Panagiotis Christopoulos Charitos 2 years ago
parent
commit
5bdded2bc3

+ 5 - 2
AnKi/Renderer/IndirectDiffuseProbes.cpp

@@ -300,17 +300,20 @@ void IndirectDiffuseProbes::populateRenderGraph(RenderingContext& rctx)
 
 		// Shadow visibility. Optional
 		Array<GpuVisibilityOutput, 6> shadowVisOuts;
-		Array<Mat4, 6> cascadeViewProjMats;
+		Array<Mat4, 6> cascadeProjMats;
 		Array<Mat3x4, 6> cascadeViewMats;
+		Array<Mat4, 6> cascadeViewProjMats;
 		if(doShadows)
 		{
 			for(U32 i = 0; i < 6; ++i)
 			{
 				constexpr U32 kCascadeCount = 1;
 				dirLightc->computeCascadeFrustums(frustums[i], Array<F32, kCascadeCount>{probeToRefresh->getShadowsRenderRadius()},
-												  WeakArray<Mat4>(&cascadeViewProjMats[i], kCascadeCount),
+												  WeakArray<Mat4>(&cascadeProjMats[i], kCascadeCount),
 												  WeakArray<Mat3x4>(&cascadeViewMats[i], kCascadeCount));
 
+				cascadeViewProjMats[i] = cascadeProjMats[i] * Mat4(cascadeViewMats[i], Vec4(0.0f, 0.0f, 0.0f, 1.0f));
+
 				Array<F32, kMaxLodCount - 1> lodDistances = {1000.0f, 1001.0f}; // Something far to force detailed LODs
 
 				FrustumGpuVisibilityInput visIn;

+ 4 - 1
AnKi/Renderer/ProbeReflections.cpp

@@ -417,15 +417,18 @@ void ProbeReflections::populateRenderGraph(RenderingContext& rctx)
 	Array<GpuVisibilityOutput, 6> shadowVisOuts;
 	Array<Mat4, 6> cascadeViewProjMats;
 	Array<Mat3x4, 6> cascadeViewMats;
+	Array<Mat4, 6> cascadeProjMats;
 	if(doShadows)
 	{
 		for(U i = 0; i < 6; ++i)
 		{
 			constexpr U32 kCascadeCount = 1;
 			dirLightc->computeCascadeFrustums(frustums[i], Array<F32, kCascadeCount>{probeToRefresh->getShadowsRenderRadius()},
-											  WeakArray<Mat4>(&cascadeViewProjMats[i], kCascadeCount),
+											  WeakArray<Mat4>(&cascadeProjMats[i], kCascadeCount),
 											  WeakArray<Mat3x4>(&cascadeViewMats[i], kCascadeCount));
 
+			cascadeViewProjMats[i] = cascadeProjMats[i] * Mat4(cascadeViewMats[i], Vec4(0.0f, 0.0f, 0.0f, 1.0f));
+
 			Array<F32, kMaxLodCount - 1> lodDistances = {1000.0f, 1001.0f}; // Something far to force detailed LODs
 
 			FrustumGpuVisibilityInput visIn;

+ 17 - 11
AnKi/Renderer/ShadowMapping.cpp

@@ -354,23 +354,29 @@ void ShadowMapping::processLights(RenderingContext& ctx)
 
 		Array<Mat4, kMaxShadowCascades> cascadeViewProjMats;
 		Array<Mat3x4, kMaxShadowCascades> cascadeViewMats;
-		dirLight->computeCascadeFrustums(mainCam.getFrustum(), {&cascadeDistances[0], cascadeCount}, {&cascadeViewProjMats[0], cascadeCount},
+		Array<Mat4, kMaxShadowCascades> cascadeProjMats;
+		dirLight->computeCascadeFrustums(mainCam.getFrustum(), {&cascadeDistances[0], cascadeCount}, {&cascadeProjMats[0], cascadeCount},
 										 {&cascadeViewMats[0], cascadeCount});
+		for(U cascade = 0; cascade < cascadeCount; ++cascade)
+		{
+			cascadeViewProjMats[cascade] = cascadeProjMats[cascade] * Mat4(cascadeViewMats[cascade], Vec4(0.0f, 0.0f, 0.0f, 1.0f));
+		}
 
 		// HZB generation
-		Array<RenderTargetHandle, kMaxShadowCascades> hzbRts;
-		Array<UVec2, kMaxShadowCascades> hzbSizes;
-		Array<Mat4, kMaxShadowCascades> dstViewProjectionMats;
+		HzbDirectionalLightInput hzbGenIn;
+		hzbGenIn.m_cascadeCount = cascadeCount;
+		hzbGenIn.m_depthBufferRt = getRenderer().getGBuffer().getDepthRt();
+		hzbGenIn.m_depthBufferRtSize = getRenderer().getInternalResolution();
+		hzbGenIn.m_cameraInverseViewProjectionMatrix = ctx.m_matrices.m_invertedViewProjection;
 		for(U cascade = 0; cascade < cascadeCount; ++cascade)
 		{
-			hzbRts[cascade] = rgraph.newRenderTarget(m_cascadeHzbRtDescrs[cascade]);
-			hzbSizes[cascade] = UVec2(m_cascadeHzbRtDescrs[cascade].m_width, m_cascadeHzbRtDescrs[cascade].m_height);
-			dstViewProjectionMats[cascade] = cascadeViewProjMats[cascade];
+			hzbGenIn.m_cascades[cascade].m_hzbRt = rgraph.newRenderTarget(m_cascadeHzbRtDescrs[cascade]);
+			hzbGenIn.m_cascades[cascade].m_hzbRtSize = UVec2(m_cascadeHzbRtDescrs[cascade].m_width, m_cascadeHzbRtDescrs[cascade].m_height);
+			hzbGenIn.m_cascades[cascade].m_viewMatrix = cascadeViewMats[cascade];
+			hzbGenIn.m_cascades[cascade].m_projectionMatrix = cascadeProjMats[cascade];
 		}
 
-		getRenderer().getHzbGenerator().populateRenderGraphDirectionalLight(
-			getRenderer().getGBuffer().getDepthRt(), getRenderer().getInternalResolution(), {hzbRts.getBegin(), cascadeCount},
-			{dstViewProjectionMats.getBegin(), cascadeCount}, {hzbSizes.getBegin(), cascadeCount}, ctx.m_matrices.m_invertedViewProjection, rgraph);
+		getRenderer().getHzbGenerator().populateRenderGraphDirectionalLight(hzbGenIn, rgraph);
 
 		// Vis testing
 		for(U cascade = 0; cascade < cascadeCount; ++cascade)
@@ -388,7 +394,7 @@ void ShadowMapping::processLights(RenderingContext& ctx)
 			visIn.m_viewProjectionMatrix = cascadeViewProjMats[cascade];
 			visIn.m_lodReferencePoint = ctx.m_matrices.m_cameraTransform.getTranslationPart().xyz();
 			visIn.m_lodDistances = lodDistances;
-			visIn.m_hzbRt = &hzbRts[cascade];
+			visIn.m_hzbRt = &hzbGenIn.m_cascades[cascade].m_hzbRt;
 			visIn.m_rgraph = &rgraph;
 
 			getRenderer().getGpuVisibility().populateRenderGraph(visIn, work.m_visOut);

+ 20 - 18
AnKi/Renderer/Utils/HzbGenerator.cpp

@@ -195,18 +195,15 @@ void HzbGenerator::populateRenderGraph(RenderTargetHandle srcDepthRt, UVec2 srcD
 	populateRenderGraphInternal({&in, 1}, 0, customName, rgraph);
 }
 
-void HzbGenerator::populateRenderGraphDirectionalLight(RenderTargetHandle srcDepthRt, UVec2 srcDepthRtSize,
-													   ConstWeakArray<RenderTargetHandle> dstHzbRts, ConstWeakArray<Mat4> dstViewProjectionMats,
-													   ConstWeakArray<UVec2> dstHzbSizes, const Mat4& invViewProjMat,
-													   RenderGraphDescription& rgraph) const
+void HzbGenerator::populateRenderGraphDirectionalLight(const HzbDirectionalLightInput& in, RenderGraphDescription& rgraph) const
 {
-	RenderTargetHandle maxDepthRt;
-	constexpr U32 kTileSize = 64;
-	const UVec2 maxDepthRtSize((srcDepthRtSize.x() + kTileSize - 1) / kTileSize, (srcDepthRtSize.y() + kTileSize - 1) / kTileSize);
-	const U32 cascadeCount = dstHzbRts.getSize();
+	const U32 cascadeCount = in.m_cascadeCount;
 	ANKI_ASSERT(cascadeCount > 0);
 
 	// Generate a temp RT with the max depth of each 64x64 tile of the depth buffer
+	RenderTargetHandle maxDepthRt;
+	constexpr U32 kTileSize = 64;
+	const UVec2 maxDepthRtSize = (in.m_depthBufferRtSize + kTileSize - 1) / kTileSize;
 	{
 		RenderTargetDescription maxDepthRtDescr("HZB max tile depth");
 		maxDepthRtDescr.m_width = maxDepthRtSize.x();
@@ -217,13 +214,13 @@ void HzbGenerator::populateRenderGraphDirectionalLight(RenderTargetHandle srcDep
 
 		ComputeRenderPassDescription& pass = rgraph.newComputeRenderPass("HZB max tile depth");
 
-		pass.newTextureDependency(srcDepthRt, TextureUsageBit::kSampledCompute, DepthStencilAspectBit::kDepth);
+		pass.newTextureDependency(in.m_depthBufferRt, TextureUsageBit::kSampledCompute, DepthStencilAspectBit::kDepth);
 		pass.newTextureDependency(maxDepthRt, TextureUsageBit::kImageComputeWrite);
 
-		pass.setWork([this, srcDepthRt, maxDepthRt, maxDepthRtSize, srcDepthRtSize](RenderPassWorkContext& rgraphCtx) {
+		pass.setWork([this, depthBufferRt = in.m_depthBufferRt, maxDepthRt, maxDepthRtSize](RenderPassWorkContext& rgraphCtx) {
 			CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
 
-			rgraphCtx.bindTexture(0, 0, srcDepthRt, TextureSubresourceInfo(DepthStencilAspectBit::kDepth));
+			rgraphCtx.bindTexture(0, 0, depthBufferRt, TextureSubresourceInfo(DepthStencilAspectBit::kDepth));
 			cmdb.bindSampler(0, 1, getRenderer().getSamplers().m_trilinearClamp.get());
 			rgraphCtx.bindImage(0, 2, maxDepthRt);
 
@@ -237,9 +234,11 @@ void HzbGenerator::populateRenderGraphDirectionalLight(RenderTargetHandle srcDep
 	Array<RenderTargetHandle, kMaxShadowCascades> depthRts;
 	for(U32 i = 0; i < cascadeCount; ++i)
 	{
+		const HzbDirectionalLightInput::Cascade& cascade = in.m_cascades[i];
+
 		RenderTargetDescription depthRtDescr("HZB boxes depth");
-		depthRtDescr.m_width = dstHzbSizes[i].x() * 2;
-		depthRtDescr.m_height = dstHzbSizes[i].y() * 2;
+		depthRtDescr.m_width = cascade.m_hzbRtSize.x() * 2;
+		depthRtDescr.m_height = cascade.m_hzbRtSize.y() * 2;
 		depthRtDescr.m_format = Format::kD16_Unorm;
 		depthRtDescr.bake();
 		depthRts[i] = rgraph.newRenderTarget(depthRtDescr);
@@ -251,8 +250,9 @@ void HzbGenerator::populateRenderGraphDirectionalLight(RenderTargetHandle srcDep
 		pass.newTextureDependency(maxDepthRt, TextureUsageBit::kSampledFragment);
 		pass.newTextureDependency(depthRts[i], TextureUsageBit::kFramebufferWrite, DepthStencilAspectBit::kDepth);
 
-		pass.setWork([this, maxDepthRt, invViewProjMat, lightViewProjMat = dstViewProjectionMats[i], viewport = dstHzbSizes[i] * 2,
-					  maxDepthRtSize](RenderPassWorkContext& rgraphCtx) {
+		pass.setWork([this, maxDepthRt, invViewProjMat = in.m_cameraInverseViewProjectionMatrix,
+					  lightViewProjMat = cascade.m_projectionMatrix * Mat4(cascade.m_viewMatrix, Vec4(0.0f, 0.0f, 0.0f, 1.0f)),
+					  viewport = cascade.m_hzbRtSize * 2, maxDepthRtSize](RenderPassWorkContext& rgraphCtx) {
 			CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
 
 			cmdb.setDepthCompareOperation(CompareOperation::kGreater);
@@ -285,10 +285,12 @@ void HzbGenerator::populateRenderGraphDirectionalLight(RenderTargetHandle srcDep
 	Array<DispatchInput, kMaxShadowCascades> inputs;
 	for(U32 i = 0; i < cascadeCount; ++i)
 	{
-		inputs[i].m_dstHzbRt = dstHzbRts[i];
-		inputs[i].m_dstHzbRtSize = dstHzbSizes[i];
+		const HzbDirectionalLightInput::Cascade& cascade = in.m_cascades[i];
+
+		inputs[i].m_dstHzbRt = cascade.m_hzbRt;
+		inputs[i].m_dstHzbRtSize = cascade.m_hzbRtSize;
 		inputs[i].m_srcDepthRt = depthRts[i];
-		inputs[i].m_srcDepthRtSize = dstHzbSizes[i] * 2;
+		inputs[i].m_srcDepthRtSize = cascade.m_hzbRtSize * 2;
 	}
 	populateRenderGraphInternal({&inputs[0], cascadeCount}, 1, "HZB generation shadow cascades", rgraph);
 }

+ 23 - 3
AnKi/Renderer/Utils/HzbGenerator.h

@@ -12,6 +12,28 @@ namespace anki {
 /// @addtogroup renderer
 /// @{
 
+/// @memberof HzbGenerator
+class HzbDirectionalLightInput
+{
+public:
+	class Cascade
+	{
+	public:
+		RenderTargetHandle m_hzbRt;
+		UVec2 m_hzbRtSize;
+		Mat3x4 m_viewMatrix;
+		Mat4 m_projectionMatrix;
+	};
+
+	RenderTargetHandle m_depthBufferRt;
+	UVec2 m_depthBufferRtSize;
+
+	Mat4 m_cameraInverseViewProjectionMatrix;
+
+	Array<Cascade, kMaxShadowCascades> m_cascades;
+	U32 m_cascadeCount = 0;
+};
+
 /// Hierarchical depth generator.
 class HzbGenerator : public RendererObject
 {
@@ -21,9 +43,7 @@ public:
 	void populateRenderGraph(RenderTargetHandle srcDepthRt, UVec2 srcDepthRtSize, RenderTargetHandle dstHzbRt, UVec2 dstHzbRtSize,
 							 RenderGraphDescription& rgraph, CString customName = {}) const;
 
-	void populateRenderGraphDirectionalLight(RenderTargetHandle srcDepthRt, UVec2 srcDepthRtSize, ConstWeakArray<RenderTargetHandle> dstHzbRts,
-											 ConstWeakArray<Mat4> dstViewProjectionMats, ConstWeakArray<UVec2> dstHzbSizes,
-											 const Mat4& invViewProjMat, RenderGraphDescription& rgraph) const;
+	void populateRenderGraphDirectionalLight(const HzbDirectionalLightInput& in, RenderGraphDescription& rgraph) const;
 
 private:
 	class DispatchInput

+ 6 - 10
AnKi/Scene/Components/LightComponent.cpp

@@ -204,15 +204,15 @@ Error LightComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
 	return Error::kNone;
 }
 
-void LightComponent::computeCascadeFrustums(const Frustum& primaryFrustum, ConstWeakArray<F32> cascadeDistances, WeakArray<Mat4> cascadeViewProjMats,
+void LightComponent::computeCascadeFrustums(const Frustum& primaryFrustum, ConstWeakArray<F32> cascadeDistances, WeakArray<Mat4> cascadeProjMats,
 											WeakArray<Mat3x4> cascadeViewMats) const
 {
 	ANKI_ASSERT(m_type == LightComponentType::kDirectional);
 	ANKI_ASSERT(m_shadow);
-	ANKI_ASSERT(cascadeViewProjMats.getSize() <= kMaxShadowCascades && cascadeViewProjMats.getSize() > 0);
-	ANKI_ASSERT(cascadeDistances.getSize() == cascadeViewProjMats.getSize());
+	ANKI_ASSERT(cascadeProjMats.getSize() <= kMaxShadowCascades && cascadeProjMats.getSize() > 0);
+	ANKI_ASSERT(cascadeDistances.getSize() == cascadeProjMats.getSize());
 
-	const U32 shadowCascadeCount = cascadeViewProjMats.getSize();
+	const U32 shadowCascadeCount = cascadeProjMats.getSize();
 
 	// Compute the texture matrices
 	if(primaryFrustum.getFrustumType() == FrustumType::kPerspective)
@@ -324,12 +324,8 @@ void LightComponent::computeCascadeFrustums(const Frustum& primaryFrustum, Const
 			}
 
 			// Write the results
-			cascadeViewProjMats[cascade] = cascadeProjMat * cascadeViewMat;
-
-			if(cascade < cascadeViewMats.getSize())
-			{
-				cascadeViewMats[cascade] = Mat3x4(cascadeViewMat);
-			}
+			cascadeProjMats[cascade] = cascadeProjMat;
+			cascadeViewMats[cascade] = Mat3x4(cascadeViewMat);
 		}
 	}
 	else

+ 2 - 2
AnKi/Scene/Components/LightComponent.h

@@ -138,9 +138,9 @@ public:
 	/// Calculate some matrices for each cascade. For dir lights.
 	/// @param cameraFrustum Who is looking at the light.
 	/// @param cascadeDistances The distances of the cascades.
-	/// @param cascadeViewProjMats View projection matrices for each cascade.
+	/// @param cascadeProjMats View projection matrices for each cascade.
 	/// @param cascadeViewMats View matrices for each cascade. Optional.
-	void computeCascadeFrustums(const Frustum& cameraFrustum, ConstWeakArray<F32> cascadeDistances, WeakArray<Mat4> cascadeViewProjMats,
+	void computeCascadeFrustums(const Frustum& cameraFrustum, ConstWeakArray<F32> cascadeDistances, WeakArray<Mat4> cascadeProjMats,
 								WeakArray<Mat3x4> cascadeViewMats = {}) const;
 
 	U32 getUuid() const