In glsl
, we had:
-sin(angle)
In tsl
was wondering what would be the most eloquent way to write please ? Anyone know what would be the most optimized one please ?
float(-1.).mul(sin(angle))
or
sin(angle).mul(-1)
or
negate(sin(angle))
Another one ?
TSL generates a tree of nodes, that is transformed into an AST (abstract syntax tree), which in turn is compiled into a shader language (WGSL or GLSL). This means that sometimes what appears to be uniquely optimal in TSL is not necessarily uniquely optimal as GLSL (or is not translated into GLSL at all).
You may experiment with the TSL Editor and check how TSL code is compiled into GLSL.
These three TSL instructions:
const val1 = float(-1.).mul(sin(angle)).toVar();
const val2 = sin(angle).mul(-1).toVar();
const val3 = negate(sin(angle)).toVar();
are translated into GLSL as:
nodeVar1 = ( -1.0 * sin( nodeVar2 ) );
nodeVar3 = ( sin( nodeVar2 ) * -1.0 );
nodeVar4 = ( - sin( nodeVar2 ) );
but I believe the GLSL compiler will (should!) generate the same code for all three cases.
PS. I added .toVar
in order to isolate the code, otherwise it gets embedded in expressions that use them.
6 Likes