803: Player-based GPU test framework r=cwfitzgerald a=kvark

**Connections**
Closes #786

**Description**
This change adds a GPU-based testing by re-using the Player from tracing infrastructure - #289.
It converts the player into a lib + binary, and adds an integration test into the crate that implements RON-specified testing.

Current implementation has a few requirements/gotchas that are listed in `test.rs`:
* in all the IDs, the backend is `Empty`
* all expected buffers have `MAP_READ` usage on them
* last action is `Submit`

I believe it's workable, and we can improve it down the road (e.g. with #792).

**Testing**
MUHAHAHA
`cargo test` nails it

Co-authored-by: Dzmitry Malyshau <kvarkus@gmail.com>
This commit is contained in:
bors[bot]
2020-07-17 15:03:49 +00:00
committed by GitHub
9 changed files with 399 additions and 169 deletions

1
Cargo.lock generated
View File

@@ -978,6 +978,7 @@ dependencies = [
"raw-window-handle",
"renderdoc",
"ron",
"serde",
"wgpu-core",
"wgpu-types",
"winit",

View File

@@ -36,3 +36,6 @@ features = ["replay", "raw-window-handle"]
[target.'cfg(all(unix, not(target_os = "ios"), not(target_os = "macos")))'.dependencies]
gfx-backend-vulkan = { version = "0.5", features = ["x11"] }
[dev-dependencies]
serde = "1"

View File

@@ -1,10 +1,11 @@
# wgpu player
This is application that allows replaying the `wgpu` workloads recorded elsewhere.
This is application that allows replaying the `wgpu` workloads recorded elsewhere. You must use the player built from
the same revision as an application was linking to, or otherwise the data may fail to load.
Launch as:
```rust
player <trace-dir>
play <trace-dir>
```
When built with "winit" feature, it's able to replay the workloads that operate on a swapchain. It renders each frame sequentially, then waits for the user to close the window. When built without "winit", it launches in console mode and can replay any trace that doesn't use swapchains.

166
player/src/bin/play.rs Normal file
View File

@@ -0,0 +1,166 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*! This is a player for WebGPU traces.
!*/
use player::{gfx_select, GlobalPlay as _, IdentityPassThroughFactory};
use wgc::device::trace;
use std::{
fs,
path::{Path, PathBuf},
};
fn main() {
#[cfg(feature = "winit")]
use winit::{event_loop::EventLoop, window::WindowBuilder};
env_logger::init();
#[cfg(feature = "renderdoc")]
#[cfg_attr(feature = "winit", allow(unused))]
let mut rd = renderdoc::RenderDoc::<renderdoc::V110>::new()
.expect("Failed to connect to RenderDoc: are you running without it?");
//TODO: setting for the backend bits
//TODO: setting for the target frame, or controls
let dir = match std::env::args().nth(1) {
Some(arg) if Path::new(&arg).is_dir() => PathBuf::from(arg),
_ => panic!("Provide the dir path as the parameter"),
};
log::info!("Loading trace '{:?}'", dir);
let file = fs::File::open(dir.join(trace::FILE_NAME)).unwrap();
let mut actions: Vec<trace::Action> = ron::de::from_reader(file).unwrap();
actions.reverse(); // allows us to pop from the top
log::info!("Found {} actions", actions.len());
#[cfg(feature = "winit")]
let event_loop = {
log::info!("Creating a window");
EventLoop::new()
};
#[cfg(feature = "winit")]
let window = WindowBuilder::new()
.with_title("wgpu player")
.with_resizable(false)
.build(&event_loop)
.unwrap();
let global =
wgc::hub::Global::new("player", IdentityPassThroughFactory, wgt::BackendBit::all());
let mut command_buffer_id_manager = wgc::hub::IdentityManager::default();
#[cfg(feature = "winit")]
let surface =
global.instance_create_surface(&window, wgc::id::TypedId::zip(0, 1, wgt::Backend::Empty));
let device = match actions.pop() {
Some(trace::Action::Init { desc, backend }) => {
log::info!("Initializing the device for backend: {:?}", backend);
let adapter = global
.pick_adapter(
&wgc::instance::RequestAdapterOptions {
power_preference: wgt::PowerPreference::Default,
#[cfg(feature = "winit")]
compatible_surface: Some(surface),
#[cfg(not(feature = "winit"))]
compatible_surface: None,
},
wgc::instance::AdapterInputs::IdSet(
&[wgc::id::TypedId::zip(0, 0, backend)],
|id| id.backend(),
),
)
.expect("Unable to find an adapter for selected backend");
let info = gfx_select!(adapter => global.adapter_get_info(adapter));
log::info!("Picked '{}'", info.name);
gfx_select!(adapter => global.adapter_request_device(
adapter,
&desc,
None,
wgc::id::TypedId::zip(1, 0, wgt::Backend::Empty)
))
.expect("Failed to request device")
}
_ => panic!("Expected Action::Init"),
};
log::info!("Executing actions");
#[cfg(not(feature = "winit"))]
{
#[cfg(feature = "renderdoc")]
rd.start_frame_capture(std::ptr::null(), std::ptr::null());
while let Some(action) = actions.pop() {
gfx_select!(device => global.process(device, action, &dir, &mut command_buffer_id_manager));
}
#[cfg(feature = "renderdoc")]
rd.end_frame_capture(std::ptr::null(), std::ptr::null());
gfx_select!(device => global.device_poll(device, true));
}
#[cfg(feature = "winit")]
{
use winit::{
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::ControlFlow,
};
let mut frame_count = 0;
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
match event {
Event::MainEventsCleared => {
window.request_redraw();
}
Event::RedrawRequested(_) => loop {
match actions.pop() {
Some(trace::Action::CreateSwapChain { id, desc }) => {
log::info!("Initializing the swapchain");
assert_eq!(id.to_surface_id(), surface);
window.set_inner_size(winit::dpi::PhysicalSize::new(
desc.width,
desc.height,
));
gfx_select!(device => global.device_create_swap_chain(device, surface, &desc));
}
Some(trace::Action::PresentSwapChain(id)) => {
frame_count += 1;
log::debug!("Presenting frame {}", frame_count);
gfx_select!(device => global.swap_chain_present(id));
break;
}
Some(action) => {
gfx_select!(device => global.process(device, action, &dir, &mut command_buffer_id_manager));
}
None => break,
}
},
Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Escape),
state: ElementState::Pressed,
..
},
..
}
| WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
}
_ => {}
},
Event::LoopDestroyed => {
log::info!("Closing");
gfx_select!(device => global.device_poll(device, true));
}
_ => {}
}
});
}
}

View File

@@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*! This is a player for WebGPU traces.
/*! This is a player library for WebGPU traces.
*
* # Notes
* - we call device_maintain_ids() before creating any refcounted resource,
@@ -12,15 +12,9 @@
use wgc::device::trace;
use std::{
ffi::CString,
fmt::Debug,
fs,
marker::PhantomData,
path::{Path, PathBuf},
ptr,
};
use std::{ffi::CString, fmt::Debug, fs, marker::PhantomData, path::Path, ptr};
#[macro_export]
macro_rules! gfx_select {
($id:expr => $global:ident.$method:ident( $($param:expr),+ )) => {
match $id.backend() {
@@ -56,7 +50,7 @@ impl Label {
}
#[derive(Debug)]
struct IdentityPassThrough<I>(PhantomData<I>);
pub struct IdentityPassThrough<I>(PhantomData<I>);
impl<I: Clone + Debug + wgc::id::TypedId> wgc::hub::IdentityHandler<I> for IdentityPassThrough<I> {
type Input = I;
@@ -67,7 +61,7 @@ impl<I: Clone + Debug + wgc::id::TypedId> wgc::hub::IdentityHandler<I> for Ident
fn free(&self, _id: I) {}
}
struct IdentityPassThroughFactory;
pub struct IdentityPassThroughFactory;
impl<I: Clone + Debug + wgc::id::TypedId> wgc::hub::IdentityHandlerFactory<I>
for IdentityPassThroughFactory
@@ -79,7 +73,7 @@ impl<I: Clone + Debug + wgc::id::TypedId> wgc::hub::IdentityHandlerFactory<I>
}
impl wgc::hub::GlobalIdentityHandlerFactory for IdentityPassThroughFactory {}
trait GlobalExt {
pub trait GlobalPlay {
fn encode_commands<B: wgc::hub::GfxBackend>(
&self,
encoder: wgc::id::CommandEncoderId,
@@ -89,12 +83,12 @@ trait GlobalExt {
&self,
device: wgc::id::DeviceId,
action: trace::Action,
dir: &PathBuf,
dir: &Path,
comb_manager: &mut wgc::hub::IdentityManager,
);
}
impl GlobalExt for wgc::hub::Global<IdentityPassThroughFactory> {
impl GlobalPlay for wgc::hub::Global<IdentityPassThroughFactory> {
fn encode_commands<B: wgc::hub::GfxBackend>(
&self,
encoder: wgc::id::CommandEncoderId,
@@ -148,7 +142,7 @@ impl GlobalExt for wgc::hub::Global<IdentityPassThroughFactory> {
&self,
device: wgc::id::DeviceId,
action: trace::Action,
dir: &PathBuf,
dir: &Path,
comb_manager: &mut wgc::hub::IdentityManager,
) {
use wgc::device::trace::Action as A;
@@ -416,155 +410,3 @@ impl GlobalExt for wgc::hub::Global<IdentityPassThroughFactory> {
}
}
}
fn main() {
#[cfg(feature = "winit")]
use winit::{event_loop::EventLoop, window::WindowBuilder};
env_logger::init();
#[cfg(feature = "renderdoc")]
#[cfg_attr(feature = "winit", allow(unused))]
let mut rd = renderdoc::RenderDoc::<renderdoc::V110>::new()
.expect("Failed to connect to RenderDoc: are you running without it?");
//TODO: setting for the backend bits
//TODO: setting for the target frame, or controls
let dir = match std::env::args().nth(1) {
Some(arg) if Path::new(&arg).is_dir() => PathBuf::from(arg),
_ => panic!("Provide the dir path as the parameter"),
};
log::info!("Loading trace '{:?}'", dir);
let file = fs::File::open(dir.join(trace::FILE_NAME)).unwrap();
let mut actions: Vec<trace::Action> = ron::de::from_reader(file).unwrap();
actions.reverse(); // allows us to pop from the top
log::info!("Found {} actions", actions.len());
#[cfg(feature = "winit")]
let event_loop = {
log::info!("Creating a window");
EventLoop::new()
};
#[cfg(feature = "winit")]
let window = WindowBuilder::new()
.with_title("wgpu player")
.with_resizable(false)
.build(&event_loop)
.unwrap();
let global =
wgc::hub::Global::new("player", IdentityPassThroughFactory, wgt::BackendBit::all());
let mut command_buffer_id_manager = wgc::hub::IdentityManager::default();
#[cfg(feature = "winit")]
let surface =
global.instance_create_surface(&window, wgc::id::TypedId::zip(0, 1, wgt::Backend::Empty));
let device = match actions.pop() {
Some(trace::Action::Init { desc, backend }) => {
log::info!("Initializing the device for backend: {:?}", backend);
let adapter = global
.pick_adapter(
&wgc::instance::RequestAdapterOptions {
power_preference: wgt::PowerPreference::Default,
#[cfg(feature = "winit")]
compatible_surface: Some(surface),
#[cfg(not(feature = "winit"))]
compatible_surface: None,
},
wgc::instance::AdapterInputs::IdSet(
&[wgc::id::TypedId::zip(0, 0, backend)],
|id| id.backend(),
),
)
.expect("Unable to find an adapter for selected backend");
let info = gfx_select!(adapter => global.adapter_get_info(adapter));
log::info!("Picked '{}'", info.name);
gfx_select!(adapter => global.adapter_request_device(
adapter,
&desc,
None,
wgc::id::TypedId::zip(1, 0, wgt::Backend::Empty)
))
.expect("Failed to request device")
}
_ => panic!("Expected Action::Init"),
};
log::info!("Executing actions");
#[cfg(not(feature = "winit"))]
{
#[cfg(feature = "renderdoc")]
rd.start_frame_capture(ptr::null(), ptr::null());
while let Some(action) = actions.pop() {
gfx_select!(device => global.process(device, action, &dir, &mut command_buffer_id_manager));
}
#[cfg(feature = "renderdoc")]
rd.end_frame_capture(ptr::null(), ptr::null());
gfx_select!(device => global.device_poll(device, true));
}
#[cfg(feature = "winit")]
{
use winit::{
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::ControlFlow,
};
let mut frame_count = 0;
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
match event {
Event::MainEventsCleared => {
window.request_redraw();
}
Event::RedrawRequested(_) => loop {
match actions.pop() {
Some(trace::Action::CreateSwapChain { id, desc }) => {
log::info!("Initializing the swapchain");
assert_eq!(id.to_surface_id(), surface);
window.set_inner_size(winit::dpi::PhysicalSize::new(
desc.width,
desc.height,
));
gfx_select!(device => global.device_create_swap_chain(device, surface, &desc));
}
Some(trace::Action::PresentSwapChain(id)) => {
frame_count += 1;
log::debug!("Presenting frame {}", frame_count);
gfx_select!(device => global.swap_chain_present(id));
break;
}
Some(action) => {
gfx_select!(device => global.process(device, action, &dir, &mut command_buffer_id_manager));
}
None => break,
}
},
Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Escape),
state: ElementState::Pressed,
..
},
..
}
| WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
}
_ => {}
},
Event::LoopDestroyed => {
log::info!("Closing");
gfx_select!(device => global.device_poll(device, true));
}
_ => {}
}
});
}
}

View File

@@ -0,0 +1,6 @@
(
backends: (bits: 0x7),
tests: [
"buffer-copy.ron",
],
)

View File

@@ -0,0 +1,34 @@
(
features: (bits: 0x0),
expectations: [
(
name: "basic",
buffer: (index: 0, epoch: 1),
offset: 0,
data: [0x00, 0x00, 0x80, 0xBF],
)
],
actions: [
CreateBuffer(
id: Id(0, 1, Empty),
desc: (
label: "",
size: 16,
usage: (
bits: 41,
),
mapped_at_creation: false,
),
),
WriteBuffer(
id: Id(0, 1, Empty),
data: "data1.bin",
range: (
start: 0,
end: 16,
),
queued: true,
),
Submit(1, []),
],
)

BIN
player/tests/data/data1.bin Normal file

Binary file not shown.

177
player/tests/test.rs Normal file
View File

@@ -0,0 +1,177 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*! Tester for WebGPU
* It enumerates the available backends on the system,
* and run the tests through them.
*
* Test requirements:
* - all IDs have the backend `Empty`
* - all expected buffers have `MAP_READ` usage
* - last action is `Submit`
* - no swapchain use
!*/
use player::{gfx_select, GlobalPlay, IdentityPassThroughFactory};
use std::{
fs::{read_to_string, File},
path::{Path, PathBuf},
ptr, slice,
};
#[derive(serde::Deserialize)]
struct RawId {
index: u32,
epoch: u32,
}
#[derive(serde::Deserialize)]
struct Expectation {
name: String,
buffer: RawId,
offset: wgt::BufferAddress,
data: Vec<u8>,
}
#[derive(serde::Deserialize)]
struct Test {
features: wgt::Features,
expectations: Vec<Expectation>,
actions: Vec<wgc::device::trace::Action>,
}
extern "C" fn map_callback(status: wgc::resource::BufferMapAsyncStatus, _user_data: *mut u8) {
match status {
wgc::resource::BufferMapAsyncStatus::Success => (),
_ => panic!("Unable to map"),
}
}
impl Test {
fn load(path: PathBuf, backend: wgt::Backend) -> Self {
let backend_name = match backend {
wgt::Backend::Vulkan => "Vulkan",
wgt::Backend::Metal => "Metal",
wgt::Backend::Dx12 => "Dx12",
wgt::Backend::Dx11 => "Dx11",
wgt::Backend::Gl => "Gl",
_ => unreachable!(),
};
let string = read_to_string(path).unwrap().replace("Empty", backend_name);
ron::de::from_str(&string).unwrap()
}
fn run(
self,
dir: &Path,
global: &wgc::hub::Global<IdentityPassThroughFactory>,
adapter: wgc::id::AdapterId,
) {
let backend = adapter.backend();
let device = gfx_select!(adapter => global.adapter_request_device(
adapter,
&wgt::DeviceDescriptor {
features: self.features | wgt::Features::MAPPABLE_PRIMARY_BUFFERS,
limits: wgt::Limits::default(),
shader_validation: true,
},
None,
wgc::id::TypedId::zip(1, 0, backend)
))
.unwrap();
let mut command_buffer_id_manager = wgc::hub::IdentityManager::default();
println!("\t\t\tRunning...");
for action in self.actions {
gfx_select!(device => global.process(device, action, dir, &mut command_buffer_id_manager));
}
println!("\t\t\tMapping...");
for expect in &self.expectations {
let buffer = wgc::id::TypedId::zip(expect.buffer.index, expect.buffer.epoch, backend);
gfx_select!(device => global.buffer_map_async(
buffer,
expect.offset .. expect.offset+expect.data.len() as wgt::BufferAddress,
wgc::resource::BufferMapOperation {
host: wgc::device::HostMap::Read,
callback: map_callback,
user_data: ptr::null_mut(),
}
));
}
println!("\t\t\tWaiting...");
gfx_select!(device => global.device_poll(device, true));
for expect in self.expectations {
println!("\t\t\tChecking {}", expect.name);
let buffer = wgc::id::TypedId::zip(expect.buffer.index, expect.buffer.epoch, backend);
let ptr =
gfx_select!(device => global.buffer_get_mapped_range(buffer, expect.offset, None));
let contents = unsafe { slice::from_raw_parts(ptr, expect.data.len()) };
assert_eq!(&expect.data[..], contents);
}
}
}
#[derive(serde::Deserialize)]
struct Corpus {
backends: wgt::BackendBit,
tests: Vec<String>,
}
const BACKENDS: &[wgt::Backend] = &[
wgt::Backend::Vulkan,
wgt::Backend::Metal,
wgt::Backend::Dx12,
wgt::Backend::Dx11,
wgt::Backend::Gl,
];
impl Corpus {
fn run_from(path: PathBuf) {
println!("Corpus {:?}", path);
let dir = path.parent().unwrap();
let corpus: Corpus = ron::de::from_reader(File::open(&path).unwrap()).unwrap();
let global = wgc::hub::Global::new("test", IdentityPassThroughFactory, corpus.backends);
for &backend in BACKENDS {
if !corpus.backends.contains(backend.into()) {
continue;
}
let adapter = match global.pick_adapter(
&wgc::instance::RequestAdapterOptions {
power_preference: wgt::PowerPreference::Default,
compatible_surface: None,
},
wgc::instance::AdapterInputs::IdSet(
&[wgc::id::TypedId::zip(0, 0, backend)],
|id| id.backend(),
),
) {
Some(adapter) => adapter,
None => continue,
};
println!("\tBackend {:?}", backend);
let supported_features = gfx_select!(adapter => global.adapter_features(adapter));
for test_path in &corpus.tests {
println!("\t\tTest '{:?}'", test_path);
let test = Test::load(dir.join(test_path), adapter.backend());
if !supported_features.contains(test.features) {
println!(
"\t\tSkipped due to missing features {:?}",
test.features - supported_features
);
continue;
}
test.run(dir, &global, adapter);
}
}
}
}
#[test]
fn test_api() {
Corpus::run_from(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/data/all.ron"))
}