Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TextureOptions::wrap_mode #3954

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/egui-wgpu/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,12 +923,19 @@ fn create_sampler(
epaint::textures::TextureFilter::Nearest => wgpu::FilterMode::Nearest,
epaint::textures::TextureFilter::Linear => wgpu::FilterMode::Linear,
};
let address_mode = match options.wrap_mode {
epaint::textures::TextureWrapMode::ClampToEdge => wgpu::AddressMode::ClampToEdge,
epaint::textures::TextureWrapMode::Repeat => wgpu::AddressMode::Repeat,
epaint::textures::TextureWrapMode::MirroredRepeat => wgpu::AddressMode::MirrorRepeat,
};
device.create_sampler(&wgpu::SamplerDescriptor {
label: Some(&format!(
"egui sampler (mag: {mag_filter:?}, min {min_filter:?})"
)),
mag_filter,
min_filter,
address_mode_u: address_mode,
address_mode_v: address_mode,
..Default::default()
})
}
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ pub use emath::{
pub use epaint::{
mutex,
text::{FontData, FontDefinitions, FontFamily, FontId, FontTweak},
textures::{TextureFilter, TextureOptions, TexturesDelta},
textures::{TextureFilter, TextureOptions, TextureWrapMode, TexturesDelta},
ClippedPrimitive, ColorImage, FontImage, ImageData, Mesh, PaintCallback, PaintCallbackInfo,
Rounding, Shape, Stroke, TextureHandle, TextureId,
};
Expand Down
18 changes: 16 additions & 2 deletions crates/egui_glow/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ impl TextureFilterExt for egui::TextureFilter {
}
}

trait TextureWrapModeExt {
fn glow_code(&self) -> u32;
}

impl TextureWrapModeExt for egui::TextureWrapMode {
fn glow_code(&self) -> u32 {
match self {
Self::ClampToEdge => glow::CLAMP_TO_EDGE,
Self::Repeat => glow::REPEAT,
Self::MirroredRepeat => glow::MIRRORED_REPEAT,
}
}
}

#[derive(Debug)]
pub struct PainterError(String);

Expand Down Expand Up @@ -555,12 +569,12 @@ impl Painter {
self.gl.tex_parameter_i32(
glow::TEXTURE_2D,
glow::TEXTURE_WRAP_S,
glow::CLAMP_TO_EDGE as i32,
options.wrap_mode.glow_code() as i32,
);
self.gl.tex_parameter_i32(
glow::TEXTURE_2D,
glow::TEXTURE_WRAP_T,
glow::CLAMP_TO_EDGE as i32,
options.wrap_mode.glow_code() as i32,
);
check_for_gl_error!(&self.gl, "tex_parameter");

Expand Down
47 changes: 47 additions & 0 deletions crates/epaint/src/textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,52 @@ pub struct TextureOptions {

/// How to filter when minifying (when texels are smaller than pixels).
pub minification: TextureFilter,

/// How to wrap the texture when the texture coordinates are outside the [0, 1] range.
pub wrap_mode: TextureWrapMode,
}

impl TextureOptions {
/// Linear magnification and minification.
pub const LINEAR: Self = Self {
magnification: TextureFilter::Linear,
minification: TextureFilter::Linear,
wrap_mode: TextureWrapMode::ClampToEdge,
};

/// Nearest magnification and minification.
pub const NEAREST: Self = Self {
magnification: TextureFilter::Nearest,
minification: TextureFilter::Nearest,
wrap_mode: TextureWrapMode::ClampToEdge,
};

/// Linear magnification and minification, but with the texture repeated.
pub const LINEAR_REPEAT: Self = Self {
magnification: TextureFilter::Linear,
minification: TextureFilter::Linear,
wrap_mode: TextureWrapMode::Repeat,
};

/// Linear magnification and minification, but with the texture mirrored and repeated.
pub const LINEAR_MIRRORED_REPEAT: Self = Self {
magnification: TextureFilter::Linear,
minification: TextureFilter::Linear,
wrap_mode: TextureWrapMode::MirroredRepeat,
};

/// Nearest magnification and minification, but with the texture repeated.
pub const NEAREST_REPEAT: Self = Self {
magnification: TextureFilter::Nearest,
minification: TextureFilter::Nearest,
wrap_mode: TextureWrapMode::Repeat,
};

/// Nearest magnification and minification, but with the texture mirrored and repeated.
pub const NEAREST_MIRRORED_REPEAT: Self = Self {
magnification: TextureFilter::Nearest,
minification: TextureFilter::Nearest,
wrap_mode: TextureWrapMode::MirroredRepeat,
};
}

Expand All @@ -193,6 +226,20 @@ pub enum TextureFilter {
Linear,
}

/// Defines how textures are wrapped around objects when texture coordinates fall outside the [0, 1] range.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum TextureWrapMode {
/// Stretches the edge pixels to fill beyond the texture's bounds.
ClampToEdge,
emilk marked this conversation as resolved.
Show resolved Hide resolved

/// Tiles the texture across the surface, repeating it horizontally and vertically.
Repeat,

/// Mirrors the texture with each repetition, creating symmetrical tiling.
MirroredRepeat,
}

// ----------------------------------------------------------------------------

/// What has been allocated and freed during the last period.
Expand Down
Loading