WebGPUPipelineUtils.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. FrontSide, BackSide, DoubleSide,
  7. NeverDepth, AlwaysDepth, LessDepth, LessEqualDepth, EqualDepth, GreaterEqualDepth, GreaterDepth, NotEqualDepth,
  8. NoBlending, NormalBlending, AdditiveBlending, SubtractiveBlending, MultiplyBlending, CustomBlending,
  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 'three';
  15. class WebGPUPipelineUtils {
  16. constructor( backend ) {
  17. this.backend = backend;
  18. }
  19. _getSampleCount( renderObjectContext ) {
  20. let sampleCount = this.backend.utils.getSampleCount( renderObjectContext );
  21. if ( sampleCount > 1 ) {
  22. // WebGPU only supports power-of-two sample counts and 2 is not a valid value
  23. sampleCount = Math.pow( 2, Math.floor( Math.log2( sampleCount ) ) );
  24. if ( sampleCount === 2 ) {
  25. sampleCount = 4;
  26. }
  27. }
  28. return sampleCount;
  29. }
  30. createRenderPipeline( renderObject, promises ) {
  31. const { object, material, geometry, pipeline } = renderObject;
  32. const { vertexProgram, fragmentProgram } = pipeline;
  33. const backend = this.backend;
  34. const device = backend.device;
  35. const utils = backend.utils;
  36. const pipelineData = backend.get( pipeline );
  37. // bind group layouts
  38. const bindGroupLayouts = [];
  39. for ( const bindGroup of renderObject.getBindings() ) {
  40. const bindingsData = backend.get( bindGroup );
  41. bindGroupLayouts.push( bindingsData.layout );
  42. }
  43. // vertex buffers
  44. const vertexBuffers = backend.attributeUtils.createShaderVertexBuffers( renderObject );
  45. // blending
  46. let blending;
  47. if ( material.transparent === true && material.blending !== NoBlending ) {
  48. blending = this._getBlending( material );
  49. }
  50. // stencil
  51. let stencilFront = {};
  52. if ( material.stencilWrite === true ) {
  53. stencilFront = {
  54. compare: this._getStencilCompare( material ),
  55. failOp: this._getStencilOperation( material.stencilFail ),
  56. depthFailOp: this._getStencilOperation( material.stencilZFail ),
  57. passOp: this._getStencilOperation( material.stencilZPass )
  58. };
  59. }
  60. const colorWriteMask = this._getColorWriteMask( material );
  61. const targets = [];
  62. if ( renderObject.context.textures !== null ) {
  63. const textures = renderObject.context.textures;
  64. for ( let i = 0; i < textures.length; i ++ ) {
  65. const colorFormat = utils.getTextureFormatGPU( textures[ i ] );
  66. targets.push( {
  67. format: colorFormat,
  68. blend: blending,
  69. writeMask: colorWriteMask
  70. } );
  71. }
  72. } else {
  73. const colorFormat = utils.getCurrentColorFormat( renderObject.context );
  74. targets.push( {
  75. format: colorFormat,
  76. blend: blending,
  77. writeMask: colorWriteMask
  78. } );
  79. }
  80. const vertexModule = backend.get( vertexProgram ).module;
  81. const fragmentModule = backend.get( fragmentProgram ).module;
  82. const primitiveState = this._getPrimitiveState( object, geometry, material );
  83. const depthCompare = this._getDepthCompare( material );
  84. const depthStencilFormat = utils.getCurrentDepthStencilFormat( renderObject.context );
  85. const sampleCount = this._getSampleCount( renderObject.context );
  86. const pipelineDescriptor = {
  87. label: 'renderPipeline',
  88. vertex: Object.assign( {}, vertexModule, { buffers: vertexBuffers } ),
  89. fragment: Object.assign( {}, fragmentModule, { targets } ),
  90. primitive: primitiveState,
  91. depthStencil: {
  92. format: depthStencilFormat,
  93. depthWriteEnabled: material.depthWrite,
  94. depthCompare: depthCompare,
  95. stencilFront: stencilFront,
  96. stencilBack: {}, // three.js does not provide an API to configure the back function (gl.stencilFuncSeparate() was never used)
  97. stencilReadMask: material.stencilFuncMask,
  98. stencilWriteMask: material.stencilWriteMask
  99. },
  100. multisample: {
  101. count: sampleCount,
  102. alphaToCoverageEnabled: material.alphaToCoverage
  103. },
  104. layout: device.createPipelineLayout( {
  105. bindGroupLayouts
  106. } )
  107. };
  108. if ( promises === null ) {
  109. pipelineData.pipeline = device.createRenderPipeline( pipelineDescriptor );
  110. } else {
  111. const p = new Promise( ( resolve /*, reject*/ ) => {
  112. device.createRenderPipelineAsync( pipelineDescriptor ).then( pipeline => {
  113. pipelineData.pipeline = pipeline;
  114. resolve();
  115. } );
  116. } );
  117. promises.push( p );
  118. }
  119. }
  120. createBundleEncoder( renderContext, renderObject ) {
  121. const backend = this.backend;
  122. const { utils, device } = backend;
  123. const renderContextData = backend.get( renderContext );
  124. const renderObjectData = backend.get( renderObject );
  125. const depthStencilFormat = utils.getCurrentDepthStencilFormat( renderContext );
  126. const colorFormat = utils.getCurrentColorFormat( renderContext );
  127. const sampleCount = this._getSampleCount( renderObject.context );
  128. const descriptor = {
  129. label: 'renderBundleEncoder',
  130. colorFormats: [ colorFormat ],
  131. depthStencilFormat,
  132. sampleCount
  133. };
  134. const bundleEncoder = device.createRenderBundleEncoder( descriptor );
  135. renderObjectData.bundleEncoder = bundleEncoder;
  136. renderContextData.currentSets = { attributes: {} };
  137. renderContextData._renderBundleViewport = renderContext.width + '_' + renderContext.height;
  138. return bundleEncoder;
  139. }
  140. createComputePipeline( pipeline, bindings ) {
  141. const backend = this.backend;
  142. const device = backend.device;
  143. const computeProgram = backend.get( pipeline.computeProgram ).module;
  144. const pipelineGPU = backend.get( pipeline );
  145. // bind group layouts
  146. const bindGroupLayouts = [];
  147. for ( const bindingsGroup of bindings ) {
  148. const bindingsData = backend.get( bindingsGroup );
  149. bindGroupLayouts.push( bindingsData.layout );
  150. }
  151. pipelineGPU.pipeline = device.createComputePipeline( {
  152. compute: computeProgram,
  153. layout: device.createPipelineLayout( {
  154. bindGroupLayouts
  155. } )
  156. } );
  157. }
  158. _getBlending( material ) {
  159. let color, alpha;
  160. const blending = material.blending;
  161. const blendSrc = material.blendSrc;
  162. const blendDst = material.blendDst;
  163. const blendEquation = material.blendEquation;
  164. if ( blending === CustomBlending ) {
  165. const blendSrcAlpha = material.blendSrcAlpha !== null ? material.blendSrcAlpha : blendSrc;
  166. const blendDstAlpha = material.blendDstAlpha !== null ? material.blendDstAlpha : blendDst;
  167. const blendEquationAlpha = material.blendEquationAlpha !== null ? material.blendEquationAlpha : blendEquation;
  168. color = {
  169. srcFactor: this._getBlendFactor( blendSrc ),
  170. dstFactor: this._getBlendFactor( blendDst ),
  171. operation: this._getBlendOperation( blendEquation )
  172. };
  173. alpha = {
  174. srcFactor: this._getBlendFactor( blendSrcAlpha ),
  175. dstFactor: this._getBlendFactor( blendDstAlpha ),
  176. operation: this._getBlendOperation( blendEquationAlpha )
  177. };
  178. } else {
  179. const premultipliedAlpha = material.premultipliedAlpha;
  180. const setBlend = ( srcRGB, dstRGB, srcAlpha, dstAlpha ) => {
  181. color = {
  182. srcFactor: srcRGB,
  183. dstFactor: dstRGB,
  184. operation: GPUBlendOperation.Add
  185. };
  186. alpha = {
  187. srcFactor: srcAlpha,
  188. dstFactor: dstAlpha,
  189. operation: GPUBlendOperation.Add
  190. };
  191. };
  192. if ( premultipliedAlpha ) {
  193. switch ( blending ) {
  194. case NormalBlending:
  195. setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.OneMinusSrcAlpha, GPUBlendFactor.One, GPUBlendFactor.OneMinusSrcAlpha );
  196. break;
  197. case AdditiveBlending:
  198. setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.One, GPUBlendFactor.One, GPUBlendFactor.One );
  199. break;
  200. case SubtractiveBlending:
  201. setBlend( GPUBlendFactor.Zero, GPUBlendFactor.OneMinusSrc, GPUBlendFactor.Zero, GPUBlendFactor.One );
  202. break;
  203. case MultiplyBlending:
  204. setBlend( GPUBlendFactor.Zero, GPUBlendFactor.Src, GPUBlendFactor.Zero, GPUBlendFactor.SrcAlpha );
  205. break;
  206. }
  207. } else {
  208. switch ( blending ) {
  209. case NormalBlending:
  210. setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.OneMinusSrcAlpha, GPUBlendFactor.One, GPUBlendFactor.OneMinusSrcAlpha );
  211. break;
  212. case AdditiveBlending:
  213. setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.One, GPUBlendFactor.SrcAlpha, GPUBlendFactor.One );
  214. break;
  215. case SubtractiveBlending:
  216. setBlend( GPUBlendFactor.Zero, GPUBlendFactor.OneMinusSrc, GPUBlendFactor.Zero, GPUBlendFactor.One );
  217. break;
  218. case MultiplyBlending:
  219. setBlend( GPUBlendFactor.Zero, GPUBlendFactor.Src, GPUBlendFactor.Zero, GPUBlendFactor.Src );
  220. break;
  221. }
  222. }
  223. }
  224. if ( color !== undefined && alpha !== undefined ) {
  225. return { color, alpha };
  226. } else {
  227. console.error( 'THREE.WebGPURenderer: Invalid blending: ', blending );
  228. }
  229. }
  230. _getBlendFactor( blend ) {
  231. let blendFactor;
  232. switch ( blend ) {
  233. case ZeroFactor:
  234. blendFactor = GPUBlendFactor.Zero;
  235. break;
  236. case OneFactor:
  237. blendFactor = GPUBlendFactor.One;
  238. break;
  239. case SrcColorFactor:
  240. blendFactor = GPUBlendFactor.Src;
  241. break;
  242. case OneMinusSrcColorFactor:
  243. blendFactor = GPUBlendFactor.OneMinusSrc;
  244. break;
  245. case SrcAlphaFactor:
  246. blendFactor = GPUBlendFactor.SrcAlpha;
  247. break;
  248. case OneMinusSrcAlphaFactor:
  249. blendFactor = GPUBlendFactor.OneMinusSrcAlpha;
  250. break;
  251. case DstColorFactor:
  252. blendFactor = GPUBlendFactor.Dst;
  253. break;
  254. case OneMinusDstColorFactor:
  255. blendFactor = GPUBlendFactor.OneMinusDstColor;
  256. break;
  257. case DstAlphaFactor:
  258. blendFactor = GPUBlendFactor.DstAlpha;
  259. break;
  260. case OneMinusDstAlphaFactor:
  261. blendFactor = GPUBlendFactor.OneMinusDstAlpha;
  262. break;
  263. case SrcAlphaSaturateFactor:
  264. blendFactor = GPUBlendFactor.SrcAlphaSaturated;
  265. break;
  266. case BlendColorFactor:
  267. blendFactor = GPUBlendFactor.Constant;
  268. break;
  269. case OneMinusBlendColorFactor:
  270. blendFactor = GPUBlendFactor.OneMinusConstant;
  271. break;
  272. default:
  273. console.error( 'THREE.WebGPURenderer: Blend factor not supported.', blend );
  274. }
  275. return blendFactor;
  276. }
  277. _getStencilCompare( material ) {
  278. let stencilCompare;
  279. const stencilFunc = material.stencilFunc;
  280. switch ( stencilFunc ) {
  281. case NeverStencilFunc:
  282. stencilCompare = GPUCompareFunction.Never;
  283. break;
  284. case AlwaysStencilFunc:
  285. stencilCompare = GPUCompareFunction.Always;
  286. break;
  287. case LessStencilFunc:
  288. stencilCompare = GPUCompareFunction.Less;
  289. break;
  290. case LessEqualStencilFunc:
  291. stencilCompare = GPUCompareFunction.LessEqual;
  292. break;
  293. case EqualStencilFunc:
  294. stencilCompare = GPUCompareFunction.Equal;
  295. break;
  296. case GreaterEqualStencilFunc:
  297. stencilCompare = GPUCompareFunction.GreaterEqual;
  298. break;
  299. case GreaterStencilFunc:
  300. stencilCompare = GPUCompareFunction.Greater;
  301. break;
  302. case NotEqualStencilFunc:
  303. stencilCompare = GPUCompareFunction.NotEqual;
  304. break;
  305. default:
  306. console.error( 'THREE.WebGPURenderer: Invalid stencil function.', stencilFunc );
  307. }
  308. return stencilCompare;
  309. }
  310. _getStencilOperation( op ) {
  311. let stencilOperation;
  312. switch ( op ) {
  313. case KeepStencilOp:
  314. stencilOperation = GPUStencilOperation.Keep;
  315. break;
  316. case ZeroStencilOp:
  317. stencilOperation = GPUStencilOperation.Zero;
  318. break;
  319. case ReplaceStencilOp:
  320. stencilOperation = GPUStencilOperation.Replace;
  321. break;
  322. case InvertStencilOp:
  323. stencilOperation = GPUStencilOperation.Invert;
  324. break;
  325. case IncrementStencilOp:
  326. stencilOperation = GPUStencilOperation.IncrementClamp;
  327. break;
  328. case DecrementStencilOp:
  329. stencilOperation = GPUStencilOperation.DecrementClamp;
  330. break;
  331. case IncrementWrapStencilOp:
  332. stencilOperation = GPUStencilOperation.IncrementWrap;
  333. break;
  334. case DecrementWrapStencilOp:
  335. stencilOperation = GPUStencilOperation.DecrementWrap;
  336. break;
  337. default:
  338. console.error( 'THREE.WebGPURenderer: Invalid stencil operation.', stencilOperation );
  339. }
  340. return stencilOperation;
  341. }
  342. _getBlendOperation( blendEquation ) {
  343. let blendOperation;
  344. switch ( blendEquation ) {
  345. case AddEquation:
  346. blendOperation = GPUBlendOperation.Add;
  347. break;
  348. case SubtractEquation:
  349. blendOperation = GPUBlendOperation.Subtract;
  350. break;
  351. case ReverseSubtractEquation:
  352. blendOperation = GPUBlendOperation.ReverseSubtract;
  353. break;
  354. case MinEquation:
  355. blendOperation = GPUBlendOperation.Min;
  356. break;
  357. case MaxEquation:
  358. blendOperation = GPUBlendOperation.Max;
  359. break;
  360. default:
  361. console.error( 'THREE.WebGPUPipelineUtils: Blend equation not supported.', blendEquation );
  362. }
  363. return blendOperation;
  364. }
  365. _getPrimitiveState( object, geometry, material ) {
  366. const descriptor = {};
  367. const utils = this.backend.utils;
  368. descriptor.topology = utils.getPrimitiveTopology( object, material );
  369. if ( geometry.index !== null && object.isLine === true && object.isLineSegments !== true ) {
  370. descriptor.stripIndexFormat = ( geometry.index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;
  371. }
  372. switch ( material.side ) {
  373. case FrontSide:
  374. descriptor.frontFace = GPUFrontFace.CCW;
  375. descriptor.cullMode = GPUCullMode.Back;
  376. break;
  377. case BackSide:
  378. descriptor.frontFace = GPUFrontFace.CCW;
  379. descriptor.cullMode = GPUCullMode.Front;
  380. break;
  381. case DoubleSide:
  382. descriptor.frontFace = GPUFrontFace.CCW;
  383. descriptor.cullMode = GPUCullMode.None;
  384. break;
  385. default:
  386. console.error( 'THREE.WebGPUPipelineUtils: Unknown material.side value.', material.side );
  387. break;
  388. }
  389. return descriptor;
  390. }
  391. _getColorWriteMask( material ) {
  392. return ( material.colorWrite === true ) ? GPUColorWriteFlags.All : GPUColorWriteFlags.None;
  393. }
  394. _getDepthCompare( material ) {
  395. let depthCompare;
  396. if ( material.depthTest === false ) {
  397. depthCompare = GPUCompareFunction.Always;
  398. } else {
  399. const depthFunc = material.depthFunc;
  400. switch ( depthFunc ) {
  401. case NeverDepth:
  402. depthCompare = GPUCompareFunction.Never;
  403. break;
  404. case AlwaysDepth:
  405. depthCompare = GPUCompareFunction.Always;
  406. break;
  407. case LessDepth:
  408. depthCompare = GPUCompareFunction.Less;
  409. break;
  410. case LessEqualDepth:
  411. depthCompare = GPUCompareFunction.LessEqual;
  412. break;
  413. case EqualDepth:
  414. depthCompare = GPUCompareFunction.Equal;
  415. break;
  416. case GreaterEqualDepth:
  417. depthCompare = GPUCompareFunction.GreaterEqual;
  418. break;
  419. case GreaterDepth:
  420. depthCompare = GPUCompareFunction.Greater;
  421. break;
  422. case NotEqualDepth:
  423. depthCompare = GPUCompareFunction.NotEqual;
  424. break;
  425. default:
  426. console.error( 'THREE.WebGPUPipelineUtils: Invalid depth function.', depthFunc );
  427. }
  428. }
  429. return depthCompare;
  430. }
  431. }
  432. export default WebGPUPipelineUtils;