GLSLNodeBuilder.js 9.9 KB

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