Highlighted texture of CAMetalLayer is strange on some devices

Image from the metal layer

I am using the below code to get and add a pixel buffer from a metal layer. On some non-specific devices, the output looks like below and the pixelFormat drawable text is.invalid

static func make(with currentDrawable: CAMetalDrawable, usingBuffer pool: CVPixelBufferPool) -> (CVPixelBuffer?, UIImage) {

      let destinationTexture = currentDrawable.texture

      var pixelBuffer: CVPixelBuffer?
      _ = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool, &pixelBuffer)
      if let pixelBuffer = pixelBuffer {
        CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags.init(rawValue: 0))
        let region = MTLRegionMake2D(0, 0, Int(currentDrawable.layer.drawableSize.width), Int(currentDrawable.layer.drawableSize.height))

        let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)

        let tempBuffer = CVPixelBufferGetBaseAddress(pixelBuffer)
        destinationTexture.getBytes(tempBuffer!, bytesPerRow: Int(bytesPerRow), from: region, mipmapLevel: 0)

        let image = imageFromCVPixelBuffer(buffer: pixelBuffer)
        CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags.init(rawValue: 0))
        return (pixelBuffer, image)
      }
      return (nil, UIImage())
    }


static func imageFromCVPixelBuffer(buffer: CVPixelBuffer) -> UIImage {

      let ciimage = CIImage(cvPixelBuffer: buffer)
      let cgimgage = context.createCGImage(ciimage, from: CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(buffer), height: CVPixelBufferGetHeight(buffer)))

      let uiimage = UIImage(cgImage: cgimgage!)

      return uiimage
    }

      

Does anyone know why this is happening and how to prevent it?

Below are some feedback from people who might find this: https://github.com/svtek/SceneKitVideoRecorder/issues/3

+3


source to share





All Articles