From 03a089610b9b23ce5cfb8571c69763d7a2045695 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Mon, 22 Apr 2024 00:06:33 -0700 Subject: [PATCH] Core: store framebuffer information as components Signed-off-by: Stephen Gutekanst --- examples/core-custom-entrypoint/Game.zig | 2 +- examples/custom-renderer/Renderer.zig | 2 +- src/Core.zig | 22 +++++++++++++++++++++- src/gfx/SpritePipeline.zig | 2 +- src/gfx/TextPipeline.zig | 2 +- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/examples/core-custom-entrypoint/Game.zig b/examples/core-custom-entrypoint/Game.zig index 265da066..6160e7e2 100644 --- a/examples/core-custom-entrypoint/Game.zig +++ b/examples/core-custom-entrypoint/Game.zig @@ -23,7 +23,7 @@ fn init(game: *Mod, core: *mach.Core.Mod) !void { // Color target describes e.g. the pixel format of the window we are rendering to. const color_target = gpu.ColorTargetState{ - .format = mach.core.descriptor.format, + .format = core.get(core.state().main_window, .framebuffer_format).?, .blend = &blend, }; diff --git a/examples/custom-renderer/Renderer.zig b/examples/custom-renderer/Renderer.zig index 08abba09..eb1b2680 100644 --- a/examples/custom-renderer/Renderer.zig +++ b/examples/custom-renderer/Renderer.zig @@ -48,7 +48,7 @@ fn init( // Fragment state const blend = gpu.BlendState{}; const color_target = gpu.ColorTargetState{ - .format = mach.core.descriptor.format, + .format = core.get(core.state().main_window, .framebuffer_format).?, .blend = &blend, .write_mask = gpu.ColorWriteMaskFlags.all, }; diff --git a/src/Core.zig b/src/Core.zig index 94ce8a06..eb5fb7bb 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -1,5 +1,7 @@ const std = @import("std"); + const mach = @import("main.zig"); +const gpu = mach.gpu; // TODO(important): mach.core has a lot of standard Zig APIs, and some global variables, which are // part of its old API design. We should elevate them into this module instead. @@ -45,6 +47,18 @@ pub const components = .{ \\ If setting this component yourself, ensure the buffer is allocated using core.state().allocator \\ as it will be freed for you as part of the .deinit event. }, + + .framebuffer_format = .{ .type = gpu.Texture.Format, .description = + \\ The texture format of the framebuffer + }, + + .framebuffer_width = .{ .type = u32, .description = + \\ The width of the framebuffer in texels + }, + + .framebuffer_height = .{ .type = u32, .description = + \\ The width of the framebuffer in texels + }, }; /// Prints into the window title buffer using a format string and arguments. e.g. @@ -78,11 +92,17 @@ fn init(core: *Mod) !void { mach.core.allocator = gpa.allocator(); // TODO: banish this global allocator try mach.core.init(.{}); + // TODO(important): update this information upon framebuffer resize events + const main_window = try core.newEntity(); + try core.set(main_window, .framebuffer_format, mach.core.descriptor.format); + try core.set(main_window, .framebuffer_width, mach.core.descriptor.width); + try core.set(main_window, .framebuffer_height, mach.core.descriptor.height); + core.init(.{ .allocator = mach.core.allocator, .device = mach.core.device, .queue = mach.core.device.getQueue(), - .main_window = try core.newEntity(), + .main_window = main_window, }); core.sendGlobal(.init, .{}); diff --git a/src/gfx/SpritePipeline.zig b/src/gfx/SpritePipeline.zig index 9f6dec3b..7e6d4be2 100644 --- a/src/gfx/SpritePipeline.zig +++ b/src/gfx/SpritePipeline.zig @@ -281,7 +281,7 @@ fn buildPipeline( defer shader_module.release(); const color_target = opt_color_target_state orelse gpu.ColorTargetState{ - .format = mach.core.descriptor.format, + .format = core.get(core.state().main_window, .framebuffer_format).?, .blend = &blend_state, .write_mask = gpu.ColorWriteMaskFlags.all, }; diff --git a/src/gfx/TextPipeline.zig b/src/gfx/TextPipeline.zig index aeb3bb65..3d8543a0 100644 --- a/src/gfx/TextPipeline.zig +++ b/src/gfx/TextPipeline.zig @@ -307,7 +307,7 @@ fn buildPipeline( defer shader_module.release(); const color_target = opt_color_target_state orelse gpu.ColorTargetState{ - .format = mach.core.descriptor.format, + .format = core.get(core.state().main_window, .framebuffer_format).?, .blend = &blend_state, .write_mask = gpu.ColorWriteMaskFlags.all, };