Skip to content

Commit 9808cd0

Browse files
committed
example-runner-wgpu: fix compute example.
1 parent 2ed23d0 commit 9808cd0

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

examples/runners/wgpu/src/compute.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ pub async fn start_internal(
2424
.await
2525
.expect("Failed to find an appropriate adapter");
2626

27-
let mut features = wgpu::Features::TIMESTAMP_QUERY;
27+
let mut features =
28+
wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::TIMESTAMP_QUERY_INSIDE_PASSES;
2829
if options.force_spirv_passthru {
2930
features |= wgpu::Features::SPIRV_SHADER_PASSTHROUGH;
3031
}
@@ -110,10 +111,17 @@ pub async fn start_internal(
110111
let timestamp_buffer = device.create_buffer(&wgpu::BufferDescriptor {
111112
label: Some("Timestamps buffer"),
112113
size: 16,
114+
usage: wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC,
115+
mapped_at_creation: false,
116+
});
117+
118+
let timestamp_readback_buffer = device.create_buffer(&wgpu::BufferDescriptor {
119+
label: None,
120+
size: 16,
113121
usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
114122
mapped_at_creation: true,
115123
});
116-
timestamp_buffer.unmap();
124+
timestamp_readback_buffer.unmap();
117125

118126
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
119127
label: None,
@@ -150,10 +158,17 @@ pub async fn start_internal(
150158
src.len() as wgpu::BufferAddress,
151159
);
152160
encoder.resolve_query_set(&queries, 0..2, &timestamp_buffer, 0);
161+
encoder.copy_buffer_to_buffer(
162+
&timestamp_buffer,
163+
0,
164+
&timestamp_readback_buffer,
165+
0,
166+
timestamp_buffer.size(),
167+
);
153168

154169
queue.submit(Some(encoder.finish()));
155170
let buffer_slice = readback_buffer.slice(..);
156-
let timestamp_slice = timestamp_buffer.slice(..);
171+
let timestamp_slice = timestamp_readback_buffer.slice(..);
157172
timestamp_slice.map_async(wgpu::MapMode::Read, |r| r.unwrap());
158173
buffer_slice.map_async(wgpu::MapMode::Read, |r| r.unwrap());
159174
// NOTE(eddyb) `poll` should return only after the above callbacks fire
@@ -173,7 +188,7 @@ pub async fn start_internal(
173188
drop(data);
174189
readback_buffer.unmap();
175190
drop(timing_data);
176-
timestamp_buffer.unmap();
191+
timestamp_readback_buffer.unmap();
177192
let mut max = 0;
178193
for (src, out) in src_range.zip(result.iter().copied()) {
179194
if out == u32::MAX {

0 commit comments

Comments
 (0)