2

我正在尝试使用 MSL 编译自定义过滤器,如本 2017 WWDCCIFilter 文档的这一部分中所述。如果没有 MTLLINKER_FLAGS,我的程序运行顺利,但是在打开标志(值设置为 -cifilter)的情况下,我的程序的 metal device.defaultLibrary 无法返回顶点和片段函数。事实上,当我打印出库的函数名时,它们都是空的。

guard let library = device.newDefaultLibrary() else { return }
let pipelineDescriptor = MTLRenderPipelineDescriptor()
pipelineDescriptor.sampleCount = 1
pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
pipelineDescriptor.depthAttachmentPixelFormat = .invalid

NSLog("%@", library.functionNames)
pipelineDescriptor.vertexFunction = library.makeFunction(name: "mapTexture")
pipelineDescriptor.fragmentFunction = library.makeFunction(name: "displayTexture")

do {
    try renderPipelineState = device.makeRenderPipelineState(descriptor: pipelineDescriptor)
}
catch {
    NSLog("%@", error.localizedDescription)
    assertionFailure("Failed creating a render state pipeline. Can't render the texture without one.")
    return
}

金属代码:

    #include <metal_stdlib>
using namespace metal;

typedef struct {
    float4 renderedCoordinate [[position]];
    float2 textureCoordinate;
} TextureMappingVertex;

vertex TextureMappingVertex mapTexture(unsigned int vertex_id [[ vertex_id ]]) {
    float4x4 renderedCoordinates = float4x4(float4(-1.0, -1.0, 0.0, 1.0),
                                            float4( 1.0, -1.0, 0.0, 1.0),
                                            float4(-1.0,  1.0, 0.0, 1.0),
                                            float4( 1.0,  1.0, 0.0, 1.0));
    float4x2 textureCoordinates = float4x2(float2( 0.0, 1.0),
                                           float2( 1.0, 1.0),
                                           float2( 0.0, 0.0),
                                           float2( 1.0, 0.0));
    TextureMappingVertex outVertex;
    outVertex.renderedCoordinate = renderedCoordinates[vertex_id];
    outVertex.textureCoordinate = textureCoordinates[vertex_id];

    return outVertex;
}

fragment half4 displayTexture(TextureMappingVertex mappingVertex [[ stage_in ]],
                              texture2d<float, access::sample> texture [[ texture(0) ]]) {
    constexpr sampler s(address::clamp_to_edge, filter::linear);

    return half4(texture.sample(s, mappingVertex.textureCoordinate));
}
4

0 回答 0