Deferred shading - problems with point lights

Started by
1 comment, last by MJP 11 years, 5 months ago
Hi everyone. I am working on deferred shading because I want to add this to my own engine. Currently geometry phase and directional lights from lighting phase are working fine on both versions of directx 9 and 11.
I stucked on points lights. Here is a shader's code for point light:

[source lang="cpp"]
cbuffer VertexShaderParameters
{
matrix ModelViewProjection;
};

cbuffer PixelShaderParameters
{
float3 LightPosition;
float4 LightColor;
float LightIntensity;
float LightRange;
matrix ViewProjectionInverse;
};

Texture2D DepthMap;
Texture2D NormalMap;
SamplerState PointClampSampler;

struct VertexShaderInput
{
float3 Position : Position;
};

struct PixelShaderInput
{
float4 Position : SV_Position;
};

PixelShaderInput VertexShaderMain(VertexShaderInput input)
{
PixelShaderInput output;
output.Position = mul(float4(input.Position, 1.0f), ModelViewProjection);
return output;
}

float4 PixelShaderMain(PixelShaderInput input) : SV_Target
{
float3 posXYZ = input.Position.xyz / input.Position.w;
float2 uv = float2(posXYZ.x, -posXYZ.y) * 0.5f + 0.5f;

float depth = DepthMap.Sample(PointClampSampler, uv);
float3 normal = NormalMap.Sample(PointClampSampler, uv) * 2.0f - 1.0f;

float4 posXYZW = float4(posXYZ.xy, depth, 1.0f);
float4 worldPosition = mul(posXYZW, ViewProjectionInverse);
worldPosition /= worldPosition.w;

float3 lightVector = LightPosition - worldPosition.xyz;
float3 lightDirection = normalize(lightVector);
float dotProduct = dot(lightDirection, normalize(normal));

float lightDistance = length(lightVector);
float falloff = saturate(1.0f - lightDistance / LightRange);
float i = saturate(dotProduct) * LightIntensity * falloff * falloff;

return LightColor * i;
}
[/source]

Implementation that is working in DirectX 9 isn't working on DirectX 11.

Please for help to resolve this problem.
Advertisement
Try changing line 50 to:

float dotProduct = abs(dot(lightDirection, normalize(normal)));
I would suggest outputting your position to make sure that you're reconstructing the correct value. You can also try outputting your normals to verify that they're correct. You can also debug your shader in PIX or the VS 2012 graphics debugger, so that you can step through and see where you're going wrong. And if you haven't enabled debug output when creating your device, then you should do so to ensure that you catch any errors.

This topic is closed to new replies.

Advertisement