How to require GLSL extension for material

Version: 7.38.0
Engine: WebGL2
Browser: Chrome

Background:
I’m trying to implement the WEBGL_multi_draw extension in babylon.js, see previous context here:

Per spec, #extension GL_ANGLE_multi_draw needs to be added to the shader, but when doing this in a material plugin, it emits error:

BJS - [09:42:26]: Error: VERTEX SHADER ERROR: 0:296: '
' : extension directive must occur before any non-preprocessor tokens in ESSL3

Playground:

After hours of searching in the code base, I found engine._shaderProcessor which can do this, but it’s marked with protected, and can not be used in typescript without force cast of Reflect things:

Question:
Is there a standand way, to require GLSL extension for material

Adding @Evgeni_Popov our material plugin Guru

I think the easiest way would be to simply move #define CUSTOM_VERTEX_BEGIN / #define CUSTOM_FRAGMENT_BEGIN at the very start of the standard / PBR shader files, and use CUSTOM_VERTEX_BEGIN as the injection point in your material plugin.

I don’t think it would be a breaking change, as they are currently defined just after a #include<__decl__pbrXXX>, which only inject struct / uniform definitions.

What do you think @sebavan?

Could it not break other extensions ? if not all good for me

No, I think it’s ok, we don’t use these injection points ourselves, and I doubt people use them either.

This case would work if 0 or 1 plugin requires extension, right?

No, it would work for any number of plugins: the #define CUSTOM_VERTEX_BEGIN line is not removed when a plugin inject some code, so another plugin can still inject code at the same place.

But if one plugin adds extension, and another adds uniform, it would break again.

Initial State

#define CUSTOM_VERTEX_BEGIN

Plugin 1 add extension

#define CUSTOM_VERTEX_BEGIN
+#extension GL_ANGLE_multi_draw : require

Plugin 2 add extension and uniform

#define CUSTOM_VERTEX_BEGIN
+#extension GL_ANGLE_base_vertex_base_instance_shader_builtin : require
+uniform int base_instance;
#extension GL_ANGLE_multi_draw : require

Indeed, if the plugin uses CUSTOM_VERTEX_BEGIN instead of CUSTOM_VERTEX_DEFINITIONS to inject uniforms…

So, I added new injection points for extensions:

3 Likes