WebGPURenderPipelines.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. import { GPUPrimitiveTopology, GPUIndexFormat, GPUTextureFormat, 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 '../../../../build/three.module.js';
  11. import ShaderLib from './ShaderLib.js';
  12. import WebGPUNodeBuilder from './WebGPUNodeBuilder.js';
  13. class WebGPURenderPipelines {
  14. constructor( renderer, properties, device, glslang, sampleCount ) {
  15. this.renderer = renderer;
  16. this.properties = properties;
  17. this.device = device;
  18. this.glslang = glslang;
  19. this.sampleCount = sampleCount;
  20. this.bindings = new WeakMap();
  21. this.pipelines = new WeakMap();
  22. this.shaderAttributes = new WeakMap();
  23. this.shaderModules = {
  24. vertex: new WeakMap(),
  25. fragment: new WeakMap()
  26. };
  27. }
  28. get( object ) {
  29. // @TODO: Avoid a 1:1 relationship between pipelines and objects. It's necessary
  30. // to check various conditions in order to request an appropriate pipeline.
  31. //
  32. // - material's version and node configuration
  33. // - environment map (material)
  34. // - fog and environment (scene)
  35. // - output encoding (renderer)
  36. // - light state
  37. // - clipping planes
  38. //
  39. // The renderer needs to manage multiple pipelines per object so
  40. // GPUDevice.createRenderPipeline() is only called when no pipeline exists for the
  41. // current configuration.
  42. let pipeline = this.pipelines.get( object );
  43. if ( pipeline === undefined ) {
  44. const device = this.device;
  45. const material = object.material;
  46. // shader source
  47. let shader;
  48. if ( material.isMeshBasicMaterial ) {
  49. shader = ShaderLib.meshBasic;
  50. } else if ( material.isPointsMaterial ) {
  51. shader = ShaderLib.pointsBasic;
  52. } else if ( material.isLineBasicMaterial ) {
  53. shader = ShaderLib.lineBasic;
  54. } else {
  55. console.error( 'THREE.WebGPURenderer: Unknwon shader type.' );
  56. }
  57. // parse nodes
  58. const nodeBuilder = new WebGPUNodeBuilder( material, this.renderer );
  59. shader = nodeBuilder.parse( shader.vertexShader, shader.fragmentShader );
  60. this.bindings.set( object, nodeBuilder.getBindings() );
  61. // shader modules
  62. const glslang = this.glslang;
  63. let moduleVertex = this.shaderModules.vertex.get( shader );
  64. if ( moduleVertex === undefined ) {
  65. const byteCodeVertex = glslang.compileGLSL( shader.vertexShader, 'vertex' );
  66. moduleVertex = {
  67. module: device.createShaderModule( { code: byteCodeVertex } ),
  68. entryPoint: 'main'
  69. };
  70. this.shaderModules.vertex.set( shader, moduleVertex );
  71. }
  72. let moduleFragment = this.shaderModules.fragment.get( shader );
  73. if ( moduleFragment === undefined ) {
  74. const byteCodeFragment = glslang.compileGLSL( shader.fragmentShader, 'fragment' );
  75. moduleFragment = {
  76. module: device.createShaderModule( { code: byteCodeFragment } ),
  77. entryPoint: 'main'
  78. };
  79. this.shaderModules.fragment.set( shader, moduleFragment );
  80. }
  81. // determine shader attributes
  82. const shaderAttributes = this._parseShaderAttributes( shader.vertexShader );
  83. // vertex buffers
  84. const vertexBuffers = [];
  85. const geometry = object.geometry;
  86. for ( const attribute of shaderAttributes ) {
  87. const name = attribute.name;
  88. const geometryAttribute = geometry.getAttribute( name );
  89. const stepMode = ( geometryAttribute !== undefined && geometryAttribute.isInstancedBufferAttribute ) ? GPUInputStepMode.Instance : GPUInputStepMode.Vertex;
  90. vertexBuffers.push( {
  91. arrayStride: attribute.arrayStride,
  92. attributes: [ { shaderLocation: attribute.slot, offset: 0, format: attribute.format } ],
  93. stepMode: stepMode
  94. } );
  95. }
  96. //
  97. let indexFormat;
  98. if ( object.isLine ) {
  99. const count = ( geometry.index ) ? geometry.index.count : geometry.attributes.position.count;
  100. indexFormat = ( count > 65535 ) ? GPUIndexFormat.Uint32 : GPUIndexFormat.Uint16; // define data type for primitive restart value
  101. }
  102. //
  103. let alphaBlend = {};
  104. let colorBlend = {};
  105. if ( material.transparent === true && material.blending !== NoBlending ) {
  106. alphaBlend = this._getAlphaBlend( material );
  107. colorBlend = this._getColorBlend( material );
  108. }
  109. //
  110. let stencilFront = {};
  111. if ( material.stencilWrite === true ) {
  112. stencilFront = {
  113. compare: this._getStencilCompare( material ),
  114. failOp: this._getStencilOperation( material.stencilFail ),
  115. depthFailOp: this._getStencilOperation( material.stencilZFail ),
  116. passOp: this._getStencilOperation( material.stencilZPass )
  117. };
  118. }
  119. // pipeline
  120. const primitiveTopology = this._getPrimitiveTopology( object );
  121. const rasterizationState = this._getRasterizationStateDescriptor( material );
  122. const colorWriteMask = this._getColorWriteMask( material );
  123. const depthCompare = this._getDepthCompare( material );
  124. const colorFormat = this._getColorFormat( this.renderer );
  125. const depthStencilFormat = this._getDepthStencilFormat( this.renderer );
  126. pipeline = device.createRenderPipeline( {
  127. vertexStage: moduleVertex,
  128. fragmentStage: moduleFragment,
  129. primitiveTopology: primitiveTopology,
  130. rasterizationState: rasterizationState,
  131. colorStates: [ {
  132. format: colorFormat,
  133. alphaBlend: alphaBlend,
  134. colorBlend: colorBlend,
  135. writeMask: colorWriteMask
  136. } ],
  137. depthStencilState: {
  138. format: depthStencilFormat,
  139. depthWriteEnabled: material.depthWrite,
  140. depthCompare: depthCompare,
  141. stencilFront: stencilFront,
  142. stencilBack: {}, // three.js does not provide an API to configure the back function (gl.stencilFuncSeparate() was never used)
  143. stencilReadMask: material.stencilFuncMask,
  144. stencilWriteMask: material.stencilWriteMask
  145. },
  146. vertexState: {
  147. indexFormat: indexFormat,
  148. vertexBuffers: vertexBuffers
  149. },
  150. sampleCount: this.sampleCount
  151. } );
  152. this.pipelines.set( object, pipeline );
  153. this.shaderAttributes.set( pipeline, shaderAttributes );
  154. }
  155. return pipeline;
  156. }
  157. getBindings( object ) {
  158. return this.bindings.get( object );
  159. }
  160. getShaderAttributes( pipeline ) {
  161. return this.shaderAttributes.get( pipeline );
  162. }
  163. dispose() {
  164. this.pipelines = new WeakMap();
  165. this.shaderAttributes = new WeakMap();
  166. this.shaderModules = {
  167. vertex: new WeakMap(),
  168. fragment: new WeakMap()
  169. };
  170. }
  171. _getArrayStride( type ) {
  172. // @TODO: This code is GLSL specific. We need to update when we switch to WGSL.
  173. if ( type === 'float' ) return 4;
  174. if ( type === 'vec2' ) return 8;
  175. if ( type === 'vec3' ) return 12;
  176. if ( type === 'vec4' ) return 16;
  177. if ( type === 'int' ) return 4;
  178. if ( type === 'ivec2' ) return 8;
  179. if ( type === 'ivec3' ) return 12;
  180. if ( type === 'ivec4' ) return 16;
  181. if ( type === 'uint' ) return 4;
  182. if ( type === 'uvec2' ) return 8;
  183. if ( type === 'uvec3' ) return 12;
  184. if ( type === 'uvec4' ) return 16;
  185. console.error( 'THREE.WebGPURenderer: Shader variable type not supported yet.', type );
  186. }
  187. _getAlphaBlend( material ) {
  188. const blending = material.blending;
  189. const premultipliedAlpha = material.premultipliedAlpha;
  190. let alphaBlend = undefined;
  191. switch ( blending ) {
  192. case NormalBlending:
  193. if ( premultipliedAlpha === false ) {
  194. alphaBlend = {
  195. srcFactor: GPUBlendFactor.One,
  196. dstFactor: GPUBlendFactor.OneMinusSrcAlpha,
  197. operation: GPUBlendOperation.Add
  198. };
  199. }
  200. break;
  201. case AdditiveBlending:
  202. // no alphaBlend settings
  203. break;
  204. case SubtractiveBlending:
  205. if ( premultipliedAlpha === true ) {
  206. alphaBlend = {
  207. srcFactor: GPUBlendFactor.OneMinusSrcColor,
  208. dstFactor: GPUBlendFactor.OneMinusSrcAlpha,
  209. operation: GPUBlendOperation.Add
  210. };
  211. }
  212. break;
  213. case MultiplyBlending:
  214. if ( premultipliedAlpha === true ) {
  215. alphaBlend = {
  216. srcFactor: GPUBlendFactor.Zero,
  217. dstFactor: GPUBlendFactor.SrcAlpha,
  218. operation: GPUBlendOperation.Add
  219. };
  220. }
  221. break;
  222. case CustomBlending:
  223. const blendSrcAlpha = material.blendSrcAlpha;
  224. const blendDstAlpha = material.blendDstAlpha;
  225. const blendEquationAlpha = material.blendEquationAlpha;
  226. if ( blendSrcAlpha !== null && blendDstAlpha !== null && blendEquationAlpha !== null ) {
  227. alphaBlend = {
  228. srcFactor: this._getBlendFactor( blendSrcAlpha ),
  229. dstFactor: this._getBlendFactor( blendDstAlpha ),
  230. operation: this._getBlendOperation( blendEquationAlpha )
  231. };
  232. }
  233. break;
  234. default:
  235. console.error( 'THREE.WebGPURenderer: Blending not supported.', blending );
  236. }
  237. return alphaBlend;
  238. }
  239. _getBlendFactor( blend ) {
  240. let blendFactor;
  241. switch ( blend ) {
  242. case ZeroFactor:
  243. blendFactor = GPUBlendFactor.Zero;
  244. break;
  245. case OneFactor:
  246. blendFactor = GPUBlendFactor.One;
  247. break;
  248. case SrcColorFactor:
  249. blendFactor = GPUBlendFactor.SrcColor;
  250. break;
  251. case OneMinusSrcColorFactor:
  252. blendFactor = GPUBlendFactor.OneMinusSrcColor;
  253. break;
  254. case SrcAlphaFactor:
  255. blendFactor = GPUBlendFactor.SrcAlpha;
  256. break;
  257. case OneMinusSrcAlphaFactor:
  258. blendFactor = GPUBlendFactor.OneMinusSrcAlpha;
  259. break;
  260. case DstColorFactor:
  261. blendFactor = GPUBlendFactor.DstColor;
  262. break;
  263. case OneMinusDstColorFactor:
  264. blendFactor = GPUBlendFactor.OneMinusDstColor;
  265. break;
  266. case DstAlphaFactor:
  267. blendFactor = GPUBlendFactor.DstAlpha;
  268. break;
  269. case OneMinusDstAlphaFactor:
  270. blendFactor = GPUBlendFactor.OneMinusDstAlpha;
  271. break;
  272. case SrcAlphaSaturateFactor:
  273. blendFactor = GPUBlendFactor.SrcAlphaSaturated;
  274. break;
  275. case BlendColorFactor:
  276. blendFactor = GPUBlendFactor.BlendColor;
  277. break;
  278. case OneMinusBlendColorFactor:
  279. blendFactor = GPUBlendFactor.OneMinusBlendColor;
  280. break;
  281. default:
  282. console.error( 'THREE.WebGPURenderer: Blend factor not supported.', blend );
  283. }
  284. return blendFactor;
  285. }
  286. _getBlendOperation( blendEquation ) {
  287. let blendOperation;
  288. switch ( blendEquation ) {
  289. case AddEquation:
  290. blendOperation = GPUBlendOperation.Add;
  291. break;
  292. case SubtractEquation:
  293. blendOperation = GPUBlendOperation.Subtract;
  294. break;
  295. case ReverseSubtractEquation:
  296. blendOperation = GPUBlendOperation.ReverseSubtract;
  297. break;
  298. case MinEquation:
  299. blendOperation = GPUBlendOperation.Min;
  300. break;
  301. case MaxEquation:
  302. blendOperation = GPUBlendOperation.Max;
  303. break;
  304. default:
  305. console.error( 'THREE.WebGPURenderer: Blend equation not supported.', blendEquation );
  306. }
  307. return blendOperation;
  308. }
  309. _getColorBlend( material ) {
  310. const blending = material.blending;
  311. const premultipliedAlpha = material.premultipliedAlpha;
  312. const colorBlend = {
  313. srcFactor: null,
  314. dstFactor: null,
  315. operation: null
  316. };
  317. switch ( blending ) {
  318. case NormalBlending:
  319. colorBlend.srcFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.One : GPUBlendFactor.SrcAlpha;
  320. colorBlend.dstFactor = GPUBlendFactor.OneMinusSrcAlpha;
  321. colorBlend.operation = GPUBlendOperation.Add;
  322. break;
  323. case AdditiveBlending:
  324. colorBlend.srcFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.One : GPUBlendFactor.SrcAlpha;
  325. colorBlend.operation = GPUBlendOperation.Add;
  326. break;
  327. case SubtractiveBlending:
  328. colorBlend.srcFactor = GPUBlendFactor.Zero;
  329. colorBlend.dstFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.Zero : GPUBlendFactor.OneMinusSrcColor;
  330. colorBlend.operation = GPUBlendOperation.Add;
  331. break;
  332. case MultiplyBlending:
  333. colorBlend.srcFactor = GPUBlendFactor.Zero;
  334. colorBlend.dstFactor = GPUBlendFactor.SrcColor;
  335. colorBlend.operation = GPUBlendOperation.Add;
  336. break;
  337. case CustomBlending:
  338. colorBlend.srcFactor = this._getBlendFactor( material.blendSrc );
  339. colorBlend.dstFactor = this._getBlendFactor( material.blendDst );
  340. colorBlend.operation = this._getBlendOperation( material.blendEquation );
  341. break;
  342. default:
  343. console.error( 'THREE.WebGPURenderer: Blending not supported.', blending );
  344. }
  345. return colorBlend;
  346. }
  347. _getColorFormat( renderer ) {
  348. let format;
  349. const renderTarget = renderer.getRenderTarget();
  350. if ( renderTarget !== null ) {
  351. const renderTargetProperties = this.properties.get( renderTarget );
  352. format = renderTargetProperties.colorTextureFormat;
  353. } else {
  354. format = GPUTextureFormat.BRGA8Unorm; // default swap chain format
  355. }
  356. return format;
  357. }
  358. _getColorWriteMask( material ) {
  359. return ( material.colorWrite === true ) ? GPUColorWriteFlags.All : GPUColorWriteFlags.None;
  360. }
  361. _getDepthCompare( material ) {
  362. let depthCompare;
  363. if ( material.depthTest === false ) {
  364. depthCompare = GPUCompareFunction.Always;
  365. } else {
  366. const depthFunc = material.depthFunc;
  367. switch ( depthFunc ) {
  368. case NeverDepth:
  369. depthCompare = GPUCompareFunction.Never;
  370. break;
  371. case AlwaysDepth:
  372. depthCompare = GPUCompareFunction.Always;
  373. break;
  374. case LessDepth:
  375. depthCompare = GPUCompareFunction.Less;
  376. break;
  377. case LessEqualDepth:
  378. depthCompare = GPUCompareFunction.LessEqual;
  379. break;
  380. case EqualDepth:
  381. depthCompare = GPUCompareFunction.Equal;
  382. break;
  383. case GreaterEqualDepth:
  384. depthCompare = GPUCompareFunction.GreaterEqual;
  385. break;
  386. case GreaterDepth:
  387. depthCompare = GPUCompareFunction.Greater;
  388. break;
  389. case NotEqualDepth:
  390. depthCompare = GPUCompareFunction.NotEqual;
  391. break;
  392. default:
  393. console.error( 'THREE.WebGPURenderer: Invalid depth function.', depthFunc );
  394. }
  395. }
  396. return depthCompare;
  397. }
  398. _getDepthStencilFormat( renderer ) {
  399. let format;
  400. const renderTarget = renderer.getRenderTarget();
  401. if ( renderTarget !== null ) {
  402. const renderTargetProperties = this.properties.get( renderTarget );
  403. format = renderTargetProperties.depthTextureFormat;
  404. } else {
  405. format = GPUTextureFormat.Depth24PlusStencil8;
  406. }
  407. return format;
  408. }
  409. _getPrimitiveTopology( object ) {
  410. if ( object.isMesh ) return GPUPrimitiveTopology.TriangleList;
  411. else if ( object.isPoints ) return GPUPrimitiveTopology.PointList;
  412. else if ( object.isLine ) return GPUPrimitiveTopology.LineStrip;
  413. else if ( object.isLineSegments ) return GPUPrimitiveTopology.LineList;
  414. }
  415. _getRasterizationStateDescriptor( material ) {
  416. const descriptor = {};
  417. switch ( material.side ) {
  418. case FrontSide:
  419. descriptor.frontFace = GPUFrontFace.CCW;
  420. descriptor.cullMode = GPUCullMode.Back;
  421. break;
  422. case BackSide:
  423. descriptor.frontFace = GPUFrontFace.CW;
  424. descriptor.cullMode = GPUCullMode.Back;
  425. break;
  426. case DoubleSide:
  427. descriptor.frontFace = GPUFrontFace.CCW;
  428. descriptor.cullMode = GPUCullMode.None;
  429. break;
  430. default:
  431. console.error( 'THREE.WebGPURenderer: Unknown Material.side value.', material.side );
  432. break;
  433. }
  434. return descriptor;
  435. }
  436. _getStencilCompare( material ) {
  437. let stencilCompare;
  438. const stencilFunc = material.stencilFunc;
  439. switch ( stencilFunc ) {
  440. case NeverStencilFunc:
  441. stencilCompare = GPUCompareFunction.Never;
  442. break;
  443. case AlwaysStencilFunc:
  444. stencilCompare = GPUCompareFunction.Always;
  445. break;
  446. case LessStencilFunc:
  447. stencilCompare = GPUCompareFunction.Less;
  448. break;
  449. case LessEqualStencilFunc:
  450. stencilCompare = GPUCompareFunction.LessEqual;
  451. break;
  452. case EqualStencilFunc:
  453. stencilCompare = GPUCompareFunction.Equal;
  454. break;
  455. case GreaterEqualStencilFunc:
  456. stencilCompare = GPUCompareFunction.GreaterEqual;
  457. break;
  458. case GreaterStencilFunc:
  459. stencilCompare = GPUCompareFunction.Greater;
  460. break;
  461. case NotEqualStencilFunc:
  462. stencilCompare = GPUCompareFunction.NotEqual;
  463. break;
  464. default:
  465. console.error( 'THREE.WebGPURenderer: Invalid stencil function.', stencilFunc );
  466. }
  467. return stencilCompare;
  468. }
  469. _getStencilOperation( op ) {
  470. let stencilOperation;
  471. switch ( op ) {
  472. case KeepStencilOp:
  473. stencilOperation = GPUStencilOperation.Keep;
  474. break;
  475. case ZeroStencilOp:
  476. stencilOperation = GPUStencilOperation.Zero;
  477. break;
  478. case ReplaceStencilOp:
  479. stencilOperation = GPUStencilOperation.Replace;
  480. break;
  481. case InvertStencilOp:
  482. stencilOperation = GPUStencilOperation.Invert;
  483. break;
  484. case IncrementStencilOp:
  485. stencilOperation = GPUStencilOperation.IncrementClamp;
  486. break;
  487. case DecrementStencilOp:
  488. stencilOperation = GPUStencilOperation.DecrementClamp;
  489. break;
  490. case IncrementWrapStencilOp:
  491. stencilOperation = GPUStencilOperation.IncrementWrap;
  492. break;
  493. case DecrementWrapStencilOp:
  494. stencilOperation = GPUStencilOperation.DecrementWrap;
  495. break;
  496. default:
  497. console.error( 'THREE.WebGPURenderer: Invalid stencil operation.', stencilOperation );
  498. }
  499. return stencilOperation;
  500. }
  501. _getVertexFormat( type ) {
  502. // @TODO: This code is GLSL specific. We need to update when we switch to WGSL.
  503. if ( type === 'float' ) return GPUVertexFormat.Float;
  504. if ( type === 'vec2' ) return GPUVertexFormat.Float2;
  505. if ( type === 'vec3' ) return GPUVertexFormat.Float3;
  506. if ( type === 'vec4' ) return GPUVertexFormat.Float4;
  507. if ( type === 'int' ) return GPUVertexFormat.Int;
  508. if ( type === 'ivec2' ) return GPUVertexFormat.Int2;
  509. if ( type === 'ivec3' ) return GPUVertexFormat.Int3;
  510. if ( type === 'ivec4' ) return GPUVertexFormat.Int4;
  511. if ( type === 'uint' ) return GPUVertexFormat.UInt;
  512. if ( type === 'uvec2' ) return GPUVertexFormat.UInt2;
  513. if ( type === 'uvec3' ) return GPUVertexFormat.UInt3;
  514. if ( type === 'uvec4' ) return GPUVertexFormat.UInt4;
  515. console.error( 'THREE.WebGPURenderer: Shader variable type not supported yet.', type );
  516. }
  517. _parseShaderAttributes( shader ) {
  518. // find "layout (location = num) in type name" in vertex shader
  519. const regex = /^\s*layout\s*\(\s*location\s*=\s*(?<location>[0-9]+)\s*\)\s*in\s+(?<type>\w+)\s+(?<name>\w+)\s*;/gmi;
  520. let shaderAttribute = null;
  521. const attributes = [];
  522. while ( shaderAttribute = regex.exec( shader ) ) {
  523. const shaderLocation = parseInt( shaderAttribute.groups.location );
  524. const arrayStride = this._getArrayStride( shaderAttribute.groups.type );
  525. const vertexFormat = this._getVertexFormat( shaderAttribute.groups.type );
  526. attributes.push( {
  527. name: shaderAttribute.groups.name,
  528. arrayStride: arrayStride,
  529. slot: shaderLocation,
  530. format: vertexFormat
  531. } );
  532. }
  533. // the sort ensures to setup vertex buffers in the correct order
  534. return attributes.sort( function ( a, b ) {
  535. return a.slot - b.slot;
  536. } );
  537. }
  538. }
  539. export default WebGPURenderPipelines;