+ All Binding done
This commit is contained in:
@@ -92,7 +92,6 @@ pub fn build(b: *Build) void {
|
|||||||
run_step.dependOn(&run_cmd.step);
|
run_step.dependOn(&run_cmd.step);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
docs.root_module.addOptions("config", config);
|
docs.root_module.addOptions("config", config);
|
||||||
docs.root_module.addImport("nzslzig", nzslzig);
|
docs.root_module.addImport("nzslzig", nzslzig);
|
||||||
|
|
||||||
|
|||||||
@@ -16,8 +16,9 @@
|
|||||||
// internet connectivity.
|
// internet connectivity.
|
||||||
.dependencies = .{
|
.dependencies = .{
|
||||||
.nzsl_source = .{
|
.nzsl_source = .{
|
||||||
.url = "https://github.com/REMqb/ShaderLang/archive/2a939b32e1dfd318964a6f03d442c565d51815b9.tar.gz",
|
.url = "https://github.com/NazaraEngine/ShaderLang/archive/97dfc07923572cb26aa2db485df6bd837dea79bb.tar.gz",
|
||||||
.hash = "1220bb61ec2a696eeaac96d3d419bcbe91979706c14f76a4ca696c06e28f9b4f7fc0",
|
.hash = "12203c913b5cbccbb4b6b11d7e5730f81ed0737553fe3fc30995d059bf859101933f",
|
||||||
|
.lazy = true,
|
||||||
},
|
},
|
||||||
.NazaraUtils = .{
|
.NazaraUtils = .{
|
||||||
.url = "https://github.com/NazaraEngine/NazaraUtils/archive/cbdb331a9610fa52f51e7c86576cc9e2e7235691.tar.gz",
|
.url = "https://github.com/NazaraEngine/NazaraUtils/archive/cbdb331a9610fa52f51e7c86576cc9e2e7235691.tar.gz",
|
||||||
|
|||||||
@@ -11,17 +11,14 @@ pub fn main() !void {
|
|||||||
const state = nzsl.WriterStates.create();
|
const state = nzsl.WriterStates.create();
|
||||||
defer state.release();
|
defer state.release();
|
||||||
|
|
||||||
state.setOption(nzsl.hashOption("foo"), 42);
|
state.setDebugLevel(.full);
|
||||||
state.setOption(nzsl.hashOption("foo"), 42.3);
|
|
||||||
state.setOption(nzsl.hashOption("foo"), true);
|
|
||||||
state.setOption(nzsl.hashOption("foo"), .{true, false});
|
|
||||||
state.setOption(nzsl.hashOption("foo"), .{12, 24});
|
|
||||||
state.setOption(nzsl.hashOption("foo"), .{12.4, 24});
|
|
||||||
|
|
||||||
const mappings = nzsl.GlslBindingMapping.create();
|
const mappings = nzsl.GlslBindingMapping.create();
|
||||||
defer mappings.release();
|
defer mappings.release();
|
||||||
|
|
||||||
const output = try glslWriter.generate(mandelbrotModule, mappings);
|
const output = try glslWriter.generate(mandelbrotModule, mappings, state);
|
||||||
defer output.release();
|
defer output.release();
|
||||||
|
|
||||||
|
std.log.debug("Generated code: \n{s}", .{ output.getCode() });
|
||||||
|
|
||||||
}
|
}
|
||||||
107
src/GlslWriter.zig
git.filemode.normal_file
107
src/GlslWriter.zig
git.filemode.normal_file
@@ -0,0 +1,107 @@
|
|||||||
|
const cnzsl = @import("nzsl-c.zig");
|
||||||
|
const nzsl = @import("lib.zig");
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub const ShaderStageType = enum(cnzsl.nzslShaderStageType) {
|
||||||
|
compute = cnzsl.NZSL_STAGE_COMPUTE,
|
||||||
|
fragment = cnzsl.NZSL_STAGE_FRAGMENT,
|
||||||
|
vertex = cnzsl.NZSL_STAGE_VERTEX,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const GlslWriter = struct {
|
||||||
|
instance: *cnzsl.nzslGlslWriter,
|
||||||
|
|
||||||
|
pub fn create() GlslWriter {
|
||||||
|
return .{.instance = cnzsl.nzslGlslWriterCreate() orelse unreachable};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn release(self: GlslWriter) void {
|
||||||
|
cnzsl.nzslGlslWriterDestroy(self.instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate(self: GlslWriter, module: nzsl.Module, bindingMapping: GlslBindingMapping, states: nzsl.WriterStates) !GlslOutput {
|
||||||
|
var output: ?*cnzsl.nzslGlslOutput = null;
|
||||||
|
|
||||||
|
output = cnzsl.nzslGlslWriterGenerate(self.instance, module.instance, bindingMapping.instance, states.instance);
|
||||||
|
|
||||||
|
if(output == null) {
|
||||||
|
std.log.err("Failed to generate glsl output: {s}", .{ self.getLastError() });
|
||||||
|
|
||||||
|
return error.FailedToGenerateGlsl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return .{.instance = output orelse unreachable};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generateStage(self: GlslWriter, stage: ShaderStageType, module: nzsl.Module, bindingMapping: GlslBindingMapping, states: nzsl.WriterStates) !GlslOutput {
|
||||||
|
var output: ?*cnzsl.nzslGlslOutput = null;
|
||||||
|
|
||||||
|
output = cnzsl.nzslGlslWriterGenerateStage(@intFromEnum(stage), self.instance, module.instance, bindingMapping.instance, states.instance);
|
||||||
|
|
||||||
|
if(output == null) {
|
||||||
|
std.log.err("Failed to generate glsl output: {s}", .{ self.getLastError() });
|
||||||
|
|
||||||
|
return error.FailedToGenerateGlsl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return .{.instance = output orelse unreachable};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getLastError(self: GlslWriter) [*c]const u8 {
|
||||||
|
return cnzsl.nzslGlslWriterGetLastError(self.instance);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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,
|
||||||
|
|
||||||
|
pub fn release(self: GlslOutput) void {
|
||||||
|
cnzsl.nzslGlslOutputDestroy(self.instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getCode(self: GlslOutput) []const u8 {
|
||||||
|
var size: usize = undefined;
|
||||||
|
const code = cnzsl.nzslGlslOutputGetCode(self.instance, &size);
|
||||||
|
|
||||||
|
return code[0..size];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return texture binding in output or -1 if binding doesn't exists
|
||||||
|
pub fn getExplicitTextureBinding(self: GlslOutput, bindingName: [*c]const u8) c_int {
|
||||||
|
return cnzsl.nzslGlslOutputGetExplicitTextureBinding(self.instance, bindingName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return uniform binding in output or -1 if binding doesn't exists
|
||||||
|
pub fn getExplicitUniformBlockBinding(self: GlslOutput, bindingName: [*c]const u8) c_int {
|
||||||
|
return cnzsl.nzslGlslOutputGetExplicitUniformBlockBinding(self.instance, bindingName);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getUsesDrawParameterBaseInstanceUniform(self: GlslOutput) c_int {
|
||||||
|
return cnzsl.nzslGlslOutputGetUsesDrawParameterBaseInstanceUniform(self.instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getUsesDrawParameterBaseVertexUniform(self: GlslOutput) c_int {
|
||||||
|
return cnzsl.nzslGlslOutputGetUsesDrawParameterBaseVertexUniform(self.instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getUsesDrawParameterDrawIndexUniform(self: GlslOutput) c_int {
|
||||||
|
return cnzsl.nzslGlslOutputGetUsesDrawParameterDrawIndexUniform(self.instance);
|
||||||
|
}
|
||||||
|
};
|
||||||
49
src/LangWriter.zig
git.filemode.normal_file
49
src/LangWriter.zig
git.filemode.normal_file
@@ -0,0 +1,49 @@
|
|||||||
|
const cnzsl = @import("nzsl-c.zig");
|
||||||
|
const nzsl = @import("lib.zig");
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub const LangWriter = struct {
|
||||||
|
instance: *cnzsl.nzslLangWriter,
|
||||||
|
|
||||||
|
pub fn create() LangWriter {
|
||||||
|
return .{.instance = cnzsl.nzslLangWriterCreate() orelse unreachable};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn release(self: LangWriter) void {
|
||||||
|
cnzsl.nzslLangWriterDestroy(self.instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getLastError(self: LangWriter) [*c]const u8 {
|
||||||
|
return cnzsl.nzslLangWriterGetLastError(self.instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate(self: LangWriter, module: nzsl.Module, states: nzsl.WriterStates) LangOutput {
|
||||||
|
var output: ?*cnzsl.nzslLangOutput = null;
|
||||||
|
|
||||||
|
output = cnzsl.nzslLangWriterGenerate(self.instance, module.instance, states.instance);
|
||||||
|
|
||||||
|
if(output == null) {
|
||||||
|
std.log.err("Failed to generate lang output: {s}", .{ self.getLastError() });
|
||||||
|
|
||||||
|
return error.FailedToGenerateLang;
|
||||||
|
}
|
||||||
|
|
||||||
|
return .{.instance = output orelse unreachable};
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const LangOutput = struct {
|
||||||
|
instance: *cnzsl.nzslLangOutput,
|
||||||
|
|
||||||
|
pub fn release(self: LangOutput) void {
|
||||||
|
cnzsl.nzslLangOutputDestroy(self.instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getCode(self: LangOutput) []const u8 {
|
||||||
|
var size: usize = undefined;
|
||||||
|
const code = cnzsl.nzslLangOutputGetCode(self.instance, &size);
|
||||||
|
|
||||||
|
return code[0..size];
|
||||||
|
}
|
||||||
|
};
|
||||||
17
src/Module.zig
git.filemode.normal_file
17
src/Module.zig
git.filemode.normal_file
@@ -0,0 +1,17 @@
|
|||||||
|
const cnzsl = @import("nzsl-c.zig");
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getLastError(self: Module) [*c]const u8 {
|
||||||
|
return cnzsl.nzslModuleGetLastError(self.instance);
|
||||||
|
}
|
||||||
|
};
|
||||||
44
src/Parser.zig
git.filemode.normal_file
44
src/Parser.zig
git.filemode.normal_file
@@ -0,0 +1,44 @@
|
|||||||
|
const cnzsl = @import("nzsl-c.zig");
|
||||||
|
const nzsl = @import("lib.zig");
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub fn parseSource(source: []const u8) !nzsl.Module {
|
||||||
|
return parseSourceWithFilePath(source, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse a NZSL source code and stores it inside a nzslModule
|
||||||
|
/// In case of failure, a negative value is returned and an error code is set
|
||||||
|
///
|
||||||
|
/// @param module pointer to a
|
||||||
|
/// @param source pointer to NZSL source
|
||||||
|
/// @param sourceLen length of source in characters
|
||||||
|
/// @param filePath used when reporting errors
|
||||||
|
/// @param filePathLen length of filePath in characters
|
||||||
|
/// @return 0 if parsing succeeded and a negative value in case of failure
|
||||||
|
///
|
||||||
|
/// @see nzslModuleGetLastError
|
||||||
|
pub fn parseSourceWithFilePath(source: []const u8, filePath: []const u8) !nzsl.Module {
|
||||||
|
const module = nzsl.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) !nzsl.Module {
|
||||||
|
const module = nzsl.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() });
|
||||||
|
|
||||||
|
return error.FailedToParse;
|
||||||
|
}
|
||||||
|
|
||||||
|
return module;
|
||||||
|
}
|
||||||
55
src/SpirvWriter.zig
git.filemode.normal_file
55
src/SpirvWriter.zig
git.filemode.normal_file
@@ -0,0 +1,55 @@
|
|||||||
|
const cnzsl = @import("nzsl-c.zig");
|
||||||
|
const nzsl = @import("lib.zig");
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub const SpirvWriterEnvironment = cnzsl.nzslSpirvWriterEnvironment;
|
||||||
|
|
||||||
|
pub const SpirvWriter = struct {
|
||||||
|
instance: *cnzsl.nzslSpirvWriter,
|
||||||
|
|
||||||
|
pub fn create() SpirvWriter {
|
||||||
|
return .{.instance = cnzsl.nzslSpirvWriterCreate() orelse unreachable};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn release(self: SpirvWriter) void {
|
||||||
|
cnzsl.nzslSpirvWriterDestroy(self.instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getLastError(self: SpirvWriter) [*c]const u8 {
|
||||||
|
return cnzsl.nzslSpirvWriterGetLastError(self.instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn setEnv(self: SpirvWriter, env: SpirvWriterEnvironment) void {
|
||||||
|
return cnzsl.nzslSpirvWriterSetEnv(self.instance, env);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate(self: SpirvWriter, module: nzsl.Module, states: nzsl.WriterStates) SpirvOutput {
|
||||||
|
var output: ?*cnzsl.nzslSpirvOutput = null;
|
||||||
|
|
||||||
|
output = cnzsl.nzslSpirvWriterGenerate(self.instance, module.instance, states.instance);
|
||||||
|
|
||||||
|
if(output == null) {
|
||||||
|
std.log.err("Failed to generate spirv output: {s}", .{ self.getLastError() });
|
||||||
|
|
||||||
|
return error.FailedToGenerateSpirv;
|
||||||
|
}
|
||||||
|
|
||||||
|
return .{.instance = output orelse unreachable};
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const SpirvOutput = struct {
|
||||||
|
instance: *cnzsl.nzslSpirvOutput,
|
||||||
|
|
||||||
|
pub fn release(self: SpirvOutput) void {
|
||||||
|
cnzsl.nzslSpirvOutputDestroy(self.instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getCode(self: SpirvOutput) []const u32 {
|
||||||
|
var size: usize = undefined;
|
||||||
|
const code = cnzsl.nzslSpirvOutputGetSpirv(self.instance, &size);
|
||||||
|
|
||||||
|
return code[0..size];
|
||||||
|
}
|
||||||
|
};
|
||||||
93
src/WriterStates.zig
git.filemode.normal_file
93
src/WriterStates.zig
git.filemode.normal_file
@@ -0,0 +1,93 @@
|
|||||||
|
const cnzsl = @import("nzsl-c.zig");
|
||||||
|
const nzsl = @import("lib.zig");
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub fn vec(comptime T: type, comptime dim: usize) type {
|
||||||
|
return [dim]T;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const vec2bool = vec(bool, 2);
|
||||||
|
pub const vec2nzslBool = vec(cnzsl.nzslBool, 2);
|
||||||
|
pub const vec3bool = vec(bool, 3);
|
||||||
|
pub const vec3nzslBool = vec(cnzsl.nzslBool, 3);
|
||||||
|
pub const vec4bool = vec(bool, 4);
|
||||||
|
pub const vec4nzslBool = vec(cnzsl.nzslBool, 4);
|
||||||
|
pub const vec2f32 = vec(f32, 2);
|
||||||
|
pub const vec3f32 = vec(f32, 3);
|
||||||
|
pub const vec4f32 = vec(f32, 4);
|
||||||
|
pub const vec2u32 = vec(u32, 2);
|
||||||
|
pub const vec3u32 = vec(u32, 3);
|
||||||
|
pub const vec4u32 = vec(u32, 4);
|
||||||
|
pub const vec2i32 = vec(i32, 2);
|
||||||
|
pub const vec3i32 = vec(i32, 3);
|
||||||
|
pub const vec4i32 = vec(i32, 4);
|
||||||
|
|
||||||
|
pub const DebugLevel = enum(cnzsl.nzslDebugLevel) {
|
||||||
|
none = cnzsl.NZSL_DEBUG_NONE,
|
||||||
|
full = cnzsl.NZSL_DEBUG_FULL,
|
||||||
|
minimal = cnzsl.NZSL_DEBUG_MINIMAL,
|
||||||
|
regular = cnzsl.NZSL_DEBUG_REGULAR,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const OptionHash = cnzsl.nzslOptionHash;
|
||||||
|
|
||||||
|
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, @intFromEnum(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 vec2bool => {
|
||||||
|
self.setOption(optionHash, vec2nzslBool{@intFromBool(value[0]), @intFromBool(value[1])});
|
||||||
|
},
|
||||||
|
inline vec2nzslBool => cnzsl.nzslWriterStatesSetOption_vec2bool(self.instance, optionHash, &value),
|
||||||
|
inline vec3bool => {
|
||||||
|
self.setOption(optionHash, vec3nzslBool{@intFromBool(value[0]), @intFromBool(value[1]), @intFromBool(value[2])});
|
||||||
|
},
|
||||||
|
inline vec3nzslBool => cnzsl.nzslWriterStatesSetOption_vec3bool(self.instance, optionHash, &value),
|
||||||
|
inline vec4bool => {
|
||||||
|
self.setOption(optionHash, vec4nzslBool{@intFromBool(value[0]), @intFromBool(value[1]), @intFromBool(value[2]), @intFromBool(value[3])});
|
||||||
|
},
|
||||||
|
inline vec4nzslBool => cnzsl.nzslWriterStatesSetOption_vec4bool(self.instance, optionHash, &value),
|
||||||
|
inline comptime_float, f32 => cnzsl.nzslWriterStatesSetOption_f32(self.instance, optionHash, value),
|
||||||
|
inline vec2f32 => cnzsl.nzslWriterStatesSetOption_vec2f32(self.instance, optionHash, &value),
|
||||||
|
inline vec3f32 => cnzsl.nzslWriterStatesSetOption_vec3f32(self.instance, optionHash, &value),
|
||||||
|
inline vec4f32 => cnzsl.nzslWriterStatesSetOption_vec4f32(self.instance, optionHash, &value),
|
||||||
|
inline i32 => cnzsl.nzslWriterStatesSetOption_i32(self.instance, optionHash, value),
|
||||||
|
inline comptime_int => {
|
||||||
|
std.log.debug("/!\\ setOption with comptime_int defaults to i32 with value {} ", .{ value });
|
||||||
|
cnzsl.nzslWriterStatesSetOption_i32(self.instance, optionHash, value);
|
||||||
|
},
|
||||||
|
inline vec2i32 => cnzsl.nzslWriterStatesSetOption_vec2i32(self.instance, optionHash, &value),
|
||||||
|
inline vec3i32 => cnzsl.nzslWriterStatesSetOption_vec3i32(self.instance, optionHash, &value),
|
||||||
|
inline vec4i32 => cnzsl.nzslWriterStatesSetOption_vec4i32(self.instance, optionHash, &value),
|
||||||
|
inline u32 => cnzsl.nzslWriterStatesSetOption_u32(self.instance, optionHash, value),
|
||||||
|
inline vec2u32 => cnzsl.nzslWriterStatesSetOption_vec2u32(self.instance, optionHash, &value),
|
||||||
|
inline vec3u32 => cnzsl.nzslWriterStatesSetOption_vec3u32(self.instance, optionHash, &value),
|
||||||
|
inline vec4u32 => cnzsl.nzslWriterStatesSetOption_vec4u32(self.instance, optionHash, &value),
|
||||||
|
else => std.debug.panic("Unsupported type {}", .{@TypeOf(value)})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn release(self: WriterStates) void {
|
||||||
|
cnzsl.nzslWriterStatesDestroy(self.instance);
|
||||||
|
}
|
||||||
|
};
|
||||||
156
src/lib.zig
156
src/lib.zig
@@ -4,154 +4,8 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const cnzsl = @import("nzsl-c.zig");
|
const cnzsl = @import("nzsl-c.zig");
|
||||||
|
|
||||||
pub const DebugLevel = cnzsl.nzslDebugLevel;
|
pub usingnamespace @import("WriterStates.zig");
|
||||||
pub const OptionHash = cnzsl.nzslOptionHash;
|
pub usingnamespace @import("GlslWriter.zig");
|
||||||
|
pub usingnamespace @import("SpirvWriter.zig");
|
||||||
pub fn parseSource(source: []const u8) !Module {
|
pub usingnamespace @import("Module.zig");
|
||||||
return parseSourceWithFilePath(source, "");
|
pub usingnamespace @import("Parser.zig");
|
||||||
}
|
|
||||||
|
|
||||||
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() });
|
|
||||||
|
|
||||||
return error.FailedToParse;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn getLastError(self: Module) [*c]const u8 {
|
|
||||||
return cnzsl.nzslModuleGetLastError(self.instance);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const GlslWriter = struct {
|
|
||||||
instance: *cnzsl.nzslGlslWriter,
|
|
||||||
|
|
||||||
pub fn create() GlslWriter {
|
|
||||||
return .{.instance = cnzsl.nzslGlslWriterCreate() orelse unreachable};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn release(self: GlslWriter) void {
|
|
||||||
cnzsl.nzslGlslWriterDestroy(self.instance);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn generate(self: GlslWriter, module: Module, bindingMapping: GlslBindingMapping) !GlslOutput {
|
|
||||||
var output: ?*cnzsl.nzslGlslOutput = 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() });
|
|
||||||
|
|
||||||
return error.FailedToGenerateGlsl;
|
|
||||||
}
|
|
||||||
|
|
||||||
return .{.instance = output orelse unreachable};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn getLastError(self: GlslWriter) [*c]const u8 {
|
|
||||||
return cnzsl.nzslGlslWriterGetLastError(self.instance);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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,
|
|
||||||
|
|
||||||
pub fn release(self: GlslOutput) void {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|||||||
24
src/main.zig
24
src/main.zig
@@ -1,24 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
|
|
||||||
pub fn main() !void {
|
|
||||||
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
|
|
||||||
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
|
||||||
|
|
||||||
// stdout is for the actual output of your application, for example if you
|
|
||||||
// are implementing gzip, then only the compressed bytes should be sent to
|
|
||||||
// stdout, not any debugging messages.
|
|
||||||
const stdout_file = std.io.getStdOut().writer();
|
|
||||||
var bw = std.io.bufferedWriter(stdout_file);
|
|
||||||
const stdout = bw.writer();
|
|
||||||
|
|
||||||
try stdout.print("Run `zig build test` to run the tests.\n", .{});
|
|
||||||
|
|
||||||
try bw.flush(); // don't forget to flush!
|
|
||||||
}
|
|
||||||
|
|
||||||
test "simple test" {
|
|
||||||
var list = std.ArrayList(i32).init(std.testing.allocator);
|
|
||||||
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
|
|
||||||
try list.append(42);
|
|
||||||
try std.testing.expectEqual(@as(i32, 42), list.pop());
|
|
||||||
}
|
|
||||||
10
src/root.zig
10
src/root.zig
@@ -1,10 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
const testing = std.testing;
|
|
||||||
|
|
||||||
export fn add(a: i32, b: i32) i32 {
|
|
||||||
return a + b;
|
|
||||||
}
|
|
||||||
|
|
||||||
test "basic add functionality" {
|
|
||||||
try testing.expect(add(3, 7) == 10);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user