From f07f684a6c8df030b132223898bfbfb730f62d2f Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Tue, 17 Nov 2026 17:02:12 -0300 Subject: [PATCH] hk: resolve each occurrence of a command buffer in a submission A simultaneous-use command buffer may occur more than once in one vkQueueSubmit, for example {c0, c1, c0}. hk_sync_resolve_submit() kept the resolved barrier and prior fields in each hk_cs, so the second occurrence of c0 overwrote those of the first before the payload was built. The first occurrence then went out with the second's fields: a wait on a command after it, which the G17 kernel refuses (barrier past the commands before it, EINVAL) and the device is lost. Resolve every occurrence completely, the first-command chain and the vertex-half split included, before the next one starts, and save its fields to a per-queue array in submission order. queue_submit() and the sibling queue check read that array, and the payload loop loads each entry into its stream before emitting it. A sparse bind that only flushes the sparse table has no stream to resolve: the array is not resized for it, since util_dynarray_resize() returns NULL for zero entries of a never-allocated array, which lost the device on a queue's first such bind (vkd3d-proton's tiled-resource tests). The hk-g17p-sparse-bind-first-submit shim test binds one tile as a queue's first submission. Signed-off-by: Jonathan Goyvaerts Assisted-by: LLM --- src/asahi/meson.build | 17 ++ src/asahi/vulkan/hk_queue.c | 29 +++- src/asahi/vulkan/hk_queue.h | 4 + src/asahi/vulkan/hk_sync.c | 128 +++++++------- src/asahi/vulkan/hk_sync.h | 24 ++- src/asahi/vulkan/meson.build | 9 + .../hk_vulkan_sparse_bind_first_submit.c | 162 ++++++++++++++++++ src/asahi/vulkan/tests/test-hk-sync-prior.c | 81 ++++++--- 8 files changed, 363 insertions(+), 91 deletions(-) create mode 100644 src/asahi/vulkan/tests/hk_vulkan_sparse_bind_first_submit.c diff --git a/src/asahi/meson.build b/src/asahi/meson.build index 6ed75ee37ba9..63bf298bb5a1 100644 --- a/src/asahi/meson.build +++ b/src/asahi/meson.build @@ -143,6 +143,23 @@ if with_tools.contains('drm-shim') and with_asahi_vk and with_tests suite : ['asahi'], ) + # A sparse image bind as a queue's first submission, with no command buffer. + test( + 'hk-g17p-sparse-bind-first-submit', + prog_python, + args : [ + files('../gallium/drivers/asahi/tests/run-shim.py'), + libasahi_noop_drm_shim, + hk_vulkan_sparse_bind_first_submit, + ], + 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_queue.c b/src/asahi/vulkan/hk_queue.c index 63cc80edfb62..ca16e6cec025 100644 --- a/src/asahi/vulkan/hk_queue.c +++ b/src/asahi/vulkan/hk_queue.c @@ -1479,6 +1479,7 @@ hk_QueueBindSparse(VkQueue _queue, uint32_t bindInfoCount, static bool hk_sibling_eligible(struct hk_device *dev, const struct hk_queue *queue, const struct vk_queue_submit *submit, + const struct hk_sync_resolved *resolved, const uint64_t base[HK_SYNC_NUM_SQ], unsigned command_count, bool kernel_cumulative) { @@ -1506,9 +1507,10 @@ hk_sibling_eligible(struct hk_device *dev, const struct hk_queue *queue, return false; /* A zero barrier names every command of earlier ioctls. */ + const struct hk_sync_resolved *r = resolved++; for (unsigned sq = 0; sq < HK_SYNC_NUM_SQ; ++sq) { - if ((cs->sync.prior[sq] && cs->sync.prior[sq] <= base[sq]) || - cs->sync.barrier[sq] == 0) + if ((r->prior[sq] && r->prior[sq] <= base[sq]) || + r->barrier[sq] == 0) return false; } } @@ -1594,11 +1596,21 @@ queue_submit(struct hk_device *dev, struct hk_queue *queue, if (queue->sync.exact_prior && !hk_sync_resolve_submit( queue, (struct hk_cmd_buffer *const *)submit->command_buffers, - submit->command_buffer_count, false, false, 0)) + submit->command_buffer_count, false, false, 0, NULL)) return vk_device_set_lost(&dev->vk, "Asahi command ordinal overflow"); return queue_submit_empty(dev, queue, submit); } + /* Resolved sync fields, one entry per stream occurrence: a + * simultaneous-use command buffer may occur more than once. A sparse bind + * that only flushes the sparse table has none, and resizing a never + * allocated array to zero returns NULL. */ + const unsigned resolved_count = command_count - flush_sstt; + if (resolved_count && + !util_dynarray_resize(&queue->sync_resolved, struct hk_sync_resolved, + resolved_count)) + return vk_error(queue, VK_ERROR_OUT_OF_HOST_MEMORY); + bool replaced; VkResult queue_result = hk_queue_ensure_drm( dev, queue, hk_submit_needs_render_rq(submit), &replaced); @@ -1803,13 +1815,15 @@ queue_submit(struct hk_device *dev, struct hk_queue *queue, * (hk_drm_serialize_prefix), and keeps no ordinals, so it skips this. */ const bool resolve = !queue->sync.serialize || queue->sync.exact_prior || queue->sync.stage_overlap; + const struct hk_sync_resolved *resolved = + resolve ? queue->sync_resolved.data : NULL; if (resolve && !hk_sync_resolve_submit(queue, (struct hk_cmd_buffer *const *)submit->command_buffers, submit->command_buffer_count, flush_sstt, wait_count > 0 && !(dev->dev.params.features & DRM_ASAHI_FEATURE_PER_COMMAND_INPUT_SYNCS), - completed)) { + completed, queue->sync_resolved.data)) { util_dynarray_fini(&payload); if (unlikely(anatomy)) hk_frame_anatomy_end_submit(anatomy, dev, queue, VK_ERROR_DEVICE_LOST); @@ -1825,8 +1839,8 @@ queue_submit(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) && - hk_sibling_eligible(dev, queue, submit, prior_base, command_count, - kernel_cumulative) && + hk_sibling_eligible(dev, queue, submit, resolved, prior_base, + command_count, kernel_cumulative) && hk_sibling_deep(dev, queue, first_chunk_point - 1)) { sib_target = queue->sib.next; queue->sib.next = (queue->sib.next + 1) % queue->sib.count; @@ -1864,6 +1878,8 @@ queue_submit(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++); 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]); @@ -2651,5 +2667,6 @@ hk_queue_finish(struct hk_device *dev, struct hk_queue *queue) agx_destroy_command_queue(&dev->dev, queue->sib.id[i]); } agx_bo_unreference(&dev->dev, queue->g17p_sstt_flush_bo); + util_dynarray_fini(&queue->sync_resolved); vk_free(&dev->vk.alloc, queue); } diff --git a/src/asahi/vulkan/hk_queue.h b/src/asahi/vulkan/hk_queue.h index 6c0a723d5fb9..910af5aa06d7 100644 --- a/src/asahi/vulkan/hk_queue.h +++ b/src/asahi/vulkan/hk_queue.h @@ -68,6 +68,10 @@ struct hk_queue { /* Kinds of streams this queue has ever run, see hk_sync.h. */ struct hk_sync_queue sync; + /* Scratch for queue_submit: the resolved sync fields of each stream + * occurrence of the submission being built (hk_sync_resolve_submit()). */ + struct util_dynarray sync_resolved; + /* Compute kick (hk_compute_kick.c). watched is set at device creation * for a queue whose priority class has a kick queue. The rest is * protected by the kicker's lock. */ diff --git a/src/asahi/vulkan/hk_sync.c b/src/asahi/vulkan/hk_sync.c index afe0e937fbfc..ff14f9ff4b48 100644 --- a/src/asahi/vulkan/hk_sync.c +++ b/src/asahi/vulkan/hk_sync.c @@ -1779,11 +1779,21 @@ resolve_one(const struct submit_state *ss, unsigned sq, } } +void +hk_sync_load_resolved(struct hk_cs *cs, const struct hk_sync_resolved *r) +{ + memcpy(cs->sync.barrier, r->barrier, sizeof(r->barrier)); + memcpy(cs->sync.barrier_ta, r->barrier_ta, sizeof(r->barrier_ta)); + memcpy(cs->sync.prior, r->prior, sizeof(r->prior)); + memcpy(cs->sync.prior_ta, r->prior_ta, sizeof(r->prior_ta)); + cs->sync.ta_split = r->ta_split; +} + bool hk_sync_resolve_submit(struct hk_queue *queue, struct hk_cmd_buffer *const *cmdbufs, unsigned count, bool first_cdm_is_flush, bool chain_first, - uint64_t completed) + uint64_t completed, struct hk_sync_resolved *out) { /* Validate all ordinal arithmetic before mutating command or queue state. * Historical ordinals use zero for absence, so wrapping them is invalid. @@ -1857,6 +1867,32 @@ hk_sync_resolve_submit(struct hk_queue *queue, } } + /* Legacy kernels attach input syncs to the first hardware command only; + * the caller requests that chain when needed. A sparse flush must still + * precede everything. Every + * command that does not already follow the first one transitively gets + * an explicit wait on it. Waits are prefixes, so following any command + * that follows the first is enough. */ + /* The first command is the first stream of the first command buffer that + * emits any: command buffers without streams (barrier-only or empty) send + * nothing to the kernel. Without any command there is nothing to chain. */ + int first_sq = first_cdm_is_flush ? HK_SYNC_CDM : -1; + for (unsigned i = 0; first_sq < 0 && i < count; ++i) { + if (!list_is_empty(&cmdbufs[i]->control_streams)) + first_sq = hk_sync_sq(list_first_entry(&cmdbufs[i]->control_streams, + struct hk_cs, node)); + } + const bool chain = (chain_first || first_cdm_is_flush) && first_sq >= 0; + + /* covered[sq]: the lowest index on sq of a command that follows the + * first command, 0 if none yet. A wait on prefix b of subqueue d + * follows the first command when covered[d] is within that prefix. */ + uint32_t covered[HK_SYNC_NUM_SQ] = {0, first_cdm_is_flush ? 1 : 0}; + bool seen_first = first_cdm_is_flush; + + /* A command buffer may occur more than once: everything below that reads + * or writes a stream's index and resolved fields finishes with one + * occurrence, and saves it to out, before the next one starts. */ for (unsigned i = 0; i < count; ++i) { struct hk_cmd_buffer *cmd = cmdbufs[i]; @@ -1969,36 +2005,9 @@ hk_sync_resolve_submit(struct hk_queue *queue, ss.last[k][sq] = value_max(ss.last[k][sq], last->sync.index); } } - } - - /* Legacy kernels attach input syncs to the first hardware command only; - * the caller requests that chain when needed. A sparse flush must still - * precede everything. Every - * command that does not already follow the first one transitively gets - * an explicit wait on it. Waits are prefixes, so following any command - * that follows the first is enough. */ - /* The first command is the first stream of the first command buffer that - * emits any: command buffers without streams (barrier-only or empty) send - * nothing to the kernel. Without any command there is nothing to chain. */ - int first_sq = first_cdm_is_flush ? HK_SYNC_CDM : -1; - for (unsigned i = 0; first_sq < 0 && i < count; ++i) { - if (!list_is_empty(&cmdbufs[i]->control_streams)) - first_sq = hk_sync_sq(list_first_entry(&cmdbufs[i]->control_streams, - struct hk_cs, node)); - } - - if ((chain_first || first_cdm_is_flush) && first_sq >= 0) { - /* covered[sq]: the lowest index on sq of a command that follows the - * first command, 0 if none yet. A wait on prefix b of subqueue d - * follows the first command when covered[d] is within that prefix. */ - uint32_t covered[HK_SYNC_NUM_SQ] = {0, 0}; - if (first_cdm_is_flush) - covered[HK_SYNC_CDM] = 1; - bool seen_first = first_cdm_is_flush; - for (unsigned i = 0; i < count; ++i) { - list_for_each_entry(struct hk_cs, cs, &cmdbufs[i]->control_streams, - node) { + list_for_each_entry(struct hk_cs, cs, &cmd->control_streams, node) { + if (chain) { const unsigned sq = hk_sync_sq(cs); bool covers = false; @@ -2023,41 +2032,44 @@ hk_sync_resolve_submit(struct hk_queue *queue, if (covers && covered[sq] == 0) covered[sq] = cs->sync.index; } - } - } - /* A render's vertex half may wait for less than its fragment half only - * when the kernel orders the halves separately and nothing above raised - * the whole render's fields afterwards (cross-command-buffer tile hazards - * and the first-command chain only touch barrier[]): recompute, and keep - * the vertex half at least as strict as before. */ - for (unsigned i = 0; i < count; ++i) { - list_for_each_entry(struct hk_cs, cs, &cmdbufs[i]->control_streams, - node) { + /* A render's vertex half may wait for less than its fragment half + * only when the kernel orders the halves separately and nothing + * above raised the whole render's fields afterwards + * (cross-command-buffer tile hazards and the first-command chain + * only touch barrier[]): recompute, and keep the vertex half at + * least as strict as before. */ cs->sync.ta_split = false; - if (cs->type == HK_CS_CDM) - continue; - if (chain_first || first_cdm_is_flush) { + if (cs->type != HK_CS_CDM && (chain_first || first_cdm_is_flush)) { memcpy(cs->sync.barrier_ta, cs->sync.barrier, sizeof(cs->sync.barrier_ta)); memcpy(cs->sync.prior_ta, cs->sync.prior, sizeof(cs->sync.prior_ta)); - continue; - } - /* M5: tiling whose pre-rasterization programs keep state in - * thread-private storage corrupts that state when it overlaps an - * earlier render's fragment work (Counter-Strike 2: one-frame - * garbage geometry and blocks, only for such renders). Such a - * render's vertex half keeps the whole render-subqueue prefix; - * its compute prefix may still be narrower. */ - if (cs->m5_vertex_tps) { - cs->sync.barrier_ta[HK_SYNC_VDM] = cs->sync.barrier[HK_SYNC_VDM]; - cs->sync.prior_ta[HK_SYNC_VDM] = cs->sync.prior[HK_SYNC_VDM]; + } else if (cs->type != HK_CS_CDM) { + /* M5: tiling whose pre-rasterization programs keep state in + * thread-private storage corrupts that state when it overlaps an + * earlier render's fragment work (Counter-Strike 2: one-frame + * garbage geometry and blocks, only for such renders). Such a + * render's vertex half keeps the whole render-subqueue prefix; + * its compute prefix may still be narrower. */ + if (cs->m5_vertex_tps) { + cs->sync.barrier_ta[HK_SYNC_VDM] = cs->sync.barrier[HK_SYNC_VDM]; + cs->sync.prior_ta[HK_SYNC_VDM] = cs->sync.prior[HK_SYNC_VDM]; + } + for (unsigned sq = 0; sq < HK_SYNC_NUM_SQ; ++sq) { + cs->sync.ta_split |= + cs->sync.barrier_ta[sq] != cs->sync.barrier[sq] || + cs->sync.prior_ta[sq] != cs->sync.prior[sq]; + } } - for (unsigned sq = 0; sq < HK_SYNC_NUM_SQ; ++sq) { - cs->sync.ta_split |= - cs->sync.barrier_ta[sq] != cs->sync.barrier[sq] || - cs->sync.prior_ta[sq] != cs->sync.prior[sq]; + + if (out) { + struct hk_sync_resolved *r = out++; + memcpy(r->barrier, cs->sync.barrier, sizeof(r->barrier)); + memcpy(r->barrier_ta, cs->sync.barrier_ta, sizeof(r->barrier_ta)); + memcpy(r->prior, cs->sync.prior, sizeof(r->prior)); + memcpy(r->prior_ta, cs->sync.prior_ta, sizeof(r->prior_ta)); + r->ta_split = cs->sync.ta_split; } } } diff --git a/src/asahi/vulkan/hk_sync.h b/src/asahi/vulkan/hk_sync.h index 0a02c0263ee3..baaacb5583b4 100644 --- a/src/asahi/vulkan/hk_sync.h +++ b/src/asahi/vulkan/hk_sync.h @@ -230,7 +230,10 @@ struct hk_sync_stream { struct hk_cs *prev_mem; /* Set at submission: 1-based position among the commands of this - * subqueue in the ioctl being built. */ + * subqueue in the ioctl being built. Like the resolved fields below, it + * describes one occurrence of the stream: a simultaneous-use command + * buffer may occur more than once in a submission + * (hk_sync_resolve_submit()). */ uint32_t index; /* Resolved barrier fields for the ioctl being built. */ @@ -259,6 +262,15 @@ struct hk_sync_stream { uint32_t vtx_images_epoch; }; +/* The resolved fields of one occurrence of a stream in a submission. */ +struct hk_sync_resolved { + uint16_t barrier[HK_SYNC_NUM_SQ]; + uint16_t barrier_ta[HK_SYNC_NUM_SQ]; + uint64_t prior[HK_SYNC_NUM_SQ]; + uint64_t prior_ta[HK_SYNC_NUM_SQ]; + bool ta_split; +}; + /* Per-command-buffer state. */ struct hk_sync_state { uint32_t next_seq; @@ -517,8 +529,16 @@ void hk_sync_resolve_end(struct hk_cmd_buffer *cmd); * command must run after; chain_first says the commands must transitively * follow the first hardware command (the kernel attaches the ioctl's input * syncs only to it). + * + * A command buffer may occur more than once in cmdbufs, and its streams then + * resolve differently at each occurrence. Unless out is NULL, it receives + * one entry per stream occurrence in submission order; the caller loads each + * into its stream (hk_sync_load_resolved()) before emitting it. The streams + * keep the fields of their last occurrence. */ bool hk_sync_resolve_submit(struct hk_queue *queue, struct hk_cmd_buffer *const *cmdbufs, unsigned count, bool first_cdm_is_flush, - bool chain_first, uint64_t completed); + bool chain_first, uint64_t completed, + struct hk_sync_resolved *out); +void hk_sync_load_resolved(struct hk_cs *cs, const struct hk_sync_resolved *r); diff --git a/src/asahi/vulkan/meson.build b/src/asahi/vulkan/meson.build index 8bb67f865c63..01bcbbc64965 100644 --- a/src/asahi/vulkan/meson.build +++ b/src/asahi/vulkan/meson.build @@ -1011,6 +1011,15 @@ if with_tests gnu_symbol_visibility : 'hidden', ) + hk_vulkan_sparse_bind_first_submit = executable( + 'hk-vulkan-sparse-bind-first-submit', + files('tests/hk_vulkan_sparse_bind_first_submit.c'), + include_directories : [inc_include], + dependencies : [cc.find_library('vulkan')], + c_args : [c_msvc_compat_args], + gnu_symbol_visibility : 'hidden', + ) + hk_vulkan_explicit_plane_layout = executable( 'hk-vulkan-explicit-plane-layout', files('tests/hk_vulkan_explicit_plane_layout.c'), diff --git a/src/asahi/vulkan/tests/hk_vulkan_sparse_bind_first_submit.c b/src/asahi/vulkan/tests/hk_vulkan_sparse_bind_first_submit.c new file mode 100644 index 000000000000..3471efb44d67 --- /dev/null +++ b/src/asahi/vulkan/tests/hk_vulkan_sparse_bind_first_submit.c @@ -0,0 +1,162 @@ +/* Copyright 2026 Aurora Silicon + * SPDX-License-Identifier: MIT + * + * A queue's first submission is a vkQueueBindSparse of one tile of a + * sparse-residency image, with no command buffer, as vkd3d-proton issues for + * a tiled resource. On G17P the bind also flushes the image's sparse table, + * which is a one-command GPU submission of its own. It must succeed: the + * device must not be lost, and the fence must signal. Nothing executes on + * the GPU, so the test also runs under the no-op DRM shim. + */ +#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) + +int +main(void) +{ + 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; + } + + VkPhysicalDeviceFeatures features; + vkGetPhysicalDeviceFeatures(pdev, &features); + if (!features.sparseBinding || !features.sparseResidencyImage2D) { + fprintf(stderr, "sparseBinding or sparseResidencyImage2D missing\n"); + return 1; + } + + VkQueueFamilyProperties families[8]; + uint32_t family_count = 8; + vkGetPhysicalDeviceQueueFamilyProperties(pdev, &family_count, families); + uint32_t family = UINT32_MAX; + for (uint32_t i = 0; i < family_count && family == UINT32_MAX; i++) { + if (families[i].queueFlags & VK_QUEUE_SPARSE_BINDING_BIT) + family = i; + } + if (family == UINT32_MAX) { + fprintf(stderr, "no sparse binding queue\n"); + return 1; + } + + const float priority = 1.0f; + const VkDeviceQueueCreateInfo queue_info = { + .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, + .queueFamilyIndex = family, + .queueCount = 1, + .pQueuePriorities = &priority, + }; + const VkPhysicalDeviceFeatures enabled = { + .sparseBinding = VK_TRUE, + .sparseResidencyImage2D = VK_TRUE, + }; + const VkDeviceCreateInfo device_info = { + .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, + .queueCreateInfoCount = 1, + .pQueueCreateInfos = &queue_info, + .pEnabledFeatures = &enabled, + }; + VkDevice dev; + CHECK(vkCreateDevice(pdev, &device_info, NULL, &dev)); + VkQueue queue; + vkGetDeviceQueue(dev, family, 0, &queue); + + const VkImageCreateInfo image_info = { + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .flags = VK_IMAGE_CREATE_SPARSE_BINDING_BIT | + VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT, + .imageType = VK_IMAGE_TYPE_2D, + .format = VK_FORMAT_R8G8B8A8_UNORM, + .extent = {512, 512, 1}, + .mipLevels = 1, + .arrayLayers = 1, + .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, + .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, + }; + VkImage image; + CHECK(vkCreateImage(dev, &image_info, NULL, &image)); + + VkMemoryRequirements reqs; + vkGetImageMemoryRequirements(dev, image, &reqs); + VkSparseImageMemoryRequirements sparse_reqs; + uint32_t sparse_count = 1; + vkGetImageSparseMemoryRequirements(dev, image, &sparse_count, &sparse_reqs); + if (sparse_count == 0) { + fprintf(stderr, "FAIL: no sparse memory requirements\n"); + return 1; + } + + 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.alignment, + .memoryTypeIndex = type, + }; + VkDeviceMemory memory; + CHECK(vkAllocateMemory(dev, &alloc_info, NULL, &memory)); + + /* One tile, the first one of level 0 */ + const VkExtent3D tile = sparse_reqs.formatProperties.imageGranularity; + const VkSparseImageMemoryBind bind = { + .subresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0}, + .offset = {0, 0, 0}, + .extent = tile, + .memory = memory, + .memoryOffset = 0, + }; + const VkSparseImageMemoryBindInfo image_bind = { + .image = image, + .bindCount = 1, + .pBinds = &bind, + }; + const VkBindSparseInfo bind_info = { + .sType = VK_STRUCTURE_TYPE_BIND_SPARSE_INFO, + .imageBindCount = 1, + .pImageBinds = &image_bind, + }; + const VkFenceCreateInfo fence_info = { + .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, + }; + VkFence fence; + CHECK(vkCreateFence(dev, &fence_info, NULL, &fence)); + + CHECK(vkQueueBindSparse(queue, 1, &bind_info, fence)); + CHECK(vkWaitForFences(dev, 1, &fence, VK_TRUE, 5000000000ull)); + CHECK(vkDeviceWaitIdle(dev)); + + vkDestroyFence(dev, fence, NULL); + vkDestroyImage(dev, image, NULL); + vkFreeMemory(dev, memory, NULL); + vkDestroyDevice(dev, NULL); + vkDestroyInstance(instance, NULL); + printf("sparse bind as the first submission: ok\n"); + return 0; +} diff --git a/src/asahi/vulkan/tests/test-hk-sync-prior.c b/src/asahi/vulkan/tests/test-hk-sync-prior.c index f7745236609b..e7ac274b8381 100644 --- a/src/asahi/vulkan/tests/test-hk-sync-prior.c +++ b/src/asahi/vulkan/tests/test-hk-sync-prior.c @@ -96,7 +96,7 @@ static bool submit(struct hk_queue *q, struct hk_cmd_buffer *c) { struct hk_cmd_buffer *cs[] = {c}; - return hk_sync_resolve_submit(q, cs, 1, false, false, 0); + return hk_sync_resolve_submit(q, cs, 1, false, false, 0, NULL); } static struct hk_cmd_buffer * @@ -451,11 +451,11 @@ test_precise_tile_evictions(void) resolved && q->sync.evicted_preserve == point && q->sync.evicted > point); ++q->drm.timeline_value; T("retired preserving pass allows complete write", - hk_sync_resolve_submit(q, one, 1, false, false, point) && + hk_sync_resolve_submit(q, one, 1, false, false, point, NULL) && write->sync.barrier[HK_SYNC_VDM] == DRM_ASAHI_BARRIER_NONE); ++q->drm.timeline_value; T("partial pass still waits on live evicted complete writes", - hk_sync_resolve_submit(q, p, 1, false, false, point) && + hk_sync_resolve_submit(q, p, 1, false, false, point, NULL) && preserve->sync.barrier[HK_SYNC_VDM] == 0); q = new_queue(true); @@ -463,14 +463,14 @@ test_precise_tile_evictions(void) q->drm.timeline_value = 1; struct hk_cmd_buffer *pair[] = {partial, full}; T("partial then full in one submit retains dependency", - hk_sync_resolve_submit(q, pair, 2, false, false, 0) && + hk_sync_resolve_submit(q, pair, 2, false, false, 0, NULL) && write->sync.barrier[HK_SYNC_VDM] == preserve->sync.index); q = new_queue(true); q->sync.precise_tile_evictions = true; q->drm.timeline_value = 1; struct hk_cmd_buffer *reverse[] = {full, partial}; T("full then partial in one submit retains dependency", - hk_sync_resolve_submit(q, reverse, 2, false, false, 0) && + hk_sync_resolve_submit(q, reverse, 2, false, false, 0, NULL) && preserve->sync.barrier[HK_SYNC_VDM] == write->sync.index); q = new_queue(true); @@ -666,7 +666,7 @@ test_multiview_layer_preservation(void) sparse = render_pass(b, image, 2, true); struct hk_cmd_buffer *pair[] = {a, b}; T("multiview across command buffers submit", - hk_sync_resolve_submit(queue, pair, 2, false, false, 0)); + hk_sync_resolve_submit(queue, pair, 2, false, false, 0, NULL)); T("across command buffers keeps layer store ordered", sparse->sync.barrier[HK_SYNC_VDM] == first->sync.index); @@ -679,7 +679,7 @@ test_multiview_layer_preservation(void) queue->drm.timeline_value = 2; struct hk_cmd_buffer *single[] = {b}; T("multiview later queue submission", - hk_sync_resolve_submit(queue, single, 1, false, false, 0)); + hk_sync_resolve_submit(queue, single, 1, false, false, 0, NULL)); T("across queue submissions waits on prior layer store", sparse->sync.barrier[HK_SYNC_VDM] == 0); } @@ -791,7 +791,7 @@ test_loaded_color_preservation(void) queue = new_queue(true); struct hk_cmd_buffer *pair[] = {a, b}; T("loaded color across command buffers submit", - hk_sync_resolve_submit(queue, pair, 2, false, false, 0)); + hk_sync_resolve_submit(queue, pair, 2, false, false, 0, NULL)); T("loaded color across command buffers retains compute clear", load->sync.barrier[HK_SYNC_CDM] == clear->sync.index && load->sync.barrier[HK_SYNC_VDM] == DRM_ASAHI_BARRIER_NONE); @@ -802,7 +802,7 @@ test_loaded_color_preservation(void) pair[0] = a; pair[1] = b; queue = new_queue(true); T("unrelated color across command buffers submit", - hk_sync_resolve_submit(queue, pair, 2, false, false, 0)); + hk_sync_resolve_submit(queue, pair, 2, false, false, 0, NULL)); T("unrelated color across command buffers stays independent", load->sync.barrier[HK_SYNC_CDM] == DRM_ASAHI_BARRIER_NONE && load->sync.barrier[HK_SYNC_VDM] == DRM_ASAHI_BARRIER_NONE); @@ -825,7 +825,7 @@ test_loaded_color_preservation(void) queue->drm.timeline_value = 2; struct hk_cmd_buffer *single[] = {b}; T("loaded color consumer submission", - hk_sync_resolve_submit(queue, single, 1, false, false, 0)); + hk_sync_resolve_submit(queue, single, 1, false, false, 0, NULL)); T("loaded color waits on prior compute history", load->sync.barrier[HK_SYNC_CDM] == 0 && load->sync.barrier[HK_SYNC_VDM] == DRM_ASAHI_BARRIER_NONE); @@ -854,7 +854,7 @@ test_compute_clear_layer_preservation(void) sparse = render_pass(b, image, 2, true); struct hk_cmd_buffer *pair[] = {a, b}; T("compute clear across command buffers submit", - hk_sync_resolve_submit(queue, pair, 2, false, false, 0)); + hk_sync_resolve_submit(queue, pair, 2, false, false, 0, NULL)); T("across command buffers names compute not render engine", sparse->sync.barrier[HK_SYNC_CDM] == clear->sync.index && sparse->sync.barrier[HK_SYNC_VDM] == DRM_ASAHI_BARRIER_NONE); @@ -868,12 +868,12 @@ test_compute_clear_layer_preservation(void) queue->drm.timeline_value = 2; struct hk_cmd_buffer *single[] = {b}; T("compute clear later queue submission", - hk_sync_resolve_submit(queue, single, 1, false, false, 0)); + hk_sync_resolve_submit(queue, single, 1, false, false, 0, NULL)); T("prior compute clear hazard names only compute history", sparse->sync.barrier[HK_SYNC_CDM] == 0 && sparse->sync.barrier[HK_SYNC_VDM] == DRM_ASAHI_BARRIER_NONE); T("completed prior clear submission", hk_sync_resolve_submit( - queue, single, 1, false, false, 1)); + queue, single, 1, false, false, 1, NULL)); T("completed prior clear has no remaining hazard", sparse->sync.barrier[HK_SYNC_CDM] == DRM_ASAHI_BARRIER_NONE); @@ -1186,7 +1186,7 @@ test_image_barrier_vertex_class(void) queue = new_queue(true); struct hk_cmd_buffer *pair[] = {a, b}; T("carried image class submit", - hk_sync_resolve_submit(queue, pair, 2, false, false, 0)); + hk_sync_resolve_submit(queue, pair, 2, false, false, 0, NULL)); T("carried image class leaves a plain vertex half unordered", plain->sync.barrier_ta[HK_SYNC_VDM] == DRM_ASAHI_BARRIER_NONE && plain->sync.prior_ta[HK_SYNC_VDM] == 0); @@ -1207,7 +1207,7 @@ test_chain_first_skips_empty_buffers(void) struct hk_cs *render = add(b, false); struct hk_cmd_buffer *ecr[] = {empty, a, b}; T("chain [empty, compute, render]", - hk_sync_resolve_submit(q, ecr, 3, false, true, 0)); + hk_sync_resolve_submit(q, ecr, 3, false, true, 0, NULL)); T("chain first command has no barrier", compute->sync.barrier[HK_SYNC_VDM] == DRM_ASAHI_BARRIER_NONE && compute->sync.barrier[HK_SYNC_CDM] == DRM_ASAHI_BARRIER_NONE); @@ -1227,7 +1227,7 @@ test_chain_first_skips_empty_buffers(void) compute = add(b, true); struct hk_cmd_buffer *erc[] = {empty, a, b}; T("chain [empty, render, compute]", - hk_sync_resolve_submit(q, erc, 3, false, true, 0)); + hk_sync_resolve_submit(q, erc, 3, false, true, 0, NULL)); T("chain no barrier on CDM before any compute", compute->sync.barrier[HK_SYNC_CDM] == DRM_ASAHI_BARRIER_NONE && compute->sync.barrier[HK_SYNC_VDM] == 1 && @@ -1242,7 +1242,7 @@ test_chain_first_skips_empty_buffers(void) render = add(b, false); struct hk_cmd_buffer *cr[] = {a, b}; T("chain [compute, render]", - hk_sync_resolve_submit(q, cr, 2, false, true, 0)); + hk_sync_resolve_submit(q, cr, 2, false, true, 0, NULL)); T("chain [compute, render] render follows compute", compute->sync.barrier[HK_SYNC_CDM] == DRM_ASAHI_BARRIER_NONE && render->sync.barrier[HK_SYNC_VDM] == DRM_ASAHI_BARRIER_NONE && @@ -1252,12 +1252,42 @@ test_chain_first_skips_empty_buffers(void) q = new_queue(exact); struct hk_cmd_buffer *ee[] = {new_cmd(), new_cmd()}; T("chain without commands", - hk_sync_resolve_submit(q, ee, 2, false, true, 0) && + hk_sync_resolve_submit(q, ee, 2, false, true, 0, NULL) && q->sync.submitted[HK_SYNC_VDM] == 0 && q->sync.submitted[HK_SYNC_CDM] == 0); } } +/* One simultaneous-use recording twice in a submission, {a, b, a}: each + * occurrence resolves at its own position, so every stream waits on the one + * just before it and never on itself or a later command. */ +static void +test_repeated_recording(void) +{ + for (unsigned mode = 0; mode < 4; ++mode) { + struct hk_queue *q = new_queue(mode & 1); + struct hk_cmd_buffer *a = new_cmd(), *b = new_cmd(); + for (unsigned i = 0; i < 4; ++i) { + hk_sync_barrier(i < 2 ? a : b, HK_SYNC_ALL, HK_SYNC_ALL); + add(i < 2 ? a : b, true); + } + struct hk_cmd_buffer *cmds[] = {a, b, a}; + struct hk_sync_resolved out[6]; + T("repeated recording submission", + hk_sync_resolve_submit(q, cmds, 3, false, mode & 2, 0, out)); + bool ordered = true; + for (unsigned i = 0; i < 6; ++i) { + ordered &= out[i].barrier[HK_SYNC_CDM] == + (i ? i : DRM_ASAHI_BARRIER_NONE) && + out[i].barrier[HK_SYNC_VDM] == DRM_ASAHI_BARRIER_NONE && + !out[i].prior[HK_SYNC_CDM] && !out[i].prior[HK_SYNC_VDM]; + } + T("repeated recording waits on the previous occurrence", ordered); + T("repeated recording ordinals", + !(mode & 1) || q->sync.submitted[HK_SYNC_CDM] == 6); + } +} + /* Native ray tracing sends the extended compute command, which only a * kernel advertising both the exact-prior layout and ray query state parses. * Anything less keeps ray tracing off. */ @@ -1318,7 +1348,7 @@ test_classic_submission_order(void) for (unsigned i = 0; i < 4; ++i) streams[i] = add(cmds[i * groups / 4], mix & (1u << i)); T("classic mixed submission", hk_sync_resolve_submit( - q, cmds, groups, false, false, 0)); + q, cmds, groups, false, false, 0, NULL)); unsigned prefix[2] = {0, 0}; const uint64_t base[2] = {7, 11}; for (unsigned i = 0; i < 4; ++i) { @@ -1346,7 +1376,7 @@ test_classic_submission_order(void) add(draw, false); struct hk_cmd_buffer *cmds[] = {empty, event, event, draw, event}; T("classic repeated recording", hk_sync_resolve_submit( - q, cmds, 5, false, false, 0)); + q, cmds, 5, false, false, 0, NULL)); unsigned prefix[2] = {0, 0}; const uint64_t base[2] = {7, 11}; for (unsigned i = 0; i < 5; ++i) { @@ -1450,10 +1480,10 @@ test_direct_single_footprints(void) a->drm.timeline_value = b->drm.timeline_value = r + 1; const uint64_t completed = r % 4 ? 0 : r; const bool flush = r % 7 == 0, chain = r % 3 == 0; - same &= hk_sync_resolve_submit(a, cmds, count, flush, chain, completed); + same &= hk_sync_resolve_submit(a, cmds, count, flush, chain, completed, NULL); for (unsigned j = 0; j < n; ++j) saved[j] = streams[j]->sync; - same &= hk_sync_resolve_submit(b, cmds, count, flush, chain, completed); + same &= hk_sync_resolve_submit(b, cmds, count, flush, chain, completed, NULL); for (unsigned j = 0; j < n; ++j) { same &= saved[j].index == streams[j]->sync.index && saved[j].ta_split == streams[j]->sync.ta_split; @@ -1513,6 +1543,7 @@ main(void) test_g17p_vertex_after_memory_writers(); test_g17p_tiling_order(); test_chain_first_skips_empty_buffers(); + test_repeated_recording(); test_rt_kernel_gate(); test_timestamp_execution_scopes(); test_timestamp_reused_compute(); @@ -1584,7 +1615,7 @@ main(void) hk_sync_barrier(a, HK_SYNC_COMPUTE, HK_SYNC_GFX); d = add(b, false); struct hk_cmd_buffer *pair[] = {a, b}; - T("same-submit", hk_sync_resolve_submit(q, pair, 2, false, false, 0)); + T("same-submit", hk_sync_resolve_submit(q, pair, 2, false, false, 0, NULL)); T("same-submit keeps relative1", d->sync.barrier[1] == 1 && d->sync.prior[1] == 0); q = new_queue(true); @@ -1597,7 +1628,7 @@ main(void) T("empty source no future dependency", d->sync.prior[1] == 0 && d->sync.barrier[1] == DRM_ASAHI_BARRIER_NONE); q = new_queue(true); - T("synthetic SSTT only", hk_sync_resolve_submit(q, NULL, 0, true, false, 0)); + T("synthetic SSTT only", hk_sync_resolve_submit(q, NULL, 0, true, false, 0, NULL)); T("SSTT increments actual CDM ordinal", q->sync.submitted[1] == 1 && q->sync.last_absolute[1][1] == 1); a = new_cmd(); @@ -1669,7 +1700,7 @@ main(void) hk_sync_barrier(a, HK_SYNC_COMPUTE, HK_SYNC_GFX); struct hk_cmd_buffer *single[] = {a}; T("SSTT plus application", - hk_sync_resolve_submit(q, single, 1, true, false, 0)); + hk_sync_resolve_submit(q, single, 1, true, false, 0, NULL)); T("SSTT plus app counters", q->sync.submitted[1] == 2 && d->sync.index == 2 && d->sync.barrier[1] == 1);