I'm programming a two-pass effect in DirectX 11 (SharpDX). It's supposed to write the depth to a texture in the first pass and then use that texture to extract data on the second one in the pixel shader.
What I get is a white screen, with nothing but the interface and I don't know why nothing is being printed. What could be the problem? I would say I should get at least something from the Depth Texture.
This is how I'm setting the depth texture values:
this.depthBuffer = new Texture2D(device, new Texture2DDescription()
{
Format = Format.R32_Typeless,
ArraySize = 1,
MipLevels = 1,
Width = (int)host.ActualWidth,
Height = (int)host.ActualHeight,
SampleDescription = new SampleDescription(1, 0),
Usage = ResourceUsage.Default,
BindFlags = BindFlags.DepthStencil | BindFlags.ShaderResource,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
});
this.depthBufferShaderResourceView = new ShaderResourceView(this.device, this.depthBuffer, new ShaderResourceViewDescription()
{
Format = Format.R32_Float,
Dimension = ShaderResourceViewDimension.Texture2D,
Texture2D = new ShaderResourceViewDescription.Texture2DResource()
{
MipLevels = 1,
MostDetailedMip = 0,
}
});
var depthStencilDesc = new DepthStencilStateDescription()
{
DepthComparison = Comparison.LessEqual,
DepthWriteMask = global::SharpDX.Direct3D11.DepthWriteMask.All,
IsDepthEnabled = true,
};
And here is how I sample the depth in the .fx file:
int3 posTex = int3(input.p.xy, 0);
float depthPixel = DepthTexture.Load(posTex);
float4 color = float4(depthPixel, depthPixel , depthPixel, 1.0f );
return color;
And here the way I'm now setting the Depth Buffer stencil view as a Render Target in 2 passes. In the first I try to set the depthstencilview as a target. In the second pass I'm trying to set teh depth texture as a shader resource to read from it.
this.device.ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(this.vertexBuffer, LinesVertex.SizeInBytes, 0));
// PASS 0
this.device.ImmediateContext.OutputMerger.SetTargets(depthBufferStencilView);
this.device.ImmediateContext.ClearDepthStencilView(this.depthBufferStencilView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0);
this.technique.GetPassByIndex(0).Apply(this.device.ImmediateContext);
this.device.ImmediateContext.DrawIndexed(this.geometry.Indices.Length, 0, 0);
// PASS 1
this.device.ImmediateContext.OutputMerger.ResetTargets(); // unbinding the depthStencilView
this.device.ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(this.vertexBuffer, LinesVertex.SizeInBytes, 0));
this.depthStencilShaderResourceVariable = effect.GetVariableByName("DepthTexture").AsShaderResource();
this.depthStencilShaderResourceVariable.SetResource(this.depthBufferShaderResourceView);
this.technique.GetPassByIndex(1).Apply(this.device.ImmediateContext);
this.device.ImmediateContext.DrawIndexed(this.geometry.Indices.Length, 0, 0);
Finally, this is how I set the two passes in the .fx file:
technique11 RenderMyTechnique
{
pass P0
{
SetDepthStencilState( DSSDepthLessEqual, 0 );
SetVertexShader ( CompileShader( vs_4_0, VShader() ) );
SetHullShader ( NULL );
SetDomainShader ( NULL );
SetGeometryShader ( NULL );
SetPixelShader ( NULL );
}
pass P1
{
SetDepthStencilState( DSSDepthLessEqual, 0 );
SetVertexShader ( CompileShader( vs_4_0, VShader() ) );
SetHullShader ( NULL );
SetDomainShader ( NULL );
SetGeometryShader ( CompileShader( gs_4_0, GShader() ) );
SetPixelShader ( CompileShader( ps_4_0, PShader() ) );
}
}
DepthStencilView
of the buffer (using theD32_FLOAT
format) and pass it in yourSetRenderTargets
call. \$\endgroup\$DepthWrite
is turned on in yourDepthStencilState
bound to the context when rendering when you render your scene. I don't recall if it's set by default, but it might not be. \$\endgroup\$DepthStencilView
to mySetRenderTargets
call and set aDepthStencilState
withIsDepthEnabled
astrue
(added the DepthStencilStateDescription in the question. (see changes in question) So I realized I'm now getting always a 1.0f because I'm clearing the DepthBuffer to 1.0f. So I'm not writing on it, the thing is I don't know why I'm not writing to it..? @NathanReed @SeanMiddleditch \$\endgroup\$