WebGPUPipelineUtils.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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 ) {
  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. // determine shader attributes
  27. const shaderAttributes = backend.attributeUtils.createShaderAttributes( renderObject );
  28. // vertex buffers
  29. const vertexBuffers = [];
  30. for ( const attribute of shaderAttributes ) {
  31. vertexBuffers.push( {
  32. arrayStride: attribute.arrayStride,
  33. attributes: [ { shaderLocation: attribute.slot, offset: attribute.offset, format: attribute.format } ],
  34. stepMode: attribute.stepMode
  35. } );
  36. }
  37. // blending
  38. let blending;
  39. if ( material.transparent === true && material.blending !== NoBlending ) {
  40. blending = {
  41. alpha: this._getAlphaBlend( material ),
  42. color: this._getColorBlend( material )
  43. };
  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. //
  56. const vertexModule = backend.get( vertexProgram ).module;
  57. const fragmentModule = backend.get( fragmentProgram ).module;
  58. const primitiveState = this._getPrimitiveState( object, geometry, material );
  59. const colorWriteMask = this._getColorWriteMask( material );
  60. const depthCompare = this._getDepthCompare( material );
  61. const colorFormat = utils.getCurrentColorFormat( renderObject.context );
  62. const depthStencilFormat = utils.getCurrentDepthStencilFormat( renderObject.context );
  63. const sampleCount = utils.getSampleCount( renderObject.context );
  64. pipelineData.pipeline = device.createRenderPipeline( {
  65. vertex: Object.assign( {}, vertexModule, { buffers: vertexBuffers } ),
  66. fragment: Object.assign( {}, fragmentModule, { targets: [ {
  67. format: colorFormat,
  68. blend: blending,
  69. writeMask: colorWriteMask
  70. } ] } ),
  71. primitive: primitiveState,
  72. depthStencil: {
  73. format: depthStencilFormat,
  74. depthWriteEnabled: material.depthWrite,
  75. depthCompare: depthCompare,
  76. stencilFront: stencilFront,
  77. stencilBack: {}, // three.js does not provide an API to configure the back function (gl.stencilFuncSeparate() was never used)
  78. stencilReadMask: material.stencilFuncMask,
  79. stencilWriteMask: material.stencilWriteMask
  80. },
  81. multisample: {
  82. count: sampleCount
  83. },
  84. layout: 'auto'
  85. } );
  86. }
  87. createComputePipeline( pipeline ) {
  88. const backend = this.backend;
  89. const device = backend.device;
  90. const computeProgram = backend.get( pipeline.computeProgram ).module;
  91. const pipelineGPU = backend.get( pipeline );
  92. pipelineGPU.pipeline = device.createComputePipeline( {
  93. compute: computeProgram,
  94. layout: 'auto'
  95. } );
  96. }
  97. _getAlphaBlend( material ) {
  98. const blending = material.blending;
  99. const premultipliedAlpha = material.premultipliedAlpha;
  100. let alphaBlend = undefined;
  101. switch ( blending ) {
  102. case NormalBlending:
  103. if ( premultipliedAlpha === false ) {
  104. alphaBlend = {
  105. srcFactor: GPUBlendFactor.One,
  106. dstFactor: GPUBlendFactor.OneMinusSrcAlpha,
  107. operation: GPUBlendOperation.Add
  108. };
  109. }
  110. break;
  111. case AdditiveBlending:
  112. alphaBlend = {
  113. srcFactor: GPUBlendFactor.Zero,
  114. dstFactor: GPUBlendFactor.One,
  115. operation: GPUBlendOperation.Add
  116. };
  117. break;
  118. case SubtractiveBlending:
  119. if ( premultipliedAlpha === true ) {
  120. alphaBlend = {
  121. srcFactor: GPUBlendFactor.OneMinusSrcColor,
  122. dstFactor: GPUBlendFactor.OneMinusSrcAlpha,
  123. operation: GPUBlendOperation.Add
  124. };
  125. }
  126. break;
  127. case MultiplyBlending:
  128. if ( premultipliedAlpha === true ) {
  129. alphaBlend = {
  130. srcFactor: GPUBlendFactor.Zero,
  131. dstFactor: GPUBlendFactor.SrcAlpha,
  132. operation: GPUBlendOperation.Add
  133. };
  134. }
  135. break;
  136. case CustomBlending:
  137. const blendSrcAlpha = material.blendSrcAlpha;
  138. const blendDstAlpha = material.blendDstAlpha;
  139. const blendEquationAlpha = material.blendEquationAlpha;
  140. if ( blendSrcAlpha !== null && blendDstAlpha !== null && blendEquationAlpha !== null ) {
  141. alphaBlend = {
  142. srcFactor: this._getBlendFactor( blendSrcAlpha ),
  143. dstFactor: this._getBlendFactor( blendDstAlpha ),
  144. operation: this._getBlendOperation( blendEquationAlpha )
  145. };
  146. }
  147. break;
  148. default:
  149. console.error( 'THREE.WebGPURenderer: Blending not supported.', blending );
  150. }
  151. return alphaBlend;
  152. }
  153. _getBlendFactor( blend ) {
  154. let blendFactor;
  155. switch ( blend ) {
  156. case ZeroFactor:
  157. blendFactor = GPUBlendFactor.Zero;
  158. break;
  159. case OneFactor:
  160. blendFactor = GPUBlendFactor.One;
  161. break;
  162. case SrcColorFactor:
  163. blendFactor = GPUBlendFactor.SrcColor;
  164. break;
  165. case OneMinusSrcColorFactor:
  166. blendFactor = GPUBlendFactor.OneMinusSrcColor;
  167. break;
  168. case SrcAlphaFactor:
  169. blendFactor = GPUBlendFactor.SrcAlpha;
  170. break;
  171. case OneMinusSrcAlphaFactor:
  172. blendFactor = GPUBlendFactor.OneMinusSrcAlpha;
  173. break;
  174. case DstColorFactor:
  175. blendFactor = GPUBlendFactor.DstColor;
  176. break;
  177. case OneMinusDstColorFactor:
  178. blendFactor = GPUBlendFactor.OneMinusDstColor;
  179. break;
  180. case DstAlphaFactor:
  181. blendFactor = GPUBlendFactor.DstAlpha;
  182. break;
  183. case OneMinusDstAlphaFactor:
  184. blendFactor = GPUBlendFactor.OneMinusDstAlpha;
  185. break;
  186. case SrcAlphaSaturateFactor:
  187. blendFactor = GPUBlendFactor.SrcAlphaSaturated;
  188. break;
  189. case BlendColorFactor:
  190. blendFactor = GPUBlendFactor.BlendColor;
  191. break;
  192. case OneMinusBlendColorFactor:
  193. blendFactor = GPUBlendFactor.OneMinusBlendColor;
  194. break;
  195. default:
  196. console.error( 'THREE.WebGPURenderer: Blend factor not supported.', blend );
  197. }
  198. return blendFactor;
  199. }
  200. _getColorBlend( material ) {
  201. const blending = material.blending;
  202. const premultipliedAlpha = material.premultipliedAlpha;
  203. const colorBlend = {
  204. srcFactor: null,
  205. dstFactor: null,
  206. operation: null
  207. };
  208. switch ( blending ) {
  209. case NormalBlending:
  210. colorBlend.srcFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.One : GPUBlendFactor.SrcAlpha;
  211. colorBlend.dstFactor = GPUBlendFactor.OneMinusSrcAlpha;
  212. colorBlend.operation = GPUBlendOperation.Add;
  213. break;
  214. case AdditiveBlending:
  215. colorBlend.srcFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.One : GPUBlendFactor.SrcAlpha;
  216. colorBlend.dstFactor = GPUBlendFactor.One;
  217. colorBlend.operation = GPUBlendOperation.Add;
  218. break;
  219. case SubtractiveBlending:
  220. colorBlend.srcFactor = GPUBlendFactor.Zero;
  221. colorBlend.dstFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.Zero : GPUBlendFactor.OneMinusSrcColor;
  222. colorBlend.operation = GPUBlendOperation.Add;
  223. break;
  224. case MultiplyBlending:
  225. colorBlend.srcFactor = GPUBlendFactor.Zero;
  226. colorBlend.dstFactor = GPUBlendFactor.SrcColor;
  227. colorBlend.operation = GPUBlendOperation.Add;
  228. break;
  229. case CustomBlending:
  230. colorBlend.srcFactor = this._getBlendFactor( material.blendSrc );
  231. colorBlend.dstFactor = this._getBlendFactor( material.blendDst );
  232. colorBlend.operation = this._getBlendOperation( material.blendEquation );
  233. break;
  234. default:
  235. console.error( 'THREE.WebGPURenderer: Blending not supported.', blending );
  236. }
  237. return colorBlend;
  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 ( object.isLine === true && object.isLineSegments !== true ) {
  332. const count = ( geometry.index ) ? geometry.index.count : geometry.attributes.position.count;
  333. descriptor.stripIndexFormat = ( count > 65535 ) ? GPUIndexFormat.Uint32 : GPUIndexFormat.Uint16; // define data type for primitive restart value
  334. }
  335. switch ( material.side ) {
  336. case FrontSide:
  337. descriptor.frontFace = GPUFrontFace.CW;
  338. descriptor.cullMode = GPUCullMode.Front;
  339. break;
  340. case BackSide:
  341. descriptor.frontFace = GPUFrontFace.CW;
  342. descriptor.cullMode = GPUCullMode.Back;
  343. break;
  344. case DoubleSide:
  345. descriptor.frontFace = GPUFrontFace.CW;
  346. descriptor.cullMode = GPUCullMode.None;
  347. break;
  348. default:
  349. console.error( 'THREE.WebGPUPipelineUtils: Unknown material.side value.', material.side );
  350. break;
  351. }
  352. return descriptor;
  353. }
  354. _getColorWriteMask( material ) {
  355. return ( material.colorWrite === true ) ? GPUColorWriteFlags.All : GPUColorWriteFlags.None;
  356. }
  357. _getDepthCompare( material ) {
  358. let depthCompare;
  359. if ( material.depthTest === false ) {
  360. depthCompare = GPUCompareFunction.Always;
  361. } else {
  362. const depthFunc = material.depthFunc;
  363. switch ( depthFunc ) {
  364. case NeverDepth:
  365. depthCompare = GPUCompareFunction.Never;
  366. break;
  367. case AlwaysDepth:
  368. depthCompare = GPUCompareFunction.Always;
  369. break;
  370. case LessDepth:
  371. depthCompare = GPUCompareFunction.Less;
  372. break;
  373. case LessEqualDepth:
  374. depthCompare = GPUCompareFunction.LessEqual;
  375. break;
  376. case EqualDepth:
  377. depthCompare = GPUCompareFunction.Equal;
  378. break;
  379. case GreaterEqualDepth:
  380. depthCompare = GPUCompareFunction.GreaterEqual;
  381. break;
  382. case GreaterDepth:
  383. depthCompare = GPUCompareFunction.Greater;
  384. break;
  385. case NotEqualDepth:
  386. depthCompare = GPUCompareFunction.NotEqual;
  387. break;
  388. default:
  389. console.error( 'THREE.WebGPUPipelineUtils: Invalid depth function.', depthFunc );
  390. }
  391. }
  392. return depthCompare;
  393. }
  394. }
  395. export default WebGPUPipelineUtils;