Skip to content

Instantly share code, notes, and snippets.

View lassade's full-sized avatar

Felipe Jorge lassade

View GitHub Profile
@lassade
lassade / inspector.zig
Created May 11, 2025 20:08
A simple example of how a recursive inspector can be implemented
pub fn inspector(context: anytype) void {
if (imgui.Begin("Inspector", .{})) {
const World = @typeInfo(@TypeOf(context.world)).pointer.child;
inline for (World.entities, 0..) |E, k| {
if (k == context.target.kind) {
const slice = context.world.tables[k].columns.slice();
if (context.target.id >= slice.len) break; // invalid
@lassade
lassade / rtti.zig
Created May 11, 2025 19:59
Simple reflection system for zig
const std = @import("std");
const builtin = @import("builtin");
pub const Reflection = struct {
internal: []const type,
types: []const Type,
lookup: std.StaticStringMap(LocalId), // todo: use HashMap for this ?
pub const LocalId = u32;
pub const invalid_id = std.math.maxInt(LocalId);
@lassade
lassade / DebugAllocator.zig
Last active September 5, 2024 23:51
A allocator with leak test that only accuses unreachable memory as leaked. You can then don't bother to free memory at the end of your program, can check for leaks many times during the execution of your program;
// update 1 - fix sorting, skip std.mem.Allocator, added a leak counter
const std = @import("std");
const Allocator = std.mem.Allocator;
const StackTrace = std.builtin.StackTrace;
const log = std.log.scoped(.mem);
const tracy = @import("tracy");
// todo: support for double free
pub const DebugAllocator = struct {
@lassade
lassade / fixed_swiss_hashmap.zig
Last active August 31, 2024 03:59
A fixed (no allocations) hashmap that follows the Google Swiss Table design principles
const std = @import("std");
const builtin = @import("builtin");
pub const StringContext = struct {
pub inline fn hash(_: @This(), s: []const u8) u64 {
return std.hash_map.hashString(s);
}
pub inline fn eql(_: @This(), a: []const u8, b: []const u8) bool {
return std.mem.eql(u8, a, b);
}
@lassade
lassade / rc.zig
Created August 9, 2024 22:41
reference counted implementation for critique
const std = @import("std");
const Atomic = std.atomic.Value;
// core is a upper namespace, with way more stuff with it
const core = struct {
pub var allocator: std.mem.Allocator = undefined;
};
/// lock-free stack
pub const Stack = extern struct {
@lassade
lassade / slabAlloc.zig
Created July 17, 2024 19:27
allocates many slices with different sizes with a single allocation
pub fn SlabAllocation(comptime types: []const type) type {
var alignment: usize = 1;
var slices: [types.len]type = undefined;
for (types, 0..) |T, i| {
slices[i] = []T;
alignment = @max(alignment, @alignOf(T));
}
const out_alignment = alignment;
const Slices = std.meta.Tuple(&slices);
return struct {
@lassade
lassade / decode_hex.zig
Last active June 3, 2024 02:17
zig decodeHex SIMD
// compile with: -mcpu=x86_64+sse2 to ensure sse2 support
pub fn decodeHex(input: []const u8, output: []u8) !void {
const block_size = 16; // change block size to use avx2
const ByteBlock = @Vector(block_size, u8);
const IntBlock = @Vector(block_size / 4, u32);
if (input.len & 1 != 0) {
return error.OddLenghtInput;
}
@lassade
lassade / UnitySRP.cs
Last active September 28, 2022 19:39
Unity SRP
// SPR isn't a silver bullet for the driver hell, for my experience DX11 works best in all platforms
// but Vulkan often has a better performance:
//
// Intel - DX11 works and runs faster than Vulkan that crashes a lot
// AMD - DX11 video enconding doesn't work in some cards, Vulkan has no issues
// NVIDEA - I had no problems with DX11 or Vulkan
// on a compute shader
Buffer<uint2> _SourcePositions; // WORNG! I can swear it was working somehow but it stopped working at some point
StructuredBuffer<uint2> _SourcePositions; // RIGHT!