WebGPUPipelineUtils.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. const blendSrc = material.blendSrc;
  152. const blendDst = material.blendDst;
  153. const blendEquation = material.blendEquation;
  154. if ( blending === CustomBlending ) {
  155. const blendSrcAlpha = material.blendSrcAlpha !== null ? material.blendSrcAlpha : blendSrc;
  156. const blendDstAlpha = material.blendDstAlpha !== null ? material.blendDstAlpha : blendDst;
  157. const blendEquationAlpha = material.blendEquationAlpha !== null ? material.blendEquationAlpha : blendEquation;
  158. color = {
  159. srcFactor: this._getBlendFactor( blendSrc ),
  160. dstFactor: this._getBlendFactor( blendDst ),
  161. operation: this._getBlendOperation( blendEquation )
  162. };
  163. alpha = {
  164. srcFactor: this._getBlendFactor( blendSrcAlpha ),
  165. dstFactor: this._getBlendFactor( blendDstAlpha ),
  166. operation: this._getBlendOperation( blendEquationAlpha )
  167. };
  168. } else {
  169. const premultipliedAlpha = material.premultipliedAlpha;
  170. const setBlend = ( srcRGB, dstRGB, srcAlpha, dstAlpha ) => {
  171. color = {
  172. srcFactor: srcRGB,
  173. dstFactor: dstRGB,
  174. operation: GPUBlendOperation.Add
  175. };
  176. alpha = {
  177. srcFactor: srcAlpha,
  178. dstFactor: dstAlpha,
  179. operation: GPUBlendOperation.Add
  180. };
  181. };
  182. if ( premultipliedAlpha ) {
  183. switch ( blending ) {
  184. case NormalBlending:
  185. setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.OneMinusSrcAlpha, GPUBlendFactor.One, GPUBlendFactor.OneMinusSrcAlpha );
  186. break;
  187. case AdditiveBlending:
  188. setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.One, GPUBlendFactor.One, GPUBlendFactor.One );
  189. break;
  190. case SubtractiveBlending:
  191. setBlend( GPUBlendFactor.Zero, GPUBlendFactor.OneMinusSrc, GPUBlendFactor.Zero, GPUBlendFactor.One );
  192. break;
  193. case MultiplyBlending:
  194. setBlend( GPUBlendFactor.Zero, GPUBlendFactor.Src, GPUBlendFactor.Zero, GPUBlendFactor.SrcAlpha );
  195. break;
  196. }
  197. } else {
  198. switch ( blending ) {
  199. case NormalBlending:
  200. setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.OneMinusSrcAlpha, GPUBlendFactor.One, GPUBlendFactor.OneMinusSrcAlpha );
  201. break;
  202. case AdditiveBlending:
  203. setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.One, GPUBlendFactor.SrcAlpha, GPUBlendFactor.One );
  204. break;
  205. case SubtractiveBlending:
  206. setBlend( GPUBlendFactor.Zero, GPUBlendFactor.OneMinusSrc, GPUBlendFactor.Zero, GPUBlendFactor.One );
  207. break;
  208. case MultiplyBlending:
  209. setBlend( GPUBlendFactor.Zero, GPUBlendFactor.Src, GPUBlendFactor.Zero, GPUBlendFactor.Src );
  210. break;
  211. }
  212. }
  213. }
  214. if ( color !== undefined && alpha !== undefined ) {
  215. return { color, alpha };
  216. } else {
  217. console.error( 'THREE.WebGPURenderer: Invalid blending: ', blending );
  218. }
  219. }
  220. _getBlendFactor( blend ) {
  221. let blendFactor;
  222. switch ( blend ) {
  223. case ZeroFactor:
  224. blendFactor = GPUBlendFactor.Zero;
  225. break;
  226. case OneFactor:
  227. blendFactor = GPUBlendFactor.One;
  228. break;
  229. case SrcColorFactor:
  230. blendFactor = GPUBlendFactor.Src;
  231. break;
  232. case OneMinusSrcColorFactor:
  233. blendFactor = GPUBlendFactor.OneMinusSrc;
  234. break;
  235. case SrcAlphaFactor:
  236. blendFactor = GPUBlendFactor.SrcAlpha;
  237. break;
  238. case OneMinusSrcAlphaFactor:
  239. blendFactor = GPUBlendFactor.OneMinusSrcAlpha;
  240. break;
  241. case DstColorFactor:
  242. blendFactor = GPUBlendFactor.Dst;
  243. break;
  244. case OneMinusDstColorFactor:
  245. blendFactor = GPUBlendFactor.OneMinusDstColor;
  246. break;
  247. case DstAlphaFactor:
  248. blendFactor = GPUBlendFactor.DstAlpha;
  249. break;
  250. case OneMinusDstAlphaFactor:
  251. blendFactor = GPUBlendFactor.OneMinusDstAlpha;
  252. break;
  253. case SrcAlphaSaturateFactor:
  254. blendFactor = GPUBlendFactor.SrcAlphaSaturated;
  255. break;
  256. case BlendColorFactor:
  257. blendFactor = GPUBlendFactor.Constant;
  258. break;
  259. case OneMinusBlendColorFactor:
  260. blendFactor = GPUBlendFactor.OneMinusConstant;
  261. break;
  262. default:
  263. console.error( 'THREE.WebGPURenderer: Blend factor not supported.', blend );
  264. }
  265. return blendFactor;
  266. }
  267. _getStencilCompare( material ) {
  268. let stencilCompare;
  269. const stencilFunc = material.stencilFunc;
  270. switch ( stencilFunc ) {
  271. case NeverStencilFunc:
  272. stencilCompare = GPUCompareFunction.Never;
  273. break;
  274. case AlwaysStencilFunc:
  275. stencilCompare = GPUCompareFunction.Always;
  276. break;
  277. case LessStencilFunc:
  278. stencilCompare = GPUCompareFunction.Less;
  279. break;
  280. case LessEqualStencilFunc:
  281. stencilCompare = GPUCompareFunction.LessEqual;
  282. break;
  283. case EqualStencilFunc:
  284. stencilCompare = GPUCompareFunction.Equal;
  285. break;
  286. case GreaterEqualStencilFunc:
  287. stencilCompare = GPUCompareFunction.GreaterEqual;
  288. break;
  289. case GreaterStencilFunc:
  290. stencilCompare = GPUCompareFunction.Greater;
  291. break;
  292. case NotEqualStencilFunc:
  293. stencilCompare = GPUCompareFunction.NotEqual;
  294. break;
  295. default:
  296. console.error( 'THREE.WebGPURenderer: Invalid stencil function.', stencilFunc );
  297. }
  298. return stencilCompare;
  299. }
  300. _getStencilOperation( op ) {
  301. let stencilOperation;
  302. switch ( op ) {
  303. case KeepStencilOp:
  304. stencilOperation = GPUStencilOperation.Keep;
  305. break;
  306. case ZeroStencilOp:
  307. stencilOperation = GPUStencilOperation.Zero;
  308. break;
  309. case ReplaceStencilOp:
  310. stencilOperation = GPUStencilOperation.Replace;
  311. break;
  312. case InvertStencilOp:
  313. stencilOperation = GPUStencilOperation.Invert;
  314. break;
  315. case IncrementStencilOp:
  316. stencilOperation = GPUStencilOperation.IncrementClamp;
  317. break;
  318. case DecrementStencilOp:
  319. stencilOperation = GPUStencilOperation.DecrementClamp;
  320. break;
  321. case IncrementWrapStencilOp:
  322. stencilOperation = GPUStencilOperation.IncrementWrap;
  323. break;
  324. case DecrementWrapStencilOp:
  325. stencilOperation = GPUStencilOperation.DecrementWrap;
  326. break;
  327. default:
  328. console.error( 'THREE.WebGPURenderer: Invalid stencil operation.', stencilOperation );
  329. }
  330. return stencilOperation;
  331. }
  332. _getBlendOperation( blendEquation ) {
  333. let blendOperation;
  334. switch ( blendEquation ) {
  335. case AddEquation:
  336. blendOperation = GPUBlendOperation.Add;
  337. break;
  338. case SubtractEquation:
  339. blendOperation = GPUBlendOperation.Subtract;
  340. break;
  341. case ReverseSubtractEquation:
  342. blendOperation = GPUBlendOperation.ReverseSubtract;
  343. break;
  344. case MinEquation:
  345. blendOperation = GPUBlendOperation.Min;
  346. break;
  347. case MaxEquation:
  348. blendOperation = GPUBlendOperation.Max;
  349. break;
  350. default:
  351. console.error( 'THREE.WebGPUPipelineUtils: Blend equation not supported.', blendEquation );
  352. }
  353. return blendOperation;
  354. }
  355. _getPrimitiveState( object, geometry, material ) {
  356. const descriptor = {};
  357. const utils = this.backend.utils;
  358. descriptor.topology = utils.getPrimitiveTopology( object, material );
  359. if ( geometry.index !== null && object.isLine === true && object.isLineSegments !== true ) {
  360. descriptor.stripIndexFormat = ( geometry.index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;
  361. }
  362. switch ( material.side ) {
  363. case FrontSide:
  364. descriptor.frontFace = GPUFrontFace.CCW;
  365. descriptor.cullMode = GPUCullMode.Back;
  366. break;
  367. case BackSide:
  368. descriptor.frontFace = GPUFrontFace.CCW;
  369. descriptor.cullMode = GPUCullMode.Front;
  370. break;
  371. case DoubleSide:
  372. descriptor.frontFace = GPUFrontFace.CCW;
  373. descriptor.cullMode = GPUCullMode.None;
  374. break;
  375. default:
  376. console.error( 'THREE.WebGPUPipelineUtils: Unknown material.side value.', material.side );
  377. break;
  378. }
  379. return descriptor;
  380. }
  381. _getColorWriteMask( material ) {
  382. return ( material.colorWrite === true ) ? GPUColorWriteFlags.All : GPUColorWriteFlags.None;
  383. }
  384. _getDepthCompare( material ) {
  385. let depthCompare;
  386. if ( material.depthTest === false ) {
  387. depthCompare = GPUCompareFunction.Always;
  388. } else {
  389. const depthFunc = material.depthFunc;
  390. switch ( depthFunc ) {
  391. case NeverDepth:
  392. depthCompare = GPUCompareFunction.Never;
  393. break;
  394. case AlwaysDepth:
  395. depthCompare = GPUCompareFunction.Always;
  396. break;
  397. case LessDepth:
  398. depthCompare = GPUCompareFunction.Less;
  399. break;
  400. case LessEqualDepth:
  401. depthCompare = GPUCompareFunction.LessEqual;
  402. break;
  403. case EqualDepth:
  404. depthCompare = GPUCompareFunction.Equal;
  405. break;
  406. case GreaterEqualDepth:
  407. depthCompare = GPUCompareFunction.GreaterEqual;
  408. break;
  409. case GreaterDepth:
  410. depthCompare = GPUCompareFunction.Greater;
  411. break;
  412. case NotEqualDepth:
  413. depthCompare = GPUCompareFunction.NotEqual;
  414. break;
  415. default:
  416. console.error( 'THREE.WebGPUPipelineUtils: Invalid depth function.', depthFunc );
  417. }
  418. }
  419. return depthCompare;
  420. }
  421. }
  422. export default WebGPUPipelineUtils;