adding object base

This commit is contained in:
2025-10-28 23:08:13 +01:00
parent 8faba19927
commit 610f377fac
7 changed files with 111 additions and 44 deletions

View File

@@ -13,62 +13,49 @@ const implementations = [_]ImplementationDesc{
},
};
pub fn build(b: *std.Build) !void {
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const libvulkan_mod = b.createModule(.{
const common_mod = b.createModule(.{
.root_source_file = b.path("src/vulkan/lib.zig"),
.target = target,
.optimize = optimize,
});
const vulkan_headers = b.dependency("vulkan_headers", .{});
const vulkan = b.dependency("vulkan_zig", .{
.registry = b.dependency("vulkan_headers", .{}).path("registry/vk.xml"),
.registry = vulkan_headers.path("registry/vk.xml"),
}).module("vulkan-zig");
libvulkan_mod.addImport("vulkan", vulkan);
const libvulkan = b.addLibrary(.{
.name = "vulkan",
.root_module = libvulkan_mod,
.linkage = .dynamic,
});
b.installArtifact(libvulkan);
common_mod.addImport("vulkan", vulkan);
common_mod.addSystemIncludePath(vulkan_headers.path("include"));
for (implementations) |impl| {
b.installArtifact(try buildImplementation(b, target, optimize, &impl, vulkan));
}
const libvulkan_tests = b.addTest(.{
.root_module = libvulkan_mod,
});
const run_libvulkan_tests = b.addRunArtifact(libvulkan_tests);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_libvulkan_tests.step);
}
fn buildImplementation(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
desc: *const ImplementationDesc,
vulkan_bindings: *std.Build.Module,
) !*Step.Compile {
const lib_mod = b.createModule(.{
.root_source_file = b.path(desc.root_source_file),
.root_source_file = b.path(impl.root_source_file),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "common", .module = common_mod },
.{ .name = "vulkan", .module = vulkan },
},
});
lib_mod.addImport("vulkan", vulkan_bindings);
lib_mod.addSystemIncludePath(vulkan_headers.path("include"));
return b.addLibrary(.{
.name = try std.fmt.allocPrint(b.allocator, "vulkan_{s}", .{desc.name}),
const lib = b.addLibrary(.{
.name = b.fmt("vulkan_{s}", .{impl.name}),
.root_module = lib_mod,
.linkage = .dynamic,
});
b.installArtifact(lib);
const lib_tests = b.addTest(.{ .root_module = lib_mod });
const run_tests = b.addRunArtifact(lib_tests);
const test_step = b.step(b.fmt("test-{s}", .{impl.name}), b.fmt("Run lib{s} tests", .{impl.name}));
test_step.dependOn(&run_tests.step);
}
}

View File

@@ -0,0 +1,7 @@
const std = @import("std");
const common = @import("common");
test {
std.testing.refAllDeclsRecursive(@This());
}

9
src/vulkan/Instance.zig git.filemode.normal_file
View File

@@ -0,0 +1,9 @@
const vk = @import("vulkan");
const Object = @import("object.zig").Object;
pub const Instance = extern struct {
const Self = @This();
const ObjectType: vk.ObjectType = .instance;
object: Object,
};

View File

14
src/vulkan/PhysicalDevice.zig git.filemode.normal_file
View File

@@ -0,0 +1,14 @@
const vk = @import("vulkan");
const Instance = @import("Instance.zig").Instance;
const Object = @import("object.zig").Object;
pub const PhysicalDevice = extern struct {
const Self = @This();
const ObjectType: vk.ObjectType = .physical_device;
object: Object,
instance: *Instance,
props: vk.PhysicalDeviceProperties,
queue_families: [3]vk.QueueFamilyProperties,
};

View File

@@ -0,0 +1,8 @@
const std = @import("std");
pub const Instance = @import("Instance.zig");
pub const PhysicalDevice = @import("PhysicalDevice.zig");
test {
std.testing.refAllDeclsRecursive(@This());
}

42
src/vulkan/object.zig git.filemode.normal_file
View File

@@ -0,0 +1,42 @@
const vk = @import("vulkan");
const c = @cImport({
@cInclude("vulkan/vk_icd.h");
});
pub const Object = extern struct {
const Self = @This();
loader_data: c.VK_LOADER_DATA,
kind: vk.ObjectType,
owner: ?*anyopaque,
// VK_EXT_debug_utils
name: ?[]const u8,
pub fn init(owner: ?*anyopaque, kind: vk.ObjectType) Self {
return .{
.loader_data = c.ICD_LOADER_MAGIC,
.kind = kind,
.owner = owner,
.name = null,
};
}
};
pub inline fn fromHandle(comptime T: type, comptime VkT: type, handle: VkT) !*T {
if (handle == .null_handle) {
return error.NullHandle;
}
if (!@hasDecl(T, "object")) {
return error.NotAnObject;
}
if (!@hasDecl(T, "ObjectType") || @TypeOf(T.ObjectType) != vk.ObjectType) {
@panic("Object type \"" ++ @typeName(T) ++ "\" is malformed.");
}
const dispatchable: *T = @ptrFromInt(@intFromEnum(handle));
if (dispatchable.object.kind != T.ObjectType) {
return error.InvalidObjectType;
}
return dispatchable;
}