WebGPUPipelineUtils.js 20 KB

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