12.2 多渲染目标
12.1 的 RenderTexture 是「画进一块纹理」;多渲染目标(MultiRenderTarget) 把这件事推广成「一次画进多块纹理」。延迟着色、GBuffer、SSAO 法线/深度分离全部依赖它。MultiRenderTarget 在 OgreRenderTexture.h 与 RenderTexture 同居一文件,同样继承 RenderTarget,但内部不再持有某个 HardwarePixelBuffer,而是持一张「已绑表面」列表。
本章与站内既有内容的分工
helio 四卷实现过帧缓冲的多个颜色附件绑定;OGRE 的 MultiRenderTarget 是平台无关的「多附件抽象」,真正把附件挂到 FBO/Device 的是各后端的 bindSurfaceImpl。这节讲通用机制与 GL/D3D11 两种后端风格,12.3 讲完整 RTT 使用流程。
MultiRenderTarget:一次画进多块纹理
类注释列出三条约束:所有绑定的表面同尺寸、同位深、且 0 号附件必须被绑定:
// OgreMain/include/OgreRenderTexture.h:MultiRenderTarget 类注释与声明(v14.6.3 源码摘录)
/** This class represents a render target that renders to multiple RenderTextures
at once. Surfaces can be bound and unbound at will, as long as the following constraints
are met:
- All bound surfaces have the same size
- All bound surfaces have the same bit depth
- Target 0 is bound
*/
class _OgreExport MultiRenderTarget: public RenderTarget
{
public:
MultiRenderTarget(const String &name);
/** Bind a surface to a certain attachment point.
@param attachment 0 .. mCapabilities->getNumMultiRenderTargets()-1
@param target RenderTexture to bind.
It does not bind the surface and fails with an exception (ERR_INVALIDPARAMS) if:
- Not all bound surfaces have the same size
- Not all bound surfaces have the same internal format
*/
void bindSurface(size_t attachment, RenderTexture *target);
/** Unbind attachment.
*/
virtual void unbindSurface(size_t attachment)
{
if (attachment < mBoundSurfaces.size())
mBoundSurfaces[attachment] = 0;
unbindSurfaceImpl(attachment);
}
注意 unbindSurface 是 header 内联实现:先把槽位置 0,再调用纯虚的 unbindSurfaceImpl。mBoundSurfaces 是 std::vector<RenderTexture*>,bindSurfaceImpl/unbindSurfaceImpl 都是纯虚,必须由后端实现:
// OgreMain/include/OgreRenderTexture.h:表面列表与后端钩子(v14.6.3 源码摘录)
protected:
BoundSufaceList mBoundSurfaces;
/// Implementation of bindSurface, must be provided
virtual void bindSurfaceImpl(size_t attachment, RenderTexture *target) = 0;
/// Implementation of unbindSurface, must be provided
virtual void unbindSurfaceImpl(size_t attachment) = 0;
bindSurface 与向量扩容
构造函数把优先级设为 OGRE_REND_TO_TEX_RT_GROUP,宽高在没挂任何目标时是 0。bindSurface 的核心动作是:深度格式先清深度池;把 mBoundSurfaces 从当前长度扩充到 attachment+1(中间的槽填 0,允许稀疏绑定);登记表面后调 bindSurfaceImpl:
// OgreMain/src/OgreRenderTexture.cpp:MultiRenderTarget 构造与 bindSurface(v14.6.3 源码摘录)
MultiRenderTarget::MultiRenderTarget(const String &name)
{
mPriority = OGRE_REND_TO_TEX_RT_GROUP;
mName = name;
/// Width and height is unknown with no targets attached
mWidth = mHeight = 0;
}
void MultiRenderTarget::bindSurface(size_t attachment, RenderTexture* target)
{
if(PixelUtil::isDepth(target->suggestPixelFormat()))
setDepthBufferPool(RBP_NONE); // unbinds any previously bound depth render buffer
for (size_t i = mBoundSurfaces.size(); i <= attachment; ++i)
{
mBoundSurfaces.push_back(0);
}
mBoundSurfaces[attachment] = target;
bindSurfaceImpl(attachment, target);
}
| 动作 | 含义 |
|---|---|
setDepthBufferPool(RBP_NONE) | 绑定深度纹理时清掉之前绑的深度缓冲池(深度目标自带深度,不再共享) |
push_back(0) 循环 | 稀疏附件(如直接绑 3 号)自动补空槽 |
mBoundSurfaces[attachment] = target | 登记表面,之后 getBoundSurfaceList() 可查 |
bindSurfaceImpl(...) | 虚调用,交给后端真正把附件挂到图形 API |
copyContentsToMemory 直接拒绝:多目标没有单一像素布局,无法读回:
// OgreMain/src/OgreRenderTexture.cpp:MultiRenderTarget::copyContentsToMemory(v14.6.3 源码摘录)
void MultiRenderTarget::copyContentsToMemory(const Box& src, const PixelBox &dst, FrameBuffer buffer)
{
OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
"Cannot get MultiRenderTargets pixels",
"MultiRenderTarget::copyContentsToMemory");
}
后端绑定:GL 与 D3D11 的两种做法
GL 后端(GLSupport 的 GLMultiRenderTarget)把每个 surface 绑到同一个 FBO,宽高、FSAA 从该 FBO 取回:
// RenderSystems/GLSupport/src/OgreGLMultiRenderTarget.cpp:bindSurfaceImpl(v14.6.3 源码摘录,已删减)
void GLMultiRenderTarget::bindSurfaceImpl(size_t attachment, RenderTexture *target)
{
/// Check if the render target is in the rendertarget->FBO map
auto fbobj = dynamic_cast<GLRenderTarget*>(target)->getFBO();
assert(fbobj);
fbo->setRenderTargetPool(mDepthBufferPoolId);
fbo->bindSurface(attachment, fbobj->getSurface(0));
// Set width and height
mWidth = fbo->getWidth();
mHeight = fbo->getHeight();
mFSAA = fbo->getFSAA();
}
D3D11 后端先检查已绑目标尺寸是否一致,深度格式走 attachDepthBuffer,普通格式存 RenderTargetView 并维护 mNumberOfViews:
// RenderSystems/Direct3D11/src/OgreD3D11MultiRenderTarget.cpp:bindSurfaceImpl(v14.6.3 源码摘录,已删减)
void D3D11MultiRenderTarget::bindSurfaceImpl(size_t attachment, RenderTexture *target)
{
assert(attachment<OGRE_MAX_MULTIPLE_RENDER_TARGETS);
D3D11RenderTarget* d3d11RenderTarget = dynamic_cast<D3D11RenderTarget*>(target);
/// Find first non-null target
int y;
for(y=0; y<OGRE_MAX_MULTIPLE_RENDER_TARGETS && !mRenderTargets[y]; ++y) ;
if(y!=OGRE_MAX_MULTIPLE_RENDER_TARGETS)
{
/// If there is another target bound, compare sizes
if(mRenderTargets[y]->getWidth() != target->getWidth() ||
mRenderTargets[y]->getHeight() != target->getHeight())
{
OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "MultiRenderTarget surfaces are not of same size");
}
}
mRenderTargets[attachment] = target;
if(PixelUtil::isDepth(target->suggestPixelFormat()))
{
attachDepthBuffer(target->getDepthBuffer());
return;
}
mRenderTargetViews[attachment] =
d3d11RenderTarget ? d3d11RenderTarget->getRenderTargetView() : NULL;
if(mNumberOfViews < OGRE_MAX_MULTIPLE_RENDER_TARGETS)
mNumberOfViews++;
}
| 后端 | 绑定策略 | 上限 |
|---|---|---|
| GL | 共享 GLFrameBufferObjectCommon,bindSurface(attachment, surface) | 由硬件 GL_MAX_COLOR_ATTACHMENTS 决定 |
| D3D11 | mRenderTargets[] 数组 + mRenderTargetViews[],深度单独走 attachDepthBuffer | OGRE_MAX_MULTIPLE_RENDER_TARGETS = 8(OgreConfig.h:63) |
两处都在 unbindSurfaceImpl 里把对应槽清掉,并重新从 FBO 取宽高(GL)或减 mNumberOfViews(D3D11)。
源码定位
OgreMain/include/OgreRenderTexture.h:类注释与约束(63-69)、class _OgreExport MultiRenderTarget(70-120)、内联unbindSurface(85-92)、mBoundSurfaces(112)、纯虚bindSurfaceImpl/unbindSurfaceImpl(115-117)OgreMain/src/OgreRenderTexture.cpp:构造(63-69)、bindSurface(70-82)、copyContentsToMemory(85-90)OgreMain/include/OgreConfig.h:OGRE_MAX_MULTIPLE_RENDER_TARGETS 8(63)RenderSystems/GLSupport/src/OgreGLMultiRenderTarget.cpp:bindSurfaceImpl(43-55)、unbindSurfaceImpl(57-65)RenderSystems/Direct3D11/src/OgreD3D11MultiRenderTarget.cpp:bindSurfaceImpl(53-88)、unbindSurfaceImpl(90-99)
上游引用固定到 OGRE v14.6.3 源码 。
可运行产物:C++ 最小验证
下面用纯 C++ 复现 MultiRenderTarget 的机制:构造时宽高为 0、优先级进 REND_TO_TEX 组;bindSurface 的深度池清理与向量扩容;unbindSurface 的槽位置 0;copyContentsToMemory 拒绝读回;GL 风格共享 FBO 取宽高与 D3D11 风格尺寸校验 / view 计数 / 8 附件上限。它与 src/12-2-multi-render-target.cpp 逐字节一致:
// file: src/12-2-multi-render-target.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/12-2-multi-render-target.cpp -o bin/12-2-multi-render-target && ./bin/12-2-multi-render-target
//
// 复现 MultiRenderTarget 多渲染目标(对应 OgreMain/include/OgreRenderTexture.h 63-120、
// OgreMain/src/OgreRenderTexture.cpp 63-90、
// RenderSystems/GLSupport/src/OgreGLMultiRenderTarget.cpp 43-65 与
// RenderSystems/Direct3D11/src/OgreD3D11MultiRenderTarget.cpp 53-99):
// 构造时优先级进 REND_TO_TEX 组、宽高未知(0);
// bindSurface 对深度格式先清深度池,再把向量扩充到 attachment+1 后登记 surface,
// 最后调纯虚 bindSurfaceImpl 交给后端;unbindSurface 把槽位置 0 后调 unbindSurfaceImpl;
// copyContentsToMemory 拒绝读回(真实代码抛 ERR_INVALIDPARAMS)。
// 两个后端实现对照:GL 风格把 surface 绑到共享 FBO 并从 FBO 取宽高/FSAA;
// D3D11 风格比较已绑 surface 的尺寸、深度格式走深度缓冲、普通格式存 RenderTargetView
// 并维护 mNumberOfViews,attachment 不能超过 OGRE_MAX_MULTIPLE_RENDER_TARGETS(8)。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"
#include <cstdint>
#include <cstddef>
#include <string>
#include <vector>
namespace ogre_repro {
enum PixelFormat : uint8_t { PF_UNKNOWN, PF_A8R8G8B8, PF_DEPTH24_STENCIL8 };
// 对应 PixelUtil::isDepth 的简化
bool isDepthFormat(PixelFormat f) { return f == PF_DEPTH24_STENCIL8; }
// 对应 OgreConfig.h:63
constexpr size_t OGRE_MAX_MULTIPLE_RENDER_TARGETS = 8;
// RenderTarget 最小子集(宽/高/优先级/深度缓冲池)
class RenderTarget {
public:
uint32_t getWidth() const { return mWidth; }
uint32_t getHeight() const { return mHeight; }
uint8_t getPriority() const { return mPriority; }
uint16_t getDepthBufferPoolId() const { return mDepthBufferPoolId; }
void setDepthBufferPool(uint16_t pool) { mDepthBufferPoolId = pool; }
protected:
uint32_t mWidth = 0, mHeight = 0;
uint8_t mPriority = 0;
uint16_t mDepthBufferPoolId = 1; // 对应 RBP_DEFAULT(OgreCommon.h:138)
};
// RenderTexture 最小子集(12-1 已见完整版)
class RenderTexture : public RenderTarget {
public:
RenderTexture(uint32_t w, uint32_t h, PixelFormat fmt) : mFormat(fmt) {
mPriority = OGRE_REND_TO_TEX_RT_GROUP;
mWidth = w;
mHeight = h;
}
PixelFormat suggestPixelFormat() const { return mFormat; }
enum : uint8_t { OGRE_REND_TO_TEX_RT_GROUP = 2 };
private:
PixelFormat mFormat;
};
// 对应 MultiRenderTarget(OgreRenderTexture.h 70-120)
class MultiRenderTarget : public RenderTarget {
public:
explicit MultiRenderTarget(const std::string& name) : mName(name) {
mPriority = OGRE_REND_TO_TEX_RT_GROUP;
/// Width and height is unknown with no targets attached
mWidth = mHeight = 0;
}
virtual ~MultiRenderTarget() = default;
// 对应 bindSurface(OgreRenderTexture.cpp 70-82)
void bindSurface(size_t attachment, RenderTexture* target) {
if (isDepthFormat(target->suggestPixelFormat()))
setDepthBufferPool(RBP_NONE); // unbinds any previously bound depth render buffer
for (size_t i = mBoundSurfaces.size(); i <= attachment; ++i) {
mBoundSurfaces.push_back(nullptr);
}
mBoundSurfaces[attachment] = target;
bindSurfaceImpl(attachment, target);
}
// 对应 unbindSurface(OgreRenderTexture.h 85-92)
virtual void unbindSurface(size_t attachment) {
if (attachment < mBoundSurfaces.size())
mBoundSurfaces[attachment] = nullptr;
unbindSurfaceImpl(attachment);
}
// 对应 copyContentsToMemory(OgreRenderTexture.cpp 85-90)
bool copyContentsToMemory() {
mCopyRejected = true; // 真实代码抛 Exception::ERR_INVALIDPARAMS
return false;
}
bool getCopyRejected() const { return mCopyRejected; }
const std::vector<RenderTexture*>& getBoundSurfaceList() const { return mBoundSurfaces; }
RenderTexture* getBoundSurface(size_t index) { return mBoundSurfaces.at(index); }
size_t getBoundCount() const { return mBoundSurfaces.size(); }
const std::string& getName() const { return mName; }
enum : uint8_t { OGRE_REND_TO_TEX_RT_GROUP = 2 }; // OgreRenderTarget.h:40
enum : uint16_t { RBP_NONE = 0 }; // OgreCommon.h:136
protected:
std::vector<RenderTexture*> mBoundSurfaces;
virtual void bindSurfaceImpl(size_t attachment, RenderTexture* target) = 0;
virtual void unbindSurfaceImpl(size_t attachment) = 0;
private:
std::string mName;
bool mCopyRejected = false;
};
// 对应 GLMultiRenderTarget(OgreGLMultiRenderTarget.cpp 43-65):
// surface 绑到共享 FBO,宽高/FSAA 从 FBO 取
class GLMultiRenderTarget : public MultiRenderTarget {
public:
explicit GLMultiRenderTarget(const std::string& name) : MultiRenderTarget(name) {}
protected:
void bindSurfaceImpl(size_t attachment, RenderTexture* target) override {
// 对应 fbobj->getFBO() 与 fbo->bindSurface(attachment, fbobj->getSurface(0))
(void)attachment;
mFboBound = true;
mWidth = target->getWidth(); // 对应 fbo->getWidth()
mHeight = target->getHeight(); // 对应 fbo->getHeight()
mFboSurfaces++;
}
void unbindSurfaceImpl(size_t attachment) override {
(void)attachment;
if (mFboSurfaces > 0) mFboSurfaces--;
}
public:
bool getFboBound() const { return mFboBound; }
size_t getFboSurfaces() const { return mFboSurfaces; }
private:
bool mFboBound = false;
size_t mFboSurfaces = 0;
};
// 对应 D3D11MultiRenderTarget(OgreD3D11MultiRenderTarget.cpp 53-99):
// 尺寸一致性检查 + 深度格式走深度缓冲 + RenderTargetView 计数
class D3D11MultiRenderTarget : public MultiRenderTarget {
public:
explicit D3D11MultiRenderTarget(const std::string& name)
: MultiRenderTarget(name), mRenderTargetViews(OGRE_MAX_MULTIPLE_RENDER_TARGETS, 0),
mRenderTargets(OGRE_MAX_MULTIPLE_RENDER_TARGETS, nullptr) {}
protected:
void bindSurfaceImpl(size_t attachment, RenderTexture* target) override {
if (attachment >= OGRE_MAX_MULTIPLE_RENDER_TARGETS) {
mTooManyAttachments = true;
return;
}
/// Find first non-null target
size_t y = 0;
for (; y < OGRE_MAX_MULTIPLE_RENDER_TARGETS && !mRenderTargets[y]; ++y) {}
if (y != OGRE_MAX_MULTIPLE_RENDER_TARGETS) {
/// If there is another target bound, compare sizes
if (mRenderTargets[y]->getWidth() != target->getWidth() ||
mRenderTargets[y]->getHeight() != target->getHeight()) {
mSizeMismatch = true; // 真实代码抛 Exception::ERR_INVALIDPARAMS
return;
}
}
mRenderTargets[attachment] = target;
if (isDepthFormat(target->suggestPixelFormat())) {
mDepthBound = true; // 对应 attachDepthBuffer(target->getDepthBuffer())
return;
}
mRenderTargetViews[attachment] = 1; // 对应 getRenderTargetView()
if (mNumberOfViews < OGRE_MAX_MULTIPLE_RENDER_TARGETS)
mNumberOfViews++;
mUpdated++;
}
void unbindSurfaceImpl(size_t attachment) override {
if (attachment >= OGRE_MAX_MULTIPLE_RENDER_TARGETS) return;
mRenderTargetViews[attachment] = 0;
if (mNumberOfViews > 0) mNumberOfViews--;
mUpdated++;
}
public:
bool getSizeMismatch() const { return mSizeMismatch; }
bool getTooManyAttachments() const { return mTooManyAttachments; }
bool getDepthBound() const { return mDepthBound; }
size_t getNumberOfViews() const { return mNumberOfViews; }
size_t getUpdated() const { return mUpdated; }
private:
std::vector<int> mRenderTargetViews;
std::vector<RenderTexture*> mRenderTargets;
size_t mNumberOfViews = 0;
size_t mUpdated = 0;
bool mSizeMismatch = false;
bool mTooManyAttachments = false;
bool mDepthBound = false;
};
} // namespace ogre_repro
int main() {
using namespace ogre_repro;
// 两块同尺寸颜色纹理 + 一块深度纹理
RenderTexture colorA(256, 256, PF_A8R8G8B8);
RenderTexture colorB(256, 256, PF_A8R8G8B8);
RenderTexture colorC(512, 512, PF_A8R8G8B8); // 尺寸不一致
RenderTexture depthBuf(256, 256, PF_DEPTH24_STENCIL8);
// 构造:宽高未知(0),优先级进 REND_TO_TEX 组,默认深度池 RBP_DEFAULT(1)
GLMultiRenderTarget gl("MRT/GL");
CHECK(gl.getWidth() == 0);
CHECK(gl.getHeight() == 0);
CHECK(gl.getPriority() == MultiRenderTarget::OGRE_REND_TO_TEX_RT_GROUP);
CHECK(gl.getDepthBufferPoolId() == 1);
CHECK(gl.getName() == "MRT/GL");
// bindSurface:向量扩充 + 登记 + bindSurfaceImpl
gl.bindSurface(0, &colorA);
CHECK(gl.getBoundCount() == 1);
CHECK(gl.getBoundSurface(0) == &colorA);
CHECK(gl.getWidth() == 256); // GL 风格从 FBO 取宽高
CHECK(gl.getHeight() == 256);
CHECK(gl.getFboBound());
CHECK(gl.getFboSurfaces() == 1);
// 绑到 attachment=3:中间槽自动补 0
gl.bindSurface(3, &colorB);
CHECK(gl.getBoundCount() == 4);
CHECK(gl.getBoundSurface(1) == nullptr);
CHECK(gl.getBoundSurface(2) == nullptr);
CHECK(gl.getBoundSurface(3) == &colorB);
CHECK(gl.getFboSurfaces() == 2);
// 深度格式绑定:深度缓冲池被清为 RBP_NONE,向量扩到 attachment+1
gl.bindSurface(4, &depthBuf);
CHECK(gl.getDepthBufferPoolId() == MultiRenderTarget::RBP_NONE);
CHECK(gl.getBoundCount() == 5);
// unbindSurface:槽位置 0,向量长度不变
gl.unbindSurface(3);
CHECK(gl.getBoundSurface(3) == nullptr);
CHECK(gl.getBoundCount() == 5);
CHECK(gl.getFboSurfaces() == 2);
// 同尺寸多目标绑定:D3D11 维护 RenderTargetView 计数
D3D11MultiRenderTarget d3d("MRT/D3D11");
d3d.bindSurface(0, &colorA);
d3d.bindSurface(1, &colorB);
CHECK(d3d.getNumberOfViews() == 2);
CHECK(d3d.getBoundSurface(0) == &colorA);
CHECK(d3d.getBoundSurface(1) == &colorB);
CHECK(d3d.getUpdated() == 2);
// unbind 一个:view 数减一
d3d.unbindSurface(1);
CHECK(d3d.getNumberOfViews() == 1);
CHECK(d3d.getBoundSurface(1) == nullptr);
// 深度目标走深度缓冲路径,不计入 view
d3d.bindSurface(1, &depthBuf);
CHECK(d3d.getNumberOfViews() == 1);
CHECK(d3d.getDepthBound());
// 尺寸不一致被拒绝(真实代码抛 ERR_INVALIDPARAMS)
D3D11MultiRenderTarget bad("MRT/size");
bad.bindSurface(0, &colorA);
bad.bindSurface(1, &colorC);
CHECK(bad.getSizeMismatch());
// attachment 上限 OGRE_MAX_MULTIPLE_RENDER_TARGETS(8)
D3D11MultiRenderTarget capped("MRT/cap");
capped.bindSurface(8, &colorA);
CHECK(capped.getTooManyAttachments());
// copyContentsToMemory 拒绝读回
CHECK(!gl.copyContentsToMemory());
CHECK(gl.getCopyRejected());
return lab::report("12-2 多渲染目标");
}
本机实测(Apple clang 16,clang++ -std=c++20 -Wall -I.):编译零警告,断言 33/33 通过。
小结
MultiRenderTarget是画进多块纹理的RenderTarget,持std::vector<RenderTexture*> mBoundSurfaces,约束同尺寸、同位深、0 号必绑。- 构造时优先级进
OGRE_REND_TO_TEX_RT_GROUP,无目标时宽高为 0。 bindSurface对深度格式先setDepthBufferPool(RBP_NONE),再把向量扩到attachment+1(稀疏绑定自动补空槽),最后调纯虚bindSurfaceImpl。unbindSurface内联在 header:槽位置 0 后调unbindSurfaceImpl。- GL 后端绑共享 FBO 并从 FBO 取宽高/FSAA;D3D11 后端校验尺寸一致、深度走深度缓冲、普通格式计
mNumberOfViews,上限 8。 copyContentsToMemory抛ERR_INVALIDPARAMS,多目标不能读回像素。
阅读导航:上一节:12.1 渲染纹理 · 下一节:12.3 RTT 使用流程 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。