forked from webgpu/webgpufundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
184 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
export class RollingAverage { | ||
#total = 0; | ||
#samples = []; | ||
#cursor = 0; | ||
#numSamples; | ||
constructor(numSamples = 30) { | ||
this.#numSamples = numSamples; | ||
} | ||
addSample(v) { | ||
if (!Number.isNaN(v) && Number.isFinite(v)) { | ||
this.#total += v - (this.#samples[this.#cursor] || 0); | ||
this.#samples[this.#cursor] = v; | ||
this.#cursor = (this.#cursor + 1) % this.#numSamples; | ||
} | ||
} | ||
get() { | ||
return this.#total / this.#samples.length; | ||
} | ||
} | ||
|
||
function assert(cond, msg = '') { | ||
if (!cond) { | ||
throw new Error(msg); | ||
} | ||
} | ||
|
||
export class TimingHelper { | ||
#canTimestamp; | ||
#device; | ||
#querySet; | ||
#resolveBuffer; | ||
#resultBuffer; | ||
#resultBuffers = []; | ||
// state can be 'free', 'need resolve', 'wait for result' | ||
#state = 'free'; | ||
|
||
constructor(device) { | ||
this.#device = device; | ||
this.#canTimestamp = device.features.has('timestamp-query'); | ||
if (this.#canTimestamp) { | ||
this.#querySet = device.createQuerySet({ | ||
type: 'timestamp', | ||
count: 2, | ||
}); | ||
this.#resolveBuffer = device.createBuffer({ | ||
size: this.#querySet.count * 8, | ||
usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC, | ||
}); | ||
} | ||
} | ||
|
||
#beginTimestampPass(encoder, fnName, descriptor) { | ||
if (this.#canTimestamp) { | ||
assert(this.#state === 'free', 'state not free'); | ||
this.#state = 'need resolve'; | ||
|
||
const pass = encoder[fnName]({ | ||
...descriptor, | ||
...{ | ||
timestampWrites: { | ||
querySet: this.#querySet, | ||
beginningOfPassWriteIndex: 0, | ||
endOfPassWriteIndex: 1, | ||
}, | ||
}, | ||
}); | ||
|
||
const resolve = () => this.#resolveTiming(encoder); | ||
pass.end = (function(origFn) { | ||
return function() { | ||
origFn.call(this); | ||
resolve(); | ||
}; | ||
})(pass.end); | ||
|
||
return pass; | ||
} else { | ||
return encoder[fnName](descriptor); | ||
} | ||
} | ||
|
||
beginRenderPass(encoder, descriptor = {}) { | ||
return this.#beginTimestampPass(encoder, 'beginRenderPass', descriptor); | ||
} | ||
|
||
beginComputePass(encoder, descriptor = {}) { | ||
return this.#beginTimestampPass(encoder, 'beginComputePass', descriptor); | ||
} | ||
|
||
#resolveTiming(encoder) { | ||
if (!this.#canTimestamp) { | ||
return; | ||
} | ||
assert(this.#state === 'need resolve', 'must call addTimestampToPass'); | ||
this.#state = 'wait for result'; | ||
|
||
this.#resultBuffer = this.#resultBuffers.pop() || this.#device.createBuffer({ | ||
size: this.#resolveBuffer.size, | ||
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ, | ||
}); | ||
|
||
encoder.resolveQuerySet(this.#querySet, 0, this.#querySet.count, this.#resolveBuffer, 0); | ||
encoder.copyBufferToBuffer(this.#resolveBuffer, 0, this.#resultBuffer, 0, this.#resultBuffer.size); | ||
} | ||
|
||
async getResult() { | ||
if (!this.#canTimestamp) { | ||
return 0; | ||
} | ||
assert(this.#state === 'wait for result', 'must call resolveTiming'); | ||
this.#state = 'free'; | ||
|
||
const resultBuffer = this.#resultBuffer; | ||
await resultBuffer.mapAsync(GPUMapMode.READ); | ||
const times = new BigInt64Array(resultBuffer.getMappedRange()); | ||
const duration = Number(times[1] - times[0]); | ||
resultBuffer.unmap(); | ||
this.#resultBuffers.push(resultBuffer); | ||
return duration; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters