Get time in a Fragment Shader. Built-in Variables in jxs file.

andrea@nbl7.net's icon

Hello everyone!

I am new to Fragment Shader and I would like to know how to get the time variable inside my GLSL code.

I've studied all the Book of Shaders (https://thebookofshaders.com) but it seems that the name of the variables when I load a jxs file in Jitter are different than the default.

Also other functions, like "distance", are not working.

Do you know where can I find all the variable and function names for Jitter?

Thank you so much!

A.

diablodale's icon

Shaders in Max 7 and earlier are yet stuck in the 11 year old GLSL 1.2. :-( Therefore, you can use the official documentation of GLSL 1.2 which is on the Internet.

You could get the time in your Max patch. For example, get the time as a Unix epoc which is a 32 (legacy) integer. Can't use double or 64-bit integer because of...11 years old. Then pass that value as a shader parameter and bind it to a GLSL uniform variable. https://docs.cycling74.com/max7/tutorials/jitterchapter99_appendixc

On some OSs and GPU drivers, you can force a higher GLSL level to get some modern language features and functions using the " #extension" or " #version " mechanism in the shader code. This usually only works on Windows and a subset of GPUs.

Your specific usage of the the distance function may not have been supported until GLSL 4.0.
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/distance.xhtml
You'll need to do the algorithm yourself if the GLSL 1.2 version doesn't meet your needs.

I recommend looking at the Max tutorials and examples for shaders. The GLSL code is scattered in file and patches. Many of the GLSL files are at C:\Program Files\Cycling '74\Max 7\resources\media\jitter\shaders

Rob Ramirez's icon

for a time parameter (and many other super useful tools), i strongly recommend you check out the jit.mo package from the Package Manager. there is an object called jit.time built exactly for this purpose.

in order to use the parameter in your shader, you have to create a uniform variable, and bind to the parameter using the jxs xml tags. something like:

<param name="time" type="float" default="0.0" />

<bind param="time" program="fp" />

uniform float time;

andrea@nbl7.net's icon

Thanks Diablodale, Rob,

This was very helpful. I am still not sure if it is the right idea to use shader instead of normal jitter objects.

I want to make some visuals that are run by some input audio and I don't want to use too much CPU.

But actually sometimes i got the video flickering and not fluid even though I am using a shader.

Thanks again

A-

andrea@nbl7.net's icon

@rob ramirez Is it normal that jit.mo.time take more than 20% of my CPU?

Isn't it too much?

Also I am using only a Shader with jit.gl.slab in Max and my CPU is at 60%.

Wasn't the Shader a way for not consuming CPU but GPU instead?

Does anyone know why?

shader.maxpat
Max Patch

Rob Ramirez's icon

the texture readback to CPU is more than likely what's causing your heavy usage. either get rid of that jit.matrix and jit.pwindow and replace with a gl shared context preview, or simply put a gate between the gl.slab and jit.matrix, and only open it for testing. for more info, search the Max reference window for an article called "GL Texture Output".

andrea@nbl7.net's icon

Thank you, Rob.