From 4b23abe79511f2fe10267e88db464f94da0f26bf Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Sat, 8 Nov 2025 01:00:30 +0100 Subject: [PATCH] adding device memory base --- build.zig | 5 ++-- build.zig.zon | 14 +++++----- src/soft/PhysicalDevice.zig | 2 ++ src/vulkan/Buffer.zig | 13 +++++++++ src/vulkan/DeviceMemory.zig | 37 ++++++++++++++++++++++++ src/vulkan/Dispatchable.zig | 29 +------------------ src/vulkan/NonDispatchable.zig | 51 ++++++++++++++++++++++++++++++++++ src/vulkan/lib.zig | 1 + 8 files changed, 115 insertions(+), 37 deletions(-) create mode 100644 src/vulkan/Buffer.zig create mode 100644 src/vulkan/DeviceMemory.zig create mode 100644 src/vulkan/NonDispatchable.zig diff --git a/build.zig b/build.zig index 7dedea6..21a4191 100644 --- a/build.zig +++ b/build.zig @@ -60,7 +60,7 @@ pub fn build(b: *std.Build) void { .root_module = lib_mod, .linkage = .dynamic, }); - b.installArtifact(lib); + const lib_install_step = b.addInstallArtifact(lib, .{}); const lib_tests = b.addTest(.{ .root_module = lib_mod }); @@ -90,8 +90,9 @@ pub fn build(b: *std.Build) void { b.installArtifact(c_test_exe); const run_c_test = b.addRunArtifact(c_test_exe); + run_c_test.step.dependOn(&lib_install_step.step); + const test_c_step = b.step(b.fmt("test-c-{s}", .{impl.name}), b.fmt("Run lib{s} C test", .{impl.name})); - test_c_step.dependOn(b.getInstallStep()); test_c_step.dependOn(&run_c_test.step); } } diff --git a/build.zig.zon b/build.zig.zon index 0653c1d..c114eb3 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,8 +1,8 @@ .{ .name = .VulkanDriver, - .version = "0.0.0", + .version = "0.0.1", .fingerprint = 0x52cb73649f1107de, - .minimum_zig_version = "0.15.1", + .minimum_zig_version = "0.15.2", .dependencies = .{ .vulkan_headers = .{ @@ -13,11 +13,6 @@ .url = "git+https://github.com/catmeow72/vulkan-zig/#8961518db28f88d2cf09ea68e146923de2cfa7f0", .hash = "vulkan-0.0.0-r7Ytx6hBAwD8X_TN32qlkzul4riK6vFvjtK9fZfRvALg", }, - .cpuinfo = .{ - .url = "git+https://github.com/Kbz-8/cpuinfo-zig#77f82a1248194e7fb706967343c66021f8522766", - .hash = "cpuinfo-0.1.0-V7dMLcghAADJuG7dkd3MnwDPZ232pBK_8uGjxY43eP5u", - .lazy = true, - }, .zdt = .{ .url = "git+https://github.com/FObersteiner/zdt/?ref=v0.8.1#8b551a0a3e5ae64a32b5bad0e6a93119787b43af", .hash = "zdt-0.8.1-xr0_vAxUDwCJRDh9pcAS_mdZBIsvcGTtN-K8JJSWY4I6", @@ -27,6 +22,11 @@ .hash = "N-V-__8AAPn9BwCBHnaxOC_rffCpFI7QRfi5qBCLvov9EYK3", .lazy = true, }, + .cpuinfo = .{ + .url = "git+https://github.com/Kbz-8/cpuinfo-zig#77f82a1248194e7fb706967343c66021f8522766", + .hash = "cpuinfo-0.1.0-V7dMLcghAADJuG7dkd3MnwDPZ232pBK_8uGjxY43eP5u", + .lazy = true, + }, }, .paths = .{ diff --git a/src/soft/PhysicalDevice.zig b/src/soft/PhysicalDevice.zig index 90d46e5..eed363c 100644 --- a/src/soft/PhysicalDevice.zig +++ b/src/soft/PhysicalDevice.zig @@ -37,8 +37,10 @@ pub fn create(allocator: std.mem.Allocator, instance: *const base.Instance) VkEr interface.mem_props.memory_types[0] = .{ .heap_index = 0, .property_flags = .{ + .device_local_bit = true, .host_visible_bit = true, .host_coherent_bit = true, + .host_cached_bit = true, }, }; interface.mem_props.memory_heap_count = 1; diff --git a/src/vulkan/Buffer.zig b/src/vulkan/Buffer.zig new file mode 100644 index 0000000..2570fc1 --- /dev/null +++ b/src/vulkan/Buffer.zig @@ -0,0 +1,13 @@ +const std = @import("std"); +const vk = @import("vulkan"); + +const VkError = @import("error_set.zig").VkError; +const Device = @import("Device.zig"); + +const Self = @This(); +pub const ObjectType: vk.ObjectType = .buffer; + +owner: *const Device, +size: vk.DeviceSize, +offset: vk.DeviceSize, +usage: vk.BufferUsageFlags, diff --git a/src/vulkan/DeviceMemory.zig b/src/vulkan/DeviceMemory.zig new file mode 100644 index 0000000..056875d --- /dev/null +++ b/src/vulkan/DeviceMemory.zig @@ -0,0 +1,37 @@ +const std = @import("std"); +const vk = @import("vulkan"); + +const VkError = @import("error_set.zig").VkError; +const Device = @import("Device.zig"); + +const Self = @This(); +pub const ObjectType: vk.ObjectType = .device_memory; + +owner: *const Device, +size: vk.DeviceSize, +memory_type_index: u32, +is_mapped: bool, + +vtable: *const VTable, + +pub const VTable = struct { + map: *const fn (*Self, vk.DeviceSize, vk.DeviceSize) VkError!?*anyopaque, + unmap: *const fn (*Self) void, +}; + +pub fn init(device: *const Device, size: vk.DeviceSize, memory_type_index: u32) VkError!Self { + return .{ + .owner = device, + .size = size, + .memory_type_index = memory_type_index, + .is_mapped = false, + }; +} + +pub inline fn map(self: *Self, offset: vk.DeviceSize, size: vk.DeviceSize) VkError!?*anyopaque { + return self.vtable.map(self, offset, size); +} + +pub inline fn unmap(self: *Self) void { + return self.vtable.unmap(self); +} diff --git a/src/vulkan/Dispatchable.zig b/src/vulkan/Dispatchable.zig index c55b20e..b6af105 100644 --- a/src/vulkan/Dispatchable.zig +++ b/src/vulkan/Dispatchable.zig @@ -13,30 +13,6 @@ pub fn Dispatchable(comptime T: type) type { loader_data: c.VK_LOADER_DATA, object_type: vk.ObjectType, object: *T, - is_owner: bool = false, - - pub fn create(allocator: std.mem.Allocator, args: anytype) VkError!*Self { - comptime { - const ti = @typeInfo(@TypeOf(args)); - if (ti != .@"struct" or !ti.@"struct".is_tuple) { - @compileError("pass a tuple literal like .{...}"); - } - - if (!std.meta.hasMethod(T, "init")) { - @compileError("Dispatchable types are expected to have 'init' and 'deinit' methods."); - } - const init_params = @typeInfo(@TypeOf(T.init)).@"fn".params; - if (init_params.len < 1 or init_params[0].type != std.mem.Allocator) { - @compileError("Dispatchable types 'init' method should take a 'std.mem.Allocator' as its first parameter."); - } - } - - const self = allocator.create(Self) catch return VkError.OutOfHostMemory; - const object = allocator.create(T) catch return VkError.OutOfHostMemory; - object.* = try @call(.auto, T.init, .{allocator} ++ args); - self.is_owner = true; - return self.wrap(object); - } pub fn wrap(allocator: std.mem.Allocator, object: *T) VkError!*Self { const self = allocator.create(Self) catch return VkError.OutOfHostMemory; @@ -48,10 +24,7 @@ pub fn Dispatchable(comptime T: type) type { return self; } - pub fn destroy(self: *Self, allocator: std.mem.Allocator) void { - if (self.is_owner) { - allocator.destroy(self.object); - } + pub inline fn destroy(self: *Self, allocator: std.mem.Allocator) void { allocator.destroy(self); } diff --git a/src/vulkan/NonDispatchable.zig b/src/vulkan/NonDispatchable.zig new file mode 100644 index 0000000..d0955fc --- /dev/null +++ b/src/vulkan/NonDispatchable.zig @@ -0,0 +1,51 @@ +const std = @import("std"); +const vk = @import("vulkan"); + +const VkError = @import("error_set.zig").VkError; + +pub fn NonDispatchable(comptime T: type) type { + return struct { + const Self = @This(); + + object_type: vk.ObjectType, + object: *T, + + pub fn wrap(allocator: std.mem.Allocator, object: *T) VkError!*Self { + const self = allocator.create(Self) catch return VkError.OutOfHostMemory; + self.* = .{ + .object_type = T.ObjectType, + .object = object, + }; + return self; + } + + pub inline fn destroy(self: *Self, allocator: std.mem.Allocator) void { + allocator.destroy(self); + } + + pub inline fn toHandle(self: *Self) usize { + return @intFromPtr(self); + } + + pub inline fn toVkHandle(self: *Self, comptime VkT: type) VkT { + return @enumFromInt(@intFromPtr(self)); + } + + pub inline fn fromHandle(vk_handle: anytype) VkError!*Self { + const handle = @intFromEnum(vk_handle); + if (handle == 0) { + return VkError.Unknown; + } + const nondispatchable: *Self = @ptrFromInt(handle); + if (nondispatchable.object_type != T.ObjectType) { + return VkError.Unknown; + } + return nondispatchable; + } + + pub inline fn fromHandleObject(handle: anytype) VkError!*T { + const nondispatchable_handle = try Self.fromHandle(handle); + return nondispatchable_handle.object; + } + }; +} diff --git a/src/vulkan/lib.zig b/src/vulkan/lib.zig index cdc3e3f..4d340f5 100644 --- a/src/vulkan/lib.zig +++ b/src/vulkan/lib.zig @@ -5,6 +5,7 @@ pub const lib_vulkan = @import("lib_vulkan.zig"); pub const logger = @import("logger.zig"); pub const Dispatchable = @import("Dispatchable.zig").Dispatchable; +pub const NonDispatchable = @import("NonDispatchable.zig").NonDispatchable; pub const VkError = @import("error_set.zig").VkError; pub const Instance = @import("Instance.zig");