adding device memory support, fences base

This commit is contained in:
2025-11-09 00:34:52 +01:00
parent 4b23abe795
commit e89d1ff8d9
14 changed files with 381 additions and 27 deletions

View File

@@ -1,6 +1,6 @@
const std = @import("std");
const vk = @import("vulkan");
const Instance = @import("Instance.zig");
const SoftDeviceMemory = @import("SoftDeviceMemory.zig");
const base = @import("base");
const VkError = base.VkError;
@@ -9,6 +9,7 @@ const Self = @This();
pub const Interface = base.Device;
interface: Interface,
device_allocator: std.heap.ThreadSafeAllocator,
pub fn create(physical_device: *base.PhysicalDevice, allocator: std.mem.Allocator, infos: *const vk.DeviceCreateInfo) VkError!*Self {
const self = allocator.create(Self) catch return VkError.OutOfHostMemory;
@@ -17,16 +18,29 @@ pub fn create(physical_device: *base.PhysicalDevice, allocator: std.mem.Allocato
var interface = try Interface.init(allocator, physical_device, infos);
interface.dispatch_table = &.{
.allocateMemory = allocateMemory,
.freeMemory = freeMemory,
.destroy = destroy,
};
self.* = .{
.interface = interface,
.device_allocator = .{ .child_allocator = std.heap.c_allocator }, // TODO: better device allocator base
};
return self;
}
pub fn allocateMemory(interface: *Interface, allocator: std.mem.Allocator, infos: *const vk.MemoryAllocateInfo) VkError!*base.DeviceMemory {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
const device_memory = try SoftDeviceMemory.create(self, allocator, infos.allocation_size, infos.memory_type_index);
return &device_memory.interface;
}
pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) VkError!void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
allocator.destroy(self);
}
pub fn freeMemory(_: *Interface, allocator: std.mem.Allocator, device_memory: *base.DeviceMemory) VkError!void {
device_memory.destroy(allocator);
}

49
src/soft/SoftDeviceMemory.zig git.filemode.normal_file
View File

@@ -0,0 +1,49 @@
const std = @import("std");
const vk = @import("vulkan");
const SoftDevice = @import("SoftDevice.zig");
const base = @import("base");
const VkError = base.VkError;
const Self = @This();
pub const Interface = base.DeviceMemory;
interface: Interface,
data: []u8,
pub fn create(device: *SoftDevice, allocator: std.mem.Allocator, size: vk.DeviceSize, memory_type_index: u32) VkError!*Self {
const self = allocator.create(Self) catch return VkError.OutOfHostMemory;
errdefer allocator.destroy(self);
var interface = try Interface.init(&device.interface, size, memory_type_index);
interface.vtable = &.{
.destroy = destroy,
.map = map,
.unmap = unmap,
};
self.* = .{
.interface = interface,
.data = device.device_allocator.allocator().alignedAlloc(u8, std.mem.Alignment.@"16", size) catch return VkError.OutOfDeviceMemory,
};
return self;
}
pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
allocator.destroy(self);
}
pub fn map(interface: *Interface, offset: vk.DeviceSize, size: vk.DeviceSize) VkError!?*anyopaque {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
if (offset >= self.data.len or (size != vk.WHOLE_SIZE and offset + size > self.data.len)) {
return VkError.MemoryMapFailed;
}
interface.is_mapped = true;
return @ptrCast(&self.data[offset]);
}
pub fn unmap(interface: *Interface) void {
interface.is_mapped = false;
}

83
src/soft/SoftFence.zig git.filemode.normal_file
View File

@@ -0,0 +1,83 @@
const std = @import("std");
const vk = @import("vulkan");
const base = @import("base");
const VkError = base.VkError;
const Device = base.Device;
const Self = @This();
pub const Interface = base.Fence;
interface: Interface,
mutex: std.Thread.Mutex,
condition: std.Thread.Condition,
is_signaled: bool,
pub fn create(device: *const Device, allocator: std.mem.Allocator, info: *const vk.FenceCreateInfo) VkError!*Self {
const self = allocator.create(Self) catch return VkError.OutOfHostMemory;
errdefer allocator.destroy(self);
var interface = try Interface.init(device, info);
interface.vtable = &.{
.destroy = destroy,
.getStatus = getStatus,
.reset = reset,
.signal = signal,
.wait = wait,
};
self.* = .{
.interface = interface,
.mutex = std.Thread.Mutex{},
.condition = std.Thread.Condition{},
.is_signaled = info.flags.signaled_bit,
};
return self;
}
pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
allocator.destroy(self);
}
pub fn getStatus(interface: *Interface) VkError!void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
self.mutex.lock();
defer self.mutex.unlock();
if (!self.is_signaled) {
return VkError.NotReady;
}
}
pub fn reset(interface: *Interface) VkError!void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
self.mutex.lock();
defer self.mutex.unlock();
self.is_signaled = false;
}
pub fn signal(interface: *Interface) VkError!void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
{
self.mutex.lock();
defer self.mutex.unlock();
self.is_signaled = true;
}
self.condition.broadcast();
}
pub fn wait(interface: *Interface, timeout: u64) VkError!void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
if (self.is_signaled) return;
if (timeout == 0) return VkError.Timeout;
self.mutex.lock();
defer self.mutex.unlock();
if (timeout == std.math.maxInt(@TypeOf(timeout))) {
self.condition.wait(self.mutex);
} else {
self.condition.timedWait(self.mutex, timeout) catch return VkError.Timeout;
}
}

View File

@@ -1,7 +1,7 @@
const std = @import("std");
const vk = @import("vulkan");
const base = @import("base");
const PhysicalDevice = @import("PhysicalDevice.zig");
const SoftPhysicalDevice = @import("SoftPhysicalDevice.zig");
const Dispatchable = base.Dispatchable;
@@ -27,9 +27,9 @@ pub fn create(allocator: std.mem.Allocator, infos: *const vk.InstanceCreateInfo)
fn requestPhysicalDevices(interface: *Interface, allocator: std.mem.Allocator) VkError!void {
// Software driver only has one physical device (the CPU)
const physical_device = try PhysicalDevice.create(allocator, interface);
const physical_device = try SoftPhysicalDevice.create(allocator, interface);
errdefer physical_device.interface.releasePhysicalDevice(allocator) catch {};
interface.physical_devices.append(allocator, try Dispatchable(PhysicalDevice.Interface).wrap(allocator, &physical_device.interface)) catch return VkError.OutOfHostMemory;
interface.physical_devices.append(allocator, try Dispatchable(SoftPhysicalDevice.Interface).wrap(allocator, &physical_device.interface)) catch return VkError.OutOfHostMemory;
}
fn releasePhysicalDevices(interface: *Interface, allocator: std.mem.Allocator) VkError!void {

View File

@@ -4,8 +4,7 @@ const base = @import("base");
const root = @import("lib.zig");
const cpuinfo = @import("cpuinfo");
const Device = @import("Device.zig");
const Instance = @import("Instance.zig");
const SoftDevice = @import("SoftDevice.zig");
const VkError = base.VkError;
@@ -90,8 +89,8 @@ pub fn create(allocator: std.mem.Allocator, instance: *const base.Instance) VkEr
return self;
}
pub fn createDevice(interface: *Interface, allocator: std.mem.Allocator, infos: *const vk.DeviceCreateInfo) VkError!*Device.Interface {
const device = try Device.create(interface, allocator, infos);
pub fn createDevice(interface: *Interface, allocator: std.mem.Allocator, infos: *const vk.DeviceCreateInfo) VkError!*SoftDevice.Interface {
const device = try SoftDevice.create(interface, allocator, infos);
return &device.interface;
}

View File

@@ -2,9 +2,12 @@ const std = @import("std");
const vk = @import("vulkan");
pub const base = @import("base");
pub const Instance = @import("Instance.zig");
const Device = @import("Device.zig");
const PhysicalDevice = @import("PhysicalDevice.zig");
pub const SoftInstance = @import("SoftInstance.zig");
pub const SoftDevice = @import("SoftDevice.zig");
pub const SoftDeviceMemory = @import("SoftDeviceMemory.zig");
pub const SoftPhysicalDevice = @import("SoftPhysicalDevice.zig");
pub const Instance = SoftInstance;
pub const DRIVER_LOGS_ENV_NAME = base.DRIVER_LOGS_ENV_NAME;
pub const DRIVER_NAME = "Soft";