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