From 8c31529f8aa0689aaf71ff3b31e6e27b66058bb6 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Fri, 29 Oct 2021 16:08:56 -0700 Subject: [PATCH] glfw: make glfw.getInstanceProcAddress conform to GLFW C ABI Having `glfw.getInstanceProcAddress` conform to the GLFW C ABI is important as it is often likely to be passed into libraries which expect exactly that ABI, e.g. zig-vulkan. Fixes hexops/mach#49 Signed-off-by: Stephen Gutekanst --- glfw/src/vulkan.zig | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/glfw/src/vulkan.zig b/glfw/src/vulkan.zig index 0b062344..8fbd8695 100644 --- a/glfw/src/vulkan.zig +++ b/glfw/src/vulkan.zig @@ -94,14 +94,17 @@ pub const VKProc = fn () callconv(.C) void; /// @param[in] procname The ASCII encoded name of the function. /// @return The address of the function, or null if an error occurred. /// -/// Possible errors include glfw.Error.NotInitialized and glfw.Error.APIUnavailable. +/// To maintain ABI compatability with the C glfwGetInstanceProcAddress, as it is commonly passed +/// into libraries expecting that exact ABI, this function does not return an error. Instead, if +/// glfw.Error.NotInitialized or glfw.Error.APIUnavailable would occur this function will panic. +/// You may check glfw.vulkanSupported prior to invoking this function. /// /// @pointer_lifetime The returned function pointer is valid until the library is terminated. /// /// @thread_safety This function may be called from any thread. -pub inline fn getInstanceProcAddress(vk_instance: ?*opaque {}, proc_name: [*c]const u8) Error!?VKProc { +pub fn getInstanceProcAddress(vk_instance: ?*opaque {}, proc_name: [*c]const u8) callconv(.C) ?VKProc { const proc_address = c.glfwGetInstanceProcAddress(if (vk_instance) |v| @ptrCast(c.VkInstance, v) else null, proc_name); - try getError(); + getError() catch @panic("glfw.getInstanceProcAddress failed, not initialized or Vulkan API unavailable"); if (proc_address) |addr| return addr; return null; } @@ -218,8 +221,8 @@ test "getInstanceProcAddress" { try glfw.init(); defer glfw.terminate(); - // syntax check only, we don't have a real vulkan instance. - _ = glfw.getInstanceProcAddress(null, "foobar") catch |err| std.debug.print("failed to get vulkan instance proc address, error={}\n", .{err}); + // syntax check only, we don't have a real vulkan instance and so this function would panic. + _ = glfw.getInstanceProcAddress; } test "syntax" {