Directx11 tutorial (50) Output depth/stencel buffer content

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.