Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

egui: switch to gamma space blending, fix blend mode #217

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions blade-egui/shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ struct Vertex {
}
var<storage, read> r_vertex_data: array<Vertex>;

fn linear_from_srgb(srgb: vec3<f32>) -> vec3<f32> {
let cutoff = srgb < vec3<f32>(10.31475);
let lower = srgb / vec3<f32>(3294.6);
let higher = pow((srgb + vec3<f32>(14.025)) / vec3<f32>(269.025), vec3<f32>(2.4));
fn linear_from_gamma(srgb: vec3<f32>) -> vec3<f32> {
let cutoff = srgb < vec3<f32>(0.04045);
let lower = srgb / vec3<f32>(12.92);
let higher = pow((srgb + vec3<f32>(0.055)) / vec3<f32>(1.055), vec3<f32>(2.4));
return select(higher, lower, cutoff);
}

Expand All @@ -35,8 +35,7 @@ fn vs_main(
let input = r_vertex_data[v_index];
var out: VertexOutput;
out.tex_coord = vec2<f32>(input.tex_coord_x, input.tex_coord_y);
let color = unpack4x8unorm(input.color);
out.color = vec4<f32>(pow(color.xyz, vec3<f32>(2.2)), color.a);
out.color = unpack4x8unorm(input.color);
out.position = vec4<f32>(
2.0 * input.pos_x / r_uniforms.screen_size.x - 1.0,
1.0 - 2.0 * input.pos_y / r_uniforms.screen_size.y,
Expand All @@ -51,5 +50,9 @@ var r_sampler: sampler;

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
return in.color * textureSample(r_texture, r_sampler, in.tex_coord);
//Note: we always assume rendering to linear color space,
// but Egui wants to blend in gamma space, see
// https://github.com/emilk/egui/pull/2071
let blended = in.color * textureSample(r_texture, r_sampler, in.tex_coord);
return vec4f(linear_from_gamma(blended.xyz), blended.a);
}
15 changes: 13 additions & 2 deletions blade-egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct GuiTexture {

impl GuiTexture {
fn create(context: &blade_graphics::Context, name: &str, size: blade_graphics::Extent) -> Self {
let format = blade_graphics::TextureFormat::Rgba8UnormSrgb;
let format = blade_graphics::TextureFormat::Rgba8Unorm;
let allocation = context.create_texture(blade_graphics::TextureDesc {
name,
format,
Expand Down Expand Up @@ -147,7 +147,18 @@ impl GuiPainter {
fragment: shader.at("fs_main"),
color_targets: &[blade_graphics::ColorTargetState {
format: info.format,
blend: Some(blade_graphics::BlendState::ALPHA_BLENDING),
blend: Some(blade_graphics::BlendState {
color: blade_graphics::BlendComponent {
src_factor: blade_graphics::BlendFactor::One,
dst_factor: blade_graphics::BlendFactor::OneMinusSrcAlpha,
operation: blade_graphics::BlendOperation::Add,
},
alpha: blade_graphics::BlendComponent {
src_factor: blade_graphics::BlendFactor::OneMinusDstAlpha,
dst_factor: blade_graphics::BlendFactor::One,
operation: blade_graphics::BlendOperation::Add,
},
}),
write_mask: blade_graphics::ColorWrites::all(),
}],
});
Expand Down
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Changelog for Blade
- destroy old surface on resize
- Vulkan:
- support unused bind groups
- egui:
- fix blending color space

## blade-egui-0.5 (09 Nov 2024)

Expand Down
Loading