Skip to content

Commit 00e2612

Browse files
committed
Add more tests, fix clippy
1 parent c95d38e commit 00e2612

4 files changed

Lines changed: 171 additions & 50 deletions

File tree

examples/lut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn setup(
7676
.to_linear(),
7777
_ => panic!(),
7878
};
79-
data[offset * 4 + 0] = (c.red * 255.0) as u8;
79+
data[offset * 4] = (c.red * 255.0) as u8;
8080
data[offset * 4 + 1] = (c.green * 255.0) as u8;
8181
data[offset * 4 + 2] = (c.blue * 255.0) as u8;
8282
data[offset * 4 + 3] = 255;

src/graph/expr.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ impl Module {
789789
///
790790
/// [`Graph`]: crate::graph::Graph
791791
#[derive(Debug, Clone, PartialEq, Eq, Error)]
792+
#[non_exhaustive]
792793
pub enum ExprError {
793794
/// Expression type error.
794795
///
@@ -5304,4 +5305,45 @@ mod tests {
53045305
assert!(ret.is_err());
53055306
}
53065307
}
5308+
5309+
#[test]
5310+
fn texture_load_expr() {
5311+
let mut module = Module::default();
5312+
let coordinates = module.lit(Vec2::ZERO);
5313+
let array_index = module.lit(0_u32);
5314+
let mip_level = module.lit(0_u32);
5315+
5316+
let dims = [
5317+
SlotDimension::D1,
5318+
SlotDimension::D2,
5319+
SlotDimension::D2Array,
5320+
SlotDimension::Cube,
5321+
SlotDimension::CubeArray,
5322+
SlotDimension::D3,
5323+
SlotDimension::DepthD2,
5324+
SlotDimension::DepthD2Array,
5325+
SlotDimension::DepthCube,
5326+
SlotDimension::DepthCubeArray,
5327+
];
5328+
let mips = [None, Some(mip_level)];
5329+
for dim in &dims {
5330+
for mip in &mips {
5331+
let mip = *mip;
5332+
5333+
let res = TextureLoadExpr::new(4, *dim, coordinates, None, mip);
5334+
assert_eq!(
5335+
res.is_ok(),
5336+
!dim.is_array() && !dim.is_cube(),
5337+
"array=false dim={dim:?} res={res:?} mip={mip:?}"
5338+
);
5339+
5340+
let res = TextureLoadExpr::new(4, *dim, coordinates, Some(array_index), mip);
5341+
assert_eq!(
5342+
res.is_ok(),
5343+
dim.is_array() && !dim.is_cube(),
5344+
"array=true dim={dim:?} res={res:?} mip={mip:?}"
5345+
);
5346+
}
5347+
}
5348+
}
53075349
}

src/lib.rs

Lines changed: 117 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/render/property.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -805,9 +805,9 @@ pub struct PropertyBindGroupKey {
805805
// Note: use by HashMap to turn a key reference into an owned key when inserting
806806
// an entry. We use the same struct (even if we shouldn't, to avoid
807807
// TextureLayout cloning) for both.
808-
impl Into<PropertyBindGroupKey> for &PropertyBindGroupKey {
809-
fn into(self) -> PropertyBindGroupKey {
810-
self.clone()
808+
impl From<&PropertyBindGroupKey> for PropertyBindGroupKey {
809+
fn from(value: &PropertyBindGroupKey) -> Self {
810+
value.clone()
811811
}
812812
}
813813

@@ -833,6 +833,7 @@ impl PropertyBindGroupKey {
833833
self.binding_size > 0
834834
}
835835

836+
#[allow(dead_code)]
836837
pub fn has_textures(&self) -> bool {
837838
!self.texture_layout.layout.is_empty()
838839
}
@@ -892,9 +893,8 @@ impl PropertyBindGroups {
892893

893894
let align = render_device.limits().min_storage_buffer_offset_alignment;
894895

895-
let mut entries;
896-
if with_prefix_sum {
897-
entries = (*BindGroupEntries::sequential((
896+
let mut entries = if with_prefix_sum {
897+
(*BindGroupEntries::sequential((
898898
spawner_buffer.as_entire_binding(),
899899
prefix_sum_buffer.as_entire_binding(),
900900
BufferBinding {
@@ -903,9 +903,9 @@ impl PropertyBindGroups {
903903
size: Some(GpuBatchInfo::aligned_size(align)),
904904
},
905905
)))
906-
.to_vec();
906+
.to_vec()
907907
} else {
908-
entries = (*BindGroupEntries::with_indices((
908+
(*BindGroupEntries::with_indices((
909909
(0, spawner_buffer.as_entire_binding()),
910910
(
911911
1,
@@ -916,8 +916,8 @@ impl PropertyBindGroups {
916916
},
917917
),
918918
)))
919-
.to_vec();
920-
}
919+
.to_vec()
920+
};
921921
if let Some(property_buffer) = property_buffer {
922922
// @group(2) @binding(3) var<storage, read> properties : array<Properties>
923923
entries.push(BindGroupEntry {
@@ -931,7 +931,7 @@ impl PropertyBindGroups {
931931
textures: textures.iter().map(|h| h.id()).collect(),
932932
};
933933
assert_eq!(material.layout.layout.len(), material.textures.len());
934-
material.append_binding_entries(4, &gpu_images, &mut entries);
934+
material.append_binding_entries(4, gpu_images, &mut entries);
935935

936936
trace!("Creating @2 bind group with {} entries:", entries.len());
937937
for e in &entries {

0 commit comments

Comments
 (0)