XNA 4.0 - 分配渲染目标时的奇怪行为

时间:2015-01-18 11:50:46

标签: 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 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);
}

没有渲染 - 我有一个紫色的屏幕! 这对你有意义吗?

P.S。 我知道样本没有多大意义(比如分配和立即取消分配渲染目标)。这里的真实情况要复杂得多,但我能够将问题的本质缩小到这种奇怪的行为,我可以在这个非常简单的例子中证明这一点!

1 个答案:

答案 0 :(得分:0)

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

在我看来,后备缓冲区的内容 - 就像渲染目标一样 - 只要你切换到另一个渲染目标就会被破坏。