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