Files
XCEngine/docs/api/rhi/opengl/opengl-buffer.md

136 lines
3.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# OpenGLBuffer
OpenGL 缓冲区的实现,封装 OpenGL buffer object。
## 头文件
```cpp
#include <XCEngine/RHI/OpenGL/OpenGLBuffer.h>
```
## 继承关系
```
RHIBuffer (interface)
└── OpenGLBuffer (implementation)
```
## OpenGLBufferType 枚举
| 值 | 描述 |
|----|------|
| `Vertex` | 顶点缓冲区 |
| `Index` | 索引缓冲区 |
| `Uniform` | Uniform 缓冲区 (UBO) |
| `CopyRead` | 复制源缓冲区 |
| `CopyWrite` | 复制目标缓冲区 |
| `AtomicCounter` | 原子计数器缓冲区 |
| `DispatchIndirect` | 间接计算调用缓冲区 |
| `DrawIndirect` | 间接绘制缓冲区 |
| `ShaderBindingTable` | 光线追踪 Shader 绑定表 |
## 公共成员函数
### 构造函数与析构函数
#### `OpenGLBuffer()`
#### `~OpenGLBuffer() override`
### 初始化
#### `bool Initialize(OpenGLBufferType type, size_t size, const void* data = nullptr, bool dynamic = false)`
通用初始化。
- `type`: 缓冲区类型
- `size`: 大小(字节)
- `data`: 初始数据(可选)
- `dynamic`: 是否动态更新
#### `bool InitializeVertexBuffer(const void* data, size_t size)`
初始化顶点缓冲区。
#### `bool InitializeIndexBuffer(const void* data, size_t size)`
初始化索引缓冲区。
#### `void Shutdown() override`
### 绑定操作
#### `void Bind() const`
绑定缓冲区到当前 target。
#### `void Unbind() const`
解除绑定。
#### `void BindBase(unsigned int target, unsigned int index) const`
绑定到指定的 binding point用于 UBO、SSBO 等)。
### 数据操作
#### `void* Map() override`
映射缓冲区到 CPU。
#### `void Unmap() override`
解除映射。
#### `void SetData(const void* data, size_t size, size_t offset = 0) override`
更新缓冲区数据。
### 属性
#### `unsigned int GetID() const`
获取 OpenGL buffer ID。
#### `uint64_t GetSize() const override`
获取缓冲区大小。
#### `OpenGLBufferType GetType() const`
获取缓冲区类型。
#### `bool IsDynamic() const`
是否动态缓冲区。
#### `BufferType GetBufferType() const override / void SetBufferType(BufferType type) override`
#### `uint32_t GetStride() const override / void SetStride(uint32_t stride) override`
#### `void* GetNativeHandle() override`
返回 `reinterpret_cast<void*>(static_cast<uintptr_t>(m_buffer))`
#### `ResourceStates GetState() const override`
返回 `ResourceStates::Common`OpenGL 无显式状态)
#### `const std::string& GetName() const override / void SetName(...) override`
## 内部成员
| 成员 | 类型 | 描述 |
|------|------|------|
| `m_buffer` | `unsigned int` | GL buffer ID |
| `m_size` | `size_t` | 大小 |
| `m_isIndexBuffer` | `bool` | 是否索引缓冲区 |
| `m_dynamic` | `bool` | 是否动态 |
| `m_type` | `OpenGLBufferType` | 缓冲区类型 |
| `m_bufferType` | `BufferType` | RHI 缓冲区类型 |
| `m_stride` | `uint32_t` | 顶点步长 |
| `m_name` | `std::string` | 名称 |
## 使用示例
```cpp
OpenGLBuffer vb;
float vertices[] = { ... };
vb.InitializeVertexBuffer(vertices, sizeof(vertices));
vb.SetBufferType(BufferType::Vertex);
vb.SetStride(sizeof(float) * 5);
OpenGLBuffer ib;
uint16_t indices[] = { 0, 1, 2, ... };
ib.InitializeIndexBuffer(indices, sizeof(indices));
// Bind and draw
vb.Bind();
ib.Bind();
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, 0);
```