• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
c4sh

My texture stores only the clear value

3 posts in this topic

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

 

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

 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?

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0