2

In my iOS project Metal is used extensively. Sometimes MTLDevice newTextureWithDescriptor fails to create a texture. Texture format is valid, it's a small RGBA 512x512 texture, everything is set up correctly.

When I print MTLDevice.currentAllocatedSize >> 20 it's 1396 MB on iPhone XR ( A12 processor ).

I've used a lot this thread for max runtime RAM estimate: https://stackoverflow.com/a/15200855/2567725
So I believe for iPhone XR max allowed RAM is 1792 MB.

I guess the texture is not created because RAM has been exsausted.

So the guestions are:

  1. Is it true that on A12 Metal's currentAllocatedSize correlates with CPU memory, and GPU memory is "shared" with the CPU one, so 1792 MB = GPU+CPU for iPhone XR ?

  2. Is there any way to get the description of Metal error? (now it just returns nil in newTextureWithDescriptor).

  3. Is it possible to know how Metal allocation strategy works, e.g. how small available RAM should be to MTLDevice newTextureWithDescriptor return nil ?

UPDATE:
In some cases, however, MTLDevice.currentAllocatedSize >> 20 is much less, e.g. 14 MB, so I suspect Metal has some state corruption. But how to check what's the reason of the error?

The debug description of a texture descriptor:

textureDescriptor.pixelFormat = 70
textureDescriptor.width = 512
textureDescriptor.height = 512
textureDescriptor.textureType = 3
textureDescriptor.usage = 23
textureDescriptor.arrayLength = 1000
textureDescriptor.sampleCount = 1
textureDescriptor.mipmapLevelCount = 1
device.currentAllocatedSize >> 20 = 14
olha
  • 2,132
  • 1
  • 18
  • 39
  • Please show your code. – Hamid Yusifli Nov 07 '20 at 12:42
  • @0xBFE1A8 I can't post the code of entire class, but here I've used an identical approach: https://github.com/gatamar/HowCoreImageInteractsWithMetal/blob/master/HowCoreImageInteractsWithMetal/ViewController.mm Please look at `- (MTLTextureDescriptor*)createTextureDescriptor` and `id metalTexture = [_metalDevice newTextureWithDescriptor:_metalTextureDescriptor];` - everything is very similar in the real project. – olha Nov 07 '20 at 12:58
  • 1
    Regarding 1: on iOS you deal with a unified memory model, hence the memory is shared between CPU and GPU. See https://developer.apple.com/documentation/metal/shader_authoring/developing_and_debugging_metal_shaders?language=objc – Sc4v Nov 17 '20 at 17:01

0 Answers0