Skip to content

Commit 7fc1ab4

Browse files
zmikeMarge Bot
authored and
Marge Bot
committed
zink: defer all compute shader creation
with other various changes, this should enable the entirety of compute shader creation to occur in the compile thread it also enables (slightly) simplifying shader create by moving cube check out to the caller Reviewed-by: Dave Airlie <[email protected]> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18197>
1 parent 3652ca0 commit 7fc1ab4

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

src/gallium/drivers/zink/zink_compiler.c

-1
Original file line numberDiff line numberDiff line change
@@ -3120,7 +3120,6 @@ zink_shader_create(struct zink_screen *screen, struct nir_shader *nir,
31203120
} else {
31213121
assert(var->data.mode == nir_var_uniform ||
31223122
var->data.mode == nir_var_image);
3123-
ret->has_cubes |= glsl_type_is_sampler(type) && glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE;
31243123
if (var->data.bindless) {
31253124
ret->bindless = true;
31263125
handle_bindless_var(nir, var, type, &bindless);

src/gallium/drivers/zink/zink_program.c

+41-41
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ update_cs_shader_module(struct zink_context *ctx, struct zink_compute_program *c
530530
void
531531
zink_update_compute_program(struct zink_context *ctx)
532532
{
533+
util_queue_fence_wait(&ctx->curr_compute->base.cache_fence);
533534
update_cs_shader_module(ctx, ctx->curr_compute);
534535
}
535536

@@ -740,8 +741,29 @@ precompile_compute_job(void *data, void *gdata, int thread_index)
740741
struct zink_compute_program *comp = data;
741742
struct zink_screen *screen = gdata;
742743

744+
comp->shader = zink_shader_create(screen, comp->nir, NULL);
745+
comp->curr = comp->module = CALLOC_STRUCT(zink_shader_module);
746+
assert(comp->module);
747+
comp->module->shader = zink_shader_compile(screen, comp->shader, comp->shader->nir, NULL);
748+
assert(comp->module->shader);
749+
util_dynarray_init(&comp->shader_cache[0], NULL);
750+
util_dynarray_init(&comp->shader_cache[1], NULL);
751+
752+
struct blob blob = {0};
753+
blob_init(&blob);
754+
nir_serialize(&blob, comp->shader->nir, true);
755+
756+
struct mesa_sha1 sha1_ctx;
757+
_mesa_sha1_init(&sha1_ctx);
758+
_mesa_sha1_update(&sha1_ctx, blob.data, blob.size);
759+
_mesa_sha1_final(&sha1_ctx, comp->base.sha1);
760+
blob_finish(&blob);
761+
762+
zink_descriptor_program_init(comp->base.ctx, &comp->base);
763+
743764
zink_screen_get_pipeline_cache(screen, &comp->base, true);
744-
comp->base_pipeline = zink_create_compute_pipeline(screen, comp, NULL);
765+
if (comp->base.can_precompile)
766+
comp->base_pipeline = zink_create_compute_pipeline(screen, comp, NULL);
745767
if (comp->base_pipeline)
746768
zink_screen_update_pipeline_cache(screen, &comp->base, true);
747769
}
@@ -752,49 +774,19 @@ create_compute_program(struct zink_context *ctx, nir_shader *nir)
752774
struct zink_screen *screen = zink_screen(ctx->base.screen);
753775
struct zink_compute_program *comp = create_program(ctx, true);
754776
if (!comp)
755-
goto fail;
756-
757-
comp->shader = zink_shader_create(screen, nir, NULL);
758-
comp->curr = comp->module = CALLOC_STRUCT(zink_shader_module);
759-
assert(comp->module);
760-
comp->module->shader = zink_shader_compile(screen, comp->shader, comp->shader->nir, NULL);
761-
assert(comp->module->shader);
762-
util_dynarray_init(&comp->shader_cache[0], NULL);
763-
util_dynarray_init(&comp->shader_cache[1], NULL);
777+
return NULL;
778+
comp->nir = nir;
764779

765780
comp->use_local_size = !(nir->info.workgroup_size[0] ||
766781
nir->info.workgroup_size[1] ||
767782
nir->info.workgroup_size[2]);
768-
783+
comp->base.can_precompile = !comp->use_local_size && (screen->info.have_EXT_non_seamless_cube_map || !zink_shader_has_cubes(nir));
769784
_mesa_hash_table_init(&comp->pipelines, comp, NULL, comp->use_local_size ?
770785
equals_compute_pipeline_state_local_size :
771786
equals_compute_pipeline_state);
772-
773-
struct blob blob = {0};
774-
blob_init(&blob);
775-
nir_serialize(&blob, nir, true);
776-
777-
struct mesa_sha1 sha1_ctx;
778-
_mesa_sha1_init(&sha1_ctx);
779-
_mesa_sha1_update(&sha1_ctx, blob.data, blob.size);
780-
_mesa_sha1_final(&sha1_ctx, comp->base.sha1);
781-
blob_finish(&blob);
782-
783-
if (!zink_descriptor_program_init(ctx, &comp->base))
784-
goto fail;
785-
786-
if (comp->use_local_size || (!screen->info.have_EXT_non_seamless_cube_map && comp->shader->has_cubes)) {
787-
zink_screen_get_pipeline_cache(screen, &comp->base, false);
788-
} else {
789-
comp->base.can_precompile = true;
790-
util_queue_add_job(&screen->cache_get_thread, comp, &comp->base.cache_fence, precompile_compute_job, NULL, 0);
791-
}
787+
util_queue_add_job(&screen->cache_get_thread, comp, &comp->base.cache_fence,
788+
precompile_compute_job, NULL, 0);
792789
return comp;
793-
794-
fail:
795-
if (comp)
796-
zink_destroy_compute_program(ctx, comp);
797-
return NULL;
798790
}
799791

800792
uint32_t
@@ -988,6 +980,12 @@ zink_destroy_compute_program(struct zink_context *ctx,
988980
ralloc_free(comp);
989981
}
990982

983+
ALWAYS_INLINE static bool
984+
compute_can_shortcut(const struct zink_compute_program *comp)
985+
{
986+
return !comp->use_local_size && !comp->curr->num_uniforms && !comp->curr->has_nonseamless;
987+
}
988+
991989
VkPipeline
992990
zink_get_compute_pipeline(struct zink_screen *screen,
993991
struct zink_compute_program *comp,
@@ -1007,21 +1005,22 @@ zink_get_compute_pipeline(struct zink_screen *screen,
10071005
state->dirty = false;
10081006
state->final_hash ^= state->hash;
10091007
}
1010-
if (!comp->use_local_size && !comp->curr->num_uniforms && !comp->curr->has_nonseamless && comp->base_pipeline) {
1008+
1009+
util_queue_fence_wait(&comp->base.cache_fence);
1010+
if (comp->base_pipeline && compute_can_shortcut(comp)) {
10111011
state->pipeline = comp->base_pipeline;
10121012
return state->pipeline;
10131013
}
10141014
entry = _mesa_hash_table_search_pre_hashed(&comp->pipelines, state->final_hash, state);
10151015

10161016
if (!entry) {
1017-
util_queue_fence_wait(&comp->base.cache_fence);
10181017
VkPipeline pipeline = zink_create_compute_pipeline(screen, comp, state);
10191018

10201019
if (pipeline == VK_NULL_HANDLE)
10211020
return VK_NULL_HANDLE;
10221021

10231022
zink_screen_update_pipeline_cache(screen, &comp->base, false);
1024-
if (!comp->use_local_size && !comp->curr->num_uniforms && !comp->curr->has_nonseamless) {
1023+
if (compute_can_shortcut(comp)) {
10251024
/* don't add base pipeline to cache */
10261025
state->pipeline = comp->base_pipeline = pipeline;
10271026
return state->pipeline;
@@ -1234,7 +1233,7 @@ zink_bind_cs_state(struct pipe_context *pctx,
12341233
{
12351234
struct zink_context *ctx = zink_context(pctx);
12361235
struct zink_compute_program *comp = cso;
1237-
if (comp && comp->shader->nir->info.num_inlinable_uniforms)
1236+
if (comp && comp->nir->info.num_inlinable_uniforms)
12381237
ctx->shader_has_inlinable_uniforms_mask |= 1 << MESA_SHADER_COMPUTE;
12391238
else
12401239
ctx->shader_has_inlinable_uniforms_mask &= ~(1 << MESA_SHADER_COMPUTE);
@@ -1249,7 +1248,8 @@ zink_bind_cs_state(struct pipe_context *pctx,
12491248
ctx->curr_compute = comp;
12501249
if (comp && comp != ctx->curr_compute) {
12511250
ctx->compute_pipeline_state.module_hash = ctx->curr_compute->curr->hash;
1252-
ctx->compute_pipeline_state.module = ctx->curr_compute->curr->shader;
1251+
if (util_queue_fence_is_signalled(&comp->base.cache_fence))
1252+
ctx->compute_pipeline_state.module = ctx->curr_compute->curr->shader;
12531253
ctx->compute_pipeline_state.final_hash ^= ctx->compute_pipeline_state.module_hash;
12541254
if (ctx->compute_pipeline_state.key.base.nonseamless_cube_mask)
12551255
ctx->dirty_shader_stages |= BITFIELD_BIT(MESA_SHADER_COMPUTE);

src/gallium/drivers/zink/zink_types.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,6 @@ struct zink_shader {
596596
uint32_t ssbos_used; // bitfield of which ssbo indices are used
597597
bool bindless;
598598
bool can_inline;
599-
bool has_cubes;
600599
struct spirv_shader *spirv;
601600

602601
simple_mtx_t lock;
@@ -807,6 +806,8 @@ struct zink_compute_program {
807806

808807
bool use_local_size;
809808

809+
nir_shader *nir;
810+
810811
struct zink_shader_module *curr;
811812

812813
struct zink_shader_module *module; //base

0 commit comments

Comments
 (0)