WebGPUPipelineUtils.js 13 KB

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