Skip to content

Instantly share code, notes, and snippets.

@koljakube
Created June 14, 2020 12:57
Show Gist options
  • Save koljakube/58a5026594b72fb0832a18a3aac24502 to your computer and use it in GitHub Desktop.
Save koljakube/58a5026594b72fb0832a18a3aac24502 to your computer and use it in GitHub Desktop.
pub fn VertexBuffer(comptime T: type, comptime usage: e.Usage) type {
const Wrapped = c.sg_buffer;
const Desc = c.sg_buffer_desc;
if (@typeInfo(T).Pointer.size != std.builtin.TypeInfo.Pointer.Size.Slice) {
@compileError("buffer data must be a slice");
}
if (usage == e.Usage.Immutable and !@typeInfo(T).Pointer.is_const) {
@compileError("can not use variable data for immutable buffers");
}
const is_immutable = usage == e.Usage.Immutable;
const elem_size = @sizeOf(@typeInfo(T).Pointer.child);
return struct {
const Self = @This();
s: Wrapped = undefined,
pub fn init(data: T) Self {
var d = util.zeroed(Desc);
d.type = e.BufferType.VertexBuffer.sokolValue();
d.usage = usage.sokolValue();
d.content = data.ptr;
d.size = @intCast(c_int, elem_size * data.len);
return Self{ .s = c.sg_make_buffer(&d) };
}
pub fn deinit(self: Self) void {
c.sg_destroy_buffer(self.s);
}
usingnamespace if (!is_immutable)
struct {
pub fn update(self: Self, data: T) void {
c.sg_update_buffer(self.s, data.ptr, @intCast(c_int, elem_size * data.len));
}
}
else
struct {};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment