WebGPUNodeBuilder.js 11 KB

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