Is Custom HLSL material node repeat running the code for each additional output?

When I use Custom HLSL node in Unreal Engine’s material blueprint, and add a include file it works fine in case there is only one output.

However, if I add additional output, it tells me there is a redefinition error.
It acts as if the content of the include file is repeated several times.

I am suspecting that Custom HLSL node is creating separate HLSL compilation for each output.

The error is not really a problem.
I can fix it by using ifndef macro.

But the problem is if what I am suspecting is right, it means the node repeatedly runs the same code with the same input for each output, even though all outputs can be computed at once.

My suspicion grew after reading bellow post.
https://www.exportgeometry.com/blog/defining-custom-material-expression-node
The post shows some strutcure of custom hlsl node class’s HLSL generation or compilation.

Do you think what I am suspecting is true?

If what I am suspecting is right, can anyone suggest a solution for this?

Or is avoiding to use multiple outputs on Custom HLSL node only solution?

Can you share any of the code / screenshots?

One quirk of the custom node is that the first output needs to be undefined within the code (because the output menu defines it) while secondary outputs need their definitions within the code.

So you’d want to have something like this…

output1 = (0,0,0);
float3 output2 = (1,1,1);

But if you try to write it as…

float3 output1 = (0,0,0);
float3 output2 = (1,1,1);

You will get an error because output 1 is defined outside of the code block using the drop-down menu, which will cause a redefinition error. This may be your problem.

I have looked into the generated HLSL code of the material.

It seems the redefinition error occurs because I have connected one custom node output to material position offset and another output to material normal.

It creates the same functions with different names from the custom node.

So material blueprint does not repeatedly run custom node as the number of outputs, it just creates the same content functions with different names and use them for different purposes.

GetMaterialCustomizedUVs function,
and GetMaterialPreviousWorldPositionOffsetRaw function in the generated HLSL code uses the same but different name function twice but with different inputs.

There are some other in the HLSL code that uses the custom node function just once.

So I think there is no unnecessary repeated execution of the custom node code as the number of output increases.

However as I have coupled Normal and Position offset via the custom node, the generated material HLSL code may run the whole function when it only needs to calculate normal or when it only needs to calculate position offset.

So I think it is better to avoid directly connecting material output with the custom node output.
It seems you should try to avoid a custom node being shared by multiple material output paths.