Improved Examples

This commit is contained in:
abux 2026-08-11 21:26:15 +01:00
parent b95d814ce7
commit 3f8b5617e0
5 changed files with 196 additions and 151 deletions

View file

@ -10,3 +10,6 @@ Low cortisol crossplatform RHI (Rendering Hardware Interface)
- Fire and forget
- Dead simple + Performance + Flexible
- Handles
# Examples
- ```zig build run-simple```

View file

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

132
examples/simple/simple.zig Normal file
View file

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

1
examples/vendor/sdl3.h vendored Normal file
View file

@ -0,0 +1 @@
#include <SDL3/SDL.h>

View file

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