//! Vulkan renderer and presentation bridge for the wio platform backend. //! //! This backend owns the Vulkan instance, device, surface, and swapchain. It //! uses Vulkan 1.2 and classic render passes by default, with optional Vulkan //! 1.3 dynamic rendering. //! //! With `.presentation = .framebuffer` it owns no surface and no swapchain: //! each frame is drawn into an image of its own, copied into host memory in //! the same submission, and handed to the window as a wio framebuffer. That //! is for a window system Vulkan cannot make a surface for -- wio's native //! Wayland client, which has no libwayland `wl_display` to give it -- and //! costs a copy through the CPU each frame. const std = @import("std"); const dvui = @import("dvui"); const vk = @import("vk"); const wio = @import("wio"); const Renderer = @import("vulkan/renderer.zig"); pub const dmabuf = @import("vulkan/dmabuf.zig"); const log = std.log.scoped(.dvui_vulkan); pub const kind: dvui.enums.RenderBackend = .vulkan; pub const default_api_version = vk.API_VERSION_1_2; /// Compatibility alias for the backend's default Vulkan version. pub const api_version = default_api_version; /// Vulkan declarations used by applications which record their own rendering /// before DVUI in the same frame. pub const vulkan = vk; pub const RenderStats = Renderer.StatsSnapshot; pub const TextureIdx = Renderer.TextureIdx; pub const ResourceUsage = Renderer.ResourceUsage; pub const MappedBufferMemory = Renderer.MappedBufferMemory; pub const BufferAllocation = Renderer.BufferAllocation; pub const ImageAllocation = Renderer.ImageAllocation; pub const ResourceAllocator = Renderer.ResourceAllocator; pub const VkMemory = Renderer.VkMemory; pub const GpuAllocator = Renderer.GpuAllocator; const frames_in_flight = 2; pub const RenderingMode = enum { render_pass, dynamic, }; /// How a finished frame reaches the window. pub const Presentation = enum { /// A Vulkan surface and swapchain. swapchain, /// Read back and written to a wio framebuffer, which needs wio built with /// `enable_framebuffer`. Paced by the window system's frame callbacks /// rather than by a present mode, so `vsync` does nothing. framebuffer, /// Copied on the GPU into a dma-buf the compositor reads directly, /// through wio's native Wayland client: no copy through the CPU. Needs /// the compositor to offer linux-dmabuf and name its device, and that /// device to be one Vulkan can share memory from; where either is /// missing, or the compositor refuses the buffer, it is `.framebuffer` /// instead. Paced like `.framebuffer`. dmabuf, }; /// Whether wio has the native Wayland client that `.dmabuf` needs. const native_wayland = wio.build_options.wayland and @hasDecl(wio.build_options, "wayland_native") and wio.build_options.wayland_native; const wl_present = if (native_wayland) wio.native_wayland.present else struct {}; const NativeWayland = if (native_wayland) wio.NativeWayland else void; /// What `.dmabuf` presentation holds: the window's presentation objects, a /// pool of dma-bufs the compositor has let go of, and the semaphores that /// carry the kernel's implicit fences in and out of each submission. On the /// heap, because the pool calls back into it. const Dma = if (!native_wayland) struct {} else struct { allocator: std.mem.Allocator, vk_alloc: ?*vk.AllocationCallbacks, instance: vk.InstanceProxy, physical_device: vk.PhysicalDevice, device: vk.DeviceProxy, support: dmabuf.Support, nw: NativeWayland, pool: ?*wl_present.BufferPool = null, /// The buffer this frame is copied into, from `beginApplicationFrame` /// until it is presented. current: ?*wl_present.Buffer = null, /// Per slot: what the compositor may still be doing with the buffer, to /// wait for; and the frame's completion, to hand to the buffer. wait: [frames_in_flight]vk.Semaphore = @splat(.null_handle), waiting: [frames_in_flight]bool = @splat(false), done: [frames_in_flight]vk.Semaphore = @splat(.null_handle), /// Explicit sync, where the compositor and the device both have it; the /// implicit kind above otherwise. sync: ?wl_present.Syncobj = null, /// Buffers presented under explicit sync whose release points have not /// yet been seen to signal: the pool holds them as busy until then. awaiting: std.ArrayList(*wl_present.Buffer) = .empty, /// What a pool buffer's handle points at. const Target = struct { memory: union(enum) { buffer: dmabuf.Buffer, image: dmabuf.Image, }, /// Explicit sync: this buffer's own timeline, as a semaphore and as /// the compositor's import of it. Its points alternate, acquire then /// release, so they are always added in order. timeline: vk.Semaphore = .null_handle, wl_timeline: ?wl_present.Syncobj.Timeline = null, /// The last release point; the next acquire point is one past it. point: u64 = 0, fn fd(self: *const Target) std.posix.fd_t { return switch (self.memory) { inline else => |t| t.fd, }; } }; fn target(buffer: *const wl_present.Buffer) *Target { return @ptrCast(@alignCast(buffer.storage.dmabuf.handle.?)); } fn allocate(context: ?*anyopaque, width: u32, height: u32, format: wl_present.Format, modifiers: []const u64) anyerror!wl_present.Dmabuf.Allocation { const self: *Dma = @ptrCast(@alignCast(context.?)); if (format != .xrgb8888) return error.UnsupportedFormat; const t = try self.allocator.create(Target); errdefer self.allocator.destroy(t); t.* = .{ .memory = undefined }; const extent: vk.Extent2D = .{ .width = width, .height = height }; if (self.sync) |sync| { t.timeline = try dmabuf.createTimeline(self.device, self.vk_alloc); const fd = try dmabuf.exportTimeline(self.device, t.timeline); defer _ = std.os.linux.close(fd); t.wl_timeline = try sync.importTimeline(fd); } errdefer self.dropTimeline(t); // An explicit modifier the device and compositor share, drawn into by // a copy between images. if (self.support.modifiers) { var explicit: [64]u64 = undefined; var count: usize = 0; for (modifiers) |m| if (m != dmabuf.mod_invalid and count < explicit.len) { explicit[count] = m; count += 1; }; if (dmabuf.createImage(self.allocator, self.instance, self.physical_device, self.device, self.support, .{ .extent = extent, .format = .b8g8r8a8_unorm, .usage = .{ .transfer_dst_bit = true }, .modifiers = explicit[0..count], }, self.vk_alloc)) |image| { t.memory = .{ .image = image }; var allocation: wl_present.Dmabuf.Allocation = .{ .handle = t, .modifier = image.modifier, .plane_count = image.plane_count }; for (allocation.planes[0..image.plane_count], image.planes[0..image.plane_count]) |*to, from| { to.* = .{ .fd = image.fd, .offset = from.offset, .stride = from.stride }; } return allocation; } else |err| log.debug("no modifier shared with the compositor ({t}); a linear buffer instead", .{err}); } // A linear buffer, filled by a copy from the image. The compositor // may offer only the implicit modifier; memory with no layout of its // own is linear under that too. const modifier: u64 = if (std.mem.indexOfScalar(u64, modifiers, dmabuf.mod_linear) != null) dmabuf.mod_linear else if (std.mem.indexOfScalar(u64, modifiers, dmabuf.mod_invalid) != null) dmabuf.mod_invalid else return error.NoLinearModifier; const buffer = try dmabuf.createBuffer(self.instance, self.physical_device, self.device, extent, self.vk_alloc); t.memory = .{ .buffer = buffer }; var allocation: wl_present.Dmabuf.Allocation = .{ .handle = t, .modifier = modifier, .plane_count = 1 }; allocation.planes[0] = .{ .fd = buffer.fd, .offset = 0, .stride = buffer.stride }; return allocation; } fn free(context: ?*anyopaque, handle: ?*anyopaque) void { const self: *Dma = @ptrCast(@alignCast(context.?)); const t: *Target = @ptrCast(@alignCast(handle.?)); // A copy into it may still be in flight. self.device.deviceWaitIdle() catch {}; for (self.awaiting.items, 0..) |b, i| if (target(b) == t) { _ = self.awaiting.swapRemove(i); break; }; self.dropTimeline(t); switch (t.memory) { inline else => |*x| x.destroy(self.device, self.vk_alloc), } self.allocator.destroy(t); } fn dropTimeline(self: *Dma, t: *Target) void { if (t.wl_timeline) |tl| tl.destroy(self.nw.connection) catch {}; if (t.timeline != .null_handle) self.device.destroySemaphore(t.timeline, self.vk_alloc); t.wl_timeline = null; t.timeline = .null_handle; } }; pub const QueueFamilyCandidate = struct { index: u32, properties: vk.QueueFamilyProperties, present_support: bool, }; /// Information passed to an optional physical-device selector. The backend has /// already checked its Vulkan version, extension, surface, and feature needs. pub const DeviceCandidate = struct { instance: vk.InstanceProxy, physical_device: vk.PhysicalDevice, properties: vk.PhysicalDeviceProperties, features: vk.PhysicalDeviceFeatures, /// Borrowed for the duration of `init`. queue_families: []const QueueFamilyCandidate, graphics_queue_family: u32, present_queue_family: u32, }; pub const DeviceSelector = struct { userdata: ?*anyopaque = null, /// Choose an index from `candidates`. Null chooses the first candidate. select: ?*const fn (userdata: ?*anyopaque, candidates: []const DeviceCandidate) ?usize = null, /// Called once for the selected device before device creation. Counts are /// indexed by queue family and already include DVUI's graphics/present /// queues. They may be increased, but not decreased. configure_queues: ?*const fn (userdata: ?*anyopaque, candidate: DeviceCandidate, queue_counts: []u32) anyerror!void = null, }; pub const InitOptions = struct { size_physical: dvui.Size.Physical = .{ .w = 640, .h = 480 }, vsync: bool = true, vk_alloc: ?*vk.AllocationCallbacks = null, /// Maximum number of indices that can be submitted in a single frame. max_indices_per_frame: u32 = 1024 * 256, /// Maximum number of vertices that can be submitted in a single frame. max_vertices_per_frame: u32 = 1024 * 96, /// Maximum number of live textures, including render targets, across all /// frames in flight. max_textures: TextureIdx = 256, /// Optional GPU allocation strategy for renderer-owned buffers and images. /// A custom allocator's callback userdata must remain valid until `deinit` /// returns. When null, the backend derives and uses its built-in allocator. gpu_allocator: ?GpuAllocator = null, color_formats: []const vk.Format = &.{ .a2b10g10r10_unorm_pack32, .a2r10g10b10_unorm_pack32, .b8g8r8a8_unorm, }, instance_extensions: []const [*:0]const u8 = &.{}, device_extensions: []const [*:0]const u8 = &.{}, device_features: vk.PhysicalDeviceFeatures = .{}, /// Optional Vulkan feature structs chained to VkDeviceCreateInfo. The /// pointed-to structs only need to remain alive for the duration of init. device_features_p_next: ?*const anyopaque = null, select_device: ?DeviceSelector = null, /// Vulkan version requested from the loader and physical device. Versions /// older than 1.2 are not supported by this backend. api_version: vk.Version = default_api_version, /// Dynamic rendering requires Vulkan 1.3 and is enabled automatically. rendering: RenderingMode = .render_pass, /// color_attachment_bit is added automatically and all requested usages /// are validated against the selected surface. swapchain_image_usage: vk.ImageUsageFlags = .{}, /// When set, the backend creates and clears one depth attachment per /// swapchain image and includes it in the application/DVUI render pass. depth_format: ?vk.Format = null, /// A swapchain, or a framebuffer when Vulkan cannot present to the window /// itself. With `.framebuffer`, `color_formats` is not consulted: the /// image is B8G8R8A8, which is what a framebuffer's pixels are. presentation: Presentation = .swapchain, }; const QueueFamilies = struct { graphics: u32, present: u32, }; const SelectedDevice = struct { device: vk.PhysicalDevice, families: QueueFamilies, portability_subset: bool, /// Set when the device is the compositor's and can share dma-bufs with /// it; see `Presentation.dmabuf`. dmabuf_support: ?dmabuf.Support = null, queue_counts: []u32, }; const DeviceExtensionSupport = struct { swapchain: bool = false, portability_subset: bool = false, }; const Slot = struct { command_buffer: vk.CommandBuffer = .null_handle, image_available: vk.Semaphore = .null_handle, fence: vk.Fence = .null_handle, }; const VulkanResources = struct { instance_wrapper: *vk.InstanceWrapper, instance: vk.InstanceProxy, physical_device: vk.PhysicalDevice, device_wrapper: *vk.DeviceWrapper, device: vk.DeviceProxy, graphics_queue: vk.QueueProxy, present_queue: vk.QueueProxy, queue_families: QueueFamilies, command_pool: vk.CommandPool, surface: vk.SurfaceKHR, dmabuf_support: ?dmabuf.Support, fn deinit(self: *@This(), allocator: std.mem.Allocator, vk_alloc: ?*vk.AllocationCallbacks) void { self.device.destroyCommandPool(self.command_pool, vk_alloc); self.device.destroyDevice(vk_alloc); allocator.destroy(self.device_wrapper); if (self.surface != .null_handle) self.instance.destroySurfaceKHR(self.surface, vk_alloc); self.instance.destroyInstance(vk_alloc); allocator.destroy(self.instance_wrapper); } }; const ReadbackSubmitContext = struct { device: vk.DeviceProxy, queue: vk.Queue, }; fn submitReadbackAndWait(userdata: ?*anyopaque, command_buffer: vk.CommandBuffer) !void { const context: *ReadbackSubmitContext = @ptrCast(@alignCast(userdata orelse return error.MissingReadbackContext)); try context.device.queueSubmit(context.queue, &.{.{ .command_buffer_count = 1, .p_command_buffers = &.{command_buffer}, }}, .null_handle); try context.device.queueWaitIdle(context.queue); } allocator: std.mem.Allocator, window: *wio.Window, vk_alloc: ?*vk.AllocationCallbacks, instance_wrapper: *vk.InstanceWrapper, instance: vk.InstanceProxy, physical_device: vk.PhysicalDevice, device_wrapper: *vk.DeviceWrapper, device: vk.DeviceProxy, graphics_queue: vk.QueueProxy, present_queue: vk.QueueProxy, queue_families: QueueFamilies, command_pool: vk.CommandPool, surface: vk.SurfaceKHR, requested_api_version: vk.Version, rendering_mode: RenderingMode, presentation: Presentation, /// For `.framebuffer`: the memory behind the one image drawn into, the /// host-visible buffer it is copied to, and the window's framebuffer. offscreen_memory: vk.DeviceMemory = .null_handle, readback_buffer: vk.Buffer = .null_handle, readback_memory: vk.DeviceMemory = .null_handle, readback_pixels: []const u32 = &.{}, readback_coherent: bool = true, window_framebuffer: if (wio.build_options.framebuffer) ?wio.Framebuffer else void = if (wio.build_options.framebuffer) null else {}, /// For `.dmabuf`. dma: ?*Dma = null, swapchain: vk.SwapchainKHR = .null_handle, color_format: vk.Format = .undefined, color_space: vk.ColorSpaceKHR = .srgb_nonlinear_khr, extent: vk.Extent2D = .{ .width = 1, .height = 1 }, images: []vk.Image = &.{}, image_layouts: []vk.ImageLayout = &.{}, image_views: []vk.ImageView = &.{}, depth_images: []vk.Image = &.{}, depth_image_views: []vk.ImageView = &.{}, depth_memories: []vk.DeviceMemory = &.{}, depth_layouts: []vk.ImageLayout = &.{}, framebuffers: []vk.Framebuffer = &.{}, image_fences: []vk.Fence = &.{}, render_finished: []vk.Semaphore = &.{}, slots: []Slot = &.{}, slot_index: usize = 0, render_pass: vk.RenderPass, renderer: Renderer, preferred_formats: []vk.Format, swapchain_image_usage: vk.ImageUsageFlags, depth_format: ?vk.Format, swapchain_generation: u64 = 0, vsync: bool, needs_recreate: bool = false, frame_active: bool = false, dvui_frame_active: bool = false, previous_stats: RenderStats = .{}, pending_present: bool = false, current_image: u32 = 0, current_slot: usize = 0, readback_submit_context: ?*ReadbackSubmitContext = null, pub fn init(allocator: std.mem.Allocator, window: *wio.Window, options: InitOptions) !@This() { // `.dmabuf` needs the compositor's feedback, and its device, before a // device is chosen; without them it is `.framebuffer` from the start. var nw: NativeWayland = undefined; const dmabuf_device: ?u64 = if (options.presentation != .dmabuf) null else if (comptime !native_wayland) null else blk: { nw = (window.nativeWayland() catch null) orelse break :blk null; const d = nw.dmabuf orelse break :blk null; break :blk d.mainDevice(); }; const owned = blk: { var resources = try initVulkanResources(allocator, window, options, dmabuf_device); errdefer resources.deinit(allocator, options.vk_alloc); const preferred_formats = try allocator.dupe(vk.Format, options.color_formats); break :blk .{ .resources = resources, .preferred_formats = preferred_formats }; }; const resources = owned.resources; var self: @This() = .{ .allocator = allocator, .window = window, .vk_alloc = options.vk_alloc, .instance_wrapper = resources.instance_wrapper, .instance = resources.instance, .physical_device = resources.physical_device, .device_wrapper = resources.device_wrapper, .device = resources.device, .graphics_queue = resources.graphics_queue, .present_queue = resources.present_queue, .queue_families = resources.queue_families, .command_pool = resources.command_pool, .surface = resources.surface, .requested_api_version = options.api_version, .rendering_mode = options.rendering, .presentation = if (options.presentation == .dmabuf and resources.dmabuf_support == null) .framebuffer else options.presentation, .render_pass = .null_handle, .renderer = undefined, .preferred_formats = owned.preferred_formats, .swapchain_image_usage = options.swapchain_image_usage.merge(.{ .color_attachment_bit = true }), .depth_format = options.depth_format, .vsync = options.vsync, }; errdefer self.deinitBeforeRenderer(); if (self.presentation == .dmabuf) { if (comptime !native_wayland) unreachable; self.dma = try allocator.create(Dma); self.dma.?.* = .{ .allocator = allocator, .vk_alloc = options.vk_alloc, .instance = resources.instance, .physical_device = resources.physical_device, .device = resources.device, .support = resources.dmabuf_support.?, .nw = nw, }; for (&self.dma.?.wait, &self.dma.?.done) |*wait, *done| { wait.* = try resources.device.createSemaphore(&.{}, options.vk_alloc); const exportable: vk.ExportSemaphoreCreateInfo = .{ .handle_types = .{ .sync_fd_bit = true } }; done.* = try resources.device.createSemaphore(&.{ .p_next = &exportable }, options.vk_alloc); } } const readback_submit_context = try allocator.create(ReadbackSubmitContext); readback_submit_context.* = .{ .device = resources.device, .queue = resources.graphics_queue.handle, }; self.readback_submit_context = readback_submit_context; if (self.presentation == .dmabuf) self.startDmabuf(sizeToExtent(options.size_physical)) catch |err| { log.info("presenting through shared memory: the compositor will not take a dma-buf ({t})", .{err}); self.stopDmabuf(); self.presentation = .framebuffer; }; try self.createTarget(sizeToExtent(options.size_physical)); if (options.depth_format) |format| try validateDepthFormat(resources.instance, resources.physical_device, format); if (options.rendering == .render_pass) { self.render_pass = try createRenderPass(resources.device, self.color_format, options.depth_format, self.finalLayout(), options.vk_alloc); } try self.createFramebuffers(); const properties = resources.instance.getPhysicalDeviceProperties(resources.physical_device); const color_formats = [_]vk.Format{self.color_format}; const main_target: Renderer.MainTarget = switch (options.rendering) { .render_pass => .{ .static = .{ .render_pass = self.render_pass } }, .dynamic => .{ .dynamic = .{ .info = .{ .view_mask = 0, .color_attachment_count = 1, .p_color_attachment_formats = &color_formats, .depth_attachment_format = if (options.depth_format) |format| if (hasDepth(format)) format else .undefined else .undefined, .stencil_attachment_format = if (options.depth_format) |format| if (hasStencil(format)) format else .undefined else .undefined, } } }, }; self.renderer = try Renderer.init(allocator, .{ .dev = resources.device, .graphics_queue_family_index = resources.queue_families.graphics, .submit_readback = .{ .userdata = readback_submit_context, .submit = submitReadbackAndWait, }, .gpu_allocator = options.gpu_allocator orelse .{ .builtin = Renderer.VkMemory.init( resources.instance.getPhysicalDeviceMemoryProperties(resources.physical_device), properties.limits.non_coherent_atom_size, ) orelse return error.NoCompatibleMemoryType }, .render_pass = main_target, .max_frames_in_flight = frames_in_flight, .max_indices_per_frame = options.max_indices_per_frame, .max_vertices_per_frame = options.max_vertices_per_frame, .max_textures = options.max_textures, .vk_alloc = options.vk_alloc, }); return self; } pub fn deinit(self: *@This()) void { self.device.deviceWaitIdle() catch {}; self.renderer.deinit(); self.deinitBeforeRenderer(); self.* = undefined; } fn deinitBeforeRenderer(self: *@This()) void { self.device.deviceWaitIdle() catch {}; self.stopDmabuf(); if (self.readback_submit_context) |context| self.allocator.destroy(context); self.readback_submit_context = null; self.destroyFramebuffers(); if (self.render_pass != .null_handle) self.device.destroyRenderPass(self.render_pass, self.vk_alloc); self.destroyTarget(); if (self.preferred_formats.len != 0) self.allocator.free(self.preferred_formats); if (self.command_pool != .null_handle) self.device.destroyCommandPool(self.command_pool, self.vk_alloc); self.device.destroyDevice(self.vk_alloc); self.allocator.destroy(self.device_wrapper); if (self.surface != .null_handle) self.instance.destroySurfaceKHR(self.surface, self.vk_alloc); self.instance.destroyInstance(self.vk_alloc); self.allocator.destroy(self.instance_wrapper); } pub fn beginWithSize(self: *@This(), arena: std.mem.Allocator, physical_size: dvui.Size.Physical) dvui.Backend.GenericError!void { return self.beginInternal(arena, physical_size) catch |err| return mapGenericError(err); } fn beginInternal(self: *@This(), arena: std.mem.Allocator, physical_size: dvui.Size.Physical) !void { if (!self.frame_active) _ = try self.beginApplicationFrame(physical_size); if (!self.frame_active) return; if (self.dvui_frame_active) return error.FrameAlreadyActive; try self.renderer.beginFrame(self.slots[self.current_slot].command_buffer, self.extent); try self.renderer.beginWithSize(arena, physical_size); self.dvui_frame_active = true; } /// Borrowed Vulkan handles for creating application-owned resources. pub const VulkanContext = struct { instance: vk.InstanceProxy, physical_device: vk.PhysicalDevice, device: vk.DeviceProxy, graphics_queue: vk.QueueProxy, present_queue: vk.QueueProxy, graphics_queue_family: u32, present_queue_family: u32, api_version: vk.Version, rendering_target: RenderingTarget, /// Render-pass handle when using classic render-pass mode; null when using /// dynamic rendering. Switch on `rendering_target` to support both modes. render_pass: vk.RenderPass, color_format: vk.Format, depth_format: ?vk.Format, }; /// Handles exposed for application rendering at the start of a Vulkan frame. /// Commands recorded here execute before DVUI commands recorded by `Window.begin`. pub const ApplicationFrame = struct { device: vk.DeviceProxy, command_buffer: vk.CommandBuffer, rendering_target: RenderingTarget, /// Render-pass handle when using classic render-pass mode; null when using /// dynamic rendering. Switch on `rendering_target` to support both modes. render_pass: vk.RenderPass, extent: vk.Extent2D, image: vk.Image, image_view: vk.ImageView, image_index: u32, swapchain_generation: u64, }; pub const DynamicRenderingTarget = struct { color_format: vk.Format, depth_format: vk.Format, stencil_format: vk.Format, samples: vk.SampleCountFlags = .{ .@"1_bit" = true }, }; pub const RenderingTarget = union(enum) { render_pass: vk.RenderPass, dynamic: DynamicRenderingTarget, }; /// Acquire a swapchain image and begin its render pass or dynamic-rendering /// scope so the application can render before DVUI. Call `Window.begin` /// afterwards to append the DVUI draw. /// Returns null while the drawable has no usable extent or is being recreated. pub fn beginApplicationFrame(self: *@This(), physical_size: dvui.Size.Physical) !?ApplicationFrame { if (self.frame_active) return error.FrameAlreadyActive; if (self.pending_present) self.present() catch |err| log.err("presenting delayed Vulkan frame failed: {}", .{err}); if (physical_size.w < 1 or physical_size.h < 1) return null; const requested = sizeToExtent(physical_size); if (self.needs_recreate or requested.width != self.extent.width or requested.height != self.extent.height) { try self.recreate(requested); } const slot = &self.slots[self.slot_index]; _ = try self.device.waitForFences(&.{slot.fence}, .true, std.math.maxInt(u64)); // Drawn into the one image there is, once the frame before is done with // it: the fence below. if (self.presentation == .dmabuf) try self.acquireDmabuf(); const acquired: struct { image_index: u32 } = if (self.presentation != .swapchain) .{ .image_index = 0 } else blk: { const got = self.device.acquireNextImageKHR(self.swapchain, std.math.maxInt(u64), slot.image_available, .null_handle) catch |err| switch (err) { error.OutOfDateKHR => { self.needs_recreate = true; return null; }, else => return err, }; if (got.result == .suboptimal_khr) self.needs_recreate = true; break :blk .{ .image_index = got.image_index }; }; if (self.image_fences[acquired.image_index] != .null_handle) { _ = try self.device.waitForFences(&.{self.image_fences[acquired.image_index]}, .true, std.math.maxInt(u64)); } try self.device.resetCommandBuffer(slot.command_buffer, .{}); try self.device.beginCommandBuffer(slot.command_buffer, &.{ .flags = .{ .one_time_submit_bit = true } }); switch (self.rendering_mode) { .render_pass => { const clear_values = [_]vk.ClearValue{ .{ .color = .{ .float_32 = .{ 0, 0, 0, 1 } } }, .{ .depth_stencil = .{ .depth = 1, .stencil = 0 } }, }; self.device.cmdBeginRenderPass(slot.command_buffer, &.{ .render_pass = self.render_pass, .framebuffer = self.framebuffers[acquired.image_index], .render_area = .{ .offset = .{ .x = 0, .y = 0 }, .extent = self.extent }, .clear_value_count = if (self.depth_format == null) 1 else 2, .p_clear_values = &clear_values, }, .@"inline"); }, .dynamic => self.beginDynamicRendering(slot.command_buffer, acquired.image_index), } self.current_image = acquired.image_index; self.current_slot = self.slot_index; self.frame_active = true; return .{ .device = self.device, .command_buffer = slot.command_buffer, .rendering_target = self.renderingTarget(), .render_pass = self.render_pass, .extent = self.extent, .image = self.images[acquired.image_index], .image_view = self.image_views[acquired.image_index], .image_index = acquired.image_index, .swapchain_generation = self.swapchain_generation, }; } /// Borrowed Vulkan handles for creating application-owned resources. They are /// valid until this renderer is deinitialized. pub fn vulkanContext(self: *const @This()) VulkanContext { return .{ .instance = self.instance, .physical_device = self.physical_device, .device = self.device, .graphics_queue = self.graphics_queue, .present_queue = self.present_queue, .graphics_queue_family = self.queue_families.graphics, .present_queue_family = self.queue_families.present, .api_version = self.requested_api_version, .rendering_target = self.renderingTarget(), .render_pass = self.render_pass, .color_format = self.color_format, .depth_format = self.depth_format, }; } pub fn swapchainGeneration(self: *const @This()) u64 { return self.swapchain_generation; } pub fn vulkanDevice(self: *const @This()) vk.DeviceProxy { return self.device; } pub fn vulkanRenderPass(self: *const @This()) vk.RenderPass { return self.render_pass; } pub fn vulkanAllocationCallbacks(self: *const @This()) ?*vk.AllocationCallbacks { return self.vk_alloc; } fn renderingTarget(self: *const @This()) RenderingTarget { return switch (self.rendering_mode) { .render_pass => .{ .render_pass = self.render_pass }, .dynamic => .{ .dynamic = .{ .color_format = self.color_format, .depth_format = if (self.depth_format) |format| if (hasDepth(format)) format else .undefined else .undefined, .stencil_format = if (self.depth_format) |format| if (hasStencil(format)) format else .undefined else .undefined, } }, }; } fn beginDynamicRendering(self: *@This(), command_buffer: vk.CommandBuffer, image_index: u32) void { var barriers: [2]vk.ImageMemoryBarrier = undefined; var barrier_count: usize = 1; const old_color_layout = self.image_layouts[image_index]; barriers[0] = .{ .src_access_mask = .{}, .dst_access_mask = .{ .color_attachment_read_bit = true, .color_attachment_write_bit = true }, .old_layout = old_color_layout, .new_layout = .color_attachment_optimal, .src_queue_family_index = vk.QUEUE_FAMILY_IGNORED, .dst_queue_family_index = vk.QUEUE_FAMILY_IGNORED, .image = self.images[image_index], .subresource_range = colorRange(), }; if (self.depth_format) |format| { const old_depth_layout = self.depth_layouts[image_index]; barriers[1] = .{ .src_access_mask = if (old_depth_layout == .undefined) .{} else .{ .depth_stencil_attachment_write_bit = true }, .dst_access_mask = .{ .depth_stencil_attachment_read_bit = true, .depth_stencil_attachment_write_bit = true }, .old_layout = old_depth_layout, .new_layout = .depth_stencil_attachment_optimal, .src_queue_family_index = vk.QUEUE_FAMILY_IGNORED, .dst_queue_family_index = vk.QUEUE_FAMILY_IGNORED, .image = self.depth_images[image_index], .subresource_range = depthRange(format), }; barrier_count = 2; self.depth_layouts[image_index] = .depth_stencil_attachment_optimal; } self.device.cmdPipelineBarrier( command_buffer, .{ .top_of_pipe_bit = true, .color_attachment_output_bit = true, .early_fragment_tests_bit = true, .late_fragment_tests_bit = true }, .{ .color_attachment_output_bit = true, .early_fragment_tests_bit = true, .late_fragment_tests_bit = true }, .{}, null, null, barriers[0..barrier_count], ); self.image_layouts[image_index] = .color_attachment_optimal; const color_attachment = vk.RenderingAttachmentInfo{ .image_view = self.image_views[image_index], .image_layout = .color_attachment_optimal, .resolve_mode = .{}, .resolve_image_layout = .color_attachment_optimal, .load_op = .clear, .store_op = .store, .clear_value = .{ .color = .{ .float_32 = .{ 0, 0, 0, 1 } } }, }; const depth_attachment = vk.RenderingAttachmentInfo{ .image_view = if (self.depth_format == null) .null_handle else self.depth_image_views[image_index], .image_layout = .depth_stencil_attachment_optimal, .resolve_mode = .{}, .resolve_image_layout = .depth_stencil_attachment_optimal, .load_op = .clear, .store_op = .dont_care, .clear_value = .{ .depth_stencil = .{ .depth = 1, .stencil = 0 } }, }; self.device.cmdBeginRendering(command_buffer, &.{ .render_area = .{ .offset = .{ .x = 0, .y = 0 }, .extent = self.extent }, .layer_count = 1, .view_mask = 0, .color_attachment_count = 1, .p_color_attachments = @ptrCast(&color_attachment), .p_depth_attachment = if (self.depth_format) |format| if (hasDepth(format)) &depth_attachment else null else null, .p_stencil_attachment = if (self.depth_format) |format| if (hasStencil(format)) &depth_attachment else null else null, }); } fn endDynamicRendering(self: *@This(), command_buffer: vk.CommandBuffer, image_index: u32) void { const barrier = vk.ImageMemoryBarrier{ .src_access_mask = .{ .color_attachment_write_bit = true }, .dst_access_mask = .{}, .old_layout = .color_attachment_optimal, .new_layout = self.finalLayout(), .src_queue_family_index = vk.QUEUE_FAMILY_IGNORED, .dst_queue_family_index = vk.QUEUE_FAMILY_IGNORED, .image = self.images[image_index], .subresource_range = colorRange(), }; self.device.cmdPipelineBarrier(command_buffer, .{ .color_attachment_output_bit = true }, .{ .bottom_of_pipe_bit = true }, .{}, null, null, &.{barrier}); self.image_layouts[image_index] = self.finalLayout(); } pub fn end(self: *@This()) dvui.Backend.GenericError!void { return self.endInternal() catch |err| return mapGenericError(err); } fn endInternal(self: *@This()) !void { if (!self.frame_active) return; const slot = &self.slots[self.current_slot]; const recorded = if (self.dvui_frame_active) blk: { const frame = try self.renderer.endFrame(); self.previous_stats = self.renderer.statsSnapshot(); break :blk frame; } else Renderer.RecordedFrame{ .prepass = null, .main = slot.command_buffer }; switch (self.rendering_mode) { .render_pass => self.device.cmdEndRenderPass(slot.command_buffer), .dynamic => { self.device.cmdEndRendering(slot.command_buffer); self.endDynamicRendering(slot.command_buffer, self.current_image); }, } switch (self.presentation) { .swapchain => {}, .framebuffer => self.recordReadback(slot.command_buffer), .dmabuf => self.recordDmabufCopy(slot.command_buffer), } try self.device.endCommandBuffer(slot.command_buffer); try self.device.resetFences(&.{slot.fence}); var command_buffers: [2]vk.CommandBuffer = undefined; var command_count: u32 = 1; command_buffers[0] = slot.command_buffer; if (recorded.prepass) |prepass| { command_buffers[0] = prepass; command_buffers[1] = slot.command_buffer; command_count = 2; } // A swapchain image waits to be acquired and signals when it may be // presented; a dma-buf waits for the compositor's reads and signals the // copy into it. var wait: ?vk.Semaphore = null; var signal: ?vk.Semaphore = null; // Explicit sync: the buffer's timeline, at its next acquire point. var signal_value: u64 = 0; switch (self.presentation) { .swapchain => { wait = slot.image_available; signal = self.render_finished[self.current_image]; }, .framebuffer => {}, .dmabuf => if (comptime native_wayland) { const dma = self.dma.?; if (dma.sync != null) { const t = Dma.target(dma.current.?); signal = t.timeline; signal_value = t.point + 1; } else { if (dma.waiting[self.current_slot]) wait = dma.wait[self.current_slot]; dma.waiting[self.current_slot] = false; signal = dma.done[self.current_slot]; } }, } const timeline_values: vk.TimelineSemaphoreSubmitInfo = .{ .signal_semaphore_value_count = 1, .p_signal_semaphore_values = &.{signal_value}, }; try self.device.queueSubmit(self.graphics_queue.handle, &.{.{ .p_next = if (signal_value != 0) &timeline_values else null, .wait_semaphore_count = @intFromBool(wait != null), .p_wait_semaphores = &.{wait orelse .null_handle}, .p_wait_dst_stage_mask = &.{if (self.presentation == .swapchain) .{ .color_attachment_output_bit = true } else .{ .transfer_bit = true }}, .command_buffer_count = command_count, .p_command_buffers = &command_buffers, .signal_semaphore_count = @intFromBool(signal != null), .p_signal_semaphores = &.{signal orelse .null_handle}, }}, slot.fence); if (self.presentation == .dmabuf) self.fenceDmabuf(); self.image_fences[self.current_image] = slot.fence; self.pending_present = true; self.frame_active = false; self.dvui_frame_active = false; } pub fn stats(self: *const @This()) RenderStats { return self.previous_stats; } /// Request vsync on or off. The swapchain is recreated safely at the start of /// the next frame; disabling vsync may still fall back to FIFO when necessary. pub fn setVsync(self: *@This(), enabled: bool) void { if (self.vsync == enabled) return; self.vsync = enabled; self.needs_recreate = true; } pub fn vsyncEnabled(self: *const @This()) bool { return self.vsync; } /// Draw renderer statistics into the current DVUI container. pub fn drawStats(self: *@This()) void { const snapshot = self.stats(); const h2 = dvui.Font.theme(.body).larger(-2).withWeight(.bold).withLineHeight(1.1); var vsync = self.vsyncEnabled(); if (dvui.checkbox(@src(), &vsync, "VSync", .{})) self.setVsync(vsync); dvui.label(@src(), "draw calls: {}", .{snapshot.draw_calls}, .{ .expand = .horizontal }); drawStatsUsageRow("indices", snapshot.indices, snapshot.index_capacity, 0); drawStatsUsageRow("vertices", snapshot.vertices, snapshot.vertex_capacity, 1); dvui.labelNoFmt(@src(), "Textures", .{}, .{ .font = h2, .expand = .horizontal }); dvui.label(@src(), "count: {}", .{snapshot.textures_alive}, .{ .expand = .horizontal }); dvui.label(@src(), "GPU memory: {Bi:.1}", .{snapshot.texture_memory}, .{ .expand = .horizontal }); dvui.labelNoFmt(@src(), "Preallocated streaming memory", .{}, .{ .font = h2, .expand = .horizontal }); dvui.label(@src(), "total: {Bi:.1}", .{snapshot.stream_memory_total}, .{ .expand = .horizontal }); dvui.label(@src(), "frame: {Bi:.1} / {Bi:.1}", .{ snapshot.stream_memory_used, snapshot.stream_memory_per_frame }, .{ .expand = .horizontal }); drawStatsUsageBar(snapshot.stream_memory_used, snapshot.stream_memory_per_frame, 2); } /// Draw renderer statistics in a floating window. `rect`, when provided, must /// remain valid across frames so the window can retain its position and size. pub fn drawStatsWindow(self: *@This(), rect: ?*dvui.Rect) void { var float = dvui.floatingWindow(@src(), .{ .rect = rect }, .{ .min_size_content = .{ .w = 300, .h = 0 }, .id_extra = @intFromPtr(self) }); defer float.deinit(); float.dragAreaSet(dvui.windowHeader("Vulkan Renderer Stats", "", null)); self.drawStats(); } fn drawStatsUsageRow(comptime label: []const u8, used: anytype, capacity: usize, id_extra: usize) void { dvui.label(@src(), label ++ ": {} / {}", .{ used, capacity }, .{ .expand = .horizontal, .id_extra = id_extra }); drawStatsUsageBar(used, capacity, id_extra); } fn drawStatsUsageBar(used: anytype, capacity: usize, id_extra: usize) void { const percent = if (capacity == 0) 0 else @as(f32, @floatFromInt(used)) / @as(f32, @floatFromInt(capacity)); const original = dvui.themeGet().highlight.fill; defer dvui.currentWindow().theme.highlight.fill = original; dvui.currentWindow().theme.highlight.fill = dvui.Color.fromHSLuv(@max(12, (1 - percent * percent) * 155), 99, 50, 100); dvui.progress(@src(), .{ .percent = percent }, .{ .expand = .horizontal, .id_extra = id_extra }); } pub fn renderPresent(self: *@This()) void { self.present() catch |err| switch (err) { error.OutOfDateKHR => self.needs_recreate = true, else => log.err("Vulkan present failed: {}", .{err}), }; } fn present(self: *@This()) !void { if (!self.pending_present) return; defer self.pending_present = false; switch (self.presentation) { .swapchain => {}, .framebuffer => return self.presentFramebuffer(), .dmabuf => return self.presentDmabuf(), } const result = try self.device.queuePresentKHR(self.present_queue.handle, &.{ .wait_semaphore_count = 1, .p_wait_semaphores = &.{self.render_finished[self.current_image]}, .swapchain_count = 1, .p_swapchains = &.{self.swapchain}, .p_image_indices = &.{self.current_image}, }); self.slot_index = (self.current_slot + 1) % self.slots.len; if (result == .suboptimal_khr) self.needs_recreate = true; } pub fn clear(_: *@This()) void {} pub fn drawClippedTriangles(self: *@This(), size: dvui.Size.Physical, texture: ?dvui.Texture, vertices: []const dvui.Vertex, indices: []const dvui.Vertex.Index, clip: ?dvui.Rect.Physical) dvui.Backend.GenericError!void { if (self.frame_active) try self.renderer.drawClippedTriangles(size, texture, vertices, indices, clip); } pub fn textureCreate(self: *@This(), pixels: [*]const u8, options: dvui.Texture.CreateOptions) dvui.Backend.TextureError!dvui.Texture { return self.renderer.textureCreate(pixels, options); } pub fn textureDestroy(self: *@This(), texture: dvui.Texture) void { self.renderer.textureDestroy(texture); } pub fn textureCreateTarget(self: *@This(), options: dvui.Texture.CreateOptions) dvui.Backend.TextureError!dvui.TextureTarget { return self.renderer.textureCreateTarget(options); } pub fn textureReadTarget(self: *@This(), texture: dvui.TextureTarget, pixels: [*]u8) dvui.Backend.TextureError!void { return self.renderer.textureReadTarget(texture, pixels); } pub fn textureClearTarget(self: *@This(), texture: dvui.Texture.Target) void { self.renderer.textureClearTarget(texture) catch |err| log.err("clearing Vulkan texture target failed: {}", .{err}); } pub fn textureDestroyTarget(self: *@This(), texture: dvui.Texture.Target) void { self.renderer.textureDestroyTarget(texture); } pub fn textureFromTarget(self: *@This(), texture: dvui.TextureTarget) dvui.Backend.TextureError!dvui.Texture { return self.renderer.textureFromTarget(texture); } pub fn textureFromTargetTemp(_: *@This(), texture: dvui.TextureTarget) dvui.Backend.TextureError!dvui.Texture { return dvui.Texture.cast(texture); } pub fn renderTarget(self: *@This(), texture: ?dvui.TextureTarget) dvui.Backend.GenericError!void { return self.renderer.renderTarget(texture) catch |err| return mapGenericError(err); } fn recreate(self: *@This(), requested: vk.Extent2D) !void { if (requested.width == 0 or requested.height == 0) return; try self.device.deviceWaitIdle(); self.destroyFramebuffers(); self.destroyTarget(); try self.createTarget(requested); try self.createFramebuffers(); self.needs_recreate = false; } fn createSwapchain(self: *@This(), requested: vk.Extent2D) !void { const capabilities = try self.instance.getPhysicalDeviceSurfaceCapabilitiesKHR(self.physical_device, self.surface); if (!capabilities.supported_usage_flags.contains(self.swapchain_image_usage)) return error.UnsupportedSwapchainImageUsage; self.extent = if (capabilities.current_extent.width != std.math.maxInt(u32)) capabilities.current_extent else .{ .width = std.math.clamp(requested.width, capabilities.min_image_extent.width, capabilities.max_image_extent.width), .height = std.math.clamp(requested.height, capabilities.min_image_extent.height, capabilities.max_image_extent.height), }; const formats = try self.instance.getPhysicalDeviceSurfaceFormatsAllocKHR(self.physical_device, self.surface, self.allocator); defer self.allocator.free(formats); const selected_format = chooseColorFormat(formats, if (self.color_format == .undefined) self.preferred_formats else &.{self.color_format}); if (self.color_format != .undefined and selected_format.format != self.color_format) return error.SurfaceFormatChanged; self.color_format = selected_format.format; self.color_space = selected_format.color_space; const modes = try self.instance.getPhysicalDeviceSurfacePresentModesAllocKHR(self.physical_device, self.surface, self.allocator); defer self.allocator.free(modes); const image_count = if (capabilities.max_image_count == 0) capabilities.min_image_count + 1 else @min(capabilities.min_image_count + 1, capabilities.max_image_count); const family_indices = [_]u32{ self.queue_families.graphics, self.queue_families.present }; self.swapchain = try self.device.createSwapchainKHR(&.{ .surface = self.surface, .min_image_count = image_count, .image_format = self.color_format, .image_color_space = self.color_space, .image_extent = self.extent, .image_array_layers = 1, .image_usage = self.swapchain_image_usage, .image_sharing_mode = if (self.queue_families.graphics == self.queue_families.present) .exclusive else .concurrent, .queue_family_index_count = if (self.queue_families.graphics == self.queue_families.present) 0 else 2, .p_queue_family_indices = if (self.queue_families.graphics == self.queue_families.present) null else &family_indices, .pre_transform = capabilities.current_transform, .composite_alpha = chooseCompositeAlpha(capabilities.supported_composite_alpha), .present_mode = choosePresentMode(modes, self.vsync), .clipped = .true, }, self.vk_alloc); errdefer self.destroySwapchain(); self.images = try self.device.getSwapchainImagesAllocKHR(self.swapchain, self.allocator); self.image_layouts = try self.allocator.alloc(vk.ImageLayout, self.images.len); @memset(self.image_layouts, .undefined); self.image_views = try self.allocator.alloc(vk.ImageView, self.images.len); @memset(self.image_views, .null_handle); for (self.images, self.image_views) |image, *view| { view.* = try self.device.createImageView(&.{ .image = image, .view_type = .@"2d", .format = self.color_format, .components = .{ .r = .identity, .g = .identity, .b = .identity, .a = .identity }, .subresource_range = colorRange(), }, self.vk_alloc); } self.image_fences = try self.allocator.alloc(vk.Fence, self.images.len); @memset(self.image_fences, .null_handle); self.render_finished = try self.allocator.alloc(vk.Semaphore, self.images.len); @memset(self.render_finished, .null_handle); for (self.render_finished) |*semaphore| semaphore.* = try self.device.createSemaphore(&.{}, self.vk_alloc); self.slots = try self.allocator.alloc(Slot, frames_in_flight); @memset(self.slots, Slot{}); for (self.slots) |*slot| { slot.image_available = try self.device.createSemaphore(&.{}, self.vk_alloc); slot.fence = try self.device.createFence(&.{ .flags = .{ .signaled_bit = true } }, self.vk_alloc); try self.device.allocateCommandBuffers(&.{ .command_pool = self.command_pool, .level = .primary, .command_buffer_count = 1, }, @ptrCast(&slot.command_buffer)); } self.swapchain_generation += 1; } fn destroySwapchain(self: *@This()) void { for (self.slots) |slot| { if (slot.image_available != .null_handle) self.device.destroySemaphore(slot.image_available, self.vk_alloc); if (slot.fence != .null_handle) self.device.destroyFence(slot.fence, self.vk_alloc); if (slot.command_buffer != .null_handle) self.device.freeCommandBuffers(self.command_pool, &.{slot.command_buffer}); } for (self.render_finished) |semaphore| if (semaphore != .null_handle) self.device.destroySemaphore(semaphore, self.vk_alloc); for (self.image_views) |view| if (view != .null_handle) self.device.destroyImageView(view, self.vk_alloc); if (self.slots.len != 0) self.allocator.free(self.slots); if (self.render_finished.len != 0) self.allocator.free(self.render_finished); if (self.image_fences.len != 0) self.allocator.free(self.image_fences); if (self.image_views.len != 0) self.allocator.free(self.image_views); if (self.image_layouts.len != 0) self.allocator.free(self.image_layouts); if (self.images.len != 0) self.allocator.free(self.images); if (self.swapchain != .null_handle) self.device.destroySwapchainKHR(self.swapchain, self.vk_alloc); self.slots = &.{}; self.render_finished = &.{}; self.image_fences = &.{}; self.image_views = &.{}; self.image_layouts = &.{}; self.images = &.{}; self.swapchain = .null_handle; self.slot_index = 0; } /// The layout a finished frame is left in: ready to present, or ready to /// copy out. fn finalLayout(self: *const @This()) vk.ImageLayout { return switch (self.presentation) { .swapchain => .present_src_khr, .framebuffer, .dmabuf => .transfer_src_optimal, }; } /// What frames are drawn into: the swapchain's images, or the one image of /// our own that is copied out. fn createTarget(self: *@This(), requested: vk.Extent2D) !void { switch (self.presentation) { .swapchain => try self.createSwapchain(requested), .framebuffer, .dmabuf => try self.createOffscreen(requested), } } fn destroyTarget(self: *@This()) void { switch (self.presentation) { .swapchain => self.destroySwapchain(), .framebuffer, .dmabuf => self.destroyOffscreen(), } } fn createOffscreen(self: *@This(), requested: vk.Extent2D) !void { errdefer self.destroyOffscreen(); // A wio framebuffer's sides are 16-bit. self.extent = .{ .width = @min(requested.width, std.math.maxInt(u16)), .height = @min(requested.height, std.math.maxInt(u16)), }; // A framebuffer's pixels are 0xRRGGBB words, which in memory is B, G, R // and a byte that is ignored: this format, read back as it is. self.color_format = .b8g8r8a8_unorm; self.color_space = .srgb_nonlinear_khr; const memory_properties = self.instance.getPhysicalDeviceMemoryProperties(self.physical_device); self.images = try self.allocator.alloc(vk.Image, 1); self.images[0] = .null_handle; self.images[0] = try self.device.createImage(&.{ .image_type = .@"2d", .format = self.color_format, .extent = .{ .width = self.extent.width, .height = self.extent.height, .depth = 1 }, .mip_levels = 1, .array_layers = 1, .samples = .{ .@"1_bit" = true }, .tiling = .optimal, .usage = self.swapchain_image_usage.merge(.{ .transfer_src_bit = true }), .sharing_mode = .exclusive, .initial_layout = .undefined, }, self.vk_alloc); const image_requirements = self.device.getImageMemoryRequirements(self.images[0]); const image_memory_type = findMemoryType(memory_properties, image_requirements.memory_type_bits, .{ .device_local_bit = true }) orelse findMemoryType(memory_properties, image_requirements.memory_type_bits, .{}) orelse return error.NoCompatibleMemoryType; self.offscreen_memory = try self.device.allocateMemory(&.{ .allocation_size = image_requirements.size, .memory_type_index = image_memory_type, }, self.vk_alloc); try self.device.bindImageMemory(self.images[0], self.offscreen_memory, 0); self.image_layouts = try self.allocator.alloc(vk.ImageLayout, 1); self.image_layouts[0] = .undefined; self.image_views = try self.allocator.alloc(vk.ImageView, 1); self.image_views[0] = .null_handle; self.image_views[0] = try self.device.createImageView(&.{ .image = self.images[0], .view_type = .@"2d", .format = self.color_format, .components = .{ .r = .identity, .g = .identity, .b = .identity, .a = .identity }, .subresource_range = colorRange(), }, self.vk_alloc); self.image_fences = try self.allocator.alloc(vk.Fence, 1); self.image_fences[0] = .null_handle; // Nothing to wait for or signal without a swapchain. self.render_finished = try self.allocator.alloc(vk.Semaphore, 1); self.render_finished[0] = .null_handle; if (self.presentation == .framebuffer) try self.createReadback(); self.slots = try self.allocator.alloc(Slot, frames_in_flight); @memset(self.slots, Slot{}); for (self.slots) |*slot| { slot.fence = try self.device.createFence(&.{ .flags = .{ .signaled_bit = true } }, self.vk_alloc); try self.device.allocateCommandBuffers(&.{ .command_pool = self.command_pool, .level = .primary, .command_buffer_count = 1, }, @ptrCast(&slot.command_buffer)); } self.swapchain_generation += 1; } /// Host memory the frame is copied to, and the framebuffer it is written to /// from there: `.framebuffer`. fn createReadback(self: *@This()) !void { if (comptime !wio.build_options.framebuffer) return error.FramebufferPresentationNeedsWioFramebuffer; const memory_properties = self.instance.getPhysicalDeviceMemoryProperties(self.physical_device); const byte_size: vk.DeviceSize = @as(vk.DeviceSize, self.extent.width) * self.extent.height * 4; self.readback_buffer = try self.device.createBuffer(&.{ .size = byte_size, .usage = .{ .transfer_dst_bit = true }, .sharing_mode = .exclusive, }, self.vk_alloc); const buffer_requirements = self.device.getBufferMemoryRequirements(self.readback_buffer); const buffer_memory_type = findMemoryType(memory_properties, buffer_requirements.memory_type_bits, .{ .host_visible_bit = true, .host_cached_bit = true }) orelse findMemoryType(memory_properties, buffer_requirements.memory_type_bits, .{ .host_visible_bit = true }) orelse return error.NoCompatibleMemoryType; self.readback_coherent = memory_properties.memory_types[buffer_memory_type].property_flags.host_coherent_bit; self.readback_memory = try self.device.allocateMemory(&.{ .allocation_size = buffer_requirements.size, .memory_type_index = buffer_memory_type, }, self.vk_alloc); try self.device.bindBufferMemory(self.readback_buffer, self.readback_memory, 0); const mapped = try self.device.mapMemory(self.readback_memory, 0, vk.WHOLE_SIZE, .{}) orelse return error.MapFailed; const words: [*]const u32 = @ptrCast(@alignCast(mapped)); self.readback_pixels = words[0 .. @as(usize, self.extent.width) * self.extent.height]; self.window_framebuffer = try self.window.createFramebuffer(.{ .width = @intCast(self.extent.width), .height = @intCast(self.extent.height), }); } fn destroyOffscreen(self: *@This()) void { if (comptime wio.build_options.framebuffer) { if (self.window_framebuffer) |*fb| fb.destroy(); self.window_framebuffer = null; } if (self.readback_pixels.len != 0) self.device.unmapMemory(self.readback_memory); self.readback_pixels = &.{}; if (self.readback_buffer != .null_handle) self.device.destroyBuffer(self.readback_buffer, self.vk_alloc); if (self.readback_memory != .null_handle) self.device.freeMemory(self.readback_memory, self.vk_alloc); self.readback_buffer = .null_handle; self.readback_memory = .null_handle; // The rest is what a swapchain has too, and is let go of the same way: // the views, then the image, which is ours here rather than the // swapchain's. const image: vk.Image = if (self.images.len != 0) self.images[0] else .null_handle; self.destroySwapchain(); if (image != .null_handle) self.device.destroyImage(image, self.vk_alloc); if (self.offscreen_memory != .null_handle) self.device.freeMemory(self.offscreen_memory, self.vk_alloc); self.offscreen_memory = .null_handle; } /// Copies the finished frame into the readback buffer, in the command buffer /// that drew it, so that one fence says both are done. fn recordReadback(self: *@This(), command_buffer: vk.CommandBuffer) void { // The render pass has left the image in TRANSFER_SRC_OPTIMAL; this makes // its writes visible to the copy. const ready = vk.ImageMemoryBarrier{ .src_access_mask = .{ .color_attachment_write_bit = true }, .dst_access_mask = .{ .transfer_read_bit = true }, .old_layout = .transfer_src_optimal, .new_layout = .transfer_src_optimal, .src_queue_family_index = vk.QUEUE_FAMILY_IGNORED, .dst_queue_family_index = vk.QUEUE_FAMILY_IGNORED, .image = self.images[0], .subresource_range = colorRange(), }; self.device.cmdPipelineBarrier(command_buffer, .{ .color_attachment_output_bit = true }, .{ .transfer_bit = true }, .{}, null, null, &.{ready}); self.device.cmdCopyImageToBuffer(command_buffer, self.images[0], .transfer_src_optimal, self.readback_buffer, &.{.{ .buffer_offset = 0, .buffer_row_length = 0, .buffer_image_height = 0, .image_subresource = .{ .aspect_mask = .{ .color_bit = true }, .mip_level = 0, .base_array_layer = 0, .layer_count = 1, }, .image_offset = .{ .x = 0, .y = 0, .z = 0 }, .image_extent = .{ .width = self.extent.width, .height = self.extent.height, .depth = 1 }, }}); const to_host = vk.BufferMemoryBarrier{ .src_access_mask = .{ .transfer_write_bit = true }, .dst_access_mask = .{ .host_read_bit = true }, .src_queue_family_index = vk.QUEUE_FAMILY_IGNORED, .dst_queue_family_index = vk.QUEUE_FAMILY_IGNORED, .buffer = self.readback_buffer, .offset = 0, .size = vk.WHOLE_SIZE, }; self.device.cmdPipelineBarrier(command_buffer, .{ .transfer_bit = true }, .{ .host_bit = true }, .{}, null, &.{to_host}, null); } /// Waits for the frame, and hands its pixels to the window. fn presentFramebuffer(self: *@This()) !void { if (comptime !wio.build_options.framebuffer) return error.FramebufferPresentationNeedsWioFramebuffer; const slot = &self.slots[self.current_slot]; _ = try self.device.waitForFences(&.{slot.fence}, .true, std.math.maxInt(u64)); if (!self.readback_coherent) try self.device.invalidateMappedMemoryRanges(&.{.{ .memory = self.readback_memory, .offset = 0, .size = vk.WHOLE_SIZE, }}); const fb = &(self.window_framebuffer orelse return); fb.write(self.readback_pixels, self.extent.width); self.window.presentFramebuffer(fb); self.slot_index = (self.current_slot + 1) % self.slots.len; } /// The pool of dma-bufs, and one of them made and given back: whether the /// compositor takes them is known before the first frame, while falling /// back is still simple. fn startDmabuf(self: *@This(), extent: vk.Extent2D) !void { if (comptime !native_wayland) unreachable; const dma = self.dma.?; const d = dma.nw.dmabuf.?; if (!d.supports(.xrgb8888)) return error.FormatNotOffered; // Explicit sync from the first buffer, since every buffer then needs a // timeline, and every commit points on one. if (dma.support.timelines) if (dma.nw.syncobj) |syncobj| { if (dma.nw.presenter.enableExplicitSync(syncobj)) |_| { dma.sync = syncobj; } else |err| log.info("no explicit sync ({t}); implicit instead", .{err}); }; dma.pool = try wl_present.BufferPool.createDmabuf(self.allocator, dma.nw.connection, d, .{ .context = dma, .allocate = Dma.allocate, .free = Dma.free, }, .{}); const first = (try dma.pool.?.acquire(extent.width, extent.height, .xrgb8888)) orelse return error.NoBuffer; const t = Dma.target(first); log.info("presenting through dma-bufs: {s}, modifier 0x{x}, {s} sync", .{ @tagName(t.memory), first.storage.dmabuf.modifier, if (dma.sync != null) "explicit" else "implicit", }); dma.pool.?.cancel(first); } fn stopDmabuf(self: *@This()) void { if (comptime !native_wayland) return; const dma = self.dma orelse return; if (dma.pool) |pool| { if (dma.current) |b| pool.cancel(b); pool.destroy(); } dma.awaiting.deinit(self.allocator); // What presents after this may be shared memory, which explicit sync // refuses. if (dma.sync != null) dma.nw.presenter.disableExplicitSync(); for (dma.wait ++ dma.done) |semaphore| { if (semaphore != .null_handle) self.device.destroySemaphore(semaphore, self.vk_alloc); } self.allocator.destroy(dma); self.dma = null; } /// A buffer the compositor has let go of, for this frame, and what it may /// still be doing with it as a semaphore to wait on. fn acquireDmabuf(self: *@This()) !void { if (comptime !native_wayland) unreachable; const dma = self.dma.?; const pool = dma.pool.?; if (dma.sync != null) try self.reapReleased(false); const buffer = while (true) { if (try pool.acquire(self.extent.width, self.extent.height, .xrgb8888)) |b| break b; // Every buffer is on screen: wait for the compositor to let one go, // which under explicit sync is a release point signalling. if (dma.sync != null and dma.awaiting.items.len != 0) try self.reapReleased(true) else _ = try dma.nw.connection.dispatch(); }; dma.current = buffer; // The compositor's release points say when it has finished; there is // nothing implicit to wait for. if (dma.sync != null) return; // The compositor's reads of it, as the kernel's implicit fences. A // kernel before 5.20 cannot say, and then wl_buffer.release is all // there is to go on. const sync = dmabuf.exportSyncFile(Dma.target(buffer).fd(), .write) catch return; self.device.importSemaphoreFdKHR(&.{ .semaphore = dma.wait[self.slot_index], .flags = .{ .temporary_bit = true }, .handle_type = .{ .sync_fd_bit = true }, .fd = sync, }) catch { _ = std.os.linux.close(sync); return; }; dma.waiting[self.slot_index] = true; } /// Gives back to the pool every buffer whose release point has signalled; /// with `block`, waits a while for one of them first. fn reapReleased(self: *@This(), block: bool) !void { if (comptime !native_wayland) unreachable; const dma = self.dma.?; if (block) { var semaphores: [8]vk.Semaphore = undefined; var values: [8]u64 = undefined; const n = @min(dma.awaiting.items.len, semaphores.len); for (dma.awaiting.items[0..n], semaphores[0..n], values[0..n]) |b, *s, *v| { s.* = Dma.target(b).timeline; v.* = Dma.target(b).point; } _ = try self.device.waitSemaphores(&.{ .flags = .{ .any_bit = true }, .semaphore_count = @intCast(n), .p_semaphores = &semaphores, .p_values = &values, }, std.time.ns_per_s); } var i: usize = 0; while (i < dma.awaiting.items.len) { const b = dma.awaiting.items[i]; const t = Dma.target(b); if (try self.device.getSemaphoreCounterValue(t.timeline) >= t.point) { _ = dma.awaiting.swapRemove(i); // Which may retire it, if it is of a size gone by. dma.pool.?.released(b); } else i += 1; } } /// Copies the finished frame into this frame's dma-buf, taking it from the /// compositor and giving it back: ownership passes through the foreign /// queue family, which is how Vulkan says "outside this device's queues". fn recordDmabufCopy(self: *@This(), command_buffer: vk.CommandBuffer) void { if (comptime !native_wayland) unreachable; const dma = self.dma.?; const t = Dma.target(dma.current.?); const ours = self.queue_families.graphics; const ready = vk.ImageMemoryBarrier{ .src_access_mask = .{ .color_attachment_write_bit = true }, .dst_access_mask = .{ .transfer_read_bit = true }, .old_layout = .transfer_src_optimal, .new_layout = .transfer_src_optimal, .src_queue_family_index = vk.QUEUE_FAMILY_IGNORED, .dst_queue_family_index = vk.QUEUE_FAMILY_IGNORED, .image = self.images[0], .subresource_range = colorRange(), }; const extent: vk.Extent3D = .{ .width = self.extent.width, .height = self.extent.height, .depth = 1 }; const layers: vk.ImageSubresourceLayers = .{ .aspect_mask = .{ .color_bit = true }, .mip_level = 0, .base_array_layer = 0, .layer_count = 1 }; switch (t.memory) { .buffer => |b| { const take = vk.BufferMemoryBarrier{ .src_access_mask = .{}, .dst_access_mask = .{ .transfer_write_bit = true }, .src_queue_family_index = vk.QUEUE_FAMILY_FOREIGN_EXT, .dst_queue_family_index = ours, .buffer = b.buffer, .offset = 0, .size = vk.WHOLE_SIZE, }; self.device.cmdPipelineBarrier(command_buffer, .{ .color_attachment_output_bit = true, .top_of_pipe_bit = true }, .{ .transfer_bit = true }, .{}, null, &.{take}, &.{ready}); self.device.cmdCopyImageToBuffer(command_buffer, self.images[0], .transfer_src_optimal, b.buffer, &.{.{ .buffer_offset = 0, .buffer_row_length = b.stride / 4, .buffer_image_height = 0, .image_subresource = layers, .image_offset = .{ .x = 0, .y = 0, .z = 0 }, .image_extent = extent, }}); var give = take; give.src_access_mask = .{ .transfer_write_bit = true }; give.dst_access_mask = .{}; give.src_queue_family_index = ours; give.dst_queue_family_index = vk.QUEUE_FAMILY_FOREIGN_EXT; self.device.cmdPipelineBarrier(command_buffer, .{ .transfer_bit = true }, .{ .bottom_of_pipe_bit = true }, .{}, null, &.{give}, null); }, .image => |img| { const take = vk.ImageMemoryBarrier{ .src_access_mask = .{}, .dst_access_mask = .{ .transfer_write_bit = true }, // Its contents are about to be replaced whole. .old_layout = .undefined, .new_layout = .transfer_dst_optimal, .src_queue_family_index = vk.QUEUE_FAMILY_FOREIGN_EXT, .dst_queue_family_index = ours, .image = img.image, .subresource_range = colorRange(), }; self.device.cmdPipelineBarrier(command_buffer, .{ .color_attachment_output_bit = true, .top_of_pipe_bit = true }, .{ .transfer_bit = true }, .{}, null, null, &.{ ready, take }); self.device.cmdCopyImage(command_buffer, self.images[0], .transfer_src_optimal, img.image, .transfer_dst_optimal, &.{.{ .src_subresource = layers, .src_offset = .{ .x = 0, .y = 0, .z = 0 }, .dst_subresource = layers, .dst_offset = .{ .x = 0, .y = 0, .z = 0 }, .extent = extent, }}); const give = vk.ImageMemoryBarrier{ .src_access_mask = .{ .transfer_write_bit = true }, .dst_access_mask = .{}, .old_layout = .transfer_dst_optimal, .new_layout = .general, .src_queue_family_index = ours, .dst_queue_family_index = vk.QUEUE_FAMILY_FOREIGN_EXT, .image = img.image, .subresource_range = colorRange(), }; self.device.cmdPipelineBarrier(command_buffer, .{ .transfer_bit = true }, .{ .bottom_of_pipe_bit = true }, .{}, null, null, &.{give}); }, } } /// Attaches the frame's completion to the dma-buf, as a write, so that the /// compositor reading it waits for the copy. Where the kernel cannot, the /// copy is waited for here instead. fn fenceDmabuf(self: *@This()) void { if (comptime !native_wayland) unreachable; const dma = self.dma.?; if (dma.sync != null) return; const fd = Dma.target(dma.current.?).fd(); const sync = self.device.getSemaphoreFdKHR(&.{ .semaphore = dma.done[self.current_slot], .handle_type = .{ .sync_fd_bit = true } }) catch |err| { log.warn("no sync file for the frame ({t}); waiting for it", .{err}); _ = self.device.waitForFences(&.{self.slots[self.current_slot].fence}, .true, std.math.maxInt(u64)) catch {}; return; }; defer _ = std.os.linux.close(sync); dmabuf.importSyncFile(fd, sync, .write) catch { _ = self.device.waitForFences(&.{self.slots[self.current_slot].fence}, .true, std.math.maxInt(u64)) catch {}; }; } fn presentDmabuf(self: *@This()) !void { if (comptime !native_wayland) unreachable; const dma = self.dma.?; const buffer = dma.current orelse return; dma.current = null; if (dma.sync != null) { const t = Dma.target(buffer); const timeline = t.wl_timeline.?; try dma.nw.presenter.present(buffer, .{ .sync = .{ .acquire = .{ .timeline = timeline, .value = t.point + 1 }, .release = .{ .timeline = timeline, .value = t.point + 2 }, } }); t.point += 2; try dma.awaiting.append(self.allocator, buffer); } else { try dma.nw.presenter.present(buffer, .{}); } self.slot_index = (self.current_slot + 1) % self.slots.len; } fn createFramebuffers(self: *@This()) !void { if (self.depth_format != null) try self.createDepthResources(); errdefer self.destroyFramebuffers(); if (self.rendering_mode == .dynamic) return; self.framebuffers = try self.allocator.alloc(vk.Framebuffer, self.image_views.len); @memset(self.framebuffers, .null_handle); for (self.image_views, self.framebuffers, 0..) |view, *framebuffer, i| { const attachments = [_]vk.ImageView{ view, if (self.depth_format == null) .null_handle else self.depth_image_views[i] }; framebuffer.* = try self.device.createFramebuffer(&.{ .render_pass = self.render_pass, .attachment_count = if (self.depth_format == null) 1 else 2, .p_attachments = &attachments, .width = self.extent.width, .height = self.extent.height, .layers = 1, }, self.vk_alloc); } } fn destroyFramebuffers(self: *@This()) void { for (self.framebuffers) |framebuffer| if (framebuffer != .null_handle) self.device.destroyFramebuffer(framebuffer, self.vk_alloc); if (self.framebuffers.len != 0) self.allocator.free(self.framebuffers); self.framebuffers = &.{}; self.destroyDepthResources(); } fn createDepthResources(self: *@This()) !void { const format = self.depth_format orelse return; const memory_properties = self.instance.getPhysicalDeviceMemoryProperties(self.physical_device); self.depth_images = try self.allocator.alloc(vk.Image, self.image_views.len); @memset(self.depth_images, .null_handle); errdefer self.destroyDepthResources(); self.depth_image_views = try self.allocator.alloc(vk.ImageView, self.image_views.len); @memset(self.depth_image_views, .null_handle); self.depth_memories = try self.allocator.alloc(vk.DeviceMemory, self.image_views.len); @memset(self.depth_memories, .null_handle); self.depth_layouts = try self.allocator.alloc(vk.ImageLayout, self.image_views.len); @memset(self.depth_layouts, .undefined); for (self.depth_images, self.depth_image_views, self.depth_memories) |*image, *view, *memory| { image.* = try self.device.createImage(&.{ .image_type = .@"2d", .format = format, .extent = .{ .width = self.extent.width, .height = self.extent.height, .depth = 1 }, .mip_levels = 1, .array_layers = 1, .samples = .{ .@"1_bit" = true }, .tiling = .optimal, .usage = .{ .depth_stencil_attachment_bit = true }, .sharing_mode = .exclusive, .initial_layout = .undefined, }, self.vk_alloc); const requirements = self.device.getImageMemoryRequirements(image.*); const memory_type_index = findMemoryType(memory_properties, requirements.memory_type_bits, .{ .device_local_bit = true }) orelse findMemoryType(memory_properties, requirements.memory_type_bits, .{}) orelse return error.NoCompatibleMemoryType; memory.* = try self.device.allocateMemory(&.{ .allocation_size = requirements.size, .memory_type_index = memory_type_index, }, self.vk_alloc); try self.device.bindImageMemory(image.*, memory.*, 0); view.* = try self.device.createImageView(&.{ .image = image.*, .view_type = .@"2d", .format = format, .components = .{ .r = .identity, .g = .identity, .b = .identity, .a = .identity }, .subresource_range = depthRange(format), }, self.vk_alloc); } } fn destroyDepthResources(self: *@This()) void { for (self.depth_image_views) |view| if (view != .null_handle) self.device.destroyImageView(view, self.vk_alloc); for (self.depth_images) |image| if (image != .null_handle) self.device.destroyImage(image, self.vk_alloc); for (self.depth_memories) |memory| if (memory != .null_handle) self.device.freeMemory(memory, self.vk_alloc); if (self.depth_image_views.len != 0) self.allocator.free(self.depth_image_views); if (self.depth_images.len != 0) self.allocator.free(self.depth_images); if (self.depth_memories.len != 0) self.allocator.free(self.depth_memories); if (self.depth_layouts.len != 0) self.allocator.free(self.depth_layouts); self.depth_image_views = &.{}; self.depth_images = &.{}; self.depth_memories = &.{}; self.depth_layouts = &.{}; } /// `dmabuf_device` is the compositor's main device, for `.dmabuf` /// presentation: a device that is it, and can share dma-bufs, is chosen /// over any other. fn initVulkanResources(allocator: std.mem.Allocator, window: *wio.Window, options: InitOptions, dmabuf_device: ?u64) !VulkanResources { const vk_alloc = options.vk_alloc; const base = vk.BaseWrapper.load(getInstanceProcAddr); if (options.api_version.toU32() < vk.API_VERSION_1_2.toU32()) return error.UnsupportedApiVersion; if (options.rendering == .dynamic and options.api_version.toU32() < vk.API_VERSION_1_3.toU32()) return error.DynamicRenderingRequiresVulkan13; if (options.rendering == .dynamic and pNextContains(options.device_features_p_next, .physical_device_dynamic_rendering_features)) return error.DuplicateDynamicRenderingFeature; const loader_version = if (base.dispatch.vkEnumerateInstanceVersion != null) try base.enumerateInstanceVersion() else vk.API_VERSION_1_0.toU32(); if (loader_version < options.api_version.toU32()) return error.UnsupportedApiVersion; const instance_wrapper = try allocator.create(vk.InstanceWrapper); errdefer allocator.destroy(instance_wrapper); // Without a surface, none of what the window system would need for one. const wio_extensions: []const [*:0]const u8 = if (options.presentation == .swapchain) wio.getRequiredVulkanInstanceExtensions() else &.{}; const available_extensions = try base.enumerateInstanceExtensionPropertiesAlloc(null, allocator); defer allocator.free(available_extensions); const portability_enumeration = hasExtension(available_extensions, vk.extensions.khr_portability_enumeration.name); const extension_storage = try allocator.alloc( [*:0]const u8, wio_extensions.len + options.instance_extensions.len + @intFromBool(portability_enumeration), ); defer allocator.free(extension_storage); var extension_count: usize = 0; appendUniqueExtensions(extension_storage, &extension_count, wio_extensions); appendUniqueExtensions(extension_storage, &extension_count, options.instance_extensions); if (portability_enumeration) appendUniqueExtensions( extension_storage, &extension_count, &.{vk.extensions.khr_portability_enumeration.name.ptr}, ); const extensions = extension_storage[0..extension_count]; for (extensions) |extension| { if (!hasExtension(available_extensions, std.mem.span(extension))) return error.MissingRequiredInstanceExtension; } const instance_handle = try base.createInstance(&.{ .flags = .{ .enumerate_portability_bit_khr = portability_enumeration }, .p_application_info = &.{ .p_application_name = "dvui", .application_version = 0, .p_engine_name = "dvui", .engine_version = 0, .api_version = options.api_version.toU32(), }, .enabled_extension_count = @intCast(extensions.len), .pp_enabled_extension_names = extensions.ptr, }, vk_alloc); instance_wrapper.* = vk.InstanceWrapper.load(instance_handle, getInstanceProcAddr); const instance = vk.InstanceProxy.init(instance_handle, instance_wrapper); errdefer instance.destroyInstance(vk_alloc); var surface_value: u64 = 0; if (options.presentation == .swapchain) try window.vkCreateSurface(@intFromEnum(instance_handle), vk_alloc, &surface_value); const surface: vk.SurfaceKHR = @enumFromInt(surface_value); errdefer if (surface != .null_handle) instance.destroySurfaceKHR(surface, vk_alloc); const selected = try selectPhysicalDevice(allocator, instance, surface, options, dmabuf_device); defer allocator.free(selected.queue_counts); var priority_count: usize = 0; var queue_info_count: usize = 0; for (selected.queue_counts) |count| { priority_count += count; queue_info_count += @intFromBool(count != 0); } const priorities = try allocator.alloc(f32, priority_count); defer allocator.free(priorities); @memset(priorities, 1); const queue_infos = try allocator.alloc(vk.DeviceQueueCreateInfo, queue_info_count); defer allocator.free(queue_infos); var queue_info_index: usize = 0; var priority_index: usize = 0; for (selected.queue_counts, 0..) |count, family_index| { if (count == 0) continue; queue_infos[queue_info_index] = .{ .queue_family_index = @intCast(family_index), .queue_count = count, .p_queue_priorities = priorities[priority_index..].ptr, }; queue_info_index += 1; priority_index += count; } const device_wrapper = try allocator.create(vk.DeviceWrapper); errdefer allocator.destroy(device_wrapper); const device_extension_storage = try allocator.alloc([*:0]const u8, options.device_extensions.len + 2 + dmabuf.required_device_extensions.len + 1); defer allocator.free(device_extension_storage); var device_extension_count: usize = 0; if (options.presentation == .swapchain) appendUniqueExtensions(device_extension_storage, &device_extension_count, &.{vk.extensions.khr_swapchain.name.ptr}); appendUniqueExtensions(device_extension_storage, &device_extension_count, options.device_extensions); if (selected.portability_subset) appendUniqueExtensions( device_extension_storage, &device_extension_count, &.{vk.extensions.khr_portability_subset.name.ptr}, ); if (selected.dmabuf_support) |support| { var names: [dmabuf.required_device_extensions.len + 1][*:0]const u8 = undefined; appendUniqueExtensions(device_extension_storage, &device_extension_count, support.extensions(&names)); } const device_extensions = device_extension_storage[0..device_extension_count]; var dynamic_rendering_features = vk.PhysicalDeviceDynamicRenderingFeatures{ .p_next = @constCast(options.device_features_p_next), .dynamic_rendering = .true, }; const features_next: ?*const anyopaque = if (options.rendering == .dynamic) &dynamic_rendering_features else options.device_features_p_next; // Explicit sync's timelines. Where the application's own chain already // names timeline or 1.2 features, which could say either way, it is // left alone and explicit sync is not used. var dmabuf_support = selected.dmabuf_support; if (dmabuf_support) |*support| support.timelines = support.timelines and !pNextContains(options.device_features_p_next, .physical_device_timeline_semaphore_features) and !pNextContains(options.device_features_p_next, .physical_device_vulkan_1_2_features); var timeline_features = vk.PhysicalDeviceTimelineSemaphoreFeatures{ .p_next = @constCast(features_next), .timeline_semaphore = .true, }; const device_handle = try instance.createDevice(selected.device, &.{ .p_next = if (dmabuf_support != null and dmabuf_support.?.timelines) &timeline_features else features_next, .queue_create_info_count = @intCast(queue_infos.len), .p_queue_create_infos = queue_infos.ptr, .enabled_extension_count = @intCast(device_extensions.len), .pp_enabled_extension_names = device_extensions.ptr, .p_enabled_features = &options.device_features, }, vk_alloc); device_wrapper.* = vk.DeviceWrapper.load(device_handle, instance_wrapper.dispatch.vkGetDeviceProcAddr.?); const device = vk.DeviceProxy.init(device_handle, device_wrapper); errdefer device.destroyDevice(vk_alloc); const command_pool = try device.createCommandPool(&.{ .flags = .{ .reset_command_buffer_bit = true }, .queue_family_index = selected.families.graphics, }, vk_alloc); errdefer device.destroyCommandPool(command_pool, vk_alloc); return .{ .instance_wrapper = instance_wrapper, .instance = instance, .physical_device = selected.device, .device_wrapper = device_wrapper, .device = device, .graphics_queue = vk.QueueProxy.init(device.getDeviceQueue(selected.families.graphics, 0), device_wrapper), .present_queue = vk.QueueProxy.init(device.getDeviceQueue(selected.families.present, 0), device_wrapper), .queue_families = selected.families, .command_pool = command_pool, .surface = surface, .dmabuf_support = dmabuf_support, }; } fn selectPhysicalDevice( allocator: std.mem.Allocator, instance: vk.InstanceProxy, surface: vk.SurfaceKHR, options: InitOptions, dmabuf_device: ?u64, ) !SelectedDevice { const devices = try instance.enumeratePhysicalDevicesAlloc(allocator); defer allocator.free(devices); const candidates = try allocator.alloc(DeviceCandidate, devices.len); defer allocator.free(candidates); const portability = try allocator.alloc(bool, devices.len); defer allocator.free(portability); const sharing = try allocator.alloc(?dmabuf.Support, devices.len); defer allocator.free(sharing); var candidate_count: usize = 0; defer for (candidates[0..candidate_count]) |candidate| allocator.free(candidate.queue_families); for (devices) |device| { const properties = instance.getPhysicalDeviceProperties(device); if (properties.api_version < options.api_version.toU32()) continue; const available_extensions = try instance.enumerateDeviceExtensionPropertiesAlloc(device, null, allocator); defer allocator.free(available_extensions); const extension_support = getDeviceExtensionSupport(available_extensions); if (options.presentation == .swapchain and !extension_support.swapchain) continue; var extensions_available = true; for (options.device_extensions) |extension| { if (!hasExtension(available_extensions, std.mem.span(extension))) extensions_available = false; } if (!extensions_available) continue; var dynamic_rendering_features = vk.PhysicalDeviceDynamicRenderingFeatures{}; var features_2 = vk.PhysicalDeviceFeatures2{ .p_next = if (options.rendering == .dynamic) &dynamic_rendering_features else null, .features = .{}, }; instance.getPhysicalDeviceFeatures2(device, &features_2); const features = features_2.features; if (!supportsFeatures(features, options.device_features)) continue; if (options.rendering == .dynamic and dynamic_rendering_features.dynamic_rendering != .true) continue; const families = try instance.getPhysicalDeviceQueueFamilyPropertiesAlloc(device, allocator); defer allocator.free(families); const queue_families = try allocator.alloc(QueueFamilyCandidate, families.len); errdefer allocator.free(queue_families); for (families, 0..) |family, i| { const index: u32 = @intCast(i); queue_families[i] = .{ .index = index, .properties = family, // Without a surface, "presenting" is a copy on the graphics // queue. .present_support = if (surface == .null_handle) family.queue_flags.graphics_bit else try instance.getPhysicalDeviceSurfaceSupportKHR(device, index, surface) == .true, }; } const chosen_families = chooseQueueFamilies(queue_families) orelse { allocator.free(queue_families); continue; }; if (surface != .null_handle) { const surface_capabilities = try instance.getPhysicalDeviceSurfaceCapabilitiesKHR(device, surface); if (!surface_capabilities.supported_usage_flags.contains(options.swapchain_image_usage.merge(.{ .color_attachment_bit = true }))) { allocator.free(queue_families); continue; } } if (options.depth_format) |format| { const format_properties = instance.getPhysicalDeviceFormatProperties(device, format); if (!format_properties.optimal_tiling_features.depth_stencil_attachment_bit) { allocator.free(queue_families); continue; } } candidates[candidate_count] = .{ .instance = instance, .physical_device = device, .properties = properties, .features = features, .queue_families = queue_families, .graphics_queue_family = chosen_families.graphics, .present_queue_family = chosen_families.present, }; portability[candidate_count] = extension_support.portability_subset; sharing[candidate_count] = if (dmabuf_device) |dev| blk: { const support = dmabuf.probe(instance, device, available_extensions) orelse break :blk null; break :blk if (support.isDevice(dev)) support else null; } else null; candidate_count += 1; } if (candidate_count == 0) return error.NoSuitableDevice; const candidate_slice = candidates[0..candidate_count]; // The compositor's own device, when there is one that can share with it: // a buffer from any other would have to cross between GPUs. const sharing_index: ?usize = for (sharing[0..candidate_count], 0..) |support, i| { if (support != null) break i; } else null; const selected_index = if (options.select_device) |selector| if (selector.select) |select| select(selector.userdata, candidate_slice) orelse return error.NoSuitableDevice else sharing_index orelse 0 else sharing_index orelse 0; if (selected_index >= candidate_slice.len) return error.InvalidDeviceSelection; const candidate = candidate_slice[selected_index]; const queue_counts = try allocator.alloc(u32, candidate.queue_families.len); errdefer allocator.free(queue_counts); @memset(queue_counts, 0); queue_counts[candidate.graphics_queue_family] = 1; queue_counts[candidate.present_queue_family] = 1; const reserved_counts = try allocator.dupe(u32, queue_counts); defer allocator.free(reserved_counts); if (options.select_device) |selector| if (selector.configure_queues) |configure| try configure(selector.userdata, candidate, queue_counts); try validateQueueCounts(queue_counts, reserved_counts, candidate.queue_families); return .{ .device = candidate.physical_device, .families = .{ .graphics = candidate.graphics_queue_family, .present = candidate.present_queue_family, }, .portability_subset = portability[selected_index], .dmabuf_support = sharing[selected_index], .queue_counts = queue_counts, }; } fn getDeviceExtensionSupport(extensions: []const vk.ExtensionProperties) DeviceExtensionSupport { var support: DeviceExtensionSupport = .{}; for (extensions) |extension| { const name = std.mem.sliceTo(&extension.extension_name, 0); if (std.mem.eql(u8, name, vk.extensions.khr_swapchain.name)) support.swapchain = true; if (std.mem.eql(u8, name, vk.extensions.khr_portability_subset.name)) support.portability_subset = true; } return support; } fn supportsFeatures(available: vk.PhysicalDeviceFeatures, required: vk.PhysicalDeviceFeatures) bool { inline for (@typeInfo(vk.PhysicalDeviceFeatures).@"struct".fields) |field| { if (@field(required, field.name) == .true and @field(available, field.name) != .true) return false; } return true; } fn chooseQueueFamilies(families: []const QueueFamilyCandidate) ?QueueFamilies { for (families) |family| { if (family.properties.queue_count != 0 and family.properties.queue_flags.graphics_bit and family.present_support) return .{ .graphics = family.index, .present = family.index }; } var graphics: ?u32 = null; var present_family: ?u32 = null; for (families) |family| { if (family.properties.queue_count == 0) continue; if (graphics == null and family.properties.queue_flags.graphics_bit) graphics = family.index; if (present_family == null and family.present_support) present_family = family.index; } if (graphics == null or present_family == null) return null; return .{ .graphics = graphics.?, .present = present_family.? }; } fn validateQueueCounts(counts: []const u32, reserved: []const u32, families: []const QueueFamilyCandidate) !void { if (counts.len != reserved.len or counts.len != families.len) return error.InvalidQueueCount; for (counts, reserved, families) |count, minimum, family| { if (count < minimum or count > family.properties.queue_count) return error.InvalidQueueCount; } } fn pNextContains(head: ?*const anyopaque, structure_type: vk.StructureType) bool { var current: ?*const vk.BaseInStructure = if (head) |ptr| @ptrCast(@alignCast(ptr)) else null; while (current) |item| : (current = item.p_next) { if (item.s_type == structure_type) return true; } return false; } fn hasExtension(extensions: []const vk.ExtensionProperties, wanted: []const u8) bool { for (extensions) |extension| { if (std.mem.eql(u8, std.mem.sliceTo(&extension.extension_name, 0), wanted)) return true; } return false; } fn hasExtensionName(extensions: []const [*:0]const u8, wanted: []const u8) bool { for (extensions) |extension| { if (std.mem.eql(u8, std.mem.span(extension), wanted)) return true; } return false; } fn appendUniqueExtensions( destination: [][*:0]const u8, count: *usize, extensions: []const [*:0]const u8, ) void { for (extensions) |extension| { if (hasExtensionName(destination[0..count.*], std.mem.span(extension))) continue; destination[count.*] = extension; count.* += 1; } } fn createRenderPass( device: vk.DeviceProxy, color_format: vk.Format, depth_format: ?vk.Format, final_layout: vk.ImageLayout, vk_alloc: ?*vk.AllocationCallbacks, ) !vk.RenderPass { const attachments = [_]vk.AttachmentDescription{ .{ .format = color_format, .samples = .{ .@"1_bit" = true }, .load_op = .clear, .store_op = .store, .stencil_load_op = .dont_care, .stencil_store_op = .dont_care, .initial_layout = .undefined, .final_layout = final_layout, }, .{ .format = depth_format orelse .undefined, .samples = .{ .@"1_bit" = true }, .load_op = .clear, .store_op = .dont_care, .stencil_load_op = .clear, .stencil_store_op = .dont_care, .initial_layout = .undefined, .final_layout = .depth_stencil_attachment_optimal, }, }; const color_reference = vk.AttachmentReference{ .attachment = 0, .layout = .color_attachment_optimal }; const depth_reference = vk.AttachmentReference{ .attachment = 1, .layout = .depth_stencil_attachment_optimal }; const subpass = vk.SubpassDescription{ .pipeline_bind_point = .graphics, .color_attachment_count = 1, .p_color_attachments = @ptrCast(&color_reference), .p_depth_stencil_attachment = if (depth_format == null) null else &depth_reference, }; const dependency = vk.SubpassDependency{ .src_subpass = vk.SUBPASS_EXTERNAL, .dst_subpass = 0, .src_stage_mask = .{ .color_attachment_output_bit = true, .early_fragment_tests_bit = depth_format != null, }, .dst_stage_mask = .{ .color_attachment_output_bit = true, .early_fragment_tests_bit = depth_format != null, }, .dst_access_mask = .{ .color_attachment_read_bit = true, .color_attachment_write_bit = true, .depth_stencil_attachment_read_bit = depth_format != null, .depth_stencil_attachment_write_bit = depth_format != null, }, }; return device.createRenderPass(&.{ .attachment_count = if (depth_format == null) 1 else 2, .p_attachments = &attachments, .subpass_count = 1, .p_subpasses = @ptrCast(&subpass), .dependency_count = 1, .p_dependencies = @ptrCast(&dependency), }, vk_alloc); } fn validateDepthFormat(instance: vk.InstanceProxy, physical_device: vk.PhysicalDevice, format: vk.Format) !void { const properties = instance.getPhysicalDeviceFormatProperties(physical_device, format); if (!properties.optimal_tiling_features.depth_stencil_attachment_bit) return error.UnsupportedDepthFormat; } fn findMemoryType( properties: vk.PhysicalDeviceMemoryProperties, type_bits: u32, required: vk.MemoryPropertyFlags, ) ?u32 { for (properties.memory_types[0..properties.memory_type_count], 0..) |memory_type, i| { const bit = @as(u32, 1) << @intCast(i); if (type_bits & bit != 0 and memory_type.property_flags.contains(required)) return @intCast(i); } return null; } fn chooseColorFormat(available: []const vk.SurfaceFormatKHR, preferred: []const vk.Format) vk.SurfaceFormatKHR { if (available.len == 1 and available[0].format == .undefined and preferred.len != 0) { return .{ .format = preferred[0], .color_space = available[0].color_space }; } for (preferred) |format| for (available) |item| if (item.format == format) return item; return available[0]; } fn choosePresentMode(available: []const vk.PresentModeKHR, vsync: bool) vk.PresentModeKHR { if (!vsync) for (available) |mode| if (mode == .mailbox_khr) return mode; if (!vsync) for (available) |mode| if (mode == .immediate_khr) return mode; return .fifo_khr; } fn chooseCompositeAlpha(supported: vk.CompositeAlphaFlagsKHR) vk.CompositeAlphaFlagsKHR { if (supported.opaque_bit_khr) return .{ .opaque_bit_khr = true }; if (supported.pre_multiplied_bit_khr) return .{ .pre_multiplied_bit_khr = true }; if (supported.post_multiplied_bit_khr) return .{ .post_multiplied_bit_khr = true }; return .{ .inherit_bit_khr = true }; } fn colorRange() vk.ImageSubresourceRange { return .{ .aspect_mask = .{ .color_bit = true }, .base_mip_level = 0, .level_count = 1, .base_array_layer = 0, .layer_count = 1, }; } fn depthRange(format: vk.Format) vk.ImageSubresourceRange { return .{ .aspect_mask = .{ .depth_bit = format != .s8_uint, .stencil_bit = switch (format) { .s8_uint, .d16_unorm_s8_uint, .d24_unorm_s8_uint, .d32_sfloat_s8_uint => true, else => false, }, }, .base_mip_level = 0, .level_count = 1, .base_array_layer = 0, .layer_count = 1, }; } fn hasDepth(format: vk.Format) bool { return format != .s8_uint; } fn hasStencil(format: vk.Format) bool { return switch (format) { .s8_uint, .d16_unorm_s8_uint, .d24_unorm_s8_uint, .d32_sfloat_s8_uint => true, else => false, }; } fn sizeToExtent(size: dvui.Size.Physical) vk.Extent2D { return .{ .width = @max(1, @as(u32, @intFromFloat(@round(size.w)))), .height = @max(1, @as(u32, @intFromFloat(@round(size.h)))), }; } fn getInstanceProcAddr(instance: vk.Instance, name: [*:0]const u8) callconv(vk.vulkan_call_conv) vk.PfnVoidFunction { return @ptrCast(wio.vkGetInstanceProcAddr(@intFromEnum(instance), name)); } fn mapGenericError(err: anyerror) dvui.Backend.GenericError { log.err("Vulkan backend operation failed: {}", .{err}); return if (err == error.OutOfMemory) error.OutOfMemory else error.BackendError; } test "queue family selection prefers unified graphics and present" { var properties = [_]vk.QueueFamilyProperties{ std.mem.zeroes(vk.QueueFamilyProperties), std.mem.zeroes(vk.QueueFamilyProperties), std.mem.zeroes(vk.QueueFamilyProperties), }; properties[0].queue_count = 1; properties[0].queue_flags = .{ .graphics_bit = true }; properties[1].queue_count = 1; properties[2].queue_count = 1; properties[2].queue_flags = .{ .graphics_bit = true }; const families = [_]QueueFamilyCandidate{ .{ .index = 0, .properties = properties[0], .present_support = false }, .{ .index = 1, .properties = properties[1], .present_support = true }, .{ .index = 2, .properties = properties[2], .present_support = true }, }; try std.testing.expectEqual(QueueFamilies{ .graphics = 2, .present = 2 }, chooseQueueFamilies(&families).?); } test "queue counts may only increase within family capacity" { var properties = [_]vk.QueueFamilyProperties{ std.mem.zeroes(vk.QueueFamilyProperties), std.mem.zeroes(vk.QueueFamilyProperties), }; properties[0].queue_count = 2; properties[1].queue_count = 1; const families = [_]QueueFamilyCandidate{ .{ .index = 0, .properties = properties[0], .present_support = true }, .{ .index = 1, .properties = properties[1], .present_support = false }, }; try validateQueueCounts(&.{ 2, 1 }, &.{ 1, 0 }, &families); try std.testing.expectError(error.InvalidQueueCount, validateQueueCounts(&.{ 0, 0 }, &.{ 1, 0 }, &families)); try std.testing.expectError(error.InvalidQueueCount, validateQueueCounts(&.{ 3, 0 }, &.{ 1, 0 }, &families)); } test "pNext duplicate detection" { const feature = vk.PhysicalDeviceDynamicRenderingFeatures{}; try std.testing.expect(pNextContains(&feature, .physical_device_dynamic_rendering_features)); try std.testing.expect(!pNextContains(null, .physical_device_dynamic_rendering_features)); } test { std.testing.refAllDecls(@This()); }