mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 21:30:03 -04:00
Add the Ability to Specify Egui Texture Filters (#1636)
Only works for egui_glow
This commit is contained in:
@@ -20,20 +20,13 @@ pub use glow::Context;
|
||||
const VERT_SRC: &str = include_str!("shader/vertex.glsl");
|
||||
const FRAG_SRC: &str = include_str!("shader/fragment.glsl");
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum TextureFilter {
|
||||
Linear,
|
||||
Nearest,
|
||||
}
|
||||
pub type TextureFilter = egui::epaint::textures::TextureFilter;
|
||||
|
||||
impl Default for TextureFilter {
|
||||
fn default() -> Self {
|
||||
TextureFilter::Linear
|
||||
}
|
||||
trait TextureFilterExt {
|
||||
fn glow_code(&self) -> u32;
|
||||
}
|
||||
|
||||
impl TextureFilter {
|
||||
pub(crate) fn glow_code(&self) -> u32 {
|
||||
impl TextureFilterExt for TextureFilter {
|
||||
fn glow_code(&self) -> u32 {
|
||||
match self {
|
||||
TextureFilter::Linear => glow::LINEAR,
|
||||
TextureFilter::Nearest => glow::NEAREST,
|
||||
@@ -60,8 +53,6 @@ pub struct Painter {
|
||||
is_embedded: bool,
|
||||
vao: crate::vao::VertexArrayObject,
|
||||
srgb_support: bool,
|
||||
/// The filter used for subsequent textures.
|
||||
texture_filter: TextureFilter,
|
||||
post_process: Option<PostProcess>,
|
||||
vbo: glow::Buffer,
|
||||
element_array_buffer: glow::Buffer,
|
||||
@@ -216,7 +207,6 @@ impl Painter {
|
||||
is_embedded: matches!(shader_version, ShaderVersion::Es100 | ShaderVersion::Es300),
|
||||
vao,
|
||||
srgb_support,
|
||||
texture_filter: Default::default(),
|
||||
post_process,
|
||||
vbo,
|
||||
element_array_buffer,
|
||||
@@ -457,12 +447,6 @@ 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;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
pub fn set_texture(&mut self, tex_id: egui::TextureId, delta: &egui::epaint::ImageDelta) {
|
||||
@@ -488,7 +472,7 @@ impl Painter {
|
||||
|
||||
let data: &[u8] = bytemuck::cast_slice(image.pixels.as_ref());
|
||||
|
||||
self.upload_texture_srgb(delta.pos, image.size, data);
|
||||
self.upload_texture_srgb(delta.pos, image.size, delta.filter, data);
|
||||
}
|
||||
egui::ImageData::Font(image) => {
|
||||
assert_eq!(
|
||||
@@ -507,12 +491,18 @@ impl Painter {
|
||||
.flat_map(|a| a.to_array())
|
||||
.collect();
|
||||
|
||||
self.upload_texture_srgb(delta.pos, image.size, &data);
|
||||
self.upload_texture_srgb(delta.pos, image.size, delta.filter, &data);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn upload_texture_srgb(&mut self, pos: Option<[usize; 2]>, [w, h]: [usize; 2], data: &[u8]) {
|
||||
fn upload_texture_srgb(
|
||||
&mut self,
|
||||
pos: Option<[usize; 2]>,
|
||||
[w, h]: [usize; 2],
|
||||
texture_filter: TextureFilter,
|
||||
data: &[u8],
|
||||
) {
|
||||
assert_eq!(data.len(), w * h * 4);
|
||||
assert!(
|
||||
w >= 1 && h >= 1,
|
||||
@@ -532,12 +522,12 @@ impl Painter {
|
||||
self.gl.tex_parameter_i32(
|
||||
glow::TEXTURE_2D,
|
||||
glow::TEXTURE_MAG_FILTER,
|
||||
self.texture_filter.glow_code() as i32,
|
||||
texture_filter.glow_code() as i32,
|
||||
);
|
||||
self.gl.tex_parameter_i32(
|
||||
glow::TEXTURE_2D,
|
||||
glow::TEXTURE_MIN_FILTER,
|
||||
self.texture_filter.glow_code() as i32,
|
||||
texture_filter.glow_code() as i32,
|
||||
);
|
||||
|
||||
self.gl.tex_parameter_i32(
|
||||
|
||||
Reference in New Issue
Block a user