1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
30 #include "freedreno_util.h"
33 #include "ir3_compiler.h"
34 #include "ir3_shader.h"
36 #include "nir/tgsi_to_nir.h"
38 static const nir_shader_compiler_options options
= {
49 .native_integers
= true,
50 .vertex_id_zero_based
= true,
51 .lower_extract_byte
= true,
52 .lower_extract_word
= true,
53 .lower_all_io_to_temps
= true,
57 ir3_tgsi_to_nir(const struct tgsi_token
*tokens
)
59 return tgsi_to_nir(tokens
, &options
);
62 const nir_shader_compiler_options
*
63 ir3_get_compiler_options(struct ir3_compiler
*compiler
)
68 /* for given shader key, are any steps handled in nir? */
70 ir3_key_lowers_nir(const struct ir3_shader_key
*key
)
72 return key
->fsaturate_s
| key
->fsaturate_t
| key
->fsaturate_r
|
73 key
->vsaturate_s
| key
->vsaturate_t
| key
->vsaturate_r
|
74 key
->ucp_enables
| key
->color_two_side
|
75 key
->fclamp_color
| key
->vclamp_color
;
78 #define OPT(nir, pass, ...) ({ \
79 bool this_progress = false; \
80 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
84 #define OPT_V(nir, pass, ...) NIR_PASS_V(nir, pass, ##__VA_ARGS__)
87 ir3_optimize_loop(nir_shader
*s
)
93 OPT_V(s
, nir_lower_vars_to_ssa
);
94 progress
|= OPT(s
, nir_opt_copy_prop_vars
);
95 progress
|= OPT(s
, nir_lower_alu_to_scalar
);
96 progress
|= OPT(s
, nir_lower_phis_to_scalar
);
98 progress
|= OPT(s
, nir_copy_prop
);
99 progress
|= OPT(s
, nir_opt_dce
);
100 progress
|= OPT(s
, nir_opt_cse
);
105 progress
|= OPT(s
, nir_opt_gcm
, true);
107 progress
|= OPT(s
, nir_opt_gcm
, false);
108 progress
|= OPT(s
, nir_opt_peephole_select
, 16);
109 progress
|= OPT(s
, nir_opt_intrinsics
);
110 progress
|= OPT(s
, nir_opt_algebraic
);
111 progress
|= OPT(s
, nir_opt_constant_folding
);
112 progress
|= OPT(s
, nir_opt_dead_cf
);
113 if (OPT(s
, nir_opt_trivial_continues
)) {
115 /* If nir_opt_trivial_continues makes progress, then we need to clean
116 * things up if we want any hope of nir_opt_if or nir_opt_loop_unroll
119 OPT(s
, nir_copy_prop
);
122 progress
|= OPT(s
, nir_opt_if
);
123 progress
|= OPT(s
, nir_opt_remove_phis
);
124 progress
|= OPT(s
, nir_opt_undef
);
130 ir3_optimize_nir(struct ir3_shader
*shader
, nir_shader
*s
,
131 const struct ir3_shader_key
*key
)
133 struct nir_lower_tex_options tex_options
= {
138 switch (shader
->type
) {
139 case SHADER_FRAGMENT
:
140 tex_options
.saturate_s
= key
->fsaturate_s
;
141 tex_options
.saturate_t
= key
->fsaturate_t
;
142 tex_options
.saturate_r
= key
->fsaturate_r
;
145 tex_options
.saturate_s
= key
->vsaturate_s
;
146 tex_options
.saturate_t
= key
->vsaturate_t
;
147 tex_options
.saturate_r
= key
->vsaturate_r
;
155 if (shader
->compiler
->gpu_id
>= 400) {
156 /* a4xx seems to have *no* sam.p */
157 tex_options
.lower_txp
= ~0; /* lower all txp */
159 /* a3xx just needs to avoid sam.p for 3d tex */
160 tex_options
.lower_txp
= (1 << GLSL_SAMPLER_DIM_3D
);
163 if (fd_mesa_debug
& FD_DBG_DISASM
) {
164 debug_printf("----------------------\n");
165 nir_print_shader(s
, stdout
);
166 debug_printf("----------------------\n");
169 OPT_V(s
, nir_opt_global_to_local
);
170 OPT_V(s
, nir_lower_regs_to_ssa
);
173 if (s
->info
.stage
== MESA_SHADER_VERTEX
) {
174 OPT_V(s
, nir_lower_clip_vs
, key
->ucp_enables
);
175 if (key
->vclamp_color
)
176 OPT_V(s
, nir_lower_clamp_color_outputs
);
177 } else if (s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
178 OPT_V(s
, nir_lower_clip_fs
, key
->ucp_enables
);
179 if (key
->fclamp_color
)
180 OPT_V(s
, nir_lower_clamp_color_outputs
);
182 if (key
->color_two_side
) {
183 OPT_V(s
, nir_lower_two_sided_color
);
186 /* only want to do this the first time (when key is null)
187 * and not again on any potential 2nd variant lowering pass:
189 OPT_V(s
, ir3_nir_apply_trig_workarounds
);
192 OPT_V(s
, nir_lower_tex
, &tex_options
);
193 OPT_V(s
, nir_lower_load_const_to_scalar
);
194 if (shader
->compiler
->gpu_id
< 500)
195 OPT_V(s
, ir3_nir_lower_tg4_to_tex
);
197 ir3_optimize_loop(s
);
199 /* do idiv lowering after first opt loop to give a chance for
200 * divide by immed power-of-two to be caught first:
202 if (OPT(s
, nir_lower_idiv
))
203 ir3_optimize_loop(s
);
205 OPT_V(s
, nir_remove_dead_variables
, nir_var_local
);
207 if (fd_mesa_debug
& FD_DBG_DISASM
) {
208 debug_printf("----------------------\n");
209 nir_print_shader(s
, stdout
);
210 debug_printf("----------------------\n");
219 ir3_nir_scan_driver_consts(nir_shader
*shader
,
220 struct ir3_driver_const_layout
*layout
)
222 nir_foreach_function(function
, shader
) {
226 nir_foreach_block(block
, function
->impl
) {
227 nir_foreach_instr(instr
, block
) {
228 if (instr
->type
!= nir_instr_type_intrinsic
)
231 nir_intrinsic_instr
*intr
=
232 nir_instr_as_intrinsic(instr
);
235 switch (intr
->intrinsic
) {
236 case nir_intrinsic_get_buffer_size
:
237 idx
= nir_src_as_const_value(intr
->src
[0])->u32
[0];
238 if (layout
->ssbo_size
.mask
& (1 << idx
))
240 layout
->ssbo_size
.mask
|= (1 << idx
);
241 layout
->ssbo_size
.off
[idx
] =
242 layout
->ssbo_size
.count
;
243 layout
->ssbo_size
.count
+= 1; /* one const per */
245 case nir_intrinsic_image_store
:
246 idx
= intr
->variables
[0]->var
->data
.driver_location
;
247 if (layout
->image_dims
.mask
& (1 << idx
))
249 layout
->image_dims
.mask
|= (1 << idx
);
250 layout
->ssbo_size
.off
[idx
] =
251 layout
->image_dims
.count
;
252 layout
->image_dims
.count
+= 3; /* three const per */