1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
30 #include "freedreno_util.h"
33 #include "ir3_compiler.h"
34 #include "ir3_shader.h"
36 #include "nir/tgsi_to_nir.h"
39 ir3_tgsi_to_nir(const struct tgsi_token
*tokens
)
41 static const nir_shader_compiler_options options
= {
49 .native_integers
= true,
50 .vertex_id_zero_based
= true,
51 .lower_extract_byte
= true,
52 .lower_extract_word
= true,
54 return tgsi_to_nir(tokens
, &options
);
57 /* for given shader key, are any steps handled in nir? */
59 ir3_key_lowers_nir(const struct ir3_shader_key
*key
)
61 return key
->fsaturate_s
| key
->fsaturate_t
| key
->fsaturate_r
|
62 key
->vsaturate_s
| key
->vsaturate_t
| key
->vsaturate_r
|
63 key
->ucp_enables
| key
->color_two_side
|
64 key
->fclamp_color
| key
->vclamp_color
;
67 #define OPT(nir, pass, ...) ({ \
68 bool this_progress = false; \
69 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
73 #define OPT_V(nir, pass, ...) NIR_PASS_V(nir, pass, ##__VA_ARGS__)
76 ir3_optimize_nir(struct ir3_shader
*shader
, nir_shader
*s
,
77 const struct ir3_shader_key
*key
)
79 struct nir_lower_tex_options tex_options
= {
85 switch (shader
->type
) {
88 tex_options
.saturate_s
= key
->fsaturate_s
;
89 tex_options
.saturate_t
= key
->fsaturate_t
;
90 tex_options
.saturate_r
= key
->fsaturate_r
;
93 tex_options
.saturate_s
= key
->vsaturate_s
;
94 tex_options
.saturate_t
= key
->vsaturate_t
;
95 tex_options
.saturate_r
= key
->vsaturate_r
;
100 if (shader
->compiler
->gpu_id
>= 400) {
101 /* a4xx seems to have *no* sam.p */
102 tex_options
.lower_txp
= ~0; /* lower all txp */
104 /* a3xx just needs to avoid sam.p for 3d tex */
105 tex_options
.lower_txp
= (1 << GLSL_SAMPLER_DIM_3D
);
108 if (fd_mesa_debug
& FD_DBG_DISASM
) {
109 debug_printf("----------------------\n");
110 nir_print_shader(s
, stdout
);
111 debug_printf("----------------------\n");
114 OPT_V(s
, nir_opt_global_to_local
);
115 OPT_V(s
, nir_convert_to_ssa
);
118 if (s
->stage
== MESA_SHADER_VERTEX
) {
119 OPT_V(s
, nir_lower_clip_vs
, key
->ucp_enables
);
120 if (key
->vclamp_color
)
121 OPT_V(s
, nir_lower_clamp_color_outputs
);
122 } else if (s
->stage
== MESA_SHADER_FRAGMENT
) {
123 OPT_V(s
, nir_lower_clip_fs
, key
->ucp_enables
);
124 if (key
->fclamp_color
)
125 OPT_V(s
, nir_lower_clamp_color_outputs
);
127 if (key
->color_two_side
) {
128 OPT_V(s
, nir_lower_two_sided_color
);
131 /* only want to do this the first time (when key is null)
132 * and not again on any potential 2nd variant lowering pass:
134 OPT_V(s
, ir3_nir_apply_trig_workarounds
);
137 OPT_V(s
, nir_lower_tex
, &tex_options
);
138 OPT_V(s
, nir_lower_idiv
);
139 OPT_V(s
, nir_lower_load_const_to_scalar
);
144 OPT_V(s
, nir_lower_vars_to_ssa
);
145 OPT_V(s
, nir_lower_alu_to_scalar
);
146 OPT_V(s
, nir_lower_phis_to_scalar
);
148 progress
|= OPT(s
, nir_copy_prop
);
149 progress
|= OPT(s
, nir_opt_dce
);
150 progress
|= OPT(s
, nir_opt_cse
);
151 progress
|= OPT(s
, ir3_nir_lower_if_else
);
152 progress
|= OPT(s
, nir_opt_algebraic
);
153 progress
|= OPT(s
, nir_opt_constant_folding
);
157 OPT_V(s
, nir_remove_dead_variables
, nir_var_local
);
159 if (fd_mesa_debug
& FD_DBG_DISASM
) {
160 debug_printf("----------------------\n");
161 nir_print_shader(s
, stdout
);
162 debug_printf("----------------------\n");