GLSLNodeBuilder.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. snippet += `${varying.needsInterpolation ? 'out' : '/*out*/'} ${varying.type} ${varying.name};\n`;
  152. }
  153. } else if ( shaderStage === 'fragment' ) {
  154. for ( const varying of varyings ) {
  155. if ( varying.needsInterpolation ) {
  156. snippet += `in ${varying.type} ${varying.name};\n`;
  157. }
  158. }
  159. }
  160. return snippet;
  161. }
  162. getVertexIndex() {
  163. return 'gl_VertexID';
  164. }
  165. getFrontFacing() {
  166. return 'gl_FrontFacing';
  167. }
  168. getFragCoord() {
  169. return 'gl_FragCoord';
  170. }
  171. isAvailable( name ) {
  172. return supports[ name ] === true;
  173. }
  174. isFlipY() {
  175. return true;
  176. }
  177. _getGLSLUniformStruct( name, vars ) {
  178. return `
  179. layout( std140 ) uniform ${name} {
  180. ${vars}
  181. };`;
  182. }
  183. _getGLSLVertexCode( shaderData ) {
  184. return `#version 300 es
  185. ${ this.getSignature() }
  186. // precision
  187. precision highp float;
  188. precision highp int;
  189. // uniforms
  190. ${shaderData.uniforms}
  191. // varyings
  192. ${shaderData.varyings}
  193. // attributes
  194. ${shaderData.attributes}
  195. // codes
  196. ${shaderData.codes}
  197. void main() {
  198. // vars
  199. ${shaderData.vars}
  200. // flow
  201. ${shaderData.flow}
  202. gl_PointSize = 1.0;
  203. }
  204. `;
  205. }
  206. _getGLSLFragmentCode( shaderData ) {
  207. return `#version 300 es
  208. ${ this.getSignature() }
  209. // precision
  210. precision highp float;
  211. precision highp int;
  212. precision lowp sampler2DShadow;
  213. // uniforms
  214. ${shaderData.uniforms}
  215. // varyings
  216. ${shaderData.varyings}
  217. // codes
  218. ${shaderData.codes}
  219. ${shaderData.structs}
  220. void main() {
  221. // vars
  222. ${shaderData.vars}
  223. // flow
  224. ${shaderData.flow}
  225. }
  226. `;
  227. }
  228. buildCode() {
  229. const shadersData = this.material !== null ? { fragment: {}, vertex: {} } : { compute: {} };
  230. for ( const shaderStage in shadersData ) {
  231. let flow = '// code\n\n';
  232. flow += this.flowCode[ shaderStage ];
  233. const flowNodes = this.flowNodes[ shaderStage ];
  234. const mainNode = flowNodes[ flowNodes.length - 1 ];
  235. for ( const node of flowNodes ) {
  236. const flowSlotData = this.getFlowData( node/*, shaderStage*/ );
  237. const slotName = node.name;
  238. if ( slotName ) {
  239. if ( flow.length > 0 ) flow += '\n';
  240. flow += `\t// flow -> ${ slotName }\n\t`;
  241. }
  242. flow += `${ flowSlotData.code }\n\t`;
  243. if ( node === mainNode && shaderStage !== 'compute' ) {
  244. flow += '// result\n\t';
  245. if ( shaderStage === 'vertex' ) {
  246. flow += 'gl_Position = ';
  247. flow += `${ flowSlotData.result };`;
  248. } else if ( shaderStage === 'fragment' ) {
  249. if ( ! node.outputNode.isOutputStructNode ) {
  250. flow += 'fragColor = ';
  251. flow += `${ flowSlotData.result };`;
  252. }
  253. }
  254. }
  255. }
  256. const stageData = shadersData[ shaderStage ];
  257. stageData.uniforms = this.getUniforms( shaderStage );
  258. stageData.attributes = this.getAttributes( shaderStage );
  259. stageData.varyings = this.getVaryings( shaderStage );
  260. stageData.vars = this.getVars( shaderStage );
  261. stageData.structs = this.getStructs( shaderStage );
  262. stageData.codes = this.getCodes( shaderStage );
  263. stageData.flow = flow;
  264. }
  265. if ( this.material !== null ) {
  266. this.vertexShader = this._getGLSLVertexCode( shadersData.vertex );
  267. this.fragmentShader = this._getGLSLFragmentCode( shadersData.fragment );
  268. } else {
  269. console.warn( 'GLSLNodeBuilder: compute shaders are not supported.' );
  270. //this.computeShader = this._getGLSLComputeCode( shadersData.compute );
  271. }
  272. }
  273. getUniformFromNode( node, type, shaderStage, name = null ) {
  274. const uniformNode = super.getUniformFromNode( node, type, shaderStage, name );
  275. const nodeData = this.getDataFromNode( node, shaderStage );
  276. let uniformGPU = nodeData.uniformGPU;
  277. if ( uniformGPU === undefined ) {
  278. if ( type === 'texture' ) {
  279. uniformGPU = new NodeSampledTexture( uniformNode.name, uniformNode.node );
  280. this.bindings[ shaderStage ].push( uniformGPU );
  281. } else if ( type === 'cubeTexture' ) {
  282. uniformGPU = new NodeSampledCubeTexture( uniformNode.name, uniformNode.node );
  283. this.bindings[ shaderStage ].push( uniformGPU );
  284. } else {
  285. let uniformsGroup = this.uniformsGroup[ shaderStage ];
  286. if ( uniformsGroup === undefined ) {
  287. uniformsGroup = new UniformsGroup( shaderStage + 'NodeUniforms' );
  288. //uniformsGroup.setVisibility( gpuShaderStageLib[ shaderStage ] );
  289. this.uniformsGroup[ shaderStage ] = uniformsGroup;
  290. this.bindings[ shaderStage ].push( uniformsGroup );
  291. }
  292. uniformGPU = this.getNodeUniform( uniformNode, type );
  293. uniformsGroup.addUniform( uniformGPU );
  294. }
  295. nodeData.uniformGPU = uniformGPU;
  296. }
  297. return uniformNode;
  298. }
  299. build() {
  300. // @TODO: Move this code to super.build()
  301. const { object, material } = this;
  302. if ( material !== null ) {
  303. NodeMaterial.fromMaterial( material ).build( this );
  304. } else {
  305. this.addFlow( 'compute', object );
  306. }
  307. return super.build();
  308. }
  309. }
  310. export default GLSLNodeBuilder;