diff --git a/src/gfx.zig b/src/gfx.zig deleted file mode 100644 index 2574e64..0000000 --- a/src/gfx.zig +++ /dev/null @@ -1,134 +0,0 @@ -const std = @import("std"); -const builtin = @import("builtin"); - -const vk = @import("vk"); -const c = @import("c.zig"); - -pub fn uploadData( - comptime T: type, - pdev: vk.PhysicalDevice, - vki: Instance.Wrapper, - dev: vk.Device, - vkd: Device.Wrapper, - queue: vk.Queue, - pool: vk.CommandPool, - buffer: vk.Buffer, - source: []const T, -) !void { - // if (@typeInfo(T) == .Struct and @typeInfo(T).Struct.layout == .auto) @compileError("Requires defined T layout"); - - const size = @sizeOf(T) * source.len; - - const staging_buffer = try vkd.createBuffer(dev, &.{ - .size = size, - .usage = .{ .transfer_src_bit = true }, - .sharing_mode = .exclusive, - }, null); - defer vkd.destroyBuffer(dev, staging_buffer, null); - - const vally = VkAllocator.init(pdev, vki); - - const mem_reqs = vkd.getBufferMemoryRequirements(dev, staging_buffer); - const staging_memory = try vally.alloc(dev, vkd, mem_reqs, .{ .host_visible_bit = true, .host_coherent_bit = true }); - // const staging_memory = try allocate(pdev, vki, dev, vkd, mem_reqs, .{ - // .host_visible_bit = true, - // .host_coherent_bit = true, - // }); - defer vkd.freeMemory(dev, staging_memory, null); - - try vkd.bindBufferMemory(dev, staging_buffer, staging_memory, 0); - - { - const data = try vkd.mapMemory(dev, staging_memory, 0, vk.WHOLE_SIZE, .{}); - defer vkd.unmapMemory(dev, staging_memory); - - const dest: [*]T = @ptrCast(@alignCast(data)); - @memcpy(dest, source); - } - - try copyBuffer(dev, queue, pool, buffer, staging_buffer, size, vkd); -} - -pub fn copyBuffer( - dev: vk.Device, - queue: vk.Queue, - pool: vk.CommandPool, - dst: vk.Buffer, - src: vk.Buffer, - size: vk.DeviceSize, - vkd: Device.Wrapper, -) !void { - var cmdbuf: vk.CommandBuffer = undefined; - try vkd.allocateCommandBuffers(dev, &.{ - .command_pool = pool, - .level = .primary, - .command_buffer_count = 1, - }, @ptrCast(&cmdbuf)); - defer vkd.freeCommandBuffers(dev, pool, 1, @ptrCast(&cmdbuf)); - - try vkd.beginCommandBuffer(cmdbuf, &.{ - .flags = .{ .one_time_submit_bit = true }, - }); - - const region = vk.BufferCopy{ - .src_offset = 0, - .dst_offset = 0, - .size = size, - }; - vkd.cmdCopyBuffer(cmdbuf, src, dst, 1, @ptrCast(®ion)); - - try vkd.endCommandBuffer(cmdbuf); - - const si = vk.SubmitInfo{ - .command_buffer_count = 1, - .p_command_buffers = @ptrCast(&cmdbuf), - .p_wait_dst_stage_mask = undefined, - }; - - // creating and submitting a queue for every copy operation seems a bad idea for "streamed" data - // gonna want a way to send a copy operation WITH SYNCHRONIZATION PRIMITIVES on a particular queue - // see https://stackoverflow.com/a/62183243 - // - // this may be a misunderstanding on how submission works... - - try vkd.queueSubmit(queue, 1, @ptrCast(&si), .null_handle); - try vkd.queueWaitIdle(queue); -} - -pub const VkAllocator = struct { - memory_types: [vk.MAX_MEMORY_TYPES]vk.MemoryType, - memory_type_count: u32, - - pub fn init( - pdev: vk.PhysicalDevice, - vki: Instance.Wrapper, - ) VkAllocator { - const props = vki.getPhysicalDeviceMemoryProperties(pdev); - - return VkAllocator{ - .memory_types = props.memory_types, - .memory_type_count = props.memory_type_count, - }; - } - - pub fn alloc( - self: VkAllocator, - dev: vk.Device, - vkd: Device.Wrapper, - reqs: vk.MemoryRequirements, - flags: vk.MemoryPropertyFlags, - ) !vk.DeviceMemory { - const memory_type_bits = reqs.memory_type_bits; - - for (self.memory_types[0..self.memory_type_count], 0..) |mem_type, idx| { - if (memory_type_bits & (@as(u32, 1) << @truncate(idx)) != 0 and mem_type.property_flags.contains(flags)) { - return try vkd.allocateMemory(dev, &.{ - .allocation_size = reqs.size, - .memory_type_index = @intCast(idx), - }, null); - } - } - - return error.NoSuitableMemoryType; - } -}; diff --git a/src/inspect.zig b/src/inspect.zig deleted file mode 100644 index 66cc397..0000000 --- a/src/inspect.zig +++ /dev/null @@ -1,146 +0,0 @@ -const std = @import("std"); -const vk = @import("vk"); - -const c = @cImport({ - @cDefine("GLFW_INCLUDE_NONE", {}); - @cInclude("GLFW/glfw3.h"); -}); - -const BaseWrapper = vk.BaseWrapper(.{ - .getInstanceProcAddr = true, - .createInstance = true, -}); - -const InstanceWrapper = vk.InstanceWrapper(.{ - .destroyInstance = true, - .enumeratePhysicalDevices = true, - .getPhysicalDeviceProperties = true, - .getPhysicalDeviceQueueFamilyProperties = true, - .getPhysicalDeviceSurfaceFormatsKHR = true, - .getPhysicalDeviceSurfacePresentModesKHR = true, - .getPhysicalDeviceSurfaceSupportKHR = true, - .getPhysicalDeviceSurfaceCapabilitiesKHR = true, - .destroySurfaceKHR = true, -}); - -extern fn glfwGetRequiredInstanceExtensions(count: *u32) [*]const [*:0]const u8; - -extern fn glfwCreateWindowSurface( - instance: vk.Instance, - window: *c.GLFWwindow, - allocation_callbacks: ?*const vk.AllocationCallbacks, - surface: *vk.SurfaceKHR, -) vk.Result; - -extern fn vkGetInstanceProcAddr(instance: vk.Instance, procname: [*:0]const u8) vk.PfnVoidFunction; -extern fn vkGetDeviceProcAddr(device: vk.Device, procname: [*:0]const u8) vk.PfnVoidFunction; - -pub fn main() !void { - if (c.glfwInit() == c.GLFW_FALSE) return error.glfwInitFailed; - defer c.glfwTerminate(); - - c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API); - c.glfwWindowHintString(c.GLFW_X11_CLASS_NAME, "floating_window"); - c.glfwWindowHintString(c.GLFW_X11_INSTANCE_NAME, "floating_window"); - const window = c.glfwCreateWindow(400, 300, "vkinspect", null, null) orelse - return error.glfwWindowCreateFailed; - defer c.glfwDestroyWindow(window); - - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - const ally = gpa.allocator(); - - const vkb = try BaseWrapper.load(vkGetInstanceProcAddr); - - var ext_count: u32 = undefined; - const exts = glfwGetRequiredInstanceExtensions(&ext_count); - - const instance = try vkb.createInstance(&.{ - .p_application_info = &.{ - .p_application_name = "vkinspect", - .application_version = 0, - .p_engine_name = "vkinspect", - .engine_version = 0, - .api_version = vk.API_VERSION_1_3, - }, - .enabled_extension_count = ext_count, - .pp_enabled_extension_names = exts, - .enabled_layer_count = 0, - }, null); - const vki = try InstanceWrapper.load(instance, vkGetInstanceProcAddr); - defer vki.destroyInstance(instance, null); - - var surface: vk.SurfaceKHR = undefined; - switch (glfwCreateWindowSurface(instance, window, null, &surface)) { - .success => {}, - else => return error.Unknown, - } - defer vki.destroySurfaceKHR(instance, surface, null); - - var pdev_count: u32 = undefined; - _ = try vki.enumeratePhysicalDevices(instance, &pdev_count, null); - const pdevs = try ally.alloc(vk.PhysicalDevice, pdev_count); - defer ally.free(pdevs); - _ = try vki.enumeratePhysicalDevices(instance, &pdev_count, pdevs.ptr); - - std.debug.print("{d} physical devices:\n", .{pdev_count}); - for (pdevs) |pdev| { - const props = vki.getPhysicalDeviceProperties(pdev); - const name = std.mem.sliceTo(&props.device_name, 0); - std.debug.print("=" ** 30 ++ "\n", .{}); - std.debug.print("= {s}\n", .{name}); - std.debug.print("=" ** 30 ++ "\n", .{}); - - std.debug.print("type: {any}\n", .{props.device_type}); - // props.device_type - - std.debug.print("max_push_constants_size: {d}\n", .{props.limits.max_push_constants_size}); - - var family_count: u32 = undefined; - vki.getPhysicalDeviceQueueFamilyProperties(pdev, &family_count, null); - const families = try ally.alloc(vk.QueueFamilyProperties, family_count); - defer ally.free(families); - vki.getPhysicalDeviceQueueFamilyProperties(pdev, &family_count, families.ptr); - - std.debug.print(" {d} queue families:\n", .{family_count}); - for (families, 0..) |family, idx| { - const support = try vki.getPhysicalDeviceSurfaceSupportKHR(pdev, @intCast(idx), surface); - std.debug.print(" - {any}\n", .{family.queue_flags}); - std.debug.print(" (max {d}, surface {any})\n", .{ - family.queue_count, - support != 0, - }); - } - - var format_count: u32 = undefined; - _ = try vki.getPhysicalDeviceSurfaceFormatsKHR(pdev, surface, &format_count, null); - const formats = try ally.alloc(vk.SurfaceFormatKHR, format_count); - defer ally.free(formats); - _ = try vki.getPhysicalDeviceSurfaceFormatsKHR(pdev, surface, &format_count, formats.ptr); - - var mode_count: u32 = undefined; - _ = try vki.getPhysicalDeviceSurfacePresentModesKHR(pdev, surface, &mode_count, null); - const modes = try ally.alloc(vk.PresentModeKHR, mode_count); - defer ally.free(modes); - _ = try vki.getPhysicalDeviceSurfacePresentModesKHR(pdev, surface, &mode_count, modes.ptr); - - std.debug.print(" {d} formats\n", .{format_count}); - for (formats) |format| { - std.debug.print(" - {any}\n", .{format}); - } - std.debug.print(" {d} present modes\n", .{mode_count}); - for (modes) |mode| { - std.debug.print(" - {any}\n", .{mode}); - } - - const caps = try vki.getPhysicalDeviceSurfaceCapabilitiesKHR(pdev, surface); - std.debug.print(" surface capabilities:\n", .{}); - std.debug.print(" {any}\n", .{caps.current_extent}); - std.debug.print(" current: {any}\n", .{caps.current_transform}); - std.debug.print(" supported: {any}\n", .{caps.supported_transforms}); - std.debug.print(" {any}\n", .{caps.supported_usage_flags}); - std.debug.print(" {} - {} images\n", .{ caps.min_image_count, caps.max_image_count }); - std.debug.print(" {} - {} extent\n", .{ caps.min_image_extent, caps.max_image_extent }); - std.debug.print(" 1 - {} arrays\n", .{caps.max_image_array_layers}); - std.debug.print(" {}\n", .{caps.supported_composite_alpha}); - } -}