WebGPUPipelineUtils.js 14 KB

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