WebGPURenderPipelines.js 19 KB

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