WebGPUPipelineUtils.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. import { BlendColorFactor, OneMinusBlendColorFactor, } from '../../common/Constants.js';
  2. import {
  3. GPUFrontFace, GPUCullMode, GPUColorWriteFlags, GPUCompareFunction, GPUBlendFactor, GPUBlendOperation, GPUIndexFormat, GPUStencilOperation
  4. } from './WebGPUConstants.js';
  5. import {
  6. BackSide, DoubleSide,
  7. NeverDepth, AlwaysDepth, LessDepth, LessEqualDepth, EqualDepth, GreaterEqualDepth, GreaterDepth, NotEqualDepth,
  8. NoBlending, NormalBlending, AdditiveBlending, SubtractiveBlending, MultiplyBlending, CustomBlending, MaterialBlending,
  9. ZeroFactor, OneFactor, SrcColorFactor, OneMinusSrcColorFactor, SrcAlphaFactor, OneMinusSrcAlphaFactor, DstColorFactor,
  10. OneMinusDstColorFactor, DstAlphaFactor, OneMinusDstAlphaFactor, SrcAlphaSaturateFactor,
  11. AddEquation, SubtractEquation, ReverseSubtractEquation, MinEquation, MaxEquation,
  12. KeepStencilOp, ZeroStencilOp, ReplaceStencilOp, InvertStencilOp, IncrementStencilOp, DecrementStencilOp, IncrementWrapStencilOp, DecrementWrapStencilOp,
  13. NeverStencilFunc, AlwaysStencilFunc, LessStencilFunc, LessEqualStencilFunc, EqualStencilFunc, GreaterEqualStencilFunc, GreaterStencilFunc, NotEqualStencilFunc
  14. } from '../../../constants.js';
  15. import { error, warnOnce } from '../../../utils.js';
  16. /**
  17. * A WebGPU backend utility module for managing pipelines.
  18. *
  19. * @private
  20. */
  21. class WebGPUPipelineUtils {
  22. /**
  23. * Constructs a new utility object.
  24. *
  25. * @param {WebGPUBackend} backend - The WebGPU backend.
  26. */
  27. constructor( backend ) {
  28. /**
  29. * A reference to the WebGPU backend.
  30. *
  31. * @type {WebGPUBackend}
  32. */
  33. this.backend = backend;
  34. /**
  35. * A Weak Map that tracks the active pipeline for render or compute passes.
  36. *
  37. * @private
  38. * @type {WeakMap<(GPURenderPassEncoder|GPUComputePassEncoder),(GPURenderPipeline|GPUComputePipeline)>}
  39. */
  40. this._activePipelines = new WeakMap();
  41. }
  42. /**
  43. * Sets the given pipeline for the given pass. The method makes sure to only set the
  44. * pipeline when necessary.
  45. *
  46. * @param {(GPURenderPassEncoder|GPUComputePassEncoder)} pass - The pass encoder.
  47. * @param {(GPURenderPipeline|GPUComputePipeline)} pipeline - The pipeline.
  48. */
  49. setPipeline( pass, pipeline ) {
  50. const currentPipeline = this._activePipelines.get( pass );
  51. if ( currentPipeline !== pipeline ) {
  52. pass.setPipeline( pipeline );
  53. this._activePipelines.set( pass, pipeline );
  54. }
  55. }
  56. /**
  57. * Returns the sample count derived from the given render context.
  58. *
  59. * @private
  60. * @param {RenderContext} renderContext - The render context.
  61. * @return {number} The sample count.
  62. */
  63. _getSampleCount( renderContext ) {
  64. return this.backend.utils.getSampleCountRenderContext( renderContext );
  65. }
  66. /**
  67. * Creates a render pipeline for the given render object.
  68. *
  69. * @param {RenderObject} renderObject - The render object.
  70. * @param {Array<Promise>} promises - An array of compilation promises which are used in `compileAsync()`.
  71. */
  72. createRenderPipeline( renderObject, promises ) {
  73. const { object, material, geometry, pipeline } = renderObject;
  74. const { vertexProgram, fragmentProgram } = pipeline;
  75. const backend = this.backend;
  76. const device = backend.device;
  77. const utils = backend.utils;
  78. const pipelineData = backend.get( pipeline );
  79. // bind group layouts
  80. const bindGroupLayouts = [];
  81. for ( const bindGroup of renderObject.getBindings() ) {
  82. const bindingsData = backend.get( bindGroup );
  83. const { layoutGPU } = bindingsData.layout;
  84. bindGroupLayouts.push( layoutGPU );
  85. }
  86. // vertex buffers
  87. const vertexBuffers = backend.attributeUtils.createShaderVertexBuffers( renderObject );
  88. // material blending
  89. let materialBlending;
  90. if ( material.blending !== NoBlending && ( material.blending !== NormalBlending || material.transparent !== false ) ) {
  91. materialBlending = this._getBlending( material );
  92. }
  93. // stencil
  94. let stencilFront = {};
  95. if ( material.stencilWrite === true ) {
  96. stencilFront = {
  97. compare: this._getStencilCompare( material ),
  98. failOp: this._getStencilOperation( material.stencilFail ),
  99. depthFailOp: this._getStencilOperation( material.stencilZFail ),
  100. passOp: this._getStencilOperation( material.stencilZPass )
  101. };
  102. }
  103. const colorWriteMask = this._getColorWriteMask( material );
  104. const targets = [];
  105. if ( renderObject.context.textures !== null ) {
  106. const textures = renderObject.context.textures;
  107. const mrt = renderObject.context.mrt;
  108. for ( let i = 0; i < textures.length; i ++ ) {
  109. const texture = textures[ i ];
  110. const colorFormat = utils.getTextureFormatGPU( texture );
  111. // mrt blending
  112. let blending;
  113. if ( mrt !== null ) {
  114. if ( this.backend.compatibilityMode !== true ) {
  115. const blendMode = mrt.getBlendMode( texture.name );
  116. if ( blendMode.blending === MaterialBlending ) {
  117. blending = materialBlending;
  118. } else if ( blendMode.blending !== NoBlending ) {
  119. blending = this._getBlending( blendMode );
  120. }
  121. } else {
  122. 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.' );
  123. blending = materialBlending;
  124. }
  125. } else {
  126. blending = materialBlending;
  127. }
  128. targets.push( {
  129. format: colorFormat,
  130. blend: blending,
  131. writeMask: colorWriteMask
  132. } );
  133. }
  134. } else {
  135. const colorFormat = utils.getCurrentColorFormat( renderObject.context );
  136. targets.push( {
  137. format: colorFormat,
  138. blend: materialBlending,
  139. writeMask: colorWriteMask
  140. } );
  141. }
  142. const vertexModule = backend.get( vertexProgram ).module;
  143. const fragmentModule = backend.get( fragmentProgram ).module;
  144. const primitiveState = this._getPrimitiveState( object, geometry, material );
  145. const depthCompare = this._getDepthCompare( material );
  146. const depthStencilFormat = utils.getCurrentDepthStencilFormat( renderObject.context );
  147. const sampleCount = this._getSampleCount( renderObject.context );
  148. const pipelineDescriptor = {
  149. label: `renderPipeline_${ material.name || material.type }_${ material.id }`,
  150. vertex: Object.assign( {}, vertexModule, { buffers: vertexBuffers } ),
  151. fragment: Object.assign( {}, fragmentModule, { targets } ),
  152. primitive: primitiveState,
  153. multisample: {
  154. count: sampleCount,
  155. alphaToCoverageEnabled: material.alphaToCoverage && sampleCount > 1
  156. },
  157. layout: device.createPipelineLayout( {
  158. bindGroupLayouts
  159. } )
  160. };
  161. const depthStencil = {};
  162. const renderDepth = renderObject.context.depth;
  163. const renderStencil = renderObject.context.stencil;
  164. if ( renderDepth === true || renderStencil === true ) {
  165. if ( renderDepth === true ) {
  166. depthStencil.format = depthStencilFormat;
  167. depthStencil.depthWriteEnabled = material.depthWrite;
  168. depthStencil.depthCompare = depthCompare;
  169. }
  170. if ( renderStencil === true ) {
  171. depthStencil.stencilFront = stencilFront;
  172. depthStencil.stencilBack = {}; // three.js does not provide an API to configure the back function (gl.stencilFuncSeparate() was never used)
  173. depthStencil.stencilReadMask = material.stencilFuncMask;
  174. depthStencil.stencilWriteMask = material.stencilWriteMask;
  175. }
  176. if ( material.polygonOffset === true ) {
  177. depthStencil.depthBias = material.polygonOffsetUnits;
  178. depthStencil.depthBiasSlopeScale = material.polygonOffsetFactor;
  179. depthStencil.depthBiasClamp = 0; // three.js does not provide an API to configure this value
  180. }
  181. pipelineDescriptor.depthStencil = depthStencil;
  182. }
  183. // create pipeline
  184. device.pushErrorScope( 'validation' );
  185. if ( promises === null ) {
  186. pipelineData.pipeline = device.createRenderPipeline( pipelineDescriptor );
  187. device.popErrorScope().then( ( err ) => {
  188. if ( err !== null ) {
  189. pipelineData.error = true;
  190. error( err.message );
  191. }
  192. } );
  193. } else {
  194. const p = new Promise( async ( resolve /*, reject*/ ) => {
  195. try {
  196. pipelineData.pipeline = await device.createRenderPipelineAsync( pipelineDescriptor );
  197. } catch ( err ) { }
  198. const errorScope = await device.popErrorScope();
  199. if ( errorScope !== null ) {
  200. pipelineData.error = true;
  201. error( errorScope.message );
  202. }
  203. resolve();
  204. } );
  205. promises.push( p );
  206. }
  207. }
  208. /**
  209. * Creates GPU render bundle encoder for the given render context.
  210. *
  211. * @param {RenderContext} renderContext - The render context.
  212. * @param {?string} [label='renderBundleEncoder'] - The label.
  213. * @return {GPURenderBundleEncoder} The GPU render bundle encoder.
  214. */
  215. createBundleEncoder( renderContext, label = 'renderBundleEncoder' ) {
  216. const backend = this.backend;
  217. const { utils, device } = backend;
  218. const depthStencilFormat = utils.getCurrentDepthStencilFormat( renderContext );
  219. const colorFormats = utils.getCurrentColorFormats( renderContext );
  220. const sampleCount = this._getSampleCount( renderContext );
  221. const descriptor = {
  222. label,
  223. colorFormats,
  224. depthStencilFormat,
  225. sampleCount
  226. };
  227. return device.createRenderBundleEncoder( descriptor );
  228. }
  229. /**
  230. * Creates a compute pipeline for the given compute node.
  231. *
  232. * @param {ComputePipeline} pipeline - The compute pipeline.
  233. * @param {Array<BindGroup>} bindings - The bindings.
  234. */
  235. createComputePipeline( pipeline, bindings ) {
  236. const backend = this.backend;
  237. const device = backend.device;
  238. const computeProgram = backend.get( pipeline.computeProgram ).module;
  239. const pipelineGPU = backend.get( pipeline );
  240. // bind group layouts
  241. const bindGroupLayouts = [];
  242. for ( const bindingsGroup of bindings ) {
  243. const bindingsData = backend.get( bindingsGroup );
  244. const { layoutGPU } = bindingsData.layout;
  245. bindGroupLayouts.push( layoutGPU );
  246. }
  247. pipelineGPU.pipeline = device.createComputePipeline( {
  248. compute: computeProgram,
  249. layout: device.createPipelineLayout( {
  250. bindGroupLayouts
  251. } )
  252. } );
  253. }
  254. /**
  255. * Returns the blending state as a descriptor object required
  256. * for the pipeline creation.
  257. *
  258. * @private
  259. * @param {Material|BlendMode} object - The object containing blending information.
  260. * @return {Object} The blending state.
  261. */
  262. _getBlending( object ) {
  263. let color, alpha;
  264. const blending = object.blending;
  265. const blendSrc = object.blendSrc;
  266. const blendDst = object.blendDst;
  267. const blendEquation = object.blendEquation;
  268. if ( blending === CustomBlending ) {
  269. const blendSrcAlpha = object.blendSrcAlpha !== null ? object.blendSrcAlpha : blendSrc;
  270. const blendDstAlpha = object.blendDstAlpha !== null ? object.blendDstAlpha : blendDst;
  271. const blendEquationAlpha = object.blendEquationAlpha !== null ? object.blendEquationAlpha : blendEquation;
  272. color = {
  273. srcFactor: this._getBlendFactor( blendSrc ),
  274. dstFactor: this._getBlendFactor( blendDst ),
  275. operation: this._getBlendOperation( blendEquation )
  276. };
  277. alpha = {
  278. srcFactor: this._getBlendFactor( blendSrcAlpha ),
  279. dstFactor: this._getBlendFactor( blendDstAlpha ),
  280. operation: this._getBlendOperation( blendEquationAlpha )
  281. };
  282. } else {
  283. const premultipliedAlpha = object.premultipliedAlpha;
  284. const setBlend = ( srcRGB, dstRGB, srcAlpha, dstAlpha ) => {
  285. color = {
  286. srcFactor: srcRGB,
  287. dstFactor: dstRGB,
  288. operation: GPUBlendOperation.Add
  289. };
  290. alpha = {
  291. srcFactor: srcAlpha,
  292. dstFactor: dstAlpha,
  293. operation: GPUBlendOperation.Add
  294. };
  295. };
  296. if ( premultipliedAlpha ) {
  297. switch ( blending ) {
  298. case NormalBlending:
  299. setBlend( GPUBlendFactor.One, GPUBlendFactor.OneMinusSrcAlpha, GPUBlendFactor.One, GPUBlendFactor.OneMinusSrcAlpha );
  300. break;
  301. case AdditiveBlending:
  302. setBlend( GPUBlendFactor.One, GPUBlendFactor.One, GPUBlendFactor.One, GPUBlendFactor.One );
  303. break;
  304. case SubtractiveBlending:
  305. setBlend( GPUBlendFactor.Zero, GPUBlendFactor.OneMinusSrc, GPUBlendFactor.Zero, GPUBlendFactor.One );
  306. break;
  307. case MultiplyBlending:
  308. setBlend( GPUBlendFactor.Dst, GPUBlendFactor.OneMinusSrcAlpha, GPUBlendFactor.Zero, GPUBlendFactor.One );
  309. break;
  310. }
  311. } else {
  312. switch ( blending ) {
  313. case NormalBlending:
  314. setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.OneMinusSrcAlpha, GPUBlendFactor.One, GPUBlendFactor.OneMinusSrcAlpha );
  315. break;
  316. case AdditiveBlending:
  317. setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.One, GPUBlendFactor.One, GPUBlendFactor.One );
  318. break;
  319. case SubtractiveBlending:
  320. error( `WebGPURenderer: "SubtractiveBlending" requires "${ object.isMaterial ? 'material' : 'blendMode' }.premultipliedAlpha = true".` );
  321. break;
  322. case MultiplyBlending:
  323. error( `WebGPURenderer: "MultiplyBlending" requires "${ object.isMaterial ? 'material' : 'blendMode' }.premultipliedAlpha = true".` );
  324. break;
  325. }
  326. }
  327. }
  328. if ( color !== undefined && alpha !== undefined ) {
  329. return { color, alpha };
  330. } else {
  331. error( 'WebGPURenderer: Invalid blending: ', blending );
  332. }
  333. }
  334. /**
  335. * Returns the GPU blend factor which is required for the pipeline creation.
  336. *
  337. * @private
  338. * @param {number} blend - The blend factor as a three.js constant.
  339. * @return {string} The GPU blend factor.
  340. */
  341. _getBlendFactor( blend ) {
  342. let blendFactor;
  343. switch ( blend ) {
  344. case ZeroFactor:
  345. blendFactor = GPUBlendFactor.Zero;
  346. break;
  347. case OneFactor:
  348. blendFactor = GPUBlendFactor.One;
  349. break;
  350. case SrcColorFactor:
  351. blendFactor = GPUBlendFactor.Src;
  352. break;
  353. case OneMinusSrcColorFactor:
  354. blendFactor = GPUBlendFactor.OneMinusSrc;
  355. break;
  356. case SrcAlphaFactor:
  357. blendFactor = GPUBlendFactor.SrcAlpha;
  358. break;
  359. case OneMinusSrcAlphaFactor:
  360. blendFactor = GPUBlendFactor.OneMinusSrcAlpha;
  361. break;
  362. case DstColorFactor:
  363. blendFactor = GPUBlendFactor.Dst;
  364. break;
  365. case OneMinusDstColorFactor:
  366. blendFactor = GPUBlendFactor.OneMinusDst;
  367. break;
  368. case DstAlphaFactor:
  369. blendFactor = GPUBlendFactor.DstAlpha;
  370. break;
  371. case OneMinusDstAlphaFactor:
  372. blendFactor = GPUBlendFactor.OneMinusDstAlpha;
  373. break;
  374. case SrcAlphaSaturateFactor:
  375. blendFactor = GPUBlendFactor.SrcAlphaSaturated;
  376. break;
  377. case BlendColorFactor:
  378. blendFactor = GPUBlendFactor.Constant;
  379. break;
  380. case OneMinusBlendColorFactor:
  381. blendFactor = GPUBlendFactor.OneMinusConstant;
  382. break;
  383. default:
  384. error( 'WebGPURenderer: Blend factor not supported.', blend );
  385. }
  386. return blendFactor;
  387. }
  388. /**
  389. * Returns the GPU stencil compare function which is required for the pipeline creation.
  390. *
  391. * @private
  392. * @param {Material} material - The material.
  393. * @return {string} The GPU stencil compare function.
  394. */
  395. _getStencilCompare( material ) {
  396. let stencilCompare;
  397. const stencilFunc = material.stencilFunc;
  398. switch ( stencilFunc ) {
  399. case NeverStencilFunc:
  400. stencilCompare = GPUCompareFunction.Never;
  401. break;
  402. case AlwaysStencilFunc:
  403. stencilCompare = GPUCompareFunction.Always;
  404. break;
  405. case LessStencilFunc:
  406. stencilCompare = GPUCompareFunction.Less;
  407. break;
  408. case LessEqualStencilFunc:
  409. stencilCompare = GPUCompareFunction.LessEqual;
  410. break;
  411. case EqualStencilFunc:
  412. stencilCompare = GPUCompareFunction.Equal;
  413. break;
  414. case GreaterEqualStencilFunc:
  415. stencilCompare = GPUCompareFunction.GreaterEqual;
  416. break;
  417. case GreaterStencilFunc:
  418. stencilCompare = GPUCompareFunction.Greater;
  419. break;
  420. case NotEqualStencilFunc:
  421. stencilCompare = GPUCompareFunction.NotEqual;
  422. break;
  423. default:
  424. error( 'WebGPURenderer: Invalid stencil function.', stencilFunc );
  425. }
  426. return stencilCompare;
  427. }
  428. /**
  429. * Returns the GPU stencil operation which is required for the pipeline creation.
  430. *
  431. * @private
  432. * @param {number} op - A three.js constant defining the stencil operation.
  433. * @return {string} The GPU stencil operation.
  434. */
  435. _getStencilOperation( op ) {
  436. let stencilOperation;
  437. switch ( op ) {
  438. case KeepStencilOp:
  439. stencilOperation = GPUStencilOperation.Keep;
  440. break;
  441. case ZeroStencilOp:
  442. stencilOperation = GPUStencilOperation.Zero;
  443. break;
  444. case ReplaceStencilOp:
  445. stencilOperation = GPUStencilOperation.Replace;
  446. break;
  447. case InvertStencilOp:
  448. stencilOperation = GPUStencilOperation.Invert;
  449. break;
  450. case IncrementStencilOp:
  451. stencilOperation = GPUStencilOperation.IncrementClamp;
  452. break;
  453. case DecrementStencilOp:
  454. stencilOperation = GPUStencilOperation.DecrementClamp;
  455. break;
  456. case IncrementWrapStencilOp:
  457. stencilOperation = GPUStencilOperation.IncrementWrap;
  458. break;
  459. case DecrementWrapStencilOp:
  460. stencilOperation = GPUStencilOperation.DecrementWrap;
  461. break;
  462. default:
  463. error( 'WebGPURenderer: Invalid stencil operation.', stencilOperation );
  464. }
  465. return stencilOperation;
  466. }
  467. /**
  468. * Returns the GPU blend operation which is required for the pipeline creation.
  469. *
  470. * @private
  471. * @param {number} blendEquation - A three.js constant defining the blend equation.
  472. * @return {string} The GPU blend operation.
  473. */
  474. _getBlendOperation( blendEquation ) {
  475. let blendOperation;
  476. switch ( blendEquation ) {
  477. case AddEquation:
  478. blendOperation = GPUBlendOperation.Add;
  479. break;
  480. case SubtractEquation:
  481. blendOperation = GPUBlendOperation.Subtract;
  482. break;
  483. case ReverseSubtractEquation:
  484. blendOperation = GPUBlendOperation.ReverseSubtract;
  485. break;
  486. case MinEquation:
  487. blendOperation = GPUBlendOperation.Min;
  488. break;
  489. case MaxEquation:
  490. blendOperation = GPUBlendOperation.Max;
  491. break;
  492. default:
  493. error( 'WebGPUPipelineUtils: Blend equation not supported.', blendEquation );
  494. }
  495. return blendOperation;
  496. }
  497. /**
  498. * Returns the primitive state as a descriptor object required
  499. * for the pipeline creation.
  500. *
  501. * @private
  502. * @param {Object3D} object - The 3D object.
  503. * @param {BufferGeometry} geometry - The geometry.
  504. * @param {Material} material - The material.
  505. * @return {Object} The primitive state.
  506. */
  507. _getPrimitiveState( object, geometry, material ) {
  508. const descriptor = {};
  509. const utils = this.backend.utils;
  510. //
  511. descriptor.topology = utils.getPrimitiveTopology( object, material );
  512. if ( geometry.index !== null && object.isLine === true && object.isLineSegments !== true ) {
  513. descriptor.stripIndexFormat = ( geometry.index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;
  514. }
  515. //
  516. let flipSided = ( material.side === BackSide );
  517. if ( object.isMesh && object.matrixWorld.determinant() < 0 ) flipSided = ! flipSided;
  518. descriptor.frontFace = ( flipSided === true ) ? GPUFrontFace.CW : GPUFrontFace.CCW;
  519. //
  520. descriptor.cullMode = ( material.side === DoubleSide ) ? GPUCullMode.None : GPUCullMode.Back;
  521. return descriptor;
  522. }
  523. /**
  524. * Returns the GPU color write mask which is required for the pipeline creation.
  525. *
  526. * @private
  527. * @param {Material} material - The material.
  528. * @return {number} The GPU color write mask.
  529. */
  530. _getColorWriteMask( material ) {
  531. return ( material.colorWrite === true ) ? GPUColorWriteFlags.All : GPUColorWriteFlags.None;
  532. }
  533. /**
  534. * Returns the GPU depth compare function which is required for the pipeline creation.
  535. *
  536. * @private
  537. * @param {Material} material - The material.
  538. * @return {string} The GPU depth compare function.
  539. */
  540. _getDepthCompare( material ) {
  541. let depthCompare;
  542. if ( material.depthTest === false ) {
  543. depthCompare = GPUCompareFunction.Always;
  544. } else {
  545. const depthFunc = material.depthFunc;
  546. switch ( depthFunc ) {
  547. case NeverDepth:
  548. depthCompare = GPUCompareFunction.Never;
  549. break;
  550. case AlwaysDepth:
  551. depthCompare = GPUCompareFunction.Always;
  552. break;
  553. case LessDepth:
  554. depthCompare = GPUCompareFunction.Less;
  555. break;
  556. case LessEqualDepth:
  557. depthCompare = GPUCompareFunction.LessEqual;
  558. break;
  559. case EqualDepth:
  560. depthCompare = GPUCompareFunction.Equal;
  561. break;
  562. case GreaterEqualDepth:
  563. depthCompare = GPUCompareFunction.GreaterEqual;
  564. break;
  565. case GreaterDepth:
  566. depthCompare = GPUCompareFunction.Greater;
  567. break;
  568. case NotEqualDepth:
  569. depthCompare = GPUCompareFunction.NotEqual;
  570. break;
  571. default:
  572. error( 'WebGPUPipelineUtils: Invalid depth function.', depthFunc );
  573. }
  574. }
  575. return depthCompare;
  576. }
  577. }
  578. export default WebGPUPipelineUtils;
粤ICP备19079148号