gloam 0.4.7

Loader generator for Vulkan, OpenGL, OpenGL ES, EGL, GLX, and WGL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
{% import "utils.j2" as u with context %}
{#- Built-in platform loader. Only included when --loader is passed. -#}
{% include "library.j2" %}
{% if fs.spec_name in ["gl", "gles1", "gles2", "glcore"] %}
/* ---- GL / GLES built-in loader ----------------------------------------- */

/* Transient userptr: set for the duration of a gloamLoaderLoad*Context call.
 * Holds the library handle and — for desktop GL — the correctly-typed
 * platform proc-addr function pointer so that the calling convention is
 * always right. Only valid between entry and return; never accessed
 * concurrently.
 */
struct gloam_gl_load_userptr {
    void *handle;
#if defined(GLOAM_PLATFORM_WINDOWS)
    /* wglGetProcAddress is WINAPI (__stdcall); must not be called through a
     * plain cdecl function pointer.
	 */
    GloamAPIProc (WINAPI *wgl_get_proc)(const char *);
#elif !defined(__APPLE__) && !defined(__HAIKU__)
    GloamAPIProc (*glx_get_proc)(const char *);
#endif
};
static struct gloam_gl_load_userptr gloam_gl_load_state;

/* GL desktop adapter — matches GloamLoadFunc (__cdecl), dispatches through
 * the correctly-typed platform proc-addr pointer stored in the userptr.
 */
static GloamAPIProc gloam_gl_get_proc(const char *name)
{
    GloamAPIProc result = NULL;
    struct gloam_gl_load_userptr *u = &gloam_gl_load_state;
    if (!u->handle)
        return NULL;
#if defined(__APPLE__) || defined(__HAIKU__)
    result = (GloamAPIProc)gloam_dlsym(u->handle, name);
#elif defined(GLOAM_PLATFORM_WINDOWS)
    if (u->wgl_get_proc)
        result = u->wgl_get_proc(name);
    if (!result)
        result = (GloamAPIProc)gloam_dlsym(u->handle, name);
#else
    if (u->glx_get_proc)
        result = u->glx_get_proc(name);
    if (!result)
        result = (GloamAPIProc)gloam_dlsym(u->handle, name);
#endif
    return result;
}

/* GLES adapter: all symbols (including extensions) are exported directly from
 * the GLES library so plain dlsym is sufficient — no platform indirection.
 */
static GloamAPIProc gloam_gles_get_proc(const char *name)
{
    struct gloam_gl_load_userptr *u = &gloam_gl_load_state;
    if (!u->handle)
        return NULL;
    return (GloamAPIProc)gloam_dlsym(u->handle, name);
}

{%- for api in fs.apis %}

int gloamLoaderLoad{{ api | api_display }}Context({{ u.ctx_arg() }})
{
    int did_open = 0;
    int version;
    void *handle;
{% if api in ["gles1", "gles2"] %}
    static const char * const kLibNames[] = {
#if defined(__APPLE__)
        "libGLESv2.dylib",
#elif defined(GLOAM_PLATFORM_WINDOWS)
        "GLESv2.dll", "libGLESv2.dll",
#else
        "libGLESv2.so.2", "libGLESv2.so",
#endif
    };
{% else %}
    static const char * const kLibNames[] = {
#if defined(__APPLE__)
        "../Frameworks/OpenGL.framework/OpenGL",
        "/Library/Frameworks/OpenGL.framework/OpenGL",
        "/System/Library/Frameworks/OpenGL.framework/OpenGL",
        "/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL",
#elif defined(GLOAM_PLATFORM_WINDOWS)
        "opengl32.dll",
#else
        "libGL.so.1",
        "libGL.so",
#endif
    };
{% endif %}
    handle = context->gloam_loader_handle;

    if (!handle) {
        handle = gloam_open_library(kLibNames, GLOAM_ARRAYSIZE(kLibNames));
        did_open = 1;
    }

    if (!handle)
        return 0;

    gloam_gl_load_state.handle = handle;
#if defined(GLOAM_PLATFORM_WINDOWS)
    gloam_gl_load_state.wgl_get_proc = (GloamAPIProc (WINAPI *)(const char *))gloam_dlsym(handle, "wglGetProcAddress");
#elif !defined(__APPLE__) && !defined(__HAIKU__)
    gloam_gl_load_state.glx_get_proc = (GloamAPIProc (*)(const char *))gloam_dlsym(handle, "glXGetProcAddressARB");
#endif

{% if api in ["gles1", "gles2"] %}
    version = gloamLoad{{ api | api_display }}Context(context, gloam_gles_get_proc);
{% else %}
    version = gloamLoad{{ api | api_display }}Context(context, gloam_gl_get_proc);
{% endif %}
    gloam_gl_load_state.handle = NULL;

    if (!version && did_open) {
        gloam_dlclose(handle);
        return 0;
    }

    context->gloam_loader_handle = handle;
    context->gloam_loader_owns_handle |= (uint8_t)did_open;

    return version;
}

int gloamLoaderLoad{{ api | api_display }}(void)
{
    return gloamLoaderLoad{{ api | api_display }}Context(&gloam_{{ fs.spec_name }}_context);
}

void gloamLoaderUnload{{ api | api_display }}Context({{ u.ctx_arg() }})
{
    if (context->gloam_loader_handle && context->gloam_loader_owns_handle) {
        gloam_dlclose(context->gloam_loader_handle);
    }
    gloamLoaderReset{{ api | api_display }}Context(context);
}

void gloamLoaderUnload{{ api | api_display }}(void)
{
    gloamLoaderUnload{{ api | api_display }}Context(&gloam_{{ fs.spec_name }}_context);
}

void gloamLoaderReset{{ api | api_display }}Context({{ u.ctx_arg() }})
{
    memset(context, 0, sizeof(*context));
}

void gloamLoaderReset{{ api | api_display }}(void)
{
    gloamLoaderReset{{ api | api_display }}Context(&gloam_{{ fs.spec_name }}_context);
}
{%- endfor %}
{% elif fs.spec_name == "egl" %}
/* ---- EGL built-in loader ------------------------------------------------ */

static const char * const gloam_egl_lib_names[] = {
#if defined(__APPLE__)
    "libEGL.dylib",
#elif defined(GLOAM_PLATFORM_WINDOWS)
    "libEGL.dll", "EGL.dll",
#else
    "libEGL.so.1", "libEGL.so",
#endif
};

/* eglGetProcAddress has its own calling convention (declared in EGL headers as
 * PFNEGLGETPROCADDRESSPROC). Store it with the correct type so the call always
 * uses the right convention, even when the default differs (e.g. Win32).
 */
struct gloam_egl_load_userptr {
    void *handle;
    PFNEGLGETPROCADDRESSPROC get_proc_address;
};
static struct gloam_egl_load_userptr gloam_egl_load_state;

static GloamAPIProc gloam_egl_get_proc(const char *name)
{
    struct gloam_egl_load_userptr *u = &gloam_egl_load_state;
    GloamAPIProc result = (GloamAPIProc)gloam_dlsym(u->handle, name);
    if (!result && u->get_proc_address)
        result = u->get_proc_address(name);
    return result;
}

{% for api in fs.apis %}
int gloamLoaderLoad{{ api | api_display }}Context({{ u.ctx_arg(', ') }}EGLDisplay display)
{
    int did_open = 0;
    int version;
    void *handle;

    handle = context->gloam_loader_handle;

    if (!handle) {
        handle = gloam_open_library(gloam_egl_lib_names, GLOAM_ARRAYSIZE(gloam_egl_lib_names));
        did_open = 1;
    }

    if (!handle)
        return 0;

    gloam_egl_load_state.handle = handle;
    gloam_egl_load_state.get_proc_address =
        (PFNEGLGETPROCADDRESSPROC)gloam_dlsym(handle, "eglGetProcAddress");

    if (!gloam_egl_load_state.get_proc_address) {
        if (did_open)
            gloam_dlclose(handle);
        return 0;
    }

    version = gloamLoad{{ api | api_display }}Context(context, display, gloam_egl_get_proc);
    gloam_egl_load_state.handle = NULL;

    if (!version && did_open) {
        gloam_dlclose(handle);
        return 0;
    }

    context->gloam_loader_handle = handle;
    context->gloam_loader_owns_handle |= (uint8_t)did_open;

    return version;
}

int gloamLoaderLoad{{ api | api_display }}(EGLDisplay display)
{
    return gloamLoaderLoad{{ api | api_display }}Context(&gloam_{{ fs.spec_name }}_context, display);
}

void gloamLoaderUnload{{ api | api_display }}Context({{ u.ctx_arg() }})
{
    if (context->gloam_loader_handle && context->gloam_loader_owns_handle) {
        gloam_dlclose(context->gloam_loader_handle);
    }
    gloamLoaderReset{{ api | api_display }}Context(context);
}

void gloamLoaderUnload{{ api | api_display }}(void)
{
    gloamLoaderUnload{{ api | api_display }}Context(&gloam_{{ fs.spec_name }}_context);
}

void gloamLoaderReset{{ api | api_display }}Context({{ u.ctx_arg() }})
{
    memset(context, 0, sizeof(*context));
}

void gloamLoaderReset{{ api | api_display }}(void)
{
    gloamLoaderReset{{ api | api_display }}Context(&gloam_{{ fs.spec_name }}_context);
}
{%- endfor %}
{%- elif fs.spec_name == "glx" %}
/* ---- GLX built-in loader ------------------------------------------------ */

static const char * const gloam_glx_lib_names[] = {
#if defined(__CYGWIN__)
    "libGL-1.so",
#endif
    "libGL.so.1", "libGL.so",
};

/* glXGetProcAddressARB returns __GLXextFuncPtr = void(*)(void); store with the
 * correct function-pointer type so the call goes through the right ABI.
 */
struct gloam_glx_load_userptr {
    void *handle;
    GloamAPIProc (*get_proc_address)(const char *);
};
static struct gloam_glx_load_userptr gloam_glx_load_state;

static GloamAPIProc gloam_glx_get_proc(const char *name)
{
    struct gloam_glx_load_userptr *u = &gloam_glx_load_state;
    GloamAPIProc result = NULL;
    if (u->get_proc_address)
        result = u->get_proc_address(name);
    if (!result)
        result = (GloamAPIProc)gloam_dlsym(u->handle, name);
    return result;
}

{%- for api in fs.apis %}

int gloamLoaderLoad{{ api | api_display }}Context({{ u.ctx_arg(', ') }}Display *display, int screen)
{
    int did_open = 0, version;
    void *handle;

    handle = context->gloam_loader_handle;

    if (!handle) {
        handle = gloam_open_library(gloam_glx_lib_names, GLOAM_ARRAYSIZE(gloam_glx_lib_names));
        did_open = 1;
    }

    if (!handle)
        return 0;

    gloam_glx_load_state.handle = handle;
    gloam_glx_load_state.get_proc_address = (GloamAPIProc (*)(const char *))gloam_dlsym(handle, "glXGetProcAddressARB");

    if (!gloam_glx_load_state.get_proc_address) {
        if (did_open)
            gloam_dlclose(handle);
        return 0;
    }

    version = gloamLoad{{ api | api_display }}Context(context, display, screen, gloam_glx_get_proc);
    gloam_glx_load_state.handle = NULL;

    if (!version && did_open) {
        gloam_dlclose(handle);
        return 0;
    }

    context->gloam_loader_handle = handle;
    context->gloam_loader_owns_handle |= (uint8_t)did_open;

    return version;
}

int gloamLoaderLoad{{ api | api_display }}(Display *display, int screen)
{
    return gloamLoaderLoad{{ api | api_display }}Context(&gloam_{{ fs.spec_name }}_context, display, screen);
}

void gloamLoaderUnload{{ api | api_display }}Context({{ u.ctx_arg() }})
{
    if (context->gloam_loader_handle && context->gloam_loader_owns_handle) {
        gloam_dlclose(context->gloam_loader_handle);
    }
    gloamLoaderReset{{ api | api_display }}Context(context);
}

void gloamLoaderUnload{{ api | api_display }}(void)
{
    gloamLoaderUnload{{ api | api_display }}Context(&gloam_{{ fs.spec_name }}_context);
}

void gloamLoaderReset{{ api | api_display }}Context({{ u.ctx_arg() }})
{
    memset(context, 0, sizeof(*context));
}

void gloamLoaderReset{{ api | api_display }}(void)
{
    gloamLoaderReset{{ api | api_display }}Context(&gloam_{{ fs.spec_name }}_context);
}
{%- endfor %}
{% elif fs.spec_name == "wgl" %}
/* ---- WGL built-in loader ------------------------------------------------ */

static const char * const gloam_wgl_lib_names[] = { "opengl32.dll" };

/* wglGetProcAddress is WINAPI (__stdcall); must not be stored or called through
 * a plain cdecl pointer. Store with the correct type in a userptr struct.
 */
struct gloam_wgl_load_userptr {
    void *handle;
    GloamAPIProc (WINAPI *wgl_get_proc)(const char *);
};
static struct gloam_wgl_load_userptr gloam_wgl_load_state;

static GloamAPIProc gloam_wgl_get_proc(const char *name)
{
    struct gloam_wgl_load_userptr *u = &gloam_wgl_load_state;
    GloamAPIProc result = NULL;
    if (u->wgl_get_proc)
        result = u->wgl_get_proc(name);
    if (!result)
        result = (GloamAPIProc)gloam_dlsym(u->handle, name);
    return result;
}

{%- for api in fs.apis %}
int gloamLoaderLoad{{ api | api_display }}Context({{ u.ctx_arg(', ') }}HDC hdc)
{
    int did_open = 0;
    int version;
    void *handle = context->gloam_loader_handle;

    if (!handle) {
        handle = gloam_open_library(
            gloam_wgl_lib_names, GLOAM_ARRAYSIZE(gloam_wgl_lib_names));
        did_open = 1;
    }

    if (!handle)
        return 0;

    gloam_wgl_load_state.handle = handle;
    gloam_wgl_load_state.wgl_get_proc =
        (GloamAPIProc (WINAPI *)(const char *))
            gloam_dlsym(handle, "wglGetProcAddress");

    if (!gloam_wgl_load_state.wgl_get_proc) {
        if (did_open)
            gloam_dlclose(handle);
        return 0;
    }

    version = gloamLoad{{ api | api_display }}Context(context, hdc, gloam_wgl_get_proc);
    gloam_wgl_load_state.handle = NULL;

    if (!version && did_open) {
        gloam_dlclose(handle);
        return 0;
    }

    context->gloam_loader_handle = handle;
    context->gloam_loader_owns_handle |= (uint8_t)did_open;

    return version;
}

int gloamLoaderLoad{{ api | api_display }}(HDC hdc)
{
    return gloamLoaderLoad{{ api | api_display }}Context(&gloam_{{ fs.spec_name }}_context, hdc);
}

void gloamLoaderUnload{{ api | api_display }}Context({{ u.ctx_arg() }})
{
    if (context->gloam_loader_handle && context->gloam_loader_owns_handle) {
        gloam_dlclose(context->gloam_loader_handle);
    }
    gloamLoaderReset{{ api | api_display }}Context(context);
}

void gloamLoaderUnload{{ api | api_display }}(void)
{
    gloamLoaderUnload{{ api | api_display }}Context(&gloam_{{ fs.spec_name }}_context);
}

void gloamLoaderReset{{ api | api_display }}Context({{ u.ctx_arg() }})
{
    memset(context, 0, sizeof(*context));
}

void gloamLoaderReset{{ api | api_display }}(void)
{
    gloamLoaderReset{{ api | api_display }}Context(&gloam_{{ fs.spec_name }}_context);
}
{%- endfor %}
{% elif fs.spec_name == "vk" %}
/* ---- Vulkan built-in discovery loader ----------------------------------- */

/* gloamLoaderLoadVulkanContext
 *
 * Opens the Vulkan library into context->gloam_loader_handle if it is not
 * already set, stores vkGetInstanceProcAddr in the context, then delegates to
 * gloamVulkanDiscoverContext. Follows the same additive multi-call contract
 * as the underlying discover function.
 */
int gloamLoaderLoadVulkanContext({{ u.ctx_arg(', ') }}VkInstance instance, VkPhysicalDevice physical_device, VkDevice device)
{
    int did_open = 0;
    int version;
    void *handle;

    handle = context->gloam_loader_handle;

    if (!handle) {
        handle = gloam_open_library(
            gloam_vk_lib_names, GLOAM_ARRAYSIZE(gloam_vk_lib_names));
        did_open = 1;
    }

    if (!handle)
        return 0;

    if (!context->GetInstanceProcAddr)
        context->GetInstanceProcAddr =
            (PFN_vkGetInstanceProcAddr)gloam_dlsym(handle, "vkGetInstanceProcAddr");

    if (!context->GetInstanceProcAddr) {
        if (did_open)
            gloam_dlclose(handle);
        return 0;
    }

    version = gloamVulkanDiscoverContext(context, instance, physical_device, device);

    if (!version && did_open) {
        gloam_dlclose(handle);
        return 0;
    }

    context->gloam_loader_handle = handle;
    context->gloam_loader_owns_handle |= (uint8_t)did_open;

    return version;
}

int gloamLoaderLoadVulkan(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device)
{
    return gloamLoaderLoadVulkanContext(&gloam_vk_context, instance,
                                        physical_device, device);
}

/* Close the library handle (if set) then zero all context state. */
void gloamLoaderUnloadVulkanContext({{ u.ctx_arg() }})
{
    if (context->gloam_loader_handle && context->gloam_loader_owns_handle) {
        gloam_dlclose(context->gloam_loader_handle);
    }
    gloamLoaderResetVulkanContext(context);
}

void gloamLoaderUnloadVulkan(void)
{
    gloamLoaderUnloadVulkanContext(&gloam_vk_context);
}

/* Zero all context state without touching the library handle. */
void gloamLoaderResetVulkanContext({{ u.ctx_arg() }})
{
    memset(context, 0, sizeof(*context));
}

void gloamLoaderResetVulkan(void)
{
    gloamLoaderResetVulkanContext(&gloam_vk_context);
}
{% endif %}