WebGPUNodeBuilder.js 5.3 KB

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