How to time profile/see when tasks are complete in wgpu (targeting web)?

314 Views Asked by At

Basically title. There are support for timestamp queries, but that requires specific device feature support (my laptop for example does not support it when targeting web). There is also the function Queue::onSubmittedWorkDone(callback) that throws an unreachable code error when I use any callback on wasm. Finally, device.poll(Maintain::wait) is explicitly a no-op on web according to the docs (for me, it never returns true when I try it). Is there any way to see whats in the Queue or check if it is empty? In the WebGPU api spec, onSubmittedWorkDone is an async function that returns a Promise. If that were the case, we could simply await until completion. I included an example with my attempt at using Queue::onSubmittedWorkDone(callback) in case it is user error (likely).

let mut encoder = driver.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
self.calculate_summary(&mut encoder);
self.color_map(&mut encoder); 
driver.queue.on_submitted_work_done(Self::done);
driver.queue.submit(Some(encoder.finish()));

fn done(){
\\\Ive tried just print statements, incrementing dummy variables, and even empty functions, but it doesn't work
}
2

There are 2 best solutions below

1
Andrew P On BEST ANSWER

I had a similar situation for a complex render chain and came up with the solution of reading a 1-byte buffer. I can then block awaiting the result, and so then I know the queue is empty again.

4
gman On

You might be able to use timestamp queries. At this point in time they are in limbo because the spec for them is not done but, you can enable the current version in Chrome by going to about:flags and enabling "WebGPU Developer Features" and passing in --enable-dawn-features=allow_unsafe_apis on the command line. (Yes, that means this is not a user facing feature yet)

Maybe they're still disabled on your machine? This example uses them if they're enabled

It should be noted though that render pass timestamp queries (setup in the renderpass) are separate from the one in the encoder and writes to buffers (via writeBuffer and copyBufferToBuffer and copyTextureToBuffer) all all out of band (not included in the time).

This means it's hard to get good timing across varied usages. It's one of the reasons timestamp queries are disabled by default so that the spec can be revised to possibly deal with these issues.