Source code for simvx.graphics._vulkan_compat

"""Vulkan library compatibility: ensures libvulkan loads on all platforms.

The ``vulkan`` Python package only searches for ``libvulkan.so.1`` on Linux,
but Android ships ``libvulkan.so`` (no version suffix). Pre-loading the library
via ctypes before ``vulkan`` is imported ensures cffi's ``ffi.dlopen()`` finds it.

Import this module **before** ``import vulkan`` on any platform where the library
name may differ from the default list.
"""

import ctypes
import logging
import sys

log = logging.getLogger(__name__)

def _preload_vulkan() -> None:
    """Attempt to pre-load the Vulkan shared library using platform-specific names."""
    if sys.platform == "win32":
        return  # vulkan-1.dll is handled by the vulkan package

    # Android: libvulkan.so (no .so.1 suffix)
    # Standard Linux: libvulkan.so.1 (handled by vulkan package)
    # macOS: libvulkan.dylib or libMoltenVK.dylib (handled by vulkan package)
    extra_names = []
    if sys.platform == "linux":
        extra_names = ["libvulkan.so"]
    elif sys.platform == "darwin":
        extra_names = ["libMoltenVK.dylib"]

    for name in extra_names:
        try:
            ctypes.CDLL(name)
            log.debug("Pre-loaded Vulkan library: %s", name)
            return
        except OSError:
            continue

_preload_vulkan()