Skip to content

TSL - Nested If-nodes cause wrong code compilation #31039

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

Closed
holtsetio opened this issue May 3, 2025 · 1 comment · Fixed by #31076
Closed

TSL - Nested If-nodes cause wrong code compilation #31039

holtsetio opened this issue May 3, 2025 · 1 comment · Fixed by #31076
Assignees
Labels
TSL Three.js Shading Language
Milestone

Comments

@holtsetio
Copy link
Contributor

holtsetio commented May 3, 2025

Description

When nesting If- and Else-nodes in TSL, the code compilation gets messed up.

Consider for example the following TSL code:

const foo = float(1).mul(2);
const result = float(0).toVar();
If( instanceIndex.equal(uint(0)), () => {
	result.assign( foo );
} ).Else( () => {
	If( instanceIndex.equal(uint(1)), () => {
		result.assign( foo );
	} ).Else( () => {
		result.assign( foo );
	} );
} );
outputBuffer.element(instanceIndex).assign(result);

Here it is clear that every element of the outputBuffer should be set to 1*2=2, the initial value of variable foo.

However, it gets transpiled to the following wgsl code:

// #--- TSL debug - compute shader ---#
nodeVar0 = 0.0;
if ( ( instanceIndex == 0u ) ) {
	nodeVar0 = ( 1.0 * 2.0 );
} else {
	if ( ( instanceIndex == 1u ) ) {
		nodeVar1 = ( 1.0 * 2.0 );
		nodeVar0 = nodeVar1;
	} else {
		nodeVar0 = nodeVar1;
	}
}
NodeBuffer_529.value[ instanceIndex ] = nodeVar0;

Where nodeVar0 gets set to an uninitialized nodeVar1 for every instanceIndex except 0 and 1.

This breaks for example the mx_hsvtorgb() function in the current release r176.

See the attached jsfiddle for a live example.

Live example

Screenshots

No response

Version

r176-dev

@mrdoob mrdoob added the TSL Three.js Shading Language label May 3, 2025
@Oxynt
Copy link

Oxynt commented May 9, 2025

Having the same issue, I investigated my own code and can add further tiny detail:
it happen only when same variable names ("foo" in the exemple provided ) are used trough both conditions.
when variables names+references are different, the issue doesn't appear ( but different name referencing same variable will fail).

So the current "bandaid" is to duplicate variable/operation before the condition ( break referencing ).
then merge/replace them after the whole if-else evaluation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TSL Three.js Shading Language
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants