"Cold Snow" open depth cache

Source: Internet
Author: User
concepts related to depth testing

Depth buffers are also often z-buffer, which is a memory buffer used to store depth information drawn to each pixel point on the screen, a surface that contains only specific pixel depth information without image data, and the depth cache retains a depth value for each pixel in the final drawn image. If we draw a screen with a resolution of 800x600 pixels, then the depth cache size is also 800x600.
Direct3D determines whether the current pixel is drawn by comparing the depth of the currently drawn pixel point and the depth value of the point corresponding to the depth buffer. If the result of the depth test is true, the current pixel is drawn and the depth buffer is updated with the depth value of the current pixel point. If the test does not pass, the result is false and the current pixel is not drawn.
use of depth buffers in Direct3D9

In Direct3D, the depth cache is turned on by default. But let's just mention the whole process of using it.
Create depth buffer open depth test set depth test function update depth buffer

To create a depth buffer
Created when the depth buffer was created at initialization time. The process of initializing the Direct3D needs to populate a d3dpresent_parameters structure, the following is the prototype of the struct

typedef struct D3DPRESENT_PARAMETERS {
  UINT                backbufferwidth;
  UINT                backbufferheight;
  D3dformat           Backbufferformat;
  UINT                BackBufferCount;
  D3dmultisample_type MultiSampleType;
  DWORD               multisamplequality;
  D3dswapeffect       SwapEffect;
  HWND                Hdevicewindow;
  BOOL                windowed;
  BOOL                EnableAutoDepthStencil;
  D3dformat           Autodepthstencilformat;
  DWORD               Flags;
  UINT                fullscreen_refreshrateinhz;
  UINT                presentationinterval;
} D3dpresent_parameters, *lpd3dpresent_parameters;
BOOL EnableAutoDepthStencil: Indicates whether the Direct3D automatically generates a deep cache for the application, which is set to true to require that the deep cache be managed automatically. At this point, you need to set the relevant pixel format for the next member Autodepthstencilformat.

D3dformat Autodepthstencilformat: This parameter needs to be set when EnableAutoDepthStencil is set to true. The value of this parameter can be taken from D3dformat. Generally we choose the following three kinds:

D3dformat_d16   //depth cache uses 16 bits to store the depth value of each pixel
d3dformat_d24x8//depth cache uses 24 bits to store the depth value of each pixel
d3dformat_d32   // Depth cache uses 32 bits to store the depth value of each pixel

The time to use the deep cache in the rendering. You only need to set EnableAutoDepthStencil to True and Autodepthstencilformat.

Open Depth Test
Just use SetRenderState to set the rendering state for this step. The calling code example is as follows:

G_pd3ddevice->setrenderstate (d3drs_zenable,true);

This is set when you need to close:

G_pd3ddevice->setrenderstate (D3drs_zenable,false);

Setting the depth test function
This step still calls SetRenderState, the first parameter is set to D3drs_zfunc, and the second parameter is set to the depth test function that you want to use. Takes a value in the D3dcmpfunc enumeration type. The enumeration type is defined as follows:

typedef enum _D3DCMPFUNC {
    D3dcmp_never                = 1,
    d3dcmp_less                 = 2,
    d3dcmp_equal                = 3,
    d3dcmp_ Lessequal            = 4,
    d3dcmp_greater              = 5,
    d3dcmp_notequal             = 6,
    d3dcmp_greaterequal         = 7,
    d3dcmp_always               = 8,
    d3dcmp_force_dword          = 0x7fffffff,/* Force 32-bit size enum */
} D3dcmpfunc ;

The following is a table that describes the members of these enumeration types

Returns True when the
description
d3dcmp_never depth test function always returns FASLE
d3dcmp_less test Point depth value is less than corresponding value in depth buffer, returns TRUE, default value
d3dcmp_equal test Point depth value equals the corresponding value in the depth buffer
d3dcmp_lessequal returns True when the test point depth value is less than or equal to the depth buffer corresponding value.
d3dcmp_greater returns True when the test point depth value is greater than the depth buffer corresponding value
d3dcmp_notequal test Point depth value is not equal to depth buffer corresponding value, return True
d3dcmp_greaterequal returns True when the test point depth value is greater than or equal to the depth buffer corresponding value
d3dcmp_always test is always true
D3dcmp_force_dword This enumeration is generally not used, and is guaranteed to compile the D3dcmpfunc enumeration type to 32 bits

In general, we set the depth test function to d3dcmp_less, which means that when the test point depth value is less than the corresponding value in the depth buffer, the relevant pixels are tested and plotted so that objects that are not obscured are displayed.
The following is an example of a call

G_pd3ddevice->setrenderstate (d3drs_zfunc,d3dcmp_less);

Update Depth Buffer
The third step sets the comparison function. To match the results of the comparison function, here you set how to update the depth cache. Or to call the SetRenderState method, the first parameter is set to D3drs_zwriteenable, and the second parameter is set to true;

G_pd3ddevice->setrenderstate (d3drs_zwriteenable,true);
Sample Program
First step. Create a deep cache
D3DPP. EnableAutoDepthStencil = true;
D3DPP. Autodepthstencilformat = D3dfmt_d16;
No. 234 step. Set depth test function, open depth test, update depth cache
g_pd3ddevice->setrenderstate (D3drs_zfunc, d3dcmp_less);
G_pd3ddevice->setrenderstate (d3drs_zenable, true);
G_pd3ddevice->setrenderstate (d3drs_zwriteenable, true);
The order of the three steps can be reversed.


Copyright©by Cold River Snow
date:2017.1.6

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.