nir/algebraic: rename lower_bitshift to lower_bitops
[mesa.git] / src / gallium / drivers / lima / lima_program.c
1 /*
2 * Copyright (c) 2017-2019 Lima Project
3 *
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, sub license,
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:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
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 NON-INFRINGEMENT. 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
21 * DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25 #include "util/u_memory.h"
26 #include "util/ralloc.h"
27 #include "util/u_debug.h"
28
29 #include "tgsi/tgsi_dump.h"
30 #include "compiler/nir/nir.h"
31 #include "nir/tgsi_to_nir.h"
32
33 #include "pipe/p_state.h"
34
35 #include "lima_screen.h"
36 #include "lima_context.h"
37 #include "lima_program.h"
38 #include "lima_bo.h"
39 #include "ir/lima_ir.h"
40
41 static const nir_shader_compiler_options vs_nir_options = {
42 .lower_ffma = true,
43 .lower_fpow = true,
44 .lower_ffract = true,
45 .lower_fdiv = true,
46 .lower_fmod = true,
47 .lower_fsqrt = true,
48 .lower_sub = true,
49 .lower_flrp32 = true,
50 .lower_flrp64 = true,
51 .lower_ftrunc = true,
52 /* could be implemented by clamp */
53 .lower_fsat = true,
54 .lower_bitops = true,
55 .lower_rotate = true,
56 .lower_sincos = true,
57 };
58
59 static const nir_shader_compiler_options fs_nir_options = {
60 .lower_ffma = true,
61 .lower_fpow = true,
62 .lower_fdiv = true,
63 .lower_fmod = true,
64 .lower_sub = true,
65 .lower_flrp32 = true,
66 .lower_flrp64 = true,
67 .lower_fsign = true,
68 .lower_rotate = true,
69 .lower_fdot = true,
70 };
71
72 static const struct nir_lower_tex_options tex_options = {
73 .lower_txp = ~0u,
74 };
75
76 const void *
77 lima_program_get_compiler_options(enum pipe_shader_type shader)
78 {
79 switch (shader) {
80 case PIPE_SHADER_VERTEX:
81 return &vs_nir_options;
82 case PIPE_SHADER_FRAGMENT:
83 return &fs_nir_options;
84 default:
85 return NULL;
86 }
87 }
88
89 static int
90 type_size(const struct glsl_type *type, bool bindless)
91 {
92 return glsl_count_attribute_slots(type, false);
93 }
94
95 void
96 lima_program_optimize_vs_nir(struct nir_shader *s)
97 {
98 bool progress;
99
100 NIR_PASS_V(s, nir_lower_viewport_transform);
101 NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size, 0);
102 NIR_PASS_V(s, nir_lower_load_const_to_scalar);
103 NIR_PASS_V(s, lima_nir_lower_uniform_to_scalar);
104 NIR_PASS_V(s, nir_lower_io_to_scalar,
105 nir_var_shader_in|nir_var_shader_out);
106
107 do {
108 progress = false;
109
110 NIR_PASS_V(s, nir_lower_vars_to_ssa);
111 NIR_PASS(progress, s, nir_lower_alu_to_scalar, NULL);
112 NIR_PASS(progress, s, nir_lower_phis_to_scalar);
113 NIR_PASS(progress, s, nir_copy_prop);
114 NIR_PASS(progress, s, nir_opt_remove_phis);
115 NIR_PASS(progress, s, nir_opt_dce);
116 NIR_PASS(progress, s, nir_opt_dead_cf);
117 NIR_PASS(progress, s, nir_opt_cse);
118 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
119 NIR_PASS(progress, s, nir_opt_algebraic);
120 NIR_PASS(progress, s, nir_opt_constant_folding);
121 NIR_PASS(progress, s, nir_opt_undef);
122 NIR_PASS(progress, s, nir_opt_loop_unroll,
123 nir_var_shader_in |
124 nir_var_shader_out |
125 nir_var_function_temp);
126 } while (progress);
127
128 NIR_PASS_V(s, nir_lower_int_to_float);
129 NIR_PASS_V(s, nir_lower_bool_to_float);
130 NIR_PASS_V(s, nir_copy_prop);
131 NIR_PASS_V(s, nir_opt_dce);
132 NIR_PASS_V(s, nir_lower_locals_to_regs);
133 NIR_PASS_V(s, nir_convert_from_ssa, true);
134 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
135 nir_sweep(s);
136 }
137
138 void
139 lima_program_optimize_fs_nir(struct nir_shader *s)
140 {
141 bool progress;
142
143 NIR_PASS_V(s, nir_lower_fragcoord_wtrans);
144 NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size, 0);
145 NIR_PASS_V(s, nir_lower_regs_to_ssa);
146 NIR_PASS_V(s, nir_lower_tex, &tex_options);
147
148 do {
149 progress = false;
150
151 NIR_PASS_V(s, nir_lower_vars_to_ssa);
152 //NIR_PASS(progress, s, nir_lower_alu_to_scalar, NULL);
153 NIR_PASS(progress, s, nir_lower_phis_to_scalar);
154 NIR_PASS(progress, s, nir_copy_prop);
155 NIR_PASS(progress, s, nir_opt_remove_phis);
156 NIR_PASS(progress, s, nir_opt_dce);
157 NIR_PASS(progress, s, nir_opt_dead_cf);
158 NIR_PASS(progress, s, nir_opt_cse);
159 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
160 NIR_PASS(progress, s, nir_opt_algebraic);
161 NIR_PASS(progress, s, nir_opt_constant_folding);
162 NIR_PASS(progress, s, nir_opt_undef);
163 NIR_PASS(progress, s, nir_opt_loop_unroll,
164 nir_var_shader_in |
165 nir_var_shader_out |
166 nir_var_function_temp);
167 } while (progress);
168
169 NIR_PASS_V(s, nir_lower_int_to_float);
170 NIR_PASS_V(s, nir_lower_bool_to_float);
171
172 /* Lower modifiers */
173 NIR_PASS_V(s, nir_lower_to_source_mods, nir_lower_all_source_mods);
174 NIR_PASS_V(s, nir_copy_prop);
175 NIR_PASS_V(s, nir_opt_dce);
176
177 NIR_PASS_V(s, nir_lower_locals_to_regs);
178 NIR_PASS_V(s, nir_convert_from_ssa, true);
179 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
180
181 NIR_PASS_V(s, nir_move_vec_src_uses_to_dest);
182 NIR_PASS_V(s, nir_lower_vec_to_movs);
183
184 nir_sweep(s);
185 }
186
187 static void *
188 lima_create_fs_state(struct pipe_context *pctx,
189 const struct pipe_shader_state *cso)
190 {
191 struct lima_screen *screen = lima_screen(pctx->screen);
192 struct lima_fs_shader_state *so = rzalloc(NULL, struct lima_fs_shader_state);
193
194 if (!so)
195 return NULL;
196
197 nir_shader *nir;
198 if (cso->type == PIPE_SHADER_IR_NIR)
199 nir = cso->ir.nir;
200 else {
201 assert(cso->type == PIPE_SHADER_IR_TGSI);
202
203 nir = tgsi_to_nir(cso->tokens, pctx->screen);
204 }
205
206 lima_program_optimize_fs_nir(nir);
207
208 if (lima_debug & LIMA_DEBUG_PP)
209 nir_print_shader(nir, stdout);
210
211 if (!ppir_compile_nir(so, nir, screen->pp_ra)) {
212 ralloc_free(so);
213 return NULL;
214 }
215
216 return so;
217 }
218
219 static void
220 lima_bind_fs_state(struct pipe_context *pctx, void *hwcso)
221 {
222 struct lima_context *ctx = lima_context(pctx);
223
224 ctx->fs = hwcso;
225 ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_FRAG;
226 }
227
228 static void
229 lima_delete_fs_state(struct pipe_context *pctx, void *hwcso)
230 {
231 struct lima_fs_shader_state *so = hwcso;
232
233 if (so->bo)
234 lima_bo_free(so->bo);
235
236 ralloc_free(so);
237 }
238
239 bool
240 lima_update_vs_state(struct lima_context *ctx)
241 {
242 struct lima_vs_shader_state *vs = ctx->vs;
243 if (!vs->bo) {
244 struct lima_screen *screen = lima_screen(ctx->base.screen);
245 vs->bo = lima_bo_create(screen, vs->shader_size, 0);
246 if (!vs->bo) {
247 fprintf(stderr, "lima: create vs shader bo fail\n");
248 return false;
249 }
250
251 memcpy(lima_bo_map(vs->bo), vs->shader, vs->shader_size);
252 ralloc_free(vs->shader);
253 vs->shader = NULL;
254 }
255
256 return true;
257 }
258
259 bool
260 lima_update_fs_state(struct lima_context *ctx)
261 {
262 struct lima_fs_shader_state *fs = ctx->fs;
263 if (!fs->bo) {
264 struct lima_screen *screen = lima_screen(ctx->base.screen);
265 fs->bo = lima_bo_create(screen, fs->shader_size, 0);
266 if (!fs->bo) {
267 fprintf(stderr, "lima: create fs shader bo fail\n");
268 return false;
269 }
270
271 memcpy(lima_bo_map(fs->bo), fs->shader, fs->shader_size);
272 ralloc_free(fs->shader);
273 fs->shader = NULL;
274 }
275
276 return true;
277 }
278
279 static void *
280 lima_create_vs_state(struct pipe_context *pctx,
281 const struct pipe_shader_state *cso)
282 {
283 struct lima_vs_shader_state *so = rzalloc(NULL, struct lima_vs_shader_state);
284
285 if (!so)
286 return NULL;
287
288 nir_shader *nir;
289 if (cso->type == PIPE_SHADER_IR_NIR)
290 nir = cso->ir.nir;
291 else {
292 assert(cso->type == PIPE_SHADER_IR_TGSI);
293
294 nir = tgsi_to_nir(cso->tokens, pctx->screen);
295 }
296
297 lima_program_optimize_vs_nir(nir);
298
299 if (lima_debug & LIMA_DEBUG_GP)
300 nir_print_shader(nir, stdout);
301
302 if (!gpir_compile_nir(so, nir)) {
303 ralloc_free(so);
304 return NULL;
305 }
306
307 return so;
308 }
309
310 static void
311 lima_bind_vs_state(struct pipe_context *pctx, void *hwcso)
312 {
313 struct lima_context *ctx = lima_context(pctx);
314
315 ctx->vs = hwcso;
316 ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_VERT;
317 }
318
319 static void
320 lima_delete_vs_state(struct pipe_context *pctx, void *hwcso)
321 {
322 struct lima_vs_shader_state *so = hwcso;
323
324 if (so->bo)
325 lima_bo_free(so->bo);
326
327 ralloc_free(so);
328 }
329
330 void
331 lima_program_init(struct lima_context *ctx)
332 {
333 ctx->base.create_fs_state = lima_create_fs_state;
334 ctx->base.bind_fs_state = lima_bind_fs_state;
335 ctx->base.delete_fs_state = lima_delete_fs_state;
336
337 ctx->base.create_vs_state = lima_create_vs_state;
338 ctx->base.bind_vs_state = lima_bind_vs_state;
339 ctx->base.delete_vs_state = lima_delete_vs_state;
340 }