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