big rework towards Zig 0.15
This commit is contained in:
81
src/BackendParameters.zig
git.filemode.normal_file
81
src/BackendParameters.zig
git.filemode.normal_file
@@ -0,0 +1,81 @@
|
||||
const std = @import("std");
|
||||
const DebugLevel = @import("lib.zig").DebugLevel;
|
||||
const OptionHash = @import("lib.zig").OptionHash;
|
||||
const cnzsl = @cImport({
|
||||
@cInclude("CNZSL/CNZSL.h");
|
||||
});
|
||||
|
||||
const Self = @This();
|
||||
|
||||
instance: *cnzsl.nzslBackendParameters,
|
||||
|
||||
pub fn init() !Self {
|
||||
const cparameters = cnzsl.nzslBackendParametersCreate() orelse return error.NullPointer;
|
||||
return .{
|
||||
.instance = cparameters,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: Self) void {
|
||||
cnzsl.nzslBackendParametersDestroy(self.instance);
|
||||
}
|
||||
|
||||
pub fn enableDeadCodeRemoval(self: Self, enable: bool) void {
|
||||
cnzsl.nzslBackendParametersEnableDeadCodeRemoval(self.instance, enable);
|
||||
}
|
||||
|
||||
pub fn enableOptimization(self: Self, enable: bool) void {
|
||||
cnzsl.nzslBackendParametersEnableOptimization(self.instance, enable);
|
||||
}
|
||||
|
||||
pub fn enableResolving(self: Self, enable: bool) void {
|
||||
cnzsl.nzslBackendParametersEnableResolving(self.instance, enable);
|
||||
}
|
||||
|
||||
pub fn enableTargetRequired(self: Self, enable: bool) void {
|
||||
cnzsl.nzslBackendParametersEnableTargetRequired(self.instance, enable);
|
||||
}
|
||||
|
||||
pub fn enableValidation(self: Self, enable: bool) void {
|
||||
cnzsl.nzslBackendParametersEnableValidation(self.instance, 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 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}),
|
||||
.array => |a| {
|
||||
const sanitized_value, const sanitized_primitive_name = switch (@typeInfo(a.child)) {
|
||||
.bool => blk: {
|
||||
var new_array: [a.len]c_int = undefined;
|
||||
for (value, 0..a.len) |v, i| {
|
||||
new_array[i] = @intFromBool(v);
|
||||
}
|
||||
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) },
|
||||
.comptime_float => .{ value, "f32" },
|
||||
.comptime_int => .{ value, "i32" },
|
||||
else => @compileError("Unhandled type"),
|
||||
};
|
||||
|
||||
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"),
|
||||
};
|
||||
|
||||
@call(.auto, @field(cnzsl, "nzslBackendParametersSetOption_" ++ type_name), .{ self.instance, hash, c_value });
|
||||
},
|
||||
else => @compileError("Unhandled type"),
|
||||
}
|
||||
}
|
||||
@@ -1,107 +1,104 @@
|
||||
const cnzsl = @import("nzsl-c.zig");
|
||||
const nzsl = @import("lib.zig");
|
||||
const std = @import("std");
|
||||
const Module = @import("Module.zig");
|
||||
const BackendParameters = @import("BackendParameters.zig");
|
||||
const ShaderStageType = @import("lib.zig").ShaderStageType;
|
||||
const cnzsl = @cImport({
|
||||
@cInclude("CNZSL/CNZSL.h");
|
||||
});
|
||||
|
||||
pub const ShaderStageType = enum(cnzsl.nzslShaderStageType) {
|
||||
compute = cnzsl.NZSL_STAGE_COMPUTE,
|
||||
fragment = cnzsl.NZSL_STAGE_FRAGMENT,
|
||||
vertex = cnzsl.NZSL_STAGE_VERTEX,
|
||||
};
|
||||
const Self = @This();
|
||||
|
||||
pub const GlslWriter = struct {
|
||||
instance: *cnzsl.nzslGlslWriter,
|
||||
instance: *cnzsl.nzslGlslWriter,
|
||||
|
||||
pub fn create() GlslWriter {
|
||||
return .{.instance = cnzsl.nzslGlslWriterCreate() orelse unreachable};
|
||||
pub fn init() !Self {
|
||||
const cwriter = cnzsl.nzslGlslWriterCreate() orelse return error.NullPointer;
|
||||
return .{
|
||||
.instance = cwriter,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: Self) void {
|
||||
cnzsl.nzslGlslWriterDestroy(self.instance);
|
||||
}
|
||||
|
||||
pub fn generate(self: Self, module: Module, backend_parameters: BackendParameters, parameters: Parameters) !Output {
|
||||
const output = cnzsl.nzslGlslWriterGenerate(self.instance, @ptrCast(module.instance), @ptrCast(backend_parameters.instance), @ptrCast(parameters.instance)) orelse return error.FailedToGenerateGlsl;
|
||||
return .{
|
||||
.instance = output,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn generateStage(self: Self, stage: ShaderStageType, module: Module, backend_parameters: BackendParameters, parameters: Parameters) !Output {
|
||||
const output = cnzsl.nzslGlslWriterGenerateStage(self.instance, @intFromEnum(stage), @ptrCast(module.instance), @ptrCast(backend_parameters.instance), @ptrCast(parameters.instance)) orelse return error.FailedToGenerateGlsl;
|
||||
return .{
|
||||
.instance = output,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getLastError(self: Self) ![]const u8 {
|
||||
const err = cnzsl.nzslGlslWriterGetLastError(self.instance) orelse return error.NullPointer;
|
||||
return std.mem.span(err);
|
||||
}
|
||||
|
||||
pub const Parameters = struct {
|
||||
const InnerSelf = @This();
|
||||
|
||||
instance: *cnzsl.nzslGlslWriterParameters,
|
||||
|
||||
pub fn init() !InnerSelf {
|
||||
const cparameters = cnzsl.nzslGlslWriterParametersCreate() orelse return error.NullPointer;
|
||||
return .{
|
||||
.instance = cparameters,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn release(self: GlslWriter) void {
|
||||
cnzsl.nzslGlslWriterDestroy(self.instance);
|
||||
pub fn deinit(self: InnerSelf) void {
|
||||
cnzsl.nzslGlslWriterParametersDestroy(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 setBindingMapping(self: InnerSelf, set_index: u32, binding_index: u32, gl_binding: u32) void {
|
||||
cnzsl.nzslGlslWriterParametersSetBindingMapping(self.instance, set_index, binding_index, @intCast(gl_binding));
|
||||
}
|
||||
|
||||
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 fn setPushConstantBinding(self: InnerSelf, gl_binding: u32) void {
|
||||
cnzsl.nzslGlslWriterParametersSetPushConstantBinding(self.instance, @intCast(gl_binding));
|
||||
}
|
||||
};
|
||||
|
||||
pub const GlslBindingMapping = struct {
|
||||
instance: *cnzsl.nzslGlslBindingMapping,
|
||||
pub const Output = struct {
|
||||
const InnerSelf = @This();
|
||||
|
||||
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 {
|
||||
pub fn deinit(self: InnerSelf) void {
|
||||
cnzsl.nzslGlslOutputDestroy(self.instance);
|
||||
}
|
||||
|
||||
pub fn getCode(self: GlslOutput) []const u8 {
|
||||
pub fn getCode(self: InnerSelf) []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);
|
||||
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;
|
||||
}
|
||||
|
||||
/// 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 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;
|
||||
}
|
||||
|
||||
pub fn getUsesDrawParameterBaseInstanceUniform(self: GlslOutput) c_int {
|
||||
pub fn usesDrawParameterBaseInstanceUniform(self: InnerSelf) bool {
|
||||
return cnzsl.nzslGlslOutputGetUsesDrawParameterBaseInstanceUniform(self.instance);
|
||||
}
|
||||
|
||||
pub fn getUsesDrawParameterBaseVertexUniform(self: GlslOutput) c_int {
|
||||
pub fn usesDrawParameterBaseVertexUniform(self: InnerSelf) bool {
|
||||
return cnzsl.nzslGlslOutputGetUsesDrawParameterBaseVertexUniform(self.instance);
|
||||
}
|
||||
|
||||
pub fn getUsesDrawParameterDrawIndexUniform(self: GlslOutput) c_int {
|
||||
pub fn usesDrawParameterDrawIndexUniform(self: InnerSelf) bool {
|
||||
return cnzsl.nzslGlslOutputGetUsesDrawParameterDrawIndexUniform(self.instance);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,49 +1,48 @@
|
||||
const cnzsl = @import("nzsl-c.zig");
|
||||
const nzsl = @import("lib.zig");
|
||||
const std = @import("std");
|
||||
const Module = @import("Module.zig");
|
||||
const cnzsl = @cImport({
|
||||
@cInclude("CNZSL/CNZSL.h");
|
||||
});
|
||||
|
||||
pub const LangWriter = struct {
|
||||
instance: *cnzsl.nzslLangWriter,
|
||||
const Self = @This();
|
||||
|
||||
pub fn create() LangWriter {
|
||||
return .{.instance = cnzsl.nzslLangWriterCreate() orelse unreachable};
|
||||
}
|
||||
instance: *cnzsl.nzslLangWriter,
|
||||
|
||||
pub fn release(self: LangWriter) void {
|
||||
cnzsl.nzslLangWriterDestroy(self.instance);
|
||||
}
|
||||
pub fn init() !Self {
|
||||
const cwriter = cnzsl.nzslLangWriterCreate() orelse return error.NullPointer;
|
||||
return .{
|
||||
.instance = cwriter,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getLastError(self: LangWriter) [*c]const u8 {
|
||||
return cnzsl.nzslLangWriterGetLastError(self.instance);
|
||||
}
|
||||
pub fn deinit(self: Self) void {
|
||||
cnzsl.nzslLangWriterDestroy(self.instance);
|
||||
}
|
||||
|
||||
pub fn generate(self: LangWriter, module: nzsl.Module, states: nzsl.WriterStates) LangOutput {
|
||||
var output: ?*cnzsl.nzslLangOutput = null;
|
||||
pub fn getLastError(self: Self) ![]const u8 {
|
||||
const err = cnzsl.nzslLangWriterGetLastError(self.instance) orelse return error.NullPointer;
|
||||
return std.mem.span(err);
|
||||
}
|
||||
|
||||
output = cnzsl.nzslLangWriterGenerate(self.instance, module.instance, states.instance);
|
||||
pub fn generate(self: Self, module: Module) !Output {
|
||||
const output = cnzsl.nzslLangWriterGenerate(self.instance, @ptrCast(module.instance)) orelse return error.FailedToGenerateLang;
|
||||
return .{
|
||||
.instance = output,
|
||||
};
|
||||
}
|
||||
|
||||
if(output == null) {
|
||||
std.log.err("Failed to generate lang output: {s}", .{ self.getLastError() });
|
||||
pub const Output = struct {
|
||||
const InnerSelf = @This();
|
||||
|
||||
return error.FailedToGenerateLang;
|
||||
}
|
||||
|
||||
return .{.instance = output orelse unreachable};
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
pub const LangOutput = struct {
|
||||
instance: *cnzsl.nzslLangOutput,
|
||||
|
||||
pub fn release(self: LangOutput) void {
|
||||
pub fn deinit(self: InnerSelf) void {
|
||||
cnzsl.nzslLangOutputDestroy(self.instance);
|
||||
}
|
||||
|
||||
pub fn getCode(self: LangOutput) []const u8 {
|
||||
pub fn getCode(self: InnerSelf) []const u8 {
|
||||
var size: usize = undefined;
|
||||
const code = cnzsl.nzslLangOutputGetCode(self.instance, &size);
|
||||
|
||||
return code[0..size];
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,17 +1,24 @@
|
||||
const cnzsl = @import("nzsl-c.zig");
|
||||
const std = @import("std");
|
||||
const cnzsl = @cImport({
|
||||
@cInclude("CNZSL/CNZSL.h");
|
||||
});
|
||||
|
||||
pub const Module = struct {
|
||||
instance: *cnzsl.nzslModule,
|
||||
const Self = @This();
|
||||
|
||||
pub fn create() Module {
|
||||
return .{.instance = cnzsl.nzslModuleCreate() orelse unreachable};
|
||||
}
|
||||
instance: *cnzsl.nzslModule,
|
||||
|
||||
pub fn release(self: Module) void {
|
||||
cnzsl.nzslModuleDestroy(self.instance);
|
||||
}
|
||||
pub fn init() !Self {
|
||||
const cmodule = cnzsl.nzslModuleCreate() orelse return error.NullPointer;
|
||||
return .{
|
||||
.instance = cmodule,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getLastError(self: Module) [*c]const u8 {
|
||||
return cnzsl.nzslModuleGetLastError(self.instance);
|
||||
}
|
||||
};
|
||||
pub fn deinit(self: Self) void {
|
||||
cnzsl.nzslModuleDestroy(@ptrCast(self.instance));
|
||||
}
|
||||
|
||||
pub fn getLastError(self: Self) ![]const u8 {
|
||||
const err = cnzsl.nzslModuleGetLastError(@ptrCast(self.instance)) orelse return error.NullPointer;
|
||||
return std.mem.span(err);
|
||||
}
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
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;
|
||||
}
|
||||
@@ -1,55 +1,65 @@
|
||||
const cnzsl = @import("nzsl-c.zig");
|
||||
const nzsl = @import("lib.zig");
|
||||
const std = @import("std");
|
||||
const Module = @import("Module.zig");
|
||||
const BackendParameters = @import("BackendParameters.zig");
|
||||
const cnzsl = @cImport({
|
||||
@cInclude("CNZSL/CNZSL.h");
|
||||
});
|
||||
|
||||
pub const SpirvWriterEnvironment = cnzsl.nzslSpirvWriterEnvironment;
|
||||
const Self = @This();
|
||||
|
||||
pub const SpirvWriter = struct {
|
||||
instance: *cnzsl.nzslSpirvWriter,
|
||||
instance: *cnzsl.nzslSpirvWriter,
|
||||
|
||||
pub fn create() SpirvWriter {
|
||||
return .{.instance = cnzsl.nzslSpirvWriterCreate() orelse unreachable};
|
||||
}
|
||||
pub fn init() !Self {
|
||||
const cwriter = cnzsl.nzslSpirvWriterCreate() orelse return error.NullPointer;
|
||||
return .{
|
||||
.instance = cwriter,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn release(self: SpirvWriter) void {
|
||||
cnzsl.nzslSpirvWriterDestroy(self.instance);
|
||||
}
|
||||
pub fn deinit(self: Self) void {
|
||||
cnzsl.nzslSpirvWriterDestroy(self.instance);
|
||||
}
|
||||
|
||||
pub fn getLastError(self: SpirvWriter) [*c]const u8 {
|
||||
return cnzsl.nzslSpirvWriterGetLastError(self.instance);
|
||||
}
|
||||
pub fn getLastError(self: Self) ![]const u8 {
|
||||
const err = cnzsl.nzslSpirvWriterGetLastError(self.instance) orelse return error.NullPointer;
|
||||
return std.mem.span(err);
|
||||
}
|
||||
|
||||
pub fn setEnv(self: SpirvWriter, env: SpirvWriterEnvironment) void {
|
||||
return cnzsl.nzslSpirvWriterSetEnv(self.instance, env);
|
||||
}
|
||||
pub fn setEnv(self: Self, env: Environment) void {
|
||||
const SpirvEnv = extern struct {
|
||||
spv_major_version: u32,
|
||||
spv_minor_version: u32,
|
||||
};
|
||||
const cenv: SpirvEnv = .{
|
||||
.spv_major_version = @intCast(env.spv_version.major),
|
||||
.spv_minor_version = @intCast(env.spv_version.minor),
|
||||
};
|
||||
return cnzsl.nzslSpirvWriterSetEnv(self.instance, @ptrCast(&cenv));
|
||||
}
|
||||
|
||||
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 fn generate(self: Self, module: Module, backend_parameters: BackendParameters) !Output {
|
||||
const output = cnzsl.nzslSpirvWriterGenerate(self.instance, @ptrCast(module.instance), @ptrCast(backend_parameters.instance)) orelse return error.FailedToGenerateSpirv;
|
||||
return .{
|
||||
.instance = output,
|
||||
};
|
||||
}
|
||||
|
||||
pub const Environment = struct {
|
||||
spv_version: std.SemanticVersion,
|
||||
};
|
||||
|
||||
pub const SpirvOutput = struct {
|
||||
pub const Output = struct {
|
||||
const InnerSelf = @This();
|
||||
|
||||
instance: *cnzsl.nzslSpirvOutput,
|
||||
|
||||
pub fn release(self: SpirvOutput) void {
|
||||
pub fn deinit(self: InnerSelf) void {
|
||||
cnzsl.nzslSpirvOutputDestroy(self.instance);
|
||||
}
|
||||
|
||||
pub fn getCode(self: SpirvOutput) []const u32 {
|
||||
pub fn getCode(self: InnerSelf) []const u32 {
|
||||
var size: usize = undefined;
|
||||
const code = cnzsl.nzslSpirvOutputGetSpirv(self.instance, &size);
|
||||
|
||||
return code[0..size];
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,93 +0,0 @@
|
||||
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);
|
||||
}
|
||||
};
|
||||
37
src/lib.zig
37
src/lib.zig
@@ -1,11 +1,30 @@
|
||||
// const cnzsl = @cImport({
|
||||
// @cInclude("CNZSL/CNZSL.h");
|
||||
// });
|
||||
const std = @import("std");
|
||||
const cnzsl = @import("nzsl-c.zig");
|
||||
const cnzsl = @cImport({
|
||||
@cInclude("CNZSL/CNZSL.h");
|
||||
});
|
||||
|
||||
pub usingnamespace @import("WriterStates.zig");
|
||||
pub usingnamespace @import("GlslWriter.zig");
|
||||
pub usingnamespace @import("SpirvWriter.zig");
|
||||
pub usingnamespace @import("Module.zig");
|
||||
pub usingnamespace @import("Parser.zig");
|
||||
pub const ShaderStageType = enum(cnzsl.nzslShaderStageType) {
|
||||
compute = cnzsl.NZSL_STAGE_COMPUTE,
|
||||
fragment = cnzsl.NZSL_STAGE_FRAGMENT,
|
||||
vertex = cnzsl.NZSL_STAGE_VERTEX,
|
||||
};
|
||||
|
||||
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 = u32;
|
||||
|
||||
pub fn hashOption(str: [:0]const u8) !OptionHash {
|
||||
return cnzsl.nzslHashOption(str);
|
||||
}
|
||||
|
||||
pub const BackendParameters = @import("BackendParameters.zig");
|
||||
pub const GlslWriter = @import("GlslWriter.zig");
|
||||
pub const LangWriter = @import("LangWriter.zig");
|
||||
pub const Module = @import("Module.zig");
|
||||
pub const parser = @import("parser.zig");
|
||||
pub const SpirvWriter = @import("SpirvWriter.zig");
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
pub usingnamespace @cImport({
|
||||
@cInclude("CNZSL/CNZSL.h");
|
||||
});
|
||||
27
src/parser.zig
git.filemode.normal_file
27
src/parser.zig
git.filemode.normal_file
@@ -0,0 +1,27 @@
|
||||
const std = @import("std");
|
||||
const cnzsl = @cImport({
|
||||
@cInclude("CNZSL/CNZSL.h");
|
||||
});
|
||||
const Module = @import("Module.zig");
|
||||
|
||||
pub fn parseSource(source: []const u8) !Module {
|
||||
return parseSourceWithFilePath(source, "");
|
||||
}
|
||||
|
||||
pub fn parseSourceWithFilePath(source: []const u8, filePath: []const u8) !Module {
|
||||
const module = try Module.init();
|
||||
errdefer module.deinit();
|
||||
if (cnzsl.nzslParserParseSourceWithFilePath(@ptrCast(module.instance), source.ptr, source.len, filePath.ptr, filePath.len) != 0) {
|
||||
return error.FailedToParse;
|
||||
}
|
||||
return module;
|
||||
}
|
||||
|
||||
pub fn parseFromFile(filePath: []const u8) !Module {
|
||||
const module = try Module.init();
|
||||
errdefer module.deinit();
|
||||
if (cnzsl.nzslParserParseFromFile(@ptrCast(module.instance), filePath.ptr, filePath.len) != 0) {
|
||||
return error.FailedToParse;
|
||||
}
|
||||
return module;
|
||||
}
|
||||
Reference in New Issue
Block a user