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