33a695557a6208e909fc1c25b5b45b6ff63e93cb
[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 "ir/lima_ir.h"
41
42 static const nir_shader_compiler_options vs_nir_options = {
43 .lower_ffma = true,
44 .lower_fpow = true,
45 .lower_ffract = true,
46 .lower_fdiv = true,
47 .lower_fmod = true,
48 .lower_fsqrt = true,
49 .lower_sub = true,
50 .lower_flrp32 = true,
51 .lower_flrp64 = 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, lima_nir_lower_ftrunc);
126 NIR_PASS(progress, s, nir_opt_constant_folding);
127 NIR_PASS(progress, s, nir_opt_undef);
128 NIR_PASS(progress, s, nir_opt_loop_unroll,
129 nir_var_shader_in |
130 nir_var_shader_out |
131 nir_var_function_temp);
132 } while (progress);
133
134 NIR_PASS_V(s, nir_lower_int_to_float);
135 /* int_to_float pass generates ftrunc, so lower it */
136 NIR_PASS(progress, s, lima_nir_lower_ftrunc);
137 NIR_PASS_V(s, nir_lower_bool_to_float);
138
139 NIR_PASS_V(s, nir_copy_prop);
140 NIR_PASS_V(s, nir_opt_dce);
141 NIR_PASS_V(s, nir_lower_locals_to_regs);
142 NIR_PASS_V(s, nir_convert_from_ssa, true);
143 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
144 nir_sweep(s);
145 }
146
147 static bool
148 lima_alu_to_scalar_filter_cb(const nir_instr *instr, const void *data)
149 {
150 if (instr->type != nir_instr_type_alu)
151 return false;
152
153 nir_alu_instr *alu = nir_instr_as_alu(instr);
154 switch (alu->op) {
155 case nir_op_frcp:
156 case nir_op_frsq:
157 case nir_op_flog2:
158 case nir_op_fexp2:
159 case nir_op_fsqrt:
160 case nir_op_fsin:
161 case nir_op_fcos:
162 return true;
163 default:
164 break;
165 }
166
167 /* nir vec4 fcsel assumes that each component of the condition will be
168 * used to select the same component from the two options, but Utgard PP
169 * has only 1 component condition. If all condition components are not the
170 * same we need to lower it to scalar.
171 */
172 switch (alu->op) {
173 case nir_op_bcsel:
174 case nir_op_fcsel:
175 break;
176 default:
177 return false;
178 }
179
180 int num_components = nir_dest_num_components(alu->dest.dest);
181
182 uint8_t swizzle = alu->src[0].swizzle[0];
183
184 for (int i = 1; i < num_components; i++)
185 if (alu->src[0].swizzle[i] != swizzle)
186 return true;
187
188 return false;
189 }
190
191 void
192 lima_program_optimize_fs_nir(struct nir_shader *s)
193 {
194 bool progress;
195
196 NIR_PASS_V(s, nir_lower_fragcoord_wtrans);
197 NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size, 0);
198 NIR_PASS_V(s, nir_lower_regs_to_ssa);
199 NIR_PASS_V(s, nir_lower_tex, &tex_options);
200
201 do {
202 progress = false;
203 NIR_PASS(progress, s, nir_opt_vectorize);
204 } while (progress);
205
206 do {
207 progress = false;
208
209 NIR_PASS_V(s, nir_lower_vars_to_ssa);
210 NIR_PASS(progress, s, nir_lower_alu_to_scalar, lima_alu_to_scalar_filter_cb, NULL);
211 NIR_PASS(progress, s, nir_copy_prop);
212 NIR_PASS(progress, s, nir_opt_remove_phis);
213 NIR_PASS(progress, s, nir_opt_dce);
214 NIR_PASS(progress, s, nir_opt_dead_cf);
215 NIR_PASS(progress, s, nir_opt_cse);
216 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
217 NIR_PASS(progress, s, nir_opt_algebraic);
218 NIR_PASS(progress, s, nir_opt_constant_folding);
219 NIR_PASS(progress, s, nir_opt_undef);
220 NIR_PASS(progress, s, nir_opt_loop_unroll,
221 nir_var_shader_in |
222 nir_var_shader_out |
223 nir_var_function_temp);
224 NIR_PASS(progress, s, lima_nir_split_load_input);
225 } while (progress);
226
227 NIR_PASS_V(s, nir_lower_int_to_float);
228 NIR_PASS_V(s, nir_lower_bool_to_float);
229
230 /* Some ops must be lowered after being converted from int ops,
231 * so re-run nir_opt_algebraic after int lowering. */
232 do {
233 progress = false;
234 NIR_PASS(progress, s, nir_opt_algebraic);
235 } while (progress);
236
237 /* Must be run after optimization loop */
238 NIR_PASS_V(s, lima_nir_scale_trig);
239
240 /* Lower modifiers */
241 NIR_PASS_V(s, nir_lower_to_source_mods, nir_lower_all_source_mods);
242 NIR_PASS_V(s, nir_copy_prop);
243 NIR_PASS_V(s, nir_opt_dce);
244
245 NIR_PASS_V(s, nir_lower_locals_to_regs);
246 NIR_PASS_V(s, nir_convert_from_ssa, true);
247 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
248
249 NIR_PASS_V(s, nir_move_vec_src_uses_to_dest);
250 NIR_PASS_V(s, nir_lower_vec_to_movs);
251
252 nir_sweep(s);
253 }
254
255 static void *
256 lima_create_fs_state(struct pipe_context *pctx,
257 const struct pipe_shader_state *cso)
258 {
259 struct lima_context *ctx = lima_context(pctx);
260 struct lima_screen *screen = lima_screen(pctx->screen);
261 struct lima_fs_shader_state *so = rzalloc(NULL, struct lima_fs_shader_state);
262
263 if (!so)
264 return NULL;
265
266 nir_shader *nir;
267 if (cso->type == PIPE_SHADER_IR_NIR)
268 nir = cso->ir.nir;
269 else {
270 assert(cso->type == PIPE_SHADER_IR_TGSI);
271
272 nir = tgsi_to_nir(cso->tokens, pctx->screen);
273 }
274
275 lima_program_optimize_fs_nir(nir);
276
277 if (lima_debug & LIMA_DEBUG_PP)
278 nir_print_shader(nir, stdout);
279
280 if (!ppir_compile_nir(so, nir, screen->pp_ra, &ctx->debug)) {
281 ralloc_free(so);
282 return NULL;
283 }
284
285 so->uses_discard = nir->info.fs.uses_discard;
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_unreference(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 struct lima_job *job = lima_job_get(ctx);
348 job->pp_max_stack_size = MAX2(job->pp_max_stack_size, ctx->fs->stack_size);
349
350 return true;
351 }
352
353 static void *
354 lima_create_vs_state(struct pipe_context *pctx,
355 const struct pipe_shader_state *cso)
356 {
357 struct lima_context *ctx = lima_context(pctx);
358 struct lima_vs_shader_state *so = rzalloc(NULL, struct lima_vs_shader_state);
359
360 if (!so)
361 return NULL;
362
363 nir_shader *nir;
364 if (cso->type == PIPE_SHADER_IR_NIR)
365 nir = cso->ir.nir;
366 else {
367 assert(cso->type == PIPE_SHADER_IR_TGSI);
368
369 nir = tgsi_to_nir(cso->tokens, pctx->screen);
370 }
371
372 lima_program_optimize_vs_nir(nir);
373
374 if (lima_debug & LIMA_DEBUG_GP)
375 nir_print_shader(nir, stdout);
376
377 if (!gpir_compile_nir(so, nir, &ctx->debug)) {
378 ralloc_free(so);
379 return NULL;
380 }
381
382 ralloc_free(nir);
383
384 return so;
385 }
386
387 static void
388 lima_bind_vs_state(struct pipe_context *pctx, void *hwcso)
389 {
390 struct lima_context *ctx = lima_context(pctx);
391
392 ctx->vs = hwcso;
393 ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_VERT;
394 }
395
396 static void
397 lima_delete_vs_state(struct pipe_context *pctx, void *hwcso)
398 {
399 struct lima_vs_shader_state *so = hwcso;
400
401 if (so->bo)
402 lima_bo_unreference(so->bo);
403
404 ralloc_free(so);
405 }
406
407 void
408 lima_program_init(struct lima_context *ctx)
409 {
410 ctx->base.create_fs_state = lima_create_fs_state;
411 ctx->base.bind_fs_state = lima_bind_fs_state;
412 ctx->base.delete_fs_state = lima_delete_fs_state;
413
414 ctx->base.create_vs_state = lima_create_vs_state;
415 ctx->base.bind_vs_state = lima_bind_vs_state;
416 ctx->base.delete_vs_state = lima_delete_vs_state;
417 }