improving mul test
Some checks failed
Build / build (push) Successful in 55s
Test / build (push) Failing after 4m26s

This commit is contained in:
2026-01-14 20:14:23 +01:00
parent b8a564e135
commit 88e847e2d9
4 changed files with 103 additions and 85 deletions

View File

@@ -13,6 +13,12 @@ const SpvByte = spv.SpvByte;
const SpvWord = spv.SpvWord;
const SpvBool = spv.SpvBool;
const MathType = enum {
Float,
SInt,
UInt,
};
pub const OpCodeFunc = *const fn (std.mem.Allocator, SpvWord, *Runtime) RuntimeError!void;
pub const SetupDispatcher = block: {
@@ -55,8 +61,8 @@ pub const RuntimeDispatcher = block: {
.AccessChain = opAccessChain,
.CompositeConstruct = opCompositeConstruct,
.CompositeExtract = opCompositeExtract,
.FMul = opFMul,
.IMul = opIMul,
.FMul = maths(.Float).opMul,
.IMul = maths(.SInt).opMul,
.Load = opLoad,
.Return = opReturn,
.Store = opStore,
@@ -592,7 +598,9 @@ fn opReturn(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
}
}
fn opFMul(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
fn maths(comptime T: MathType) type {
return struct {
fn opMul(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
const target_type = (rt.results[try rt.it.next()].variant orelse return RuntimeError.InvalidSpirV).Type;
const value = try rt.results[try rt.it.next()].getValue();
const op1_value = try rt.results[try rt.it.next()].getValue();
@@ -600,57 +608,46 @@ fn opFMul(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
const size = sw: switch (target_type) {
.Vector => |v| continue :sw (rt.results[v.components_type_word].variant orelse return RuntimeError.InvalidSpirV).Type,
.Float => |f| f.bit_length,
.Float => |f| if (T == .Float) f.bit_length else return RuntimeError.InvalidSpirV,
.Int => |i| if (T == .SInt or T == .UInt) i.bit_length else return RuntimeError.InvalidSpirV,
else => return RuntimeError.InvalidSpirV,
};
const operator = struct {
fn process(bit_count: SpvWord, v: *Result.Value, op1_v: *const Result.Value, op2_v: *const Result.Value) RuntimeError!void {
switch (bit_count) {
switch (T) {
.Float => switch (bit_count) {
16 => v.Float.float16 = op1_v.Float.float16 * op2_v.Float.float16,
32 => v.Float.float32 = op1_v.Float.float32 * op2_v.Float.float32,
64 => v.Float.float64 = op1_v.Float.float64 * op2_v.Float.float64,
else => return RuntimeError.InvalidSpirV,
},
.SInt => switch (bit_count) {
8 => v.Int.sint8 = @mulWithOverflow(op1_v.Int.sint8, op2_v.Int.sint8)[0],
16 => v.Int.sint16 = @mulWithOverflow(op1_v.Int.sint16, op2_v.Int.sint16)[0],
32 => v.Int.sint32 = @mulWithOverflow(op1_v.Int.sint32, op2_v.Int.sint32)[0],
64 => v.Int.sint64 = @mulWithOverflow(op1_v.Int.sint64, op2_v.Int.sint64)[0],
else => return RuntimeError.InvalidSpirV,
},
.UInt => switch (bit_count) {
8 => v.Int.uint8 = @mulWithOverflow(op1_v.Int.uint8, op2_v.Int.uint8)[0],
16 => v.Int.uint16 = @mulWithOverflow(op1_v.Int.uint16, op2_v.Int.uint16)[0],
32 => v.Int.uint32 = @mulWithOverflow(op1_v.Int.uint32, op2_v.Int.uint32)[0],
64 => v.Int.uint64 = @mulWithOverflow(op1_v.Int.uint64, op2_v.Int.uint64)[0],
else => return RuntimeError.InvalidSpirV,
},
}
}
};
switch (value.*) {
.Float => try operator.process(size, value, op1_value, op2_value),
.Float => if (T == .Float) try operator.process(size, value, op1_value, op2_value) else return RuntimeError.InvalidSpirV,
.Int => if (T == .SInt or T == .UInt) try operator.process(size, value, op1_value, op2_value) else return RuntimeError.InvalidSpirV,
.Vector => |vec| for (vec, op1_value.Vector, op2_value.Vector) |*val, op1_v, op2_v| try operator.process(size, val, &op1_v, &op2_v),
else => return RuntimeError.InvalidSpirV,
}
}
fn opIMul(_: std.mem.Allocator, _: SpvWord, rt: *Runtime) RuntimeError!void {
const target_type = (rt.results[try rt.it.next()].variant orelse return RuntimeError.InvalidSpirV).Type;
const value = try rt.results[try rt.it.next()].getValue();
const op1_value = try rt.results[try rt.it.next()].getValue();
const op2_value = try rt.results[try rt.it.next()].getValue();
const size = sw: switch (target_type) {
.Vector => |v| continue :sw (rt.results[v.components_type_word].variant orelse return RuntimeError.InvalidSpirV).Type,
.Int => |i| i.bit_length,
else => return RuntimeError.InvalidSpirV,
};
const operator = struct {
fn process(bit_count: SpvWord, v: *Result.Value, op1_v: *const Result.Value, op2_v: *const Result.Value) RuntimeError!void {
switch (bit_count) {
8 => v.Int.sint8 = op1_v.Int.sint8 * op2_v.Int.sint8,
16 => v.Int.sint16 = op1_v.Int.sint16 * op2_v.Int.sint16,
32 => v.Int.sint32 = op1_v.Int.sint32 * op2_v.Int.sint32,
64 => v.Int.sint64 = op1_v.Int.sint64 * op2_v.Int.sint64,
else => return RuntimeError.InvalidSpirV,
}
}
};
switch (value.*) {
.Int => try operator.process(size, value, op1_value, op2_value),
.Vector => |vec| for (vec, op1_value.Vector, op2_value.Vector) |*val, op1_v, op2_v| try operator.process(size, val, &op1_v, &op2_v),
else => return RuntimeError.InvalidSpirV,
}
}
fn setupConstant(allocator: std.mem.Allocator, rt: *Runtime) RuntimeError!*Result {

View File

@@ -25,5 +25,5 @@ test "FMul vec4[f32]" {
const code = try compileNzsl(allocator, shader);
defer allocator.free(code);
try case.expectOutput(f32, code, "color", &.{ 4, 3, 2, 1 });
try case.expectOutput(f32, 4, code, "color", &.{ 4, 3, 2, 1 });
}

View File

@@ -5,25 +5,26 @@ const case = root.case;
test "Mul vec4" {
const allocator = std.testing.allocator;
const types = [_]type{ f32, i32 };
const types = [_]type{
f32,
//f64,
i32,
u32,
};
inline for (types) |T| {
const prng: std.Random.DefaultPrng = .init(@intCast(std.time.microTimestamp()));
const base_color: [4]T = undefined;
std.Random.shuffle(prng, T, base_color);
const ratio: [4]T = undefined;
std.Random.shuffle(prng, T, ratio);
const expected = [4]T{
base_color[0] * ratio[0],
base_color[1] * ratio[1],
base_color[2] * ratio[2],
base_color[3] * ratio[3],
const base_color = case.random(@Vector(4, T));
const ratio = case.random(@Vector(4, T));
const expected = switch (@typeInfo(T)) {
.float => base_color * ratio,
.int => @mulWithOverflow(base_color, ratio)[0],
else => unreachable,
};
const shader = try std.fmt.allocPrint(
allocator,
\\ [nzsl_version("1.1")]
\\ [feature(float64)]
\\ module;
\\
\\ struct FragOut
@@ -41,6 +42,7 @@ test "Mul vec4" {
\\ return output;
\\ }}
,
.{
@typeName(T),
@typeName(T),
ratio[0],
@@ -52,10 +54,11 @@ test "Mul vec4" {
base_color[1],
base_color[2],
base_color[3],
},
);
defer allocator.free(shader);
const code = try compileNzsl(allocator, shader);
defer allocator.free(code);
try case.expectOutput(f32, code, "color", &expected);
try case.expectOutput(T, 4, code, "color", &@as([4]T, expected));
}
}

View File

@@ -20,7 +20,7 @@ pub fn compileNzsl(allocator: std.mem.Allocator, source: []const u8) ![]const u3
}
pub const case = struct {
pub fn expectOutput(comptime T: type, source: []const u32, output_name: []const u8, comptime expected: []const T) !void {
pub fn expectOutput(comptime T: type, comptime len: usize, source: []const u32, output_name: []const u8, expected: []const T) !void {
const allocator = std.testing.allocator;
var module = try spv.Module.init(allocator, source);
@@ -30,11 +30,29 @@ pub const case = struct {
defer rt.deinit(allocator);
try rt.callEntryPoint(allocator, try rt.getEntryPointByName("main"));
var output: [expected.len]T = undefined;
try rt.readOutput(T, output[0..output.len], try rt.getResultByName(output_name));
var output: [len]T = undefined;
try rt.readOutput(T, output[0..len], try rt.getResultByName(output_name));
try std.testing.expectEqualSlices(T, expected, &output);
}
pub fn random(comptime T: type) T {
var prng: std.Random.DefaultPrng = .init(@intCast(std.time.microTimestamp()));
const rand = prng.random();
return switch (@typeInfo(T)) {
.int => rand.int(T),
.float => rand.float(T),
.vector => |v| blk: {
var vec: @Vector(v.len, v.child) = undefined;
for (0..v.len) |i| {
vec[i] = random(v.child);
}
break :blk vec;
},
inline else => unreachable,
};
}
};
test {