fix(d3d12): 修复 Surface 重复释放问题,完善容器文档
- 改用 utl::free_list 管理 surface,避免 vector 扩容导致的资源重复释放 - 为 d3d12_surface 添加移动语义,禁用拷贝构造 - 添加撕裂检测支持(DXGI_PRESENT_ALLOW_TEARING) - 为 FreeList.h 和 Vector.h 添加完整的 Doxygen 中文注释 - 更新 D3D12 学习 Wiki,添加 free_list 章节
This commit is contained in:
@@ -604,6 +604,75 @@ D3D12_VIEWPORT viewport{
|
||||
D3D12_RECT scissor_rect{0, 0, width, height};
|
||||
```
|
||||
|
||||
### 8.6 Surface 管理与 free_list
|
||||
|
||||
#### 问题:Vector 扩容导致的资源重复释放
|
||||
|
||||
使用 `utl::vector` 管理 surface 时,扩容会触发元素移动:
|
||||
|
||||
```
|
||||
vector 扩容流程:
|
||||
1. 分配新内存块
|
||||
2. 移动元素到新内存(默认移动是浅拷贝)
|
||||
3. 析构旧位置的元素 → 调用 release()
|
||||
4. 新位置的元素持有悬空指针 → 崩溃!
|
||||
```
|
||||
|
||||
#### 解决方案:使用 free_list
|
||||
|
||||
`utl::free_list` 是带槽位复用机制的容器:
|
||||
|
||||
```cpp
|
||||
// 定义 surface 集合类型
|
||||
using surface_collection = utl::free_list<d3d12_surface>;
|
||||
surface_collection surfaces;
|
||||
|
||||
// 创建 surface
|
||||
surface create_surface(platform::window window)
|
||||
{
|
||||
surfaces.emplace_back(window);
|
||||
surface_id id{ (u32)surfaces.size() - 1 };
|
||||
surfaces[id].create_swap_chain(...);
|
||||
return surface{id};
|
||||
}
|
||||
|
||||
// 删除 surface(槽位被回收)
|
||||
void remove_surface(surface_id id)
|
||||
{
|
||||
gfx_command.flush();
|
||||
surfaces.remove(id);
|
||||
}
|
||||
```
|
||||
|
||||
#### free_list 数据结构
|
||||
|
||||
```
|
||||
初始状态:
|
||||
_array: [ 空 | 空 | 空 | 空 ]
|
||||
_next_free_index = invalid_id
|
||||
|
||||
添加元素 A、B、C 后:
|
||||
_array: [ A | B | C | 空 ]
|
||||
_next_free_index = invalid_id
|
||||
|
||||
删除元素 B 后:
|
||||
_array: [ A | ->2 | C | 空 ] // 槽位1存储下一个空闲索引
|
||||
_next_free_index = 1
|
||||
|
||||
添加新元素 D:
|
||||
_array: [ D | ->2 | C | 空 ] // 复用槽位1
|
||||
_next_free_index = 2
|
||||
```
|
||||
|
||||
#### free_list vs vector
|
||||
|
||||
| 特性 | free_list | vector |
|
||||
|------|-----------|--------|
|
||||
| 删除复杂度 | O(1) | O(n) |
|
||||
| 索引稳定性 | 删除后可复用 | 删除后失效 |
|
||||
| 内存管理 | 槽位复用 | 可能扩容移动 |
|
||||
| 适用场景 | 资源句柄管理 | 顺序数据存储 |
|
||||
|
||||
## 9. 渲染表面与窗口
|
||||
|
||||
### 9.1 render_surface 结构
|
||||
|
||||
Reference in New Issue
Block a user