WebGPUPipelineUtils.js 14 KB

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