Hello Forum, hoping this is in the correct place.
I have implemented a deferred renderer with DirectX 11 using HLSL shaders. The process is as follows:
- The first pass renders all information to separate textures like albedo, normals, worldPosition etc..
- The second pass does all the lighting calculations.
I know that I could be working out the position using the depth, however that doesn't matter at the moment as the deferred lighting works.
The problem that is now arising is I want to include shadows into the procedure, most online tutorials assume forward rendering but I would believe the process is the same. At the moment I have simply added in the view and projection matrix values into the light buffer, these are bound within both passes as I'm trying to get the depth value of the light within the first pass and then used the shadow map on the second. Note the shaders do allow for multiple lights but I'm focusing on using one light to get the shadows working and then I can easily add the rest in.
This is what I have at the moment:
First pass
struct PS_OUTPUT
{
//Other targets here
/*ShadowMaps*/
float4 light1ViewPos : SV_Target4;
float4 light1ShadowMap : SV_Target5;
/*ShadowMaps*/
};
VS_OUTPUT VS_Main(float3 inPos : POSITION, float2 inTexCoord : TEXCOORD, float3 normal : NORMAL)
{
VS_OUTPUT output;
//Camera world view projection transformations here..
/*Light Map*/
output.lightPos = mul(float4(inPos, 1.0f), worldMatrix);
output.lightPos = mul(output.lightPos, lights[0]._viewMatrix);
output.lightPos = mul(output.lightPos, lights[0]._projMatrix);
/*Light Map*/
return output;
}
PS_OUTPUT PS_Main(VS_OUTPUT input) : SV_TARGET
{
PS_OUTPUT output;
//other renderTarget values here..
float depth = input.lightPos.z / input.lightPos.w;
output.light1ViewPos = input.lightPos;
output.light1ShadowMap = float4(depth, depth, depth, 1.0f);
return output;
}
Second pass in the pixel shader:
pixel.lightViewPosition = Light1ViewPosMapTexture.Sample(ObjSamplerState, pixel.texCoord);
pixel.lightViewPosition.xyz /= pixel.lightViewPosition.w;
if (pixel.lightViewPosition.x < -1.0f || pixel.lightViewPosition.x > 1.0f ||
pixel.lightViewPosition.y < -1.0f || pixel.lightViewPosition.y > 1.0f ||
pixel.lightViewPosition.z < 0.0f || pixel.lightViewPosition.z > 1.0f)
return output;
//transform clip space coords to texture space coords (-1:1 to 0:1)
pixel.lightViewPosition.x = pixel.lightViewPosition.x / 2 + 0.5;
pixel.lightViewPosition.y = pixel.lightViewPosition.y / -2 + 0.5;
float shadowMapBias = 0.2f;
pixel.lightViewPosition.z -= shadowMapBias;
float shadowMapDepth = Light1ViewPosMapTexture.Sample(ObjSamplerState, pixel.lightViewPosition.xy).r;
worldPosition = WorldPositionTexture.Sample(ObjSamplerState, pixel.texCoord);
if (shadowMapDepth < pixel.lightViewPosition.z) return output;
output.color = float4(1.0f, 1.0f, 1.0f, 1.0f);
return output;
//Actual lighting calculations after here
The result I'm actually getting is quite odd... it seems to render shadows but they seem to move when the camera moves, meaning the are always in incorrect positions. I have been looking the cause of this for a couple of days with no hope, if any of you could point me to the right direction I would be very appreciated. On a side note the fustrum for the light is working as if I remove the if (shadowMapDepth < pixel.lightViewPosition.z) return output; Then I get a nice square of white light so its deffo something to do with the shadow map.
One question would be if I render the shadow map should it be white or go light to dark with depth because different tutorials show different things for example:
http://www.rastertek.com/dx11tut40.html
http://takinginitiative.wordpress.com/2011/05/15/directx10-tutorial-10-shadow-mapping/
Thanks for any help.