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/nir/glsl_to_nir.h"
27 #include "compiler/nir/nir_builder.h"
28 #include "program/prog_to_nir.h"
31 is_input(nir_intrinsic_instr
*intrin
)
33 return intrin
->intrinsic
== nir_intrinsic_load_input
||
34 intrin
->intrinsic
== nir_intrinsic_load_per_vertex_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
, GLbitfield64 inputs_read
)
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(inputs_read
& BITFIELD64_MASK(attr
));
116 intrin
->const_index
[0] = 4 * slot
;
123 remap_inputs_with_vue_map(nir_block
*block
, const struct brw_vue_map
*vue_map
)
125 nir_foreach_instr(instr
, block
) {
126 if (instr
->type
!= nir_instr_type_intrinsic
)
129 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
131 if (intrin
->intrinsic
== nir_intrinsic_load_input
||
132 intrin
->intrinsic
== nir_intrinsic_load_per_vertex_input
) {
133 int vue_slot
= vue_map
->varying_to_slot
[intrin
->const_index
[0]];
134 assert(vue_slot
!= -1);
135 intrin
->const_index
[0] = vue_slot
;
142 remap_patch_urb_offsets(nir_block
*block
, nir_builder
*b
,
143 const struct brw_vue_map
*vue_map
)
145 nir_foreach_instr_safe(instr
, block
) {
146 if (instr
->type
!= nir_instr_type_intrinsic
)
149 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
151 gl_shader_stage stage
= b
->shader
->stage
;
153 if ((stage
== MESA_SHADER_TESS_CTRL
&& is_output(intrin
)) ||
154 (stage
== MESA_SHADER_TESS_EVAL
&& is_input(intrin
))) {
155 int vue_slot
= vue_map
->varying_to_slot
[intrin
->const_index
[0]];
156 assert(vue_slot
!= -1);
157 intrin
->const_index
[0] = vue_slot
;
159 nir_src
*vertex
= nir_get_io_vertex_index_src(intrin
);
161 nir_const_value
*const_vertex
= nir_src_as_const_value(*vertex
);
163 intrin
->const_index
[0] += const_vertex
->u32
[0] *
164 vue_map
->num_per_vertex_slots
;
166 b
->cursor
= nir_before_instr(&intrin
->instr
);
168 /* Multiply by the number of per-vertex slots. */
169 nir_ssa_def
*vertex_offset
=
171 nir_ssa_for_src(b
, *vertex
, 1),
173 vue_map
->num_per_vertex_slots
));
175 /* Add it to the existing offset */
176 nir_src
*offset
= nir_get_io_offset_src(intrin
);
177 nir_ssa_def
*total_offset
=
178 nir_iadd(b
, vertex_offset
,
179 nir_ssa_for_src(b
, *offset
, 1));
181 nir_instr_rewrite_src(&intrin
->instr
, offset
,
182 nir_src_for_ssa(total_offset
));
191 brw_nir_lower_vs_inputs(nir_shader
*nir
,
192 const struct brw_device_info
*devinfo
,
194 bool use_legacy_snorm_formula
,
195 const uint8_t *vs_attrib_wa_flags
)
197 /* Start with the location of the variable's base. */
198 foreach_list_typed(nir_variable
, var
, node
, &nir
->inputs
) {
199 var
->data
.driver_location
= var
->data
.location
;
202 /* Now use nir_lower_io to walk dereference chains. Attribute arrays
203 * are loaded as one vec4 per element (or matrix column), so we use
204 * type_size_vec4 here.
206 nir_lower_io(nir
, nir_var_shader_in
, type_size_vec4
);
208 /* This pass needs actual constants */
209 nir_opt_constant_folding(nir
);
211 add_const_offset_to_base(nir
, nir_var_shader_in
);
213 brw_nir_apply_attribute_workarounds(nir
, use_legacy_snorm_formula
,
217 /* Finally, translate VERT_ATTRIB_* values into the actual registers.
219 * Note that we can use nir->info.inputs_read instead of
220 * key->inputs_read since the two are identical aside from Gen4-5
221 * edge flag differences.
223 GLbitfield64 inputs_read
= nir
->info
.inputs_read
;
225 nir_foreach_function(function
, nir
) {
226 if (function
->impl
) {
227 nir_foreach_block(block
, function
->impl
) {
228 remap_vs_attrs(block
, inputs_read
);
236 brw_nir_lower_vue_inputs(nir_shader
*nir
, bool is_scalar
,
237 const struct brw_vue_map
*vue_map
)
239 foreach_list_typed(nir_variable
, var
, node
, &nir
->inputs
) {
240 var
->data
.driver_location
= var
->data
.location
;
243 /* Inputs are stored in vec4 slots, so use type_size_vec4(). */
244 nir_lower_io(nir
, nir_var_shader_in
, type_size_vec4
);
246 if (is_scalar
|| nir
->stage
!= MESA_SHADER_GEOMETRY
) {
247 /* This pass needs actual constants */
248 nir_opt_constant_folding(nir
);
250 add_const_offset_to_base(nir
, nir_var_shader_in
);
252 nir_foreach_function(function
, nir
) {
253 if (function
->impl
) {
254 nir_foreach_block(block
, function
->impl
) {
255 remap_inputs_with_vue_map(block
, vue_map
);
263 brw_nir_lower_tes_inputs(nir_shader
*nir
, const struct brw_vue_map
*vue_map
)
265 foreach_list_typed(nir_variable
, var
, node
, &nir
->inputs
) {
266 var
->data
.driver_location
= var
->data
.location
;
269 nir_lower_io(nir
, nir_var_shader_in
, type_size_vec4
);
271 /* This pass needs actual constants */
272 nir_opt_constant_folding(nir
);
274 add_const_offset_to_base(nir
, nir_var_shader_in
);
276 nir_foreach_function(function
, nir
) {
277 if (function
->impl
) {
279 nir_builder_init(&b
, function
->impl
);
280 nir_foreach_block(block
, function
->impl
) {
281 remap_patch_urb_offsets(block
, &b
, vue_map
);
288 brw_nir_lower_fs_inputs(nir_shader
*nir
)
290 nir_assign_var_locations(&nir
->inputs
, &nir
->num_inputs
, type_size_scalar
);
291 nir_lower_io(nir
, nir_var_shader_in
, type_size_scalar
);
295 brw_nir_lower_vue_outputs(nir_shader
*nir
,
299 nir_assign_var_locations(&nir
->outputs
, &nir
->num_outputs
,
300 type_size_vec4_times_4
);
301 nir_lower_io(nir
, nir_var_shader_out
, type_size_vec4_times_4
);
303 nir_foreach_variable(var
, &nir
->outputs
)
304 var
->data
.driver_location
= var
->data
.location
;
305 nir_lower_io(nir
, nir_var_shader_out
, type_size_vec4
);
310 brw_nir_lower_tcs_outputs(nir_shader
*nir
, const struct brw_vue_map
*vue_map
)
312 nir_foreach_variable(var
, &nir
->outputs
) {
313 var
->data
.driver_location
= var
->data
.location
;
316 nir_lower_io(nir
, nir_var_shader_out
, type_size_vec4
);
318 /* This pass needs actual constants */
319 nir_opt_constant_folding(nir
);
321 add_const_offset_to_base(nir
, nir_var_shader_out
);
323 nir_foreach_function(function
, nir
) {
324 if (function
->impl
) {
326 nir_builder_init(&b
, function
->impl
);
327 nir_foreach_block(block
, function
->impl
) {
328 remap_patch_urb_offsets(block
, &b
, vue_map
);
335 brw_nir_lower_fs_outputs(nir_shader
*nir
)
337 nir_assign_var_locations(&nir
->outputs
, &nir
->num_outputs
,
339 nir_lower_io(nir
, nir_var_shader_out
, type_size_scalar
);
343 type_size_scalar_bytes(const struct glsl_type
*type
)
345 return type_size_scalar(type
) * 4;
349 type_size_vec4_bytes(const struct glsl_type
*type
)
351 return type_size_vec4(type
) * 16;
355 brw_nir_lower_uniforms(nir_shader
*nir
, bool is_scalar
)
358 nir_assign_var_locations(&nir
->uniforms
, &nir
->num_uniforms
,
359 type_size_scalar_bytes
);
360 nir_lower_io(nir
, nir_var_uniform
, type_size_scalar_bytes
);
362 nir_assign_var_locations(&nir
->uniforms
, &nir
->num_uniforms
,
363 type_size_vec4_bytes
);
364 nir_lower_io(nir
, nir_var_uniform
, type_size_vec4_bytes
);
369 brw_nir_lower_cs_shared(nir_shader
*nir
)
371 nir_assign_var_locations(&nir
->shared
, &nir
->num_shared
,
372 type_size_scalar_bytes
);
373 nir_lower_io(nir
, nir_var_shared
, type_size_scalar_bytes
);
376 #define OPT(pass, ...) ({ \
377 bool this_progress = false; \
378 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
384 #define OPT_V(pass, ...) NIR_PASS_V(nir, pass, ##__VA_ARGS__)
387 nir_optimize(nir_shader
*nir
, bool is_scalar
)
392 OPT_V(nir_lower_vars_to_ssa
);
395 OPT_V(nir_lower_alu_to_scalar
);
401 OPT_V(nir_lower_phis_to_scalar
);
407 OPT(nir_opt_peephole_select
);
408 OPT(nir_opt_algebraic
);
409 OPT(nir_opt_constant_folding
);
410 OPT(nir_opt_dead_cf
);
411 OPT(nir_opt_remove_phis
);
413 OPT_V(nir_lower_doubles
, nir_lower_drcp
|
420 nir_lower_dround_even
|
422 OPT_V(nir_lower_double_pack
);
428 /* Does some simple lowering and runs the standard suite of optimizations
430 * This is intended to be called more-or-less directly after you get the
431 * shader out of GLSL or some other source. While it is geared towards i965,
432 * it is not at all generator-specific except for the is_scalar flag. Even
433 * there, it is safe to call with is_scalar = false for a shader that is
434 * intended for the FS backend as long as nir_optimize is called again with
435 * is_scalar = true to scalarize everything prior to code gen.
438 brw_preprocess_nir(const struct brw_compiler
*compiler
, nir_shader
*nir
)
440 bool progress
; /* Written by OPT and OPT_V */
443 const bool is_scalar
= compiler
->scalar_stage
[nir
->stage
];
445 if (nir
->stage
== MESA_SHADER_GEOMETRY
)
446 OPT(nir_lower_gs_intrinsics
);
448 if (compiler
->precise_trig
)
449 OPT(brw_nir_apply_trig_workarounds
);
451 static const nir_lower_tex_options tex_options
= {
455 OPT(nir_lower_tex
, &tex_options
);
456 OPT(nir_normalize_cubemap_coords
);
458 OPT(nir_lower_global_vars_to_local
);
460 OPT(nir_split_var_copies
);
462 nir
= nir_optimize(nir
, is_scalar
);
465 OPT_V(nir_lower_load_const_to_scalar
);
468 /* Lower a bunch of stuff */
469 OPT_V(nir_lower_var_copies
);
471 /* Get rid of split copies */
472 nir
= nir_optimize(nir
, is_scalar
);
474 OPT(nir_remove_dead_variables
, nir_var_local
);
479 /* Prepare the given shader for codegen
481 * This function is intended to be called right before going into the actual
482 * backend and is highly backend-specific. Also, once this function has been
483 * called on a shader, it will no longer be in SSA form so most optimizations
487 brw_postprocess_nir(nir_shader
*nir
,
488 const struct brw_device_info
*devinfo
,
492 (INTEL_DEBUG
& intel_debug_flag_for_shader_stage(nir
->stage
));
494 bool progress
; /* Written by OPT and OPT_V */
497 nir
= nir_optimize(nir
, is_scalar
);
499 if (devinfo
->gen
>= 6) {
500 /* Try and fuse multiply-adds */
501 OPT(brw_nir_opt_peephole_ffma
);
504 OPT(nir_opt_algebraic_late
);
506 OPT(nir_lower_locals_to_regs
);
508 OPT_V(nir_lower_to_source_mods
);
512 if (unlikely(debug_enabled
)) {
513 /* Re-index SSA defs so we print more sensible numbers. */
514 nir_foreach_function(function
, nir
) {
516 nir_index_ssa_defs(function
->impl
);
519 fprintf(stderr
, "NIR (SSA form) for %s shader:\n",
520 _mesa_shader_stage_to_string(nir
->stage
));
521 nir_print_shader(nir
, stderr
);
524 OPT_V(nir_convert_from_ssa
, true);
527 OPT_V(nir_move_vec_src_uses_to_dest
);
528 OPT(nir_lower_vec_to_movs
);
531 /* This is the last pass we run before we start emitting stuff. It
532 * determines when we need to insert boolean resolves on Gen <= 5. We
533 * run it last because it stashes data in instr->pass_flags and we don't
534 * want that to be squashed by other NIR passes.
536 if (devinfo
->gen
<= 5)
537 brw_nir_analyze_boolean_resolves(nir
);
541 if (unlikely(debug_enabled
)) {
542 fprintf(stderr
, "NIR (final form) for %s shader:\n",
543 _mesa_shader_stage_to_string(nir
->stage
));
544 nir_print_shader(nir
, stderr
);
551 brw_create_nir(struct brw_context
*brw
,
552 const struct gl_shader_program
*shader_prog
,
553 const struct gl_program
*prog
,
554 gl_shader_stage stage
,
557 struct gl_context
*ctx
= &brw
->ctx
;
558 const nir_shader_compiler_options
*options
=
559 ctx
->Const
.ShaderCompilerOptions
[stage
].NirOptions
;
563 /* First, lower the GLSL IR or Mesa IR to NIR */
565 nir
= glsl_to_nir(shader_prog
, stage
, options
);
566 OPT_V(nir_lower_outputs_to_temporaries
, nir_shader_get_entrypoint(nir
));
568 nir
= prog_to_nir(prog
, options
);
569 OPT_V(nir_convert_to_ssa
); /* turn registers into SSA */
571 nir_validate_shader(nir
);
575 nir
= brw_preprocess_nir(brw
->intelScreen
->compiler
, nir
);
577 OPT(nir_lower_system_values
);
578 OPT_V(brw_nir_lower_uniforms
, is_scalar
);
581 OPT_V(nir_lower_samplers
, shader_prog
);
582 OPT_V(nir_lower_atomics
, shader_prog
);
589 brw_nir_apply_sampler_key(nir_shader
*nir
,
590 const struct brw_device_info
*devinfo
,
591 const struct brw_sampler_prog_key_data
*key_tex
,
594 nir_lower_tex_options tex_options
= { 0 };
596 /* Iron Lake and prior require lowering of all rectangle textures */
597 if (devinfo
->gen
< 6)
598 tex_options
.lower_rect
= true;
600 /* Prior to Broadwell, our hardware can't actually do GL_CLAMP */
601 if (devinfo
->gen
< 8) {
602 tex_options
.saturate_s
= key_tex
->gl_clamp_mask
[0];
603 tex_options
.saturate_t
= key_tex
->gl_clamp_mask
[1];
604 tex_options
.saturate_r
= key_tex
->gl_clamp_mask
[2];
607 /* Prior to Haswell, we have to fake texture swizzle */
608 for (unsigned s
= 0; s
< MAX_SAMPLERS
; s
++) {
609 if (key_tex
->swizzles
[s
] == SWIZZLE_NOOP
)
612 tex_options
.swizzle_result
|= (1 << s
);
613 for (unsigned c
= 0; c
< 4; c
++)
614 tex_options
.swizzles
[s
][c
] = GET_SWZ(key_tex
->swizzles
[s
], c
);
617 if (nir_lower_tex(nir
, &tex_options
)) {
618 nir_validate_shader(nir
);
619 nir
= nir_optimize(nir
, is_scalar
);
626 brw_type_for_nir_type(nir_alu_type type
)
630 case nir_type_uint32
:
631 return BRW_REGISTER_TYPE_UD
;
634 case nir_type_bool32
:
636 return BRW_REGISTER_TYPE_D
;
638 case nir_type_float32
:
639 return BRW_REGISTER_TYPE_F
;
640 case nir_type_float64
:
641 return BRW_REGISTER_TYPE_DF
;
643 case nir_type_uint64
:
644 /* TODO we should only see these in moves, so for now it's ok, but when
645 * we add actual 64-bit integer support we should fix this.
647 return BRW_REGISTER_TYPE_DF
;
649 unreachable("unknown type");
652 return BRW_REGISTER_TYPE_F
;
655 /* Returns the glsl_base_type corresponding to a nir_alu_type.
656 * This is used by both brw_vec4_nir and brw_fs_nir.
659 brw_glsl_base_type_for_nir_type(nir_alu_type type
)
663 case nir_type_float32
:
664 return GLSL_TYPE_FLOAT
;
666 case nir_type_float64
:
667 return GLSL_TYPE_DOUBLE
;
671 return GLSL_TYPE_INT
;
674 case nir_type_uint32
:
675 return GLSL_TYPE_UINT
;
678 unreachable("bad type");