WebGPUNodeBuilder.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. import WebGPUNodeUniformsGroup from './WebGPUNodeUniformsGroup.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 } from './WebGPUNodeSampledTexture.js';
  8. import NodeSlot from '../../nodes/core/NodeSlot.js';
  9. import VarNode from '../../nodes/core/VarNode.js';
  10. import NodeBuilder from '../../nodes/core/NodeBuilder.js';
  11. import MaterialNode from '../../nodes/accessors/MaterialNode.js';
  12. import NormalNode from '../../nodes/accessors/NormalNode.js';
  13. import ModelViewProjectionNode from '../../nodes/accessors/ModelViewProjectionNode.js';
  14. import LightContextNode from '../../nodes/lights/LightContextNode.js';
  15. import ShaderLib from './ShaderLib.js';
  16. class WebGPUNodeBuilder extends NodeBuilder {
  17. constructor( material, renderer, lightNode = null ) {
  18. super( material, renderer );
  19. this.lightNode = lightNode;
  20. this.bindings = { vertex: [], fragment: [] };
  21. this.bindingsOffset = { vertex: 0, fragment: 0 };
  22. this.uniformsGroup = {};
  23. this.nativeShader = null;
  24. this._parseMaterial();
  25. }
  26. _parseMaterial() {
  27. const material = this.material;
  28. // get shader
  29. let shader = null;
  30. if ( material.isMeshStandardMaterial ) {
  31. shader = ShaderLib.standard;
  32. } else if ( material.isMeshPhongMaterial ) {
  33. shader = ShaderLib.phong;
  34. } else {
  35. shader = ShaderLib.common;
  36. }
  37. this.nativeShader = shader;
  38. // parse inputs
  39. if ( material.isMeshStandardMaterial || material.isMeshPhongMaterial || material.isMeshBasicMaterial || material.isPointsMaterial || material.isLineBasicMaterial ) {
  40. const mvpNode = new ModelViewProjectionNode();
  41. let lightNode = material.lightNode;
  42. if ( lightNode === undefined && this.lightNode && this.lightNode.hasLights === true ) {
  43. lightNode = this.lightNode;
  44. }
  45. if ( material.positionNode !== undefined ) {
  46. mvpNode.position = material.positionNode;
  47. }
  48. this.addSlot( 'vertex', new NodeSlot( mvpNode, 'MVP', 'vec4' ) );
  49. if ( material.alphaTestNode !== undefined ) {
  50. this.addSlot( 'fragment', new NodeSlot( material.alphaTestNode, 'ALPHA_TEST', 'float' ) );
  51. } else {
  52. this.addSlot( 'fragment', new NodeSlot( new MaterialNode( MaterialNode.ALPHA_TEST ), 'ALPHA_TEST', 'float' ) );
  53. }
  54. if ( material.colorNode !== undefined ) {
  55. this.addSlot( 'fragment', new NodeSlot( material.colorNode, 'COLOR', 'vec4' ) );
  56. } else {
  57. this.addSlot( 'fragment', new NodeSlot( new MaterialNode( MaterialNode.COLOR ), 'COLOR', 'vec4' ) );
  58. }
  59. if ( material.opacityNode !== undefined ) {
  60. this.addSlot( 'fragment', new NodeSlot( material.opacityNode, 'OPACITY', 'float' ) );
  61. } else {
  62. this.addSlot( 'fragment', new NodeSlot( new MaterialNode( MaterialNode.OPACITY ), 'OPACITY', 'float' ) );
  63. }
  64. if ( material.isMeshStandardMaterial ) {
  65. if ( material.metalnessNode !== undefined ) {
  66. this.addSlot( 'fragment', new NodeSlot( material.metalnessNode, 'METALNESS', 'float' ) );
  67. } else {
  68. this.addSlot( 'fragment', new NodeSlot( new MaterialNode( MaterialNode.METALNESS ), 'METALNESS', 'float' ) );
  69. }
  70. if ( material.roughnessNode !== undefined ) {
  71. this.addSlot( 'fragment', new NodeSlot( material.roughnessNode, 'ROUGHNESS', 'float' ) );
  72. } else {
  73. this.addSlot( 'fragment', new NodeSlot( new MaterialNode( MaterialNode.ROUGHNESS ), 'ROUGHNESS', 'float' ) );
  74. }
  75. let normalNode = null;
  76. if ( material.normalNode !== undefined ) {
  77. normalNode = material.normalNode;
  78. } else {
  79. normalNode = new NormalNode( NormalNode.VIEW );
  80. }
  81. this.addSlot( 'fragment', new NodeSlot( new VarNode( normalNode, 'TransformedNormalView', 'vec3' ), 'NORMAL', 'vec3' ) );
  82. } else if ( material.isMeshPhongMaterial ) {
  83. if ( material.specularNode !== undefined ) {
  84. this.addSlot( 'fragment', new NodeSlot( material.specularNode, 'SPECULAR', 'vec3' ) );
  85. } else {
  86. this.addSlot( 'fragment', new NodeSlot( new MaterialNode( MaterialNode.SPECULAR ), 'SPECULAR', 'vec3' ) );
  87. }
  88. if ( material.shininessNode !== undefined ) {
  89. this.addSlot( 'fragment', new NodeSlot( material.shininessNode, 'SHININESS', 'float' ) );
  90. } else {
  91. this.addSlot( 'fragment', new NodeSlot( new MaterialNode( MaterialNode.SHININESS ), 'SHININESS', 'float' ) );
  92. }
  93. }
  94. if ( lightNode && lightNode.isNode ) {
  95. const lightContextNode = new LightContextNode( lightNode );
  96. this.addSlot( 'fragment', new NodeSlot( lightContextNode, 'LIGHT', 'vec3' ) );
  97. }
  98. }
  99. }
  100. getTexture( textureProperty, uvSnippet, biasSnippet = null ) {
  101. if ( biasSnippet !== null ) {
  102. return `texture( sampler2D( ${textureProperty}, ${textureProperty}_sampler ), ${uvSnippet}, ${biasSnippet} )`;
  103. } else {
  104. return `texture( sampler2D( ${textureProperty}, ${textureProperty}_sampler ), ${uvSnippet} )`;
  105. }
  106. }
  107. getPropertyName( node ) {
  108. if ( node.isNodeUniform === true ) {
  109. const name = node.name;
  110. const type = node.type;
  111. if ( type === 'texture' ) {
  112. return name;
  113. } else {
  114. return `nodeUniforms.${name}`;
  115. }
  116. }
  117. return super.getPropertyName( node );
  118. }
  119. getBindings() {
  120. const bindings = this.bindings;
  121. return [ ...bindings.vertex, ...bindings.fragment ];
  122. }
  123. getUniformFromNode( node, shaderStage, type ) {
  124. const uniformNode = super.getUniformFromNode( node, shaderStage, type );
  125. const nodeData = this.getDataFromNode( node, shaderStage );
  126. if ( nodeData.uniformGPU === undefined ) {
  127. let uniformGPU;
  128. const bindings = this.bindings[ shaderStage ];
  129. if ( type === 'texture' ) {
  130. const sampler = new WebGPUNodeSampler( `${uniformNode.name}_sampler`, uniformNode.node );
  131. const texture = new WebGPUNodeSampledTexture( uniformNode.name, uniformNode.node );
  132. // add first textures in sequence and group for last
  133. const lastBinding = bindings[ bindings.length - 1 ];
  134. const index = lastBinding && lastBinding.isUniformsGroup ? bindings.length - 1 : bindings.length;
  135. bindings.splice( index, 0, sampler, texture );
  136. uniformGPU = { sampler, texture };
  137. } else {
  138. let uniformsGroup = this.uniformsGroup[ shaderStage ];
  139. if ( uniformsGroup === undefined ) {
  140. uniformsGroup = new WebGPUNodeUniformsGroup( shaderStage );
  141. this.uniformsGroup[ shaderStage ] = uniformsGroup;
  142. bindings.push( uniformsGroup );
  143. }
  144. if ( type === 'float' ) {
  145. uniformGPU = new FloatNodeUniform( uniformNode );
  146. } else if ( type === 'vec2' ) {
  147. uniformGPU = new Vector2NodeUniform( uniformNode );
  148. } else if ( type === 'vec3' ) {
  149. uniformGPU = new Vector3NodeUniform( uniformNode );
  150. } else if ( type === 'vec4' ) {
  151. uniformGPU = new Vector4NodeUniform( uniformNode );
  152. } else if ( type === 'color' ) {
  153. uniformGPU = new ColorNodeUniform( uniformNode );
  154. } else if ( type === 'mat3' ) {
  155. uniformGPU = new Matrix3NodeUniform( uniformNode );
  156. } else if ( type === 'mat4' ) {
  157. uniformGPU = new Matrix4NodeUniform( uniformNode );
  158. } else {
  159. throw new Error( `Uniform "${type}" not declared.` );
  160. }
  161. uniformsGroup.addUniform( uniformGPU );
  162. }
  163. nodeData.uniformGPU = uniformGPU;
  164. if ( shaderStage === 'vertex' ) {
  165. this.bindingsOffset[ 'fragment' ] = bindings.length;
  166. }
  167. }
  168. return uniformNode;
  169. }
  170. getAttributes( shaderStage ) {
  171. let snippet = '';
  172. if ( shaderStage === 'vertex' ) {
  173. const attributes = this.attributes;
  174. for ( let index = 0; index < attributes.length; index ++ ) {
  175. const attribute = attributes[ index ];
  176. snippet += `layout(location = ${index}) in ${attribute.type} ${attribute.name}; `;
  177. }
  178. }
  179. return snippet;
  180. }
  181. getVarys( shaderStage ) {
  182. let snippet = '';
  183. const varys = this.varys;
  184. const ioStage = shaderStage === 'vertex' ? 'out' : 'in';
  185. for ( let index = 0; index < varys.length; index ++ ) {
  186. const vary = varys[ index ];
  187. snippet += `layout(location = ${index}) ${ioStage} ${vary.type} ${vary.name}; `;
  188. }
  189. return snippet;
  190. }
  191. getUniforms( shaderStage ) {
  192. const uniforms = this.uniforms[ shaderStage ];
  193. let snippet = '';
  194. let groupSnippet = '';
  195. let index = this.bindingsOffset[ shaderStage ];
  196. for ( const uniform of uniforms ) {
  197. if ( uniform.type === 'texture' ) {
  198. snippet += `layout(set = 0, binding = ${index ++}) uniform sampler ${uniform.name}_sampler; `;
  199. snippet += `layout(set = 0, binding = ${index ++}) uniform texture2D ${uniform.name}; `;
  200. } else {
  201. const vectorType = this.getVectorType( uniform.type );
  202. groupSnippet += `uniform ${vectorType} ${uniform.name}; `;
  203. }
  204. }
  205. if ( groupSnippet ) {
  206. snippet += `layout(set = 0, binding = ${index ++}) uniform NodeUniforms { ${groupSnippet} } nodeUniforms; `;
  207. }
  208. return snippet;
  209. }
  210. composeShaderCode( code, snippet ) {
  211. // use regex maybe for security?
  212. const versionStrIndex = code.indexOf( '\n' );
  213. let finalCode = code.substr( 0, versionStrIndex ) + '\n\n';
  214. finalCode += snippet;
  215. finalCode += code.substr( versionStrIndex );
  216. return finalCode;
  217. }
  218. build() {
  219. const keywords = this.getContextValue( 'keywords' );
  220. for ( const shaderStage of [ 'vertex', 'fragment' ] ) {
  221. this.shaderStage = shaderStage;
  222. keywords.include( this, this.nativeShader.fragmentShader );
  223. }
  224. super.build();
  225. this.vertexShader = this.composeShaderCode( this.nativeShader.vertexShader, this.vertexShader );
  226. this.fragmentShader = this.composeShaderCode( this.nativeShader.fragmentShader, this.fragmentShader );
  227. return this;
  228. }
  229. }
  230. export default WebGPUNodeBuilder;