XNA 4.0 Spritbatch - 不得在设备上设置渲染目标

时间:2015-09-04 18:01:53

标签: xna-4.0 spritebatch rendertarget

我正在将XNA 3.0云效果转换为XNA 4.0但我收到错误

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

它发生在这一行的第二个循环中:

mover.Parameters["PressureMap"].SetValue(PressureOffsets);

代码:

for (int i = 0; i < 10; i++)
{
   graf.SetRenderTarget(rt5);

   mover.Parameters["PressureMap"].SetValue(PressureOffsets);
   mover.Parameters["DivergenceMap"].SetValue(Divergence);
   mover.CurrentTechnique = mover.Techniques["Jacobi"];
   mover.CurrentTechnique.Passes[0].Apply();

   sp.Begin(SpriteSortMode.Deferred, BlendState.Opaque, SamplerState.PointWrap, DepthStencilState.DepthRead, RasterizerState.CullNone);
   sp.Draw(Velocity, new Vector2(0, 0), Color.White);
   sp.End();

   graf.SetRenderTarget(null);
   PressureOffsets = rt5;
}

我似乎无法在XNA 4.0中将纹理设置为效果参数,如果它已设置为rendertarget。 但我不知道如何将其转换为在XNA 4.0中工作:(

2 个答案:

答案 0 :(得分:0)

我解决了。 必须使用新的rendertar更改rendertarget。 我使用了一个帮助器类“RenderTargetState”。

    public RenderTargetState SetToDevice()
    {
        oldState = new RenderTargetState(GraphicsDevice);
        GraphicsDevice.SetRenderTarget(new RenderTarget2D(GraphicsDevice, renderTargetWidth, renderTargetHeight));

        return oldState;
    }

    public RenderTargetState BeginRenderToTexture()
    {
        oldState = SetToDevice();

        return oldState;
    }

    public Texture2D EndRenderGetTexture()
    {
        RenderTargetState renderBuffer = oldState.SetToDevice();
        oldState = null;
        return renderBuffer.RenderTarget;
    }

所以代码现在看起来像这样:

for (int i = 0; i < 10; i++)
{
  rt5.BeginRenderToTexture();
  graf.SetRenderTarget(rt5.RenderTarget);    

  mover.Parameters["PressureMap"].SetValue(PressureOffsets);
  mover.Parameters["DivergenceMap"].SetValue(Divergence);
  mover.CurrentTechnique = mover.Techniques["Jacobi"];
  mover.CurrentTechnique.Passes[0].Apply();

  sp.Begin(SpriteSortMode.Deferred, BlendState.Opaque);
  sp.Draw(Velocity, new Vector2(0, 0), Color.White);
  sp.End();

  graf.SetRenderTarget(null);
  PressureOffsets = rt5.EndRenderGetTexture();
}

答案 1 :(得分:0)

有一种更简单的方法可以做到这一点。

rt5 = PressureOffsets as RenderTarget2D
graf.SetRenderTarget(null);
PressureOffsets = rt5;