D3D12: Add bounds check to GetBackBuffer and update unit tests

- Add assert() bounds check to GetBackBuffer() to catch invalid indices
- Include <cassert> in D3D12SwapChain.cpp
- Update test_swap_chain.cpp to use reference return type
- Mark InvalidIndex test as DISABLED (assert aborts on invalid index)
- Update get-back-buffer.md documentation
This commit is contained in:
2026-03-20 18:35:00 +08:00
parent 460a2477c3
commit 26fe3cd835
3 changed files with 27 additions and 16 deletions

View File

@@ -3,22 +3,29 @@
## 函数签名
```cpp
D3D12Texture* GetBackBuffer(uint32_t index) const
D3D12Texture& GetBackBuffer(uint32_t index)
const D3D12Texture& GetBackBuffer(uint32_t index) const
```
## 中文描述
获取指定索引的后台缓冲区。
获取指定索引的后台缓冲区。返回引用而非指针,避免空指针检查。
## 参数
| 参数 | 类型 | 描述 |
|------|------|------|
| `index` | `uint32_t` | 缓冲区索引 |
| `index` | `uint32_t` | 缓冲区索引(必须在有效范围内) |
## 返回值
`D3D12Texture*` - 后台缓冲区纹理指针
`D3D12Texture&` - 后台缓冲区纹理引用
## 注意事项
- 返回引用而非指针,调用者无需检查空值
- 如果索引越界,会触发 assert 断言失败
- 返回的引用在 SwapChain 存活期间有效SwapChain 销毁后引用失效
## 复杂度
@@ -27,7 +34,12 @@ O(1)
## 示例
```cpp
D3D12Texture* buffer = swapChain->GetBackBuffer(0);
// 获取后台缓冲区(返回引用)
D3D12Texture& buffer = swapChain.GetBackBuffer(0);
// 使用封装接口
gCommandList.TransitionBarrier(buffer.GetResource(),
ResourceStates::Present, ResourceStates::RenderTarget);
```
## 相关文档