intel/fs: Implement nir_intrinsic_load_fs_input_interp_deltas
[mesa.git] / src / intel / compiler / brw_nir.c
1 /*
2 * Copyright © 2014 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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "brw_nir.h"
25 #include "brw_shader.h"
26 #include "dev/gen_debug.h"
27 #include "compiler/glsl_types.h"
28 #include "compiler/nir/nir_builder.h"
29 #include "util/u_math.h"
30
31 static bool
32 is_input(nir_intrinsic_instr *intrin)
33 {
34 return intrin->intrinsic == nir_intrinsic_load_input ||
35 intrin->intrinsic == nir_intrinsic_load_per_vertex_input ||
36 intrin->intrinsic == nir_intrinsic_load_interpolated_input ||
37 intrin->intrinsic == nir_intrinsic_load_fs_input_interp_deltas;
38 }
39
40 static bool
41 is_output(nir_intrinsic_instr *intrin)
42 {
43 return intrin->intrinsic == nir_intrinsic_load_output ||
44 intrin->intrinsic == nir_intrinsic_load_per_vertex_output ||
45 intrin->intrinsic == nir_intrinsic_store_output ||
46 intrin->intrinsic == nir_intrinsic_store_per_vertex_output;
47 }
48
49 /**
50 * In many cases, we just add the base and offset together, so there's no
51 * reason to keep them separate. Sometimes, combining them is essential:
52 * if a shader only accesses part of a compound variable (such as a matrix
53 * or array), the variable's base may not actually exist in the VUE map.
54 *
55 * This pass adds constant offsets to instr->const_index[0], and resets
56 * the offset source to 0. Non-constant offsets remain unchanged - since
57 * we don't know what part of a compound variable is accessed, we allocate
58 * storage for the entire thing.
59 */
60
61 static bool
62 add_const_offset_to_base_block(nir_block *block, nir_builder *b,
63 nir_variable_mode mode)
64 {
65 nir_foreach_instr_safe(instr, block) {
66 if (instr->type != nir_instr_type_intrinsic)
67 continue;
68
69 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
70
71 if ((mode == nir_var_shader_in && is_input(intrin)) ||
72 (mode == nir_var_shader_out && is_output(intrin))) {
73 nir_src *offset = nir_get_io_offset_src(intrin);
74
75 if (nir_src_is_const(*offset)) {
76 intrin->const_index[0] += nir_src_as_uint(*offset);
77 b->cursor = nir_before_instr(&intrin->instr);
78 nir_instr_rewrite_src(&intrin->instr, offset,
79 nir_src_for_ssa(nir_imm_int(b, 0)));
80 }
81 }
82 }
83 return true;
84 }
85
86 static void
87 add_const_offset_to_base(nir_shader *nir, nir_variable_mode mode)
88 {
89 nir_foreach_function(f, nir) {
90 if (f->impl) {
91 nir_builder b;
92 nir_builder_init(&b, f->impl);
93 nir_foreach_block(block, f->impl) {
94 add_const_offset_to_base_block(block, &b, mode);
95 }
96 }
97 }
98 }
99
100 static bool
101 remap_tess_levels(nir_builder *b, nir_intrinsic_instr *intr,
102 GLenum primitive_mode)
103 {
104 const int location = nir_intrinsic_base(intr);
105 const unsigned component = nir_intrinsic_component(intr);
106 bool out_of_bounds;
107
108 if (location == VARYING_SLOT_TESS_LEVEL_INNER) {
109 switch (primitive_mode) {
110 case GL_QUADS:
111 /* gl_TessLevelInner[0..1] lives at DWords 3-2 (reversed). */
112 nir_intrinsic_set_base(intr, 0);
113 nir_intrinsic_set_component(intr, 3 - component);
114 out_of_bounds = false;
115 break;
116 case GL_TRIANGLES:
117 /* gl_TessLevelInner[0] lives at DWord 4. */
118 nir_intrinsic_set_base(intr, 1);
119 out_of_bounds = component > 0;
120 break;
121 case GL_ISOLINES:
122 out_of_bounds = true;
123 break;
124 default:
125 unreachable("Bogus tessellation domain");
126 }
127 } else if (location == VARYING_SLOT_TESS_LEVEL_OUTER) {
128 if (primitive_mode == GL_ISOLINES) {
129 /* gl_TessLevelOuter[0..1] lives at DWords 6-7 (in order). */
130 nir_intrinsic_set_base(intr, 1);
131 nir_intrinsic_set_component(intr, 2 + nir_intrinsic_component(intr));
132 out_of_bounds = component > 1;
133 } else {
134 /* Triangles use DWords 7-5 (reversed); Quads use 7-4 (reversed) */
135 nir_intrinsic_set_base(intr, 1);
136 nir_intrinsic_set_component(intr, 3 - nir_intrinsic_component(intr));
137 out_of_bounds = component == 3 && primitive_mode == GL_TRIANGLES;
138 }
139 } else {
140 return false;
141 }
142
143 if (out_of_bounds) {
144 if (nir_intrinsic_infos[intr->intrinsic].has_dest) {
145 b->cursor = nir_before_instr(&intr->instr);
146 nir_ssa_def *undef = nir_ssa_undef(b, 1, 32);
147 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(undef));
148 }
149 nir_instr_remove(&intr->instr);
150 }
151
152 return true;
153 }
154
155 static bool
156 remap_patch_urb_offsets(nir_block *block, nir_builder *b,
157 const struct brw_vue_map *vue_map,
158 GLenum tes_primitive_mode)
159 {
160 const bool is_passthrough_tcs = b->shader->info.name &&
161 strcmp(b->shader->info.name, "passthrough") == 0;
162
163 nir_foreach_instr_safe(instr, block) {
164 if (instr->type != nir_instr_type_intrinsic)
165 continue;
166
167 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
168
169 gl_shader_stage stage = b->shader->info.stage;
170
171 if ((stage == MESA_SHADER_TESS_CTRL && is_output(intrin)) ||
172 (stage == MESA_SHADER_TESS_EVAL && is_input(intrin))) {
173
174 if (!is_passthrough_tcs &&
175 remap_tess_levels(b, intrin, tes_primitive_mode))
176 continue;
177
178 int vue_slot = vue_map->varying_to_slot[intrin->const_index[0]];
179 assert(vue_slot != -1);
180 intrin->const_index[0] = vue_slot;
181
182 nir_src *vertex = nir_get_io_vertex_index_src(intrin);
183 if (vertex) {
184 if (nir_src_is_const(*vertex)) {
185 intrin->const_index[0] += nir_src_as_uint(*vertex) *
186 vue_map->num_per_vertex_slots;
187 } else {
188 b->cursor = nir_before_instr(&intrin->instr);
189
190 /* Multiply by the number of per-vertex slots. */
191 nir_ssa_def *vertex_offset =
192 nir_imul(b,
193 nir_ssa_for_src(b, *vertex, 1),
194 nir_imm_int(b,
195 vue_map->num_per_vertex_slots));
196
197 /* Add it to the existing offset */
198 nir_src *offset = nir_get_io_offset_src(intrin);
199 nir_ssa_def *total_offset =
200 nir_iadd(b, vertex_offset,
201 nir_ssa_for_src(b, *offset, 1));
202
203 nir_instr_rewrite_src(&intrin->instr, offset,
204 nir_src_for_ssa(total_offset));
205 }
206 }
207 }
208 }
209 return true;
210 }
211
212 void
213 brw_nir_lower_vs_inputs(nir_shader *nir,
214 const uint8_t *vs_attrib_wa_flags)
215 {
216 /* Start with the location of the variable's base. */
217 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
218 var->data.driver_location = var->data.location;
219 }
220
221 /* Now use nir_lower_io to walk dereference chains. Attribute arrays are
222 * loaded as one vec4 or dvec4 per element (or matrix column), depending on
223 * whether it is a double-precision type or not.
224 */
225 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, 0);
226
227 /* This pass needs actual constants */
228 nir_opt_constant_folding(nir);
229
230 add_const_offset_to_base(nir, nir_var_shader_in);
231
232 brw_nir_apply_attribute_workarounds(nir, vs_attrib_wa_flags);
233
234 /* The last step is to remap VERT_ATTRIB_* to actual registers */
235
236 /* Whether or not we have any system generated values. gl_DrawID is not
237 * included here as it lives in its own vec4.
238 */
239 const bool has_sgvs =
240 nir->info.system_values_read &
241 (BITFIELD64_BIT(SYSTEM_VALUE_FIRST_VERTEX) |
242 BITFIELD64_BIT(SYSTEM_VALUE_BASE_INSTANCE) |
243 BITFIELD64_BIT(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) |
244 BITFIELD64_BIT(SYSTEM_VALUE_INSTANCE_ID));
245
246 const unsigned num_inputs = util_bitcount64(nir->info.inputs_read);
247
248 nir_foreach_function(function, nir) {
249 if (!function->impl)
250 continue;
251
252 nir_builder b;
253 nir_builder_init(&b, function->impl);
254
255 nir_foreach_block(block, function->impl) {
256 nir_foreach_instr_safe(instr, block) {
257 if (instr->type != nir_instr_type_intrinsic)
258 continue;
259
260 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
261
262 switch (intrin->intrinsic) {
263 case nir_intrinsic_load_first_vertex:
264 case nir_intrinsic_load_base_instance:
265 case nir_intrinsic_load_vertex_id_zero_base:
266 case nir_intrinsic_load_instance_id:
267 case nir_intrinsic_load_is_indexed_draw:
268 case nir_intrinsic_load_draw_id: {
269 b.cursor = nir_after_instr(&intrin->instr);
270
271 /* gl_VertexID and friends are stored by the VF as the last
272 * vertex element. We convert them to load_input intrinsics at
273 * the right location.
274 */
275 nir_intrinsic_instr *load =
276 nir_intrinsic_instr_create(nir, nir_intrinsic_load_input);
277 load->src[0] = nir_src_for_ssa(nir_imm_int(&b, 0));
278
279 nir_intrinsic_set_base(load, num_inputs);
280 switch (intrin->intrinsic) {
281 case nir_intrinsic_load_first_vertex:
282 nir_intrinsic_set_component(load, 0);
283 break;
284 case nir_intrinsic_load_base_instance:
285 nir_intrinsic_set_component(load, 1);
286 break;
287 case nir_intrinsic_load_vertex_id_zero_base:
288 nir_intrinsic_set_component(load, 2);
289 break;
290 case nir_intrinsic_load_instance_id:
291 nir_intrinsic_set_component(load, 3);
292 break;
293 case nir_intrinsic_load_draw_id:
294 case nir_intrinsic_load_is_indexed_draw:
295 /* gl_DrawID and IsIndexedDraw are stored right after
296 * gl_VertexID and friends if any of them exist.
297 */
298 nir_intrinsic_set_base(load, num_inputs + has_sgvs);
299 if (intrin->intrinsic == nir_intrinsic_load_draw_id)
300 nir_intrinsic_set_component(load, 0);
301 else
302 nir_intrinsic_set_component(load, 1);
303 break;
304 default:
305 unreachable("Invalid system value intrinsic");
306 }
307
308 load->num_components = 1;
309 nir_ssa_dest_init(&load->instr, &load->dest, 1, 32, NULL);
310 nir_builder_instr_insert(&b, &load->instr);
311
312 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
313 nir_src_for_ssa(&load->dest.ssa));
314 nir_instr_remove(&intrin->instr);
315 break;
316 }
317
318 case nir_intrinsic_load_input: {
319 /* Attributes come in a contiguous block, ordered by their
320 * gl_vert_attrib value. That means we can compute the slot
321 * number for an attribute by masking out the enabled attributes
322 * before it and counting the bits.
323 */
324 int attr = nir_intrinsic_base(intrin);
325 int slot = util_bitcount64(nir->info.inputs_read &
326 BITFIELD64_MASK(attr));
327 nir_intrinsic_set_base(intrin, slot);
328 break;
329 }
330
331 default:
332 break; /* Nothing to do */
333 }
334 }
335 }
336 }
337 }
338
339 void
340 brw_nir_lower_vue_inputs(nir_shader *nir,
341 const struct brw_vue_map *vue_map)
342 {
343 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
344 var->data.driver_location = var->data.location;
345 }
346
347 /* Inputs are stored in vec4 slots, so use type_size_vec4(). */
348 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, 0);
349
350 /* This pass needs actual constants */
351 nir_opt_constant_folding(nir);
352
353 add_const_offset_to_base(nir, nir_var_shader_in);
354
355 nir_foreach_function(function, nir) {
356 if (!function->impl)
357 continue;
358
359 nir_foreach_block(block, function->impl) {
360 nir_foreach_instr(instr, block) {
361 if (instr->type != nir_instr_type_intrinsic)
362 continue;
363
364 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
365
366 if (intrin->intrinsic == nir_intrinsic_load_input ||
367 intrin->intrinsic == nir_intrinsic_load_per_vertex_input) {
368 /* Offset 0 is the VUE header, which contains
369 * VARYING_SLOT_LAYER [.y], VARYING_SLOT_VIEWPORT [.z], and
370 * VARYING_SLOT_PSIZ [.w].
371 */
372 int varying = nir_intrinsic_base(intrin);
373 int vue_slot;
374 switch (varying) {
375 case VARYING_SLOT_PSIZ:
376 nir_intrinsic_set_base(intrin, 0);
377 nir_intrinsic_set_component(intrin, 3);
378 break;
379
380 default:
381 vue_slot = vue_map->varying_to_slot[varying];
382 assert(vue_slot != -1);
383 nir_intrinsic_set_base(intrin, vue_slot);
384 break;
385 }
386 }
387 }
388 }
389 }
390 }
391
392 void
393 brw_nir_lower_tes_inputs(nir_shader *nir, const struct brw_vue_map *vue_map)
394 {
395 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
396 var->data.driver_location = var->data.location;
397 }
398
399 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, 0);
400
401 /* This pass needs actual constants */
402 nir_opt_constant_folding(nir);
403
404 add_const_offset_to_base(nir, nir_var_shader_in);
405
406 nir_foreach_function(function, nir) {
407 if (function->impl) {
408 nir_builder b;
409 nir_builder_init(&b, function->impl);
410 nir_foreach_block(block, function->impl) {
411 remap_patch_urb_offsets(block, &b, vue_map,
412 nir->info.tess.primitive_mode);
413 }
414 }
415 }
416 }
417
418 void
419 brw_nir_lower_fs_inputs(nir_shader *nir,
420 const struct gen_device_info *devinfo,
421 const struct brw_wm_prog_key *key)
422 {
423 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
424 var->data.driver_location = var->data.location;
425
426 /* Apply default interpolation mode.
427 *
428 * Everything defaults to smooth except for the legacy GL color
429 * built-in variables, which might be flat depending on API state.
430 */
431 if (var->data.interpolation == INTERP_MODE_NONE) {
432 const bool flat = key->flat_shade &&
433 (var->data.location == VARYING_SLOT_COL0 ||
434 var->data.location == VARYING_SLOT_COL1);
435
436 var->data.interpolation = flat ? INTERP_MODE_FLAT
437 : INTERP_MODE_SMOOTH;
438 }
439
440 /* On Ironlake and below, there is only one interpolation mode.
441 * Centroid interpolation doesn't mean anything on this hardware --
442 * there is no multisampling.
443 */
444 if (devinfo->gen < 6) {
445 var->data.centroid = false;
446 var->data.sample = false;
447 }
448 }
449
450 nir_lower_io_options lower_io_options = 0;
451 if (key->persample_interp)
452 lower_io_options |= nir_lower_io_force_sample_interpolation;
453
454 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, lower_io_options);
455
456 /* This pass needs actual constants */
457 nir_opt_constant_folding(nir);
458
459 add_const_offset_to_base(nir, nir_var_shader_in);
460 }
461
462 void
463 brw_nir_lower_vue_outputs(nir_shader *nir)
464 {
465 nir_foreach_variable(var, &nir->outputs) {
466 var->data.driver_location = var->data.location;
467 }
468
469 nir_lower_io(nir, nir_var_shader_out, type_size_vec4, 0);
470 }
471
472 void
473 brw_nir_lower_tcs_outputs(nir_shader *nir, const struct brw_vue_map *vue_map,
474 GLenum tes_primitive_mode)
475 {
476 nir_foreach_variable(var, &nir->outputs) {
477 var->data.driver_location = var->data.location;
478 }
479
480 nir_lower_io(nir, nir_var_shader_out, type_size_vec4, 0);
481
482 /* This pass needs actual constants */
483 nir_opt_constant_folding(nir);
484
485 add_const_offset_to_base(nir, nir_var_shader_out);
486
487 nir_foreach_function(function, nir) {
488 if (function->impl) {
489 nir_builder b;
490 nir_builder_init(&b, function->impl);
491 nir_foreach_block(block, function->impl) {
492 remap_patch_urb_offsets(block, &b, vue_map, tes_primitive_mode);
493 }
494 }
495 }
496 }
497
498 void
499 brw_nir_lower_fs_outputs(nir_shader *nir)
500 {
501 nir_foreach_variable(var, &nir->outputs) {
502 var->data.driver_location =
503 SET_FIELD(var->data.index, BRW_NIR_FRAG_OUTPUT_INDEX) |
504 SET_FIELD(var->data.location, BRW_NIR_FRAG_OUTPUT_LOCATION);
505 }
506
507 nir_lower_io(nir, nir_var_shader_out, type_size_dvec4, 0);
508 }
509
510 #define OPT(pass, ...) ({ \
511 bool this_progress = false; \
512 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
513 if (this_progress) \
514 progress = true; \
515 this_progress; \
516 })
517
518 static nir_variable_mode
519 brw_nir_no_indirect_mask(const struct brw_compiler *compiler,
520 gl_shader_stage stage)
521 {
522 nir_variable_mode indirect_mask = 0;
523
524 if (compiler->glsl_compiler_options[stage].EmitNoIndirectInput)
525 indirect_mask |= nir_var_shader_in;
526 if (compiler->glsl_compiler_options[stage].EmitNoIndirectOutput)
527 indirect_mask |= nir_var_shader_out;
528 if (compiler->glsl_compiler_options[stage].EmitNoIndirectTemp)
529 indirect_mask |= nir_var_function_temp;
530
531 return indirect_mask;
532 }
533
534 void
535 brw_nir_optimize(nir_shader *nir, const struct brw_compiler *compiler,
536 bool is_scalar, bool allow_copies)
537 {
538 nir_variable_mode indirect_mask =
539 brw_nir_no_indirect_mask(compiler, nir->info.stage);
540
541 bool progress;
542 unsigned lower_flrp =
543 (nir->options->lower_flrp16 ? 16 : 0) |
544 (nir->options->lower_flrp32 ? 32 : 0) |
545 (nir->options->lower_flrp64 ? 64 : 0);
546
547 do {
548 progress = false;
549 OPT(nir_split_array_vars, nir_var_function_temp);
550 OPT(nir_shrink_vec_array_vars, nir_var_function_temp);
551 OPT(nir_opt_deref);
552 OPT(nir_lower_vars_to_ssa);
553 if (allow_copies) {
554 /* Only run this pass in the first call to brw_nir_optimize. Later
555 * calls assume that we've lowered away any copy_deref instructions
556 * and we don't want to introduce any more.
557 */
558 OPT(nir_opt_find_array_copies);
559 }
560 OPT(nir_opt_copy_prop_vars);
561 OPT(nir_opt_dead_write_vars);
562 OPT(nir_opt_combine_stores, nir_var_all);
563
564 if (is_scalar) {
565 OPT(nir_lower_alu_to_scalar, NULL);
566 }
567
568 OPT(nir_copy_prop);
569
570 if (is_scalar) {
571 OPT(nir_lower_phis_to_scalar);
572 }
573
574 OPT(nir_copy_prop);
575 OPT(nir_opt_dce);
576 OPT(nir_opt_cse);
577 OPT(nir_opt_combine_stores, nir_var_all);
578
579 /* Passing 0 to the peephole select pass causes it to convert
580 * if-statements that contain only move instructions in the branches
581 * regardless of the count.
582 *
583 * Passing 1 to the peephole select pass causes it to convert
584 * if-statements that contain at most a single ALU instruction (total)
585 * in both branches. Before Gen6, some math instructions were
586 * prohibitively expensive and the results of compare operations need an
587 * extra resolve step. For these reasons, this pass is more harmful
588 * than good on those platforms.
589 *
590 * For indirect loads of uniforms (push constants), we assume that array
591 * indices will nearly always be in bounds and the cost of the load is
592 * low. Therefore there shouldn't be a performance benefit to avoid it.
593 * However, in vec4 tessellation shaders, these loads operate by
594 * actually pulling from memory.
595 */
596 const bool is_vec4_tessellation = !is_scalar &&
597 (nir->info.stage == MESA_SHADER_TESS_CTRL ||
598 nir->info.stage == MESA_SHADER_TESS_EVAL);
599 OPT(nir_opt_peephole_select, 0, !is_vec4_tessellation, false);
600 OPT(nir_opt_peephole_select, 1, !is_vec4_tessellation,
601 compiler->devinfo->gen >= 6);
602
603 OPT(nir_opt_intrinsics);
604 OPT(nir_opt_idiv_const, 32);
605 OPT(nir_opt_algebraic);
606 OPT(nir_opt_constant_folding);
607
608 if (lower_flrp != 0) {
609 /* To match the old behavior, set always_precise only for scalar
610 * shader stages.
611 */
612 if (OPT(nir_lower_flrp,
613 lower_flrp,
614 false /* always_precise */,
615 compiler->devinfo->gen >= 6)) {
616 OPT(nir_opt_constant_folding);
617 }
618
619 /* Nothing should rematerialize any flrps, so we only need to do this
620 * lowering once.
621 */
622 lower_flrp = 0;
623 }
624
625 OPT(nir_opt_dead_cf);
626 if (OPT(nir_opt_trivial_continues)) {
627 /* If nir_opt_trivial_continues makes progress, then we need to clean
628 * things up if we want any hope of nir_opt_if or nir_opt_loop_unroll
629 * to make progress.
630 */
631 OPT(nir_copy_prop);
632 OPT(nir_opt_dce);
633 }
634 OPT(nir_opt_if, false);
635 if (nir->options->max_unroll_iterations != 0) {
636 OPT(nir_opt_loop_unroll, indirect_mask);
637 }
638 OPT(nir_opt_remove_phis);
639 OPT(nir_opt_undef);
640 OPT(nir_lower_pack);
641 } while (progress);
642
643 /* Workaround Gfxbench unused local sampler variable which will trigger an
644 * assert in the opt_large_constants pass.
645 */
646 OPT(nir_remove_dead_variables, nir_var_function_temp);
647 }
648
649 static unsigned
650 lower_bit_size_callback(const nir_alu_instr *alu, UNUSED void *data)
651 {
652 assert(alu->dest.dest.is_ssa);
653 if (alu->dest.dest.ssa.bit_size >= 32)
654 return 0;
655
656 const struct brw_compiler *compiler = (const struct brw_compiler *) data;
657
658 switch (alu->op) {
659 case nir_op_idiv:
660 case nir_op_imod:
661 case nir_op_irem:
662 case nir_op_udiv:
663 case nir_op_umod:
664 case nir_op_fceil:
665 case nir_op_ffloor:
666 case nir_op_ffract:
667 case nir_op_fround_even:
668 case nir_op_ftrunc:
669 return 32;
670 case nir_op_frcp:
671 case nir_op_frsq:
672 case nir_op_fsqrt:
673 case nir_op_fpow:
674 case nir_op_fexp2:
675 case nir_op_flog2:
676 case nir_op_fsin:
677 case nir_op_fcos:
678 return compiler->devinfo->gen < 9 ? 32 : 0;
679 default:
680 return 0;
681 }
682 }
683
684 /* Does some simple lowering and runs the standard suite of optimizations
685 *
686 * This is intended to be called more-or-less directly after you get the
687 * shader out of GLSL or some other source. While it is geared towards i965,
688 * it is not at all generator-specific except for the is_scalar flag. Even
689 * there, it is safe to call with is_scalar = false for a shader that is
690 * intended for the FS backend as long as nir_optimize is called again with
691 * is_scalar = true to scalarize everything prior to code gen.
692 */
693 void
694 brw_preprocess_nir(const struct brw_compiler *compiler, nir_shader *nir,
695 const nir_shader *softfp64)
696 {
697 const struct gen_device_info *devinfo = compiler->devinfo;
698 UNUSED bool progress; /* Written by OPT */
699
700 const bool is_scalar = compiler->scalar_stage[nir->info.stage];
701
702 if (is_scalar) {
703 OPT(nir_lower_alu_to_scalar, NULL);
704 }
705
706 if (nir->info.stage == MESA_SHADER_GEOMETRY)
707 OPT(nir_lower_gs_intrinsics);
708
709 /* See also brw_nir_trig_workarounds.py */
710 if (compiler->precise_trig &&
711 !(devinfo->gen >= 10 || devinfo->is_kabylake))
712 OPT(brw_nir_apply_trig_workarounds);
713
714 static const nir_lower_tex_options tex_options = {
715 .lower_txp = ~0,
716 .lower_txf_offset = true,
717 .lower_rect_offset = true,
718 .lower_tex_without_implicit_lod = true,
719 .lower_txd_cube_map = true,
720 .lower_txb_shadow_clamp = true,
721 .lower_txd_shadow_clamp = true,
722 .lower_txd_offset_clamp = true,
723 .lower_tg4_offsets = true,
724 };
725
726 OPT(nir_lower_tex, &tex_options);
727 OPT(nir_normalize_cubemap_coords);
728
729 OPT(nir_lower_global_vars_to_local);
730
731 OPT(nir_split_var_copies);
732 OPT(nir_split_struct_vars, nir_var_function_temp);
733
734 brw_nir_optimize(nir, compiler, is_scalar, true);
735
736 bool lowered_64bit_ops = false;
737 do {
738 progress = false;
739
740 OPT(nir_lower_int64, nir->options->lower_int64_options);
741 OPT(nir_lower_doubles, softfp64, nir->options->lower_doubles_options);
742
743 /* Necessary to lower add -> sub and div -> mul/rcp */
744 OPT(nir_opt_algebraic);
745
746 lowered_64bit_ops |= progress;
747 } while (progress);
748
749 /* This needs to be run after the first optimization pass but before we
750 * lower indirect derefs away
751 */
752 if (compiler->supports_shader_constants) {
753 OPT(nir_opt_large_constants, NULL, 32);
754 }
755
756 OPT(nir_lower_bit_size, lower_bit_size_callback, (void *)compiler);
757
758 if (is_scalar) {
759 OPT(nir_lower_load_const_to_scalar);
760 }
761
762 /* Lower a bunch of stuff */
763 OPT(nir_lower_var_copies);
764
765 OPT(nir_lower_system_values);
766
767 const nir_lower_subgroups_options subgroups_options = {
768 .subgroup_size = BRW_SUBGROUP_SIZE,
769 .ballot_bit_size = 32,
770 .lower_to_scalar = true,
771 .lower_subgroup_masks = true,
772 .lower_vote_trivial = !is_scalar,
773 .lower_shuffle = true,
774 };
775 OPT(nir_lower_subgroups, &subgroups_options);
776
777 OPT(nir_lower_clip_cull_distance_arrays);
778
779 nir_variable_mode indirect_mask =
780 brw_nir_no_indirect_mask(compiler, nir->info.stage);
781 OPT(nir_lower_indirect_derefs, indirect_mask);
782
783 /* Lower array derefs of vectors for SSBO and UBO loads. For both UBOs and
784 * SSBOs, our back-end is capable of loading an entire vec4 at a time and
785 * we would like to take advantage of that whenever possible regardless of
786 * whether or not the app gives us full loads. This should allow the
787 * optimizer to combine UBO and SSBO load operations and save us some send
788 * messages.
789 */
790 OPT(nir_lower_array_deref_of_vec,
791 nir_var_mem_ubo | nir_var_mem_ssbo,
792 nir_lower_direct_array_deref_of_vec_load);
793
794 /* Get rid of split copies */
795 brw_nir_optimize(nir, compiler, is_scalar, false);
796 }
797
798 void
799 brw_nir_link_shaders(const struct brw_compiler *compiler,
800 nir_shader *producer, nir_shader *consumer)
801 {
802 nir_lower_io_arrays_to_elements(producer, consumer);
803 nir_validate_shader(producer, "after nir_lower_io_arrays_to_elements");
804 nir_validate_shader(consumer, "after nir_lower_io_arrays_to_elements");
805
806 const bool p_is_scalar = compiler->scalar_stage[producer->info.stage];
807 const bool c_is_scalar = compiler->scalar_stage[consumer->info.stage];
808
809 if (p_is_scalar && c_is_scalar) {
810 NIR_PASS_V(producer, nir_lower_io_to_scalar_early, nir_var_shader_out);
811 NIR_PASS_V(consumer, nir_lower_io_to_scalar_early, nir_var_shader_in);
812 brw_nir_optimize(producer, compiler, p_is_scalar, false);
813 brw_nir_optimize(consumer, compiler, c_is_scalar, false);
814 }
815
816 if (nir_link_opt_varyings(producer, consumer))
817 brw_nir_optimize(consumer, compiler, c_is_scalar, false);
818
819 NIR_PASS_V(producer, nir_remove_dead_variables, nir_var_shader_out);
820 NIR_PASS_V(consumer, nir_remove_dead_variables, nir_var_shader_in);
821
822 if (nir_remove_unused_varyings(producer, consumer)) {
823 NIR_PASS_V(producer, nir_lower_global_vars_to_local);
824 NIR_PASS_V(consumer, nir_lower_global_vars_to_local);
825
826 /* The backend might not be able to handle indirects on
827 * temporaries so we need to lower indirects on any of the
828 * varyings we have demoted here.
829 */
830 NIR_PASS_V(producer, nir_lower_indirect_derefs,
831 brw_nir_no_indirect_mask(compiler, producer->info.stage));
832 NIR_PASS_V(consumer, nir_lower_indirect_derefs,
833 brw_nir_no_indirect_mask(compiler, consumer->info.stage));
834
835 brw_nir_optimize(producer, compiler, p_is_scalar, false);
836 brw_nir_optimize(consumer, compiler, c_is_scalar, false);
837 }
838
839 NIR_PASS_V(producer, nir_lower_io_to_vector, nir_var_shader_out);
840 NIR_PASS_V(producer, nir_opt_combine_stores, nir_var_shader_out);
841 NIR_PASS_V(consumer, nir_lower_io_to_vector, nir_var_shader_in);
842
843 if (producer->info.stage != MESA_SHADER_TESS_CTRL) {
844 /* Calling lower_io_to_vector creates output variable writes with
845 * write-masks. On non-TCS outputs, the back-end can't handle it and we
846 * need to call nir_lower_io_to_temporaries to get rid of them. This,
847 * in turn, creates temporary variables and extra copy_deref intrinsics
848 * that we need to clean up.
849 */
850 NIR_PASS_V(producer, nir_lower_io_to_temporaries,
851 nir_shader_get_entrypoint(producer), true, false);
852 NIR_PASS_V(producer, nir_lower_global_vars_to_local);
853 NIR_PASS_V(producer, nir_split_var_copies);
854 NIR_PASS_V(producer, nir_lower_var_copies);
855 }
856 }
857
858 /* Prepare the given shader for codegen
859 *
860 * This function is intended to be called right before going into the actual
861 * backend and is highly backend-specific. Also, once this function has been
862 * called on a shader, it will no longer be in SSA form so most optimizations
863 * will not work.
864 */
865 void
866 brw_postprocess_nir(nir_shader *nir, const struct brw_compiler *compiler,
867 bool is_scalar)
868 {
869 const struct gen_device_info *devinfo = compiler->devinfo;
870 bool debug_enabled =
871 (INTEL_DEBUG & intel_debug_flag_for_shader_stage(nir->info.stage));
872
873 UNUSED bool progress; /* Written by OPT */
874
875 OPT(brw_nir_lower_mem_access_bit_sizes);
876 OPT(nir_lower_int64, nir->options->lower_int64_options);
877
878 do {
879 progress = false;
880 OPT(nir_opt_algebraic_before_ffma);
881 } while (progress);
882
883 brw_nir_optimize(nir, compiler, is_scalar, false);
884
885 if (devinfo->gen >= 6) {
886 /* Try and fuse multiply-adds */
887 OPT(brw_nir_opt_peephole_ffma);
888 }
889
890 if (OPT(nir_opt_comparison_pre)) {
891 OPT(nir_copy_prop);
892 OPT(nir_opt_dce);
893 OPT(nir_opt_cse);
894
895 /* Do the select peepehole again. nir_opt_comparison_pre (combined with
896 * the other optimization passes) will have removed at least one
897 * instruction from one of the branches of the if-statement, so now it
898 * might be under the threshold of conversion to bcsel.
899 *
900 * See brw_nir_optimize for the explanation of is_vec4_tessellation.
901 */
902 const bool is_vec4_tessellation = !is_scalar &&
903 (nir->info.stage == MESA_SHADER_TESS_CTRL ||
904 nir->info.stage == MESA_SHADER_TESS_EVAL);
905 OPT(nir_opt_peephole_select, 0, is_vec4_tessellation, false);
906 OPT(nir_opt_peephole_select, 1, is_vec4_tessellation,
907 compiler->devinfo->gen >= 6);
908 }
909
910 do {
911 progress = false;
912 if (OPT(nir_opt_algebraic_late)) {
913 /* At this late stage, anything that makes more constants will wreak
914 * havok on the vec4 backend. The handling of constants in the vec4
915 * backend is not good.
916 */
917 if (is_scalar) {
918 OPT(nir_opt_constant_folding);
919 OPT(nir_copy_prop);
920 }
921 OPT(nir_opt_dce);
922 OPT(nir_opt_cse);
923 }
924 } while (progress);
925
926
927 OPT(brw_nir_lower_conversions);
928
929 if (is_scalar)
930 OPT(nir_lower_alu_to_scalar, NULL);
931 OPT(nir_lower_to_source_mods, nir_lower_all_source_mods);
932 OPT(nir_copy_prop);
933 OPT(nir_opt_dce);
934 OPT(nir_opt_move_comparisons);
935
936 OPT(nir_lower_bool_to_int32);
937
938 OPT(nir_lower_locals_to_regs);
939
940 if (unlikely(debug_enabled)) {
941 /* Re-index SSA defs so we print more sensible numbers. */
942 nir_foreach_function(function, nir) {
943 if (function->impl)
944 nir_index_ssa_defs(function->impl);
945 }
946
947 fprintf(stderr, "NIR (SSA form) for %s shader:\n",
948 _mesa_shader_stage_to_string(nir->info.stage));
949 nir_print_shader(nir, stderr);
950 }
951
952 OPT(nir_convert_from_ssa, true);
953
954 if (!is_scalar) {
955 OPT(nir_move_vec_src_uses_to_dest);
956 OPT(nir_lower_vec_to_movs);
957 }
958
959 OPT(nir_opt_dce);
960
961 if (OPT(nir_opt_rematerialize_compares))
962 OPT(nir_opt_dce);
963
964 /* This is the last pass we run before we start emitting stuff. It
965 * determines when we need to insert boolean resolves on Gen <= 5. We
966 * run it last because it stashes data in instr->pass_flags and we don't
967 * want that to be squashed by other NIR passes.
968 */
969 if (devinfo->gen <= 5)
970 brw_nir_analyze_boolean_resolves(nir);
971
972 nir_sweep(nir);
973
974 if (unlikely(debug_enabled)) {
975 fprintf(stderr, "NIR (final form) for %s shader:\n",
976 _mesa_shader_stage_to_string(nir->info.stage));
977 nir_print_shader(nir, stderr);
978 }
979 }
980
981 void
982 brw_nir_apply_sampler_key(nir_shader *nir,
983 const struct brw_compiler *compiler,
984 const struct brw_sampler_prog_key_data *key_tex,
985 bool is_scalar)
986 {
987 const struct gen_device_info *devinfo = compiler->devinfo;
988 nir_lower_tex_options tex_options = {
989 .lower_txd_clamp_bindless_sampler = true,
990 .lower_txd_clamp_if_sampler_index_not_lt_16 = true,
991 };
992
993 /* Iron Lake and prior require lowering of all rectangle textures */
994 if (devinfo->gen < 6)
995 tex_options.lower_rect = true;
996
997 /* Prior to Broadwell, our hardware can't actually do GL_CLAMP */
998 if (devinfo->gen < 8) {
999 tex_options.saturate_s = key_tex->gl_clamp_mask[0];
1000 tex_options.saturate_t = key_tex->gl_clamp_mask[1];
1001 tex_options.saturate_r = key_tex->gl_clamp_mask[2];
1002 }
1003
1004 /* Prior to Haswell, we have to fake texture swizzle */
1005 for (unsigned s = 0; s < MAX_SAMPLERS; s++) {
1006 if (key_tex->swizzles[s] == SWIZZLE_NOOP)
1007 continue;
1008
1009 tex_options.swizzle_result |= (1 << s);
1010 for (unsigned c = 0; c < 4; c++)
1011 tex_options.swizzles[s][c] = GET_SWZ(key_tex->swizzles[s], c);
1012 }
1013
1014 /* Prior to Haswell, we have to lower gradients on shadow samplers */
1015 tex_options.lower_txd_shadow = devinfo->gen < 8 && !devinfo->is_haswell;
1016
1017 tex_options.lower_y_uv_external = key_tex->y_uv_image_mask;
1018 tex_options.lower_y_u_v_external = key_tex->y_u_v_image_mask;
1019 tex_options.lower_yx_xuxv_external = key_tex->yx_xuxv_image_mask;
1020 tex_options.lower_xy_uxvx_external = key_tex->xy_uxvx_image_mask;
1021 tex_options.lower_ayuv_external = key_tex->ayuv_image_mask;
1022 tex_options.lower_xyuv_external = key_tex->xyuv_image_mask;
1023
1024 /* Setup array of scaling factors for each texture. */
1025 memcpy(&tex_options.scale_factors, &key_tex->scale_factors,
1026 sizeof(tex_options.scale_factors));
1027
1028 if (nir_lower_tex(nir, &tex_options)) {
1029 nir_validate_shader(nir, "after nir_lower_tex");
1030 brw_nir_optimize(nir, compiler, is_scalar, false);
1031 }
1032 }
1033
1034 enum brw_reg_type
1035 brw_type_for_nir_type(const struct gen_device_info *devinfo, nir_alu_type type)
1036 {
1037 switch (type) {
1038 case nir_type_uint:
1039 case nir_type_uint32:
1040 return BRW_REGISTER_TYPE_UD;
1041 case nir_type_bool:
1042 case nir_type_int:
1043 case nir_type_bool32:
1044 case nir_type_int32:
1045 return BRW_REGISTER_TYPE_D;
1046 case nir_type_float:
1047 case nir_type_float32:
1048 return BRW_REGISTER_TYPE_F;
1049 case nir_type_float16:
1050 return BRW_REGISTER_TYPE_HF;
1051 case nir_type_float64:
1052 return BRW_REGISTER_TYPE_DF;
1053 case nir_type_int64:
1054 return devinfo->gen < 8 ? BRW_REGISTER_TYPE_DF : BRW_REGISTER_TYPE_Q;
1055 case nir_type_uint64:
1056 return devinfo->gen < 8 ? BRW_REGISTER_TYPE_DF : BRW_REGISTER_TYPE_UQ;
1057 case nir_type_int16:
1058 return BRW_REGISTER_TYPE_W;
1059 case nir_type_uint16:
1060 return BRW_REGISTER_TYPE_UW;
1061 case nir_type_int8:
1062 return BRW_REGISTER_TYPE_B;
1063 case nir_type_uint8:
1064 return BRW_REGISTER_TYPE_UB;
1065 default:
1066 unreachable("unknown type");
1067 }
1068
1069 return BRW_REGISTER_TYPE_F;
1070 }
1071
1072 /* Returns the glsl_base_type corresponding to a nir_alu_type.
1073 * This is used by both brw_vec4_nir and brw_fs_nir.
1074 */
1075 enum glsl_base_type
1076 brw_glsl_base_type_for_nir_type(nir_alu_type type)
1077 {
1078 switch (type) {
1079 case nir_type_float:
1080 case nir_type_float32:
1081 return GLSL_TYPE_FLOAT;
1082
1083 case nir_type_float16:
1084 return GLSL_TYPE_FLOAT16;
1085
1086 case nir_type_float64:
1087 return GLSL_TYPE_DOUBLE;
1088
1089 case nir_type_int:
1090 case nir_type_int32:
1091 return GLSL_TYPE_INT;
1092
1093 case nir_type_uint:
1094 case nir_type_uint32:
1095 return GLSL_TYPE_UINT;
1096
1097 case nir_type_int16:
1098 return GLSL_TYPE_INT16;
1099
1100 case nir_type_uint16:
1101 return GLSL_TYPE_UINT16;
1102
1103 default:
1104 unreachable("bad type");
1105 }
1106 }
1107
1108 nir_shader *
1109 brw_nir_create_passthrough_tcs(void *mem_ctx, const struct brw_compiler *compiler,
1110 const nir_shader_compiler_options *options,
1111 const struct brw_tcs_prog_key *key)
1112 {
1113 nir_builder b;
1114 nir_builder_init_simple_shader(&b, mem_ctx, MESA_SHADER_TESS_CTRL,
1115 options);
1116 nir_shader *nir = b.shader;
1117 nir_variable *var;
1118 nir_intrinsic_instr *load;
1119 nir_intrinsic_instr *store;
1120 nir_ssa_def *zero = nir_imm_int(&b, 0);
1121 nir_ssa_def *invoc_id = nir_load_invocation_id(&b);
1122
1123 nir->info.inputs_read = key->outputs_written &
1124 ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
1125 nir->info.outputs_written = key->outputs_written;
1126 nir->info.tess.tcs_vertices_out = key->input_vertices;
1127 nir->info.name = ralloc_strdup(nir, "passthrough");
1128 nir->num_uniforms = 8 * sizeof(uint32_t);
1129
1130 var = nir_variable_create(nir, nir_var_uniform, glsl_vec4_type(), "hdr_0");
1131 var->data.location = 0;
1132 var = nir_variable_create(nir, nir_var_uniform, glsl_vec4_type(), "hdr_1");
1133 var->data.location = 1;
1134
1135 /* Write the patch URB header. */
1136 for (int i = 0; i <= 1; i++) {
1137 load = nir_intrinsic_instr_create(nir, nir_intrinsic_load_uniform);
1138 load->num_components = 4;
1139 load->src[0] = nir_src_for_ssa(zero);
1140 nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
1141 nir_intrinsic_set_base(load, i * 4 * sizeof(uint32_t));
1142 nir_builder_instr_insert(&b, &load->instr);
1143
1144 store = nir_intrinsic_instr_create(nir, nir_intrinsic_store_output);
1145 store->num_components = 4;
1146 store->src[0] = nir_src_for_ssa(&load->dest.ssa);
1147 store->src[1] = nir_src_for_ssa(zero);
1148 nir_intrinsic_set_base(store, VARYING_SLOT_TESS_LEVEL_INNER - i);
1149 nir_intrinsic_set_write_mask(store, WRITEMASK_XYZW);
1150 nir_builder_instr_insert(&b, &store->instr);
1151 }
1152
1153 /* Copy inputs to outputs. */
1154 uint64_t varyings = nir->info.inputs_read;
1155
1156 while (varyings != 0) {
1157 const int varying = ffsll(varyings) - 1;
1158
1159 load = nir_intrinsic_instr_create(nir,
1160 nir_intrinsic_load_per_vertex_input);
1161 load->num_components = 4;
1162 load->src[0] = nir_src_for_ssa(invoc_id);
1163 load->src[1] = nir_src_for_ssa(zero);
1164 nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
1165 nir_intrinsic_set_base(load, varying);
1166 nir_builder_instr_insert(&b, &load->instr);
1167
1168 store = nir_intrinsic_instr_create(nir,
1169 nir_intrinsic_store_per_vertex_output);
1170 store->num_components = 4;
1171 store->src[0] = nir_src_for_ssa(&load->dest.ssa);
1172 store->src[1] = nir_src_for_ssa(invoc_id);
1173 store->src[2] = nir_src_for_ssa(zero);
1174 nir_intrinsic_set_base(store, varying);
1175 nir_intrinsic_set_write_mask(store, WRITEMASK_XYZW);
1176 nir_builder_instr_insert(&b, &store->instr);
1177
1178 varyings &= ~BITFIELD64_BIT(varying);
1179 }
1180
1181 nir_validate_shader(nir, "in brw_nir_create_passthrough_tcs");
1182
1183 brw_preprocess_nir(compiler, nir, NULL);
1184
1185 return nir;
1186 }