10.2 Sampler 采样器
上一节说过 TextureUnitState 的采样参数不直接存,而是存进 Sampler。这节把镜头对准 Sampler 本身——它从 OgreTextureUnitState.h 第 54 行开始,是「纹理怎么被采样」的完整答案。采样状态可以被多个纹理单元共享(SamplerPtr 共享指针),这是它独立成类的根本原因。
本章与站内既有内容的分工
helio 四卷的 SamplerDesc 只装过滤与寻址;OGRE 的 Sampler 更完整:三档过滤粒度、比较模式(阴影 PCF)、边界色、各向异性与 mip 偏置全在里面,且与 TextureUnitState 的委托关系是重点。
Sampler:过滤的三档粒度
Sampler 的类注释点明它决定「纹理数据如何被取回」,FilterOptions 与 TextureAddressingMode 是两个核心概念。类定义的开头先给出寻址结构:
// OgreMain/include/OgreTextureUnitState.h:Sampler 类定义(v14.6.3 源码摘录,已删减)
class _OgreExport Sampler {
public:
/** Texture addressing mode for each texture coordinate. */
struct UVWAddressingMode
{
TextureAddressingMode u, v, w;
};
/// must be created through TextureManager
Sampler();
virtual ~Sampler();
void setFiltering(TextureFilterOptions filterType);
void setFiltering(FilterType ftype, FilterOptions opts);
void setFiltering(FilterOptions minFilter, FilterOptions magFilter, FilterOptions mipFilter);
/// Get the texture filtering for the given type.
FilterOptions getFiltering(FilterType ftype) const;
过滤设置有三档粒度,这是 Sampler 最值得讲的设计:
| 重载 | 用法 | 场景 |
|---|---|---|
setFiltering(TextureFilterOptions) | 一次定三通道(NONE/BILINEAR/TRILINEAR/ANISOTROPIC) | 快捷接口 |
setFiltering(FilterType, FilterOptions) | 只改 min/mag/mip 中的一个 | 精细调单一通道 |
setFiltering(min, mag, mip) | 三个通道分别给 FO_* 值 | 完全控制 |
寻址模式同理,也有「一次定全部」与「逐坐标定」两种:
// OgreMain/include/OgreTextureUnitState.h:Sampler 寻址接口(v14.6.3 源码摘录,已删减)
const UVWAddressingMode& getAddressingMode() const { return mAddressMode; }
void setAddressingMode(TextureAddressingMode tam) { setAddressingMode({tam, tam, tam}); }
void setAddressingMode(TextureAddressingMode u, TextureAddressingMode v,
TextureAddressingMode w)
{
setAddressingMode({u, v, w});
}
void setAddressingMode(const UVWAddressingMode& uvw);
setAddressingMode(tam) 把同一个模式填进 u/v/w 三个坐标({tam, tam, tam} 的初始化列表展开),单坐标超界(uv > 1.0)时的行为由 TextureAddressingMode 决定:默认 TAM_WRAP 重复采样。
各向异性、偏置与比较模式
其余采样参数都是「一个 setter 配一个 getter」,且每个 setter 都会置 mDirty:
// OgreMain/include/OgreTextureUnitState.h:Sampler 采样参数(v14.6.3 源码摘录,已删减)
void setAnisotropy(unsigned int maxAniso)
{
mMaxAniso = maxAniso;
mDirty = true;
}
unsigned int getAnisotropy() const { return mMaxAniso; }
...
void setMipmapBias(float bias)
{
mMipmapBias = bias;
mDirty = true;
}
...
void setCompareEnabled(bool enabled)
{
mCompareEnabled = enabled;
mDirty = true;
}
bool getCompareEnabled() const { return mCompareEnabled; }
void setCompareFunction(CompareFunction function)
{
mCompareFunc = function;
mDirty = true;
}
...
void setBorderColour(const ColourValue& colour)
{
mBorderColour = colour;
mDirty = true;
}
mDirty 的用途注释写得很清楚:// flag for derived classes to sync with implementation——后端(如 D3D11 的 D3D11Sampler)在同步本机采样器对象时靠它判断是否需要重建。setCompareEnabled 配合 setCompareFunction 做深度纹理比较(阴影贴图硬件 PCF):启用后采样返回「采样值与参考值比较的结果」而不是采样值本身。
成员清单在类尾:
// OgreMain/include/OgreTextureUnitState.h:Sampler 保护成员(v14.6.3 源码摘录)
protected:
ColourValue mBorderColour;
/// Texture anisotropy.
unsigned int mMaxAniso;
/// Mipmap bias (always float, not Real).
float mMipmapBias;
UVWAddressingMode mAddressMode;
/// Texture filtering - minification.
FilterOptions mMinFilter;
/// Texture filtering - magnification.
FilterOptions mMagFilter;
/// Texture filtering - mipmapping.
FilterOptions mMipFilter;
CompareFunction mCompareFunc;
bool mCompareEnabled : 1;
bool mDirty : 1; // flag for derived classes to sync with implementation
两个位域布尔(mCompareEnabled : 1 与 mDirty : 1)压缩进一个字节,是 OGRE 对高频小对象的惯用优化。
共享与持有:SamplerPtr
Sampler 的 typedef 紧跟类定义:
// OgreMain/include/OgreTextureUnitState.h:SamplerPtr typedef(v14.6.3 源码摘录)
typedef std::shared_ptr<Sampler> SamplerPtr;
TextureUnitState 通过 SamplerPtr mSampler 持有采样器(10.1 已见 getSampler/setSampler 与 _getLocalSampler)。共享指针让多个纹理单元能指向同一个 Sampler 实例——例如大量物体用同一种三线性采样,只需一份 Sampler 状态,各 TextureUnitState 引用即可,渲染系统对每个纹理单元只需「引用采样器 + 单元编号」。
源码定位
OgreMain/include/OgreTextureUnitState.h:class _OgreExport Sampler(54-225)、UVWAddressingMode(56-60)、三档setFiltering(74-93)、setAnisotropy(144-150)、setMipmapBias(167-175)、setCompareEnabled(183-188)、setBorderColour(202-207)、保护成员(209-225)、typedef std::shared_ptr<Sampler> SamplerPtr(226)
上游引用固定到 OGRE v14.6.3 源码 。
可运行产物:C++ 最小验证
下面用纯 C++ 复现 Sampler 的默认状态、三档过滤与共享持有。它与 src/10-2-sampler.cpp 逐字节一致:
// file: src/10-2-sampler.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/10-2-sampler.cpp -o bin/10-2-sampler && ./bin/10-2-sampler
//
// 复现 Sampler 类(对应 OgreMain/include/OgreTextureUnitState.h 54-226):
// UVWAddressingMode 按 u/v/w 三个坐标存寻址模式,默认 TAM_WRAP;
// 过滤支持三种粒度:TextureFilterOptions 简版、FilterType 单通道、min/mag/mip 详版;
// setAnisotropy/setMipmapBias/setCompareEnabled/setBorderColour 都置 mDirty;
// CompareEnabled+CompareFunction 用于深度纹理硬件 PCF;TextureUnitState 持 SamplerPtr。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"
#include <cstdint>
#include <memory>
#include <string>
namespace ogre_repro {
enum TextureAddressingMode { TAM_WRAP, TAM_MIRROR, TAM_CLAMP, TAM_BORDER };
enum FilterType { FT_MIN, FT_MAG, FT_MIP };
enum FilterOptions { FO_NONE, FO_POINT, FO_LINEAR, FO_ANISOTROPIC };
enum TextureFilterOptions { TFO_NONE, TFO_BILINEAR, TFO_TRILINEAR, TFO_ANISOTROPIC };
enum CompareFunction { CMPF_LESS, CMPF_LESS_EQUAL, CMPF_GREATER, CMPF_GREATER_EQUAL };
struct ColourValue {
float r = 0, g = 0, b = 0, a = 0;
static ColourValue Black() { return ColourValue(); }
static ColourValue White() {
ColourValue c;
c.r = c.g = c.b = c.a = 1.0f;
return c;
}
};
// 对应 Sampler 类(OgreTextureUnitState.h 54-226)
class Sampler {
public:
// 对应 Sampler::UVWAddressingMode
struct UVWAddressingMode {
TextureAddressingMode u, v, w;
};
Sampler() = default;
virtual ~Sampler() = default;
// 简版过滤:按 TextureFilterOptions 一次设定三通道
void setFiltering(TextureFilterOptions filterType) {
switch (filterType) {
case TFO_NONE: mMinFilter = FO_POINT; mMagFilter = FO_POINT; mMipFilter = FO_NONE; break;
case TFO_BILINEAR: mMinFilter = FO_LINEAR; mMagFilter = FO_LINEAR; mMipFilter = FO_POINT; break;
case TFO_TRILINEAR: mMinFilter = FO_LINEAR; mMagFilter = FO_LINEAR; mMipFilter = FO_LINEAR; break;
case TFO_ANISOTROPIC: mMinFilter = FO_ANISOTROPIC; mMagFilter = FO_ANISOTROPIC; mMipFilter = FO_LINEAR; break;
}
mDirty = true;
}
// 单通道版:只改某一个 FilterType
void setFiltering(FilterType ftype, FilterOptions opts) {
if (ftype == FT_MIN) mMinFilter = opts;
else if (ftype == FT_MAG) mMagFilter = opts;
else mMipFilter = opts;
mDirty = true;
}
// 详版:min/mag/mip 分别给
void setFiltering(FilterOptions minFilter, FilterOptions magFilter, FilterOptions mipFilter) {
mMinFilter = minFilter;
mMagFilter = magFilter;
mMipFilter = mipFilter;
mDirty = true;
}
FilterOptions getFiltering(FilterType ftype) const {
if (ftype == FT_MIN) return mMinFilter;
if (ftype == FT_MAG) return mMagFilter;
return mMipFilter;
}
const UVWAddressingMode& getAddressingMode() const { return mAddressMode; }
void setAddressingMode(TextureAddressingMode tam) { setAddressingMode({tam, tam, tam}); }
void setAddressingMode(TextureAddressingMode u, TextureAddressingMode v, TextureAddressingMode w) {
setAddressingMode({u, v, w});
}
void setAddressingMode(const UVWAddressingMode& uvw) {
mAddressMode = uvw;
mDirty = true;
}
void setAnisotropy(unsigned int maxAniso) {
mMaxAniso = maxAniso;
mDirty = true;
}
unsigned int getAnisotropy() const { return mMaxAniso; }
void setMipmapBias(float bias) {
mMipmapBias = bias;
mDirty = true;
}
float getMipmapBias() const { return mMipmapBias; }
void setCompareEnabled(bool enabled) {
mCompareEnabled = enabled;
mDirty = true;
}
bool getCompareEnabled() const { return mCompareEnabled; }
void setCompareFunction(CompareFunction function) {
mCompareFunc = function;
mDirty = true;
}
CompareFunction getCompareFunction() const { return mCompareFunc; }
void setBorderColour(const ColourValue& colour) {
mBorderColour = colour;
mDirty = true;
}
const ColourValue& getBorderColour() const { return mBorderColour; }
bool isDirty() const { return mDirty; }
protected:
ColourValue mBorderColour = ColourValue::Black();
unsigned int mMaxAniso = 1;
float mMipmapBias = 0.0f;
UVWAddressingMode mAddressMode{TAM_WRAP, TAM_WRAP, TAM_WRAP};
FilterOptions mMinFilter = FO_LINEAR;
FilterOptions mMagFilter = FO_LINEAR;
FilterOptions mMipFilter = FO_POINT;
CompareFunction mCompareFunc = CMPF_LESS_EQUAL;
bool mCompareEnabled = false;
bool mDirty = false;
};
using SamplerPtr = std::shared_ptr<Sampler>;
// 简化 TextureUnitState:只演示持 SamplerPtr
class TextureUnitState {
public:
void setSampler(const SamplerPtr& s) { mSampler = s; }
const SamplerPtr& getSampler() const { return mSampler; }
private:
SamplerPtr mSampler;
};
} // namespace ogre_repro
int main() {
using namespace ogre_repro;
Sampler s;
// 默认状态:TAM_WRAP 寻址、线性/点过滤、无比较、各向异性 1、未脏
CHECK(s.getAddressingMode().u == TAM_WRAP);
CHECK(s.getAddressingMode().v == TAM_WRAP);
CHECK(s.getAddressingMode().w == TAM_WRAP);
CHECK(s.getFiltering(FT_MIN) == FO_LINEAR);
CHECK(s.getFiltering(FT_MIP) == FO_POINT);
CHECK(s.getAnisotropy() == 1);
CHECK(!s.getCompareEnabled());
CHECK(!s.isDirty());
// 简版过滤:TRILINEAR 一次设定三通道
s.setFiltering(TFO_TRILINEAR);
CHECK(s.getFiltering(FT_MIN) == FO_LINEAR);
CHECK(s.getFiltering(FT_MAG) == FO_LINEAR);
CHECK(s.getFiltering(FT_MIP) == FO_LINEAR);
CHECK(s.isDirty());
// 单通道版:只改 MAG
s.setFiltering(FT_MAG, FO_ANISOTROPIC);
CHECK(s.getFiltering(FT_MAG) == FO_ANISOTROPIC);
CHECK(s.getFiltering(FT_MIN) == FO_LINEAR); // 未受影响
// 详版:min/mag/mip 分别给
s.setFiltering(FO_POINT, FO_LINEAR, FO_LINEAR);
CHECK(s.getFiltering(FT_MIN) == FO_POINT);
CHECK(s.getFiltering(FT_MAG) == FO_LINEAR);
CHECK(s.getFiltering(FT_MIP) == FO_LINEAR);
// 各向异性 + mipmap bias
s.setAnisotropy(8);
CHECK(s.getAnisotropy() == 8);
s.setMipmapBias(-0.5f);
CHECK(s.getMipmapBias() == -0.5f);
// 深度纹理比较(硬件 PCF)
s.setCompareEnabled(true);
CHECK(s.getCompareEnabled());
s.setCompareFunction(CMPF_LESS);
CHECK(s.getCompareFunction() == CMPF_LESS);
CHECK(s.isDirty());
// 边界色(TAM_BORDER 用)
ColourValue blue;
blue.b = 1.0f;
s.setBorderColour(blue);
CHECK(s.getBorderColour().b == 1.0f);
CHECK(s.getBorderColour().r == 0.0f);
// TextureUnitState 持有 SamplerPtr
SamplerPtr sp = std::make_shared<Sampler>();
sp->setFiltering(TFO_ANISOTROPIC);
TextureUnitState tus;
tus.setSampler(sp);
CHECK(tus.getSampler() == sp);
CHECK(tus.getSampler()->getFiltering(FT_MIN) == FO_ANISOTROPIC);
return lab::report("10-2 Sampler 采样器");
}
本机实测(Apple clang 16,clang++ -std=c++20 -Wall -I.):编译零警告,断言 26/26 通过。
小结
Sampler封装完整采样状态:UVWAddressingMode三坐标寻址、过滤三档粒度、各向异性、mip 偏置、深度比较与边界色。- 过滤提供
TextureFilterOptions简版、FilterType单通道、min/mag/mip详版三种重载。 - 所有修改采样参数的 setter 都会置
mDirty,供后端派生类同步本机采样器。 mCompareEnabled/mCompareFunc实现阴影贴图硬件 PCF:采样返回比较结果而非采样值。SamplerPtr是std::shared_ptr<Sampler>,TextureUnitState通过它共享复用采样器。
阅读导航:上一节:10.1 纹理单元状态 · 下一节:10.3 纹理硬件加载 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。