From 632f0a5b061428e59c2ac5488bbf76801de2d540 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 2 Oct 2020 11:57:13 -0700 Subject: [PATCH] break shadow computes out into include file --- .../DeferredPBR_DirectionalLightEffect.fxb | 4 +- .../DeferredPBR_DirectionalLightEffect.fx | 129 +++++------------- Effects/HLSL/Shadow.fxh | 87 ++++++++++++ 3 files changed, 122 insertions(+), 98 deletions(-) create mode 100644 Effects/HLSL/Shadow.fxh diff --git a/Effects/FXB/DeferredPBR_DirectionalLightEffect.fxb b/Effects/FXB/DeferredPBR_DirectionalLightEffect.fxb index 9f6cf27..1ec8b6e 100644 --- a/Effects/FXB/DeferredPBR_DirectionalLightEffect.fxb +++ b/Effects/FXB/DeferredPBR_DirectionalLightEffect.fxb @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:be44f16057328acf2bf9d2e4ff1e2d448df720711a2900daa39f5fc8c8732711 -size 21692 +oid sha256:3eb5c7dfc3e166c9baf57be77405a644fdf390aa8deb4bb7d39e044b7b3d338b +size 21776 diff --git a/Effects/HLSL/DeferredPBR_DirectionalLightEffect.fx b/Effects/HLSL/DeferredPBR_DirectionalLightEffect.fx index 0982521..de683cd 100644 --- a/Effects/HLSL/DeferredPBR_DirectionalLightEffect.fx +++ b/Effects/HLSL/DeferredPBR_DirectionalLightEffect.fx @@ -1,5 +1,6 @@ #include "Macros.fxh" //from FNA #include "Lighting.fxh" +#include "Shadow.fxh" static const int NUM_SHADOW_CASCADES = 4; @@ -35,26 +36,6 @@ MATRIX_CONSTANTS END_CONSTANTS -static float2 poissonDisk[16] = -{ - float2( -0.94201624, -0.39906216 ), - float2( 0.94558609, -0.76890725 ), - float2( -0.094184101, -0.92938870 ), - float2( 0.34495938, 0.29387760 ), - float2( -0.91588581, 0.45771432 ), - float2( -0.81544232, -0.87912464 ), - float2( -0.38277543, 0.27676845 ), - float2( 0.97484398, 0.75648379 ), - float2( 0.44323325, -0.97511554 ), - float2( 0.53742981, -0.47373420 ), - float2( -0.26496911, -0.41893023 ), - float2( 0.79197514, 0.19090188 ), - float2( -0.24188840, 0.99706507 ), - float2( -0.81409955, 0.91437590 ), - float2( 0.19984126, 0.78641367 ), - float2( 0.14383161, -0.14100790 ) -}; - struct VertexInput { float4 Position : POSITION; @@ -79,35 +60,8 @@ PixelInput main_vs(VertexInput input) // Pixel Shader -// Returns a random number based on a vec3 and an int. -float random(float3 seed, int i){ - float4 seed4 = float4(seed, i); - float dot_product = dot(seed4, float4(12.9898,78.233,45.164,94.673)); - return frac(sin(dot_product) * 43758.5453); -} - -float PoissonCoord(sampler shadowMap, float3 worldPosition, float2 texCoord, float fragmentDepth, float bias) -{ - float visibility = 1.0; - - for (int i = 0; i < 16; i++) - { - int index = int(16.0 * random(floor(worldPosition * 1000.0), i)) % 16; - - if (tex2D(shadowMap, texCoord + poissonDisk[index] / 1024.0).r < fragmentDepth - bias) - { - visibility -= 0.05; - } - } - - return visibility; -} - float ComputeShadow(float3 positionWorldSpace, float3 N, float3 L) { - float bias = 0.005 * tan(acos(dot(N, L))); - bias = clamp(bias, 0, 0.01); - float4 positionCameraSpace = mul(float4(positionWorldSpace, 1.0), ViewMatrix); int shadowCascadeIndex = 0; // 0 is closest @@ -139,69 +93,52 @@ float ComputeShadow(float3 positionWorldSpace, float3 N, float3 L) lightSpaceMatrix = LightSpaceMatrixFour; } - float4 positionLightSpace = mul(float4(positionWorldSpace, 1.0), lightSpaceMatrix); - - // maps to [-1, 1] - float3 projectionCoords = positionLightSpace.xyz / positionLightSpace.w; - - // maps to [0, 1] - projectionCoords.x = (projectionCoords.x * 0.5) + 0.5; - projectionCoords.y = (projectionCoords.y * 0.5) + 0.5; - projectionCoords.y *= -1; - // in XNA clip z is 0 to 1 already - - if (projectionCoords.z > 1.0) - { - return 1.0; - } - - float inc = 1.0 / ShadowMapSize; // TODO: shadow map size uniform - // PCF + Poisson soft shadows - float visibility = 0.0; - // for (int row = -1; row <= 1; row++) - // { - // for (int col = -1; col <= 1; col++) - // { - // if (shadowCascadeIndex == 0) - // { - // visibility += PoissonCoord(SAMPLER(shadowMapOne), positionWorldSpace, projectionCoords.xy + float2(row, col) * inc, projectionCoords.z, bias); - // } - // else if (shadowCascadeIndex == 1) - // { - // visibility += PoissonCoord(SAMPLER(shadowMapTwo), positionWorldSpace, projectionCoords.xy + float2(row, col) * inc, projectionCoords.z, bias); - // } - // else if (shadowCascadeIndex == 2) - // { - // visibility += PoissonCoord(SAMPLER(shadowMapThree), positionWorldSpace, projectionCoords.xy + float2(row, col) * inc, projectionCoords.z, bias); - // } - // else - // { - // visibility += PoissonCoord(SAMPLER(shadowMapFour), positionWorldSpace, projectionCoords.xy + float2(row, col) * inc, projectionCoords.z, bias); - // } - // } - // } - - // visibility /= 9.0; if (shadowCascadeIndex == 0) { - visibility = PoissonCoord(SAMPLER(shadowMapOne), positionWorldSpace, projectionCoords.xy, projectionCoords.z, bias); + return PoissonShadow( + positionWorldSpace, + N, + L, + lightSpaceMatrix, + SAMPLER(shadowMapOne), + ShadowMapSize + ); } else if (shadowCascadeIndex == 1) { - visibility = PoissonCoord(SAMPLER(shadowMapTwo), positionWorldSpace, projectionCoords.xy, projectionCoords.z, bias); + return PoissonShadow( + positionWorldSpace, + N, + L, + lightSpaceMatrix, + SAMPLER(shadowMapTwo), + ShadowMapSize + ); } else if (shadowCascadeIndex == 2) { - visibility = PoissonCoord(SAMPLER(shadowMapThree), positionWorldSpace, projectionCoords.xy, projectionCoords.z, bias); + return PoissonShadow( + positionWorldSpace, + N, + L, + lightSpaceMatrix, + SAMPLER(shadowMapThree), + ShadowMapSize + ); } else { - visibility = PoissonCoord(SAMPLER(shadowMapFour), positionWorldSpace, projectionCoords.xy, projectionCoords.z, bias); + return PoissonShadow( + positionWorldSpace, + N, + L, + lightSpaceMatrix, + SAMPLER(shadowMapFour), + ShadowMapSize + ); } - - return visibility; } float4 ComputeColor( diff --git a/Effects/HLSL/Shadow.fxh b/Effects/HLSL/Shadow.fxh new file mode 100644 index 0000000..38efc3e --- /dev/null +++ b/Effects/HLSL/Shadow.fxh @@ -0,0 +1,87 @@ +static float2 poissonDisk[16] = +{ + float2( -0.94201624, -0.39906216 ), + float2( 0.94558609, -0.76890725 ), + float2( -0.094184101, -0.92938870 ), + float2( 0.34495938, 0.29387760 ), + float2( -0.91588581, 0.45771432 ), + float2( -0.81544232, -0.87912464 ), + float2( -0.38277543, 0.27676845 ), + float2( 0.97484398, 0.75648379 ), + float2( 0.44323325, -0.97511554 ), + float2( 0.53742981, -0.47373420 ), + float2( -0.26496911, -0.41893023 ), + float2( 0.79197514, 0.19090188 ), + float2( -0.24188840, 0.99706507 ), + float2( -0.81409955, 0.91437590 ), + float2( 0.19984126, 0.78641367 ), + float2( 0.14383161, -0.14100790 ) +}; + +// TODO: this should probably sample a noise texture instead +// Returns a random number based on a vec3 and an int. +float random(float3 seed, int i){ + float4 seed4 = float4(seed, i); + float dot_product = dot(seed4, float4(12.9898,78.233,45.164,94.673)); + return frac(sin(dot_product) * 43758.5453); +} + +float PoissonCoord(sampler shadowMap, float3 worldPosition, float2 texCoord, float fragmentDepth, float bias) +{ + float visibility = 1.0; + + for (int i = 0; i < 16; i++) + { + int index = int(16.0 * random(floor(worldPosition * 1000.0), i)) % 16; + + if (tex2D(shadowMap, texCoord + poissonDisk[index] / 1024.0).r < fragmentDepth - bias) + { + visibility -= 0.05; + } + } + + return visibility; +} + +float PoissonShadow( + float3 positionWorldSpace, + float3 N, + float3 L, + float4x4 lightSpaceMatrix, + sampler shadowMap, + int shadowMapSize +) { + float bias = 0.005 * tan(acos(dot(N, L))); + bias = clamp(bias, 0, 0.01); + + float4 positionLightSpace = mul(float4(positionWorldSpace, 1.0), lightSpaceMatrix); + + // maps to [-1, 1] + float3 projectionCoords = positionLightSpace.xyz / positionLightSpace.w; + + // maps to [0, 1] + // NOTE: In XNA, clip space z is [0, 1] range + projectionCoords.x = (projectionCoords.x * 0.5) + 0.5; + projectionCoords.y = (projectionCoords.y * 0.5) + 0.5; + projectionCoords.y *= -1; + + if (projectionCoords.z > 1.0) + { + return 1.0; + } + + float inc = 1.0 / shadowMapSize; + + // Poisson soft shadows + float visibility = 0.0; + + visibility = PoissonCoord( + shadowMap, + positionWorldSpace, + projectionCoords.xy, + projectionCoords.z, + bias + ); + + return visibility; +}