Should I avoid creating multiple variables when programming a shader?

Now I'm starting to learn shaders (HLSL, GLSL), and I saw a lot of tutorials in which not many variables were created, and this made reading difficult. I was wondering if creating new variables affects shader performance.

So for example (in CG):

it

    inline float alphaForPos(float4 pos, float4 nearVertex){
            return (1.0/_FogMaxRadius)*clamp(_FogRadius - max(length(pos.z - nearVertex.z),length(pos.x - nearVertex.x)), 0.0, _FogRadius)/_FogRadius;
    }

Faster than that?

    inline float alphaForPos(float4 pos, float4 nearVertex){
        float distX = length(pos.x - nearVertex.x);
        float distZ = length(pos.z - nearVertex.z);
        float alpha = 0.0;
        alpha = _FogRadius - max(distZ,distX);
        alpha = clamp(alpha, 0.0, _FogRadius);
            return (1.0/_FogMaxRadius)*alpha/_FogRadius;
    }
+4
source share
1 answer

This will not affect performance.

At the end of the day, everything is embedded, and the optimizing part of the compiler will not take care of individual variables at the end of time optimization.

I put together two simple pixel shaders that call these two functions, and compile up to 9 commands in HLSL.

, ( :)).

+5

All Articles