At which time does Metal allocate and deallocate memory for textures? I've observed that the textures live for the whole time of the commandBuffer. So, if I have multiple large textures that I need in subsequent shaders, it would make sense to work with multiple commandBuffers to enable deallocation in order to reduce peak memory usage. Is that correct? Do you have any other suggestions on how to reduce peak memory usage when working with large metal textures? Hint: I am using compute shaders only.
Memory allocation of textures in Metal
-
Metal allocates textures when you create them, and deallocates them when there are no more reference to the texture object. By default, a MTLCommandBuffer will keep references to any Metal resource used by it - from encoding to GPU execution completion. (Does not apply if the command buffer was deliberately created with commandBufferWithUnretainedReferences).
-
If you are using Metal 4 , that reference will be held by the MTLResidencySet.
-
If you are concerned by the peak memory usage, you may split the compute processing into several command buffers, and only create new textures once some resources are freed and not used anymore.
-
For a more controlled memory usage, you can use a MTLHeap, with a predefined capacity. Leveraging MTLSharedEvent, the CPU can be signaled when specific work completes, letting you replace specific resources in that heap.