Important thing related to shaders and timer

Hi.

I am using a shader to animate the texture. Currently the animation is a simple scrolling. Obviously I’m using CC_Time uniform variable to make things happen. But currently (version 2.1.4) Cocos2d has a problem with CC_Time’s precision. It appears that after quite a short peroid of time, about 10000 frames which is about 2.7 minutes, the precision of mediump float drops significantly. Thus modifying the uv-coordinates of the texture which belongs to [0.0; 1.0] becomes noticeably jerky.
Solution is quite simple (however I’m uncertain about performance). CC_Time should have a highp precision qualifier. I have tested this solution for about an hour and it seems the animation stayed smooth.
Another possible solution is to pass truncated values from the C++ code to the shader’s uniform variable. Smaller floats have higher precision and vice versa (that’s why they are called ‘floats’). Although this solution may be not all-purpose since we don’t know what range of values will be required by client’s shader.

It would be great if someone of authors or contributors comment this situation.

Hmm… it seems it’s possible to solve this problem without changing the source code of Cocos2d. The workaround is to use a varying highp float variable in the vertex shader and pass it to the fragment shader. So it looks as follows:

vsh:

...
some_usual_code
...
varying highp float scroll_speed;

void main()
{
...
more_usual_code
...
scroll_speed = fract(0.1 * CC_Time.g);
}

fsh:

varying mediump float scroll_speed;
void main()
{
...
use of scroll_speed
...
}

Notice that now we can use mediump precision qualifier in the fragment shader.

Is nobody interested or is it an obvious thing I shouldn’t even mention?

your solution seems to be good.

Do you know if it is possible to set a precision for certain uniform ? In particular, is it possible to do uniform highp vec4 CC_Time ?

Ricardo Quesada wrote:

your solution seems to be good.
>
Do you know if it is possible to set a precision for certain uniform ? In particular, is it possible to do uniform highp vec4 CC_Time ?

Yes, it’s possible. And this method is described in my first message:

Solution is quite simple (however I’m uncertain about performance). CC_Time should have a highp precision qualifier. I have tested this solution for about an hour and it seems the animation stayed smooth.

Personally I prefer the solution described in my second message, since it’s less expensive: it’s better to have highp in the vertex shader, because vertex shader is called by GPU thousands times rarer than the fragment one. Although maybe this performance saving is so insignificant that it’s not even worth to bother with it.
The drawback of this method is that it’s hard to make it all-purpose: its idea is that I shorten the range of values in the vertex shader:

scroll_speed = fract(0.1 * CC_Time.g);