Writing the contents of MTKView.drawable.texture to a file

Hi folks! I'm an amateur working on an art project. I've made a ray marcher that I'm quite happy with, and it's running on my M1 Max at a beautiful 4k 60 fps. But I've found while I can generate these graphics live quite well, it's another thing to capture them. After getting disappointing results from a capture card and a second computer, I'm wondering whether it's possible to write the contents of drawable directly into a file. I'm using the method here:

https://stackoverflow.com/questions/43838089/capture-metal-mtkview-as-movie-in-realtime/43860229#43860229

and while it works, it drops my frame rate from 60 to ~7. I don't really know what I'm doing, and am having a hard time reasoning about whether it would be possible to do this at a satisfactory rate. (When the capture function is off, I'm running about 11ms per frame.)

I'm writing directly to drawable.texture from the shader, and then as per the stack overflow example, I'm calling MTLTexture.getBytes(...) to copy the pixels into an AVAssetWriter pixel buffer. I'm wondering if there's an obvious bottleneck here that I don't know enough to avoid, or whether this is a fool's errand in terms of e.g. memory bus speed between the GPU and CPU. Could I increase performance with a blit copy? Is operating directly on drawable introducing unnecessary constraints? Or is this just going to be impossible on current hardware?

Writing the contents of MTKView.drawable.texture to a file
 
 
Q