72a514756fdaa05cd60a44cb210a0f097100d38e
[mesa.git] / src / gallium / drivers / vc4 / vc4_nir_lower_io.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 #include "vc4_qir.h"
25 #include "glsl/nir/nir_builder.h"
26 #include "util/u_format.h"
27
28 /**
29 * Walks the NIR generated by TGSI-to-NIR to lower its io intrinsics into
30 * something amenable to the VC4 architecture.
31 *
32 * Currently, it split inputs, outputs, and uniforms into scalars, drops any
33 * non-position outputs in coordinate shaders, and fixes up the addressing on
34 * indirect uniform loads.
35 */
36
37 static void
38 replace_intrinsic_with_vec4(nir_builder *b, nir_intrinsic_instr *intr,
39 nir_ssa_def **comps)
40 {
41
42 /* Batch things back together into a vec4. This will get split by the
43 * later ALU scalarization pass.
44 */
45 nir_ssa_def *vec = nir_vec4(b, comps[0], comps[1], comps[2], comps[3]);
46
47 /* Replace the old intrinsic with a reference to our reconstructed
48 * vec4.
49 */
50 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(vec));
51 nir_instr_remove(&intr->instr);
52 }
53
54 static nir_ssa_def *
55 vc4_nir_unpack_8i(nir_builder *b, nir_ssa_def *src, unsigned chan)
56 {
57 return nir_ubitfield_extract(b,
58 src,
59 nir_imm_int(b, 8 * chan),
60 nir_imm_int(b, 8));
61 }
62
63 /** Returns the 16 bit field as a sign-extended 32-bit value. */
64 static nir_ssa_def *
65 vc4_nir_unpack_16i(nir_builder *b, nir_ssa_def *src, unsigned chan)
66 {
67 return nir_ibitfield_extract(b,
68 src,
69 nir_imm_int(b, 16 * chan),
70 nir_imm_int(b, 16));
71 }
72
73 /** Returns the 16 bit field as an unsigned 32 bit value. */
74 static nir_ssa_def *
75 vc4_nir_unpack_16u(nir_builder *b, nir_ssa_def *src, unsigned chan)
76 {
77 if (chan == 0) {
78 return nir_iand(b, src, nir_imm_int(b, 0xffff));
79 } else {
80 return nir_ushr(b, src, nir_imm_int(b, 16));
81 }
82 }
83
84 static nir_ssa_def *
85 vc4_nir_unpack_8f(nir_builder *b, nir_ssa_def *src, unsigned chan)
86 {
87 return nir_channel(b, nir_unpack_unorm_4x8(b, src), chan);
88 }
89
90 static nir_ssa_def *
91 vc4_nir_get_vattr_channel_vpm(struct vc4_compile *c,
92 nir_builder *b,
93 nir_ssa_def **vpm_reads,
94 uint8_t swiz,
95 const struct util_format_description *desc)
96 {
97 const struct util_format_channel_description *chan =
98 &desc->channel[swiz];
99 nir_ssa_def *temp;
100
101 if (swiz > UTIL_FORMAT_SWIZZLE_W) {
102 return vc4_nir_get_swizzled_channel(b, vpm_reads, swiz);
103 } else if (chan->size == 32 && chan->type == UTIL_FORMAT_TYPE_FLOAT) {
104 return vc4_nir_get_swizzled_channel(b, vpm_reads, swiz);
105 } else if (chan->size == 32 && chan->type == UTIL_FORMAT_TYPE_SIGNED) {
106 if (chan->normalized) {
107 return nir_fmul(b,
108 nir_i2f(b, vpm_reads[swiz]),
109 nir_imm_float(b,
110 1.0 / 0x7fffffff));
111 } else {
112 return nir_i2f(b, vpm_reads[swiz]);
113 }
114 } else if (chan->size == 8 &&
115 (chan->type == UTIL_FORMAT_TYPE_UNSIGNED ||
116 chan->type == UTIL_FORMAT_TYPE_SIGNED)) {
117 nir_ssa_def *vpm = vpm_reads[0];
118 if (chan->type == UTIL_FORMAT_TYPE_SIGNED) {
119 temp = nir_ixor(b, vpm, nir_imm_int(b, 0x80808080));
120 if (chan->normalized) {
121 return nir_fsub(b, nir_fmul(b,
122 vc4_nir_unpack_8f(b, temp, swiz),
123 nir_imm_float(b, 2.0)),
124 nir_imm_float(b, 1.0));
125 } else {
126 return nir_fadd(b,
127 nir_i2f(b,
128 vc4_nir_unpack_8i(b, temp,
129 swiz)),
130 nir_imm_float(b, -128.0));
131 }
132 } else {
133 if (chan->normalized) {
134 return vc4_nir_unpack_8f(b, vpm, swiz);
135 } else {
136 return nir_i2f(b, vc4_nir_unpack_8i(b, vpm, swiz));
137 }
138 }
139 } else if (chan->size == 16 &&
140 (chan->type == UTIL_FORMAT_TYPE_UNSIGNED ||
141 chan->type == UTIL_FORMAT_TYPE_SIGNED)) {
142 nir_ssa_def *vpm = vpm_reads[swiz / 2];
143
144 /* Note that UNPACK_16F eats a half float, not ints, so we use
145 * UNPACK_16_I for all of these.
146 */
147 if (chan->type == UTIL_FORMAT_TYPE_SIGNED) {
148 temp = nir_i2f(b, vc4_nir_unpack_16i(b, vpm, swiz & 1));
149 if (chan->normalized) {
150 return nir_fmul(b, temp,
151 nir_imm_float(b, 1/32768.0f));
152 } else {
153 return temp;
154 }
155 } else {
156 temp = nir_i2f(b, vc4_nir_unpack_16u(b, vpm, swiz & 1));
157 if (chan->normalized) {
158 return nir_fmul(b, temp,
159 nir_imm_float(b, 1 / 65535.0));
160 } else {
161 return temp;
162 }
163 }
164 } else {
165 return NULL;
166 }
167 }
168
169 static void
170 vc4_nir_lower_vertex_attr(struct vc4_compile *c, nir_builder *b,
171 nir_intrinsic_instr *intr)
172 {
173 b->cursor = nir_before_instr(&intr->instr);
174
175 int attr = intr->const_index[0];
176 enum pipe_format format = c->vs_key->attr_formats[attr];
177 uint32_t attr_size = util_format_get_blocksize(format);
178
179 /* All TGSI-to-NIR inputs are vec4. */
180 assert(intr->num_components == 4);
181
182 /* Generate dword loads for the VPM values (Since these intrinsics may
183 * be reordered, the actual reads will be generated at the top of the
184 * shader by ntq_setup_inputs().
185 */
186 nir_ssa_def *vpm_reads[4];
187 for (int i = 0; i < align(attr_size, 4) / 4; i++) {
188 nir_intrinsic_instr *intr_comp =
189 nir_intrinsic_instr_create(c->s,
190 nir_intrinsic_load_input);
191 intr_comp->num_components = 1;
192 intr_comp->const_index[0] = intr->const_index[0] * 4 + i;
193 nir_ssa_dest_init(&intr_comp->instr, &intr_comp->dest, 1, NULL);
194 nir_builder_instr_insert(b, &intr_comp->instr);
195
196 vpm_reads[i] = &intr_comp->dest.ssa;
197 }
198
199 bool format_warned = false;
200 const struct util_format_description *desc =
201 util_format_description(format);
202
203 nir_ssa_def *dests[4];
204 for (int i = 0; i < 4; i++) {
205 uint8_t swiz = desc->swizzle[i];
206 dests[i] = vc4_nir_get_vattr_channel_vpm(c, b, vpm_reads, swiz,
207 desc);
208
209 if (!dests[i]) {
210 if (!format_warned) {
211 fprintf(stderr,
212 "vtx element %d unsupported type: %s\n",
213 attr, util_format_name(format));
214 format_warned = true;
215 }
216 dests[i] = nir_imm_float(b, 0.0);
217 }
218 }
219
220 replace_intrinsic_with_vec4(b, intr, dests);
221 }
222
223 static void
224 vc4_nir_lower_fs_input(struct vc4_compile *c, nir_builder *b,
225 nir_intrinsic_instr *intr)
226 {
227 b->cursor = nir_before_instr(&intr->instr);
228
229 if (intr->const_index[0] >= VC4_NIR_TLB_COLOR_READ_INPUT &&
230 intr->const_index[0] < (VC4_NIR_TLB_COLOR_READ_INPUT +
231 VC4_MAX_SAMPLES)) {
232 /* This doesn't need any lowering. */
233 return;
234 }
235
236 nir_variable *input_var = NULL;
237 nir_foreach_variable(var, &c->s->inputs) {
238 if (var->data.driver_location == intr->const_index[0]) {
239 input_var = var;
240 break;
241 }
242 }
243 assert(input_var);
244
245 /* All TGSI-to-NIR inputs are vec4. */
246 assert(intr->num_components == 4);
247
248 /* Generate scalar loads equivalent to the original VEC4. */
249 nir_ssa_def *dests[4];
250 for (unsigned i = 0; i < intr->num_components; i++) {
251 nir_intrinsic_instr *intr_comp =
252 nir_intrinsic_instr_create(c->s, nir_intrinsic_load_input);
253 intr_comp->num_components = 1;
254 intr_comp->const_index[0] = intr->const_index[0] * 4 + i;
255 nir_ssa_dest_init(&intr_comp->instr, &intr_comp->dest, 1, NULL);
256 nir_builder_instr_insert(b, &intr_comp->instr);
257
258 dests[i] = &intr_comp->dest.ssa;
259 }
260
261 if (input_var->data.location == VARYING_SLOT_FACE) {
262 dests[0] = nir_fsub(b,
263 nir_imm_float(b, 1.0),
264 nir_fmul(b,
265 nir_i2f(b, dests[0]),
266 nir_imm_float(b, 2.0)));
267 dests[1] = nir_imm_float(b, 0.0);
268 dests[2] = nir_imm_float(b, 0.0);
269 dests[3] = nir_imm_float(b, 1.0);
270 } else if (input_var->data.location >= VARYING_SLOT_VAR0) {
271 if (c->fs_key->point_sprite_mask &
272 (1 << (input_var->data.location -
273 VARYING_SLOT_VAR0))) {
274 if (!c->fs_key->is_points) {
275 dests[0] = nir_imm_float(b, 0.0);
276 dests[1] = nir_imm_float(b, 0.0);
277 }
278 if (c->fs_key->point_coord_upper_left) {
279 dests[1] = nir_fsub(b,
280 nir_imm_float(b, 1.0),
281 dests[1]);
282 }
283 dests[2] = nir_imm_float(b, 0.0);
284 dests[3] = nir_imm_float(b, 1.0);
285 }
286 }
287
288 replace_intrinsic_with_vec4(b, intr, dests);
289 }
290
291 static void
292 vc4_nir_lower_output(struct vc4_compile *c, nir_builder *b,
293 nir_intrinsic_instr *intr)
294 {
295 nir_variable *output_var = NULL;
296 nir_foreach_variable(var, &c->s->outputs) {
297 if (var->data.driver_location == intr->const_index[0]) {
298 output_var = var;
299 break;
300 }
301 }
302 assert(output_var);
303
304 if (c->stage == QSTAGE_COORD &&
305 output_var->data.location != VARYING_SLOT_POS &&
306 output_var->data.location != VARYING_SLOT_PSIZ) {
307 nir_instr_remove(&intr->instr);
308 return;
309 }
310
311 /* Color output is lowered by vc4_nir_lower_blend(). */
312 if (c->stage == QSTAGE_FRAG &&
313 (output_var->data.location == FRAG_RESULT_COLOR ||
314 output_var->data.location == FRAG_RESULT_DATA0)) {
315 intr->const_index[0] *= 4;
316 return;
317 }
318
319 /* All TGSI-to-NIR outputs are VEC4. */
320 assert(intr->num_components == 4);
321
322 b->cursor = nir_before_instr(&intr->instr);
323
324 for (unsigned i = 0; i < intr->num_components; i++) {
325 nir_intrinsic_instr *intr_comp =
326 nir_intrinsic_instr_create(c->s, nir_intrinsic_store_output);
327 intr_comp->num_components = 1;
328 intr_comp->const_index[0] = intr->const_index[0] * 4 + i;
329
330 assert(intr->src[0].is_ssa);
331 intr_comp->src[0] =
332 nir_src_for_ssa(nir_channel(b, intr->src[0].ssa, i));
333 nir_builder_instr_insert(b, &intr_comp->instr);
334 }
335
336 nir_instr_remove(&intr->instr);
337 }
338
339 static void
340 vc4_nir_lower_uniform(struct vc4_compile *c, nir_builder *b,
341 nir_intrinsic_instr *intr)
342 {
343 /* All TGSI-to-NIR uniform loads are vec4, but we may create dword
344 * loads in our lowering passes.
345 */
346 if (intr->num_components == 1)
347 return;
348 assert(intr->num_components == 4);
349
350 b->cursor = nir_before_instr(&intr->instr);
351
352 /* Generate scalar loads equivalent to the original VEC4. */
353 nir_ssa_def *dests[4];
354 for (unsigned i = 0; i < intr->num_components; i++) {
355 nir_intrinsic_instr *intr_comp =
356 nir_intrinsic_instr_create(c->s, intr->intrinsic);
357 intr_comp->num_components = 1;
358 nir_ssa_dest_init(&intr_comp->instr, &intr_comp->dest, 1, NULL);
359
360 if (intr->intrinsic == nir_intrinsic_load_uniform_indirect) {
361 /* Convert the variable TGSI register index to a byte
362 * offset.
363 */
364 intr_comp->src[0] =
365 nir_src_for_ssa(nir_ishl(b,
366 intr->src[0].ssa,
367 nir_imm_int(b, 4)));
368
369 /* Convert the offset to be a byte index, too. */
370 intr_comp->const_index[0] = (intr->const_index[0] * 16 +
371 i * 4);
372 } else {
373 /* We want a dword index for non-indirect uniform
374 * loads.
375 */
376 intr_comp->const_index[0] = (intr->const_index[0] * 4 +
377 i);
378 }
379
380 dests[i] = &intr_comp->dest.ssa;
381
382 nir_builder_instr_insert(b, &intr_comp->instr);
383 }
384
385 replace_intrinsic_with_vec4(b, intr, dests);
386 }
387
388 static void
389 vc4_nir_lower_io_instr(struct vc4_compile *c, nir_builder *b,
390 struct nir_instr *instr)
391 {
392 if (instr->type != nir_instr_type_intrinsic)
393 return;
394 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
395
396 switch (intr->intrinsic) {
397 case nir_intrinsic_load_input:
398 if (c->stage == QSTAGE_FRAG)
399 vc4_nir_lower_fs_input(c, b, intr);
400 else
401 vc4_nir_lower_vertex_attr(c, b, intr);
402 break;
403
404 case nir_intrinsic_store_output:
405 vc4_nir_lower_output(c, b, intr);
406 break;
407
408 case nir_intrinsic_load_uniform:
409 case nir_intrinsic_load_uniform_indirect:
410 case nir_intrinsic_load_user_clip_plane:
411 vc4_nir_lower_uniform(c, b, intr);
412 break;
413
414 default:
415 break;
416 }
417 }
418
419 static bool
420 vc4_nir_lower_io_block(nir_block *block, void *arg)
421 {
422 struct vc4_compile *c = arg;
423 nir_function_impl *impl =
424 nir_cf_node_get_function(&block->cf_node);
425
426 nir_builder b;
427 nir_builder_init(&b, impl);
428
429 nir_foreach_instr_safe(block, instr)
430 vc4_nir_lower_io_instr(c, &b, instr);
431
432 return true;
433 }
434
435 static bool
436 vc4_nir_lower_io_impl(struct vc4_compile *c, nir_function_impl *impl)
437 {
438 nir_foreach_block(impl, vc4_nir_lower_io_block, c);
439
440 nir_metadata_preserve(impl, nir_metadata_block_index |
441 nir_metadata_dominance);
442
443 return true;
444 }
445
446 void
447 vc4_nir_lower_io(struct vc4_compile *c)
448 {
449 nir_foreach_overload(c->s, overload) {
450 if (overload->impl)
451 vc4_nir_lower_io_impl(c, overload->impl);
452 }
453 }