Repository: libsdl-org/SDL
File: src/render/metal/SDL_render_metal.m (Objective-C)

Code before:
        id<MTLBuffer> mtlbufconstants = [data.mtldevice newBufferWithLength:sizeof(*shader_constants) options:MTLResourceStorageModeShared];
        mtlbufconstants.label = @"SDL shader constants data";
        SDL_memcpy([mtlbufconstants contents], shader_constants, sizeof(*shader_constants));
        [data.mtlcmdencoder setFragmentBuffer:mtlbufconstants offset:0 atIndex:0];

        SDL_memcpy(&statecache->shader_constants, shader_constants, sizeof(*shader_constants));
        statecache->shader_constants_dirty = false;
    }

    if (constants_offset != statecache->constants_offset) {
        if (constants_offset != CONSTANTS_OFFSET_INVALID) {
            [data.mtlcmdencoder setVertexBuffer:data.mtlbufconstants offset:constants_offset atIndex:3];
        }
        statecache->constants_offset = constants_offset;
    }

    [data.mtlcmdencoder setVertexBufferOffset:first atIndex:0]; // position/texcoords
    return true;
}

static id<MTLSamplerState> GetSampler(SDL3METAL_RenderData *data, SDL_PixelFormat format, SDL_ScaleMode scale_mode, SDL_TextureAddressMode address_u, SDL_TextureAddressMode address_v)
{
    if(format == SDL_PIXELFORMAT_INDEX8) {
        // We'll do linear sampling in the shader if needed
        scale_mode = SDL_SCALEMODE_NEAREST;
    }
    NSNumber *key = [NSNumber numberWithInteger:RENDER_SAMPLER_HASHKEY(scale_mode, address_u, address_v)];
    id<MTLSamplerState> mtlsampler = data.mtlsamplers[key];
    if (mtlsampler == nil) {
        MTLSamplerDescriptor *samplerdesc;
        samplerdesc = [[MTLSamplerDescriptor alloc] init];
        switch (scale_mode) {
        case SDL_SCALEMODE_NEAREST:
            samplerdesc.minFilter = MTLSamplerMinMagFilterNearest;
            samplerdesc.magFilter = MTLSamplerMinMagFilterNearest;
            break;
        case SDL_SCALEMODE_PIXELART:    // Uses linear sampling
        case SDL_SCALEMODE_LINEAR:
            samplerdesc.minFilter = MTLSamplerMinMagFilterLinear;
            samplerdesc.magFilter = MTLSamplerMinMagFilterLinear;
            break;
        default:
            SDL_SetError("Unknown scale mode: %d", scale_mode);
            return nil;
        }
        switch (address_u) {
        case SDL_TEXTURE_ADDRESS_CLAMP:
            samplerdesc.sAddressMode = MTLSamplerAddressModeClampToEdge;
            break;
        case SDL_TEXTURE_ADDRESS_WRAP:
            samplerdesc.sAddressMode = MTLSamplerAddressModeRepeat;

Review comment (suggestion):
```suggestion
    if (format == SDL_PIXELFORMAT_INDEX8) {
        // We'll do linear sampling in the shader if needed
        scale_mode = SDL_SCALEMODE_NEAREST;
    }

```

Code after:
        id<MTLBuffer> mtlbufconstants = [data.mtldevice newBufferWithLength:sizeof(*shader_constants) options:MTLResourceStorageModeShared];
        mtlbufconstants.label = @"SDL shader constants data";
        SDL_memcpy([mtlbufconstants contents], shader_constants, sizeof(*shader_constants));
        [data.mtlcmdencoder setFragmentBuffer:mtlbufconstants offset:0 atIndex:0];

        SDL_memcpy(&statecache->shader_constants, shader_constants, sizeof(*shader_constants));
        statecache->shader_constants_dirty = false;
    }

    if (constants_offset != statecache->constants_offset) {
        if (constants_offset != CONSTANTS_OFFSET_INVALID) {
            [data.mtlcmdencoder setVertexBuffer:data.mtlbufconstants offset:constants_offset atIndex:3];
        }
        statecache->constants_offset = constants_offset;
    }

    [data.mtlcmdencoder setVertexBufferOffset:first atIndex:0]; // position/texcoords
    return true;
}

static id<MTLSamplerState> GetSampler(SDL3METAL_RenderData *data, SDL_PixelFormat format, SDL_ScaleMode scale_mode, SDL_TextureAddressMode address_u, SDL_TextureAddressMode address_v)
{
    if (format == SDL_PIXELFORMAT_INDEX8) {
        // We'll do linear sampling in the shader if needed
        scale_mode = SDL_SCALEMODE_NEAREST;
    }

    NSNumber *key = [NSNumber numberWithInteger:RENDER_SAMPLER_HASHKEY(scale_mode, address_u, address_v)];
    id<MTLSamplerState> mtlsampler = data.mtlsamplers[key];
    if (mtlsampler == nil) {
        MTLSamplerDescriptor *samplerdesc;
        samplerdesc = [[MTLSamplerDescriptor alloc] init];
        switch (scale_mode) {
        case SDL_SCALEMODE_NEAREST:
            samplerdesc.minFilter = MTLSamplerMinMagFilterNearest;
            samplerdesc.magFilter = MTLSamplerMinMagFilterNearest;
            break;
        case SDL_SCALEMODE_PIXELART:    // Uses linear sampling
        case SDL_SCALEMODE_LINEAR:
            samplerdesc.minFilter = MTLSamplerMinMagFilterLinear;
            samplerdesc.magFilter = MTLSamplerMinMagFilterLinear;
            break;
        default:
            SDL_SetError("Unknown scale mode: %d", scale_mode);
            return nil;
        }
        switch (address_u) {
        case SDL_TEXTURE_ADDRESS_CLAMP:
            samplerdesc.sAddressMode = MTLSamplerAddressModeClampToEdge;
            break;
        case SDL_TEXTURE_ADDRESS_WRAP: