2021-05-29 08:17:36 +10:00
|
|
|
use piet_gpu_hal::include_shader;
|
2021-05-30 09:33:52 +10:00
|
|
|
use piet_gpu_hal::{BufferUsage, Instance, Session};
|
2020-04-07 05:11:37 +10:00
|
|
|
|
|
|
|
fn main() {
|
2021-05-26 11:06:51 +10:00
|
|
|
let (instance, _) = Instance::new(None).unwrap();
|
2020-04-07 05:11:37 +10:00
|
|
|
unsafe {
|
2020-04-30 23:02:48 +10:00
|
|
|
let device = instance.device(None).unwrap();
|
2021-05-30 09:33:52 +10:00
|
|
|
let session = Session::new(device);
|
2021-05-25 04:44:56 +10:00
|
|
|
let usage = BufferUsage::MAP_READ | BufferUsage::STORAGE;
|
2020-04-07 05:11:37 +10:00
|
|
|
let src = (0..256).map(|x| x + 1).collect::<Vec<u32>>();
|
2021-05-25 04:44:56 +10:00
|
|
|
let buffer = session.create_buffer_init(&src, usage).unwrap();
|
2021-05-29 08:17:36 +10:00
|
|
|
let code = include_shader!(&session, "./shader/gen/collatz");
|
2020-11-25 07:36:27 +11:00
|
|
|
let pipeline = session.create_simple_compute_pipeline(code, 1).unwrap();
|
2020-11-18 03:04:25 +11:00
|
|
|
let descriptor_set = session
|
2020-11-25 07:36:27 +11:00
|
|
|
.create_simple_descriptor_set(&pipeline, &[&buffer])
|
2020-11-15 03:22:56 +11:00
|
|
|
.unwrap();
|
2020-11-18 03:04:25 +11:00
|
|
|
let query_pool = session.create_query_pool(2).unwrap();
|
|
|
|
let mut cmd_buf = session.cmd_buf().unwrap();
|
2020-04-07 05:11:37 +10:00
|
|
|
cmd_buf.begin();
|
2020-04-30 11:13:56 +10:00
|
|
|
cmd_buf.reset_query_pool(&query_pool);
|
2020-04-13 15:28:03 +10:00
|
|
|
cmd_buf.write_timestamp(&query_pool, 0);
|
2021-05-29 08:17:36 +10:00
|
|
|
cmd_buf.dispatch(&pipeline, &descriptor_set, (256, 1, 1), (1, 1, 1));
|
2020-04-13 15:28:03 +10:00
|
|
|
cmd_buf.write_timestamp(&query_pool, 1);
|
2020-11-16 08:03:05 +11:00
|
|
|
cmd_buf.host_barrier();
|
2020-04-07 05:11:37 +10:00
|
|
|
cmd_buf.finish();
|
2020-11-18 03:04:25 +11:00
|
|
|
let submitted = session.run_cmd_buf(cmd_buf, &[], &[]).unwrap();
|
|
|
|
submitted.wait().unwrap();
|
|
|
|
let timestamps = session.fetch_query_pool(&query_pool);
|
2020-04-07 05:11:37 +10:00
|
|
|
let mut dst: Vec<u32> = Default::default();
|
2020-11-18 03:04:25 +11:00
|
|
|
buffer.read(&mut dst).unwrap();
|
2020-04-07 05:11:37 +10:00
|
|
|
for (i, val) in dst.iter().enumerate().take(16) {
|
|
|
|
println!("{}: {}", i, val);
|
|
|
|
}
|
2020-04-13 15:28:03 +10:00
|
|
|
println!("{:?}", timestamps);
|
2020-04-07 05:11:37 +10:00
|
|
|
}
|
|
|
|
}
|