WGSLNodeBuilder.js 19 KB

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