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"
41 #include "nir/nir_draw_helpers.h"
43 /* for pipeline cache */
44 #define XXH_INLINE_ALL
45 #include "util/xxhash.h"
48 precompile_job(void *data, void *gdata, int thread_index);
49 struct zink_gfx_program *
50 create_gfx_program_separable(struct zink_context *ctx, struct zink_shader **stages, unsigned vertices_per_patch);
53 debug_describe_zink_gfx_program(char *buf, const struct zink_gfx_program *ptr)
55 sprintf(buf, "zink_gfx_program");
59 debug_describe_zink_compute_program(char *buf, const struct zink_compute_program *ptr)
61 sprintf(buf, "zink_compute_program");
64 ALWAYS_INLINE static bool
65 shader_key_matches_tcs_nongenerated(const struct zink_shader_module *zm, const struct zink_shader_key *key, unsigned num_uniforms)
67 if (zm->num_uniforms != num_uniforms || zm->has_nonseamless != !!key->base.nonseamless_cube_mask ||
68 zm->needs_zs_shader_swizzle != key->base.needs_zs_shader_swizzle)
70 const uint32_t nonseamless_size = zm->has_nonseamless ? sizeof(uint32_t) : 0;
71 return (!nonseamless_size || !memcmp(zm->key + zm->key_size, &key->base.nonseamless_cube_mask, nonseamless_size)) &&
72 (!num_uniforms || !memcmp(zm->key + zm->key_size + nonseamless_size,
73 key->base.inlined_uniform_values, zm->num_uniforms * sizeof(uint32_t)));
76 ALWAYS_INLINE static bool
77 shader_key_matches(const struct zink_shader_module *zm,
78 const struct zink_shader_key *key, unsigned num_uniforms,
79 bool has_inline, bool has_nonseamless)
81 const uint32_t nonseamless_size = !has_nonseamless && zm->has_nonseamless ? sizeof(uint32_t) : 0;
83 if (zm->num_uniforms != num_uniforms ||
85 memcmp(zm->key + zm->key_size + nonseamless_size,
86 key->base.inlined_uniform_values, zm->num_uniforms * sizeof(uint32_t))))
89 if (!has_nonseamless) {
90 if (zm->has_nonseamless != !!key->base.nonseamless_cube_mask ||
91 (nonseamless_size && memcmp(zm->key + zm->key_size, &key->base.nonseamless_cube_mask, nonseamless_size)))
94 if (zm->needs_zs_shader_swizzle != key->base.needs_zs_shader_swizzle)
96 return !memcmp(zm->key, key, zm->key_size);
100 shader_module_hash(const struct zink_shader_module *zm)
102 const uint32_t nonseamless_size = zm->has_nonseamless ? sizeof(uint32_t) : 0;
103 unsigned key_size = zm->key_size + nonseamless_size + zm->num_uniforms * sizeof(uint32_t);
104 return _mesa_hash_data(zm->key, key_size);
107 ALWAYS_INLINE static void
108 gather_shader_module_info(struct zink_context *ctx, struct zink_screen *screen,
109 struct zink_shader *zs, struct zink_gfx_program *prog,
110 struct zink_gfx_pipeline_state *state,
111 bool has_inline, //is inlining enabled?
112 bool has_nonseamless, //is nonseamless ext present?
113 unsigned *inline_size, unsigned *nonseamless_size)
115 gl_shader_stage stage = zs->nir->info.stage;
116 struct zink_shader_key *key = &state->shader_keys.key[stage];
117 if (has_inline && ctx && zs->nir->info.num_inlinable_uniforms &&
118 ctx->inlinable_uniforms_valid_mask & BITFIELD64_BIT(stage)) {
119 if (zs->can_inline && (screen->is_cpu || prog->inlined_variant_count[stage] < ZINK_MAX_INLINED_VARIANTS))
120 *inline_size = zs->nir->info.num_inlinable_uniforms;
122 key->inline_uniforms = false;
124 if (!has_nonseamless && key->base.nonseamless_cube_mask)
125 *nonseamless_size = sizeof(uint32_t);
128 ALWAYS_INLINE static struct zink_shader_module *
129 create_shader_module_for_stage(struct zink_context *ctx, struct zink_screen *screen,
130 struct zink_shader *zs, struct zink_gfx_program *prog,
131 gl_shader_stage stage,
132 struct zink_gfx_pipeline_state *state,
133 unsigned inline_size, unsigned nonseamless_size,
134 bool has_inline, //is inlining enabled?
135 bool has_nonseamless) //is nonseamless ext present?
138 struct zink_shader_module *zm;
139 const struct zink_shader_key *key = &state->shader_keys.key[stage];
140 /* non-generated tcs won't use the shader key */
141 const bool is_nongenerated_tcs = stage == MESA_SHADER_TESS_CTRL && !zs->non_fs.is_generated;
142 const bool shadow_needs_shader_swizzle = key->base.needs_zs_shader_swizzle ||
143 (stage == MESA_SHADER_FRAGMENT && key->key.fs.base.shadow_needs_shader_swizzle);
144 zm = malloc(sizeof(struct zink_shader_module) + key->size +
145 (!has_nonseamless ? nonseamless_size : 0) + inline_size * sizeof(uint32_t) +
146 (shadow_needs_shader_swizzle ? sizeof(struct zink_zs_swizzle_key) : 0));
150 unsigned patch_vertices = state->shader_keys.key[MESA_SHADER_TESS_CTRL ].key.tcs.patch_vertices;
151 if (stage == MESA_SHADER_TESS_CTRL && zs->non_fs.is_generated && zs->spirv) {
152 assert(ctx); //TODO async
153 mod = zink_shader_tcs_compile(screen, zs, patch_vertices);
155 mod = zink_shader_compile(screen, zs, prog->nir[stage], key, &ctx->di.zs_swizzle[stage]);
162 zm->num_uniforms = inline_size;
163 if (!is_nongenerated_tcs) {
164 zm->key_size = key->size;
165 memcpy(zm->key, key, key->size);
168 memset(zm->key, 0, key->size);
170 if (!has_nonseamless && nonseamless_size) {
171 /* nonseamless mask gets added to base key if it exists */
172 memcpy(zm->key + key->size, &key->base.nonseamless_cube_mask, nonseamless_size);
174 zm->needs_zs_shader_swizzle = shadow_needs_shader_swizzle;
175 zm->has_nonseamless = has_nonseamless ? 0 : !!nonseamless_size;
177 memcpy(zm->key + key->size + nonseamless_size, key->base.inlined_uniform_values, inline_size * sizeof(uint32_t));
178 if (stage == MESA_SHADER_TESS_CTRL && zs->non_fs.is_generated)
179 zm->hash = patch_vertices;
181 zm->hash = shader_module_hash(zm);
182 if (unlikely(shadow_needs_shader_swizzle)) {
183 memcpy(zm->key + key->size + nonseamless_size + inline_size * sizeof(uint32_t), &ctx->di.zs_swizzle[stage], sizeof(struct zink_zs_swizzle_key));
184 zm->hash ^= _mesa_hash_data(&ctx->di.zs_swizzle[stage], sizeof(struct zink_zs_swizzle_key));
186 zm->default_variant = !shadow_needs_shader_swizzle && !inline_size && !util_dynarray_contains(&prog->shader_cache[stage][0][0], void*);
188 prog->inlined_variant_count[stage]++;
189 util_dynarray_append(&prog->shader_cache[stage][has_nonseamless ? 0 : !!nonseamless_size][!!inline_size], void*, zm);
193 ALWAYS_INLINE static struct zink_shader_module *
194 get_shader_module_for_stage(struct zink_context *ctx, struct zink_screen *screen,
195 struct zink_shader *zs, struct zink_gfx_program *prog,
196 gl_shader_stage stage,
197 struct zink_gfx_pipeline_state *state,
198 unsigned inline_size, unsigned nonseamless_size,
199 bool has_inline, //is inlining enabled?
200 bool has_nonseamless) //is nonseamless ext present?
202 const struct zink_shader_key *key = &state->shader_keys.key[stage];
203 /* non-generated tcs won't use the shader key */
204 const bool is_nongenerated_tcs = stage == MESA_SHADER_TESS_CTRL && !zs->non_fs.is_generated;
205 const bool shadow_needs_shader_swizzle = unlikely(key->base.needs_zs_shader_swizzle) ||
206 (stage == MESA_SHADER_FRAGMENT && unlikely(key->key.fs.base.shadow_needs_shader_swizzle));
208 struct util_dynarray *shader_cache = &prog->shader_cache[stage][!has_nonseamless ? !!nonseamless_size : 0][has_inline ? !!inline_size : 0];
209 unsigned count = util_dynarray_num_elements(shader_cache, struct zink_shader_module *);
210 struct zink_shader_module **pzm = shader_cache->data;
211 for (unsigned i = 0; i < count; i++) {
212 struct zink_shader_module *iter = pzm[i];
213 if (is_nongenerated_tcs) {
214 if (!shader_key_matches_tcs_nongenerated(iter, key, has_inline ? !!inline_size : 0))
217 if (stage == MESA_SHADER_VERTEX && iter->key_size != key->size)
219 if (!shader_key_matches(iter, key, inline_size, has_inline, has_nonseamless))
221 if (unlikely(shadow_needs_shader_swizzle)) {
222 /* shadow swizzle data needs a manual compare since it's so fat */
223 if (memcmp(iter->key + iter->key_size + nonseamless_size + iter->num_uniforms * sizeof(uint32_t),
224 &ctx->di.zs_swizzle[stage], sizeof(struct zink_zs_swizzle_key)))
229 struct zink_shader_module *zero = pzm[0];
239 ALWAYS_INLINE static struct zink_shader_module *
240 create_shader_module_for_stage_optimal(struct zink_context *ctx, struct zink_screen *screen,
241 struct zink_shader *zs, struct zink_gfx_program *prog,
242 gl_shader_stage stage,
243 struct zink_gfx_pipeline_state *state)
246 struct zink_shader_module *zm;
248 unsigned mask = stage == MESA_SHADER_FRAGMENT ? BITFIELD_MASK(16) : BITFIELD_MASK(8);
249 bool shadow_needs_shader_swizzle = false;
250 if (zs == prog->last_vertex_stage) {
251 key = (uint16_t*)&state->shader_keys_optimal.key.vs_base;
252 } else if (stage == MESA_SHADER_FRAGMENT) {
253 key = (uint16_t*)&state->shader_keys_optimal.key.fs;
254 shadow_needs_shader_swizzle = ctx ? ctx->gfx_pipeline_state.shader_keys_optimal.key.fs.shadow_needs_shader_swizzle : false;
255 } else if (stage == MESA_SHADER_TESS_CTRL && zs->non_fs.is_generated) {
256 key = (uint16_t*)&state->shader_keys_optimal.key.tcs;
260 size_t key_size = sizeof(uint16_t);
261 zm = calloc(1, sizeof(struct zink_shader_module) + (key ? key_size : 0) + (unlikely(shadow_needs_shader_swizzle) ? sizeof(struct zink_zs_swizzle_key) : 0));
265 if (stage == MESA_SHADER_TESS_CTRL && zs->non_fs.is_generated && zs->spirv) {
266 assert(ctx); //TODO async
267 struct zink_tcs_key *tcs = (struct zink_tcs_key*)key;
268 mod = zink_shader_tcs_compile(screen, zs, tcs->patch_vertices);
270 mod = zink_shader_compile(screen, zs, prog->nir[stage], (struct zink_shader_key*)key, shadow_needs_shader_swizzle ? &ctx->di.zs_swizzle[stage] : NULL);
277 /* non-generated tcs won't use the shader key */
278 const bool is_nongenerated_tcs = stage == MESA_SHADER_TESS_CTRL && !zs->non_fs.is_generated;
279 if (key && !is_nongenerated_tcs) {
280 zm->key_size = key_size;
281 uint16_t *data = (uint16_t*)zm->key;
282 /* sanitize actual key bits */
283 *data = (*key) & mask;
284 if (unlikely(shadow_needs_shader_swizzle))
285 memcpy(&data[1], &ctx->di.zs_swizzle[stage], sizeof(struct zink_zs_swizzle_key));
287 zm->default_variant = !util_dynarray_contains(&prog->shader_cache[stage][0][0], void*);
288 util_dynarray_append(&prog->shader_cache[stage][0][0], void*, zm);
292 ALWAYS_INLINE static struct zink_shader_module *
293 get_shader_module_for_stage_optimal(struct zink_context *ctx, struct zink_screen *screen,
294 struct zink_shader *zs, struct zink_gfx_program *prog,
295 gl_shader_stage stage,
296 struct zink_gfx_pipeline_state *state)
298 /* non-generated tcs won't use the shader key */
299 const bool is_nongenerated_tcs = stage == MESA_SHADER_TESS_CTRL && !zs->non_fs.is_generated;
300 bool shadow_needs_shader_swizzle = false;
302 unsigned mask = stage == MESA_SHADER_FRAGMENT ? BITFIELD_MASK(16) : BITFIELD_MASK(8);
303 if (zs == prog->last_vertex_stage) {
304 key = (uint16_t*)&ctx->gfx_pipeline_state.shader_keys_optimal.key.vs_base;
305 } else if (stage == MESA_SHADER_FRAGMENT) {
306 key = (uint16_t*)&ctx->gfx_pipeline_state.shader_keys_optimal.key.fs;
307 shadow_needs_shader_swizzle = ctx->gfx_pipeline_state.shader_keys_optimal.key.fs.shadow_needs_shader_swizzle;
308 } else if (stage == MESA_SHADER_TESS_CTRL && zs->non_fs.is_generated) {
309 key = (uint16_t*)&ctx->gfx_pipeline_state.shader_keys_optimal.key.tcs;
313 struct util_dynarray *shader_cache = &prog->shader_cache[stage][0][0];
314 unsigned count = util_dynarray_num_elements(shader_cache, struct zink_shader_module *);
315 struct zink_shader_module **pzm = shader_cache->data;
316 for (unsigned i = 0; i < count; i++) {
317 struct zink_shader_module *iter = pzm[i];
318 if (is_nongenerated_tcs) {
321 uint16_t val = (*key) & mask;
322 /* no key is bigger than uint16_t */
323 if (memcmp(iter->key, &val, sizeof(uint16_t)))
325 if (unlikely(shadow_needs_shader_swizzle)) {
326 /* shadow swizzle data needs a manual compare since it's so fat */
327 if (memcmp(iter->key + sizeof(uint16_t), &ctx->di.zs_swizzle[stage], sizeof(struct zink_zs_swizzle_key)))
332 struct zink_shader_module *zero = pzm[0];
343 zink_destroy_shader_module(struct zink_screen *screen, struct zink_shader_module *zm)
345 VKSCR(DestroyShaderModule)(screen->dev, zm->shader, NULL);
350 destroy_shader_cache(struct zink_screen *screen, struct util_dynarray *sc)
352 while (util_dynarray_contains(sc, void*)) {
353 struct zink_shader_module *zm = util_dynarray_pop(sc, struct zink_shader_module*);
354 zink_destroy_shader_module(screen, zm);
358 ALWAYS_INLINE static void
359 update_gfx_shader_modules(struct zink_context *ctx,
360 struct zink_screen *screen,
361 struct zink_gfx_program *prog, uint32_t mask,
362 struct zink_gfx_pipeline_state *state,
363 bool has_inline, //is inlining enabled?
364 bool has_nonseamless) //is nonseamless ext present?
366 bool hash_changed = false;
367 bool default_variants = true;
368 assert(prog->modules[MESA_SHADER_VERTEX]);
369 uint32_t variant_hash = prog->last_variant_hash;
370 prog->has_edgeflags = prog->shaders[MESA_SHADER_VERTEX]->has_edgeflags;
371 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
372 if (!(mask & BITFIELD_BIT(i)))
375 assert(prog->shaders[i]);
377 unsigned inline_size = 0, nonseamless_size = 0;
378 gather_shader_module_info(ctx, screen, prog->shaders[i], prog, state, has_inline, has_nonseamless, &inline_size, &nonseamless_size);
379 struct zink_shader_module *zm = get_shader_module_for_stage(ctx, screen, prog->shaders[i], prog, i, state,
380 inline_size, nonseamless_size, has_inline, has_nonseamless);
382 zm = create_shader_module_for_stage(ctx, screen, prog->shaders[i], prog, i, state,
383 inline_size, nonseamless_size, has_inline, has_nonseamless);
384 state->modules[i] = zm->shader;
385 if (prog->modules[i] == zm->shader)
387 variant_hash ^= prog->module_hash[i];
389 default_variants &= zm->default_variant;
390 prog->modules[i] = zm->shader;
391 prog->module_hash[i] = zm->hash;
393 if (zm->num_uniforms)
394 prog->inline_variants |= BITFIELD_BIT(i);
396 prog->inline_variants &= ~BITFIELD_BIT(i);
398 variant_hash ^= prog->module_hash[i];
401 if (hash_changed && state) {
402 if (default_variants)
403 prog->last_variant_hash = prog->default_variant_hash;
405 prog->last_variant_hash = variant_hash;
407 state->modules_changed = true;
412 generate_gfx_program_modules(struct zink_context *ctx, struct zink_screen *screen, struct zink_gfx_program *prog, struct zink_gfx_pipeline_state *state)
414 assert(!prog->modules[MESA_SHADER_VERTEX]);
415 uint32_t variant_hash = 0;
416 bool default_variants = true;
417 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
418 if (!(prog->stages_present & BITFIELD_BIT(i)))
421 assert(prog->shaders[i]);
423 unsigned inline_size = 0, nonseamless_size = 0;
424 gather_shader_module_info(ctx, screen, prog->shaders[i], prog, state,
425 screen->driconf.inline_uniforms, screen->info.have_EXT_non_seamless_cube_map,
426 &inline_size, &nonseamless_size);
427 struct zink_shader_module *zm = create_shader_module_for_stage(ctx, screen, prog->shaders[i], prog, i, state,
428 inline_size, nonseamless_size,
429 screen->driconf.inline_uniforms, screen->info.have_EXT_non_seamless_cube_map);
430 state->modules[i] = zm->shader;
431 prog->modules[i] = zm->shader;
432 prog->module_hash[i] = zm->hash;
433 if (zm->num_uniforms)
434 prog->inline_variants |= BITFIELD_BIT(i);
435 default_variants &= zm->default_variant;
436 variant_hash ^= prog->module_hash[i];
439 p_atomic_dec(&prog->base.reference.count);
440 state->modules_changed = true;
442 prog->last_variant_hash = variant_hash;
443 if (default_variants)
444 prog->default_variant_hash = prog->last_variant_hash;
448 generate_gfx_program_modules_optimal(struct zink_context *ctx, struct zink_screen *screen, struct zink_gfx_program *prog, struct zink_gfx_pipeline_state *state)
450 assert(!prog->modules[MESA_SHADER_VERTEX]);
451 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
452 if (!(prog->stages_present & BITFIELD_BIT(i)))
455 assert(prog->shaders[i]);
457 struct zink_shader_module *zm = create_shader_module_for_stage_optimal(ctx, screen, prog->shaders[i], prog, i, state);
458 prog->modules[i] = zm->shader;
461 p_atomic_dec(&prog->base.reference.count);
462 state->modules_changed = true;
463 prog->last_variant_hash = state->shader_keys_optimal.key.val;
467 hash_pipeline_lib_generated_tcs(const void *key)
469 const struct zink_gfx_library_key *gkey = key;
470 return gkey->optimal_key;
475 equals_pipeline_lib_generated_tcs(const void *a, const void *b)
477 return !memcmp(a, b, sizeof(uint32_t));
481 hash_pipeline_lib(const void *key)
483 const struct zink_gfx_library_key *gkey = key;
484 /* remove generated tcs bits */
485 return zink_shader_key_optimal_no_tcs(gkey->optimal_key);
489 equals_pipeline_lib(const void *a, const void *b)
491 const struct zink_gfx_library_key *ak = a;
492 const struct zink_gfx_library_key *bk = b;
493 /* remove generated tcs bits */
494 uint32_t val_a = zink_shader_key_optimal_no_tcs(ak->optimal_key);
495 uint32_t val_b = zink_shader_key_optimal_no_tcs(bk->optimal_key);
496 return val_a == val_b;
500 hash_gfx_input_dynamic(const void *key)
502 const struct zink_gfx_input_key *ikey = key;
507 equals_gfx_input_dynamic(const void *a, const void *b)
509 const struct zink_gfx_input_key *ikey_a = a;
510 const struct zink_gfx_input_key *ikey_b = b;
511 return ikey_a->idx == ikey_b->idx;
515 hash_gfx_input(const void *key)
517 const struct zink_gfx_input_key *ikey = key;
518 if (ikey->uses_dynamic_stride)
520 return _mesa_hash_data(key, offsetof(struct zink_gfx_input_key, pipeline));
524 equals_gfx_input(const void *a, const void *b)
526 const struct zink_gfx_input_key *ikey_a = a;
527 const struct zink_gfx_input_key *ikey_b = b;
528 if (ikey_a->uses_dynamic_stride)
529 return ikey_a->element_state == ikey_b->element_state &&
530 !memcmp(a, b, offsetof(struct zink_gfx_input_key, vertex_buffers_enabled_mask));
531 return !memcmp(a, b, offsetof(struct zink_gfx_input_key, pipeline));
535 hash_gfx_output_ds3(const void *key)
537 const uint8_t *data = key;
538 return _mesa_hash_data(data, sizeof(uint32_t));
542 equals_gfx_output_ds3(const void *a, const void *b)
544 const uint8_t *da = a;
545 const uint8_t *db = b;
546 return !memcmp(da, db, sizeof(uint32_t));
550 hash_gfx_output(const void *key)
552 const uint8_t *data = key;
553 return _mesa_hash_data(data, offsetof(struct zink_gfx_output_key, pipeline));
557 equals_gfx_output(const void *a, const void *b)
559 const uint8_t *da = a;
560 const uint8_t *db = b;
561 return !memcmp(da, db, offsetof(struct zink_gfx_output_key, pipeline));
564 ALWAYS_INLINE static void
565 update_gfx_program_nonseamless(struct zink_context *ctx, struct zink_gfx_program *prog, bool has_nonseamless)
567 struct zink_screen *screen = zink_screen(ctx->base.screen);
568 if (screen->driconf.inline_uniforms)
569 update_gfx_shader_modules(ctx, screen, prog,
570 ctx->dirty_gfx_stages & prog->stages_present, &ctx->gfx_pipeline_state,
571 true, has_nonseamless);
573 update_gfx_shader_modules(ctx, screen, prog,
574 ctx->dirty_gfx_stages & prog->stages_present, &ctx->gfx_pipeline_state,
575 false, has_nonseamless);
579 update_gfx_program(struct zink_context *ctx, struct zink_gfx_program *prog)
581 struct zink_screen *screen = zink_screen(ctx->base.screen);
582 if (screen->info.have_EXT_non_seamless_cube_map)
583 update_gfx_program_nonseamless(ctx, prog, true);
585 update_gfx_program_nonseamless(ctx, prog, false);
589 zink_gfx_program_update(struct zink_context *ctx)
591 if (ctx->last_vertex_stage_dirty) {
592 gl_shader_stage pstage = ctx->last_vertex_stage->nir->info.stage;
593 ctx->dirty_gfx_stages |= BITFIELD_BIT(pstage);
594 memcpy(&ctx->gfx_pipeline_state.shader_keys.key[pstage].key.vs_base,
595 &ctx->gfx_pipeline_state.shader_keys.last_vertex.key.vs_base,
596 sizeof(struct zink_vs_key_base));
597 ctx->last_vertex_stage_dirty = false;
599 if (ctx->gfx_dirty) {
600 struct zink_gfx_program *prog = NULL;
602 simple_mtx_lock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
603 struct hash_table *ht = &ctx->program_cache[zink_program_cache_stages(ctx->shader_stages)];
604 const uint32_t hash = ctx->gfx_hash;
605 struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(ht, hash, ctx->gfx_stages);
606 /* this must be done before prog is updated */
607 if (ctx->curr_program)
608 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
610 prog = (struct zink_gfx_program*)entry->data;
611 for (unsigned i = 0; i < ZINK_GFX_SHADER_COUNT; i++) {
612 if (prog->stages_present & ~ctx->dirty_gfx_stages & BITFIELD_BIT(i))
613 ctx->gfx_pipeline_state.modules[i] = prog->modules[i];
615 /* ensure variants are always updated if keys have changed since last use */
616 ctx->dirty_gfx_stages |= prog->stages_present;
617 update_gfx_program(ctx, prog);
619 ctx->dirty_gfx_stages |= ctx->shader_stages;
620 prog = zink_create_gfx_program(ctx, ctx->gfx_stages, ctx->gfx_pipeline_state.dyn_state2.vertices_per_patch, hash);
621 zink_screen_get_pipeline_cache(zink_screen(ctx->base.screen), &prog->base, false);
622 _mesa_hash_table_insert_pre_hashed(ht, hash, prog->shaders, prog);
623 prog->base.removed = false;
624 generate_gfx_program_modules(ctx, zink_screen(ctx->base.screen), prog, &ctx->gfx_pipeline_state);
626 simple_mtx_unlock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
627 if (prog && prog != ctx->curr_program)
628 zink_batch_reference_program(&ctx->batch, &prog->base);
629 ctx->curr_program = prog;
630 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
631 ctx->gfx_dirty = false;
632 } else if (ctx->dirty_gfx_stages) {
633 /* remove old hash */
634 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
635 update_gfx_program(ctx, ctx->curr_program);
637 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
639 ctx->dirty_gfx_stages = 0;
642 ALWAYS_INLINE static bool
643 update_gfx_shader_module_optimal(struct zink_context *ctx, struct zink_gfx_program *prog, gl_shader_stage pstage)
645 struct zink_screen *screen = zink_screen(ctx->base.screen);
646 if (screen->info.have_EXT_graphics_pipeline_library)
647 util_queue_fence_wait(&prog->base.cache_fence);
648 struct zink_shader_module *zm = get_shader_module_for_stage_optimal(ctx, screen, prog->shaders[pstage], prog, pstage, &ctx->gfx_pipeline_state);
650 zm = create_shader_module_for_stage_optimal(ctx, screen, prog->shaders[pstage], prog, pstage, &ctx->gfx_pipeline_state);
652 bool changed = prog->modules[pstage] != zm->shader;
653 prog->modules[pstage] = zm->shader;
658 update_gfx_program_optimal(struct zink_context *ctx, struct zink_gfx_program *prog)
660 const union zink_shader_key_optimal *optimal_key = (union zink_shader_key_optimal*)&prog->last_variant_hash;
661 if (ctx->gfx_pipeline_state.shader_keys_optimal.key.vs_bits != optimal_key->vs_bits) {
662 assert(!prog->is_separable);
663 bool changed = update_gfx_shader_module_optimal(ctx, prog, ctx->last_vertex_stage->nir->info.stage);
664 ctx->gfx_pipeline_state.modules_changed |= changed;
666 const bool shadow_needs_shader_swizzle = optimal_key->fs.shadow_needs_shader_swizzle && (ctx->dirty_gfx_stages & BITFIELD_BIT(MESA_SHADER_FRAGMENT));
667 if (ctx->gfx_pipeline_state.shader_keys_optimal.key.fs_bits != optimal_key->fs_bits ||
668 /* always recheck shadow swizzles since they aren't directly part of the key */
669 unlikely(shadow_needs_shader_swizzle)) {
670 assert(!prog->is_separable);
671 bool changed = update_gfx_shader_module_optimal(ctx, prog, MESA_SHADER_FRAGMENT);
672 ctx->gfx_pipeline_state.modules_changed |= changed;
673 if (unlikely(shadow_needs_shader_swizzle)) {
674 struct zink_shader_module **pzm = prog->shader_cache[MESA_SHADER_FRAGMENT][0][0].data;
675 ctx->gfx_pipeline_state.shadow = (struct zink_zs_swizzle_key*)pzm[0]->key + sizeof(uint16_t);
678 if (prog->shaders[MESA_SHADER_TESS_CTRL] && prog->shaders[MESA_SHADER_TESS_CTRL]->non_fs.is_generated &&
679 ctx->gfx_pipeline_state.shader_keys_optimal.key.tcs_bits != optimal_key->tcs_bits) {
680 assert(!prog->is_separable);
681 bool changed = update_gfx_shader_module_optimal(ctx, prog, MESA_SHADER_TESS_CTRL);
682 ctx->gfx_pipeline_state.modules_changed |= changed;
684 prog->last_variant_hash = ctx->gfx_pipeline_state.shader_keys_optimal.key.val;
688 zink_gfx_program_update_optimal(struct zink_context *ctx)
690 if (ctx->gfx_dirty) {
691 struct zink_gfx_program *prog = NULL;
692 ctx->gfx_pipeline_state.optimal_key = ctx->gfx_pipeline_state.shader_keys_optimal.key.val;
693 struct hash_table *ht = &ctx->program_cache[zink_program_cache_stages(ctx->shader_stages)];
694 const uint32_t hash = ctx->gfx_hash;
695 simple_mtx_lock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
696 struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(ht, hash, ctx->gfx_stages);
698 if (ctx->curr_program)
699 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
701 prog = (struct zink_gfx_program*)entry->data;
702 if (prog->is_separable) {
703 /* shader variants can't be handled by separable programs: sync and compile */
704 if (!ZINK_SHADER_KEY_OPTIMAL_IS_DEFAULT(ctx->gfx_pipeline_state.optimal_key))
705 util_queue_fence_wait(&prog->base.cache_fence);
706 /* If the optimized linked pipeline is done compiling, swap it into place. */
707 if (util_queue_fence_is_signalled(&prog->base.cache_fence)) {
708 struct zink_gfx_program *real = prog->full_prog;
710 entry->key = real->shaders;
711 real->base.removed = false;
712 prog->full_prog = NULL;
713 prog->base.removed = true;
714 zink_gfx_program_reference(zink_screen(ctx->base.screen), &prog, NULL);
718 update_gfx_program_optimal(ctx, prog);
720 ctx->dirty_gfx_stages |= ctx->shader_stages;
721 prog = create_gfx_program_separable(ctx, ctx->gfx_stages, ctx->gfx_pipeline_state.dyn_state2.vertices_per_patch);
722 prog->base.removed = false;
723 _mesa_hash_table_insert_pre_hashed(ht, hash, prog->shaders, prog);
724 if (!prog->is_separable) {
725 zink_screen_get_pipeline_cache(zink_screen(ctx->base.screen), &prog->base, false);
726 generate_gfx_program_modules_optimal(ctx, zink_screen(ctx->base.screen), prog, &ctx->gfx_pipeline_state);
729 simple_mtx_unlock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
730 if (prog && prog != ctx->curr_program)
731 zink_batch_reference_program(&ctx->batch, &prog->base);
732 ctx->curr_program = prog;
733 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
734 } else if (ctx->dirty_gfx_stages) {
735 /* remove old hash */
736 ctx->gfx_pipeline_state.optimal_key = ctx->gfx_pipeline_state.shader_keys_optimal.key.val;
737 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
738 if (ctx->curr_program->is_separable) {
739 struct zink_gfx_program *prog = ctx->curr_program;
740 if (prog->is_separable && !ZINK_SHADER_KEY_OPTIMAL_IS_DEFAULT(ctx->gfx_pipeline_state.optimal_key)) {
741 util_queue_fence_wait(&prog->base.cache_fence);
742 /* shader variants can't be handled by separable programs: sync and compile */
743 struct hash_table *ht = &ctx->program_cache[zink_program_cache_stages(ctx->shader_stages)];
744 const uint32_t hash = ctx->gfx_hash;
745 simple_mtx_lock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
746 struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(ht, hash, ctx->gfx_stages);
747 struct zink_gfx_program *real = prog->full_prog;
749 entry->key = real->shaders;
750 real->base.removed = false;
751 prog->full_prog = NULL;
752 prog->base.removed = true;
753 zink_gfx_program_reference(zink_screen(ctx->base.screen), &prog, NULL);
754 ctx->curr_program = real;
755 simple_mtx_unlock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
758 update_gfx_program_optimal(ctx, ctx->curr_program);
760 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
762 ctx->dirty_gfx_stages = 0;
763 ctx->gfx_dirty = false;
764 ctx->last_vertex_stage_dirty = false;
768 optimized_compile_job(void *data, void *gdata, int thread_index)
770 struct zink_gfx_pipeline_cache_entry *pc_entry = data;
771 struct zink_screen *screen = gdata;
774 pipeline = zink_create_gfx_pipeline_combined(screen, pc_entry->prog, pc_entry->ikey->pipeline, &pc_entry->gkey->pipeline, 1, pc_entry->okey->pipeline, true);
776 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);
778 pc_entry->unoptimized_pipeline = pc_entry->pipeline;
779 pc_entry->pipeline = pipeline;
784 zink_gfx_program_compile_queue(struct zink_context *ctx, struct zink_gfx_pipeline_cache_entry *pc_entry)
786 util_queue_add_job(&zink_screen(ctx->base.screen)->cache_get_thread, pc_entry, &pc_entry->fence, optimized_compile_job, NULL, 0);
790 update_cs_shader_module(struct zink_context *ctx, struct zink_compute_program *comp)
792 struct zink_screen *screen = zink_screen(ctx->base.screen);
793 struct zink_shader *zs = comp->shader;
795 struct zink_shader_module *zm = NULL;
796 unsigned inline_size = 0, nonseamless_size = 0, zs_swizzle_size = 0;
797 struct zink_shader_key *key = &ctx->compute_pipeline_state.key;
798 ASSERTED bool check_robustness = screen->driver_workarounds.lower_robustImageAccess2 && (ctx->flags & PIPE_CONTEXT_ROBUST_BUFFER_ACCESS);
799 assert(zink_cs_key(key)->robust_access == check_robustness);
801 if (ctx && zs->nir->info.num_inlinable_uniforms &&
802 ctx->inlinable_uniforms_valid_mask & BITFIELD64_BIT(MESA_SHADER_COMPUTE)) {
803 if (screen->is_cpu || comp->inlined_variant_count < ZINK_MAX_INLINED_VARIANTS)
804 inline_size = zs->nir->info.num_inlinable_uniforms;
806 key->inline_uniforms = false;
808 if (key->base.nonseamless_cube_mask)
809 nonseamless_size = sizeof(uint32_t);
810 if (key->base.needs_zs_shader_swizzle)
811 zs_swizzle_size = sizeof(struct zink_zs_swizzle_key);
813 if (inline_size || nonseamless_size || zink_cs_key(key)->robust_access || zs_swizzle_size) {
814 struct util_dynarray *shader_cache = &comp->shader_cache[!!nonseamless_size];
815 unsigned count = util_dynarray_num_elements(shader_cache, struct zink_shader_module *);
816 struct zink_shader_module **pzm = shader_cache->data;
817 for (unsigned i = 0; i < count; i++) {
818 struct zink_shader_module *iter = pzm[i];
819 if (!shader_key_matches(iter, key, inline_size,
820 screen->driconf.inline_uniforms,
821 screen->info.have_EXT_non_seamless_cube_map))
823 if (unlikely(zs_swizzle_size)) {
824 /* zs swizzle data needs a manual compare since it's so fat */
825 if (memcmp(iter->key + iter->key_size + nonseamless_size + inline_size * sizeof(uint32_t),
826 &ctx->di.zs_swizzle[MESA_SHADER_COMPUTE], zs_swizzle_size))
830 struct zink_shader_module *zero = pzm[0];
841 zm = malloc(sizeof(struct zink_shader_module) + nonseamless_size + inline_size * sizeof(uint32_t) + zs_swizzle_size);
845 mod = zink_shader_compile(screen, zs, comp->shader->nir, key, zs_swizzle_size ? &ctx->di.zs_swizzle[MESA_SHADER_COMPUTE] : NULL);
851 zm->num_uniforms = inline_size;
852 zm->key_size = key->size;
853 memcpy(zm->key, key, key->size);
854 zm->has_nonseamless = !!nonseamless_size;
855 zm->needs_zs_shader_swizzle = !!zs_swizzle_size;
856 assert(nonseamless_size || inline_size || zink_cs_key(key)->robust_access || zs_swizzle_size);
857 if (nonseamless_size)
858 memcpy(zm->key + zm->key_size, &key->base.nonseamless_cube_mask, nonseamless_size);
860 memcpy(zm->key + zm->key_size + nonseamless_size, key->base.inlined_uniform_values, inline_size * sizeof(uint32_t));
862 memcpy(zm->key + zm->key_size + nonseamless_size + inline_size * sizeof(uint32_t), &ctx->di.zs_swizzle[MESA_SHADER_COMPUTE], zs_swizzle_size);
864 zm->hash = shader_module_hash(zm);
865 zm->default_variant = false;
867 comp->inlined_variant_count++;
869 /* this is otherwise the default variant, which is stored as comp->module */
870 if (zm->num_uniforms || nonseamless_size || zink_cs_key(key)->robust_access || zs_swizzle_size)
871 util_dynarray_append(&comp->shader_cache[!!nonseamless_size], void*, zm);
873 if (comp->curr == zm)
875 ctx->compute_pipeline_state.final_hash ^= ctx->compute_pipeline_state.module_hash;
877 ctx->compute_pipeline_state.module_hash = zm->hash;
878 ctx->compute_pipeline_state.final_hash ^= ctx->compute_pipeline_state.module_hash;
879 ctx->compute_pipeline_state.module_changed = true;
883 zink_update_compute_program(struct zink_context *ctx)
885 util_queue_fence_wait(&ctx->curr_compute->base.cache_fence);
886 update_cs_shader_module(ctx, ctx->curr_compute);
890 zink_pipeline_layout_create(struct zink_screen *screen, VkDescriptorSetLayout *dsl, unsigned num_dsl, bool is_compute, VkPipelineLayoutCreateFlags flags)
892 VkPipelineLayoutCreateInfo plci = {0};
893 plci.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
896 plci.pSetLayouts = dsl;
897 plci.setLayoutCount = num_dsl;
899 VkPushConstantRange pcr;
901 pcr.stageFlags = VK_SHADER_STAGE_ALL_GRAPHICS;
903 pcr.size = sizeof(struct zink_gfx_push_constant);
904 plci.pushConstantRangeCount = 1;
905 plci.pPushConstantRanges = &pcr;
908 VkPipelineLayout layout;
909 VkResult result = VKSCR(CreatePipelineLayout)(screen->dev, &plci, NULL, &layout);
910 if (result != VK_SUCCESS) {
911 mesa_loge("vkCreatePipelineLayout failed (%s)", vk_Result_to_str(result));
912 return VK_NULL_HANDLE;
919 create_program(struct zink_context *ctx, bool is_compute)
921 struct zink_program *pg = rzalloc_size(NULL, is_compute ? sizeof(struct zink_compute_program) : sizeof(struct zink_gfx_program));
925 pipe_reference_init(&pg->reference, 1);
926 util_queue_fence_init(&pg->cache_fence);
927 pg->is_compute = is_compute;
933 assign_io(struct zink_screen *screen,
934 struct zink_gfx_program *prog,
935 struct zink_shader *stages[ZINK_GFX_SHADER_COUNT])
937 struct zink_shader *shaders[MESA_SHADER_STAGES];
939 /* build array in pipeline order */
940 for (unsigned i = 0; i < ZINK_GFX_SHADER_COUNT; i++)
941 shaders[i] = stages[i];
943 for (unsigned i = 0; i < MESA_SHADER_FRAGMENT;) {
944 nir_shader *producer = shaders[i]->nir;
945 for (unsigned j = i + 1; j < ZINK_GFX_SHADER_COUNT; i++, j++) {
946 struct zink_shader *consumer = shaders[j];
949 if (!prog->nir[producer->info.stage])
950 prog->nir[producer->info.stage] = nir_shader_clone(prog, producer);
952 prog->nir[j] = nir_shader_clone(prog, consumer->nir);
953 zink_compiler_assign_io(screen, prog->nir[producer->info.stage], prog->nir[j]);
961 zink_gfx_lib_cache_unref(struct zink_screen *screen, struct zink_gfx_lib_cache *libs)
963 if (!p_atomic_dec_zero(&libs->refcount))
966 simple_mtx_destroy(&libs->lock);
967 set_foreach_remove(&libs->libs, he) {
968 struct zink_gfx_library_key *gkey = (void*)he->key;
969 VKSCR(DestroyPipeline)(screen->dev, gkey->pipeline, NULL);
975 static struct zink_gfx_lib_cache *
976 create_lib_cache(struct zink_gfx_program *prog, bool generated_tcs)
978 struct zink_gfx_lib_cache *libs = rzalloc(NULL, struct zink_gfx_lib_cache);
979 simple_mtx_init(&libs->lock, mtx_plain);
981 _mesa_set_init(&libs->libs, libs, hash_pipeline_lib_generated_tcs, equals_pipeline_lib_generated_tcs);
983 _mesa_set_init(&libs->libs, libs, hash_pipeline_lib, equals_pipeline_lib);
987 static struct zink_gfx_lib_cache *
988 find_or_create_lib_cache(struct zink_screen *screen, struct zink_gfx_program *prog)
990 unsigned stages_present = prog->stages_present;
991 bool generated_tcs = prog->shaders[MESA_SHADER_TESS_CTRL] && prog->shaders[MESA_SHADER_TESS_CTRL]->non_fs.is_generated;
993 stages_present &= ~BITFIELD_BIT(MESA_SHADER_TESS_CTRL);
994 unsigned idx = zink_program_cache_stages(stages_present);
995 struct set *ht = &screen->pipeline_libs[idx];
996 const uint32_t hash = prog->gfx_hash;
998 simple_mtx_lock(&screen->pipeline_libs_lock[idx]);
1000 struct set_entry *entry = _mesa_set_search_or_add_pre_hashed(ht, hash, prog->shaders, &found);
1001 struct zink_gfx_lib_cache *libs;
1003 libs = (void*)entry->key;
1005 libs = create_lib_cache(prog, generated_tcs);
1006 memcpy(libs->shaders, prog->shaders, sizeof(prog->shaders));
1009 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
1010 if (prog->shaders[i] && (!generated_tcs || i != MESA_SHADER_TESS_CTRL)) {
1011 simple_mtx_lock(&prog->shaders[i]->lock);
1012 util_dynarray_append(&prog->shaders[i]->pipeline_libs, struct zink_gfx_lib_cache*, libs);
1013 simple_mtx_unlock(&prog->shaders[i]->lock);
1017 p_atomic_set(&libs->refcount, refs);
1019 simple_mtx_unlock(&screen->pipeline_libs_lock[idx]);
1023 struct zink_gfx_program *
1024 zink_create_gfx_program(struct zink_context *ctx,
1025 struct zink_shader **stages,
1026 unsigned vertices_per_patch,
1029 struct zink_screen *screen = zink_screen(ctx->base.screen);
1030 struct zink_gfx_program *prog = create_program(ctx, false);
1035 prog->gfx_hash = gfx_hash;
1036 prog->base.removed = true;
1038 prog->has_edgeflags = prog->shaders[MESA_SHADER_VERTEX] &&
1039 prog->shaders[MESA_SHADER_VERTEX]->has_edgeflags;
1040 for (int i = 0; i < ZINK_GFX_SHADER_COUNT; ++i) {
1041 util_dynarray_init(&prog->shader_cache[i][0][0], prog);
1042 util_dynarray_init(&prog->shader_cache[i][0][1], prog);
1043 util_dynarray_init(&prog->shader_cache[i][1][0], prog);
1044 util_dynarray_init(&prog->shader_cache[i][1][1], prog);
1046 prog->shaders[i] = stages[i];
1047 prog->stages_present |= BITFIELD_BIT(i);
1050 if (stages[MESA_SHADER_TESS_EVAL] && !stages[MESA_SHADER_TESS_CTRL]) {
1051 prog->shaders[MESA_SHADER_TESS_EVAL]->non_fs.generated_tcs =
1052 prog->shaders[MESA_SHADER_TESS_CTRL] =
1053 zink_shader_tcs_create(screen, stages[MESA_SHADER_VERTEX], vertices_per_patch);
1054 prog->stages_present |= BITFIELD_BIT(MESA_SHADER_TESS_CTRL);
1056 prog->stages_remaining = prog->stages_present;
1058 assign_io(screen, prog, prog->shaders);
1060 if (stages[MESA_SHADER_GEOMETRY])
1061 prog->last_vertex_stage = stages[MESA_SHADER_GEOMETRY];
1062 else if (stages[MESA_SHADER_TESS_EVAL])
1063 prog->last_vertex_stage = stages[MESA_SHADER_TESS_EVAL];
1065 prog->last_vertex_stage = stages[MESA_SHADER_VERTEX];
1067 for (int r = 0; r < ARRAY_SIZE(prog->pipelines); ++r) {
1068 for (int i = 0; i < ARRAY_SIZE(prog->pipelines[0]); ++i) {
1069 _mesa_hash_table_init(&prog->pipelines[r][i], prog, NULL, zink_get_gfx_pipeline_eq_func(screen, prog));
1070 /* only need first 3/4 for point/line/tri/patch */
1071 if (screen->info.have_EXT_extended_dynamic_state &&
1072 i == (prog->last_vertex_stage->nir->info.stage == MESA_SHADER_TESS_EVAL ? 4 : 3))
1077 if (screen->optimal_keys)
1078 prog->libs = find_or_create_lib_cache(screen, prog);
1080 struct mesa_sha1 sctx;
1081 _mesa_sha1_init(&sctx);
1082 for (int i = 0; i < ZINK_GFX_SHADER_COUNT; ++i) {
1083 if (prog->shaders[i]) {
1084 simple_mtx_lock(&prog->shaders[i]->lock);
1085 _mesa_set_add(prog->shaders[i]->programs, prog);
1086 simple_mtx_unlock(&prog->shaders[i]->lock);
1087 zink_gfx_program_reference(screen, NULL, prog);
1088 _mesa_sha1_update(&sctx, prog->shaders[i]->base.sha1, sizeof(prog->shaders[i]->base.sha1));
1091 _mesa_sha1_final(&sctx, prog->base.sha1);
1093 if (!zink_descriptor_program_init(ctx, &prog->base))
1100 zink_destroy_gfx_program(screen, prog);
1104 /* Creates a replacement, optimized zink_gfx_program for this set of separate shaders, which will
1105 * be swapped in in place of the fast-linked separable program once it's done compiling.
1108 create_linked_separable_job(void *data, void *gdata, int thread_index)
1110 struct zink_gfx_program *prog = data;
1111 prog->full_prog = zink_create_gfx_program(prog->ctx, prog->shaders, 0, prog->gfx_hash);
1112 precompile_job(prog->full_prog, gdata, thread_index);
1115 struct zink_gfx_program *
1116 create_gfx_program_separable(struct zink_context *ctx, struct zink_shader **stages, unsigned vertices_per_patch)
1118 struct zink_screen *screen = zink_screen(ctx->base.screen);
1119 unsigned shader_stages = BITFIELD_BIT(MESA_SHADER_VERTEX) | BITFIELD_BIT(MESA_SHADER_FRAGMENT);
1120 /* filter cases that need real pipelines */
1121 if (ctx->shader_stages != shader_stages ||
1122 !stages[MESA_SHADER_VERTEX]->precompile.mod || !stages[MESA_SHADER_FRAGMENT]->precompile.mod ||
1123 /* TODO: maybe try variants? grimace */
1124 !ZINK_SHADER_KEY_OPTIMAL_IS_DEFAULT(ctx->gfx_pipeline_state.optimal_key) ||
1125 !zink_can_use_pipeline_libs(ctx))
1126 return zink_create_gfx_program(ctx, stages, vertices_per_patch, ctx->gfx_hash);
1127 /* ensure async gpl creation is done */
1128 util_queue_fence_wait(&stages[MESA_SHADER_VERTEX]->precompile.fence);
1129 util_queue_fence_wait(&stages[MESA_SHADER_FRAGMENT]->precompile.fence);
1131 struct zink_gfx_program *prog = create_program(ctx, false);
1136 prog->is_separable = true;
1137 prog->gfx_hash = ctx->gfx_hash;
1139 prog->shaders[MESA_SHADER_VERTEX] = stages[MESA_SHADER_VERTEX];
1140 prog->stages_remaining = prog->stages_present = shader_stages;
1141 prog->shaders[MESA_SHADER_FRAGMENT] = stages[MESA_SHADER_FRAGMENT];
1142 prog->last_vertex_stage = stages[MESA_SHADER_VERTEX];
1143 prog->libs = create_lib_cache(prog, false);
1144 /* this libs cache is owned by the program */
1145 p_atomic_set(&prog->libs->refcount, 1);
1148 for (int i = 0; i < ZINK_GFX_SHADER_COUNT; ++i) {
1149 if (prog->shaders[i]) {
1150 simple_mtx_lock(&prog->shaders[i]->lock);
1151 _mesa_set_add(prog->shaders[i]->programs, prog);
1152 simple_mtx_unlock(&prog->shaders[i]->lock);
1156 /* We can do this add after the _mesa_set_adds above because we know the prog->shaders[] are
1157 * referenced by the draw state and zink_shader_free() can't be called on them while we're in here.
1159 p_atomic_add(&prog->base.reference.count, refs);
1161 for (int r = 0; r < ARRAY_SIZE(prog->pipelines); ++r) {
1162 for (int i = 0; i < ARRAY_SIZE(prog->pipelines[0]); ++i) {
1163 _mesa_hash_table_init(&prog->pipelines[r][i], prog, NULL, zink_get_gfx_pipeline_eq_func(screen, prog));
1164 /* only need first 3/4 for point/line/tri/patch */
1165 if (screen->info.have_EXT_extended_dynamic_state &&
1166 i == (prog->last_vertex_stage->nir->info.stage == MESA_SHADER_TESS_EVAL ? 4 : 3))
1171 if (prog->shaders[MESA_SHADER_VERTEX]->precompile.dsl) {
1172 prog->base.dd.binding_usage |= BITFIELD_BIT(0);
1173 prog->base.dsl[prog->base.num_dsl] = prog->shaders[MESA_SHADER_VERTEX]->precompile.dsl;
1174 prog->base.num_dsl++;
1176 if (prog->shaders[MESA_SHADER_FRAGMENT]->precompile.dsl) {
1177 prog->base.dd.binding_usage |= BITFIELD_BIT(1);
1178 prog->base.dsl[prog->base.num_dsl] = prog->shaders[MESA_SHADER_FRAGMENT]->precompile.dsl;
1179 /* guarantee a null dsl if vs doesn't have descriptors */
1180 prog->base.num_dsl = 2;
1182 prog->base.dd.bindless = prog->shaders[MESA_SHADER_VERTEX]->bindless | prog->shaders[MESA_SHADER_FRAGMENT]->bindless;
1183 if (prog->base.dd.bindless) {
1184 prog->base.num_dsl = screen->compact_descriptors ? ZINK_DESCRIPTOR_ALL_TYPES - ZINK_DESCRIPTOR_COMPACT : ZINK_DESCRIPTOR_ALL_TYPES;
1185 prog->base.dsl[screen->desc_set_id[ZINK_DESCRIPTOR_BINDLESS]] = screen->bindless_layout;
1187 prog->base.layout = zink_pipeline_layout_create(screen, prog->base.dsl, prog->base.num_dsl, false, VK_PIPELINE_LAYOUT_CREATE_INDEPENDENT_SETS_BIT_EXT);
1189 VkPipeline libs[] = {stages[MESA_SHADER_VERTEX]->precompile.gpl, stages[MESA_SHADER_FRAGMENT]->precompile.gpl};
1190 prog->last_variant_hash = ctx->gfx_pipeline_state.optimal_key;
1192 struct zink_gfx_library_key *gkey = CALLOC_STRUCT(zink_gfx_library_key);
1193 gkey->optimal_key = prog->last_variant_hash;
1194 assert(gkey->optimal_key);
1195 gkey->pipeline = zink_create_gfx_pipeline_combined(screen, prog, VK_NULL_HANDLE, libs, 2, VK_NULL_HANDLE, false);
1196 _mesa_set_add(&prog->libs->libs, gkey);
1198 util_queue_add_job(&screen->cache_get_thread, prog, &prog->base.cache_fence, create_linked_separable_job, NULL, 0);
1203 zink_destroy_gfx_program(screen, prog);
1208 hash_compute_pipeline_state_local_size(const void *key)
1210 const struct zink_compute_pipeline_state *state = key;
1211 uint32_t hash = _mesa_hash_data(state, offsetof(struct zink_compute_pipeline_state, hash));
1212 hash = XXH32(&state->local_size[0], sizeof(state->local_size), hash);
1217 hash_compute_pipeline_state(const void *key)
1219 const struct zink_compute_pipeline_state *state = key;
1220 return _mesa_hash_data(state, offsetof(struct zink_compute_pipeline_state, hash));
1224 zink_program_update_compute_pipeline_state(struct zink_context *ctx, struct zink_compute_program *comp, const uint block[3])
1226 if (comp->use_local_size) {
1227 for (int i = 0; i < ARRAY_SIZE(ctx->compute_pipeline_state.local_size); i++) {
1228 if (ctx->compute_pipeline_state.local_size[i] != block[i])
1229 ctx->compute_pipeline_state.dirty = true;
1230 ctx->compute_pipeline_state.local_size[i] = block[i];
1236 equals_compute_pipeline_state(const void *a, const void *b)
1238 const struct zink_compute_pipeline_state *sa = a;
1239 const struct zink_compute_pipeline_state *sb = b;
1240 return !memcmp(a, b, offsetof(struct zink_compute_pipeline_state, hash)) &&
1241 sa->module == sb->module;
1245 equals_compute_pipeline_state_local_size(const void *a, const void *b)
1247 const struct zink_compute_pipeline_state *sa = a;
1248 const struct zink_compute_pipeline_state *sb = b;
1249 return !memcmp(a, b, offsetof(struct zink_compute_pipeline_state, hash)) &&
1250 !memcmp(sa->local_size, sb->local_size, sizeof(sa->local_size)) &&
1251 sa->module == sb->module;
1255 precompile_compute_job(void *data, void *gdata, int thread_index)
1257 struct zink_compute_program *comp = data;
1258 struct zink_screen *screen = gdata;
1260 comp->shader = zink_shader_create(screen, comp->nir, NULL);
1261 comp->curr = comp->module = CALLOC_STRUCT(zink_shader_module);
1262 assert(comp->module);
1263 comp->module->shader = zink_shader_compile(screen, comp->shader, comp->shader->nir, NULL, NULL);
1264 assert(comp->module->shader);
1265 util_dynarray_init(&comp->shader_cache[0], comp);
1266 util_dynarray_init(&comp->shader_cache[1], comp);
1268 struct blob blob = {0};
1270 nir_serialize(&blob, comp->shader->nir, true);
1272 struct mesa_sha1 sha1_ctx;
1273 _mesa_sha1_init(&sha1_ctx);
1274 _mesa_sha1_update(&sha1_ctx, blob.data, blob.size);
1275 _mesa_sha1_final(&sha1_ctx, comp->base.sha1);
1278 zink_descriptor_program_init(comp->base.ctx, &comp->base);
1280 zink_screen_get_pipeline_cache(screen, &comp->base, true);
1281 if (comp->base.can_precompile)
1282 comp->base_pipeline = zink_create_compute_pipeline(screen, comp, NULL);
1283 if (comp->base_pipeline)
1284 zink_screen_update_pipeline_cache(screen, &comp->base, true);
1287 static struct zink_compute_program *
1288 create_compute_program(struct zink_context *ctx, nir_shader *nir)
1290 struct zink_screen *screen = zink_screen(ctx->base.screen);
1291 struct zink_compute_program *comp = create_program(ctx, true);
1296 comp->use_local_size = !(nir->info.workgroup_size[0] ||
1297 nir->info.workgroup_size[1] ||
1298 nir->info.workgroup_size[2]);
1299 comp->base.can_precompile = !comp->use_local_size &&
1300 (screen->info.have_EXT_non_seamless_cube_map || !zink_shader_has_cubes(nir)) &&
1301 (screen->info.rb2_feats.robustImageAccess2 || !(ctx->flags & PIPE_CONTEXT_ROBUST_BUFFER_ACCESS));
1302 _mesa_hash_table_init(&comp->pipelines, comp, NULL, comp->use_local_size ?
1303 equals_compute_pipeline_state_local_size :
1304 equals_compute_pipeline_state);
1305 util_queue_add_job(&screen->cache_get_thread, comp, &comp->base.cache_fence,
1306 precompile_compute_job, NULL, 0);
1311 zink_program_get_descriptor_usage(struct zink_context *ctx, gl_shader_stage stage, enum zink_descriptor_type type)
1313 struct zink_shader *zs = NULL;
1315 case MESA_SHADER_VERTEX:
1316 case MESA_SHADER_TESS_CTRL:
1317 case MESA_SHADER_TESS_EVAL:
1318 case MESA_SHADER_GEOMETRY:
1319 case MESA_SHADER_FRAGMENT:
1320 zs = ctx->gfx_stages[stage];
1322 case MESA_SHADER_COMPUTE: {
1323 zs = ctx->curr_compute->shader;
1327 unreachable("unknown shader type");
1332 case ZINK_DESCRIPTOR_TYPE_UBO:
1333 return zs->ubos_used;
1334 case ZINK_DESCRIPTOR_TYPE_SSBO:
1335 return zs->ssbos_used;
1336 case ZINK_DESCRIPTOR_TYPE_SAMPLER_VIEW:
1337 return BITSET_TEST_RANGE(zs->nir->info.textures_used, 0, PIPE_MAX_SAMPLERS - 1);
1338 case ZINK_DESCRIPTOR_TYPE_IMAGE:
1339 return BITSET_TEST_RANGE(zs->nir->info.images_used, 0, PIPE_MAX_SAMPLERS - 1);
1341 unreachable("unknown descriptor type!");
1347 zink_program_descriptor_is_buffer(struct zink_context *ctx, gl_shader_stage stage, enum zink_descriptor_type type, unsigned i)
1349 struct zink_shader *zs = NULL;
1351 case MESA_SHADER_VERTEX:
1352 case MESA_SHADER_TESS_CTRL:
1353 case MESA_SHADER_TESS_EVAL:
1354 case MESA_SHADER_GEOMETRY:
1355 case MESA_SHADER_FRAGMENT:
1356 zs = ctx->gfx_stages[stage];
1358 case MESA_SHADER_COMPUTE: {
1359 zs = ctx->curr_compute->shader;
1363 unreachable("unknown shader type");
1367 return zink_shader_descriptor_is_buffer(zs, type, i);
1371 get_num_bindings(struct zink_shader *zs, enum zink_descriptor_type type)
1374 case ZINK_DESCRIPTOR_TYPE_UNIFORMS:
1375 return !!zs->has_uniforms;
1376 case ZINK_DESCRIPTOR_TYPE_UBO:
1377 case ZINK_DESCRIPTOR_TYPE_SSBO:
1378 return zs->num_bindings[type];
1382 unsigned num_bindings = 0;
1383 for (int i = 0; i < zs->num_bindings[type]; i++)
1384 num_bindings += zs->bindings[type][i].size;
1385 return num_bindings;
1389 zink_program_num_bindings_typed(const struct zink_program *pg, enum zink_descriptor_type type)
1391 unsigned num_bindings = 0;
1392 if (pg->is_compute) {
1393 struct zink_compute_program *comp = (void*)pg;
1394 return get_num_bindings(comp->shader, type);
1396 struct zink_gfx_program *prog = (void*)pg;
1397 for (unsigned i = 0; i < ZINK_GFX_SHADER_COUNT; i++) {
1398 if (prog->shaders[i])
1399 num_bindings += get_num_bindings(prog->shaders[i], type);
1401 return num_bindings;
1405 zink_program_num_bindings(const struct zink_program *pg)
1407 unsigned num_bindings = 0;
1408 for (unsigned i = 0; i < ZINK_DESCRIPTOR_BASE_TYPES; i++)
1409 num_bindings += zink_program_num_bindings_typed(pg, i);
1410 return num_bindings;
1414 deinit_program(struct zink_screen *screen, struct zink_program *pg)
1416 util_queue_fence_wait(&pg->cache_fence);
1418 VKSCR(DestroyPipelineLayout)(screen->dev, pg->layout, NULL);
1420 if (pg->pipeline_cache)
1421 VKSCR(DestroyPipelineCache)(screen->dev, pg->pipeline_cache, NULL);
1422 zink_descriptor_program_deinit(screen, pg);
1426 zink_destroy_gfx_program(struct zink_screen *screen,
1427 struct zink_gfx_program *prog)
1429 unsigned max_idx = ARRAY_SIZE(prog->pipelines[0]);
1430 if (screen->info.have_EXT_extended_dynamic_state) {
1431 /* only need first 3/4 for point/line/tri/patch */
1432 if ((prog->stages_present &
1433 (BITFIELD_BIT(MESA_SHADER_TESS_EVAL) | BITFIELD_BIT(MESA_SHADER_GEOMETRY))) ==
1434 BITFIELD_BIT(MESA_SHADER_TESS_EVAL))
1441 if (prog->is_separable)
1442 zink_gfx_program_reference(screen, &prog->full_prog, NULL);
1443 for (unsigned r = 0; r < ARRAY_SIZE(prog->pipelines); r++) {
1444 for (int i = 0; i < max_idx; ++i) {
1445 hash_table_foreach(&prog->pipelines[r][i], entry) {
1446 struct zink_gfx_pipeline_cache_entry *pc_entry = entry->data;
1448 util_queue_fence_wait(&pc_entry->fence);
1449 VKSCR(DestroyPipeline)(screen->dev, pc_entry->pipeline, NULL);
1450 VKSCR(DestroyPipeline)(screen->dev, pc_entry->unoptimized_pipeline, NULL);
1456 deinit_program(screen, &prog->base);
1458 for (int i = 0; i < ZINK_GFX_SHADER_COUNT; ++i) {
1459 if (prog->shaders[i]) {
1460 _mesa_set_remove_key(prog->shaders[i]->programs, prog);
1461 prog->shaders[i] = NULL;
1463 if (!prog->is_separable) {
1464 destroy_shader_cache(screen, &prog->shader_cache[i][0][0]);
1465 destroy_shader_cache(screen, &prog->shader_cache[i][0][1]);
1466 destroy_shader_cache(screen, &prog->shader_cache[i][1][0]);
1467 destroy_shader_cache(screen, &prog->shader_cache[i][1][1]);
1468 ralloc_free(prog->nir[i]);
1471 if (prog->is_separable)
1472 zink_gfx_lib_cache_unref(screen, prog->libs);
1478 zink_destroy_compute_program(struct zink_screen *screen,
1479 struct zink_compute_program *comp)
1481 deinit_program(screen, &comp->base);
1483 assert(comp->shader);
1484 assert(!comp->shader->spirv);
1486 _mesa_set_destroy(comp->shader->programs, NULL);
1487 ralloc_free(comp->shader->nir);
1488 ralloc_free(comp->shader);
1490 destroy_shader_cache(screen, &comp->shader_cache[0]);
1491 destroy_shader_cache(screen, &comp->shader_cache[1]);
1493 hash_table_foreach(&comp->pipelines, entry) {
1494 struct compute_pipeline_cache_entry *pc_entry = entry->data;
1496 VKSCR(DestroyPipeline)(screen->dev, pc_entry->pipeline, NULL);
1499 VKSCR(DestroyPipeline)(screen->dev, comp->base_pipeline, NULL);
1500 VKSCR(DestroyShaderModule)(screen->dev, comp->module->shader, NULL);
1506 ALWAYS_INLINE static bool
1507 compute_can_shortcut(const struct zink_compute_program *comp)
1509 return !comp->use_local_size && !comp->curr->num_uniforms && !comp->curr->has_nonseamless;
1513 zink_get_compute_pipeline(struct zink_screen *screen,
1514 struct zink_compute_program *comp,
1515 struct zink_compute_pipeline_state *state)
1517 struct hash_entry *entry = NULL;
1519 if (!state->dirty && !state->module_changed)
1520 return state->pipeline;
1522 if (state->pipeline) //avoid on first hash
1523 state->final_hash ^= state->hash;
1524 if (comp->use_local_size)
1525 state->hash = hash_compute_pipeline_state_local_size(state);
1527 state->hash = hash_compute_pipeline_state(state);
1528 state->dirty = false;
1529 state->final_hash ^= state->hash;
1532 util_queue_fence_wait(&comp->base.cache_fence);
1533 if (comp->base_pipeline && compute_can_shortcut(comp)) {
1534 state->pipeline = comp->base_pipeline;
1535 return state->pipeline;
1537 entry = _mesa_hash_table_search_pre_hashed(&comp->pipelines, state->final_hash, state);
1540 VkPipeline pipeline = zink_create_compute_pipeline(screen, comp, state);
1542 if (pipeline == VK_NULL_HANDLE)
1543 return VK_NULL_HANDLE;
1545 zink_screen_update_pipeline_cache(screen, &comp->base, false);
1546 if (compute_can_shortcut(comp)) {
1547 /* don't add base pipeline to cache */
1548 state->pipeline = comp->base_pipeline = pipeline;
1549 return state->pipeline;
1552 struct compute_pipeline_cache_entry *pc_entry = CALLOC_STRUCT(compute_pipeline_cache_entry);
1554 return VK_NULL_HANDLE;
1556 memcpy(&pc_entry->state, state, sizeof(*state));
1557 pc_entry->pipeline = pipeline;
1559 entry = _mesa_hash_table_insert_pre_hashed(&comp->pipelines, state->final_hash, pc_entry, pc_entry);
1563 struct compute_pipeline_cache_entry *cache_entry = entry->data;
1564 state->pipeline = cache_entry->pipeline;
1565 return state->pipeline;
1569 bind_gfx_stage(struct zink_context *ctx, gl_shader_stage stage, struct zink_shader *shader);
1572 unbind_generated_gs(struct zink_context *ctx, gl_shader_stage stage, struct zink_shader *shader)
1574 for (int i = 0; i < ARRAY_SIZE(shader->non_fs.generated_gs); i++) {
1575 if (ctx->gfx_stages[stage]->non_fs.generated_gs[i] &&
1576 ctx->gfx_stages[MESA_SHADER_GEOMETRY] ==
1577 ctx->gfx_stages[stage]->non_fs.generated_gs[i]) {
1578 assert(stage != MESA_SHADER_GEOMETRY); /* let's not keep recursing! */
1579 bind_gfx_stage(ctx, MESA_SHADER_GEOMETRY, NULL);
1585 bind_gfx_stage(struct zink_context *ctx, gl_shader_stage stage, struct zink_shader *shader)
1587 if (shader && shader->nir->info.num_inlinable_uniforms)
1588 ctx->shader_has_inlinable_uniforms_mask |= 1 << stage;
1590 ctx->shader_has_inlinable_uniforms_mask &= ~(1 << stage);
1592 if (ctx->gfx_stages[stage]) {
1593 ctx->gfx_hash ^= ctx->gfx_stages[stage]->hash;
1595 if (stage != MESA_SHADER_FRAGMENT)
1596 unbind_generated_gs(ctx, stage, shader);
1599 ctx->gfx_stages[stage] = shader;
1600 ctx->gfx_dirty = ctx->gfx_stages[MESA_SHADER_FRAGMENT] && ctx->gfx_stages[MESA_SHADER_VERTEX];
1601 ctx->gfx_pipeline_state.modules_changed = true;
1603 ctx->shader_stages |= BITFIELD_BIT(stage);
1604 ctx->gfx_hash ^= ctx->gfx_stages[stage]->hash;
1606 ctx->gfx_pipeline_state.modules[stage] = VK_NULL_HANDLE;
1607 if (ctx->curr_program)
1608 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
1609 ctx->curr_program = NULL;
1610 ctx->shader_stages &= ~BITFIELD_BIT(stage);
1614 static enum pipe_prim_type
1615 gs_output_to_reduced_prim_type(struct shader_info *info)
1617 switch (info->gs.output_primitive) {
1618 case SHADER_PRIM_POINTS:
1619 return PIPE_PRIM_POINTS;
1621 case SHADER_PRIM_LINES:
1622 case SHADER_PRIM_LINE_LOOP:
1623 case SHADER_PRIM_LINE_STRIP:
1624 case SHADER_PRIM_LINES_ADJACENCY:
1625 case SHADER_PRIM_LINE_STRIP_ADJACENCY:
1626 return PIPE_PRIM_LINES;
1628 case SHADER_PRIM_TRIANGLES:
1629 case SHADER_PRIM_TRIANGLE_STRIP:
1630 case SHADER_PRIM_TRIANGLE_FAN:
1631 case SHADER_PRIM_TRIANGLES_ADJACENCY:
1632 case SHADER_PRIM_TRIANGLE_STRIP_ADJACENCY:
1633 return PIPE_PRIM_TRIANGLES;
1636 unreachable("unexpected output primitive type");
1640 static enum pipe_prim_type
1641 update_rast_prim(struct zink_shader *shader)
1643 struct shader_info *info = &shader->nir->info;
1644 if (info->stage == MESA_SHADER_GEOMETRY)
1645 return gs_output_to_reduced_prim_type(info);
1646 else if (info->stage == MESA_SHADER_TESS_EVAL) {
1647 if (info->tess.point_mode)
1648 return PIPE_PRIM_POINTS;
1650 switch (info->tess._primitive_mode) {
1651 case TESS_PRIMITIVE_ISOLINES:
1652 return PIPE_PRIM_LINES;
1653 case TESS_PRIMITIVE_TRIANGLES:
1654 case TESS_PRIMITIVE_QUADS:
1655 return PIPE_PRIM_TRIANGLES;
1657 return PIPE_PRIM_MAX;
1661 return PIPE_PRIM_MAX;
1665 bind_last_vertex_stage(struct zink_context *ctx)
1667 gl_shader_stage old = ctx->last_vertex_stage ? ctx->last_vertex_stage->nir->info.stage : MESA_SHADER_STAGES;
1668 if (ctx->gfx_stages[MESA_SHADER_GEOMETRY])
1669 ctx->last_vertex_stage = ctx->gfx_stages[MESA_SHADER_GEOMETRY];
1670 else if (ctx->gfx_stages[MESA_SHADER_TESS_EVAL])
1671 ctx->last_vertex_stage = ctx->gfx_stages[MESA_SHADER_TESS_EVAL];
1673 ctx->last_vertex_stage = ctx->gfx_stages[MESA_SHADER_VERTEX];
1674 gl_shader_stage current = ctx->last_vertex_stage ? ctx->last_vertex_stage->nir->info.stage : MESA_SHADER_VERTEX;
1676 /* update rast_prim */
1677 ctx->gfx_pipeline_state.shader_rast_prim =
1678 ctx->last_vertex_stage ? update_rast_prim(ctx->last_vertex_stage) :
1681 if (old != current) {
1682 if (!zink_screen(ctx->base.screen)->optimal_keys) {
1683 if (old != MESA_SHADER_STAGES) {
1684 memset(&ctx->gfx_pipeline_state.shader_keys.key[old].key.vs_base, 0, sizeof(struct zink_vs_key_base));
1685 ctx->dirty_gfx_stages |= BITFIELD_BIT(old);
1687 /* always unset vertex shader values when changing to a non-vs last stage */
1688 memset(&ctx->gfx_pipeline_state.shader_keys.key[MESA_SHADER_VERTEX].key.vs_base, 0, sizeof(struct zink_vs_key_base));
1692 unsigned num_viewports = ctx->vp_state.num_viewports;
1693 struct zink_screen *screen = zink_screen(ctx->base.screen);
1694 /* number of enabled viewports is based on whether last vertex stage writes viewport index */
1695 if (ctx->last_vertex_stage) {
1696 if (ctx->last_vertex_stage->nir->info.outputs_written & (VARYING_BIT_VIEWPORT | VARYING_BIT_VIEWPORT_MASK))
1697 ctx->vp_state.num_viewports = MIN2(screen->info.props.limits.maxViewports, PIPE_MAX_VIEWPORTS);
1699 ctx->vp_state.num_viewports = 1;
1701 ctx->vp_state.num_viewports = 1;
1703 ctx->vp_state_changed |= num_viewports != ctx->vp_state.num_viewports;
1704 if (!screen->info.have_EXT_extended_dynamic_state) {
1705 if (ctx->gfx_pipeline_state.dyn_state1.num_viewports != ctx->vp_state.num_viewports)
1706 ctx->gfx_pipeline_state.dirty = true;
1707 ctx->gfx_pipeline_state.dyn_state1.num_viewports = ctx->vp_state.num_viewports;
1709 ctx->last_vertex_stage_dirty = true;
1714 zink_bind_vs_state(struct pipe_context *pctx,
1717 struct zink_context *ctx = zink_context(pctx);
1718 if (!cso && !ctx->gfx_stages[MESA_SHADER_VERTEX])
1720 bind_gfx_stage(ctx, MESA_SHADER_VERTEX, cso);
1721 bind_last_vertex_stage(ctx);
1723 struct zink_shader *zs = cso;
1724 ctx->shader_reads_drawid = BITSET_TEST(zs->nir->info.system_values_read, SYSTEM_VALUE_DRAW_ID);
1725 ctx->shader_reads_basevertex = BITSET_TEST(zs->nir->info.system_values_read, SYSTEM_VALUE_BASE_VERTEX);
1727 ctx->shader_reads_drawid = false;
1728 ctx->shader_reads_basevertex = false;
1732 /* if gl_SampleMask[] is written to, we have to ensure that we get a shader with the same sample count:
1733 * in GL, samples==1 means ignore gl_SampleMask[]
1734 * in VK, gl_SampleMask[] is never ignored
1737 zink_update_fs_key_samples(struct zink_context *ctx)
1739 if (!ctx->gfx_stages[MESA_SHADER_FRAGMENT])
1741 nir_shader *nir = ctx->gfx_stages[MESA_SHADER_FRAGMENT]->nir;
1742 if (nir->info.outputs_written & (1 << FRAG_RESULT_SAMPLE_MASK)) {
1743 bool samples = zink_get_fs_base_key(ctx)->samples;
1744 if (samples != (ctx->fb_state.samples > 1))
1745 zink_set_fs_base_key(ctx)->samples = ctx->fb_state.samples > 1;
1749 void zink_update_gs_key_rectangular_line(struct zink_context *ctx)
1751 bool line_rectangular = zink_get_gs_key(ctx)->line_rectangular;
1752 if (line_rectangular != ctx->rast_state->base.line_rectangular)
1753 zink_set_gs_key(ctx)->line_rectangular = ctx->rast_state->base.line_rectangular;
1757 zink_bind_fs_state(struct pipe_context *pctx,
1760 struct zink_context *ctx = zink_context(pctx);
1761 if (!cso && !ctx->gfx_stages[MESA_SHADER_FRAGMENT])
1763 unsigned shadow_mask = ctx->gfx_stages[MESA_SHADER_FRAGMENT] ? ctx->gfx_stages[MESA_SHADER_FRAGMENT]->fs.legacy_shadow_mask : 0;
1764 bind_gfx_stage(ctx, MESA_SHADER_FRAGMENT, cso);
1765 ctx->fbfetch_outputs = 0;
1767 nir_shader *nir = ctx->gfx_stages[MESA_SHADER_FRAGMENT]->nir;
1768 if (nir->info.fs.uses_fbfetch_output) {
1769 nir_foreach_shader_out_variable(var, ctx->gfx_stages[MESA_SHADER_FRAGMENT]->nir) {
1770 if (var->data.fb_fetch_output)
1771 ctx->fbfetch_outputs |= BITFIELD_BIT(var->data.location - FRAG_RESULT_DATA0);
1774 zink_update_fs_key_samples(ctx);
1775 if (zink_screen(pctx->screen)->info.have_EXT_rasterization_order_attachment_access) {
1776 if (ctx->gfx_pipeline_state.rast_attachment_order != nir->info.fs.uses_fbfetch_output)
1777 ctx->gfx_pipeline_state.dirty = true;
1778 ctx->gfx_pipeline_state.rast_attachment_order = nir->info.fs.uses_fbfetch_output;
1780 zink_set_zs_needs_shader_swizzle_key(ctx, MESA_SHADER_FRAGMENT, false);
1781 if (shadow_mask != ctx->gfx_stages[MESA_SHADER_FRAGMENT]->fs.legacy_shadow_mask &&
1782 !zink_screen(pctx->screen)->driver_workarounds.needs_zs_shader_swizzle)
1783 zink_update_shadow_samplerviews(ctx, shadow_mask | ctx->gfx_stages[MESA_SHADER_FRAGMENT]->fs.legacy_shadow_mask);
1784 if (!zink_screen(ctx->base.screen)->driver_workarounds.track_renderpasses && !ctx->blitting)
1785 zink_parse_tc_info(ctx);
1787 zink_update_fbfetch(ctx);
1791 zink_bind_gs_state(struct pipe_context *pctx,
1794 struct zink_context *ctx = zink_context(pctx);
1795 if (!cso && !ctx->gfx_stages[MESA_SHADER_GEOMETRY])
1797 bind_gfx_stage(ctx, MESA_SHADER_GEOMETRY, cso);
1798 bind_last_vertex_stage(ctx);
1802 zink_bind_tcs_state(struct pipe_context *pctx,
1805 bind_gfx_stage(zink_context(pctx), MESA_SHADER_TESS_CTRL, cso);
1809 zink_bind_tes_state(struct pipe_context *pctx,
1812 struct zink_context *ctx = zink_context(pctx);
1813 if (!cso && !ctx->gfx_stages[MESA_SHADER_TESS_EVAL])
1815 if (!!ctx->gfx_stages[MESA_SHADER_TESS_EVAL] != !!cso) {
1817 /* if unsetting a TESS that uses a generated TCS, ensure the TCS is unset */
1818 if (ctx->gfx_stages[MESA_SHADER_TESS_EVAL]->non_fs.generated_tcs)
1819 ctx->gfx_stages[MESA_SHADER_TESS_CTRL] = NULL;
1822 bind_gfx_stage(ctx, MESA_SHADER_TESS_EVAL, cso);
1823 bind_last_vertex_stage(ctx);
1827 zink_create_cs_state(struct pipe_context *pctx,
1828 const struct pipe_compute_state *shader)
1830 struct nir_shader *nir;
1831 if (shader->ir_type != PIPE_SHADER_IR_NIR)
1832 nir = zink_tgsi_to_nir(pctx->screen, shader->prog);
1834 nir = (struct nir_shader *)shader->prog;
1836 if (nir->info.uses_bindless)
1837 zink_descriptors_init_bindless(zink_context(pctx));
1839 return create_compute_program(zink_context(pctx), nir);
1843 zink_bind_cs_state(struct pipe_context *pctx,
1846 struct zink_context *ctx = zink_context(pctx);
1847 struct zink_compute_program *comp = cso;
1848 if (comp && comp->nir->info.num_inlinable_uniforms)
1849 ctx->shader_has_inlinable_uniforms_mask |= 1 << MESA_SHADER_COMPUTE;
1851 ctx->shader_has_inlinable_uniforms_mask &= ~(1 << MESA_SHADER_COMPUTE);
1853 if (ctx->curr_compute) {
1854 zink_batch_reference_program(&ctx->batch, &ctx->curr_compute->base);
1855 ctx->compute_pipeline_state.final_hash ^= ctx->compute_pipeline_state.module_hash;
1856 ctx->compute_pipeline_state.module = VK_NULL_HANDLE;
1857 ctx->compute_pipeline_state.module_hash = 0;
1859 ctx->compute_pipeline_state.dirty = true;
1860 ctx->curr_compute = comp;
1861 if (comp && comp != ctx->curr_compute) {
1862 ctx->compute_pipeline_state.module_hash = ctx->curr_compute->curr->hash;
1863 if (util_queue_fence_is_signalled(&comp->base.cache_fence))
1864 ctx->compute_pipeline_state.module = ctx->curr_compute->curr->shader;
1865 ctx->compute_pipeline_state.final_hash ^= ctx->compute_pipeline_state.module_hash;
1866 if (ctx->compute_pipeline_state.key.base.nonseamless_cube_mask)
1867 ctx->compute_dirty = true;
1869 zink_select_launch_grid(ctx);
1873 zink_delete_cs_shader_state(struct pipe_context *pctx, void *cso)
1875 struct zink_compute_program *comp = cso;
1876 zink_compute_program_reference(zink_screen(pctx->screen), &comp, NULL);
1880 zink_delete_shader_state(struct pipe_context *pctx, void *cso)
1882 zink_shader_free(zink_screen(pctx->screen), cso);
1886 zink_create_gfx_shader_state(struct pipe_context *pctx, const struct pipe_shader_state *shader)
1889 if (shader->type != PIPE_SHADER_IR_NIR)
1890 nir = zink_tgsi_to_nir(pctx->screen, shader->tokens);
1892 nir = (struct nir_shader *)shader->ir.nir;
1894 if (nir->info.stage == MESA_SHADER_FRAGMENT && nir->info.fs.uses_fbfetch_output)
1895 zink_descriptor_util_init_fbfetch(zink_context(pctx));
1896 if (nir->info.uses_bindless)
1897 zink_descriptors_init_bindless(zink_context(pctx));
1899 return zink_shader_create(zink_screen(pctx->screen), nir, &shader->stream_output);
1903 zink_delete_cached_shader_state(struct pipe_context *pctx, void *cso)
1905 struct zink_screen *screen = zink_screen(pctx->screen);
1906 util_shader_reference(pctx, &screen->shaders, &cso, NULL);
1910 zink_create_cached_shader_state(struct pipe_context *pctx, const struct pipe_shader_state *shader)
1913 struct zink_screen *screen = zink_screen(pctx->screen);
1914 return util_live_shader_cache_get(pctx, &screen->shaders, shader, &cache_hit);
1917 /* caller must lock prog->libs->lock */
1918 struct zink_gfx_library_key *
1919 zink_create_pipeline_lib(struct zink_screen *screen, struct zink_gfx_program *prog, struct zink_gfx_pipeline_state *state)
1921 struct zink_gfx_library_key *gkey = CALLOC_STRUCT(zink_gfx_library_key);
1922 gkey->optimal_key = state->optimal_key;
1923 assert(gkey->optimal_key);
1924 memcpy(gkey->modules, prog->modules, sizeof(gkey->modules));
1925 gkey->pipeline = zink_create_gfx_pipeline_library(screen, prog);
1926 _mesa_set_add(&prog->libs->libs, gkey);
1931 print_exe_stages(VkShaderStageFlags stages)
1933 if (stages == VK_SHADER_STAGE_VERTEX_BIT)
1935 if (stages == (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT))
1937 if (stages == (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT))
1938 return "VS+TCS+TES";
1939 if (stages == (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT | VK_SHADER_STAGE_GEOMETRY_BIT))
1940 return "VS+TCS+TES+GS";
1941 if (stages == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT)
1943 if (stages == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)
1945 if (stages == VK_SHADER_STAGE_GEOMETRY_BIT)
1947 if (stages == VK_SHADER_STAGE_FRAGMENT_BIT)
1949 if (stages == VK_SHADER_STAGE_COMPUTE_BIT)
1951 unreachable("unhandled combination of stages!");
1955 print_pipeline_stats(struct zink_screen *screen, VkPipeline pipeline)
1957 VkPipelineInfoKHR pinfo = {
1958 VK_STRUCTURE_TYPE_PIPELINE_INFO_KHR,
1962 unsigned exe_count = 0;
1963 VkPipelineExecutablePropertiesKHR props[10] = {0};
1964 for (unsigned i = 0; i < ARRAY_SIZE(props); i++) {
1965 props[i].sType = VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_PROPERTIES_KHR;
1966 props[i].pNext = NULL;
1968 VKSCR(GetPipelineExecutablePropertiesKHR)(screen->dev, &pinfo, &exe_count, NULL);
1969 VKSCR(GetPipelineExecutablePropertiesKHR)(screen->dev, &pinfo, &exe_count, props);
1970 printf("PIPELINE STATISTICS:");
1971 for (unsigned e = 0; e < exe_count; e++) {
1972 VkPipelineExecutableInfoKHR info = {
1973 VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_INFO_KHR,
1979 printf("\n\t%s (%s): ", print_exe_stages(props[e].stages), props[e].name);
1980 VkPipelineExecutableStatisticKHR *stats = NULL;
1981 VKSCR(GetPipelineExecutableStatisticsKHR)(screen->dev, &info, &count, NULL);
1982 stats = calloc(count, sizeof(VkPipelineExecutableStatisticKHR));
1983 for (unsigned i = 0; i < count; i++)
1984 stats[i].sType = VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_STATISTIC_KHR;
1985 VKSCR(GetPipelineExecutableStatisticsKHR)(screen->dev, &info, &count, stats);
1987 for (unsigned i = 0; i < count; i++) {
1990 switch (stats[i].format) {
1991 case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_BOOL32_KHR:
1992 printf("%s: %u", stats[i].name, stats[i].value.b32);
1994 case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_INT64_KHR:
1995 printf("%s: %" PRIi64, stats[i].name, stats[i].value.i64);
1997 case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR:
1998 printf("%s: %" PRIu64, stats[i].name, stats[i].value.u64);
2000 case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_FLOAT64_KHR:
2001 printf("%s: %g", stats[i].name, stats[i].value.f64);
2004 unreachable("unknown statistic");
2012 precompile_job(void *data, void *gdata, int thread_index)
2014 struct zink_screen *screen = gdata;
2015 struct zink_gfx_program *prog = data;
2017 struct zink_gfx_pipeline_state state = {0};
2018 state.shader_keys_optimal.key.vs_base.last_vertex_stage = true;
2019 state.shader_keys_optimal.key.tcs.patch_vertices = 3; //random guess, generated tcs precompile is hard
2020 state.optimal_key = state.shader_keys_optimal.key.val;
2021 generate_gfx_program_modules_optimal(NULL, screen, prog, &state);
2022 zink_screen_get_pipeline_cache(screen, &prog->base, true);
2023 simple_mtx_lock(&prog->libs->lock);
2024 zink_create_pipeline_lib(screen, prog, &state);
2025 simple_mtx_unlock(&prog->libs->lock);
2026 zink_screen_update_pipeline_cache(screen, &prog->base, true);
2030 precompile_separate_shader_job(void *data, void *gdata, int thread_index)
2032 struct zink_screen *screen = gdata;
2033 struct zink_shader *zs = data;
2035 zs->precompile.mod = zink_shader_compile_separate(screen, zs);
2036 zink_descriptor_shader_init(screen, zs);
2037 VkShaderModule mods[ZINK_GFX_SHADER_COUNT] = {0};
2038 mods[zs->nir->info.stage] = zs->precompile.mod;
2039 zs->precompile.gpl = zink_create_gfx_pipeline_separate(screen, mods, zs->precompile.layout);
2043 zink_link_gfx_shader(struct pipe_context *pctx, void **shaders)
2045 struct zink_context *ctx = zink_context(pctx);
2046 struct zink_shader **zshaders = (struct zink_shader **)shaders;
2047 if (shaders[MESA_SHADER_COMPUTE])
2049 /* can't precompile fixedfunc */
2050 if (!shaders[MESA_SHADER_VERTEX] || !shaders[MESA_SHADER_FRAGMENT]) {
2051 if (shaders[MESA_SHADER_VERTEX] || shaders[MESA_SHADER_FRAGMENT]) {
2052 struct zink_shader *zs = shaders[MESA_SHADER_VERTEX] ? shaders[MESA_SHADER_VERTEX] : shaders[MESA_SHADER_FRAGMENT];
2053 if (zs->nir->info.separate_shader && !zs->precompile.mod && util_queue_fence_is_signalled(&zs->precompile.fence) &&
2054 zink_descriptor_mode == ZINK_DESCRIPTOR_MODE_DB &&
2055 /* sample shading can't precompile */
2056 (!shaders[MESA_SHADER_FRAGMENT] || !zs->nir->info.fs.uses_sample_shading))
2057 util_queue_add_job(&zink_screen(pctx->screen)->cache_get_thread, zs, &zs->precompile.fence, precompile_separate_shader_job, NULL, 0);
2062 unsigned shader_stages = 0;
2063 for (unsigned i = 0; i < ZINK_GFX_SHADER_COUNT; i++) {
2065 hash ^= zshaders[i]->hash;
2066 shader_stages |= BITFIELD_BIT(i);
2069 unsigned tess_stages = BITFIELD_BIT(MESA_SHADER_TESS_CTRL) | BITFIELD_BIT(MESA_SHADER_TESS_EVAL);
2070 unsigned tess = shader_stages & tess_stages;
2071 /* can't do fixedfunc tes either */
2072 if (tess && !shaders[MESA_SHADER_TESS_EVAL])
2074 struct hash_table *ht = &ctx->program_cache[zink_program_cache_stages(shader_stages)];
2075 simple_mtx_lock(&ctx->program_lock[zink_program_cache_stages(shader_stages)]);
2076 /* link can be called repeatedly with the same shaders: ignore */
2077 if (_mesa_hash_table_search_pre_hashed(ht, hash, shaders)) {
2078 simple_mtx_unlock(&ctx->program_lock[zink_program_cache_stages(shader_stages)]);
2081 struct zink_gfx_program *prog = zink_create_gfx_program(ctx, zshaders, 3, hash);
2082 u_foreach_bit(i, shader_stages)
2083 assert(prog->shaders[i]);
2084 _mesa_hash_table_insert_pre_hashed(ht, hash, prog->shaders, prog);
2085 prog->base.removed = false;
2086 simple_mtx_unlock(&ctx->program_lock[zink_program_cache_stages(shader_stages)]);
2087 if (zink_debug & ZINK_DEBUG_SHADERDB) {
2088 struct zink_screen *screen = zink_screen(pctx->screen);
2089 if (screen->optimal_keys)
2090 generate_gfx_program_modules_optimal(ctx, screen, prog, &ctx->gfx_pipeline_state);
2092 generate_gfx_program_modules(ctx, screen, prog, &ctx->gfx_pipeline_state);
2093 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);
2094 print_pipeline_stats(screen, pipeline);
2096 util_queue_add_job(&zink_screen(pctx->screen)->cache_get_thread, prog, &prog->base.cache_fence, precompile_job, NULL, 0);
2101 zink_program_init(struct zink_context *ctx)
2103 ctx->base.create_vs_state = zink_create_cached_shader_state;
2104 ctx->base.bind_vs_state = zink_bind_vs_state;
2105 ctx->base.delete_vs_state = zink_delete_cached_shader_state;
2107 ctx->base.create_fs_state = zink_create_cached_shader_state;
2108 ctx->base.bind_fs_state = zink_bind_fs_state;
2109 ctx->base.delete_fs_state = zink_delete_cached_shader_state;
2111 ctx->base.create_gs_state = zink_create_cached_shader_state;
2112 ctx->base.bind_gs_state = zink_bind_gs_state;
2113 ctx->base.delete_gs_state = zink_delete_cached_shader_state;
2115 ctx->base.create_tcs_state = zink_create_cached_shader_state;
2116 ctx->base.bind_tcs_state = zink_bind_tcs_state;
2117 ctx->base.delete_tcs_state = zink_delete_cached_shader_state;
2119 ctx->base.create_tes_state = zink_create_cached_shader_state;
2120 ctx->base.bind_tes_state = zink_bind_tes_state;
2121 ctx->base.delete_tes_state = zink_delete_cached_shader_state;
2123 ctx->base.create_compute_state = zink_create_cs_state;
2124 ctx->base.bind_compute_state = zink_bind_cs_state;
2125 ctx->base.delete_compute_state = zink_delete_cs_shader_state;
2127 if (zink_screen(ctx->base.screen)->info.have_EXT_vertex_input_dynamic_state)
2128 _mesa_set_init(&ctx->gfx_inputs, ctx, hash_gfx_input_dynamic, equals_gfx_input_dynamic);
2130 _mesa_set_init(&ctx->gfx_inputs, ctx, hash_gfx_input, equals_gfx_input);
2131 if (zink_screen(ctx->base.screen)->have_full_ds3)
2132 _mesa_set_init(&ctx->gfx_outputs, ctx, hash_gfx_output_ds3, equals_gfx_output_ds3);
2134 _mesa_set_init(&ctx->gfx_outputs, ctx, hash_gfx_output, equals_gfx_output);
2135 /* validate struct packing */
2136 STATIC_ASSERT(offsetof(struct zink_gfx_output_key, sample_mask) == sizeof(uint32_t));
2137 STATIC_ASSERT(offsetof(struct zink_gfx_pipeline_state, vertex_buffers_enabled_mask) - offsetof(struct zink_gfx_pipeline_state, input) ==
2138 offsetof(struct zink_gfx_input_key, vertex_buffers_enabled_mask) - offsetof(struct zink_gfx_input_key, input));
2139 STATIC_ASSERT(offsetof(struct zink_gfx_pipeline_state, vertex_strides) - offsetof(struct zink_gfx_pipeline_state, input) ==
2140 offsetof(struct zink_gfx_input_key, vertex_strides) - offsetof(struct zink_gfx_input_key, input));
2141 STATIC_ASSERT(offsetof(struct zink_gfx_pipeline_state, element_state) - offsetof(struct zink_gfx_pipeline_state, input) ==
2142 offsetof(struct zink_gfx_input_key, element_state) - offsetof(struct zink_gfx_input_key, input));
2144 STATIC_ASSERT(sizeof(union zink_shader_key_optimal) == sizeof(uint32_t));
2146 if (zink_screen(ctx->base.screen)->info.have_EXT_graphics_pipeline_library || zink_debug & ZINK_DEBUG_SHADERDB)
2147 ctx->base.link_shader = zink_link_gfx_shader;
2151 zink_set_rasterizer_discard(struct zink_context *ctx, bool disable)
2153 bool value = disable ? false : (ctx->rast_state ? ctx->rast_state->base.rasterizer_discard : false);
2154 bool changed = ctx->gfx_pipeline_state.dyn_state2.rasterizer_discard != value;
2155 ctx->gfx_pipeline_state.dyn_state2.rasterizer_discard = value;
2158 if (!zink_screen(ctx->base.screen)->info.have_EXT_extended_dynamic_state2)
2159 ctx->gfx_pipeline_state.dirty |= true;
2160 ctx->rasterizer_discard_changed = true;
2165 zink_driver_thread_add_job(struct pipe_screen *pscreen, void *data,
2166 struct util_queue_fence *fence,
2167 pipe_driver_thread_func execute,
2168 pipe_driver_thread_func cleanup,
2169 const size_t job_size)
2171 struct zink_screen *screen = zink_screen(pscreen);
2172 util_queue_add_job(&screen->cache_get_thread, data, fence, execute, cleanup, job_size);
2176 has_edge_flags(struct zink_context *ctx)
2178 switch(ctx->gfx_pipeline_state.gfx_prim_mode) {
2179 case PIPE_PRIM_POINTS:
2180 case PIPE_PRIM_LINE_STRIP:
2181 case PIPE_PRIM_LINE_STRIP_ADJACENCY:
2182 case PIPE_PRIM_LINES:
2183 case PIPE_PRIM_LINE_LOOP:
2184 case PIPE_PRIM_LINES_ADJACENCY:
2185 case PIPE_PRIM_TRIANGLE_STRIP:
2186 case PIPE_PRIM_TRIANGLE_FAN:
2187 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
2188 case PIPE_PRIM_QUAD_STRIP:
2190 case PIPE_PRIM_TRIANGLES:
2191 case PIPE_PRIM_TRIANGLES_ADJACENCY:
2192 case PIPE_PRIM_QUADS:
2193 case PIPE_PRIM_POLYGON:
2194 case PIPE_PRIM_PATCHES:
2199 return ctx->gfx_pipeline_state.rast_prim == PIPE_PRIM_LINES &&
2200 ctx->gfx_stages[MESA_SHADER_VERTEX]->has_edgeflags;
2204 zink_set_primitive_emulation_keys(struct zink_context *ctx)
2206 struct zink_screen *screen = zink_screen(ctx->base.screen);
2207 bool lower_line_stipple = ctx->gfx_pipeline_state.rast_prim == PIPE_PRIM_LINES &&
2208 screen->driver_workarounds.no_linestipple &&
2209 ctx->rast_state->base.line_stipple_enable &&
2210 !ctx->num_so_targets;
2212 bool lower_point_smooth = ctx->gfx_pipeline_state.rast_prim == PIPE_PRIM_POINTS &&
2213 screen->driconf.emulate_point_smooth &&
2214 ctx->rast_state->base.point_smooth;
2216 if (zink_get_fs_key(ctx)->lower_line_stipple != lower_line_stipple) {
2217 assert(zink_get_gs_key(ctx)->lower_line_stipple ==
2218 zink_get_fs_key(ctx)->lower_line_stipple);
2219 zink_set_fs_key(ctx)->lower_line_stipple = lower_line_stipple;
2220 zink_set_gs_key(ctx)->lower_line_stipple = lower_line_stipple;
2223 bool lower_line_smooth = ctx->gfx_pipeline_state.rast_prim == PIPE_PRIM_LINES &&
2224 screen->driver_workarounds.no_linesmooth &&
2225 ctx->rast_state->base.line_smooth &&
2226 !ctx->num_so_targets;
2228 bool lower_edge_flags = has_edge_flags(ctx);
2230 if (zink_get_fs_key(ctx)->lower_line_smooth != lower_line_smooth) {
2231 assert(zink_get_gs_key(ctx)->lower_line_smooth ==
2232 zink_get_fs_key(ctx)->lower_line_smooth);
2233 zink_set_fs_key(ctx)->lower_line_smooth = lower_line_smooth;
2234 zink_set_gs_key(ctx)->lower_line_smooth = lower_line_smooth;
2237 if (zink_get_fs_key(ctx)->lower_point_smooth != lower_point_smooth) {
2238 zink_set_fs_key(ctx)->lower_point_smooth = lower_point_smooth;
2241 if (lower_line_stipple || lower_line_smooth || lower_edge_flags ||
2242 zink_get_gs_key(ctx)->lower_gl_point) {
2243 enum pipe_shader_type prev_vertex_stage =
2244 ctx->gfx_stages[MESA_SHADER_TESS_EVAL] ?
2245 MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;
2247 if (!ctx->gfx_stages[MESA_SHADER_GEOMETRY] ||
2248 (ctx->gfx_stages[MESA_SHADER_GEOMETRY]->nir->info.gs.input_primitive != ctx->gfx_pipeline_state.gfx_prim_mode)) {
2249 assert(!screen->optimal_keys);
2251 if (!ctx->gfx_stages[prev_vertex_stage]->non_fs.generated_gs[ctx->gfx_pipeline_state.gfx_prim_mode]) {
2252 nir_shader *nir = nir_create_passthrough_gs(
2253 &screen->nir_options,
2254 ctx->gfx_stages[prev_vertex_stage]->nir,
2255 ctx->gfx_pipeline_state.gfx_prim_mode,
2259 struct zink_shader *shader = zink_shader_create(screen, nir, NULL);
2260 ctx->gfx_stages[prev_vertex_stage]->non_fs.generated_gs[ctx->gfx_pipeline_state.gfx_prim_mode] = shader;
2261 shader->non_fs.is_generated = true;
2264 bind_gfx_stage(ctx, MESA_SHADER_GEOMETRY,
2265 ctx->gfx_stages[prev_vertex_stage]->non_fs.generated_gs[ctx->gfx_pipeline_state.gfx_prim_mode]);
2267 } else if (ctx->gfx_stages[MESA_SHADER_GEOMETRY] &&
2268 ctx->gfx_stages[MESA_SHADER_GEOMETRY]->non_fs.is_generated)
2269 bind_gfx_stage(ctx, MESA_SHADER_GEOMETRY, NULL);
2273 zink_create_primitive_emulation_gs(struct zink_context *ctx)
2275 struct zink_screen *screen = zink_screen(ctx->base.screen);
2276 bool lower_edge_flags = has_edge_flags(ctx);
2278 if (lower_edge_flags) {
2279 enum pipe_shader_type prev_vertex_stage =
2280 ctx->gfx_stages[MESA_SHADER_TESS_EVAL] ?
2281 MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;
2283 if (!ctx->gfx_stages[MESA_SHADER_GEOMETRY] ||
2284 (ctx->gfx_stages[MESA_SHADER_GEOMETRY]->nir->info.gs.input_primitive != ctx->gfx_pipeline_state.gfx_prim_mode)) {
2286 if (!ctx->gfx_stages[prev_vertex_stage]->non_fs.generated_gs[ctx->gfx_pipeline_state.gfx_prim_mode]) {
2287 nir_shader *nir = nir_create_passthrough_gs(
2288 &screen->nir_options,
2289 ctx->gfx_stages[prev_vertex_stage]->nir,
2290 ctx->gfx_pipeline_state.gfx_prim_mode,
2294 struct zink_shader *shader = zink_shader_create(screen, nir, NULL);
2295 ctx->gfx_stages[prev_vertex_stage]->non_fs.generated_gs[ctx->gfx_pipeline_state.gfx_prim_mode] = shader;
2296 shader->non_fs.is_generated = true;
2299 bind_gfx_stage(ctx, MESA_SHADER_GEOMETRY,
2300 ctx->gfx_stages[prev_vertex_stage]->non_fs.generated_gs[ctx->gfx_pipeline_state.gfx_prim_mode]);
2302 } else if (ctx->gfx_stages[MESA_SHADER_GEOMETRY] &&
2303 ctx->gfx_stages[MESA_SHADER_GEOMETRY]->non_fs.is_generated)
2304 bind_gfx_stage(ctx, MESA_SHADER_GEOMETRY, NULL);