Add vertex input

This commit is contained in:
Przemysław Gasiǹski 2024-07-14 19:53:53 +02:00
parent c4193db891
commit 2ec8a315c6
9 changed files with 179 additions and 88 deletions

View file

@ -8,6 +8,9 @@ const Utilities = @import("utilities.zig");
const QueueFamilyIndices = Utilities.QueueFamilyIndices;
const SwapchainDetails = Utilities.SwapchainDetails;
const SwapchainImage = Utilities.SwapchainImage;
const Vertex = Utilities.Vertex;
const Mesh = @import("Mesh.zig");
const enable_validation_layers = builtin.mode == .Debug;
const validation_layers = [_][*:0]const u8{"VK_LAYER_KHRONOS_validation"};
@ -28,10 +31,10 @@ const BaseDispatch = vk.BaseWrapper(apis);
const InstanceDispatch = vk.InstanceWrapper(apis);
const DeviceDispatch = vk.DeviceWrapper(apis);
const Instance = vk.InstanceProxy(apis);
const Device = vk.DeviceProxy(apis);
const Queue = vk.QueueProxy(apis);
const CommandBuffer = vk.CommandBufferProxy(apis);
pub const Instance = vk.InstanceProxy(apis);
pub const Device = vk.DeviceProxy(apis);
pub const Queue = vk.QueueProxy(apis);
pub const CommandBuffer = vk.CommandBufferProxy(apis);
pub const VulkanRenderer = struct {
const Self = @This();
@ -44,6 +47,9 @@ pub const VulkanRenderer = struct {
current_frame: u32 = 0,
// Scene objects
first_mesh: Mesh,
// Main
instance: Instance,
physical_device: vk.PhysicalDevice,
@ -95,6 +101,16 @@ pub const VulkanRenderer = struct {
try self.getPhysicalDevice();
try self.createLogicalDevice();
// Create mesh
var mesh_vertices = [_]Vertex{
.{ .pos = .{ 0.0, -0.4, 0.0 } },
.{ .pos = .{ 0.4, 0.4, 0.0 } },
.{ .pos = .{ -0.4, 0.4, 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();
@ -157,11 +173,13 @@ pub const VulkanRenderer = struct {
}
pub fn deinit(self: *Self) void {
self.device.deviceWaitIdle() catch undefined;
if (enable_validation_layers) {
self.instance.destroyDebugUtilsMessengerEXT(self.debug_utils.?, null);
}
self.device.deviceWaitIdle() catch undefined;
self.first_mesh.destroyVertexBuffer();
for (0..MAX_FRAME_DRAWS) |i| {
self.device.destroySemaphore(self.render_finished[i], null);
@ -470,10 +488,32 @@ pub const VulkanRenderer = struct {
fragment_shader_create_info,
};
// -- Vertex input (TODO: Put in vertex descriptions when resources created) --
// How the data for a single vertex (including info such as position, colour, texture coords, normals, etc...) is as a whole
const binding_description: vk.VertexInputBindingDescription = .{
.binding = 0, // Can bind multiple streams of data, this defines which one
.stride = @sizeOf(Vertex), // Size of simple vertex object
.input_rate = .vertex, // How to move between data after each vertex
// vertex: move to the next vertex
// instance: move to a vertex for the next instance
};
// How the data for an attribute is defined within the vertex
const attribute_descriptions = [_]vk.VertexInputAttributeDescription{
// Position attribute
.{
.binding = 0, // Which binding the data is at (should be same as above)
.location = 0, // Location in shader where data will be read from
.format = vk.Format.r32g32b32_sfloat, // Format the data will take (also helps define size of data)
.offset = @offsetOf(Vertex, "pos"), // Where this attribute is defined in data for a single vertex
},
};
// -- Vertex input --
const vertex_input_create_info: vk.PipelineVertexInputStateCreateInfo = .{
.p_vertex_binding_descriptions = null, // List of vertex binding descriptions (data spacing, stride info)
.p_vertex_attribute_descriptions = null, // List of vertex attribute descriptions (data format and where to bind to/from)
.vertex_binding_description_count = 1,
.p_vertex_binding_descriptions = @ptrCast(&binding_description), // List of vertex binding descriptions (data spacing, stride info)
.vertex_attribute_description_count = @intCast(attribute_descriptions.len),
.p_vertex_attribute_descriptions = &attribute_descriptions, // List of vertex attribute descriptions (data format and where to bind to/from)
};
// -- Input assembly --
@ -697,15 +737,24 @@ pub const VulkanRenderer = struct {
// Begin render pass
command_buffer.beginRenderPass(&render_pass_begin_info, vk.SubpassContents.@"inline");
// Bind pipeline to be used in render pass
command_buffer.bindPipeline(.graphics, self.graphics_pipeline);
{
// Bind pipeline to be used in render pass
command_buffer.bindPipeline(.graphics, self.graphics_pipeline);
// Needed when using dynamic state
command_buffer.setViewport(0, 1, @ptrCast(&self.viewport));
command_buffer.setScissor(0, 1, @ptrCast(&self.scissor));
// Buffers to bind
const vertex_buffers = [_]vk.Buffer{self.first_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);
// Execute a pipeline
command_buffer.draw(3, 1, 0, 0);
// Needed when using dynamic state
command_buffer.setViewport(0, 1, @ptrCast(&self.viewport));
command_buffer.setScissor(0, 1, @ptrCast(&self.scissor));
// Execute a pipeline
command_buffer.draw(self.first_mesh.vertex_count, 1, 0, 0);
}
// End render pass
command_buffer.endRenderPass();