Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading