AVAssetWriter是否会内部等待GPU完成CVPixelBuffer的渲染操作?
提问:AVAssetWriter(或AVAssetWriterInputPixelBufferAdaptor)是否会在内部等待GPU完成对传入像素缓冲区的修改?
我的渲染流程如下:
- 在
captureOutput(_ captureOutput: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)方法中接收CVPixelBuffer - 使用
CVMetalTextureCacheCreateTextureFromImage创建基于该像素缓冲区的MTLTexture - 创建命令缓冲区,编码修改上述纹理的计算管线并提交命令缓冲区
- 通过
AVAssetWriterInputPixelBufferAdaptor.append()将原始像素缓冲区传入AVAssetWriter
即使我在步骤4前不调用MTLCommandBuffer.waitUntilCompleted()(或通过MTLCommandBuffer.addCompletedHandler等待GPU完成),输出视频文件仍呈现渲染后的效果,而非未修改的像素缓冲区。
为验证此现象,我做了以下测试:
测试步骤
1. 修改着色器增加耗时无意义操作
uint2 gid [[thread_position_in_grid]]) { // 无关的实际着色器代码 for (int i = 0; i < 10000; i++) { outputColor.r += float(i) * 0.00000000001; } // 将outputColor写回纹理 }``` ### 2. 配置日志记录 - 记录每个像素缓冲区的追加时间及其内存地址 - 记录修改每个像素缓冲区的命令缓冲区实际完成时间(通过`addCompletedHandler`) 日志显示,像素缓冲区被传给AVAssetWriter的时间远早于其对应的GPU命令完成时间,但输出文件仍包含渲染效果。日志示例如下:
Appending <CVPixelBuffer 0x282b6acb0> // <--- 接收像素缓冲区后立即执行
Finished rendering <CVPixelBuffer [其他地址]>
Finished rendering <CVPixelBuffer [其他地址]>
Appending <CVPixelBuffer [其他地址]>
Finished rendering <CVPixelBuffer [其他地址]>
Appending <CVPixelBuffer [其他地址]>
Finished rendering <CVPixelBuffer [其他地址]>
Finished rendering <CVPixelBuffer 0x282b6acb0> // <--- 耗时着色器执行完成后才发生,但输出文件包含该渲染结果!
内容的提问来源于stack exchange,提问作者tenuki
相关产品推荐
相关产品推荐

