dinishing bindings, adding some tess

This commit is contained in:
Kbz-8
2025-10-25 01:48:42 +02:00
parent faacb7ced1
commit 3ddb09068a
6 changed files with 149 additions and 70 deletions

View File

@@ -1,6 +1,7 @@
const std = @import("std");
const DebugLevel = @import("lib.zig").DebugLevel;
const OptionHash = @import("lib.zig").OptionHash;
const FilesystemModuleResolver = @import("lib.zig").FilesystemModuleResolver;
const cnzsl = @cImport({
@cInclude("CNZSL/CNZSL.h");
});
@@ -21,37 +22,38 @@ pub fn deinit(self: Self) void {
}
pub fn enableDeadCodeRemoval(self: Self, enable: bool) void {
cnzsl.nzslBackendParametersEnableDeadCodeRemoval(self.instance, enable);
cnzsl.nzslBackendParametersEnableDeadCodeRemoval(self.instance, @intFromBool(enable));
}
pub fn enableOptimization(self: Self, enable: bool) void {
cnzsl.nzslBackendParametersEnableOptimization(self.instance, enable);
cnzsl.nzslBackendParametersEnableOptimization(self.instance, @intFromBool(enable));
}
pub fn enableResolving(self: Self, enable: bool) void {
cnzsl.nzslBackendParametersEnableResolving(self.instance, enable);
cnzsl.nzslBackendParametersEnableResolving(self.instance, @intFromBool(enable));
}
pub fn enableTargetRequired(self: Self, enable: bool) void {
cnzsl.nzslBackendParametersEnableTargetRequired(self.instance, enable);
cnzsl.nzslBackendParametersEnableTargetRequired(self.instance, @intFromBool(enable));
}
pub fn enableValidation(self: Self, enable: bool) void {
cnzsl.nzslBackendParametersEnableValidation(self.instance, enable);
cnzsl.nzslBackendParametersEnableValidation(self.instance, @intFromBool(enable));
}
pub fn setDebugLevel(self: Self, debug_level: DebugLevel) void {
cnzsl.nzslBackendParametersSetDebugLevel(self.instance, @intFromEnum(debug_level));
}
//pub fn SetModuleResolver_Filesystem(self: Self, const nzslFilesystemModuleResolver* resolverPtr) void {
//}
pub fn SetModuleResolverFilesystem(self: Self, filesystem_module_resolver: FilesystemModuleResolver) void {
cnzsl.nzslBackendParametersSetModuleResolver_Filesystem(@ptrCast(self.instance), @ptrCast(filesystem_module_resolver.instance));
}
pub fn setOption(self: Self, hash: OptionHash, value: anytype) void {
pub fn setOption(self: Self, hash: OptionHash, value: anytype) !void {
const T = @TypeOf(value);
switch (@typeInfo(T)) {
.vector => |v| self.setOption(hash, @as([v.len]v.child, value)),
.float, .int, .comptime_float, .comptime_int, .bool => self.setOption(hash, [1]T{value}),
.vector => |v| try self.setOption(hash, @as([v.len]v.child, value)),
.float, .int, .comptime_float, .comptime_int, .bool => try self.setOption(hash, [1]T{value}),
.array => |a| {
const sanitized_value, const sanitized_primitive_name = switch (@typeInfo(a.child)) {
.bool => blk: {
@@ -61,21 +63,52 @@ pub fn setOption(self: Self, hash: OptionHash, value: anytype) void {
}
break :blk .{ new_array, "bool" };
},
.float => |f| if (f.bits != 32) @compileError("Unhandled float size (not 32 bits)") else .{ value, @typeName(a.child) },
.int => |i| if (i.bits != 32) @compileError("Unhandled integer size (not 32 bits)") else .{ value, @typeName(a.child) },
.float => |f| if (f.bits == 32) .{ value, @typeName(a.child) } else return error.Not32BitsFloat,
.int => |i| if (i.bits == 32) .{ value, @typeName(a.child) } else return error.Not32BitsInt,
.comptime_float => .{ value, "f32" },
.comptime_int => .{ value, "i32" },
else => @compileError("Unhandled type"),
else => return error.UnhandledType,
};
const type_name, const c_value = switch (a.len) {
1 => .{ "" ++ sanitized_primitive_name, sanitized_value[0] },
2...4 => .{ "vec" ++ [_]u8{'0' + a.len} ++ sanitized_primitive_name, @as([*c]const a.child, @ptrCast(&sanitized_value)) },
else => @compileError("Unhandled array or vector size"),
2...4 => .{ "vec" ++ [_]u8{'0' + a.len} ++ sanitized_primitive_name, @as([*c]const @TypeOf(sanitized_value[0]), @ptrCast(&sanitized_value)) },
else => return error.UnhandledArrayOrVectorSize,
};
@call(.auto, @field(cnzsl, "nzslBackendParametersSetOption_" ++ type_name), .{ self.instance, hash, c_value });
},
else => @compileError("Unhandled type"),
else => return error.UnhandledType,
}
}
test "init" {
const params = try init();
defer params.deinit();
}
test "valid setOption" {
const params = try init();
defer params.deinit();
const primitive_types = [_]type{
f32,
i32,
u32,
bool,
};
inline for (1..4) |i| {
inline for (primitive_types) |T| {
const value: T = undefined;
if (i == 1) {
try params.setOption(0, value);
} else {
const array = [_]T{value} ** i;
const vector: @Vector(i, T) = array;
try params.setOption(0, vector);
try params.setOption(0, array);
}
}
}
}

View File

@@ -9,7 +9,7 @@ const Self = @This();
instance: *cnzsl.nzslFilesystemModuleResolver,
pub fn init() !Self {
const cfmr = cnzsl.nzslFilesystemModuleResolverCreate() orelse error.NullPointer;
const cfmr = cnzsl.nzslFilesystemModuleResolverCreate() orelse return error.NullPointer;
return .{
.instance = cfmr,
};
@@ -18,3 +18,24 @@ pub fn init() !Self {
pub fn deinit(self: Self) void {
cnzsl.nzslFilesystemModuleResolverDestroy(@ptrCast(self.instance));
}
pub fn getLastError(self: Self) ![]const u8 {
const err = cnzsl.nzslFilesystemModuleResolverGetLastError(self.instance) orelse return error.NullPointer;
return std.mem.span(err);
}
pub fn registerDirectory(self: Self, source_path: []const u8) void {
cnzsl.nzslFilesystemModuleResolverRegisterDirectory(@ptrCast(self.instance), source_path.ptr, source_path.len);
}
pub fn registerFile(self: Self, source_path: []const u8) void {
cnzsl.nzslFilesystemModuleResolverRegisterFile(@ptrCast(self.instance), source_path.ptr, source_path.len);
}
pub fn registerModule(self: Self, module: Module) void {
cnzsl.nzslFilesystemModuleResolverRegisterModule(@ptrCast(self.instance), @ptrCast(module.instance));
}
pub fn registerModuleFromSource(self: Self, source: []const u8) void {
cnzsl.nzslFilesystemModuleResolverRegisterModuleFromSource(@ptrCast(self.instance), source.ptr, source.len);
}

View File

@@ -80,25 +80,31 @@ pub const Output = struct {
return code[0..size];
}
pub fn getExplicitTextureBinding(self: InnerSelf, binding_name: [:0]const u8) i32 {
pub fn getExplicitTextureBinding(self: InnerSelf, binding_name: [:0]const u8) !i32 {
const res = cnzsl.nzslGlslOutputGetExplicitTextureBinding(self.instance, binding_name);
return if (res != -1) res else error.BindingNameNotFound;
if (res == -1) {
return error.BindingNameNotFound;
}
return res;
}
pub fn getExplicitUniformBlockBinding(self: InnerSelf, binding_name: [:0]const u8) i32 {
pub fn getExplicitUniformBlockBinding(self: InnerSelf, binding_name: [:0]const u8) !i32 {
const res = cnzsl.nzslGlslOutputGetExplicitUniformBlockBinding(self.instance, binding_name);
return if (res != -1) res else error.BindingNameNotFound;
if (res == -1) {
return error.BindingNameNotFound;
}
return res;
}
pub fn usesDrawParameterBaseInstanceUniform(self: InnerSelf) bool {
return cnzsl.nzslGlslOutputGetUsesDrawParameterBaseInstanceUniform(self.instance);
return cnzsl.nzslGlslOutputGetUsesDrawParameterBaseInstanceUniform(self.instance) != 0;
}
pub fn usesDrawParameterBaseVertexUniform(self: InnerSelf) bool {
return cnzsl.nzslGlslOutputGetUsesDrawParameterBaseVertexUniform(self.instance);
return cnzsl.nzslGlslOutputGetUsesDrawParameterBaseVertexUniform(self.instance) != 0;
}
pub fn usesDrawParameterDrawIndexUniform(self: InnerSelf) bool {
return cnzsl.nzslGlslOutputGetUsesDrawParameterDrawIndexUniform(self.instance);
return cnzsl.nzslGlslOutputGetUsesDrawParameterDrawIndexUniform(self.instance) != 0;
}
};

View File

@@ -31,3 +31,19 @@ pub const Module = @import("Module.zig");
pub const parser = @import("parser.zig");
pub const Serializer = @import("Serializer.zig");
pub const SpirvWriter = @import("SpirvWriter.zig");
test {
std.testing.refAllDeclsRecursive(@This());
}
test "hashOption" {
try std.testing.expect(try hashOption("test") == 2949673445);
try std.testing.expect(try hashOption("nzsl") == 1968067102);
try std.testing.expect(try hashOption("shaderlang") == 1855243988);
try std.testing.expect(try hashOption("a") == 3826002220);
try std.testing.expect(try hashOption("aa") == 1277494327);
try std.testing.expect(try hashOption("aaa") == 876991330);
try std.testing.expect(try hashOption("aaaa") == 1290481081);
try std.testing.expect(try hashOption("aaaaa") == 3996723976);
try std.testing.expect(try hashOption("aaaaaa") == 1828673099);
}