ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Direct3D 11 Resources
    CS/게임 프로그래밍 2024. 2. 2. 22:50

    Resources Overview

    Systematic Structure of Resource Class

     

    Resource Creation

    • ID3D11Device interface generates all the memory resources.
    • Parameters of the resource generation method
      1. Resource description
        • A structure that specifies all options for resource generation.
        • Used to define the desired characteristics of the generated resource.
      2. A pointer for D3D11_SUBRESOURCE_DATA structure
        • Provide initial data to be loaded on resources.
      3. A double pointer indicating a resource interface that matches the resource type
        • A pointer pointing to the resource is set in this parameter if resource generation is successful.
    • Usage specification : determine whether CPU and GPU ca read / write resources.
      1. IMMUTABLE
        • Never modify the generated resources.
        • Used to supply data to GPU.
        • Ex. static constant, vertex buffer, index buffer, ...
      2. DEFAULT
        • GPU can access resources as quickly as possible.
        • Ex. render target texture, stream output vertex buffer, ...
      3. DYNAMIC
        • CPU produces the contents of resources and GPU consumes.
        • Data flows from CPU to GPU in one direction.
        • Ex. A constant buffer for supplying rendering data that may be changed for each frame to the programmable shader stage, ...
      4. STAGING
        • Used for interim calculation in special cases other than the above three.
    enum D3D11_USAGE {
        D3D11_USAGE_DEFAULT,
        D3D11_USAGE_IMMUTABLE,
        D3D11_USAGE_DYNAMIC,
        D3D11_USAGE_STAGING
    }

    Table of the usage specification

    • CPU access flag : specify the CPU's approach to resources.
    enum D3D11_CPU_ACCESS_FLAG {
        D3D11_CPU_ACCESS_WRITE,
        D3D11_CPU_ACCESS_READ
    }
    • Bind flag : indicate where resources can be binded to the pipeline.
    enum D3D11_BIND_FLAG {
        D3D11_BIND_VERTEX_BUFFER,
        D3D11_BIND_INDEX_BUFFER,
        D3D11_BIND_CONSTANT_BUFFER,
        D3D11_BIND_STREAM_OUTPUT
        D3D11_BIND_SHADER_RESOURCE,
        D3D11_BIND_RENDER_TARGET,
        D3D11_BIND_DEPTH_STENCIL,
        D3D11_BIND_UNORDERED_ACCESS
    }

     

    Resource View

    • Resouces that can be connected directly (without resource view) to the pipeline
      • vertex buffer, index buffer, constant buffer, stream output buffer
    • Resource view : an adaptor object that connects resouces to the pipeline.
    • Types of resource view
      1. Render Target View, RTV
        • Connect the texture resource to be output from the rendering pipeline.
        • Can be connected to output merger stage.
      2. Depth Stencil View, DSV
        • Store the depth and stencil values used in the depth and stencil test.
        • Can be connected to output merger state.
      3. Shader Resource View, SRV
        • Make it possible to read resouces in the shader / write is impossible.
        • Can be connected to all programmable shader stages.
      4. Unordered Access View, UAV
        • Make it possible to read or write resources in the shader.
        • "Scatter" operation because the output location is not predetermined.
        • Can be connected to pixel shader stage and compute shader stage.
    • Resource view creation
      • Parameters of the resource view generating method
        1. A pointer indicating the resource to which the resource view is to be applied.
        2. A pointer that refers to a structure that describes a resource view.
        3. A pointer to resource view objects of the type.
    // create shader resource view
    ID3D11ShaderResourceView* CreateShaderResourceView(ID3D11Resource* pResource, D3D11_SHADER_RESOURCE_VIEW_DESC* pDesc) 
    {
        ID3D11ShaderResourceView* pView = 0;
        HRESULT hr = m_pDevice->CreateShaderResourceView(pResource, pDesc, &pView);
        return pView;
    }

     

    Resources in Detail

    Buffer Resource

    • Vertex buffer
      • An array of vertex structure.
      • Available forms and formats : position, normal vector, texture coordinate, ...
      • Multiple buffers
        1. Vertex data can be divided into multiple buffers.
        2. Reduce the data transmission bandwidth required for rendering operation.
      • Instanced rendering
        1. Consists of per-vertex and per-instance information.
        2. One draw method call render multiple objects, reducing CPU burden.
      • Bind flag : D3D11_BIND_VERTEX_BUFFER
      • Binding stage : input assembler / stream output

    Available structure of the vertex buffer

    • Index buffer
      • Store the index list indicating the elements in the vertex list.
      • Significantly reduce the total number of vertices to be defined; shared vertices.
      • Bind flag : D3D11_BIND_INDEX_BUFFER
      • Binding stage : input assembler

    Example of the index buffer

    • Constant buffer
      • A resource that can be accessed within the HLSL code.
      • The value has not changed since the draw method was called.
      • Define the structure and use it.
      • Buffer size must be a multiple of 16 bytes to efficiently process buffers with the 4-tuple register formats of the GPU.
      • Bind flag is only available for D3D11_BIND_CONSTANT_BUFFER.
      • Binding stage : all programmable shader stage
    • Standard / structured buffer
      • Provide a large amount of structured data.
      • Need to use the resource view for the pipeline to bind.
      • Shader resource view can be binded to multiple points at the same time.
      • Unordered access view can be binded to only one points at the same time.
        1. Able to not only read the buffer but also write it.
        2. Can be used as a means of communication between various stage of the pipeline
      • Bind flag : D3D11_BIND_SHADER_RESOURCE or D3D11_BIND_UNORDERED_ACCESS
      • Binding stage 
        1. Shader resource view : all programmable shader stage
        2. Unordered access view : pixel shader stage, compute shader stage

    Example of the standard / structured buffer

    • Append / consume buffer
      • Special deformation of structured buffer that must be binded to the pipeline through an unordered access view.
      • Provide a new method to use the buffer as if it were a stack within the HLSL.
      • append() : push method / consume() : pull method

    Example of the append / consume buffer

    • Byte address buffer
      • Provide a memory block to manipulate in a lower level manner.
      • Use byte offsets to directly access the desired data.

    Example of the byte address buffer

    • Indirect argument buffer
      • Used to provide parameters for pipeline execution functions through resources rather than directly designated by the host program.
      • The position offset in the buffer must be a value aligned at a 4-byte boundary.
      • Pipeline execution functions that can use indirect argument buffer.
    DrawInstancedIndirect(ID3D11Buffer *pBufferForArgs, UINT AlignedByteOffestForArgs);
    DrawIndexedInstancedIndirect(ID3D11Buffer *pBufferForArgs, UINT AlignedByteOffestForArgs);
    DispatchIndirect(ID3D11Buffer *pBufferForArgs, UINT AlignedByteOffestForArgs);
    // pBufferForArgs : a pointer to a buffer with parameter values
    // AlignedByteOffestForArgs : offset in the buffer

     

    Texture Resource

    • UVW coordinate : mapping the sizes of the texture in the $X$, $Y$, and $Z$ in the ranges of $[0, 1]$ of $U$, $V$, and $W$

    Example of the UVW coordinate

    • Mip-map
      • Low resolution version of texture resources.
      • The size of the effective resources actually used for rendering decreases, decreasing texture cache thrasing problem significantly.

    Example of the mip-map

    • 1D texture
      • The usage of 1D texture
        1. A lookup table
        2. A scale of the color visualization
        3. The resouce for general calculation of compute shader
      • Resouce view requirements : a resource view is essential to bind resources to the pipeline.

    Example of the 1D texture

    // Shader resource view
    struct D3D11_TEX1D_SRV {
    // The level of mip-map at the beginning of the mip-map range
        UINT MostDetailedMip; 
    // The number of mip-map levels
        UINT MipLevels;
    }
    struct D3D11_TEX1D_ARRAY_SRV {
        UINT MostDetailedMip;
        UINT MipLevels;
    // The first slice of the texture slice range to be included in the resource view
        UINT FirstArraySlice;
    // The number of slices
        UINT ArraySize;
    }
    
    // Unordered access view : provide one single level of mip-map to the pipeline
    struct D3D11_TEX1D_UAV {
    // A level of mip-map to be provided to the pipeline
        UINT MipSlice;
    }
    struct D3D11_TEX1D_ARRAY_UAV {
        UINT MipSlice;
        UINT FirstArraySlice;
        UINT ArraySize;
    }
    
    // Render target view
    struct D3D11_TEX1D_RTV {
        UINT MipSlice;
    }
    struct D3D11_TEX1D_ARRAY_RTV {
        UINT MipSlice;
        UINT FirstArraySlice;
        UINT ArraySize;
    }
    
    // Depth stencil view
    struct D3D11_TEX1D_DSV {
        UINT MipSlice;
    }
    struct D3D11_TEX1D_ARRAY_DSV {
        UINT MipSlice;
        UINT FirstArraySlice;
        UINT ArraySize;
    }
    • 2D texture
      • Support multisample texture for MSAA (Multisample Anti-aliasing)
      • The usage of 2D texture
        1. Render target, depth-stencil target
        2. Used to apply surface attributes to the geometry to be rendered.
      • Resource view requirements : a resource view is essential to bind resources to the pipeline.

    Example of the 2D texture

    // Shader resource view
    struct D3D11_TEX2D_SRV {
        UINT MostDetailedMip;
        UINT MipLevels;
    }
    struct D3D11_TEX2D_ARRAY_SRV {
        UINT MostDetailedMip;
        UINT MipLevels;
        UINT FirstArraySlice;
        UINT ArraySize;
    }
    struct D3D11_TEX2DMS_SRV {
    // Since a multisampled 2D texture contains a single subresource, there is actually nothing to specify
    // UnusedField_NothingToDefine is included so that this structure will compile in C
        UINT UnusedField_NothingToDefine;
    }
    struct D3D11_TEX2DMS_ARRAY_SRV {
        UINT FirstArraySlice;
        UINT ArraySize;
    }
    struct D3D11_TEXCUBE_SRV {
        UINT MostDetailedMip;
        UINT MipLevels;
    }
    struct D3D11_TEXCUBE_ARRAY_SRV {
        UINT MostDetailedMip;
        UINT MipLevels;
    // Specify the first texture element to be used
        UINT First2DArrayFace;
    // The number of cube maps; must be multiplication of 6
        UINT NumCubes;
    }
    
    // Unordered access view
    struct D3D11_TEX2D_UAV {
        UINT MipSlice;
    }
    struct D3D11_TEX2D_ARRAY_UAV {
        UINT MipSlice;
        UINT FirstArraySlice;
        UINT ArraySize;
    }
    
    // Render target view
    struct D3D11_TEX2D_RTV {
        UINT MipSlice;
    }
    struct D3D11_TEX2D_ARRAY_RTV {
        UINT MipSlice;
        UINT FirstArraySlice;
        UINT ArraySize;
    }
    struct D3D11_TEX2DMS_RTV {
        UINT UnusedField_NothingToDefine;
    }
    struct D3D11_TEX2DMS_ARRAY_RTV {
        UINT FirstArraySlice;
        UINT ArraySize;
    }
    
    // Depth stencil view
    struct D3D11_TEX2D_DSV {
        UINT MipSlice;
    }
    struct D3D11_TEX2D_ARRAY_DSV {
        UINT MipSlice;
        UINT FirstArraySlice;
        UINT ArraySize;
    }
    • 3D texture
      • Not support texture array and multisample texture.
      • The usage of 3D texture
        1. Used only at low resolution or only when complete 3D representation is essential
        2. Voxel structure
        3. Discharged light
      • Resource view requirements : a resource view is essential to bind resources to the pipeline and do not support the use of depth stencil view.

    The example of the 3D texture

    // Shader resource view
    struct D3D11_TEX3D_SRV {
    	UINT MostDetailedMip;
    	UINT MipLevels;
    }
    
    // Unordered access view
    struct D3D11_TEX3D_UAV {
    	UINT MipSlice;
    // determine some slices (depth range) to be provided in the mip-map
    	UINT FirstWSlice;
    	UINT WSize;
    }
    
    // Render target view
    // actually treated as a 2D texture array
    struct D3D11_TEX3D_RTV {
    	UINT MipSlice;
    	UINT FirstWSlice;
    	UINT WSize;
    }
    
    // Depth stencil view 
    // Instead, 2D texture array resources should be used as depth stencil targets for 3D texture rendering

     

    Sampler Status Object

    • Contain all the settings used by the shader program to sample texture resources.
    • Filter : point sampling / linear sampling / ansiotropic sampling
    • Texture address mode
      1. Wrap : repeat the texture coordinates.
      2. Mirror : repeat adjacent images.
      3. Clamp : the pixel at the end of the texture are stretched out.
      4. Boarder : designate the color to be returned to the outer coordinates as a BorderColor field.
    • Fields to manipulate the level of mip-map to be used in the sampling process
      1. MinLOD, MaxLOD : the minimum / maximum level accessible during sampling.
      2. MipLODBias : a constant offset to be added to the mip-map level selected by the sampling hardware.
    • ComparisonFunc : the type of comparison function to be applied to the texture sample before performing the filtering operation.
    struct D3D11_SAMPLER_DESC {
        D3D11_FILTER Filter;
        D3D11_TEXTURE_ADDRESS_MODE AddressU;
        D3D11_TEXTURE_ADDRESS_MODE AddressV;
        D3D11_TEXTURE_ADDRESS_MODE AddressW;
        FLOAT MipLODBias;
        UINT MaxAnisotropy;
        D3D11_COMPARISON_FUNC ComparisonFunc;
        FLOAT BorderColor[4];
        FLOAT MinLOD;
        FLOAT MaxLOD;
    }

     

     

    Resource Manipulations

    Update Resource

    1. Resource mapping
      • A basic means of enabling the CPU to read and write the contents of buffer resources.
      • The unit of mapping is one whole subresource.
    2. Update subresources
      • Possible to update only a fraction of one subresource.
    // Resource mapping
    HRESULT Map(ID3D11Resource *pResource, UINT Subresource, D3D11_MAP MapType, UINT MapFlags, D3D11_MAPPED_SUBRESOURCE *pMappedResource);
    void Unmap(ID3D11Resource *pResource, UINT Subresource);
    
    // Update subresources
    void UpdateSubresource(
    // a pointer indicating a resource to be updated and an index of a subresource
        ID3D11Resource *pDstResource, 
        UINT DstSubresource, 
        const D3D11_BOX *pDstBox,
    // a pointer indicating a memory block containing original data to be used for renewal
        const void *pSrcData, 
    // a row and depth pitch indicating the structure of the block
        UINT SrcRowPitch, 
        UINT SrcDepthPitch
    );

     

    Copy Resource

    1. CopyResource method
      • Copy the entire content of one resource into another.
      • Use to create a second resource with a different usage pattern.
      • Cannot use immutable data, depth stencil resource, multisample resource as target resources.
    2. CopySubresourceRegion method
      • Copy only part of the resources.
      • The types of resources must be the same and the forms must be compatible.
    3. CopyStructureCount method
    void CopyResource(ID3D11Resource *pDstResource, ID3D11Resource *pSrcResource);
    void CopySubresourceRegion(ID3D11Resource *pDstResource, UINT DstSubresource, UINT DstX, UINT DstY, UINT DstZ, ID3D11Resource *pSrcResource, UINT SrcSubresource, const D3D11_BOX *pSrcBox);
    void CopyStructureCount(ID3D11Buffer *pDstBuffer, UINT DstAlignedByteOffset, ID3D11UnorderedAccessView *pSrcView);

     

    Create Resource

    1. GenerateMips method
    2. ResolveSubresource method
      • Calculate the final color of the non-multisample texture using the originally designated multisample texture resource
      • Resolve partial samples into one pixel
    void GenerateMips(ID3D11ShaderResourceView *pShaderResourceView);
    void ResolveSubresource(ID3D11Resource *pDstResource, UINT DstSubresource, ID3D11Resource *pSrcResource, UINT SrcSubresource, DXGI_FORMAT Format);
Designed by Tistory.