From 6f7378cf2138ccea8500f552a7327193535d08ec Mon Sep 17 00:00:00 2001 From: David Allemang Date: Mon, 1 Apr 2024 21:28:49 -0400 Subject: [PATCH] un-comment command buffer sections --- src/main.zig | 286 ++++++++++++++++++++++++++------------------------- 1 file changed, 148 insertions(+), 138 deletions(-) diff --git a/src/main.zig b/src/main.zig index 0ecee37..fd12717 100644 --- a/src/main.zig +++ b/src/main.zig @@ -502,145 +502,155 @@ fn copyBuffer( try vkd.queueWaitIdle(queue); } -// fn createCommandBuffers( -// pool: vk.CommandPool, -// allocator: Allocator, -// vertex_buffer: vk.Buffer, -// index_buffer: vk.Buffer, -// pipeline: vk.Pipeline, -// extent: vk.Extent2D, -// ) ![]vk.CommandBuffer { -// const cmdbufs = try allocator.alloc(vk.CommandBuffer, swapchain.swap_images.len); -// errdefer allocator.free(cmdbufs); -// -// try vkd.allocateCommandBuffers(dev, &.{ -// .command_pool = pool, -// .level = .primary, -// .command_buffer_count = @as(u32, @truncate(cmdbufs.len)), -// }, cmdbufs.ptr); -// errdefer vkd.freeCommandBuffers(dev, pool, @truncate(cmdbufs.len), cmdbufs.ptr); -// -// const clear = vk.ClearValue{ -// .color = .{ .float_32 = .{ 0, 0, 0, 1 } }, -// }; -// -// const viewport = vk.Viewport{ -// .x = 0, -// .y = 0, -// .width = @as(f32, @floatFromInt(extent.width)), -// .height = @as(f32, @floatFromInt(extent.height)), -// .min_depth = 0, -// .max_depth = 1, -// }; -// -// const scissor = vk.Rect2D{ -// .offset = .{ .x = 0, .y = 0 }, -// .extent = extent, -// }; -// -// for (cmdbufs, swapchain.swap_images) |cmdbuf, image| { -// try vkd.beginCommandBuffer(cmdbuf, &.{}); -// -// vkd.cmdPipelineBarrier( -// cmdbuf, -// .{ .top_of_pipe_bit = true }, -// .{ .color_attachment_output_bit = true }, -// .{}, -// 0, -// null, -// 0, -// null, -// 1, -// @ptrCast(&vk.ImageMemoryBarrier{ -// .src_access_mask = .{}, -// .dst_access_mask = .{ .color_attachment_write_bit = true }, -// .old_layout = .undefined, -// .new_layout = .color_attachment_optimal, -// .src_queue_family_index = 0, -// .dst_queue_family_index = 0, -// .image = image.image, -// .subresource_range = .{ -// .aspect_mask = .{ .color_bit = true }, -// .base_mip_level = 0, -// .level_count = 1, -// .base_array_layer = 0, -// .layer_count = 1, -// }, -// }), -// ); -// -// vkd.cmdSetViewport(cmdbuf, 0, 1, @ptrCast(&viewport)); -// vkd.cmdSetScissor(cmdbuf, 0, 1, @ptrCast(&scissor)); -// -// const color_attachments = [_]vk.RenderingAttachmentInfoKHR{ -// .{ -// .image_view = image.view, -// .image_layout = .color_attachment_optimal, -// .resolve_mode = .{}, -// .resolve_image_view = .null_handle, -// .resolve_image_layout = .undefined, -// .load_op = .clear, -// .store_op = .store, -// .clear_value = clear, -// }, -// }; -// -// const render_info = vk.RenderingInfoKHR{ -// .render_area = scissor, // since we always do full-frame changes -// .layer_count = 1, -// .view_mask = 0, -// .color_attachment_count = color_attachments.len, -// .p_color_attachments = &color_attachments, -// }; -// -// vkd.cmdBeginRenderingKHR(cmdbuf, &render_info); -// -// vkd.cmdBindPipeline(cmdbuf, .graphics, pipeline); -// const offset = [_]vk.DeviceSize{0}; -// vkd.cmdBindVertexBuffers(cmdbuf, 0, 1, @ptrCast(&vertex_buffer), &offset); -// vkd.cmdBindIndexBuffer(cmdbuf, index_buffer, 0, .uint16); -// vkd.cmdDrawIndexed(cmdbuf, indices.len, 1, 0, 0, 0); -// -// vkd.cmdEndRenderingKHR(cmdbuf); -// -// vkd.cmdPipelineBarrier( -// cmdbuf, -// .{ .color_attachment_output_bit = true }, -// .{ .bottom_of_pipe_bit = true }, -// .{}, -// 0, -// null, -// 0, -// null, -// 1, -// @ptrCast(&vk.ImageMemoryBarrier{ -// .src_access_mask = .{ .color_attachment_write_bit = true }, -// .dst_access_mask = .{}, -// .old_layout = .color_attachment_optimal, -// .new_layout = .present_src_khr, -// .src_queue_family_index = 0, -// .dst_queue_family_index = 0, -// .image = image.image, -// .subresource_range = .{ -// .aspect_mask = .{ .color_bit = true }, -// .base_mip_level = 0, -// .level_count = 1, -// .base_array_layer = 0, -// .layer_count = 1, -// }, -// }), -// ); -// -// try vkd.endCommandBuffer(cmdbuf); -// } -// -// return cmdbufs; -// } +fn createCommandBuffers( + views: []const vk.Image, + images: []const vk.ImageView, + dev: vk.Device, + vkd: gfx.DeviceDispatch, + pool: vk.CommandPool, + allocator: Allocator, + vertex_buffer: vk.Buffer, + index_buffer: vk.Buffer, + pipeline: vk.Pipeline, + extent: vk.Extent2D, +) ![]vk.CommandBuffer { + const cmdbufs = try allocator.alloc(vk.CommandBuffer, images.len); + errdefer allocator.free(cmdbufs); -// fn destroyCommandBuffers(gc: *const Context, pool: vk.CommandPool, allocator: Allocator, cmdbufs: []vk.CommandBuffer) void { -// vkd.freeCommandBuffers(dev, pool, @truncate(cmdbufs.len), cmdbufs.ptr); -// allocator.free(cmdbufs); -// } + try vkd.allocateCommandBuffers(dev, &.{ + .command_pool = pool, + .level = .primary, + .command_buffer_count = @as(u32, @truncate(cmdbufs.len)), + }, cmdbufs.ptr); + errdefer vkd.freeCommandBuffers(dev, pool, @truncate(cmdbufs.len), cmdbufs.ptr); + + const clear = vk.ClearValue{ + .color = .{ .float_32 = .{ 0, 0, 0, 1 } }, + }; + + const viewport = vk.Viewport{ + .x = 0, + .y = 0, + .width = @as(f32, @floatFromInt(extent.width)), + .height = @as(f32, @floatFromInt(extent.height)), + .min_depth = 0, + .max_depth = 1, + }; + + const scissor = vk.Rect2D{ + .offset = .{ .x = 0, .y = 0 }, + .extent = extent, + }; + + for (cmdbufs, images, views) |cmdbuf, image, view| { + try vkd.beginCommandBuffer(cmdbuf, &.{}); + + vkd.cmdPipelineBarrier( + cmdbuf, + .{ .top_of_pipe_bit = true }, + .{ .color_attachment_output_bit = true }, + .{}, + 0, + null, + 0, + null, + 1, + @ptrCast(&vk.ImageMemoryBarrier{ + .src_access_mask = .{}, + .dst_access_mask = .{ .color_attachment_write_bit = true }, + .old_layout = .undefined, + .new_layout = .color_attachment_optimal, + .src_queue_family_index = 0, + .dst_queue_family_index = 0, + .image = image, + .subresource_range = .{ + .aspect_mask = .{ .color_bit = true }, + .base_mip_level = 0, + .level_count = 1, + .base_array_layer = 0, + .layer_count = 1, + }, + }), + ); + + vkd.cmdSetViewport(cmdbuf, 0, 1, @ptrCast(&viewport)); + vkd.cmdSetScissor(cmdbuf, 0, 1, @ptrCast(&scissor)); + + const color_attachments = [_]vk.RenderingAttachmentInfoKHR{ + .{ + .image_view = view, + .image_layout = .color_attachment_optimal, + .resolve_mode = .{}, + .resolve_image_view = .null_handle, + .resolve_image_layout = .undefined, + .load_op = .clear, + .store_op = .store, + .clear_value = clear, + }, + }; + + const render_info = vk.RenderingInfoKHR{ + .render_area = scissor, // since we always do full-frame changes + .layer_count = 1, + .view_mask = 0, + .color_attachment_count = color_attachments.len, + .p_color_attachments = &color_attachments, + }; + + vkd.cmdBeginRenderingKHR(cmdbuf, &render_info); + + vkd.cmdBindPipeline(cmdbuf, .graphics, pipeline); + const offset = [_]vk.DeviceSize{0}; + vkd.cmdBindVertexBuffers(cmdbuf, 0, 1, @ptrCast(&vertex_buffer), &offset); + vkd.cmdBindIndexBuffer(cmdbuf, index_buffer, 0, .uint16); + vkd.cmdDrawIndexed(cmdbuf, indices.len, 1, 0, 0, 0); + + vkd.cmdEndRenderingKHR(cmdbuf); + + vkd.cmdPipelineBarrier( + cmdbuf, + .{ .color_attachment_output_bit = true }, + .{ .bottom_of_pipe_bit = true }, + .{}, + 0, + null, + 0, + null, + 1, + @ptrCast(&vk.ImageMemoryBarrier{ + .src_access_mask = .{ .color_attachment_write_bit = true }, + .dst_access_mask = .{}, + .old_layout = .color_attachment_optimal, + .new_layout = .present_src_khr, + .src_queue_family_index = 0, + .dst_queue_family_index = 0, + .image = image, + .subresource_range = .{ + .aspect_mask = .{ .color_bit = true }, + .base_mip_level = 0, + .level_count = 1, + .base_array_layer = 0, + .layer_count = 1, + }, + }), + ); + + try vkd.endCommandBuffer(cmdbuf); + } + + return cmdbufs; +} + +fn destroyCommandBuffers( + dev: vk.Device, + vkd: gfx.DeviceDispatch, + pool: vk.CommandPool, + allocator: Allocator, + cmdbufs: []vk.CommandBuffer, +) void { + vkd.freeCommandBuffers(dev, pool, @truncate(cmdbufs.len), cmdbufs.ptr); + allocator.free(cmdbufs); +} fn createPipeline(dev: vk.Device, layout: vk.PipelineLayout, format: vk.SurfaceFormatKHR, vkd: gfx.DeviceDispatch) !vk.Pipeline { const vert = try vkd.createShaderModule(dev, &.{