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