@@ -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+
183190fn 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.
191205fn 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
11561170fn 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