如何使用 MTLSamplerState 而不是在我的片段着色器代码中声明采样器?

how to use MTLSamplerState instead of declaring a sampler in my fragment shader code?

我在下面的着色器中定义了一个采样器 (constexpr sampler textureSampler (mag_filter::linear,min_filter::linear);)。

    using namespace metal;

    struct ProjectedVertex {
      'float4 position [[position]];
      'float2 textureCoord;
    };

    fragment float4 fragmentShader(const ProjectedVertex in [[stage_in]],
                                   const texture2d<float> colorTexture [[texture(0)]],
                                   constant float4 &opacity [[buffer(1)]]){

      constexpr sampler textureSampler (mag_filter::linear,min_filter::linear);
      const float4 colorSample = colorTexture.sample(textureSampler, in.textureCoord);
      return colorSample*opacity[0];

    }

现在我想避免在我的着色器代码中几乎不定义这个采样器。我找到了MTLSamplerState但是我不知道怎么用

如何创建 SamplerState

首先,声明MTLSamplerDescriptor并配置addressModes、magFilter、minFilter等一些属性。 其次,从 MTLDevice 调用 makeSamplerState 方法。大多数情况下默认设备。 那

您可以使用下面的代码。希望对你有帮助。

private static func buildSamplerState() -> MTLSamplerState? {
    let descriptor = MTLSamplerDescriptor()
    descriptor.sAddressMode = .repeat // .clampToEdge, .mirrorRepeat, .clampToZero
    descriptor.tAddressMode = .repeat // .clampToEdge, .mirrorRepeat, .clampToZero
    descriptor.magFilter = .linear // .nearest
    descriptor.minFilter = .linear // .nearest
    let samplerState = MTLCreateSystemDefaultDevice()?.makeSamplerState(descriptor: descriptor)
    return samplerState
}

如何使用

...

let samplerState = buildSamplerState()
...

// call `makeRenderCommandEncoder` to create commandEncoder from commandBuffer
let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor)
commandEncoder.setFragmentSamplerState(samplerState, index: 0)


在你的片段着色器中



fragment float4 exampleShader(VertexIO       inputFragment [[stage_in]],
                              sampler textureSampler [[sampler(0)]],
                              texture2d<float> inputTexture [[texture(0)]])
{
    float2 position = inputFragment.textureCoord;
    // ...
    return inputTexture.sample(textureSampler, position);
}

要创建采样器,首先创建一个 MTLSamplerDescriptor 对象并配置描述符的属性。然后在将使用此采样器的 MTLDevice 对象上调用 newSamplerStateWithDescriptor: 方法。创建采样器后,您可以释放描述符或重新配置其属性以创建其他采样器。

// Create default sampler state
MTLSamplerDescriptor *samplerDesc = [MTLSamplerDescriptor new];
samplerDesc.rAddressMode = MTLSamplerAddressModeRepeat;
samplerDesc.sAddressMode = MTLSamplerAddressModeRepeat;
samplerDesc.tAddressMode = MTLSamplerAddressModeRepeat;
samplerDesc.minFilter = MTLSamplerMinMagFilterLinear;
samplerDesc.magFilter = MTLSamplerMinMagFilterLinear;
samplerDesc.mipFilter = MTLSamplerMipFilterNotMipmapped;
id<MTLSamplerState> ss = [device newSamplerStateWithDescriptor:samplerDesc];

设置片段函数的采样器状态:

    id<MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor: passDescriptor];
     ...
    [encoder setFragmentSamplerState: ss atIndex:0];

从着色器访问:

fragment float4 albedoMainFragment(ImageColor in [[stage_in]],
                                texture2d<float> diffuseTexture [[texture(0)]],
                                sampler smp [[sampler(0)]]) {

    float4 color = diffuseTexture.sample(smp, in.texCoord);
    return color;
}