WebGPUNodeBuilder.js 17 KB

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