From 3f8b5617e07a00f201cad78306503062150f1baf Mon Sep 17 00:00:00 2001 From: abux Date: Tue, 11 Aug 2026 21:26:15 +0100 Subject: [PATCH] Improved Examples --- README.md | 3 + build.zig | 69 +++++++++++++++--- examples/simple/simple.zig | 132 ++++++++++++++++++++++++++++++++++ examples/vendor/sdl3.h | 1 + src/gfx.zig | 142 ------------------------------------- 5 files changed, 196 insertions(+), 151 deletions(-) create mode 100644 examples/simple/simple.zig create mode 100644 examples/vendor/sdl3.h diff --git a/README.md b/README.md index 71dbdc4..2ac2875 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,6 @@ Low cortisol crossplatform RHI (Rendering Hardware Interface) - Fire and forget - Dead simple + Performance + Flexible - Handles + +# Examples +- ```zig build run-simple``` diff --git a/build.zig b/build.zig index 4c08291..d37ee0c 100644 --- a/build.zig +++ b/build.zig @@ -1,6 +1,6 @@ const std = @import("std"); -pub fn build(b: *std.Build) void { +pub fn build(b: *std.Build) !void { // --- TARGET --- const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); @@ -42,14 +42,6 @@ pub fn build(b: *std.Build) void { // SYSTEM DEPENDENCIES // - const sdl = b.addTranslateC(.{ - .target = target, - .optimize = optimize, - .root_source_file = b.path("./src/vendor/sdl3/sdl.h"), - }); - mod.addImport("sdl", sdl.createModule()); - mod.linkSystemLibrary("SDL3", .{}); - const vulkan = b.addTranslateC(.{ .target = target, .optimize = optimize, @@ -57,4 +49,63 @@ pub fn build(b: *std.Build) void { }); mod.addImport("vulkan", vulkan.createModule()); mod.linkSystemLibrary("vulkan", .{}); + + // + // EXAMPLES + // + + inline for ([_]struct { + name: []const u8, + src: []const u8, + }{ + .{ .name = "simple", .src = "examples/simple/simple.zig" }, + }) |excfg| { + const ex_name = excfg.name; + const ex_src = excfg.src; + + const ex_build_desc = try std.fmt.allocPrint( + b.allocator, + "build the {s} example", + .{ex_name}, + ); + const ex_run_stepname = try std.fmt.allocPrint( + b.allocator, + "run-{s}", + .{ex_name}, + ); + const ex_run_stepdesc = try std.fmt.allocPrint( + b.allocator, + "run the {s} example", + .{ex_name}, + ); + const example_run_step = b.step(ex_run_stepname, ex_run_stepdesc); + const example_step = b.step(ex_name, ex_build_desc); + + const exe_mod = b.addModule(ex_name, .{ + .root_source_file = b.path(ex_src), + .target = target, + .optimize = optimize, + }); + + var example = b.addExecutable(.{ + .name = ex_name, + .root_module = exe_mod, + }); + + example.root_module.addImport("gfx", mod); + + const sdl = b.addTranslateC(.{ + .target = target, + .optimize = optimize, + .root_source_file = b.path("examples/vendor/sdl3.h"), + }); + example.root_module.addImport("sdl", sdl.createModule()); + example.root_module.linkSystemLibrary("SDL3", .{}); + + const example_run = b.addRunArtifact(example); + example_run_step.dependOn(&example_run.step); + + const example_build_step = b.addInstallArtifact(example, .{}); + example_step.dependOn(&example_build_step.step); + } } diff --git a/examples/simple/simple.zig b/examples/simple/simple.zig new file mode 100644 index 0000000..946a799 --- /dev/null +++ b/examples/simple/simple.zig @@ -0,0 +1,132 @@ +//! ---------------------------------------------------- +//! ---------------------------------------------------- + +const std = @import("std"); +const sdl = @import("sdl"); +const Gfx = @import("gfx"); + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +const Vertex = struct { + pos: [2]f32, + normal: [2]f32, + uv: [2]f32, +}; + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +const vertices = [_]Vertex{ + .{ .pos = .{ -0.5, -0.5 }, .normal = .{ 0, 0 }, .uv = .{ 0, 0 } }, + .{ .pos = .{ 0.5, -0.5 }, .normal = .{ 0, 0 }, .uv = .{ 1, 0 } }, + .{ .pos = .{ 0.5, 0.5 }, .normal = .{ 0, 0 }, .uv = .{ 1, 1 } }, + .{ .pos = .{ -0.5, 0.5 }, .normal = .{ 0, 0 }, .uv = .{ 0, 1 } }, +}; + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +const indices = [_]u32{ + 0, 1, 2, + 0, 2, 3, +}; + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn main(init: std.process.Init) !void { + + // + // SDL + // + + // --- # --- + if (!sdl.SDL_Init(sdl.SDL_INIT_VIDEO)) return error.FailedToInitSDL; + defer sdl.SDL_Quit(); + + // --- # --- + const window = sdl.SDL_CreateWindow("NYXGFX", 800, 600, 0) orelse return error.FailedToCreateWindow; + defer sdl.SDL_DestroyWindow(window); + + // --- # --- + const props = sdl.SDL_GetWindowProperties(window); + const display = sdl.SDL_GetPointerProperty(props, sdl.SDL_PROP_WINDOW_X11_DISPLAY_POINTER, null); + const xwindow = sdl.SDL_GetNumberProperty(props, sdl.SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0); + + // + // GFX + // + + // --- # --- + var gfx: Gfx = try .init(.{ + .window = .{ + .xlib = .{ + .window = @intCast(xwindow), + .display = display.?, + }, + }, + }, init.gpa, init.io); + defer gfx.deinit(); + + // --- # --- + const adapter = try gfx.createAdapter(); + const surface = try gfx.createSurface(); + const device = try gfx.createDevice(adapter, surface); + const swapchain = try gfx.createSwapchain(adapter, device, surface); + + // --- BUFFERS --- + const vbuf = try gfx.createBuffer(vertices, .vertex, adapter, device); + const ibuf = try gfx.createBuffer(indices, .index, adapter, device); + + // --- SHADERS --- + const simple_shader = try gfx.createShader( + "assets/shaders/compiled/simple.vert.spv", + "assets/shaders/compiled/simple.frag.spv", + device, + ); + + const pipeline = try gfx.createPipeline(.{ + .vertex_attributes = &.{ + .{ .location = 0, .format = .vec2, .offset = @offsetOf(Vertex, "pos") }, + .{ .location = 1, .format = .vec2, .offset = @offsetOf(Vertex, "normal") }, + .{ .location = 2, .format = .vec2, .offset = @offsetOf(Vertex, "uv") }, + }, + .vertex_binding = .{ .stride = @sizeOf(Vertex) }, + + .shader = simple_shader, + .device = device, + .swapchain = swapchain, + }); + + try pipeline.setUniform("uTime", 0.1); + try pipeline.setUniform("uTime", 0.1); + + var event: sdl.SDL_Event = undefined; + var running: bool = true; + + while (running) { + while (sdl.SDL_PollEvent(&event)) { + switch (event.type) { + sdl.SDL_EVENT_QUIT => running = false, + else => {}, + } + } + + try gfx.beginFrame(swapchain, device); + defer gfx.endFrame(swapchain, device) catch unreachable; + + { + try gfx.beginRendering(swapchain); + defer gfx.endRendering(swapchain) catch unreachable; + + try pipeline.bind(); + try vbuf.bind(); + try ibuf.bind(); + try gfx.drawIndexed(indices.len, 1, 0, 0, 0); + } + + // if (gfx.beginRendering(...)) |pass| { + // defer pass.end(); + // + // try pass.bindPipeline(...); + // try pass.draw(...); + // } + } +} diff --git a/examples/vendor/sdl3.h b/examples/vendor/sdl3.h new file mode 100644 index 0000000..ba66a34 --- /dev/null +++ b/examples/vendor/sdl3.h @@ -0,0 +1 @@ +#include diff --git a/src/gfx.zig b/src/gfx.zig index 73867f7..ac4ea09 100644 --- a/src/gfx.zig +++ b/src/gfx.zig @@ -251,145 +251,3 @@ pub fn drawIndexed(self: *Self, index_count: u32, instance_count: u32, first_ind }, } } - -// -// TESTS -// - -const sdl = @import("sdl"); -const math = @import("math"); - -const Vertex = struct { - pos: [2]f32, - normal: [2]f32, - uv: [2]f32, -}; - -// const vertices = [_]Vertex{ -// .{ .pos = .{ -0.5, -0.5 }, .normal = .{ 0, 0 }, .uv = .{ 0, 0 } }, -// .{ .pos = .{ 0.5, -0.5 }, .normal = .{ 0, 0 }, .uv = .{ 1, 0 } }, -// .{ .pos = .{ 0.5, 0.5 }, .normal = .{ 0, 0 }, .uv = .{ 1, 1 } }, -// -// .{ .pos = .{ -0.5, -0.5 }, .normal = .{ 0, 0 }, .uv = .{ 0, 0 } }, -// .{ .pos = .{ 0.5, 0.5 }, .normal = .{ 0, 0 }, .uv = .{ 1, 1 } }, -// .{ .pos = .{ -0.5, 0.5 }, .normal = .{ 0, 0 }, .uv = .{ 0, 1 } }, -// }; - -const vertices = [_]Vertex{ - .{ .pos = .{ -0.5, -0.5 }, .normal = .{ 0, 0 }, .uv = .{ 0, 0 } }, - .{ .pos = .{ 0.5, -0.5 }, .normal = .{ 0, 0 }, .uv = .{ 1, 0 } }, - .{ .pos = .{ 0.5, 0.5 }, .normal = .{ 0, 0 }, .uv = .{ 1, 1 } }, - .{ .pos = .{ -0.5, 0.5 }, .normal = .{ 0, 0 }, .uv = .{ 0, 1 } }, -}; - -const indices = [_]u32{ - 0, 1, 2, - 0, 2, 3, -}; - -test "Main" { - - // - // SDL - // - - // --- # --- - if (!sdl.SDL_Init(sdl.SDL_INIT_VIDEO)) return error.FailedToInitSDL; - defer sdl.SDL_Quit(); - - // --- # --- - const window = sdl.SDL_CreateWindow("NYXGFX", 800, 600, 0) orelse return error.FailedToCreateWindow; - defer sdl.SDL_DestroyWindow(window); - - // --- # --- - const props = sdl.SDL_GetWindowProperties(window); - const display = sdl.SDL_GetPointerProperty(props, sdl.SDL_PROP_WINDOW_X11_DISPLAY_POINTER, null); - const xwindow = sdl.SDL_GetNumberProperty(props, sdl.SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0); - - // - // GFX - // - - // --- # --- - var gfx: Self = try .init(Config{ - .window = .{ - .xlib = .{ - .window = @intCast(xwindow), - .display = display.?, - }, - }, - }, std.testing.allocator, std.testing.io); - defer gfx.deinit(); - - // --- # --- - const adapter = try gfx.createAdapter(); - const surface = try gfx.createSurface(); - const device = try gfx.createDevice(adapter, surface); - const swapchain = try gfx.createSwapchain(adapter, device, surface); - - const vbuf = try gfx.createBuffer(vertices, .vertex, adapter, device); - const ibuf = try gfx.createBuffer(indices, .index, adapter, device); - - const simple_shader = try gfx.createShader( - "assets/shaders/compiled/simple.vert.spv", - "assets/shaders/compiled/simple.frag.spv", - device, - ); - - const pipeline = try gfx.createPipeline(.{ - .vertex_attributes = &.{ - .{ .location = 0, .format = .vec2, .offset = @offsetOf(Vertex, "pos") }, - .{ .location = 1, .format = .vec2, .offset = @offsetOf(Vertex, "normal") }, - .{ .location = 2, .format = .vec2, .offset = @offsetOf(Vertex, "uv") }, - }, - .vertex_binding = .{ .stride = @sizeOf(Vertex) }, - - .shader = simple_shader, - .device = device, - .swapchain = swapchain, - }); - - try pipeline.setUniform("uTime", 0.1); - try pipeline.setUniform("uTime", 0.1); - - // --- # --- - if (gtl.log.print(.debug, "WINDOW", gtl.ansi.blue)) |v| { - defer v.end(); - - v.write("Title: {s}", .{"NYXGFX"}); - v.write("Width: {}", .{800}); - v.write("Height: {}", .{600}); - } - - var event: sdl.SDL_Event = undefined; - var running: bool = true; - - while (running) { - while (sdl.SDL_PollEvent(&event)) { - switch (event.type) { - sdl.SDL_EVENT_QUIT => running = false, - else => {}, - } - } - - try gfx.beginFrame(swapchain, device); - defer gfx.endFrame(swapchain, device) catch unreachable; - - { - try gfx.beginRendering(swapchain); - defer gfx.endRendering(swapchain) catch unreachable; - - try pipeline.bind(); - try vbuf.bind(); - try ibuf.bind(); - try gfx.drawIndexed(indices.len, 1, 0, 0, 0); - } - - // if (gfx.beginRendering(...)) |pass| { - // defer pass.end(); - // - // try pass.bindPipeline(...); - // try pass.draw(...); - // } - } -}