Skip to content

Commit 10c9b1d

Browse files
committed
egl: implement multi-context support
Signed-off-by: Steven Noonan <steven@uplinklabs.net>
1 parent ba3c786 commit 10c9b1d

File tree

5 files changed

+92
-31
lines changed

5 files changed

+92
-31
lines changed

glad/generator/c/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
jinja2_contextfilter
2020
)
2121
from glad.parse import Type, EnumType
22-
from glad.specification import VK, GL, WGL
22+
from glad.specification import VK, EGL, GL, WGL
2323
import glad.util
2424

2525
_ARRAY_RE = re.compile(r'\[[\d\w]*\]')
@@ -324,7 +324,7 @@ def get_template_arguments(self, spec, feature_set, config):
324324
args = JinjaGenerator.get_template_arguments(self, spec, feature_set, config)
325325

326326
# TODO allow MX for every specification/api
327-
if spec.name not in (VK.NAME, GL.NAME):
327+
if spec.name not in (EGL.NAME, VK.NAME, GL.NAME):
328328
args['options']['mx'] = False
329329
args['options']['mx_global'] = False
330330

glad/generator/c/templates/egl.c

+33-7
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static int glad_egl_find_core_{{ api|lower }}(EGLDisplay display) {
7070
return GLAD_MAKE_VERSION(major, minor);
7171
}
7272

73-
int gladLoad{{ api|api }}UserPtr(EGLDisplay display, GLADuserptrloadfunc load, void* userptr) {
73+
int gladLoad{{ api|api }}{{ 'Context' if options.mx }}UserPtr({{ template_utils.context_arg(',') }} EGLDisplay display, GLADuserptrloadfunc load, void *userptr) {
7474
int version;
7575
eglGetDisplay = (PFNEGLGETDISPLAYPROC) load(userptr, "eglGetDisplay");
7676
eglGetCurrentDisplay = (PFNEGLGETCURRENTDISPLAYPROC) load(userptr, "eglGetCurrentDisplay");
@@ -80,25 +80,51 @@ int gladLoad{{ api|api }}UserPtr(EGLDisplay display, GLADuserptrloadfunc load, v
8080

8181
version = glad_egl_find_core_{{ api|lower }}(display);
8282
if (!version) return 0;
83-
{% for feature, _ in loadable(feature_set.features) %}
84-
glad_egl_load_{{ feature.name }}(load, userptr);
83+
{% for feature, _ in loadable(feature_set.features, api=api) %}
84+
glad_egl_load_{{ feature.name }}({{'context, ' if options.mx }}load, userptr);
8585
{% endfor %}
8686

8787
if (!glad_egl_find_extensions_{{ api|lower }}(display)) return 0;
88-
{% for extension, _ in loadable(feature_set.extensions) %}
89-
glad_egl_load_{{ extension.name }}(load, userptr);
88+
{% for extension, _ in loadable(feature_set.extensions, api=api) %}
89+
glad_egl_load_{{ extension.name }}({{'context, ' if options.mx }}load, userptr);
9090
{% endfor %}
9191

92+
{% if options.mx_global %}
93+
gladSet{{ feature_set.name|api }}Context(context);
94+
{% endif %}
95+
9296
{% if options.alias %}
93-
glad_egl_resolve_aliases();
97+
glad_egl_resolve_aliases({{ 'context' if options.mx }});
9498
{% endif %}
9599

96100
return version;
97101
}
98102

103+
{% if options.mx_global %}
104+
int gladLoad{{ api|api }}UserPtr(EGLDisplay display, GLADuserptrloadfunc load, void *userptr) {
105+
return gladLoad{{ api|api }}ContextUserPtr(gladGet{{ feature_set.name|api }}Context(), display, load, userptr);
106+
}
107+
{% endif %}
108+
109+
int gladLoad{{ api|api }}{{ 'Context' if options.mx }}({{ template_utils.context_arg(',') }} EGLDisplay display, GLADloadfunc load) {
110+
return gladLoad{{ api|api }}{{ 'Context' if options.mx }}UserPtr({{'context,' if options.mx }} display, glad_egl_get_proc_from_userptr, GLAD_GNUC_EXTENSION (void*) load);
111+
}
112+
113+
{% if options.mx_global %}
99114
int gladLoad{{ api|api }}(EGLDisplay display, GLADloadfunc load) {
100-
return gladLoad{{ api|api }}UserPtr(display, glad_egl_get_proc_from_userptr, GLAD_GNUC_EXTENSION (void*) load);
115+
return gladLoad{{ api|api }}Context(gladGet{{ feature_set.name|api }}Context(), display, load);
101116
}
117+
{% endif %}
102118
{% endfor %}
103119

120+
{% if options.mx_global %}
121+
Glad{{ feature_set.name|api }}Context* gladGet{{ feature_set.name|api }}Context() {
122+
return {{ global_context }};
123+
}
124+
125+
void gladSet{{ feature_set.name|api }}Context(Glad{{ feature_set.name|api }}Context *context) {
126+
{{ global_context }} = context;
127+
}
128+
{% endif %}
129+
104130
{% endblock %}

glad/generator/c/templates/egl.h

+5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
{% block custom_declarations %}
44
{% for api in feature_set.info.apis %}
5+
GLAD_API_CALL int gladLoad{{ api|api }}{{ 'Context' if options.mx }}UserPtr({{ template_utils.context_arg(',') }} EGLDisplay display, GLADuserptrloadfunc load, void *userptr);
6+
GLAD_API_CALL int gladLoad{{ api|api }}{{ 'Context' if options.mx }}({{ template_utils.context_arg(',') }} EGLDisplay display, GLADloadfunc load);
7+
8+
{% if options.mx_global %}
59
GLAD_API_CALL int gladLoad{{ api|api }}UserPtr(EGLDisplay display, GLADuserptrloadfunc load, void *userptr);
610
GLAD_API_CALL int gladLoad{{ api|api }}(EGLDisplay display, GLADloadfunc load);
11+
{% endif %}
712
{% endfor %}
813
{% endblock %}

glad/generator/c/templates/loader/egl.c

+46-20
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ static GLADapiproc glad_egl_get_proc(void *vuserptr, const char* name) {
2020
return result;
2121
}
2222

23-
static void* _egl_handle = NULL;
23+
{% if not options.mx %}
24+
static void* {{ template_utils.handle() }} = NULL;
25+
{% endif %}
2426

25-
static void* glad_egl_dlopen_handle(void) {
27+
static void* glad_egl_dlopen_handle({{ template_utils.context_arg(def='void') }}) {
2628
#if GLAD_PLATFORM_APPLE
2729
static const char *NAMES[] = {"libEGL.dylib"};
2830
#elif GLAD_PLATFORM_WIN32
@@ -31,11 +33,11 @@ static void* glad_egl_dlopen_handle(void) {
3133
static const char *NAMES[] = {"libEGL.so.1", "libEGL.so"};
3234
#endif
3335

34-
if (_egl_handle == NULL) {
35-
_egl_handle = glad_get_dlopen_handle(NAMES, sizeof(NAMES) / sizeof(NAMES[0]));
36+
if ({{ template_utils.handle() }} == NULL) {
37+
{{ template_utils.handle() }} = glad_get_dlopen_handle(NAMES, sizeof(NAMES) / sizeof(NAMES[0]));
3638
}
3739

38-
return _egl_handle;
40+
return {{ template_utils.handle() }};
3941
}
4042

4143
static struct _glad_egl_userptr glad_egl_build_userptr(void *handle) {
@@ -46,30 +48,34 @@ static struct _glad_egl_userptr glad_egl_build_userptr(void *handle) {
4648
}
4749

4850
{% if not options.on_demand %}
49-
int gladLoaderLoadEGL(EGLDisplay display) {
51+
int gladLoaderLoadEGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(',') }} EGLDisplay display) {
5052
int version = 0;
51-
void *handle = NULL;
53+
void *handle;
5254
int did_load = 0;
5355
struct _glad_egl_userptr userptr;
5456

55-
did_load = _egl_handle == NULL;
56-
handle = glad_egl_dlopen_handle();
57-
if (handle != NULL) {
57+
did_load = {{ template_utils.handle() }} == NULL;
58+
handle = glad_egl_dlopen_handle({{ 'context' if options.mx }});
59+
if (handle) {
5860
userptr = glad_egl_build_userptr(handle);
5961

60-
if (userptr.get_proc_address_ptr != NULL) {
61-
version = gladLoadEGLUserPtr(display, glad_egl_get_proc, &userptr);
62-
}
62+
version = gladLoadEGL{{ 'Context' if options.mx }}UserPtr({{ 'context, ' if options.mx }}display, glad_egl_get_proc, &userptr);
6363

64-
if (!version && did_load) {
65-
gladLoaderUnloadEGL();
64+
if (did_load) {
65+
gladLoaderUnloadEGL{{ 'Context' if options.mx }}({{ 'context' if options.mx }});
6666
}
6767
}
6868

6969
return version;
7070
}
7171
{% endif %}
7272

73+
{% if options.mx_global %}
74+
int gladLoaderLoadEGL(EGLDisplay display) {
75+
return gladLoaderLoadEGLContext(gladGet{{ feature_set.name|api }}Context(), display);
76+
}
77+
{% endif %}
78+
7379
{% if options.on_demand %}
7480
{% call template_utils.zero_initialized() %}static struct _glad_egl_userptr glad_egl_internal_loader_global_userptr{% endcall %}
7581
static GLADapiproc glad_egl_internal_loader_get_proc(const char *name) {
@@ -81,19 +87,38 @@ static GLADapiproc glad_egl_internal_loader_get_proc(const char *name) {
8187
}
8288
{% endif %}
8389

84-
void gladLoaderUnloadEGL() {
85-
if (_egl_handle != NULL) {
86-
glad_close_dlopen_handle(_egl_handle);
87-
_egl_handle = NULL;
90+
{% if options.mx_global %}
91+
void gladLoaderResetEGL(void) {
92+
gladLoaderResetEGLContext(gladGetEGLContext());
93+
}
94+
{% endif %}
95+
96+
void gladLoaderUnloadEGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }}) {
97+
if ({{ template_utils.handle() }} != NULL) {
98+
glad_close_dlopen_handle({{ template_utils.handle() }});
99+
{{ template_utils.handle() }} = NULL;
88100
{% if options.on_demand %}
89101
glad_egl_internal_loader_global_userptr.handle = NULL;
90102
{% endif %}
91103
}
92104

105+
{% if not options.mx %}
93106
gladLoaderResetEGL();
107+
{% else %}
108+
gladLoaderResetEGLContext(context);
109+
{% endif %}
94110
}
95111

96-
void gladLoaderResetEGL() {
112+
{%if options.mx_global %}
113+
void gladLoaderUnloadEGL(void) {
114+
gladLoaderUnloadEGLContext(gladGet{{ feature_set.name|api }}Context());
115+
}
116+
{% endif %}
117+
118+
void gladLoaderResetEGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }}) {
119+
{% if options.mx %}
120+
memset(context, 0, sizeof(GladEGLContext));
121+
{% else %}
97122
{% if not options.on_demand %}
98123
{% for feature in feature_set.features %}
99124
GLAD_{{ feature.name }} = 0;
@@ -114,6 +139,7 @@ void gladLoaderResetEGL() {
114139
{{ command.name|ctx }} = NULL;
115140
{% endfor %}
116141
{% endfor %}
142+
{% endif %}
117143
}
118144

119145
#endif /* GLAD_EGL */
+6-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#ifdef GLAD_EGL
22

33
{% if not options.on_demand %}
4-
GLAD_API_CALL int gladLoaderLoadEGL(EGLDisplay display);
4+
GLAD_API_CALL int gladLoaderLoadEGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(',') }} EGLDisplay display);
55
{% endif %}
6-
6+
GLAD_API_CALL void gladLoaderUnloadEGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }});
7+
GLAD_API_CALL void gladLoaderResetEGL{{ 'Context' if options.mx }}({{ template_utils.context_arg(def='void') }});
8+
{% if options.mx_global %}
9+
GLAD_API_CALL int gladLoaderLoadEGL(EGLDisplay display);
710
GLAD_API_CALL void gladLoaderUnloadEGL(void);
811
GLAD_API_CALL void gladLoaderResetEGL(void);
12+
{% endif %}
913

1014
#endif

0 commit comments

Comments
 (0)