How to update TextureResource with MTLTexture?

Hi I have a monitoring app, that will take input video from uvc and process it using Metal, and eventually get a MTLTexture. The problem I'm facing is I have to convert MTLTexture to CGImage then call TextureResource.replace, which is super slow. Metal processing speed is same as input frame rate(50pfs), but MTLTexture -> CGImage -> TextureResource only got 7fps... Is there any way I can make it faster?

Hello,

Is it possible to perform your processing on the texture in place? If I understand your question properly you should be able to 'lock' your texture, run a kernel over the data in that texture then 'unlock' the texture and use it to draw, having never converted to CGImage.

Discover Metal 4 along with the sample code: Processing a Texture

has the Metal 4 way to do this kind of thing.

Here is another sample that shows a similar approach in RealitKit:

https://developer.apple.com/documentation/visionOS/generating-procedural-textures-in-visionos

Please post back here if I've misunderstood your question.

Thanks! It works! FPS imrpoved from 6fps/s to 36fps/s!

How to update TextureResource with MTLTexture?
 
 
Q