WebGPUNodeBuilder.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. import WebGPUUniformsGroup from '../WebGPUUniformsGroup.js';
  2. import {
  3. FloatNodeUniform, Vector2NodeUniform, Vector3NodeUniform, Vector4NodeUniform,
  4. ColorNodeUniform, Matrix3NodeUniform, Matrix4NodeUniform
  5. } from './WebGPUNodeUniform.js';
  6. import WebGPUNodeSampler from './WebGPUNodeSampler.js';
  7. import { WebGPUNodeSampledTexture, WebGPUNodeSampledCubeTexture } from './WebGPUNodeSampledTexture.js';
  8. import WebGPUUniformBuffer from '../WebGPUUniformBuffer.js';
  9. import WebGPUStorageBuffer from '../WebGPUStorageBuffer.js';
  10. import { getVectorLength, getStrideLength } from '../WebGPUBufferUtils.js';
  11. import { NodeBuilder, WGSLNodeParser, CodeNode, NodeMaterial } from 'three/nodes';
  12. const gpuShaderStageLib = {
  13. 'vertex': GPUShaderStage.VERTEX,
  14. 'fragment': GPUShaderStage.FRAGMENT,
  15. 'compute': GPUShaderStage.COMPUTE
  16. };
  17. const supports = {
  18. instance: true
  19. };
  20. const wgslTypeLib = {
  21. float: 'f32',
  22. int: 'i32',
  23. uint: 'u32',
  24. bool: 'bool',
  25. color: 'vec3<f32>',
  26. vec2: 'vec2<f32>',
  27. ivec2: 'vec2<i32>',
  28. uvec2: 'vec2<u32>',
  29. bvec2: 'vec2<bool>',
  30. vec3: 'vec3<f32>',
  31. ivec3: 'vec3<i32>',
  32. uvec3: 'vec3<u32>',
  33. bvec3: 'vec3<bool>',
  34. vec4: 'vec4<f32>',
  35. ivec4: 'vec4<i32>',
  36. uvec4: 'vec4<u32>',
  37. bvec4: 'vec4<bool>',
  38. mat3: 'mat3x3<f32>',
  39. imat3: 'mat3x3<i32>',
  40. umat3: 'mat3x3<u32>',
  41. bmat3: 'mat3x3<bool>',
  42. mat4: 'mat4x4<f32>',
  43. imat4: 'mat4x4<i32>',
  44. umat4: 'mat4x4<u32>',
  45. bmat4: 'mat4x4<bool>'
  46. };
  47. const wgslMethods = {
  48. dFdx: 'dpdx',
  49. dFdy: 'dpdy',
  50. mod: 'threejs_mod',
  51. lessThanEqual: 'threejs_lessThanEqual',
  52. inversesqrt: 'inverseSqrt'
  53. };
  54. const wgslPolyfill = {
  55. lessThanEqual: new CodeNode( `
  56. fn threejs_lessThanEqual( a : vec3<f32>, b : vec3<f32> ) -> vec3<bool> {
  57. return vec3<bool>( a.x <= b.x, a.y <= b.y, a.z <= b.z );
  58. }
  59. ` ),
  60. mod: new CodeNode( `
  61. fn threejs_mod( x : f32, y : f32 ) -> f32 {
  62. return x - y * floor( x / y );
  63. }
  64. ` ),
  65. repeatWrapping: new CodeNode( `
  66. fn threejs_repeatWrapping( uv : vec2<f32>, dimension : vec2<u32> ) -> vec2<u32> {
  67. let uvScaled = vec2<u32>( uv * vec2<f32>( dimension ) );
  68. return ( ( uvScaled % dimension ) + dimension ) % dimension;
  69. }
  70. ` )
  71. };
  72. class WebGPUNodeBuilder extends NodeBuilder {
  73. constructor( object, renderer ) {
  74. super( object, renderer, new WGSLNodeParser() );
  75. this.bindings = { vertex: [], fragment: [], compute: [] };
  76. this.bindingsOffset = { vertex: 0, fragment: 0, compute: 0 };
  77. this.uniformsGroup = {};
  78. this.builtins = {
  79. vertex: new Map(),
  80. fragment: new Map(),
  81. compute: new Map(),
  82. attribute: new Map()
  83. };
  84. }
  85. build() {
  86. const { object, material } = this;
  87. if ( material !== null ) {
  88. NodeMaterial.fromMaterial( material ).build( this );
  89. } else {
  90. this.addFlow( 'compute', object );
  91. }
  92. return super.build();
  93. }
  94. getSampler( textureProperty, uvSnippet, shaderStage = this.shaderStage ) {
  95. if ( shaderStage === 'fragment' ) {
  96. return `textureSample( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet} )`;
  97. } else {
  98. this._include( 'repeatWrapping' );
  99. const dimension = `textureDimensions( ${textureProperty}, 0 )`;
  100. return `textureLoad( ${textureProperty}, threejs_repeatWrapping( ${uvSnippet}, ${dimension} ), 0 )`;
  101. }
  102. }
  103. getSamplerLevel( textureProperty, uvSnippet, biasSnippet, shaderStage = this.shaderStage ) {
  104. if ( shaderStage === 'fragment' ) {
  105. return `textureSampleLevel( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet}, ${biasSnippet} )`;
  106. } else {
  107. this._include( 'repeatWrapping' );
  108. const dimension = `textureDimensions( ${textureProperty}, 0 )`;
  109. return `textureLoad( ${textureProperty}, threejs_repeatWrapping( ${uvSnippet}, ${dimension} ), i32( ${biasSnippet} ) )`;
  110. }
  111. }
  112. getTexture( textureProperty, uvSnippet, shaderStage = this.shaderStage ) {
  113. return this.getSampler( textureProperty, uvSnippet, shaderStage );
  114. }
  115. getTextureLevel( textureProperty, uvSnippet, biasSnippet, shaderStage = this.shaderStage ) {
  116. return this.getSamplerLevel( textureProperty, uvSnippet, biasSnippet, shaderStage );
  117. }
  118. getCubeTexture( textureProperty, uvSnippet, shaderStage = this.shaderStage ) {
  119. return this.getSampler( textureProperty, uvSnippet, shaderStage );
  120. }
  121. getCubeTextureLevel( textureProperty, uvSnippet, biasSnippet, shaderStage = this.shaderStage ) {
  122. return this.getSamplerLevel( textureProperty, uvSnippet, biasSnippet, shaderStage );
  123. }
  124. getVideoTexture( textureProperty, uvSnippet, shaderStage = this.shaderStage ) {
  125. if ( shaderStage === 'fragment' ) {
  126. return `textureSampleBaseClampToEdge( ${textureProperty}, ${textureProperty}_sampler, vec2<f32>( ${uvSnippet}.x, 1.0 - ${uvSnippet}.y ) )`;
  127. } else {
  128. console.error( `WebGPURenderer: THREE.VideoTexture does not support ${ shaderStage } shader.` );
  129. }
  130. }
  131. getPropertyName( node, shaderStage = this.shaderStage ) {
  132. if ( node.isNodeVarying === true && node.needsInterpolation === true ) {
  133. if ( shaderStage === 'vertex' ) {
  134. return `NodeVaryings.${ node.name }`;
  135. }
  136. } else if ( node.isNodeUniform === true ) {
  137. const name = node.name;
  138. const type = node.type;
  139. if ( type === 'texture' || type === 'cubeTexture' ) {
  140. return name;
  141. } else if ( type === 'buffer' || type === 'storageBuffer' ) {
  142. return `NodeBuffer_${node.node.id}.${name}`;
  143. } else {
  144. return `NodeUniforms.${name}`;
  145. }
  146. }
  147. return super.getPropertyName( node );
  148. }
  149. getBindings() {
  150. const bindings = this.bindings;
  151. return this.material !== null ? [ ...bindings.vertex, ...bindings.fragment ] : bindings.compute;
  152. }
  153. getUniformFromNode( node, shaderStage, type ) {
  154. const uniformNode = super.getUniformFromNode( node, shaderStage, type );
  155. const nodeData = this.getDataFromNode( node, shaderStage );
  156. if ( nodeData.uniformGPU === undefined ) {
  157. let uniformGPU;
  158. const bindings = this.bindings[ shaderStage ];
  159. if ( type === 'texture' || type === 'cubeTexture' ) {
  160. const sampler = new WebGPUNodeSampler( `${uniformNode.name}_sampler`, uniformNode.node );
  161. let texture = null;
  162. if ( type === 'texture' ) {
  163. texture = new WebGPUNodeSampledTexture( uniformNode.name, uniformNode.node );
  164. } else if ( type === 'cubeTexture' ) {
  165. texture = new WebGPUNodeSampledCubeTexture( uniformNode.name, uniformNode.node );
  166. }
  167. // add first textures in sequence and group for last
  168. const lastBinding = bindings[ bindings.length - 1 ];
  169. const index = lastBinding && lastBinding.isUniformsGroup ? bindings.length - 1 : bindings.length;
  170. if ( shaderStage === 'fragment' ) {
  171. bindings.splice( index, 0, sampler, texture );
  172. uniformGPU = [ sampler, texture ];
  173. } else {
  174. bindings.splice( index, 0, texture );
  175. uniformGPU = [ texture ];
  176. }
  177. } else if ( type === 'buffer' || type === 'storageBuffer' ) {
  178. const bufferClass = type === 'storageBuffer' ? WebGPUStorageBuffer : WebGPUUniformBuffer;
  179. const buffer = new bufferClass( 'NodeBuffer_' + node.id, node.value );
  180. buffer.setVisibility( gpuShaderStageLib[ shaderStage ] );
  181. // add first textures in sequence and group for last
  182. const lastBinding = bindings[ bindings.length - 1 ];
  183. const index = lastBinding && lastBinding.isUniformsGroup ? bindings.length - 1 : bindings.length;
  184. bindings.splice( index, 0, buffer );
  185. uniformGPU = buffer;
  186. } else {
  187. let uniformsGroup = this.uniformsGroup[ shaderStage ];
  188. if ( uniformsGroup === undefined ) {
  189. uniformsGroup = new WebGPUUniformsGroup( 'nodeUniforms' );
  190. uniformsGroup.setVisibility( gpuShaderStageLib[ shaderStage ] );
  191. this.uniformsGroup[ shaderStage ] = uniformsGroup;
  192. bindings.push( uniformsGroup );
  193. }
  194. if ( node.isArrayUniformNode === true ) {
  195. uniformGPU = [];
  196. for ( const uniformNode of node.nodes ) {
  197. const uniformNodeGPU = this._getNodeUniform( uniformNode, type );
  198. // fit bounds to buffer
  199. uniformNodeGPU.boundary = getVectorLength( uniformNodeGPU.itemSize );
  200. uniformNodeGPU.itemSize = getStrideLength( uniformNodeGPU.itemSize );
  201. uniformsGroup.addUniform( uniformNodeGPU );
  202. uniformGPU.push( uniformNodeGPU );
  203. }
  204. } else {
  205. uniformGPU = this._getNodeUniform( uniformNode, type );
  206. uniformsGroup.addUniform( uniformGPU );
  207. }
  208. }
  209. nodeData.uniformGPU = uniformGPU;
  210. if ( shaderStage === 'vertex' ) {
  211. this.bindingsOffset[ 'fragment' ] = bindings.length;
  212. }
  213. }
  214. return uniformNode;
  215. }
  216. isReference( type ) {
  217. return super.isReference( type ) || type === 'texture_2d' || type === 'texture_cube';
  218. }
  219. getBuiltin( name, property, type, shaderStage = this.shaderStage ) {
  220. const map = this.builtins[ shaderStage ];
  221. if ( map.has( name ) === false ) {
  222. map.set( name, {
  223. name,
  224. property,
  225. type
  226. } );
  227. }
  228. return property;
  229. }
  230. getInstanceIndex() {
  231. if ( this.shaderStage === 'vertex' ) {
  232. return this.getBuiltin( 'instance_index', 'instanceIndex', 'u32', 'attribute' );
  233. }
  234. return 'instanceIndex';
  235. }
  236. getFrontFacing() {
  237. return this.getBuiltin( 'front_facing', 'isFront', 'bool' );
  238. }
  239. getFragCoord() {
  240. return this.getBuiltin( 'position', 'fragCoord', 'vec4<f32>', 'fragment' );
  241. }
  242. isFlipY() {
  243. return false;
  244. }
  245. getAttributes( shaderStage ) {
  246. const snippets = [];
  247. if ( shaderStage === 'vertex' || shaderStage === 'compute' ) {
  248. if ( shaderStage === 'compute' ) {
  249. this.getBuiltin( 'global_invocation_id', 'id', 'vec3<u32>', 'attribute' );
  250. }
  251. for ( const { name, property, type } of this.builtins.attribute.values() ) {
  252. snippets.push( `@builtin( ${name} ) ${property} : ${type}` );
  253. }
  254. const attributes = this.attributes;
  255. const length = attributes.length;
  256. for ( let index = 0; index < length; index ++ ) {
  257. const attribute = attributes[ index ];
  258. const name = attribute.name;
  259. const type = this.getType( attribute.type );
  260. snippets.push( `@location( ${index} ) ${ name } : ${ type }` );
  261. }
  262. }
  263. return snippets.join( ',\n\t' );
  264. }
  265. getVars( shaderStage ) {
  266. const snippets = [];
  267. const vars = this.vars[ shaderStage ];
  268. for ( const variable of vars ) {
  269. const name = variable.name;
  270. const type = this.getType( variable.type );
  271. snippets.push( `\tvar ${name} : ${type};` );
  272. }
  273. return `\n${ snippets.join( '\n' ) }\n`;
  274. }
  275. getVaryings( shaderStage ) {
  276. const snippets = [];
  277. if ( shaderStage === 'vertex' ) {
  278. this.getBuiltin( 'position', 'Vertex', 'vec4<f32>', 'vertex' );
  279. const varyings = this.varyings;
  280. const vars = this.vars[ shaderStage ];
  281. for ( let index = 0; index < varyings.length; index ++ ) {
  282. const varying = varyings[ index ];
  283. if ( varying.needsInterpolation ) {
  284. snippets.push( `@location( ${index} ) ${ varying.name } : ${ this.getType( varying.type ) }` );
  285. } else if ( vars.includes( varying ) === false ) {
  286. vars.push( varying );
  287. }
  288. }
  289. } else if ( shaderStage === 'fragment' ) {
  290. const varyings = this.varyings;
  291. const vars = this.vars[ shaderStage ];
  292. for ( let index = 0; index < varyings.length; index ++ ) {
  293. const varying = varyings[ index ];
  294. if ( varying.needsInterpolation ) {
  295. snippets.push( `@location( ${index} ) ${ varying.name } : ${ this.getType( varying.type ) }` );
  296. } else if ( vars.includes( varying ) === false ) {
  297. vars.push( varying );
  298. }
  299. }
  300. }
  301. for ( const { name, property, type } of this.builtins[ shaderStage ].values() ) {
  302. snippets.push( `@builtin( ${name} ) ${property} : ${type}` );
  303. }
  304. const code = snippets.join( ',\n\t' );
  305. return shaderStage === 'vertex' ? this._getWGSLStruct( 'NodeVaryingsStruct', '\t' + code ) : code;
  306. }
  307. getUniforms( shaderStage ) {
  308. const uniforms = this.uniforms[ shaderStage ];
  309. const bindingSnippets = [];
  310. const bufferSnippets = [];
  311. const groupSnippets = [];
  312. let index = this.bindingsOffset[ shaderStage ];
  313. for ( const uniform of uniforms ) {
  314. if ( uniform.type === 'texture' ) {
  315. if ( shaderStage === 'fragment' ) {
  316. bindingSnippets.push( `@group( 0 ) @binding( ${index ++} ) var ${uniform.name}_sampler : sampler;` );
  317. }
  318. const texture = uniform.node.value;
  319. if ( texture.isVideoTexture === true ) {
  320. bindingSnippets.push( `@group( 0 ) @binding( ${index ++} ) var ${uniform.name} : texture_external;` );
  321. } else {
  322. bindingSnippets.push( `@group( 0 ) @binding( ${index ++} ) var ${uniform.name} : texture_2d<f32>;` );
  323. }
  324. } else if ( uniform.type === 'cubeTexture' ) {
  325. if ( shaderStage === 'fragment' ) {
  326. bindingSnippets.push( `@group( 0 ) @binding( ${index ++} ) var ${uniform.name}_sampler : sampler;` );
  327. }
  328. bindingSnippets.push( `@group( 0 ) @binding( ${index ++} ) var ${uniform.name} : texture_cube<f32>;` );
  329. } else if ( uniform.type === 'buffer' || uniform.type === 'storageBuffer' ) {
  330. const bufferNode = uniform.node;
  331. const bufferType = this.getType( bufferNode.bufferType );
  332. const bufferCount = bufferNode.bufferCount;
  333. const bufferCountSnippet = bufferCount > 0 ? ', ' + bufferCount : '';
  334. const bufferSnippet = `\t${uniform.name} : array< ${bufferType}${bufferCountSnippet} >\n`;
  335. const bufferAccessMode = bufferNode.isStorageBufferNode ? 'storage,read_write' : 'uniform';
  336. bufferSnippets.push( this._getWGSLStructBinding( 'NodeBuffer_' + bufferNode.id, bufferSnippet, bufferAccessMode, index ++ ) );
  337. } else {
  338. const vectorType = this.getType( this.getVectorType( uniform.type ) );
  339. if ( Array.isArray( uniform.value ) === true ) {
  340. const length = uniform.value.length;
  341. groupSnippets.push( `uniform ${vectorType}[ ${length} ] ${uniform.name}` );
  342. } else {
  343. groupSnippets.push( `\t${uniform.name} : ${ vectorType}` );
  344. }
  345. }
  346. }
  347. let code = bindingSnippets.join( '\n' );
  348. code += bufferSnippets.join( '\n' );
  349. if ( groupSnippets.length > 0 ) {
  350. code += this._getWGSLStructBinding( 'NodeUniforms', groupSnippets.join( ',\n' ), 'uniform', index ++ );
  351. }
  352. return code;
  353. }
  354. buildCode() {
  355. const shadersData = this.material !== null ? { fragment: {}, vertex: {} } : { compute: {} };
  356. for ( const shaderStage in shadersData ) {
  357. let flow = '// code\n\n';
  358. flow += this.flowCode[ shaderStage ];
  359. const flowNodes = this.flowNodes[ shaderStage ];
  360. const mainNode = flowNodes[ flowNodes.length - 1 ];
  361. for ( const node of flowNodes ) {
  362. const flowSlotData = this.getFlowData( node/*, shaderStage*/ );
  363. const slotName = node.name;
  364. if ( slotName ) {
  365. if ( flow.length > 0 ) flow += '\n';
  366. flow += `\t// flow -> ${ slotName }\n\t`;
  367. }
  368. flow += `${ flowSlotData.code }\n\t`;
  369. if ( node === mainNode && shaderStage !== 'compute' ) {
  370. flow += '// result\n\t';
  371. if ( shaderStage === 'vertex' ) {
  372. flow += 'NodeVaryings.Vertex = ';
  373. } else if ( shaderStage === 'fragment' ) {
  374. flow += 'return ';
  375. }
  376. flow += `${ flowSlotData.result };`;
  377. }
  378. }
  379. const stageData = shadersData[ shaderStage ];
  380. stageData.uniforms = this.getUniforms( shaderStage );
  381. stageData.attributes = this.getAttributes( shaderStage );
  382. stageData.varyings = this.getVaryings( shaderStage );
  383. stageData.vars = this.getVars( shaderStage );
  384. stageData.codes = this.getCodes( shaderStage );
  385. stageData.flow = flow;
  386. }
  387. if ( this.material !== null ) {
  388. this.vertexShader = this._getWGSLVertexCode( shadersData.vertex );
  389. this.fragmentShader = this._getWGSLFragmentCode( shadersData.fragment );
  390. } else {
  391. this.computeShader = this._getWGSLComputeCode( shadersData.compute, ( this.object.workgroupSize || [ 64 ] ).join( ', ' ) );
  392. }
  393. }
  394. getMethod( method ) {
  395. if ( wgslPolyfill[ method ] !== undefined ) {
  396. this._include( method );
  397. }
  398. return wgslMethods[ method ] || method;
  399. }
  400. getType( type ) {
  401. return wgslTypeLib[ type ] || type;
  402. }
  403. isAvailable( name ) {
  404. return supports[ name ] === true;
  405. }
  406. _include( name ) {
  407. wgslPolyfill[ name ].build( this );
  408. }
  409. _getNodeUniform( uniformNode, type ) {
  410. if ( type === 'float' ) return new FloatNodeUniform( uniformNode );
  411. if ( type === 'vec2' ) return new Vector2NodeUniform( uniformNode );
  412. if ( type === 'vec3' ) return new Vector3NodeUniform( uniformNode );
  413. if ( type === 'vec4' ) return new Vector4NodeUniform( uniformNode );
  414. if ( type === 'color' ) return new ColorNodeUniform( uniformNode );
  415. if ( type === 'mat3' ) return new Matrix3NodeUniform( uniformNode );
  416. if ( type === 'mat4' ) return new Matrix4NodeUniform( uniformNode );
  417. throw new Error( `Uniform "${type}" not declared.` );
  418. }
  419. _getWGSLVertexCode( shaderData ) {
  420. return `${ this.getSignature() }
  421. // uniforms
  422. ${shaderData.uniforms}
  423. // varyings
  424. ${shaderData.varyings}
  425. // codes
  426. ${shaderData.codes}
  427. @vertex
  428. fn main( ${shaderData.attributes} ) -> NodeVaryingsStruct {
  429. // system
  430. var NodeVaryings: NodeVaryingsStruct;
  431. // vars
  432. ${shaderData.vars}
  433. // flow
  434. ${shaderData.flow}
  435. return NodeVaryings;
  436. }
  437. `;
  438. }
  439. _getWGSLFragmentCode( shaderData ) {
  440. return `${ this.getSignature() }
  441. // uniforms
  442. ${shaderData.uniforms}
  443. // codes
  444. ${shaderData.codes}
  445. @fragment
  446. fn main( ${shaderData.varyings} ) -> @location( 0 ) vec4<f32> {
  447. // vars
  448. ${shaderData.vars}
  449. // flow
  450. ${shaderData.flow}
  451. }
  452. `;
  453. }
  454. _getWGSLComputeCode( shaderData, workgroupSize ) {
  455. return `${ this.getSignature() }
  456. // system
  457. var<private> instanceIndex : u32;
  458. // uniforms
  459. ${shaderData.uniforms}
  460. // codes
  461. ${shaderData.codes}
  462. @compute @workgroup_size( ${workgroupSize} )
  463. fn main( ${shaderData.attributes} ) {
  464. // system
  465. instanceIndex = id.x;
  466. // vars
  467. ${shaderData.vars}
  468. // flow
  469. ${shaderData.flow}
  470. }
  471. `;
  472. }
  473. _getWGSLStruct( name, vars ) {
  474. return `
  475. struct ${name} {
  476. ${vars}
  477. };`;
  478. }
  479. _getWGSLStructBinding( name, vars, access, binding = 0, group = 0 ) {
  480. const structName = name + 'Struct';
  481. const structSnippet = this._getWGSLStruct( structName, vars );
  482. return `${structSnippet}
  483. @binding( ${binding} ) @group( ${group} )
  484. var<${access}> ${name} : ${structName};`;
  485. }
  486. }
  487. export default WebGPUNodeBuilder;