I have the following code below, and it generates the following render. I am using a texture test image, and it looks like its just taking the bottom row of pixels of that image and repeating then. It looks like its correctly applied to the edge it starts on, at least. Any ideas?

        // Create a sampler object
    let samplerDescriptor = MTLSamplerDescriptor()
    samplerDescriptor.minFilter = .linear
    samplerDescriptor.magFilter = .linear
    samplerDescriptor.mipFilter = .linear
    samplerDescriptor.sAddressMode = .clampToEdge
    samplerDescriptor.tAddressMode = .clampToEdge
    textureSamplerState = device.makeSamplerState(descriptor: samplerDescriptor)!

    let textureLoader = MTKTextureLoader(device: device)
    try! textures.append(textureLoader.newTexture(name: "test", scaleFactor: 1.0, bundle: nil, options: nil))

My shader is:

fragment float4 fragmentShader(VertexOut vertexOut [[stage_in]],
                               metal::texture2d<float> texture [[texture(0)]],
                               metal::sampler textureSampler [[sampler(0)]])
    float4 color = texture.sample(textureSampler, vertexOut.texCoord);
    return color;

  • 2
    $\begingroup$ What are the texture coordinates in your triangle vertices? What is the declaration of your vertex type/layout (both in CPU and GPU)? This is likely to be a problem with the texture coordinates, not the texture. $\endgroup$
    – Kevin Reid
    Feb 20, 2023 at 15:52
  • $\begingroup$ Just to add to Kevin's comment - the fact you seem to have "clampToEdge" would suggest one of U or V is set to something well outside [0,1] $\endgroup$
    – Simon F
    Feb 21, 2023 at 16:29


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.