WGSLNodeBuilder.js 18 KB

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