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 support anisotropic filtering and for limiting the maximum number of mipmaps #502

Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions examples/fog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ pub async fn run() {
Interpolation::Nearest,
Interpolation::Nearest,
None,
None,
Wrapping::ClampToEdge,
Wrapping::ClampToEdge,
None,
);
let mut depth_texture = DepthTexture2D::new::<f32>(
&context,
Expand Down Expand Up @@ -95,8 +97,10 @@ pub async fn run() {
Interpolation::Nearest,
Interpolation::Nearest,
None,
None,
Wrapping::ClampToEdge,
Wrapping::ClampToEdge,
None,
);
depth_texture = DepthTexture2D::new::<f32>(
&context,
Expand Down
2 changes: 2 additions & 0 deletions src/core/render_target/color_target_multisample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ impl<C: TextureDataType> ColorTargetMultisample<C> {
Interpolation::Linear,
Interpolation::Linear,
None,
None,
Wrapping::ClampToEdge,
Wrapping::ClampToEdge,
None,
);
self.resolve_to(&color_texture.as_color_target(None));
color_texture
Expand Down
4 changes: 4 additions & 0 deletions src/core/render_target/multisample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ impl<C: TextureDataType, D: DepthTextureDataType> RenderTargetMultisample<C, D>
Interpolation::Linear,
Interpolation::Linear,
None,
None,
Wrapping::ClampToEdge,
Wrapping::ClampToEdge,
None,
);
self.resolve_color_to(&color_texture.as_color_target(None));
color_texture
Expand Down Expand Up @@ -171,8 +173,10 @@ impl<C: TextureDataType, D: DepthTextureDataType> RenderTargetMultisample<C, D>
Interpolation::Linear,
Interpolation::Linear,
None,
None,
Wrapping::ClampToEdge,
Wrapping::ClampToEdge,
None,
);
let mut depth_texture = DepthTexture2D::new::<D>(
&self.context,
Expand Down
26 changes: 25 additions & 1 deletion src/core/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ fn set_parameters(
wrap_s: Wrapping,
wrap_t: Wrapping,
wrap_r: Option<Wrapping>,
anisotropic_filter: Option<u32>,
) {
unsafe {
match mip_map_filter {
Expand Down Expand Up @@ -346,11 +347,29 @@ fn set_parameters(
if let Some(r) = wrap_r {
context.tex_parameter_i32(target, crate::context::TEXTURE_WRAP_R, wrapping_from(r));
}
if let Some(level) = anisotropic_filter {
let extensions = context.supported_extensions();
// Desktop
if extensions.contains("GL_ARB_texture_filter_anisotropic") ||
extensions.contains("GL_EXT_texture_filter_anisotropic") ||
// Web
extensions.contains("EXT_texture_filter_anisotropic")
{
let max_level =
context.get_parameter_f32(crate::context::MAX_TEXTURE_MAX_ANISOTROPY_EXT);
context.tex_parameter_f32(
target,
crate::context::TEXTURE_MAX_ANISOTROPY_EXT,
max_level.min(level as f32),
);
}
}
}
}

fn calculate_number_of_mip_maps<T: TextureDataType>(
mip_map_filter: Option<Interpolation>,
mip_map_limit: Option<u32>,
width: u32,
height: u32,
depth: Option<u32>,
Expand All @@ -368,7 +387,12 @@ fn calculate_number_of_mip_maps<T: TextureDataType>(
1
} else {
let power_of_two = max_size.next_power_of_two();
(power_of_two as f64).log2() as u32
let levels = (power_of_two as f64).log2() as u32;
if let Some(limit) = mip_map_limit {
levels.min(limit).max(1)
} else {
levels
}
}
} else {
1
Expand Down
1 change: 1 addition & 0 deletions src/core/texture/depth_texture2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl DepthTexture2D {
wrap_s,
wrap_t,
None,
None,
);
unsafe {
context.tex_storage_2d(
Expand Down
1 change: 1 addition & 0 deletions src/core/texture/depth_texture2d_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl DepthTexture2DArray {
wrap_s,
wrap_t,
None,
None,
);
unsafe {
context.tex_storage_3d(
Expand Down
1 change: 1 addition & 0 deletions src/core/texture/depth_texture_cube_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl DepthTextureCubeMap {
wrap_s,
wrap_t,
Some(wrap_r),
None,
);
unsafe {
context.tex_storage_2d(
Expand Down
10 changes: 8 additions & 2 deletions src/core/texture/texture2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ impl Texture2D {
cpu_texture.min_filter,
cpu_texture.mag_filter,
cpu_texture.mip_map_filter,
cpu_texture.mip_map_limit,
cpu_texture.wrap_s,
cpu_texture.wrap_t,
cpu_texture.anisotropic_filter,
);
texture.fill(data);
texture
Expand All @@ -68,12 +70,14 @@ impl Texture2D {
min_filter: Interpolation,
mag_filter: Interpolation,
mip_map_filter: Option<Interpolation>,
mip_map_limit: Option<u32>,
wrap_s: Wrapping,
wrap_t: Wrapping,
anisotropic_filter: Option<u32>,
) -> Self {
let id = generate(context);
let number_of_mip_maps =
calculate_number_of_mip_maps::<T>(mip_map_filter, width, height, None);
calculate_number_of_mip_maps::<T>(mip_map_filter, mip_map_limit, width, height, None);
let texture = Self {
context: context.clone(),
id,
Expand All @@ -96,6 +100,7 @@ impl Texture2D {
wrap_s,
wrap_t,
None,
anisotropic_filter,
);
unsafe {
context.tex_storage_2d(
Expand Down Expand Up @@ -165,7 +170,8 @@ impl Texture2D {
self.number_of_mip_maps
}

pub(crate) fn generate_mip_maps(&self) {
/// Generate mip maps for this texture.
pub fn generate_mip_maps(&self) {
if self.number_of_mip_maps > 1 {
self.bind();
unsafe {
Expand Down
10 changes: 8 additions & 2 deletions src/core/texture/texture2d_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ impl Texture2DArray {
cpu_texture.min_filter,
cpu_texture.mag_filter,
cpu_texture.mip_map_filter,
cpu_texture.mip_map_limit,
cpu_texture.wrap_s,
cpu_texture.wrap_t,
cpu_texture.anisotropic_filter,
);
texture.fill(data);
texture
Expand All @@ -157,12 +159,14 @@ impl Texture2DArray {
min_filter: Interpolation,
mag_filter: Interpolation,
mip_map_filter: Option<Interpolation>,
mip_map_limit: Option<u32>,
wrap_s: Wrapping,
wrap_t: Wrapping,
anisotropic_filter: Option<u32>,
) -> Self {
let id = generate(context);
let number_of_mip_maps =
calculate_number_of_mip_maps::<T>(mip_map_filter, width, height, None);
calculate_number_of_mip_maps::<T>(mip_map_filter, mip_map_limit, width, height, None);
let texture = Self {
context: context.clone(),
id,
Expand All @@ -186,6 +190,7 @@ impl Texture2DArray {
wrap_s,
wrap_t,
None,
anisotropic_filter,
);
unsafe {
context.tex_storage_3d(
Expand Down Expand Up @@ -286,7 +291,8 @@ impl Texture2DArray {
self.number_of_mip_maps
}

pub(in crate::core) fn generate_mip_maps(&self) {
/// Generate mip maps for this texture.
pub fn generate_mip_maps(&self) {
if self.number_of_mip_maps > 1 {
self.bind();
unsafe {
Expand Down
17 changes: 14 additions & 3 deletions src/core/texture/texture3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ impl Texture3D {
cpu_texture.min_filter,
cpu_texture.mag_filter,
cpu_texture.mip_map_filter,
cpu_texture.mip_map_limit,
cpu_texture.wrap_s,
cpu_texture.wrap_t,
cpu_texture.wrap_r,
cpu_texture.anisotropic_filter,
);
texture.fill(data);
texture
Expand All @@ -69,13 +71,20 @@ impl Texture3D {
min_filter: Interpolation,
mag_filter: Interpolation,
mip_map_filter: Option<Interpolation>,
mip_map_limit: Option<u32>,
wrap_s: Wrapping,
wrap_t: Wrapping,
wrap_r: Wrapping,
anisotropic_filter: Option<u32>,
) -> Self {
let id = generate(context);
let number_of_mip_maps =
calculate_number_of_mip_maps::<T>(mip_map_filter, width, height, Some(depth));
let number_of_mip_maps = calculate_number_of_mip_maps::<T>(
mip_map_filter,
mip_map_limit,
width,
height,
Some(depth),
);
let texture = Self {
context: context.clone(),
id,
Expand All @@ -99,6 +108,7 @@ impl Texture3D {
wrap_s,
wrap_t,
Some(wrap_r),
anisotropic_filter,
);
unsafe {
context.tex_storage_3d(
Expand Down Expand Up @@ -168,7 +178,8 @@ impl Texture3D {
self.number_of_mip_maps
}

fn generate_mip_maps(&self) {
/// Generate mip maps for this texture.
pub fn generate_mip_maps(&self) {
if self.number_of_mip_maps > 1 {
self.bind();
unsafe {
Expand Down
16 changes: 12 additions & 4 deletions src/core/texture/texture_cube_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,11 @@ impl TextureCubeMap {
cpu_texture.min_filter,
cpu_texture.mag_filter,
cpu_texture.mip_map_filter,
cpu_texture.mip_map_limit,
cpu_texture.wrap_s,
cpu_texture.wrap_t,
wrap_r,
cpu_texture.anisotropic_filter,
);
texture.fill(
right_data,
Expand All @@ -310,13 +312,15 @@ impl TextureCubeMap {
min_filter: Interpolation,
mag_filter: Interpolation,
mip_map_filter: Option<Interpolation>,
mip_map_limit: Option<u32>,
wrap_s: Wrapping,
wrap_t: Wrapping,
wrap_r: Wrapping,
anisotropic_filter: Option<u32>,
) -> Self {
let id = generate(context);
let number_of_mip_maps =
calculate_number_of_mip_maps::<T>(mip_map_filter, width, height, None);
calculate_number_of_mip_maps::<T>(mip_map_filter, mip_map_limit, width, height, None);
let texture = Self {
context: context.clone(),
id,
Expand All @@ -339,6 +343,7 @@ impl TextureCubeMap {
wrap_s,
wrap_t,
Some(wrap_r),
anisotropic_filter,
);
unsafe {
context.tex_storage_2d(
Expand Down Expand Up @@ -454,9 +459,11 @@ impl TextureCubeMap {
Interpolation::Linear,
Interpolation::Linear,
Some(Interpolation::Linear),
cpu_texture.mip_map_limit,
Wrapping::ClampToEdge,
Wrapping::ClampToEdge,
Wrapping::ClampToEdge,
cpu_texture.anisotropic_filter,
);

{
Expand All @@ -467,9 +474,9 @@ impl TextureCubeMap {
uniform vec3 up;

in vec2 uvs;

layout (location = 0) out vec4 outColor;

void main()
{
vec3 right = cross(direction, up);
Expand Down Expand Up @@ -534,7 +541,8 @@ impl TextureCubeMap {
self.number_of_mip_maps
}

pub(in crate::core) fn generate_mip_maps(&self) {
/// Generate mip maps for this texture.
pub fn generate_mip_maps(&self) {
if self.number_of_mip_maps > 1 {
self.bind();
unsafe {
Expand Down
4 changes: 4 additions & 0 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ macro_rules! impl_render_target_extensions_body {
Interpolation::Nearest,
Interpolation::Nearest,
None,
None,
Wrapping::ClampToEdge,
Wrapping::ClampToEdge,
None
);
let mut geometry_pass_depth_texture = DepthTexture2D::new::<f32>(
&self.context,
Expand Down Expand Up @@ -648,8 +650,10 @@ pub fn ray_intersect(
Interpolation::Nearest,
Interpolation::Nearest,
None,
None,
Wrapping::ClampToEdge,
Wrapping::ClampToEdge,
None
);
let mut depth_texture = DepthTexture2D::new::<f32>(
context,
Expand Down
Loading
Loading