Skip to content

Commit 3cc9eca

Browse files
AsmPrgmC3mankinskin
authored andcommitted
Fix alpha blending in WebGL2 backend (emilk#650)
Add a render-to-texture step with an sRGBA8 texture
1 parent 1f4aef6 commit 3cc9eca

File tree

11 files changed

+230
-54
lines changed

11 files changed

+230
-54
lines changed

egui_demo_lib/src/apps/color_test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ impl epi::App for ColorTest {
3939
if frame.is_web() {
4040
ui.colored_label(
4141
RED,
42-
"NOTE: The WebGL backend does NOT pass the color test."
42+
"NOTE: The WebGL1 backend does NOT pass the color test. The WebGL2 backend does."
4343
);
44-
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?");
4544
ui.separator();
4645
}
4746
ScrollArea::auto_sized().show(ui, |ui| {

egui_demo_lib/src/apps/demo/painting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl Default for Painting {
1212
fn default() -> Self {
1313
Self {
1414
lines: Default::default(),
15-
stroke: Stroke::new(2.0, Color32::LIGHT_BLUE), // Thin strokes looks bad on web
15+
stroke: Stroke::new(1.0, Color32::LIGHT_BLUE),
1616
}
1717
}
1818
}

egui_web/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ All notable changes to the `egui_web` integration will be noted in this file.
55

66
## Unreleased
77

8+
### Fixed 🐛
9+
* Fix alpha blending for WebGL2 backend, now having identical results as egui_glium
10+
811

912
## 0.14.0 - 2021-08-24
1013

egui_web/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,13 @@ features = [
9292
"TouchList",
9393
"WebGl2RenderingContext",
9494
"WebGlBuffer",
95+
"WebGlFramebuffer",
9596
"WebGlProgram",
9697
"WebGlRenderingContext",
9798
"WebGlShader",
9899
"WebGlTexture",
99100
"WebGlUniformLocation",
101+
"WebGlVertexArrayObject",
100102
"WheelEvent",
101103
"Window",
102104
]

egui_web/src/shader/fragment_100es.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ vec4 linear_from_srgba(vec4 srgba) {
3030
}
3131

3232
void main() {
33-
// We must decode the colors, since WebGL doesn't come with sRGBA textures:
33+
// We must decode the colors, since WebGL1 doesn't come with sRGBA textures:
3434
vec4 texture_rgba = linear_from_srgba(texture2D(u_sampler, v_tc) * 255.0);
3535

3636
/// Multiply vertex color with texture color (in linear space).

egui_web/src/shader/fragment_300es.glsl

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
precision mediump float;
2+
uniform sampler2D u_sampler;
3+
varying vec4 v_rgba;
4+
varying vec2 v_tc;
5+
6+
void main() {
7+
// The texture is set up with `SRGB8_ALPHA8`, so no need to decode here!
8+
vec4 texture_rgba = texture2D(u_sampler, v_tc);
9+
10+
// Multiply vertex color with texture color (in linear space).
11+
// Linear color is written and blended in Framebuffer and converted to sRGB later
12+
gl_FragColor = v_rgba * texture_rgba;
13+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
precision mediump float;
2+
uniform sampler2D u_sampler;
3+
varying vec2 v_tc;
4+
5+
// 0-255 sRGB from 0-1 linear
6+
vec3 srgb_from_linear(vec3 rgb) {
7+
bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
8+
vec3 lower = rgb * vec3(3294.6);
9+
vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025);
10+
return mix(higher, lower, vec3(cutoff));
11+
}
12+
13+
// 0-255 sRGBA from 0-1 linear
14+
vec4 srgba_from_linear(vec4 rgba) {
15+
return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a);
16+
}
17+
18+
void main() {
19+
gl_FragColor = texture2D(u_sampler, v_tc);
20+
21+
gl_FragColor = srgba_from_linear(gl_FragColor) / 255.;
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
precision mediump float;
2+
attribute vec2 a_pos;
3+
varying vec2 v_tc;
4+
5+
void main() {
6+
gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0);
7+
v_tc = a_pos;
8+
}

0 commit comments

Comments
 (0)