13.3 深度缓冲管理
深度缓冲不便宜:一张 1080p 的 D24S8 动辄几 MB,若每张渲染目标都独享一张,RTT 链会吃掉大量显存。OGRE 的解法是把深度缓冲池化——同一「池 ID + FSAA(+ 尺寸)」的渲染目标共享一张。这一节看 RenderSystem::setDepthBufferFor 怎么从池里挑缓冲、挑不中怎么新建。
本章与站内既有内容的分工
13.1 看了 DepthBuffer 单张缓冲的兼容判定,这一节看它如何被 DepthBufferMap 池化复用;与卷一 ogre-core 的 ArchiveManager 缓存范式同源,但这里是按「哈希桶」而非名字查找。cg 三卷不涉及资源复用,此处的取舍纯属 OGRE 的资源策略。
池的结构:DepthBufferMap
RenderSystem 用一个两级结构装深度缓冲:
// OgreMain/include/OgreRenderSystem.h:DepthBufferMap typedef(v14.6.3 源码摘录,已删减)
typedef std::vector<DepthBuffer*> DepthBufferVec;
typedef std::map< uint32, DepthBufferVec > DepthBufferMap;
外层 std::map<uint32, DepthBufferVec>:键是哈希过的池描述,值是一串候选缓冲;内层 DepthBufferVec 是共享同一键的所有缓冲。成员 mDepthBufferPool 就挂在 RenderSystem 的 protected 区,和 1.3 讲过的 mRenderTargets/mPrioritisedRenderTargets 并排:
// OgreMain/include/OgreRenderSystem.h:DepthBufferMap mDepthBufferPool(v14.6.3 源码摘录,已删减)
/** DepthBuffers to be attached to render targets */
DepthBufferMap mDepthBufferPool;
setDepthBufferFor:先查后建
RenderTarget 创建后不会自动有深度缓冲,得由 RenderSystem::setDepthBufferFor 补上。它先把「池 ID + FSAA(+ 尺寸)」揉成一个桶键,再进桶挑缓冲:
// OgreMain/src/OgreRenderSystem.cpp:RenderSystem::setDepthBufferFor(v14.6.3 源码摘录,已删减)
void RenderSystem::setDepthBufferFor( RenderTarget *renderTarget )
{
uint16 poolNum = renderTarget->getDepthBufferPool();
if( poolNum == RBP_NONE )
return; //RenderTarget explicitly requested no depth buffer
uint32 poolId = HashCombine(0, poolNum);
poolId = HashCombine(poolId, renderTarget->getFSAA());
if(!getCapabilities()->hasCapability(RSC_RTT_INDEPENDENT_BUFFER_SIZE))
{
//Depth buffer must be EXACT same size as RT
poolId = HashCombine(poolId, renderTarget->getWidth());
poolId = HashCombine(poolId, renderTarget->getHeight());
}
//Find a depth buffer in the pool
bool bAttached = false;
for (auto& d : mDepthBufferPool[poolId]) {
bAttached = renderTarget->attachDepthBuffer(d);
if (bAttached) break;
}
//Not found yet? Create a new one!
if( !bAttached )
{
DepthBuffer *newDepthBuffer = _createDepthBufferFor( renderTarget );
if( newDepthBuffer )
{
mDepthBufferPool[poolId].push_back( newDepthBuffer );
bAttached = renderTarget->attachDepthBuffer( newDepthBuffer );
OgreAssert( bAttached ,"A new DepthBuffer for a RenderTarget was created, but after creation"
" it says it's incompatible with that RT" );
}
else
LogManager::getSingleton().logWarning( "Couldn't create a suited DepthBuffer"
"for RT: " + renderTarget->getName());
}
}
| 分支 | 行为 |
|---|---|
poolNum == RBP_NONE | RT 明确不要深度缓冲,直接返回 |
无 RSC_RTT_INDEPENDENT_BUFFER_SIZE | 尺寸必须精确一致,宽高进哈希键 |
| 有独立尺寸能力 | 尺寸不进键,允许小 RT 复用大缓冲 |
| 桶内找到兼容缓冲 | attachDepthBuffer(d) 返回 true,复用 |
| 桶内全不兼容 | _createDepthBufferFor 新建 → 入池 → 再附着 |
注意 mDepthBufferPool[poolId] 用 operator[]:键不存在也会创建空桶,随后新建缓冲入池,桶随之有内容。池 ID 由 RenderTarget::getDepthBufferPool() 提供,默认 RBP_DEFAULT,所有 RT 默认共享一个池。
复用条件:attach 与兼容
桶内逐个尝试 renderTarget->attachDepthBuffer(d),这个函数(13.1 看过)内部先问 isCompatible:宽高不小于、FSAA 相同才返回 true。所以「同桶」不保证「可复用」,桶只是粗筛,精确判定仍由每张缓冲自己做:
// OgreMain/src/OgreRenderTarget.cpp:RenderTarget::attachDepthBuffer(v14.6.3 源码摘录,已删减)
bool RenderTarget::attachDepthBuffer( DepthBuffer *depthBuffer )
{
bool retVal = false;
if( (retVal = depthBuffer->isCompatible( this )) )
{
detachDepthBuffer();
mDepthBuffer = depthBuffer;
mDepthBuffer->_notifyRenderTargetAttached( this );
}
return retVal;
}
被换掉的旧缓冲通过 detachDepthBuffer() 通知摘除,随后新缓冲 _notifyRenderTargetAttached 入集合。池的清理统一走 _cleanupDepthBuffers(shutdown() 时调用),按桶释放所有非手动缓冲。
深度缓冲池复用流程
flowchart TD
A["RenderTarget 请求深度缓冲"] --> B{"存在可用池?"}
B -->|"是"| C["遍历池内候选"]
C --> D{"兼容且附着成功?"}
D -->|"是"| E["复用候选"]
D -->|"候选耗尽"| F["交给后端创建"]
B -->|"无兼容项"| F
F --> G{"创建成功?"}
G -->|"是"| H["入池并附着"]
G -->|"否"| I["保留无深度缓冲状态"]
深度缓冲池减少分配的条件
池号缩小搜索范围,候选还需符合目标的兼容条件并成功附着。多个目标复用的前提来自后端实现,尺寸、采样数和格式改变后需重新判断。
对一对配置相同目标与一对采样数不匹配目标记录资源身份。释放阶段同时核查渲染目标和深度池的反向引用。
源码定位
OgreMain/src/OgreRenderSystem.cpp:setDepthBufferFor(512-553)、_cleanupDepthBuffers(487-499)、_createDepthBufferFor调用点(538)OgreMain/include/OgreRenderSystem.h:DepthBufferVec/DepthBufferMaptypedef(56-57)、mDepthBufferPool(1137)OgreMain/src/OgreRenderTarget.cpp:attachDepthBuffer(94-106)、detachDepthBuffer(108-115)OgreMain/include/OgreRenderTarget.h:getDepthBufferPool(110)、setDepthBufferPool(107)
上游引用固定到 OGRE v14.6.3 源码 。
可运行产物:C++ 最小验证
下面复现池化分配:跳过、共享、分桶、能力开关与清理。它与 src/13-3-depth-management.cpp 逐字节一致:
// file: src/13-3-depth-management.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/13-3-depth-management.cpp -o bin/13-3-depth-management && ./bin/13-3-depth-management
//
// 复现 setDepthBufferFor 的池化分配(对应 OgreRenderSystem.cpp 512-553 与 OgreRenderSystem.h 56-57):
// 桶键 = Hash(poolNum, fsaa[, width, height]);RSC_RTT_INDEPENDENT_BUFFER_SIZE 缺失时尺寸进键;
// 在桶里找第一张兼容缓冲附着;未找到则 _createDepthBufferFor 新建并入池;
// POOL_NO_DEPTH(RBP_NONE) 直接跳过,不分配。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"
#include <cstdint>
#include <map>
#include <vector>
namespace ogre_repro {
constexpr uint16_t RBP_NONE = 0;
constexpr uint16_t RBP_DEFAULT = 1;
class RenderTargetRepro;
class DepthBufferRepro {
public:
DepthBufferRepro(uint32_t poolId, uint32_t width, uint32_t height, uint32_t fsaa)
: mPoolId(poolId), mWidth(width), mHeight(height), mFsaa(fsaa) {}
uint32_t getPoolId() const { return mPoolId; }
uint32_t getWidth() const { return mWidth; }
uint32_t getHeight() const { return mHeight; }
uint32_t getFSAA() const { return mFsaa; }
bool isCompatible(const RenderTargetRepro* rt) const;
void setAttachedTargets(size_t n) { mAttachedTargets = n; }
size_t getAttachedTargets() const { return mAttachedTargets; }
private:
uint32_t mPoolId;
uint32_t mWidth;
uint32_t mHeight;
uint32_t mFsaa;
size_t mAttachedTargets = 0;
};
class RenderTargetRepro {
public:
RenderTargetRepro(uint16_t pool, uint32_t width, uint32_t height, uint32_t fsaa)
: mPool(pool), mWidth(width), mHeight(height), mFsaa(fsaa) {}
uint16_t getPool() const { return mPool; }
uint32_t getWidth() const { return mWidth; }
uint32_t getHeight() const { return mHeight; }
uint32_t getFSAA() const { return mFsaa; }
// 对应 RenderTarget::attachDepthBuffer:兼容才附,附前摘旧
bool attachDepthBuffer(DepthBufferRepro* db) {
if (!db->isCompatible(this)) return false;
if (mDepthBuffer) {
mDepthBuffer->setAttachedTargets(mDepthBuffer->getAttachedTargets() - 1);
}
mDepthBuffer = db;
db->setAttachedTargets(db->getAttachedTargets() + 1);
return true;
}
DepthBufferRepro* getDepthBuffer() const { return mDepthBuffer; }
private:
uint16_t mPool;
uint32_t mWidth;
uint32_t mHeight;
uint32_t mFsaa;
DepthBufferRepro* mDepthBuffer = nullptr;
};
inline bool DepthBufferRepro::isCompatible(const RenderTargetRepro* rt) const {
return mWidth >= rt->getWidth() && mHeight >= rt->getHeight() && mFsaa == rt->getFSAA();
}
class RenderSystemRepro {
public:
explicit RenderSystemRepro(bool independentBufferSize) : mIndependentBufferSize(independentBufferSize) {}
static uint32_t hashCombine(uint32_t seed, uint32_t v) {
// 简化版组合哈希(OGRE 用 MurmurHash3 的 HashCombine)
return seed ^ (v + 0x9e3779b9u + (seed << 6) + (seed >> 2));
}
// 对应 RenderSystem::setDepthBufferFor
void setDepthBufferFor(RenderTargetRepro* rt) {
uint16_t poolNum = rt->getPool();
if (poolNum == RBP_NONE) return; // POOL_NO_DEPTH:明确要求不分配
uint32_t poolId = hashCombine(0, poolNum);
poolId = hashCombine(poolId, rt->getFSAA());
if (!mIndependentBufferSize) {
poolId = hashCombine(poolId, rt->getWidth());
poolId = hashCombine(poolId, rt->getHeight());
}
// 在桶里找第一张兼容缓冲
bool attached = false;
for (auto* d : mPool[poolId]) {
attached = rt->attachDepthBuffer(d);
if (attached) break;
}
// 未找到:新建并入池
if (!attached) {
DepthBufferRepro* ndb =
new DepthBufferRepro(poolId, rt->getWidth(), rt->getHeight(), rt->getFSAA());
mPool[poolId].push_back(ndb);
++mCreated;
rt->attachDepthBuffer(ndb);
}
}
// 对应 _cleanupDepthBuffers:释放池内所有缓冲
void cleanup() {
for (auto& kv : mPool)
for (auto* b : kv.second) delete b;
mPool.clear();
}
size_t poolSize() const {
size_t n = 0;
for (auto& kv : mPool) n += kv.second.size();
return n;
}
int createdCount() const { return mCreated; }
private:
bool mIndependentBufferSize;
int mCreated = 0;
std::map<uint32_t, std::vector<DepthBufferRepro*>> mPool; // 对应 DepthBufferMap
};
} // namespace ogre_repro
int main() {
using namespace ogre_repro;
// POOL_NO_DEPTH:直接跳过,不创建任何缓冲
RenderSystemRepro rs(false);
RenderTargetRepro noDepth(RBP_NONE, 800, 600, 4);
rs.setDepthBufferFor(&noDepth);
CHECK(noDepth.getDepthBuffer() == nullptr);
CHECK_EQ(rs.createdCount(), 0);
CHECK_EQ(rs.poolSize(), size_t(0));
// 同尺寸两个 RT:共享同一张深度缓冲
RenderTargetRepro a(RBP_DEFAULT, 800, 600, 4);
RenderTargetRepro b(RBP_DEFAULT, 800, 600, 4);
rs.setDepthBufferFor(&a);
CHECK(a.getDepthBuffer() != nullptr);
CHECK_EQ(rs.createdCount(), 1);
rs.setDepthBufferFor(&b);
CHECK(b.getDepthBuffer() == a.getDepthBuffer());
CHECK_EQ(rs.createdCount(), 1);
CHECK_EQ(a.getDepthBuffer()->getAttachedTargets(), size_t(2));
// 不同尺寸、无 INDEPENDENT_BUFFER_SIZE 能力:尺寸进键,分桶新建
RenderTargetRepro big(RBP_DEFAULT, 1280, 1024, 4);
rs.setDepthBufferFor(&big);
CHECK(big.getDepthBuffer() != a.getDepthBuffer());
CHECK_EQ(rs.createdCount(), 2);
// INDEPENDENT_BUFFER_SIZE 开启:尺寸不进键,小 RT 复用大的缓冲
RenderSystemRepro rsInd(true);
RenderTargetRepro c(RBP_DEFAULT, 800, 600, 4);
RenderTargetRepro d(RBP_DEFAULT, 1600, 1200, 4);
rsInd.setDepthBufferFor(&d);
CHECK_EQ(rsInd.createdCount(), 1);
rsInd.setDepthBufferFor(&c);
CHECK(c.getDepthBuffer() == d.getDepthBuffer());
CHECK_EQ(rsInd.createdCount(), 1);
// FSAA 不同:尺寸相同也不共享(进入不同桶)
RenderTargetRepro e(RBP_DEFAULT, 800, 600, 8);
rs.setDepthBufferFor(&e);
CHECK(e.getDepthBuffer() != a.getDepthBuffer());
CHECK_EQ(rs.createdCount(), 3);
// cleanup 后池清空
rs.cleanup();
rsInd.cleanup();
CHECK_EQ(rs.poolSize(), size_t(0));
CHECK_EQ(rsInd.poolSize(), size_t(0));
return lab::report("13-3 深度缓冲管理");
}
本机实测(Apple clang 16,clang++ -std=c++20):编译通过(-Wall 零警告),断言 17/17 通过。
小结
- 深度缓冲存进
DepthBufferMap(外层桶、内层候选列表),键由池 ID、FSAA(和尺寸)组合哈希得到。 setDepthBufferFor先桶内逐个试attachDepthBuffer,全不兼容才_createDepthBufferFor新建入池。POOL_NO_DEPTH(RBP_NONE)让 RT 明确跳过深度缓冲分配。RSC_RTT_INDEPENDENT_BUFFER_SIZE能力决定尺寸是否进键,从而决定能否跨尺寸共享。- 精确兼容判定仍由
DepthBuffer::isCompatible完成,桶只是粗筛。
阅读导航:上一节:13.2 模板测试 · 下一节:14.1 渲染目标管理 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。