@@ -825,6 +825,38 @@ impl SlotDimension {
825825 | SlotDimension :: DepthCubeArray
826826 )
827827 }
828+
829+ /// Convert this slot dimension to a WGSL texture type name.
830+ pub fn to_wgsl_texture_type ( & self ) -> String {
831+ match * self {
832+ SlotDimension :: D1 => "texture_1d<f32>" ,
833+ SlotDimension :: D2 => "texture_2d<f32>" ,
834+ SlotDimension :: D2Array => "texture_2d_array<f32>" ,
835+ SlotDimension :: D3 => "texture_3d<f32>" ,
836+ SlotDimension :: Cube => "texture_cube<f32>" ,
837+ SlotDimension :: CubeArray => "texture_cube_array<f32>" ,
838+ SlotDimension :: DepthD2 => "texture_depth_2d<f32>" ,
839+ SlotDimension :: DepthD2Array => "texture_depth_2d_array<f32>" ,
840+ SlotDimension :: DepthCube => "texture_depth_cube<f32>" ,
841+ SlotDimension :: DepthCubeArray => "texture_depth_cube_array<f32>" ,
842+ }
843+ . to_string ( )
844+ }
845+
846+ /// Convert this slot dimension to a WGSL sampler type.
847+ ///
848+ /// This returns `sampler_comparison` if [`is_depth()`] is `true`, or
849+ /// `sampler` otherwise.
850+ ///
851+ /// [`is_depth()`]: Self::is_depth
852+ pub fn to_wgsl_sampler_type ( & self ) -> String {
853+ if self . is_depth ( ) {
854+ "sampler_comparison"
855+ } else {
856+ "sampler"
857+ }
858+ . to_string ( )
859+ }
828860}
829861
830862/// Texture slot of a [`Module`].
@@ -877,12 +909,11 @@ impl TextureSlot {
877909
878910 // Cube textures need a number of layer multiple of 6. And non-array ones need
879911 // exactly 6.
880- if self . dimension . is_cube ( ) {
881- if !self . dimension . is_array ( ) && ( array_layer_count != 6 ) {
882- return false ;
883- } else if !array_layer_count. is_multiple_of ( 6 ) {
884- return false ;
885- }
912+ if self . dimension . is_cube ( )
913+ && ( ( !self . dimension . is_array ( ) && ( array_layer_count != 6 ) )
914+ || !array_layer_count. is_multiple_of ( 6 ) )
915+ {
916+ return false ;
886917 }
887918
888919 // A layer count > 1 requires an array textures or a cube texture
@@ -953,36 +984,6 @@ impl TextureSlot {
953984 } ;
954985 BindingType :: Sampler ( sampler_binding_type)
955986 }
956-
957- /// Convert this slot to a WGSL texture type.
958- pub fn to_wgsl_texture_type ( & self ) -> String {
959- match self . dimension {
960- SlotDimension :: D1 => "texture_1d<f32>" ,
961- SlotDimension :: D2 => "texture_2d<f32>" ,
962- SlotDimension :: D2Array => "texture_2d_array<f32>" ,
963- SlotDimension :: D3 => "texture_3d<f32>" ,
964- SlotDimension :: Cube => "texture_cube<f32>" ,
965- SlotDimension :: CubeArray => "texture_cube_array<f32>" ,
966- SlotDimension :: DepthD2 => "texture_depth_2d<f32>" ,
967- SlotDimension :: DepthD2Array => "texture_depth_2d_array<f32>" ,
968- SlotDimension :: DepthCube => "texture_depth_cube<f32>" ,
969- SlotDimension :: DepthCubeArray => "texture_depth_cube_array<f32>" ,
970- }
971- . to_string ( )
972- }
973-
974- /// Convert this slot to a WGSL sampler type.
975- ///
976- /// This returns `sampler_comparison` if [`SlotDimension::is_depth()`] is
977- /// `true`, or `sampler` otherwise.
978- pub fn to_wgsl_sampler_type ( & self ) -> String {
979- if self . dimension . is_depth ( ) {
980- "sampler_comparison"
981- } else {
982- "sampler"
983- }
984- . to_string ( )
985- }
986987}
987988
988989/// Texture layout.
@@ -1068,8 +1069,8 @@ impl TextureLayout {
10681069 for ( slot_index, slot) in self . layout . iter ( ) . enumerate ( ) {
10691070 let tex_index = bind_index;
10701071 let sampler_index = bind_index + 1 ;
1071- let texture_type = slot. to_wgsl_texture_type ( ) ;
1072- let sampler_type = slot. to_wgsl_sampler_type ( ) ;
1072+ let texture_type = slot. dimension . to_wgsl_texture_type ( ) ;
1073+ let sampler_type = slot. dimension . to_wgsl_sampler_type ( ) ;
10731074 code. push_str ( & format ! (
10741075 "@group({group_index}) @binding({tex_index}) var material_texture_{slot_index}: {texture_type};
10751076@group({group_index}) @binding({sampler_index}) var material_sampler_{slot_index}: {sampler_type};
@@ -3039,4 +3040,82 @@ else { return c1; }
30393040 let accepts = ( TextureDimension :: D2 , LayerMatchFlags :: MULTIPLE_OF_SIX , true ) ;
30403041 check_texslot ( & slot_depth_cube_array, accepts) ;
30413042 }
3043+
3044+ #[ test]
3045+ fn slotdim_is ( ) {
3046+ for dim in [
3047+ SlotDimension :: D1 ,
3048+ SlotDimension :: D2 ,
3049+ SlotDimension :: D2Array ,
3050+ SlotDimension :: Cube ,
3051+ SlotDimension :: CubeArray ,
3052+ SlotDimension :: D3 ,
3053+ SlotDimension :: DepthD2 ,
3054+ SlotDimension :: DepthD2Array ,
3055+ SlotDimension :: DepthCube ,
3056+ SlotDimension :: DepthCubeArray ,
3057+ ] {
3058+ // The canonical WGSL name, which is what the SlotDimentions debug-format to,
3059+ // happens to always contain "array" if the texture is an array texture, "depth"
3060+ // if it's used for comparison, and "cube" if it's a cube texture. We use this
3061+ // as validation.
3062+ let name = format ! ( "{:?}" , dim) . to_ascii_lowercase ( ) ;
3063+
3064+ let is_array = name. contains ( "array" ) ;
3065+ assert_eq ! ( is_array, dim. is_array( ) ) ;
3066+
3067+ let is_depth = name. contains ( "depth" ) ;
3068+ assert_eq ! ( is_depth, dim. is_depth( ) ) ;
3069+
3070+ let is_cube = name. contains ( "cube" ) ;
3071+ assert_eq ! ( is_cube, dim. is_cube( ) ) ;
3072+ }
3073+ }
3074+
3075+ #[ test]
3076+ fn slotdim_texture_type ( ) {
3077+ for dim in [
3078+ SlotDimension :: D1 ,
3079+ SlotDimension :: D2 ,
3080+ SlotDimension :: D2Array ,
3081+ SlotDimension :: Cube ,
3082+ SlotDimension :: CubeArray ,
3083+ SlotDimension :: D3 ,
3084+ SlotDimension :: DepthD2 ,
3085+ SlotDimension :: DepthD2Array ,
3086+ SlotDimension :: DepthCube ,
3087+ SlotDimension :: DepthCubeArray ,
3088+ ] {
3089+ let tex = dim. to_wgsl_texture_type ( ) ;
3090+
3091+ let slot_type = format ! ( "{dim:?}" )
3092+ . to_ascii_lowercase ( )
3093+ . replace ( "array" , "_array" )
3094+ . replace ( "depth" , "depth_" )
3095+ . replace ( "d1" , "1d" )
3096+ . replace ( "d2" , "2d" )
3097+ . replace ( "d3" , "3d" ) ;
3098+
3099+ assert_eq ! ( tex, format!( "texture_{slot_type}<f32>" ) ) ;
3100+ }
3101+ }
3102+
3103+ #[ test]
3104+ fn slotdim_sampler_type ( ) {
3105+ for dim in [
3106+ SlotDimension :: D1 ,
3107+ SlotDimension :: D2 ,
3108+ SlotDimension :: D2Array ,
3109+ SlotDimension :: Cube ,
3110+ SlotDimension :: CubeArray ,
3111+ SlotDimension :: D3 ,
3112+ SlotDimension :: DepthD2 ,
3113+ SlotDimension :: DepthD2Array ,
3114+ SlotDimension :: DepthCube ,
3115+ SlotDimension :: DepthCubeArray ,
3116+ ] {
3117+ let sampler = dim. to_wgsl_sampler_type ( ) ;
3118+ assert_eq ! ( sampler. contains( "comparison" ) , dim. is_depth( ) ) ;
3119+ }
3120+ }
30423121}
0 commit comments