Sign in to follow this  

Integers for logic, floats for graphics

Recommended Posts


I'm aware that a GPU works with floats (although some newer cards I understand can work with integers). From what I've seen from sample code over the net and on these forums the game logic code also seems to use floats instead of integers, what I don't understand though is why? Is this a case of performance, or what? Is the performance hit from casting an int to a float to perform a matrix operation too great? Similarly, if I were to create a particle effect (for example sparks from a welder), then could these particles be tracked with integers?


Share this post

Link to post
Share on other sites
You could track particles with integers.... but what would be the benefit of doing so?

Can you give an example of a situation where you've seen someone use floats, but you thought they could've used integers instead?

Back when CPUs were really bad at floating point ([i]or didn't even support floating point at all[/i]) lots of games used fixed-point math ([i]where you use an integer to represent fractional amounts[/i]). These days, there's no huge performance penalty for float any more, so if a value can be in-between whole numbers ([i]like a position moving naturally through space[/i]), then floats make perfect sense.

On x86, converting between float/int is quick, but on a PowerPC ([i]like in game consoles[/i]) it is actually rediculously slow. So much so that if you're doing mixed logic + math ([i]where the math has to use floats[/i]), it often makes sense to also do the logic using floats.

e.g. on a PPC, instead of:[code]bool conditionA = myFloat >= 12.0f;
bool conditionB = myFloat < 24.0f;
bool conditionC = conditionA && conditionB;
float answer = conditionC ? foo : bar;[/code]You might use:[code]float conditionA = fstep(12.0f, myFloat);//same as (myFloat>=12.0f ? 1.0f : 0.0f)
float conditionB = fstep(myFloat, 24.0f);//same as (myFloat<24.0f ? 1.0f : 0.0f)
float conditionC = conditionA * conditionB;
float answer = fselect(conditionC-1, bar, foo);//same as (conditionC>=1.0f ? foo : bar)[/code]But if the data your working with was integer data (e.g. [font="Courier New"]myInt[/font] as input, and [font="Courier New"]int answer[/font] as output) then you'd write it as you normally would. This is just to avoid the PPC's costly float/int conversions.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this