# D3D12CommandAllocator DirectX 12 命令分配器的 D3D12 实现,封装了 `ID3D12CommandAllocator`。 ## 头文件 ```cpp #include ``` ## 公共成员函数 ### 构造函数与析构函数 #### `D3D12CommandAllocator()` 默认构造函数。 #### `~D3D12CommandAllocator()` 析构函数,确保调用 `Shutdown()`。 ### 初始化 #### `bool Initialize(ID3D12Device* device, CommandQueueType type = CommandQueueType::Direct)` 初始化命令分配器。 - `device`: D3D12 设备 - `type`: 命令类型,必须与命令列表类型匹配 - 返回: 初始化是否成功 #### `void Shutdown()` 释放命令分配器。 ### 操作 #### `void Reset()` 重置命令分配器,丢弃所有已记录的指令。 #### `bool IsReady() const` 检查分配器是否准备就绪(GPU 不再使用)。 ### 属性 #### `ID3D12CommandAllocator* GetCommandAllocator() const` 获取底层 `ID3D12CommandAllocator` 指针。 ## 内部成员 | 成员 | 类型 | 描述 | |------|------|------| | `m_commandAllocator` | `ComPtr` | D3D12 命令分配器 | | `m_type` | `CommandQueueType` | 命令类型 | ## 使用示例 ```cpp // Create allocator D3D12CommandAllocator allocator; allocator.Initialize(device->GetDevice(), CommandQueueType::Direct); // Create command list with allocator D3D12CommandList cmdList; cmdList.Initialize(device->GetDevice(), CommandQueueType::Direct, allocator.GetCommandAllocator()); // Use command list... cmdList->Close(); cmdQueue->ExecuteCommandListsInternal(1, &cmdList); // Wait for GPU to finish cmdQueue->WaitForIdle(); // Reset allocator for next frame allocator.Reset(); cmdList->Reset(allocator.GetCommandAllocator(), nullptr); ``` ## 备注 - 命令分配器必须在 GPU 完成所有关联命令后才能重置 - 每个帧通常有独立的命令分配器以支持帧间并行 - 命令分配器创建开销小,可以频繁创建销毁