How to handle java Time from shader?

I want to make animation with shaders, so I did:

material.setFloat("JavaTime",(float)System.currentTimeMillis());

Then I’d like to confront the JavaTime with the

uniform float g_Time;

But I don’t understand how to confront these two values.

float val = g_Time - m_JavaTime;
if (val < 10.0) {

doesn’t give me the expected result…

Why not just using the tpf value of jme?
just do mytime += tpf and done you have a incrementing float since start of applications.

But I need it only after a certain event occurs… that is why I’m sending a timestamp to the shader.

use a control.
Have a control stack tpf in a variable float time.
Then pass time to the shader as a material parameter.

mat.setFloat("MyTime", time);

then in the shader instead of g_Time decalre an uniform float m_MyTime and use it in place of g_Time.

Just enable the control on the geometry when you want the animation to start and make sure time is reset to 0 when the control is enabled.

Don’t use if() in a shader, branching in shaders is very very slow and unefficient.

1 Like

Is it a good practice to pass parameters to shader for each update cycle or am I slowing things down?

Look for example to any shadow renderer. For every cycle all parameters are updated. So don’t worry.

1 Like

To avoid branching in shader, there are a lot of “bitwise hacks” you can use. Maybe someone help you.

As a note, gpus are not that good when it comes to integer math, they are highly specialized for floats…
At least on the not modern gpus this was an issue

1 Like

I also noticed that modern GPUs handles branching very well. I tested two versions of my shader, one full of if(), the other one with almost all of them replaced by ‘math’ ( Avoiding Shader Conditionals ). On two test scenes (~180fps and ~120fps) I did not noticed any significant difference, all of them was within an error, because it was an 2-3 fps difference for 180fps.
Graphics card I used: GTX 750 Ti

I think this is true only for very modern GPUs, when they do nice optimizations, such removing some simple branchings… To mobile devices, we still need avoid branching.

Also nice article, bookmarked.

There’s nothing wrong with using conditionals per-se… But specifically using them to avoid doing expensive calculations doesn’t work, e.g.:

if (needToDoIt) {
    doSomethingReallyComplicated();
}

This doesn’t actually save you from doing the complicated computation, just makes the side effects from it not visible. So in most cases you won’t see any performance difference with or without the if statement because the computation has to be done in either case.

Yes, I know that. As far as I remember it is because all shading units share the same program counter and must execute exactly the same way.

I’ll study again about GPU Units, because this sounds really strange to me - this probably is a gap of knowledge of mine.