XNA 4.0 - 分配渲染目标时的奇怪行为
XNA 4.0 - strange behaviour when assigning render target
我目前面临以下奇怪的问题:
以下代码片段可以按预期完美呈现:
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。
我知道该示例没有多大意义(例如分配和立即取消分配渲染目标)。这里的真实情况要复杂得多,但我能够将问题的本质缩小到这种奇怪的行为,我可以在这个非常简单的例子中证明这一点!
刚刚通过进一步检查 API 找到了解决方案:将 Params.RenderTargetUsage = RenderTargetUsage.PreserveContents 添加到 PresentationParameter 结构中解决了问题。
在我看来,后台缓冲区的内容就像渲染目标一样,只要您切换到另一个渲染目标就会被销毁。
我目前面临以下奇怪的问题: 以下代码片段可以按预期完美呈现:
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。 我知道该示例没有多大意义(例如分配和立即取消分配渲染目标)。这里的真实情况要复杂得多,但我能够将问题的本质缩小到这种奇怪的行为,我可以在这个非常简单的例子中证明这一点!
刚刚通过进一步检查 API 找到了解决方案:将 Params.RenderTargetUsage = RenderTargetUsage.PreserveContents 添加到 PresentationParameter 结构中解决了问题。
在我看来,后台缓冲区的内容就像渲染目标一样,只要您切换到另一个渲染目标就会被销毁。