Phyronnaz / HLSLMaterial

HLSL Material for Unreal Engine
MIT License
490 stars 67 forks source link

Including Operations Only Possible in Custom Nodes Causes the Entire Function To Be Inside A Custom Node #1

Open Bartalon opened 3 years ago

Bartalon commented 3 years ago

Ideally this plugin would be able to differentiate between content that needs to definitely be part of a Custom node and everything else in order to take advantage of constant folding.

Function code:

void testFunc(float3 color, float intensity, out float3 diffuse, out float x) {

    float val = 0.1;
    int i = 0;
    while ( i <= 10 ) {
        diffuse.x = (val * (float)i);
        i += 1;
    }

    x = intensity * 0.5;
}
Phyronnaz commented 3 years ago

Thanks for the feedback!

The plugin is indeed pretty simple & doesn't translate the actual HLSL into a shader graph. Doing so would require parsing the HLSL code itself, which would be way out of the scope of the plugin.

Out of curiosity, do you have examples where constant folding really improves performance? I've always been curious to see how important is really is.

Bartalon commented 3 years ago

No problem, I figured it might be out of scope. One day perhaps :)

I'm not a programmer, but constant folding seems to apply to code compiling in general. From what I understand, something like 2 * 10 * 12 would be constant-folded to become just 240 without any multiply operations. A Custom node in the Material Editor (according to Epic) would not benefit from this and instead would have each multiplication operation as part of the compiled shader code.

https://en.wikipedia.org/wiki/Constant_folding

Phyronnaz commented 3 years ago

Gotcha :)

Actually, "classic" constant folding is already done by the shader compiler - if you have 2 * 10 * 12 the bytecode will just use 240.

The unreal constant folding is to apply that to "constant" parameters that aren't known at the shader compile time. Eg, if you have sin(MyParameter) unreal can precompute the sin when you set MyParameter, only doing the operation once instead of once per pixel per frame.

As far as I can tell this can be useful in some very specific scenarios, but in the vast majority of cases it shouldn't matter much if at all.

Bartalon commented 3 years ago

Oh cool, thanks for the extra info! This makes sense.

It would be ideal to have that separation with this plug-in if only just to maintain team-accessible graphs. But as you said, out of scope for the time being. :)