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"
38 #include "lima_program.h"
40 #include "lima_format.h"
42 #include "ir/lima_ir.h"
44 static const nir_shader_compiler_options vs_nir_options
= {
54 /* could be implemented by clamp */
62 static const nir_shader_compiler_options fs_nir_options
= {
75 .lower_vector_cmp
= true,
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_point_size
, 1.0f
, 100.0f
);
104 NIR_PASS_V(s
, nir_lower_io
, nir_var_all
, type_size
, 0);
105 NIR_PASS_V(s
, nir_lower_load_const_to_scalar
);
106 NIR_PASS_V(s
, lima_nir_lower_uniform_to_scalar
);
107 NIR_PASS_V(s
, nir_lower_io_to_scalar
,
108 nir_var_shader_in
|nir_var_shader_out
);
113 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
114 NIR_PASS(progress
, s
, nir_lower_alu_to_scalar
, NULL
, NULL
);
115 NIR_PASS(progress
, s
, nir_lower_phis_to_scalar
);
116 NIR_PASS(progress
, s
, nir_copy_prop
);
117 NIR_PASS(progress
, s
, nir_opt_remove_phis
);
118 NIR_PASS(progress
, s
, nir_opt_dce
);
119 NIR_PASS(progress
, s
, nir_opt_dead_cf
);
120 NIR_PASS(progress
, s
, nir_opt_cse
);
121 NIR_PASS(progress
, s
, nir_opt_peephole_select
, 8, true, true);
122 NIR_PASS(progress
, s
, nir_opt_algebraic
);
123 NIR_PASS(progress
, s
, lima_nir_lower_ftrunc
);
124 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
125 NIR_PASS(progress
, s
, nir_opt_undef
);
126 NIR_PASS(progress
, s
, nir_opt_loop_unroll
,
129 nir_var_function_temp
);
132 NIR_PASS_V(s
, nir_lower_int_to_float
);
133 /* int_to_float pass generates ftrunc, so lower it */
134 NIR_PASS(progress
, s
, lima_nir_lower_ftrunc
);
135 NIR_PASS_V(s
, nir_lower_bool_to_float
);
137 NIR_PASS_V(s
, nir_copy_prop
);
138 NIR_PASS_V(s
, nir_opt_dce
);
139 NIR_PASS_V(s
, nir_lower_locals_to_regs
);
140 NIR_PASS_V(s
, nir_convert_from_ssa
, true);
141 NIR_PASS_V(s
, nir_remove_dead_variables
, nir_var_function_temp
);
146 lima_alu_to_scalar_filter_cb(const nir_instr
*instr
, const void *data
)
148 if (instr
->type
!= nir_instr_type_alu
)
151 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
165 /* nir vec4 fcsel assumes that each component of the condition will be
166 * used to select the same component from the two options, but Utgard PP
167 * has only 1 component condition. If all condition components are not the
168 * same we need to lower it to scalar.
178 int num_components
= nir_dest_num_components(alu
->dest
.dest
);
180 uint8_t swizzle
= alu
->src
[0].swizzle
[0];
182 for (int i
= 1; i
< num_components
; i
++)
183 if (alu
->src
[0].swizzle
[i
] != swizzle
)
190 lima_program_optimize_fs_nir(struct nir_shader
*s
,
191 struct nir_lower_tex_options
*tex_options
)
195 NIR_PASS_V(s
, nir_lower_fragcoord_wtrans
);
196 NIR_PASS_V(s
, nir_lower_io
, nir_var_all
, type_size
, 0);
197 NIR_PASS_V(s
, nir_lower_regs_to_ssa
);
198 NIR_PASS_V(s
, nir_lower_tex
, tex_options
);
202 NIR_PASS(progress
, s
, nir_opt_vectorize
);
208 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
209 NIR_PASS(progress
, s
, nir_lower_alu_to_scalar
, lima_alu_to_scalar_filter_cb
, NULL
);
210 NIR_PASS(progress
, s
, nir_copy_prop
);
211 NIR_PASS(progress
, s
, nir_opt_remove_phis
);
212 NIR_PASS(progress
, s
, nir_opt_dce
);
213 NIR_PASS(progress
, s
, nir_opt_dead_cf
);
214 NIR_PASS(progress
, s
, nir_opt_cse
);
215 NIR_PASS(progress
, s
, nir_opt_peephole_select
, 8, true, true);
216 NIR_PASS(progress
, s
, nir_opt_algebraic
);
217 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
218 NIR_PASS(progress
, s
, nir_opt_undef
);
219 NIR_PASS(progress
, s
, nir_opt_loop_unroll
,
222 nir_var_function_temp
);
223 NIR_PASS(progress
, s
, lima_nir_split_load_input
);
226 NIR_PASS_V(s
, nir_lower_int_to_float
);
227 NIR_PASS_V(s
, nir_lower_bool_to_float
);
229 /* Some ops must be lowered after being converted from int ops,
230 * so re-run nir_opt_algebraic after int lowering. */
233 NIR_PASS(progress
, s
, nir_opt_algebraic
);
236 /* Must be run after optimization loop */
237 NIR_PASS_V(s
, lima_nir_scale_trig
);
239 /* Lower modifiers */
240 NIR_PASS_V(s
, nir_lower_to_source_mods
, nir_lower_all_source_mods
);
241 NIR_PASS_V(s
, nir_copy_prop
);
242 NIR_PASS_V(s
, nir_opt_dce
);
244 NIR_PASS_V(s
, nir_lower_locals_to_regs
);
245 NIR_PASS_V(s
, nir_convert_from_ssa
, true);
246 NIR_PASS_V(s
, nir_remove_dead_variables
, nir_var_function_temp
);
248 NIR_PASS_V(s
, nir_move_vec_src_uses_to_dest
);
249 NIR_PASS_V(s
, nir_lower_vec_to_movs
);
251 NIR_PASS_V(s
, lima_nir_duplicate_load_uniforms
);
252 NIR_PASS_V(s
, lima_nir_duplicate_load_inputs
);
253 NIR_PASS_V(s
, lima_nir_duplicate_load_consts
);
259 lima_fs_compile_shader(struct lima_context
*ctx
,
260 struct lima_fs_shader_state
*fs
,
261 struct nir_lower_tex_options
*tex_options
)
263 struct lima_screen
*screen
= lima_screen(ctx
->base
.screen
);
264 nir_shader
*nir
= nir_shader_clone(fs
, fs
->base
.ir
.nir
);
266 lima_program_optimize_fs_nir(nir
, tex_options
);
268 if (lima_debug
& LIMA_DEBUG_PP
)
269 nir_print_shader(nir
, stdout
);
271 if (!ppir_compile_nir(fs
, nir
, screen
->pp_ra
, &ctx
->debug
)) {
276 fs
->uses_discard
= nir
->info
.fs
.uses_discard
;
283 lima_create_fs_state(struct pipe_context
*pctx
,
284 const struct pipe_shader_state
*cso
)
286 struct lima_context
*ctx
= lima_context(pctx
);
287 struct lima_fs_shader_state
*so
= rzalloc(NULL
, struct lima_fs_shader_state
);
293 if (cso
->type
== PIPE_SHADER_IR_NIR
)
294 /* The backend takes ownership of the NIR shader on state
299 assert(cso
->type
== PIPE_SHADER_IR_TGSI
);
301 nir
= tgsi_to_nir(cso
->tokens
, pctx
->screen
, false);
304 so
->base
.type
= PIPE_SHADER_IR_NIR
;
305 so
->base
.ir
.nir
= nir
;
307 uint8_t identity
[4] = { PIPE_SWIZZLE_X
,
312 struct nir_lower_tex_options tex_options
= {
317 /* Initialize with identity swizzles. That should suffice for most shaders */
318 for (int i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++)
319 memcpy(so
->swizzles
[i
], identity
, 4);
321 if (!lima_fs_compile_shader(ctx
, so
, &tex_options
)) {
330 lima_bind_fs_state(struct pipe_context
*pctx
, void *hwcso
)
332 struct lima_context
*ctx
= lima_context(pctx
);
335 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_SHADER_FRAG
;
339 lima_delete_fs_state(struct pipe_context
*pctx
, void *hwcso
)
341 struct lima_fs_shader_state
*so
= hwcso
;
344 lima_bo_unreference(so
->bo
);
346 ralloc_free(so
->base
.ir
.nir
);
351 lima_update_vs_state(struct lima_context
*ctx
)
353 struct lima_vs_shader_state
*vs
= ctx
->vs
;
355 struct lima_screen
*screen
= lima_screen(ctx
->base
.screen
);
356 vs
->bo
= lima_bo_create(screen
, vs
->shader_size
, 0);
358 fprintf(stderr
, "lima: create vs shader bo fail\n");
362 memcpy(lima_bo_map(vs
->bo
), vs
->shader
, vs
->shader_size
);
363 ralloc_free(vs
->shader
);
371 lima_update_fs_state(struct lima_context
*ctx
)
373 struct lima_fs_shader_state
*fs
= ctx
->fs
;
374 struct lima_texture_stateobj
*lima_tex
= &ctx
->tex_stateobj
;
375 struct nir_lower_tex_options tex_options
= {
379 bool needs_recompile
= false;
381 /* Check if texture formats has changed since last compilation.
382 * If it has we need to recompile shader.
384 if (((ctx
->dirty
& LIMA_CONTEXT_DIRTY_TEXTURES
) &&
385 lima_tex
->num_samplers
&&
386 lima_tex
->num_textures
)) {
387 uint8_t identity
[4] = { PIPE_SWIZZLE_X
,
391 for (int i
= 0; i
< lima_tex
->num_samplers
; i
++) {
392 struct lima_sampler_view
*texture
= lima_sampler_view(lima_tex
->textures
[i
]);
393 struct pipe_resource
*prsc
= texture
->base
.texture
;
394 const uint8_t *swizzle
= lima_format_get_texel_swizzle(prsc
->format
);
395 if (memcmp(fs
->swizzles
[i
], swizzle
, 4)) {
396 needs_recompile
= true;
397 memcpy(fs
->swizzles
[i
], swizzle
, 4);
400 for (int j
= 0; j
< 4; j
++)
401 tex_options
.swizzles
[i
][j
] = swizzle
[j
];
403 if (memcmp(swizzle
, identity
, 4))
404 tex_options
.swizzle_result
|= (1 << i
);
407 /* Fill rest with identity swizzle */
408 for (int i
= lima_tex
->num_samplers
; i
< PIPE_MAX_SAMPLERS
; i
++)
409 memcpy(fs
->swizzles
[i
], identity
, 4);
412 if (needs_recompile
) {
414 lima_bo_unreference(fs
->bo
);
418 if (!lima_fs_compile_shader(ctx
, fs
, &tex_options
))
423 struct lima_screen
*screen
= lima_screen(ctx
->base
.screen
);
424 fs
->bo
= lima_bo_create(screen
, fs
->shader_size
, 0);
426 fprintf(stderr
, "lima: create fs shader bo fail\n");
430 memcpy(lima_bo_map(fs
->bo
), fs
->shader
, fs
->shader_size
);
431 ralloc_free(fs
->shader
);
435 struct lima_job
*job
= lima_job_get(ctx
);
436 job
->pp_max_stack_size
= MAX2(job
->pp_max_stack_size
, ctx
->fs
->stack_size
);
442 lima_create_vs_state(struct pipe_context
*pctx
,
443 const struct pipe_shader_state
*cso
)
445 struct lima_context
*ctx
= lima_context(pctx
);
446 struct lima_vs_shader_state
*so
= rzalloc(NULL
, struct lima_vs_shader_state
);
452 if (cso
->type
== PIPE_SHADER_IR_NIR
)
455 assert(cso
->type
== PIPE_SHADER_IR_TGSI
);
457 nir
= tgsi_to_nir(cso
->tokens
, pctx
->screen
, false);
460 lima_program_optimize_vs_nir(nir
);
462 if (lima_debug
& LIMA_DEBUG_GP
)
463 nir_print_shader(nir
, stdout
);
465 if (!gpir_compile_nir(so
, nir
, &ctx
->debug
)) {
476 lima_bind_vs_state(struct pipe_context
*pctx
, void *hwcso
)
478 struct lima_context
*ctx
= lima_context(pctx
);
481 ctx
->dirty
|= LIMA_CONTEXT_DIRTY_SHADER_VERT
;
485 lima_delete_vs_state(struct pipe_context
*pctx
, void *hwcso
)
487 struct lima_vs_shader_state
*so
= hwcso
;
490 lima_bo_unreference(so
->bo
);
496 lima_program_init(struct lima_context
*ctx
)
498 ctx
->base
.create_fs_state
= lima_create_fs_state
;
499 ctx
->base
.bind_fs_state
= lima_bind_fs_state
;
500 ctx
->base
.delete_fs_state
= lima_delete_fs_state
;
502 ctx
->base
.create_vs_state
= lima_create_vs_state
;
503 ctx
->base
.bind_vs_state
= lima_bind_vs_state
;
504 ctx
->base
.delete_vs_state
= lima_delete_vs_state
;