# DX11 DX11 - Deferred Shadows - Not working

## Recommended Posts

Migi0027    4630

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

// 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;

float4 lightViewPosition;
lightViewPosition = position;
lightViewPosition = mul(lightViewPosition, lightViewMatrix);
lightViewPosition = mul(lightViewPosition, lightProjectionMatrix);

float4 color = float4(0,0,0,1);

{
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(
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))
{
color.a = 0.0f;

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.

-MIGI0027

Edited by Migi0027

##### Share on other sites
Migi0027    4630

People seem quiet, does everything look fine here?

If so, are you interested in the cpu side?

##### Share on other sites
Telanor    1486
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.

##### Share on other sites
kauna    2922

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)
{
}

Cheers!

##### Share on other sites
Migi0027    4630

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  .

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

##### Share on other sites
Migi0027    4630

Finally, it's working

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!

Edited by Migi0027

## Create an account

Register a new account

• ## Partner Spotlight

• ### Similar Content

• By RubenRS
How do i open an image to use it as Texture2D information without D3DX11CreateShaderResourceViewFromFile? And how it works for different formats like (JPG, PNG, BMP, DDS,  etc.)?
I have an (512 x 512) image with font letters, also i have the position and texcoord of every letter. The main idea is that i want to obtain the image pixel info, use the position and texcoords to create a new texture with one letter and render it. Or am I wrong in something?
• By thmfrnk
Hey,
I found a very interesting blog post here: https://bartwronski.com/2017/04/13/cull-that-cone/
However, I didn't really got how to use his "TestConeVsSphere" test in 3D (last piece of code on his post). I have the frustumCorners of a 2D Tile cell in ViewSpace and my 3D Cone Origin and Direction, so where to place the "testSphere"? I thought about to also move the Cone into viewspace and put the sphere to the Center of the Cell with the radius of half-cellsize, however what about depth? A sphere does not have inf depth?
I am missing anything? Any Ideas?
Thx, Thomas
• By Modymek
hi all
I want to enable and disable shader in MPCH Media player Classic
I want the shader to read each file extension before it plays the file
so if the video file name is video.GR.Mp4 it will play it in Grayscale shader
if it is not and standard file name Video.Mp4 without GR. unique extension so it plays standard without shader or end the shader
here is the shader I have for grayscale
sampler s0 : register(s0);
float4 main(float2 tex : TEXCOORD0) : COLOR {
float c0 = dot(tex2D(s0, tex), float4(0.299, 0.587, 0.114, 0));
return c0;
}

I want to add if or block stantement or bloean to detect file name before it call the shader in order to go to the procedure or disable it or goto end direct without it

any thoughts or help

• I've gotten to part in my DirectX 11 project where I need to pass the MVP matrices to my vertex shader. And I'm a little lost when it comes to the use of the constant buffer with the vertex shader
I understand I need to set up the constant buffer just like any other buffer:
1. Create a buffer description with the D3D11_BIND_CONSTANT_BUFFER flag 2. Map my matrix data into the constant buffer 3. Use VSSetConstantBuffers to actually use the buffer But I get lost at the VertexShader part, how does my vertex shader know to use this constant buffer when we get to the shader side of things
In the example I'm following I see they have this as their vertex shader, but I don't understand how the shader knows to use the MatrixBuffer cbuffer. They just use the members directly. What if there was multiple cbuffer declarations like the Microsoft documentation says you could have?
//Inside vertex shader cbuffer MatrixBuffer { matrix worldMatrix; matrix viewMatrix; matrix projectionMatrix; }; struct VertexInputType { float4 position : POSITION; float4 color : COLOR; }; struct PixelInputType { float4 position : SV_POSITION; float4 color : COLOR; }; PixelInputType ColorVertexShader(VertexInputType input) { PixelInputType output; // Change the position vector to be 4 units for proper matrix calculations. input.position.w = 1.0f; // Calculate the position of the vertex against the world, view, and projection matrices. output.position = mul(input.position, worldMatrix); output.position = mul(output.position, viewMatrix); output.position = mul(output.position, projectionMatrix); // Store the input color for the pixel shader to use. output.color = input.color; return output; }
• By gomidas
I am trying to add normal map to my project I have an example of a cube:
I have normal in my shader I think. Then I set shader resource view for texture (NOT BUMP)
device.ImmediateContext.PixelShader.SetShaderResource(0, textureView);             device.ImmediateContext.Draw(VerticesCount,0); What should I do to set my normal map or how it is done in dx11 generally example c++?

• 9
• 11
• 9
• 14
• 13