Questions tagged [mtktextureloader]

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:

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.

14 questions
6
votes
1 answer

MTKTextureLoader saturates image

I am trying to use a MTKTextureLoader to load a CGImage as a texture. Here is the original image However after I convert that CGImage into a MTLTexture and that texture back to a CGImage it looks horrible, like this: Here is sorta what is going on…
J.Doe
  • 1,502
  • 13
  • 47
5
votes
3 answers

Load an image using MTKTextureLoader without flipping?

It seems like MTKTexureLoader newTextureWithContentsOfURL... automatically flips the image. This causes all of my models and meshes containing conventional UV coordinates to display incorrectly. I didn't see anything in the options to specify…
Curyous
  • 8,716
  • 15
  • 58
  • 83
4
votes
1 answer

How do I use MetalKit texture loader with Metal heaps?

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…
OliverD
  • 1,074
  • 13
  • 19
3
votes
0 answers

How can I load a Texture Set stored in my Asset Catalog?

I have a SceneKit/ARKit App with all textures images stored in an Asset catalog as Texture Sets. For texturing my materials, I am setting the material.diffuse.content property to the name (as String) of the desired Texture Set, and everything works…
ggmorga
  • 61
  • 5
3
votes
1 answer

MTKTextureLoader causing banding in grayscale image

I'm trying to implement a simple LUT color grade in a metal shader. It works with a color LUT, but when the LUT is grayscale, problems crop up. First, loading the grayscale image causes an "image decoding failed" error, which is fixed with this bug…
Ian
  • 2,078
  • 1
  • 17
  • 27
3
votes
1 answer

How to use the options of MTKTextureLoader newTextureWithContentsOfURL?

I am trying to use MetalKit newTextureWithContentsOfURL to create the texture for a cow. MTKTextureLoader *texture_loader = [[MTKTextureLoader alloc] initWithDevice:device]; NSURL *cow_image = [[NSBundle mainBundle] URLForResource:@"spot_texture"…
BigFatTom
  • 149
  • 1
  • 6
2
votes
1 answer

MTKTextureLoader messes PNG RGB vs BGR formats

I'm trying to port my OpenGL code to Metal one. As part of it I need to draw PNG textures, and I'm using MTKTextureLoader to load them. Here is my pipeline code: texturePipelineDescriptor.vertexFunction =…
Iron-Eagle
  • 1,707
  • 2
  • 17
  • 34
2
votes
0 answers

MTKTextureLoader fails with mipmapped texture

When I try to load a texture from an asset catalog: let texLoader = MTKTextureLoader.init(device: device) texLoader.newTexture(withName: "Temple", scaleFactor: 1.0, bundle: nil, options: [:]) {(tex: MTLTexture?, error: Error?) in …
Curyous
  • 8,716
  • 15
  • 58
  • 83
2
votes
1 answer

What is the preferred method to load a texture in Metal?

I am trying to load an asset image in Metal like so: let textureLoader = MTKTextureLoader(device: context.device) do{ let image = UIImage(named: name) try texture = textureLoader.newTextureWithCGImage(image!.CGImage!, options: [:]) }catch…
gloo
  • 2,490
  • 3
  • 22
  • 38
1
vote
1 answer

Load a remote image using MTKTextureLoader

I'm trying to load a remote image into a MTLTexture with this code: let textureLoader = MTKTextureLoader(device: device) textureLoader.newTexture(withContentsOf: url, options: options) { [weak self] (texture, error) in if let t = texture { …
endavid
  • 1,781
  • 17
  • 42
0
votes
0 answers

MTKTextureLoader freezes the thread

I noticed strange behaviour of sync newTexture(name: String, scaleFactor: CGFloat, bundle: Bundle?, options: [MTKTextureLoader.Option : Any]? = nil) throws -> MTLTexture method, sometimes (like 1 of 10 times) it just the freezes thread and does not…
we_lokin
  • 33
  • 6
0
votes
1 answer

Use a remote image for MTKTextureLoader

I'm trying to load my texture from a url with this code: let textureLoader = MTKTextureLoader(device: device) var texture: MTLTexture? let origin = NSString(string: MTKTextureLoader.Origin.bottomLeft.rawValue) let textureLoaderOptions =…
Karla
  • 43
  • 5
0
votes
1 answer

Rendering different texture color formats in Metal

I've been using MTKTextureLoader to load user provided images in to textures for rendering. I am rendering these provided textures to an intermediate texture, and then rendering the intermediate texture to the MTKView drawable. Both the intermediate…
-1
votes
1 answer

How to use MTKTextureLoader newTextureWithContentsOfURL to retrieve an image from the web?

Is it possible to use MTKTextureLoader.newTextureWithContentsOfURL to retrieve an image from the web (e.g. https://i.stack.imgur.com/EwPfY.jpg?s=48&g=1)? I tried but I didn't succeed, so maybe I did something wrong.
zeus
  • 12,173
  • 9
  • 63
  • 184