adding icd base

This commit is contained in:
2025-11-04 17:44:10 +01:00
parent 795b38bdcc
commit 12ad577c25
6 changed files with 7202 additions and 31 deletions

View File

@@ -11,9 +11,9 @@ physical_devices: std.ArrayList(*Dispatchable(PhysicalDevice)),
dispatch_table: *const DispatchTable,
pub const DispatchTable = struct {
requestPhysicalDevices: *const fn (*Self, std.mem.Allocator) VkError!void,
releasePhysicalDevices: *const fn (*Self, std.mem.Allocator) VkError!void,
destroyInstance: *const fn (*Self, std.mem.Allocator) VkError!void,
releasePhysicalDevices: *const fn (*Self, std.mem.Allocator) VkError!void,
requestPhysicalDevices: *const fn (*Self, std.mem.Allocator) VkError!void,
};
pub fn init(allocator: std.mem.Allocator, infos: *const vk.InstanceCreateInfo) VkError!Self {
@@ -25,6 +25,26 @@ pub fn init(allocator: std.mem.Allocator, infos: *const vk.InstanceCreateInfo) V
};
}
pub fn deinit(self: *Self, allocator: std.mem.Allocator) VkError!void {
try self.dispatch_table.releasePhysicalDevices(self, allocator);
try self.dispatch_table.destroyInstance(self, allocator);
}
pub fn enumerateExtensionProperties(layer_name: ?[]const u8, property_count: *u32, properties: ?*vk.ExtensionProperties) VkError!void {
if (layer_name) |_| {
return VkError.LayerNotPresent;
}
_ = properties;
_ = std.StaticStringMap(vk.ExtensionProperties).initComptime(.{});
property_count.* = 0;
}
pub fn releasePhysicalDevices(self: *Self, allocator: std.mem.Allocator) VkError!void {
try self.dispatch_table.releasePhysicalDevices(self, allocator);
}
pub fn requestPhysicalDevices(self: *Self, allocator: std.mem.Allocator) VkError!void {
try self.dispatch_table.requestPhysicalDevices(self, allocator);
if (self.physical_devices.items.len == 0) {
@@ -35,12 +55,3 @@ pub fn requestPhysicalDevices(self: *Self, allocator: std.mem.Allocator) VkError
std.log.scoped(.vkCreateInstance).info("Found VkPhysicalDevice named {s}", .{physical_device.object.props.device_name});
}
}
pub fn releasePhysicalDevices(self: *Self, allocator: std.mem.Allocator) VkError!void {
try self.dispatch_table.releasePhysicalDevices(self, allocator);
}
pub fn deinit(self: *Self, allocator: std.mem.Allocator) VkError!void {
try self.dispatch_table.releasePhysicalDevices(self, allocator);
try self.dispatch_table.destroyInstance(self, allocator);
}

View File

@@ -17,6 +17,38 @@ const PhysicalDevice = @import("PhysicalDevice.zig");
extern fn __vkImplCreateInstance(*const std.mem.Allocator, *const vk.InstanceCreateInfo) ?*Instance;
/// This file contains all exported Vulkan entrypoints.
///
/// The use of official Vulkan function names is assumed
/// and is not a concern, given that this driver only implements Vulkan's API.
// ICD Interface =============================================================================================================================================
pub export fn vk_icdNegotiateLoaderICDInterfaceVersion(p_version: *u32) callconv(vk.vulkan_call_conv) vk.Result {
p_version.* = 7;
return .success;
}
pub export fn vk_icdGetInstanceProcAddr(p_instance: vk.Instance, p_name: ?[*:0]const u8) callconv(vk.vulkan_call_conv) vk.PfnVoidFunction {
if (p_name == null) {
return null;
}
const name = std.mem.span(p_name.?);
std.log.scoped(.vk_icdGetInstanceProcAddr).info("Loading {s}...", .{name});
logger.indent();
defer logger.unindent();
const icd_pfn_map = std.StaticStringMap(vk.PfnVoidFunction).initComptime(.{
functionMapElement("vk_icdGetInstanceProcAddr"),
functionMapElement("vk_icdNegotiateLoaderICDInterfaceVersion"),
});
if (icd_pfn_map.get(name)) |pfn| return pfn;
return vkGetInstanceProcAddr(p_instance, p_name);
}
// Global functions ==========================================================================================================================================
fn functionMapElement(name: []const u8) struct { []const u8, vk.PfnVoidFunction } {
if (!std.meta.hasFn(@This(), name)) {
std.log.scoped(.vkGetInstanceProcAddr).err("Could not find function {s}", .{name});
@@ -25,6 +57,10 @@ fn functionMapElement(name: []const u8) struct { []const u8, vk.PfnVoidFunction
return .{ name, @as(vk.PfnVoidFunction, @ptrCast(&@field(@This(), name))) };
}
const device_pfn_map = std.StaticStringMap(vk.PfnVoidFunction).initComptime(.{
functionMapElement("vkDestroyDevice"),
});
pub export fn vkGetInstanceProcAddr(p_instance: vk.Instance, p_name: ?[*:0]const u8) callconv(vk.vulkan_call_conv) vk.PfnVoidFunction {
if (p_name == null) {
return null;
@@ -32,30 +68,34 @@ pub export fn vkGetInstanceProcAddr(p_instance: vk.Instance, p_name: ?[*:0]const
const name = std.mem.span(p_name.?);
const global_pfn_map = std.StaticStringMap(vk.PfnVoidFunction).initComptime(.{
functionMapElement("vkGetInstanceProcAddr"),
functionMapElement("vkCreateInstance"),
functionMapElement("vkGetInstanceProcAddr"),
functionMapElement("vkEnumerateInstanceExtensionProperties"),
});
const instance_pfn_map = std.StaticStringMap(vk.PfnVoidFunction).initComptime(.{
functionMapElement("vkCreateDevice"),
functionMapElement("vkDestroyInstance"),
functionMapElement("vkEnumeratePhysicalDevices"),
functionMapElement("vkGetDeviceProcAddr"),
functionMapElement("vkGetPhysicalDeviceProperties"),
functionMapElement("vkGetPhysicalDeviceProperties"),
});
const device_pfn_map = std.StaticStringMap(vk.PfnVoidFunction).initComptime(.{
functionMapElement("vkDestroyDevice"),
});
std.log.scoped(.vkGetInstanceProcAddr).info("Loading {s}...", .{name});
logger.indent();
defer logger.unindent();
if (global_pfn_map.get(name)) |pfn| return pfn;
if (p_instance == .null_handle) return null;
if (p_instance == .null_handle) {
std.log.scoped(.vkGetInstanceProcAddr).err("Could not find global entrypoint {s}", .{name});
return null;
}
if (instance_pfn_map.get(name)) |pfn| return pfn;
return if (device_pfn_map.get(name)) |pfn| pfn else null;
if (device_pfn_map.get(name)) |pfn| return pfn;
std.log.scoped(.vkGetInstanceProcAddr).err("Could not find entrypoint {s}", .{name});
return null;
}
pub export fn vkCreateInstance(p_infos: ?*const vk.InstanceCreateInfo, callbacks: ?*const vk.AllocationCallbacks, p_instance: *vk.Instance) callconv(vk.vulkan_call_conv) vk.Result {
@@ -75,6 +115,17 @@ pub export fn vkCreateInstance(p_infos: ?*const vk.InstanceCreateInfo, callbacks
return .success;
}
pub export fn vkEnumerateInstanceExtensionProperties(p_layer_name: ?[*:0]const u8, property_count: *u32, properties: ?*vk.ExtensionProperties) callconv(vk.vulkan_call_conv) vk.Result {
var name: ?[]const u8 = null;
if (p_layer_name) |layer_name| {
name = std.mem.span(layer_name);
}
Instance.enumerateExtensionProperties(name, property_count, properties) catch |err| return toVkResult(err);
return .success;
}
// Instance functions ========================================================================================================================================
pub export fn vkDestroyInstance(p_instance: vk.Instance, callbacks: ?*const vk.AllocationCallbacks) callconv(vk.vulkan_call_conv) void {
std.log.scoped(.vkDestroyInstance).info("Destroying VkInstance", .{});
logger.indent();
@@ -123,6 +174,8 @@ pub export fn vkCreateDevice(p_physical_device: vk.PhysicalDevice, p_infos: ?*co
return .success;
}
// Device functions ==========================================================================================================================================
pub export fn vkDestroyDevice(p_device: vk.Device, callbacks: ?*const vk.AllocationCallbacks) callconv(vk.vulkan_call_conv) void {
const allocator = VulkanAllocator.init(callbacks, .device).allocator();
const dispatchable = Dispatchable(Device).fromHandle(p_device) catch return;
@@ -133,3 +186,17 @@ pub export fn vkDestroyDevice(p_device: vk.Device, callbacks: ?*const vk.Allocat
dispatchable.object.destroy(allocator) catch return;
dispatchable.destroy(allocator);
}
pub export fn vkGetDeviceProcAddr(p_device: vk.Device, p_name: ?[*:0]const u8) callconv(vk.vulkan_call_conv) vk.PfnVoidFunction {
if (p_name == null) {
return null;
}
const name = std.mem.span(p_name.?);
std.log.scoped(.vkGetDeviceProcAddr).info("Loading {s}...", .{name});
logger.indent();
defer logger.unindent();
if (p_device == .null_handle) return null;
return if (device_pfn_map.get(name)) |pfn| pfn else null;
}

View File

@@ -47,7 +47,7 @@ pub fn log(comptime level: std.log.Level, comptime scope: @Type(.enum_literal),
std.fmt.comptimePrint("({s}): ", .{scope_name});
};
const prefix = "[" ++ comptime level.asText() ++ "] ";
const prefix = std.fmt.comptimePrint("{s: <8}", .{"[" ++ comptime level.asText() ++ "] "});
const level_color: std.Io.tty.Color = switch (level) {
.info => .blue,

View File

@@ -12,6 +12,9 @@
#define LIBVK "vulkan"
#endif
#define VOLK_IMPLEMENTATION
#include "volk.h"
#define CheckVk(x) \
do { \
if((x) != VK_SUCCESS) \
@@ -31,27 +34,33 @@ int main(void)
}
puts("openned ./zig-out/lib/lib" LIBVK ".so");
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = dlsym(lib, "vkGetInstanceProcAddr");
volkInitialize();
#define VULKAN_GLOBAL_FUNCTION(fn) PFN_##fn fn = (PFN_##fn)vkGetInstanceProcAddr(NULL, #fn);
VULKAN_GLOBAL_FUNCTION(vkCreateInstance)
VkDirectDriverLoadingInfoLUNARG directLoadingInfo = {};
directLoadingInfo.sType = VK_STRUCTURE_TYPE_DIRECT_DRIVER_LOADING_INFO_LUNARG;
directLoadingInfo.pfnGetInstanceProcAddr = (PFN_vkGetInstanceProcAddrLUNARG)(dlsym(lib, "vk_icdGetInstanceProcAddr"));
VkInstanceCreateInfo instance_create_info = { 0 };
VkDirectDriverLoadingListLUNARG directDriverList = {};
directDriverList.sType = VK_STRUCTURE_TYPE_DIRECT_DRIVER_LOADING_LIST_LUNARG;
directDriverList.mode = VK_DIRECT_DRIVER_LOADING_MODE_EXCLUSIVE_LUNARG;
directDriverList.driverCount = 1;
directDriverList.pDrivers = &directLoadingInfo;
const char* extensions[] = { VK_LUNARG_DIRECT_DRIVER_LOADING_EXTENSION_NAME };
VkInstanceCreateInfo instance_create_info = {};
instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instance_create_info.pApplicationInfo = NULL;
instance_create_info.enabledExtensionCount = 1;
instance_create_info.ppEnabledExtensionNames = extensions;
instance_create_info.pNext = &directDriverList;
VkInstance instance = VK_NULL_HANDLE;
CheckVk(vkCreateInstance(&instance_create_info, NULL, &instance));
volkLoadInstance(instance);
printf("VkInstance %p\n", instance);
#define VULKAN_INSTANCE_FUNCTION(fn) PFN_##fn fn = (PFN_##fn)vkGetInstanceProcAddr(instance, #fn);
VULKAN_INSTANCE_FUNCTION(vkEnumeratePhysicalDevices)
VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceProperties)
VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceMemoryProperties)
VULKAN_INSTANCE_FUNCTION(vkDestroyInstance)
VULKAN_INSTANCE_FUNCTION(vkCreateDevice)
VULKAN_INSTANCE_FUNCTION(vkDestroyDevice)
uint32_t count;
vkEnumeratePhysicalDevices(instance, &count, NULL);
printf("VkPhysicalDevice count %d\n", count);
@@ -69,6 +78,8 @@ int main(void)
CheckVk(vkCreateDevice(physical_devices[0], &device_create_info, NULL, &device));
printf("VkDevice %p\n", device);
volkLoadDevice(device);
vkDestroyDevice(device, NULL);
vkDestroyInstance(instance, NULL);

3899
test/c/volk.c git.filemode.normal_file

File diff suppressed because it is too large Load Diff

3183
test/c/volk.h git.filemode.normal_file

File diff suppressed because it is too large Load Diff