iris: fill out params array with built-ins, like clip planes
[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 "compiler/nir/nir.h"
40 #include "compiler/nir/nir_builder.h"
41 #include "intel/compiler/brw_compiler.h"
42 #include "intel/compiler/brw_nir.h"
43 #include "iris_context.h"
44
45 static unsigned
46 get_new_program_id(struct iris_screen *screen)
47 {
48 return p_atomic_inc_return(&screen->program_id);
49 }
50
51 /**
52 * An uncompiled, API-facing shader. This is the Gallium CSO for shaders.
53 * It primarily contains the NIR for the shader.
54 *
55 * Each API-facing shader can be compiled into multiple shader variants,
56 * based on non-orthogonal state dependencies, recorded in the shader key.
57 *
58 * See iris_compiled_shader, which represents a compiled shader variant.
59 */
60 struct iris_uncompiled_shader {
61 nir_shader *nir;
62
63 struct pipe_stream_output_info stream_output;
64
65 unsigned program_id;
66
67 /** Bitfield of (1 << IRIS_NOS_*) flags. */
68 unsigned nos;
69 };
70
71 static nir_ssa_def *
72 get_aoa_deref_offset(nir_builder *b,
73 nir_deref_instr *deref,
74 unsigned elem_size)
75 {
76 unsigned array_size = elem_size;
77 nir_ssa_def *offset = nir_imm_int(b, 0);
78
79 while (deref->deref_type != nir_deref_type_var) {
80 assert(deref->deref_type == nir_deref_type_array);
81
82 /* This level's element size is the previous level's array size */
83 nir_ssa_def *index = nir_ssa_for_src(b, deref->arr.index, 1);
84 assert(deref->arr.index.ssa);
85 offset = nir_iadd(b, offset,
86 nir_imul(b, index, nir_imm_int(b, array_size)));
87
88 deref = nir_deref_instr_parent(deref);
89 assert(glsl_type_is_array(deref->type));
90 array_size *= glsl_get_length(deref->type);
91 }
92
93 /* Accessing an invalid surface index with the dataport can result in a
94 * hang. According to the spec "if the index used to select an individual
95 * element is negative or greater than or equal to the size of the array,
96 * the results of the operation are undefined but may not lead to
97 * termination" -- which is one of the possible outcomes of the hang.
98 * Clamp the index to prevent access outside of the array bounds.
99 */
100 return nir_umin(b, offset, nir_imm_int(b, array_size - elem_size));
101 }
102
103 static void
104 iris_lower_storage_image_derefs(nir_shader *nir)
105 {
106 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
107
108 nir_builder b;
109 nir_builder_init(&b, impl);
110
111 nir_foreach_block(block, impl) {
112 nir_foreach_instr_safe(instr, block) {
113 if (instr->type != nir_instr_type_intrinsic)
114 continue;
115
116 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
117 switch (intrin->intrinsic) {
118 case nir_intrinsic_image_deref_load:
119 case nir_intrinsic_image_deref_store:
120 case nir_intrinsic_image_deref_atomic_add:
121 case nir_intrinsic_image_deref_atomic_min:
122 case nir_intrinsic_image_deref_atomic_max:
123 case nir_intrinsic_image_deref_atomic_and:
124 case nir_intrinsic_image_deref_atomic_or:
125 case nir_intrinsic_image_deref_atomic_xor:
126 case nir_intrinsic_image_deref_atomic_exchange:
127 case nir_intrinsic_image_deref_atomic_comp_swap:
128 case nir_intrinsic_image_deref_size:
129 case nir_intrinsic_image_deref_samples: {
130 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
131 nir_variable *var = nir_deref_instr_get_variable(deref);
132
133 b.cursor = nir_before_instr(&intrin->instr);
134 nir_ssa_def *index =
135 nir_iadd(&b, nir_imm_int(&b, var->data.driver_location),
136 get_aoa_deref_offset(&b, deref, 1));
137 brw_nir_rewrite_image_intrinsic(intrin, index);
138 break;
139 }
140
141 default:
142 break;
143 }
144 }
145 }
146 }
147
148
149
150 // XXX: need unify_interfaces() at link time...
151
152 /**
153 * The pipe->create_[stage]_state() driver hooks.
154 *
155 * Performs basic NIR preprocessing, records any state dependencies, and
156 * returns an iris_uncompiled_shader as the Gallium CSO.
157 *
158 * Actual shader compilation to assembly happens later, at first use.
159 */
160 static void *
161 iris_create_uncompiled_shader(struct pipe_context *ctx,
162 nir_shader *nir,
163 const struct pipe_stream_output_info *so_info)
164 {
165 //struct iris_context *ice = (struct iris_context *)ctx;
166 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
167 const struct gen_device_info *devinfo = &screen->devinfo;
168
169 struct iris_uncompiled_shader *ish =
170 calloc(1, sizeof(struct iris_uncompiled_shader));
171 if (!ish)
172 return NULL;
173
174 nir = brw_preprocess_nir(screen->compiler, nir);
175
176 NIR_PASS_V(nir, brw_nir_lower_image_load_store, devinfo);
177 NIR_PASS_V(nir, iris_lower_storage_image_derefs);
178
179 ish->program_id = get_new_program_id(screen);
180 ish->nir = nir;
181 if (so_info)
182 memcpy(&ish->stream_output, so_info, sizeof(*so_info));
183
184 switch (nir->info.stage) {
185 case MESA_SHADER_VERTEX:
186 // XXX: NOS
187 break;
188 case MESA_SHADER_TESS_CTRL:
189 // XXX: NOS
190 break;
191 case MESA_SHADER_TESS_EVAL:
192 // XXX: NOS
193 break;
194 case MESA_SHADER_GEOMETRY:
195 // XXX: NOS
196 break;
197 case MESA_SHADER_FRAGMENT:
198 ish->nos |= IRIS_NOS_FRAMEBUFFER |
199 IRIS_NOS_DEPTH_STENCIL_ALPHA |
200 IRIS_NOS_RASTERIZER |
201 IRIS_NOS_BLEND;
202
203 /* The program key needs the VUE map if there are > 16 inputs */
204 if (util_bitcount64(ish->nir->info.inputs_read &
205 BRW_FS_VARYING_INPUT_MASK) > 16) {
206 ish->nos |= IRIS_NOS_LAST_VUE_MAP;
207 }
208 break;
209 case MESA_SHADER_COMPUTE:
210 // XXX: NOS
211 break;
212 default:
213 break;
214 }
215
216 // XXX: precompile!
217 // XXX: disallow more than 64KB of shared variables
218
219 return ish;
220 }
221
222 /**
223 * The pipe->delete_[stage]_state() driver hooks.
224 *
225 * Frees the iris_uncompiled_shader.
226 */
227 static void *
228 iris_create_shader_state(struct pipe_context *ctx,
229 const struct pipe_shader_state *state)
230 {
231 assert(state->type == PIPE_SHADER_IR_NIR);
232
233 return iris_create_uncompiled_shader(ctx, state->ir.nir,
234 &state->stream_output);
235 }
236
237 static void *
238 iris_create_compute_state(struct pipe_context *ctx,
239 const struct pipe_compute_state *state)
240 {
241 assert(state->ir_type == PIPE_SHADER_IR_NIR);
242
243 return iris_create_uncompiled_shader(ctx, (void *) state->prog, NULL);
244 }
245
246 static void
247 iris_delete_shader_state(struct pipe_context *ctx, void *state)
248 {
249 struct iris_uncompiled_shader *ish = state;
250
251 ralloc_free(ish->nir);
252 free(ish);
253 }
254
255 /**
256 * The pipe->bind_[stage]_state() driver hook.
257 *
258 * Binds an uncompiled shader as the current one for a particular stage.
259 * Updates dirty tracking to account for the shader's NOS.
260 */
261 static void
262 bind_state(struct iris_context *ice,
263 struct iris_uncompiled_shader *ish,
264 gl_shader_stage stage)
265 {
266 uint64_t dirty_bit = IRIS_DIRTY_UNCOMPILED_VS << stage;
267 const uint64_t nos = ish ? ish->nos : 0;
268
269 ice->shaders.uncompiled[stage] = ish;
270 ice->state.dirty |= dirty_bit;
271
272 /* Record that CSOs need to mark IRIS_DIRTY_UNCOMPILED_XS when they change
273 * (or that they no longer need to do so).
274 */
275 for (int i = 0; i < IRIS_NOS_COUNT; i++) {
276 if (nos & (1 << i))
277 ice->state.dirty_for_nos[i] |= dirty_bit;
278 else
279 ice->state.dirty_for_nos[i] &= ~dirty_bit;
280 }
281 }
282
283 static void
284 iris_bind_vs_state(struct pipe_context *ctx, void *state)
285 {
286 bind_state((void *) ctx, state, MESA_SHADER_VERTEX);
287 }
288
289 static void
290 iris_bind_tcs_state(struct pipe_context *ctx, void *state)
291 {
292 bind_state((void *) ctx, state, MESA_SHADER_TESS_CTRL);
293 }
294
295 static void
296 iris_bind_tes_state(struct pipe_context *ctx, void *state)
297 {
298 struct iris_context *ice = (struct iris_context *)ctx;
299
300 /* Enabling/disabling optional stages requires a URB reconfiguration. */
301 if (!!state != !!ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL])
302 ice->state.dirty |= IRIS_DIRTY_URB;
303
304 bind_state((void *) ctx, state, MESA_SHADER_TESS_EVAL);
305 }
306
307 static void
308 iris_bind_gs_state(struct pipe_context *ctx, void *state)
309 {
310 struct iris_context *ice = (struct iris_context *)ctx;
311
312 /* Enabling/disabling optional stages requires a URB reconfiguration. */
313 if (!!state != !!ice->shaders.uncompiled[MESA_SHADER_GEOMETRY])
314 ice->state.dirty |= IRIS_DIRTY_URB;
315
316 bind_state((void *) ctx, state, MESA_SHADER_GEOMETRY);
317 }
318
319 static void
320 iris_bind_fs_state(struct pipe_context *ctx, void *state)
321 {
322 bind_state((void *) ctx, state, MESA_SHADER_FRAGMENT);
323 }
324
325 static void
326 iris_bind_cs_state(struct pipe_context *ctx, void *state)
327 {
328 bind_state((void *) ctx, state, MESA_SHADER_COMPUTE);
329 }
330
331 /**
332 * Sets up the starting offsets for the groups of binding table entries
333 * common to all pipeline stages.
334 *
335 * Unused groups are initialized to 0xd0d0d0d0 to make it obvious that they're
336 * unused but also make sure that addition of small offsets to them will
337 * trigger some of our asserts that surface indices are < BRW_MAX_SURFACES.
338 */
339 static uint32_t
340 assign_common_binding_table_offsets(const struct gen_device_info *devinfo,
341 const struct nir_shader *nir,
342 struct brw_stage_prog_data *prog_data,
343 uint32_t next_binding_table_offset)
344 {
345 const struct shader_info *info = &nir->info;
346
347 if (info->num_textures) {
348 prog_data->binding_table.texture_start = next_binding_table_offset;
349 prog_data->binding_table.gather_texture_start = next_binding_table_offset;
350 next_binding_table_offset += info->num_textures;
351 } else {
352 prog_data->binding_table.texture_start = 0xd0d0d0d0;
353 prog_data->binding_table.gather_texture_start = 0xd0d0d0d0;
354 }
355
356 if (info->num_images) {
357 prog_data->binding_table.image_start = next_binding_table_offset;
358 next_binding_table_offset += info->num_images;
359 } else {
360 prog_data->binding_table.image_start = 0xd0d0d0d0;
361 }
362
363 int num_ubos = info->num_ubos + (nir->num_uniforms > 0 ? 1 : 0);
364
365 if (num_ubos) {
366 //assert(info->num_ubos <= BRW_MAX_UBO);
367 prog_data->binding_table.ubo_start = next_binding_table_offset;
368 next_binding_table_offset += num_ubos;
369 } else {
370 prog_data->binding_table.ubo_start = 0xd0d0d0d0;
371 }
372
373 if (info->num_ssbos || info->num_abos) {
374 prog_data->binding_table.ssbo_start = next_binding_table_offset;
375 // XXX: see iris_state "wasting 16 binding table slots for ABOs" comment
376 next_binding_table_offset += IRIS_MAX_ABOS + info->num_ssbos;
377 } else {
378 prog_data->binding_table.ssbo_start = 0xd0d0d0d0;
379 }
380
381 prog_data->binding_table.shader_time_start = 0xd0d0d0d0;
382
383 /* This may or may not be used depending on how the compile goes. */
384 prog_data->binding_table.pull_constants_start = next_binding_table_offset;
385 next_binding_table_offset++;
386
387 /* Plane 0 is just the regular texture section */
388 prog_data->binding_table.plane_start[0] = prog_data->binding_table.texture_start;
389
390 prog_data->binding_table.plane_start[1] = next_binding_table_offset;
391 next_binding_table_offset += info->num_textures;
392
393 prog_data->binding_table.plane_start[2] = next_binding_table_offset;
394 next_binding_table_offset += info->num_textures;
395
396 /* Set the binding table size */
397 prog_data->binding_table.size_bytes = next_binding_table_offset * 4;
398
399 return next_binding_table_offset;
400 }
401
402 /**
403 * Associate NIR uniform variables with the prog_data->param[] mechanism
404 * used by the backend. Also, decide which UBOs we'd like to push in an
405 * ideal situation (though the backend can reduce this).
406 */
407 static void
408 iris_setup_uniforms(const struct brw_compiler *compiler,
409 void *mem_ctx,
410 nir_shader *nir,
411 struct brw_stage_prog_data *prog_data)
412 {
413 /* The intel compiler assumes that num_uniforms is in bytes. For
414 * scalar that means 4 bytes per uniform slot.
415 *
416 * Ref: brw_nir_lower_uniforms, type_size_scalar_bytes.
417 */
418 nir->num_uniforms *= 4;
419
420 prog_data->nr_params = 0;
421 prog_data->param = rzalloc_array(mem_ctx, uint32_t, 1);
422
423 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
424
425 nir_builder b;
426 nir_builder_init(&b, impl);
427
428 b.cursor = nir_before_block(nir_start_block(impl));
429 nir_ssa_def *temp_ubo_name = nir_ssa_undef(&b, 1, 32);
430
431 nir_foreach_block(block, impl) {
432 nir_foreach_instr_safe(instr, block) {
433 if (instr->type != nir_instr_type_intrinsic)
434 continue;
435
436 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
437
438 unsigned param_idx = prog_data->nr_params;
439 uint32_t *param = NULL;
440
441 switch (intrin->intrinsic) {
442 case nir_intrinsic_load_user_clip_plane: {
443 unsigned ucp = nir_intrinsic_ucp_id(intrin);
444 param = brw_stage_prog_data_add_params(prog_data, 4);
445 for (int i = 0; i < 4; i++) {
446 param[i] =
447 IRIS_PARAM(BUILTIN, BRW_PARAM_BUILTIN_CLIP_PLANE(ucp, i));
448 }
449 break;
450 }
451 default:
452 continue;
453 }
454
455 b.cursor = nir_before_instr(instr);
456
457 unsigned comps = nir_intrinsic_dest_components(intrin);
458 nir_ssa_def *offset = nir_imm_int(&b, param_idx * sizeof(uint32_t));
459
460 nir_intrinsic_instr *load =
461 nir_intrinsic_instr_create(nir, nir_intrinsic_load_ubo);
462 load->num_components = comps;
463 load->src[0] = nir_src_for_ssa(temp_ubo_name);
464 load->src[1] = nir_src_for_ssa(offset);
465 nir_ssa_dest_init(&load->instr, &load->dest, comps, 32, NULL);
466 nir_builder_instr_insert(&b, &load->instr);
467 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
468 nir_src_for_ssa(&load->dest.ssa));
469 nir_instr_remove(instr);
470 }
471 }
472
473 nir_foreach_block(block, impl) {
474 nir_foreach_instr_safe(instr, block) {
475 if (instr->type != nir_instr_type_intrinsic)
476 continue;
477
478 nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);
479
480 if (load->intrinsic != nir_intrinsic_load_ubo)
481 continue;
482
483 if (load->src[0].ssa == temp_ubo_name) {
484 load->src[0] = nir_src_for_ssa(nir_imm_int(&b, 0));
485 } else if (nir_src_as_uint(load->src[0]) == 0) {
486 nir_ssa_def *offset =
487 nir_iadd(&b, load->src[1].ssa,
488 nir_imm_int(&b, prog_data->nr_params));
489 load->src[1] = nir_src_for_ssa(offset);
490 }
491 }
492 }
493
494 // XXX: vs clip planes?
495 if (nir->info.stage != MESA_SHADER_COMPUTE)
496 brw_nir_analyze_ubo_ranges(compiler, nir, NULL, prog_data->ubo_ranges);
497 }
498
499 /**
500 * If we still have regular uniforms as push constants after the backend
501 * compilation, set up a UBO range for them. This will be used to fill
502 * out the 3DSTATE_CONSTANT_* packets which cause the data to be pushed.
503 */
504 static void
505 iris_setup_push_uniform_range(const struct brw_compiler *compiler,
506 struct brw_stage_prog_data *prog_data)
507 {
508 if (prog_data->nr_params) {
509 for (int i = 3; i > 0; i--)
510 prog_data->ubo_ranges[i] = prog_data->ubo_ranges[i - 1];
511
512 prog_data->ubo_ranges[0] = (struct brw_ubo_range) {
513 .block = 0,
514 .start = 0,
515 .length = DIV_ROUND_UP(prog_data->nr_params, 8),
516 };
517 }
518 }
519
520 /**
521 * Compile a vertex shader, and upload the assembly.
522 */
523 static bool
524 iris_compile_vs(struct iris_context *ice,
525 struct iris_uncompiled_shader *ish,
526 const struct brw_vs_prog_key *key)
527 {
528 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
529 const struct brw_compiler *compiler = screen->compiler;
530 const struct gen_device_info *devinfo = &screen->devinfo;
531 void *mem_ctx = ralloc_context(NULL);
532 struct brw_vs_prog_data *vs_prog_data =
533 rzalloc(mem_ctx, struct brw_vs_prog_data);
534 struct brw_vue_prog_data *vue_prog_data = &vs_prog_data->base;
535 struct brw_stage_prog_data *prog_data = &vue_prog_data->base;
536
537 nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);
538
539 // XXX: alt mode
540 assign_common_binding_table_offsets(devinfo, nir, prog_data, 0);
541
542 iris_setup_uniforms(compiler, mem_ctx, nir, prog_data);
543
544 brw_compute_vue_map(devinfo,
545 &vue_prog_data->vue_map, nir->info.outputs_written,
546 nir->info.separate_shader);
547
548 char *error_str = NULL;
549 const unsigned *program =
550 brw_compile_vs(compiler, &ice->dbg, mem_ctx, key, vs_prog_data,
551 nir, -1, &error_str);
552 if (program == NULL) {
553 dbg_printf("Failed to compile vertex shader: %s\n", error_str);
554 ralloc_free(mem_ctx);
555 return false;
556 }
557
558 iris_setup_push_uniform_range(compiler, prog_data);
559
560 uint32_t *so_decls =
561 ice->vtbl.create_so_decl_list(&ish->stream_output,
562 &vue_prog_data->vue_map);
563
564 iris_upload_and_bind_shader(ice, IRIS_CACHE_VS, key, program, prog_data,
565 so_decls);
566
567 ralloc_free(mem_ctx);
568 return true;
569 }
570
571 /**
572 * Update the current vertex shader variant.
573 *
574 * Fill out the key, look in the cache, compile and bind if needed.
575 */
576 static void
577 iris_update_compiled_vs(struct iris_context *ice)
578 {
579 struct iris_uncompiled_shader *ish =
580 ice->shaders.uncompiled[MESA_SHADER_VERTEX];
581
582 struct brw_vs_prog_key key = { .program_string_id = ish->program_id };
583 ice->vtbl.populate_vs_key(ice, &key);
584
585 if (iris_bind_cached_shader(ice, IRIS_CACHE_VS, &key))
586 return;
587
588 UNUSED bool success = iris_compile_vs(ice, ish, &key);
589 }
590
591 /**
592 * Get the shader_info for a given stage, or NULL if the stage is disabled.
593 */
594 const struct shader_info *
595 iris_get_shader_info(const struct iris_context *ice, gl_shader_stage stage)
596 {
597 const struct iris_uncompiled_shader *ish = ice->shaders.uncompiled[stage];
598
599 if (!ish)
600 return NULL;
601
602 const nir_shader *nir = ish->nir;
603 return &nir->info;
604 }
605
606 // XXX: this function is gross
607 unsigned
608 iris_get_shader_num_ubos(const struct iris_context *ice, gl_shader_stage stage)
609 {
610 const struct iris_uncompiled_shader *ish = ice->shaders.uncompiled[stage];
611
612 if (ish) {
613 const nir_shader *nir = ish->nir;
614 /* see assign_common_binding_table_offsets */
615 return nir->info.num_ubos + (nir->num_uniforms > 0 ? 1 : 0);
616 }
617 return 0;
618 }
619
620 /**
621 * Get the union of TCS output and TES input slots.
622 *
623 * TCS and TES need to agree on a common URB entry layout. In particular,
624 * the data for all patch vertices is stored in a single URB entry (unlike
625 * GS which has one entry per input vertex). This means that per-vertex
626 * array indexing needs a stride.
627 *
628 * SSO requires locations to match, but doesn't require the number of
629 * outputs/inputs to match (in fact, the TCS often has extra outputs).
630 * So, we need to take the extra step of unifying these on the fly.
631 */
632 static void
633 get_unified_tess_slots(const struct iris_context *ice,
634 uint64_t *per_vertex_slots,
635 uint32_t *per_patch_slots)
636 {
637 const struct shader_info *tcs =
638 iris_get_shader_info(ice, MESA_SHADER_TESS_CTRL);
639 const struct shader_info *tes =
640 iris_get_shader_info(ice, MESA_SHADER_TESS_EVAL);
641
642 *per_vertex_slots = tes->inputs_read;
643 *per_patch_slots = tes->patch_inputs_read;
644
645 if (tcs) {
646 *per_vertex_slots |= tcs->inputs_read;
647 *per_patch_slots |= tcs->patch_inputs_read;
648 }
649 }
650
651 /**
652 * Compile a tessellation control shader, and upload the assembly.
653 */
654 static bool
655 iris_compile_tcs(struct iris_context *ice,
656 struct iris_uncompiled_shader *ish,
657 const struct brw_tcs_prog_key *key)
658 {
659 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
660 const struct brw_compiler *compiler = screen->compiler;
661 const struct nir_shader_compiler_options *options =
662 compiler->glsl_compiler_options[MESA_SHADER_TESS_CTRL].NirOptions;
663 const struct gen_device_info *devinfo = &screen->devinfo;
664 void *mem_ctx = ralloc_context(NULL);
665 struct brw_tcs_prog_data *tcs_prog_data =
666 rzalloc(mem_ctx, struct brw_tcs_prog_data);
667 struct brw_vue_prog_data *vue_prog_data = &tcs_prog_data->base;
668 struct brw_stage_prog_data *prog_data = &vue_prog_data->base;
669
670 nir_shader *nir;
671
672 if (ish) {
673 nir = nir_shader_clone(mem_ctx, ish->nir);
674
675 assign_common_binding_table_offsets(devinfo, nir, prog_data, 0);
676 iris_setup_uniforms(compiler, mem_ctx, nir, prog_data);
677 } else {
678 nir = brw_nir_create_passthrough_tcs(mem_ctx, compiler, options, key);
679
680 /* Reserve space for passing the default tess levels as constants. */
681 prog_data->param = rzalloc_array(mem_ctx, uint32_t, 8);
682 prog_data->nr_params = 8;
683 prog_data->ubo_ranges[0].length = 1;
684 }
685
686 char *error_str = NULL;
687 const unsigned *program =
688 brw_compile_tcs(compiler, &ice->dbg, mem_ctx, key, tcs_prog_data, nir,
689 -1, &error_str);
690 if (program == NULL) {
691 dbg_printf("Failed to compile evaluation shader: %s\n", error_str);
692 ralloc_free(mem_ctx);
693 return false;
694 }
695
696 iris_setup_push_uniform_range(compiler, prog_data);
697
698 iris_upload_and_bind_shader(ice, IRIS_CACHE_TCS, key, program, prog_data,
699 NULL);
700
701 ralloc_free(mem_ctx);
702 return true;
703 }
704
705 /**
706 * Update the current tessellation control shader variant.
707 *
708 * Fill out the key, look in the cache, compile and bind if needed.
709 */
710 static void
711 iris_update_compiled_tcs(struct iris_context *ice)
712 {
713 struct iris_uncompiled_shader *tcs =
714 ice->shaders.uncompiled[MESA_SHADER_TESS_CTRL];
715
716 const struct shader_info *tes_info =
717 iris_get_shader_info(ice, MESA_SHADER_TESS_EVAL);
718 struct brw_tcs_prog_key key = {
719 .program_string_id = tcs ? tcs->program_id : 0,
720 .tes_primitive_mode = tes_info->tess.primitive_mode,
721 .input_vertices = ice->state.vertices_per_patch,
722 };
723 get_unified_tess_slots(ice, &key.outputs_written,
724 &key.patch_outputs_written);
725 ice->vtbl.populate_tcs_key(ice, &key);
726
727 if (iris_bind_cached_shader(ice, IRIS_CACHE_TCS, &key))
728 return;
729
730 UNUSED bool success = iris_compile_tcs(ice, tcs, &key);
731 }
732
733 /**
734 * Compile a tessellation evaluation shader, and upload the assembly.
735 */
736 static bool
737 iris_compile_tes(struct iris_context *ice,
738 struct iris_uncompiled_shader *ish,
739 const struct brw_tes_prog_key *key)
740 {
741 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
742 const struct brw_compiler *compiler = screen->compiler;
743 const struct gen_device_info *devinfo = &screen->devinfo;
744 void *mem_ctx = ralloc_context(NULL);
745 struct brw_tes_prog_data *tes_prog_data =
746 rzalloc(mem_ctx, struct brw_tes_prog_data);
747 struct brw_vue_prog_data *vue_prog_data = &tes_prog_data->base;
748 struct brw_stage_prog_data *prog_data = &vue_prog_data->base;
749
750 nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);
751
752 assign_common_binding_table_offsets(devinfo, nir, prog_data, 0);
753
754 iris_setup_uniforms(compiler, mem_ctx, nir, prog_data);
755
756 struct brw_vue_map input_vue_map;
757 brw_compute_tess_vue_map(&input_vue_map, key->inputs_read,
758 key->patch_inputs_read);
759
760 char *error_str = NULL;
761 const unsigned *program =
762 brw_compile_tes(compiler, &ice->dbg, mem_ctx, key, &input_vue_map,
763 tes_prog_data, nir, NULL, -1, &error_str);
764 if (program == NULL) {
765 dbg_printf("Failed to compile evaluation shader: %s\n", error_str);
766 ralloc_free(mem_ctx);
767 return false;
768 }
769
770 iris_setup_push_uniform_range(compiler, prog_data);
771
772 uint32_t *so_decls =
773 ice->vtbl.create_so_decl_list(&ish->stream_output,
774 &vue_prog_data->vue_map);
775
776 iris_upload_and_bind_shader(ice, IRIS_CACHE_TES, key, program, prog_data,
777 so_decls);
778
779 ralloc_free(mem_ctx);
780 return true;
781 }
782
783 /**
784 * Update the current tessellation evaluation shader variant.
785 *
786 * Fill out the key, look in the cache, compile and bind if needed.
787 */
788 static void
789 iris_update_compiled_tes(struct iris_context *ice)
790 {
791 struct iris_uncompiled_shader *ish =
792 ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL];
793
794 struct brw_tes_prog_key key = { .program_string_id = ish->program_id };
795 get_unified_tess_slots(ice, &key.inputs_read, &key.patch_inputs_read);
796 ice->vtbl.populate_tes_key(ice, &key);
797
798 if (iris_bind_cached_shader(ice, IRIS_CACHE_TES, &key))
799 return;
800
801 UNUSED bool success = iris_compile_tes(ice, ish, &key);
802 }
803
804 /**
805 * Compile a geometry shader, and upload the assembly.
806 */
807 static bool
808 iris_compile_gs(struct iris_context *ice,
809 struct iris_uncompiled_shader *ish,
810 const struct brw_gs_prog_key *key)
811 {
812 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
813 const struct brw_compiler *compiler = screen->compiler;
814 const struct gen_device_info *devinfo = &screen->devinfo;
815 void *mem_ctx = ralloc_context(NULL);
816 struct brw_gs_prog_data *gs_prog_data =
817 rzalloc(mem_ctx, struct brw_gs_prog_data);
818 struct brw_vue_prog_data *vue_prog_data = &gs_prog_data->base;
819 struct brw_stage_prog_data *prog_data = &vue_prog_data->base;
820
821 nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);
822
823 assign_common_binding_table_offsets(devinfo, nir, prog_data, 0);
824
825 iris_setup_uniforms(compiler, mem_ctx, nir, prog_data);
826
827 brw_compute_vue_map(devinfo,
828 &vue_prog_data->vue_map, nir->info.outputs_written,
829 nir->info.separate_shader);
830
831 char *error_str = NULL;
832 const unsigned *program =
833 brw_compile_gs(compiler, &ice->dbg, mem_ctx, key, gs_prog_data, nir,
834 NULL, -1, &error_str);
835 if (program == NULL) {
836 dbg_printf("Failed to compile geometry shader: %s\n", error_str);
837 ralloc_free(mem_ctx);
838 return false;
839 }
840
841 iris_setup_push_uniform_range(compiler, prog_data);
842
843 uint32_t *so_decls =
844 ice->vtbl.create_so_decl_list(&ish->stream_output,
845 &vue_prog_data->vue_map);
846
847 iris_upload_and_bind_shader(ice, IRIS_CACHE_GS, key, program, prog_data,
848 so_decls);
849
850 ralloc_free(mem_ctx);
851 return true;
852 }
853
854 /**
855 * Update the current geometry shader variant.
856 *
857 * Fill out the key, look in the cache, compile and bind if needed.
858 */
859 static void
860 iris_update_compiled_gs(struct iris_context *ice)
861 {
862 struct iris_uncompiled_shader *ish =
863 ice->shaders.uncompiled[MESA_SHADER_GEOMETRY];
864
865 if (!ish) {
866 iris_unbind_shader(ice, IRIS_CACHE_GS);
867 return;
868 }
869
870 struct brw_gs_prog_key key = { .program_string_id = ish->program_id };
871 ice->vtbl.populate_gs_key(ice, &key);
872
873 if (iris_bind_cached_shader(ice, IRIS_CACHE_GS, &key))
874 return;
875
876 UNUSED bool success = iris_compile_gs(ice, ish, &key);
877 }
878
879 /**
880 * Compile a fragment (pixel) shader, and upload the assembly.
881 */
882 static bool
883 iris_compile_fs(struct iris_context *ice,
884 struct iris_uncompiled_shader *ish,
885 const struct brw_wm_prog_key *key,
886 struct brw_vue_map *vue_map)
887 {
888 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
889 const struct brw_compiler *compiler = screen->compiler;
890 const struct gen_device_info *devinfo = &screen->devinfo;
891 void *mem_ctx = ralloc_context(NULL);
892 struct brw_wm_prog_data *fs_prog_data =
893 rzalloc(mem_ctx, struct brw_wm_prog_data);
894 struct brw_stage_prog_data *prog_data = &fs_prog_data->base;
895
896 nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);
897
898 // XXX: alt mode
899 assign_common_binding_table_offsets(devinfo, nir, prog_data,
900 MAX2(key->nr_color_regions, 1));
901
902 iris_setup_uniforms(compiler, mem_ctx, nir, prog_data);
903
904 char *error_str = NULL;
905 const unsigned *program =
906 brw_compile_fs(compiler, &ice->dbg, mem_ctx, key, fs_prog_data,
907 nir, NULL, -1, -1, -1, true, false, vue_map, &error_str);
908 if (program == NULL) {
909 dbg_printf("Failed to compile fragment shader: %s\n", error_str);
910 ralloc_free(mem_ctx);
911 return false;
912 }
913
914 //brw_alloc_stage_scratch(brw, &brw->wm.base, prog_data.base.total_scratch);
915
916 iris_setup_push_uniform_range(compiler, prog_data);
917
918 iris_upload_and_bind_shader(ice, IRIS_CACHE_FS, key, program, prog_data,
919 NULL);
920
921 ralloc_free(mem_ctx);
922 return true;
923 }
924
925 /**
926 * Update the current fragment shader variant.
927 *
928 * Fill out the key, look in the cache, compile and bind if needed.
929 */
930 static void
931 iris_update_compiled_fs(struct iris_context *ice)
932 {
933 struct iris_uncompiled_shader *ish =
934 ice->shaders.uncompiled[MESA_SHADER_FRAGMENT];
935 struct brw_wm_prog_key key = { .program_string_id = ish->program_id };
936 ice->vtbl.populate_fs_key(ice, &key);
937
938 if (ish->nos & IRIS_NOS_LAST_VUE_MAP)
939 key.input_slots_valid = ice->shaders.last_vue_map->slots_valid;
940
941 if (iris_bind_cached_shader(ice, IRIS_CACHE_FS, &key))
942 return;
943
944 UNUSED bool success =
945 iris_compile_fs(ice, ish, &key, ice->shaders.last_vue_map);
946 }
947
948 /**
949 * Get the compiled shader for the last enabled geometry stage.
950 *
951 * This stage is the one which will feed stream output and the rasterizer.
952 */
953 static struct iris_compiled_shader *
954 last_vue_shader(struct iris_context *ice)
955 {
956 if (ice->shaders.prog[MESA_SHADER_GEOMETRY])
957 return ice->shaders.prog[MESA_SHADER_GEOMETRY];
958
959 if (ice->shaders.prog[MESA_SHADER_TESS_EVAL])
960 return ice->shaders.prog[MESA_SHADER_TESS_EVAL];
961
962 return ice->shaders.prog[MESA_SHADER_VERTEX];
963 }
964
965 /**
966 * Update the last enabled stage's VUE map.
967 *
968 * When the shader feeding the rasterizer's output interface changes, we
969 * need to re-emit various packets.
970 */
971 static void
972 update_last_vue_map(struct iris_context *ice,
973 struct brw_stage_prog_data *prog_data)
974 {
975 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
976 struct brw_vue_map *vue_map = &vue_prog_data->vue_map;
977 struct brw_vue_map *old_map = ice->shaders.last_vue_map;
978 const uint64_t changed_slots =
979 (old_map ? old_map->slots_valid : 0ull) ^ vue_map->slots_valid;
980
981 if (changed_slots & VARYING_BIT_VIEWPORT) {
982 // XXX: could use ctx->Const.MaxViewports for old API efficiency
983 ice->state.num_viewports =
984 (vue_map->slots_valid & VARYING_BIT_VIEWPORT) ? IRIS_MAX_VIEWPORTS : 1;
985 ice->state.dirty |= IRIS_DIRTY_CLIP |
986 IRIS_DIRTY_SF_CL_VIEWPORT |
987 IRIS_DIRTY_CC_VIEWPORT |
988 IRIS_DIRTY_SCISSOR_RECT |
989 IRIS_DIRTY_UNCOMPILED_FS |
990 ice->state.dirty_for_nos[IRIS_NOS_LAST_VUE_MAP];
991 // XXX: CC_VIEWPORT?
992 }
993
994 if (changed_slots || (old_map && old_map->separate != vue_map->separate)) {
995 ice->state.dirty |= IRIS_DIRTY_SBE;
996 }
997
998 ice->shaders.last_vue_map = &vue_prog_data->vue_map;
999 }
1000
1001 /**
1002 * Get the prog_data for a given stage, or NULL if the stage is disabled.
1003 */
1004 static struct brw_vue_prog_data *
1005 get_vue_prog_data(struct iris_context *ice, gl_shader_stage stage)
1006 {
1007 if (!ice->shaders.prog[stage])
1008 return NULL;
1009
1010 return (void *) ice->shaders.prog[stage]->prog_data;
1011 }
1012
1013 /**
1014 * Update the current shader variants for the given state.
1015 *
1016 * This should be called on every draw call to ensure that the correct
1017 * shaders are bound. It will also flag any dirty state triggered by
1018 * swapping out those shaders.
1019 */
1020 void
1021 iris_update_compiled_shaders(struct iris_context *ice)
1022 {
1023 const uint64_t dirty = ice->state.dirty;
1024
1025 struct brw_vue_prog_data *old_prog_datas[4];
1026 if (!(dirty & IRIS_DIRTY_URB)) {
1027 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++)
1028 old_prog_datas[i] = get_vue_prog_data(ice, i);
1029 }
1030
1031 if (dirty & (IRIS_DIRTY_UNCOMPILED_TCS | IRIS_DIRTY_UNCOMPILED_TES)) {
1032 struct iris_uncompiled_shader *tes =
1033 ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL];
1034 if (tes) {
1035 iris_update_compiled_tcs(ice);
1036 iris_update_compiled_tes(ice);
1037 } else {
1038 iris_unbind_shader(ice, IRIS_CACHE_TCS);
1039 iris_unbind_shader(ice, IRIS_CACHE_TES);
1040 }
1041 }
1042
1043 if (dirty & IRIS_DIRTY_UNCOMPILED_VS)
1044 iris_update_compiled_vs(ice);
1045 if (dirty & IRIS_DIRTY_UNCOMPILED_GS)
1046 iris_update_compiled_gs(ice);
1047
1048 struct iris_compiled_shader *shader = last_vue_shader(ice);
1049 update_last_vue_map(ice, shader->prog_data);
1050 if (ice->state.streamout != shader->streamout) {
1051 ice->state.streamout = shader->streamout;
1052 ice->state.dirty |= IRIS_DIRTY_SO_DECL_LIST | IRIS_DIRTY_STREAMOUT;
1053 }
1054
1055 if (dirty & IRIS_DIRTY_UNCOMPILED_FS)
1056 iris_update_compiled_fs(ice);
1057 // ...
1058
1059 /* Changing shader interfaces may require a URB configuration. */
1060 if (!(dirty & IRIS_DIRTY_URB)) {
1061 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
1062 struct brw_vue_prog_data *old = old_prog_datas[i];
1063 struct brw_vue_prog_data *new = get_vue_prog_data(ice, i);
1064 if (!!old != !!new ||
1065 (new && new->urb_entry_size != old->urb_entry_size)) {
1066 ice->state.dirty |= IRIS_DIRTY_URB;
1067 break;
1068 }
1069 }
1070 }
1071 }
1072
1073 static bool
1074 iris_compile_cs(struct iris_context *ice,
1075 struct iris_uncompiled_shader *ish,
1076 const struct brw_cs_prog_key *key)
1077 {
1078 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
1079 const struct brw_compiler *compiler = screen->compiler;
1080 const struct gen_device_info *devinfo = &screen->devinfo;
1081 void *mem_ctx = ralloc_context(NULL);
1082 struct brw_cs_prog_data *cs_prog_data =
1083 rzalloc(mem_ctx, struct brw_cs_prog_data);
1084 struct brw_stage_prog_data *prog_data = &cs_prog_data->base;
1085
1086 nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);
1087
1088 cs_prog_data->binding_table.work_groups_start = 0;
1089 assign_common_binding_table_offsets(devinfo, nir, prog_data, 1);
1090
1091 iris_setup_uniforms(compiler, mem_ctx, nir, prog_data);
1092
1093 char *error_str = NULL;
1094 const unsigned *program =
1095 brw_compile_cs(compiler, &ice->dbg, mem_ctx, key, cs_prog_data,
1096 nir, -1, &error_str);
1097 if (program == NULL) {
1098 dbg_printf("Failed to compile compute shader: %s\n", error_str);
1099 ralloc_free(mem_ctx);
1100 return false;
1101 }
1102
1103 iris_upload_and_bind_shader(ice, IRIS_CACHE_CS, key, program, prog_data,
1104 NULL);
1105
1106 ralloc_free(mem_ctx);
1107 return true;
1108 }
1109
1110 void
1111 iris_update_compiled_compute_shader(struct iris_context *ice)
1112 {
1113 struct iris_uncompiled_shader *ish =
1114 ice->shaders.uncompiled[MESA_SHADER_COMPUTE];
1115
1116 struct brw_cs_prog_key key = { .program_string_id = ish->program_id };
1117 ice->vtbl.populate_cs_key(ice, &key);
1118
1119 if (iris_bind_cached_shader(ice, IRIS_CACHE_CS, &key))
1120 return;
1121
1122 UNUSED bool success = iris_compile_cs(ice, ish, &key);
1123 }
1124
1125 void
1126 iris_fill_cs_push_const_buffer(struct brw_cs_prog_data *cs_prog_data,
1127 uint32_t *dst)
1128 {
1129 struct brw_stage_prog_data *prog_data = &cs_prog_data->base;
1130 assert(cs_prog_data->push.total.size > 0);
1131 assert(cs_prog_data->push.cross_thread.size == 0);
1132 assert(cs_prog_data->push.per_thread.dwords == 1);
1133 assert(prog_data->param[0] == BRW_PARAM_BUILTIN_SUBGROUP_ID);
1134 for (unsigned t = 0; t < cs_prog_data->threads; t++)
1135 dst[8 * t] = t;
1136 }
1137
1138 /**
1139 * Allocate scratch BOs as needed for the given per-thread size and stage.
1140 *
1141 * Returns the 32-bit "Scratch Space Base Pointer" value.
1142 */
1143 uint32_t
1144 iris_get_scratch_space(struct iris_context *ice,
1145 unsigned per_thread_scratch,
1146 gl_shader_stage stage)
1147 {
1148 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
1149 struct iris_bufmgr *bufmgr = screen->bufmgr;
1150 const struct gen_device_info *devinfo = &screen->devinfo;
1151
1152 unsigned encoded_size = ffs(per_thread_scratch) - 11;
1153 assert(encoded_size < (1 << 16));
1154
1155 struct iris_bo **bop = &ice->shaders.scratch_bos[encoded_size][stage];
1156
1157 /* The documentation for 3DSTATE_PS "Scratch Space Base Pointer" says:
1158 *
1159 * "Scratch Space per slice is computed based on 4 sub-slices. SW must
1160 * allocate scratch space enough so that each slice has 4 slices
1161 * allowed."
1162 *
1163 * According to the other driver team, this applies to compute shaders
1164 * as well. This is not currently documented at all.
1165 */
1166 unsigned subslice_total = 4 * devinfo->num_slices;
1167 assert(subslice_total >= screen->subslice_total);
1168
1169 if (!*bop) {
1170 unsigned scratch_ids_per_subslice = devinfo->max_cs_threads;
1171 uint32_t max_threads[] = {
1172 [MESA_SHADER_VERTEX] = devinfo->max_vs_threads,
1173 [MESA_SHADER_TESS_CTRL] = devinfo->max_tcs_threads,
1174 [MESA_SHADER_TESS_EVAL] = devinfo->max_tes_threads,
1175 [MESA_SHADER_GEOMETRY] = devinfo->max_gs_threads,
1176 [MESA_SHADER_FRAGMENT] = devinfo->max_wm_threads,
1177 [MESA_SHADER_COMPUTE] = scratch_ids_per_subslice * subslice_total,
1178 };
1179
1180 uint32_t size = per_thread_scratch * max_threads[stage];
1181
1182 *bop = iris_bo_alloc(bufmgr, "scratch", size, IRIS_MEMZONE_SHADER);
1183 }
1184
1185 return (*bop)->gtt_offset;
1186 }
1187
1188 void
1189 iris_init_program_functions(struct pipe_context *ctx)
1190 {
1191 ctx->create_vs_state = iris_create_shader_state;
1192 ctx->create_tcs_state = iris_create_shader_state;
1193 ctx->create_tes_state = iris_create_shader_state;
1194 ctx->create_gs_state = iris_create_shader_state;
1195 ctx->create_fs_state = iris_create_shader_state;
1196 ctx->create_compute_state = iris_create_compute_state;
1197
1198 ctx->delete_vs_state = iris_delete_shader_state;
1199 ctx->delete_tcs_state = iris_delete_shader_state;
1200 ctx->delete_tes_state = iris_delete_shader_state;
1201 ctx->delete_gs_state = iris_delete_shader_state;
1202 ctx->delete_fs_state = iris_delete_shader_state;
1203 ctx->delete_compute_state = iris_delete_shader_state;
1204
1205 ctx->bind_vs_state = iris_bind_vs_state;
1206 ctx->bind_tcs_state = iris_bind_tcs_state;
1207 ctx->bind_tes_state = iris_bind_tes_state;
1208 ctx->bind_gs_state = iris_bind_gs_state;
1209 ctx->bind_fs_state = iris_bind_fs_state;
1210 ctx->bind_compute_state = iris_bind_cs_state;
1211 }