From f92d22dce150e9578456705c89093ebc5931236f Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Tue, 18 Jun 2024 15:53:56 -0400 Subject: [PATCH 1/3] refactor: `vertex_index_common`: use `strum` `enum` iter. Replace manual enumerations of various `enum`s with `derive`d ones via `strum::{EnumIter, IntoEnumIterator}`. --- Cargo.lock | 1 + Cargo.toml | 1 + tests/Cargo.toml | 1 + tests/tests/vertex_indices/mod.rs | 45 +++++++++---------------------- 4 files changed, 16 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 649cb43dc6..77dc403a8d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4388,6 +4388,7 @@ dependencies = [ "profiling", "serde", "serde_json", + "strum", "wasm-bindgen", "wasm-bindgen-futures", "wasm-bindgen-test", diff --git a/Cargo.toml b/Cargo.toml index 9ec41622c1..ae64ebd7ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -121,6 +121,7 @@ serde = "1" serde_json = "1.0.116" smallvec = "1" static_assertions = "1.1.0" +strum = { version = "0.25.0", features = ["derive"] } tracy-client = "0.17" thiserror = "1" wgpu = { version = "0.20.0", path = "./wgpu" } diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 0e509c712a..f371393461 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -35,6 +35,7 @@ pollster.workspace = true profiling.workspace = true serde_json.workspace = true serde.workspace = true +strum = { workspace = true, features = ["derive"] } wgpu-macros.workspace = true wgpu.workspace = true wgt = { workspace = true, features = ["serde"] } diff --git a/tests/tests/vertex_indices/mod.rs b/tests/tests/vertex_indices/mod.rs index 840cad8be9..41fd0c6858 100644 --- a/tests/tests/vertex_indices/mod.rs +++ b/tests/tests/vertex_indices/mod.rs @@ -5,6 +5,7 @@ use std::{num::NonZeroU64, ops::Range}; +use strum::IntoEnumIterator; use wgpu::util::{BufferInitDescriptor, DeviceExt, RenderEncoder}; use wgpu_test::{gpu_test, GpuTestConfiguration, TestParameters, TestingContext}; @@ -79,7 +80,7 @@ impl Draw { } } -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, strum::EnumIter)] enum TestCase { /// A single draw call with 6 vertices Draw, @@ -94,14 +95,6 @@ enum TestCase { } impl TestCase { - const ARRAY: [Self; 5] = [ - Self::Draw, - Self::DrawNonZeroFirstVertex, - Self::DrawBaseVertex, - Self::DrawInstanced, - Self::DrawNonZeroFirstInstance, - ]; - // Get the draw calls for this test case fn draws(&self) -> &'static [Draw] { match self { @@ -148,7 +141,7 @@ impl TestCase { } } -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, strum::EnumIter)] enum IdSource { /// Use buffers to load the vertex and instance index Buffers, @@ -156,30 +149,18 @@ enum IdSource { Builtins, } -impl IdSource { - const ARRAY: [Self; 2] = [Self::Buffers, Self::Builtins]; -} - -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, strum::EnumIter)] enum DrawCallKind { Direct, Indirect, } -impl DrawCallKind { - const ARRAY: [Self; 2] = [Self::Direct, Self::Indirect]; -} - -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, strum::EnumIter)] enum EncoderKind { RenderPass, RenderBundle, } -impl EncoderKind { - const ARRAY: [Self; 2] = [Self::RenderPass, Self::RenderBundle]; -} - struct Test { case: TestCase, id_source: IdSource, @@ -338,15 +319,15 @@ async fn vertex_index_common(ctx: TestingContext) { .create_view(&wgpu::TextureViewDescriptor::default()); let mut tests = Vec::with_capacity( - TestCase::ARRAY.len() - * IdSource::ARRAY.len() - * DrawCallKind::ARRAY.len() - * EncoderKind::ARRAY.len(), + TestCase::iter().count() + * IdSource::iter().count() + * DrawCallKind::iter().count() + * EncoderKind::iter().count(), ); - for case in TestCase::ARRAY { - for id_source in IdSource::ARRAY { - for draw_call_kind in DrawCallKind::ARRAY { - for encoder_kind in EncoderKind::ARRAY { + for case in TestCase::iter() { + for id_source in IdSource::iter() { + for draw_call_kind in DrawCallKind::iter() { + for encoder_kind in EncoderKind::iter() { tests.push(Test { case, id_source, From b584f03a9045a9da195024dc06f0cab51f498121 Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Tue, 18 Jun 2024 15:53:56 -0400 Subject: [PATCH 2/3] refactor: `vertex_index_common`: elide `tests` alloc. w/ `Itertools::cartesian_product` --- Cargo.lock | 1 + Cargo.toml | 1 + tests/Cargo.toml | 1 + tests/tests/vertex_indices/mod.rs | 32 ++++++++++++------------------- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 77dc403a8d..4b69d456d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4378,6 +4378,7 @@ dependencies = [ "env_logger", "futures-lite", "image", + "itertools", "js-sys", "libtest-mimic", "log", diff --git a/Cargo.toml b/Cargo.toml index ae64ebd7ba..d1589a71a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -91,6 +91,7 @@ getrandom = "0.2" glam = "0.27" heck = "0.5.0" image = { version = "0.24", default-features = false, features = ["png"] } +itertools = { version = "0.10.5" } ktx2 = "0.3" libc = "0.2" # libloading 0.8 switches from `winapi` to `windows-sys`; permit either diff --git a/tests/Cargo.toml b/tests/Cargo.toml index f371393461..d6b5ae672c 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -27,6 +27,7 @@ bytemuck.workspace = true cfg-if.workspace = true ctor.workspace = true futures-lite.workspace = true +itertools.workspace = true libtest-mimic.workspace = true log.workspace = true parking_lot.workspace = true diff --git a/tests/tests/vertex_indices/mod.rs b/tests/tests/vertex_indices/mod.rs index 41fd0c6858..31c3701fbd 100644 --- a/tests/tests/vertex_indices/mod.rs +++ b/tests/tests/vertex_indices/mod.rs @@ -5,6 +5,7 @@ use std::{num::NonZeroU64, ops::Range}; +use itertools::Itertools; use strum::IntoEnumIterator; use wgpu::util::{BufferInitDescriptor, DeviceExt, RenderEncoder}; @@ -318,26 +319,17 @@ async fn vertex_index_common(ctx: TestingContext) { ) .create_view(&wgpu::TextureViewDescriptor::default()); - let mut tests = Vec::with_capacity( - TestCase::iter().count() - * IdSource::iter().count() - * DrawCallKind::iter().count() - * EncoderKind::iter().count(), - ); - for case in TestCase::iter() { - for id_source in IdSource::iter() { - for draw_call_kind in DrawCallKind::iter() { - for encoder_kind in EncoderKind::iter() { - tests.push(Test { - case, - id_source, - draw_call_kind, - encoder_kind, - }) - } - } - } - } + let tests = TestCase::iter() + .cartesian_product(IdSource::iter()) + .cartesian_product(DrawCallKind::iter()) + .cartesian_product(EncoderKind::iter()) + .map(|(((case, id_source), draw_call_kind), encoder_kind)| Test { + case, + id_source, + draw_call_kind, + encoder_kind, + }) + .collect::>(); let features = ctx.adapter.features(); From c8882d03695e78b6d4574370a60203c18c697db4 Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Tue, 18 Jun 2024 15:53:56 -0400 Subject: [PATCH 3/3] refactor: `vertex_index_common`: use `size_of_val` instead of magic number --- tests/tests/vertex_indices/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/tests/vertex_indices/mod.rs b/tests/tests/vertex_indices/mod.rs index 31c3701fbd..dcc2ca82f5 100644 --- a/tests/tests/vertex_indices/mod.rs +++ b/tests/tests/vertex_indices/mod.rs @@ -8,7 +8,6 @@ use std::{num::NonZeroU64, ops::Range}; use itertools::Itertools; use strum::IntoEnumIterator; use wgpu::util::{BufferInitDescriptor, DeviceExt, RenderEncoder}; - use wgpu_test::{gpu_test, GpuTestConfiguration, TestParameters, TestingContext}; use wgt::RenderBundleDescriptor; @@ -342,7 +341,7 @@ async fn vertex_index_common(ctx: TestingContext) { let expected = test.expectation(&ctx); - let buffer_size = 4 * expected.len() as u64; + let buffer_size = (std::mem::size_of_val(&expected[0]) * expected.len()) as u64; let cpu_buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor { label: None, size: buffer_size,