diff --git a/runtime/vk_device.c b/runtime/vk_device.c index 9b547287..ac68eb3a 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -21,6 +21,10 @@ PFN_vkGetDeviceProcAddr vk_dpa = vkGetDeviceProcAddr; static uint32_t g_gfx_queues = 1; // queues the graphics family offers (pick_device) +#define QFAM_MAX 16 +static uint32_t g_nqfam; // the device's queue families (pick_device): their flags and queue counts +static VkQueueFlags g_qfam_flags[QFAM_MAX]; +static uint32_t g_qfam_count[QFAM_MAX]; static VkCtx g_vk; static int g_ready; @@ -271,6 +275,8 @@ retry: vkGetPhysicalDeviceQueueFamilyProperties(g_vk.phys, &nq, qp); g_vk.gfx_family = find_gfx_family(g_vk.phys, qp, nq); g_gfx_queues = qp[g_vk.gfx_family].queueCount; + g_nqfam = nq < QFAM_MAX ? nq : QFAM_MAX; + for (uint32_t i = 0; i < g_nqfam; i++) g_qfam_flags[i] = qp[i].queueFlags, g_qfam_count[i] = qp[i].queueCount; g_vk.xfer_family = g_vk.gfx_family; for (uint32_t i = 0; i < nq; i++) // a family that does transfer only (async DMA engine) if ((qp[i].queueFlags & VK_QUEUE_TRANSFER_BIT) && !(qp[i].queueFlags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT))) { @@ -541,18 +547,44 @@ static int create_device(void) { has_fbl && c->fbl.attachmentFeedbackLoopLayout, has_lr && c->lr.dynamicRenderingLocalRead); } - // Frame generation (vk_fg.cpp) presents on a second queue of the graphics family and acquires on a third - static const float prio[3] = {1.0f, 1.0f, 1.0f}; + // Frame generation (vk_fg.cpp) presents on a second queue of the graphics family and acquires on a third. AMD's + // swapchain needs a present queue nothing else submits to (FidelityFX: the engine must not use it, or it can + // deadlock); the image acquire queue needs no capability. Where the graphics family has one queue (AMD's Windows + // driver, an RX 580), both come from another family: a queue of a family that can present for the present, and + // a second queue of it, or of any other family, for the acquire (the present queue itself when there is none). + // Without them vk_fg_create_swapchain gave up without a word: frame generation set in the menu did nothing. + static const float prio[4] = {1.0f, 1.0f, 1.0f, 1.0f}; uint32_t nq_gfx = vk_fg_queue_count() < g_gfx_queues ? vk_fg_queue_count() : g_gfx_queues; - VkDeviceQueueCreateInfo qi[2] = { - {.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, .queueFamilyIndex = g_vk.gfx_family, .queueCount = nq_gfx, - .pQueuePriorities = prio}}; - uint32_t nqi = 1; - if (g_vk.xfer_family != g_vk.gfx_family) { - qi[1] = (VkDeviceQueueCreateInfo){.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, - .queueFamilyIndex = g_vk.xfer_family, .queueCount = 1, .pQueuePriorities = prio}; - nqi = 2; + uint32_t used[QFAM_MAX] = {0}; + if (g_vk.gfx_family < QFAM_MAX) used[g_vk.gfx_family] = nq_gfx; + if (g_vk.xfer_family != g_vk.gfx_family && g_vk.xfer_family < QFAM_MAX) used[g_vk.xfer_family] = 1; + uint32_t fg_pf = UINT32_MAX, fg_pi = 0, fg_af = UINT32_MAX, fg_ai = 0; + if (nq_gfx >= 2) { + fg_pf = fg_af = g_vk.gfx_family, fg_pi = 1, fg_ai = nq_gfx - 1; + } else { + for (int pass = 0; pass < 2 && fg_pf == UINT32_MAX; pass++) // compute families first, then the others + for (uint32_t f = 0; f < g_nqfam && fg_pf == UINT32_MAX; f++) { + if (f == g_vk.gfx_family || used[f] >= g_qfam_count[f]) continue; + if (!(g_qfam_flags[f] & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT))) continue; + if (!pass != !!(g_qfam_flags[f] & VK_QUEUE_COMPUTE_BIT)) continue; + if (!gfx_window_vk_presentable(g_vk.instance, g_vk.phys, f)) continue; + fg_pf = f, fg_pi = used[f]++; + } + if (fg_pf != UINT32_MAX) { + if (used[fg_pf] < g_qfam_count[fg_pf] && used[fg_pf] < 4) fg_af = fg_pf, fg_ai = used[fg_pf]++; + for (uint32_t f = 0; f < g_nqfam && fg_af == UINT32_MAX; f++) + if (f != g_vk.gfx_family && used[f] < g_qfam_count[f] && used[f] < 4) fg_af = f, fg_ai = used[f]++; + if (fg_af == UINT32_MAX) fg_af = fg_pf, fg_ai = fg_pi; + } } + VkDeviceQueueCreateInfo qi[QFAM_MAX]; + uint32_t nqi = 0; + qi[nqi++] = (VkDeviceQueueCreateInfo){.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, + .queueFamilyIndex = g_vk.gfx_family, .queueCount = nq_gfx, .pQueuePriorities = prio}; + for (uint32_t f = 0; f < g_nqfam; f++) + if (f != g_vk.gfx_family && used[f]) + qi[nqi++] = (VkDeviceQueueCreateInfo){.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, + .queueFamilyIndex = f, .queueCount = used[f], .pQueuePriorities = prio}; VkDeviceCreateInfo di = {.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, .pNext = &c->f2, .queueCreateInfoCount = nqi, .pQueueCreateInfos = qi, .enabledExtensionCount = n_ext, .ppEnabledExtensionNames = ext}; VkResult r = vk_dlssg_create_device(g_vk.instance, g_vk.phys, &di, &g_vk.device); @@ -564,11 +596,19 @@ static int create_device(void) { rt_log("vk: device entry points come through Streamline's interposer (DLSS Frame Generation)"); } vkGetDeviceQueue(g_vk.device, g_vk.gfx_family, 0, &g_vk.gfx_queue); - if (nq_gfx >= 2) { + if (fg_pf != UINT32_MAX) { VkQueue present, acquire; - vkGetDeviceQueue(g_vk.device, g_vk.gfx_family, 1, &present); - vkGetDeviceQueue(g_vk.device, g_vk.gfx_family, nq_gfx - 1, &acquire); - vk_fg_queues(present, acquire); + vkGetDeviceQueue(g_vk.device, fg_pf, fg_pi, &present); + vkGetDeviceQueue(g_vk.device, fg_af, fg_ai, &acquire); + vk_fg_queues(present, fg_pf, acquire, fg_af); + if (fg_pf != g_vk.gfx_family) + rt_log("vk: frame generation queues: the graphics family %u has %u queue%s; presents on family %u queue %u, " + "image acquires on family %u queue %u", g_vk.gfx_family, g_gfx_queues, g_gfx_queues == 1 ? "" : "s", + fg_pf, fg_pi, fg_af, fg_ai); + } else { + rt_log("vk: frame generation queues: none (the graphics family %u has %u queue%s and no other family with a free " + "queue can present): AMD FSR 3.1 frame generation cannot run", g_vk.gfx_family, g_gfx_queues, + g_gfx_queues == 1 ? "" : "s"); } if (g_vk.xfer_family != g_vk.gfx_family) vkGetDeviceQueue(g_vk.device, g_vk.xfer_family, 0, &g_vk.xfer_queue); else { g_vk.xfer_queue = g_vk.gfx_queue; g_vk.xfer_shared = 1; } diff --git a/runtime/vk_ffx.cpp b/runtime/vk_ffx.cpp index 41272894..eca5062e 100644 --- a/runtime/vk_ffx.cpp +++ b/runtime/vk_ffx.cpp @@ -28,6 +28,7 @@ extern "C" { #include "platform/platform.h" #include "vk.h" #include "vk_ffx.h" +#include "vk_fg.h" } namespace { @@ -260,7 +261,8 @@ extern "C" int vk_ffx_available(void) { #endif } -extern "C" int vk_ffx_framegen_available(void) { return vk_ffx_available(); } // the same library, its API calls +// the same library, its API calls; and a queue for the frame generation swapchain's presents (vk_fg_has_queues) +extern "C" int vk_ffx_framegen_available(void) { return vk_ffx_available() && vk_fg_has_queues(); } // NX2NAVI_FSR4 (default 0, live): FSR 4 where it runs (vk_ffx4.cpp) and has not failed in a row, else FSR 3.1. extern "C" int vk_ffx_fsr4(void) { return cfg_bool("NX2NAVI_FSR4") && vk_ffx4_available() && !vk_ffx4_failed(); } diff --git a/runtime/vk_fg.cpp b/runtime/vk_fg.cpp index 3ed69bef..290564ee 100644 --- a/runtime/vk_fg.cpp +++ b/runtime/vk_fg.cpp @@ -35,6 +35,7 @@ namespace { struct { VkQueue present = VK_NULL_HANDLE, acquire = VK_NULL_HANDLE; + uint32_t pfam = 0, afam = 0; // their queue families ffxContext sc = nullptr; // the swapchain context ffxContext fg = nullptr; // the frame generation context VkExtent2D fg_ext = {0, 0}; @@ -102,6 +103,12 @@ extern "C" int vk_fg_wanted(void) { if (F.sc) return !F.failed && !four; if (four) return 1; if (F.failed) return 0; + if (!F.present) { // no queue to present on besides the game's (vk_device.c says why): the plain swapchain stays + static int told_q; + if (!__atomic_exchange_n(&told_q, 1, __ATOMIC_RELAXED)) + rt_log("fg: AMD FSR 3.1 frame generation is set, but the device got no queue for its presents: off"); + return 0; + } if (vk_ffx_framegen_available()) return 1; static int told; if (!__atomic_exchange_n(&told, 1, __ATOMIC_RELAXED)) @@ -112,11 +119,13 @@ extern "C" int vk_fg_wanted(void) { // Always the three queues: the setting can be switched on during a run, and idle queues cost nothing. extern "C" uint32_t vk_fg_queue_count(void) { return 3; } -extern "C" void vk_fg_queues(VkQueue present, VkQueue acquire) { - F.present = present, F.acquire = acquire; - vk_fg4_queues(present, acquire); +extern "C" void vk_fg_queues(VkQueue present, uint32_t present_family, VkQueue acquire, uint32_t acquire_family) { + F.present = present, F.acquire = acquire, F.pfam = present_family, F.afam = acquire_family; + vk_fg4_queues(present, present_family, acquire, acquire_family); } +extern "C" int vk_fg_has_queues(void) { return F.present != VK_NULL_HANDLE; } + // AMD's swapchain is in place: the window's swapchain handle is AMD's object, and every swapchain call has to go // through AMD's functions. That holds after a failure too, until the present code has replaced the swapchain // (vk_fg_wanted): the plain Vulkan calls must never get that handle. @@ -147,8 +156,8 @@ extern "C" VkResult vk_fg_create_swapchain(const VkSwapchainCreateInfoKHR *ci, V d.createInfo.oldSwapchain = VK_NULL_HANDLE; d.gameQueue = {c->gfx_queue, c->gfx_family, submit_game}; d.asyncComputeQueue = {VK_NULL_HANDLE, c->gfx_family, nullptr}; - d.presentQueue = {F.present, c->gfx_family, nullptr}; - d.imageAcquireQueue = {F.acquire, c->gfx_family, nullptr}; + d.presentQueue = {F.present, F.pfam, nullptr}; + d.imageAcquireQueue = {F.acquire, F.afam, nullptr}; ffxReturnCode_t r = vk_ffx_create(&F.sc, &d.header); if (r != FFX_API_RETURN_OK) { F.sc = nullptr; diff --git a/runtime/vk_fg.h b/runtime/vk_fg.h index 31a97cb7..54771529 100644 --- a/runtime/vk_fg.h +++ b/runtime/vk_fg.h @@ -5,7 +5,10 @@ int vk_fg_wanted(void); // NX2NAVI_FRAMEGEN = fsr3 with DLSS or FSR 3.1 upscaling on (live), where FSR 3.1 loads uint32_t vk_fg_queue_count(void); // graphics-family queues the device creates -void vk_fg_queues(VkQueue present, VkQueue acquire); // vk_device.c, after the device exists +// vk_device.c, after the device exists: the queues AMD's swapchain presents and acquires on, and their families (the +// graphics family's own where it has three queues, else queues of another family: AMD's Windows driver gives one) +void vk_fg_queues(VkQueue present, uint32_t present_family, VkQueue acquire, uint32_t acquire_family); +int vk_fg_has_queues(void); // vk_fg_queues got a present queue: frame generation can run here int vk_fg_active(void); // the frame generation swapchain exists int vk_fg_generating(void); // ... and the last frame had generation on (not paused for the menu) // gfx_present.c's swapchain calls while vk_fg_wanted: the first create makes AMD's swapchain context, later ones @@ -34,7 +37,7 @@ int vk_fg4_failed(void); // it failed in this run: FSR 3.1's const char *vk_fg4_name(void); // the frame generation version the loader lists first for the device ("3.1.6"), "" unknown int vk_fg4_active(void); int vk_fg4_generating(void); -void vk_fg4_queues(VkQueue present, VkQueue acquire); +void vk_fg4_queues(VkQueue present, uint32_t present_family, VkQueue acquire, uint32_t acquire_family); VkResult vk_fg4_create_swapchain(const VkSwapchainCreateInfoKHR *ci, VkSwapchainKHR *sc); void vk_fg4_destroy_swapchain(VkSwapchainKHR sc); VkResult vk_fg4_get_images(VkSwapchainKHR sc, uint32_t *n, VkImage *img); diff --git a/runtime/vk_fg4.cpp b/runtime/vk_fg4.cpp index c380e156..6ca06b23 100644 --- a/runtime/vk_fg4.cpp +++ b/runtime/vk_fg4.cpp @@ -28,7 +28,7 @@ extern "C" int vk_fg4_failed(void) { return 0; } extern "C" const char *vk_fg4_name(void) { return ""; } extern "C" int vk_fg4_active(void) { return 0; } extern "C" int vk_fg4_generating(void) { return 0; } -extern "C" void vk_fg4_queues(VkQueue, VkQueue) {} +extern "C" void vk_fg4_queues(VkQueue, uint32_t, VkQueue, uint32_t) {} extern "C" VkResult vk_fg4_create_swapchain(const VkSwapchainCreateInfoKHR *, VkSwapchainKHR *) { return VK_ERROR_FEATURE_NOT_PRESENT; } @@ -68,6 +68,7 @@ namespace { struct { VkQueue present = VK_NULL_HANDLE, acquire = VK_NULL_HANDLE; + uint32_t pfam = 0, afam = 0; // their queue families ffxContext sc = nullptr; // the swapchain context ffxContext fg = nullptr; // the frame generation context VkSwapchainKHR handle = VK_NULL_HANDLE; // the swapchain the context made: remade in place, destroyed after the context @@ -200,7 +201,9 @@ extern "C" int vk_fg4_failed(void) { return F.failed; } extern "C" const char *vk_fg4_name(void) { return g_fg_name; } -extern "C" void vk_fg4_queues(VkQueue present, VkQueue acquire) { F.present = present, F.acquire = acquire; } +extern "C" void vk_fg4_queues(VkQueue present, uint32_t present_family, VkQueue acquire, uint32_t acquire_family) { + F.present = present, F.acquire = acquire, F.pfam = present_family, F.afam = acquire_family; +} // The SDK's swapchain is in place: the window's swapchain handle is its object, and every swapchain call has to go // through its functions, after a failure too, until the present code has taken it down. @@ -230,8 +233,8 @@ extern "C" VkResult vk_fg4_create_swapchain(const VkSwapchainCreateInfoKHR *ci, F.sc_desc.createInfo.oldSwapchain = VK_NULL_HANDLE; F.sc_desc.gameQueue = {c->gfx_queue, c->gfx_family, submit_game}; F.sc_desc.asyncComputeQueue = {VK_NULL_HANDLE, c->gfx_family, nullptr}; - F.sc_desc.presentQueue = {F.present, c->gfx_family, nullptr}; - F.sc_desc.imageAcquireQueue = {F.acquire, c->gfx_family, nullptr}; + F.sc_desc.presentQueue = {F.present, F.pfam, nullptr}; + F.sc_desc.imageAcquireQueue = {F.acquire, F.afam, nullptr}; // the version this code was built against, which the SDK asks for with every context, and a test's version F.sc_ver = {}; F.sc_ver.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_FRAMEGENERATIONSWAPCHAIN_VERSION_VK;