Shader source recompilation

Basically, this topic is about recompiling shaders.
First, off, jme3’s GLRenderer.updateShaderSourceData:

public void updateShaderSourceData(ShaderSource source) {
    int id = source.getId();
    if (id == -1) {
        ...
       source.setId(id);
    } else { throw new RendererException("Cannot recompile shader source"); }

From this it looks like jme3 currently does not support shader recompilation.

Questions(for anyone who knows):

  • Is there a reason why jme3 does not support recompiling shaders?
  • Is shader recompilation supported for different graphics cards?

Here is how I currently recompile shaders:

    Shader shader = ...//get from material's TechniqueDefLogic.makeCurrent;
    int shaderId = shader.getId();
    ShaderSource source = ... shader.getSources()[0];
    int id = source.getId();
    
    gl.glShaderSource(id, new String[]{ newCode.toString() }, newCodeLength);
    gl.glCompileShader(id);
    gl.glAttachShader(shaderId, id);
    gl.glLinkProgram(shaderId);

    for(Uniform u : shader.getUniformMap().values()) u.reset();              

This work well for me so far. From https://www.opengl.org/sdk/docs/man/html/glShaderSource.xhtml

glShaderSource sets the source code in shader to the source code in the array of strings specified by string.
Any source code previously stored in the shader object is completely replaced.

This paragraph states that previously stored shader source code gets replaced, from which I assume that shader recompilation could work and it does on my pc.

Thus, if it works, can it be implemented into jme3 at some point?

Although it is supported by OpenGL, I would tend to avoid it for several reasons:

  1. jME materials manage shaders automatically using an internal cache, and having users recompile those shaders could confuse the material that manages it.
  2. Vulkan does not support recompiling shaders, so when the Vulkan backend is implemented, this capability would be difficult to support.
1 Like

Okay, thank you for the information.

  1. It might have some niche uses. eg glsl editor preview, etc.
  2. I wonder if creating new one on Vulkan would be faster or slower then recompiling on GLSL.