-
Direct3D 11 ResourcesCS/게임 프로그래밍 2024. 2. 2. 22:50
Resources Overview
Resource Creation
- ID3D11Device interface generates all the memory resources.
- Parameters of the resource generation method
- Resource description
- A structure that specifies all options for resource generation.
- Used to define the desired characteristics of the generated resource.
- A pointer for D3D11_SUBRESOURCE_DATA structure
- Provide initial data to be loaded on resources.
- 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.
- Resource description
- Usage specification : determine whether CPU and GPU ca read / write resources.
- IMMUTABLE
- Never modify the generated resources.
- Used to supply data to GPU.
- Ex. static constant, vertex buffer, index buffer, ...
- DEFAULT
- GPU can access resources as quickly as possible.
- Ex. render target texture, stream output vertex buffer, ...
- 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, ...
- STAGING
- Used for interim calculation in special cases other than the above three.
- IMMUTABLE
enum D3D11_USAGE { D3D11_USAGE_DEFAULT, D3D11_USAGE_IMMUTABLE, D3D11_USAGE_DYNAMIC, D3D11_USAGE_STAGING }
- 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
- Render Target View, RTV
- Connect the texture resource to be output from the rendering pipeline.
- Can be connected to output merger stage.
- Depth Stencil View, DSV
- Store the depth and stencil values used in the depth and stencil test.
- Can be connected to output merger state.
- Shader Resource View, SRV
- Make it possible to read resouces in the shader / write is impossible.
- Can be connected to all programmable shader stages.
- 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.
- Render Target View, RTV
- Resource view creation
- Parameters of the resource view generating method
- A pointer indicating the resource to which the resource view is to be applied.
- A pointer that refers to a structure that describes a resource view.
- A pointer to resource view objects of the type.
- Parameters of the resource view generating method
// 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
- Vertex data can be divided into multiple buffers.
- Reduce the data transmission bandwidth required for rendering operation.
- Instanced rendering
- Consists of per-vertex and per-instance information.
- One draw method call render multiple objects, reducing CPU burden.
- Bind flag : D3D11_BIND_VERTEX_BUFFER
- Binding stage : input assembler / stream output
- 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
- 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.
- Able to not only read the buffer but also write it.
- 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
- Shader resource view : all programmable shader stage
- Unordered access view : pixel shader stage, compute shader stage
- 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
- Byte address buffer
- Provide a memory block to manipulate in a lower level manner.
- Use byte offsets to directly access the desired data.
- 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$
- 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.
- 1D texture
- The usage of 1D texture
- A lookup table
- A scale of the color visualization
- The resouce for general calculation of compute shader
- Resouce view requirements : a resource view is essential to bind resources to the pipeline.
- The usage of 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
- Render target, depth-stencil target
- 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.
// 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
- Used only at low resolution or only when complete 3D representation is essential
- Voxel structure
- 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.
// 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
- Wrap : repeat the texture coordinates.
- Mirror : repeat adjacent images.
- Clamp : the pixel at the end of the texture are stretched out.
- 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
- MinLOD, MaxLOD : the minimum / maximum level accessible during sampling.
- 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
- 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.
- 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
- 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.
- CopySubresourceRegion method
- Copy only part of the resources.
- The types of resources must be the same and the forms must be compatible.
- 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
- GenerateMips method
- 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);
'CS > 게임 프로그래밍' 카테고리의 다른 글
The Tessellation Pipeline (0) 2024.02.12 The Rendering Pipeline - After Tessellation (0) 2024.02.05 The Rendering Pipeline - Before Tessellation (0) 2024.02.04 The Rendering Pipeline - Background (0) 2024.02.04 Overview of Direct3D 11 (0) 2024.01.29