+ WIP bindings

This commit is contained in:
REMqb
2024-04-30 20:05:18 +02:00
parent 97c82cd688
commit b3ae163518
6 changed files with 115 additions and 147 deletions

View File

@@ -1,13 +1,33 @@
//const nzsl = @cImport({
// @cInclude("CNZSL/CNZSL.h");
//});
// const cnzsl = @cImport({
// @cInclude("CNZSL/CNZSL.h");
// });
const std = @import("std");
const cnzsl = @import("cimport.zig");
const cnzsl = @import("nzsl-c.zig");
pub const DebugLevel = cnzsl.nzslDebugLevel;
pub const OptionHash = cnzsl.nzslOptionHash;
pub fn parseSource(source: []const u8) !Module {
const module = Module{.instance = null};
return parseSourceWithFilePath(source, "");
}
if(cnzsl.nzslParserParseSource(module.instance, source.ptr, source.len) != 0) {
pub fn parseSourceWithFilePath(source: []const u8, filePath: []const u8) !Module {
const module = Module.create();
if(cnzsl.nzslParserParseSourceWithFilePath(module.instance, source.ptr, source.len, filePath.ptr, filePath.len) != 0) {
defer module.release();
std.log.err("Error while parsing source({s}): {s}", .{ filePath, module.getLastError() });
return error.FailedToParse;
}
return module;
}
pub fn parseFromFile(filePath: []const u8) !Module {
const module = Module.create();
if(cnzsl.nzslParserParseFromFile(module.instance, filePath.ptr, filePath.len) != 0) {
defer module.release();
std.log.err("Error while parsing source: {s}", .{ module.getLastError() });
@@ -17,45 +37,13 @@ pub fn parseSource(source: []const u8) !Module {
return module;
}
pub fn parseSourceWithFilePath(source: []const u8, filePath: []const u8) !Module {
const module: ?*cnzsl.nzslModule = null;
if(cnzsl.nzslParserParseSourceWithFilePath(module, source.ptr, source.len, filePath.ptr, filePath.len) != 0) {
defer cnzsl.nzslModuleDestroy(module);
std.log.err("Error while parsing source: {s}", .{ module.getLastError() });
return error.FailedToParse;
}
if (module == null) {
std.log.err("Error while parsing source: Unknown error", .{ });
return error.FailedToParse;
}
return .{.instance = module};
}
pub fn parseFromFile(filePath: []const u8) !Module {
const module: ?*cnzsl.nzslModule = null;
if(cnzsl.nzslParserParseFromFile(module, filePath.ptr, filePath.len) != 0) {
defer cnzsl.nzslModuleDestroy(module);
std.log.err("Error while parsing source: {s}", .{ cnzsl.nzslModuleGetLastError(module) });
return error.FailedToParse;
}
if (module == null) {
std.log.err("Error while parsing source: Unknown error", .{ });
return error.FailedToParse;
}
return .{.instance = module};
}
pub const Module = struct {
instance: *cnzsl.nzslModule,
pub fn create() Module {
return .{.instance = cnzsl.nzslModuleCreate() orelse unreachable};
}
pub fn release(self: Module) void {
cnzsl.nzslModuleDestroy(self.instance);
}
@@ -76,10 +64,10 @@ pub const GlslWriter = struct {
cnzsl.nzslGlslWriterDestroy(self.instance);
}
pub fn generate(self: GlslWriter, module: Module) !GlslOutput {
pub fn generate(self: GlslWriter, module: Module, bindingMapping: GlslBindingMapping) !GlslOutput {
var output: ?*cnzsl.nzslGlslOutput = null;
output = cnzsl.nzslGlslWriterGenerate(self.instance, module.instance, null, null);
output = cnzsl.nzslGlslWriterGenerate(self.instance, module.instance, bindingMapping.instance, null);
if(output == null) {
std.log.err("Failed to generate glsl output: {s}", .{ self.getLastError() });
@@ -95,6 +83,23 @@ pub const GlslWriter = struct {
}
};
pub const GlslBindingMapping = struct {
instance: *cnzsl.nzslGlslBindingMapping,
pub fn create() GlslBindingMapping {
return .{.instance = cnzsl.nzslGlslBindingMappingCreate() orelse unreachable};
}
pub fn release(self: GlslBindingMapping) void {
cnzsl.nzslGlslBindingMappingDestroy(self.instance);
}
pub fn setBinding(self: GlslBindingMapping, setIndex: u32, bindingIndex: u32, glBinding: c_uint) void {
cnzsl.nzslGlslBindingMappingSetBinding(self.instance, setIndex, bindingIndex, glBinding);
}
};
pub const GlslOutput = struct {
instance: *cnzsl.nzslGlslOutput,
@@ -102,3 +107,51 @@ pub const GlslOutput = struct {
cnzsl.nzslGlslOutputDestroy(self.instance);
}
};
pub fn hashOption(str: [*c]const u8) OptionHash {
return cnzsl.nzslHashOption(str);
}
pub const WriterStates = struct {
instance: *cnzsl.nzslWriterStates,
pub fn create() WriterStates {
return .{.instance = cnzsl.nzslWriterStatesCreate() orelse unreachable};
}
pub fn enableOptimization(self: WriterStates, enable: bool) void {
cnzsl.nzslWriterStatesEnableOptimization(self.instance, enable);
}
pub fn enableSanitization(self: WriterStates, enable: bool) void {
cnzsl.nzslWriterStatesEnableSanitization(self.instance, enable);
}
pub fn setDebugLevel(self: WriterStates, debugLevel: DebugLevel) void {
cnzsl.nzslWriterStatesSetDebugLevel(self.instance, debugLevel);
}
pub fn setOption(self: WriterStates, optionHash: OptionHash, value: anytype) void {
switch (@TypeOf(value)) {
inline bool => cnzsl.nzslWriterStatesSetOption_bool(self.instance, optionHash, @intFromBool(value)),
inline cnzsl.nzslBool => cnzsl.nzslWriterStatesSetOption_bool(self.instance, optionHash, value),
inline [2]bool, [2]bool, [2]cnzsl.nzslBool => cnzsl.nzslWriterStatesSetOption_vec2bool(self.instance, optionHash, value.ptr),
inline [3]bool, [3]cnzsl.nzslBool => cnzsl.nzslWriterStatesSetOption_vec3bool(self.instance, optionHash, value.ptr),
inline [4]bool, [4]cnzsl.nzslBool => cnzsl.nzslWriterStatesSetOption_vec4bool(self.instance, optionHash, value.ptr),
inline comptime_float, f32 => cnzsl.nzslWriterStatesSetOption_f32(self.instance, optionHash, value),
inline [2]f32 => cnzsl.nzslWriterStatesSetOption_vec2f32(self.instance, optionHash, value.ptr),
inline [3]f32 => cnzsl.nzslWriterStatesSetOption_vec3f32(self.instance, optionHash, value.ptr),
inline [4]f32 => cnzsl.nzslWriterStatesSetOption_vec4f32(self.instance, optionHash, value.ptr),
inline comptime_int, i32 => cnzsl.nzslWriterStatesSetOption_i32(self.instance, optionHash, value),
inline [2]i32 => cnzsl.nzslWriterStatesSetOption_vec2i32(self.instance, optionHash, value.ptr),
inline [3]i32 => cnzsl.nzslWriterStatesSetOption_vec3i32(self.instance, optionHash, value.ptr),
inline [4]i32 => cnzsl.nzslWriterStatesSetOption_vec4i32(self.instance, optionHash, value.ptr),
inline u32 => cnzsl.nzslWriterStatesSetOption_u32(self.instance, optionHash, value),
inline [2]u32 => cnzsl.nzslWriterStatesSetOption_vec2u32(self.instance, optionHash, value.ptr),
inline [3]u32 => cnzsl.nzslWriterStatesSetOption_vec3u32(self.instance, optionHash, value.ptr),
inline [4]u32 => cnzsl.nzslWriterStatesSetOption_vec4u32(self.instance, optionHash, value.ptr),
else => std.debug.panic("Unsupported type {}", .{@TypeOf(value)})
}
}
pub fn release(self: WriterStates) void {
cnzsl.nzslWriterStatesDestroy(self.instance);
}
};

View File

@@ -1,3 +1,3 @@
pub usingnamespace @cImport({
@cInclude("CNZSL/CNZSL.h");
});
});