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

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;
}