Integers for logic, floats for graphics,
Members - Reputation: 263
Posted 18 May 2011 - 07:40 AM
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?
Moderators - Reputation: 48936
Posted 18 May 2011 - 08:07 AM
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 (or didn't even support floating point at all) lots of games used fixed-point math (where you use an integer to represent fractional amounts). These days, there's no huge performance penalty for float any more, so if a value can be in-between whole numbers (like a position moving naturally through space), then floats make perfect sense.
On x86, converting between float/int is quick, but on a PowerPC (like in game consoles) it is actually rediculously slow. So much so that if you're doing mixed logic + math (where the math has to use floats), it often makes sense to also do the logic using floats.
e.g. on a PPC, instead of:
bool conditionA = myFloat >= 12.0f; bool conditionB = myFloat < 24.0f; bool conditionC = conditionA && conditionB; float answer = conditionC ? foo : bar;You might use:
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)But if the data your working with was integer data (e.g. myInt as input, and int answer as output) then you'd write it as you normally would. This is just to avoid the PPC's costly float/int conversions.