2 * Copyright 2018 Collabora Ltd.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include "zink_program.h"
26 #include "zink_compiler.h"
27 #include "zink_context.h"
28 #include "zink_descriptors.h"
29 #include "zink_helpers.h"
30 #include "zink_pipeline.h"
31 #include "zink_render_pass.h"
32 #include "zink_resource.h"
33 #include "zink_screen.h"
34 #include "zink_state.h"
35 #include "zink_inlines.h"
37 #include "util/u_debug.h"
38 #include "util/u_memory.h"
39 #include "util/u_prim.h"
40 #include "nir_serialize.h"
42 /* for pipeline cache */
43 #define XXH_INLINE_ALL
44 #include "util/xxhash.h"
47 debug_describe_zink_gfx_program(char *buf, const struct zink_gfx_program *ptr)
49 sprintf(buf, "zink_gfx_program");
53 debug_describe_zink_compute_program(char *buf, const struct zink_compute_program *ptr)
55 sprintf(buf, "zink_compute_program");
58 ALWAYS_INLINE static bool
59 shader_key_matches_tcs_nongenerated(const struct zink_shader_module *zm, const struct zink_shader_key *key, unsigned num_uniforms)
61 if (zm->num_uniforms != num_uniforms || zm->has_nonseamless != !!key->base.nonseamless_cube_mask)
63 const uint32_t nonseamless_size = zm->has_nonseamless ? sizeof(uint32_t) : 0;
64 return (!nonseamless_size || !memcmp(zm->key + zm->key_size, &key->base.nonseamless_cube_mask, nonseamless_size)) &&
65 (!num_uniforms || !memcmp(zm->key + zm->key_size + nonseamless_size,
66 key->base.inlined_uniform_values, zm->num_uniforms * sizeof(uint32_t)));
69 ALWAYS_INLINE static bool
70 shader_key_matches(const struct zink_shader_module *zm,
71 const struct zink_shader_key *key, unsigned num_uniforms,
72 bool has_inline, bool has_nonseamless)
74 const uint32_t nonseamless_size = !has_nonseamless && zm->has_nonseamless ? sizeof(uint32_t) : 0;
76 if (zm->num_uniforms != num_uniforms ||
78 memcmp(zm->key + zm->key_size + nonseamless_size,
79 key->base.inlined_uniform_values, zm->num_uniforms * sizeof(uint32_t))))
82 if (!has_nonseamless) {
83 if (zm->has_nonseamless != !!key->base.nonseamless_cube_mask ||
84 (nonseamless_size && memcmp(zm->key + zm->key_size, &key->base.nonseamless_cube_mask, nonseamless_size)))
87 return !memcmp(zm->key, key, zm->key_size);
91 shader_module_hash(const struct zink_shader_module *zm)
93 const uint32_t nonseamless_size = zm->has_nonseamless ? sizeof(uint32_t) : 0;
94 unsigned key_size = zm->key_size + nonseamless_size + zm->num_uniforms * sizeof(uint32_t);
95 return _mesa_hash_data(zm->key, key_size);
98 ALWAYS_INLINE static void
99 gather_shader_module_info(struct zink_context *ctx, struct zink_screen *screen,
100 struct zink_shader *zs, struct zink_gfx_program *prog,
101 struct zink_gfx_pipeline_state *state,
102 bool has_inline, //is inlining enabled?
103 bool has_nonseamless, //is nonseamless ext present?
104 unsigned *inline_size, unsigned *nonseamless_size)
106 gl_shader_stage stage = zs->nir->info.stage;
107 struct zink_shader_key *key = &state->shader_keys.key[stage];
108 if (has_inline && ctx && zs->nir->info.num_inlinable_uniforms &&
109 ctx->inlinable_uniforms_valid_mask & BITFIELD64_BIT(stage)) {
110 if (zs->can_inline && (screen->is_cpu || prog->inlined_variant_count[stage] < ZINK_MAX_INLINED_VARIANTS))
111 *inline_size = zs->nir->info.num_inlinable_uniforms;
113 key->inline_uniforms = false;
115 if (!has_nonseamless && key->base.nonseamless_cube_mask)
116 *nonseamless_size = sizeof(uint32_t);
119 ALWAYS_INLINE static struct zink_shader_module *
120 create_shader_module_for_stage(struct zink_context *ctx, struct zink_screen *screen,
121 struct zink_shader *zs, struct zink_gfx_program *prog,
122 gl_shader_stage stage,
123 struct zink_gfx_pipeline_state *state,
124 unsigned inline_size, unsigned nonseamless_size,
125 bool has_inline, //is inlining enabled?
126 bool has_nonseamless) //is nonseamless ext present?
129 struct zink_shader_module *zm;
130 const struct zink_shader_key *key = &state->shader_keys.key[stage];
131 /* non-generated tcs won't use the shader key */
132 const bool is_nongenerated_tcs = stage == MESA_SHADER_TESS_CTRL && !zs->tcs.is_generated;
133 zm = malloc(sizeof(struct zink_shader_module) + key->size + (!has_nonseamless ? nonseamless_size : 0) + inline_size * sizeof(uint32_t));
137 unsigned patch_vertices = state->shader_keys.key[MESA_SHADER_TESS_CTRL ].key.tcs.patch_vertices;
138 if (stage == MESA_SHADER_TESS_CTRL && zs->tcs.is_generated && zs->spirv) {
139 assert(ctx); //TODO async
140 mod = zink_shader_tcs_compile(screen, zs, patch_vertices);
142 mod = zink_shader_compile(screen, zs, prog->nir[stage], key);
149 zm->num_uniforms = inline_size;
150 if (!is_nongenerated_tcs) {
151 zm->key_size = key->size;
152 memcpy(zm->key, key, key->size);
155 memset(zm->key, 0, key->size);
157 if (!has_nonseamless && nonseamless_size) {
158 /* nonseamless mask gets added to base key if it exists */
159 memcpy(zm->key + key->size, &key->base.nonseamless_cube_mask, nonseamless_size);
161 zm->has_nonseamless = has_nonseamless ? 0 : !!nonseamless_size;
163 memcpy(zm->key + key->size + nonseamless_size, key->base.inlined_uniform_values, inline_size * sizeof(uint32_t));
164 if (stage == MESA_SHADER_TESS_CTRL && zs->tcs.is_generated)
165 zm->hash = patch_vertices;
167 zm->hash = shader_module_hash(zm);
168 zm->default_variant = !inline_size && !util_dynarray_contains(&prog->shader_cache[stage][0][0], void*);
170 prog->inlined_variant_count[stage]++;
171 util_dynarray_append(&prog->shader_cache[stage][has_nonseamless ? 0 : !!nonseamless_size][!!inline_size], void*, zm);
175 ALWAYS_INLINE static struct zink_shader_module *
176 get_shader_module_for_stage(struct zink_context *ctx, struct zink_screen *screen,
177 struct zink_shader *zs, struct zink_gfx_program *prog,
178 gl_shader_stage stage,
179 struct zink_gfx_pipeline_state *state,
180 unsigned inline_size, unsigned nonseamless_size,
181 bool has_inline, //is inlining enabled?
182 bool has_nonseamless) //is nonseamless ext present?
184 const struct zink_shader_key *key = &state->shader_keys.key[stage];
185 /* non-generated tcs won't use the shader key */
186 const bool is_nongenerated_tcs = stage == MESA_SHADER_TESS_CTRL && !zs->tcs.is_generated;
188 struct util_dynarray *shader_cache = &prog->shader_cache[stage][!has_nonseamless ? !!nonseamless_size : 0][has_inline ? !!inline_size : 0];
189 unsigned count = util_dynarray_num_elements(shader_cache, struct zink_shader_module *);
190 struct zink_shader_module **pzm = shader_cache->data;
191 for (unsigned i = 0; i < count; i++) {
192 struct zink_shader_module *iter = pzm[i];
193 if (is_nongenerated_tcs) {
194 if (!shader_key_matches_tcs_nongenerated(iter, key, has_inline ? !!inline_size : 0))
197 if (stage == MESA_SHADER_VERTEX && iter->key_size != key->size)
199 if (!shader_key_matches(iter, key, inline_size, has_inline, has_nonseamless))
203 struct zink_shader_module *zero = pzm[0];
213 ALWAYS_INLINE static struct zink_shader_module *
214 create_shader_module_for_stage_optimal(struct zink_context *ctx, struct zink_screen *screen,
215 struct zink_shader *zs, struct zink_gfx_program *prog,
216 gl_shader_stage stage,
217 struct zink_gfx_pipeline_state *state)
220 struct zink_shader_module *zm;
222 unsigned mask = stage == MESA_SHADER_FRAGMENT ? BITFIELD_MASK(16) : BITFIELD_MASK(8);
223 if (zs == prog->last_vertex_stage) {
224 key = (uint16_t*)&state->shader_keys_optimal.key.vs_base;
225 } else if (stage == MESA_SHADER_FRAGMENT) {
226 key = (uint16_t*)&state->shader_keys_optimal.key.fs;
227 } else if (stage == MESA_SHADER_TESS_CTRL && zs->tcs.is_generated) {
228 key = (uint16_t*)&state->shader_keys_optimal.key.tcs;
232 size_t key_size = sizeof(uint16_t);
233 zm = calloc(1, sizeof(struct zink_shader_module) + (key ? key_size : 0));
237 if (stage == MESA_SHADER_TESS_CTRL && zs->tcs.is_generated && zs->spirv) {
238 assert(ctx); //TODO async
239 struct zink_tcs_key *tcs = (struct zink_tcs_key*)key;
240 mod = zink_shader_tcs_compile(screen, zs, tcs->patch_vertices);
242 mod = zink_shader_compile(screen, zs, prog->nir[stage], (struct zink_shader_key*)key);
249 /* non-generated tcs won't use the shader key */
250 const bool is_nongenerated_tcs = stage == MESA_SHADER_TESS_CTRL && !zs->tcs.is_generated;
251 if (key && !is_nongenerated_tcs) {
252 zm->key_size = key_size;
253 uint16_t *data = (uint16_t*)zm->key;
254 /* sanitize actual key bits */
255 *data = (*key) & mask;
257 zm->default_variant = !util_dynarray_contains(&prog->shader_cache[stage][0][0], void*);
258 util_dynarray_append(&prog->shader_cache[stage][0][0], void*, zm);
262 ALWAYS_INLINE static struct zink_shader_module *
263 get_shader_module_for_stage_optimal(struct zink_context *ctx, struct zink_screen *screen,
264 struct zink_shader *zs, struct zink_gfx_program *prog,
265 gl_shader_stage stage,
266 struct zink_gfx_pipeline_state *state)
268 /* non-generated tcs won't use the shader key */
269 const bool is_nongenerated_tcs = stage == MESA_SHADER_TESS_CTRL && !zs->tcs.is_generated;
271 unsigned mask = stage == MESA_SHADER_FRAGMENT ? BITFIELD_MASK(16) : BITFIELD_MASK(8);
272 if (zs == prog->last_vertex_stage) {
273 key = (uint16_t*)&ctx->gfx_pipeline_state.shader_keys_optimal.key.vs_base;
274 } else if (stage == MESA_SHADER_FRAGMENT) {
275 key = (uint16_t*)&ctx->gfx_pipeline_state.shader_keys_optimal.key.fs;
276 } else if (stage == MESA_SHADER_TESS_CTRL && zs->tcs.is_generated) {
277 key = (uint16_t*)&ctx->gfx_pipeline_state.shader_keys_optimal.key.tcs;
281 struct util_dynarray *shader_cache = &prog->shader_cache[stage][0][0];
282 unsigned count = util_dynarray_num_elements(shader_cache, struct zink_shader_module *);
283 struct zink_shader_module **pzm = shader_cache->data;
284 for (unsigned i = 0; i < count; i++) {
285 struct zink_shader_module *iter = pzm[i];
286 if (is_nongenerated_tcs) {
289 uint16_t val = (*key) & mask;
290 /* no key is bigger than uint16_t */
291 if (memcmp(iter->key, &val, sizeof(uint16_t)))
295 struct zink_shader_module *zero = pzm[0];
306 zink_destroy_shader_module(struct zink_screen *screen, struct zink_shader_module *zm)
308 VKSCR(DestroyShaderModule)(screen->dev, zm->shader, NULL);
313 destroy_shader_cache(struct zink_screen *screen, struct util_dynarray *sc)
315 while (util_dynarray_contains(sc, void*)) {
316 struct zink_shader_module *zm = util_dynarray_pop(sc, struct zink_shader_module*);
317 zink_destroy_shader_module(screen, zm);
321 ALWAYS_INLINE static void
322 update_gfx_shader_modules(struct zink_context *ctx,
323 struct zink_screen *screen,
324 struct zink_gfx_program *prog, uint32_t mask,
325 struct zink_gfx_pipeline_state *state,
326 bool has_inline, //is inlining enabled?
327 bool has_nonseamless) //is nonseamless ext present?
329 bool hash_changed = false;
330 bool default_variants = true;
331 assert(prog->modules[MESA_SHADER_VERTEX]);
332 uint32_t variant_hash = prog->last_variant_hash;
333 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
334 if (!(mask & BITFIELD_BIT(i)))
337 assert(prog->shaders[i]);
339 unsigned inline_size = 0, nonseamless_size = 0;
340 gather_shader_module_info(ctx, screen, prog->shaders[i], prog, state, has_inline, has_nonseamless, &inline_size, &nonseamless_size);
341 struct zink_shader_module *zm = get_shader_module_for_stage(ctx, screen, prog->shaders[i], prog, i, state,
342 inline_size, nonseamless_size, has_inline, has_nonseamless);
344 zm = create_shader_module_for_stage(ctx, screen, prog->shaders[i], prog, i, state,
345 inline_size, nonseamless_size, has_inline, has_nonseamless);
346 state->modules[i] = zm->shader;
347 if (prog->modules[i] == zm->shader)
349 variant_hash ^= prog->module_hash[i];
351 default_variants &= zm->default_variant;
352 prog->modules[i] = zm->shader;
353 prog->module_hash[i] = zm->hash;
355 if (zm->num_uniforms)
356 prog->inline_variants |= BITFIELD_BIT(i);
358 prog->inline_variants &= ~BITFIELD_BIT(i);
360 variant_hash ^= prog->module_hash[i];
363 if (hash_changed && state) {
364 if (default_variants)
365 prog->last_variant_hash = prog->default_variant_hash;
367 prog->last_variant_hash = variant_hash;
369 state->modules_changed = true;
373 ALWAYS_INLINE static void
374 update_gfx_shader_modules_optimal(struct zink_context *ctx,
375 struct zink_screen *screen,
376 struct zink_gfx_program *prog, uint32_t mask,
377 struct zink_gfx_pipeline_state *state)
379 assert(prog->modules[MESA_SHADER_VERTEX]);
380 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
381 if (!(mask & BITFIELD_BIT(i)))
384 assert(prog->shaders[i]);
386 struct zink_shader_module *zm = get_shader_module_for_stage_optimal(ctx, screen, prog->shaders[i], prog, i, state);
388 zm = create_shader_module_for_stage_optimal(ctx, screen, prog->shaders[i], prog, i, state);
389 if (prog->modules[i] == zm->shader)
391 state->modules_changed = true;
392 prog->modules[i] = zm->shader;
395 prog->last_variant_hash = state->shader_keys_optimal.key.val;
399 generate_gfx_program_modules(struct zink_context *ctx, struct zink_screen *screen, struct zink_gfx_program *prog, struct zink_gfx_pipeline_state *state)
401 assert(!prog->modules[MESA_SHADER_VERTEX]);
402 uint32_t variant_hash = 0;
403 bool default_variants = true;
404 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
405 if (!(prog->stages_present & BITFIELD_BIT(i)))
408 assert(prog->shaders[i]);
410 unsigned inline_size = 0, nonseamless_size = 0;
411 gather_shader_module_info(ctx, screen, prog->shaders[i], prog, state,
412 screen->driconf.inline_uniforms, screen->info.have_EXT_non_seamless_cube_map,
413 &inline_size, &nonseamless_size);
414 struct zink_shader_module *zm = create_shader_module_for_stage(ctx, screen, prog->shaders[i], prog, i, state,
415 inline_size, nonseamless_size,
416 screen->driconf.inline_uniforms, screen->info.have_EXT_non_seamless_cube_map);
417 state->modules[i] = zm->shader;
418 prog->modules[i] = zm->shader;
419 prog->module_hash[i] = zm->hash;
420 if (zm->num_uniforms)
421 prog->inline_variants |= BITFIELD_BIT(i);
422 default_variants &= zm->default_variant;
423 variant_hash ^= prog->module_hash[i];
426 p_atomic_dec(&prog->base.reference.count);
427 state->modules_changed = true;
429 prog->last_variant_hash = variant_hash;
430 if (default_variants)
431 prog->default_variant_hash = prog->last_variant_hash;
435 generate_gfx_program_modules_optimal(struct zink_context *ctx, struct zink_screen *screen, struct zink_gfx_program *prog, struct zink_gfx_pipeline_state *state)
437 assert(!prog->modules[MESA_SHADER_VERTEX]);
438 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
439 if (!(prog->stages_present & BITFIELD_BIT(i)))
442 assert(prog->shaders[i]);
444 struct zink_shader_module *zm = create_shader_module_for_stage_optimal(ctx, screen, prog->shaders[i], prog, i, state);
445 prog->modules[i] = zm->shader;
448 p_atomic_dec(&prog->base.reference.count);
449 state->modules_changed = true;
450 prog->last_variant_hash = state->shader_keys_optimal.key.val;
454 hash_pipeline_lib_generated_tcs(const void *key)
461 equals_pipeline_lib_generated_tcs(const void *a, const void *b)
463 return !memcmp(a, b, sizeof(uint32_t));
467 hash_pipeline_lib(const void *key)
469 const struct zink_gfx_library_key *gkey = key;
470 /* remove generated tcs bits */
471 return zink_shader_key_optimal_no_tcs(gkey->optimal_key);
475 equals_pipeline_lib(const void *a, const void *b)
477 const struct zink_gfx_library_key *ak = a;
478 const struct zink_gfx_library_key *bk = b;
479 /* remove generated tcs bits */
480 uint32_t val_a = zink_shader_key_optimal_no_tcs(ak->optimal_key);
481 uint32_t val_b = zink_shader_key_optimal_no_tcs(bk->optimal_key);
482 return val_a == val_b;
486 hash_gfx_input_dynamic(const void *key)
488 const struct zink_gfx_input_key *ikey = key;
493 equals_gfx_input_dynamic(const void *a, const void *b)
495 const struct zink_gfx_input_key *ikey_a = a;
496 const struct zink_gfx_input_key *ikey_b = b;
497 return ikey_a->idx == ikey_b->idx;
501 hash_gfx_input(const void *key)
503 const struct zink_gfx_input_key *ikey = key;
504 if (ikey->uses_dynamic_stride)
506 return _mesa_hash_data(key, offsetof(struct zink_gfx_input_key, pipeline));
510 equals_gfx_input(const void *a, const void *b)
512 const struct zink_gfx_input_key *ikey_a = a;
513 const struct zink_gfx_input_key *ikey_b = b;
514 if (ikey_a->uses_dynamic_stride)
515 return ikey_a->element_state == ikey_b->element_state &&
516 !memcmp(a, b, offsetof(struct zink_gfx_input_key, vertex_buffers_enabled_mask));
517 return !memcmp(a, b, offsetof(struct zink_gfx_input_key, pipeline));
521 hash_gfx_output_ds3(const void *key)
523 const uint8_t *data = key;
524 return _mesa_hash_data(data, sizeof(uint32_t));
528 equals_gfx_output_ds3(const void *a, const void *b)
530 const uint8_t *da = a;
531 const uint8_t *db = b;
532 return !memcmp(da, db, sizeof(uint32_t));
536 hash_gfx_output(const void *key)
538 const uint8_t *data = key;
539 return _mesa_hash_data(data, offsetof(struct zink_gfx_output_key, pipeline));
543 equals_gfx_output(const void *a, const void *b)
545 const uint8_t *da = a;
546 const uint8_t *db = b;
547 return !memcmp(da, db, offsetof(struct zink_gfx_output_key, pipeline));
550 ALWAYS_INLINE static void
551 update_gfx_program_nonseamless(struct zink_context *ctx, struct zink_gfx_program *prog, bool has_nonseamless)
553 struct zink_screen *screen = zink_screen(ctx->base.screen);
554 if (screen->driconf.inline_uniforms)
555 update_gfx_shader_modules(ctx, screen, prog,
556 ctx->dirty_gfx_stages & prog->stages_present, &ctx->gfx_pipeline_state,
557 true, has_nonseamless);
559 update_gfx_shader_modules(ctx, screen, prog,
560 ctx->dirty_gfx_stages & prog->stages_present, &ctx->gfx_pipeline_state,
561 false, has_nonseamless);
565 update_gfx_program(struct zink_context *ctx, struct zink_gfx_program *prog)
567 struct zink_screen *screen = zink_screen(ctx->base.screen);
568 if (screen->info.have_EXT_non_seamless_cube_map)
569 update_gfx_program_nonseamless(ctx, prog, true);
571 update_gfx_program_nonseamless(ctx, prog, false);
575 zink_gfx_program_update(struct zink_context *ctx)
577 if (ctx->last_vertex_stage_dirty) {
578 gl_shader_stage pstage = ctx->last_vertex_stage->nir->info.stage;
579 ctx->dirty_gfx_stages |= BITFIELD_BIT(pstage);
580 memcpy(&ctx->gfx_pipeline_state.shader_keys.key[pstage].key.vs_base,
581 &ctx->gfx_pipeline_state.shader_keys.last_vertex.key.vs_base,
582 sizeof(struct zink_vs_key_base));
583 ctx->last_vertex_stage_dirty = false;
585 if (ctx->gfx_dirty) {
586 struct zink_gfx_program *prog = NULL;
588 simple_mtx_lock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
589 struct hash_table *ht = &ctx->program_cache[zink_program_cache_stages(ctx->shader_stages)];
590 const uint32_t hash = ctx->gfx_hash;
591 struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(ht, hash, ctx->gfx_stages);
593 prog = (struct zink_gfx_program*)entry->data;
594 for (unsigned i = 0; i < ZINK_GFX_SHADER_COUNT; i++) {
595 if (prog->stages_present & ~ctx->dirty_gfx_stages & BITFIELD_BIT(i))
596 ctx->gfx_pipeline_state.modules[i] = prog->modules[i];
598 /* ensure variants are always updated if keys have changed since last use */
599 ctx->dirty_gfx_stages |= prog->stages_present;
600 update_gfx_program(ctx, prog);
602 ctx->dirty_gfx_stages |= ctx->shader_stages;
603 prog = zink_create_gfx_program(ctx, ctx->gfx_stages, ctx->gfx_pipeline_state.dyn_state2.vertices_per_patch);
604 zink_screen_get_pipeline_cache(zink_screen(ctx->base.screen), &prog->base, false);
605 _mesa_hash_table_insert_pre_hashed(ht, hash, prog->shaders, prog);
606 generate_gfx_program_modules(ctx, zink_screen(ctx->base.screen), prog, &ctx->gfx_pipeline_state);
608 simple_mtx_unlock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
609 if (prog && prog != ctx->curr_program)
610 zink_batch_reference_program(&ctx->batch, &prog->base);
611 if (ctx->curr_program)
612 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
613 ctx->curr_program = prog;
614 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
615 ctx->gfx_dirty = false;
616 } else if (ctx->dirty_gfx_stages) {
617 /* remove old hash */
618 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
619 update_gfx_program(ctx, ctx->curr_program);
621 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
623 ctx->dirty_gfx_stages = 0;
626 ALWAYS_INLINE static void
627 update_gfx_shader_module_optimal(struct zink_context *ctx, struct zink_gfx_program *prog, gl_shader_stage pstage)
629 struct zink_screen *screen = zink_screen(ctx->base.screen);
630 if (screen->info.have_EXT_graphics_pipeline_library)
631 util_queue_fence_wait(&prog->base.cache_fence);
632 struct zink_shader_module *zm = get_shader_module_for_stage_optimal(ctx, screen, prog->shaders[pstage], prog, pstage, &ctx->gfx_pipeline_state);
634 zm = create_shader_module_for_stage_optimal(ctx, screen, prog->shaders[pstage], prog, pstage, &ctx->gfx_pipeline_state);
635 prog->modules[pstage] = zm->shader;
639 update_gfx_program_optimal(struct zink_context *ctx, struct zink_gfx_program *prog)
641 const union zink_shader_key_optimal *optimal_key = (union zink_shader_key_optimal*)&prog->last_variant_hash;
642 if (ctx->gfx_pipeline_state.shader_keys_optimal.key.vs_bits != optimal_key->vs_bits) {
643 update_gfx_shader_module_optimal(ctx, prog, ctx->last_vertex_stage->nir->info.stage);
644 ctx->gfx_pipeline_state.modules_changed = true;
646 if (ctx->gfx_pipeline_state.shader_keys_optimal.key.fs_bits != optimal_key->fs_bits) {
647 update_gfx_shader_module_optimal(ctx, prog, MESA_SHADER_FRAGMENT);
648 ctx->gfx_pipeline_state.modules_changed = true;
650 if (prog->shaders[MESA_SHADER_TESS_CTRL] && prog->shaders[MESA_SHADER_TESS_CTRL]->tcs.is_generated &&
651 ctx->gfx_pipeline_state.shader_keys_optimal.key.tcs_bits != optimal_key->tcs_bits) {
652 update_gfx_shader_module_optimal(ctx, prog, MESA_SHADER_TESS_CTRL);
653 ctx->gfx_pipeline_state.modules_changed = true;
655 prog->last_variant_hash = ctx->gfx_pipeline_state.shader_keys_optimal.key.val;
659 zink_gfx_program_update_optimal(struct zink_context *ctx)
661 if (ctx->gfx_dirty) {
662 struct zink_gfx_program *prog = NULL;
663 ctx->gfx_pipeline_state.optimal_key = ctx->gfx_pipeline_state.shader_keys_optimal.key.val;
664 struct hash_table *ht = &ctx->program_cache[zink_program_cache_stages(ctx->shader_stages)];
665 const uint32_t hash = ctx->gfx_hash;
666 simple_mtx_lock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
667 struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(ht, hash, ctx->gfx_stages);
669 if (ctx->curr_program)
670 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
672 prog = (struct zink_gfx_program*)entry->data;
673 update_gfx_program_optimal(ctx, prog);
675 ctx->dirty_gfx_stages |= ctx->shader_stages;
676 prog = zink_create_gfx_program(ctx, ctx->gfx_stages, ctx->gfx_pipeline_state.dyn_state2.vertices_per_patch);
677 zink_screen_get_pipeline_cache(zink_screen(ctx->base.screen), &prog->base, false);
678 _mesa_hash_table_insert_pre_hashed(ht, hash, prog->shaders, prog);
679 generate_gfx_program_modules_optimal(ctx, zink_screen(ctx->base.screen), prog, &ctx->gfx_pipeline_state);
681 simple_mtx_unlock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
682 if (prog && prog != ctx->curr_program)
683 zink_batch_reference_program(&ctx->batch, &prog->base);
684 ctx->curr_program = prog;
685 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
686 } else if (ctx->dirty_gfx_stages) {
687 /* remove old hash */
688 ctx->gfx_pipeline_state.optimal_key = ctx->gfx_pipeline_state.shader_keys_optimal.key.val;
689 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
690 update_gfx_program_optimal(ctx, ctx->curr_program);
692 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
694 ctx->dirty_gfx_stages = 0;
695 ctx->gfx_dirty = false;
696 ctx->last_vertex_stage_dirty = false;
700 optimized_compile_job(void *data, void *gdata, int thread_index)
702 struct zink_gfx_pipeline_cache_entry *pc_entry = data;
703 struct zink_screen *screen = gdata;
706 pipeline = zink_create_gfx_pipeline_combined(screen, pc_entry->prog, pc_entry->ikey->pipeline, pc_entry->gkey->pipeline, pc_entry->okey->pipeline, true);
708 pipeline = zink_create_gfx_pipeline(screen, pc_entry->prog, &pc_entry->state, pc_entry->state.element_state->binding_map, zink_primitive_topology(pc_entry->state.gfx_prim_mode), true);
710 pc_entry->unoptimized_pipeline = pc_entry->pipeline;
711 pc_entry->pipeline = pipeline;
716 zink_gfx_program_compile_queue(struct zink_context *ctx, struct zink_gfx_pipeline_cache_entry *pc_entry)
718 util_queue_add_job(&zink_screen(ctx->base.screen)->cache_get_thread, pc_entry, &pc_entry->fence, optimized_compile_job, NULL, 0);
722 update_cs_shader_module(struct zink_context *ctx, struct zink_compute_program *comp)
724 struct zink_screen *screen = zink_screen(ctx->base.screen);
725 struct zink_shader *zs = comp->shader;
727 struct zink_shader_module *zm = NULL;
728 unsigned inline_size = 0, nonseamless_size = 0;
729 struct zink_shader_key *key = &ctx->compute_pipeline_state.key;
731 if (ctx && zs->nir->info.num_inlinable_uniforms &&
732 ctx->inlinable_uniforms_valid_mask & BITFIELD64_BIT(MESA_SHADER_COMPUTE)) {
733 if (screen->is_cpu || comp->inlined_variant_count < ZINK_MAX_INLINED_VARIANTS)
734 inline_size = zs->nir->info.num_inlinable_uniforms;
736 key->inline_uniforms = false;
738 if (key->base.nonseamless_cube_mask)
739 nonseamless_size = sizeof(uint32_t);
741 if (inline_size || nonseamless_size) {
742 struct util_dynarray *shader_cache = &comp->shader_cache[!!nonseamless_size];
743 unsigned count = util_dynarray_num_elements(shader_cache, struct zink_shader_module *);
744 struct zink_shader_module **pzm = shader_cache->data;
745 for (unsigned i = 0; i < count; i++) {
746 struct zink_shader_module *iter = pzm[i];
747 if (!shader_key_matches(iter, key, inline_size,
748 screen->driconf.inline_uniforms,
749 screen->info.have_EXT_non_seamless_cube_map))
752 struct zink_shader_module *zero = pzm[0];
763 zm = malloc(sizeof(struct zink_shader_module) + nonseamless_size + inline_size * sizeof(uint32_t));
767 mod = zink_shader_compile(screen, zs, comp->shader->nir, key);
773 zm->num_uniforms = inline_size;
775 zm->has_nonseamless = !!nonseamless_size;
776 assert(nonseamless_size || inline_size);
777 if (nonseamless_size)
778 memcpy(zm->key, &key->base.nonseamless_cube_mask, nonseamless_size);
780 memcpy(zm->key + nonseamless_size, key->base.inlined_uniform_values, inline_size * sizeof(uint32_t));
781 zm->hash = shader_module_hash(zm);
782 zm->default_variant = false;
784 comp->inlined_variant_count++;
786 /* this is otherwise the default variant, which is stored as comp->module */
787 if (zm->num_uniforms || nonseamless_size)
788 util_dynarray_append(&comp->shader_cache[!!nonseamless_size], void*, zm);
790 if (comp->curr == zm)
792 ctx->compute_pipeline_state.final_hash ^= ctx->compute_pipeline_state.module_hash;
794 ctx->compute_pipeline_state.module_hash = zm->hash;
795 ctx->compute_pipeline_state.final_hash ^= ctx->compute_pipeline_state.module_hash;
796 ctx->compute_pipeline_state.module_changed = true;
800 zink_update_compute_program(struct zink_context *ctx)
802 util_queue_fence_wait(&ctx->curr_compute->base.cache_fence);
803 update_cs_shader_module(ctx, ctx->curr_compute);
807 zink_pipeline_layout_create(struct zink_screen *screen, VkDescriptorSetLayout *dsl, unsigned num_dsl, bool is_compute)
809 VkPipelineLayoutCreateInfo plci = {0};
810 plci.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
812 plci.pSetLayouts = dsl;
813 plci.setLayoutCount = num_dsl;
815 VkPushConstantRange pcr;
817 pcr.stageFlags = VK_SHADER_STAGE_ALL_GRAPHICS;
819 pcr.size = sizeof(struct zink_gfx_push_constant);
820 plci.pushConstantRangeCount = 1;
821 plci.pPushConstantRanges = &pcr;
824 VkPipelineLayout layout;
825 VkResult result = VKSCR(CreatePipelineLayout)(screen->dev, &plci, NULL, &layout);
826 if (result != VK_SUCCESS) {
827 mesa_loge("vkCreatePipelineLayout failed (%s)", vk_Result_to_str(result));
828 return VK_NULL_HANDLE;
835 create_program(struct zink_context *ctx, bool is_compute)
837 struct zink_program *pg = rzalloc_size(NULL, is_compute ? sizeof(struct zink_compute_program) : sizeof(struct zink_gfx_program));
841 pipe_reference_init(&pg->reference, 1);
842 util_queue_fence_init(&pg->cache_fence);
843 pg->is_compute = is_compute;
849 assign_io(struct zink_screen *screen,
850 struct zink_gfx_program *prog,
851 struct zink_shader *stages[ZINK_GFX_SHADER_COUNT])
853 struct zink_shader *shaders[MESA_SHADER_STAGES];
855 /* build array in pipeline order */
856 for (unsigned i = 0; i < ZINK_GFX_SHADER_COUNT; i++)
857 shaders[i] = stages[i];
859 for (unsigned i = 0; i < MESA_SHADER_FRAGMENT;) {
860 nir_shader *producer = shaders[i]->nir;
861 for (unsigned j = i + 1; j < ZINK_GFX_SHADER_COUNT; i++, j++) {
862 struct zink_shader *consumer = shaders[j];
865 if (!prog->nir[producer->info.stage])
866 prog->nir[producer->info.stage] = nir_shader_clone(prog, producer);
868 prog->nir[j] = nir_shader_clone(prog, consumer->nir);
869 zink_compiler_assign_io(screen, prog->nir[producer->info.stage], prog->nir[j]);
876 struct zink_gfx_program *
877 zink_create_gfx_program(struct zink_context *ctx,
878 struct zink_shader **stages,
879 unsigned vertices_per_patch)
881 struct zink_screen *screen = zink_screen(ctx->base.screen);
882 struct zink_gfx_program *prog = create_program(ctx, false);
888 for (int i = 0; i < ZINK_GFX_SHADER_COUNT; ++i) {
889 util_dynarray_init(&prog->shader_cache[i][0][0], NULL);
890 util_dynarray_init(&prog->shader_cache[i][0][1], NULL);
891 util_dynarray_init(&prog->shader_cache[i][1][0], NULL);
892 util_dynarray_init(&prog->shader_cache[i][1][1], NULL);
894 prog->shaders[i] = stages[i];
895 prog->stages_present |= BITFIELD_BIT(i);
898 bool generated_tcs = false;
899 if (stages[MESA_SHADER_TESS_EVAL] && !stages[MESA_SHADER_TESS_CTRL]) {
900 prog->shaders[MESA_SHADER_TESS_EVAL]->tes.generated =
901 prog->shaders[MESA_SHADER_TESS_CTRL] =
902 zink_shader_tcs_create(screen, stages[MESA_SHADER_VERTEX], vertices_per_patch);
903 prog->stages_present |= BITFIELD_BIT(MESA_SHADER_TESS_CTRL);
904 generated_tcs = true;
906 prog->stages_remaining = prog->stages_present;
908 assign_io(screen, prog, prog->shaders);
910 if (stages[MESA_SHADER_GEOMETRY])
911 prog->last_vertex_stage = stages[MESA_SHADER_GEOMETRY];
912 else if (stages[MESA_SHADER_TESS_EVAL])
913 prog->last_vertex_stage = stages[MESA_SHADER_TESS_EVAL];
915 prog->last_vertex_stage = stages[MESA_SHADER_VERTEX];
917 for (int r = 0; r < ARRAY_SIZE(prog->pipelines); ++r) {
918 for (int i = 0; i < ARRAY_SIZE(prog->pipelines[0]); ++i) {
919 _mesa_hash_table_init(&prog->pipelines[r][i], prog, NULL, zink_get_gfx_pipeline_eq_func(screen, prog));
920 /* only need first 3/4 for point/line/tri/patch */
921 if (screen->info.have_EXT_extended_dynamic_state &&
922 i == (prog->last_vertex_stage->nir->info.stage == MESA_SHADER_TESS_EVAL ? 4 : 3))
928 _mesa_set_init(&prog->libs, prog, hash_pipeline_lib_generated_tcs, equals_pipeline_lib_generated_tcs);
930 _mesa_set_init(&prog->libs, prog, hash_pipeline_lib, equals_pipeline_lib);
932 struct mesa_sha1 sctx;
933 _mesa_sha1_init(&sctx);
934 for (int i = 0; i < ZINK_GFX_SHADER_COUNT; ++i) {
935 if (prog->shaders[i]) {
936 simple_mtx_lock(&prog->shaders[i]->lock);
937 _mesa_set_add(prog->shaders[i]->programs, prog);
938 simple_mtx_unlock(&prog->shaders[i]->lock);
939 zink_gfx_program_reference(screen, NULL, prog);
940 _mesa_sha1_update(&sctx, prog->shaders[i]->base.sha1, sizeof(prog->shaders[i]->base.sha1));
943 _mesa_sha1_final(&sctx, prog->base.sha1);
945 if (!zink_descriptor_program_init(ctx, &prog->base))
952 zink_destroy_gfx_program(screen, prog);
957 hash_compute_pipeline_state_local_size(const void *key)
959 const struct zink_compute_pipeline_state *state = key;
960 uint32_t hash = _mesa_hash_data(state, offsetof(struct zink_compute_pipeline_state, hash));
961 hash = XXH32(&state->local_size[0], sizeof(state->local_size), hash);
966 hash_compute_pipeline_state(const void *key)
968 const struct zink_compute_pipeline_state *state = key;
969 return _mesa_hash_data(state, offsetof(struct zink_compute_pipeline_state, hash));
973 zink_program_update_compute_pipeline_state(struct zink_context *ctx, struct zink_compute_program *comp, const uint block[3])
975 if (comp->use_local_size) {
976 for (int i = 0; i < ARRAY_SIZE(ctx->compute_pipeline_state.local_size); i++) {
977 if (ctx->compute_pipeline_state.local_size[i] != block[i])
978 ctx->compute_pipeline_state.dirty = true;
979 ctx->compute_pipeline_state.local_size[i] = block[i];
985 equals_compute_pipeline_state(const void *a, const void *b)
987 const struct zink_compute_pipeline_state *sa = a;
988 const struct zink_compute_pipeline_state *sb = b;
989 return !memcmp(a, b, offsetof(struct zink_compute_pipeline_state, hash)) &&
990 sa->module == sb->module;
994 equals_compute_pipeline_state_local_size(const void *a, const void *b)
996 const struct zink_compute_pipeline_state *sa = a;
997 const struct zink_compute_pipeline_state *sb = b;
998 return !memcmp(a, b, offsetof(struct zink_compute_pipeline_state, hash)) &&
999 !memcmp(sa->local_size, sb->local_size, sizeof(sa->local_size)) &&
1000 sa->module == sb->module;
1004 precompile_compute_job(void *data, void *gdata, int thread_index)
1006 struct zink_compute_program *comp = data;
1007 struct zink_screen *screen = gdata;
1009 comp->shader = zink_shader_create(screen, comp->nir, NULL);
1010 comp->curr = comp->module = CALLOC_STRUCT(zink_shader_module);
1011 assert(comp->module);
1012 comp->module->shader = zink_shader_compile(screen, comp->shader, comp->shader->nir, NULL);
1013 assert(comp->module->shader);
1014 util_dynarray_init(&comp->shader_cache[0], NULL);
1015 util_dynarray_init(&comp->shader_cache[1], NULL);
1017 struct blob blob = {0};
1019 nir_serialize(&blob, comp->shader->nir, true);
1021 struct mesa_sha1 sha1_ctx;
1022 _mesa_sha1_init(&sha1_ctx);
1023 _mesa_sha1_update(&sha1_ctx, blob.data, blob.size);
1024 _mesa_sha1_final(&sha1_ctx, comp->base.sha1);
1027 zink_descriptor_program_init(comp->base.ctx, &comp->base);
1029 zink_screen_get_pipeline_cache(screen, &comp->base, true);
1030 if (comp->base.can_precompile)
1031 comp->base_pipeline = zink_create_compute_pipeline(screen, comp, NULL);
1032 if (comp->base_pipeline)
1033 zink_screen_update_pipeline_cache(screen, &comp->base, true);
1036 static struct zink_compute_program *
1037 create_compute_program(struct zink_context *ctx, nir_shader *nir)
1039 struct zink_screen *screen = zink_screen(ctx->base.screen);
1040 struct zink_compute_program *comp = create_program(ctx, true);
1045 comp->use_local_size = !(nir->info.workgroup_size[0] ||
1046 nir->info.workgroup_size[1] ||
1047 nir->info.workgroup_size[2]);
1048 comp->base.can_precompile = !comp->use_local_size && (screen->info.have_EXT_non_seamless_cube_map || !zink_shader_has_cubes(nir));
1049 _mesa_hash_table_init(&comp->pipelines, comp, NULL, comp->use_local_size ?
1050 equals_compute_pipeline_state_local_size :
1051 equals_compute_pipeline_state);
1052 util_queue_add_job(&screen->cache_get_thread, comp, &comp->base.cache_fence,
1053 precompile_compute_job, NULL, 0);
1058 zink_program_get_descriptor_usage(struct zink_context *ctx, gl_shader_stage stage, enum zink_descriptor_type type)
1060 struct zink_shader *zs = NULL;
1062 case MESA_SHADER_VERTEX:
1063 case MESA_SHADER_TESS_CTRL:
1064 case MESA_SHADER_TESS_EVAL:
1065 case MESA_SHADER_GEOMETRY:
1066 case MESA_SHADER_FRAGMENT:
1067 zs = ctx->gfx_stages[stage];
1069 case MESA_SHADER_COMPUTE: {
1070 zs = ctx->curr_compute->shader;
1074 unreachable("unknown shader type");
1079 case ZINK_DESCRIPTOR_TYPE_UBO:
1080 return zs->ubos_used;
1081 case ZINK_DESCRIPTOR_TYPE_SSBO:
1082 return zs->ssbos_used;
1083 case ZINK_DESCRIPTOR_TYPE_SAMPLER_VIEW:
1084 return BITSET_TEST_RANGE(zs->nir->info.textures_used, 0, PIPE_MAX_SAMPLERS - 1);
1085 case ZINK_DESCRIPTOR_TYPE_IMAGE:
1086 return BITSET_TEST_RANGE(zs->nir->info.images_used, 0, PIPE_MAX_SAMPLERS - 1);
1088 unreachable("unknown descriptor type!");
1094 zink_program_descriptor_is_buffer(struct zink_context *ctx, gl_shader_stage stage, enum zink_descriptor_type type, unsigned i)
1096 struct zink_shader *zs = NULL;
1098 case MESA_SHADER_VERTEX:
1099 case MESA_SHADER_TESS_CTRL:
1100 case MESA_SHADER_TESS_EVAL:
1101 case MESA_SHADER_GEOMETRY:
1102 case MESA_SHADER_FRAGMENT:
1103 zs = ctx->gfx_stages[stage];
1105 case MESA_SHADER_COMPUTE: {
1106 zs = ctx->curr_compute->shader;
1110 unreachable("unknown shader type");
1114 return zink_shader_descriptor_is_buffer(zs, type, i);
1118 get_num_bindings(struct zink_shader *zs, enum zink_descriptor_type type)
1121 case ZINK_DESCRIPTOR_TYPE_UNIFORMS:
1122 return !!zs->has_uniforms;
1123 case ZINK_DESCRIPTOR_TYPE_UBO:
1124 case ZINK_DESCRIPTOR_TYPE_SSBO:
1125 return zs->num_bindings[type];
1129 unsigned num_bindings = 0;
1130 for (int i = 0; i < zs->num_bindings[type]; i++)
1131 num_bindings += zs->bindings[type][i].size;
1132 return num_bindings;
1136 zink_program_num_bindings_typed(const struct zink_program *pg, enum zink_descriptor_type type)
1138 unsigned num_bindings = 0;
1139 if (pg->is_compute) {
1140 struct zink_compute_program *comp = (void*)pg;
1141 return get_num_bindings(comp->shader, type);
1143 struct zink_gfx_program *prog = (void*)pg;
1144 for (unsigned i = 0; i < ZINK_GFX_SHADER_COUNT; i++) {
1145 if (prog->shaders[i])
1146 num_bindings += get_num_bindings(prog->shaders[i], type);
1148 return num_bindings;
1152 zink_program_num_bindings(const struct zink_program *pg)
1154 unsigned num_bindings = 0;
1155 for (unsigned i = 0; i < ZINK_DESCRIPTOR_BASE_TYPES; i++)
1156 num_bindings += zink_program_num_bindings_typed(pg, i);
1157 return num_bindings;
1161 deinit_program(struct zink_screen *screen, struct zink_program *pg)
1163 util_queue_fence_wait(&pg->cache_fence);
1165 VKSCR(DestroyPipelineLayout)(screen->dev, pg->layout, NULL);
1167 if (pg->pipeline_cache)
1168 VKSCR(DestroyPipelineCache)(screen->dev, pg->pipeline_cache, NULL);
1169 zink_descriptor_program_deinit(screen, pg);
1173 zink_destroy_gfx_program(struct zink_screen *screen,
1174 struct zink_gfx_program *prog)
1176 unsigned max_idx = ARRAY_SIZE(prog->pipelines[0]);
1177 if (screen->info.have_EXT_extended_dynamic_state) {
1178 /* only need first 3/4 for point/line/tri/patch */
1179 if ((prog->stages_present &
1180 (BITFIELD_BIT(MESA_SHADER_TESS_EVAL) | BITFIELD_BIT(MESA_SHADER_GEOMETRY))) ==
1181 BITFIELD_BIT(MESA_SHADER_TESS_EVAL))
1188 for (unsigned r = 0; r < ARRAY_SIZE(prog->pipelines); r++) {
1189 for (int i = 0; i < max_idx; ++i) {
1190 hash_table_foreach(&prog->pipelines[r][i], entry) {
1191 struct zink_gfx_pipeline_cache_entry *pc_entry = entry->data;
1193 util_queue_fence_wait(&pc_entry->fence);
1194 VKSCR(DestroyPipeline)(screen->dev, pc_entry->pipeline, NULL);
1195 VKSCR(DestroyPipeline)(screen->dev, pc_entry->unoptimized_pipeline, NULL);
1201 deinit_program(screen, &prog->base);
1203 for (int i = 0; i < ZINK_GFX_SHADER_COUNT; ++i) {
1204 if (prog->shaders[i]) {
1205 _mesa_set_remove_key(prog->shaders[i]->programs, prog);
1206 prog->shaders[i] = NULL;
1208 destroy_shader_cache(screen, &prog->shader_cache[i][0][0]);
1209 destroy_shader_cache(screen, &prog->shader_cache[i][0][1]);
1210 destroy_shader_cache(screen, &prog->shader_cache[i][1][0]);
1211 destroy_shader_cache(screen, &prog->shader_cache[i][1][1]);
1212 ralloc_free(prog->nir[i]);
1215 set_foreach_remove(&prog->libs, he) {
1216 struct zink_gfx_library_key *gkey = (void*)he->key;
1217 VKSCR(DestroyPipeline)(screen->dev, gkey->pipeline, NULL);
1224 zink_destroy_compute_program(struct zink_screen *screen,
1225 struct zink_compute_program *comp)
1227 deinit_program(screen, &comp->base);
1230 _mesa_set_remove_key(comp->shader->programs, comp);
1231 destroy_shader_cache(screen, &comp->shader_cache[0]);
1232 destroy_shader_cache(screen, &comp->shader_cache[1]);
1234 hash_table_foreach(&comp->pipelines, entry) {
1235 struct compute_pipeline_cache_entry *pc_entry = entry->data;
1237 VKSCR(DestroyPipeline)(screen->dev, pc_entry->pipeline, NULL);
1240 VKSCR(DestroyPipeline)(screen->dev, comp->base_pipeline, NULL);
1241 VKSCR(DestroyShaderModule)(screen->dev, comp->module->shader, NULL);
1247 ALWAYS_INLINE static bool
1248 compute_can_shortcut(const struct zink_compute_program *comp)
1250 return !comp->use_local_size && !comp->curr->num_uniforms && !comp->curr->has_nonseamless;
1254 zink_get_compute_pipeline(struct zink_screen *screen,
1255 struct zink_compute_program *comp,
1256 struct zink_compute_pipeline_state *state)
1258 struct hash_entry *entry = NULL;
1260 if (!state->dirty && !state->module_changed)
1261 return state->pipeline;
1263 if (state->pipeline) //avoid on first hash
1264 state->final_hash ^= state->hash;
1265 if (comp->use_local_size)
1266 state->hash = hash_compute_pipeline_state_local_size(state);
1268 state->hash = hash_compute_pipeline_state(state);
1269 state->dirty = false;
1270 state->final_hash ^= state->hash;
1273 util_queue_fence_wait(&comp->base.cache_fence);
1274 if (comp->base_pipeline && compute_can_shortcut(comp)) {
1275 state->pipeline = comp->base_pipeline;
1276 return state->pipeline;
1278 entry = _mesa_hash_table_search_pre_hashed(&comp->pipelines, state->final_hash, state);
1281 VkPipeline pipeline = zink_create_compute_pipeline(screen, comp, state);
1283 if (pipeline == VK_NULL_HANDLE)
1284 return VK_NULL_HANDLE;
1286 zink_screen_update_pipeline_cache(screen, &comp->base, false);
1287 if (compute_can_shortcut(comp)) {
1288 /* don't add base pipeline to cache */
1289 state->pipeline = comp->base_pipeline = pipeline;
1290 return state->pipeline;
1293 struct compute_pipeline_cache_entry *pc_entry = CALLOC_STRUCT(compute_pipeline_cache_entry);
1295 return VK_NULL_HANDLE;
1297 memcpy(&pc_entry->state, state, sizeof(*state));
1298 pc_entry->pipeline = pipeline;
1300 entry = _mesa_hash_table_insert_pre_hashed(&comp->pipelines, state->final_hash, pc_entry, pc_entry);
1304 struct compute_pipeline_cache_entry *cache_entry = entry->data;
1305 state->pipeline = cache_entry->pipeline;
1306 return state->pipeline;
1309 ALWAYS_INLINE static void
1310 bind_gfx_stage(struct zink_context *ctx, gl_shader_stage stage, struct zink_shader *shader)
1312 if (shader && shader->nir->info.num_inlinable_uniforms)
1313 ctx->shader_has_inlinable_uniforms_mask |= 1 << stage;
1315 ctx->shader_has_inlinable_uniforms_mask &= ~(1 << stage);
1317 if (ctx->gfx_stages[stage])
1318 ctx->gfx_hash ^= ctx->gfx_stages[stage]->hash;
1319 ctx->gfx_stages[stage] = shader;
1320 ctx->gfx_dirty = ctx->gfx_stages[MESA_SHADER_FRAGMENT] && ctx->gfx_stages[MESA_SHADER_VERTEX];
1321 ctx->gfx_pipeline_state.modules_changed = true;
1323 ctx->shader_stages |= BITFIELD_BIT(stage);
1324 ctx->gfx_hash ^= ctx->gfx_stages[stage]->hash;
1326 ctx->gfx_pipeline_state.modules[stage] = VK_NULL_HANDLE;
1327 if (ctx->curr_program)
1328 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
1329 ctx->curr_program = NULL;
1330 ctx->shader_stages &= ~BITFIELD_BIT(stage);
1334 static enum pipe_prim_type
1335 gs_output_to_reduced_prim_type(struct shader_info *info)
1337 switch (info->gs.output_primitive) {
1338 case SHADER_PRIM_POINTS:
1339 return PIPE_PRIM_POINTS;
1341 case SHADER_PRIM_LINES:
1342 case SHADER_PRIM_LINE_LOOP:
1343 case SHADER_PRIM_LINE_STRIP:
1344 case SHADER_PRIM_LINES_ADJACENCY:
1345 case SHADER_PRIM_LINE_STRIP_ADJACENCY:
1346 return PIPE_PRIM_LINES;
1348 case SHADER_PRIM_TRIANGLES:
1349 case SHADER_PRIM_TRIANGLE_STRIP:
1350 case SHADER_PRIM_TRIANGLE_FAN:
1351 case SHADER_PRIM_TRIANGLES_ADJACENCY:
1352 case SHADER_PRIM_TRIANGLE_STRIP_ADJACENCY:
1353 return PIPE_PRIM_TRIANGLES;
1356 unreachable("unexpected output primitive type");
1360 static enum pipe_prim_type
1361 update_rast_prim(struct zink_shader *shader)
1363 struct shader_info *info = &shader->nir->info;
1364 if (info->stage == MESA_SHADER_GEOMETRY)
1365 return gs_output_to_reduced_prim_type(info);
1366 else if (info->stage == MESA_SHADER_TESS_EVAL) {
1367 if (info->tess.point_mode)
1368 return PIPE_PRIM_POINTS;
1370 switch (info->tess._primitive_mode) {
1371 case TESS_PRIMITIVE_ISOLINES:
1372 return PIPE_PRIM_LINES;
1373 case TESS_PRIMITIVE_TRIANGLES:
1374 case TESS_PRIMITIVE_QUADS:
1375 return PIPE_PRIM_TRIANGLES;
1377 return PIPE_PRIM_MAX;
1381 return PIPE_PRIM_MAX;
1385 bind_last_vertex_stage(struct zink_context *ctx)
1387 gl_shader_stage old = ctx->last_vertex_stage ? ctx->last_vertex_stage->nir->info.stage : MESA_SHADER_STAGES;
1388 if (ctx->gfx_stages[MESA_SHADER_GEOMETRY])
1389 ctx->last_vertex_stage = ctx->gfx_stages[MESA_SHADER_GEOMETRY];
1390 else if (ctx->gfx_stages[MESA_SHADER_TESS_EVAL])
1391 ctx->last_vertex_stage = ctx->gfx_stages[MESA_SHADER_TESS_EVAL];
1393 ctx->last_vertex_stage = ctx->gfx_stages[MESA_SHADER_VERTEX];
1394 gl_shader_stage current = ctx->last_vertex_stage ? ctx->last_vertex_stage->nir->info.stage : MESA_SHADER_VERTEX;
1396 /* update rast_prim */
1397 ctx->gfx_pipeline_state.shader_rast_prim =
1398 ctx->last_vertex_stage ? update_rast_prim(ctx->last_vertex_stage) :
1401 if (old != current) {
1402 if (!zink_screen(ctx->base.screen)->optimal_keys) {
1403 if (old != MESA_SHADER_STAGES) {
1404 memset(&ctx->gfx_pipeline_state.shader_keys.key[old].key.vs_base, 0, sizeof(struct zink_vs_key_base));
1405 ctx->dirty_gfx_stages |= BITFIELD_BIT(old);
1407 /* always unset vertex shader values when changing to a non-vs last stage */
1408 memset(&ctx->gfx_pipeline_state.shader_keys.key[MESA_SHADER_VERTEX].key.vs_base, 0, sizeof(struct zink_vs_key_base));
1412 unsigned num_viewports = ctx->vp_state.num_viewports;
1413 struct zink_screen *screen = zink_screen(ctx->base.screen);
1414 /* number of enabled viewports is based on whether last vertex stage writes viewport index */
1415 if (ctx->last_vertex_stage) {
1416 if (ctx->last_vertex_stage->nir->info.outputs_written & (VARYING_BIT_VIEWPORT | VARYING_BIT_VIEWPORT_MASK))
1417 ctx->vp_state.num_viewports = MIN2(screen->info.props.limits.maxViewports, PIPE_MAX_VIEWPORTS);
1419 ctx->vp_state.num_viewports = 1;
1421 ctx->vp_state.num_viewports = 1;
1423 ctx->vp_state_changed |= num_viewports != ctx->vp_state.num_viewports;
1424 if (!screen->info.have_EXT_extended_dynamic_state) {
1425 if (ctx->gfx_pipeline_state.dyn_state1.num_viewports != ctx->vp_state.num_viewports)
1426 ctx->gfx_pipeline_state.dirty = true;
1427 ctx->gfx_pipeline_state.dyn_state1.num_viewports = ctx->vp_state.num_viewports;
1429 ctx->last_vertex_stage_dirty = true;
1434 zink_bind_vs_state(struct pipe_context *pctx,
1437 struct zink_context *ctx = zink_context(pctx);
1438 if (!cso && !ctx->gfx_stages[MESA_SHADER_VERTEX])
1440 bind_gfx_stage(ctx, MESA_SHADER_VERTEX, cso);
1441 bind_last_vertex_stage(ctx);
1443 struct zink_shader *zs = cso;
1444 ctx->shader_reads_drawid = BITSET_TEST(zs->nir->info.system_values_read, SYSTEM_VALUE_DRAW_ID);
1445 ctx->shader_reads_basevertex = BITSET_TEST(zs->nir->info.system_values_read, SYSTEM_VALUE_BASE_VERTEX);
1447 ctx->shader_reads_drawid = false;
1448 ctx->shader_reads_basevertex = false;
1452 /* if gl_SampleMask[] is written to, we have to ensure that we get a shader with the same sample count:
1453 * in GL, samples==1 means ignore gl_SampleMask[]
1454 * in VK, gl_SampleMask[] is never ignored
1457 zink_update_fs_key_samples(struct zink_context *ctx)
1459 if (!ctx->gfx_stages[MESA_SHADER_FRAGMENT])
1461 nir_shader *nir = ctx->gfx_stages[MESA_SHADER_FRAGMENT]->nir;
1462 if (nir->info.outputs_written & (1 << FRAG_RESULT_SAMPLE_MASK)) {
1463 bool samples = zink_get_fs_key(ctx)->samples;
1464 if (samples != (ctx->fb_state.samples > 1))
1465 zink_set_fs_key(ctx)->samples = ctx->fb_state.samples > 1;
1470 zink_bind_fs_state(struct pipe_context *pctx,
1473 struct zink_context *ctx = zink_context(pctx);
1474 if (!cso && !ctx->gfx_stages[MESA_SHADER_FRAGMENT])
1476 bind_gfx_stage(ctx, MESA_SHADER_FRAGMENT, cso);
1477 ctx->fbfetch_outputs = 0;
1479 nir_shader *nir = ctx->gfx_stages[MESA_SHADER_FRAGMENT]->nir;
1480 if (nir->info.fs.uses_fbfetch_output) {
1481 nir_foreach_shader_out_variable(var, ctx->gfx_stages[MESA_SHADER_FRAGMENT]->nir) {
1482 if (var->data.fb_fetch_output)
1483 ctx->fbfetch_outputs |= BITFIELD_BIT(var->data.location - FRAG_RESULT_DATA0);
1486 zink_update_fs_key_samples(ctx);
1487 if (zink_screen(pctx->screen)->info.have_EXT_rasterization_order_attachment_access) {
1488 if (ctx->gfx_pipeline_state.rast_attachment_order != nir->info.fs.uses_fbfetch_output)
1489 ctx->gfx_pipeline_state.dirty = true;
1490 ctx->gfx_pipeline_state.rast_attachment_order = nir->info.fs.uses_fbfetch_output;
1493 zink_update_fbfetch(ctx);
1497 zink_bind_gs_state(struct pipe_context *pctx,
1500 struct zink_context *ctx = zink_context(pctx);
1501 if (!cso && !ctx->gfx_stages[MESA_SHADER_GEOMETRY])
1503 bind_gfx_stage(ctx, MESA_SHADER_GEOMETRY, cso);
1504 bind_last_vertex_stage(ctx);
1508 zink_bind_tcs_state(struct pipe_context *pctx,
1511 bind_gfx_stage(zink_context(pctx), MESA_SHADER_TESS_CTRL, cso);
1515 zink_bind_tes_state(struct pipe_context *pctx,
1518 struct zink_context *ctx = zink_context(pctx);
1519 if (!cso && !ctx->gfx_stages[MESA_SHADER_TESS_EVAL])
1521 if (!!ctx->gfx_stages[MESA_SHADER_TESS_EVAL] != !!cso) {
1523 /* if unsetting a TESS that uses a generated TCS, ensure the TCS is unset */
1524 if (ctx->gfx_stages[MESA_SHADER_TESS_EVAL]->tes.generated)
1525 ctx->gfx_stages[MESA_SHADER_TESS_CTRL] = NULL;
1528 bind_gfx_stage(ctx, MESA_SHADER_TESS_EVAL, cso);
1529 bind_last_vertex_stage(ctx);
1533 zink_create_cs_state(struct pipe_context *pctx,
1534 const struct pipe_compute_state *shader)
1536 struct nir_shader *nir;
1537 if (shader->ir_type != PIPE_SHADER_IR_NIR)
1538 nir = zink_tgsi_to_nir(pctx->screen, shader->prog);
1540 nir = (struct nir_shader *)shader->prog;
1542 if (nir->info.uses_bindless)
1543 zink_descriptors_init_bindless(zink_context(pctx));
1545 return create_compute_program(zink_context(pctx), nir);
1549 zink_bind_cs_state(struct pipe_context *pctx,
1552 struct zink_context *ctx = zink_context(pctx);
1553 struct zink_compute_program *comp = cso;
1554 if (comp && comp->nir->info.num_inlinable_uniforms)
1555 ctx->shader_has_inlinable_uniforms_mask |= 1 << MESA_SHADER_COMPUTE;
1557 ctx->shader_has_inlinable_uniforms_mask &= ~(1 << MESA_SHADER_COMPUTE);
1559 if (ctx->curr_compute) {
1560 zink_batch_reference_program(&ctx->batch, &ctx->curr_compute->base);
1561 ctx->compute_pipeline_state.final_hash ^= ctx->compute_pipeline_state.module_hash;
1562 ctx->compute_pipeline_state.module = VK_NULL_HANDLE;
1563 ctx->compute_pipeline_state.module_hash = 0;
1565 ctx->compute_pipeline_state.dirty = true;
1566 ctx->curr_compute = comp;
1567 if (comp && comp != ctx->curr_compute) {
1568 ctx->compute_pipeline_state.module_hash = ctx->curr_compute->curr->hash;
1569 if (util_queue_fence_is_signalled(&comp->base.cache_fence))
1570 ctx->compute_pipeline_state.module = ctx->curr_compute->curr->shader;
1571 ctx->compute_pipeline_state.final_hash ^= ctx->compute_pipeline_state.module_hash;
1572 if (ctx->compute_pipeline_state.key.base.nonseamless_cube_mask)
1573 ctx->compute_dirty = true;
1575 zink_select_launch_grid(ctx);
1579 zink_delete_cs_shader_state(struct pipe_context *pctx, void *cso)
1581 struct zink_compute_program *comp = cso;
1582 zink_compute_program_reference(zink_screen(pctx->screen), &comp, NULL);
1586 zink_delete_shader_state(struct pipe_context *pctx, void *cso)
1588 zink_shader_free(zink_screen(pctx->screen), cso);
1592 zink_create_gfx_shader_state(struct pipe_context *pctx, const struct pipe_shader_state *shader)
1595 if (shader->type != PIPE_SHADER_IR_NIR)
1596 nir = zink_tgsi_to_nir(pctx->screen, shader->tokens);
1598 nir = (struct nir_shader *)shader->ir.nir;
1600 if (nir->info.stage == MESA_SHADER_FRAGMENT && nir->info.fs.uses_fbfetch_output)
1601 zink_descriptor_util_init_fbfetch(zink_context(pctx));
1602 if (nir->info.uses_bindless)
1603 zink_descriptors_init_bindless(zink_context(pctx));
1605 return zink_shader_create(zink_screen(pctx->screen), nir, &shader->stream_output);
1609 zink_delete_cached_shader_state(struct pipe_context *pctx, void *cso)
1611 struct zink_screen *screen = zink_screen(pctx->screen);
1612 util_shader_reference(pctx, &screen->shaders, &cso, NULL);
1616 zink_create_cached_shader_state(struct pipe_context *pctx, const struct pipe_shader_state *shader)
1619 struct zink_screen *screen = zink_screen(pctx->screen);
1620 return util_live_shader_cache_get(pctx, &screen->shaders, shader, &cache_hit);
1623 struct zink_gfx_library_key *
1624 zink_create_pipeline_lib(struct zink_screen *screen, struct zink_gfx_program *prog, struct zink_gfx_pipeline_state *state)
1626 struct zink_gfx_library_key *gkey = rzalloc(prog, struct zink_gfx_library_key);
1627 gkey->optimal_key = state->optimal_key;
1628 assert(gkey->optimal_key);
1629 memcpy(gkey->modules, prog->modules, sizeof(gkey->modules));
1630 gkey->pipeline = zink_create_gfx_pipeline_library(screen, prog);
1631 _mesa_set_add(&prog->libs, gkey);
1636 print_exe_stages(VkShaderStageFlags stages)
1638 if (stages == VK_SHADER_STAGE_VERTEX_BIT)
1640 if (stages == (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT))
1642 if (stages == (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT))
1643 return "VS+TCS+TES";
1644 if (stages == (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT | VK_SHADER_STAGE_GEOMETRY_BIT))
1645 return "VS+TCS+TES+GS";
1646 if (stages == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT)
1648 if (stages == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)
1650 if (stages == VK_SHADER_STAGE_GEOMETRY_BIT)
1652 if (stages == VK_SHADER_STAGE_FRAGMENT_BIT)
1654 if (stages == VK_SHADER_STAGE_COMPUTE_BIT)
1656 unreachable("unhandled combination of stages!");
1660 print_pipeline_stats(struct zink_screen *screen, VkPipeline pipeline)
1662 VkPipelineInfoKHR pinfo = {
1663 VK_STRUCTURE_TYPE_PIPELINE_INFO_KHR,
1667 unsigned exe_count = 0;
1668 VkPipelineExecutablePropertiesKHR props[10] = {0};
1669 for (unsigned i = 0; i < ARRAY_SIZE(props); i++) {
1670 props[i].sType = VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_PROPERTIES_KHR;
1671 props[i].pNext = NULL;
1673 VKSCR(GetPipelineExecutablePropertiesKHR)(screen->dev, &pinfo, &exe_count, NULL);
1674 VKSCR(GetPipelineExecutablePropertiesKHR)(screen->dev, &pinfo, &exe_count, props);
1675 printf("PIPELINE STATISTICS:");
1676 for (unsigned e = 0; e < exe_count; e++) {
1677 VkPipelineExecutableInfoKHR info = {
1678 VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_INFO_KHR,
1684 printf("\n\t%s (%s): ", print_exe_stages(props[e].stages), props[e].name);
1685 VkPipelineExecutableStatisticKHR *stats = NULL;
1686 VKSCR(GetPipelineExecutableStatisticsKHR)(screen->dev, &info, &count, NULL);
1687 stats = calloc(count, sizeof(VkPipelineExecutableStatisticKHR));
1688 for (unsigned i = 0; i < count; i++)
1689 stats[i].sType = VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_STATISTIC_KHR;
1690 VKSCR(GetPipelineExecutableStatisticsKHR)(screen->dev, &info, &count, stats);
1692 for (unsigned i = 0; i < count; i++) {
1695 switch (stats[i].format) {
1696 case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_BOOL32_KHR:
1697 printf("%s: %u", stats[i].name, stats[i].value.b32);
1699 case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_INT64_KHR:
1700 printf("%s: %" PRIi64, stats[i].name, stats[i].value.i64);
1702 case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR:
1703 printf("%s: %" PRIu64, stats[i].name, stats[i].value.u64);
1705 case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_FLOAT64_KHR:
1706 printf("%s: %g", stats[i].name, stats[i].value.f64);
1709 unreachable("unknown statistic");
1717 precompile_job(void *data, void *gdata, int thread_index)
1719 struct zink_screen *screen = gdata;
1720 struct zink_gfx_program *prog = data;
1722 struct zink_gfx_pipeline_state state = {0};
1723 state.shader_keys_optimal.key.vs_base.last_vertex_stage = true;
1724 state.shader_keys_optimal.key.tcs.patch_vertices = 3; //random guess, generated tcs precompile is hard
1725 state.optimal_key = state.shader_keys_optimal.key.val;
1726 generate_gfx_program_modules_optimal(NULL, screen, prog, &state);
1727 zink_screen_get_pipeline_cache(screen, &prog->base, true);
1728 zink_create_pipeline_lib(screen, prog, &state);
1729 zink_screen_update_pipeline_cache(screen, &prog->base, true);
1733 zink_link_gfx_shader(struct pipe_context *pctx, void **shaders)
1735 struct zink_context *ctx = zink_context(pctx);
1736 struct zink_shader **zshaders = (struct zink_shader **)shaders;
1737 if (shaders[MESA_SHADER_COMPUTE])
1739 /* can't precompile fixedfunc */
1740 if (!shaders[MESA_SHADER_VERTEX] || !shaders[MESA_SHADER_FRAGMENT])
1743 unsigned shader_stages = 0;
1744 for (unsigned i = 0; i < ZINK_GFX_SHADER_COUNT; i++) {
1746 hash ^= zshaders[i]->hash;
1747 shader_stages |= BITFIELD_BIT(i);
1750 unsigned tess_stages = BITFIELD_BIT(MESA_SHADER_TESS_CTRL) | BITFIELD_BIT(MESA_SHADER_TESS_EVAL);
1751 unsigned tess = shader_stages & tess_stages;
1752 /* can't do fixedfunc tes either */
1753 if (tess && !shaders[MESA_SHADER_TESS_EVAL])
1755 struct hash_table *ht = &ctx->program_cache[zink_program_cache_stages(shader_stages)];
1756 simple_mtx_lock(&ctx->program_lock[zink_program_cache_stages(shader_stages)]);
1757 /* link can be called repeatedly with the same shaders: ignore */
1758 if (_mesa_hash_table_search_pre_hashed(ht, hash, shaders)) {
1759 simple_mtx_unlock(&ctx->program_lock[zink_program_cache_stages(shader_stages)]);
1762 struct zink_gfx_program *prog = zink_create_gfx_program(ctx, zshaders, 3);
1763 u_foreach_bit(i, shader_stages)
1764 assert(prog->shaders[i]);
1765 _mesa_hash_table_insert_pre_hashed(ht, hash, prog->shaders, prog);
1766 simple_mtx_unlock(&ctx->program_lock[zink_program_cache_stages(shader_stages)]);
1767 if (zink_debug & ZINK_DEBUG_SHADERDB) {
1768 struct zink_screen *screen = zink_screen(pctx->screen);
1769 if (screen->optimal_keys)
1770 generate_gfx_program_modules_optimal(ctx, screen, prog, &ctx->gfx_pipeline_state);
1772 generate_gfx_program_modules(ctx, screen, prog, &ctx->gfx_pipeline_state);
1773 VkPipeline pipeline = zink_create_gfx_pipeline(screen, prog, &ctx->gfx_pipeline_state, ctx->gfx_pipeline_state.element_state->binding_map, shaders[MESA_SHADER_TESS_EVAL] ? VK_PRIMITIVE_TOPOLOGY_PATCH_LIST : VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, true);
1774 print_pipeline_stats(screen, pipeline);
1776 util_queue_add_job(&zink_screen(pctx->screen)->cache_get_thread, prog, &prog->base.cache_fence, precompile_job, NULL, 0);
1781 zink_program_init(struct zink_context *ctx)
1783 ctx->base.create_vs_state = zink_create_cached_shader_state;
1784 ctx->base.bind_vs_state = zink_bind_vs_state;
1785 ctx->base.delete_vs_state = zink_delete_cached_shader_state;
1787 ctx->base.create_fs_state = zink_create_cached_shader_state;
1788 ctx->base.bind_fs_state = zink_bind_fs_state;
1789 ctx->base.delete_fs_state = zink_delete_cached_shader_state;
1791 ctx->base.create_gs_state = zink_create_cached_shader_state;
1792 ctx->base.bind_gs_state = zink_bind_gs_state;
1793 ctx->base.delete_gs_state = zink_delete_cached_shader_state;
1795 ctx->base.create_tcs_state = zink_create_cached_shader_state;
1796 ctx->base.bind_tcs_state = zink_bind_tcs_state;
1797 ctx->base.delete_tcs_state = zink_delete_cached_shader_state;
1799 ctx->base.create_tes_state = zink_create_cached_shader_state;
1800 ctx->base.bind_tes_state = zink_bind_tes_state;
1801 ctx->base.delete_tes_state = zink_delete_cached_shader_state;
1803 ctx->base.create_compute_state = zink_create_cs_state;
1804 ctx->base.bind_compute_state = zink_bind_cs_state;
1805 ctx->base.delete_compute_state = zink_delete_cs_shader_state;
1807 if (zink_screen(ctx->base.screen)->info.have_EXT_vertex_input_dynamic_state)
1808 _mesa_set_init(&ctx->gfx_inputs, ctx, hash_gfx_input_dynamic, equals_gfx_input_dynamic);
1810 _mesa_set_init(&ctx->gfx_inputs, ctx, hash_gfx_input, equals_gfx_input);
1811 if (zink_screen(ctx->base.screen)->have_full_ds3)
1812 _mesa_set_init(&ctx->gfx_outputs, ctx, hash_gfx_output_ds3, equals_gfx_output_ds3);
1814 _mesa_set_init(&ctx->gfx_outputs, ctx, hash_gfx_output, equals_gfx_output);
1815 /* validate struct packing */
1816 STATIC_ASSERT(offsetof(struct zink_gfx_output_key, sample_mask) == sizeof(uint32_t));
1817 STATIC_ASSERT(offsetof(struct zink_gfx_pipeline_state, vertex_buffers_enabled_mask) - offsetof(struct zink_gfx_pipeline_state, input) ==
1818 offsetof(struct zink_gfx_input_key, vertex_buffers_enabled_mask) - offsetof(struct zink_gfx_input_key, input));
1819 STATIC_ASSERT(offsetof(struct zink_gfx_pipeline_state, vertex_strides) - offsetof(struct zink_gfx_pipeline_state, input) ==
1820 offsetof(struct zink_gfx_input_key, vertex_strides) - offsetof(struct zink_gfx_input_key, input));
1821 STATIC_ASSERT(offsetof(struct zink_gfx_pipeline_state, element_state) - offsetof(struct zink_gfx_pipeline_state, input) ==
1822 offsetof(struct zink_gfx_input_key, element_state) - offsetof(struct zink_gfx_input_key, input));
1824 STATIC_ASSERT(sizeof(union zink_shader_key_optimal) == sizeof(uint32_t));
1826 if (zink_screen(ctx->base.screen)->info.have_EXT_graphics_pipeline_library || zink_debug & ZINK_DEBUG_SHADERDB)
1827 ctx->base.link_shader = zink_link_gfx_shader;
1831 zink_set_rasterizer_discard(struct zink_context *ctx, bool disable)
1833 bool value = disable ? false : (ctx->rast_state ? ctx->rast_state->base.rasterizer_discard : false);
1834 bool changed = ctx->gfx_pipeline_state.dyn_state2.rasterizer_discard != value;
1835 ctx->gfx_pipeline_state.dyn_state2.rasterizer_discard = value;
1838 if (!zink_screen(ctx->base.screen)->info.have_EXT_extended_dynamic_state2)
1839 ctx->gfx_pipeline_state.dirty |= true;
1840 ctx->rasterizer_discard_changed = true;
1845 zink_driver_thread_add_job(struct pipe_screen *pscreen, void *data,
1846 struct util_queue_fence *fence,
1847 pipe_driver_thread_func execute,
1848 pipe_driver_thread_func cleanup,
1849 const size_t job_size)
1851 struct zink_screen *screen = zink_screen(pscreen);
1852 util_queue_add_job(&screen->cache_get_thread, data, fence, execute, cleanup, job_size);