PistonDevelopers / opengl_graphics

An OpenGL 2D back-end for the Piston game engine
MIT License
61 stars 45 forks source link

Properly selecting between shaders for GL/GLES/WebGL #324

Open agausmann opened 4 years ago

agausmann commented 4 years ago

At the moment, opengl_graphics selects between shader sources at compile time by checking target_os = "emscripten", but this isn't a perfect solution. I'm working on a library that translates GL to WebGL in wasm32-unknown-unknown, which will also require GLSL ES shaders; however, it won't work with this logic. It is also possible to target OpenGL ES outside of WebAssembly, and such use cases will also require GLSL ES shaders. I did implement some manual shader selection using feature flags in #318 and #319, but that isn't the best way to solve this either.

GL has a parameter called SHADING_LANGUAGE_VERSION which allows you to query the supported shader version at runtime. GL, WebGL and GL ES all support this, with the following formats:

I propose that the logic is changed to detect the shader version using this scheme instead of at compile time. This will mean that the user does not have to manually pick a shader version at compile time, with the cost being some additional overhead; there will be some extra time overhead at startup from querying the GL, and slightly larger binary sizes due to all of the shader versions being included (though it should be less than a few KB, and the included shaders can even be manually pruned if needed using default feature flags).

agausmann commented 4 years ago

To be clear on why I think the current feature-based solution isn't great:

bvssvni commented 4 years ago

This seems reasonable. As a precaution, we could try developing against a different branch to test this for while before releasing it. What do you think?