8

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?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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"
"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
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.
@PabloAriel Okay, I guess everybody including the official spec (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!
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.