Skip to content
Merged
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
56 changes: 37 additions & 19 deletions src/nodes/utils/FunctionOverloadingNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FunctionOverloadingNode extends Node {
* @private
* @type {ShaderCallNodeInternal}
*/
this._candidateFnCall = null;
this._candidateFn = null;

/**
* This node is marked as global.
Expand All @@ -65,22 +65,30 @@ class FunctionOverloadingNode extends Node {
* @param {NodeBuilder} builder - The current node builder.
* @return {string} The node type.
*/
getNodeType() {
getNodeType( builder ) {

return this.functionNodes[ 0 ].shaderNode.layout.type;
const candidateFn = this.getCandidateFn( builder );

return candidateFn.shaderNode.layout.type;

}

setup( builder ) {
/**
* Returns the candidate function for the current parameters.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {FunctionNode} The candidate function.
*/
getCandidateFn( builder ) {

const params = this.parametersNodes;

let candidateFnCall = this._candidateFnCall;
let candidateFn = this._candidateFn;

if ( candidateFnCall === null ) {
if ( candidateFn === null ) {

let candidateFn = null;
let candidateScore = - 1;
let bestCandidateFn = null;
let bestScore = - 1;

for ( const functionNode of this.functionNodes ) {

Expand All @@ -97,7 +105,7 @@ class FunctionOverloadingNode extends Node {

if ( params.length === inputs.length ) {

let score = 0;
let currentScore = 0;

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

Expand All @@ -106,32 +114,42 @@ class FunctionOverloadingNode extends Node {

if ( param.getNodeType( builder ) === input.type ) {

score ++;

} else {

score = 0;
currentScore ++;

}

}

if ( score > candidateScore ) {
if ( currentScore > bestScore ) {

candidateFn = functionNode;
candidateScore = score;
bestCandidateFn = functionNode;
bestScore = currentScore;

}

}

}

this._candidateFnCall = candidateFnCall = candidateFn( ...params );
this._candidateFn = candidateFn = bestCandidateFn;

}

return candidateFnCall;
return candidateFn;

}

/**
* Sets up the node for the current parameters.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {Node} The setup node.
*/
setup( builder ) {

const candidateFn = this.getCandidateFn( builder );

return candidateFn( ...this.parametersNodes );

}

Expand Down