《OGRE 3D 渲染引擎原理与架构》6.1 Compositor 后处理概念

Compositor 是把最终画面「复合」出来的资源对象:CompositionTechnique 定义技术方案,TextureScope 决定纹理可见范围,CompositionTargetPass 用 InputMode 描述输入来源。源码在 OgreMain 的 OgreCompositor.h 与 OgreCompositionTechnique.h。lab 复现 Compositor→Technique→TargetPass→Pass 的对象树与 IM_PREVIOUS 链语义。

6.1 Compositor 后处理概念

场景渲染完之后,画面往往还要再「过一遍」:模糊、辉光、色调映射、SSAO……OGRE 管这一整套叫 Compositor。它不只做滤镜,还能把多个场景渲染、全屏四边形和中间操作复合成最终结果。这一节先看它最上层的对象模型:Compositor → CompositionTechnique → CompositionTargetPass → CompositionPass 四层树,以及「纹理作用域」和「输入模式」这两个核心概念。

OgreCompositor.h 开头注释一句话点明定位:

Compositors provide the means to flexibly “composite” the final rendering result from multiple scene renders and intermediate operations like rendering fullscreen quads. This makes it possible to apply postfilter effects, HDRI postprocessing, and shadow effects to a Viewport.

也就是说,Compositor 是作用于一个 Viewport 的后期合成配方,而「最终渲染结果」被拆成一系列对 RenderTarget 的操作。

本章与站内既有内容的分工

卷一 ogre-core 的资源系统把「材质脚本」当 Resource 管理;Compositor 是另一类 Resource,但走同一套 ResourceManager 生命线。helio 四卷的 postprocess 是从零实现一个后处理链,本书只解析 OGRE 的对象树与编译流程;cg 图形学三卷讲模糊/辉光背后的数学,这里不重复公式,只看这些效果怎么被组织成 C++ 对象。本章三节(6.1 概念、6.2 通道、6.3 链)自上而下把 Compositor 体系拆完。

Compositor:后处理资源对象

Compositor 是 Resource 的子类,持有多个 CompositionTechnique。之所以有多个 technique,是因为不同显卡能力需要不同实现:一张老卡跑不了高精度版本,就用降级版。创建 technique 的代码极其直白:

// OgreMain/src/OgreCompositor.cpp:Compositor::createTechnique(v14.6.3 源码摘录,已删减)
CompositionTechnique *Compositor::createTechnique()
{
    CompositionTechnique *t = OGRE_NEW CompositionTechnique(this);
    mTechniques.push_back(t);
    mCompilationRequired = true;
    return t;
}

每次增删 technique 都置 mCompilationRequired = true,之后 loadImpl() 或显式 compile() 会「筛出受支持的 technique」:

// OgreMain/src/OgreCompositor.cpp:Compositor::compile(v14.6.3 源码摘录,已删减)
void Compositor::compile()
{
    /// Sift out supported techniques
    mSupportedTechniques.clear();

    for (auto t : mTechniques)
    {
        // Allow texture support with degraded pixel format
        if (t->isSupported(true))
        {
            mSupportedTechniques.push_back(t);
        }
    }

    if (mSupportedTechniques.empty())
        LogManager::getSingleton().logError("Compositor '" + getName() + "' has no supported techniques");

getSupportedTechnique() 只从编译后的 mSupportedTechniques 里取,因此 Compositor 是先描述、后编译的两阶段模型。

CompositionTechnique:技术方案与纹理作用域

CompositionTechnique 是真正的「做法」:它声明本技术需要的局部纹理(中间渲染目标),并维护若干 CompositionTargetPass。纹理有一个作用域概念,决定谁看得到它:

// OgreMain/include/OgreCompositionTechnique.h:CompositionTechnique::TextureScope / TextureDefinition(v14.6.3 源码摘录,已删减)
        enum TextureScope { 
            //Local texture - only available to the compositor passes in this technique
            TS_LOCAL, 
            //Chain texture - available to the other compositors in the chain
            TS_CHAIN, 
            //Global texture - available to everyone in every scope
            TS_GLOBAL 
        };

        class TextureDefinition : public CompositorInstAlloc
        {
        public:
            String name;
            String refCompName; //If a reference, the name of the compositor being referenced
            String refTexName;  //If a reference, the name of the texture in the compositor being referenced
            uint32 width;       // 0 means adapt to target width
            uint32 height;      // 0 means adapt to target height
            uint32 depth;       // only used for 2d_array textures
            TextureType type;   // either 2d, 2d_array or cubic
            float widthFactor;  // multiple of target width to use (if width = 0)
            float heightFactor; // multiple of target height to use (if height = 0)
            PixelFormatList formatList; // more than one means MRT
            uint8 fsaa;         // FSAA level; 1 = determine from main target (if render_scene), 0 = disable
            bool hwGammaWrite;  // Do sRGB gamma correction on write (only 8-bit per channel formats) 
            uint16 depthBufferId;//Depth Buffer's pool ID. (unrelated to "pool" variable below)
            bool pooled;        // whether to use pooled textures for this one
            TextureScope scope; // Which scope has access to this texture
        };

三级作用域是合成链的分层可见性规则:

作用域可见范围典型用途
TS_LOCAL仅本 technique 的通道中间模糊缓冲,用完即弃
TS_CHAIN同一条 CompositorChain 里的其他 compositor辉光结果传给下一位后处理
TS_GLOBAL所有人(跨 scope)共享噪声贴图、LUT

宽度/高度为 0 表示「适配目标尺寸」,配合 widthFactor/heightFactor 按比例缩放——这是后处理里常见的「半分辨率模糊缓冲」的声明来源。

CompositionTargetPass:target operation 的蓝图

CompositionTechnique 里真正干活的单元是 CompositionTargetPass(一个「对某个 RenderTarget 的一次渲染」),它通过 InputMode 声明输入从哪来:

// OgreMain/include/OgreCompositionTargetPass.h:CompositionTargetPass::InputMode / createPass(v14.6.3 源码摘录,已删减)
        enum InputMode
        {
            IM_NONE,        /// No input
            IM_PREVIOUS     /// Output of previous Composition in chain
        };
...
        CompositionPass *createPass(CompositionPass::PassType type = CompositionPass::PT_RENDERQUAD);

IM_NONE 表示该 target pass 自己画(通常是首帧场景),IM_PREVIOUS 表示输入来自链上上一个 compositor 的输出——正是靠它,辉光结果才能喂给下一个后处理。每个 target pass 用 setOutputName 给自己的输出命名,名字会被 IM_PREVIOUS 的后续 pass 消费。createPass 进一步往下创建 CompositionPass(6.2 详述)。每个 technique 还有一个唯一的输出 target pass(getOutputTargetPass()),即最终合并到 viewport 的那一次操作。

源码定位

上游引用固定到 OGRE v14.6.3 源码 。

可运行产物:C++ 最小验证

下面用纯 C++ 复现 Compositor 的四层对象树与作用域/输入语义。它与 src/06-1-compositor-overview.cpp 逐字节一致:

// file: src/06-1-compositor-overview.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/06-1-compositor-overview.cpp -o bin/06-1-compositor-overview && ./bin/06-1-compositor-overview
//
// 复现 Compositor 的对象树与作用域语义(对应 OgreCompositor.h 49-174、
// OgreCompositionTechnique.h 46-190、OgreCompositionTargetPass.h 47-182):
//   Compositor::createTechnique 向 mTechniques 追加并置 mCompilationRequired=true;
//   CompositionTechnique 持有 TextureDefinition 列表与 TargetPass 列表,输出目标 pass 单例;
//   CompositionTargetPass::InputMode IM_NONE/IM_PREVIOUS,IM_PREVIOUS 引用链上前一个
//   compositor 的输出;TextureScope TS_LOCAL/TS_CHAIN/TS_GLOBAL 决定纹理可见范围。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"

#include <cstdint>
#include <string>
#include <vector>

namespace ogre_repro {

enum class TextureScope : uint8_t { TS_LOCAL, TS_CHAIN, TS_GLOBAL };

enum class InputMode : uint8_t { IM_NONE, IM_PREVIOUS };

struct TextureDefinition {
  std::string name;
  TextureScope scope;
  uint32_t width = 0;  // 0 表示适配目标尺寸
  uint32_t height = 0;
  TextureDefinition() : scope(TextureScope::TS_LOCAL) {}
  TextureDefinition(const std::string& n, TextureScope s) : name(n), scope(s) {}
};

class CompositionPass;
class CompositionTargetPass {
 public:
  explicit CompositionTargetPass(bool shadowsEnabled)
      : mInputMode(InputMode::IM_NONE), mShadowsEnabled(shadowsEnabled) {}
  void setInputMode(InputMode m) { mInputMode = m; }
  InputMode getInputMode() const { return mInputMode; }
  void setOutputName(const std::string& n) { mOutputName = n; }
  const std::string& getOutputName() const { return mOutputName; }
  bool getShadowsEnabled() const { return mShadowsEnabled; }
  size_t getNumPasses() const { return mPasses.size(); }
  CompositionPass* createPass();
  std::vector<CompositionPass*> mPasses;

 private:
  InputMode mInputMode;
  std::string mOutputName;
  bool mShadowsEnabled;
};

class CompositionPass {
 public:
  explicit CompositionPass(CompositionTargetPass* parent) : mParent(parent) {}
  CompositionTargetPass* getParent() const { return mParent; }

 private:
  CompositionTargetPass* mParent;
};

class CompositionTechnique {
 public:
  CompositionTechnique() : mOutputTarget(nullptr) {}
  TextureDefinition* createTextureDefinition(const std::string& name, TextureScope scope) {
    mTextureDefinitions.push_back(new TextureDefinition(name, scope));
    return mTextureDefinitions.back();
  }
  size_t getNumTextureDefinitions() const { return mTextureDefinitions.size(); }
  CompositionTargetPass* createTargetPass(bool shadowsEnabled) {
    auto* tp = new CompositionTargetPass(shadowsEnabled);
    mTargetPasses.push_back(tp);
    return tp;
  }
  void setOutputTargetPass(CompositionTargetPass* tp) { mOutputTarget = tp; }
  CompositionTargetPass* getOutputTargetPass() const { return mOutputTarget; }
  size_t getNumTargetPasses() const { return mTargetPasses.size(); }
  std::vector<TextureDefinition*> mTextureDefinitions;
  std::vector<CompositionTargetPass*> mTargetPasses;
  CompositionTargetPass* mOutputTarget;
};

class Compositor {
 public:
  size_t getNumTechniques() const { return mTechniques.size(); }
  CompositionTechnique* createTechnique() {
    auto* t = new CompositionTechnique();
    mTechniques.push_back(t);
    mCompilationRequired = true;  // 对应 Compositor::createTechnique
    return t;
  }
  bool isCompilationRequired() const { return mCompilationRequired; }
  std::vector<CompositionTechnique*> mTechniques;
  bool mCompilationRequired = false;
};

CompositionPass* CompositionTargetPass::createPass() {
  mPasses.push_back(new CompositionPass(this));
  return mPasses.back();
}

}  // namespace ogre_repro

int main() {
  using namespace ogre_repro;

  Compositor comp;
  CHECK_EQ(comp.getNumTechniques(), size_t(0));
  auto* tech = comp.createTechnique();
  CHECK_EQ(comp.getNumTechniques(), size_t(1));
  CHECK(comp.isCompilationRequired());

  // 纹理作用域:默认 TS_LOCAL,可声明为 CHAIN/GLOBAL
  auto* local = tech->createTextureDefinition("rt0", TextureScope::TS_LOCAL);
  auto* chain = tech->createTextureDefinition("shared", TextureScope::TS_CHAIN);
  CHECK_EQ(tech->getNumTextureDefinitions(), size_t(2));
  CHECK(local->scope == TextureScope::TS_LOCAL);
  CHECK(chain->scope == TextureScope::TS_CHAIN);
  CHECK_EQ(local->width, uint32_t(0));  // 默认适配目标宽度

  // 中间 target pass 与输出 target pass
  auto* tp1 = tech->createTargetPass(true);
  tp1->setInputMode(InputMode::IM_PREVIOUS);  // 输入 = 链上前一个 compositor 的输出
  tp1->setOutputName("shared");
  auto* out = tech->createTargetPass(false);
  tech->setOutputTargetPass(out);
  CHECK_EQ(tech->getNumTargetPasses(), size_t(2));
  CHECK(tp1->getInputMode() == InputMode::IM_PREVIOUS);
  CHECK(out->getInputMode() == InputMode::IM_NONE);
  CHECK(tech->getOutputTargetPass() == out);
  CHECK(!out->getShadowsEnabled());

  // target pass 创建通道
  tp1->createPass();
  tp1->createPass();
  CHECK_EQ(tp1->getNumPasses(), size_t(2));
  CHECK(tp1->mPasses[0]->getParent() == tp1);

  // IM_PREVIOUS 的链语义:未命名输出为空串
  Compositor comp2;
  auto* tech2 = comp2.createTechnique();
  auto* tp2 = tech2->createTargetPass(true);
  tp2->setInputMode(InputMode::IM_PREVIOUS);
  CHECK(tp2->getOutputName().empty());

  return lab::report("06-1 Compositor 后处理概念");
}

本机实测(Apple clang 16,clang++ -std=c++20):编译通过(-Wall 零警告),断言 15/15 通过。

小结

  • Compositor 是后处理配方资源,持有多个 CompositionTechnique,compile() 把受支持的 technique 筛入 mSupportedTechniques。
  • CompositionTechnique::TextureScope 用 TS_LOCAL/TS_CHAIN/TS_GLOBAL 控制中间纹理的可见范围。
  • CompositionTargetPass::InputMode 只有 IM_NONE(自己画)与 IM_PREVIOUS(吃链上前一个 compositor 的输出)两种。
  • 每个 technique 有一个唯一输出 target pass;target pass 再向下创建 CompositionPass。

阅读导航:上一节:5.3 QueuedRenderable 与提交 · 下一节:6.2 CompositionPass 后处理通道 。

继续阅读

探索更多技术文章

浏览归档,发现更多关于系统设计、工具链和工程实践的内容。

全部文章 返回首页

「books」更多文章

  1. 《大规模分布式计算深入解析》15.3 落地:从 Neo4j 到自研图引擎
  2. 《大规模分布式计算深入解析·图计算与图数据库内核》 · 目录与学习路径(45 篇)
  3. 《大规模分布式计算深入解析》15.2 十一卷主线:从切分到图