WebGPUNodeBuilder.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import WebGPUNodeUniformsGroup from './WebGPUNodeUniformsGroup.js';
  2. import { FloatNodeUniform, Vector2NodeUniform, Vector3NodeUniform, Vector4NodeUniform, ColorNodeUniform } from './WebGPUNodeUniform.js';
  3. import WebGPUSampler from '../WebGPUSampler.js';
  4. import { WebGPUSampledTexture } from '../WebGPUSampledTexture.js';
  5. import NodeSlot from '../../nodes/core/NodeSlot.js';
  6. import NodeBuilder from '../../nodes/core/NodeBuilder.js';
  7. import ShaderLib from './ShaderLib.js';
  8. class WebGPUNodeBuilder extends NodeBuilder {
  9. constructor( material, renderer ) {
  10. super( material, renderer );
  11. this.bindingIndex = 2;
  12. this.bindings = { vertex: [], fragment: [] };
  13. this.attributeIndex = 1;
  14. this.varyIndex = 0;
  15. this.uniformsGroup = {};
  16. this.nativeShader = null;
  17. this._parseMaterial();
  18. }
  19. _parseMaterial() {
  20. const material = this.material;
  21. // get shader
  22. if ( material.isMeshBasicMaterial ) {
  23. this.nativeShader = ShaderLib.meshBasic;
  24. } else if ( material.isPointsMaterial ) {
  25. this.nativeShader = ShaderLib.pointsBasic;
  26. } else if ( material.isLineBasicMaterial ) {
  27. this.nativeShader = ShaderLib.lineBasic;
  28. } else {
  29. console.error( 'THREE.WebGPURenderer: Unknwon shader type.' );
  30. }
  31. // parse inputs
  32. if ( material.isMeshBasicMaterial || material.isPointsMaterial || material.isLineBasicMaterial ) {
  33. if ( material.colorNode !== undefined ) {
  34. this.addSlot( 'fragment', new NodeSlot( material.colorNode, 'COLOR', 'vec4' ) );
  35. }
  36. if ( material.opacityNode !== undefined ) {
  37. this.addSlot( 'fragment', new NodeSlot( material.opacityNode, 'OPACITY', 'float' ) );
  38. }
  39. }
  40. }
  41. getTexture( textureProperty, uvSnippet ) {
  42. return `texture( sampler2D( ${textureProperty}, ${textureProperty}_sampler ), ${uvSnippet} )`;
  43. }
  44. getPropertyName( nodeUniform ) {
  45. if ( nodeUniform.type === 'texture' ) {
  46. return nodeUniform.name;
  47. } else {
  48. return `nodeUniforms.${nodeUniform.name}`;
  49. }
  50. }
  51. getBindings( shaderStage ) {
  52. return this.bindings[ shaderStage ];
  53. }
  54. getUniformFromNode( node, shaderStage, type ) {
  55. const uniformNode = super.getUniformFromNode( node, shaderStage, type );
  56. const nodeData = this.getDataFromNode( node, shaderStage );
  57. if ( nodeData.uniformGPU === undefined ) {
  58. let uniformGPU;
  59. const bindings = this.bindings[ shaderStage ];
  60. if ( type === 'texture' ) {
  61. const sampler = new WebGPUSampler( `${uniformNode.name}_sampler`, uniformNode.value );
  62. const texture = new WebGPUSampledTexture( uniformNode.name, uniformNode.value );
  63. // add first textures in sequence and group for last
  64. const lastBinding = bindings[ bindings.length - 1 ];
  65. const index = lastBinding && lastBinding.isUniformsGroup ? bindings.length - 1 : bindings.length;
  66. bindings.splice( index, 0, sampler, texture );
  67. uniformGPU = { sampler, texture };
  68. } else {
  69. let uniformsGroup = this.uniformsGroup[ shaderStage ];
  70. if ( uniformsGroup === undefined ) {
  71. uniformsGroup = new WebGPUNodeUniformsGroup( shaderStage );
  72. this.uniformsGroup[ shaderStage ] = uniformsGroup;
  73. bindings.push( uniformsGroup );
  74. }
  75. if ( type === 'float' ) {
  76. uniformGPU = new FloatNodeUniform( uniformNode );
  77. } else if ( type === 'vec2' ) {
  78. uniformGPU = new Vector2NodeUniform( uniformNode );
  79. } else if ( type === 'vec3' ) {
  80. uniformGPU = new Vector3NodeUniform( uniformNode );
  81. } else if ( type === 'vec4' ) {
  82. uniformGPU = new Vector4NodeUniform( uniformNode );
  83. } else if ( type === 'color' ) {
  84. uniformGPU = new ColorNodeUniform( uniformNode );
  85. } else {
  86. throw new Error( `Uniform "${type}" not declared.` );
  87. }
  88. uniformsGroup.addUniform( uniformGPU );
  89. }
  90. nodeData.uniformGPU = uniformGPU;
  91. }
  92. return uniformNode;
  93. }
  94. getAttributesHeaderSnippet( shaderStage ) {
  95. let snippet = '';
  96. const attributes = this.attributes;
  97. let attributeIndex = this.attributeIndex;
  98. let varyIndex = this.varyIndex;
  99. for ( let name in attributes ) {
  100. let attribute = attributes[ name ];
  101. let type = attribute.type;
  102. let property = attribute.property;
  103. if ( shaderStage === 'vertex' ) {
  104. snippet += `layout(location = ${attributeIndex ++}) in ${type} ${name};`;
  105. snippet += `layout(location = ${varyIndex ++}) out ${type} ${property};`;
  106. } else if ( shaderStage === 'fragment' ) {
  107. snippet += `layout(location = ${varyIndex ++}) in ${type} ${property};`;
  108. }
  109. }
  110. return snippet;
  111. }
  112. getAttributesBodySnippet( shaderStage ) {
  113. let snippet = '';
  114. const attributes = this.attributes;
  115. for ( let name in attributes ) {
  116. let attribute = attributes[ name ];
  117. let property = attribute.property;
  118. snippet += `${property} = ${name};`;
  119. }
  120. return snippet;
  121. }
  122. getUniformsHeaderSnippet( shaderStage ) {
  123. const uniforms = this.uniforms[ shaderStage ];
  124. let snippet = '';
  125. let groupSnippet = '';
  126. let bindingIndex = this.bindingIndex;
  127. for ( let uniform of uniforms ) {
  128. if ( uniform.type === 'texture' ) {
  129. snippet += `layout(set = 0, binding = ${bindingIndex ++}) uniform sampler ${uniform.name}_sampler;`;
  130. snippet += `layout(set = 0, binding = ${bindingIndex ++}) uniform texture2D ${uniform.name};`;
  131. } else {
  132. let vectorType = this.getVectorType( uniform.type );
  133. groupSnippet += `uniform ${vectorType} ${uniform.name};`;
  134. }
  135. }
  136. if ( groupSnippet ) {
  137. snippet += `layout(set = 0, binding = ${bindingIndex ++}) uniform NodeUniforms { ${groupSnippet} } nodeUniforms;`;
  138. }
  139. return snippet;
  140. }
  141. composeShaderCode( code, snippet ) {
  142. // use regex maybe for security?
  143. const versionStrIndex = code.indexOf( "\n" );
  144. let finalCode = code.substr( 0, versionStrIndex ) + "\n\n";
  145. finalCode += snippet;
  146. finalCode += code.substr( versionStrIndex );
  147. return finalCode;
  148. }
  149. build() {
  150. super.build();
  151. this.vertexShader = this.composeShaderCode( this.nativeShader.vertexShader, this.vertexShader );
  152. this.fragmentShader = this.composeShaderCode( this.nativeShader.fragmentShader, this.fragmentShader );
  153. return this;
  154. }
  155. }
  156. export default WebGPUNodeBuilder;