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 <emil.ernerfeldt@gmail.com>
This commit is contained in:
Justin Jaffray 2022-01-06 08:09:53 -08:00 committed by GitHub
parent b0ea4dc0b5
commit 4fe5fa6c59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 3 deletions

View file

@ -3,7 +3,7 @@ All notable changes to the `egui_glow` integration will be noted in this file.
## Unreleased ## Unreleased
* Added `set_texture_filter` method to `Painter` ((#1041)[https://github.com/emilk/egui/pull/1041]).
## 0.16.0 - 2021-12-29 ## 0.16.0 - 2021-12-29
* Made winit/glutin an optional dependency ([#868](https://github.com/emilk/egui/pull/868)). * Made winit/glutin an optional dependency ([#868](https://github.com/emilk/egui/pull/868)).

View file

@ -2,10 +2,13 @@
use glow::HasContext; use glow::HasContext;
use std::option::Option::Some; use std::option::Option::Some;
use crate::painter::TextureFilter;
pub(crate) fn srgbtexture2d( pub(crate) fn srgbtexture2d(
gl: &glow::Context, gl: &glow::Context,
is_webgl_1: bool, is_webgl_1: bool,
srgb_support: bool, srgb_support: bool,
texture_filter: TextureFilter,
data: &[u8], data: &[u8],
w: usize, w: usize,
h: usize, h: usize,
@ -20,12 +23,12 @@ pub(crate) fn srgbtexture2d(
gl.tex_parameter_i32( gl.tex_parameter_i32(
glow::TEXTURE_2D, glow::TEXTURE_2D,
glow::TEXTURE_MAG_FILTER, glow::TEXTURE_MAG_FILTER,
glow::LINEAR as i32, texture_filter.glow_code() as i32,
); );
gl.tex_parameter_i32( gl.tex_parameter_i32(
glow::TEXTURE_2D, glow::TEXTURE_2D,
glow::TEXTURE_MIN_FILTER, glow::TEXTURE_MIN_FILTER,
glow::LINEAR as i32, texture_filter.glow_code() as i32,
); );
gl.tex_parameter_i32( gl.tex_parameter_i32(
glow::TEXTURE_2D, glow::TEXTURE_2D,

View file

@ -35,6 +35,8 @@ pub struct Painter {
is_embedded: bool, is_embedded: bool,
vertex_array: crate::misc_util::VAO, vertex_array: crate::misc_util::VAO,
srgb_support: bool, srgb_support: bool,
/// The filter used for subsequent textures.
texture_filter: TextureFilter,
post_process: Option<PostProcess>, post_process: Option<PostProcess>,
vertex_buffer: glow::Buffer, vertex_buffer: glow::Buffer,
element_array_buffer: glow::Buffer, element_array_buffer: glow::Buffer,
@ -52,6 +54,27 @@ pub struct Painter {
destroyed: bool, 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 { impl Painter {
/// Create painter. /// Create painter.
/// ///
@ -190,6 +213,7 @@ impl Painter {
is_embedded: matches!(shader_version, ShaderVersion::Es100 | ShaderVersion::Es300), is_embedded: matches!(shader_version, ShaderVersion::Es100 | ShaderVersion::Es300),
vertex_array, vertex_array,
srgb_support, srgb_support,
texture_filter: Default::default(),
post_process, post_process,
vertex_buffer, vertex_buffer,
element_array_buffer, element_array_buffer,
@ -224,6 +248,7 @@ impl Painter {
gl, gl,
self.is_webgl_1, self.is_webgl_1,
self.srgb_support, self.srgb_support,
self.texture_filter,
&pixels, &pixels,
font_image.width, font_image.width,
font_image.height, 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")] #[cfg(feature = "epi")]
@ -410,6 +441,7 @@ impl Painter {
gl, gl,
self.is_webgl_1, self.is_webgl_1,
self.srgb_support, self.srgb_support,
self.texture_filter,
&pixels, &pixels,
image.size[0], image.size[0],
image.size[1], image.size[1],