-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Make wgpu_hal::api::Empty
into a usable Noop
backend for testing.
#7063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -815,7 +815,7 @@ cfg_if::cfg_if! { | |
} | ||
// Fallback | ||
else { | ||
type Api = hal::api::Empty; | ||
type Api = hal::api::Noop; | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use core::cell::UnsafeCell; | ||
use core::ops::Range; | ||
use core::ptr; | ||
use std::sync::Arc; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Buffer { | ||
/// This data is potentially accessed mutably in arbitrary non-overlapping slices, | ||
/// so we must store it in `UnsafeCell` to avoid making any too-strong no-aliasing claims. | ||
storage: Arc<UnsafeCell<[u8]>>, | ||
|
||
/// Size of the allocation. | ||
/// | ||
/// This is redundant with `storage.get().len()`, but that method is not | ||
/// available until our MSRV is 1.79 or greater. | ||
size: usize, | ||
} | ||
|
||
/// SAFETY: | ||
/// This shared mutable data will not be accessed in a way which causes data races; | ||
/// the obligation to do so is on the caller of the HAL API. | ||
/// For safe code, `wgpu-core` validation manages appropriate access. | ||
unsafe impl Send for Buffer {} | ||
unsafe impl Sync for Buffer {} | ||
|
||
impl Buffer { | ||
pub(super) fn new(desc: &crate::BufferDescriptor) -> Result<Self, crate::DeviceError> { | ||
let &crate::BufferDescriptor { | ||
label: _, | ||
size, | ||
usage: _, | ||
memory_flags: _, | ||
} = desc; | ||
|
||
let size = usize::try_from(size).map_err(|_| crate::DeviceError::OutOfMemory)?; | ||
|
||
let mut vector: Vec<u8> = Vec::new(); | ||
vector | ||
.try_reserve_exact(size) | ||
.map_err(|_| crate::DeviceError::OutOfMemory)?; | ||
vector.resize(size, 0); | ||
let storage: Arc<[u8]> = Arc::from(vector); | ||
debug_assert_eq!(storage.len(), size); | ||
|
||
// SAFETY: `UnsafeCell<[u8]>` and `[u8]` have the same layout. | ||
// This is just adding a wrapper type without changing any layout, | ||
// because there is not currently a safe language/`std` way to accomplish this. | ||
let storage: Arc<UnsafeCell<[u8]>> = | ||
unsafe { Arc::from_raw(Arc::into_raw(storage) as *mut UnsafeCell<[u8]>) }; | ||
|
||
Ok(Buffer { storage, size }) | ||
} | ||
|
||
/// Returns a pointer to the memory owned by this buffer within the given `range`. | ||
/// | ||
/// This may be used to create any number of simultaneous pointers; | ||
/// aliasing is only a concern when actually reading, writing, or converting the pointer | ||
/// to a reference. | ||
pub(super) fn get_slice_ptr(&self, range: crate::MemoryRange) -> *mut [u8] { | ||
let base_ptr = self.storage.get(); | ||
let range = range_to_usize(range, self.size); | ||
|
||
// We must obtain a slice pointer without ever creating a slice reference | ||
// that could alias with another slice. | ||
ptr::slice_from_raw_parts_mut( | ||
// SAFETY: `range_to_usize` bounds checks this addition. | ||
unsafe { base_ptr.cast::<u8>().add(range.start) }, | ||
range.len(), | ||
) | ||
} | ||
} | ||
|
||
/// Convert a [`crate::MemoryRange`] to `Range<usize>` and bounds check it. | ||
fn range_to_usize(range: crate::MemoryRange, upper_bound: usize) -> Range<usize> { | ||
// Note: these assertions should be impossible to trigger from safe code. | ||
// We're doing them anyway since this entire backend is for testing | ||
// (except for when it is an unused placeholder) | ||
let start = usize::try_from(range.start).expect("range too large"); | ||
let end = usize::try_from(range.end).expect("range too large"); | ||
assert!(start <= end && end <= upper_bound, "range out of bounds"); | ||
start..end | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.