Skip to content

Commit bfb0714

Browse files
committed
Don't convert web text colors to linear
1 parent 7f9afac commit bfb0714

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(crate) struct GlyphToRender {
5151
dim: [u16; 2],
5252
uv: [u16; 2],
5353
color: u32,
54-
content_type: u32,
54+
content_type_with_srgb: [u16; 2],
5555
depth: f32,
5656
}
5757

src/shader.wgsl

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ struct VertexInput {
44
@location(1) dim: u32,
55
@location(2) uv: u32,
66
@location(3) color: u32,
7-
@location(4) content_type: u32,
7+
@location(4) content_type_with_srgb: u32,
88
@location(5) depth: f32,
99
}
1010

@@ -32,8 +32,7 @@ var mask_atlas_texture: texture_2d<f32>;
3232
@group(0) @binding(3)
3333
var atlas_sampler: sampler;
3434

35-
fn srgb_to_linear(srgb: u32) -> f32 {
36-
let c = f32(srgb) / 255.0;
35+
fn srgb_to_linear(c: f32) -> f32 {
3736
if c <= 0.04045 {
3837
return c / 12.92;
3938
} else {
@@ -78,15 +77,31 @@ fn vs_main(in_vert: VertexInput) -> VertexOutput {
7877

7978
vert_output.position.y *= -1.0;
8079

81-
vert_output.color = vec4<f32>(
82-
srgb_to_linear((color & 0x00ff0000u) >> 16u),
83-
srgb_to_linear((color & 0x0000ff00u) >> 8u),
84-
srgb_to_linear(color & 0x000000ffu),
85-
f32((color & 0xff000000u) >> 24u) / 255.0,
86-
);
80+
let content_type = in_vert.content_type_with_srgb & 0xffffu;
81+
let srgb = (in_vert.content_type_with_srgb & 0xffff0000u) >> 16u;
82+
83+
switch srgb {
84+
case 0u: {
85+
vert_output.color = vec4<f32>(
86+
f32((color & 0x00ff0000u) >> 16u) / 255.0,
87+
f32((color & 0x0000ff00u) >> 8u) / 255.0,
88+
f32(color & 0x000000ffu) / 255.0,
89+
f32((color & 0xff000000u) >> 24u) / 255.0,
90+
);
91+
}
92+
case 1u: {
93+
vert_output.color = vec4<f32>(
94+
srgb_to_linear(f32((color & 0x00ff0000u) >> 16u) / 255.0),
95+
srgb_to_linear(f32((color & 0x0000ff00u) >> 8u) / 255.0),
96+
srgb_to_linear(f32(color & 0x000000ffu) / 255.0),
97+
f32((color & 0xff000000u) >> 24u) / 255.0,
98+
);
99+
}
100+
default: {}
101+
}
87102

88103
var dim: vec2<u32> = vec2(0u);
89-
switch in_vert.content_type {
104+
switch content_type {
90105
case 0u: {
91106
dim = textureDimensions(color_atlas_texture);
92107
break;
@@ -98,7 +113,7 @@ fn vs_main(in_vert: VertexInput) -> VertexOutput {
98113
default: {}
99114
}
100115

101-
vert_output.content_type = in_vert.content_type;
116+
vert_output.content_type = content_type;
102117

103118
vert_output.uv = vec2<f32>(uv) / vec2<f32>(dim);
104119

src/text_atlas.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ pub struct TextAtlas {
271271
pub(crate) shader: ShaderModule,
272272
pub(crate) vertex_buffers: [wgpu::VertexBufferLayout<'static>; 1],
273273
pub(crate) format: TextureFormat,
274+
pub(crate) color_mode: ColorMode,
274275
}
275276

276277
impl TextAtlas {
@@ -450,6 +451,7 @@ impl TextAtlas {
450451
shader,
451452
vertex_buffers,
452453
format,
454+
color_mode,
453455
}
454456
}
455457

src/text_render.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
2-
FontSystem, GlyphDetails, GlyphToRender, GpuCacheStatus, Params, PrepareError, RenderError,
3-
Resolution, SwashCache, SwashContent, TextArea, TextAtlas,
2+
ColorMode, FontSystem, GlyphDetails, GlyphToRender, GpuCacheStatus, Params, PrepareError,
3+
RenderError, Resolution, SwashCache, SwashContent, TextArea, TextAtlas,
44
};
55
use std::{iter, mem::size_of, slice, sync::Arc};
66
use wgpu::{
@@ -276,7 +276,13 @@ impl TextRenderer {
276276
dim: [width as u16, height as u16],
277277
uv: [atlas_x, atlas_y],
278278
color: color.0,
279-
content_type: content_type as u32,
279+
content_type_with_srgb: [
280+
content_type as u16,
281+
match atlas.color_mode {
282+
ColorMode::Accurate => TextColorConversion::ConvertToLinear,
283+
ColorMode::Web => TextColorConversion::None,
284+
} as u16,
285+
],
280286
depth,
281287
})
282288
.take(4),
@@ -405,13 +411,20 @@ impl TextRenderer {
405411
}
406412
}
407413

408-
#[repr(u32)]
414+
#[repr(u16)]
409415
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
410416
pub enum ContentType {
411417
Color = 0,
412418
Mask = 1,
413419
}
414420

421+
#[repr(u16)]
422+
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
423+
enum TextColorConversion {
424+
None = 0,
425+
ConvertToLinear = 1,
426+
}
427+
415428
fn next_copy_buffer_size(size: u64) -> u64 {
416429
let align_mask = COPY_BUFFER_ALIGNMENT - 1;
417430
((size.next_power_of_two() + align_mask) & !align_mask).max(COPY_BUFFER_ALIGNMENT)

0 commit comments

Comments
 (0)