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