|
|
|
@ -43,7 +43,7 @@ const UboViewProjection = struct {
|
|
|
|
|
view: zm.Mat align(16),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub const UboModel = struct {
|
|
|
|
|
pub const Model = struct {
|
|
|
|
|
model: zm.Mat align(16),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -81,6 +81,7 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
|
|
|
|
|
// Descriptors
|
|
|
|
|
descriptor_set_layout: vk.DescriptorSetLayout,
|
|
|
|
|
push_constant_range: vk.PushConstantRange,
|
|
|
|
|
|
|
|
|
|
descriptor_pool: vk.DescriptorPool,
|
|
|
|
|
descriptor_sets: []vk.DescriptorSet,
|
|
|
|
@ -88,9 +89,6 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
vp_uniform_buffer: []vk.Buffer,
|
|
|
|
|
vp_uniform_buffer_memory: []vk.DeviceMemory,
|
|
|
|
|
|
|
|
|
|
model_duniform_buffer: []vk.Buffer,
|
|
|
|
|
model_duniform_buffer_memory: []vk.DeviceMemory,
|
|
|
|
|
|
|
|
|
|
// Pipeline
|
|
|
|
|
graphics_pipeline: vk.Pipeline,
|
|
|
|
|
pipeline_layout: vk.PipelineLayout,
|
|
|
|
@ -103,10 +101,6 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
swapchain_image_format: vk.Format,
|
|
|
|
|
extent: vk.Extent2D,
|
|
|
|
|
|
|
|
|
|
min_uniform_buffer_offset: vk.DeviceSize,
|
|
|
|
|
model_uniform_alignment: usize,
|
|
|
|
|
model_transfer_space: [MAX_OBJECTS]UboModel,
|
|
|
|
|
|
|
|
|
|
// Synchronisation
|
|
|
|
|
image_available: [MAX_FRAME_DRAWS]vk.Semaphore,
|
|
|
|
|
render_finished: [MAX_FRAME_DRAWS]vk.Semaphore,
|
|
|
|
@ -134,6 +128,7 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
try self.createSwapchain();
|
|
|
|
|
try self.createRenderPass();
|
|
|
|
|
try self.createDescriptorSetLayout();
|
|
|
|
|
try self.createPushConstantRange();
|
|
|
|
|
try self.createGraphicsPipeline();
|
|
|
|
|
try self.createFramebuffers();
|
|
|
|
|
try self.createCommandPool();
|
|
|
|
@ -201,12 +196,10 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
self.meshes = [_]Mesh{ first_mesh, second_mesh };
|
|
|
|
|
|
|
|
|
|
try self.createCommandBuffers();
|
|
|
|
|
try self.allocateDynamicBufferTransferSpace();
|
|
|
|
|
try self.createUniformBuffers();
|
|
|
|
|
try self.createDescriptorPool();
|
|
|
|
|
try self.createDescriptorSets();
|
|
|
|
|
|
|
|
|
|
try self.recordCommands();
|
|
|
|
|
try self.createSynchronisation();
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
@ -220,7 +213,12 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
|
|
|
|
|
pub fn draw(self: *Self) !void {
|
|
|
|
|
// Wait for given fence to signal (open) from last draw before continuing
|
|
|
|
|
_ = try self.device.waitForFences(1, @ptrCast(&self.draw_fences[self.current_frame]), vk.TRUE, std.math.maxInt(u64));
|
|
|
|
|
_ = try self.device.waitForFences(
|
|
|
|
|
1,
|
|
|
|
|
@ptrCast(&self.draw_fences[self.current_frame]),
|
|
|
|
|
vk.TRUE,
|
|
|
|
|
std.math.maxInt(u64),
|
|
|
|
|
);
|
|
|
|
|
// Manually reset (close) fences
|
|
|
|
|
try self.device.resetFences(1, @ptrCast(&self.draw_fences[self.current_frame]));
|
|
|
|
|
|
|
|
|
@ -233,6 +231,7 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
.null_handle,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
try self.recordCommands(image_index_result.image_index);
|
|
|
|
|
try self.updateUniformBuffers(image_index_result.image_index);
|
|
|
|
|
|
|
|
|
|
// -- Submit command buffer to render
|
|
|
|
@ -281,13 +280,9 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
for (0..self.swapchain_images.len) |i| {
|
|
|
|
|
self.device.destroyBuffer(self.vp_uniform_buffer[i], null);
|
|
|
|
|
self.device.freeMemory(self.vp_uniform_buffer_memory[i], null);
|
|
|
|
|
self.device.destroyBuffer(self.model_duniform_buffer[i], null);
|
|
|
|
|
self.device.freeMemory(self.model_duniform_buffer_memory[i], null);
|
|
|
|
|
}
|
|
|
|
|
self.allocator.free(self.vp_uniform_buffer);
|
|
|
|
|
self.allocator.free(self.vp_uniform_buffer_memory);
|
|
|
|
|
self.allocator.free(self.model_duniform_buffer);
|
|
|
|
|
self.allocator.free(self.model_duniform_buffer_memory);
|
|
|
|
|
self.allocator.free(self.descriptor_sets);
|
|
|
|
|
|
|
|
|
|
for (self.meshes) |mesh| {
|
|
|
|
@ -576,16 +571,7 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
.p_immutable_samplers = null, // For texture: can make smapler data immutable by specifying in layout
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Model binding info
|
|
|
|
|
const model_layout_binding: vk.DescriptorSetLayoutBinding = .{
|
|
|
|
|
.binding = 1,
|
|
|
|
|
.descriptor_type = .uniform_buffer_dynamic,
|
|
|
|
|
.descriptor_count = 1,
|
|
|
|
|
.stage_flags = .{ .vertex_bit = true },
|
|
|
|
|
.p_immutable_samplers = null,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const layout_bindings = [_]vk.DescriptorSetLayoutBinding{ vp_layout_binding, model_layout_binding };
|
|
|
|
|
const layout_bindings = [_]vk.DescriptorSetLayoutBinding{vp_layout_binding};
|
|
|
|
|
|
|
|
|
|
// Create descriptor set layout with given bindings
|
|
|
|
|
const layout_create_info: vk.DescriptorSetLayoutCreateInfo = .{
|
|
|
|
@ -597,6 +583,15 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
self.descriptor_set_layout = try self.device.createDescriptorSetLayout(&layout_create_info, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn createPushConstantRange(self: *Self) !void {
|
|
|
|
|
// Define push constant values (no 'create' needed)
|
|
|
|
|
self.push_constant_range = .{
|
|
|
|
|
.stage_flags = .{ .vertex_bit = true }, // Shader stage push constant will go to
|
|
|
|
|
.offset = 0, // Offset into given data to pass to push constant
|
|
|
|
|
.size = @sizeOf(Model), // Size of data being passed
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn createGraphicsPipeline(self: *Self) !void {
|
|
|
|
|
// Create shader modules
|
|
|
|
|
const vert = try self.device.createShaderModule(&.{
|
|
|
|
@ -757,6 +752,8 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
const pipeline_layout_create_info: vk.PipelineLayoutCreateInfo = .{
|
|
|
|
|
.set_layout_count = 1,
|
|
|
|
|
.p_set_layouts = @ptrCast(&self.descriptor_set_layout),
|
|
|
|
|
.push_constant_range_count = 1,
|
|
|
|
|
.p_push_constant_ranges = @ptrCast(&self.push_constant_range),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.pipeline_layout = try self.device.createPipelineLayout(&pipeline_layout_create_info, null);
|
|
|
|
@ -820,6 +817,7 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
const pool_create_info: vk.CommandPoolCreateInfo = .{
|
|
|
|
|
// Queue family type that buffers from this command pool will use
|
|
|
|
|
.queue_family_index = queue_family_indices.graphics_family.?,
|
|
|
|
|
.flags = .{ .reset_command_buffer_bit = true },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Create a graphics queue family command pool
|
|
|
|
@ -861,14 +859,9 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
// View projection buffer size
|
|
|
|
|
const vp_buffer_size: vk.DeviceSize = @sizeOf(UboViewProjection);
|
|
|
|
|
|
|
|
|
|
// Model buffer size
|
|
|
|
|
const model_buffer_size: vk.DeviceSize = self.model_uniform_alignment * MAX_OBJECTS;
|
|
|
|
|
|
|
|
|
|
// One uniform buffer for each image (and by extension, command buffer)
|
|
|
|
|
self.vp_uniform_buffer = try self.allocator.alloc(vk.Buffer, self.swapchain_images.len);
|
|
|
|
|
self.vp_uniform_buffer_memory = try self.allocator.alloc(vk.DeviceMemory, self.swapchain_images.len);
|
|
|
|
|
self.model_duniform_buffer = try self.allocator.alloc(vk.Buffer, self.swapchain_images.len);
|
|
|
|
|
self.model_duniform_buffer_memory = try self.allocator.alloc(vk.DeviceMemory, self.swapchain_images.len);
|
|
|
|
|
|
|
|
|
|
// Create the uniform buffers
|
|
|
|
|
for (0..self.vp_uniform_buffer.len) |i| {
|
|
|
|
@ -882,17 +875,6 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
&self.vp_uniform_buffer[i],
|
|
|
|
|
&self.vp_uniform_buffer_memory[i],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
try Utilities.createBuffer(
|
|
|
|
|
self.physical_device,
|
|
|
|
|
self.instance,
|
|
|
|
|
self.device,
|
|
|
|
|
model_buffer_size,
|
|
|
|
|
.{ .uniform_buffer_bit = true },
|
|
|
|
|
.{ .host_visible_bit = true, .host_coherent_bit = true },
|
|
|
|
|
&self.model_duniform_buffer[i],
|
|
|
|
|
&self.model_duniform_buffer_memory[i],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -904,14 +886,8 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
.descriptor_count = @intCast(self.vp_uniform_buffer.len),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Model pool (dynamic)
|
|
|
|
|
const model_pool_size: vk.DescriptorPoolSize = .{
|
|
|
|
|
.type = .uniform_buffer_dynamic,
|
|
|
|
|
.descriptor_count = @intCast(self.model_duniform_buffer.len),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// List of pool sizes
|
|
|
|
|
const descriptor_pool_sizes = [_]vk.DescriptorPoolSize{ vp_pool_size, model_pool_size };
|
|
|
|
|
const descriptor_pool_sizes = [_]vk.DescriptorPoolSize{vp_pool_size};
|
|
|
|
|
|
|
|
|
|
// Data to create descriptor pool
|
|
|
|
|
const pool_create_info: vk.DescriptorPoolCreateInfo = .{
|
|
|
|
@ -966,27 +942,8 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
.p_texel_buffer_view = undefined,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// -- Model descriptor
|
|
|
|
|
// Model buffer binding info
|
|
|
|
|
const model_buffer_info: vk.DescriptorBufferInfo = .{
|
|
|
|
|
.buffer = self.model_duniform_buffer[i],
|
|
|
|
|
.offset = 0,
|
|
|
|
|
.range = self.model_uniform_alignment,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const model_set_write: vk.WriteDescriptorSet = .{
|
|
|
|
|
.dst_set = self.descriptor_sets[i],
|
|
|
|
|
.dst_binding = 1,
|
|
|
|
|
.dst_array_element = 0,
|
|
|
|
|
.descriptor_type = .uniform_buffer_dynamic,
|
|
|
|
|
.descriptor_count = 1,
|
|
|
|
|
.p_buffer_info = @ptrCast(&model_buffer_info),
|
|
|
|
|
.p_image_info = undefined,
|
|
|
|
|
.p_texel_buffer_view = undefined,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// List of descriptor set writes
|
|
|
|
|
const set_writes = [_]vk.WriteDescriptorSet{ vp_set_write, model_set_write };
|
|
|
|
|
const set_writes = [_]vk.WriteDescriptorSet{vp_set_write};
|
|
|
|
|
|
|
|
|
|
// Update the descriptor sets with new buffer/binding info
|
|
|
|
|
self.device.updateDescriptorSets(@intCast(set_writes.len), &set_writes, 0, null);
|
|
|
|
@ -995,7 +952,7 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
|
|
|
|
|
fn updateUniformBuffers(self: *Self, image_index: u32) !void {
|
|
|
|
|
// Copy VP data
|
|
|
|
|
var data = try self.device.mapMemory(
|
|
|
|
|
const data = try self.device.mapMemory(
|
|
|
|
|
self.vp_uniform_buffer_memory[image_index],
|
|
|
|
|
0,
|
|
|
|
|
@sizeOf(UboViewProjection),
|
|
|
|
@ -1005,26 +962,9 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
const vp_data: *UboViewProjection = @ptrCast(@alignCast(data));
|
|
|
|
|
vp_data.* = self.ubo_view_projection;
|
|
|
|
|
self.device.unmapMemory(self.vp_uniform_buffer_memory[image_index]);
|
|
|
|
|
|
|
|
|
|
// Copy model data
|
|
|
|
|
for (self.meshes, 0..) |mesh, i| {
|
|
|
|
|
self.model_transfer_space[i] = mesh.ubo_model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Map the list of model data
|
|
|
|
|
data = try self.device.mapMemory(
|
|
|
|
|
self.model_duniform_buffer_memory[image_index],
|
|
|
|
|
0,
|
|
|
|
|
self.model_uniform_alignment * self.meshes.len,
|
|
|
|
|
.{},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const model_data: [*]UboModel = @ptrCast(@alignCast(data));
|
|
|
|
|
@memcpy(model_data, self.model_transfer_space[0..self.meshes.len]);
|
|
|
|
|
self.device.unmapMemory(self.model_duniform_buffer_memory[image_index]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn recordCommands(self: *Self) !void {
|
|
|
|
|
fn recordCommands(self: *Self, current_image: u32) !void {
|
|
|
|
|
// Information about how to begin each command
|
|
|
|
|
const buffer_begin_info: vk.CommandBufferBeginInfo = .{
|
|
|
|
|
// Buffer can be resubmitted when it has already been submitted and is awaiting execution
|
|
|
|
@ -1047,59 +987,64 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
.framebuffer = undefined,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (self.command_buffers, 0..) |command_buffer, i| {
|
|
|
|
|
render_pass_begin_info.framebuffer = self.swapchain_framebuffers[i];
|
|
|
|
|
render_pass_begin_info.framebuffer = self.swapchain_framebuffers[current_image];
|
|
|
|
|
const command_buffer = self.command_buffers[current_image];
|
|
|
|
|
|
|
|
|
|
// Start recording commands to command buffer
|
|
|
|
|
try command_buffer.beginCommandBuffer(&buffer_begin_info);
|
|
|
|
|
// Start recording commands to command buffer
|
|
|
|
|
try command_buffer.beginCommandBuffer(&buffer_begin_info);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// Begin render pass
|
|
|
|
|
command_buffer.beginRenderPass(&render_pass_begin_info, vk.SubpassContents.@"inline");
|
|
|
|
|
{
|
|
|
|
|
// Begin render pass
|
|
|
|
|
command_buffer.beginRenderPass(&render_pass_begin_info, vk.SubpassContents.@"inline");
|
|
|
|
|
|
|
|
|
|
// Needed when using dynamic state
|
|
|
|
|
command_buffer.setViewport(0, 1, @ptrCast(&self.viewport));
|
|
|
|
|
command_buffer.setScissor(0, 1, @ptrCast(&self.scissor));
|
|
|
|
|
// Needed when using dynamic state
|
|
|
|
|
command_buffer.setViewport(0, 1, @ptrCast(&self.viewport));
|
|
|
|
|
command_buffer.setScissor(0, 1, @ptrCast(&self.scissor));
|
|
|
|
|
|
|
|
|
|
for (self.meshes, 0..) |mesh, j| {
|
|
|
|
|
// Bind pipeline to be used in render pass
|
|
|
|
|
command_buffer.bindPipeline(.graphics, self.graphics_pipeline);
|
|
|
|
|
for (self.meshes) |mesh| {
|
|
|
|
|
// Bind pipeline to be used in render pass
|
|
|
|
|
command_buffer.bindPipeline(.graphics, self.graphics_pipeline);
|
|
|
|
|
|
|
|
|
|
// Buffers to bind
|
|
|
|
|
const vertex_buffers = [_]vk.Buffer{mesh.vertex_buffer};
|
|
|
|
|
// Offsets into buffers being bound
|
|
|
|
|
const offsets = [_]vk.DeviceSize{0};
|
|
|
|
|
// Command to bind vertex buffer before drawing with them
|
|
|
|
|
command_buffer.bindVertexBuffers(0, 1, &vertex_buffers, &offsets);
|
|
|
|
|
// Buffers to bind
|
|
|
|
|
const vertex_buffers = [_]vk.Buffer{mesh.vertex_buffer};
|
|
|
|
|
// Offsets into buffers being bound
|
|
|
|
|
const offsets = [_]vk.DeviceSize{0};
|
|
|
|
|
// Command to bind vertex buffer before drawing with them
|
|
|
|
|
command_buffer.bindVertexBuffers(0, 1, &vertex_buffers, &offsets);
|
|
|
|
|
|
|
|
|
|
// Bind mesh index buffer, with 0 offset and using the uint32 type
|
|
|
|
|
command_buffer.bindIndexBuffer(mesh.index_buffer, 0, .uint32);
|
|
|
|
|
// Bind mesh index buffer, with 0 offset and using the uint32 type
|
|
|
|
|
command_buffer.bindIndexBuffer(mesh.index_buffer, 0, .uint32);
|
|
|
|
|
|
|
|
|
|
// Dynamic offset amount
|
|
|
|
|
const dynamic_offset: u32 = @intCast(self.model_uniform_alignment * j);
|
|
|
|
|
// Push constants to given shader stage directly (no buffer)
|
|
|
|
|
command_buffer.pushConstants(
|
|
|
|
|
self.pipeline_layout,
|
|
|
|
|
.{ .vertex_bit = true }, // Stage to push constants to
|
|
|
|
|
0, // Offset of push constants to update
|
|
|
|
|
@sizeOf(Model), // Size of data being pushed
|
|
|
|
|
@ptrCast(&mesh.ubo_model.model), // Actual data being pushed (can be array)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Bind descriptor sets
|
|
|
|
|
command_buffer.bindDescriptorSets(
|
|
|
|
|
.graphics,
|
|
|
|
|
self.pipeline_layout,
|
|
|
|
|
0,
|
|
|
|
|
1,
|
|
|
|
|
@ptrCast(&self.descriptor_sets[i]),
|
|
|
|
|
1,
|
|
|
|
|
@ptrCast(&dynamic_offset),
|
|
|
|
|
);
|
|
|
|
|
// Bind descriptor sets
|
|
|
|
|
command_buffer.bindDescriptorSets(
|
|
|
|
|
.graphics,
|
|
|
|
|
self.pipeline_layout,
|
|
|
|
|
0,
|
|
|
|
|
1,
|
|
|
|
|
@ptrCast(&self.descriptor_sets[current_image]),
|
|
|
|
|
0,
|
|
|
|
|
null,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Execute a pipeline
|
|
|
|
|
command_buffer.drawIndexed(mesh.index_count, 1, 0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// End render pass
|
|
|
|
|
command_buffer.endRenderPass();
|
|
|
|
|
// Execute a pipeline
|
|
|
|
|
command_buffer.drawIndexed(mesh.index_count, 1, 0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop recording to command buffer
|
|
|
|
|
try command_buffer.endCommandBuffer();
|
|
|
|
|
// End render pass
|
|
|
|
|
command_buffer.endRenderPass();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop recording to command buffer
|
|
|
|
|
try command_buffer.endCommandBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn getPhysicalDevice(self: *Self) !void {
|
|
|
|
@ -1120,21 +1065,6 @@ pub const VulkanRenderer = struct {
|
|
|
|
|
// TODO Obviously needs to be something else
|
|
|
|
|
unreachable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get properties of our new device
|
|
|
|
|
const device_props = self.instance.getPhysicalDeviceProperties(self.physical_device);
|
|
|
|
|
|
|
|
|
|
self.min_uniform_buffer_offset = device_props.limits.min_uniform_buffer_offset_alignment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn allocateDynamicBufferTransferSpace(self: *Self) !void {
|
|
|
|
|
// TODO Needed in zig (we have align())?
|
|
|
|
|
// Calculate alignment of model data
|
|
|
|
|
self.model_uniform_alignment =
|
|
|
|
|
(@sizeOf(UboModel) + self.min_uniform_buffer_offset - 1) & ~(self.min_uniform_buffer_offset - 1);
|
|
|
|
|
|
|
|
|
|
// Create space in memory to hold dynamic buffer that is aligned to our required alignment and holds MAX_OBJECTS
|
|
|
|
|
// self.model_transfer_space = try self.allocator.create(UboModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn getRequiredExtensions(self: Self) ![][*:0]const u8 {
|
|
|
|
|