Skip to content

Commit

Permalink
wgl: dynamically load opengl32.dll and wglGetProcAddress
Browse files Browse the repository at this point in the history
Signed-off-by: Steven Noonan <steven@uplinklabs.net>
  • Loading branch information
tycho committed Dec 28, 2023
1 parent 68d9e6b commit 58576bb
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 11 deletions.
4 changes: 1 addition & 3 deletions glad/generator/c/templates/loader/gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ int gladLoaderLoadGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(
if (handle) {
userptr = glad_gl_build_userptr(handle);

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

if (!version && did_load) {
gladLoaderUnloadGL{{ 'Context' if options.mx }}({{ 'context' if options.mx }});
Expand Down Expand Up @@ -116,9 +116,7 @@ void gladLoaderResetGL{{ 'Context' if options.mx }}({{ template_utils.context_ar
void gladLoaderResetGL(void) {
gladLoaderResetGLContext(gladGetGLContext());
}
{% endif %}

{% if options.mx_global %}
int gladLoaderLoadGL(void) {
return gladLoaderLoadGLContext(gladGet{{ feature_set.name|api }}Context());
}
Expand Down
114 changes: 109 additions & 5 deletions glad/generator/c/templates/loader/wgl.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,118 @@
#ifdef GLAD_WGL

static GLADapiproc glad_wgl_get_proc(void *vuserptr, const char* name) {
GLAD_UNUSED(vuserptr);
return GLAD_GNUC_EXTENSION (GLADapiproc) wglGetProcAddress(name);
{% include 'loader/library.c' %}

typedef void* (GLAD_API_PTR *GLADwglprocaddrfunc)(const char*);
struct _glad_wgl_userptr {
void *handle;
GLADwglprocaddrfunc wgl_get_proc_address_ptr;
};

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

static void* glad_wgl_dlopen_handle({{ template_utils.context_arg(def='void') }}) {
#if GLAD_PLATFORM_APPLE
static const char *NAMES[] = {
"../Frameworks/OpenGL.framework/OpenGL",
"/Library/Frameworks/OpenGL.framework/OpenGL",
"/System/Library/Frameworks/OpenGL.framework/OpenGL",
"/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL"
};
#elif GLAD_PLATFORM_WIN32
static const char *NAMES[] = {"opengl32.dll"};
#else
static const char *NAMES[] = {
#if defined(__CYGWIN__)
"libGL-1.so",
#endif
"libGL.so.1",
"libGL.so"
};
#endif

if ({{ template_utils.handle() }} == NULL) {
{{ template_utils.handle() }} = glad_get_dlopen_handle(NAMES, GLAD_ARRAYSIZE(NAMES));
}

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

int gladLoaderLoadWGL(HDC hdc) {
return gladLoadWGLUserPtr(hdc, glad_wgl_get_proc, NULL);
static struct _glad_wgl_userptr glad_wgl_build_userptr(void *handle) {
struct _glad_wgl_userptr userptr;

userptr.handle = handle;
#if GLAD_PLATFORM_APPLE || defined(__HAIKU__)
userptr.wgl_get_proc_address_ptr = NULL;
#elif GLAD_PLATFORM_WIN32
userptr.wgl_get_proc_address_ptr =
(GLADwglprocaddrfunc) glad_dlsym_handle(handle, "wglGetProcAddress");
#else
userptr.wgl_get_proc_address_ptr =
(GLADwglprocaddrfunc) glad_dlsym_handle(handle, "glXGetProcAddressARB");
#endif

return userptr;
}

static GLADapiproc glad_wgl_get_proc(void *vuserptr, const char *name) {
struct _glad_wgl_userptr userptr = *(struct _glad_wgl_userptr*) vuserptr;
GLADapiproc result = NULL;

if(userptr.wgl_get_proc_address_ptr != NULL) {
result = GLAD_GNUC_EXTENSION (GLADapiproc) userptr.wgl_get_proc_address_ptr(name);
}
if(result == NULL) {
result = glad_dlsym_handle(userptr.handle, name);
}

return result;
}

int gladLoaderLoadWGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(', ') }}HDC hdc) {
int version = 0;
void *handle;
int did_load = 0;
struct _glad_wgl_userptr userptr;

did_load = {{ template_utils.handle() }} == NULL;
handle = glad_wgl_dlopen_handle({{ 'context' if options.mx }});
if (handle) {
userptr = glad_wgl_build_userptr(handle);

version = gladLoadWGL{{ 'Context' if options.mx }}UserPtr({{ 'context, ' if options.mx }}hdc, glad_wgl_get_proc, &userptr);

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

{% if options.mx_global %}
int gladLoaderLoadWGL(HDC hdc) {
return gladLoaderLoadWGLContext(gladGet{{ feature_set.name|api }}Context(), hdc);
}

void gladLoaderResetWGL(void) {
gladLoaderResetWGLContext(gladGetWGLContext());
}
{% endif %}

void gladLoaderUnloadWGL{{ '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 not options.mx %}
gladLoaderResetWGL();
{% else %}
gladLoaderResetWGLContext(context);
{% endif %}
}

void gladLoaderResetWGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }}) {
{% if options.mx %}
memset(context, 0, sizeof(GladWGLContext));
Expand All @@ -35,4 +133,10 @@ void gladLoaderResetWGL{{ 'Context' if options.mx }}({{ template_utils.context_a
{% endif %}
}

{%if options.mx_global %}
void gladLoaderUnloadWGL(void) {
gladLoaderUnloadWGLContext(gladGet{{ feature_set.name|api }}Context());
}
{% endif %}

#endif /* GLAD_WGL */
10 changes: 7 additions & 3 deletions glad/generator/c/templates/loader/wgl.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{% import "template_utils.h" as template_utils with context %}
#ifdef GLAD_WGL

GLAD_API_CALL int gladLoaderLoadWGL(HDC hdc);
GLAD_API_CALL int gladLoaderLoadWGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(', ') }}HDC hdc);
GLAD_API_CALL void gladLoaderUnloadWGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }});
GLAD_API_CALL void gladLoaderResetWGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }});
{% if not options.mx_global %}
{% if options.mx_global %}
GLAD_API_CALL int gladLoaderLoadWGL(HDC hdc);
GLAD_API_CALL void gladLoaderUnloadWGL(void);
GLAD_API_CALL void gladLoaderResetWGL(void);
{% endif %}

#endif
#endif

0 comments on commit 58576bb

Please sign in to comment.