Metal 着色语言编程指南 十一

采样器(Samplers)

 Metal 着色语言中, 采样器类型决定了如何对纹理进行采样.  Metal 框架允许你创建一个采样器对象, 并将其作为参数传递给graphics 或者 kernel 函数,  除了在Metal 框架中创建采样器对象, Metal 着色语言中也可以创建采样器对象. 但是在着色语言中创建的采样器对象, 只能设定部分的采样器状态:  寻址模式, 滤波模式, 归一化坐标和比较函数. 

下表 描述了采样器状态(枚举类型) 和对应的值, 

Enum Name

Valid Values

Description

coord

normalized (default)

pixel

Specifies whether the texture coordinates when sampling from a texture are normalized or unnormalized values.

address

clamp_to_edge (default)

clamp_to_zero

mirrored_repeat

repeat

Sets the addressing mode for all texture coordinates.

s_address

t_address

r_address

clamp_to_edge (default)

clamp_to_zero

mirrored_repeat

repeat

Sets the addressing mode for an individual texture coordinate.

filter

nearest (default)

linear

Sets the magnification and minification filtering modes for texture sampling.

mag_filter

nearest (default)

linear

Sets the magnification filtering mode for texture sampling.

min_filter

nearest (default)

linear

Sets the minification filtering modes for texture sampling.

mip_filter

none (default)

nearest

linear

Sets the mipmap filtering mode for texture sampling. If none, then only one level of detail is active.

compare_func

never (default)

less

less_equal

greater

greater_equal

equal

not_equal

always

Sets the comparison test that will be used by the sample_compare and gather_compare texture functions.

<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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值