diff --git a/src/asahi/lib/agx_bo.c b/src/asahi/lib/agx_bo.c index c92b280929f..094468c9fd5 100644 --- a/src/asahi/lib/agx_bo.c +++ b/src/asahi/lib/agx_bo.c @@ -738,6 +738,37 @@ agx_bo_ensure_ro_shadow(struct agx_device *dev, struct agx_bo *bo) return ret; } +/* Hands out a freshly allocated or reused BO. */ +static struct agx_bo * +agx_bo_publish(struct agx_device *dev, struct agx_bo *bo, const char *label) +{ + bo->label = label; + p_atomic_set(&bo->refcnt, 1); + + if (dev->debug & + (AGX_DBG_TRACE | AGX_DBG_ASYNCTRACE)) { + agx_bo_map(bo); + agxdecode_track_alloc(dev->agxdecode, bo); + } + + agx_bo_dump_all_periodic(dev); + return bo; +} + +/* A BO whose only GPU address is `va`, which it takes over, also when it + * fails. Never cached: the address is free again once the BO is. */ +struct agx_bo * +agx_bo_create_at(struct agx_device *dev, size_t size, enum agx_bo_flags flags, + struct agx_va *va, const char *label) +{ + assert(size > 0 && va != NULL); + flags |= AGX_BO_NO_CACHE; + + struct agx_bo *bo = dev->ops.bo_alloc(dev, ALIGN_POT(size, AIL_PAGESIZE), + AIL_PAGESIZE, flags, va); + return bo ? agx_bo_publish(dev, bo, label) : NULL; +} + struct agx_bo * agx_bo_create_checked(struct agx_device *dev, size_t size, unsigned align, enum agx_bo_flags flags, const char *label, @@ -786,14 +817,14 @@ agx_bo_create_checked(struct agx_device *dev, size_t size, unsigned align, * flush the cache to make space for the new allocation. */ if (!bo) - bo = dev->ops.bo_alloc(dev, size, align, flags); + bo = dev->ops.bo_alloc(dev, size, align, flags, NULL); if (!bo) { bo = agx_bo_cache_fetch(dev, size, align, flags, false); reused = bo != NULL; } if (!bo) { agx_bo_cache_evict_all(dev); - bo = dev->ops.bo_alloc(dev, size, align, flags); + bo = dev->ops.bo_alloc(dev, size, align, flags, NULL); } if (!bo) { @@ -839,17 +870,7 @@ agx_bo_create_checked(struct agx_device *dev, size_t size, unsigned align, memset(map, 0, bo->size); } - bo->label = label; - p_atomic_set(&bo->refcnt, 1); - - if (dev->debug & - (AGX_DBG_TRACE | AGX_DBG_ASYNCTRACE)) { - agx_bo_map(bo); - agxdecode_track_alloc(dev->agxdecode, bo); - } - - agx_bo_dump_all_periodic(dev); - return bo; + return agx_bo_publish(dev, bo, label); } struct agx_bo * diff --git a/src/asahi/lib/agx_bo.h b/src/asahi/lib/agx_bo.h index e8f4e8104b8..16e1a4d61fc 100644 --- a/src/asahi/lib/agx_bo.h +++ b/src/asahi/lib/agx_bo.h @@ -184,6 +184,9 @@ struct agx_bo *agx_bo_create_checked(struct agx_device *dev, size_t size, struct agx_bo *agx_bo_create(struct agx_device *dev, size_t size, unsigned align, enum agx_bo_flags flags, const char *label); +struct agx_bo *agx_bo_create_at(struct agx_device *dev, size_t size, + enum agx_bo_flags flags, struct agx_va *va, + const char *label); int agx_bo_ensure_ro_shadow(struct agx_device *dev, struct agx_bo *bo); diff --git a/src/asahi/lib/agx_device.c b/src/asahi/lib/agx_device.c index 58161f6205f..fd6bc5f2721 100644 --- a/src/asahi/lib/agx_device.c +++ b/src/asahi/lib/agx_device.c @@ -410,7 +410,7 @@ agx_bind_timestamps(struct agx_device *dev, struct agx_bo *bo, uint32_t *handle) static struct agx_bo * agx_bo_alloc(struct agx_device *dev, size_t size, size_t align, - enum agx_bo_flags flags) + enum agx_bo_flags flags, struct agx_va *va) { struct agx_bo *bo; unsigned handle = 0; @@ -431,6 +431,7 @@ agx_bo_alloc(struct agx_device *dev, size_t size, size_t align, int ret = drmIoctl(dev->fd, DRM_IOCTL_ASAHI_GEM_CREATE, &gem_create); if (ret) { fprintf(stderr, "DRM_IOCTL_ASAHI_GEM_CREATE failed: %m\n"); + agx_va_free(dev, va, false); return NULL; } @@ -452,7 +453,7 @@ agx_bo_alloc(struct agx_device *dev, size_t size, size_t align, bo->prime_fd = -1; enum agx_va_flags va_flags = flags & AGX_BO_LOW_VA ? AGX_VA_USC : 0; - bo->va = agx_va_alloc(dev, size, bo->align, va_flags, 0); + bo->va = va ?: agx_va_alloc(dev, size, bo->align, va_flags, 0); if (!bo->va) { fprintf(stderr, "Failed to allocate BO VMA\n"); agx_bo_free(dev, bo); diff --git a/src/asahi/lib/agx_device.h b/src/asahi/lib/agx_device.h index e0ee64c1df4..70d530d3c83 100644 --- a/src/asahi/lib/agx_device.h +++ b/src/asahi/lib/agx_device.h @@ -101,8 +101,10 @@ struct agx_submit_virt { }; typedef struct { + /* A BO at `va` (agx_bo_create_at()), which it takes over and frees on + * failure too, or at a VA of its own when `va` is NULL. */ struct agx_bo *(*bo_alloc)(struct agx_device *dev, size_t size, size_t align, - enum agx_bo_flags flags); + enum agx_bo_flags flags, struct agx_va *va); int (*bo_bind)(struct agx_device *dev, struct drm_asahi_gem_bind_op *ops, uint32_t count); void (*bo_mmap)(struct agx_device *dev, struct agx_bo *bo, void *fixed_addr, diff --git a/src/asahi/lib/agx_device_virtio.c b/src/asahi/lib/agx_device_virtio.c index 6bf89810937..59b186842f7 100644 --- a/src/asahi/lib/agx_device_virtio.c +++ b/src/asahi/lib/agx_device_virtio.c @@ -52,7 +52,7 @@ agx_virtio_simple_ioctl(struct agx_device *dev, unsigned cmd, void *_req) static struct agx_bo * agx_virtio_bo_alloc(struct agx_device *dev, size_t size, size_t align, - enum agx_bo_flags flags) + enum agx_bo_flags flags, struct agx_va *va) { struct agx_bo *bo; unsigned handle = 0; @@ -79,7 +79,8 @@ agx_virtio_bo_alloc(struct agx_device *dev, size_t size, size_t align, uint32_t blob_id = p_atomic_inc_return(&dev->next_blob_id); enum agx_va_flags va_flags = flags & AGX_BO_LOW_VA ? AGX_VA_USC : 0; - struct agx_va *va = agx_va_alloc(dev, size, align, va_flags, 0); + if (!va) + va = agx_va_alloc(dev, size, align, va_flags, 0); if (!va) { fprintf(stderr, "Failed to allocate BO VMA\n"); return NULL; @@ -92,6 +93,7 @@ agx_virtio_bo_alloc(struct agx_device *dev, size_t size, size_t align, handle = vdrm_bo_create(dev->vdrm, size, blob_flags, blob_id, 0, &req.hdr); if (!handle) { fprintf(stderr, "vdrm_bo_created failed\n"); + agx_va_free(dev, va, false); return NULL; } diff --git a/src/asahi/lib/tests/test-agx-bo-cache.c b/src/asahi/lib/tests/test-agx-bo-cache.c index d2ad77b74c1..f7f53420c89 100644 --- a/src/asahi/lib/tests/test-agx-bo-cache.c +++ b/src/asahi/lib/tests/test-agx-bo-cache.c @@ -48,8 +48,9 @@ static struct mock_state mock; static struct agx_bo * mock_bo_alloc(struct agx_device *dev, size_t size, size_t align, - enum agx_bo_flags flags) + enum agx_bo_flags flags, struct agx_va *va) { + assert(va == NULL); mock.alloc_calls++; if (mock.alloc_fail) return NULL; diff --git a/src/asahi/meson.build b/src/asahi/meson.build index e6dac84e266..6e3b8a3d47b 100644 --- a/src/asahi/meson.build +++ b/src/asahi/meson.build @@ -603,6 +603,43 @@ if with_tools.contains('drm-shim') and with_asahi_vk and with_tests timeout : 120, ) + # Buffer device address capture/replay through capture/replay memory alone. + foreach chip : ['g13g', 'g17p'] + test( + 'brl-' + chip + '-capture-replay', + prog_python, + args : [ + files('../gallium/drivers/asahi/tests/run-shim.py'), + libasahi_noop_drm_shim, + hk_vulkan_capture_replay, + ], + env : [ + 'ASAHI_SHIM_CHIP=' + chip, + 'VK_DRIVER_FILES=' + _dev_icd.full_path(), + ], + depends : [_dev_icd, libvulkan_asahi], + suite : ['asahi'], + ) + endforeach + + # A wait for a host-set event, also one without barriers, gives its + # command buffer memory scope at its end, before memory first aliases. + test( + 'brl-g17p-event-alias', + prog_python, + args : [ + files('../gallium/drivers/asahi/tests/run-shim.py'), + libasahi_noop_drm_shim, + hk_vulkan_event_alias, + ], + 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_buffer.c b/src/asahi/vulkan/hk_buffer.c index 5fa1844c3c9..424db4f1b4f 100644 --- a/src/asahi/vulkan/hk_buffer.c +++ b/src/asahi/vulkan/hk_buffer.c @@ -348,10 +348,26 @@ hk_CreateBuffer(VkDevice device, const VkBufferCreateInfo *pCreateInfo, if (buffer->vk.create_flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) p_atomic_set(&dev->sparse_used, 1); + if (buffer->vk.create_flags & + VK_BUFFER_CREATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT) { + uint64_t sparse_map_addr; + hk_get_buffer_replay_addrs(pCreateInfo, &buffer->opaque_capture_address, + &sparse_map_addr); + } + + /* A sparse buffer has an address of its own, and so has a capture/replay + * buffer of VK_EXT_buffer_device_address, whose memory need not replay + * and whose address may be asked before the bind + * (VUID-vkGetBufferDeviceAddress-bufferDeviceAddress-03324). + * Any other buffer takes its memory's address plus the offset, which + * replays with capture/replay memory + * (VUID-VkBindBufferMemoryInfo-bufferDeviceAddressCaptureReplay-09200, + * VUID-VkBindBufferMemoryInfo-buffer-09201). */ if (buffer->vk.size > 0 && - (buffer->vk.create_flags & - (VK_BUFFER_CREATE_SPARSE_BINDING_BIT | - VK_BUFFER_CREATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT))) { + ((buffer->vk.create_flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) || + ((buffer->vk.create_flags & + VK_BUFFER_CREATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT) && + dev->vk.enabled_features.bufferDeviceAddressCaptureReplayEXT))) { const uint32_t alignment = hk_get_buffer_alignment( hk_device_physical(dev), buffer->vk.usage, buffer->vk.create_flags); @@ -736,8 +752,8 @@ hk_BindBufferMemory2(VkDevice device, uint32_t bindInfoCount, assert(buffer->vk.device_address == 0); buffer->vk.device_address = hk_device_memory_addr(mem) + pBindInfos[i].memoryOffset; - if (!mem->replay_va && - !(mem->bo->flags & (AGX_BO_SHARED | AGX_BO_SHAREABLE | AGX_BO_HOST_PTR))) { + if (!(mem->bo->flags & + (AGX_BO_SHARED | AGX_BO_SHAREABLE | AGX_BO_HOST_PTR))) { buffer->storage_backing_handle = mem->bo->handle; buffer->storage_backing_offset = pBindInfos[i].memoryOffset; } @@ -792,6 +808,15 @@ hk_GetBufferOpaqueCaptureAddress(UNUSED VkDevice device, { VK_FROM_HANDLE(hk_buffer, buffer, pInfo->buffer); + /* The address asked for is the answer (vkGetBufferOpaqueCaptureAddress), + * also before the bind, which this query does not need. A KHR + * capture/replay buffer has no address of its own until then: 0 is no + * specific address. */ + if (buffer->opaque_capture_address) + return buffer->opaque_capture_address; + if (!buffer->vk.device_address) + return 0; + return hk_buffer_address_rw(buffer, 0); } diff --git a/src/asahi/vulkan/hk_buffer.h b/src/asahi/vulkan/hk_buffer.h index 9d3741c04c3..da03e0a1311 100644 --- a/src/asahi/vulkan/hk_buffer.h +++ b/src/asahi/vulkan/hk_buffer.h @@ -25,6 +25,9 @@ struct hk_buffer { /** Reserved VA for sparse buffers, NULL otherwise. */ struct agx_va *va; + /** Capture/replay address the application asked for, 0 for none. */ + uint64_t opaque_capture_address; + /** Bytes of that VA counted in hk_device::sparse_scratch_B. */ uint64_t sparse_scratch_B; diff --git a/src/asahi/vulkan/hk_cmd_buffer.c b/src/asahi/vulkan/hk_cmd_buffer.c index ca0f9b9017c..91f22ecdb65 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.c +++ b/src/asahi/vulkan/hk_cmd_buffer.c @@ -229,25 +229,8 @@ hk_cmd_buffer_move_sparse_sites(struct hk_cmd_buffer *cmd, const void *from, } } -void -hk_cmd_buffer_move_alias_sites(struct hk_cmd_buffer *cmd, const void *from, - const void *from_end, uint8_t *to, size_t skip) -{ - util_dynarray_foreach(&cmd->alias_sites, struct hk_alias_site, site) { - const uint8_t *word = (const uint8_t *)site->word; - if (word < (const uint8_t *)from || word >= (const uint8_t *)from_end) - continue; - - /* A site in the folded leading record moves to the trailing record - * that absorbed it. */ - const size_t offset = word - (const uint8_t *)from; - site->word = (uint32_t *)(offset < skip ? to - AGX3_CDM_BARRIER_SIZE - : to + offset - skip); - } -} - /* At vkEndCommandBuffer, once nothing moves the streams any more: a command - * buffer that uses events gets memory scope throughout + * buffer that waits for a host-set event gets memory scope throughout * (hk_cmd_alias_scope()), one recorded while a resource may alias by now is * written at once, and any other waits in host_alias.cmd_buffers for * hk_device_patch_alias_sites(), besides the check at submission. */ @@ -257,7 +240,7 @@ hk_cmd_buffer_end_alias_sites(struct hk_cmd_buffer *cmd) if (!cmd->alias_sites.size) return; - if (cmd->state.uses_events) { + if (cmd->state.alias_host_wait) { hk_cmd_buffer_patch_alias_sites(cmd); return; } @@ -1318,8 +1301,8 @@ hk_EndCommandBuffer(VkCommandBuffer commandBuffer) * with ENOTSUPP ("G18 compute admission rejected ... stream_bytes=52"). */ if (cs->type == HK_CS_CDM && !cs->g18_dispatch_sealed) { - /* With host_alias.dynamic, every stream holding work ends with the - * memory-scoped strong flush, also where its last record is an + /* M5 with host_alias.dynamic: every stream holding work ends with + * the memory-scoped strong flush, also where its last record is an * application barrier recorded without memory scope. Each command * then writes back what it wrote by its end, as the memory scope of * each barrier did under the per-device rule, so a later command @@ -1329,8 +1312,11 @@ hk_EndCommandBuffer(VkCommandBuffer commandBuffer) * another device's, process's or API's mapping of the same pages, * which Vulkan orders after this work only at a submission * boundary. Submit packing keeps the scope only at a packet's end - * while no resource may alias. */ - if (dev->host_alias.dynamic && + * while no resource may alias. The end of a G17P command already + * publishes its writes to every address (bt xsub with the second + * address created while the writer runs reads exact), so G17P + * streams end with their own records. */ + if (dev->host_alias.dynamic && agx_chip_g17_m5(dev->dev.chip) && hk_cdm_visibility_flush_needed(dev, cs, true)) { if (cs->current != cs->weak_barrier_end && cs->current != cs->strong_flush_end) diff --git a/src/asahi/vulkan/hk_cmd_buffer.h b/src/asahi/vulkan/hk_cmd_buffer.h index 2b2298ef7b9..8aeb125525a 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.h +++ b/src/asahi/vulkan/hk_cmd_buffer.h @@ -1819,10 +1819,13 @@ struct hk_cs { /* The vertex stages read the hoisted preamble after this stream. */ bool pre_gfx_writes_preamble; - /* M5: patch sites recorded in this stream (hk_cmd_buffer::alias_sites), + /* Patch sites recorded in this stream (hk_cmd_buffer::alias_sites), * which a merge moves with its bytes. */ uint32_t alias_sites; + /* current just after a barrier record that is a patch site: a later + * launch must not drop it (hk_tess_batch_stage()). */ + void *alias_pin; /* AGX2: sparse patch sites recorded in this stream * (hk_cmd_buffer::sparse_sites), which a merge moves with its bytes. */ uint32_t sparse_sites; @@ -2329,12 +2332,10 @@ hk_root_only_flush_supported(enum agx_chip chip, const struct hk_cs *cs) void *hk_cmd_buffer_emit_cs_jump(struct hk_cmd_buffer *cmd, void *current, uint64_t target, bool vdm); -/* Moves the patch sites of cmd in [from, from_end), copied to `to` without - * their first `skip` bytes, which were folded into the record just before - * `to` (hk_cs_merge_cdm_until()). */ -void hk_cmd_buffer_move_alias_sites(struct hk_cmd_buffer *cmd, - const void *from, const void *from_end, - uint8_t *to, size_t skip); +static inline void hk_cmd_buffer_move_alias_sites(struct hk_cmd_buffer *cmd, + const void *from, + const void *from_end, + uint8_t *to, size_t skip); /* As hk_cmd_buffer_move_alias_sites(), for the AGX2 sparse patch sites. */ void hk_cmd_buffer_move_sparse_sites(struct hk_cmd_buffer *cmd, @@ -2412,6 +2413,11 @@ hk_cs_merge_cdm_until(struct hk_cs *a, const struct hk_cs *b, void *b_current) if (b->alias_sites && a->cmd == b->cmd) hk_cmd_buffer_move_alias_sites(a->cmd, b->start, b_current, dst, b_skip); + a->alias_pin = + b->alias_pin && a->cmd == b->cmd + ? dst + ((uint8_t *)b->alias_pin - (uint8_t *)b->start) - b_skip + : a->current == dst ? a->alias_pin + : NULL; if (b->sparse_sites && a->cmd == b->cmd) hk_cmd_buffer_move_sparse_sites(a->cmd, b->start, b_current, dst, b_skip); @@ -2448,6 +2454,7 @@ hk_cs_merge_cdm_until(struct hk_cs *a, const struct hk_cs *b, void *b_current) a->strong_flush_end = b->strong_flush_end; a->weak_barrier_end = b->weak_barrier_end; a->memory_flush_end = b->memory_flush_end; + a->alias_pin = b->alias_pin; a->app_dispatch_end = NULL; } @@ -2750,6 +2757,10 @@ struct hk_cmd_buffer { * pipeline barrier that carries its barriers. */ bool brl_legacy_event_wait; + /* A wait for an event the host may set: its barriers name HOST or no + * device stage as their source (hk_CmdWaitEvents2()). */ + bool alias_host_wait; + /* Scoped around direct/indirect AS builds, including their meta blits. */ bool as_build_recording; @@ -2920,7 +2931,7 @@ struct hk_cmd_buffer { * reference. */ struct hk_ioto_arena ioto; - /* M5 with hk_device::host_alias.dynamic: the application dependencies + /* With hk_device::host_alias.dynamic: the application dependencies * recorded without memory scope (struct hk_alias_site), written once an * import may alias (hk_cdm_alias_scope()). alias_patched (atomic) once * they are; alias_registered (atomic) while alias_link is in @@ -2954,6 +2965,25 @@ struct hk_alias_site { uint32_t value; }; +/* Moves the patch sites of cmd in [from, from_end), copied to `to` without + * their first `skip` bytes, which were folded into the record just before + * `to` (hk_cs_merge_cdm_until()). */ +static inline void +hk_cmd_buffer_move_alias_sites(struct hk_cmd_buffer *cmd, const void *from, + const void *from_end, uint8_t *to, size_t skip) +{ + util_dynarray_foreach(&cmd->alias_sites, struct hk_alias_site, site) { + const uint8_t *word = (const uint8_t *)site->word; + if (word < (const uint8_t *)from || word >= (const uint8_t *)from_end) + continue; + + /* A site in the folded leading record moves to the trailing record + * that absorbed it. */ + const size_t offset = word - (const uint8_t *)from; + site->word = (uint32_t *)(offset < skip ? to - AGX3_CDM_BARRIER_SIZE + : to + offset - skip); + } +} /* A patch site: the stream word of an AGX2 barrier record emitted with the * reduced flag word while the device held no sparse resource. `value` is * the full-word record, written at submit when a sparse resource exists by @@ -2965,7 +2995,12 @@ struct hk_sparse_site { /* Whether an application dependency recorded now needs memory scope: as * hk_cdm_alias_scope(), and also, with host_alias.dynamic, in a command - * buffer that uses events. */ + * buffer that waits for an event the host may set. Such a wait is the one way + * an execution that already started can reach memory aliased after it + * started (VUID-vkCmdWaitEvents2-pEvents-03839), and by then the CDM may + * have read the records past the wait (about 256 bytes of stream on G17P), + * so writing its sites comes too late. Setting an event, as DXVK does for + * its event queries, lets nothing in. */ bool hk_cmd_alias_scope(struct hk_cmd_buffer *cmd); /* After an application dependency was recorded into cs without memory scope diff --git a/src/asahi/vulkan/hk_cmd_dispatch.c b/src/asahi/vulkan/hk_cmd_dispatch.c index 6abf7cb6b9f..5b294454361 100644 --- a/src/asahi/vulkan/hk_cmd_dispatch.c +++ b/src/asahi/vulkan/hk_cmd_dispatch.c @@ -792,7 +792,7 @@ hk_cmd_alias_scope(struct hk_cmd_buffer *cmd) { const struct hk_device *dev = hk_cmd_buffer_device(cmd); return hk_cdm_alias_scope(dev) || - (dev->host_alias.dynamic && cmd->state.uses_events); + (dev->host_alias.dynamic && cmd->state.alias_host_wait); } void @@ -826,9 +826,10 @@ hk_cdm_note_alias_site(struct hk_cmd_buffer *cmd, struct hk_cs *cs) .value = dev->host_alias.flush_word, }; cs->alias_sites++; - /* A following application dispatch must not drop the record - * (hk_dispatch_m5_native_impl()). */ + /* A following launch must not drop the record (application + * dispatch barrier elision, hk_tess_batch_stage()). */ cs->app_dispatch_end = NULL; + cs->alias_pin = cs->current; return; } } diff --git a/src/asahi/vulkan/hk_cmd_draw.c b/src/asahi/vulkan/hk_cmd_draw.c index 0e8059fc277..1ba00fdd0e2 100644 --- a/src/asahi/vulkan/hk_cmd_draw.c +++ b/src/asahi/vulkan/hk_cmd_draw.c @@ -3948,15 +3948,17 @@ hk_xfer_order(struct hk_cmd_buffer *cmd, struct hk_cs *cs) } /* Each launch ends in a weak ordering record; strengthen the last one - * into a cache flush, as a barrier does (hk_CmdPipelineBarrier2()). No - * patch site is recorded here (hk_cdm_alias_scope_unpatched()). */ - const bool alias_scope = hk_cdm_alias_scope_unpatched(dev); + * into a cache flush, as a barrier does (hk_CmdPipelineBarrier2()), + * including its patch site. */ + const bool alias_scope = hk_cmd_alias_scope(cmd); for (unsigned i = 0; i < n; ++i) { struct hk_cs *w = flush[i]; if (hk_cdm_visibility_flush_needed(dev, w, alias_scope)) { hk_ensure_cs_has_space(cmd, w, AGX3_CDM_BARRIER_SIZE); hk_cdm_visibility_flush(dev, w, alias_scope); } + if (!alias_scope) + hk_cdm_note_alias_site(cmd, w); } perf_debug(cmd, "Ordering an unsynchronized transfer before graphics"); @@ -18765,15 +18767,16 @@ hk_dispatch_task_kernel(struct hk_cmd_buffer *cmd, struct hk_shader *task, /* The launch ends in a weak ordering record, which doesn't make the * entries visible to the mesh draws that read them; strengthen it into a - * cache flush, as a barrier does, without a patch site - * (hk_cdm_alias_scope_unpatched()). */ + * cache flush, as a barrier does, including its patch site. */ struct hk_device *dev = hk_cmd_buffer_device(cmd); - const bool alias_scope = hk_cdm_alias_scope_unpatched(dev); + const bool alias_scope = hk_cmd_alias_scope(cmd); if (!ccs->g18_dispatch_sealed && hk_cdm_visibility_flush_needed(dev, ccs, alias_scope)) { hk_ensure_cs_has_space(cmd, ccs, AGX3_CDM_BARRIER_SIZE); hk_cdm_visibility_flush(dev, ccs, alias_scope); } + if (!alias_scope) + hk_cdm_note_alias_site(cmd, ccs); } /* G17P software task stage. The task shader runs as a compute kernel in the diff --git a/src/asahi/vulkan/hk_device.c b/src/asahi/vulkan/hk_device.c index 09dff0e5489..fd172abf4d4 100644 --- a/src/asahi/vulkan/hk_device.c +++ b/src/asahi/vulkan/hk_device.c @@ -928,11 +928,13 @@ hk_init_g17p_grid_helper(struct hk_device *dev, /* Whether memory can be mapped at a second GPU virtual address at any time * in the life of `dev`, from what it enabled (hk_cdm_alias_scope()): * - * - sparse buffers and images, and capture/replay buffers, have an address - * of their own, and the memory bound to them keeps its own as well; - * - an fd import of memory from elsewhere gets an address of its own, and - * another import may map the same pages (two dma-bufs over one memfd, or - * a dma-buf and a host import of its pages); + * - sparse buffers and images, and capture/replay buffers of + * VK_EXT_buffer_device_address, have an address of their own, and the + * memory bound to them keeps its own as well; + * - an fd import gets an address of its own. Importing one GEM object again, + * our own export included, gives the same BO and address (agx_bo_import()), + * but two dma-bufs can share pages under different GEM objects (two + * udmabufs over one memfd), and a host import may map a dma-buf's pages; * - a host import gets an address of its own, and the same pages may be * imported again, or be mapped by the application at a second virtual * address that another import, or memory of another kind, reaches. With @@ -940,19 +942,20 @@ hk_init_g17p_grid_helper(struct hk_device *dev, * GPU, so the GPU writes no imported page and a read through the import * cannot miss a write through another address. * - * Nothing else maps memory the GPU writes at two addresses: the read-only - * shadow of every allocation is read only through sparse and capture/replay - * buffers, and the driver's other second mappings (descriptor tables, - * acceleration structure tables, shader code) hold memory written by the - * CPU alone. The structures of these features may only be used when they - * are enabled, so a device that enabled none of them never has such memory. + * Nothing else maps memory the GPU writes at two addresses: capture/replay + * memory has its address in the capture/replay region alone, and other + * buffers and images bound to it take that address; the read-only shadow of + * every allocation is read only through sparse buffers; and the driver's + * other second mappings (descriptor tables, acceleration structure tables, + * shader code) hold memory written by the CPU alone. The structures of these + * features may only be used when they are enabled, so a device that enabled + * none of them never has such memory. * - * On M5 every resource through which these features map memory at a second - * address is seen as it is created, so the scope follows the live ones - * instead (host_alias.dynamic, hk_cdm_alias_scope()): buffers and image - * planes with an address of their own and memory mapped again for - * descriptor buffer capture/replay (va_alias_resources), fd and dma-buf - * imports, and host imports that may alias (hk_host_import_aliases()). An + * With VA-tagged caches every resource through which these features map + * memory at a second address is seen as it is created, so the scope follows + * the live ones instead (host_alias.dynamic, hk_cdm_alias_scope()): buffers + * and image planes with an address of their own, fd and dma-buf imports, + * and host imports that may alias (hk_host_import_aliases()). An * acceleration structure's address is its buffer's, also on replay, so its * capture/replay is covered by the buffer's. An import of private anonymous * memory of this process (malloc, aligned_alloc, an anonymous private mmap) @@ -973,13 +976,7 @@ hk_device_may_alias_memory(const struct hk_device *dev, f->sparseResidencyImage2D || f->sparseResidencyImage3D || f->sparseResidency2Samples || f->sparseResidency4Samples || f->sparseResidency8Samples || f->sparseResidency16Samples || - f->sparseResidencyAliased) - return true; - - if (f->bufferDeviceAddressCaptureReplay || - f->bufferDeviceAddressCaptureReplayEXT || - f->descriptorBufferCaptureReplay || - f->accelerationStructureCaptureReplay) + f->sparseResidencyAliased || f->bufferDeviceAddressCaptureReplayEXT) return true; if (ext->KHR_external_memory_fd || ext->EXT_external_memory_dma_buf) @@ -1013,13 +1010,8 @@ hk_device_trace_alias_reasons(const struct hk_device *dev, f->sparseResidency8Samples || f->sparseResidency16Samples, "sparseResidencyNSamples"); HK_ALIAS_REASON(f->sparseResidencyAliased, "sparseResidencyAliased"); - HK_ALIAS_REASON(f->bufferDeviceAddressCaptureReplay || - f->bufferDeviceAddressCaptureReplayEXT, - "bufferDeviceAddressCaptureReplay"); - HK_ALIAS_REASON(f->descriptorBufferCaptureReplay, - "descriptorBufferCaptureReplay"); - HK_ALIAS_REASON(f->accelerationStructureCaptureReplay, - "accelerationStructureCaptureReplay"); + HK_ALIAS_REASON(f->bufferDeviceAddressCaptureReplayEXT, + "bufferDeviceAddressCaptureReplayEXT"); HK_ALIAS_REASON(ext->KHR_external_memory_fd, "KHR_external_memory_fd"); HK_ALIAS_REASON(ext->EXT_external_memory_dma_buf, "EXT_external_memory_dma_buf"); HK_ALIAS_REASON(ext->EXT_external_memory_host && @@ -1085,11 +1077,12 @@ hk_CreateDevice(VkPhysicalDevice physicalDevice, simple_mtx_init(&dev->host_alias.cmd_lock, mtx_plain); list_inithead(&dev->host_alias.cmd_buffers); u_rwlock_init(&dev->host_alias.patch_lock); - dev->host_alias.trace = agx_chip_g17_m5(pdev->dev.chip) && + /* dev->dev is opened below: the chip comes from pdev. */ + dev->host_alias.trace = hk_va_tagged_caches(pdev->dev.chip) && (pdev->dev.debug & AGX_DBG_HKTRACE); /* A device whose dependencies always take memory scope has none to * decide per resource (agx_cdm_ordering_memory_scope()). */ - if (dev->may_alias_memory && agx_chip_g17_m5(pdev->dev.chip) && + if (dev->may_alias_memory && hk_va_tagged_caches(pdev->dev.chip) && !agx_cdm_ordering_memory_scope(&pdev->dev)) { /* The word hk_cdm_flush() records for a memory-scoped strong flush. */ const struct agx3_cdm_barrier_info info = { @@ -1213,10 +1206,11 @@ hk_CreateDevice(VkPhysicalDevice physicalDevice, } } - /* Capture-replay buffers reserve their VA and bind the fill page until - * memory is bound, like sparse buffers, so they need it too. */ + /* VK_EXT_buffer_device_address capture/replay buffers reserve their VA + * and bind the fill page until memory is bound, like sparse buffers, so + * they need it too (hk_CreateBuffer()). */ if (dev->vk.enabled_features.sparseBinding || - dev->vk.enabled_features.bufferDeviceAddressCaptureReplay) { + dev->vk.enabled_features.bufferDeviceAddressCaptureReplayEXT) { dev->sparse_fill = agx_bo_create(&dev->dev, AIL_PAGESIZE, 0, 0, "Sparse fill page"); if (!dev->sparse_fill || !agx_bo_map(dev->sparse_fill)) { diff --git a/src/asahi/vulkan/hk_device.h b/src/asahi/vulkan/hk_device.h index 4ef8cfe32cd..9c2ec49ac8c 100644 --- a/src/asahi/vulkan/hk_device.h +++ b/src/asahi/vulkan/hk_device.h @@ -142,15 +142,16 @@ struct hk_device { /* Scratch-filled sparse VA (M5: bounded by sparseAddressSpaceSize). */ uint64_t sparse_scratch_B; /* Live resources through which memory has or may have a second GPU virtual - * address: sparse/capture-replay buffers and image planes, capture/replay - * memory mappings (hk_device_va_alias_get()), fd/dma-buf imports, and host - * imports that may alias (hk_host_import_track()). Atomic; see - * hk_cdm_alias_scope(). */ + * address, on every chip: buffers and image planes with an address of + * their own (hk_device_va_alias_get()), fd/dma-buf imports, and host + * imports that may alias (hk_host_import_track()). Atomic. Read by the + * compute paths that take disjoint addresses for disjoint memory; barrier + * scope follows alias_live instead (hk_cdm_alias_scope()). */ uint32_t va_alias_resources; /* Whether anything this device enabled can map memory at a second GPU * virtual address at any time, decided at creation - * (hk_device_may_alias_memory()). M5 uses it in place of - * va_alias_resources, see hk_cdm_alias_scope(). */ + * (hk_device_may_alias_memory()). Left false with host_alias.dynamic, + * see hk_cdm_alias_scope(). */ bool may_alias_memory; /* The kernel runs a render's fragment work after the previous render's * on a queue, partial renders included @@ -158,10 +159,11 @@ struct hk_device { * whole tiles an earlier render stores waits for it in its fragment half * alone (hk_sync.h, tile hazards). */ bool load_hazard_frag; - /* M5 with host_alias.dynamic: live resources through which memory has or - * may have a second GPU address (hk_device_alias_get()), atomic: - * va_alias_resources, fd and dma-buf imports, and host imports that may - * alias. hk_cdm_alias_scope() asks for memory scope while it is not 0. */ + /* With host_alias.dynamic: live resources through which memory has or + * may have a second GPU address (hk_device_alias_get()), atomic: buffers + * and image planes with an address of their own, fd and dma-buf imports, + * and host imports that may alias. hk_cdm_alias_scope() asks for memory + * scope while it is not 0. */ uint32_t alias_live; /* Monotonic counter incremented atomically whenever a buffer is first bound @@ -171,9 +173,9 @@ struct hk_device { * HK_SYNC_GFX_MEM. */ uint32_t buffer_alias_mem_gen; struct { - /* M5: the device enabled a feature through which memory can get a - * second address, so the scope follows alias_live instead of - * may_alias_memory, which was left false + /* VA-tagged caches (hk_va_tagged_caches()): the device enabled a + * feature through which memory can get a second address, so the scope + * follows alias_live instead of may_alias_memory, which was left false * (hk_device_may_alias_memory()). */ bool dynamic; /* ASAHI_MESA_DEBUG=hktrace. */ @@ -509,41 +511,46 @@ enum hk_perftest { #define HK_PERF(dev, flag) unlikely((dev)->perftest &HK_PERF_##flag) -/* G17P and M5 GPU caches are tagged by virtual address, so when memory may be - * mapped at more than one GPU VA an application compute/transfer barrier needs - * the memory-scoped strong flush (hk_cdm_visibility_flush()). +/* Whether the GPU caches are tagged by virtual address (G17P and M5): a + * strong CDM flush then makes a write visible only to reads through the same + * mapping, and a read through a second address of the same memory needs the + * memory-scope flush (hk_cdm_alias_scope()). */ +static inline bool +hk_va_tagged_caches(enum agx_chip chip) +{ + return chip == AGX_CHIP_G17P || agx_chip_g17_m5(chip); +} + +/* With VA-tagged caches, when memory may be mapped at more than one GPU VA an + * application compute/transfer barrier needs the memory-scoped strong flush + * (hk_cdm_visibility_flush()). * - * On M5, devices whose barriers always take memory scope - * (agx_cdm_ordering_memory_scope()) take it on every barrier and in every - * launch's ordering record. Other M5 devices that enable aliasing-capable - * features (sparse, BDA, capture/replay, fd/dma-buf, writable host imports) - * track active aliasing resources in alias_live under host_alias.dynamic (or - * force memory scope via may_alias_memory / ASAHI_M5_HOST_ALIAS_STATIC=1). - * Barriers recorded while alias_live == 0 record patch sites (struct - * hk_alias_site) that are upgraded in place when alias_live leaves 0 or at - * submission; unpatched paths (graphics dependencies, event-using command - * buffers) and stream-end flushes keep memory scope unconditionally while - * dynamic mode is active. + * Devices whose barriers always take memory scope + * (agx_cdm_ordering_memory_scope(), multi-die M5) take it on every barrier + * and in every launch's ordering record. Other devices that enable + * aliasing-capable features (sparse, VK_EXT_buffer_device_address + * capture/replay, fd/dma-buf, writable host imports) track live aliasing + * resources in alias_live under host_alias.dynamic (or force memory scope via + * may_alias_memory / on M5 ASAHI_M5_HOST_ALIAS_STATIC=1). Barriers recorded + * while alias_live == 0 record patch sites (struct hk_alias_site): the 4-byte + * barrier record, upgraded in place when alias_live leaves 0 or at + * submission. A command buffer that waits for an event the host may set keeps + * memory scope throughout while dynamic mode is active. * - * On G17P, memory scope is selected at barrier recording time whenever - * va_alias_resources != 0 (live sparse, capture/replay, fd, or aliasing host - * imports). The flush is emitted into an open compute stream holding work; - * earlier closed streams already flushed at stream end. + * M5 compute streams end with the memory-scoped flush while dynamic mode is + * active. G17P streams end without it: the end of a G17P command already + * publishes its writes to every address. * - * Not covered on G17P: resources or imports created after command buffer - * recording and reached via device address or UpdateAfterBind descriptors; - * graphics-only sources writing through a sparse image read via a plain alias - * receive only the ordinary post-graphics strong flush (untested). + * Not covered: graphics-only sources writing through a sparse image read via + * a plain alias receive only the ordinary post-graphics strong flush + * (untested). */ static inline bool hk_cdm_alias_scope(const struct hk_device *dev) { - if (agx_chip_g17_m5(dev->dev.chip)) - return agx_cdm_ordering_memory_scope(&dev->dev) || - dev->may_alias_memory || p_atomic_read(&dev->alias_live); - - return dev->dev.chip == AGX_CHIP_G17P && - p_atomic_read(&dev->va_alias_resources) != 0; + return hk_va_tagged_caches(dev->dev.chip) && + (agx_cdm_ordering_memory_scope(&dev->dev) || + dev->may_alias_memory || p_atomic_read(&dev->alias_live)); } /* Whether the CDM strong flush has a memory-scoped form, which memory @@ -580,15 +587,6 @@ hk_cdm_caches_pending(const struct hk_device *dev) return dev->dev.chip == AGX_CHIP_G17A; } -/* As hk_cdm_alias_scope(), for an application dependency recorded without a - * patch site (struct hk_alias_site): with host_alias.dynamic always, since - * nothing would give the record memory scope once a resource may alias. */ -static inline bool -hk_cdm_alias_scope_unpatched(const struct hk_device *dev) -{ - return hk_cdm_alias_scope(dev) || dev->host_alias.dynamic; -} - /* Writes the patch sites of every executable command buffer that has * unwritten ones (host_alias.cmd_buffers), when the first resource that may * alias appears. Returns how many command buffers it wrote. */ @@ -648,8 +646,8 @@ hk_device_alias_put(struct hk_device *dev, uint32_t n) p_atomic_add(&dev->alias_live, -(int32_t)n); } -/* A buffer, image plane or memory object that maps memory at a GPU address - * of its own (va_alias_resources), from its creation to its destruction. */ +/* A buffer or image plane that maps memory at a GPU address of its own + * (va_alias_resources), from its creation to its destruction. */ static inline void hk_device_va_alias_get(struct hk_device *dev, enum hk_alias_cause cause) { diff --git a/src/asahi/vulkan/hk_device_memory.c b/src/asahi/vulkan/hk_device_memory.c index 3143a860cc0..495501afd23 100644 --- a/src/asahi/vulkan/hk_device_memory.c +++ b/src/asahi/vulkan/hk_device_memory.c @@ -307,8 +307,8 @@ hk_host_maps_classify_self(const void *pointer, uint64_t size) } /* Enter a host import into the device's set so va_alias_resources (and - * alias_live on M5 with host_alias.dynamic) only counts imports that may - * alias another GPU virtual address (hk_host_import_aliases()). A + * alias_live with host_alias.dynamic) only counts imports that may alias + * another GPU virtual address (hk_host_import_aliases()). A * GPU-writable import of pages that are all private anonymous memory of this * process (/proc/self/maps), and that no other live import overlaps, has its * pages at no other GPU address. A read-only one needs only private mappings: @@ -323,7 +323,8 @@ hk_host_maps_classify_self(const void *pointer, uint64_t size) * * Costs two reads of /proc/self/maps up to the lines that cover the range. * The imports that began to alias count in va_alias_resources (and - * alias_live via hk_device_alias_add() on M5) before the import is returned. */ + * alias_live via hk_device_alias_add() with host_alias.dynamic) before the + * import is returned. */ static VkResult hk_host_import_track(struct hk_device *dev, struct hk_device_memory *mem, const void *pointer, uint64_t size, @@ -399,19 +400,6 @@ hk_host_import_untrack(struct hk_device *dev, struct hk_device_memory *mem) (void *)mem, stopped, p_atomic_read(&dev->alias_live)); } -/* Memory mapped again in the capture/replay region (replay_va) counts from - * its mapping to its release (hk_device_va_alias_get()). */ -static void -hk_memory_release_replay_va(struct hk_device *dev, - struct hk_device_memory *mem) -{ - if (!mem->replay_va) - return; - agx_va_free(&dev->dev, mem->replay_va, true); - mem->replay_va = NULL; - hk_device_va_alias_put(dev); -} - VKAPI_ATTR VkResult VKAPI_CALL hk_AllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo, const VkAllocationCallbacks *pAllocator, VkDeviceMemory *pMem) @@ -588,16 +576,56 @@ hk_AllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo, if (mem->vk.alloc_flags & VK_MEMORY_ALLOCATE_ZERO_INITIALIZE_BIT_EXT) flags |= AGX_BO_ZERO_ON_REUSE; + /* Capture/replay memory is never cached (agx_bo_create_at()). */ + const bool capture_replay = + mem->vk.alloc_flags & + VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT; + /* A BO from the cache allocates nothing new. Fetch from the cache once * and run hk_system_memory_fits() only on a real miss (including after * discarding purged entries), avoiding both a duplicate cache-lock walk - * and per-allocation /proc/meminfo reads. */ + * and per-allocation /proc/meminfo reads. Capture/replay memory always + * allocates. */ bool mem_rejected = false; /* D3D12 promises 64 KiB alignment for heap addresses, and this costs * only virtual address space. */ - mem->bo = agx_bo_create_checked(&dev->dev, aligned_size, 64 * 1024, flags, - "App memory", hk_allow_bo_alloc, - &mem_rejected); + if (capture_replay) { + /* Not from the cache, but as in agx_bo_create_checked(), idle cached + * BOs must not be why it fails: evict them and ask once more. */ + if (!hk_system_memory_fits(aligned_size)) { + agx_bo_cache_evict_all(&dev->dev); + mem_rejected = !hk_system_memory_fits(aligned_size); + } + } else { + mem->bo = agx_bo_create_checked(&dev->dev, aligned_size, 64 * 1024, + flags, "App memory", hk_allow_bo_alloc, + &mem_rejected); + } + + if (capture_replay && !mem_rejected) { + /* Capture/replay memory has one address, in the capture/replay + * region and the captured one on replay. Buffers and images bound + * to it take it plus their offset, so they replay with it. */ + const uint64_t fixed_addr = + capture_info ? capture_info->opaqueCaptureAddress : 0; + enum agx_va_flags va_flags = fixed_addr ? AGX_VA_FIXED : 0; + if (dev->dev.replay_heap_initialized) + va_flags |= AGX_VA_CAPTURE_REPLAY; + struct agx_va *va = agx_va_alloc(&dev->dev, aligned_size, 64 * 1024, + va_flags, fixed_addr); + if (!va) { + /* A replay address that is already taken is the application's + * error to handle, not an out-of-memory condition. */ + result = fixed_addr + ? vk_errorf(dev, VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS, + "Capture/replay address %#llx is in use", + (unsigned long long)fixed_addr) + : vk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY); + goto fail_alloc; + } + mem->bo = agx_bo_create_at(&dev->dev, aligned_size, flags, va, + "App memory"); + } if (!mem->bo) { if (mem_rejected) { result = vk_errorf(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY, @@ -610,64 +638,12 @@ hk_AllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo, } } - /* VK_EXT_descriptor_buffer capture/replay: image descriptors embed the - * address of the image's memory, so memory allocated for capture/replay - * also maps at an address in the capture/replay region, the captured one - * on replay when that is one. Buffers and images bound to it take that - * address (hk_device_memory_addr()); buffers with a capture/replay address - * of their own keep it. */ - if ((mem->vk.alloc_flags & - VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT) && - dev->vk.enabled_features.descriptorBufferCaptureReplay && - dev->dev.replay_heap_initialized) { - const uint64_t fixed_addr = - capture_info ? capture_info->opaqueCaptureAddress : 0; - /* The range follows the requested size, not the BO's: a BO from the - * cache can be up to twice as large, so its size differs between - * capture and replay, and a larger replayed range would cover the next - * captured address. Bindings only reach offsets below that size. */ - mem->replay_va = agx_va_alloc( - &dev->dev, aligned_size, 64 * 1024, - AGX_VA_CAPTURE_REPLAY | (fixed_addr ? AGX_VA_FIXED : 0), fixed_addr); - if (mem->replay_va && - (agx_bo_bind(&dev->dev, mem->bo, mem->replay_va->addr, - aligned_size, 0, - DRM_ASAHI_BIND_READ | DRM_ASAHI_BIND_WRITE) || - agx_bo_bind(&dev->dev, mem->bo, - agx_rw_addr_to_ro(&dev->dev, mem->replay_va->addr), - aligned_size, 0, DRM_ASAHI_BIND_READ))) { - agx_va_free(&dev->dev, mem->replay_va, true); - mem->replay_va = NULL; - agx_bo_unreference(&dev->dev, mem->bo); - mem->bo = NULL; - result = vk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY); - goto fail_alloc; - } - /* An address this region cannot take was not captured with the - * feature (on a device without it, it is the BO's own address): keep - * buffer device address replay as it was, through the buffers' own - * capture/replay addresses. */ - /* The memory is reached through two read-write addresses now. */ - if (mem->replay_va) - hk_device_va_alias_get(dev, HK_ALIAS_CAUSE_CAPTURE_REPLAY); - } - - if (mem->vk.alloc_flags & - VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT) { - mem->opaque_capture_address = - mem->replay_va ? mem->replay_va->addr - : capture_info && capture_info->opaqueCaptureAddress - ? capture_info->opaqueCaptureAddress - : mem->bo->va->addr; - } - /* Shadow map in case this is used for a sparse resident buffer. The mapping * persists with the BO across userspace cache reuse, so cached allocations * skip the redundant VM_BIND ioctl. */ int ret = agx_bo_ensure_ro_shadow(&dev->dev, mem->bo); if (ret) { /* The bind fails once the VM carries a sticky error. */ - hk_memory_release_replay_va(dev, mem); agx_bo_unreference(&dev->dev, mem->bo); mem->bo = NULL; vk_device_memory_destroy(&dev->vk, pAllocator, &mem->vk); @@ -678,7 +654,6 @@ hk_AllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo, result = hk_host_import_track(dev, mem, host_info->pHostPointer, mem->bo->size, host_maps_before_pin); if (result != VK_SUCCESS) { - hk_memory_release_replay_va(dev, mem); agx_bo_unreference(&dev->dev, mem->bo); mem->bo = NULL; vk_device_memory_destroy(&dev->vk, pAllocator, &mem->vk); @@ -806,8 +781,6 @@ hk_FreeMemory(VkDevice device, VkDeviceMemory _mem, /* The BO may outlive this object in the BO cache. */ hk_set_memory_priority(dev, mem, 0.5f); - hk_memory_release_replay_va(dev, mem); - if (mem->alias_import) p_atomic_dec(&dev->va_alias_resources); @@ -1025,5 +998,5 @@ hk_GetDeviceMemoryOpaqueCaptureAddress( { VK_FROM_HANDLE(hk_device_memory, memory, pInfo->memory); - return memory->opaque_capture_address; + return hk_device_memory_addr(memory); } diff --git a/src/asahi/vulkan/hk_device_memory.h b/src/asahi/vulkan/hk_device_memory.h index 82ff86637de..b2162b6f024 100644 --- a/src/asahi/vulkan/hk_device_memory.h +++ b/src/asahi/vulkan/hk_device_memory.h @@ -22,16 +22,6 @@ struct hk_device_memory { struct agx_bo *bo; - /* Opaque application-visible token used for BDA capture/replay. This is - * deliberately separate from the BO's current mapping: replay buffers own - * the fixed VA that determines their device address. */ - uint64_t opaque_capture_address; - - /* VK_EXT_descriptor_buffer capture/replay (hk_AllocateMemory()): a second - * mapping of the BO at an address that replays, which buffers and images - * bound to this memory take as its address. NULL otherwise. */ - struct agx_va *replay_va; - void *map; /* map is this object's own mapping of map_size bytes rather than the @@ -53,7 +43,7 @@ struct hk_device_memory { bool alias_import; /* A host import tracked in hk_device::host_alias.imports; counts in - * hk_device::va_alias_resources (and hk_device::alias_live on M5 with + * hk_device::va_alias_resources (and hk_device::alias_live with * host_alias.dynamic) only while it may alias. */ bool host_alias_tracked; /* An fd or dma-buf import, counted in hk_device::alias_live for its @@ -74,7 +64,7 @@ VK_DEFINE_NONDISP_HANDLE_CASTS(hk_device_memory, vk.base, VkDeviceMemory, static inline uint64_t hk_device_memory_addr(const struct hk_device_memory *mem) { - return mem->replay_va ? mem->replay_va->addr : mem->bo->va->addr; + return mem->bo->va->addr; } void *hk_device_memory_copy_map(struct hk_device_memory *mem); diff --git a/src/asahi/vulkan/hk_event.c b/src/asahi/vulkan/hk_event.c index 6d965d29bfa..e7e2427c132 100644 --- a/src/asahi/vulkan/hk_event.c +++ b/src/asahi/vulkan/hk_event.c @@ -335,19 +335,21 @@ hk_CmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent _event, /* The wait orders later work behind this stream's source work, but on * G17P and M5 a read through another address of the same memory also * needs the source writes flushed with memory scope - * (hk_cdm_alias_scope()). A command buffer that uses events records no - * patch site for a later host import: it has the scope whenever an - * import could add such an address (hk_cmd_alias_scope()). */ + * (hk_cdm_alias_scope()). Where only a resource created later could + * map it there, the stream's last record is a patch site instead. */ struct hk_device *dev = hk_cmd_buffer_device(cmd); struct hk_cs *cs = cmd->current_cs.cs; if (cs && cs->imm_writes.size && (src_kinds & HK_SYNC_XFER)) { hk_dispatch_imm_writes(cmd, cs); cs = cmd->current_cs.cs; } - if (cs && (src_kinds & (HK_SYNC_COMPUTE | HK_SYNC_XFER)) && - hk_cmd_alias_scope(cmd) && hk_cdm_visibility_flush_needed(dev, cs, true)) { - hk_ensure_cs_has_space(cmd, cs, AGX3_CDM_BARRIER_SIZE); - hk_cdm_visibility_flush(dev, cs, true); + if (cs && (src_kinds & (HK_SYNC_COMPUTE | HK_SYNC_XFER))) { + if (!hk_cmd_alias_scope(cmd)) { + hk_cdm_note_alias_site(cmd, cs); + } else if (hk_cdm_visibility_flush_needed(dev, cs, true)) { + hk_ensure_cs_has_space(cmd, cs, AGX3_CDM_BARRIER_SIZE); + hk_cdm_visibility_flush(dev, cs, true); + } } /* A host access in this dependency is made available before the status @@ -381,6 +383,35 @@ hk_CmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent _event, hk_event_write(cmd, event, hk_sync_kinds_src(stageMask), false); } +/* Whether a wait may be on an event set by vkSetEvent. Its barriers must then + * hold only host operations in their first synchronization scope + * (VUID-vkCmdWaitEvents2-pEvents-03839, + * VUID-vkCmdWaitEvents-srcStageMask-01158): no barriers at all, or barriers + * whose srcStageMask is HOST or NONE. Only a source stage on the device, with + * no HOST beside it, rules the host out. */ +static bool +hk_dependency_waits_for_host(const VkDependencyInfo *info) +{ + const VkPipelineStageFlags2 host = VK_PIPELINE_STAGE_2_HOST_BIT; + VkPipelineStageFlags2 src = 0; + for (uint32_t i = 0; i < info->memoryBarrierCount; ++i) { + if (info->pMemoryBarriers[i].srcStageMask & host) + return true; + src |= info->pMemoryBarriers[i].srcStageMask; + } + for (uint32_t i = 0; i < info->bufferMemoryBarrierCount; ++i) { + if (info->pBufferMemoryBarriers[i].srcStageMask & host) + return true; + src |= info->pBufferMemoryBarriers[i].srcStageMask; + } + for (uint32_t i = 0; i < info->imageMemoryBarrierCount; ++i) { + if (info->pImageMemoryBarriers[i].srcStageMask & host) + return true; + src |= info->pImageMemoryBarriers[i].srcStageMask; + } + return src == VK_PIPELINE_STAGE_2_NONE; +} + /* A wait polls the event word on the GPU, so a later host vkSetEvent is * honoured. A poll whose set never comes hangs the queue, which only G17P * recovers from; elsewhere the wait is the plain barrier below. */ @@ -431,6 +462,11 @@ hk_CmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, cmd->state.uses_events = true; perf_debug(cmd, "Wait events"); + /* An event the host sets (hk_cmd_alias_scope()). */ + for (uint32_t i = 0; i < eventCount; ++i) + cmd->state.alias_host_wait |= + hk_dependency_waits_for_host(&pDependencyInfos[i]); + for (uint32_t i = 0; i < eventCount; ++i) { VK_FROM_HANDLE(hk_event, event, pEvents[i]); assert(pDependencyInfos[i].sType == VK_STRUCTURE_TYPE_DEPENDENCY_INFO); diff --git a/src/asahi/vulkan/hk_tess_batch.c b/src/asahi/vulkan/hk_tess_batch.c index ab05823cd1a..3943fe01229 100644 --- a/src/asahi/vulkan/hk_tess_batch.c +++ b/src/asahi/vulkan/hk_tess_batch.c @@ -51,7 +51,8 @@ hk_tess_batch_stage(struct hk_cmd_buffer *cmd, enum hk_tess_stage stage) /* Before the tail goes: a chunk link must still follow a barrier. */ hk_ensure_cs_has_space(cmd, cs, 0x2000); - if (cs->stats.cmds && cs->current == cs->weak_barrier_end) { + if (cs->stats.cmds && cs->current == cs->weak_barrier_end && + cs->current != cs->alias_pin) { batch->dropped[stage] = cs->current; cs->current = (uint8_t *)cs->current - AGX3_CDM_BARRIER_SIZE; cs->weak_barrier_end = NULL; diff --git a/src/asahi/vulkan/meson.build b/src/asahi/vulkan/meson.build index 449a589f169..347a4ccca4c 100644 --- a/src/asahi/vulkan/meson.build +++ b/src/asahi/vulkan/meson.build @@ -1591,6 +1591,24 @@ if with_tests gnu_symbol_visibility : 'hidden', ) + hk_vulkan_capture_replay = executable( + 'brl-vulkan-capture-replay', + files('tests/hk_vulkan_capture_replay.c'), + include_directories : [inc_include], + dependencies : [cc.find_library('vulkan')], + c_args : [c_msvc_compat_args], + gnu_symbol_visibility : 'hidden', + ) + + hk_vulkan_event_alias = executable( + 'brl-vulkan-event-alias', + files('tests/hk_vulkan_event_alias.c'), + include_directories : [inc_include], + dependencies : [cc.find_library('vulkan')], + c_args : [c_msvc_compat_args], + gnu_symbol_visibility : 'hidden', + ) + # Submit-time render packing on reusable command buffers: packed, replayed, # alone and regrouped submissions, each pixel-exact (hk_render_pack.h). hk_vulkan_render_pack_replay = executable( diff --git a/src/asahi/vulkan/tests/hk_vulkan_capture_replay.c b/src/asahi/vulkan/tests/hk_vulkan_capture_replay.c new file mode 100644 index 00000000000..68cadfa4314 --- /dev/null +++ b/src/asahi/vulkan/tests/hk_vulkan_capture_replay.c @@ -0,0 +1,235 @@ +/* Copyright 2026 Aurora Silicon + * SPDX-License-Identifier: MIT + * + * Buffer device address capture/replay without a second mapping: a + * capture/replay buffer takes its memory's address plus the bind offset, + * memory allocated again on another device at its captured address gets it + * back and so does the buffer, and an address still in use is refused with + * VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS. With VK_EXT_buffer_device_address, + * whose memory need not be capture/replay memory, the buffer's own address + * replays. Nothing executes on the GPU, so the test also runs under the + * no-op DRM shim. + */ +#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) + +#define EXPECT(cond) \ + do { \ + if (!(cond)) { \ + fprintf(stderr, "FAIL: %s\n", #cond); \ + exit(1); \ + } \ + } while (0) + +static const VkDeviceSize offset = 64 * 1024, size = 256 * 1024; + +static VkDevice +make_device(VkPhysicalDevice pdev, bool ext) +{ + const float priority = 1.0f; + const VkDeviceQueueCreateInfo queue_info = { + .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, + .queueCount = 1, + .pQueuePriorities = &priority, + }; + const VkPhysicalDeviceVulkan12Features f12 = { + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES, + .bufferDeviceAddress = VK_TRUE, + .bufferDeviceAddressCaptureReplay = VK_TRUE, + }; + const VkPhysicalDeviceBufferDeviceAddressFeaturesEXT fext = { + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_EXT, + .bufferDeviceAddress = VK_TRUE, + .bufferDeviceAddressCaptureReplay = VK_TRUE, + }; + const char *ext_name = VK_EXT_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME; + const VkDeviceCreateInfo device_info = { + .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, + .pNext = ext ? (const void *)&fext : &f12, + .queueCreateInfoCount = 1, + .pQueueCreateInfos = &queue_info, + .enabledExtensionCount = ext, + .ppEnabledExtensionNames = &ext_name, + }; + VkDevice dev; + CHECK(vkCreateDevice(pdev, &device_info, NULL, &dev)); + return dev; +} + +static VkResult +alloc(VkDevice dev, uint64_t replay_addr, VkDeviceMemory *mem) +{ + const VkMemoryOpaqueCaptureAddressAllocateInfo capture = { + .sType = VK_STRUCTURE_TYPE_MEMORY_OPAQUE_CAPTURE_ADDRESS_ALLOCATE_INFO, + .opaqueCaptureAddress = replay_addr, + }; + const VkMemoryAllocateFlagsInfo flags = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO, + .pNext = &capture, + .flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT | + VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT, + }; + const VkMemoryAllocateInfo info = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .pNext = &flags, + .allocationSize = size, + }; + return vkAllocateMemory(dev, &info, NULL, mem); +} + +/* A capture/replay buffer bound at `offset` in `mem`; returns its address. */ +static uint64_t +bind_buffer(VkDevice dev, VkDeviceMemory mem, uint64_t replay_addr, + VkBuffer *buf) +{ + const VkBufferOpaqueCaptureAddressCreateInfo capture = { + .sType = VK_STRUCTURE_TYPE_BUFFER_OPAQUE_CAPTURE_ADDRESS_CREATE_INFO, + .opaqueCaptureAddress = replay_addr, + }; + const VkBufferCreateInfo info = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .pNext = &capture, + .flags = VK_BUFFER_CREATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT, + .size = 4096, + .usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | + VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, + }; + CHECK(vkCreateBuffer(dev, &info, NULL, buf)); + const VkBufferDeviceAddressInfo addr_info = { + .sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO, + .buffer = *buf, + }; + /* Before the bind, the address asked for (none on capture). */ + EXPECT(vkGetBufferOpaqueCaptureAddress(dev, &addr_info) == replay_addr); + VkMemoryRequirements req; + vkGetBufferMemoryRequirements(dev, *buf, &req); + EXPECT((req.memoryTypeBits & 1) && offset % req.alignment == 0); + CHECK(vkBindBufferMemory(dev, *buf, mem, offset)); + + const uint64_t addr = vkGetBufferDeviceAddress(dev, &addr_info); + EXPECT(vkGetBufferOpaqueCaptureAddress(dev, &addr_info) == addr); + return addr; +} + +static uint64_t +memory_addr(VkDevice dev, VkDeviceMemory mem) +{ + const VkDeviceMemoryOpaqueCaptureAddressInfo info = { + .sType = VK_STRUCTURE_TYPE_DEVICE_MEMORY_OPAQUE_CAPTURE_ADDRESS_INFO, + .memory = mem, + }; + return vkGetDeviceMemoryOpaqueCaptureAddress(dev, &info); +} + +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; + } + + /* Capture. */ + VkDevice dev = make_device(pdev, false); + VkDeviceMemory mem, taken; + VkBuffer buf; + CHECK(alloc(dev, 0, &mem)); + const uint64_t mem_addr = memory_addr(dev, mem); + const uint64_t buf_addr = bind_buffer(dev, mem, 0, &buf); + EXPECT(mem_addr != 0 && buf_addr == mem_addr + offset); + EXPECT(alloc(dev, mem_addr, &taken) == + VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS); + vkDestroyBuffer(dev, buf, NULL); + vkFreeMemory(dev, mem, NULL); + vkDestroyDevice(dev, NULL); + + /* Replay, on a new device. */ + dev = make_device(pdev, false); + CHECK(alloc(dev, mem_addr, &mem)); + EXPECT(memory_addr(dev, mem) == mem_addr); + EXPECT(bind_buffer(dev, mem, buf_addr, &buf) == buf_addr); + vkDestroyBuffer(dev, buf, NULL); + vkFreeMemory(dev, mem, NULL); + + /* Freed, the address is free again. */ + CHECK(alloc(dev, mem_addr, &mem)); + vkFreeMemory(dev, mem, NULL); + vkDestroyDevice(dev, NULL); + + /* VK_EXT_buffer_device_address: ordinary memory, the buffer replays its + * own address. The replay device allocates other memory first, so that + * ordinary addresses differ from the capture's. */ + const VkMemoryAllocateInfo plain = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = size, + }; + uint64_t ext_addr = 0; + for (unsigned replay = 0; replay < 2; replay++) { + dev = make_device(pdev, true); + PFN_vkGetBufferDeviceAddressEXT get_addr = + (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr( + dev, "vkGetBufferDeviceAddressEXT"); + EXPECT(get_addr != NULL); + VkDeviceMemory other = VK_NULL_HANDLE; + if (replay) + CHECK(vkAllocateMemory(dev, &plain, NULL, &other)); + CHECK(vkAllocateMemory(dev, &plain, NULL, &mem)); + const VkBufferDeviceAddressCreateInfoEXT replay_info = { + .sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_CREATE_INFO_EXT, + .deviceAddress = ext_addr, + }; + const VkBufferCreateInfo info = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .pNext = &replay_info, + .flags = VK_BUFFER_CREATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT, + .size = 4096, + .usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | + VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, + }; + CHECK(vkCreateBuffer(dev, &info, NULL, &buf)); + const VkBufferDeviceAddressInfo addr_info = { + .sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO, + .buffer = buf, + }; + /* The EXT feature lets the address be asked before the bind + * (VUID-vkGetBufferDeviceAddress-bufferDeviceAddress-03324). */ + const uint64_t addr = get_addr(dev, &addr_info); + CHECK(vkBindBufferMemory(dev, buf, mem, offset)); + EXPECT(get_addr(dev, &addr_info) == addr); + EXPECT(addr != 0 && (!replay || addr == ext_addr)); + ext_addr = addr; + vkDestroyBuffer(dev, buf, NULL); + vkFreeMemory(dev, mem, NULL); + vkFreeMemory(dev, other, NULL); + vkDestroyDevice(dev, NULL); + } + + vkDestroyInstance(instance, NULL); + printf("PASS\n"); + return 0; +} diff --git a/src/asahi/vulkan/tests/hk_vulkan_event_alias.c b/src/asahi/vulkan/tests/hk_vulkan_event_alias.c new file mode 100644 index 00000000000..fe881acc6d8 --- /dev/null +++ b/src/asahi/vulkan/tests/hk_vulkan_event_alias.c @@ -0,0 +1,254 @@ +/* Copyright 2026 Aurora Silicon + * SPDX-License-Identifier: MIT + * + * A command buffer that waits for an event the host sets must take memory + * scope throughout at vkEndCommandBuffer on G17P and M5: memory that first + * aliases while it waits is too late to patch, the CDM may have read the + * records past the wait. A vkSetEvent wait may have no barriers at all, or + * barriers with no source stage (VUID-vkCmdWaitEvents2-pEvents-03839). Each + * case records transfer work, the wait and a barrier, ends the command + * buffer, then creates a sparse buffer so that memory first aliases after + * recording. The driver's alias trace says how many command buffers were + * still waiting for that to be patched: none for a host wait, the one + * recorded for an event the device sets. Nothing + * executes on the GPU, so the test runs under the no-op DRM shim. + */ +#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 VkDevice dev; +static VkQueue queue; +static VkCommandPool pool; +static VkBuffer dst; +static VkEvent event; + +enum wait_kind { WAIT_EMPTY, WAIT_NONE_STAGE, WAIT_HOST_STAGE, WAIT_DEVICE_SET }; + +static const VkMemoryBarrier2 xfer_barrier = { + .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, + .srcStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT, + .srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT, + .dstAccessMask = VK_ACCESS_2_TRANSFER_READ_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT, +}; +static const VkMemoryBarrier2 host_barrier = { + .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, + .srcStageMask = VK_PIPELINE_STAGE_2_HOST_BIT, + .srcAccessMask = VK_ACCESS_2_HOST_WRITE_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT, + .dstAccessMask = VK_ACCESS_2_TRANSFER_READ_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT, +}; + +/* An empty first scope also holds only host operations. */ +static const VkMemoryBarrier2 none_barrier = { + .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, + .srcStageMask = VK_PIPELINE_STAGE_2_NONE, + .dstStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT, + .dstAccessMask = VK_ACCESS_2_TRANSFER_READ_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT, +}; + +static VkDependencyInfo +dep(const VkMemoryBarrier2 *b) +{ + return (VkDependencyInfo){ + .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, + .memoryBarrierCount = b ? 1 : 0, + .pMemoryBarriers = b, + }; +} + +/* Records and ends the case, then returns the patched= count the trace + * reports when the first aliasing resource appears. */ +static unsigned +run_case(enum wait_kind kind) +{ + VkCommandBuffer cb; + const VkCommandBufferAllocateInfo cai = { + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, + .commandPool = pool, + .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, + .commandBufferCount = 1, + }; + CHECK(vkAllocateCommandBuffers(dev, &cai, &cb)); + const VkCommandBufferBeginInfo bi = { + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, + }; + CHECK(vkBeginCommandBuffer(cb, &bi)); + + vkCmdFillBuffer(cb, dst, 0, 4096, 1); + const VkDependencyInfo xfer = dep(&xfer_barrier); + VkDependencyInfo wait = dep(NULL); + if (kind == WAIT_NONE_STAGE) + wait = dep(&none_barrier); + if (kind == WAIT_HOST_STAGE) + wait = dep(&host_barrier); + if (kind == WAIT_DEVICE_SET) { + vkCmdSetEvent2(cb, event, &xfer); + wait = xfer; + } + vkCmdWaitEvents2(cb, 1, &event, &wait); + vkCmdPipelineBarrier2(cb, &xfer); + vkCmdFillBuffer(cb, dst, 4096, 4096, 2); + vkCmdPipelineBarrier2(cb, &xfer); + vkCmdFillBuffer(cb, dst, 8192, 4096, 3); + CHECK(vkEndCommandBuffer(cb)); + + /* The first aliasing resource, after recording; the trace goes to stderr. */ + fflush(stderr); + FILE *log = tmpfile(); + const int saved = dup(2); + if (!log || saved < 0 || dup2(fileno(log), 2) < 0) { + fprintf(stderr, "FAIL: redirect stderr\n"); + exit(1); + } + const VkBufferCreateInfo sbi = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .flags = VK_BUFFER_CREATE_SPARSE_BINDING_BIT, + .size = 1 << 20, + .usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, + }; + VkBuffer sparse; + const VkResult r = vkCreateBuffer(dev, &sbi, NULL, &sparse); + fflush(stderr); + dup2(saved, 2); + close(saved); + CHECK(r); + + unsigned patched = ~0u; + char line[256]; + rewind(log); + while (fgets(line, sizeof(line), log)) { + if (sscanf(line, "M5_HOST_ALIAS began cause=sparse patched=%u", + &patched) == 1) + break; + } + fclose(log); + + if (kind != WAIT_DEVICE_SET) + CHECK(vkSetEvent(dev, event)); + vkDestroyBuffer(dev, sparse, NULL); + vkFreeCommandBuffers(dev, pool, 1, &cb); + CHECK(vkResetEvent(dev, event)); + return patched; +} + +int +main(void) +{ + /* The driver's alias trace, keeping any flags the caller set. */ + const char *flags = getenv("ASAHI_MESA_DEBUG"); + char with_trace[256]; + snprintf(with_trace, sizeof(with_trace), "%s%shktrace", flags ? flags : "", + flags && *flags ? "," : ""); + setenv("ASAHI_MESA_DEBUG", with_trace, 1); + + const VkApplicationInfo app = { + .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, + .apiVersion = VK_API_VERSION_1_3, + }; + const VkInstanceCreateInfo ici = { + .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, + .pApplicationInfo = &app, + }; + VkInstance inst; + CHECK(vkCreateInstance(&ici, NULL, &inst)); + uint32_t n = 1; + VkPhysicalDevice pdev; + const VkResult er = vkEnumeratePhysicalDevices(inst, &n, &pdev); + if ((er != VK_SUCCESS && er != VK_INCOMPLETE) || n == 0) { + fprintf(stderr, "FAIL: no physical device\n"); + return 1; + } + + VkPhysicalDeviceFeatures supported; + vkGetPhysicalDeviceFeatures(pdev, &supported); + if (!supported.sparseBinding) { + printf("SKIP: no sparseBinding\n"); + return 77; + } + + const float priority = 1.0f; + const VkDeviceQueueCreateInfo qi = { + .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, + .queueCount = 1, + .pQueuePriorities = &priority, + }; + const VkPhysicalDeviceVulkan13Features f13 = { + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES, + .synchronization2 = VK_TRUE, + }; + const VkPhysicalDeviceFeatures f = {.sparseBinding = VK_TRUE}; + const VkDeviceCreateInfo dci = { + .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, + .pNext = &f13, + .queueCreateInfoCount = 1, + .pQueueCreateInfos = &qi, + .pEnabledFeatures = &f, + }; + CHECK(vkCreateDevice(pdev, &dci, NULL, &dev)); + vkGetDeviceQueue(dev, 0, 0, &queue); + + const VkCommandPoolCreateInfo pci = { + .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, + }; + CHECK(vkCreateCommandPool(dev, &pci, NULL, &pool)); + const VkEventCreateInfo eci = {.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO}; + CHECK(vkCreateEvent(dev, &eci, NULL, &event)); + + const VkBufferCreateInfo bci = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .size = 16384, + .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT, + }; + CHECK(vkCreateBuffer(dev, &bci, NULL, &dst)); + VkMemoryRequirements req; + vkGetBufferMemoryRequirements(dev, dst, &req); + const VkMemoryAllocateInfo mai = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = req.size, + .memoryTypeIndex = __builtin_ctz(req.memoryTypeBits), + }; + VkDeviceMemory mem; + CHECK(vkAllocateMemory(dev, &mai, NULL, &mem)); + CHECK(vkBindBufferMemory(dev, dst, mem, 0)); + + static const struct { + enum wait_kind kind; + const char *name; + unsigned patched; + } cases[] = { + {WAIT_EMPTY, "host event, no barriers", 0}, + {WAIT_NONE_STAGE, "host event, NONE srcStageMask", 0}, + {WAIT_HOST_STAGE, "host event, HOST in srcStageMask", 0}, + {WAIT_DEVICE_SET, "event set by vkCmdSetEvent2", 1}, + }; + int fails = 0; + for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { + const unsigned patched = run_case(cases[i].kind); + const bool ok = patched == cases[i].patched; + printf("%s: %s: patched=%d, want %u\n", ok ? "ok" : "FAIL", + cases[i].name, (int)patched, cases[i].patched); + fails += !ok; + } + + vkDestroyBuffer(dev, dst, NULL); + vkFreeMemory(dev, mem, NULL); + vkDestroyEvent(dev, event, NULL); + vkDestroyCommandPool(dev, pool, NULL); + vkDestroyDevice(dev, NULL); + vkDestroyInstance(inst, NULL); + return fails ? 1 : 0; +} diff --git a/src/asahi/vulkan/tests/test-hk-execution-allowance.c b/src/asahi/vulkan/tests/test-hk-execution-allowance.c index 47687a60d4e..05998121bdc 100644 --- a/src/asahi/vulkan/tests/test-hk-execution-allowance.c +++ b/src/asahi/vulkan/tests/test-hk-execution-allowance.c @@ -20,15 +20,8 @@ hk_cmd_buffer_emit_cs_jump(struct hk_cmd_buffer *cmd, void *current, return (uint8_t *)current + AGX_CDM_STREAM_LINK_LENGTH; } -/* The streams here record no alias sites (hk_cs::alias_sites stays zero), so - * the merge never moves any. */ -void -hk_cmd_buffer_move_alias_sites(struct hk_cmd_buffer *cmd, const void *from, - const void *from_end, uint8_t *to, size_t skip) -{ - assert(!"no alias sites are recorded"); -} - +/* The streams here record no sparse sites (hk_cs::sparse_sites stays zero), + * so the merge never moves any. */ void hk_cmd_buffer_move_sparse_sites(struct hk_cmd_buffer *cmd, const void *from, const void *from_end, uint8_t *to, size_t skip) @@ -119,6 +112,52 @@ init_opener(struct hk_cs *cs, uint8_t *bytes, size_t size, bool memory, } } +/* Patch sites and the pin move with the words an inline merge copies; a site + * on b's folded opening barrier moves to a's trailing record, and a pin on + * a's tail stays where b adds nothing after it. */ +static void +check_merge_alias_sites(void) +{ + static struct hk_cmd_buffer cmd; + uint8_t a_bytes[HK_CS_SPACE_PADDING + 256], b_bytes[64]; + struct hk_cs a, b; + uint8_t *a_tail = a_bytes + LAUNCH_BYTES; + + init_tail(&a, a_bytes, sizeof(a_bytes), false); + init_opener(&b, b_bytes, sizeof(b_bytes), false, true); + agx3_cmdstream_store_le32(b_bytes + AGX3_CDM_BARRIER_SIZE + LAUNCH_BYTES, + plain_flush); + b.current = b_bytes + 2 * AGX3_CDM_BARRIER_SIZE + LAUNCH_BYTES; + b.strong_flush_end = b.alias_pin = b.current; + a.cmd = b.cmd = &cmd; + b.alias_sites = 2; + cmd.alias_sites = UTIL_DYNARRAY_INIT; + const struct hk_alias_site opener = {(uint32_t *)b_bytes, memory_flush}; + const struct hk_alias_site flush = { + (uint32_t *)(b_bytes + AGX3_CDM_BARRIER_SIZE + LAUNCH_BYTES), + memory_flush}; + util_dynarray_append(&cmd.alias_sites, opener); + util_dynarray_append(&cmd.alias_sites, flush); + hk_cs_merge_cdm(&a, &b); + + const struct hk_alias_site *sites = cmd.alias_sites.data; + assert((uint8_t *)sites[0].word == a_tail); + assert((uint8_t *)sites[1].word == + a_tail + AGX3_CDM_BARRIER_SIZE + LAUNCH_BYTES); + assert(word_at(sites[1].word) == plain_flush); + assert((uint8_t *)a.alias_pin == + (uint8_t *)sites[1].word + AGX3_CDM_BARRIER_SIZE); + assert(a.alias_sites == 2); + util_dynarray_fini(&cmd.alias_sites); + + init_tail(&a, a_bytes, sizeof(a_bytes), false); + init_opener(&b, b_bytes, sizeof(b_bytes), false, false); + a.cmd = b.cmd = &cmd; + a.alias_pin = a.current; + hk_cs_merge_cdm(&a, &b); + assert(a.alias_pin == a.current); +} + static void check_merge_memory_scope(void) { @@ -298,5 +337,6 @@ int main(void) for (unsigned link = 0; link < 2; ++link) check_merge(a, b, link); check_merge_memory_scope(); + check_merge_alias_sites(); return 0; } diff --git a/src/asahi/vulkan/tests/test-hk-memory-oom.c b/src/asahi/vulkan/tests/test-hk-memory-oom.c index 1cbe0effedc..21e07e50f55 100644 --- a/src/asahi/vulkan/tests/test-hk-memory-oom.c +++ b/src/asahi/vulkan/tests/test-hk-memory-oom.c @@ -79,15 +79,17 @@ __wrap_os_get_total_physical_memory(uint64_t *size) * reference counting, VA release and WSI object cleanup use production code. */ static struct agx_bo * mock_bo_alloc(struct agx_device *dev, size_t size, size_t align, - enum agx_bo_flags flags) + enum agx_bo_flags flags, struct agx_va *va) { ++bo_calls; - if (limited && size > limit - host - pinned - cached) { - ++kernel_oom_attempts; + /* bo_alloc takes over a caller's VA and frees it when it fails. */ + if ((limited && size > limit - host - pinned - cached) || bo_fails) { + if (limited && size > limit - host - pinned - cached) + ++kernel_oom_attempts; + if (va) + agx_va_free(dev, va, false); return NULL; } - if (bo_fails) - return NULL; struct agx_bo *bo = calloc(1, sizeof(*bo)); assert(bo); unsigned handle; @@ -102,7 +104,7 @@ mock_bo_alloc(struct agx_device *dev, size_t size, size_t align, bo->size = size; bo->align = align; bo->flags = flags; - bo->va = agx_va_alloc(dev, size, align, 0, 0); + bo->va = va ?: agx_va_alloc(dev, size, align, 0, 0); assert(bo->va); pinned += size; if (pinned > peak) @@ -162,7 +164,7 @@ struct agx_bo * __wrap_agx_bo_import(struct agx_device *dev, int fd) { struct agx_bo *bo = mock_bo_alloc(dev, import_size, AIL_PAGESIZE, - AGX_BO_SHARED | AGX_BO_SHAREABLE); + AGX_BO_SHARED | AGX_BO_SHAREABLE, NULL); if (bo) p_atomic_set(&bo->refcnt, 1); return bo; @@ -175,7 +177,7 @@ __wrap_agx_bo_import_host_ptr(struct agx_device *dev, void *ptr, size_t size, enum agx_bo_flags flags = AGX_BO_HOST_PTR | AGX_BO_WRITEBACK | AGX_BO_NO_CACHE; if (access == AGX_HOST_PTR_READ_ONLY) flags |= AGX_BO_READONLY; - struct agx_bo *bo = mock_bo_alloc(dev, size, AIL_PAGESIZE, flags); + struct agx_bo *bo = mock_bo_alloc(dev, size, AIL_PAGESIZE, flags, NULL); if (bo) { bo->_map = ptr; bo->_map_size = size; @@ -369,6 +371,23 @@ alloc(struct hk_device *dev, uint64_t size, VkDeviceMemory *memory) return alloc_type(dev, size, 0, memory); } +static VkResult +alloc_capture_replay(struct hk_device *dev, uint64_t size, + VkDeviceMemory *memory) +{ + const VkMemoryAllocateFlagsInfo flags = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO, + .flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT | + VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT, + }; + const VkMemoryAllocateInfo info = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .pNext = &flags, + .allocationSize = size, + }; + return hk_AllocateMemory(hk_device_to_handle(dev), &info, &allocator, memory); +} + static VkResult alloc_host(struct hk_device *dev, void *ptr, uint64_t size, VkDeviceMemory *memory) @@ -667,6 +686,27 @@ main(void) hk_FreeMemory(hk_device_to_handle(dev), memory, &allocator); agx_bo_cache_evict_all(&dev->dev); + /* Capture/replay memory never comes from the cache and never goes back + * to it, but a refusal still evicts the cache and asks once more. */ + fake_available = 64 * 1024 * MIB; + assert(alloc(dev, 128 * MIB, &memory) == VK_SUCCESS); + hk_FreeMemory(hk_device_to_handle(dev), memory, &allocator); + assert(dev->dev.bo_cache.size == 128 * MIB); + fake_available = 512 * MIB + 1024 * MIB - 1; + free_returns_available = true; + meminfo_reads = 0; + before = bo_calls; + assert(alloc_capture_replay(dev, 512 * MIB, &memory) == VK_SUCCESS); + assert(meminfo_reads == 2 && bo_calls == before + 1); + assert(!dev->dev.bo_cache.size && pinned == 512 * MIB); + free_returns_available = false; + hk_FreeMemory(hk_device_to_handle(dev), memory, &allocator); + assert(!dev->dev.bo_cache.size && !pinned); + /* Without anything to evict, it is refused. */ + fake_available = 1024 * MIB; + assert(alloc_capture_replay(dev, 512 * MIB, &memory) == + VK_ERROR_OUT_OF_DEVICE_MEMORY); + /* Warm BO-cache reuse across 4 KiB..64 MiB must issue zero VM_BINDs, * isolate memory types by BO flags, retry after a bind failure, and reset * cleanly on cache eviction. */ @@ -824,7 +864,7 @@ main(void) vk_object_base_finish(&cmd.vk.base); /* Host-pointer imports track aliasing per import via /proc/self/maps and - * range overlap on non-M5 chips and on M5 with host_alias.dynamic, while + * range overlap on every chip but M5 under its static rule, while * keeping va_alias_resources == 0 for disjoint private anonymous mappings. */ const size_t host_sz = 65536; void *priv_a = mmap(NULL, host_sz * 2, PROT_READ | PROT_WRITE, @@ -833,9 +873,10 @@ main(void) MAP_SHARED | MAP_ANONYMOUS, -1, 0); assert(priv_a != MAP_FAILED && shared_a != MAP_FAILED); - /* G17P: disjoint private-anonymous imports do not raise va_alias_resources; - * overlapping or shared-mapping imports do, and freeing restores 0. */ - dev->dev.chip = AGX_CHIP_G17P; + /* Without VA-tagged caches (G13G): disjoint private-anonymous imports do + * not raise va_alias_resources, overlapping or shared-mapping imports do, + * and freeing restores 0. Barriers never take memory scope there. */ + dev->dev.chip = AGX_CHIP_G13G; dev->host_alias.dynamic = false; dev->may_alias_memory = false; assert(!dev->va_alias_resources && !dev->alias_live && @@ -844,45 +885,49 @@ main(void) VkDeviceMemory hm0 = 0, hm1 = 0, hm_ov = 0, hm_sh = 0; passing = 32; assert(alloc_host(dev, priv_a, host_sz, &hm0) == VK_SUCCESS); - assert(dev->va_alias_resources == 0 && !hk_cdm_alias_scope(dev)); + assert(dev->va_alias_resources == 0); assert(alloc_host(dev, (char *)priv_a + host_sz, host_sz, &hm1) == VK_SUCCESS); - assert(dev->va_alias_resources == 0 && !hk_cdm_alias_scope(dev)); + assert(dev->va_alias_resources == 0); assert(alloc_host(dev, priv_a, host_sz, &hm_ov) == VK_SUCCESS); - assert(dev->va_alias_resources == 2 && hk_cdm_alias_scope(dev)); + assert(dev->va_alias_resources == 2 && !hk_cdm_alias_scope(dev)); hk_FreeMemory(hk_device_to_handle(dev), hm_ov, &allocator); - assert(dev->va_alias_resources == 0 && !hk_cdm_alias_scope(dev)); + assert(dev->va_alias_resources == 0); assert(alloc_host(dev, shared_a, host_sz, &hm_sh) == VK_SUCCESS); - assert(dev->va_alias_resources == 1 && hk_cdm_alias_scope(dev)); + assert(dev->va_alias_resources == 1 && !hk_cdm_alias_scope(dev)); hk_FreeMemory(hk_device_to_handle(dev), hm_sh, &allocator); - assert(dev->va_alias_resources == 0 && !hk_cdm_alias_scope(dev)); + assert(dev->va_alias_resources == 0); hk_FreeMemory(hk_device_to_handle(dev), hm1, &allocator); hk_FreeMemory(hk_device_to_handle(dev), hm0, &allocator); - assert(dev->va_alias_resources == 0 && !pinned && !host_live); - - /* M5 (G17C) with host_alias.dynamic: va_alias_resources and alias_live stay - * 0 for disjoint private-anonymous imports and track overlaps/shared maps - * together. */ - dev->dev.chip = AGX_CHIP_G17C; - dev->host_alias.dynamic = true; - dev->may_alias_memory = false; - passing = 32; - assert(alloc_host(dev, priv_a, host_sz, &hm0) == VK_SUCCESS); - assert(dev->va_alias_resources == 0 && dev->alias_live == 0 && - !hk_cdm_alias_scope(dev)); - assert(alloc_host(dev, priv_a, host_sz, &hm_ov) == VK_SUCCESS); - assert(dev->va_alias_resources == 2 && dev->alias_live == 2 && - hk_cdm_alias_scope(dev)); - hk_FreeMemory(hk_device_to_handle(dev), hm_ov, &allocator); - assert(dev->va_alias_resources == 0 && dev->alias_live == 0 && - !hk_cdm_alias_scope(dev)); - assert(alloc_host(dev, shared_a, host_sz, &hm_sh) == VK_SUCCESS); - assert(dev->va_alias_resources == 1 && dev->alias_live == 1 && - hk_cdm_alias_scope(dev)); - hk_FreeMemory(hk_device_to_handle(dev), hm_sh, &allocator); - hk_FreeMemory(hk_device_to_handle(dev), hm0, &allocator); assert(dev->va_alias_resources == 0 && dev->alias_live == 0 && !pinned && !host_live); + /* G17P and M5 (G17C) with host_alias.dynamic: va_alias_resources and + * alias_live stay 0 for disjoint private-anonymous imports and track + * overlaps/shared maps together; barrier scope follows alias_live. */ + static const enum agx_chip tagged[] = {AGX_CHIP_G17P, AGX_CHIP_G17C}; + for (unsigned i = 0; i < ARRAY_SIZE(tagged); ++i) { + dev->dev.chip = tagged[i]; + dev->host_alias.dynamic = true; + dev->may_alias_memory = false; + passing = 32; + assert(alloc_host(dev, priv_a, host_sz, &hm0) == VK_SUCCESS); + assert(dev->va_alias_resources == 0 && dev->alias_live == 0 && + !hk_cdm_alias_scope(dev)); + assert(alloc_host(dev, priv_a, host_sz, &hm_ov) == VK_SUCCESS); + assert(dev->va_alias_resources == 2 && dev->alias_live == 2 && + hk_cdm_alias_scope(dev)); + hk_FreeMemory(hk_device_to_handle(dev), hm_ov, &allocator); + assert(dev->va_alias_resources == 0 && dev->alias_live == 0 && + !hk_cdm_alias_scope(dev)); + assert(alloc_host(dev, shared_a, host_sz, &hm_sh) == VK_SUCCESS); + assert(dev->va_alias_resources == 1 && dev->alias_live == 1 && + hk_cdm_alias_scope(dev)); + hk_FreeMemory(hk_device_to_handle(dev), hm_sh, &allocator); + hk_FreeMemory(hk_device_to_handle(dev), hm0, &allocator); + assert(dev->va_alias_resources == 0 && dev->alias_live == 0 && + !pinned && !host_live); + } + /* M5 static per-device fallback: writable host imports still count * unconditionally in va_alias_resources. */ dev->host_alias.dynamic = false; @@ -893,8 +938,9 @@ main(void) hk_FreeMemory(hk_device_to_handle(dev), hm0, &allocator); assert(dev->va_alias_resources == 0 && !pinned && !host_live); - /* Reset to G17P and verify fd imports still count unconditionally. */ + /* G17P with host_alias.dynamic: fd imports count unconditionally. */ dev->dev.chip = AGX_CHIP_G17P; + dev->host_alias.dynamic = true; dev->may_alias_memory = false; import_info.fd = open("/dev/null", O_RDONLY | O_CLOEXEC); assert(import_info.fd >= 0); @@ -902,9 +948,12 @@ main(void) passing = 16; assert(hk_AllocateMemory(hk_device_to_handle(dev), &import_alloc, &allocator, &memory) == VK_SUCCESS); - assert(dev->va_alias_resources == 1 && hk_cdm_alias_scope(dev)); + assert(dev->va_alias_resources == 1 && dev->alias_live == 1 && + hk_cdm_alias_scope(dev)); hk_FreeMemory(hk_device_to_handle(dev), memory, &allocator); - assert(dev->va_alias_resources == 0 && !hk_cdm_alias_scope(dev)); + assert(dev->va_alias_resources == 0 && dev->alias_live == 0 && + !hk_cdm_alias_scope(dev)); + dev->host_alias.dynamic = false; munmap(shared_a, host_sz); munmap(priv_a, host_sz * 2); diff --git a/src/asahi/vulkan/tests/test-hk-query-oq.c b/src/asahi/vulkan/tests/test-hk-query-oq.c index f1c637b40ad..c009b0fac8d 100644 --- a/src/asahi/vulkan/tests/test-hk-query-oq.c +++ b/src/asahi/vulkan/tests/test-hk-query-oq.c @@ -209,6 +209,14 @@ hk_cmd_alias_scope(struct hk_cmd_buffer *cmd) return false; } +/* No memory aliases here, so no barrier needs a patch site. */ +void +hk_cdm_note_alias_site(struct hk_cmd_buffer *cmd, struct hk_cs *cs) +{ + (void)cmd; + (void)cs; +} + bool hk_cdm_visibility_flush_needed(const struct hk_device *dev, const struct hk_cs *cs, bool memory) diff --git a/src/asahi/vulkan/tests/test-hk-tess-batch.c b/src/asahi/vulkan/tests/test-hk-tess-batch.c index 4fe0147a0d2..ec620f721c0 100644 --- a/src/asahi/vulkan/tests/test-hk-tess-batch.c +++ b/src/asahi/vulkan/tests/test-hk-tess-batch.c @@ -73,15 +73,8 @@ hk_cmd_buffer_emit_cs_jump(struct hk_cmd_buffer *cmd, void *current, return (uint8_t *)current + AGX_CDM_STREAM_LINK_LENGTH; } -/* The streams here record no alias sites (hk_cs::alias_sites stays zero), so - * the merge never moves any. */ -void -hk_cmd_buffer_move_alias_sites(struct hk_cmd_buffer *cmd, const void *from, - const void *from_end, uint8_t *to, size_t skip) -{ - assert(!"no alias sites are recorded"); -} - +/* The streams here record no sparse sites (hk_cs::sparse_sites stays zero), + * so the merge never moves any. */ void hk_cmd_buffer_move_sparse_sites(struct hk_cmd_buffer *cmd, const void *from, const void *from_end, uint8_t *to, size_t skip) @@ -414,6 +407,19 @@ main(void) stage_major(r, n, 0, 3, 0) && count(r, n, WEAK) == 0 && link_ok && pre->stream_linked); + /* A patch site at a stage's tail (hk_cdm_note_alias_site()) stays: the + * next draw's launch goes after it. */ + cmd = new_cmd(); + pre = open_pre_gfx(cmd); + record_draw(cmd, 0, 0); + struct hk_cs *pinned = cmd->tess_batch.stages[HK_TESS_STAGE_VS]; + pinned->alias_pin = pinned->current; + record_draw(cmd, 1, 0); + hk_tess_batch_flush(cmd); + n = decode(pre, r, MAX_RECORDS, &link_ok); + T("patch site: kept", n > 2 && r[0] == (LAUNCH | HK_TESS_STAGE_VS) && + r[1] == WEAK && count(r, n, WEAK) == 1); + /* Other pre-graphics work splits the batch: it follows the tessellation * of the draws before it and precedes that of the draws after it. */ cmd = new_cmd();