What's Advantageous in Shader-Loading: Two Questions

I have two questions about shader-loading, if I may:

First, if I perform two shader-loads, each with a different vertex-shader file, is there any advantage (performance, memory-usage, caching, whatever) to their using the same fragment-shader file?

Or, to put it another way, when loading two shaders that use different vertex-shader files, is there any disadvantage to those loads also using different fragment-shader files (where it could perhaps be avoided)?

i.e. Is there any advantage to doing this:

myShader1 = Shader.load(Shader.SL_GLSL,
                        "myVertexShader.glsl",
                        "myFragmentShader.glsl")
myShader2 = Shader.load(Shader.SL_GLSL,
                        "myOtherVertexShader.glsl",
                        "myFragmentShader.glsl")
# And then presumably using them...

As opposed to this:

myShader1 = Shader.load(Shader.SL_GLSL,
                        "myVertexShader.glsl",
                        "myFragmentShader.glsl")
myShader2 = Shader.load(Shader.SL_GLSL,
                        "myOtherVertexShader.glsl",
                        "myOtherFragmentShader.glsl")
# And then presumably using them...

And second, when loading shaders multiple times from the same pair of shader-files, is it better to cache the resulting shader-object and re-use it, or–as with model-loading–is it fine to “reload” repeatedly? (Whether due to Panda caching shader-objects internally, or for some other reason.)

(The former comes from my thinking about making a slightly-variant fragment-shader for a specific purpose, in a case in which the object already uses a variant vertex-shader. But hesitating for fear of over-proliferating shaders–only to consider that I don’t know that the addition would make any difference, given that I am already using a variant vertex-shader.

The latter comes as a result of thinking about the above for this thread, and wondering whether I might be already introducing an inefficiency, since I load shaders–and often the same shaders–a fair bit.)

Not right now. In Panda 1.11, when the new shader pipeline is merged, there will be a slight improvement in first-time compilation time because the first stage of compilation will be cached.

There is no runtime advantage. In the end, it will still be a different Shader object, so the cost of switching to a different shader still applies.

Panda automatically caches the shader objects; you can verify this by calling Shader.load twice and comparing their equivalence with == or by checking the .this member which returns the underlying C++ pointer value.

Okay, that’s good news to me on both counts!

(The former means that I’m already getting a new shader-object in the scenario that prompted this–and thus that I might as well make a variant fragment-shader, which may improve the look of the thing being rendered.

The latter means that I don’t have to worry about all those loads elsewhere of the same shaders!)

Thank you! :slight_smile: