rework of the queue
This commit is contained in:
@@ -1,10 +1,18 @@
|
||||
const std = @import("std");
|
||||
const vk = @import("vulkan");
|
||||
|
||||
const VkError = @import("error_set.zig").VkError;
|
||||
const CommandPool = @import("CommandPool.zig");
|
||||
const Device = @import("Device.zig");
|
||||
const cmd = @import("commands.zig");
|
||||
|
||||
const NonDispatchable = @import("NonDispatchable.zig").NonDispatchable;
|
||||
const VkError = @import("error_set.zig").VkError;
|
||||
const VulkanAllocator = @import("VulkanAllocator.zig");
|
||||
|
||||
const Device = @import("Device.zig");
|
||||
|
||||
const Buffer = @import("Buffer.zig");
|
||||
const CommandPool = @import("CommandPool.zig");
|
||||
|
||||
const COMMAND_BUFFER_BASE_CAPACITY = 256;
|
||||
|
||||
const State = enum {
|
||||
Initial,
|
||||
@@ -21,6 +29,9 @@ owner: *Device,
|
||||
pool: *CommandPool,
|
||||
state: State,
|
||||
begin_info: ?vk.CommandBufferBeginInfo,
|
||||
host_allocator: VulkanAllocator,
|
||||
commands: std.ArrayList(cmd.Command),
|
||||
state_mutex: std.Thread.Mutex,
|
||||
|
||||
vtable: *const VTable,
|
||||
dispatch_table: *const DispatchTable,
|
||||
@@ -28,6 +39,7 @@ dispatch_table: *const DispatchTable,
|
||||
pub const DispatchTable = struct {
|
||||
begin: *const fn (*Self, *const vk.CommandBufferBeginInfo) VkError!void,
|
||||
end: *const fn (*Self) VkError!void,
|
||||
fillBuffer: *const fn (*Self, *Buffer, vk.DeviceSize, vk.DeviceSize, u32) VkError!void,
|
||||
reset: *const fn (*Self, vk.CommandBufferResetFlags) VkError!void,
|
||||
};
|
||||
|
||||
@@ -36,12 +48,14 @@ pub const VTable = struct {
|
||||
};
|
||||
|
||||
pub fn init(device: *Device, allocator: std.mem.Allocator, info: *const vk.CommandBufferAllocateInfo) VkError!Self {
|
||||
_ = allocator;
|
||||
return .{
|
||||
.owner = device,
|
||||
.pool = try NonDispatchable(CommandPool).fromHandleObject(info.command_pool),
|
||||
.state = .Initial,
|
||||
.begin_info = null,
|
||||
.host_allocator = VulkanAllocator.from(allocator).clone(),
|
||||
.commands = std.ArrayList(cmd.Command).initCapacity(allocator, COMMAND_BUFFER_BASE_CAPACITY) catch return VkError.OutOfHostMemory,
|
||||
.state_mutex = .{},
|
||||
.vtable = undefined,
|
||||
.dispatch_table = undefined,
|
||||
};
|
||||
@@ -51,10 +65,13 @@ inline fn transitionState(self: *Self, target: State, from_allowed: []const Stat
|
||||
if (!std.EnumSet(State).initMany(from_allowed).contains(self.state)) {
|
||||
return error.NotAllowed;
|
||||
}
|
||||
self.state_mutex.lock();
|
||||
defer self.state_mutex.unlock();
|
||||
self.state = target;
|
||||
}
|
||||
|
||||
pub inline fn destroy(self: *Self, allocator: std.mem.Allocator) void {
|
||||
self.commands.deinit(allocator);
|
||||
self.vtable.destroy(self, allocator);
|
||||
}
|
||||
|
||||
@@ -89,3 +106,16 @@ pub inline fn submit(self: *Self) VkError!void {
|
||||
}
|
||||
self.transitionState(.Pending, &.{ .Pending, .Executable }) catch return VkError.ValidationFailed;
|
||||
}
|
||||
|
||||
// Commands ====================================================================================================
|
||||
|
||||
pub inline fn fillBuffer(self: *Self, buffer: *Buffer, offset: vk.DeviceSize, size: vk.DeviceSize, data: u32) VkError!void {
|
||||
const allocator = self.host_allocator.allocator();
|
||||
self.commands.append(allocator, .{ .FillBuffer = .{
|
||||
.buffer = buffer,
|
||||
.offset = offset,
|
||||
.size = size,
|
||||
.data = data,
|
||||
} }) catch return VkError.OutOfHostMemory;
|
||||
try self.dispatch_table.fillBuffer(self, buffer, offset, size, data);
|
||||
}
|
||||
|
||||
@@ -85,7 +85,9 @@ pub inline fn destroy(self: *Self, allocator: std.mem.Allocator) VkError!void {
|
||||
}
|
||||
|
||||
pub inline fn createBuffer(self: *Self, allocator: std.mem.Allocator, info: *const vk.BufferCreateInfo) VkError!*Buffer {
|
||||
return self.dispatch_table.createBuffer(self, allocator, info);
|
||||
const buffer = try self.dispatch_table.createBuffer(self, allocator, info);
|
||||
std.debug.assert(buffer.allowed_memory_types != 0);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
pub inline fn createFence(self: *Self, allocator: std.mem.Allocator, info: *const vk.FenceCreateInfo) VkError!*Fence {
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
const std = @import("std");
|
||||
const vk = @import("vulkan");
|
||||
|
||||
const Dispatchable = @import("Dispatchable.zig").Dispatchable;
|
||||
const VkError = @import("error_set.zig").VkError;
|
||||
const VulkanAllocator = @import("VulkanAllocator.zig");
|
||||
|
||||
const CommandBuffer = @import("CommandBuffer.zig");
|
||||
const Device = @import("Device.zig");
|
||||
const Dispatchable = @import("Dispatchable.zig").Dispatchable;
|
||||
|
||||
const Fence = @import("Fence.zig");
|
||||
|
||||
const Self = @This();
|
||||
@@ -14,23 +17,58 @@ owner: *Device,
|
||||
family_index: u32,
|
||||
index: u32,
|
||||
flags: vk.DeviceQueueCreateFlags,
|
||||
host_allocator: VulkanAllocator,
|
||||
|
||||
dispatch_table: *const DispatchTable,
|
||||
|
||||
pub const DispatchTable = struct {
|
||||
bindSparse: *const fn (*Self, []const vk.BindSparseInfo, ?*Fence) VkError!void,
|
||||
submit: *const fn (*Self, []const vk.SubmitInfo, ?*Fence) VkError!void,
|
||||
submit: *const fn (*Self, []SubmitInfo, ?*Fence) VkError!void,
|
||||
waitIdle: *const fn (*Self) VkError!void,
|
||||
};
|
||||
|
||||
pub const SubmitInfo = struct {
|
||||
command_buffers: std.ArrayList(*CommandBuffer),
|
||||
// TODO: complete
|
||||
|
||||
fn initBlob(allocator: std.mem.Allocator, infos: []const vk.SubmitInfo) VkError!std.ArrayList(SubmitInfo) {
|
||||
var self = std.ArrayList(SubmitInfo).initCapacity(allocator, infos.len) catch return VkError.OutOfHostMemory;
|
||||
errdefer self.deinit(allocator);
|
||||
|
||||
loop: for (infos) |info| {
|
||||
if (info.command_buffer_count == 0) continue :loop;
|
||||
if (info.p_command_buffers == null) continue :loop;
|
||||
|
||||
var submit_info: SubmitInfo = .{
|
||||
.command_buffers = std.ArrayList(*CommandBuffer).initCapacity(allocator, info.command_buffer_count) catch return VkError.OutOfHostMemory,
|
||||
};
|
||||
|
||||
for (info.p_command_buffers.?[0..info.command_buffer_count]) |vk_command_buffer| {
|
||||
submit_info.command_buffers.append(allocator, try Dispatchable(CommandBuffer).fromHandleObject(vk_command_buffer)) catch return VkError.OutOfHostMemory;
|
||||
}
|
||||
|
||||
self.append(allocator, submit_info) catch return VkError.OutOfHostMemory;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
fn deinitBlob(allocator: std.mem.Allocator, self: *std.ArrayList(SubmitInfo)) void {
|
||||
for (self.items) |submit_info| {
|
||||
const command_buffers = &submit_info.command_buffers;
|
||||
@constCast(command_buffers).deinit(allocator);
|
||||
}
|
||||
self.deinit(allocator);
|
||||
}
|
||||
};
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, device: *Device, index: u32, family_index: u32, flags: vk.DeviceQueueCreateFlags) VkError!Self {
|
||||
std.log.scoped(.vkCreateDevice).info("Creating device queue with family index {d} and index {d}", .{ family_index, index });
|
||||
_ = allocator;
|
||||
return .{
|
||||
.owner = device,
|
||||
.family_index = family_index,
|
||||
.index = index,
|
||||
.flags = flags,
|
||||
.host_allocator = VulkanAllocator.from(allocator).clone(),
|
||||
.dispatch_table = undefined,
|
||||
};
|
||||
}
|
||||
@@ -39,16 +77,13 @@ pub inline fn bindSparse(self: *Self, info: []const vk.BindSparseInfo, fence: ?*
|
||||
try self.dispatch_table.bindSparse(self, info, fence);
|
||||
}
|
||||
|
||||
pub inline fn submit(self: *Self, info: []const vk.SubmitInfo, fence: ?*Fence) VkError!void {
|
||||
try self.dispatch_table.submit(self, info, fence);
|
||||
for (info) |submit_info| {
|
||||
if (submit_info.p_command_buffers) |p_command_buffers| {
|
||||
for (p_command_buffers[0..submit_info.command_buffer_count]) |p_cmd| {
|
||||
const cmd = try Dispatchable(CommandBuffer).fromHandleObject(p_cmd);
|
||||
try cmd.submit();
|
||||
}
|
||||
}
|
||||
}
|
||||
pub inline fn submit(self: *Self, infos: []const vk.SubmitInfo, p_fence: ?*Fence) VkError!void {
|
||||
const allocator = self.host_allocator.cloneWithScope(.command).allocator();
|
||||
|
||||
var submit_infos = try SubmitInfo.initBlob(allocator, infos);
|
||||
defer SubmitInfo.deinitBlob(allocator, &submit_infos);
|
||||
|
||||
try self.dispatch_table.submit(self, submit_infos.items, p_fence);
|
||||
}
|
||||
|
||||
pub inline fn waitIdle(self: *Self) VkError!void {
|
||||
|
||||
@@ -4,17 +4,32 @@ const vk = @import("vulkan");
|
||||
const Buffer = @import("Buffer.zig");
|
||||
|
||||
pub const CommandType = enum {
|
||||
BindPipeline,
|
||||
BindVertexBuffer,
|
||||
CopyBuffer,
|
||||
Draw,
|
||||
DrawIndexed,
|
||||
DrawIndirect,
|
||||
DrawIndexedIndirect,
|
||||
BindPipeline,
|
||||
DrawIndirect,
|
||||
FillBuffer,
|
||||
};
|
||||
|
||||
pub const CommandCopyBuffer = struct {
|
||||
src: *Buffer,
|
||||
dst: *Buffer,
|
||||
regions: []*const vk.BufferCopy,
|
||||
};
|
||||
|
||||
pub const CommandFillBuffer = struct {
|
||||
buffer: *Buffer,
|
||||
offset: vk.DeviceSize,
|
||||
size: vk.DeviceSize,
|
||||
data: u32,
|
||||
};
|
||||
|
||||
pub const CommandBindVertexBuffer = struct {
|
||||
buffers: std.ArrayList(Buffer),
|
||||
offsets: std.ArrayList(vk.DeviceSize),
|
||||
buffers: []*const Buffer,
|
||||
offsets: []vk.DeviceSize,
|
||||
first_binding: u32,
|
||||
};
|
||||
|
||||
@@ -52,10 +67,12 @@ pub const CommandBindPipeline = struct {
|
||||
};
|
||||
|
||||
pub const Command = union(CommandType) {
|
||||
BindPipeline: CommandBindPipeline,
|
||||
BindVertexBuffer: CommandBindVertexBuffer,
|
||||
CopyBuffer: CommandCopyBuffer,
|
||||
Draw: CommandDraw,
|
||||
DrawIndexed: CommandDrawIndexed,
|
||||
DrawIndirect: CommandDrawIndirect,
|
||||
DrawIndexedIndirect: CommandDrawIndexedIndirect,
|
||||
BindPipeline: CommandBindPipeline,
|
||||
DrawIndirect: CommandDrawIndirect,
|
||||
FillBuffer: CommandFillBuffer,
|
||||
};
|
||||
|
||||
@@ -301,6 +301,8 @@ pub export fn strollQueueBindSparse(p_queue: vk.Queue, count: u32, info: [*]vk.B
|
||||
}
|
||||
|
||||
pub export fn strollQueueSubmit(p_queue: vk.Queue, count: u32, info: [*]const vk.SubmitInfo, p_fence: vk.Fence) callconv(vk.vulkan_call_conv) vk.Result {
|
||||
if (count == 0) return .success;
|
||||
|
||||
const queue = Dispatchable(Queue).fromHandleObject(p_queue) catch |err| return toVkResult(err);
|
||||
const fence = if (p_fence != .null_handle) NonDispatchable(Fence).fromHandleObject(p_fence) catch |err| return toVkResult(err) else null;
|
||||
queue.submit(info[0..count], fence) catch |err| return toVkResult(err);
|
||||
|
||||
Reference in New Issue
Block a user