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