You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description
Float literals seems to be used as doubles in intermediate operations when using ternary operators.
This makes the shaders require double operations which is usually unexpected.
I'm unsure if this behavior is intentional?
Moving forward, we have added a bit of code on our end to check the shader feature flags to see if D3D_SHADER_REQUIRES_DOUBLES is set, and if so consider compilation failed.
Hi @simco, this is the expected behavior. Today HLSL gets to this result through a bit of a roundabout way because of the HLSL literal type rules (we have an issue tracking removing literal types: microsoft/hlsl-specs#73).
literal types default to "the biggest type available" (in this case double), unless an implicit or explicit cast is contained within the expression. In your example the rule applies in a rather pedantic sense. Your ternary operator results are all literal float, so there is no implicit or explicit cast within the expressions... but the expression result is implicitly casted to float. This results in the worst of all worlds behavior where the type for each of the subexpressions becomes double even though it just gets casted to float in the final result. Then it becomes up to the optimizer whether or not it is smart enough to optimize it away (spoiler alert: it isn't).
A really silly illustration of how this plays out in practice can be seen in this silly modification of your example:
You'll notice I made just one of your arguments an explicitly-sized float. this results in the first sub-ternary operator being float typed, which forces all the literal float values to resolve as float instead of double.
Regardless of the literal types the resolution here would be unchanged, because under C/C++ rules, non-suffixed floating point literals are double. For this reason, the best advice I can give is: always explicitly suffix literals.
Description
Float literals seems to be used as doubles in intermediate operations when using ternary operators.
This makes the shaders require double operations which is usually unexpected.
I'm unsure if this behavior is intentional?
Steps to Reproduce
Below a simplified example:
dxc.exe -T cs_6_6 -E CSMain
Godbolt link: https://godbolt.org/z/YdojbWcE3
Moving forward, we have added a bit of code on our end to check the shader feature flags to see if
D3D_SHADER_REQUIRES_DOUBLES
is set, and if so consider compilation failed.Environment
Using either DXC C++ API or CLI
The text was updated successfully, but these errors were encountered: