XNA 4.0-指定渲染目标时的奇怪行为

XNA 4.0-指定渲染目标时的奇怪行为,xna,direct3d,Xna,Direct3d,我目前面临以下奇怪的问题: 以下代码段按预期完美呈现: private void DoRenderSkybox (GameTime Time) { this.Device.SetRenderTarget(this.GridTexture); this.Device.SetRenderTarget(null); // compute a temporary transformation matrix containing // the combined world and pr

我目前面临以下奇怪的问题: 以下代码段按预期完美呈现:

private void DoRenderSkybox (GameTime Time) {
  this.Device.SetRenderTarget(this.GridTexture);
  this.Device.SetRenderTarget(null);


  // compute a temporary transformation matrix containing
  // the combined world and projection transfromation
  Matrix WorldViewProjection = this.Camera.View * this.Camera.Projection;

  // set the render target to the back buffer in any case
  this.Device.SetRenderTarget(null);

  // assign the vertex - declaration and the vertex- and the index - buffer
  this.Device.SetVertexBuffer(this.SkyboxVertices);
  this.Device.Indices = this.SkyboxIndices;

  // choose the appropriate technique for the current render pass
  this.SceneEffect.CurrentTechnique = SceneEffect.Techniques["Skybox"];
  this.SceneEffect.CurrentTechnique.Passes[0].Apply();
  this.SceneEffect.Parameters["WorldViewProjection"].SetValue(WorldViewProjection);

  // finally render the sykbox with disabled depth stencil buffering
  this.Device.DepthStencilState = DepthStencilState.None;
  this.Device.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, 36, 0, 12);
  this.Device.DepthStencilState = DepthStencilState.Default;


  //this.Device.SetRenderTarget(this.GridTexture);
  //this.Device.SetRenderTarget(null);
}
但是,如果我在函数末尾指定(并立即取消指定)渲染目标,如下所示:

private void DoRenderSkybox (GameTime Time) {
  //this.Device.SetRenderTarget(this.GridTexture);
  //this.Device.SetRenderTarget(null);


  // compute a temporary transformation matrix containing
  // the combined world and projection transfromation
  Matrix WorldViewProjection = this.Camera.View * this.Camera.Projection;

  // set the render target to the back buffer in any case
  this.Device.SetRenderTarget(null);

  // assign the vertex - declaration and the vertex- and the index - buffer
  this.Device.SetVertexBuffer(this.SkyboxVertices);
  this.Device.Indices = this.SkyboxIndices;

  // choose the appropriate technique for the current render pass
  this.SceneEffect.CurrentTechnique = SceneEffect.Techniques["Skybox"];
  this.SceneEffect.CurrentTechnique.Passes[0].Apply();
  this.SceneEffect.Parameters["WorldViewProjection"].SetValue(WorldViewProjection);

  // finally render the sykbox with disabled depth stencil buffering
  this.Device.DepthStencilState = DepthStencilState.None;
  this.Device.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, 36, 0, 12);
  this.Device.DepthStencilState = DepthStencilState.Default;


  this.Device.SetRenderTarget(this.GridTexture);
  this.Device.SetRenderTarget(null);
}
没有渲染任何内容-我得到了一个紫色屏幕! 这对你有意义吗

附言。
我知道这个示例没有多大意义(比如指定并立即取消指定渲染目标)。这里的真实场景要复杂得多,但我能够将问题的本质缩小到这种奇怪的行为,我可以在这个非常简单的示例中演示

刚刚通过进一步检查API找到了解决方案:将Params.RenderTargetUsage=RenderTargetUsage.PreserveContents添加到PresentationParameter结构中解决了问题

在我看来,无论何时切换到另一个渲染目标,backbuffer的内容都会被销毁,就像渲染目标一样