Files
XCEngine/docs/api/rhi/d3d12/d3d12-common.md

125 lines
3.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# D3D12Common
D3D12 通用辅助函数集合,提供描述符大小、屏障创建、格式支持检查等功能。
## 头文件
```cpp
#include <XCEngine/RHI/D3D12/D3D12Common.h>
```
## 命名空间
`XCEngine::RHI`
## 描述符大小函数
#### `inline UINT GetDescriptorHandleIncrementSize(ID3D12Device* device, D3D12_DESCRIPTOR_HEAP_TYPE heapType)`
获取描述符增量大小。
#### `inline UINT GetRTVDescriptorSize(ID3D12Device* device)`
获取 RTV 描述符大小。
#### `inline UINT GetDSVDescriptorSize(ID3D12Device* device)`
获取 DSV 描述符大小。
#### `inline UINT GetCBV_SRV_UAVDescriptorSize(ID3D12Device* device)`
获取 CBV/SRV/UAV 描述符大小。
#### `inline UINT GetSamplerDescriptorSize(ID3D12Device* device)`
获取 Sampler 描述符大小。
## 屏障创建函数
#### `inline D3D12_RESOURCE_BARRIER CreateTransitionBarrier(...)`
创建资源状态转换屏障。
参数:
- `resource`: 目标资源
- `stateBefore`: 转换前状态
- `stateAfter`: 转换后状态
- `subresource`: 子资源索引(默认所有)
#### `inline D3D12_RESOURCE_BARRIER CreateUAVBarrier(ID3D12Resource* resource = nullptr)`
创建 UAV 屏障。
#### `inline D3D12_RESOURCE_BARRIER CreateAliasingBarrier(...)`
创建别名化屏障。
## 格式支持检查
#### `inline bool CheckFormatSupport(ID3D12Device* device, DXGI_FORMAT format, D3D12_FORMAT_SUPPORT1 support1, D3D12_FORMAT_SUPPORT2 support2 = D3D12_FORMAT_SUPPORT2_NONE)`
检查格式支持。
#### `inline bool IsRenderTargetFormatSupported(ID3D12Device* device, DXGI_FORMAT format)`
检查是否支持作为渲染目标。
#### `inline bool IsDepthStencilFormatSupported(ID3D12Device* device, DXGI_FORMAT format)`
检查是否支持作为深度模板。
#### `inline bool IsShaderResourceFormatSupported(ID3D12Device* device, DXGI_FORMAT format)`
检查 shader 是否可以读取该格式。
#### `inline bool IsTextureFormatSupported(ID3D12Device* device, DXGI_FORMAT format)`
检查是否支持作为纹理。
## 清除值创建
#### `inline D3D12_CLEAR_VALUE CreateRenderTargetClearValue(DXGI_FORMAT format, float r = 0.0f, float g = 0.0f, float b = 0.0f, float a = 1.0f)`
创建渲染目标清除值。
#### `inline D3D12_CLEAR_VALUE CreateDepthStencilClearValue(DXGI_FORMAT format, float depth = 1.0f, uint8_t stencil = 0)`
创建深度模板清除值。
## 视口和裁剪矩形
#### `inline D3D12_VIEWPORT CreateViewport(float width, float height, float topLeftX = 0.0f, float topLeftY = 0.0f, float minDepth = 0.0f, float maxDepth = 1.0f)`
创建视口。
#### `inline D3D12_RECT CreateScissorRect(int left, int top, int right, int bottom)`
创建裁剪矩形。
## 缓冲区视图
#### `inline D3D12_VERTEX_BUFFER_VIEW CreateVertexBufferView(...)`
创建顶点缓冲区视图。
#### `inline D3D12_INDEX_BUFFER_VIEW CreateIndexBufferView(...)`
创建索引缓冲区视图。
## 描述符句柄运算
#### `inline D3D12_CPU_DESCRIPTOR_HANDLE GetCPUDescriptorHandle(...)`
计算偏移后的 CPU 描述符句柄。
#### `inline D3D12_GPU_DESCRIPTOR_HANDLE GetGPUDescriptorHandle(...)`
计算偏移后的 GPU 描述符句柄。
## 使用示例
```cpp
// Check format support
if (!IsRenderTargetFormatSupported(device, DXGI_FORMAT_R8G8B8A8_UNORM)) {
// Fall back to supported format
}
// Create barriers
D3D12_RESOURCE_BARRIER barriers[2];
barriers[0] = CreateTransitionBarrier(
resource, D3D12_RESOURCE_STATE_RENDER_TARGET,
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
barriers[1] = CreateUAVBarrier(uavResource);
// Set barriers
cmdList->ResourceBarrier(2, barriers);
// Create viewport
D3D12_VIEWPORT vp = CreateViewport(1920.0f, 1080.0f);
cmdList->RSSetViewports(1, &vp);
```
## 备注
- 所有函数为 inline可在头文件中使用
- 这些是高频使用的辅助函数,避免重复代码