fixing vector times scalar
This commit is contained in:
@@ -154,3 +154,70 @@ test "Maths vectors" {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tests all mathematical operation on vec2/3/4 with scalars with all NZSL supported primitive types
|
||||
test "Maths vectors with scalars" {
|
||||
const allocator = std.testing.allocator;
|
||||
const types = [_]type{ f32, f64, i32, u32 };
|
||||
var operations = std.EnumMap(Operations, u8).init(.{
|
||||
.Mul = '*',
|
||||
.Div = '/',
|
||||
.Mod = '%',
|
||||
});
|
||||
|
||||
var it = operations.iterator();
|
||||
while (it.next()) |op| {
|
||||
inline for (2..5) |L| {
|
||||
inline for (types) |T| {
|
||||
const base_color: case.Vec(L, T) = .{ .val = case.random(@Vector(L, T)) };
|
||||
const ratio = case.random(T);
|
||||
const splat_ratio = @as(@Vector(L, T), @splat(ratio));
|
||||
const expected = switch (op.key) {
|
||||
.Mul => if (@typeInfo(T) == .int) @mulWithOverflow(base_color.val, splat_ratio)[0] else base_color.val * splat_ratio,
|
||||
.Div => if (@typeInfo(T) == .int) @divTrunc(base_color.val, splat_ratio) else base_color.val / splat_ratio,
|
||||
.Mod => @mod(base_color.val, splat_ratio),
|
||||
else => unreachable,
|
||||
};
|
||||
|
||||
const shader = try std.fmt.allocPrint(
|
||||
allocator,
|
||||
\\ [nzsl_version("1.1")]
|
||||
\\ [feature(float64)]
|
||||
\\ module;
|
||||
\\
|
||||
\\ struct FragOut
|
||||
\\ {{
|
||||
\\ [location(0)] color: vec{d}[{s}]
|
||||
\\ }}
|
||||
\\
|
||||
\\ [entry(frag)]
|
||||
\\ fn main() -> FragOut
|
||||
\\ {{
|
||||
\\ let output: FragOut;
|
||||
\\ output.color = vec{d}[{s}]({f}) {c} {d};
|
||||
\\ return output;
|
||||
\\ }}
|
||||
,
|
||||
.{
|
||||
L,
|
||||
@typeName(T),
|
||||
L,
|
||||
@typeName(T),
|
||||
base_color,
|
||||
op.value.*,
|
||||
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(&@as([L]T, expected)),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user