ef7facff3328f64e5bef4c936d989e7779ab9c4d
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_nir.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
5 *
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:
12 *
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
15 * Software.
16 *
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
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29
30 #include "freedreno_util.h"
31
32 #include "ir3_nir.h"
33 #include "ir3_compiler.h"
34 #include "ir3_shader.h"
35
36 #include "nir/tgsi_to_nir.h"
37
38 static const nir_shader_compiler_options options = {
39 .lower_fpow = true,
40 .lower_fsat = true,
41 .lower_scmp = true,
42 .lower_flrp32 = true,
43 .lower_flrp64 = true,
44 .lower_ffract = true,
45 .lower_fmod32 = true,
46 .lower_fmod64 = true,
47 .lower_fdiv = true,
48 .fuse_ffma = true,
49 .native_integers = true,
50 .vertex_id_zero_based = true,
51 .lower_extract_byte = true,
52 .lower_extract_word = true,
53 };
54
55 static const nir_shader_compiler_options options_5xx = {
56 .lower_fpow = true,
57 .lower_fsat = true,
58 .lower_scmp = true,
59 .lower_flrp32 = true,
60 .lower_flrp64 = true,
61 .lower_ffract = true,
62 .lower_fmod32 = true,
63 .lower_fmod64 = true,
64 .lower_fdiv = true,
65 .fuse_ffma = true,
66 .native_integers = true,
67 .vertex_id_zero_based = false,
68 .lower_extract_byte = true,
69 .lower_extract_word = true,
70 };
71
72 struct nir_shader *
73 ir3_tgsi_to_nir(const struct tgsi_token *tokens)
74 {
75 return tgsi_to_nir(tokens, &options);
76 }
77
78 const nir_shader_compiler_options *
79 ir3_get_compiler_options(struct ir3_compiler *compiler)
80 {
81 if (compiler->gpu_id >= 500)
82 return &options_5xx;
83 return &options;
84 }
85
86 /* for given shader key, are any steps handled in nir? */
87 bool
88 ir3_key_lowers_nir(const struct ir3_shader_key *key)
89 {
90 return key->fsaturate_s | key->fsaturate_t | key->fsaturate_r |
91 key->vsaturate_s | key->vsaturate_t | key->vsaturate_r |
92 key->ucp_enables | key->color_two_side |
93 key->fclamp_color | key->vclamp_color;
94 }
95
96 #define OPT(nir, pass, ...) ({ \
97 bool this_progress = false; \
98 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
99 this_progress; \
100 })
101
102 #define OPT_V(nir, pass, ...) NIR_PASS_V(nir, pass, ##__VA_ARGS__)
103
104 static void
105 ir3_optimize_loop(nir_shader *s)
106 {
107 bool progress;
108 do {
109 progress = false;
110
111 OPT_V(s, nir_lower_vars_to_ssa);
112 progress |= OPT(s, nir_opt_copy_prop_vars);
113 progress |= OPT(s, nir_lower_alu_to_scalar);
114 progress |= OPT(s, nir_lower_phis_to_scalar);
115
116 progress |= OPT(s, nir_copy_prop);
117 progress |= OPT(s, nir_opt_dce);
118 progress |= OPT(s, nir_opt_cse);
119 progress |= OPT(s, ir3_nir_lower_if_else);
120 progress |= OPT(s, nir_opt_algebraic);
121 progress |= OPT(s, nir_opt_constant_folding);
122
123 } while (progress);
124 }
125
126 struct nir_shader *
127 ir3_optimize_nir(struct ir3_shader *shader, nir_shader *s,
128 const struct ir3_shader_key *key)
129 {
130 struct nir_lower_tex_options tex_options = {
131 .lower_rect = 0,
132 };
133
134 if (key) {
135 switch (shader->type) {
136 case SHADER_FRAGMENT:
137 tex_options.saturate_s = key->fsaturate_s;
138 tex_options.saturate_t = key->fsaturate_t;
139 tex_options.saturate_r = key->fsaturate_r;
140 break;
141 case SHADER_VERTEX:
142 tex_options.saturate_s = key->vsaturate_s;
143 tex_options.saturate_t = key->vsaturate_t;
144 tex_options.saturate_r = key->vsaturate_r;
145 break;
146 default:
147 /* TODO */
148 break;
149 }
150 }
151
152 if (shader->compiler->gpu_id >= 400) {
153 /* a4xx seems to have *no* sam.p */
154 tex_options.lower_txp = ~0; /* lower all txp */
155 } else {
156 /* a3xx just needs to avoid sam.p for 3d tex */
157 tex_options.lower_txp = (1 << GLSL_SAMPLER_DIM_3D);
158 }
159
160 if (fd_mesa_debug & FD_DBG_DISASM) {
161 debug_printf("----------------------\n");
162 nir_print_shader(s, stdout);
163 debug_printf("----------------------\n");
164 }
165
166 OPT_V(s, nir_opt_global_to_local);
167 OPT_V(s, nir_lower_regs_to_ssa);
168
169 if (key) {
170 if (s->info.stage == MESA_SHADER_VERTEX) {
171 OPT_V(s, nir_lower_clip_vs, key->ucp_enables);
172 if (key->vclamp_color)
173 OPT_V(s, nir_lower_clamp_color_outputs);
174 } else if (s->info.stage == MESA_SHADER_FRAGMENT) {
175 OPT_V(s, nir_lower_clip_fs, key->ucp_enables);
176 if (key->fclamp_color)
177 OPT_V(s, nir_lower_clamp_color_outputs);
178 }
179 if (key->color_two_side) {
180 OPT_V(s, nir_lower_two_sided_color);
181 }
182 } else {
183 /* only want to do this the first time (when key is null)
184 * and not again on any potential 2nd variant lowering pass:
185 */
186 OPT_V(s, ir3_nir_apply_trig_workarounds);
187 }
188
189 OPT_V(s, nir_lower_tex, &tex_options);
190 OPT_V(s, nir_lower_load_const_to_scalar);
191
192 ir3_optimize_loop(s);
193
194 /* do idiv lowering after first opt loop to give a chance for
195 * divide by immed power-of-two to be caught first:
196 */
197 if (OPT(s, nir_lower_idiv))
198 ir3_optimize_loop(s);
199
200 OPT_V(s, nir_remove_dead_variables, nir_var_local);
201
202 if (fd_mesa_debug & FD_DBG_DISASM) {
203 debug_printf("----------------------\n");
204 nir_print_shader(s, stdout);
205 debug_printf("----------------------\n");
206 }
207
208 nir_sweep(s);
209
210 return s;
211 }