采样器(Samplers)
下表 描述了采样器状态(枚举类型) 和对应的值,
Enum Name | Valid Values | Description |
---|---|---|
| | Specifies whether the texture coordinates when sampling from a texture are normalized or unnormalized values. |
| | Sets the addressing mode for all texture coordinates. |
| | Sets the addressing mode for an individual texture coordinate. |
| | Sets the magnification and minification filtering modes for texture sampling. |
| | Sets the magnification filtering mode for texture sampling. |
| | Sets the minification filtering modes for texture sampling. |
| | Sets the mipmap filtering mode for texture sampling. If |
| | Sets the comparison test that will be used by the |
<pre style="margin: -0.083em 0.333em 0px 0.5em; font-size: 12.2746px; font-family: Courier, Consolas, monospace; color: rgb(102, 102, 102); line-height: 13.5021px; white-space: pre-wrap; background-color: rgb(241, 245, 249);">enum class coord { normalized, pixel };
enum class filter { nearest, linear };
enum class min_filter { nearest, linear };
enum class mag_filter { nearest, linear };
enum class s_address { clamp_to_zero, clamp_to_edge,
repeat, mirrored_repeat };
enum class t_address { clamp_to_zero, clamp_to_edge,
repeat, mirrored_repeat };
enum class r_address { clamp_to_zero, clamp_to_edge,
repeat, mirrored_repeat };
enum class address { clamp_to_zero, clamp_to_edge,
repeat, mirrored_repeat };
enum class mip_filter { none, nearest, linear };
enum class compare_func { never, less, less_equal, greater,
greater_equal, equal, not_equal, always };
寻址模式中的 clamp_to_zero 跟OpenGL中的clamp-to-boarder类似, 当采样到边界之外的时候, 如果该纹理不包含alpha分量的,其颜色值永远为(0.0, 0.0, 0.0, 1.0), 否则, 该颜色值为(0.0, 0.0, 0.0, 0.0).
下面的例子描述了一个metal 采样器的实现:
struct sampler {
public:
// full version of sampler constructor
template<typename... Ts>
constexpr sampler(Ts... sampler_params){};
private:
};
Ts 就是上面说述的采样器状态的枚举类型. 如果同一个类型的采样器枚举状态被声明多次, 最后一个声明才是有效的。
下面的例子说明了在着色语言中是如何声明采样器的。 关于 buffer(0), texture(0), sampler(3) 的描述会在 Attribute Qualifier to Local Resource 这一章中进行阐述。 注意:生命在着色语言中的采样器不需要这些修饰。
定义在着色语言中的采样器必须被声明为 constexpr, 指向采样器的指针或者引用是不支持的,会引发编译错误。
constexpr sampler s(coord::pixel,
address::clamp_to_zero,
filter::linear);
constexpr sampler a(coord::normalized);
constexpr sampler b(address::repeat);
constexpr sampler s(address::clamp_to_zero,
filter::linear,
compare_func::less);
kernel void
my_kernel(device float4 *p [[ buffer(0) ]],
texture2d<float> img [[ texture(0) ]],
sampler smp [[ sampler(3) ]],
...)
{
...
}
译自:
https://developer.apple.com/library/ios/documentation/Metal/Reference/MetalShadingLanguageGuide/data-types/data-types.html#//apple_ref/doc/uid/TP40014364-CH2-SW1