mono/MonoGame — pull request #1666 by tomspilman (2013-04-19 07:53:44+00:00)
Title: GraphicsDevice RenderTarget Fixes

I came across a couple of big flaws in how render targets are managed in GraphicsDevice.

First if you look at this code...

```
var bindings = new RenderTargetBinding[1];

bindings[0] = new RenderTargetBinding(target1);
device.SetRenderTargets(bindings);
DrawToTarget();

bindings[0] = new RenderTargetBinding(target2);
device.SetRenderTargets(bindings);
DrawToTarget();
```

This code doesn't work... `target2` is never written to. This is because internally GraphicsDevice keeps a hold of the RenderTargetBinding array we passed to it. This means when we set it the second time the arrays are already equal... we then early out without change the render target.

The second bug was this...

```
device.SetRenderTarget(target1);
var bindings = graphicsDevice.GetRenderTargets();
bindings[0] = new RenderTargetBinding(target2);
device.SetRenderTargets(bindings);
DrawToTarget();
```

Like the code above... `target2` is not rendered to. This is because `GetRenderTargets()` returns the internal `_currentRenderTargetBindings` array... which then we modify. Again when we call `SetRenderTargets` the arrays are equal in value and no render target change occurs.

To solve this i made the following changes...

- Added _currentRenderTargetCount.
- _currentRenderTargetBindings is a fixed sized array and never null.
- GetRenderTargets() now returns copy of array.
- ApplyRenderTargets() now copies the incoming binding array elements.

I know there have been other render target bugs reported lately... not sure if my changes here resolve some of them as well.