Skip to content

Commit 1f9279a

Browse files
SomeoneToIgnoreexroklocalcc
authored
linux: Add missing linear to sRGB transform in mono sprite rendering (#38944)
Part of #7992 Takes #7992 (comment) 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 <[email protected]> Co-authored-by: Kate <[email protected]>
1 parent da71465 commit 1f9279a

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,27 @@ fn srgb_to_linear(srgb: vec3<f32>) -> vec3<f32> {
180180
return select(higher, lower, cutoff);
181181
}
182182

183+
fn srgb_to_linear_component(a: f32) -> f32 {
184+
let cutoff = a < 0.04045;
185+
let higher = pow((a + 0.055) / 1.055, 2.4);
186+
let lower = a / 12.92;
187+
return select(higher, lower, cutoff);
188+
}
189+
183190
fn linear_to_srgb(linear: vec3<f32>) -> vec3<f32> {
184191
let cutoff = linear < vec3<f32>(0.0031308);
185192
let higher = vec3<f32>(1.055) * pow(linear, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055);
186193
let lower = linear * vec3<f32>(12.92);
187194
return select(higher, lower, cutoff);
188195
}
189196

197+
fn linear_to_srgb_component(linear: f32) -> f32 {
198+
let cutoff = linear < 0.0031308;
199+
let higher = 1.055 * pow(linear, 1.0 / 2.4) - 0.055;
200+
let lower = linear * 12.92;
201+
return select(higher, lower, cutoff);
202+
}
203+
190204
/// Convert a linear color to sRGBA space.
191205
fn linear_to_srgba(color: vec4<f32>) -> vec4<f32> {
192206
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
11551169
@fragment
11561170
fn fs_mono_sprite(input: MonoSpriteVarying) -> @location(0) vec4<f32> {
11571171
let sample = textureSample(t_sprite, s_sprite, input.tile_position).r;
1158-
let alpha_corrected = apply_contrast_and_gamma_correction(sample, input.color.rgb, grayscale_enhanced_contrast, gamma_ratios);
1172+
// converting to linear space to do color operations as cosmic_text outputs texture data in srgb format
1173+
// cannot make the gpu automatically do the conversion as there's no R8UnormSrgb format
1174+
let sample_linear = srgb_to_linear_component(sample);
1175+
let alpha_corrected = apply_contrast_and_gamma_correction(sample_linear, input.color.rgb, grayscale_enhanced_contrast, gamma_ratios);
11591176

11601177
// Alpha clip after using the derivatives.
11611178
if (any(input.clip_distances < vec4<f32>(0.0))) {
11621179
return vec4<f32>(0.0);
11631180
}
1164-
return blend_color(input.color, alpha_corrected);
1181+
1182+
// convert to srgb space as the rest of the code (output swapchain) expects that
1183+
return blend_color(input.color, linear_to_srgb_component(alpha_corrected));
11651184
}
11661185

11671186
// --- polychrome sprites --- //
@@ -1214,7 +1233,7 @@ fn fs_poly_sprite(input: PolySpriteVarying) -> @location(0) vec4<f32> {
12141233
let grayscale = dot(color.rgb, GRAYSCALE_FACTORS);
12151234
color = vec4<f32>(vec3<f32>(grayscale), sample.a);
12161235
}
1217-
return blend_color(color, sprite.opacity * saturate(0.5 - distance));
1236+
return blend_color(color, linear_to_srgb_component(sprite.opacity * saturate(0.5 - distance)));
12181237
}
12191238

12201239
// --- surfaces --- //

0 commit comments

Comments
 (0)