Graphics Module: - Add platform abstraction layer (GraphicsPlatformInterface) - Add unified renderer entry point (Renderer) - Add D3D12 backend implementation (D3D12Core, D3D12Interface) - Add TestRenderer for multi-window rendering tests Documentation: - Add Graphics渲染架构分析.md - Add D3D12学习Wiki.md - Add changelogs directory with per-commit documentation - Add 20260326-dx12-initial.md for initial framework - Add 20260326-d3d12-foundation.md for Graphics module Fixes: - Resolve header include issues and type redefinition errors
183 lines
5.8 KiB
Markdown
183 lines
5.8 KiB
Markdown
# Graphics 渲染架构分析
|
||
|
||
## 1. 模块概述
|
||
|
||
Graphics 模块是引擎的图形渲染核心,采用**平台抽象层设计**,支持多图形后端(Direct3D12、Vulkan、OpenGL)的统一接口调用。
|
||
|
||
## 2. 目录结构
|
||
|
||
```
|
||
Engine/Graphics/
|
||
├── Direct3D12/ # D3D12 后端实现
|
||
│ ├── D3D12CommonHeader.h # D3D12 公共头文件
|
||
│ ├── D3D12Core.h/.cpp # D3D12 核心初始化/关闭
|
||
│ └── D3D12Interface.h/.cpp # 平台接口绑定
|
||
├── GraphicsPlatformInterface.h # 平台接口抽象定义
|
||
└── Renderer.h/.cpp # 统一渲染器入口
|
||
```
|
||
|
||
## 3. 核心设计模式
|
||
|
||
### 3.1 平台抽象接口
|
||
|
||
[GraphicsPlatformInterface.h](file:///d:/AllWX/AllC/FeatureExtractDemo/Engine/Graphics/GraphicsPlatformInterface.h) 定义了平台无关的接口结构:
|
||
|
||
```cpp
|
||
struct platform_interface {
|
||
bool(*initialize)(void);
|
||
void(*shutdown)(void);
|
||
};
|
||
```
|
||
|
||
这种设计允许:
|
||
- 运行时选择图形后端
|
||
- 各平台独立实现,互不干扰
|
||
- 上层代码无需关心底层 API 细节
|
||
|
||
### 3.2 渲染器入口
|
||
|
||
[Renderer.h](file:///d:/AllWX/AllC/FeatureExtractDemo/Engine/Graphics/Renderer.h) 提供统一的渲染器入口:
|
||
|
||
```cpp
|
||
enum class graphics_platform : u32 {
|
||
direct3d12 = 0,
|
||
vulkan = 1,
|
||
opengl = 2,
|
||
};
|
||
|
||
bool initialize(graphics_platform platform);
|
||
void shutdown();
|
||
```
|
||
|
||
### 3.3 平台接口绑定流程
|
||
|
||
[Renderer.cpp](file:///d:/AllWX/AllC/FeatureExtractDemo/Engine/Graphics/Renderer.cpp) 实现平台选择逻辑:
|
||
|
||
```
|
||
用户调用 graphics::initialize(direct3d12)
|
||
↓
|
||
set_platform_interface() 根据枚举选择平台
|
||
↓
|
||
调用 d3d12::get_platform_interface(gfx)
|
||
↓
|
||
绑定 gfx.initialize/shutdown 到 D3D12 实现
|
||
↓
|
||
执行实际初始化
|
||
```
|
||
|
||
## 4. Direct3D12 后端实现
|
||
|
||
### 4.1 公共头文件
|
||
|
||
[D3D12CommonHeader.h](file:///d:/AllWX/AllC/FeatureExtractDemo/Engine/Graphics/Direct3D12/D3D12CommonHeader.h) 引入必要的 D3D12 依赖:
|
||
|
||
| 头文件 | 用途 |
|
||
|--------|------|
|
||
| `<dxgi_6.h>` | DXGI 6.0 接口,枚举 GPU、管理交换链 |
|
||
| `<d3d12.h>` | Direct3D 12 API 核心 |
|
||
| `<wrl.h>` | COM 智能指针(ComPtr) |
|
||
|
||
### 4.2 核心模块
|
||
|
||
[D3D12Core.cpp](file:///d:/AllWX/AllC/FeatureExtractDemo/Engine/Graphics/Direct3D12/D3D12Core.cpp) 管理 D3D12 设备:
|
||
|
||
```cpp
|
||
namespace {
|
||
ID3D12Device8* main_device; // 主设备指针
|
||
}
|
||
|
||
bool initialize() {
|
||
// TODO: 确定适配器并创建设备
|
||
return true;
|
||
}
|
||
|
||
void shutdown() {
|
||
// TODO: 释放设备资源
|
||
}
|
||
```
|
||
|
||
### 4.3 接口绑定
|
||
|
||
[D3D12Interface.cpp](file:///d:/AllWX/AllC/FeatureExtractDemo/Engine/Graphics/Direct3D12/D3D12Interface.cpp) 将 D3D12 实现绑定到平台接口:
|
||
|
||
```cpp
|
||
void get_platform_interface(platform_interface& pi) {
|
||
pi.initialize = core::initialize;
|
||
pi.shutdown = core::shutdown;
|
||
}
|
||
```
|
||
|
||
## 5. 渲染表面(Render Surface)
|
||
|
||
### 5.1 数据结构
|
||
|
||
```cpp
|
||
class surface {}; // 渲染表面抽象
|
||
|
||
struct render_surface {
|
||
platform::window window{}; // 关联窗口
|
||
surface surface{}; // 渲染表面
|
||
};
|
||
```
|
||
|
||
### 5.2 多窗口支持
|
||
|
||
TestRenderer 测试展示了多窗口渲染场景:
|
||
- 支持 4 个独立渲染表面
|
||
- 每个表面关联独立的平台窗口
|
||
- 全屏切换支持(Alt+Enter)
|
||
|
||
## 6. 与其他模块的关系
|
||
|
||
```
|
||
┌─────────────────────────────────────────────────┐
|
||
│ EngineTest │
|
||
│ (TestRenderer 测试) │
|
||
└─────────────────────┬───────────────────────────┘
|
||
│ 调用
|
||
▼
|
||
┌─────────────────────────────────────────────────┐
|
||
│ Graphics/Renderer │
|
||
│ (统一渲染器入口 + 平台选择) │
|
||
└─────────────────────┬───────────────────────────┘
|
||
│ 分发
|
||
┌─────────────┼─────────────┐
|
||
▼ ▼ ▼
|
||
┌─────────┐ ┌─────────┐ ┌─────────┐
|
||
│ D3D12 │ │ Vulkan │ │ OpenGL │
|
||
│ Backend │ │ Backend │ │ Backend │
|
||
└─────────┘ └─────────┘ └─────────┘
|
||
│
|
||
▼
|
||
┌─────────────────────────────────────────────────┐
|
||
│ Platform/Window │
|
||
│ (窗口管理 + 平台抽象) │
|
||
└─────────────────────────────────────────────────┘
|
||
```
|
||
|
||
## 7. 扩展指南
|
||
|
||
### 7.1 添加新图形后端
|
||
|
||
1. 创建 `Engine/Graphics/Vulkan/` 或 `Engine/Graphics/OpenGL/` 目录
|
||
2. 实现 `XxxCore.h/.cpp`(初始化/关闭逻辑)
|
||
3. 实现 `XxxInterface.cpp`(`get_platform_interface` 函数)
|
||
4. 在 `Renderer.cpp` 的 `set_platform_interface` 中添加分支
|
||
|
||
### 7.2 D3D12 后续开发重点
|
||
|
||
- 适配器枚举与选择
|
||
- 命令队列创建
|
||
- 交换链管理
|
||
- 描述符堆管理
|
||
- 资源创建与管理
|
||
- 渲染管线状态对象(PSO)
|
||
- 根签名(Root Signature)
|
||
|
||
## 8. 设计优势
|
||
|
||
1. **平台无关性**:上层代码无需关心底层图形 API
|
||
2. **可扩展性**:易于添加新的图形后端
|
||
3. **模块化**:各平台实现完全隔离
|
||
4. **测试友好**:可通过 TestRenderer 进行独立测试
|