Skip to content

Commit

Permalink
NodeBuilder: .buildFunctionNode() and getFunctionOperator()
Browse files Browse the repository at this point in the history
  • Loading branch information
sunag committed Dec 2, 2023
1 parent fd57e8d commit 28d1942
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 14 deletions.
18 changes: 18 additions & 0 deletions examples/jsm/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import NodeCode from './NodeCode.js';
import NodeKeywords from './NodeKeywords.js';
import NodeCache from './NodeCache.js';
import ParameterNode from './ParameterNode.js';
import FunctionNode from '../code/FunctionNode.js';
import { createNodeMaterialFromType } from '../materials/NodeMaterial.js';
import { NodeUpdateType, defaultBuildStages, shaderStages } from './constants.js';

Expand Down Expand Up @@ -95,6 +96,8 @@ class NodeBuilder {
this.stacks = [];
this.tab = '\t';

this.currentFunctionNode = null;

this.context = {
keywords: new NodeKeywords(),
material: this.material
Expand Down Expand Up @@ -848,6 +851,15 @@ class NodeBuilder {

}

buildFunctionNode( shaderNode ) {

const fn = new FunctionNode();

const previous = this.currentFunctionNode;


}

flowShaderNode( shaderNode ) {

const layout = shaderNode.layout;
Expand Down Expand Up @@ -920,6 +932,12 @@ class NodeBuilder {

}

getFunctionOperator() {

return null;

}

flowChildNode( node, output = null ) {

const previousFlow = this.flow;
Expand Down
25 changes: 19 additions & 6 deletions examples/jsm/nodes/math/OperatorNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,34 +157,47 @@ class OperatorNode extends TempNode {
const b = bNode.build( builder, typeB );

const outputLength = builder.getTypeLength( output );
const fnOpSnippet = builder.getFunctionOperator( op );

if ( output !== 'void' ) {

if ( op === '<' && outputLength > 1 ) {

return builder.format( `${ builder.getMethod( 'lessThan' ) }( ${a}, ${b} )`, type, output );
return builder.format( `${ builder.getMethod( 'lessThan' ) }( ${ a }, ${ b } )`, type, output );

} else if ( op === '<=' && outputLength > 1 ) {

return builder.format( `${ builder.getMethod( 'lessThanEqual' ) }( ${a}, ${b} )`, type, output );
return builder.format( `${ builder.getMethod( 'lessThanEqual' ) }( ${ a }, ${ b } )`, type, output );

} else if ( op === '>' && outputLength > 1 ) {

return builder.format( `${ builder.getMethod( 'greaterThan' ) }( ${a}, ${b} )`, type, output );
return builder.format( `${ builder.getMethod( 'greaterThan' ) }( ${ a }, ${ b } )`, type, output );

} else if ( op === '>=' && outputLength > 1 ) {

return builder.format( `${ builder.getMethod( 'greaterThanEqual' ) }( ${a}, ${b} )`, type, output );
return builder.format( `${ builder.getMethod( 'greaterThanEqual' ) }( ${ a }, ${ b } )`, type, output );

} else if ( fnOpSnippet ) {

return builder.format( `${ fnOpSnippet }( ${ a }, ${ b } )`, type, output );

} else {

return builder.format( `( ${a} ${this.op} ${b} )`, type, output );
return builder.format( `( ${ a } ${ op } ${ b } )`, type, output );

}

} else if ( typeA !== 'void' ) {

return builder.format( `${a} ${this.op} ${b}`, type, output );
if ( fnOpSnippet ) {

return builder.format( `${ fnOpSnippet }( ${ a }, ${ b } )`, type, output );

} else {

return builder.format( `${ a } ${ op } ${ b }`, type, output );

}

}

Expand Down
19 changes: 18 additions & 1 deletion examples/jsm/nodes/shadernode/ShaderNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ class ShaderCallNodeInternal extends Node {

}

if ( builder.currentFunctionNode !== null ) {

builder.currentFunctionNode.includes.push( functionNode );

}

return nodeObject( functionNode.call( inputNodes ) );

}
Expand Down Expand Up @@ -505,7 +511,18 @@ addNodeClass( 'ShaderNode', ShaderNode );

//

export const setCurrentStack = stack => currentStack = stack;
export const setCurrentStack = ( stack ) => {

if ( currentStack === stack ) {

//throw new Error( 'Stack already defined.' );

}

currentStack = stack;

};

export const getCurrentStack = () => currentStack;

export const If = ( ...params ) => currentStack.if( ...params );
Expand Down
30 changes: 30 additions & 0 deletions examples/jsm/renderers/webgl-legacy/nodes/WebGLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,36 @@ class WebGLNodeBuilder extends NodeBuilder {

}

buildFunctionCode( shaderNode ) {

const layout = shaderNode.layout;
const flowData = this.flowShaderNode( shaderNode );

const parameters = [];

for ( const input of layout.inputs ) {

parameters.push( this.getType( input.type ) + ' ' + input.name );

}

//

const code = `${ this.getType( layout.type ) } ${ layout.name }( ${ parameters.join( ', ' ) } ) {
${ flowData.vars }
${ flowData.code }
return ${ flowData.result };
}`;

//

return code;

}

getUniforms( shaderStage ) {

const uniforms = this.uniforms[ shaderStage ];
Expand Down
6 changes: 3 additions & 3 deletions examples/jsm/renderers/webgl/nodes/GLSLNodeBuilder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MathNode, GLSLNodeParser, NodeBuilder, NodeMaterial, FunctionNode } from '../../../nodes/Nodes.js';
import { MathNode, GLSLNodeParser, NodeBuilder, NodeMaterial } from '../../../nodes/Nodes.js';

import UniformBuffer from '../../common/UniformBuffer.js';
import NodeUniformsGroup from '../../common/nodes/NodeUniformsGroup.js';
Expand Down Expand Up @@ -53,7 +53,7 @@ class GLSLNodeBuilder extends NodeBuilder {

}

buildFunctionNode( shaderNode ) {
buildFunctionCode( shaderNode ) {

const layout = shaderNode.layout;
const flowData = this.flowShaderNode( shaderNode );
Expand All @@ -79,7 +79,7 @@ ${ flowData.code }

//

return new FunctionNode( code );
return code;

}

Expand Down
17 changes: 13 additions & 4 deletions examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import UniformBuffer from '../../common/UniformBuffer.js';
import StorageBuffer from '../../common/StorageBuffer.js';
import { getVectorLength, getStrideLength } from '../../common/BufferUtils.js';

import { NodeBuilder, CodeNode, NodeMaterial, FunctionNode } from '../../../nodes/Nodes.js';
import { NodeBuilder, CodeNode, NodeMaterial } from '../../../nodes/Nodes.js';

import { getFormat } from '../utils/WebGPUTextureUtils.js';

Expand Down Expand Up @@ -492,7 +492,7 @@ class WGSLNodeBuilder extends NodeBuilder {

}

buildFunctionNode( shaderNode ) {
buildFunctionCode( shaderNode ) {

const layout = shaderNode.layout;
const flowData = this.flowShaderNode( shaderNode );
Expand All @@ -516,7 +516,7 @@ ${ flowData.code }

//

return new FunctionNode( code );
return code;

}

Expand Down Expand Up @@ -962,7 +962,16 @@ ${ flowData.code }

_include( name ) {

wgslPolyfill[ name ].build( this );
const codeNode = wgslPolyfill[ name ];
codeNode.build( this );

if ( this.currentFunctionNode !== null ) {

this.currentFunctionNode.includes.push( codeNode );

}

return codeNode;

}

Expand Down

0 comments on commit 28d1942

Please sign in to comment.