-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBindGroup.ts
533 lines (465 loc) · 17.8 KB
/
BindGroup.ts
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
import { isRenderer, Renderer } from '../renderers/utils'
import { generateUUID, toKebabCase } from '../../utils/utils'
import { WritableBufferBinding, WritableBufferBindingParams } from '../bindings/WritableBufferBinding'
import { BufferBinding } from '../bindings/BufferBinding'
import {
AllowedBindGroups,
BindGroupBindingElement,
BindGroupBufferBindingElement,
BindGroupEntries,
BindGroupParams,
ReadOnlyInputBindings,
} from '../../types/BindGroups'
import { GPUCurtains } from '../../curtains/GPUCurtains'
import { TextureBindGroupParams } from './TextureBindGroup'
import { BindingType } from '../bindings/Binding'
/**
* Used to handle all inputs data sent to the GPU.<br>
* In WebGPU, data (buffers, textures or samplers, called bindings) are organised by bind groups, containing those bindings.
*
* ## Bindings
*
* A {@link BindGroup} is responsible for creating each {@link BufferBinding} {@link GPUBuffer} and then the {@link GPUBindGroup} and {@link GPUBindGroupLayout} that are used to create {@link GPUComputePipeline} or {@link GPURenderPipeline}.<br>
* Those are generally automatically created by the {@link core/materials/Material.Material | Material} using this {@link BindGroup}. If you need to manually create them, you will have to call its {@link BindGroup#createBindGroup | `createBindGroup()` method}
*
* ### Samplers and textures
*
* A {@link BindGroup} is best suited to handle {@link GPUBuffer} only bindings. If you need to handle {@link GPUSampler}, a {@link GPUTexture} or a {@link GPUExternalTexture}, you should use a {@link core/bindGroups/TextureBindGroup.TextureBindGroup | TextureBindGroup} instead.
*
* ### Updating a GPUBindGroup or GPUBindGroupLayout
*
* Each time one of the {@link https://developer.mozilla.org/en-US/docs/Web/API/GPUDevice/createBindGroup#resource | binding resource} changes, its {@link BindGroup#bindGroup | bindGroup} will be recreated (usually, when a {@link GPUTexture} is uploaded).<br>
* Each time one of the {@link https://developer.mozilla.org/en-US/docs/Web/API/GPUDevice/createBindGroupLayout#resource_layout_objects | binding resource layout} changes, its {@link BindGroup#bindGroupLayout | bindGroupLayout} and {@link BindGroup#bindGroup | bindGroup} will be recreated, and the {@link GPUComputePipeline} or {@link GPURenderPipeline} will be recreated as well.
*
* @example
* ```javascript
* // set our main GPUCurtains instance
* const gpuCurtains = new GPUCurtains({
* container: '#canvas' // selector of our WebGPU canvas container
* })
*
* // set the GPU device
* // note this is asynchronous
* await gpuCurtains.setDevice()
*
* const bindGroup = new BindGroup(gpuCurtains, {
* label: 'My bind group',
* uniforms: {
* params: {
* struct: {
* opacity: {
* type: 'f32',
* value: 1,
* },
* mousePosition: {
* type: 'vec2f',
* value: new Vec2(),
* },
* },
* },
* },
* })
*
* // create the GPU buffer, bindGroupLayout and bindGroup
* bindGroup.createBindGroup()
* ```
*/
export class BindGroup {
/** The type of the {@link BindGroup} */
type: string
/** The universal unique id of the {@link BindGroup} */
uuid: string
/** The {@link Renderer} used */
renderer: Renderer
/** Options used to create this {@link BindGroup} */
options: TextureBindGroupParams
/** Index of this {@link BindGroup}, used to link struct in the shaders */
index: number
/** List of {@link BindGroupBindingElement | bindings} (buffers, texture, etc.) handled by this {@link BindGroup} */
bindings: BindGroupBindingElement[]
/** Our {@link BindGroup} {@link BindGroupEntries | entries} objects */
entries: BindGroupEntries
/** Our {@link BindGroup}{@link GPUBindGroupLayout} */
bindGroupLayout: null | GPUBindGroupLayout
/** Our {@link BindGroup} {@link GPUBindGroup} */
bindGroup: null | GPUBindGroup
/** Flag indicating whether we need to flush and recreate the pipeline using this {@link BindGroup} s*/
needsPipelineFlush: boolean
/**
* BindGroup constructor
* @param renderer - a {@link Renderer} class object or a {@link GPUCurtains} class object
* @param parameters - {@link BindGroupParams | parameters} used to create our {@link BindGroup}
*/
constructor(
renderer: Renderer | GPUCurtains,
{ label = 'BindGroup', index = 0, bindings = [], uniforms, storages }: BindGroupParams = {}
) {
this.type = 'BindGroup'
// we could pass our curtains object OR our curtains renderer object
renderer = (renderer && (renderer as GPUCurtains).renderer) || (renderer as Renderer)
isRenderer(renderer, this.type)
this.renderer = renderer
this.options = {
label,
index,
bindings,
...(uniforms && { uniforms }),
...(storages && { storages }),
}
this.index = index
this.uuid = generateUUID()
this.bindings = []
bindings.length && this.addBindings(bindings)
if (this.options.uniforms || this.options.storages) this.setInputBindings()
this.resetEntries()
this.bindGroupLayout = null
this.bindGroup = null
// if we ever update our bind group layout
// we will have to recreate the whole pipeline again
this.needsPipelineFlush = false
this.renderer.addBindGroup(this)
}
/**
* Sets our {@link BindGroup#index | bind group index}
* @param index - {@link BindGroup#index | bind group index} to set
*/
setIndex(index: number) {
this.index = index
}
/**
* Adds an array of already created {@link bindings} (buffers, texture, etc.) to the {@link bindings} array
* @param bindings - {@link bindings} to add
*/
addBindings(bindings: BindGroupBindingElement[] = []) {
this.bindings = [...this.bindings, ...bindings]
}
/**
* Adds an already created {@link bindings} (buffers, texture, etc.) to the {@link bindings} array
* @param binding - binding to add
*/
addBinding(binding: BindGroupBindingElement) {
this.bindings.push(binding)
}
/**
* Creates Bindings based on a list of inputs
* @param bindingType - {@link core/bindings/Binding.Binding#bindingType | binding type}
* @param inputs - {@link ReadOnlyInputBindings | inputs (uniform or storage)} that will be used to create the binding
* @returns - a {@link bindings} array
*/
createInputBindings(
bindingType: BindingType = 'uniform',
inputs: ReadOnlyInputBindings = {}
): BindGroupBindingElement[] {
return [
...Object.keys(inputs).map((inputKey) => {
const binding = inputs[inputKey] as WritableBufferBindingParams
const bindingParams: WritableBufferBindingParams = {
label: toKebabCase(binding.label || inputKey),
name: inputKey,
bindingType,
useStruct: true, // by default
visibility: binding.access === 'read_write' ? 'compute' : binding.visibility,
access: binding.access ?? 'read', // read by default
struct: binding.struct,
...(binding.shouldCopyResult !== undefined && { shouldCopyResult: binding.shouldCopyResult }),
}
const BufferBindingConstructor = bindingParams.access === 'read_write' ? WritableBufferBinding : BufferBinding
return binding.useStruct !== false
? new BufferBindingConstructor(bindingParams)
: Object.keys(binding.struct).map((bindingKey) => {
bindingParams.label = toKebabCase(binding.label ? binding.label + bindingKey : inputKey + bindingKey)
bindingParams.name = inputKey + bindingKey
bindingParams.useStruct = false
bindingParams.struct = { [bindingKey]: binding.struct[bindingKey] }
return new BufferBindingConstructor(bindingParams)
})
}),
].flat()
}
/**
* Create and adds {@link bindings} based on inputs provided upon creation
*/
setInputBindings() {
this.addBindings([
...this.createInputBindings('uniform', this.options.uniforms),
...this.createInputBindings('storage', this.options.storages),
])
}
/**
* Get whether the GPU bind group is ready to be created
* It can be created if it has {@link bindings} and has not been created yet
* @readonly
*/
get shouldCreateBindGroup(): boolean {
return !this.bindGroup && !!this.bindings.length
}
/**
* Reset our {@link BindGroup} {@link entries}
*/
resetEntries() {
this.entries = {
bindGroupLayout: [],
bindGroup: [],
}
}
/**
* Create the GPU buffers, {@link bindings}, {@link entries}, {@link bindGroupLayout} and {@link bindGroup}
*/
createBindGroup() {
this.fillEntries()
this.setBindGroupLayout()
this.setBindGroup()
}
/**
* Reset the {@link BindGroup#entries.bindGroup | bindGroup entries}, recreates them and then recreate the {@link BindGroup#bindGroup | GPU bind group}
*/
resetBindGroup() {
this.entries.bindGroup = []
this.bindings.forEach((binding) => {
this.entries.bindGroup.push({
binding: this.entries.bindGroup.length,
resource: binding.resource,
})
})
this.setBindGroup()
}
/**
* Reset the {@link BindGroup#entries.bindGroupLayout | bindGroupLayout entries}, recreates them and then recreate the {@link BindGroup#bindGroupLayout | GPU bind group layout}
*/
resetBindGroupLayout() {
this.entries.bindGroupLayout = []
this.bindings.forEach((binding) => {
this.entries.bindGroupLayout.push({
binding: this.entries.bindGroupLayout.length,
...binding.resourceLayout,
visibility: binding.visibility,
})
})
this.setBindGroupLayout()
}
/**
* Called when the {@link core/renderers/GPUDeviceManager.GPUDeviceManager#device | device} has been lost to prepare everything for restoration
*/
loseContext() {
this.resetEntries()
this.bufferBindings.forEach((binding) => {
binding.buffer = null
if ('resultBuffer' in binding) {
binding.resultBuffer = null
}
})
this.bindGroup = null
this.bindGroupLayout = null
this.needsPipelineFlush = true
}
/**
* Get all {@link BindGroup#bindings | bind group bindings} that handle a {@link GPUBuffer}
*/
get bufferBindings(): BindGroupBufferBindingElement[] {
return this.bindings.filter(
(binding) => binding instanceof BufferBinding || binding instanceof WritableBufferBinding
) as BindGroupBufferBindingElement[]
}
/**
* Creates binding GPUBuffer with correct params
* @param binding - the binding element
*/
createBindingBuffer(binding: BindGroupBufferBindingElement) {
// TODO user defined usage?
// [Kangz](https://github.com/Kangz) said:
// "In general though COPY_SRC/DST is free (at least in Dawn / Chrome because we add it all the time for our own purpose)."
binding.buffer = this.renderer.createBuffer({
label: this.options.label + ': ' + binding.bindingType + ' buffer from: ' + binding.label,
size: binding.arrayBuffer.byteLength,
usage:
binding.bindingType === 'uniform'
? GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC | GPUBufferUsage.VERTEX
: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC | GPUBufferUsage.VERTEX,
})
if ('resultBuffer' in binding) {
binding.resultBuffer = this.renderer.createBuffer({
label: this.options.label + ': Result buffer from: ' + binding.label,
size: binding.arrayBuffer.byteLength,
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
})
}
}
/**
* Fill in our entries bindGroupLayout and bindGroup arrays with the correct binding resources.
* For buffer struct, create a GPUBuffer first if needed
*/
fillEntries() {
this.bindings.forEach((binding) => {
// if no visibility specified, just set it to the maximum default capabilities
if (!binding.visibility) {
binding.visibility = GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT | GPUShaderStage.COMPUTE
}
// if it's a buffer binding, create the GPUBuffer
if ('buffer' in binding && !binding.buffer) {
this.createBindingBuffer(binding)
}
// now that everything is ready, fill our entries
this.entries.bindGroupLayout.push({
binding: this.entries.bindGroupLayout.length,
...binding.resourceLayout,
visibility: binding.visibility,
})
this.entries.bindGroup.push({
binding: this.entries.bindGroup.length,
resource: binding.resource,
})
})
}
/**
* Get a bind group binding by name/key
* @param bindingName - the binding name or key
* @returns - the found binding, or null if not found
*/
getBindingByName(bindingName = ''): BindGroupBindingElement | null {
return this.bindings.find((binding) => binding.name === bindingName)
}
/**
* Create a GPUBindGroupLayout and set our {@link bindGroupLayout}
*/
setBindGroupLayout() {
this.bindGroupLayout = this.renderer.createBindGroupLayout({
label: this.options.label + ' layout',
entries: this.entries.bindGroupLayout,
})
}
/**
* Create a GPUBindGroup and set our {@link bindGroup}
*/
setBindGroup() {
this.bindGroup = this.renderer.createBindGroup({
label: this.options.label,
layout: this.bindGroupLayout,
entries: this.entries.bindGroup,
})
}
/**
* Check whether we should update (write) our {@link GPUBuffer} or not.
*/
updateBufferBindings() {
this.bufferBindings.forEach((binding, index) => {
// update binding elements
binding.update()
// now write to the GPUBuffer if needed
if (binding.shouldUpdate) {
// bufferOffset is always equals to 0 in our case
if (!binding.useStruct && binding.bufferElements.length > 1) {
// we're in a non struct buffer binding with multiple entries
// that should not happen but that way we're covered
this.renderer.queueWriteBuffer(binding.buffer, 0, binding.bufferElements[index].view)
} else {
this.renderer.queueWriteBuffer(binding.buffer, 0, binding.arrayBuffer)
}
}
// reset update flag
binding.shouldUpdate = false
})
}
/**
* Update the {@link BindGroup}, which means update its {@link BindGroup#bufferBindings | buffer bindings} and {@link BindGroup#resetBindGroup | reset it} if needed.
* Called at each render from the parentMesh {@link core/materials/Material.Material | material}
*/
update() {
this.updateBufferBindings()
const needBindGroupReset = this.bindings.some((binding) => binding.shouldResetBindGroup)
const needBindGroupLayoutReset = this.bindings.some((binding) => binding.shouldResetBindGroupLayout)
// since other bind groups might be using that binding
// wait for the end of the render loop to reset the bindings flags
if (needBindGroupReset || needBindGroupLayoutReset) {
this.renderer.onAfterCommandEncoderSubmission.add(
() => {
this.bindings.forEach((binding) => {
binding.shouldResetBindGroup = false
binding.shouldResetBindGroupLayout = false
})
},
{ once: true }
)
}
if (needBindGroupLayoutReset) {
this.resetBindGroupLayout()
// bind group layout has changed, we have to recreate the pipeline
this.needsPipelineFlush = true
}
if (needBindGroupReset) {
this.resetBindGroup()
}
}
/**
* Clones a {@link BindGroup} from a list of {@link bindings}
* Useful to create a new bind group with already created buffers, but swapped
* @param parameters - parameters to use for cloning
* @param parameters.bindings - our input {@link bindings}
* @param [parameters.keepLayout=false] - whether we should keep original {@link bindGroupLayout} or not
* @returns - the cloned {@link BindGroup}
*/
clone({
bindings = [],
keepLayout = false,
}: {
bindings?: BindGroupBindingElement[]
keepLayout?: boolean
} = {}): AllowedBindGroups {
const params = { ...this.options }
params.label += ' (copy)'
const bindGroupCopy = new (this.constructor as typeof BindGroup)(this.renderer, {
label: params.label,
})
bindGroupCopy.setIndex(this.index)
bindGroupCopy.options = params
const bindingsRef = bindings.length ? bindings : this.bindings
bindingsRef.forEach((binding, index) => {
bindGroupCopy.addBinding(binding)
// if it's a buffer binding without a GPUBuffer, create it now
if ('buffer' in binding && !binding.buffer) {
bindGroupCopy.createBindingBuffer(binding)
}
// if we should create a new bind group layout, fill it
if (!keepLayout) {
bindGroupCopy.entries.bindGroupLayout.push({
binding: bindGroupCopy.entries.bindGroupLayout.length,
...binding.resourceLayout,
visibility: binding.visibility,
})
}
bindGroupCopy.entries.bindGroup.push({
binding: bindGroupCopy.entries.bindGroup.length,
resource: binding.resource,
} as GPUBindGroupEntry)
})
// if we should copy the given bind group layout
if (keepLayout) {
bindGroupCopy.entries.bindGroupLayout = [...this.entries.bindGroupLayout]
}
bindGroupCopy.setBindGroupLayout()
bindGroupCopy.setBindGroup()
return bindGroupCopy
}
/**
* Destroy our {@link BindGroup}
* Most important is to destroy the GPUBuffers to free the memory
*/
destroy() {
this.renderer.removeBindGroup(this)
this.bufferBindings.forEach((binding) => {
if ('buffer' in binding) {
this.renderer.removeBuffer(binding.buffer)
binding.buffer?.destroy()
binding.buffer = null
}
if ('resultBuffer' in binding) {
this.renderer.removeBuffer(binding.resultBuffer)
binding.resultBuffer?.destroy()
binding.resultBuffer = null
}
})
this.bindings = []
this.bindGroupLayout = null
this.bindGroup = null
this.resetEntries()
}
}