@@ -356,6 +356,8 @@ pub struct Image {
356356 pub sampler : ImageSampler ,
357357 pub texture_view_descriptor : Option < TextureViewDescriptor < Option < & ' static str > > > ,
358358 pub asset_usage : RenderAssetUsages ,
359+ /// Whether this image should be copied on the GPU when resized.
360+ pub copy_on_resize : bool ,
359361}
360362
361363/// Used in [`Image`], this determines what image sampler to use when rendering. The default setting,
@@ -747,12 +749,15 @@ impl Image {
747749 label : None ,
748750 mip_level_count : 1 ,
749751 sample_count : 1 ,
750- usage : TextureUsages :: TEXTURE_BINDING | TextureUsages :: COPY_DST ,
752+ usage : TextureUsages :: TEXTURE_BINDING
753+ | TextureUsages :: COPY_DST
754+ | TextureUsages :: COPY_SRC ,
751755 view_formats : & [ ] ,
752756 } ,
753757 sampler : ImageSampler :: Default ,
754758 texture_view_descriptor : None ,
755759 asset_usage,
760+ copy_on_resize : false ,
756761 }
757762 }
758763
@@ -887,13 +892,15 @@ impl Image {
887892 /// When growing, the new space is filled with 0. When shrinking, the image is clipped.
888893 ///
889894 /// For faster resizing when keeping pixel data intact is not important, use [`Image::resize`].
890- pub fn resize_in_place ( & mut self , new_size : Extent3d ) -> Result < ( ) , ResizeError > {
895+ pub fn resize_in_place ( & mut self , new_size : Extent3d ) {
891896 let old_size = self . texture_descriptor . size ;
892897 let pixel_size = self . texture_descriptor . format . pixel_size ( ) ;
893898 let byte_len = self . texture_descriptor . format . pixel_size ( ) * new_size. volume ( ) ;
899+ self . texture_descriptor . size = new_size;
894900
895901 let Some ( ref mut data) = self . data else {
896- return Err ( ResizeError :: ImageWithoutData ) ;
902+ self . copy_on_resize = true ;
903+ return ;
897904 } ;
898905
899906 let mut new: Vec < u8 > = vec ! [ 0 ; byte_len] ;
@@ -923,10 +930,6 @@ impl Image {
923930 }
924931
925932 self . data = Some ( new) ;
926-
927- self . texture_descriptor . size = new_size;
928-
929- Ok ( ( ) )
930933 }
931934
932935 /// Takes a 2D image containing vertically stacked images of the same size, and reinterprets
@@ -1591,14 +1594,6 @@ pub enum TextureError {
15911594 IncompleteCubemap ,
15921595}
15931596
1594- /// An error that occurs when an image cannot be resized.
1595- #[ derive( Error , Debug ) ]
1596- pub enum ResizeError {
1597- /// Failed to resize an Image because it has no data.
1598- #[ error( "resize method requires cpu-side image data but none was present" ) ]
1599- ImageWithoutData ,
1600- }
1601-
16021597/// The type of a raw image buffer.
16031598#[ derive( Debug ) ]
16041599pub enum ImageType < ' a > {
@@ -1822,13 +1817,11 @@ mod test {
18221817 }
18231818
18241819 // Grow image
1825- image
1826- . resize_in_place ( Extent3d {
1827- width : 4 ,
1828- height : 4 ,
1829- depth_or_array_layers : 1 ,
1830- } )
1831- . unwrap ( ) ;
1820+ image. resize_in_place ( Extent3d {
1821+ width : 4 ,
1822+ height : 4 ,
1823+ depth_or_array_layers : 1 ,
1824+ } ) ;
18321825
18331826 // After growing, the test pattern should be the same.
18341827 assert ! ( matches!(
@@ -1849,13 +1842,11 @@ mod test {
18491842 ) ) ;
18501843
18511844 // Shrink
1852- image
1853- . resize_in_place ( Extent3d {
1854- width : 1 ,
1855- height : 1 ,
1856- depth_or_array_layers : 1 ,
1857- } )
1858- . unwrap ( ) ;
1845+ image. resize_in_place ( Extent3d {
1846+ width : 1 ,
1847+ height : 1 ,
1848+ depth_or_array_layers : 1 ,
1849+ } ) ;
18591850
18601851 // Images outside of the new dimensions should be clipped
18611852 assert ! ( image. get_color_at( 1 , 1 ) . is_err( ) ) ;
@@ -1898,13 +1889,11 @@ mod test {
18981889 }
18991890
19001891 // Grow image
1901- image
1902- . resize_in_place ( Extent3d {
1903- width : 4 ,
1904- height : 4 ,
1905- depth_or_array_layers : LAYERS + 1 ,
1906- } )
1907- . unwrap ( ) ;
1892+ image. resize_in_place ( Extent3d {
1893+ width : 4 ,
1894+ height : 4 ,
1895+ depth_or_array_layers : LAYERS + 1 ,
1896+ } ) ;
19081897
19091898 // After growing, the test pattern should be the same.
19101899 assert ! ( matches!(
@@ -1929,13 +1918,11 @@ mod test {
19291918 }
19301919
19311920 // Shrink
1932- image
1933- . resize_in_place ( Extent3d {
1934- width : 1 ,
1935- height : 1 ,
1936- depth_or_array_layers : 1 ,
1937- } )
1938- . unwrap ( ) ;
1921+ image. resize_in_place ( Extent3d {
1922+ width : 1 ,
1923+ height : 1 ,
1924+ depth_or_array_layers : 1 ,
1925+ } ) ;
19391926
19401927 // Images outside of the new dimensions should be clipped
19411928 assert ! ( image. get_color_at_3d( 1 , 1 , 0 ) . is_err( ) ) ;
@@ -1944,13 +1931,11 @@ mod test {
19441931 assert ! ( image. get_color_at_3d( 0 , 0 , 1 ) . is_err( ) ) ;
19451932
19461933 // Grow layers
1947- image
1948- . resize_in_place ( Extent3d {
1949- width : 1 ,
1950- height : 1 ,
1951- depth_or_array_layers : 2 ,
1952- } )
1953- . unwrap ( ) ;
1934+ image. resize_in_place ( Extent3d {
1935+ width : 1 ,
1936+ height : 1 ,
1937+ depth_or_array_layers : 2 ,
1938+ } ) ;
19541939
19551940 // Pixels in the newly added layer should be zeroes.
19561941 assert ! ( matches!(
0 commit comments