WGSLNodeBuilder.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. getVertexIndex() {
  240. if ( this.shaderStage === 'vertex' ) {
  241. return this.getBuiltin( 'vertex_index', 'vertexIndex', 'u32', 'attribute' );
  242. }
  243. return 'vertexIndex';
  244. }
  245. getInstanceIndex() {
  246. if ( this.shaderStage === 'vertex' ) {
  247. return this.getBuiltin( 'instance_index', 'instanceIndex', 'u32', 'attribute' );
  248. }
  249. return 'instanceIndex';
  250. }
  251. getFrontFacing() {
  252. return this.getBuiltin( 'front_facing', 'isFront', 'bool' );
  253. }
  254. getFragCoord() {
  255. return this.getBuiltin( 'position', 'fragCoord', 'vec4<f32>', 'fragment' );
  256. }
  257. isFlipY() {
  258. return false;
  259. }
  260. getAttributes( shaderStage ) {
  261. const snippets = [];
  262. if ( shaderStage === 'compute' ) {
  263. this.getBuiltin( 'global_invocation_id', 'id', 'vec3<u32>', 'attribute' );
  264. }
  265. if ( shaderStage === 'vertex' || shaderStage === 'compute' ) {
  266. for ( const { name, property, type } of this.builtins.attribute.values() ) {
  267. snippets.push( `@builtin( ${name} ) ${property} : ${type}` );
  268. }
  269. const attributes = this.getAttributesArray();
  270. for ( let index = 0, length = attributes.length; index < length; index ++ ) {
  271. const attribute = attributes[ index ];
  272. const name = attribute.name;
  273. const type = this.getType( attribute.type );
  274. snippets.push( `@location( ${index} ) ${ name } : ${ type }` );
  275. }
  276. }
  277. return snippets.join( ',\n\t' );
  278. }
  279. getVar( type, name ) {
  280. return `var ${ name } : ${ this.getType( type ) }`;
  281. }
  282. getVars( shaderStage ) {
  283. const snippets = [];
  284. const vars = this.vars[ shaderStage ];
  285. for ( const variable of vars ) {
  286. snippets.push( `\t${ this.getVar( variable.type, variable.name ) };` );
  287. }
  288. return `\n${ snippets.join( '\n' ) }\n`;
  289. }
  290. getVaryings( shaderStage ) {
  291. const snippets = [];
  292. if ( shaderStage === 'vertex' ) {
  293. this.getBuiltin( 'position', 'Vertex', 'vec4<f32>', 'vertex' );
  294. }
  295. if ( shaderStage === 'vertex' || shaderStage === 'fragment' ) {
  296. const varyings = this.varyings;
  297. const vars = this.vars[ shaderStage ];
  298. for ( let index = 0; index < varyings.length; index ++ ) {
  299. const varying = varyings[ index ];
  300. if ( varying.needsInterpolation ) {
  301. let attributesSnippet = `@location( ${index} )`;
  302. if ( varying.type === 'int' || varying.type === 'uint' ) {
  303. attributesSnippet += ' @interpolate( flat )';
  304. }
  305. snippets.push( `${ attributesSnippet } ${ varying.name } : ${ this.getType( varying.type ) }` );
  306. } else if ( shaderStage === 'vertex' && vars.includes( varying ) === false ) {
  307. vars.push( varying );
  308. }
  309. }
  310. }
  311. for ( const { name, property, type } of this.builtins[ shaderStage ].values() ) {
  312. snippets.push( `@builtin( ${name} ) ${property} : ${type}` );
  313. }
  314. const code = snippets.join( ',\n\t' );
  315. return shaderStage === 'vertex' ? this._getWGSLStruct( 'NodeVaryingsStruct', '\t' + code ) : code;
  316. }
  317. getUniforms( shaderStage ) {
  318. const uniforms = this.uniforms[ shaderStage ];
  319. const bindingSnippets = [];
  320. const bufferSnippets = [];
  321. const groupSnippets = [];
  322. let index = this.bindingsOffset[ shaderStage ];
  323. for ( const uniform of uniforms ) {
  324. if ( uniform.type === 'texture' || uniform.type === 'cubeTexture' ) {
  325. if ( shaderStage === 'fragment' ) {
  326. bindingSnippets.push( `@binding( ${index ++} ) @group( 0 ) var ${uniform.name}_sampler : sampler;` );
  327. }
  328. const texture = uniform.node.value;
  329. let textureType;
  330. if ( texture.isCubeTexture === true ) {
  331. textureType = 'texture_cube<f32>';
  332. } else if ( texture.isDepthTexture === true ) {
  333. textureType = 'texture_depth_2d';
  334. } else if ( texture.isVideoTexture === true ) {
  335. textureType = 'texture_external';
  336. } else {
  337. textureType = 'texture_2d<f32>';
  338. }
  339. bindingSnippets.push( `@binding( ${index ++} ) @group( 0 ) var ${uniform.name} : ${textureType};` );
  340. } else if ( uniform.type === 'buffer' || uniform.type === 'storageBuffer' ) {
  341. const bufferNode = uniform.node;
  342. const bufferType = this.getType( bufferNode.bufferType );
  343. const bufferCount = bufferNode.bufferCount;
  344. const bufferCountSnippet = bufferCount > 0 ? ', ' + bufferCount : '';
  345. const bufferSnippet = `\t${uniform.name} : array< ${bufferType}${bufferCountSnippet} >\n`;
  346. const bufferAccessMode = bufferNode.isStorageBufferNode ? 'storage,read_write' : 'uniform';
  347. bufferSnippets.push( this._getWGSLStructBinding( 'NodeBuffer_' + bufferNode.id, bufferSnippet, bufferAccessMode, index ++ ) );
  348. } else {
  349. const vectorType = this.getType( this.getVectorType( uniform.type ) );
  350. if ( Array.isArray( uniform.value ) === true ) {
  351. const length = uniform.value.length;
  352. groupSnippets.push( `uniform ${vectorType}[ ${length} ] ${uniform.name}` );
  353. } else {
  354. groupSnippets.push( `\t${uniform.name} : ${ vectorType}` );
  355. }
  356. }
  357. }
  358. let code = bindingSnippets.join( '\n' );
  359. code += bufferSnippets.join( '\n' );
  360. if ( groupSnippets.length > 0 ) {
  361. code += this._getWGSLStructBinding( 'NodeUniforms', groupSnippets.join( ',\n' ), 'uniform', index ++ );
  362. }
  363. return code;
  364. }
  365. buildCode() {
  366. const shadersData = this.material !== null ? { fragment: {}, vertex: {} } : { compute: {} };
  367. for ( const shaderStage in shadersData ) {
  368. let flow = '// code\n\n';
  369. flow += this.flowCode[ shaderStage ];
  370. const flowNodes = this.flowNodes[ shaderStage ];
  371. const mainNode = flowNodes[ flowNodes.length - 1 ];
  372. for ( const node of flowNodes ) {
  373. const flowSlotData = this.getFlowData( node/*, shaderStage*/ );
  374. const slotName = node.name;
  375. if ( slotName ) {
  376. if ( flow.length > 0 ) flow += '\n';
  377. flow += `\t// flow -> ${ slotName }\n\t`;
  378. }
  379. flow += `${ flowSlotData.code }\n\t`;
  380. if ( node === mainNode && shaderStage !== 'compute' ) {
  381. flow += '// result\n\t';
  382. if ( shaderStage === 'vertex' ) {
  383. flow += 'NodeVaryings.Vertex = ';
  384. } else if ( shaderStage === 'fragment' ) {
  385. flow += 'return ';
  386. }
  387. flow += `${ flowSlotData.result };`;
  388. }
  389. }
  390. const stageData = shadersData[ shaderStage ];
  391. stageData.uniforms = this.getUniforms( shaderStage );
  392. stageData.attributes = this.getAttributes( shaderStage );
  393. stageData.varyings = this.getVaryings( shaderStage );
  394. stageData.vars = this.getVars( shaderStage );
  395. stageData.codes = this.getCodes( shaderStage );
  396. stageData.flow = flow;
  397. }
  398. if ( this.material !== null ) {
  399. this.vertexShader = this._getWGSLVertexCode( shadersData.vertex );
  400. this.fragmentShader = this._getWGSLFragmentCode( shadersData.fragment );
  401. } else {
  402. this.computeShader = this._getWGSLComputeCode( shadersData.compute, ( this.object.workgroupSize || [ 64 ] ).join( ', ' ) );
  403. }
  404. }
  405. getRenderTarget( width, height, options ) {
  406. return new RenderTarget( width, height, options );
  407. }
  408. getCubeRenderTarget( size, options ) {
  409. return new CubeRenderTarget( size, options );
  410. }
  411. getMethod( method ) {
  412. if ( wgslPolyfill[ method ] !== undefined ) {
  413. this._include( method );
  414. }
  415. return wgslMethods[ method ] || method;
  416. }
  417. getType( type ) {
  418. return wgslTypeLib[ type ] || type;
  419. }
  420. isAvailable( name ) {
  421. return supports[ name ] === true;
  422. }
  423. _include( name ) {
  424. wgslPolyfill[ name ].build( this );
  425. }
  426. _getNodeUniform( uniformNode, type ) {
  427. if ( type === 'float' ) return new FloatNodeUniform( uniformNode );
  428. if ( type === 'vec2' ) return new Vector2NodeUniform( uniformNode );
  429. if ( type === 'vec3' ) return new Vector3NodeUniform( uniformNode );
  430. if ( type === 'vec4' ) return new Vector4NodeUniform( uniformNode );
  431. if ( type === 'color' ) return new ColorNodeUniform( uniformNode );
  432. if ( type === 'mat3' ) return new Matrix3NodeUniform( uniformNode );
  433. if ( type === 'mat4' ) return new Matrix4NodeUniform( uniformNode );
  434. throw new Error( `Uniform "${type}" not declared.` );
  435. }
  436. _getWGSLVertexCode( shaderData ) {
  437. return `${ this.getSignature() }
  438. // uniforms
  439. ${shaderData.uniforms}
  440. // varyings
  441. ${shaderData.varyings}
  442. // codes
  443. ${shaderData.codes}
  444. @vertex
  445. fn main( ${shaderData.attributes} ) -> NodeVaryingsStruct {
  446. // system
  447. var NodeVaryings: NodeVaryingsStruct;
  448. // vars
  449. ${shaderData.vars}
  450. // flow
  451. ${shaderData.flow}
  452. return NodeVaryings;
  453. }
  454. `;
  455. }
  456. _getWGSLFragmentCode( shaderData ) {
  457. return `${ this.getSignature() }
  458. // uniforms
  459. ${shaderData.uniforms}
  460. // codes
  461. ${shaderData.codes}
  462. @fragment
  463. fn main( ${shaderData.varyings} ) -> @location( 0 ) vec4<f32> {
  464. // vars
  465. ${shaderData.vars}
  466. // flow
  467. ${shaderData.flow}
  468. }
  469. `;
  470. }
  471. _getWGSLComputeCode( shaderData, workgroupSize ) {
  472. return `${ this.getSignature() }
  473. // system
  474. var<private> instanceIndex : u32;
  475. // uniforms
  476. ${shaderData.uniforms}
  477. // codes
  478. ${shaderData.codes}
  479. @compute @workgroup_size( ${workgroupSize} )
  480. fn main( ${shaderData.attributes} ) {
  481. // system
  482. instanceIndex = id.x;
  483. // vars
  484. ${shaderData.vars}
  485. // flow
  486. ${shaderData.flow}
  487. }
  488. `;
  489. }
  490. _getWGSLStruct( name, vars ) {
  491. return `
  492. struct ${name} {
  493. ${vars}
  494. };`;
  495. }
  496. _getWGSLStructBinding( name, vars, access, binding = 0, group = 0 ) {
  497. const structName = name + 'Struct';
  498. const structSnippet = this._getWGSLStruct( structName, vars );
  499. return `${structSnippet}
  500. @binding( ${binding} ) @group( ${group} )
  501. var<${access}> ${name} : ${structName};`;
  502. }
  503. }
  504. export default WGSLNodeBuilder;