Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 138 additions & 50 deletions src/asahi/vulkan/hk_cmd_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -6081,21 +6081,93 @@ 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,
const struct hk_image *image,
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);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -6345,8 +6388,18 @@ 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 flushed unabsorbed (for example
* at vkEndCommandBuffer() or before a pass with different attachments),
* the clear stays on VDM without injecting a VDM->CDM->VDM round-trip. */
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);
Expand Down Expand Up @@ -6462,7 +6515,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;
Expand All @@ -6477,13 +6531,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;
}
}
Expand All @@ -6498,6 +6556,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));
Expand All @@ -6510,7 +6569,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
Expand Down Expand Up @@ -6570,6 +6629,21 @@ hk_flush_deferred_clears_why(struct hk_cmd_buffer *cmd, const char *why)
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; leaving it there avoids a VDM->CDM->VDM crossing. */
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,
Expand Down Expand Up @@ -6615,18 +6689,20 @@ 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;
}

Expand Down Expand Up @@ -6813,7 +6889,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];
Expand All @@ -6823,9 +6902,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;
}
6 changes: 6 additions & 0 deletions src/asahi/vulkan/hk_cmd_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2505,6 +2505,12 @@ 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; otherwise left in
* control_streams on flush so the clear stays on VDM without a
* VDM->CDM->VDM crossing. */
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;
Expand Down
10 changes: 7 additions & 3 deletions src/asahi/vulkan/hk_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions src/asahi/vulkan/tests/test-hk-sync-prior.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
{
Expand Down