Jump to content
  • Advertisement
Sign in to follow this  
Happy SDE

Explicit suffix “.f” for floats in HLSL.

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!