adding base matrix management
This commit is contained in:
@@ -221,3 +221,76 @@ test "Maths vectors with scalars" {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tests all mathematical operation on mat3/4 with all NZSL supported primitive types
|
||||
test "Maths matrices" {
|
||||
const allocator = std.testing.allocator;
|
||||
const types = [_]type{ f32, f64 };
|
||||
var operations = std.EnumMap(Operations, u8).init(.{
|
||||
.Add = '+',
|
||||
.Sub = '-',
|
||||
.Mul = '*',
|
||||
});
|
||||
|
||||
var it = operations.iterator();
|
||||
while (it.next()) |op| {
|
||||
inline for (3..5) |L| {
|
||||
inline for (types) |T| {
|
||||
const base: case.Mat(L, T) = .{ .val = case.random([L][L]T) };
|
||||
const ratio: case.Mat(L, T) = .{ .val = case.random([L][L]T) };
|
||||
var expected: case.Mat(L, T) = undefined;
|
||||
for (expected.val[0..], base.val[0..], ratio.val[0..]) |*ec, bc, rc| {
|
||||
for (ec[0..], bc[0..], rc[0..]) |*e, b, r| {
|
||||
e.* = switch (op.key) {
|
||||
.Add => b + r,
|
||||
.Sub => b - r,
|
||||
.Mul => b * r,
|
||||
else => unreachable,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const shader = try std.fmt.allocPrint(
|
||||
allocator,
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ [feature(float64)]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {{
|
||||
\\ [location(0)] value: mat{d}[{s}]
|
||||
\\ }}
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main() -> FragOut
|
||||
\\ {{
|
||||
\\ let output: FragOut;
|
||||
\\ output.value = mat{d}[{s}]({f}) {c} mat{d}[{s}]({f});
|
||||
\\ return output;
|
||||
\\ }}
|
||||
,
|
||||
.{
|
||||
L,
|
||||
@typeName(T),
|
||||
L,
|
||||
@typeName(T),
|
||||
base,
|
||||
op.value.*,
|
||||
L,
|
||||
@typeName(T),
|
||||
ratio,
|
||||
},
|
||||
);
|
||||
defer allocator.free(shader);
|
||||
const code = try compileNzsl(allocator, shader);
|
||||
defer allocator.free(code);
|
||||
try case.expect(.{
|
||||
.source = code,
|
||||
.expected_outputs = &.{
|
||||
std.mem.asBytes(&expected),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+26
-4
@@ -33,12 +33,12 @@ pub const case = struct {
|
||||
|
||||
// To test with all important module options
|
||||
const module_options = [_]spv.Module.ModuleOptions{
|
||||
.{
|
||||
.use_simd_vectors_specializations = true,
|
||||
},
|
||||
.{
|
||||
.use_simd_vectors_specializations = false,
|
||||
},
|
||||
//.{
|
||||
// .use_simd_vectors_specializations = true,
|
||||
//},
|
||||
};
|
||||
|
||||
for (module_options) |opt| {
|
||||
@@ -78,7 +78,7 @@ pub const case = struct {
|
||||
}
|
||||
|
||||
pub fn random(comptime T: type) T {
|
||||
var prng: std.Random.DefaultPrng = .init(@intCast(std.Io.Timestamp.now(std.testing.io, .real).toMicroseconds()));
|
||||
var prng: std.Random.DefaultPrng = .init(@intCast(std.Io.Timestamp.now(std.testing.io, .real).toNanoseconds()));
|
||||
const rand = prng.random();
|
||||
|
||||
return switch (@typeInfo(T)) {
|
||||
@@ -91,6 +91,13 @@ pub const case = struct {
|
||||
}
|
||||
break :blk vec;
|
||||
},
|
||||
.array => |a| blk: {
|
||||
var arr: [a.len]a.child = undefined;
|
||||
inline for (0..a.len) |i| {
|
||||
arr[i] = random(a.child);
|
||||
}
|
||||
break :blk arr;
|
||||
},
|
||||
inline else => unreachable,
|
||||
};
|
||||
}
|
||||
@@ -107,6 +114,21 @@ pub const case = struct {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn Mat(comptime len: usize, comptime T: type) type {
|
||||
return struct {
|
||||
const Self = @This();
|
||||
val: [len][len]T,
|
||||
pub fn format(self: *const Self, w: *std.Io.Writer) std.Io.Writer.Error!void {
|
||||
inline for (0..len) |i| {
|
||||
inline for (0..len) |j| {
|
||||
try w.print("{d}", .{self.val[i][j]});
|
||||
if (i < len - 1 or j < len - 1) try w.writeAll(", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
test {
|
||||
|
||||
Reference in New Issue
Block a user