Home > Blockchain >  Metal API, GetBytes Validation rowBytes(1600) must be >= (4680)
Metal API, GetBytes Validation rowBytes(1600) must be >= (4680)

Time:01-31

Metal experts!

I'm struggling with the following error:

failed assertion `GetBytes Validation rowBytes(1600) must be >= (4680)

at

texture.getBytes(pixelBufferBytes, bytesPerRow: bytesPerRow, from: region, mipmapLevel: 0)

when I try to record the MTLTexture as a video frame using AVFoundation. Therefore I'm converting the MTLTexture into the CVPixelBuffer using the following SO answer (see VideoRecorder.writeFrame).

Here is the place where I pass the texture into the recorder:

extension MetalRenderer: MTKViewDelegate {
  func mtkView(_: MTKView, drawableSizeWillChange _: CGSize) {}

  func draw(in view: MTKView) {
    guard let drawable = view.currentDrawable, let descriptor = view.currentRenderPassDescriptor else {
      return
    }

    let commandBuffer = commandQueue.makeCommandBuffer()!
    let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: descriptor)!

    let deltaTime = 1 / Float(view.preferredFramesPerSecond)
    scene?.render(commandEncoder: commandEncoder, deltaTime: deltaTime)

    commandEncoder.endEncoding()
    commandBuffer.present(drawable)
    commandBuffer.commit()

    commandBuffer.addCompletedHandler { commandBuffer in
      let texture = drawable.layer.nextDrawable()?.texture
      recorder.writeFrame(forTexture: texture!)
    }
  }
}

I suppose, something could be wrong with addCompletedHandler or grabbing the texture maybe?

Workarounds

Workaround 1

I tried to disable the Metal API Validation in Run Scheme and got a bunch of the following errors, but not sure how relevant they are:

2023-01-29 23:03:50.009525 0100 [Client] Synchronous remote object proxy returned error: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.commcenter.coretelephony.xpc was invalidated: failed at lookup with error 3 - No such process." UserInfo={NSDebugDescription=The connection to service named com.apple.commcenter.coretelephony.xpc was invalidated: failed at lookup with error 3 - No such process.}

Workaround 2

Also, I tried to set the metalView.framebufferOnly to false, which didn't help to resolve the crash.

I'd appreciate it if someone could answer. Thanks in advance!

CodePudding user response:

Using the same sizes of MTLTexture and CVPixelBuffer solved the issue. Big thanks to udi for fixing this!

CodePudding user response:

The error message failed assertion `GetBytes Validation rowBytes(1600) must be >= (4680) says that the number of bytes in the row of the CVPixelBuffer obtained from the MTLTexture is not enough to hold the entire row of data in the texture.

So matching the CVPixelBuffer dimensions with the MTLTexture will solve the issue.

  • Related