# Deferred rendering + Shadow maps

I have a 1920x1200 shadowmap which just has the depth values of my scene drawn from the point of view of the light.

I also have a 1920x1200 gbuffer that draws my scene to a fullscreen quad.

How can I sample the shadowmap from the gbuffer lighting phase? What kind of maths needs to be performed? I have access to view space normal and view space position.

• Why are you rendering a 1920x1200 shadow map? Your shadow map isn't in screen space; it's in light space. – Nicol Bolas Apr 13 '12 at 1:46
• Dont know, its just for schoolwork I don't really care about performance. – Dave Apr 13 '12 at 21:36
• It's not a question of performance so much as quality. – Nicol Bolas Apr 13 '12 at 21:52
• What should I set it to for better quality? I assumed that the larger the better. – Dave Apr 13 '12 at 22:39
• My point is that the shadow map's size doesn't need to be the same size as the screen. You can make it bigger, smaller, whatever you feel works best. – Nicol Bolas Apr 13 '12 at 22:41

Once you have the position of the shaded point, it's just the same as applying a shadow map in regular forward lighting. You need to convert the shaded point to shadow map screen space, which means getting its world position and then transforming it through the same view and projection matrices used to render the shadow map. You can bake all down into one 4x4 "view space to shadow" matrix that you pass in to your lighting shader.

Then once you have the point in shadow map space, do a texture lookup with comparison mode enabled, as usual for shadow maps.

• Your the best! Helping newbies like that, You should be rewarded for your efforts. I managed to get it working, no matter what text I read it was unclear until I read your simple answer here. – Dave Apr 13 '12 at 21:35
• Question: In the forward rendering setup, it was enough to do this transformation in the vertex shader and hand the pixel shader a set of interpolated texture lookup coordinates. It looks to me though like I have to do this transformation per-pixel in deferred rendering because the coordinates that I need are inside a texture that I can only access in the fragment shader. Is there a way around that? – TravisG Aug 16 '13 at 15:27
• @TravisG Sort of. You can do most of the transformation in the vertex shader, and all the fragment shader has to do is convert the depth buffer to linear and do a mad to get the final position. Matt Pettineo has a blog post on the subject: Position From Depth. – Nathan Reed Aug 16 '13 at 16:42