6

I have not found any definitive answers to this, so decided to ask here. Is there really no way to save and load compiled webgl shaders? It seems a waste to compile the shaders every time someone loads the page, when all you would have to do is compile the shaders once, save it to a file, then load the compiled shader object, as you would HLSL (I know it's not GLSL, but i'm still a little new to OpenGL).

So, if possible, how can i save and load a compiled shader in webgl?

iedoc
  • 2,266
  • 3
  • 30
  • 53

1 Answers1

4

There really is no way, and imho thats a good thing. It would pose a security issue(feeding arbitrary bytecode to the GPU) in addition to that when drivers are updated the precompiled shaders are potentially missing new optimizations or just break.

when all you would have to do is compile the shaders once, save it to a file, then load the compiled shader object, as you would HLSL

OpenGL(and its derivatives) does not support loading pre-compiled shaders the same way DirectX does:

Program binary formats are not intended to be transmitted. It is not reasonable to expect different hardware vendors to accept the same binary formats. It is not reasonable to expect different hardware from the same vendor to accept the same binary formats. https://www.opengl.org/wiki/Shader_Compilation#Binary_limitations

There seems to be no intermediate format like SPIR-V in OpenGL so you would need to compile the shaders on the target platform introducing a whole lot of additional concerns with users changing their graphics cards / employing a hybrid graphics solution, storage limitations on the client(5 MB using localstorage) and the possibility of abusing it to fingerprint the hardware.

LJᛃ
  • 7,655
  • 2
  • 24
  • 35
  • accidentally deleted last comment. just wanted to say your right, you can not pre compile glsl programs as you can hlsl (coming from a directx background here). I've edited my question to remove the misinforming statement "as you would in any other graphics api" – iedoc Mar 15 '16 at 23:39
  • "Program binary formats are not intended to be transmitted. It is not reasonable to expect different hardware vendors to accept the same binary formats. It is not reasonable to expect different hardware from the same vendor to accept the same binary formats." Says who? The ones who can't implement it? [You can also precompile shaders in Vulkan, btw](https://www.khronos.org/spir/) – Pablo Ariel Jul 13 '21 at 17:53
  • A program binary is a preprocessed source and can run on any hardware that can interpret the bytecode through the hardware itself or making use of interpreter source code. So, no, a program binary is not necessarily hardware-vendor specific bytecode. It's just compiled sources and plenty of mistakes and errors can be detected in the compilation steps. In the end, the only difference is that one (the compiled code) is more reliable and had better testing than the other. – Pablo Ariel Jul 21 '21 at 19:17
  • @PabloAriel Okay, I guess [everybody](https://stackoverflow.com/questions/15900161/are-opengl-binary-program-formats-standardized) including [the official spec](https://www.khronos.org/registry/OpenGL/specs/gl/glspec46.core.pdf) (Page 131) _"Loading a program binary may also fail if the implementation determines that there has been a change in hardware or software configuration from when the program binary was produced such as having been compiled with an incompatible or outdated version of the compiler"_ has it wrong then. Got it! – LJᛃ Jul 21 '21 at 23:47
  • @PabloAriel I'm just stating the facts, I don't know what mission you're on but it's completely out of place and off-topic. – LJᛃ Jul 24 '21 at 16:48