DX11 Shadow Map

Started by
4 comments, last by Matthew Meeks 11 years, 3 months ago

Back again with a new problem, I'm having issues rendering a shadow map and then using it in another shader.

I create a 256x256 texture here with R32_Float as the format:


            Texture2D shadowBuffer = new Texture2D(device, new Texture2DDescription()
            {
                ArraySize = 1,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = Format.R32_Float,
                Height = 256,
                Width = 256,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default
            });
            var shadowView = new RenderTargetView(device, shadowBuffer, new RenderTargetViewDescription()
            {
                Format = Format.R32_Float,
                Dimension = RenderTargetViewDimension.Texture2D,
                MipSlice = 0,
            });
            shadowSView = new ShaderResourceView(device, shadowBuffer, new ShaderResourceViewDescription()
            {
                Format = Format.R32_Float,
                Dimension = ShaderResourceViewDimension.Texture2D,
                MostDetailedMip = 0,
                MipLevels = 1,
            });

I then render depth values to this texture and use it in another shader. My problem is that when the values are sampled from the texture, they are clamped to 0-1.

Any help is appreciated.

EDIT: It has occured to me that the problem may lie in light matrices. I create a orthographic projection and view matrix to act as a view from the sun:


        private static void SetUpSun()
        {
            SunProj = Matrix.OrthoLH(512, 512, 256, 1024);
        }

        private static void UpdateSun()
        {
            Vector3 SunPos = new Vector3((float)Math.Sin(wt / 1f) * -512, (float)Math.Cos(wt / 1f) * -512, 0) + Camera.Position;
            SunView = Matrix.LookAtLH(SunPos, Camera.Position, Vector3.UnitY);
        }
Advertisement

Typically the depth values stored in the shadow map are in range 0..1.

I don't understand from your post what is the actual problem.

Best regards!

Yeah sorry wrote that with a migraine and tried to do it as fast as possible, it didn't work. I've changed some things around and got closer to what I wanted, getting values in-between 0 and 1, except now the farther objects are 0 and the closer are 1. My understanding is that farther objects return depths closer to 1.

In the vs:


float4 worldPosition = mul(Output.position, World);
float4 viewPosition = mul(worldPosition, View);
Output.position = mul(viewPosition, Projection);

In the ps:


return input.position.z / input.position.w;

Really simple yet I still can't figure out why.

And just in case the problem lies in the matrices:

View:


View = Matrix.LookAtLH(Position, cameraFinalTarget, up);

Projection:


Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)Width / (float)Height, 0.01f, 800f);

Thanks for the help and apologies for the vagueness of the original post.

Yeah the z-values should be in between 0..1 where 0 is closer and 1 is further.

Why are you dividing your z-value by w? Isn't the division by w done automatically?

If you are doing basic shadow mapping, you may even disable your pixel shader if you are outputting to a depth-stencil buffer.

Best regards!

I have tried both with and without dividing by w with the same result. I had placed the division of w to ensure that w was always one as it should have been. I am not outputting to a depth-stencil buffer but rather a Texture2D render target. Not sure if this is the preferred method but i choose this because I eventually want to do more with the shadow map (different levels of light from the same source for semitransparent objects).

EDIT: I will try binding a depth buffer and attempt that way.

Thanks for the help!

Nevermind passed the wrong description.

This topic is closed to new replies.

Advertisement