|
| 1 | +//! Tests of [`wgpu::Backend::Noop`]. |
| 2 | +
|
| 3 | +use std::sync::atomic::{AtomicBool, Ordering::Relaxed}; |
| 4 | +use std::sync::Arc; |
| 5 | + |
| 6 | +#[test] |
| 7 | +fn device_and_buffers() { |
| 8 | + let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor { |
| 9 | + backends: wgpu::Backends::NOOP, |
| 10 | + backend_options: wgpu::BackendOptions { |
| 11 | + noop: wgpu::NoopBackendOptions { enable: true }, |
| 12 | + ..Default::default() |
| 13 | + }, |
| 14 | + ..Default::default() |
| 15 | + }); |
| 16 | + let adapter = |
| 17 | + pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) |
| 18 | + .expect("adapter"); |
| 19 | + let (device, queue) = |
| 20 | + pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default(), None)) |
| 21 | + .expect("device"); |
| 22 | + |
| 23 | + assert_eq!(adapter.get_info().backend, wgpu::Backend::Noop); |
| 24 | + |
| 25 | + // Demonstrate that creating and *writing* to a buffer succeeds. |
| 26 | + // This also involves creation of a staging buffer. |
| 27 | + let buffer = device.create_buffer(&wgpu::BufferDescriptor { |
| 28 | + label: Some("hello world"), |
| 29 | + size: 8, |
| 30 | + usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC, |
| 31 | + mapped_at_creation: false, |
| 32 | + }); |
| 33 | + assert_eq!(buffer.size(), 8); |
| 34 | + queue.write_buffer(&buffer, 0, &[1, 2, 3, 4]); |
| 35 | + queue.write_buffer(&buffer, 4, &[5, 6, 7, 8]); |
| 36 | + |
| 37 | + // Demonstrate that we can read back data from the buffer. |
| 38 | + // This also involves copy_buffer_to_buffer(). |
| 39 | + let done: Arc<AtomicBool> = Arc::default(); |
| 40 | + let done2 = done.clone(); |
| 41 | + wgpu::util::DownloadBuffer::read_buffer(&device, &queue, &buffer.slice(..), move |result| { |
| 42 | + assert_eq!(*result.unwrap(), [1, 2, 3, 4, 5, 6, 7, 8],); |
| 43 | + done.store(true, Relaxed); |
| 44 | + }); |
| 45 | + device.poll(wgpu::Maintain::Wait); |
| 46 | + assert!(done2.load(Relaxed)); |
| 47 | +} |
0 commit comments