Skip to content

Commit 73d68d6

Browse files
authored
Change GpuImage::size from UVec2 to Extent3d (#16815)
# Objective When preparing `GpuImage`s, we currently discard the `depth_or_array_layers` of the `Image`'s size by converting it into a `UVec2`. Fixes #16715. ## Solution Change `GpuImage::size` to `Extent3d`, and just pass that through when creating `GpuImage`s. Also copy the `aspect_ratio`, and `size` (now `size_2d` for disambiguation from the field) functions from `Image` to `GpuImage` for ease of use with 2D textures. I originally copied all size-related functions (like `width`, and `height`), but i think they are unnecessary considering how visible the `size` field on `GpuImage` is compared to `Image`. ## Testing Tested via `cargo r -p ci` for everything except docs, when generating docs it keeps spitting out a ton of ``` error[E0554]: `#![feature]` may not be used on the stable release channel --> crates/bevy_dylib/src/lib.rs:1:21 | 1 | #![cfg_attr(docsrs, feature(doc_auto_cfg))] | ``` Not sure why this is happening, but it also happens without my changes, so it's almost certainly some strange issue specific to my machine. ## Migration Guide - `GpuImage::size` is now an `Extent3d`. To easily get 2D size, use `size_2d()`.
1 parent bfa6553 commit 73d68d6

File tree

10 files changed

+43
-40
lines changed

10 files changed

+43
-40
lines changed

crates/bevy_pbr/src/render/mesh.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ impl FromWorld for MeshPipeline {
15231523
texture_view,
15241524
texture_format: image.texture_descriptor.format,
15251525
sampler,
1526-
size: image.size(),
1526+
size: image.texture_descriptor.size,
15271527
mip_level_count: image.texture_descriptor.mip_level_count,
15281528
}
15291529
};

crates/bevy_render/src/gpu_readback.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use bevy_ecs::{
2323
use bevy_image::{Image, TextureFormatPixelInfo};
2424
use bevy_reflect::Reflect;
2525
use bevy_render_macros::ExtractComponent;
26-
use bevy_utils::{default, tracing::warn, HashMap};
26+
use bevy_utils::{tracing::warn, HashMap};
2727
use encase::internal::ReadFrom;
2828
use encase::private::Reader;
2929
use encase::ShaderType;
@@ -239,17 +239,16 @@ fn prepare_buffers(
239239
match readback {
240240
Readback::Texture(image) => {
241241
if let Some(gpu_image) = gpu_images.get(image) {
242-
let size = Extent3d {
243-
width: gpu_image.size.x,
244-
height: gpu_image.size.y,
245-
..default()
246-
};
247-
let layout = layout_data(size.width, size.height, gpu_image.texture_format);
242+
let layout = layout_data(
243+
gpu_image.size.width,
244+
gpu_image.size.height,
245+
gpu_image.texture_format,
246+
);
248247
let buffer = buffer_pool.get(
249248
&render_device,
250249
get_aligned_size(
251-
size.width,
252-
size.height,
250+
gpu_image.size.width,
251+
gpu_image.size.height,
253252
gpu_image.texture_format.pixel_size() as u32,
254253
) as u64,
255254
);
@@ -259,7 +258,7 @@ fn prepare_buffers(
259258
src: ReadbackSource::Texture {
260259
texture: gpu_image.texture.clone(),
261260
layout,
262-
size,
261+
size: gpu_image.size,
263262
},
264263
buffer,
265264
rx,

crates/bevy_render/src/texture/fallback_image.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn fallback_image_new(
135135
texture_view,
136136
texture_format: image.texture_descriptor.format,
137137
sampler,
138-
size: image.size(),
138+
size: image.texture_descriptor.size,
139139
mip_level_count: image.texture_descriptor.mip_level_count,
140140
}
141141
}

crates/bevy_render/src/texture/gpu_image.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::{
66
use bevy_asset::AssetId;
77
use bevy_ecs::system::{lifetimeless::SRes, SystemParamItem};
88
use bevy_image::{Image, ImageSampler};
9-
use bevy_math::UVec2;
10-
use wgpu::{TextureFormat, TextureViewDescriptor};
9+
use bevy_math::{AspectRatio, UVec2};
10+
use wgpu::{Extent3d, TextureFormat, TextureViewDescriptor};
1111

1212
/// The GPU-representation of an [`Image`].
1313
/// Consists of the [`Texture`], its [`TextureView`] and the corresponding [`Sampler`], and the texture's size.
@@ -17,7 +17,7 @@ pub struct GpuImage {
1717
pub texture_view: TextureView,
1818
pub texture_format: TextureFormat,
1919
pub sampler: Sampler,
20-
pub size: UVec2,
20+
pub size: Extent3d,
2121
pub mip_level_count: u32,
2222
}
2323

@@ -53,7 +53,6 @@ impl RenderAsset for GpuImage {
5353
&image.data,
5454
);
5555

56-
let size = image.size();
5756
let texture_view = texture.create_view(
5857
image
5958
.texture_view_descriptor
@@ -73,8 +72,24 @@ impl RenderAsset for GpuImage {
7372
texture_view,
7473
texture_format: image.texture_descriptor.format,
7574
sampler,
76-
size,
75+
size: image.texture_descriptor.size,
7776
mip_level_count: image.texture_descriptor.mip_level_count,
7877
})
7978
}
8079
}
80+
81+
impl GpuImage {
82+
/// Returns the aspect ratio (width / height) of a 2D image.
83+
#[inline]
84+
pub fn aspect_ratio(&self) -> AspectRatio {
85+
AspectRatio::try_from_pixels(self.size.width, self.size.height).expect(
86+
"Failed to calculate aspect ratio: Image dimensions must be positive, non-zero values",
87+
)
88+
}
89+
90+
/// Returns the size of a 2D image.
91+
#[inline]
92+
pub fn size_2d(&self) -> UVec2 {
93+
UVec2::new(self.size.width, self.size.height)
94+
}
95+
}

crates/bevy_render/src/view/window/screenshot.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,8 @@ fn prepare_screenshots(
302302
continue;
303303
};
304304
let format = gpu_image.texture_format;
305-
let size = Extent3d {
306-
width: gpu_image.size.x,
307-
height: gpu_image.size.y,
308-
..default()
309-
};
310305
let (texture_view, state) = prepare_screenshot_state(
311-
size,
306+
gpu_image.size,
312307
format,
313308
&render_device,
314309
&screenshot_pipeline,
@@ -542,8 +537,8 @@ pub(crate) fn submit_screenshot_commands(world: &World, encoder: &mut CommandEnc
542537
warn!("Unknown image for screenshot, skipping: {:?}", image);
543538
continue;
544539
};
545-
let width = gpu_image.size.x;
546-
let height = gpu_image.size.y;
540+
let width = gpu_image.size.width;
541+
let height = gpu_image.size.height;
547542
let texture_format = gpu_image.texture_format;
548543
let texture_view = gpu_image.texture_view.deref();
549544
render_screenshot(

crates/bevy_sprite/src/mesh2d/mesh.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl FromWorld for Mesh2dPipeline {
313313
texture_view,
314314
texture_format: image.texture_descriptor.format,
315315
sampler,
316-
size: image.size(),
316+
size: image.texture_descriptor.size,
317317
mip_level_count: image.texture_descriptor.mip_level_count,
318318
}
319319
};

crates/bevy_sprite/src/render/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl FromWorld for SpritePipeline {
117117
texture_view,
118118
texture_format: image.texture_descriptor.format,
119119
sampler,
120-
size: image.size(),
120+
size: image.texture_descriptor.size,
121121
mip_level_count: image.texture_descriptor.mip_level_count,
122122
}
123123
};
@@ -676,7 +676,7 @@ pub fn prepare_sprite_image_bind_groups(
676676
continue;
677677
};
678678

679-
batch_image_size = gpu_image.size.as_vec2();
679+
batch_image_size = gpu_image.size_2d().as_vec2();
680680
batch_image_handle = extracted_sprite.image_handle_id;
681681
image_bind_groups
682682
.values

crates/bevy_ui/src/render/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ pub fn prepare_uinodes(
10521052
);
10531053
// Rescale atlases. This is done here because we need texture data that might not be available in Extract.
10541054
let atlas_extent = atlas_scaling
1055-
.map(|scaling| image.size.as_vec2() * scaling)
1055+
.map(|scaling| image.size_2d().as_vec2() * scaling)
10561056
.unwrap_or(uinode_rect.max);
10571057
if *flip_x {
10581058
core::mem::swap(&mut uinode_rect.max.x, &mut uinode_rect.min.x);
@@ -1127,7 +1127,7 @@ pub fn prepare_uinodes(
11271127
.get(extracted_uinode.image)
11281128
.expect("Image was checked during batching and should still exist");
11291129

1130-
let atlas_extent = image.size.as_vec2() * *atlas_scaling;
1130+
let atlas_extent = image.size_2d().as_vec2() * *atlas_scaling;
11311131

11321132
let color = extracted_uinode.color.to_f32_array();
11331133
for glyph in &extracted_uinodes.glyphs[range.clone()] {

crates/bevy_ui/src/render/ui_texture_slice_pipeline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ pub fn prepare_ui_slices(
442442
if let Some(gpu_image) = gpu_images.get(texture_slices.image) {
443443
batch_item_index = item_index;
444444
batch_image_handle = texture_slices.image;
445-
batch_image_size = gpu_image.size.as_vec2();
445+
batch_image_size = gpu_image.size_2d().as_vec2();
446446

447447
let new_batch = UiTextureSlicerBatch {
448448
range: vertices_index..vertices_index,
@@ -475,7 +475,7 @@ pub fn prepare_ui_slices(
475475
{
476476
if let Some(gpu_image) = gpu_images.get(texture_slices.image) {
477477
batch_image_handle = texture_slices.image;
478-
batch_image_size = gpu_image.size.as_vec2();
478+
batch_image_size = gpu_image.size_2d().as_vec2();
479479
existing_batch.as_mut().unwrap().1.image = texture_slices.image;
480480

481481
image_bind_groups

examples/app/headless_renderer.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -367,15 +367,9 @@ impl render_graph::Node for ImageCopyDriver {
367367
// That's why image in buffer can be little bit wider
368368
// This should be taken into account at copy from buffer stage
369369
let padded_bytes_per_row = RenderDevice::align_copy_bytes_per_row(
370-
(src_image.size.x as usize / block_dimensions.0 as usize) * block_size as usize,
370+
(src_image.size.width as usize / block_dimensions.0 as usize) * block_size as usize,
371371
);
372372

373-
let texture_extent = Extent3d {
374-
width: src_image.size.x,
375-
height: src_image.size.y,
376-
depth_or_array_layers: 1,
377-
};
378-
379373
encoder.copy_texture_to_buffer(
380374
src_image.texture.as_image_copy(),
381375
ImageCopyBuffer {
@@ -390,7 +384,7 @@ impl render_graph::Node for ImageCopyDriver {
390384
rows_per_image: None,
391385
},
392386
},
393-
texture_extent,
387+
src_image.size,
394388
);
395389

396390
let render_queue = world.get_resource::<RenderQueue>().unwrap();

0 commit comments

Comments
 (0)