rework of the queue
This commit is contained in:
@@ -2,7 +2,12 @@ const std = @import("std");
|
||||
const vk = @import("vulkan");
|
||||
const base = @import("base");
|
||||
|
||||
const Executor = @import("Executor.zig");
|
||||
const Dispatchable = base.Dispatchable;
|
||||
|
||||
const CommandBuffer = base.CommandBuffer;
|
||||
const SoftDevice = @import("SoftDevice.zig");
|
||||
|
||||
const SoftDeviceMemory = @import("SoftDeviceMemory.zig");
|
||||
const SoftFence = @import("SoftFence.zig");
|
||||
|
||||
@@ -13,7 +18,6 @@ pub const Interface = base.Queue;
|
||||
|
||||
interface: Interface,
|
||||
wait_group: std.Thread.WaitGroup,
|
||||
mutex: std.Thread.Mutex,
|
||||
worker_mutex: std.Thread.Mutex,
|
||||
|
||||
pub fn create(allocator: std.mem.Allocator, device: *base.Device, index: u32, family_index: u32, flags: vk.DeviceQueueCreateFlags) VkError!*Interface {
|
||||
@@ -31,7 +35,6 @@ pub fn create(allocator: std.mem.Allocator, device: *base.Device, index: u32, fa
|
||||
self.* = .{
|
||||
.interface = interface,
|
||||
.wait_group = .{},
|
||||
.mutex = .{},
|
||||
.worker_mutex = .{},
|
||||
};
|
||||
return &self.interface;
|
||||
@@ -50,36 +53,50 @@ pub fn bindSparse(interface: *Interface, info: []const vk.BindSparseInfo, fence:
|
||||
return VkError.FeatureNotPresent;
|
||||
}
|
||||
|
||||
pub fn submit(interface: *Interface, info: []const vk.SubmitInfo, fence: ?*base.Fence) VkError!void {
|
||||
pub fn submit(interface: *Interface, infos: []Interface.SubmitInfo, p_fence: ?*base.Fence) VkError!void {
|
||||
var self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
_ = info;
|
||||
|
||||
const Runner = struct {
|
||||
fn run(queue: *Self, p_fence: ?*base.Fence) void {
|
||||
// Waiting for older submits to finish execution
|
||||
queue.worker_mutex.lock();
|
||||
defer queue.worker_mutex.unlock();
|
||||
|
||||
// TODO: commands executions
|
||||
|
||||
if (p_fence) |fence_obj| {
|
||||
fence_obj.signal() catch {};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
self.mutex.lock();
|
||||
defer self.mutex.unlock();
|
||||
|
||||
var soft_device: *SoftDevice = @alignCast(@fieldParentPtr("interface", interface.owner));
|
||||
soft_device.workers.spawnWg(&self.wait_group, Runner.run, .{ self, fence });
|
||||
|
||||
if (p_fence) |fence| {
|
||||
const soft_fence: *SoftFence = @alignCast(@fieldParentPtr("interface", fence));
|
||||
soft_fence.concurrent_submits_count = std.atomic.Value(usize).init(infos.len);
|
||||
}
|
||||
|
||||
for (infos) |info| {
|
||||
// Cloning info to keep them alive until commands dispatch end
|
||||
const cloned_info: Interface.SubmitInfo = .{
|
||||
.command_buffers = info.command_buffers.clone(soft_device.device_allocator.allocator()) catch return VkError.OutOfDeviceMemory,
|
||||
};
|
||||
soft_device.workers.spawnWg(&self.wait_group, Self.taskRunner, .{ self, cloned_info, p_fence });
|
||||
}
|
||||
}
|
||||
|
||||
pub fn waitIdle(interface: *Interface) VkError!void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
|
||||
self.mutex.lock();
|
||||
defer self.mutex.unlock();
|
||||
|
||||
self.wait_group.wait();
|
||||
}
|
||||
|
||||
fn taskRunner(self: *Self, info: Interface.SubmitInfo, p_fence: ?*base.Fence) void {
|
||||
var soft_device: *SoftDevice = @alignCast(@fieldParentPtr("interface", self.interface.owner));
|
||||
defer {
|
||||
var command_buffers = info.command_buffers;
|
||||
command_buffers.deinit(soft_device.device_allocator.allocator());
|
||||
}
|
||||
|
||||
var executor = Executor.init();
|
||||
defer executor.deinit();
|
||||
|
||||
loop: for (info.command_buffers.items) |command_buffer| {
|
||||
command_buffer.submit() catch continue :loop;
|
||||
for (command_buffer.commands.items) |command| {
|
||||
executor.dispatch(&command);
|
||||
}
|
||||
}
|
||||
|
||||
if (p_fence) |fence| {
|
||||
const soft_fence: *SoftFence = @alignCast(@fieldParentPtr("interface", fence));
|
||||
if (soft_fence.concurrent_submits_count.fetchSub(1, .release) == 1) {
|
||||
fence.signal() catch {};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user