MTKTextureLoader is a class in MetalKit used for loading image data or files into MTLTexture objects. While this can be done manually, MTKTextureLoader abstracts away this process to help reduce boilerplate code. If an issue is believed to be caused by MTKTextureLoader, use this tag. Other tags to consider include [textures], [metal], or [metalkit].
MTKTextureLoader
is a class provided by the Apple MetalKit framework. This class provides several methods to create an MTLTexture
object from image data and files of various formats. An example of usage is the loadTexture
function included in the Metal game example code provided by Xcode:
class func loadTexture(device: MTLDevice,
textureName: String) throws -> MTLTexture {
/// Load texture data with optimal parameters for sampling
let textureLoader = MTKTextureLoader(device: device)
let textureLoaderOptions = [
MTKTextureLoader.Option.textureUsage: NSNumber(value: MTLTextureUsage.shaderRead.rawValue),
MTKTextureLoader.Option.textureStorageMode: NSNumber(value: MTLStorageMode.`private`.rawValue)
]
return try textureLoader.newTexture(name: textureName,
scaleFactor: 1.0,
bundle: nil,
options: textureLoaderOptions)
}
In this example, a filename and loader options are provided, and the newTexture
method of MTKTextureLoader
automatically finds the file in the assets of the application, and converts the image format into the MTLTexture
format for use by the Metal game.
This tag is appropriate for issues pertaining to the use, or questions about the behavior, of MTKTextureLoader
or any methods of this class. In addition to this tag:
- Use objective-c or swift depending on the language used
- Use ios or macos if the issue is platform-specific
- Other related tags: metal metalkit textures
Do not use this tag if the question is about an issue in a Metal game that uses this class if the issue at hand is unlikely to be related to or caused by MTKTextureLoader
or the related methods.