Repository: mrdoob/three.js (JavaScript)

Issue:
TSL: Using debug() changes the compiled code
### Description

Using the debug() function can in some cases change the compiled wgsl code. See attached fiddle for an example.

The compute shader with the following tsl code:
```js
      const foo = float(1).toVar();
      const foo2 = float(2).toVar();
      const bar = foo.mul(2);
      foo.assign(bar);
      foo2.assign(bar);
      outputBuffer.element(0).assign(foo2);
```

compiles to the following wgsl code with debug():
```
      nodeVar0 = 1.0;
      nodeVar1 = 2.0;
      nodeVar0 = ( nodeVar0 * 2.0 );
      nodeVar1 = ( nodeVar0 * 2.0 );
      NodeBuffer_528.value[ 0u ] = nodeVar1;
```
 and to the following code without debug():
```
      nodeVar0 = 1.0;
      nodeVar1 = 2.0;
      nodeVar2 = ( nodeVar0 * 2.0 );
      nodeVar0 = nodeVar2;
      nodeVar1 = nodeVar2;
      NodeBuffer_528.value[ 0u ] = nodeVar1;
```

I think it has to do with the nodeBuilder not counting the tempNode's usageCount right in debug-mode, but I don't understand the nodeBuilder well enough to find the exact cause.

### Live example

* [jsfiddle-dev WebGPURenderer](https://jsfiddle.net/ch0jr1gm/2/)


### Screenshots

<img width="209" alt="Image" src="https://github.com/user-attachments/assets/0d92f003-2054-446f-b3d8-6a98fe3cc5cc" />

### Version

175-dev


Discussion: Looking further into it, the bug only exists for compute shaders, debug() works well with vertex and fragment shaders.

Also, the following change in DebugNode.js fixes the problem:

replace
```
setup( builder ) {
	return this.node.build( builder );
}
```

with:
```
setup( builder ) {
	this.node.build( builder );
	return this.node;
}
```

See a monkey patched demonstration here: https://jsfiddle.net/1yg9x0dn/

However I don't understand the nodeBuilder system well enough to feel confident to make a pull request out of this. Can someone with more insights double check?



Fix (patch):
diff --git a/src/nodes/core/Node.js b/src/nodes/core/Node.js
index 358fbcaacb11fb..171b640edfc96a 100644
--- a/src/nodes/core/Node.js
+++ b/src/nodes/core/Node.js
@@ -586,12 +586,14 @@ class Node extends EventDispatcher {
 	}
 
 	/**
-	 * This method performs the build of a node. The behavior of this method as well as its return value depend
-	 * on the current build stage (setup, analyze or generate).
+	 * This method performs the build of a node. The behavior and return value depend on the current build stage:
+	 * - **setup**: Prepares the node and its children for the build process. This process can also create new nodes. Returns the node itself or a variant.
+	 * - **analyze**: Analyzes the node hierarchy for optimizations in the code generation stage. Returns `null`.
+	 * - **generate**: Generates the shader code for the node. Returns the generated shader string.
 	 *
 	 * @param {NodeBuilder} builder - The current node builder.
-	 * @param {?string} output - Can be used to define the output type.
-	 * @return {?string} When this method is executed in the setup or analyze stage, `null` is returned. In the generate stage, the generated shader string.
+	 * @param {?string} [output=null] - Can be used to define the output type.
+	 * @return {Node|string|null} The result of the build process, depending on the build stage.
 	 */
 	build( builder, output = null ) {
 
@@ -657,6 +659,8 @@ class Node extends EventDispatcher {
 
 			}
 
+			result = properties.outputNode || null;
+
 		} else if ( buildStage === 'analyze' ) {
 
 			this.analyze( builder );
diff --git a/src/nodes/gpgpu/ComputeNode.js b/src/nodes/gpgpu/ComputeNode.js
index 8075dae395bbb7..ec64913b2e43e0 100644
--- a/src/nodes/gpgpu/ComputeNode.js
+++ b/src/nodes/gpgpu/ComputeNode.js
@@ -165,7 +165,7 @@ class ComputeNode extends Node {
 
 	setup( builder ) {
 
-		const result = this.computeNode.setup( builder );
+		const result = this.computeNode.build( builder );
 
 		if ( result ) {