mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 14:20:04 -04:00
Improve texture filtering by doing it in gamma space (#7311)
* Closes https://github.com/emilk/egui/pull/5839 This makes some transparent images look a lot nicer when blended:  Cursive text will also look nicer. This unfortunately changes the contract of what `register_native_texture` expects --------- Co-authored-by: Adrian Blumer <blumer.adrian@gmail.com>
This commit is contained in:
@@ -172,12 +172,7 @@ impl Painter {
|
||||
|
||||
let supported_extensions = gl.supported_extensions();
|
||||
log::trace!("OpenGL extensions: {supported_extensions:?}");
|
||||
let srgb_textures = shader_version == ShaderVersion::Es300 // WebGL2 always support sRGB
|
||||
|| supported_extensions.iter().any(|extension| {
|
||||
// EXT_sRGB, GL_ARB_framebuffer_sRGB, GL_EXT_sRGB, GL_EXT_texture_sRGB_decode, …
|
||||
extension.contains("sRGB")
|
||||
});
|
||||
log::debug!("SRGB texture Support: {:?}", srgb_textures);
|
||||
let srgb_textures = false; // egui wants normal sRGB-unaware textures
|
||||
|
||||
let supports_srgb_framebuffer = !cfg!(target_arch = "wasm32")
|
||||
&& supported_extensions.iter().any(|extension| {
|
||||
@@ -202,11 +197,10 @@ impl Painter {
|
||||
&gl,
|
||||
glow::FRAGMENT_SHADER,
|
||||
&format!(
|
||||
"{}\n#define NEW_SHADER_INTERFACE {}\n#define DITHERING {}\n#define SRGB_TEXTURES {}\n{}\n{}",
|
||||
"{}\n#define NEW_SHADER_INTERFACE {}\n#define DITHERING {}\n{}\n{}",
|
||||
shader_version_declaration,
|
||||
shader_version.is_new_shader_interface() as i32,
|
||||
dithering as i32,
|
||||
srgb_textures as i32,
|
||||
shader_prefix,
|
||||
FRAG_SRC
|
||||
),
|
||||
|
||||
@@ -43,25 +43,8 @@ vec3 dither_interleaved(vec3 rgb, float levels) {
|
||||
return rgb + noise / (levels - 1.0);
|
||||
}
|
||||
|
||||
// 0-1 sRGB gamma from 0-1 linear
|
||||
vec3 srgb_gamma_from_linear(vec3 rgb) {
|
||||
bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
|
||||
vec3 lower = rgb * vec3(12.92);
|
||||
vec3 higher = vec3(1.055) * pow(rgb, vec3(1.0 / 2.4)) - vec3(0.055);
|
||||
return mix(higher, lower, vec3(cutoff));
|
||||
}
|
||||
|
||||
// 0-1 sRGBA gamma from 0-1 linear
|
||||
vec4 srgba_gamma_from_linear(vec4 rgba) {
|
||||
return vec4(srgb_gamma_from_linear(rgba.rgb), rgba.a);
|
||||
}
|
||||
|
||||
void main() {
|
||||
#if SRGB_TEXTURES
|
||||
vec4 texture_in_gamma = srgba_gamma_from_linear(texture2D(u_sampler, v_tc));
|
||||
#else
|
||||
vec4 texture_in_gamma = texture2D(u_sampler, v_tc);
|
||||
#endif
|
||||
|
||||
// We multiply the colors in gamma space, because that's the only way to get text to look right.
|
||||
vec4 frag_color_gamma = v_rgba_in_gamma * texture_in_gamma;
|
||||
|
||||
Reference in New Issue
Block a user