0
\$\begingroup\$

I've got this simple code that uses SpriteBatch to draw a texture onto a RenderTarget2D:

private void drawScene(GameTime g)
{
    GraphicsDevice.Clear(skyColor);
    GraphicsDevice.SetRenderTarget(targetScene);
    drawSunAndMoon();
    effect.Fog = true;
    GraphicsDevice.SetVertexBuffer(line);
    effect.MainEffect.CurrentTechnique.Passes[0].Apply();
    GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
    GraphicsDevice.SetRenderTarget(null);
    SceneTexture = targetScene;
}

private void drawPostProcessing(GameTime g)
{
    effect.SceneTexture = SceneTexture;
    GraphicsDevice.SetRenderTarget(targetBloom);
    spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, null, null, null);
    {
        if (Bloom) effect.BlurEffect.CurrentTechnique.Passes[0].Apply();
        spriteBatch.Draw(
            targetScene,
            new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
            Color.White);
    }
    spriteBatch.End();
    BloomTexture = targetBloom;
    GraphicsDevice.SetRenderTarget(null);
}

Both methods are called from my Draw(GameTime gameTime) function. First drawScene is called, then drawPostProcessing is called.

The thing is, when I run this code I get an error on the spriteBatch.Draw call:

The render target must not be set on the device when it is used as a texture.

I already found the solution, which is to draw the actual render target (targetScene) to the texture so it doesn't create a reference to the loaded render target.

However, to my knowledge, the only way of doing this is to write:

GraphicsDevice.SetRenderTarget(outputTarget)
SpriteBatch.Draw(inputTarget, ...)
GraphicsDevice.SetRenderTarget(null)

Which encounters the same exact problem I'm having right now.

So, the question I'm asking is: how would I render inputTarget to outputTarget without reference issues?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Sounds like targetScene and targetBloom are the same texture. You should check you're not assigning one to the other somewhere in your code. Or maybe, your spriteBatch is using the same texture you're using as a render target (targetBloom); you just can't do that (that's a hardware limitation).

I know your question is about how to fix your fix, but I don't understand what you're trying to do, and I see no reason why your first solution wouldn't work.

\$\endgroup\$
1
  • \$\begingroup\$ OH, just as I read that everything just clicked into understanding. Yeah, targetScene and targetBloom were the same. I was initializing them via: targetScene = targetBloom = new RenderTarget2D(...) \$\endgroup\$
    – Jared
    Apr 4, 2012 at 18:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .