From d67de4fbe8f434ba4a4e9b39e3ee2145914e63dd Mon Sep 17 00:00:00 2001 From: Vivianne Langdon Date: Wed, 27 Jul 2022 00:19:25 -0700 Subject: [PATCH] initialize mesh system, vulkan abstract renderer a bit, actually draw meshes (but none added) --- src/main.zig | 14 ++++------- src/rendering/Renderer.zig | 25 ++++++++++++++++++++ src/rendering/meshes.zig | 30 +++++++++++++++++++----- src/rendering/vulkan/Command.zig | 31 ++++++++++++++++++++++++ src/rendering/vulkan/MainPass.zig | 39 +++++++++++++++++-------------- src/rendering/vulkan/Renderer.zig | 6 ++--- src/rendering/vulkan/meshes.zig | 32 +++++++++++++++++++++++++ 7 files changed, 141 insertions(+), 36 deletions(-) create mode 100644 src/rendering/Renderer.zig create mode 100644 src/rendering/vulkan/meshes.zig diff --git a/src/main.zig b/src/main.zig index 3245b37..54ea63e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,7 +2,7 @@ const std = @import("std"); const glfw = @import("glfw"); const vk = @import("vulkan"); const resources = @import("resources"); -const Renderer = @import("rendering/vulkan/Renderer.zig"); +const Renderer = @import("rendering/Renderer.zig"); // TODO: const Allocator = std.mem.Allocator; @@ -13,15 +13,11 @@ pub fn main() !void { try glfw.init(.{}); defer glfw.terminate(); - var renderer = try Renderer.init(); - defer renderer.deinit(); + try Renderer.init(); + defer Renderer.deinit(); - std.debug.print("Using device: {s}\n", .{renderer.device.getName()}); - - defer renderer.deinit(); - - while (!renderer.window.handle.shouldClose()) { - try renderer.update(); + while (!Renderer.shouldClose()) { + try Renderer.update(); try glfw.pollEvents(); } } diff --git a/src/rendering/Renderer.zig b/src/rendering/Renderer.zig new file mode 100644 index 0000000..0c36c98 --- /dev/null +++ b/src/rendering/Renderer.zig @@ -0,0 +1,25 @@ +const std = @import("std"); +const VkRenderer = @import("vulkan/Renderer.zig"); +const meshes = @import("meshes.zig"); + +var s_vulkan: VkRenderer = undefined; + +pub fn init() !void { + s_vulkan = try VkRenderer.init(); + std.debug.print("Using device: {s}\n", .{s_vulkan.device.getName()}); + + meshes.init(); +} + +pub fn update() !void { + try s_vulkan.update(); +} + +pub fn deinit() void { + meshes.deinit(); + s_vulkan.deinit(); +} + +pub fn shouldClose() bool { + return s_vulkan.window.handle.shouldClose(); +} diff --git a/src/rendering/meshes.zig b/src/rendering/meshes.zig index 8060c2c..fa16003 100644 --- a/src/rendering/meshes.zig +++ b/src/rendering/meshes.zig @@ -1,23 +1,41 @@ +const std = @import("std"); const UUID = @import("uuid").UUID; +const RefTable = @import("../containers/table.zig").RefTable; const vec = @import("../math/vec.zig"); const Vec4f = vec.Vec4f; const Vec4i = vec.Vec4i; +const VkMeshId = @import("vulkan/meshes.zig").Mesh.Id; + +// TODO memory +var gpa = std.heap.GeneralPurposeAllocator(.{}){}; +const allocator = gpa.allocator(); + +const MTable = RefTable(UUID, Mesh); +var s_meshes: MTable = undefined; + +pub fn init() void { + s_meshes = MTable.init(allocator); +} + +pub fn deinit() void { + s_meshes.deinit(); +} + +pub fn get(id: Mesh.Id) ?*Mesh { + return s_meshes.getPtr(id); +} pub const Mesh = packed struct { const version = 5; + pub const Id = MTable.Id; positions: [*]Vec4f, normals: [*]Vec4f, uvs: [*]Vec4f, tex_indices: [*]Vec4i, length: i32, - id: Id, - - pub const Id = packed struct { - version: u8, - index: u24, - }; + vk_id: VkMeshId, }; pub const DiskMesh = packed struct { diff --git a/src/rendering/vulkan/Command.zig b/src/rendering/vulkan/Command.zig index 53254a3..d92c69a 100644 --- a/src/rendering/vulkan/Command.zig +++ b/src/rendering/vulkan/Command.zig @@ -2,6 +2,8 @@ const std = @import("std"); const assert = @import("std").debug.assert; const vk = @import("vulkan"); +const Vec4f = @import("../../math/vec.zig").Vec4f; + const settings = @import("settings.zig"); const Pass = @import("Pass.zig"); const Swapchain = @import("swapchain.zig").Swapchain; @@ -16,6 +18,7 @@ const Fence = @import("sync.zig").Fence; const Semaphore = @import("sync.zig").Semaphore; const Bindings = @import("Bindings.zig"); const BufferBuffer = @import("Buffer.zig"); +const meshes = @import("meshes.zig"); const queues = @import("queues.zig"); @@ -305,6 +308,34 @@ pub const Buffer = struct { device.dispatch.cmdDraw(self.handle, vertex_count, 1, first_vertex, 0); } + pub fn drawMesh(self: *Self, device: *const Device, mesh_id: meshes.Mesh.Id) void { + assert(self.handle != .null_handle); + assert(self.gfx); + if (meshes.get(mesh_id)) |vk_mesh| { + assert(vk_mesh.vert_count > 0); + assert(vk_mesh.buffer.handle != .null_handle); + + if (vk_mesh.vert_count > 0) { + const stream_size = vk_mesh.vert_count * @sizeOf(Vec4f); + const buffers = [_]vk.Buffer{ + vk_mesh.buffer.handle, + vk_mesh.buffer.handle, + vk_mesh.buffer.handle, + vk_mesh.buffer.handle, + }; + const offsets = [_]vk.DeviceSize{ + stream_size * 0, + stream_size * 1, + stream_size * 2, + stream_size * 3, + }; + + device.dispatch.cmdBindVertexBuffers(self.handle, 0, buffers.len, &buffers, &offsets); + device.dispatch.cmdDraw(self.handle, vk_mesh.vert_count, 1, 0, 0); + } + } + } + pub fn touchBuffer(self: *Self, buf: *BufferBuffer) void { assert(self.handle != .null_handle); assert(buf.handle != .null_handle); diff --git a/src/rendering/vulkan/MainPass.zig b/src/rendering/vulkan/MainPass.zig index 99ad05f..d593867 100644 --- a/src/rendering/vulkan/MainPass.zig +++ b/src/rendering/vulkan/MainPass.zig @@ -19,6 +19,7 @@ const RenderPass = @import("RenderPass.zig"); const Bindings = @import("Bindings.zig"); const Entities = @import("../Entities.zig"); const Texture = @import("../textures.zig").Texture; +const meshes = @import("../meshes.zig"); const vec = @import("../../math/vec.zig"); const mat = @import("../../math/mat.zig"); @@ -426,26 +427,28 @@ const OpaquePass = struct { defer cmdbuf.endRenderPass(device); const ents = Entities.get(); - for (ents.meshes.items) |mesh, i| { - const mAlbedo = Texture.get(ents.materials.items[i].albedo); - const mRome = Texture.get(ents.materials.items[i].rome); - const mNormal = Texture.get(ents.materials.items[i].normal); + for (ents.meshes.items) |id, i| { + if (meshes.get(id)) |mesh| { + const mAlbedo = Texture.get(ents.materials.items[i].albedo); + const mRome = Texture.get(ents.materials.items[i].rome); + const mNormal = Texture.get(ents.materials.items[i].normal); - const pc = PushConstants{ - .kLocalToWorld = ents.matrices.items[i], - .kIMc0 = ents.inv_matrices.items[i].c0, - .kIMc1 = ents.inv_matrices.items[i].c1, - .kIMc2 = ents.inv_matrices.items[i].c2, - .kTexInds = .{ - .x = if (mAlbedo) |albedo| albedo.slot.index else 0, - .y = if (mRome) |rome| rome.slot.index else 0, - .z = if (mNormal) |normal| normal.slot.index else 0, - .w = 0, - }, - }; + const pc = PushConstants{ + .kLocalToWorld = ents.matrices.items[i], + .kIMc0 = ents.inv_matrices.items[i].c0, + .kIMc1 = ents.inv_matrices.items[i].c1, + .kIMc2 = ents.inv_matrices.items[i].c2, + .kTexInds = .{ + .x = if (mAlbedo) |albedo| albedo.slot.index else 0, + .y = if (mRome) |rome| rome.slot.index else 0, + .z = if (mNormal) |normal| normal.slot.index else 0, + .w = 0, + }, + }; - cmdbuf.pushConstants(device, &s_pass, &pc); - cmdbuf.drawMesh(mesh.id); + cmdbuf.pushConstants(device, &s_pass, &pc); + cmdbuf.drawMesh(device, mesh.vk_id); + } } const pc = PushConstants{ diff --git a/src/rendering/vulkan/Renderer.zig b/src/rendering/vulkan/Renderer.zig index 3ec052d..3e6d0f3 100644 --- a/src/rendering/vulkan/Renderer.zig +++ b/src/rendering/vulkan/Renderer.zig @@ -13,6 +13,7 @@ const MainPass = @import("MainPass.zig"); const Command = @import("Command.zig"); const Targets = @import("Targets.zig"); const Bindings = @import("Bindings.zig"); +const meshes = @import("meshes.zig"); instance: Instance, window: Window, @@ -20,7 +21,6 @@ device: Device, swapchain: Swapchain, // sampler: Sampler, TODO // tex_table: TexTable, TODO -// mesh_sys: MeshSys, TODO // im_sys: ImSys, TODO const Self = @This(); @@ -56,7 +56,7 @@ pub fn init() !Self { // try self.texTable.init(); try Bindings.init(&device); try Targets.init(&device, &swapchain); - // try self.meshSys.init(); + try meshes.init(); // try self.imSys.init(); try MainPass.init(&device, &swapchain); @@ -113,7 +113,7 @@ pub fn deinit(self: Self) void { MainPass.deinit(&self.device); // self.imSys.deinit(); - // self.meshSys.deinit(); + meshes.deinit(); Targets.deinit(&self.device); Bindings.deinit(&self.device); // self.texTable.deinit(); diff --git a/src/rendering/vulkan/meshes.zig b/src/rendering/vulkan/meshes.zig new file mode 100644 index 0000000..0eef407 --- /dev/null +++ b/src/rendering/vulkan/meshes.zig @@ -0,0 +1,32 @@ +const std = @import("std"); +const UUID = @import("uuid").UUID; +const vk = @import("vulkan"); +const Table = @import("../../containers/table.zig").Table; + +const Buffer = @import("Buffer.zig"); + +// TODO memory +var gpa = std.heap.GeneralPurposeAllocator(.{}){}; +const allocator = gpa.allocator(); + +const MTable = Table(UUID, Mesh); +var s_meshes: MTable = undefined; + +pub fn init() !void { + s_meshes = MTable.init(allocator); +} + +pub fn deinit() void { + s_meshes.deinit(); +} + +pub fn get(id: MTable.Id) ?*Mesh { + return s_meshes.getPtr(id); +} + +pub const Mesh = struct { + pub const Id = MTable.Id; + + buffer: Buffer, + vert_count: u32, +};