adding opcodes and results
This commit is contained in:
132
src/Module.zig
132
src/Module.zig
@@ -12,6 +12,8 @@ const SpvBool = spv.SpvBool;
|
||||
const SpvMember = spv.SpvMember;
|
||||
const SpvBinding = spv.SpvBinding;
|
||||
|
||||
const Result = @import("Result.zig");
|
||||
const Runtime = @import("Runtime.zig");
|
||||
const WordIterator = @import("WordIterator.zig");
|
||||
|
||||
const Self = @This();
|
||||
@@ -23,6 +25,13 @@ const SpvEntryPoint = struct {
|
||||
globals: []SpvWord,
|
||||
};
|
||||
|
||||
const SpvSource = struct {
|
||||
file_name: []const u8,
|
||||
lang: spv.SpvSourceLanguage,
|
||||
lang_version: SpvWord,
|
||||
source: []const u8,
|
||||
};
|
||||
|
||||
const ModuleError = error{
|
||||
InvalidSpirV,
|
||||
InvalidMagic,
|
||||
@@ -45,6 +54,11 @@ code: []const SpvWord,
|
||||
addressing: spv.SpvAddressingModel,
|
||||
memory_model: spv.SpvMemoryModel,
|
||||
|
||||
files: std.ArrayList(SpvSource),
|
||||
extensions: std.ArrayList([]const u8),
|
||||
|
||||
results: std.ArrayList(Result),
|
||||
|
||||
entry_points: std.ArrayList(SpvEntryPoint),
|
||||
capabilities: std.EnumSet(spv.SpvCapability),
|
||||
|
||||
@@ -52,6 +66,11 @@ local_size_x: SpvWord,
|
||||
local_size_y: SpvWord,
|
||||
local_size_z: SpvWord,
|
||||
|
||||
geometry_invocations: SpvWord,
|
||||
geometry_output_count: SpvWord,
|
||||
geometry_input: SpvWord,
|
||||
geometry_output: SpvWord,
|
||||
|
||||
input_locations: std.AutoHashMap(SpvWord, *SpvMember),
|
||||
output_locations: std.AutoHashMap(SpvWord, *SpvMember),
|
||||
bindings: std.AutoHashMap(SpvBinding, *SpvMember),
|
||||
@@ -60,6 +79,9 @@ push_constants: []SpvMember,
|
||||
pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!Self {
|
||||
var self: Self = std.mem.zeroInit(Self, .{
|
||||
.code = allocator.dupe(SpvWord, source) catch return ModuleError.OutOfMemory,
|
||||
.files = std.ArrayList(SpvSource).empty,
|
||||
.extensions = std.ArrayList([]const u8).empty,
|
||||
.results = std.ArrayList(Result).empty,
|
||||
.entry_points = std.ArrayList(SpvEntryPoint).empty,
|
||||
.capabilities = std.EnumSet(spv.SpvCapability).initEmpty(),
|
||||
.local_size_x = 1,
|
||||
@@ -69,7 +91,7 @@ pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!S
|
||||
.output_locations = std.AutoHashMap(SpvWord, *SpvMember).init(allocator),
|
||||
.bindings = std.AutoHashMap(SpvBinding, *SpvMember).init(allocator),
|
||||
});
|
||||
errdefer allocator.free(self.code);
|
||||
errdefer self.deinit(allocator);
|
||||
|
||||
self.it = WordIterator.init(self.code);
|
||||
|
||||
@@ -90,40 +112,57 @@ pub fn init(allocator: std.mem.Allocator, source: []const SpvWord) ModuleError!S
|
||||
self.generator_version = @intCast(generator & 0x0000FFFF);
|
||||
|
||||
self.bound = self.it.next() orelse return ModuleError.InvalidSpirV;
|
||||
self.results.resize(allocator, self.bound) catch return ModuleError.OutOfMemory;
|
||||
for (self.results.items) |*result| {
|
||||
result.* = Result.init();
|
||||
}
|
||||
|
||||
_ = self.it.skip(); // Skip schema
|
||||
|
||||
const it_save = self.it;
|
||||
while (self.it.next()) |opcode_data| {
|
||||
const word_count = ((opcode_data & (~spv.SpvOpCodeMask)) >> spv.SpvWordCountShift) - 1;
|
||||
const opcode = (opcode_data & spv.SpvOpCodeMask);
|
||||
|
||||
var it_tmp = self.it; // Save because operations may iter on this iterator
|
||||
if (std.enums.fromInt(spv.SpvOp, opcode)) |spv_op| {
|
||||
if (op.SetupDispatcher.get(spv_op)) |pfn| {
|
||||
pfn(allocator, word_count, &self) catch {};
|
||||
}
|
||||
}
|
||||
_ = it_tmp.skipN(word_count);
|
||||
self.it = it_tmp;
|
||||
}
|
||||
self.it = it_save;
|
||||
|
||||
std.log.scoped(.SPIRV_Interpreter).debug(
|
||||
\\Loaded shader module with infos:
|
||||
\\ SPIR-V version: {d}.{d}
|
||||
\\ Generator: {s} (ID {d}), encoded version 0x{X}
|
||||
\\ Capabilities count: {d}
|
||||
\\ Entry points count: {d}
|
||||
, .{
|
||||
self.version_major,
|
||||
self.version_minor,
|
||||
spv.vendorName(self.generator_id),
|
||||
self.generator_id,
|
||||
self.generator_version,
|
||||
self.capabilities.count(),
|
||||
self.entry_points.items.len,
|
||||
const prepassOps = std.EnumSet(spv.SpvOp).initMany(&[_]spv.SpvOp{
|
||||
spv.SpvOp.Name,
|
||||
});
|
||||
try self.pass(allocator, prepassOps); // Pre-pass
|
||||
try self.pass(allocator, prepassOps.complement()); // Setup pass
|
||||
|
||||
if (std.process.hasEnvVarConstant("SPIRV_INTERPRETER_DEBUG_LOGS")) {
|
||||
var capability_set_names: std.ArrayList([]const u8) = .empty;
|
||||
defer capability_set_names.deinit(allocator);
|
||||
|
||||
var it = self.capabilities.iterator();
|
||||
while (it.next()) |cap| {
|
||||
capability_set_names.append(allocator, @tagName(cap)) catch return ModuleError.OutOfMemory;
|
||||
}
|
||||
|
||||
const capabilities = std.mem.join(allocator, ", ", capability_set_names.items) catch return ModuleError.OutOfMemory;
|
||||
defer allocator.free(capabilities);
|
||||
|
||||
var entry_points_names = std.ArrayList([]const u8).initCapacity(allocator, self.entry_points.items.len) catch return ModuleError.OutOfMemory;
|
||||
defer entry_points_names.deinit(allocator);
|
||||
|
||||
for (self.entry_points.items) |entry_point| {
|
||||
entry_points_names.appendAssumeCapacity(entry_point.name);
|
||||
}
|
||||
|
||||
const entry_points = std.mem.join(allocator, ", ", entry_points_names.items) catch return ModuleError.OutOfMemory;
|
||||
defer allocator.free(entry_points);
|
||||
|
||||
std.log.scoped(.SPIRV_Interpreter).debug(
|
||||
\\Loaded shader module with infos:
|
||||
\\ SPIR-V version: {d}.{d}
|
||||
\\ Generator: {s} (ID {d}), encoded version 0x{X}
|
||||
\\ Capabilities: [{s}]
|
||||
\\ Entry points: [{s}]
|
||||
, .{
|
||||
self.version_major,
|
||||
self.version_minor,
|
||||
spv.vendorName(self.generator_id),
|
||||
self.generator_id,
|
||||
self.generator_version,
|
||||
capabilities,
|
||||
entry_points,
|
||||
});
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
@@ -139,6 +178,26 @@ fn checkEndiannessFromSpvMagic(magic: SpvWord) bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
fn pass(self: *Self, allocator: std.mem.Allocator, opcodes: std.EnumSet(spv.SpvOp)) ModuleError!void {
|
||||
var rt = Runtime.init(self);
|
||||
defer rt.deinit();
|
||||
while (rt.it.next()) |opcode_data| {
|
||||
const word_count = ((opcode_data & (~spv.SpvOpCodeMask)) >> spv.SpvWordCountShift) - 1;
|
||||
const opcode = (opcode_data & spv.SpvOpCodeMask);
|
||||
|
||||
var it_tmp = rt.it; // Save because operations may iter on this iterator
|
||||
if (std.enums.fromInt(spv.SpvOp, opcode)) |spv_op| {
|
||||
if (opcodes.contains(spv_op)) {
|
||||
if (op.SetupDispatcher.get(spv_op)) |pfn| {
|
||||
pfn(allocator, word_count, &rt) catch return ModuleError.InvalidSpirV;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = it_tmp.skipN(word_count);
|
||||
rt.it = it_tmp;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
|
||||
allocator.free(self.code);
|
||||
self.input_locations.deinit();
|
||||
@@ -149,4 +208,15 @@ pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
|
||||
allocator.free(entry.globals);
|
||||
}
|
||||
self.entry_points.deinit(allocator);
|
||||
self.files.deinit(allocator);
|
||||
|
||||
for (self.extensions.items) |ext| {
|
||||
allocator.free(ext);
|
||||
}
|
||||
self.extensions.deinit(allocator);
|
||||
|
||||
for (self.results.items) |*result| {
|
||||
result.deinit(allocator);
|
||||
}
|
||||
self.results.deinit(allocator);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user