Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?)

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

In XNA, setting a a render target (including the backbuffer) clears it. This is because preserving the contents of the buffer requires a slow copy operation on the Xbox 360. The PC version just uses the same behaviour to make porting easier.

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.

If it's possible, without requiring more targets or draw operations (and it usually is), it's better to re-order your draw function so you don't have to re-set targets you've already rendered to.

More info in this blog post.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.