Refactor literally everything

This commit is contained in:
2025-11-26 01:19:20 +01:00
parent d6a4b8c1fe
commit 9f2d1e4608
22 changed files with 2070 additions and 1034 deletions

View File

@@ -281,7 +281,17 @@ pub fn init(allocator: std.mem.Allocator, maybe_window: ?*glfw.Window) !Engine {
try enabled_extensions.appendBounded(vk.extensions.khr_swapchain.name);
}
const enabled_features_vulkan10: vk.PhysicalDeviceFeatures = .{
.shader_int_16 = .true,
};
var enabled_features_vulkan11: vk.PhysicalDeviceVulkan11Features = .{
.storage_buffer_16_bit_access = .true,
.uniform_and_storage_buffer_16_bit_access = .true,
};
var enabled_features_vulkan12: vk.PhysicalDeviceVulkan12Features = .{
.p_next = &enabled_features_vulkan11,
.descriptor_binding_partially_bound = .true,
.descriptor_binding_variable_descriptor_count = .true,
.runtime_descriptor_array = .true,
@@ -294,6 +304,7 @@ pub fn init(allocator: std.mem.Allocator, maybe_window: ?*glfw.Window) !Engine {
.p_queue_create_infos = queue_create_info.items.ptr,
.enabled_extension_count = @intCast(enabled_extensions.items.len),
.pp_enabled_extension_names = enabled_extensions.items.ptr,
.p_enabled_features = &enabled_features_vulkan10,
}, &vk_allocator.interface);
};
@@ -561,3 +572,541 @@ fn findPresentationQueueFamily(queue_families_properties: []const vk.QueueFamily
return null;
}
// --- VULKAN WRAPPERS ---------------------------------------------------------
pub const BufferCreateInfo = struct {
flags: vk.BufferCreateFlags = .{},
size: vk.DeviceSize,
usage: vk.BufferUsageFlags,
queue_family_indices: []const u32 = &.{},
};
pub const DescriptorPoolCreateInfo = struct {
flags: vk.DescriptorPoolCreateFlags = .{},
max_sets: u32,
pool_sizes: []const vk.DescriptorPoolSize = &.{},
};
pub const DescriptorSetAllocateInfo = struct {
descriptor_pool: vk.DescriptorPool,
set_layouts: []const vk.DescriptorSetLayout,
variable_descriptor_counts: []const u32 = &.{},
};
pub const DescriptorSetLayoutBinding = struct {
binding: u32,
descriptor_type: vk.DescriptorType,
descriptor_count: u32,
stage_flags: vk.ShaderStageFlags,
immutable_samplers: []const vk.Sampler = &.{},
flags: vk.DescriptorBindingFlags = .{},
};
pub const DescriptorSetLayoutCreateInfo = struct {
flags: vk.DescriptorSetLayoutCreateFlags = .{},
bindings: []const DescriptorSetLayoutBinding = &.{},
};
pub const DescriptorSetsUpdateInfo = struct {
writes: []const WriteDescriptorSet = &.{},
copies: []const vk.CopyDescriptorSet = &.{},
};
pub const FramebufferCreateInfo = struct {
flags: vk.FramebufferCreateFlags = .{},
render_pass: vk.RenderPass,
attachments: []const vk.ImageView = &.{},
width: u32,
height: u32,
layers: u32,
};
pub const GraphicsPipelineCreateInfo = struct {
flags: vk.PipelineCreateFlags = .{},
stages: []const PipelineShaderStageCreateInfo = &.{},
vertex_input_state: ?PipelineVertexInputStateCreateInfo = null,
input_assembly_state: ?vk.PipelineInputAssemblyStateCreateInfo = null,
tessellation_state: ?vk.PipelineTessellationStateCreateInfo = null,
viewport_state: ?PipelineViewportStateCreateInfo = null,
rasterization_state: ?vk.PipelineRasterizationStateCreateInfo = null,
multisample_state: ?vk.PipelineMultisampleStateCreateInfo = null,
depth_stencil_state: ?vk.PipelineDepthStencilStateCreateInfo = null,
color_blend_state: ?PipelineColorBlendStateCreateInfo = null,
dynamic_state: ?PipelineDynamicStateCreateInfo = null,
layout: vk.PipelineLayout = .null_handle,
render_pass: vk.RenderPass = .null_handle,
subpass: u32,
base_pipeline_handle: vk.Pipeline = .null_handle,
};
pub const ImageCreateInfo = struct {
flags: vk.ImageCreateFlags = .{},
image_type: vk.ImageType,
format: vk.Format,
extent: vk.Extent3D,
mip_levels: u32,
array_layers: u32,
samples: vk.SampleCountFlags,
tiling: vk.ImageTiling,
usage: vk.ImageUsageFlags,
queue_family_indices: []const u32 = &.{},
initial_layout: vk.ImageLayout,
};
pub const ImageViewCreateInfo = struct {
flags: vk.ImageViewCreateFlags = .{},
image: vk.Image,
view_type: vk.ImageViewType,
format: vk.Format,
components: vk.ComponentMapping = .{
.r = .identity,
.g = .identity,
.b = .identity,
.a = .identity,
},
subresource_range: vk.ImageSubresourceRange,
};
pub const PipelineColorBlendStateCreateInfo = struct {
flags: vk.PipelineColorBlendStateCreateFlags = .{},
logic_op_enable: vk.Bool32,
logic_op: vk.LogicOp,
attachments: []const vk.PipelineColorBlendAttachmentState = &.{},
blend_constants: [4]f32,
};
pub const PipelineDynamicStateCreateInfo = struct {
flags: vk.PipelineDynamicStateCreateFlags = .{},
dynamic_states: []const vk.DynamicState = &.{},
};
pub const PipelineLayoutCreateInfo = struct {
flags: vk.PipelineLayoutCreateFlags = .{},
set_layouts: []const vk.DescriptorSetLayout = &.{},
push_constant_ranges: []const vk.PushConstantRange = &.{},
};
pub const PipelineShaderStageCreateInfo = struct {
flags: vk.PipelineShaderStageCreateFlags = .{},
stage: vk.ShaderStageFlags,
module: vk.ShaderModule = .null_handle,
name: [*:0]const u8,
specialization_info: ?SpecializationInfo = null,
};
pub const PipelineVertexInputStateCreateInfo = struct {
flags: vk.PipelineVertexInputStateCreateFlags = .{},
vertex_binding_descriptions: []const vk.VertexInputBindingDescription = &.{},
vertex_attribute_descriptions: []const vk.VertexInputAttributeDescription = &.{},
};
pub const PipelineViewportStateCreateInfo = struct {
flags: vk.PipelineViewportStateCreateFlags = .{},
viewports: []const vk.Viewport = &.{},
scissors: []const vk.Rect2D = &.{},
};
pub const ShaderModuleCreateInfo = struct {
flags: vk.ShaderModuleCreateFlags = .{},
code: []align(@alignOf(u32)) const u8,
};
pub const SpecializationInfo = struct {
map_entries: []vk.SpecializationMapEntry = &.{},
data: []const u8 = &.{},
};
pub const WriteDescriptorSet = struct {
dst_set: vk.DescriptorSet,
dst_binding: u32,
dst_array_element: u32,
descriptor_type: vk.DescriptorType,
descriptor_infos: union(enum) {
image: []const vk.DescriptorImageInfo,
buffer: []const vk.DescriptorBufferInfo,
texel_buffer_view: []const vk.BufferView,
},
};
pub fn createBuffer(self: *Engine, create_info: BufferCreateInfo) !vk.Buffer {
var arena: std.heap.ArenaAllocator = .init(self.vk_allocator.allocator);
defer arena.deinit();
const allocator = arena.allocator();
var queue_family_indices_set: std.AutoArrayHashMapUnmanaged(u32, void) = .{};
for (create_info.queue_family_indices) |queue_family_index| {
try queue_family_indices_set.put(allocator, queue_family_index, {});
}
const queue_family_indices = queue_family_indices_set.keys();
const buffer = self.device.createBuffer(&.{
.flags = create_info.flags,
.size = create_info.size,
.usage = create_info.usage,
.sharing_mode = if (queue_family_indices.len > 1) .concurrent else .exclusive,
.queue_family_index_count = if (queue_family_indices.len > 1) @intCast(queue_family_indices.len) else 0,
.p_queue_family_indices = if (queue_family_indices.len > 1) queue_family_indices.ptr else null,
}, &self.vk_allocator.interface);
return buffer;
}
pub fn allocateDescriptorSets(self: *Engine, allocate_info: DescriptorSetAllocateInfo, descriptor_sets: []vk.DescriptorSet) !void {
std.debug.assert(descriptor_sets.len >= allocate_info.set_layouts.len);
const has_variable_descriptor_counts = allocate_info.variable_descriptor_counts.len > 0;
var p_next: ?*const anyopaque = null;
if (has_variable_descriptor_counts) {
p_next = &vk.DescriptorSetVariableDescriptorCountAllocateInfo{
.p_next = p_next,
.descriptor_set_count = @intCast(allocate_info.variable_descriptor_counts.len),
.p_descriptor_counts = allocate_info.variable_descriptor_counts.ptr,
};
}
try self.device.allocateDescriptorSets(&.{
.p_next = p_next,
.descriptor_pool = allocate_info.descriptor_pool,
.descriptor_set_count = @intCast(allocate_info.set_layouts.len),
.p_set_layouts = allocate_info.set_layouts.ptr,
}, descriptor_sets.ptr);
}
pub fn createDescriptorSetLayout(self: *Engine, create_info: DescriptorSetLayoutCreateInfo) !vk.DescriptorSetLayout {
var arena: std.heap.ArenaAllocator = .init(self.vk_allocator.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const has_binding_flags = blk: {
for (create_info.bindings) |binding| {
if (@as(vk.Flags, @bitCast(binding.flags)) != 0) {
break :blk true;
}
}
break :blk false;
};
var p_next: ?*const anyopaque = null;
if (has_binding_flags) {
const descriptor_set_layout_bindings_flags = try allocator.alloc(vk.DescriptorBindingFlags, create_info.bindings.len);
for (descriptor_set_layout_bindings_flags, create_info.bindings) |*x, binding| {
x.* = binding.flags;
}
p_next = &vk.DescriptorSetLayoutBindingFlagsCreateInfo{
.p_next = p_next,
.binding_count = @intCast(descriptor_set_layout_bindings_flags.len),
.p_binding_flags = descriptor_set_layout_bindings_flags.ptr,
};
}
const bindings = try allocator.alloc(vk.DescriptorSetLayoutBinding, create_info.bindings.len);
for (bindings, create_info.bindings) |*x, binding| {
x.* = .{
.binding = binding.binding,
.descriptor_type = binding.descriptor_type,
.descriptor_count = binding.descriptor_count,
.stage_flags = binding.stage_flags,
.p_immutable_samplers = binding.immutable_samplers.ptr,
};
}
const descriptor_set_layout = try self.device.createDescriptorSetLayout(&.{
.p_next = p_next,
.flags = create_info.flags,
.binding_count = @intCast(bindings.len),
.p_bindings = bindings.ptr,
}, &self.vk_allocator.interface);
return descriptor_set_layout;
}
pub fn createDescriptorPool(self: *Engine, create_info: DescriptorPoolCreateInfo) !vk.DescriptorPool {
const descriptor_pool = try self.device.createDescriptorPool(&.{
.flags = create_info.flags,
.max_sets = create_info.max_sets,
.pool_size_count = @intCast(create_info.pool_sizes.len),
.p_pool_sizes = create_info.pool_sizes.ptr,
}, &self.vk_allocator.interface);
return descriptor_pool;
}
pub fn createFence(self: *Engine, create_info: vk.FenceCreateInfo) !vk.Fence {
const fence = try self.device.createFence(&create_info, &self.vk_allocator.interface);
return fence;
}
pub fn createFramebuffer(self: *Engine, create_info: FramebufferCreateInfo) !vk.Framebuffer {
const framebuffer = try self.device.createFramebuffer(&.{
.flags = create_info.flags,
.render_pass = create_info.render_pass,
.attachment_count = @intCast(create_info.attachments.len),
.p_attachments = create_info.attachments.ptr,
.width = create_info.width,
.height = create_info.height,
.layers = create_info.layers,
}, &self.vk_allocator.interface);
return framebuffer;
}
pub fn createGraphicsPipeline(self: *Engine, create_info: GraphicsPipelineCreateInfo) !vk.Pipeline {
var arena: std.heap.ArenaAllocator = .init(self.vk_allocator.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const stages = try allocator.alloc(vk.PipelineShaderStageCreateInfo, create_info.stages.len);
for (stages, create_info.stages) |*x, *stage| {
x.* = .{
.flags = stage.flags,
.stage = stage.stage,
.p_name = stage.name,
.module = stage.module,
.p_specialization_info = blk: {
if (stage.specialization_info) |y| {
const specialization_info = try allocator.create(vk.SpecializationInfo);
specialization_info.* = .{
.map_entry_count = @intCast(y.map_entries.len),
.p_map_entries = y.map_entries.ptr,
.data_size = y.data.len,
.p_data = y.data.ptr,
};
break :blk specialization_info;
} else {
break :blk null;
}
},
};
}
const graphics_pipeline_create_infos = [_]vk.GraphicsPipelineCreateInfo{
.{
.flags = create_info.flags,
.stage_count = @intCast(stages.len),
.p_stages = stages.ptr,
.p_vertex_input_state = if (create_info.vertex_input_state) |vertex_input_state| &.{
.flags = vertex_input_state.flags,
.vertex_binding_description_count = @intCast(vertex_input_state.vertex_binding_descriptions.len),
.p_vertex_binding_descriptions = vertex_input_state.vertex_binding_descriptions.ptr,
.vertex_attribute_description_count = @intCast(vertex_input_state.vertex_attribute_descriptions.len),
.p_vertex_attribute_descriptions = vertex_input_state.vertex_attribute_descriptions.ptr,
} else null,
.p_input_assembly_state = if (create_info.input_assembly_state) |*x| x else null,
.p_tessellation_state = if (create_info.tessellation_state) |*x| x else null,
.p_viewport_state = if (create_info.viewport_state) |viewport_state| &.{
.flags = viewport_state.flags,
.viewport_count = @intCast(viewport_state.viewports.len),
.p_viewports = viewport_state.viewports.ptr,
.scissor_count = @intCast(viewport_state.scissors.len),
.p_scissors = viewport_state.scissors.ptr,
} else null,
.p_rasterization_state = if (create_info.rasterization_state) |*x| x else null,
.p_multisample_state = if (create_info.multisample_state) |*x| x else null,
.p_depth_stencil_state = if (create_info.depth_stencil_state) |*x| x else null,
.p_color_blend_state = if (create_info.color_blend_state) |color_blend_state| &.{
.flags = color_blend_state.flags,
.logic_op_enable = color_blend_state.logic_op_enable,
.logic_op = color_blend_state.logic_op,
.attachment_count = @intCast(color_blend_state.attachments.len),
.p_attachments = color_blend_state.attachments.ptr,
.blend_constants = color_blend_state.blend_constants,
} else null,
.p_dynamic_state = if (create_info.dynamic_state) |dynamic_state| &.{
.flags = dynamic_state.flags,
.dynamic_state_count = @intCast(dynamic_state.dynamic_states.len),
.p_dynamic_states = dynamic_state.dynamic_states.ptr,
} else null,
.layout = create_info.layout,
.render_pass = create_info.render_pass,
.subpass = create_info.subpass,
.base_pipeline_handle = create_info.base_pipeline_handle,
.base_pipeline_index = -1,
},
};
var pipelines: [1]vk.Pipeline = undefined;
_ = try self.device.createGraphicsPipelines(.null_handle, graphics_pipeline_create_infos.len, &graphics_pipeline_create_infos, &self.vk_allocator.interface, &pipelines);
return pipelines[0];
}
pub fn createImage(self: *Engine, create_info: ImageCreateInfo) !vk.Image {
var arena: std.heap.ArenaAllocator = .init(self.vk_allocator.allocator);
defer arena.deinit();
const allocator = arena.allocator();
var queue_family_indices_set: std.AutoArrayHashMapUnmanaged(u32, void) = .{};
for (create_info.queue_family_indices) |queue_family_index| {
try queue_family_indices_set.put(allocator, queue_family_index, {});
}
const queue_family_indices = queue_family_indices_set.keys();
const image = self.device.createImage(&.{
.flags = create_info.flags,
.image_type = create_info.image_type,
.format = create_info.format,
.extent = create_info.extent,
.mip_levels = create_info.mip_levels,
.array_layers = create_info.array_layers,
.samples = create_info.samples,
.tiling = create_info.tiling,
.usage = create_info.usage,
.sharing_mode = if (queue_family_indices.len > 1) .concurrent else .exclusive,
.queue_family_index_count = if (queue_family_indices.len > 1) @intCast(queue_family_indices.len) else 0,
.p_queue_family_indices = if (queue_family_indices.len > 1) queue_family_indices.ptr else null,
.initial_layout = create_info.initial_layout,
}, &self.vk_allocator.interface);
return image;
}
pub fn createImageView(self: *Engine, create_info: ImageViewCreateInfo) !vk.ImageView {
const image_view = try self.device.createImageView(&.{
.flags = create_info.flags,
.image = create_info.image,
.view_type = create_info.view_type,
.format = create_info.format,
.components = create_info.components,
.subresource_range = create_info.subresource_range,
}, &self.vk_allocator.interface);
return image_view;
}
pub fn createPipelineLayout(self: *Engine, create_info: PipelineLayoutCreateInfo) !vk.PipelineLayout {
const pipeline_layout = try self.device.createPipelineLayout(&.{
.flags = create_info.flags,
.set_layout_count = @intCast(create_info.set_layouts.len),
.p_set_layouts = create_info.set_layouts.ptr,
.push_constant_range_count = @intCast(create_info.push_constant_ranges.len),
.p_push_constant_ranges = create_info.push_constant_ranges.ptr,
}, &self.vk_allocator.interface);
return pipeline_layout;
}
pub fn createSampler(self: *Engine, create_info: vk.SamplerCreateInfo) !vk.Sampler {
const sampler = try self.device.createSampler(&create_info, &self.vk_allocator.interface);
return sampler;
}
pub fn createSemaphore(self: *Engine) !vk.Semaphore {
const semaphore = try self.device.createSemaphore(&.{}, &self.vk_allocator.interface);
return semaphore;
}
pub fn createShaderModule(self: *Engine, create_info: ShaderModuleCreateInfo) !vk.ShaderModule {
const shader_module = try self.device.createShaderModule(&.{
.flags = create_info.flags,
.code_size = create_info.code.len,
.p_code = @ptrCast(create_info.code.ptr),
}, &self.vk_allocator.interface);
return shader_module;
}
pub fn destroyBuffer(self: *Engine, buffer: vk.Buffer) void {
self.device.destroyBuffer(buffer, &self.vk_allocator.interface);
}
pub fn destroyDescriptorPool(self: *Engine, descriptor_pool: vk.DescriptorPool) void {
self.device.destroyDescriptorPool(descriptor_pool, &self.vk_allocator.interface);
}
pub fn destroyDescriptorSetLayout(self: *Engine, descriptor_set_layout: vk.DescriptorSetLayout) void {
self.device.destroyDescriptorSetLayout(descriptor_set_layout, &self.vk_allocator.interface);
}
pub fn destroyFence(self: *Engine, fence: vk.Fence) void {
self.device.destroyFence(fence, &self.vk_allocator.interface);
}
pub fn destroyFramebuffer(self: *Engine, framebuffer: vk.Framebuffer) void {
self.device.destroyFramebuffer(framebuffer, &self.vk_allocator.interface);
}
pub fn destroyImage(self: *Engine, image: vk.Image) void {
self.device.destroyImage(image, &self.vk_allocator.interface);
}
pub fn destroyImageView(self: *Engine, image_view: vk.ImageView) void {
self.device.destroyImageView(image_view, &self.vk_allocator.interface);
}
pub fn destroyPipeline(self: *Engine, pipeline: vk.Pipeline) void {
self.device.destroyPipeline(pipeline, &self.vk_allocator.interface);
}
pub fn destroyPipelineLayout(self: *Engine, pipeline_layout: vk.PipelineLayout) void {
self.device.destroyPipelineLayout(pipeline_layout, &self.vk_allocator.interface);
}
pub fn destroySampler(self: *Engine, sampler: vk.Sampler) void {
self.device.destroySampler(sampler, &self.vk_allocator.interface);
}
pub fn destroySemaphore(self: *Engine, semaphore: vk.Semaphore) void {
self.device.destroySemaphore(semaphore, &self.vk_allocator.interface);
}
pub fn destroyShaderModule(self: *Engine, shader_module: vk.ShaderModule) void {
self.device.destroyShaderModule(shader_module, &self.vk_allocator.interface);
}
pub fn freeMemory(self: *Engine, device_memory: vk.DeviceMemory) void {
self.device.freeMemory(device_memory, &self.vk_allocator.interface);
}
pub fn resetFence(self: *Engine, fence: vk.Fence) !void {
try self.device.resetFences(1, @ptrCast(&fence));
}
pub fn updateDescriptorSets(self: *Engine, update_info: DescriptorSetsUpdateInfo) !void {
var arena: std.heap.ArenaAllocator = .init(self.vk_allocator.allocator);
defer arena.deinit();
const allocator = arena.allocator();
const descriptor_writes = try allocator.alloc(vk.WriteDescriptorSet, update_info.writes.len);
for (descriptor_writes, update_info.writes) |*x, write| {
x.* = switch (write.descriptor_infos) {
.image => |y| .{
.dst_set = write.dst_set,
.dst_binding = write.dst_binding,
.dst_array_element = write.dst_array_element,
.descriptor_count = @intCast(y.len),
.descriptor_type = write.descriptor_type,
.p_image_info = y.ptr,
.p_buffer_info = &.{},
.p_texel_buffer_view = &.{},
},
.buffer => |y| .{
.dst_set = write.dst_set,
.dst_binding = write.dst_binding,
.dst_array_element = write.dst_array_element,
.descriptor_count = @intCast(y.len),
.descriptor_type = write.descriptor_type,
.p_image_info = &.{},
.p_buffer_info = y.ptr,
.p_texel_buffer_view = &.{},
},
.texel_buffer_view => |y| .{
.dst_set = write.dst_set,
.dst_binding = write.dst_binding,
.dst_array_element = write.dst_array_element,
.descriptor_count = @intCast(y.len),
.descriptor_type = write.descriptor_type,
.p_image_info = &.{},
.p_buffer_info = &.{},
.p_texel_buffer_view = y.ptr,
},
};
}
self.device.updateDescriptorSets(
@intCast(descriptor_writes.len),
descriptor_writes.ptr,
@intCast(update_info.copies.len),
update_info.copies.ptr,
);
}
pub fn waitForFence(self: *Engine, fence: vk.Fence) !void {
_ = try self.device.waitForFences(1, @ptrCast(&fence), .true, std.math.maxInt(u64));
}