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