mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-19 22:11:01 +01:00
273 lines
8.0 KiB
Rust
273 lines
8.0 KiB
Rust
//! Tests of [`wgpu::Buffer`] and related.
|
|
|
|
use std::any::Any;
|
|
|
|
use wgpu_core as wgc;
|
|
use wgpu_types as wgt;
|
|
|
|
use wgc::{command::Command, device::trace::Action};
|
|
|
|
#[derive(Eq, PartialEq)]
|
|
enum TestType {
|
|
Normal,
|
|
FailedCommands,
|
|
FailedSubmit,
|
|
}
|
|
|
|
fn trace_test(test_type: TestType) {
|
|
let global = wgc::global::Global::new(
|
|
"test",
|
|
wgt::instance::InstanceDescriptor {
|
|
backends: wgt::Backends::NOOP,
|
|
backend_options: wgt::BackendOptions {
|
|
noop: wgt::NoopBackendOptions::enabled(),
|
|
..Default::default()
|
|
},
|
|
..wgt::instance::InstanceDescriptor::new_without_display_handle()
|
|
},
|
|
None,
|
|
);
|
|
let adapter_id = global
|
|
.request_adapter(
|
|
&wgt::RequestAdapterOptions::default(),
|
|
wgt::Backends::NOOP,
|
|
None,
|
|
)
|
|
.unwrap();
|
|
let (device_id, queue_id) = global
|
|
.adapter_request_device(
|
|
adapter_id,
|
|
&wgt::DeviceDescriptor {
|
|
trace: wgt::Trace::Memory,
|
|
..Default::default()
|
|
},
|
|
None,
|
|
None,
|
|
)
|
|
.unwrap();
|
|
|
|
let (buffer_id, error) = global.device_create_buffer(
|
|
device_id,
|
|
&wgt::BufferDescriptor {
|
|
label: None,
|
|
size: 1024,
|
|
usage: wgt::BufferUsages::COPY_DST,
|
|
mapped_at_creation: false,
|
|
},
|
|
None,
|
|
);
|
|
assert!(error.is_none());
|
|
|
|
let (encoder_id, error) = global.device_create_command_encoder(
|
|
device_id,
|
|
&wgt::CommandEncoderDescriptor::default(),
|
|
None,
|
|
);
|
|
assert!(error.is_none());
|
|
|
|
match test_type {
|
|
TestType::Normal => {
|
|
global
|
|
.command_encoder_clear_buffer(encoder_id, buffer_id, 0, None)
|
|
.unwrap();
|
|
let (cmdbuf_id, error) = global.command_encoder_finish(
|
|
encoder_id,
|
|
&wgt::CommandBufferDescriptor::default(),
|
|
None,
|
|
);
|
|
assert!(error.is_none());
|
|
global.queue_submit(queue_id, &[cmdbuf_id]).unwrap();
|
|
}
|
|
TestType::FailedCommands => {
|
|
// Try to clear past the end of the buffer.
|
|
global
|
|
.command_encoder_clear_buffer(encoder_id, buffer_id, 0, Some(2048))
|
|
.unwrap();
|
|
let (_cmdbuf_id, error) = global.command_encoder_finish(
|
|
encoder_id,
|
|
&wgt::CommandBufferDescriptor::default(),
|
|
None,
|
|
);
|
|
assert!(error.is_some());
|
|
}
|
|
TestType::FailedSubmit => {
|
|
// Destroy the buffer after encoding the clear command, before submitting it.
|
|
global
|
|
.command_encoder_clear_buffer(encoder_id, buffer_id, 0, None)
|
|
.unwrap();
|
|
let (cmdbuf_id, error) = global.command_encoder_finish(
|
|
encoder_id,
|
|
&wgt::CommandBufferDescriptor::default(),
|
|
None,
|
|
);
|
|
assert!(error.is_none());
|
|
global.buffer_destroy(buffer_id);
|
|
global.queue_submit(queue_id, &[cmdbuf_id]).unwrap_err();
|
|
}
|
|
}
|
|
|
|
let trace = global.device_take_trace(device_id).unwrap();
|
|
let trace = (trace.as_ref() as &dyn Any)
|
|
.downcast_ref::<wgc::device::trace::MemoryTrace>()
|
|
.unwrap();
|
|
let actions = trace.actions();
|
|
|
|
match test_type {
|
|
TestType::Normal => {
|
|
let Some(Action::Submit(_, commands)) = actions.last() else {
|
|
panic!("expected last action to be Submit");
|
|
};
|
|
assert_eq!(commands.len(), 1);
|
|
assert!(matches!(
|
|
commands[0],
|
|
Command::ClearBuffer {
|
|
dst: _,
|
|
offset: 0,
|
|
size: None,
|
|
},
|
|
));
|
|
}
|
|
TestType::FailedCommands => {
|
|
let Some(Action::FailedCommands {
|
|
commands: Some(commands),
|
|
failed_at_submit: None,
|
|
error,
|
|
}) = actions.last()
|
|
else {
|
|
panic!("expected last action to be FailedCommands");
|
|
};
|
|
assert_eq!(
|
|
error,
|
|
"Clear of 0..2048 would end up overrunning the bounds of the buffer of size 1024"
|
|
);
|
|
assert_eq!(commands.len(), 1);
|
|
assert!(matches!(
|
|
commands[0],
|
|
Command::ClearBuffer {
|
|
dst: _,
|
|
offset: 0,
|
|
size: Some(2048),
|
|
},
|
|
));
|
|
}
|
|
TestType::FailedSubmit => {
|
|
let Some(Action::FailedCommands {
|
|
commands: Some(commands),
|
|
failed_at_submit: Some(_),
|
|
error,
|
|
}) = actions.last()
|
|
else {
|
|
panic!("expected last action to be FailedCommands");
|
|
};
|
|
assert_eq!(error, "Buffer with '' label has been destroyed");
|
|
assert_eq!(commands.len(), 1);
|
|
assert!(matches!(
|
|
commands[0],
|
|
Command::ClearBuffer {
|
|
dst: _,
|
|
offset: 0,
|
|
size: None,
|
|
},
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn trace_clear_buffer() {
|
|
trace_test(TestType::Normal);
|
|
}
|
|
|
|
#[test]
|
|
fn trace_failed_commands() {
|
|
trace_test(TestType::FailedCommands);
|
|
}
|
|
|
|
#[test]
|
|
fn trace_failed_submit() {
|
|
trace_test(TestType::FailedSubmit);
|
|
}
|
|
|
|
#[test]
|
|
fn trace_texture_test() {
|
|
let global = wgc::global::Global::new(
|
|
"test",
|
|
wgt::instance::InstanceDescriptor {
|
|
backends: wgt::Backends::NOOP,
|
|
backend_options: wgt::BackendOptions {
|
|
noop: wgt::NoopBackendOptions::enabled(),
|
|
..Default::default()
|
|
},
|
|
..wgt::instance::InstanceDescriptor::new_without_display_handle()
|
|
},
|
|
None,
|
|
);
|
|
let adapter_id = global
|
|
.request_adapter(
|
|
&wgt::RequestAdapterOptions::default(),
|
|
wgt::Backends::NOOP,
|
|
None,
|
|
)
|
|
.unwrap();
|
|
let (device_id, _) = global
|
|
.adapter_request_device(
|
|
adapter_id,
|
|
&wgt::DeviceDescriptor {
|
|
trace: wgt::Trace::Memory,
|
|
..Default::default()
|
|
},
|
|
None,
|
|
None,
|
|
)
|
|
.unwrap();
|
|
|
|
let desc = wgt::TextureDescriptor {
|
|
label: None,
|
|
size: wgt::Extent3d {
|
|
width: 1024,
|
|
height: 1024,
|
|
depth_or_array_layers: 1,
|
|
},
|
|
mip_level_count: 1,
|
|
sample_count: 1,
|
|
dimension: wgt::TextureDimension::D2,
|
|
format: wgt::TextureFormat::Rgba8Unorm,
|
|
usage: wgt::TextureUsages::COPY_DST | wgt::TextureUsages::TEXTURE_BINDING,
|
|
view_formats: Vec::new(),
|
|
};
|
|
|
|
let (texture_id, error) = global.device_create_texture(device_id, &desc, None);
|
|
|
|
assert!(error.is_none());
|
|
|
|
let texture_error_id = global.create_texture_error(device_id, None, &desc);
|
|
|
|
global.texture_drop(texture_id);
|
|
global.texture_drop(texture_error_id);
|
|
|
|
let trace = global.device_take_trace(device_id).unwrap();
|
|
let trace = (trace.as_ref() as &dyn Any)
|
|
.downcast_ref::<wgc::device::trace::MemoryTrace>()
|
|
.unwrap();
|
|
let actions = trace.actions();
|
|
// first one is init
|
|
let actions = &actions[1..];
|
|
|
|
assert_eq!(actions.len(), 4);
|
|
|
|
let Action::CreateTexture(texture, ..) = actions[0] else {
|
|
panic!("expected first action to be CreateTexture");
|
|
};
|
|
let Action::CreateTextureError(texture_error, ..) = actions[1] else {
|
|
panic!("expected second action to be CreateTextureError");
|
|
};
|
|
let Action::DropTexture(texture_drop) = actions[2] else {
|
|
panic!("expected third action to be DropTexture");
|
|
};
|
|
assert_eq!(texture, texture_drop);
|
|
let Action::DropTexture(texture_error_drop) = actions[3] else {
|
|
panic!("expected fourth action to be DropTexture");
|
|
};
|
|
assert_eq!(texture_error, texture_error_drop);
|
|
}
|