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

Replace all usages of texture_descritor.size.* with the helper methods #10227

Merged
merged 5 commits into from
Oct 23, 2023
Merged
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
13 changes: 6 additions & 7 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use bevy_ecs::{
query::{QueryItem, ROQueryItem},
system::{lifetimeless::*, SystemParamItem, SystemState},
};
use bevy_math::{Affine3, Vec2, Vec4};
use bevy_math::{Affine3, Vec4};
use bevy_render::{
batching::{
batch_and_prepare_render_phase, write_batched_instance_buffer, GetBatchData,
Expand Down Expand Up @@ -374,7 +374,9 @@ impl FromWorld for MeshPipeline {
let texture = render_device.create_texture(&image.texture_descriptor);
let sampler = match image.sampler_descriptor {
ImageSampler::Default => (**default_sampler).clone(),
ImageSampler::Descriptor(descriptor) => render_device.create_sampler(&descriptor),
ImageSampler::Descriptor(ref descriptor) => {
render_device.create_sampler(descriptor)
}
};

let format_size = image.texture_descriptor.format.pixel_size();
Expand All @@ -388,7 +390,7 @@ impl FromWorld for MeshPipeline {
&image.data,
ImageDataLayout {
offset: 0,
bytes_per_row: Some(image.texture_descriptor.size.width * format_size as u32),
bytes_per_row: Some(image.width() * format_size as u32),
rows_per_image: None,
},
image.texture_descriptor.size,
Expand All @@ -400,10 +402,7 @@ impl FromWorld for MeshPipeline {
texture_view,
texture_format: image.texture_descriptor.format,
sampler,
size: Vec2::new(
image.texture_descriptor.size.width as f32,
image.texture_descriptor.size.height as f32,
),
size: image.size_f32(),
mip_level_count: image.texture_descriptor.mip_level_count,
}
};
Expand Down
5 changes: 2 additions & 3 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use bevy_window::{
NormalizedWindowRef, PrimaryWindow, Window, WindowCreated, WindowRef, WindowResized,
};
use std::{borrow::Cow, ops::Range};
use wgpu::{BlendState, Extent3d, LoadOp, TextureFormat};
use wgpu::{BlendState, LoadOp, TextureFormat};

/// Render viewport configuration for the [`Camera`] component.
///
Expand Down Expand Up @@ -509,9 +509,8 @@ impl NormalizedRenderTarget {
}),
NormalizedRenderTarget::Image(image_handle) => {
let image = images.get(image_handle)?;
let Extent3d { width, height, .. } = image.texture_descriptor.size;
Some(RenderTargetInfo {
physical_size: UVec2::new(width, height),
physical_size: image.size(),
scale_factor: 1.0,
})
}
Expand Down
8 changes: 2 additions & 6 deletions crates/bevy_render/src/texture/fallback_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use bevy_ecs::{
prelude::{FromWorld, Res, ResMut},
system::{Resource, SystemParam},
};
use bevy_math::Vec2;
use bevy_utils::HashMap;
use wgpu::{Extent3d, TextureFormat};

Expand Down Expand Up @@ -103,17 +102,14 @@ fn fallback_image_new(
});
let sampler = match image.sampler_descriptor {
ImageSampler::Default => (**default_sampler).clone(),
ImageSampler::Descriptor(descriptor) => render_device.create_sampler(&descriptor),
ImageSampler::Descriptor(ref descriptor) => render_device.create_sampler(descriptor),
};
GpuImage {
texture,
texture_view,
texture_format: image.texture_descriptor.format,
sampler,
size: Vec2::new(
image.texture_descriptor.size.width as f32,
image.texture_descriptor.size.height as f32,
),
size: image.size_f32(),
mip_level_count: image.texture_descriptor.mip_level_count,
}
}
Expand Down
21 changes: 13 additions & 8 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,27 +254,32 @@ impl Image {
value
}

/// Returns the aspect ratio (height/width) of a 2D image.
pub fn aspect_ratio(&self) -> f32 {
self.height() as f32 / self.width() as f32
}

/// Returns the width of a 2D image.
#[inline]
pub fn width(&self) -> u32 {
self.texture_descriptor.size.width
}

/// Returns the height of a 2D image.
#[inline]
pub fn height(&self) -> u32 {
self.texture_descriptor.size.height
}

/// Returns the aspect ratio (height/width) of a 2D image.
#[inline]
pub fn aspect_ratio(&self) -> f32 {
self.height() as f32 / self.width() as f32
}

/// Returns the size of a 2D image as f32.
#[inline]
pub fn size_f32(&self) -> Vec2 {
Vec2::new(self.width() as f32, self.height() as f32)
}

/// Returns the size of a 2D image.
#[inline]
pub fn size(&self) -> UVec2 {
UVec2::new(self.width(), self.height())
}
Expand Down Expand Up @@ -316,11 +321,11 @@ impl Image {
// Must be a stacked image, and the height must be divisible by layers.
assert!(self.texture_descriptor.dimension == TextureDimension::D2);
assert!(self.texture_descriptor.size.depth_or_array_layers == 1);
assert_eq!(self.texture_descriptor.size.height % layers, 0);
assert_eq!(self.height() % layers, 0);

self.reinterpret_size(Extent3d {
width: self.texture_descriptor.size.width,
height: self.texture_descriptor.size.height / layers,
width: self.width(),
height: self.height() / layers,
depth_or_array_layers: layers,
});
}
Expand Down
40 changes: 15 additions & 25 deletions crates/bevy_render/src/texture/image_texture_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,38 +165,28 @@ impl Image {
/// To convert [`Image`] to a different format see: [`Image::convert`].
pub fn try_into_dynamic(self) -> Result<DynamicImage, IntoDynamicImageError> {
match self.texture_descriptor.format {
TextureFormat::R8Unorm => ImageBuffer::from_raw(
self.texture_descriptor.size.width,
self.texture_descriptor.size.height,
self.data,
)
.map(DynamicImage::ImageLuma8),
TextureFormat::Rg8Unorm => ImageBuffer::from_raw(
self.texture_descriptor.size.width,
self.texture_descriptor.size.height,
self.data,
)
.map(DynamicImage::ImageLumaA8),
TextureFormat::Rgba8UnormSrgb => ImageBuffer::from_raw(
self.texture_descriptor.size.width,
self.texture_descriptor.size.height,
self.data,
)
.map(DynamicImage::ImageRgba8),
TextureFormat::R8Unorm => ImageBuffer::from_raw(self.width(), self.height(), self.data)
.map(DynamicImage::ImageLuma8),
TextureFormat::Rg8Unorm => {
ImageBuffer::from_raw(self.width(), self.height(), self.data)
.map(DynamicImage::ImageLumaA8)
}
TextureFormat::Rgba8UnormSrgb => {
ImageBuffer::from_raw(self.width(), self.height(), self.data)
.map(DynamicImage::ImageRgba8)
}
// This format is commonly used as the format for the swapchain texture
// This conversion is added here to support screenshots
TextureFormat::Bgra8UnormSrgb | TextureFormat::Bgra8Unorm => ImageBuffer::from_raw(
self.texture_descriptor.size.width,
self.texture_descriptor.size.height,
{
TextureFormat::Bgra8UnormSrgb | TextureFormat::Bgra8Unorm => {
ImageBuffer::from_raw(self.width(), self.height(), {
let mut data = self.data;
for bgra in data.chunks_exact_mut(4) {
bgra.swap(0, 2);
}
data
},
)
.map(DynamicImage::ImageRgba8),
})
.map(DynamicImage::ImageRgba8)
}
// Throw and error if conversion isn't supported
texture_format => return Err(IntoDynamicImageError::UnsupportedFormat(texture_format)),
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_sprite/src/dynamic_texture_atlas_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ impl DynamicTextureAtlasBuilder {
texture: &Image,
) -> Option<usize> {
let allocation = self.atlas_allocator.allocate(size2(
texture.texture_descriptor.size.width as i32 + self.padding,
texture.texture_descriptor.size.height as i32 + self.padding,
texture.width() as i32 + self.padding,
texture.height() as i32 + self.padding,
));
if let Some(allocation) = allocation {
let atlas_texture = textures.get_mut(&texture_atlas.texture).unwrap();
Expand All @@ -59,7 +59,7 @@ impl DynamicTextureAtlasBuilder {
let mut rect = allocation.rectangle;
rect.max.x -= self.padding;
rect.max.y -= self.padding;
let atlas_width = atlas_texture.texture_descriptor.size.width as usize;
let atlas_width = atlas_texture.width() as usize;
let rect_width = rect.width() as usize;
let format_size = atlas_texture.texture_descriptor.format.pixel_size();

Expand Down
13 changes: 6 additions & 7 deletions crates/bevy_sprite/src/mesh2d/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy_ecs::{
query::{QueryItem, ROQueryItem},
system::{lifetimeless::*, SystemParamItem, SystemState},
};
use bevy_math::{Affine3, Vec2, Vec4};
use bevy_math::{Affine3, Vec4};
use bevy_reflect::Reflect;
use bevy_render::{
batching::{
Expand Down Expand Up @@ -297,7 +297,9 @@ impl FromWorld for Mesh2dPipeline {
let texture = render_device.create_texture(&image.texture_descriptor);
let sampler = match image.sampler_descriptor {
ImageSampler::Default => (**default_sampler).clone(),
ImageSampler::Descriptor(descriptor) => render_device.create_sampler(&descriptor),
ImageSampler::Descriptor(ref descriptor) => {
render_device.create_sampler(descriptor)
}
};

let format_size = image.texture_descriptor.format.pixel_size();
Expand All @@ -311,7 +313,7 @@ impl FromWorld for Mesh2dPipeline {
&image.data,
ImageDataLayout {
offset: 0,
bytes_per_row: Some(image.texture_descriptor.size.width * format_size as u32),
bytes_per_row: Some(image.width() * format_size as u32),
rows_per_image: None,
},
image.texture_descriptor.size,
Expand All @@ -323,10 +325,7 @@ impl FromWorld for Mesh2dPipeline {
texture_view,
texture_format: image.texture_descriptor.format,
sampler,
size: Vec2::new(
image.texture_descriptor.size.width as f32,
image.texture_descriptor.size.height as f32,
),
size: image.size_f32(),
mip_level_count: image.texture_descriptor.mip_level_count,
}
};
Expand Down
11 changes: 5 additions & 6 deletions crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ impl FromWorld for SpritePipeline {
let texture = render_device.create_texture(&image.texture_descriptor);
let sampler = match image.sampler_descriptor {
ImageSampler::Default => (**default_sampler).clone(),
ImageSampler::Descriptor(descriptor) => render_device.create_sampler(&descriptor),
ImageSampler::Descriptor(ref descriptor) => {
render_device.create_sampler(descriptor)
}
};

let format_size = image.texture_descriptor.format.pixel_size();
Expand All @@ -107,7 +109,7 @@ impl FromWorld for SpritePipeline {
&image.data,
ImageDataLayout {
offset: 0,
bytes_per_row: Some(image.texture_descriptor.size.width * format_size as u32),
bytes_per_row: Some(image.width() * format_size as u32),
rows_per_image: None,
},
image.texture_descriptor.size,
Expand All @@ -118,10 +120,7 @@ impl FromWorld for SpritePipeline {
texture_view,
texture_format: image.texture_descriptor.format,
sampler,
size: Vec2::new(
image.texture_descriptor.size.width as f32,
image.texture_descriptor.size.height as f32,
),
size: image.size_f32(),
mip_level_count: image.texture_descriptor.mip_level_count,
}
};
Expand Down
7 changes: 2 additions & 5 deletions crates/bevy_sprite/src/texture_atlas_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl TextureAtlasBuilder {
let rect_height = (packed_location.height() - padding.y) as usize;
let rect_x = packed_location.x() as usize;
let rect_y = packed_location.y() as usize;
let atlas_width = atlas_texture.texture_descriptor.size.width as usize;
let atlas_width = atlas_texture.width() as usize;
let format_size = atlas_texture.texture_descriptor.format.pixel_size();

for (texture_y, bound_y) in (rect_y..rect_y + rect_height).enumerate() {
Expand Down Expand Up @@ -247,10 +247,7 @@ impl TextureAtlasBuilder {
self.copy_converted_texture(&mut atlas_texture, texture, packed_location);
}
Ok(TextureAtlas {
size: Vec2::new(
atlas_texture.texture_descriptor.size.width as f32,
atlas_texture.texture_descriptor.size.height as f32,
),
size: atlas_texture.size_f32(),
texture: textures.add(atlas_texture),
textures: texture_rects,
texture_handles: Some(texture_ids),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_text/src/font_atlas_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl FontAtlasSet {
.texture_descriptor
.size
.height
.max(glyph_texture.texture_descriptor.size.width);
.max(glyph_texture.width());
// Pick the higher of 512 or the smallest power of 2 greater than glyph_max_size
let containing = (1u32 << (32 - glyph_max_size.leading_zeros())).max(512) as f32;
font_atlases.push(FontAtlas::new(
Expand Down
5 changes: 1 addition & 4 deletions crates/bevy_ui/src/widget/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ pub fn update_image_content_size_system(

for (mut content_size, image, mut image_size) in &mut query {
if let Some(texture) = textures.get(&image.texture) {
let size = Vec2::new(
texture.texture_descriptor.size.width as f32,
texture.texture_descriptor.size.height as f32,
);
let size = texture.size_f32();
// Update only if size or scale factor has changed to avoid needless layout calculations
if size != image_size.size
|| combined_scale_factor != *previous_combined_scale_factor
Expand Down
4 changes: 1 addition & 3 deletions examples/3d/skybox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ fn asset_loaded(
// NOTE: PNGs do not have any metadata that could indicate they contain a cubemap texture,
// so they appear as one texture. The following code reconfigures the texture as necessary.
if image.texture_descriptor.array_layer_count() == 1 {
image.reinterpret_stacked_2d_as_array(
image.texture_descriptor.size.height / image.texture_descriptor.size.width,
);
image.reinterpret_stacked_2d_as_array(image.height() / image.width());
image.texture_view_descriptor = Some(TextureViewDescriptor {
dimension: Some(TextureViewDimension::Cube),
..default()
Expand Down