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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
{% import "utils.j2" as u with context -%}
{{ preamble }}

#ifndef GLOAM_{{ guard }}
#define GLOAM_{{ guard }}
{# ---- System-header conflict guards ---------------------------------------
   These prevent including both the platform/system GL or Vulkan headers and
   this gloam header, which would cause duplicate declarations. Using
   reserved identifiers (double-underscore prefixed) requires suppressing the
   clang warning for that specific pragma block. #}
{%- if fs.spec_name in ["gl", "gles1", "gles2", "glcore"] %}
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#endif
{%- for api in fs.apis %}
{%- if api in ["gl", "glcore"] %}
{{ u.header_error("__gl_h_",     "OpenGL (gl.h)",     api) }}
{{ u.header_error("__glext_h_",  "OpenGL (glext.h)",  api) }}
{%- elif api == "gles1" %}
{{ u.header_error("__gl_h_",       "OpenGL ES 1 (gl.h)",   api) }}
{{ u.header_error("__gles1_gl_h_", "OpenGL ES 1",          api) }}
{%- elif api == "gles2" %}
{{ u.header_error("__gl2_h_",       "OpenGL ES 2 (gl2.h)", api) }}
{{ u.header_error("__gles2_gl2_h_", "OpenGL ES 2",         api) }}
{{ u.header_error("__gl3_h_",       "OpenGL ES 3 (gl3.h)", api) }}
{{ u.header_error("__gles2_gl3_h_", "OpenGL ES 3",         api) }}
{%- endif %}
{%- endfor %}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
{%- elif fs.spec_name == "vk" and not external_headers %}
{{ u.header_error("VULKAN_H_",      "Vulkan (vulkan.h)",      "vulkan") }}
{{ u.header_error("VULKAN_CORE_H_", "Vulkan (vulkan_core.h)", "vulkan") }}
{%- elif fs.spec_name == "egl" %}
{{ u.header_error("__egl_h_", "EGL (egl.h)", "egl") }}
{%- elif fs.spec_name == "glx" %}
{{ u.header_error("GLX_H", "GLX (glx.h)", "glx") }}
{%- elif fs.spec_name == "wgl" %}
{{ u.header_error("__wgl_h_", "WGL (wgl.h)", "wgl") }}
{%- endif %}

/* ---- Platform detection ------------------------------------------------ */
#ifndef GLOAM_PLATFORM_DETECTED_
#define GLOAM_PLATFORM_DETECTED_
#if defined(__CYGWIN__) || defined(_WIN32)
#  define GLOAM_PLATFORM_WINDOWS 1
#endif
#if defined(__linux)
#  define GLOAM_PLATFORM_LINUX 1
#endif
#endif /* GLOAM_PLATFORM_DETECTED_ */

#ifndef GLOAM_HAS_ENUM_BASE_TYPE
#if defined(__cplusplus) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L)
#  define GLOAM_HAS_ENUM_BASE_TYPE 1
#else
#  define GLOAM_HAS_ENUM_BASE_TYPE 0
#endif
#endif
{%- if fs.is_vulkan %}

#ifndef GLOAM_FORCE_INLINE
#if defined(_MSC_VER)
#  define GLOAM_FORCE_INLINE static __forceinline
#elif defined(__GNUC__) || defined(__clang__)
#  define GLOAM_FORCE_INLINE static inline __attribute__((always_inline))
#else
#  define GLOAM_FORCE_INLINE static inline
#endif
#endif
{%- endif %}
{%- if fs.spec_name == "glx" %}

#ifdef GLOAM_PLATFORM_LINUX

#include <gloam/gl.h>

#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

{%- elif fs.spec_name == "wgl" %}

#ifdef GLOAM_PLATFORM_WINDOWS

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>

#include <gloam/gl.h>

/* These are defined in wingdi.h and we're redefining them. */
#undef wglUseFontBitmaps
#undef wglUseFontOutlines
{%- endif %}
{%- if fs.is_vulkan and external_headers %}

{# ---- External Vulkan headers mode ----------------------------------------
   Include upstream Vulkan-Headers instead of embedding type definitions.
   Uses forward declarations for heavyweight platform types (Win32, Xlib)
   to avoid pulling in large/slow platform headers. -#}
#ifndef VK_NO_PROTOTYPES
#define VK_NO_PROTOTYPES
#endif

#ifndef VULKAN_H_
#include <vulkan/vk_platform.h>
#include <vulkan/vulkan_core.h>
#endif

#if !defined(VULKAN_H_)

#ifdef VK_USE_PLATFORM_ANDROID_KHR
#include <vulkan/vulkan_android.h>
#endif

#ifdef VK_USE_PLATFORM_FUCHSIA
#include <zircon/types.h>
#include <vulkan/vulkan_fuchsia.h>
#endif

#ifdef VK_USE_PLATFORM_IOS_MVK
#include <vulkan/vulkan_ios.h>
#endif

#ifdef VK_USE_PLATFORM_MACOS_MVK
#include <vulkan/vulkan_macos.h>
#endif

#ifdef VK_USE_PLATFORM_METAL_EXT
#include <vulkan/vulkan_metal.h>
#endif

#ifdef VK_USE_PLATFORM_VI_NN
#include <vulkan/vulkan_vi.h>
#endif

#ifdef VK_USE_PLATFORM_WAYLAND_KHR
#include <vulkan/vulkan_wayland.h>
#endif

#ifdef VK_USE_PLATFORM_WIN32_KHR
typedef unsigned long DWORD;
typedef const wchar_t* LPCWSTR;
typedef void* HANDLE;
typedef struct HINSTANCE__* HINSTANCE;
typedef struct HWND__* HWND;
typedef struct HMONITOR__* HMONITOR;
typedef struct _SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES;
#include <vulkan/vulkan_win32.h>
#endif

#ifdef VK_USE_PLATFORM_XCB_KHR
#include <xcb/xcb.h>
#include <vulkan/vulkan_xcb.h>
#endif

#ifdef VK_USE_PLATFORM_XLIB_KHR
typedef struct _XDisplay Display;
typedef unsigned long Window;
typedef unsigned long VisualID;
#include <vulkan/vulkan_xlib.h>
#endif

#ifdef VK_USE_PLATFORM_DIRECTFB_EXT
#include <directfb.h>
#include <vulkan/vulkan_directfb.h>
#endif

#ifdef VK_USE_PLATFORM_XLIB_XRANDR_EXT
typedef struct _XDisplay Display;
typedef unsigned long RROutput;
#include <vulkan/vulkan_xlib_xrandr.h>
#endif

#ifdef VK_USE_PLATFORM_GGP
#include <ggp_c/vulkan_types.h>
#include <vulkan/vulkan_ggp.h>
#endif

#ifdef VK_USE_PLATFORM_SCREEN_QNX
#include <screen/screen.h>
#include <vulkan/vulkan_screen.h>
#endif

#ifdef VK_USE_PLATFORM_SCI
#include <nvscisync.h>
#include <nvscibuf.h>
#include <vulkan/vulkan_sci.h>
#endif

#ifdef VK_ENABLE_BETA_EXTENSIONS
#include <vulkan/vulkan_beta.h>
#endif

#ifdef VK_USE_PLATFORM_OHOS
#include <vulkan/vulkan_ohos.h>
#endif

#endif /* !defined(VULKAN_H_) */

{%- else %}

#include <stddef.h>
#include <stdint.h>
{% if fs.required_headers | length > 0 %}
{% for hdr in fs.required_headers -%}
#include "{{ hdr }}"
{% endfor -%}
{%- endif %}
{%- if fs.include_type_groups | length > 0 %}
{# Platform / WSI system headers: include-category types whose protection
   guards were inferred at generation time from which extensions require them.
   An empty protect list means the include is unconditional.
   Consecutive types sharing the same guard are coalesced into a single
   #ifdef/#endif block by the resolver. -#}
{% for group in fs.include_type_groups -%}
{{ u.protect_begin(group.protect) -}}
{% for type in group.items -%}
{{ type.raw_c }}
{% endfor -%}
{{ u.protect_end(group.protect) -}}
{% endfor -%}
{%- endif %}
{%- endif %}
#ifdef __cplusplus
extern "C" {
#endif
{%- if fs.is_gl_family %}

/* Calling convention */
#ifndef APIENTRY
#  if defined(GLOAM_PLATFORM_WINDOWS)
#    define APIENTRY __stdcall
#  else
#    define APIENTRY
#  endif
#endif
#ifndef APIENTRYP
#  define APIENTRYP APIENTRY *
#endif
#ifndef GLAPI
#  define GLAPI extern
#endif

/* Calling convention for GL callback function pointers (e.g. debug callbacks,
 * blob cache functions). Piggybacks on APIENTRY if already defined so that
 * code which defines APIENTRY before including this header gets consistent
 * behaviour. Guard against redefinition so multiple gloam headers included
 * in the same translation unit don't conflict.
 */
#ifndef GLOAM_API_PTR
#  ifdef APIENTRY
#    define GLOAM_API_PTR APIENTRY
#  elif defined(GLOAM_PLATFORM_WINDOWS)
#    define GLOAM_API_PTR __stdcall
#  else
#    define GLOAM_API_PTR
#  endif
#endif
#ifndef GLAPIENTRY
#  define GLAPIENTRY GLOAM_API_PTR
#endif
{%- endif %}
{%- if fs.spec_name in ["gl", "gles1", "gles2", "glcore"] %}

/* Forward declarations for OpenCL interop types. */
struct _cl_context;
struct _cl_event;
{%- endif %}
{%- if not (fs.is_vulkan and external_headers) %}
{%- if fs.features | length > 0 %}

/* ---- Version feature guards ----------------------------------------------
 * These mirror the upstream vulkan_core.h / gl.h definitions so that code
 * guarded by e.g. #ifdef GL_VERSION_3_3 compiles correctly against this
 * header.
 */
{% for feat in fs.features -%}
#define {{ feat.full_name }} 1
{% endfor -%}
{%- endif %}
{%- if fs.ext_guard_groups | length > 0 %}
/* ---- Extension compile-time guards ---------------------------------------
 * These mirror the definitions in standard glext.h/gl2ext.h/eglext.h so
 * that code guarded by e.g. #ifdef GL_ARB_draw_indirect compiles correctly
 * against this header.
 */
{% for group in fs.ext_guard_groups -%}
{{- u.protect_begin(group.protect) -}}
{% for ext in group.items -%}
#define {{ ext.name }} 1
{% endfor -%}
{{ u.protect_end(group.protect) -}}
{%- endfor %}
{% endif -%}
{%- if fs.flat_enums | length > 0 -%}
/* ---- Constants ----------------------------------------------------------- */
{% for group in fs.flat_enum_groups -%}
{% if group.protect | length > 0 -%}
#if defined({{ group.protect | join(") && defined(") }})
{% for enum in group.items -%}
#define {{ enum.name }} {{ enum.value }}{% if enum.comment %} /* {{ enum.comment }} */{% endif %}
{% endfor -%}
#endif
{% else -%}
{% for enum in group.items -%}
#define {{ enum.name }} {{ enum.value }}{% if enum.comment %} /* {{ enum.comment }} */{% endif %}
{% endfor -%}
{% endif -%}
{% endfor -%}
{% endif -%}
{%- if fs.enum_groups | length > 0 %}
/* ---- Vulkan enum groups -------------------------------------------------- */
{% for group in fs.enum_groups %}
{%- if group.bitwidth == 64 %}
/* {{ group.name }} — 64-bit; uses enum : uint64_t in C++/C23, static const otherwise. */
#if GLOAM_HAS_ENUM_BASE_TYPE
typedef enum {{ group.name }} : uint64_t {
{%- for val in group.values %}
    {{ val.name }} = {{ val.value }}{% if not loop.last %},{% endif %}{% if val.comment %} /* {{ val.comment }} */{% endif %}
{%- endfor %}
} {{ group.name }};
#else
typedef uint64_t {{ group.name }};
{%- for val in group.values %}
static const {{ group.name }} {{ val.name | rjust(48) }} = {{ val.literal_value | ull }};{% if val.comment %} /* {{ val.comment }} */{% endif %}
{%- endfor %}
#endif

{%- else %}
typedef enum {{ group.name }} {
{%- for val in group.values %}
    {{ val.name }} = {{ val.value }}{% if val.comment %} /* {{ val.comment }} */{% endif %},
{%- endfor %}
    {{ group.name | vk_max_enum_name }} = 0x7FFFFFFF
} {{ group.name }};
{% endif -%}
{% endfor -%}
{% endif %}
/* ---- Types ----------------------------------------------------------------
 * Emitted in topological dependency order. Consecutive types sharing the
 * same platform guard are coalesced into a single #ifdef/#endif block.
 */
{% for group in fs.type_groups -%}
{% if group.protect | length > 0 -%}
#if defined({{ group.protect | join(") && defined(") }})
{% endif -%}
{% for type in group.items -%}
{{ type.raw_c }}
{% if not loop.last %}
{% endif -%}
{% endfor -%}
{% if group.protect | length > 0 -%}
#endif
{% endif -%}
{% if not loop.last %}
{% endif -%}
{% endfor %}

/* ---- PFN typedefs -------------------------------------------------------- */
{% if fs.is_gl_family -%}
{% for group in fs.cmd_pfn_groups -%}
{{- u.protect_begin(group.protect) -}}
{% for cmd in group.items -%}
typedef {{ cmd.return_type }} (APIENTRYP {{ cmd.pfn_type }})({{ cmd.params_str }});
{% endfor -%}
{{ u.protect_end(group.protect) -}}
{% endfor -%}
{% else %}
{% for group in fs.cmd_pfn_groups -%}
{{- u.protect_begin(group.protect) -}}
{% for cmd in group.items -%}
typedef {{ cmd.return_type }} (VKAPI_PTR *{{ cmd.pfn_type }})({{ cmd.params_str }});
{% endfor -%}
{{- u.protect_end(group.protect) -}}
{% endfor -%}
{% endif %}
{%- endif %}

/* ---- Context struct ------------------------------------------------------
 * Three anonymous unions give both indexed (array) and named (struct member)
 * access to the same memory, at zero runtime cost.
 *
 * featArray / extArray: unsigned char flags, one per feature / extension.
 * pfnArray: function pointer slots, one per command.
 *
 * Anonymous structs inside the unions are a C11 / GCC extension; they are
 * universally supported on our target compilers.
 */
typedef struct {{ fs.context_name }} {
    union {
        unsigned char featArray[{{ fs.features | length }}];
        struct {
{%- for feat in fs.features %}
        /* {{ feat.index | rjust(4) }} */ unsigned char {{ feat.short_name }};
{%- endfor %}
        };
    };
{% if fs.extensions | length > 0 %}
    union {
        unsigned char extArray[{{ fs.extensions | length }}];
        struct {
{%- for ext in fs.extensions %}
        /* {{ ext.index | rjust(4) }} */ unsigned char {{ ext.short_name | c_ident }};
{%- endfor %}
        };
    };
{% endif %}
    union {
        void *pfnArray[{{ fs.commands | length }}];
        struct {
{%- for group in fs.cmd_pfn_groups %}
{%- if group.protect | length > 0 %}
#if defined({{ group.protect | join(") && defined(") }})
{%- for cmd in group.items %}
        /* {{ cmd.index | rjust(4) }} */ {{ cmd.pfn_type }} {{ cmd.short_name }};
{%- endfor %}
#else
{%- for cmd in group.items %}
        /* {{ cmd.index | rjust(4) }} */ void *_pad{{ cmd.index }};
{%- endfor %}
#endif
{%- else %}
{%- for cmd in group.items %}
        /* {{ cmd.index | rjust(4) }} */ {{ cmd.pfn_type }} {{ cmd.short_name }};
{%- endfor %}
{%- endif %}
{%- endfor %}
        };
    };

    /* Built-in loader library handle. Set by gloamVulkanInitialize*Context /
     * gloamLoaderLoad*Context when it opens the platform library. If the
     * caller pre-populates this field, the open is skipped and ownership is
     * NOT taken (gloam will not close it). Present on all context types so
     * user code can use a single field name regardless of API.
     */
    void *gloam_loader_handle;

    /* Non-zero if gloam opened the library handle itself and is responsible
     * for closing it in gloamVulkanFinalize / gloamLoaderUnload.
     */
    uint8_t gloam_loader_owns_handle;
{% if fs.is_vulkan %}
    /* The last VkInstance this context loaded entry points from */
    VkInstance vk_loaded_instance;
    /* The last VkDevice this context loaded entry points from */
    VkDevice vk_loaded_device;
    /* Vulkan discovery-path metadata — used by gloamLoaderLoadVulkanContext to make
     * repeated calls additive without re-enumerating already-cached scopes.
     */
    uint32_t vk_instance_version;    /* cached EnumerateInstanceVersion result        */
    uint32_t vk_device_version;      /* cached GetPhysicalDeviceProperties.apiVersion */
    uint8_t  vk_found_instance_exts; /* set once instance extensions enumerated       */
    uint8_t  vk_found_device_exts;   /* set once device extensions enumerated         */
{% endif -%}
} {{ fs.context_name }};

/* Global context instance — a value, not a pointer, so the compiler knows
 * its address is fixed and does not re-load it on every access.
 */
extern {{ fs.context_name }} gloam_{{ fs.spec_name }}_context;

/* ---- Feature presence macros --------------------------------------------
 * Test whether a versioned feature was detected at load time.
 */
{%- for feat in fs.features %}
#define GLOAM_{{ feat.full_name }} (gloam_{{ fs.spec_name }}_context.{{ feat.short_name }})
{%- endfor %}

/* ---- Extension presence macros ------------------------------------------ */
{% for group in fs.ext_guard_groups %}
{{- u.protect_begin(group.protect) -}}
{% for ext in group.items -%}
#define GLOAM_{{ ext.name }} (gloam_{{ fs.spec_name }}_context.{{ ext.short_name | c_ident }})
{% endfor -%}
{{- u.protect_end(group.protect) -}}
{%- endfor %}

/* ---- Dispatch ------------------------------------------------------------ */
{% if fs.is_vulkan -%}
{# Vulkan: force-inlined function wrappers that dispatch through the global
   context struct. Unlike #define macros, these don't pollute the preprocessor
   namespace (avoids collisions with VMA, etc.). #}
{% for group in fs.cmd_pfn_groups -%}
{% if group.protect | length > 0 -%}
#if defined({{ group.protect | join(") && defined(") }})
{% endif -%}
{% for cmd in group.items -%}
GLOAM_FORCE_INLINE {{ cmd.return_type }} {{ cmd.name }}({{ cmd.params_str }}) {
    {% if cmd.return_type != "void" %}return {% endif %}gloam_{{ fs.spec_name }}_context.{{ cmd.short_name }}({{ cmd.params | map(attribute="name") | join(", ") }});
}
{% endfor -%}
{% if group.protect | length > 0 -%}
#endif
{% endif -%}
{% endfor -%}
{% else -%}
{# GL/EGL/GLX/WGL: macro dispatch with IntelliSense prototypes for IDE support. #}
#ifdef __INTELLISENSE__
{% for group in fs.cmd_pfn_groups -%}
{% if group.protect | length > 0 -%}
#if defined({{ group.protect | join(") && defined(") }})
{% endif -%}
{% for cmd in group.items -%}
{{ cmd.return_type }} {{ cmd.name }}({{ cmd.params_str }});
{% endfor -%}
{% if group.protect | length > 0 -%}
#endif
{% endif -%}
{% endfor -%}
#else
{% for group in fs.cmd_pfn_groups -%}
{% if group.protect | length > 0 -%}
#if defined({{ group.protect | join(") && defined(") }})
{% endif -%}
{% for cmd in group.items -%}
#define {{ cmd.name }} (gloam_{{ fs.spec_name }}_context.{{ cmd.short_name }})
{% endfor -%}
{% if group.protect | length > 0 -%}
#endif
{% endif -%}
{% endfor -%}
#endif /* __INTELLISENSE__ */
{% endif -%}

/* ---- API declarations ---------------------------------------------------- */

#ifndef GLOAM_DEFINED_CALLBACK_TYPES_
#define GLOAM_DEFINED_CALLBACK_TYPES_
/* Opaque function pointer type — the common return type for all load
 * callbacks. Callers cast to the specific PFN type they need.
 */
typedef void (*GloamAPIProc)(void);

/* Load function pointer type (GL / EGL / GLX / WGL). */
typedef GloamAPIProc (*GloamLoadFunc)(const char *name);
#endif
{% for api in fs.apis -%}
{%- if fs.spec_name in ["gl", "gles1", "gles2", "glcore"] %}
int gloamLoad{{ api | api_display }}Context({{ u.ctx_arg(', ') }}GloamLoadFunc getProcAddr);
int gloamLoad{{ api | api_display }}(GloamLoadFunc getProcAddr);
{%- elif fs.spec_name == "egl" %}
int gloamLoad{{ api | api_display }}Context({{ u.ctx_arg(', ') }}EGLDisplay display, GloamLoadFunc getProcAddr);
int gloamLoad{{ api | api_display }}(EGLDisplay display, GloamLoadFunc getProcAddr);
{%- elif fs.spec_name == "glx" %}
int gloamLoad{{ api | api_display }}Context({{ u.ctx_arg(', ') }}Display *display, int screen, GloamLoadFunc getProcAddr);
int gloamLoad{{ api | api_display }}(Display *display, int screen, GloamLoadFunc getProcAddr);
{%- elif fs.spec_name == "wgl" %}
int gloamLoad{{ api | api_display }}Context({{ u.ctx_arg(', ') }}HDC hdc, GloamLoadFunc getProcAddr);
int gloamLoad{{ api | api_display }}(HDC hdc, GloamLoadFunc getProcAddr);
{%- elif fs.spec_name == "vk" %}
/* ---- Vulkan enabled API (Volk-like) ----------------------------------------
 * Phased loading: Initialize → LoadInstance → LoadDevice.
 * The caller owns extension discovery and tells gloam what was enabled.
 *
 * Phase 0 — Initialize: open libvulkan and load the handful of Global-scope
 * PFNs needed to create an instance (vkCreateInstance, vkEnumerateInstance*).
 * If library_handle is non-NULL, use it without taking ownership; if NULL,
 * dlopen the platform default and take ownership.
 *
 * Phase 1 — LoadInstance: load Global + Instance-scope PFNs for core features
 * and enabled instance extensions. Set featArray from api_version
 * (VK_MAKE_API_VERSION or VK_API_VERSION_x_y). Set extArray for enabled
 * instance extensions. Resolve aliases.
 *
 * Phase 1.5 (optional) — LoadPhysicalDeviceExtension(s): pre-load
 * Instance-scope PFNs for device extensions the application wants to query
 * before creating a VkDevice (e.g. vkGetPhysicalDeviceFragmentShadingRatesKHR
 * from VK_KHR_fragment_shading_rate). Does NOT set extArray.
 *
 * Phase 2 — LoadDevice: load PFNs for enabled device extensions (all scopes).
 * Device-scope commands use vkGetDeviceProcAddr for the fast path; Instance-
 * scope commands in device extensions use vkGetInstanceProcAddr. Update
 * featArray from the device's api_version. Set extArray for enabled device
 * extensions. Resolve aliases.
 *
 * Finalize: close library handle if gloam owns it, zero the context.
 */
{% if loader -%}
int  gloamVulkanInitializeContext({{ u.ctx_arg(', ') }}void *library_handle);
int  gloamVulkanInitialize(void *library_handle);
{% endif -%}
void gloamVulkanInitializeCustomContext({{ u.ctx_arg(', ') }}PFN_vkGetInstanceProcAddr getInstanceProcAddr);
void gloamVulkanInitializeCustom(PFN_vkGetInstanceProcAddr getInstanceProcAddr);
uint32_t gloamVulkanGetInstanceVersionContext({{ u.ctx_arg() }});
uint32_t gloamVulkanGetInstanceVersion(void);
int  gloamVulkanLoadInstanceContext({{ u.ctx_arg(', ') }}VkInstance instance, uint32_t api_version, uint32_t num_instance_extensions, const char *const *instance_extensions);
int  gloamVulkanLoadInstance(VkInstance instance, uint32_t api_version, uint32_t num_instance_extensions, const char *const *instance_extensions);
VkInstance gloamVulkanGetLoadedInstanceContext({{ u.ctx_arg() }});
VkInstance gloamVulkanGetLoadedInstance(void);
void gloamVulkanLoadPhysicalDeviceExtensionContext({{ u.ctx_arg(', ') }}const char *device_extension);
void gloamVulkanLoadPhysicalDeviceExtension(const char *device_extension);
void gloamVulkanLoadPhysicalDeviceExtensionsContext({{ u.ctx_arg(', ') }}uint32_t num_device_extensions, const char *const *device_extensions);
void gloamVulkanLoadPhysicalDeviceExtensions(uint32_t num_device_extensions, const char *const *device_extensions);
int  gloamVulkanLoadDeviceContext({{ u.ctx_arg(', ') }}VkDevice device, VkPhysicalDevice physical_device, uint32_t num_device_extensions, const char *const *device_extensions);
int  gloamVulkanLoadDevice(VkDevice device, VkPhysicalDevice physical_device, uint32_t num_device_extensions, const char *const *device_extensions);
VkDevice gloamVulkanGetLoadedDeviceContext({{ u.ctx_arg() }});
VkDevice gloamVulkanGetLoadedDevice(void);
void gloamVulkanFinalizeContext({{ u.ctx_arg() }});
void gloamVulkanFinalize(void);
{% endif %}
{% endfor -%}

{%- if loader -%}
/* Built-in loader: opens the platform library if needed and calls the
 * appropriate load function for you. Non-Vulkan loaders call the detection-
 * based gloamLoad* functions. Vulkan loaders handle all extension detection
 * and PFN loading internally.
 * Each Load function may be called multiple times (additive).
 */
{%- for api in fs.apis %}
{%- if fs.spec_name in ["gl", "gles1", "gles2", "glcore"] %}
int  gloamLoaderLoad{{ api | api_display }}Context({{ u.ctx_arg() }});
int  gloamLoaderLoad{{ api | api_display }}(void);
void gloamLoaderUnload{{ api | api_display }}Context({{ u.ctx_arg() }});
void gloamLoaderUnload{{ api | api_display }}(void);
void gloamLoaderReset{{ api | api_display }}Context({{ u.ctx_arg() }});
void gloamLoaderReset{{ api | api_display }}(void);
{%- elif fs.spec_name == "egl" %}
int  gloamLoaderLoad{{ api | api_display }}Context({{ u.ctx_arg(', ') }}EGLDisplay display);
int  gloamLoaderLoad{{ api | api_display }}(EGLDisplay display);
void gloamLoaderUnload{{ api | api_display }}Context({{ u.ctx_arg() }});
void gloamLoaderUnload{{ api | api_display }}(void);
void gloamLoaderReset{{ api | api_display }}Context({{ u.ctx_arg() }});
void gloamLoaderReset{{ api | api_display }}(void);
{%- elif fs.spec_name == "glx" %}
int  gloamLoaderLoad{{ api | api_display }}Context({{ u.ctx_arg(', ') }}Display *display, int screen);
int  gloamLoaderLoad{{ api | api_display }}(Display *display, int screen);
void gloamLoaderUnload{{ api | api_display }}Context({{ u.ctx_arg() }});
void gloamLoaderUnload{{ api | api_display }}(void);
void gloamLoaderReset{{ api | api_display }}Context({{ u.ctx_arg() }});
void gloamLoaderReset{{ api | api_display }}(void);
{%- elif fs.spec_name == "wgl" %}
int  gloamLoaderLoad{{ api | api_display }}Context({{ u.ctx_arg(', ') }}HDC hdc);
int  gloamLoaderLoad{{ api | api_display }}(HDC hdc);
void gloamLoaderUnload{{ api | api_display }}Context({{ u.ctx_arg() }});
void gloamLoaderUnload{{ api | api_display }}(void);
void gloamLoaderReset{{ api | api_display }}Context({{ u.ctx_arg() }});
void gloamLoaderReset{{ api | api_display }}(void);
{%- elif fs.spec_name == "vk" %}
int  gloamLoaderLoadVulkanContext({{ u.ctx_arg(', ') }}VkInstance instance, VkPhysicalDevice physical_device, VkDevice device);
int  gloamLoaderLoadVulkan(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device);
void gloamLoaderUnloadVulkanContext({{ u.ctx_arg() }});
void gloamLoaderUnloadVulkan(void);
void gloamLoaderResetVulkanContext({{ u.ctx_arg() }});
void gloamLoaderResetVulkan(void);
{%- endif %}
{% endfor -%}
{% endif %}
#ifdef __cplusplus
}
#endif
{% if fs.spec_name == "glx" %}
#endif /* GLOAM_PLATFORM_LINUX */
{% elif fs.spec_name == "wgl" %}
#endif /* GLOAM_PLATFORM_WINDOWS */
{% endif %}
#endif /* GLOAM_{{ guard }} */