WebGPUNodeBuilder.js 17 KB

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