diff --git a/src/asahi/meson.build b/src/asahi/meson.build index ba69e48a25f8..aa46f3c68545 100644 --- a/src/asahi/meson.build +++ b/src/asahi/meson.build @@ -237,6 +237,24 @@ if with_tools.contains('drm-shim') and with_asahi_vk and with_tests timeout : 120, ) + # A host-only vkCmdWaitEvents2 starts its poll in an ioctl sent after + # vkSetEvent; a device-set event's wait stays in the one ioctl. + test( + 'hk-g17p-host-event-wait', + prog_python, + args : [ + files('../gallium/drivers/asahi/tests/run-shim.py'), + libasahi_noop_drm_shim, + hk_vulkan_host_event_wait, + ], + env : [ + 'ASAHI_SHIM_CHIP=g17p', + 'VK_DRIVER_FILES=' + _dev_icd.full_path(), + ], + depends : [_dev_icd, libvulkan_asahi], + suite : ['asahi'], + ) + # Explicit DRM plane layouts: placement, requirements, subresource layouts # and validation, without executing on the GPU. foreach chip : ['g13g', 'g17p'] diff --git a/src/asahi/vulkan/hk_cmd_buffer.c b/src/asahi/vulkan/hk_cmd_buffer.c index 490a881863fe..fb358f6065f5 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.c +++ b/src/asahi/vulkan/hk_cmd_buffer.c @@ -320,6 +320,7 @@ hk_reset_cmd_buffer(struct vk_command_buffer *vk_cmd_buffer, cmd->geom_index_count = 0; cmd->geom_instance_count = 0; cmd->uses_heap = false; + cmd->host_waits = 0; hk_sync_reset(cmd); if (cmd->m5_pushed_images) _mesa_set_clear(cmd->m5_pushed_images, NULL); @@ -785,7 +786,7 @@ merge_control_streams(struct hk_cmd_buffer *cmd) !last->timestamp.end.handle && !(last->timestamp.start.handle && cs->g17p_timestamp_query) && !(last->timestamp.start.handle && cs->timestamp.start.handle) && - !hk_sync_merge_delays(cmd, last, cs)) { + !cs->host_wait && !hk_sync_merge_delays(cmd, last, cs)) { hk_sync_merge(cmd, last, cs); hk_cs_merge_cdm(last, cs); diff --git a/src/asahi/vulkan/hk_cmd_buffer.h b/src/asahi/vulkan/hk_cmd_buffer.h index 73137e054ff8..5c0e247d133a 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.h +++ b/src/asahi/vulkan/hk_cmd_buffer.h @@ -1163,6 +1163,11 @@ struct hk_cs { bool gen1_native_vertex_rq; bool m5_matrix_ops; + /* The host-set event a host-only vkCmdWaitEvents2 polls for in this + * stream: the stream starts a command of its own (merge_control_streams), + * which submission sends after vkSetEvent. */ + struct hk_event *host_wait; + /* Threads of the application's direct dispatches in this stream, and * whether it holds an indirect one (hk_queue::sib only takes streams with * enough work to overlap). */ @@ -1857,6 +1862,9 @@ struct hk_cmd_buffer { /* Does the command buffer use the geometry heap? */ bool uses_heap; + /* Streams with hk_cs::host_wait. */ + uint32_t host_waits; + /* Owned BOs and retained references needed by recorded commands. */ struct util_dynarray large_bos; /* Direct-mapped temporal cache avoids rescanning large_bos for the linked diff --git a/src/asahi/vulkan/hk_device.c b/src/asahi/vulkan/hk_device.c index 45e7b1476896..b430b366befc 100644 --- a/src/asahi/vulkan/hk_device.c +++ b/src/asahi/vulkan/hk_device.c @@ -1220,6 +1220,8 @@ hk_CreateDevice(VkPhysicalDevice physicalDevice, *pDevice = hk_device_to_handle(dev); + mtx_init(&dev->host_event.lock, mtx_plain); + cnd_init(&dev->host_event.set); simple_mtx_init(&dev->cmd_pending_lock, mtx_plain); list_inithead(&dev->cmd_pending_queues); list_inithead(&dev->cmd_pending_failed); @@ -1315,6 +1317,12 @@ hk_DestroyDevice(VkDevice _device, const VkAllocationCallbacks *pAllocator) /* Its thread and kicks use the queues and the device's kernels. */ hk_compute_kick_stop(dev); + /* A submission still waiting for a vkSetEvent fails instead. */ + mtx_lock(&dev->host_event.lock); + dev->host_event.stop = true; + cnd_broadcast(&dev->host_event.set); + mtx_unlock(&dev->host_event.lock); + util_dynarray_fini(&dev->external_bos.counts); util_dynarray_fini(&dev->external_bos.list); u_rwlock_destroy(&dev->external_bos.lock); @@ -1352,6 +1360,8 @@ hk_DestroyDevice(VkDevice _device, const VkAllocationCallbacks *pAllocator) * a new recording during teardown. Kernel job pins retain failed work. */ hk_cmd_pending_finish(dev); simple_mtx_destroy(&dev->cmd_pending_lock); + cnd_destroy(&dev->host_event.set); + mtx_destroy(&dev->host_event.lock); hk_device_finish_vdm_slabs(dev); simple_mtx_destroy(&dev->vdm.lock); diff --git a/src/asahi/vulkan/hk_device.h b/src/asahi/vulkan/hk_device.h index 320a22775c3f..be2755ad33eb 100644 --- a/src/asahi/vulkan/hk_device.h +++ b/src/asahi/vulkan/hk_device.h @@ -319,6 +319,15 @@ struct hk_device { uint32_t next_size[HK_CMD_SLAB_COUNT]; } vdm; + /* A host vkSetEvent wakes a submission waiting for it on a submit + * thread (hk_event_wait_host_set()); stop ends those waits when the + * device is destroyed, whose queues join their submit threads. */ + struct { + mtx_t lock; + cnd_t set; + bool stop; + } host_event; + bool cmd_pending_ready; simple_mtx_t cmd_pending_lock; /* Queues with pending submissions (hk_queue::cmd_link). */ diff --git a/src/asahi/vulkan/hk_event.c b/src/asahi/vulkan/hk_event.c index 5e2626e578a0..bea5c56c27f5 100644 --- a/src/asahi/vulkan/hk_event.c +++ b/src/asahi/vulkan/hk_event.c @@ -72,13 +72,46 @@ hk_GetEventStatus(VkDevice device, VkEvent _event) VKAPI_ATTR VkResult VKAPI_CALL hk_SetEvent(VkDevice device, VkEvent _event) { + VK_FROM_HANDLE(hk_device, dev, device); VK_FROM_HANDLE(hk_event, event, _event); + mtx_lock(&dev->host_event.lock); *event->status = VK_EVENT_SET; + event->host_sets++; + cnd_broadcast(&dev->host_event.set); + mtx_unlock(&dev->host_event.lock); return VK_SUCCESS; } +/* Whether the event is still reset. *sets is the count of host sets so far, + * for hk_event_wait_host_set(). */ +bool +hk_event_host_unset(struct hk_device *dev, struct hk_event *event, + uint32_t *sets) +{ + mtx_lock(&dev->host_event.lock); + *sets = event->host_sets; + const bool unset = *event->status != VK_EVENT_SET; + mtx_unlock(&dev->host_event.lock); + return unset; +} + +/* Until the host sets the event after the sets counted. False if the device + * is being destroyed first. */ +bool +hk_event_wait_host_set(struct hk_device *dev, struct hk_event *event, + uint32_t sets) +{ + mtx_lock(&dev->host_event.lock); + while (!dev->host_event.stop && event->host_sets == sets && + *event->status != VK_EVENT_SET) + cnd_wait(&dev->host_event.set, &dev->host_event.lock); + const bool set = !dev->host_event.stop; + mtx_unlock(&dev->host_event.lock); + return set; +} + VKAPI_ATTR VkResult VKAPI_CALL hk_ResetEvent(VkDevice device, VkEvent _event) { @@ -163,6 +196,24 @@ hk_event_wait_polls(const struct hk_device *dev) return dev->dev.chip == AGX_CHIP_G17P; } +/* Whether the first synchronization scope is host operations alone. Only + * vkSetEvent sets the event then: a wait on a host-set event names only the + * host stage (VUID-vkCmdWaitEvents2-pEvents-03839), and vkCmdSetEvent2, + * whose scope the wait repeats, cannot name it. A legacy vkCmdWaitEvents + * mask that adds device stages for other events is not host-only. */ +static bool +hk_dependency_host_only(const VkDependencyInfo *info) +{ + VkPipelineStageFlags2 src = 0; + for (uint32_t i = 0; i < info->memoryBarrierCount; ++i) + src |= info->pMemoryBarriers[i].srcStageMask; + for (uint32_t i = 0; i < info->bufferMemoryBarrierCount; ++i) + src |= info->pBufferMemoryBarriers[i].srcStageMask; + for (uint32_t i = 0; i < info->imageMemoryBarrierCount; ++i) + src |= info->pImageMemoryBarriers[i].srcStageMask; + return src == VK_PIPELINE_STAGE_2_HOST_BIT; +} + VKAPI_ATTR void VKAPI_CALL hk_CmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents, @@ -192,6 +243,14 @@ hk_CmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, hk_sync_src_dep(cmd, src_kinds, cmd->current_cs.cs, dep); if (hk_event_wait_polls(dev)) { + /* A host-only wait polls in a stream of its own: the GPU does not + * see a host write made while a command runs, so submission holds + * that stream's command back until vkSetEvent + * (queue_submit_looped()). */ + const bool host = hk_dependency_host_only(&pDependencyInfos[i]); + if (host) + hk_cmd_buffer_end_compute(cmd); + cmd->sync.kind_override = HK_SYNC_ALL; struct hk_cs *cs = hk_sync_get_cs_after(cmd, dep); if (cs) @@ -203,6 +262,10 @@ hk_CmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, /* Later work of the destination kinds runs after the poll. The * poll stream is ended so it cannot absorb that work. */ cs = cmd->current_cs.cs ? cmd->current_cs.cs : cs; + if (host && cs) { + cs->host_wait = event; + cmd->host_waits++; + } hk_sync_after_stream(cmd, cs, dst_kinds); hk_cmd_buffer_end_compute(cmd); } diff --git a/src/asahi/vulkan/hk_event.h b/src/asahi/vulkan/hk_event.h index c675ceada8ab..c03fb1172a07 100644 --- a/src/asahi/vulkan/hk_event.h +++ b/src/asahi/vulkan/hk_event.h @@ -17,6 +17,16 @@ struct hk_event { uint64_t addr; VkResult *status; + + /* Host sets, under hk_device::host_event.lock. */ + uint32_t host_sets; }; +struct hk_device; + +bool hk_event_host_unset(struct hk_device *dev, struct hk_event *event, + uint32_t *sets); +bool hk_event_wait_host_set(struct hk_device *dev, struct hk_event *event, + uint32_t sets); + VK_DEFINE_NONDISP_HANDLE_CASTS(hk_event, base, VkEvent, VK_OBJECT_TYPE_EVENT) diff --git a/src/asahi/vulkan/hk_queue.c b/src/asahi/vulkan/hk_queue.c index 7473a1ef8dec..e8f8fcf68e83 100644 --- a/src/asahi/vulkan/hk_queue.c +++ b/src/asahi/vulkan/hk_queue.c @@ -26,6 +26,7 @@ #include "hk_frame_anatomy.h" #include "hk_device.h" #include "hk_entrypoints.h" +#include "hk_event.h" #include "hk_drm_render_core.h" #include "hk_drm_prior_core.h" #include "hk_execution_allowance.h" @@ -551,11 +552,36 @@ queue_submit_single(struct hk_device *dev, struct drm_asahi_submit *submit, return VK_SUCCESS; } +/* A host-only event wait whose event was reset when its submission was + * built: the hardware command its poll starts, sent after vkSetEvent. */ +struct hk_host_cut { + unsigned command; + struct hk_event *event; + uint32_t sets; +}; + +/* Whether a submission may wait on its submit thread for a host vkSetEvent + * (queue_submit_looped()). The wait must hold no lock an application thread + * takes: the runtime holds the queue lock around a submission to a shared + * or internally synchronized queue, and hk_queue_admit() takes it too. */ +static bool +hk_queue_may_wait_host(struct hk_queue *queue) +{ + return !queue->vk.real_queue && !queue->vk.internally_synchronized && + vk_device_supports_threaded_submit(queue->vk.base.device); +} + /* * The kernel/firmware jointly impose a limit on commands per submit ioctl, but * we can build up arbitrarily large command buffers. We handle this here by * looping the ioctl, submitting slices of the command buffers that are within * bounds. + * + * An ioctl also ends before each host cut, whose command is sent once the + * host has set its event: G17P loads do not see a host write made while their + * command runs, only one made before it starts, so a poll already running + * would never end. The kernel resolves its waits at submission, so waiting + * for a host signal there is not possible. */ static VkResult queue_submit_looped(struct hk_device *dev, struct hk_queue *queue, @@ -563,7 +589,8 @@ queue_submit_looped(struct hk_device *dev, struct hk_queue *queue, unsigned ring_idx, const uint64_t prior_base[2], bool chunk_timelines, uint64_t first_chunk_point, struct hk_frame_anatomy_submit *anatomy, - struct hk_cmd_pending *pending) + struct hk_cmd_pending *pending, + const struct hk_host_cut *cut, unsigned cut_count) { uint8_t *cmdbuf = (uint8_t *)(uintptr_t)submit->cmdbuf; uint32_t offs = 0; @@ -581,11 +608,24 @@ queue_submit_looped(struct hk_device *dev, struct hk_queue *queue, chunk_signal->sync_type == DRM_ASAHI_SYNC_TIMELINE_SYNCOBJ && chunk_signal->handle == queue->drm.syncobj); + const struct hk_host_cut *cut_end = cut + cut_count; while (commands_remaining) { - bool first = commands_remaining == command_count; - bool last = commands_remaining <= max_commands_per_submit(dev); + const unsigned at = command_count - commands_remaining; + if (cut != cut_end && cut->command == at) { + if (!hk_event_wait_host_set(dev, cut->event, cut->sets)) { + if (chunk_timelines) + queue->drm.timeline_value = last_attempted_point; + return vk_device_set_lost( + &dev->vk, "Device destroyed before a host-only event wait"); + } + cut++; + } + bool first = commands_remaining == command_count; unsigned count = MIN2(commands_remaining, max_commands_per_submit(dev)); + if (cut != cut_end) + count = MIN2(count, cut->command - at); + bool last = count == commands_remaining; commands_remaining -= count; assert(!last || commands_remaining == 0); @@ -1401,7 +1441,7 @@ hk_QueueSubmit2(VkQueue _queue, uint32_t submitCount, const bool rq_capable = dev->gen1_native_graphics_rq || dev->gen1_native_fragment_rq; - bool needs_queue = false; + bool needs_queue = false, host_waits = false; unsigned render_rq = 0; for (uint32_t i = 0; i < submitCount; i++) { for (uint32_t j = 0; j < pSubmits[i].commandBufferInfoCount; j++) { @@ -1410,6 +1450,7 @@ hk_QueueSubmit2(VkQueue _queue, uint32_t submitCount, if (list_is_empty(&cmd->control_streams)) continue; needs_queue = true; + host_waits |= cmd->host_waits != 0; if (rq_capable) render_rq |= hk_cmd_buffer_needs_render_rq(cmd); } @@ -1419,6 +1460,17 @@ hk_QueueSubmit2(VkQueue _queue, uint32_t submitCount, if (result != VK_SUCCESS) return result; } + + /* A host-only event wait may hold its submission back until + * vkSetEvent (queue_submit_looped()), which only a submit thread can + * wait for. The queue keeps the thread, as for a wait before signal. */ + if (host_waits && + queue->vk.submit.mode != VK_QUEUE_SUBMIT_MODE_THREADED && + hk_queue_may_wait_host(queue)) { + VkResult result = vk_queue_enable_submit_thread(&queue->vk); + if (result != VK_SUCCESS) + return result; + } } return vk_common_QueueSubmit2(_queue, submitCount, pSubmits, fence); @@ -1591,6 +1643,7 @@ queue_submit_recordings(struct hk_device *dev, struct hk_queue *queue, } unsigned command_count = flush_sstt ? 1 : 0; + unsigned host_waits = 0; /* Gather the number of individual commands to submit up front */ for (unsigned i = 0; i < submit->command_buffer_count; ++i) { @@ -1598,8 +1651,15 @@ queue_submit_recordings(struct hk_device *dev, struct hk_queue *queue, (struct hk_cmd_buffer *)submit->command_buffers[i]; command_count += list_length(&cmdbuf->control_streams); + host_waits += cmdbuf->host_waits; } + /* On the submit thread hk_QueueSubmit2() started, each host-only event + * wait may end an ioctl (queue_submit_looped()). */ + if (host_waits && (queue->vk.submit.mode != VK_QUEUE_SUBMIT_MODE_THREADED || + !hk_queue_may_wait_host(queue))) + host_waits = 0; + perf_debug_dev(&dev->dev, "Submitting %u control streams (%u command buffers)", command_count, submit->command_buffer_count); @@ -1878,7 +1938,7 @@ queue_submit_recordings(struct hk_device *dev, struct hk_queue *queue, const bool sib_remap = queue->sib.count > 1 && queue->sib.used; bool sib_earlier = false; if (queue->sib.count > 1 && !replaced && !flush_sstt && !unlikely(anatomy) && - !packed_bo && + !packed_bo && !host_waits && hk_sibling_eligible(dev, queue, submit, resolved, prior_base, command_count, kernel_cumulative) && hk_sibling_deep(dev, queue, first_chunk_point - 1)) { @@ -1902,6 +1962,10 @@ queue_submit_recordings(struct hk_device *dev, struct hk_queue *queue, kq_base[HK_SYNC_CDM] = queue->sib.submitted[sib_target][HK_SYNC_CDM]; } + struct hk_host_cut *cuts = + host_waits ? alloca(host_waits * sizeof(*cuts)) : NULL; + unsigned cut_count = 0; + for (unsigned i = 0; i < submit->command_buffer_count; ++i) { struct hk_cmd_buffer *cmdbuf = (struct hk_cmd_buffer *)submit->command_buffers[i]; @@ -1920,6 +1984,18 @@ queue_submit_recordings(struct hk_device *dev, struct hk_queue *queue, list_for_each_entry(struct hk_cs, cs, &cmdbuf->control_streams, node) { if (resolved) hk_sync_load_resolved(cs, resolved++); + + /* A set event needs no cut: the poll starts after the set. */ + uint32_t sets; + if (host_waits && cs->host_wait && + hk_event_host_unset(dev, cs->host_wait, &sets)) { + cuts[cut_count++] = (struct hk_host_cut){ + .command = nr_vdm + nr_cdm, + .event = cs->host_wait, + .sets = sets, + }; + } + struct drm_asahi_cmd_header header = agx_cmd_header(cs->type == HK_CS_CDM, cs->sync.barrier[HK_SYNC_VDM], cs->sync.barrier[HK_SYNC_CDM]); @@ -2308,7 +2384,7 @@ queue_submit_recordings(struct hk_device *dev, struct hk_queue *queue, hk_frame_anatomy_prepare(anatomy, dev, payload.data, payload.size); VkResult result; - if (command_count <= max_commands_per_submit(dev)) { + if (command_count <= max_commands_per_submit(dev) && !cut_count) { if (unlikely(anatomy)) hk_frame_anatomy_note_barriers(anatomy, payload.data, 0, payload.size); result = @@ -2317,7 +2393,7 @@ queue_submit_recordings(struct hk_device *dev, struct hk_queue *queue, result = queue_submit_looped(dev, queue, &submit_ioctl, command_count, queue->drm.virt_ring_idx, kq_base, chunk_timelines, first_chunk_point, anatomy, - pending); + pending, cuts, cut_count); } hk_cmd_pending_end(dev, pending, queue, result); if (queue->sib.count > 1 && result == VK_SUCCESS) { diff --git a/src/asahi/vulkan/meson.build b/src/asahi/vulkan/meson.build index c2e2b2c29fd8..afb21254e994 100644 --- a/src/asahi/vulkan/meson.build +++ b/src/asahi/vulkan/meson.build @@ -1063,6 +1063,15 @@ if with_tests gnu_symbol_visibility : 'hidden', ) + hk_vulkan_host_event_wait = executable( + 'hk-vulkan-host-event-wait', + files('tests/hk_vulkan_host_event_wait.c'), + include_directories : [inc_include], + dependencies : [cc.find_library('vulkan')], + c_args : [c_msvc_compat_args], + gnu_symbol_visibility : 'hidden', + ) + hk_vulkan_descriptor_buffer_sizes = executable( 'hk-vulkan-descriptor-buffer-sizes', files('tests/hk_vulkan_descriptor_buffer_sizes.c'), diff --git a/src/asahi/vulkan/tests/hk_vulkan_host_event_wait.c b/src/asahi/vulkan/tests/hk_vulkan_host_event_wait.c new file mode 100644 index 000000000000..5beb7c9fd1cf --- /dev/null +++ b/src/asahi/vulkan/tests/hk_vulkan_host_event_wait.c @@ -0,0 +1,317 @@ +/* Copyright 2026 Aurora Silicon + * SPDX-License-Identifier: MIT + * + * G17P loads do not see a host write made while their command runs, so the + * poll of a vkCmdWaitEvents2 on an event the host sets later must start in an + * ioctl sent after vkSetEvent. Under the no-op DRM shim, which logs every + * submit ioctl (ASAHI_SHIM_SUBMIT_LOG), a fill, a wait and a fill go as: + * - host-only source, event reset: one ioctl before vkSetEvent, and one + * after it that starts with the poll; + * - host-only source, event already set: one ioctl; + * - a device-set event (vkCmdSetEvent2, compute source): one ioctl; + * - a fill command buffer, then one that opens with the host-only wait: + * the same two ioctls, the poll does not continue the fill's command; + * - vkDestroyDevice with a host-only wait whose set never comes returns. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#define CHECK(call) \ + do { \ + VkResult r_ = (call); \ + if (r_ != VK_SUCCESS) { \ + fprintf(stderr, "FAIL: %s: %d\n", #call, r_); \ + exit(1); \ + } \ + } while (0) + +static char log_path[256]; + +/* Submit ioctls logged so far; *first_cmd: the first hardware command of the + * last one. */ +static unsigned +submits(char first_cmd[16]) +{ + FILE *f = fopen(log_path, "r"); + if (!f) + return 0; + unsigned n = 0; + bool want = false; + char line[512]; + while (fgets(line, sizeof(line), f)) { + if (!strncmp(line, "submit ", 7)) { + n++; + want = true; + } else if (want && (!strncmp(line, " cmd compute", 13) || + !strncmp(line, " cmd render", 12))) { + if (first_cmd) + snprintf(first_cmd, 16, "%s", line[6] == 'c' ? "compute" : "render"); + want = false; + } + } + fclose(f); + return n; +} + +static void +sleep_ms(unsigned ms) +{ + const struct timespec t = {ms / 1000, (ms % 1000) * 1000000L}; + nanosleep(&t, NULL); +} + +static VkDevice dev; +static VkQueue queue; +static VkCommandPool pool; +static VkBuffer buffer; + +static VkCommandBuffer +record(VkEvent event, bool host, bool lead) +{ + const VkCommandBufferAllocateInfo alloc = { + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, + .commandPool = pool, + .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, + .commandBufferCount = 1, + }; + VkCommandBuffer cb; + CHECK(vkAllocateCommandBuffers(dev, &alloc, &cb)); + const VkCommandBufferBeginInfo begin = { + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, + }; + CHECK(vkBeginCommandBuffer(cb, &begin)); + if (lead) + vkCmdFillBuffer(cb, buffer, 0, 256, 1); + if (event == VK_NULL_HANDLE) { + /* Flushed, the fill's command could take the next command buffer's + * first stream (hk_cs_cdm_may_continue()). */ + const VkMemoryBarrier2 flush = { + .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, + .srcStageMask = VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT, + .srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT, + }; + const VkDependencyInfo flush_dep = { + .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, + .memoryBarrierCount = 1, + .pMemoryBarriers = &flush, + }; + vkCmdPipelineBarrier2(cb, &flush_dep); + CHECK(vkEndCommandBuffer(cb)); + return cb; + } + + const VkMemoryBarrier2 barrier = { + .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, + .srcStageMask = host ? VK_PIPELINE_STAGE_2_HOST_BIT + : VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT, + .srcAccessMask = host ? VK_ACCESS_2_HOST_WRITE_BIT + : VK_ACCESS_2_TRANSFER_WRITE_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT, + .dstAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT, + }; + const VkDependencyInfo dep = { + .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, + .memoryBarrierCount = 1, + .pMemoryBarriers = &barrier, + }; + if (!host) + vkCmdSetEvent2(cb, event, &dep); + vkCmdWaitEvents2(cb, 1, &event, &dep); + vkCmdFillBuffer(cb, buffer, 256, 256, 2); + CHECK(vkEndCommandBuffer(cb)); + return cb; +} + +/* The ioctls since before, once count are there or 5 s passed, and 50 ms + * more. The submit thread sends them; the shim's syncobjs do not wait, so + * vkQueueWaitIdle() would not wait for that thread. */ +static unsigned +settle(unsigned before, unsigned count) +{ + unsigned n = 0; + for (unsigned t = 0; t < 5000 && (n = submits(NULL) - before) < count; ++t) + sleep_ms(1); + sleep_ms(50); + return submits(NULL) - before; +} + +static void +submit_n(const VkCommandBuffer *cbs, uint32_t count) +{ + const VkSubmitInfo info = { + .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, + .commandBufferCount = count, + .pCommandBuffers = cbs, + }; + CHECK(vkQueueSubmit(queue, 1, &info, VK_NULL_HANDLE)); +} + +static void +submit(VkCommandBuffer cb) +{ + submit_n(&cb, 1); +} + +/* A host-only wait set after the submission: one ioctl before the set, + * a second one starting with the poll after it. */ +static int +check_held_back(const char *what, const VkCommandBuffer *cbs, uint32_t count, + VkEvent event) +{ + int fails = 0; + CHECK(vkResetEvent(dev, event)); + unsigned before = submits(NULL); + submit_n(cbs, count); + unsigned n = settle(before, 1); + if (n != 1) { + fprintf(stderr, "FAIL: %s: %u ioctls before vkSetEvent, expected 1\n", + what, n); + fails++; + } + CHECK(vkSetEvent(dev, event)); + n = settle(before, 2); + char first[16] = ""; + submits(first); + if (n != 2 || strcmp(first, "compute")) { + fprintf(stderr, "FAIL: %s: %u ioctls after vkSetEvent, the last " + "starting with %s, expected 2 starting with compute\n", + what, n, first[0] ? first : "nothing"); + fails++; + } + return fails; +} + +int +main(void) +{ + snprintf(log_path, sizeof(log_path), "/tmp/hk-host-event-wait-%d.log", + (int)getpid()); + unlink(log_path); + setenv("ASAHI_SHIM_SUBMIT_LOG", log_path, 1); + + const VkApplicationInfo app = { + .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, + .apiVersion = VK_API_VERSION_1_3, + }; + const VkInstanceCreateInfo instance_info = { + .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, + .pApplicationInfo = &app, + }; + VkInstance instance; + CHECK(vkCreateInstance(&instance_info, NULL, &instance)); + uint32_t count = 1; + VkPhysicalDevice pdev; + VkResult r = vkEnumeratePhysicalDevices(instance, &count, &pdev); + if ((r != VK_SUCCESS && r != VK_INCOMPLETE) || count == 0) { + fprintf(stderr, "no physical device\n"); + return 2; + } + + const float priority = 1.0f; + const VkDeviceQueueCreateInfo queue_info = { + .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, + .queueFamilyIndex = 0, + .queueCount = 1, + .pQueuePriorities = &priority, + }; + VkPhysicalDeviceVulkan13Features features13 = { + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES, + .synchronization2 = VK_TRUE, + }; + const VkDeviceCreateInfo device_info = { + .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, + .pNext = &features13, + .queueCreateInfoCount = 1, + .pQueueCreateInfos = &queue_info, + }; + CHECK(vkCreateDevice(pdev, &device_info, NULL, &dev)); + vkGetDeviceQueue(dev, 0, 0, &queue); + + const VkCommandPoolCreateInfo pool_info = { + .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, + .queueFamilyIndex = 0, + }; + CHECK(vkCreateCommandPool(dev, &pool_info, NULL, &pool)); + + const VkBufferCreateInfo buffer_info = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .size = 4096, + .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT, + }; + CHECK(vkCreateBuffer(dev, &buffer_info, NULL, &buffer)); + VkMemoryRequirements reqs; + vkGetBufferMemoryRequirements(dev, buffer, &reqs); + uint32_t type = 0; + while (type < 32 && !(reqs.memoryTypeBits & (1u << type))) + type++; + const VkMemoryAllocateInfo alloc_info = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = reqs.size, + .memoryTypeIndex = type, + }; + VkDeviceMemory memory; + CHECK(vkAllocateMemory(dev, &alloc_info, NULL, &memory)); + CHECK(vkBindBufferMemory(dev, buffer, memory, 0)); + + const VkEventCreateInfo event_info = { + .sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO, + }; + VkEvent event; + CHECK(vkCreateEvent(dev, &event_info, NULL, &event)); + int fails = 0; + + /* Host-only source, the host sets the event after the submission. */ + VkCommandBuffer host_cb = record(event, true, true); + fails += check_held_back("host wait", &host_cb, 1, event); + + /* The same with the event already set: nothing to hold back. */ + unsigned before = submits(NULL); + submit(host_cb); + unsigned n = settle(before, 1); + if (n != 1) { + fprintf(stderr, "FAIL: host wait on a set event: %u ioctls, " + "expected 1\n", n); + fails++; + } + + /* A device-set event is not held back. */ + CHECK(vkResetEvent(dev, event)); + VkCommandBuffer device_cb = record(event, false, true); + before = submits(NULL); + submit(device_cb); + n = settle(before, 1); + if (n != 1) { + fprintf(stderr, "FAIL: device-set event wait: %u ioctls, expected 1\n", + n); + fails++; + } + + /* A poll opening a command buffer does not continue the command of the + * one before. */ + const VkCommandBuffer pair[2] = {record(VK_NULL_HANDLE, false, true), + record(event, true, false)}; + fails += check_held_back("host wait after a fill", pair, 2, event); + + /* A wait whose set never comes ends with the device. */ + CHECK(vkResetEvent(dev, event)); + before = submits(NULL); + submit(host_cb); + settle(before, 1); + /* The command buffer is still pending: keep what it uses. */ + alarm(20); + vkDestroyDevice(dev, NULL); + vkDestroyInstance(instance, NULL); + unlink(log_path); + + if (!fails) + printf("PASS: a host-only event wait starts after vkSetEvent\n"); + return fails ? 1 : 0; +}