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

Fix alpha blending in WebGL backends #650

Merged
merged 10 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
8 changes: 0 additions & 8 deletions egui_demo_lib/src/apps/color_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ impl epi::App for ColorTest {

fn update(&mut self, ctx: &egui::CtxRef, frame: &mut epi::Frame<'_>) {
egui::CentralPanel::default().show(ctx, |ui| {
if frame.is_web() {
ui.colored_label(
RED,
"NOTE: The WebGL backend does NOT pass the color test."
);
ui.small("This is because WebGL does not support a linear framebuffer blending (not even WebGL2!).\nMaybe when WebGL3 becomes mainstream in 2030 the web can finally get colors right?");
ui.separator();
}
ScrollArea::auto_sized().show(ui, |ui| {
AsmPrgmC3 marked this conversation as resolved.
Show resolved Hide resolved
self.ui(ui, &mut Some(frame.tex_allocator()));
});
Expand Down
2 changes: 1 addition & 1 deletion egui_demo_lib/src/apps/demo/painting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Default for Painting {
fn default() -> Self {
Self {
lines: Default::default(),
stroke: Stroke::new(2.0, Color32::LIGHT_BLUE), // Thin strokes looks bad on web
stroke: Stroke::new(1.0, Color32::LIGHT_BLUE),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions egui_web/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to the `egui_web` integration will be noted in this file.

## Unreleased

### Fixed 🐛
* Fix alpha blending, now having identical results as egui_glium


## 0.14.0 - 2021-08-24

Expand Down
2 changes: 2 additions & 0 deletions egui_web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ features = [
"TouchList",
"WebGl2RenderingContext",
"WebGlBuffer",
"WebGlFramebuffer",
"WebGlProgram",
"WebGlRenderingContext",
"WebGlShader",
"WebGlTexture",
"WebGlUniformLocation",
"WebGlVertexArrayObject",
"WheelEvent",
"Window",
]
58 changes: 0 additions & 58 deletions egui_web/src/shader/fragment_100es.glsl

This file was deleted.

45 changes: 0 additions & 45 deletions egui_web/src/shader/fragment_300es.glsl

This file was deleted.

26 changes: 26 additions & 0 deletions egui_web/src/shader/main_fragment_100es.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
precision mediump float;
uniform sampler2D u_sampler;
varying vec4 v_rgba;
varying vec2 v_tc;


// 0-1 linear from 0-255 sRGB
vec3 linear_from_srgb(vec3 srgb) {
bvec3 cutoff = lessThan(srgb, vec3(10.31475));
vec3 lower = srgb / vec3(3294.6);
vec3 higher = pow((srgb + vec3(14.025)) / vec3(269.025), vec3(2.4));
return mix(higher, lower, vec3(cutoff));
}

// 0-1 linear from 0-255 sRGBA
vec4 linear_from_srgba(vec4 srgba) {
return vec4(linear_from_srgb(srgba.rgb), srgba.a / 255.0);
}

void main() {
// We must decode the colors, since WebGL doesn't come with sRGBA textures:
AsmPrgmC3 marked this conversation as resolved.
Show resolved Hide resolved
vec4 texture_rgba = linear_from_srgba(texture2D(u_sampler, v_tc) * 255.0);

/// Multiply vertex color with texture color (in linear space).
gl_FragColor = v_rgba * texture_rgba;
}
13 changes: 13 additions & 0 deletions egui_web/src/shader/main_fragment_300es.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
precision mediump float;
uniform sampler2D u_sampler;
varying vec4 v_rgba;
varying vec2 v_tc;

void main() {
// The texture is set up with `SRGB8_ALPHA8`, so no need to decode here!
vec4 texture_rgba = texture2D(u_sampler, v_tc);

// Multiply vertex color with texture color (in linear space).
// Linear color is written and blended in Framebuffer and converted to sRGB later
gl_FragColor = v_rgba * texture_rgba;
}
22 changes: 22 additions & 0 deletions egui_web/src/shader/post_fragment_100es.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
precision mediump float;
uniform sampler2D u_sampler;
varying vec2 v_tc;

// 0-255 sRGB from 0-1 linear
vec3 srgb_from_linear(vec3 rgb) {
bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
vec3 lower = rgb * vec3(3294.6);
vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025);
return mix(higher, lower, vec3(cutoff));
}

// 0-255 sRGBA from 0-1 linear
vec4 srgba_from_linear(vec4 rgba) {
return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a);
}

void main() {
gl_FragColor = texture2D(u_sampler, v_tc);

gl_FragColor = srgba_from_linear(gl_FragColor) / 255.;
}
22 changes: 22 additions & 0 deletions egui_web/src/shader/post_fragment_300es.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
precision mediump float;
uniform sampler2D u_sampler;
varying vec2 v_tc;

// 0-255 sRGB from 0-1 linear
vec3 srgb_from_linear(vec3 rgb) {
bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
vec3 lower = rgb * vec3(3294.6);
vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025);
return mix(higher, lower, vec3(cutoff));
}

// 0-255 sRGBA from 0-1 linear
vec4 srgba_from_linear(vec4 rgba) {
return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a);
}

void main() {
gl_FragColor = texture2D(u_sampler, v_tc);

gl_FragColor = srgba_from_linear(gl_FragColor) / 255.;
}
8 changes: 8 additions & 0 deletions egui_web/src/shader/post_vertex_100es.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
precision mediump float;
attribute vec2 a_pos;
varying vec2 v_tc;

void main() {
gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0);
v_tc = a_pos;
}
8 changes: 8 additions & 0 deletions egui_web/src/shader/post_vertex_300es.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
precision mediump float;
attribute vec2 a_pos;
varying vec2 v_tc;

void main() {
gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0);
v_tc = a_pos;
}
Loading