I have a set of Metal textures that are stored in an Xcode Assets Catalog as Texture Sets. I'm loading these using MTKTextureLoader.newTexture(name:scaleFactor:bundle:options)
.
I then use a MTLArgumentEncoder
to encode all of the textures into a Metal 2 argument buffer.
This works great. However, the Introducing Metal 2 WWDC 2017 session recommends combining argument buffers with resource heaps for even better performance, and I'm quite keen to try this. According to the Argument Buffer documentation, instead of having to call MTLRenderCommandEncoder.useResource
on each texture in the argument buffer, you just call useHeap
on the heap that the textures were allocated from.
However, I haven't found a straightforward way to use MTKTextureLoader
together with MTLHeap
. It doesn't seem to have a loading option to allocate the texture from a heap.
I'm guessing that the approach would be:
- load the textures with
MTKTextureLoader
- reverse-engineer a set of
MTLTextureDescriptor
objects for each texture - use the texture descriptors to create an appropriately sized
MTLHeap
- assign a new set of textures from the
MTLHeap
- use some method to copy the textures across, perhaps
replaceBytes
or maybe even aMTLBlitCommandEncoder
- deallocate the original textures loaded with the
MTKTextureLoader
It seems like a fairly long-winded approach, and i've not seen any examples of this, so I thought I'd ask here first in case I'm missing something obvious.
Should I abandon MTKTextureLoader
, and search out some pre-MetalKit art on loading textures from asset catalogs?
I'm using Swift, but happy to accept Objective-C answers.