From 4fe5fa6c593a29848650d15e33f33c74a75006c4 Mon Sep 17 00:00:00 2001 From: Justin Jaffray Date: Thu, 6 Jan 2022 08:09:53 -0800 Subject: [PATCH] egui_glow: Add function to set the texture filter (#1041) * Allow setting the scale filter for `glow` backend This commit adds a `set_scale_filter` method to the `glow` painter so that textures can be set to scale using nearest-neighbour scaling rather than linear. This is useful for pixel art. I wasn't entirely sure what kind of API you want for this kind of change so I went with what seemed least intrusive, I don't mind doing something more holistic if this isn't what you had in mind. * Rename scale_filter -> texture_filter * Store the TextureFilter directly * PR link in changelog * Use inter-doc links Co-authored-by: Emil Ernerfeldt --- egui_glow/CHANGELOG.md | 2 +- egui_glow/src/misc_util.rs | 7 +++++-- egui_glow/src/painter.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/egui_glow/CHANGELOG.md b/egui_glow/CHANGELOG.md index 5fa3505f..4d107dcb 100644 --- a/egui_glow/CHANGELOG.md +++ b/egui_glow/CHANGELOG.md @@ -3,7 +3,7 @@ All notable changes to the `egui_glow` integration will be noted in this file. ## Unreleased - +* Added `set_texture_filter` method to `Painter` ((#1041)[https://github.com/emilk/egui/pull/1041]). ## 0.16.0 - 2021-12-29 * Made winit/glutin an optional dependency ([#868](https://github.com/emilk/egui/pull/868)). diff --git a/egui_glow/src/misc_util.rs b/egui_glow/src/misc_util.rs index 0ce12000..7c711813 100644 --- a/egui_glow/src/misc_util.rs +++ b/egui_glow/src/misc_util.rs @@ -2,10 +2,13 @@ use glow::HasContext; use std::option::Option::Some; +use crate::painter::TextureFilter; + pub(crate) fn srgbtexture2d( gl: &glow::Context, is_webgl_1: bool, srgb_support: bool, + texture_filter: TextureFilter, data: &[u8], w: usize, h: usize, @@ -20,12 +23,12 @@ pub(crate) fn srgbtexture2d( gl.tex_parameter_i32( glow::TEXTURE_2D, glow::TEXTURE_MAG_FILTER, - glow::LINEAR as i32, + texture_filter.glow_code() as i32, ); gl.tex_parameter_i32( glow::TEXTURE_2D, glow::TEXTURE_MIN_FILTER, - glow::LINEAR as i32, + texture_filter.glow_code() as i32, ); gl.tex_parameter_i32( glow::TEXTURE_2D, diff --git a/egui_glow/src/painter.rs b/egui_glow/src/painter.rs index 49f35eb2..a23037a0 100644 --- a/egui_glow/src/painter.rs +++ b/egui_glow/src/painter.rs @@ -35,6 +35,8 @@ pub struct Painter { is_embedded: bool, vertex_array: crate::misc_util::VAO, srgb_support: bool, + /// The filter used for subsequent textures. + texture_filter: TextureFilter, post_process: Option, vertex_buffer: glow::Buffer, element_array_buffer: glow::Buffer, @@ -52,6 +54,27 @@ pub struct Painter { destroyed: bool, } +#[derive(Copy, Clone)] +pub enum TextureFilter { + Linear, + Nearest, +} + +impl Default for TextureFilter { + fn default() -> Self { + TextureFilter::Linear + } +} + +impl TextureFilter { + pub(crate) fn glow_code(&self) -> u32 { + match self { + TextureFilter::Linear => glow::LINEAR, + TextureFilter::Nearest => glow::NEAREST, + } + } +} + impl Painter { /// Create painter. /// @@ -190,6 +213,7 @@ impl Painter { is_embedded: matches!(shader_version, ShaderVersion::Es100 | ShaderVersion::Es300), vertex_array, srgb_support, + texture_filter: Default::default(), post_process, vertex_buffer, element_array_buffer, @@ -224,6 +248,7 @@ impl Painter { gl, self.is_webgl_1, self.srgb_support, + self.texture_filter, &pixels, font_image.width, font_image.height, @@ -387,6 +412,12 @@ impl Painter { } } + // Set the filter to be used for any subsequent textures loaded via + // [`Self::set_texture`]. + pub fn set_texture_filter(&mut self, texture_filter: TextureFilter) { + self.texture_filter = texture_filter; + } + // ------------------------------------------------------------------------ #[cfg(feature = "epi")] @@ -410,6 +441,7 @@ impl Painter { gl, self.is_webgl_1, self.srgb_support, + self.texture_filter, &pixels, image.size[0], image.size[1],