Compare commits

...

2 Commits

Author SHA1 Message Date
b333f143b4 working on spirv debug
Some checks failed
Build / build (push) Successful in 2m18s
Test / build_and_test (push) Failing after 1h22m47s
2026-01-25 21:55:02 +01:00
067db7a48a adding base spv interpreter 2026-01-25 17:57:17 +01:00
8 changed files with 88 additions and 5 deletions

View File

@@ -130,6 +130,13 @@ fn customSoft(b: *std.Build, lib: *std.Build.Step.Compile) !void {
const cpuinfo = b.lazyDependency("cpuinfo", .{}) orelse return error.UnresolvedDependency;
lib.addSystemIncludePath(cpuinfo.path("include"));
lib.linkLibrary(cpuinfo.artifact("cpuinfo"));
const spv = b.dependency("SPIRV_Interpreter", .{
.@"no-example" = true,
.@"no-test" = true,
.@"use-llvm" = true,
}).module("spv");
lib.root_module.addImport("spv", spv);
}
fn addCTest(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, vulkan_headers: *std.Build.Dependency, impl: *const ImplementationDesc, impl_lib: *std.Build.Step.Compile) !*std.Build.Step.Compile {

View File

@@ -33,6 +33,11 @@
.url = "https://github.com/Aandreba/zigrc/archive/refs/tags/1.1.0.tar.gz",
.hash = "zigrc-1.0.0-lENlWzvQAACulrbkL9PVhWjFsWSkYhi7AmfSbCM-2Xlh",
},
.SPIRV_Interpreter = .{
.url = "git+https://git.kbz8.me/kbz_8/SPIRV-Interpreter#e21d26d9975b96a222b70648ceeea9e473e9657f",
.hash = "SPIRV_Interpreter-0.0.1-ajmpn2tAAwCBI0oWa3VKlYX3MEM0OxN4iXQ-PwO6_Vhx",
//.path = "../SPIRV-Interpreter",
},
.cpuinfo = .{
.url = "git+https://github.com/Kbz-8/cpuinfo#4883954cfcec3f6c9ca9c4aaddfc26107e08726f",
.hash = "cpuinfo-0.0.1-RLgIQTLRMgF4dLo8AJ-HvnpFsJe6jmXCJjMWWjil6RF1",

View File

@@ -1,6 +1,7 @@
const std = @import("std");
const vk = @import("vulkan");
const base = @import("base");
const spv = @import("spv");
const VkError = base.VkError;
const Device = base.Device;

View File

@@ -1,6 +1,8 @@
const std = @import("std");
const vk = @import("vulkan");
const base = @import("base");
const spv = @import("spv");
const lib = @import("lib.zig");
const VkError = base.VkError;
const Device = base.Device;
@@ -9,6 +11,7 @@ const Self = @This();
pub const Interface = base.ShaderModule;
interface: Interface,
module: spv.Module,
pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const vk.ShaderModuleCreateInfo) VkError!*Self {
const self = allocator.create(Self) catch return VkError.OutOfHostMemory;
@@ -20,13 +23,26 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v
.destroy = destroy,
};
const code = info.p_code[0..@divExact(info.code_size, 4)];
self.* = .{
.interface = interface,
.module = spv.Module.init(
allocator,
code,
.{
.use_simd_vectors_specializations = !std.process.hasEnvVarConstant(lib.NO_SHADER_SIMD_ENV_NAME),
},
) catch |err| switch (err) {
spv.Module.ModuleError.OutOfMemory => return VkError.OutOfHostMemory,
else => return VkError.ValidationFailed,
},
};
return self;
}
pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
self.module.deinit(allocator);
allocator.destroy(self);
}

View File

@@ -40,6 +40,8 @@ pub const VULKAN_VERSION = vk.makeApiVersion(0, 1, 0, 0);
pub const DRIVER_VERSION = vk.makeApiVersion(0, 0, 0, 1);
pub const DEVICE_ID = 0x600DCAFE;
pub const NO_SHADER_SIMD_ENV_NAME = "STROLL_SOFT_NO_SIMD";
/// Generic system memory.
pub const MEMORY_TYPE_GENERIC_BIT = 0;

View File

@@ -14,6 +14,8 @@ pub const ObjectType: vk.ObjectType = .pipeline;
owner: *Device,
vtable: *const VTable,
bind_point: vk.PipelineBindPoint,
stages: vk.ShaderStageFlags,
pub const VTable = struct {
destroy: *const fn (*Self, std.mem.Allocator) void,
@@ -22,20 +24,31 @@ pub const VTable = struct {
pub fn initCompute(device: *Device, allocator: std.mem.Allocator, cache: ?*PipelineCache, info: *const vk.ComputePipelineCreateInfo) VkError!Self {
_ = allocator;
_ = cache;
_ = info;
return .{
.owner = device,
.vtable = undefined,
.bind_point = .compute,
.stages = info.stage.stage,
};
}
pub fn initGraphics(device: *Device, allocator: std.mem.Allocator, cache: ?*PipelineCache, info: *const vk.GraphicsPipelineCreateInfo) VkError!Self {
_ = allocator;
_ = cache;
_ = info;
var stages: vk.ShaderStageFlags = .{};
if (info.p_stages) |p_stages| {
for (p_stages[0..info.stage_count]) |stage| {
stages = stages.merge(stage.stage);
}
}
return .{
.owner = device,
.vtable = undefined,
.bind_point = .graphics,
.stages = stages,
};
}

View File

@@ -1,5 +1,6 @@
const std = @import("std");
const vk = @import("vulkan");
const lib = @import("lib.zig");
const NonDispatchable = @import("NonDispatchable.zig");
@@ -19,8 +20,12 @@ pub const VTable = struct {
};
pub fn init(device: *Device, allocator: std.mem.Allocator, info: *const vk.ShaderModuleCreateInfo) VkError!Self {
_ = allocator;
_ = info;
if (std.process.hasEnvVarConstant(lib.DRIVER_LOG_SPIRV_ENV_NAME)) {
logShaderModule(allocator, info) catch |e| {
std.log.scoped(.ShaderModule).err("Failed to disassemble SPIR-V module to readable text: {s}", .{@errorName(e)});
};
}
return .{
.owner = device,
.vtable = undefined,
@@ -30,3 +35,35 @@ pub fn init(device: *Device, allocator: std.mem.Allocator, info: *const vk.Shade
pub inline fn destroy(self: *Self, allocator: std.mem.Allocator) void {
self.vtable.destroy(self, allocator);
}
fn logShaderModule(allocator: std.mem.Allocator, info: *const vk.ShaderModuleCreateInfo) !void {
std.log.scoped(.ShaderModule).info("Logging SPIR-V module", .{});
var process = std.process.Child.init(&[_][]const u8{ "spirv-dis", "--no-color", "/home/kbz8/Documents/Code/Zig/SPIRV-Interpreter/example/shader.spv" }, allocator);
process.stdout_behavior = .Pipe;
process.stderr_behavior = .Pipe;
process.stdin_behavior = .Pipe;
var stdout: std.ArrayList(u8) = .empty;
defer stdout.deinit(allocator);
var stderr: std.ArrayList(u8) = .empty;
defer stderr.deinit(allocator);
try process.spawn();
errdefer {
_ = process.kill() catch {};
}
if (process.stdin) |stdin| {
_ = try stdin.write(@ptrCast(@alignCast(info.p_code[0..@divExact(info.code_size, 4)])));
}
try process.collectOutput(allocator, &stdout, &stderr, 1024 * 1024);
_ = try process.wait();
if (stderr.items.len != 0) {
std.log.scoped(.ShaderModule).err("Failed to disassemble SPIR-V module to readable text.\nError:\n{s}", .{stderr.items});
} else if (stdout.items.len != 0) {
std.log.scoped(.ShaderModule).info("{s}\n{d}", .{ stdout.items, stdout.items.len });
}
}

View File

@@ -50,8 +50,10 @@ pub const VULKAN_VENDOR_ID = @typeInfo(vk.VendorId).@"enum".fields[@typeInfo(vk.
pub const DRIVER_DEBUG_ALLOCATOR_ENV_NAME = "STROLL_DEBUG_ALLOCATOR";
pub const DRIVER_LOGS_ENV_NAME = "STROLL_LOGS_LEVEL";
pub const DRIVER_LOG_SPIRV_ENV_NAME = "STROLL_LOG_SPIRV";
/// Default driver name
pub const DRIVER_NAME = "Unnamed Driver";
pub const DRIVER_NAME = "Unnamed Stroll Driver";
/// Default Vulkan version
pub const VULKAN_VERSION = vk.makeApiVersion(0, 1, 0, 0);