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