big architectural rework

This commit is contained in:
2025-11-04 00:11:15 +01:00
parent 4e9fdac9b7
commit 446ac9c1f0
13 changed files with 348 additions and 306 deletions

View File

@@ -1,40 +1,30 @@
const std = @import("std");
const vk = @import("vulkan");
const base = @import("base");
const PhysicalDevice = @import("PhysicalDevice.zig");
const soft_physical_device = @import("physical_device.zig");
const dispatchable = base.dispatchable;
const Dispatchable = base.Dispatchable;
const VulkanAllocator = base.VulkanAllocator;
const Self = @This();
export fn __vkImplInstanceInit(base_instance: *base.Instance, allocator: *const std.mem.Allocator) ?*anyopaque {
return realVkImplInstanceInit(base_instance, allocator.*) catch return null;
export fn __vkImplInstanceInit(base_instance: *base.Instance, allocator: *const std.mem.Allocator, infos: *const vk.InstanceCreateInfo) ?*anyopaque {
return realVkImplInstanceInit(base_instance, allocator.*, infos) catch return null;
}
// Pure Zig implementation to leverage `errdefer` and avoid memory leaks or complex resources handling
fn realVkImplInstanceInit(base_instance: *base.Instance, allocator: std.mem.Allocator) !?*anyopaque {
base_instance.dispatch_table = .{
.destroyInstance = deinit,
.enumerateInstanceVersion = null,
//.enumerateInstanceLayerProperties = null,
.enumerateInstanceExtensionProperties = null,
};
fn realVkImplInstanceInit(instance: *base.Instance, allocator: std.mem.Allocator, infos: *const vk.InstanceCreateInfo) !?*anyopaque {
_ = infos;
// Software driver only has one physical device (the CPU)
const dispatchable_physical_device = try PhysicalDevice.init(base_instance, allocator);
errdefer dispatchable_physical_device.destroy(allocator);
const physical_device = try Dispatchable(base.PhysicalDevice).create(allocator, .{instance});
errdefer physical_device.destroy(allocator);
try base_instance.physical_devices.append(allocator, @enumFromInt(dispatchable_physical_device.toHandle()));
try soft_physical_device.setup(allocator, physical_device.object);
try instance.physical_devices.append(allocator, physical_device.toVkHandle(vk.PhysicalDevice));
const self = try allocator.create(Self);
errdefer allocator.destroy(self);
return @ptrCast(self);
}
pub fn deinit(base_instance: *const base.Instance, allocator: std.mem.Allocator) !void {
for (base_instance.physical_devices.items) |physical_device| {
const dispatchable_physical_device = try dispatchable.fromHandle(base.PhysicalDevice, @intFromEnum(physical_device));
dispatchable_physical_device.destroy(allocator);
}
}

View File

@@ -1,44 +0,0 @@
const std = @import("std");
const vk = @import("vulkan");
const Instance = @import("Instance.zig");
const base = @import("base");
const root = @import("lib.zig");
const cpuinfo = @import("cpuinfo");
const dispatchable = base.dispatchable;
const Self = @This();
pub fn init(instance: *const base.Instance, allocator: std.mem.Allocator) !*dispatchable.Dispatchable(base.PhysicalDevice) {
const dispatchable_physical_device = try base.PhysicalDevice.init(instance, allocator);
errdefer dispatchable_physical_device.destroy(allocator);
const base_physical_device = dispatchable_physical_device.object;
base_physical_device.props.api_version = @bitCast(root.VULKAN_VERSION);
base_physical_device.props.driver_version = @bitCast(root.DRIVER_VERSION);
base_physical_device.props.device_id = root.DEVICE_ID;
base_physical_device.props.device_type = .cpu;
base_physical_device.mem_props.memory_type_count = 1;
base_physical_device.mem_props.memory_types[0] = .{
.heap_index = 0,
.property_flags = .{
.host_visible_bit = true,
.host_coherent_bit = true,
},
};
base_physical_device.mem_props.memory_heap_count = 1;
base_physical_device.mem_props.memory_heaps[0] = .{
.size = std.process.totalSystemMemory() catch 0,
.flags = .{}, // Host memory
};
const info = try cpuinfo.get(allocator);
defer info.deinit(allocator);
var writer = std.io.Writer.fixed(base_physical_device.props.device_name[0 .. vk.MAX_PHYSICAL_DEVICE_NAME_SIZE - 1]);
try writer.print("{s} [Soft Vulkan Driver]", .{info.name});
return dispatchable_physical_device;
}

View File

@@ -3,7 +3,6 @@ const vk = @import("vulkan");
const base = @import("base");
const Instance = @import("Instance.zig");
const PhysicalDevice = @import("PhysicalDevice.zig");
pub const VULKAN_VERSION = vk.makeApiVersion(0, 1, 0, 0);
pub const DRIVER_VERSION = vk.makeApiVersion(0, 0, 0, 1);
@@ -12,5 +11,4 @@ pub const DEVICE_ID = 0x600DCAFE;
comptime {
_ = base;
_ = Instance;
_ = PhysicalDevice;
}

33
src/soft/physical_device.zig git.filemode.normal_file
View File

@@ -0,0 +1,33 @@
const std = @import("std");
const vk = @import("vulkan");
const Instance = @import("Instance.zig");
const base = @import("base");
const root = @import("lib.zig");
const cpuinfo = @import("cpuinfo");
pub fn setup(allocator: std.mem.Allocator, physical_device: *base.PhysicalDevice) !void {
physical_device.props.api_version = @bitCast(root.VULKAN_VERSION);
physical_device.props.driver_version = @bitCast(root.DRIVER_VERSION);
physical_device.props.device_id = root.DEVICE_ID;
physical_device.props.device_type = .cpu;
physical_device.mem_props.memory_type_count = 1;
physical_device.mem_props.memory_types[0] = .{
.heap_index = 0,
.property_flags = .{
.host_visible_bit = true,
.host_coherent_bit = true,
},
};
physical_device.mem_props.memory_heap_count = 1;
physical_device.mem_props.memory_heaps[0] = .{
.size = std.process.totalSystemMemory() catch 0,
.flags = .{}, // Host memory
};
const info = try cpuinfo.get(allocator);
defer info.deinit(allocator);
var writer = std.io.Writer.fixed(physical_device.props.device_name[0 .. vk.MAX_PHYSICAL_DEVICE_NAME_SIZE - 1]);
try writer.print("{s} [Soft Vulkan Driver]", .{info.name});
}