rework of the queue
This commit is contained in:
19
src/soft/Executor.zig
git.filemode.normal_file
19
src/soft/Executor.zig
git.filemode.normal_file
@@ -0,0 +1,19 @@
|
||||
const std = @import("std");
|
||||
const vk = @import("vulkan");
|
||||
|
||||
const cmd = @import("base").commands;
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn init() Self {
|
||||
return .{};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self) void {
|
||||
_ = self;
|
||||
}
|
||||
|
||||
pub fn dispatch(self: *Self, command: *const cmd.Command) void {
|
||||
_ = self;
|
||||
_ = command;
|
||||
}
|
||||
@@ -2,6 +2,8 @@ const std = @import("std");
|
||||
const vk = @import("vulkan");
|
||||
const base = @import("base");
|
||||
|
||||
const lib = @import("lib.zig");
|
||||
|
||||
const VkError = base.VkError;
|
||||
const Device = base.Device;
|
||||
|
||||
@@ -21,6 +23,8 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v
|
||||
.getMemoryRequirements = getMemoryRequirements,
|
||||
};
|
||||
|
||||
interface.allowed_memory_types = lib.MEMORY_TYPE_GENERIC_BIT;
|
||||
|
||||
self.* = .{
|
||||
.interface = interface,
|
||||
};
|
||||
@@ -33,6 +37,14 @@ pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) void {
|
||||
}
|
||||
|
||||
pub fn getMemoryRequirements(interface: *Interface, requirements: *vk.MemoryRequirements) void {
|
||||
_ = interface;
|
||||
_ = requirements;
|
||||
requirements.alignment = lib.MEMORY_REQUIREMENTS_ALIGNMENT;
|
||||
if (interface.usage.uniform_texel_buffer_bit or interface.usage.uniform_texel_buffer_bit) {
|
||||
requirements.alignment = @max(requirements.alignment, lib.MIN_TEXEL_BUFFER_ALIGNMENT);
|
||||
}
|
||||
if (interface.usage.storage_buffer_bit) {
|
||||
requirements.alignment = @max(requirements.alignment, lib.MIN_STORAGE_BUFFER_ALIGNMENT);
|
||||
}
|
||||
if (interface.usage.uniform_buffer_bit) {
|
||||
requirements.alignment = @max(requirements.alignment, lib.MIN_UNIFORM_BUFFER_ALIGNMENT);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v
|
||||
interface.dispatch_table = &.{
|
||||
.begin = begin,
|
||||
.end = end,
|
||||
.fillBuffer = fillBuffer,
|
||||
.reset = reset,
|
||||
};
|
||||
|
||||
@@ -38,15 +39,29 @@ pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) void {
|
||||
}
|
||||
|
||||
pub fn begin(interface: *Interface, info: *const vk.CommandBufferBeginInfo) VkError!void {
|
||||
// No-op
|
||||
_ = interface;
|
||||
_ = info;
|
||||
}
|
||||
|
||||
pub fn end(interface: *Interface) VkError!void {
|
||||
// No-op
|
||||
_ = interface;
|
||||
}
|
||||
|
||||
pub fn reset(interface: *Interface, flags: vk.CommandBufferResetFlags) VkError!void {
|
||||
// No-op
|
||||
_ = interface;
|
||||
_ = flags;
|
||||
}
|
||||
|
||||
// Commands ====================================================================================================
|
||||
|
||||
pub fn fillBuffer(interface: *Interface, buffer: *base.Buffer, offset: vk.DeviceSize, size: vk.DeviceSize, data: u32) VkError!void {
|
||||
// No-op
|
||||
_ = interface;
|
||||
_ = buffer;
|
||||
_ = offset;
|
||||
_ = size;
|
||||
_ = data;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ interface: Interface,
|
||||
mutex: std.Thread.Mutex,
|
||||
condition: std.Thread.Condition,
|
||||
is_signaled: bool,
|
||||
/// Used by impl queues to know when the fence should be signaled
|
||||
concurrent_submits_count: std.atomic.Value(usize),
|
||||
|
||||
pub fn create(device: *Device, allocator: std.mem.Allocator, info: *const vk.FenceCreateInfo) VkError!*Self {
|
||||
const self = allocator.create(Self) catch return VkError.OutOfHostMemory;
|
||||
@@ -32,6 +34,7 @@ pub fn create(device: *Device, allocator: std.mem.Allocator, info: *const vk.Fen
|
||||
.mutex = std.Thread.Mutex{},
|
||||
.condition = std.Thread.Condition{},
|
||||
.is_signaled = info.flags.signaled_bit,
|
||||
.concurrent_submits_count = std.atomic.Value(usize).init(0),
|
||||
};
|
||||
return self;
|
||||
}
|
||||
|
||||
@@ -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 {};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ const std = @import("std");
|
||||
const vk = @import("vulkan");
|
||||
pub const base = @import("base");
|
||||
|
||||
pub const Executor = @import("Executor.zig");
|
||||
|
||||
pub const SoftInstance = @import("SoftInstance.zig");
|
||||
pub const SoftDevice = @import("SoftDevice.zig");
|
||||
pub const SoftPhysicalDevice = @import("SoftPhysicalDevice.zig");
|
||||
@@ -22,6 +24,19 @@ pub const VULKAN_VERSION = vk.makeApiVersion(0, 1, 0, 0);
|
||||
pub const DRIVER_VERSION = vk.makeApiVersion(0, 0, 0, 1);
|
||||
pub const DEVICE_ID = 0x600DCAFE;
|
||||
|
||||
/// Generic system memory.
|
||||
pub const MEMORY_TYPE_GENERIC_BIT = 0x1;
|
||||
|
||||
/// 16 bytes for 128-bit vector types.
|
||||
pub const MEMORY_REQUIREMENTS_ALIGNMENT = 16;
|
||||
|
||||
/// Vulkan 1.2 requires buffer offset alignment to be at most 256.
|
||||
pub const MIN_TEXEL_BUFFER_ALIGNMENT = 256;
|
||||
/// Vulkan 1.2 requires buffer offset alignment to be at most 256.
|
||||
pub const MIN_UNIFORM_BUFFER_ALIGNMENT = 256;
|
||||
/// Vulkan 1.2 requires buffer offset alignment to be at most 256.
|
||||
pub const MIN_STORAGE_BUFFER_ALIGNMENT = 256;
|
||||
|
||||
pub const std_options = base.std_options;
|
||||
|
||||
comptime {
|
||||
|
||||
Reference in New Issue
Block a user