Skip to content

Commit

Permalink
Remove unnecessary alternate create_texture path in prepare_asset for…
Browse files Browse the repository at this point in the history
… Image (bevyengine#6671)

# Objective

`prepare_asset` for Image has an alternate path for texture creation that is used when the image is not compressed and does not contain mipmaps. This additional code path is unnecessary as `render_device.create_texture_with_data()` will handle both cases correctly.

## Solution

Use `render_device.create_texture_with_data()` in all cases.

Tested successfully with the following examples:
- load_gltf
- render_to_texture
- texture
- 3d_shapes
- sprite
- sprite_sheet
- array_texture
- shader_material_screenspace_texture
- skybox (though this already would use the `create_texture_with_data()` branch anyway)
  • Loading branch information
DGriffin91 committed Dec 5, 2022
1 parent e621acd commit f9ad051
Showing 1 changed file with 6 additions and 39 deletions.
45 changes: 6 additions & 39 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ use bevy_math::Vec2;
use bevy_reflect::{FromReflect, Reflect, TypeUuid};
use std::hash::Hash;
use thiserror::Error;
use wgpu::{
Extent3d, ImageCopyTexture, ImageDataLayout, Origin3d, TextureDimension, TextureFormat,
TextureViewDescriptor,
};
use wgpu::{Extent3d, TextureDimension, TextureFormat, TextureViewDescriptor};

pub const TEXTURE_ASSET_INDEX: u64 = 0;
pub const SAMPLER_ASSET_INDEX: u64 = 1;
Expand Down Expand Up @@ -629,41 +626,11 @@ impl RenderAsset for Image {
image: Self::ExtractedAsset,
(render_device, render_queue, default_sampler): &mut SystemParamItem<Self::Param>,
) -> Result<Self::PreparedAsset, PrepareAssetError<Self::ExtractedAsset>> {
let texture = if image.texture_descriptor.mip_level_count > 1 || image.is_compressed() {
render_device.create_texture_with_data(
render_queue,
&image.texture_descriptor,
&image.data,
)
} else {
let texture = render_device.create_texture(&image.texture_descriptor);
let format_size = image.texture_descriptor.format.pixel_size();
render_queue.write_texture(
ImageCopyTexture {
texture: &texture,
mip_level: 0,
origin: Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
&image.data,
ImageDataLayout {
offset: 0,
bytes_per_row: Some(
std::num::NonZeroU32::new(
image.texture_descriptor.size.width * format_size as u32,
)
.unwrap(),
),
rows_per_image: if image.texture_descriptor.size.depth_or_array_layers > 1 {
std::num::NonZeroU32::new(image.texture_descriptor.size.height)
} else {
None
},
},
image.texture_descriptor.size,
);
texture
};
let texture = render_device.create_texture_with_data(
render_queue,
&image.texture_descriptor,
&image.data,
);

let texture_view = texture.create_view(
image
Expand Down

0 comments on commit f9ad051

Please sign in to comment.