DirectX10 access ZBuffer

Started by
6 comments, last by robydx 16 years, 8 months ago
In the papers Intro to Direct3D10, on paper 5a there is written this "Directly binding the z buffer eliminates the need to write out depth into a texture so it can later be used for scene depth based / deferred rendering" So it mean that I can use ZBuffer into shader. But I can't create a shader resource view as I've maden for render target. Anyone know how I can use DepthStencil buffer to access depth information?

http://www.notjustcode.it

DirectX tutorial

Advertisement
Quote:Original post by robydx
In the papers Intro to Direct3D10, on paper 5a there is written this

"Directly binding the z buffer eliminates the need to write out depth into a texture so it can later be used for scene depth based / deferred rendering"

So it mean that I can use ZBuffer into shader.
But I can't create a shader resource view as I've maden for render target.
Anyone know how I can use DepthStencil buffer to access depth information?

It turns out that you can't actually create a shader resource view from a depth-stencil texture if it's multi-sampled. It simply fails to bind. I believe a few nVidia samples mention this problem as well. This has been a major source of problems for us, but I believe it will be fixed in DX 10.1. So until then, we actually have to go down the MRT route anyway. (As a side note, if someone here has actually managed to get this to work I'd love to hear how!)

But if that isn't the case, what happens when you try to create/set the shader resource view? Are there any error messages? If you go to the DirectX Control Panel in the SDK folder you can force the Debug device and get a bunch of helpful information.
My understanding is that this is one of the subtle but important changes from 10.0 to 10.1 - reading back MSAA'd depth information.

If you don't want MSAA then you should be able to do exactly what the paper suggests, but if you want MSAA you're stuck with traditional D3D9-era methods.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

it's not multisampled
I simply try to create the shader resource view. The code is this

D3D10_TEXTURE2D_DESC descDepth;
descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D32_FLOAT;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D10_USAGE_DEFAULT;
descDepth.BindFlags = D3D10_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;

ID3D10Texture2D *pDepthBuffer;
hr=device->CreateTexture2D( &descDepth, NULL, &pDepthBuffer );

Here I can use as bind flags D3D10_BIND_DEPTH_STENCIL | D3D10_BIND_SHADER_RESOURCE or it fail texture2D creation


D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
HRESULT hr= device->GetDevice()->CreateDepthStencilView( pDepthBuffer, &descDSV, &renderTargetDepth );
pDepthBuffer->Release();

D3D10_SHADER_RESOURCE_VIEW_DESC srDesc;
srDesc.Format = DXGI_FORMAT_D32_FLOAT;
srDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
srDesc.Texture2D.MostDetailedMip = 0;
srDesc.Texture2D.MipLevels = 1;

hr=device->GetDevice()->CreateShaderResourceView( pDepthBuffer, &srDesc, &resource );

And here i gave me an error so I can't create the shader resource view.

http://www.notjustcode.it

DirectX tutorial

What does the debug runtime have to say? It's usually very good at explaining error codes.

You can set the second parameter's of both CreateShaderResourceView() and CreateDepthStencilView() to NULL and let D3D take the top-level MIP and format according to the underlying resource. This is effectively what you're explicitly telling it, so try letting D3D figure it out itself.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

D3D10: ERROR: ID3D10Device::CreateShaderResourceView: The format (0x28, D32_FLOAT) cannot be used with a ShaderResource view. [ STATE_CREATION ERROR #127: CREATESHADERRESOURCEVIEW_INVALIDFORMAT ]
D3D10: ERROR: ID3D10Device::CreateShaderResourceView: A ShaderResourceView cannot be created of a Resource that did not specify the D3D10_BIND_SHADER_RESOURCE BindFlag. [ STATE_CREATION ERROR #129: CREATESHADERRESOURCEVIEW_INVALIDRESOURCE ]
Eccezione first-chance a 0x7632b09e in TutorialDX10.exe: Eccezione di Microsoft C++: _com_error nella posizione di memoria 0x0012f540..
D3D10: ERROR: ID3D10Device::CreateShaderResourceView: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #131: CREATESHADERRESOURCEVIEW_INVALIDARG_RETURN ]

here the debugger tell me that I must use bind_shader_flags when I create the DepthStencil texture

So I use this Bind_Flag

D3D10_TEXTURE2D_DESC descDepth;
descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D32_FLOAT;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D10_USAGE_DEFAULT;
descDepth.BindFlags = D3D10_BIND_DEPTH_STENCIL | D3D10_BIND_SHADER_RESOURCE;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;

but create texture give me this error




D3D10: ERROR: ID3D10Device::CreateTexture2D: The format (0x28, D32_FLOAT) cannot be bound as a ShaderResource or cast to a format that could be bound as a ShaderResource. Therefore this format cannot support D3D10_BIND_SHADER_RESOURCE. Specifiying the format R32_TYPELESS instead would solve this issue. [ STATE_CREATION ERROR #92: CREATETEXTURE2D_UNSUPPORTEDFORMAT ]
Eccezione first-chance a 0x7632b09e in TutorialDX10.exe: Eccezione di Microsoft C++: _com_error nella posizione di memoria 0x0012f4cc..
D3D10: ERROR: ID3D10Device::CreateTexture2D: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #104: CREATETEXTURE2D_INVALIDARG_RETURN ]

All depth stencil format dont work. If I use R32_TYPELESS I can't create the depth stencil.




http://www.notjustcode.it

DirectX tutorial

Pretty much what I expected - thought it'd be a casting/format issue [smile]

My understanding of views/resources is that you can create the actual Texture2D as a R32_TYPELESS and then the DSV as D32_FLOAT and the SRV as R32_FLOAT and it should work fine. Are you saying this doesn't work? If so, what debug messages do you get?

Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

yes, know it's work. I use R32_TYPELESS for the texture, D32 for stencilView and R32_FLOAT for resourceView.
I didn't know that I can use different format between texture and his resource.
Thank you, it's really usefull for DOF and outline rendering.
Here the code

D3D10_TEXTURE2D_DESC descDepth;
descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_R32_TYPELESS;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D10_USAGE_DEFAULT;
descDepth.BindFlags = D3D10_BIND_DEPTH_STENCIL | D3D10_BIND_SHADER_RESOURCE;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;

ID3D10Texture2D *pDepthBuffer;

if( FAILED(device->GetDevice()->CreateTexture2D( &descDepth, NULL, &pDepthBuffer )))return false;

D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
descDSV.Format = DXGI_FORMAT_D32_FLOAT;
descDSV.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
HRESULT hr= device->GetDevice()->CreateDepthStencilView( pDepthBuffer, &descDSV, &renderTargetDepth );


D3D10_SHADER_RESOURCE_VIEW_DESC srDesc;
srDesc.Format = DXGI_FORMAT_R32_FLOAT;
srDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
srDesc.Texture2D.MostDetailedMip = 0;
srDesc.Texture2D.MipLevels = 1;

hr=device->GetDevice()->CreateShaderResourceView( pDepthBuffer, &srDesc, &zResource );

pDepthBuffer->Release();


http://www.notjustcode.it

DirectX tutorial

This topic is closed to new replies.

Advertisement