2 * Copyright © 2014 Intel Corporation
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:
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
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
25 #include "brw_shader.h"
26 #include "compiler/glsl_types.h"
27 #include "compiler/nir/nir_builder.h"
30 is_input(nir_intrinsic_instr
*intrin
)
32 return intrin
->intrinsic
== nir_intrinsic_load_input
||
33 intrin
->intrinsic
== nir_intrinsic_load_per_vertex_input
||
34 intrin
->intrinsic
== nir_intrinsic_load_interpolated_input
;
38 is_output(nir_intrinsic_instr
*intrin
)
40 return intrin
->intrinsic
== nir_intrinsic_load_output
||
41 intrin
->intrinsic
== nir_intrinsic_load_per_vertex_output
||
42 intrin
->intrinsic
== nir_intrinsic_store_output
||
43 intrin
->intrinsic
== nir_intrinsic_store_per_vertex_output
;
47 * In many cases, we just add the base and offset together, so there's no
48 * reason to keep them separate. Sometimes, combining them is essential:
49 * if a shader only accesses part of a compound variable (such as a matrix
50 * or array), the variable's base may not actually exist in the VUE map.
52 * This pass adds constant offsets to instr->const_index[0], and resets
53 * the offset source to 0. Non-constant offsets remain unchanged - since
54 * we don't know what part of a compound variable is accessed, we allocate
55 * storage for the entire thing.
59 add_const_offset_to_base_block(nir_block
*block
, nir_builder
*b
,
60 nir_variable_mode mode
)
62 nir_foreach_instr_safe(instr
, block
) {
63 if (instr
->type
!= nir_instr_type_intrinsic
)
66 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
68 if ((mode
== nir_var_shader_in
&& is_input(intrin
)) ||
69 (mode
== nir_var_shader_out
&& is_output(intrin
))) {
70 nir_src
*offset
= nir_get_io_offset_src(intrin
);
71 nir_const_value
*const_offset
= nir_src_as_const_value(*offset
);
74 intrin
->const_index
[0] += const_offset
->u32
[0];
75 b
->cursor
= nir_before_instr(&intrin
->instr
);
76 nir_instr_rewrite_src(&intrin
->instr
, offset
,
77 nir_src_for_ssa(nir_imm_int(b
, 0)));
85 add_const_offset_to_base(nir_shader
*nir
, nir_variable_mode mode
)
87 nir_foreach_function(f
, nir
) {
90 nir_builder_init(&b
, f
->impl
);
91 nir_foreach_block(block
, f
->impl
) {
92 add_const_offset_to_base_block(block
, &b
, mode
);
99 remap_vs_attrs(nir_block
*block
, shader_info
*nir_info
)
101 nir_foreach_instr(instr
, block
) {
102 if (instr
->type
!= nir_instr_type_intrinsic
)
105 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
107 if (intrin
->intrinsic
== nir_intrinsic_load_input
) {
108 /* Attributes come in a contiguous block, ordered by their
109 * gl_vert_attrib value. That means we can compute the slot
110 * number for an attribute by masking out the enabled attributes
111 * before it and counting the bits.
113 int attr
= intrin
->const_index
[0];
114 int slot
= _mesa_bitcount_64(nir_info
->inputs_read
&
115 BITFIELD64_MASK(attr
));
116 int dslot
= _mesa_bitcount_64(nir_info
->double_inputs_read
&
117 BITFIELD64_MASK(attr
));
118 intrin
->const_index
[0] = 4 * (slot
+ dslot
);
125 remap_inputs_with_vue_map(nir_block
*block
, const struct brw_vue_map
*vue_map
)
127 nir_foreach_instr(instr
, block
) {
128 if (instr
->type
!= nir_instr_type_intrinsic
)
131 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
133 if (intrin
->intrinsic
== nir_intrinsic_load_input
||
134 intrin
->intrinsic
== nir_intrinsic_load_per_vertex_input
) {
135 int vue_slot
= vue_map
->varying_to_slot
[intrin
->const_index
[0]];
136 assert(vue_slot
!= -1);
137 intrin
->const_index
[0] = vue_slot
;
144 remap_patch_urb_offsets(nir_block
*block
, nir_builder
*b
,
145 const struct brw_vue_map
*vue_map
)
147 nir_foreach_instr_safe(instr
, block
) {
148 if (instr
->type
!= nir_instr_type_intrinsic
)
151 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
153 gl_shader_stage stage
= b
->shader
->stage
;
155 if ((stage
== MESA_SHADER_TESS_CTRL
&& is_output(intrin
)) ||
156 (stage
== MESA_SHADER_TESS_EVAL
&& is_input(intrin
))) {
157 int vue_slot
= vue_map
->varying_to_slot
[intrin
->const_index
[0]];
158 assert(vue_slot
!= -1);
159 intrin
->const_index
[0] = vue_slot
;
161 nir_src
*vertex
= nir_get_io_vertex_index_src(intrin
);
163 nir_const_value
*const_vertex
= nir_src_as_const_value(*vertex
);
165 intrin
->const_index
[0] += const_vertex
->u32
[0] *
166 vue_map
->num_per_vertex_slots
;
168 b
->cursor
= nir_before_instr(&intrin
->instr
);
170 /* Multiply by the number of per-vertex slots. */
171 nir_ssa_def
*vertex_offset
=
173 nir_ssa_for_src(b
, *vertex
, 1),
175 vue_map
->num_per_vertex_slots
));
177 /* Add it to the existing offset */
178 nir_src
*offset
= nir_get_io_offset_src(intrin
);
179 nir_ssa_def
*total_offset
=
180 nir_iadd(b
, vertex_offset
,
181 nir_ssa_for_src(b
, *offset
, 1));
183 nir_instr_rewrite_src(&intrin
->instr
, offset
,
184 nir_src_for_ssa(total_offset
));
193 brw_nir_lower_vs_inputs(nir_shader
*nir
,
195 bool use_legacy_snorm_formula
,
196 const uint8_t *vs_attrib_wa_flags
)
198 /* Start with the location of the variable's base. */
199 foreach_list_typed(nir_variable
, var
, node
, &nir
->inputs
) {
200 var
->data
.driver_location
= var
->data
.location
;
203 /* Now use nir_lower_io to walk dereference chains. Attribute arrays are
204 * loaded as one vec4 or dvec4 per element (or matrix column), depending on
205 * whether it is a double-precision type or not.
207 nir_lower_io(nir
, nir_var_shader_in
, type_size_vs_input
, 0);
209 /* This pass needs actual constants */
210 nir_opt_constant_folding(nir
);
212 add_const_offset_to_base(nir
, nir_var_shader_in
);
214 brw_nir_apply_attribute_workarounds(nir
, use_legacy_snorm_formula
,
218 /* Finally, translate VERT_ATTRIB_* values into the actual registers. */
220 nir_foreach_function(function
, nir
) {
221 if (function
->impl
) {
222 nir_foreach_block(block
, function
->impl
) {
223 remap_vs_attrs(block
, &nir
->info
);
231 brw_nir_lower_vue_inputs(nir_shader
*nir
, bool is_scalar
,
232 const struct brw_vue_map
*vue_map
)
234 foreach_list_typed(nir_variable
, var
, node
, &nir
->inputs
) {
235 var
->data
.driver_location
= var
->data
.location
;
238 /* Inputs are stored in vec4 slots, so use type_size_vec4(). */
239 nir_lower_io(nir
, nir_var_shader_in
, type_size_vec4
, 0);
241 if (is_scalar
|| nir
->stage
!= MESA_SHADER_GEOMETRY
) {
242 /* This pass needs actual constants */
243 nir_opt_constant_folding(nir
);
245 add_const_offset_to_base(nir
, nir_var_shader_in
);
247 nir_foreach_function(function
, nir
) {
248 if (function
->impl
) {
249 nir_foreach_block(block
, function
->impl
) {
250 remap_inputs_with_vue_map(block
, vue_map
);
258 brw_nir_lower_tes_inputs(nir_shader
*nir
, const struct brw_vue_map
*vue_map
)
260 foreach_list_typed(nir_variable
, var
, node
, &nir
->inputs
) {
261 var
->data
.driver_location
= var
->data
.location
;
264 nir_lower_io(nir
, nir_var_shader_in
, type_size_vec4
, 0);
266 /* This pass needs actual constants */
267 nir_opt_constant_folding(nir
);
269 add_const_offset_to_base(nir
, nir_var_shader_in
);
271 nir_foreach_function(function
, nir
) {
272 if (function
->impl
) {
274 nir_builder_init(&b
, function
->impl
);
275 nir_foreach_block(block
, function
->impl
) {
276 remap_patch_urb_offsets(block
, &b
, vue_map
);
283 brw_nir_lower_fs_inputs(nir_shader
*nir
,
284 const struct gen_device_info
*devinfo
,
285 const struct brw_wm_prog_key
*key
)
287 foreach_list_typed(nir_variable
, var
, node
, &nir
->inputs
) {
288 var
->data
.driver_location
= var
->data
.location
;
290 /* Apply default interpolation mode.
292 * Everything defaults to smooth except for the legacy GL color
293 * built-in variables, which might be flat depending on API state.
295 if (var
->data
.interpolation
== INTERP_MODE_NONE
) {
296 const bool flat
= key
->flat_shade
&&
297 (var
->data
.location
== VARYING_SLOT_COL0
||
298 var
->data
.location
== VARYING_SLOT_COL1
);
300 var
->data
.interpolation
= flat
? INTERP_MODE_FLAT
301 : INTERP_MODE_SMOOTH
;
304 /* On Ironlake and below, there is only one interpolation mode.
305 * Centroid interpolation doesn't mean anything on this hardware --
306 * there is no multisampling.
308 if (devinfo
->gen
< 6) {
309 var
->data
.centroid
= false;
310 var
->data
.sample
= false;
314 nir_lower_io_options lower_io_options
= 0;
315 if (key
->persample_interp
)
316 lower_io_options
|= nir_lower_io_force_sample_interpolation
;
318 nir_lower_io(nir
, nir_var_shader_in
, type_size_vec4
, lower_io_options
);
320 /* This pass needs actual constants */
321 nir_opt_constant_folding(nir
);
323 add_const_offset_to_base(nir
, nir_var_shader_in
);
327 brw_nir_lower_vue_outputs(nir_shader
*nir
,
330 nir_foreach_variable(var
, &nir
->outputs
) {
331 var
->data
.driver_location
= var
->data
.location
;
334 nir_lower_io(nir
, nir_var_shader_out
, type_size_vec4
, 0);
338 brw_nir_lower_tcs_outputs(nir_shader
*nir
, const struct brw_vue_map
*vue_map
)
340 nir_foreach_variable(var
, &nir
->outputs
) {
341 var
->data
.driver_location
= var
->data
.location
;
344 nir_lower_io(nir
, nir_var_shader_out
, type_size_vec4
, 0);
346 /* This pass needs actual constants */
347 nir_opt_constant_folding(nir
);
349 add_const_offset_to_base(nir
, nir_var_shader_out
);
351 nir_foreach_function(function
, nir
) {
352 if (function
->impl
) {
354 nir_builder_init(&b
, function
->impl
);
355 nir_foreach_block(block
, function
->impl
) {
356 remap_patch_urb_offsets(block
, &b
, vue_map
);
363 brw_nir_lower_fs_outputs(nir_shader
*nir
)
365 nir_foreach_variable(var
, &nir
->outputs
) {
366 var
->data
.driver_location
=
367 SET_FIELD(var
->data
.index
, BRW_NIR_FRAG_OUTPUT_INDEX
) |
368 SET_FIELD(var
->data
.location
, BRW_NIR_FRAG_OUTPUT_LOCATION
);
371 nir_lower_io(nir
, nir_var_shader_out
, type_size_dvec4
, 0);
375 brw_nir_lower_cs_shared(nir_shader
*nir
)
377 nir_assign_var_locations(&nir
->shared
, &nir
->num_shared
, 0,
378 type_size_scalar_bytes
);
379 nir_lower_io(nir
, nir_var_shared
, type_size_scalar_bytes
, 0);
382 #define OPT(pass, ...) ({ \
383 bool this_progress = false; \
384 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
390 #define OPT_V(pass, ...) NIR_PASS_V(nir, pass, ##__VA_ARGS__)
393 nir_optimize(nir_shader
*nir
, bool is_scalar
)
398 OPT_V(nir_lower_vars_to_ssa
);
401 OPT(nir_lower_alu_to_scalar
);
407 OPT(nir_lower_phis_to_scalar
);
413 OPT(nir_opt_peephole_select
, 0);
414 OPT(nir_opt_algebraic
);
415 OPT(nir_opt_constant_folding
);
416 OPT(nir_opt_dead_cf
);
417 OPT(nir_opt_remove_phis
);
419 OPT_V(nir_lower_doubles
, nir_lower_drcp
|
426 nir_lower_dround_even
|
428 OPT_V(nir_lower_double_pack
);
434 /* Does some simple lowering and runs the standard suite of optimizations
436 * This is intended to be called more-or-less directly after you get the
437 * shader out of GLSL or some other source. While it is geared towards i965,
438 * it is not at all generator-specific except for the is_scalar flag. Even
439 * there, it is safe to call with is_scalar = false for a shader that is
440 * intended for the FS backend as long as nir_optimize is called again with
441 * is_scalar = true to scalarize everything prior to code gen.
444 brw_preprocess_nir(const struct brw_compiler
*compiler
, nir_shader
*nir
)
446 bool progress
; /* Written by OPT and OPT_V */
449 const bool is_scalar
= compiler
->scalar_stage
[nir
->stage
];
451 if (nir
->stage
== MESA_SHADER_GEOMETRY
)
452 OPT(nir_lower_gs_intrinsics
);
454 if (compiler
->precise_trig
)
455 OPT(brw_nir_apply_trig_workarounds
);
457 static const nir_lower_tex_options tex_options
= {
459 .lower_txf_offset
= true,
460 .lower_rect_offset
= true,
463 OPT(nir_lower_tex
, &tex_options
);
464 OPT(nir_normalize_cubemap_coords
);
466 OPT(nir_lower_global_vars_to_local
);
468 OPT(nir_split_var_copies
);
470 nir
= nir_optimize(nir
, is_scalar
);
473 OPT_V(nir_lower_load_const_to_scalar
);
476 /* Lower a bunch of stuff */
477 OPT_V(nir_lower_var_copies
);
479 /* Get rid of split copies */
480 nir
= nir_optimize(nir
, is_scalar
);
482 OPT(nir_remove_dead_variables
, nir_var_local
);
487 /* Prepare the given shader for codegen
489 * This function is intended to be called right before going into the actual
490 * backend and is highly backend-specific. Also, once this function has been
491 * called on a shader, it will no longer be in SSA form so most optimizations
495 brw_postprocess_nir(nir_shader
*nir
,
496 const struct gen_device_info
*devinfo
,
500 (INTEL_DEBUG
& intel_debug_flag_for_shader_stage(nir
->stage
));
502 bool progress
; /* Written by OPT and OPT_V */
505 nir
= nir_optimize(nir
, is_scalar
);
507 if (devinfo
->gen
>= 6) {
508 /* Try and fuse multiply-adds */
509 OPT(brw_nir_opt_peephole_ffma
);
512 OPT(nir_opt_algebraic_late
);
514 OPT(nir_lower_locals_to_regs
);
516 OPT_V(nir_lower_to_source_mods
);
520 if (unlikely(debug_enabled
)) {
521 /* Re-index SSA defs so we print more sensible numbers. */
522 nir_foreach_function(function
, nir
) {
524 nir_index_ssa_defs(function
->impl
);
527 fprintf(stderr
, "NIR (SSA form) for %s shader:\n",
528 _mesa_shader_stage_to_string(nir
->stage
));
529 nir_print_shader(nir
, stderr
);
532 OPT_V(nir_convert_from_ssa
, true);
535 OPT_V(nir_move_vec_src_uses_to_dest
);
536 OPT(nir_lower_vec_to_movs
);
539 /* This is the last pass we run before we start emitting stuff. It
540 * determines when we need to insert boolean resolves on Gen <= 5. We
541 * run it last because it stashes data in instr->pass_flags and we don't
542 * want that to be squashed by other NIR passes.
544 if (devinfo
->gen
<= 5)
545 brw_nir_analyze_boolean_resolves(nir
);
549 if (unlikely(debug_enabled
)) {
550 fprintf(stderr
, "NIR (final form) for %s shader:\n",
551 _mesa_shader_stage_to_string(nir
->stage
));
552 nir_print_shader(nir
, stderr
);
559 brw_nir_apply_sampler_key(nir_shader
*nir
,
560 const struct gen_device_info
*devinfo
,
561 const struct brw_sampler_prog_key_data
*key_tex
,
564 nir_lower_tex_options tex_options
= { 0 };
566 /* Iron Lake and prior require lowering of all rectangle textures */
567 if (devinfo
->gen
< 6)
568 tex_options
.lower_rect
= true;
570 /* Prior to Broadwell, our hardware can't actually do GL_CLAMP */
571 if (devinfo
->gen
< 8) {
572 tex_options
.saturate_s
= key_tex
->gl_clamp_mask
[0];
573 tex_options
.saturate_t
= key_tex
->gl_clamp_mask
[1];
574 tex_options
.saturate_r
= key_tex
->gl_clamp_mask
[2];
577 /* Prior to Haswell, we have to fake texture swizzle */
578 for (unsigned s
= 0; s
< MAX_SAMPLERS
; s
++) {
579 if (key_tex
->swizzles
[s
] == SWIZZLE_NOOP
)
582 tex_options
.swizzle_result
|= (1 << s
);
583 for (unsigned c
= 0; c
< 4; c
++)
584 tex_options
.swizzles
[s
][c
] = GET_SWZ(key_tex
->swizzles
[s
], c
);
587 tex_options
.lower_y_uv_external
= key_tex
->y_uv_image_mask
;
588 tex_options
.lower_y_u_v_external
= key_tex
->y_u_v_image_mask
;
589 tex_options
.lower_yx_xuxv_external
= key_tex
->yx_xuxv_image_mask
;
591 if (nir_lower_tex(nir
, &tex_options
)) {
592 nir_validate_shader(nir
);
593 nir
= nir_optimize(nir
, is_scalar
);
600 brw_type_for_nir_type(nir_alu_type type
)
604 case nir_type_uint32
:
605 return BRW_REGISTER_TYPE_UD
;
608 case nir_type_bool32
:
610 return BRW_REGISTER_TYPE_D
;
612 case nir_type_float32
:
613 return BRW_REGISTER_TYPE_F
;
614 case nir_type_float64
:
615 return BRW_REGISTER_TYPE_DF
;
617 case nir_type_uint64
:
618 /* TODO we should only see these in moves, so for now it's ok, but when
619 * we add actual 64-bit integer support we should fix this.
621 return BRW_REGISTER_TYPE_DF
;
623 unreachable("unknown type");
626 return BRW_REGISTER_TYPE_F
;
629 /* Returns the glsl_base_type corresponding to a nir_alu_type.
630 * This is used by both brw_vec4_nir and brw_fs_nir.
633 brw_glsl_base_type_for_nir_type(nir_alu_type type
)
637 case nir_type_float32
:
638 return GLSL_TYPE_FLOAT
;
640 case nir_type_float64
:
641 return GLSL_TYPE_DOUBLE
;
645 return GLSL_TYPE_INT
;
648 case nir_type_uint32
:
649 return GLSL_TYPE_UINT
;
652 unreachable("bad type");