WebGPUPipelineUtils.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 = 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. pipelineData.pipeline = device.createRenderPipeline( {
  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. }
  99. createComputePipeline( pipeline, bindings ) {
  100. const backend = this.backend;
  101. const device = backend.device;
  102. const computeProgram = backend.get( pipeline.computeProgram ).module;
  103. const pipelineGPU = backend.get( pipeline );
  104. const bindingsData = backend.get( bindings );
  105. pipelineGPU.pipeline = device.createComputePipeline( {
  106. compute: computeProgram,
  107. layout: device.createPipelineLayout( {
  108. bindGroupLayouts: [ bindingsData.layout ]
  109. } )
  110. } );
  111. }
  112. _getBlending( material ) {
  113. let color, alpha;
  114. const blending = material.blending;
  115. if ( blending === CustomBlending ) {
  116. const blendSrcAlpha = material.blendSrcAlpha !== null ? material.blendSrcAlpha : GPUBlendFactor.One;
  117. const blendDstAlpha = material.blendDstAlpha !== null ? material.blendDstAlpha : GPUBlendFactor.Zero;
  118. const blendEquationAlpha = material.blendEquationAlpha !== null ? material.blendEquationAlpha : GPUBlendFactor.Add;
  119. color = {
  120. srcFactor: this._getBlendFactor( material.blendSrc ),
  121. dstFactor: this._getBlendFactor( material.blendDst ),
  122. operation: this._getBlendOperation( material.blendEquation )
  123. };
  124. alpha = {
  125. srcFactor: this._getBlendFactor( blendSrcAlpha ),
  126. dstFactor: this._getBlendFactor( blendDstAlpha ),
  127. operation: this._getBlendOperation( blendEquationAlpha )
  128. };
  129. } else {
  130. const premultipliedAlpha = material.premultipliedAlpha;
  131. const setBlend = ( srcRGB, dstRGB, srcAlpha, dstAlpha ) => {
  132. color = {
  133. srcFactor: srcRGB,
  134. dstFactor: dstRGB,
  135. operation: GPUBlendOperation.Add
  136. };
  137. alpha = {
  138. srcFactor: srcAlpha,
  139. dstFactor: dstAlpha,
  140. operation: GPUBlendOperation.Add
  141. };
  142. };
  143. if ( premultipliedAlpha ) {
  144. switch ( blending ) {
  145. case NormalBlending:
  146. setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.OneMinusSrcAlpha, GPUBlendFactor.One, GPUBlendFactor.OneMinusSrcAlpha );
  147. break;
  148. case AdditiveBlending:
  149. setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.One, GPUBlendFactor.One, GPUBlendFactor.One );
  150. break;
  151. case SubtractiveBlending:
  152. setBlend( GPUBlendFactor.Zero, GPUBlendFactor.OneMinusSrc, GPUBlendFactor.Zero, GPUBlendFactor.One );
  153. break;
  154. case MultiplyBlending:
  155. setBlend( GPUBlendFactor.Zero, GPUBlendFactor.Src, GPUBlendFactor.Zero, GPUBlendFactor.SrcAlpha );
  156. break;
  157. }
  158. } else {
  159. switch ( blending ) {
  160. case NormalBlending:
  161. setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.OneMinusSrcAlpha, GPUBlendFactor.One, GPUBlendFactor.OneMinusSrcAlpha );
  162. break;
  163. case AdditiveBlending:
  164. setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.One, GPUBlendFactor.SrcAlpha, GPUBlendFactor.One );
  165. break;
  166. case SubtractiveBlending:
  167. setBlend( GPUBlendFactor.Zero, GPUBlendFactor.OneMinusSrc, GPUBlendFactor.Zero, GPUBlendFactor.One );
  168. break;
  169. case MultiplyBlending:
  170. setBlend( GPUBlendFactor.Zero, GPUBlendFactor.Src, GPUBlendFactor.Zero, GPUBlendFactor.Src );
  171. break;
  172. }
  173. }
  174. }
  175. if ( color !== undefined && alpha !== undefined ) {
  176. return { color, alpha };
  177. } else {
  178. console.error( 'THREE.WebGPURenderer: Invalid blending: ', blending );
  179. }
  180. }
  181. _getBlendFactor( blend ) {
  182. let blendFactor;
  183. switch ( blend ) {
  184. case ZeroFactor:
  185. blendFactor = GPUBlendFactor.Zero;
  186. break;
  187. case OneFactor:
  188. blendFactor = GPUBlendFactor.One;
  189. break;
  190. case SrcColorFactor:
  191. blendFactor = GPUBlendFactor.Src;
  192. break;
  193. case OneMinusSrcColorFactor:
  194. blendFactor = GPUBlendFactor.OneMinusSrc;
  195. break;
  196. case SrcAlphaFactor:
  197. blendFactor = GPUBlendFactor.SrcAlpha;
  198. break;
  199. case OneMinusSrcAlphaFactor:
  200. blendFactor = GPUBlendFactor.OneMinusSrcAlpha;
  201. break;
  202. case DstColorFactor:
  203. blendFactor = GPUBlendFactor.Dst;
  204. break;
  205. case OneMinusDstColorFactor:
  206. blendFactor = GPUBlendFactor.OneMinusDstColor;
  207. break;
  208. case DstAlphaFactor:
  209. blendFactor = GPUBlendFactor.DstAlpha;
  210. break;
  211. case OneMinusDstAlphaFactor:
  212. blendFactor = GPUBlendFactor.OneMinusDstAlpha;
  213. break;
  214. case SrcAlphaSaturateFactor:
  215. blendFactor = GPUBlendFactor.SrcAlphaSaturated;
  216. break;
  217. case BlendColorFactor:
  218. blendFactor = GPUBlendFactor.Constant;
  219. break;
  220. case OneMinusBlendColorFactor:
  221. blendFactor = GPUBlendFactor.OneMinusConstant;
  222. break;
  223. default:
  224. console.error( 'THREE.WebGPURenderer: Blend factor not supported.', blend );
  225. }
  226. return blendFactor;
  227. }
  228. _getStencilCompare( material ) {
  229. let stencilCompare;
  230. const stencilFunc = material.stencilFunc;
  231. switch ( stencilFunc ) {
  232. case NeverStencilFunc:
  233. stencilCompare = GPUCompareFunction.Never;
  234. break;
  235. case AlwaysStencilFunc:
  236. stencilCompare = GPUCompareFunction.Always;
  237. break;
  238. case LessStencilFunc:
  239. stencilCompare = GPUCompareFunction.Less;
  240. break;
  241. case LessEqualStencilFunc:
  242. stencilCompare = GPUCompareFunction.LessEqual;
  243. break;
  244. case EqualStencilFunc:
  245. stencilCompare = GPUCompareFunction.Equal;
  246. break;
  247. case GreaterEqualStencilFunc:
  248. stencilCompare = GPUCompareFunction.GreaterEqual;
  249. break;
  250. case GreaterStencilFunc:
  251. stencilCompare = GPUCompareFunction.Greater;
  252. break;
  253. case NotEqualStencilFunc:
  254. stencilCompare = GPUCompareFunction.NotEqual;
  255. break;
  256. default:
  257. console.error( 'THREE.WebGPURenderer: Invalid stencil function.', stencilFunc );
  258. }
  259. return stencilCompare;
  260. }
  261. _getStencilOperation( op ) {
  262. let stencilOperation;
  263. switch ( op ) {
  264. case KeepStencilOp:
  265. stencilOperation = GPUStencilOperation.Keep;
  266. break;
  267. case ZeroStencilOp:
  268. stencilOperation = GPUStencilOperation.Zero;
  269. break;
  270. case ReplaceStencilOp:
  271. stencilOperation = GPUStencilOperation.Replace;
  272. break;
  273. case InvertStencilOp:
  274. stencilOperation = GPUStencilOperation.Invert;
  275. break;
  276. case IncrementStencilOp:
  277. stencilOperation = GPUStencilOperation.IncrementClamp;
  278. break;
  279. case DecrementStencilOp:
  280. stencilOperation = GPUStencilOperation.DecrementClamp;
  281. break;
  282. case IncrementWrapStencilOp:
  283. stencilOperation = GPUStencilOperation.IncrementWrap;
  284. break;
  285. case DecrementWrapStencilOp:
  286. stencilOperation = GPUStencilOperation.DecrementWrap;
  287. break;
  288. default:
  289. console.error( 'THREE.WebGPURenderer: Invalid stencil operation.', stencilOperation );
  290. }
  291. return stencilOperation;
  292. }
  293. _getBlendOperation( blendEquation ) {
  294. let blendOperation;
  295. switch ( blendEquation ) {
  296. case AddEquation:
  297. blendOperation = GPUBlendOperation.Add;
  298. break;
  299. case SubtractEquation:
  300. blendOperation = GPUBlendOperation.Subtract;
  301. break;
  302. case ReverseSubtractEquation:
  303. blendOperation = GPUBlendOperation.ReverseSubtract;
  304. break;
  305. case MinEquation:
  306. blendOperation = GPUBlendOperation.Min;
  307. break;
  308. case MaxEquation:
  309. blendOperation = GPUBlendOperation.Max;
  310. break;
  311. default:
  312. console.error( 'THREE.WebGPUPipelineUtils: Blend equation not supported.', blendEquation );
  313. }
  314. return blendOperation;
  315. }
  316. _getPrimitiveState( object, geometry, material ) {
  317. const descriptor = {};
  318. const utils = this.backend.utils;
  319. descriptor.topology = utils.getPrimitiveTopology( object, material );
  320. if ( geometry.index !== null && object.isLine === true && object.isLineSegments !== true ) {
  321. descriptor.stripIndexFormat = ( geometry.index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;
  322. }
  323. switch ( material.side ) {
  324. case FrontSide:
  325. descriptor.frontFace = GPUFrontFace.CCW;
  326. descriptor.cullMode = GPUCullMode.Back;
  327. break;
  328. case BackSide:
  329. descriptor.frontFace = GPUFrontFace.CCW;
  330. descriptor.cullMode = GPUCullMode.Front;
  331. break;
  332. case DoubleSide:
  333. descriptor.frontFace = GPUFrontFace.CCW;
  334. descriptor.cullMode = GPUCullMode.None;
  335. break;
  336. default:
  337. console.error( 'THREE.WebGPUPipelineUtils: Unknown material.side value.', material.side );
  338. break;
  339. }
  340. return descriptor;
  341. }
  342. _getColorWriteMask( material ) {
  343. return ( material.colorWrite === true ) ? GPUColorWriteFlags.All : GPUColorWriteFlags.None;
  344. }
  345. _getDepthCompare( material ) {
  346. let depthCompare;
  347. if ( material.depthTest === false ) {
  348. depthCompare = GPUCompareFunction.Always;
  349. } else {
  350. const depthFunc = material.depthFunc;
  351. switch ( depthFunc ) {
  352. case NeverDepth:
  353. depthCompare = GPUCompareFunction.Never;
  354. break;
  355. case AlwaysDepth:
  356. depthCompare = GPUCompareFunction.Always;
  357. break;
  358. case LessDepth:
  359. depthCompare = GPUCompareFunction.Less;
  360. break;
  361. case LessEqualDepth:
  362. depthCompare = GPUCompareFunction.LessEqual;
  363. break;
  364. case EqualDepth:
  365. depthCompare = GPUCompareFunction.Equal;
  366. break;
  367. case GreaterEqualDepth:
  368. depthCompare = GPUCompareFunction.GreaterEqual;
  369. break;
  370. case GreaterDepth:
  371. depthCompare = GPUCompareFunction.Greater;
  372. break;
  373. case NotEqualDepth:
  374. depthCompare = GPUCompareFunction.NotEqual;
  375. break;
  376. default:
  377. console.error( 'THREE.WebGPUPipelineUtils: Invalid depth function.', depthFunc );
  378. }
  379. }
  380. return depthCompare;
  381. }
  382. }
  383. export default WebGPUPipelineUtils;