GTL/src/gtl.zig
2026-07-13 16:41:14 +01:00

142 lines
3.9 KiB
Zig

//
// PRIVATE
//
const std = @import("std");
//
// PUBLIC
//
pub const log = @import("log.zig");
pub const ansi = @import("ansi.zig");
pub const Handle = @import("handle.zig").Handle;
pub const SlotMap = @import("slot_map.zig").SlotMap;
pub const KeyedSlotMap = @import("keyed_slot_map.zig").KeyedSlotMap;
pub const RingBuffer = @import("ring_buffer.zig").RingBuffer;
pub const TypeID = @import("type_id.zig").of;
//
// STRUCTS
//
// --- SHADER ---
const ShaderID = union(enum) { pbr, unlit, custom: Handle([]const u8) };
const GPUShader = struct { raw: *anyopaque };
// --- PIPELINE ---
const PipelineKey = struct { shader: ShaderID };
const GPUPipeline = struct { raw: *anyopaque };
// --- MATERIAL ---
const Material = struct {
shader: ShaderID = .pbr,
albedo: @Vector(4, f32) = .{ 0, 0, 0, 1 },
};
test "SlotMap" {
// --- ALLOCATOR ---
const alloc = std.testing.allocator;
//
// LISTS
//
// --- MATERIALS ---
var materials: std.ArrayList(Material) = .empty;
defer materials.deinit(alloc);
//
// SLOT MAPS
//
// --- SHADER MAP ---
var shaders: KeyedSlotMap(ShaderID, GPUShader) = .init(alloc);
defer shaders.deinit();
// --- PIPELINE MAP ---
var pipelines: KeyedSlotMap(PipelineKey, GPUPipeline) = .init(alloc);
defer pipelines.deinit();
// --- ADD MATERIALS ---
try materials.append(alloc, .{ .shader = .pbr }); // BLACK
try materials.append(alloc, .{ .shader = .pbr, .albedo = .{ 1, 0, 0, 1 } }); // RED
try materials.append(alloc, .{ .shader = .unlit, .albedo = .{ 0, 1, 0, 1 } }); // GREEN
try materials.append(alloc, .{ .shader = .unlit, .albedo = .{ 0, 0, 1, 1 } }); // GREEN
{
// --- DEBUG ---
std.debug.print("┏━ MATERIALS\n", .{});
defer std.debug.print("┗━\n", .{});
for (materials.items) |material| {
// --- REGISTER SHADER ---
const shader = shaders.getByKey(material.shader) orelse blk: {
const handle = try shaders.put(material.shader, .{ .raw = undefined });
break :blk shaders.get(handle).?;
};
_ = shader;
// --- DEBUG ---
std.debug.print("┃ ┏━\n", .{});
std.debug.print("┃ ┃ Shader: {s}\n", .{@tagName(material.shader)});
std.debug.print("┃ ┃ Albedo: rgba({}, {}, {}, {})\n", .{ material.albedo[0], material.albedo[1], material.albedo[2], material.albedo[3] });
std.debug.print("┃ ┗━\n", .{});
}
}
{
// --- DEBUG ---
std.debug.print("┏━ SHADERS\n", .{});
defer std.debug.print("┗━\n", .{});
for (shaders.items()) |entry| {
const source = switch (entry.key) {
.pbr => "assets/shaders/pbr.wgsl",
.unlit => "assets/shaders/unlit.wgsl",
.custom => "idk",
};
std.debug.print("┃ ┏━\n", .{});
std.debug.print("┃ ┃ ID: {s}\n", .{@tagName(entry.key)});
std.debug.print("┃ ┃ Source: {s}\n", .{source});
std.debug.print("┃ ┗━\n", .{});
}
}
{
std.debug.print("┏━ ANSI\n", .{});
defer std.debug.print("┗━\n", .{});
std.debug.print(
\\{s}┃ RED
\\{s}┃ GREEN
\\{s}┃ BLUE
\\{s}
, .{
ansi.red,
ansi.green,
ansi.blue,
ansi.reset,
});
}
{
log.warn("...", .{});
log.err("...", .{});
log.info("...", .{});
log.debug("...", .{});
log.trace("...", .{});
}
var ring: RingBuffer([]const u8, 3) = .{};
try ring.push("Hello");
try ring.push("World");
try ring.push("!");
var i: usize = 0;
while (ring.pop()) |data| {
i += 1;
std.debug.print("ring[{}] = \"{s}\"\n", .{ i, data });
}
}