anv,i965,iris: deduplicate setting of total_shared
[mesa.git] / src / gallium / drivers / iris / iris_program.c
1 /*
2 * Copyright © 2017 Intel Corporation
3 *
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 /**
24 * @file iris_program.c
25 *
26 * This file contains the driver interface for compiling shaders.
27 *
28 * See iris_program_cache.c for the in-memory program cache where the
29 * compiled shaders are stored.
30 */
31
32 #include <stdio.h>
33 #include <errno.h>
34 #include "pipe/p_defines.h"
35 #include "pipe/p_state.h"
36 #include "pipe/p_context.h"
37 #include "pipe/p_screen.h"
38 #include "util/u_atomic.h"
39 #include "util/u_upload_mgr.h"
40 #include "util/debug.h"
41 #include "compiler/nir/nir.h"
42 #include "compiler/nir/nir_builder.h"
43 #include "compiler/nir/nir_serialize.h"
44 #include "intel/compiler/brw_compiler.h"
45 #include "intel/compiler/brw_nir.h"
46 #include "iris_context.h"
47 #include "nir/tgsi_to_nir.h"
48
49 #define KEY_INIT_NO_ID(gen) \
50 .base.subgroup_size_type = BRW_SUBGROUP_SIZE_UNIFORM, \
51 .base.tex.swizzles[0 ... MAX_SAMPLERS - 1] = 0x688, \
52 .base.tex.compressed_multisample_layout_mask = ~0, \
53 .base.tex.msaa_16 = (gen >= 9 ? ~0 : 0)
54 #define KEY_INIT(gen) .base.program_string_id = ish->program_id, KEY_INIT_NO_ID(gen)
55
56 static unsigned
57 get_new_program_id(struct iris_screen *screen)
58 {
59 return p_atomic_inc_return(&screen->program_id);
60 }
61
62 static void *
63 upload_state(struct u_upload_mgr *uploader,
64 struct iris_state_ref *ref,
65 unsigned size,
66 unsigned alignment)
67 {
68 void *p = NULL;
69 u_upload_alloc(uploader, 0, size, alignment, &ref->offset, &ref->res, &p);
70 return p;
71 }
72
73 void
74 iris_upload_ubo_ssbo_surf_state(struct iris_context *ice,
75 struct pipe_shader_buffer *buf,
76 struct iris_state_ref *surf_state,
77 bool ssbo)
78 {
79 struct pipe_context *ctx = &ice->ctx;
80 struct iris_screen *screen = (struct iris_screen *) ctx->screen;
81
82 void *map =
83 upload_state(ice->state.surface_uploader, surf_state,
84 screen->isl_dev.ss.size, 64);
85 if (!unlikely(map)) {
86 surf_state->res = NULL;
87 return;
88 }
89
90 struct iris_resource *res = (void *) buf->buffer;
91 struct iris_bo *surf_bo = iris_resource_bo(surf_state->res);
92 surf_state->offset += iris_bo_offset_from_base_address(surf_bo);
93
94 isl_buffer_fill_state(&screen->isl_dev, map,
95 .address = res->bo->gtt_offset + res->offset +
96 buf->buffer_offset,
97 .size_B = buf->buffer_size - res->offset,
98 .format = ssbo ? ISL_FORMAT_RAW
99 : ISL_FORMAT_R32G32B32A32_FLOAT,
100 .swizzle = ISL_SWIZZLE_IDENTITY,
101 .stride_B = 1,
102 .mocs = ice->vtbl.mocs(res->bo));
103 }
104
105 static nir_ssa_def *
106 get_aoa_deref_offset(nir_builder *b,
107 nir_deref_instr *deref,
108 unsigned elem_size)
109 {
110 unsigned array_size = elem_size;
111 nir_ssa_def *offset = nir_imm_int(b, 0);
112
113 while (deref->deref_type != nir_deref_type_var) {
114 assert(deref->deref_type == nir_deref_type_array);
115
116 /* This level's element size is the previous level's array size */
117 nir_ssa_def *index = nir_ssa_for_src(b, deref->arr.index, 1);
118 assert(deref->arr.index.ssa);
119 offset = nir_iadd(b, offset,
120 nir_imul(b, index, nir_imm_int(b, array_size)));
121
122 deref = nir_deref_instr_parent(deref);
123 assert(glsl_type_is_array(deref->type));
124 array_size *= glsl_get_length(deref->type);
125 }
126
127 /* Accessing an invalid surface index with the dataport can result in a
128 * hang. According to the spec "if the index used to select an individual
129 * element is negative or greater than or equal to the size of the array,
130 * the results of the operation are undefined but may not lead to
131 * termination" -- which is one of the possible outcomes of the hang.
132 * Clamp the index to prevent access outside of the array bounds.
133 */
134 return nir_umin(b, offset, nir_imm_int(b, array_size - elem_size));
135 }
136
137 static void
138 iris_lower_storage_image_derefs(nir_shader *nir)
139 {
140 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
141
142 nir_builder b;
143 nir_builder_init(&b, impl);
144
145 nir_foreach_block(block, impl) {
146 nir_foreach_instr_safe(instr, block) {
147 if (instr->type != nir_instr_type_intrinsic)
148 continue;
149
150 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
151 switch (intrin->intrinsic) {
152 case nir_intrinsic_image_deref_load:
153 case nir_intrinsic_image_deref_store:
154 case nir_intrinsic_image_deref_atomic_add:
155 case nir_intrinsic_image_deref_atomic_min:
156 case nir_intrinsic_image_deref_atomic_max:
157 case nir_intrinsic_image_deref_atomic_and:
158 case nir_intrinsic_image_deref_atomic_or:
159 case nir_intrinsic_image_deref_atomic_xor:
160 case nir_intrinsic_image_deref_atomic_exchange:
161 case nir_intrinsic_image_deref_atomic_comp_swap:
162 case nir_intrinsic_image_deref_size:
163 case nir_intrinsic_image_deref_samples:
164 case nir_intrinsic_image_deref_load_raw_intel:
165 case nir_intrinsic_image_deref_store_raw_intel: {
166 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
167 nir_variable *var = nir_deref_instr_get_variable(deref);
168
169 b.cursor = nir_before_instr(&intrin->instr);
170 nir_ssa_def *index =
171 nir_iadd(&b, nir_imm_int(&b, var->data.driver_location),
172 get_aoa_deref_offset(&b, deref, 1));
173 nir_rewrite_image_intrinsic(intrin, index, false);
174 break;
175 }
176
177 default:
178 break;
179 }
180 }
181 }
182 }
183
184 // XXX: need unify_interfaces() at link time...
185
186 /**
187 * Fix an uncompiled shader's stream output info.
188 *
189 * Core Gallium stores output->register_index as a "slot" number, where
190 * slots are assigned consecutively to all outputs in info->outputs_written.
191 * This naive packing of outputs doesn't work for us - we too have slots,
192 * but the layout is defined by the VUE map, which we won't have until we
193 * compile a specific shader variant. So, we remap these and simply store
194 * VARYING_SLOT_* in our copy's output->register_index fields.
195 *
196 * We also fix up VARYING_SLOT_{LAYER,VIEWPORT,PSIZ} to select the Y/Z/W
197 * components of our VUE header. See brw_vue_map.c for the layout.
198 */
199 static void
200 update_so_info(struct pipe_stream_output_info *so_info,
201 uint64_t outputs_written)
202 {
203 uint8_t reverse_map[64] = {};
204 unsigned slot = 0;
205 while (outputs_written) {
206 reverse_map[slot++] = u_bit_scan64(&outputs_written);
207 }
208
209 for (unsigned i = 0; i < so_info->num_outputs; i++) {
210 struct pipe_stream_output *output = &so_info->output[i];
211
212 /* Map Gallium's condensed "slots" back to real VARYING_SLOT_* enums */
213 output->register_index = reverse_map[output->register_index];
214
215 /* The VUE header contains three scalar fields packed together:
216 * - gl_PointSize is stored in VARYING_SLOT_PSIZ.w
217 * - gl_Layer is stored in VARYING_SLOT_PSIZ.y
218 * - gl_ViewportIndex is stored in VARYING_SLOT_PSIZ.z
219 */
220 switch (output->register_index) {
221 case VARYING_SLOT_LAYER:
222 assert(output->num_components == 1);
223 output->register_index = VARYING_SLOT_PSIZ;
224 output->start_component = 1;
225 break;
226 case VARYING_SLOT_VIEWPORT:
227 assert(output->num_components == 1);
228 output->register_index = VARYING_SLOT_PSIZ;
229 output->start_component = 2;
230 break;
231 case VARYING_SLOT_PSIZ:
232 assert(output->num_components == 1);
233 output->start_component = 3;
234 break;
235 }
236
237 //info->outputs_written |= 1ull << output->register_index;
238 }
239 }
240
241 static void
242 setup_vec4_image_sysval(uint32_t *sysvals, uint32_t idx,
243 unsigned offset, unsigned n)
244 {
245 assert(offset % sizeof(uint32_t) == 0);
246
247 for (unsigned i = 0; i < n; ++i)
248 sysvals[i] = BRW_PARAM_IMAGE(idx, offset / sizeof(uint32_t) + i);
249
250 for (unsigned i = n; i < 4; ++i)
251 sysvals[i] = BRW_PARAM_BUILTIN_ZERO;
252 }
253
254 /**
255 * Associate NIR uniform variables with the prog_data->param[] mechanism
256 * used by the backend. Also, decide which UBOs we'd like to push in an
257 * ideal situation (though the backend can reduce this).
258 */
259 static void
260 iris_setup_uniforms(const struct brw_compiler *compiler,
261 void *mem_ctx,
262 nir_shader *nir,
263 struct brw_stage_prog_data *prog_data,
264 enum brw_param_builtin **out_system_values,
265 unsigned *out_num_system_values,
266 unsigned *out_num_cbufs)
267 {
268 UNUSED const struct gen_device_info *devinfo = compiler->devinfo;
269
270 /* The intel compiler assumes that num_uniforms is in bytes. For
271 * scalar that means 4 bytes per uniform slot.
272 *
273 * Ref: brw_nir_lower_uniforms, type_size_scalar_bytes.
274 */
275 nir->num_uniforms *= 4;
276
277 const unsigned IRIS_MAX_SYSTEM_VALUES =
278 PIPE_MAX_SHADER_IMAGES * BRW_IMAGE_PARAM_SIZE;
279 enum brw_param_builtin *system_values =
280 rzalloc_array(mem_ctx, enum brw_param_builtin, IRIS_MAX_SYSTEM_VALUES);
281 unsigned num_system_values = 0;
282
283 unsigned patch_vert_idx = -1;
284 unsigned ucp_idx[IRIS_MAX_CLIP_PLANES];
285 unsigned img_idx[PIPE_MAX_SHADER_IMAGES];
286 memset(ucp_idx, -1, sizeof(ucp_idx));
287 memset(img_idx, -1, sizeof(img_idx));
288
289 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
290
291 nir_builder b;
292 nir_builder_init(&b, impl);
293
294 b.cursor = nir_before_block(nir_start_block(impl));
295 nir_ssa_def *temp_ubo_name = nir_ssa_undef(&b, 1, 32);
296 nir_ssa_def *temp_const_ubo_name = NULL;
297
298 /* Turn system value intrinsics into uniforms */
299 nir_foreach_block(block, impl) {
300 nir_foreach_instr_safe(instr, block) {
301 if (instr->type != nir_instr_type_intrinsic)
302 continue;
303
304 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
305 nir_ssa_def *offset;
306
307 switch (intrin->intrinsic) {
308 case nir_intrinsic_load_constant: {
309 /* This one is special because it reads from the shader constant
310 * data and not cbuf0 which gallium uploads for us.
311 */
312 b.cursor = nir_before_instr(instr);
313 nir_ssa_def *offset =
314 nir_iadd_imm(&b, nir_ssa_for_src(&b, intrin->src[0], 1),
315 nir_intrinsic_base(intrin));
316
317 if (temp_const_ubo_name == NULL)
318 temp_const_ubo_name = nir_imm_int(&b, 0);
319
320 nir_intrinsic_instr *load_ubo =
321 nir_intrinsic_instr_create(b.shader, nir_intrinsic_load_ubo);
322 load_ubo->num_components = intrin->num_components;
323 load_ubo->src[0] = nir_src_for_ssa(temp_const_ubo_name);
324 load_ubo->src[1] = nir_src_for_ssa(offset);
325 nir_ssa_dest_init(&load_ubo->instr, &load_ubo->dest,
326 intrin->dest.ssa.num_components,
327 intrin->dest.ssa.bit_size,
328 intrin->dest.ssa.name);
329 nir_builder_instr_insert(&b, &load_ubo->instr);
330
331 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
332 nir_src_for_ssa(&load_ubo->dest.ssa));
333 nir_instr_remove(&intrin->instr);
334 continue;
335 }
336 case nir_intrinsic_load_user_clip_plane: {
337 unsigned ucp = nir_intrinsic_ucp_id(intrin);
338
339 if (ucp_idx[ucp] == -1) {
340 ucp_idx[ucp] = num_system_values;
341 num_system_values += 4;
342 }
343
344 for (int i = 0; i < 4; i++) {
345 system_values[ucp_idx[ucp] + i] =
346 BRW_PARAM_BUILTIN_CLIP_PLANE(ucp, i);
347 }
348
349 b.cursor = nir_before_instr(instr);
350 offset = nir_imm_int(&b, ucp_idx[ucp] * sizeof(uint32_t));
351 break;
352 }
353 case nir_intrinsic_load_patch_vertices_in:
354 if (patch_vert_idx == -1)
355 patch_vert_idx = num_system_values++;
356
357 system_values[patch_vert_idx] =
358 BRW_PARAM_BUILTIN_PATCH_VERTICES_IN;
359
360 b.cursor = nir_before_instr(instr);
361 offset = nir_imm_int(&b, patch_vert_idx * sizeof(uint32_t));
362 break;
363 case nir_intrinsic_image_deref_load_param_intel: {
364 assert(devinfo->gen < 9);
365 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
366 nir_variable *var = nir_deref_instr_get_variable(deref);
367
368 if (img_idx[var->data.binding] == -1) {
369 /* GL only allows arrays of arrays of images. */
370 assert(glsl_type_is_image(glsl_without_array(var->type)));
371 unsigned num_images = MAX2(1, glsl_get_aoa_size(var->type));
372
373 for (int i = 0; i < num_images; i++) {
374 const unsigned img = var->data.binding + i;
375
376 img_idx[img] = num_system_values;
377 num_system_values += BRW_IMAGE_PARAM_SIZE;
378
379 uint32_t *img_sv = &system_values[img_idx[img]];
380
381 setup_vec4_image_sysval(
382 img_sv + BRW_IMAGE_PARAM_OFFSET_OFFSET, img,
383 offsetof(struct brw_image_param, offset), 2);
384 setup_vec4_image_sysval(
385 img_sv + BRW_IMAGE_PARAM_SIZE_OFFSET, img,
386 offsetof(struct brw_image_param, size), 3);
387 setup_vec4_image_sysval(
388 img_sv + BRW_IMAGE_PARAM_STRIDE_OFFSET, img,
389 offsetof(struct brw_image_param, stride), 4);
390 setup_vec4_image_sysval(
391 img_sv + BRW_IMAGE_PARAM_TILING_OFFSET, img,
392 offsetof(struct brw_image_param, tiling), 3);
393 setup_vec4_image_sysval(
394 img_sv + BRW_IMAGE_PARAM_SWIZZLING_OFFSET, img,
395 offsetof(struct brw_image_param, swizzling), 2);
396 }
397 }
398
399 b.cursor = nir_before_instr(instr);
400 offset = nir_iadd(&b,
401 get_aoa_deref_offset(&b, deref, BRW_IMAGE_PARAM_SIZE * 4),
402 nir_imm_int(&b, img_idx[var->data.binding] * 4 +
403 nir_intrinsic_base(intrin) * 16));
404 break;
405 }
406 default:
407 continue;
408 }
409
410 unsigned comps = nir_intrinsic_dest_components(intrin);
411
412 nir_intrinsic_instr *load =
413 nir_intrinsic_instr_create(nir, nir_intrinsic_load_ubo);
414 load->num_components = comps;
415 load->src[0] = nir_src_for_ssa(temp_ubo_name);
416 load->src[1] = nir_src_for_ssa(offset);
417 nir_ssa_dest_init(&load->instr, &load->dest, comps, 32, NULL);
418 nir_builder_instr_insert(&b, &load->instr);
419 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
420 nir_src_for_ssa(&load->dest.ssa));
421 nir_instr_remove(instr);
422 }
423 }
424
425 nir_validate_shader(nir, "before remapping");
426
427 /* Uniforms are stored in constant buffer 0, the
428 * user-facing UBOs are indexed by one. So if any constant buffer is
429 * needed, the constant buffer 0 will be needed, so account for it.
430 */
431 unsigned num_cbufs = nir->info.num_ubos;
432 if (num_cbufs || nir->num_uniforms)
433 num_cbufs++;
434
435 /* Place the new params in a new cbuf. */
436 if (num_system_values > 0) {
437 unsigned sysval_cbuf_index = num_cbufs;
438 num_cbufs++;
439
440 system_values = reralloc(mem_ctx, system_values, enum brw_param_builtin,
441 num_system_values);
442
443 nir_foreach_block(block, impl) {
444 nir_foreach_instr_safe(instr, block) {
445 if (instr->type != nir_instr_type_intrinsic)
446 continue;
447
448 nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);
449
450 if (load->intrinsic != nir_intrinsic_load_ubo)
451 continue;
452
453 b.cursor = nir_before_instr(instr);
454
455 assert(load->src[0].is_ssa);
456
457 if (load->src[0].ssa == temp_ubo_name) {
458 nir_ssa_def *imm = nir_imm_int(&b, sysval_cbuf_index);
459 nir_instr_rewrite_src(instr, &load->src[0],
460 nir_src_for_ssa(imm));
461 }
462 }
463 }
464
465 /* We need to fold the new iadds for brw_nir_analyze_ubo_ranges */
466 nir_opt_constant_folding(nir);
467 } else {
468 ralloc_free(system_values);
469 system_values = NULL;
470 }
471
472 assert(num_cbufs < PIPE_MAX_CONSTANT_BUFFERS);
473 nir_validate_shader(nir, "after remap");
474
475 /* We don't use params[], but fs_visitor::nir_setup_uniforms() asserts
476 * about it for compute shaders, so go ahead and make some fake ones
477 * which the backend will dead code eliminate.
478 */
479 prog_data->nr_params = nir->num_uniforms / 4;
480 prog_data->param = rzalloc_array(mem_ctx, uint32_t, prog_data->nr_params);
481
482 /* Constant loads (if any) need to go at the end of the constant buffers so
483 * we need to know num_cbufs before we can lower to them.
484 */
485 if (temp_const_ubo_name != NULL) {
486 nir_load_const_instr *const_ubo_index =
487 nir_instr_as_load_const(temp_const_ubo_name->parent_instr);
488 assert(const_ubo_index->def.bit_size == 32);
489 const_ubo_index->value[0].u32 = num_cbufs;
490 }
491
492 *out_system_values = system_values;
493 *out_num_system_values = num_system_values;
494 *out_num_cbufs = num_cbufs;
495 }
496
497 static const char *surface_group_names[] = {
498 [IRIS_SURFACE_GROUP_RENDER_TARGET] = "render target",
499 [IRIS_SURFACE_GROUP_CS_WORK_GROUPS] = "CS work groups",
500 [IRIS_SURFACE_GROUP_TEXTURE] = "texture",
501 [IRIS_SURFACE_GROUP_UBO] = "ubo",
502 [IRIS_SURFACE_GROUP_SSBO] = "ssbo",
503 [IRIS_SURFACE_GROUP_IMAGE] = "image",
504 };
505
506 static void
507 iris_print_binding_table(FILE *fp, const char *name,
508 const struct iris_binding_table *bt)
509 {
510 STATIC_ASSERT(ARRAY_SIZE(surface_group_names) == IRIS_SURFACE_GROUP_COUNT);
511
512 uint32_t total = 0;
513 uint32_t compacted = 0;
514
515 for (int i = 0; i < IRIS_SURFACE_GROUP_COUNT; i++) {
516 uint32_t size = bt->sizes[i];
517 total += size;
518 if (size)
519 compacted += util_bitcount64(bt->used_mask[i]);
520 }
521
522 if (total == 0) {
523 fprintf(fp, "Binding table for %s is empty\n\n", name);
524 return;
525 }
526
527 if (total != compacted) {
528 fprintf(fp, "Binding table for %s "
529 "(compacted to %u entries from %u entries)\n",
530 name, compacted, total);
531 } else {
532 fprintf(fp, "Binding table for %s (%u entries)\n", name, total);
533 }
534
535 uint32_t entry = 0;
536 for (int i = 0; i < IRIS_SURFACE_GROUP_COUNT; i++) {
537 uint64_t mask = bt->used_mask[i];
538 while (mask) {
539 int index = u_bit_scan64(&mask);
540 fprintf(fp, " [%u] %s #%d\n", entry++, surface_group_names[i], index);
541 }
542 }
543 fprintf(fp, "\n");
544 }
545
546 enum {
547 /* Max elements in a surface group. */
548 SURFACE_GROUP_MAX_ELEMENTS = 64,
549 };
550
551 /**
552 * Map a <group, index> pair to a binding table index.
553 *
554 * For example: <UBO, 5> => binding table index 12
555 */
556 uint32_t
557 iris_group_index_to_bti(const struct iris_binding_table *bt,
558 enum iris_surface_group group, uint32_t index)
559 {
560 assert(index < bt->sizes[group]);
561 uint64_t mask = bt->used_mask[group];
562 uint64_t bit = 1ull << index;
563 if (bit & mask) {
564 return bt->offsets[group] + util_bitcount64((bit - 1) & mask);
565 } else {
566 return IRIS_SURFACE_NOT_USED;
567 }
568 }
569
570 /**
571 * Map a binding table index back to a <group, index> pair.
572 *
573 * For example: binding table index 12 => <UBO, 5>
574 */
575 uint32_t
576 iris_bti_to_group_index(const struct iris_binding_table *bt,
577 enum iris_surface_group group, uint32_t bti)
578 {
579 uint64_t used_mask = bt->used_mask[group];
580 assert(bti >= bt->offsets[group]);
581
582 uint32_t c = bti - bt->offsets[group];
583 while (used_mask) {
584 int i = u_bit_scan64(&used_mask);
585 if (c == 0)
586 return i;
587 c--;
588 }
589
590 return IRIS_SURFACE_NOT_USED;
591 }
592
593 static void
594 rewrite_src_with_bti(nir_builder *b, struct iris_binding_table *bt,
595 nir_instr *instr, nir_src *src,
596 enum iris_surface_group group)
597 {
598 assert(bt->sizes[group] > 0);
599
600 b->cursor = nir_before_instr(instr);
601 nir_ssa_def *bti;
602 if (nir_src_is_const(*src)) {
603 uint32_t index = nir_src_as_uint(*src);
604 bti = nir_imm_intN_t(b, iris_group_index_to_bti(bt, group, index),
605 src->ssa->bit_size);
606 } else {
607 /* Indirect usage makes all the surfaces of the group to be available,
608 * so we can just add the base.
609 */
610 assert(bt->used_mask[group] == BITFIELD64_MASK(bt->sizes[group]));
611 bti = nir_iadd_imm(b, src->ssa, bt->offsets[group]);
612 }
613 nir_instr_rewrite_src(instr, src, nir_src_for_ssa(bti));
614 }
615
616 static void
617 mark_used_with_src(struct iris_binding_table *bt, nir_src *src,
618 enum iris_surface_group group)
619 {
620 assert(bt->sizes[group] > 0);
621
622 if (nir_src_is_const(*src)) {
623 uint64_t index = nir_src_as_uint(*src);
624 assert(index < bt->sizes[group]);
625 bt->used_mask[group] |= 1ull << index;
626 } else {
627 /* There's an indirect usage, we need all the surfaces. */
628 bt->used_mask[group] = BITFIELD64_MASK(bt->sizes[group]);
629 }
630 }
631
632 static bool
633 skip_compacting_binding_tables(void)
634 {
635 static int skip = -1;
636 if (skip < 0)
637 skip = env_var_as_boolean("INTEL_DISABLE_COMPACT_BINDING_TABLE", false);
638 return skip;
639 }
640
641 /**
642 * Set up the binding table indices and apply to the shader.
643 */
644 static void
645 iris_setup_binding_table(struct nir_shader *nir,
646 struct iris_binding_table *bt,
647 unsigned num_render_targets,
648 unsigned num_system_values,
649 unsigned num_cbufs)
650 {
651 const struct shader_info *info = &nir->info;
652
653 memset(bt, 0, sizeof(*bt));
654
655 /* Set the sizes for each surface group. For some groups, we already know
656 * upfront how many will be used, so mark them.
657 */
658 if (info->stage == MESA_SHADER_FRAGMENT) {
659 bt->sizes[IRIS_SURFACE_GROUP_RENDER_TARGET] = num_render_targets;
660 /* All render targets used. */
661 bt->used_mask[IRIS_SURFACE_GROUP_RENDER_TARGET] =
662 BITFIELD64_MASK(num_render_targets);
663 } else if (info->stage == MESA_SHADER_COMPUTE) {
664 bt->sizes[IRIS_SURFACE_GROUP_CS_WORK_GROUPS] = 1;
665 }
666
667 bt->sizes[IRIS_SURFACE_GROUP_TEXTURE] = util_last_bit(info->textures_used);
668 bt->used_mask[IRIS_SURFACE_GROUP_TEXTURE] = info->textures_used;
669
670 bt->sizes[IRIS_SURFACE_GROUP_IMAGE] = info->num_images;
671
672 /* Allocate an extra slot in the UBO section for NIR constants.
673 * Binding table compaction will remove it if unnecessary.
674 *
675 * We don't include them in iris_compiled_shader::num_cbufs because
676 * they are uploaded separately from shs->constbuf[], but from a shader
677 * point of view, they're another UBO (at the end of the section).
678 */
679 bt->sizes[IRIS_SURFACE_GROUP_UBO] = num_cbufs + 1;
680
681 /* The first IRIS_MAX_ABOs indices in the SSBO group are for atomics, real
682 * SSBOs start after that. Compaction will remove unused ABOs.
683 */
684 bt->sizes[IRIS_SURFACE_GROUP_SSBO] = IRIS_MAX_ABOS + info->num_ssbos;
685
686 for (int i = 0; i < IRIS_SURFACE_GROUP_COUNT; i++)
687 assert(bt->sizes[i] <= SURFACE_GROUP_MAX_ELEMENTS);
688
689 /* Mark surfaces used for the cases we don't have the information available
690 * upfront.
691 */
692 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
693 nir_foreach_block (block, impl) {
694 nir_foreach_instr (instr, block) {
695 if (instr->type != nir_instr_type_intrinsic)
696 continue;
697
698 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
699 switch (intrin->intrinsic) {
700 case nir_intrinsic_load_num_work_groups:
701 bt->used_mask[IRIS_SURFACE_GROUP_CS_WORK_GROUPS] = 1;
702 break;
703
704 case nir_intrinsic_image_size:
705 case nir_intrinsic_image_load:
706 case nir_intrinsic_image_store:
707 case nir_intrinsic_image_atomic_add:
708 case nir_intrinsic_image_atomic_min:
709 case nir_intrinsic_image_atomic_max:
710 case nir_intrinsic_image_atomic_and:
711 case nir_intrinsic_image_atomic_or:
712 case nir_intrinsic_image_atomic_xor:
713 case nir_intrinsic_image_atomic_exchange:
714 case nir_intrinsic_image_atomic_comp_swap:
715 case nir_intrinsic_image_load_raw_intel:
716 case nir_intrinsic_image_store_raw_intel:
717 mark_used_with_src(bt, &intrin->src[0], IRIS_SURFACE_GROUP_IMAGE);
718 break;
719
720 case nir_intrinsic_load_ubo:
721 mark_used_with_src(bt, &intrin->src[0], IRIS_SURFACE_GROUP_UBO);
722 break;
723
724 case nir_intrinsic_store_ssbo:
725 mark_used_with_src(bt, &intrin->src[1], IRIS_SURFACE_GROUP_SSBO);
726 break;
727
728 case nir_intrinsic_get_buffer_size:
729 case nir_intrinsic_ssbo_atomic_add:
730 case nir_intrinsic_ssbo_atomic_imin:
731 case nir_intrinsic_ssbo_atomic_umin:
732 case nir_intrinsic_ssbo_atomic_imax:
733 case nir_intrinsic_ssbo_atomic_umax:
734 case nir_intrinsic_ssbo_atomic_and:
735 case nir_intrinsic_ssbo_atomic_or:
736 case nir_intrinsic_ssbo_atomic_xor:
737 case nir_intrinsic_ssbo_atomic_exchange:
738 case nir_intrinsic_ssbo_atomic_comp_swap:
739 case nir_intrinsic_ssbo_atomic_fmin:
740 case nir_intrinsic_ssbo_atomic_fmax:
741 case nir_intrinsic_ssbo_atomic_fcomp_swap:
742 case nir_intrinsic_load_ssbo:
743 mark_used_with_src(bt, &intrin->src[0], IRIS_SURFACE_GROUP_SSBO);
744 break;
745
746 default:
747 break;
748 }
749 }
750 }
751
752 /* When disable we just mark everything as used. */
753 if (unlikely(skip_compacting_binding_tables())) {
754 for (int i = 0; i < IRIS_SURFACE_GROUP_COUNT; i++)
755 bt->used_mask[i] = BITFIELD64_MASK(bt->sizes[i]);
756 }
757
758 /* Calculate the offsets and the binding table size based on the used
759 * surfaces. After this point, the functions to go between "group indices"
760 * and binding table indices can be used.
761 */
762 uint32_t next = 0;
763 for (int i = 0; i < IRIS_SURFACE_GROUP_COUNT; i++) {
764 if (bt->used_mask[i] != 0) {
765 bt->offsets[i] = next;
766 next += util_bitcount64(bt->used_mask[i]);
767 }
768 }
769 bt->size_bytes = next * 4;
770
771 if (unlikely(INTEL_DEBUG & DEBUG_BT)) {
772 iris_print_binding_table(stderr, gl_shader_stage_name(info->stage), bt);
773 }
774
775 /* Apply the binding table indices. The backend compiler is not expected
776 * to change those, as we haven't set any of the *_start entries in brw
777 * binding_table.
778 */
779 nir_builder b;
780 nir_builder_init(&b, impl);
781
782 nir_foreach_block (block, impl) {
783 nir_foreach_instr (instr, block) {
784 if (instr->type == nir_instr_type_tex) {
785 nir_tex_instr *tex = nir_instr_as_tex(instr);
786 tex->texture_index =
787 iris_group_index_to_bti(bt, IRIS_SURFACE_GROUP_TEXTURE,
788 tex->texture_index);
789 continue;
790 }
791
792 if (instr->type != nir_instr_type_intrinsic)
793 continue;
794
795 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
796 switch (intrin->intrinsic) {
797 case nir_intrinsic_image_size:
798 case nir_intrinsic_image_load:
799 case nir_intrinsic_image_store:
800 case nir_intrinsic_image_atomic_add:
801 case nir_intrinsic_image_atomic_min:
802 case nir_intrinsic_image_atomic_max:
803 case nir_intrinsic_image_atomic_and:
804 case nir_intrinsic_image_atomic_or:
805 case nir_intrinsic_image_atomic_xor:
806 case nir_intrinsic_image_atomic_exchange:
807 case nir_intrinsic_image_atomic_comp_swap:
808 case nir_intrinsic_image_load_raw_intel:
809 case nir_intrinsic_image_store_raw_intel:
810 rewrite_src_with_bti(&b, bt, instr, &intrin->src[0],
811 IRIS_SURFACE_GROUP_IMAGE);
812 break;
813
814 case nir_intrinsic_load_ubo:
815 rewrite_src_with_bti(&b, bt, instr, &intrin->src[0],
816 IRIS_SURFACE_GROUP_UBO);
817 break;
818
819 case nir_intrinsic_store_ssbo:
820 rewrite_src_with_bti(&b, bt, instr, &intrin->src[1],
821 IRIS_SURFACE_GROUP_SSBO);
822 break;
823
824 case nir_intrinsic_get_buffer_size:
825 case nir_intrinsic_ssbo_atomic_add:
826 case nir_intrinsic_ssbo_atomic_imin:
827 case nir_intrinsic_ssbo_atomic_umin:
828 case nir_intrinsic_ssbo_atomic_imax:
829 case nir_intrinsic_ssbo_atomic_umax:
830 case nir_intrinsic_ssbo_atomic_and:
831 case nir_intrinsic_ssbo_atomic_or:
832 case nir_intrinsic_ssbo_atomic_xor:
833 case nir_intrinsic_ssbo_atomic_exchange:
834 case nir_intrinsic_ssbo_atomic_comp_swap:
835 case nir_intrinsic_ssbo_atomic_fmin:
836 case nir_intrinsic_ssbo_atomic_fmax:
837 case nir_intrinsic_ssbo_atomic_fcomp_swap:
838 case nir_intrinsic_load_ssbo:
839 rewrite_src_with_bti(&b, bt, instr, &intrin->src[0],
840 IRIS_SURFACE_GROUP_SSBO);
841 break;
842
843 default:
844 break;
845 }
846 }
847 }
848 }
849
850 static void
851 iris_debug_recompile(struct iris_context *ice,
852 struct shader_info *info,
853 const struct brw_base_prog_key *key)
854 {
855 struct iris_screen *screen = (struct iris_screen *) ice->ctx.screen;
856 const struct brw_compiler *c = screen->compiler;
857
858 if (!info)
859 return;
860
861 c->shader_perf_log(&ice->dbg, "Recompiling %s shader for program %s: %s\n",
862 _mesa_shader_stage_to_string(info->stage),
863 info->name ? info->name : "(no identifier)",
864 info->label ? info->label : "");
865
866 const void *old_key =
867 iris_find_previous_compile(ice, info->stage, key->program_string_id);
868
869 brw_debug_key_recompile(c, &ice->dbg, info->stage, old_key, key);
870 }
871
872 /**
873 * Get the shader for the last enabled geometry stage.
874 *
875 * This stage is the one which will feed stream output and the rasterizer.
876 */
877 static gl_shader_stage
878 last_vue_stage(struct iris_context *ice)
879 {
880 if (ice->shaders.uncompiled[MESA_SHADER_GEOMETRY])
881 return MESA_SHADER_GEOMETRY;
882
883 if (ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL])
884 return MESA_SHADER_TESS_EVAL;
885
886 return MESA_SHADER_VERTEX;
887 }
888
889 /**
890 * Compile a vertex shader, and upload the assembly.
891 */
892 static struct iris_compiled_shader *
893 iris_compile_vs(struct iris_context *ice,
894 struct iris_uncompiled_shader *ish,
895 const struct brw_vs_prog_key *key)
896 {
897 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
898 const struct brw_compiler *compiler = screen->compiler;
899 const struct gen_device_info *devinfo = &screen->devinfo;
900 void *mem_ctx = ralloc_context(NULL);
901 struct brw_vs_prog_data *vs_prog_data =
902 rzalloc(mem_ctx, struct brw_vs_prog_data);
903 struct brw_vue_prog_data *vue_prog_data = &vs_prog_data->base;
904 struct brw_stage_prog_data *prog_data = &vue_prog_data->base;
905 enum brw_param_builtin *system_values;
906 unsigned num_system_values;
907 unsigned num_cbufs;
908
909 nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);
910
911 if (key->nr_userclip_plane_consts) {
912 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
913 nir_lower_clip_vs(nir, (1 << key->nr_userclip_plane_consts) - 1, true);
914 nir_lower_io_to_temporaries(nir, impl, true, false);
915 nir_lower_global_vars_to_local(nir);
916 nir_lower_vars_to_ssa(nir);
917 nir_shader_gather_info(nir, impl);
918 }
919
920 prog_data->use_alt_mode = ish->use_alt_mode;
921
922 iris_setup_uniforms(compiler, mem_ctx, nir, prog_data, &system_values,
923 &num_system_values, &num_cbufs);
924
925 struct iris_binding_table bt;
926 iris_setup_binding_table(nir, &bt, /* num_render_targets */ 0,
927 num_system_values, num_cbufs);
928
929 brw_nir_analyze_ubo_ranges(compiler, nir, NULL, prog_data->ubo_ranges);
930
931 brw_compute_vue_map(devinfo,
932 &vue_prog_data->vue_map, nir->info.outputs_written,
933 nir->info.separate_shader);
934
935 /* Don't tell the backend about our clip plane constants, we've already
936 * lowered them in NIR and we don't want it doing it again.
937 */
938 struct brw_vs_prog_key key_no_ucp = *key;
939 key_no_ucp.nr_userclip_plane_consts = 0;
940
941 char *error_str = NULL;
942 const unsigned *program =
943 brw_compile_vs(compiler, &ice->dbg, mem_ctx, &key_no_ucp, vs_prog_data,
944 nir, -1, &error_str);
945 if (program == NULL) {
946 dbg_printf("Failed to compile vertex shader: %s\n", error_str);
947 ralloc_free(mem_ctx);
948 return false;
949 }
950
951 if (ish->compiled_once) {
952 iris_debug_recompile(ice, &nir->info, &key->base);
953 } else {
954 ish->compiled_once = true;
955 }
956
957 uint32_t *so_decls =
958 ice->vtbl.create_so_decl_list(&ish->stream_output,
959 &vue_prog_data->vue_map);
960
961 struct iris_compiled_shader *shader =
962 iris_upload_shader(ice, IRIS_CACHE_VS, sizeof(*key), key, program,
963 prog_data, so_decls, system_values, num_system_values,
964 num_cbufs, &bt);
965
966 iris_disk_cache_store(screen->disk_cache, ish, shader, key, sizeof(*key));
967
968 ralloc_free(mem_ctx);
969 return shader;
970 }
971
972 /**
973 * Update the current vertex shader variant.
974 *
975 * Fill out the key, look in the cache, compile and bind if needed.
976 */
977 static void
978 iris_update_compiled_vs(struct iris_context *ice)
979 {
980 struct iris_shader_state *shs = &ice->state.shaders[MESA_SHADER_VERTEX];
981 struct iris_uncompiled_shader *ish =
982 ice->shaders.uncompiled[MESA_SHADER_VERTEX];
983 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
984 const struct gen_device_info *devinfo = &screen->devinfo;
985
986 struct brw_vs_prog_key key = { KEY_INIT(devinfo->gen) };
987 ice->vtbl.populate_vs_key(ice, &ish->nir->info, last_vue_stage(ice), &key);
988
989 struct iris_compiled_shader *old = ice->shaders.prog[IRIS_CACHE_VS];
990 struct iris_compiled_shader *shader =
991 iris_find_cached_shader(ice, IRIS_CACHE_VS, sizeof(key), &key);
992
993 if (!shader)
994 shader = iris_disk_cache_retrieve(ice, ish, &key, sizeof(key));
995
996 if (!shader)
997 shader = iris_compile_vs(ice, ish, &key);
998
999 if (old != shader) {
1000 ice->shaders.prog[IRIS_CACHE_VS] = shader;
1001 ice->state.dirty |= IRIS_DIRTY_VS |
1002 IRIS_DIRTY_BINDINGS_VS |
1003 IRIS_DIRTY_CONSTANTS_VS |
1004 IRIS_DIRTY_VF_SGVS;
1005 shs->sysvals_need_upload = true;
1006
1007 const struct brw_vs_prog_data *vs_prog_data =
1008 (void *) shader->prog_data;
1009 const bool uses_draw_params = vs_prog_data->uses_firstvertex ||
1010 vs_prog_data->uses_baseinstance;
1011 const bool uses_derived_draw_params = vs_prog_data->uses_drawid ||
1012 vs_prog_data->uses_is_indexed_draw;
1013 const bool needs_sgvs_element = uses_draw_params ||
1014 vs_prog_data->uses_instanceid ||
1015 vs_prog_data->uses_vertexid;
1016 bool needs_edge_flag = false;
1017 nir_foreach_variable(var, &ish->nir->inputs) {
1018 if (var->data.location == VERT_ATTRIB_EDGEFLAG)
1019 needs_edge_flag = true;
1020 }
1021
1022 if (ice->state.vs_uses_draw_params != uses_draw_params ||
1023 ice->state.vs_uses_derived_draw_params != uses_derived_draw_params ||
1024 ice->state.vs_needs_edge_flag != needs_edge_flag) {
1025 ice->state.dirty |= IRIS_DIRTY_VERTEX_BUFFERS |
1026 IRIS_DIRTY_VERTEX_ELEMENTS;
1027 }
1028 ice->state.vs_uses_draw_params = uses_draw_params;
1029 ice->state.vs_uses_derived_draw_params = uses_derived_draw_params;
1030 ice->state.vs_needs_sgvs_element = needs_sgvs_element;
1031 ice->state.vs_needs_edge_flag = needs_edge_flag;
1032 }
1033 }
1034
1035 /**
1036 * Get the shader_info for a given stage, or NULL if the stage is disabled.
1037 */
1038 const struct shader_info *
1039 iris_get_shader_info(const struct iris_context *ice, gl_shader_stage stage)
1040 {
1041 const struct iris_uncompiled_shader *ish = ice->shaders.uncompiled[stage];
1042
1043 if (!ish)
1044 return NULL;
1045
1046 const nir_shader *nir = ish->nir;
1047 return &nir->info;
1048 }
1049
1050 /**
1051 * Get the union of TCS output and TES input slots.
1052 *
1053 * TCS and TES need to agree on a common URB entry layout. In particular,
1054 * the data for all patch vertices is stored in a single URB entry (unlike
1055 * GS which has one entry per input vertex). This means that per-vertex
1056 * array indexing needs a stride.
1057 *
1058 * SSO requires locations to match, but doesn't require the number of
1059 * outputs/inputs to match (in fact, the TCS often has extra outputs).
1060 * So, we need to take the extra step of unifying these on the fly.
1061 */
1062 static void
1063 get_unified_tess_slots(const struct iris_context *ice,
1064 uint64_t *per_vertex_slots,
1065 uint32_t *per_patch_slots)
1066 {
1067 const struct shader_info *tcs =
1068 iris_get_shader_info(ice, MESA_SHADER_TESS_CTRL);
1069 const struct shader_info *tes =
1070 iris_get_shader_info(ice, MESA_SHADER_TESS_EVAL);
1071
1072 *per_vertex_slots = tes->inputs_read;
1073 *per_patch_slots = tes->patch_inputs_read;
1074
1075 if (tcs) {
1076 *per_vertex_slots |= tcs->outputs_written;
1077 *per_patch_slots |= tcs->patch_outputs_written;
1078 }
1079 }
1080
1081 /**
1082 * Compile a tessellation control shader, and upload the assembly.
1083 */
1084 static struct iris_compiled_shader *
1085 iris_compile_tcs(struct iris_context *ice,
1086 struct iris_uncompiled_shader *ish,
1087 const struct brw_tcs_prog_key *key)
1088 {
1089 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
1090 const struct brw_compiler *compiler = screen->compiler;
1091 const struct nir_shader_compiler_options *options =
1092 compiler->glsl_compiler_options[MESA_SHADER_TESS_CTRL].NirOptions;
1093 void *mem_ctx = ralloc_context(NULL);
1094 struct brw_tcs_prog_data *tcs_prog_data =
1095 rzalloc(mem_ctx, struct brw_tcs_prog_data);
1096 struct brw_vue_prog_data *vue_prog_data = &tcs_prog_data->base;
1097 struct brw_stage_prog_data *prog_data = &vue_prog_data->base;
1098 enum brw_param_builtin *system_values = NULL;
1099 unsigned num_system_values = 0;
1100 unsigned num_cbufs = 0;
1101
1102 nir_shader *nir;
1103
1104 struct iris_binding_table bt;
1105
1106 if (ish) {
1107 nir = nir_shader_clone(mem_ctx, ish->nir);
1108
1109 iris_setup_uniforms(compiler, mem_ctx, nir, prog_data, &system_values,
1110 &num_system_values, &num_cbufs);
1111 iris_setup_binding_table(nir, &bt, /* num_render_targets */ 0,
1112 num_system_values, num_cbufs);
1113 brw_nir_analyze_ubo_ranges(compiler, nir, NULL, prog_data->ubo_ranges);
1114 } else {
1115 nir = brw_nir_create_passthrough_tcs(mem_ctx, compiler, options, key);
1116
1117 /* Reserve space for passing the default tess levels as constants. */
1118 num_cbufs = 1;
1119 num_system_values = 8;
1120 system_values =
1121 rzalloc_array(mem_ctx, enum brw_param_builtin, num_system_values);
1122 prog_data->param = rzalloc_array(mem_ctx, uint32_t, num_system_values);
1123 prog_data->nr_params = num_system_values;
1124
1125 if (key->tes_primitive_mode == GL_QUADS) {
1126 for (int i = 0; i < 4; i++)
1127 system_values[7 - i] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X + i;
1128
1129 system_values[3] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_X;
1130 system_values[2] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_Y;
1131 } else if (key->tes_primitive_mode == GL_TRIANGLES) {
1132 for (int i = 0; i < 3; i++)
1133 system_values[7 - i] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X + i;
1134
1135 system_values[4] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_X;
1136 } else {
1137 assert(key->tes_primitive_mode == GL_ISOLINES);
1138 system_values[7] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_Y;
1139 system_values[6] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X;
1140 }
1141
1142 /* Manually setup the TCS binding table. */
1143 memset(&bt, 0, sizeof(bt));
1144 bt.sizes[IRIS_SURFACE_GROUP_UBO] = 1;
1145 bt.used_mask[IRIS_SURFACE_GROUP_UBO] = 1;
1146 bt.size_bytes = 4;
1147
1148 prog_data->ubo_ranges[0].length = 1;
1149 }
1150
1151 char *error_str = NULL;
1152 const unsigned *program =
1153 brw_compile_tcs(compiler, &ice->dbg, mem_ctx, key, tcs_prog_data, nir,
1154 -1, &error_str);
1155 if (program == NULL) {
1156 dbg_printf("Failed to compile control shader: %s\n", error_str);
1157 ralloc_free(mem_ctx);
1158 return false;
1159 }
1160
1161 if (ish) {
1162 if (ish->compiled_once) {
1163 iris_debug_recompile(ice, &nir->info, &key->base);
1164 } else {
1165 ish->compiled_once = true;
1166 }
1167 }
1168
1169 struct iris_compiled_shader *shader =
1170 iris_upload_shader(ice, IRIS_CACHE_TCS, sizeof(*key), key, program,
1171 prog_data, NULL, system_values, num_system_values,
1172 num_cbufs, &bt);
1173
1174 if (ish)
1175 iris_disk_cache_store(screen->disk_cache, ish, shader, key, sizeof(*key));
1176
1177 ralloc_free(mem_ctx);
1178 return shader;
1179 }
1180
1181 /**
1182 * Update the current tessellation control shader variant.
1183 *
1184 * Fill out the key, look in the cache, compile and bind if needed.
1185 */
1186 static void
1187 iris_update_compiled_tcs(struct iris_context *ice)
1188 {
1189 struct iris_shader_state *shs = &ice->state.shaders[MESA_SHADER_TESS_CTRL];
1190 struct iris_uncompiled_shader *tcs =
1191 ice->shaders.uncompiled[MESA_SHADER_TESS_CTRL];
1192 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
1193 const struct brw_compiler *compiler = screen->compiler;
1194 const struct gen_device_info *devinfo = &screen->devinfo;
1195
1196 const struct shader_info *tes_info =
1197 iris_get_shader_info(ice, MESA_SHADER_TESS_EVAL);
1198 struct brw_tcs_prog_key key = {
1199 KEY_INIT_NO_ID(devinfo->gen),
1200 .base.program_string_id = tcs ? tcs->program_id : 0,
1201 .tes_primitive_mode = tes_info->tess.primitive_mode,
1202 .input_vertices =
1203 !tcs || compiler->use_tcs_8_patch ? ice->state.vertices_per_patch : 0,
1204 };
1205 get_unified_tess_slots(ice, &key.outputs_written,
1206 &key.patch_outputs_written);
1207 ice->vtbl.populate_tcs_key(ice, &key);
1208
1209 struct iris_compiled_shader *old = ice->shaders.prog[IRIS_CACHE_TCS];
1210 struct iris_compiled_shader *shader =
1211 iris_find_cached_shader(ice, IRIS_CACHE_TCS, sizeof(key), &key);
1212
1213 if (tcs && !shader)
1214 shader = iris_disk_cache_retrieve(ice, tcs, &key, sizeof(key));
1215
1216 if (!shader)
1217 shader = iris_compile_tcs(ice, tcs, &key);
1218
1219 if (old != shader) {
1220 ice->shaders.prog[IRIS_CACHE_TCS] = shader;
1221 ice->state.dirty |= IRIS_DIRTY_TCS |
1222 IRIS_DIRTY_BINDINGS_TCS |
1223 IRIS_DIRTY_CONSTANTS_TCS;
1224 shs->sysvals_need_upload = true;
1225 }
1226 }
1227
1228 /**
1229 * Compile a tessellation evaluation shader, and upload the assembly.
1230 */
1231 static struct iris_compiled_shader *
1232 iris_compile_tes(struct iris_context *ice,
1233 struct iris_uncompiled_shader *ish,
1234 const struct brw_tes_prog_key *key)
1235 {
1236 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
1237 const struct brw_compiler *compiler = screen->compiler;
1238 void *mem_ctx = ralloc_context(NULL);
1239 struct brw_tes_prog_data *tes_prog_data =
1240 rzalloc(mem_ctx, struct brw_tes_prog_data);
1241 struct brw_vue_prog_data *vue_prog_data = &tes_prog_data->base;
1242 struct brw_stage_prog_data *prog_data = &vue_prog_data->base;
1243 enum brw_param_builtin *system_values;
1244 unsigned num_system_values;
1245 unsigned num_cbufs;
1246
1247 nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);
1248
1249 if (key->nr_userclip_plane_consts) {
1250 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
1251 nir_lower_clip_vs(nir, (1 << key->nr_userclip_plane_consts) - 1, true);
1252 nir_lower_io_to_temporaries(nir, impl, true, false);
1253 nir_lower_global_vars_to_local(nir);
1254 nir_lower_vars_to_ssa(nir);
1255 nir_shader_gather_info(nir, impl);
1256 }
1257
1258 iris_setup_uniforms(compiler, mem_ctx, nir, prog_data, &system_values,
1259 &num_system_values, &num_cbufs);
1260
1261 struct iris_binding_table bt;
1262 iris_setup_binding_table(nir, &bt, /* num_render_targets */ 0,
1263 num_system_values, num_cbufs);
1264
1265 brw_nir_analyze_ubo_ranges(compiler, nir, NULL, prog_data->ubo_ranges);
1266
1267 struct brw_vue_map input_vue_map;
1268 brw_compute_tess_vue_map(&input_vue_map, key->inputs_read,
1269 key->patch_inputs_read);
1270
1271 char *error_str = NULL;
1272 const unsigned *program =
1273 brw_compile_tes(compiler, &ice->dbg, mem_ctx, key, &input_vue_map,
1274 tes_prog_data, nir, NULL, -1, &error_str);
1275 if (program == NULL) {
1276 dbg_printf("Failed to compile evaluation shader: %s\n", error_str);
1277 ralloc_free(mem_ctx);
1278 return false;
1279 }
1280
1281 if (ish->compiled_once) {
1282 iris_debug_recompile(ice, &nir->info, &key->base);
1283 } else {
1284 ish->compiled_once = true;
1285 }
1286
1287 uint32_t *so_decls =
1288 ice->vtbl.create_so_decl_list(&ish->stream_output,
1289 &vue_prog_data->vue_map);
1290
1291
1292 struct iris_compiled_shader *shader =
1293 iris_upload_shader(ice, IRIS_CACHE_TES, sizeof(*key), key, program,
1294 prog_data, so_decls, system_values, num_system_values,
1295 num_cbufs, &bt);
1296
1297 iris_disk_cache_store(screen->disk_cache, ish, shader, key, sizeof(*key));
1298
1299 ralloc_free(mem_ctx);
1300 return shader;
1301 }
1302
1303 /**
1304 * Update the current tessellation evaluation shader variant.
1305 *
1306 * Fill out the key, look in the cache, compile and bind if needed.
1307 */
1308 static void
1309 iris_update_compiled_tes(struct iris_context *ice)
1310 {
1311 struct iris_shader_state *shs = &ice->state.shaders[MESA_SHADER_TESS_EVAL];
1312 struct iris_uncompiled_shader *ish =
1313 ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL];
1314 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
1315 const struct gen_device_info *devinfo = &screen->devinfo;
1316
1317 struct brw_tes_prog_key key = { KEY_INIT(devinfo->gen) };
1318 get_unified_tess_slots(ice, &key.inputs_read, &key.patch_inputs_read);
1319 ice->vtbl.populate_tes_key(ice, &ish->nir->info, last_vue_stage(ice), &key);
1320
1321 struct iris_compiled_shader *old = ice->shaders.prog[IRIS_CACHE_TES];
1322 struct iris_compiled_shader *shader =
1323 iris_find_cached_shader(ice, IRIS_CACHE_TES, sizeof(key), &key);
1324
1325 if (!shader)
1326 shader = iris_disk_cache_retrieve(ice, ish, &key, sizeof(key));
1327
1328 if (!shader)
1329 shader = iris_compile_tes(ice, ish, &key);
1330
1331 if (old != shader) {
1332 ice->shaders.prog[IRIS_CACHE_TES] = shader;
1333 ice->state.dirty |= IRIS_DIRTY_TES |
1334 IRIS_DIRTY_BINDINGS_TES |
1335 IRIS_DIRTY_CONSTANTS_TES;
1336 shs->sysvals_need_upload = true;
1337 }
1338
1339 /* TODO: Could compare and avoid flagging this. */
1340 const struct shader_info *tes_info = &ish->nir->info;
1341 if (tes_info->system_values_read & (1ull << SYSTEM_VALUE_VERTICES_IN)) {
1342 ice->state.dirty |= IRIS_DIRTY_CONSTANTS_TES;
1343 ice->state.shaders[MESA_SHADER_TESS_EVAL].sysvals_need_upload = true;
1344 }
1345 }
1346
1347 /**
1348 * Compile a geometry shader, and upload the assembly.
1349 */
1350 static struct iris_compiled_shader *
1351 iris_compile_gs(struct iris_context *ice,
1352 struct iris_uncompiled_shader *ish,
1353 const struct brw_gs_prog_key *key)
1354 {
1355 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
1356 const struct brw_compiler *compiler = screen->compiler;
1357 const struct gen_device_info *devinfo = &screen->devinfo;
1358 void *mem_ctx = ralloc_context(NULL);
1359 struct brw_gs_prog_data *gs_prog_data =
1360 rzalloc(mem_ctx, struct brw_gs_prog_data);
1361 struct brw_vue_prog_data *vue_prog_data = &gs_prog_data->base;
1362 struct brw_stage_prog_data *prog_data = &vue_prog_data->base;
1363 enum brw_param_builtin *system_values;
1364 unsigned num_system_values;
1365 unsigned num_cbufs;
1366
1367 nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);
1368
1369 if (key->nr_userclip_plane_consts) {
1370 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
1371 nir_lower_clip_gs(nir, (1 << key->nr_userclip_plane_consts) - 1);
1372 nir_lower_io_to_temporaries(nir, impl, true, false);
1373 nir_lower_global_vars_to_local(nir);
1374 nir_lower_vars_to_ssa(nir);
1375 nir_shader_gather_info(nir, impl);
1376 }
1377
1378 iris_setup_uniforms(compiler, mem_ctx, nir, prog_data, &system_values,
1379 &num_system_values, &num_cbufs);
1380
1381 struct iris_binding_table bt;
1382 iris_setup_binding_table(nir, &bt, /* num_render_targets */ 0,
1383 num_system_values, num_cbufs);
1384
1385 brw_nir_analyze_ubo_ranges(compiler, nir, NULL, prog_data->ubo_ranges);
1386
1387 brw_compute_vue_map(devinfo,
1388 &vue_prog_data->vue_map, nir->info.outputs_written,
1389 nir->info.separate_shader);
1390
1391 char *error_str = NULL;
1392 const unsigned *program =
1393 brw_compile_gs(compiler, &ice->dbg, mem_ctx, key, gs_prog_data, nir,
1394 NULL, -1, &error_str);
1395 if (program == NULL) {
1396 dbg_printf("Failed to compile geometry shader: %s\n", error_str);
1397 ralloc_free(mem_ctx);
1398 return false;
1399 }
1400
1401 if (ish->compiled_once) {
1402 iris_debug_recompile(ice, &nir->info, &key->base);
1403 } else {
1404 ish->compiled_once = true;
1405 }
1406
1407 uint32_t *so_decls =
1408 ice->vtbl.create_so_decl_list(&ish->stream_output,
1409 &vue_prog_data->vue_map);
1410
1411 struct iris_compiled_shader *shader =
1412 iris_upload_shader(ice, IRIS_CACHE_GS, sizeof(*key), key, program,
1413 prog_data, so_decls, system_values, num_system_values,
1414 num_cbufs, &bt);
1415
1416 iris_disk_cache_store(screen->disk_cache, ish, shader, key, sizeof(*key));
1417
1418 ralloc_free(mem_ctx);
1419 return shader;
1420 }
1421
1422 /**
1423 * Update the current geometry shader variant.
1424 *
1425 * Fill out the key, look in the cache, compile and bind if needed.
1426 */
1427 static void
1428 iris_update_compiled_gs(struct iris_context *ice)
1429 {
1430 struct iris_shader_state *shs = &ice->state.shaders[MESA_SHADER_GEOMETRY];
1431 struct iris_uncompiled_shader *ish =
1432 ice->shaders.uncompiled[MESA_SHADER_GEOMETRY];
1433 struct iris_compiled_shader *old = ice->shaders.prog[IRIS_CACHE_GS];
1434 struct iris_compiled_shader *shader = NULL;
1435
1436 if (ish) {
1437 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
1438 const struct gen_device_info *devinfo = &screen->devinfo;
1439 struct brw_gs_prog_key key = { KEY_INIT(devinfo->gen) };
1440 ice->vtbl.populate_gs_key(ice, &ish->nir->info, last_vue_stage(ice), &key);
1441
1442 shader =
1443 iris_find_cached_shader(ice, IRIS_CACHE_GS, sizeof(key), &key);
1444
1445 if (!shader)
1446 shader = iris_disk_cache_retrieve(ice, ish, &key, sizeof(key));
1447
1448 if (!shader)
1449 shader = iris_compile_gs(ice, ish, &key);
1450 }
1451
1452 if (old != shader) {
1453 ice->shaders.prog[IRIS_CACHE_GS] = shader;
1454 ice->state.dirty |= IRIS_DIRTY_GS |
1455 IRIS_DIRTY_BINDINGS_GS |
1456 IRIS_DIRTY_CONSTANTS_GS;
1457 shs->sysvals_need_upload = true;
1458 }
1459 }
1460
1461 /**
1462 * Compile a fragment (pixel) shader, and upload the assembly.
1463 */
1464 static struct iris_compiled_shader *
1465 iris_compile_fs(struct iris_context *ice,
1466 struct iris_uncompiled_shader *ish,
1467 const struct brw_wm_prog_key *key,
1468 struct brw_vue_map *vue_map)
1469 {
1470 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
1471 const struct brw_compiler *compiler = screen->compiler;
1472 void *mem_ctx = ralloc_context(NULL);
1473 struct brw_wm_prog_data *fs_prog_data =
1474 rzalloc(mem_ctx, struct brw_wm_prog_data);
1475 struct brw_stage_prog_data *prog_data = &fs_prog_data->base;
1476 enum brw_param_builtin *system_values;
1477 unsigned num_system_values;
1478 unsigned num_cbufs;
1479
1480 nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);
1481
1482 prog_data->use_alt_mode = ish->use_alt_mode;
1483
1484 iris_setup_uniforms(compiler, mem_ctx, nir, prog_data, &system_values,
1485 &num_system_values, &num_cbufs);
1486
1487 struct iris_binding_table bt;
1488 iris_setup_binding_table(nir, &bt, MAX2(key->nr_color_regions, 1),
1489 num_system_values, num_cbufs);
1490
1491 brw_nir_analyze_ubo_ranges(compiler, nir, NULL, prog_data->ubo_ranges);
1492
1493 char *error_str = NULL;
1494 const unsigned *program =
1495 brw_compile_fs(compiler, &ice->dbg, mem_ctx, key, fs_prog_data,
1496 nir, NULL, -1, -1, -1, true, false, vue_map, &error_str);
1497 if (program == NULL) {
1498 dbg_printf("Failed to compile fragment shader: %s\n", error_str);
1499 ralloc_free(mem_ctx);
1500 return false;
1501 }
1502
1503 if (ish->compiled_once) {
1504 iris_debug_recompile(ice, &nir->info, &key->base);
1505 } else {
1506 ish->compiled_once = true;
1507 }
1508
1509 struct iris_compiled_shader *shader =
1510 iris_upload_shader(ice, IRIS_CACHE_FS, sizeof(*key), key, program,
1511 prog_data, NULL, system_values, num_system_values,
1512 num_cbufs, &bt);
1513
1514 iris_disk_cache_store(screen->disk_cache, ish, shader, key, sizeof(*key));
1515
1516 ralloc_free(mem_ctx);
1517 return shader;
1518 }
1519
1520 /**
1521 * Update the current fragment shader variant.
1522 *
1523 * Fill out the key, look in the cache, compile and bind if needed.
1524 */
1525 static void
1526 iris_update_compiled_fs(struct iris_context *ice)
1527 {
1528 struct iris_shader_state *shs = &ice->state.shaders[MESA_SHADER_FRAGMENT];
1529 struct iris_uncompiled_shader *ish =
1530 ice->shaders.uncompiled[MESA_SHADER_FRAGMENT];
1531 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
1532 const struct gen_device_info *devinfo = &screen->devinfo;
1533 struct brw_wm_prog_key key = { KEY_INIT(devinfo->gen) };
1534 ice->vtbl.populate_fs_key(ice, &ish->nir->info, &key);
1535
1536 if (ish->nos & (1ull << IRIS_NOS_LAST_VUE_MAP))
1537 key.input_slots_valid = ice->shaders.last_vue_map->slots_valid;
1538
1539 struct iris_compiled_shader *old = ice->shaders.prog[IRIS_CACHE_FS];
1540 struct iris_compiled_shader *shader =
1541 iris_find_cached_shader(ice, IRIS_CACHE_FS, sizeof(key), &key);
1542
1543 if (!shader)
1544 shader = iris_disk_cache_retrieve(ice, ish, &key, sizeof(key));
1545
1546 if (!shader)
1547 shader = iris_compile_fs(ice, ish, &key, ice->shaders.last_vue_map);
1548
1549 if (old != shader) {
1550 // XXX: only need to flag CLIP if barycentric has NONPERSPECTIVE
1551 // toggles. might be able to avoid flagging SBE too.
1552 ice->shaders.prog[IRIS_CACHE_FS] = shader;
1553 ice->state.dirty |= IRIS_DIRTY_FS |
1554 IRIS_DIRTY_BINDINGS_FS |
1555 IRIS_DIRTY_CONSTANTS_FS |
1556 IRIS_DIRTY_WM |
1557 IRIS_DIRTY_CLIP |
1558 IRIS_DIRTY_SBE;
1559 shs->sysvals_need_upload = true;
1560 }
1561 }
1562
1563 /**
1564 * Update the last enabled stage's VUE map.
1565 *
1566 * When the shader feeding the rasterizer's output interface changes, we
1567 * need to re-emit various packets.
1568 */
1569 static void
1570 update_last_vue_map(struct iris_context *ice,
1571 struct brw_stage_prog_data *prog_data)
1572 {
1573 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
1574 struct brw_vue_map *vue_map = &vue_prog_data->vue_map;
1575 struct brw_vue_map *old_map = ice->shaders.last_vue_map;
1576 const uint64_t changed_slots =
1577 (old_map ? old_map->slots_valid : 0ull) ^ vue_map->slots_valid;
1578
1579 if (changed_slots & VARYING_BIT_VIEWPORT) {
1580 ice->state.num_viewports =
1581 (vue_map->slots_valid & VARYING_BIT_VIEWPORT) ? IRIS_MAX_VIEWPORTS : 1;
1582 ice->state.dirty |= IRIS_DIRTY_CLIP |
1583 IRIS_DIRTY_SF_CL_VIEWPORT |
1584 IRIS_DIRTY_CC_VIEWPORT |
1585 IRIS_DIRTY_SCISSOR_RECT |
1586 IRIS_DIRTY_UNCOMPILED_FS |
1587 ice->state.dirty_for_nos[IRIS_NOS_LAST_VUE_MAP];
1588 }
1589
1590 if (changed_slots || (old_map && old_map->separate != vue_map->separate)) {
1591 ice->state.dirty |= IRIS_DIRTY_SBE;
1592 }
1593
1594 ice->shaders.last_vue_map = &vue_prog_data->vue_map;
1595 }
1596
1597 /**
1598 * Get the prog_data for a given stage, or NULL if the stage is disabled.
1599 */
1600 static struct brw_vue_prog_data *
1601 get_vue_prog_data(struct iris_context *ice, gl_shader_stage stage)
1602 {
1603 if (!ice->shaders.prog[stage])
1604 return NULL;
1605
1606 return (void *) ice->shaders.prog[stage]->prog_data;
1607 }
1608
1609 // XXX: iris_compiled_shaders are space-leaking :(
1610 // XXX: do remember to unbind them if deleting them.
1611
1612 /**
1613 * Update the current shader variants for the given state.
1614 *
1615 * This should be called on every draw call to ensure that the correct
1616 * shaders are bound. It will also flag any dirty state triggered by
1617 * swapping out those shaders.
1618 */
1619 void
1620 iris_update_compiled_shaders(struct iris_context *ice)
1621 {
1622 const uint64_t dirty = ice->state.dirty;
1623
1624 struct brw_vue_prog_data *old_prog_datas[4];
1625 if (!(dirty & IRIS_DIRTY_URB)) {
1626 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++)
1627 old_prog_datas[i] = get_vue_prog_data(ice, i);
1628 }
1629
1630 if (dirty & (IRIS_DIRTY_UNCOMPILED_TCS | IRIS_DIRTY_UNCOMPILED_TES)) {
1631 struct iris_uncompiled_shader *tes =
1632 ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL];
1633 if (tes) {
1634 iris_update_compiled_tcs(ice);
1635 iris_update_compiled_tes(ice);
1636 } else {
1637 ice->shaders.prog[IRIS_CACHE_TCS] = NULL;
1638 ice->shaders.prog[IRIS_CACHE_TES] = NULL;
1639 ice->state.dirty |=
1640 IRIS_DIRTY_TCS | IRIS_DIRTY_TES |
1641 IRIS_DIRTY_BINDINGS_TCS | IRIS_DIRTY_BINDINGS_TES |
1642 IRIS_DIRTY_CONSTANTS_TCS | IRIS_DIRTY_CONSTANTS_TES;
1643 }
1644 }
1645
1646 if (dirty & IRIS_DIRTY_UNCOMPILED_VS)
1647 iris_update_compiled_vs(ice);
1648 if (dirty & IRIS_DIRTY_UNCOMPILED_GS)
1649 iris_update_compiled_gs(ice);
1650
1651 if (dirty & (IRIS_DIRTY_UNCOMPILED_GS | IRIS_DIRTY_UNCOMPILED_TES)) {
1652 const struct iris_compiled_shader *gs =
1653 ice->shaders.prog[MESA_SHADER_GEOMETRY];
1654 const struct iris_compiled_shader *tes =
1655 ice->shaders.prog[MESA_SHADER_TESS_EVAL];
1656
1657 bool points_or_lines = false;
1658
1659 if (gs) {
1660 const struct brw_gs_prog_data *gs_prog_data = (void *) gs->prog_data;
1661 points_or_lines =
1662 gs_prog_data->output_topology == _3DPRIM_POINTLIST ||
1663 gs_prog_data->output_topology == _3DPRIM_LINESTRIP;
1664 } else if (tes) {
1665 const struct brw_tes_prog_data *tes_data = (void *) tes->prog_data;
1666 points_or_lines =
1667 tes_data->output_topology == BRW_TESS_OUTPUT_TOPOLOGY_LINE ||
1668 tes_data->output_topology == BRW_TESS_OUTPUT_TOPOLOGY_POINT;
1669 }
1670
1671 if (ice->shaders.output_topology_is_points_or_lines != points_or_lines) {
1672 /* Outbound to XY Clip enables */
1673 ice->shaders.output_topology_is_points_or_lines = points_or_lines;
1674 ice->state.dirty |= IRIS_DIRTY_CLIP;
1675 }
1676 }
1677
1678 gl_shader_stage last_stage = last_vue_stage(ice);
1679 struct iris_compiled_shader *shader = ice->shaders.prog[last_stage];
1680 struct iris_uncompiled_shader *ish = ice->shaders.uncompiled[last_stage];
1681 update_last_vue_map(ice, shader->prog_data);
1682 if (ice->state.streamout != shader->streamout) {
1683 ice->state.streamout = shader->streamout;
1684 ice->state.dirty |= IRIS_DIRTY_SO_DECL_LIST | IRIS_DIRTY_STREAMOUT;
1685 }
1686
1687 if (ice->state.streamout_active) {
1688 for (int i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
1689 struct iris_stream_output_target *so =
1690 (void *) ice->state.so_target[i];
1691 if (so)
1692 so->stride = ish->stream_output.stride[i] * sizeof(uint32_t);
1693 }
1694 }
1695
1696 if (dirty & IRIS_DIRTY_UNCOMPILED_FS)
1697 iris_update_compiled_fs(ice);
1698
1699 /* Changing shader interfaces may require a URB configuration. */
1700 if (!(dirty & IRIS_DIRTY_URB)) {
1701 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
1702 struct brw_vue_prog_data *old = old_prog_datas[i];
1703 struct brw_vue_prog_data *new = get_vue_prog_data(ice, i);
1704 if (!!old != !!new ||
1705 (new && new->urb_entry_size != old->urb_entry_size)) {
1706 ice->state.dirty |= IRIS_DIRTY_URB;
1707 break;
1708 }
1709 }
1710 }
1711 }
1712
1713 static struct iris_compiled_shader *
1714 iris_compile_cs(struct iris_context *ice,
1715 struct iris_uncompiled_shader *ish,
1716 const struct brw_cs_prog_key *key)
1717 {
1718 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
1719 const struct brw_compiler *compiler = screen->compiler;
1720 void *mem_ctx = ralloc_context(NULL);
1721 struct brw_cs_prog_data *cs_prog_data =
1722 rzalloc(mem_ctx, struct brw_cs_prog_data);
1723 struct brw_stage_prog_data *prog_data = &cs_prog_data->base;
1724 enum brw_param_builtin *system_values;
1725 unsigned num_system_values;
1726 unsigned num_cbufs;
1727
1728 nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);
1729
1730 iris_setup_uniforms(compiler, mem_ctx, nir, prog_data, &system_values,
1731 &num_system_values, &num_cbufs);
1732
1733 struct iris_binding_table bt;
1734 iris_setup_binding_table(nir, &bt, /* num_render_targets */ 0,
1735 num_system_values, num_cbufs);
1736
1737 char *error_str = NULL;
1738 const unsigned *program =
1739 brw_compile_cs(compiler, &ice->dbg, mem_ctx, key, cs_prog_data,
1740 nir, -1, &error_str);
1741 if (program == NULL) {
1742 dbg_printf("Failed to compile compute shader: %s\n", error_str);
1743 ralloc_free(mem_ctx);
1744 return false;
1745 }
1746
1747 if (ish->compiled_once) {
1748 iris_debug_recompile(ice, &nir->info, &key->base);
1749 } else {
1750 ish->compiled_once = true;
1751 }
1752
1753 struct iris_compiled_shader *shader =
1754 iris_upload_shader(ice, IRIS_CACHE_CS, sizeof(*key), key, program,
1755 prog_data, NULL, system_values, num_system_values,
1756 num_cbufs, &bt);
1757
1758 iris_disk_cache_store(screen->disk_cache, ish, shader, key, sizeof(*key));
1759
1760 ralloc_free(mem_ctx);
1761 return shader;
1762 }
1763
1764 void
1765 iris_update_compiled_compute_shader(struct iris_context *ice)
1766 {
1767 struct iris_shader_state *shs = &ice->state.shaders[MESA_SHADER_COMPUTE];
1768 struct iris_uncompiled_shader *ish =
1769 ice->shaders.uncompiled[MESA_SHADER_COMPUTE];
1770
1771 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
1772 const struct gen_device_info *devinfo = &screen->devinfo;
1773 struct brw_cs_prog_key key = { KEY_INIT(devinfo->gen) };
1774 ice->vtbl.populate_cs_key(ice, &key);
1775
1776 struct iris_compiled_shader *old = ice->shaders.prog[IRIS_CACHE_CS];
1777 struct iris_compiled_shader *shader =
1778 iris_find_cached_shader(ice, IRIS_CACHE_CS, sizeof(key), &key);
1779
1780 if (!shader)
1781 shader = iris_disk_cache_retrieve(ice, ish, &key, sizeof(key));
1782
1783 if (!shader)
1784 shader = iris_compile_cs(ice, ish, &key);
1785
1786 if (old != shader) {
1787 ice->shaders.prog[IRIS_CACHE_CS] = shader;
1788 ice->state.dirty |= IRIS_DIRTY_CS |
1789 IRIS_DIRTY_BINDINGS_CS |
1790 IRIS_DIRTY_CONSTANTS_CS;
1791 shs->sysvals_need_upload = true;
1792 }
1793 }
1794
1795 void
1796 iris_fill_cs_push_const_buffer(struct brw_cs_prog_data *cs_prog_data,
1797 uint32_t *dst)
1798 {
1799 assert(cs_prog_data->push.total.size > 0);
1800 assert(cs_prog_data->push.cross_thread.size == 0);
1801 assert(cs_prog_data->push.per_thread.dwords == 1);
1802 assert(cs_prog_data->base.param[0] == BRW_PARAM_BUILTIN_SUBGROUP_ID);
1803 for (unsigned t = 0; t < cs_prog_data->threads; t++)
1804 dst[8 * t] = t;
1805 }
1806
1807 /**
1808 * Allocate scratch BOs as needed for the given per-thread size and stage.
1809 */
1810 struct iris_bo *
1811 iris_get_scratch_space(struct iris_context *ice,
1812 unsigned per_thread_scratch,
1813 gl_shader_stage stage)
1814 {
1815 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
1816 struct iris_bufmgr *bufmgr = screen->bufmgr;
1817 const struct gen_device_info *devinfo = &screen->devinfo;
1818
1819 unsigned encoded_size = ffs(per_thread_scratch) - 11;
1820 assert(encoded_size < (1 << 16));
1821
1822 struct iris_bo **bop = &ice->shaders.scratch_bos[encoded_size][stage];
1823
1824 /* The documentation for 3DSTATE_PS "Scratch Space Base Pointer" says:
1825 *
1826 * "Scratch Space per slice is computed based on 4 sub-slices. SW
1827 * must allocate scratch space enough so that each slice has 4
1828 * slices allowed."
1829 *
1830 * According to the other driver team, this applies to compute shaders
1831 * as well. This is not currently documented at all.
1832 *
1833 * This hack is no longer necessary on Gen11+.
1834 */
1835 unsigned subslice_total = screen->subslice_total;
1836 if (devinfo->gen < 11)
1837 subslice_total = 4 * devinfo->num_slices;
1838 assert(subslice_total >= screen->subslice_total);
1839
1840 if (!*bop) {
1841 unsigned scratch_ids_per_subslice = devinfo->max_cs_threads;
1842 uint32_t max_threads[] = {
1843 [MESA_SHADER_VERTEX] = devinfo->max_vs_threads,
1844 [MESA_SHADER_TESS_CTRL] = devinfo->max_tcs_threads,
1845 [MESA_SHADER_TESS_EVAL] = devinfo->max_tes_threads,
1846 [MESA_SHADER_GEOMETRY] = devinfo->max_gs_threads,
1847 [MESA_SHADER_FRAGMENT] = devinfo->max_wm_threads,
1848 [MESA_SHADER_COMPUTE] = scratch_ids_per_subslice * subslice_total,
1849 };
1850
1851 uint32_t size = per_thread_scratch * max_threads[stage];
1852
1853 *bop = iris_bo_alloc(bufmgr, "scratch", size, IRIS_MEMZONE_SHADER);
1854 }
1855
1856 return *bop;
1857 }
1858
1859 /* ------------------------------------------------------------------- */
1860
1861 /**
1862 * The pipe->create_[stage]_state() driver hooks.
1863 *
1864 * Performs basic NIR preprocessing, records any state dependencies, and
1865 * returns an iris_uncompiled_shader as the Gallium CSO.
1866 *
1867 * Actual shader compilation to assembly happens later, at first use.
1868 */
1869 static void *
1870 iris_create_uncompiled_shader(struct pipe_context *ctx,
1871 nir_shader *nir,
1872 const struct pipe_stream_output_info *so_info)
1873 {
1874 struct iris_context *ice = (void *)ctx;
1875 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1876 const struct gen_device_info *devinfo = &screen->devinfo;
1877
1878 struct iris_uncompiled_shader *ish =
1879 calloc(1, sizeof(struct iris_uncompiled_shader));
1880 if (!ish)
1881 return NULL;
1882
1883 brw_preprocess_nir(screen->compiler, nir, NULL);
1884
1885 NIR_PASS_V(nir, brw_nir_lower_image_load_store, devinfo);
1886 NIR_PASS_V(nir, iris_lower_storage_image_derefs);
1887
1888 nir_sweep(nir);
1889
1890 if (nir->constant_data_size > 0) {
1891 unsigned data_offset;
1892 u_upload_data(ice->shaders.uploader, 0, nir->constant_data_size,
1893 32, nir->constant_data, &data_offset, &ish->const_data);
1894
1895 struct pipe_shader_buffer psb = {
1896 .buffer = ish->const_data,
1897 .buffer_offset = data_offset,
1898 .buffer_size = nir->constant_data_size,
1899 };
1900 iris_upload_ubo_ssbo_surf_state(ice, &psb, &ish->const_data_state, false);
1901 }
1902
1903 ish->program_id = get_new_program_id(screen);
1904 ish->nir = nir;
1905 if (so_info) {
1906 memcpy(&ish->stream_output, so_info, sizeof(*so_info));
1907 update_so_info(&ish->stream_output, nir->info.outputs_written);
1908 }
1909
1910 /* Save this now before potentially dropping nir->info.name */
1911 if (nir->info.name && strncmp(nir->info.name, "ARB", 3) == 0)
1912 ish->use_alt_mode = true;
1913
1914 if (screen->disk_cache) {
1915 /* Serialize the NIR to a binary blob that we can hash for the disk
1916 * cache. First, drop unnecessary information (like variable names)
1917 * so the serialized NIR is smaller, and also to let us detect more
1918 * isomorphic shaders when hashing, increasing cache hits. We clone
1919 * the NIR before stripping away this info because it can be useful
1920 * when inspecting and debugging shaders.
1921 */
1922 nir_shader *clone = nir_shader_clone(NULL, nir);
1923 nir_strip(clone);
1924
1925 struct blob blob;
1926 blob_init(&blob);
1927 nir_serialize(&blob, clone);
1928 _mesa_sha1_compute(blob.data, blob.size, ish->nir_sha1);
1929 blob_finish(&blob);
1930
1931 ralloc_free(clone);
1932 }
1933
1934 return ish;
1935 }
1936
1937 static struct iris_uncompiled_shader *
1938 iris_create_shader_state(struct pipe_context *ctx,
1939 const struct pipe_shader_state *state)
1940 {
1941 struct nir_shader *nir;
1942
1943 if (state->type == PIPE_SHADER_IR_TGSI)
1944 nir = tgsi_to_nir(state->tokens, ctx->screen);
1945 else
1946 nir = state->ir.nir;
1947
1948 return iris_create_uncompiled_shader(ctx, nir, &state->stream_output);
1949 }
1950
1951 static void *
1952 iris_create_vs_state(struct pipe_context *ctx,
1953 const struct pipe_shader_state *state)
1954 {
1955 struct iris_context *ice = (void *) ctx;
1956 struct iris_screen *screen = (void *) ctx->screen;
1957 struct iris_uncompiled_shader *ish = iris_create_shader_state(ctx, state);
1958
1959 /* User clip planes */
1960 if (ish->nir->info.clip_distance_array_size == 0)
1961 ish->nos |= (1ull << IRIS_NOS_RASTERIZER);
1962
1963 if (screen->precompile) {
1964 const struct gen_device_info *devinfo = &screen->devinfo;
1965 struct brw_vs_prog_key key = { KEY_INIT(devinfo->gen) };
1966
1967 if (!iris_disk_cache_retrieve(ice, ish, &key, sizeof(key)))
1968 iris_compile_vs(ice, ish, &key);
1969 }
1970
1971 return ish;
1972 }
1973
1974 static void *
1975 iris_create_tcs_state(struct pipe_context *ctx,
1976 const struct pipe_shader_state *state)
1977 {
1978 struct iris_context *ice = (void *) ctx;
1979 struct iris_screen *screen = (void *) ctx->screen;
1980 const struct brw_compiler *compiler = screen->compiler;
1981 struct iris_uncompiled_shader *ish = iris_create_shader_state(ctx, state);
1982 struct shader_info *info = &ish->nir->info;
1983
1984 if (screen->precompile) {
1985 const unsigned _GL_TRIANGLES = 0x0004;
1986 const struct gen_device_info *devinfo = &screen->devinfo;
1987 struct brw_tcs_prog_key key = {
1988 KEY_INIT(devinfo->gen),
1989 // XXX: make sure the linker fills this out from the TES...
1990 .tes_primitive_mode =
1991 info->tess.primitive_mode ? info->tess.primitive_mode
1992 : _GL_TRIANGLES,
1993 .outputs_written = info->outputs_written,
1994 .patch_outputs_written = info->patch_outputs_written,
1995 };
1996
1997 /* 8_PATCH mode needs the key to contain the input patch dimensionality.
1998 * We don't have that information, so we randomly guess that the input
1999 * and output patches are the same size. This is a bad guess, but we
2000 * can't do much better.
2001 */
2002 if (compiler->use_tcs_8_patch)
2003 key.input_vertices = info->tess.tcs_vertices_out;
2004
2005 if (!iris_disk_cache_retrieve(ice, ish, &key, sizeof(key)))
2006 iris_compile_tcs(ice, ish, &key);
2007 }
2008
2009 return ish;
2010 }
2011
2012 static void *
2013 iris_create_tes_state(struct pipe_context *ctx,
2014 const struct pipe_shader_state *state)
2015 {
2016 struct iris_context *ice = (void *) ctx;
2017 struct iris_screen *screen = (void *) ctx->screen;
2018 struct iris_uncompiled_shader *ish = iris_create_shader_state(ctx, state);
2019 struct shader_info *info = &ish->nir->info;
2020
2021 /* User clip planes */
2022 if (ish->nir->info.clip_distance_array_size == 0)
2023 ish->nos |= (1ull << IRIS_NOS_RASTERIZER);
2024
2025 if (screen->precompile) {
2026 const struct gen_device_info *devinfo = &screen->devinfo;
2027 struct brw_tes_prog_key key = {
2028 KEY_INIT(devinfo->gen),
2029 // XXX: not ideal, need TCS output/TES input unification
2030 .inputs_read = info->inputs_read,
2031 .patch_inputs_read = info->patch_inputs_read,
2032 };
2033
2034 if (!iris_disk_cache_retrieve(ice, ish, &key, sizeof(key)))
2035 iris_compile_tes(ice, ish, &key);
2036 }
2037
2038 return ish;
2039 }
2040
2041 static void *
2042 iris_create_gs_state(struct pipe_context *ctx,
2043 const struct pipe_shader_state *state)
2044 {
2045 struct iris_context *ice = (void *) ctx;
2046 struct iris_screen *screen = (void *) ctx->screen;
2047 struct iris_uncompiled_shader *ish = iris_create_shader_state(ctx, state);
2048
2049 /* User clip planes */
2050 if (ish->nir->info.clip_distance_array_size == 0)
2051 ish->nos |= (1ull << IRIS_NOS_RASTERIZER);
2052
2053 if (screen->precompile) {
2054 const struct gen_device_info *devinfo = &screen->devinfo;
2055 struct brw_gs_prog_key key = { KEY_INIT(devinfo->gen) };
2056
2057 if (!iris_disk_cache_retrieve(ice, ish, &key, sizeof(key)))
2058 iris_compile_gs(ice, ish, &key);
2059 }
2060
2061 return ish;
2062 }
2063
2064 static void *
2065 iris_create_fs_state(struct pipe_context *ctx,
2066 const struct pipe_shader_state *state)
2067 {
2068 struct iris_context *ice = (void *) ctx;
2069 struct iris_screen *screen = (void *) ctx->screen;
2070 struct iris_uncompiled_shader *ish = iris_create_shader_state(ctx, state);
2071 struct shader_info *info = &ish->nir->info;
2072
2073 ish->nos |= (1ull << IRIS_NOS_FRAMEBUFFER) |
2074 (1ull << IRIS_NOS_DEPTH_STENCIL_ALPHA) |
2075 (1ull << IRIS_NOS_RASTERIZER) |
2076 (1ull << IRIS_NOS_BLEND);
2077
2078 /* The program key needs the VUE map if there are > 16 inputs */
2079 if (util_bitcount64(ish->nir->info.inputs_read &
2080 BRW_FS_VARYING_INPUT_MASK) > 16) {
2081 ish->nos |= (1ull << IRIS_NOS_LAST_VUE_MAP);
2082 }
2083
2084 if (screen->precompile) {
2085 const uint64_t color_outputs = info->outputs_written &
2086 ~(BITFIELD64_BIT(FRAG_RESULT_DEPTH) |
2087 BITFIELD64_BIT(FRAG_RESULT_STENCIL) |
2088 BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK));
2089
2090 bool can_rearrange_varyings =
2091 util_bitcount64(info->inputs_read & BRW_FS_VARYING_INPUT_MASK) <= 16;
2092
2093 const struct gen_device_info *devinfo = &screen->devinfo;
2094 struct brw_wm_prog_key key = {
2095 KEY_INIT(devinfo->gen),
2096 .nr_color_regions = util_bitcount(color_outputs),
2097 .coherent_fb_fetch = true,
2098 .input_slots_valid =
2099 can_rearrange_varyings ? 0 : info->inputs_read | VARYING_BIT_POS,
2100 };
2101
2102 if (!iris_disk_cache_retrieve(ice, ish, &key, sizeof(key)))
2103 iris_compile_fs(ice, ish, &key, NULL);
2104 }
2105
2106 return ish;
2107 }
2108
2109 static void *
2110 iris_create_compute_state(struct pipe_context *ctx,
2111 const struct pipe_compute_state *state)
2112 {
2113 assert(state->ir_type == PIPE_SHADER_IR_NIR);
2114
2115 struct iris_context *ice = (void *) ctx;
2116 struct iris_screen *screen = (void *) ctx->screen;
2117 struct iris_uncompiled_shader *ish =
2118 iris_create_uncompiled_shader(ctx, (void *) state->prog, NULL);
2119
2120 // XXX: disallow more than 64KB of shared variables
2121
2122 if (screen->precompile) {
2123 const struct gen_device_info *devinfo = &screen->devinfo;
2124 struct brw_cs_prog_key key = { KEY_INIT(devinfo->gen) };
2125
2126 if (!iris_disk_cache_retrieve(ice, ish, &key, sizeof(key)))
2127 iris_compile_cs(ice, ish, &key);
2128 }
2129
2130 return ish;
2131 }
2132
2133 /**
2134 * The pipe->delete_[stage]_state() driver hooks.
2135 *
2136 * Frees the iris_uncompiled_shader.
2137 */
2138 static void
2139 iris_delete_shader_state(struct pipe_context *ctx, void *state, gl_shader_stage stage)
2140 {
2141 struct iris_uncompiled_shader *ish = state;
2142 struct iris_context *ice = (void *) ctx;
2143
2144 if (ice->shaders.uncompiled[stage] == ish) {
2145 ice->shaders.uncompiled[stage] = NULL;
2146 ice->state.dirty |= IRIS_DIRTY_UNCOMPILED_VS << stage;
2147 }
2148
2149 if (ish->const_data) {
2150 pipe_resource_reference(&ish->const_data, NULL);
2151 pipe_resource_reference(&ish->const_data_state.res, NULL);
2152 }
2153
2154 ralloc_free(ish->nir);
2155 free(ish);
2156 }
2157
2158 static void
2159 iris_delete_vs_state(struct pipe_context *ctx, void *state)
2160 {
2161 iris_delete_shader_state(ctx, state, MESA_SHADER_VERTEX);
2162 }
2163
2164 static void
2165 iris_delete_tcs_state(struct pipe_context *ctx, void *state)
2166 {
2167 iris_delete_shader_state(ctx, state, MESA_SHADER_TESS_CTRL);
2168 }
2169
2170 static void
2171 iris_delete_tes_state(struct pipe_context *ctx, void *state)
2172 {
2173 iris_delete_shader_state(ctx, state, MESA_SHADER_TESS_EVAL);
2174 }
2175
2176 static void
2177 iris_delete_gs_state(struct pipe_context *ctx, void *state)
2178 {
2179 iris_delete_shader_state(ctx, state, MESA_SHADER_GEOMETRY);
2180 }
2181
2182 static void
2183 iris_delete_fs_state(struct pipe_context *ctx, void *state)
2184 {
2185 iris_delete_shader_state(ctx, state, MESA_SHADER_FRAGMENT);
2186 }
2187
2188 static void
2189 iris_delete_cs_state(struct pipe_context *ctx, void *state)
2190 {
2191 iris_delete_shader_state(ctx, state, MESA_SHADER_COMPUTE);
2192 }
2193
2194 /**
2195 * The pipe->bind_[stage]_state() driver hook.
2196 *
2197 * Binds an uncompiled shader as the current one for a particular stage.
2198 * Updates dirty tracking to account for the shader's NOS.
2199 */
2200 static void
2201 bind_shader_state(struct iris_context *ice,
2202 struct iris_uncompiled_shader *ish,
2203 gl_shader_stage stage)
2204 {
2205 uint64_t dirty_bit = IRIS_DIRTY_UNCOMPILED_VS << stage;
2206 const uint64_t nos = ish ? ish->nos : 0;
2207
2208 const struct shader_info *old_info = iris_get_shader_info(ice, stage);
2209 const struct shader_info *new_info = ish ? &ish->nir->info : NULL;
2210
2211 if ((old_info ? util_last_bit(old_info->textures_used) : 0) !=
2212 (new_info ? util_last_bit(new_info->textures_used) : 0)) {
2213 ice->state.dirty |= IRIS_DIRTY_SAMPLER_STATES_VS << stage;
2214 }
2215
2216 ice->shaders.uncompiled[stage] = ish;
2217 ice->state.dirty |= dirty_bit;
2218
2219 /* Record that CSOs need to mark IRIS_DIRTY_UNCOMPILED_XS when they change
2220 * (or that they no longer need to do so).
2221 */
2222 for (int i = 0; i < IRIS_NOS_COUNT; i++) {
2223 if (nos & (1 << i))
2224 ice->state.dirty_for_nos[i] |= dirty_bit;
2225 else
2226 ice->state.dirty_for_nos[i] &= ~dirty_bit;
2227 }
2228 }
2229
2230 static void
2231 iris_bind_vs_state(struct pipe_context *ctx, void *state)
2232 {
2233 struct iris_context *ice = (struct iris_context *)ctx;
2234 struct iris_uncompiled_shader *new_ish = state;
2235
2236 if (new_ish &&
2237 ice->state.window_space_position !=
2238 new_ish->nir->info.vs.window_space_position) {
2239 ice->state.window_space_position =
2240 new_ish->nir->info.vs.window_space_position;
2241
2242 ice->state.dirty |= IRIS_DIRTY_CLIP |
2243 IRIS_DIRTY_RASTER |
2244 IRIS_DIRTY_CC_VIEWPORT;
2245 }
2246
2247 bind_shader_state((void *) ctx, state, MESA_SHADER_VERTEX);
2248 }
2249
2250 static void
2251 iris_bind_tcs_state(struct pipe_context *ctx, void *state)
2252 {
2253 bind_shader_state((void *) ctx, state, MESA_SHADER_TESS_CTRL);
2254 }
2255
2256 static void
2257 iris_bind_tes_state(struct pipe_context *ctx, void *state)
2258 {
2259 struct iris_context *ice = (struct iris_context *)ctx;
2260
2261 /* Enabling/disabling optional stages requires a URB reconfiguration. */
2262 if (!!state != !!ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL])
2263 ice->state.dirty |= IRIS_DIRTY_URB;
2264
2265 bind_shader_state((void *) ctx, state, MESA_SHADER_TESS_EVAL);
2266 }
2267
2268 static void
2269 iris_bind_gs_state(struct pipe_context *ctx, void *state)
2270 {
2271 struct iris_context *ice = (struct iris_context *)ctx;
2272
2273 /* Enabling/disabling optional stages requires a URB reconfiguration. */
2274 if (!!state != !!ice->shaders.uncompiled[MESA_SHADER_GEOMETRY])
2275 ice->state.dirty |= IRIS_DIRTY_URB;
2276
2277 bind_shader_state((void *) ctx, state, MESA_SHADER_GEOMETRY);
2278 }
2279
2280 static void
2281 iris_bind_fs_state(struct pipe_context *ctx, void *state)
2282 {
2283 struct iris_context *ice = (struct iris_context *) ctx;
2284 struct iris_uncompiled_shader *old_ish =
2285 ice->shaders.uncompiled[MESA_SHADER_FRAGMENT];
2286 struct iris_uncompiled_shader *new_ish = state;
2287
2288 const unsigned color_bits =
2289 BITFIELD64_BIT(FRAG_RESULT_COLOR) |
2290 BITFIELD64_RANGE(FRAG_RESULT_DATA0, BRW_MAX_DRAW_BUFFERS);
2291
2292 /* Fragment shader outputs influence HasWriteableRT */
2293 if (!old_ish || !new_ish ||
2294 (old_ish->nir->info.outputs_written & color_bits) !=
2295 (new_ish->nir->info.outputs_written & color_bits))
2296 ice->state.dirty |= IRIS_DIRTY_PS_BLEND;
2297
2298 bind_shader_state((void *) ctx, state, MESA_SHADER_FRAGMENT);
2299 }
2300
2301 static void
2302 iris_bind_cs_state(struct pipe_context *ctx, void *state)
2303 {
2304 bind_shader_state((void *) ctx, state, MESA_SHADER_COMPUTE);
2305 }
2306
2307 void
2308 iris_init_program_functions(struct pipe_context *ctx)
2309 {
2310 ctx->create_vs_state = iris_create_vs_state;
2311 ctx->create_tcs_state = iris_create_tcs_state;
2312 ctx->create_tes_state = iris_create_tes_state;
2313 ctx->create_gs_state = iris_create_gs_state;
2314 ctx->create_fs_state = iris_create_fs_state;
2315 ctx->create_compute_state = iris_create_compute_state;
2316
2317 ctx->delete_vs_state = iris_delete_vs_state;
2318 ctx->delete_tcs_state = iris_delete_tcs_state;
2319 ctx->delete_tes_state = iris_delete_tes_state;
2320 ctx->delete_gs_state = iris_delete_gs_state;
2321 ctx->delete_fs_state = iris_delete_fs_state;
2322 ctx->delete_compute_state = iris_delete_cs_state;
2323
2324 ctx->bind_vs_state = iris_bind_vs_state;
2325 ctx->bind_tcs_state = iris_bind_tcs_state;
2326 ctx->bind_tes_state = iris_bind_tes_state;
2327 ctx->bind_gs_state = iris_bind_gs_state;
2328 ctx->bind_fs_state = iris_bind_fs_state;
2329 ctx->bind_compute_state = iris_bind_cs_state;
2330 }