lima/gp: Clean up lima_program_optimize_vs_nir() a little
[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_bitshift = true,
55 .lower_rotate = true,
56 .lower_sincos = true,
57 };
58
59 static const nir_shader_compiler_options fs_nir_options = {
60 .lower_ffma = true,
61 .lower_fpow = true,
62 .lower_fdiv = true,
63 .lower_fmod = true,
64 .lower_sub = true,
65 .lower_flrp32 = true,
66 .lower_flrp64 = true,
67 .lower_fsign = true,
68 .lower_rotate = true,
69 };
70
71 const void *
72 lima_program_get_compiler_options(enum pipe_shader_type shader)
73 {
74 switch (shader) {
75 case PIPE_SHADER_VERTEX:
76 return &vs_nir_options;
77 case PIPE_SHADER_FRAGMENT:
78 return &fs_nir_options;
79 default:
80 return NULL;
81 }
82 }
83
84 static int
85 type_size(const struct glsl_type *type, bool bindless)
86 {
87 return glsl_count_attribute_slots(type, false);
88 }
89
90 void
91 lima_program_optimize_vs_nir(struct nir_shader *s)
92 {
93 bool progress;
94
95 NIR_PASS_V(s, nir_lower_viewport_transform);
96 NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size, 0);
97 NIR_PASS_V(s, nir_lower_load_const_to_scalar);
98 NIR_PASS_V(s, lima_nir_lower_uniform_to_scalar);
99 NIR_PASS_V(s, nir_lower_io_to_scalar,
100 nir_var_shader_in|nir_var_shader_out);
101
102 do {
103 progress = false;
104
105 NIR_PASS_V(s, nir_lower_vars_to_ssa);
106 NIR_PASS(progress, s, nir_lower_alu_to_scalar, NULL);
107 NIR_PASS(progress, s, nir_lower_phis_to_scalar);
108 NIR_PASS(progress, s, nir_copy_prop);
109 NIR_PASS(progress, s, nir_opt_remove_phis);
110 NIR_PASS(progress, s, nir_opt_dce);
111 NIR_PASS(progress, s, nir_opt_dead_cf);
112 NIR_PASS(progress, s, nir_opt_cse);
113 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
114 NIR_PASS(progress, s, nir_opt_algebraic);
115 NIR_PASS(progress, s, nir_opt_constant_folding);
116 NIR_PASS(progress, s, nir_opt_undef);
117 NIR_PASS(progress, s, nir_opt_loop_unroll,
118 nir_var_shader_in |
119 nir_var_shader_out |
120 nir_var_function_temp);
121 } while (progress);
122
123 NIR_PASS_V(s, nir_lower_int_to_float);
124 NIR_PASS_V(s, nir_lower_bool_to_float);
125 NIR_PASS_V(s, nir_copy_prop);
126 NIR_PASS_V(s, nir_opt_dce);
127 NIR_PASS_V(s, nir_lower_locals_to_regs);
128 NIR_PASS_V(s, nir_convert_from_ssa, true);
129 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
130 nir_sweep(s);
131 }
132
133 void
134 lima_program_optimize_fs_nir(struct nir_shader *s)
135 {
136 bool progress;
137
138 NIR_PASS_V(s, nir_lower_fragcoord_wtrans);
139 NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size, 0);
140 NIR_PASS_V(s, nir_lower_regs_to_ssa);
141
142 do {
143 progress = false;
144
145 NIR_PASS_V(s, nir_lower_vars_to_ssa);
146 //NIR_PASS(progress, s, nir_lower_alu_to_scalar, NULL);
147 NIR_PASS(progress, s, nir_lower_phis_to_scalar);
148 NIR_PASS(progress, s, nir_copy_prop);
149 NIR_PASS(progress, s, nir_opt_remove_phis);
150 NIR_PASS(progress, s, nir_opt_dce);
151 NIR_PASS(progress, s, nir_opt_dead_cf);
152 NIR_PASS(progress, s, nir_opt_cse);
153 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
154 NIR_PASS(progress, s, nir_opt_algebraic);
155 NIR_PASS(progress, s, nir_opt_constant_folding);
156 NIR_PASS(progress, s, nir_opt_undef);
157 NIR_PASS(progress, s, nir_opt_loop_unroll,
158 nir_var_shader_in |
159 nir_var_shader_out |
160 nir_var_function_temp);
161 } while (progress);
162
163 NIR_PASS_V(s, nir_lower_int_to_float);
164 NIR_PASS_V(s, nir_lower_bool_to_float);
165
166 /* Lower modifiers */
167 NIR_PASS_V(s, nir_lower_to_source_mods, nir_lower_all_source_mods);
168 NIR_PASS_V(s, nir_copy_prop);
169 NIR_PASS_V(s, nir_opt_dce);
170
171 NIR_PASS_V(s, nir_lower_locals_to_regs);
172 NIR_PASS_V(s, nir_convert_from_ssa, true);
173 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
174
175 NIR_PASS_V(s, nir_move_vec_src_uses_to_dest);
176 NIR_PASS_V(s, nir_lower_vec_to_movs);
177
178 nir_sweep(s);
179 }
180
181 static void *
182 lima_create_fs_state(struct pipe_context *pctx,
183 const struct pipe_shader_state *cso)
184 {
185 struct lima_screen *screen = lima_screen(pctx->screen);
186 struct lima_fs_shader_state *so = rzalloc(NULL, struct lima_fs_shader_state);
187
188 if (!so)
189 return NULL;
190
191 nir_shader *nir;
192 if (cso->type == PIPE_SHADER_IR_NIR)
193 nir = cso->ir.nir;
194 else {
195 assert(cso->type == PIPE_SHADER_IR_TGSI);
196
197 nir = tgsi_to_nir(cso->tokens, pctx->screen);
198 }
199
200 lima_program_optimize_fs_nir(nir);
201
202 if (lima_debug & LIMA_DEBUG_PP)
203 nir_print_shader(nir, stdout);
204
205 if (!ppir_compile_nir(so, nir, screen->pp_ra)) {
206 ralloc_free(so);
207 return NULL;
208 }
209
210 return so;
211 }
212
213 static void
214 lima_bind_fs_state(struct pipe_context *pctx, void *hwcso)
215 {
216 struct lima_context *ctx = lima_context(pctx);
217
218 ctx->fs = hwcso;
219 ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_FRAG;
220 }
221
222 static void
223 lima_delete_fs_state(struct pipe_context *pctx, void *hwcso)
224 {
225 struct lima_fs_shader_state *so = hwcso;
226
227 if (so->bo)
228 lima_bo_free(so->bo);
229
230 ralloc_free(so);
231 }
232
233 bool
234 lima_update_vs_state(struct lima_context *ctx)
235 {
236 struct lima_vs_shader_state *vs = ctx->vs;
237 if (!vs->bo) {
238 struct lima_screen *screen = lima_screen(ctx->base.screen);
239 vs->bo = lima_bo_create(screen, vs->shader_size, 0);
240 if (!vs->bo) {
241 fprintf(stderr, "lima: create vs shader bo fail\n");
242 return false;
243 }
244
245 memcpy(lima_bo_map(vs->bo), vs->shader, vs->shader_size);
246 ralloc_free(vs->shader);
247 vs->shader = NULL;
248 }
249
250 return true;
251 }
252
253 bool
254 lima_update_fs_state(struct lima_context *ctx)
255 {
256 struct lima_fs_shader_state *fs = ctx->fs;
257 if (!fs->bo) {
258 struct lima_screen *screen = lima_screen(ctx->base.screen);
259 fs->bo = lima_bo_create(screen, fs->shader_size, 0);
260 if (!fs->bo) {
261 fprintf(stderr, "lima: create fs shader bo fail\n");
262 return false;
263 }
264
265 memcpy(lima_bo_map(fs->bo), fs->shader, fs->shader_size);
266 ralloc_free(fs->shader);
267 fs->shader = NULL;
268 }
269
270 return true;
271 }
272
273 static void *
274 lima_create_vs_state(struct pipe_context *pctx,
275 const struct pipe_shader_state *cso)
276 {
277 struct lima_vs_shader_state *so = rzalloc(NULL, struct lima_vs_shader_state);
278
279 if (!so)
280 return NULL;
281
282 nir_shader *nir;
283 if (cso->type == PIPE_SHADER_IR_NIR)
284 nir = cso->ir.nir;
285 else {
286 assert(cso->type == PIPE_SHADER_IR_TGSI);
287
288 nir = tgsi_to_nir(cso->tokens, pctx->screen);
289 }
290
291 lima_program_optimize_vs_nir(nir);
292
293 if (lima_debug & LIMA_DEBUG_GP)
294 nir_print_shader(nir, stdout);
295
296 if (!gpir_compile_nir(so, nir)) {
297 ralloc_free(so);
298 return NULL;
299 }
300
301 return so;
302 }
303
304 static void
305 lima_bind_vs_state(struct pipe_context *pctx, void *hwcso)
306 {
307 struct lima_context *ctx = lima_context(pctx);
308
309 ctx->vs = hwcso;
310 ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_VERT;
311 }
312
313 static void
314 lima_delete_vs_state(struct pipe_context *pctx, void *hwcso)
315 {
316 struct lima_vs_shader_state *so = hwcso;
317
318 if (so->bo)
319 lima_bo_free(so->bo);
320
321 ralloc_free(so);
322 }
323
324 void
325 lima_program_init(struct lima_context *ctx)
326 {
327 ctx->base.create_fs_state = lima_create_fs_state;
328 ctx->base.bind_fs_state = lima_bind_fs_state;
329 ctx->base.delete_fs_state = lima_delete_fs_state;
330
331 ctx->base.create_vs_state = lima_create_vs_state;
332 ctx->base.bind_vs_state = lima_bind_vs_state;
333 ctx->base.delete_vs_state = lima_delete_vs_state;
334 }