Skip to content

Commit

Permalink
Add a test for the noop backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Feb 13, 2025
1 parent 7df1307 commit 4f1d489
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
7 changes: 6 additions & 1 deletion tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ name = "wgpu-compile-test"
path = "compile_tests/root.rs"
harness = true

[[test]]
name = "wgpu-validation-test"
path = "validation_tests/root.rs"
harness = true

[features]
webgl = ["wgpu/webgl"]

[dependencies]
wgpu.workspace = true
wgpu = { workspace = true, features = ["noop"] }
wgpu-macros.workspace = true

anyhow.workspace = true
Expand Down
61 changes: 61 additions & 0 deletions tests/validation_tests/noop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! Tests of [`wgpu::Backend::Noop`].
use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
use std::sync::Arc;

#[test]
fn device_is_not_available_by_default() {
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
backends: wgpu::Backends::NOOP,
..Default::default()
});

assert_eq!(
pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())),
None,
"noop backend adapter present when it should not be"
);
}

#[test]
fn device_and_buffers() {
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");
let (device, queue) =
pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default(), None))
.expect("device");

assert_eq!(adapter.get_info().backend, wgpu::Backend::Noop);

// Demonstrate that creating and *writing* to a buffer succeeds.
// This also involves creation of a staging buffer.
let buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("hello world"),
size: 8,
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
assert_eq!(buffer.size(), 8);
queue.write_buffer(&buffer, 0, &[1, 2, 3, 4]);
queue.write_buffer(&buffer, 4, &[5, 6, 7, 8]);

// Demonstrate that we can read back data from the buffer.
// This also involves copy_buffer_to_buffer().
let done: Arc<AtomicBool> = Arc::default();
let done2 = done.clone();
wgpu::util::DownloadBuffer::read_buffer(&device, &queue, &buffer.slice(..), move |result| {
assert_eq!(*result.unwrap(), [1, 2, 3, 4, 5, 6, 7, 8],);
done.store(true, Relaxed);
});
device.poll(wgpu::Maintain::Wait);
assert!(done2.load(Relaxed));
}
3 changes: 3 additions & 0 deletions tests/validation_tests/root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Tests of the [`wgpu`] library API that are not run against a particular GPU.
mod noop;

0 comments on commit 4f1d489

Please sign in to comment.