Răsfoiți Sursa

Put some more work on the shaders

Panagiotis Christopoulos Charitos 7 ani în urmă
părinte
comite
8a56301c5b

+ 1 - 0
shaders/ClusteredShadingCommon.glsl

@@ -39,6 +39,7 @@ layout(ANKI_UBO_BINDING(LIGHT_SET, LIGHT_UBO_BINDING), std140, row_major) unifor
 #	define u_invViewProjMat u_lightingUniforms.m_invViewProjMat
 #	define u_prevViewProjMat u_lightingUniforms.m_prevViewProjMat
 #	define u_prevViewProjMatMulInvViewProjMat u_lightingUniforms.m_prevViewProjMatMulInvViewProjMat
+#	define u_dirLight u_lightingUniforms.m_dirLight
 
 #else
 const U32 _NEXT_UBO_BINDING = LIGHT_UBO_BINDING;

+ 23 - 0
shaders/LightShading.glslp

@@ -145,6 +145,29 @@ void main()
 	// Ambient and emissive color
 	out_color = gbuffer.m_diffuse * gbuffer.m_emission;
 
+	// Dir light
+	if(u_dirLight.m_cascadeCount > 0u && false)
+	{
+		F32 linearDepth = linearizeDepth(depth, u_near, u_far);
+
+		F32 cascadeCountf = F32(u_dirLight.m_cascadeCount);
+
+		U32 cascadeIdx = U32(linearDepth * cascadeCountf);
+		cascadeIdx = min(cascadeIdx, u_dirLight.m_cascadeCount);
+
+		Mat4 lightProjectionMat = u_dirLight.m_textureMatrices[cascadeIdx];
+
+		Vec4 texCoords4 = lightProjectionMat * Vec4(worldPos, 1.0);
+		Vec3 texCoords3 = texCoords4.xyz / texCoords4.w;
+
+		F32 cascadeLinearDepth = texCoords3.z;
+
+		F32 shadowFactor = textureLod(u_shadowTex, texCoords3.xy, 0.0).r;
+		shadowFactor = saturate(exp(ESM_CONSTANT * (shadowFactor - cascadeLinearDepth)));
+
+		out_color += diffuseLambert(gbuffer.m_diffuse) * u_dirLight.m_diffuseColor * shadowFactor;
+	}
+
 	// Point lights
 	Vec3 viewDir = normalize(u_cameraPos - worldPos);
 	U32 idx;

+ 2 - 1
shaders/glsl_cpp_common/ClusteredShading.h

@@ -49,7 +49,7 @@ ANKI_SHADER_STATIC_ASSERT(sizeof(SpotLight) == SIZEOF_SPOT_LIGHT)
 struct DirectionalLight
 {
 	Vec3 m_diffuseColor;
-	U32 m_present; // If it's been used or not
+	U32 m_cascadeCount; // If it's zero then it's not active
 	Vec3 m_dir;
 	U32 m_padding;
 	Mat4 m_textureMatrices[MAX_SHADOW_CASCADES];
@@ -107,6 +107,7 @@ struct LightingUniforms
 	Mat4 m_invViewProjMat;
 	Mat4 m_prevViewProjMat;
 	Mat4 m_prevViewProjMatMulInvViewProjMat; // Used to re-project previous frames
+	DirectionalLight m_dirLight;
 };
 
 ANKI_SHADER_FUNC_INLINE F32 computeClusterKf(ClustererMagicValues magic, Vec3 worldPos)

+ 4 - 1
shaders/glsl_cpp_common/Common.h

@@ -6,7 +6,7 @@
 #pragma once
 
 //
-// Macros & functions
+// Macros & functions C++
 //
 #if defined(__cplusplus)
 #	define ANKI_BEGIN_NAMESPACE \
@@ -29,6 +29,9 @@ inline F32 dot(const T& a, const T& b)
 }
 ANKI_END_NAMESPACE
 
+//
+// Macros & functions GLSL
+//
 #else
 #	define x() x
 #	define y() y

+ 3 - 1
src/anki/core/Config.cpp

@@ -5,6 +5,8 @@
 
 #include <anki/core/Config.h>
 #include <anki/util/System.h>
+#include <anki/Math.h>
+#include <shaders/glsl_cpp_common/ClusteredShading.h>
 
 namespace anki
 {
@@ -27,7 +29,7 @@ Config::Config()
 	newOption("r.shadowMapping.enabled", true);
 	newOption("r.shadowMapping.tileResolution", 128);
 	newOption("r.shadowMapping.tileCountPerRowOrColumn", 16);
-	newOption("r.shadowMapping.scratchTileCountX", 4 * 5);
+	newOption("r.shadowMapping.scratchTileCountX", 4 * (MAX_SHADOW_CASCADES + 2));
 	newOption("r.shadowMapping.scratchTileCountY", 4);
 	newOption("r.shadowMapping.lightLodDistance0", 10.0);
 	newOption("r.shadowMapping.lightLodDistance1", 20.0);

+ 20 - 0
src/anki/renderer/Renderer.cpp

@@ -564,6 +564,26 @@ void Renderer::updateLightShadingUniforms(RenderingContext& ctx) const
 
 	blk->m_prevViewProjMatMulInvViewProjMat =
 		ctx.m_prevMatrices.m_viewProjection * ctx.m_matrices.m_viewProjectionJitter.getInverse();
+
+	// Directional light
+	if(ctx.m_renderQueue->m_directionalLight.m_shadowCascadeCount > 0)
+	{
+		DirectionalLight& out = blk->m_dirLight;
+		const DirectionalLightQueueElement& in = ctx.m_renderQueue->m_directionalLight;
+
+		out.m_diffuseColor = in.m_diffuseColor;
+		out.m_cascadeCount = in.m_shadowCascadeCount;
+		out.m_dir = in.m_direction;
+
+		for(U cascade = 0; cascade < in.m_shadowCascadeCount; ++cascade)
+		{
+			out.m_textureMatrices[cascade] = in.m_textureMatrices[cascade];
+		}
+	}
+	else
+	{
+		blk->m_dirLight.m_cascadeCount = 0;
+	}
 }
 
 } // end namespace anki

+ 3 - 2
src/anki/renderer/TileAllocator.cpp

@@ -253,8 +253,6 @@ TileAllocatorResult TileAllocator::allocate(Timestamp crntTimestamp,
 		if(it != m_lightInfoToTileIdx.getEnd())
 		{
 			Tile& tile = m_allTiles[*it];
-			ANKI_ASSERT(tile.m_lastUsedTimestamp != crntTimestamp
-						&& "Trying to allocate the same thing twice in this timestamp?");
 
 			if(tile.m_lightUuid != lightUuid || tile.m_lightLod != lod || tile.m_lightFace != lightFace)
 			{
@@ -265,6 +263,9 @@ TileAllocatorResult TileAllocator::allocate(Timestamp crntTimestamp,
 			{
 				// Same light & lod & face, found the cache entry.
 
+				ANKI_ASSERT(tile.m_lastUsedTimestamp != crntTimestamp
+							&& "Trying to allocate the same thing twice in this timestamp?");
+
 				ANKI_ASSERT(tile.m_lightUuid == lightUuid && tile.m_lightLod == lod && tile.m_lightFace == lightFace);
 
 				tileViewport = {tile.m_viewport[0], tile.m_viewport[1], tile.m_viewport[2], tile.m_viewport[3]};