Search code examples
iosrotationmetal

Rotate MTLTexture


I'm using Metal to display camera frames. I convert the outputted sample buffers into id<MTLTexture>s using CVMetalTextureCacheCreateTextureFromImage, and that works great... except that the frames come rotated 90 degrees clockwise.

How can I rotate the id<MTLTexture> 90 degrees counter clockwise?

I considered doing this when I display the texture (in the MTKView), but then it will still be rotated the wrong way when I record the video.


Solution

  • You have at least a couple of different options here.

    The easiest is probably requesting "physically" rotated frames from AVFoundation. Assuming you're using an AVCaptureConnection, you can use the setVideoOrientation API (when available) to specify that you want frames to be rotated before they're delivered. Then, displaying and recording can be handled in a uniform way with no further work on your part.

    Alternatively, you could apply a rotation transform both when drawing the frame into the Metal view and when recording a movie. It sounds like you already know how to do the former. The latter just amounts to setting the transform property on your AVAssetWriterInput, assuming that's what you're using. Similar APIs exist on lower-level AVFoundation classes such as AVMutableComposition.