WebGPUNodeBuilder.js 10 KB

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