Snektron / vulkan-zig

Vulkan binding generator for Zig
MIT License
500 stars 58 forks source link

[question] error: Expected `c_import.T` found `lib.T` #164

Closed heysokam closed 1 week ago

heysokam commented 1 week ago

Hi there

I'm getting this error when I compile the triangle.zig example from CLI (no zig build, just zig run {opts} thefile.zig), but the example works normally when using examples/build.zig

zig/lib/vulkan/vk.zig:25399:36: error: expected type '?*cimport.struct_VkInstance_T', found 'lib.vulkan.vk.Instance'
                if (loader(Instance.null_handle, name)) |cmd_ptr| {
                           ~~~~~~~~^~~~~~~~~~~~
zig/lib/vulkan/vk.zig:801:22: note: enum declared here
pub const Instance = enum(usize) { null_handle = 0, _ };
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bin/.cache/zig/o/7fdaa537f250ebdce1f3e435a0a5330a/cimport.zig:18643:52: note: parameter type declared here
pub extern fn glfwGetInstanceProcAddress(instance: VkInstance, procname: [*c]const u8) GLFWvkproc;
                                                   ^~~~~~~~~~
zig/triangle.zig:350:40: error: expected type '?*cimport.struct_VkInstance_T', found 'lib.vulkan.vk.Instance'
    if (glfw.vk.surface.create(instance.handle, window, null, &surface) != .success) return error.SurfaceInitFailed;
                               ~~~~~~~~^~~~~~~
zig/lib/vulkan/vk.zig:801:22: note: enum declared here
pub const Instance = enum(usize) { null_handle = 0, _ };
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bin/.cache/zig/o/7fdaa537f250ebdce1f3e435a0a5330a/cimport.zig:18645:49: note: parameter type declared here
pub extern fn glfwCreateWindowSurface(instance: VkInstance, window: ?*GLFWwindow, allocator: [*c]const VkAllocationCallbacks, surface: [*c]VkSurfaceKHR) VkResult;
                                                ^~~~~~~~~~
referenced by:
    init: zig/triangle.zig:273:39
    main: zig/triangle.zig:865:40
    6 reference(s) hidden; use '-freference-trace=8' to see all references

This is the failing code: https://github.com/heysokam/vulkan/blob/13a30424e8bbe5403f181a5ce398c0c2ee7e0b6c/zig/triangle.zig#L197-L1306

I can imagine I'm doing something wrong with my zig run file.zig compilation step/setup but can't find what it is Any ideas on how to approach it?

heysokam commented 1 week ago

Somebody sent some help my way on discord:

// @section GLFW + Vulkan Compatibility code
const vk = @import("./vulkan/vk.zig");

pub const instance = struct {
  const Func = *const fn (instance: vk.Instance, procname: [*:0]const u8) vk.PfnVoidFunction;
  pub const getProc :instance.Func= @ptrCast(&c.glfwGetInstanceProcAddress);
};

pub const surface = struct {
  const Func = *const fn (instance: vk.Instance, window :?*glfw.Window, allocator :?*const vk.AllocationCallbacks, surface :*const vk.SurfaceKHR) vk.Result;
  pub const create :surface.Func= @ptrCast(&c.glfwCreateWindowSurface);
};