# multipass shadow mapping renderer in XNA

I am wanting to implement a multipass renderer in XNA (additive blending combines the contributions from each light). I have the renderer working without any shadows, but when I try to add shadow mapping support I run into an issue with switching render targets to draw the shadow maps. When I switch render targets, I lose the contents of the backbuffer which ruins the whole additive blending idea.

For example:

Draw()
{
DrawAmbientLighting()
foreach (DirectionalLight)
{
DrawDirectionalShadowMap() // <-- I lose all previous lighting contributions when I switch to the shadow map render target here
DrawDirectionalLighting()
}
}


Is there any way around my issue? (I could render all the shadow maps first, but then I have to make and hold onto a render target for each light that casts a shadow--is this the only way?)

You can override this behaviour by passing RenderTargetUsage.PreserveContents to the constructor of RenderTarget2D.
To set it on the backbuffer, respond to PreparingDeviceSettings and modify GraphicsDeviceInformation.PresentationParameters.RenderTargetUsage.