1.2 渲染状态管理
1.1 看了接口;这一节看状态。渲染系统是一台「状态机」:剔除开关、深度测试、混合方式、纹理绑定……渲染一条指令前,必须先保证状态机处于它期望的状态。OGRE 的做法是:基类保存目标状态,后端的 _set* 方法把目标状态翻译成 API 调用。
本章与站内既有内容的分工
helio 的 Renderer 用命令队列封装状态;OGRE 走的是即时翻译路线——每次设置状态立即调用 API。区别在「延迟批处理」与「每次翻译」的取舍。
目标状态:基类成员
OgreRenderSystem.h 的保护区存着一批「当前目标状态」:
// OgreMain/include/OgreRenderSystem.h:RenderSystem 保护成员(v14.6.3 源码摘录,已删减)
CullingMode mCullingMode;
size_t mBatchCount;
size_t mFaceCount;
size_t mVertexCount;
bool mInvertVertexWinding;
bool mIsReverseDepthBufferEnabled;
构造时给默认值:
// OgreMain/src/OgreRenderSystem.cpp:RenderSystem::RenderSystem(v14.6.3 源码摘录,已删减)
, mCullingMode(CULL_CLOCKWISE)
注意默认是 CULL_CLOCKWISE:剔除顺时针顶点,即多边形的正面按逆时针计数——这与 OpenGL 和右手坐标系一致。这是 OGRE 的全局坐标约定(卷一 4.1 的左手/右手约定在此落地)。
翻译:目标状态 → API 调用
_setCullingMode 是纯虚,GL3Plus 的实现展示了「翻译」的完整形态:
// RenderSystems/GL3Plus/src/OgreGL3PlusRenderSystem.cpp:GL3PlusRenderSystem::_setCullingMode(v14.6.3 源码摘录,已删减)
void GL3PlusRenderSystem::_setCullingMode(CullingMode mode)
{
mCullingMode = mode;
GLenum cullMode;
bool flip = flipFrontFace();
OGRE_CHECK_GL_ERROR(glFrontFace(flip ? GL_CW : GL_CCW));
switch( mode )
{
case CULL_NONE:
mStateCacheManager->setEnabled( GL_CULL_FACE, false );
return;
case CULL_CLOCKWISE:
cullMode = GL_BACK;
break;
case CULL_ANTICLOCKWISE:
cullMode = GL_FRONT;
break;
}
mStateCacheManager->setEnabled( GL_CULL_FACE, true );
mStateCacheManager->setCullFace( cullMode );
}
三步走:
- 先存目标状态:
mCullingMode = mode。之后_getCullingMode能读回逻辑状态,不必反向解析 API 状态。 - 处理坐标翻转:Windows 上 GL 的窗口坐标系导致前后面互换,
flipFrontFace()统一把glFrontFace定为GL_CW/GL_CCW——逻辑枚举和物理 API 不是一一对应,中间隔着一层坐标约定。 - 翻译成状态缓存调用:
CULL_NONE直接关掉GL_CULL_FACE;CULL_CLOCKWISE对应剔除背面GL_BACK;CULL_ANTICLOCKWISE对应剔除正面GL_FRONT。
mStateCacheManager 是后端的 API 状态缓存器——它记下「上一次我告诉 GPU 的状态」,翻译层只负责计算该给什么,缓存器负责去重(见下)。
深度状态:三参数 → 两个开关
_setDepthBufferParams 把三个逻辑参数翻译成两个 API 状态:
// RenderSystems/GL3Plus/src/OgreGL3PlusRenderSystem.cpp:GL3PlusRenderSystem::_setDepthBufferParams(v14.6.3 源码摘录,已删减)
void GL3PlusRenderSystem::_setDepthBufferParams(bool depthTest, bool depthWrite, CompareFunction depthFunction)
{
if (depthTest)
{
mStateCacheManager->setClearDepth(isReverseDepthBufferEnabled() ? 0.0f : 1.0f);
}
mStateCacheManager->setEnabled(GL_DEPTH_TEST, depthTest);
mStateCacheManager->setDepthMask( depthWrite );
// Store for reference in _beginFrame
mDepthWrite = depthWrite;
if(isReverseDepthBufferEnabled())
depthFunction = reverseCompareFunction(depthFunction);
mStateCacheManager->setDepthFunc(convertCompareFunction(depthFunction));
}
depthTest→GL_DEPTH_TEST开关depthWrite→setDepthMask,且顺手存一份mDepthWrite供_beginFrame参考depthFunction→setDepthFunc;反向深度缓冲开启时比较函数要反转(reverseCompareFunction),这是 OGRE 可选OGRE_REVERSED_Z的落点
状态管理就这么纯粹:上层永远用逻辑量(bool + 枚举),翻译层负责换算成物理调用。上层代码从不出现 GL_* 常量。
源码定位
OgreMain/include/OgreRenderSystem.h:CullingMode mCullingMode(1158)、_setCullingMode(CullingMode mode) = 0(692)、_getCullingMode(void) const(694)OgreMain/src/OgreRenderSystem.cpp:mCullingMode(CULL_CLOCKWISE)(56,构造默认)RenderSystems/GL3Plus/src/OgreGL3PlusRenderSystem.cpp:GL3PlusRenderSystem::_setCullingMode(755-778)、_setDepthBufferParams(785-798)OgreMain/include/OgreCommon.h:CULL_NONE = 1、CULL_CLOCKWISE = 2、CULL_ANTICLOCKWISE = 3(167-171)
上游引用固定到 OGRE v14.6.3 源码 。
可运行产物:C++ 最小验证
下面用纯 C++ 复现「目标状态 + 翻译 + 脏检查」。它与 src/01-2-render-state-management.cpp 逐字节一致:
// file: src/01-2-render-state-management.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/01-2-render-state-management.cpp -o bin/01-2-render-state-management && ./bin/01-2-render-state-management
//
// 复现渲染状态管理与「目标状态 → API 调用」翻译(对应 OgreRenderSystem.h 692-694、1158、
// GL3PlusRenderSystem.cpp 755-798):
// CullingMode 枚举(CULL_NONE/CULL_CLOCKWISE/CULL_ANTICLOCKWISE,对应 OgreCommon.h 167-171);
// _setCullingMode 先保存 mCullingMode 目标状态,再翻译成后端布尔(cullFace 开关 + 面别);
// _setDepthBufferParams(depthTest, depthWrite, func) 翻译成 depthMask + depthFunc;
// 状态缓存器持有翻译后的 API 级状态,渲染时直接消费。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"
#include <string>
namespace ogre_repro {
// 对应 OgreCommon.h 的 CullingMode 枚举
enum CullingMode { CULL_NONE = 1, CULL_CLOCKWISE = 2, CULL_ANTICLOCKWISE = 3 };
// 深度比较函数(简化:对应 CompareFunction 的 L/G 区间)
enum CompareFunction {
CMPF_ALWAYS = 7, // 恒真
CMPF_LESS = 2, // 深度小
CMPF_LESS_EQUAL = 3, // 深度小于等于
};
// API 级状态缓存(对应 mStateCacheManager 的一小部分)
struct ApiState {
bool cullEnabled = false;
std::string cullFace; // "BACK" / "FRONT"(GL 语义)
std::string frontFace; // "CW" / "CCW"
bool depthTest = false;
bool depthMask = true; // true = 可写
std::string depthFunc; // "LESS" / "LEQUAL" / "ALWAYS"
};
class RenderSystem {
public:
// 对应 _setCullingMode:先存目标状态,再翻译
void _setCullingMode(CullingMode mode) {
mCullingMode = mode;
// flipFrontFace 对应 GL3Plus 里 Windows 的逆时针翻转;此处恒为 CCW
mApi.frontFace = "CCW";
switch (mode) {
case CULL_NONE:
mApi.cullEnabled = false;
return;
case CULL_CLOCKWISE:
mApi.cullEnabled = true;
mApi.cullFace = "BACK";
return;
case CULL_ANTICLOCKWISE:
mApi.cullEnabled = true;
mApi.cullFace = "FRONT";
return;
}
}
CullingMode _getCullingMode() const { return mCullingMode; }
// 对应 _setDepthBufferParams:depthTest/depthWrite/func 翻译
void _setDepthBufferParams(bool depthTest, bool depthWrite, CompareFunction func) {
mApi.depthTest = depthTest;
mApi.depthMask = depthWrite;
switch (func) {
case CMPF_ALWAYS: mApi.depthFunc = "ALWAYS"; break;
case CMPF_LESS: mApi.depthFunc = "LESS"; break;
case CMPF_LESS_EQUAL: mApi.depthFunc = "LEQUAL"; break;
}
}
const ApiState& getApiState() const { return mApi; }
size_t getApiStateChangeCount() const { return mApiStateChangeCount; }
protected:
CullingMode mCullingMode = CULL_CLOCKWISE; // 对应 RenderSystem 构造默认值
ApiState mApi;
size_t mApiStateChangeCount = 0;
};
// 带「变化计数」的状态跟踪子类:重复设置同值不再翻译(脏检查的朴素版)
class TrackingRenderSystem : public RenderSystem {
public:
void _setCullingMode(CullingMode mode) {
if (mode == mCullingMode) return; // 状态未变,跳过翻译
++mApiStateChangeCount;
RenderSystem::_setCullingMode(mode);
}
};
} // namespace ogre_repro
int main() {
using namespace ogre_repro;
// 构造默认:CULL_CLOCKWISE(与 OGRE 一致:右手系默认剔除顺时针)
RenderSystem rs;
CHECK(rs._getCullingMode() == CULL_CLOCKWISE);
// CULL_CLOCKWISE → cullEnabled + cullFace=BACK(保留正面)
rs._setCullingMode(CULL_CLOCKWISE);
CHECK(rs.getApiState().cullEnabled);
CHECK(rs.getApiState().cullFace == "BACK");
// CULL_NONE → 关闭剔除
rs._setCullingMode(CULL_NONE);
CHECK(!rs.getApiState().cullEnabled);
// CULL_ANTICLOCKWISE → cullFace=FRONT
rs._setCullingMode(CULL_ANTICLOCKWISE);
CHECK(rs.getApiState().cullFace == "FRONT");
// 深度状态翻译
rs._setDepthBufferParams(true, false, CMPF_LESS);
CHECK(rs.getApiState().depthTest);
CHECK(!rs.getApiState().depthMask);
CHECK(rs.getApiState().depthFunc == "LESS");
rs._setDepthBufferParams(false, true, CMPF_ALWAYS);
CHECK(!rs.getApiState().depthTest);
CHECK(rs.getApiState().depthMask);
CHECK(rs.getApiState().depthFunc == "ALWAYS");
// 脏检查:同值重复设置不再触发翻译
TrackingRenderSystem trs;
CHECK(trs.getApiStateChangeCount() == 0);
trs._setCullingMode(CULL_NONE);
CHECK(trs.getApiStateChangeCount() == 1);
trs._setCullingMode(CULL_NONE); // 同值,跳过
CHECK(trs.getApiStateChangeCount() == 1);
trs._setCullingMode(CULL_CLOCKWISE);
CHECK(trs.getApiStateChangeCount() == 2);
return lab::report("01-2 渲染状态管理");
}
本机实测(Apple clang 16,clang++ -std=c++20):编译通过(-Wall 零警告),断言 15/15 通过。
小结
- 基类用
mCullingMode等成员保存目标状态(逻辑量),默认CULL_CLOCKWISE。 - 后端的
_setCullingMode先存目标状态,再经坐标翻转检查翻译成 API 调用。 _setDepthBufferParams把depthTest/depthWrite/CompareFunction翻译成GL_DEPTH_TEST、setDepthMask、setDepthFunc(反向深度时反转比较函数)。- 状态缓存的去重交给
mStateCacheManager;翻译层只负责计算「该给什么」。
阅读导航:上一节:1.1 RenderSystem 抽象基类 · 下一节:1.3 创建渲染目标 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。