Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Vertices/TexCoord/Normals in float or double?  (Read 3768 times)
0 Members and 1 Guest are viewing this topic.
Online Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 6 years


Java guru wanabee


« Posted 2011-04-04 13:29:09 »

Hi gurus,

I am wondering what is better for the cpu/gpu/heap between the following:
1. Casting all my doubles/ints to floats when adding them to the vbo's/glvertex3f(d).
2. Using the double version in LWJGL.

My gut tells me that using the double version is more work for the gpu while throwing them to floats is more work for the cpu, but that's just based on the knowledge that gpu's uses floats.

Kind regards,
Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2011-04-04 14:25:25 »

The GPU converts doubles to floats, so sending doubles to the GPU is pure waste of time.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Spasi
« Reply #2 - Posted 2011-04-04 15:00:31 »

It depends on which double version you use.

Using glVertex3d or glVertexPointer (with or without VBOs) is indeed a waste of time, all double values are converted to floats, by the driver. So it's always more work for the CPU, the GPU only works with floats.

By using the functions found in ARB/EXT_vertex_attrib_64bit or GL41 you can properly submit 64bit data to the GPU, but that obviously requires high-end hardware (DX11 level).

Why do you need doubles anyway?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 6 years


Java guru wanabee


« Reply #3 - Posted 2011-04-04 15:33:41 »

It depends on which double version you use.

Using glVertex3d or glVertexPointer (with or without VBOs) is indeed a waste of time, all double values are converted to floats, by the driver. So it's always more work for the CPU, the GPU only works with floats.

By using the functions found in ARB/EXT_vertex_attrib_64bit or GL41 you can properly submit 64bit data to the GPU, but that obviously requires high-end hardware (DX11 level).

Why do you need doubles anyway?

Okay, so if it is the driver that converts it, does it matter if I leave it up to the driver to "cast" it or if I do it in java?

I thought that double's are more reliable and will keep the exact number, while floats can return incorrect numbers when doing multiplications/division? If that isn't the case, then I'll move over from doubles to floats.

Kind regards,
Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Roquen
« Reply #4 - Posted 2011-04-04 15:44:48 »

Doubles have a wider dynamic range (more exponent bits) and a higher precision (more mantissa bits).  From the perspective of the abstract concept of Reals in mathematics, neither is more nor less correct than the other.  Which is "better" to use is hard to describe without going into numerical analysis and an exact understanding of how they work.  It is a common misconception that using doubles somehow magically makes results better...this is simply not the case.  I'm sure this is about as clear as mud. 
Online Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 6 years


Java guru wanabee


« Reply #5 - Posted 2011-04-04 16:24:09 »

Doubles have a wider dynamic range (more exponent bits) and a higher precision (more mantissa bits).  From the perspective of the abstract concept of Reals in mathematics, neither is more nor less correct than the other.  Which is "better" to use is hard to describe without going into numerical analysis and an exact understanding of how they work.  It is a common misconception that using doubles somehow magically makes results better...this is simply not the case.  I'm sure this is about as clear as mud.  

Oh, if that is the case then I'll move over my doubles, where don't require more than 6-7 positions of accuracy, to floats. Will save some casting Smiley

Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Scarzzurs
« Reply #6 - Posted 2011-04-04 18:42:57 »

Be careful, this sounds like premature optimization?

Also, I believe I once read that java converts floats to doubles internally for computation, unless specifically told not to. (Could someone confirm this?)
If this is indeed the case, you are better off using higher precision for game logic, and casting only once, when sending it to the GPU...

- Scarzzurs

My games and Projects:
BlastingPixels.com,
Old website
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2011-04-04 20:09:34 »

Also, I believe I once read that java converts floats to doubles internally for computation, unless specifically told not to. (Could someone confirm this?)

Almost right.

The CPU calculates the operations on both floasts and doubles in 80 bits, and convert them back to 32bits (floats) and 64 bits (double).


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline lhkbob

JGO Knight


Medals: 32



« Reply #8 - Posted 2011-04-04 21:08:51 »

Be careful, this sounds like premature optimization?

This might be premature optimization but sending doubles is pretty much unneeded. For 99% of projects, a float does the job, and then smart use of local coordinate systems means that the other 1% can still use floats.  And when you use floats, you halve the amount of data you send to the graphics card.  It's not so much the CPU time spent in processing doubles/floats but in transferring them.  Even if the driver will cast the double to a float, because this is Java, we transfer the doubles across the JNI bridge and that adds up.

Offline JL235

JGO Coder


Medals: 10



« Reply #9 - Posted 2011-04-04 22:31:46 »

Some CPUs actually work faster on doubles. I'd expect this is true in some modern x86 chips, but the only place I've heard this definitively is that you are recommended by MS to use doubles over floats on the 360 as it's faster (and probably true for the PPE in the Cell processor too, which is the core that does all the non-specialist work in the PS3).

You'd be hard pressed to find a big performance difference between float and double on x86. But if you can find a situation where the difference between floats and doubles naturally gives you a big difference in performance (emphasis here on it happening naturally within your program, not through benchmarks), then the algorithm is run so often that it would be better to think up a pure integer alternative.

You'll probably also find it difficult to find situations in a game where floats are not accurate enough. The main situation I personally run into with doubles vs float is when your mixing very large with very small numbers togethor. In a simplified sense, floats store a number and a location of the decimal point in relation to that number. As a result a very large number will not store small numbers such as decimal values; even adding 1 onto a very large float results in no change to that number. As doubles use more bits for greater accuracy you can mix a larger range of large and small numbers (and so be less likely to run into this issue). However again you'd be hard pressed to find a situation where this actually happens naturally in a game.

So in short, don't worry about it!

But if you _HAVE_ to pick one based on performance, then pick floats. I once wrote a 32 bit random class which used integers and floats. I then ported it to use longs and doubles. The 64-bit version was _MUCH_ slower, but this was shown using artificial benchmarks which did not represent real world usage (polling for millions of random values in a row, which is rarely performed on each frame).

Otherwise pick whatever the majority of your libraries use. If they mostly take/return floats, then use floats. If they mostly return/take doubles, then pick doubles.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 6 years


Java guru wanabee


« Reply #10 - Posted 2011-04-04 22:52:32 »

Otherwise pick whatever the majority of your libraries use. If they mostly take/return floats, then use floats. If they mostly return/take doubles, then pick doubles.

Thanks to all who answered, it was very insightful and I wrote everything myself so I have the luxury to choose Smiley I'll use floats then for things that needs to be sent to opengl, sounds reasonable.

And regarding premature optimization, oh yes! I'm doing the dumbest thing anyone can do as a first java program/game, an mmo, and instead of spending time with features I'm polishing the engine as much as I can. It sucks being a perfectionist when working alone on a big project Smiley

Kind regards,
Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline lhkbob

JGO Knight


Medals: 32



« Reply #11 - Posted 2011-04-05 01:42:16 »

Don't worry about it, I've been working on a graphics engine for at least 5 years now at the expense of finishing a game.  At this point, I've realized that I find it as much fun to make an engine as it is to make a game.  Just do what you enjoy and that's what counts.

Offline Roquen
« Reply #12 - Posted 2011-04-05 13:00:17 »

Quote
The CPU calculates the operations on both floasts and doubles in 80 bits, and convert them back to 32bits (floats) and 64 bits (double).

I'm pretty sure that this has been changed and that HotSpot now sets the cpuflags (This is an intel-a-like specific issue, BTW) to limit computations to 64-bits.  Additionally newer versions use the scalar versions of SSE instructions, which will be limited to precision of the specified type.  This means that a specific computation will not be bit exact from hardware-to-hardware, nor even from HotSpot version-to-version on the same box.  Floating point is fun!
Offline gouessej
« Reply #13 - Posted 2011-04-14 10:33:29 »

Don't worry about it, I've been working on a graphics engine for at least 5 years now at the expense of finishing a game.  At this point, I've realized that I find it as much fun to make an engine as it is to make a game.  Just do what you enjoy and that's what counts.
FreeLookCameraInputManager.installTriggers() should not be private on my view... Sorry for the offtopic.

Offline lhkbob

JGO Knight


Medals: 32



« Reply #14 - Posted 2011-04-14 13:42:35 »

Don't worry about it, I've been working on a graphics engine for at least 5 years now at the expense of finishing a game.  At this point, I've realized that I find it as much fun to make an engine as it is to make a game.  Just do what you enjoy and that's what counts.
FreeLookCameraInputManager.installTriggers() should not be private on my view... Sorry for the offtopic.

That is off topic Smiley You're probably right though, and I need to rewrite that whole bit anyway

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!