Skip to content

Commit afe8b5f

Browse files
authored
Replace all usages of texture_descritor.size.* with the helper methods (#10227)
# Objective A follow-up PR for #10221 ## Changelog Replaced usages of texture_descriptor.size with the helper methods of `Image` through the entire engine codebase
1 parent faa1b57 commit afe8b5f

File tree

12 files changed

+57
-78
lines changed

12 files changed

+57
-78
lines changed

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use bevy_ecs::{
1717
query::{QueryItem, ROQueryItem},
1818
system::{lifetimeless::*, SystemParamItem, SystemState},
1919
};
20-
use bevy_math::{Affine3, Vec2, Vec4};
20+
use bevy_math::{Affine3, Vec4};
2121
use bevy_render::{
2222
batching::{
2323
batch_and_prepare_render_phase, write_batched_instance_buffer, GetBatchData,
@@ -374,7 +374,9 @@ impl FromWorld for MeshPipeline {
374374
let texture = render_device.create_texture(&image.texture_descriptor);
375375
let sampler = match image.sampler_descriptor {
376376
ImageSampler::Default => (**default_sampler).clone(),
377-
ImageSampler::Descriptor(descriptor) => render_device.create_sampler(&descriptor),
377+
ImageSampler::Descriptor(ref descriptor) => {
378+
render_device.create_sampler(descriptor)
379+
}
378380
};
379381

380382
let format_size = image.texture_descriptor.format.pixel_size();
@@ -388,7 +390,7 @@ impl FromWorld for MeshPipeline {
388390
&image.data,
389391
ImageDataLayout {
390392
offset: 0,
391-
bytes_per_row: Some(image.texture_descriptor.size.width * format_size as u32),
393+
bytes_per_row: Some(image.width() * format_size as u32),
392394
rows_per_image: None,
393395
},
394396
image.texture_descriptor.size,
@@ -400,10 +402,7 @@ impl FromWorld for MeshPipeline {
400402
texture_view,
401403
texture_format: image.texture_descriptor.format,
402404
sampler,
403-
size: Vec2::new(
404-
image.texture_descriptor.size.width as f32,
405-
image.texture_descriptor.size.height as f32,
406-
),
405+
size: image.size_f32(),
407406
mip_level_count: image.texture_descriptor.mip_level_count,
408407
}
409408
};

crates/bevy_render/src/camera/camera.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use bevy_window::{
2727
NormalizedWindowRef, PrimaryWindow, Window, WindowCreated, WindowRef, WindowResized,
2828
};
2929
use std::{borrow::Cow, ops::Range};
30-
use wgpu::{BlendState, Extent3d, LoadOp, TextureFormat};
30+
use wgpu::{BlendState, LoadOp, TextureFormat};
3131

3232
/// Render viewport configuration for the [`Camera`] component.
3333
///
@@ -509,9 +509,8 @@ impl NormalizedRenderTarget {
509509
}),
510510
NormalizedRenderTarget::Image(image_handle) => {
511511
let image = images.get(image_handle)?;
512-
let Extent3d { width, height, .. } = image.texture_descriptor.size;
513512
Some(RenderTargetInfo {
514-
physical_size: UVec2::new(width, height),
513+
physical_size: image.size(),
515514
scale_factor: 1.0,
516515
})
517516
}

crates/bevy_render/src/texture/fallback_image.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use bevy_ecs::{
44
prelude::{FromWorld, Res, ResMut},
55
system::{Resource, SystemParam},
66
};
7-
use bevy_math::Vec2;
87
use bevy_utils::HashMap;
98
use wgpu::{Extent3d, TextureFormat};
109

@@ -103,17 +102,14 @@ fn fallback_image_new(
103102
});
104103
let sampler = match image.sampler_descriptor {
105104
ImageSampler::Default => (**default_sampler).clone(),
106-
ImageSampler::Descriptor(descriptor) => render_device.create_sampler(&descriptor),
105+
ImageSampler::Descriptor(ref descriptor) => render_device.create_sampler(descriptor),
107106
};
108107
GpuImage {
109108
texture,
110109
texture_view,
111110
texture_format: image.texture_descriptor.format,
112111
sampler,
113-
size: Vec2::new(
114-
image.texture_descriptor.size.width as f32,
115-
image.texture_descriptor.size.height as f32,
116-
),
112+
size: image.size_f32(),
117113
mip_level_count: image.texture_descriptor.mip_level_count,
118114
}
119115
}

crates/bevy_render/src/texture/image.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,27 +254,32 @@ impl Image {
254254
value
255255
}
256256

257-
/// Returns the aspect ratio (height/width) of a 2D image.
258-
pub fn aspect_ratio(&self) -> f32 {
259-
self.height() as f32 / self.width() as f32
260-
}
261-
262257
/// Returns the width of a 2D image.
258+
#[inline]
263259
pub fn width(&self) -> u32 {
264260
self.texture_descriptor.size.width
265261
}
266262

267263
/// Returns the height of a 2D image.
264+
#[inline]
268265
pub fn height(&self) -> u32 {
269266
self.texture_descriptor.size.height
270267
}
271268

269+
/// Returns the aspect ratio (height/width) of a 2D image.
270+
#[inline]
271+
pub fn aspect_ratio(&self) -> f32 {
272+
self.height() as f32 / self.width() as f32
273+
}
274+
272275
/// Returns the size of a 2D image as f32.
276+
#[inline]
273277
pub fn size_f32(&self) -> Vec2 {
274278
Vec2::new(self.width() as f32, self.height() as f32)
275279
}
276280

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

321326
self.reinterpret_size(Extent3d {
322-
width: self.texture_descriptor.size.width,
323-
height: self.texture_descriptor.size.height / layers,
327+
width: self.width(),
328+
height: self.height() / layers,
324329
depth_or_array_layers: layers,
325330
});
326331
}

crates/bevy_render/src/texture/image_texture_conversion.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -165,38 +165,28 @@ impl Image {
165165
/// To convert [`Image`] to a different format see: [`Image::convert`].
166166
pub fn try_into_dynamic(self) -> Result<DynamicImage, IntoDynamicImageError> {
167167
match self.texture_descriptor.format {
168-
TextureFormat::R8Unorm => ImageBuffer::from_raw(
169-
self.texture_descriptor.size.width,
170-
self.texture_descriptor.size.height,
171-
self.data,
172-
)
173-
.map(DynamicImage::ImageLuma8),
174-
TextureFormat::Rg8Unorm => ImageBuffer::from_raw(
175-
self.texture_descriptor.size.width,
176-
self.texture_descriptor.size.height,
177-
self.data,
178-
)
179-
.map(DynamicImage::ImageLumaA8),
180-
TextureFormat::Rgba8UnormSrgb => ImageBuffer::from_raw(
181-
self.texture_descriptor.size.width,
182-
self.texture_descriptor.size.height,
183-
self.data,
184-
)
185-
.map(DynamicImage::ImageRgba8),
168+
TextureFormat::R8Unorm => ImageBuffer::from_raw(self.width(), self.height(), self.data)
169+
.map(DynamicImage::ImageLuma8),
170+
TextureFormat::Rg8Unorm => {
171+
ImageBuffer::from_raw(self.width(), self.height(), self.data)
172+
.map(DynamicImage::ImageLumaA8)
173+
}
174+
TextureFormat::Rgba8UnormSrgb => {
175+
ImageBuffer::from_raw(self.width(), self.height(), self.data)
176+
.map(DynamicImage::ImageRgba8)
177+
}
186178
// This format is commonly used as the format for the swapchain texture
187179
// This conversion is added here to support screenshots
188-
TextureFormat::Bgra8UnormSrgb | TextureFormat::Bgra8Unorm => ImageBuffer::from_raw(
189-
self.texture_descriptor.size.width,
190-
self.texture_descriptor.size.height,
191-
{
180+
TextureFormat::Bgra8UnormSrgb | TextureFormat::Bgra8Unorm => {
181+
ImageBuffer::from_raw(self.width(), self.height(), {
192182
let mut data = self.data;
193183
for bgra in data.chunks_exact_mut(4) {
194184
bgra.swap(0, 2);
195185
}
196186
data
197-
},
198-
)
199-
.map(DynamicImage::ImageRgba8),
187+
})
188+
.map(DynamicImage::ImageRgba8)
189+
}
200190
// Throw and error if conversion isn't supported
201191
texture_format => return Err(IntoDynamicImageError::UnsupportedFormat(texture_format)),
202192
}

crates/bevy_sprite/src/dynamic_texture_atlas_builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ impl DynamicTextureAtlasBuilder {
3636
texture: &Image,
3737
) -> Option<usize> {
3838
let allocation = self.atlas_allocator.allocate(size2(
39-
texture.texture_descriptor.size.width as i32 + self.padding,
40-
texture.texture_descriptor.size.height as i32 + self.padding,
39+
texture.width() as i32 + self.padding,
40+
texture.height() as i32 + self.padding,
4141
));
4242
if let Some(allocation) = allocation {
4343
let atlas_texture = textures.get_mut(&texture_atlas.texture).unwrap();
@@ -59,7 +59,7 @@ impl DynamicTextureAtlasBuilder {
5959
let mut rect = allocation.rectangle;
6060
rect.max.x -= self.padding;
6161
rect.max.y -= self.padding;
62-
let atlas_width = atlas_texture.texture_descriptor.size.width as usize;
62+
let atlas_width = atlas_texture.width() as usize;
6363
let rect_width = rect.width() as usize;
6464
let format_size = atlas_texture.texture_descriptor.format.pixel_size();
6565

crates/bevy_sprite/src/mesh2d/mesh.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bevy_ecs::{
88
query::{QueryItem, ROQueryItem},
99
system::{lifetimeless::*, SystemParamItem, SystemState},
1010
};
11-
use bevy_math::{Affine3, Vec2, Vec4};
11+
use bevy_math::{Affine3, Vec4};
1212
use bevy_reflect::Reflect;
1313
use bevy_render::{
1414
batching::{
@@ -297,7 +297,9 @@ impl FromWorld for Mesh2dPipeline {
297297
let texture = render_device.create_texture(&image.texture_descriptor);
298298
let sampler = match image.sampler_descriptor {
299299
ImageSampler::Default => (**default_sampler).clone(),
300-
ImageSampler::Descriptor(descriptor) => render_device.create_sampler(&descriptor),
300+
ImageSampler::Descriptor(ref descriptor) => {
301+
render_device.create_sampler(descriptor)
302+
}
301303
};
302304

303305
let format_size = image.texture_descriptor.format.pixel_size();
@@ -311,7 +313,7 @@ impl FromWorld for Mesh2dPipeline {
311313
&image.data,
312314
ImageDataLayout {
313315
offset: 0,
314-
bytes_per_row: Some(image.texture_descriptor.size.width * format_size as u32),
316+
bytes_per_row: Some(image.width() * format_size as u32),
315317
rows_per_image: None,
316318
},
317319
image.texture_descriptor.size,
@@ -323,10 +325,7 @@ impl FromWorld for Mesh2dPipeline {
323325
texture_view,
324326
texture_format: image.texture_descriptor.format,
325327
sampler,
326-
size: Vec2::new(
327-
image.texture_descriptor.size.width as f32,
328-
image.texture_descriptor.size.height as f32,
329-
),
328+
size: image.size_f32(),
330329
mip_level_count: image.texture_descriptor.mip_level_count,
331330
}
332331
};

crates/bevy_sprite/src/render/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ impl FromWorld for SpritePipeline {
9393
let texture = render_device.create_texture(&image.texture_descriptor);
9494
let sampler = match image.sampler_descriptor {
9595
ImageSampler::Default => (**default_sampler).clone(),
96-
ImageSampler::Descriptor(descriptor) => render_device.create_sampler(&descriptor),
96+
ImageSampler::Descriptor(ref descriptor) => {
97+
render_device.create_sampler(descriptor)
98+
}
9799
};
98100

99101
let format_size = image.texture_descriptor.format.pixel_size();
@@ -107,7 +109,7 @@ impl FromWorld for SpritePipeline {
107109
&image.data,
108110
ImageDataLayout {
109111
offset: 0,
110-
bytes_per_row: Some(image.texture_descriptor.size.width * format_size as u32),
112+
bytes_per_row: Some(image.width() * format_size as u32),
111113
rows_per_image: None,
112114
},
113115
image.texture_descriptor.size,
@@ -118,10 +120,7 @@ impl FromWorld for SpritePipeline {
118120
texture_view,
119121
texture_format: image.texture_descriptor.format,
120122
sampler,
121-
size: Vec2::new(
122-
image.texture_descriptor.size.width as f32,
123-
image.texture_descriptor.size.height as f32,
124-
),
123+
size: image.size_f32(),
125124
mip_level_count: image.texture_descriptor.mip_level_count,
126125
}
127126
};

crates/bevy_sprite/src/texture_atlas_builder.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl TextureAtlasBuilder {
105105
let rect_height = (packed_location.height() - padding.y) as usize;
106106
let rect_x = packed_location.x() as usize;
107107
let rect_y = packed_location.y() as usize;
108-
let atlas_width = atlas_texture.texture_descriptor.size.width as usize;
108+
let atlas_width = atlas_texture.width() as usize;
109109
let format_size = atlas_texture.texture_descriptor.format.pixel_size();
110110

111111
for (texture_y, bound_y) in (rect_y..rect_y + rect_height).enumerate() {
@@ -247,10 +247,7 @@ impl TextureAtlasBuilder {
247247
self.copy_converted_texture(&mut atlas_texture, texture, packed_location);
248248
}
249249
Ok(TextureAtlas {
250-
size: Vec2::new(
251-
atlas_texture.texture_descriptor.size.width as f32,
252-
atlas_texture.texture_descriptor.size.height as f32,
253-
),
250+
size: atlas_texture.size_f32(),
254251
texture: textures.add(atlas_texture),
255252
textures: texture_rects,
256253
texture_handles: Some(texture_ids),

crates/bevy_text/src/font_atlas_set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl FontAtlasSet {
107107
.texture_descriptor
108108
.size
109109
.height
110-
.max(glyph_texture.texture_descriptor.size.width);
110+
.max(glyph_texture.width());
111111
// Pick the higher of 512 or the smallest power of 2 greater than glyph_max_size
112112
let containing = (1u32 << (32 - glyph_max_size.leading_zeros())).max(512) as f32;
113113
font_atlases.push(FontAtlas::new(

0 commit comments

Comments
 (0)