Skip to content

Commit

Permalink
dedicated sample for timestamp queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed May 7, 2023
1 parent d2f7e8b commit 9680a18
Show file tree
Hide file tree
Showing 7 changed files with 393 additions and 146 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ By @cwfitzgerald in [#3671](https://github.com/gfx-rs/wgpu/pull/3671).
Addition of `TimestampWrites` to compute and render passes to allow profiling.
This brings us in line with the spec.

Added new example to demonstrate the various kinds of timestamps.

By @FL33TW00D & @wumpf in [#3636](https://github.com/gfx-rs/wgpu/pull/3636).

#### Misc Breaking Changes
Expand Down
70 changes: 35 additions & 35 deletions wgpu/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,41 @@ All the examples use [WGSL](https://gpuweb.github.io/gpuweb/wgsl.html) shaders u
All framework-based examples render to the window and are reftested against the screenshot in the directory.

## Feature matrix
| Feature | boids | bunnymark | cube | mipmap | msaa-line | shadow | skybox | texture-arrays | water | conservative-raster | stencil-triangles |
|------------------------------| ------ | --------- | ------ | ------ | --------- | ------ | ------ | -------------- | ------ | ------------------- |-------------------|
| vertex attributes | :star: | | :star: | | :star: | :star: | :star: | :star: | :star: | | |
| instancing | :star: | | | | | | | | | | |
| lines and points | | | | | :star: | | | | | :star: | |
| dynamic buffer offsets | | :star: | | | | :star: | | | | | |
| implicit layout | | | | :star: | | | | | | | |
| sampled color textures | :star: | :star: | :star: | :star: | | | :star: | :star: | :star: | :star: | |
| storage textures | :star: | | | | | | | | | | |
| comparison samplers | | | | | | :star: | | | | | |
| subresource views | | | | :star: | | :star: | | | | | |
| cubemaps | | | | | | | :star: | | | | |
| multisampling | | | | | :star: | | | | | | |
| off-screen rendering | | | | | | :star: | | | :star: | :star: | |
| stencil testing | | | | | | | | | | | :star: |
| depth testing | | | | | | :star: | :star: | | :star: | | |
| depth biasing | | | | | | :star: | | | | | |
| read-only depth | | | | | | | | | :star: | | |
| blending | | :star: | :star: | | | | | | :star: | | |
| render bundles | | | | | :star: | | | | :star: | | |
| compute passes | :star: | | | | | | | | | | |
| error scopes | | | :star: | | | | | | | | |
| *optional extensions* | | | | | | | | :star: | | | |
| - SPIR-V shaders | | | | | | | | | | | |
| - binding array | | | | | | | | :star: | | | |
| - push constants | | | | | | | | | | | |
| - depth clamping | | | | | | :star: | | | | | |
| - compressed textures | | | | | | | :star: | | | | |
| - polygon mode | | | :star: | | | | | | | | |
| - queries | | | | :star: | | | | | | | |
| - conservative rasterization | | | | | | | | | | :star: | |
| *integrations* | | | | | | | | | | | |
| - staging belt | | | | | | | :star: | | | | |
| - typed arena | | | | | | | | | | | |
| - obj loading | | | | | | | :star: | | | | |
| Feature | boids | bunnymark | cube | mipmap | msaa-line | shadow | skybox | texture-arrays | water | conservative-raster | stencil-triangles | timestamp-queries |
|------------------------------| ------ | --------- | ------ | ------ | --------- | ------ | ------ | -------------- | ------ | ------------------- |-------------------|-------------------|
| vertex attributes | :star: | | :star: | | :star: | :star: | :star: | :star: | :star: | | | |
| instancing | :star: | | | | | | | | | | | |
| lines and points | | | | | :star: | | | | | :star: | | |
| dynamic buffer offsets | | :star: | | | | :star: | | | | | | |
| implicit layout | | | | :star: | | | | | | | | |
| sampled color textures | :star: | :star: | :star: | :star: | | | :star: | :star: | :star: | :star: | | |
| storage textures | :star: | | | | | | | | | | | |
| comparison samplers | | | | | | :star: | | | | | | |
| subresource views | | | | :star: | | :star: | | | | | | |
| cubemaps | | | | | | | :star: | | | | | |
| multisampling | | | | | :star: | | | | | | | |
| off-screen rendering | | | | | | :star: | | | :star: | :star: | | |
| stencil testing | | | | | | | | | | | :star: | |
| depth testing | | | | | | :star: | :star: | | :star: | | | |
| depth biasing | | | | | | :star: | | | | | | |
| read-only depth | | | | | | | | | :star: | | | |
| blending | | :star: | :star: | | | | | | :star: | | | |
| render bundles | | | | | :star: | | | | :star: | | | |
| compute passes | :star: | | | | | | | | | | | :star: |
| error scopes | | | :star: | | | | | | | | | |
| *optional extensions* | | | | | | | | :star: | | | | |
| - SPIR-V shaders | | | | | | | | | | | | |
| - binding array | | | | | | | | :star: | | | | |
| - push constants | | | | | | | | | | | | |
| - depth clamping | | | | | | :star: | | | | | | |
| - compressed textures | | | | | | | :star: | | | | | |
| - polygon mode | | | :star: | | | | | | | | | |
| - queries | | | | :star: | | | | | | | | :star: |
| - conservative rasterization | | | | | | | | | | :star: | | |
| *integrations* | | | | | | | | | | | | |
| - staging belt | | | | | | | :star: | | | | | |
| - typed arena | | | | | | | | | | | | |
| - obj loading | | | | | | | :star: | | | | | |

## Hacking

Expand Down
53 changes: 1 addition & 52 deletions wgpu/examples/hello-compute/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::{borrow::Cow, str::FromStr};
use wgpu::util::DeviceExt;
use wgpu::Buffer;

// Indicates a u32 overflow in an intermediate Collatz value
const OVERFLOW: u32 = 0xffffffff;

const NUM_SAMPLES: u64 = 2;

async fn run() {
let numbers = if std::env::args().len() <= 1 {
let default = vec![1, 2, 3, 4];
Expand Down Expand Up @@ -71,14 +68,6 @@ async fn execute_gpu_inner(
queue: &wgpu::Queue,
numbers: &[u32],
) -> Option<Vec<u32>> {
//Create query set
let query_set = device.create_query_set(&wgpu::QuerySetDescriptor {
label: Some("Timestamp query set"),
count: NUM_SAMPLES as u32,
ty: wgpu::QueryType::Timestamp,
});
let timestamp_period = queue.get_timestamp_period();

// Loads the shader from WGSL
let cs_module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
Expand Down Expand Up @@ -145,11 +134,7 @@ async fn execute_gpu_inner(
{
let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: None,
timestamp_writes: Some(wgpu::ComputePassTimestampWrites {
query_set: &query_set,
beginning_of_pass_write_index: Some(0),
end_of_pass_write_index: Some(1),
}),
timestamp_writes: None,
});
cpass.set_pipeline(&compute_pipeline);
cpass.set_bind_group(0, &bind_group, &[]);
Expand All @@ -160,27 +145,6 @@ async fn execute_gpu_inner(
// Will copy data from storage buffer on GPU to staging buffer on CPU.
encoder.copy_buffer_to_buffer(&storage_buffer, 0, &staging_buffer, 0, size);

let query_resolve_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("query resolve buffer"),
size: (std::mem::size_of::<u64>() * NUM_SAMPLES as usize) as wgpu::BufferAddress,
usage: wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::QUERY_RESOLVE,
mapped_at_creation: false,
});
let query_destination_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("query dest buffer"),
size: (std::mem::size_of::<u64>() * NUM_SAMPLES as usize) as wgpu::BufferAddress,
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
mapped_at_creation: false,
});
encoder.resolve_query_set(&query_set, 0..NUM_SAMPLES as u32, &query_resolve_buffer, 0);
encoder.copy_buffer_to_buffer(
&query_resolve_buffer,
0,
&query_destination_buffer,
0,
query_resolve_buffer.size(),
);

// Submits command encoder for processing
queue.submit(Some(encoder.finish()));

Expand Down Expand Up @@ -217,26 +181,11 @@ async fn execute_gpu_inner(
None
};

query_destination_buffer
.slice(..)
.map_async(wgpu::MapMode::Read, |_| ());
device.poll(wgpu::Maintain::Wait);
resolve_timestamps(&query_destination_buffer, timestamp_period);

res
}

fn resolve_timestamps(destination_buffer: &Buffer, timestamp_period: f32) {
let timestamp_view = destination_buffer
.slice(..(std::mem::size_of::<u64>() * 2) as wgpu::BufferAddress)
.get_mapped_range();

let timestamps: &[u64] = bytemuck::cast_slice(&timestamp_view);
log::info!("Timestamps: {:?}", timestamps);
let elapsed_ns = (timestamps[1] - timestamps[0]) as f64 * timestamp_period as f64;
log::info!("Elapsed time: {:.2} μs", elapsed_ns / 1000.0);
}

fn main() {
#[cfg(not(target_arch = "wasm32"))]
{
Expand Down
Loading

0 comments on commit 9680a18

Please sign in to comment.