我正在尝试使用写入 SV_DEPTH 的像素着色器,但使用的是默认值。我确信我写的深度值是正确的,但它们没有写入深度缓冲区。
我在着色器中使用颜色和深度结构作为像素着色器的输出:
struct PSOut
{
float4 color : SV_TARGET0;
float depth : SV_DEPTH;
};
我没有在默认的后备缓冲区上渲染。我像这样创建渲染目标和相应的深度缓冲区:
var texture = new SharpDX.Direct3D11.Texture2D(device, new Texture2DDescription
{
ArraySize = 1,
BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
CpuAccessFlags = CpuAccessFlags.None,
Format = Format.R8G8B8A8_UNorm,
Width = width,
Height = height,
MipLevels = 1,
OptionFlags = ResourceOptionFlags.None,
SampleDescription = new SampleDescription(1, 0),
Usage = ResourceUsage.Default
});
targetView = new RenderTargetView(device, texture);
resourceView = new ShaderResourceView(device, texture);
var depthBuffer = new SharpDX.Direct3D11.Texture2D(device, new Texture2DDescription()
{
Format = Format.D32_Float,
ArraySize = 1,
MipLevels = 1,
Width = width,
Height = height,
SampleDescription = new SampleDescription(1, 0),
Usage = ResourceUsage.Default,
BindFlags = BindFlags.DepthStencil,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None
});
depthView = new DepthStencilView(device, depthBuffer);
由于我使用的是工具包(v2.5),因此我将深度模板状态设置如下:
this.GraphicsDevice.SetDepthStencilState(this.GraphicsDevice.DepthStencilStates.Default);
对应于sharpdx 文档,默认用法设置了GPU 的读写访问权限。