-
-
Notifications
You must be signed in to change notification settings - Fork 36.3k
Expand file tree
/
Copy pathWebGPUPipelineUtils.js
More file actions
978 lines (659 loc) · 23.6 KB
/
WebGPUPipelineUtils.js
File metadata and controls
978 lines (659 loc) · 23.6 KB
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
965
966
967
968
969
970
971
972
973
974
975
976
977
978
import { BlendColorFactor, OneMinusBlendColorFactor, } from '../../common/Constants.js';
import {
GPUFrontFace, GPUCullMode, GPUColorWriteFlags, GPUCompareFunction, GPUBlendFactor, GPUBlendOperation, GPUIndexFormat, GPUStencilOperation
} from './WebGPUConstants.js';
import {
BackSide, DoubleSide,
NeverDepth, AlwaysDepth, LessDepth, LessEqualDepth, EqualDepth, GreaterEqualDepth, GreaterDepth, NotEqualDepth,
NoBlending, NormalBlending, AdditiveBlending, SubtractiveBlending, MultiplyBlending, CustomBlending, MaterialBlending,
ZeroFactor, OneFactor, SrcColorFactor, OneMinusSrcColorFactor, SrcAlphaFactor, OneMinusSrcAlphaFactor, DstColorFactor,
OneMinusDstColorFactor, DstAlphaFactor, OneMinusDstAlphaFactor, SrcAlphaSaturateFactor,
AddEquation, SubtractEquation, ReverseSubtractEquation, MinEquation, MaxEquation,
KeepStencilOp, ZeroStencilOp, ReplaceStencilOp, InvertStencilOp, IncrementStencilOp, DecrementStencilOp, IncrementWrapStencilOp, DecrementWrapStencilOp,
NeverStencilFunc, AlwaysStencilFunc, LessStencilFunc, LessEqualStencilFunc, EqualStencilFunc, GreaterEqualStencilFunc, GreaterStencilFunc, NotEqualStencilFunc
} from '../../../constants.js';
import { error, ReversedDepthFuncs, warn, warnOnce } from '../../../utils.js';
/**
* A WebGPU backend utility module for managing pipelines.
*
* @private
*/
class WebGPUPipelineUtils {
/**
* Constructs a new utility object.
*
* @param {WebGPUBackend} backend - The WebGPU backend.
*/
constructor( backend ) {
/**
* A reference to the WebGPU backend.
*
* @type {WebGPUBackend}
*/
this.backend = backend;
/**
* A Weak Map that tracks the active pipeline for render or compute passes.
*
* @private
* @type {WeakMap<(GPURenderPassEncoder|GPUComputePassEncoder),(GPURenderPipeline|GPUComputePipeline)>}
*/
this._activePipelines = new WeakMap();
}
/**
* Sets the given pipeline for the given pass. The method makes sure to only set the
* pipeline when necessary.
*
* @param {(GPURenderPassEncoder|GPUComputePassEncoder)} pass - The pass encoder.
* @param {(GPURenderPipeline|GPUComputePipeline)} pipeline - The pipeline.
*/
setPipeline( pass, pipeline ) {
const currentPipeline = this._activePipelines.get( pass );
if ( currentPipeline !== pipeline ) {
pass.setPipeline( pipeline );
this._activePipelines.set( pass, pipeline );
}
}
/**
* Returns the sample count derived from the given render context.
*
* @private
* @param {RenderContext} renderContext - The render context.
* @return {number} The sample count.
*/
_getSampleCount( renderContext ) {
return this.backend.utils.getSampleCountRenderContext( renderContext );
}
/**
* Creates a render pipeline for the given render object.
*
* @param {RenderObject} renderObject - The render object.
* @param {Array<Promise>} promises - An array of compilation promises which are used in `compileAsync()`.
*/
createRenderPipeline( renderObject, promises ) {
const { object, material, geometry, pipeline } = renderObject;
const { vertexProgram, fragmentProgram } = pipeline;
const backend = this.backend;
const device = backend.device;
const utils = backend.utils;
const pipelineData = backend.get( pipeline );
// bind group layouts
const bindGroupLayouts = [];
for ( const bindGroup of renderObject.getBindings() ) {
const bindingsData = backend.get( bindGroup );
const { layoutGPU } = bindingsData.layout;
bindGroupLayouts.push( layoutGPU );
}
// vertex buffers
const vertexBuffers = backend.attributeUtils.createShaderVertexBuffers( renderObject );
// material blending
let materialBlending;
if ( material.blending !== NoBlending && ( material.blending !== NormalBlending || material.transparent !== false ) ) {
materialBlending = this._getBlending( material );
}
// stencil
let stencilFront = {};
if ( material.stencilWrite === true ) {
stencilFront = {
compare: this._getStencilCompare( material ),
failOp: this._getStencilOperation( material.stencilFail ),
depthFailOp: this._getStencilOperation( material.stencilZFail ),
passOp: this._getStencilOperation( material.stencilZPass )
};
}
const colorWriteMask = this._getColorWriteMask( material );
const targets = [];
if ( renderObject.context.textures !== null ) {
const textures = renderObject.context.textures;
const mrt = renderObject.context.mrt;
for ( let i = 0; i < textures.length; i ++ ) {
const texture = textures[ i ];
const colorFormat = utils.getTextureFormatGPU( texture );
// mrt blending
let blending;
if ( mrt !== null ) {
if ( this.backend.compatibilityMode !== true ) {
const blendMode = mrt.getBlendMode( texture.name );
if ( blendMode.blending === MaterialBlending ) {
blending = materialBlending;
} else if ( blendMode.blending !== NoBlending ) {
blending = this._getBlending( blendMode );
}
} else {
warnOnce( 'WebGPURenderer: Multiple Render Targets (MRT) blending configuration is not fully supported in compatibility mode. The material blending will be used for all render targets.' );
blending = materialBlending;
}
} else {
blending = materialBlending;
}
targets.push( {
format: colorFormat,
blend: blending,
writeMask: colorWriteMask
} );
}
} else {
const colorFormat = utils.getCurrentColorFormat( renderObject.context );
targets.push( {
format: colorFormat,
blend: materialBlending,
writeMask: colorWriteMask
} );
}
const vertexModule = backend.get( vertexProgram ).module;
const fragmentModule = backend.get( fragmentProgram ).module;
const primitiveState = this._getPrimitiveState( object, geometry, material );
const depthCompare = this._getDepthCompare( material );
const depthStencilFormat = utils.getCurrentDepthStencilFormat( renderObject.context );
const sampleCount = this._getSampleCount( renderObject.context );
const pipelineDescriptor = {
label: `renderPipeline_${ material.name || material.type }_${ material.id }`,
vertex: Object.assign( {}, vertexModule, { buffers: vertexBuffers } ),
fragment: Object.assign( {}, fragmentModule, { targets } ),
primitive: primitiveState,
multisample: {
count: sampleCount,
alphaToCoverageEnabled: material.alphaToCoverage && sampleCount > 1
},
layout: device.createPipelineLayout( {
bindGroupLayouts
} )
};
const depthStencil = {};
const renderDepth = renderObject.context.depth;
const renderStencil = renderObject.context.stencil;
if ( renderDepth === true || renderStencil === true ) {
if ( renderDepth === true ) {
depthStencil.format = depthStencilFormat;
depthStencil.depthWriteEnabled = material.depthWrite;
depthStencil.depthCompare = depthCompare;
}
if ( renderStencil === true ) {
depthStencil.stencilFront = stencilFront;
depthStencil.stencilBack = stencilFront; // apply the same stencil ops to both faces, matching gl.stencilOp() which is not face-separated
depthStencil.stencilReadMask = material.stencilFuncMask;
depthStencil.stencilWriteMask = material.stencilWriteMask;
}
if ( material.polygonOffset === true ) {
depthStencil.depthBias = material.polygonOffsetUnits;
depthStencil.depthBiasSlopeScale = material.polygonOffsetFactor;
depthStencil.depthBiasClamp = 0; // three.js does not provide an API to configure this value
}
pipelineDescriptor.depthStencil = depthStencil;
}
// create pipeline
device.pushErrorScope( 'validation' );
const stages = [
{ program: vertexProgram, module: vertexModule.module },
{ program: fragmentProgram, module: fragmentModule.module }
];
const pipelineLabel = pipelineDescriptor.label;
if ( promises === null ) {
pipelineData.pipeline = device.createRenderPipeline( pipelineDescriptor );
device.popErrorScope().then( ( err ) => {
if ( err !== null ) {
pipelineData.error = true;
error( `WebGPURenderer: Render pipeline creation failed (${ pipelineLabel }): ${ err.message }` );
this._reportShaderDiagnostics( stages, pipelineLabel );
}
} );
} else {
const p = new Promise( async ( resolve /*, reject*/ ) => {
try {
let asyncError = null;
try {
pipelineData.pipeline = await device.createRenderPipelineAsync( pipelineDescriptor );
} catch ( err ) {
asyncError = err;
}
const errorScope = await device.popErrorScope();
if ( errorScope !== null || asyncError !== null ) {
pipelineData.error = true;
const reason = ( errorScope && errorScope.message ) || ( asyncError && asyncError.message ) || 'unknown';
error( `WebGPURenderer: Async render pipeline creation failed (${ pipelineLabel }): ${ reason }` );
await this._reportShaderDiagnostics( stages, pipelineLabel );
}
} finally {
// Guarantee resolution so `compileAsync`'s Promise.all cannot hang on an
// unexpected throw from any await above.
resolve();
}
} );
promises.push( p );
}
}
/**
* Creates GPU render bundle encoder for the given render context.
*
* @param {RenderContext} renderContext - The render context.
* @param {?string} [label='renderBundleEncoder'] - The label.
* @return {GPURenderBundleEncoder} The GPU render bundle encoder.
*/
createBundleEncoder( renderContext, label = 'renderBundleEncoder' ) {
const backend = this.backend;
const { utils, device } = backend;
const depthStencilFormat = utils.getCurrentDepthStencilFormat( renderContext );
const colorFormats = utils.getCurrentColorFormats( renderContext );
const sampleCount = this._getSampleCount( renderContext );
const descriptor = {
label,
colorFormats,
depthStencilFormat,
sampleCount
};
return device.createRenderBundleEncoder( descriptor );
}
/**
* Creates a compute pipeline for the given compute node.
*
* @param {ComputePipeline} pipeline - The compute pipeline.
* @param {Array<BindGroup>} bindings - The bindings.
*/
createComputePipeline( pipeline, bindings ) {
const backend = this.backend;
const device = backend.device;
const computeProgram = backend.get( pipeline.computeProgram ).module;
const pipelineGPU = backend.get( pipeline );
// bind group layouts
const bindGroupLayouts = [];
for ( const bindingsGroup of bindings ) {
const bindingsData = backend.get( bindingsGroup );
const { layoutGPU } = bindingsData.layout;
bindGroupLayouts.push( layoutGPU );
}
const computeStage = pipeline.computeProgram;
const pipelineLabel = `computePipeline_${ computeStage.stage }${ computeStage.name ? `_${ computeStage.name }` : '' }`;
device.pushErrorScope( 'validation' );
pipelineGPU.pipeline = device.createComputePipeline( {
label: pipelineLabel,
compute: computeProgram,
layout: device.createPipelineLayout( {
bindGroupLayouts
} )
} );
device.popErrorScope().then( ( err ) => {
if ( err !== null ) {
pipelineGPU.error = true;
error( `WebGPURenderer: Compute pipeline creation failed (${ pipelineLabel }): ${ err.message }` );
this._reportShaderDiagnostics( [ { program: computeStage, module: computeProgram.module } ], pipelineLabel );
}
} );
}
/**
* Reads line-accurate diagnostics from shader modules and logs any
* errors/warnings/info messages. Called from pipeline creation error paths
* to turn opaque validation failures into actionable WGSL feedback.
*
* Contract: this method is best-effort and must never propagate an error
* to its caller. All failures (spec gaps, custom logger throwing, future
* edits) are swallowed by the top-level try/catch. Callers can fire and
* forget without a `.catch()` guard.
*
* @private
* @param {Array<{program: ProgrammableStage, module: GPUShaderModule}>} stages - Pairs of program + compiled shader module.
* @param {string} pipelineLabel - Label of the owning pipeline, used as log prefix.
* @return {Promise<void>}
*/
async _reportShaderDiagnostics( stages, pipelineLabel ) {
try {
for ( const { program, module } of stages ) {
if ( ! module || typeof module.getCompilationInfo !== 'function' ) continue;
let info;
try {
info = await module.getCompilationInfo();
} catch ( _ ) {
continue;
}
if ( ! info || ! info.messages || info.messages.length === 0 ) continue;
const stageName = program ? program.stage : 'shader';
const sourceLines = program && program.code ? program.code.split( '\n' ) : null;
for ( const msg of info.messages ) {
const location = ( msg.lineNum > 0 )
? ` at line ${ msg.lineNum }${ msg.linePos > 0 ? `:${ msg.linePos }` : '' }`
: '';
const header = `WebGPURenderer [${ pipelineLabel } / ${ stageName } ${ msg.type }]${ location }: ${ msg.message }`;
let excerpt = '';
if ( sourceLines && msg.lineNum > 0 ) {
const line = sourceLines[ msg.lineNum - 1 ];
if ( line !== undefined ) {
excerpt = `\n ${ line }`;
if ( msg.linePos > 0 ) {
excerpt += `\n ${ ' '.repeat( Math.max( 0, msg.linePos - 1 ) ) }^`;
}
}
}
if ( msg.type === 'error' ) {
error( header + excerpt );
} else {
warn( header + excerpt );
}
}
}
} catch ( _ ) {
// Diagnostics are best-effort; never propagate.
}
}
/**
* Returns the blending state as a descriptor object required
* for the pipeline creation.
*
* @private
* @param {Material|BlendMode} object - The object containing blending information.
* @return {Object} The blending state.
*/
_getBlending( object ) {
let color, alpha;
const blending = object.blending;
const blendSrc = object.blendSrc;
const blendDst = object.blendDst;
const blendEquation = object.blendEquation;
if ( blending === CustomBlending ) {
const blendSrcAlpha = object.blendSrcAlpha !== null ? object.blendSrcAlpha : blendSrc;
const blendDstAlpha = object.blendDstAlpha !== null ? object.blendDstAlpha : blendDst;
const blendEquationAlpha = object.blendEquationAlpha !== null ? object.blendEquationAlpha : blendEquation;
color = {
srcFactor: this._getBlendFactor( blendSrc ),
dstFactor: this._getBlendFactor( blendDst ),
operation: this._getBlendOperation( blendEquation )
};
alpha = {
srcFactor: this._getBlendFactor( blendSrcAlpha ),
dstFactor: this._getBlendFactor( blendDstAlpha ),
operation: this._getBlendOperation( blendEquationAlpha )
};
} else {
const premultipliedAlpha = object.premultipliedAlpha;
const setBlend = ( srcRGB, dstRGB, srcAlpha, dstAlpha ) => {
color = {
srcFactor: srcRGB,
dstFactor: dstRGB,
operation: GPUBlendOperation.Add
};
alpha = {
srcFactor: srcAlpha,
dstFactor: dstAlpha,
operation: GPUBlendOperation.Add
};
};
if ( premultipliedAlpha ) {
switch ( blending ) {
case NormalBlending:
setBlend( GPUBlendFactor.One, GPUBlendFactor.OneMinusSrcAlpha, GPUBlendFactor.One, GPUBlendFactor.OneMinusSrcAlpha );
break;
case AdditiveBlending:
setBlend( GPUBlendFactor.One, GPUBlendFactor.One, GPUBlendFactor.One, GPUBlendFactor.One );
break;
case SubtractiveBlending:
setBlend( GPUBlendFactor.Zero, GPUBlendFactor.OneMinusSrc, GPUBlendFactor.Zero, GPUBlendFactor.One );
break;
case MultiplyBlending:
setBlend( GPUBlendFactor.Dst, GPUBlendFactor.OneMinusSrcAlpha, GPUBlendFactor.Zero, GPUBlendFactor.One );
break;
}
} else {
switch ( blending ) {
case NormalBlending:
setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.OneMinusSrcAlpha, GPUBlendFactor.One, GPUBlendFactor.OneMinusSrcAlpha );
break;
case AdditiveBlending:
setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.One, GPUBlendFactor.One, GPUBlendFactor.One );
break;
case SubtractiveBlending:
error( `WebGPURenderer: "SubtractiveBlending" requires "${ object.isMaterial ? 'material' : 'blendMode' }.premultipliedAlpha = true".` );
break;
case MultiplyBlending:
error( `WebGPURenderer: "MultiplyBlending" requires "${ object.isMaterial ? 'material' : 'blendMode' }.premultipliedAlpha = true".` );
break;
}
}
}
if ( color !== undefined && alpha !== undefined ) {
return { color, alpha };
} else {
error( 'WebGPURenderer: Invalid blending: ', blending );
}
}
/**
* Returns the GPU blend factor which is required for the pipeline creation.
*
* @private
* @param {number} blend - The blend factor as a three.js constant.
* @return {string} The GPU blend factor.
*/
_getBlendFactor( blend ) {
let blendFactor;
switch ( blend ) {
case ZeroFactor:
blendFactor = GPUBlendFactor.Zero;
break;
case OneFactor:
blendFactor = GPUBlendFactor.One;
break;
case SrcColorFactor:
blendFactor = GPUBlendFactor.Src;
break;
case OneMinusSrcColorFactor:
blendFactor = GPUBlendFactor.OneMinusSrc;
break;
case SrcAlphaFactor:
blendFactor = GPUBlendFactor.SrcAlpha;
break;
case OneMinusSrcAlphaFactor:
blendFactor = GPUBlendFactor.OneMinusSrcAlpha;
break;
case DstColorFactor:
blendFactor = GPUBlendFactor.Dst;
break;
case OneMinusDstColorFactor:
blendFactor = GPUBlendFactor.OneMinusDst;
break;
case DstAlphaFactor:
blendFactor = GPUBlendFactor.DstAlpha;
break;
case OneMinusDstAlphaFactor:
blendFactor = GPUBlendFactor.OneMinusDstAlpha;
break;
case SrcAlphaSaturateFactor:
blendFactor = GPUBlendFactor.SrcAlphaSaturated;
break;
case BlendColorFactor:
blendFactor = GPUBlendFactor.Constant;
break;
case OneMinusBlendColorFactor:
blendFactor = GPUBlendFactor.OneMinusConstant;
break;
default:
error( 'WebGPURenderer: Blend factor not supported.', blend );
}
return blendFactor;
}
/**
* Returns the GPU stencil compare function which is required for the pipeline creation.
*
* @private
* @param {Material} material - The material.
* @return {string} The GPU stencil compare function.
*/
_getStencilCompare( material ) {
let stencilCompare;
const stencilFunc = material.stencilFunc;
switch ( stencilFunc ) {
case NeverStencilFunc:
stencilCompare = GPUCompareFunction.Never;
break;
case AlwaysStencilFunc:
stencilCompare = GPUCompareFunction.Always;
break;
case LessStencilFunc:
stencilCompare = GPUCompareFunction.Less;
break;
case LessEqualStencilFunc:
stencilCompare = GPUCompareFunction.LessEqual;
break;
case EqualStencilFunc:
stencilCompare = GPUCompareFunction.Equal;
break;
case GreaterEqualStencilFunc:
stencilCompare = GPUCompareFunction.GreaterEqual;
break;
case GreaterStencilFunc:
stencilCompare = GPUCompareFunction.Greater;
break;
case NotEqualStencilFunc:
stencilCompare = GPUCompareFunction.NotEqual;
break;
default:
error( 'WebGPURenderer: Invalid stencil function.', stencilFunc );
}
return stencilCompare;
}
/**
* Returns the GPU stencil operation which is required for the pipeline creation.
*
* @private
* @param {number} op - A three.js constant defining the stencil operation.
* @return {string} The GPU stencil operation.
*/
_getStencilOperation( op ) {
let stencilOperation;
switch ( op ) {
case KeepStencilOp:
stencilOperation = GPUStencilOperation.Keep;
break;
case ZeroStencilOp:
stencilOperation = GPUStencilOperation.Zero;
break;
case ReplaceStencilOp:
stencilOperation = GPUStencilOperation.Replace;
break;
case InvertStencilOp:
stencilOperation = GPUStencilOperation.Invert;
break;
case IncrementStencilOp:
stencilOperation = GPUStencilOperation.IncrementClamp;
break;
case DecrementStencilOp:
stencilOperation = GPUStencilOperation.DecrementClamp;
break;
case IncrementWrapStencilOp:
stencilOperation = GPUStencilOperation.IncrementWrap;
break;
case DecrementWrapStencilOp:
stencilOperation = GPUStencilOperation.DecrementWrap;
break;
default:
error( 'WebGPURenderer: Invalid stencil operation.', stencilOperation );
}
return stencilOperation;
}
/**
* Returns the GPU blend operation which is required for the pipeline creation.
*
* @private
* @param {number} blendEquation - A three.js constant defining the blend equation.
* @return {string} The GPU blend operation.
*/
_getBlendOperation( blendEquation ) {
let blendOperation;
switch ( blendEquation ) {
case AddEquation:
blendOperation = GPUBlendOperation.Add;
break;
case SubtractEquation:
blendOperation = GPUBlendOperation.Subtract;
break;
case ReverseSubtractEquation:
blendOperation = GPUBlendOperation.ReverseSubtract;
break;
case MinEquation:
blendOperation = GPUBlendOperation.Min;
break;
case MaxEquation:
blendOperation = GPUBlendOperation.Max;
break;
default:
error( 'WebGPUPipelineUtils: Blend equation not supported.', blendEquation );
}
return blendOperation;
}
/**
* Returns the primitive state as a descriptor object required
* for the pipeline creation.
*
* @private
* @param {Object3D} object - The 3D object.
* @param {BufferGeometry} geometry - The geometry.
* @param {Material} material - The material.
* @return {Object} The primitive state.
*/
_getPrimitiveState( object, geometry, material ) {
const descriptor = {};
const utils = this.backend.utils;
//
descriptor.topology = utils.getPrimitiveTopology( object, material );
if ( geometry.index !== null && object.isLine === true && object.isLineSegments !== true ) {
descriptor.stripIndexFormat = ( geometry.index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;
}
//
let flipSided = ( material.side === BackSide );
if ( object.isMesh && object.matrixWorld.determinant() < 0 ) flipSided = ! flipSided;
descriptor.frontFace = ( flipSided === true ) ? GPUFrontFace.CW : GPUFrontFace.CCW;
//
descriptor.cullMode = ( material.side === DoubleSide ) ? GPUCullMode.None : GPUCullMode.Back;
return descriptor;
}
/**
* Returns the GPU color write mask which is required for the pipeline creation.
*
* @private
* @param {Material} material - The material.
* @return {number} The GPU color write mask.
*/
_getColorWriteMask( material ) {
return ( material.colorWrite === true ) ? GPUColorWriteFlags.All : GPUColorWriteFlags.None;
}
/**
* Returns the GPU depth compare function which is required for the pipeline creation.
*
* @private
* @param {Material} material - The material.
* @return {string} The GPU depth compare function.
*/
_getDepthCompare( material ) {
let depthCompare;
if ( material.depthTest === false ) {
depthCompare = GPUCompareFunction.Always;
} else {
const depthFunc = ( this.backend.parameters.reversedDepthBuffer ) ? ReversedDepthFuncs[ material.depthFunc ] : material.depthFunc;
switch ( depthFunc ) {
case NeverDepth:
depthCompare = GPUCompareFunction.Never;
break;
case AlwaysDepth:
depthCompare = GPUCompareFunction.Always;
break;
case LessDepth:
depthCompare = GPUCompareFunction.Less;
break;
case LessEqualDepth:
depthCompare = GPUCompareFunction.LessEqual;
break;
case EqualDepth:
depthCompare = GPUCompareFunction.Equal;
break;
case GreaterEqualDepth:
depthCompare = GPUCompareFunction.GreaterEqual;
break;
case GreaterDepth:
depthCompare = GPUCompareFunction.Greater;
break;
case NotEqualDepth:
depthCompare = GPUCompareFunction.NotEqual;
break;
default:
error( 'WebGPUPipelineUtils: Invalid depth function.', depthFunc );
}
}
return depthCompare;
}
}
export default WebGPUPipelineUtils;