Skip to content
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
12 changes: 3 additions & 9 deletions src/nodes/core/ContextNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ class ContextNode extends Node {

analyze( builder ) {

const previousContext = builder.getContext();

builder.setContext( { ...builder.context, ...this.value } );
const previousContext = builder.addContext( this.value );

this.node.build( builder );

Expand All @@ -106,9 +104,7 @@ class ContextNode extends Node {

setup( builder ) {

const previousContext = builder.getContext();

builder.setContext( { ...builder.context, ...this.value } );
const previousContext = builder.addContext( this.value );

this.node.build( builder );

Expand All @@ -118,9 +114,7 @@ class ContextNode extends Node {

generate( builder, output ) {

const previousContext = builder.getContext();

builder.setContext( { ...builder.context, ...this.value } );
const previousContext = builder.addContext( this.value );

const snippet = this.node.build( builder, output );

Expand Down
16 changes: 16 additions & 0 deletions src/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,22 @@ class NodeBuilder {

}

/**
* Adds context data to the builder's current context.
*
* @param {Object} context - The context to add.
* @return {Object} The previous context.
*/
addContext( context ) {

const previousContext = this.getContext();

this.setContext( { ...this.context, ...context } );

return previousContext;

}

/**
* Gets a context used in shader construction that can be shared across different materials.
* This is necessary since the renderer cache can reuse shaders generated in one material and use them in another.
Expand Down
20 changes: 4 additions & 16 deletions src/nodes/core/StackNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,9 @@ class StackNode extends Node {

for ( const childNode of this.getChildren() ) {

if ( childNode.isVarNode && childNode.intent === true ) {
if ( childNode.isVarNode && childNode.isAssign( builder ) !== true ) {

const properties = builder.getNodeProperties( childNode );

if ( properties.assign !== true ) {

continue;

}
continue;

}

Expand Down Expand Up @@ -302,15 +296,9 @@ class StackNode extends Node {

for ( const node of this.nodes ) {

if ( node.isVarNode && node.intent === true ) {
if ( node.isVarNode && node.isAssign( builder ) !== true ) {

const properties = builder.getNodeProperties( node );

if ( properties.assign !== true ) {

continue;

}
continue;

}

Expand Down
43 changes: 41 additions & 2 deletions src/nodes/core/VarNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,53 @@ class VarNode extends Node {

}

isAssign( builder ) {

if ( this.intent !== true ) return true;

//

const properties = builder.getNodeProperties( this );

let assign = properties.assign;

if ( assign !== true ) {

if ( this.node.isShaderCallNodeInternal && this.node.shaderNode.getLayout() === null ) {

if ( builder.context.fnCall && builder.context.fnCall.shaderNode ) {

const nodeType = this.node.getNodeType( builder );

if ( nodeType !== 'void' ) {

const shaderNodeData = builder.getDataFromNode( this.node.shaderNode );

if ( shaderNodeData.hasLoop ) {

assign = true;

}

}

}

}

}

return assign;

}

build( ...params ) {

if ( this.intent === true ) {

const builder = params[ 0 ];
const properties = builder.getNodeProperties( this );

if ( properties.assign !== true ) {
if ( this.isAssign( builder ) !== true ) {

return this.node.build( ...params );

Expand Down
14 changes: 13 additions & 1 deletion src/nodes/tsl/TSLCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ class ShaderCallNodeInternal extends Node {
//

const previousSubBuildFn = builder.subBuildFn;
const previousContext = builder.addContext( { fnCall: this } );

builder.subBuildFn = subBuild;

Expand Down Expand Up @@ -565,6 +566,7 @@ class ShaderCallNodeInternal extends Node {
}

builder.subBuildFn = previousSubBuildFn;
builder.setContext( previousContext );

if ( shaderNode.once ) {

Expand Down Expand Up @@ -608,6 +610,8 @@ class ShaderCallNodeInternal extends Node {
const subBuildOutput = builder.getSubBuildOutput( this );
const outputNode = this.getOutputNode( builder );

const previousContext = builder.addContext( { fnCall: this } );

if ( buildStage === 'setup' ) {

const subBuildInitialized = builder.getSubBuildProperty( 'initialized', this );
Expand Down Expand Up @@ -655,6 +659,8 @@ class ShaderCallNodeInternal extends Node {

}

builder.setContext( previousContext );

return result;

}
Expand Down Expand Up @@ -788,9 +794,15 @@ class ShaderNodeInternal extends Node {

}

getLayout() {

return this.layout;

}

call( rawInputs = null ) {

return nodeObject( new ShaderCallNodeInternal( this, rawInputs ) );
return new ShaderCallNodeInternal( this, rawInputs );

}

Expand Down
11 changes: 9 additions & 2 deletions src/nodes/utils/LoopNode.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Node from '../core/Node.js';
import { expression } from '../code/ExpressionNode.js';
import { nodeObject, nodeArray, Fn } from '../tsl/TSLBase.js';
import { nodeArray, Fn } from '../tsl/TSLBase.js';
import { error } from '../../utils.js';

/**
Expand Down Expand Up @@ -139,6 +139,13 @@ class LoopNode extends Node {

this.getProperties( builder );

if ( builder.context.fnCall ) {

const shaderNodeData = builder.getDataFromNode( builder.context.fnCall.shaderNode );
shaderNodeData.hasLoop = true;

}

}

generate( builder ) {
Expand Down Expand Up @@ -333,7 +340,7 @@ export default LoopNode;
* @param {...any} params - A list of parameters.
* @returns {LoopNode}
*/
export const Loop = ( ...params ) => nodeObject( new LoopNode( nodeArray( params, 'int' ) ) ).toStack();
export const Loop = ( ...params ) => new LoopNode( nodeArray( params, 'int' ) ).toStack();

/**
* TSL function for creating a `Continue()` expression.
Expand Down
Loading