nir/build: add nir_vec() helper
[mesa.git] / src / glsl / nir / nir_lower_tex.c
1 /*
2 * Copyright © 2015 Broadcom
3 *
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, sublicense,
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:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
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 NONINFRINGEMENT. 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /*
25 * This lowering pass supports (as configured via nir_lower_tex_options)
26 * various texture related conversions:
27 * + texture projector lowering: converts the coordinate division for
28 * texture projection to be done in ALU instructions instead of
29 * asking the texture operation to do so.
30 * + lowering RECT: converts the un-normalized RECT texture coordinates
31 * to normalized coordinates with txs plus ALU instructions
32 * + saturate s/t/r coords: to emulate certain texture clamp/wrap modes,
33 * inserts instructions to clamp specified coordinates to [0.0, 1.0].
34 * Note that this automatically triggers texture projector lowering if
35 * needed, since clamping must happen after projector lowering.
36 */
37
38 #include "nir.h"
39 #include "nir_builder.h"
40
41 typedef struct {
42 nir_builder b;
43 const nir_lower_tex_options *options;
44 } lower_tex_state;
45
46 static void
47 project_src(nir_builder *b, nir_tex_instr *tex)
48 {
49 /* Find the projector in the srcs list, if present. */
50 unsigned proj_index;
51 for (proj_index = 0; proj_index < tex->num_srcs; proj_index++) {
52 if (tex->src[proj_index].src_type == nir_tex_src_projector)
53 break;
54 }
55 if (proj_index == tex->num_srcs)
56 return;
57
58 b->cursor = nir_before_instr(&tex->instr);
59
60 nir_ssa_def *inv_proj =
61 nir_frcp(b, nir_ssa_for_src(b, tex->src[proj_index].src, 1));
62
63 /* Walk through the sources projecting the arguments. */
64 for (unsigned i = 0; i < tex->num_srcs; i++) {
65 switch (tex->src[i].src_type) {
66 case nir_tex_src_coord:
67 case nir_tex_src_comparitor:
68 break;
69 default:
70 continue;
71 }
72 nir_ssa_def *unprojected =
73 nir_ssa_for_src(b, tex->src[i].src, nir_tex_instr_src_size(tex, i));
74 nir_ssa_def *projected = nir_fmul(b, unprojected, inv_proj);
75
76 /* Array indices don't get projected, so make an new vector with the
77 * coordinate's array index untouched.
78 */
79 if (tex->is_array && tex->src[i].src_type == nir_tex_src_coord) {
80 switch (tex->coord_components) {
81 case 4:
82 projected = nir_vec4(b,
83 nir_channel(b, projected, 0),
84 nir_channel(b, projected, 1),
85 nir_channel(b, projected, 2),
86 nir_channel(b, unprojected, 3));
87 break;
88 case 3:
89 projected = nir_vec3(b,
90 nir_channel(b, projected, 0),
91 nir_channel(b, projected, 1),
92 nir_channel(b, unprojected, 2));
93 break;
94 case 2:
95 projected = nir_vec2(b,
96 nir_channel(b, projected, 0),
97 nir_channel(b, unprojected, 1));
98 break;
99 default:
100 unreachable("bad texture coord count for array");
101 break;
102 }
103 }
104
105 nir_instr_rewrite_src(&tex->instr,
106 &tex->src[i].src,
107 nir_src_for_ssa(projected));
108 }
109
110 /* Now move the later tex sources down the array so that the projector
111 * disappears.
112 */
113 nir_instr_rewrite_src(&tex->instr, &tex->src[proj_index].src,
114 NIR_SRC_INIT);
115 for (unsigned i = proj_index + 1; i < tex->num_srcs; i++) {
116 tex->src[i-1].src_type = tex->src[i].src_type;
117 nir_instr_move_src(&tex->instr, &tex->src[i-1].src, &tex->src[i].src);
118 }
119 tex->num_srcs--;
120 }
121
122 static nir_ssa_def *
123 get_texture_size(nir_builder *b, nir_tex_instr *tex)
124 {
125 b->cursor = nir_before_instr(&tex->instr);
126
127 /* RECT textures should not be array: */
128 assert(!tex->is_array);
129
130 nir_tex_instr *txs;
131
132 txs = nir_tex_instr_create(b->shader, 1);
133 txs->op = nir_texop_txs;
134 txs->sampler_dim = GLSL_SAMPLER_DIM_RECT;
135 txs->sampler_index = tex->sampler_index;
136
137 /* only single src, the lod: */
138 txs->src[0].src = nir_src_for_ssa(nir_imm_int(b, 0));
139 txs->src[0].src_type = nir_tex_src_lod;
140
141 nir_ssa_dest_init(&txs->instr, &txs->dest, 2, NULL);
142 nir_builder_instr_insert(b, &txs->instr);
143
144 return nir_i2f(b, &txs->dest.ssa);
145 }
146
147 static void
148 lower_rect(nir_builder *b, nir_tex_instr *tex)
149 {
150 nir_ssa_def *txs = get_texture_size(b, tex);
151 nir_ssa_def *scale = nir_frcp(b, txs);
152
153 /* Walk through the sources normalizing the requested arguments. */
154 for (unsigned i = 0; i < tex->num_srcs; i++) {
155 if (tex->src[i].src_type != nir_tex_src_coord)
156 continue;
157
158 nir_ssa_def *coords =
159 nir_ssa_for_src(b, tex->src[i].src, tex->coord_components);
160 nir_instr_rewrite_src(&tex->instr,
161 &tex->src[i].src,
162 nir_src_for_ssa(nir_fmul(b, coords, scale)));
163 }
164
165 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
166 }
167
168 static void
169 saturate_src(nir_builder *b, nir_tex_instr *tex, unsigned sat_mask)
170 {
171 b->cursor = nir_before_instr(&tex->instr);
172
173 /* Walk through the sources saturating the requested arguments. */
174 for (unsigned i = 0; i < tex->num_srcs; i++) {
175 if (tex->src[i].src_type != nir_tex_src_coord)
176 continue;
177
178 nir_ssa_def *src =
179 nir_ssa_for_src(b, tex->src[i].src, tex->coord_components);
180
181 /* split src into components: */
182 nir_ssa_def *comp[4];
183
184 for (unsigned j = 0; j < tex->coord_components; j++)
185 comp[j] = nir_channel(b, src, j);
186
187 /* clamp requested components, array index does not get clamped: */
188 unsigned ncomp = tex->coord_components;
189 if (tex->is_array)
190 ncomp--;
191
192 for (unsigned j = 0; j < ncomp; j++) {
193 if ((1 << j) & sat_mask) {
194 if (tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) {
195 /* non-normalized texture coords, so clamp to texture
196 * size rather than [0.0, 1.0]
197 */
198 nir_ssa_def *txs = get_texture_size(b, tex);
199 comp[j] = nir_fmax(b, comp[j], nir_imm_float(b, 0.0));
200 comp[j] = nir_fmin(b, comp[j], nir_channel(b, txs, j));
201 } else {
202 comp[j] = nir_fsat(b, comp[j]);
203 }
204 }
205 }
206
207 /* and move the result back into a single vecN: */
208 src = nir_vec(b, comp, tex->coord_components);
209
210 nir_instr_rewrite_src(&tex->instr,
211 &tex->src[i].src,
212 nir_src_for_ssa(src));
213 }
214 }
215
216 static bool
217 nir_lower_tex_block(nir_block *block, void *void_state)
218 {
219 lower_tex_state *state = void_state;
220 nir_builder *b = &state->b;
221
222 nir_foreach_instr_safe(block, instr) {
223 if (instr->type != nir_instr_type_tex)
224 continue;
225
226 nir_tex_instr *tex = nir_instr_as_tex(instr);
227 bool lower_txp = !!(state->options->lower_txp & (1 << tex->sampler_dim));
228
229 /* mask of src coords to saturate (clamp): */
230 unsigned sat_mask = 0;
231
232 if ((1 << tex->sampler_index) & state->options->saturate_r)
233 sat_mask |= (1 << 2); /* .z */
234 if ((1 << tex->sampler_index) & state->options->saturate_t)
235 sat_mask |= (1 << 1); /* .y */
236 if ((1 << tex->sampler_index) & state->options->saturate_s)
237 sat_mask |= (1 << 0); /* .x */
238
239 /* If we are clamping any coords, we must lower projector first
240 * as clamping happens *after* projection:
241 */
242 if (lower_txp || sat_mask)
243 project_src(b, tex);
244
245 if ((tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) &&
246 state->options->lower_rect)
247 lower_rect(b, tex);
248
249 if (sat_mask)
250 saturate_src(b, tex, sat_mask);
251 }
252
253 return true;
254 }
255
256 static void
257 nir_lower_tex_impl(nir_function_impl *impl, lower_tex_state *state)
258 {
259 nir_builder_init(&state->b, impl);
260
261 nir_foreach_block(impl, nir_lower_tex_block, state);
262
263 nir_metadata_preserve(impl, nir_metadata_block_index |
264 nir_metadata_dominance);
265 }
266
267 void
268 nir_lower_tex(nir_shader *shader, const nir_lower_tex_options *options)
269 {
270 lower_tex_state state;
271 state.options = options;
272 nir_foreach_overload(shader, overload) {
273 if (overload->impl)
274 nir_lower_tex_impl(overload->impl, &state);
275 }
276 }