2 * Copyright (c) 2017-2019 Lima Project
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:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
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.
25 #include "util/u_memory.h"
26 #include "util/ralloc.h"
27 #include "util/u_debug.h"
29 #include "tgsi/tgsi_dump.h"
30 #include "compiler/nir/nir.h"
31 #include "nir/tgsi_to_nir.h"
33 #include "pipe/p_state.h"
35 #include "lima_screen.h"
36 #include "lima_context.h"
37 #include "lima_program.h"
39 #include "ir/lima_ir.h"
41 static const nir_shader_compiler_options vs_nir_options
= {
52 /* could be implemented by clamp */
59 static const nir_shader_compiler_options fs_nir_options
= {
71 .lower_vector_cmp
= true,
74 static const struct nir_lower_tex_options tex_options
= {
79 lima_program_get_compiler_options(enum pipe_shader_type shader
)
82 case PIPE_SHADER_VERTEX
:
83 return &vs_nir_options
;
84 case PIPE_SHADER_FRAGMENT
:
85 return &fs_nir_options
;
92 type_size(const struct glsl_type
*type
, bool bindless
)
94 return glsl_count_attribute_slots(type
, false);
98 lima_program_optimize_vs_nir(struct nir_shader
*s
)
102 NIR_PASS_V(s
, nir_lower_viewport_transform
);
103 NIR_PASS_V(s
, nir_lower_io
, nir_var_all
, type_size
, 0);
104 NIR_PASS_V(s
, nir_lower_load_const_to_scalar
);
105 NIR_PASS_V(s
, lima_nir_lower_uniform_to_scalar
);
106 NIR_PASS_V(s
, nir_lower_io_to_scalar
,
107 nir_var_shader_in
|nir_var_shader_out
);
112 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
113 NIR_PASS(progress
, s
, nir_lower_alu_to_scalar
, NULL
, NULL
);
114 NIR_PASS(progress
, s
, nir_lower_phis_to_scalar
);
115 NIR_PASS(progress
, s
, nir_copy_prop
);
116 NIR_PASS(progress
, s
, nir_opt_remove_phis
);
117 NIR_PASS(progress
, s
, nir_opt_dce
);
118 NIR_PASS(progress
, s
, nir_opt_dead_cf
);
119 NIR_PASS(progress
, s
, nir_opt_cse
);
120 NIR_PASS(progress
, s
, nir_opt_peephole_select
, 8, true, true);
121 NIR_PASS(progress
, s
, nir_opt_algebraic
);
122 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
123 NIR_PASS(progress
, s
, nir_opt_undef
);
124 NIR_PASS(progress
, s
, nir_opt_loop_unroll
,
127 nir_var_function_temp
);
130 NIR_PASS_V(s
, nir_lower_int_to_float
);
131 NIR_PASS_V(s
, nir_lower_bool_to_float
);
133 /* Some ops must be lowered after being converted from int ops,
134 * so re-run nir_opt_algebraic after int lowering. */
137 NIR_PASS(progress
, s
, nir_opt_algebraic
);
140 NIR_PASS_V(s
, nir_copy_prop
);
141 NIR_PASS_V(s
, nir_opt_dce
);
142 NIR_PASS_V(s
, nir_lower_locals_to_regs
);
143 NIR_PASS_V(s
, nir_convert_from_ssa
, true);
144 NIR_PASS_V(s
, nir_remove_dead_variables
, nir_var_function_temp
);
149 lima_alu_to_scalar_filter_cb(const nir_instr
*instr
, const void *data
)
151 if (instr
->type
!= nir_instr_type_alu
)
154 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
163 /* nir vec4 fcsel assumes that each component of the condition will be
164 * used to select the same component from the two options, but lima
165 * can't implement that since we only have 1 component condition */
177 lima_program_optimize_fs_nir(struct nir_shader
*s
)
181 NIR_PASS_V(s
, nir_lower_fragcoord_wtrans
);
182 NIR_PASS_V(s
, nir_lower_io
, nir_var_all
, type_size
, 0);
183 NIR_PASS_V(s
, nir_lower_regs_to_ssa
);
184 NIR_PASS_V(s
, nir_lower_tex
, &tex_options
);
188 NIR_PASS(progress
, s
, nir_opt_vectorize
);
194 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
195 NIR_PASS(progress
, s
, nir_lower_alu_to_scalar
, lima_alu_to_scalar_filter_cb
, NULL
);
196 NIR_PASS(progress
, s
, nir_lower_phis_to_scalar
);
197 NIR_PASS(progress
, s
, nir_copy_prop
);
198 NIR_PASS(progress
, s
, nir_opt_remove_phis
);
199 NIR_PASS(progress
, s
, nir_opt_dce
);
200 NIR_PASS(progress
, s
, nir_opt_dead_cf
);
201 NIR_PASS(progress
, s
, nir_opt_cse
);
202 NIR_PASS(progress
, s
, nir_opt_peephole_select
, 8, true, true);
203 NIR_PASS(progress
, s
, nir_opt_algebraic
);
204 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
205 NIR_PASS(progress
, s
, nir_opt_undef
);
206 NIR_PASS(progress
, s
, nir_opt_loop_unroll
,
209 nir_var_function_temp
);
212 NIR_PASS_V(s
, nir_lower_int_to_float
);
213 NIR_PASS_V(s
, nir_lower_bool_to_float
);
215 /* Some ops must be lowered after being converted from int ops,
216 * so re-run nir_opt_algebraic after int lowering. */
219 NIR_PASS(progress
, s
, nir_opt_algebraic
);
222 /* Must be run after optimization loop */
223 NIR_PASS_V(s
, lima_nir_scale_trig
);
225 /* Lower modifiers */
226 NIR_PASS_V(s
, nir_lower_to_source_mods
, nir_lower_all_source_mods
);
227 NIR_PASS_V(s
, nir_copy_prop
);
228 NIR_PASS_V(s
, nir_opt_dce
);
230 NIR_PASS_V(s
, nir_lower_locals_to_regs
);
231 NIR_PASS_V(s
, nir_convert_from_ssa
, true);
232 NIR_PASS_V(s
, nir_remove_dead_variables
, nir_var_function_temp
);
234 NIR_PASS_V(s
, nir_move_vec_src_uses_to_dest
);
235 NIR_PASS_V(s
, nir_lower_vec_to_movs
);
241 lima_create_fs_state(struct pipe_context
*pctx
,
242 const struct pipe_shader_state
*cso
)
244 struct lima_context
*ctx
= lima_context(pctx
);
245 struct lima_screen
*screen
= lima_screen(pctx
->screen
);
246 struct lima_fs_shader_state
*so
= rzalloc(NULL
, struct lima_fs_shader_state
);
252 if (cso
->type
== PIPE_SHADER_IR_NIR
)
255 assert(cso
->type
== PIPE_SHADER_IR_TGSI
);
257 nir
= tgsi_to_nir(cso
->tokens
, pctx
->screen
);
260 lima_program_optimize_fs_nir(nir
);
262 if (lima_debug
& LIMA_DEBUG_PP
)
263 nir_print_shader(nir
, stdout
);
265 if (!ppir_compile_nir(so
, nir
, screen
->pp_ra
, &ctx
->debug
)) {
274 lima_bind_fs_state(struct pipe_context
*pctx
, void *hwcso
)
276 struct lima_context
*ctx
= lima_context(pctx
);
279 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_SHADER_FRAG
;
283 lima_delete_fs_state(struct pipe_context
*pctx
, void *hwcso
)
285 struct lima_fs_shader_state
*so
= hwcso
;
288 lima_bo_free(so
->bo
);
294 lima_update_vs_state(struct lima_context
*ctx
)
296 struct lima_vs_shader_state
*vs
= ctx
->vs
;
298 struct lima_screen
*screen
= lima_screen(ctx
->base
.screen
);
299 vs
->bo
= lima_bo_create(screen
, vs
->shader_size
, 0);
301 fprintf(stderr
, "lima: create vs shader bo fail\n");
305 memcpy(lima_bo_map(vs
->bo
), vs
->shader
, vs
->shader_size
);
306 ralloc_free(vs
->shader
);
314 lima_update_fs_state(struct lima_context
*ctx
)
316 struct lima_fs_shader_state
*fs
= ctx
->fs
;
318 struct lima_screen
*screen
= lima_screen(ctx
->base
.screen
);
319 fs
->bo
= lima_bo_create(screen
, fs
->shader_size
, 0);
321 fprintf(stderr
, "lima: create fs shader bo fail\n");
325 memcpy(lima_bo_map(fs
->bo
), fs
->shader
, fs
->shader_size
);
326 ralloc_free(fs
->shader
);
330 ctx
->pp_max_stack_size
= MAX2(ctx
->pp_max_stack_size
, ctx
->fs
->stack_size
);
336 lima_create_vs_state(struct pipe_context
*pctx
,
337 const struct pipe_shader_state
*cso
)
339 struct lima_context
*ctx
= lima_context(pctx
);
340 struct lima_vs_shader_state
*so
= rzalloc(NULL
, struct lima_vs_shader_state
);
346 if (cso
->type
== PIPE_SHADER_IR_NIR
)
349 assert(cso
->type
== PIPE_SHADER_IR_TGSI
);
351 nir
= tgsi_to_nir(cso
->tokens
, pctx
->screen
);
354 lima_program_optimize_vs_nir(nir
);
356 if (lima_debug
& LIMA_DEBUG_GP
)
357 nir_print_shader(nir
, stdout
);
359 if (!gpir_compile_nir(so
, nir
, &ctx
->debug
)) {
368 lima_bind_vs_state(struct pipe_context
*pctx
, void *hwcso
)
370 struct lima_context
*ctx
= lima_context(pctx
);
373 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_SHADER_VERT
;
377 lima_delete_vs_state(struct pipe_context
*pctx
, void *hwcso
)
379 struct lima_vs_shader_state
*so
= hwcso
;
382 lima_bo_free(so
->bo
);
388 lima_program_init(struct lima_context
*ctx
)
390 ctx
->base
.create_fs_state
= lima_create_fs_state
;
391 ctx
->base
.bind_fs_state
= lima_bind_fs_state
;
392 ctx
->base
.delete_fs_state
= lima_delete_fs_state
;
394 ctx
->base
.create_vs_state
= lima_create_vs_state
;
395 ctx
->base
.bind_vs_state
= lima_bind_vs_state
;
396 ctx
->base
.delete_vs_state
= lima_delete_vs_state
;