jit.gl.shader — compilation vs. binding, and autowatch behavior

YaleO'neil's icon

I’m building a system for live performance where I may need many different shader modes. To avoid having one very long shader file with dozens of branches, I’d like to preload multiple jit.gl.shader objects and simply switch them at runtime. For this to be reliable on stage, I need to be sure about when compilation actually happens, and whether switching shaders ever triggers recompilation.

Here are my specific questions:

• When exactly does a jit.gl.shader compile its code? Is it compiled immediately when I read a file or set the @file attribute (when autowatch is 1), or only once it is bound to a jit.gl.mesh (or other OB3D)? Otherwise, if autowatch is 0, does it just sit idle until I explicitly send "compile" or "read"?

• Is “compile” and “bind” treated as separate steps in Jitter (like OpenGL’s glCompileShader vs. glUseProgram)? “For example, if I change the @shader attribute of a jit.gl.mesh from phongShader to blinnShader, does that trigger any recompilation, or does it simply bind an already compiled program?”

• Does the autowatch attribute affect this process? My understanding is that autowatch 1 only recompiles when the source file changes on disk, not when shaders are bound/unbound. Is that correct?

I’ve read the reference docs, and while they imply this separation, they don’t spell it out. I’d love to have a confirmation of how jit.gl.shader handles compilation vs binding, and exactly what role autowatch plays.

Rob Ramirez's icon

• When exactly does a jit.gl.shader compile its code? Is it compiled immediately when I read a file or set the @file attribute (when autowatch is 1), or only once it is bound to a jit.gl.mesh (or other OB3D)?

A shader is compiled the first time a jit.gl object it is bound to draws, so the latter is correct.

Otherwise, if autowatch is 0, does it just sit idle until I explicitly send "compile" or "read"?

yes.

For example, if I change the @shader attribute of a jit.gl.mesh from phongShader to blinnShader, does that trigger any recompilation, or does it simply bind an already compiled program?

Depends, if that shader was already previously compiled then the latter, otherwise the former.

My understanding is that autowatch 1 only recompiles when the source file changes on disk, not when shaders are bound/unbound

autowatch triggers a reloading of the shader source when the file is changed. Compiling only happens as mentioned above, when the binding object draws the first time. Though in theory this can be decoupled from the binding object using the compile and link messages to jit.gl.shader. However I should say that is atypical usage.

YaleO'neil's icon

Thanks a lot, that clears things up perfectly. Much appreciated!