bd7c40f7f6e59afba2fdc520281694fba853988b
[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_job.h"
38 #include "lima_program.h"
39 #include "lima_bo.h"
40 #include "lima_format.h"
41
42 #include "ir/lima_ir.h"
43
44 static const nir_shader_compiler_options vs_nir_options = {
45 .lower_ffma = true,
46 .lower_fpow = true,
47 .lower_ffract = true,
48 .lower_fdiv = true,
49 .lower_fmod = true,
50 .lower_fsqrt = true,
51 .lower_sub = true,
52 .lower_flrp32 = true,
53 .lower_flrp64 = true,
54 /* could be implemented by clamp */
55 .lower_fsat = true,
56 .lower_bitops = true,
57 .lower_rotate = true,
58 .lower_sincos = true,
59 .lower_fceil = true,
60 };
61
62 static const nir_shader_compiler_options fs_nir_options = {
63 .lower_ffma = true,
64 .lower_fpow = true,
65 .lower_fdiv = true,
66 .lower_fmod = true,
67 .lower_sub = true,
68 .lower_flrp32 = true,
69 .lower_flrp64 = true,
70 .lower_fsign = true,
71 .lower_rotate = true,
72 .lower_fdot = true,
73 .lower_fdph = true,
74 .lower_bitops = true,
75 .lower_vector_cmp = true,
76 };
77
78 const void *
79 lima_program_get_compiler_options(enum pipe_shader_type shader)
80 {
81 switch (shader) {
82 case PIPE_SHADER_VERTEX:
83 return &vs_nir_options;
84 case PIPE_SHADER_FRAGMENT:
85 return &fs_nir_options;
86 default:
87 return NULL;
88 }
89 }
90
91 static int
92 type_size(const struct glsl_type *type, bool bindless)
93 {
94 return glsl_count_attribute_slots(type, false);
95 }
96
97 void
98 lima_program_optimize_vs_nir(struct nir_shader *s)
99 {
100 bool progress;
101
102 NIR_PASS_V(s, nir_lower_viewport_transform);
103 NIR_PASS_V(s, nir_lower_point_size, 1.0f, 100.0f);
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, lima_nir_lower_ftrunc);
124 NIR_PASS(progress, s, nir_opt_constant_folding);
125 NIR_PASS(progress, s, nir_opt_undef);
126 NIR_PASS(progress, s, nir_opt_loop_unroll,
127 nir_var_shader_in |
128 nir_var_shader_out |
129 nir_var_function_temp);
130 } while (progress);
131
132 NIR_PASS_V(s, nir_lower_int_to_float);
133 /* int_to_float pass generates ftrunc, so lower it */
134 NIR_PASS(progress, s, lima_nir_lower_ftrunc);
135 NIR_PASS_V(s, nir_lower_bool_to_float);
136
137 NIR_PASS_V(s, nir_copy_prop);
138 NIR_PASS_V(s, nir_opt_dce);
139 NIR_PASS_V(s, nir_lower_locals_to_regs);
140 NIR_PASS_V(s, nir_convert_from_ssa, true);
141 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
142 nir_sweep(s);
143 }
144
145 static bool
146 lima_alu_to_scalar_filter_cb(const nir_instr *instr, const void *data)
147 {
148 if (instr->type != nir_instr_type_alu)
149 return false;
150
151 nir_alu_instr *alu = nir_instr_as_alu(instr);
152 switch (alu->op) {
153 case nir_op_frcp:
154 case nir_op_frsq:
155 case nir_op_flog2:
156 case nir_op_fexp2:
157 case nir_op_fsqrt:
158 case nir_op_fsin:
159 case nir_op_fcos:
160 return true;
161 default:
162 break;
163 }
164
165 /* nir vec4 fcsel assumes that each component of the condition will be
166 * used to select the same component from the two options, but Utgard PP
167 * has only 1 component condition. If all condition components are not the
168 * same we need to lower it to scalar.
169 */
170 switch (alu->op) {
171 case nir_op_bcsel:
172 case nir_op_fcsel:
173 break;
174 default:
175 return false;
176 }
177
178 int num_components = nir_dest_num_components(alu->dest.dest);
179
180 uint8_t swizzle = alu->src[0].swizzle[0];
181
182 for (int i = 1; i < num_components; i++)
183 if (alu->src[0].swizzle[i] != swizzle)
184 return true;
185
186 return false;
187 }
188
189 void
190 lima_program_optimize_fs_nir(struct nir_shader *s,
191 struct nir_lower_tex_options *tex_options)
192 {
193 bool progress;
194
195 NIR_PASS_V(s, nir_lower_fragcoord_wtrans);
196 NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size, 0);
197 NIR_PASS_V(s, nir_lower_regs_to_ssa);
198 NIR_PASS_V(s, nir_lower_tex, tex_options);
199
200 do {
201 progress = false;
202 NIR_PASS(progress, s, nir_opt_vectorize);
203 } while (progress);
204
205 do {
206 progress = false;
207
208 NIR_PASS_V(s, nir_lower_vars_to_ssa);
209 NIR_PASS(progress, s, nir_lower_alu_to_scalar, lima_alu_to_scalar_filter_cb, NULL);
210 NIR_PASS(progress, s, nir_copy_prop);
211 NIR_PASS(progress, s, nir_opt_remove_phis);
212 NIR_PASS(progress, s, nir_opt_dce);
213 NIR_PASS(progress, s, nir_opt_dead_cf);
214 NIR_PASS(progress, s, nir_opt_cse);
215 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
216 NIR_PASS(progress, s, nir_opt_algebraic);
217 NIR_PASS(progress, s, nir_opt_constant_folding);
218 NIR_PASS(progress, s, nir_opt_undef);
219 NIR_PASS(progress, s, nir_opt_loop_unroll,
220 nir_var_shader_in |
221 nir_var_shader_out |
222 nir_var_function_temp);
223 NIR_PASS(progress, s, lima_nir_split_load_input);
224 } while (progress);
225
226 NIR_PASS_V(s, nir_lower_int_to_float);
227 NIR_PASS_V(s, nir_lower_bool_to_float);
228
229 /* Some ops must be lowered after being converted from int ops,
230 * so re-run nir_opt_algebraic after int lowering. */
231 do {
232 progress = false;
233 NIR_PASS(progress, s, nir_opt_algebraic);
234 } while (progress);
235
236 /* Must be run after optimization loop */
237 NIR_PASS_V(s, lima_nir_scale_trig);
238
239 /* Lower modifiers */
240 NIR_PASS_V(s, nir_lower_to_source_mods, nir_lower_all_source_mods);
241 NIR_PASS_V(s, nir_copy_prop);
242 NIR_PASS_V(s, nir_opt_dce);
243
244 NIR_PASS_V(s, nir_lower_locals_to_regs);
245 NIR_PASS_V(s, nir_convert_from_ssa, true);
246 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
247
248 NIR_PASS_V(s, nir_move_vec_src_uses_to_dest);
249 NIR_PASS_V(s, nir_lower_vec_to_movs);
250
251 NIR_PASS_V(s, lima_nir_duplicate_load_uniforms);
252 NIR_PASS_V(s, lima_nir_duplicate_load_inputs);
253
254 nir_sweep(s);
255 }
256
257 static bool
258 lima_fs_compile_shader(struct lima_context *ctx,
259 struct lima_fs_shader_state *fs,
260 struct nir_lower_tex_options *tex_options)
261 {
262 struct lima_screen *screen = lima_screen(ctx->base.screen);
263 nir_shader *nir = nir_shader_clone(fs, fs->base.ir.nir);
264
265 lima_program_optimize_fs_nir(nir, tex_options);
266
267 if (lima_debug & LIMA_DEBUG_PP)
268 nir_print_shader(nir, stdout);
269
270 if (!ppir_compile_nir(fs, nir, screen->pp_ra, &ctx->debug)) {
271 ralloc_free(nir);
272 return false;
273 }
274
275 fs->uses_discard = nir->info.fs.uses_discard;
276 ralloc_free(nir);
277
278 return true;
279 }
280
281 static void *
282 lima_create_fs_state(struct pipe_context *pctx,
283 const struct pipe_shader_state *cso)
284 {
285 struct lima_context *ctx = lima_context(pctx);
286 struct lima_fs_shader_state *so = rzalloc(NULL, struct lima_fs_shader_state);
287
288 if (!so)
289 return NULL;
290
291 nir_shader *nir;
292 if (cso->type == PIPE_SHADER_IR_NIR)
293 /* The backend takes ownership of the NIR shader on state
294 * creation.
295 */
296 nir = cso->ir.nir;
297 else {
298 assert(cso->type == PIPE_SHADER_IR_TGSI);
299
300 nir = tgsi_to_nir(cso->tokens, pctx->screen);
301 }
302
303 so->base.type = PIPE_SHADER_IR_NIR;
304 so->base.ir.nir = nir;
305
306 uint8_t identity[4] = { PIPE_SWIZZLE_X,
307 PIPE_SWIZZLE_Y,
308 PIPE_SWIZZLE_Z,
309 PIPE_SWIZZLE_W };
310
311 struct nir_lower_tex_options tex_options = {
312 .lower_txp = ~0u,
313 .swizzle_result = 0,
314 };
315
316 /* Initialize with identity swizzles. That should suffice for most shaders */
317 for (int i = 0; i < PIPE_MAX_SAMPLERS; i++)
318 memcpy(so->swizzles[i], identity, 4);
319
320 if (!lima_fs_compile_shader(ctx, so, &tex_options)) {
321 ralloc_free(so);
322 return NULL;
323 }
324
325 return so;
326 }
327
328 static void
329 lima_bind_fs_state(struct pipe_context *pctx, void *hwcso)
330 {
331 struct lima_context *ctx = lima_context(pctx);
332
333 ctx->fs = hwcso;
334 ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_FRAG;
335 }
336
337 static void
338 lima_delete_fs_state(struct pipe_context *pctx, void *hwcso)
339 {
340 struct lima_fs_shader_state *so = hwcso;
341
342 if (so->bo)
343 lima_bo_unreference(so->bo);
344
345 ralloc_free(so->base.ir.nir);
346 ralloc_free(so);
347 }
348
349 bool
350 lima_update_vs_state(struct lima_context *ctx)
351 {
352 struct lima_vs_shader_state *vs = ctx->vs;
353 if (!vs->bo) {
354 struct lima_screen *screen = lima_screen(ctx->base.screen);
355 vs->bo = lima_bo_create(screen, vs->shader_size, 0);
356 if (!vs->bo) {
357 fprintf(stderr, "lima: create vs shader bo fail\n");
358 return false;
359 }
360
361 memcpy(lima_bo_map(vs->bo), vs->shader, vs->shader_size);
362 ralloc_free(vs->shader);
363 vs->shader = NULL;
364 }
365
366 return true;
367 }
368
369 bool
370 lima_update_fs_state(struct lima_context *ctx)
371 {
372 struct lima_fs_shader_state *fs = ctx->fs;
373 struct lima_texture_stateobj *lima_tex = &ctx->tex_stateobj;
374 struct nir_lower_tex_options tex_options = {
375 .lower_txp = ~0u,
376 .swizzle_result = 0,
377 };
378 bool needs_recompile = false;
379
380 /* Check if texture formats has changed since last compilation.
381 * If it has we need to recompile shader.
382 */
383 if (((ctx->dirty & LIMA_CONTEXT_DIRTY_TEXTURES) &&
384 lima_tex->num_samplers &&
385 lima_tex->num_textures)) {
386 uint8_t identity[4] = { PIPE_SWIZZLE_X,
387 PIPE_SWIZZLE_Y,
388 PIPE_SWIZZLE_Z,
389 PIPE_SWIZZLE_W };
390 for (int i = 0; i < lima_tex->num_samplers; i++) {
391 struct lima_sampler_view *texture = lima_sampler_view(lima_tex->textures[i]);
392 struct pipe_resource *prsc = texture->base.texture;
393 const uint8_t *swizzle = lima_format_get_texel_swizzle(prsc->format);
394 if (memcmp(fs->swizzles[i], swizzle, 4)) {
395 needs_recompile = true;
396 memcpy(fs->swizzles[i], swizzle, 4);
397 }
398
399 for (int j = 0; j < 4; j++)
400 tex_options.swizzles[i][j] = swizzle[j];
401
402 if (memcmp(swizzle, identity, 4))
403 tex_options.swizzle_result |= (1 << i);
404 }
405
406 /* Fill rest with identity swizzle */
407 for (int i = lima_tex->num_samplers; i < PIPE_MAX_SAMPLERS; i++)
408 memcpy(fs->swizzles[i], identity, 4);
409 }
410
411 if (needs_recompile) {
412 if (fs->bo) {
413 lima_bo_unreference(fs->bo);
414 fs->bo = NULL;
415 }
416
417 if (!lima_fs_compile_shader(ctx, fs, &tex_options))
418 return false;
419 }
420
421 if (!fs->bo) {
422 struct lima_screen *screen = lima_screen(ctx->base.screen);
423 fs->bo = lima_bo_create(screen, fs->shader_size, 0);
424 if (!fs->bo) {
425 fprintf(stderr, "lima: create fs shader bo fail\n");
426 return false;
427 }
428
429 memcpy(lima_bo_map(fs->bo), fs->shader, fs->shader_size);
430 ralloc_free(fs->shader);
431 fs->shader = NULL;
432 }
433
434 struct lima_job *job = lima_job_get(ctx);
435 job->pp_max_stack_size = MAX2(job->pp_max_stack_size, ctx->fs->stack_size);
436
437 return true;
438 }
439
440 static void *
441 lima_create_vs_state(struct pipe_context *pctx,
442 const struct pipe_shader_state *cso)
443 {
444 struct lima_context *ctx = lima_context(pctx);
445 struct lima_vs_shader_state *so = rzalloc(NULL, struct lima_vs_shader_state);
446
447 if (!so)
448 return NULL;
449
450 nir_shader *nir;
451 if (cso->type == PIPE_SHADER_IR_NIR)
452 nir = cso->ir.nir;
453 else {
454 assert(cso->type == PIPE_SHADER_IR_TGSI);
455
456 nir = tgsi_to_nir(cso->tokens, pctx->screen);
457 }
458
459 lima_program_optimize_vs_nir(nir);
460
461 if (lima_debug & LIMA_DEBUG_GP)
462 nir_print_shader(nir, stdout);
463
464 if (!gpir_compile_nir(so, nir, &ctx->debug)) {
465 ralloc_free(so);
466 return NULL;
467 }
468
469 ralloc_free(nir);
470
471 return so;
472 }
473
474 static void
475 lima_bind_vs_state(struct pipe_context *pctx, void *hwcso)
476 {
477 struct lima_context *ctx = lima_context(pctx);
478
479 ctx->vs = hwcso;
480 ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_VERT;
481 }
482
483 static void
484 lima_delete_vs_state(struct pipe_context *pctx, void *hwcso)
485 {
486 struct lima_vs_shader_state *so = hwcso;
487
488 if (so->bo)
489 lima_bo_unreference(so->bo);
490
491 ralloc_free(so);
492 }
493
494 void
495 lima_program_init(struct lima_context *ctx)
496 {
497 ctx->base.create_fs_state = lima_create_fs_state;
498 ctx->base.bind_fs_state = lima_bind_fs_state;
499 ctx->base.delete_fs_state = lima_delete_fs_state;
500
501 ctx->base.create_vs_state = lima_create_vs_state;
502 ctx->base.bind_vs_state = lima_bind_vs_state;
503 ctx->base.delete_vs_state = lima_delete_vs_state;
504 }