Add vertex input
This commit is contained in:
parent
c4193db891
commit
2ec8a315c6
9 changed files with 179 additions and 88 deletions
94
src/Mesh.zig
Normal file
94
src/Mesh.zig
Normal file
|
@ -0,0 +1,94 @@
|
|||
const std = @import("std");
|
||||
const vk = @import("vulkan");
|
||||
const Utilities = @import("utilities.zig");
|
||||
const Vertex = Utilities.Vertex;
|
||||
const Device = @import("vulkan_renderer.zig").Device;
|
||||
const Instance = @import("vulkan_renderer.zig").Instance;
|
||||
|
||||
const Self = @This();
|
||||
|
||||
vertex_count: u32,
|
||||
vertex_buffer: vk.Buffer,
|
||||
vertex_buffer_memory: vk.DeviceMemory,
|
||||
|
||||
instance: Instance,
|
||||
physical_device: vk.PhysicalDevice,
|
||||
device: Device,
|
||||
|
||||
pub fn new(instance: Instance, pdev: vk.PhysicalDevice, device: Device, vertices: []const Vertex) !Self {
|
||||
var mesh: Self = undefined;
|
||||
|
||||
mesh.vertex_count = @intCast(vertices.len);
|
||||
mesh.instance = instance;
|
||||
mesh.physical_device = pdev;
|
||||
mesh.device = device;
|
||||
try mesh.createVertexBuffer(vertices);
|
||||
|
||||
return mesh;
|
||||
}
|
||||
|
||||
pub fn destroyVertexBuffer(self: Self) void {
|
||||
self.device.destroyBuffer(self.vertex_buffer, null);
|
||||
self.device.freeMemory(self.vertex_buffer_memory, null);
|
||||
}
|
||||
|
||||
fn createVertexBuffer(self: *Self, vertices: []const Vertex) !void {
|
||||
// Create vertex buffer
|
||||
// Information to create buffer (doesn't include assigning memory)
|
||||
const buffer_create_info: vk.BufferCreateInfo = .{
|
||||
.size = @sizeOf(Vertex) * vertices.len, // Size of buffer (size of 1 vertex * number of vertices)
|
||||
.usage = .{ .vertex_buffer_bit = true }, // Multiple types of buffer possible, we want vertex buffer
|
||||
.sharing_mode = .exclusive, // Similar to swapchain images, can share vertex buffers
|
||||
};
|
||||
|
||||
self.vertex_buffer = try self.device.createBuffer(&buffer_create_info, null);
|
||||
|
||||
// Get buffer memory requirements
|
||||
const mem_requirements = self.device.getBufferMemoryRequirements(self.vertex_buffer);
|
||||
|
||||
// Allocate memory to buffer
|
||||
const allocate_info: vk.MemoryAllocateInfo = .{
|
||||
.allocation_size = mem_requirements.size,
|
||||
.memory_type_index = self.findMemoryTypeIndex(
|
||||
mem_requirements.memory_type_bits, // Index of memory type of physical device that has required bit flags
|
||||
.{
|
||||
.host_visible_bit = true, // CPU can interact with memory
|
||||
.host_coherent_bit = true, // Allows placement of data straight into buffer after mapping (otherwise would have to specify manually)
|
||||
},
|
||||
),
|
||||
};
|
||||
|
||||
// Allocate memory to vkDeviceMemory
|
||||
self.vertex_buffer_memory = try self.device.allocateMemory(&allocate_info, null);
|
||||
|
||||
// Allocate memory to given vertex buffer
|
||||
try self.device.bindBufferMemory(self.vertex_buffer, self.vertex_buffer_memory, 0);
|
||||
|
||||
// Map memory to vertex
|
||||
// 1. Create pointer to a point in normal memory
|
||||
// 2. Map the vertex buffer memory to that point
|
||||
const data = try self.device.mapMemory(self.vertex_buffer_memory, 0, vk.WHOLE_SIZE, .{});
|
||||
|
||||
// 3. Copy memory from vertices vector to the point in memory
|
||||
const gpu_vertices: [*]Vertex = @ptrCast(@alignCast(data));
|
||||
@memcpy(gpu_vertices, vertices[0..]);
|
||||
|
||||
// 4. Unmap the vertex buffer memory
|
||||
self.device.unmapMemory(self.vertex_buffer_memory);
|
||||
}
|
||||
|
||||
fn findMemoryTypeIndex(self: Self, allowed_types: u32, properties: vk.MemoryPropertyFlags) u32 {
|
||||
// Get properties of physical device memory
|
||||
const memory_properties = self.instance.getPhysicalDeviceMemoryProperties(self.physical_device);
|
||||
const mem_type_count = memory_properties.memory_type_count;
|
||||
|
||||
for (memory_properties.memory_types[0..mem_type_count], 0..mem_type_count) |mem_type, i| {
|
||||
// Index of memory type must match corresponding bit in allowed_types
|
||||
if (allowed_types & (@as(u32, 1) << @truncate(i)) != 0 and mem_type.property_flags.contains(properties)) {
|
||||
// Return the index of the valid memory type
|
||||
return @truncate(i);
|
||||
}
|
||||
}
|
||||
|
||||
unreachable;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue