with the recode of the core of my engine, most rendering functionality was gone.
That was also the case with the depth buffer, and now I would like to re-implement it finally.
However, I would not post here if that would work out of the box.
When I set my DepthStencilView and DepthStencilState, my scene just looks like no depth buffer would be there at all.
And I have no idea why.
It would be great if you could have a look over my code and maybe tell me what is wrong.
Here is the creation of the texture/view:
RenderToDepthStencilStruct(UINT SizeX,UINT SizeY,DXGI_FORMAT Format, HRESULT* Result=NULL, DXGI_FORMAT DSVFormat=DXGI_FORMAT_UNKNOWN, DXGI_FORMAT SRVFormat=DXGI_FORMAT_UNKNOWN) { HRESULT hr=S_OK; Texture=NULL; ShaderResView=NULL; DepthStencilView=NULL; if(SizeX == 0 || SizeY == 0) { LogError() << L"SizeX or SizeY can't be 0"; } if(Format==0) { LogError() << L"DXGI_FORMAT_UNKNOWN (0) isn't a valid texture format"; } //Create a new render target texture D3D11_TEXTURE2D_DESC Desc; ZeroMemory( &Desc, sizeof( D3D10_TEXTURE2D_DESC ) ); Desc.ArraySize = 1; Desc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE; Desc.Usage = D3D11_USAGE_DEFAULT; Desc.Format = Format; Desc.Width = SizeX; Desc.Height = SizeY; Desc.MipLevels = 1; Desc.SampleDesc.Count = 1; LE(DXUTGetD3D11Device()->CreateTexture2D(&Desc,NULL,&Texture)); //Create a render target view D3D11_DEPTH_STENCIL_VIEW_DESC DescDSV; DescDSV.Format = (DSVFormat != DXGI_FORMAT_UNKNOWN ? DSVFormat : Desc.Format); DescDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; DescDSV.Texture2D.MipSlice = 0; DescDSV.Flags=0; LE(DXUTGetD3D11Device()->CreateDepthStencilView((ID3D11Resource *)Texture,&DescDSV,&DepthStencilView)); // Create the resource view D3D11_SHADER_RESOURCE_VIEW_DESC DescRV; DescRV.Format = (SRVFormat != DXGI_FORMAT_UNKNOWN ? SRVFormat : Desc.Format); DescRV.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; DescRV.Texture2D.MipLevels = 1; DescRV.Texture2D.MostDetailedMip = 0; LE(DXUTGetD3D11Device()->CreateShaderResourceView( (ID3D11Resource *)Texture, &DescRV, &ShaderResView )); if(FAILED(hr)) { LogError() << L"Coould not create ID3D11Texture2D, ID3D11ShaderResourceView, or ID3D11DepthStencilView. Killing created resources (If any)."; ReleaseAll(); if(Result)*Result=hr; return; } LogInfo() << L"RenderToDepthStencilStruct: Successfully created ID3D11Texture2D, ID3D11ShaderResourceView, and ID3D11DepthStencilView."; if(Result)*Result=hr; }
Thats how I call it:
DepthBuffer=new RenderToDepthStencilStruct(SizeX, SizeY, DXGI_FORMAT_R32_TYPELESS, &hr, DXGI_FORMAT_D32_FLOAT, DXGI_FORMAT_R32_FLOAT);
And here I set the view and the state:
DXUTGetD3D11DeviceContext()->OMSetRenderTargets(1, &RenderTargetView, DepthBuffer->GetDepthStencilView()); DXUTGetD3D11DeviceContext()->OMSetDepthStencilState(DefaultDepthStencilState, 0);
I had this problem before, and I even made a thread for it: http://www.gamedev.net/community/forums/topic.asp?topic_id=574751
But there I just forgot to set the depth stencil state. Now I do it, and I don't know what's wrong...