Skip to content

Commit 3459ee0

Browse files
committed
clean up compute-shader-hello a bit
1 parent 06ed792 commit 3459ee0

File tree

2 files changed

+10
-92
lines changed

2 files changed

+10
-92
lines changed

compute-shader-hello/src/encode.rs

Lines changed: 0 additions & 67 deletions
This file was deleted.

compute-shader-hello/src/main.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616

1717
//! A simple application to run a compute shader.
1818
19-
mod encode;
20-
2119
use std::time::Instant;
2220

2321
use wgpu::util::DeviceExt;
2422

25-
use encode::Codable;
23+
use bytemuck;
2624

2725
async fn run() {
2826
let instance = wgpu::Instance::new(wgpu::Backends::PRIMARY);
@@ -57,10 +55,11 @@ async fn run() {
5755
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
5856
});
5957
println!("shader compilation {:?}", start_instant.elapsed());
60-
let input: Vec<u8> = Codable::encode_vec(&[1.0f32, 2.0f32]);
58+
let input_f = &[1.0f32, 2.0f32];
59+
let input : &[u8] = bytemuck::bytes_of(input_f);
6160
let input_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
6261
label: None,
63-
contents: &input,
62+
contents: input,
6463
usage: wgpu::BufferUsages::STORAGE
6564
| wgpu::BufferUsages::COPY_DST
6665
| wgpu::BufferUsages::COPY_SRC,
@@ -103,7 +102,7 @@ async fn run() {
103102
let mut cpass = encoder.begin_compute_pass(&Default::default());
104103
cpass.set_pipeline(&pipeline);
105104
cpass.set_bind_group(0, &bind_group, &[]);
106-
cpass.dispatch(1, 1, 1);
105+
cpass.dispatch(input_f.len() as u32, 1, 1);
107106
}
108107
if let Some(query_set) = &query_set {
109108
encoder.write_timestamp(query_set, 1);
@@ -122,32 +121,18 @@ async fn run() {
122121
device.poll(wgpu::Maintain::Wait);
123122
println!("post-poll {:?}", std::time::Instant::now());
124123
if buf_future.await.is_ok() {
125-
let data = buf_slice.get_mapped_range();
124+
let data_raw = &*buf_slice.get_mapped_range();
125+
let data : &[f32] = bytemuck::cast_slice(data_raw);
126126
println!("data: {:?}", &*data);
127127
}
128128
if features.contains(wgpu::Features::TIMESTAMP_QUERY) {
129129
let ts_period = queue.get_timestamp_period();
130-
let ts_data: Vec<u64> = Codable::decode_vec(&*query_slice.get_mapped_range());
131-
let ts_data = ts_data
132-
.iter()
133-
.map(|ts| *ts as f64 * ts_period as f64 * 1e-6)
134-
.collect::<Vec<_>>();
135-
println!("compute shader elapsed: {:?}ms", ts_data[1] - ts_data[0]);
130+
let ts_data_raw = &*query_slice.get_mapped_range();
131+
let ts_data : &[u64] = bytemuck::cast_slice(ts_data_raw);
132+
println!("compute shader elapsed: {:?}ms", (ts_data[1] - ts_data[0]) as f64 * ts_period as f64 * 1e-6);
136133
}
137134
}
138135

139-
#[allow(unused)]
140-
fn bytes_to_u32(bytes: &[u8]) -> Vec<u32> {
141-
bytes
142-
.chunks_exact(4)
143-
.map(|b| {
144-
let mut bytes = [0; 4];
145-
bytes.copy_from_slice(b);
146-
u32::from_le_bytes(bytes)
147-
})
148-
.collect()
149-
}
150-
151136
fn main() {
152137
pollster::block_on(run());
153138
}

0 commit comments

Comments
 (0)