initialize mesh system, vulkan abstract renderer a bit, actually draw meshes (but none added)

This commit is contained in:
Vivianne 2022-07-27 00:19:25 -07:00
parent 32a5db1371
commit d67de4fbe8
7 changed files with 141 additions and 36 deletions

View file

@ -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();
}
}

View file

@ -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();
}

View file

@ -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 {

View file

@ -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);

View file

@ -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{

View file

@ -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();

View file

@ -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,
};