mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 21:00:03 -04:00
Add the Ability to Specify Egui Texture Filters (#1636)
Only works for egui_glow
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::Color32;
|
||||
use crate::{textures::TextureFilter, Color32};
|
||||
|
||||
/// An image stored in RAM.
|
||||
///
|
||||
@@ -272,6 +272,8 @@ pub struct ImageDelta {
|
||||
/// If [`Self::pos`] is `Some`, this describes a patch of the whole image starting at [`Self::pos`].
|
||||
pub image: ImageData,
|
||||
|
||||
pub filter: TextureFilter,
|
||||
|
||||
/// If `None`, set the whole texture to [`Self::image`].
|
||||
///
|
||||
/// If `Some(pos)`, update a sub-region of an already allocated texture with the patch in [`Self::image`].
|
||||
@@ -280,17 +282,19 @@ pub struct ImageDelta {
|
||||
|
||||
impl ImageDelta {
|
||||
/// Update the whole texture.
|
||||
pub fn full(image: impl Into<ImageData>) -> Self {
|
||||
pub fn full(image: impl Into<ImageData>, filter: TextureFilter) -> Self {
|
||||
Self {
|
||||
image: image.into(),
|
||||
filter,
|
||||
pos: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Update a sub-region of an existing texture.
|
||||
pub fn partial(pos: [usize; 2], image: impl Into<ImageData>) -> Self {
|
||||
pub fn partial(pos: [usize; 2], image: impl Into<ImageData>, filter: TextureFilter) -> Self {
|
||||
Self {
|
||||
image: image.into(),
|
||||
filter,
|
||||
pos: Some(pos),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use emath::{remap_clamp, Rect};
|
||||
|
||||
use crate::{FontImage, ImageDelta};
|
||||
use crate::{textures::TextureFilter, FontImage, ImageDelta};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
struct Rectu {
|
||||
@@ -176,12 +176,12 @@ impl TextureAtlas {
|
||||
if dirty == Rectu::NOTHING {
|
||||
None
|
||||
} else if dirty == Rectu::EVERYTHING {
|
||||
Some(ImageDelta::full(self.image.clone()))
|
||||
Some(ImageDelta::full(self.image.clone(), TextureFilter::Linear))
|
||||
} else {
|
||||
let pos = [dirty.min_x, dirty.min_y];
|
||||
let size = [dirty.max_x - dirty.min_x, dirty.max_y - dirty.min_y];
|
||||
let region = self.image.region(pos, size);
|
||||
Some(ImageDelta::partial(pos, region))
|
||||
Some(ImageDelta::partial(pos, region, TextureFilter::Linear))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{emath::NumExt, mutex::RwLock, ImageData, ImageDelta, TextureId, TextureManager};
|
||||
use crate::{
|
||||
emath::NumExt, mutex::RwLock, textures::TextureFilter, ImageData, ImageDelta, TextureId,
|
||||
TextureManager,
|
||||
};
|
||||
|
||||
/// Used to paint images.
|
||||
///
|
||||
@@ -63,17 +66,22 @@ impl TextureHandle {
|
||||
}
|
||||
|
||||
/// Assign a new image to an existing texture.
|
||||
pub fn set(&mut self, image: impl Into<ImageData>) {
|
||||
pub fn set(&mut self, image: impl Into<ImageData>, filter: TextureFilter) {
|
||||
self.tex_mngr
|
||||
.write()
|
||||
.set(self.id, ImageDelta::full(image.into()));
|
||||
.set(self.id, ImageDelta::full(image.into(), filter));
|
||||
}
|
||||
|
||||
/// Assign a new image to a subregion of the whole texture.
|
||||
pub fn set_partial(&mut self, pos: [usize; 2], image: impl Into<ImageData>) {
|
||||
pub fn set_partial(
|
||||
&mut self,
|
||||
pos: [usize; 2],
|
||||
image: impl Into<ImageData>,
|
||||
filter: TextureFilter,
|
||||
) {
|
||||
self.tex_mngr
|
||||
.write()
|
||||
.set(self.id, ImageDelta::partial(pos, image.into()));
|
||||
.set(self.id, ImageDelta::partial(pos, image.into(), filter));
|
||||
}
|
||||
|
||||
/// width x height
|
||||
|
||||
@@ -27,7 +27,7 @@ impl TextureManager {
|
||||
/// MUST have a white pixel at (0,0) ([`crate::WHITE_UV`]).
|
||||
///
|
||||
/// The texture is given a retain-count of `1`, requiring one call to [`Self::free`] to free it.
|
||||
pub fn alloc(&mut self, name: String, image: ImageData) -> TextureId {
|
||||
pub fn alloc(&mut self, name: String, image: ImageData, filter: TextureFilter) -> TextureId {
|
||||
let id = TextureId::Managed(self.next_id);
|
||||
self.next_id += 1;
|
||||
|
||||
@@ -36,9 +36,10 @@ impl TextureManager {
|
||||
size: image.size(),
|
||||
bytes_per_pixel: image.bytes_per_pixel(),
|
||||
retain_count: 1,
|
||||
filter,
|
||||
});
|
||||
|
||||
self.delta.set.insert(id, ImageDelta::full(image));
|
||||
self.delta.set.insert(id, ImageDelta::full(image, filter));
|
||||
id
|
||||
}
|
||||
|
||||
@@ -130,6 +131,22 @@ pub struct TextureMeta {
|
||||
|
||||
/// Free when this reaches zero.
|
||||
pub retain_count: usize,
|
||||
|
||||
/// The texture filtering mode to use when rendering
|
||||
pub filter: TextureFilter,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
||||
pub enum TextureFilter {
|
||||
Linear,
|
||||
Nearest,
|
||||
}
|
||||
|
||||
impl Default for TextureFilter {
|
||||
fn default() -> Self {
|
||||
Self::Linear
|
||||
}
|
||||
}
|
||||
|
||||
impl TextureMeta {
|
||||
|
||||
Reference in New Issue
Block a user