diff --git a/src/gallium/drivers/asahi/agx_pipe.c b/src/gallium/drivers/asahi/agx_pipe.c index 9d8fef0894a1..d464d3d4b6f9 100644 --- a/src/gallium/drivers/asahi/agx_pipe.c +++ b/src/gallium/drivers/asahi/agx_pipe.c @@ -37,6 +37,7 @@ #include "util/os_misc.h" #include "util/simple_mtx.h" #include "util/timespec.h" +#include "util/u_cpu_detect.h" #include "util/u_drm.h" #include "util/u_gen_mipmap.h" #include "util/u_helpers.h" @@ -2415,6 +2416,18 @@ asahi_clear_buffer(struct pipe_context *pipe, struct pipe_resource *resource, clear_value_size); } +static void +agx_set_debug_callback(struct pipe_context *pctx, + const struct util_debug_callback *cb) +{ + struct agx_screen *screen = agx_screen(pctx->screen); + + if (util_queue_is_initialized(&screen->shader_compiler_queue)) + util_queue_finish(&screen->shader_compiler_queue); + + u_default_set_debug_callback(pctx, cb); +} + static struct pipe_context * agx_create_context(struct pipe_screen *screen, void *priv, unsigned flags) { @@ -2497,7 +2510,7 @@ agx_create_context(struct pipe_screen *screen, void *priv, unsigned flags) pctx->buffer_subdata = u_default_buffer_subdata; pctx->clear_buffer = asahi_clear_buffer; pctx->texture_subdata = agx_texture_subdata; - pctx->set_debug_callback = u_default_set_debug_callback; + pctx->set_debug_callback = agx_set_debug_callback; pctx->get_sample_position = u_default_get_sample_position; pctx->invalidate_resource = agx_invalidate_resource; pctx->memory_barrier = agx_memory_barrier; @@ -3162,6 +3175,9 @@ agx_destroy_screen(struct pipe_screen *pscreen) if (p_atomic_read(&screen->submit_quarantined)) return; + if (util_queue_is_initialized(&screen->shader_compiler_queue)) + util_queue_destroy(&screen->shader_compiler_queue); + drmSyncobjDestroy(screen->dev.fd, screen->flush_syncobj); if (screen->dev.ro) @@ -3200,6 +3216,26 @@ agx_get_disk_shader_cache(struct pipe_screen *pscreen) return agx_screen(pscreen)->disk_cache; } +static void +agx_set_max_shader_compiler_threads(struct pipe_screen *pscreen, + unsigned max_threads) +{ + struct agx_screen *screen = agx_screen(pscreen); + + if (util_queue_is_initialized(&screen->shader_compiler_queue)) + util_queue_adjust_num_threads(&screen->shader_compiler_queue, + max_threads, false); +} + +static bool +agx_is_parallel_shader_compilation_finished(struct pipe_screen *pscreen, + void *shader, + mesa_shader_stage shader_type) +{ + struct agx_uncompiled_shader *so = shader; + return !so || util_queue_fence_is_signalled(&so->ready); +} + static const struct u_transfer_vtbl transfer_vtbl = { .resource_create = agx_resource_create, .resource_destroy = agx_resource_destroy, @@ -3327,6 +3363,10 @@ agx_screen_create(int fd, struct renderonly *ro, screen->fence_finish = agx_fence_finish; screen->fence_get_fd = agx_fence_get_fd; screen->get_disk_shader_cache = agx_get_disk_shader_cache; + screen->set_max_shader_compiler_threads = + agx_set_max_shader_compiler_threads; + screen->is_parallel_shader_compilation_finished = + agx_is_parallel_shader_compilation_finished; screen->get_cl_cts_version = agx_get_cl_cts_version; /* G17G iterates no varying with a register coefficient index: have @@ -3353,6 +3393,14 @@ agx_screen_create(int fd, struct renderonly *ro, agx_disk_cache_init(agx_screen); + const unsigned num_threads = + CLAMP(util_get_cpu_caps()->nr_cpus, 1u, 32u); + util_queue_init(&agx_screen->shader_compiler_queue, "agx_sh", 64, + num_threads, + UTIL_QUEUE_INIT_RESIZE_IF_FULL | + UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY, + NULL); + /* TODO: Refactor readonly data? */ { struct agx_bo *bo = diff --git a/src/gallium/drivers/asahi/agx_state.c b/src/gallium/drivers/asahi/agx_state.c index 4d11a703052c..2d213daa74a3 100644 --- a/src/gallium/drivers/asahi/agx_state.c +++ b/src/gallium/drivers/asahi/agx_state.c @@ -3117,7 +3117,8 @@ agx_nir_offset_instance_id(nir_builder *b, nir_intrinsic_instr *intr, /* Does not take ownership of key. Clones if necessary. */ static struct agx_compiled_shader * -agx_compile_variant(struct agx_device *dev, struct pipe_context *pctx, +agx_compile_variant(struct agx_device *dev, + struct util_debug_callback *debug_cb, struct agx_uncompiled_shader *so, union asahi_shader_key *key_) { @@ -3132,8 +3133,9 @@ agx_compile_variant(struct agx_device *dev, struct pipe_context *pctx, * later API calls, once per context, and the count an application sees * would depend on earlier work rather than on its own commands. */ struct util_debug_callback *debug = - dev->chip != AGX_CHIP_G18G || so->report_shader_info ? &pctx->debug - : NULL; + debug_cb && (dev->chip != AGX_CHIP_G18G || so->report_shader_info) + ? debug_cb + : NULL; /* Auxiliary programs */ struct poly_gs_info gs_info = {0}; @@ -3723,7 +3725,8 @@ agx_compile_variant(struct agx_device *dev, struct pipe_context *pctx, } static struct agx_compiled_shader * -agx_get_shader_variant(struct agx_screen *screen, struct pipe_context *pctx, +agx_get_shader_variant(struct agx_screen *screen, + struct util_debug_callback *debug_cb, struct agx_uncompiled_shader *so, union asahi_shader_key *key) { @@ -3731,7 +3734,7 @@ agx_get_shader_variant(struct agx_screen *screen, struct pipe_context *pctx, agx_disk_cache_retrieve(screen, so, key); if (!compiled) { - compiled = agx_compile_variant(&screen->dev, pctx, so, key); + compiled = agx_compile_variant(&screen->dev, debug_cb, so, key); /* The serialized form carries only the binary and its info, so a * refused variant would come back from the cache indistinguishable @@ -3790,7 +3793,7 @@ agx_precompile_variant(struct agx_device *dev, struct pipe_context *pctx, union asahi_shader_key *key) { struct agx_compiled_shader *compiled = - agx_get_shader_variant(agx_screen(pctx->screen), pctx, so, key); + agx_get_shader_variant(agx_screen(pctx->screen), &pctx->debug, so, key); if (!cso->report_compile_error || (compiled && !compiled->failed)) return true; @@ -4309,6 +4312,117 @@ agx_shader_initialize(struct agx_device *dev, struct agx_uncompiled_shader *so, } } +static bool +agx_shader_compile_sync(struct agx_screen *screen, struct pipe_context *pctx, + bool report_compile_error) +{ + if (!util_queue_is_initialized(&screen->shader_compiler_queue)) + return true; + + /* G18G surfaces backend encoding refusals as link-time errors when st/mesa + * requests compile-error reporting (WebGL). */ + if (screen->dev.chip == AGX_CHIP_G18G && report_compile_error) + return true; + + if (pctx->debug.debug_message && !pctx->debug.async) + return true; + + if (screen->dev.debug & AGX_DBG_PRECOMPILE) + return true; + + if (agx_get_compiler_debug() & (AGX_DBG_SHADERS | AGX_DBG_SHADERDB | + AGX_DBG_VERBOSE | AGX_DBG_INTERNAL)) + return true; + + return false; +} + +static unsigned +agx_fill_precompile_keys(const struct agx_device *dev, + const struct agx_uncompiled_shader *so, + mesa_shader_stage next_stage, + union asahi_shader_key keys[2]) +{ + /* G17G runs geometry and tessellation stages on the CPU (agx_swtnl.c); + * their GPU variants are only built if a draw needs them. */ + const bool cpu_stage = agx_chip_g17_m5(dev->chip) && + (so->type == MESA_SHADER_TESS_CTRL || + so->type == MESA_SHADER_GEOMETRY); + if (!cpu_stage && ((so->type == MESA_SHADER_TESS_CTRL) || + (so->type == MESA_SHADER_GEOMETRY) || + (so->type == MESA_SHADER_FRAGMENT && + !so->info.uses_fbfetch))) { + union asahi_shader_key key = {0}; + if (so->type == MESA_SHADER_FRAGMENT) { + /* Match agx_update_fs()'s single-sample key so the precompiled + * variant hits on the first 1x draw instead of recompiling. */ + if (agx_chip_native_agx3(dev->chip) || agx_chip_g17_m5(dev->chip) || + dev->chip == AGX_CHIP_G18G || so->info.needs_sample_count || + so->info.uses_sample_shading) + key.fs.nr_samples = 1; + } + keys[0] = key; + return 1; + } + + if (so->type == MESA_SHADER_VERTEX) { + keys[0] = (union asahi_shader_key){ + .vs.hw = next_stage == MESA_SHADER_FRAGMENT, + }; + if (next_stage == MESA_SHADER_NONE) { + keys[1] = (union asahi_shader_key){ + .vs.hw = true, + }; + return 2; + } + return 1; + } + + return 0; +} + +static void +agx_precompile_shader_async(void *job, void *gdata, int thread_index) +{ + struct agx_uncompiled_shader *so = job; + struct agx_screen *screen = so->precompile.screen; + struct agx_device *dev = &screen->dev; + struct util_debug_callback *debug_cb = + so->precompile.debug.debug_message ? &so->precompile.debug : NULL; + + if (p_atomic_read(&so->precompile.cancelled)) { + ralloc_free(so->precompile.nir); + so->precompile.nir = NULL; + return; + } + + if (so->precompile.nir) { + agx_shader_initialize(dev, so, so->precompile.nir, + so->precompile.support_lod_bias, + so->precompile.robust); + ralloc_free(so->precompile.nir); + so->precompile.nir = NULL; + } + + if (p_atomic_read(&so->precompile.cancelled)) + return; + + if (so->type == MESA_SHADER_COMPUTE) { + union asahi_shader_key key = {0}; + agx_get_shader_variant(screen, debug_cb, so, &key); + return; + } + + union asahi_shader_key keys[2]; + const unsigned num_keys = + agx_fill_precompile_keys(dev, so, so->precompile.next_stage, keys); + for (unsigned i = 0; i < num_keys; ++i) { + if (p_atomic_read(&so->precompile.cancelled)) + return; + agx_get_shader_variant(screen, debug_cb, so, &keys[i]); + } +} + static void * agx_create_shader_state(struct pipe_context *pctx, const struct pipe_shader_state *cso) @@ -4316,17 +4430,22 @@ agx_create_shader_state(struct pipe_context *pctx, struct agx_context *ctx = agx_context(pctx); struct agx_uncompiled_shader *so = rzalloc(NULL, struct agx_uncompiled_shader); - struct agx_device *dev = agx_device(pctx->screen); + struct agx_screen *screen = agx_screen(pctx->screen); + struct agx_device *dev = &screen->dev; if (!so) return NULL; + util_queue_fence_init(&so->ready); so->base = *cso; nir_shader *nir = cso->type == PIPE_SHADER_IR_NIR ? cso->ir.nir : tgsi_to_nir(cso->tokens, pctx->screen, false); + so->type = nir->info.stage == MESA_SHADER_KERNEL ? MESA_SHADER_COMPUTE + : nir->info.stage; + /* TGSI reaches this driver only from gallium and state-tracker utilities * (u_blitter, u_simple_shaders); their NIR shaders come from nir_builder, * which marks them internal. */ @@ -4377,6 +4496,19 @@ agx_create_shader_state(struct pipe_context *pctx, if (agx_chip_g17_m5(dev->chip)) agx_xfb_cpu_create_vs(ctx, so, cso, nir); + if (!agx_shader_compile_sync(screen, pctx, cso->report_compile_error)) { + so->precompile.screen = screen; + so->precompile.nir = nir; + if (pctx->debug.debug_message) + so->precompile.debug = pctx->debug; + so->precompile.next_stage = nir->info.next_stage; + so->precompile.support_lod_bias = ctx->support_lod_bias; + so->precompile.robust = ctx->robust; + util_queue_add_job(&screen->shader_compiler_queue, so, &so->ready, + agx_precompile_shader_async, NULL, 0); + return so; + } + agx_shader_initialize(dev, so, nir, ctx->support_lod_bias, ctx->robust); mesa_shader_stage next_stage = nir->info.next_stage; @@ -4388,28 +4520,12 @@ agx_create_shader_state(struct pipe_context *pctx, * shader with a default key. This could be improved but hopefully this is * acceptable for now. */ - /* G17G runs geometry and tessellation stages on the CPU (agx_swtnl.c); - * their GPU variants are only built if a draw needs them. */ - const bool cpu_stage = agx_chip_g17_m5(dev->chip) && - (so->type == MESA_SHADER_TESS_CTRL || - so->type == MESA_SHADER_GEOMETRY); - if (!cpu_stage && ((so->type == MESA_SHADER_TESS_CTRL) || - (so->type == MESA_SHADER_GEOMETRY) || - (so->type == MESA_SHADER_FRAGMENT && - !so->info.uses_fbfetch))) { - union asahi_shader_key key = {0}; - if (!agx_precompile_variant(dev, pctx, so, cso, &key)) - return NULL; - } else if (so->type == MESA_SHADER_VERTEX) { - union asahi_shader_key key = { - .vs.hw = next_stage == MESA_SHADER_FRAGMENT, - }; - if (!agx_precompile_variant(dev, pctx, so, cso, &key)) - return NULL; - - if (next_stage == MESA_SHADER_NONE) { - key.vs.hw = true; - if (!agx_precompile_variant(dev, pctx, so, cso, &key)) + union asahi_shader_key keys[2]; + const unsigned num_keys = + agx_fill_precompile_keys(dev, so, next_stage, keys); + if (num_keys) { + for (unsigned i = 0; i < num_keys; ++i) { + if (!agx_precompile_variant(dev, pctx, so, cso, &keys[i])) return NULL; } } else if (dev->debug & AGX_DBG_PRECOMPILE) { @@ -4432,7 +4548,7 @@ agx_create_shader_state(struct pipe_context *pctx, UNREACHABLE("Unknown shader stage in shader-db precompile"); } - agx_compile_variant(dev, pctx, so, &key); + agx_compile_variant(dev, &pctx->debug, so, &key); } return so; @@ -4443,24 +4559,39 @@ agx_create_compute_state(struct pipe_context *pctx, const struct pipe_compute_state *cso) { struct agx_context *ctx = agx_context(pctx); - struct agx_device *dev = agx_device(pctx->screen); + struct agx_screen *screen = agx_screen(pctx->screen); + struct agx_device *dev = &screen->dev; struct agx_uncompiled_shader *so = rzalloc(NULL, struct agx_uncompiled_shader); if (!so) return NULL; + util_queue_fence_init(&so->ready); + so->type = MESA_SHADER_COMPUTE; so->variants = _mesa_hash_table_create(so, asahi_cs_shader_key_hash, asahi_cs_shader_key_equal); - union asahi_shader_key key = {0}; - assert(cso->ir_type == PIPE_SHADER_IR_NIR && "TGSI kernels unsupported"); nir_shader *nir = (void *)cso->prog; so->report_shader_info = !nir->info.internal; + if (!agx_shader_compile_sync(screen, pctx, false)) { + so->precompile.screen = screen; + so->precompile.nir = nir; + if (pctx->debug.debug_message) + so->precompile.debug = pctx->debug; + so->precompile.support_lod_bias = ctx->support_lod_bias; + so->precompile.robust = ctx->robust; + util_queue_add_job(&screen->shader_compiler_queue, so, &so->ready, + agx_precompile_shader_async, NULL, 0); + return so; + } + + union asahi_shader_key key = {0}; + agx_shader_initialize(dev, so, nir, ctx->support_lod_bias, ctx->robust); - agx_get_shader_variant(agx_screen(pctx->screen), pctx, so, &key); + agx_get_shader_variant(screen, &pctx->debug, so, &key); /* We're done with the NIR, throw it away */ ralloc_free(nir); @@ -4471,9 +4602,16 @@ static void agx_get_compute_state_info(struct pipe_context *pctx, void *cso, struct pipe_compute_state_object_info *info) { + struct agx_uncompiled_shader *uncompiled = cso; + agx_shader_wait_ready(uncompiled); + union asahi_shader_key key = {0}; + struct hash_entry *variant = + _mesa_hash_table_search(uncompiled->variants, &key); struct agx_compiled_shader *so = - agx_get_shader_variant(agx_screen(pctx->screen), pctx, cso, &key); + variant ? variant->data + : agx_get_shader_variant(agx_screen(pctx->screen), &pctx->debug, + uncompiled, &key); info->max_threads = agx_occupancy_for_register_count(so->b.info.nr_gprs).max_threads; @@ -4489,6 +4627,7 @@ agx_update_shader(struct agx_context *ctx, struct agx_compiled_shader **out, { struct agx_uncompiled_shader *so = ctx->stage[stage].shader; assert(so != NULL); + agx_shader_wait_ready(so); struct hash_entry *he = _mesa_hash_table_search(so->variants, key); @@ -4501,7 +4640,7 @@ agx_update_shader(struct agx_context *ctx, struct agx_compiled_shader **out, } struct agx_screen *screen = agx_screen(ctx->base.screen); - *out = agx_get_shader_variant(screen, &ctx->base, so, key); + *out = agx_get_shader_variant(screen, &ctx->base.debug, so, key); return true; } @@ -5221,8 +5360,10 @@ agx_update_tcs(struct agx_context *ctx, const struct pipe_draw_info *info) { assert(info->mode == MESA_PRIM_PATCHES); - struct hash_entry *he = _mesa_hash_table_next_entry( - ctx->stage[MESA_SHADER_TESS_CTRL].shader->variants, NULL); + struct agx_uncompiled_shader *tcs = ctx->stage[MESA_SHADER_TESS_CTRL].shader; + agx_shader_wait_ready(tcs); + + struct hash_entry *he = _mesa_hash_table_next_entry(tcs->variants, NULL); if (he) { ctx->tcs = he->data; return true; @@ -5715,9 +5856,16 @@ agx_bind_shader_state(struct pipe_context *pctx, void *cso, struct agx_uncompiled_shader *old = ctx->stage[stage].shader; struct agx_uncompiled_shader *next = cso; - if ((old && old->info.uses_query_lod) != - (next && next->info.uses_query_lod)) - ctx->stage[stage].dirty |= AGX_STAGE_DIRTY_SAMPLER; + if (stage == MESA_SHADER_FRAGMENT) { + const bool old_ready = + !old || util_queue_fence_is_signalled(&old->ready); + const bool next_ready = + !next || util_queue_fence_is_signalled(&next->ready); + if (!old_ready || !next_ready || + (old && old->info.uses_query_lod) != + (next && next->info.uses_query_lod)) + ctx->stage[stage].dirty |= AGX_STAGE_DIRTY_SAMPLER; + } ctx->stage[stage].shader = cso; } @@ -5782,6 +5930,16 @@ static void agx_delete_uncompiled_shader(struct agx_device *dev, struct agx_uncompiled_shader *so) { + if (so->precompile.screen) { + p_atomic_set(&so->precompile.cancelled, true); + util_queue_drop_job(&so->precompile.screen->shader_compiler_queue, + &so->ready); + } + util_queue_fence_wait(&so->ready); + ralloc_free(so->precompile.nir); + so->precompile.nir = NULL; + util_queue_fence_destroy(&so->ready); + hash_table_foreach(so->variants, ent) { agx_delete_compiled_shader(dev, ent->data); } @@ -16137,6 +16295,9 @@ agx_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info, if (unlikely(!agx_render_condition_check(ctx))) return; + for (unsigned s = 0; s < MESA_SHADER_COMPUTE; ++s) + agx_shader_wait_ready(ctx->stage[s].shader); + if (num_draws > 1) { agx_draw_multi(pctx, info, drawid_offset, indirect, draws, num_draws); return; @@ -17399,6 +17560,7 @@ agx_launch_grid(struct pipe_context *pipe, const struct pipe_grid_info *info) struct agx_uncompiled_shader *uncompiled = ctx->stage[MESA_SHADER_COMPUTE].shader; + agx_shader_wait_ready(uncompiled); union asahi_shader_key key = { .cs.custom_borders = @@ -17412,7 +17574,7 @@ agx_launch_grid(struct pipe_context *pipe, const struct pipe_grid_info *info) _mesa_hash_table_search(uncompiled->variants, &key); struct agx_compiled_shader *cs = variant ? variant->data - : agx_get_shader_variant(agx_screen(pipe->screen), pipe, + : agx_get_shader_variant(agx_screen(pipe->screen), &pipe->debug, uncompiled, &key); struct agx_grid grid; diff --git a/src/gallium/drivers/asahi/agx_state.h b/src/gallium/drivers/asahi/agx_state.h index b912b094eba9..1652c83a1ac1 100644 --- a/src/gallium/drivers/asahi/agx_state.h +++ b/src/gallium/drivers/asahi/agx_state.h @@ -34,6 +34,7 @@ #include "util/set.h" #include "util/slab.h" #include "util/u_atomic.h" +#include "util/u_queue.h" #include "util/u_range.h" #include "agx_bg_eot.h" #include "agx_helpers.h" @@ -459,6 +460,20 @@ struct agx_uncompiled_shader { /* Set on VS, passed to FS for linkage */ unsigned base_varying; + /* Fence signaled once asynchronous shader lowering and precompilation end. */ + struct util_queue_fence ready; + + /* Owned by the async shader worker until @ready is signaled. */ + struct { + struct agx_screen *screen; + nir_shader *nir; + struct util_debug_callback debug; + mesa_shader_stage next_stage; + bool support_lod_bias; + bool robust; + bool cancelled; + } precompile; + /* Tessellation info */ struct { uint64_t per_vertex_outputs; @@ -472,6 +487,13 @@ struct agx_uncompiled_shader { } tess; }; +static inline void +agx_shader_wait_ready(struct agx_uncompiled_shader *so) +{ + if (so) + util_queue_fence_wait(&so->ready); +} + enum agx_stage_dirty { AGX_STAGE_DIRTY_CONST = BITFIELD_BIT(0), AGX_STAGE_DIRTY_SSBO = BITFIELD_BIT(1), @@ -1338,6 +1360,7 @@ struct agx_screen { struct pipe_screen pscreen; struct agx_device dev; struct disk_cache *disk_cache; + struct util_queue shader_compiler_queue; /* Atomic: a context with unknown completion keeps the device alive. */ bool submit_quarantined; diff --git a/src/gallium/drivers/asahi/tests/test-agx-g17p-blit.c b/src/gallium/drivers/asahi/tests/test-agx-g17p-blit.c index f44b7dd15ed5..ca2e7dfffc8a 100644 --- a/src/gallium/drivers/asahi/tests/test-agx-g17p-blit.c +++ b/src/gallium/drivers/asahi/tests/test-agx-g17p-blit.c @@ -278,6 +278,7 @@ check_sampler_boundary_compile(struct pipe_context *pctx) }; struct agx_uncompiled_shader *shader = pctx->create_fs_state(pctx, &cso); + agx_shader_wait_ready(shader); const bool indirect = samplers[i] >= AGX_GALLIUM_G17P_IMMEDIATE_API_SAMPLERS; @@ -305,6 +306,7 @@ check_sampler_boundary_compile(struct pipe_context *pctx) }; struct agx_uncompiled_shader *dynamic = pctx->create_fs_state(pctx, &dynamic_cso); + agx_shader_wait_ready(dynamic); if (!dynamic || dynamic_cso.error_message || !dynamic->uses_bindless_samplers) { fprintf(stderr, "G17P dynamic sampler table did not compile: %s\n", @@ -434,6 +436,7 @@ check_query_lod_compile(struct pipe_context *pctx) .ir.nir = b.shader, }; struct agx_uncompiled_shader *shader = pctx->create_fs_state(pctx, &cso); + agx_shader_wait_ready(shader); const bool ok = shader && !cso.error_message && shader->info.uses_query_lod && shader->uses_bindless_samplers; @@ -1135,6 +1138,7 @@ static bool vertex_state_encoded(void *cso) { struct agx_uncompiled_shader *so = cso; + agx_shader_wait_ready(so); bool ok = true; hash_table_foreach(so->variants, ent) { struct agx_compiled_shader *cs = ent->data; @@ -1649,6 +1653,7 @@ check_discard_main_sample_key(struct pipe_context *pctx) void *ve_cso = pctx->create_vertex_elements_state(pctx, 0, NULL); if (!vs_cso || !fs_cso || !rast_cso || !depth_cso || !ve_cso) return false; + agx_shader_wait_ready(fs_cso); if (fs_cso->info.needs_sample_count || fs_cso->info.uses_fbfetch || fs_cso->info.uses_sample_shading) return false; diff --git a/src/gallium/drivers/asahi/tests/test-agx-threaded-context.c b/src/gallium/drivers/asahi/tests/test-agx-threaded-context.c index 4031cf028ef5..9c0bc6454b5c 100644 --- a/src/gallium/drivers/asahi/tests/test-agx-threaded-context.c +++ b/src/gallium/drivers/asahi/tests/test-agx-threaded-context.c @@ -14,6 +14,7 @@ #include "pipe/p_defines.h" #include "pipe/p_screen.h" #include "pipe/p_state.h" +#include "compiler/nir/nir_builder.h" #include "util/driconf.h" #include "util/u_inlines.h" #include "util/xmlconfig.h" @@ -356,6 +357,98 @@ test_threaded_context(struct pipe_screen *screen) pctx->destroy(pctx); } +static nir_shader * +build_simple_cs(void) +{ + nir_builder b = nir_builder_init_simple_shader( + MESA_SHADER_COMPUTE, &agx_nir_options, "test_parallel_cs"); + b.shader->info.workgroup_size[0] = 64; + b.shader->info.workgroup_size[1] = 1; + b.shader->info.workgroup_size[2] = 1; + return b.shader; +} + +static nir_shader * +build_simple_fs(void) +{ + nir_builder b = nir_builder_init_simple_shader( + MESA_SHADER_FRAGMENT, &agx_nir_options, "test_parallel_fs"); + nir_store_output(&b, nir_imm_vec4(&b, 1.0f, 0.5f, 0.25f, 1.0f), + nir_imm_int(&b, 0), .base = 0, .write_mask = 0xf, + .io_semantics.location = FRAG_RESULT_DATA0); + nir_shader_gather_info(b.shader, nir_shader_get_entrypoint(b.shader)); + return b.shader; +} + +static void +test_parallel_shader_compile(struct pipe_screen *screen) +{ + CHECK(screen->set_max_shader_compiler_threads); + CHECK(screen->is_parallel_shader_compilation_finished); + screen->set_max_shader_compiler_threads(screen, 2); + + struct pipe_context *pctx = + screen->context_create(screen, NULL, PIPE_CONTEXT_PREFER_THREADED); + CHECK(pctx); + struct pipe_context *raw = threaded_context_unwrap_sync(pctx); + CHECK(raw); + + /* Async compute create -> get_compute_state_info waits and reuses the + * precompiled variant without recompiling. */ + struct pipe_compute_state cs_state = { + .ir_type = PIPE_SHADER_IR_NIR, + .prog = build_simple_cs(), + }; + struct agx_uncompiled_shader *cs = pctx->create_compute_state(pctx, &cs_state); + CHECK(cs); + struct pipe_compute_state_object_info cs_info = {0}; + raw->get_compute_state_info(raw, cs, &cs_info); + CHECK(cs_info.preferred_simd_size == 32 && cs_info.max_threads > 0); + CHECK(screen->is_parallel_shader_compilation_finished(screen, cs, + MESA_SHADER_COMPUTE)); + CHECK(_mesa_hash_table_num_entries(cs->variants) == 1); + pctx->delete_compute_state(pctx, cs); + + /* Immediate create+delete burst exercises cancellation and drop without + * races or leaks. */ + for (unsigned i = 0; i < 4; ++i) { + struct pipe_compute_state burst_cs = { + .ir_type = PIPE_SHADER_IR_NIR, + .prog = build_simple_cs(), + }; + void *c = pctx->create_compute_state(pctx, &burst_cs); + CHECK(c); + pctx->delete_compute_state(pctx, c); + + struct pipe_shader_state burst_fs = { + .type = PIPE_SHADER_IR_NIR, + .ir.nir = build_simple_fs(), + }; + void *f = pctx->create_fs_state(pctx, &burst_fs); + CHECK(f); + pctx->delete_fs_state(pctx, f); + } + + /* Async fragment compile drains cleanly on set_debug_callback and on native + * AGX3 precompiles with nr_samples = 1 to match single-sample draws. */ + struct pipe_shader_state fs_state = { + .type = PIPE_SHADER_IR_NIR, + .ir.nir = build_simple_fs(), + }; + struct agx_uncompiled_shader *fs = pctx->create_fs_state(pctx, &fs_state); + CHECK(fs); + pctx->set_debug_callback(pctx, NULL); + CHECK(screen->is_parallel_shader_compilation_finished(screen, fs, + MESA_SHADER_FRAGMENT)); + if (agx_chip_native_agx3(agx_device(screen)->chip)) { + struct asahi_fs_shader_key key = {.nr_samples = 1}; + CHECK(_mesa_hash_table_search(fs->variants, &key) != NULL); + } + pctx->delete_fs_state(pctx, fs); + + pctx->destroy(pctx); +} + int main(void) { @@ -372,6 +465,7 @@ main(void) CHECK(screen); test_threaded_context(screen); + test_parallel_shader_compile(screen); screen->destroy(screen); driDestroyOptionInfo(&info);