WebGPURenderPipeline.js 16 KB

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