WGSLNodeBuilder.js 18 KB

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