implementing binary semaphores
This commit is contained in:
@@ -9,6 +9,9 @@ const Self = @This();
|
||||
pub const Interface = base.BinarySemaphore;
|
||||
|
||||
interface: Interface,
|
||||
mutex: std.Io.Mutex,
|
||||
condition: std.Io.Condition,
|
||||
is_signaled: bool,
|
||||
|
||||
pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const vk.SemaphoreCreateInfo) VkError!*Self {
|
||||
const self = allocator.create(Self) catch return VkError.OutOfHostMemory;
|
||||
@@ -18,10 +21,15 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v
|
||||
|
||||
interface.vtable = &.{
|
||||
.destroy = destroy,
|
||||
.signal = signal,
|
||||
.wait = wait,
|
||||
};
|
||||
|
||||
self.* = .{
|
||||
.interface = interface,
|
||||
.mutex = .init,
|
||||
.condition = .init,
|
||||
.is_signaled = false,
|
||||
};
|
||||
return self;
|
||||
}
|
||||
@@ -30,3 +38,27 @@ pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
allocator.destroy(self);
|
||||
}
|
||||
|
||||
pub fn signal(interface: *Interface) VkError!void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
const io = interface.owner.io();
|
||||
|
||||
self.mutex.lock(io) catch return VkError.DeviceLost;
|
||||
defer self.mutex.unlock(io);
|
||||
|
||||
self.is_signaled = true;
|
||||
self.condition.broadcast(io);
|
||||
}
|
||||
|
||||
pub fn wait(interface: *Interface) VkError!void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
const io = interface.owner.io();
|
||||
|
||||
self.mutex.lock(io) catch return VkError.DeviceLost;
|
||||
defer self.mutex.unlock(io);
|
||||
|
||||
while (!self.is_signaled) {
|
||||
self.condition.wait(io, &self.mutex) catch return VkError.DeviceLost;
|
||||
}
|
||||
self.is_signaled = false;
|
||||
}
|
||||
|
||||
@@ -57,10 +57,10 @@ pub fn copyBuffer(self: *const Self, dst: *Self, regions: []const vk.BufferCopy)
|
||||
}
|
||||
|
||||
pub fn fillBuffer(self: *Self, offset: vk.DeviceSize, size: vk.DeviceSize, data: u32) VkError!void {
|
||||
const memory = if (self.interface.memory) |memory| memory else return VkError.InvalidDeviceMemoryDrv;
|
||||
var bytes = if (size == vk.WHOLE_SIZE) memory.size - offset else size;
|
||||
if (self.interface.memory == null) return VkError.InvalidDeviceMemoryDrv;
|
||||
var bytes = if (size == vk.WHOLE_SIZE) self.interface.size - offset else size;
|
||||
|
||||
const map = try self.mapAsSliceWithOffset(u32, offset, @divFloor(bytes, @sizeOf(u32)) * @sizeOf(u32));
|
||||
const map = try self.mapAsSliceWithAddedOffset(u32, offset, @divFloor(bytes, @sizeOf(u32)) * @sizeOf(u32));
|
||||
|
||||
var i: usize = 0;
|
||||
while (bytes >= 4) : ({
|
||||
|
||||
@@ -85,6 +85,7 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v
|
||||
.setStencilReference = setStencilReference,
|
||||
.setStencilWriteMask = setStencilWriteMask,
|
||||
.setViewport = setViewport,
|
||||
.updateBuffer = updateBuffer,
|
||||
.waitEvent = waitEvent,
|
||||
};
|
||||
|
||||
@@ -819,7 +820,7 @@ pub fn dispatchIndirect(interface: *Interface, buffer: *base.Buffer, offset: vk.
|
||||
|
||||
pub fn execute(context: *anyopaque, device: *ExecutionDevice) VkError!void {
|
||||
const impl: *Impl = @ptrCast(@alignCast(context));
|
||||
const command = try impl.buffer.mapAsWithOffset(vk.DispatchIndirectCommand, impl.offset);
|
||||
const command = try impl.buffer.mapToWithAddedOffset(vk.DispatchIndirectCommand, impl.offset);
|
||||
try device.compute.dispatch(command.x, command.y, command.z);
|
||||
}
|
||||
};
|
||||
@@ -908,7 +909,7 @@ pub fn drawIndexedIndirect(interface: *Interface, buffer: *base.Buffer, offset:
|
||||
pub fn execute(context: *anyopaque, device: *ExecutionDevice) VkError!void {
|
||||
const impl: *Impl = @ptrCast(@alignCast(context));
|
||||
for (0..impl.count) |index| {
|
||||
const command = try impl.buffer.mapAsWithOffset(vk.DrawIndexedIndirectCommand, impl.offset + index * impl.stride);
|
||||
const command = try impl.buffer.mapToWithAddedOffset(vk.DrawIndexedIndirectCommand, impl.offset + index * impl.stride);
|
||||
try device.renderer.drawIndexed(command.index_count, command.instance_count, command.first_index, command.first_instance, command.vertex_offset);
|
||||
}
|
||||
}
|
||||
@@ -940,7 +941,7 @@ pub fn drawIndirect(interface: *Interface, buffer: *base.Buffer, offset: usize,
|
||||
pub fn execute(context: *anyopaque, device: *ExecutionDevice) VkError!void {
|
||||
const impl: *Impl = @ptrCast(@alignCast(context));
|
||||
for (0..impl.count) |index| {
|
||||
const command = try impl.buffer.mapAsWithOffset(vk.DrawIndirectCommand, impl.offset + index * impl.stride);
|
||||
const command = try impl.buffer.mapToWithAddedOffset(vk.DrawIndirectCommand, impl.offset + index * impl.stride);
|
||||
try device.renderer.draw(command.vertex_count, command.instance_count, command.first_vertex, command.first_instance);
|
||||
}
|
||||
}
|
||||
@@ -1028,6 +1029,36 @@ pub fn fillBuffer(interface: *Interface, buffer: *base.Buffer, offset: vk.Device
|
||||
self.commands.append(allocator, .{ .ptr = cmd, .vtable = &.{ .execute = CommandImpl.execute } }) catch return VkError.OutOfHostMemory;
|
||||
}
|
||||
|
||||
pub fn updateBuffer(interface: *Interface, buffer: *base.Buffer, offset: vk.DeviceSize, data: []const u8) VkError!void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
const allocator = self.command_allocator.allocator();
|
||||
|
||||
const CommandImpl = struct {
|
||||
const Impl = @This();
|
||||
|
||||
buffer: *SoftBuffer,
|
||||
offset: vk.DeviceSize,
|
||||
data: []const u8,
|
||||
|
||||
pub fn execute(context: *anyopaque, _: *ExecutionDevice) VkError!void {
|
||||
const impl: *Impl = @ptrCast(@alignCast(context));
|
||||
const map = try impl.buffer.mapAsSliceWithAddedOffset(u8, impl.offset, impl.data.len);
|
||||
@memcpy(map, impl.data);
|
||||
}
|
||||
};
|
||||
|
||||
const cmd = allocator.create(CommandImpl) catch return VkError.OutOfHostMemory;
|
||||
errdefer allocator.destroy(cmd);
|
||||
|
||||
const data_copy = allocator.dupe(u8, data) catch return VkError.OutOfHostMemory;
|
||||
cmd.* = .{
|
||||
.buffer = @alignCast(@fieldParentPtr("interface", buffer)),
|
||||
.offset = offset,
|
||||
.data = data_copy,
|
||||
};
|
||||
self.commands.append(allocator, .{ .ptr = cmd, .vtable = &.{ .execute = CommandImpl.execute } }) catch return VkError.OutOfHostMemory;
|
||||
}
|
||||
|
||||
pub fn nextSubpass(interface: *Interface, _: vk.SubpassContents) VkError!void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
const allocator = self.command_allocator.allocator();
|
||||
|
||||
+120
-33
@@ -2,8 +2,6 @@ const std = @import("std");
|
||||
const vk = @import("vulkan");
|
||||
const base = @import("base");
|
||||
|
||||
const RefCounter = base.RefCounter;
|
||||
|
||||
const ExecutionDevice = @import("device/Device.zig");
|
||||
const Dispatchable = base.Dispatchable;
|
||||
|
||||
@@ -18,6 +16,18 @@ pub const Interface = base.Queue;
|
||||
|
||||
interface: Interface,
|
||||
group: std.Io.Group,
|
||||
mutex: std.Io.Mutex,
|
||||
condition: std.Io.Condition,
|
||||
next_sequence: usize,
|
||||
executing_sequence: usize,
|
||||
|
||||
const TaskData = struct {
|
||||
queue: *Self,
|
||||
soft_device: *SoftDevice,
|
||||
sequence: usize,
|
||||
infos: std.ArrayList(Interface.SubmitInfo),
|
||||
fence: ?*base.Fence,
|
||||
};
|
||||
|
||||
pub fn create(allocator: std.mem.Allocator, device: *base.Device, index: u32, family_index: u32, flags: vk.DeviceQueueCreateFlags) VkError!*Interface {
|
||||
const self = allocator.create(Self) catch return VkError.OutOfHostMemory;
|
||||
@@ -34,6 +44,10 @@ pub fn create(allocator: std.mem.Allocator, device: *base.Device, index: u32, fa
|
||||
self.* = .{
|
||||
.interface = interface,
|
||||
.group = .init,
|
||||
.mutex = .init,
|
||||
.condition = .init,
|
||||
.next_sequence = 0,
|
||||
.executing_sequence = 0,
|
||||
};
|
||||
return &self.interface;
|
||||
}
|
||||
@@ -54,22 +68,34 @@ pub fn bindSparse(interface: *Interface, info: []const vk.BindSparseInfo, fence:
|
||||
pub fn submit(interface: *Interface, infos: []Interface.SubmitInfo, p_fence: ?*base.Fence) VkError!void {
|
||||
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
||||
const soft_device: *SoftDevice = @alignCast(@fieldParentPtr("interface", interface.owner));
|
||||
|
||||
const allocator = soft_device.device_allocator.allocator();
|
||||
const io = soft_device.interface.io();
|
||||
const io = interface.owner.io();
|
||||
|
||||
const runners_counter = allocator.create(RefCounter) catch return VkError.OutOfDeviceMemory;
|
||||
errdefer allocator.destroy(runners_counter);
|
||||
runners_counter.* = .init;
|
||||
runners_counter.setRef(infos.len);
|
||||
const task_data = allocator.create(TaskData) catch return VkError.OutOfDeviceMemory;
|
||||
errdefer allocator.destroy(task_data);
|
||||
|
||||
for (infos) |info| {
|
||||
// Cloning info to keep them alive until command execution ends
|
||||
const cloned_info: Interface.SubmitInfo = .{
|
||||
.command_buffers = info.command_buffers.clone(allocator) catch return VkError.OutOfDeviceMemory,
|
||||
};
|
||||
self.group.async(io, Self.taskRunner, .{ self, cloned_info, p_fence, runners_counter });
|
||||
}
|
||||
var cloned_infos = try cloneSubmitInfos(allocator, infos);
|
||||
errdefer deinitSubmitInfos(allocator, &cloned_infos);
|
||||
|
||||
const sequence = blk: {
|
||||
self.mutex.lock(io) catch return VkError.DeviceLost;
|
||||
defer self.mutex.unlock(io);
|
||||
|
||||
const seq = self.next_sequence;
|
||||
self.next_sequence += 1;
|
||||
|
||||
break :blk seq;
|
||||
};
|
||||
|
||||
task_data.* = .{
|
||||
.queue = self,
|
||||
.soft_device = soft_device,
|
||||
.sequence = sequence,
|
||||
.infos = cloned_infos,
|
||||
.fence = p_fence,
|
||||
};
|
||||
|
||||
self.group.async(io, Self.taskRunner, .{task_data});
|
||||
}
|
||||
|
||||
pub fn waitIdle(interface: *Interface) VkError!void {
|
||||
@@ -78,20 +104,9 @@ pub fn waitIdle(interface: *Interface) VkError!void {
|
||||
self.group.await(io) catch return VkError.DeviceLost;
|
||||
}
|
||||
|
||||
fn taskRunner(self: *Self, info: Interface.SubmitInfo, p_fence: ?*base.Fence, runners_counter: *RefCounter) void {
|
||||
defer {
|
||||
runners_counter.unref();
|
||||
if (!runners_counter.hasRefs()) {
|
||||
const soft_device: *SoftDevice = @alignCast(@fieldParentPtr("interface", self.interface.owner));
|
||||
const allocator = soft_device.device_allocator.allocator();
|
||||
allocator.destroy(runners_counter);
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
fn executeSubmitInfo(soft_device: *SoftDevice, info: Interface.SubmitInfo) VkError!void {
|
||||
for (info.wait_semaphores.items) |semaphore| {
|
||||
try semaphore.wait();
|
||||
}
|
||||
|
||||
var execution_device: ExecutionDevice = undefined;
|
||||
@@ -103,9 +118,81 @@ fn taskRunner(self: *Self, info: Interface.SubmitInfo, p_fence: ?*base.Fence, ru
|
||||
soft_command_buffer.execute(&execution_device);
|
||||
}
|
||||
|
||||
if (p_fence) |fence| {
|
||||
if (runners_counter.getRefsCount() == 1) {
|
||||
fence.signal() catch {};
|
||||
}
|
||||
for (info.signal_semaphores.items) |semaphore| {
|
||||
try semaphore.signal();
|
||||
}
|
||||
}
|
||||
|
||||
fn taskRunner(data: *TaskData) void {
|
||||
const io = data.queue.interface.owner.io();
|
||||
const allocator = data.soft_device.device_allocator.allocator();
|
||||
defer {
|
||||
deinitSubmitInfos(allocator, &data.infos);
|
||||
allocator.destroy(data);
|
||||
}
|
||||
|
||||
{
|
||||
data.queue.mutex.lock(io) catch return;
|
||||
defer data.queue.mutex.unlock(io);
|
||||
|
||||
while (data.sequence != data.queue.executing_sequence) {
|
||||
data.queue.condition.wait(io, &data.queue.mutex) catch return;
|
||||
}
|
||||
}
|
||||
|
||||
var submit_err: ?VkError = null;
|
||||
for (data.infos.items) |info| {
|
||||
executeSubmitInfo(data.soft_device, info) catch |err| {
|
||||
submit_err = err;
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
if (submit_err) |err| {
|
||||
std.log.scoped(.SoftQueue).err("Queue submit failed with '{s}'", .{@errorName(err)});
|
||||
}
|
||||
|
||||
if (data.fence) |fence| {
|
||||
fence.signal() catch |err| {
|
||||
std.log.scoped(.SoftQueue).err("Queue submit fence signal failed with '{s}'", .{@errorName(err)});
|
||||
};
|
||||
}
|
||||
|
||||
data.queue.mutex.lock(io) catch return;
|
||||
data.queue.executing_sequence += 1;
|
||||
data.queue.condition.broadcast(io);
|
||||
data.queue.mutex.unlock(io);
|
||||
}
|
||||
|
||||
fn cloneSubmitInfos(allocator: std.mem.Allocator, infos: []Interface.SubmitInfo) VkError!std.ArrayList(Interface.SubmitInfo) {
|
||||
var cloned_infos = std.ArrayList(Interface.SubmitInfo).initCapacity(allocator, infos.len) catch return VkError.OutOfDeviceMemory;
|
||||
errdefer deinitSubmitInfos(allocator, &cloned_infos);
|
||||
|
||||
for (infos) |info| {
|
||||
var wait_semaphores = info.wait_semaphores.clone(allocator) catch return VkError.OutOfDeviceMemory;
|
||||
errdefer wait_semaphores.deinit(allocator);
|
||||
|
||||
var command_buffers = info.command_buffers.clone(allocator) catch return VkError.OutOfDeviceMemory;
|
||||
errdefer command_buffers.deinit(allocator);
|
||||
|
||||
var signal_semaphores = info.signal_semaphores.clone(allocator) catch return VkError.OutOfDeviceMemory;
|
||||
errdefer signal_semaphores.deinit(allocator);
|
||||
|
||||
cloned_infos.append(allocator, .{
|
||||
.wait_semaphores = wait_semaphores,
|
||||
.command_buffers = command_buffers,
|
||||
.signal_semaphores = signal_semaphores,
|
||||
}) catch return VkError.OutOfDeviceMemory;
|
||||
}
|
||||
|
||||
return cloned_infos;
|
||||
}
|
||||
|
||||
fn deinitSubmitInfos(allocator: std.mem.Allocator, infos: *std.ArrayList(Interface.SubmitInfo)) void {
|
||||
for (infos.items) |*info| {
|
||||
info.wait_semaphores.deinit(allocator);
|
||||
info.command_buffers.deinit(allocator);
|
||||
info.signal_semaphores.deinit(allocator);
|
||||
}
|
||||
infos.deinit(allocator);
|
||||
}
|
||||
|
||||
@@ -47,7 +47,8 @@ pub fn init(device: *SoftDevice, state: *PipelineState) Self {
|
||||
}
|
||||
|
||||
pub fn dispatch(self: *Self, group_count_x: u32, group_count_y: u32, group_count_z: u32) VkError!void {
|
||||
const group_count: usize = @intCast(group_count_x * group_count_y * group_count_z);
|
||||
const group_count_xy = std.math.mul(usize, group_count_x, group_count_y) catch return VkError.ValidationFailed;
|
||||
const group_count = std.math.mul(usize, group_count_xy, group_count_z) catch return VkError.ValidationFailed;
|
||||
|
||||
const pipeline = self.state.pipeline orelse return VkError.InvalidPipelineDrv;
|
||||
const shader = pipeline.stages.getPtr(.compute) orelse return VkError.InvalidPipelineDrv;
|
||||
|
||||
Reference in New Issue
Block a user