Skip to content

Commit

Permalink
Add missing BufferUsages mismatch check along with a test to ensure i…
Browse files Browse the repository at this point in the history
…t works.
  • Loading branch information
Imberflur committed Sep 13, 2022
1 parent 94ce763 commit 8dd9957
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
14 changes: 14 additions & 0 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,20 @@ impl<A: HalApi> Device<A> {
return Err(resource::CreateBufferError::EmptyUsage);
}

if !self
.features
.contains(wgt::Features::MAPPABLE_PRIMARY_BUFFERS)
{
use wgt::BufferUsages as Bu;
let write_mismatch = desc.usage.contains(Bu::MAP_WRITE)
&& !(Bu::MAP_WRITE | Bu::COPY_SRC).contains(desc.usage);
let read_mismatch = desc.usage.contains(Bu::MAP_READ)
&& !(Bu::MAP_READ | Bu::COPY_DST).contains(desc.usage);
if write_mismatch || read_mismatch {
return Err(resource::CreateBufferError::UsageMismatch(desc.usage));
}
}

if desc.mapped_at_creation {
if desc.size % wgt::COPY_BUFFER_ALIGNMENT != 0 {
return Err(resource::CreateBufferError::UnalignedSize);
Expand Down
71 changes: 71 additions & 0 deletions wgpu/tests/buffer_usages.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! Tests for buffer usages validation.

use wgt::BufferAddress;

use crate::common::{initialize_test, TestParameters};

#[test]
fn buffer_usage() {
fn try_create(
usage: wgpu::BufferUsages,
enable_mappable_primary_buffers: bool,
should_panic: bool,
) {
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| {
let _buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor {
label: None,
size: BUFFER_SIZE,
usage,
mapped_at_creation: false,
});
//let data = vec![255; size as usize];
//ctx.queue.write_buffer(&buffer, offset, &data);
});
}

macro_rules! usage_consts {
($($name:ident),*) => {
$(const $name: wgpu::BufferUsages = wgpu::BufferUsages::$name;)*
};
}
usage_consts!(MAP_READ, MAP_WRITE, COPY_SRC, COPY_DST, STORAGE);

// Buffers cannot have empty usage flags
try_create(wgpu::BufferUsages::empty(), false, true);
try_create(MAP_READ, false, false);
try_create(MAP_WRITE, false, false);
try_create(MAP_READ | COPY_DST, false, false);
try_create(MAP_WRITE | COPY_SRC, false, false);
// MAP_READ can only be paired with COPY_DST and MAP_WRITE can only be paired with COPY_SRC
// without enabling Features::MAPPABlE_PRIMARY_BUFFER.
try_create(MAP_READ | COPY_DST | COPY_SRC, false, true);
try_create(MAP_WRITE | COPY_SRC | COPY_DST, false, true);
try_create(MAP_READ | MAP_WRITE, false, true);
try_create(MAP_WRITE | MAP_READ, false, true);
try_create(MAP_READ | COPY_DST | STORAGE, false, true);
try_create(MAP_WRITE | COPY_SRC | STORAGE, false, true);
try_create(wgpu::BufferUsages::all(), false, true);

// Enable MAPPABLE_PRIMARY_BUFFERS
try_create(wgpu::BufferUsages::empty(), true, true);
try_create(MAP_READ, true, false);
try_create(MAP_WRITE, true, false);
try_create(MAP_READ | COPY_DST, true, false);
try_create(MAP_WRITE | COPY_SRC, true, false);
try_create(MAP_READ | COPY_DST | COPY_SRC, true, false);
try_create(MAP_WRITE | COPY_SRC | COPY_DST, true, false);
try_create(MAP_READ | MAP_WRITE, true, false);
try_create(MAP_WRITE | MAP_READ, true, false);
try_create(MAP_READ | COPY_DST | STORAGE, true, false);
try_create(MAP_WRITE | COPY_SRC | STORAGE, true, false);
}

const BUFFER_SIZE: BufferAddress = 1234;
1 change: 1 addition & 0 deletions wgpu/tests/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod common;

mod buffer_copy;
mod buffer_usages;
mod clear_texture;
mod device;
mod example_wgsl;
Expand Down

0 comments on commit 8dd9957

Please sign in to comment.