DX11 - Deferred Shadows - Not working

Started by
4 comments, last by Migi0027 10 years, 3 months ago

Hi guys wink.png .

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 smile.png

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. happy.png

-MIGI0027

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

Advertisement

People seem quiet, does everything look fine here?

If so, are you interested in the cpu side?

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

I'm not seeing anything obviously wrong. A couple suggestions though. Post a screenshot of the issue, run the DX debug device to make sure there are no DX errors or warnings, and change your shader to output these things to help debug: lightDepthValue, depthValue, depthValue - lightDepthValue. Also try rendering debug colors for shadow/non-shadow:


if(lightDepthValue < depthValue)
	return float4(1, 0, 0, 1);
return float4(0, 1, 0, 1);
Hopefully one of those will reveal where the issue lies.

Not really related to the issue, but you can render your depth map directly to a depth stencil target (no need to use render target). This way you can take advantage of the fast z-buffer fills and you don't need to use a pixel shader at all (unless you'll need alpha transparency).

In the shadowing code, most of the IFs are not necessary and won't add anything to the result. You can use clamping as shadow texture addressing mode.

I think also that your way of performing alpha testing is overly complicated.

the line "if (ShadowMap.Sample(ss, projectTexCoord).a == 0)color.a = 0.0f;" is unnecessary if you change the shadow pixel shader:

if (_alphamap == 1)
{
if(t_alphamap.Sample(ss, input.texcoord).a == 0.0f)discard;
}

Cheers!

Thanks for the input!

Yeah, it does seem that I've over complicated things, thanks for the points, I'll change them after I've fixed this issue smile.png .

Telanor: Actually I tried most of what you mentioned, and the behavior was: it passed the if((saturate(projectTexCoord.x) == projectTexCoord.x) && (saturate(projectTexCoord.y) == projectTexCoord.y)) but not the if(lightDepthValue < depthValue). I'm going to try the other points.

-MIGI0027

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

Finally, it's working smile.png

In the older version I used to have a global view + projection matrix (for shadows), but as that changed, I needed to replace some variables with others, so I replaced almost all of the occurrences but forgot one, therefore it broke, and replacing the last one did the magic.

Thanks guys!

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

This topic is closed to new replies.

Advertisement