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
= {
73 static const struct nir_lower_tex_options tex_options
= {
78 lima_program_get_compiler_options(enum pipe_shader_type shader
)
81 case PIPE_SHADER_VERTEX
:
82 return &vs_nir_options
;
83 case PIPE_SHADER_FRAGMENT
:
84 return &fs_nir_options
;
91 type_size(const struct glsl_type
*type
, bool bindless
)
93 return glsl_count_attribute_slots(type
, false);
97 lima_program_optimize_vs_nir(struct nir_shader
*s
)
101 NIR_PASS_V(s
, nir_lower_viewport_transform
);
102 NIR_PASS_V(s
, nir_lower_io
, nir_var_all
, type_size
, 0);
103 NIR_PASS_V(s
, nir_lower_load_const_to_scalar
);
104 NIR_PASS_V(s
, lima_nir_lower_uniform_to_scalar
);
105 NIR_PASS_V(s
, nir_lower_io_to_scalar
,
106 nir_var_shader_in
|nir_var_shader_out
);
111 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
112 NIR_PASS(progress
, s
, nir_lower_alu_to_scalar
, NULL
);
113 NIR_PASS(progress
, s
, nir_lower_phis_to_scalar
);
114 NIR_PASS(progress
, s
, nir_copy_prop
);
115 NIR_PASS(progress
, s
, nir_opt_remove_phis
);
116 NIR_PASS(progress
, s
, nir_opt_dce
);
117 NIR_PASS(progress
, s
, nir_opt_dead_cf
);
118 NIR_PASS(progress
, s
, nir_opt_cse
);
119 NIR_PASS(progress
, s
, nir_opt_peephole_select
, 8, true, true);
120 NIR_PASS(progress
, s
, nir_opt_algebraic
);
121 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
122 NIR_PASS(progress
, s
, nir_opt_undef
);
123 NIR_PASS(progress
, s
, nir_opt_loop_unroll
,
126 nir_var_function_temp
);
129 NIR_PASS_V(s
, nir_lower_int_to_float
);
130 NIR_PASS_V(s
, nir_lower_bool_to_float
);
131 NIR_PASS_V(s
, nir_copy_prop
);
132 NIR_PASS_V(s
, nir_opt_dce
);
133 NIR_PASS_V(s
, nir_lower_locals_to_regs
);
134 NIR_PASS_V(s
, nir_convert_from_ssa
, true);
135 NIR_PASS_V(s
, nir_remove_dead_variables
, nir_var_function_temp
);
140 lima_program_optimize_fs_nir(struct nir_shader
*s
)
142 BITSET_DECLARE(alu_lower
, nir_num_opcodes
) = {0};
145 BITSET_SET(alu_lower
, nir_op_frcp
);
146 BITSET_SET(alu_lower
, nir_op_frsq
);
147 BITSET_SET(alu_lower
, nir_op_flog2
);
148 BITSET_SET(alu_lower
, nir_op_fexp2
);
149 BITSET_SET(alu_lower
, nir_op_fsqrt
);
150 BITSET_SET(alu_lower
, nir_op_fsin
);
151 BITSET_SET(alu_lower
, nir_op_fcos
);
153 NIR_PASS_V(s
, nir_lower_fragcoord_wtrans
);
154 NIR_PASS_V(s
, nir_lower_io
, nir_var_all
, type_size
, 0);
155 NIR_PASS_V(s
, nir_lower_regs_to_ssa
);
156 NIR_PASS_V(s
, nir_lower_tex
, &tex_options
);
161 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
162 NIR_PASS(progress
, s
, nir_lower_alu_to_scalar
, alu_lower
);
163 NIR_PASS(progress
, s
, nir_lower_phis_to_scalar
);
164 NIR_PASS(progress
, s
, nir_copy_prop
);
165 NIR_PASS(progress
, s
, nir_opt_remove_phis
);
166 NIR_PASS(progress
, s
, nir_opt_dce
);
167 NIR_PASS(progress
, s
, nir_opt_dead_cf
);
168 NIR_PASS(progress
, s
, nir_opt_cse
);
169 NIR_PASS(progress
, s
, nir_opt_peephole_select
, 8, true, true);
170 NIR_PASS(progress
, s
, nir_opt_algebraic
);
171 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
172 NIR_PASS(progress
, s
, nir_opt_undef
);
173 NIR_PASS(progress
, s
, nir_opt_loop_unroll
,
176 nir_var_function_temp
);
179 NIR_PASS_V(s
, nir_lower_int_to_float
);
180 NIR_PASS_V(s
, nir_lower_bool_to_float
);
182 /* Lower modifiers */
183 NIR_PASS_V(s
, nir_lower_to_source_mods
, nir_lower_all_source_mods
);
184 NIR_PASS_V(s
, nir_copy_prop
);
185 NIR_PASS_V(s
, nir_opt_dce
);
187 NIR_PASS_V(s
, nir_lower_locals_to_regs
);
188 NIR_PASS_V(s
, nir_convert_from_ssa
, true);
189 NIR_PASS_V(s
, nir_remove_dead_variables
, nir_var_function_temp
);
191 NIR_PASS_V(s
, nir_move_vec_src_uses_to_dest
);
192 NIR_PASS_V(s
, nir_lower_vec_to_movs
);
198 lima_create_fs_state(struct pipe_context
*pctx
,
199 const struct pipe_shader_state
*cso
)
201 struct lima_screen
*screen
= lima_screen(pctx
->screen
);
202 struct lima_fs_shader_state
*so
= rzalloc(NULL
, struct lima_fs_shader_state
);
208 if (cso
->type
== PIPE_SHADER_IR_NIR
)
211 assert(cso
->type
== PIPE_SHADER_IR_TGSI
);
213 nir
= tgsi_to_nir(cso
->tokens
, pctx
->screen
);
216 lima_program_optimize_fs_nir(nir
);
218 if (lima_debug
& LIMA_DEBUG_PP
)
219 nir_print_shader(nir
, stdout
);
221 if (!ppir_compile_nir(so
, nir
, screen
->pp_ra
)) {
230 lima_bind_fs_state(struct pipe_context
*pctx
, void *hwcso
)
232 struct lima_context
*ctx
= lima_context(pctx
);
235 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_SHADER_FRAG
;
239 lima_delete_fs_state(struct pipe_context
*pctx
, void *hwcso
)
241 struct lima_fs_shader_state
*so
= hwcso
;
244 lima_bo_free(so
->bo
);
250 lima_update_vs_state(struct lima_context
*ctx
)
252 struct lima_vs_shader_state
*vs
= ctx
->vs
;
254 struct lima_screen
*screen
= lima_screen(ctx
->base
.screen
);
255 vs
->bo
= lima_bo_create(screen
, vs
->shader_size
, 0);
257 fprintf(stderr
, "lima: create vs shader bo fail\n");
261 memcpy(lima_bo_map(vs
->bo
), vs
->shader
, vs
->shader_size
);
262 ralloc_free(vs
->shader
);
270 lima_update_fs_state(struct lima_context
*ctx
)
272 struct lima_fs_shader_state
*fs
= ctx
->fs
;
274 struct lima_screen
*screen
= lima_screen(ctx
->base
.screen
);
275 fs
->bo
= lima_bo_create(screen
, fs
->shader_size
, 0);
277 fprintf(stderr
, "lima: create fs shader bo fail\n");
281 memcpy(lima_bo_map(fs
->bo
), fs
->shader
, fs
->shader_size
);
282 ralloc_free(fs
->shader
);
290 lima_create_vs_state(struct pipe_context
*pctx
,
291 const struct pipe_shader_state
*cso
)
293 struct lima_vs_shader_state
*so
= rzalloc(NULL
, struct lima_vs_shader_state
);
299 if (cso
->type
== PIPE_SHADER_IR_NIR
)
302 assert(cso
->type
== PIPE_SHADER_IR_TGSI
);
304 nir
= tgsi_to_nir(cso
->tokens
, pctx
->screen
);
307 lima_program_optimize_vs_nir(nir
);
309 if (lima_debug
& LIMA_DEBUG_GP
)
310 nir_print_shader(nir
, stdout
);
312 if (!gpir_compile_nir(so
, nir
)) {
321 lima_bind_vs_state(struct pipe_context
*pctx
, void *hwcso
)
323 struct lima_context
*ctx
= lima_context(pctx
);
326 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_SHADER_VERT
;
330 lima_delete_vs_state(struct pipe_context
*pctx
, void *hwcso
)
332 struct lima_vs_shader_state
*so
= hwcso
;
335 lima_bo_free(so
->bo
);
341 lima_program_init(struct lima_context
*ctx
)
343 ctx
->base
.create_fs_state
= lima_create_fs_state
;
344 ctx
->base
.bind_fs_state
= lima_bind_fs_state
;
345 ctx
->base
.delete_fs_state
= lima_delete_fs_state
;
347 ctx
->base
.create_vs_state
= lima_create_vs_state
;
348 ctx
->base
.bind_vs_state
= lima_bind_vs_state
;
349 ctx
->base
.delete_vs_state
= lima_delete_vs_state
;