GLSLNodeBuilder.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. import { MathNode, GLSLNodeParser, NodeBuilder, NodeMaterial } from '../../../nodes/Nodes.js';
  2. import UniformsGroup from '../../common/UniformsGroup.js';
  3. import { NodeSampledTexture, NodeSampledCubeTexture } from '../../common/nodes/NodeSampledTexture.js';
  4. const glslMethods = {
  5. [ MathNode.ATAN2 ]: 'atan',
  6. textureDimensions: 'textureSize'
  7. };
  8. const precisionLib = {
  9. low: 'lowp',
  10. medium: 'mediump',
  11. high: 'highp'
  12. };
  13. class GLSLNodeBuilder extends NodeBuilder {
  14. constructor( object, renderer, scene = null ) {
  15. super( object, renderer, new GLSLNodeParser(), scene );
  16. this.uniformsGroup = {};
  17. }
  18. getMethod( method ) {
  19. return glslMethods[ method ] || method;
  20. }
  21. getPropertyName( node, shaderStage ) {
  22. if ( node.isOutputStructVar ) return '';
  23. return super.getPropertyName( node, shaderStage );
  24. }
  25. getTexture( texture, textureProperty, uvSnippet ) {
  26. if ( texture.isTextureCube ) {
  27. return `textureCube( ${textureProperty}, ${uvSnippet} )`;
  28. } else if ( texture.isDepthTexture ) {
  29. return `texture( ${textureProperty}, ${uvSnippet} ).x`;
  30. } else {
  31. return `texture( ${textureProperty}, ${uvSnippet} )`;
  32. }
  33. }
  34. getTextureLevel( texture, textureProperty, uvSnippet, biasSnippet ) {
  35. return `textureLod( ${textureProperty}, ${uvSnippet}, ${biasSnippet} )`;
  36. }
  37. getVars( shaderStage ) {
  38. const snippets = [];
  39. const vars = this.vars[ shaderStage ];
  40. for ( const variable of vars ) {
  41. if ( variable.isOutputStructVar ) continue;
  42. snippets.push( `${ this.getVar( variable.type, variable.name ) };` );
  43. }
  44. return snippets.join( '\n\t' );
  45. }
  46. getUniforms( shaderStage ) {
  47. const uniforms = this.uniforms[ shaderStage ];
  48. const bindingSnippets = [];
  49. const groupSnippets = [];
  50. for ( const uniform of uniforms ) {
  51. let snippet = null;
  52. let group = false;
  53. if ( uniform.type === 'texture' ) {
  54. snippet = `sampler2D ${uniform.name};`;
  55. } else if ( uniform.type === 'cubeTexture' ) {
  56. snippet = `samplerCube ${uniform.name};`;
  57. } else {
  58. const vectorType = this.getVectorType( uniform.type );
  59. snippet = `${vectorType} ${uniform.name};`;
  60. group = true;
  61. }
  62. const precision = uniform.node.precision;
  63. if ( precision !== null ) {
  64. snippet = precisionLib[ precision ] + ' ' + snippet;
  65. }
  66. if ( group ) {
  67. snippet = '\t' + snippet;
  68. groupSnippets.push( snippet );
  69. } else {
  70. snippet = 'uniform ' + snippet;
  71. bindingSnippets.push( snippet );
  72. }
  73. }
  74. let output = '';
  75. if ( groupSnippets.length > 0 ) {
  76. output += this._getGLSLUniformStruct( shaderStage + 'NodeUniforms', groupSnippets.join( '\n' ) ) + '\n';
  77. }
  78. output += bindingSnippets.join( '\n' );
  79. return output;
  80. }
  81. getAttributes( shaderStage ) {
  82. let snippet = '';
  83. if ( shaderStage === 'vertex' ) {
  84. const attributes = this.attributes;
  85. let location = 0;
  86. for ( const attribute of attributes ) {
  87. snippet += `layout( location = ${ location ++ } ) in ${ attribute.type } ${ attribute.name };\n`;
  88. }
  89. }
  90. return snippet;
  91. }
  92. getStructMembers( struct ) {
  93. const snippets = [];
  94. const members = struct.getMemberTypes();
  95. for ( let i = 0; i < members.length; i ++ ) {
  96. const member = members[ i ];
  97. snippets.push( `layout( location = ${i} ) out ${ member} m${i};` );
  98. }
  99. return snippets.join( '\n' );
  100. }
  101. getStructs( shaderStage ) {
  102. const snippets = [];
  103. const structs = this.structs[ shaderStage ];
  104. if ( structs.length === 0 ) {
  105. return "layout( location = 0 ) out vec4 fragColor;\n";
  106. }
  107. for ( let index = 0, length = structs.length; index < length; index ++ ) {
  108. const struct = structs[ index ];
  109. let snippet = `\n`;
  110. snippet += this.getStructMembers( struct );
  111. snippet += '\n';
  112. snippets.push( snippet );
  113. }
  114. return snippets.join( '\n\n' );
  115. }
  116. getVaryings( shaderStage ) {
  117. let snippet = '';
  118. const varyings = this.varyings;
  119. if ( shaderStage === 'vertex' ) {
  120. for ( const varying of varyings ) {
  121. snippet += `${varying.needsInterpolation ? 'out' : '/*out*/'} ${varying.type} ${varying.name};\n`;
  122. }
  123. } else if ( shaderStage === 'fragment' ) {
  124. for ( const varying of varyings ) {
  125. if ( varying.needsInterpolation ) {
  126. snippet += `in ${varying.type} ${varying.name};\n`;
  127. }
  128. }
  129. }
  130. return snippet;
  131. }
  132. getVertexIndex() {
  133. return 'gl_VertexID';
  134. }
  135. getFrontFacing() {
  136. return 'gl_FrontFacing';
  137. }
  138. getFragCoord() {
  139. return 'gl_FragCoord';
  140. }
  141. isFlipY() {
  142. return true;
  143. }
  144. _getGLSLUniformStruct( name, vars ) {
  145. return `
  146. layout( std140 ) uniform ${name} {
  147. ${vars}
  148. };`;
  149. }
  150. _getGLSLVertexCode( shaderData ) {
  151. return `#version 300 es
  152. ${ this.getSignature() }
  153. // precision
  154. precision highp float;
  155. precision highp int;
  156. // uniforms
  157. ${shaderData.uniforms}
  158. // varyings
  159. ${shaderData.varyings}
  160. // attributes
  161. ${shaderData.attributes}
  162. // codes
  163. ${shaderData.codes}
  164. void main() {
  165. // vars
  166. ${shaderData.vars}
  167. // flow
  168. ${shaderData.flow}
  169. gl_PointSize = 1.0;
  170. }
  171. `;
  172. }
  173. _getGLSLFragmentCode( shaderData ) {
  174. return `#version 300 es
  175. ${ this.getSignature() }
  176. // precision
  177. precision highp float;
  178. precision highp int;
  179. // uniforms
  180. ${shaderData.uniforms}
  181. // varyings
  182. ${shaderData.varyings}
  183. // codes
  184. ${shaderData.codes}
  185. ${shaderData.structs}
  186. void main() {
  187. // vars
  188. ${shaderData.vars}
  189. // flow
  190. ${shaderData.flow}
  191. }
  192. `;
  193. }
  194. buildCode() {
  195. const shadersData = this.material !== null ? { fragment: {}, vertex: {} } : { compute: {} };
  196. for ( const shaderStage in shadersData ) {
  197. let flow = '// code\n\n';
  198. flow += this.flowCode[ shaderStage ];
  199. const flowNodes = this.flowNodes[ shaderStage ];
  200. const mainNode = flowNodes[ flowNodes.length - 1 ];
  201. for ( const node of flowNodes ) {
  202. const flowSlotData = this.getFlowData( node/*, shaderStage*/ );
  203. const slotName = node.name;
  204. if ( slotName ) {
  205. if ( flow.length > 0 ) flow += '\n';
  206. flow += `\t// flow -> ${ slotName }\n\t`;
  207. }
  208. flow += `${ flowSlotData.code }\n\t`;
  209. if ( node === mainNode && shaderStage !== 'compute' ) {
  210. flow += '// result\n\t';
  211. if ( shaderStage === 'vertex' ) {
  212. flow += 'gl_Position = ';
  213. flow += `${ flowSlotData.result };`;
  214. } else if ( shaderStage === 'fragment' ) {
  215. if ( ! node.outputNode.isOutputStructNode ) {
  216. flow += 'fragColor = ';
  217. flow += `${ flowSlotData.result };`;
  218. }
  219. }
  220. }
  221. }
  222. const stageData = shadersData[ shaderStage ];
  223. stageData.uniforms = this.getUniforms( shaderStage );
  224. stageData.attributes = this.getAttributes( shaderStage );
  225. stageData.varyings = this.getVaryings( shaderStage );
  226. stageData.vars = this.getVars( shaderStage );
  227. stageData.structs = this.getStructs( shaderStage );
  228. stageData.codes = this.getCodes( shaderStage );
  229. stageData.flow = flow;
  230. }
  231. if ( this.material !== null ) {
  232. this.vertexShader = this._getGLSLVertexCode( shadersData.vertex );
  233. this.fragmentShader = this._getGLSLFragmentCode( shadersData.fragment );
  234. //console.log( this.vertexShader );
  235. //console.log( this.fragmentShader );
  236. } else {
  237. console.warn( 'GLSLNodeBuilder: compute shaders are not supported.' );
  238. //this.computeShader = this._getGLSLComputeCode( shadersData.compute );
  239. }
  240. }
  241. getUniformFromNode( node, type, shaderStage, name = null ) {
  242. const uniformNode = super.getUniformFromNode( node, type, shaderStage, name );
  243. const nodeData = this.getDataFromNode( node, shaderStage );
  244. let uniformGPU = nodeData.uniformGPU;
  245. if ( uniformGPU === undefined ) {
  246. if ( type === 'texture' ) {
  247. uniformGPU = new NodeSampledTexture( uniformNode.name, uniformNode.node );
  248. this.bindings[ shaderStage ].push( uniformGPU );
  249. } else if ( type === 'cubeTexture' ) {
  250. uniformGPU = new NodeSampledCubeTexture( uniformNode.name, uniformNode.node );
  251. this.bindings[ shaderStage ].push( uniformGPU );
  252. } else {
  253. let uniformsGroup = this.uniformsGroup[ shaderStage ];
  254. if ( uniformsGroup === undefined ) {
  255. uniformsGroup = new UniformsGroup( shaderStage + 'NodeUniforms' );
  256. //uniformsGroup.setVisibility( gpuShaderStageLib[ shaderStage ] );
  257. this.uniformsGroup[ shaderStage ] = uniformsGroup;
  258. this.bindings[ shaderStage ].push( uniformsGroup );
  259. }
  260. uniformGPU = this.getNodeUniform( uniformNode, type );
  261. uniformsGroup.addUniform( uniformGPU );
  262. }
  263. nodeData.uniformGPU = uniformGPU;
  264. }
  265. return uniformNode;
  266. }
  267. build() {
  268. // @TODO: Move this code to super.build()
  269. const { object, material } = this;
  270. if ( material !== null ) {
  271. NodeMaterial.fromMaterial( material ).build( this );
  272. } else {
  273. this.addFlow( 'compute', object );
  274. }
  275. return super.build();
  276. }
  277. }
  278. export default GLSLNodeBuilder;