Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodeBuilder: Introduce addFlowCodeHierarchy() / NodeBlock #29495

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/webgpu_volume_perlin.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@

const geometry = new THREE.BoxGeometry( 1, 1, 1 );
const material = new THREE.VolumeNodeMaterial( {
side: THREE.BackSide,
side: THREE.BackSide
} );

material.base = new THREE.Color( 0x798aa0 );
Expand All @@ -103,7 +103,7 @@

If( mapValue.greaterThan( threshold ), () => {

const p = vec3().temp().assign( probe ).addAssign( 0.5 );
const p = vec3( probe ).add( 0.5 );

finalColor.rgb.assign( map.normal( p ).mul( 0.5 ).add( probe.mul( 1.5 ).add( 0.25 ) ) );
finalColor.a.assign( 1 );
Expand Down
12 changes: 6 additions & 6 deletions src/materials/nodes/VolumeNodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { materialReference } from '../../nodes/accessors/MaterialReferenceNode.j
import { modelWorldMatrixInverse } from '../../nodes/accessors/ModelNode.js';
import { cameraPosition } from '../../nodes/accessors/Camera.js';
import { positionGeometry } from '../../nodes/accessors/Position.js';
import { Fn, varying, vec2, vec3, vec4 } from '../../nodes/tsl/TSLBase.js';
import { Fn, varying, float, vec2, vec3, vec4 } from '../../nodes/tsl/TSLBase.js';
import { min, max } from '../../nodes/math/MathNode.js';
import { Loop, Break } from '../../nodes/utils/LoopNode.js';
import { texture3D } from '../../nodes/accessors/Texture3DNode.js';
Expand Down Expand Up @@ -59,19 +59,19 @@ class VolumeNodeMaterial extends NodeMaterial {
const vDirection = varying( positionGeometry.sub( vOrigin ) );

const rayDir = vDirection.normalize();
const bounds = property( 'vec2', 'bounds' ).assign( hitBox( { orig: vOrigin, dir: rayDir } ) );
const bounds = vec2( hitBox( { orig: vOrigin, dir: rayDir } ) ).toVar();

bounds.x.greaterThan( bounds.y ).discard();

bounds.assign( vec2( max( bounds.x, 0.0 ), bounds.y ) );

const p = property( 'vec3', 'p' ).assign( vOrigin.add( bounds.x.mul( rayDir ) ) );
const inc = property( 'vec3', 'inc' ).assign( vec3( rayDir.abs().reciprocal() ) );
const delta = property( 'float', 'delta' ).assign( min( inc.x, min( inc.y, inc.z ) ) );
const p = vec3( vOrigin.add( bounds.x.mul( rayDir ) ) ).toVar();
const inc = vec3( rayDir.abs().reciprocal() ).toVar();
const delta = float( min( inc.x, min( inc.y, inc.z ) ) ).toVar( 'delta' ); // used 'delta' name in loop

delta.divAssign( materialReference( 'steps', 'float' ) );

const ac = property( 'vec4', 'ac' ).assign( vec4( materialReference( 'base', 'color' ), 0.0 ) );
const ac = vec4( materialReference( 'base', 'color' ), 0.0 ).toVar();

Loop( { type: 'float', start: bounds.x, end: bounds.y, update: '+= delta' }, () => {

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/accessors/StorageTextureNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class StorageTextureNode extends TextureNode {

const snippet = builder.generateTextureStore( builder, textureProperty, uvSnippet, storeSnippet );

builder.addLineFlowCode( snippet );
builder.addLineFlowCode( snippet, this );

}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/accessors/Texture3DNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const normal = Fn( ( { texture, uv } ) => {

const epsilon = 0.0001;

const ret = vec3().temp();
const ret = vec3().toVar();

If( uv.x.lessThan( epsilon ), () => {

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/accessors/TextureNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class TextureNode extends UniformNode {

const snippet = this.generateSnippet( builder, textureProperty, uvSnippet, levelSnippet, biasSnippet, depthSnippet, compareSnippet, gradSnippet );

builder.addLineFlowCode( `${propertyName} = ${snippet}` );
builder.addLineFlowCode( `${propertyName} = ${snippet}`, this );

nodeData.snippet = snippet;
nodeData.propertyName = propertyName;
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/code/ExpressionNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ExpressionNode extends Node {

if ( type === 'void' ) {

builder.addLineFlowCode( snippet );
builder.addLineFlowCode( snippet, this );

} else {

Expand Down
6 changes: 3 additions & 3 deletions src/nodes/core/AssignNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ class AssignNode extends TempNode {
const sourceVar = builder.getVarFromNode( this, null, targetType );
const sourceProperty = builder.getPropertyName( sourceVar );

builder.addLineFlowCode( `${ sourceProperty } = ${ source }` );
builder.addLineFlowCode( `${ sourceProperty } = ${ source }`, this );

const targetRoot = targetNode.node.context( { assign: true } ).build( builder );

for ( let i = 0; i < targetNode.components.length; i ++ ) {

const component = targetNode.components[ i ];

builder.addLineFlowCode( `${ targetRoot }.${ component } = ${ sourceProperty }[ ${ i } ]` );
builder.addLineFlowCode( `${ targetRoot }.${ component } = ${ sourceProperty }[ ${ i } ]`, this );

}

Expand All @@ -104,7 +104,7 @@ class AssignNode extends TempNode {

if ( output === 'void' || sourceType === 'void' ) {

builder.addLineFlowCode( snippet );
builder.addLineFlowCode( snippet, this );

if ( output !== 'void' ) {

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/core/BypassNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BypassNode extends Node {

if ( snippet !== '' ) {

builder.addLineFlowCode( snippet );
builder.addLineFlowCode( snippet, this );

}

Expand Down
4 changes: 4 additions & 0 deletions src/nodes/core/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ class Node extends EventDispatcher {

nodeData.snippet = result;

} else if ( nodeData.flowCodes !== undefined && builder.context.nodeBlock !== undefined ) {

builder.addFlowCodeHierarchy( this, builder.context.nodeBlock );

}

result = builder.format( result, type, output );
Expand Down
51 changes: 50 additions & 1 deletion src/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -941,10 +941,59 @@ class NodeBuilder {

}

addLineFlowCode( code ) {
addFlowCodeHierarchy( node, nodeBlock ) {

const { flowCodes, flowCodeBlock } = this.getDataFromNode( node );

let needsFlowCode = true;
let nodeBlockHierarchy = nodeBlock;

while ( nodeBlockHierarchy ) {

if ( flowCodeBlock.get( nodeBlockHierarchy ) === true ) {

needsFlowCode = false;
break;

}

nodeBlockHierarchy = this.getDataFromNode( nodeBlockHierarchy ).parentNodeBlock;

}

if ( needsFlowCode ) {

for ( const flowCode of flowCodes ) {

this.addLineFlowCode( flowCode );

}

}

}

addLineFlowCodeBlock( node, code, nodeBlock ) {

const nodeData = this.getDataFromNode( node );
const flowCodes = nodeData.flowCodes || ( nodeData.flowCodes = [] );
const codeBlock = nodeData.flowCodeBlock || ( nodeData.flowCodeBlock = new WeakMap() );

flowCodes.push( code );
codeBlock.set( nodeBlock, true );

}

addLineFlowCode( code, node = null ) {

if ( code === '' ) return this;

if ( node !== null && this.context.nodeBlock ) {

this.addLineFlowCodeBlock( node, code, this.context.nodeBlock );

}

code = this.tab + code;

if ( ! /;\s*$/.test( code ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/core/OutputStructNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class OutputStructNode extends Node {

const snippet = members[ i ].build( builder, output );

builder.addLineFlowCode( `${ structPrefix }m${ i } = ${ snippet }` );
builder.addLineFlowCode( `${ structPrefix }m${ i } = ${ snippet }`, this );

}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/core/TempNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TempNode extends Node {
const nodeVar = builder.getVarFromNode( this, null, type );
const propertyName = builder.getPropertyName( nodeVar );

builder.addLineFlowCode( `${propertyName} = ${snippet}` );
builder.addLineFlowCode( `${propertyName} = ${snippet}`, this );

nodeData.snippet = snippet;
nodeData.propertyName = propertyName;
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/core/VarNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class VarNode extends Node {

const snippet = node.build( builder, nodeVar.type );

builder.addLineFlowCode( `${propertyName} = ${snippet}` );
builder.addLineFlowCode( `${propertyName} = ${snippet}`, this );

return propertyName;

Expand Down
4 changes: 2 additions & 2 deletions src/nodes/gpgpu/AtomicFunctionNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class AtomicFunctionNode extends TempNode {

const varSnippet = this.storeNode.build( builder, inputType );

builder.addLineFlowCode( `${varSnippet} = ${methodSnippet}` );
builder.addLineFlowCode( `${varSnippet} = ${methodSnippet}`, this );

} else {

builder.addLineFlowCode( methodSnippet );
builder.addLineFlowCode( methodSnippet, this );

}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/gpgpu/BarrierNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BarrierNode extends Node {

} else {

builder.addLineFlowCode( `${scope}Barrier()` );
builder.addLineFlowCode( `${scope}Barrier()`, this );

}

Expand Down
2 changes: 0 additions & 2 deletions src/nodes/gpgpu/ComputeBuiltinNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class ComputeBuiltinNode extends Node {

getBuiltinName( /*builder*/ ) {

console.log( this._builtinName );

return this._builtinName;

}
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/gpgpu/ComputeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ComputeNode extends Node {

if ( snippet !== '' ) {

builder.addLineFlowCode( snippet );
builder.addLineFlowCode( snippet, this );

}

Expand Down
19 changes: 16 additions & 3 deletions src/nodes/math/ConditionalNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,23 @@ class ConditionalNode extends Node {

setup( builder ) {

const condNode = this.condNode.cache();
const ifNode = this.ifNode.cache();
const elseNode = this.elseNode ? this.elseNode.cache() : null;

//

const currentNodeBlock = builder.context.nodeBlock;

builder.getDataFromNode( ifNode ).parentNodeBlock = currentNodeBlock;
if ( elseNode !== null ) builder.getDataFromNode( elseNode ).parentNodeBlock = currentNodeBlock;

//

const properties = builder.getNodeProperties( this );
properties.condNode = this.condNode.cache();
properties.ifNode = this.ifNode.cache();
properties.elseNode = this.elseNode ? this.elseNode.cache() : null;
properties.condNode = condNode;
properties.ifNode = ifNode.context( { nodeBlock: ifNode } );
properties.elseNode = elseNode ? elseNode.context( { nodeBlock: elseNode } ) : null;

}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/utils/FlipNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class FlipNode extends TempNode {
const sourceCache = builder.getVarFromNode( this );
const sourceProperty = builder.getPropertyName( sourceCache );

builder.addLineFlowCode( sourceProperty + ' = ' + sourceSnippet );
builder.addLineFlowCode( sourceProperty + ' = ' + sourceSnippet, this );

const length = builder.getTypeLength( sourceType );
const snippetValues = [];
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ ${ flowData.code }

this.getVarFromNode( node, propertySizeName, 'uint' );

this.addLineFlowCode( `${ propertySizeName } = uint( textureSize( ${ textureName }, 0 ).x )` );
this.addLineFlowCode( `${ propertySizeName } = uint( textureSize( ${ textureName }, 0 ).x )`, storageArrayElementNode );

bufferNodeData.propertySizeName = propertySizeName;

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

}

this.addLineFlowCode( `${ propertyName } = ${prefix}(${ snippet })${channel}` );
this.addLineFlowCode( `${ propertyName } = ${prefix}(${ snippet })${channel}`, storageArrayElementNode );

elementNodeData.propertyName = propertyName;

Expand Down