diff --git a/client/dmabuf_attach.c b/client/dmabuf_attach.c new file mode 100644 index 0000000..4e139de --- /dev/null +++ b/client/dmabuf_attach.c @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright (C) 2026 ASEbrahim and MaliBridge contributors + * + * Puts a write fence into a dma-buf (DMA_BUF_IOCTL_IMPORT_SYNC_FILE, Linux 6.0): whoever reads the buffer next waits for + * it (gamescope polls the dma-buf, DroidDeck's compositor exports the fence as its layer's acquire fence). The + * presenting thread's early commit (VKBRIDGE_PRESENT_EARLY, wsi_wayland.c) rests on this. + */ +#define _GNU_SOURCE +#include +#include +#include + +struct vkb_dma_buf_import_sync_file { + uint32_t flags; + int32_t fd; +}; +#define VKB_DMA_BUF_SYNC_WRITE (1u << 1) +#define VKB_DMA_BUF_IOCTL_IMPORT_SYNC_FILE _IOW('b', 3, struct vkb_dma_buf_import_sync_file) + +int vkb_dmabuf_attach_write_fence(int dmabuf_fd, int sync_fd); /* as in vkb_client.h; this file stands alone for the host test */ + +/* 0 when the sync file is now a write fence of the dma-buf; else the errno (ENOTTY: no such ioctl, EACCES/EPERM: not allowed here). */ +int vkb_dmabuf_attach_write_fence(int dmabuf_fd, int sync_fd) +{ + struct vkb_dma_buf_import_sync_file x = {VKB_DMA_BUF_SYNC_WRITE, sync_fd}; + int r; + do r = ioctl(dmabuf_fd, VKB_DMA_BUF_IOCTL_IMPORT_SYNC_FILE, &x); + while (r < 0 && errno == EINTR); + return r < 0 ? errno : 0; +} diff --git a/client/vkb_client.h b/client/vkb_client.h index ad4f0c1..25bd0dc 100644 --- a/client/vkb_client.h +++ b/client/vkb_client.h @@ -300,6 +300,7 @@ void vkb_log_info_always(const char *fmt, ...) __attribute__((format(printf, 1, /* hitch.c: the per-hitch line's client half (VKBRIDGE_HITCH_MS). vkb_hitch_active: 0 while off. */ extern int vkb_hitch_active; +int vkb_dmabuf_attach_write_fence(int dmabuf_fd, int sync_fd); /* dmabuf_attach.c: 0 or an errno */ extern __thread int vkb_hitch_in_present; /* inside the swapchain's own wait (wsi.c) */ uint64_t vkb_hitch_now(void); void vkb_hitch_server_ms(uint32_t ms); /* vkbQueryServer's hitch_ms (physdev.c) */ diff --git a/client/wsi_wayland.c b/client/wsi_wayland.c index e43880a..d1b8a09 100644 --- a/client/wsi_wayland.c +++ b/client/wsi_wayland.c @@ -274,6 +274,10 @@ typedef struct wl_state { uint32_t rendering; /* frames given to the thread whose rendering it has not seen done yet (VKBRIDGE_FRAME_QUEUE) */ uint64_t fq_calls, fq_waits, fq_ns, fq_max_ns; /* the frame queue's waits, for the line at the end */ uint32_t ring_lat[64]; /* ... each with its latency log number (latency.c), 0 */ + uint8_t ring_early[64]; /* ... and whether its submit signals esem[image] (VKBRIDGE_PRESENT_EARLY) */ + VkSemaphore *esem; /* one per image, exportable as a sync fd: signalled by the present submit; NULL: not early */ + int early_off; /* the early path is given up for this swapchain (atomic) */ + uint64_t e_frames, e_fallback, e_errors, e_last_ns, e_said; uint64_t ring_id[64]; /* ... and its present id (VK_KHR_present_id, wsi.c), 0 */ uint32_t lat_cur; /* the presenting thread's frame being shown: its latency log number */ uint64_t id_cur; /* ... its present id */ @@ -283,6 +287,13 @@ typedef struct wl_state { struct wl_proxy *lat_fb[16]; /* feedback objects not answered yet (destroyed before the queue) */ uint32_t lat_fb_n[16]; /* ... their frames' latency log numbers */ uint64_t lat_fb_id[16]; /* ... and present ids (0: none) */ + /* VKBRIDGE_PRESENT_REAP: an early frame's wait for its rendering (accounting, fence reuse) is done by this thread, so the + * presenting thread goes on to the next queued frame at once instead of sitting in the wait (own connection to the server). */ + pthread_t reaper; + int reaper_on, reap_quit; + pthread_cond_t reap_cond; /* with sc->lock */ + uint32_t reap_ring[64], reap_lat[64], reap_head, reap_len; + uint8_t *reap_busy; /* per image: queued or being waited for (its fence may not be submitted again yet) */ } wl_state; /* The compositor's zero-copy switch. A live swapchain built for the other mode is retired (out of @@ -489,6 +500,121 @@ static int dispatch_or_wake(wl_state *s, int efd) static VkResult wl_present(vkb_swapchain *sc, uint32_t index, const VkPresentRegionKHR *region); +/* + * VKBRIDGE_PRESENT_EARLY=1: the presenting thread commits a frame to the compositor without waiting for its rendering. The frame's + * present submit also signals a sync-fd exportable semaphore of its own (not the fence the thread waits on afterwards, whose + * payload an export would take); the thread exports that as a sync file (a round trip that does not wait for the GPU: it is ordered + * after the submit by the request's barrier), puts it into the image's dma-buf as a write fence (dmabuf_attach.c), and only then + * commits. Whoever reads the buffer waits for the fence: gamescope polls the dma-buf, DroidDeck's compositor exports it as the + * layer's acquire fence. A frame whose fence could not be attached is waited for and committed as without the knob, and the + * first failure ends the early path for the swapchain (the semaphore's state is not known any more); a failure that is the + * system's (no ioctl, not allowed) ends it for the process. The wait for the fence follows the commit, for the accounting. + */ +static int early_wanted(void) +{ + static int v = -1; + int c = __atomic_load_n(&v, __ATOMIC_RELAXED); + if (c >= 0) return c; + const char *e = getenv("VKBRIDGE_PRESENT_EARLY"); + c = e && *e && strcmp(e, "0") != 0; + __atomic_store_n(&v, c, __ATOMIC_RELAXED); + return c; +} +static int early_dead; /* the process: the system does not let the fence be attached (atomic) */ + +static uint64_t early_now(void) +{ + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; +} + +static int stats_seconds(void) +{ + static int v = -1; + if (v < 0) { + const char *e = getenv("VKBRIDGE_STATS"); + v = e && atoi(e) > 0 ? atoi(e) : 0; + } + return v; +} + +static void early_line(wl_state *s, const char *why, int final) +{ + if (!s->e_frames && !s->e_fallback && !s->e_errors && !why) return; + VKB_INFO("present early: %llu frames, fallback %llu, import errors %llu%s%s%s", (unsigned long long)s->e_frames, + (unsigned long long)s->e_fallback, (unsigned long long)s->e_errors, why ? " (" : "", why ? why : "", why ? ")" : ""); + (void)final; + s->e_frames = s->e_fallback = s->e_errors = 0; +} + +/* Tries to hand the frame's fence to its dma-buf: 1 when it is there (the frame may be committed now). */ +static int early_attach(vkb_swapchain *sc, wl_state *s, uint32_t index) +{ + int fd = -1; + VkSemaphoreGetFdInfoKHR gi = {VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR, NULL, s->esem[index], + VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT}; + VkResult er = vkb_wire_vkGetSemaphoreFdKHR((VkDevice)sc->dev, &gi, &fd); + if (er != VK_SUCCESS || fd < 0) { + if (fd >= 0) close(fd); + __atomic_store_n(&s->early_off, 1, __ATOMIC_RELAXED); + s->e_errors++; + VKB_WARN("present early: no sync fd for the frame's fence (%s); presenting as without VKBRIDGE_PRESENT_EARLY from now on", + er != VK_SUCCESS ? "export failed" : "already signalled or abandoned"); + return 0; + } + int err = vkb_dmabuf_attach_write_fence(sc->images[index].dmabuf_fd, fd); + close(fd); + if (!err) return 1; + s->e_errors++; + __atomic_store_n(&s->early_off, 1, __ATOMIC_RELAXED); + if (err == ENOTTY || err == EACCES || err == EPERM || err == EINVAL || err == ENOSYS || err == EBADF || err == EOPNOTSUPP) + __atomic_store_n(&early_dead, 1, __ATOMIC_RELAXED); + VKB_WARN("present early: DMA_BUF_IOCTL_IMPORT_SYNC_FILE failed (%s); presenting as without VKBRIDGE_PRESENT_EARLY from now on", + strerror(err)); + return 0; +} + +static int reap_wanted(void) +{ + static int v = -1; + int c = __atomic_load_n(&v, __ATOMIC_RELAXED); + if (c >= 0) return c; + const char *e = getenv("VKBRIDGE_PRESENT_REAP"); + c = !(e && *e && strcmp(e, "0") == 0); /* default on */ + __atomic_store_n(&v, c, __ATOMIC_RELAXED); + return c; +} + +static void *reaper_thread(void *arg) +{ + vkb_swapchain *sc = arg; + wl_state *s = sc->bdata; + pthread_setname_np(pthread_self(), "vkb-present-rp"); + pthread_mutex_lock(&sc->lock); + for (;;) { + while (!s->reap_len && !s->reap_quit) pthread_cond_wait(&s->reap_cond, &sc->lock); + if (!s->reap_len) break; + uint32_t index = s->reap_ring[s->reap_head], lat = s->reap_lat[s->reap_head]; + pthread_mutex_unlock(&sc->lock); + VkResult rw = vkb_wsi_wait_frame(sc, s->fences[index]); + if (rw >= 0) vkb_lat_stamp(lat, VKB_LAT_DONE, 0); + pthread_mutex_lock(&sc->lock); + s->reap_head = (s->reap_head + 1) % 64; + s->reap_len--; + s->reap_busy[index] = 0; + s->rendering--; + if (rw < 0) { + sc->images[index].busy = 0; + if (sc->status >= 0) sc->status = rw == VK_ERROR_DEVICE_LOST ? rw : VK_ERROR_SURFACE_LOST_KHR; + s->dead = 1; + } + pthread_cond_broadcast(&s->changed); + } + pthread_mutex_unlock(&sc->lock); + return NULL; +} + /* Waits for each queued frame's rendering, then shows it; between frames, takes the * compositor's events (buffer releases, frame callbacks) so images come back to the game. */ static void *present_thread(void *arg) @@ -504,6 +630,7 @@ static void *present_thread(void *arg) uint32_t index = have ? s->ring[s->head] : 0; uint32_t lat = have ? s->ring_lat[s->head] : 0; uint64_t pid = have ? s->ring_id[s->head] : 0; + int ewant = have ? s->ring_early[s->head] : 0; if (have) { s->head = (s->head + 1) % 64; s->len--; @@ -512,12 +639,20 @@ static void *present_thread(void *arg) if (have) { /* A slow frame (a game loading) is waited for; only a real loss or a frame past the limit * (VKBRIDGE_PRESENT_TIMEOUT_S) ends the swapchain (wsi.c) */ - VkResult r = vkb_wsi_wait_frame(sc, s->fences[index]); - if (r >= 0) vkb_lat_stamp(lat, VKB_LAT_DONE, 0); - pthread_mutex_lock(&sc->lock); - s->rendering--; /* a present waiting for the frame queue (wl_wait_queue) may go on */ - pthread_cond_broadcast(&s->changed); - pthread_mutex_unlock(&sc->lock); + VkResult r = VK_SUCCESS; + int early = 0; + if (ewant) { + if (!__atomic_load_n(&s->early_off, __ATOMIC_RELAXED)) early = early_attach(sc, s, index); + if (!early) s->e_fallback++; + } + if (!early) { + r = vkb_wsi_wait_frame(sc, s->fences[index]); + if (r >= 0) vkb_lat_stamp(lat, VKB_LAT_DONE, 0); + pthread_mutex_lock(&sc->lock); + s->rendering--; /* a present waiting for the frame queue (wl_wait_queue) may go on */ + pthread_cond_broadcast(&s->changed); + pthread_mutex_unlock(&sc->lock); + } s->lat_cur = lat; s->id_cur = pid; s->fb_for_id = 0; @@ -531,6 +666,35 @@ static void *present_thread(void *arg) if (shown) vkb_wsi_pw_committed(sc, pid, s->fb_for_id); s->lat_cur = 0; s->id_cur = 0; + if (early) { + /* committed with the fence attached; now the thread waits for the rendering, for its accounting and the fence's reuse */ + if (shown) s->e_frames++; + if (s->reaper_on) { + pthread_mutex_lock(&sc->lock); + s->reap_ring[(s->reap_head + s->reap_len) % 64] = index; + s->reap_lat[(s->reap_head + s->reap_len) % 64] = lat; + s->reap_len++; + s->reap_busy[index] = 1; + pthread_cond_signal(&s->reap_cond); + pthread_mutex_unlock(&sc->lock); + } else { + VkResult rw = vkb_wsi_wait_frame(sc, s->fences[index]); + if (rw >= 0) vkb_lat_stamp(lat, VKB_LAT_DONE, 0); + else if (r >= 0) r = rw; + pthread_mutex_lock(&sc->lock); + s->rendering--; + pthread_cond_broadcast(&s->changed); + pthread_mutex_unlock(&sc->lock); + } + } + if (stats_seconds() && ewant) { + uint64_t now = early_now(); + if (!s->e_last_ns) s->e_last_ns = now; + if (now - s->e_last_ns >= (uint64_t)stats_seconds() * 1000000000ull) { + early_line(s, NULL, 0); + s->e_last_ns = now; + } + } pthread_mutex_lock(&sc->lock); if (r < 0 || s->dead) { sc->images[index].busy = 0; @@ -573,13 +737,42 @@ static VkResult wl_present_async(vkb_swapchain *sc, VkQueue queue, uint32_t inde VkFenceCreateInfo fci = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; for (uint32_t i = 0; i < sc->count; i++) if (vkb_ep_vkCreateFence(device, &fci, NULL, &s->fences[i]) != VK_SUCCESS) return VK_ERROR_OUT_OF_HOST_MEMORY; + if (early_wanted() && !__atomic_load_n(&early_dead, __ATOMIC_RELAXED)) { + s->esem = calloc(sc->count, sizeof(VkSemaphore)); + VkExportSemaphoreCreateInfo xi = {VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO, NULL, + VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT}; + VkSemaphoreCreateInfo sci = {VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, &xi, 0}; + int ok = s->esem != NULL; + for (uint32_t i = 0; ok && i < sc->count; i++) ok = vkb_ep_vkCreateSemaphore(device, &sci, NULL, &s->esem[i]) == VK_SUCCESS; + if (!ok) { + VKB_WARN("present early: no exportable semaphores; presenting as without VKBRIDGE_PRESENT_EARLY"); + for (uint32_t i = 0; s->esem && i < sc->count; i++) + if (s->esem[i]) vkb_ep_vkDestroySemaphore(device, s->esem[i], NULL); + free(s->esem); + s->esem = NULL; + } else { + VKB_INFO("present early: on (VKBRIDGE_PRESENT_EARLY): frames are committed once their fence is in the dma-buf"); + } + } pthread_cond_init(&s->changed, NULL); + pthread_cond_init(&s->reap_cond, NULL); + s->reap_busy = calloc(sc->count, 1); + if (s->esem && s->reap_busy && reap_wanted() && pthread_create(&s->reaper, NULL, reaper_thread, sc) == 0) { + s->reaper_on = 1; + VKB_INFO("present early: the wait for a committed frame's rendering is on its own thread (VKBRIDGE_PRESENT_REAP=0 to turn off)"); + } if (pthread_create(&s->thread, NULL, present_thread, sc) != 0) return VK_ERROR_OUT_OF_HOST_MEMORY; s->thread_on = 1; } VkPipelineStageFlags *stages = alloca((nwait ? nwait : 1) * sizeof(VkPipelineStageFlags)); for (uint32_t i = 0; i < nwait; i++) stages[i] = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; - VkSubmitInfo si = {VK_STRUCTURE_TYPE_SUBMIT_INFO, NULL, nwait, wait, stages, 0, NULL, 0, NULL}; + int early = s->esem && !__atomic_load_n(&s->early_off, __ATOMIC_RELAXED) && !__atomic_load_n(&early_dead, __ATOMIC_RELAXED); + VkSubmitInfo si = {VK_STRUCTURE_TYPE_SUBMIT_INFO, NULL, nwait, wait, stages, 0, NULL, early, early ? &s->esem[index] : NULL}; + if (s->reaper_on) { /* the fence of this image's last frame is reset by the reaper thread: not before */ + pthread_mutex_lock(&sc->lock); + while (s->reap_busy[index] && sc->status >= 0 && !s->dead) pthread_cond_wait(&s->changed, &sc->lock); + pthread_mutex_unlock(&sc->lock); + } VkResult r = vkb_sm_queue_submit(queue, 1, &si, s->fences[index]); pthread_mutex_lock(&sc->lock); if (r != VK_SUCCESS || s->len == 64) { @@ -589,6 +782,7 @@ static VkResult wl_present_async(vkb_swapchain *sc, VkQueue queue, uint32_t inde } s->ring[(s->head + s->len) % 64] = index; s->ring_lat[(s->head + s->len) % 64] = sc->lat_n; + s->ring_early[(s->head + s->len) % 64] = (uint8_t)early; s->ring_id[(s->head + s->len) % 64] = sc->pw_cur; s->len++; s->rendering++; @@ -1158,6 +1352,16 @@ static void wl_destroy(vkb_swapchain *sc) uint64_t one = 1; if (write(s->efd, &one, sizeof(one)) < 0) { /* the counter is already set */ } pthread_join(s->thread, NULL); + if (s->reaper_on) { + pthread_mutex_lock(&sc->lock); + s->reap_quit = 1; + pthread_cond_signal(&s->reap_cond); + pthread_mutex_unlock(&sc->lock); + pthread_join(s->reaper, NULL); + s->reaper_on = 0; + } + free(s->reap_busy); + pthread_cond_destroy(&s->reap_cond); pthread_cond_destroy(&s->changed); s->thread_on = 0; } @@ -1167,6 +1371,13 @@ static void wl_destroy(vkb_swapchain *sc) free(s->fences); s->fences = NULL; } + if (s && s->esem) { + for (uint32_t i = 0; i < sc->count; i++) + if (s->esem[i]) vkb_ep_vkDestroySemaphore((VkDevice)sc->dev, s->esem[i], NULL); + free(s->esem); + s->esem = NULL; + early_line(s, __atomic_load_n(&s->early_off, __ATOMIC_RELAXED) ? "given up" : "swapchain ended", 1); + } if (s && s->efd > 0) close(s->efd); wl_teardown(sc->bdata); sc->bdata = NULL; diff --git a/host/CMakeLists.txt b/host/CMakeLists.txt index cf0e6e9..02a16cc 100644 --- a/host/CMakeLists.txt +++ b/host/CMakeLists.txt @@ -324,6 +324,14 @@ target_link_libraries(wire_check PRIVATE vkb_host_flags) target_compile_options(wire_check PRIVATE -O2) vkb_host_output(wire_check) +# present_early_check: VKBRIDGE_PRESENT_EARLY's fence-into-dma-buf step on a udmabuf (host/present_early_test.sh) +add_executable(present_early_check + "${VKB}/tests/present_early_check.c" + "${VKB}/client/dmabuf_attach.c" +) +target_link_libraries(present_early_check PRIVATE vkb_host_flags ${CMAKE_DL_LIBS}) +vkb_host_output(present_early_check) + # The capability check's SPIR-V scan (server/spirv_caps.c) on its own (run_ci_tests.sh, capgate_test.sh) add_executable(capgate_unit "${VKB}/tests/capgate_unit.c" diff --git a/tests/present_early_check.c b/tests/present_early_check.c new file mode 100644 index 0000000..03bb32b --- /dev/null +++ b/tests/present_early_check.c @@ -0,0 +1,174 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright (C) 2026 ASEbrahim and MaliBridge contributors + * + * present_early_check: what VKBRIDGE_PRESENT_EARLY (client/wsi_wayland.c) rests on, on a real dma-buf (udmabuf): + * a submit signals a sync-fd exportable binary semaphore, the semaphore is exported as a sync file, client/dmabuf_attach.c puts + * it into the dma-buf as a write fence, and a reader polling the dma-buf (gamescope's way) is not woken before the submit's + * GPU work is done. The submit waits for a timeline value the test signals later, so "not done yet" is certain. + * 1 the export returns a sync fd (the semaphore is one the submit signals together with a fence of the app's) + * 2 it attaches 3 (soft) the buffer is normally not readable right after 4 (hard) readable => the work is done; 3 rounds + * (Mesa defers a submit that waits on an unsignalled timeline, so its export would block: the work here is simply long.) + * Exits 77 (skip) without /dev/udmabuf, sync-fd export or the ioctl. + */ +#define _GNU_SOURCE +#define VK_NO_PROTOTYPES +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int vkb_dmabuf_attach_write_fence(int dmabuf_fd, int sync_fd); + +static int fails; +#define CHECK(c, ...) \ + do { \ + if (c) { printf("ok "); } else { printf("FAIL "); fails++; } \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } while (0) +#define SKIP(...) do { printf("SKIP: "); printf(__VA_ARGS__); printf("\n"); return 77; } while (0) + +static int make_dmabuf(void) +{ + int dev = open("/dev/udmabuf", O_RDWR | O_CLOEXEC); + if (dev < 0) return -1; + int mfd = memfd_create("present_early", MFD_ALLOW_SEALING); + if (mfd < 0 || ftruncate(mfd, 1 << 20) < 0) { close(dev); return -1; } + fcntl(mfd, F_ADD_SEALS, F_SEAL_SHRINK); + struct udmabuf_create c = {mfd, 0, 0, 1 << 20}; + int fd = ioctl(dev, UDMABUF_CREATE, &c); + close(dev); + close(mfd); + return fd; +} + +/* 1: readable (a write fence, if any, has signalled) within ms */ +static int readable(int fd, int ms) +{ + struct pollfd p = {fd, POLLIN, 0}; + return poll(&p, 1, ms) == 1 && (p.revents & POLLIN); +} + +int main(void) +{ + int dbuf = make_dmabuf(); + if (dbuf < 0) SKIP("no /dev/udmabuf access (%s)", strerror(errno)); + void *lib = dlopen("libvulkan.so.1", RTLD_NOW); + if (!lib) { printf("FAIL loader: %s\n", dlerror()); return 1; } + PFN_vkGetInstanceProcAddr gipa = (PFN_vkGetInstanceProcAddr)dlsym(lib, "vkGetInstanceProcAddr"); + VkInstance inst = NULL; +#define GI(name) PFN_##name name = (PFN_##name)gipa(inst, #name) + GI(vkCreateInstance); + VkApplicationInfo app = {VK_STRUCTURE_TYPE_APPLICATION_INFO, NULL, "present_early_check", 1, "none", 1, VK_API_VERSION_1_2}; + VkInstanceCreateInfo ici = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, NULL, 0, &app}; + if (vkCreateInstance(&ici, NULL, &inst) != VK_SUCCESS) { printf("FAIL instance\n"); return 1; } + GI(vkEnumeratePhysicalDevices); + GI(vkEnumerateDeviceExtensionProperties); + GI(vkGetPhysicalDeviceExternalSemaphoreProperties); + GI(vkCreateDevice); + PFN_vkGetDeviceProcAddr gdpa = (PFN_vkGetDeviceProcAddr)gipa(inst, "vkGetDeviceProcAddr"); + uint32_t n = 1; + VkPhysicalDevice pd = VK_NULL_HANDLE; + if (vkEnumeratePhysicalDevices(inst, &n, &pd) < 0 || !n || !pd) { printf("FAIL no GPU\n"); return 1; } + uint32_t en = 0; + vkEnumerateDeviceExtensionProperties(pd, NULL, &en, NULL); + VkExtensionProperties *e = calloc(en ? en : 1, sizeof(*e)); + vkEnumerateDeviceExtensionProperties(pd, NULL, &en, e); + int sfd = 0; + for (uint32_t i = 0; i < en; i++) sfd |= !strcmp(e[i].extensionName, "VK_KHR_external_semaphore_fd"); + free(e); + if (!sfd) SKIP("no VK_KHR_external_semaphore_fd"); + VkPhysicalDeviceExternalSemaphoreInfo xi = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_SEMAPHORE_INFO, NULL, + VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT}; + VkExternalSemaphoreProperties xp = {VK_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_PROPERTIES}; + vkGetPhysicalDeviceExternalSemaphoreProperties(pd, &xi, &xp); + if (!(xp.externalSemaphoreFeatures & VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT)) SKIP("SYNC_FD semaphores not exportable"); + + float prio = 1; + VkDeviceQueueCreateInfo q = {VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, NULL, 0, 0, 1, &prio}; + const char *exts[] = {"VK_KHR_external_semaphore_fd"}; + VkDeviceCreateInfo dci = {VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, NULL, 0, 1, &q, 0, NULL, 1, exts}; + VkDevice dev; + if (vkCreateDevice(pd, &dci, NULL, &dev) != VK_SUCCESS) { printf("FAIL device\n"); return 1; } +#define GD(name) PFN_##name name = (PFN_##name)gdpa(dev, #name) + GD(vkGetDeviceQueue); GD(vkQueueSubmit); GD(vkCreateSemaphore); GD(vkGetSemaphoreFdKHR); GD(vkDeviceWaitIdle); + GD(vkDestroySemaphore); GD(vkCreateFence); GD(vkGetFenceStatus); GD(vkResetFences); GD(vkCreateCommandPool); + GD(vkAllocateCommandBuffers); GD(vkBeginCommandBuffer); GD(vkEndCommandBuffer); GD(vkCmdFillBuffer); GD(vkCreateBuffer); + GD(vkGetBufferMemoryRequirements); GD(vkAllocateMemory); GD(vkBindBufferMemory); + GI(vkGetPhysicalDeviceMemoryProperties); + if (!vkGetSemaphoreFdKHR) { printf("FAIL vkGetSemaphoreFdKHR missing\n"); return 1; } + VkQueue queue; + vkGetDeviceQueue(dev, 0, 0, &queue); + + /* the "frame's rendering": a lot of fills of a big buffer, in one command buffer */ + VkBuffer big; + VkBufferCreateInfo bi = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, NULL, 0, 64u << 20, VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_SHARING_MODE_EXCLUSIVE}; + vkCreateBuffer(dev, &bi, NULL, &big); + VkMemoryRequirements mr; + vkGetBufferMemoryRequirements(dev, big, &mr); + VkPhysicalDeviceMemoryProperties mp; + vkGetPhysicalDeviceMemoryProperties(pd, &mp); + uint32_t t = 0; + while (t < mp.memoryTypeCount && !((mr.memoryTypeBits >> t) & 1)) t++; + VkMemoryAllocateInfo mai = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, NULL, mr.size, t}; + VkDeviceMemory bm; + vkAllocateMemory(dev, &mai, NULL, &bm); + vkBindBufferMemory(dev, big, bm, 0); + VkCommandPool pool; + VkCommandPoolCreateInfo pci = {VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, NULL, 0, 0}; + vkCreateCommandPool(dev, &pci, NULL, &pool); + VkCommandBufferAllocateInfo cai = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, NULL, pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1}; + VkCommandBuffer cb; + vkAllocateCommandBuffers(dev, &cai, &cb); + VkCommandBufferBeginInfo cbi = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; + vkBeginCommandBuffer(cb, &cbi); + for (int i = 0; i < 2000; i++) vkCmdFillBuffer(cb, big, 0, 64u << 20, (uint32_t)i); + vkEndCommandBuffer(cb); + + VkSemaphore bin; + VkExportSemaphoreCreateInfo ex = {VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO, NULL, VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT}; + VkSemaphoreCreateInfo sci = {VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, &ex, 0}; + VkFence fence; + VkFenceCreateInfo fci = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; + if (vkCreateSemaphore(dev, &sci, NULL, &bin) || vkCreateFence(dev, &fci, NULL, &fence)) { printf("FAIL semaphore/fence\n"); return 1; } + + CHECK(readable(dbuf, 0), "0 a fresh dma-buf is readable"); + for (int round = 1; round <= 3; round++) { + /* the present submit: signals the exportable semaphore and the bridge's own fence (the semaphore is the one exported) */ + VkSubmitInfo si = {VK_STRUCTURE_TYPE_SUBMIT_INFO, NULL, 0, NULL, NULL, 1, &cb, 1, &bin}; + VkResult r = vkQueueSubmit(queue, 1, &si, fence); + int fd = -1; + VkSemaphoreGetFdInfoKHR gi = {VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR, NULL, bin, VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT}; + VkResult g = vkGetSemaphoreFdKHR(dev, &gi, &fd); + CHECK(r == VK_SUCCESS && g == VK_SUCCESS && fd >= 0, "%d.1 submit %d, sync fd export %d, fd %d", round, r, g, fd); + if (fd < 0) return 1; + int err = vkb_dmabuf_attach_write_fence(dbuf, fd); + close(fd); + if (err == ENOTTY || err == EPERM || err == EACCES) SKIP("the kernel/host does not allow IMPORT_SYNC_FILE (%s)", strerror(err)); + CHECK(!err, "%d.2 fence attached to the dma-buf (%s)", round, strerror(err)); + /* soft: the work is long, so the buffer is normally not readable yet (only reported) */ + int early = readable(dbuf, 0); + int done_at_poll = vkGetFenceStatus(dev, fence) == VK_SUCCESS; + printf(" %d.3 right after the attach: dma-buf %s, fence %s%s\n", round, early ? "readable" : "not readable", + done_at_poll ? "signalled" : "pending", (early && !done_at_poll) ? " <-- readable before the work was done?" : ""); + /* hard: once the dma-buf is readable, the rendering is done */ + int rd = readable(dbuf, 20000); + /* the app's fence and the semaphore are two sync objects of one batch: the fence may trail by a few ms, not by the work */ + VkResult fs = vkGetFenceStatus(dev, fence); + for (int k = 0; k < 50 && fs != VK_SUCCESS; k++) { usleep(1000); fs = vkGetFenceStatus(dev, fence); } + CHECK(rd && fs == VK_SUCCESS, "%d.4 dma-buf readable (%d) with the work done (fence within 50 ms): status %d", round, rd, fs); + vkResetFences(dev, 1, &fence); + } + vkDeviceWaitIdle(dev); + vkDestroySemaphore(dev, bin, NULL); + printf(fails ? "FAILED: %d failure(s)\n" : "OK: 0 failure(s)\n", fails); + return fails ? 1 : 0; +}