我正在尝试使用写入SV_DEPTH的像素着色器,但使用了deault值。我确信我写的深度值是正确的,但它们不会写入深度缓冲区。
我在着色器中使用结构作为颜色和深度作为像素着色器的输出:
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的读写访问权限。