1- use super :: { SamplerDescriptor , TextureDescriptor , TextureFormat } ;
1+ use super :: { Extent3d , SamplerDescriptor , TextureDescriptor , TextureDimension , TextureFormat } ;
22use crate :: renderer:: {
33 RenderResource , RenderResourceContext , RenderResourceId , RenderResourceType ,
44} ;
55use bevy_app:: prelude:: { EventReader , Events } ;
66use bevy_asset:: { AssetEvent , Assets , Handle } ;
77use bevy_ecs:: { Res , ResMut } ;
8- use bevy_math:: Vec2 ;
98use bevy_type_registry:: TypeUuid ;
109use bevy_utils:: HashSet ;
1110
@@ -16,40 +15,58 @@ pub const SAMPLER_ASSET_INDEX: u64 = 1;
1615#[ uuid = "6ea26da6-6cf8-4ea2-9986-1d7bf6c17d6f" ]
1716pub struct Texture {
1817 pub data : Vec < u8 > ,
19- pub size : Vec2 ,
18+ pub size : Extent3d ,
2019 pub format : TextureFormat ,
20+ pub dimension : TextureDimension ,
2121 pub sampler : SamplerDescriptor ,
2222}
2323
2424impl Default for Texture {
2525 fn default ( ) -> Self {
2626 Texture {
2727 data : Default :: default ( ) ,
28- size : Default :: default ( ) ,
28+ size : Extent3d {
29+ width : 1 ,
30+ height : 1 ,
31+ depth : 1 ,
32+ } ,
2933 format : TextureFormat :: Rgba8UnormSrgb ,
34+ dimension : TextureDimension :: D2 ,
3035 sampler : Default :: default ( ) ,
3136 }
3237 }
3338}
3439
3540impl Texture {
36- pub fn new ( size : Vec2 , data : Vec < u8 > , format : TextureFormat ) -> Self {
41+ pub fn new (
42+ size : Extent3d ,
43+ dimension : TextureDimension ,
44+ data : Vec < u8 > ,
45+ format : TextureFormat ,
46+ ) -> Self {
3747 debug_assert_eq ! (
38- size. x as usize * size . y as usize * format. pixel_size( ) ,
48+ size. volume ( ) * format. pixel_size( ) ,
3949 data. len( ) ,
4050 "Pixel data, size and format have to match" ,
4151 ) ;
4252 Self {
4353 data,
4454 size,
55+ dimension,
4556 format,
4657 ..Default :: default ( )
4758 }
4859 }
4960
50- pub fn new_fill ( size : Vec2 , pixel : & [ u8 ] , format : TextureFormat ) -> Self {
61+ pub fn new_fill (
62+ size : Extent3d ,
63+ dimension : TextureDimension ,
64+ pixel : & [ u8 ] ,
65+ format : TextureFormat ,
66+ ) -> Self {
5167 let mut value = Texture {
5268 format,
69+ dimension,
5370 ..Default :: default ( )
5471 } ;
5572 value. resize ( size) ;
@@ -70,16 +87,42 @@ impl Texture {
7087 value
7188 }
7289
73- pub fn aspect ( & self ) -> f32 {
74- self . size . y / self . size . x
90+ pub fn aspect_2d ( & self ) -> f32 {
91+ self . size . height as f32 / self . size . width as f32
7592 }
7693
77- pub fn resize ( & mut self , size : Vec2 ) {
94+ pub fn resize ( & mut self , size : Extent3d ) {
7895 self . size = size;
79- let width = size. x as usize ;
80- let height = size. y as usize ;
8196 self . data
82- . resize ( width * height * self . format . pixel_size ( ) , 0 ) ;
97+ . resize ( size. volume ( ) * self . format . pixel_size ( ) , 0 ) ;
98+ }
99+
100+ /// Changes the `size`, asserting that the total number of data elements (pixels) remains the same.
101+ pub fn reinterpret_size ( & mut self , new_size : Extent3d ) {
102+ assert ! (
103+ new_size. volume( ) == self . size. volume( ) ,
104+ "Incompatible sizes: old = {:?} new = {:?}" ,
105+ self . size,
106+ new_size
107+ ) ;
108+
109+ self . size = new_size;
110+ }
111+
112+ /// Takes a 2D texture containing vertically stacked images of the same size, and reinterprets it as a 2D array texture,
113+ /// where each of the stacked images becomes one layer of the array. This is primarily for use with the `texture2DArray`
114+ /// shader uniform type.
115+ pub fn reinterpret_stacked_2d_as_array ( & mut self , layers : u32 ) {
116+ // Must be a stacked image, and the height must be divisible by layers.
117+ assert ! ( self . dimension == TextureDimension :: D2 ) ;
118+ assert ! ( self . size. depth == 1 ) ;
119+ assert_eq ! ( self . size. height % layers, 0 ) ;
120+
121+ self . reinterpret_size ( Extent3d {
122+ width : self . size . width ,
123+ height : self . size . height / layers,
124+ depth : layers,
125+ } ) ;
83126 }
84127
85128 pub fn texture_resource_system (
0 commit comments