查找newRenderPipelineStateWithDescriptor失败的原因

时间:2016-08-13 11:57:37

标签: ios swift metal

我正在尝试在swift中创建管道状态,应用程序与SIGABRT崩溃。我将调用newRenderPipelineStateWithDescriptor放在try catch块中。为什么不缓存呢?

这是代码,

    let defaultLibrary = device.newDefaultLibrary()!
    let fragmentProgram = defaultLibrary.newFunctionWithName("passThroughFragment")!
    let vertexProgram = defaultLibrary.newFunctionWithName("passGeometry")!

    // check TexturedVertex
    let vertexDesc = MTLVertexDescriptor()
    vertexDesc.attributes[0].format = .Float3
    vertexDesc.attributes[0].offset = 0
    vertexDesc.attributes[0].bufferIndex = 0
    vertexDesc.attributes[1].format = .Float3
    vertexDesc.attributes[0].offset = sizeof(Vec3)
    vertexDesc.attributes[0].bufferIndex = 0
    vertexDesc.attributes[2].format = .Float2
    vertexDesc.attributes[0].offset = sizeof(Vec3) * 2
    vertexDesc.attributes[0].bufferIndex = 0
    vertexDesc.layouts[0].stepFunction = .PerVertex
    vertexDesc.layouts[0].stride = sizeof(TexturedVertex)


    let pipelineStateDescriptor = MTLRenderPipelineDescriptor()
    pipelineStateDescriptor.vertexFunction = vertexProgram
    pipelineStateDescriptor.fragmentFunction = fragmentProgram
    //pipelineStateDescriptor.vertexDescriptor = vertexDesc
    pipelineStateDescriptor.colorAttachments[0].pixelFormat = view.colorPixelFormat
    pipelineStateDescriptor.colorAttachments[0].blendingEnabled = false
    pipelineStateDescriptor.sampleCount = view.sampleCount

    do {
        // SIGABRT will happen here when enabling .vertexDescriptor = vertexDesc
        try pipelineState = device.newRenderPipelineStateWithDescriptor(pipelineStateDescriptor)
    } catch let error {
        print("Failed to create pipeline state, error \(error)")
    }

上面的代码不会崩溃,直到我启用此行

pipelineStateDescriptor.vertexDescriptor = vertexDesc

为了给出一些背景,我的顶点着色器用于接收packed_float4缓冲区作为输入,但我正在尝试将其更新为结构,如上面的MTLVertexDescriptor中所定义。

令人沮丧的是,该应用程序只是在没有提供任何错误提示的情况下崩溃。

调用device.newRenderPipelineStateWithDescriptor时是否有任何获取错误消息的方法?

编辑: 这可以修复崩溃,

    vertexDesc.attributes[0].format = .Float3
    vertexDesc.attributes[0].offset = 0
    vertexDesc.attributes[0].bufferIndex = 0
    vertexDesc.attributes[1].format = .Float3
    vertexDesc.attributes[1].offset = sizeof(Vec3)
    vertexDesc.attributes[1].bufferIndex = 0
    vertexDesc.attributes[2].format = .Float2
    vertexDesc.attributes[2].offset = sizeof(Vec3) * 2
    vertexDesc.attributes[2].bufferIndex = 0

但显然目前还没有办法让newRenderPipelineStateWithDescriptor抛出异常。

0 个答案:

没有答案