230 lines
5.1 KiB
Markdown
230 lines
5.1 KiB
Markdown
# OpenGLPipelineState
|
|
|
|
OpenGL 管线状态对象实现,封装多种 OpenGL 状态。
|
|
|
|
## 头文件
|
|
|
|
```cpp
|
|
#include <XCEngine/RHI/OpenGL/OpenGLPipelineState.h>
|
|
```
|
|
|
|
## 继承关系
|
|
|
|
```
|
|
RHIPipelineState (interface)
|
|
└── OpenGLPipelineState (implementation)
|
|
```
|
|
|
|
## 枚举
|
|
|
|
### OpenGLPrimitiveTopology
|
|
|
|
| 值 | 描述 |
|
|
|----|------|
|
|
| `Points` | 点 |
|
|
| `Lines` | 线 |
|
|
| `LineStrip` | 线带 |
|
|
| `Triangles` | 三角形 |
|
|
| `TriangleStrip` | 三角形带 |
|
|
|
|
### OpenGLBlendOp
|
|
|
|
| 值 | OpenGL 常量 |
|
|
|----|-------------|
|
|
| `Add` | `GL_FUNC_ADD` |
|
|
| `Subtract` | `GL_FUNC_SUBTRACT` |
|
|
| `ReverseSubtract` | `GL_FUNC_REVERSE_SUBTRACT` |
|
|
| `Min` | `GL_MIN` |
|
|
| `Max` | `GL_MAX` |
|
|
|
|
### CullFace / FrontFace / PolygonMode
|
|
|
|
```cpp
|
|
enum class CullFace { Front, Back, FrontAndBack };
|
|
enum class FrontFace { Clockwise, CounterClockwise };
|
|
enum class PolygonMode { Point, Line, Fill };
|
|
```
|
|
|
|
## 状态结构体
|
|
|
|
### OpenGLDepthStencilState
|
|
|
|
```cpp
|
|
struct OpenGLDepthStencilState {
|
|
bool depthTestEnable = true;
|
|
bool depthWriteEnable = true;
|
|
ComparisonFunc depthFunc = ComparisonFunc::Less;
|
|
bool stencilEnable = false;
|
|
uint8_t stencilReadMask = 0xFF;
|
|
uint8_t stencilWriteMask = 0xFF;
|
|
int stencilRef = 0;
|
|
ComparisonFunc stencilFunc = ComparisonFunc::Always;
|
|
StencilOp stencilFailOp = StencilOp::Keep;
|
|
StencilOp stencilDepthFailOp = StencilOp::Keep;
|
|
StencilOp stencilDepthPassOp = StencilOp::Keep;
|
|
};
|
|
```
|
|
|
|
### OpenGLBlendState
|
|
|
|
```cpp
|
|
struct OpenGLBlendState {
|
|
bool blendEnable = false;
|
|
BlendFactor srcBlend = BlendFactor::SrcAlpha;
|
|
BlendFactor dstBlend = BlendFactor::InvSrcAlpha;
|
|
BlendFactor srcBlendAlpha = BlendFactor::One;
|
|
BlendFactor dstBlendAlpha = BlendFactor::Zero;
|
|
BlendOp blendOp = BlendOp::Add;
|
|
BlendOp blendOpAlpha = BlendOp::Add;
|
|
uint8_t colorWriteMask = 0xF;
|
|
float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
|
|
};
|
|
```
|
|
|
|
### OpenGLRasterizerState
|
|
|
|
```cpp
|
|
struct OpenGLRasterizerState {
|
|
bool cullFaceEnable = true;
|
|
CullFace cullFace = CullFace::Back;
|
|
FrontFace frontFace = FrontFace::CounterClockwise;
|
|
PolygonMode polygonMode = PolygonMode::Fill;
|
|
float polygonOffsetFactor = 0.0f;
|
|
float polygonOffsetUnits = 0.0f;
|
|
bool depthClipEnable = true;
|
|
bool scissorTestEnable = false;
|
|
bool multisampleEnable = false;
|
|
bool sampleAlphaToCoverageEnable = false;
|
|
};
|
|
```
|
|
|
|
### ViewportState / ScissorState / LogicalOperation
|
|
|
|
```cpp
|
|
struct ViewportState {
|
|
float x = 0.0f, y = 0.0f;
|
|
float width = 0.0f, height = 0.0f;
|
|
float minDepth = 0.0f, maxDepth = 1.0f;
|
|
};
|
|
|
|
struct ScissorState {
|
|
bool enable = false;
|
|
int x = 0, y = 0, width = 0, height = 0;
|
|
};
|
|
|
|
struct LogicalOperation {
|
|
bool enable = false;
|
|
uint32_t operation = 0;
|
|
};
|
|
```
|
|
|
|
## 公共成员函数
|
|
|
|
### 生命周期
|
|
|
|
#### `OpenGLPipelineState()`
|
|
|
|
#### `~OpenGLPipelineState() override`
|
|
|
|
#### `void Shutdown() override`
|
|
|
|
### 绑定
|
|
|
|
#### `void Bind() override`
|
|
应用所有状态。
|
|
|
|
#### `void Unbind() override`
|
|
|
|
#### `void* GetNativeHandle() override`
|
|
|
|
#### `PipelineType GetType() const override { return PipelineType::Graphics; }`
|
|
|
|
### 状态设置
|
|
|
|
#### `void SetDepthStencilState(const OpenGLDepthStencilState& state)`
|
|
|
|
#### `void SetBlendState(const OpenGLBlendState& state)`
|
|
|
|
#### `void SetRasterizerState(const OpenGLRasterizerState& state)`
|
|
|
|
#### `void SetViewport(const ViewportState& state)`
|
|
|
|
#### `void SetScissor(const ScissorState& state)`
|
|
|
|
#### `void SetLogicalOperation(const LogicalOperation& state)`
|
|
|
|
### 状态应用
|
|
|
|
#### `void Apply()`
|
|
应用所有状态。
|
|
|
|
#### `void ApplyDepthStencil()`
|
|
|
|
#### `void ApplyBlend()`
|
|
|
|
#### `void ApplyRasterizer()`
|
|
|
|
#### `void ApplyViewport()`
|
|
|
|
#### `void ApplyScissor()`
|
|
|
|
### 清除
|
|
|
|
#### `void SetClearColor(float r, float g, float b, float a)`
|
|
|
|
#### `void Clear(unsigned int buffers)`
|
|
|
|
### Shader 附加
|
|
|
|
#### `void AttachShader(unsigned int program)`
|
|
|
|
#### `void DetachShader()`
|
|
|
|
### 属性获取
|
|
|
|
#### `const OpenGLDepthStencilState& GetDepthStencilState() const`
|
|
|
|
#### `const OpenGLBlendState& GetBlendState() const`
|
|
|
|
#### `const OpenGLRasterizerState& GetRasterizerState() const`
|
|
|
|
## 内部成员
|
|
|
|
| 成员 | 类型 | 描述 |
|
|
|------|------|------|
|
|
| `m_depthStencilState` | `OpenGLDepthStencilState` | 深度模板状态 |
|
|
| `m_blendState` | `OpenGLBlendState` | 混合状态 |
|
|
| `m_rasterizerState` | `OpenGLRasterizerState` | 光栅化状态 |
|
|
| `m_viewportState` | `ViewportState` | 视口状态 |
|
|
| `m_scissorState` | `ScissorState` | 裁剪状态 |
|
|
| `m_logicalOperation` | `LogicalOperation` | 逻辑操作 |
|
|
| `m_clearColor` | `float[4]` | 清除颜色 |
|
|
| `m_program` | `unsigned int` | GL program |
|
|
| `m_programAttached` | `bool` | program 是否附加 |
|
|
|
|
## 使用示例
|
|
|
|
```cpp
|
|
OpenGLPipelineState pso;
|
|
|
|
OpenGLDepthStencilState ds;
|
|
ds.depthTestEnable = true;
|
|
ds.depthWriteEnable = true;
|
|
ds.depthFunc = ComparisonFunc::Less;
|
|
pso.SetDepthStencilState(ds);
|
|
|
|
OpenGLBlendState blend;
|
|
blend.blendEnable = true;
|
|
blend.srcBlend = BlendFactor::SrcAlpha;
|
|
blend.dstBlend = BlendFactor::InvSrcAlpha;
|
|
pso.SetBlendState(blend);
|
|
|
|
OpenGLRasterizerState raster;
|
|
raster.cullFaceEnable = true;
|
|
raster.cullFace = CullFace::Back;
|
|
pso.SetRasterizerState(raster);
|
|
|
|
pso.AttachShader(shaderProgram);
|
|
pso.Bind();
|
|
```
|