Skip to content

Commit

Permalink
Don't convert web text colors to linear
Browse files Browse the repository at this point in the history
  • Loading branch information
grovesNL committed Jan 16, 2024
1 parent 7f9afac commit bfb0714
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) struct GlyphToRender {
dim: [u16; 2],
uv: [u16; 2],
color: u32,
content_type: u32,
content_type_with_srgb: [u16; 2],
depth: f32,
}

Expand Down
37 changes: 26 additions & 11 deletions src/shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct VertexInput {
@location(1) dim: u32,
@location(2) uv: u32,
@location(3) color: u32,
@location(4) content_type: u32,
@location(4) content_type_with_srgb: u32,
@location(5) depth: f32,
}

Expand Down Expand Up @@ -32,8 +32,7 @@ var mask_atlas_texture: texture_2d<f32>;
@group(0) @binding(3)
var atlas_sampler: sampler;

fn srgb_to_linear(srgb: u32) -> f32 {
let c = f32(srgb) / 255.0;
fn srgb_to_linear(c: f32) -> f32 {
if c <= 0.04045 {
return c / 12.92;
} else {
Expand Down Expand Up @@ -78,15 +77,31 @@ fn vs_main(in_vert: VertexInput) -> VertexOutput {

vert_output.position.y *= -1.0;

vert_output.color = vec4<f32>(
srgb_to_linear((color & 0x00ff0000u) >> 16u),
srgb_to_linear((color & 0x0000ff00u) >> 8u),
srgb_to_linear(color & 0x000000ffu),
f32((color & 0xff000000u) >> 24u) / 255.0,
);
let content_type = in_vert.content_type_with_srgb & 0xffffu;
let srgb = (in_vert.content_type_with_srgb & 0xffff0000u) >> 16u;

switch srgb {
case 0u: {
vert_output.color = vec4<f32>(
f32((color & 0x00ff0000u) >> 16u) / 255.0,
f32((color & 0x0000ff00u) >> 8u) / 255.0,
f32(color & 0x000000ffu) / 255.0,
f32((color & 0xff000000u) >> 24u) / 255.0,
);
}
case 1u: {
vert_output.color = vec4<f32>(
srgb_to_linear(f32((color & 0x00ff0000u) >> 16u) / 255.0),
srgb_to_linear(f32((color & 0x0000ff00u) >> 8u) / 255.0),
srgb_to_linear(f32(color & 0x000000ffu) / 255.0),
f32((color & 0xff000000u) >> 24u) / 255.0,
);
}
default: {}
}

var dim: vec2<u32> = vec2(0u);
switch in_vert.content_type {
switch content_type {
case 0u: {
dim = textureDimensions(color_atlas_texture);
break;
Expand All @@ -98,7 +113,7 @@ fn vs_main(in_vert: VertexInput) -> VertexOutput {
default: {}
}

vert_output.content_type = in_vert.content_type;
vert_output.content_type = content_type;

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

Expand Down
2 changes: 2 additions & 0 deletions src/text_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ pub struct TextAtlas {
pub(crate) shader: ShaderModule,
pub(crate) vertex_buffers: [wgpu::VertexBufferLayout<'static>; 1],
pub(crate) format: TextureFormat,
pub(crate) color_mode: ColorMode,
}

impl TextAtlas {
Expand Down Expand Up @@ -450,6 +451,7 @@ impl TextAtlas {
shader,
vertex_buffers,
format,
color_mode,
}
}

Expand Down
21 changes: 17 additions & 4 deletions src/text_render.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
FontSystem, GlyphDetails, GlyphToRender, GpuCacheStatus, Params, PrepareError, RenderError,
Resolution, SwashCache, SwashContent, TextArea, TextAtlas,
ColorMode, FontSystem, GlyphDetails, GlyphToRender, GpuCacheStatus, Params, PrepareError,
RenderError, Resolution, SwashCache, SwashContent, TextArea, TextAtlas,
};
use std::{iter, mem::size_of, slice, sync::Arc};
use wgpu::{
Expand Down Expand Up @@ -276,7 +276,13 @@ impl TextRenderer {
dim: [width as u16, height as u16],
uv: [atlas_x, atlas_y],
color: color.0,
content_type: content_type as u32,
content_type_with_srgb: [
content_type as u16,
match atlas.color_mode {
ColorMode::Accurate => TextColorConversion::ConvertToLinear,
ColorMode::Web => TextColorConversion::None,
} as u16,
],
depth,
})
.take(4),
Expand Down Expand Up @@ -405,13 +411,20 @@ impl TextRenderer {
}
}

#[repr(u32)]
#[repr(u16)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ContentType {
Color = 0,
Mask = 1,
}

#[repr(u16)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
enum TextColorConversion {
None = 0,
ConvertToLinear = 1,
}

fn next_copy_buffer_size(size: u64) -> u64 {
let align_mask = COPY_BUFFER_ALIGNMENT - 1;
((size.next_power_of_two() + align_mask) & !align_mask).max(COPY_BUFFER_ALIGNMENT)
Expand Down

0 comments on commit bfb0714

Please sign in to comment.