GTL/src/gtl.zig

219 lines
6.3 KiB
Zig
Raw Normal View History

2026-07-12 19:46:40 +01:00
//
// 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 TypeID = @import("type_id.zig").of;
// --- ARRAYS ---
2026-07-12 19:46:40 +01:00
pub const SlotMap = @import("slot_map.zig").SlotMap;
2026-07-12 21:22:53 +01:00
pub const KeyedSlotMap = @import("keyed_slot_map.zig").KeyedSlotMap;
pub const FixedArrayList = @import("fixed_array_list.zig").FixedArrayList;
pub const FixedSlotMap = @import("fixed_slot_map.zig").FixedSlotMap;
2026-07-13 16:41:14 +01:00
pub const RingBuffer = @import("ring_buffer.zig").RingBuffer;
2026-07-12 19:46:40 +01:00
//
// STRUCTS
//
// --- SHADER ---
const ShaderID = union(enum) { pbr, unlit, custom: Handle(ShaderID) };
2026-07-12 19:46:40 +01:00
const GPUShader = struct { raw: *anyopaque };
// --- PIPELINE ---
const PipelineKey = struct { shader: ShaderID };
const GPUPipeline = struct { raw: *anyopaque };
/// ----------------------------------------------------
/// ----------------------------------------------------
const Material = struct {
/// ----------------------------------------------------
/// ----------------------------------------------------
pub const Style = struct {
albedo: @Vector(4, f32) = .{ 1, 1, 1, 1 },
metallic: f32 = 0.0,
roughness: f32 = 0.5,
};
/// ----------------------------------------------------
/// ----------------------------------------------------
pub const Unique = struct {
shader: ShaderID = .pbr,
front_face: enum { cw, ccw } = .ccw,
cull_mode: enum { back, front, none } = .back,
topology: enum { triangle_list, point_list, line_list } = .triangle_list,
blend_mode: enum { none, alpha, additive, multiply } = .none,
};
2026-07-12 19:46:40 +01:00
//
// FIELDS
2026-07-12 19:46:40 +01:00
//
style: Style = .{},
unique: Unique = .{},
};
test "SlotMap" {
// --- ALLOCATOR ---
const alloc = std.testing.allocator;
std.debug.print("{s}━━━ SLOTMAP ━━━{s}\n", .{ ansi.blue, ansi.reset });
2026-07-12 19:46:40 +01:00
// --- MATERIALS ---
var materials: SlotMap(Material) = .init(alloc);
defer materials.deinit();
2026-07-12 19:46:40 +01:00
// --- ADD MATERIALS ---
_ = try materials.put(.{ .style = .{ .albedo = .{ 0.8, 0.2, 0.2, 1.0 } } });
_ = try materials.put(.{ .unique = .{ .topology = .line_list } });
2026-07-12 19:46:40 +01:00
// --- # ---
if (log.print(.debug, "MATERIALS", ansi.blue)) |a| {
defer a.end();
2026-07-12 19:46:40 +01:00
// --- # ---
for (materials.items()) |material| {
if (a.section(.debug, "", ansi.cyan)) |b| {
defer b.end();
2026-07-12 19:46:40 +01:00
// --- STYLE ---
if (b.section(.debug, "STYLE", ansi.yellow)) |style| {
defer style.end();
2026-07-12 19:46:40 +01:00
style.write("Albedo: rgba({}, {}, {}, {})", .{ material.style.albedo[0], material.style.albedo[1], material.style.albedo[2], material.style.albedo[3] });
style.write("Metallic: {}", .{material.style.metallic});
style.write("Roughness: {}", .{material.style.roughness});
}
2026-07-12 19:46:40 +01:00
// --- STYLE ---
if (b.section(.debug, "UNIQUE", ansi.red)) |unique| {
defer unique.end();
2026-07-12 19:46:40 +01:00
unique.write("Shader: {s}", .{@tagName(material.unique.shader)});
unique.write("Topology: {s}", .{@tagName(material.unique.topology)});
unique.write("Cull Mode: {s}", .{@tagName(material.unique.cull_mode)});
}
}
}
2026-07-12 19:46:40 +01:00
}
}
2026-07-12 19:46:40 +01:00
test "SlotMap-DEP" {
2026-07-12 19:46:40 +01:00
{
2026-07-31 23:57:45 +01:00
// --- LOG ---
log.warn("...", .{}, @src());
log.err("...", .{}, @src());
log.info("...", .{}, @src());
log.debug("...", .{}, @src());
log.trace("...", .{}, @src());
// --- PRINT ---
if (log.print(.debug, "DEVICE INFO", ansi.bold ++ ansi.red)) |a| {
defer a.end();
// --- VALUES ---
a.write("device -> {s}", .{"GTX"});
a.write("vendor -> {s}", .{"Nvidia"});
// --- SECTION ---
if (a.section(.err, "MEMORY", ansi.green)) |b| {
defer b.end();
b.write("VRAM -> {}GB", .{24});
}
}
log.set(.{ .style = .rounded });
// --- PRINT ---
if (log.print(.debug, "DEVICE INFO", ansi.bold ++ ansi.red)) |a| {
defer a.end();
// --- VALUES ---
a.write("device -> {s}", .{"GTX"});
a.write("vendor -> {s}", .{"Nvidia"});
// --- SECTION ---
if (a.section(.err, "MEMORY", ansi.green)) |b| {
defer b.end();
b.write("VRAM -> {}GB", .{24});
}
}
log.set(.{ .style = .pipe });
// --- PRINT ---
if (log.print(.debug, "DEVICE INFO", ansi.bold ++ ansi.red)) |a| {
defer a.end();
// --- VALUES ---
a.write("device -> {s}", .{"GTX"});
a.write("vendor -> {s}", .{"Nvidia"});
// --- SECTION ---
if (a.section(.err, "MEMORY", ansi.green)) |b| {
defer b.end();
// --- VALUES ---
2026-07-31 23:57:45 +01:00
b.write("VRAM -> {}GB", .{24});
}
}
2026-07-12 19:46:40 +01:00
}
}
2026-07-13 16:41:14 +01:00
test "RingBuffer" {
std.debug.print("{s}━━━ RING BUFFER ━━━{s}\n", .{ ansi.blue, ansi.reset });
var prev_inputs: RingBuffer([]const u8, 256) = .{};
2026-07-13 16:41:14 +01:00
try prev_inputs.push("w");
try prev_inputs.push("s");
try prev_inputs.push("a");
try prev_inputs.push("d");
2026-07-13 16:41:14 +01:00
var i: usize = 0;
while (prev_inputs.pop()) |data| {
defer i += 1;
std.debug.print("[{}][PreviousInputs] -> \"{s}\"\n", .{ i, data });
}
}
test "FixedArrayList" {
std.debug.print("{s}━━━ FIXED ARRAY LIST ━━━{s}\n", .{ ansi.blue, ansi.reset });
var entities: FixedArrayList(u32, 4) = .empty;
try entities.append(1);
try entities.append(2);
try entities.append(3);
if (entities.getPtr(0)) |v| v.* = 69;
for (entities.items()) |item| {
std.debug.print("Entity: {}\n", .{item});
}
}
test "FixedSlotMap" {
std.debug.print("{s}━━━ FIXED SLOT MAP ━━━{s}\n", .{ ansi.blue, ansi.reset });
var materials: FixedSlotMap(Material, 3) = .empty;
_ = try materials.put(.{});
_ = try materials.put(.{});
const handle = try materials.put(.{ .unique = .{ .topology = .line_list } });
if (materials.get(handle)) |v| {
std.debug.print("Material: {}\n", .{v.unique.topology});
2026-07-13 16:41:14 +01:00
}
2026-07-12 19:46:40 +01:00
}