Skip to content

Commit

Permalink
c: fix extensions loading and parsing
Browse files Browse the repository at this point in the history
Simplify extension array:
Use NULL as array terminator and don't store the count of extensions
anymore.

Don't use version to detect extensions:
Check if functions are usable and fallback on old method if they are
not.

gh: #448 
closes: #446
  • Loading branch information
lephilousophe authored Dec 31, 2023
1 parent 7a7c639 commit 73eaae0
Showing 1 changed file with 40 additions and 52 deletions.
92 changes: 40 additions & 52 deletions glad/generator/c/templates/gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,24 @@ static void _post_call_{{ feature_set.name }}_callback_default(void *ret, const


{% block loader %}
#if defined(GL_ES_VERSION_3_0) || defined(GL_VERSION_3_0)
#define GLAD_GL_IS_SOME_NEW_VERSION 1
#else
#define GLAD_GL_IS_SOME_NEW_VERSION 0
#endif

static int glad_gl_get_extensions({{ template_utils.context_arg(',') }} int version, const char **out_exts, unsigned int *out_num_exts_i, char ***out_exts_i) {
#if GLAD_GL_IS_SOME_NEW_VERSION
if(GLAD_VERSION_MAJOR(version) < 3) {
#else
GLAD_UNUSED(version);
GLAD_UNUSED(out_num_exts_i);
GLAD_UNUSED(out_exts_i);
#endif
if ({{ 'glGetString'|ctx }} == NULL) {
return 0;
static void glad_gl_free_extensions(char **exts_i) {
if (exts_i != NULL) {
unsigned int index;
for(index = 0; exts_i[index]; index++) {
free((void *) (exts_i[index]));
}
*out_exts = (const char *){{ 'glGetString'|ctx }}(GL_EXTENSIONS);
#if GLAD_GL_IS_SOME_NEW_VERSION
} else {
free((void *)exts_i);
exts_i = NULL;
}
}
static int glad_gl_get_extensions({{ template_utils.context_arg(',') }} const char **out_exts, char ***out_exts_i) {
#if defined(GL_ES_VERSION_3_0) || defined(GL_VERSION_3_0)
if ({{ 'glGetStringi'|ctx }} != NULL && {{ 'glGetIntegerv'|ctx }} != NULL) {
unsigned int index = 0;
unsigned int num_exts_i = 0;
char **exts_i = NULL;
if ({{ 'glGetStringi'|ctx }} == NULL || {{ 'glGetIntegerv'|ctx }} == NULL) {
return 0;
}
{{ 'glGetIntegerv'|ctx }}(GL_NUM_EXTENSIONS, (int*) &num_exts_i);
if (num_exts_i > 0) {
exts_i = (char **) malloc(num_exts_i * (sizeof *exts_i));
}
exts_i = (char **) malloc((num_exts_i + 1) * (sizeof *exts_i));
if (exts_i == NULL) {
return 0;
}
Expand All @@ -74,31 +62,40 @@ static int glad_gl_get_extensions({{ template_utils.context_arg(',') }} int vers
size_t len = strlen(gl_str_tmp) + 1;

char *local_str = (char*) malloc(len * sizeof(char));
if(local_str != NULL) {
memcpy(local_str, gl_str_tmp, len * sizeof(char));
if(local_str == NULL) {
exts_i[index] = NULL;
glad_gl_free_extensions(exts_i);
return 0;
}

memcpy(local_str, gl_str_tmp, len * sizeof(char));
exts_i[index] = local_str;
}
exts_i[index] = NULL;

*out_num_exts_i = num_exts_i;
*out_exts_i = exts_i;

return 1;
}
#else
GLAD_UNUSED(out_exts_i);
#endif
if ({{ 'glGetString'|ctx }} == NULL) {
return 0;
}
*out_exts = (const char *){{ 'glGetString'|ctx }}(GL_EXTENSIONS);
return 1;
}
static void glad_gl_free_extensions(char **exts_i, unsigned int num_exts_i) {
if (exts_i != NULL) {
static int glad_gl_has_extension(const char *exts, char **exts_i, const char *ext) {
if(exts_i) {
unsigned int index;
for(index = 0; index < num_exts_i; index++) {
free((void *) (exts_i[index]));
for(index = 0; exts_i[index]; index++) {
const char *e = exts_i[index];
if(strcmp(e, ext) == 0) {
return 1;
}
}
free((void *)exts_i);
exts_i = NULL;
}
}
static int glad_gl_has_extension(int version, const char *exts, unsigned int num_exts_i, char **exts_i, const char *ext) {
if(GLAD_VERSION_MAJOR(version) < 3 || !GLAD_GL_IS_SOME_NEW_VERSION) {
} else {
const char *extensions;
const char *loc;
const char *terminator;
Expand All @@ -118,14 +115,6 @@ static int glad_gl_has_extension(int version, const char *exts, unsigned int num
}
extensions = terminator;
}
} else {
unsigned int index;
for(index = 0; index < num_exts_i; index++) {
const char *e = exts_i[index];
if(strcmp(e, ext) == 0) {
return 1;
}
}
}
return 0;
}
Expand All @@ -135,19 +124,18 @@ static GLADapiproc glad_gl_get_proc_from_userptr(void *userptr, const char* name
}

{% for api in feature_set.info.apis %}
static int glad_gl_find_extensions_{{ api|lower }}({{ template_utils.context_arg(',') }} int version) {
static int glad_gl_find_extensions_{{ api|lower }}({{ template_utils.context_arg(def='void') }}) {
const char *exts = NULL;
unsigned int num_exts_i = 0;
char **exts_i = NULL;
if (!glad_gl_get_extensions({{ 'context, ' if options.mx }}version, &exts, &num_exts_i, &exts_i)) return 0;
if (!glad_gl_get_extensions({{ 'context, ' if options.mx }}&exts, &exts_i)) return 0;

{% for extension in feature_set.extensions|select('supports', api) %}
{{ ('GLAD_' + extension.name)|ctx(name_only=True) }} = glad_gl_has_extension(version, exts, num_exts_i, exts_i, "{{ extension.name }}");
{{ ('GLAD_' + extension.name)|ctx(name_only=True) }} = glad_gl_has_extension(exts, exts_i, "{{ extension.name }}");
{% else %}
GLAD_UNUSED(glad_gl_has_extension);
{% endfor %}

glad_gl_free_extensions(exts_i, num_exts_i);
glad_gl_free_extensions(exts_i);

return 1;
}
Expand Down Expand Up @@ -194,7 +182,7 @@ int gladLoad{{ api|api }}{{ 'Context' if options.mx }}UserPtr({{ template_utils.
glad_gl_load_{{ feature.name }}({{'context, ' if options.mx }}load, userptr);
{% endfor %}

if (!glad_gl_find_extensions_{{ api|lower }}({{ 'context, ' if options.mx }}version)) return 0;
if (!glad_gl_find_extensions_{{ api|lower }}({{ 'context' if options.mx }})) return 0;
{% for extension, _ in loadable(feature_set.extensions, api=api) %}
glad_gl_load_{{ extension.name }}({{'context, ' if options.mx }}load, userptr);
{% endfor %}
Expand Down

0 comments on commit 73eaae0

Please sign in to comment.