Search code examples
swiftmacosrenderingtexturesmetal

Metal rendering odd darker textures even with [MTKTextureLoader.Option.SRGB : false]?


I already checked this question. Using [MTKTextureLoader.Option.SRGB : false] in texture loader options doesn't work. All that happens is when I do [MTKTextureLoader.Option.SRGB : true], it's a lot darker instead of slightly darker. There must be something else. Here is an example of this unwanted behavior. Example of unwanted behavior Here is the original texture for reference:
Original texture for reference
As you can see I am using the mouse to hold the original packed ice texture over the rendered one. It's considerably lighter, even with [MTKTextureLoader.Option.SRGB : false]. Even though the image I am hovering over the rendered texture is slightly transparent, the unwanted effect is still clearly present.

Here is my fragment function:

fragment half4 TextureFragment(VertexOut VertexIn [[stage_in]], texture2d<float> Texture [[texture(0)]]) {
    constexpr sampler ColorSampler(mip_filter::nearest, mag_filter::nearest, min_filter::nearest);
    float4 Color = Texture.sample(ColorSampler, VertexIn.TexCoord.xy / VertexIn.TexCoord.z);
    return half4(Color.r, Color.g, Color.b, 1);
}

How can I fix the problem? How can I render the actual texture instead of a darker version?

Note: I must fix the problem, not work around it. A workaround would be like return half4(Color.r+0.1, Color.g+0.1, Color.b+0.1, 1); from the fragment shader. I must fix the problem from the root. Why are there so many bugs within the libraries?

I also tried changing the black background to white, and it doesn't change the texture, so that doesn't work.

I think I was once told to check the texture using Xcode's GPU frame debugger. What does that mean?

Help will be greatly appreciated.


Solution

  • For loading those PNG images, I need to set the pixelFormat to bgra8Unorm_srgb instead of bgra8Unorm. Why isn't it just bgra8Unorm_srgb by default? This answer specifies how to do this. I figured it out myself, meanwhile I got no other comments, no other answers. I basically had to figure it out alone.

    MetalView.colorPixelFormat = .bgra8Unorm_srgb
    

    So I can

    PipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm_srgb
    

    So I get the correct image rendered

    enter image description here