WebGPURenderPipeline.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. import { GPUPrimitiveTopology, GPUIndexFormat, GPUCompareFunction, GPUFrontFace, GPUCullMode, GPUVertexFormat, GPUBlendFactor, GPUBlendOperation, BlendColorFactor, OneMinusBlendColorFactor, GPUColorWriteFlags, GPUStencilOperation, GPUInputStepMode } from './constants.js';
  2. import {
  3. FrontSide, BackSide, DoubleSide,
  4. NeverDepth, AlwaysDepth, LessDepth, LessEqualDepth, EqualDepth, GreaterEqualDepth, GreaterDepth, NotEqualDepth,
  5. NeverStencilFunc, AlwaysStencilFunc, LessStencilFunc, LessEqualStencilFunc, EqualStencilFunc, GreaterEqualStencilFunc, GreaterStencilFunc, NotEqualStencilFunc,
  6. KeepStencilOp, ZeroStencilOp, ReplaceStencilOp, InvertStencilOp, IncrementStencilOp, DecrementStencilOp, IncrementWrapStencilOp, DecrementWrapStencilOp,
  7. NoBlending, NormalBlending, AdditiveBlending, SubtractiveBlending, MultiplyBlending, CustomBlending,
  8. AddEquation, SubtractEquation, ReverseSubtractEquation, MinEquation, MaxEquation,
  9. ZeroFactor, OneFactor, SrcColorFactor, OneMinusSrcColorFactor, SrcAlphaFactor, OneMinusSrcAlphaFactor, DstAlphaFactor, OneMinusDstAlphaFactor, DstColorFactor, OneMinusDstColorFactor, SrcAlphaSaturateFactor
  10. } from 'three';
  11. class WebGPURenderPipeline {
  12. constructor( device, renderer, sampleCount ) {
  13. this.cacheKey = null;
  14. this.shaderAttributes = null;
  15. this.stageVertex = null;
  16. this.stageFragment = null;
  17. this.usedTimes = 0;
  18. this._device = device;
  19. this._renderer = renderer;
  20. this._sampleCount = sampleCount;
  21. }
  22. init( cacheKey, stageVertex, stageFragment, object, nodeBuilder ) {
  23. const material = object.material;
  24. const geometry = object.geometry;
  25. // determine shader attributes
  26. const shaderAttributes = this._parseShaderAttributes( nodeBuilder.vertexShader );
  27. // vertex buffers
  28. const vertexBuffers = [];
  29. for ( const attribute of shaderAttributes ) {
  30. const name = attribute.name;
  31. const geometryAttribute = geometry.getAttribute( name );
  32. const stepMode = ( geometryAttribute !== undefined && geometryAttribute.isInstancedBufferAttribute ) ? GPUInputStepMode.Instance : GPUInputStepMode.Vertex;
  33. vertexBuffers.push( {
  34. arrayStride: attribute.arrayStride,
  35. attributes: [ { shaderLocation: attribute.slot, offset: 0, format: attribute.format } ],
  36. stepMode: stepMode
  37. } );
  38. }
  39. this.cacheKey = cacheKey;
  40. this.shaderAttributes = shaderAttributes;
  41. this.stageVertex = stageVertex;
  42. this.stageFragment = stageFragment;
  43. // blending
  44. let alphaBlend = {};
  45. let colorBlend = {};
  46. if ( material.transparent === true && material.blending !== NoBlending ) {
  47. alphaBlend = this._getAlphaBlend( material );
  48. colorBlend = this._getColorBlend( material );
  49. }
  50. // stencil
  51. let stencilFront = {};
  52. if ( material.stencilWrite === true ) {
  53. stencilFront = {
  54. compare: this._getStencilCompare( material ),
  55. failOp: this._getStencilOperation( material.stencilFail ),
  56. depthFailOp: this._getStencilOperation( material.stencilZFail ),
  57. passOp: this._getStencilOperation( material.stencilZPass )
  58. };
  59. }
  60. //
  61. const primitiveState = this._getPrimitiveState( object, material );
  62. const colorWriteMask = this._getColorWriteMask( material );
  63. const depthCompare = this._getDepthCompare( material );
  64. const colorFormat = this._renderer.getCurrentColorFormat();
  65. const depthStencilFormat = this._renderer.getCurrentDepthStencilFormat();
  66. this.pipeline = this._device.createRenderPipeline( {
  67. vertex: Object.assign( {}, stageVertex.stage, { buffers: vertexBuffers } ),
  68. fragment: Object.assign( {}, stageFragment.stage, { targets: [ {
  69. format: colorFormat,
  70. blend: {
  71. alpha: alphaBlend,
  72. color: colorBlend
  73. },
  74. writeMask: colorWriteMask
  75. } ] } ),
  76. primitive: primitiveState,
  77. depthStencil: {
  78. format: depthStencilFormat,
  79. depthWriteEnabled: material.depthWrite,
  80. depthCompare: depthCompare,
  81. stencilFront: stencilFront,
  82. stencilBack: {}, // three.js does not provide an API to configure the back function (gl.stencilFuncSeparate() was never used)
  83. stencilReadMask: material.stencilFuncMask,
  84. stencilWriteMask: material.stencilWriteMask
  85. },
  86. multisample: {
  87. count: this._sampleCount
  88. }
  89. } );
  90. }
  91. _getArrayStride( type ) {
  92. // @TODO: This code is GLSL specific. We need to update when we switch to WGSL.
  93. if ( type === 'float' ) return 4;
  94. if ( type === 'vec2' ) return 8;
  95. if ( type === 'vec3' ) return 12;
  96. if ( type === 'vec4' ) return 16;
  97. if ( type === 'int' ) return 4;
  98. if ( type === 'ivec2' ) return 8;
  99. if ( type === 'ivec3' ) return 12;
  100. if ( type === 'ivec4' ) return 16;
  101. if ( type === 'uint' ) return 4;
  102. if ( type === 'uvec2' ) return 8;
  103. if ( type === 'uvec3' ) return 12;
  104. if ( type === 'uvec4' ) return 16;
  105. console.error( 'THREE.WebGPURenderer: Shader variable type not supported yet.', type );
  106. }
  107. _getAlphaBlend( material ) {
  108. const blending = material.blending;
  109. const premultipliedAlpha = material.premultipliedAlpha;
  110. let alphaBlend = undefined;
  111. switch ( blending ) {
  112. case NormalBlending:
  113. if ( premultipliedAlpha === false ) {
  114. alphaBlend = {
  115. srcFactor: GPUBlendFactor.One,
  116. dstFactor: GPUBlendFactor.OneMinusSrcAlpha,
  117. operation: GPUBlendOperation.Add
  118. };
  119. }
  120. break;
  121. case AdditiveBlending:
  122. // no alphaBlend settings
  123. break;
  124. case SubtractiveBlending:
  125. if ( premultipliedAlpha === true ) {
  126. alphaBlend = {
  127. srcFactor: GPUBlendFactor.OneMinusSrcColor,
  128. dstFactor: GPUBlendFactor.OneMinusSrcAlpha,
  129. operation: GPUBlendOperation.Add
  130. };
  131. }
  132. break;
  133. case MultiplyBlending:
  134. if ( premultipliedAlpha === true ) {
  135. alphaBlend = {
  136. srcFactor: GPUBlendFactor.Zero,
  137. dstFactor: GPUBlendFactor.SrcAlpha,
  138. operation: GPUBlendOperation.Add
  139. };
  140. }
  141. break;
  142. case CustomBlending:
  143. const blendSrcAlpha = material.blendSrcAlpha;
  144. const blendDstAlpha = material.blendDstAlpha;
  145. const blendEquationAlpha = material.blendEquationAlpha;
  146. if ( blendSrcAlpha !== null && blendDstAlpha !== null && blendEquationAlpha !== null ) {
  147. alphaBlend = {
  148. srcFactor: this._getBlendFactor( blendSrcAlpha ),
  149. dstFactor: this._getBlendFactor( blendDstAlpha ),
  150. operation: this._getBlendOperation( blendEquationAlpha )
  151. };
  152. }
  153. break;
  154. default:
  155. console.error( 'THREE.WebGPURenderer: Blending not supported.', blending );
  156. }
  157. return alphaBlend;
  158. }
  159. _getBlendFactor( blend ) {
  160. let blendFactor;
  161. switch ( blend ) {
  162. case ZeroFactor:
  163. blendFactor = GPUBlendFactor.Zero;
  164. break;
  165. case OneFactor:
  166. blendFactor = GPUBlendFactor.One;
  167. break;
  168. case SrcColorFactor:
  169. blendFactor = GPUBlendFactor.SrcColor;
  170. break;
  171. case OneMinusSrcColorFactor:
  172. blendFactor = GPUBlendFactor.OneMinusSrcColor;
  173. break;
  174. case SrcAlphaFactor:
  175. blendFactor = GPUBlendFactor.SrcAlpha;
  176. break;
  177. case OneMinusSrcAlphaFactor:
  178. blendFactor = GPUBlendFactor.OneMinusSrcAlpha;
  179. break;
  180. case DstColorFactor:
  181. blendFactor = GPUBlendFactor.DstColor;
  182. break;
  183. case OneMinusDstColorFactor:
  184. blendFactor = GPUBlendFactor.OneMinusDstColor;
  185. break;
  186. case DstAlphaFactor:
  187. blendFactor = GPUBlendFactor.DstAlpha;
  188. break;
  189. case OneMinusDstAlphaFactor:
  190. blendFactor = GPUBlendFactor.OneMinusDstAlpha;
  191. break;
  192. case SrcAlphaSaturateFactor:
  193. blendFactor = GPUBlendFactor.SrcAlphaSaturated;
  194. break;
  195. case BlendColorFactor:
  196. blendFactor = GPUBlendFactor.BlendColor;
  197. break;
  198. case OneMinusBlendColorFactor:
  199. blendFactor = GPUBlendFactor.OneMinusBlendColor;
  200. break;
  201. default:
  202. console.error( 'THREE.WebGPURenderer: Blend factor not supported.', blend );
  203. }
  204. return blendFactor;
  205. }
  206. _getBlendOperation( blendEquation ) {
  207. let blendOperation;
  208. switch ( blendEquation ) {
  209. case AddEquation:
  210. blendOperation = GPUBlendOperation.Add;
  211. break;
  212. case SubtractEquation:
  213. blendOperation = GPUBlendOperation.Subtract;
  214. break;
  215. case ReverseSubtractEquation:
  216. blendOperation = GPUBlendOperation.ReverseSubtract;
  217. break;
  218. case MinEquation:
  219. blendOperation = GPUBlendOperation.Min;
  220. break;
  221. case MaxEquation:
  222. blendOperation = GPUBlendOperation.Max;
  223. break;
  224. default:
  225. console.error( 'THREE.WebGPURenderer: Blend equation not supported.', blendEquation );
  226. }
  227. return blendOperation;
  228. }
  229. _getColorBlend( material ) {
  230. const blending = material.blending;
  231. const premultipliedAlpha = material.premultipliedAlpha;
  232. const colorBlend = {
  233. srcFactor: null,
  234. dstFactor: null,
  235. operation: null
  236. };
  237. switch ( blending ) {
  238. case NormalBlending:
  239. colorBlend.srcFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.One : GPUBlendFactor.SrcAlpha;
  240. colorBlend.dstFactor = GPUBlendFactor.OneMinusSrcAlpha;
  241. colorBlend.operation = GPUBlendOperation.Add;
  242. break;
  243. case AdditiveBlending:
  244. colorBlend.srcFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.One : GPUBlendFactor.SrcAlpha;
  245. colorBlend.operation = GPUBlendOperation.Add;
  246. break;
  247. case SubtractiveBlending:
  248. colorBlend.srcFactor = GPUBlendFactor.Zero;
  249. colorBlend.dstFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.Zero : GPUBlendFactor.OneMinusSrcColor;
  250. colorBlend.operation = GPUBlendOperation.Add;
  251. break;
  252. case MultiplyBlending:
  253. colorBlend.srcFactor = GPUBlendFactor.Zero;
  254. colorBlend.dstFactor = GPUBlendFactor.SrcColor;
  255. colorBlend.operation = GPUBlendOperation.Add;
  256. break;
  257. case CustomBlending:
  258. colorBlend.srcFactor = this._getBlendFactor( material.blendSrc );
  259. colorBlend.dstFactor = this._getBlendFactor( material.blendDst );
  260. colorBlend.operation = this._getBlendOperation( material.blendEquation );
  261. break;
  262. default:
  263. console.error( 'THREE.WebGPURenderer: Blending not supported.', blending );
  264. }
  265. return colorBlend;
  266. }
  267. _getColorWriteMask( material ) {
  268. return ( material.colorWrite === true ) ? GPUColorWriteFlags.All : GPUColorWriteFlags.None;
  269. }
  270. _getDepthCompare( material ) {
  271. let depthCompare;
  272. if ( material.depthTest === false ) {
  273. depthCompare = GPUCompareFunction.Always;
  274. } else {
  275. const depthFunc = material.depthFunc;
  276. switch ( depthFunc ) {
  277. case NeverDepth:
  278. depthCompare = GPUCompareFunction.Never;
  279. break;
  280. case AlwaysDepth:
  281. depthCompare = GPUCompareFunction.Always;
  282. break;
  283. case LessDepth:
  284. depthCompare = GPUCompareFunction.Less;
  285. break;
  286. case LessEqualDepth:
  287. depthCompare = GPUCompareFunction.LessEqual;
  288. break;
  289. case EqualDepth:
  290. depthCompare = GPUCompareFunction.Equal;
  291. break;
  292. case GreaterEqualDepth:
  293. depthCompare = GPUCompareFunction.GreaterEqual;
  294. break;
  295. case GreaterDepth:
  296. depthCompare = GPUCompareFunction.Greater;
  297. break;
  298. case NotEqualDepth:
  299. depthCompare = GPUCompareFunction.NotEqual;
  300. break;
  301. default:
  302. console.error( 'THREE.WebGPURenderer: Invalid depth function.', depthFunc );
  303. }
  304. }
  305. return depthCompare;
  306. }
  307. _getPrimitiveState( object, material ) {
  308. const descriptor = {};
  309. descriptor.topology = this._getPrimitiveTopology( object );
  310. if ( object.isLine === true && object.isLineSegments !== true ) {
  311. const geometry = object.geometry;
  312. const count = ( geometry.index ) ? geometry.index.count : geometry.attributes.position.count;
  313. descriptor.stripIndexFormat = ( count > 65535 ) ? GPUIndexFormat.Uint32 : GPUIndexFormat.Uint16; // define data type for primitive restart value
  314. }
  315. switch ( material.side ) {
  316. case FrontSide:
  317. descriptor.frontFace = GPUFrontFace.CCW;
  318. descriptor.cullMode = GPUCullMode.Back;
  319. break;
  320. case BackSide:
  321. descriptor.frontFace = GPUFrontFace.CW;
  322. descriptor.cullMode = GPUCullMode.Back;
  323. break;
  324. case DoubleSide:
  325. descriptor.frontFace = GPUFrontFace.CCW;
  326. descriptor.cullMode = GPUCullMode.None;
  327. break;
  328. default:
  329. console.error( 'THREE.WebGPURenderer: Unknown Material.side value.', material.side );
  330. break;
  331. }
  332. return descriptor;
  333. }
  334. _getPrimitiveTopology( object ) {
  335. if ( object.isMesh ) return GPUPrimitiveTopology.TriangleList;
  336. else if ( object.isPoints ) return GPUPrimitiveTopology.PointList;
  337. else if ( object.isLineSegments ) return GPUPrimitiveTopology.LineList;
  338. else if ( object.isLine ) return GPUPrimitiveTopology.LineStrip;
  339. }
  340. _getStencilCompare( material ) {
  341. let stencilCompare;
  342. const stencilFunc = material.stencilFunc;
  343. switch ( stencilFunc ) {
  344. case NeverStencilFunc:
  345. stencilCompare = GPUCompareFunction.Never;
  346. break;
  347. case AlwaysStencilFunc:
  348. stencilCompare = GPUCompareFunction.Always;
  349. break;
  350. case LessStencilFunc:
  351. stencilCompare = GPUCompareFunction.Less;
  352. break;
  353. case LessEqualStencilFunc:
  354. stencilCompare = GPUCompareFunction.LessEqual;
  355. break;
  356. case EqualStencilFunc:
  357. stencilCompare = GPUCompareFunction.Equal;
  358. break;
  359. case GreaterEqualStencilFunc:
  360. stencilCompare = GPUCompareFunction.GreaterEqual;
  361. break;
  362. case GreaterStencilFunc:
  363. stencilCompare = GPUCompareFunction.Greater;
  364. break;
  365. case NotEqualStencilFunc:
  366. stencilCompare = GPUCompareFunction.NotEqual;
  367. break;
  368. default:
  369. console.error( 'THREE.WebGPURenderer: Invalid stencil function.', stencilFunc );
  370. }
  371. return stencilCompare;
  372. }
  373. _getStencilOperation( op ) {
  374. let stencilOperation;
  375. switch ( op ) {
  376. case KeepStencilOp:
  377. stencilOperation = GPUStencilOperation.Keep;
  378. break;
  379. case ZeroStencilOp:
  380. stencilOperation = GPUStencilOperation.Zero;
  381. break;
  382. case ReplaceStencilOp:
  383. stencilOperation = GPUStencilOperation.Replace;
  384. break;
  385. case InvertStencilOp:
  386. stencilOperation = GPUStencilOperation.Invert;
  387. break;
  388. case IncrementStencilOp:
  389. stencilOperation = GPUStencilOperation.IncrementClamp;
  390. break;
  391. case DecrementStencilOp:
  392. stencilOperation = GPUStencilOperation.DecrementClamp;
  393. break;
  394. case IncrementWrapStencilOp:
  395. stencilOperation = GPUStencilOperation.IncrementWrap;
  396. break;
  397. case DecrementWrapStencilOp:
  398. stencilOperation = GPUStencilOperation.DecrementWrap;
  399. break;
  400. default:
  401. console.error( 'THREE.WebGPURenderer: Invalid stencil operation.', stencilOperation );
  402. }
  403. return stencilOperation;
  404. }
  405. _getVertexFormat( type ) {
  406. // @TODO: This code is GLSL specific. We need to update when we switch to WGSL.
  407. if ( type === 'float' ) return GPUVertexFormat.Float32;
  408. if ( type === 'vec2' ) return GPUVertexFormat.Float32x2;
  409. if ( type === 'vec3' ) return GPUVertexFormat.Float32x3;
  410. if ( type === 'vec4' ) return GPUVertexFormat.Float32x4;
  411. if ( type === 'int' ) return GPUVertexFormat.Sint32;
  412. if ( type === 'ivec2' ) return GPUVertexFormat.Sint32x2;
  413. if ( type === 'ivec3' ) return GPUVertexFormat.Sint32x3;
  414. if ( type === 'ivec4' ) return GPUVertexFormat.Sint32x4;
  415. if ( type === 'uint' ) return GPUVertexFormat.Uint32;
  416. if ( type === 'uvec2' ) return GPUVertexFormat.Uint32x2;
  417. if ( type === 'uvec3' ) return GPUVertexFormat.Uint32x3;
  418. if ( type === 'uvec4' ) return GPUVertexFormat.Uint32x4;
  419. console.error( 'THREE.WebGPURenderer: Shader variable type not supported yet.', type );
  420. }
  421. _parseShaderAttributes( shader ) {
  422. // find "layout (location = num) in type name" in vertex shader
  423. const regex = /\s*layout\s*\(\s*location\s*=\s*(?<location>[0-9]+)\s*\)\s*in\s+(?<type>\w+)\s+(?<name>\w+)\s*;/gmi;
  424. let shaderAttribute = null;
  425. const attributes = [];
  426. while ( shaderAttribute = regex.exec( shader ) ) {
  427. const shaderLocation = parseInt( shaderAttribute.groups.location );
  428. const arrayStride = this._getArrayStride( shaderAttribute.groups.type );
  429. const vertexFormat = this._getVertexFormat( shaderAttribute.groups.type );
  430. attributes.push( {
  431. name: shaderAttribute.groups.name,
  432. arrayStride: arrayStride,
  433. slot: shaderLocation,
  434. format: vertexFormat
  435. } );
  436. }
  437. // the sort ensures to setup vertex buffers in the correct order
  438. return attributes.sort( function ( a, b ) {
  439. return a.slot - b.slot;
  440. } );
  441. }
  442. }
  443. export default WebGPURenderPipeline;