Aeva / m.grl

Midnight Graphics & Recreation Library
http://mgrl.midnightsisters.org
GNU Lesser General Public License v3.0
44 stars 3 forks source link

provide better time precision to shaders #256

Closed Aeva closed 7 years ago

Aeva commented 7 years ago

Currently m.grl provides the float mgrl_frame_start uniform, who's value is performance.now()/1000.0. Supposedly performance.now() provides accuracy up to 5 microseconds (expressed as milliseconds).

Converting seconds and then dumping it into a float is a fast way to loose precision, and makes mgrl_frame_start not as useful for animation. The reason for this, is that as a float's magnitude increases, the fractional part of the float becomes choppy.

It is tempting instead to provide instead just the whole number of milliseconds as an integer instead, and leave it as an exercise for the shaders to extract the relevant fractional component out of that.

It might also be useful to provide the number of seconds as an integer, and the fractional component as a separate floating point.

So, for this ticket:

Some things to consider:

A possible approach:

  1. send int seconds + float fraction of seconds to shader
  2. produce texture for seconds and seconds+1
  3. lerp results with fraction of seconds as alpha value
Aeva commented 7 years ago

With some testing, it looks like the glsl sin function drops a lot of precision. Without doing anything fancy, it looks like a highp float is fine for a day of precision for time if I do something like this first:

  float speed = 1.3;
  float two_pies = 6.283185307179586;
  float time = fract(mgrl_frame_start*speed/two_pies)*two_pies;
  float wave = sin((dist - time) * frequency);

So, nothing really to do here.