WebGPURenderPipelines.js 19 KB

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