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

Support for Mipmapped Textures #1685

Closed
wants to merge 15 commits into from
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ path = "examples/3d/spawner.rs"
name = "texture"
path = "examples/3d/texture.rs"

[[example]]
name = "texture_filtering"
path = "examples/3d/texture_filtering.rs"

[[example]]
name = "update_gltf_scene"
path = "examples/3d/update_gltf_scene.rs"
Expand Down
17 changes: 17 additions & 0 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,23 @@ async fn load_gltf<'a, 'b>(
)?
}
};

let needs_mipmaps = gltf_texture
.sampler()
.min_filter()
.map(|mf| match mf {
MinFilter::NearestMipmapNearest
| MinFilter::NearestMipmapLinear
| MinFilter::LinearMipmapNearest
| MinFilter::LinearMipmapLinear => true,
MinFilter::Linear | MinFilter::Nearest => false,
})
.unwrap_or(false);

if needs_mipmaps {
texture.generate_mipmaps(None, None);
}

let texture_label = texture_label(&gltf_texture);
texture.sampler = texture_sampler(&gltf_texture);
if linear_textures.contains(&gltf_texture.index()) {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub mod prelude {
pass::ClearColor,
pipeline::RenderPipelines,
shader::Shader,
texture::Texture,
texture::{FilterMode, Texture},
};
}

Expand Down
34 changes: 26 additions & 8 deletions crates/bevy_render/src/mesh/shape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ pub struct Quad {
pub size: Vec2,
/// Flips the texture coords of the resulting vertices.
pub flip: bool,
/// Scale the texture to tile it.
pub uv_scale: Vec2,
}

impl Default for Quad {
Expand All @@ -136,11 +138,27 @@ impl Default for Quad {

impl Quad {
pub fn new(size: Vec2) -> Self {
Self { size, flip: false }
Self {
size,
flip: false,
uv_scale: Vec2::splat(1.0),
}
}

pub fn flipped(size: Vec2) -> Self {
inodentry marked this conversation as resolved.
Show resolved Hide resolved
Self { size, flip: true }
Self {
size,
flip: true,
uv_scale: Vec2::splat(1.0),
}
}

pub fn tiled(size: Vec2, uv_scale: Vec2) -> Self {
Self {
size,
flip: false,
uv_scale,
}
}
}

Expand All @@ -158,12 +176,12 @@ impl From<Quad> for Mesh {
(
[south_east.x, south_east.y, 0.0],
[0.0, 0.0, 1.0],
[1.0, 1.0],
[quad.uv_scale.x, quad.uv_scale.y],
),
(
[north_east.x, north_east.y, 0.0],
[0.0, 0.0, 1.0],
[1.0, 0.0],
[quad.uv_scale.x, 0.0],
),
(
[north_west.x, north_west.y, 0.0],
Expand All @@ -173,15 +191,15 @@ impl From<Quad> for Mesh {
(
[south_west.x, south_west.y, 0.0],
[0.0, 0.0, 1.0],
[0.0, 1.0],
[0.0, quad.uv_scale.y],
),
]
} else {
[
(
[south_west.x, south_west.y, 0.0],
[0.0, 0.0, 1.0],
[0.0, 1.0],
[0.0, quad.uv_scale.y],
),
(
[north_west.x, north_west.y, 0.0],
Expand All @@ -191,12 +209,12 @@ impl From<Quad> for Mesh {
(
[north_east.x, north_east.y, 0.0],
[0.0, 0.0, 1.0],
[1.0, 0.0],
[quad.uv_scale.x, 0.0],
),
(
[south_east.x, south_east.y, 0.0],
[0.0, 0.0, 1.0],
[1.0, 1.0],
[quad.uv_scale.x, quad.uv_scale.y],
),
]
};
Expand Down
103 changes: 62 additions & 41 deletions crates/bevy_render/src/render_graph/nodes/texture_copy_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,49 +33,70 @@ impl Node for TextureCopyNode {
}

let texture_descriptor: TextureDescriptor = texture.into();
let width = texture.size.width as usize;
let aligned_width =
render_context.resources().get_aligned_texture_size(width);
let format_size = texture.format.pixel_size();
let mut aligned_data = vec![
0;
format_size
* aligned_width
* texture.size.height as usize
* texture.size.depth as usize
];
texture
.data
.chunks_exact(format_size * width)
.enumerate()
.for_each(|(index, row)| {
let offset = index * aligned_width * format_size;
aligned_data[offset..(offset + width * format_size)]
.copy_from_slice(row);
});
let texture_buffer = render_context.resources().create_buffer_with_data(
BufferInfo {
buffer_usage: BufferUsage::COPY_SRC,
..Default::default()
},
&aligned_data,
);

let texture_resource = render_context
.resources()
.get_asset_resource(handle, TEXTURE_ASSET_INDEX)
.unwrap();
debug_assert!(texture_descriptor.mip_level_count > 0);

render_context.copy_buffer_to_texture(
texture_buffer,
0,
(format_size * aligned_width) as u32,
texture_resource.get_texture().unwrap(),
[0, 0, 0],
0,
texture_descriptor.size,
);
render_context.resources().remove_buffer(texture_buffer);
for (mip_level, data) in texture.iter_mipmaps().enumerate() {
if let Some(max_level) = texture.max_mip_level {
if mip_level > max_level {
break;
}
}

let mip_size = texture.mip_size(mip_level as usize);
let width = mip_size.width as usize;
let aligned_width =
render_context.resources().get_aligned_texture_size(width);
let format_size = texture.format.pixel_size();

let mut temporary_buffer;
let aligned_data = if aligned_width != width {
// must create a temporary buffer with the proper alignment
temporary_buffer = vec![
0;
format_size
* aligned_width
* mip_size.height as usize
* mip_size.depth as usize
];
data.chunks_exact(format_size * width).enumerate().for_each(
|(index, row)| {
let offset = index * aligned_width * format_size;
temporary_buffer[offset..(offset + width * format_size)]
.copy_from_slice(row);
},
);
temporary_buffer.as_slice()
} else {
// source data already has the correct alignment
data
};

let texture_buffer =
render_context.resources().create_buffer_with_data(
BufferInfo {
buffer_usage: BufferUsage::COPY_SRC,
..Default::default()
},
&aligned_data,
);

let texture_resource = render_context
.resources()
.get_asset_resource(handle, TEXTURE_ASSET_INDEX)
.unwrap();

render_context.copy_buffer_to_texture(
texture_buffer,
0,
(format_size * aligned_width) as u32,
texture_resource.get_texture().unwrap(),
[0, 0, 0],
mip_level as u32,
mip_size,
);
render_context.resources().remove_buffer(texture_buffer);
}

copied_textures.insert(&handle.id);
}
Expand Down
20 changes: 12 additions & 8 deletions crates/bevy_render/src/texture/image_texture_conversion.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use super::{Extent3d, Texture, TextureDimension, TextureFormat};

/// Helper method to convert a `DynamicImage` to a `Texture`
pub(crate) fn image_to_texture(dyn_img: image::DynamicImage) -> Texture {
/// Helper method to create data for a `Texture` from a `DynamicImage`
/// Returns (dimensions, format, data).
pub(crate) fn image_to_texture_data(
dyn_img: image::DynamicImage,
) -> (Extent3d, TextureFormat, Vec<u8>) {
use bevy_core::AsBytes;

let width;
Expand Down Expand Up @@ -111,12 +114,13 @@ pub(crate) fn image_to_texture(dyn_img: image::DynamicImage) -> Texture {
}
}

Texture::new(
Extent3d::new(width, height, 1),
TextureDimension::D2,
data,
format,
)
(Extent3d::new(width, height, 1), format, data)
}

/// Helper method to convert a `DynamicImage` to a `Texture`
pub(crate) fn image_to_texture(dyn_img: image::DynamicImage) -> Texture {
let (size, format, data) = image_to_texture_data(dyn_img);
Texture::new(size, TextureDimension::D2, data, format)
}

/// Helper method to convert a `Texture` to a `DynamicImage`. Not all `Texture` formats are
Expand Down
Loading