Sometimes, we need to check the content of depth/stencel buffer. For example, in the previous chapter, we want to check the stencel buffer to see if the stencel value we set works, in this case, depth/stencel buffer content is output. However, this content cannot be directly viewed in the GPU. Texture copy and resource ing are required, copy to system memory for direct viewing.
The following is the code for outputting depth/stencel buffer. The final result is output to a CSV file, which is easy to view in Excel. I added this function to the d3dclass class:
Bool d3dclass: savedepthstencilbuffer ()
{
D3d11_texture2d_desc dsdesc, desttexdesc;
Id3d11texture2d * desttex;
Hresult result;
If (m_depthstencilbuffer)
{
M_depthstencilbuffer-> getdesc (& dsdesc );
// Make the object and source description consistent
Memcpy (& desttexdesc, & dsdesc, sizeof (desttexdesc ));
Desttexdesc. Usage = d3d11_usage_staging;
Desttexdesc. bindflags = 0;
Desttexdesc. cpuaccessflags = d3d11_cpu_access_read;
Result = m_device-> createtexture2d (& desttexdesc, 0, & desttex );
If (failed (result ))
{
HR (result );
Return false;
}
// Depthbufferdesc. format = dxgi_format_d24_unorm_s8_uint;
M_devicecontext-> copyresource (desttex, m_depthstencilbuffer );
D3d11_mapped_subresource mappedresource;
Result = m_devicecontext-> map (desttex, 0, d3d11_map_read, 0, & mappedresource );
If (failed (result ))
{
Return false;
}
File * fp = fopen ("depth-stencil.csv", "W ");
Const uint width = desttexdesc. width;
Const uint Height = desttexdesc. height;
// Ding to a 32-bit DWORD
DWORD * ptexels = (DWORD *) mappedresource. pdata;
For (uint ROW = 0; row {
Uint rowstart = row * mappedresource. rowpitch/sizeof (ptexels [0]);
For (uint Col = 0; Col <width; Col ++)
{
Fprintf (FP, "% 08x,", ptexels [rowstart + Col]);
}
Fprintf (FP, "\ n ");
}
Fclose (FP );
M_devicecontext-> unmap (desttex, 0 );
}
Return true;
}
In the render function of the graphicclass class, call this function to obtain the depth/stencel buffer content.
This article provides a reference to the blogat http://www.cppblog.com/gameacademe/articles/directx11.html.