Jump to content
  • Advertisement
Sign in to follow this  
BelkacemTuxLm

DX11 Depth buffer ! fail to save it !

This topic is 2596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

sometime we need to get the depth buffer and render target from back buffer to use them in our application , so i try to get the render target and the depth buffer and save them to my hard disk .

OK , i success to save the render target but i fail to save the depth map and i don't know why

her is the full code

void CL_RenderToTexture(ID3D11Device *const Device , Buffer const &mybuffer , UINT const &MipLevels , ::DXGI_SAMPLE_DESC const &SampleDesc )
{
_Target_Texture_Desc.ArraySize = 1 ;
_Target_Texture_Desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE ;
_Target_Texture_Desc.CPUAccessFlags = NULL ;
_Target_Texture_Desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM ;
_Target_Texture_Desc.Height = mybuffer.Height ;
_Target_Texture_Desc.MipLevels = MipLevels ;
_Target_Texture_Desc.MiscFlags = 0 ;
_Target_Texture_Desc.SampleDesc = SampleDesc ;
_Target_Texture_Desc.Usage = D3D11_USAGE_DEFAULT ;
_Target_Texture_Desc.Width = mybuffer.Width ;

HRESULT hr = Device->CreateTexture2D ( &_Target_Texture_Desc , NULL , &_Target_Texture);

Device->CreateRenderTargetView (_Target_Texture ,NULL , &_Target_View);
Device->CreateShaderResourceView ( _Target_Texture, NULL , &_Color_Map);


_Depth_Texture_Desc.ArraySize = 1 ;
_Depth_Texture_Desc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE ;
_Depth_Texture_Desc.CPUAccessFlags = NULL ;
_Depth_Texture_Desc.Format = ::DXGI_FORMAT_R32_TYPELESS ;
_Depth_Texture_Desc.Height = mybuffer.Height ;
_Depth_Texture_Desc.MipLevels = MipLevels ;
_Depth_Texture_Desc.MiscFlags = 0 ;
_Depth_Texture_Desc.SampleDesc = SampleDesc ;
_Depth_Texture_Desc.Usage = D3D11_USAGE_DEFAULT ;
_Depth_Texture_Desc.Width = mybuffer.Width ;



_Depth_shader_Desc.Format = DXGI_FORMAT_R32_FLOAT ;
_Depth_shader_Desc.Texture2D.MostDetailedMip = 0 ;
_Depth_shader_Desc.Texture2D.MipLevels = _Depth_Texture_Desc.MipLevels ;
_Depth_shader_Desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D ;

hr = Device->CreateTexture2D ( &_Depth_Texture_Desc , NULL , &_Depth_Texture);

ZeroMemory(&_Depth_Desc , sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC));
_Depth_Desc.Format = DXGI_FORMAT_D32_FLOAT ;
_Depth_Desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D ;
_Depth_Desc.Texture2D.MipSlice = 0 ;

hr = Device->CreateDepthStencilView ( _Depth_Texture , &_Depth_Desc ,&_Depth_View);


Device->CreateShaderResourceView (_Depth_Texture ,&_Depth_shader_Desc, &_Depth_Map);

_View_Port.Height = mybuffer.Height ;
_View_Port.MaxDepth = 1.0f ;
_View_Port.MinDepth = 0.0f ;
_View_Port.TopLeftX = 0.0f ;
_View_Port.TopLeftY = 0.0f ;
_View_Port.Width = mybuffer.Width;



}

void Drawdepth(::ID3D11DeviceContext *const Context)
{


Context->RSSetViewports (1 , &_View_Port );
float color [4] = { 0.0f , 1.0f ,0.0f ,0.0f };
::ID3D11RenderTargetView *_temprender [1] = {_Target_View};
Context->OMSetRenderTargets (1 , _temprender , _Depth_View );

if (_Target_View)
Context->ClearRenderTargetView ( _Target_View , color );

Context->ClearDepthStencilView (_Depth_View , D3D11_CLEAR_DEPTH , 1.0f , 0 );

::D3DX11SaveTextureToFileA ( Context , _Depth_Texture , ::D3DX11_IFF_BMP , "Depth.bmp"); /* Fail */
::D3DX11SaveTextureToFileA ( Context , _Target_Texture , ::D3DX11_IFF_BMP , "Color.bmp");/* SUCCEEDED */

}


any help and thank you

Share this post


Link to post
Share on other sites
Advertisement
Probably because the depth buffer isn't in a format that can be saved as a BMP file. Save it as a DDS instead, and use the texture tool in the DX SDK to inspect it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!