break shadow computes out into include file
parent
2fb20747e4
commit
632f0a5b06
BIN
Effects/FXB/DeferredPBR_DirectionalLightEffect.fxb (Stored with Git LFS)
BIN
Effects/FXB/DeferredPBR_DirectionalLightEffect.fxb (Stored with Git LFS)
Binary file not shown.
|
@ -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(
|
||||
|
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue