WebGPURenderPipeline.js 16 KB

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