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

Open
holtsetio opened this issue May 3, 2025 · 0 comments
Open

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

holtsetio opened this issue May 3, 2025 · 0 comments
Labels
Needs Investigation TSL Three.js Shading Language

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation TSL Three.js Shading Language
Projects
None yet
Development

No branches or pull requests

3 participants