Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workload simulator: update timestamp query API usage #484

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 4 additions & 61 deletions sample/workloadSimulator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,6 @@ <h2><center>Web graphics workload simulator</center></h2>
let renderBundleAfterScissor;
let renderBundleNumDrawCalls;
let renderBundleInstances;
let querySet;
let queryBuffers = [];
let queryResolveBuffer;
let mapAsyncArray = new Float32Array(0);
let mapAsyncTargetBuffer;
let mapAsyncBufferSize = 0;
Expand Down Expand Up @@ -448,8 +445,6 @@ <h2><center>Web graphics workload simulator</center></h2>
let timeout = null;
let rafPending = 0;
let postMessagePending = 0;
let timeQueryResultsMs = [];
let lastTimeQueryTime = 0;

const animationDirection = [1, 1];
async function render(fromRaf, fromPostMessage) {
Expand Down Expand Up @@ -596,10 +591,7 @@ <h2><center>Web graphics workload simulator</center></h2>
features: Array.from(adapter.features || []).sort(),
limits: adapter.limits
}, 0, 2);
const useTimestampQueries = adapter.features.has('timestamp-query');
device = await adapter.requestDevice({
nonGuaranteedFeatures: useTimestampQueries ? ['timestamp-query'] : [],
});
device = await adapter.requestDevice({});
device.addEventListener('uncapturederror', (e)=>{
if (!errorMessage.textContent) errorMessage.textContent = 'Uncaptured error: ' + e.error.message;
});
Expand Down Expand Up @@ -709,13 +701,6 @@ <h2><center>Web graphics workload simulator</center></h2>
{ binding: 2, resource: texture.createView() },
],
});
if (useTimestampQueries && device.createQuerySet) {
querySet = device.createQuerySet({type: 'timestamp', count: 2});
queryResolveBuffer = device.createBuffer({
size: 16,
usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC,
});
}
} catch(e) {
if (!errorMessage.textContent) errorMessage.textContent = "Error initializing WebGPU: " + e;
return;
Expand All @@ -731,15 +716,10 @@ <h2><center>Web graphics workload simulator</center></h2>
}
try {
const canvasView = canvasContext.getCurrentTexture().createView();
const commandBuffers = [];

device.queue.writeBuffer(uniformBuffer, 0, new Float32Array([position[0] / size * 2 - 1, 1 - position[1] * 2 / size]));
let buffer = null;

// Start off by writing a timestamp for the beginning of the frame.
const timestampEncoder = device.createCommandEncoder();
if (querySet) timestampEncoder.writeTimestamp(querySet, 0);
device.queue.submit([timestampEncoder.finish()]);

// Upload data using createBuffer with mappedAtCreation.
if (bufferData.value > 0) {
buffer = device.createBuffer({ size: bufferDataArray.byteLength, usage: GPUBufferUsage.VERTEX, mappedAtCreation: true });
Expand Down Expand Up @@ -890,39 +870,7 @@ <h2><center>Web graphics workload simulator</center></h2>
}
passEncoder.end();
if (buffer) buffer.destroy();
let queryBuffer;
if (querySet) {
// Timestamp for the end of the frame.
commandEncoder.writeTimestamp(querySet, 1);
// We create a queue of staging buffers to hold the timestamps while we
// map them into CPU memory. If the queue is empty, create a new buffer.
if (queryBuffers.length == 0) {
const buffer = device.createBuffer({
size: 16,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
});
queryBuffers.push(buffer);
}
// Write the timestamps into a GPU side buffer and copy into the staging buffer.
queryBuffer = queryBuffers.shift();
commandEncoder.resolveQuerySet(querySet, 0, 2, queryResolveBuffer, 0);
commandEncoder.copyBufferToBuffer(queryResolveBuffer, 0, queryBuffer, 0, 16);
}
commandBuffers.push(commandEncoder.finish());
device.queue.submit(commandBuffers);
if (querySet) {
// Once the staging buffer is mapped we can finally calculate the frame duration
// and put the staging buffer back into the queue.
queryBuffer.mapAsync(GPUMapMode.READ).then(()=>{
let array = new BigInt64Array(queryBuffer.getMappedRange());
let nanoseconds = Number(array[1] - array[0]);
if (timeQueryResultsMs.length > 10) timeQueryResultsMs.shift();
timeQueryResultsMs.push(nanoseconds / 1000000);
lastTimeQueryTime = performance.now();
queryBuffer.unmap();
queryBuffers.push(queryBuffer);
});
}
device.queue.submit([commandEncoder.finish()]);
} catch(e) {
if (!errorMessage.textContent) errorMessage.textContent = "Error: " + e;
throw e;
Expand Down Expand Up @@ -1174,12 +1122,7 @@ <h2><center>Web graphics workload simulator</center></h2>
}

if (showFps.checked) {
if (performance.now() - lastTimeQueryTime < 1000 && timeQueryResultsMs.length) {
const averageTime = timeQueryResultsMs.reduce((x, y) => x + y) / timeQueryResultsMs.length;
fpsSpan.textContent = `${counters['render'].fps.toFixed()}, ${averageTime.toFixed(1)} ms GPU time`;
} else {
fpsSpan.textContent = counters['render'].fps.toFixed();
}
fpsSpan.textContent = counters['render'].fps.toFixed();
if (showStats.checked) {
let text = "";
for (let key in counters) {
Expand Down
Loading