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