8.1 分页概述
第七章的地形可以很大:整个世界由几百块 Terrain 拼成,但每块都常驻内存显然不现实。OGRE 的 Paging 组件(Components/Paging)就是为此设计的通用机制——把「世界」切成「页」,只加载相机附近的页。这一节讲它的四层结构和页的生命周期。
本章与站内既有内容的分工
helio 四卷没有分页组件;webarch 卷讲过流式加载思路。这里解析真实 OGRE 的 Paging:PageManager → PagedWorld → PagedWorldSection → Page 的层级、PageMap 的持有方式、frameLastHeld 的卸载候选判定。
四层结构
Paging 的类层级是一个「管理器 → 世界 → 分区 → 页」的嵌套树:
PageManager
└── PagedWorld (可多个,按名字管理)
└── PagedWorldSection (每个世界可多个,各用一种 PageStrategy)
└── Page (PageMap: PageID -> Page*)
PageManager:顶层,创建/销毁世界、注册 PageStrategy 与 PageProvider、跟踪相机。PagedWorld:一个独立的世界实例,可含多个分区。PagedWorldSection:一个分区,使用某一种PageStrategy(第八章后半讲 Grid2D 等策略),内部持有PageMap。Page:最小分页单元,在分区内 ID 唯一。
Page:ID、父分区、最近持有帧
Page 是数据容器,关键信息是 ID、所属分区、以及「最近一次被持有」的帧号:
// Components/Paging/include/OgrePage.h:Page(v14.6.3 源码摘录,已删减)
/// Get the ID of this page, unique within the parent
virtual PageID getID() const { return mID; }
/// Get the PagedWorldSection this page belongs to
virtual PagedWorldSection* getParentSection() const { return mParent; }
/** Get the frame number in which this Page was last loaded or held.
A Page that has not been requested to be loaded or held in the recent
past will be a candidate for removal.
*/
virtual unsigned long getFrameLastHeld() { return mFrameLastHeld; }
/// 'Touch' the page to let it know it's being used
virtual void touch();
mFrameLastHeld 是整个分页系统的节流阀:策略每帧扫描相机附近的页,被需要的页 touch()(更新帧号),长期没被触摸的页就成了「卸载候选」。注释说得很直白:A Page that has not been requested to be loaded or held in the recent past will be a candidate for removal.(8.2 的 notifyCamera 就干这个)。
PagedWorldSection:PageMap 与 load/unload/hold
分区用 std::map<PageID, Page*> 持有已加载的页,提供三个核心操作:
// Components/Paging/include/OgrePagedWorldSection.h:PagedWorldSection(v14.6.3 源码摘录,已删减)
typedef std::map<PageID, Page*> PageMap;
...
virtual void loadPage(PageID pageID, bool forceSynchronous = false);
virtual void unloadPage(PageID pageID, bool forceSynchronous = false);
virtual void unloadPage(Page* p, bool forceSynchronous = false);
...
virtual void holdPage(PageID pageID);
loadPage(pageID):请求加载一页。具体加载工作由PageProvider完成;forceSynchronous为 false 时走后台工作队列,返回后内容可能仍在变化(所以Page::isDeferredProcessInProgress()会提示「现在访问不安全」)。unloadPage:卸载并释放。同样支持同步/异步。holdPage(pageID):只「触摸」不加载——用于保持缓冲区内已加载页不卸载。
PageManager 与 PagedWorld
PageManager 是入口,createWorld 按名创建世界;世界再 createSection 建分区:
// Components/Paging/include/OgrePageManager.h:PageManager(v14.6.3 源码摘录,已删减)
/** Create a new PagedWorld instance.
@param name Optionally give a name to the world (if no name is given, one
will be generated).
*/
PagedWorld* createWorld(const String& name = BLANKSTRING);
...
/** Add a new PageStrategy implementation.
The caller remains resonsible for destruction of this instance.
*/
void addStrategy(PageStrategy* strategy);
// Components/Paging/include/OgrePagedWorld.h:PagedWorld::createSection(v14.6.3 源码摘录,已删减)
/** Create a new manually defined section of the world.
World sections are areas of the world that use a particular
PageStrategy, with a certain set of parameters specific to that
strategy. So you would have more than one section in a world only
if you needed different simultaneous paging strategies, or you
wanted the same strategy but parameterised differently.
@param sceneMgr The SceneManager to use for this section.
@param typeName The type of section to use (must be registered
with PageManager), or blank to use the default type (simple grid)
@param sectionName An optional name to give the section (if none is
provided, one will be generated)
*/
PagedWorldSection* createSection(SceneManager* sceneMgr,
const String& typeName,
const String& sectionName = BLANKSTRING);
为什么要「多分区」?因为一个世界可能同时用多种策略:主地形用 Grid2D 分页、散落的建筑用另一种策略,两者各占一个分区、参数互不影响。
与地形的关系
Paging 是独立于 Terrain 的通用机制;第八章最后一节(8.3)会看到 TerrainPagedWorldSection 把两者桥接起来:Page 的 ID 被解包成格子坐标,页的内容就是一块 Terrain。设计上先有通用分页,地形只是它的一个「内容提供者」。
源码定位
Components/Paging/include/OgrePageManager.h:PageManager(57)、createWorld(144)、addStrategy(189)、addCamera(397)Components/Paging/include/OgrePagedWorld.h:PagedWorld(38)、createSection(109/127/143)Components/Paging/include/OgrePagedWorldSection.h:PagedWorldSection(52)、PageMap(72)、loadPage(178)、unloadPage(187/195)、holdPage(242)Components/Paging/include/OgrePage.h:Page(60)、getID(110)、getParentSection(112)、getFrameLastHeld(117)、touch(120)
上游引用固定到 OGRE v14.6.3 源码 。
可运行产物:C++ 最小验证
下面用纯 C++ 复现分页四层结构与页生命周期。它与 src/08-1-paging-overview.cpp 逐字节一致:
// file: src/08-1-paging-overview.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/08-1-paging-overview.cpp -o bin/08-1-paging-overview && ./bin/08-1-paging-overview
//
// 复现 Paging 组件的分层生命周期(对应 OgrePageManager.h 的 PageManager、
// OgrePagedWorld.h 的 PagedWorld、OgrePagedWorldSection.h 的 PagedWorldSection、
// OgrePage.h 的 Page):
// PageManager 管理 PagedWorld(createWorld/destroyWorld);
// PagedWorld 管理若干 PagedWorldSection(createSection),每个 section 用一种 PageStrategy;
// PagedWorldSection 用 PageMap(map<PageID,Page*>)持有已加载页,loadPage 幂等创建、
// unloadPage 移除释放、holdPage 更新 frameLastHeld;
// Page 携带全局唯一的 PageID 与父 section,frameLastHeld 标记最近被持有/加载的帧号,
// 长期未持有的页是卸载候选。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"
#include <cstdint>
#include <map>
#include <string>
namespace ogre_repro {
using PageID = uint32_t;
class PagedWorldSection; // 前置声明,Page 只存指针
// 对应 OgrePage.h 的 Page(110- getID/getParentSection/getFrameLastHeld)
class Page {
public:
Page(PageID id, PagedWorldSection* parent, unsigned long frame)
: mID(id), mParent(parent), mFrameLastHeld(frame) {}
virtual ~Page() = default;
PageID getID() const { return mID; }
PagedWorldSection* getParentSection() const { return mParent; }
unsigned long getFrameLastHeld() const { return mFrameLastHeld; }
// 对应 touch():标记本帧正在使用
void touch(unsigned long frame) { mFrameLastHeld = frame; }
private:
PageID mID;
PagedWorldSection* mParent;
unsigned long mFrameLastHeld;
};
// 对应 OgrePagedWorldSection.h 的 PagedWorldSection(72- PageMap,178- loadPage/unloadPage/holdPage)
class PagedWorldSection {
public:
using PageMap = std::map<PageID, Page*>;
~PagedWorldSection() {
for (auto& kv : mPages) delete kv.second; // 兜底释放残余页
}
// 对应 loadPage:已存在则返回既有页,否则创建
Page* loadPage(PageID pageID, unsigned long frame) {
auto it = mPages.find(pageID);
if (it != mPages.end()) return it->second;
Page* p = new Page(pageID, this, frame);
mPages[pageID] = p;
return p;
}
// 对应 unloadPage:移除并释放
void unloadPage(PageID pageID) {
auto it = mPages.find(pageID);
if (it != mPages.end()) {
delete it->second;
mPages.erase(it);
}
}
// 对应 holdPage:'触摸'页以推迟卸载
void holdPage(PageID pageID, unsigned long frame) {
auto it = mPages.find(pageID);
if (it != mPages.end()) it->second->touch(frame);
}
Page* getPage(PageID pageID) {
auto it = mPages.find(pageID);
return it == mPages.end() ? nullptr : it->second;
}
size_t getLoadedPageCount() const { return mPages.size(); }
PageMap mPages; // 对应 PageMap mPages
};
// 对应 OgrePagedWorld.h 的 PagedWorld(109- createSection)
class PagedWorld {
public:
~PagedWorld() {
for (auto& kv : mSections) delete kv.second;
}
PagedWorldSection* createSection(const std::string& name) {
PagedWorldSection* sec = new PagedWorldSection();
mSections[name] = sec;
return sec;
}
PagedWorldSection* getSection(const std::string& name) {
auto it = mSections.find(name);
return it == mSections.end() ? nullptr : it->second;
}
size_t getSectionCount() const { return mSections.size(); }
private:
std::map<std::string, PagedWorldSection*> mSections;
};
// 对应 OgrePageManager.h 的 PageManager(144- createWorld,189- addStrategy)
class PageManager {
public:
~PageManager() {
for (auto& kv : mWorlds) delete kv.second;
}
PagedWorld* createWorld(const std::string& name) {
PagedWorld* w = new PagedWorld();
mWorlds[name] = w;
return w;
}
void destroyWorld(const std::string& name) {
auto it = mWorlds.find(name);
if (it != mWorlds.end()) {
delete it->second;
mWorlds.erase(it);
}
}
PagedWorld* getWorld(const std::string& name) {
auto it = mWorlds.find(name);
return it == mWorlds.end() ? nullptr : it->second;
}
size_t getWorldCount() const { return mWorlds.size(); }
private:
std::map<std::string, PagedWorld*> mWorlds;
};
} // namespace ogre_repro
int main() {
using namespace ogre_repro;
// PageManager -> PagedWorld -> PagedWorldSection -> Page
PageManager mgr;
PagedWorld* world = mgr.createWorld("W");
CHECK_EQ(mgr.getWorldCount(), 1u);
CHECK(mgr.getWorld("W") == world);
PagedWorldSection* sec = world->createSection("grid");
CHECK_EQ(world->getSectionCount(), 1u);
CHECK(world->getSection("grid") == sec);
// loadPage:创建并登记,幂等
CHECK(sec->getPage(5) == nullptr);
Page* p5 = sec->loadPage(5, 0);
CHECK_EQ(p5->getID(), 5u);
CHECK(p5->getParentSection() == sec);
CHECK_EQ(sec->getLoadedPageCount(), 1u);
CHECK(sec->loadPage(5, 1) == p5); // 重复 load 复用同一页
CHECK_EQ(sec->getLoadedPageCount(), 1u);
// 第二个页
sec->loadPage(7, 0);
CHECK_EQ(sec->getLoadedPageCount(), 2u);
// unloadPage:移除并释放
sec->unloadPage(5);
CHECK_EQ(sec->getLoadedPageCount(), 1u);
CHECK(sec->getPage(5) == nullptr);
// holdPage:更新 frameLastHeld
sec->holdPage(7, 100);
CHECK_EQ(sec->getPage(7)->getFrameLastHeld(), 100ul);
// 长期未持有的页是卸载候选(frame - lastHeld > 阈值)
Page* p9 = sec->loadPage(9, 0);
unsigned long frame = 100;
CHECK(frame - p9->getFrameLastHeld() > 10); // 100-0 > 10:候选
sec->holdPage(9, 95);
CHECK(!(frame - sec->getPage(9)->getFrameLastHeld() > 10)); // 100-95=5:不候选
// 清空 section
sec->unloadPage(7);
sec->unloadPage(9);
CHECK_EQ(sec->getLoadedPageCount(), 0u);
// 一个世界可有多个 section
world->createSection("other");
CHECK_EQ(world->getSectionCount(), 2u);
// 销毁世界
mgr.destroyWorld("W");
CHECK_EQ(mgr.getWorldCount(), 0u);
CHECK(mgr.getWorld("W") == nullptr);
return lab::report("08-1 分页概述");
}
本机实测(Apple clang 16,clang++ -std=c++20):编译通过(-Wall 零警告),断言 20/20 通过。
小结
- Paging 是
PageManager → PagedWorld → PagedWorldSection → Page的四层嵌套树。 Page在分区内 ID 唯一,frameLastHeld标记最近被持有/加载的帧号。PagedWorldSection用PageMap持有页,loadPage幂等、unloadPage移除释放、holdPage触摸推迟卸载。- 长期未持有的页是卸载候选——策略每帧扫描相机附近页并
touch()。 PagedWorld可含多个分区,各用不同策略;PageManager负责建世界、注册策略与跟踪相机。
阅读导航:上一节:7.3 地形材质 · 下一节:8.2 分页策略 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。