Browse Source

Move RT shadows to the new clusters

Panagiotis Christopoulos Charitos 4 years ago
parent
commit
07ec07689d

+ 3 - 3
AnKi/Renderer/ClusterBinning.cpp

@@ -74,7 +74,7 @@ void ClusterBinning::run(RenderPassWorkContext& rgraphCtx)
 {
 	CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
 
-	const ClustererGpuObjects& tokens = m_runCtx.m_ctx->m_clusterShading;
+	const ClusteredShadingContext& tokens = m_runCtx.m_ctx->m_clusterShading;
 
 	cmdb->bindShaderProgram(m_grProg);
 	bindUniforms(cmdb, 0, 0, tokens.m_clusteredShadingUniformsToken);
@@ -146,7 +146,7 @@ void ClusterBinning::writeClustererBuffers(RenderingContext& ctx)
 	}
 
 	// Allocate buffers
-	ClustererGpuObjects& cs = ctx.m_clusterShading;
+	ClusteredShadingContext& cs = ctx.m_clusterShading;
 	StagingGpuMemoryManager& stagingMem = m_r->getStagingGpuMemoryManager();
 
 	cs.m_clusteredShadingUniformsAddress = stagingMem.allocateFrame(
@@ -233,7 +233,7 @@ void ClusterBinning::writeClustererBuffersTask()
 	ANKI_TRACE_SCOPED_EVENT(R_WRITE_CLUSTER_SHADING_OBJECTS);
 
 	RenderingContext& ctx = *m_runCtx.m_ctx;
-	ClustererGpuObjects& cs = ctx.m_clusterShading;
+	ClusteredShadingContext& cs = ctx.m_clusterShading;
 	const RenderQueue& rqueue = *ctx.m_renderQueue;
 
 	// Point lights

+ 2 - 2
AnKi/Renderer/Common.h

@@ -137,7 +137,7 @@ public:
 };
 
 /// GPU buffers and textures that the clusterer refers to.
-class ClustererGpuObjects
+class ClusteredShadingContext
 {
 public:
 	StagingGpuMemoryToken m_pointLightsToken;
@@ -183,7 +183,7 @@ public:
 
 	StagingGpuMemoryToken m_lightShadingUniformsToken;
 
-	ClustererGpuObjects m_clusterShading;
+	ClusteredShadingContext m_clusterShading;
 
 	RenderingContext(const StackAllocator<U8>& alloc)
 		: m_tempAllocator(alloc)

+ 1 - 1
AnKi/Renderer/ForwardShading.cpp

@@ -40,7 +40,7 @@ void ForwardShading::run(const RenderingContext& ctx, RenderPassWorkContext& rgr
 		cmdb->setDepthWrite(false);
 		cmdb->setBlendFactors(0, BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA);
 
-		const ClustererGpuObjects& rsrc = ctx.m_clusterShading;
+		const ClusteredShadingContext& rsrc = ctx.m_clusterShading;
 		cmdb->bindSampler(0, 0, m_r->getSamplers().m_trilinearClamp);
 
 		rgraphCtx.bindTexture(0, 1, m_r->getDepthDownscale().getHiZRt(), HIZ_HALF_DEPTH);

+ 1 - 1
AnKi/Renderer/LightShading.cpp

@@ -117,7 +117,7 @@ void LightShading::run(RenderPassWorkContext& rgraphCtx)
 		cmdb->setDepthWrite(false);
 
 		// Bind all
-		const ClustererGpuObjects& binning = ctx.m_clusterShading;
+		const ClusteredShadingContext& binning = ctx.m_clusterShading;
 		bindUniforms(cmdb, 0, 0, binning.m_clusteredShadingUniformsToken);
 
 		bindUniforms(cmdb, 0, 1, binning.m_pointLightsToken);

+ 20 - 21
AnKi/Renderer/RtShadows.cpp

@@ -477,36 +477,35 @@ void RtShadows::run(RenderPassWorkContext& rgraphCtx)
 {
 	const RenderingContext& ctx = *m_runCtx.m_ctx;
 	CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
-	const ClusterBinOut& rsrc = ctx.m_clusterBinOut;
+	const ClusteredShadingContext& rsrc = ctx.m_clusterShading;
 
 	cmdb->bindShaderProgram(m_rtLibraryGrProg);
 
-	bindUniforms(cmdb, 0, 0, ctx.m_lightShadingUniformsToken);
+	bindUniforms(cmdb, 0, 0, rsrc.m_clusteredShadingUniformsToken);
 
 	bindUniforms(cmdb, 0, 1, rsrc.m_pointLightsToken);
 	bindUniforms(cmdb, 0, 2, rsrc.m_spotLightsToken);
 	rgraphCtx.bindColorTexture(0, 3, m_r->getShadowMapping().getShadowmapRt());
 
 	bindStorage(cmdb, 0, 4, rsrc.m_clustersToken);
-	bindStorage(cmdb, 0, 5, rsrc.m_indicesToken);
-
-	cmdb->bindSampler(0, 6, m_r->getSamplers().m_trilinearRepeat);
-
-	rgraphCtx.bindImage(0, 7, m_runCtx.m_intermediateShadowsRts[0]);
-
-	rgraphCtx.bindColorTexture(0, 8, m_runCtx.m_historyRt);
-	cmdb->bindSampler(0, 9, m_r->getSamplers().m_trilinearClamp);
-	cmdb->bindSampler(0, 10, m_r->getSamplers().m_nearestNearestClamp);
-	rgraphCtx.bindTexture(0, 11, m_r->getDepthDownscale().getHiZRt(), HIZ_HALF_DEPTH);
-	rgraphCtx.bindColorTexture(0, 12, m_r->getMotionVectors().getMotionVectorsRt());
-	rgraphCtx.bindColorTexture(0, 13, m_r->getMotionVectors().getRejectionFactorRt());
-	rgraphCtx.bindColorTexture(0, 14, m_r->getGBuffer().getColorRt(2));
-	rgraphCtx.bindAccelerationStructure(0, 15, m_r->getAccelerationStructureBuilder().getAccelerationStructureHandle());
-	rgraphCtx.bindColorTexture(0, 16, m_runCtx.m_prevHistoryLengthRt);
-	rgraphCtx.bindImage(0, 17, m_runCtx.m_currentHistoryLengthRt);
-	rgraphCtx.bindColorTexture(0, 18, m_runCtx.m_prevMomentsRt);
-	rgraphCtx.bindImage(0, 19, m_runCtx.m_currentMomentsRt);
-	cmdb->bindTexture(0, 20, m_blueNoiseTex->getGrTextureView(), TextureUsageBit::SAMPLED_TRACE_RAYS);
+
+	cmdb->bindSampler(0, 5, m_r->getSamplers().m_trilinearRepeat);
+
+	rgraphCtx.bindImage(0, 6, m_runCtx.m_intermediateShadowsRts[0]);
+
+	rgraphCtx.bindColorTexture(0, 7, m_runCtx.m_historyRt);
+	cmdb->bindSampler(0, 8, m_r->getSamplers().m_trilinearClamp);
+	cmdb->bindSampler(0, 9, m_r->getSamplers().m_nearestNearestClamp);
+	rgraphCtx.bindTexture(0, 10, m_r->getDepthDownscale().getHiZRt(), HIZ_HALF_DEPTH);
+	rgraphCtx.bindColorTexture(0, 11, m_r->getMotionVectors().getMotionVectorsRt());
+	rgraphCtx.bindColorTexture(0, 12, m_r->getMotionVectors().getRejectionFactorRt());
+	rgraphCtx.bindColorTexture(0, 13, m_r->getGBuffer().getColorRt(2));
+	rgraphCtx.bindAccelerationStructure(0, 14, m_r->getAccelerationStructureBuilder().getAccelerationStructureHandle());
+	rgraphCtx.bindColorTexture(0, 15, m_runCtx.m_prevHistoryLengthRt);
+	rgraphCtx.bindImage(0, 16, m_runCtx.m_currentHistoryLengthRt);
+	rgraphCtx.bindColorTexture(0, 17, m_runCtx.m_prevMomentsRt);
+	rgraphCtx.bindImage(0, 18, m_runCtx.m_currentMomentsRt);
+	cmdb->bindTexture(0, 19, m_blueNoiseTex->getGrTextureView(), TextureUsageBit::SAMPLED_TRACE_RAYS);
 
 	cmdb->bindAllBindless(1);
 

+ 1 - 1
AnKi/Renderer/ShadowmapsResolve.cpp

@@ -70,7 +70,7 @@ void ShadowmapsResolve::run(RenderPassWorkContext& rgraphCtx)
 {
 	const RenderingContext& ctx = *m_runCtx.m_ctx;
 	CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
-	const ClustererGpuObjects& rsrc = ctx.m_clusterShading;
+	const ClusteredShadingContext& rsrc = ctx.m_clusterShading;
 
 	cmdb->bindShaderProgram(m_grProg);
 

+ 1 - 1
AnKi/Renderer/VolumetricLightingAccumulation.cpp

@@ -102,7 +102,7 @@ void VolumetricLightingAccumulation::run(RenderPassWorkContext& rgraphCtx)
 {
 	CommandBufferPtr& cmdb = rgraphCtx.m_commandBuffer;
 	RenderingContext& ctx = *m_runCtx.m_ctx;
-	const ClustererGpuObjects& rsrc = ctx.m_clusterShading;
+	const ClusteredShadingContext& rsrc = ctx.m_clusterShading;
 
 	cmdb->bindShaderProgram(m_grProg);
 

+ 1 - 1
AnKi/Shaders/RtShadowsHit.ankiprog

@@ -17,7 +17,7 @@ layout(shaderRecordEXT, scalar) buffer b_model
 	ModelGpuDescriptor u_modelDescriptor;
 };
 
-layout(set = 0, binding = 6) uniform sampler u_sampler;
+layout(set = 0, binding = 5) uniform sampler u_sampler;
 
 ANKI_BINDLESS_SET(1);
 #endif

+ 40 - 35
AnKi/Shaders/RtShadowsRayGen.ankiprog

@@ -13,27 +13,29 @@
 #include <AnKi/Shaders/Pack.glsl>
 #include <AnKi/Shaders/RtShadows.glsl>
 
-#define LIGHT_SET 0
-#define LIGHT_COMMON_UNIS_BINDING 0
-#define LIGHT_LIGHTS_BINDING 1
-#define LIGHT_CLUSTERS_BINDING 4
-#include <AnKi/Shaders/ClusteredShadingCommon.glsl>
-
-layout(set = 0, binding = 6) uniform sampler u_trilinearRepeatSampler; // Used by the hit shaders
-layout(set = 0, binding = 7) uniform uimage2D u_shadowsImage;
-layout(set = 0, binding = 8) uniform utexture2D u_historyShadowsTex;
-layout(set = 0, binding = 9) uniform sampler u_linearAnyClampSampler;
-layout(set = 0, binding = 10) uniform sampler u_nearestAnyClampSampler;
-layout(set = 0, binding = 11) uniform texture2D u_depthRt;
-layout(set = 0, binding = 12) uniform texture2D u_motionVectorsRt;
-layout(set = 0, binding = 13) uniform texture2D u_motionVectorsRejectionRt;
-layout(set = 0, binding = 14) uniform texture2D u_normalRt;
-layout(set = 0, binding = 15) uniform accelerationStructureEXT u_tlas;
-layout(set = 0, binding = 16) uniform texture2D u_prevHistoryLengthTex;
-layout(set = 0, binding = 17) uniform image2D u_historyLengthImage;
-layout(set = 0, binding = 18) uniform texture2D u_prevMomentsTex;
-layout(set = 0, binding = 19) uniform image2D u_momentsImage;
-layout(set = 0, binding = 20) uniform texture2D u_blueNoiseTex;
+#define CLUSTERED_SHADING_SET 0
+#define CLUSTERED_SHADING_UNIFORMS_BINDING 0
+#define CLUSTERED_SHADING_LIGHTS_BINDING 1
+#define CLUSTERED_SHADING_CLUSTERS_BINDING 4
+#include <AnKi/Shaders/ClusteredShadingCommon2.glsl>
+
+// Used by the hit shaders. When changing the binding you need to update other shaders
+layout(set = 0, binding = 5) uniform sampler u_trilinearRepeatSampler;
+
+layout(set = 0, binding = 6) uniform uimage2D u_shadowsImage;
+layout(set = 0, binding = 7) uniform utexture2D u_historyShadowsTex;
+layout(set = 0, binding = 8) uniform sampler u_linearAnyClampSampler;
+layout(set = 0, binding = 9) uniform sampler u_nearestAnyClampSampler;
+layout(set = 0, binding = 10) uniform texture2D u_depthRt;
+layout(set = 0, binding = 11) uniform texture2D u_motionVectorsRt;
+layout(set = 0, binding = 12) uniform texture2D u_motionVectorsRejectionRt;
+layout(set = 0, binding = 13) uniform texture2D u_normalRt;
+layout(set = 0, binding = 14) uniform accelerationStructureEXT u_tlas;
+layout(set = 0, binding = 15) uniform texture2D u_prevHistoryLengthTex;
+layout(set = 0, binding = 16) uniform image2D u_historyLengthImage;
+layout(set = 0, binding = 17) uniform texture2D u_prevMomentsTex;
+layout(set = 0, binding = 18) uniform image2D u_momentsImage;
+layout(set = 0, binding = 19) uniform texture2D u_blueNoiseTex;
 
 ANKI_BINDLESS_SET(1); // Used by the hit shaders
 
@@ -66,7 +68,7 @@ void main()
 	const Vec2 uv = (Vec2(gl_LaunchIDEXT.xy) + 0.5) / Vec2(gl_LaunchSizeEXT.xy);
 	const Vec2 ndc = UV_TO_NDC(uv);
 	const F32 depth = textureLod(u_depthRt, u_linearAnyClampSampler, uv, 0.0).r;
-	const Vec4 worldPos4 = u_invViewProjMat * Vec4(ndc, depth, 1.0);
+	const Vec4 worldPos4 = u_clusterShading.m_matrices.m_invertedViewProjectionJitter * Vec4(ndc, depth, 1.0);
 	const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
 
 	if(depth == 1.0)
@@ -85,9 +87,8 @@ void main()
 	const Vec3 normal = readNormalFromGBuffer(u_normalRt, u_linearAnyClampSampler, uv);
 
 	// Cluster
-	const U32 clusterIdx = computeClusterIndex(u_clustererMagic, uv, worldPos, u_lightingUniforms.m_clusterCount.x,
-											   u_lightingUniforms.m_clusterCount.y);
-	U32 idxOffset = u_clusters[clusterIdx];
+	Cluster cluster = getClusterUv(uv, depth, u_clusterShading.m_tileCounts, u_clusterShading.m_zSplitCount,
+								   u_clusterShading.m_zSplitMagic.x, u_clusterShading.m_zSplitMagic.y);
 
 	F32 shadowFactors[MAX_RT_SHADOW_LAYERS];
 	zeroRtShadowLayers(shadowFactors);
@@ -96,7 +97,7 @@ void main()
 	Vec3 random[RAYS_PER_PIXEL];
 	for(U32 i = 0; i < RAYS_PER_PIXEL; ++i)
 	{
-		const U32 frameIdx = u_lightingUniforms.m_frameCount * RAYS_PER_PIXEL + i;
+		const U32 frameIdx = u_clusterShading.m_frame * RAYS_PER_PIXEL + i;
 #if 0
 		const UVec3 irandom = rand3DPCG16(UVec3(gl_LaunchIDEXT.xy, frameIdx));
 		random[i] = Vec3(irandom) / F32(0xFFFF) * 2.0 - 1.0; // In [-1.0, 1.0]
@@ -109,26 +110,28 @@ void main()
 	}
 
 	// Dir light
-	ANKI_BRANCH if(u_dirLight.m_active != 0u && u_dirLight.m_cascadeCount > 0)
+	const DirectionalLight2 dirLight = u_clusterShading.m_directionalLight;
+	ANKI_BRANCH if(dirLight.m_active != 0u && dirLight.m_cascadeCount > 0)
 	{
 		for(U32 i = 0; i < RAYS_PER_PIXEL; ++i)
 		{
-			const Vec3 dirLightPos = worldPos + -u_dirLight.m_dir * 10.0 + random[i];
+			const Vec3 dirLightPos = worldPos + -dirLight.m_direction * 10.0 + random[i];
 			const Vec3 rayDir = normalize(dirLightPos - worldPos);
 
 			const F32 lambertTerm = dot(rayDir, normal);
 			ANKI_BRANCH if(lambertTerm > 0.0)
 			{
-				shadowFactors[u_dirLight.m_shadowLayer] += trace(worldPos, rayDir, 10000.0) / F32(RAYS_PER_PIXEL);
+				shadowFactors[dirLight.m_shadowLayer] += trace(worldPos, rayDir, 10000.0) / F32(RAYS_PER_PIXEL);
 			}
 		}
 	}
 
 	// Point lights
-	U32 idx;
-	ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
+	ANKI_LOOP while(cluster.m_pointLightsMask != 0u)
 	{
-		PointLight light = u_pointLights[idx];
+		const I32 idx = findLSB64(cluster.m_pointLightsMask);
+		cluster.m_pointLightsMask &= ~(1ul << U64(idx));
+		const PointLight2 light = u_pointLights2[idx];
 
 		ANKI_BRANCH if(light.m_shadowAtlasTileScale >= 0.0)
 		{
@@ -151,11 +154,13 @@ void main()
 	}
 
 	// Spot lights
-	ANKI_LOOP while((idx = u_lightIndices[idxOffset++]) != MAX_U32)
+	ANKI_LOOP while(cluster.m_spotLightsMask != 0u)
 	{
-		SpotLight light = u_spotLights[idx];
+		const I32 idx = findLSB64(cluster.m_spotLightsMask);
+		cluster.m_spotLightsMask &= ~(1ul << U64(idx));
+		const SpotLight2 light = u_spotLights2[idx];
 
-		ANKI_BRANCH if(light.m_shadowmapId >= 0.0)
+		ANKI_BRANCH if(light.m_shadowLayer != MAX_U32)
 		{
 			for(U32 i = 0; i < RAYS_PER_PIXEL; ++i)
 			{