【问题标题】:How to use MTLBlitCommandEncoder for copying interlaced video fields into a MTLBuffer如何使用 MTLBlitCommandEncoder 将隔行扫描视频字段复制到 MTLBuffer
【发布时间】:2017-03-28 12:12:51
【问题描述】:

我们正在处理隔行扫描高清视频。我用于渲染的金属色附件具有一个字段的尺寸(1920*540 RGBA)。 当我尝试将两个渲染字段复制到大小为 1920*1080*4 = 8294400 字节的同一 MTLBuffer 中时,它仅在目标偏移量为零时才有效。

let commandBuffer = commandQueue.makeCommandBuffer()
let blitEncoder = commandBuffer.makeBlitCommandEncoder()
blitEncoder.copy(from: attachmentTexture,
                 sourceSlice: 0,
                 sourceLevel: 0,
                 sourceOrigin: MTLOriginMake(0, 0, 0),
                 sourceSize: MTLSizeMake(attachmentTexture.width, attachmentTexture.height, 1),
                 to: destinationBuffer,
                 destinationOffset: 1920*4,
                 destinationBytesPerRow: 1920*4*2,
                 destinationBytesPerImage: destinationBuffer.length)
blitEncoder.endEncoding()
commandBuffer.commit()

对于目标偏移量为零的第一个字段,该函数运行良好。每隔一行填充一次目标缓冲区。

但是,当我想将具有相同代码的第二个字段写入相同的 MTLBuffer 对象时,仅将destinationOffset 设置为 1920*4,就像您在上面的代码中看到的那样(从缓冲区中的第二行开始),然后我得到这样的断言:

-[MTLDebugBlitCommandEncoder validateCopyFromTexture:sourceSlice:sourceLevel:sourceOrigin:sourceSize:toBuffer:destinationOffset:destinationBytesPerRow:destinationBytesPerImage:options:]:677: 断言失败`totalBytesUsed(8302080) 必须是

totalBytesUsed 正好是目标缓冲区长度(以字节为单位)加上偏移量。所以我在这个函数中使用的每一个偏移量都会导致这个断言错误。

谁能解释我如何正确使用这个函数,因为反过来,比如为传入的视频帧创建两个 MTLTexture 对象(奇数和偶数场)在类似的参数下效果很好。

【问题讨论】:

    标签: swift macos metal


    【解决方案1】:

    我最近也遇到过这样的事情。

    您将destinationBuffer.length 传递给destinationBytesPerImage: 参数。正如您所注意到的,Metal 将偏移量和每图像字节值相加,并将其与to: 目标缓冲区 (destinationBuffer) 的长度进行比较。它注意到偏移量加上每个图像的字节数不适合缓冲区并拒绝接受。

    您可以简单地将 0 传递给 destinationBytesPerImage:,因为您没有使用 3D 或 2D 数组纹理。如果这不起作用,请传递destinationBuffer.length - 1920*4

    【讨论】:

      猜你喜欢
      • 2021-05-01
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多