Skip to content

Commit

Permalink
fix(image cache): 🐛 fix srgb weirdness
Browse files Browse the repository at this point in the history
  • Loading branch information
melody-rs committed Nov 30, 2023
1 parent 8036f50 commit ab0607a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
6 changes: 4 additions & 2 deletions crates/graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ impl GraphicsState {
}

pub fn push_constants_supported(render_state: &egui_wgpu::RenderState) -> bool {
render_state
let feature_supported = render_state
.device
.features()
.contains(wgpu::Features::PUSH_CONSTANTS)
.contains(wgpu::Features::PUSH_CONSTANTS);
let is_dx_12 = render_state.adapter.get_info().backend == wgpu::Backend::Dx12;
feature_supported && !is_dx_12
}
2 changes: 2 additions & 0 deletions crates/graphics/src/sprite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl Sprite {
graphics_state: &'rpass GraphicsState,
render_pass: &mut wgpu::RenderPass<'rpass>,
) {
render_pass.push_debug_group("sprite render");
render_pass.set_pipeline(&graphics_state.pipelines.sprites[&self.blend_mode]);
render_pass.set_bind_group(0, &self.bind_group, &[]);

Expand All @@ -108,6 +109,7 @@ impl Sprite {
}

self.vertices.draw(render_pass);
render_pass.pop_debug_group();
}
}

Expand Down
15 changes: 14 additions & 1 deletion crates/graphics/src/sprite/sprite.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ fn vs_main(
return out;
}

// 0-1 sRGB gamma from 0-1 linear
fn gamma_from_linear_rgb(rgb: vec3<f32>) -> vec3<f32> {
let cutoff = rgb < vec3<f32>(0.0031308);
let lower = rgb * vec3<f32>(12.92);
let higher = vec3<f32>(1.055) * pow(rgb, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055);
return select(higher, lower, cutoff);
}

// 0-1 sRGBA gamma from 0-1 linear
fn gamma_from_linear_rgba(linear_rgba: vec4<f32>) -> vec4<f32> {
return vec4<f32>(gamma_from_linear_rgb(linear_rgba.rgb), linear_rgba.a);
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
var tex_sample = textureSample(t_diffuse, s_diffuse, in.tex_coords);
Expand All @@ -98,5 +111,5 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
tex_sample = vec4<f32>(hsv_to_rgb(hsv), tex_sample.a);
}

return tex_sample;
return gamma_from_linear_rgba(tex_sample);
}
2 changes: 1 addition & 1 deletion crates/graphics/src/tiles/atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl Atlas {
dimension: wgpu::TextureDimension::D2,
mip_level_count: 1,
sample_count: 1,
format: wgpu::TextureFormat::Rgba8Unorm,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::COPY_SRC
| wgpu::TextureUsages::COPY_DST
| wgpu::TextureUsages::TEXTURE_BINDING,
Expand Down
14 changes: 13 additions & 1 deletion crates/graphics/src/tiles/tilemap.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ fn vs_main(vertex: VertexInput, instance: InstanceInput) -> VertexOutput {
return out;
}

// 0-1 sRGB gamma from 0-1 linear
fn gamma_from_linear_rgb(rgb: vec3<f32>) -> vec3<f32> {
let cutoff = rgb < vec3<f32>(0.0031308);
let lower = rgb * vec3<f32>(12.92);
let higher = vec3<f32>(1.055) * pow(rgb, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055);
return select(higher, lower, cutoff);
}

// 0-1 sRGBA gamma from 0-1 linear
fn gamma_from_linear_rgba(linear_rgba: vec4<f32>) -> vec4<f32> {
return vec4<f32>(gamma_from_linear_rgb(linear_rgba.rgb), linear_rgba.a);
}

@fragment
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
Expand All @@ -128,5 +140,5 @@ fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
discard;
}

return color;
return gamma_from_linear_rgba(color);
}

0 comments on commit ab0607a

Please sign in to comment.