WebGPUNodeBuilder.js 12 KB

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