WebGPURenderPipelines.js 19 KB

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