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 "glsl/glsl_parser_extras.h"
27 #include "glsl/nir/glsl_to_nir.h"
28 #include "program/prog_to_nir.h"
31 nir_optimize(nir_shader
*nir
, bool is_scalar
)
36 nir_lower_vars_to_ssa(nir
);
37 nir_validate_shader(nir
);
40 nir_lower_alu_to_scalar(nir
);
41 nir_validate_shader(nir
);
44 progress
|= nir_copy_prop(nir
);
45 nir_validate_shader(nir
);
48 nir_lower_phis_to_scalar(nir
);
49 nir_validate_shader(nir
);
52 progress
|= nir_copy_prop(nir
);
53 nir_validate_shader(nir
);
54 progress
|= nir_opt_dce(nir
);
55 nir_validate_shader(nir
);
56 progress
|= nir_opt_cse(nir
);
57 nir_validate_shader(nir
);
58 progress
|= nir_opt_peephole_select(nir
);
59 nir_validate_shader(nir
);
60 progress
|= nir_opt_algebraic(nir
);
61 nir_validate_shader(nir
);
62 progress
|= nir_opt_constant_folding(nir
);
63 nir_validate_shader(nir
);
64 progress
|= nir_opt_remove_phis(nir
);
65 nir_validate_shader(nir
);
70 brw_create_nir(struct brw_context
*brw
,
71 const struct gl_shader_program
*shader_prog
,
72 const struct gl_program
*prog
,
73 gl_shader_stage stage
,
76 struct gl_context
*ctx
= &brw
->ctx
;
77 const nir_shader_compiler_options
*options
=
78 ctx
->Const
.ShaderCompilerOptions
[stage
].NirOptions
;
79 struct gl_shader
*shader
= shader_prog
? shader_prog
->_LinkedShaders
[stage
] : NULL
;
80 bool debug_enabled
= INTEL_DEBUG
& intel_debug_flag_for_shader_stage(stage
);
83 /* First, lower the GLSL IR or Mesa IR to NIR */
85 nir
= glsl_to_nir(shader
, options
);
87 nir
= prog_to_nir(prog
, options
);
88 nir_convert_to_ssa(nir
); /* turn registers into SSA */
90 nir_validate_shader(nir
);
92 nir_lower_global_vars_to_local(nir
);
93 nir_validate_shader(nir
);
95 nir_lower_tex_projector(nir
);
96 nir_validate_shader(nir
);
98 nir_normalize_cubemap_coords(nir
);
99 nir_validate_shader(nir
);
101 nir_split_var_copies(nir
);
102 nir_validate_shader(nir
);
104 nir_optimize(nir
, is_scalar
);
106 /* Lower a bunch of stuff */
107 nir_lower_var_copies(nir
);
108 nir_validate_shader(nir
);
110 /* Get rid of split copies */
111 nir_optimize(nir
, is_scalar
);
114 nir_assign_var_locations(&nir
->uniforms
,
117 nir_assign_var_locations(&nir
->inputs
, &nir
->num_inputs
, type_size_scalar
);
118 nir_assign_var_locations(&nir
->outputs
, &nir
->num_outputs
, type_size_scalar
);
119 nir_lower_io(nir
, type_size_scalar
);
121 nir_assign_var_locations(&nir
->uniforms
,
125 nir_assign_var_locations(&nir
->inputs
, &nir
->num_inputs
, type_size_vec4
);
127 foreach_list_typed(nir_variable
, var
, node
, &nir
->outputs
)
128 var
->data
.driver_location
= var
->data
.location
;
130 nir_lower_io(nir
, type_size_vec4
);
133 nir_validate_shader(nir
);
135 nir_remove_dead_variables(nir
);
136 nir_validate_shader(nir
);
139 nir_lower_samplers(nir
, shader_prog
);
140 nir_validate_shader(nir
);
143 nir_lower_system_values(nir
);
144 nir_validate_shader(nir
);
146 nir_lower_atomics(nir
);
147 nir_validate_shader(nir
);
149 nir_optimize(nir
, is_scalar
);
152 /* Try and fuse multiply-adds */
153 nir_opt_peephole_ffma(nir
);
154 nir_validate_shader(nir
);
157 nir_opt_algebraic_late(nir
);
158 nir_validate_shader(nir
);
160 nir_lower_locals_to_regs(nir
);
161 nir_validate_shader(nir
);
163 nir_lower_to_source_mods(nir
);
164 nir_validate_shader(nir
);
166 nir_validate_shader(nir
);
168 nir_validate_shader(nir
);
170 if (unlikely(debug_enabled
)) {
171 /* Re-index SSA defs so we print more sensible numbers. */
172 nir_foreach_overload(nir
, overload
) {
174 nir_index_ssa_defs(overload
->impl
);
177 fprintf(stderr
, "NIR (SSA form) for %s shader:\n",
178 _mesa_shader_stage_to_string(stage
));
179 nir_print_shader(nir
, stderr
);
182 nir_convert_from_ssa(nir
, is_scalar
);
183 nir_validate_shader(nir
);
186 nir_lower_vec_to_movs(nir
);
187 nir_validate_shader(nir
);
190 /* This is the last pass we run before we start emitting stuff. It
191 * determines when we need to insert boolean resolves on Gen <= 5. We
192 * run it last because it stashes data in instr->pass_flags and we don't
193 * want that to be squashed by other NIR passes.
196 brw_nir_analyze_boolean_resolves(nir
);
200 if (unlikely(debug_enabled
)) {
201 fprintf(stderr
, "NIR (final form) for %s shader:\n",
202 _mesa_shader_stage_to_string(stage
));
203 nir_print_shader(nir
, stderr
);
210 brw_type_for_nir_type(nir_alu_type type
)
213 case nir_type_unsigned
:
214 return BRW_REGISTER_TYPE_UD
;
217 return BRW_REGISTER_TYPE_D
;
219 return BRW_REGISTER_TYPE_F
;
221 unreachable("unknown type");
224 return BRW_REGISTER_TYPE_F
;
227 /* Returns the glsl_base_type corresponding to a nir_alu_type.
228 * This is used by both brw_vec4_nir and brw_fs_nir.
231 brw_glsl_base_type_for_nir_type(nir_alu_type type
)
235 return GLSL_TYPE_FLOAT
;
238 return GLSL_TYPE_INT
;
240 case nir_type_unsigned
:
241 return GLSL_TYPE_UINT
;
244 unreachable("bad type");