建立描述項
描述並顯示建立索引、頂點和常數緩衝區檢視的範例;著色器資源、轉譯目標、未排序的存取、數據流輸出和深度樣板檢視;和取樣器。 建立描述元的所有方法都是自由線程。
索引緩衝區檢視
若要建立索引緩衝區檢視,請填寫 D3D12_INDEX_BUFFER_VIEW 結構:
typedef struct D3D12_INDEX_BUFFER_VIEW
{
D3D12_GPU_VIRTUAL_ADDRESS BufferLocation;
UINT SizeInBytes;
DXGI_FORMAT Format;
} D3D12_INDEX_BUFFER_VIEW;
設定位置 (呼叫 GetGPUVirtualAddress) 和緩衝區的大小,指出D3D12_GPU_VIRTUAL_ADDRESS定義為:
typedef UINT64 D3D12_GPU_VIRTUAL_ADDRESS;
請參閱DXGI_FORMAT列舉。 通常針對索引緩衝區,可能會使用下列定義:
const DXGI_FORMAT StandardIndexFormat = DXGI_FORMAT_R32_UINT;
最後,呼叫ID3D12GraphicsCommandList::IASetIndexBuffer。
例如,
// Initialize the index buffer view.
m_indexBufferView.BufferLocation = m_indexBuffer->GetGPUVirtualAddress();
m_indexBufferView.SizeInBytes = SampleAssets::IndexDataSize;
m_indexBufferView.Format = SampleAssets::StandardIndexFormat;
頂點緩衝區檢視
若要建立頂點緩衝區檢視,請填寫 D3D12_VERTEX_BUFFER_VIEW 結構:
typedef struct D3D12_VERTEX_BUFFER_VIEW {
D3D12_GPU_VIRTUAL_ADDRESS BufferLocation;
UINT SizeInBytes;
UINT StrideInBytes;
} D3D12_VERTEX_BUFFER_VIEW;
設定位置 (呼叫 GetGPUVirtualAddress) 和緩衝區的大小,指出D3D12_GPU_VIRTUAL_ADDRESS定義為:
typedef UINT64 D3D12_GPU_VIRTUAL_ADDRESS;
步幅通常是單一頂點數據結構的大小,例如 sizeof(Vertex);
。 然後呼叫 ID3D12GraphicsCommandList::IASetVertexBuffers。
例如,
// Initialize the vertex buffer view.
m_vertexBufferView.BufferLocation = m_vertexBuffer->GetGPUVirtualAddress();
m_vertexBufferView.SizeInBytes = SampleAssets::VertexDataSize;
m_vertexBufferView.StrideInBytes = SampleAssets::StandardVertexStride;
著色器資源檢視
若要建立著色器資源檢視,請填寫 D3D12_SHADER_RESOURCE_VIEW_DESC 結構:
typedef struct D3D12_SHADER_RESOURCE_VIEW_DESC
{
DXGI_FORMAT Format;
D3D12_SRV_DIMENSION ViewDimension;
UINT Shader4ComponentMapping;
union
{
D3D12_BUFFER_SRV Buffer;
D3D12_TEX1D_SRV Texture1D;
D3D12_TEX1D_ARRAY_SRV Texture1DArray;
D3D12_TEX2D_SRV Texture2D;
D3D12_TEX2D_ARRAY_SRV Texture2DArray;
D3D12_TEX2DMS_SRV Texture2DMS;
D3D12_TEX2DMS_ARRAY_SRV Texture2DMSArray;
D3D12_TEX3D_SRV Texture3D;
D3D12_TEXCUBE_SRV TextureCube;
D3D12_TEXCUBE_ARRAY_SRV TextureCubeArray;
} ;
} D3D12_SHADER_RESOURCE_VIEW_DESC;
ViewDimension 字段會設定為D3D12_SRV_DIMENSION列舉的值。
D3D12_SHADER_RESOURCE_VIEW_DESC所參考的列舉和結構為:
- DXGI_FORMAT
- D3D12_BUFFER_SRV
- D3D12_TEX1D_SRV
- D3D12_TEX1D_ARRAY_SRV
- D3D12_TEX2D_SRV
- D3D12_TEX2D_ARRAY_SRV
- D3D12_TEX2DMS_SRV
- D3D12_TEX2DMS_ARRAY_SRV
- D3D12_TEX3D_SRV
- D3D12_TEXCUBE_SRV
- D3D12_TEXCUBE_ARRAY_SRV
注意
在下列程式代碼範例中,float ResourceMinLODClamp 是 Tex1D/2D/3D/Cube 的 SRV 的一部分。 在 Direct3D 11 中,它是資源的 屬性;但這不符合它在硬體中實作的方式。 StructureByteStride 是緩衝區 SRV 的一部分;而在 Direct3D 11 中,它是資源的 屬性。 如果步幅不是零,則表示結構化緩衝區檢視,且格式必須設定為 DXGI_FORMAT_UNKNOWN。
最後,若要建立著色器資源檢視,請呼叫ID3D12Device::CreateShaderResourceView。
例如,
// Describe and create an SRV.
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.Format = tex.Format;
srvDesc.Texture2D.MipLevels = tex.MipLevels;
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.ResourceMinLODClamp = 0.0f;
m_device->CreateShaderResourceView(m_textures[i].Get(), &srvDesc, cbvSrvHandle);
常數緩衝區檢視
若要建立常數緩衝區檢視,請填寫 D3D12_CONSTANT_BUFFER_VIEW_DESC 結構:
typedef struct D3D12_CONSTANT_BUFFER_VIEW_DESC {
D3D12_GPU_VIRTUAL_ADDRESS BufferLocation;
UINT SizeInBytes;
} D3D12_CONSTANT_BUFFER_VIEW_DESC;
然後呼叫 ID3D12Device::CreateConstantBufferView。
例如,
// Describe and create a constant buffer view.
D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc = {};
cbvDesc.BufferLocation = m_constantBuffer->GetGPUVirtualAddress();
cbvDesc.SizeInBytes = (sizeof(ConstantBuffer) + 255) & ~255; // CB size is required to be 256-byte aligned.
m_device->CreateConstantBufferView(&cbvDesc, m_cbvHeap->GetCPUDescriptorHandleForHeapStart());
取樣
若要建立取樣器,請填寫 D3D12_SAMPLER_DESC 結構:
typedef struct D3D12_SAMPLER_DESC
{
D3D12_FILTER Filter;
D3D12_TEXTURE_ADDRESS_MODE AddressU;
D3D12_TEXTURE_ADDRESS_MODE AddressV;
D3D12_TEXTURE_ADDRESS_MODE AddressW;
FLOAT MipLODBias;
UINT MaxAnisotropy;
D3D12_COMPARISON_FUNC ComparisonFunc;
FLOAT BorderColor[4]; // RGBA
FLOAT MinLOD;
FLOAT MaxLOD;
} D3D12_SAMPLER_DESC;
若要填寫此結構,請參閱下列列舉:
- D3D12_FILTER
- D3D12_FILTER_TYPE
- D3D12_FILTER_REDUCTION_TYPE
- D3D12_TEXTURE_ADDRESS_MODE
- D3D12_COMPARISON_FUNC
最後,呼叫 ID3D12Device::CreateSampler。
例如,
// Describe and create a sampler.
D3D12_SAMPLER_DESC samplerDesc = {};
samplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
samplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
samplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D12_FLOAT32_MAX;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 1;
samplerDesc.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
m_device->CreateSampler(&samplerDesc, m_samplerHeap->GetCPUDescriptorHandleForHeapStart());
未排序的存取檢視
若要建立未排序的存取檢視,請填寫 D3D12_UNORDERED_ACCESS_VIEW_DESC 結構:
typedef struct D3D12_UNORDERED_ACCESS_VIEW_DESC
{
DXGI_FORMAT Format;
D3D12_UAV_DIMENSION ViewDimension;
union
{
D3D12_BUFFER_UAV Buffer;
D3D12_TEX1D_UAV Texture1D;
D3D12_TEX1D_ARRAY_UAV Texture1DArray;
D3D12_TEX2D_UAV Texture2D;
D3D12_TEX2D_ARRAY_UAV Texture2DArray;
D3D12_TEX3D_UAV Texture3D;
};
} D3D12_UNORDERED_ACCESS_VIEW_DESC;
ViewDimension 字段設定為零或一個D3D12_BUFFER_UAV_FLAGS列舉值。
請參閱下列列舉和結構:
- DXGI_FORMAT
- D3D12_BUFFER_UAV
- D3D12_TEX1D_UAV
- D3D12_TEX1D_ARRAY_UAV
- D3D12_TEX2D_UAV
- D3D12_TEX2D_ARRAY_UAV
- D3D12_TEX3D_UAV
StructureByteStride 是緩衝區 UAV 的一部分;而在 Direct3D 11 中,它是資源的 屬性。 如果步幅不是零,則表示結構化緩衝區檢視,且格式必須設定為 DXGI_FORMAT_UNKNOWN。
最後呼叫 ID3D12Device::CreateUnorderedAccessView。
例如,
// Create the unordered access views (UAVs) that store the results of the compute work.
CD3DX12_CPU_DESCRIPTOR_HANDLE processedCommandsHandle(m_cbvSrvUavHeap->GetCPUDescriptorHandleForHeapStart(), ProcessedCommandsOffset, m_cbvSrvUavDescriptorSize);
for (UINT frame = 0; frame < FrameCount; frame++)
{
// Allocate a buffer large enough to hold all of the indirect commands
// for a single frame as well as a UAV counter.
commandBufferDesc = CD3DX12_RESOURCE_DESC::Buffer(CommandBufferSizePerFrame + sizeof(UINT), D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
ThrowIfFailed(m_device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
D3D12_HEAP_FLAG_NONE,
&commandBufferDesc,
D3D12_RESOURCE_STATE_COPY_DEST,
nullptr,
IID_PPV_ARGS(&m_processedCommandBuffers[frame])));
D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
uavDesc.Format = DXGI_FORMAT_UNKNOWN;
uavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
uavDesc.Buffer.FirstElement = 0;
uavDesc.Buffer.NumElements = TriangleCount;
uavDesc.Buffer.StructureByteStride = sizeof(IndirectCommand);
uavDesc.Buffer.CounterOffsetInBytes = CommandBufferSizePerFrame;
uavDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_NONE;
m_device->CreateUnorderedAccessView(
m_processedCommandBuffers[frame].Get(),
m_processedCommandBuffers[frame].Get(),
&uavDesc,
processedCommandsHandle);
processedCommandsHandle.Offset(CbvSrvUavDescriptorCountPerFrame, m_cbvSrvUavDescriptorSize);
}
// Allocate a buffer that can be used to reset the UAV counters and initialize it to 0.
ThrowIfFailed(m_device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
D3D12_HEAP_FLAG_NONE,
&CD3DX12_RESOURCE_DESC::Buffer(sizeof(UINT)),
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_PPV_ARGS(&m_processedCommandBufferCounterReset)));
UINT8* pMappedCounterReset = nullptr;
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_processedCommandBufferCounterReset->Map(0, &readRange, reinterpret_cast<void**>(&pMappedCounterReset)));
ZeroMemory(pMappedCounterReset, sizeof(UINT));
m_processedCommandBufferCounterReset->Unmap(0, nullptr);
串流輸出檢視
若要建立數據流輸出檢視,請填寫 D3D12_STREAM_OUTPUT_DESC 結構。
typedef struct D3D12_STREAM_OUTPUT_DESC
{
_Field_size_full_(NumEntries) const D3D12_SO_DECLARATION_ENTRY *pSODeclaration;
UINT NumEntries;
_Field_size_full_(NumStrides) const UINT *pBufferStrides;
UINT NumStrides;
UINT RasterizedStream;
} D3D12_STREAM_OUTPUT_DESC;
然後呼叫 ID3D12GraphicsCommandList::SOSetTargets。
轉譯目標檢視
若要建立轉譯目標檢視,請填寫 D3D12_RENDER_TARGET_VIEW_DESC 結構。
typedef struct D3D12_RENDER_TARGET_VIEW_DESC
{
DXGI_FORMAT Format;
D3D12_RTV_DIMENSION ViewDimension;
union
{
D3D12_BUFFER_RTV Buffer;
D3D12_TEX1D_RTV Texture1D;
D3D12_TEX1D_ARRAY_RTV Texture1DArray;
D3D12_TEX2D_RTV Texture2D;
D3D12_TEX2D_ARRAY_RTV Texture2DArray;
D3D12_TEX2DMS_RTV Texture2DMS;
D3D12_TEX2DMS_ARRAY_RTV Texture2DMSArray;
D3D12_TEX3D_RTV Texture3D;
};
} D3D12_RENDER_TARGET_VIEW_DESC;
ViewDimension 字段設定為零,或D3D12_RTV_DIMENSION列舉的一個值。
請參閱下列列舉和結構:
- DXGI_FORMAT
- D3D12_BUFFER_RTV
- D3D12_TEX1D_RTV
- D3D12_TEX1D_ARRAY_RTV
- D3D12_TEX2D_RTV
- D3D12_TEX2DMS_RTV
- D3D12_TEX2D_ARRAY_RTV
- D3D12_TEX2DMS_ARRAY_RTV
- D3D12_TEX3D_RTV
最後,呼叫 ID3D12Device::CreateRenderTargetView。
例如,
// Create descriptor heaps.
{
// Describe and create a render target view (RTV) descriptor heap.
D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
rtvHeapDesc.NumDescriptors = FrameCount;
rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
ThrowIfFailed(m_device->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&m_rtvHeap)));
m_rtvDescriptorSize = m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
}
// Create frame resources.
{
CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart());
// Create a RTV for each frame.
for (UINT n = 0; n < FrameCount; n++)
{
ThrowIfFailed(m_swapChain->GetBuffer(n, IID_PPV_ARGS(&m_renderTargets[n])));
m_device->CreateRenderTargetView(m_renderTargets[n].Get(), nullptr, rtvHandle);
rtvHandle.Offset(1, m_rtvDescriptorSize);
}
深度樣板檢視
若要建立深度樣板檢視,請填寫 D3D12_DEPTH_STENCIL_VIEW_DESC 結構:
typedef struct D3D12_DEPTH_STENCIL_VIEW_DESC
{
DXGI_FORMAT Format;
D3D12_DSV_DIMENSION ViewDimension;
D3D12_DSV_FLAGS Flags;
union
{
D3D12_TEX1D_DSV Texture1D;
D3D12_TEX1D_ARRAY_DSV Texture1DArray;
D3D12_TEX2D_DSV Texture2D;
D3D12_TEX2D_ARRAY_DSV Texture2DArray;
D3D12_TEX2DMS_DSV Texture2DMS;
D3D12_TEX2DMS_ARRAY_DSV Texture2DMSArray;
} ;
} D3D12_DEPTH_STENCIL_VIEW_DESC;
ViewDimension 字段設定為零或一個D3D12_DSV_DIMENSION列舉值。 請參閱旗標設定D3D12_DSV_FLAGS列舉。
請參閱下列列舉和結構:
- DXGI_FORMAT
- D3D12_TEX1D_DSV
- D3D12_TEX1D_ARRAY_DSV
- D3D12_TEX2D_DSV
- D3D12_TEX2D_ARRAY_DSV
- D3D12_TEX2DMS_DSV
- D3D12_TEX2DMS_ARRAY_DSV
最後,呼叫 ID3D12Device::CreateDepthStencilView。
例如,
// Create the depth stencil view.
{
D3D12_DEPTH_STENCIL_VIEW_DESC depthStencilDesc = {};
depthStencilDesc.Format = DXGI_FORMAT_D32_FLOAT;
depthStencilDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
depthStencilDesc.Flags = D3D12_DSV_FLAG_NONE;
D3D12_CLEAR_VALUE depthOptimizedClearValue = {};
depthOptimizedClearValue.Format = DXGI_FORMAT_D32_FLOAT;
depthOptimizedClearValue.DepthStencil.Depth = 1.0f;
depthOptimizedClearValue.DepthStencil.Stencil = 0;
ThrowIfFailed(m_device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
D3D12_HEAP_FLAG_NONE,
&CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_D32_FLOAT, m_width, m_height, 1, 0, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
D3D12_RESOURCE_STATE_DEPTH_WRITE,
&depthOptimizedClearValue,
IID_PPV_ARGS(&m_depthStencil)
));
m_device->CreateDepthStencilView(m_depthStencil.Get(), &depthStencilDesc, m_dsvHeap->GetCPUDescriptorHandleForHeapStart());
}