lima/ppir: lower ffma in ppir
[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_bitshift = true,
55 };
56
57 static const nir_shader_compiler_options fs_nir_options = {
58 .lower_ffma = true,
59 .lower_fpow = true,
60 .lower_fdiv = true,
61 .lower_fmod = true,
62 .lower_sub = true,
63 .lower_flrp32 = true,
64 .lower_flrp64 = true,
65 .lower_fsign = true,
66 };
67
68 const void *
69 lima_program_get_compiler_options(enum pipe_shader_type shader)
70 {
71 switch (shader) {
72 case PIPE_SHADER_VERTEX:
73 return &vs_nir_options;
74 case PIPE_SHADER_FRAGMENT:
75 return &fs_nir_options;
76 default:
77 return NULL;
78 }
79 }
80
81 static int
82 type_size(const struct glsl_type *type, bool bindless)
83 {
84 return glsl_count_attribute_slots(type, false);
85 }
86
87 static void
88 lima_program_optimize_vs_nir(struct nir_shader *s)
89 {
90 bool progress;
91
92 NIR_PASS_V(s, nir_lower_viewport_transform);
93 NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size, 0);
94 NIR_PASS_V(s, nir_lower_regs_to_ssa);
95 NIR_PASS_V(s, nir_lower_load_const_to_scalar);
96 NIR_PASS_V(s, lima_nir_lower_uniform_to_scalar);
97 NIR_PASS_V(s, nir_lower_io_to_scalar,
98 nir_var_shader_in|nir_var_shader_out);
99 NIR_PASS_V(s, nir_lower_sincos);
100
101 do {
102 progress = false;
103
104 NIR_PASS_V(s, nir_lower_vars_to_ssa);
105 NIR_PASS(progress, s, nir_lower_alu_to_scalar, NULL);
106 NIR_PASS(progress, s, nir_lower_phis_to_scalar);
107 NIR_PASS(progress, s, nir_copy_prop);
108 NIR_PASS(progress, s, nir_opt_remove_phis);
109 NIR_PASS(progress, s, nir_opt_dce);
110 NIR_PASS(progress, s, nir_opt_dead_cf);
111 NIR_PASS(progress, s, nir_opt_cse);
112 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
113 NIR_PASS(progress, s, nir_opt_algebraic);
114 NIR_PASS(progress, s, nir_opt_constant_folding);
115 NIR_PASS(progress, s, nir_opt_undef);
116 NIR_PASS(progress, s, nir_opt_loop_unroll,
117 nir_var_shader_in |
118 nir_var_shader_out |
119 nir_var_function_temp);
120 } while (progress);
121
122 NIR_PASS_V(s, nir_lower_int_to_float);
123 NIR_PASS_V(s, nir_lower_bool_to_float);
124 NIR_PASS_V(s, nir_copy_prop);
125 NIR_PASS_V(s, nir_lower_locals_to_regs);
126 NIR_PASS_V(s, nir_convert_from_ssa, true);
127 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
128 nir_sweep(s);
129 }
130
131 static void
132 lima_program_optimize_fs_nir(struct nir_shader *s)
133 {
134 bool progress;
135
136 NIR_PASS_V(s, nir_lower_fragcoord_wtrans);
137 NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size, 0);
138 NIR_PASS_V(s, nir_lower_regs_to_ssa);
139
140 do {
141 progress = false;
142
143 NIR_PASS_V(s, nir_lower_vars_to_ssa);
144 //NIR_PASS(progress, s, nir_lower_alu_to_scalar, NULL);
145 NIR_PASS(progress, s, nir_lower_phis_to_scalar);
146 NIR_PASS(progress, s, nir_copy_prop);
147 NIR_PASS(progress, s, nir_opt_remove_phis);
148 NIR_PASS(progress, s, nir_opt_dce);
149 NIR_PASS(progress, s, nir_opt_dead_cf);
150 NIR_PASS(progress, s, nir_opt_cse);
151 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
152 NIR_PASS(progress, s, nir_opt_algebraic);
153 NIR_PASS(progress, s, nir_opt_constant_folding);
154 NIR_PASS(progress, s, nir_opt_undef);
155 NIR_PASS(progress, s, nir_opt_loop_unroll,
156 nir_var_shader_in |
157 nir_var_shader_out |
158 nir_var_function_temp);
159 } while (progress);
160
161 NIR_PASS_V(s, nir_lower_int_to_float);
162 NIR_PASS_V(s, nir_lower_bool_to_float);
163
164 /* Lower modifiers */
165 NIR_PASS_V(s, nir_lower_to_source_mods, nir_lower_all_source_mods);
166 NIR_PASS_V(s, nir_copy_prop);
167 NIR_PASS_V(s, nir_opt_dce);
168
169 NIR_PASS_V(s, nir_lower_locals_to_regs);
170 NIR_PASS_V(s, nir_convert_from_ssa, true);
171 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
172
173 NIR_PASS_V(s, nir_move_vec_src_uses_to_dest);
174 NIR_PASS_V(s, nir_lower_vec_to_movs);
175
176 nir_sweep(s);
177 }
178
179 static void *
180 lima_create_fs_state(struct pipe_context *pctx,
181 const struct pipe_shader_state *cso)
182 {
183 struct lima_screen *screen = lima_screen(pctx->screen);
184 struct lima_fs_shader_state *so = rzalloc(NULL, struct lima_fs_shader_state);
185
186 if (!so)
187 return NULL;
188
189 nir_shader *nir;
190 if (cso->type == PIPE_SHADER_IR_NIR)
191 nir = cso->ir.nir;
192 else {
193 assert(cso->type == PIPE_SHADER_IR_TGSI);
194
195 nir = tgsi_to_nir(cso->tokens, pctx->screen);
196 }
197
198 lima_program_optimize_fs_nir(nir);
199
200 if (lima_debug & LIMA_DEBUG_PP)
201 nir_print_shader(nir, stdout);
202
203 if (!ppir_compile_nir(so, nir, screen->pp_ra)) {
204 ralloc_free(so);
205 return NULL;
206 }
207
208 return so;
209 }
210
211 static void
212 lima_bind_fs_state(struct pipe_context *pctx, void *hwcso)
213 {
214 struct lima_context *ctx = lima_context(pctx);
215
216 ctx->fs = hwcso;
217 ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_FRAG;
218 }
219
220 static void
221 lima_delete_fs_state(struct pipe_context *pctx, void *hwcso)
222 {
223 struct lima_fs_shader_state *so = hwcso;
224
225 if (so->bo)
226 lima_bo_free(so->bo);
227
228 ralloc_free(so);
229 }
230
231 bool
232 lima_update_vs_state(struct lima_context *ctx)
233 {
234 struct lima_vs_shader_state *vs = ctx->vs;
235 if (!vs->bo) {
236 struct lima_screen *screen = lima_screen(ctx->base.screen);
237 vs->bo = lima_bo_create(screen, vs->shader_size, 0);
238 if (!vs->bo) {
239 fprintf(stderr, "lima: create vs shader bo fail\n");
240 return false;
241 }
242
243 memcpy(lima_bo_map(vs->bo), vs->shader, vs->shader_size);
244 ralloc_free(vs->shader);
245 vs->shader = NULL;
246 }
247
248 return true;
249 }
250
251 bool
252 lima_update_fs_state(struct lima_context *ctx)
253 {
254 struct lima_fs_shader_state *fs = ctx->fs;
255 if (!fs->bo) {
256 struct lima_screen *screen = lima_screen(ctx->base.screen);
257 fs->bo = lima_bo_create(screen, fs->shader_size, 0);
258 if (!fs->bo) {
259 fprintf(stderr, "lima: create fs shader bo fail\n");
260 return false;
261 }
262
263 memcpy(lima_bo_map(fs->bo), fs->shader, fs->shader_size);
264 ralloc_free(fs->shader);
265 fs->shader = NULL;
266 }
267
268 return true;
269 }
270
271 static void *
272 lima_create_vs_state(struct pipe_context *pctx,
273 const struct pipe_shader_state *cso)
274 {
275 struct lima_vs_shader_state *so = rzalloc(NULL, struct lima_vs_shader_state);
276
277 if (!so)
278 return NULL;
279
280 nir_shader *nir;
281 if (cso->type == PIPE_SHADER_IR_NIR)
282 nir = cso->ir.nir;
283 else {
284 assert(cso->type == PIPE_SHADER_IR_TGSI);
285
286 nir = tgsi_to_nir(cso->tokens, pctx->screen);
287 }
288
289 lima_program_optimize_vs_nir(nir);
290
291 if (lima_debug & LIMA_DEBUG_GP)
292 nir_print_shader(nir, stdout);
293
294 if (!gpir_compile_nir(so, nir)) {
295 ralloc_free(so);
296 return NULL;
297 }
298
299 return so;
300 }
301
302 static void
303 lima_bind_vs_state(struct pipe_context *pctx, void *hwcso)
304 {
305 struct lima_context *ctx = lima_context(pctx);
306
307 ctx->vs = hwcso;
308 ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_VERT;
309 }
310
311 static void
312 lima_delete_vs_state(struct pipe_context *pctx, void *hwcso)
313 {
314 struct lima_vs_shader_state *so = hwcso;
315
316 if (so->bo)
317 lima_bo_free(so->bo);
318
319 ralloc_free(so);
320 }
321
322 void
323 lima_program_init(struct lima_context *ctx)
324 {
325 ctx->base.create_fs_state = lima_create_fs_state;
326 ctx->base.bind_fs_state = lima_bind_fs_state;
327 ctx->base.delete_fs_state = lima_delete_fs_state;
328
329 ctx->base.create_vs_state = lima_create_vs_state;
330 ctx->base.bind_vs_state = lima_bind_vs_state;
331 ctx->base.delete_vs_state = lima_delete_vs_state;
332 }