removing spirv headers dependencies, reworking architecture

This commit is contained in:
2025-12-24 18:09:16 +01:00
parent b4ff6f559b
commit 885919952c
15 changed files with 2969 additions and 140 deletions

View File

@@ -1,14 +1,17 @@
const std = @import("std");
const builtin = @import("builtin");
const lib = @import("lib.zig");
const spv_data = @import("spv_data.zig");
const spv = @import("spv.zig");
const op = @import("opcodes.zig");
const SpvVoid = lib.SpvVoid;
const SpvByte = lib.SpvByte;
const SpvWord = lib.SpvWord;
const SpvBool = lib.SpvBool;
const spv = lib.spv;
const SpvVoid = spv.SpvVoid;
const SpvByte = spv.SpvByte;
const SpvWord = spv.SpvWord;
const SpvBool = spv.SpvBool;
const SpvMember = spv.SpvMember;
const SpvBinding = spv.SpvBinding;
const Interpreter = @import("Interpreter.zig");
const WordIterator = @import("WordIterator.zig");
const Self = @This();
@@ -24,11 +27,10 @@ const SpvEntryPoint = struct {
const ModuleError = error{
InvalidSpirV,
InvalidMagic,
UnsupportedEndianness,
OutOfMemory,
};
ctx: *const Interpreter,
it: WordIterator,
version_major: SpvByte,
@@ -51,20 +53,32 @@ local_size_x: SpvWord,
local_size_y: SpvWord,
local_size_z: SpvWord,
pub fn init(allocator: std.mem.Allocator, ctx: *const Interpreter, source: []const SpvWord) ModuleError!Self {
input_locations: std.AutoHashMap(SpvWord, *SpvMember),
output_locations: std.AutoHashMap(SpvWord, *SpvMember),
bindings: std.AutoHashMap(SpvBinding, *SpvMember),
push_constants: []SpvMember,
pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!Self {
var self: Self = std.mem.zeroInit(Self, .{
.ctx = ctx,
.code = allocator.dupe(SpvWord, source) catch return ModuleError.OutOfMemory,
.local_size_x = 1,
.local_size_y = 1,
.local_size_z = 1,
.input_locations = std.AutoHashMap(SpvWord, *SpvMember).init(allocator),
.output_locations = std.AutoHashMap(SpvWord, *SpvMember).init(allocator),
.bindings = std.AutoHashMap(SpvBinding, *SpvMember).init(allocator),
});
errdefer allocator.free(self.code);
self.it = WordIterator.init(self.code);
const magic = self.it.next() orelse return ModuleError.InvalidSpirV;
if (magic != lib.spv.SpvMagicNumber) return ModuleError.InvalidMagic;
if (magic != spv.SpvMagicNumber) {
return ModuleError.InvalidMagic;
}
if (!checkEndiannessFromSpvMagic(magic)) {
return ModuleError.UnsupportedEndianness;
}
const version = self.it.next() orelse return ModuleError.InvalidSpirV;
self.version_major = @intCast((version & 0x00FF0000) >> 16);
@@ -78,14 +92,22 @@ pub fn init(allocator: std.mem.Allocator, ctx: *const Interpreter, source: []con
_ = self.it.skip(); // Skip schema
while (self.it.next()) |opcode| {
if (std.enums.fromInt(spv.SpvOp, opcode)) |spv_op| {
if (op.SetupDispatcher.get(spv_op)) |pfn| {
pfn(0, &self);
}
}
}
std.log.scoped(.SPIRV_Interpreter).debug(
\\Loaded shader module with infos:
\\ SPIR-V version: {d}.{d}
\\ Generator: {s} (ID {d}), encoded version {d}
\\ Generator: {s} (ID {d}), encoded version 0x{X}
, .{
self.version_major,
self.version_minor,
spv_data.vendorName(self.generator_id),
spv.vendorName(self.generator_id),
self.generator_id,
self.generator_version,
});
@@ -93,6 +115,20 @@ pub fn init(allocator: std.mem.Allocator, ctx: *const Interpreter, source: []con
return self;
}
pub fn deinit(self: *const Self, allocator: std.mem.Allocator) void {
allocator.free(self.code);
fn checkEndiannessFromSpvMagic(magic: SpvWord) bool {
const bytes: [4]u8 = @bitCast(magic);
if (0x03 == bytes[0] and 0x02 == bytes[1] and 0x23 == bytes[2] and 0x07 == bytes[3]) {
return builtin.cpu.arch.endian() == .little;
}
if (0x07 == bytes[0] and 0x23 == bytes[1] and 0x02 == bytes[2] and 0x03 == bytes[3]) {
return builtin.cpu.arch.endian() == .big;
}
return false;
}
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
allocator.free(self.code);
self.input_locations.deinit();
self.output_locations.deinit();
self.bindings.deinit();
}