From ab0e4b1b6f334649e3cd5132fd4282d8decc0040 Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Sun, 23 Feb 2025 12:57:19 -0500 Subject: [PATCH] Refactor Validation Tests a Bit (#7199) --- tests/tests/binding_array/mod.rs | 1 - tests/tests/binding_array/validation.rs | 87 ------------------ tests/validation_tests/api/binding_arrays.rs | 94 ++++++++++++++++++++ tests/validation_tests/api/buffer.rs | 74 --------------- tests/validation_tests/api/buffer_slice.rs | 69 ++++++++++++++ tests/validation_tests/api/mod.rs | 2 + tests/validation_tests/root.rs | 7 +- 7 files changed, 170 insertions(+), 164 deletions(-) delete mode 100644 tests/tests/binding_array/validation.rs create mode 100644 tests/validation_tests/api/binding_arrays.rs create mode 100644 tests/validation_tests/api/buffer_slice.rs diff --git a/tests/tests/binding_array/mod.rs b/tests/tests/binding_array/mod.rs index 72b95b1db..4b8972fcd 100644 --- a/tests/tests/binding_array/mod.rs +++ b/tests/tests/binding_array/mod.rs @@ -2,4 +2,3 @@ mod buffers; mod sampled_textures; mod samplers; mod storage_textures; -mod validation; diff --git a/tests/tests/binding_array/validation.rs b/tests/tests/binding_array/validation.rs deleted file mode 100644 index bc3b3de18..000000000 --- a/tests/tests/binding_array/validation.rs +++ /dev/null @@ -1,87 +0,0 @@ -use std::num::NonZeroU32; - -use wgpu::*; -use wgpu_test::{fail, gpu_test, GpuTestConfiguration, TestParameters, TestingContext}; - -#[gpu_test] -static VALIDATION: GpuTestConfiguration = GpuTestConfiguration::new() - .parameters( - TestParameters::default() - .features(Features::TEXTURE_BINDING_ARRAY) - .limits(Limits { - max_dynamic_storage_buffers_per_pipeline_layout: 1, - max_binding_array_elements_per_shader_stage: 4, - ..Limits::downlevel_defaults() - }), - ) - .run_async(validation); - -async fn validation(ctx: TestingContext) { - // Check that you can't create a bind group with both dynamic offset and binding array - fail( - &ctx.device, - || { - ctx.device - .create_bind_group_layout(&BindGroupLayoutDescriptor { - label: Some("Test1"), - entries: &[ - BindGroupLayoutEntry { - binding: 0, - visibility: ShaderStages::FRAGMENT, - ty: BindingType::Texture { - sample_type: TextureSampleType::Float { filterable: false }, - view_dimension: TextureViewDimension::D2, - multisampled: false, - }, - count: Some(NonZeroU32::new(4).unwrap()), - }, - BindGroupLayoutEntry { - binding: 1, - visibility: ShaderStages::FRAGMENT, - ty: BindingType::Buffer { - ty: BufferBindingType::Storage { read_only: true }, - has_dynamic_offset: true, - min_binding_size: None, - }, - count: None, - }, - ], - }) - }, - Some("binding array and a dynamically offset buffer"), - ); - - // Check that you can't create a bind group with both uniform buffer and binding array - fail( - &ctx.device, - || { - ctx.device - .create_bind_group_layout(&BindGroupLayoutDescriptor { - label: Some("Test2"), - entries: &[ - BindGroupLayoutEntry { - binding: 0, - visibility: ShaderStages::FRAGMENT, - ty: BindingType::Texture { - sample_type: TextureSampleType::Float { filterable: false }, - view_dimension: TextureViewDimension::D2, - multisampled: false, - }, - count: Some(NonZeroU32::new(4).unwrap()), - }, - BindGroupLayoutEntry { - binding: 1, - visibility: ShaderStages::FRAGMENT, - ty: BindingType::Buffer { - ty: BufferBindingType::Uniform, - has_dynamic_offset: false, - min_binding_size: None, - }, - count: None, - }, - ], - }) - }, - Some("binding array and a uniform buffer"), - ); -} diff --git a/tests/validation_tests/api/binding_arrays.rs b/tests/validation_tests/api/binding_arrays.rs new file mode 100644 index 000000000..381bde336 --- /dev/null +++ b/tests/validation_tests/api/binding_arrays.rs @@ -0,0 +1,94 @@ +use std::num::NonZeroU32; + +use wgpu::*; +use wgpu_test::fail; + +#[test] +fn dynamic_offset() { + let (device, _queue) = crate::request_noop_device_with_desc(&DeviceDescriptor { + required_features: Features::TEXTURE_BINDING_ARRAY, + required_limits: Limits { + max_binding_array_elements_per_shader_stage: 4, + ..Limits::default() + }, + ..DeviceDescriptor::default() + }); + + // Check that you can't create a bind group with both dynamic offset and binding array + fail( + &device, + || { + device.create_bind_group_layout(&BindGroupLayoutDescriptor { + label: Some("Test1"), + entries: &[ + BindGroupLayoutEntry { + binding: 0, + visibility: ShaderStages::FRAGMENT, + ty: BindingType::Texture { + sample_type: TextureSampleType::Float { filterable: false }, + view_dimension: TextureViewDimension::D2, + multisampled: false, + }, + count: Some(NonZeroU32::new(4).unwrap()), + }, + BindGroupLayoutEntry { + binding: 1, + visibility: ShaderStages::FRAGMENT, + ty: BindingType::Buffer { + ty: BufferBindingType::Storage { read_only: true }, + has_dynamic_offset: true, + min_binding_size: None, + }, + count: None, + }, + ], + }) + }, + Some("binding array and a dynamically offset buffer"), + ); +} + +#[test] +fn uniform_buffer() { + let (device, _queue) = crate::request_noop_device_with_desc(&DeviceDescriptor { + required_features: Features::TEXTURE_BINDING_ARRAY, + required_limits: Limits { + max_binding_array_elements_per_shader_stage: 4, + ..Limits::default() + }, + ..DeviceDescriptor::default() + }); + + // Check that you can't create a bind group with both uniform buffer and binding array + fail( + &device, + || { + device.create_bind_group_layout(&BindGroupLayoutDescriptor { + label: Some("Test2"), + entries: &[ + BindGroupLayoutEntry { + binding: 0, + visibility: ShaderStages::FRAGMENT, + ty: BindingType::Texture { + sample_type: TextureSampleType::Float { filterable: false }, + view_dimension: TextureViewDimension::D2, + multisampled: false, + }, + count: Some(NonZeroU32::new(4).unwrap()), + }, + BindGroupLayoutEntry { + binding: 1, + visibility: ShaderStages::FRAGMENT, + ty: BindingType::Buffer { + ty: BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }, + ], + }) + }, + Some("binding array and a uniform buffer"), + ); +} diff --git a/tests/validation_tests/api/buffer.rs b/tests/validation_tests/api/buffer.rs index 813e618b6..41f655e9f 100644 --- a/tests/validation_tests/api/buffer.rs +++ b/tests/validation_tests/api/buffer.rs @@ -1,7 +1,5 @@ //! Tests of [`wgpu::Buffer`] and related. -use core::num::NonZero; - /// Ensures that submitting a command buffer referencing an already destroyed buffer /// results in an error. #[test] @@ -23,75 +21,3 @@ fn destroyed_buffer() { queue.submit([encoder.finish()]); } - -mod buffer_slice { - use super::*; - - const ARBITRARY_DESC: &wgpu::BufferDescriptor = &wgpu::BufferDescriptor { - label: None, - size: 100, - usage: wgpu::BufferUsages::VERTEX, - mapped_at_creation: false, - }; - - #[test] - fn reslice_success() { - let (device, _queue) = crate::request_noop_device(); - let buffer = device.create_buffer(ARBITRARY_DESC); - - assert_eq!(buffer.slice(10..90).slice(10..70), buffer.slice(20..80)); - } - - #[test] - #[should_panic = "slice offset 10 size 80 is out of range for buffer of size 80"] - fn reslice_out_of_bounds() { - let (device, _queue) = crate::request_noop_device(); - let buffer = device.create_buffer(ARBITRARY_DESC); - - buffer.slice(10..90).slice(10..90); - } - - #[test] - fn getters() { - let (device, _queue) = crate::request_noop_device(); - let buffer = device.create_buffer(ARBITRARY_DESC); - - let slice_with_size = buffer.slice(10..90); - assert_eq!( - ( - slice_with_size.buffer(), - slice_with_size.offset(), - slice_with_size.size() - ), - (&buffer, 10, NonZero::new(80).unwrap()) - ); - - let slice_without_size = buffer.slice(10..); - assert_eq!( - ( - slice_without_size.buffer(), - slice_without_size.offset(), - slice_without_size.size() - ), - (&buffer, 10, NonZero::new(90).unwrap()) - ); - } - - #[test] - fn into_buffer_binding() { - let (device, _queue) = crate::request_noop_device(); - let buffer = device.create_buffer(ARBITRARY_DESC); - - // BindingResource doesn’t implement PartialEq, so use matching - let wgpu::BindingResource::Buffer(wgpu::BufferBinding { - buffer: b, - offset: 50, - size: Some(size), - }) = wgpu::BindingResource::from(buffer.slice(50..80)) - else { - panic!("didn't match") - }; - assert_eq!(b, &buffer); - assert_eq!(size, NonZero::new(30).unwrap()); - } -} diff --git a/tests/validation_tests/api/buffer_slice.rs b/tests/validation_tests/api/buffer_slice.rs new file mode 100644 index 000000000..14f244eab --- /dev/null +++ b/tests/validation_tests/api/buffer_slice.rs @@ -0,0 +1,69 @@ +use std::num::NonZero; + +const ARBITRARY_DESC: &wgpu::BufferDescriptor = &wgpu::BufferDescriptor { + label: None, + size: 100, + usage: wgpu::BufferUsages::VERTEX, + mapped_at_creation: false, +}; + +#[test] +fn reslice_success() { + let (device, _queue) = crate::request_noop_device(); + let buffer = device.create_buffer(ARBITRARY_DESC); + + assert_eq!(buffer.slice(10..90).slice(10..70), buffer.slice(20..80)); +} + +#[test] +#[should_panic = "slice offset 10 size 80 is out of range for buffer of size 80"] +fn reslice_out_of_bounds() { + let (device, _queue) = crate::request_noop_device(); + let buffer = device.create_buffer(ARBITRARY_DESC); + + buffer.slice(10..90).slice(10..90); +} + +#[test] +fn getters() { + let (device, _queue) = crate::request_noop_device(); + let buffer = device.create_buffer(ARBITRARY_DESC); + + let slice_with_size = buffer.slice(10..90); + assert_eq!( + ( + slice_with_size.buffer(), + slice_with_size.offset(), + slice_with_size.size() + ), + (&buffer, 10, NonZero::new(80).unwrap()) + ); + + let slice_without_size = buffer.slice(10..); + assert_eq!( + ( + slice_without_size.buffer(), + slice_without_size.offset(), + slice_without_size.size() + ), + (&buffer, 10, NonZero::new(90).unwrap()) + ); +} + +#[test] +fn into_buffer_binding() { + let (device, _queue) = crate::request_noop_device(); + let buffer = device.create_buffer(ARBITRARY_DESC); + + // BindingResource doesn’t implement PartialEq, so use matching + let wgpu::BindingResource::Buffer(wgpu::BufferBinding { + buffer: b, + offset: 50, + size: Some(size), + }) = wgpu::BindingResource::from(buffer.slice(50..80)) + else { + panic!("didn't match") + }; + assert_eq!(b, &buffer); + assert_eq!(size, NonZero::new(30).unwrap()); +} diff --git a/tests/validation_tests/api/mod.rs b/tests/validation_tests/api/mod.rs index 8cc93efa9..86e7a8d10 100644 --- a/tests/validation_tests/api/mod.rs +++ b/tests/validation_tests/api/mod.rs @@ -1,2 +1,4 @@ +mod binding_arrays; mod buffer; +mod buffer_slice; mod texture; diff --git a/tests/validation_tests/root.rs b/tests/validation_tests/root.rs index 1f899a5b1..a0f303abe 100644 --- a/tests/validation_tests/root.rs +++ b/tests/validation_tests/root.rs @@ -6,6 +6,10 @@ mod noop; /// Obtain a device using [`wgpu::Backend::Noop`]. /// This should never fail. fn request_noop_device() -> (wgpu::Device, wgpu::Queue) { + request_noop_device_with_desc(&wgpu::DeviceDescriptor::default()) +} + +fn request_noop_device_with_desc(desc: &wgpu::DeviceDescriptor) -> (wgpu::Device, wgpu::Queue) { let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor { backends: wgpu::Backends::NOOP, backend_options: wgpu::BackendOptions { @@ -20,6 +24,5 @@ fn request_noop_device() -> (wgpu::Device, wgpu::Queue) { .expect("adapter"); assert_eq!(adapter.get_info().backend, wgpu::Backend::Noop); - pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default(), None)) - .expect("device") + pollster::block_on(adapter.request_device(desc, None)).expect("device") }