WebGPUNodeBuilder.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 { vec4 } 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. 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 = colorNode;
  155. if ( lightNode && lightNode.isNode ) {
  156. const lightContextNode = new LightContextNode( lightNode );
  157. outputNode = this.addFlow( 'fragment', new VarNode( lightContextNode, 'Light', 'vec3' ) );
  158. }
  159. // RESULT
  160. const outputEncoding = this.renderer.outputEncoding;
  161. if ( outputEncoding !== LinearEncoding ) {
  162. outputNode = new ColorSpaceNode( ColorSpaceNode.LINEAR_TO_LINEAR, vec4( outputNode ) );
  163. outputNode.fromEncoding( outputEncoding );
  164. }
  165. this.addFlow( 'fragment', new VarNode( outputNode, 'Output', 'vec4' ) );
  166. }
  167. }
  168. addFlowCode( code ) {
  169. if ( ! /;\s*$/.test( code ) ) {
  170. code += ';';
  171. }
  172. super.addFlowCode( code + '\n\t' );
  173. }
  174. getTexture( textureProperty, uvSnippet, biasSnippet, shaderStage = this.shaderStage ) {
  175. if ( shaderStage === 'fragment' ) {
  176. return `textureSample( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet} )`;
  177. } else {
  178. this._include( 'repeatWrapping' );
  179. const dimension = `textureDimensions( ${textureProperty}, 0 )`;
  180. return `textureLoad( ${textureProperty}, repeatWrapping( ${uvSnippet}, ${dimension} ), 0 )`;
  181. }
  182. }
  183. getPropertyName( node, shaderStage = this.shaderStage ) {
  184. if ( node.isNodeVary === true ) {
  185. if ( shaderStage === 'vertex' ) {
  186. return `NodeVarys.${ node.name }`;
  187. }
  188. } else if ( node.isNodeUniform === true ) {
  189. const name = node.name;
  190. const type = node.type;
  191. if ( type === 'texture' ) {
  192. return name;
  193. } else if ( type === 'buffer' ) {
  194. return `NodeBuffer.${name}`;
  195. } else {
  196. return `NodeUniforms.${name}`;
  197. }
  198. }
  199. return super.getPropertyName( node );
  200. }
  201. getBindings() {
  202. const bindings = this.bindings;
  203. return [ ...bindings.vertex, ...bindings.fragment ];
  204. }
  205. getUniformFromNode( node, shaderStage, type ) {
  206. const uniformNode = super.getUniformFromNode( node, shaderStage, type );
  207. const nodeData = this.getDataFromNode( node, shaderStage );
  208. if ( nodeData.uniformGPU === undefined ) {
  209. let uniformGPU;
  210. const bindings = this.bindings[ shaderStage ];
  211. if ( type === 'texture' ) {
  212. const sampler = new WebGPUNodeSampler( `${uniformNode.name}_sampler`, uniformNode.node );
  213. const texture = new WebGPUNodeSampledTexture( uniformNode.name, uniformNode.node );
  214. // add first textures in sequence and group for last
  215. const lastBinding = bindings[ bindings.length - 1 ];
  216. const index = lastBinding && lastBinding.isUniformsGroup ? bindings.length - 1 : bindings.length;
  217. if ( shaderStage === 'fragment' ) {
  218. bindings.splice( index, 0, sampler, texture );
  219. uniformGPU = [ sampler, texture ];
  220. } else {
  221. bindings.splice( index, 0, texture );
  222. uniformGPU = [ texture ];
  223. }
  224. } else if ( type === 'buffer' ) {
  225. const buffer = new WebGPUUniformBuffer( 'NodeBuffer', node.value );
  226. // add first textures in sequence and group for last
  227. const lastBinding = bindings[ bindings.length - 1 ];
  228. const index = lastBinding && lastBinding.isUniformsGroup ? bindings.length - 1 : bindings.length;
  229. bindings.splice( index, 0, buffer );
  230. uniformGPU = buffer;
  231. } else {
  232. let uniformsGroup = this.uniformsGroup[ shaderStage ];
  233. if ( uniformsGroup === undefined ) {
  234. uniformsGroup = new WebGPUNodeUniformsGroup( shaderStage );
  235. this.uniformsGroup[ shaderStage ] = uniformsGroup;
  236. bindings.push( uniformsGroup );
  237. }
  238. if ( node.isArrayInputNode === true ) {
  239. uniformGPU = [];
  240. for ( const inputNode of node.nodes ) {
  241. const uniformNodeGPU = this._getNodeUniform( inputNode, type );
  242. // fit bounds to buffer
  243. uniformNodeGPU.boundary = getVectorLength( uniformNodeGPU.itemSize );
  244. uniformNodeGPU.itemSize = getStrideLength( uniformNodeGPU.itemSize );
  245. uniformsGroup.addUniform( uniformNodeGPU );
  246. uniformGPU.push( uniformNodeGPU );
  247. }
  248. } else {
  249. uniformGPU = this._getNodeUniform( uniformNode, type );
  250. uniformsGroup.addUniform( uniformGPU );
  251. }
  252. }
  253. nodeData.uniformGPU = uniformGPU;
  254. if ( shaderStage === 'vertex' ) {
  255. this.bindingsOffset[ 'fragment' ] = bindings.length;
  256. }
  257. }
  258. return uniformNode;
  259. }
  260. getAttributes( shaderStage ) {
  261. let snippet = '';
  262. if ( shaderStage === 'vertex' ) {
  263. const attributes = this.attributes;
  264. const length = attributes.length;
  265. snippet += '\n';
  266. for ( let index = 0; index < length; index ++ ) {
  267. const attribute = attributes[ index ];
  268. const name = attribute.name;
  269. const type = this.getType( attribute.type );
  270. snippet += `\t[[ location( ${index} ) ]] ${ name } : ${ type }`;
  271. if ( index + 1 < length ) {
  272. snippet += ',\n';
  273. }
  274. }
  275. snippet += '\n';
  276. }
  277. return snippet;
  278. }
  279. getVars( shaderStage ) {
  280. let snippet = '';
  281. const vars = this.vars[ shaderStage ];
  282. for ( let index = 0; index < vars.length; index ++ ) {
  283. const variable = vars[ index ];
  284. const name = variable.name;
  285. const type = this.getType( variable.type );
  286. snippet += `var ${name} : ${type}; `;
  287. }
  288. return snippet;
  289. }
  290. getVarys( shaderStage ) {
  291. let snippet = '';
  292. if ( shaderStage === 'vertex' ) {
  293. snippet += '\t[[ builtin( position ) ]] Vertex: vec4<f32>;\n';
  294. const varys = this.varys;
  295. for ( let index = 0; index < varys.length; index ++ ) {
  296. const vary = varys[ index ];
  297. snippet += `\t[[ location( ${index} ) ]] ${ vary.name } : ${ this.getType( vary.type ) };\n`;
  298. }
  299. snippet = this._getWGSLStruct( 'NodeVarysStruct', snippet );
  300. } else if ( shaderStage === 'fragment' ) {
  301. const varys = this.varys;
  302. snippet += '\n';
  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 ) }`;
  306. if ( index + 1 < varys.length ) {
  307. snippet += ',\n';
  308. }
  309. }
  310. snippet += '\n';
  311. }
  312. return snippet;
  313. }
  314. getUniforms( shaderStage ) {
  315. const uniforms = this.uniforms[ shaderStage ];
  316. let snippet = '';
  317. let groupSnippet = '';
  318. let index = this.bindingsOffset[ shaderStage ];
  319. for ( const uniform of uniforms ) {
  320. if ( uniform.type === 'texture' ) {
  321. if ( shaderStage === 'fragment' ) {
  322. snippet += `[[ group( 0 ), binding( ${index ++} ) ]] var ${uniform.name}_sampler : sampler; `;
  323. }
  324. snippet += `[[ group( 0 ), binding( ${index ++} ) ]] var ${uniform.name} : texture_2d<f32>; `;
  325. } else if ( uniform.type === 'buffer' ) {
  326. const bufferNode = uniform.node;
  327. const bufferType = this.getType( bufferNode.bufferType );
  328. const bufferCount = bufferNode.bufferCount;
  329. const bufferSnippet = `\t${uniform.name} : array< ${bufferType}, ${bufferCount} >;\n`;
  330. snippet += this._getWGSLUniforms( 'NodeBuffer', bufferSnippet, index ++ ) + '\n\n';
  331. } else {
  332. const vectorType = this.getType( this.getVectorType( uniform.type ) );
  333. if ( Array.isArray( uniform.value ) === true ) {
  334. const length = uniform.value.length;
  335. groupSnippet += `uniform ${vectorType}[ ${length} ] ${uniform.name}; `;
  336. } else {
  337. groupSnippet += `\t${uniform.name} : ${ vectorType};\n`;
  338. }
  339. }
  340. }
  341. if ( groupSnippet ) {
  342. snippet += this._getWGSLUniforms( 'NodeUniforms', groupSnippet, index ++ );
  343. }
  344. return snippet;
  345. }
  346. buildCode() {
  347. const shadersData = { fragment: {}, vertex: {} };
  348. for ( const shaderStage in shadersData ) {
  349. let flow = '// code\n';
  350. flow += `\t${ this.flowCode[ shaderStage ] }`;
  351. flow += '\n';
  352. const flowNodes = this.flowNodes[ shaderStage ];
  353. const mainNode = flowNodes[ flowNodes.length - 1 ];
  354. for ( const node of flowNodes ) {
  355. const flowSlotData = this.getFlowData( shaderStage, node );
  356. const slotName = node.name;
  357. if ( slotName ) {
  358. if ( flow.length > 0 ) flow += '\n';
  359. flow += `\t// FLOW -> ${ slotName }\n\t`;
  360. }
  361. flow += `${ flowSlotData.code }\n\t`;
  362. if ( node === mainNode ) {
  363. flow += '// FLOW RESULT\n\t';
  364. if ( shaderStage === 'vertex' ) {
  365. flow += 'NodeVarys.Vertex = ';
  366. } else if ( shaderStage === 'fragment' ) {
  367. flow += 'return ';
  368. }
  369. flow += `${ flowSlotData.result };`;
  370. }
  371. }
  372. const stageData = shadersData[ shaderStage ];
  373. stageData.uniforms = this.getUniforms( shaderStage );
  374. stageData.attributes = this.getAttributes( shaderStage );
  375. stageData.varys = this.getVarys( shaderStage );
  376. stageData.vars = this.getVars( shaderStage );
  377. stageData.codes = this.getCodes( shaderStage );
  378. stageData.flow = flow;
  379. }
  380. this.vertexShader = this._getWGSLVertexCode( shadersData.vertex );
  381. this.fragmentShader = this._getWGSLFragmentCode( shadersData.fragment );
  382. }
  383. getMethod( method ) {
  384. if ( wgslPolyfill[ method ] !== undefined ) {
  385. this._include( method );
  386. }
  387. return wgslMethods[ method ] || method;
  388. }
  389. getType( type ) {
  390. return wgslTypeLib[ type ] || type;
  391. }
  392. _include( name ) {
  393. wgslPolyfill[ name ].build( this );
  394. }
  395. _getNodeUniform( uniformNode, type ) {
  396. if ( type === 'float' ) return new FloatNodeUniform( uniformNode );
  397. if ( type === 'vec2' ) return new Vector2NodeUniform( uniformNode );
  398. if ( type === 'vec3' ) return new Vector3NodeUniform( uniformNode );
  399. if ( type === 'vec4' ) return new Vector4NodeUniform( uniformNode );
  400. if ( type === 'color' ) return new ColorNodeUniform( uniformNode );
  401. if ( type === 'mat3' ) return new Matrix3NodeUniform( uniformNode );
  402. if ( type === 'mat4' ) return new Matrix4NodeUniform( uniformNode );
  403. throw new Error( `Uniform "${type}" not declared.` );
  404. }
  405. _getWGSLVertexCode( shaderData ) {
  406. return `${ this.getSignature() }
  407. // uniforms
  408. ${shaderData.uniforms}
  409. // varys
  410. ${shaderData.varys}
  411. // codes
  412. ${shaderData.codes}
  413. [[ stage( vertex ) ]]
  414. fn main( ${shaderData.attributes} ) -> NodeVarysStruct {
  415. // system
  416. var NodeVarys: NodeVarysStruct;
  417. // vars
  418. ${shaderData.vars}
  419. // flow
  420. ${shaderData.flow}
  421. return NodeVarys;
  422. }
  423. `;
  424. }
  425. _getWGSLFragmentCode( shaderData ) {
  426. return `${ this.getSignature() }
  427. // uniforms
  428. ${shaderData.uniforms}
  429. // codes
  430. ${shaderData.codes}
  431. [[ stage( fragment ) ]]
  432. fn main( ${shaderData.varys} ) -> [[ location( 0 ) ]] vec4<f32> {
  433. // vars
  434. ${shaderData.vars}
  435. // flow
  436. ${shaderData.flow}
  437. }
  438. `;
  439. }
  440. _getWGSLStruct( name, vars ) {
  441. return `
  442. struct ${name} {
  443. \n${vars}
  444. };`;
  445. }
  446. _getWGSLUniforms( name, vars, binding = 0, group = 0 ) {
  447. const structName = name + 'Struct';
  448. const structSnippet = this._getWGSLStruct( structName, vars );
  449. return `${structSnippet}
  450. [[ binding( ${binding} ), group( ${group} ) ]]
  451. var<uniform> ${name} : ${structName};`;
  452. }
  453. }
  454. export default WebGPUNodeBuilder;