feat: implement D3D12 device initialization with debug layer

D3D12 Core:
- Implement complete device initialization flow
- Add debug layer enablement in DEBUG mode
- Add GPU adapter enumeration and selection
- Add feature level checking (min 11.0)
- Add DXCall and NAME_D3D12_OBJECT macros
- Add release template function
- Configure info queue for error/warning breakpoints

Documentation:
- Add changelog for D3D12 device initialization
- Update D3D12 Wiki with current implementation status
- Add Doxygen comments to Renderer and platform interface
This commit is contained in:
SpecialX
2026-03-26 19:01:08 +08:00
parent b7eebc11b2
commit 3fdc774f3f
11 changed files with 425 additions and 15 deletions

View File

@@ -0,0 +1,142 @@
# 变更记录D3D12 设备初始化实现
**提交日期**: 2026-03-26
**提交哈希**: `69b7352`
**变更类型**: 功能实现
---
## 变更概述
本次提交实现了 D3D12 设备的完整初始化流程包括调试层、DXGI 工厂创建、GPU 适配器枚举与选择、特性级别检查和设备创建。
## 修改文件
### Engine/Graphics/Direct3D12/
| 文件 | 变更说明 |
|------|----------|
| `D3D12Core.cpp` | 实现完整的 D3D12 设备初始化流程 |
| `D3D12Core.h` | 添加 `release` 模板函数 |
| `D3D12CommonHeader.h` | 添加库链接、DXCall 宏、NAME_D3D12_OBJECT 宏 |
### Engine/Graphics/
| 文件 | 变更说明 |
|------|----------|
| `Renderer.h` | 添加 Doxygen 注释 |
| `Renderer.cpp` | 添加 Doxygen 注释 |
| `GraphicsPlatformInterface.h` | 添加 Doxygen 注释 |
---
## 技术要点
### 1. 调试层启用
```cpp
#ifdef _DEBUG
{
ComPtr<ID3D12Debug3> debug_interface;
DXCall(D3D12GetDebugInterface(IID_PPV_ARGS(&debug_interface)));
debug_interface->EnableDebugLayer();
dxgi_factory_flag |= DXGI_CREATE_FACTORY_DEBUG;
}
#endif
```
调试层可捕获无效 API 调用和资源泄漏。
### 2. GPU 适配器选择
```cpp
IDXGIAdapter4* determine_main_adapter()
{
for (u32 i{ 0 }; ...; ++i)
{
// 按高性能优先枚举适配器
if (SUCCEEDED(D3D12CreateDevice(adapter, minumum_feature_level, ...)))
return adapter;
}
return nullptr;
}
```
优先选择高性能 GPU并验证 D3D12 支持。
### 3. 特性级别检查
```cpp
constexpr D3D_FEATURE_LEVEL feature_levels[4]{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_12_0,
D3D_FEATURE_LEVEL_12_1,
};
```
获取适配器支持的最高特性级别,确保不低于最低要求 (11.0)。
### 4. 调试宏
```cpp
// DXCall 宏 - 检查 HRESULT 并输出错误信息
#define DXCall(x) if(FAILED(x)) { ... __debugbreak(); }
// NAME_D3D12_OBJECT 宏 - 为 D3D12 对象设置调试名称
#define NAME_D3D12_OBJECT(obj, name) obj->SetName(name);
```
### 5. 信息队列配置
```cpp
info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_CORRUPTION, true);
info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_WARNING, true);
info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_ERROR, true);
```
在调试模式下,遇到警告或错误时自动断点。
---
## 初始化流程
```
initialize()
├─► 启用调试层 (DEBUG 模式)
├─► 创建 DXGI 工厂
├─► 枚举并选择 GPU 适配器
│ └─► 按高性能优先
│ └─► 验证 D3D12 支持
├─► 获取最高特性级别
│ └─► 必须 >= 11.0
├─► 创建 D3D12 设备
└─► 配置信息队列 (DEBUG 模式)
```
---
## 依赖
- `dxgi.lib` - DXGI 库
- `d3d12.lib` - Direct3D 12 库
---
## 后续工作
- [ ] 命令队列创建
- [ ] 交换链管理
- [ ] 描述符堆
---
## 相关文档
- [D3D12学习Wiki](../wiki/D3D12学习Wiki.md)

View File

@@ -1,7 +1,7 @@
# 变更记录D3D12 基础框架
**提交日期**: 2026-03-26
**提交哈希**: `16723ba`
**提交哈希**: `b7eebc1`
**变更类型**: 新增功能
---

View File

@@ -16,6 +16,7 @@ changelogs/
| 日期 | 提交 | 变更内容 |
|------|------|----------|
| 2026-03-26 | [D3D12设备初始化](./2026-03/20260326-d3d12-device-init.md) | D3D12 设备创建与调试层实现 |
| 2026-03-26 | [Graphics模块](./2026-03/20260326-d3d12-foundation.md) | Graphics 模块与 D3D12 后端框架 |
| 2026-03-19 | [DX12初始框架](./2026-03/20260326-dx12-initial.md) | 初始 DX12 基础框架 |

View File

@@ -78,19 +78,36 @@ IDXGISwapChain // 交换链接口
### 3.2 项目当前状态
[D3D12Core.cpp](file:///d:/AllWX/AllC/FeatureExtractDemo/Engine/Graphics/Direct3D12/D3D12Core.cpp) 目前是框架代码
[D3D12Core.cpp](file:///d:/AllWX/AllC/FeatureExtractDemo/Engine/Graphics/Direct3D12/D3D12Core.cpp) 已实现完整的设备初始化
```cpp
namespace {
ID3D12Device8* main_device; // 已声明主设备
ID3D12Device8* main_device{ nullptr };
IDXGIFactory7* dxgi_factory{ nullptr };
}
bool initialize() {
// TODO: 实现适配器选择和设备创建
return true;
// 1. 启用调试层 (DEBUG 模式)
// 2. 创建 DXGI 工厂
// 3. 枚举并选择 GPU 适配器
// 4. 获取最高特性级别
// 5. 创建 D3D12 设备
// 6. 配置信息队列 (DEBUG 模式)
}
```
### 3.3 调试宏
项目定义了两个重要的调试宏:
```cpp
// DXCall - 检查 HRESULT 并断点
#define DXCall(x) if(FAILED(x)) { ... __debugbreak(); }
// NAME_D3D12_OBJECT - 为对象设置调试名称
#define NAME_D3D12_OBJECT(obj, name) obj->SetName(name);
```
## 4. COM 对象管理
### 4.1 什么是 COM
@@ -193,7 +210,7 @@ if (wparam == VK_RETURN && (HIWORD(lparam) & KF_ALTDOWN)) {
### 7.1 基础阶段
- [ ] 完成设备创建和适配器枚举
- [x] 完成设备创建和适配器枚举
- [ ] 创建命令队列和命令列表
- [ ] 实现交换链和后台缓冲区
- [ ] 渲染第一个三角形