WGSLNodeBuilder.js 19 KB

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