2
0

WGSLNodeBuilder.js 19 KB

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