Jump to content

  • Log In with Google      Sign In   
  • Create Account

My texture stores only the clear value


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 c4sh   Members   -  Reputation: 127

Like
0Likes
Like

Posted 15 November 2013 - 11:48 AM

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

 



Sponsor:

#2 MJP   Moderators   -  Reputation: 11741

Like
1Likes
Like

Posted 15 November 2013 - 12:55 PM

According to your code, it looks like you're binding your depth buffer as a depth-stencil view with depth-writes enabled, and simultaneously binding it as a shader resource view for reading. You can't do this, because it could create a read/write hazard where the the shader reads stale data for a pixel. This is also a restriction for render targets, btw. If you want to read from the depth buffer, you'll need to finish performing all writes and then unbind it as a depth-stencil view before binding it as a shader resource view. Or if you still need to use the depth buffer for depth testing while you're reading from it, you can create a separate read-only depth-stencil view and bind that for any passes that need to read from the depth buffer.

If you want to make sure that this is the case, you should enable the debug layer when creating the device. When you do this, D3D will output warning and error messages to the native debugger output when something bad happens.



#3 c4sh   Members   -  Reputation: 127

Like
0Likes
Like

Posted 18 November 2013 - 04:56 AM

 Is it possible that the key aspect is the 2-pass structure? I'm setting all these structures up just once. And running the two passes. Could it make sense to set them up at the beginning for the first pass and then changing/unbinding some of them?



#4 c4sh   Members   -  Reputation: 127

Like
0Likes
Like

Posted 19 November 2013 - 09:13 AM

As hinted by MJP, I tried to unbind the depth stencil view before binding it as a shader resource view, so this is how I'm now setting the resources at each pass:

 

// running of the passes in the .cs file

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);

Unfortunately I'm not getting now ANY result from the Depth Texture I'm reading. Just a white screen with nothing (not even the "clear" result I set on the Depth Stencil View).
I'm I doing something wrong on the order/way of setting and unbinding the resources here? Thanks for any hint.

EDIT:

I read this in an old topic of this forum:


If you create the backing resource as a typeless 32-bit surface (DXGI_FORMAT_R32_TYPELESS), you can create both depth stencil and shader resource views against it so you can directly access it as a texture after you've used it as an actual depth buffer.

Is this still valid? Because then maybe I woudln't need the unbinding etc and it should be correct to use it like I was doing before, and the error could be somewhere else?






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS