meson: don't generate file into subdirs
[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 .lower_fceil = true,
58 };
59
60 static const nir_shader_compiler_options fs_nir_options = {
61 .lower_ffma = true,
62 .lower_fpow = true,
63 .lower_fdiv = true,
64 .lower_fmod = true,
65 .lower_sub = true,
66 .lower_flrp32 = true,
67 .lower_flrp64 = true,
68 .lower_fsign = true,
69 .lower_rotate = true,
70 .lower_fdot = true,
71 .lower_bitops = true,
72 .lower_vector_cmp = true,
73 };
74
75 static const struct nir_lower_tex_options tex_options = {
76 .lower_txp = ~0u,
77 };
78
79 const void *
80 lima_program_get_compiler_options(enum pipe_shader_type shader)
81 {
82 switch (shader) {
83 case PIPE_SHADER_VERTEX:
84 return &vs_nir_options;
85 case PIPE_SHADER_FRAGMENT:
86 return &fs_nir_options;
87 default:
88 return NULL;
89 }
90 }
91
92 static int
93 type_size(const struct glsl_type *type, bool bindless)
94 {
95 return glsl_count_attribute_slots(type, false);
96 }
97
98 void
99 lima_program_optimize_vs_nir(struct nir_shader *s)
100 {
101 bool progress;
102
103 NIR_PASS_V(s, nir_lower_viewport_transform);
104 NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size, 0);
105 NIR_PASS_V(s, nir_lower_load_const_to_scalar);
106 NIR_PASS_V(s, lima_nir_lower_uniform_to_scalar);
107 NIR_PASS_V(s, nir_lower_io_to_scalar,
108 nir_var_shader_in|nir_var_shader_out);
109
110 do {
111 progress = false;
112
113 NIR_PASS_V(s, nir_lower_vars_to_ssa);
114 NIR_PASS(progress, s, nir_lower_alu_to_scalar, NULL, NULL);
115 NIR_PASS(progress, s, nir_lower_phis_to_scalar);
116 NIR_PASS(progress, s, nir_copy_prop);
117 NIR_PASS(progress, s, nir_opt_remove_phis);
118 NIR_PASS(progress, s, nir_opt_dce);
119 NIR_PASS(progress, s, nir_opt_dead_cf);
120 NIR_PASS(progress, s, nir_opt_cse);
121 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
122 NIR_PASS(progress, s, nir_opt_algebraic);
123 NIR_PASS(progress, s, nir_opt_constant_folding);
124 NIR_PASS(progress, s, nir_opt_undef);
125 NIR_PASS(progress, s, nir_opt_loop_unroll,
126 nir_var_shader_in |
127 nir_var_shader_out |
128 nir_var_function_temp);
129 } while (progress);
130
131 NIR_PASS_V(s, nir_lower_int_to_float);
132 /* Run opt_algebraic between int_to_float and bool_to_float because
133 * int_to_float emits ftrunc, and ftrunc lowering generates bool ops
134 */
135 do {
136 progress = false;
137 NIR_PASS(progress, s, nir_opt_algebraic);
138 } while (progress);
139
140 NIR_PASS_V(s, nir_lower_bool_to_float);
141
142 NIR_PASS_V(s, nir_copy_prop);
143 NIR_PASS_V(s, nir_opt_dce);
144 NIR_PASS_V(s, nir_lower_locals_to_regs);
145 NIR_PASS_V(s, nir_convert_from_ssa, true);
146 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
147 nir_sweep(s);
148 }
149
150 static bool
151 lima_alu_to_scalar_filter_cb(const nir_instr *instr, const void *data)
152 {
153 if (instr->type != nir_instr_type_alu)
154 return false;
155
156 nir_alu_instr *alu = nir_instr_as_alu(instr);
157 switch (alu->op) {
158 case nir_op_frcp:
159 case nir_op_frsq:
160 case nir_op_flog2:
161 case nir_op_fexp2:
162 case nir_op_fsqrt:
163 case nir_op_fsin:
164 case nir_op_fcos:
165 return true;
166 default:
167 break;
168 }
169
170 /* nir vec4 fcsel assumes that each component of the condition will be
171 * used to select the same component from the two options, but Utgard PP
172 * has only 1 component condition. If all condition components are not the
173 * same we need to lower it to scalar.
174 */
175 switch (alu->op) {
176 case nir_op_bcsel:
177 case nir_op_fcsel:
178 break;
179 default:
180 return false;
181 }
182
183 int num_components = nir_dest_num_components(alu->dest.dest);
184
185 uint8_t swizzle = alu->src[0].swizzle[0];
186
187 for (int i = 1; i < num_components; i++)
188 if (alu->src[0].swizzle[i] != swizzle)
189 return true;
190
191 return false;
192 }
193
194 void
195 lima_program_optimize_fs_nir(struct nir_shader *s)
196 {
197 bool progress;
198
199 NIR_PASS_V(s, nir_lower_fragcoord_wtrans);
200 NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size, 0);
201 NIR_PASS_V(s, nir_lower_regs_to_ssa);
202 NIR_PASS_V(s, nir_lower_tex, &tex_options);
203
204 do {
205 progress = false;
206 NIR_PASS(progress, s, nir_opt_vectorize);
207 } while (progress);
208
209 do {
210 progress = false;
211
212 NIR_PASS_V(s, nir_lower_vars_to_ssa);
213 NIR_PASS(progress, s, nir_lower_alu_to_scalar, lima_alu_to_scalar_filter_cb, NULL);
214 NIR_PASS(progress, s, nir_copy_prop);
215 NIR_PASS(progress, s, nir_opt_remove_phis);
216 NIR_PASS(progress, s, nir_opt_dce);
217 NIR_PASS(progress, s, nir_opt_dead_cf);
218 NIR_PASS(progress, s, nir_opt_cse);
219 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
220 NIR_PASS(progress, s, nir_opt_algebraic);
221 NIR_PASS(progress, s, nir_opt_constant_folding);
222 NIR_PASS(progress, s, nir_opt_undef);
223 NIR_PASS(progress, s, nir_opt_loop_unroll,
224 nir_var_shader_in |
225 nir_var_shader_out |
226 nir_var_function_temp);
227 } while (progress);
228
229 NIR_PASS_V(s, nir_lower_int_to_float);
230 NIR_PASS_V(s, nir_lower_bool_to_float);
231
232 /* Some ops must be lowered after being converted from int ops,
233 * so re-run nir_opt_algebraic after int lowering. */
234 do {
235 progress = false;
236 NIR_PASS(progress, s, nir_opt_algebraic);
237 } while (progress);
238
239 /* Must be run after optimization loop */
240 NIR_PASS_V(s, lima_nir_scale_trig);
241
242 /* Lower modifiers */
243 NIR_PASS_V(s, nir_lower_to_source_mods, nir_lower_all_source_mods);
244 NIR_PASS_V(s, nir_copy_prop);
245 NIR_PASS_V(s, nir_opt_dce);
246
247 NIR_PASS_V(s, nir_lower_locals_to_regs);
248 NIR_PASS_V(s, nir_convert_from_ssa, true);
249 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
250
251 NIR_PASS_V(s, nir_move_vec_src_uses_to_dest);
252 NIR_PASS_V(s, nir_lower_vec_to_movs);
253
254 nir_sweep(s);
255 }
256
257 static void *
258 lima_create_fs_state(struct pipe_context *pctx,
259 const struct pipe_shader_state *cso)
260 {
261 struct lima_context *ctx = lima_context(pctx);
262 struct lima_screen *screen = lima_screen(pctx->screen);
263 struct lima_fs_shader_state *so = rzalloc(NULL, struct lima_fs_shader_state);
264
265 if (!so)
266 return NULL;
267
268 nir_shader *nir;
269 if (cso->type == PIPE_SHADER_IR_NIR)
270 nir = cso->ir.nir;
271 else {
272 assert(cso->type == PIPE_SHADER_IR_TGSI);
273
274 nir = tgsi_to_nir(cso->tokens, pctx->screen);
275 }
276
277 lima_program_optimize_fs_nir(nir);
278
279 if (lima_debug & LIMA_DEBUG_PP)
280 nir_print_shader(nir, stdout);
281
282 if (!ppir_compile_nir(so, nir, screen->pp_ra, &ctx->debug)) {
283 ralloc_free(so);
284 return NULL;
285 }
286
287 return so;
288 }
289
290 static void
291 lima_bind_fs_state(struct pipe_context *pctx, void *hwcso)
292 {
293 struct lima_context *ctx = lima_context(pctx);
294
295 ctx->fs = hwcso;
296 ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_FRAG;
297 }
298
299 static void
300 lima_delete_fs_state(struct pipe_context *pctx, void *hwcso)
301 {
302 struct lima_fs_shader_state *so = hwcso;
303
304 if (so->bo)
305 lima_bo_free(so->bo);
306
307 ralloc_free(so);
308 }
309
310 bool
311 lima_update_vs_state(struct lima_context *ctx)
312 {
313 struct lima_vs_shader_state *vs = ctx->vs;
314 if (!vs->bo) {
315 struct lima_screen *screen = lima_screen(ctx->base.screen);
316 vs->bo = lima_bo_create(screen, vs->shader_size, 0);
317 if (!vs->bo) {
318 fprintf(stderr, "lima: create vs shader bo fail\n");
319 return false;
320 }
321
322 memcpy(lima_bo_map(vs->bo), vs->shader, vs->shader_size);
323 ralloc_free(vs->shader);
324 vs->shader = NULL;
325 }
326
327 return true;
328 }
329
330 bool
331 lima_update_fs_state(struct lima_context *ctx)
332 {
333 struct lima_fs_shader_state *fs = ctx->fs;
334 if (!fs->bo) {
335 struct lima_screen *screen = lima_screen(ctx->base.screen);
336 fs->bo = lima_bo_create(screen, fs->shader_size, 0);
337 if (!fs->bo) {
338 fprintf(stderr, "lima: create fs shader bo fail\n");
339 return false;
340 }
341
342 memcpy(lima_bo_map(fs->bo), fs->shader, fs->shader_size);
343 ralloc_free(fs->shader);
344 fs->shader = NULL;
345 }
346
347 ctx->pp_max_stack_size = MAX2(ctx->pp_max_stack_size, ctx->fs->stack_size);
348
349 return true;
350 }
351
352 static void *
353 lima_create_vs_state(struct pipe_context *pctx,
354 const struct pipe_shader_state *cso)
355 {
356 struct lima_context *ctx = lima_context(pctx);
357 struct lima_vs_shader_state *so = rzalloc(NULL, struct lima_vs_shader_state);
358
359 if (!so)
360 return NULL;
361
362 nir_shader *nir;
363 if (cso->type == PIPE_SHADER_IR_NIR)
364 nir = cso->ir.nir;
365 else {
366 assert(cso->type == PIPE_SHADER_IR_TGSI);
367
368 nir = tgsi_to_nir(cso->tokens, pctx->screen);
369 }
370
371 lima_program_optimize_vs_nir(nir);
372
373 if (lima_debug & LIMA_DEBUG_GP)
374 nir_print_shader(nir, stdout);
375
376 if (!gpir_compile_nir(so, nir, &ctx->debug)) {
377 ralloc_free(so);
378 return NULL;
379 }
380
381 return so;
382 }
383
384 static void
385 lima_bind_vs_state(struct pipe_context *pctx, void *hwcso)
386 {
387 struct lima_context *ctx = lima_context(pctx);
388
389 ctx->vs = hwcso;
390 ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_VERT;
391 }
392
393 static void
394 lima_delete_vs_state(struct pipe_context *pctx, void *hwcso)
395 {
396 struct lima_vs_shader_state *so = hwcso;
397
398 if (so->bo)
399 lima_bo_free(so->bo);
400
401 ralloc_free(so);
402 }
403
404 void
405 lima_program_init(struct lima_context *ctx)
406 {
407 ctx->base.create_fs_state = lima_create_fs_state;
408 ctx->base.bind_fs_state = lima_bind_fs_state;
409 ctx->base.delete_fs_state = lima_delete_fs_state;
410
411 ctx->base.create_vs_state = lima_create_vs_state;
412 ctx->base.bind_vs_state = lima_bind_vs_state;
413 ctx->base.delete_vs_state = lima_delete_vs_state;
414 }