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