diff --git a/src/asahi/vulkan/hk_cmd_buffer.c b/src/asahi/vulkan/hk_cmd_buffer.c index ce3d623c4062..5889d80473ca 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.c +++ b/src/asahi/vulkan/hk_cmd_buffer.c @@ -6081,7 +6081,8 @@ hk_defer_clear_internal(struct hk_cmd_buffer *cmd, struct hk_image *image, VkImageAspectFlags aspects, const VkClearValue *value, uint32_t range_count, const VkImageSubresourceRange *ranges, bool from_gfx, - const struct hk_sync_dep *gfx_dep); + const struct hk_sync_dep *gfx_dep, + struct hk_cs *vdm_cs); static bool hk_deferred_clear_eligible(struct hk_cmd_buffer *cmd, @@ -6089,13 +6090,84 @@ hk_deferred_clear_eligible(struct hk_cmd_buffer *cmd, VkImageAspectFlags aspects, uint32_t range_count, const VkImageSubresourceRange *ranges); +static void +hk_init_empty_vdm_render_state(struct hk_cs *cs) +{ + /* Normally supplied by the first draw. A clear has no application + * sample-location state; use the attachment sample count and leave + * has_sample_locations uncommitted, just like other meta operations. + */ + cs->ppp_multisamplectl = + hk_default_sample_positions(MAX2(cs->tib.nr_samples, 1)); + + /* The render submission still binds these arrays even without PPP draw + * state referencing them. Give it an owned, neutral full-frame entry + * rather than a zero-byte upload. Render-area handling and empty-tile + * selection remain in the existing render setup. + */ + if (cs->scissor.size == 0) { + uint8_t *scissor = + util_dynarray_grow_bytes(&cs->scissor, 1, AGX_SCISSOR_LENGTH); + agx_pack(scissor, SCISSOR, cfg) { + cfg.min_x = cfg.min_y = 0; + cfg.max_x = cs->cr.width; + cfg.max_y = cs->cr.height; + cfg.min_z = 0.0f; + cfg.max_z = 1.0f; + } + } + + if (cs->depth_bias.size == 0) { + void *bias = util_dynarray_grow_bytes(&cs->depth_bias, 1, + AGX_DEPTH_BIAS_LENGTH); + memset(bias, 0, AGX_DEPTH_BIAS_LENGTH); + } +} + +static bool +hk_deferred_clear_vdm_in_use(const struct hk_cmd_buffer *cmd, + const struct hk_cs *vdm_cs) +{ + for (unsigned i = 0; i < cmd->state.deferred_clear_count; ++i) { + if (cmd->state.deferred_clear[i].vdm_cs == vdm_cs) + return true; + } + return false; +} + +static void +hk_drop_deferred_clear_vdm(struct hk_cmd_buffer *cmd, struct hk_cs *vdm_cs) +{ + if (!vdm_cs || hk_deferred_clear_vdm_in_use(cmd, vdm_cs)) + return; + + if (cmd->render_pack.head_stream == vdm_cs) { + cmd->render_pack.head = false; + cmd->render_pack.head_stream = NULL; + cmd->render_pack.head_why = "deferred clear"; + } + if (cmd->render_pack.tail_stream == vdm_cs) { + cmd->render_pack.tail_open = false; + cmd->render_pack.tail_stream = NULL; + } + if (cmd->pass_merge.cs == vdm_cs) { + cmd->pass_merge.valid = false; + cmd->pass_merge.cs = NULL; + } + + hk_sync_forget_stream(cmd, vdm_cs); + list_del(&vdm_cs->node); + hk_cs_destroy(vdm_cs); +} + /* Try to fold an empty clear-only render pass into deferred whole-image clears * so a following pass that loads (or re-clears) the attachment absorbs the * clear directly as a tilebuffer load-op clear without an extra compute fill * job or memory round-trip. Only folds when every cleared attachment in the * pass is eligible; otherwise returns false so the caller emits compute fills. */ static bool -hk_try_defer_empty_render_clears(struct hk_cmd_buffer *cmd, struct hk_cs *cs) +hk_try_defer_empty_render_clears(struct hk_cmd_buffer *cmd, struct hk_cs *cs, + bool keep_vdm) { struct hk_rendering_state *render = &cmd->state.gfx.render; struct hk_device *dev = hk_cmd_buffer_device(cmd); @@ -6238,7 +6310,7 @@ hk_try_defer_empty_render_clears(struct hk_cmd_buffer *cmd, struct hk_cs *cs) }; bool ok = hk_defer_clear_internal(cmd, cands[i].image, cands[i].aspects, &cands[i].value, 1, &range, true, - saved_dep); + saved_dep, keep_vdm ? cs : NULL); assert(ok); } @@ -6306,36 +6378,7 @@ hk_optimize_empty_vdm(struct hk_cmd_buffer *cmd) (continuation || (cs->cr.process_empty_tiles && (m5_ms_clear || !agx_get_precompiled(&dev->bg_eot, LIBAGX_FILL_UINT4)))))) { - /* Normally supplied by the first draw. A clear has no application - * sample-location state; use the attachment sample count and leave - * has_sample_locations uncommitted, just like other meta operations. - */ - cs->ppp_multisamplectl = - hk_default_sample_positions(MAX2(cs->tib.nr_samples, 1)); - - /* The render submission still binds these arrays even without PPP draw - * state referencing them. Give it an owned, neutral full-frame entry - * rather than a zero-byte upload. Render-area handling and empty-tile - * selection remain in the existing render setup. - */ - if (cs->scissor.size == 0) { - uint8_t *scissor = - util_dynarray_grow_bytes(&cs->scissor, 1, AGX_SCISSOR_LENGTH); - agx_pack(scissor, SCISSOR, cfg) { - cfg.min_x = cfg.min_y = 0; - cfg.max_x = cs->cr.width; - cfg.max_y = cs->cr.height; - cfg.min_z = 0.0f; - cfg.max_z = 1.0f; - } - } - - if (cs->depth_bias.size == 0) { - void *bias = util_dynarray_grow_bytes(&cs->depth_bias, 1, - AGX_DEPTH_BIAS_LENGTH); - memset(bias, 0, AGX_DEPTH_BIAS_LENGTH); - } - + hk_init_empty_vdm_render_state(cs); return false; } @@ -6345,8 +6388,19 @@ hk_optimize_empty_vdm(struct hk_cmd_buffer *cmd) /* Fold whole-image single-sample clear-only render passes into deferred * clears so a following pass that loads (or re-clears) the attachment - * absorbs the clear directly without an extra compute fill kick. */ - if (hk_try_defer_empty_render_clears(cmd, cs)) { + * absorbs the clear directly without an extra kick. On native AGX3 / M5, + * keep the finalized VDM render stream alive while deferred: if absorbed, + * hk_take_deferred_clears() drops it; if a later pass with different + * attachments flushes it, the clear stays on VDM instead of a compute fill + * between two renders (VDM->CDM->VDM); any other flush (for example + * vkEndCommandBuffer()) drops it and fills the image as before. */ + const bool keep_vdm = + native_eot && cs->cr.process_empty_tiles && cs->g17p_pass_ends; + if (hk_try_defer_empty_render_clears(cmd, cs, keep_vdm)) { + if (keep_vdm) { + hk_init_empty_vdm_render_state(cs); + return false; + } hk_sync_forget_stream(cmd, cs); list_del(&cs->node); hk_cs_destroy(cs); @@ -6462,7 +6516,8 @@ hk_defer_clear_internal(struct hk_cmd_buffer *cmd, struct hk_image *image, VkImageAspectFlags aspects, const VkClearValue *value, uint32_t range_count, const VkImageSubresourceRange *ranges, bool from_gfx, - const struct hk_sync_dep *gfx_dep) + const struct hk_sync_dep *gfx_dep, + struct hk_cs *vdm_cs) { if (!hk_deferred_clear_eligible(cmd, image, aspects, range_count, ranges)) return false; @@ -6477,13 +6532,17 @@ hk_defer_clear_internal(struct hk_cmd_buffer *cmd, struct hk_image *image, for (unsigned i = 0; i < cmd->state.deferred_clear_count; ++i) { struct hk_deferred_clear *e = &cmd->state.deferred_clear[i]; if (e->image == image) { + struct hk_cs *old_vdm = e->vdm_cs; e->aspects = aspects; e->value = *value; e->from_gfx = from_gfx; + e->vdm_cs = vdm_cs; if (from_gfx && gfx_dep) memcpy(e->gfx_dep, gfx_dep, sizeof(e->gfx_dep)); else memset(e->gfx_dep, 0, sizeof(e->gfx_dep)); + if (old_vdm && old_vdm != vdm_cs) + hk_drop_deferred_clear_vdm(cmd, old_vdm); return true; } } @@ -6498,6 +6557,7 @@ hk_defer_clear_internal(struct hk_cmd_buffer *cmd, struct hk_image *image, .aspects = aspects, .value = *value, .from_gfx = from_gfx, + .vdm_cs = vdm_cs, }; if (from_gfx && gfx_dep) memcpy(slot->gfx_dep, gfx_dep, sizeof(slot->gfx_dep)); @@ -6510,7 +6570,7 @@ hk_defer_clear(struct hk_cmd_buffer *cmd, struct hk_image *image, uint32_t range_count, const VkImageSubresourceRange *ranges) { return hk_defer_clear_internal(cmd, image, aspects, value, range_count, - ranges, false, NULL); + ranges, false, NULL, NULL); } /* A buffer command (copy, fill, update) cannot access an image except @@ -6556,8 +6616,33 @@ hk_deferred_clears_hold_buffer(struct hk_cmd_buffer *cmd, uint64_t addr, cmd->state.deferred_clear_hold = true; } -void -hk_flush_deferred_clears_why(struct hk_cmd_buffer *cmd, const char *why) +/* Unless a render pass follows, turn the kept clear-only VDM renders back + * into deferred fills: a clear-only command buffer, or a clear that compute, + * a transfer or a barrier flushes, is cheaper as a compute fill than as a + * render of its own. Every entry of a dropped render is filled. */ +static void +hk_unkeep_deferred_clear_vdms(struct hk_cmd_buffer *cmd) +{ + struct hk_cs *vdm[HK_MAX_DEFERRED_CLEARS]; + unsigned vdm_count = 0; + for (unsigned i = 0; i < cmd->state.deferred_clear_count; ++i) { + struct hk_deferred_clear *e = &cmd->state.deferred_clear[i]; + if (!e->vdm_cs) + continue; + bool seen = false; + for (unsigned k = 0; k < vdm_count; ++k) + seen |= vdm[k] == e->vdm_cs; + if (!seen) + vdm[vdm_count++] = e->vdm_cs; + e->vdm_cs = NULL; + } + for (unsigned i = 0; i < vdm_count; ++i) + hk_drop_deferred_clear_vdm(cmd, vdm[i]); +} + +static void +hk_flush_deferred_clears_impl(struct hk_cmd_buffer *cmd, const char *why, + bool render_follows) { /* Whatever observes the deferred clears may also read a pending store. */ cmd->state.pending_z_store.cs = NULL; @@ -6565,11 +6650,30 @@ hk_flush_deferred_clears_why(struct hk_cmd_buffer *cmd, const char *why) if (!cmd->state.deferred_clear_count || cmd->state.deferred_clear_flushing) return; + if (!render_follows) + hk_unkeep_deferred_clear_vdms(cmd); + struct hk_device *dev = hk_cmd_buffer_device(cmd); cmd->state.deferred_clear_flushing = true; bool any_xfer = false, any_gfx = false; for (unsigned i = 0; i < cmd->state.deferred_clear_count; ++i) { const struct hk_deferred_clear *e = &cmd->state.deferred_clear[i]; + if (e->vdm_cs) { + /* The empty clear-only render stream is already in control_streams + * at its recorded VDM position with its dependencies and footprints + * intact; the render pass that follows would otherwise wait on a + * compute fill between two renders (VDM->CDM->VDM). */ + if (unlikely(dev->frame_anatomy)) { + hk_frame_anatomy_note( + dev, + "deferred-clear kept vdm by %s: %ux%u %s aspects=0x%x compressed=%u", + why, e->image->vk.extent.width, e->image->vk.extent.height, + util_format_short_name( + hk_format_to_pipe_format(e->image->vk.format)), + e->aspects, e->image->planes[0].layout.compressed); + } + continue; + } if (unlikely(dev->frame_anatomy)) { hk_frame_anatomy_note( dev, @@ -6615,21 +6719,35 @@ hk_flush_deferred_clears_why(struct hk_cmd_buffer *cmd, const char *why) /* The barriers that followed the clears were recorded before the fills; * order everything recorded from now on after them, as * hk_CmdPipelineBarrier2() does outside a render pass. */ - struct hk_cs *cs = cmd->current_cs.cs; - const bool alias_scope = hk_cmd_alias_scope(cmd); - if (cs && hk_cdm_visibility_flush_needed(dev, cs, alias_scope)) { - hk_ensure_cs_has_space(cmd, cs, AGX3_CDM_BARRIER_SIZE); - hk_cdm_visibility_flush(dev, cs, alias_scope); + if (any_xfer || any_gfx) { + struct hk_cs *cs = cmd->current_cs.cs; + const bool alias_scope = hk_cmd_alias_scope(cmd); + if (cs && hk_cdm_visibility_flush_needed(dev, cs, alias_scope)) { + hk_ensure_cs_has_space(cmd, cs, AGX3_CDM_BARRIER_SIZE); + hk_cdm_visibility_flush(dev, cs, alias_scope); + } + if (cs && !alias_scope) + hk_cdm_note_alias_site(cmd, cs); + if (any_xfer) + hk_sync_barrier(cmd, HK_SYNC_XFER, HK_SYNC_ALL); + if (any_gfx) + hk_sync_barrier(cmd, HK_SYNC_GFX, HK_SYNC_ALL); } - if (cs && !alias_scope) - hk_cdm_note_alias_site(cmd, cs); - if (any_xfer) - hk_sync_barrier(cmd, HK_SYNC_XFER, HK_SYNC_ALL); - if (any_gfx) - hk_sync_barrier(cmd, HK_SYNC_GFX, HK_SYNC_ALL); cmd->state.deferred_clear_flushing = false; } +void +hk_flush_deferred_clears_why(struct hk_cmd_buffer *cmd, const char *why) +{ + hk_flush_deferred_clears_impl(cmd, why, false); +} + +void +hk_flush_deferred_clears_before_render(struct hk_cmd_buffer *cmd) +{ + hk_flush_deferred_clears_impl(cmd, "render pass", true); +} + /* How a pass may use a deferred clear for an attachment of the given aspect * in this layout. A load-op clear lands in the tilebuffer, not in memory. * @@ -6813,7 +6931,10 @@ hk_take_deferred_clears(struct hk_cmd_buffer *cmd, const VkRenderingInfo *info, } /* Drop the taken clears that need no fill; the caller records the rest - * before the pass. */ + * before the pass. Any clear-only VDM render whose attachments were all + * absorbed without needing a memory fill is dropped here as well. */ + struct hk_cs *dropped_vdm[HK_MAX_DEFERRED_CLEARS]; + unsigned dropped_vdm_count = 0; unsigned kept = 0; for (unsigned i = 0; i < cmd->state.deferred_clear_count; ++i) { struct hk_deferred_clear *e = &cmd->state.deferred_clear[i]; @@ -6823,9 +6944,18 @@ hk_take_deferred_clears(struct hk_cmd_buffer *cmd, const VkRenderingInfo *info, taken |= e == col[j]; fill |= e == col[j] && col_fill[j]; } - if (!taken || fill) + if (!taken || fill) { cmd->state.deferred_clear[kept++] = *e; + } else if (e->vdm_cs) { + bool seen = false; + for (unsigned k = 0; k < dropped_vdm_count; ++k) + seen |= dropped_vdm[k] == e->vdm_cs; + if (!seen) + dropped_vdm[dropped_vdm_count++] = e->vdm_cs; + } } cmd->state.deferred_clear_count = kept; + for (unsigned i = 0; i < dropped_vdm_count; ++i) + hk_drop_deferred_clear_vdm(cmd, dropped_vdm[i]); return copy; } diff --git a/src/asahi/vulkan/hk_cmd_buffer.h b/src/asahi/vulkan/hk_cmd_buffer.h index e91aea90f9ab..c1fc7673f6f4 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.h +++ b/src/asahi/vulkan/hk_cmd_buffer.h @@ -2505,6 +2505,13 @@ struct hk_cmd_buffer { VkImageAspectFlags aspects; VkClearValue value; bool from_gfx; + /* Finalized clear-only VDM stream kept alive when an empty clear + * pass is deferred on native AGX3 / M5. Dropped if every cleared + * attachment it owns is absorbed or superseded; left in + * control_streams when a later render pass flushes it, so the clear + * stays on VDM without a VDM->CDM->VDM crossing; dropped for a + * compute fill on any other flush. */ + struct hk_cs *vdm_cs; struct hk_sync_dep gfx_dep[HK_SYNC_NUM_SQ]; } deferred_clear[HK_MAX_DEFERRED_CLEARS]; uint8_t deferred_clear_count; @@ -3127,6 +3134,10 @@ uint64_t hk_default_sample_positions(unsigned nr_samples); void hk_flush_deferred_clears_why(struct hk_cmd_buffer *cmd, const char *why); #define hk_flush_deferred_clears(cmd) hk_flush_deferred_clears_why(cmd, __func__) +/* As hk_flush_deferred_clears(), for the deferred clears a render pass that + * is being recorded did not absorb. */ +void hk_flush_deferred_clears_before_render(struct hk_cmd_buffer *cmd); + /* Aspects of the depth/stencil image of the pending store are overwritten as a * whole before anything read them: drop those aspect stores (see * hk_take_pending_z_store()). */ diff --git a/src/asahi/vulkan/hk_cmd_draw.c b/src/asahi/vulkan/hk_cmd_draw.c index a0bb94efe7bb..7e686cc6b1ea 100644 --- a/src/asahi/vulkan/hk_cmd_draw.c +++ b/src/asahi/vulkan/hk_cmd_draw.c @@ -6704,7 +6704,7 @@ hk_CmdBeginRendering(VkCommandBuffer commandBuffer, VkRenderingAttachmentInfo color[HK_MAX_RTS], depth, stencil; const VkRenderingInfo *taken = hk_take_deferred_clears( cmd, pRenderingInfo, &info, color, &depth, &stencil); - hk_flush_deferred_clears(cmd); + hk_flush_deferred_clears_before_render(cmd); hk_begin_rendering(commandBuffer, taken); } diff --git a/src/asahi/vulkan/hk_sync.c b/src/asahi/vulkan/hk_sync.c index 52c605602ff1..87b5b444b72f 100644 --- a/src/asahi/vulkan/hk_sync.c +++ b/src/asahi/vulkan/hk_sync.c @@ -262,10 +262,14 @@ pass_footprints_commit(struct hk_cmd_buffer *cmd) static void records_replace_stream(struct hk_cmd_buffer *cmd, struct hk_cs *from, - struct hk_cs *to) + struct hk_cs *to, bool drop_singleton) { util_dynarray_foreach(&cmd->sync_pass_records, struct hk_sync_pass_record, rec) { + if (drop_singleton && rec->first == from && rec->last == from) { + rec->first = rec->last = NULL; + continue; + } if (rec->first == from) rec->first = to; if (rec->last == from) @@ -1849,7 +1853,7 @@ hk_sync_forget_stream(struct hk_cmd_buffer *cmd, struct hk_cs *cs) (!prev || prev->sync.seq < st->pass_tile_first->sync.seq)) { st->pass_tile_last = st->pass_tile_first; } - records_replace_stream(cmd, cs, prev); + records_replace_stream(cmd, cs, prev, true); list_for_each_entry(struct hk_cs, it, &cmd->control_streams, node) { replace_pointer(&it->sync.dep[sq].cut, cs, prev); @@ -1953,7 +1957,7 @@ hk_sync_merge(struct hk_cmd_buffer *cmd, struct hk_cs *into, replace_pointer(&st->pass_heap_last[sq], from, into); for (unsigned hs = 0; hs < HK_HEAP_SLOTS; ++hs) replace_pointer(&st->last_heap_slot[hs][sq], from, into); - records_replace_stream(cmd, from, into); + records_replace_stream(cmd, from, into, false); xfer_replace_stream(st, from, into); list_for_each_entry(struct hk_cs, it, &cmd->control_streams, node) { diff --git a/src/asahi/vulkan/tests/test-hk-sync-prior.c b/src/asahi/vulkan/tests/test-hk-sync-prior.c index 7c570a0a35fd..8f2b2400a041 100644 --- a/src/asahi/vulkan/tests/test-hk-sync-prior.c +++ b/src/asahi/vulkan/tests/test-hk-sync-prior.c @@ -3513,6 +3513,30 @@ main(void) p_atomic_read(&mq->drm.signalled_value) == 12); } test_vertex_image_bloom(); + /* Forgetting a singleton render pass (such as an absorbed deferred-clear + * VDM render) drops its pass footprint instead of rebinding it to an + * earlier unrelated render on the subqueue. */ + { + const void *img_a = (const void *)0x1111; + const void *img_b = (const void *)0x2222; + struct hk_cmd_buffer *cmd = new_cmd(); + struct hk_cs *pass_a = render_pass(cmd, img_a, 1, false); + struct hk_cs *clear_b = render_pass(cmd, img_b, 1, false); + hk_sync_forget_stream(cmd, clear_b); + list_del(&clear_b->node); + + hk_sync_begin_rendering(cmd, false); + hk_sync_pass_footprint(cmd, img_b, 0, 0, 1, 32, 32, + (VkRect2D){{0, 0}, {32, 32}}, false, true); + hk_sync_pass_footprints_done(cmd); + struct hk_cs *draw_b = add(cmd, false); + hk_sync_end_rendering(cmd, false); + + T("forgotten singleton clear leaves no false dependency on earlier pass", + draw_b->sync.dep[HK_SYNC_VDM].cut == NULL && + draw_b->sync.dep_ta[HK_SYNC_VDM].cut == NULL && + pass_a->sync.seq != 0); + } /* Retired exact prior pruning drops completed VDM/CDM ordinals while * preserving unretired dependencies and FRAGMENT_BARRIERS ta_split. */ {