[Notes] D3D12 learning 4.3.8 Create the Depth / Stencil Buffer and View

We now need to create a depth / stencil buffer. As §4.1.5, just a 2D texture depth buffer to store the most recent visible object depth information (if using a template (Stencil), then the template information is also stored). Texture is a GPU resources, so we describe D3D12_RESOURCE_DESC structure by filling out the texture of resources, then call ID3D12Device :: CreateCommittedResource way to create one. D3D12_RESOURCE_DESC structure is defined as follows:

typedef struct D3D12_RESOURCE_DESC {
  D3D12_RESOURCE_DIMENSION Dimension;
  UINT64 Alignment;
  UINT64 Width;
  UINT Height;
  UINT16 DepthOrArraySize;
  UINT16 MipLevels;
  DXGI_FORMAT Format;
  DXGI_SAMPLE_DESC SampleDesc;
  D3D12_TEXTURE_LAYOUT Layout;
  D3D12_RESOURCE_MISC_FLAG MiscFlags;
} D3D12_RESOURCE_DESC;

1.Dimension: Dimensions of resources, it is one of the following enumerated type:

  enum D3D12_RESOURCE_DIMENSION
  {
    D3D12_RESOURCE_DIMENSION_UNKNOWN = 0,
    D3D12_RESOURCE_DIMENSION_BUFFER = 1,
    D3D12_RESOURCE_DIMENSION_TEXTURE1D = 2,
    D3D12_RESOURCE_DIMENSION_TEXTURE2D = 3,
    D3D12_RESOURCE_DIMENSION_TEXTURE3D = 4
  } D3D12_RESOURCE_DIMENSION;

2. the Width: width of the texture, in units of texels. For buffer resource, which is the number of bytes in the buffer.

3.Height: Texture height in units of texels.

4.DepthOrArraySize: Texture depth of the texture, the array size or texture (texture for 1D and 2D). Please note that you can not have an array of textures 3D textures.

5.MipLevels: the number of mipmap levels. Chapter 9 describes the texture related Mipmap. In order to create a depth / stencil buffer, we only need a texture mipmap level.
Format: The format of the members of DXGI_FORMAT enumerated type, specifying texels. For depth / stencil buffer, which needs to be one of the formats shown in §4.1.5.

6.SampleDesc: the number of multi-sampling and quality levels; see §4.1.7 and §4.1.8. Recall, 4X MSAA using 4 times larger than the screen resolution of the depth buffer and the buffer for storing depth and color / template information of each sub-pixel. Therefore, multiple sampling set for depth / stencil buffer must be used to render the target set matches.

7.Layout: members D3D12_TEXTURE_LAYOUT enumerated type, specifying the texture layout. Currently, we do not have to worry about the layout, you can specify D3D12_TEXTURE_LAYOUT_UNKNOWN

8.MiscFlags: miscellaneous resource flag. For depth / stencil buffer resources, specify D3D12_RESOURCE_MISC_DEPTH_STENCIL.

 

GPU resources exist in the stack, these resources are essentially GPU memory block having certain properties. ID3D12Device :: CreateCommittedResource method we specify the attributes of resources created and submitted to the memory blocks:

HRESULT ID3D12Device::CreateCommittedResource(
  const D3D12_HEAP_PROPERTIES *pHeapProperties,
  D3D12_HEAP_MISC_FLAG HeapMiscFlags,
  const D3D12_RESOURCE_DESC *pResourceDesc,
  D3D12_RESOURCE_USAGE InitialResourceState,
  const D3D12_CLEAR_VALUE *pOptimizedClearValue,
  REFIID riidResource,
  void **ppvResource);
 
typedef struct D3D12_HEAP_PROPERTIES {
 D3D12_HEAP_TYPE     Type;
 D3D12_CPU_PAGE_PROPERTIES CPUPageProperties;
 D3D12_MEMORY_POOL    MemoryPoolPreference;
 UINT CreationNodeMask;
 UINT VisibleNodeMask;
} D3D12_HEAP_PROPERTIES;

1.pHeapProperties: We want the resources committed to the properties of the heap. Some of these properties for advanced usage. At present, the main attributes we need to consider is D3D12_HEAP_TYPE, it can be one of the following members D3D12_HEAP_PROPERTIES enumerated type:
  D3D12_HEAP_TYPE_DEFAULT: default heap. This is where the resources we have submitted will only be accessed by the GPU alone. Depth / stencil buffer, for example: GPU reading and writing depth / stencil buffer. CPU never need to access it, so the depth / stencil buffer will be placed in the default heap;
  D3D12_HEAP_TYPE_UPLOAD: Upload heap. If we need to upload data from the CPU to the GPU, we need to be submitted to the resources here;
  D3D12_HEAP_TYPE_READBACK: readback heap (Read-back heap). If the resources we need to be submitted to read CPU, we will submit to the resources here;
  D3D12_HEAP_TYPE_CUSTOM: the use of advanced scenarios, see the MSDN documentation for more information.

Side note: resources should be placed in the default heap for optimal performance. Use only upload or read back when the heap in the respective function.
2.HeapMiscFlags: Other signs about the resources we have to submit to the heap. This is usually D3D12_HEAP_MISC_NONE.
3.pResourceDesc: D3D12_RESOURCE_DESC pointer points to describe instances of the resource that we want to create.
4.InitialResourceState: Review §4.2.3 has a current resource use. Use this parameter to set up a resource when you create the initial state of the resource. For depth / stencil buffer, the initial state will D3D12_RESOURCE_USAGE_INITIAL, then we will want to convert it to D3D12_RESOURCE_USAGE_DEPTH, so that it can be used as a depth / stencil buffer to bind to the pipeline.
5.pOptimizedClearValue: D3D12_CLEAR_VALUE pointer object, the object described optimized value is cleared resources. Clear and optimization of call clearing match the value of the call may be cleared faster than the optimized value does not match the clear. This value can also be specified Null, in order to optimize the clearance value is not specified.

struct D3D12_CLEAR_VALUE
{
    DXGI_FORMAT Format;
    union {
      FLOAT Color[ 4 ];
      D3D12_DEPTH_STENCIL_VALUE DepthStencil;
    };
}D3D12_CLEAR_VALUE;

6.riidResource: We want to get a pointer to the COM ID ID3D12Resource interface.
7.ppvResource: ID3D12Resource returns a pointer to a pointer, the ID3D12Resource represents the newly created resources.

 

In addition, before using the depth / stencil buffer, we must create an associated depth / stencil view to bind to the pipeline. This is similar to create render target view. The following code example shows how we create depth / stencil texture and its corresponding depth / stencil view:

// Create the depth/stencil buffer and view.
D3D12_RESOURCE_DESC depthStencilDesc;
depthStencilDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
depthStencilDesc.Alignment = 0;
depthStencilDesc.Width = mClientWidth;
depthStencilDesc.Height = mClientHeight;
depthStencilDesc.DepthOrArraySize = 1;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.Format = mDepthStencilFormat;
depthStencilDesc.SampleDesc.Count = m4xMsaaState ? 4 : 1;
depthStencilDesc.SampleDesc.Quality = m4xMsaaState ? (m4xMsaaQuality - 1) : 0;
depthStencilDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
depthStencilDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
 
D3D12_CLEAR_VALUE optClear;
optClear.Format = mDepthStencilFormat;
optClear.DepthStencil.Depth = 1.0f;
optClear.DepthStencil.Stencil = 0;
ThrowIfFailed(md3dDevice->CreateCommittedResource(
  &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
  D3D12_HEAP_FLAG_NONE,
  &depthStencilDesc,
  D3D12_RESOURCE_STATE_COMMON,
  &optClear,
  IID_PPV_ARGS(mDepthStencilBuffer.GetAddressOf())));
// Create descriptor to mip level 0 of entire resource using the
// format of the resource.
md3dDevice->CreateDepthStencilView(
  mDepthStencilBuffer.Get(),
  nullptr, 
  DepthStencilView());
 
// Transition the resource from its initial state to be used as a depth buffer.
mCommandList->ResourceBarrier(
  1, 
  &CD3DX12_RESOURCE_BARRIER::Transition(
    mDepthStencilBuffer.Get(),
    D3D12_RESOURCE_STATE_COMMON,
    D3D12_RESOURCE_STATE_DEPTH_WRITE));
Note that we use the CD3DX12_HEAP_PROPERTIES helper constructor to create the heap properties structure, which is implemented like so:
explicit CD3DX12_HEAP_PROPERTIES( 
    D3D12_HEAP_TYPE type, 
    UINT creationNodeMask = 1, 
    UINT nodeMask = 1 )
{
  Type = type;
  CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  CreationNodeMask = creationNodeMask;
  VisibleNodeMask = nodeMask;
}

CreateDepthStencilView second parameter is a pointer pointing to D3D12_DEPTH_STENCIL_VIEW_DESC. In addition, the data structure describes the type of resource elements (format). If the resource is created using a typed format (ie non-free type), then this parameter can be null, which represents a mipmap level view this resource creation (depth / stencil buffer only create a mipmap level ) use create a format resources. (Mipmap discussed in Chapter 9.) Because we specified the type of depth / stencil buffer, so we specify null for this parameter.

Guess you like

Origin www.cnblogs.com/heben/p/11324842.html