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)
69 const uint32_t nonseamless_size = zm->has_nonseamless ? sizeof(uint32_t) : 0;
70 return (!nonseamless_size || !memcmp(zm->key + zm->key_size, &key->base.nonseamless_cube_mask, nonseamless_size)) &&
71 (!num_uniforms || !memcmp(zm->key + zm->key_size + nonseamless_size,
72 key->base.inlined_uniform_values, zm->num_uniforms * sizeof(uint32_t)));
75 ALWAYS_INLINE static bool
76 shader_key_matches(const struct zink_shader_module *zm,
77 const struct zink_shader_key *key, unsigned num_uniforms,
78 bool has_inline, bool has_nonseamless)
80 const uint32_t nonseamless_size = !has_nonseamless && zm->has_nonseamless ? sizeof(uint32_t) : 0;
82 if (zm->num_uniforms != num_uniforms ||
84 memcmp(zm->key + zm->key_size + nonseamless_size,
85 key->base.inlined_uniform_values, zm->num_uniforms * sizeof(uint32_t))))
88 if (!has_nonseamless) {
89 if (zm->has_nonseamless != !!key->base.nonseamless_cube_mask ||
90 (nonseamless_size && memcmp(zm->key + zm->key_size, &key->base.nonseamless_cube_mask, nonseamless_size)))
93 return !memcmp(zm->key, key, zm->key_size);
97 shader_module_hash(const struct zink_shader_module *zm)
99 const uint32_t nonseamless_size = zm->has_nonseamless ? sizeof(uint32_t) : 0;
100 unsigned key_size = zm->key_size + nonseamless_size + zm->num_uniforms * sizeof(uint32_t);
101 return _mesa_hash_data(zm->key, key_size);
104 ALWAYS_INLINE static void
105 gather_shader_module_info(struct zink_context *ctx, struct zink_screen *screen,
106 struct zink_shader *zs, struct zink_gfx_program *prog,
107 struct zink_gfx_pipeline_state *state,
108 bool has_inline, //is inlining enabled?
109 bool has_nonseamless, //is nonseamless ext present?
110 unsigned *inline_size, unsigned *nonseamless_size)
112 gl_shader_stage stage = zs->nir->info.stage;
113 struct zink_shader_key *key = &state->shader_keys.key[stage];
114 if (has_inline && ctx && zs->nir->info.num_inlinable_uniforms &&
115 ctx->inlinable_uniforms_valid_mask & BITFIELD64_BIT(stage)) {
116 if (zs->can_inline && (screen->is_cpu || prog->inlined_variant_count[stage] < ZINK_MAX_INLINED_VARIANTS))
117 *inline_size = zs->nir->info.num_inlinable_uniforms;
119 key->inline_uniforms = false;
121 if (!has_nonseamless && key->base.nonseamless_cube_mask)
122 *nonseamless_size = sizeof(uint32_t);
125 ALWAYS_INLINE static struct zink_shader_module *
126 create_shader_module_for_stage(struct zink_context *ctx, struct zink_screen *screen,
127 struct zink_shader *zs, struct zink_gfx_program *prog,
128 gl_shader_stage stage,
129 struct zink_gfx_pipeline_state *state,
130 unsigned inline_size, unsigned nonseamless_size,
131 bool has_inline, //is inlining enabled?
132 bool has_nonseamless) //is nonseamless ext present?
135 struct zink_shader_module *zm;
136 const struct zink_shader_key *key = &state->shader_keys.key[stage];
137 /* non-generated tcs won't use the shader key */
138 const bool is_nongenerated_tcs = stage == MESA_SHADER_TESS_CTRL && !zs->non_fs.is_generated;
139 const bool shadow_needs_shader_swizzle = stage == MESA_SHADER_FRAGMENT && key->key.fs.base.shadow_needs_shader_swizzle;
140 zm = malloc(sizeof(struct zink_shader_module) + key->size +
141 (!has_nonseamless ? nonseamless_size : 0) + inline_size * sizeof(uint32_t) +
142 (shadow_needs_shader_swizzle ? sizeof(struct zink_fs_shadow_key) : 0));
146 unsigned patch_vertices = state->shader_keys.key[MESA_SHADER_TESS_CTRL ].key.tcs.patch_vertices;
147 if (stage == MESA_SHADER_TESS_CTRL && zs->non_fs.is_generated && zs->spirv) {
148 assert(ctx); //TODO async
149 mod = zink_shader_tcs_compile(screen, zs, patch_vertices);
151 mod = zink_shader_compile(screen, zs, prog->nir[stage], key, &ctx->di.shadow);
158 zm->num_uniforms = inline_size;
159 if (!is_nongenerated_tcs) {
160 zm->key_size = key->size;
161 memcpy(zm->key, key, key->size);
164 memset(zm->key, 0, key->size);
166 if (!has_nonseamless && nonseamless_size) {
167 /* nonseamless mask gets added to base key if it exists */
168 memcpy(zm->key + key->size, &key->base.nonseamless_cube_mask, nonseamless_size);
170 zm->has_nonseamless = has_nonseamless ? 0 : !!nonseamless_size;
172 memcpy(zm->key + key->size + nonseamless_size, key->base.inlined_uniform_values, inline_size * sizeof(uint32_t));
173 if (stage == MESA_SHADER_TESS_CTRL && zs->non_fs.is_generated)
174 zm->hash = patch_vertices;
176 zm->hash = shader_module_hash(zm);
177 if (unlikely(shadow_needs_shader_swizzle)) {
178 memcpy(zm->key + key->size + nonseamless_size + inline_size * sizeof(uint32_t), &ctx->di.shadow, sizeof(struct zink_fs_shadow_key));
179 zm->hash ^= _mesa_hash_data(&ctx->di.shadow, sizeof(struct zink_fs_shadow_key));
181 zm->default_variant = !inline_size && !util_dynarray_contains(&prog->shader_cache[stage][0][0], void*);
183 prog->inlined_variant_count[stage]++;
184 util_dynarray_append(&prog->shader_cache[stage][has_nonseamless ? 0 : !!nonseamless_size][!!inline_size], void*, zm);
188 ALWAYS_INLINE static struct zink_shader_module *
189 get_shader_module_for_stage(struct zink_context *ctx, struct zink_screen *screen,
190 struct zink_shader *zs, struct zink_gfx_program *prog,
191 gl_shader_stage stage,
192 struct zink_gfx_pipeline_state *state,
193 unsigned inline_size, unsigned nonseamless_size,
194 bool has_inline, //is inlining enabled?
195 bool has_nonseamless) //is nonseamless ext present?
197 const struct zink_shader_key *key = &state->shader_keys.key[stage];
198 /* non-generated tcs won't use the shader key */
199 const bool is_nongenerated_tcs = stage == MESA_SHADER_TESS_CTRL && !zs->non_fs.is_generated;
200 const bool shadow_needs_shader_swizzle = stage == MESA_SHADER_FRAGMENT && unlikely(key->key.fs.base.shadow_needs_shader_swizzle);
202 struct util_dynarray *shader_cache = &prog->shader_cache[stage][!has_nonseamless ? !!nonseamless_size : 0][has_inline ? !!inline_size : 0];
203 unsigned count = util_dynarray_num_elements(shader_cache, struct zink_shader_module *);
204 struct zink_shader_module **pzm = shader_cache->data;
205 for (unsigned i = 0; i < count; i++) {
206 struct zink_shader_module *iter = pzm[i];
207 if (is_nongenerated_tcs) {
208 if (!shader_key_matches_tcs_nongenerated(iter, key, has_inline ? !!inline_size : 0))
211 if (stage == MESA_SHADER_VERTEX && iter->key_size != key->size)
213 if (!shader_key_matches(iter, key, inline_size, has_inline, has_nonseamless))
215 if (unlikely(shadow_needs_shader_swizzle)) {
216 /* shadow swizzle data needs a manual compare since it's so fat */
217 if (memcmp(iter->key + iter->key_size + nonseamless_size + iter->num_uniforms * sizeof(uint32_t),
218 &ctx->di.shadow, sizeof(struct zink_fs_shadow_key)))
223 struct zink_shader_module *zero = pzm[0];
233 ALWAYS_INLINE static struct zink_shader_module *
234 create_shader_module_for_stage_optimal(struct zink_context *ctx, struct zink_screen *screen,
235 struct zink_shader *zs, struct zink_gfx_program *prog,
236 gl_shader_stage stage,
237 struct zink_gfx_pipeline_state *state)
240 struct zink_shader_module *zm;
242 unsigned mask = stage == MESA_SHADER_FRAGMENT ? BITFIELD_MASK(16) : BITFIELD_MASK(8);
243 bool shadow_needs_shader_swizzle = false;
244 if (zs == prog->last_vertex_stage) {
245 key = (uint16_t*)&state->shader_keys_optimal.key.vs_base;
246 } else if (stage == MESA_SHADER_FRAGMENT) {
247 key = (uint16_t*)&state->shader_keys_optimal.key.fs;
248 shadow_needs_shader_swizzle = ctx ? ctx->gfx_pipeline_state.shader_keys_optimal.key.fs.shadow_needs_shader_swizzle : false;
249 } else if (stage == MESA_SHADER_TESS_CTRL && zs->non_fs.is_generated) {
250 key = (uint16_t*)&state->shader_keys_optimal.key.tcs;
254 size_t key_size = sizeof(uint16_t);
255 zm = calloc(1, sizeof(struct zink_shader_module) + (key ? key_size : 0) + (unlikely(shadow_needs_shader_swizzle) ? sizeof(struct zink_fs_shadow_key) : 0));
259 if (stage == MESA_SHADER_TESS_CTRL && zs->non_fs.is_generated && zs->spirv) {
260 assert(ctx); //TODO async
261 struct zink_tcs_key *tcs = (struct zink_tcs_key*)key;
262 mod = zink_shader_tcs_compile(screen, zs, tcs->patch_vertices);
264 mod = zink_shader_compile(screen, zs, prog->nir[stage], (struct zink_shader_key*)key, shadow_needs_shader_swizzle ? &ctx->di.shadow : NULL);
271 /* non-generated tcs won't use the shader key */
272 const bool is_nongenerated_tcs = stage == MESA_SHADER_TESS_CTRL && !zs->non_fs.is_generated;
273 if (key && !is_nongenerated_tcs) {
274 zm->key_size = key_size;
275 uint16_t *data = (uint16_t*)zm->key;
276 /* sanitize actual key bits */
277 *data = (*key) & mask;
278 if (unlikely(shadow_needs_shader_swizzle))
279 memcpy(&data[1], &ctx->di.shadow, sizeof(struct zink_fs_shadow_key));
281 zm->default_variant = !util_dynarray_contains(&prog->shader_cache[stage][0][0], void*);
282 util_dynarray_append(&prog->shader_cache[stage][0][0], void*, zm);
286 ALWAYS_INLINE static struct zink_shader_module *
287 get_shader_module_for_stage_optimal(struct zink_context *ctx, struct zink_screen *screen,
288 struct zink_shader *zs, struct zink_gfx_program *prog,
289 gl_shader_stage stage,
290 struct zink_gfx_pipeline_state *state)
292 /* non-generated tcs won't use the shader key */
293 const bool is_nongenerated_tcs = stage == MESA_SHADER_TESS_CTRL && !zs->non_fs.is_generated;
294 bool shadow_needs_shader_swizzle = false;
296 unsigned mask = stage == MESA_SHADER_FRAGMENT ? BITFIELD_MASK(16) : BITFIELD_MASK(8);
297 if (zs == prog->last_vertex_stage) {
298 key = (uint16_t*)&ctx->gfx_pipeline_state.shader_keys_optimal.key.vs_base;
299 } else if (stage == MESA_SHADER_FRAGMENT) {
300 key = (uint16_t*)&ctx->gfx_pipeline_state.shader_keys_optimal.key.fs;
301 shadow_needs_shader_swizzle = ctx->gfx_pipeline_state.shader_keys_optimal.key.fs.shadow_needs_shader_swizzle;
302 } else if (stage == MESA_SHADER_TESS_CTRL && zs->non_fs.is_generated) {
303 key = (uint16_t*)&ctx->gfx_pipeline_state.shader_keys_optimal.key.tcs;
307 struct util_dynarray *shader_cache = &prog->shader_cache[stage][0][0];
308 unsigned count = util_dynarray_num_elements(shader_cache, struct zink_shader_module *);
309 struct zink_shader_module **pzm = shader_cache->data;
310 for (unsigned i = 0; i < count; i++) {
311 struct zink_shader_module *iter = pzm[i];
312 if (is_nongenerated_tcs) {
315 uint16_t val = (*key) & mask;
316 /* no key is bigger than uint16_t */
317 if (memcmp(iter->key, &val, sizeof(uint16_t)))
319 if (unlikely(shadow_needs_shader_swizzle)) {
320 /* shadow swizzle data needs a manual compare since it's so fat */
321 if (memcmp(iter->key + sizeof(uint16_t), &ctx->di.shadow, sizeof(struct zink_fs_shadow_key)))
326 struct zink_shader_module *zero = pzm[0];
337 zink_destroy_shader_module(struct zink_screen *screen, struct zink_shader_module *zm)
339 VKSCR(DestroyShaderModule)(screen->dev, zm->shader, NULL);
344 destroy_shader_cache(struct zink_screen *screen, struct util_dynarray *sc)
346 while (util_dynarray_contains(sc, void*)) {
347 struct zink_shader_module *zm = util_dynarray_pop(sc, struct zink_shader_module*);
348 zink_destroy_shader_module(screen, zm);
352 ALWAYS_INLINE static void
353 update_gfx_shader_modules(struct zink_context *ctx,
354 struct zink_screen *screen,
355 struct zink_gfx_program *prog, uint32_t mask,
356 struct zink_gfx_pipeline_state *state,
357 bool has_inline, //is inlining enabled?
358 bool has_nonseamless) //is nonseamless ext present?
360 bool hash_changed = false;
361 bool default_variants = true;
362 assert(prog->modules[MESA_SHADER_VERTEX]);
363 uint32_t variant_hash = prog->last_variant_hash;
364 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
365 if (!(mask & BITFIELD_BIT(i)))
368 assert(prog->shaders[i]);
370 unsigned inline_size = 0, nonseamless_size = 0;
371 gather_shader_module_info(ctx, screen, prog->shaders[i], prog, state, has_inline, has_nonseamless, &inline_size, &nonseamless_size);
372 struct zink_shader_module *zm = get_shader_module_for_stage(ctx, screen, prog->shaders[i], prog, i, state,
373 inline_size, nonseamless_size, has_inline, has_nonseamless);
375 zm = create_shader_module_for_stage(ctx, screen, prog->shaders[i], prog, i, state,
376 inline_size, nonseamless_size, has_inline, has_nonseamless);
377 state->modules[i] = zm->shader;
378 if (prog->modules[i] == zm->shader)
380 variant_hash ^= prog->module_hash[i];
382 default_variants &= zm->default_variant;
383 prog->modules[i] = zm->shader;
384 prog->module_hash[i] = zm->hash;
386 if (zm->num_uniforms)
387 prog->inline_variants |= BITFIELD_BIT(i);
389 prog->inline_variants &= ~BITFIELD_BIT(i);
391 variant_hash ^= prog->module_hash[i];
394 if (hash_changed && state) {
395 if (default_variants)
396 prog->last_variant_hash = prog->default_variant_hash;
398 prog->last_variant_hash = variant_hash;
400 state->modules_changed = true;
405 generate_gfx_program_modules(struct zink_context *ctx, struct zink_screen *screen, struct zink_gfx_program *prog, struct zink_gfx_pipeline_state *state)
407 assert(!prog->modules[MESA_SHADER_VERTEX]);
408 uint32_t variant_hash = 0;
409 bool default_variants = true;
410 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
411 if (!(prog->stages_present & BITFIELD_BIT(i)))
414 assert(prog->shaders[i]);
416 unsigned inline_size = 0, nonseamless_size = 0;
417 gather_shader_module_info(ctx, screen, prog->shaders[i], prog, state,
418 screen->driconf.inline_uniforms, screen->info.have_EXT_non_seamless_cube_map,
419 &inline_size, &nonseamless_size);
420 struct zink_shader_module *zm = create_shader_module_for_stage(ctx, screen, prog->shaders[i], prog, i, state,
421 inline_size, nonseamless_size,
422 screen->driconf.inline_uniforms, screen->info.have_EXT_non_seamless_cube_map);
423 state->modules[i] = zm->shader;
424 prog->modules[i] = zm->shader;
425 prog->module_hash[i] = zm->hash;
426 if (zm->num_uniforms)
427 prog->inline_variants |= BITFIELD_BIT(i);
428 default_variants &= zm->default_variant;
429 variant_hash ^= prog->module_hash[i];
432 p_atomic_dec(&prog->base.reference.count);
433 state->modules_changed = true;
435 prog->last_variant_hash = variant_hash;
436 if (default_variants)
437 prog->default_variant_hash = prog->last_variant_hash;
441 generate_gfx_program_modules_optimal(struct zink_context *ctx, struct zink_screen *screen, struct zink_gfx_program *prog, struct zink_gfx_pipeline_state *state)
443 assert(!prog->modules[MESA_SHADER_VERTEX]);
444 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
445 if (!(prog->stages_present & BITFIELD_BIT(i)))
448 assert(prog->shaders[i]);
450 struct zink_shader_module *zm = create_shader_module_for_stage_optimal(ctx, screen, prog->shaders[i], prog, i, state);
451 prog->modules[i] = zm->shader;
454 p_atomic_dec(&prog->base.reference.count);
455 state->modules_changed = true;
456 prog->last_variant_hash = state->shader_keys_optimal.key.val;
460 hash_pipeline_lib_generated_tcs(const void *key)
462 const struct zink_gfx_library_key *gkey = key;
463 return gkey->optimal_key;
468 equals_pipeline_lib_generated_tcs(const void *a, const void *b)
470 return !memcmp(a, b, sizeof(uint32_t));
474 hash_pipeline_lib(const void *key)
476 const struct zink_gfx_library_key *gkey = key;
477 /* remove generated tcs bits */
478 return zink_shader_key_optimal_no_tcs(gkey->optimal_key);
482 equals_pipeline_lib(const void *a, const void *b)
484 const struct zink_gfx_library_key *ak = a;
485 const struct zink_gfx_library_key *bk = b;
486 /* remove generated tcs bits */
487 uint32_t val_a = zink_shader_key_optimal_no_tcs(ak->optimal_key);
488 uint32_t val_b = zink_shader_key_optimal_no_tcs(bk->optimal_key);
489 return val_a == val_b;
493 hash_gfx_input_dynamic(const void *key)
495 const struct zink_gfx_input_key *ikey = key;
500 equals_gfx_input_dynamic(const void *a, const void *b)
502 const struct zink_gfx_input_key *ikey_a = a;
503 const struct zink_gfx_input_key *ikey_b = b;
504 return ikey_a->idx == ikey_b->idx;
508 hash_gfx_input(const void *key)
510 const struct zink_gfx_input_key *ikey = key;
511 if (ikey->uses_dynamic_stride)
513 return _mesa_hash_data(key, offsetof(struct zink_gfx_input_key, pipeline));
517 equals_gfx_input(const void *a, const void *b)
519 const struct zink_gfx_input_key *ikey_a = a;
520 const struct zink_gfx_input_key *ikey_b = b;
521 if (ikey_a->uses_dynamic_stride)
522 return ikey_a->element_state == ikey_b->element_state &&
523 !memcmp(a, b, offsetof(struct zink_gfx_input_key, vertex_buffers_enabled_mask));
524 return !memcmp(a, b, offsetof(struct zink_gfx_input_key, pipeline));
528 hash_gfx_output_ds3(const void *key)
530 const uint8_t *data = key;
531 return _mesa_hash_data(data, sizeof(uint32_t));
535 equals_gfx_output_ds3(const void *a, const void *b)
537 const uint8_t *da = a;
538 const uint8_t *db = b;
539 return !memcmp(da, db, sizeof(uint32_t));
543 hash_gfx_output(const void *key)
545 const uint8_t *data = key;
546 return _mesa_hash_data(data, offsetof(struct zink_gfx_output_key, pipeline));
550 equals_gfx_output(const void *a, const void *b)
552 const uint8_t *da = a;
553 const uint8_t *db = b;
554 return !memcmp(da, db, offsetof(struct zink_gfx_output_key, pipeline));
557 ALWAYS_INLINE static void
558 update_gfx_program_nonseamless(struct zink_context *ctx, struct zink_gfx_program *prog, bool has_nonseamless)
560 struct zink_screen *screen = zink_screen(ctx->base.screen);
561 if (screen->driconf.inline_uniforms)
562 update_gfx_shader_modules(ctx, screen, prog,
563 ctx->dirty_gfx_stages & prog->stages_present, &ctx->gfx_pipeline_state,
564 true, has_nonseamless);
566 update_gfx_shader_modules(ctx, screen, prog,
567 ctx->dirty_gfx_stages & prog->stages_present, &ctx->gfx_pipeline_state,
568 false, has_nonseamless);
572 update_gfx_program(struct zink_context *ctx, struct zink_gfx_program *prog)
574 struct zink_screen *screen = zink_screen(ctx->base.screen);
575 if (screen->info.have_EXT_non_seamless_cube_map)
576 update_gfx_program_nonseamless(ctx, prog, true);
578 update_gfx_program_nonseamless(ctx, prog, false);
582 zink_gfx_program_update(struct zink_context *ctx)
584 if (ctx->last_vertex_stage_dirty) {
585 gl_shader_stage pstage = ctx->last_vertex_stage->nir->info.stage;
586 ctx->dirty_gfx_stages |= BITFIELD_BIT(pstage);
587 memcpy(&ctx->gfx_pipeline_state.shader_keys.key[pstage].key.vs_base,
588 &ctx->gfx_pipeline_state.shader_keys.last_vertex.key.vs_base,
589 sizeof(struct zink_vs_key_base));
590 ctx->last_vertex_stage_dirty = false;
592 if (ctx->gfx_dirty) {
593 struct zink_gfx_program *prog = NULL;
595 simple_mtx_lock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
596 struct hash_table *ht = &ctx->program_cache[zink_program_cache_stages(ctx->shader_stages)];
597 const uint32_t hash = ctx->gfx_hash;
598 struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(ht, hash, ctx->gfx_stages);
600 prog = (struct zink_gfx_program*)entry->data;
601 for (unsigned i = 0; i < ZINK_GFX_SHADER_COUNT; i++) {
602 if (prog->stages_present & ~ctx->dirty_gfx_stages & BITFIELD_BIT(i))
603 ctx->gfx_pipeline_state.modules[i] = prog->modules[i];
605 /* ensure variants are always updated if keys have changed since last use */
606 ctx->dirty_gfx_stages |= prog->stages_present;
607 update_gfx_program(ctx, prog);
609 ctx->dirty_gfx_stages |= ctx->shader_stages;
610 prog = zink_create_gfx_program(ctx, ctx->gfx_stages, ctx->gfx_pipeline_state.dyn_state2.vertices_per_patch, hash);
611 zink_screen_get_pipeline_cache(zink_screen(ctx->base.screen), &prog->base, false);
612 _mesa_hash_table_insert_pre_hashed(ht, hash, prog->shaders, prog);
613 prog->base.removed = false;
614 generate_gfx_program_modules(ctx, zink_screen(ctx->base.screen), prog, &ctx->gfx_pipeline_state);
616 simple_mtx_unlock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
617 if (prog && prog != ctx->curr_program)
618 zink_batch_reference_program(&ctx->batch, &prog->base);
619 if (ctx->curr_program)
620 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
621 ctx->curr_program = prog;
622 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
623 ctx->gfx_dirty = false;
624 } else if (ctx->dirty_gfx_stages) {
625 /* remove old hash */
626 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
627 update_gfx_program(ctx, ctx->curr_program);
629 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
631 ctx->dirty_gfx_stages = 0;
634 ALWAYS_INLINE static bool
635 update_gfx_shader_module_optimal(struct zink_context *ctx, struct zink_gfx_program *prog, gl_shader_stage pstage)
637 struct zink_screen *screen = zink_screen(ctx->base.screen);
638 if (screen->info.have_EXT_graphics_pipeline_library)
639 util_queue_fence_wait(&prog->base.cache_fence);
640 struct zink_shader_module *zm = get_shader_module_for_stage_optimal(ctx, screen, prog->shaders[pstage], prog, pstage, &ctx->gfx_pipeline_state);
642 zm = create_shader_module_for_stage_optimal(ctx, screen, prog->shaders[pstage], prog, pstage, &ctx->gfx_pipeline_state);
644 bool changed = prog->modules[pstage] != zm->shader;
645 prog->modules[pstage] = zm->shader;
650 update_gfx_program_optimal(struct zink_context *ctx, struct zink_gfx_program *prog)
652 const union zink_shader_key_optimal *optimal_key = (union zink_shader_key_optimal*)&prog->last_variant_hash;
653 if (ctx->gfx_pipeline_state.shader_keys_optimal.key.vs_bits != optimal_key->vs_bits) {
654 assert(!prog->is_separable);
655 bool changed = update_gfx_shader_module_optimal(ctx, prog, ctx->last_vertex_stage->nir->info.stage);
656 ctx->gfx_pipeline_state.modules_changed |= changed;
658 const bool shadow_needs_shader_swizzle = optimal_key->fs.shadow_needs_shader_swizzle && (ctx->dirty_gfx_stages & BITFIELD_BIT(MESA_SHADER_FRAGMENT));
659 if (ctx->gfx_pipeline_state.shader_keys_optimal.key.fs_bits != optimal_key->fs_bits ||
660 /* always recheck shadow swizzles since they aren't directly part of the key */
661 unlikely(shadow_needs_shader_swizzle)) {
662 assert(!prog->is_separable);
663 bool changed = update_gfx_shader_module_optimal(ctx, prog, MESA_SHADER_FRAGMENT);
664 ctx->gfx_pipeline_state.modules_changed |= changed;
665 if (unlikely(shadow_needs_shader_swizzle)) {
666 struct zink_shader_module **pzm = prog->shader_cache[MESA_SHADER_FRAGMENT][0][0].data;
667 ctx->gfx_pipeline_state.shadow = (struct zink_fs_shadow_key*)pzm[0]->key + sizeof(uint16_t);
670 if (prog->shaders[MESA_SHADER_TESS_CTRL] && prog->shaders[MESA_SHADER_TESS_CTRL]->non_fs.is_generated &&
671 ctx->gfx_pipeline_state.shader_keys_optimal.key.tcs_bits != optimal_key->tcs_bits) {
672 assert(!prog->is_separable);
673 bool changed = update_gfx_shader_module_optimal(ctx, prog, MESA_SHADER_TESS_CTRL);
674 ctx->gfx_pipeline_state.modules_changed |= changed;
676 prog->last_variant_hash = ctx->gfx_pipeline_state.shader_keys_optimal.key.val;
680 zink_gfx_program_update_optimal(struct zink_context *ctx)
682 if (ctx->gfx_dirty) {
683 struct zink_gfx_program *prog = NULL;
684 ctx->gfx_pipeline_state.optimal_key = ctx->gfx_pipeline_state.shader_keys_optimal.key.val;
685 struct hash_table *ht = &ctx->program_cache[zink_program_cache_stages(ctx->shader_stages)];
686 const uint32_t hash = ctx->gfx_hash;
687 simple_mtx_lock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
688 struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(ht, hash, ctx->gfx_stages);
690 if (ctx->curr_program)
691 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
693 prog = (struct zink_gfx_program*)entry->data;
694 if (prog->is_separable) {
695 /* shader variants can't be handled by separable programs: sync and compile */
696 if (!ZINK_SHADER_KEY_OPTIMAL_IS_DEFAULT(ctx->gfx_pipeline_state.optimal_key))
697 util_queue_fence_wait(&prog->base.cache_fence);
698 /* If the optimized linked pipeline is done compiling, swap it into place. */
699 if (util_queue_fence_is_signalled(&prog->base.cache_fence)) {
700 struct zink_gfx_program *real = prog->full_prog;
702 entry->key = real->shaders;
703 real->base.removed = false;
704 prog->full_prog = NULL;
705 prog->base.removed = true;
706 zink_gfx_program_reference(zink_screen(ctx->base.screen), &prog, NULL);
710 update_gfx_program_optimal(ctx, prog);
712 ctx->dirty_gfx_stages |= ctx->shader_stages;
713 prog = create_gfx_program_separable(ctx, ctx->gfx_stages, ctx->gfx_pipeline_state.dyn_state2.vertices_per_patch);
714 prog->base.removed = false;
715 _mesa_hash_table_insert_pre_hashed(ht, hash, prog->shaders, prog);
716 if (!prog->is_separable) {
717 zink_screen_get_pipeline_cache(zink_screen(ctx->base.screen), &prog->base, false);
718 generate_gfx_program_modules_optimal(ctx, zink_screen(ctx->base.screen), prog, &ctx->gfx_pipeline_state);
721 simple_mtx_unlock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
722 if (prog && prog != ctx->curr_program)
723 zink_batch_reference_program(&ctx->batch, &prog->base);
724 ctx->curr_program = prog;
725 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
726 } else if (ctx->dirty_gfx_stages) {
727 /* remove old hash */
728 ctx->gfx_pipeline_state.optimal_key = ctx->gfx_pipeline_state.shader_keys_optimal.key.val;
729 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
730 if (ctx->curr_program->is_separable) {
731 struct zink_gfx_program *prog = ctx->curr_program;
732 if (prog->is_separable && !ZINK_SHADER_KEY_OPTIMAL_IS_DEFAULT(ctx->gfx_pipeline_state.optimal_key)) {
733 util_queue_fence_wait(&prog->base.cache_fence);
734 /* shader variants can't be handled by separable programs: sync and compile */
735 struct hash_table *ht = &ctx->program_cache[zink_program_cache_stages(ctx->shader_stages)];
736 const uint32_t hash = ctx->gfx_hash;
737 simple_mtx_lock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
738 struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(ht, hash, ctx->gfx_stages);
739 struct zink_gfx_program *real = prog->full_prog;
741 entry->key = real->shaders;
742 real->base.removed = false;
743 prog->full_prog = NULL;
744 prog->base.removed = true;
745 zink_gfx_program_reference(zink_screen(ctx->base.screen), &prog, NULL);
746 ctx->curr_program = real;
747 simple_mtx_unlock(&ctx->program_lock[zink_program_cache_stages(ctx->shader_stages)]);
750 update_gfx_program_optimal(ctx, ctx->curr_program);
752 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
754 ctx->dirty_gfx_stages = 0;
755 ctx->gfx_dirty = false;
756 ctx->last_vertex_stage_dirty = false;
760 optimized_compile_job(void *data, void *gdata, int thread_index)
762 struct zink_gfx_pipeline_cache_entry *pc_entry = data;
763 struct zink_screen *screen = gdata;
766 pipeline = zink_create_gfx_pipeline_combined(screen, pc_entry->prog, pc_entry->ikey->pipeline, &pc_entry->gkey->pipeline, 1, pc_entry->okey->pipeline, true);
768 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);
770 pc_entry->unoptimized_pipeline = pc_entry->pipeline;
771 pc_entry->pipeline = pipeline;
776 zink_gfx_program_compile_queue(struct zink_context *ctx, struct zink_gfx_pipeline_cache_entry *pc_entry)
778 util_queue_add_job(&zink_screen(ctx->base.screen)->cache_get_thread, pc_entry, &pc_entry->fence, optimized_compile_job, NULL, 0);
782 update_cs_shader_module(struct zink_context *ctx, struct zink_compute_program *comp)
784 struct zink_screen *screen = zink_screen(ctx->base.screen);
785 struct zink_shader *zs = comp->shader;
787 struct zink_shader_module *zm = NULL;
788 unsigned inline_size = 0, nonseamless_size = 0;
789 struct zink_shader_key *key = &ctx->compute_pipeline_state.key;
790 ASSERTED bool check_robustness = screen->driver_workarounds.lower_robustImageAccess2 && (ctx->flags & PIPE_CONTEXT_ROBUST_BUFFER_ACCESS);
791 assert(zink_cs_key(key)->robust_access == check_robustness);
793 if (ctx && zs->nir->info.num_inlinable_uniforms &&
794 ctx->inlinable_uniforms_valid_mask & BITFIELD64_BIT(MESA_SHADER_COMPUTE)) {
795 if (screen->is_cpu || comp->inlined_variant_count < ZINK_MAX_INLINED_VARIANTS)
796 inline_size = zs->nir->info.num_inlinable_uniforms;
798 key->inline_uniforms = false;
800 if (key->base.nonseamless_cube_mask)
801 nonseamless_size = sizeof(uint32_t);
803 if (inline_size || nonseamless_size || zink_cs_key(key)->robust_access) {
804 struct util_dynarray *shader_cache = &comp->shader_cache[!!nonseamless_size];
805 unsigned count = util_dynarray_num_elements(shader_cache, struct zink_shader_module *);
806 struct zink_shader_module **pzm = shader_cache->data;
807 for (unsigned i = 0; i < count; i++) {
808 struct zink_shader_module *iter = pzm[i];
809 if (!shader_key_matches(iter, key, inline_size,
810 screen->driconf.inline_uniforms,
811 screen->info.have_EXT_non_seamless_cube_map))
814 struct zink_shader_module *zero = pzm[0];
825 zm = malloc(sizeof(struct zink_shader_module) + nonseamless_size + inline_size * sizeof(uint32_t));
829 mod = zink_shader_compile(screen, zs, comp->shader->nir, key, NULL);
835 zm->num_uniforms = inline_size;
836 zm->key_size = key->size;
837 memcpy(zm->key, key, key->size);
838 zm->has_nonseamless = !!nonseamless_size;
839 assert(nonseamless_size || inline_size || zink_cs_key(key)->robust_access);
840 if (nonseamless_size)
841 memcpy(zm->key + zm->key_size, &key->base.nonseamless_cube_mask, nonseamless_size);
843 memcpy(zm->key + zm->key_size + nonseamless_size, key->base.inlined_uniform_values, inline_size * sizeof(uint32_t));
844 zm->hash = shader_module_hash(zm);
845 zm->default_variant = false;
847 comp->inlined_variant_count++;
849 /* this is otherwise the default variant, which is stored as comp->module */
850 if (zm->num_uniforms || nonseamless_size || zink_cs_key(key)->robust_access)
851 util_dynarray_append(&comp->shader_cache[!!nonseamless_size], void*, zm);
853 if (comp->curr == zm)
855 ctx->compute_pipeline_state.final_hash ^= ctx->compute_pipeline_state.module_hash;
857 ctx->compute_pipeline_state.module_hash = zm->hash;
858 ctx->compute_pipeline_state.final_hash ^= ctx->compute_pipeline_state.module_hash;
859 ctx->compute_pipeline_state.module_changed = true;
863 zink_update_compute_program(struct zink_context *ctx)
865 util_queue_fence_wait(&ctx->curr_compute->base.cache_fence);
866 update_cs_shader_module(ctx, ctx->curr_compute);
870 zink_pipeline_layout_create(struct zink_screen *screen, VkDescriptorSetLayout *dsl, unsigned num_dsl, bool is_compute, VkPipelineLayoutCreateFlags flags)
872 VkPipelineLayoutCreateInfo plci = {0};
873 plci.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
876 plci.pSetLayouts = dsl;
877 plci.setLayoutCount = num_dsl;
879 VkPushConstantRange pcr;
881 pcr.stageFlags = VK_SHADER_STAGE_ALL_GRAPHICS;
883 pcr.size = sizeof(struct zink_gfx_push_constant);
884 plci.pushConstantRangeCount = 1;
885 plci.pPushConstantRanges = &pcr;
888 VkPipelineLayout layout;
889 VkResult result = VKSCR(CreatePipelineLayout)(screen->dev, &plci, NULL, &layout);
890 if (result != VK_SUCCESS) {
891 mesa_loge("vkCreatePipelineLayout failed (%s)", vk_Result_to_str(result));
892 return VK_NULL_HANDLE;
899 create_program(struct zink_context *ctx, bool is_compute)
901 struct zink_program *pg = rzalloc_size(NULL, is_compute ? sizeof(struct zink_compute_program) : sizeof(struct zink_gfx_program));
905 pipe_reference_init(&pg->reference, 1);
906 util_queue_fence_init(&pg->cache_fence);
907 pg->is_compute = is_compute;
913 assign_io(struct zink_screen *screen,
914 struct zink_gfx_program *prog,
915 struct zink_shader *stages[ZINK_GFX_SHADER_COUNT])
917 struct zink_shader *shaders[MESA_SHADER_STAGES];
919 /* build array in pipeline order */
920 for (unsigned i = 0; i < ZINK_GFX_SHADER_COUNT; i++)
921 shaders[i] = stages[i];
923 for (unsigned i = 0; i < MESA_SHADER_FRAGMENT;) {
924 nir_shader *producer = shaders[i]->nir;
925 for (unsigned j = i + 1; j < ZINK_GFX_SHADER_COUNT; i++, j++) {
926 struct zink_shader *consumer = shaders[j];
929 if (!prog->nir[producer->info.stage])
930 prog->nir[producer->info.stage] = nir_shader_clone(prog, producer);
932 prog->nir[j] = nir_shader_clone(prog, consumer->nir);
933 zink_compiler_assign_io(screen, prog->nir[producer->info.stage], prog->nir[j]);
941 zink_gfx_lib_cache_unref(struct zink_screen *screen, struct zink_gfx_lib_cache *libs)
943 if (!p_atomic_dec_zero(&libs->refcount))
946 simple_mtx_destroy(&libs->lock);
947 set_foreach_remove(&libs->libs, he) {
948 struct zink_gfx_library_key *gkey = (void*)he->key;
949 VKSCR(DestroyPipeline)(screen->dev, gkey->pipeline, NULL);
955 static struct zink_gfx_lib_cache *
956 create_lib_cache(struct zink_gfx_program *prog, bool generated_tcs)
958 struct zink_gfx_lib_cache *libs = rzalloc(NULL, struct zink_gfx_lib_cache);
959 simple_mtx_init(&libs->lock, mtx_plain);
961 _mesa_set_init(&libs->libs, libs, hash_pipeline_lib_generated_tcs, equals_pipeline_lib_generated_tcs);
963 _mesa_set_init(&libs->libs, libs, hash_pipeline_lib, equals_pipeline_lib);
967 static struct zink_gfx_lib_cache *
968 find_or_create_lib_cache(struct zink_screen *screen, struct zink_gfx_program *prog)
970 unsigned stages_present = prog->stages_present;
971 bool generated_tcs = prog->shaders[MESA_SHADER_TESS_CTRL] && prog->shaders[MESA_SHADER_TESS_CTRL]->non_fs.is_generated;
973 stages_present &= ~BITFIELD_BIT(MESA_SHADER_TESS_CTRL);
974 unsigned idx = zink_program_cache_stages(stages_present);
975 struct set *ht = &screen->pipeline_libs[idx];
976 const uint32_t hash = prog->gfx_hash;
978 simple_mtx_lock(&screen->pipeline_libs_lock[idx]);
980 struct set_entry *entry = _mesa_set_search_or_add_pre_hashed(ht, hash, prog->shaders, &found);
981 struct zink_gfx_lib_cache *libs;
983 libs = (void*)entry->key;
985 libs = create_lib_cache(prog, generated_tcs);
986 memcpy(libs->shaders, prog->shaders, sizeof(prog->shaders));
989 for (unsigned i = 0; i < MESA_SHADER_COMPUTE; i++) {
990 if (prog->shaders[i] && (!generated_tcs || i != MESA_SHADER_TESS_CTRL)) {
991 simple_mtx_lock(&prog->shaders[i]->lock);
992 util_dynarray_append(&prog->shaders[i]->pipeline_libs, struct zink_gfx_lib_cache*, libs);
993 simple_mtx_unlock(&prog->shaders[i]->lock);
997 p_atomic_set(&libs->refcount, refs);
999 simple_mtx_unlock(&screen->pipeline_libs_lock[idx]);
1003 struct zink_gfx_program *
1004 zink_create_gfx_program(struct zink_context *ctx,
1005 struct zink_shader **stages,
1006 unsigned vertices_per_patch,
1009 struct zink_screen *screen = zink_screen(ctx->base.screen);
1010 struct zink_gfx_program *prog = create_program(ctx, false);
1015 prog->gfx_hash = gfx_hash;
1016 prog->base.removed = true;
1018 for (int i = 0; i < ZINK_GFX_SHADER_COUNT; ++i) {
1019 util_dynarray_init(&prog->shader_cache[i][0][0], prog);
1020 util_dynarray_init(&prog->shader_cache[i][0][1], prog);
1021 util_dynarray_init(&prog->shader_cache[i][1][0], prog);
1022 util_dynarray_init(&prog->shader_cache[i][1][1], prog);
1024 prog->shaders[i] = stages[i];
1025 prog->stages_present |= BITFIELD_BIT(i);
1028 if (stages[MESA_SHADER_TESS_EVAL] && !stages[MESA_SHADER_TESS_CTRL]) {
1029 prog->shaders[MESA_SHADER_TESS_EVAL]->non_fs.generated_tcs =
1030 prog->shaders[MESA_SHADER_TESS_CTRL] =
1031 zink_shader_tcs_create(screen, stages[MESA_SHADER_VERTEX], vertices_per_patch);
1032 prog->stages_present |= BITFIELD_BIT(MESA_SHADER_TESS_CTRL);
1034 prog->stages_remaining = prog->stages_present;
1036 assign_io(screen, prog, prog->shaders);
1038 if (stages[MESA_SHADER_GEOMETRY])
1039 prog->last_vertex_stage = stages[MESA_SHADER_GEOMETRY];
1040 else if (stages[MESA_SHADER_TESS_EVAL])
1041 prog->last_vertex_stage = stages[MESA_SHADER_TESS_EVAL];
1043 prog->last_vertex_stage = stages[MESA_SHADER_VERTEX];
1045 for (int r = 0; r < ARRAY_SIZE(prog->pipelines); ++r) {
1046 for (int i = 0; i < ARRAY_SIZE(prog->pipelines[0]); ++i) {
1047 _mesa_hash_table_init(&prog->pipelines[r][i], prog, NULL, zink_get_gfx_pipeline_eq_func(screen, prog));
1048 /* only need first 3/4 for point/line/tri/patch */
1049 if (screen->info.have_EXT_extended_dynamic_state &&
1050 i == (prog->last_vertex_stage->nir->info.stage == MESA_SHADER_TESS_EVAL ? 4 : 3))
1055 if (screen->optimal_keys)
1056 prog->libs = find_or_create_lib_cache(screen, prog);
1058 struct mesa_sha1 sctx;
1059 _mesa_sha1_init(&sctx);
1060 for (int i = 0; i < ZINK_GFX_SHADER_COUNT; ++i) {
1061 if (prog->shaders[i]) {
1062 simple_mtx_lock(&prog->shaders[i]->lock);
1063 _mesa_set_add(prog->shaders[i]->programs, prog);
1064 simple_mtx_unlock(&prog->shaders[i]->lock);
1065 zink_gfx_program_reference(screen, NULL, prog);
1066 _mesa_sha1_update(&sctx, prog->shaders[i]->base.sha1, sizeof(prog->shaders[i]->base.sha1));
1069 _mesa_sha1_final(&sctx, prog->base.sha1);
1071 if (!zink_descriptor_program_init(ctx, &prog->base))
1078 zink_destroy_gfx_program(screen, prog);
1082 /* Creates a replacement, optimized zink_gfx_program for this set of separate shaders, which will
1083 * be swapped in in place of the fast-linked separable program once it's done compiling.
1086 create_linked_separable_job(void *data, void *gdata, int thread_index)
1088 struct zink_gfx_program *prog = data;
1089 prog->full_prog = zink_create_gfx_program(prog->ctx, prog->shaders, 0, prog->gfx_hash);
1090 precompile_job(prog->full_prog, gdata, thread_index);
1093 struct zink_gfx_program *
1094 create_gfx_program_separable(struct zink_context *ctx, struct zink_shader **stages, unsigned vertices_per_patch)
1096 struct zink_screen *screen = zink_screen(ctx->base.screen);
1097 unsigned shader_stages = BITFIELD_BIT(MESA_SHADER_VERTEX) | BITFIELD_BIT(MESA_SHADER_FRAGMENT);
1098 /* filter cases that need real pipelines */
1099 if (ctx->shader_stages != shader_stages ||
1100 !stages[MESA_SHADER_VERTEX]->precompile.mod || !stages[MESA_SHADER_FRAGMENT]->precompile.mod ||
1101 /* TODO: maybe try variants? grimace */
1102 !ZINK_SHADER_KEY_OPTIMAL_IS_DEFAULT(ctx->gfx_pipeline_state.optimal_key) ||
1103 !zink_can_use_pipeline_libs(ctx))
1104 return zink_create_gfx_program(ctx, stages, vertices_per_patch, ctx->gfx_hash);
1105 /* ensure async gpl creation is done */
1106 util_queue_fence_wait(&stages[MESA_SHADER_VERTEX]->precompile.fence);
1107 util_queue_fence_wait(&stages[MESA_SHADER_FRAGMENT]->precompile.fence);
1109 struct zink_gfx_program *prog = create_program(ctx, false);
1114 prog->is_separable = true;
1115 prog->gfx_hash = ctx->gfx_hash;
1117 prog->shaders[MESA_SHADER_VERTEX] = stages[MESA_SHADER_VERTEX];
1118 prog->stages_remaining = prog->stages_present = shader_stages;
1119 prog->shaders[MESA_SHADER_FRAGMENT] = stages[MESA_SHADER_FRAGMENT];
1120 prog->last_vertex_stage = stages[MESA_SHADER_VERTEX];
1121 prog->libs = create_lib_cache(prog, false);
1122 /* this libs cache is owned by the program */
1123 p_atomic_set(&prog->libs->refcount, 1);
1126 for (int i = 0; i < ZINK_GFX_SHADER_COUNT; ++i) {
1127 if (prog->shaders[i]) {
1128 simple_mtx_lock(&prog->shaders[i]->lock);
1129 _mesa_set_add(prog->shaders[i]->programs, prog);
1130 simple_mtx_unlock(&prog->shaders[i]->lock);
1134 /* We can do this add after the _mesa_set_adds above because we know the prog->shaders[] are
1135 * referenced by the draw state and zink_shader_free() can't be called on them while we're in here.
1137 p_atomic_add(&prog->base.reference.count, refs);
1139 for (int r = 0; r < ARRAY_SIZE(prog->pipelines); ++r) {
1140 for (int i = 0; i < ARRAY_SIZE(prog->pipelines[0]); ++i) {
1141 _mesa_hash_table_init(&prog->pipelines[r][i], prog, NULL, zink_get_gfx_pipeline_eq_func(screen, prog));
1142 /* only need first 3/4 for point/line/tri/patch */
1143 if (screen->info.have_EXT_extended_dynamic_state &&
1144 i == (prog->last_vertex_stage->nir->info.stage == MESA_SHADER_TESS_EVAL ? 4 : 3))
1149 if (prog->shaders[MESA_SHADER_VERTEX]->precompile.dsl) {
1150 prog->base.dd.binding_usage |= BITFIELD_BIT(0);
1151 prog->base.dsl[prog->base.num_dsl] = prog->shaders[MESA_SHADER_VERTEX]->precompile.dsl;
1152 prog->base.num_dsl++;
1154 if (prog->shaders[MESA_SHADER_FRAGMENT]->precompile.dsl) {
1155 prog->base.dd.binding_usage |= BITFIELD_BIT(1);
1156 prog->base.dsl[prog->base.num_dsl] = prog->shaders[MESA_SHADER_FRAGMENT]->precompile.dsl;
1157 /* guarantee a null dsl if vs doesn't have descriptors */
1158 prog->base.num_dsl = 2;
1160 prog->base.dd.bindless = prog->shaders[MESA_SHADER_VERTEX]->bindless | prog->shaders[MESA_SHADER_FRAGMENT]->bindless;
1161 if (prog->base.dd.bindless) {
1162 prog->base.num_dsl = screen->compact_descriptors ? ZINK_DESCRIPTOR_ALL_TYPES - ZINK_DESCRIPTOR_COMPACT : ZINK_DESCRIPTOR_ALL_TYPES;
1163 prog->base.dsl[screen->desc_set_id[ZINK_DESCRIPTOR_BINDLESS]] = screen->bindless_layout;
1165 prog->base.layout = zink_pipeline_layout_create(screen, prog->base.dsl, prog->base.num_dsl, false, VK_PIPELINE_LAYOUT_CREATE_INDEPENDENT_SETS_BIT_EXT);
1167 VkPipeline libs[] = {stages[MESA_SHADER_VERTEX]->precompile.gpl, stages[MESA_SHADER_FRAGMENT]->precompile.gpl};
1168 prog->last_variant_hash = ctx->gfx_pipeline_state.optimal_key;
1170 struct zink_gfx_library_key *gkey = CALLOC_STRUCT(zink_gfx_library_key);
1171 gkey->optimal_key = prog->last_variant_hash;
1172 assert(gkey->optimal_key);
1173 gkey->pipeline = zink_create_gfx_pipeline_combined(screen, prog, VK_NULL_HANDLE, libs, 2, VK_NULL_HANDLE, false);
1174 _mesa_set_add(&prog->libs->libs, gkey);
1176 util_queue_add_job(&screen->cache_get_thread, prog, &prog->base.cache_fence, create_linked_separable_job, NULL, 0);
1181 zink_destroy_gfx_program(screen, prog);
1186 hash_compute_pipeline_state_local_size(const void *key)
1188 const struct zink_compute_pipeline_state *state = key;
1189 uint32_t hash = _mesa_hash_data(state, offsetof(struct zink_compute_pipeline_state, hash));
1190 hash = XXH32(&state->local_size[0], sizeof(state->local_size), hash);
1195 hash_compute_pipeline_state(const void *key)
1197 const struct zink_compute_pipeline_state *state = key;
1198 return _mesa_hash_data(state, offsetof(struct zink_compute_pipeline_state, hash));
1202 zink_program_update_compute_pipeline_state(struct zink_context *ctx, struct zink_compute_program *comp, const uint block[3])
1204 if (comp->use_local_size) {
1205 for (int i = 0; i < ARRAY_SIZE(ctx->compute_pipeline_state.local_size); i++) {
1206 if (ctx->compute_pipeline_state.local_size[i] != block[i])
1207 ctx->compute_pipeline_state.dirty = true;
1208 ctx->compute_pipeline_state.local_size[i] = block[i];
1214 equals_compute_pipeline_state(const void *a, const void *b)
1216 const struct zink_compute_pipeline_state *sa = a;
1217 const struct zink_compute_pipeline_state *sb = b;
1218 return !memcmp(a, b, offsetof(struct zink_compute_pipeline_state, hash)) &&
1219 sa->module == sb->module;
1223 equals_compute_pipeline_state_local_size(const void *a, const void *b)
1225 const struct zink_compute_pipeline_state *sa = a;
1226 const struct zink_compute_pipeline_state *sb = b;
1227 return !memcmp(a, b, offsetof(struct zink_compute_pipeline_state, hash)) &&
1228 !memcmp(sa->local_size, sb->local_size, sizeof(sa->local_size)) &&
1229 sa->module == sb->module;
1233 precompile_compute_job(void *data, void *gdata, int thread_index)
1235 struct zink_compute_program *comp = data;
1236 struct zink_screen *screen = gdata;
1238 comp->shader = zink_shader_create(screen, comp->nir, NULL);
1239 comp->curr = comp->module = CALLOC_STRUCT(zink_shader_module);
1240 assert(comp->module);
1241 comp->module->shader = zink_shader_compile(screen, comp->shader, comp->shader->nir, NULL, NULL);
1242 assert(comp->module->shader);
1243 util_dynarray_init(&comp->shader_cache[0], comp);
1244 util_dynarray_init(&comp->shader_cache[1], comp);
1246 struct blob blob = {0};
1248 nir_serialize(&blob, comp->shader->nir, true);
1250 struct mesa_sha1 sha1_ctx;
1251 _mesa_sha1_init(&sha1_ctx);
1252 _mesa_sha1_update(&sha1_ctx, blob.data, blob.size);
1253 _mesa_sha1_final(&sha1_ctx, comp->base.sha1);
1256 zink_descriptor_program_init(comp->base.ctx, &comp->base);
1258 zink_screen_get_pipeline_cache(screen, &comp->base, true);
1259 if (comp->base.can_precompile)
1260 comp->base_pipeline = zink_create_compute_pipeline(screen, comp, NULL);
1261 if (comp->base_pipeline)
1262 zink_screen_update_pipeline_cache(screen, &comp->base, true);
1265 static struct zink_compute_program *
1266 create_compute_program(struct zink_context *ctx, nir_shader *nir)
1268 struct zink_screen *screen = zink_screen(ctx->base.screen);
1269 struct zink_compute_program *comp = create_program(ctx, true);
1274 comp->use_local_size = !(nir->info.workgroup_size[0] ||
1275 nir->info.workgroup_size[1] ||
1276 nir->info.workgroup_size[2]);
1277 comp->base.can_precompile = !comp->use_local_size &&
1278 (screen->info.have_EXT_non_seamless_cube_map || !zink_shader_has_cubes(nir)) &&
1279 (screen->info.rb2_feats.robustImageAccess2 || !(ctx->flags & PIPE_CONTEXT_ROBUST_BUFFER_ACCESS));
1280 _mesa_hash_table_init(&comp->pipelines, comp, NULL, comp->use_local_size ?
1281 equals_compute_pipeline_state_local_size :
1282 equals_compute_pipeline_state);
1283 util_queue_add_job(&screen->cache_get_thread, comp, &comp->base.cache_fence,
1284 precompile_compute_job, NULL, 0);
1289 zink_program_get_descriptor_usage(struct zink_context *ctx, gl_shader_stage stage, enum zink_descriptor_type type)
1291 struct zink_shader *zs = NULL;
1293 case MESA_SHADER_VERTEX:
1294 case MESA_SHADER_TESS_CTRL:
1295 case MESA_SHADER_TESS_EVAL:
1296 case MESA_SHADER_GEOMETRY:
1297 case MESA_SHADER_FRAGMENT:
1298 zs = ctx->gfx_stages[stage];
1300 case MESA_SHADER_COMPUTE: {
1301 zs = ctx->curr_compute->shader;
1305 unreachable("unknown shader type");
1310 case ZINK_DESCRIPTOR_TYPE_UBO:
1311 return zs->ubos_used;
1312 case ZINK_DESCRIPTOR_TYPE_SSBO:
1313 return zs->ssbos_used;
1314 case ZINK_DESCRIPTOR_TYPE_SAMPLER_VIEW:
1315 return BITSET_TEST_RANGE(zs->nir->info.textures_used, 0, PIPE_MAX_SAMPLERS - 1);
1316 case ZINK_DESCRIPTOR_TYPE_IMAGE:
1317 return BITSET_TEST_RANGE(zs->nir->info.images_used, 0, PIPE_MAX_SAMPLERS - 1);
1319 unreachable("unknown descriptor type!");
1325 zink_program_descriptor_is_buffer(struct zink_context *ctx, gl_shader_stage stage, enum zink_descriptor_type type, unsigned i)
1327 struct zink_shader *zs = NULL;
1329 case MESA_SHADER_VERTEX:
1330 case MESA_SHADER_TESS_CTRL:
1331 case MESA_SHADER_TESS_EVAL:
1332 case MESA_SHADER_GEOMETRY:
1333 case MESA_SHADER_FRAGMENT:
1334 zs = ctx->gfx_stages[stage];
1336 case MESA_SHADER_COMPUTE: {
1337 zs = ctx->curr_compute->shader;
1341 unreachable("unknown shader type");
1345 return zink_shader_descriptor_is_buffer(zs, type, i);
1349 get_num_bindings(struct zink_shader *zs, enum zink_descriptor_type type)
1352 case ZINK_DESCRIPTOR_TYPE_UNIFORMS:
1353 return !!zs->has_uniforms;
1354 case ZINK_DESCRIPTOR_TYPE_UBO:
1355 case ZINK_DESCRIPTOR_TYPE_SSBO:
1356 return zs->num_bindings[type];
1360 unsigned num_bindings = 0;
1361 for (int i = 0; i < zs->num_bindings[type]; i++)
1362 num_bindings += zs->bindings[type][i].size;
1363 return num_bindings;
1367 zink_program_num_bindings_typed(const struct zink_program *pg, enum zink_descriptor_type type)
1369 unsigned num_bindings = 0;
1370 if (pg->is_compute) {
1371 struct zink_compute_program *comp = (void*)pg;
1372 return get_num_bindings(comp->shader, type);
1374 struct zink_gfx_program *prog = (void*)pg;
1375 for (unsigned i = 0; i < ZINK_GFX_SHADER_COUNT; i++) {
1376 if (prog->shaders[i])
1377 num_bindings += get_num_bindings(prog->shaders[i], type);
1379 return num_bindings;
1383 zink_program_num_bindings(const struct zink_program *pg)
1385 unsigned num_bindings = 0;
1386 for (unsigned i = 0; i < ZINK_DESCRIPTOR_BASE_TYPES; i++)
1387 num_bindings += zink_program_num_bindings_typed(pg, i);
1388 return num_bindings;
1392 deinit_program(struct zink_screen *screen, struct zink_program *pg)
1394 util_queue_fence_wait(&pg->cache_fence);
1396 VKSCR(DestroyPipelineLayout)(screen->dev, pg->layout, NULL);
1398 if (pg->pipeline_cache)
1399 VKSCR(DestroyPipelineCache)(screen->dev, pg->pipeline_cache, NULL);
1400 zink_descriptor_program_deinit(screen, pg);
1404 zink_destroy_gfx_program(struct zink_screen *screen,
1405 struct zink_gfx_program *prog)
1407 unsigned max_idx = ARRAY_SIZE(prog->pipelines[0]);
1408 if (screen->info.have_EXT_extended_dynamic_state) {
1409 /* only need first 3/4 for point/line/tri/patch */
1410 if ((prog->stages_present &
1411 (BITFIELD_BIT(MESA_SHADER_TESS_EVAL) | BITFIELD_BIT(MESA_SHADER_GEOMETRY))) ==
1412 BITFIELD_BIT(MESA_SHADER_TESS_EVAL))
1419 if (prog->is_separable)
1420 zink_gfx_program_reference(screen, &prog->full_prog, NULL);
1421 for (unsigned r = 0; r < ARRAY_SIZE(prog->pipelines); r++) {
1422 for (int i = 0; i < max_idx; ++i) {
1423 hash_table_foreach(&prog->pipelines[r][i], entry) {
1424 struct zink_gfx_pipeline_cache_entry *pc_entry = entry->data;
1426 util_queue_fence_wait(&pc_entry->fence);
1427 VKSCR(DestroyPipeline)(screen->dev, pc_entry->pipeline, NULL);
1428 VKSCR(DestroyPipeline)(screen->dev, pc_entry->unoptimized_pipeline, NULL);
1434 deinit_program(screen, &prog->base);
1436 for (int i = 0; i < ZINK_GFX_SHADER_COUNT; ++i) {
1437 if (prog->shaders[i]) {
1438 _mesa_set_remove_key(prog->shaders[i]->programs, prog);
1439 prog->shaders[i] = NULL;
1441 if (!prog->is_separable) {
1442 destroy_shader_cache(screen, &prog->shader_cache[i][0][0]);
1443 destroy_shader_cache(screen, &prog->shader_cache[i][0][1]);
1444 destroy_shader_cache(screen, &prog->shader_cache[i][1][0]);
1445 destroy_shader_cache(screen, &prog->shader_cache[i][1][1]);
1446 ralloc_free(prog->nir[i]);
1449 if (prog->is_separable)
1450 zink_gfx_lib_cache_unref(screen, prog->libs);
1456 zink_destroy_compute_program(struct zink_screen *screen,
1457 struct zink_compute_program *comp)
1459 deinit_program(screen, &comp->base);
1461 assert(comp->shader);
1462 assert(!comp->shader->spirv);
1464 _mesa_set_destroy(comp->shader->programs, NULL);
1465 ralloc_free(comp->shader->nir);
1466 ralloc_free(comp->shader);
1468 destroy_shader_cache(screen, &comp->shader_cache[0]);
1469 destroy_shader_cache(screen, &comp->shader_cache[1]);
1471 hash_table_foreach(&comp->pipelines, entry) {
1472 struct compute_pipeline_cache_entry *pc_entry = entry->data;
1474 VKSCR(DestroyPipeline)(screen->dev, pc_entry->pipeline, NULL);
1477 VKSCR(DestroyPipeline)(screen->dev, comp->base_pipeline, NULL);
1478 VKSCR(DestroyShaderModule)(screen->dev, comp->module->shader, NULL);
1484 ALWAYS_INLINE static bool
1485 compute_can_shortcut(const struct zink_compute_program *comp)
1487 return !comp->use_local_size && !comp->curr->num_uniforms && !comp->curr->has_nonseamless;
1491 zink_get_compute_pipeline(struct zink_screen *screen,
1492 struct zink_compute_program *comp,
1493 struct zink_compute_pipeline_state *state)
1495 struct hash_entry *entry = NULL;
1497 if (!state->dirty && !state->module_changed)
1498 return state->pipeline;
1500 if (state->pipeline) //avoid on first hash
1501 state->final_hash ^= state->hash;
1502 if (comp->use_local_size)
1503 state->hash = hash_compute_pipeline_state_local_size(state);
1505 state->hash = hash_compute_pipeline_state(state);
1506 state->dirty = false;
1507 state->final_hash ^= state->hash;
1510 util_queue_fence_wait(&comp->base.cache_fence);
1511 if (comp->base_pipeline && compute_can_shortcut(comp)) {
1512 state->pipeline = comp->base_pipeline;
1513 return state->pipeline;
1515 entry = _mesa_hash_table_search_pre_hashed(&comp->pipelines, state->final_hash, state);
1518 VkPipeline pipeline = zink_create_compute_pipeline(screen, comp, state);
1520 if (pipeline == VK_NULL_HANDLE)
1521 return VK_NULL_HANDLE;
1523 zink_screen_update_pipeline_cache(screen, &comp->base, false);
1524 if (compute_can_shortcut(comp)) {
1525 /* don't add base pipeline to cache */
1526 state->pipeline = comp->base_pipeline = pipeline;
1527 return state->pipeline;
1530 struct compute_pipeline_cache_entry *pc_entry = CALLOC_STRUCT(compute_pipeline_cache_entry);
1532 return VK_NULL_HANDLE;
1534 memcpy(&pc_entry->state, state, sizeof(*state));
1535 pc_entry->pipeline = pipeline;
1537 entry = _mesa_hash_table_insert_pre_hashed(&comp->pipelines, state->final_hash, pc_entry, pc_entry);
1541 struct compute_pipeline_cache_entry *cache_entry = entry->data;
1542 state->pipeline = cache_entry->pipeline;
1543 return state->pipeline;
1547 bind_gfx_stage(struct zink_context *ctx, gl_shader_stage stage, struct zink_shader *shader)
1549 if (shader && shader->nir->info.num_inlinable_uniforms)
1550 ctx->shader_has_inlinable_uniforms_mask |= 1 << stage;
1552 ctx->shader_has_inlinable_uniforms_mask &= ~(1 << stage);
1554 if (ctx->gfx_stages[stage]) {
1555 ctx->gfx_hash ^= ctx->gfx_stages[stage]->hash;
1557 /* unbind the generated GS */
1558 if (stage != MESA_SHADER_FRAGMENT &&
1559 ctx->gfx_stages[stage]->non_fs.generated_gs &&
1560 ctx->gfx_stages[MESA_SHADER_GEOMETRY] ==
1561 ctx->gfx_stages[stage]->non_fs.generated_gs) {
1562 assert(stage != MESA_SHADER_GEOMETRY); /* let's not keep recursing! */
1563 bind_gfx_stage(ctx, MESA_SHADER_GEOMETRY, NULL);
1566 ctx->gfx_stages[stage] = shader;
1567 ctx->gfx_dirty = ctx->gfx_stages[MESA_SHADER_FRAGMENT] && ctx->gfx_stages[MESA_SHADER_VERTEX];
1568 ctx->gfx_pipeline_state.modules_changed = true;
1570 ctx->shader_stages |= BITFIELD_BIT(stage);
1571 ctx->gfx_hash ^= ctx->gfx_stages[stage]->hash;
1573 ctx->gfx_pipeline_state.modules[stage] = VK_NULL_HANDLE;
1574 if (ctx->curr_program)
1575 ctx->gfx_pipeline_state.final_hash ^= ctx->curr_program->last_variant_hash;
1576 ctx->curr_program = NULL;
1577 ctx->shader_stages &= ~BITFIELD_BIT(stage);
1581 static enum pipe_prim_type
1582 gs_output_to_reduced_prim_type(struct shader_info *info)
1584 switch (info->gs.output_primitive) {
1585 case SHADER_PRIM_POINTS:
1586 return PIPE_PRIM_POINTS;
1588 case SHADER_PRIM_LINES:
1589 case SHADER_PRIM_LINE_LOOP:
1590 case SHADER_PRIM_LINE_STRIP:
1591 case SHADER_PRIM_LINES_ADJACENCY:
1592 case SHADER_PRIM_LINE_STRIP_ADJACENCY:
1593 return PIPE_PRIM_LINES;
1595 case SHADER_PRIM_TRIANGLES:
1596 case SHADER_PRIM_TRIANGLE_STRIP:
1597 case SHADER_PRIM_TRIANGLE_FAN:
1598 case SHADER_PRIM_TRIANGLES_ADJACENCY:
1599 case SHADER_PRIM_TRIANGLE_STRIP_ADJACENCY:
1600 return PIPE_PRIM_TRIANGLES;
1603 unreachable("unexpected output primitive type");
1607 static enum pipe_prim_type
1608 update_rast_prim(struct zink_shader *shader)
1610 struct shader_info *info = &shader->nir->info;
1611 if (info->stage == MESA_SHADER_GEOMETRY)
1612 return gs_output_to_reduced_prim_type(info);
1613 else if (info->stage == MESA_SHADER_TESS_EVAL) {
1614 if (info->tess.point_mode)
1615 return PIPE_PRIM_POINTS;
1617 switch (info->tess._primitive_mode) {
1618 case TESS_PRIMITIVE_ISOLINES:
1619 return PIPE_PRIM_LINES;
1620 case TESS_PRIMITIVE_TRIANGLES:
1621 case TESS_PRIMITIVE_QUADS:
1622 return PIPE_PRIM_TRIANGLES;
1624 return PIPE_PRIM_MAX;
1628 return PIPE_PRIM_MAX;
1632 bind_last_vertex_stage(struct zink_context *ctx)
1634 gl_shader_stage old = ctx->last_vertex_stage ? ctx->last_vertex_stage->nir->info.stage : MESA_SHADER_STAGES;
1635 if (ctx->gfx_stages[MESA_SHADER_GEOMETRY])
1636 ctx->last_vertex_stage = ctx->gfx_stages[MESA_SHADER_GEOMETRY];
1637 else if (ctx->gfx_stages[MESA_SHADER_TESS_EVAL])
1638 ctx->last_vertex_stage = ctx->gfx_stages[MESA_SHADER_TESS_EVAL];
1640 ctx->last_vertex_stage = ctx->gfx_stages[MESA_SHADER_VERTEX];
1641 gl_shader_stage current = ctx->last_vertex_stage ? ctx->last_vertex_stage->nir->info.stage : MESA_SHADER_VERTEX;
1643 /* update rast_prim */
1644 ctx->gfx_pipeline_state.shader_rast_prim =
1645 ctx->last_vertex_stage ? update_rast_prim(ctx->last_vertex_stage) :
1648 if (old != current) {
1649 if (!zink_screen(ctx->base.screen)->optimal_keys) {
1650 if (old != MESA_SHADER_STAGES) {
1651 memset(&ctx->gfx_pipeline_state.shader_keys.key[old].key.vs_base, 0, sizeof(struct zink_vs_key_base));
1652 ctx->dirty_gfx_stages |= BITFIELD_BIT(old);
1654 /* always unset vertex shader values when changing to a non-vs last stage */
1655 memset(&ctx->gfx_pipeline_state.shader_keys.key[MESA_SHADER_VERTEX].key.vs_base, 0, sizeof(struct zink_vs_key_base));
1659 unsigned num_viewports = ctx->vp_state.num_viewports;
1660 struct zink_screen *screen = zink_screen(ctx->base.screen);
1661 /* number of enabled viewports is based on whether last vertex stage writes viewport index */
1662 if (ctx->last_vertex_stage) {
1663 if (ctx->last_vertex_stage->nir->info.outputs_written & (VARYING_BIT_VIEWPORT | VARYING_BIT_VIEWPORT_MASK))
1664 ctx->vp_state.num_viewports = MIN2(screen->info.props.limits.maxViewports, PIPE_MAX_VIEWPORTS);
1666 ctx->vp_state.num_viewports = 1;
1668 ctx->vp_state.num_viewports = 1;
1670 ctx->vp_state_changed |= num_viewports != ctx->vp_state.num_viewports;
1671 if (!screen->info.have_EXT_extended_dynamic_state) {
1672 if (ctx->gfx_pipeline_state.dyn_state1.num_viewports != ctx->vp_state.num_viewports)
1673 ctx->gfx_pipeline_state.dirty = true;
1674 ctx->gfx_pipeline_state.dyn_state1.num_viewports = ctx->vp_state.num_viewports;
1676 ctx->last_vertex_stage_dirty = true;
1681 zink_bind_vs_state(struct pipe_context *pctx,
1684 struct zink_context *ctx = zink_context(pctx);
1685 if (!cso && !ctx->gfx_stages[MESA_SHADER_VERTEX])
1687 bind_gfx_stage(ctx, MESA_SHADER_VERTEX, cso);
1688 bind_last_vertex_stage(ctx);
1690 struct zink_shader *zs = cso;
1691 ctx->shader_reads_drawid = BITSET_TEST(zs->nir->info.system_values_read, SYSTEM_VALUE_DRAW_ID);
1692 ctx->shader_reads_basevertex = BITSET_TEST(zs->nir->info.system_values_read, SYSTEM_VALUE_BASE_VERTEX);
1694 ctx->shader_reads_drawid = false;
1695 ctx->shader_reads_basevertex = false;
1699 /* if gl_SampleMask[] is written to, we have to ensure that we get a shader with the same sample count:
1700 * in GL, samples==1 means ignore gl_SampleMask[]
1701 * in VK, gl_SampleMask[] is never ignored
1704 zink_update_fs_key_samples(struct zink_context *ctx)
1706 if (!ctx->gfx_stages[MESA_SHADER_FRAGMENT])
1708 nir_shader *nir = ctx->gfx_stages[MESA_SHADER_FRAGMENT]->nir;
1709 if (nir->info.outputs_written & (1 << FRAG_RESULT_SAMPLE_MASK)) {
1710 bool samples = zink_get_fs_base_key(ctx)->samples;
1711 if (samples != (ctx->fb_state.samples > 1))
1712 zink_set_fs_base_key(ctx)->samples = ctx->fb_state.samples > 1;
1717 zink_bind_fs_state(struct pipe_context *pctx,
1720 struct zink_context *ctx = zink_context(pctx);
1721 if (!cso && !ctx->gfx_stages[MESA_SHADER_FRAGMENT])
1723 unsigned shadow_mask = ctx->gfx_stages[MESA_SHADER_FRAGMENT] ? ctx->gfx_stages[MESA_SHADER_FRAGMENT]->fs.legacy_shadow_mask : 0;
1724 bind_gfx_stage(ctx, MESA_SHADER_FRAGMENT, cso);
1725 ctx->fbfetch_outputs = 0;
1727 nir_shader *nir = ctx->gfx_stages[MESA_SHADER_FRAGMENT]->nir;
1728 if (nir->info.fs.uses_fbfetch_output) {
1729 nir_foreach_shader_out_variable(var, ctx->gfx_stages[MESA_SHADER_FRAGMENT]->nir) {
1730 if (var->data.fb_fetch_output)
1731 ctx->fbfetch_outputs |= BITFIELD_BIT(var->data.location - FRAG_RESULT_DATA0);
1734 zink_update_fs_key_samples(ctx);
1735 if (zink_screen(pctx->screen)->info.have_EXT_rasterization_order_attachment_access) {
1736 if (ctx->gfx_pipeline_state.rast_attachment_order != nir->info.fs.uses_fbfetch_output)
1737 ctx->gfx_pipeline_state.dirty = true;
1738 ctx->gfx_pipeline_state.rast_attachment_order = nir->info.fs.uses_fbfetch_output;
1740 zink_set_fs_shadow_needs_shader_swizzle_key(ctx, false);
1741 if (shadow_mask != ctx->gfx_stages[MESA_SHADER_FRAGMENT]->fs.legacy_shadow_mask)
1742 zink_update_shadow_samplerviews(ctx, shadow_mask | ctx->gfx_stages[MESA_SHADER_FRAGMENT]->fs.legacy_shadow_mask);
1744 zink_update_fbfetch(ctx);
1748 zink_bind_gs_state(struct pipe_context *pctx,
1751 struct zink_context *ctx = zink_context(pctx);
1752 if (!cso && !ctx->gfx_stages[MESA_SHADER_GEOMETRY])
1754 bind_gfx_stage(ctx, MESA_SHADER_GEOMETRY, cso);
1755 bind_last_vertex_stage(ctx);
1759 zink_bind_tcs_state(struct pipe_context *pctx,
1762 bind_gfx_stage(zink_context(pctx), MESA_SHADER_TESS_CTRL, cso);
1766 zink_bind_tes_state(struct pipe_context *pctx,
1769 struct zink_context *ctx = zink_context(pctx);
1770 if (!cso && !ctx->gfx_stages[MESA_SHADER_TESS_EVAL])
1772 if (!!ctx->gfx_stages[MESA_SHADER_TESS_EVAL] != !!cso) {
1774 /* if unsetting a TESS that uses a generated TCS, ensure the TCS is unset */
1775 if (ctx->gfx_stages[MESA_SHADER_TESS_EVAL]->non_fs.generated_tcs)
1776 ctx->gfx_stages[MESA_SHADER_TESS_CTRL] = NULL;
1779 bind_gfx_stage(ctx, MESA_SHADER_TESS_EVAL, cso);
1780 bind_last_vertex_stage(ctx);
1784 zink_create_cs_state(struct pipe_context *pctx,
1785 const struct pipe_compute_state *shader)
1787 struct nir_shader *nir;
1788 if (shader->ir_type != PIPE_SHADER_IR_NIR)
1789 nir = zink_tgsi_to_nir(pctx->screen, shader->prog);
1791 nir = (struct nir_shader *)shader->prog;
1793 if (nir->info.uses_bindless)
1794 zink_descriptors_init_bindless(zink_context(pctx));
1796 return create_compute_program(zink_context(pctx), nir);
1800 zink_bind_cs_state(struct pipe_context *pctx,
1803 struct zink_context *ctx = zink_context(pctx);
1804 struct zink_compute_program *comp = cso;
1805 if (comp && comp->nir->info.num_inlinable_uniforms)
1806 ctx->shader_has_inlinable_uniforms_mask |= 1 << MESA_SHADER_COMPUTE;
1808 ctx->shader_has_inlinable_uniforms_mask &= ~(1 << MESA_SHADER_COMPUTE);
1810 if (ctx->curr_compute) {
1811 zink_batch_reference_program(&ctx->batch, &ctx->curr_compute->base);
1812 ctx->compute_pipeline_state.final_hash ^= ctx->compute_pipeline_state.module_hash;
1813 ctx->compute_pipeline_state.module = VK_NULL_HANDLE;
1814 ctx->compute_pipeline_state.module_hash = 0;
1816 ctx->compute_pipeline_state.dirty = true;
1817 ctx->curr_compute = comp;
1818 if (comp && comp != ctx->curr_compute) {
1819 ctx->compute_pipeline_state.module_hash = ctx->curr_compute->curr->hash;
1820 if (util_queue_fence_is_signalled(&comp->base.cache_fence))
1821 ctx->compute_pipeline_state.module = ctx->curr_compute->curr->shader;
1822 ctx->compute_pipeline_state.final_hash ^= ctx->compute_pipeline_state.module_hash;
1823 if (ctx->compute_pipeline_state.key.base.nonseamless_cube_mask)
1824 ctx->compute_dirty = true;
1826 zink_select_launch_grid(ctx);
1830 zink_delete_cs_shader_state(struct pipe_context *pctx, void *cso)
1832 struct zink_compute_program *comp = cso;
1833 zink_compute_program_reference(zink_screen(pctx->screen), &comp, NULL);
1837 zink_delete_shader_state(struct pipe_context *pctx, void *cso)
1839 zink_shader_free(zink_screen(pctx->screen), cso);
1843 zink_create_gfx_shader_state(struct pipe_context *pctx, const struct pipe_shader_state *shader)
1846 if (shader->type != PIPE_SHADER_IR_NIR)
1847 nir = zink_tgsi_to_nir(pctx->screen, shader->tokens);
1849 nir = (struct nir_shader *)shader->ir.nir;
1851 if (nir->info.stage == MESA_SHADER_FRAGMENT && nir->info.fs.uses_fbfetch_output)
1852 zink_descriptor_util_init_fbfetch(zink_context(pctx));
1853 if (nir->info.uses_bindless)
1854 zink_descriptors_init_bindless(zink_context(pctx));
1856 return zink_shader_create(zink_screen(pctx->screen), nir, &shader->stream_output);
1860 zink_delete_cached_shader_state(struct pipe_context *pctx, void *cso)
1862 struct zink_screen *screen = zink_screen(pctx->screen);
1863 util_shader_reference(pctx, &screen->shaders, &cso, NULL);
1867 zink_create_cached_shader_state(struct pipe_context *pctx, const struct pipe_shader_state *shader)
1870 struct zink_screen *screen = zink_screen(pctx->screen);
1871 return util_live_shader_cache_get(pctx, &screen->shaders, shader, &cache_hit);
1874 /* caller must lock prog->libs->lock */
1875 struct zink_gfx_library_key *
1876 zink_create_pipeline_lib(struct zink_screen *screen, struct zink_gfx_program *prog, struct zink_gfx_pipeline_state *state)
1878 struct zink_gfx_library_key *gkey = CALLOC_STRUCT(zink_gfx_library_key);
1879 gkey->optimal_key = state->optimal_key;
1880 assert(gkey->optimal_key);
1881 memcpy(gkey->modules, prog->modules, sizeof(gkey->modules));
1882 gkey->pipeline = zink_create_gfx_pipeline_library(screen, prog);
1883 _mesa_set_add(&prog->libs->libs, gkey);
1888 print_exe_stages(VkShaderStageFlags stages)
1890 if (stages == VK_SHADER_STAGE_VERTEX_BIT)
1892 if (stages == (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT))
1894 if (stages == (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT))
1895 return "VS+TCS+TES";
1896 if (stages == (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT | VK_SHADER_STAGE_GEOMETRY_BIT))
1897 return "VS+TCS+TES+GS";
1898 if (stages == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT)
1900 if (stages == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)
1902 if (stages == VK_SHADER_STAGE_GEOMETRY_BIT)
1904 if (stages == VK_SHADER_STAGE_FRAGMENT_BIT)
1906 if (stages == VK_SHADER_STAGE_COMPUTE_BIT)
1908 unreachable("unhandled combination of stages!");
1912 print_pipeline_stats(struct zink_screen *screen, VkPipeline pipeline)
1914 VkPipelineInfoKHR pinfo = {
1915 VK_STRUCTURE_TYPE_PIPELINE_INFO_KHR,
1919 unsigned exe_count = 0;
1920 VkPipelineExecutablePropertiesKHR props[10] = {0};
1921 for (unsigned i = 0; i < ARRAY_SIZE(props); i++) {
1922 props[i].sType = VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_PROPERTIES_KHR;
1923 props[i].pNext = NULL;
1925 VKSCR(GetPipelineExecutablePropertiesKHR)(screen->dev, &pinfo, &exe_count, NULL);
1926 VKSCR(GetPipelineExecutablePropertiesKHR)(screen->dev, &pinfo, &exe_count, props);
1927 printf("PIPELINE STATISTICS:");
1928 for (unsigned e = 0; e < exe_count; e++) {
1929 VkPipelineExecutableInfoKHR info = {
1930 VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_INFO_KHR,
1936 printf("\n\t%s (%s): ", print_exe_stages(props[e].stages), props[e].name);
1937 VkPipelineExecutableStatisticKHR *stats = NULL;
1938 VKSCR(GetPipelineExecutableStatisticsKHR)(screen->dev, &info, &count, NULL);
1939 stats = calloc(count, sizeof(VkPipelineExecutableStatisticKHR));
1940 for (unsigned i = 0; i < count; i++)
1941 stats[i].sType = VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_STATISTIC_KHR;
1942 VKSCR(GetPipelineExecutableStatisticsKHR)(screen->dev, &info, &count, stats);
1944 for (unsigned i = 0; i < count; i++) {
1947 switch (stats[i].format) {
1948 case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_BOOL32_KHR:
1949 printf("%s: %u", stats[i].name, stats[i].value.b32);
1951 case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_INT64_KHR:
1952 printf("%s: %" PRIi64, stats[i].name, stats[i].value.i64);
1954 case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR:
1955 printf("%s: %" PRIu64, stats[i].name, stats[i].value.u64);
1957 case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_FLOAT64_KHR:
1958 printf("%s: %g", stats[i].name, stats[i].value.f64);
1961 unreachable("unknown statistic");
1969 precompile_job(void *data, void *gdata, int thread_index)
1971 struct zink_screen *screen = gdata;
1972 struct zink_gfx_program *prog = data;
1974 struct zink_gfx_pipeline_state state = {0};
1975 state.shader_keys_optimal.key.vs_base.last_vertex_stage = true;
1976 state.shader_keys_optimal.key.tcs.patch_vertices = 3; //random guess, generated tcs precompile is hard
1977 state.optimal_key = state.shader_keys_optimal.key.val;
1978 generate_gfx_program_modules_optimal(NULL, screen, prog, &state);
1979 zink_screen_get_pipeline_cache(screen, &prog->base, true);
1980 simple_mtx_lock(&prog->libs->lock);
1981 zink_create_pipeline_lib(screen, prog, &state);
1982 simple_mtx_unlock(&prog->libs->lock);
1983 zink_screen_update_pipeline_cache(screen, &prog->base, true);
1987 precompile_separate_shader_job(void *data, void *gdata, int thread_index)
1989 struct zink_screen *screen = gdata;
1990 struct zink_shader *zs = data;
1993 zs->precompile.mod = zink_shader_compile_separate(screen, zs, &nir);
1994 zink_descriptor_shader_init(screen, zs);
1995 VkShaderModule mods[ZINK_GFX_SHADER_COUNT] = {0};
1996 mods[nir->info.stage] = zs->precompile.mod;
1997 zs->precompile.gpl = zink_create_gfx_pipeline_separate(screen, mods, zs->precompile.layout);
2001 zink_link_gfx_shader(struct pipe_context *pctx, void **shaders)
2003 struct zink_context *ctx = zink_context(pctx);
2004 struct zink_shader **zshaders = (struct zink_shader **)shaders;
2005 if (shaders[MESA_SHADER_COMPUTE])
2007 /* can't precompile fixedfunc */
2008 if (!shaders[MESA_SHADER_VERTEX] || !shaders[MESA_SHADER_FRAGMENT]) {
2009 if (shaders[MESA_SHADER_VERTEX] || shaders[MESA_SHADER_FRAGMENT]) {
2010 struct zink_shader *zs = shaders[MESA_SHADER_VERTEX] ? shaders[MESA_SHADER_VERTEX] : shaders[MESA_SHADER_FRAGMENT];
2011 if (zs->nir->info.separate_shader && !zs->precompile.mod && util_queue_fence_is_signalled(&zs->precompile.fence) &&
2012 zink_descriptor_mode == ZINK_DESCRIPTOR_MODE_DB &&
2013 /* sample shading can't precompile */
2014 (!shaders[MESA_SHADER_FRAGMENT] || !zs->nir->info.fs.uses_sample_shading))
2015 util_queue_add_job(&zink_screen(pctx->screen)->cache_get_thread, zs, &zs->precompile.fence, precompile_separate_shader_job, NULL, 0);
2020 unsigned shader_stages = 0;
2021 for (unsigned i = 0; i < ZINK_GFX_SHADER_COUNT; i++) {
2023 hash ^= zshaders[i]->hash;
2024 shader_stages |= BITFIELD_BIT(i);
2027 unsigned tess_stages = BITFIELD_BIT(MESA_SHADER_TESS_CTRL) | BITFIELD_BIT(MESA_SHADER_TESS_EVAL);
2028 unsigned tess = shader_stages & tess_stages;
2029 /* can't do fixedfunc tes either */
2030 if (tess && !shaders[MESA_SHADER_TESS_EVAL])
2032 struct hash_table *ht = &ctx->program_cache[zink_program_cache_stages(shader_stages)];
2033 simple_mtx_lock(&ctx->program_lock[zink_program_cache_stages(shader_stages)]);
2034 /* link can be called repeatedly with the same shaders: ignore */
2035 if (_mesa_hash_table_search_pre_hashed(ht, hash, shaders)) {
2036 simple_mtx_unlock(&ctx->program_lock[zink_program_cache_stages(shader_stages)]);
2039 struct zink_gfx_program *prog = zink_create_gfx_program(ctx, zshaders, 3, hash);
2040 u_foreach_bit(i, shader_stages)
2041 assert(prog->shaders[i]);
2042 _mesa_hash_table_insert_pre_hashed(ht, hash, prog->shaders, prog);
2043 prog->base.removed = false;
2044 simple_mtx_unlock(&ctx->program_lock[zink_program_cache_stages(shader_stages)]);
2045 if (zink_debug & ZINK_DEBUG_SHADERDB) {
2046 struct zink_screen *screen = zink_screen(pctx->screen);
2047 if (screen->optimal_keys)
2048 generate_gfx_program_modules_optimal(ctx, screen, prog, &ctx->gfx_pipeline_state);
2050 generate_gfx_program_modules(ctx, screen, prog, &ctx->gfx_pipeline_state);
2051 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);
2052 print_pipeline_stats(screen, pipeline);
2054 util_queue_add_job(&zink_screen(pctx->screen)->cache_get_thread, prog, &prog->base.cache_fence, precompile_job, NULL, 0);
2059 zink_program_init(struct zink_context *ctx)
2061 ctx->base.create_vs_state = zink_create_cached_shader_state;
2062 ctx->base.bind_vs_state = zink_bind_vs_state;
2063 ctx->base.delete_vs_state = zink_delete_cached_shader_state;
2065 ctx->base.create_fs_state = zink_create_cached_shader_state;
2066 ctx->base.bind_fs_state = zink_bind_fs_state;
2067 ctx->base.delete_fs_state = zink_delete_cached_shader_state;
2069 ctx->base.create_gs_state = zink_create_cached_shader_state;
2070 ctx->base.bind_gs_state = zink_bind_gs_state;
2071 ctx->base.delete_gs_state = zink_delete_cached_shader_state;
2073 ctx->base.create_tcs_state = zink_create_cached_shader_state;
2074 ctx->base.bind_tcs_state = zink_bind_tcs_state;
2075 ctx->base.delete_tcs_state = zink_delete_cached_shader_state;
2077 ctx->base.create_tes_state = zink_create_cached_shader_state;
2078 ctx->base.bind_tes_state = zink_bind_tes_state;
2079 ctx->base.delete_tes_state = zink_delete_cached_shader_state;
2081 ctx->base.create_compute_state = zink_create_cs_state;
2082 ctx->base.bind_compute_state = zink_bind_cs_state;
2083 ctx->base.delete_compute_state = zink_delete_cs_shader_state;
2085 if (zink_screen(ctx->base.screen)->info.have_EXT_vertex_input_dynamic_state)
2086 _mesa_set_init(&ctx->gfx_inputs, ctx, hash_gfx_input_dynamic, equals_gfx_input_dynamic);
2088 _mesa_set_init(&ctx->gfx_inputs, ctx, hash_gfx_input, equals_gfx_input);
2089 if (zink_screen(ctx->base.screen)->have_full_ds3)
2090 _mesa_set_init(&ctx->gfx_outputs, ctx, hash_gfx_output_ds3, equals_gfx_output_ds3);
2092 _mesa_set_init(&ctx->gfx_outputs, ctx, hash_gfx_output, equals_gfx_output);
2093 /* validate struct packing */
2094 STATIC_ASSERT(offsetof(struct zink_gfx_output_key, sample_mask) == sizeof(uint32_t));
2095 STATIC_ASSERT(offsetof(struct zink_gfx_pipeline_state, vertex_buffers_enabled_mask) - offsetof(struct zink_gfx_pipeline_state, input) ==
2096 offsetof(struct zink_gfx_input_key, vertex_buffers_enabled_mask) - offsetof(struct zink_gfx_input_key, input));
2097 STATIC_ASSERT(offsetof(struct zink_gfx_pipeline_state, vertex_strides) - offsetof(struct zink_gfx_pipeline_state, input) ==
2098 offsetof(struct zink_gfx_input_key, vertex_strides) - offsetof(struct zink_gfx_input_key, input));
2099 STATIC_ASSERT(offsetof(struct zink_gfx_pipeline_state, element_state) - offsetof(struct zink_gfx_pipeline_state, input) ==
2100 offsetof(struct zink_gfx_input_key, element_state) - offsetof(struct zink_gfx_input_key, input));
2102 STATIC_ASSERT(sizeof(union zink_shader_key_optimal) == sizeof(uint32_t));
2104 if (zink_screen(ctx->base.screen)->info.have_EXT_graphics_pipeline_library || zink_debug & ZINK_DEBUG_SHADERDB)
2105 ctx->base.link_shader = zink_link_gfx_shader;
2109 zink_set_rasterizer_discard(struct zink_context *ctx, bool disable)
2111 bool value = disable ? false : (ctx->rast_state ? ctx->rast_state->base.rasterizer_discard : false);
2112 bool changed = ctx->gfx_pipeline_state.dyn_state2.rasterizer_discard != value;
2113 ctx->gfx_pipeline_state.dyn_state2.rasterizer_discard = value;
2116 if (!zink_screen(ctx->base.screen)->info.have_EXT_extended_dynamic_state2)
2117 ctx->gfx_pipeline_state.dirty |= true;
2118 ctx->rasterizer_discard_changed = true;
2123 zink_driver_thread_add_job(struct pipe_screen *pscreen, void *data,
2124 struct util_queue_fence *fence,
2125 pipe_driver_thread_func execute,
2126 pipe_driver_thread_func cleanup,
2127 const size_t job_size)
2129 struct zink_screen *screen = zink_screen(pscreen);
2130 util_queue_add_job(&screen->cache_get_thread, data, fence, execute, cleanup, job_size);
2134 zink_set_primitive_emulation_keys(struct zink_context *ctx)
2136 struct zink_screen *screen = zink_screen(ctx->base.screen);
2137 bool lower_line_stipple = ctx->gfx_pipeline_state.rast_prim == PIPE_PRIM_LINES &&
2138 screen->driver_workarounds.no_linestipple &&
2139 ctx->rast_state->base.line_stipple_enable &&
2140 !ctx->num_so_targets;
2142 if (zink_get_fs_key(ctx)->lower_line_stipple != lower_line_stipple) {
2143 assert(zink_get_gs_key(ctx)->lower_line_stipple ==
2144 zink_get_fs_key(ctx)->lower_line_stipple);
2145 zink_set_fs_key(ctx)->lower_line_stipple = lower_line_stipple;
2146 zink_set_gs_key(ctx)->lower_line_stipple = lower_line_stipple;
2149 bool lower_line_smooth = screen->driver_workarounds.no_linesmooth &&
2150 ctx->rast_state->base.line_smooth &&
2151 !ctx->num_so_targets;
2153 if (zink_get_fs_key(ctx)->lower_line_smooth != lower_line_smooth) {
2154 assert(zink_get_gs_key(ctx)->lower_line_smooth ==
2155 zink_get_fs_key(ctx)->lower_line_smooth);
2156 zink_set_fs_key(ctx)->lower_line_smooth = lower_line_smooth;
2157 zink_set_gs_key(ctx)->lower_line_smooth = lower_line_smooth;
2160 if (lower_line_stipple || lower_line_smooth ||
2161 zink_get_gs_key(ctx)->lower_gl_point) {
2162 enum pipe_shader_type prev_vertex_stage =
2163 ctx->gfx_stages[MESA_SHADER_TESS_EVAL] ?
2164 MESA_SHADER_TESS_EVAL : MESA_SHADER_VERTEX;
2166 if (!ctx->gfx_stages[MESA_SHADER_GEOMETRY]) {
2167 assert(!screen->optimal_keys);
2169 if (!ctx->gfx_stages[prev_vertex_stage]->non_fs.generated_gs) {
2170 nir_shader *nir = nir_create_passthrough_gs(
2171 &screen->nir_options,
2172 ctx->gfx_stages[prev_vertex_stage]->nir,
2173 (lower_line_stipple || lower_line_smooth) ? SHADER_PRIM_LINE_STRIP : SHADER_PRIM_POINTS,
2174 (lower_line_stipple || lower_line_smooth) ? 2 : 1);
2176 struct zink_shader *shader = zink_shader_create(screen, nir, NULL);
2177 ctx->gfx_stages[prev_vertex_stage]->non_fs.generated_gs = shader;
2178 shader->non_fs.is_generated = true;
2181 bind_gfx_stage(ctx, MESA_SHADER_GEOMETRY,
2182 ctx->gfx_stages[prev_vertex_stage]->non_fs.generated_gs);
2184 } else if (ctx->gfx_stages[MESA_SHADER_GEOMETRY] &&
2185 ctx->gfx_stages[MESA_SHADER_GEOMETRY]->non_fs.is_generated)
2186 bind_gfx_stage(ctx, MESA_SHADER_GEOMETRY, NULL);