GLSLNodeBuilder.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. import { MathNode, GLSLNodeParser, NodeBuilder, TextureNode, vectorComponents } from '../../../nodes/Nodes.js';
  2. import NodeUniformBuffer from '../../common/nodes/NodeUniformBuffer.js';
  3. import NodeUniformsGroup from '../../common/nodes/NodeUniformsGroup.js';
  4. import { NodeSampledTexture, NodeSampledCubeTexture, NodeSampledTexture3D } from '../../common/nodes/NodeSampledTexture.js';
  5. import { ByteType, ShortType, RGBAIntegerFormat, RGBIntegerFormat, RedIntegerFormat, RGIntegerFormat, UnsignedByteType, UnsignedIntType, UnsignedShortType, RedFormat, RGFormat, IntType, RGBFormat, RGBAFormat, FloatType } from '../../../constants.js';
  6. import { DataTexture } from '../../../textures/DataTexture.js';
  7. const glslMethods = {
  8. [ MathNode.ATAN2 ]: 'atan',
  9. textureDimensions: 'textureSize',
  10. equals: 'equal'
  11. };
  12. const precisionLib = {
  13. low: 'lowp',
  14. medium: 'mediump',
  15. high: 'highp'
  16. };
  17. const supports = {
  18. swizzleAssign: true,
  19. storageBuffer: false
  20. };
  21. const defaultPrecisions = `
  22. precision highp float;
  23. precision highp int;
  24. precision highp sampler2D;
  25. precision highp sampler3D;
  26. precision highp samplerCube;
  27. precision highp sampler2DArray;
  28. precision highp usampler2D;
  29. precision highp usampler3D;
  30. precision highp usamplerCube;
  31. precision highp usampler2DArray;
  32. precision highp isampler2D;
  33. precision highp isampler3D;
  34. precision highp isamplerCube;
  35. precision highp isampler2DArray;
  36. precision lowp sampler2DShadow;
  37. `;
  38. class GLSLNodeBuilder extends NodeBuilder {
  39. constructor( object, renderer ) {
  40. super( object, renderer, new GLSLNodeParser() );
  41. this.uniformGroups = {};
  42. this.transforms = [];
  43. this.instanceBindGroups = false;
  44. }
  45. getMethod( method ) {
  46. return glslMethods[ method ] || method;
  47. }
  48. getOutputStructName() {
  49. return '';
  50. }
  51. buildFunctionCode( shaderNode ) {
  52. const layout = shaderNode.layout;
  53. const flowData = this.flowShaderNode( shaderNode );
  54. const parameters = [];
  55. for ( const input of layout.inputs ) {
  56. parameters.push( this.getType( input.type ) + ' ' + input.name );
  57. }
  58. //
  59. const code = `${ this.getType( layout.type ) } ${ layout.name }( ${ parameters.join( ', ' ) } ) {
  60. ${ flowData.vars }
  61. ${ flowData.code }
  62. return ${ flowData.result };
  63. }`;
  64. //
  65. return code;
  66. }
  67. setupPBO( storageBufferNode ) {
  68. const attribute = storageBufferNode.value;
  69. if ( attribute.pbo === undefined ) {
  70. const originalArray = attribute.array;
  71. const numElements = attribute.count * attribute.itemSize;
  72. const { itemSize } = attribute;
  73. const isInteger = attribute.array.constructor.name.toLowerCase().includes( 'int' );
  74. let format = isInteger ? RedIntegerFormat : RedFormat;
  75. if ( itemSize === 2 ) {
  76. format = isInteger ? RGIntegerFormat : RGFormat;
  77. } else if ( itemSize === 3 ) {
  78. format = isInteger ? RGBIntegerFormat : RGBFormat;
  79. } else if ( itemSize === 4 ) {
  80. format = isInteger ? RGBAIntegerFormat : RGBAFormat;
  81. }
  82. const typeMap = {
  83. Float32Array: FloatType,
  84. Uint8Array: UnsignedByteType,
  85. Uint16Array: UnsignedShortType,
  86. Uint32Array: UnsignedIntType,
  87. Int8Array: ByteType,
  88. Int16Array: ShortType,
  89. Int32Array: IntType,
  90. Uint8ClampedArray: UnsignedByteType,
  91. };
  92. const width = Math.pow( 2, Math.ceil( Math.log2( Math.sqrt( numElements / itemSize ) ) ) );
  93. let height = Math.ceil( ( numElements / itemSize ) / width );
  94. if ( width * height * itemSize < numElements ) height ++; // Ensure enough space
  95. const newSize = width * height * itemSize;
  96. const newArray = new originalArray.constructor( newSize );
  97. newArray.set( originalArray, 0 );
  98. attribute.array = newArray;
  99. const pboTexture = new DataTexture( attribute.array, width, height, format, typeMap[ attribute.array.constructor.name ] || FloatType );
  100. pboTexture.needsUpdate = true;
  101. pboTexture.isPBOTexture = true;
  102. const pbo = new TextureNode( pboTexture, null, null );
  103. pbo.setPrecision( 'high' );
  104. attribute.pboNode = pbo;
  105. attribute.pbo = pbo.value;
  106. this.getUniformFromNode( attribute.pboNode, 'texture', this.shaderStage, this.context.label );
  107. }
  108. }
  109. getPropertyName( node, shaderStage = this.shaderStage ) {
  110. if ( node.isNodeUniform && node.node.isTextureNode !== true && node.node.isBufferNode !== true ) {
  111. return shaderStage.charAt( 0 ) + '_' + node.name;
  112. }
  113. return super.getPropertyName( node, shaderStage );
  114. }
  115. generatePBO( storageArrayElementNode ) {
  116. const { node, indexNode } = storageArrayElementNode;
  117. const attribute = node.value;
  118. if ( this.renderer.backend.has( attribute ) ) {
  119. const attributeData = this.renderer.backend.get( attribute );
  120. attributeData.pbo = attribute.pbo;
  121. }
  122. const nodeUniform = this.getUniformFromNode( attribute.pboNode, 'texture', this.shaderStage, this.context.label );
  123. const textureName = this.getPropertyName( nodeUniform );
  124. indexNode.increaseUsage( this ); // force cache generate to be used as index in x,y
  125. const indexSnippet = indexNode.build( this, 'uint' );
  126. const elementNodeData = this.getDataFromNode( storageArrayElementNode );
  127. let propertyName = elementNodeData.propertyName;
  128. if ( propertyName === undefined ) {
  129. // property element
  130. const nodeVar = this.getVarFromNode( storageArrayElementNode );
  131. propertyName = this.getPropertyName( nodeVar );
  132. // property size
  133. const bufferNodeData = this.getDataFromNode( node );
  134. let propertySizeName = bufferNodeData.propertySizeName;
  135. if ( propertySizeName === undefined ) {
  136. propertySizeName = propertyName + 'Size';
  137. this.getVarFromNode( node, propertySizeName, 'uint' );
  138. this.addLineFlowCode( `${ propertySizeName } = uint( textureSize( ${ textureName }, 0 ).x )` );
  139. bufferNodeData.propertySizeName = propertySizeName;
  140. }
  141. //
  142. const { itemSize } = attribute;
  143. const channel = '.' + vectorComponents.join( '' ).slice( 0, itemSize );
  144. const uvSnippet = `ivec2(${indexSnippet} % ${ propertySizeName }, ${indexSnippet} / ${ propertySizeName })`;
  145. const snippet = this.generateTextureLoad( null, textureName, uvSnippet, null, '0' );
  146. //
  147. let prefix = 'vec4';
  148. if ( attribute.pbo.type === UnsignedIntType ) {
  149. prefix = 'uvec4';
  150. } else if ( attribute.pbo.type === IntType ) {
  151. prefix = 'ivec4';
  152. }
  153. this.addLineFlowCode( `${ propertyName } = ${prefix}(${ snippet })${channel}` );
  154. elementNodeData.propertyName = propertyName;
  155. }
  156. return propertyName;
  157. }
  158. generateTextureLoad( texture, textureProperty, uvIndexSnippet, depthSnippet, levelSnippet = '0' ) {
  159. if ( depthSnippet ) {
  160. return `texelFetch( ${ textureProperty }, ivec3( ${ uvIndexSnippet }, ${ depthSnippet } ), ${ levelSnippet } )`;
  161. } else {
  162. return `texelFetch( ${ textureProperty }, ${ uvIndexSnippet }, ${ levelSnippet } )`;
  163. }
  164. }
  165. generateTexture( texture, textureProperty, uvSnippet, depthSnippet ) {
  166. if ( texture.isDepthTexture ) {
  167. return `texture( ${ textureProperty }, ${ uvSnippet } ).x`;
  168. } else {
  169. if ( depthSnippet ) uvSnippet = `vec3( ${ uvSnippet }, ${ depthSnippet } )`;
  170. return `texture( ${ textureProperty }, ${ uvSnippet } )`;
  171. }
  172. }
  173. generateTextureLevel( texture, textureProperty, uvSnippet, levelSnippet ) {
  174. return `textureLod( ${ textureProperty }, ${ uvSnippet }, ${ levelSnippet } )`;
  175. }
  176. generateTextureBias( texture, textureProperty, uvSnippet, biasSnippet ) {
  177. return `texture( ${ textureProperty }, ${ uvSnippet }, ${ biasSnippet } )`;
  178. }
  179. generateTextureGrad( texture, textureProperty, uvSnippet, gradSnippet ) {
  180. return `textureGrad( ${ textureProperty }, ${ uvSnippet }, ${ gradSnippet[ 0 ] }, ${ gradSnippet[ 1 ] } )`;
  181. }
  182. generateTextureCompare( texture, textureProperty, uvSnippet, compareSnippet, depthSnippet, shaderStage = this.shaderStage ) {
  183. if ( shaderStage === 'fragment' ) {
  184. return `texture( ${ textureProperty }, vec3( ${ uvSnippet }, ${ compareSnippet } ) )`;
  185. } else {
  186. console.error( `WebGPURenderer: THREE.DepthTexture.compareFunction() does not support ${ shaderStage } shader.` );
  187. }
  188. }
  189. getVars( shaderStage ) {
  190. const snippets = [];
  191. const vars = this.vars[ shaderStage ];
  192. if ( vars !== undefined ) {
  193. for ( const variable of vars ) {
  194. snippets.push( `${ this.getVar( variable.type, variable.name ) };` );
  195. }
  196. }
  197. return snippets.join( '\n\t' );
  198. }
  199. getUniforms( shaderStage ) {
  200. const uniforms = this.uniforms[ shaderStage ];
  201. const bindingSnippets = [];
  202. const uniformGroups = {};
  203. for ( const uniform of uniforms ) {
  204. let snippet = null;
  205. let group = false;
  206. if ( uniform.type === 'texture' ) {
  207. const texture = uniform.node.value;
  208. let typePrefix = '';
  209. if ( texture.isDataTexture === true ) {
  210. if ( texture.type === UnsignedIntType ) {
  211. typePrefix = 'u';
  212. } else if ( texture.type === IntType ) {
  213. typePrefix = 'i';
  214. }
  215. }
  216. if ( texture.compareFunction ) {
  217. snippet = `sampler2DShadow ${ uniform.name };`;
  218. } else if ( texture.isDataArrayTexture === true ) {
  219. snippet = `${typePrefix}sampler2DArray ${ uniform.name };`;
  220. } else {
  221. snippet = `${typePrefix}sampler2D ${ uniform.name };`;
  222. }
  223. } else if ( uniform.type === 'cubeTexture' ) {
  224. snippet = `samplerCube ${ uniform.name };`;
  225. } else if ( uniform.type === 'texture3D' ) {
  226. snippet = `sampler3D ${ uniform.name };`;
  227. } else if ( uniform.type === 'buffer' ) {
  228. const bufferNode = uniform.node;
  229. const bufferType = this.getType( bufferNode.bufferType );
  230. const bufferCount = bufferNode.bufferCount;
  231. const bufferCountSnippet = bufferCount > 0 ? bufferCount : '';
  232. snippet = `${bufferNode.name} {\n\t${ bufferType } ${ uniform.name }[${ bufferCountSnippet }];\n};\n`;
  233. } else {
  234. const vectorType = this.getVectorType( uniform.type );
  235. snippet = `${ vectorType } ${ this.getPropertyName( uniform, shaderStage ) };`;
  236. group = true;
  237. }
  238. const precision = uniform.node.precision;
  239. if ( precision !== null ) {
  240. snippet = precisionLib[ precision ] + ' ' + snippet;
  241. }
  242. if ( group ) {
  243. snippet = '\t' + snippet;
  244. const groupName = uniform.groupNode.name;
  245. const groupSnippets = uniformGroups[ groupName ] || ( uniformGroups[ groupName ] = [] );
  246. groupSnippets.push( snippet );
  247. } else {
  248. snippet = 'uniform ' + snippet;
  249. bindingSnippets.push( snippet );
  250. }
  251. }
  252. let output = '';
  253. for ( const name in uniformGroups ) {
  254. const groupSnippets = uniformGroups[ name ];
  255. output += this._getGLSLUniformStruct( shaderStage + '_' + name, groupSnippets.join( '\n' ) ) + '\n';
  256. }
  257. output += bindingSnippets.join( '\n' );
  258. return output;
  259. }
  260. getTypeFromAttribute( attribute ) {
  261. let nodeType = super.getTypeFromAttribute( attribute );
  262. if ( /^[iu]/.test( nodeType ) && attribute.gpuType !== IntType ) {
  263. let dataAttribute = attribute;
  264. if ( attribute.isInterleavedBufferAttribute ) dataAttribute = attribute.data;
  265. const array = dataAttribute.array;
  266. if ( ( array instanceof Uint32Array || array instanceof Int32Array || array instanceof Uint16Array || array instanceof Int16Array ) === false ) {
  267. nodeType = nodeType.slice( 1 );
  268. }
  269. }
  270. return nodeType;
  271. }
  272. getAttributes( shaderStage ) {
  273. let snippet = '';
  274. if ( shaderStage === 'vertex' || shaderStage === 'compute' ) {
  275. const attributes = this.getAttributesArray();
  276. let location = 0;
  277. for ( const attribute of attributes ) {
  278. snippet += `layout( location = ${ location ++ } ) in ${ attribute.type } ${ attribute.name };\n`;
  279. }
  280. }
  281. return snippet;
  282. }
  283. getStructMembers( struct ) {
  284. const snippets = [];
  285. const members = struct.getMemberTypes();
  286. for ( let i = 0; i < members.length; i ++ ) {
  287. const member = members[ i ];
  288. snippets.push( `layout( location = ${i} ) out ${ member} m${i};` );
  289. }
  290. return snippets.join( '\n' );
  291. }
  292. getStructs( shaderStage ) {
  293. const snippets = [];
  294. const structs = this.structs[ shaderStage ];
  295. if ( structs.length === 0 ) {
  296. return 'layout( location = 0 ) out vec4 fragColor;\n';
  297. }
  298. for ( let index = 0, length = structs.length; index < length; index ++ ) {
  299. const struct = structs[ index ];
  300. let snippet = '\n';
  301. snippet += this.getStructMembers( struct );
  302. snippet += '\n';
  303. snippets.push( snippet );
  304. }
  305. return snippets.join( '\n\n' );
  306. }
  307. getVaryings( shaderStage ) {
  308. let snippet = '';
  309. const varyings = this.varyings;
  310. if ( shaderStage === 'vertex' || shaderStage === 'compute' ) {
  311. for ( const varying of varyings ) {
  312. if ( shaderStage === 'compute' ) varying.needsInterpolation = true;
  313. const type = varying.type;
  314. const flat = type.includes( 'int' ) || type.includes( 'uv' ) || type.includes( 'iv' ) ? 'flat ' : '';
  315. snippet += `${flat}${varying.needsInterpolation ? 'out' : '/*out*/'} ${type} ${varying.name};\n`;
  316. }
  317. } else if ( shaderStage === 'fragment' ) {
  318. for ( const varying of varyings ) {
  319. if ( varying.needsInterpolation ) {
  320. const type = varying.type;
  321. const flat = type.includes( 'int' ) || type.includes( 'uv' ) || type.includes( 'iv' ) ? 'flat ' : '';
  322. snippet += `${flat}in ${type} ${varying.name};\n`;
  323. }
  324. }
  325. }
  326. return snippet;
  327. }
  328. getVertexIndex() {
  329. return 'uint( gl_VertexID )';
  330. }
  331. getInstanceIndex() {
  332. return 'uint( gl_InstanceID )';
  333. }
  334. getDrawIndex() {
  335. const extensions = this.renderer.backend.extensions;
  336. if ( extensions.has( 'WEBGL_multi_draw' ) ) {
  337. return 'uint( gl_DrawID )';
  338. }
  339. return null;
  340. }
  341. getFrontFacing() {
  342. return 'gl_FrontFacing';
  343. }
  344. getFragCoord() {
  345. return 'gl_FragCoord';
  346. }
  347. getFragDepth() {
  348. return 'gl_FragDepth';
  349. }
  350. getExtensions( shaderStage ) {
  351. let extensions = '';
  352. if ( shaderStage === 'vertex' ) {
  353. const ext = this.renderer.backend.extensions;
  354. const isBatchedMesh = this.object.isBatchedMesh;
  355. if ( isBatchedMesh && ext.has( 'WEBGL_multi_draw' ) ) {
  356. extensions += '#extension GL_ANGLE_multi_draw : require\n';
  357. }
  358. }
  359. return extensions;
  360. }
  361. isAvailable( name ) {
  362. let result = supports[ name ];
  363. if ( result === undefined ) {
  364. if ( name === 'float32Filterable' ) {
  365. const extensions = this.renderer.backend.extensions;
  366. if ( extensions.has( 'OES_texture_float_linear' ) ) {
  367. extensions.get( 'OES_texture_float_linear' );
  368. result = true;
  369. } else {
  370. result = false;
  371. }
  372. }
  373. supports[ name ] = result;
  374. }
  375. return result;
  376. }
  377. isFlipY() {
  378. return true;
  379. }
  380. registerTransform( varyingName, attributeNode ) {
  381. this.transforms.push( { varyingName, attributeNode } );
  382. }
  383. getTransforms( /* shaderStage */ ) {
  384. const transforms = this.transforms;
  385. let snippet = '';
  386. for ( let i = 0; i < transforms.length; i ++ ) {
  387. const transform = transforms[ i ];
  388. const attributeName = this.getPropertyName( transform.attributeNode );
  389. snippet += `${ transform.varyingName } = ${ attributeName };\n\t`;
  390. }
  391. return snippet;
  392. }
  393. _getGLSLUniformStruct( name, vars ) {
  394. return `
  395. layout( std140 ) uniform ${name} {
  396. ${vars}
  397. };`;
  398. }
  399. _getGLSLVertexCode( shaderData ) {
  400. return `#version 300 es
  401. // extensions
  402. ${shaderData.extensions}
  403. // precision
  404. ${ defaultPrecisions }
  405. // uniforms
  406. ${shaderData.uniforms}
  407. // varyings
  408. ${shaderData.varyings}
  409. // attributes
  410. ${shaderData.attributes}
  411. // codes
  412. ${shaderData.codes}
  413. void main() {
  414. // vars
  415. ${shaderData.vars}
  416. // transforms
  417. ${shaderData.transforms}
  418. // flow
  419. ${shaderData.flow}
  420. gl_PointSize = 1.0;
  421. }
  422. `;
  423. }
  424. _getGLSLFragmentCode( shaderData ) {
  425. return `#version 300 es
  426. ${ this.getSignature() }
  427. // precision
  428. ${ defaultPrecisions }
  429. // uniforms
  430. ${shaderData.uniforms}
  431. // varyings
  432. ${shaderData.varyings}
  433. // codes
  434. ${shaderData.codes}
  435. ${shaderData.structs}
  436. void main() {
  437. // vars
  438. ${shaderData.vars}
  439. // flow
  440. ${shaderData.flow}
  441. }
  442. `;
  443. }
  444. buildCode() {
  445. const shadersData = this.material !== null ? { fragment: {}, vertex: {} } : { compute: {} };
  446. for ( const shaderStage in shadersData ) {
  447. let flow = '// code\n\n';
  448. flow += this.flowCode[ shaderStage ];
  449. const flowNodes = this.flowNodes[ shaderStage ];
  450. const mainNode = flowNodes[ flowNodes.length - 1 ];
  451. for ( const node of flowNodes ) {
  452. const flowSlotData = this.getFlowData( node/*, shaderStage*/ );
  453. const slotName = node.name;
  454. if ( slotName ) {
  455. if ( flow.length > 0 ) flow += '\n';
  456. flow += `\t// flow -> ${ slotName }\n\t`;
  457. }
  458. flow += `${ flowSlotData.code }\n\t`;
  459. if ( node === mainNode && shaderStage !== 'compute' ) {
  460. flow += '// result\n\t';
  461. if ( shaderStage === 'vertex' ) {
  462. flow += 'gl_Position = ';
  463. flow += `${ flowSlotData.result };`;
  464. } else if ( shaderStage === 'fragment' ) {
  465. if ( ! node.outputNode.isOutputStructNode ) {
  466. flow += 'fragColor = ';
  467. flow += `${ flowSlotData.result };`;
  468. }
  469. }
  470. }
  471. }
  472. const stageData = shadersData[ shaderStage ];
  473. stageData.extensions = this.getExtensions( shaderStage );
  474. stageData.uniforms = this.getUniforms( shaderStage );
  475. stageData.attributes = this.getAttributes( shaderStage );
  476. stageData.varyings = this.getVaryings( shaderStage );
  477. stageData.vars = this.getVars( shaderStage );
  478. stageData.structs = this.getStructs( shaderStage );
  479. stageData.codes = this.getCodes( shaderStage );
  480. stageData.transforms = this.getTransforms( shaderStage );
  481. stageData.flow = flow;
  482. }
  483. if ( this.material !== null ) {
  484. this.vertexShader = this._getGLSLVertexCode( shadersData.vertex );
  485. this.fragmentShader = this._getGLSLFragmentCode( shadersData.fragment );
  486. } else {
  487. this.computeShader = this._getGLSLVertexCode( shadersData.compute );
  488. }
  489. }
  490. getUniformFromNode( node, type, shaderStage, name = null ) {
  491. const uniformNode = super.getUniformFromNode( node, type, shaderStage, name );
  492. const nodeData = this.getDataFromNode( node, shaderStage, this.globalCache );
  493. let uniformGPU = nodeData.uniformGPU;
  494. if ( uniformGPU === undefined ) {
  495. const group = node.groupNode;
  496. const groupName = group.name;
  497. const bindings = this.getBindGroupArray( groupName, shaderStage );
  498. if ( type === 'texture' ) {
  499. uniformGPU = new NodeSampledTexture( uniformNode.name, uniformNode.node, group );
  500. bindings.push( uniformGPU );
  501. } else if ( type === 'cubeTexture' ) {
  502. uniformGPU = new NodeSampledCubeTexture( uniformNode.name, uniformNode.node, group );
  503. bindings.push( uniformGPU );
  504. } else if ( type === 'texture3D' ) {
  505. uniformGPU = new NodeSampledTexture3D( uniformNode.name, uniformNode.node, group );
  506. bindings.push( uniformGPU );
  507. } else if ( type === 'buffer' ) {
  508. node.name = `NodeBuffer_${ node.id }`;
  509. uniformNode.name = `buffer${ node.id }`;
  510. const buffer = new NodeUniformBuffer( node, group );
  511. buffer.name = node.name;
  512. bindings.push( buffer );
  513. uniformGPU = buffer;
  514. } else {
  515. const uniformsStage = this.uniformGroups[ shaderStage ] || ( this.uniformGroups[ shaderStage ] = {} );
  516. let uniformsGroup = uniformsStage[ groupName ];
  517. if ( uniformsGroup === undefined ) {
  518. uniformsGroup = new NodeUniformsGroup( shaderStage + '_' + groupName, group );
  519. //uniformsGroup.setVisibility( gpuShaderStageLib[ shaderStage ] );
  520. uniformsStage[ groupName ] = uniformsGroup;
  521. bindings.push( uniformsGroup );
  522. }
  523. uniformGPU = this.getNodeUniform( uniformNode, type );
  524. uniformsGroup.addUniform( uniformGPU );
  525. }
  526. nodeData.uniformGPU = uniformGPU;
  527. }
  528. return uniformNode;
  529. }
  530. }
  531. export default GLSLNodeBuilder;