Hi Caroline, one important topic I did not see (or perhaps missed?) in the book is on resources deallocation. Surprisingly the Apple Metal docs do not have anything on the topic as well.
If a MTLTexture
handle in Swift goes out of scope, are the GPU resources associated with it automatically freed up? Same for MTLBuffer
, MTLRenderPipelineState
etc?
Coming from OpenGL, one can use gl.deleteBuffer()
, gl.deleteTexture()
and so on. What about Metal?
Interesting question. I have always assumed that the GPU clears up after itself after each draw call. ie If it’s not in the usable attachments, the GPU will have garbage collected (or overwritten rather than wiped unused memory areas).
I did a little experiment, not quite sure what I’d find.
This is the project from the materials challenge where you render a helmet. I added a stepper to render multiple helmets.
If you want to run it, you’ll need to include the textures in Textures.xcassets, as it was too big to upload with them.
MaterialsHelmet.zip (3.2 MB)
Rendering one helmet:
Rendering ten helmets:
Back to rendering one helmet:
The CPU was interesting. It gradually increased as I increased the number of helmets, but when I removed helmets, it never quite freed all the memory. It’s either doing something efficient, or my memory management is horrible. Peaks are ten, valleys are zero.
I think it warrants further investigation, although I don’t know when I’ll have time. If you find out anything interesting, please post here!