forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackend.js
667 lines (550 loc) · 16.2 KB
/
Backend.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
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
let _vector2 = null;
let _color4 = null;
import Color4 from './Color4.js';
import { Vector2 } from '../../math/Vector2.js';
import { createCanvasElement, warnOnce } from '../../utils.js';
import { REVISION } from '../../constants.js';
/**
* Most of the rendering related logic is implemented in the
* {@link Renderer} module and related management components.
* Sometimes it is required though to execute commands which are
* specific to the current 3D backend (which is WebGPU or WebGL 2).
* This abstract base class defines an interface that encapsulates
* all backend-related logic. Derived classes for each backend must
* implement the interface.
*
* @abstract
* @private
*/
class Backend {
/**
* Constructs a new backend.
*
* @param {Object} parameters - An object holding parameters for the backend.
*/
constructor( parameters = {} ) {
/**
* The parameters of the backend.
*
* @type {Object}
*/
this.parameters = Object.assign( {}, parameters );
/**
* This weak map holds backend-specific data of objects
* like textures, attributes or render targets.
*
* @type {WeakMap}
*/
this.data = new WeakMap();
/**
* A reference to the renderer.
*
* @type {?Renderer}
* @default null
*/
this.renderer = null;
/**
* A reference to the canvas element the renderer is drawing to.
*
* @type {?(HTMLCanvasElement|OffscreenCanvas)}
* @default null
*/
this.domElement = null;
/**
* A reference to the timestamp query pool.
*
* @type {{render: ?TimestampQueryPool, compute: ?TimestampQueryPool}}
*/
this.timestampQueryPool = {
'render': null,
'compute': null
};
/**
* Whether to track timestamps with a Timestamp Query API or not.
*
* @type {boolean}
* @default false
*/
this.trackTimestamp = ( parameters.trackTimestamp === true );
}
/**
* Initializes the backend so it is ready for usage. Concrete backends
* are supposed to implement their rendering context creation and related
* operations in this method.
*
* @async
* @param {Renderer} renderer - The renderer.
* @return {Promise} A Promise that resolves when the backend has been initialized.
*/
async init( renderer ) {
this.renderer = renderer;
}
/**
* The coordinate system of the backend.
*
* @abstract
* @type {number}
* @readonly
*/
get coordinateSystem() {}
// render context
/**
* This method is executed at the beginning of a render call and
* can be used by the backend to prepare the state for upcoming
* draw calls.
*
* @abstract
* @param {RenderContext} renderContext - The render context.
*/
beginRender( /*renderContext*/ ) {}
/**
* This method is executed at the end of a render call and
* can be used by the backend to finalize work after draw
* calls.
*
* @abstract
* @param {RenderContext} renderContext - The render context.
*/
finishRender( /*renderContext*/ ) {}
/**
* This method is executed at the beginning of a compute call and
* can be used by the backend to prepare the state for upcoming
* compute tasks.
*
* @abstract
* @param {Node|Array<Node>} computeGroup - The compute node(s).
*/
beginCompute( /*computeGroup*/ ) {}
/**
* This method is executed at the end of a compute call and
* can be used by the backend to finalize work after compute
* tasks.
*
* @abstract
* @param {Node|Array<Node>} computeGroup - The compute node(s).
*/
finishCompute( /*computeGroup*/ ) {}
// render object
/**
* Executes a draw command for the given render object.
*
* @abstract
* @param {RenderObject} renderObject - The render object to draw.
* @param {Info} info - Holds a series of statistical information about the GPU memory and the rendering process.
*/
draw( /*renderObject, info*/ ) { }
// compute node
/**
* Executes a compute command for the given compute node.
*
* @abstract
* @param {Node|Array<Node>} computeGroup - The group of compute nodes of a compute call. Can be a single compute node.
* @param {Node} computeNode - The compute node.
* @param {Array<BindGroup>} bindings - The bindings.
* @param {ComputePipeline} computePipeline - The compute pipeline.
*/
compute( /*computeGroup, computeNode, computeBindings, computePipeline*/ ) { }
// program
/**
* Creates a shader program from the given programmable stage.
*
* @abstract
* @param {ProgrammableStage} program - The programmable stage.
*/
createProgram( /*program*/ ) { }
/**
* Destroys the shader program of the given programmable stage.
*
* @abstract
* @param {ProgrammableStage} program - The programmable stage.
*/
destroyProgram( /*program*/ ) { }
// bindings
/**
* Creates bindings from the given bind group definition.
*
* @abstract
* @param {BindGroup} bindGroup - The bind group.
* @param {Array<BindGroup>} bindings - Array of bind groups.
* @param {number} cacheIndex - The cache index.
* @param {number} version - The version.
*/
createBindings( /*bindGroup, bindings, cacheIndex, version*/ ) { }
/**
* Updates the given bind group definition.
*
* @abstract
* @param {BindGroup} bindGroup - The bind group.
* @param {Array<BindGroup>} bindings - Array of bind groups.
* @param {number} cacheIndex - The cache index.
* @param {number} version - The version.
*/
updateBindings( /*bindGroup, bindings, cacheIndex, version*/ ) { }
/**
* Updates a buffer binding.
*
* @abstract
* @param {Buffer} binding - The buffer binding to update.
*/
updateBinding( /*binding*/ ) { }
// pipeline
/**
* Creates a render pipeline for the given render object.
*
* @abstract
* @param {RenderObject} renderObject - The render object.
* @param {Array<Promise>} promises - An array of compilation promises which are used in `compileAsync()`.
*/
createRenderPipeline( /*renderObject, promises*/ ) { }
/**
* Creates a compute pipeline for the given compute node.
*
* @abstract
* @param {ComputePipeline} computePipeline - The compute pipeline.
* @param {Array<BindGroup>} bindings - The bindings.
*/
createComputePipeline( /*computePipeline, bindings*/ ) { }
// cache key
/**
* Returns `true` if the render pipeline requires an update.
*
* @abstract
* @param {RenderObject} renderObject - The render object.
* @return {boolean} Whether the render pipeline requires an update or not.
*/
needsRenderUpdate( /*renderObject*/ ) { }
/**
* Returns a cache key that is used to identify render pipelines.
*
* @abstract
* @param {RenderObject} renderObject - The render object.
* @return {string} The cache key.
*/
getRenderCacheKey( /*renderObject*/ ) { }
// node builder
/**
* Returns a node builder for the given render object.
*
* @abstract
* @param {RenderObject} renderObject - The render object.
* @param {Renderer} renderer - The renderer.
* @return {NodeBuilder} The node builder.
*/
createNodeBuilder( /*renderObject, renderer*/ ) { }
// textures
/**
* Creates a GPU sampler for the given texture.
*
* @abstract
* @param {Texture} texture - The texture to create the sampler for.
*/
createSampler( /*texture*/ ) { }
/**
* Destroys the GPU sampler for the given texture.
*
* @abstract
* @param {Texture} texture - The texture to destroy the sampler for.
*/
destroySampler( /*texture*/ ) {}
/**
* Creates a default texture for the given texture that can be used
* as a placeholder until the actual texture is ready for usage.
*
* @abstract
* @param {Texture} texture - The texture to create a default texture for.
*/
createDefaultTexture( /*texture*/ ) { }
/**
* Defines a texture on the GPU for the given texture object.
*
* @abstract
* @param {Texture} texture - The texture.
* @param {Object} [options={}] - Optional configuration parameter.
*/
createTexture( /*texture, options={}*/ ) { }
/**
* Uploads the updated texture data to the GPU.
*
* @abstract
* @param {Texture} texture - The texture.
* @param {Object} [options={}] - Optional configuration parameter.
*/
updateTexture( /*texture, options = {}*/ ) { }
/**
* Generates mipmaps for the given texture.
*
* @abstract
* @param {Texture} texture - The texture.
*/
generateMipmaps( /*texture*/ ) { }
/**
* Destroys the GPU data for the given texture object.
*
* @abstract
* @param {Texture} texture - The texture.
*/
destroyTexture( /*texture*/ ) { }
/**
* Returns texture data as a typed array.
*
* @abstract
* @async
* @param {Texture} texture - The texture to copy.
* @param {number} x - The x coordinate of the copy origin.
* @param {number} y - The y coordinate of the copy origin.
* @param {number} width - The width of the copy.
* @param {number} height - The height of the copy.
* @param {number} faceIndex - The face index.
* @return {Promise<TypedArray>} A Promise that resolves with a typed array when the copy operation has finished.
*/
async copyTextureToBuffer( /*texture, x, y, width, height, faceIndex*/ ) {}
/**
* Copies data of the given source texture to the given destination texture.
*
* @abstract
* @param {Texture} srcTexture - The source texture.
* @param {Texture} dstTexture - The destination texture.
* @param {?(Box3|Box2)} [srcRegion=null] - The region of the source texture to copy.
* @param {?(Vector2|Vector3)} [dstPosition=null] - The destination position of the copy.
* @param {number} [srcLevel=0] - The source mip level to copy from.
* @param {number} [dstLevel=0] - The destination mip level to copy to.
*/
copyTextureToTexture( /*srcTexture, dstTexture, srcRegion = null, dstPosition = null, srcLevel = 0, dstLevel = 0*/ ) {}
/**
* Copies the current bound framebuffer to the given texture.
*
* @abstract
* @param {Texture} texture - The destination texture.
* @param {RenderContext} renderContext - The render context.
* @param {Vector4} rectangle - A four dimensional vector defining the origin and dimension of the copy.
*/
copyFramebufferToTexture( /*texture, renderContext, rectangle*/ ) {}
// attributes
/**
* Creates the GPU buffer of a shader attribute.
*
* @abstract
* @param {BufferAttribute} attribute - The buffer attribute.
*/
createAttribute( /*attribute*/ ) { }
/**
* Creates the GPU buffer of an indexed shader attribute.
*
* @abstract
* @param {BufferAttribute} attribute - The indexed buffer attribute.
*/
createIndexAttribute( /*attribute*/ ) { }
/**
* Creates the GPU buffer of a storage attribute.
*
* @abstract
* @param {BufferAttribute} attribute - The buffer attribute.
*/
createStorageAttribute( /*attribute*/ ) { }
/**
* Updates the GPU buffer of a shader attribute.
*
* @abstract
* @param {BufferAttribute} attribute - The buffer attribute to update.
*/
updateAttribute( /*attribute*/ ) { }
/**
* Destroys the GPU buffer of a shader attribute.
*
* @abstract
* @param {BufferAttribute} attribute - The buffer attribute to destroy.
*/
destroyAttribute( /*attribute*/ ) { }
// canvas
/**
* Returns the backend's rendering context.
*
* @abstract
* @return {Object} The rendering context.
*/
getContext() { }
/**
* Backends can use this method if they have to run
* logic when the renderer gets resized.
*
* @abstract
*/
updateSize() { }
/**
* Updates the viewport with the values from the given render context.
*
* @abstract
* @param {RenderContext} renderContext - The render context.
*/
updateViewport( /*renderContext*/ ) {}
// utils
/**
* Returns `true` if the given 3D object is fully occluded by other
* 3D objects in the scene. Backends must implement this method by using
* a Occlusion Query API.
*
* @abstract
* @param {RenderContext} renderContext - The render context.
* @param {Object3D} object - The 3D object to test.
* @return {boolean} Whether the 3D object is fully occluded or not.
*/
isOccluded( /*renderContext, object*/ ) {}
/**
* Resolves the time stamp for the given render context and type.
*
* @async
* @abstract
* @param {string} [type='render'] - The type of the time stamp.
* @return {Promise<number>} A Promise that resolves with the time stamp.
*/
async resolveTimestampsAsync( type = 'render' ) {
if ( ! this.trackTimestamp ) {
warnOnce( 'WebGPURenderer: Timestamp tracking is disabled.' );
return;
}
const queryPool = this.timestampQueryPool[ type ];
if ( ! queryPool ) {
warnOnce( `WebGPURenderer: No timestamp query pool for type '${type}' found.` );
return;
}
const duration = await queryPool.resolveQueriesAsync();
this.renderer.info[ type ].timestamp = duration;
return duration;
}
/**
* Can be used to synchronize CPU operations with GPU tasks. So when this method is called,
* the CPU waits for the GPU to complete its operation (e.g. a compute task).
*
* @async
* @abstract
* @return {Promise} A Promise that resolves when synchronization has been finished.
*/
async waitForGPU() {}
/**
* This method performs a readback operation by moving buffer data from
* a storage buffer attribute from the GPU to the CPU.
*
* @async
* @param {StorageBufferAttribute} attribute - The storage buffer attribute.
* @return {Promise<ArrayBuffer>} A promise that resolves with the buffer data when the data are ready.
*/
async getArrayBufferAsync( /* attribute */ ) {}
/**
* Checks if the given feature is supported by the backend.
*
* @async
* @abstract
* @param {string} name - The feature's name.
* @return {Promise<boolean>} A Promise that resolves with a bool that indicates whether the feature is supported or not.
*/
async hasFeatureAsync( /*name*/ ) { }
/**
* Checks if the given feature is supported by the backend.
*
* @abstract
* @param {string} name - The feature's name.
* @return {boolean} Whether the feature is supported or not.
*/
hasFeature( /*name*/ ) {}
/**
* Returns the maximum anisotropy texture filtering value.
*
* @abstract
* @return {number} The maximum anisotropy texture filtering value.
*/
getMaxAnisotropy() {}
/**
* Returns the drawing buffer size.
*
* @return {Vector2} The drawing buffer size.
*/
getDrawingBufferSize() {
_vector2 = _vector2 || new Vector2();
return this.renderer.getDrawingBufferSize( _vector2 );
}
/**
* Defines the scissor test.
*
* @abstract
* @param {boolean} boolean - Whether the scissor test should be enabled or not.
*/
setScissorTest( /*boolean*/ ) { }
/**
* Returns the clear color and alpha into a single
* color object.
*
* @return {Color4} The clear color.
*/
getClearColor() {
const renderer = this.renderer;
_color4 = _color4 || new Color4();
renderer.getClearColor( _color4 );
_color4.getRGB( _color4 );
return _color4;
}
/**
* Returns the DOM element. If no DOM element exists, the backend
* creates a new one.
*
* @return {HTMLCanvasElement} The DOM element.
*/
getDomElement() {
let domElement = this.domElement;
if ( domElement === null ) {
domElement = ( this.parameters.canvas !== undefined ) ? this.parameters.canvas : createCanvasElement();
// OffscreenCanvas does not have setAttribute, see #22811
if ( 'setAttribute' in domElement ) domElement.setAttribute( 'data-engine', `three.js r${REVISION} webgpu` );
this.domElement = domElement;
}
return domElement;
}
/**
* Sets a dictionary for the given object into the
* internal data structure.
*
* @param {Object} object - The object.
* @param {Object} value - The dictionary to set.
*/
set( object, value ) {
this.data.set( object, value );
}
/**
* Returns the dictionary for the given object.
*
* @param {Object} object - The object.
* @return {Object} The object's dictionary.
*/
get( object ) {
let map = this.data.get( object );
if ( map === undefined ) {
map = {};
this.data.set( object, map );
}
return map;
}
/**
* Checks if the given object has a dictionary
* with data defined.
*
* @param {Object} object - The object.
* @return {boolean} Whether a dictionary for the given object as been defined or not.
*/
has( object ) {
return this.data.has( object );
}
/**
* Deletes an object from the internal data structure.
*
* @param {Object} object - The object to delete.
*/
delete( object ) {
this.data.delete( object );
}
/**
* Frees internal resources.
*
* @abstract
*/
dispose() { }
}
export default Backend;