Skip to content

Commit

Permalink
Some tests for texture and buffer copies.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblandy committed Aug 14, 2022
1 parent a9d9127 commit abc7501
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
3 changes: 2 additions & 1 deletion wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
// Platform validation requires that the staging buffer always been
// freed, even if an error occurs. All paths from here must call
// `device.pending_writes.consume`.
let (staging_buffer, staging_buffer_ptr) = prepare_staging_buffer(&mut device.raw, stage_size)?;
let (staging_buffer, staging_buffer_ptr) =
prepare_staging_buffer(&mut device.raw, stage_size)?;

if stage_bytes_per_row == bytes_per_row {
profiling::scope!("copy aligned");
Expand Down
42 changes: 42 additions & 0 deletions wgpu/tests/buffer_copy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Tests for buffer copy validation.

use wgt::BufferAddress;

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

#[test]
fn copy_alignment() {
fn try_copy(offset: BufferAddress, size: BufferAddress, should_panic: bool) {
let mut parameters = TestParameters::default();
if should_panic {
parameters = parameters.failure();
}

initialize_test(parameters, |ctx| {
let buffer = ctx.device.create_buffer(&BUFFER_DESCRIPTOR);
let data = vec![255; size as usize];
ctx.queue.write_buffer(&buffer, offset, &data);
});
}

try_copy(0, 0, false);
try_copy(4, 16 + 1, true);
try_copy(64, 20 + 2, true);
try_copy(256, 44 + 3, true);
try_copy(1024, 8 + 4, false);

try_copy(0, 4, false);
try_copy(4 + 1, 8, true);
try_copy(64 + 2, 12, true);
try_copy(256 + 3, 16, true);
try_copy(1024 + 4, 4, false);
}

const BUFFER_SIZE: BufferAddress = 1234;

const BUFFER_DESCRIPTOR: wgpu::BufferDescriptor = wgpu::BufferDescriptor {
label: None,
size: BUFFER_SIZE,
usage: wgpu::BufferUsages::COPY_SRC.union(wgpu::BufferUsages::COPY_DST),
mapped_at_creation: false,
};
2 changes: 2 additions & 0 deletions wgpu/tests/root.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// All files containing tests
mod common;

mod buffer_copy;
mod clear_texture;
mod device;
mod example_wgsl;
mod instance;
mod poll;
mod resource_descriptor_accessor;
mod shader_primitive_index;
mod texture_bounds;
mod vertex_indices;
mod zero_init_texture_after_discard;
61 changes: 61 additions & 0 deletions wgpu/tests/texture_bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! Tests for texture copy bounds checks.

use crate::common::{initialize_test, TestParameters};
use std::num::NonZeroU32;

#[test]
fn bad_copy_origin() {
fn try_origin(origin: wgpu::Origin3d, should_panic: bool) {
let mut parameters = TestParameters::default();
if should_panic {
parameters = parameters.failure();
}

initialize_test(parameters, |ctx| {
let texture = ctx.device.create_texture(&TEXTURE_DESCRIPTOR);
let data = vec![255; BUFFER_SIZE as usize];
ctx.queue.write_texture(
wgpu::ImageCopyTexture {
texture: &texture,
mip_level: 0,
origin,
aspect: wgpu::TextureAspect::All,
},
&data,
BUFFER_COPY_LAYOUT,
TEXTURE_SIZE,
);
});
}

try_origin(wgpu::Origin3d { x: 0, y: 0, z: 0 }, false);
try_origin(wgpu::Origin3d { x: 1, y: 0, z: 0 }, true);
try_origin(wgpu::Origin3d { x: 0, y: 1, z: 0 }, true);
try_origin(wgpu::Origin3d { x: 0, y: 0, z: 1 }, true);
}

const TEXTURE_SIZE: wgpu::Extent3d = wgpu::Extent3d {
width: 64,
height: 64,
depth_or_array_layers: 1,
};

const TEXTURE_DESCRIPTOR: wgpu::TextureDescriptor = wgpu::TextureDescriptor {
label: Some("CopyOrigin"),
size: TEXTURE_SIZE,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::COPY_DST.union(wgpu::TextureUsages::COPY_SRC),
};

const BYTES_PER_PIXEL: u32 = 4;

const BUFFER_SIZE: u32 = TEXTURE_SIZE.width * TEXTURE_SIZE.height * BYTES_PER_PIXEL;

const BUFFER_COPY_LAYOUT: wgpu::ImageDataLayout = wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: NonZeroU32::new(TEXTURE_SIZE.width * BYTES_PER_PIXEL),
rows_per_image: None,
};

0 comments on commit abc7501

Please sign in to comment.