From b62b14c2936ac26a6ee50cf326aae3fd182840a8 Mon Sep 17 00:00:00 2001 From: atlv Date: Thu, 26 Jun 2025 16:53:49 -0400 Subject: [PATCH] Add UVec to_extents helper method (#19807) # Objective - Simplify common usecase ## Solution - Helper trait --- crates/bevy_anti_aliasing/src/smaa/mod.rs | 29 ++++++---------- crates/bevy_anti_aliasing/src/taa/mod.rs | 12 +++---- crates/bevy_core_pipeline/src/bloom/mod.rs | 21 +++++------- crates/bevy_core_pipeline/src/core_2d/mod.rs | 15 +++----- .../core_3d/main_transmissive_pass_3d_node.rs | 9 ++--- crates/bevy_core_pipeline/src/core_3d/mod.rs | 32 +++++------------ .../src/deferred/copy_lighting_id.rs | 14 ++------ .../src/experimental/mip_generation/mod.rs | 6 +--- .../bevy_core_pipeline/src/tonemapping/mod.rs | 6 +--- crates/bevy_image/src/image.rs | 34 +++++++++++++------ crates/bevy_pbr/src/atmosphere/resources.rs | 25 +++----------- crates/bevy_pbr/src/ssao/mod.rs | 7 ++-- crates/bevy_render/src/view/mod.rs | 14 +++----- .../bevy_render/src/view/window/screenshot.rs | 8 ++--- crates/bevy_solari/Cargo.toml | 1 + crates/bevy_solari/src/pathtracer/prepare.rs | 11 ++---- crates/bevy_sprite/src/sprite.rs | 10 ++---- crates/bevy_sprite/src/tilemap_chunk/mod.rs | 12 ++----- crates/bevy_text/src/font_atlas.rs | 10 ++---- crates/bevy_ui/src/widget/viewport.rs | 16 +++------ examples/stress_tests/many_cubes.rs | 6 +--- 21 files changed, 100 insertions(+), 198 deletions(-) diff --git a/crates/bevy_anti_aliasing/src/smaa/mod.rs b/crates/bevy_anti_aliasing/src/smaa/mod.rs index bb082c5a01..3996f389d0 100644 --- a/crates/bevy_anti_aliasing/src/smaa/mod.rs +++ b/crates/bevy_anti_aliasing/src/smaa/mod.rs @@ -50,7 +50,7 @@ use bevy_ecs::{ system::{lifetimeless::Read, Commands, Query, Res, ResMut}, world::{FromWorld, World}, }; -use bevy_image::{BevyDefault, Image}; +use bevy_image::{BevyDefault, Image, ToExtents}; use bevy_math::{vec4, Vec4}; use bevy_reflect::{std_traits::ReflectDefault, Reflect}; use bevy_render::{ @@ -64,14 +64,13 @@ use bevy_render::{ binding_types::{sampler, texture_2d, uniform_buffer}, AddressMode, BindGroup, BindGroupEntries, BindGroupLayout, BindGroupLayoutEntries, CachedRenderPipelineId, ColorTargetState, ColorWrites, CompareFunction, DepthStencilState, - DynamicUniformBuffer, Extent3d, FilterMode, FragmentState, LoadOp, MultisampleState, - Operations, PipelineCache, PrimitiveState, RenderPassColorAttachment, - RenderPassDepthStencilAttachment, RenderPassDescriptor, RenderPipeline, - RenderPipelineDescriptor, SamplerBindingType, SamplerDescriptor, Shader, ShaderDefVal, - ShaderStages, ShaderType, SpecializedRenderPipeline, SpecializedRenderPipelines, - StencilFaceState, StencilOperation, StencilState, StoreOp, TextureDescriptor, - TextureDimension, TextureFormat, TextureSampleType, TextureUsages, TextureView, - VertexState, + DynamicUniformBuffer, FilterMode, FragmentState, LoadOp, MultisampleState, Operations, + PipelineCache, PrimitiveState, RenderPassColorAttachment, RenderPassDepthStencilAttachment, + RenderPassDescriptor, RenderPipeline, RenderPipelineDescriptor, SamplerBindingType, + SamplerDescriptor, Shader, ShaderDefVal, ShaderStages, ShaderType, + SpecializedRenderPipeline, SpecializedRenderPipelines, StencilFaceState, StencilOperation, + StencilState, StoreOp, TextureDescriptor, TextureDimension, TextureFormat, + TextureSampleType, TextureUsages, TextureView, VertexState, }, renderer::{RenderContext, RenderDevice, RenderQueue}, texture::{CachedTexture, GpuImage, TextureCache}, @@ -704,18 +703,12 @@ fn prepare_smaa_textures( continue; }; - let texture_size = Extent3d { - width: texture_size.x, - height: texture_size.y, - depth_or_array_layers: 1, - }; - // Create the two-channel RG texture for phase 1 (edge detection). let edge_detection_color_texture = texture_cache.get( &render_device, TextureDescriptor { label: Some("SMAA edge detection color texture"), - size: texture_size, + size: texture_size.to_extents(), mip_level_count: 1, sample_count: 1, dimension: TextureDimension::D2, @@ -730,7 +723,7 @@ fn prepare_smaa_textures( &render_device, TextureDescriptor { label: Some("SMAA edge detection stencil texture"), - size: texture_size, + size: texture_size.to_extents(), mip_level_count: 1, sample_count: 1, dimension: TextureDimension::D2, @@ -746,7 +739,7 @@ fn prepare_smaa_textures( &render_device, TextureDescriptor { label: Some("SMAA blend texture"), - size: texture_size, + size: texture_size.to_extents(), mip_level_count: 1, sample_count: 1, dimension: TextureDimension::D2, diff --git a/crates/bevy_anti_aliasing/src/taa/mod.rs b/crates/bevy_anti_aliasing/src/taa/mod.rs index 6a00e9c0cf..442a268e2a 100644 --- a/crates/bevy_anti_aliasing/src/taa/mod.rs +++ b/crates/bevy_anti_aliasing/src/taa/mod.rs @@ -15,7 +15,7 @@ use bevy_ecs::{ system::{Commands, Query, Res, ResMut}, world::{FromWorld, World}, }; -use bevy_image::BevyDefault as _; +use bevy_image::{BevyDefault as _, ToExtents}; use bevy_math::vec2; use bevy_reflect::{std_traits::ReflectDefault, Reflect}; use bevy_render::{ @@ -25,8 +25,8 @@ use bevy_render::{ render_resource::{ binding_types::{sampler, texture_2d, texture_depth_2d}, BindGroupEntries, BindGroupLayout, BindGroupLayoutEntries, CachedRenderPipelineId, - ColorTargetState, ColorWrites, Extent3d, FilterMode, FragmentState, MultisampleState, - Operations, PipelineCache, PrimitiveState, RenderPassColorAttachment, RenderPassDescriptor, + ColorTargetState, ColorWrites, FilterMode, FragmentState, MultisampleState, Operations, + PipelineCache, PrimitiveState, RenderPassColorAttachment, RenderPassDescriptor, RenderPipelineDescriptor, Sampler, SamplerBindingType, SamplerDescriptor, Shader, ShaderStages, SpecializedRenderPipeline, SpecializedRenderPipelines, TextureDescriptor, TextureDimension, TextureFormat, TextureSampleType, TextureUsages, @@ -420,11 +420,7 @@ fn prepare_taa_history_textures( if let Some(physical_target_size) = camera.physical_target_size { let mut texture_descriptor = TextureDescriptor { label: None, - size: Extent3d { - depth_or_array_layers: 1, - width: physical_target_size.x, - height: physical_target_size.y, - }, + size: physical_target_size.to_extents(), mip_level_count: 1, sample_count: 1, dimension: TextureDimension::D2, diff --git a/crates/bevy_core_pipeline/src/bloom/mod.rs b/crates/bevy_core_pipeline/src/bloom/mod.rs index 65e51c8472..901275e01c 100644 --- a/crates/bevy_core_pipeline/src/bloom/mod.rs +++ b/crates/bevy_core_pipeline/src/bloom/mod.rs @@ -2,6 +2,7 @@ mod downsampling_pipeline; mod settings; mod upsampling_pipeline; +use bevy_image::ToExtents; pub use settings::{Bloom, BloomCompositeMode, BloomPrefilter}; use crate::{ @@ -347,26 +348,22 @@ fn prepare_bloom_textures( views: Query<(Entity, &ExtractedCamera, &Bloom)>, ) { for (entity, camera, bloom) in &views { - if let Some(UVec2 { - x: width, - y: height, - }) = camera.physical_viewport_size - { + if let Some(viewport) = camera.physical_viewport_size { // How many times we can halve the resolution minus one so we don't go unnecessarily low let mip_count = bloom.max_mip_dimension.ilog2().max(2) - 1; - let mip_height_ratio = if height != 0 { - bloom.max_mip_dimension as f32 / height as f32 + let mip_height_ratio = if viewport.y != 0 { + bloom.max_mip_dimension as f32 / viewport.y as f32 } else { 0. }; let texture_descriptor = TextureDescriptor { label: Some("bloom_texture"), - size: Extent3d { - width: ((width as f32 * mip_height_ratio).round() as u32).max(1), - height: ((height as f32 * mip_height_ratio).round() as u32).max(1), - depth_or_array_layers: 1, - }, + size: (viewport.as_vec2() * mip_height_ratio) + .round() + .as_uvec2() + .max(UVec2::ONE) + .to_extents(), mip_level_count: mip_count, sample_count: 1, dimension: TextureDimension::D2, diff --git a/crates/bevy_core_pipeline/src/core_2d/mod.rs b/crates/bevy_core_pipeline/src/core_2d/mod.rs index 725ac38ed9..94232fbb05 100644 --- a/crates/bevy_core_pipeline/src/core_2d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_2d/mod.rs @@ -34,6 +34,7 @@ pub mod graph { use core::ops::Range; use bevy_asset::UntypedAssetId; +use bevy_image::ToExtents; use bevy_platform::collections::{HashMap, HashSet}; use bevy_render::{ batching::gpu_preprocessing::GpuPreprocessingMode, @@ -58,8 +59,8 @@ use bevy_render::{ ViewSortedRenderPhases, }, render_resource::{ - BindGroupId, CachedRenderPipelineId, Extent3d, TextureDescriptor, TextureDimension, - TextureFormat, TextureUsages, + BindGroupId, CachedRenderPipelineId, TextureDescriptor, TextureDimension, TextureFormat, + TextureUsages, }, renderer::RenderDevice, sync_world::MainEntity, @@ -474,16 +475,10 @@ pub fn prepare_core_2d_depth_textures( let cached_texture = textures .entry(camera.target.clone()) .or_insert_with(|| { - // The size of the depth texture - let size = Extent3d { - depth_or_array_layers: 1, - width: physical_target_size.x, - height: physical_target_size.y, - }; - let descriptor = TextureDescriptor { label: Some("view_depth_texture"), - size, + // The size of the depth texture + size: physical_target_size.to_extents(), mip_level_count: 1, sample_count: msaa.samples(), dimension: TextureDimension::D2, diff --git a/crates/bevy_core_pipeline/src/core_3d/main_transmissive_pass_3d_node.rs b/crates/bevy_core_pipeline/src/core_3d/main_transmissive_pass_3d_node.rs index 0a2e98f0bf..8c656171e7 100644 --- a/crates/bevy_core_pipeline/src/core_3d/main_transmissive_pass_3d_node.rs +++ b/crates/bevy_core_pipeline/src/core_3d/main_transmissive_pass_3d_node.rs @@ -1,11 +1,12 @@ use super::{Camera3d, ViewTransmissionTexture}; use crate::core_3d::Transmissive3d; use bevy_ecs::{prelude::*, query::QueryItem}; +use bevy_image::ToExtents; use bevy_render::{ camera::ExtractedCamera, render_graph::{NodeRunError, RenderGraphContext, ViewNode}, render_phase::ViewSortedRenderPhases, - render_resource::{Extent3d, RenderPassDescriptor, StoreOp}, + render_resource::{RenderPassDescriptor, StoreOp}, renderer::RenderContext, view::{ExtractedView, ViewDepthTexture, ViewTarget}, }; @@ -85,11 +86,7 @@ impl ViewNode for MainTransmissivePass3dNode { render_context.command_encoder().copy_texture_to_texture( target.main_texture().as_image_copy(), transmission.texture.as_image_copy(), - Extent3d { - width: physical_target_size.x, - height: physical_target_size.y, - depth_or_array_layers: 1, - }, + physical_target_size.to_extents(), ); let mut render_pass = diff --git a/crates/bevy_core_pipeline/src/core_3d/mod.rs b/crates/bevy_core_pipeline/src/core_3d/mod.rs index 0ff61db842..6fcccd3a72 100644 --- a/crates/bevy_core_pipeline/src/core_3d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_3d/mod.rs @@ -85,7 +85,7 @@ use bevy_app::{App, Plugin, PostUpdate}; use bevy_asset::UntypedAssetId; use bevy_color::LinearRgba; use bevy_ecs::prelude::*; -use bevy_image::BevyDefault; +use bevy_image::{BevyDefault, ToExtents}; use bevy_math::FloatOrd; use bevy_platform::collections::{HashMap, HashSet}; use bevy_render::{ @@ -99,8 +99,8 @@ use bevy_render::{ ViewSortedRenderPhases, }, render_resource::{ - CachedRenderPipelineId, Extent3d, FilterMode, Sampler, SamplerDescriptor, Texture, - TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, TextureView, + CachedRenderPipelineId, FilterMode, Sampler, SamplerDescriptor, Texture, TextureDescriptor, + TextureDimension, TextureFormat, TextureUsages, TextureView, }, renderer::RenderDevice, sync_world::{MainEntity, RenderEntity}, @@ -811,20 +811,14 @@ pub fn prepare_core_3d_depth_textures( let cached_texture = textures .entry((camera.target.clone(), msaa)) .or_insert_with(|| { - // The size of the depth texture - let size = Extent3d { - depth_or_array_layers: 1, - width: physical_target_size.x, - height: physical_target_size.y, - }; - let usage = *render_target_usage .get(&camera.target.clone()) .expect("The depth texture usage should already exist for this target"); let descriptor = TextureDescriptor { label: Some("view_depth_texture"), - size, + // The size of the depth texture + size: physical_target_size.to_extents(), mip_level_count: 1, sample_count: msaa.samples(), dimension: TextureDimension::D2, @@ -897,13 +891,6 @@ pub fn prepare_core_3d_transmission_textures( .or_insert_with(|| { let usage = TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST; - // The size of the transmission texture - let size = Extent3d { - depth_or_array_layers: 1, - width: physical_target_size.x, - height: physical_target_size.y, - }; - let format = if view.hdr { ViewTarget::TEXTURE_FORMAT_HDR } else { @@ -912,7 +899,8 @@ pub fn prepare_core_3d_transmission_textures( let descriptor = TextureDescriptor { label: Some("view_transmission_texture"), - size, + // The size of the transmission texture + size: physical_target_size.to_extents(), mip_level_count: 1, sample_count: 1, // No need for MSAA, as we'll only copy the main texture here dimension: TextureDimension::D2, @@ -1023,11 +1011,7 @@ pub fn prepare_prepass_textures( continue; }; - let size = Extent3d { - depth_or_array_layers: 1, - width: physical_target_size.x, - height: physical_target_size.y, - }; + let size = physical_target_size.to_extents(); let cached_depth_texture = depth_prepass.then(|| { depth_textures diff --git a/crates/bevy_core_pipeline/src/deferred/copy_lighting_id.rs b/crates/bevy_core_pipeline/src/deferred/copy_lighting_id.rs index 2cf44a4015..bfad57757f 100644 --- a/crates/bevy_core_pipeline/src/deferred/copy_lighting_id.rs +++ b/crates/bevy_core_pipeline/src/deferred/copy_lighting_id.rs @@ -5,7 +5,7 @@ use crate::{ use bevy_app::prelude::*; use bevy_asset::{embedded_asset, load_embedded_asset}; use bevy_ecs::prelude::*; -use bevy_math::UVec2; +use bevy_image::ToExtents; use bevy_render::{ camera::ExtractedCamera, render_resource::{binding_types::texture_2d, *}, @@ -178,18 +178,10 @@ fn prepare_deferred_lighting_id_textures( views: Query<(Entity, &ExtractedCamera), With>, ) { for (entity, camera) in &views { - if let Some(UVec2 { - x: width, - y: height, - }) = camera.physical_target_size - { + if let Some(physical_target_size) = camera.physical_target_size { let texture_descriptor = TextureDescriptor { label: Some("deferred_lighting_id_depth_texture_a"), - size: Extent3d { - width, - height, - depth_or_array_layers: 1, - }, + size: physical_target_size.to_extents(), mip_level_count: 1, sample_count: 1, dimension: TextureDimension::D2, diff --git a/crates/bevy_core_pipeline/src/experimental/mip_generation/mod.rs b/crates/bevy_core_pipeline/src/experimental/mip_generation/mod.rs index 1223ed35ec..f7df3ad1b6 100644 --- a/crates/bevy_core_pipeline/src/experimental/mip_generation/mod.rs +++ b/crates/bevy_core_pipeline/src/experimental/mip_generation/mod.rs @@ -529,11 +529,7 @@ pub fn create_depth_pyramid_dummy_texture( render_device .create_texture(&TextureDescriptor { label: Some(texture_label), - size: Extent3d { - width: 1, - height: 1, - depth_or_array_layers: 1, - }, + size: Extent3d::default(), mip_level_count: 1, sample_count: 1, dimension: TextureDimension::D2, diff --git a/crates/bevy_core_pipeline/src/tonemapping/mod.rs b/crates/bevy_core_pipeline/src/tonemapping/mod.rs index 7453b2bf19..f4d88357db 100644 --- a/crates/bevy_core_pipeline/src/tonemapping/mod.rs +++ b/crates/bevy_core_pipeline/src/tonemapping/mod.rs @@ -448,11 +448,7 @@ pub fn lut_placeholder() -> Image { Image { data: Some(data), texture_descriptor: TextureDescriptor { - size: Extent3d { - width: 1, - height: 1, - depth_or_array_layers: 1, - }, + size: Extent3d::default(), format, dimension: TextureDimension::D3, label: None, diff --git a/crates/bevy_image/src/image.rs b/crates/bevy_image/src/image.rs index c92f947c5f..a5ced88ec6 100644 --- a/crates/bevy_image/src/image.rs +++ b/crates/bevy_image/src/image.rs @@ -337,6 +337,28 @@ impl ImageFormat { } } +pub trait ToExtents { + fn to_extents(self) -> Extent3d; +} +impl ToExtents for UVec2 { + fn to_extents(self) -> Extent3d { + Extent3d { + width: self.x, + height: self.y, + depth_or_array_layers: 1, + } + } +} +impl ToExtents for UVec3 { + fn to_extents(self) -> Extent3d { + Extent3d { + width: self.x, + height: self.y, + depth_or_array_layers: self.z, + } + } +} + #[derive(Asset, Debug, Clone)] #[cfg_attr( feature = "bevy_reflect", @@ -772,11 +794,7 @@ impl Image { debug_assert!(format.pixel_size() == 4); let data = vec![255, 255, 255, 0]; Image::new( - Extent3d { - width: 1, - height: 1, - depth_or_array_layers: 1, - }, + Extent3d::default(), TextureDimension::D2, data, format, @@ -786,11 +804,7 @@ impl Image { /// Creates a new uninitialized 1x1x1 image pub fn default_uninit() -> Image { Image::new_uninit( - Extent3d { - width: 1, - height: 1, - depth_or_array_layers: 1, - }, + Extent3d::default(), TextureDimension::D2, TextureFormat::bevy_default(), RenderAssetUsages::default(), diff --git a/crates/bevy_pbr/src/atmosphere/resources.rs b/crates/bevy_pbr/src/atmosphere/resources.rs index aa682fbb03..5a75fbe4d1 100644 --- a/crates/bevy_pbr/src/atmosphere/resources.rs +++ b/crates/bevy_pbr/src/atmosphere/resources.rs @@ -8,6 +8,7 @@ use bevy_ecs::{ system::{Commands, Query, Res, ResMut}, world::{FromWorld, World}, }; +use bevy_image::ToExtents; use bevy_math::{Mat4, Vec3}; use bevy_render::{ camera::Camera, @@ -434,11 +435,7 @@ pub(super) fn prepare_atmosphere_textures( &render_device, TextureDescriptor { label: Some("transmittance_lut"), - size: Extent3d { - width: lut_settings.transmittance_lut_size.x, - height: lut_settings.transmittance_lut_size.y, - depth_or_array_layers: 1, - }, + size: lut_settings.transmittance_lut_size.to_extents(), mip_level_count: 1, sample_count: 1, dimension: TextureDimension::D2, @@ -452,11 +449,7 @@ pub(super) fn prepare_atmosphere_textures( &render_device, TextureDescriptor { label: Some("multiscattering_lut"), - size: Extent3d { - width: lut_settings.multiscattering_lut_size.x, - height: lut_settings.multiscattering_lut_size.y, - depth_or_array_layers: 1, - }, + size: lut_settings.multiscattering_lut_size.to_extents(), mip_level_count: 1, sample_count: 1, dimension: TextureDimension::D2, @@ -470,11 +463,7 @@ pub(super) fn prepare_atmosphere_textures( &render_device, TextureDescriptor { label: Some("sky_view_lut"), - size: Extent3d { - width: lut_settings.sky_view_lut_size.x, - height: lut_settings.sky_view_lut_size.y, - depth_or_array_layers: 1, - }, + size: lut_settings.sky_view_lut_size.to_extents(), mip_level_count: 1, sample_count: 1, dimension: TextureDimension::D2, @@ -488,11 +477,7 @@ pub(super) fn prepare_atmosphere_textures( &render_device, TextureDescriptor { label: Some("aerial_view_lut"), - size: Extent3d { - width: lut_settings.aerial_view_lut_size.x, - height: lut_settings.aerial_view_lut_size.y, - depth_or_array_layers: lut_settings.aerial_view_lut_size.z, - }, + size: lut_settings.aerial_view_lut_size.to_extents(), mip_level_count: 1, sample_count: 1, dimension: TextureDimension::D3, diff --git a/crates/bevy_pbr/src/ssao/mod.rs b/crates/bevy_pbr/src/ssao/mod.rs index dc3ea865f2..7d56495605 100644 --- a/crates/bevy_pbr/src/ssao/mod.rs +++ b/crates/bevy_pbr/src/ssao/mod.rs @@ -15,6 +15,7 @@ use bevy_ecs::{ system::{Commands, Query, Res, ResMut}, world::{FromWorld, World}, }; +use bevy_image::ToExtents; use bevy_reflect::{std_traits::ReflectDefault, Reflect}; use bevy_render::{ camera::{ExtractedCamera, TemporalJitter}, @@ -536,11 +537,7 @@ fn prepare_ssao_textures( let Some(physical_viewport_size) = camera.physical_viewport_size else { continue; }; - let size = Extent3d { - width: physical_viewport_size.x, - height: physical_viewport_size.y, - depth_or_array_layers: 1, - }; + let size = physical_viewport_size.to_extents(); let preprocessed_depth_texture = texture_cache.get( &render_device, diff --git a/crates/bevy_render/src/view/mod.rs b/crates/bevy_render/src/view/mod.rs index 7596799061..a348b361c7 100644 --- a/crates/bevy_render/src/view/mod.rs +++ b/crates/bevy_render/src/view/mod.rs @@ -30,7 +30,7 @@ use bevy_app::{App, Plugin}; use bevy_color::LinearRgba; use bevy_derive::{Deref, DerefMut}; use bevy_ecs::prelude::*; -use bevy_image::BevyDefault as _; +use bevy_image::{BevyDefault as _, ToExtents}; use bevy_math::{mat3, vec2, vec3, Mat3, Mat4, UVec4, Vec2, Vec3, Vec4, Vec4Swizzles}; use bevy_platform::collections::{hash_map::Entry, HashMap}; use bevy_reflect::{std_traits::ReflectDefault, Reflect}; @@ -41,7 +41,7 @@ use core::{ sync::atomic::{AtomicUsize, Ordering}, }; use wgpu::{ - BufferUsages, Extent3d, RenderPassColorAttachment, RenderPassDepthStencilAttachment, StoreOp, + BufferUsages, RenderPassColorAttachment, RenderPassDepthStencilAttachment, StoreOp, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, }; @@ -1046,12 +1046,6 @@ pub fn prepare_view_targets( continue; }; - let size = Extent3d { - width: target_size.x, - height: target_size.y, - depth_or_array_layers: 1, - }; - let main_texture_format = if view.hdr { ViewTarget::TEXTURE_FORMAT_HDR } else { @@ -1069,7 +1063,7 @@ pub fn prepare_view_targets( .or_insert_with(|| { let descriptor = TextureDescriptor { label: None, - size, + size: target_size.to_extents(), mip_level_count: 1, sample_count: 1, dimension: TextureDimension::D2, @@ -1100,7 +1094,7 @@ pub fn prepare_view_targets( &render_device, TextureDescriptor { label: Some("main_texture_sampled"), - size, + size: target_size.to_extents(), mip_level_count: 1, sample_count: msaa.samples(), dimension: TextureDimension::D2, diff --git a/crates/bevy_render/src/view/window/screenshot.rs b/crates/bevy_render/src/view/window/screenshot.rs index 33b76d269d..9a64709f4e 100644 --- a/crates/bevy_render/src/view/window/screenshot.rs +++ b/crates/bevy_render/src/view/window/screenshot.rs @@ -22,7 +22,7 @@ use bevy_derive::{Deref, DerefMut}; use bevy_ecs::{ entity::EntityHashMap, event::event_update_system, prelude::*, system::SystemState, }; -use bevy_image::{Image, TextureFormatPixelInfo}; +use bevy_image::{Image, TextureFormatPixelInfo, ToExtents}; use bevy_platform::collections::HashSet; use bevy_reflect::Reflect; use bevy_tasks::AsyncComputeTaskPool; @@ -321,11 +321,7 @@ fn prepare_screenshots( continue; }; let format = manual_texture_view.format; - let size = Extent3d { - width: manual_texture_view.size.x, - height: manual_texture_view.size.y, - ..default() - }; + let size = manual_texture_view.size.to_extents(); let (texture_view, state) = prepare_screenshot_state( size, format, diff --git a/crates/bevy_solari/Cargo.toml b/crates/bevy_solari/Cargo.toml index ffaca58ba3..1929ab42d1 100644 --- a/crates/bevy_solari/Cargo.toml +++ b/crates/bevy_solari/Cargo.toml @@ -25,6 +25,7 @@ bevy_platform = { path = "../bevy_platform", version = "0.17.0-dev", default-fea ] } bevy_reflect = { path = "../bevy_reflect", version = "0.17.0-dev" } bevy_render = { path = "../bevy_render", version = "0.17.0-dev" } +bevy_image = { path = "../bevy_image", version = "0.17.0-dev" } bevy_transform = { path = "../bevy_transform", version = "0.17.0-dev" } # other diff --git a/crates/bevy_solari/src/pathtracer/prepare.rs b/crates/bevy_solari/src/pathtracer/prepare.rs index 7ef4733124..ddef965222 100644 --- a/crates/bevy_solari/src/pathtracer/prepare.rs +++ b/crates/bevy_solari/src/pathtracer/prepare.rs @@ -5,11 +5,10 @@ use bevy_ecs::{ query::With, system::{Commands, Query, Res, ResMut}, }; +use bevy_image::ToExtents; use bevy_render::{ camera::ExtractedCamera, - render_resource::{ - Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, - }, + render_resource::{TextureDescriptor, TextureDimension, TextureFormat, TextureUsages}, renderer::RenderDevice, texture::{CachedTexture, TextureCache}, }; @@ -30,11 +29,7 @@ pub fn prepare_pathtracer_accumulation_texture( let descriptor = TextureDescriptor { label: Some("pathtracer_accumulation_texture"), - size: Extent3d { - width: viewport.x, - height: viewport.y, - depth_or_array_layers: 1, - }, + size: viewport.to_extents(), mip_level_count: 1, sample_count: 1, dimension: TextureDimension::D2, diff --git a/crates/bevy_sprite/src/sprite.rs b/crates/bevy_sprite/src/sprite.rs index 61461ab640..581ba69201 100644 --- a/crates/bevy_sprite/src/sprite.rs +++ b/crates/bevy_sprite/src/sprite.rs @@ -278,10 +278,10 @@ impl From for Anchor { mod tests { use bevy_asset::{Assets, RenderAssetUsages}; use bevy_color::Color; - use bevy_image::Image; + use bevy_image::{Image, ToExtents}; use bevy_image::{TextureAtlas, TextureAtlasLayout}; use bevy_math::{Rect, URect, UVec2, Vec2}; - use bevy_render::render_resource::{Extent3d, TextureDimension, TextureFormat}; + use bevy_render::render_resource::{TextureDimension, TextureFormat}; use crate::Anchor; @@ -290,11 +290,7 @@ mod tests { /// Makes a new image of the specified size. fn make_image(size: UVec2) -> Image { Image::new_fill( - Extent3d { - width: size.x, - height: size.y, - depth_or_array_layers: 1, - }, + size.to_extents(), TextureDimension::D2, &[0, 0, 0, 255], TextureFormat::Rgba8Unorm, diff --git a/crates/bevy_sprite/src/tilemap_chunk/mod.rs b/crates/bevy_sprite/src/tilemap_chunk/mod.rs index f6b35264a4..1ac8568643 100644 --- a/crates/bevy_sprite/src/tilemap_chunk/mod.rs +++ b/crates/bevy_sprite/src/tilemap_chunk/mod.rs @@ -11,14 +11,12 @@ use bevy_ecs::{ system::{Query, ResMut}, world::DeferredWorld, }; -use bevy_image::{Image, ImageSampler}; +use bevy_image::{Image, ImageSampler, ToExtents}; use bevy_math::{FloatOrd, UVec2, Vec2, Vec3}; use bevy_platform::collections::HashMap; use bevy_render::{ mesh::{Indices, Mesh, Mesh2d, PrimitiveTopology}, - render_resource::{ - Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, - }, + render_resource::{TextureDescriptor, TextureDimension, TextureFormat, TextureUsages}, }; use tracing::warn; @@ -201,11 +199,7 @@ fn make_chunk_image(size: &UVec2, indices: &[Option]) -> Image { .collect(), ), texture_descriptor: TextureDescriptor { - size: Extent3d { - width: size.x, - height: size.y, - depth_or_array_layers: 1, - }, + size: size.to_extents(), dimension: TextureDimension::D2, format: TextureFormat::R16Uint, label: None, diff --git a/crates/bevy_text/src/font_atlas.rs b/crates/bevy_text/src/font_atlas.rs index a10dee5923..5407d25f84 100644 --- a/crates/bevy_text/src/font_atlas.rs +++ b/crates/bevy_text/src/font_atlas.rs @@ -1,10 +1,10 @@ use bevy_asset::{Assets, Handle}; -use bevy_image::{prelude::*, ImageSampler}; +use bevy_image::{prelude::*, ImageSampler, ToExtents}; use bevy_math::{IVec2, UVec2}; use bevy_platform::collections::HashMap; use bevy_render::{ render_asset::RenderAssetUsages, - render_resource::{Extent3d, TextureDimension, TextureFormat}, + render_resource::{TextureDimension, TextureFormat}, }; use crate::{FontSmoothing, GlyphAtlasLocation, TextError}; @@ -41,11 +41,7 @@ impl FontAtlas { font_smoothing: FontSmoothing, ) -> FontAtlas { let mut image = Image::new_fill( - Extent3d { - width: size.x, - height: size.y, - depth_or_array_layers: 1, - }, + size.to_extents(), TextureDimension::D2, &[0, 0, 0, 0], TextureFormat::Rgba8UnormSrgb, diff --git a/crates/bevy_ui/src/widget/viewport.rs b/crates/bevy_ui/src/widget/viewport.rs index 9cdc348da5..fe5b6eeb6d 100644 --- a/crates/bevy_ui/src/widget/viewport.rs +++ b/crates/bevy_ui/src/widget/viewport.rs @@ -7,8 +7,8 @@ use bevy_ecs::{ reflect::ReflectComponent, system::{Commands, Query, Res, ResMut}, }; -use bevy_image::Image; -use bevy_math::Rect; +use bevy_image::{Image, ToExtents}; +use bevy_math::{Rect, UVec2}; #[cfg(feature = "bevy_ui_picking_backend")] use bevy_picking::{ events::PointerState, @@ -17,12 +17,8 @@ use bevy_picking::{ }; use bevy_platform::collections::HashMap; use bevy_reflect::Reflect; -use bevy_render::{ - camera::{Camera, NormalizedRenderTarget}, - render_resource::Extent3d, -}; +use bevy_render::camera::{Camera, NormalizedRenderTarget}; use bevy_transform::components::GlobalTransform; -use bevy_utils::default; #[cfg(feature = "bevy_ui_picking_backend")] use uuid::Uuid; @@ -166,11 +162,7 @@ pub fn update_viewport_render_target_size( let Some(image_handle) = camera.target.as_image() else { continue; }; - let size = Extent3d { - width: u32::max(1, size.x as u32), - height: u32::max(1, size.y as u32), - ..default() - }; + let size = size.as_uvec2().max(UVec2::ONE).to_extents(); images.get_mut(image_handle).unwrap().resize(size); } } diff --git a/examples/stress_tests/many_cubes.rs b/examples/stress_tests/many_cubes.rs index 1d8a503131..f18821ee96 100644 --- a/examples/stress_tests/many_cubes.rs +++ b/examples/stress_tests/many_cubes.rs @@ -268,11 +268,7 @@ fn init_textures(args: &Args, images: &mut Assets) -> Vec> .chunks(4) .map(|pixel| { images.add(Image::new_fill( - Extent3d { - width: 1, - height: 1, - depth_or_array_layers: 1, - }, + Extent3d::default(), TextureDimension::D2, pixel, TextureFormat::Rgba8UnormSrgb,