WGSLNodeBuilder.js 19 KB

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