Hi guys .
So for a long time I've been doing shadows per object pixel, not deferred, and, I had to switch to the deferred way. Which I am now attempting, direct lighting works fine for e.g. Directional lights, but, for the shadows, it's another story. They simply don't work, the reason is unclear for now, so, I've come here in order to ask for your help
I'm almost (Hence the almost) certain that the cpus work isn't the fault here, but if the gpus work seems fine, I'll post the cpus work.
So, the code!
For generating the shadow map I'm almost certain that it's fine as the same shader was used for the previous method and nothing has changed, but, I'll post it: ( I do realize that loads of optimizations may be done here, like outputting the depth into a one channel render target )
cbuffer ConstantObjectBuffer : register (b0)
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
float state;
float _instance;
float _alphamap;
};
struct VOut
{
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD;
float4 depthPosition : TEXTURE0;
};
Texture2D t_alphamap : register(t0);
SamplerState ss;
VOut VShader(float4 position : POSITION, float2 texcoord : TEXCOORD, float3 instancePosition : INSTANCEPOS)
{
VOut output;
if (_instance == 1)
{
position.x += instancePosition.x;
position.y += instancePosition.y;
position.z += instancePosition.z;
}
position.w = 1.0f;
output.texcoord = texcoord;
output.position = mul(position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);
// Store the position value in a second input value for depth value calculations.
output.depthPosition = output.position;
return output;
}
float4 PShader(VOut input) : SV_TARGET
{
float4 color = float4(1,1,1,1);
float depth = (input.depthPosition.z / input.depthPosition.w);
color = float4(depth, depth, depth, 1);
if (_alphamap == 1)
{
color.a *= t_alphamap.Sample(ss, input.texcoord).a;
}
return color;
}
PS. The resource filled by this shader looks fine, the shadow map I mean.
The directional deferred shader, rendered onto a full screen quad: (PS)
// Get Normal Data
float4 normal = t_normals.Sample(ss, input.Tex); // Stored in view space
normal = mul(float4(normal.xyz, 0), texture_transform); // texture_transform == view^(-1)
// Get Position
float4 position = t_position.Sample(ss, input.Tex); // Stored in view space
position = mul(float4(position.xyz, 1), texture_transform);
normal.a = 1.0f;
position.w = 1.0f;
// Get Shadow Vertex Position
float4 lightViewPosition;
lightViewPosition = position;
lightViewPosition = mul(lightViewPosition, lightViewMatrix);
lightViewPosition = mul(lightViewPosition, lightProjectionMatrix);
float4 color = float4(0,0,0,1);
if (shadows == 1)
{
color.rgb += GetShadowOcclusion(
t_depthmap, // The shadow map
normal,
cameraPosition,
position,
lPosition-position,
lDirection,
lColor,
lightViewPosition,
1.0f // Specular Intensity
).rgb;
}
else
{
float diffusebrightness = saturate(dot(normal, lDirection));
color.rgb += lColor * diffusebrightness;
}
return color;
Helpers:
float4 GetShadowOcclusion(
Texture2D ShadowMap,
float3 Normal,
float3 EyePos,
float3 WorldPosition,
float3 LightSurfaceDir,
float3 LightDirection,
float3 LightColor,
float4 LightViewPosition,
float SpecularIntensity
)
{
float bias;
float4 color = float4(0,0,0,1);
float2 projectTexCoord;
float depthValue;
float lightDepthValue;
float lightIntensity;
bias = 0.0001;
float occlusion = 0;
projectTexCoord.x = LightViewPosition.x / LightViewPosition.w / 2.0f + 0.5f;
projectTexCoord.y = -LightViewPosition.y / LightViewPosition.w / 2.0f + 0.5f;
if((saturate(projectTexCoord.x) == projectTexCoord.x) && (saturate(projectTexCoord.y) == projectTexCoord.y))
{
if (ShadowMap.Sample(ss, projectTexCoord).a == 0)
color.a = 0.0f;
depthValue = ShadowMap.Sample(ss, projectTexCoord).r;
lightDepthValue = LightViewPosition.z / LightViewPosition.w;
lightDepthValue = lightDepthValue - bias;
lightIntensity = saturate(dot(Normal, LightSurfaceDir));
if(lightDepthValue < depthValue)
{
//float diffusebrightness = saturate(dot(normal, lDirection));
//color.rgb += lColor * diffusebrightness;
color.rgb += LightColor*lightIntensity;
occlusion = lightIntensity;
}
}
// Secular lighting occurs here, but it's irrelevant
}
I hope this will be enough to supply you with to approach this issue.
EDIT: I did not post any kind of result, so, where the shadows are not present are not being lit for some reason, it seems that the if(lightDepthValue < depthValue) is never fulfilled.
Thank you for your time.
-MIGI0027