-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimestamp-helper.js
175 lines (145 loc) · 5.49 KB
/
timestamp-helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/** Number of timing samples to collect before computing the average */
const AVG_SAMPLE_COUNT = 30;
interface TimestampWrites {
querySet: GPUQuerySet,
beginningOfPassWriteIndex: number,
endOfPassWriteIndex: number,
};
interface TimestampResults {
[Key: string]: number;
}
/**
* A utility that makes it a bit easier to gather timings from WebGPU compute and render passes.
*/
export class TimestampHelper {
device: GPUDevice;
#timestampsSupported = false;
#timestampQuerySet: GPUQuerySet;
#timestampResolveBuffer: GPUBuffer;
#timestampReadbackBuffers: GPUBuffer[] = [];
#readbackBufferCount = 0;
#currentReadbackBuffer?: GPUBuffer = null;
#passTimings = new Map();
#maxPassCount = 0;
#nextQueryIndex = 0;
#queriesUsed = new Map();
#averages: TimestampResults = {};
constructor(device: GPUDevice, maxPassCount = 16) {
this.device = device;
this.#maxPassCount = maxPassCount;
this.#timestampsSupported = this.device.features.has('timestamp-query');
if (this.#timestampsSupported) {
this.#timestampQuerySet = this.device.createQuerySet({
label: 'Timestamp Helper',
type: 'timestamp',
count: this.#maxPassCount,
});
this.#timestampResolveBuffer = this.device.createBuffer({
size: BigUint64Array.BYTES_PER_ELEMENT * this.#maxPassCount,
usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC,
});
}
}
get timestampsSupported() {
return this.#timestampsSupported;
}
timestampWrites(name: string): TimestampWrites {
if (!this.#timestampsSupported) { return undefined; }
if (this.#currentReadbackBuffer) {
throw new Error('Must read back the previous resolve before new timestamps can be added.');
}
if (this.#nextQueryIndex >= this.#maxPassCount * 2) {
throw new Error('Exceeded the number of passes that can be queried in a single resolve.');
}
const timestampWrites = {
querySet: this.#timestampQuerySet,
beginningOfPassWriteIndex: this.#nextQueryIndex,
endOfPassWriteIndex: this.#nextQueryIndex + 1
};
this.#queriesUsed.set(name, {
begin: timestampWrites.beginningOfPassWriteIndex,
end: timestampWrites.endOfPassWriteIndex,
});
this.#nextQueryIndex += 2;
return timestampWrites;
}
resolve(commandEncoder: GPUCommandEncoder) {
if (!this.#timestampsSupported) { return; }
if (this.#currentReadbackBuffer) {
throw new Error('Must read back the previous resolve before resolve can be called again.');
}
if (this.#timestampReadbackBuffers.length > 0) {
this.#currentReadbackBuffer = this.#timestampReadbackBuffers.pop();
} else {
this.#currentReadbackBuffer = this.device.createBuffer({
label: `Timestamp Readback ${this.#readbackBufferCount}`,
size: this.#timestampResolveBuffer.size,
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
});
this.#readbackBufferCount++;
}
commandEncoder.resolveQuerySet(this.#timestampQuerySet, 0, this.#nextQueryIndex, this.#timestampResolveBuffer, 0);
commandEncoder.copyBufferToBuffer(this.#timestampResolveBuffer, 0, this.#currentReadbackBuffer, 0, this.#timestampResolveBuffer.size);
}
async read(): Promise<TimestampResults> {
if (!this.#currentReadbackBuffer) { return; }
let readbackBuffer = this.#currentReadbackBuffer;
let queries = new Map(this.#queriesUsed);
this.#currentReadbackBuffer = null;
this.#queriesUsed.clear();
this.#nextQueryIndex = 0;
await readbackBuffer.mapAsync(GPUMapMode.READ);
const mappedArray = new BigUint64Array(readbackBuffer.getMappedRange());
const results: TimestampResults = {};
for (const [ name, query ] of queries.entries()) {
const passTime = Number(mappedArray[query.end] - mappedArray[query.begin]);
// Discard negative times
if (passTime >= 0) {
let passTimings = this.#passTimings.get(name);
if (!passTimings) {
passTimings = {
values: new Array(AVG_SAMPLE_COUNT),
index: 0,
};
this.#passTimings.set(name, passTimings);
}
// Storing pass timings in µs
let passTimeMicro = passTime / 1000;
passTimings.values[passTimings.index++ % AVG_SAMPLE_COUNT] = passTimeMicro;
if (passTimings.index % AVG_SAMPLE_COUNT == 0) {
// Update the average
let avg = 0;
for (const value of passTimings.values) {
avg += value;
}
this.#averages[name] = avg / AVG_SAMPLE_COUNT;
}
results[name] = passTimeMicro;
}
}
// Update any existing keys not in the queries to 0. This way if one of them
// isn't present in the readback it will zero out instead of appearing to
// stay steady at the last reading.
for (const name of this.#passTimings.keys()) {
if (!queries.has(name)) {
let passTimings = this.#passTimings.get(name);
passTimings.values[passTimings.index++ % AVG_SAMPLE_COUNT] = 0;
if (passTimings.index % AVG_SAMPLE_COUNT == 0) {
// Update the average
let avg = 0;
for (const value of passTimings.values) {
avg += value;
}
this.#averages[name] = avg / AVG_SAMPLE_COUNT;
}
}
}
readbackBuffer.unmap();
this.#timestampReadbackBuffers.push(readbackBuffer);
return results;
}
hasPendingResults(): boolean { return this.#nextQueryIndex != 0; }
get averages(): TimestampResults {
return this.#averages;
}
}