WebGPURenderPipeline.js 16 KB

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