From 7b8495eeee9633a22023580b6d9394b76f11e2e4 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 23 Oct 2026 15:36:24 +1100 Subject: [PATCH 1/2] hk: honour custom primitive restart indices on every draw path VK_EXT_primitive_restart_index lets vkCmdSetPrimitiveRestartIndexEXT pick any restart value, zero included. Several draw paths still used the all-ones value of the index type. - G15S, G15C and G17P direct indexed draws wrote 0xff, 0xffff or 0xffffffff into the restart state update in front of the draw. - Indirect indexed draws did the same through the HAL200 indirect record and through agx_vdm_draw() on the other Index List chips. Conditional rendering turns a direct draw into an indirect one, so it was hit there too. - The GPU-side robust index repair emitted the fixed value as well. - The AGX2 path cached the last emitted restart index and used zero to mean "nothing emitted yet", so a custom index of zero at the start of a stream was never emitted. Take the value from the dynamic state for application draws. Geometry shading and transform feedback keep the fixed value, since they draw an index buffer that poly generates. agx_vdm_draw() and the robust index kernels now take the restart index as an argument, and the gallium driver passes the GL restart index the same way. The AGX2 cache gets an explicit flag instead of the zero sentinel. vulkan-cts-1.4.7.0 added 3,242 primitive_restart_index cases. On G17P 120 of the 216 that fraction 0 of 16 runs failed with an image mismatch, all custom_index_{zero,one,max_minus_one} and conditional_rendering variants. They all pass now. G14 passed before and still does. Assisted-by: LLM Signed-off-by: Ryan Murray --- src/asahi/genxml/tests/test-agx3-packing.c | 58 ++++++++++++++----- src/asahi/libagx/draws.cl | 24 ++++---- src/asahi/libagx/libagx_dgc.h | 15 ++--- src/asahi/vulkan/hk_cmd_buffer.h | 6 +- src/asahi/vulkan/hk_cmd_draw.c | 45 +++++++++----- .../vulkan/tests/robust-index-exhaustion.py | 25 ++++++-- src/gallium/drivers/asahi/agx_state.c | 5 +- 7 files changed, 125 insertions(+), 53 deletions(-) diff --git a/src/asahi/genxml/tests/test-agx3-packing.c b/src/asahi/genxml/tests/test-agx3-packing.c index 59552a04b9a7..7c80741b5735 100644 --- a/src/asahi/genxml/tests/test-agx3-packing.c +++ b/src/asahi/genxml/tests/test-agx3-packing.c @@ -338,7 +338,7 @@ test_agx3_live_vdm_draw(void) true); CHECK_EQ(agx_vdm_draw_size(chips[i], nonindexed), hal300 ? 20 : 16); uint32_t *end = - agx_vdm_draw(packed, chips[i], nonindexed, (enum agx_primitive)6); + agx_vdm_draw(packed, chips[i], nonindexed, (enum agx_primitive)6, 0); CHECK_EQ((uintptr_t)end - (uintptr_t)packed, hal300 ? 20 : 16); ++checks; if (memcmp(packed, hal300 ? nonindexed_hal300 : nonindexed_g15, @@ -355,7 +355,8 @@ test_agx3_live_vdm_draw(void) CHECK_EQ(agx_vdm_draw_supported(chips[i], indexed, (enum agx_primitive)9), true); CHECK_EQ(agx_vdm_draw_size(chips[i], indexed), hal300 ? 32 : 28); - end = agx_vdm_draw(packed, chips[i], indexed, (enum agx_primitive)9); + end = agx_vdm_draw(packed, chips[i], indexed, (enum agx_primitive)9, + UINT32_MAX); CHECK_EQ((uintptr_t)end - (uintptr_t)packed, hal300 ? 32 : 28); ++checks; if (memcmp(packed, hal300 ? indexed_hal300 : indexed_g15, @@ -373,7 +374,7 @@ test_agx3_live_vdm_draw(void) CHECK_EQ(agx_vdm_draw_size(chips[i], indirect_nonindexed), hal300 ? 16 : 12); end = agx_vdm_draw(packed, chips[i], indirect_nonindexed, - (enum agx_primitive)6); + (enum agx_primitive)6, 0); CHECK_EQ((uintptr_t)end - (uintptr_t)packed, hal300 ? 16 : 12); ++checks; if (memcmp(packed, @@ -395,7 +396,7 @@ test_agx3_live_vdm_draw(void) CHECK_EQ(agx_vdm_draw_size(chips[i], indirect_indexed), hal300 ? 36 : 32); end = agx_vdm_draw(packed, chips[i], indirect_indexed, - (enum agx_primitive)9); + (enum agx_primitive)9, UINT32_MAX); CHECK_EQ((uintptr_t)end - (uintptr_t)packed, hal300 ? 36 : 32); ++checks; if (memcmp(packed, @@ -405,6 +406,21 @@ test_agx3_live_vdm_draw(void) fprintf(stderr, "AGX3 live indirect indexed VDM mismatch\n"); exit(1); } + + /* VK_EXT_primitive_restart_index: a custom index, zero included, + * replaces the type's all-ones value in the record's restart State + * Update and nothing else. */ + uint32_t custom[9] = {0}; + agx_vdm_draw(custom, chips[i], indirect_indexed, (enum agx_primitive)9, + 0); + CHECK_EQ(custom[0], UINT32_C(0x40000001)); + CHECK_EQ(custom[1], 0); + custom[1] = UINT32_MAX; + ++checks; + if (memcmp(custom, packed, hal300 ? 36 : 32)) { + fprintf(stderr, "AGX3 custom restart index changed other words\n"); + exit(1); + } } /* G17P's GPU-generated robust draw must match the complete native HAL200 @@ -434,8 +450,8 @@ test_agx3_live_vdm_draw(void) CHECK_EQ(libagx_draw_robust_index_vdm_size(AGX_CHIP_G17P, false), sizeof(robust_g17p)); uint32_t *robust_end = agx_vdm_draw_robust_index( - robust_packed, AGX_CHIP_G17P, robust_draw, (enum agx_primitive)9, 0, - false, 0); + robust_packed, AGX_CHIP_G17P, robust_draw, (enum agx_primitive)9, + UINT32_MAX, 0, false, 0); CHECK_EQ((uintptr_t)robust_end - (uintptr_t)robust_packed, sizeof(robust_g17p)); ++checks; @@ -449,7 +465,7 @@ test_agx3_live_vdm_draw(void) sizeof(robust_g17p_with_state)); robust_end = agx_vdm_draw_robust_index( robust_packed, AGX_CHIP_G17P, robust_draw, (enum agx_primitive)9, - UINT32_C(0x48000), true, 0); + UINT32_MAX, UINT32_C(0x48000), true, 0); CHECK_EQ((uintptr_t)robust_end - (uintptr_t)robust_packed, sizeof(robust_g17p_with_state)); ++checks; @@ -459,6 +475,20 @@ test_agx3_live_vdm_draw(void) exit(1); } + /* The repaired draw keeps the application's custom restart index. */ + memset(robust_packed, 0, sizeof(robust_packed)); + agx_vdm_draw_robust_index(robust_packed, AGX_CHIP_G17P, robust_draw, + (enum agx_primitive)9, 1, UINT32_C(0x48000), + true, 0); + CHECK_EQ(robust_packed[2], 1); + robust_packed[2] = UINT32_MAX; + ++checks; + if (memcmp(robust_packed, robust_g17p_with_state, + sizeof(robust_g17p_with_state))) { + fprintf(stderr, "G17P robust custom restart index mismatch\n"); + exit(1); + } + /* The HAL200 framing is G17P-only; M3's existing GPU-generated packet is * byte-for-byte unchanged even if the caller's state flag is set. */ memset(robust_packed, 0, sizeof(robust_packed)); @@ -466,7 +496,7 @@ test_agx3_live_vdm_draw(void) sizeof(indexed_g15)); robust_end = agx_vdm_draw_robust_index( robust_packed, AGX_CHIP_G15G, robust_draw, (enum agx_primitive)9, - UINT32_C(0x48000), true, 0); + UINT32_MAX, UINT32_C(0x48000), true, 0); CHECK_EQ((uintptr_t)robust_end - (uintptr_t)robust_packed, sizeof(indexed_g15)); ++checks; @@ -481,11 +511,11 @@ test_agx3_live_vdm_draw(void) agx2_draw.index_buffer = UINT64_C(0x3456789abc); uint32_t *agx2_expected_end = agx_vdm_draw(agx2_expected, AGX_CHIP_G13G, agx2_draw, - (enum agx_primitive)9); + (enum agx_primitive)9, UINT32_MAX); uint32_t *agx2_robust_end = agx_vdm_draw_robust_index(agx2_robust, AGX_CHIP_G13G, agx2_draw, - (enum agx_primitive)9, UINT32_C(0x48000), - true, 0); + (enum agx_primitive)9, UINT32_MAX, + UINT32_C(0x48000), true, 0); const size_t agx2_size = (uintptr_t)agx2_expected_end - (uintptr_t)agx2_expected; CHECK_EQ((uintptr_t)agx2_robust_end - (uintptr_t)agx2_robust, agx2_size); @@ -509,7 +539,7 @@ test_agx3_live_vdm_draw(void) memset(robust_packed, 0xcd, sizeof(robust_packed)); robust_end = agx_vdm_draw_robust_index( robust_packed, AGX_CHIP_G17P, empty_draw, - (enum agx_primitive)9, UINT32_C(0x48000), true, + (enum agx_primitive)9, UINT32_MAX, UINT32_C(0x48000), true, UINT64_C(0x5a12345678)); CHECK_EQ((uintptr_t)robust_end - (uintptr_t)robust_packed, sizeof(empty_jump_with_state)); @@ -526,7 +556,7 @@ test_agx3_live_vdm_draw(void) memset(robust_packed, 0xcd, sizeof(robust_packed)); robust_end = agx_vdm_draw_robust_index( robust_packed, AGX_CHIP_G17P, empty_draw, - (enum agx_primitive)9, UINT32_C(0x48000), true, + (enum agx_primitive)9, UINT32_MAX, UINT32_C(0x48000), true, UINT64_C(0x5a12345678)); CHECK_EQ((uintptr_t)robust_end - (uintptr_t)robust_packed, sizeof(empty_jump_with_state)); @@ -544,7 +574,7 @@ test_agx3_live_vdm_draw(void) memset(robust_packed, 0xcd, sizeof(robust_packed)); robust_end = agx_vdm_draw_robust_index( robust_packed, AGX_CHIP_G17P, empty_draw, - (enum agx_primitive)9, 0, false, UINT64_C(0x5a12345678)); + (enum agx_primitive)9, UINT32_MAX, 0, false, UINT64_C(0x5a12345678)); CHECK_EQ((uintptr_t)robust_end - (uintptr_t)robust_packed, sizeof(empty_jump_without_state)); ++checks; diff --git a/src/asahi/libagx/draws.cl b/src/asahi/libagx/draws.cl index 90b22849483a..139581200cc9 100644 --- a/src/asahi/libagx/draws.cl +++ b/src/asahi/libagx/draws.cl @@ -210,8 +210,9 @@ libagx_draw_robust_index_impl( global uint32_t *vdm, global struct poly_heap *heap, global uint32_t *printf_buffer, constant VkDrawIndexedIndirectCommand *cmd, uint64_t in_buf_ptr, - uint32_t in_buf_range_B, ushort restart, enum agx_chip chip, - enum agx_primitive topology, uint32_t draw_state_context_offset, + uint32_t in_buf_range_B, ushort restart, uint32_t restart_index, + enum agx_chip chip, enum agx_primitive topology, + uint32_t draw_state_context_offset, uint32_t render_state_emitted, uint64_t empty_draw_jump, enum agx_index_size index_size__3) { @@ -299,7 +300,7 @@ libagx_draw_robust_index_impl( } if (first) { - agx_vdm_draw_robust_index(vdm, chip, draw, topology, + agx_vdm_draw_robust_index(vdm, chip, draw, topology, restart_index, draw_state_context_offset, render_state_emitted != 0, empty_draw_jump); } @@ -311,17 +312,17 @@ libagx_draw_robust_index(global uint32_t *vdm, global struct poly_heap *heap, global uint32_t *printf_buffer, constant VkDrawIndexedIndirectCommand *cmd, uint64_t in_buf_ptr, uint32_t in_buf_range_B, - ushort restart, enum agx_chip chip, - enum agx_primitive topology, + ushort restart, uint32_t restart_index, + enum agx_chip chip, enum agx_primitive topology, uint32_t draw_state_context_offset, uint32_t render_state_emitted, uint64_t empty_draw_jump, enum agx_index_size index_size__3) { libagx_draw_robust_index_impl( - vdm, heap, printf_buffer, cmd, in_buf_ptr, in_buf_range_B, restart, chip, - topology, draw_state_context_offset, render_state_emitted, empty_draw_jump, - index_size__3); + vdm, heap, printf_buffer, cmd, in_buf_ptr, in_buf_range_B, restart, + restart_index, chip, topology, draw_state_context_offset, + render_state_emitted, empty_draw_jump, index_size__3); } KERNEL(32) @@ -329,15 +330,16 @@ libagx_draw_robust_index_batch(constant struct libagx_robust_index_draw *draws, global struct poly_heap *heap, global uint32_t *printf_buffer, uint64_t in_buf_ptr, uint32_t in_buf_range_B, - ushort restart, enum agx_chip chip, - enum agx_primitive topology, + ushort restart, uint32_t restart_index, + enum agx_chip chip, enum agx_primitive topology, enum agx_index_size index_size__3) { constant struct libagx_robust_index_draw *draw = draws + get_group_id(0); libagx_draw_robust_index_impl( (global uint32_t *)draw->vdm, heap, printf_buffer, (constant VkDrawIndexedIndirectCommand *)draw->command, in_buf_ptr, - in_buf_range_B, restart, chip, topology, draw->draw_state_offset, + in_buf_range_B, restart, restart_index, chip, topology, + draw->draw_state_offset, draw->render_state_emitted, draw->empty_draw_jump, index_size__3); } diff --git a/src/asahi/libagx/libagx_dgc.h b/src/asahi/libagx/libagx_dgc.h index d0a0a8453cd5..220402390052 100644 --- a/src/asahi/libagx/libagx_dgc.h +++ b/src/asahi/libagx/libagx_dgc.h @@ -345,9 +345,13 @@ agx_cdm_launch(GLOBAL uint32_t *out, enum agx_chip chip, struct agx_grid grid, return out; } +/* `restart_index` is the value an indexed draw's indices are compared with + * when draw.restart is set. Index List records that carry their own restart + * State Update write it there; older records take it from the separate + * VDM_STATE_RESTART_INDEX the caller emits. */ static inline GLOBAL uint32_t * agx_vdm_draw(GLOBAL uint32_t *out, enum agx_chip chip, struct agx_draw draw, - enum agx_primitive topology) + enum agx_primitive topology, uint32_t restart_index) { uint64_t ib = draw.indexed ? agx_draw_index_buffer(draw) : 0; @@ -362,11 +366,7 @@ agx_vdm_draw(GLOBAL uint32_t *out, enum agx_chip chip, struct agx_draw draw, size_valid && agx_vdm_index_list_indirect_pack_words( chip, (uint8_t)topology, draw.indexed, draw.restart, (uint8_t)draw.index_size, ib, - draw.b.ptr, - draw.indexed - ? agx_primitive_restart_index( - draw.index_size) - : 0, + draw.b.ptr, draw.indexed ? restart_index : 0, size_word, &words); assert(valid); if (!valid) @@ -874,6 +874,7 @@ static inline GLOBAL uint32_t * agx_vdm_draw_robust_index(GLOBAL uint32_t *out, enum agx_chip chip, struct agx_draw draw, enum agx_primitive topology, + uint32_t restart_index, uint32_t draw_state_context_offset, bool render_state_emitted, uint64_t empty_draw_jump) @@ -922,7 +923,7 @@ agx_vdm_draw_robust_index(GLOBAL uint32_t *out, enum agx_chip chip, *out++ = draw_state_context_offset; *out++ = 0x40000001u; - *out++ = agx_primitive_restart_index(draw.index_size); + *out++ = restart_index; } for (unsigned i = 0; i < words.count; ++i) diff --git a/src/asahi/vulkan/hk_cmd_buffer.h b/src/asahi/vulkan/hk_cmd_buffer.h index b7a348bae200..96f785b63ed6 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.h +++ b/src/asahi/vulkan/hk_cmd_buffer.h @@ -984,12 +984,14 @@ struct hk_cs { * attachment command must travel with the control stream it describes. */ struct hk_drm_attachment_set fragment_attachments; - /* Active restart index if one is set. Zero if there is no restart index set - * yet, since Vulkan does not allow zero restart indices (unlike OpenGL). + /* The restart index last emitted to this stream, valid once + * restart_index_set. VK_EXT_primitive_restart_index makes every value, + * zero included, a legal custom index, so no value can mean "unset". * This is used in place of dirty tracking, because dirty tracking * restart indices is complicated and just checking the saved value is cheap. */ uint32_t restart_index; + bool restart_index_set; }; static inline unsigned diff --git a/src/asahi/vulkan/hk_cmd_draw.c b/src/asahi/vulkan/hk_cmd_draw.c index 8f902779464c..1a89555aa281 100644 --- a/src/asahi/vulkan/hk_cmd_draw.c +++ b/src/asahi/vulkan/hk_cmd_draw.c @@ -6583,6 +6583,25 @@ translate_ppp_vertex(unsigned vtx) return vtx + 1; } +/* The value a draw's indices are compared with for primitive restart. An + * application draw restarts at the dynamic index, the custom one from + * VK_EXT_primitive_restart_index or the default of the bound index type. + * Geometry shading and transform feedback instead draw an index buffer that + * poly generates, which restarts at the all-ones value of its own index size; + * the same fixed value is kept for draws that do not restart. + */ +static uint32_t +hk_draw_restart_index(struct hk_cmd_buffer *cmd, struct agx_draw draw) +{ + if (!draw.indexed) + return 0; + + if (!draw.restart || cmd->state.gfx.shaders[MESA_SHADER_GEOMETRY]) + return agx_primitive_restart_index(draw.index_size); + + return cmd->vk.dynamic_graphics_state.ia.primitive_restart_index; +} + static void hk_flush_index(struct hk_cmd_buffer *cmd, struct hk_cs *cs) { @@ -6598,7 +6617,7 @@ hk_flush_index(struct hk_cmd_buffer *cmd, struct hk_cs *cs) /* VDM State updates are relatively expensive, so only emit them when the * restart index changes. This is simpler than accurate dirty tracking. */ - if (cs->restart_index != index) { + if (!cs->restart_index_set || cs->restart_index != index) { uint8_t *out = cs->current; agx_push(out, VDM_STATE, cfg) { cfg.restart_index_present = true; @@ -6610,6 +6629,7 @@ hk_flush_index(struct hk_cmd_buffer *cmd, struct hk_cs *cs) cs->current = out; cs->restart_index = index; + cs->restart_index_set = true; } } @@ -9803,12 +9823,8 @@ hk_g17p_emit_draw(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint8_t *out, draw.indexed ? agx_index_size_to_B(draw.index_size) : 0, .restart_enable = draw.restart, /* Geometry/XFB lowers a non-indexed API draw to a synthetic - * indexed draw. Derive the fixed Vulkan restart value from - * that draw, not from the original bound-index state. */ - .restart_index = draw.indexed - ? agx_primitive_restart_index( - draw.index_size) - : 0, + * indexed draw, which restarts at its own fixed value. */ + .restart_index = hk_draw_restart_index(cmd, draw), .index_buffer = draw.index_buffer, .index_buffer_range_B = draw.index_buffer_range_B, /* Unlike PPP and stream-link pointers, the Index List @@ -9861,9 +9877,7 @@ hk_g17p_emit_draw(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint8_t *out, const uint64_t ib = draw.index_buffer + offset_B; w[n++] = UINT32_C(0x40000001); - w[n++] = index_size_B == 1 ? UINT32_C(0xff) - : index_size_B == 2 ? UINT32_C(0xffff) - : UINT32_MAX; + w[n++] = hk_draw_restart_index(cmd, draw); w[n++] = UINT32_C(0x61f00000) | (uint32_t)((ib >> 32) & 0xff) | ((uint32_t)topology << 8) | ((uint32_t)draw.index_size << 17) | @@ -9967,6 +9981,7 @@ struct hk_robust_index_batch { uint32_t count; struct hk_cs *cs; struct agx_draw draw; + uint32_t restart_index; }; static void @@ -9984,6 +9999,7 @@ hk_flush_robust_index_batch(struct hk_cmd_buffer *cmd, dev->dev.printf_buffer_address, draw->index_buffer, draw->index_buffer_range_B, draw->restart, + batch->restart_index, agx_chip_record_family(dev->dev.chip), cmd->state.gfx.topology, draw->index_size); batch->cs->stats.robust_index_dispatches++; @@ -10032,6 +10048,7 @@ hk_emit_robust_index(struct hk_cmd_buffer *cmd, struct hk_cs *cs, .draw_state_offset = draw_state_offset, .render_state_emitted = tail_emitted, }; + const uint32_t restart_index = hk_draw_restart_index(cmd, draw); cs->stats.robust_index_draws++; if (batch) { assert(batch->count == 0 || batch->cs == cs); @@ -10039,11 +10056,12 @@ hk_emit_robust_index(struct hk_cmd_buffer *cmd, struct hk_cs *cs, job; batch->cs = cs; batch->draw = draw; + batch->restart_index = restart_index; } else { libagx_draw_robust_index( cmd, agx_1d(32), AGX_BARRIER_ALL | AGX_PREGFX, target, hk_heap(cmd), dev->dev.printf_buffer_address, draw.b.ptr, draw.index_buffer, - draw.index_buffer_range_B, draw.restart, + draw.index_buffer_range_B, draw.restart, restart_index, agx_chip_record_family(dev->dev.chip), topology, draw_state_offset, tail_emitted, empty_draw_jump, draw.index_size); cs->stats.robust_index_dispatches++; @@ -10427,8 +10445,9 @@ hk_draw_impl(struct hk_cmd_buffer *cmd, uint16_t draw_id, struct agx_draw draw_, g17p_native_bind_tail_emitted)) return; } else { - cs->current = (void *)agx_vdm_draw((uint32_t *)out, - dev->dev.chip, draw, topology); + cs->current = (void *)agx_vdm_draw( + (uint32_t *)out, dev->dev.chip, draw, topology, + hk_draw_restart_index(cmd, draw)); } } diff --git a/src/asahi/vulkan/tests/robust-index-exhaustion.py b/src/asahi/vulkan/tests/robust-index-exhaustion.py index ce6ad15fbb46..66876b092ba2 100644 --- a/src/asahi/vulkan/tests/robust-index-exhaustion.py +++ b/src/asahi/vulkan/tests/robust-index-exhaustion.py @@ -87,13 +87,23 @@ def source(path, revision=None): assert(from==0); if(lane==0) broadcast_value=x; return broadcast_value; } static void agx_vdm_draw_robust_index(uint32_t *vdm,enum agx_chip chip, - struct agx_draw draw,enum agx_primitive topology,uint state,bool emitted,uint64_t jump) { - (void)vdm;(void)chip;(void)topology; assert(state==12 && emitted && jump==64); + struct agx_draw draw,enum agx_primitive topology,uint32_t restart_index,uint state, + bool emitted,uint64_t jump) { + (void)vdm;(void)chip;(void)topology; assert(restart_index==0xffff); + assert(state==12 && emitted && jump==64); captured=draw; emissions++; } static struct agx_grid agx_3d(uint x,uint y,uint z) {return (struct agx_grid){{x,y,z}};} ''' +OLD_STUB = ( + "enum agx_primitive topology,uint32_t restart_index,uint state,\n" + " bool emitted,uint64_t jump) {\n" + " (void)vdm;(void)chip;(void)topology; assert(restart_index==0xffff);\n", + "enum agx_primitive topology,uint state,bool emitted,uint64_t jump) {\n" + " (void)vdm;(void)chip;(void)topology;\n", +) + TESTS = r''' static uint status_words[4]; static void repair(struct poly_heap *h, VkDrawIndexedIndirectCommand *cmd, @@ -102,8 +112,8 @@ def source(path, revision=None): .command=(uintptr_t)cmd,.empty_draw_jump=64,.draw_state_offset=12,.render_state_emitted=1}}; emissions=0; group=1; for(lane=0;lane<32;lane++) { - if(batch) libagx_draw_robust_index_batch(jobs,h,FAILURE_ARG (uintptr_t)in,range,true,chip,topology,width); - else libagx_draw_robust_index(NULL,h,FAILURE_ARG cmd,(uintptr_t)in,range,true,chip,topology,12,1,64,width); + if(batch) libagx_draw_robust_index_batch(jobs,h,FAILURE_ARG (uintptr_t)in,range,true,0xffff,chip,topology,width); + else libagx_draw_robust_index(NULL,h,FAILURE_ARG cmd,(uintptr_t)in,range,true,0xffff,chip,topology,12,1,64,width); } assert(emissions==1); } @@ -261,7 +271,12 @@ def kernels(revision=None, mutation=None): if mutation == "drop-points": code = code.replace("if (topology == AGX_PRIMITIVE_POINTS)", "if (false)") code += "#define FAILURE_ARG " + ("status_words," if "printf_buffer" in cl else "") + "\n" - return code + TESTS + tests = TESTS + if "uint32_t restart_index" not in cl: + # Kernels from before they took the custom primitive restart index. + code = code.replace(OLD_STUB[0], OLD_STUB[1]) + tests = tests.replace(",true,0xffff,", ",true,") + return code + tests API_PREFIX = r''' diff --git a/src/gallium/drivers/asahi/agx_state.c b/src/gallium/drivers/asahi/agx_state.c index d92cab01290a..98932b424473 100644 --- a/src/gallium/drivers/asahi/agx_state.c +++ b/src/gallium/drivers/asahi/agx_state.c @@ -8553,7 +8553,10 @@ agx_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info, dev->chip, out, &draw, primitive, info->restart_index, draw_state_offset); } else { - out = (void *)agx_vdm_draw((uint32_t *)out, dev->chip, draw, primitive); + out = (void *)agx_vdm_draw( + (uint32_t *)out, dev->chip, draw, primitive, + draw.restart ? info->restart_index + : agx_primitive_restart_index(draw.index_size)); } /* Barrier transform feedback writes on themselves for consistency. From b4050f56d2042ca14353f815082691962502fb53 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 23 Oct 2026 15:36:24 +1100 Subject: [PATCH 2/2] hk: report memory budget usage for the whole process VK_EXT_memory_budget defines heapUsage as an estimate of how much memory the process is using in the heap. Every VkInstance enumerates its own hk_physical_device, though, and each one counted only the allocations made through it. A second instance in the same process saw none of the first one's memory. Keep the usage and BO cache counters in one record per render node, shared by every physical device of that node in the process and reference counted by them. Allocation, free, the heap size check and the budget all read the shared counters. heapUsage also stops counting idle BOs in the BO cache. They are reclaimable, and an allocation that reuses one moved a BO from the cache counter to the used counter, so the reported usage did not change. The cache still lowers the budget. This fixes dEQP-VK.info.device_memory_budget_multi_instance, new in vulkan-cts-1.4.7.0 and on its mandatory list, which failed with "Memory heap usage not increased for heap 0": first because the second instance did not see the first one's allocation, then because the second allocation came out of the BO cache. Assisted-by: LLM Signed-off-by: Ryan Murray --- src/asahi/vulkan/hk_device.c | 2 +- src/asahi/vulkan/hk_device_memory.c | 6 +- src/asahi/vulkan/hk_physical_device.c | 75 +++++++++++++++++++-- src/asahi/vulkan/hk_physical_device.h | 19 +++++- src/asahi/vulkan/tests/test-hk-memory-oom.c | 6 +- 5 files changed, 96 insertions(+), 12 deletions(-) diff --git a/src/asahi/vulkan/hk_device.c b/src/asahi/vulkan/hk_device.c index 38a3b7180e78..6b23d70a7e44 100644 --- a/src/asahi/vulkan/hk_device.c +++ b/src/asahi/vulkan/hk_device.c @@ -850,7 +850,7 @@ hk_CreateDevice(VkPhysicalDevice physicalDevice, /* All BOs use the single UMA heap. Cached BOs remain pinned after * vkFreeMemory, so include their bytes across every logical device. */ assert(pdev->mem_heap_count == 1); - dev->dev.bo_cache.size_counter = &pdev->mem_heaps[0].cached; + dev->dev.bo_cache.size_counter = pdev->mem_heaps[0].cached; bool succ = agx_open_device(NULL, &dev->dev); drmFreeDevice(&drm_device); if (!succ) { diff --git a/src/asahi/vulkan/hk_device_memory.c b/src/asahi/vulkan/hk_device_memory.c index e6fe0f8e2d07..a8f70e251f26 100644 --- a/src/asahi/vulkan/hk_device_memory.c +++ b/src/asahi/vulkan/hk_device_memory.c @@ -193,7 +193,7 @@ hk_AllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo, const uint64_t aligned_size = align64(pAllocateInfo->allocationSize, alignment); - uint64_t used = p_atomic_read(&heap->used); + uint64_t used = p_atomic_read(heap->used); if (used > heap->size || aligned_size > heap->size - used) return vk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY); @@ -256,7 +256,7 @@ hk_AllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo, if (mem->bo->flags & (AGX_BO_SHAREABLE | AGX_BO_SHARED)) hk_add_ext_bo(dev, mem->bo); - uint64_t heap_used = p_atomic_add_return(&heap->used, mem->bo->size); + uint64_t heap_used = p_atomic_add_return(heap->used, mem->bo->size); if (heap_used > heap->size) { hk_FreeMemory(device, hk_device_memory_to_handle(mem), pAllocator); return vk_errorf(device, VK_ERROR_OUT_OF_DEVICE_MEMORY, @@ -299,7 +299,7 @@ hk_FreeMemory(VkDevice device, VkDeviceMemory _mem, const VkMemoryType *type = &pdev->mem_types[mem->vk.memory_type_index]; struct hk_memory_heap *heap = &pdev->mem_heaps[type->heapIndex]; - p_atomic_add(&heap->used, -((int64_t)mem->bo->size)); + p_atomic_add(heap->used, -((int64_t)mem->bo->size)); if (mem->bo->flags & (AGX_BO_SHAREABLE | AGX_BO_SHARED)) hk_remove_ext_bo(dev, mem->bo); diff --git a/src/asahi/vulkan/hk_physical_device.c b/src/asahi/vulkan/hk_physical_device.c index 56ca6f8ade35..a5c466673429 100644 --- a/src/asahi/vulkan/hk_physical_device.c +++ b/src/asahi/vulkan/hk_physical_device.c @@ -1712,6 +1712,57 @@ hk_get_sysmem_heap_size(struct hk_physical_device *pdev) &instance->drirc.misc.heap_memory_percent); } +static simple_mtx_t hk_heap_usage_lock = SIMPLE_MTX_INITIALIZER; +static struct hk_heap_usage *hk_heap_usages; + +/* Return the process-wide usage record for a render node, creating it on + * first use. It outlives any one instance, so it is not allocated with an + * instance's allocation callbacks. */ +static struct hk_heap_usage * +hk_heap_usage_get(dev_t render_dev) +{ + simple_mtx_lock(&hk_heap_usage_lock); + + struct hk_heap_usage *usage = hk_heap_usages; + while (usage && usage->render_dev != render_dev) + usage = usage->next; + + if (usage) { + usage->refcount++; + } else { + usage = calloc(1, sizeof(*usage)); + if (usage) { + usage->render_dev = render_dev; + usage->refcount = 1; + usage->next = hk_heap_usages; + hk_heap_usages = usage; + } + } + + simple_mtx_unlock(&hk_heap_usage_lock); + return usage; +} + +static void +hk_heap_usage_put(struct hk_heap_usage *usage) +{ + if (!usage) + return; + + simple_mtx_lock(&hk_heap_usage_lock); + + if (--usage->refcount == 0) { + struct hk_heap_usage **link = &hk_heap_usages; + while (*link != usage) + link = &(*link)->next; + + *link = usage->next; + free(usage); + } + + simple_mtx_unlock(&hk_heap_usage_lock); +} + static uint64_t hk_get_sysmem_heap_available(struct hk_physical_device *pdev) { @@ -1720,8 +1771,8 @@ hk_get_sysmem_heap_available(struct hk_physical_device *pdev) uint64_t total_used = 0; for (unsigned i = 0; i < pdev->mem_heap_count; i++) { const struct hk_memory_heap *heap = &pdev->mem_heaps[i]; - uint64_t used = p_atomic_read(&heap->used) + - p_atomic_read(&heap->cached.value); + uint64_t used = p_atomic_read(heap->used) + + p_atomic_read(&heap->cached->value); total_used += used; } @@ -1899,9 +1950,17 @@ hk_create_drm_physical_device(struct vk_instance *_instance, goto fail_disk_cache; } + pdev->heap_usage = hk_heap_usage_get(render_dev); + if (!pdev->heap_usage) { + result = vk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY); + goto fail_disk_cache; + } + uint32_t sysmem_heap_idx = pdev->mem_heap_count++; pdev->mem_heaps[sysmem_heap_idx] = (struct hk_memory_heap){ .size = sysmem_size_B, + .used = &pdev->heap_usage->used[sysmem_heap_idx], + .cached = &pdev->heap_usage->cached[sysmem_heap_idx], .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT, }; @@ -1993,6 +2052,7 @@ hk_create_drm_physical_device(struct vk_instance *_instance, return VK_SUCCESS; fail_disk_cache: + hk_heap_usage_put(pdev->heap_usage); hk_physical_device_free_disk_cache(pdev); vk_physical_device_finish(&pdev->vk); fail_agx_device: @@ -2019,6 +2079,7 @@ hk_physical_device_destroy(struct vk_physical_device *vk_pdev) close(pdev->master_fd); simple_mtx_destroy(&pdev->debug_compile_lock); + hk_heap_usage_put(pdev->heap_usage); hk_physical_device_free_disk_cache(pdev); agx_close_device(&pdev->dev); vk_physical_device_finish(&pdev->vk); @@ -2056,8 +2117,8 @@ hk_GetPhysicalDeviceMemoryProperties2( for (unsigned i = 0; i < pdev->mem_heap_count; i++) { const struct hk_memory_heap *heap = &pdev->mem_heaps[i]; - uint64_t used = p_atomic_read(&heap->used) + - p_atomic_read(&heap->cached.value); + uint64_t allocated = p_atomic_read(heap->used); + uint64_t used = allocated + p_atomic_read(&heap->cached->value); /* From the Vulkan 1.3.278 spec: * @@ -2066,9 +2127,13 @@ hk_GetPhysicalDeviceMemoryProperties2( * for each memory heap. A heap’s usage is an estimate of how * much memory the process is currently using in that heap." * + * Idle BOs in the BO cache are reclaimable, and an allocation + * that reuses one must still raise the reported usage, so they + * only lower the budget below. + * * TODO: Include internal allocations? */ - p->heapUsage[i] = used; + p->heapUsage[i] = allocated; /* Set the budget at 90% to avoid thrashing. */ float percent = 0.9f; diff --git a/src/asahi/vulkan/hk_physical_device.h b/src/asahi/vulkan/hk_physical_device.h index 6527c7984b5d..daad7d945ba0 100644 --- a/src/asahi/vulkan/hk_physical_device.h +++ b/src/asahi/vulkan/hk_physical_device.h @@ -31,10 +31,24 @@ struct hk_queue_family { uint32_t queue_count; }; +/* VK_EXT_memory_budget: "A heap's usage is an estimate of how much memory + * the process is currently using in that heap." Every VkInstance enumerates + * its own hk_physical_device, so the counters live in one record per render + * node, shared by every physical device of that node in the process. + */ +struct hk_heap_usage { + struct hk_heap_usage *next; + dev_t render_dev; + unsigned refcount; + uint64_t used[3]; + p_atomic_uint64_t cached[3]; +}; + struct hk_memory_heap { uint64_t size; - uint64_t used; - p_atomic_uint64_t cached; + /* Point into the physical device's hk_heap_usage. */ + uint64_t *used; + p_atomic_uint64_t *cached; VkMemoryHeapFlags flags; }; @@ -51,6 +65,7 @@ struct hk_physical_device { uint8_t device_uuid[VK_UUID_SIZE]; // TODO: add mapable VRAM heap if possible + struct hk_heap_usage *heap_usage; struct hk_memory_heap mem_heaps[3]; VkMemoryType mem_types[3]; uint8_t mem_heap_count; diff --git a/src/asahi/vulkan/tests/test-hk-memory-oom.c b/src/asahi/vulkan/tests/test-hk-memory-oom.c index e095b26e81d4..c23a0a2043c2 100644 --- a/src/asahi/vulkan/tests/test-hk-memory-oom.c +++ b/src/asahi/vulkan/tests/test-hk-memory-oom.c @@ -315,7 +315,7 @@ swapchain_attempt(struct hk_device *dev, uint64_t image_size) assert(!dev->dev.bo_cache.size); for (unsigned i = 0; i < exported_count; ++i) assert(fcntl(exported_fds[i], F_GETFD) == -1 && errno == EBADF); - assert(!hk_device_physical(dev)->mem_heaps[0].used); + assert(!*hk_device_physical(dev)->mem_heaps[0].used); assert(!kernel_oom_attempts); } @@ -333,8 +333,12 @@ main(void) vk_object_base_init(&dev->vk, &dev->vk.base, VK_OBJECT_TYPE_DEVICE); vk_object_base_init(NULL, &pdev->vk.base, VK_OBJECT_TYPE_PHYSICAL_DEVICE); dev->dev.chip = AGX_CHIP_G17P; + static struct hk_heap_usage heap_usage; + pdev->heap_usage = &heap_usage; pdev->mem_types[0].heapIndex = 0; pdev->mem_heaps[0].size = 8 * 1024 * MIB; + pdev->mem_heaps[0].used = &heap_usage.used[0]; + pdev->mem_heaps[0].cached = &heap_usage.cached[0]; simple_mtx_init(&dev->dev.bo_map_lock, mtx_plain); simple_mtx_init(&dev->dev.bo_cache.lock, mtx_plain); simple_mtx_init(&dev->dev.vma_lock, mtx_plain);