Home > Software design >  How do I know from within a BGFX shader it is running on the OpenGL API?
How do I know from within a BGFX shader it is running on the OpenGL API?

Time:08-24

I'm working on a project based on BGFX and I'm trying to define with a fragment shader is BGFX is running in OpenGL or DirectX.

gl_FragColor = texture2D(color_tex, UV0);

I need this information to access a texture, as the texture coordinate (UV0) is different between GL and DirectX. I could create a specific version of the shader for both APIs but there must be a most clever way to handle this. I looked in BGFX documentation but couldn't find anything about this point.

Furthermore, isn't the whole point of BGFX to abstract this kind of APIs differences ?

CodePudding user response:

BGFX provides a series of macros that let the shader preprocessor to know in what context it is working. You will find an example here : https://github.com/bkaradzic/bgfx/blob/69fb21f50e1136d9f3828a134bb274545b4547cb/examples/41-tess/matrices.sh#L22

In your case, your SL code could read like this:

#if BGFX_SHADER_LANGUAGE_GLSL
    vec2 UV0_corrected = vec2(1.0, 1.0)   vec2(-1.0, -1.0) * UV0;
#else
    vec2 UV0_corrected = vec2(1.0, 0.0)   vec2(-1.0, 1.0) * UV0;
#endif
  • Related