WebGPURenderPipelines.js 19 KB

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