16
16
17
17
//! A simple application to run a compute shader.
18
18
19
- mod encode;
20
-
21
19
use std:: time:: Instant ;
22
20
23
21
use wgpu:: util:: DeviceExt ;
24
22
25
- use encode :: Codable ;
23
+ use bytemuck ;
26
24
27
25
async fn run ( ) {
28
26
let instance = wgpu:: Instance :: new ( wgpu:: Backends :: PRIMARY ) ;
@@ -57,10 +55,11 @@ async fn run() {
57
55
source : wgpu:: ShaderSource :: Wgsl ( include_str ! ( "shader.wgsl" ) . into ( ) ) ,
58
56
} ) ;
59
57
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) ;
61
60
let input_buf = device. create_buffer_init ( & wgpu:: util:: BufferInitDescriptor {
62
61
label : None ,
63
- contents : & input,
62
+ contents : input,
64
63
usage : wgpu:: BufferUsages :: STORAGE
65
64
| wgpu:: BufferUsages :: COPY_DST
66
65
| wgpu:: BufferUsages :: COPY_SRC ,
@@ -103,7 +102,7 @@ async fn run() {
103
102
let mut cpass = encoder. begin_compute_pass ( & Default :: default ( ) ) ;
104
103
cpass. set_pipeline ( & pipeline) ;
105
104
cpass. set_bind_group ( 0 , & bind_group, & [ ] ) ;
106
- cpass. dispatch ( 1 , 1 , 1 ) ;
105
+ cpass. dispatch ( input_f . len ( ) as u32 , 1 , 1 ) ;
107
106
}
108
107
if let Some ( query_set) = & query_set {
109
108
encoder. write_timestamp ( query_set, 1 ) ;
@@ -122,32 +121,18 @@ async fn run() {
122
121
device. poll ( wgpu:: Maintain :: Wait ) ;
123
122
println ! ( "post-poll {:?}" , std:: time:: Instant :: now( ) ) ;
124
123
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) ;
126
126
println ! ( "data: {:?}" , & * data) ;
127
127
}
128
128
if features. contains ( wgpu:: Features :: TIMESTAMP_QUERY ) {
129
129
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 ) ;
136
133
}
137
134
}
138
135
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
-
151
136
fn main ( ) {
152
137
pollster:: block_on ( run ( ) ) ;
153
138
}
0 commit comments