WebGPUPipelineUtils.js 14 KB

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