WebGPUNodeBuilder.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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.isMeshBasicMaterial || material.isPointsMaterial || material.isLineBasicMaterial ) {
  39. const mvpNode = new ModelViewProjectionNode();
  40. let lightNode = material.lightNode;
  41. if ( lightNode === null && this.lightNode && this.lightNode.hasLights === true ) {
  42. lightNode = this.lightNode;
  43. }
  44. if ( material.positionNode && material.positionNode.isNode ) {
  45. mvpNode.position = material.positionNode;
  46. }
  47. this.addSlot( 'vertex', new NodeSlot( mvpNode, 'MVP', 'vec4' ) );
  48. if ( material.alphaTestNode && material.alphaTestNode.isNode ) {
  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 && material.colorNode.isNode ) {
  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 && material.opacityNode.isNode ) {
  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 && material.metalnessNode.isNode ) {
  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 && material.roughnessNode.isNode ) {
  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 && material.normalNode.isNode ) {
  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 && material.specularNode.isNode ) {
  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 && material.shininessNode.isNode ) {
  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. for ( const inputNode of node.value ) {
  146. const uniformNodeGPU = this._getNodeUniform( inputNode, type );
  147. // fit bounds to buffer
  148. uniformNodeGPU.boundary = getVectorLength( uniformNodeGPU.itemSize );
  149. uniformNodeGPU.itemSize = getStrideLength( uniformNodeGPU.itemSize );
  150. uniformsGroup.addUniform( uniformNodeGPU );
  151. uniformGPU.push( uniformNodeGPU );
  152. }
  153. } else {
  154. uniformGPU = this._getNodeUniform( uniformNode, type );
  155. uniformsGroup.addUniform( uniformGPU );
  156. }
  157. }
  158. nodeData.uniformGPU = uniformGPU;
  159. if ( shaderStage === 'vertex' ) {
  160. this.bindingsOffset[ 'fragment' ] = bindings.length;
  161. }
  162. }
  163. return uniformNode;
  164. }
  165. getAttributes( shaderStage ) {
  166. let snippet = '';
  167. if ( shaderStage === 'vertex' ) {
  168. const attributes = this.attributes;
  169. for ( let index = 0; index < attributes.length; index ++ ) {
  170. const attribute = attributes[ index ];
  171. snippet += `layout(location = ${index}) in ${attribute.type} ${attribute.name}; `;
  172. }
  173. }
  174. return snippet;
  175. }
  176. getVarys( shaderStage ) {
  177. let snippet = '';
  178. const varys = this.varys;
  179. const ioStage = shaderStage === 'vertex' ? 'out' : 'in';
  180. for ( let index = 0; index < varys.length; index ++ ) {
  181. const vary = varys[ index ];
  182. snippet += `layout(location = ${index}) ${ioStage} ${vary.type} ${vary.name}; `;
  183. }
  184. return snippet;
  185. }
  186. getUniforms( shaderStage ) {
  187. const uniforms = this.uniforms[ shaderStage ];
  188. let snippet = '';
  189. let groupSnippet = '';
  190. let index = this.bindingsOffset[ shaderStage ];
  191. for ( const uniform of uniforms ) {
  192. if ( uniform.type === 'texture' ) {
  193. snippet += `layout(set = 0, binding = ${index ++}) uniform sampler ${uniform.name}_sampler; `;
  194. snippet += `layout(set = 0, binding = ${index ++}) uniform texture2D ${uniform.name}; `;
  195. } else {
  196. const vectorType = this.getVectorType( uniform.type );
  197. if ( Array.isArray( uniform.value ) === true ) {
  198. const length = uniform.value.length;
  199. groupSnippet += `uniform ${vectorType}[ ${length} ] ${uniform.name}; `;
  200. } else {
  201. groupSnippet += `uniform ${vectorType} ${uniform.name}; `;
  202. }
  203. }
  204. }
  205. if ( groupSnippet ) {
  206. snippet += `layout(set = 0, binding = ${index ++}) uniform NodeUniforms { ${groupSnippet} } nodeUniforms; `;
  207. }
  208. return snippet;
  209. }
  210. build() {
  211. const keywords = this.getContextValue( 'keywords' );
  212. for ( const shaderStage of [ 'vertex', 'fragment' ] ) {
  213. this.shaderStage = shaderStage;
  214. keywords.include( this, this.nativeShader.fragmentShader );
  215. }
  216. super.build();
  217. this.vertexShader = this._composeShaderCode( this.nativeShader.vertexShader, this.vertexShader );
  218. this.fragmentShader = this._composeShaderCode( this.nativeShader.fragmentShader, this.fragmentShader );
  219. return this;
  220. }
  221. _composeShaderCode( code, snippet ) {
  222. // use regex maybe for security?
  223. const versionStrIndex = code.indexOf( '\n' );
  224. let finalCode = code.substr( 0, versionStrIndex ) + '\n\n';
  225. finalCode += snippet;
  226. finalCode += code.substr( versionStrIndex );
  227. return finalCode;
  228. }
  229. _getNodeUniform( uniformNode, type ) {
  230. if ( type === 'float' ) return new FloatNodeUniform( uniformNode );
  231. if ( type === 'vec2' ) return new Vector2NodeUniform( uniformNode );
  232. if ( type === 'vec3' ) return new Vector3NodeUniform( uniformNode );
  233. if ( type === 'vec4' ) return new Vector4NodeUniform( uniformNode );
  234. if ( type === 'color' ) return new ColorNodeUniform( uniformNode );
  235. if ( type === 'mat3' ) return new Matrix3NodeUniform( uniformNode );
  236. if ( type === 'mat4' ) return new Matrix4NodeUniform( uniformNode );
  237. throw new Error( `Uniform "${type}" not declared.` );
  238. }
  239. }
  240. export default WebGPUNodeBuilder;