swiftshadertexturesmetalmipmaps

Mipmaps aren't being used


In Metal, I figured out how to generate and populate a MTLTexture's mipmap levels. However, when I render thousands of these mipmapped textures, and zoom out, I get the distinctive shimmering. I have used Xcode's debugger to determine that these mipmap levels got populated, hence this code works.

let CommandBuffer = CommandQue.makeCommandBuffer()!, CommandEncoder =  CommandBuffer.makeBlitCommandEncoder()!
    CommandEncoder.generateMipmaps(for: BlockTextures!)
    CommandEncoder.endEncoding()
    CommandBuffer.commit()

The mipmap levels are created and populated, but the shimmering still occurs, meaning they aren't being rendered and used. How can I configure either the render pipeline or the fragment shaders, whichever one uses the mipmaps, how do I tell them to use the created mipmaps?


Solution

  • Create a sampler object in your shader that sets the mip_filter. For reference see 2.9 Samplers in the metal shading language specification.

    constexpr sampler s(filter::linear, mip_filter::linear)
    

    Alternatively, create a MTLSamplerDescriptor. For more information see https://developer.apple.com/documentation/metal/textures/improving_filtering_quality_and_sampling_performance/adding_mipmap_filtering_to_samplers?language=objc