mosra / magnum

Lightweight and modular C++11 graphics middleware for games and data visualization
https://magnum.graphics/
Other
4.75k stars 439 forks source link

Extension points in builtin shaders #569

Open mosra opened 2 years ago

mosra commented 2 years ago

Discussed on Gitter a while ago, putting it here to avoid forgetting the details.

The main idea is that vertex (fragment, geometry...) shaders would contain various extension placeholders using macros. At the very least one for defining extra uniforms / inputs / outputs, and one inside the main function, with actual code.

#ifdef VERTEX_SHADER_INTERFACE_EXTENSION
VERTEX_SHADER_INTERFACE_EXTENSION
#endif

void main() {
    ...
    #ifdef VERTEX_SHADER_MAIN_EXTENSION
    VERTEX_SHADER_MAIN_EXTENSION
    #endif
}

Then, the user would have an option to subclass the shader and supply an additonal file to the base class, which defines a subset of these macros, such as:

#define VERTEX_SHADER_INTERFACE_EXTENSION \
    uniform(location = 69) in float fancyFactor;
#define VERTEX_SHADER_MAIN_EXTENSION \
    gl_Position.z *= fancyFactor;

This file would be inserted as the very first (after the #version directive), to allow also enabling various extension in it:

#extension GL_ARB_bindless_texture: require

#define VERTEX_SHADER_INTERFACE_EXTENSION \
    ...

Similar approach is taken in Babylon.js: https://github.com/BabylonJS/Babylon.js/blob/35eb617f377d67383e70e263e8a19596d80b5069/packages/dev/core/src/Shaders/default.fragment.fx

What's left to figure out:

hsdk123 commented 2 years ago

I'm not sure how do-able this would be, but would also be great if there was an easy way to just completely replace ex. the main() function in the shaders.

mosra commented 2 years ago

I merged #576 yesterday, which resolves the major blockers here (see the updated list above). So this is getting closer to being doable ;)

hsdk123 commented 2 years ago

Cool! Hoping https://github.com/mosra/magnum/issues/506 can be picked up as well once you free up.