CVOpenGLESTextureCache & CVMetalTextureCache correct usage

There is very little documentation available on CVOpenGLESTextureCache & CVMetalTextureCache, more specifically about their correct usage. For the purpose of mapping CVPixelBuffer to OpenGLES texture, we create texture cache as follows:


let err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, nil, oglContext as CVEAGLContext, nil, &textureCache)
            if err != 0 {
                NSLog("Error at CVOpenGLESTextureCacheCreate %d", err)
                success = false
                break bail
            }



and then we create an OpenGLES texture from a CVPixelBuffer using CVOpenGLESTextureCacheCreateTextureFromImage(...) API.


Metal flow is exactly same, first create CVMetalTextureCache


if CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, device!, nil, &metalTextureCache) == kCVReturnSuccess {
         
}

and then create Metal Texture from CVPixelBuffer using CVMetalTextureCacheCreateTextureFromImage(...) API.



My questions:



1. How many CVOpenGLESTextureCache/CVMetalTextureCache objects can we create? Is there a hidden performance penalty by using multiple texture cache objects in the code? I see some codes create one global texture cache and some



2. OpenGLES code uses EAGLContext to create texture cache, which means we can not reuse same CVOpenGLESTextureCache on multiple threads. However, it appears we can have just one CVMetalTextureCache which can be reused across all threads. Is that really the case?



3. I get CVPixelBuffer objected from camera which are IOSurface backed up. I am recreating OpenGLES/Metal texture multiple times for the same CVPixelBuffer in different methods, possibly using a different texture cache object, and may be on different threads. Is that the right way of doing things from performance and memory management perspective? Or should I create texture once and for all and feed the texture to multiple methods?

Replies

Dear Apple Engineers, can you throw some light on the correct usage of opaque types such as CVMetalTextureCache and CVOpenglesTextureCache? In general, is it safe to create one texture cache reusable across the application in multiple threads? Is it okay to pass textures created through the cache across multiple threads in Metal/OpenGLES?