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

88 lines
2.7 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.
# OpenGL Backend Overview
OpenGL RHI 后端实现,基于 GLFW 和现代 OpenGL (Core Profile)。
## 头文件
所有 OpenGL 后端头文件位于 `engine/include/XCEngine/RHI/OpenGL/`
## 架构说明
OpenGL 是**立即模式** API与 D3D12 的命令列表模式有本质区别:
- 无显式的命令列表录制和提交
- 状态通过 OpenGL 状态机管理
- `OpenGLCommandList` 主要用于状态批处理和 RHI 接口兼容
- 交换链使用 GLFW 窗口管理
## 后端组件
| 类 | 文件 | 描述 |
|----|------|------|
| `OpenGLDevice` | `OpenGLDevice.h` | 主设备,管理窗口和 OpenGL 上下文 |
| `OpenGLBuffer` | `OpenGLBuffer.h` | GPU 缓冲区VBO/UBO/SSBO |
| `OpenGLTexture` | `OpenGLTexture.h` | 纹理对象1D/2D/3D/Cube |
| `OpenGLCommandList` | `OpenGLCommandList.h` | 命令列表(状态批处理) |
| `OpenGLCommandQueue` | `OpenGLCommandQueue.h` | 命令队列RHI 兼容层) |
| `OpenGLSwapChain` | `OpenGLSwapChain.h` | 交换链GLFW 窗口) |
| `OpenGLFence` | `OpenGLFence.h` | 栅栏同步GLsync |
| `OpenGLShader` | `OpenGLShader.h` | Shader Program |
| `OpenGLPipelineState` | `OpenGLPipelineState.h` | 管线状态 |
| `OpenGLSampler` | `OpenGLSampler.h` | 采样器对象 |
| `OpenGLVertexArray` | `OpenGLVertexArray.h` | 顶点数组对象 (VAO) |
| `OpenGLRenderTargetView` | `OpenGLRenderTargetView.h` | 渲染目标 (FBO) |
| `OpenGLDepthStencilView` | `OpenGLDepthStencilView.h` | 深度模板 (FBO) |
## 初始化流程
```cpp
#include <XCEngine/RHI/OpenGL/OpenGLDevice.h>
using namespace XCEngine::RHI;
OpenGLDevice device;
device.CreateRenderWindow(1920, 1080, "XCEngine", true);
// Create resources
OpenGLShader shader;
shader.CompileFromFile("shaders/Default.vert", "shaders/Default.frag");
OpenGLBuffer vb;
vb.InitializeVertexBuffer(vertices, sizeof(vertices));
OpenGLTexture texture;
texture.LoadFromFile("textures/diffuse.png");
// Render loop
while (!device.ShouldClose()) {
device.PollEvents();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader.Use();
vb.Bind();
texture.Bind(0);
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
device.SwapBuffers();
}
```
## 与 D3D12 的差异
| 方面 | D3D12 | OpenGL |
|------|-------|--------|
| 模式 | 命令列表录制 | 立即模式 |
| 状态管理 | 显式资源状态 | OpenGL 状态机 |
| 描述符 | 描述符堆 + 句柄 | 绑定点 |
| 管线状态 | PSO不可变 | 可变状态 |
| 内存管理 | 显式显存管理 | 驱动自动管理 |
| 多线程 | 需要 Bundle | 上下文共享 |
## 扩展模块
- **GLFW**: 窗口管理和输入
- **GLSL**: 着色器语言 (Core Profile)
- **stb_image**: 纹理文件加载