moved rendering to a system.

To ensure that the rendering system is being run in the main thread, I
added the concept of "Sync systems", so that when a system group is
created it's possible to specify whether it's possible to run it on a
separate thread or not.
This commit is contained in:
Lorenzo Torres 2025-03-29 16:15:56 +01:00
parent fd7973173f
commit 16a2a40418
9 changed files with 85 additions and 64 deletions

View file

@ -2,12 +2,10 @@
layout(location = 0) in vec3 vertPos; layout(location = 0) in vec3 vertPos;
layout(binding = 0) uniform UniformObject { layout (binding = 0) uniform Uniform {
mat4 proj; mat4 proj;
mat4 view;
mat4 model;
} ubo; } ubo;
void main() { void main() {
gl_Position = ubo.view * ubo.proj * vec4(vertPos, 1.0); gl_Position = ubo.proj * vec4(vertPos, 1.0);
} }

View file

@ -118,6 +118,7 @@ pub fn build(b: *std.Build) void {
.root_module = mods, .root_module = mods,
.name = "mods", .name = "mods",
}); });
const mods_docs = b.addInstallDirectory(.{ const mods_docs = b.addInstallDirectory(.{
.source_dir = mods_lib.getEmittedDocs(), .source_dir = mods_lib.getEmittedDocs(),
.install_dir = .prefix, .install_dir = .prefix,

View file

@ -1,7 +1,12 @@
pub const components = @import("components.zig"); pub const components = @import("components.zig");
pub const entities = @import("entities.zig"); pub const entities = @import("entities.zig");
pub const SystemError = error{
fail,
die,
};
pub const Pool = entities.Pool; pub const Pool = entities.Pool;
pub const Resources = entities.Resources; pub const Resources = entities.Resources;
pub const System = *const fn (*Pool) void; pub const System = *const fn (*Pool) anyerror!void;
pub const SystemGroup = []const System; pub const SystemGroup = []const System;

View file

@ -4,14 +4,17 @@ const components = @import("components.zig");
const sparse = @import("sparse.zig"); const sparse = @import("sparse.zig");
const Renderer = @import("renderer"); const Renderer = @import("renderer");
const Input = @import("sideros").Input; const Input = @import("sideros").Input;
const ecs = @import("ecs.zig");
pub const System = *const fn (*Pool) void; pub const System = ecs.System;
pub const SystemGroup = []const System; pub const SystemGroup = []const System;
pub const SyncGroup = []const System;
pub const Resources = struct { pub const Resources = struct {
window: Renderer.Window, window: Renderer.Window,
renderer: Renderer, renderer: Renderer,
input: Input, input: Input,
delta_time: f64 = 0.0,
}; };
pub const Human = struct { pub const Human = struct {
@ -24,6 +27,7 @@ pub const Pool = struct {
resources: Resources, resources: Resources,
allocator: Allocator, allocator: Allocator,
system_groups: std.ArrayList(SystemGroup), system_groups: std.ArrayList(SystemGroup),
sync_groups: std.ArrayList(SyncGroup),
thread_pool: *std.Thread.Pool, thread_pool: *std.Thread.Pool,
wait_group: std.Thread.WaitGroup, wait_group: std.Thread.WaitGroup,
mutex: std.Thread.Mutex, mutex: std.Thread.Mutex,
@ -33,6 +37,7 @@ pub const Pool = struct {
.humans = .{}, .humans = .{},
.resources = resources, .resources = resources,
.system_groups = std.ArrayList(SystemGroup).init(allocator), .system_groups = std.ArrayList(SystemGroup).init(allocator),
.sync_groups = std.ArrayList(SystemGroup).init(allocator),
.thread_pool = try allocator.create(std.Thread.Pool), .thread_pool = try allocator.create(std.Thread.Pool),
.wait_group = .{}, .wait_group = .{},
.mutex = .{}, .mutex = .{},
@ -47,8 +52,12 @@ pub const Pool = struct {
return pool; return pool;
} }
pub fn addSystemGroup(self: *@This(), group: SystemGroup) !void { pub fn addSystemGroup(self: *@This(), group: SystemGroup, sync: bool) !void {
try self.system_groups.append(group); if (sync) {
try self.sync_groups.append(group);
} else {
try self.system_groups.append(group);
}
} }
pub fn deinit(self: *@This()) void { pub fn deinit(self: *@This()) void {
@ -65,11 +74,18 @@ pub const Pool = struct {
fn run(pool: *Pool, index: usize) void { fn run(pool: *Pool, index: usize) void {
const group = pool.system_groups.items[index]; const group = pool.system_groups.items[index];
for (group) |system| { for (group) |system| {
system(pool); // TODO: system errors should be correctly handled
system(pool) catch unreachable;
} }
} }
}.run, .{ self, i }); }.run, .{ self, i });
} }
for (0..self.sync_groups.items.len) |i| {
const group = self.sync_groups.items[i];
for (group) |system| {
system(self) catch unreachable;
}
}
} }
fn getEntities(self: *@This(), T: type) *std.MultiArrayList(T) { fn getEntities(self: *@This(), T: type) *std.MultiArrayList(T) {

View file

@ -22,13 +22,13 @@ pub fn main() !void {
const file = try std.fs.cwd().openFile("assets/core.wasm", .{}); const file = try std.fs.cwd().openFile("assets/core.wasm", .{});
const all = try file.readToEndAlloc(allocator, 1_000_000); // 1 MB const all = try file.readToEndAlloc(allocator, 1_000_000); // 1 MB
var parser = mods.Parser{ var parser = mods.Parser{
.bytes = all, .bytes = all,
.byte_idx = 0, .byte_idx = 0,
.allocator = allocator, .allocator = allocator,
}; };
const module = parser.parseModule() catch |err| { const module = parser.parseModule() catch |err| {
std.debug.print("[ERROR]: error at byte {x}(0x{x})\n", .{ parser.byte_idx, parser.bytes[parser.byte_idx] }); std.debug.print("[ERROR]: error at byte {x}(0x{x})\n", .{ parser.byte_idx, parser.bytes[parser.byte_idx] });
return err; return err;
}; };
var runtime = try mods.Runtime.init(allocator, module, &global_runtime); var runtime = try mods.Runtime.init(allocator, module, &global_runtime);
defer runtime.deinit(allocator); defer runtime.deinit(allocator);
@ -40,8 +40,8 @@ pub fn main() !void {
var w = try Renderer.Window.create(800, 600, "sideros"); var w = try Renderer.Window.create(800, 600, "sideros");
defer w.destroy(); defer w.destroy();
var r = try Renderer.create(allocator, w); var r = try Renderer.init(allocator, w);
defer r.destroy(); defer r.deinit();
const resources = ecs.Resources{ const resources = ecs.Resources{
.window = w, .window = w,
@ -52,9 +52,9 @@ pub fn main() !void {
var pool = try ecs.Pool.init(allocator, resources); var pool = try ecs.Pool.init(allocator, resources);
defer pool.deinit(); defer pool.deinit();
w.setResources(&pool.resources); w.setResources(&pool.resources);
//try pool.addSystemGroup(&[_]entities.System{ try pool.addSystemGroup(&[_]ecs.System{
// testSystem, Renderer.render,
//}); }, true);
// try pool.addSystemGroup(&[_]ecs.System{ // try pool.addSystemGroup(&[_]ecs.System{
// testSystem2, // testSystem2,
// }); // });
@ -64,10 +64,12 @@ pub fn main() !void {
// try pool.addComponent(entity, ecs.components.Position{ .x = 1.0, .y = 0.5, .z = 3.0 }); // try pool.addComponent(entity, ecs.components.Position{ .x = 1.0, .y = 0.5, .z = 3.0 });
// try pool.addComponent(entity, ecs.components.Speed{ .speed = 5.0 }); // try pool.addComponent(entity, ecs.components.Speed{ .speed = 5.0 });
// } // }
var last_time: f64 = 0.0;
while (!w.shouldClose()) { while (!w.shouldClose()) {
const current_time = Renderer.Window.getTime();
pool.resources.delta_time = current_time - last_time;
last_time = current_time;
Renderer.Window.pollEvents(); Renderer.Window.pollEvents();
try r.tick();
pool.tick(); pool.tick();
} }
} }

View file

@ -1,6 +1,8 @@
const std = @import("std"); const std = @import("std");
const ecs = @import("ecs"); const ecs = @import("ecs");
const math = @import("../math.zig"); const sideros = @import("sideros");
const math = sideros.math;
const Camera = @This(); const Camera = @This();
const UP = @Vector(3, f32){ 0.0, 1.0, 0.0 }; const UP = @Vector(3, f32){ 0.0, 1.0, 0.0 };
@ -19,15 +21,15 @@ front: @Vector(3, f32),
up: @Vector(3, f32), up: @Vector(3, f32),
speed: f32 = 2.5, speed: f32 = 2.5,
fn getProjection(width: usize, height: usize) math.Matrix { pub fn getProjection(width: usize, height: usize) math.Matrix {
return math.Matrix.perspective(math.rad(45.0), (@as(f32, @floatFromInt(width)) / @as(f32, @floatFromInt(height))), 0.1, 10.0); return math.Matrix.perspective(math.rad(45.0), (@as(f32, @floatFromInt(width)) / @as(f32, @floatFromInt(height))), 0.1, 10.0);
} }
fn getView(self: Camera) math.Matrix { pub fn getView(self: Camera) math.Matrix {
math.lookAt(self.position, self.position + self.front, self.up); math.lookAt(self.position, self.position + self.front, self.up);
} }
fn moveCamera(pool: *ecs.Pool) void { pub fn moveCamera(pool: *ecs.Pool) void {
const input = pool.resources.input; const input = pool.resources.input;
const camera = pool.resources.camera; const camera = pool.resources.camera;
if (input.isKeyDown(.w)) { if (input.isKeyDown(.w)) {

View file

@ -1,4 +1,5 @@
const c = @import("c.zig"); const c = @import("c.zig");
const ecs = @import("ecs");
const std = @import("std"); const std = @import("std");
const vk = @import("vulkan.zig"); const vk = @import("vulkan.zig");
pub const Window = @import("Window.zig"); pub const Window = @import("Window.zig");
@ -18,7 +19,7 @@ current_frame: u32,
vertex_buffer: vk.Buffer, vertex_buffer: vk.Buffer,
index_buffer: vk.Buffer, index_buffer: vk.Buffer,
pub fn create(allocator: Allocator, w: Window) !Renderer { pub fn init(allocator: Allocator, w: Window) !Renderer {
const instance = try vk.Instance.create(allocator); const instance = try vk.Instance.create(allocator);
const surface = try vk.Surface.create(instance, w); const surface = try vk.Surface.create(instance, w);
@ -61,7 +62,7 @@ pub fn create(allocator: Allocator, w: Window) !Renderer {
}; };
} }
pub fn destroy(self: Renderer) void { pub fn deinit(self: Renderer) void {
self.device.waitIdle(); self.device.waitIdle();
self.index_buffer.destroy(self.device.handle); self.index_buffer.destroy(self.device.handle);
self.vertex_buffer.destroy(self.device.handle); self.vertex_buffer.destroy(self.device.handle);
@ -73,22 +74,24 @@ pub fn destroy(self: Renderer) void {
self.instance.destroy(); self.instance.destroy();
} }
// TODO: tick is maybe a bad name? something like present() or submit() is better? // TODO: render is maybe a bad name? something like present() or submit() is better?
pub fn tick(self: *Renderer) !void { pub fn render(pool: *ecs.Pool) anyerror!void {
try self.device.waitFence(self.current_frame); var renderer = pool.resources.renderer;
const image = try self.swapchain.nextImage(self.device, self.current_frame);
try self.device.resetCommand(self.current_frame);
try self.device.beginCommand(self.current_frame);
self.render_pass.begin(self.swapchain, self.device, image, self.current_frame);
self.graphics_pipeline.bind(self.device, self.current_frame);
self.device.bindVertexBuffer(self.vertex_buffer, self.current_frame);
self.device.bindIndexBuffer(self.index_buffer, self.current_frame);
self.device.bindDescriptorSets(self.graphics_pipeline, self.current_frame);
self.device.draw(@intCast(self.index_buffer.size / @sizeOf(u16)), self.current_frame);
self.render_pass.end(self.device, self.current_frame);
try self.device.endCommand(self.current_frame);
try self.device.submit(self.swapchain, image, self.current_frame); try renderer.device.waitFence(renderer.current_frame);
const image = try renderer.swapchain.nextImage(renderer.device, renderer.current_frame);
try renderer.device.resetCommand(renderer.current_frame);
try renderer.device.beginCommand(renderer.current_frame);
renderer.render_pass.begin(renderer.swapchain, renderer.device, image, renderer.current_frame);
renderer.graphics_pipeline.bind(renderer.device, renderer.current_frame);
renderer.device.bindVertexBuffer(renderer.vertex_buffer, renderer.current_frame);
renderer.device.bindIndexBuffer(renderer.index_buffer, renderer.current_frame);
renderer.device.bindDescriptorSets(renderer.graphics_pipeline, renderer.current_frame);
renderer.device.draw(@intCast(renderer.index_buffer.size / @sizeOf(u16)), renderer.current_frame);
renderer.render_pass.end(renderer.device, renderer.current_frame);
try renderer.device.endCommand(renderer.current_frame);
self.current_frame = (self.current_frame + 1) % 2; try renderer.device.submit(renderer.swapchain, image, renderer.current_frame);
renderer.current_frame = (renderer.current_frame + 1) % 2;
} }

View file

@ -74,7 +74,7 @@ pub fn destroy(self: Window) void {
c.glfwTerminate(); c.glfwTerminate();
} }
pub fn getTime() f32 { pub fn getTime() f64 {
return c.glfwGetTime(); return c.glfwGetTime();
} }

View file

@ -3,6 +3,7 @@ const c = @import("c.zig");
const Window = @import("Window.zig"); const Window = @import("Window.zig");
const Mesh = @import("Mesh.zig"); const Mesh = @import("Mesh.zig");
const sideros = @import("sideros"); const sideros = @import("sideros");
const Camera = @import("Camera.zig");
const math = sideros.math; const math = sideros.math;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
@ -302,7 +303,7 @@ pub fn GraphicsPipeline(comptime n: usize) type {
descriptor_pool: c.VkDescriptorPool, descriptor_pool: c.VkDescriptorPool,
descriptor_set: c.VkDescriptorSet, descriptor_set: c.VkDescriptorSet,
descriptor_set_layout: c.VkDescriptorSetLayout, descriptor_set_layout: c.VkDescriptorSetLayout,
uniform_buffer: Buffer, projection_buffer: Buffer,
const Self = @This(); const Self = @This();
@ -486,36 +487,25 @@ pub fn GraphicsPipeline(comptime n: usize) type {
try mapError(c.vkAllocateDescriptorSets(device.handle, &descriptor_allocate_info, &descriptor_set)); try mapError(c.vkAllocateDescriptorSets(device.handle, &descriptor_allocate_info, &descriptor_set));
const proj = math.Matrix.perspective(math.rad(45.0), (@as(f32, @floatFromInt(swapchain.extent.width)) / @as(f32, @floatFromInt(swapchain.extent.height))), 0.1, 10.0); const projection_buffer = try device.createBuffer(BufferUsage{ .uniform_buffer = true, .transfer_dst = true }, BufferFlags{ .device_local = true }, @sizeOf(math.Matrix));
const view = math.Matrix.lookAt(@Vector(3, f32){ 0.0, 0.0, 10.0 }, @Vector(3, f32){ 0.0, 0.0, 0.0 }, @Vector(3, f32){ 0.0, 1.0, 0.0 });
const model = math.Matrix.identity();
const uniform = Uniform{
.proj = proj,
.view = view,
.model = model,
};
const uniform_buffer = try device.createBuffer(BufferUsage{ .uniform_buffer = true, .transfer_dst = true }, BufferFlags{ .device_local = true }, @sizeOf(Uniform));
var data: [*c]u8 = undefined; var data: [*c]u8 = undefined;
try mapError(c.vkMapMemory( try mapError(c.vkMapMemory(
device.handle, device.handle,
uniform_buffer.memory, projection_buffer.memory,
0, 0,
uniform_buffer.size, projection_buffer.size,
0, 0,
@ptrCast(&data), @ptrCast(&data),
)); ));
@memcpy(data[0..(@sizeOf(math.Matrix) * 3)], std.mem.asBytes(&uniform)); @memcpy(data[0..@sizeOf(math.Matrix)], std.mem.asBytes(&Camera.getProjection(swapchain.extent.width, swapchain.extent.height)));
const descriptor_buffer_info = c.VkDescriptorBufferInfo{ const descriptor_buffer_info = c.VkDescriptorBufferInfo{
.buffer = uniform_buffer.handle, .buffer = projection_buffer.handle,
.offset = 0, .offset = 0,
.range = uniform_buffer.size, .range = projection_buffer.size,
}; };
const write_descriptor_set = c.VkWriteDescriptorSet{ const write_descriptor_set = c.VkWriteDescriptorSet{
@ -536,7 +526,7 @@ pub fn GraphicsPipeline(comptime n: usize) type {
.descriptor_pool = descriptor_pool, .descriptor_pool = descriptor_pool,
.descriptor_set = descriptor_set, .descriptor_set = descriptor_set,
.descriptor_set_layout = descriptor_set_layout, .descriptor_set_layout = descriptor_set_layout,
.uniform_buffer = uniform_buffer, .projection_buffer = projection_buffer,
}; };
} }
@ -546,7 +536,7 @@ pub fn GraphicsPipeline(comptime n: usize) type {
} }
pub fn destroy(self: Self, device: Device(n)) void { pub fn destroy(self: Self, device: Device(n)) void {
self.uniform_buffer.destroy(device.handle); self.projection_buffer.destroy(device.handle);
c.vkDestroyDescriptorSetLayout(device.handle, self.descriptor_set_layout, null); c.vkDestroyDescriptorSetLayout(device.handle, self.descriptor_set_layout, null);
c.vkDestroyDescriptorPool(device.handle, self.descriptor_pool, null); c.vkDestroyDescriptorPool(device.handle, self.descriptor_pool, null);
c.vkDestroyPipeline(device.handle, self.handle, null); c.vkDestroyPipeline(device.handle, self.handle, null);
@ -871,6 +861,10 @@ pub fn Device(comptime n: usize) type {
c.vkCmdBindDescriptorSets(self.command_buffers[frame], c.VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.layout, 0, 1, &pipeline.descriptor_set, 0, null); c.vkCmdBindDescriptorSets(self.command_buffers[frame], c.VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.layout, 0, 1, &pipeline.descriptor_set, 0, null);
} }
pub fn updateBuffer(self: Self, comptime T: type, buffer: Buffer, data: [*]T, frame: usize) void {
c.vkCmdUpdateBuffer(self.command_buffers[frame], buffer.handle, 0, @sizeOf(T), @ptrCast(@alignCast(data)));
}
pub fn pick_memory_type(self: Self, type_bits: u32, flags: u32) u32 { pub fn pick_memory_type(self: Self, type_bits: u32, flags: u32) u32 {
var memory_type_index: u32 = 0; var memory_type_index: u32 = 0;
for (0..self.memory_properties.memoryTypeCount) |index| { for (0..self.memory_properties.memoryTypeCount) |index| {