diff --git a/webgpu/resources/js/rolling-average.js b/webgpu/resources/js/rolling-average.js new file mode 100644 index 00000000..a2f36c97 --- /dev/null +++ b/webgpu/resources/js/rolling-average.js @@ -0,0 +1,20 @@ +// See https://webgpufundamentals.org/webgpu/lessons/webgpu-timing.html +export default 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; + } +} diff --git a/webgpu/resources/js/timing-helper.js b/webgpu/resources/js/timing-helper.js index 306fb32f..aa645924 100644 --- a/webgpu/resources/js/timing-helper.js +++ b/webgpu/resources/js/timing-helper.js @@ -4,6 +4,7 @@ function assert(cond, msg = '') { } } +// See https://webgpufundamentals.org/webgpu/lessons/webgpu-timing.html export default class TimingHelper { #canTimestamp; #device;