From 042f087795656ab5d46002e6a6bc6d935bdc3291 Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Tue, 27 Oct 2026 10:25:18 -0300 Subject: [PATCH 01/19] asahi: read context priority from the context-creation flags agx_create_context tested its flags against PIPE_CONTEXT_PRIORITY_*, the screen's capability bits, not the PIPE_CONTEXT_*_PRIORITY creation flags. PIPE_CONTEXT_PREFER_THREADED (1 << 3), which the GL state tracker sets on nearly every context, is PIPE_CONTEXT_PRIORITY_REALTIME, so since "asahi: create HIGH-priority contexts as HIGH" every threaded GL context asked the kernel for HIGH priority. On bare metal the kernel refuses that without CAP_SYS_NICE and the context falls back to MEDIUM. Under virtio (muvm) the refused queue creation never completes: vdrm_host_sync() spins in sched_yield() forever, so every GL client in the guest (Wine's explorer.exe, glxinfo) hangs in context creation. Compute-only contexts also came out LOW, since PIPE_CONTEXT_COMPUTE_ONLY is PIPE_CONTEXT_PRIORITY_LOW. Signed-off-by: Jonathan Goyvaerts Assisted-by: LLM (cherry picked from commit b5ccd729a9049f8feab64361472345a58fbdc491) --- src/gallium/drivers/asahi/agx_pipe.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/gallium/drivers/asahi/agx_pipe.c b/src/gallium/drivers/asahi/agx_pipe.c index c769f062345d..826b81e5cdbf 100644 --- a/src/gallium/drivers/asahi/agx_pipe.c +++ b/src/gallium/drivers/asahi/agx_pipe.c @@ -1945,11 +1945,9 @@ agx_create_context(struct pipe_screen *screen, void *priv, unsigned flags) enum drm_asahi_priority priority = DRM_ASAHI_PRIORITY_MEDIUM; - if (flags & PIPE_CONTEXT_PRIORITY_LOW) + if (flags & PIPE_CONTEXT_LOW_PRIORITY) priority = DRM_ASAHI_PRIORITY_LOW; - else if (flags & PIPE_CONTEXT_PRIORITY_MEDIUM) - priority = DRM_ASAHI_PRIORITY_MEDIUM; - else if (flags & (PIPE_CONTEXT_PRIORITY_HIGH | PIPE_CONTEXT_PRIORITY_REALTIME)) + else if (flags & (PIPE_CONTEXT_HIGH_PRIORITY | PIPE_CONTEXT_REALTIME_PRIORITY)) priority = DRM_ASAHI_PRIORITY_HIGH; ctx->queue_id = agx_create_command_queue(agx_device(screen), priority); From 603953b2948d4fee9b56e4170bda740027dd5f9b Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Tue, 27 Oct 2026 13:57:14 -0300 Subject: [PATCH 02/19] asahi: virtio: close BOs through vdrm so queued binds reach the host first Under virtio, binds and other ccmds are buffered in the guest until the next flush, but bo_free closed the GEM handle with a bare GEM_CLOSE that goes to the host at once. When the closed BO had a VM_BIND still queued, the host saw the close first and its kernel rejected the bind with ENOENT. In CS2 under FEX this happened within seconds of creating the device and ended in VK_ERROR_DEVICE_LOST. vdrm_bo_close() flushes the buffered ccmds before closing. Signed-off-by: Jonathan Goyvaerts Assisted-by: LLM (cherry picked from commit f5685b51b9b970fb7e8148e07ad9eb5aee474a4d) --- src/asahi/lib/agx_device_virtio.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/asahi/lib/agx_device_virtio.c b/src/asahi/lib/agx_device_virtio.c index 0072120b7010..4cad2dc6a176 100644 --- a/src/asahi/lib/agx_device_virtio.c +++ b/src/asahi/lib/agx_device_virtio.c @@ -204,6 +204,14 @@ agx_virtio_bo_mmap(struct agx_device *dev, struct agx_bo *bo, void *fixed_addr, } } +/* Unlike a bare GEM_CLOSE, vdrm_bo_close flushes the buffered ccmds first, so + * the host doesn't drop the object before a queued VM_BIND that names it. */ +static void +agx_virtio_bo_free(struct agx_device *dev, uint32_t handle) +{ + vdrm_bo_close(dev->vdrm, handle); +} + static ssize_t agx_virtio_get_params(struct agx_device *dev, void *buf, size_t size) { @@ -298,7 +306,7 @@ const agx_device_ops_t agx_virtio_device_ops = { */ .simple_ioctl = asahi_simple_ioctl, .bo_munmap = agx_drm_bo_munmap, - .bo_free = agx_drm_bo_close, + .bo_free = agx_virtio_bo_free, }; bool From c32752c154ab1537904bfee721101d6b59908736 Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Tue, 27 Oct 2026 13:57:14 -0300 Subject: [PATCH 03/19] hk: report host-rejected virtio commands as device loss Under virtio, submits, binds and allocations are asynchronous ccmds: when the host kernel rejects one (a lost VM rejects every later submit) the guest never learns of it. The application keeps submitting and waits on fences that never signal, instead of getting VK_ERROR_DEVICE_LOST. The host counts the commands it failed in asahi_shmem::async_error. Read it in hk_check_status and report the device lost once it moves. The VM status mirror and query are asahi ioctls that can't be issued on the virtgpu fd, so skip them under virtio. Signed-off-by: Jonathan Goyvaerts Assisted-by: LLM (cherry picked from commit 14532caed2ce79f0eecca93279e7b322f8d8b4dc) --- src/asahi/lib/agx_device.h | 3 +++ src/asahi/lib/agx_device_virtio.c | 3 +++ src/asahi/vulkan/hk_device.c | 16 ++++++++++++++-- src/asahi/vulkan/hk_device.h | 3 +++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/asahi/lib/agx_device.h b/src/asahi/lib/agx_device.h index d53e5eeddcc0..30138cd136a0 100644 --- a/src/asahi/lib/agx_device.h +++ b/src/asahi/lib/agx_device.h @@ -164,6 +164,9 @@ struct agx_device { /* vdrm device */ struct vdrm_device *vdrm; + /* virtio: the host's count of failed commands (asahi_shmem::async_error), + * NULL if the host doesn't publish it. */ + const uint32_t *async_errors; uint32_t next_blob_id; /* Device handle */ diff --git a/src/asahi/lib/agx_device_virtio.c b/src/asahi/lib/agx_device_virtio.c index 4cad2dc6a176..6bf89810937d 100644 --- a/src/asahi/lib/agx_device_virtio.c +++ b/src/asahi/lib/agx_device_virtio.c @@ -322,5 +322,8 @@ agx_virtio_open_device(struct agx_device *dev) dev->vdrm = vdrm; dev->ops = agx_virtio_device_ops; + struct asahi_shmem *shmem = to_asahi_shmem(vdrm->shmem); + if (vdrm_shmem_has_field(shmem, async_error)) + dev->async_errors = &shmem->async_error; return true; } diff --git a/src/asahi/vulkan/hk_device.c b/src/asahi/vulkan/hk_device.c index 670632153f93..4ae737852a53 100644 --- a/src/asahi/vulkan/hk_device.c +++ b/src/asahi/vulkan/hk_device.c @@ -404,7 +404,9 @@ hk_sampler_heap_remove(struct hk_device *dev, struct hk_rc_sampler *rc) static void hk_init_vm_status_mirror(struct hk_device *dev) { - if (!(dev->dev.params.features & DRM_ASAHI_FEATURE_VM_STATUS) || + /* virtio: dev->dev.fd is the virtgpu fd, asahi ioctls don't go there. */ + if (dev->dev.is_virtio || + !(dev->dev.params.features & DRM_ASAHI_FEATURE_VM_STATUS) || !(dev->dev.params.features & DRM_ASAHI_FEATURE_VM_STATUS_MIRROR)) return; @@ -503,7 +505,15 @@ hk_check_status(struct vk_device *device) if (error) return vk_device_set_lost(&dev->vk, "GPU VM %u failed: %s", dev->dev.vm_id, strerror(-error)); - } else if (dev->dev.params.features & DRM_ASAHI_FEATURE_VM_STATUS) { + } else if (dev->dev.async_errors) { + /* virtio submits are asynchronous: the host counts the ones its kernel + * rejected (a lost VM rejects every later submit) instead of failing them. */ + uint32_t errors = p_atomic_read(dev->dev.async_errors); + if (errors != dev->async_errors_base) + return vk_device_set_lost(&dev->vk, "host rejected %u virtio commands", + errors - dev->async_errors_base); + } else if (!dev->dev.is_virtio && + (dev->dev.params.features & DRM_ASAHI_FEATURE_VM_STATUS)) { struct drm_asahi_vm_status status = {.vm_id = dev->dev.vm_id}; struct drm_asahi_get_params query = { .param_group = DRM_ASAHI_PARAM_GROUP_VM_STATUS, @@ -898,6 +908,8 @@ hk_CreateDevice(VkPhysicalDevice physicalDevice, dev->vk.check_status = hk_check_status; dev->vk.get_timestamp = hk_get_timestamp; hk_init_vm_status_mirror(dev); + if (dev->dev.async_errors) + dev->async_errors_base = p_atomic_read(dev->dev.async_errors); /* This holds for current platforms. We do not currently implement * timestamp scaling, this would require changes in the query copy kernel diff --git a/src/asahi/vulkan/hk_device.h b/src/asahi/vulkan/hk_device.h index fc4bfa1e5b45..a663aa035555 100644 --- a/src/asahi/vulkan/hk_device.h +++ b/src/asahi/vulkan/hk_device.h @@ -240,6 +240,9 @@ struct hk_device { int32_t *word; } vm_status_mirror; + /* virtio: dev->dev.async_errors when the device was created. */ + uint32_t async_errors_base; + struct { /* Immutable for the device's lifetime: the native texel-fetch sampler * table. Its address is published to the GPU as a sampler table base by From 0de6ca997fa55e9c721f7fc8820256f6b65f5904 Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Tue, 27 Oct 2026 13:57:14 -0300 Subject: [PATCH 04/19] asahi: virtio: keep idle BO caches small Under virtio every BO is created shareable, so every BO is an external object of the host VM, and the host kernel's cost per submit grows with their number. CS2 held 13k-17k BOs, about 10.5k of them idle 128 KiB command-pool BOs. Keep the BO cache's idle trim running under virtio, and cap idle Main command BOs per pool at HK_CMD_POOL_BO_MAX there instead of HK_G17P_CMD_POOL_MAIN_BO_MAX. Signed-off-by: Jonathan Goyvaerts Assisted-by: LLM (cherry picked from commit 2ebb59e240d61c364f781fee9a4de81753eca99f) --- src/asahi/lib/agx_bo.c | 2 +- src/asahi/vulkan/hk_cmd_pool.c | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/asahi/lib/agx_bo.c b/src/asahi/lib/agx_bo.c index f015629dbb85..3fa5b9e17575 100644 --- a/src/asahi/lib/agx_bo.c +++ b/src/asahi/lib/agx_bo.c @@ -273,7 +273,7 @@ void agx_bo_cache_start_idle_trim(struct agx_device *dev) { #ifdef HAVE_LIBDRM - if (dev->ops.bo_free != agx_drm_bo_close) + if (dev->ops.bo_free != agx_drm_bo_close && !dev->is_virtio) return; if (mtx_init(&dev->bo_cache.idle_lock, mtx_plain) != thrd_success) return; diff --git a/src/asahi/vulkan/hk_cmd_pool.c b/src/asahi/vulkan/hk_cmd_pool.c index 1becf61a6493..499389e4e6c2 100644 --- a/src/asahi/vulkan/hk_cmd_pool.c +++ b/src/asahi/vulkan/hk_cmd_pool.c @@ -499,9 +499,11 @@ hk_cmd_pool_free_bo_list(struct hk_cmd_pool *pool, enum hk_cmd_bo_type type, struct list_head *bos) { const struct hk_device *dev = hk_cmd_pool_device(pool); + /* Under virtio every BO is an external object, which the host kernel locks + * on every submit: a pool hoarding hundreds of idle BOs slows all submits. */ const unsigned max_free = (agx_native_graphics(&dev->dev) || agx_chip_g17_m5(dev->dev.chip)) && - type == HK_CMD_BO_MAIN + type == HK_CMD_BO_MAIN && !dev->dev.is_virtio ? HK_G17P_CMD_POOL_MAIN_BO_MAX : HK_CMD_POOL_BO_MAX; From c8c66e72580924eeb56a9bfe27e6166d5b2bc0b0 Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Tue, 27 Oct 2026 17:22:27 -0300 Subject: [PATCH 05/19] agx: constrain a cube sample's register LOD, not the whole file A cube sample with a register level or bias capped the fragment shader's register file at 112 halves, so that the level, staged by agx3_tex_stage_cube_lod in a seven-bit field, could not land above half 127. That cap applied to every value of the shader: material shaders that sample an environment cube with a computed level or bias spilled, though they fit in the file. Counter-Strike 2 has 48 such fragment shaders. The staging form encodes the high bit of the CEM face (f16 bit 7), so only the level needs a low register. Keep that one value below half 128 through texture_gpr, as the coordinate already is, and give plain cube samples with a register level or bias the same budget as other cube samples. Cube arrays stage their face in a seven-bit field and bias with minimum places a packed word above the allocation; both keep the cap. Counter-Strike 2's 1969 shaders (fossilize-replay of a capture): spills 11272 -> 9958, fills 15910 -> 13995, scratch -21%, instructions -0.4%; 48 shaders change, none for the worse, typically from ~34 spills to none. A fragment shader with 18 live vec4s around a register-LOD cube sample goes from 245 instructions with 7 spills to 226 without, 8-11% faster per pass, with an identical image; its CEM face lands above half 128. A third of the dEQP-VK cube texture function, filtering, mipmap and gather cases (807) give the same results as before. Signed-off-by: Jonathan Goyvaerts Assisted-by: LLM (cherry picked from commit 5704b5223f79305e7cce3131e4dd38bf9d7c8aa1) --- src/asahi/compiler/agx_register_allocate.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/asahi/compiler/agx_register_allocate.c b/src/asahi/compiler/agx_register_allocate.c index 11af5c589c18..2a794df2d04f 100644 --- a/src/asahi/compiler/agx_register_allocate.c +++ b/src/asahi/compiler/agx_register_allocate.c @@ -2807,13 +2807,13 @@ agx3_cube_register_budget(agx_context *ctx) (I->dim != AGX_DIM_CUBE && I->dim != AGX_DIM_CUBE_ARRAY)) continue; + /* A plain cube sample stages a register level or bias with CEM's face + * in a form that encodes the face's high bit, so only the level needs + * a seven-bit register, and RA keeps it there (texture_gpr). The bias + * with minimum also places a packed word above the allocation. */ const bool bias_min = I->lod_mode == AGX_LOD_MODE_AUTO_LOD_BIAS_MIN; - const bool register_lod = bias_min || - ((I->lod_mode == AGX_LOD_MODE_LOD_MIN || - I->lod_mode == AGX_LOD_MODE_AUTO_LOD_BIAS) && - I->src[1].type != AGX_INDEX_IMMEDIATE); budget = MIN2(budget, - (I->dim == AGX_DIM_CUBE_ARRAY || register_lod) ? 112 : + (I->dim == AGX_DIM_CUBE_ARRAY || bias_min) ? 112 : AGX3B_NUM_REGS - 8); } return ROUND_DOWN_TO(budget, alignment); @@ -3270,6 +3270,13 @@ agx_ra_impl(agx_context *ctx, bool disable_remat) if (I->op == AGX_OPCODE_TEXTURE_SAMPLE && I->shadow && I->src[5].type == AGX_INDEX_NORMAL) BITSET_SET(texture_gpr, I->src[5].value); + + /* A cube sample's register level or bias is a seven-bit operand of + * the staging PACK_SMPIR (agx3_tex_stage_cube_lod). */ + if (I->op == AGX_OPCODE_TEXTURE_SAMPLE && + (I->dim == AGX_DIM_CUBE || I->dim == AGX_DIM_CUBE_ARRAY) && + I->src[1].type == AGX_INDEX_NORMAL) + BITSET_SET(texture_gpr, I->src[1].value); } if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && From e40f42a50267d99ee271b7a3aa775fe598b624ca Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Tue, 27 Oct 2026 21:52:10 -0300 Subject: [PATCH 06/19] agx: give each cube sample its own CEM scratch The packer computed every cube sample's CEM in one shared scratch above the register allocation. A cube array stages CEM's face in a seven-bit PACK_SMPIR field, so for cube arrays that scratch had to sit below half 128, and the whole register file of any shader sampling a cube array was capped at 112 halves. Counter-Strike 2's cube-array material shaders hold far more than that live and spilled heavily. Before RA, give each cube sample (but bias with minimum and scalar- coordinate samples) a seventh source: an undefined four-word vector, defined right before it, which emits no code. RA places it clear of every value live at the sample, and below half 128 for a cube array (texture_gpr); the packer computes that sample's CEM there. Cube arrays lose the 112-half cap and plain cubes the eight halves reserved above the allocation. Samples with their own scratch get a 176-half budget, which keeps a heavy shader at 576 threads: Counter-Strike 2's cube-array material passes ran faster at 177 halves than with the full 184 (185 halves, 512 threads), though they spill more there. Counter-Strike 2's 1969 shaders (fossilize-replay of a capture): spills 9958 -> 3510, fills 13995 -> 4313, scratch -57%, instructions -1.9%; 181 shaders change, none with more instructions. The 90 cube-array shaders go from ~240 to ~100 spills at 177 instead of 121 halves (832 -> 576 threads); 91 plain-cube shaders each free eight halves, several gaining an occupancy step. In de_dust2 (two runs each, kernel job traces), the median of the 50 longest render jobs fell from 1.60-1.65 ms to 1.00-1.22 ms; average fps is within run-to-run noise. dEQP-VK cube and cube-array texture, shadow and GLSL texture-function cases (2615, all cube arrays and a quarter of the rest): same results as before, 586 pass, 0 fail. A cube-array pressure bench renders the same image. test-agx3-cube-scratch checks the pass. Signed-off-by: Jonathan Goyvaerts Assisted-by: LLM (cherry picked from commit c6f52b508c9995d8ed39c54ece0f84b4ffdbab36) --- .../agx3_lower_texture_publications.c | 45 +++++++++++++ src/asahi/compiler/agx3_pack_binary.c | 34 +++++++++- src/asahi/compiler/agx_compile.c | 3 + src/asahi/compiler/agx_compiler.h | 1 + src/asahi/compiler/agx_register_allocate.c | 35 ++++++++-- src/asahi/compiler/meson.build | 1 + .../compiler/test/test-agx3-cube-scratch.cpp | 67 +++++++++++++++++++ 7 files changed, 178 insertions(+), 8 deletions(-) create mode 100644 src/asahi/compiler/test/test-agx3-cube-scratch.cpp diff --git a/src/asahi/compiler/agx3_lower_texture_publications.c b/src/asahi/compiler/agx3_lower_texture_publications.c index ba0821b5c0ff..81fd40abcaef 100644 --- a/src/asahi/compiler/agx3_lower_texture_publications.c +++ b/src/asahi/compiler/agx3_lower_texture_publications.c @@ -394,6 +394,51 @@ agx3_split_texture_coordinates(agx_context *ctx) return progress; } +/* The packer computed every cube sample's CEM in one shared scratch above + * the allocation, eight halves the file had to leave free. A cube array + * stages CEM's face in a seven-bit PACK_SMPIR field + * (agx3_tex_stage_cube_array_layer), so for cube arrays that scratch had to + * sit below half 128 and capped the whole register file at 112 halves. + * + * Give each cube sample its own scratch instead: an undefined four-word + * vector the sample reads, which RA places clear of every value live at the + * sample, below half 128 for a cube array (texture_gpr). Its definition + * emits no code. Run right before RA, so the scratch lives for this one + * instruction. A bias with minimum also packs a word above the allocation + * and keeps the shared scratch, as do scalar-coordinate cube samples. */ +bool +agx3_reserve_cube_scratch(agx_context *ctx) +{ + if (ctx->key->dev.encoding != AGX_ISA_ENCODING_AGX3B) + return false; + + bool progress = false; + agx_foreach_instr_global_safe(ctx, I) { + if (I->op != AGX_OPCODE_TEXTURE_SAMPLE || + (I->dim != AGX_DIM_CUBE && I->dim != AGX_DIM_CUBE_ARRAY) || + I->nr_srcs != 6 || I->nr_dests != 1 || + I->lod_mode == AGX_LOD_MODE_AUTO_LOD_BIAS_MIN) + continue; + + agx_builder b = agx_init_builder(ctx, agx_before_instr(I)); + agx_index scratch = agx_vec_temp(ctx, AGX_SIZE_32, 4); + agx_instr *collect = agx_collect_to(&b, scratch, 4); + agx_foreach_src(collect, c) + collect->src[c] = agx_undef(AGX_SIZE_32); + + agx_instr *sample = agx_alloc_instr(&b, I->op, 1, 7); + memcpy(&sample->op, &I->op, sizeof(*I) - offsetof(agx_instr, op)); + sample->nr_srcs = 7; + sample->dest[0] = I->dest[0]; + memcpy(sample->src, I->src, 6 * sizeof(agx_index)); + sample->src[6] = scratch; + agx_builder_insert(&b.cursor, sample); + agx_remove_instruction(I); + progress = true; + } + return progress; +} + static bool ssa_preserves_publications(const agx_instr *I) { diff --git a/src/asahi/compiler/agx3_pack_binary.c b/src/asahi/compiler/agx3_pack_binary.c index d53ab77bd109..dceec01e06aa 100644 --- a/src/asahi/compiler/agx3_pack_binary.c +++ b/src/asahi/compiler/agx3_pack_binary.c @@ -2237,6 +2237,29 @@ agx3_is_staged_3d_bias_min(const agx_instr *I) !I->offset_xyz; } +/* A cube sample with its own CEM scratch (agx3_reserve_cube_scratch) + * uses it in place of the shared scratch above the allocation. */ +static bool +agx3_cube_own_scratch(const agx_instr *I) +{ + return I->op == AGX_OPCODE_TEXTURE_SAMPLE && + (I->dim == AGX_DIM_CUBE || I->dim == AGX_DIM_CUBE_ARRAY) && + !I->agx3_cube_scalar_coords && I->nr_srcs == 7 && + I->src[6].type == AGX_INDEX_REGISTER; +} + +static const uint16_t * +agx3_cube_scratch(const agx_instr *I, const uint16_t shared[3], + uint16_t own[3]) +{ + if (!agx3_cube_own_scratch(I)) + return shared; + own[0] = I->src[6].value; + own[1] = I->src[6].value + 4; + own[2] = I->src[6].value + 6; + return own; +} + /* Filtered 2D samples use scoreboard-signalling native staging producers. * Plain implicit 2D keeps its coordinate layout. Explicit per-lane LOD and * 2D-array samples stage coordinates into bank 0; their auxiliary scalar is @@ -2258,7 +2281,10 @@ agx3_emit_texture_sample(struct util_dynarray *emission, const bool published = agx_texture_resource(I) != 0; const bool scalar_cube = I->agx3_cube_scalar_coords; const bool split = I->agx3_split_coords; - const unsigned source_count = scalar_cube || split ? 8 : 6; + const unsigned source_count = scalar_cube || split ? 8 + : agx3_cube_own_scratch(I) ? 7 : 6; + uint16_t own_cem_scratch[3]; + cem_scratch = agx3_cube_scratch(I, cem_scratch, own_cem_scratch); const bool explicit_lod = I->lod_mode == AGX_LOD_MODE_LOD_MIN; const bool bias_min = I->lod_mode == AGX_LOD_MODE_AUTO_LOD_BIAS_MIN; const bool gradient_min = I->lod_mode == AGX_LOD_MODE_LOD_GRAD_MIN; @@ -9203,7 +9229,7 @@ agx3_pack_binary(agx_context *ctx, struct util_dynarray *emission) ((scan_I->lod_mode == AGX_LOD_MODE_LOD_MIN || scan_I->lod_mode == AGX_LOD_MODE_AUTO_LOD_BIAS) && scan_I->src[1].type != AGX_INDEX_IMMEDIATE); - has_cube_sample = true; + has_cube_sample |= !agx3_cube_own_scratch(scan_I); /* So is a cube face packed beside a bias and minimum. */ if (register_lod && (scan_I->dim == AGX_DIM_CUBE_ARRAY || bias_min)) @@ -9850,7 +9876,9 @@ agx3_pack_binary(agx_context *ctx, struct util_dynarray *emission) ((I->lod_mode == AGX_LOD_MODE_LOD_MIN || I->lod_mode == AGX_LOD_MODE_AUTO_LOD_BIAS) && I->src[1].type != AGX_INDEX_IMMEDIATE); - const uint16_t *scratch = cem_scratch; + uint16_t own_scratch[3]; + const uint16_t *scratch = + agx3_cube_scratch(I, cem_scratch, own_scratch); assert(nd + 5 <= AGX3_SLOT_MAX_REGS); e->dst[nd++] = scratch[0]; e->dst[nd++] = scratch[0] + 2; diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index 5443f4e509f0..0abad1e79d1a 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -10424,6 +10424,9 @@ agx_compile_function_nir_attempt(nir_shader *nir, nir_function_impl *impl, return false; } + if (agx3_reserve_cube_scratch(ctx)) + agx_validate(ctx, "cube-array scratch"); + /* Record provenance before RA can create stack instructions. */ const bool imports_only_before_ra = agx3_cprog_spill_provenance(ctx); if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && diff --git a/src/asahi/compiler/agx_compiler.h b/src/asahi/compiler/agx_compiler.h index 20461fb035fd..569b45129546 100644 --- a/src/asahi/compiler/agx_compiler.h +++ b/src/asahi/compiler/agx_compiler.h @@ -1210,6 +1210,7 @@ bool agx3_fuse_fma_texture_publications(agx_context *ctx); bool agx3_lower_scalar_texture_publications(agx_context *ctx); bool agx3_lower_scalar_cube_coordinates(agx_context *ctx); bool agx3_split_texture_coordinates(agx_context *ctx); +bool agx3_reserve_cube_scratch(agx_context *ctx); void agx_lower_uniform_sources(agx_context *ctx); void agx_opt_cse(agx_context *ctx); bool agx_opt_collect_split(agx_context *ctx); diff --git a/src/asahi/compiler/agx_register_allocate.c b/src/asahi/compiler/agx_register_allocate.c index 2a794df2d04f..22361ef82de6 100644 --- a/src/asahi/compiler/agx_register_allocate.c +++ b/src/asahi/compiler/agx_register_allocate.c @@ -2799,6 +2799,7 @@ agx3_cube_register_budget(agx_context *ctx) { unsigned budget = AGX3B_NUM_REGS; unsigned alignment = 8; + bool shared_cem = false, packed_layer = false; agx_foreach_instr_global(ctx, I) { agx_foreach_ssa_dest(I, d) alignment = MAX2(alignment, @@ -2809,13 +2810,30 @@ agx3_cube_register_budget(agx_context *ctx) /* A plain cube sample stages a register level or bias with CEM's face * in a form that encodes the face's high bit, so only the level needs - * a seven-bit register, and RA keeps it there (texture_gpr). The bias - * with minimum also places a packed word above the allocation. */ + * a seven-bit register, and RA keeps it there (texture_gpr). A cube + * array stages its face in a seven-bit field, from its own CEM scratch + * (agx3_reserve_cube_scratch) where it has one. The bias with + * minimum also places a packed word above the allocation. */ const bool bias_min = I->lod_mode == AGX_LOD_MODE_AUTO_LOD_BIAS_MIN; + /* With its own scratch, nothing of a cube sample sits above the + * allocation but a cube array's packed layer word. Its budget keeps + * the shader at 576 threads: Counter-Strike 2's register-heavy + * cube-array material passes ran faster at 177 halves than at 185 + * (512 threads), though they spill a little more. */ + const bool own_scratch = !I->agx3_cube_scalar_coords && I->nr_srcs == 7; budget = MIN2(budget, - (I->dim == AGX_DIM_CUBE_ARRAY || bias_min) ? 112 : - AGX3B_NUM_REGS - 8); - } + ((I->dim == AGX_DIM_CUBE_ARRAY && !own_scratch) || bias_min) + ? 112 : own_scratch ? AGX3B_NUM_REGS - 16 + : AGX3B_NUM_REGS - 8); + shared_cem |= !own_scratch; + packed_layer |= own_scratch && I->src[1].type != AGX_INDEX_IMMEDIATE && + (I->lod_mode == AGX_LOD_MODE_LOD_MIN || + I->lod_mode == AGX_LOD_MODE_AUTO_LOD_BIAS); + } + /* The shared CEM scratch and a cube array's prepacked layer word both sit + * above the allocation, below half 191. */ + if (shared_cem && packed_layer) + budget = MIN2(budget, AGX3B_NUM_REGS - 16); return ROUND_DOWN_TO(budget, alignment); } @@ -3277,6 +3295,13 @@ agx_ra_impl(agx_context *ctx, bool disable_remat) (I->dim == AGX_DIM_CUBE || I->dim == AGX_DIM_CUBE_ARRAY) && I->src[1].type == AGX_INDEX_NORMAL) BITSET_SET(texture_gpr, I->src[1].value); + + /* A cube array's own CEM scratch holds its face, a seven-bit operand + * of the staging PACK_SMPIR (agx3_tex_stage_cube_array_layer). */ + if (I->op == AGX_OPCODE_TEXTURE_SAMPLE && + I->dim == AGX_DIM_CUBE_ARRAY && I->nr_srcs == 7 && + I->src[6].type == AGX_INDEX_NORMAL) + BITSET_SET(texture_gpr, I->src[6].value); } if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && diff --git a/src/asahi/compiler/meson.build b/src/asahi/compiler/meson.build index 2b766a4f14d3..5e60955d2402 100644 --- a/src/asahi/compiler/meson.build +++ b/src/asahi/compiler/meson.build @@ -228,6 +228,7 @@ if with_tests 'test/test-agx3-integer-lowering.cpp', 'test/test-agx3-unary-lowering.cpp', 'test/test-agx3-fsel-uniform.cpp', + 'test/test-agx3-cube-scratch.cpp', 'test/test-fminmax-constant-denorm.cpp', 'test/test-shared-byte-offsets.cpp', 'test/test-device-load-batches.cpp', diff --git a/src/asahi/compiler/test/test-agx3-cube-scratch.cpp b/src/asahi/compiler/test/test-agx3-cube-scratch.cpp new file mode 100644 index 000000000000..257fe072e323 --- /dev/null +++ b/src/asahi/compiler/test/test-agx3-cube-scratch.cpp @@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: MIT */ +#include +#include "agx_builder.h" +#include "agx_test.h" + +/* A cube sample gets its own CEM scratch: an undefined four-word vector it + * reads, defined right before it, so RA places the scratch rather than the + * packer stacking it above the allocation. */ +static agx_instr * +cube_sample(agx_builder *b, enum agx_dim dim, enum agx_lod_mode lod) +{ + agx_index coords = agx_vec_temp(b->shader, AGX_SIZE_32, 4); + return agx_texture_sample_to(b, agx_temp(b->shader, AGX_SIZE_16), coords, + agx_immediate(0), agx_immediate(0), + agx_immediate(0), agx_immediate(0), + agx_null(), dim, lod, 0xf, false, false, false, + AGX_GATHER_NONE, false); +} + +TEST(AGX3CubeScratch, CubeSamplesReadAnUndefinedScratchVector) +{ + void *mem = ralloc_context(NULL); + agx_shader_key key = {}; + key.dev.encoding = AGX_ISA_ENCODING_AGX3B; + agx_builder *b = agx_test_builder(mem); + b->shader->key = &key; + cube_sample(b, AGX_DIM_CUBE_ARRAY, AGX_LOD_MODE_AUTO_LOD); + cube_sample(b, AGX_DIM_CUBE, AGX_LOD_MODE_AUTO_LOD); + /* The bias with minimum keeps the shared scratch and the capped file. */ + cube_sample(b, AGX_DIM_CUBE_ARRAY, AGX_LOD_MODE_AUTO_LOD_BIAS_MIN); + cube_sample(b, AGX_DIM_2D, AGX_LOD_MODE_AUTO_LOD); + + ASSERT_TRUE(agx3_reserve_cube_scratch(b->shader)); + + agx_instr *collect = NULL; + unsigned samples = 0, own = 0; + agx_foreach_instr_global(b->shader, I) { + if (I->op == AGX_OPCODE_COLLECT) { + collect = I; + continue; + } + ASSERT_EQ(I->op, AGX_OPCODE_TEXTURE_SAMPLE); + ++samples; + const bool expect_own = (I->dim == AGX_DIM_CUBE || + I->dim == AGX_DIM_CUBE_ARRAY) && + I->lod_mode != AGX_LOD_MODE_AUTO_LOD_BIAS_MIN; + if (!expect_own) { + EXPECT_EQ(I->nr_srcs, 6u); + continue; + } + ++own; + ASSERT_EQ(I->nr_srcs, 7u); + ASSERT_NE(collect, nullptr); + EXPECT_EQ(collect->dest[0].value, I->src[6].value); + EXPECT_EQ(agx_index_size_16(I->src[6]), 8u); + ASSERT_EQ(collect->nr_srcs, 4u); + agx_foreach_src(collect, s) + EXPECT_EQ(collect->src[s].type, AGX_INDEX_UNDEF); + collect = NULL; + } + EXPECT_EQ(samples, 4u); + EXPECT_EQ(own, 2u); + + /* RA may be retried after a failed allocation: no second scratch. */ + EXPECT_FALSE(agx3_reserve_cube_scratch(b->shader)); + ralloc_free(mem); +} From 482c3652cf9a677faec647610fc39ec48a07ddba Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Tue, 27 Oct 2026 16:35:17 -0300 Subject: [PATCH 07/19] hk: do not walk the tiles past the render area A render covers the whole attachment even when its render area is a small rectangle of it. Draws stay inside the render area and the tiles past its bottom-right corner keep their contents, so give the render only the extent up to that corner: the GPU then never visits those tiles. User interfaces render this way: in the Counter-Strike 2 main menu (2560x1440) about 1600 render passes a second draw one element into a full-screen target with a render area under a tenth of it. At that size the per-pass cost is dominated by fixed overhead and the frame rate moves within noise; the tile walk shows on larger attachments. On the M4 Pro a pass with a 128x64 area on an 8192x8192 attachment takes 24 us instead of 125 us (37 against 180 us when the memory clock is low). Secondary command buffers that continue a render pass do not know the area; their streams keep the full extent. tests/vulkan_render_area.c checks that pixels inside and outside small areas, including areas on the right and bottom edges, end up right (with a clamp to half the area it fails), and prints the 8192x8192 timing. dEQP-VK render-area cases and a sample of the renderpass attachment cases give the same results as before. Signed-off-by: Jonathan Goyvaerts Assisted-by: LLM (cherry picked from commit b2ddff3cd1ce9a8f91c81478006ff162dc49d6de) --- src/asahi/vulkan/hk_cmd_buffer.c | 2 + src/asahi/vulkan/hk_cmd_buffer.h | 5 + src/asahi/vulkan/hk_queue.c | 8 + src/asahi/vulkan/meson.build | 9 + src/asahi/vulkan/tests/vulkan_render_area.c | 396 ++++++++++++++++++ .../vulkan/tests/vulkan_render_area.frag | 10 + .../vulkan/tests/vulkan_render_area.vert | 9 + .../vulkan/tests/vulkan_render_area_shaders.h | 79 ++++ 8 files changed, 518 insertions(+) create mode 100644 src/asahi/vulkan/tests/vulkan_render_area.c create mode 100644 src/asahi/vulkan/tests/vulkan_render_area.frag create mode 100644 src/asahi/vulkan/tests/vulkan_render_area.vert create mode 100644 src/asahi/vulkan/tests/vulkan_render_area_shaders.h diff --git a/src/asahi/vulkan/hk_cmd_buffer.c b/src/asahi/vulkan/hk_cmd_buffer.c index 40460151ddf9..267d91e4db84 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.c +++ b/src/asahi/vulkan/hk_cmd_buffer.c @@ -3828,6 +3828,8 @@ hk_cs_init_graphics(struct hk_cmd_buffer *cmd, struct hk_cs *cs) } cs->tib = render->tilebuffer; + cs->area_x1 = render->area.offset.x + render->area.extent.width; + cs->area_y1 = render->area.offset.y + render->area.extent.height; /* Attachmentless rendering defers its API sample count. Publish the * native one-sample no-imageblock profile in this control stream while * preserving the deferred count in render state. */ diff --git a/src/asahi/vulkan/hk_cmd_buffer.h b/src/asahi/vulkan/hk_cmd_buffer.h index d426bb29b20e..ab84d19594a6 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.h +++ b/src/asahi/vulkan/hk_cmd_buffer.h @@ -1291,6 +1291,11 @@ struct hk_cs { /* Remaining state is for graphics only, ignored for compute */ struct agx_tilebuffer_layout tib; + /* Bottom-right corner of the render area, or 0 where it is not known (a + * secondary command buffer continuing a render pass). Tiles past it hold + * nothing the render may change, so the render need not visit them. */ + uint32_t area_x1, area_y1; + struct util_dynarray scissor, depth_bias; uint64_t uploaded_scissor, uploaded_zbias; diff --git a/src/asahi/vulkan/hk_queue.c b/src/asahi/vulkan/hk_queue.c index 4c7eb7eeee4f..5ab808c69fbb 100644 --- a/src/asahi/vulkan/hk_queue.c +++ b/src/asahi/vulkan/hk_queue.c @@ -196,6 +196,14 @@ asahi_fill_vdm_command(struct hk_device *dev, struct hk_cs *cs, c->width_px = empty_attachmentless ? 1 : cs->cr.width; c->height_px = empty_attachmentless ? 1 : cs->cr.height; + /* Draws stay inside the render area and the tiles past its bottom-right + * corner keep their contents, so do not walk them: user interfaces render + * small areas of full-screen targets. */ + if (cs->area_x1 && cs->area_y1) { + c->width_px = MIN2(c->width_px, cs->area_x1); + c->height_px = MIN2(c->height_px, cs->area_y1); + } + c->isp_bgobjdepth = cs->cr.isp_bgobjdepth; c->isp_bgobjvals = cs->cr.isp_bgobjvals; diff --git a/src/asahi/vulkan/meson.build b/src/asahi/vulkan/meson.build index 7519b6e2716f..164f1ca2316c 100644 --- a/src/asahi/vulkan/meson.build +++ b/src/asahi/vulkan/meson.build @@ -686,6 +686,15 @@ if with_tests gnu_symbol_visibility : 'hidden', ) + executable( + 'vulkan-render-area', + files('tests/vulkan_render_area.c'), + include_directories : [inc_include], + dependencies : [cc.find_library('vulkan')], + c_args : [c_msvc_compat_args], + gnu_symbol_visibility : 'hidden', + ) + executable( 'g17p-vulkan-multiview-clear', files('tests/g17p_vulkan_multiview_clear.c'), diff --git a/src/asahi/vulkan/tests/vulkan_render_area.c b/src/asahi/vulkan/tests/vulkan_render_area.c new file mode 100644 index 000000000000..9e4c6a992114 --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_render_area.c @@ -0,0 +1,396 @@ +/* Copyright 2026 Aurora Silicon + * SPDX-License-Identifier: MIT + * + * Render passes whose render area is a small part of a large attachment, as + * user interfaces record them, including areas on the right and bottom edges: + * every pixel outside the areas must keep its contents, every pixel inside + * must take the pass's clear or draw. The driver does not walk the tiles past + * an area's bottom-right corner (hk_queue.c), so those tiles must survive + * untouched. + * + * It then times 1000 passes of a 128x64 area on an 8192x8192 attachment. A + * driver that walks every tile of the attachment for such a pass takes + * several times longer per pass than one that stops at the area (on the + * M4 Pro: 125-180 us against 24-37 us). + * + * Usage: vulkan-render-area Exit status 0 = correct output. + */ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include + +#define CHECK(call) \ + do { \ + VkResult r = (call); \ + if (r != VK_SUCCESS) { \ + fprintf(stderr, "%s: %d\n", #call, r); \ + exit(2); \ + } \ + } while (0) + +#define W 2560 +#define H 1440 + +static VkDevice dev; +static VkPhysicalDevice pdev; +static VkQueue q; +static VkCommandBuffer cb; +static VkFence fence; +static VkImageView view; +static VkPipelineLayout pl; +static VkPipeline pipe; + +static uint32_t memory_type(uint32_t bits, VkMemoryPropertyFlags flags) { + VkPhysicalDeviceMemoryProperties m; + vkGetPhysicalDeviceMemoryProperties(pdev, &m); + for (uint32_t i = 0; i < m.memoryTypeCount; i++) + if ((bits & (1u << i)) && (m.memoryTypes[i].propertyFlags & flags) == flags) + return i; + fprintf(stderr, "no memory type\n"); + exit(2); +} + +/* Full-screen triangle, constant colour from a push constant. */ +#include "vulkan_render_area_shaders.h" + +/* The rectangles of one round: small areas, edges, corners and an offset. */ +static const VkRect2D areas[] = { + {{0, 0}, {128, 64}}, {{W - 96, 0}, {96, 96}}, + {{0, H - 32}, {64, 32}}, {{W - 200, H - 120}, {200, 120}}, + {{1000, 600}, {33, 17}}, {{512, 0}, {256, 1440}}, + {{0, 700}, {2560, 40}}, {{2000, 1000}, {1, 1}}, +}; +#define NA (sizeof(areas) / sizeof(areas[0])) + +static uint32_t pattern(uint32_t x, uint32_t y) { + return 0xff000000u | ((x * 7 + y * 13) & 0xffffff); +} + +static uint32_t area_colour(unsigned i, bool clear) { + return 0xff000000u | (clear ? 0x00a00000u : 0x0000a000u) | (i * 16 + 1); +} + +/* One pass on `area`: a load-op clear, or a load followed by a draw. */ +static void pass(VkRect2D area, uint32_t colour, bool clear) { + VkClearValue cv; + for (unsigned c = 0; c < 4; c++) + cv.color.float32[c] = ((colour >> (8 * c)) & 0xff) / 255.0f; + vkCmdBeginRendering( + cb, &(VkRenderingInfo){ + .sType = VK_STRUCTURE_TYPE_RENDERING_INFO, .renderArea = area, + .layerCount = 1, .colorAttachmentCount = 1, + .pColorAttachments = &(VkRenderingAttachmentInfo){ + .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, + .imageView = view, + .imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + .loadOp = clear ? VK_ATTACHMENT_LOAD_OP_CLEAR + : VK_ATTACHMENT_LOAD_OP_LOAD, + .storeOp = VK_ATTACHMENT_STORE_OP_STORE, + .clearValue = cv}}); + if (!clear) { + vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe); + vkCmdSetViewport(cb, 0, 1, &(VkViewport){0, 0, W, H, 0, 1}); + vkCmdSetScissor(cb, 0, 1, &area); + vkCmdPushConstants(cb, pl, VK_SHADER_STAGE_FRAGMENT_BIT, 0, 4, &colour); + vkCmdDraw(cb, 3, 1, 0, 0); + } + vkCmdEndRendering(cb); +} + +static void submit(void) { + CHECK(vkEndCommandBuffer(cb)); + CHECK(vkQueueSubmit(q, 1, + &(VkSubmitInfo){.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, + .commandBufferCount = 1, + .pCommandBuffers = &cb}, + fence)); + CHECK(vkWaitForFences(dev, 1, &fence, VK_TRUE, UINT64_MAX)); + CHECK(vkResetFences(dev, 1, &fence)); +} + +int main(void) { + VkInstance inst; + CHECK(vkCreateInstance( + &(VkInstanceCreateInfo){ + .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, + .pApplicationInfo = &(VkApplicationInfo){ + .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, + .apiVersion = VK_API_VERSION_1_3}}, + NULL, &inst)); + uint32_t n = 1; + CHECK(vkEnumeratePhysicalDevices(inst, &n, &pdev)); + if (!n) { + fprintf(stderr, "no device\n"); + return 2; + } + float prio = 1; + VkPhysicalDeviceVulkan13Features f13 = { + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES, + .dynamicRendering = VK_TRUE, .synchronization2 = VK_TRUE}; + CHECK(vkCreateDevice( + pdev, + &(VkDeviceCreateInfo){ + .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, .pNext = &f13, + .queueCreateInfoCount = 1, + .pQueueCreateInfos = &(VkDeviceQueueCreateInfo){ + .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, + .queueCount = 1, .pQueuePriorities = &prio}}, + NULL, &dev)); + vkGetDeviceQueue(dev, 0, 0, &q); + + const VkFormat fmt = VK_FORMAT_R8G8B8A8_UNORM; + VkImage img; + CHECK(vkCreateImage( + dev, + &(VkImageCreateInfo){ + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .imageType = VK_IMAGE_TYPE_2D, .format = fmt, .extent = {W, H, 1}, + .mipLevels = 1, .arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_TRANSFER_SRC_BIT | + VK_IMAGE_USAGE_TRANSFER_DST_BIT}, + NULL, &img)); + VkMemoryRequirements mr; + vkGetImageMemoryRequirements(dev, img, &mr); + VkDeviceMemory im; + CHECK(vkAllocateMemory( + dev, + &(VkMemoryAllocateInfo){ + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = mr.size, + .memoryTypeIndex = + memory_type(mr.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)}, + NULL, &im)); + CHECK(vkBindImageMemory(dev, img, im, 0)); + CHECK(vkCreateImageView( + dev, + &(VkImageViewCreateInfo){ + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = img, + .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = fmt, + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}, + NULL, &view)); + + /* Host buffer for the initial pattern and the readback. */ + const VkDeviceSize size = (VkDeviceSize)W * H * 4; + VkBuffer buf; + CHECK(vkCreateBuffer(dev, + &(VkBufferCreateInfo){ + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .size = size, + .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | + VK_BUFFER_USAGE_TRANSFER_DST_BIT}, + NULL, &buf)); + vkGetBufferMemoryRequirements(dev, buf, &mr); + VkDeviceMemory bm; + CHECK(vkAllocateMemory( + dev, + &(VkMemoryAllocateInfo){ + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = mr.size, + .memoryTypeIndex = + memory_type(mr.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)}, + NULL, &bm)); + CHECK(vkBindBufferMemory(dev, buf, bm, 0)); + uint32_t *px; + CHECK(vkMapMemory(dev, bm, 0, VK_WHOLE_SIZE, 0, (void **)&px)); + + VkShaderModule vs, fs; + CHECK(vkCreateShaderModule( + dev, + &(VkShaderModuleCreateInfo){ + .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, + .codeSize = sizeof(render_area_vert), .pCode = render_area_vert}, + NULL, &vs)); + CHECK(vkCreateShaderModule( + dev, + &(VkShaderModuleCreateInfo){ + .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, + .codeSize = sizeof(render_area_frag), .pCode = render_area_frag}, + NULL, &fs)); + CHECK(vkCreatePipelineLayout( + dev, + &(VkPipelineLayoutCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .pushConstantRangeCount = 1, + .pPushConstantRanges = &(VkPushConstantRange){ + VK_SHADER_STAGE_FRAGMENT_BIT, 0, 4}}, + NULL, &pl)); + VkDynamicState dyn[] = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR}; + CHECK(vkCreateGraphicsPipelines( + dev, VK_NULL_HANDLE, 1, + &(VkGraphicsPipelineCreateInfo){ + .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, + .pNext = &(VkPipelineRenderingCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO, + .colorAttachmentCount = 1, .pColorAttachmentFormats = &fmt}, + .stageCount = 2, + .pStages = (VkPipelineShaderStageCreateInfo[]){ + {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .stage = VK_SHADER_STAGE_VERTEX_BIT, .module = vs, .pName = "main"}, + {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .stage = VK_SHADER_STAGE_FRAGMENT_BIT, .module = fs, + .pName = "main"}}, + .pVertexInputState = &(VkPipelineVertexInputStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO}, + .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, + .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST}, + .pViewportState = &(VkPipelineViewportStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, + .viewportCount = 1, .scissorCount = 1}, + .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, + .lineWidth = 1}, + .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, + .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT}, + .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, + .attachmentCount = 1, + .pAttachments = &(VkPipelineColorBlendAttachmentState){ + .colorWriteMask = 0xf}}, + .pDynamicState = &(VkPipelineDynamicStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, + .dynamicStateCount = 2, .pDynamicStates = dyn}, + .layout = pl}, + NULL, &pipe)); + + VkCommandPool pool; + CHECK(vkCreateCommandPool( + dev, + &(VkCommandPoolCreateInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, + .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT}, + NULL, &pool)); + CHECK(vkAllocateCommandBuffers( + dev, + &(VkCommandBufferAllocateInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, + .commandPool = pool, .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, + .commandBufferCount = 1}, + &cb)); + CHECK(vkCreateFence( + dev, &(VkFenceCreateInfo){.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}, + NULL, &fence)); + + const VkImageSubresourceLayers layers = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; + const VkBufferImageCopy whole = {0, 0, 0, layers, {0, 0, 0}, {W, H, 1}}; + +#define BARRIER(from, to) \ + vkCmdPipelineBarrier2( \ + cb, &(VkDependencyInfo){ \ + .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, \ + .imageMemoryBarrierCount = 1, \ + .pImageMemoryBarriers = &(VkImageMemoryBarrier2){ \ + .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, \ + .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, \ + .srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT, \ + .dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, \ + .dstAccessMask = \ + VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT, \ + .oldLayout = (from), .newLayout = (to), .image = img, \ + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}}) + + /* Correctness: pattern, then each area once with a draw and once (half of + * them) with a clear, then read back. */ + for (uint32_t y = 0; y < H; y++) + for (uint32_t x = 0; x < W; x++) + px[y * W + x] = pattern(x, y); + CHECK(vkBeginCommandBuffer( + cb, &(VkCommandBufferBeginInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO})); + BARRIER(VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); + vkCmdCopyBufferToImage(cb, buf, img, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, + &whole); + BARRIER(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); + for (unsigned i = 0; i < NA; i++) + pass(areas[i], area_colour(i, i & 1), i & 1); + BARRIER(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + vkCmdCopyImageToBuffer(cb, img, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, buf, 1, + &whole); + submit(); + + unsigned bad = 0; + for (uint32_t y = 0; y < H; y++) { + for (uint32_t x = 0; x < W; x++) { + uint32_t want = pattern(x, y); + for (unsigned i = 0; i < NA; i++) { + const VkRect2D *a = &areas[i]; + if (x >= (uint32_t)a->offset.x && x < a->offset.x + a->extent.width && + y >= (uint32_t)a->offset.y && y < a->offset.y + a->extent.height) + want = area_colour(i, i & 1); + } + if (px[y * W + x] != want && bad++ < 8) + fprintf(stderr, "(%u, %u): %08x, expected %08x\n", x, y, + px[y * W + x], want); + } + } + + /* Cost of a small-area pass on a large attachment. */ + const uint32_t big = 8192, timed = 1000; + VkImage big_img; + CHECK(vkCreateImage( + dev, + &(VkImageCreateInfo){ + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .imageType = VK_IMAGE_TYPE_2D, .format = fmt, .extent = {big, big, 1}, + .mipLevels = 1, .arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT}, + NULL, &big_img)); + vkGetImageMemoryRequirements(dev, big_img, &mr); + VkDeviceMemory big_mem; + CHECK(vkAllocateMemory( + dev, + &(VkMemoryAllocateInfo){ + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = mr.size, + .memoryTypeIndex = + memory_type(mr.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)}, + NULL, &big_mem)); + CHECK(vkBindImageMemory(dev, big_img, big_mem, 0)); + CHECK(vkCreateImageView( + dev, + &(VkImageViewCreateInfo){ + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = big_img, + .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = fmt, + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}, + NULL, &view)); + img = big_img; + double ms[2]; + for (unsigned run = 0; run < 2; run++) { + CHECK(vkBeginCommandBuffer( + cb, &(VkCommandBufferBeginInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO})); + BARRIER(VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); + pass((VkRect2D){{0, 0}, {big, big}}, area_colour(0, true), true); + /* Alternate the height by a row so that no pass continues the previous + * one: each must be its own render. Run 0 only clears, as reference. */ + for (unsigned i = 0; run && i < timed; i++) + pass((VkRect2D){{0, 0}, {128, 64 - (i & 1)}}, area_colour(i & 7, false), + false); + struct timespec t0, t1; + clock_gettime(CLOCK_MONOTONIC, &t0); + submit(); + clock_gettime(CLOCK_MONOTONIC, &t1); + ms[run] = (t1.tv_sec - t0.tv_sec) * 1e3 + (t1.tv_nsec - t0.tv_nsec) / 1e6; + } + printf("%.1f us per 128x64 pass on a %ux%u attachment\n", + (ms[1] - ms[0]) * 1e3 / timed, big, big); + + if (bad) { + printf("FAIL: %u pixels wrong\n", bad); + return 1; + } + printf("PASS\n"); + return 0; +} diff --git a/src/asahi/vulkan/tests/vulkan_render_area.frag b/src/asahi/vulkan/tests/vulkan_render_area.frag new file mode 100644 index 000000000000..392a091183ac --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_render_area.frag @@ -0,0 +1,10 @@ +/* Copyright 2026 Aurora Silicon + * SPDX-License-Identifier: MIT + */ +#version 450 +layout(push_constant) uniform Colour { uint rgba; } pc; +layout(location = 0) out vec4 o; +void main() +{ + o = unpackUnorm4x8(pc.rgba); +} diff --git a/src/asahi/vulkan/tests/vulkan_render_area.vert b/src/asahi/vulkan/tests/vulkan_render_area.vert new file mode 100644 index 000000000000..f30223ff2793 --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_render_area.vert @@ -0,0 +1,9 @@ +/* Copyright 2026 Aurora Silicon + * SPDX-License-Identifier: MIT + */ +#version 450 +void main() +{ + vec2 p = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); + gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0); +} diff --git a/src/asahi/vulkan/tests/vulkan_render_area_shaders.h b/src/asahi/vulkan/tests/vulkan_render_area_shaders.h new file mode 100644 index 000000000000..679a7aa3f9ca --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_render_area_shaders.h @@ -0,0 +1,79 @@ +/* SPDX-License-Identifier: MIT + * Generated with glslangValidator -V from vulkan_render_area.vert/.frag. + */ +#include +static const uint32_t render_area_vert[] = { + 0x07230203, 0x00010000, 0x0008000b, 0x0000002b, 0x00000000, 0x00020011, + 0x00000001, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, + 0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0007000f, 0x00000000, + 0x00000004, 0x6e69616d, 0x00000000, 0x0000000c, 0x0000001d, 0x00030003, + 0x00000002, 0x000001c2, 0x00040005, 0x00000004, 0x6e69616d, 0x00000000, + 0x00030005, 0x00000009, 0x00000070, 0x00060005, 0x0000000c, 0x565f6c67, + 0x65747265, 0x646e4978, 0x00007865, 0x00060005, 0x0000001b, 0x505f6c67, + 0x65567265, 0x78657472, 0x00000000, 0x00060006, 0x0000001b, 0x00000000, + 0x505f6c67, 0x7469736f, 0x006e6f69, 0x00070006, 0x0000001b, 0x00000001, + 0x505f6c67, 0x746e696f, 0x657a6953, 0x00000000, 0x00070006, 0x0000001b, + 0x00000002, 0x435f6c67, 0x4470696c, 0x61747369, 0x0065636e, 0x00070006, + 0x0000001b, 0x00000003, 0x435f6c67, 0x446c6c75, 0x61747369, 0x0065636e, + 0x00030005, 0x0000001d, 0x00000000, 0x00040047, 0x0000000c, 0x0000000b, + 0x0000002a, 0x00030047, 0x0000001b, 0x00000002, 0x00050048, 0x0000001b, + 0x00000000, 0x0000000b, 0x00000000, 0x00050048, 0x0000001b, 0x00000001, + 0x0000000b, 0x00000001, 0x00050048, 0x0000001b, 0x00000002, 0x0000000b, + 0x00000003, 0x00050048, 0x0000001b, 0x00000003, 0x0000000b, 0x00000004, + 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00030016, + 0x00000006, 0x00000020, 0x00040017, 0x00000007, 0x00000006, 0x00000002, + 0x00040020, 0x00000008, 0x00000007, 0x00000007, 0x00040015, 0x0000000a, + 0x00000020, 0x00000001, 0x00040020, 0x0000000b, 0x00000001, 0x0000000a, + 0x0004003b, 0x0000000b, 0x0000000c, 0x00000001, 0x0004002b, 0x0000000a, + 0x0000000e, 0x00000001, 0x0004002b, 0x0000000a, 0x00000010, 0x00000002, + 0x00040017, 0x00000017, 0x00000006, 0x00000004, 0x00040015, 0x00000018, + 0x00000020, 0x00000000, 0x0004002b, 0x00000018, 0x00000019, 0x00000001, + 0x0004001c, 0x0000001a, 0x00000006, 0x00000019, 0x0006001e, 0x0000001b, + 0x00000017, 0x00000006, 0x0000001a, 0x0000001a, 0x00040020, 0x0000001c, + 0x00000003, 0x0000001b, 0x0004003b, 0x0000001c, 0x0000001d, 0x00000003, + 0x0004002b, 0x0000000a, 0x0000001e, 0x00000000, 0x0004002b, 0x00000006, + 0x00000020, 0x40000000, 0x0004002b, 0x00000006, 0x00000022, 0x3f800000, + 0x0004002b, 0x00000006, 0x00000025, 0x00000000, 0x00040020, 0x00000029, + 0x00000003, 0x00000017, 0x00050036, 0x00000002, 0x00000004, 0x00000000, + 0x00000003, 0x000200f8, 0x00000005, 0x0004003b, 0x00000008, 0x00000009, + 0x00000007, 0x0004003d, 0x0000000a, 0x0000000d, 0x0000000c, 0x000500c4, + 0x0000000a, 0x0000000f, 0x0000000d, 0x0000000e, 0x000500c7, 0x0000000a, + 0x00000011, 0x0000000f, 0x00000010, 0x0004006f, 0x00000006, 0x00000012, + 0x00000011, 0x0004003d, 0x0000000a, 0x00000013, 0x0000000c, 0x000500c7, + 0x0000000a, 0x00000014, 0x00000013, 0x00000010, 0x0004006f, 0x00000006, + 0x00000015, 0x00000014, 0x00050050, 0x00000007, 0x00000016, 0x00000012, + 0x00000015, 0x0003003e, 0x00000009, 0x00000016, 0x0004003d, 0x00000007, + 0x0000001f, 0x00000009, 0x0005008e, 0x00000007, 0x00000021, 0x0000001f, + 0x00000020, 0x00050050, 0x00000007, 0x00000023, 0x00000022, 0x00000022, + 0x00050083, 0x00000007, 0x00000024, 0x00000021, 0x00000023, 0x00050051, + 0x00000006, 0x00000026, 0x00000024, 0x00000000, 0x00050051, 0x00000006, + 0x00000027, 0x00000024, 0x00000001, 0x00070050, 0x00000017, 0x00000028, + 0x00000026, 0x00000027, 0x00000025, 0x00000022, 0x00050041, 0x00000029, + 0x0000002a, 0x0000001d, 0x0000001e, 0x0003003e, 0x0000002a, 0x00000028, + 0x000100fd, 0x00010038, +}; +static const uint32_t render_area_frag[] = { + 0x07230203, 0x00010000, 0x0008000b, 0x00000014, 0x00000000, 0x00020011, + 0x00000001, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, + 0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0006000f, 0x00000004, + 0x00000004, 0x6e69616d, 0x00000000, 0x00000009, 0x00030010, 0x00000004, + 0x00000007, 0x00030003, 0x00000002, 0x000001c2, 0x00040005, 0x00000004, + 0x6e69616d, 0x00000000, 0x00030005, 0x00000009, 0x0000006f, 0x00040005, + 0x0000000b, 0x6f6c6f43, 0x00007275, 0x00050006, 0x0000000b, 0x00000000, + 0x61626772, 0x00000000, 0x00030005, 0x0000000d, 0x00006370, 0x00040047, + 0x00000009, 0x0000001e, 0x00000000, 0x00030047, 0x0000000b, 0x00000002, + 0x00050048, 0x0000000b, 0x00000000, 0x00000023, 0x00000000, 0x00020013, + 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00030016, 0x00000006, + 0x00000020, 0x00040017, 0x00000007, 0x00000006, 0x00000004, 0x00040020, + 0x00000008, 0x00000003, 0x00000007, 0x0004003b, 0x00000008, 0x00000009, + 0x00000003, 0x00040015, 0x0000000a, 0x00000020, 0x00000000, 0x0003001e, + 0x0000000b, 0x0000000a, 0x00040020, 0x0000000c, 0x00000009, 0x0000000b, + 0x0004003b, 0x0000000c, 0x0000000d, 0x00000009, 0x00040015, 0x0000000e, + 0x00000020, 0x00000001, 0x0004002b, 0x0000000e, 0x0000000f, 0x00000000, + 0x00040020, 0x00000010, 0x00000009, 0x0000000a, 0x00050036, 0x00000002, + 0x00000004, 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x00050041, + 0x00000010, 0x00000011, 0x0000000d, 0x0000000f, 0x0004003d, 0x0000000a, + 0x00000012, 0x00000011, 0x0006000c, 0x00000007, 0x00000013, 0x00000001, + 0x00000040, 0x00000012, 0x0003003e, 0x00000009, 0x00000013, 0x000100fd, + 0x00010038, +}; From c560bf230efd824f7aa340d4bf2c5ecfb0617c36 Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Tue, 27 Oct 2026 17:11:13 -0300 Subject: [PATCH 08/19] hk: merge loads of one uniform buffer record A shader that reads members of one record of a uniform buffer array as separate expressions, as Counter-Strike 2's depth-of-field gather does with taps[i].xy, taps[i].z and taps[i].w in a 48-tap loop, got a load for each: once lowered to global loads, their 64-bit addresses (base + u2u64(48 + 16 * i), + u2u64(56 + 16 * i), ...) no longer show that they are the same record, so the vectorizer could not merge them. Before lowering, make component reads of a uniform buffer vector loads of the whole vector and mark the loads reorderable, as load_ubo always is: a uniform buffer does not change during a draw or dispatch. CSE then merges the repeated loads, and the vectorizer, run on the deref offsets, merges neighbouring ones. tests/vulkan_ubo_record_loop.c renders that loop twice, once as the game writes it and once with each record read once, checks that the images are identical and prints the cost ratio: on the M4 Pro 1.31 before this change (69 against 64 instructions), 1.18 after (65 against 64). The rest is the loop-invariant scale read, which stays in the loop: the game does not enable robustBufferAccess, so it may not be read speculatively. In a benchmark of the game's shader at 2560x1440 with 48 taps a pass goes from 5.35 to 4.85 ms. A sample of dEQP-VK.ubo, robustness2 uniform and binding_model uniform buffer cases (4002) passes as before. Signed-off-by: Jonathan Goyvaerts Assisted-by: LLM (cherry picked from commit a044f6b9209e08dd6b439527dae19de3cb261be2) --- src/asahi/vulkan/hk_shader.c | 37 ++ src/asahi/vulkan/meson.build | 9 + .../vulkan/tests/vulkan_ubo_record_loop.c | 450 ++++++++++++++++++ .../vulkan/tests/vulkan_ubo_record_loop.vert | 11 + .../tests/vulkan_ubo_record_loop_shaders.h | 342 +++++++++++++ .../tests/vulkan_ubo_record_loop_split.frag | 37 ++ .../tests/vulkan_ubo_record_loop_whole.frag | 38 ++ 7 files changed, 924 insertions(+) create mode 100644 src/asahi/vulkan/tests/vulkan_ubo_record_loop.c create mode 100644 src/asahi/vulkan/tests/vulkan_ubo_record_loop.vert create mode 100644 src/asahi/vulkan/tests/vulkan_ubo_record_loop_shaders.h create mode 100644 src/asahi/vulkan/tests/vulkan_ubo_record_loop_split.frag create mode 100644 src/asahi/vulkan/tests/vulkan_ubo_record_loop_whole.frag diff --git a/src/asahi/vulkan/hk_shader.c b/src/asahi/vulkan/hk_shader.c index 77d3dbdf13bc..f28cfb721676 100644 --- a/src/asahi/vulkan/hk_shader.c +++ b/src/asahi/vulkan/hk_shader.c @@ -2931,6 +2931,21 @@ hk_tes_ray_queries_in_compute(const struct hk_device *dev) dev->gen1_native_rq && dev->gen1_native_graphics_rq; } +/* A uniform buffer does not change during a draw or dispatch. */ +static bool +hk_ubo_loads_reorderable(nir_builder *b, nir_intrinsic_instr *intr, + UNUSED void *data) +{ + if (intr->intrinsic != nir_intrinsic_load_deref || + !nir_deref_mode_is(nir_src_as_deref(intr->src[0]), nir_var_mem_ubo)) + return false; + + nir_intrinsic_set_access(intr, nir_intrinsic_access(intr) | + ACCESS_CAN_REORDER | + ACCESS_NON_WRITEABLE); + return true; +} + static void hk_lower_nir(struct hk_device *dev, nir_shader *nir, const struct vk_pipeline_robustness_state *rs, bool is_multiview, @@ -3104,6 +3119,28 @@ hk_lower_nir(struct hk_device *dev, nir_shader *nir, hk_buffer_addr_format(rs->storage_buffers, rs->null_storage_buffer_descriptor, agx_chip_g17_m5(dev->dev.chip))); + /* While UBO offsets are still 32-bit deref offsets, merge the loads of a + * block that read the same record: once lowered, their 64-bit addresses + * hide how they relate. Components of a vector become loads of the whole + * vector, so that reads of one record's .xy and .z are the same load. A + * uniform buffer does not change during a draw or dispatch, so its loads + * may be reordered, as load_ubo always may: repeated ones are then merged + * and loop invariant ones may leave the loop. */ + NIR_PASS(_, nir, nir_lower_array_deref_of_vec, nir_var_mem_ubo, NULL, + nir_lower_direct_array_deref_of_vec_load); + NIR_PASS(_, nir, nir_shader_intrinsics_pass, hk_ubo_loads_reorderable, + nir_metadata_control_flow, NULL); + NIR_PASS(_, nir, nir_opt_cse); + NIR_PASS(_, nir, nir_opt_load_store_vectorize, + &(const nir_load_store_vectorize_options){ + .modes = nir_var_mem_ubo, + .robust_modes = + rs->uniform_buffers == + VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT + ? 0 + : nir_var_mem_ubo, + .callback = agx_mem_vectorize_cb, + }); NIR_PASS(_, nir, nir_lower_explicit_io, nir_var_mem_ubo, hk_ubo_addr_format(rs->uniform_buffers)); diff --git a/src/asahi/vulkan/meson.build b/src/asahi/vulkan/meson.build index 164f1ca2316c..86b12bbe3886 100644 --- a/src/asahi/vulkan/meson.build +++ b/src/asahi/vulkan/meson.build @@ -695,6 +695,15 @@ if with_tests gnu_symbol_visibility : 'hidden', ) + executable( + 'vulkan-ubo-record-loop', + files('tests/vulkan_ubo_record_loop.c'), + include_directories : [inc_include], + dependencies : [cc.find_library('vulkan'), cc.find_library('m')], + c_args : [c_msvc_compat_args], + gnu_symbol_visibility : 'hidden', + ) + executable( 'g17p-vulkan-multiview-clear', files('tests/g17p_vulkan_multiview_clear.c'), diff --git a/src/asahi/vulkan/tests/vulkan_ubo_record_loop.c b/src/asahi/vulkan/tests/vulkan_ubo_record_loop.c new file mode 100644 index 000000000000..5b92fc894bfc --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_ubo_record_loop.c @@ -0,0 +1,450 @@ +/* Copyright 2026 Aurora Silicon + * SPDX-License-Identifier: MIT + * + * A gather loop over a uniform buffer array of tap records, written two + * ways: reading each record's members in separate expressions (as + * Counter-Strike 2's depth-of-field shader does), and reading each record + * once. The two must render the same image, and a compiler that merges the + * reads of one record makes them cost about the same: the program prints + * the cost of each and their ratio (on the M4 Pro: 1.31 before the reads were + * merged, 1.18 after; what remains is the scale read, which stays in the loop + * because it may not be read speculatively). + * + * Usage: vulkan-ubo-record-loop [taps=48] Exit status 0 = same image. + */ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include + +#include "vulkan_ubo_record_loop_shaders.h" + +#define CHECK(call) \ + do { \ + VkResult r = (call); \ + if (r != VK_SUCCESS) { \ + fprintf(stderr, "%s: %d\n", #call, r); \ + exit(2); \ + } \ + } while (0) + +#define W 2048 +#define H 2048 +#define TEX 512 +#define PASSES 40 +#define ROUNDS 7 + +static VkDevice dev; +static VkPhysicalDevice pdev; +static VkQueue q; +static VkCommandBuffer cb; +static VkFence fence; +static VkImage target; +static VkImageView target_view; +static VkBuffer staging; +static VkPipeline pipe[2]; +static VkPipelineLayout layout; +static VkDescriptorSet set; + +static uint32_t memory_type(uint32_t bits, VkMemoryPropertyFlags flags) { + VkPhysicalDeviceMemoryProperties m; + vkGetPhysicalDeviceMemoryProperties(pdev, &m); + for (uint32_t i = 0; i < m.memoryTypeCount; i++) + if ((bits & (1u << i)) && (m.memoryTypes[i].propertyFlags & flags) == flags) + return i; + fprintf(stderr, "no memory type\n"); + exit(2); +} + +static VkDeviceMemory bind_memory(VkMemoryRequirements mr, + VkMemoryPropertyFlags flags) { + VkDeviceMemory mem; + CHECK(vkAllocateMemory( + dev, + &(VkMemoryAllocateInfo){.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = mr.size, + .memoryTypeIndex = + memory_type(mr.memoryTypeBits, flags)}, + NULL, &mem)); + return mem; +} + +static VkImage image(uint32_t w, uint32_t h, VkImageUsageFlags usage, + VkImageView *view) { + VkImage img; + CHECK(vkCreateImage( + dev, + &(VkImageCreateInfo){.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .imageType = VK_IMAGE_TYPE_2D, + .format = VK_FORMAT_R8G8B8A8_UNORM, + .extent = {w, h, 1}, .mipLevels = 1, + .arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, .usage = usage}, + NULL, &img)); + VkMemoryRequirements mr; + vkGetImageMemoryRequirements(dev, img, &mr); + CHECK(vkBindImageMemory( + dev, img, bind_memory(mr, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT), 0)); + CHECK(vkCreateImageView( + dev, + &(VkImageViewCreateInfo){ + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = img, + .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = VK_FORMAT_R8G8B8A8_UNORM, + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}, + NULL, view)); + return img; +} + +static VkBuffer host_buffer(VkDeviceSize size, VkBufferUsageFlags usage, + void **map) { + VkBuffer buf; + CHECK(vkCreateBuffer(dev, + &(VkBufferCreateInfo){ + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .size = size, .usage = usage}, + NULL, &buf)); + VkMemoryRequirements mr; + vkGetBufferMemoryRequirements(dev, buf, &mr); + VkDeviceMemory mem = + bind_memory(mr, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); + CHECK(vkBindBufferMemory(dev, buf, mem, 0)); + CHECK(vkMapMemory(dev, mem, 0, VK_WHOLE_SIZE, 0, map)); + return buf; +} + +static VkShaderModule module(const uint32_t *code, size_t size) { + VkShaderModule m; + CHECK(vkCreateShaderModule( + dev, + &(VkShaderModuleCreateInfo){ + .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, + .codeSize = size, .pCode = code}, + NULL, &m)); + return m; +} + +static void barrier(VkCommandBuffer cb, VkImage img, VkImageLayout from, + VkImageLayout to) { + vkCmdPipelineBarrier2( + cb, &(VkDependencyInfo){ + .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, + .imageMemoryBarrierCount = 1, + .pImageMemoryBarriers = &(VkImageMemoryBarrier2){ + .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, + .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .dstAccessMask = + VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT, + .oldLayout = from, .newLayout = to, .image = img, + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}}); +} + +static const VkImageSubresourceLayers sl = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; + +static int compare_double(const void *a, const void *b) { + double x = *(const double *)a, y = *(const double *)b; + return x < y ? -1 : x > y; +} + +/* Record `passes` draws of shader `s` (each its own render) and, if + * `readback`, copy the target to the staging buffer. Returns ms. */ +static double run(unsigned s, unsigned passes, bool readback) { + CHECK(vkBeginCommandBuffer( + cb, &(VkCommandBufferBeginInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO})); + barrier(cb, target, VK_IMAGE_LAYOUT_UNDEFINED, + VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); + for (unsigned p = 0; p < passes; p++) { + vkCmdBeginRendering( + cb, &(VkRenderingInfo){ + .sType = VK_STRUCTURE_TYPE_RENDERING_INFO, + .renderArea = {{0, 0}, {W, H}}, .layerCount = 1, + .colorAttachmentCount = 1, + .pColorAttachments = &(VkRenderingAttachmentInfo){ + .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, + .imageView = target_view, + .imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + .loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, + .storeOp = VK_ATTACHMENT_STORE_OP_STORE}}); + vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe[s]); + vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, layout, 0, + 1, &set, 0, NULL); + vkCmdSetViewport(cb, 0, 1, &(VkViewport){0, 0, W, H, 0, 1}); + vkCmdSetScissor(cb, 0, 1, &(VkRect2D){{0, 0}, {W, H}}); + vkCmdDraw(cb, 3, 1, 0, 0); + vkCmdEndRendering(cb); + } + if (readback) { + barrier(cb, target, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + vkCmdCopyImageToBuffer( + cb, target, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, staging, 1, + &(VkBufferImageCopy){0, 0, 0, sl, {0, 0, 0}, {W, H, 1}}); + } + CHECK(vkEndCommandBuffer(cb)); + struct timespec t0, t1; + clock_gettime(CLOCK_MONOTONIC, &t0); + CHECK(vkQueueSubmit(q, 1, + &(VkSubmitInfo){.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, + .commandBufferCount = 1, + .pCommandBuffers = &cb}, + fence)); + CHECK(vkWaitForFences(dev, 1, &fence, VK_TRUE, UINT64_MAX)); + clock_gettime(CLOCK_MONOTONIC, &t1); + CHECK(vkResetFences(dev, 1, &fence)); + return (t1.tv_sec - t0.tv_sec) * 1e3 + (t1.tv_nsec - t0.tv_nsec) / 1e6; +} + +int main(int argc, char **argv) { + const int taps = argc > 1 ? atoi(argv[1]) : 48; + VkInstance inst; + CHECK(vkCreateInstance( + &(VkInstanceCreateInfo){ + .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, + .pApplicationInfo = &(VkApplicationInfo){ + .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, + .apiVersion = VK_API_VERSION_1_3}}, + NULL, &inst)); + uint32_t n = 1; + CHECK(vkEnumeratePhysicalDevices(inst, &n, &pdev)); + if (!n) { + fprintf(stderr, "no device\n"); + return 2; + } + float prio = 1; + CHECK(vkCreateDevice( + pdev, + &(VkDeviceCreateInfo){ + .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, + .pNext = &(VkPhysicalDeviceVulkan13Features){ + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES, + .dynamicRendering = VK_TRUE, .synchronization2 = VK_TRUE}, + .queueCreateInfoCount = 1, + .pQueueCreateInfos = &(VkDeviceQueueCreateInfo){ + .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, + .queueCount = 1, .pQueuePriorities = &prio}}, + NULL, &dev)); + vkGetDeviceQueue(dev, 0, 0, &q); + + /* Target, a noise texture, the tap records and a readback buffer. */ + VkImageView tex_view; + target = image(W, H, + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_TRANSFER_SRC_BIT, + &target_view); + VkImage tex = image(TEX, TEX, + VK_IMAGE_USAGE_SAMPLED_BIT | + VK_IMAGE_USAGE_TRANSFER_DST_BIT, + &tex_view); + uint32_t *pixels; + staging = host_buffer((VkDeviceSize)W * H * 4, + VK_BUFFER_USAGE_TRANSFER_SRC_BIT | + VK_BUFFER_USAGE_TRANSFER_DST_BIT, + (void **)&pixels); + for (uint32_t i = 0; i < TEX * TEX; i++) + pixels[i] = (i * 2654435761u) ^ (i >> 7); + struct { + float pad0[5]; + float scale; /* 20 */ + float pad1[6]; + float tap[256][4]; /* 48 */ + int32_t count; /* 4144 */ + } *ubo_data; + VkBuffer ubo = host_buffer(sizeof(*ubo_data), + VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, + (void **)&ubo_data); + for (int i = 0; i < 256; i++) { + float a = i * 2.39996f, r = 12.0f * sqrtf((i + 0.5f) / 256.0f); + ubo_data->tap[i][0] = r * cosf(a) / W; + ubo_data->tap[i][1] = r * sinf(a) / H; + ubo_data->tap[i][2] = 0.1f; + ubo_data->tap[i][3] = 0.6f + i / 1024.0f; + } + ubo_data->count = taps; + ubo_data->scale = 1.5f; + + VkSampler smp; + CHECK(vkCreateSampler( + dev, + &(VkSamplerCreateInfo){.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, + .magFilter = VK_FILTER_LINEAR, + .minFilter = VK_FILTER_LINEAR}, + NULL, &smp)); + VkDescriptorSetLayout dsl; + CHECK(vkCreateDescriptorSetLayout( + dev, + &(VkDescriptorSetLayoutCreateInfo){ + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .bindingCount = 3, + .pBindings = (VkDescriptorSetLayoutBinding[]){ + {0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, + VK_SHADER_STAGE_FRAGMENT_BIT}, + {1, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1, + VK_SHADER_STAGE_FRAGMENT_BIT}, + {2, VK_DESCRIPTOR_TYPE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT}}}, + NULL, &dsl)); + VkDescriptorPool pool; + CHECK(vkCreateDescriptorPool( + dev, + &(VkDescriptorPoolCreateInfo){ + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, .maxSets = 1, + .poolSizeCount = 3, + .pPoolSizes = (VkDescriptorPoolSize[]){ + {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1}, + {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1}, + {VK_DESCRIPTOR_TYPE_SAMPLER, 1}}}, + NULL, &pool)); + CHECK(vkAllocateDescriptorSets( + dev, + &(VkDescriptorSetAllocateInfo){ + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, + .descriptorPool = pool, .descriptorSetCount = 1, .pSetLayouts = &dsl}, + &set)); + vkUpdateDescriptorSets( + dev, 3, + (VkWriteDescriptorSet[]){ + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = set, + .dstBinding = 0, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .pBufferInfo = &(VkDescriptorBufferInfo){ubo, 0, VK_WHOLE_SIZE}}, + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = set, + .dstBinding = 1, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, + .pImageInfo = &(VkDescriptorImageInfo){ + VK_NULL_HANDLE, tex_view, + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL}}, + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = set, + .dstBinding = 2, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER, + .pImageInfo = &(VkDescriptorImageInfo){smp}}}, + 0, NULL); + CHECK(vkCreatePipelineLayout( + dev, + &(VkPipelineLayoutCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .setLayoutCount = 1, .pSetLayouts = &dsl}, + NULL, &layout)); + + const VkFormat fmt = VK_FORMAT_R8G8B8A8_UNORM; + VkShaderModule vs = module(ubo_loop_vert, sizeof(ubo_loop_vert)); + VkShaderModule fs[2] = {module(ubo_loop_split, sizeof(ubo_loop_split)), + module(ubo_loop_whole, sizeof(ubo_loop_whole))}; + for (unsigned i = 0; i < 2; i++) { + VkDynamicState dyn[] = {VK_DYNAMIC_STATE_VIEWPORT, + VK_DYNAMIC_STATE_SCISSOR}; + CHECK(vkCreateGraphicsPipelines( + dev, VK_NULL_HANDLE, 1, + &(VkGraphicsPipelineCreateInfo){ + .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, + .pNext = &(VkPipelineRenderingCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO, + .colorAttachmentCount = 1, .pColorAttachmentFormats = &fmt}, + .stageCount = 2, + .pStages = (VkPipelineShaderStageCreateInfo[]){ + {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .stage = VK_SHADER_STAGE_VERTEX_BIT, .module = vs, + .pName = "main"}, + {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .stage = VK_SHADER_STAGE_FRAGMENT_BIT, .module = fs[i], + .pName = "main"}}, + .pVertexInputState = &(VkPipelineVertexInputStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO}, + .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo){ + .sType = + VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, + .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST}, + .pViewportState = &(VkPipelineViewportStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, + .viewportCount = 1, .scissorCount = 1}, + .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo){ + .sType = + VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, + .lineWidth = 1}, + .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, + .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT}, + .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, + .attachmentCount = 1, + .pAttachments = &(VkPipelineColorBlendAttachmentState){ + .colorWriteMask = 0xf}}, + .pDynamicState = &(VkPipelineDynamicStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, + .dynamicStateCount = 2, .pDynamicStates = dyn}, + .layout = layout}, + NULL, &pipe[i])); + } + + VkCommandPool cmd_pool; + CHECK(vkCreateCommandPool( + dev, + &(VkCommandPoolCreateInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, + .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT}, + NULL, &cmd_pool)); + CHECK(vkAllocateCommandBuffers( + dev, + &(VkCommandBufferAllocateInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, + .commandPool = cmd_pool, .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, + .commandBufferCount = 1}, + &cb)); + CHECK(vkCreateFence( + dev, &(VkFenceCreateInfo){.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}, + NULL, &fence)); + + /* Upload the texture. */ + CHECK(vkBeginCommandBuffer( + cb, &(VkCommandBufferBeginInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO})); + barrier(cb, tex, VK_IMAGE_LAYOUT_UNDEFINED, + VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); + vkCmdCopyBufferToImage( + cb, staging, tex, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, + &(VkBufferImageCopy){0, 0, 0, sl, {0, 0, 0}, {TEX, TEX, 1}}); + barrier(cb, tex, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); + CHECK(vkEndCommandBuffer(cb)); + CHECK(vkQueueSubmit(q, 1, + &(VkSubmitInfo){.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, + .commandBufferCount = 1, + .pCommandBuffers = &cb}, + fence)); + CHECK(vkWaitForFences(dev, 1, &fence, VK_TRUE, UINT64_MAX)); + CHECK(vkResetFences(dev, 1, &fence)); + + /* Both shaders must render the same image. */ + uint32_t *image0 = malloc((size_t)W * H * 4); + run(0, 1, true); + memcpy(image0, pixels, (size_t)W * H * 4); + run(1, 1, true); + unsigned bad = 0; + for (size_t i = 0; i < (size_t)W * H; i++) + bad += image0[i] != pixels[i]; + + /* Alternate the two, so that both see the same clocks. */ + double t[2][ROUNDS]; + for (unsigned r = 0; r < ROUNDS; r++) + for (unsigned s = 0; s < 2; s++) + t[s][r] = run(s, PASSES, false) / PASSES; + qsort(t[0], ROUNDS, sizeof(double), compare_double); + qsort(t[1], ROUNDS, sizeof(double), compare_double); + printf("%d taps, %ux%u: members read separately %.0f us/pass, record read " + "once %.0f us/pass, ratio %.2f\n", + taps, W, H, t[0][ROUNDS / 2] * 1e3, t[1][ROUNDS / 2] * 1e3, + t[0][ROUNDS / 2] / t[1][ROUNDS / 2]); + + if (bad) { + printf("FAIL: %u pixels differ between the two shaders\n", bad); + return 1; + } + printf("PASS\n"); + return 0; +} diff --git a/src/asahi/vulkan/tests/vulkan_ubo_record_loop.vert b/src/asahi/vulkan/tests/vulkan_ubo_record_loop.vert new file mode 100644 index 000000000000..e997c7dc2331 --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_ubo_record_loop.vert @@ -0,0 +1,11 @@ +/* Copyright 2026 Aurora Silicon + * SPDX-License-Identifier: MIT + */ +#version 450 +layout(location = 0) out vec2 uv; +void main() +{ + vec2 p = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); + uv = p; + gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0); +} diff --git a/src/asahi/vulkan/tests/vulkan_ubo_record_loop_shaders.h b/src/asahi/vulkan/tests/vulkan_ubo_record_loop_shaders.h new file mode 100644 index 000000000000..d8d939921e55 --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_ubo_record_loop_shaders.h @@ -0,0 +1,342 @@ +/* SPDX-License-Identifier: MIT + * Generated with glslangValidator -V from vulkan_ubo_record_loop{.vert,_split.frag,_whole.frag}. + */ +#include +static const uint32_t ubo_loop_vert[] = { + 0x07230203, 0x00010000, 0x0008000b, 0x0000002e, 0x00000000, 0x00020011, + 0x00000001, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, + 0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0008000f, 0x00000000, + 0x00000004, 0x6e69616d, 0x00000000, 0x0000000c, 0x00000018, 0x00000020, + 0x00030003, 0x00000002, 0x000001c2, 0x00040005, 0x00000004, 0x6e69616d, + 0x00000000, 0x00030005, 0x00000009, 0x00000070, 0x00060005, 0x0000000c, + 0x565f6c67, 0x65747265, 0x646e4978, 0x00007865, 0x00030005, 0x00000018, + 0x00007675, 0x00060005, 0x0000001e, 0x505f6c67, 0x65567265, 0x78657472, + 0x00000000, 0x00060006, 0x0000001e, 0x00000000, 0x505f6c67, 0x7469736f, + 0x006e6f69, 0x00070006, 0x0000001e, 0x00000001, 0x505f6c67, 0x746e696f, + 0x657a6953, 0x00000000, 0x00070006, 0x0000001e, 0x00000002, 0x435f6c67, + 0x4470696c, 0x61747369, 0x0065636e, 0x00070006, 0x0000001e, 0x00000003, + 0x435f6c67, 0x446c6c75, 0x61747369, 0x0065636e, 0x00030005, 0x00000020, + 0x00000000, 0x00040047, 0x0000000c, 0x0000000b, 0x0000002a, 0x00040047, + 0x00000018, 0x0000001e, 0x00000000, 0x00030047, 0x0000001e, 0x00000002, + 0x00050048, 0x0000001e, 0x00000000, 0x0000000b, 0x00000000, 0x00050048, + 0x0000001e, 0x00000001, 0x0000000b, 0x00000001, 0x00050048, 0x0000001e, + 0x00000002, 0x0000000b, 0x00000003, 0x00050048, 0x0000001e, 0x00000003, + 0x0000000b, 0x00000004, 0x00020013, 0x00000002, 0x00030021, 0x00000003, + 0x00000002, 0x00030016, 0x00000006, 0x00000020, 0x00040017, 0x00000007, + 0x00000006, 0x00000002, 0x00040020, 0x00000008, 0x00000007, 0x00000007, + 0x00040015, 0x0000000a, 0x00000020, 0x00000001, 0x00040020, 0x0000000b, + 0x00000001, 0x0000000a, 0x0004003b, 0x0000000b, 0x0000000c, 0x00000001, + 0x0004002b, 0x0000000a, 0x0000000e, 0x00000001, 0x0004002b, 0x0000000a, + 0x00000010, 0x00000002, 0x00040020, 0x00000017, 0x00000003, 0x00000007, + 0x0004003b, 0x00000017, 0x00000018, 0x00000003, 0x00040017, 0x0000001a, + 0x00000006, 0x00000004, 0x00040015, 0x0000001b, 0x00000020, 0x00000000, + 0x0004002b, 0x0000001b, 0x0000001c, 0x00000001, 0x0004001c, 0x0000001d, + 0x00000006, 0x0000001c, 0x0006001e, 0x0000001e, 0x0000001a, 0x00000006, + 0x0000001d, 0x0000001d, 0x00040020, 0x0000001f, 0x00000003, 0x0000001e, + 0x0004003b, 0x0000001f, 0x00000020, 0x00000003, 0x0004002b, 0x0000000a, + 0x00000021, 0x00000000, 0x0004002b, 0x00000006, 0x00000023, 0x40000000, + 0x0004002b, 0x00000006, 0x00000025, 0x3f800000, 0x0004002b, 0x00000006, + 0x00000028, 0x00000000, 0x00040020, 0x0000002c, 0x00000003, 0x0000001a, + 0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8, + 0x00000005, 0x0004003b, 0x00000008, 0x00000009, 0x00000007, 0x0004003d, + 0x0000000a, 0x0000000d, 0x0000000c, 0x000500c4, 0x0000000a, 0x0000000f, + 0x0000000d, 0x0000000e, 0x000500c7, 0x0000000a, 0x00000011, 0x0000000f, + 0x00000010, 0x0004006f, 0x00000006, 0x00000012, 0x00000011, 0x0004003d, + 0x0000000a, 0x00000013, 0x0000000c, 0x000500c7, 0x0000000a, 0x00000014, + 0x00000013, 0x00000010, 0x0004006f, 0x00000006, 0x00000015, 0x00000014, + 0x00050050, 0x00000007, 0x00000016, 0x00000012, 0x00000015, 0x0003003e, + 0x00000009, 0x00000016, 0x0004003d, 0x00000007, 0x00000019, 0x00000009, + 0x0003003e, 0x00000018, 0x00000019, 0x0004003d, 0x00000007, 0x00000022, + 0x00000009, 0x0005008e, 0x00000007, 0x00000024, 0x00000022, 0x00000023, + 0x00050050, 0x00000007, 0x00000026, 0x00000025, 0x00000025, 0x00050083, + 0x00000007, 0x00000027, 0x00000024, 0x00000026, 0x00050051, 0x00000006, + 0x00000029, 0x00000027, 0x00000000, 0x00050051, 0x00000006, 0x0000002a, + 0x00000027, 0x00000001, 0x00070050, 0x0000001a, 0x0000002b, 0x00000029, + 0x0000002a, 0x00000028, 0x00000025, 0x00050041, 0x0000002c, 0x0000002d, + 0x00000020, 0x00000021, 0x0003003e, 0x0000002d, 0x0000002b, 0x000100fd, + 0x00010038, +}; +static const uint32_t ubo_loop_split[] = { + 0x07230203, 0x00010000, 0x0008000b, 0x0000008c, 0x00000000, 0x00020011, + 0x00000001, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, + 0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0007000f, 0x00000004, + 0x00000004, 0x6e69616d, 0x00000000, 0x00000015, 0x00000083, 0x00030010, + 0x00000004, 0x00000007, 0x00030003, 0x00000002, 0x000001c2, 0x00040005, + 0x00000004, 0x6e69616d, 0x00000000, 0x00040005, 0x00000008, 0x696d696c, + 0x00000074, 0x00030005, 0x0000000b, 0x00786574, 0x00030005, 0x0000000f, + 0x00706d73, 0x00030005, 0x00000015, 0x00007675, 0x00030005, 0x0000001e, + 0x0000006e, 0x00030005, 0x00000022, 0x006d7573, 0x00030005, 0x0000002b, + 0x00000069, 0x00040005, 0x00000035, 0x6f636552, 0x00736472, 0x00040006, + 0x00000035, 0x00000000, 0x00000076, 0x00040005, 0x00000036, 0x73706154, + 0x00000000, 0x00050006, 0x00000036, 0x00000000, 0x6c616373, 0x00000065, + 0x00040006, 0x00000036, 0x00000001, 0x00706174, 0x00050006, 0x00000036, + 0x00000002, 0x6e756f63, 0x00000074, 0x00030005, 0x00000038, 0x00000075, + 0x00030005, 0x00000040, 0x00000070, 0x00030005, 0x0000004a, 0x00000063, + 0x00030005, 0x00000050, 0x00636f63, 0x00040005, 0x00000059, 0x6d616c63, + 0x00646570, 0x00030005, 0x00000083, 0x0000006f, 0x00040047, 0x0000000b, + 0x00000021, 0x00000001, 0x00040047, 0x0000000b, 0x00000022, 0x00000000, + 0x00040047, 0x0000000f, 0x00000021, 0x00000002, 0x00040047, 0x0000000f, + 0x00000022, 0x00000000, 0x00040047, 0x00000015, 0x0000001e, 0x00000000, + 0x00040047, 0x00000034, 0x00000006, 0x00000010, 0x00050048, 0x00000035, + 0x00000000, 0x00000023, 0x00000000, 0x00030047, 0x00000036, 0x00000002, + 0x00050048, 0x00000036, 0x00000000, 0x00000023, 0x00000014, 0x00050048, + 0x00000036, 0x00000001, 0x00000023, 0x00000030, 0x00050048, 0x00000036, + 0x00000002, 0x00000023, 0x00001030, 0x00040047, 0x00000038, 0x00000021, + 0x00000000, 0x00040047, 0x00000038, 0x00000022, 0x00000000, 0x00040047, + 0x00000083, 0x0000001e, 0x00000000, 0x00020013, 0x00000002, 0x00030021, + 0x00000003, 0x00000002, 0x00030016, 0x00000006, 0x00000020, 0x00040020, + 0x00000007, 0x00000007, 0x00000006, 0x00090019, 0x00000009, 0x00000006, + 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000, + 0x00040020, 0x0000000a, 0x00000000, 0x00000009, 0x0004003b, 0x0000000a, + 0x0000000b, 0x00000000, 0x0002001a, 0x0000000d, 0x00040020, 0x0000000e, + 0x00000000, 0x0000000d, 0x0004003b, 0x0000000e, 0x0000000f, 0x00000000, + 0x0003001b, 0x00000011, 0x00000009, 0x00040017, 0x00000013, 0x00000006, + 0x00000002, 0x00040020, 0x00000014, 0x00000001, 0x00000013, 0x0004003b, + 0x00000014, 0x00000015, 0x00000001, 0x00040017, 0x00000017, 0x00000006, + 0x00000004, 0x00040015, 0x00000019, 0x00000020, 0x00000000, 0x0004002b, + 0x00000019, 0x0000001a, 0x00000003, 0x0004002b, 0x00000006, 0x0000001c, + 0x40000000, 0x0004002b, 0x00000006, 0x0000001f, 0x3f800000, 0x00040017, + 0x00000020, 0x00000006, 0x00000003, 0x00040020, 0x00000021, 0x00000007, + 0x00000020, 0x00040015, 0x00000029, 0x00000020, 0x00000001, 0x00040020, + 0x0000002a, 0x00000007, 0x00000029, 0x0004002b, 0x00000029, 0x0000002c, + 0x00000000, 0x0004002b, 0x00000019, 0x00000033, 0x00000100, 0x0004001c, + 0x00000034, 0x00000017, 0x00000033, 0x0003001e, 0x00000035, 0x00000034, + 0x0005001e, 0x00000036, 0x00000006, 0x00000035, 0x00000029, 0x00040020, + 0x00000037, 0x00000002, 0x00000036, 0x0004003b, 0x00000037, 0x00000038, + 0x00000002, 0x0004002b, 0x00000029, 0x00000039, 0x00000002, 0x00040020, + 0x0000003a, 0x00000002, 0x00000029, 0x00020014, 0x0000003d, 0x00040020, + 0x0000003f, 0x00000007, 0x00000013, 0x0004002b, 0x00000029, 0x00000042, + 0x00000001, 0x00040020, 0x00000044, 0x00000002, 0x00000017, 0x00040020, + 0x00000049, 0x00000007, 0x00000017, 0x0004002b, 0x00000006, 0x00000055, + 0x3f000000, 0x0004002b, 0x00000006, 0x00000061, 0x00000000, 0x0004002b, + 0x00000019, 0x0000006e, 0x00000002, 0x00040020, 0x0000006f, 0x00000002, + 0x00000006, 0x00040020, 0x00000082, 0x00000003, 0x00000017, 0x0004003b, + 0x00000082, 0x00000083, 0x00000003, 0x00050036, 0x00000002, 0x00000004, + 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x0004003b, 0x00000007, + 0x00000008, 0x00000007, 0x0004003b, 0x00000007, 0x0000001e, 0x00000007, + 0x0004003b, 0x00000021, 0x00000022, 0x00000007, 0x0004003b, 0x0000002a, + 0x0000002b, 0x00000007, 0x0004003b, 0x0000003f, 0x00000040, 0x00000007, + 0x0004003b, 0x00000049, 0x0000004a, 0x00000007, 0x0004003b, 0x00000007, + 0x00000050, 0x00000007, 0x0004003b, 0x00000007, 0x00000059, 0x00000007, + 0x0004003b, 0x00000007, 0x0000005d, 0x00000007, 0x0004003d, 0x00000009, + 0x0000000c, 0x0000000b, 0x0004003d, 0x0000000d, 0x00000010, 0x0000000f, + 0x00050056, 0x00000011, 0x00000012, 0x0000000c, 0x00000010, 0x0004003d, + 0x00000013, 0x00000016, 0x00000015, 0x00050057, 0x00000017, 0x00000018, + 0x00000012, 0x00000016, 0x00050051, 0x00000006, 0x0000001b, 0x00000018, + 0x00000003, 0x00050085, 0x00000006, 0x0000001d, 0x0000001b, 0x0000001c, + 0x0003003e, 0x00000008, 0x0000001d, 0x0003003e, 0x0000001e, 0x0000001f, + 0x0004003d, 0x00000009, 0x00000023, 0x0000000b, 0x0004003d, 0x0000000d, + 0x00000024, 0x0000000f, 0x00050056, 0x00000011, 0x00000025, 0x00000023, + 0x00000024, 0x0004003d, 0x00000013, 0x00000026, 0x00000015, 0x00050057, + 0x00000017, 0x00000027, 0x00000025, 0x00000026, 0x0008004f, 0x00000020, + 0x00000028, 0x00000027, 0x00000027, 0x00000000, 0x00000001, 0x00000002, + 0x0003003e, 0x00000022, 0x00000028, 0x0003003e, 0x0000002b, 0x0000002c, + 0x000200f9, 0x0000002d, 0x000200f8, 0x0000002d, 0x000400f6, 0x0000002f, + 0x00000030, 0x00000000, 0x000200f9, 0x00000031, 0x000200f8, 0x00000031, + 0x0004003d, 0x00000029, 0x00000032, 0x0000002b, 0x00050041, 0x0000003a, + 0x0000003b, 0x00000038, 0x00000039, 0x0004003d, 0x00000029, 0x0000003c, + 0x0000003b, 0x000500b1, 0x0000003d, 0x0000003e, 0x00000032, 0x0000003c, + 0x000400fa, 0x0000003e, 0x0000002e, 0x0000002f, 0x000200f8, 0x0000002e, + 0x0004003d, 0x00000013, 0x00000041, 0x00000015, 0x0004003d, 0x00000029, + 0x00000043, 0x0000002b, 0x00070041, 0x00000044, 0x00000045, 0x00000038, + 0x00000042, 0x0000002c, 0x00000043, 0x0004003d, 0x00000017, 0x00000046, + 0x00000045, 0x0007004f, 0x00000013, 0x00000047, 0x00000046, 0x00000046, + 0x00000000, 0x00000001, 0x00050081, 0x00000013, 0x00000048, 0x00000041, + 0x00000047, 0x0003003e, 0x00000040, 0x00000048, 0x0004003d, 0x00000009, + 0x0000004b, 0x0000000b, 0x0004003d, 0x0000000d, 0x0000004c, 0x0000000f, + 0x00050056, 0x00000011, 0x0000004d, 0x0000004b, 0x0000004c, 0x0004003d, + 0x00000013, 0x0000004e, 0x00000040, 0x00050057, 0x00000017, 0x0000004f, + 0x0000004d, 0x0000004e, 0x0003003e, 0x0000004a, 0x0000004f, 0x0004003d, + 0x00000009, 0x00000051, 0x0000000b, 0x0004003d, 0x0000000d, 0x00000052, + 0x0000000f, 0x00050056, 0x00000011, 0x00000053, 0x00000051, 0x00000052, + 0x0004003d, 0x00000013, 0x00000054, 0x00000040, 0x0005008e, 0x00000013, + 0x00000056, 0x00000054, 0x00000055, 0x00050057, 0x00000017, 0x00000057, + 0x00000053, 0x00000056, 0x00050051, 0x00000006, 0x00000058, 0x00000057, + 0x00000003, 0x0003003e, 0x00000050, 0x00000058, 0x0004003d, 0x00000006, + 0x0000005a, 0x00000050, 0x0004003d, 0x00000006, 0x0000005b, 0x00000008, + 0x000500ba, 0x0000003d, 0x0000005c, 0x0000005a, 0x0000005b, 0x000300f7, + 0x0000005f, 0x00000000, 0x000400fa, 0x0000005c, 0x0000005e, 0x00000064, + 0x000200f8, 0x0000005e, 0x0004003d, 0x00000006, 0x00000060, 0x00000050, + 0x0004003d, 0x00000006, 0x00000062, 0x00000008, 0x0008000c, 0x00000006, + 0x00000063, 0x00000001, 0x0000002b, 0x00000060, 0x00000061, 0x00000062, + 0x0003003e, 0x0000005d, 0x00000063, 0x000200f9, 0x0000005f, 0x000200f8, + 0x00000064, 0x0004003d, 0x00000006, 0x00000065, 0x00000050, 0x0003003e, + 0x0000005d, 0x00000065, 0x000200f9, 0x0000005f, 0x000200f8, 0x0000005f, + 0x0004003d, 0x00000006, 0x00000066, 0x0000005d, 0x0003003e, 0x00000059, + 0x00000066, 0x0004003d, 0x00000020, 0x00000067, 0x00000022, 0x0004003d, + 0x00000006, 0x00000068, 0x0000001e, 0x00060050, 0x00000020, 0x00000069, + 0x00000068, 0x00000068, 0x00000068, 0x00050088, 0x00000020, 0x0000006a, + 0x00000067, 0x00000069, 0x0004003d, 0x00000017, 0x0000006b, 0x0000004a, + 0x0008004f, 0x00000020, 0x0000006c, 0x0000006b, 0x0000006b, 0x00000000, + 0x00000001, 0x00000002, 0x0004003d, 0x00000029, 0x0000006d, 0x0000002b, + 0x00080041, 0x0000006f, 0x00000070, 0x00000038, 0x00000042, 0x0000002c, + 0x0000006d, 0x0000006e, 0x0004003d, 0x00000006, 0x00000071, 0x00000070, + 0x0004003d, 0x00000029, 0x00000072, 0x0000002b, 0x00080041, 0x0000006f, + 0x00000073, 0x00000038, 0x00000042, 0x0000002c, 0x00000072, 0x0000001a, + 0x0004003d, 0x00000006, 0x00000074, 0x00000073, 0x0004003d, 0x00000006, + 0x00000075, 0x00000059, 0x00050041, 0x0000006f, 0x00000076, 0x00000038, + 0x0000002c, 0x0004003d, 0x00000006, 0x00000077, 0x00000076, 0x00050085, + 0x00000006, 0x00000078, 0x00000075, 0x00000077, 0x0008000c, 0x00000006, + 0x00000079, 0x00000001, 0x00000031, 0x00000071, 0x00000074, 0x00000078, + 0x00060050, 0x00000020, 0x0000007a, 0x00000079, 0x00000079, 0x00000079, + 0x0008000c, 0x00000020, 0x0000007b, 0x00000001, 0x0000002e, 0x0000006a, + 0x0000006c, 0x0000007a, 0x0004003d, 0x00000020, 0x0000007c, 0x00000022, + 0x00050081, 0x00000020, 0x0000007d, 0x0000007c, 0x0000007b, 0x0003003e, + 0x00000022, 0x0000007d, 0x0004003d, 0x00000006, 0x0000007e, 0x0000001e, + 0x00050081, 0x00000006, 0x0000007f, 0x0000007e, 0x0000001f, 0x0003003e, + 0x0000001e, 0x0000007f, 0x000200f9, 0x00000030, 0x000200f8, 0x00000030, + 0x0004003d, 0x00000029, 0x00000080, 0x0000002b, 0x00050080, 0x00000029, + 0x00000081, 0x00000080, 0x00000042, 0x0003003e, 0x0000002b, 0x00000081, + 0x000200f9, 0x0000002d, 0x000200f8, 0x0000002f, 0x0004003d, 0x00000020, + 0x00000084, 0x00000022, 0x0004003d, 0x00000006, 0x00000085, 0x0000001e, + 0x00060050, 0x00000020, 0x00000086, 0x00000085, 0x00000085, 0x00000085, + 0x00050088, 0x00000020, 0x00000087, 0x00000084, 0x00000086, 0x00050051, + 0x00000006, 0x00000088, 0x00000087, 0x00000000, 0x00050051, 0x00000006, + 0x00000089, 0x00000087, 0x00000001, 0x00050051, 0x00000006, 0x0000008a, + 0x00000087, 0x00000002, 0x00070050, 0x00000017, 0x0000008b, 0x00000088, + 0x00000089, 0x0000008a, 0x0000001f, 0x0003003e, 0x00000083, 0x0000008b, + 0x000100fd, 0x00010038, +}; +static const uint32_t ubo_loop_whole[] = { + 0x07230203, 0x00010000, 0x0008000b, 0x0000008e, 0x00000000, 0x00020011, + 0x00000001, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, + 0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0007000f, 0x00000004, + 0x00000004, 0x6e69616d, 0x00000000, 0x00000023, 0x00000085, 0x00030010, + 0x00000004, 0x00000007, 0x00030003, 0x00000002, 0x000001c2, 0x00040005, + 0x00000004, 0x6e69616d, 0x00000000, 0x00040005, 0x00000008, 0x6c616373, + 0x00000065, 0x00040005, 0x0000000d, 0x6f636552, 0x00736472, 0x00040006, + 0x0000000d, 0x00000000, 0x00000076, 0x00040005, 0x0000000f, 0x73706154, + 0x00000000, 0x00050006, 0x0000000f, 0x00000000, 0x6c616373, 0x00000065, + 0x00040006, 0x0000000f, 0x00000001, 0x00706174, 0x00050006, 0x0000000f, + 0x00000002, 0x6e756f63, 0x00000074, 0x00030005, 0x00000011, 0x00000075, + 0x00040005, 0x00000016, 0x696d696c, 0x00000074, 0x00030005, 0x00000019, + 0x00786574, 0x00030005, 0x0000001d, 0x00706d73, 0x00030005, 0x00000023, + 0x00007675, 0x00030005, 0x0000002a, 0x0000006e, 0x00030005, 0x0000002e, + 0x006d7573, 0x00030005, 0x00000036, 0x00000069, 0x00030005, 0x00000044, + 0x00000074, 0x00030005, 0x0000004b, 0x00000070, 0x00030005, 0x00000050, + 0x00000063, 0x00030005, 0x00000056, 0x00636f63, 0x00040005, 0x0000005f, + 0x6d616c63, 0x00646570, 0x00030005, 0x00000085, 0x0000006f, 0x00040047, + 0x0000000c, 0x00000006, 0x00000010, 0x00050048, 0x0000000d, 0x00000000, + 0x00000023, 0x00000000, 0x00030047, 0x0000000f, 0x00000002, 0x00050048, + 0x0000000f, 0x00000000, 0x00000023, 0x00000014, 0x00050048, 0x0000000f, + 0x00000001, 0x00000023, 0x00000030, 0x00050048, 0x0000000f, 0x00000002, + 0x00000023, 0x00001030, 0x00040047, 0x00000011, 0x00000021, 0x00000000, + 0x00040047, 0x00000011, 0x00000022, 0x00000000, 0x00040047, 0x00000019, + 0x00000021, 0x00000001, 0x00040047, 0x00000019, 0x00000022, 0x00000000, + 0x00040047, 0x0000001d, 0x00000021, 0x00000002, 0x00040047, 0x0000001d, + 0x00000022, 0x00000000, 0x00040047, 0x00000023, 0x0000001e, 0x00000000, + 0x00040047, 0x00000085, 0x0000001e, 0x00000000, 0x00020013, 0x00000002, + 0x00030021, 0x00000003, 0x00000002, 0x00030016, 0x00000006, 0x00000020, + 0x00040020, 0x00000007, 0x00000007, 0x00000006, 0x00040017, 0x00000009, + 0x00000006, 0x00000004, 0x00040015, 0x0000000a, 0x00000020, 0x00000000, + 0x0004002b, 0x0000000a, 0x0000000b, 0x00000100, 0x0004001c, 0x0000000c, + 0x00000009, 0x0000000b, 0x0003001e, 0x0000000d, 0x0000000c, 0x00040015, + 0x0000000e, 0x00000020, 0x00000001, 0x0005001e, 0x0000000f, 0x00000006, + 0x0000000d, 0x0000000e, 0x00040020, 0x00000010, 0x00000002, 0x0000000f, + 0x0004003b, 0x00000010, 0x00000011, 0x00000002, 0x0004002b, 0x0000000e, + 0x00000012, 0x00000000, 0x00040020, 0x00000013, 0x00000002, 0x00000006, + 0x00090019, 0x00000017, 0x00000006, 0x00000001, 0x00000000, 0x00000000, + 0x00000000, 0x00000001, 0x00000000, 0x00040020, 0x00000018, 0x00000000, + 0x00000017, 0x0004003b, 0x00000018, 0x00000019, 0x00000000, 0x0002001a, + 0x0000001b, 0x00040020, 0x0000001c, 0x00000000, 0x0000001b, 0x0004003b, + 0x0000001c, 0x0000001d, 0x00000000, 0x0003001b, 0x0000001f, 0x00000017, + 0x00040017, 0x00000021, 0x00000006, 0x00000002, 0x00040020, 0x00000022, + 0x00000001, 0x00000021, 0x0004003b, 0x00000022, 0x00000023, 0x00000001, + 0x0004002b, 0x0000000a, 0x00000026, 0x00000003, 0x0004002b, 0x00000006, + 0x00000028, 0x40000000, 0x0004002b, 0x00000006, 0x0000002b, 0x3f800000, + 0x00040017, 0x0000002c, 0x00000006, 0x00000003, 0x00040020, 0x0000002d, + 0x00000007, 0x0000002c, 0x00040020, 0x00000035, 0x00000007, 0x0000000e, + 0x0004002b, 0x0000000e, 0x0000003d, 0x00000002, 0x00040020, 0x0000003e, + 0x00000002, 0x0000000e, 0x00020014, 0x00000041, 0x00040020, 0x00000043, + 0x00000007, 0x00000009, 0x0004002b, 0x0000000e, 0x00000045, 0x00000001, + 0x00040020, 0x00000047, 0x00000002, 0x00000009, 0x00040020, 0x0000004a, + 0x00000007, 0x00000021, 0x0004002b, 0x00000006, 0x0000005b, 0x3f000000, + 0x0004002b, 0x00000006, 0x00000067, 0x00000000, 0x0004002b, 0x0000000a, + 0x00000073, 0x00000002, 0x00040020, 0x00000084, 0x00000003, 0x00000009, + 0x0004003b, 0x00000084, 0x00000085, 0x00000003, 0x00050036, 0x00000002, + 0x00000004, 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x0004003b, + 0x00000007, 0x00000008, 0x00000007, 0x0004003b, 0x00000007, 0x00000016, + 0x00000007, 0x0004003b, 0x00000007, 0x0000002a, 0x00000007, 0x0004003b, + 0x0000002d, 0x0000002e, 0x00000007, 0x0004003b, 0x00000035, 0x00000036, + 0x00000007, 0x0004003b, 0x00000043, 0x00000044, 0x00000007, 0x0004003b, + 0x0000004a, 0x0000004b, 0x00000007, 0x0004003b, 0x00000043, 0x00000050, + 0x00000007, 0x0004003b, 0x00000007, 0x00000056, 0x00000007, 0x0004003b, + 0x00000007, 0x0000005f, 0x00000007, 0x0004003b, 0x00000007, 0x00000063, + 0x00000007, 0x00050041, 0x00000013, 0x00000014, 0x00000011, 0x00000012, + 0x0004003d, 0x00000006, 0x00000015, 0x00000014, 0x0003003e, 0x00000008, + 0x00000015, 0x0004003d, 0x00000017, 0x0000001a, 0x00000019, 0x0004003d, + 0x0000001b, 0x0000001e, 0x0000001d, 0x00050056, 0x0000001f, 0x00000020, + 0x0000001a, 0x0000001e, 0x0004003d, 0x00000021, 0x00000024, 0x00000023, + 0x00050057, 0x00000009, 0x00000025, 0x00000020, 0x00000024, 0x00050051, + 0x00000006, 0x00000027, 0x00000025, 0x00000003, 0x00050085, 0x00000006, + 0x00000029, 0x00000027, 0x00000028, 0x0003003e, 0x00000016, 0x00000029, + 0x0003003e, 0x0000002a, 0x0000002b, 0x0004003d, 0x00000017, 0x0000002f, + 0x00000019, 0x0004003d, 0x0000001b, 0x00000030, 0x0000001d, 0x00050056, + 0x0000001f, 0x00000031, 0x0000002f, 0x00000030, 0x0004003d, 0x00000021, + 0x00000032, 0x00000023, 0x00050057, 0x00000009, 0x00000033, 0x00000031, + 0x00000032, 0x0008004f, 0x0000002c, 0x00000034, 0x00000033, 0x00000033, + 0x00000000, 0x00000001, 0x00000002, 0x0003003e, 0x0000002e, 0x00000034, + 0x0003003e, 0x00000036, 0x00000012, 0x000200f9, 0x00000037, 0x000200f8, + 0x00000037, 0x000400f6, 0x00000039, 0x0000003a, 0x00000000, 0x000200f9, + 0x0000003b, 0x000200f8, 0x0000003b, 0x0004003d, 0x0000000e, 0x0000003c, + 0x00000036, 0x00050041, 0x0000003e, 0x0000003f, 0x00000011, 0x0000003d, + 0x0004003d, 0x0000000e, 0x00000040, 0x0000003f, 0x000500b1, 0x00000041, + 0x00000042, 0x0000003c, 0x00000040, 0x000400fa, 0x00000042, 0x00000038, + 0x00000039, 0x000200f8, 0x00000038, 0x0004003d, 0x0000000e, 0x00000046, + 0x00000036, 0x00070041, 0x00000047, 0x00000048, 0x00000011, 0x00000045, + 0x00000012, 0x00000046, 0x0004003d, 0x00000009, 0x00000049, 0x00000048, + 0x0003003e, 0x00000044, 0x00000049, 0x0004003d, 0x00000021, 0x0000004c, + 0x00000023, 0x0004003d, 0x00000009, 0x0000004d, 0x00000044, 0x0007004f, + 0x00000021, 0x0000004e, 0x0000004d, 0x0000004d, 0x00000000, 0x00000001, + 0x00050081, 0x00000021, 0x0000004f, 0x0000004c, 0x0000004e, 0x0003003e, + 0x0000004b, 0x0000004f, 0x0004003d, 0x00000017, 0x00000051, 0x00000019, + 0x0004003d, 0x0000001b, 0x00000052, 0x0000001d, 0x00050056, 0x0000001f, + 0x00000053, 0x00000051, 0x00000052, 0x0004003d, 0x00000021, 0x00000054, + 0x0000004b, 0x00050057, 0x00000009, 0x00000055, 0x00000053, 0x00000054, + 0x0003003e, 0x00000050, 0x00000055, 0x0004003d, 0x00000017, 0x00000057, + 0x00000019, 0x0004003d, 0x0000001b, 0x00000058, 0x0000001d, 0x00050056, + 0x0000001f, 0x00000059, 0x00000057, 0x00000058, 0x0004003d, 0x00000021, + 0x0000005a, 0x0000004b, 0x0005008e, 0x00000021, 0x0000005c, 0x0000005a, + 0x0000005b, 0x00050057, 0x00000009, 0x0000005d, 0x00000059, 0x0000005c, + 0x00050051, 0x00000006, 0x0000005e, 0x0000005d, 0x00000003, 0x0003003e, + 0x00000056, 0x0000005e, 0x0004003d, 0x00000006, 0x00000060, 0x00000056, + 0x0004003d, 0x00000006, 0x00000061, 0x00000016, 0x000500ba, 0x00000041, + 0x00000062, 0x00000060, 0x00000061, 0x000300f7, 0x00000065, 0x00000000, + 0x000400fa, 0x00000062, 0x00000064, 0x0000006a, 0x000200f8, 0x00000064, + 0x0004003d, 0x00000006, 0x00000066, 0x00000056, 0x0004003d, 0x00000006, + 0x00000068, 0x00000016, 0x0008000c, 0x00000006, 0x00000069, 0x00000001, + 0x0000002b, 0x00000066, 0x00000067, 0x00000068, 0x0003003e, 0x00000063, + 0x00000069, 0x000200f9, 0x00000065, 0x000200f8, 0x0000006a, 0x0004003d, + 0x00000006, 0x0000006b, 0x00000056, 0x0003003e, 0x00000063, 0x0000006b, + 0x000200f9, 0x00000065, 0x000200f8, 0x00000065, 0x0004003d, 0x00000006, + 0x0000006c, 0x00000063, 0x0003003e, 0x0000005f, 0x0000006c, 0x0004003d, + 0x0000002c, 0x0000006d, 0x0000002e, 0x0004003d, 0x00000006, 0x0000006e, + 0x0000002a, 0x00060050, 0x0000002c, 0x0000006f, 0x0000006e, 0x0000006e, + 0x0000006e, 0x00050088, 0x0000002c, 0x00000070, 0x0000006d, 0x0000006f, + 0x0004003d, 0x00000009, 0x00000071, 0x00000050, 0x0008004f, 0x0000002c, + 0x00000072, 0x00000071, 0x00000071, 0x00000000, 0x00000001, 0x00000002, + 0x00050041, 0x00000007, 0x00000074, 0x00000044, 0x00000073, 0x0004003d, + 0x00000006, 0x00000075, 0x00000074, 0x00050041, 0x00000007, 0x00000076, + 0x00000044, 0x00000026, 0x0004003d, 0x00000006, 0x00000077, 0x00000076, + 0x0004003d, 0x00000006, 0x00000078, 0x0000005f, 0x0004003d, 0x00000006, + 0x00000079, 0x00000008, 0x00050085, 0x00000006, 0x0000007a, 0x00000078, + 0x00000079, 0x0008000c, 0x00000006, 0x0000007b, 0x00000001, 0x00000031, + 0x00000075, 0x00000077, 0x0000007a, 0x00060050, 0x0000002c, 0x0000007c, + 0x0000007b, 0x0000007b, 0x0000007b, 0x0008000c, 0x0000002c, 0x0000007d, + 0x00000001, 0x0000002e, 0x00000070, 0x00000072, 0x0000007c, 0x0004003d, + 0x0000002c, 0x0000007e, 0x0000002e, 0x00050081, 0x0000002c, 0x0000007f, + 0x0000007e, 0x0000007d, 0x0003003e, 0x0000002e, 0x0000007f, 0x0004003d, + 0x00000006, 0x00000080, 0x0000002a, 0x00050081, 0x00000006, 0x00000081, + 0x00000080, 0x0000002b, 0x0003003e, 0x0000002a, 0x00000081, 0x000200f9, + 0x0000003a, 0x000200f8, 0x0000003a, 0x0004003d, 0x0000000e, 0x00000082, + 0x00000036, 0x00050080, 0x0000000e, 0x00000083, 0x00000082, 0x00000045, + 0x0003003e, 0x00000036, 0x00000083, 0x000200f9, 0x00000037, 0x000200f8, + 0x00000039, 0x0004003d, 0x0000002c, 0x00000086, 0x0000002e, 0x0004003d, + 0x00000006, 0x00000087, 0x0000002a, 0x00060050, 0x0000002c, 0x00000088, + 0x00000087, 0x00000087, 0x00000087, 0x00050088, 0x0000002c, 0x00000089, + 0x00000086, 0x00000088, 0x00050051, 0x00000006, 0x0000008a, 0x00000089, + 0x00000000, 0x00050051, 0x00000006, 0x0000008b, 0x00000089, 0x00000001, + 0x00050051, 0x00000006, 0x0000008c, 0x00000089, 0x00000002, 0x00070050, + 0x00000009, 0x0000008d, 0x0000008a, 0x0000008b, 0x0000008c, 0x0000002b, + 0x0003003e, 0x00000085, 0x0000008d, 0x000100fd, 0x00010038, +}; diff --git a/src/asahi/vulkan/tests/vulkan_ubo_record_loop_split.frag b/src/asahi/vulkan/tests/vulkan_ubo_record_loop_split.frag new file mode 100644 index 000000000000..2df09e944e5b --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_ubo_record_loop_split.frag @@ -0,0 +1,37 @@ +/* Copyright 2026 Aurora Silicon + * SPDX-License-Identifier: MIT + * + * Counter-Strike 2's depth-of-field gather (its tap loop, as decompiled from + * the game's SPIR-V): it reads each tap record's members in separate + * expressions. + */ +#version 450 +/* Counter-Strike 2's layout: the array inside a struct, the scalars around + * it at explicit offsets. */ +struct Records { vec4 v[256]; }; +layout(set = 0, binding = 0, std140) uniform Taps +{ + layout(offset = 20) float scale; + layout(offset = 48) Records tap; + layout(offset = 4144) int count; +} u; +layout(set = 0, binding = 1) uniform texture2D tex; +layout(set = 0, binding = 2) uniform sampler smp; +layout(location = 0) in vec2 uv; +layout(location = 0) out vec4 o; +void main() +{ + float limit = texture(sampler2D(tex, smp), uv).a * 2.0; + float n = 1.0; + vec3 sum = texture(sampler2D(tex, smp), uv).rgb; + for (int i = 0; i < u.count; i++) { + vec2 p = uv + u.tap.v[i].xy; + vec4 c = texture(sampler2D(tex, smp), p); + float coc = texture(sampler2D(tex, smp), p * 0.5).a; + float clamped = coc > limit ? clamp(coc, 0.0, limit) : coc; + sum += mix(sum / vec3(n), c.rgb, + vec3(smoothstep(u.tap.v[i].z, u.tap.v[i].w, clamped * u.scale))); + n += 1.0; + } + o = vec4(sum / vec3(n), 1.0); +} diff --git a/src/asahi/vulkan/tests/vulkan_ubo_record_loop_whole.frag b/src/asahi/vulkan/tests/vulkan_ubo_record_loop_whole.frag new file mode 100644 index 000000000000..79fb8248c739 --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_ubo_record_loop_whole.frag @@ -0,0 +1,38 @@ +/* Copyright 2026 Aurora Silicon + * SPDX-License-Identifier: MIT + * + * The same loop with each tap record read once and the scale read before + * the loop. + */ +#version 450 +/* Counter-Strike 2's layout: the array inside a struct, the scalars around + * it at explicit offsets. */ +struct Records { vec4 v[256]; }; +layout(set = 0, binding = 0, std140) uniform Taps +{ + layout(offset = 20) float scale; + layout(offset = 48) Records tap; + layout(offset = 4144) int count; +} u; +layout(set = 0, binding = 1) uniform texture2D tex; +layout(set = 0, binding = 2) uniform sampler smp; +layout(location = 0) in vec2 uv; +layout(location = 0) out vec4 o; +void main() +{ + const float scale = u.scale; + float limit = texture(sampler2D(tex, smp), uv).a * 2.0; + float n = 1.0; + vec3 sum = texture(sampler2D(tex, smp), uv).rgb; + for (int i = 0; i < u.count; i++) { + const vec4 t = u.tap.v[i]; + vec2 p = uv + t.xy; + vec4 c = texture(sampler2D(tex, smp), p); + float coc = texture(sampler2D(tex, smp), p * 0.5).a; + float clamped = coc > limit ? clamp(coc, 0.0, limit) : coc; + sum += mix(sum / vec3(n), c.rgb, + vec3(smoothstep(t.z, t.w, clamped * scale))); + n += 1.0; + } + o = vec4(sum / vec3(n), 1.0); +} From 54e229eecd7ec8743e83a662b2ec55dc87312427 Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Tue, 27 Oct 2026 23:53:27 -0300 Subject: [PATCH 09/19] hk: keep multisample fragment variants in the disk cache A G17P fragment shader whose coverage or exports depend on the sample count (sample shading, discard or sample-mask writes, a read of the sample count), compiled without a fixed count -- a dynamic rasterization sample count or a pipeline library -- builds its multisample variant at the first draw that needs it (hk_fs_build_sample_variant). By then its shader object is already in the pipeline cache, which holds the variant as absent, so every process compiled the variant again, on the thread recording the draw. Under FEX that is a stall in the middle of play: Counter-Strike 2 renders with 2x MSAA and alpha-tested materials, and on a warm cache it still compiled about 40 such variants per de_dust2 session, 2.2 s of compiling that landed in its 200-400 ms in-game freezes. Store each variant that a draw builds in the disk cache, keyed by the saved source and compile inputs that already describe it (NIR, key, flags, robustness, features, set layouts) and the sample count, in the form hk_api_shader_serialize() writes a variant, inline-blend NIR included. A draw looks the variant up there before compiling it; an entry that does not deserialize cleanly is dropped and the variant compiled. On a warm cache Counter-Strike 2 now compiles 5 such variants per session instead of 40 (0.7 s instead of 2.2 s). tests/vulkan_sample_variant_cache.c records the same 2x MSAA draw from two instances on an empty cache directory: 2.4 ms then 0.4 ms with the variant loaded, the same image; without this change 2.2 ms then 1.5-1.9 ms, compiling it again. 535 dEQP-VK multisample, alpha-to-coverage and dynamic-sample-count cases (monolithic, library and shader-object pipelines), run twice on a fresh cache directory: same results as before, cold and warm, 276 pass, 0 fail. Signed-off-by: Jonathan Goyvaerts Assisted-by: LLM (cherry picked from commit d4b5dce10fe5d6e8a97e402711fd2e8332f39af6) --- src/asahi/vulkan/hk_shader.c | 125 ++++++- src/asahi/vulkan/meson.build | 9 + .../tests/vulkan_sample_variant_cache.c | 332 ++++++++++++++++++ .../tests/vulkan_sample_variant_cache.frag | 16 + .../tests/vulkan_sample_variant_cache.vert | 6 + .../vulkan_sample_variant_cache_shaders.h | 101 ++++++ 6 files changed, 583 insertions(+), 6 deletions(-) create mode 100644 src/asahi/vulkan/tests/vulkan_sample_variant_cache.c create mode 100644 src/asahi/vulkan/tests/vulkan_sample_variant_cache.frag create mode 100644 src/asahi/vulkan/tests/vulkan_sample_variant_cache.vert create mode 100644 src/asahi/vulkan/tests/vulkan_sample_variant_cache_shaders.h diff --git a/src/asahi/vulkan/hk_shader.c b/src/asahi/vulkan/hk_shader.c index f28cfb721676..cad568db85c9 100644 --- a/src/asahi/vulkan/hk_shader.c +++ b/src/asahi/vulkan/hk_shader.c @@ -93,6 +93,7 @@ #include "util/blob.h" #include "util/hash_table.h" #include "util/macros.h" +#include "util/disk_cache.h" #include "util/mesa-blake3.h" #include "util/os_time.h" #include "util/simple_mtx.h" @@ -5507,6 +5508,108 @@ hk_compile_fs_sample_variant(struct hk_device *dev, return result; } +static VkResult hk_deserialize_shader(struct hk_device *dev, + struct blob_reader *blob, + struct hk_shader *shader, bool absent, + bool cache_candidate); +static void hk_shader_serialize(struct vk_device *vk_dev, + const struct hk_shader *shader, + struct blob *blob); + +/* A multisample variant is built at its first draw, after its shader object + * went into the pipeline cache, so the cached object never holds it and every + * process compiled it again on the draw that needs it. Keep each built variant + * in the disk cache under its own key: the saved source and compile inputs, + * which name its NIR and key, and the sample count. */ +static bool +hk_fs_sample_variant_cache_key(struct hk_device *dev, + const struct hk_fs_sample_source *src, + unsigned samples, cache_key out) +{ + struct disk_cache *cache = hk_device_physical(dev)->vk.disk_cache; + if (!cache) + return false; + struct mesa_blake3 ctx; + blake3_hash hash; + _mesa_blake3_init(&ctx); + static const char tag[] = "hk fs sample variant"; + _mesa_blake3_update(&ctx, tag, sizeof(tag)); + _mesa_blake3_update(&ctx, &samples, sizeof(samples)); + _mesa_blake3_update(&ctx, &src->key, sizeof(src->key)); + _mesa_blake3_update(&ctx, &src->flags, sizeof(src->flags)); + _mesa_blake3_update(&ctx, &src->robustness, sizeof(src->robustness)); + _mesa_blake3_update(&ctx, &src->features, sizeof(src->features)); + _mesa_blake3_update(&ctx, &src->set_layout_count, + sizeof(src->set_layout_count)); + _mesa_blake3_update(&ctx, &src->static_set_mask, + sizeof(src->static_set_mask)); + _mesa_blake3_update(&ctx, src->nir, src->nir_size); + _mesa_blake3_final(&ctx, hash); + disk_cache_compute_key(cache, hash, sizeof(hash), out); + return true; +} + +/* The variant and the NIR its inline blending is rebuilt from at fast-link, + * as hk_api_shader_serialize() writes them for one variant. */ +static bool +hk_fs_sample_variant_load(struct hk_device *dev, struct hk_shader *shader, + const cache_key key) +{ + size_t size; + void *data = disk_cache_get(hk_device_physical(dev)->vk.disk_cache, key, + &size); + if (!data) + return false; + struct blob_reader blob; + blob_reader_init(&blob, data, size); + bool ok = hk_deserialize_shader(dev, &blob, shader, false, false) == + VK_SUCCESS; + const uint32_t mono_size = ok ? blob_read_uint32(&blob) : 0; + if (ok && mono_size) { + const void *mono_nir = blob_read_bytes(&blob, mono_size); + const void *mono_key = + blob_read_bytes(&blob, sizeof(shader->g17p_mono_key)); + shader->g17p_mono_nir = blob.overrun ? NULL : malloc(mono_size); + ok = shader->g17p_mono_nir != NULL; + if (ok) { + memcpy(shader->g17p_mono_nir, mono_nir, mono_size); + shader->g17p_mono_nir_size = mono_size; + memcpy(&shader->g17p_mono_key, mono_key, + sizeof(shader->g17p_mono_key)); + } + } + ok &= !blob.overrun && blob.current == blob.end; + free(data); + if (!ok) { + /* A stale or damaged entry: compile instead, from a clean variant. */ + hk_shader_destroy(dev, shader, true); + memset(shader, 0, sizeof(*shader)); + } + return ok; +} + +static void +hk_fs_sample_variant_store(struct hk_device *dev, + const struct hk_shader *shader, + const cache_key key) +{ + struct blob blob; + blob_init(&blob); + hk_shader_serialize(&dev->vk, shader, &blob); + const uint32_t mono_size = shader->g17p_mono_nir ? + shader->g17p_mono_nir_size : 0; + blob_write_uint32(&blob, mono_size); + if (mono_size) { + blob_write_bytes(&blob, shader->g17p_mono_nir, mono_size); + blob_write_bytes(&blob, &shader->g17p_mono_key, + sizeof(shader->g17p_mono_key)); + } + if (!blob.out_of_memory) + disk_cache_put(hk_device_physical(dev)->vk.disk_cache, key, blob.data, + blob.size, NULL); + blob_finish(&blob); +} + VkResult hk_fs_build_sample_variant(struct hk_device *dev, struct hk_api_shader *obj, unsigned samples) @@ -5522,14 +5625,24 @@ hk_fs_build_sample_variant(struct hk_device *dev, struct hk_api_shader *obj, VkResult result = VK_SUCCESS; simple_mtx_lock(&src->lock); if (!(src->ready_mask & BITFIELD_BIT(i))) { - struct blob_reader reader; - blob_reader_init(&reader, src->nir, src->nir_size); - nir_shader *nir = nir_deserialize(NULL, &agx_nir_options, &reader); - if (nir) { + cache_key key; + const bool cacheable = + hk_fs_sample_variant_cache_key(dev, src, samples, key); + if (cacheable && hk_fs_sample_variant_load(dev, &obj->variants[i], key)) { src->owned_mask |= BITFIELD_BIT(i); - result = hk_compile_fs_sample_variant(dev, NULL, nir, obj, samples); + p_atomic_set(&src->ready_mask, src->ready_mask | BITFIELD_BIT(i)); } else { - result = VK_ERROR_OUT_OF_HOST_MEMORY; + struct blob_reader reader; + blob_reader_init(&reader, src->nir, src->nir_size); + nir_shader *nir = nir_deserialize(NULL, &agx_nir_options, &reader); + if (nir) { + src->owned_mask |= BITFIELD_BIT(i); + result = hk_compile_fs_sample_variant(dev, NULL, nir, obj, samples); + if (result == VK_SUCCESS && cacheable) + hk_fs_sample_variant_store(dev, &obj->variants[i], key); + } else { + result = VK_ERROR_OUT_OF_HOST_MEMORY; + } } } simple_mtx_unlock(&src->lock); diff --git a/src/asahi/vulkan/meson.build b/src/asahi/vulkan/meson.build index 86b12bbe3886..5d85518e7ebd 100644 --- a/src/asahi/vulkan/meson.build +++ b/src/asahi/vulkan/meson.build @@ -704,6 +704,15 @@ if with_tests gnu_symbol_visibility : 'hidden', ) + executable( + 'vulkan-sample-variant-cache', + files('tests/vulkan_sample_variant_cache.c'), + include_directories : [inc_include], + dependencies : [cc.find_library('vulkan')], + c_args : [c_msvc_compat_args], + gnu_symbol_visibility : 'hidden', + ) + executable( 'g17p-vulkan-multiview-clear', files('tests/g17p_vulkan_multiview_clear.c'), diff --git a/src/asahi/vulkan/tests/vulkan_sample_variant_cache.c b/src/asahi/vulkan/tests/vulkan_sample_variant_cache.c new file mode 100644 index 000000000000..9876970687cf --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_sample_variant_cache.c @@ -0,0 +1,332 @@ +/* Copyright 2026 Aurora Silicon + * SPDX-License-Identifier: MIT + * + * A pipeline with a dynamic sample count and an alpha-tested fragment shader, + * as games build them: the driver compiles the shader's multisample variant + * at the first multisampled draw (hk_fs_build_sample_variant), in the middle + * of recording a frame. That variant must go to the disk cache, so that the + * next process that draws with the shader loads it instead of compiling it + * again. + * + * The test starts from an empty cache directory and records the same 2x MSAA + * draw from two Vulkan instances in turn, timing the command buffer + * recording and reading back the resolved image. The second recording must + * take a fraction of the first (it loads the variant) and render the same + * image. On the M4 Pro: 3.9 ms against 0.56 ms; a driver that compiles the + * variant again takes 2.7 ms against 1.5 ms. + * + * Usage: vulkan-sample-variant-cache Exit status 0 = pass. + */ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include + +#include "vulkan_sample_variant_cache_shaders.h" + +#define CHECK(call) \ + do { \ + VkResult r = (call); \ + if (r != VK_SUCCESS) { \ + fprintf(stderr, "%s: %d\n", #call, r); \ + exit(2); \ + } \ + } while (0) + +#define W 256 +#define H 256 + +static VkPhysicalDevice pdev; +static VkDevice dev; +static VkDeviceMemory image_mem[2]; +static unsigned image_count; + +static uint32_t memory_type(uint32_t bits, VkMemoryPropertyFlags flags) { + VkPhysicalDeviceMemoryProperties m; + vkGetPhysicalDeviceMemoryProperties(pdev, &m); + for (uint32_t i = 0; i < m.memoryTypeCount; i++) + if ((bits & (1u << i)) && (m.memoryTypes[i].propertyFlags & flags) == flags) + return i; + fprintf(stderr, "no memory type\n"); + exit(2); +} + +static VkImageView image(VkSampleCountFlagBits samples, VkImageUsageFlags usage, + VkImage *out) { + CHECK(vkCreateImage(dev, &(VkImageCreateInfo){ + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, + .format = VK_FORMAT_R8G8B8A8_UNORM, .extent = {W, H, 1}, .mipLevels = 1, + .arrayLayers = 1, .samples = samples, .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = usage}, NULL, out)); + VkMemoryRequirements mr; + vkGetImageMemoryRequirements(dev, *out, &mr); + VkDeviceMemory mem; + CHECK(vkAllocateMemory(dev, &(VkMemoryAllocateInfo){ + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = mr.size, + .memoryTypeIndex = memory_type(mr.memoryTypeBits, + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)}, + NULL, &mem)); + CHECK(vkBindImageMemory(dev, *out, mem, 0)); + image_mem[image_count++ % 2] = mem; + VkImageView view; + CHECK(vkCreateImageView(dev, &(VkImageViewCreateInfo){ + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = *out, + .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = VK_FORMAT_R8G8B8A8_UNORM, + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}, NULL, &view)); + return view; +} + +static VkShaderModule module(const uint32_t *code, size_t size) { + VkShaderModule m; + CHECK(vkCreateShaderModule(dev, &(VkShaderModuleCreateInfo){ + .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = size, + .pCode = code}, NULL, &m)); + return m; +} + +/* One instance: build the pipeline, record and run the draw, return the + * recording time and a hash of the resolved image. */ +static void run(double *record_ms, uint64_t *hash) { + VkInstance inst; + CHECK(vkCreateInstance(&(VkInstanceCreateInfo){ + .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, + .pApplicationInfo = &(VkApplicationInfo){ + .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, + .apiVersion = VK_API_VERSION_1_3}}, NULL, &inst)); + uint32_t n = 1; + vkEnumeratePhysicalDevices(inst, &n, &pdev); + CHECK(vkCreateDevice(pdev, &(VkDeviceCreateInfo){ + .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, + .pNext = &(VkPhysicalDeviceVulkan13Features){ + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES, + .dynamicRendering = VK_TRUE, + .pNext = &(VkPhysicalDeviceExtendedDynamicState3FeaturesEXT){ + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_3_FEATURES_EXT, + .extendedDynamicState3RasterizationSamples = VK_TRUE}}, + .enabledExtensionCount = 1, + .ppEnabledExtensionNames = (const char *[]){ + VK_EXT_EXTENDED_DYNAMIC_STATE_3_EXTENSION_NAME}, + .queueCreateInfoCount = 1, + .pQueueCreateInfos = &(VkDeviceQueueCreateInfo){ + .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, .queueCount = 1, + .pQueuePriorities = (float[]){1}}}, NULL, &dev)); + VkQueue q; + vkGetDeviceQueue(dev, 0, 0, &q); + PFN_vkCmdSetRasterizationSamplesEXT set_samples = + (PFN_vkCmdSetRasterizationSamplesEXT)vkGetDeviceProcAddr( + dev, "vkCmdSetRasterizationSamplesEXT"); + + VkImage ms_img, rs_img; + VkImageView ms_view = image(VK_SAMPLE_COUNT_2_BIT, + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, &ms_img); + VkImageView rs_view = image(VK_SAMPLE_COUNT_1_BIT, + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_TRANSFER_SRC_BIT, &rs_img); + + VkPipelineLayout pl; + CHECK(vkCreatePipelineLayout(dev, &(VkPipelineLayoutCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .pushConstantRangeCount = 1, + .pPushConstantRanges = &(VkPushConstantRange){ + VK_SHADER_STAGE_FRAGMENT_BIT, 0, 4}}, NULL, &pl)); + VkShaderModule vs = module(sample_variant_cache_vert, + sizeof(sample_variant_cache_vert)); + VkShaderModule fs = module(sample_variant_cache_frag, + sizeof(sample_variant_cache_frag)); + const VkFormat fmt = VK_FORMAT_R8G8B8A8_UNORM; + VkPipeline pipe; + CHECK(vkCreateGraphicsPipelines(dev, VK_NULL_HANDLE, 1, &(VkGraphicsPipelineCreateInfo){ + .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, + .pNext = &(VkPipelineRenderingCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO, + .colorAttachmentCount = 1, .pColorAttachmentFormats = &fmt}, + .stageCount = 2, + .pStages = (VkPipelineShaderStageCreateInfo[]){ + {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, NULL, 0, + VK_SHADER_STAGE_VERTEX_BIT, vs, "main", NULL}, + {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, NULL, 0, + VK_SHADER_STAGE_FRAGMENT_BIT, fs, "main", NULL}}, + .pVertexInputState = &(VkPipelineVertexInputStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO}, + .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, + .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST}, + .pViewportState = &(VkPipelineViewportStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, + .viewportCount = 1, .pViewports = &(VkViewport){0, 0, W, H, 0, 1}, + .scissorCount = 1, .pScissors = &(VkRect2D){{0, 0}, {W, H}}}, + .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, + .lineWidth = 1}, + .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, + .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT}, + .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, + .attachmentCount = 1, + .pAttachments = &(VkPipelineColorBlendAttachmentState){ + .colorWriteMask = 0xf}}, + .pDynamicState = &(VkPipelineDynamicStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, + .dynamicStateCount = 1, + .pDynamicStates = (VkDynamicState[]){ + VK_DYNAMIC_STATE_RASTERIZATION_SAMPLES_EXT}}, + .layout = pl}, NULL, &pipe)); + + VkBuffer rb; + CHECK(vkCreateBuffer(dev, &(VkBufferCreateInfo){ + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = W * H * 4, + .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT}, NULL, &rb)); + VkMemoryRequirements mr; + vkGetBufferMemoryRequirements(dev, rb, &mr); + VkDeviceMemory rmem; + CHECK(vkAllocateMemory(dev, &(VkMemoryAllocateInfo){ + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = mr.size, + .memoryTypeIndex = memory_type(mr.memoryTypeBits, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)}, + NULL, &rmem)); + CHECK(vkBindBufferMemory(dev, rb, rmem, 0)); + + VkCommandPool cp; + CHECK(vkCreateCommandPool(dev, &(VkCommandPoolCreateInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO}, NULL, &cp)); + VkCommandBuffer cb; + CHECK(vkAllocateCommandBuffers(dev, &(VkCommandBufferAllocateInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, .commandPool = cp, + .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, .commandBufferCount = 1}, &cb)); + + struct timespec t0, t1; + clock_gettime(CLOCK_MONOTONIC, &t0); + CHECK(vkBeginCommandBuffer(cb, &(VkCommandBufferBeginInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO})); + VkImageMemoryBarrier2 to_attachment[2]; + for (int i = 0; i < 2; i++) + to_attachment[i] = (VkImageMemoryBarrier2){ + .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, + .dstStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, + .dstAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT, + .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + .image = i ? rs_img : ms_img, + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}; + vkCmdPipelineBarrier2(cb, &(VkDependencyInfo){ + .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 2, + .pImageMemoryBarriers = to_attachment}); + vkCmdBeginRendering(cb, &(VkRenderingInfo){ + .sType = VK_STRUCTURE_TYPE_RENDERING_INFO, .renderArea = {{0, 0}, {W, H}}, + .layerCount = 1, .colorAttachmentCount = 1, + .pColorAttachments = &(VkRenderingAttachmentInfo){ + .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, + .imageView = ms_view, + .imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + .resolveMode = VK_RESOLVE_MODE_AVERAGE_BIT, + .resolveImageView = rs_view, + .resolveImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, + .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, + .clearValue.color.float32 = {0.1f, 0.2f, 0.3f, 1}}}); + vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe); + set_samples(cb, VK_SAMPLE_COUNT_2_BIT); + const float k = 0.37f; + vkCmdPushConstants(cb, pl, VK_SHADER_STAGE_FRAGMENT_BIT, 0, 4, &k); + vkCmdDraw(cb, 3, 1, 0, 0); + vkCmdEndRendering(cb); + vkCmdPipelineBarrier2(cb, &(VkDependencyInfo){ + .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 1, + .pImageMemoryBarriers = &(VkImageMemoryBarrier2){ + .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, + .srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, + .srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_COPY_BIT, + .dstAccessMask = VK_ACCESS_2_TRANSFER_READ_BIT, + .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + .newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, .image = rs_img, + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}}); + vkCmdCopyImageToBuffer(cb, rs_img, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, rb, 1, + &(VkBufferImageCopy){ + .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, + .imageExtent = {W, H, 1}}); + CHECK(vkEndCommandBuffer(cb)); + clock_gettime(CLOCK_MONOTONIC, &t1); + *record_ms = (t1.tv_sec - t0.tv_sec) * 1e3 + (t1.tv_nsec - t0.tv_nsec) / 1e6; + + VkFence fence; + CHECK(vkCreateFence(dev, &(VkFenceCreateInfo){ + .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}, NULL, &fence)); + CHECK(vkQueueSubmit(q, 1, &(VkSubmitInfo){ + .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .commandBufferCount = 1, + .pCommandBuffers = &cb}, fence)); + CHECK(vkWaitForFences(dev, 1, &fence, VK_TRUE, UINT64_MAX)); + + const uint8_t *px; + CHECK(vkMapMemory(dev, rmem, 0, VK_WHOLE_SIZE, 0, (void **)&px)); + uint64_t h = 1469598103934665603ull; + unsigned discarded = 0; + for (unsigned i = 0; i < W * H * 4; i++) + h = (h ^ px[i]) * 1099511628211ull; + for (unsigned i = 0; i < W * H; i++) + discarded += abs(px[4 * i] - 26) <= 1 && abs(px[4 * i + 1] - 51) <= 1 && + abs(px[4 * i + 2] - 77) <= 1; + *hash = h; + if (discarded == 0 || discarded == W * H) { + fprintf(stderr, "the draw does not mix kept and discarded pixels (%u)\n", + discarded); + exit(1); + } + vkUnmapMemory(dev, rmem); + vkDestroyFence(dev, fence, NULL); + vkDestroyCommandPool(dev, cp, NULL); + vkDestroyBuffer(dev, rb, NULL); + vkFreeMemory(dev, rmem, NULL); + vkDestroyPipeline(dev, pipe, NULL); + vkDestroyShaderModule(dev, vs, NULL); + vkDestroyShaderModule(dev, fs, NULL); + vkDestroyPipelineLayout(dev, pl, NULL); + vkDestroyImageView(dev, ms_view, NULL); + vkDestroyImageView(dev, rs_view, NULL); + vkDestroyImage(dev, ms_img, NULL); + vkDestroyImage(dev, rs_img, NULL); + for (unsigned i = 0; i < 2; i++) + vkFreeMemory(dev, image_mem[i], NULL); + /* Destroying the device and instance flushes the disk cache writes. */ + vkDestroyDevice(dev, NULL); + vkDestroyInstance(inst, NULL); +} + +int main(void) { + char dir[] = "/tmp/hk-sample-variant-cache-XXXXXX"; + if (!mkdtemp(dir)) { + perror("mkdtemp"); + return 2; + } + setenv("MESA_SHADER_CACHE_DIR", dir, 1); + unsetenv("MESA_SHADER_CACHE_DISABLE"); + + double cold, warm; + uint64_t h0, h1; + run(&cold, &h0); + run(&warm, &h1); + printf("recording with the variant compiled: %.2f ms, from the cache: %.2f ms\n", + cold, warm); + + char cmd[96]; + snprintf(cmd, sizeof(cmd), "rm -rf %s", dir); + if (system(cmd) != 0) + fprintf(stderr, "could not remove %s\n", dir); + + if (h0 != h1) { + fprintf(stderr, "FAIL: the cached variant renders differently\n"); + return 1; + } + if (warm * 4 > cold) { + fprintf(stderr, "FAIL: the second recording compiled the variant again\n"); + return 1; + } + printf("PASS\n"); + return 0; +} diff --git a/src/asahi/vulkan/tests/vulkan_sample_variant_cache.frag b/src/asahi/vulkan/tests/vulkan_sample_variant_cache.frag new file mode 100644 index 000000000000..6dfe93050ba2 --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_sample_variant_cache.frag @@ -0,0 +1,16 @@ +#version 450 +/* An alpha-tested material: discard makes the shader's coverage depend on the + * sample count, so a dynamic-sample-count pipeline builds its multisample + * variant at the first draw. The arithmetic only makes compiling it take + * long enough to time. */ +layout(location = 0) out vec4 o; +layout(push_constant) uniform P { float k; } p; +void main() +{ + vec4 a = vec4(gl_FragCoord.xy * 0.004, p.k, 1.0); + for (int i = 0; i < 48; i++) + a = sin(a * 1.31 + vec4(float(i))) * cos(a.yzwx * 0.7) + a.wxyz * 0.1; + if (fract(gl_FragCoord.x * 0.05) > 0.5) + discard; + o = a * 0.5 + 0.5; +} diff --git a/src/asahi/vulkan/tests/vulkan_sample_variant_cache.vert b/src/asahi/vulkan/tests/vulkan_sample_variant_cache.vert new file mode 100644 index 000000000000..486932f113ce --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_sample_variant_cache.vert @@ -0,0 +1,6 @@ +#version 450 +void main() +{ + vec2 p = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); + gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0); +} diff --git a/src/asahi/vulkan/tests/vulkan_sample_variant_cache_shaders.h b/src/asahi/vulkan/tests/vulkan_sample_variant_cache_shaders.h new file mode 100644 index 000000000000..7e8411892f1a --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_sample_variant_cache_shaders.h @@ -0,0 +1,101 @@ +/* SPDX-License-Identifier: MIT + * Generated with glslangValidator -V from vulkan_sample_variant_cache.vert/.frag. + */ +#include +static const uint32_t sample_variant_cache_vert[] = { + 0x07230203,0x00010000,0x0008000b,0x0000002b,0x00000000,0x00020011,0x00000001,0x0006000b, + 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, + 0x0007000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000c,0x0000001d,0x00030003, + 0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,0x00000000,0x00030005,0x00000009, + 0x00000070,0x00060005,0x0000000c,0x565f6c67,0x65747265,0x646e4978,0x00007865,0x00060005, + 0x0000001b,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x0000001b,0x00000000, + 0x505f6c67,0x7469736f,0x006e6f69,0x00070006,0x0000001b,0x00000001,0x505f6c67,0x746e696f, + 0x657a6953,0x00000000,0x00070006,0x0000001b,0x00000002,0x435f6c67,0x4470696c,0x61747369, + 0x0065636e,0x00070006,0x0000001b,0x00000003,0x435f6c67,0x446c6c75,0x61747369,0x0065636e, + 0x00030005,0x0000001d,0x00000000,0x00040047,0x0000000c,0x0000000b,0x0000002a,0x00030047, + 0x0000001b,0x00000002,0x00050048,0x0000001b,0x00000000,0x0000000b,0x00000000,0x00050048, + 0x0000001b,0x00000001,0x0000000b,0x00000001,0x00050048,0x0000001b,0x00000002,0x0000000b, + 0x00000003,0x00050048,0x0000001b,0x00000003,0x0000000b,0x00000004,0x00020013,0x00000002, + 0x00030021,0x00000003,0x00000002,0x00030016,0x00000006,0x00000020,0x00040017,0x00000007, + 0x00000006,0x00000002,0x00040020,0x00000008,0x00000007,0x00000007,0x00040015,0x0000000a, + 0x00000020,0x00000001,0x00040020,0x0000000b,0x00000001,0x0000000a,0x0004003b,0x0000000b, + 0x0000000c,0x00000001,0x0004002b,0x0000000a,0x0000000e,0x00000001,0x0004002b,0x0000000a, + 0x00000010,0x00000002,0x00040017,0x00000017,0x00000006,0x00000004,0x00040015,0x00000018, + 0x00000020,0x00000000,0x0004002b,0x00000018,0x00000019,0x00000001,0x0004001c,0x0000001a, + 0x00000006,0x00000019,0x0006001e,0x0000001b,0x00000017,0x00000006,0x0000001a,0x0000001a, + 0x00040020,0x0000001c,0x00000003,0x0000001b,0x0004003b,0x0000001c,0x0000001d,0x00000003, + 0x0004002b,0x0000000a,0x0000001e,0x00000000,0x0004002b,0x00000006,0x00000020,0x40000000, + 0x0004002b,0x00000006,0x00000022,0x3f800000,0x0004002b,0x00000006,0x00000025,0x00000000, + 0x00040020,0x00000029,0x00000003,0x00000017,0x00050036,0x00000002,0x00000004,0x00000000, + 0x00000003,0x000200f8,0x00000005,0x0004003b,0x00000008,0x00000009,0x00000007,0x0004003d, + 0x0000000a,0x0000000d,0x0000000c,0x000500c4,0x0000000a,0x0000000f,0x0000000d,0x0000000e, + 0x000500c7,0x0000000a,0x00000011,0x0000000f,0x00000010,0x0004006f,0x00000006,0x00000012, + 0x00000011,0x0004003d,0x0000000a,0x00000013,0x0000000c,0x000500c7,0x0000000a,0x00000014, + 0x00000013,0x00000010,0x0004006f,0x00000006,0x00000015,0x00000014,0x00050050,0x00000007, + 0x00000016,0x00000012,0x00000015,0x0003003e,0x00000009,0x00000016,0x0004003d,0x00000007, + 0x0000001f,0x00000009,0x0005008e,0x00000007,0x00000021,0x0000001f,0x00000020,0x00050050, + 0x00000007,0x00000023,0x00000022,0x00000022,0x00050083,0x00000007,0x00000024,0x00000021, + 0x00000023,0x00050051,0x00000006,0x00000026,0x00000024,0x00000000,0x00050051,0x00000006, + 0x00000027,0x00000024,0x00000001,0x00070050,0x00000017,0x00000028,0x00000026,0x00000027, + 0x00000025,0x00000022,0x00050041,0x00000029,0x0000002a,0x0000001d,0x0000001e,0x0003003e, + 0x0000002a,0x00000028,0x000100fd,0x00010038 +}; +static const uint32_t sample_variant_cache_frag[] = { + 0x07230203,0x00010000,0x0008000b,0x00000051,0x00000000,0x00020011,0x00000001,0x0006000b, + 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, + 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000004c,0x00030010, + 0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d, + 0x00000000,0x00030005,0x00000009,0x00000061,0x00060005,0x0000000b,0x465f6c67,0x43676172, + 0x64726f6f,0x00000000,0x00030005,0x00000011,0x00000050,0x00040006,0x00000011,0x00000000, + 0x0000006b,0x00030005,0x00000013,0x00000070,0x00030005,0x0000001e,0x00000069,0x00030005, + 0x0000004c,0x0000006f,0x00040047,0x0000000b,0x0000000b,0x0000000f,0x00030047,0x00000011, + 0x00000002,0x00050048,0x00000011,0x00000000,0x00000023,0x00000000,0x00040047,0x0000004c, + 0x0000001e,0x00000000,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016, + 0x00000006,0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008, + 0x00000007,0x00000007,0x00040020,0x0000000a,0x00000001,0x00000007,0x0004003b,0x0000000a, + 0x0000000b,0x00000001,0x00040017,0x0000000c,0x00000006,0x00000002,0x0004002b,0x00000006, + 0x0000000f,0x3b83126f,0x0003001e,0x00000011,0x00000006,0x00040020,0x00000012,0x00000009, + 0x00000011,0x0004003b,0x00000012,0x00000013,0x00000009,0x00040015,0x00000014,0x00000020, + 0x00000001,0x0004002b,0x00000014,0x00000015,0x00000000,0x00040020,0x00000016,0x00000009, + 0x00000006,0x0004002b,0x00000006,0x00000019,0x3f800000,0x00040020,0x0000001d,0x00000007, + 0x00000014,0x0004002b,0x00000014,0x00000025,0x00000030,0x00020014,0x00000026,0x0004002b, + 0x00000006,0x00000029,0x3fa7ae14,0x0004002b,0x00000006,0x00000032,0x3f333333,0x0004002b, + 0x00000006,0x00000038,0x3dcccccd,0x0004002b,0x00000014,0x0000003c,0x00000001,0x00040015, + 0x0000003e,0x00000020,0x00000000,0x0004002b,0x0000003e,0x0000003f,0x00000000,0x00040020, + 0x00000040,0x00000001,0x00000006,0x0004002b,0x00000006,0x00000043,0x3d4ccccd,0x0004002b, + 0x00000006,0x00000046,0x3f000000,0x00040020,0x0000004b,0x00000003,0x00000007,0x0004003b, + 0x0000004b,0x0000004c,0x00000003,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003, + 0x000200f8,0x00000005,0x0004003b,0x00000008,0x00000009,0x00000007,0x0004003b,0x0000001d, + 0x0000001e,0x00000007,0x0004003d,0x00000007,0x0000000d,0x0000000b,0x0007004f,0x0000000c, + 0x0000000e,0x0000000d,0x0000000d,0x00000000,0x00000001,0x0005008e,0x0000000c,0x00000010, + 0x0000000e,0x0000000f,0x00050041,0x00000016,0x00000017,0x00000013,0x00000015,0x0004003d, + 0x00000006,0x00000018,0x00000017,0x00050051,0x00000006,0x0000001a,0x00000010,0x00000000, + 0x00050051,0x00000006,0x0000001b,0x00000010,0x00000001,0x00070050,0x00000007,0x0000001c, + 0x0000001a,0x0000001b,0x00000018,0x00000019,0x0003003e,0x00000009,0x0000001c,0x0003003e, + 0x0000001e,0x00000015,0x000200f9,0x0000001f,0x000200f8,0x0000001f,0x000400f6,0x00000021, + 0x00000022,0x00000000,0x000200f9,0x00000023,0x000200f8,0x00000023,0x0004003d,0x00000014, + 0x00000024,0x0000001e,0x000500b1,0x00000026,0x00000027,0x00000024,0x00000025,0x000400fa, + 0x00000027,0x00000020,0x00000021,0x000200f8,0x00000020,0x0004003d,0x00000007,0x00000028, + 0x00000009,0x0005008e,0x00000007,0x0000002a,0x00000028,0x00000029,0x0004003d,0x00000014, + 0x0000002b,0x0000001e,0x0004006f,0x00000006,0x0000002c,0x0000002b,0x00070050,0x00000007, + 0x0000002d,0x0000002c,0x0000002c,0x0000002c,0x0000002c,0x00050081,0x00000007,0x0000002e, + 0x0000002a,0x0000002d,0x0006000c,0x00000007,0x0000002f,0x00000001,0x0000000d,0x0000002e, + 0x0004003d,0x00000007,0x00000030,0x00000009,0x0009004f,0x00000007,0x00000031,0x00000030, + 0x00000030,0x00000001,0x00000002,0x00000003,0x00000000,0x0005008e,0x00000007,0x00000033, + 0x00000031,0x00000032,0x0006000c,0x00000007,0x00000034,0x00000001,0x0000000e,0x00000033, + 0x00050085,0x00000007,0x00000035,0x0000002f,0x00000034,0x0004003d,0x00000007,0x00000036, + 0x00000009,0x0009004f,0x00000007,0x00000037,0x00000036,0x00000036,0x00000003,0x00000000, + 0x00000001,0x00000002,0x0005008e,0x00000007,0x00000039,0x00000037,0x00000038,0x00050081, + 0x00000007,0x0000003a,0x00000035,0x00000039,0x0003003e,0x00000009,0x0000003a,0x000200f9, + 0x00000022,0x000200f8,0x00000022,0x0004003d,0x00000014,0x0000003b,0x0000001e,0x00050080, + 0x00000014,0x0000003d,0x0000003b,0x0000003c,0x0003003e,0x0000001e,0x0000003d,0x000200f9, + 0x0000001f,0x000200f8,0x00000021,0x00050041,0x00000040,0x00000041,0x0000000b,0x0000003f, + 0x0004003d,0x00000006,0x00000042,0x00000041,0x00050085,0x00000006,0x00000044,0x00000042, + 0x00000043,0x0006000c,0x00000006,0x00000045,0x00000001,0x0000000a,0x00000044,0x000500ba, + 0x00000026,0x00000047,0x00000045,0x00000046,0x000300f7,0x00000049,0x00000000,0x000400fa, + 0x00000047,0x00000048,0x00000049,0x000200f8,0x00000048,0x000100fc,0x000200f8,0x00000049, + 0x0004003d,0x00000007,0x0000004d,0x00000009,0x0005008e,0x00000007,0x0000004e,0x0000004d, + 0x00000046,0x00070050,0x00000007,0x0000004f,0x00000046,0x00000046,0x00000046,0x00000046, + 0x00050081,0x00000007,0x00000050,0x0000004e,0x0000004f,0x0003003e,0x0000004c,0x00000050, + 0x000100fd,0x00010038 +}; From 979927972e61b0e213ba43cc43a8df8eaaa6f016 Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Tue, 27 Oct 2026 23:57:57 -0300 Subject: [PATCH 10/19] hk: remember in the disk cache that a program emulates border colours Whether G17P shaders emulate custom border colours is decided at the program's first sampler that needs it. That sampler lands at a different point among the pipelines a program compiles on worker threads each time it runs. Shaders compiled before it leave the emulation out and get an emulating twin, built on the draw that first binds them (hk_border_twin) and never cached; and the shader cache key carries the setting, so a shader compiled on one side of the sampler in one run and on the other side in the next missed the cache. Counter-Strike 2 appears to hit this: on a warm cache, each de_dust2 session still compiled ~35 shaders outside hk_compile_shaders() at draw time (~1.5 s; the path twins take) and recompiled ~45 shader objects that the previous session had compiled, and those compiles fell in its 300-900 ms in-game freezes. That they are twins and border-key misses is inferred from the compile paths and timing, not yet confirmed in Counter-Strike 2 with this change. When the first sampler of a process turns the emulation on, put a marker for the process in the disk cache; when a later run of it opens the cache, start with the emulation on. Its shaders then compile with the emulation from the start, under one stable cache key, and need no twins. The emulation is a branch on the bound sampler (agx_nir_lower_custom_border), so it is correct with or without a custom border. Programs that never create such a sampler are unaffected. tests/vulkan_border_emulation_cache.c creates a pipeline, then a sampler with a custom border colour, then records a draw across the border, from two instances on an empty cache directory: 2.2-2.5 ms then 0.27-0.29 ms to record, the same image with the custom border colour; without this change 2.6-2.8 ms then 1.3-1.4 ms, building the twin again. 876 dEQP-VK sampler border-colour, custom-border and clamp-to-border cases, run twice on a fresh cache directory: same results as before, cold and warm, 280 pass, 0 fail. Signed-off-by: Jonathan Goyvaerts Assisted-by: LLM (cherry picked from commit 9deef483ca4ed072db4b65428fb63b07938a3da5) --- src/asahi/vulkan/hk_physical_device.c | 59 +++ src/asahi/vulkan/hk_physical_device.h | 3 + src/asahi/vulkan/hk_sampler.c | 5 +- src/asahi/vulkan/meson.build | 9 + .../tests/vulkan_border_emulation_cache.c | 391 ++++++++++++++++++ .../tests/vulkan_border_emulation_cache.frag | 14 + .../vulkan_border_emulation_cache_shaders.h | 102 +++++ 7 files changed, 581 insertions(+), 2 deletions(-) create mode 100644 src/asahi/vulkan/tests/vulkan_border_emulation_cache.c create mode 100644 src/asahi/vulkan/tests/vulkan_border_emulation_cache.frag create mode 100644 src/asahi/vulkan/tests/vulkan_border_emulation_cache_shaders.h diff --git a/src/asahi/vulkan/hk_physical_device.c b/src/asahi/vulkan/hk_physical_device.c index ac48a0805b15..91d20c3854cd 100644 --- a/src/asahi/vulkan/hk_physical_device.c +++ b/src/asahi/vulkan/hk_physical_device.c @@ -14,6 +14,7 @@ #include "util/disk_cache.h" #include "util/hex.h" #include "util/mesa-blake3.h" +#include "util/u_process.h" #include "util/os_misc.h" #include "git_sha1.h" #include "hk_buffer.h" @@ -1701,6 +1702,63 @@ hk_codegen_env_hash(void) return h; } +/* Whether a program's shaders emulate custom border colours is decided at + * the first sampler that needs it (hk_sampler.c), which lands at a different + * point among the shaders compiled on worker threads each time the program + * runs. Shaders compiled before it leave the emulation out and need an + * emulating twin, built on the draw that binds them, and the shader cache key + * carries the setting, so a run that met the sampler at another point missed + * the cache. Remember in the disk cache that this program needed it, so its + * next run emulates from the start. The emulation is a branch on the bound + * sampler, correct with or without a custom border. */ +static bool +hk_border_emulation_key(struct hk_physical_device *pdev, cache_key key) +{ +#ifdef ENABLE_SHADER_CACHE + if (!pdev->vk.disk_cache) + return false; + static const char tag[] = "hk border emulation"; + const char *process = util_get_process_name(); + struct mesa_blake3 ctx; + blake3_hash hash; + _mesa_blake3_init(&ctx); + _mesa_blake3_update(&ctx, tag, sizeof(tag)); + if (process) + _mesa_blake3_update(&ctx, process, strlen(process)); + _mesa_blake3_final(&ctx, hash); + disk_cache_compute_key(pdev->vk.disk_cache, hash, sizeof(hash), key); + return true; +#else + return false; +#endif +} + +void +hk_border_emulation_recall(struct hk_physical_device *pdev) +{ +#ifdef ENABLE_SHADER_CACHE + cache_key key; + size_t size; + if (!hk_border_emulation_key(pdev, key)) + return; + void *seen = disk_cache_get(pdev->vk.disk_cache, key, &size); + if (seen) + p_atomic_set(&pdev->border_emulation, 1); + free(seen); +#endif +} + +void +hk_border_emulation_remember(struct hk_physical_device *pdev) +{ +#ifdef ENABLE_SHADER_CACHE + static const uint8_t seen = 1; + cache_key key; + if (hk_border_emulation_key(pdev, key)) + disk_cache_put(pdev->vk.disk_cache, key, &seen, sizeof(seen), NULL); +#endif +} + static void hk_physical_device_init_pipeline_cache(struct hk_physical_device *pdev) { @@ -1747,6 +1805,7 @@ hk_physical_device_init_pipeline_cache(struct hk_physical_device *pdev) pdev->vk.disk_cache = disk_cache_create(renderer, timestamp, driver_flags ^ (env_hash * UINT64_C(0xff51afd7ed558ccd))); if (pdev->vk.disk_cache != NULL) { + hk_border_emulation_recall(pdev); pdev->vk.properties.pipelineBinaryInternalCache = true; pdev->vk.properties.pipelineBinaryInternalCacheControl = true; pdev->vk.properties.pipelineBinaryPrefersInternalCache = true; diff --git a/src/asahi/vulkan/hk_physical_device.h b/src/asahi/vulkan/hk_physical_device.h index daad7d945ba0..93ab5701a6b1 100644 --- a/src/asahi/vulkan/hk_physical_device.h +++ b/src/asahi/vulkan/hk_physical_device.h @@ -109,6 +109,9 @@ VkResult hk_create_drm_physical_device(struct vk_instance *vk_instance, void hk_physical_device_destroy(struct vk_physical_device *vk_device); +void hk_border_emulation_recall(struct hk_physical_device *pdev); +void hk_border_emulation_remember(struct hk_physical_device *pdev); + #if defined(VK_USE_PLATFORM_WAYLAND_KHR) || \ defined(VK_USE_PLATFORM_XCB_KHR) || defined(VK_USE_PLATFORM_XLIB_KHR) || \ defined(VK_USE_PLATFORM_DISPLAY_KHR) diff --git a/src/asahi/vulkan/hk_sampler.c b/src/asahi/vulkan/hk_sampler.c index 97785fc5b889..2dd253faafc1 100644 --- a/src/asahi/vulkan/hk_sampler.c +++ b/src/asahi/vulkan/hk_sampler.c @@ -282,8 +282,9 @@ hk_CreateSampler(VkDevice device, * compiled before switch to their emulating twins when bound or drawn * with. Publish before the sampler can reach a descriptor. */ if (custom_border && !HK_PERF(dev, NOBORDER) && - !p_atomic_read(&pdev->border_emulation)) - p_atomic_set(&pdev->border_emulation, 1); + !p_atomic_read(&pdev->border_emulation) && + p_atomic_cmpxchg(&pdev->border_emulation, 0, 1) == 0) + hk_border_emulation_remember(pdev); /* Sanity check the noborder setting. There's no way to recover from it being * wrong but at least we can make noise to lint for errors in the driconf. diff --git a/src/asahi/vulkan/meson.build b/src/asahi/vulkan/meson.build index 5d85518e7ebd..3d71ff43b630 100644 --- a/src/asahi/vulkan/meson.build +++ b/src/asahi/vulkan/meson.build @@ -713,6 +713,15 @@ if with_tests gnu_symbol_visibility : 'hidden', ) + executable( + 'vulkan-border-emulation-cache', + files('tests/vulkan_border_emulation_cache.c'), + include_directories : [inc_include], + dependencies : [cc.find_library('vulkan')], + c_args : [c_msvc_compat_args], + gnu_symbol_visibility : 'hidden', + ) + executable( 'g17p-vulkan-multiview-clear', files('tests/g17p_vulkan_multiview_clear.c'), diff --git a/src/asahi/vulkan/tests/vulkan_border_emulation_cache.c b/src/asahi/vulkan/tests/vulkan_border_emulation_cache.c new file mode 100644 index 000000000000..92de4ff63d50 --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_border_emulation_cache.c @@ -0,0 +1,391 @@ +/* Copyright 2026 Aurora Silicon + * SPDX-License-Identifier: MIT + * + * A program that creates its pipelines before its first sampler with a custom + * border colour, as games that build pipelines on worker threads during + * loading do. Its shaders are compiled without the border emulation and get + * an emulating twin built at the first draw that binds them, in the middle of + * recording a frame. The driver must remember that the program needed the + * emulation, so that its next run compiles the shaders with it from the start + * and draws without building twins. + * + * The test starts from an empty cache directory and runs the same sequence + * from two Vulkan instances in turn: create the pipeline, then a sampler with + * a custom border colour, then record a draw that samples across the border, + * timing the recording and reading back the image. The second recording must + * take a fraction of the first and render the same image, with the custom + * border colour around the texture. + * + * Usage: vulkan-border-emulation-cache Exit status 0 = pass. + */ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include + +#include "vulkan_border_emulation_cache_shaders.h" + +#define CHECK(call) \ + do { \ + VkResult r = (call); \ + if (r != VK_SUCCESS) { \ + fprintf(stderr, "%s: %d\n", #call, r); \ + exit(2); \ + } \ + } while (0) + +#define W 256 +#define H 256 + +static VkPhysicalDevice pdev; +static VkDevice dev; + +static uint32_t memory_type(uint32_t bits, VkMemoryPropertyFlags flags) { + VkPhysicalDeviceMemoryProperties m; + vkGetPhysicalDeviceMemoryProperties(pdev, &m); + for (uint32_t i = 0; i < m.memoryTypeCount; i++) + if ((bits & (1u << i)) && (m.memoryTypes[i].propertyFlags & flags) == flags) + return i; + fprintf(stderr, "no memory type\n"); + exit(2); +} + +static VkDeviceMemory bind_image(VkImage img) { + VkMemoryRequirements mr; + vkGetImageMemoryRequirements(dev, img, &mr); + VkDeviceMemory mem; + CHECK(vkAllocateMemory(dev, &(VkMemoryAllocateInfo){ + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = mr.size, + .memoryTypeIndex = memory_type(mr.memoryTypeBits, + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)}, + NULL, &mem)); + CHECK(vkBindImageMemory(dev, img, mem, 0)); + return mem; +} + +static VkImageView view_of(VkImage img) { + VkImageView view; + CHECK(vkCreateImageView(dev, &(VkImageViewCreateInfo){ + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = img, + .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = VK_FORMAT_R8G8B8A8_UNORM, + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}, NULL, &view)); + return view; +} + +static VkShaderModule module(const uint32_t *code, size_t size) { + VkShaderModule m; + CHECK(vkCreateShaderModule(dev, &(VkShaderModuleCreateInfo){ + .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = size, + .pCode = code}, NULL, &m)); + return m; +} + +static void barrier(VkCommandBuffer cb, VkImage img, VkImageLayout from, + VkImageLayout to, VkPipelineStageFlags2 src, + VkAccessFlags2 src_access, VkPipelineStageFlags2 dst, + VkAccessFlags2 dst_access) { + vkCmdPipelineBarrier2(cb, &(VkDependencyInfo){ + .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 1, + .pImageMemoryBarriers = &(VkImageMemoryBarrier2){ + .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, + .srcStageMask = src, .srcAccessMask = src_access, + .dstStageMask = dst, .dstAccessMask = dst_access, + .oldLayout = from, .newLayout = to, .image = img, + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}}); +} + +/* One instance: pipeline first, then the custom-border sampler, then the draw. + * Returns the recording time, a hash of the image and the centre and corner + * pixels. */ +static void run(double *record_ms, uint64_t *hash, uint32_t *centre, + uint32_t *corner) { + VkInstance inst; + CHECK(vkCreateInstance(&(VkInstanceCreateInfo){ + .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, + .pApplicationInfo = &(VkApplicationInfo){ + .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, + .apiVersion = VK_API_VERSION_1_3}}, NULL, &inst)); + uint32_t n = 1; + vkEnumeratePhysicalDevices(inst, &n, &pdev); + CHECK(vkCreateDevice(pdev, &(VkDeviceCreateInfo){ + .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, + .pNext = &(VkPhysicalDeviceVulkan13Features){ + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES, + .dynamicRendering = VK_TRUE, .synchronization2 = VK_TRUE, + .pNext = &(VkPhysicalDeviceCustomBorderColorFeaturesEXT){ + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT, + .customBorderColors = VK_TRUE, + .customBorderColorWithoutFormat = VK_TRUE}}, + .enabledExtensionCount = 1, + .ppEnabledExtensionNames = (const char *[]){ + VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME}, + .queueCreateInfoCount = 1, + .pQueueCreateInfos = &(VkDeviceQueueCreateInfo){ + .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, .queueCount = 1, + .pQueuePriorities = (float[]){1}}}, NULL, &dev)); + VkQueue q; + vkGetDeviceQueue(dev, 0, 0, &q); + + VkDescriptorSetLayout dsl; + CHECK(vkCreateDescriptorSetLayout(dev, &(VkDescriptorSetLayoutCreateInfo){ + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .bindingCount = 1, + .pBindings = &(VkDescriptorSetLayoutBinding){ + 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, + VK_SHADER_STAGE_FRAGMENT_BIT, NULL}}, NULL, &dsl)); + VkPipelineLayout pl; + CHECK(vkCreatePipelineLayout(dev, &(VkPipelineLayoutCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .setLayoutCount = 1, .pSetLayouts = &dsl}, NULL, &pl)); + VkShaderModule vs = module(border_emulation_cache_vert, + sizeof(border_emulation_cache_vert)); + VkShaderModule fs = module(border_emulation_cache_frag, + sizeof(border_emulation_cache_frag)); + const VkFormat fmt = VK_FORMAT_R8G8B8A8_UNORM; + VkPipeline pipe; + CHECK(vkCreateGraphicsPipelines(dev, VK_NULL_HANDLE, 1, &(VkGraphicsPipelineCreateInfo){ + .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, + .pNext = &(VkPipelineRenderingCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO, + .colorAttachmentCount = 1, .pColorAttachmentFormats = &fmt}, + .stageCount = 2, + .pStages = (VkPipelineShaderStageCreateInfo[]){ + {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, NULL, 0, + VK_SHADER_STAGE_VERTEX_BIT, vs, "main", NULL}, + {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, NULL, 0, + VK_SHADER_STAGE_FRAGMENT_BIT, fs, "main", NULL}}, + .pVertexInputState = &(VkPipelineVertexInputStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO}, + .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, + .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST}, + .pViewportState = &(VkPipelineViewportStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, + .viewportCount = 1, .pViewports = &(VkViewport){0, 0, W, H, 0, 1}, + .scissorCount = 1, .pScissors = &(VkRect2D){{0, 0}, {W, H}}}, + .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, + .lineWidth = 1}, + .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, + .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT}, + .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, + .attachmentCount = 1, + .pAttachments = &(VkPipelineColorBlendAttachmentState){ + .colorWriteMask = 0xf}}, + .layout = pl}, NULL, &pipe)); + + /* Only now the program's first custom-border sampler. */ + VkSampler sampler; + CHECK(vkCreateSampler(dev, &(VkSamplerCreateInfo){ + .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, + .pNext = &(VkSamplerCustomBorderColorCreateInfoEXT){ + .sType = VK_STRUCTURE_TYPE_SAMPLER_CUSTOM_BORDER_COLOR_CREATE_INFO_EXT, + .customBorderColor.float32 = {0.9f, 0.1f, 0.8f, 1.0f}, + .format = VK_FORMAT_R8G8B8A8_UNORM}, + .magFilter = VK_FILTER_NEAREST, .minFilter = VK_FILTER_NEAREST, + .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER, + .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER, + .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER, + .borderColor = VK_BORDER_COLOR_FLOAT_CUSTOM_EXT, .maxLod = 0}, NULL, + &sampler)); + + VkImage tex, target; + CHECK(vkCreateImage(dev, &(VkImageCreateInfo){ + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, + .format = fmt, .extent = {4, 4, 1}, .mipLevels = 1, .arrayLayers = 1, + .samples = VK_SAMPLE_COUNT_1_BIT, .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT}, + NULL, &tex)); + CHECK(vkCreateImage(dev, &(VkImageCreateInfo){ + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, + .format = fmt, .extent = {W, H, 1}, .mipLevels = 1, .arrayLayers = 1, + .samples = VK_SAMPLE_COUNT_1_BIT, .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_TRANSFER_SRC_BIT}, NULL, &target)); + VkDeviceMemory tex_mem = bind_image(tex), target_mem = bind_image(target); + VkImageView tex_view = view_of(tex), target_view = view_of(target); + + VkDescriptorPool pool; + CHECK(vkCreateDescriptorPool(dev, &(VkDescriptorPoolCreateInfo){ + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, .maxSets = 1, + .poolSizeCount = 1, + .pPoolSizes = &(VkDescriptorPoolSize){ + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1}}, NULL, &pool)); + VkDescriptorSet set; + CHECK(vkAllocateDescriptorSets(dev, &(VkDescriptorSetAllocateInfo){ + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, + .descriptorPool = pool, .descriptorSetCount = 1, .pSetLayouts = &dsl}, + &set)); + vkUpdateDescriptorSets(dev, 1, &(VkWriteDescriptorSet){ + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = set, + .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + .pImageInfo = &(VkDescriptorImageInfo){ + sampler, tex_view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL}}, + 0, NULL); + + VkBuffer rb; + CHECK(vkCreateBuffer(dev, &(VkBufferCreateInfo){ + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = W * H * 4, + .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT}, NULL, &rb)); + VkMemoryRequirements mr; + vkGetBufferMemoryRequirements(dev, rb, &mr); + VkDeviceMemory rmem; + CHECK(vkAllocateMemory(dev, &(VkMemoryAllocateInfo){ + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = mr.size, + .memoryTypeIndex = memory_type(mr.memoryTypeBits, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)}, + NULL, &rmem)); + CHECK(vkBindBufferMemory(dev, rb, rmem, 0)); + + VkCommandPool cp; + CHECK(vkCreateCommandPool(dev, &(VkCommandPoolCreateInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO}, NULL, &cp)); + VkCommandBuffer cb; + CHECK(vkAllocateCommandBuffers(dev, &(VkCommandBufferAllocateInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, .commandPool = cp, + .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, .commandBufferCount = 1}, &cb)); + + struct timespec t0, t1; + clock_gettime(CLOCK_MONOTONIC, &t0); + CHECK(vkBeginCommandBuffer(cb, &(VkCommandBufferBeginInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO})); + barrier(cb, tex, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + VK_PIPELINE_STAGE_2_NONE, 0, VK_PIPELINE_STAGE_2_CLEAR_BIT, + VK_ACCESS_2_TRANSFER_WRITE_BIT); + vkCmdClearColorImage(cb, tex, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + &(VkClearColorValue){.float32 = {0.2f, 0.6f, 0.3f, 1}}, 1, + &(VkImageSubresourceRange){VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}); + barrier(cb, tex, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_PIPELINE_STAGE_2_CLEAR_BIT, + VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT, + VK_ACCESS_2_SHADER_SAMPLED_READ_BIT); + barrier(cb, target, VK_IMAGE_LAYOUT_UNDEFINED, + VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_PIPELINE_STAGE_2_NONE, 0, + VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, + VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT); + vkCmdBeginRendering(cb, &(VkRenderingInfo){ + .sType = VK_STRUCTURE_TYPE_RENDERING_INFO, .renderArea = {{0, 0}, {W, H}}, + .layerCount = 1, .colorAttachmentCount = 1, + .pColorAttachments = &(VkRenderingAttachmentInfo){ + .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, + .imageView = target_view, + .imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, + .storeOp = VK_ATTACHMENT_STORE_OP_STORE}}); + vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe); + vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, pl, 0, 1, &set, 0, + NULL); + vkCmdDraw(cb, 3, 1, 0, 0); + vkCmdEndRendering(cb); + barrier(cb, target, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, + VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, + VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT, VK_PIPELINE_STAGE_2_COPY_BIT, + VK_ACCESS_2_TRANSFER_READ_BIT); + vkCmdCopyImageToBuffer(cb, target, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, rb, 1, + &(VkBufferImageCopy){ + .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, + .imageExtent = {W, H, 1}}); + CHECK(vkEndCommandBuffer(cb)); + clock_gettime(CLOCK_MONOTONIC, &t1); + *record_ms = (t1.tv_sec - t0.tv_sec) * 1e3 + (t1.tv_nsec - t0.tv_nsec) / 1e6; + + VkFence fence; + CHECK(vkCreateFence(dev, &(VkFenceCreateInfo){ + .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}, NULL, &fence)); + CHECK(vkQueueSubmit(q, 1, &(VkSubmitInfo){ + .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .commandBufferCount = 1, + .pCommandBuffers = &cb}, fence)); + CHECK(vkWaitForFences(dev, 1, &fence, VK_TRUE, UINT64_MAX)); + + const uint32_t *px; + CHECK(vkMapMemory(dev, rmem, 0, VK_WHOLE_SIZE, 0, (void **)&px)); + uint64_t h = 1469598103934665603ull; + for (unsigned i = 0; i < W * H; i++) + h = (h ^ px[i]) * 1099511628211ull; + *hash = h; + *centre = px[(H / 2) * W + W / 2]; + *corner = px[4 * W + 4]; + vkUnmapMemory(dev, rmem); + + vkDestroyFence(dev, fence, NULL); + vkDestroyCommandPool(dev, cp, NULL); + vkDestroyBuffer(dev, rb, NULL); + vkFreeMemory(dev, rmem, NULL); + vkDestroyDescriptorPool(dev, pool, NULL); + vkDestroyImageView(dev, tex_view, NULL); + vkDestroyImageView(dev, target_view, NULL); + vkDestroyImage(dev, tex, NULL); + vkDestroyImage(dev, target, NULL); + vkFreeMemory(dev, tex_mem, NULL); + vkFreeMemory(dev, target_mem, NULL); + vkDestroySampler(dev, sampler, NULL); + vkDestroyPipeline(dev, pipe, NULL); + vkDestroyShaderModule(dev, vs, NULL); + vkDestroyShaderModule(dev, fs, NULL); + vkDestroyPipelineLayout(dev, pl, NULL); + vkDestroyDescriptorSetLayout(dev, dsl, NULL); + /* Destroying the device and instance flushes the disk cache writes. */ + vkDestroyDevice(dev, NULL); + vkDestroyInstance(inst, NULL); +} + +/* RGBA8 little-endian: red in the low byte. */ +static bool near(uint32_t px, float r, float g, float b) { + const float c[3] = {r, g, b}; + for (int i = 0; i < 3; i++) { + const int want = (int)(c[i] * 255.0f + 0.5f); + const int got = (px >> (8 * i)) & 0xff; + if (abs(got - want) > 2) + return false; + } + return true; +} + +int main(void) { + char dir[] = "/tmp/hk-border-emulation-cache-XXXXXX"; + if (!mkdtemp(dir)) { + perror("mkdtemp"); + return 2; + } + setenv("MESA_SHADER_CACHE_DIR", dir, 1); + unsetenv("MESA_SHADER_CACHE_DISABLE"); + + double first, second; + uint64_t h0, h1; + uint32_t centre, corner; + run(&first, &h0, ¢re, &corner); + run(&second, &h1, ¢re, &corner); + printf("recording with the emulating twin built: %.2f ms, emulating from " + "the start: %.2f ms\n", first, second); + + char cmd[96]; + snprintf(cmd, sizeof(cmd), "rm -rf %s", dir); + if (system(cmd) != 0) + fprintf(stderr, "could not remove %s\n", dir); + + if (!near(centre, 0.2f, 0.6f, 0.3f) || !near(corner, 0.9f, 0.1f, 0.8f)) { + fprintf(stderr, "FAIL: centre %08x, corner %08x: not the texture and the " + "custom border colour\n", centre, corner); + return 1; + } + if (h0 != h1) { + fprintf(stderr, "FAIL: the second run renders differently\n"); + return 1; + } + if (second * 4 > first) { + fprintf(stderr, "FAIL: the second run built an emulating twin again\n"); + return 1; + } + printf("PASS\n"); + return 0; +} diff --git a/src/asahi/vulkan/tests/vulkan_border_emulation_cache.frag b/src/asahi/vulkan/tests/vulkan_border_emulation_cache.frag new file mode 100644 index 000000000000..a7344f6f6907 --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_border_emulation_cache.frag @@ -0,0 +1,14 @@ +#version 450 +/* Samples a 4x4 texture across three times its extent, so the outer ring of + * the target reads the sampler's border colour. The arithmetic only makes + * compiling the shader take long enough to time. */ +layout(set = 0, binding = 0) uniform sampler2D tex; +layout(location = 0) out vec4 o; +void main() +{ + vec2 uv = gl_FragCoord.xy / 256.0 * 3.0 - 1.0; + vec4 a = vec4(gl_FragCoord.xy * 0.004, 0.3, 1.0); + for (int i = 0; i < 48; i++) + a = sin(a * 1.31 + vec4(float(i))) * cos(a.yzwx * 0.7) + a.wxyz * 0.1; + o = texture(tex, uv) + a * 1e-6; +} diff --git a/src/asahi/vulkan/tests/vulkan_border_emulation_cache_shaders.h b/src/asahi/vulkan/tests/vulkan_border_emulation_cache_shaders.h new file mode 100644 index 000000000000..54380137bf17 --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_border_emulation_cache_shaders.h @@ -0,0 +1,102 @@ +/* SPDX-License-Identifier: MIT + * Generated with glslangValidator -V from vulkan_sample_variant_cache.vert and + * vulkan_border_emulation_cache.frag. + */ +#include +static const uint32_t border_emulation_cache_vert[] = { + 0x07230203,0x00010000,0x0008000b,0x0000002b,0x00000000,0x00020011,0x00000001,0x0006000b, + 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, + 0x0007000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000c,0x0000001d,0x00030003, + 0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,0x00000000,0x00030005,0x00000009, + 0x00000070,0x00060005,0x0000000c,0x565f6c67,0x65747265,0x646e4978,0x00007865,0x00060005, + 0x0000001b,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x0000001b,0x00000000, + 0x505f6c67,0x7469736f,0x006e6f69,0x00070006,0x0000001b,0x00000001,0x505f6c67,0x746e696f, + 0x657a6953,0x00000000,0x00070006,0x0000001b,0x00000002,0x435f6c67,0x4470696c,0x61747369, + 0x0065636e,0x00070006,0x0000001b,0x00000003,0x435f6c67,0x446c6c75,0x61747369,0x0065636e, + 0x00030005,0x0000001d,0x00000000,0x00040047,0x0000000c,0x0000000b,0x0000002a,0x00030047, + 0x0000001b,0x00000002,0x00050048,0x0000001b,0x00000000,0x0000000b,0x00000000,0x00050048, + 0x0000001b,0x00000001,0x0000000b,0x00000001,0x00050048,0x0000001b,0x00000002,0x0000000b, + 0x00000003,0x00050048,0x0000001b,0x00000003,0x0000000b,0x00000004,0x00020013,0x00000002, + 0x00030021,0x00000003,0x00000002,0x00030016,0x00000006,0x00000020,0x00040017,0x00000007, + 0x00000006,0x00000002,0x00040020,0x00000008,0x00000007,0x00000007,0x00040015,0x0000000a, + 0x00000020,0x00000001,0x00040020,0x0000000b,0x00000001,0x0000000a,0x0004003b,0x0000000b, + 0x0000000c,0x00000001,0x0004002b,0x0000000a,0x0000000e,0x00000001,0x0004002b,0x0000000a, + 0x00000010,0x00000002,0x00040017,0x00000017,0x00000006,0x00000004,0x00040015,0x00000018, + 0x00000020,0x00000000,0x0004002b,0x00000018,0x00000019,0x00000001,0x0004001c,0x0000001a, + 0x00000006,0x00000019,0x0006001e,0x0000001b,0x00000017,0x00000006,0x0000001a,0x0000001a, + 0x00040020,0x0000001c,0x00000003,0x0000001b,0x0004003b,0x0000001c,0x0000001d,0x00000003, + 0x0004002b,0x0000000a,0x0000001e,0x00000000,0x0004002b,0x00000006,0x00000020,0x40000000, + 0x0004002b,0x00000006,0x00000022,0x3f800000,0x0004002b,0x00000006,0x00000025,0x00000000, + 0x00040020,0x00000029,0x00000003,0x00000017,0x00050036,0x00000002,0x00000004,0x00000000, + 0x00000003,0x000200f8,0x00000005,0x0004003b,0x00000008,0x00000009,0x00000007,0x0004003d, + 0x0000000a,0x0000000d,0x0000000c,0x000500c4,0x0000000a,0x0000000f,0x0000000d,0x0000000e, + 0x000500c7,0x0000000a,0x00000011,0x0000000f,0x00000010,0x0004006f,0x00000006,0x00000012, + 0x00000011,0x0004003d,0x0000000a,0x00000013,0x0000000c,0x000500c7,0x0000000a,0x00000014, + 0x00000013,0x00000010,0x0004006f,0x00000006,0x00000015,0x00000014,0x00050050,0x00000007, + 0x00000016,0x00000012,0x00000015,0x0003003e,0x00000009,0x00000016,0x0004003d,0x00000007, + 0x0000001f,0x00000009,0x0005008e,0x00000007,0x00000021,0x0000001f,0x00000020,0x00050050, + 0x00000007,0x00000023,0x00000022,0x00000022,0x00050083,0x00000007,0x00000024,0x00000021, + 0x00000023,0x00050051,0x00000006,0x00000026,0x00000024,0x00000000,0x00050051,0x00000006, + 0x00000027,0x00000024,0x00000001,0x00070050,0x00000017,0x00000028,0x00000026,0x00000027, + 0x00000025,0x00000022,0x00050041,0x00000029,0x0000002a,0x0000001d,0x0000001e,0x0003003e, + 0x0000002a,0x00000028,0x000100fd,0x00010038 +}; +static const uint32_t border_emulation_cache_frag[] = { + 0x07230203,0x00010000,0x0008000b,0x00000051,0x00000000,0x00020011,0x00000001,0x0006000b, + 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, + 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x0000000c,0x00000045,0x00030010, + 0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d, + 0x00000000,0x00030005,0x00000009,0x00007675,0x00060005,0x0000000c,0x465f6c67,0x43676172, + 0x64726f6f,0x00000000,0x00030005,0x00000018,0x00000061,0x00030005,0x00000023,0x00000069, + 0x00030005,0x00000045,0x0000006f,0x00030005,0x00000049,0x00786574,0x00040047,0x0000000c, + 0x0000000b,0x0000000f,0x00040047,0x00000045,0x0000001e,0x00000000,0x00040047,0x00000049, + 0x00000021,0x00000000,0x00040047,0x00000049,0x00000022,0x00000000,0x00020013,0x00000002, + 0x00030021,0x00000003,0x00000002,0x00030016,0x00000006,0x00000020,0x00040017,0x00000007, + 0x00000006,0x00000002,0x00040020,0x00000008,0x00000007,0x00000007,0x00040017,0x0000000a, + 0x00000006,0x00000004,0x00040020,0x0000000b,0x00000001,0x0000000a,0x0004003b,0x0000000b, + 0x0000000c,0x00000001,0x0004002b,0x00000006,0x0000000f,0x43800000,0x0004002b,0x00000006, + 0x00000012,0x40400000,0x0004002b,0x00000006,0x00000014,0x3f800000,0x00040020,0x00000017, + 0x00000007,0x0000000a,0x0004002b,0x00000006,0x0000001b,0x3b83126f,0x0004002b,0x00000006, + 0x0000001d,0x3e99999a,0x00040015,0x00000021,0x00000020,0x00000001,0x00040020,0x00000022, + 0x00000007,0x00000021,0x0004002b,0x00000021,0x00000024,0x00000000,0x0004002b,0x00000021, + 0x0000002b,0x00000030,0x00020014,0x0000002c,0x0004002b,0x00000006,0x0000002f,0x3fa7ae14, + 0x0004002b,0x00000006,0x00000038,0x3f333333,0x0004002b,0x00000006,0x0000003e,0x3dcccccd, + 0x0004002b,0x00000021,0x00000042,0x00000001,0x00040020,0x00000044,0x00000003,0x0000000a, + 0x0004003b,0x00000044,0x00000045,0x00000003,0x00090019,0x00000046,0x00000006,0x00000001, + 0x00000000,0x00000000,0x00000000,0x00000001,0x00000000,0x0003001b,0x00000047,0x00000046, + 0x00040020,0x00000048,0x00000000,0x00000047,0x0004003b,0x00000048,0x00000049,0x00000000, + 0x0004002b,0x00000006,0x0000004e,0x358637bd,0x00050036,0x00000002,0x00000004,0x00000000, + 0x00000003,0x000200f8,0x00000005,0x0004003b,0x00000008,0x00000009,0x00000007,0x0004003b, + 0x00000017,0x00000018,0x00000007,0x0004003b,0x00000022,0x00000023,0x00000007,0x0004003d, + 0x0000000a,0x0000000d,0x0000000c,0x0007004f,0x00000007,0x0000000e,0x0000000d,0x0000000d, + 0x00000000,0x00000001,0x00050050,0x00000007,0x00000010,0x0000000f,0x0000000f,0x00050088, + 0x00000007,0x00000011,0x0000000e,0x00000010,0x0005008e,0x00000007,0x00000013,0x00000011, + 0x00000012,0x00050050,0x00000007,0x00000015,0x00000014,0x00000014,0x00050083,0x00000007, + 0x00000016,0x00000013,0x00000015,0x0003003e,0x00000009,0x00000016,0x0004003d,0x0000000a, + 0x00000019,0x0000000c,0x0007004f,0x00000007,0x0000001a,0x00000019,0x00000019,0x00000000, + 0x00000001,0x0005008e,0x00000007,0x0000001c,0x0000001a,0x0000001b,0x00050051,0x00000006, + 0x0000001e,0x0000001c,0x00000000,0x00050051,0x00000006,0x0000001f,0x0000001c,0x00000001, + 0x00070050,0x0000000a,0x00000020,0x0000001e,0x0000001f,0x0000001d,0x00000014,0x0003003e, + 0x00000018,0x00000020,0x0003003e,0x00000023,0x00000024,0x000200f9,0x00000025,0x000200f8, + 0x00000025,0x000400f6,0x00000027,0x00000028,0x00000000,0x000200f9,0x00000029,0x000200f8, + 0x00000029,0x0004003d,0x00000021,0x0000002a,0x00000023,0x000500b1,0x0000002c,0x0000002d, + 0x0000002a,0x0000002b,0x000400fa,0x0000002d,0x00000026,0x00000027,0x000200f8,0x00000026, + 0x0004003d,0x0000000a,0x0000002e,0x00000018,0x0005008e,0x0000000a,0x00000030,0x0000002e, + 0x0000002f,0x0004003d,0x00000021,0x00000031,0x00000023,0x0004006f,0x00000006,0x00000032, + 0x00000031,0x00070050,0x0000000a,0x00000033,0x00000032,0x00000032,0x00000032,0x00000032, + 0x00050081,0x0000000a,0x00000034,0x00000030,0x00000033,0x0006000c,0x0000000a,0x00000035, + 0x00000001,0x0000000d,0x00000034,0x0004003d,0x0000000a,0x00000036,0x00000018,0x0009004f, + 0x0000000a,0x00000037,0x00000036,0x00000036,0x00000001,0x00000002,0x00000003,0x00000000, + 0x0005008e,0x0000000a,0x00000039,0x00000037,0x00000038,0x0006000c,0x0000000a,0x0000003a, + 0x00000001,0x0000000e,0x00000039,0x00050085,0x0000000a,0x0000003b,0x00000035,0x0000003a, + 0x0004003d,0x0000000a,0x0000003c,0x00000018,0x0009004f,0x0000000a,0x0000003d,0x0000003c, + 0x0000003c,0x00000003,0x00000000,0x00000001,0x00000002,0x0005008e,0x0000000a,0x0000003f, + 0x0000003d,0x0000003e,0x00050081,0x0000000a,0x00000040,0x0000003b,0x0000003f,0x0003003e, + 0x00000018,0x00000040,0x000200f9,0x00000028,0x000200f8,0x00000028,0x0004003d,0x00000021, + 0x00000041,0x00000023,0x00050080,0x00000021,0x00000043,0x00000041,0x00000042,0x0003003e, + 0x00000023,0x00000043,0x000200f9,0x00000025,0x000200f8,0x00000027,0x0004003d,0x00000047, + 0x0000004a,0x00000049,0x0004003d,0x00000007,0x0000004b,0x00000009,0x00050057,0x0000000a, + 0x0000004c,0x0000004a,0x0000004b,0x0004003d,0x0000000a,0x0000004d,0x00000018,0x0005008e, + 0x0000000a,0x0000004f,0x0000004d,0x0000004e,0x00050081,0x0000000a,0x00000050,0x0000004c, + 0x0000004f,0x0003003e,0x00000045,0x00000050,0x000100fd,0x00010038 +}; From c81878159506f305d3352af822275d2129afd9b7 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 29 Oct 2026 13:06:26 -0400 Subject: [PATCH 11/19] agx: validate a cube sample's own CEM scratch as four words agx3_reserve_cube_scratch gives an AGX3B cube sample a seventh source, the four-word vector the packer computes CEM in. The validator modelled that source like any other 32-bit texture source, two halves wide, so a build with assertions rejected every shader with such a sample ("source 6, expected width 2, got width 8 ... Invalid vectors after cube-array scratch") and exited. That broke the gallium blit tests on debug builds. Release builds skip validation, and RA and the packer already size the scratch from its vector, so the generated code is unchanged. Read source 6 of a seven-source texture sample as eight halves, and validate a cube and a cube-array sample before and after the pass in the cube scratch test. (cherry picked from commit 4222ab487edbf064c4dd10fcee9d113822adc942) --- src/asahi/compiler/agx_validate.c | 5 ++++ .../compiler/test/test-agx3-cube-scratch.cpp | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/asahi/compiler/agx_validate.c b/src/asahi/compiler/agx_validate.c index 7e39d8ac9037..417b3df42db7 100644 --- a/src/asahi/compiler/agx_validate.c +++ b/src/asahi/compiler/agx_validate.c @@ -488,6 +488,11 @@ agx_read_registers(const agx_context *ctx, const agx_instr *I, unsigned s) * its existing compare/offset collect. */ return agx_is_null(I->src[s]) ? 0 : 2 * agx_channels(I->src[s]); + } else if (s == 6 && I->op == AGX_OPCODE_TEXTURE_SAMPLE && + I->nr_srcs == 7) { + /* A cube sample's own CEM scratch (agx3_reserve_cube_scratch): the + * four words the packer computes CEM in. */ + return 8; } else { return size; } diff --git a/src/asahi/compiler/test/test-agx3-cube-scratch.cpp b/src/asahi/compiler/test/test-agx3-cube-scratch.cpp index 257fe072e323..f1b94b8e6523 100644 --- a/src/asahi/compiler/test/test-agx3-cube-scratch.cpp +++ b/src/asahi/compiler/test/test-agx3-cube-scratch.cpp @@ -65,3 +65,31 @@ TEST(AGX3CubeScratch, CubeSamplesReadAnUndefinedScratchVector) EXPECT_FALSE(agx3_reserve_cube_scratch(b->shader)); ralloc_free(mem); } + +/* A cube sample that reads its scratch is valid IR: the scratch source is + * four words wide. agx_validate checks only in builds with assertions. */ +TEST(AGX3CubeScratch, ScratchSourceValidates) +{ + void *mem = ralloc_context(NULL); + agx_shader_key key = {}; + key.dev.encoding = AGX_ISA_ENCODING_AGX3B; + agx_builder *b = agx_test_builder(mem); + b->shader->key = &key; + for (unsigned array = 0; array < 2; ++array) { + const unsigned comps = 3 + array; + agx_index coords = agx_vec_temp(b->shader, AGX_SIZE_32, comps); + agx_instr *collect = agx_collect_to(b, coords, comps); + agx_foreach_src(collect, c) + collect->src[c] = agx_undef(AGX_SIZE_32); + agx_texture_sample_to(b, agx_vec_temp(b->shader, AGX_SIZE_16, 4), coords, + agx_immediate(0), agx_immediate(0), + agx_immediate(0), agx_immediate(0), agx_null(), + array ? AGX_DIM_CUBE_ARRAY : AGX_DIM_CUBE, + AGX_LOD_MODE_AUTO_LOD, 0xf, false, false, false, + AGX_GATHER_NONE, false); + } + agx_validate(b->shader, "cube sample test shader"); + ASSERT_TRUE(agx3_reserve_cube_scratch(b->shader)); + agx_validate(b->shader, "cube-array scratch"); + ralloc_free(mem); +} From be3e990eba97317f1a563b8de7ac9e87f718777b Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Sat, 31 Oct 2026 01:03:19 -0400 Subject: [PATCH 12/19] hk: enable synchronization2 in vulkan-sample-variant-cache The test records vkCmdPipelineBarrier2 but created its device without the synchronization2 feature, which the validation layers report as an error unrelated to what the test checks. Enable it, as vulkan-border-emulation-cache and the other new tests already do. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit f715c9f9fe6a0c2f12b07af05563b863e1223c40) --- src/asahi/vulkan/tests/vulkan_sample_variant_cache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/asahi/vulkan/tests/vulkan_sample_variant_cache.c b/src/asahi/vulkan/tests/vulkan_sample_variant_cache.c index 9876970687cf..89e7bfc0a186 100644 --- a/src/asahi/vulkan/tests/vulkan_sample_variant_cache.c +++ b/src/asahi/vulkan/tests/vulkan_sample_variant_cache.c @@ -104,7 +104,7 @@ static void run(double *record_ms, uint64_t *hash) { .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, .pNext = &(VkPhysicalDeviceVulkan13Features){ .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES, - .dynamicRendering = VK_TRUE, + .dynamicRendering = VK_TRUE, .synchronization2 = VK_TRUE, .pNext = &(VkPhysicalDeviceExtendedDynamicState3FeaturesEXT){ .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_3_FEATURES_EXT, .extendedDynamicState3RasterizationSamples = VK_TRUE}}, From 03e88e431cf5bc5ce584ad1f432aeffa64be71ff Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Sat, 31 Oct 2026 01:03:25 -0400 Subject: [PATCH 13/19] hk: count compiles, not milliseconds, in the warm-cache variant tests vulkan-sample-variant-cache and vulkan-border-emulation-cache decided that the second, warm-cache recording compiled a shader when it took more than a quarter of the first. Scheduling and clock changes can push a recording that loads from the cache past that ratio, so the tests could fail with nothing wrong. Count the shaders compiled while recording instead. With AGX_MESA_DEBUG=shaderdb, agx_compile_shader_nir() prints one statistics line for every shader that is not internal. Prologs, epilogs and meta shaders are built with nir_builder_init_simple_shader(), which marks them internal. A multisample variant, a border-emulating twin or a G17P inline-blend program built at draw time comes from the application's shader and prints a line; loading one from the disk cache prints nothing. The tests capture stderr around the recording (vulkan_compile_count.h) and require at least one compile in the first recording and none in the second. Recording times are still printed, for information. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 32437aea8aab41f45a5326eabcc53416c32316e7) --- .../tests/vulkan_border_emulation_cache.c | 41 ++++--- src/asahi/vulkan/tests/vulkan_compile_count.h | 103 ++++++++++++++++++ .../tests/vulkan_sample_variant_cache.c | 42 ++++--- 3 files changed, 160 insertions(+), 26 deletions(-) create mode 100644 src/asahi/vulkan/tests/vulkan_compile_count.h diff --git a/src/asahi/vulkan/tests/vulkan_border_emulation_cache.c b/src/asahi/vulkan/tests/vulkan_border_emulation_cache.c index 92de4ff63d50..500280b54e40 100644 --- a/src/asahi/vulkan/tests/vulkan_border_emulation_cache.c +++ b/src/asahi/vulkan/tests/vulkan_border_emulation_cache.c @@ -12,9 +12,11 @@ * The test starts from an empty cache directory and runs the same sequence * from two Vulkan instances in turn: create the pipeline, then a sampler with * a custom border colour, then record a draw that samples across the border, - * timing the recording and reading back the image. The second recording must - * take a fraction of the first and render the same image, with the custom - * border colour around the texture. + * counting the shaders compiled while recording (vulkan_compile_count.h) and + * reading back the image. The first recording must build a twin, and the + * second must compile nothing and render the same image, with the custom + * border colour around the texture. Recording times are printed for + * information only. * * Usage: vulkan-border-emulation-cache Exit status 0 = pass. */ @@ -29,6 +31,7 @@ #include #include "vulkan_border_emulation_cache_shaders.h" +#include "vulkan_compile_count.h" #define CHECK(call) \ do { \ @@ -100,10 +103,10 @@ static void barrier(VkCommandBuffer cb, VkImage img, VkImageLayout from, } /* One instance: pipeline first, then the custom-border sampler, then the draw. - * Returns the recording time, a hash of the image and the centre and corner - * pixels. */ -static void run(double *record_ms, uint64_t *hash, uint32_t *centre, - uint32_t *corner) { + * Returns the recording time, the number of shaders compiled while recording, + * a hash of the image and the centre and corner pixels. */ +static void run(double *record_ms, unsigned *compiles, uint64_t *hash, + uint32_t *centre, uint32_t *corner) { VkInstance inst; CHECK(vkCreateInstance(&(VkInstanceCreateInfo){ .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, @@ -254,7 +257,9 @@ static void run(double *record_ms, uint64_t *hash, uint32_t *centre, .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, .commandPool = cp, .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, .commandBufferCount = 1}, &cb)); + struct compile_count count; struct timespec t0, t1; + compile_count_begin(&count); clock_gettime(CLOCK_MONOTONIC, &t0); CHECK(vkBeginCommandBuffer(cb, &(VkCommandBufferBeginInfo){ .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO})); @@ -297,6 +302,7 @@ static void run(double *record_ms, uint64_t *hash, uint32_t *centre, .imageExtent = {W, H, 1}}); CHECK(vkEndCommandBuffer(cb)); clock_gettime(CLOCK_MONOTONIC, &t1); + *compiles = compile_count_end(&count); *record_ms = (t1.tv_sec - t0.tv_sec) * 1e3 + (t1.tv_nsec - t0.tv_nsec) / 1e6; VkFence fence; @@ -359,14 +365,17 @@ int main(void) { } setenv("MESA_SHADER_CACHE_DIR", dir, 1); unsetenv("MESA_SHADER_CACHE_DISABLE"); + compile_count_enable(); double first, second; + unsigned first_compiles, second_compiles; uint64_t h0, h1; uint32_t centre, corner; - run(&first, &h0, ¢re, &corner); - run(&second, &h1, ¢re, &corner); - printf("recording with the emulating twin built: %.2f ms, emulating from " - "the start: %.2f ms\n", first, second); + run(&first, &first_compiles, &h0, ¢re, &corner); + run(&second, &second_compiles, &h1, ¢re, &corner); + printf("recording with the emulating twin built: %u compiles, %.2f ms; " + "emulating from the start: %u compiles, %.2f ms\n", + first_compiles, first, second_compiles, second); char cmd[96]; snprintf(cmd, sizeof(cmd), "rm -rf %s", dir); @@ -382,8 +391,14 @@ int main(void) { fprintf(stderr, "FAIL: the second run renders differently\n"); return 1; } - if (second * 4 > first) { - fprintf(stderr, "FAIL: the second run built an emulating twin again\n"); + if (first_compiles == 0) { + fprintf(stderr, "FAIL: the first recording compiled nothing, so the draw " + "no longer builds a twin and this test checks nothing\n"); + return 1; + } + if (second_compiles != 0) { + fprintf(stderr, "FAIL: the second recording compiled %u shaders: it " + "built an emulating twin again\n", second_compiles); return 1; } printf("PASS\n"); diff --git a/src/asahi/vulkan/tests/vulkan_compile_count.h b/src/asahi/vulkan/tests/vulkan_compile_count.h new file mode 100644 index 000000000000..1c28edfb0f8f --- /dev/null +++ b/src/asahi/vulkan/tests/vulkan_compile_count.h @@ -0,0 +1,103 @@ +/* Copyright 2026 Aurora Silicon + * SPDX-License-Identifier: MIT + * + * Counts the shaders the driver compiles while a test records a command + * buffer, for tests that check a warm disk cache keeps compiles off the draw + * path. A count does not depend on how fast the machine happens to be. + * + * With AGX_MESA_DEBUG=shaderdb, agx_compile_shader_nir() prints one + * " shader: ..." statistics line to stderr for every shader it + * compiles, except internal ones. Prologs, epilogs and meta shaders are built + * with nir_builder_init_simple_shader(), which marks them internal, so they + * print nothing. A draw-time build from the application's shader does print: + * a multisample variant, a border-emulating twin, or the G17P inline-blend + * program. Loading any of those from the disk cache compiles nothing, so it + * prints nothing. + * + * The flag is part of the driver's cache identity (hk_codegen_env_hash()), so + * call compile_count_enable() before the first Vulkan instance and keep it set + * for every instance that shares the cache. + */ +#ifndef VULKAN_COMPILE_COUNT_H +#define VULKAN_COMPILE_COUNT_H + +#include +#include +#include +#include +#include + +static void compile_count_enable(void) { + const char *old = getenv("AGX_MESA_DEBUG"); + char flags[256]; + snprintf(flags, sizeof(flags), "%s%sshaderdb", old ? old : "", + old && *old ? "," : ""); + setenv("AGX_MESA_DEBUG", flags, 1); +} + +struct compile_count { + FILE *file; + int saved_stderr; +}; + +static struct compile_count *compile_count_active; +static unsigned compile_count_end(struct compile_count *c); + +/* A test that exits while capturing still shows why. */ +static void compile_count_at_exit(void) { + if (compile_count_active) + compile_count_end(compile_count_active); +} + +/* Sends stderr to a temporary file until compile_count_end(). */ +static void compile_count_begin(struct compile_count *c) { + static bool registered; + if (!registered && atexit(compile_count_at_exit) != 0) { + perror("atexit"); + exit(2); + } + registered = true; + fflush(stderr); + c->file = tmpfile(); + c->saved_stderr = dup(STDERR_FILENO); + if (!c->file || c->saved_stderr < 0 || + dup2(fileno(c->file), STDERR_FILENO) < 0) { + perror("capturing stderr"); + exit(2); + } + compile_count_active = c; +} + +static bool compile_count_is_stats_line(const char *line) { + const char *p = line; + while (*p >= 'A' && *p <= 'Z') + p++; + return p != line && !strncmp(p, " shader: ", 9); +} + +/* Restores stderr, copies what was captured to it, and returns the number of + * shaders compiled in between. */ +static unsigned compile_count_end(struct compile_count *c) { + compile_count_active = NULL; + fflush(stderr); + if (dup2(c->saved_stderr, STDERR_FILENO) < 0) { + perror("restoring stderr"); + exit(2); + } + close(c->saved_stderr); + rewind(c->file); + + unsigned count = 0; + bool line_start = true; + char buf[512]; + while (fgets(buf, sizeof(buf), c->file)) { + if (line_start && compile_count_is_stats_line(buf)) + count++; + fputs(buf, stderr); + line_start = strchr(buf, '\n') != NULL; + } + fclose(c->file); + return count; +} + +#endif diff --git a/src/asahi/vulkan/tests/vulkan_sample_variant_cache.c b/src/asahi/vulkan/tests/vulkan_sample_variant_cache.c index 89e7bfc0a186..3e5265d0341f 100644 --- a/src/asahi/vulkan/tests/vulkan_sample_variant_cache.c +++ b/src/asahi/vulkan/tests/vulkan_sample_variant_cache.c @@ -9,11 +9,13 @@ * again. * * The test starts from an empty cache directory and records the same 2x MSAA - * draw from two Vulkan instances in turn, timing the command buffer - * recording and reading back the resolved image. The second recording must - * take a fraction of the first (it loads the variant) and render the same - * image. On the M4 Pro: 3.9 ms against 0.56 ms; a driver that compiles the - * variant again takes 2.7 ms against 1.5 ms. + * draw from two Vulkan instances in turn, counting the shaders compiled + * while recording (vulkan_compile_count.h) and reading back the resolved + * image. The first recording must compile the variant, and the second must + * compile nothing (it loads the variant) and render the same image. + * Recording times are printed for information only. On the M4 Pro: 3.9 ms + * against 0.56 ms; a driver that compiles the variant again takes 2.7 ms + * against 1.5 ms. * * Usage: vulkan-sample-variant-cache Exit status 0 = pass. */ @@ -27,6 +29,7 @@ #include #include +#include "vulkan_compile_count.h" #include "vulkan_sample_variant_cache_shaders.h" #define CHECK(call) \ @@ -90,8 +93,9 @@ static VkShaderModule module(const uint32_t *code, size_t size) { } /* One instance: build the pipeline, record and run the draw, return the - * recording time and a hash of the resolved image. */ -static void run(double *record_ms, uint64_t *hash) { + * recording time, the number of shaders compiled while recording and a hash + * of the resolved image. */ +static void run(double *record_ms, unsigned *compiles, uint64_t *hash) { VkInstance inst; CHECK(vkCreateInstance(&(VkInstanceCreateInfo){ .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, @@ -201,7 +205,9 @@ static void run(double *record_ms, uint64_t *hash) { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, .commandPool = cp, .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, .commandBufferCount = 1}, &cb)); + struct compile_count count; struct timespec t0, t1; + compile_count_begin(&count); clock_gettime(CLOCK_MONOTONIC, &t0); CHECK(vkBeginCommandBuffer(cb, &(VkCommandBufferBeginInfo){ .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO})); @@ -253,6 +259,7 @@ static void run(double *record_ms, uint64_t *hash) { .imageExtent = {W, H, 1}}); CHECK(vkEndCommandBuffer(cb)); clock_gettime(CLOCK_MONOTONIC, &t1); + *compiles = compile_count_end(&count); *record_ms = (t1.tv_sec - t0.tv_sec) * 1e3 + (t1.tv_nsec - t0.tv_nsec) / 1e6; VkFence fence; @@ -306,13 +313,16 @@ int main(void) { } setenv("MESA_SHADER_CACHE_DIR", dir, 1); unsetenv("MESA_SHADER_CACHE_DISABLE"); + compile_count_enable(); double cold, warm; + unsigned cold_compiles, warm_compiles; uint64_t h0, h1; - run(&cold, &h0); - run(&warm, &h1); - printf("recording with the variant compiled: %.2f ms, from the cache: %.2f ms\n", - cold, warm); + run(&cold, &cold_compiles, &h0); + run(&warm, &warm_compiles, &h1); + printf("recording with the variant compiled: %u compiles, %.2f ms; " + "from the cache: %u compiles, %.2f ms\n", + cold_compiles, cold, warm_compiles, warm); char cmd[96]; snprintf(cmd, sizeof(cmd), "rm -rf %s", dir); @@ -323,8 +333,14 @@ int main(void) { fprintf(stderr, "FAIL: the cached variant renders differently\n"); return 1; } - if (warm * 4 > cold) { - fprintf(stderr, "FAIL: the second recording compiled the variant again\n"); + if (cold_compiles == 0) { + fprintf(stderr, "FAIL: the first recording compiled nothing, so the draw " + "no longer builds a variant and this test checks nothing\n"); + return 1; + } + if (warm_compiles != 0) { + fprintf(stderr, "FAIL: the second recording compiled %u shaders: the " + "variant did not come from the cache\n", warm_compiles); return 1; } printf("PASS\n"); From 1545fe685cafa9c3f7f0965bf373b41e36613940 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Tue, 3 Nov 2026 11:29:53 -0500 Subject: [PATCH 14/19] asahi: Release failed context allocations with ralloc The context belongs to ralloc, so release its owner with ralloc_free when uploader creation fails. Derive the embedded pipe pointer only after checking allocation success. Signed-off-by: Satya Benson --- src/gallium/drivers/asahi/agx_pipe.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/gallium/drivers/asahi/agx_pipe.c b/src/gallium/drivers/asahi/agx_pipe.c index c769f062345d..0cca09db504a 100644 --- a/src/gallium/drivers/asahi/agx_pipe.c +++ b/src/gallium/drivers/asahi/agx_pipe.c @@ -1924,12 +1924,13 @@ static struct pipe_context * agx_create_context(struct pipe_screen *screen, void *priv, unsigned flags) { struct agx_context *ctx = rzalloc(NULL, struct agx_context); - struct pipe_context *pctx = &ctx->base; int ret; if (!ctx) return NULL; + struct pipe_context *pctx = &ctx->base; + pctx->screen = screen; pctx->priv = priv; @@ -1938,7 +1939,7 @@ agx_create_context(struct pipe_screen *screen, void *priv, unsigned flags) pctx->stream_uploader = u_upload_create_default(pctx); if (!pctx->stream_uploader) { - FREE(pctx); + ralloc_free(ctx); return NULL; } pctx->const_uploader = pctx->stream_uploader; From 0ed578243bab1b1969d13c220d5d5f1b859a36c4 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Tue, 3 Nov 2026 11:29:53 -0500 Subject: [PATCH 15/19] asahi: Admit the G15G firmware interface independently of GPU records Keep T8122 G15 command streams, Gen4 samplers and the native sampler heap for both firmware interfaces. The HAL200 profile checks C0 identity and the reported enabled-core mask without changing physical chip selection. Keep the scheduled queue feature contract and compiler requirements. Test profile axes and malformed identities, and allow the no-op shim to present the additional interface for offline compiler and device-open coverage. Signed-off-by: Satya Benson --- src/asahi/drm-shim/asahi_noop.c | 5 +++ src/asahi/lib/agx_chip.c | 45 ++++++++++++++++++---- src/asahi/lib/tests/test-chip-admission.c | 46 ++++++++++++++++++++++- 3 files changed, 87 insertions(+), 9 deletions(-) diff --git a/src/asahi/drm-shim/asahi_noop.c b/src/asahi/drm-shim/asahi_noop.c index 2beb6d6fdf17..44249af1dad1 100644 --- a/src/asahi/drm-shim/asahi_noop.c +++ b/src/asahi/drm-shim/asahi_noop.c @@ -1323,6 +1323,11 @@ asahi_ioctl_get_param(int fd, unsigned long request, void *arg) size_t size = MIN2((size_t)gp->size, sizeof(struct drm_asahi_params_global)); struct drm_asahi_params_global params = *shim_params(); params.features = shim_features(); + if (shim_params() == ¶ms_g15g && + shim_env_on("ASAHI_SHIM_G15G_HAL200")) { + params.gpu_hal_generation = DRM_ASAHI_GPU_HAL_200; + params.vm_end = UINT64_C(0x3ffffff8000); + } const char *command_limit = getenv("ASAHI_SHIM_COMMAND_LIMIT"); if (command_limit) { unsigned limit = strtoul(command_limit, NULL, 0); diff --git a/src/asahi/lib/agx_chip.c b/src/asahi/lib/agx_chip.c index da139589e67c..a02fc564462b 100644 --- a/src/asahi/lib/agx_chip.c +++ b/src/asahi/lib/agx_chip.c @@ -7,6 +7,8 @@ #include +#include "util/bitscan.h" + enum agx_chip agx_chip_from_params(const struct drm_asahi_params_global *params) { @@ -276,6 +278,41 @@ agx_chip_driver_supported(enum agx_chip chip) } } +/* The firmware interface version does not determine the GPU record layout. + * Both T8122 interfaces consume G15 records and Gen4 sampler words. Keep the + * physical chip as G15G so compiler scheduling and hardware workarounds agree. + */ +static bool +agx_t8122_params_supported(const struct drm_asahi_params_global *params) +{ + if (params->chip_id != 0x8122 || params->num_dies != 1 || + params->num_clusters_total != 1 || + params->usc_generation != AGX_ISA_AGX3) + return false; + + if (params->gpu_hal_generation == DRM_ASAHI_GPU_HAL_LEGACY) + return true; + + if (params->gpu_hal_generation != DRM_ASAHI_GPU_HAL_200 || + params->gpu_revision != 0x20 || params->num_cores_per_cluster != 10) + return false; + + const uint64_t mask = params->core_masks[0]; + const unsigned cores = util_bitcount64(mask); + if ((mask >> 10) || (cores != 8 && cores != 10)) + return false; + for (unsigned i = 1; i < DRM_ASAHI_MAX_CLUSTERS; ++i) { + if (params->core_masks[i]) + return false; + } + + return agx_chip_command_stream_generation(AGX_CHIP_G15G) == + AGX_CS_GENERATION_G15 && + agx_chip_sampler_generation(AGX_CHIP_G15G) == + AGX_SAMPLER_GENERATION_GEN4 && + agx_chip_sampler_heap_element_size(AGX_CHIP_G15G) == 32; +} + bool agx_params_driver_supported(const struct drm_asahi_params_global *params) { @@ -301,13 +338,7 @@ agx_params_driver_supported(const struct drm_asahi_params_global *params) params->gpu_hal_generation == DRM_ASAHI_GPU_HAL_200; } case AGX_CHIP_G15G: - /* T8122/M3: one die, one cluster, AGX3 shaders on the legacy firmware - * interface (14.8.3), through the kernel's T8122 runtime. The exact - * identity/runtime checks apply without a Mesa environment opt-in. */ - return params->chip_id == 0x8122 && params->num_dies == 1 && - params->num_clusters_total == 1 && - params->usc_generation == AGX_ISA_AGX3 && - params->gpu_hal_generation == DRM_ASAHI_GPU_HAL_LEGACY; + return agx_t8122_params_supported(params); case AGX_CHIP_G15S: /* T6030/M3 Pro: one die, two clusters, AGX3 shaders on the legacy * (pre-HAL200) firmware interface. */ diff --git a/src/asahi/lib/tests/test-chip-admission.c b/src/asahi/lib/tests/test-chip-admission.c index 532be1c49646..62bf2de3139d 100644 --- a/src/asahi/lib/tests/test-chip-admission.c +++ b/src/asahi/lib/tests/test-chip-admission.c @@ -223,9 +223,51 @@ main(void) bad.num_clusters_total = 2; assert(!agx_params_driver_supported(&bad)); - bad = g15g; - bad.gpu_hal_generation = DRM_ASAHI_GPU_HAL_200; + struct drm_asahi_params_global modern = g15g; + modern.gpu_hal_generation = DRM_ASAHI_GPU_HAL_200; + modern.gpu_revision = 0x20; + assert(agx_params_driver_supported(&modern)); + assert(agx_params_native_kernel_abi_supported(&modern)); + enum agx_chip modern_chip = agx_chip_from_params(&modern); + assert(modern_chip == AGX_CHIP_G15G); + assert(agx_chip_command_stream_generation(modern_chip) == + AGX_CS_GENERATION_G15); + assert(agx_chip_sampler_generation(modern_chip) == + AGX_SAMPLER_GENERATION_GEN4); + assert(agx_chip_sampler_heap_element_size(modern_chip) == 32); + assert(agx_chip_isa_encoding(modern_chip) == + agx_chip_isa_encoding(agx_chip_from_params(&g15g))); + assert(agx_chip_cdm_wide_visibility(modern_chip)); + assert(!agx_chip_sampler_has_lod_bias(modern_chip)); + modern.core_masks[0] = 0x3cf; + assert(agx_params_driver_supported(&modern)); + modern.core_masks[0] = 0x3ff; + + bad = modern; + bad.gpu_hal_generation = DRM_ASAHI_GPU_HAL_300; + assert(!agx_params_driver_supported(&bad)); + bad = modern; + bad.gpu_revision = 0x21; + assert(!agx_params_driver_supported(&bad)); + bad = modern; + bad.num_cores_per_cluster = 9; + assert(!agx_params_driver_supported(&bad)); + bad = modern; + bad.core_masks[0] = 0x1ff; assert(!agx_params_driver_supported(&bad)); + bad = modern; + bad.core_masks[0] = 0x7ff; + assert(!agx_params_driver_supported(&bad)); + bad = modern; + bad.core_masks[1] = 1; + assert(!agx_params_driver_supported(&bad)); + bad = modern; + bad.gpu_variant = 'S'; + assert(!agx_params_driver_supported(&bad)); + bad = modern; + bad.features = DRM_ASAHI_FEATURE_EXACT_PRIOR_BARRIERS | + DRM_ASAHI_FEATURE_PER_COMMAND_INPUT_SYNCS; + assert(!agx_params_native_kernel_abi_supported(&bad)); bad = g15g; bad.usc_generation = 2; From 39a605a901578976cb6c03a48021f3d7bee9ade7 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Tue, 3 Nov 2026 11:29:53 -0500 Subject: [PATCH 16/19] zink: Initialize spare query-start slots in the query test The synthetic split-query case appends starts directly, bypassing the production allocator that zeroes new capacity. Teardown visits the entire capacity to release retained references, so uninitialized spare slots can crash the test. Zero this all-empty synthetic backing before teardown. Signed-off-by: Satya Benson --- src/gallium/drivers/zink/zink_query_test.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/gallium/drivers/zink/zink_query_test.c b/src/gallium/drivers/zink/zink_query_test.c index c9c2eef62d59..455d99f65964 100644 --- a/src/gallium/drivers/zink/zink_query_test.c +++ b/src/gallium/drivers/zink/zink_query_test.c @@ -139,6 +139,8 @@ main(void) assert(can_direct_poll_query_result(&q)); util_dynarray_append(&q.starts, split); assert(!can_direct_poll_query_result(&q)); + /* Teardown also visits spare slots, as the production allocator permits. */ + memset(q.starts.data, 0, q.starts.capacity); uint64_t values[] = {2048, 2048}; union pipe_query_result result; util_query_clear_result(&result, q.type); From 7cedaf8d36a8cb8d182eac81cd8111a60165bffe Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Tue, 3 Nov 2026 11:29:53 -0500 Subject: [PATCH 17/19] asahi: Keep T8122 sampler state in the native GPU stream The G15G HAL200 interface consumes sampler state from the native stream. Leave the optional firmware-command sampler fields zero in both Gallium and Honeykrisp while preserving the native heap and shader lowering. Signed-off-by: Satya Benson --- src/asahi/lib/agx_device.h | 9 +++++++++ src/asahi/vulkan/hk_queue.c | 4 ++-- src/gallium/drivers/asahi/agx_pipe.c | 8 +++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/asahi/lib/agx_device.h b/src/asahi/lib/agx_device.h index d53e5eeddcc0..24f84eb15924 100644 --- a/src/asahi/lib/agx_device.h +++ b/src/asahi/lib/agx_device.h @@ -520,6 +520,15 @@ agx_native_graphics(const struct agx_device *dev) return agx_chip_native_agx3(dev->chip); } +/* This interface carries native sampler state in GPU records, not in the + * firmware command's optional sampler-heap fields. */ +static inline bool +agx_stream_sampler_state(const struct agx_device *dev) +{ + return dev->chip == AGX_CHIP_G15G && + dev->params.gpu_hal_generation == DRM_ASAHI_GPU_HAL_200; +} + /* T6040 uses the G17P architecture profile, while capability rollout may * still be product-specific. Device admission has already checked USC/HAL. */ static inline bool diff --git a/src/asahi/vulkan/hk_queue.c b/src/asahi/vulkan/hk_queue.c index 4c7eb7eeee4f..bf443fdeceaf 100644 --- a/src/asahi/vulkan/hk_queue.c +++ b/src/asahi/vulkan/hk_queue.c @@ -134,7 +134,7 @@ asahi_fill_cdm_command(struct hk_device *dev, struct hk_cs *cs, .ts.end.offset = cs->timestamp.end.offset_B, }; - if (cs->uses_sampler_heap) { + if (cs->uses_sampler_heap && !agx_stream_sampler_state(&dev->dev)) { cmd->sampler_heap = dev->samplers.table.bo->va->addr; cmd->sampler_count = dev->samplers.table.alloc; } @@ -296,7 +296,7 @@ asahi_fill_vdm_command(struct hk_device *dev, struct hk_cs *cs, c->isp_scissor_base = cs->uploaded_scissor; c->isp_dbias_base = cs->uploaded_zbias; - if (cs->uses_sampler_heap) { + if (cs->uses_sampler_heap && !agx_stream_sampler_state(&dev->dev)) { c->sampler_heap = dev->samplers.table.bo->va->addr; c->sampler_count = dev->samplers.table.alloc; } diff --git a/src/gallium/drivers/asahi/agx_pipe.c b/src/gallium/drivers/asahi/agx_pipe.c index c769f062345d..a25aba3fdc13 100644 --- a/src/gallium/drivers/asahi/agx_pipe.c +++ b/src/gallium/drivers/asahi/agx_pipe.c @@ -1508,7 +1508,8 @@ agx_cmdbuf(struct agx_device *dev, struct drm_asahi_cmd_render *c, c->isp_dbias_base = depth_bias_ptr; c->isp_oclqry_base = visibility_result_ptr; - if (batch->sampler_heap.bo) { + if (batch->sampler_heap.bo && + !agx_stream_sampler_state(dev)) { c->sampler_heap = batch->sampler_heap.bo->va->addr; c->sampler_count = batch->sampler_heap.count; } @@ -1673,6 +1674,11 @@ agx_flush_compute(struct agx_context *ctx, struct agx_batch *batch, .sampler_count = batch->sampler_heap.count, }; + if (agx_stream_sampler_state(dev)) { + cmdbuf->sampler_heap = 0; + cmdbuf->sampler_count = 0; + } + if (batch->cs_scratch) { // The commented out lines *may* be related to subgroup-level preemption, // which we can't support without implementing threadgroup memory in the From c9358a9c5402da51a816d3c2f16a8e9ed9d91b75 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Tue, 3 Nov 2026 17:26:04 -0500 Subject: [PATCH 18/19] asahi: probe HIGH admission and report the effective EGL context priority Signed-off-by: Satya Benson --- src/asahi/drm-shim/asahi_noop.c | 11 +++ src/asahi/lib/agx_device.c | 21 ++++++ src/asahi/lib/agx_device.h | 1 + src/egl/drivers/dri2/egl_dri2.c | 18 +++++ .../auxiliary/driver_ddebug/dd_context.c | 1 + .../auxiliary/driver_trace/tr_context.c | 1 + .../auxiliary/util/u_threaded_context.c | 1 + src/gallium/drivers/asahi/agx_pipe.c | 17 ++++- src/gallium/drivers/asahi/meson.build | 47 ++++++++++++ .../asahi/tests/test-agx-egl-priority.c | 40 +++++++++++ .../drivers/asahi/tests/test-agx-priority.c | 72 +++++++++++++++++++ src/gallium/frontends/dri/dri_context.c | 7 ++ src/gallium/frontends/dri/dri_util.h | 1 + src/gallium/include/pipe/p_context.h | 6 ++ 14 files changed, 241 insertions(+), 3 deletions(-) create mode 100644 src/gallium/drivers/asahi/tests/test-agx-egl-priority.c create mode 100644 src/gallium/drivers/asahi/tests/test-agx-priority.c diff --git a/src/asahi/drm-shim/asahi_noop.c b/src/asahi/drm-shim/asahi_noop.c index 44249af1dad1..667263cf19e3 100644 --- a/src/asahi/drm-shim/asahi_noop.c +++ b/src/asahi/drm-shim/asahi_noop.c @@ -1118,6 +1118,17 @@ asahi_ioctl_queue_create(int fd, unsigned long request, void *arg) { const struct drm_asahi_queue_create *create = arg; + /* Model authorization and a credential change after the screen probe. */ + static unsigned high_requests; + const char *policy = getenv("ASAHI_SHIM_HIGH_PRIORITY"); + if (create->priority == DRM_ASAHI_PRIORITY_HIGH) { + high_requests++; + if (policy && (!strcmp(policy, "deny") || + (!strcmp(policy, "once") && high_requests > 1))) { + errno = EACCES; + return -1; + } + } shim_usc_exec_base = create->usc_exec_base; return 0; } diff --git a/src/asahi/lib/agx_device.c b/src/asahi/lib/agx_device.c index b24942a8ebae..909075184896 100644 --- a/src/asahi/lib/agx_device.c +++ b/src/asahi/lib/agx_device.c @@ -1539,6 +1539,27 @@ agx_close_device(struct agx_device *dev) dev->fd = -1; } +bool +agx_probe_high_priority(struct agx_device *dev) +{ + /* Bypass the debug shared-queue cache: it could be MEDIUM and cannot be + * destroyed through agx_destroy_command_queue in 1queue mode. + */ + struct drm_asahi_queue_create create = { + .vm_id = dev->vm_id, + .priority = DRM_ASAHI_PRIORITY_HIGH, + .usc_exec_base = dev->shader_base, + }; + unsigned request = DRM_IOCTL_ASAHI_QUEUE_CREATE; + request = _IOC(_IOC_DIR(request), _IOC_TYPE(request), _IOC_NR(request), + offsetof(struct drm_asahi_queue_create, render_scs_layout)); + if (dev->ops.simple_ioctl(dev, request, &create)) + return false; + + struct drm_asahi_queue_destroy destroy = {.queue_id = create.queue_id}; + return dev->ops.simple_ioctl(dev, DRM_IOCTL_ASAHI_QUEUE_DESTROY, &destroy) == 0; +} + uint32_t agx_create_command_queue(struct agx_device *dev, enum drm_asahi_priority priority) diff --git a/src/asahi/lib/agx_device.h b/src/asahi/lib/agx_device.h index f164a34d22f8..a5934a3c2fab 100644 --- a/src/asahi/lib/agx_device.h +++ b/src/asahi/lib/agx_device.h @@ -495,6 +495,7 @@ uint32_t agx_create_command_queue(struct agx_device *dev, uint32_t agx_create_command_queue_with_render_scs(struct agx_device *dev, enum drm_asahi_priority priority, const uint64_t layouts[2]); int agx_destroy_command_queue(struct agx_device *dev, uint32_t queue_id); +bool agx_probe_high_priority(struct agx_device *dev); int agx_import_sync_file(struct agx_device *dev, struct agx_bo *bo, int fd); int agx_export_sync_file(struct agx_device *dev, struct agx_bo *bo); diff --git a/src/egl/drivers/dri2/egl_dri2.c b/src/egl/drivers/dri2/egl_dri2.c index b5b97744e60e..d25a3724e14b 100644 --- a/src/egl/drivers/dri2/egl_dri2.c +++ b/src/egl/drivers/dri2/egl_dri2.c @@ -1318,6 +1318,24 @@ dri2_create_context(_EGLDisplay *disp, _EGLConfig *conf, if (!dri2_ctx->dri_context) goto cleanup; + /* EGL priority is a hint; report the priority actually selected by Gallium, + * including a fallback after credentials or DRM master changed. + */ + switch (driGetContextPriority(dri2_ctx->dri_context)) { + case PIPE_CONTEXT_PRIORITY_LOW: + dri2_ctx->base.ContextPriority = EGL_CONTEXT_PRIORITY_LOW_IMG; + break; + case PIPE_CONTEXT_PRIORITY_MEDIUM: + dri2_ctx->base.ContextPriority = EGL_CONTEXT_PRIORITY_MEDIUM_IMG; + break; + case PIPE_CONTEXT_PRIORITY_HIGH: + dri2_ctx->base.ContextPriority = EGL_CONTEXT_PRIORITY_HIGH_IMG; + break; + case PIPE_CONTEXT_PRIORITY_REALTIME: + dri2_ctx->base.ContextPriority = EGL_CONTEXT_PRIORITY_REALTIME_NV; + break; + } + mtx_unlock(&dri2_dpy->lock); return &dri2_ctx->base; diff --git a/src/gallium/auxiliary/driver_ddebug/dd_context.c b/src/gallium/auxiliary/driver_ddebug/dd_context.c index 1615e9d45589..e61716100fbc 100644 --- a/src/gallium/auxiliary/driver_ddebug/dd_context.c +++ b/src/gallium/auxiliary/driver_ddebug/dd_context.c @@ -854,6 +854,7 @@ dd_context_create(struct dd_screen *dscreen, struct pipe_context *pipe) dctx->pipe = pipe; dctx->base.priv = pipe->priv; /* expose wrapped priv data */ dctx->base.screen = &dscreen->base; + dctx->base.context_priority = pipe->context_priority; dctx->base.stream_uploader = pipe->stream_uploader; dctx->base.const_uploader = pipe->const_uploader; diff --git a/src/gallium/auxiliary/driver_trace/tr_context.c b/src/gallium/auxiliary/driver_trace/tr_context.c index 09babfaceb07..f8bd56ac32c0 100644 --- a/src/gallium/auxiliary/driver_trace/tr_context.c +++ b/src/gallium/auxiliary/driver_trace/tr_context.c @@ -2543,6 +2543,7 @@ trace_context_create(struct trace_screen *tr_scr, tr_ctx->base.priv = pipe->priv; /* expose wrapped priv data */ tr_ctx->base.screen = &tr_scr->base; + tr_ctx->base.context_priority = pipe->context_priority; tr_ctx->base.stream_uploader = pipe->stream_uploader; tr_ctx->base.const_uploader = pipe->const_uploader; diff --git a/src/gallium/auxiliary/util/u_threaded_context.c b/src/gallium/auxiliary/util/u_threaded_context.c index e21195dcd001..218eb094ab15 100644 --- a/src/gallium/auxiliary/util/u_threaded_context.c +++ b/src/gallium/auxiliary/util/u_threaded_context.c @@ -5645,6 +5645,7 @@ threaded_context_create(struct pipe_context *pipe, MAX2(pipe->screen->caps.constant_buffer_offset_alignment, 64); tc->base.priv = pipe; /* priv points to the wrapped driver context */ tc->base.screen = pipe->screen; + tc->base.context_priority = pipe->context_priority; tc->base.destroy = tc_destroy; tc->base.callback = tc_callback; diff --git a/src/gallium/drivers/asahi/agx_pipe.c b/src/gallium/drivers/asahi/agx_pipe.c index 0a850fee68fb..e8a8d079ae98 100644 --- a/src/gallium/drivers/asahi/agx_pipe.c +++ b/src/gallium/drivers/asahi/agx_pipe.c @@ -1959,7 +1959,7 @@ agx_create_context(struct pipe_screen *screen, void *priv, unsigned flags) ctx->queue_id = agx_create_command_queue(agx_device(screen), priority); - /* Kernels grant HIGH only on G17, to CAP_SYS_NICE or DRM-master callers; + /* HIGH requires backend support and CAP_SYS_NICE or DRM master; * otherwise fall back to MEDIUM. */ if (ctx->queue_id == UINT32_MAX && priority == DRM_ASAHI_PRIORITY_HIGH) { @@ -1971,6 +1971,11 @@ agx_create_context(struct pipe_screen *screen, void *priv, unsigned flags) ralloc_free(ctx); return NULL; } + pctx->context_priority = priority == DRM_ASAHI_PRIORITY_LOW + ? PIPE_CONTEXT_PRIORITY_LOW + : priority == DRM_ASAHI_PRIORITY_HIGH + ? PIPE_CONTEXT_PRIORITY_HIGH + : PIPE_CONTEXT_PRIORITY_MEDIUM; ctx->virt_ring_idx = priority + 1; pctx->destroy = agx_destroy_context; @@ -2392,8 +2397,14 @@ agx_init_screen_caps(struct pipe_screen *pscreen) caps->tes_layer_viewport = true; caps->context_priority_mask = - PIPE_CONTEXT_PRIORITY_LOW | PIPE_CONTEXT_PRIORITY_MEDIUM | - PIPE_CONTEXT_PRIORITY_HIGH | PIPE_CONTEXT_PRIORITY_REALTIME; + PIPE_CONTEXT_PRIORITY_LOW | PIPE_CONTEXT_PRIORITY_MEDIUM; + + /* A chip/HAL match cannot establish the caller's authorization. The probe + * uses a private queue and immediately destroys it, without submitting work. + * Recheck creation normally: credentials/master can change after this probe. + */ + if (agx_probe_high_priority(agx_device(pscreen))) + caps->context_priority_mask |= PIPE_CONTEXT_PRIORITY_HIGH; caps->min_line_width = caps->min_line_width_aa = caps->min_point_size = caps->min_point_size_aa = 1; diff --git a/src/gallium/drivers/asahi/meson.build b/src/gallium/drivers/asahi/meson.build index e6a2d6060553..75af619b7e29 100644 --- a/src/gallium/drivers/asahi/meson.build +++ b/src/gallium/drivers/asahi/meson.build @@ -155,6 +155,53 @@ if with_tests and with_tools.contains('drm-shim') build_by_default : false, ) + asahi_priority_test = executable( + 'asahi-priority', + files('tests/test-agx-priority.c'), + include_directories : [ + inc_gallium_aux, inc_gallium, inc_include, inc_src, inc_asahi, + inc_virtio_gpu, inc_virtio_vdrm, + ], + dependencies : [ + idep_nir, idep_mesautil, idep_agx_pack, dep_libdrm, idep_libagx, + idep_libagx_shaders_h, idep_xmlconfig, + ], + link_with : [ + libasahi, libasahiwinsys, libasahi_compiler, libasahi_lib, + libasahi_layout, libasahi_decode, libgallium, libgalliumvl_stub, + ], + ) + foreach policy : ['allow', 'deny', 'once'] + test( + 'asahi-priority-' + policy, + prog_python, + args : [files('tests/run-shim.py'), libasahi_noop_drm_shim, asahi_priority_test], + env : ['ASAHI_SHIM_CHIP=g13g'] + (policy == 'allow' ? [] : ['ASAHI_SHIM_HIGH_PRIORITY=' + policy]), + suite : ['asahi'], + ) + endforeach + + if with_egl and with_glvnd + asahi_egl_priority_test = executable( + 'asahi-egl-priority', + files('tests/test-agx-egl-priority.c'), + include_directories : [inc_include], + dependencies : [dep_dl], + ) + foreach policy : ['allow', 'deny', 'once'] + test( + 'asahi-egl-priority-' + policy, + prog_python, + args : [files('tests/run-shim.py'), libasahi_noop_drm_shim, asahi_egl_priority_test], + env : [ + 'ASAHI_SHIM_CHIP=g13g', + '__EGL_VENDOR_LIBRARY_FILENAMES=' + join_paths(meson.project_build_root(), 'src/egl/devenv_glvnd_egl.json'), + ] + (policy == 'allow' ? [] : ['ASAHI_SHIM_HIGH_PRIORITY=' + policy]), + suite : ['asahi'], + ) + endforeach + endif + asahi_submit_test = executable( 'asahi-submit', files('tests/test-agx-submit.c'), diff --git a/src/gallium/drivers/asahi/tests/test-agx-egl-priority.c b/src/gallium/drivers/asahi/tests/test-agx-egl-priority.c new file mode 100644 index 000000000000..86b5db1348a0 --- /dev/null +++ b/src/gallium/drivers/asahi/tests/test-agx-egl-priority.c @@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: MIT */ +/* No GPU submission: exercise the GLVND -> EGL -> DRI -> Gallium query path. */ +#include +#include +#include +#include +#include +#include +#define CHECK(x) do { if (!(x)) { fprintf(stderr, "%u: %s\n", __LINE__, #x); abort(); } } while (0) +#define LOAD(name) PFN##name##PROC name = (void *)dlsym(egl, #name) +int main(void) +{ + void *egl = dlopen("libEGL.so.1", RTLD_NOW | RTLD_GLOBAL); + CHECK(egl); + PFNEGLGETPLATFORMDISPLAYPROC get_display = (void *)dlsym(egl, "eglGetPlatformDisplay"); + PFNEGLINITIALIZEPROC initialize = (void *)dlsym(egl, "eglInitialize"); + PFNEGLBINDAPIPROC bind_api = (void *)dlsym(egl, "eglBindAPI"); + PFNEGLCREATECONTEXTPROC create = (void *)dlsym(egl, "eglCreateContext"); + PFNEGLQUERYCONTEXTPROC query = (void *)dlsym(egl, "eglQueryContext"); + PFNEGLDESTROYCONTEXTPROC destroy = (void *)dlsym(egl, "eglDestroyContext"); + PFNEGLTERMINATEPROC terminate = (void *)dlsym(egl, "eglTerminate"); + CHECK(get_display && initialize && bind_api && create && query && destroy && terminate); + EGLDisplay d = get_display(EGL_PLATFORM_SURFACELESS_MESA, EGL_DEFAULT_DISPLAY, NULL); + EGLint major, minor; + CHECK(initialize(d, &major, &minor)); + CHECK(bind_api(EGL_OPENGL_ES_API)); + EGLint attrs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_CONTEXT_PRIORITY_LEVEL_IMG, + EGL_CONTEXT_PRIORITY_HIGH_IMG, EGL_NONE}; + EGLContext c = create(d, NULL, EGL_NO_CONTEXT, attrs); + CHECK(c != EGL_NO_CONTEXT); + EGLint actual; + CHECK(query(d, c, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &actual)); + const char *policy = getenv("ASAHI_SHIM_HIGH_PRIORITY"); + CHECK(actual == (policy ? EGL_CONTEXT_PRIORITY_MEDIUM_IMG : EGL_CONTEXT_PRIORITY_HIGH_IMG)); + printf("PASS EGL queried effective priority %#x (policy=%s)\n", actual, policy ? policy : "allow"); + CHECK(destroy(d, c)); + CHECK(terminate(d)); + dlclose(egl); + return 0; +} diff --git a/src/gallium/drivers/asahi/tests/test-agx-priority.c b/src/gallium/drivers/asahi/tests/test-agx-priority.c new file mode 100644 index 000000000000..8729a9be3435 --- /dev/null +++ b/src/gallium/drivers/asahi/tests/test-agx-priority.c @@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: MIT */ +#include +#include +#include +#include "util/driconf.h" +#include "util/xmlconfig.h" +#include "agx_state.h" +#include "agx_public.h" +#define CHECK(x) do { if (!(x)) { fprintf(stderr, "%u: %s\n", __LINE__, #x); abort(); } } while (0) + +static unsigned creates, destroys; +static int create_error, destroy_error; +static int +probe_ioctl(struct agx_device *dev, unsigned request, void *arg) +{ + if (_IOC_NR(request) == _IOC_NR(DRM_IOCTL_ASAHI_QUEUE_CREATE)) { + struct drm_asahi_queue_create *q = arg; + CHECK(q->priority == DRM_ASAHI_PRIORITY_HIGH); + CHECK(q->vm_id == dev->vm_id && q->usc_exec_base == dev->shader_base); + CHECK(_IOC_SIZE(request) == offsetof(struct drm_asahi_queue_create, render_scs_layout)); + creates++; + q->queue_id = 42; + return create_error; + } + CHECK(request == DRM_IOCTL_ASAHI_QUEUE_DESTROY); + CHECK(((struct drm_asahi_queue_destroy *)arg)->queue_id == 42); + destroys++; + return destroy_error; +} +int main(void) +{ + struct agx_device isolated = {0}; + isolated.debug = AGX_DBG_1QUEUE; + isolated.queue_id = 7; /* Probe must not reuse or destroy this shared queue. */ + isolated.ops.simple_ioctl = probe_ioctl; + CHECK(agx_probe_high_priority(&isolated)); + CHECK(creates == 1 && destroys == 1 && isolated.queue_id == 7); + create_error = -EACCES; + CHECK(!agx_probe_high_priority(&isolated)); + CHECK(creates == 2 && destroys == 1); + create_error = 0; destroy_error = -EIO; + CHECK(!agx_probe_high_priority(&isolated)); + CHECK(creates == 3 && destroys == 2); + + int fd = open("/dev/dri/renderD128", O_RDWR | O_CLOEXEC); + CHECK(fd >= 0); + static const driOptionDescription options[] = { +#include "driinfo_asahi.h" + }; + driOptionCache info, cache; + driParseOptionInfo(&info, options, ARRAY_SIZE(options)); + struct pipe_screen_config config = {.options = &cache, .options_info = &info}; + struct pipe_screen *screen = agx_screen_create(fd, NULL, &config); + CHECK(screen); + const char *policy = getenv("ASAHI_SHIM_HIGH_PRIORITY"); + unsigned mask = PIPE_CONTEXT_PRIORITY_LOW | PIPE_CONTEXT_PRIORITY_MEDIUM; + if (!policy || strcmp(policy, "deny")) mask |= PIPE_CONTEXT_PRIORITY_HIGH; + CHECK(screen->caps.context_priority_mask == mask); + unsigned flags[] = {0, PIPE_CONTEXT_LOW_PRIORITY, PIPE_CONTEXT_HIGH_PRIORITY}; + for (unsigned i = 0; i < ARRAY_SIZE(flags); i++) { + struct pipe_context *ctx = screen->context_create(screen, NULL, flags[i]); + CHECK(ctx); + unsigned expected = i == 1 ? PIPE_CONTEXT_PRIORITY_LOW : PIPE_CONTEXT_PRIORITY_MEDIUM; + if (i == 2 && !policy) expected = PIPE_CONTEXT_PRIORITY_HIGH; + CHECK(ctx->context_priority == expected); + ctx->destroy(ctx); + } + screen->destroy(screen); + close(fd); + puts("PASS probe cleanup, shared-cache bypass, mask, request flags and effective fallback"); + return 0; +} diff --git a/src/gallium/frontends/dri/dri_context.c b/src/gallium/frontends/dri/dri_context.c index cfeb490ecea6..597d62fce0aa 100644 --- a/src/gallium/frontends/dri/dri_context.c +++ b/src/gallium/frontends/dri/dri_context.c @@ -351,3 +351,10 @@ dri_get_current(void) } /* vim: set sw=3 ts=8 sts=3 expandtab: */ + +/* Zero preserves the historical hint for drivers without effective reporting. */ +unsigned +driGetContextPriority(struct dri_context *ctx) +{ + return ctx->st->pipe->context_priority; +} diff --git a/src/gallium/frontends/dri/dri_util.h b/src/gallium/frontends/dri/dri_util.h index cc92bc7ade51..8f06d2359513 100644 --- a/src/gallium/frontends/dri/dri_util.h +++ b/src/gallium/frontends/dri/dri_util.h @@ -147,6 +147,7 @@ PUBLIC int driCopyContext(struct dri_context *dest, struct dri_context *src, unsigned long mask); PUBLIC void driDestroyContext(struct dri_context *ctx); +PUBLIC unsigned driGetContextPriority(struct dri_context *ctx); PUBLIC int driBindContext(struct dri_context *ctx, struct dri_drawable *draw, struct dri_drawable *read); PUBLIC int driUnbindContext(struct dri_context *ctx); diff --git a/src/gallium/include/pipe/p_context.h b/src/gallium/include/pipe/p_context.h index 6bd3021479ac..89349ea55582 100644 --- a/src/gallium/include/pipe/p_context.h +++ b/src/gallium/include/pipe/p_context.h @@ -107,6 +107,12 @@ struct pipe_context { struct pipe_screen *screen; void *priv; /**< context private data (for DRI for example) */ + + /** Effective PIPE_CONTEXT_PRIORITY_* bit after creation; zero if unreported. + * Immutable for the lifetime of the context, including through wrappers. + */ + unsigned context_priority; + void *draw; /**< private, for draw module (temporary?) */ struct u_vbuf *vbuf; /**< for cso_context, don't use in drivers */ From 5c911612a205ea0891992d3945f162957da97b1f Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Wed, 4 Nov 2026 00:52:10 -0500 Subject: [PATCH 19/19] Fix shader cache identity, shared queue priority and render test ordering Signed-off-by: Satya Benson --- src/asahi/drm-shim/asahi_noop.c | 4 +++- src/asahi/lib/agx_device.c | 1 + src/asahi/lib/agx_device.h | 1 + src/asahi/vulkan/hk_physical_device.c | 6 ++++-- src/asahi/vulkan/hk_physical_device.h | 2 ++ src/asahi/vulkan/tests/test-hk-rt-on-demand.c | 9 +++++++++ src/asahi/vulkan/tests/vulkan_render_area.c | 12 ++++++++++++ src/gallium/drivers/asahi/agx_pipe.c | 3 +++ src/gallium/drivers/asahi/meson.build | 8 ++++++++ src/gallium/drivers/asahi/tests/test-agx-priority.c | 2 ++ 10 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/asahi/drm-shim/asahi_noop.c b/src/asahi/drm-shim/asahi_noop.c index 667263cf19e3..8c517210d985 100644 --- a/src/asahi/drm-shim/asahi_noop.c +++ b/src/asahi/drm-shim/asahi_noop.c @@ -1116,7 +1116,8 @@ asahi_ioctl_noop(int fd, unsigned long request, void *arg) static int asahi_ioctl_queue_create(int fd, unsigned long request, void *arg) { - const struct drm_asahi_queue_create *create = arg; + struct drm_asahi_queue_create *create = arg; + static uint32_t next_queue_id = 1; /* Model authorization and a credential change after the screen probe. */ static unsigned high_requests; @@ -1130,6 +1131,7 @@ asahi_ioctl_queue_create(int fd, unsigned long request, void *arg) } } shim_usc_exec_base = create->usc_exec_base; + create->queue_id = next_queue_id++; return 0; } diff --git a/src/asahi/lib/agx_device.c b/src/asahi/lib/agx_device.c index 909075184896..20006ada7af0 100644 --- a/src/asahi/lib/agx_device.c +++ b/src/asahi/lib/agx_device.c @@ -1613,6 +1613,7 @@ agx_create_command_queue_with_render_scs(struct agx_device *dev, if (dev->debug & AGX_DBG_1QUEUE) { dev->queue_id = queue_create.queue_id; + dev->queue_priority = priority; dev->queue_layouts[0] = layouts[0]; dev->queue_layouts[1] = layouts[1]; simple_mtx_unlock(&dev->vma_lock); diff --git a/src/asahi/lib/agx_device.h b/src/asahi/lib/agx_device.h index a5934a3c2fab..d05a2e93cc89 100644 --- a/src/asahi/lib/agx_device.h +++ b/src/asahi/lib/agx_device.h @@ -179,6 +179,7 @@ struct agx_device { uint32_t queue_id; /* AGX_DBG_1QUEUE: render SCS layouts the shared queue was created with. */ uint64_t queue_layouts[2]; + enum drm_asahi_priority queue_priority; /* VMA heaps */ simple_mtx_t vma_lock; diff --git a/src/asahi/vulkan/hk_physical_device.c b/src/asahi/vulkan/hk_physical_device.c index 91d20c3854cd..6cbbfab6bcb2 100644 --- a/src/asahi/vulkan/hk_physical_device.c +++ b/src/asahi/vulkan/hk_physical_device.c @@ -1666,7 +1666,7 @@ hk_env_cmp(const void *a, const void *b) return strcmp(*(const char *const *)a, *(const char *const *)b); } -static uint64_t +uint64_t hk_codegen_env_hash(void) { static const char *const runtime_only[] = { @@ -1688,7 +1688,9 @@ hk_codegen_env_hash(void) for (size_t o = 0; o + l <= name_len && !skip; ++o) skip = !strncmp(*e + o, runtime_only[i], l); } - if (!skip) + /* HK_PERFTEST includes code-generation choices such as noborder. */ + if (!skip || (name_len == strlen("HK_PERFTEST") && + !strncmp(*e, "HK_PERFTEST", name_len))) list[n++] = *e; } qsort(list, n, sizeof(list[0]), hk_env_cmp); diff --git a/src/asahi/vulkan/hk_physical_device.h b/src/asahi/vulkan/hk_physical_device.h index 93ab5701a6b1..79ab8a54ecc7 100644 --- a/src/asahi/vulkan/hk_physical_device.h +++ b/src/asahi/vulkan/hk_physical_device.h @@ -109,6 +109,8 @@ VkResult hk_create_drm_physical_device(struct vk_instance *vk_instance, void hk_physical_device_destroy(struct vk_physical_device *vk_device); +uint64_t hk_codegen_env_hash(void); + void hk_border_emulation_recall(struct hk_physical_device *pdev); void hk_border_emulation_remember(struct hk_physical_device *pdev); diff --git a/src/asahi/vulkan/tests/test-hk-rt-on-demand.c b/src/asahi/vulkan/tests/test-hk-rt-on-demand.c index e5bbd4725c0a..e4b1ba763359 100644 --- a/src/asahi/vulkan/tests/test-hk-rt-on-demand.c +++ b/src/asahi/vulkan/tests/test-hk-rt-on-demand.c @@ -441,6 +441,15 @@ main(void) test_cache_key(); + /* A noborder run must never replace border-enabled disk-cache entries. */ + unsetenv("HK_PERFTEST"); + uint64_t normal_env = hk_codegen_env_hash(); + setenv("HK_PERFTEST", "noborder", 1); + T("noborder has a separate disk-cache identity", + normal_env != hk_codegen_env_hash()); + unsetenv("HK_PERFTEST"); + T("normal disk-cache identity returns", normal_env == hk_codegen_env_hash()); + if (failures) { fprintf(stderr, "%u failure(s)\n", failures); return 1; diff --git a/src/asahi/vulkan/tests/vulkan_render_area.c b/src/asahi/vulkan/tests/vulkan_render_area.c index 9e4c6a992114..0a200228207e 100644 --- a/src/asahi/vulkan/tests/vulkan_render_area.c +++ b/src/asahi/vulkan/tests/vulkan_render_area.c @@ -100,6 +100,18 @@ static void pass(VkRect2D area, uint32_t colour, bool clear) { vkCmdDraw(cb, 3, 1, 0, 0); } vkCmdEndRendering(cb); + /* The next pass can LOAD pixels stored by this pass, including where the + * render areas overlap. Ending dynamic rendering supplies no dependency. */ + vkCmdPipelineBarrier2(cb, &(VkDependencyInfo){ + .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, + .memoryBarrierCount = 1, + .pMemoryBarriers = &(VkMemoryBarrier2){ + .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, + .srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, + .srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, + .dstAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_READ_BIT | + VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT}}); } static void submit(void) { diff --git a/src/gallium/drivers/asahi/agx_pipe.c b/src/gallium/drivers/asahi/agx_pipe.c index e8a8d079ae98..851c8c7ec737 100644 --- a/src/gallium/drivers/asahi/agx_pipe.c +++ b/src/gallium/drivers/asahi/agx_pipe.c @@ -1971,6 +1971,9 @@ agx_create_context(struct pipe_screen *screen, void *priv, unsigned flags) ralloc_free(ctx); return NULL; } + if (agx_device(screen)->debug & AGX_DBG_1QUEUE) + priority = agx_device(screen)->queue_priority; + pctx->context_priority = priority == DRM_ASAHI_PRIORITY_LOW ? PIPE_CONTEXT_PRIORITY_LOW : priority == DRM_ASAHI_PRIORITY_HIGH diff --git a/src/gallium/drivers/asahi/meson.build b/src/gallium/drivers/asahi/meson.build index 75af619b7e29..4cc3133a99aa 100644 --- a/src/gallium/drivers/asahi/meson.build +++ b/src/gallium/drivers/asahi/meson.build @@ -181,6 +181,14 @@ if with_tests and with_tools.contains('drm-shim') ) endforeach + test( + 'asahi-priority-shared', + prog_python, + args : [files('tests/run-shim.py'), libasahi_noop_drm_shim, asahi_priority_test], + env : ['ASAHI_SHIM_CHIP=g13g', 'ASAHI_MESA_DEBUG=1queue'], + suite : ['asahi'], + ) + if with_egl and with_glvnd asahi_egl_priority_test = executable( 'asahi-egl-priority', diff --git a/src/gallium/drivers/asahi/tests/test-agx-priority.c b/src/gallium/drivers/asahi/tests/test-agx-priority.c index 8729a9be3435..28a4a269cb27 100644 --- a/src/gallium/drivers/asahi/tests/test-agx-priority.c +++ b/src/gallium/drivers/asahi/tests/test-agx-priority.c @@ -62,6 +62,8 @@ int main(void) CHECK(ctx); unsigned expected = i == 1 ? PIPE_CONTEXT_PRIORITY_LOW : PIPE_CONTEXT_PRIORITY_MEDIUM; if (i == 2 && !policy) expected = PIPE_CONTEXT_PRIORITY_HIGH; + if (getenv("ASAHI_MESA_DEBUG") && strstr(getenv("ASAHI_MESA_DEBUG"), "1queue")) + expected = PIPE_CONTEXT_PRIORITY_MEDIUM; CHECK(ctx->context_priority == expected); ctx->destroy(ctx); }