-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
graphics-device.js
964 lines (833 loc) · 25.5 KB
/
graphics-device.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
import { Debug } from '../../core/debug.js';
import { EventHandler } from '../../core/event-handler.js';
import { platform } from '../../core/platform.js';
import { now } from '../../core/time.js';
import { Vec2 } from '../../core/math/vec2.js';
import { Tracing } from '../../core/tracing.js';
import { Color } from '../../core/math/color.js';
import { TRACEID_TEXTURES } from '../../core/constants.js';
import {
CULLFACE_BACK,
CLEARFLAG_COLOR, CLEARFLAG_DEPTH,
PRIMITIVE_POINTS, PRIMITIVE_TRIFAN, SEMANTIC_POSITION, TYPE_FLOAT32, PIXELFORMAT_111110F, PIXELFORMAT_RGBA16F, PIXELFORMAT_RGBA32F
} from './constants.js';
import { BlendState } from './blend-state.js';
import { DepthState } from './depth-state.js';
import { ScopeSpace } from './scope-space.js';
import { VertexBuffer } from './vertex-buffer.js';
import { VertexFormat } from './vertex-format.js';
import { StencilParameters } from './stencil-parameters.js';
/**
* The graphics device manages the underlying graphics context. It is responsible for submitting
* render state changes and graphics primitives to the hardware. A graphics device is tied to a
* specific canvas HTML element. It is valid to have more than one canvas element per page and
* create a new graphics device against each.
*
* @category Graphics
*/
class GraphicsDevice extends EventHandler {
/**
* Fired when the canvas is resized. The handler is passed the new width and height as number
* parameters.
*
* @event
* @example
* graphicsDevice.on('resizecanvas', (width, height) => {
* console.log(`The canvas was resized to ${width}x${height}`);
* });
*/
/**
* The canvas DOM element that provides the underlying WebGL context used by the graphics device.
*
* @type {HTMLCanvasElement}
* @readonly
*/
canvas;
/**
* The render target representing the main back-buffer.
*
* @type {import('./render-target.js').RenderTarget|null}
* @ignore
*/
backBuffer = null;
/**
* The dimensions of the back buffer.
*
* @ignore
*/
backBufferSize = new Vec2();
/**
* The pixel format of the back buffer. Typically PIXELFORMAT_RGBA8, PIXELFORMAT_BGRA8 or
* PIXELFORMAT_RGB8.
*
* @ignore
*/
backBufferFormat;
/**
* True if the back buffer should use anti-aliasing.
*
* @type {boolean}
*/
backBufferAntialias = false;
/**
* True if the deviceType is WebGPU
*
* @type {boolean}
* @readonly
*/
isWebGPU = false;
/**
* True if the deviceType is WebGL2
*
* @type {boolean}
* @readonly
*/
isWebGL2 = false;
/**
* The scope namespace for shader attributes and variables.
*
* @type {ScopeSpace}
* @readonly
*/
scope;
/**
* The maximum number of supported bones using uniform buffers.
*
* @type {number}
* @readonly
*/
boneLimit;
/**
* The maximum supported texture anisotropy setting.
*
* @type {number}
* @readonly
*/
maxAnisotropy;
/**
* The maximum supported dimension of a cube map.
*
* @type {number}
* @readonly
*/
maxCubeMapSize;
/**
* The maximum supported dimension of a texture.
*
* @type {number}
* @readonly
*/
maxTextureSize;
/**
* The maximum supported dimension of a 3D texture (any axis).
*
* @type {number}
* @readonly
*/
maxVolumeSize;
/**
* The maximum supported number of color buffers attached to a render target.
*
* @type {number}
* @readonly
*/
maxColorAttachments = 1;
/**
* The highest shader precision supported by this graphics device. Can be 'hiphp', 'mediump' or
* 'lowp'.
*
* @type {string}
* @readonly
*/
precision;
/**
* The number of hardware anti-aliasing samples used by the frame buffer.
*
* @readonly
* @type {number}
*/
samples;
/**
* True if the main framebuffer contains stencil attachment.
*
* @ignore
* @type {boolean}
*/
supportsStencil;
/**
* True if Multiple Render Targets feature is supported. This refers to the ability to render to
* multiple color textures with a single draw call.
*
* @readonly
* @type {boolean}
*/
supportsMrt = false;
/**
* True if the device supports volume textures.
*
* @readonly
* @type {boolean}
*/
supportsVolumeTextures = false;
/**
* True if the device supports compute shaders.
*
* @readonly
* @type {boolean}
*/
supportsCompute = false;
/**
* True if the device can read from StorageTexture in the compute shader. By default, the
* storage texture can be only used with the write operation.
* When a shader uses this feature, it's recommended to use a `requires` directive to signal the
* potential for non-portability at the top of the WGSL shader code:
* ```javascript
* requires readonly_and_readwrite_storage_textures;
* ```
*
* @readonly
* @type {boolean}
*/
supportsStorageTextureRead = false;
/**
* Currently active render target.
*
* @type {import('./render-target.js').RenderTarget|null}
* @ignore
*/
renderTarget = null;
/**
* Array of objects that need to be re-initialized after a context restore event
*
* @type {import('./shader.js').Shader[]}
* @ignore
*/
shaders = [];
/**
* An array of currently created textures.
*
* @type {import('./texture.js').Texture[]}
* @ignore
*/
textures = [];
/**
* A set of currently created render targets.
*
* @type {Set<import('./render-target.js').RenderTarget>}
* @ignore
*/
targets = new Set();
/**
* A version number that is incremented every frame. This is used to detect if some object were
* invalidated.
*
* @type {number}
* @ignore
*/
renderVersion = 0;
/**
* Index of the currently active render pass.
*
* @type {number}
* @ignore
*/
renderPassIndex;
/** @type {boolean} */
insideRenderPass = false;
/**
* True if hardware instancing is supported.
*
* @type {boolean}
* @readonly
*/
supportsInstancing;
/**
* True if the device supports uniform buffers.
*
* @type {boolean}
* @ignore
*/
supportsUniformBuffers = false;
/**
* True if 32-bit floating-point textures can be used as a frame buffer.
*
* @type {boolean}
* @readonly
*/
textureFloatRenderable;
/**
* True if 16-bit floating-point textures can be used as a frame buffer.
*
* @type {boolean}
* @readonly
*/
textureHalfFloatRenderable;
/**
* True if filtering can be applied when sampling float textures.
*
* @type {boolean}
* @readonly
*/
textureFloatFilterable = false;
/**
* True if filtering can be applied when sampling 16-bit float textures.
*
* @type {boolean}
* @readonly
*/
textureHalfFloatFilterable = false;
/**
* A vertex buffer representing a quad.
*
* @type {VertexBuffer}
* @ignore
*/
quadVertexBuffer;
/**
* An object representing current blend state
*
* @ignore
*/
blendState = new BlendState();
/**
* The current depth state.
*
* @ignore
*/
depthState = new DepthState();
/**
* True if stencil is enabled and stencilFront and stencilBack are used
*
* @ignore
*/
stencilEnabled = false;
/**
* The current front stencil parameters.
*
* @ignore
*/
stencilFront = new StencilParameters();
/**
* The current back stencil parameters.
*
* @ignore
*/
stencilBack = new StencilParameters();
/**
* The dynamic buffer manager.
*
* @type {import('./dynamic-buffers.js').DynamicBuffers}
* @ignore
*/
dynamicBuffers;
/**
* The GPU profiler.
*
* @type {import('./gpu-profiler.js').GpuProfiler}
*/
gpuProfiler;
defaultClearOptions = {
color: [0, 0, 0, 1],
depth: 1,
stencil: 0,
flags: CLEARFLAG_COLOR | CLEARFLAG_DEPTH
};
static EVENT_RESIZE = 'resizecanvas';
constructor(canvas, options) {
super();
this.canvas = canvas;
// copy options and handle defaults
this.initOptions = { ...options };
this.initOptions.depth ??= true;
this.initOptions.stencil ??= true;
this.initOptions.antialias ??= true;
this.initOptions.powerPreference ??= 'high-performance';
// Some devices window.devicePixelRatio can be less than one
// eg Oculus Quest 1 which returns a window.devicePixelRatio of 0.8
this._maxPixelRatio = platform.browser ? Math.min(1, window.devicePixelRatio) : 1;
this.buffers = [];
this._vram = {
// #if _PROFILER
texShadow: 0,
texAsset: 0,
texLightmap: 0,
// #endif
tex: 0,
vb: 0,
ib: 0,
ub: 0,
sb: 0
};
this._shaderStats = {
vsCompiled: 0,
fsCompiled: 0,
linked: 0,
materialShaders: 0,
compileTime: 0
};
this.initializeContextCaches();
// Profiler stats
this._drawCallsPerFrame = 0;
this._shaderSwitchesPerFrame = 0;
this._primsPerFrame = [];
for (let i = PRIMITIVE_POINTS; i <= PRIMITIVE_TRIFAN; i++) {
this._primsPerFrame[i] = 0;
}
this._renderTargetCreationTime = 0;
// Create the ScopeNamespace for shader attributes and variables
this.scope = new ScopeSpace("Device");
this.textureBias = this.scope.resolve("textureBias");
this.textureBias.setValue(0.0);
}
/**
* Function that executes after the device has been created.
*/
postInit() {
// create quad vertex buffer
const vertexFormat = new VertexFormat(this, [
{ semantic: SEMANTIC_POSITION, components: 2, type: TYPE_FLOAT32 }
]);
const positions = new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]);
this.quadVertexBuffer = new VertexBuffer(this, vertexFormat, 4, {
data: positions
});
}
/**
* Destroy the graphics device.
*/
destroy() {
// fire the destroy event.
// textures and other device resources may destroy themselves in response.
this.fire('destroy');
this.quadVertexBuffer?.destroy();
this.quadVertexBuffer = null;
this.dynamicBuffers?.destroy();
this.dynamicBuffers = null;
this.gpuProfiler?.destroy();
this.gpuProfiler = null;
}
onDestroyShader(shader) {
this.fire('destroy:shader', shader);
const idx = this.shaders.indexOf(shader);
if (idx !== -1) {
this.shaders.splice(idx, 1);
}
}
// executes after the extended classes have executed their destroy function
postDestroy() {
this.scope = null;
this.canvas = null;
}
/**
* Called when the device context was lost. It releases all context related resources.
*
* @ignore
*/
loseContext() {
this.contextLost = true;
// force the back-buffer to be recreated on restore
this.backBufferSize.set(-1, -1);
// release textures
for (const texture of this.textures) {
texture.loseContext();
}
// release vertex and index buffers
for (const buffer of this.buffers) {
buffer.loseContext();
}
// Reset all render targets so they'll be recreated as required.
// TODO: a solution for the case where a render target contains something
// that was previously generated that needs to be re-rendered.
for (const target of this.targets) {
target.loseContext();
}
this.gpuProfiler?.loseContext();
}
/**
* Called when the device context is restored. It reinitializes all context related resources.
*
* @ignore
*/
restoreContext() {
this.contextLost = false;
this.initializeRenderState();
this.initializeContextCaches();
// Recreate buffer objects and reupload buffer data to the GPU
for (const buffer of this.buffers) {
buffer.unlock();
}
this.gpuProfiler?.restoreContext?.();
}
// don't stringify GraphicsDevice to JSON by JSON.stringify
toJSON(key) {
return undefined;
}
initializeContextCaches() {
this.indexBuffer = null;
this.vertexBuffers = [];
this.shader = null;
this.shaderValid = undefined;
this.shaderAsyncCompile = false;
this.renderTarget = null;
}
initializeRenderState() {
this.blendState = new BlendState();
this.depthState = new DepthState();
this.cullMode = CULLFACE_BACK;
// Cached viewport and scissor dimensions
this.vx = this.vy = this.vw = this.vh = 0;
this.sx = this.sy = this.sw = this.sh = 0;
this.blendColor = new Color(0, 0, 0, 0);
}
/**
* Sets the specified stencil state. If both stencilFront and stencilBack are null, stencil
* operation is disabled.
*
* @param {StencilParameters} [stencilFront] - The front stencil parameters. Defaults to
* {@link StencilParameters.DEFAULT} if not specified.
* @param {StencilParameters} [stencilBack] - The back stencil parameters. Defaults to
* {@link StencilParameters.DEFAULT} if not specified.
*/
setStencilState(stencilFront, stencilBack) {
Debug.assert(false);
}
/**
* Sets the specified blend state.
*
* @param {BlendState} blendState - New blend state.
*/
setBlendState(blendState) {
Debug.assert(false);
}
/**
* Sets the constant blend color and alpha values used with {@link BLENDMODE_CONSTANT} and
* {@link BLENDMODE_ONE_MINUS_CONSTANT} factors specified in {@link BlendState}. Defaults to
* [0, 0, 0, 0].
*
* @param {number} r - The value for red.
* @param {number} g - The value for green.
* @param {number} b - The value for blue.
* @param {number} a - The value for alpha.
*/
setBlendColor(r, g, b, a) {
Debug.assert(false);
}
/**
* Sets the specified depth state.
*
* @param {DepthState} depthState - New depth state.
*/
setDepthState(depthState) {
Debug.assert(false);
}
/**
* Controls how triangles are culled based on their face direction. The default cull mode is
* {@link CULLFACE_BACK}.
*
* @param {number} cullMode - The cull mode to set. Can be:
*
* - {@link CULLFACE_NONE}
* - {@link CULLFACE_BACK}
* - {@link CULLFACE_FRONT}
*/
setCullMode(cullMode) {
Debug.assert(false);
}
/**
* Sets the specified render target on the device. If null is passed as a parameter, the back
* buffer becomes the current target for all rendering operations.
*
* @param {import('./render-target.js').RenderTarget|null} renderTarget - The render target to
* activate.
* @example
* // Set a render target to receive all rendering output
* device.setRenderTarget(renderTarget);
*
* // Set the back buffer to receive all rendering output
* device.setRenderTarget(null);
*/
setRenderTarget(renderTarget) {
this.renderTarget = renderTarget;
}
/**
* Sets the current index buffer on the graphics device. On subsequent calls to
* {@link GraphicsDevice#draw}, the specified index buffer will be used to provide index data
* for any indexed primitives.
*
* @param {import('./index-buffer.js').IndexBuffer} indexBuffer - The index buffer to assign to
* the device.
*/
setIndexBuffer(indexBuffer) {
// Store the index buffer
this.indexBuffer = indexBuffer;
}
/**
* Sets the current vertex buffer on the graphics device. On subsequent calls to
* {@link GraphicsDevice#draw}, the specified vertex buffer(s) will be used to provide vertex
* data for any primitives.
*
* @param {import('./vertex-buffer.js').VertexBuffer} vertexBuffer - The vertex buffer to
* assign to the device.
*/
setVertexBuffer(vertexBuffer) {
if (vertexBuffer) {
this.vertexBuffers.push(vertexBuffer);
}
}
/**
* Clears the vertex buffer set on the graphics device. This is called automatically by the
* renderer.
* @ignore
*/
clearVertexBuffer() {
this.vertexBuffers.length = 0;
}
/**
* Queries the currently set render target on the device.
*
* @returns {import('./render-target.js').RenderTarget} The current render target.
* @example
* // Get the current render target
* const renderTarget = device.getRenderTarget();
*/
getRenderTarget() {
return this.renderTarget;
}
/**
* Initialize render target before it can be used.
*
* @param {import('./render-target.js').RenderTarget} target - The render target to be
* initialized.
* @ignore
*/
initRenderTarget(target) {
if (target.initialized) return;
// #if _PROFILER
const startTime = now();
this.fire('fbo:create', {
timestamp: startTime,
target: this
});
// #endif
target.init();
this.targets.add(target);
// #if _PROFILER
this._renderTargetCreationTime += now() - startTime;
// #endif
}
/**
* Reports whether a texture source is a canvas, image, video or ImageBitmap.
*
* @param {*} texture - Texture source data.
* @returns {boolean} True if the texture is a canvas, image, video or ImageBitmap and false
* otherwise.
* @ignore
*/
_isBrowserInterface(texture) {
return this._isImageBrowserInterface(texture) ||
this._isImageCanvasInterface(texture) ||
this._isImageVideoInterface(texture);
}
_isImageBrowserInterface(texture) {
return (typeof ImageBitmap !== 'undefined' && texture instanceof ImageBitmap) ||
(typeof HTMLImageElement !== 'undefined' && texture instanceof HTMLImageElement);
}
_isImageCanvasInterface(texture) {
return (typeof HTMLCanvasElement !== 'undefined' && texture instanceof HTMLCanvasElement);
}
_isImageVideoInterface(texture) {
return (typeof HTMLVideoElement !== 'undefined' && texture instanceof HTMLVideoElement);
}
/**
* Sets the width and height of the canvas, then fires the `resizecanvas` event. Note that the
* specified width and height values will be multiplied by the value of
* {@link GraphicsDevice#maxPixelRatio} to give the final resultant width and height for the
* canvas.
*
* @param {number} width - The new width of the canvas.
* @param {number} height - The new height of the canvas.
* @ignore
*/
resizeCanvas(width, height) {
const pixelRatio = Math.min(this._maxPixelRatio, platform.browser ? window.devicePixelRatio : 1);
const w = Math.floor(width * pixelRatio);
const h = Math.floor(height * pixelRatio);
if (w !== this.canvas.width || h !== this.canvas.height) {
this.setResolution(w, h);
}
}
/**
* Sets the width and height of the canvas, then fires the `resizecanvas` event. Note that the
* value of {@link GraphicsDevice#maxPixelRatio} is ignored.
*
* @param {number} width - The new width of the canvas.
* @param {number} height - The new height of the canvas.
* @ignore
*/
setResolution(width, height) {
this.canvas.width = width;
this.canvas.height = height;
this.fire(GraphicsDevice.EVENT_RESIZE, width, height);
}
updateClientRect() {
this.clientRect = this.canvas.getBoundingClientRect();
}
/**
* Width of the back buffer in pixels.
*
* @type {number}
*/
get width() {
return this.canvas.width;
}
/**
* Height of the back buffer in pixels.
*
* @type {number}
*/
get height() {
return this.canvas.height;
}
/**
* Fullscreen mode.
*
* @type {boolean}
*/
set fullscreen(fullscreen) {
Debug.error("GraphicsDevice.fullscreen is not implemented on current device.");
}
get fullscreen() {
Debug.error("GraphicsDevice.fullscreen is not implemented on current device.");
return false;
}
/**
* Maximum pixel ratio.
*
* @type {number}
*/
set maxPixelRatio(ratio) {
this._maxPixelRatio = ratio;
}
get maxPixelRatio() {
return this._maxPixelRatio;
}
/**
* The type of the device. Can be pc.DEVICETYPE_WEBGL2 or pc.DEVICETYPE_WEBGPU.
*
* @type {import('./constants.js').DEVICETYPE_WEBGL2 | import('./constants.js').DEVICETYPE_WEBGPU}
*/
get deviceType() {
return this._deviceType;
}
/**
* Queries the maximum number of bones that can be referenced by a shader. The shader
* generators (programlib) use this number to specify the matrix array size of the uniform
* 'matrix_pose[0]'. The value is calculated based on the number of available uniform vectors
* available after subtracting the number taken by a typical heavyweight shader. If a different
* number is required, it can be tuned via {@link GraphicsDevice#setBoneLimit}.
*
* @returns {number} The maximum number of bones that can be supported by the host hardware.
* @ignore
*/
getBoneLimit() {
return this.boneLimit;
}
/**
* Specifies the maximum number of bones that the device can support on the current hardware.
* This function allows the default calculated value based on available vector uniforms to be
* overridden.
*
* @param {number} maxBones - The maximum number of bones supported by the host hardware.
* @ignore
*/
setBoneLimit(maxBones) {
this.boneLimit = maxBones;
}
startRenderPass(renderPass) {
}
endRenderPass(renderPass) {
}
startComputePass() {
}
endComputePass() {
}
/**
* Function which executes at the start of the frame. This should not be called manually, as
* it is handled by the AppBase instance.
*
* @ignore
*/
frameStart() {
this.renderPassIndex = 0;
this.renderVersion++;
Debug.call(() => {
// log out all loaded textures, sorted by gpu memory size
if (Tracing.get(TRACEID_TEXTURES)) {
const textures = this.textures.slice();
textures.sort((a, b) => b.gpuSize - a.gpuSize);
Debug.log(`Textures: ${textures.length}`);
let textureTotal = 0;
textures.forEach((texture, index) => {
const textureSize = texture.gpuSize;
textureTotal += textureSize;
Debug.log(`${index}. ${texture.name} ${texture.width}x${texture.height} VRAM: ${(textureSize / 1024 / 1024).toFixed(2)} MB`);
});
Debug.log(`Total: ${(textureTotal / 1024 / 1024).toFixed(2)}MB`);
}
});
}
/**
* Function which executes at the end of the frame. This should not be called manually, as it is
* handled by the AppBase instance.
*
* @ignore
*/
frameEnd() {
}
/**
* Dispatch multiple compute shaders inside a single compute shader pass.
*
* @param {Array<import('./compute.js').Compute>} computes - An array of compute shaders to
* dispatch.
*/
computeDispatch(computes) {
}
/**
* Get a renderable HDR pixel format supported by the graphics device.
*
* @param {number[]} [formats] - An array of pixel formats to check for support. Can contain:
*
* - {@link PIXELFORMAT_111110F}
* - {@link PIXELFORMAT_RGBA16F}
* - {@link PIXELFORMAT_RGBA32F}
*
* @param {boolean} [filterable] - If true, the format also needs to be filterable. Defaults to
* true.
* @returns {number|undefined} The first supported renderable HDR format or undefined if none is
* supported.
*/
getRenderableHdrFormat(formats = [PIXELFORMAT_111110F, PIXELFORMAT_RGBA16F, PIXELFORMAT_RGBA32F], filterable = true) {
for (let i = 0; i < formats.length; i++) {
const format = formats[i];
switch (format) {
case PIXELFORMAT_111110F: {
if (this.textureRG11B10Renderable)
return format;
break;
}
case PIXELFORMAT_RGBA16F:
if (this.textureHalfFloatRenderable && (!filterable || this.textureHalfFloatFilterable)) {
return format;
}
break;
case PIXELFORMAT_RGBA32F:
if (this.textureFloatRenderable && (!filterable || this.textureFloatFilterable)) {
return format;
}
break;
}
}
return undefined;
}
}
export { GraphicsDevice };