OpenGL texture coordinates and accuracy of small floats

I use float to indicate texture coordinates in the range 0-1. OpenGL loves things in this range, and I pinpoint coordinates that way, but it bothers me when I start using large textures (say, 4096 or 8192 pixels) that I can start to lose accuracy. For example, if I want to specify the coordinate (1,1) in the texture 8192x8192px, which will be displayed at 1/8192=0.0001220703125 . This looks like 0.000122070313 as floating though ... I am concerned that my OpenGL shader will not match this with the same pixel that I intended.

I could store the coordinates as integers in pixels for a while, but sooner or later I need to convert it (maybe already in the shader itself). Is there a workaround for this, or is this something I should even worry about?


Multiplying it back, I get 1.000000004096 , which, I think, will still be interpreted as 1 ? In fact, OpenGL does the mixing if its not an integer, right? Perhaps not with the “closest neighbor”, but with the “linear” one, it should be.

 1/4096f * 4096 = 1, error = 0 1/8192f * 8192 = 1.000000004096, error = 0.000000004096 1/16384f * 16384 = 1.0000000008192, error = 0.0000000008192 1/32768f * 32768 = 0.9999999991808, error = 0.0000000008192 ... 1/1048576f * 1048576 = 0.9999999827968, error = 0.0000000172032 

(I use the Visual Studio debugger to calculate the float, and then multiplying it back by the calculator)

The lesson here is that the error is negligible for any texture with a reasonable size?

+4
source share
1 answer

It seems like this is rated 0.000122070313 as floating though ... I am concerned that my OpenGL shader will not match this with the same pixel that I intended.

You do not have to worry. A floating point is called a floating point because the comma is floating. You get ~ 7 digits of accuracy for your mantissa, more or less regardless of how big or small the float is.

The float is not saved as 0.000122070313; it is stored as 1.22070313x10 ^ -4. Mantissa - 1.22070313, the indicator is -4. If the score were -8 instead, you would have the same accuracy.

Your single-precision floating-point exponent can go to + or - ~ 38. That is, you can have 38 zeros between the decimal and the first non-zero digit of the mantissa.

So no, you should not worry.

The only thing that should concern you is the accuracy of the interpolated value and the accuracy of the texture extraction. But they have nothing to do with the accuracy of the data in which your texture coordinates are stored.

+3
source

Source: https://habr.com/ru/post/1396334/


All Articles