WebGPURenderPipelines.js 19 KB

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