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