• 10
• 12
• 12
• 14
• 17

Explicit suffix “.f” for floats in HLSL.

This topic is 839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Looking at some HLSL tutorials I often see explicit float constant declaration.

Is there a difference between these 2 for modern hardware:

OUT.position = mul( ModelViewProjection, float4( IN.position, 1.0f ) );
OUT.position = mul( ModelViewProjection, float4( IN.position, 1 ) );


I don’t see any warnings from HLSL compiler after second string.

Is the explicit definition just automatically attempt to use C++ construction inside HLSL?

Maybe there is some meaning lies on first string, which I am not aware of.

Any ideas?

Share on other sites
There basically is no difference (see below reply).
A “difference” is that if you ever ported your code to ESSL it would error on the floating-point number.
I don’t use the “f” suffix, and if you use it in my language it will be converted to HLSL/GLSL/ESSL without it.

L. Spiro Edited by L. Spiro

Share on other sites
You should generally avoid the .f in HLSL. It explicitly tells the compiler to truncate your constant to 32bit precision. Leaving it off asks the compiler to only truncate if necessary (which usually has the exact same effect, but in the cases of long chains of literals -- e.g. multiplying other literals by Pi -- it's detrimental to truncate *before* the multiply)

Share on other sites
It's the same as C++: the "f" suffix specifies that it's a floating point literal. If you leave it off you will get an integer, which means you will get an implicit conversion to float in cases like the second line of your code snippet. It's essentially the same as doing this:

OUT.position = mul( ModelViewProjection, float4( IN.position, (float)1 ) );

Using the "f" suffix is more explicit: it tells the compiler that you want your value to be a float, and therefore no implicit conversion is necessary. In most cases there will probably be no difference in the generated code, it's more a matter of style and personal preference. However you should be careful when mixing floats with doubles, since in some cases that will give you different results, and can potentially result in more expensive double-precision ops being used by the GPU.

Share on other sites

I decided to make an experiment =)

I have a vertex shader, where I found occurrences of “0.f”, “0.75f”, and “1.f” 10 times.

I compiled it with this way and the simple way: “0”, “0.75”, “1” (in VS 2015).

After that I've diff-ed disassembly.

In Release mode there is no difference at all.

In Debug mode there is one instruction more:

float4 L = float4(light.posVS, 1.f) - P;

000000D7  mov r14.xyzw, -r14.xyzw

000000D8  add r10.xyz, r10.xyzx, r14.xyzx

000000D9  add r10.w, r14.w, l(1.000000)

float4 L = float4(light.posVS, 1) - P;

000000DB  itof r10.w, l(1)

000000DC  mov r14.xyzw, -r14.xyzw

000000DD  add r16.xyz, r10.xyzx, r14.xyzx

000000DE  add r16.w, r10.w, r14.w

So I decided to keep using the simplest construction: 0, 0.75 and 1.

Thank you all for answers!

Edited by Happy SDE