WebGPUPipelineUtils.js 13 KB

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