Resource loading
Load assets in your games and apps quickly by running a dedicated input/output queue alongside your GPU tasks.
Overview
Metal 3 adds input/output command queues and buffers that make the most of a device’s storage hardware, including flash storage and the unified memory architecture of Apple silicon, when available. When you run a dedicated input/output queue alongside your GPU tasks, you can synchronize them with Metal shared events. With this approach, you can minimize load screen times by fetching the essential assets first and streaming the rest as you need them. You can also start multiple input/output command buffers to load different asset batches and later cancel the ones you don’t need. Ensure that time-sensitive assets, such as sound effects, load with lower latency by running those command buffers on higher-priority queues that you create.
First, create MTLIOCommandQueue instances by configuring an MTLIOCommandQueueDescriptor instance and passing it to an MTLDevice instance’s makeIOCommandQueue(descriptor:) method.
For each queue, create one or more MTLIOCommandBuffer instances by calling the queue’s makeCommandBuffer() or makeCommandBufferWithUnretainedReferences() method. For each command buffer, load the assets you want by calling any of the MTLIOCommandBuffer protocol’s load methods. For example:
The load(_:offset:size:sourceHandle:sourceHandleOffset:) method loads an asset into an MTLBuffer.
The load(_:slice:level:size:sourceBytesPerRow:sourceBytesPerImage:destinationOrigin:sourceHandle:sourceHandleOffset:) method loads an asset into an MTLTexture.
The loadBytes(_:size:sourceHandle:sourceHandleOffset:) method loads an asset, such as an audio file, into a CPU-accessible memory buffer.
For each asset, create an MTLIOFileHandle instance using the input/output command buffer’s load methods. To create a file handle for your asset, call an MTLDevice instance’s makeIOHandle(url:) or makeIOHandle(url:compressionMethod:) method.
To help minimize your appʼs storage footprint, compress your assets at development time. First, create a new compression context with the MTLIOCreateCompressionContext function. Then, add data for an asset to the compression context using the MTLIOCompressionContextAppendData(_:_:_:) function. Finally, call the MTLIOFlushAndDestroyCompressionContext(_:) function to save the context to a compressed file that you add to your project.
Topics
I/O command queues
MTLIOCommandQueueMTLIOCommandQueueDescriptorMTLIOPriorityMTLIOCommandQueueTypeMTLIOScratchBufferAllocatorMTLIOScratchBuffer
I/O command buffers
MTLIOCommandBufferMTLIOFileHandleMTLIOCommandBufferHandlerMTLIOStatusMTLIOError.CodeMTLIOErrorDomain