Skip to content

Commit

Permalink
Rework buffer_usage test to properly check every failure case using e…
Browse files Browse the repository at this point in the history
…rror scopes (instead of accidentally just testing the first one).
  • Loading branch information
Imberflur authored and cwfitzgerald committed Nov 2, 2022
1 parent 32febc5 commit f018e95
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
76 changes: 46 additions & 30 deletions wgpu/tests/buffer_usages.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,81 @@
//! Tests for buffer usages validation.
use crate::common::{fail, initialize_test, valid, TestParameters};
use wgt::BufferAddress;

use crate::common::{initialize_test, TestParameters};
const BUFFER_SIZE: BufferAddress = 1234;

#[test]
fn buffer_usage() {
fn try_create(
usages: &[wgpu::BufferUsages],
enable_mappable_primary_buffers: bool,
should_panic: bool,
) {
fn try_create(enable_mappable_primary_buffers: bool, usages: &[(bool, &[wgpu::BufferUsages])]) {
let mut parameters = TestParameters::default();
if enable_mappable_primary_buffers {
parameters = parameters.features(wgpu::Features::MAPPABLE_PRIMARY_BUFFERS);
}
if should_panic {
parameters = parameters.failure();
}

initialize_test(parameters, |ctx| {
for usage in usages.iter().copied() {
let _buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor {
label: None,
size: BUFFER_SIZE,
usage,
mapped_at_creation: false,
});
for (expect_validation_error, usage) in
usages
.iter()
.flat_map(|&(expect_validation_error, usages)| {
usages
.iter()
.copied()
.map(move |u| (expect_validation_error, u))
})
{
let create_buffer = || {
let _buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor {
label: None,
size: BUFFER_SIZE,
usage,
mapped_at_creation: false,
});
};
if expect_validation_error {
fail(&ctx.device, create_buffer);
} else {
valid(&ctx.device, create_buffer);
}
}
});
}

use wgpu::BufferUsages as Bu;

let always_valid = [
let always_valid = &[
Bu::MAP_READ,
Bu::MAP_WRITE,
Bu::MAP_READ | Bu::COPY_DST,
Bu::MAP_WRITE | Bu::COPY_SRC,
];
// MAP_READ can only be paired with COPY_DST and MAP_WRITE can only be paired with COPY_SRC
// (unless Features::MAPPABlE_PRIMARY_BUFFERS is enabled).
let needs_mappable_primary_buffers = [
let needs_mappable_primary_buffers = &[
Bu::MAP_READ | Bu::COPY_DST | Bu::COPY_SRC,
Bu::MAP_WRITE | Bu::COPY_SRC | Bu::COPY_DST,
Bu::MAP_READ | Bu::MAP_WRITE,
Bu::MAP_WRITE | Bu::MAP_READ,
Bu::MAP_READ | Bu::COPY_DST | Bu::STORAGE,
Bu::MAP_WRITE | Bu::COPY_SRC | Bu::STORAGE,
wgpu::BufferUsages::all(),
Bu::all(),
];
let always_fail = [Bu::empty()];

try_create(&always_valid, false, false);
try_create(&always_valid, true, false);
let always_fail = &[Bu::empty()];

try_create(&needs_mappable_primary_buffers, false, true);
try_create(&needs_mappable_primary_buffers, true, false);

try_create(&always_fail, false, true);
try_create(&always_fail, true, true);
try_create(
false,
&[
(false, always_valid),
(true, needs_mappable_primary_buffers),
(true, always_fail),
],
);
try_create(
true,
&[
(false, always_valid),
(false, needs_mappable_primary_buffers),
(true, always_fail),
],
);
}

const BUFFER_SIZE: BufferAddress = 1234;
2 changes: 1 addition & 1 deletion wgpu/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub fn initialize_test(parameters: TestParameters, test_function: impl FnOnce(Te
backend_bits,
None,
))
.expect("could not find sutable adapter on the system");
.expect("could not find suitable adapter on the system");

let adapter_info = adapter.get_info();
let adapter_lowercase_name = adapter_info.name.to_lowercase();
Expand Down

0 comments on commit f018e95

Please sign in to comment.