Merge branch 'master' of ssh://git.ody.si:2025/przmk/vulkan-zig

This commit is contained in:
Przemysław Gasiǹski 2024-07-19 23:37:54 +02:00
commit 8c60f79f7d
4 changed files with 322 additions and 79 deletions

View file

@ -48,7 +48,7 @@ pub const VulkanRenderer = struct {
current_frame: u32 = 0,
// Scene objects
first_mesh: Mesh,
meshes: [2]Mesh,
// Main
instance: Instance,
@ -101,24 +101,58 @@ pub const VulkanRenderer = struct {
try self.getPhysicalDevice();
try self.createLogicalDevice();
// Create mesh
var mesh_vertices = [_]Vertex{
.{ .pos = .{ -0.5, -0.5, 0.0 }, .col = .{ 1.0, 0.0, 0.0 } },
.{ .pos = .{ 0.5, -0.5, 0.0 }, .col = .{ 0.0, 1.0, 0.0 } },
.{ .pos = .{ 0.5, 0.5, 0.0 }, .col = .{ 0.0, 0.0, 1.0 } },
.{ .pos = .{ 0.5, 0.5, 0.0 }, .col = .{ 0.0, 0.0, 1.0 } },
.{ .pos = .{ -0.5, 0.5, 0.0 }, .col = .{ 0.0, 1.0, 0.0 } },
.{ .pos = .{ -0.5, -0.5, 0.0 }, .col = .{ 1.0, 0.0, 0.0 } },
};
self.first_mesh = try Mesh.new(self.instance, self.physical_device, self.device, &mesh_vertices);
try self.createSwapchain();
try self.createRenderPass();
try self.createGraphicsPipeline();
try self.createFramebuffers();
try self.createCommandPool();
// Create meshes
// Vertex Data
var mesh_vertices = [_]Vertex{
.{ .pos = .{ -0.1, -0.4, 0.0 }, .col = .{ 1.0, 0.0, 0.0 } }, // 0
.{ .pos = .{ -0.1, 0.4, 0.0 }, .col = .{ 0.0, 1.0, 0.0 } }, // 1
.{ .pos = .{ -0.9, 0.4, 0.0 }, .col = .{ 0.0, 0.0, 1.0 } }, // 2
.{ .pos = .{ -0.9, -0.4, 0.0 }, .col = .{ 0.0, 1.0, 0.0 } }, // 3
};
var mesh_vertices2 = [_]Vertex{
.{ .pos = .{ 0.9, -0.3, 0.0 }, .col = .{ 1.0, 0.0, 0.0 } }, // 0
.{ .pos = .{ 0.9, 0.1, 0.0 }, .col = .{ 0.0, 1.0, 0.0 } }, // 1
.{ .pos = .{ 0.1, 0.3, 0.0 }, .col = .{ 0.0, 0.0, 1.0 } }, // 2
.{ .pos = .{ 0.1, -0.3, 0.0 }, .col = .{ 0.0, 1.0, 0.0 } }, // 3
};
// Index Data
const mesh_indices = [_]u32{
0, 1, 2,
2, 3, 0,
};
const first_mesh = try Mesh.new(
self.instance,
self.physical_device,
self.device,
self.graphics_queue.handle,
self.graphics_command_pool,
&mesh_vertices,
&mesh_indices,
self.allocator,
);
const second_mesh = try Mesh.new(
self.instance,
self.physical_device,
self.device,
self.graphics_queue.handle,
self.graphics_command_pool,
&mesh_vertices2,
&mesh_indices,
self.allocator,
);
self.meshes = [_]Mesh{ first_mesh, second_mesh };
try self.createCommandBuffers();
try self.recordCommands();
try self.createSynchronisation();
@ -181,7 +215,9 @@ pub const VulkanRenderer = struct {
self.instance.destroyDebugUtilsMessengerEXT(self.debug_utils.?, null);
}
self.first_mesh.destroyVertexBuffer();
for (self.meshes) |mesh| {
mesh.destroyBuffers();
}
for (0..MAX_FRAME_DRAWS) |i| {
self.device.destroySemaphore(self.render_finished[i], null);
@ -745,23 +781,26 @@ pub const VulkanRenderer = struct {
// 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));
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{self.first_mesh.vertex_buffer};
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);
// Needed when using dynamic state
command_buffer.setViewport(0, 1, @ptrCast(&self.viewport));
command_buffer.setScissor(0, 1, @ptrCast(&self.scissor));
// Bind mesh index buffer, with 0 offset and using the uint32 type
command_buffer.bindIndexBuffer(mesh.index_buffer, 0, .uint32);
// Execute a pipeline
command_buffer.draw(self.first_mesh.vertex_count, 1, 0, 0);
command_buffer.drawIndexed(mesh.index_count, 1, 0, 0, 0);
}
// End render pass