Big vecmath package

This commit is contained in:
2026-01-02 00:06:30 +01:00
parent 3ac7f5654d
commit fd16e5a2b0
9 changed files with 2691 additions and 244 deletions

View File

@@ -1,7 +1,11 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
_ = b.addModule("media", .{
const vm_dep = b.dependency("vecmath", .{});
const vm_module = vm_dep.module("vecmath");
const root_module = b.addModule("media", .{
.root_source_file = b.path("src/root.zig"),
});
root_module.addImport("vecmath", vm_module);
}

View File

@@ -8,4 +8,9 @@
"build.zig.zon",
},
.fingerprint = 0x6a2ca10cfcadaa2b,
.dependencies = .{
.vecmath = .{
.path = "../vecmath",
},
},
}

View File

@@ -1,227 +0,0 @@
const std = @import("std");
pub const ColorU8 = extern struct {
r: u8,
g: u8,
b: u8,
a: u8,
pub const Array = [4]u8;
pub const Vector = @Vector(4, u8);
pub const clear = ColorU8.init(0, 0, 0, 0);
pub const black = ColorU8.init(0, 0, 0, 255);
pub const white = ColorU8.init(255, 255, 255, 255);
pub inline fn init(r: u8, g: u8, b: u8, a: u8) ColorU8 {
return .{ .r = r, .g = g, .b = b, .a = a };
}
pub inline fn initArray(array: Array) ColorU8 {
return @bitCast(array);
}
pub inline fn initVector(vector: Vector) ColorU8 {
return @bitCast(@as(Array, vector));
}
pub inline fn initColorF16(color: ColorF16) ColorU8 {
const zero: ColorF16.Vector = @splat(0);
const one: ColorF16.Vector = @splat(1);
const scale: ColorF16.Vector = @splat(255);
const vector = @round(std.math.clamp(color.asVector(), zero, one) * scale);
return .{
.r = @intFromFloat(vector[0]),
.g = @intFromFloat(vector[1]),
.b = @intFromFloat(vector[2]),
.a = @intFromFloat(vector[3]),
};
}
pub inline fn initColorF32(color: ColorF32) ColorU8 {
const zero: ColorF32.Vector = @splat(0);
const one: ColorF32.Vector = @splat(1);
const scale: ColorF32.Vector = @splat(255);
const vector = @round(std.math.clamp(color.asVector(), zero, one) * scale);
return .{
.r = @intFromFloat(vector[0]),
.g = @intFromFloat(vector[1]),
.b = @intFromFloat(vector[2]),
.a = @intFromFloat(vector[3]),
};
}
pub inline fn asArray(self: ColorU8) Array {
return @bitCast(self);
}
pub inline fn asVector(self: ColorU8) Vector {
return @as(Array, @bitCast(self));
}
};
pub const ColorF16 = extern struct {
r: f16,
g: f16,
b: f16,
a: f16,
pub const Array = [4]f16;
pub const Vector = @Vector(4, f16);
pub const clear = ColorF16.init(0.0, 0.0, 0.0, 0.0);
pub const black = ColorF16.init(0.0, 0.0, 0.0, 1.0);
pub const white = ColorF16.init(1.0, 1.0, 1.0, 1.0);
pub inline fn init(r: f16, g: f16, b: f16, a: f16) ColorF16 {
return .{ .r = r, .g = g, .b = b, .a = a };
}
pub inline fn initArray(array: Array) ColorF16 {
return @bitCast(array);
}
pub inline fn initVector(vector: Vector) ColorF16 {
return @bitCast(@as(Array, vector));
}
pub inline fn initColorU8(color: ColorU8) ColorF16 {
const vector = Vector{
@as(f32, color.r),
@as(f32, color.g),
@as(f32, color.b),
@as(f32, color.a),
} / @as(Vector, @splat(255.0));
return @bitCast(@as(Array, vector));
}
pub inline fn initColorF32(color: ColorF32) ColorF16 {
return @bitCast(@as(Array, @as(Vector, @floatCast(color.asVector()))));
}
pub inline fn asArray(self: ColorF16) Array {
return @bitCast(self);
}
pub inline fn asVector(self: ColorF16) Vector {
return @as(Array, @bitCast(self));
}
pub inline fn add(self: ColorF16, other: ColorF16) ColorF16 {
return .initVector(self.asVector() + other.asVector());
}
pub inline fn sub(self: ColorF16, other: ColorF16) ColorF16 {
return .initVector(self.asVector() - other.asVector());
}
pub inline fn mul(self: ColorF16, other: ColorF16) ColorF16 {
return .initVector(self.asVector() * other.asVector());
}
pub inline fn div(self: ColorF16, other: ColorF16) ColorF16 {
return .initVector(self.asVector() / other.asVector());
}
pub inline fn mulScalar(self: ColorF16, scalar: f16) ColorF16 {
const scalar_vector: Vector = @splat(scalar);
return .initVector(self.asVector() * scalar_vector);
}
pub inline fn divScalar(self: ColorF16, scalar: f16) ColorF16 {
const scalar_vector: Vector = @splat(scalar);
return .initVector(self.asVector() / scalar_vector);
}
pub inline fn lerp(self: ColorF16, other: ColorF16, t: f16) ColorF16 {
const s = 1.0 - t;
const t_vector: Vector = @splat(t);
const s_vector: Vector = @splat(s);
return .initVector(self.asVector() * s_vector + other.asVector() * t_vector);
}
};
pub const ColorF32 = extern struct {
r: f32,
g: f32,
b: f32,
a: f32,
pub const Array = [4]f32;
pub const Vector = @Vector(4, f32);
pub const clear = ColorF32.init(0.0, 0.0, 0.0, 0.0);
pub const black = ColorF32.init(0.0, 0.0, 0.0, 1.0);
pub const white = ColorF32.init(1.0, 1.0, 1.0, 1.0);
pub inline fn init(r: f32, g: f32, b: f32, a: f32) ColorF32 {
return .{ .r = r, .g = g, .b = b, .a = a };
}
pub inline fn initArray(array: Array) ColorF32 {
return @bitCast(array);
}
pub inline fn initVector(vector: Vector) ColorF32 {
return @bitCast(@as(Array, vector));
}
pub inline fn initColorU8(color: ColorU8) ColorF32 {
const vector = Vector{
@as(f32, color.r),
@as(f32, color.g),
@as(f32, color.b),
@as(f32, color.a),
} / @as(Vector, @splat(255.0));
return @bitCast(@as(Array, vector));
}
pub inline fn initColorF16(color: ColorF16) ColorF32 {
return @bitCast(@as(Array, @as(Vector, @floatCast(color.asVector()))));
}
pub inline fn asArray(self: ColorF32) Array {
return @bitCast(self);
}
pub inline fn asVector(self: ColorF32) Vector {
return @as(Array, @bitCast(self));
}
pub inline fn add(self: ColorF32, other: ColorF32) ColorF32 {
return .initVector(self.asVector() + other.asVector());
}
pub inline fn sub(self: ColorF32, other: ColorF32) ColorF32 {
return .initVector(self.asVector() - other.asVector());
}
pub inline fn mul(self: ColorF32, other: ColorF32) ColorF32 {
return .initVector(self.asVector() * other.asVector());
}
pub inline fn div(self: ColorF32, other: ColorF32) ColorF32 {
return .initVector(self.asVector() / other.asVector());
}
pub inline fn mulScalar(self: ColorF32, scalar: f32) ColorF32 {
const scalar_vector: Vector = @splat(scalar);
return .initVector(self.asVector() * scalar_vector);
}
pub inline fn divScalar(self: ColorF32, scalar: f32) ColorF32 {
const scalar_vector: Vector = @splat(scalar);
return .initVector(self.asVector() / scalar_vector);
}
pub inline fn lerp(self: ColorF32, other: ColorF32, t: f32) ColorF32 {
const s = 1.0 - t;
const t_vector: Vector = @splat(t);
const s_vector: Vector = @splat(s);
return .initVector(self.asVector() * s_vector + other.asVector() * t_vector);
}
};

View File

@@ -1,25 +1,24 @@
const std = @import("std");
const Color = @import("color.zig").Color;
const vm = @import("vecmath");
pub fn Static(comptime W: u32, comptime H: u32) type {
return struct {
data: [W * H]Color,
data: [W * H]vm.Color,
pub const width = W;
pub const height = H;
pub fn getPixel(self: *const @This(), x: u32, y: u32) Color {
pub fn getPixel(self: *const @This(), x: u32, y: u32) vm.Color {
std.debug.assert(x < width and y < height);
return self.data[y * width + x];
}
pub fn setPixel(self: *@This(), x: u32, y: u32, color: Color) void {
pub fn setPixel(self: *@This(), x: u32, y: u32, color: vm.Color) void {
std.debug.assert(x < width and y < height);
self.data[y * width + x] = color;
}
pub fn fill(self: *@This(), color: Color) void {
pub fn fill(self: *@This(), color: vm.Color) void {
@memset(self.data, color);
}
};
@@ -29,9 +28,9 @@ pub const Dynamic = struct {
width: u32,
height: u32,
data: [*]Color,
data: [*]vm.Color,
pub fn initBuffer(width: u32, height: u32, buffer: []Color) @This() {
pub fn initBuffer(width: u32, height: u32, buffer: []vm.Color) @This() {
std.debug.assert(buffer.len == width * height);
return .{
.width = width,
@@ -41,7 +40,7 @@ pub const Dynamic = struct {
}
pub fn initAlloc(width: u32, height: u32, allocator: std.mem.Allocator) !@This() {
const buffer = try allocator.alloc(Color, width * height);
const buffer = try allocator.alloc(vm.Color, width * height);
return .{
.width = width,
.height = height,
@@ -53,17 +52,17 @@ pub const Dynamic = struct {
allocator.free(self.data[0 .. self.width * self.height]);
}
pub fn getPixel(self: *const @This(), x: u32, y: u32) Color {
pub fn getPixel(self: *const @This(), x: u32, y: u32) vm.Color {
std.debug.assert(x < self.width and y < self.height);
return self.data[y * self.width + x];
}
pub fn setPixel(self: *@This(), x: u32, y: u32, color: Color) void {
pub fn setPixel(self: *@This(), x: u32, y: u32, color: vm.Color) void {
std.debug.assert(x < self.width and y < self.height);
self.data[y * self.width + x] = color;
}
pub fn fill(self: *@This(), color: Color) void {
pub fn fill(self: *@This(), color: vm.Color) void {
@memset(self.data[0 .. self.width * self.height], color);
}
};

View File

@@ -35,9 +35,7 @@ pub fn info(buffer: []const u8) ?Header {
}
if (samples == 0) {
return .{
.streaming = {},
};
return .streaming;
} else {
return .{
.static = .{

View File

@@ -1,5 +1,4 @@
pub const audio = @import("audio.zig");
pub const color = @import("color.zig");
pub const image = @import("image.zig");
pub const qoa = @import("qoa.zig");
pub const qoi = @import("qoi.zig");

View File

@@ -0,0 +1,7 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
_ = b.addModule("vecmath", .{
.root_source_file = b.path("src/root.zig"),
});
}

View File

@@ -0,0 +1,11 @@
.{
.name = .vecmath,
.version = "0.0.0",
.minimum_zig_version = "0.15.2",
.paths = .{
"src",
"build.zig",
"build.zig.zon",
},
.fingerprint = 0x1c1c724ae9962a2d,
}

File diff suppressed because it is too large Load Diff