-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimitive.mjs
476 lines (450 loc) · 15.1 KB
/
primitive.mjs
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import { Buffer } from "./buffer.mjs";
import { TestInputBuffer, TestOutputBuffer } from "./testbuffer.mjs";
import { TimingHelper } from "./webgpufundamentals-timing.mjs";
import { wgslFunctions } from "./wgslFunctions.mjs";
export class BasePrimitive {
static pipelineLayoutsCache = new Map();
static __timingHelper; // initialized to undefined
static fnDeclarations = wgslFunctions;
constructor(args) {
// expect that args are:
// { device: device, // REQUIRED
// label: label,
// tuning: { param1: val1, param2: val2 }, // TUNABLE params
// someConfigurationSetting: thatSetting,
// uniforms: uniformbuffer0,
// inputBuffer0: inputbuffer0,
// inputBuffer1: inputbuffer1,
// outputs: outputbuffer0,
// }
if (this.constructor === BasePrimitive) {
throw new Error(
"Cannot instantiate abstract class BasePrimitive directly."
);
}
/* set label first */
if (!("label" in args)) {
this.label = this.constructor.name;
}
/* required arguments, and handled next */
for (const requiredField of ["device"]) {
if (!(requiredField in args)) {
throw new Error(
`Primitive ${this.label} requires a "${requiredField}" argument.`
);
}
this[requiredField] = args[requiredField];
}
// now let's walk through all the args
for (const [field, value] of Object.entries(args)) {
switch (field) {
case "device":
/* do nothing, handled above */
break;
default:
/* copy it into the primitive */
this[field] = value;
break;
}
}
this.__buffers = {}; // this is essentially private
}
/** registerBuffer associates a name with a buffer and
* stores the buffer in the list of buffers
* Modes of operation:
* - registerBuffer(String name) means
* "associate the buffer this[name] with name"
* Meant to register named buffers (known to the primitive)
* - registerBuffer(Buffer b) means
* "associate the buffer b with b.label"
* - registerBuffer(Object o) means
* "associate a new Buffer(o) with o.label"
*
*/
registerBuffer(bufferObj) {
switch (typeof bufferObj) {
case "string":
this.__buffers[bufferObj] = new Buffer({
label: bufferObj,
buffer: this[bufferObj],
});
break;
default:
switch (bufferObj.constructor.name) {
case "Buffer":
/* already created the buffer, don't remake it */
this.__buffers[bufferObj.label] = bufferObj;
break;
default:
this.__buffers[bufferObj.label] = new Buffer(bufferObj);
break;
}
}
}
getBuffer(label) {
return this.__buffers[label];
}
getDispatchGeometry() {
/* call this from a subclass instead */
throw new Error(
"Cannot call getDispatchGeometry() from abstract class BasePrimitive."
);
}
getSimpleDispatchGeometry() {
// todo this is too simple
let dispatchGeometry = [this.workgroupCount, 1];
while (
dispatchGeometry[0] > this.device.limits.maxComputeWorkgroupsPerDimension
) {
/* too big */
dispatchGeometry[0] = Math.ceil(dispatchGeometry[0] / 2);
dispatchGeometry[1] *= 2;
}
return dispatchGeometry;
}
getGPUBufferFromBinding(binding) {
/**
* Input is some sort of buffer object. Currently recognized:
* - GPUBuffer
* - *TestBuffer
* Returns a GPUBuffer
*/
let gpuBuffer;
switch (binding.constructor) {
case GPUBuffer:
gpuBuffer = binding;
break;
case TestInputBuffer: // fallthrough deliberate
case TestOutputBuffer:
gpuBuffer = binding.gpuBuffer;
break;
default:
console.error(
`Primitive:getGPUBufferFromBinding: Unknown datatype for buffer: ${typeof binding}`
);
break;
}
return gpuBuffer;
}
getCPUBufferFromBinding(binding) {
/**
* Input is some sort of buffer object. Currently recognized:
* - TypedArray
* - *TestBuffer
* Returns a TypedArray
*/
let cpuBuffer;
switch (binding.constructor) {
case TestInputBuffer: // fallthrough deliberate
case TestOutputBuffer:
cpuBuffer = binding.cpuBuffer;
break;
case Uint32Array:
case Int32Array:
case Float32Array:
cpuBuffer = binding;
break;
default:
console.error(
`Primitive:getCPUBufferFromBinding: Unknown datatype for buffer: ${typeof binding}`
);
break;
}
return cpuBuffer;
}
async execute(args = {}) {
/** loop through each of the actions listed in this.compute(),
* instantiating whatever WebGPU constructs are necessary to run them
*
* TODO: memoize these constructs
*/
if (args.encoder && args.enableCPUTiming) {
console.warn(
"Primitive:execute: cannot pass in an encoder AND\nenable CPU timing, CPU timing will be disabled"
);
}
/* set up defaults */
if (!("trials" in args)) {
args.trials = 1;
}
/* do we need to register any new buffers specified in execute? */
for (const knownBuffer of this.knownBuffers) {
if (knownBuffer in args) {
this.registerBuffer({
label: knownBuffer,
buffer: args[knownBuffer],
});
}
}
/* begin timestamp prep - count kernels, allocate 2 timestamps/kernel */
let kernelCount = 0; // how many kernels are there total?
if (args.enableGPUTiming) {
for (const action of this.compute()) {
if (action.constructor == Kernel && action.enabled()) {
kernelCount++;
}
}
if (BasePrimitive.__timingHelper) {
if (BasePrimitive.__timingHelper.numKernels === kernelCount) {
/* do nothing - we can reuse __timingHelper without change */
} else {
/* keep the same instance, but reset it */
/* this ensures timing buffers are destroyed */
BasePrimitive.__timingHelper.destroy();
BasePrimitive.__timingHelper.reset(kernelCount);
}
} else {
/* there's no timing helper */
BasePrimitive.__timingHelper = new TimingHelper(
this.device,
kernelCount
);
}
}
/* if we passed in an encoder, use that */
const encoder =
args.encoder ??
this.device.createCommandEncoder({
label: `${this.label} primitive encoder`,
});
for (const action of this.compute()) {
switch (action.constructor) {
case Kernel: {
if (!action.enabled()) {
/* don't run this kernel at all */
break;
}
/* action.kernel can be a string or a function */
let kernelString;
switch (typeof action.kernel) {
case "string":
kernelString = action.kernel;
break;
case "function":
/* have never used kernelArgs, but let's support it anyway */
kernelString = action.kernel(action.kernelArgs);
break;
default:
throw new Error(
"Primitive::Kernel: kernel must be a function or a string"
);
}
if (action.debugPrintKernel) {
console.log(kernelString);
}
const computeModule = this.device.createShaderModule({
label: `module: ${this.label}`,
code: kernelString,
});
// build up bindGroupLayouts and pipelineLayout
let pipelineLayout;
if (action.bufferTypes in BasePrimitive.pipelineLayoutsCache) {
/* cached, use it */
pipelineLayout =
BasePrimitive.pipelineLayoutsCache[action.bufferTypes];
} else {
/* first build up bindGroupLayouts, then create a pipeline layout */
const bindGroupLayouts = [];
for (const bufferTypesGroup of action.bufferTypes) {
/* could also cache bind groups */
const entries = [];
bufferTypesGroup.forEach((element, index) => {
if (element !== "") {
// not sure if this is right comparison for an empty elt?
entries.push({
binding: index,
visibility: GPUShaderStage.COMPUTE,
buffer: { type: element },
});
}
});
const bindGroupLayout = this.device.createBindGroupLayout({
entries,
});
bindGroupLayouts.push(bindGroupLayout);
}
pipelineLayout = this.device.createPipelineLayout({
bindGroupLayouts,
});
/* and cache it */
BasePrimitive.pipelineLayoutsCache[action.bufferTypes] =
pipelineLayout;
}
pipelineLayout.label = `${this.label} compute pipeline`;
const kernelPipeline = this.device.createComputePipeline({
label: `${this.label} compute pipeline`,
layout: pipelineLayout,
compute: {
module: computeModule,
},
});
if (action.bindings.size > 1) {
console.error(
"Primitive::execute::Kernel currently only supports one bind group",
action.bindings
);
}
const kernelBindGroup = this.device.createBindGroup({
label: `bindGroup for ${this.label} kernel`,
layout: kernelPipeline.getBindGroupLayout(0),
/* the [0] below is because we only support 1 bind group */
entries: action.bindings[0].map((element, index) => ({
binding: index,
resource: this.__buffers[element].buffer,
})),
});
const kernelDescriptor = {
label: `${this.label} compute pass`,
};
const kernelPass = args.enableGPUTiming
? BasePrimitive.__timingHelper.beginComputePass(
encoder,
kernelDescriptor
)
: encoder.beginComputePass(kernelDescriptor);
kernelPass.setPipeline(kernelPipeline);
kernelPass.setBindGroup(0, kernelBindGroup);
/* For binding geometry:
* Look in kernel first, then to primitive if nothing in kernel
* There was some binding wonkiness with using ?? to pick the gDG call
* so that's why there's an if statement instead
*
* TODO: dispatchGeometry should be able to be an array or number, not
* just a function
* */
let dispatchGeometry;
if (action.getDispatchGeometry) {
dispatchGeometry = action.getDispatchGeometry();
} else {
dispatchGeometry = this.getDispatchGeometry();
}
/* trials might be >1 so make sure additional runs are idempotent */
for (let trial = 0; trial < (args.trials ?? 1); trial++) {
kernelPass.dispatchWorkgroups(...dispatchGeometry);
}
console.info(`${this.label} | ${action.label}
size of bindings: ${action.bindings[0].map(
(e) => this.__buffers[e].buffer.buffer.size
)}
workgroupCount: ${this.workgroupCount}
workgroupSize: ${this.workgroupSize}
maxGSLWorkgroupCount: ${this.maxGSLWorkgroupCount}
dispatchGeometry: ${dispatchGeometry}`);
kernelPass.end();
break;
}
case InitializeMemoryBlock: {
let DatatypeArray;
switch (action.datatype) {
case "f32":
DatatypeArray = Float32Array;
break;
case "i32":
DatatypeArray = Int32Array;
break;
case "u32":
default:
DatatypeArray = Uint32Array;
break;
}
if (typeof action.buffer === "string") {
/** if we specify a buffer by a string,
* go find the actual buffer associated with that string.
* pick first one we find, in bindings order */
for (const buffer of this.buffers) {
if (buffer.label == action.buffer) {
action.buffer = buffer;
break;
}
}
if (typeof action.buffer === "string") {
/* we didn't find a buffer; the string didn't match any of them */
throw new Error(
`Primitive::InitializeMemoryBlock: Could not find buffer named ${action.buffer}.`
);
}
}
/* initialize entire CPU-side array to action.value ... */
const initBlock = DatatypeArray.from(
{ length: action.buffer.size / DatatypeArray.BYTES_PER_ELEMENT },
() => action.value
);
/* ... then write it into the buffer */
this.device.queue.writeBuffer(
this.getGPUBufferFromBinding(action.buffer),
0 /* offset */,
initBlock
);
break;
}
case AllocateBuffer: {
const allocatedBuffer = this.device.createBuffer({
label: action.label,
size: action.size,
usage:
action.usage ??
/* default: read AND write */
GPUBufferUsage.STORAGE |
GPUBufferUsage.COPY_SRC |
GPUBufferUsage.COPY_DST,
});
this.registerBuffer({ label: action.label, buffer: allocatedBuffer });
break;
}
}
}
if (args.encoder) {
/* user passed in an encoder, return it, don't submit it */
return encoder;
} else {
/* TODO: Is this the right way to do timing? */
const commandBuffer = encoder.finish();
if (args?.enableCPUTiming) {
await this.device.queue.onSubmittedWorkDone();
this.cpuStartTime = performance.now();
}
this.device.queue.submit([commandBuffer]);
if (args?.enableCPUTiming) {
await this.device.queue.onSubmittedWorkDone();
this.cpuEndTime = performance.now();
}
}
}
async getTimingResult() {
const gpuTotalTimeNS = await BasePrimitive.__timingHelper.getResult();
const cpuTotalTimeNS = (this.cpuEndTime - this.cpuStartTime) * 1000000.0;
return { gpuTotalTimeNS, cpuTotalTimeNS };
}
}
export class Kernel {
constructor(args) {
this.label = "kernel"; // default
if (typeof args == "function") {
/* the function takes an optional arg object and returns the kernel string */
this.kernel = args;
} else {
/* more complicated objects */
/* one field should be "kernel" */
if (!args.kernel || typeof args.kernel !== "function") {
throw new Error(
"Kernel::constructor: Requires a 'kernel' field that is a function that returns the kernel string"
);
}
Object.assign(this, args);
}
}
enabled() {
/* default: enabled */
return this.enable ?? true;
}
}
export class InitializeMemoryBlock {
constructor(args) {
Object.assign(this, args);
}
}
export class AllocateBuffer {
constructor(args) {
Object.assign(this, args);
}
}