WebGPUNodeBuilder.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. import { LinearEncoding } from 'three';
  2. import WebGPUNodeUniformsGroup from './WebGPUNodeUniformsGroup.js';
  3. import {
  4. FloatNodeUniform, Vector2NodeUniform, Vector3NodeUniform, Vector4NodeUniform,
  5. ColorNodeUniform, Matrix3NodeUniform, Matrix4NodeUniform
  6. } from './WebGPUNodeUniform.js';
  7. import WebGPUNodeSampler from './WebGPUNodeSampler.js';
  8. import { WebGPUNodeSampledTexture } from './WebGPUNodeSampledTexture.js';
  9. import WebGPUUniformBuffer from '../WebGPUUniformBuffer.js';
  10. import { getVectorLength, getStrideLength } from '../WebGPUBufferUtils.js';
  11. import VarNode from 'three-nodes/core/VarNode.js';
  12. import CodeNode from 'three-nodes/core/CodeNode.js';
  13. import BypassNode from 'three-nodes/core/BypassNode.js';
  14. import ExpressionNode from 'three-nodes/core/ExpressionNode.js';
  15. import NodeBuilder from 'three-nodes/core/NodeBuilder.js';
  16. import MaterialNode from 'three-nodes/accessors/MaterialNode.js';
  17. import PositionNode from 'three-nodes/accessors/PositionNode.js';
  18. import NormalNode from 'three-nodes/accessors/NormalNode.js';
  19. import ModelViewProjectionNode from 'three-nodes/accessors/ModelViewProjectionNode.js';
  20. import SkinningNode from 'three-nodes/accessors/SkinningNode.js';
  21. import ColorSpaceNode from 'three-nodes/display/ColorSpaceNode.js';
  22. import LightContextNode from 'three-nodes/lights/LightContextNode.js';
  23. import OperatorNode from 'three-nodes/math/OperatorNode.js';
  24. import WGSLNodeParser from 'three-nodes/parsers/WGSLNodeParser.js';
  25. import { vec3, add, join, mix, nodeObject } from 'three-nodes/ShaderNode.js';
  26. import { getRoughness } from 'three-nodes/functions/PhysicalMaterialFunctions.js';
  27. const wgslTypeLib = {
  28. float: 'f32',
  29. int: 'i32',
  30. uint: 'u32',
  31. bool: 'bool',
  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. };
  57. const wgslPolyfill = {
  58. lessThanEqual: new CodeNode( `
  59. fn 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 mod( x : f32, y : f32 ) -> f32 {
  65. return x - y * floor( x / y );
  66. }
  67. ` ),
  68. smoothstep: new CodeNode( `
  69. fn smoothstep( low : f32, high : f32, x : f32 ) -> f32 {
  70. let t = clamp( ( x - low ) / ( high - low ), 0.0, 1.0 );
  71. return t * t * ( 3.0 - 2.0 * t );
  72. }
  73. ` ),
  74. repeatWrapping: new CodeNode( `
  75. fn repeatWrapping( uv : vec2<f32>, dimension : vec2<i32> ) -> vec2<i32> {
  76. let uvScaled = vec2<i32>( uv * vec2<f32>( dimension ) );
  77. return ( ( uvScaled % dimension ) + dimension ) % dimension;
  78. }
  79. ` ),
  80. inversesqrt: new CodeNode( `
  81. fn inversesqrt( x : f32 ) -> f32 {
  82. return 1.0 / sqrt( x );
  83. }
  84. ` )
  85. };
  86. class WebGPUNodeBuilder extends NodeBuilder {
  87. constructor( object, renderer ) {
  88. super( object, renderer, new WGSLNodeParser() );
  89. this.lightNode = null;
  90. this.fogNode = null;
  91. this.bindings = { vertex: [], fragment: [] };
  92. this.bindingsOffset = { vertex: 0, fragment: 0 };
  93. this.uniformsGroup = {};
  94. }
  95. build() {
  96. this._parseObject();
  97. return super.build();
  98. }
  99. _parseObject() {
  100. const object = this.object;
  101. const material = this.material;
  102. // parse inputs
  103. if ( material.isMeshStandardMaterial || material.isMeshBasicMaterial || material.isPointsMaterial || material.isLineBasicMaterial ) {
  104. let lightNode = material.lightNode;
  105. if ( material.isMeshStandardMaterial && lightNode === null && this.lightNode ) {
  106. // use scene lights
  107. lightNode = this.lightNode;
  108. }
  109. // VERTEX STAGE
  110. let vertex = new PositionNode( PositionNode.GEOMETRY );
  111. if ( material.positionNode && material.positionNode.isNode ) {
  112. const assignPositionNode = new OperatorNode( '=', new PositionNode( PositionNode.LOCAL ), material.positionNode );
  113. vertex = new BypassNode( vertex, assignPositionNode );
  114. }
  115. if ( object.isSkinnedMesh === true ) {
  116. vertex = new BypassNode( vertex, new SkinningNode( object ) );
  117. }
  118. this.context.vertex = vertex;
  119. this.addFlow( 'vertex', new VarNode( new ModelViewProjectionNode(), 'MVP', 'vec4' ) );
  120. // COLOR
  121. let colorNode = null;
  122. if ( material.colorNode && material.colorNode.isNode ) {
  123. colorNode = material.colorNode;
  124. } else {
  125. colorNode = new MaterialNode( MaterialNode.COLOR );
  126. }
  127. colorNode = this.addFlow( 'fragment', new VarNode( colorNode, 'Color', 'vec4' ) );
  128. const diffuseColorNode = this.addFlow( 'fragment', new VarNode( colorNode, 'DiffuseColor', 'vec4' ) );
  129. // OPACITY
  130. let opacityNode = null;
  131. if ( material.opacityNode && material.opacityNode.isNode ) {
  132. opacityNode = material.opacityNode;
  133. } else {
  134. opacityNode = new VarNode( new MaterialNode( MaterialNode.OPACITY ) );
  135. }
  136. this.addFlow( 'fragment', new VarNode( opacityNode, 'OPACITY', 'float' ) );
  137. this.addFlow( 'fragment', new ExpressionNode( 'DiffuseColor.a = DiffuseColor.a * OPACITY;' ) );
  138. // ALPHA TEST
  139. let alphaTest = null;
  140. if ( material.alphaTestNode && material.alphaTestNode.isNode ) {
  141. alphaTest = material.alphaTestNode;
  142. } else if ( material.alphaTest > 0 ) {
  143. alphaTest = new MaterialNode( MaterialNode.ALPHA_TEST );
  144. }
  145. if ( alphaTest !== null ) {
  146. this.addFlow( 'fragment', new VarNode( alphaTest, 'AlphaTest', 'float' ) );
  147. this.addFlow( 'fragment', new ExpressionNode( 'if ( DiffuseColor.a <= AlphaTest ) { discard; }' ) );
  148. }
  149. if ( material.isMeshStandardMaterial ) {
  150. // METALNESS
  151. let metalnessNode = null;
  152. if ( material.metalnessNode && material.metalnessNode.isNode ) {
  153. metalnessNode = material.metalnessNode;
  154. } else {
  155. metalnessNode = new MaterialNode( MaterialNode.METALNESS );
  156. }
  157. this.addFlow( 'fragment', new VarNode( metalnessNode, 'Metalness', 'float' ) );
  158. this.addFlow( 'fragment', new ExpressionNode( 'DiffuseColor = vec4<f32>( DiffuseColor.rgb * ( 1.0 - Metalness ), DiffuseColor.a );' ) );
  159. // ROUGHNESS
  160. let roughnessNode = null;
  161. if ( material.roughnessNode && material.roughnessNode.isNode ) {
  162. roughnessNode = material.roughnessNode;
  163. } else {
  164. roughnessNode = new MaterialNode( MaterialNode.ROUGHNESS );
  165. }
  166. roughnessNode = getRoughness( { roughness: roughnessNode } );
  167. this.addFlow( 'fragment', new VarNode( roughnessNode, 'Roughness', 'float' ) );
  168. // SPECULAR_TINT
  169. this.addFlow( 'fragment', new VarNode( new ExpressionNode( 'mix( vec3<f32>( 0.04 ), Color.rgb, Metalness )', 'vec3' ), 'SpecularColor', 'color' ) );
  170. // NORMAL_VIEW
  171. let normalNode = null;
  172. if ( material.normalNode && material.normalNode.isNode ) {
  173. normalNode = material.normalNode;
  174. } else {
  175. normalNode = new NormalNode( NormalNode.VIEW );
  176. }
  177. this.addFlow( 'fragment', new VarNode( normalNode, 'TransformedNormalView', 'vec3' ) );
  178. }
  179. // LIGHT
  180. let outputNode = diffuseColorNode;
  181. if ( lightNode && lightNode.isNode && lightNode.hasLight !== false ) {
  182. const lightContextNode = new LightContextNode( lightNode );
  183. outputNode = this.addFlow( 'fragment', new VarNode( lightContextNode, 'Light', 'vec3' ) );
  184. }
  185. // OUTGOING LIGHT
  186. let outgoingLightNode = vec3( outputNode );
  187. // EMISSIVE
  188. const emissiveNode = material.emissiveNode;
  189. if ( emissiveNode && emissiveNode.isNode ) {
  190. outgoingLightNode = add( emissiveNode, outgoingLightNode );
  191. }
  192. // OUTPUT
  193. outputNode = join( vec3( outgoingLightNode ), nodeObject( diffuseColorNode ).w );
  194. // ENCODING
  195. const outputEncoding = this.renderer.outputEncoding;
  196. if ( outputEncoding !== LinearEncoding ) {
  197. outputNode = new ColorSpaceNode( ColorSpaceNode.LINEAR_TO_LINEAR, outputNode );
  198. outputNode.fromEncoding( outputEncoding );
  199. }
  200. // FOG
  201. const fogNode = this.fogNode;
  202. if ( fogNode && fogNode.isFogNode ) {
  203. outputNode = mix( outputNode, fogNode.colorNode, fogNode );
  204. }
  205. // RESULT
  206. this.addFlow( 'fragment', new VarNode( outputNode, 'Output', 'vec4' ) );
  207. }
  208. }
  209. addFlowCode( code ) {
  210. if ( ! /;\s*$/.test( code ) ) {
  211. code += ';';
  212. }
  213. super.addFlowCode( code + '\n\t' );
  214. }
  215. getTexture( textureProperty, uvSnippet, biasSnippet, shaderStage = this.shaderStage ) {
  216. if ( shaderStage === 'fragment' ) {
  217. return `textureSample( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet} )`;
  218. } else {
  219. this._include( 'repeatWrapping' );
  220. const dimension = `textureDimensions( ${textureProperty}, 0 )`;
  221. return `textureLoad( ${textureProperty}, repeatWrapping( ${uvSnippet}, ${dimension} ), 0 )`;
  222. }
  223. }
  224. getPropertyName( node, shaderStage = this.shaderStage ) {
  225. if ( node.isNodeVary === true ) {
  226. if ( shaderStage === 'vertex' ) {
  227. return `NodeVarys.${ node.name }`;
  228. }
  229. } else if ( node.isNodeUniform === true ) {
  230. const name = node.name;
  231. const type = node.type;
  232. if ( type === 'texture' ) {
  233. return name;
  234. } else if ( type === 'buffer' ) {
  235. return `NodeBuffer.${name}`;
  236. } else {
  237. return `NodeUniforms.${name}`;
  238. }
  239. }
  240. return super.getPropertyName( node );
  241. }
  242. getBindings() {
  243. const bindings = this.bindings;
  244. return [ ...bindings.vertex, ...bindings.fragment ];
  245. }
  246. getUniformFromNode( node, shaderStage, type ) {
  247. const uniformNode = super.getUniformFromNode( node, shaderStage, type );
  248. const nodeData = this.getDataFromNode( node, shaderStage );
  249. if ( nodeData.uniformGPU === undefined ) {
  250. let uniformGPU;
  251. const bindings = this.bindings[ shaderStage ];
  252. if ( type === 'texture' ) {
  253. const sampler = new WebGPUNodeSampler( `${uniformNode.name}_sampler`, uniformNode.node );
  254. const texture = new WebGPUNodeSampledTexture( uniformNode.name, uniformNode.node );
  255. // add first textures in sequence and group for last
  256. const lastBinding = bindings[ bindings.length - 1 ];
  257. const index = lastBinding && lastBinding.isUniformsGroup ? bindings.length - 1 : bindings.length;
  258. if ( shaderStage === 'fragment' ) {
  259. bindings.splice( index, 0, sampler, texture );
  260. uniformGPU = [ sampler, texture ];
  261. } else {
  262. bindings.splice( index, 0, texture );
  263. uniformGPU = [ texture ];
  264. }
  265. } else if ( type === 'buffer' ) {
  266. const buffer = new WebGPUUniformBuffer( 'NodeBuffer', node.value );
  267. // add first textures in sequence and group for last
  268. const lastBinding = bindings[ bindings.length - 1 ];
  269. const index = lastBinding && lastBinding.isUniformsGroup ? bindings.length - 1 : bindings.length;
  270. bindings.splice( index, 0, buffer );
  271. uniformGPU = buffer;
  272. } else {
  273. let uniformsGroup = this.uniformsGroup[ shaderStage ];
  274. if ( uniformsGroup === undefined ) {
  275. uniformsGroup = new WebGPUNodeUniformsGroup( shaderStage );
  276. this.uniformsGroup[ shaderStage ] = uniformsGroup;
  277. bindings.push( uniformsGroup );
  278. }
  279. if ( node.isArrayUniformNode === true ) {
  280. uniformGPU = [];
  281. for ( const uniformNode of node.nodes ) {
  282. const uniformNodeGPU = this._getNodeUniform( uniformNode, type );
  283. // fit bounds to buffer
  284. uniformNodeGPU.boundary = getVectorLength( uniformNodeGPU.itemSize );
  285. uniformNodeGPU.itemSize = getStrideLength( uniformNodeGPU.itemSize );
  286. uniformsGroup.addUniform( uniformNodeGPU );
  287. uniformGPU.push( uniformNodeGPU );
  288. }
  289. } else {
  290. uniformGPU = this._getNodeUniform( uniformNode, type );
  291. uniformsGroup.addUniform( uniformGPU );
  292. }
  293. }
  294. nodeData.uniformGPU = uniformGPU;
  295. if ( shaderStage === 'vertex' ) {
  296. this.bindingsOffset[ 'fragment' ] = bindings.length;
  297. }
  298. }
  299. return uniformNode;
  300. }
  301. getAttributes( shaderStage ) {
  302. let snippet = '';
  303. if ( shaderStage === 'vertex' ) {
  304. const attributes = this.attributes;
  305. const length = attributes.length;
  306. snippet += '\n';
  307. for ( let index = 0; index < length; index ++ ) {
  308. const attribute = attributes[ index ];
  309. const name = attribute.name;
  310. const type = this.getType( attribute.type );
  311. snippet += `\t@location( ${index} ) ${ name } : ${ type }`;
  312. if ( index + 1 < length ) {
  313. snippet += ',\n';
  314. }
  315. }
  316. snippet += '\n';
  317. }
  318. return snippet;
  319. }
  320. getVars( shaderStage ) {
  321. let snippet = '';
  322. const vars = this.vars[ shaderStage ];
  323. for ( let index = 0; index < vars.length; index ++ ) {
  324. const variable = vars[ index ];
  325. const name = variable.name;
  326. const type = this.getType( variable.type );
  327. snippet += `var ${name} : ${type}; `;
  328. }
  329. return snippet;
  330. }
  331. getVarys( shaderStage ) {
  332. let snippet = '';
  333. if ( shaderStage === 'vertex' ) {
  334. snippet += '\t@builtin( position ) Vertex: vec4<f32>;\n';
  335. const varys = this.varys;
  336. for ( let index = 0; index < varys.length; index ++ ) {
  337. const vary = varys[ index ];
  338. snippet += `\t@location( ${index} ) ${ vary.name } : ${ this.getType( vary.type ) };\n`;
  339. }
  340. snippet = this._getWGSLStruct( 'NodeVarysStruct', snippet );
  341. } else if ( shaderStage === 'fragment' ) {
  342. const varys = this.varys;
  343. snippet += '\n';
  344. for ( let index = 0; index < varys.length; index ++ ) {
  345. const vary = varys[ index ];
  346. snippet += `\t@location( ${index} ) ${ vary.name } : ${ this.getType( vary.type ) }`;
  347. if ( index + 1 < varys.length ) {
  348. snippet += ',\n';
  349. }
  350. }
  351. snippet += '\n';
  352. }
  353. return snippet;
  354. }
  355. getUniforms( shaderStage ) {
  356. const uniforms = this.uniforms[ shaderStage ];
  357. let snippet = '';
  358. let groupSnippet = '';
  359. let index = this.bindingsOffset[ shaderStage ];
  360. for ( const uniform of uniforms ) {
  361. if ( uniform.type === 'texture' ) {
  362. if ( shaderStage === 'fragment' ) {
  363. snippet += `@group( 0 ) @binding( ${index ++} ) var ${uniform.name}_sampler : sampler; `;
  364. }
  365. snippet += `@group( 0 ) @binding( ${index ++} ) var ${uniform.name} : texture_2d<f32>; `;
  366. } else if ( uniform.type === 'buffer' ) {
  367. const bufferNode = uniform.node;
  368. const bufferType = this.getType( bufferNode.bufferType );
  369. const bufferCount = bufferNode.bufferCount;
  370. const bufferSnippet = `\t${uniform.name} : array< ${bufferType}, ${bufferCount} >;\n`;
  371. snippet += this._getWGSLUniforms( 'NodeBuffer', bufferSnippet, index ++ ) + '\n\n';
  372. } else {
  373. const vectorType = this.getType( this.getVectorType( uniform.type ) );
  374. if ( Array.isArray( uniform.value ) === true ) {
  375. const length = uniform.value.length;
  376. groupSnippet += `uniform ${vectorType}[ ${length} ] ${uniform.name}; `;
  377. } else {
  378. groupSnippet += `\t${uniform.name} : ${ vectorType};\n`;
  379. }
  380. }
  381. }
  382. if ( groupSnippet ) {
  383. snippet += this._getWGSLUniforms( 'NodeUniforms', groupSnippet, index ++ );
  384. }
  385. return snippet;
  386. }
  387. buildCode() {
  388. const shadersData = { fragment: {}, vertex: {} };
  389. for ( const shaderStage in shadersData ) {
  390. let flow = '// code\n';
  391. flow += `\t${ this.flowCode[ shaderStage ] }`;
  392. flow += '\n';
  393. const flowNodes = this.flowNodes[ shaderStage ];
  394. const mainNode = flowNodes[ flowNodes.length - 1 ];
  395. for ( const node of flowNodes ) {
  396. const flowSlotData = this.getFlowData( shaderStage, node );
  397. const slotName = node.name;
  398. if ( slotName ) {
  399. if ( flow.length > 0 ) flow += '\n';
  400. flow += `\t// FLOW -> ${ slotName }\n\t`;
  401. }
  402. flow += `${ flowSlotData.code }\n\t`;
  403. if ( node === mainNode ) {
  404. flow += '// FLOW RESULT\n\t';
  405. if ( shaderStage === 'vertex' ) {
  406. flow += 'NodeVarys.Vertex = ';
  407. } else if ( shaderStage === 'fragment' ) {
  408. flow += 'return ';
  409. }
  410. flow += `${ flowSlotData.result };`;
  411. }
  412. }
  413. const stageData = shadersData[ shaderStage ];
  414. stageData.uniforms = this.getUniforms( shaderStage );
  415. stageData.attributes = this.getAttributes( shaderStage );
  416. stageData.varys = this.getVarys( shaderStage );
  417. stageData.vars = this.getVars( shaderStage );
  418. stageData.codes = this.getCodes( shaderStage );
  419. stageData.flow = flow;
  420. }
  421. this.vertexShader = this._getWGSLVertexCode( shadersData.vertex );
  422. this.fragmentShader = this._getWGSLFragmentCode( shadersData.fragment );
  423. }
  424. getMethod( method ) {
  425. if ( wgslPolyfill[ method ] !== undefined ) {
  426. this._include( method );
  427. }
  428. return wgslMethods[ method ] || method;
  429. }
  430. getType( type ) {
  431. return wgslTypeLib[ type ] || type;
  432. }
  433. _include( name ) {
  434. wgslPolyfill[ name ].build( this );
  435. }
  436. _getNodeUniform( uniformNode, type ) {
  437. if ( type === 'float' ) return new FloatNodeUniform( uniformNode );
  438. if ( type === 'vec2' ) return new Vector2NodeUniform( uniformNode );
  439. if ( type === 'vec3' ) return new Vector3NodeUniform( uniformNode );
  440. if ( type === 'vec4' ) return new Vector4NodeUniform( uniformNode );
  441. if ( type === 'color' ) return new ColorNodeUniform( uniformNode );
  442. if ( type === 'mat3' ) return new Matrix3NodeUniform( uniformNode );
  443. if ( type === 'mat4' ) return new Matrix4NodeUniform( uniformNode );
  444. throw new Error( `Uniform "${type}" not declared.` );
  445. }
  446. _getWGSLVertexCode( shaderData ) {
  447. return `${ this.getSignature() }
  448. // uniforms
  449. ${shaderData.uniforms}
  450. // varys
  451. ${shaderData.varys}
  452. // codes
  453. ${shaderData.codes}
  454. @stage( vertex )
  455. fn main( ${shaderData.attributes} ) -> NodeVarysStruct {
  456. // system
  457. var NodeVarys: NodeVarysStruct;
  458. // vars
  459. ${shaderData.vars}
  460. // flow
  461. ${shaderData.flow}
  462. return NodeVarys;
  463. }
  464. `;
  465. }
  466. _getWGSLFragmentCode( shaderData ) {
  467. return `${ this.getSignature() }
  468. // uniforms
  469. ${shaderData.uniforms}
  470. // codes
  471. ${shaderData.codes}
  472. @stage( fragment )
  473. fn main( ${shaderData.varys} ) -> @location( 0 ) vec4<f32> {
  474. // vars
  475. ${shaderData.vars}
  476. // flow
  477. ${shaderData.flow}
  478. }
  479. `;
  480. }
  481. _getWGSLStruct( name, vars ) {
  482. return `
  483. struct ${name} {
  484. \n${vars}
  485. };`;
  486. }
  487. _getWGSLUniforms( name, vars, binding = 0, group = 0 ) {
  488. const structName = name + 'Struct';
  489. const structSnippet = this._getWGSLStruct( structName, vars );
  490. return `${structSnippet}
  491. @binding( ${binding} ) @group( ${group} )
  492. var<uniform> ${name} : ${structName};`;
  493. }
  494. }
  495. export default WebGPUNodeBuilder;