如何在金属中独立渲染2张图像

时间:2018-11-07 09:25:19

标签: ios swift metal metalkit

我的目标是自发渲染2个图像。第一个图像或纹理是我的摄像机图像,它来自实时视频,第二个图像或纹理是我要从gif加载图像,其中gif的某些位置为alpha = 0,这样我也可以看到我的背面图像。我的draw method如下图所示。

    public func draw(in: MTKView) {

    autoreleasepool {
        guard let texture = texture else {
            print("Error : camera stream failed to create texture")
            return
        }
        guard let device = self.device else {
            print("Error : Metal Device is nil")
            return
        }
        guard let commandBuffer = commandQueue?.makeCommandBuffer() else {
            print("Error : Command encoder is nil")
            return
        }


        // resposible to create metal camera effect in camera raw image and output will be written to
        // internalTexture, if success, internal texture will be present to the screen using encoder

        self.createMetalEffect(texture, inputCommandBuffer: commandBuffer)

        guard let commandGifBuffer = commandQueue?.makeCommandBuffer() else{
            print("Error : Command encoder is nil")
            return
        }

        // rendering the effect output texture (writen to internalTexture via kernel) on screen
        // so the self.internalTexture must not be nil!
        render(texture: self.internalTexture!, withCommandBuffer: commandBuffer, device: device)

        // for loading gif here
        if isGifEnable{
            if count == self.imageArray.count - 1{
                count = 0
            }else{
                count += 1
            }
            if let giftexture = self.loadImageFrame(img: self.imageArray[count]){
                self.createGifMetalEffect(giftexture, inputCommandBuffer: commandGifBuffer)
                render(texture: self.gifInternalTexture!, withCommandBuffer: commandGifBuffer, device: device)
            }
        }

        /// ------------------------- end gif loading ------------------------------


    }
}

为了呈现图像,我的render方法看起来像这样。

        private func render(texture: MTLTexture,
                        withCommandBuffer commandBuffer: MTLCommandBuffer,
                        device: MTLDevice) {


        guard let renderPassDescriptor = metalView.currentRenderPassDescriptor else {
            print("Error : Render pass descriptor is nil")
            return
        }
        guard let drawable = metalView.currentDrawable else {
            print("Error : drawable from metal view is nil")
            return
        }
        guard let pipelineState = self.pipelineState else {
            print("Error : pipelineState is nil")
            return
        }
//        guard let animation = self.animationDescriptor else {
//            print("Error : animation is nil")
//            return
//        }
        guard let encoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) else {
            print("Error : Fail to create screen render encoder from command buffer!")
                return
        }
        encoder.pushDebugGroup("RenderFrame")
        encoder.setRenderPipelineState(pipelineState)
        encoder.setFragmentTexture(texture, index: 0)
        encoder.drawPrimitives(type: .triangleStrip, vertexStart: 0,
                                vertexCount: 4, instanceCount: 1)

        encoder.popDebugGroup()
        encoder.endEncoding()

        commandBuffer.present(drawable)

        commandBuffer.commit()
        commandBuffer.waitUntilCompleted()
        self.metalTexture = texture

//        self.metalWriteQueue.async {
//            self.writeTexture(texture)
//        }

    }

我也有renderGif方法,该方法看起来与render方法相同,但参数名称分别为self.gifInternalTexture!commandGifBuffer。所以问题是,当我不使用gif渲染视频时它工作正常,但是当我应用我希望在顶部渲染的gif时,没有显示,并且视频也出现了卷曲,可能是因为此命令commandBuffer.waitUntilCompleted() 它给出的警告消息是

should not be called after already presenting this drawable. Get a nextDrawable instead.

可能是因为它已经在currentdrawable函数中出现了render。所以我该如何独立渲染拖曳图像并在另一个图像下显示一个?我的建筑应该是什么?

0 个答案:

没有答案