Skip to content

Commit

Permalink
c: store handles inside mx struct
Browse files Browse the repository at this point in the history
* test: 0-initialize context struct

This bug flew under the radar because the struct does not actually need
to be initialized so far, but adding handles here requires it.

Update all of the tests, even the compile-only tests, for consistency
reasons. (And to prevent copy/paste disasters in the future)

* example: zero-initialize context structs

See previous commit.

* c: add handle field to mx context

This can be used to store the handle when combining the internal loader
with an MX context, in a way that doesn't require static global state.

* c: store _gl_handle in mx context

* c: store _gles2_handle in mx context

* c: store _gles1_handle in mx context

* c: store _vulkan_handle in mx context

closes: Dav1dde#385
  • Loading branch information
haasn authored Oct 21, 2022
1 parent 24ddbba commit e4015c5
Show file tree
Hide file tree
Showing 35 changed files with 94 additions and 78 deletions.
2 changes: 1 addition & 1 deletion example/c++/hellowindow2_macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ int main()
#endif

#ifdef GLAD_OPTION_GL_MX
GladGLContext context;
GladGLContext context = {};
#ifdef GLAD_OPTION_GL_LOADER
int version = gladLoaderLoadGLContext(&context);
#else
Expand Down
2 changes: 1 addition & 1 deletion example/c++/hellowindow2_mx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int main()
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);

GladGLContext context;
GladGLContext context = {};
int version = gladLoadGLContext(&context, glfwGetProcAddress);
if (version == 0)
{
Expand Down
2 changes: 1 addition & 1 deletion example/c++/multiwin_mx/multiwin_mx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ GLFWwindow* create_window(const char *name, int major, int minor) {
GladGLContext* create_context(GLFWwindow *window) {
glfwMakeContextCurrent(window);

GladGLContext* context = (GladGLContext*) malloc(sizeof(GladGLContext));
GladGLContext* context = (GladGLContext*) calloc(1, sizeof(GladGLContext));
if (!context) return NULL;

int version = gladLoadGLContext(context, glfwGetProcAddress);
Expand Down
4 changes: 4 additions & 0 deletions glad/generator/c/templates/base_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ typedef struct Glad{{ feature_set.name|api }}Context {
{{ command.name|pfn }} {{ command.name|ctx(member=True) }};
{% endcall %}
{% endfor %}

{% if options.loader %}
void* glad_loader_handle;
{% endif %}
} Glad{{ feature_set.name|api }}Context;

{% if options.mx_global %}
Expand Down
26 changes: 14 additions & 12 deletions glad/generator/c/templates/loader/gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ static GLADapiproc glad_gl_get_proc(void *vuserptr, const char *name) {
return result;
}

static void* _gl_handle = NULL;
{% if not options.mx %}
static void* {{ template_utils.handle() }} = NULL;
{% endif %}

static void* glad_gl_dlopen_handle(void) {
static void* glad_gl_dlopen_handle({{ template_utils.context_arg(def='void') }}) {
#if GLAD_PLATFORM_APPLE
static const char *NAMES[] = {
"../Frameworks/OpenGL.framework/OpenGL",
Expand All @@ -45,11 +47,11 @@ static void* glad_gl_dlopen_handle(void) {
};
#endif

if (_gl_handle == NULL) {
_gl_handle = glad_get_dlopen_handle(NAMES, sizeof(NAMES) / sizeof(NAMES[0]));
if ({{ template_utils.handle() }} == NULL) {
{{ template_utils.handle() }} = glad_get_dlopen_handle(NAMES, sizeof(NAMES) / sizeof(NAMES[0]));
}

return _gl_handle;
return {{ template_utils.handle() }};
}

static struct _glad_gl_userptr glad_gl_build_userptr(void *handle) {
Expand All @@ -76,15 +78,15 @@ int gladLoaderLoadGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(
int did_load = 0;
struct _glad_gl_userptr userptr;

did_load = _gl_handle == NULL;
handle = glad_gl_dlopen_handle();
did_load = {{ template_utils.handle() }} == NULL;
handle = glad_gl_dlopen_handle({{ 'context' if options.mx }});
if (handle) {
userptr = glad_gl_build_userptr(handle);

version = gladLoadGL{{ 'Context' if options.mx }}UserPtr({{ 'context,' if options.mx }}glad_gl_get_proc, &userptr);

if (did_load) {
gladLoaderUnloadGL();
gladLoaderUnloadGL{{ 'Context' if options.mx }}({{ 'context' if options.mx }});
}
}

Expand All @@ -109,10 +111,10 @@ int gladLoaderLoadGL(void) {
}
{% endif %}

void gladLoaderUnloadGL(void) {
if (_gl_handle != NULL) {
glad_close_dlopen_handle(_gl_handle);
_gl_handle = NULL;
void gladLoaderUnloadGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }}) {
if ({{ template_utils.handle() }} != NULL) {
glad_close_dlopen_handle({{ template_utils.handle() }});
{{ template_utils.handle() }} = NULL;
{% if options.on_demand %}
glad_gl_internal_loader_global_userptr.handle = NULL;
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion glad/generator/c/templates/loader/gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ GLAD_API_CALL int gladLoaderLoadGL{{ 'Context' if options.mx }}({{ template_util
{% if options.mx_global %}
GLAD_API_CALL int gladLoaderLoadGL(void);
{% endif %}
GLAD_API_CALL void gladLoaderUnloadGL(void);
GLAD_API_CALL void gladLoaderUnloadGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }});

#endif
26 changes: 14 additions & 12 deletions glad/generator/c/templates/loader/gles1.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ static GLADapiproc glad_gles1_get_proc(void *vuserptr, const char* name) {
return result;
}

static void* _gles1_handle = NULL;
{% if not options.mx %}
static void* {{ template_utils.handle() }} = NULL;
{% endif %}

static void* glad_gles1_dlopen_handle(void) {
static void* glad_gles1_dlopen_handle({{ template_utils.context_arg(def='void') }}) {
#if GLAD_PLATFORM_APPLE
static const char *NAMES[] = {"libGLESv1_CM.dylib"};
#elif GLAD_PLATFORM_WIN32
Expand All @@ -35,11 +37,11 @@ static void* glad_gles1_dlopen_handle(void) {
static const char *NAMES[] = {"libGLESv1_CM.so.1", "libGLESv1_CM.so", "libGLES_CM.so.1"};
#endif

if (_gles1_handle == NULL) {
_gles1_handle = glad_get_dlopen_handle(NAMES, sizeof(NAMES) / sizeof(NAMES[0]));
if ({{ template_utils.handle() }} == NULL) {
{{ template_utils.handle() }} = glad_get_dlopen_handle(NAMES, sizeof(NAMES) / sizeof(NAMES[0]));
}

return _gles1_handle;
return {{ template_utils.handle() }};
}

static struct _glad_gles1_userptr glad_gles1_build_userptr(void *handle) {
Expand All @@ -60,15 +62,15 @@ int gladLoaderLoadGLES1{{ 'Context' if options.mx }}({{ template_utils.context_a
return 0;
}

did_load = _gles1_handle == NULL;
handle = glad_gles1_dlopen_handle();
did_load = {{ template_utils.handle() }} == NULL;
handle = glad_gles1_dlopen_handle({{ 'context' if options.mx }});
if (handle != NULL) {
userptr = glad_gles1_build_userptr(handle);

version = gladLoadGLES1{{ 'Context' if options.mx }}UserPtr({{ 'context, ' if options.mx }}glad_gles1_get_proc, &userptr);

if (!version && did_load) {
gladLoaderUnloadGLES1();
gladLoaderUnloadGLES1{{ 'Context' if options.mx }}({{ 'context' if options.mx }});
}
}

Expand All @@ -93,10 +95,10 @@ int gladLoaderLoadGLES1(void) {
}
{% endif %}

void gladLoaderUnloadGLES1(void) {
if (_gles1_handle != NULL) {
glad_close_dlopen_handle(_gles1_handle);
_gles1_handle = NULL;
void gladLoaderUnloadGLES1{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }}) {
if ({{ template_utils.handle() }} != NULL) {
glad_close_dlopen_handle({{ template_utils.handle() }});
{{ template_utils.handle() }} = NULL;
{% if options.on_demand %}
glad_gles1_internal_loader_global_userptr.handle = NULL;
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion glad/generator/c/templates/loader/gles1.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ GLAD_API_CALL int gladLoaderLoadGLES1{{ 'Context' if options.mx }}({{ template_u
{% if options.mx_global %}
GLAD_API_CALL int gladLoaderLoadGLES1(void);
{% endif %}
GLAD_API_CALL void gladLoaderUnloadGLES1(void);
GLAD_API_CALL void gladLoaderUnloadGLES1{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }});

#endif /* GLAD_GLES1 */
26 changes: 14 additions & 12 deletions glad/generator/c/templates/loader/gles2.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ static GLADapiproc glad_gles2_get_proc(void *vuserptr, const char* name) {
return result;
}

static void* _gles2_handle = NULL;
{% if not options.mx %}
static void* {{ template_utils.handle() }} = NULL;
{% endif %}

static void* glad_gles2_dlopen_handle(void) {
static void* glad_gles2_dlopen_handle({{ template_utils.context_arg(def='void') }}) {
#if GLAD_PLATFORM_EMSCRIPTEN
#elif GLAD_PLATFORM_APPLE
static const char *NAMES[] = {"libGLESv2.dylib"};
Expand All @@ -47,11 +49,11 @@ static void* glad_gles2_dlopen_handle(void) {
#if GLAD_PLATFORM_EMSCRIPTEN
return NULL;
#else
if (_gles2_handle == NULL) {
_gles2_handle = glad_get_dlopen_handle(NAMES, sizeof(NAMES) / sizeof(NAMES[0]));
if ({{ template_utils.handle() }} == NULL) {
{{ template_utils.handle() }} = glad_get_dlopen_handle(NAMES, sizeof(NAMES) / sizeof(NAMES[0]));
}

return _gles2_handle;
return {{ template_utils.handle() }};
#endif
}

Expand Down Expand Up @@ -81,15 +83,15 @@ int gladLoaderLoadGLES2{{ 'Context' if options.mx }}({{ template_utils.context_a
return 0;
}

did_load = _gles2_handle == NULL;
handle = glad_gles2_dlopen_handle();
did_load = {{ template_utils.handle() }} == NULL;
handle = glad_gles2_dlopen_handle({{ 'context' if options.mx }});
if (handle != NULL) {
userptr = glad_gles2_build_userptr(handle);

version = gladLoadGLES2{{ 'Context' if options.mx }}UserPtr({{ 'context, ' if options.mx }}glad_gles2_get_proc, &userptr);

if (!version && did_load) {
gladLoaderUnloadGLES2();
gladLoaderUnloadGLES2{{ 'Context' if options.mx }}({{ 'context' if options.mx }});
}
}
#endif
Expand All @@ -115,10 +117,10 @@ int gladLoaderLoadGLES2(void) {
}
{% endif %}

void gladLoaderUnloadGLES2(void) {
if (_gles2_handle != NULL) {
glad_close_dlopen_handle(_gles2_handle);
_gles2_handle = NULL;
void gladLoaderUnloadGLES2{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }}) {
if ({{ template_utils.handle() }} != NULL) {
glad_close_dlopen_handle({{ template_utils.handle() }});
{{ template_utils.handle() }} = NULL;
{% if options.on_demand %}
glad_gles2_internal_loader_global_userptr.get_proc_address_ptr = NULL;
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion glad/generator/c/templates/loader/gles2.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ GLAD_API_CALL int gladLoaderLoadGLES2{{ 'Context' if options.mx }}({{ template_u
{% if options.mx_global %}
GLAD_API_CALL int gladLoaderLoadGLES2(void);
{% endif %}
GLAD_API_CALL void gladLoaderUnloadGLES2(void);
GLAD_API_CALL void gladLoaderUnloadGLES2{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }});

#endif /* GLAD_GLES2 */

26 changes: 14 additions & 12 deletions glad/generator/c/templates/loader/vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ static GLADapiproc glad_vulkan_get_proc(void *vuserptr, const char *name) {
}


static void* _vulkan_handle;
{% if not options.mx %}
static void* {{ template_utils.handle() }} = NULL;
{% endif %}

static void* glad_vulkan_dlopen_handle(void) {
static void* glad_vulkan_dlopen_handle({{ template_utils.context_arg(def='void') }}) {
static const char *NAMES[] = {
#if GLAD_PLATFORM_APPLE
"libvulkan.1.dylib",
Expand All @@ -71,11 +73,11 @@ static void* glad_vulkan_dlopen_handle(void) {
#endif
};

if (_vulkan_handle == NULL) {
_vulkan_handle = glad_get_dlopen_handle(NAMES, sizeof(NAMES) / sizeof(NAMES[0]));
if ({{ template_utils.handle() }} == NULL) {
{{ template_utils.handle() }} = glad_get_dlopen_handle(NAMES, sizeof(NAMES) / sizeof(NAMES[0]));
}

return _vulkan_handle;
return {{ template_utils.handle() }};
}

static struct _glad_vulkan_userptr glad_vulkan_build_userptr(void *handle, VkInstance instance, VkDevice device) {
Expand All @@ -95,8 +97,8 @@ int gladLoaderLoadVulkan{{ 'Context' if options.mx }}({{ template_utils.context_
int did_load = 0;
struct _glad_vulkan_userptr userptr;

did_load = _vulkan_handle == NULL;
handle = glad_vulkan_dlopen_handle();
did_load = {{ template_utils.handle() }} == NULL;
handle = glad_vulkan_dlopen_handle({{ 'context' if options.mx }});
if (handle != NULL) {
userptr = glad_vulkan_build_userptr(handle, instance, device);

Expand All @@ -105,7 +107,7 @@ int gladLoaderLoadVulkan{{ 'Context' if options.mx }}({{ template_utils.context_
}

if (!version && did_load) {
gladLoaderUnloadVulkan();
gladLoaderUnloadVulkan{{ 'Context' if options.mx }}({{ 'context' if options.mx }});
}
}

Expand Down Expand Up @@ -139,10 +141,10 @@ int gladLoaderLoadVulkan(VkInstance instance, VkPhysicalDevice physical_device,
}
{% endif %}

void gladLoaderUnloadVulkan(void) {
if (_vulkan_handle != NULL) {
glad_close_dlopen_handle(_vulkan_handle);
_vulkan_handle = NULL;
void gladLoaderUnloadVulkan{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }}) {
if ({{ template_utils.handle() }} != NULL) {
glad_close_dlopen_handle({{ template_utils.handle() }});
{{ template_utils.handle() }} = NULL;
{% if options.on_demand %}
glad_vulkan_internal_loader_global_userptr.vk_handle = NULL;
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion glad/generator/c/templates/loader/vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ GLAD_API_CALL int gladLoaderLoadVulkan{{ 'Context' if options.mx }}({{ template_
{% if options.mx_global %}
GLAD_API_CALL int gladLoaderLoadVulkan(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device);
{% endif %}
GLAD_API_CALL void gladLoaderUnloadVulkan(void);
GLAD_API_CALL void gladLoaderUnloadVulkan{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }});

{% if options.on_demand %}
GLAD_API_CALL void gladLoaderSetVulkanInstance(VkInstance instance);
Expand Down
6 changes: 5 additions & 1 deletion glad/generator/c/templates/template_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
{{ 'Glad' + feature_set.name|api + 'Context *context' + suffix if options.mx else def }}
{%- endmacro %}
{% macro handle() -%}
{{ 'context->glad' if options.mx else '_glad_' + feature_set.name|api }}_loader_handle
{%- endmacro %}
{% macro protect(symbol) %}
{% set protections = spec.protections(symbol, feature_set=feature_set) %}
Expand Down Expand Up @@ -126,4 +130,4 @@ GLAD_API_CALL {{ command.name|pfn }} glad_debug_{{ command.name }};
#else
{{ caller() }} = { 0 };
#endif
{% endmacro %}
{% endmacro %}
2 changes: 1 addition & 1 deletion test/c/compile/gl/header-only+mx/001/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <glad/gl.h>

int main(void) {
GladGLContext gl;
GladGLContext gl = {0};
(void) gladLoaderLoadGL();
(void) gladLoaderLoadGLContext(&gl);
return 0;
Expand Down
2 changes: 1 addition & 1 deletion test/c/compile/gl/header-only+mx/002/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <glad/gl.h>

int main(void) {
GladGLContext gl;
GladGLContext gl = {0};
(void) gladLoaderLoadGL();
(void) gladLoaderLoadGLContext(&gl);
return 0;
Expand Down
2 changes: 1 addition & 1 deletion test/c/compile/gl/header-only+mx/003/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <glad/gl.h>

int main(void) {
GladGLContext gl;
GladGLContext gl = {0};
(void) gladLoaderLoadGL();
(void) gladLoaderLoadGLContext(&gl);
return 0;
Expand Down
2 changes: 1 addition & 1 deletion test/c/compile/gl/header-only+mx/004/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <glad/gl.h>

int main(void) {
GladGLContext gl;
GladGLContext gl = {0};
(void) gladLoaderLoadGL();
(void) gladLoaderLoadGLContext(&gl);
return 0;
Expand Down
2 changes: 1 addition & 1 deletion test/c/compile/gl/header-only+mx/005/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <glad/gl.h>

int main(void) {
GladGLContext gl;
GladGLContext gl = {0};
(void) gladLoaderLoadGL();
(void) gladLoaderLoadGLContext(&gl);
return 0;
Expand Down
2 changes: 1 addition & 1 deletion test/c/compile/gl/header-only+mx/006/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <glad/gl.h>

int main(void) {
GladGLContext gl;
GladGLContext gl = {0};
(void) gladLoaderLoadGL();
(void) gladLoaderLoadGLContext(&gl);
return 0;
Expand Down
Loading

0 comments on commit e4015c5

Please sign in to comment.