GLSLNodeBuilder.js 19 KB

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