WebGPUPipelineUtils.js 15 KB

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