www.xbdev.net
xbdev - software development
Sunday April 20, 2025
Home | Contact | Support | WebGPU Graphics and Compute ...
     
 

WebGPU/WGSL Tutorials and Articles

Graphics and Compute ...

 


You have to do some measuring to find that sweet spot - for the best performance - and you need some way of measuring GPU perfo...
You have to do some measuring to find that sweet spot - for the best performance - and you need some way of measuring GPU performance (not JavaScript performance on the CPU)! Image from `Cable Guy' - with Jim Carry - finding the sweet spot for installing the cable.


WebGPU timestamp-query Feature


The WebGPU
timestamp-query
feature is your power tool to get precise measurement about the GPU command execution times by recording timestamps before and after command execution. This allows you to analyze and optimize the performance of your GPU workloads (so you can squeeze every drop of power out of that GPU)!

You can't just use the
timestamp-query
feature out of the box - you have to do a few things first!

Before tryign to use the
timestamp-query
feature - you have to first enable it when requesting the device. This is important, as you can't use the timestamp queries in WebGPU, unless you enable this feature (otherwise you'll get an errors).

The modification to the
requestDevice(..)
call is shown below.

const device await adapter.requestDevice({
  
requiredFeatures: ["timestamp-query"],
});


Now you're ready to setup the queries.

The following gives you an example of setting up the query set - and the buffers for reading back the data (i.e., timing information).

const querySet device.createQuerySet({
  
type'timestamp',
  
count2,
});

const 
queryResolveBuffer device.createBuffer({
  
size16// 2 timestamps, each 64 bits (8 bytes)
  
usageGPUBufferUsage.QUERY_RESOLVE GPUBufferUsage.COPY_SRC,
});

const 
readBuffer device.createBuffer({
  
size16,
  
usageGPUBufferUsage.COPY_DST GPUBufferUsage.MAP_READ,
});


After this - you're ready to go! So in your render looop - you can call
writeTimestamp(..)
at the start and
writeTimestamp(..)
at the end - but within the
commandEncoder
- as it still needs to be executed by the GPU (calculate timing information and copy it back).


const commandEncoder device.createCommandEncoder();

commandEncoder.writeTimestamp(querySet0);
const 
renderPass commandEncoder.beginRenderPass(renderPassDescriptor);
renderPass.setPipeline(pipeline);
renderPass.setBindGroup(0uniformBindGroup);
renderPass.draw(4100);
renderPass.end();
commandEncoder.writeTimestamp(querySet1);

// Resolve the query results to the resolve buffer
commandEncoder.resolveQuerySet(querySet02queryResolveBuffer0);
// Copy the results to the read buffer
commandEncoder.copyBufferToBuffer(queryResolveBuffer0readBuffer016);

device.queue.submit([commandEncoder.finish()]);

await device.queue.onSubmittedWorkDone();


After the work on the GPU has finished - you just copy it back and use it for analysis - for example - we calculate the fps and display it on the window - to see what is the maximum FPS.

await readBuffer.mapAsync(GPUMapMode.READ);
const 
mappedRange readBuffer.getMappedRange();
const 
timestamps = new BigUint64Array(mappedRange);

const 
startTime Number(timestamps[0]);
const 
endTime Number(timestamps[1]);
const 
timeTakenNs endTime startTime;

const 
timeTakenMs timeTakenNs 1000000;
const 
maxFPS 1000 timeTakenMs;

console.log(`Time taken: ${timeTakenMs.toFixed(2)} ms`);
console.log(`Maximum FPS: ${maxFPS.toFixed(2)} fps`);

// Unmap the buffer after reading the data
readBuffer.unmap();


Below in the resources shows a complete example - where were implement a ray-tracer - which renders 16,000+ triangles using an octree structure (in real-time). We use the `timing-query` to give us the GPUs possible timing information (so we can display the maximum FPS - which should be greater than 60fps if we are expecting real-time performance).


Ray-trace animated floating world real-time using WebGPU.
Ray-trace animated floating world real-time using WebGPU.


You can tinker with the octree settings (numbers of cells/nodes) - to impact the performance - to check that it's reading correctly.


Resources & Links


Floating mesh world using the timing feature to display the FPS for the render loop.
Example of Timing Feature (Ray-Tracing Demo)











101 WebGPU Programming Projects. WebGPU Development Pixels - coding fragment shaders from post processing to ray tracing! WebGPU by Example: Fractals, Image Effects, Ray-Tracing, Procedural Geometry, 2D/3D, Particles, Simulations WebGPU Games WGSL 2d 3d interactive web-based fun learning WebGPU Compute WebGPU API - Owners WebGPU Development Cookbook - coding recipes for all your webgpu needs! WebGPU & WGSL Essentials: A Hands-On Approach to Interactive Graphics, Games, 2D Interfaces, 3D Meshes, Animation, Security and Production Kenwright graphics and animations using the webgpu api 12 week course kenwright learn webgpu api kenwright programming compute and graphics applications with html5 and webgpu api kenwright real-time 3d graphics with webgpu kenwright webgpu for dummies kenwright webgpu wgsl compute graphics all in one kenwright webgpu api develompent a quick start guide kenwright webgpu by example 2022 kenwright webgpu gems kenwright webgpu interactive compute and graphics visualization cookbook kenwright wgsl webgpu shading language cookbook kenwright WebGPU Shader Language Development: Vertex, Fragment, Compute Shaders for Programmers Kenwright WGSL Fundamentals book kenwright WebGPU Data Visualization Cookbook kenwright Special Effects Programming with WebGPU kenwright WebGPU Programming Guide: Interactive Graphics and Compute Programming with WebGPU & WGSL kenwright Ray-Tracing with WebGPU kenwright



 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.