Saving/Loading 已编译的 webgl 着色器

Saving/Loading compiled webgl shaders

我还没有找到任何明确的答案,所以决定在这里问一下。真的没有办法保存和加载已编译的 webgl 着色器吗?每次有人加载页面时编译着色器似乎是一种浪费,当你所要做的就是编译一次着色器,将其保存到一个文件中,然后加载编译的着色器对象,就像你在 HLSL 中一样(我知道这不是GLSL,但我对 OpenGL 还是有点陌生​​)。

那么,如果可能的话,我如何在 webgl 中保存和加载已编译的着色器?

实在是没办法,恕我直言这是好事。它会带来安全问题(向 GPU 提供任意字节码),此外,当更新驱动程序时,预编译的着色器可能会缺少新的优化或只是中断。

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(及其衍生产品)不支持以与 DirectX 相同的方式加载预编译着色器:

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

OpenGL 中似乎没有像 SPIR-V 这样的中间格式,因此您需要在目标平台上编译着色器,这会给用户更换显卡/使用混合图形解决方案带来很多额外的担忧,客户端的存储限制(5 MB 使用 localstorage)以及滥用它来对硬件进行指纹识别的可能性。