pan/mdg: Prepare for modifier helpers
[mesa.git] / src / panfrost / midgard / midgard_opt_perspective.c
1 /*
2 * Copyright (C) 2019 Collabora, Ltd.
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 /* Midgard has some accelerated support for perspective projection on the
25 * load/store pipes. So the first perspective projection pass looks for
26 * lowered/open-coded perspective projection of the form "fmul (A.xyz,
27 * frcp(A.w))" or "fmul (A.xy, frcp(A.z))" and rewrite with a native
28 * perspective division opcode (on the load/store pipe). Caveats apply: the
29 * frcp should be used only once to make this optimization worthwhile. And the
30 * source of the frcp ought to be a varying to make it worthwhile...
31 *
32 * The second pass in this file is a step #2 of sorts: fusing that load/store
33 * projection into a varying load instruction (they can be done together
34 * implicitly). This depends on the combination pass. Again caveat: the vary
35 * should only be used once to make this worthwhile.
36 */
37
38 #include "compiler.h"
39
40 static bool
41 is_swizzle_0(unsigned *swizzle)
42 {
43 for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c)
44 if (swizzle[c])
45 return false;
46
47 return true;
48 }
49
50 bool
51 midgard_opt_combine_projection(compiler_context *ctx, midgard_block *block)
52 {
53 bool progress = false;
54
55 mir_foreach_instr_in_block_safe(block, ins) {
56 /* First search for fmul */
57 if (ins->type != TAG_ALU_4) continue;
58 if (ins->alu.op != midgard_alu_op_fmul) continue;
59
60 /* TODO: Flip */
61
62 /* Check the swizzles */
63
64 if (!mir_is_simple_swizzle(ins->swizzle[0], ins->mask)) continue;
65 if (!is_swizzle_0(ins->swizzle[1])) continue;
66
67 /* Awesome, we're the right form. Now check where src2 is from */
68 unsigned frcp = ins->src[1];
69 unsigned to = ins->dest;
70
71 if (frcp & PAN_IS_REG) continue;
72 if (to & PAN_IS_REG) continue;
73
74 bool frcp_found = false;
75 unsigned frcp_component = 0;
76 unsigned frcp_from = 0;
77
78 mir_foreach_instr_in_block_safe(block, sub) {
79 if (sub->dest != frcp) continue;
80
81 frcp_component = sub->swizzle[0][0];
82 frcp_from = sub->src[0];
83
84 frcp_found =
85 (sub->type == TAG_ALU_4) &&
86 (sub->alu.op == midgard_alu_op_frcp);
87 break;
88 }
89
90 if (!frcp_found) continue;
91 if (frcp_component != COMPONENT_W && frcp_component != COMPONENT_Z) continue;
92 if (!mir_single_use(ctx, frcp)) continue;
93
94 /* Heuristic: check if the frcp is from a single-use varying */
95
96 bool ok = false;
97
98 /* One for frcp and one for fmul */
99 if (mir_use_count(ctx, frcp_from) > 2) continue;
100
101 mir_foreach_instr_in_block_safe(block, v) {
102 if (v->dest != frcp_from) continue;
103 if (v->type != TAG_LOAD_STORE_4) break;
104 if (!OP_IS_LOAD_VARY_F(v->load_store.op)) break;
105
106 ok = true;
107 break;
108 }
109
110 if (!ok)
111 continue;
112
113 /* Nice, we got the form spot on. Let's convert! */
114
115 midgard_instruction accel = {
116 .type = TAG_LOAD_STORE_4,
117 .mask = ins->mask,
118 .dest = to,
119 .src = { frcp_from, ~0, ~0, ~0 },
120 .swizzle = SWIZZLE_IDENTITY_4,
121 .load_store = {
122 .op = frcp_component == COMPONENT_W ?
123 midgard_op_ldst_perspective_division_w :
124 midgard_op_ldst_perspective_division_z,
125 .arg_1 = 0x20
126 }
127 };
128
129 mir_insert_instruction_before(ctx, ins, accel);
130 mir_remove_instruction(ins);
131
132 progress |= true;
133 }
134
135 return progress;
136 }
137
138 bool
139 midgard_opt_varying_projection(compiler_context *ctx, midgard_block *block)
140 {
141 bool progress = false;
142
143 mir_foreach_instr_in_block_safe(block, ins) {
144 /* Search for a projection */
145 if (ins->type != TAG_LOAD_STORE_4) continue;
146 if (!OP_IS_PROJECTION(ins->load_store.op)) continue;
147
148 unsigned vary = ins->src[0];
149 unsigned to = ins->dest;
150
151 if (vary & PAN_IS_REG) continue;
152 if (to & PAN_IS_REG) continue;
153 if (!mir_single_use(ctx, vary)) continue;
154
155 /* Check for a varying source. If we find it, we rewrite */
156
157 bool rewritten = false;
158
159 mir_foreach_instr_in_block_safe(block, v) {
160 if (v->dest != vary) continue;
161 if (v->type != TAG_LOAD_STORE_4) break;
162 if (!OP_IS_LOAD_VARY_F(v->load_store.op)) break;
163
164 /* We found it, so rewrite it to project. Grab the
165 * modifier */
166
167 unsigned param = v->load_store.varying_parameters;
168 midgard_varying_parameter p;
169 memcpy(&p, &param, sizeof(p));
170
171 if (p.modifier != midgard_varying_mod_none)
172 break;
173
174 bool projects_w =
175 ins->load_store.op == midgard_op_ldst_perspective_division_w;
176
177 p.modifier = projects_w ?
178 midgard_varying_mod_perspective_w :
179 midgard_varying_mod_perspective_z;
180
181 /* Aliasing rules are annoying */
182 memcpy(&param, &p, sizeof(p));
183 v->load_store.varying_parameters = param;
184
185 /* Use the new destination */
186 v->dest = to;
187
188 rewritten = true;
189 break;
190 }
191
192 if (rewritten)
193 mir_remove_instruction(ins);
194
195 progress |= rewritten;
196 }
197
198 return progress;
199 }