adding entry point to soft driver, adding C test

This commit is contained in:
2025-10-29 21:55:28 +01:00
parent 0c0bc75959
commit 6d559d719a
8 changed files with 97 additions and 13 deletions

View File

@@ -9,7 +9,7 @@ const ImplementationDesc = struct {
const implementations = [_]ImplementationDesc{
.{
.name = "soft",
.root_source_file = "src/soft/lib.zig",
.root_source_file = "src/soft/libvulkan.zig",
},
};
@@ -57,5 +57,24 @@ pub fn build(b: *std.Build) void {
const run_tests = b.addRunArtifact(lib_tests);
const test_step = b.step(b.fmt("test-{s}", .{impl.name}), b.fmt("Run lib{s} tests", .{impl.name}));
test_step.dependOn(&run_tests.step);
const c_test_exe = b.addExecutable(.{
.name = b.fmt("c_test_vulkan_{s}", .{impl.name}),
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
c_test_exe.root_module.addCSourceFile(.{
.file = b.path("test/c/main.c"),
.flags = &.{b.fmt("-DLIBVK=\"{s}\"", .{lib.name})},
});
const run_c_test = b.addRunArtifact(c_test_exe);
const test_c_step = b.step(b.fmt("test-c-{s}", .{impl.name}), b.fmt("Run lib{s} C test", .{impl.name}));
test_c_step.dependOn(b.getInstallStep());
test_c_step.dependOn(&run_c_test.step);
}
}

View File

@@ -1,7 +0,0 @@
const std = @import("std");
const common = @import("common");
test {
std.testing.refAllDeclsRecursive(@This());
}

11
src/soft/libvulkan.zig git.filemode.normal_file
View File

@@ -0,0 +1,11 @@
const std = @import("std");
const vk = @import("vulkan");
const common = @import("common");
pub export fn vk_icdGetInstanceProcAddr(instance: vk.Instance, pName: ?[*:0]const u8) callconv(vk.vulkan_call_conv) vk.PfnVoidFunction {
if (pName == null) {
return null;
}
const name = std.mem.span(pName.?);
return common.icd.getInstanceProcAddr(instance, name);
}

View File

@@ -1,13 +1,24 @@
const std = @import("std");
const vk = @import("vulkan");
const PhysicalDevice = @import("PhysicalDevice").PhysicalDevice;
const PhysicalDevice = @import("PhysicalDevice.zig").PhysicalDevice;
const Object = @import("object.zig").Object;
pub const Instance = extern struct {
const Self = @This();
const ObjectType: vk.ObjectType = .instance;
pub const ObjectType: vk.ObjectType = .instance;
pub const vtable: *const VTable = .{};
object: Object,
physical_devices: std.ArrayList(*PhysicalDevice),
//physical_devices: std.ArrayList(*PhysicalDevice),
alloc_callbacks: vk.AllocationCallbacks,
pub const VTable = struct {
createInstance: ?vk.PfnCreateInstance,
destroyInstance: ?vk.PfnDestroyInstance,
enumeratePhysicalDevices: ?vk.PfnEnumeratePhysicalDevices,
getInstanceProcAddr: ?vk.PfnGetInstanceProcAddr,
enumerateInstanceVersion: ?vk.PfnEnumerateInstanceVersion,
//enumerateInstanceLayerProperties: vk.PfnEnumerateInstanceProperties,
enumerateInstanceExtensionProperties: ?vk.PfnEnumerateInstanceExtensionProperties,
};
};

24
src/vulkan/icd.zig git.filemode.normal_file
View File

@@ -0,0 +1,24 @@
const std = @import("std");
const vk = @import("vulkan");
const c = @cImport({
@cInclude("vulkan/vk_icd.h");
});
const Instance = @import("Instance.zig").Instance;
const fromHandle = @import("object.zig").fromHandle;
pub fn getInstanceProcAddr(vk_instance: vk.Instance, name: []const u8) vk.PfnVoidFunction {
_ = fromHandle(Instance, vk.Instance, vk_instance) catch .{};
inline for (.{
"vkCreateInstance",
"vkDestroyInstance",
"vkGetInstanceProcAddr",
}) |sym| {
if (std.mem.eql(u8, name, sym)) {
//const f = @field(Instance.vtable, sym);
return @ptrFromInt(12);
}
}
return null;
}

View File

@@ -1,5 +1,7 @@
const std = @import("std");
pub const icd = @import("icd.zig");
pub const Instance = @import("Instance.zig");
pub const PhysicalDevice = @import("PhysicalDevice.zig");

View File

@@ -10,7 +10,7 @@ pub const Object = extern struct {
kind: vk.ObjectType,
owner: ?*anyopaque,
// VK_EXT_debug_utils
name: ?[]const u8,
name: ?[*]const u8,
pub fn init(owner: ?*anyopaque, kind: vk.ObjectType) Self {
return .{
@@ -24,7 +24,7 @@ pub const Object = extern struct {
pub inline fn fromHandle(comptime T: type, comptime VkT: type, handle: VkT) !*T {
comptime {
if (!@hasDecl(T, "object") or !@hasDecl(T, "ObjectType") or @TypeOf(T.ObjectType) != vk.ObjectType) {
if (!@hasField(T, "object") or !@hasDecl(T, "ObjectType") or @TypeOf(T.ObjectType) != vk.ObjectType) {
@compileError("Object type \"" ++ @typeName(T) ++ "\" is malformed.");
}
}

24
test/c/main.c git.filemode.normal_file
View File

@@ -0,0 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
#define VK_NO_PROTOTYPES
#include <vulkan/vulkan.h>
#include <dlfcn.h>
#ifndef LIBVK
#define LIBVK "vulkan"
#endif
int main(void)
{
printf("openning ./zig-out/lib/lib" LIBVK ".so\n");
void* lib = dlopen("./zig-out/lib/lib" LIBVK ".so", RTLD_NOW | RTLD_LOCAL);
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = dlsym(lib, "vk_icdGetInstanceProcAddr");
printf("test %p\n", vkGetInstanceProcAddr(NULL, "vkCreateInstance"));
dlclose(lib);
return 0;
}