在iOS 11中编写自定义CIFilter,Metal Linker

时间:2017-10-25 17:25:25

标签: ios xcode metal ciimage cikernel

我尝试使用MSL编译自定义过滤器,如this 2017 WWDCthis part of the CIFilter Documentation中所述。如果没有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));
}

0 个答案:

没有答案