Jump to content
  • Advertisement
Sign in to follow this  
hupsilardee

HLSL shadow map

This topic is 2639 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

EDIT: It now works, yay, but there are some bugs - I might make a Youtube video if I can't solve these in the next hour or so
Can't work out what's wrong with my (very basic) shadow map shader


float4x4 WorldViewProj;
float4x4 LightWorldViewProj;
sampler TexSamp : register(s0) = sampler_state { MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; };
sampler ShadowSamp : register(s1) = sampler_state { MinFilter = Point; MagFilter = Point; MipFilter = None; };

struct VSInputShadow
{
float4 Position : POSITION0;
};
struct VSOutputShadow
{
float4 Position : POSITION0;
float2 Depth : TEXCOORD0;
};
struct PSInputShadow
{
float2 Depth : TEXCOORD0;
};
struct PSOutputShadow
{
float4 Color : COLOR0;
};
struct VSInputScene
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
};
struct VSOutputScene
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
float3 SMTexCoord : TEXCOORD1; // z = light depth in lightproj space
};
struct PSInputScene
{
float2 TexCoord : TEXCOORD0;
float3 SMTexCoord : TEXCOORD1; // z = light depth in lightproj space
};
struct PSOutputScene
{
float4 Color : COLOR0;
};

VSOutputShadow ShadowVS(VSInputShadow input)
{
VSOutputShadow output;
output.Position = mul(input.Position, LightWorldViewProj);
output.Depth = output.Position.zw;
return output;
}
PSOutputShadow ShadowPS(PSInputShadow input)
{
PSOutputShadow output;
output.Color = float4(input.Depth.x/input.Depth.y, 1, 1, 1);
return output;
}

VSOutputScene SceneVS(VSInputScene input)
{
VSOutputScene output;
output.Position = mul(input.Position, WorldViewProj);
float4 lightPos = mul(input.Position, LightWorldViewProj);
output.TexCoord = input.TexCoord;
output.SMTexCoord = lightPos.xyz / lightPos.w;
return output;
}
PSOutputScene ScenePS(PSInputScene input)
{
PSOutputScene output;

float4 col = tex2D(TexSamp, input.TexCoord);
float lightDepth = tex2D(ShadowSamp, (input.SMTexCoord.xy+float2(1,1))/2).r;

if (input.SMTexCoord.z > lightDepth)
col *= 0.2;

output.Color = col;
return output;
}

technique RenderShadowMap
{
pass p0
{
VertexShader = compile vs_2_0 ShadowVS();
PixelShader = compile ps_2_0 ShadowPS();
CullMode = None;
}
}
technique RenderScene
{
pass p0
{
VertexShader = compile vs_2_0 SceneVS();
PixelShader = compile ps_2_0 ScenePS();
CullMode = None;
}
}


In the ScenePS, SMTexCoord always seems to be (0, 0, 0, 0), because when I override the returned color to one of these components, I always get a black scene.
Also, I'm not 100% sure the depth is being correctly set/read in either pixel shader, because it makes the entire scene in shadow as is.
I am pretty sure, though, that on the C++/CPU side, all effect params/textures/render targets are being set correctly. Before use, the shadow map (D3DFMT_R32F) is cleared to (0, 0, 0, 0), and it's associated depth buffer is cleared to 1.0f.
Sorry to be asking here for help AGAIN- i seem to have a lot of problems recently. (I'm not completely useless lol - I managed to get tokamak physics integrated into the game)

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!