121 lines
4.2 KiB
Zig
121 lines
4.2 KiB
Zig
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;
|
|
|
|
const CommandBuffer = base.CommandBuffer;
|
|
const SoftDevice = @import("SoftDevice.zig");
|
|
const SoftCommandBuffer = @import("SoftCommandBuffer.zig");
|
|
|
|
const VkError = base.VkError;
|
|
|
|
const Self = @This();
|
|
pub const Interface = base.Queue;
|
|
|
|
interface: Interface,
|
|
lock: std.Io.RwLock,
|
|
|
|
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;
|
|
errdefer allocator.destroy(self);
|
|
|
|
var interface = try Interface.init(allocator, device, index, family_index, flags);
|
|
|
|
interface.dispatch_table = &.{
|
|
.bindSparse = bindSparse,
|
|
.submit = submit,
|
|
.waitIdle = waitIdle,
|
|
};
|
|
|
|
self.* = .{
|
|
.interface = interface,
|
|
.lock = .init,
|
|
};
|
|
return &self.interface;
|
|
}
|
|
|
|
pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) VkError!void {
|
|
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
|
allocator.destroy(self);
|
|
}
|
|
|
|
pub fn bindSparse(interface: *Interface, info: []const vk.BindSparseInfo, fence: ?*base.Fence) VkError!void {
|
|
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
|
_ = self;
|
|
_ = info;
|
|
_ = fence;
|
|
return VkError.FeatureNotPresent;
|
|
}
|
|
|
|
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();
|
|
|
|
// Lock here to avoid acquiring it in `waitIdle` before runners start
|
|
self.lock.lockShared(io) catch return VkError.DeviceLost;
|
|
defer self.lock.unlockShared(io);
|
|
|
|
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,
|
|
};
|
|
const runners_counter = allocator.create(RefCounter) catch return VkError.OutOfDeviceMemory;
|
|
runners_counter.* = .init;
|
|
_ = soft_device.interface.io().async(Self.taskRunner, .{ self, cloned_info, p_fence, runners_counter });
|
|
}
|
|
}
|
|
|
|
pub fn waitIdle(interface: *Interface) VkError!void {
|
|
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
|
|
const io = interface.owner.io();
|
|
|
|
self.lock.lock(io) catch return VkError.DeviceLost;
|
|
defer self.lock.unlock(io);
|
|
}
|
|
|
|
fn taskRunner(self: *Self, info: Interface.SubmitInfo, p_fence: ?*base.Fence, runners_counter: *RefCounter) void {
|
|
const io = self.interface.owner.io();
|
|
|
|
self.lock.lockShared(io) catch return;
|
|
defer self.lock.unlockShared(io);
|
|
|
|
runners_counter.ref();
|
|
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());
|
|
}
|
|
|
|
var execution_device: ExecutionDevice = .init;
|
|
execution_device.setup(soft_device);
|
|
defer execution_device.deinit();
|
|
|
|
for (info.command_buffers.items) |command_buffer| {
|
|
const soft_command_buffer: *SoftCommandBuffer = @alignCast(@fieldParentPtr("interface", command_buffer));
|
|
soft_command_buffer.execute(&execution_device);
|
|
}
|
|
|
|
if (p_fence) |fence| {
|
|
if (runners_counter.getRefsCount() == 1) {
|
|
fence.signal() catch {};
|
|
}
|
|
}
|
|
}
|