Merge remote-tracking branch 'public/master' into vulkan
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_nir.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
5 *
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:
12 *
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
15 * Software.
16 *
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
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29
30 #include "freedreno_util.h"
31
32 #include "ir3_nir.h"
33 #include "ir3_compiler.h"
34 #include "ir3_shader.h"
35
36 #include "nir/tgsi_to_nir.h"
37
38 struct nir_shader *
39 ir3_tgsi_to_nir(const struct tgsi_token *tokens)
40 {
41 static const nir_shader_compiler_options options = {
42 .lower_fpow = true,
43 .lower_fsat = true,
44 .lower_scmp = true,
45 .lower_flrp = true,
46 .lower_ffract = true,
47 .native_integers = true,
48 .lower_extract_byte = true,
49 .lower_extract_word = true,
50 };
51 return tgsi_to_nir(tokens, &options);
52 }
53
54 /* for given shader key, are any steps handled in nir? */
55 bool
56 ir3_key_lowers_nir(const struct ir3_shader_key *key)
57 {
58 return key->fsaturate_s | key->fsaturate_t | key->fsaturate_r |
59 key->vsaturate_s | key->vsaturate_t | key->vsaturate_r |
60 key->ucp_enables | key->color_two_side;
61 }
62
63 #define OPT(nir, pass, ...) ({ \
64 bool this_progress = false; \
65 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
66 this_progress; \
67 })
68
69 #define OPT_V(nir, pass, ...) NIR_PASS_V(nir, pass, ##__VA_ARGS__)
70
71 struct nir_shader *
72 ir3_optimize_nir(struct ir3_shader *shader, nir_shader *s,
73 const struct ir3_shader_key *key)
74 {
75 struct nir_lower_tex_options tex_options = {
76 .lower_rect = 0,
77 };
78 bool progress;
79
80 if (key) {
81 switch (shader->type) {
82 case SHADER_FRAGMENT:
83 case SHADER_COMPUTE:
84 tex_options.saturate_s = key->fsaturate_s;
85 tex_options.saturate_t = key->fsaturate_t;
86 tex_options.saturate_r = key->fsaturate_r;
87 break;
88 case SHADER_VERTEX:
89 tex_options.saturate_s = key->vsaturate_s;
90 tex_options.saturate_t = key->vsaturate_t;
91 tex_options.saturate_r = key->vsaturate_r;
92 break;
93 }
94 }
95
96 if (shader->compiler->gpu_id >= 400) {
97 /* a4xx seems to have *no* sam.p */
98 tex_options.lower_txp = ~0; /* lower all txp */
99 } else {
100 /* a3xx just needs to avoid sam.p for 3d tex */
101 tex_options.lower_txp = (1 << GLSL_SAMPLER_DIM_3D);
102 }
103
104 if (fd_mesa_debug & FD_DBG_DISASM) {
105 debug_printf("----------------------\n");
106 nir_print_shader(s, stdout);
107 debug_printf("----------------------\n");
108 }
109
110 OPT_V(s, nir_opt_global_to_local);
111 OPT_V(s, nir_convert_to_ssa);
112
113 if (key) {
114 if (s->stage == MESA_SHADER_VERTEX) {
115 OPT_V(s, nir_lower_clip_vs, key->ucp_enables);
116 } else if (s->stage == MESA_SHADER_FRAGMENT) {
117 OPT_V(s, nir_lower_clip_fs, key->ucp_enables);
118 }
119 if (key->color_two_side) {
120 OPT_V(s, nir_lower_two_sided_color);
121 }
122 }
123
124 OPT_V(s, nir_lower_tex, &tex_options);
125 OPT_V(s, nir_lower_idiv);
126 OPT_V(s, nir_lower_load_const_to_scalar);
127
128 do {
129 progress = false;
130
131 OPT_V(s, nir_lower_vars_to_ssa);
132 OPT_V(s, nir_lower_alu_to_scalar);
133 OPT_V(s, nir_lower_phis_to_scalar);
134
135 progress |= OPT(s, nir_copy_prop);
136 progress |= OPT(s, nir_opt_dce);
137 progress |= OPT(s, nir_opt_cse);
138 progress |= OPT(s, ir3_nir_lower_if_else);
139 progress |= OPT(s, nir_opt_algebraic);
140 progress |= OPT(s, nir_opt_constant_folding);
141
142 } while (progress);
143
144 OPT_V(s, nir_remove_dead_variables);
145
146 if (fd_mesa_debug & FD_DBG_DISASM) {
147 debug_printf("----------------------\n");
148 nir_print_shader(s, stdout);
149 debug_printf("----------------------\n");
150 }
151
152 nir_sweep(s);
153
154 return s;
155 }