Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfault: C 'get_exts()' doesn't differenciate OpenGL context from OpenGLES one #24

Closed
0x1100 opened this issue Jul 5, 2015 · 6 comments

Comments

@0x1100
Copy link

0x1100 commented Jul 5, 2015

I generate glad for OpenGL 3.0 and Open GL ES 2.0 (--api=gl=3.0,gles2=2.0).
Calling gladLoadGLES2Loader within an Open GL ES 3.0 context causes a segfault in get_exts by trying to call glGetStringi and glGetIntegerv (both NULL pointers) instead of glGetString.

Those functions aren't supposed to be loaded. get_exts uses them because of the 3.0 context without considering if we're given an OpenGL or an OpenGL ES one.

This could be considered a follow up of issue #6.

@Dav1dde
Copy link
Owner

Dav1dde commented Jul 5, 2015

This should not happen, get_exts checks for GLVersion.major < 3, gladLoadGLES2Loader resets the value before calling get_exts, the only issue I can see is if glGetString(GL_VERSION) returns a version > 3 on your ES context.

I am also not sure how to fix this, maybe introduce GL.Version.someNewVersion boolean which only gets set if the loaded version is >= 3 and Glad was built to load >= 3.

@Dav1dde
Copy link
Owner

Dav1dde commented Jul 5, 2015

Can you paste the result of glGetString(GL_VERSION) after setting up the ES context please?

Is it possible that you use a OpenGL ES context >= 3, but only built Glad to support 2.0?

EDIT: Small update, I figured out why this happens and that this should infact not happen, I will push a fix later tonight.

@Dav1dde Dav1dde closed this as completed in 4a5e79c Jul 5, 2015
@Dav1dde
Copy link
Owner

Dav1dde commented Jul 5, 2015

Thanks for the report!

Please test with current master and report back, I reproduced this locally and it seems to be fixed. I will push a new version if you give the ok ;).

@0x1100
Copy link
Author

0x1100 commented Jul 5, 2015

It's not fixed on my side...

I built for GL3 and ES2 ; then I ask for an ES 2.0 context to SDL2 but I get an ES 3.0 one. (I don't think it's a bug since there is backwards compatibility.)

I put the code generated by glad below. Currently, get_exts uses glGetIntegerv/glGetStringi if:

  • (defined(GL_ES_VERSION_3_0) or defined(GL_VERSION_3_0)) and `GLversion.major >= 3

Instead, we need to check if we get one of those:

  • 'context is GLES' and defined(GL_ES_VERSION_3_0) and GLversion.major >= 3
  • 'context is GL' and defined(GL_VERSION_3_0) and GLversion.major >= 3

Currently, get_exts choses to use glGetIntegerv/glGetStringi instead of glGetString because (defined(GL_VERSION_3_0) and GLversion.major >= 3) yet it shouldn't because ('context is GLES' and not defined(GL_ES_VERSION_3_0)).

I don't really get those two new variables (max_loaded_major, max_loaded_minor). Instead, I think that adding a mere parameter to get_exts (gl/gles) may do the trick.

#if defined(GL_ES_VERSION_3_0) || defined(GL_VERSION_3_0)
#define _GLAD_IS_SOME_NEW_VERSION 1
#endif

static int max_loaded_major;
static int max_loaded_minor;

static const char *exts = NULL;
static int num_exts_i = 0;
static const char **exts_i = NULL;

static void get_exts(void) {
#ifdef _GLAD_IS_SOME_NEW_VERSION
    if(max_loaded_major < 3) {
#endif
        exts = (const char *)glGetString(GL_EXTENSIONS);
#ifdef _GLAD_IS_SOME_NEW_VERSION
    } else {
        int index;

        num_exts_i = 0;
        glGetIntegerv(GL_NUM_EXTENSIONS, &num_exts_i);
        if (num_exts_i > 0) {
            exts_i = (const char **)realloc((void *)exts_i, num_exts_i * sizeof *exts_i);
        }

        for(index = 0; index < num_exts_i; index++) {
            exts_i[index] = (const char*)glGetStringi(GL_EXTENSIONS, index);
        }
    }
#endif
}

@Dav1dde Dav1dde reopened this Jul 5, 2015
@Dav1dde Dav1dde closed this as completed in 60d1290 Jul 5, 2015
@Dav1dde
Copy link
Owner

Dav1dde commented Jul 5, 2015

Please try again, I accidently only fixed the same problem with OpenGL contexts.

max_loaded_major/max_loaded_minor are the minimum of the GL/GLES context version and the supported version by glad. So if you get a 3.0 context but glad only supports 2.0 it will only use 2.0 versions (unlike before where it tried to use 3.0 functions which werent loaded).

I thought about clamping GLVersion instead of introducing max_loaded_major/max_loaded_minor but I didn't like that.

@0x1100
Copy link
Author

0x1100 commented Jul 6, 2015

Yep, it works. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants