diff --git a/CHANGELOG.md b/CHANGELOG.md index 41783f270..180ae0d1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -172,9 +172,9 @@ By @wumpf in [#7144](https://github.com/gfx-rs/wgpu/pull/7144) - It is now possible to create a dummy `wgpu` device even when no GPU is available. This may be useful for testing of code which manages graphics resources. Currently, it supports reading and writing buffers, and other resource types can be created but do nothing. - To use it, enable the `noop` feature of `wgpu`, and add `NoopBackendOptions { enable: true }` to the backend options; this is an additional safeguard beyond the `Backends` bits. + To use it, enable the `noop` feature of `wgpu`, and either call `Device::noop()`, or add `NoopBackendOptions { enable: true }` to the backend options of your `Instance` (this is an additional safeguard beyond the `Backends` bits). - By @kpreid in [#7063](https://github.com/gfx-rs/wgpu/pull/7063). + By @kpreid in [#7063](https://github.com/gfx-rs/wgpu/pull/7063) and [#7342](https://github.com/gfx-rs/wgpu/pull/7342). - Add `Buffer` methods corresponding to `BufferSlice` methods, so you can skip creating a `BufferSlice` when it offers no benefit, and `BufferSlice::slice()` for sub-slicing a slice. By @kpreid in [#7123](https://github.com/gfx-rs/wgpu/pull/7123). - Add `BufferSlice::buffer()`, `BufferSlice::offset()` and `BufferSlice::size()`. By @kpreid in [#7148](https://github.com/gfx-rs/wgpu/pull/7148). diff --git a/tests/validation-tests/api/binding_arrays.rs b/tests/validation-tests/api/binding_arrays.rs index 381bde336..0611742b9 100644 --- a/tests/validation-tests/api/binding_arrays.rs +++ b/tests/validation-tests/api/binding_arrays.rs @@ -5,7 +5,7 @@ use wgpu_test::fail; #[test] fn dynamic_offset() { - let (device, _queue) = crate::request_noop_device_with_desc(&DeviceDescriptor { + let (device, _queue) = wgpu::Device::noop(&DeviceDescriptor { required_features: Features::TEXTURE_BINDING_ARRAY, required_limits: Limits { max_binding_array_elements_per_shader_stage: 4, @@ -50,7 +50,7 @@ fn dynamic_offset() { #[test] fn uniform_buffer() { - let (device, _queue) = crate::request_noop_device_with_desc(&DeviceDescriptor { + let (device, _queue) = wgpu::Device::noop(&DeviceDescriptor { required_features: Features::TEXTURE_BINDING_ARRAY, required_limits: Limits { max_binding_array_elements_per_shader_stage: 4, diff --git a/tests/validation-tests/api/buffer.rs b/tests/validation-tests/api/buffer.rs index 41f655e9f..3c3dcbac6 100644 --- a/tests/validation-tests/api/buffer.rs +++ b/tests/validation-tests/api/buffer.rs @@ -5,7 +5,7 @@ #[test] #[should_panic = "Buffer with '' label has been destroyed"] fn destroyed_buffer() { - let (device, queue) = crate::request_noop_device(); + let (device, queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); let buffer = device.create_buffer(&wgpu::BufferDescriptor { label: None, size: 1024, diff --git a/tests/validation-tests/api/buffer_slice.rs b/tests/validation-tests/api/buffer_slice.rs index 14f244eab..ac9fe9f8d 100644 --- a/tests/validation-tests/api/buffer_slice.rs +++ b/tests/validation-tests/api/buffer_slice.rs @@ -9,7 +9,7 @@ const ARBITRARY_DESC: &wgpu::BufferDescriptor = &wgpu::BufferDescriptor { #[test] fn reslice_success() { - let (device, _queue) = crate::request_noop_device(); + let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); let buffer = device.create_buffer(ARBITRARY_DESC); assert_eq!(buffer.slice(10..90).slice(10..70), buffer.slice(20..80)); @@ -18,7 +18,7 @@ fn reslice_success() { #[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 (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); let buffer = device.create_buffer(ARBITRARY_DESC); buffer.slice(10..90).slice(10..90); @@ -26,7 +26,7 @@ fn reslice_out_of_bounds() { #[test] fn getters() { - let (device, _queue) = crate::request_noop_device(); + let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); let buffer = device.create_buffer(ARBITRARY_DESC); let slice_with_size = buffer.slice(10..90); @@ -52,7 +52,7 @@ fn getters() { #[test] fn into_buffer_binding() { - let (device, _queue) = crate::request_noop_device(); + let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); let buffer = device.create_buffer(ARBITRARY_DESC); // BindingResource doesn’t implement PartialEq, so use matching diff --git a/tests/validation-tests/api/texture.rs b/tests/validation-tests/api/texture.rs index de961b955..2b079cc3a 100644 --- a/tests/validation-tests/api/texture.rs +++ b/tests/validation-tests/api/texture.rs @@ -5,7 +5,7 @@ #[test] #[should_panic = "Texture with 'dst' label has been destroyed"] fn destroyed_texture() { - let (device, queue) = crate::request_noop_device(); + let (device, queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); let size = wgpu::Extent3d { width: 256, height: 256, diff --git a/tests/validation-tests/noop.rs b/tests/validation-tests/noop.rs index 39bc6e4a3..76bfff481 100644 --- a/tests/validation-tests/noop.rs +++ b/tests/validation-tests/noop.rs @@ -14,9 +14,24 @@ fn device_is_not_available_by_default() { .expect_err("noop backend adapter present when it should not be"); } +#[test] +fn device_is_available_when_requested() { + let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor { + backends: wgpu::Backends::NOOP, + backend_options: wgpu::BackendOptions { + noop: wgpu::NoopBackendOptions { enable: true }, + ..Default::default() + }, + ..Default::default() + }); + + pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) + .expect("noop backend adapter absent when it should be"); +} + #[test] fn device_and_buffers() { - let (device, queue) = crate::request_noop_device(); + let (device, queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); // Demonstrate that creating and *writing* to a buffer succeeds. // This also involves creation of a staging buffer. diff --git a/tests/validation-tests/root.rs b/tests/validation-tests/root.rs index 7227355a6..2292cc082 100644 --- a/tests/validation-tests/root.rs +++ b/tests/validation-tests/root.rs @@ -2,27 +2,3 @@ mod api; 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 { - noop: wgpu::NoopBackendOptions { enable: true }, - ..Default::default() - }, - ..Default::default() - }); - - let adapter = - pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) - .expect("adapter"); - assert_eq!(adapter.get_info().backend, wgpu::Backend::Noop); - - pollster::block_on(adapter.request_device(desc)).expect("device") -} diff --git a/wgpu/src/api/device.rs b/wgpu/src/api/device.rs index b1597c072..992d8e571 100644 --- a/wgpu/src/api/device.rs +++ b/wgpu/src/api/device.rs @@ -42,6 +42,40 @@ impl Device { } } + /// Constructs a stub device for testing using [`Backend::Noop`]. + /// + /// This is a convenience function which avoids the configuration, `async`, and fallibility + /// aspects of constructing a device through `Instance`. + #[cfg(feature = "noop")] + pub fn noop(desc: &DeviceDescriptor<'_>) -> (Device, Queue) { + use core::future::Future as _; + use core::pin::pin; + use core::task; + let ctx = &mut task::Context::from_waker(task::Waker::noop()); + + let instance = Instance::new(&InstanceDescriptor { + backends: Backends::NOOP, + backend_options: BackendOptions { + noop: NoopBackendOptions { enable: true }, + ..Default::default() + }, + ..Default::default() + }); + + // Both of these futures are trivial and should complete instantaneously, + // so we do not need an executor and can just poll them once. + let task::Poll::Ready(Ok(adapter)) = + pin!(instance.request_adapter(&RequestAdapterOptions::default())).poll(ctx) + else { + unreachable!() + }; + let task::Poll::Ready(Ok(device_and_queue)) = pin!(adapter.request_device(desc)).poll(ctx) + else { + unreachable!() + }; + device_and_queue + } + /// Check for resource cleanups and mapping callbacks. Will block if [`PollType::Wait`] is passed. /// /// Return `true` if the queue is empty, or `false` if there are more queue