Hello, I'm new to this community, so I ask for a bit of patience with my knowledge!. After using the search function and not finding anything specifically useful or that works, I decided to ask for help on this issue.
I'm programming a shader in SharpDX (DirectX11, C#) and I need to store the depth in a texture in a first pass, and then use it in a second one.
The thing is my texture returns only the clear value I'm using (1.0f) even though I think I've followed the necessary steps to write and use a Depth Texture in a shader.
Here's how I set the Texture2D, the ShaderResourceView, the DepthStencilView and the DepthStencilStateDescription and then bind them.
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,
});
// shader resource
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,
}
});
this.depthBufferStencilView = new DepthStencilView(device, depthBuffer, new DepthStencilViewDescription()
{
Format = Format.D32_Float,
Dimension = DepthStencilViewDimension.Texture2D,
Texture2D = new DepthStencilViewDescription.Texture2DResource()
{
MipSlice = 0
}
});
this.device.ImmediateContext.OutputMerger.SetTargets(depthBufferStencilView);
this.device.ImmediateContext.ClearDepthStencilView(this.depthBufferStencilView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0);
var depthStencilDesc = new DepthStencilStateDescription()
{
DepthComparison = Comparison.LessEqual,
DepthWriteMask = global::SharpDX.Direct3D11.DepthWriteMask.All,
IsDepthEnabled = true,
};
this.depthStencilState = new DepthStencilState(this.device, depthStencilDesc);
this.device.ImmediateContext.OutputMerger.SetDepthStencilState(this.depthStencilState);
this.depthStencilShaderResourceVariable = effect.GetVariableByName("DepthTexture").AsShaderResource();
this.depthStencilShaderResourceVariable.SetResource(this.depthBufferShaderResourceView);
And here's how I'm extracting the value of the Depth Texture in the pixel shader:
float depthPixel = DepthTexture.Load(posTex);
float4 color = float4(depthPixel, depthPixel , depthPixel, 1.0f ); // just a simple test, returns all pixels I in full white...
Any help would be appreciated. I would say I'm doing the "basics" correctly but I don't know if I'm either missing something or if there's something completely wrong with what I did.
Thanks, c4sh