freedreno/ir3: lower load_barycentric_at_offset
[mesa.git] / src / freedreno / ir3 / ir3_nir_lower_load_barycentric_at_offset.c
1 /*
2 * Copyright © 2019 Google, Inc.
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 "ir3_nir.h"
25 #include "compiler/nir/nir_builder.h"
26
27 /**
28 * This pass lowers load_barycentric_at_offset to dsx.3d/dsy.3d and alu
29 * instructions.
30 */
31
32 static nir_ssa_def *
33 load(nir_builder *b, unsigned ncomp, nir_intrinsic_op op)
34 {
35 nir_intrinsic_instr *load_size = nir_intrinsic_instr_create(b->shader, op);
36 load_size->num_components = ncomp;
37 nir_ssa_dest_init(&load_size->instr, &load_size->dest, ncomp, 32, NULL);
38 nir_builder_instr_insert(b, &load_size->instr);
39
40 return &load_size->dest.ssa;
41 }
42
43 static void
44 lower_load_barycentric_at_offset(nir_builder *b, nir_intrinsic_instr *intr)
45 {
46 #define chan(var, c) nir_channel(b, var, c)
47
48 nir_ssa_def *off = intr->src[0].ssa;
49 nir_ssa_def *ij = load(b, 2, nir_intrinsic_load_barycentric_pixel);
50 nir_ssa_def *s = load(b, 1, nir_intrinsic_load_size_ir3);
51
52 s = nir_frcp(b, s);
53
54 /* scaled ij with s as 3rd component: */
55 nir_ssa_def *sij = nir_vec3(b,
56 nir_fmul(b, chan(ij, 0), s),
57 nir_fmul(b, chan(ij, 1), s),
58 s);
59
60 nir_ssa_def *foo = nir_fddx(b, sij);
61 nir_ssa_def *bar = nir_fddy(b, sij);
62
63 nir_ssa_def *x, *y, *z, *i, *j;
64
65 x = nir_ffma(b, chan(off, 0), chan(foo, 0), chan(sij, 0));
66 y = nir_ffma(b, chan(off, 0), chan(foo, 1), chan(sij, 1));
67 z = nir_ffma(b, chan(off, 0), chan(foo, 2), chan(sij, 2));
68
69 x = nir_ffma(b, chan(off, 1), chan(bar, 0), x);
70 y = nir_ffma(b, chan(off, 1), chan(bar, 1), y);
71 z = nir_ffma(b, chan(off, 1), chan(bar, 2), z);
72
73 /* convert back into primitive space: */
74 z = nir_frcp(b, z);
75 i = nir_fmul(b, z, x);
76 j = nir_fmul(b, z, y);
77
78 ij = nir_vec2(b, i, j);
79
80 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(ij));
81 }
82
83 bool
84 ir3_nir_lower_load_barycentric_at_offset(nir_shader *shader)
85 {
86 bool progress = false;
87
88 debug_assert(shader->info.stage == MESA_SHADER_FRAGMENT);
89
90 nir_foreach_function (function, shader) {
91 if (!function->impl)
92 continue;
93
94 nir_builder b;
95 nir_builder_init(&b, function->impl);
96
97 nir_foreach_block (block, function->impl) {
98 nir_foreach_instr_safe(instr, block) {
99 if (instr->type != nir_instr_type_intrinsic)
100 continue;
101
102 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
103
104 if (intr->intrinsic != nir_intrinsic_load_barycentric_at_offset)
105 continue;
106
107 debug_assert(intr->src[0].is_ssa);
108 debug_assert(intr->dest.is_ssa);
109
110 b.cursor = nir_before_instr(instr);
111 lower_load_barycentric_at_offset(&b, intr);
112
113 progress = true;
114 }
115 }
116
117 if (progress) {
118 nir_metadata_preserve(function->impl,
119 nir_metadata_block_index | nir_metadata_dominance);
120 }
121 }
122
123 return progress;
124 }