WebGPURenderPipeline.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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._getShaderAttributes( nodeBuilder, geometry );
  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. layout: 'auto'
  90. } );
  91. }
  92. _getArrayStride( type, bytesPerElement ) {
  93. // @TODO: This code is GLSL specific. We need to update when we switch to WGSL.
  94. if ( type === 'float' || type === 'int' || type === 'uint' ) return bytesPerElement;
  95. if ( type === 'vec2' || type === 'ivec2' || type === 'uvec2' ) return bytesPerElement * 2;
  96. if ( type === 'vec3' || type === 'ivec3' || type === 'uvec3' ) return bytesPerElement * 3;
  97. if ( type === 'vec4' || type === 'ivec4' || type === 'uvec4' ) return bytesPerElement * 4;
  98. console.error( 'THREE.WebGPURenderer: Shader variable type not supported yet.', type );
  99. }
  100. _getAlphaBlend( material ) {
  101. const blending = material.blending;
  102. const premultipliedAlpha = material.premultipliedAlpha;
  103. let alphaBlend = undefined;
  104. switch ( blending ) {
  105. case NormalBlending:
  106. if ( premultipliedAlpha === false ) {
  107. alphaBlend = {
  108. srcFactor: GPUBlendFactor.One,
  109. dstFactor: GPUBlendFactor.OneMinusSrcAlpha,
  110. operation: GPUBlendOperation.Add
  111. };
  112. }
  113. break;
  114. case AdditiveBlending:
  115. // no alphaBlend settings
  116. break;
  117. case SubtractiveBlending:
  118. if ( premultipliedAlpha === true ) {
  119. alphaBlend = {
  120. srcFactor: GPUBlendFactor.OneMinusSrcColor,
  121. dstFactor: GPUBlendFactor.OneMinusSrcAlpha,
  122. operation: GPUBlendOperation.Add
  123. };
  124. }
  125. break;
  126. case MultiplyBlending:
  127. if ( premultipliedAlpha === true ) {
  128. alphaBlend = {
  129. srcFactor: GPUBlendFactor.Zero,
  130. dstFactor: GPUBlendFactor.SrcAlpha,
  131. operation: GPUBlendOperation.Add
  132. };
  133. }
  134. break;
  135. case CustomBlending:
  136. const blendSrcAlpha = material.blendSrcAlpha;
  137. const blendDstAlpha = material.blendDstAlpha;
  138. const blendEquationAlpha = material.blendEquationAlpha;
  139. if ( blendSrcAlpha !== null && blendDstAlpha !== null && blendEquationAlpha !== null ) {
  140. alphaBlend = {
  141. srcFactor: this._getBlendFactor( blendSrcAlpha ),
  142. dstFactor: this._getBlendFactor( blendDstAlpha ),
  143. operation: this._getBlendOperation( blendEquationAlpha )
  144. };
  145. }
  146. break;
  147. default:
  148. console.error( 'THREE.WebGPURenderer: Blending not supported.', blending );
  149. }
  150. return alphaBlend;
  151. }
  152. _getBlendFactor( blend ) {
  153. let blendFactor;
  154. switch ( blend ) {
  155. case ZeroFactor:
  156. blendFactor = GPUBlendFactor.Zero;
  157. break;
  158. case OneFactor:
  159. blendFactor = GPUBlendFactor.One;
  160. break;
  161. case SrcColorFactor:
  162. blendFactor = GPUBlendFactor.SrcColor;
  163. break;
  164. case OneMinusSrcColorFactor:
  165. blendFactor = GPUBlendFactor.OneMinusSrcColor;
  166. break;
  167. case SrcAlphaFactor:
  168. blendFactor = GPUBlendFactor.SrcAlpha;
  169. break;
  170. case OneMinusSrcAlphaFactor:
  171. blendFactor = GPUBlendFactor.OneMinusSrcAlpha;
  172. break;
  173. case DstColorFactor:
  174. blendFactor = GPUBlendFactor.DstColor;
  175. break;
  176. case OneMinusDstColorFactor:
  177. blendFactor = GPUBlendFactor.OneMinusDstColor;
  178. break;
  179. case DstAlphaFactor:
  180. blendFactor = GPUBlendFactor.DstAlpha;
  181. break;
  182. case OneMinusDstAlphaFactor:
  183. blendFactor = GPUBlendFactor.OneMinusDstAlpha;
  184. break;
  185. case SrcAlphaSaturateFactor:
  186. blendFactor = GPUBlendFactor.SrcAlphaSaturated;
  187. break;
  188. case BlendColorFactor:
  189. blendFactor = GPUBlendFactor.BlendColor;
  190. break;
  191. case OneMinusBlendColorFactor:
  192. blendFactor = GPUBlendFactor.OneMinusBlendColor;
  193. break;
  194. default:
  195. console.error( 'THREE.WebGPURenderer: Blend factor not supported.', blend );
  196. }
  197. return blendFactor;
  198. }
  199. _getBlendOperation( blendEquation ) {
  200. let blendOperation;
  201. switch ( blendEquation ) {
  202. case AddEquation:
  203. blendOperation = GPUBlendOperation.Add;
  204. break;
  205. case SubtractEquation:
  206. blendOperation = GPUBlendOperation.Subtract;
  207. break;
  208. case ReverseSubtractEquation:
  209. blendOperation = GPUBlendOperation.ReverseSubtract;
  210. break;
  211. case MinEquation:
  212. blendOperation = GPUBlendOperation.Min;
  213. break;
  214. case MaxEquation:
  215. blendOperation = GPUBlendOperation.Max;
  216. break;
  217. default:
  218. console.error( 'THREE.WebGPURenderer: Blend equation not supported.', blendEquation );
  219. }
  220. return blendOperation;
  221. }
  222. _getColorBlend( material ) {
  223. const blending = material.blending;
  224. const premultipliedAlpha = material.premultipliedAlpha;
  225. const colorBlend = {
  226. srcFactor: null,
  227. dstFactor: null,
  228. operation: null
  229. };
  230. switch ( blending ) {
  231. case NormalBlending:
  232. colorBlend.srcFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.One : GPUBlendFactor.SrcAlpha;
  233. colorBlend.dstFactor = GPUBlendFactor.OneMinusSrcAlpha;
  234. colorBlend.operation = GPUBlendOperation.Add;
  235. break;
  236. case AdditiveBlending:
  237. colorBlend.srcFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.One : GPUBlendFactor.SrcAlpha;
  238. colorBlend.operation = GPUBlendOperation.Add;
  239. break;
  240. case SubtractiveBlending:
  241. colorBlend.srcFactor = GPUBlendFactor.Zero;
  242. colorBlend.dstFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.Zero : GPUBlendFactor.OneMinusSrcColor;
  243. colorBlend.operation = GPUBlendOperation.Add;
  244. break;
  245. case MultiplyBlending:
  246. colorBlend.srcFactor = GPUBlendFactor.Zero;
  247. colorBlend.dstFactor = GPUBlendFactor.SrcColor;
  248. colorBlend.operation = GPUBlendOperation.Add;
  249. break;
  250. case CustomBlending:
  251. colorBlend.srcFactor = this._getBlendFactor( material.blendSrc );
  252. colorBlend.dstFactor = this._getBlendFactor( material.blendDst );
  253. colorBlend.operation = this._getBlendOperation( material.blendEquation );
  254. break;
  255. default:
  256. console.error( 'THREE.WebGPURenderer: Blending not supported.', blending );
  257. }
  258. return colorBlend;
  259. }
  260. _getColorWriteMask( material ) {
  261. return ( material.colorWrite === true ) ? GPUColorWriteFlags.All : GPUColorWriteFlags.None;
  262. }
  263. _getDepthCompare( material ) {
  264. let depthCompare;
  265. if ( material.depthTest === false ) {
  266. depthCompare = GPUCompareFunction.Always;
  267. } else {
  268. const depthFunc = material.depthFunc;
  269. switch ( depthFunc ) {
  270. case NeverDepth:
  271. depthCompare = GPUCompareFunction.Never;
  272. break;
  273. case AlwaysDepth:
  274. depthCompare = GPUCompareFunction.Always;
  275. break;
  276. case LessDepth:
  277. depthCompare = GPUCompareFunction.Less;
  278. break;
  279. case LessEqualDepth:
  280. depthCompare = GPUCompareFunction.LessEqual;
  281. break;
  282. case EqualDepth:
  283. depthCompare = GPUCompareFunction.Equal;
  284. break;
  285. case GreaterEqualDepth:
  286. depthCompare = GPUCompareFunction.GreaterEqual;
  287. break;
  288. case GreaterDepth:
  289. depthCompare = GPUCompareFunction.Greater;
  290. break;
  291. case NotEqualDepth:
  292. depthCompare = GPUCompareFunction.NotEqual;
  293. break;
  294. default:
  295. console.error( 'THREE.WebGPURenderer: Invalid depth function.', depthFunc );
  296. }
  297. }
  298. return depthCompare;
  299. }
  300. _getPrimitiveState( object, material ) {
  301. const descriptor = {};
  302. descriptor.topology = this._getPrimitiveTopology( object );
  303. if ( object.isLine === true && object.isLineSegments !== true ) {
  304. const geometry = object.geometry;
  305. const count = ( geometry.index ) ? geometry.index.count : geometry.attributes.position.count;
  306. descriptor.stripIndexFormat = ( count > 65535 ) ? GPUIndexFormat.Uint32 : GPUIndexFormat.Uint16; // define data type for primitive restart value
  307. }
  308. switch ( material.side ) {
  309. case FrontSide:
  310. descriptor.frontFace = GPUFrontFace.CW;
  311. descriptor.cullMode = GPUCullMode.Front;
  312. break;
  313. case BackSide:
  314. descriptor.frontFace = GPUFrontFace.CW;
  315. descriptor.cullMode = GPUCullMode.Back;
  316. break;
  317. case DoubleSide:
  318. descriptor.frontFace = GPUFrontFace.CW;
  319. descriptor.cullMode = GPUCullMode.None;
  320. break;
  321. default:
  322. console.error( 'THREE.WebGPURenderer: Unknown Material.side value.', material.side );
  323. break;
  324. }
  325. return descriptor;
  326. }
  327. _getPrimitiveTopology( object ) {
  328. if ( object.isMesh ) return GPUPrimitiveTopology.TriangleList;
  329. else if ( object.isPoints ) return GPUPrimitiveTopology.PointList;
  330. else if ( object.isLineSegments ) return GPUPrimitiveTopology.LineList;
  331. else if ( object.isLine ) return GPUPrimitiveTopology.LineStrip;
  332. }
  333. _getStencilCompare( material ) {
  334. let stencilCompare;
  335. const stencilFunc = material.stencilFunc;
  336. switch ( stencilFunc ) {
  337. case NeverStencilFunc:
  338. stencilCompare = GPUCompareFunction.Never;
  339. break;
  340. case AlwaysStencilFunc:
  341. stencilCompare = GPUCompareFunction.Always;
  342. break;
  343. case LessStencilFunc:
  344. stencilCompare = GPUCompareFunction.Less;
  345. break;
  346. case LessEqualStencilFunc:
  347. stencilCompare = GPUCompareFunction.LessEqual;
  348. break;
  349. case EqualStencilFunc:
  350. stencilCompare = GPUCompareFunction.Equal;
  351. break;
  352. case GreaterEqualStencilFunc:
  353. stencilCompare = GPUCompareFunction.GreaterEqual;
  354. break;
  355. case GreaterStencilFunc:
  356. stencilCompare = GPUCompareFunction.Greater;
  357. break;
  358. case NotEqualStencilFunc:
  359. stencilCompare = GPUCompareFunction.NotEqual;
  360. break;
  361. default:
  362. console.error( 'THREE.WebGPURenderer: Invalid stencil function.', stencilFunc );
  363. }
  364. return stencilCompare;
  365. }
  366. _getStencilOperation( op ) {
  367. let stencilOperation;
  368. switch ( op ) {
  369. case KeepStencilOp:
  370. stencilOperation = GPUStencilOperation.Keep;
  371. break;
  372. case ZeroStencilOp:
  373. stencilOperation = GPUStencilOperation.Zero;
  374. break;
  375. case ReplaceStencilOp:
  376. stencilOperation = GPUStencilOperation.Replace;
  377. break;
  378. case InvertStencilOp:
  379. stencilOperation = GPUStencilOperation.Invert;
  380. break;
  381. case IncrementStencilOp:
  382. stencilOperation = GPUStencilOperation.IncrementClamp;
  383. break;
  384. case DecrementStencilOp:
  385. stencilOperation = GPUStencilOperation.DecrementClamp;
  386. break;
  387. case IncrementWrapStencilOp:
  388. stencilOperation = GPUStencilOperation.IncrementWrap;
  389. break;
  390. case DecrementWrapStencilOp:
  391. stencilOperation = GPUStencilOperation.DecrementWrap;
  392. break;
  393. default:
  394. console.error( 'THREE.WebGPURenderer: Invalid stencil operation.', stencilOperation );
  395. }
  396. return stencilOperation;
  397. }
  398. _getVertexFormat( type, bytesPerElement ) {
  399. // float
  400. if ( type === 'float' ) return GPUVertexFormat.Float32;
  401. if ( type === 'vec2' ) {
  402. if ( bytesPerElement === 2 ) {
  403. return GPUVertexFormat.Float16x2;
  404. } else {
  405. return GPUVertexFormat.Float32x2;
  406. }
  407. }
  408. if ( type === 'vec3' ) return GPUVertexFormat.Float32x3;
  409. if ( type === 'vec4' ) {
  410. if ( bytesPerElement === 2 ) {
  411. return GPUVertexFormat.Float16x4;
  412. } else {
  413. return GPUVertexFormat.Float32x4;
  414. }
  415. }
  416. // int
  417. if ( type === 'int' ) return GPUVertexFormat.Sint32;
  418. if ( type === 'ivec2' ) {
  419. if ( bytesPerElement === 1 ) {
  420. return GPUVertexFormat.Sint8x2;
  421. } else if ( bytesPerElement === 2 ) {
  422. return GPUVertexFormat.Sint16x2;
  423. } else {
  424. return GPUVertexFormat.Sint32x2;
  425. }
  426. }
  427. if ( type === 'ivec3' ) return GPUVertexFormat.Sint32x3;
  428. if ( type === 'ivec4' ) {
  429. if ( bytesPerElement === 1 ) {
  430. return GPUVertexFormat.Sint8x4;
  431. } else if ( bytesPerElement === 2 ) {
  432. return GPUVertexFormat.Sint16x4;
  433. } else {
  434. return GPUVertexFormat.Sint32x4;
  435. }
  436. }
  437. // uint
  438. if ( type === 'uint' ) return GPUVertexFormat.Uint32;
  439. if ( type === 'uvec2' ) {
  440. if ( bytesPerElement === 1 ) {
  441. return GPUVertexFormat.Uint8x2;
  442. } else if ( bytesPerElement === 2 ) {
  443. return GPUVertexFormat.Uint16x2;
  444. } else {
  445. return GPUVertexFormat.Uint32x2;
  446. }
  447. }
  448. if ( type === 'uvec3' ) return GPUVertexFormat.Uint32x3;
  449. if ( type === 'uvec4' ) {
  450. if ( bytesPerElement === 1 ) {
  451. return GPUVertexFormat.Uint8x4;
  452. } else if ( bytesPerElement === 2 ) {
  453. return GPUVertexFormat.Uint16x4;
  454. } else {
  455. return GPUVertexFormat.Uint32x4;
  456. }
  457. }
  458. console.error( 'THREE.WebGPURenderer: Shader variable type not supported yet.', type );
  459. }
  460. _getShaderAttributes( nodeBuilder, geometry ) {
  461. const nodeAttributes = nodeBuilder.attributes;
  462. const attributes = [];
  463. for ( let slot = 0; slot < nodeAttributes.length; slot ++ ) {
  464. const nodeAttribute = nodeAttributes[ slot ];
  465. const name = nodeAttribute.name;
  466. const type = nodeAttribute.type;
  467. const geometryAttribute = geometry.getAttribute( name );
  468. const bytesPerElement = ( geometryAttribute !== undefined ) ? geometryAttribute.array.BYTES_PER_ELEMENT : 4;
  469. const arrayStride = this._getArrayStride( type, bytesPerElement );
  470. const format = this._getVertexFormat( type, bytesPerElement );
  471. attributes.push( {
  472. name,
  473. arrayStride,
  474. format,
  475. slot
  476. } );
  477. }
  478. return attributes;
  479. }
  480. }
  481. export default WebGPURenderPipeline;