When CVMetalTextureRef should be released?

I am creating id<MTLTexture> object from CVPixelBufferRef the following way:


id CreateMTLTexrure(CVMetalTextureCacheRef texture_cache,
                                CVPixelBufferRef pixel_buffer,
                                MTLPixelFormat metal_pixel_format, size_t plane,
                                int height, int width) {
  CVMetalTextureRef texture_ref;


  CVReturn err = CVMetalTextureCacheCreateTextureFromImage(
      kCFAllocatorDefault, texture_cache, pixel_buffer, NULL,
      metal_pixel_format, width, height, plane, &texture_ref);


  if (err != kCVReturnSuccess) {
    // throw error
    return nil;
  }


  id texture = CVMetalTextureGetTexture(texture_ref);
  return texture;
}


When CVMetalTextureRef object should be released?

Is it safe to release it after MTLTexture is obtained?

Replies

This is what I've done, with success, when obtaining a MTLTexture from a valid CVPixelBufferRef (cvBuff):


    id<MTLTexture>retTexture = nil;
    size_t inputWidth =  CVPixelBufferGetWidth(cvBuff);
    size_t inputHeight = CVPixelBufferGetHeight(cvBuff);

    CVMetalTextureRef texturePtr = nil;
    CVReturn retVal = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, *textureCachePtr, cvBuff,
                                                                metalCompatDict,  MTLPixelFormatBGRA8Unorm,
                                                                inputWidth, inputHeight, 0, &texturePtr);
    if (retVal==kCVReturnSuccess && texturePtr)
        retTexture = CVMetalTextureGetTexture(texturePtr);
    if (texturePtr)
        CFRelease(texturePtr);
   
    return retTexture;