adding device memory base

This commit is contained in:
2025-11-08 01:00:30 +01:00
parent 7fdc02c260
commit 4b23abe795
8 changed files with 115 additions and 37 deletions

View File

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

13
src/vulkan/Buffer.zig git.filemode.normal_file
View File

@@ -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,

37
src/vulkan/DeviceMemory.zig git.filemode.normal_file
View File

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

View File

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

51
src/vulkan/NonDispatchable.zig git.filemode.normal_file
View File

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

View File

@@ -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");