linux: Add missing linear to sRGB transform in mono sprite rendering (#38944)

Kirill Bulatov , Thomas Dagenais , and Kate created

Part of https://github.com/zed-industries/zed/issues/7992
Takes
https://github.com/zed-industries/zed/issues/7992#issuecomment-3083871615
and applies its adjusted version on the current state of things

Screenshots (left is main, right is the patch): 

* default font size

<img width="3840" height="2160" alt="image"
src="https://github.com/user-attachments/assets/26fdc42c-12e6-447f-ad3d-74808e4b2562"
/>

<img width="3840" height="2160" alt="image"
src="https://github.com/user-attachments/assets/29829c61-c998-4e77-97c3-0e66e14b236d"
/>


* buffer and ui font size 7 

<img width="3840" height="2160" alt="image"
src="https://github.com/user-attachments/assets/5d0f1d94-b7ed-488d-ab22-c25eb01e6b4a"
/>

<img width="3840" height="2160" alt="image"
src="https://github.com/user-attachments/assets/7020d62e-de65-4b86-a64b-d3eea798c217"
/>


Release Notes:

- Added missing linear to sRGB transform in mono sprite rendering on
Linux

Co-authored-by: Thomas Dagenais <exrok@i64.dev>
Co-authored-by: Kate <work@localcc.cc>

Change summary

crates/gpui/src/platform/blade/shaders.wgsl | 25 ++++++++++++++++++++--
1 file changed, 22 insertions(+), 3 deletions(-)

Detailed changes

crates/gpui/src/platform/blade/shaders.wgsl 🔗

@@ -180,6 +180,13 @@ fn srgb_to_linear(srgb: vec3<f32>) -> vec3<f32> {
     return select(higher, lower, cutoff);
 }
 
+fn srgb_to_linear_component(a: f32) -> f32 {
+    let cutoff = a < 0.04045;
+    let higher = pow((a + 0.055) / 1.055, 2.4);
+    let lower = a / 12.92;
+    return select(higher, lower, cutoff);
+}
+
 fn linear_to_srgb(linear: vec3<f32>) -> vec3<f32> {
     let cutoff = linear < vec3<f32>(0.0031308);
     let higher = vec3<f32>(1.055) * pow(linear, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055);
@@ -187,6 +194,13 @@ fn linear_to_srgb(linear: vec3<f32>) -> vec3<f32> {
     return select(higher, lower, cutoff);
 }
 
+fn linear_to_srgb_component(linear: f32) -> f32 {
+    let cutoff = linear < 0.0031308;
+    let higher = 1.055 * pow(linear, 1.0 / 2.4) - 0.055;
+    let lower = linear * 12.92;
+    return select(higher, lower, cutoff);
+}
+
 /// Convert a linear color to sRGBA space.
 fn linear_to_srgba(color: vec4<f32>) -> vec4<f32> {
     return vec4<f32>(linear_to_srgb(color.rgb), color.a);
@@ -1155,13 +1169,18 @@ fn vs_mono_sprite(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index
 @fragment
 fn fs_mono_sprite(input: MonoSpriteVarying) -> @location(0) vec4<f32> {
     let sample = textureSample(t_sprite, s_sprite, input.tile_position).r;
-    let alpha_corrected = apply_contrast_and_gamma_correction(sample, input.color.rgb, grayscale_enhanced_contrast, gamma_ratios);
+    // converting to linear space to do color operations as cosmic_text outputs texture data in srgb format
+    // cannot make the gpu automatically do the conversion as there's no R8UnormSrgb format
+    let sample_linear = srgb_to_linear_component(sample);
+    let alpha_corrected = apply_contrast_and_gamma_correction(sample_linear, input.color.rgb, grayscale_enhanced_contrast, gamma_ratios);
 
     // Alpha clip after using the derivatives.
     if (any(input.clip_distances < vec4<f32>(0.0))) {
         return vec4<f32>(0.0);
     }
-    return blend_color(input.color, alpha_corrected);
+
+    // convert to srgb space as the rest of the code (output swapchain) expects that
+    return blend_color(input.color, linear_to_srgb_component(alpha_corrected));
 }
 
 // --- polychrome sprites --- //
@@ -1214,7 +1233,7 @@ fn fs_poly_sprite(input: PolySpriteVarying) -> @location(0) vec4<f32> {
         let grayscale = dot(color.rgb, GRAYSCALE_FACTORS);
         color = vec4<f32>(vec3<f32>(grayscale), sample.a);
     }
-    return blend_color(color, sprite.opacity * saturate(0.5 - distance));
+    return blend_color(color, linear_to_srgb_component(sprite.opacity * saturate(0.5 - distance)));
 }
 
 // --- surfaces --- //