I want to simulate large scale areas, possibly more than 10x10 km and retain precision to within centimeters. How do I pass along doubles instead of floats to the shaders and specify vec3's/vec4's to be double-precision? Will this cause a significant slow-down?

**1**

# GLSL double precision

###
#2
Moderators - Reputation: **7796**

Posted 02 August 2013 - 03:38 PM

Just so you're not doing this for no reason: the 24 bits of precision in a standard float roughly corresponds to one centimeter at approximately 160 kilometers. If by "possibly" more than 10 kilometer you mean definitely less than 160 kilometers, then you already have enough precision in the standard float.

###
#4
Moderators - Reputation: **6148**

Posted 02 August 2013 - 03:53 PM

To answer the second part of the question, it depends on the exact card. On many NVIDIA cards double precision will *dramatically* hurt performance. A lot of cards won't support it at all. Ideally you'll want to run a Quadro or FireGL GPU if you're doing doubles. It is not really a mass market possibility.

Will highp be 24-bit precision on all video cards?

highp is 32 bit precision on any hardware I've seen.

**Edited by Promit, 02 August 2013 - 03:55 PM.**

###
#5
Crossbones+ - Reputation: **1354**

Posted 02 August 2013 - 05:26 PM

If you want *double* precision, you have to use doubles inside GLSL, which is not available everywhere, and is far more slow than using single precision floats ... just to note, AMDs work also with doubles, but as NVidia, it will hurt performance. There are imho three main ways to get out of the "precision hell" with large scale worlds:

1.) Your world has to be divided in cells, you have each cell and items on it in relative coordinates to the cell (each cell originates at (0, 0, 0)). Your active cell (e.g. the one where camera is) is always at origin (0,0,0), once camera moves over the edge, you *move* all the cells and camera, so that new active cell is again at origin (0,0,0). This gives you good precision near camera (e.g. on current cell), and worse precision on very distant cells (which doesn't matter that much, as details should be lower there).

2.) Use higher precision. Okay but this isn't actually solution, it just moves your problem further. This one is in my opinion the worse way to walk.

3.) Use (i know you don't wanna hear it) fixed point numbers. But this shouldn't be used for graphics, as GPUs are faster for floating point computations. You should use fixed point for game logic and physics (if high precision or F.e. network communication is needed), but for graphics, I'd use solution 1 (which I'm using for large scale worlds right now).

My current blog on programming, linux and stuff - http://gameprogrammerdiary.blogspot.com

###
#6
Members - Reputation: **178**

Posted 02 August 2013 - 06:54 PM

I thought float is 32 bit number with 32 bit precision, anyway the problem with floats is that if you have number 0.1456 (in double ) the float is 0.145612738912738914689461249641142e-40

anyway i think casting should do the trick

long double something; float crap; crap = (float)something;

Ah and if you manage to make "1" number as 1 meter you have 0.001 as one centimeter and 160000.0 as 160 km, not a big deal

since i have something like this in my code:

931322574615478515625.0f 0.000000000931322574615478515625f const float imopi = 0.017453292519943295769236907684886; const float pi = 3.1415926535897932384626433832795; const float infinity = 9999999999.90; const float sinfinity = 1000.0f; //1 kilometer (1000 meters) const float pi2 = pi / 2.0f; const float SQUARE_ROOT_2 = 1.4142135623730950488016887242097; const float SPECIAL_FEAUTRE = 0.00000001; //wtf ;u const float epsilon = 0.01; const float epsilona = 0.0001; //movement epsilon close to zero when using rotations const float EPSILON = 0.000001;

etc....

**Edited by ___, 02 August 2013 - 06:58 PM.**

Master & Mentor

https://sites.google.com/site/customprog/

###
#7
Moderators - Reputation: **7796**

Posted 02 August 2013 - 07:20 PM

I thought float is 32 bit number with 32 bit precision, [...]

A floating point number has two significant parts: the mantissa which holds the precision and the exponent which holds the scale of the mantissa. A standard float has 32 bits in total, divided into 23 bits for the mantissa, and 8 bits for the exponent. The mantissa has an implicit leading 1-valued bit, thus leaving you with 24 effective bits of precision. The reason a 32 bit float does not have 32 bits of precision is because some of those bits are dedicated for the floating binary point, which is the whole point on the floating point type in the first place. A 32 bit float has less relative precision than a 32 bit integer, but instead has greater dynamic range.

###
#8
Members - Reputation: **772**

Posted 03 August 2013 - 02:42 AM

First, I have to pay tribute to Brother Bob, and to his endless patience in answering to some trivial questions. Although I'm also involved in teaching, I'm far less tolerant to something that have to be basic knowledge in computer science (like binary numbers representation - a secondary school knowledge). My respect!

Let's back to topic... The performance drop caused by double precision is overestimated in this thread. Generally speaking, it is always better to use SP instead of DP if it is possible, but the impact of DP calculation could be well masked by compiler's optimization. On the other hand, when we are talking about GPUs, we are talking about very massive parallelism using architecture with very deep pipeline. If there is no stalls, no dependencies and a compiler is perfectly done its job, the execution time is 1 clock for each instruction. Also, different GPUs have different architecture. For example, Fermi uses two SP units to perform DP operation, while Kepler has separate DP units (the number varies from series to series). So, it is very hard to predict performance.

The major problems with DP calculation are: they are not widely supported and transcendental functions are always calculated with SP. The second problem is very serious, since even on the mighty graphics cards GLSL cannot calculate trigonometry accurately. CUDA and OpenCL overcome the problem by software emulation and the price is longer execution time. HW implementation enables transcendental function execution in a single clock (but only in single precision).

It is worth to mention (since somebody asked if SP is guaranteed) that all new graphics cards should be fully IEEE754 complaint. I can firmly claim that for NV cards since G80 (which means in last 7 years). But older cards are not. So be careful if you have to deal with older cards.

SP is not adequate for many purposes (precision of just 6-7 decimal digits), but it is almost certainly enough for visualization. My recommendation is to perform all precision-critical calculation on CPU using DP, but downcast values to SP before sending to GPU. After tons of tricks and optimizations, I have succeeded to visualize Earth's surface with precision of 1e-6m with just SP calculation in GPU. It is a very precise scientific visualization using WGS84 ellipsoid. So, if it works for me for the terrain at least three orders of magnitude greater than yours, there is no doubt it can works for you too.