| 1 |
/* |
| 2 |
* Copyright © 2024 Collabora Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "compiler/nir/nir_builder.h" |
| 7 |
#include "bi_opcodes.h" |
| 8 |
#include "pan_nir.h" |
| 9 |
|
| 10 |
/* Mali only provides instructions to fetch varyings with either flat or |
| 11 |
* perspective-correct interpolation. This pass lowers noperspective varyings |
| 12 |
* to perspective-correct varyings by multiplying by W in the VS and dividing |
| 13 |
* by W in the FS. |
| 14 |
* |
| 15 |
* This pass needs to lower noperspective varyings in the VS, however Vulkan |
| 16 |
* and OpenGL do not require interpolation qualifiers to match between stages. |
| 17 |
* Only the qualifiers in the fragment shader matter. To handle this, we load |
| 18 |
* a bitfield of noperspective varyings in the linked FS from the |
| 19 |
* 'noperspective_varyings_pan' sysval in the VS. If the FS qualifiers are |
| 20 |
* known at compile-time (for example, with monolithic pipelines in vulkan), |
| 21 |
* this may be lowered to a constant. |
| 22 |
* |
| 23 |
* This pass is expected to run after nir_lower_io_vars_to_temporaries and |
| 24 |
* nir_lower_io, so each IO location must have at most one read or write. |
| 25 |
* These properties are preserved. |
| 26 |
* |
| 27 |
* This pass is expected to run after nir_lower_viewport_transform, so |
| 28 |
* gl_Position.w is actually 1 / gl_Position.w. This is because |
| 29 |
* nir_lower_viewport_transform may clamp large W values, and we need to use |
| 30 |
* the clamped value here. */ |
| 31 |
|
| 32 |
static nir_intrinsic_instr * |
| 33 |
find_pos_store(nir_function_impl *impl) |
| 34 |
{ |
| 35 |
/* nir_lower_io_vars_to_temporaries ensures all stores are in the exit block */ |
| 36 |
nir_block *block = nir_impl_last_block(impl); |
| 37 |
nir_foreach_instr_safe(instr, block) { |
| 38 |
if (instr->type != nir_instr_type_intrinsic) |
| 39 |
continue; |
| 40 |
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); |
| 41 |
|
| 42 |
if (intrin->intrinsic != nir_intrinsic_store_output) |
| 43 |
continue; |
| 44 |
|
| 45 |
nir_io_semantics sem = nir_intrinsic_io_semantics(intrin); |
| 46 |
if (sem.location == VARYING_SLOT_POS) |
| 47 |
return intrin; |
| 48 |
} |
| 49 |
|
| 50 |
return NULL; |
| 51 |
} |
| 52 |
|
| 53 |
static bool |
| 54 |
is_noperspective_load(nir_intrinsic_instr* intrin) |
| 55 |
{ |
| 56 |
if (intrin->intrinsic != nir_intrinsic_load_interpolated_input) |
| 57 |
return false; |
| 58 |
|
| 59 |
nir_intrinsic_instr *bary_instr = nir_src_as_intrinsic(intrin->src[0]); |
| 60 |
assert(bary_instr); |
| 61 |
|
| 62 |
return nir_intrinsic_interp_mode(bary_instr) == INTERP_MODE_NOPERSPECTIVE; |
| 63 |
} |
| 64 |
|
| 65 |
static bool |
| 66 |
has_noperspective_load(nir_function_impl *impl) |
| 67 |
{ |
| 68 |
/* nir_lower_io_vars_to_temporaries ersures all loads are in the first block */ |
| 69 |
nir_block *block = nir_start_block(impl); |
| 70 |
nir_foreach_instr(instr, block) { |
| 71 |
if (instr->type != nir_instr_type_intrinsic) |
| 72 |
continue; |
| 73 |
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); |
| 74 |
|
| 75 |
if (is_noperspective_load(intrin)) |
| 76 |
return true; |
| 77 |
} |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
static nir_def * |
| 82 |
is_noperspective_output(nir_builder *b, unsigned location, |
| 83 |
nir_def *noperspective_outputs) |
| 84 |
{ |
| 85 |
if (location < VARYING_SLOT_VAR0) |
| 86 |
return nir_imm_bool(b, false); |
| 87 |
uint32_t bit = BITFIELD_BIT(location - VARYING_SLOT_VAR0); |
| 88 |
return nir_i2b(b, nir_iand_imm(b, noperspective_outputs, bit)); |
| 89 |
} |
| 90 |
|
| 91 |
struct lower_noperspective_vs_state { |
| 92 |
nir_def *pos_w; |
| 93 |
nir_def *noperspective_outputs; |
| 94 |
}; |
| 95 |
|
| 96 |
/** |
| 97 |
* Multiply all noperspective varying stores by gl_Position.w |
| 98 |
*/ |
| 99 |
static bool |
| 100 |
lower_noperspective_vs(nir_builder *b, nir_intrinsic_instr *intrin, |
| 101 |
void *data) |
| 102 |
{ |
| 103 |
struct lower_noperspective_vs_state *state = data; |
| 104 |
|
| 105 |
if (intrin->intrinsic != nir_intrinsic_store_output) |
| 106 |
return false; |
| 107 |
nir_io_semantics sem = nir_intrinsic_io_semantics(intrin); |
| 108 |
|
| 109 |
/* Only generic varyings can be noperspective */ |
| 110 |
if (sem.location < VARYING_SLOT_VAR0) |
| 111 |
return false; |
| 112 |
|
| 113 |
b->cursor = nir_before_instr(&intrin->instr); |
| 114 |
|
| 115 |
nir_def *is_noperspective = |
| 116 |
is_noperspective_output(b, sem.location, state->noperspective_outputs); |
| 117 |
|
| 118 |
nir_def *old_value = intrin->src[0].ssa; |
| 119 |
nir_def *pos_w = state->pos_w; |
| 120 |
if (old_value->bit_size == 16) |
| 121 |
pos_w = nir_f2f16(b, pos_w); |
| 122 |
nir_def *noperspective_value = nir_fmul(b, old_value, pos_w); |
| 123 |
nir_def *new_value = |
| 124 |
nir_bcsel(b, is_noperspective, noperspective_value, old_value); |
| 125 |
|
| 126 |
nir_src_rewrite(&intrin->src[0], new_value); |
| 127 |
|
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Multiply all noperspective varying loads by gl_FragCoord.w |
| 133 |
*/ |
| 134 |
static bool |
| 135 |
lower_noperspective_fs(nir_builder *b, nir_intrinsic_instr *intrin, |
| 136 |
void *data) |
| 137 |
{ |
| 138 |
if (!is_noperspective_load(intrin)) |
| 139 |
return false; |
| 140 |
|
| 141 |
b->cursor = nir_after_instr(&intrin->instr); |
| 142 |
|
| 143 |
nir_def *bary = intrin->src[0].ssa; |
| 144 |
nir_def *fragcoord_w = |
| 145 |
nir_load_var_special_pan(b, 1, bary, .flags = BI_VARYING_NAME_FRAG_W); |
| 146 |
if (intrin->def.bit_size == 16) |
| 147 |
fragcoord_w = nir_f2f16(b, fragcoord_w); |
| 148 |
|
| 149 |
nir_def *new_value = nir_fmul(b, &intrin->def, fragcoord_w); |
| 150 |
nir_def_rewrite_uses_after(&intrin->def, new_value); |
| 151 |
|
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Move all stores to output variables that occur before the specified |
| 157 |
* instruction in the same block to after the specified instruction. |
| 158 |
*/ |
| 159 |
static void |
| 160 |
move_output_stores_after(nir_instr *after) |
| 161 |
{ |
| 162 |
nir_cursor cursor = nir_after_instr(after); |
| 163 |
nir_block *block = nir_cursor_current_block(cursor); |
| 164 |
nir_foreach_instr_safe(instr, block) { |
| 165 |
if (instr == after) |
| 166 |
break; |
| 167 |
|
| 168 |
if (instr->type != nir_instr_type_intrinsic) |
| 169 |
continue; |
| 170 |
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); |
| 171 |
|
| 172 |
if (intrin->intrinsic == nir_intrinsic_store_output) |
| 173 |
nir_instr_move(cursor, instr); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
bool |
| 178 |
pan_nir_lower_noperspective_vs(nir_shader *shader) |
| 179 |
{ |
| 180 |
assert(shader->info.stage == MESA_SHADER_VERTEX); |
| 181 |
|
| 182 |
if (!(shader->info.outputs_written & VARYING_BIT_POS)) |
| 183 |
return false; |
| 184 |
|
| 185 |
nir_function_impl *impl = nir_shader_get_entrypoint(shader); |
| 186 |
|
| 187 |
nir_intrinsic_instr *pos_store = find_pos_store(impl); |
| 188 |
/* gl_Position may be written, it can also be left undefined */ |
| 189 |
bool has_pos_w = |
| 190 |
pos_store && !!(nir_intrinsic_write_mask(pos_store) & BITFIELD_BIT(3)); |
| 191 |
if (!has_pos_w) |
| 192 |
return false; |
| 193 |
|
| 194 |
nir_builder b = nir_builder_at(nir_after_instr(&pos_store->instr)); |
| 195 |
|
| 196 |
/* This is after nir_lower_viewport_transform, so stored W is 1/W */ |
| 197 |
nir_def *pos_w_recip = nir_channel(&b, pos_store->src[0].ssa, 3); |
| 198 |
nir_def *pos_w = nir_frcp(&b, pos_w_recip); |
| 199 |
|
| 200 |
/* Reorder stores to ensure pos_w def is available */ |
| 201 |
move_output_stores_after(nir_def_instr(pos_w)); |
| 202 |
|
| 203 |
nir_def *noperspective_outputs = nir_load_noperspective_varyings_pan(&b); |
| 204 |
struct lower_noperspective_vs_state state = { |
| 205 |
.pos_w = pos_w, |
| 206 |
.noperspective_outputs = noperspective_outputs, |
| 207 |
}; |
| 208 |
nir_shader_intrinsics_pass(shader, lower_noperspective_vs, |
| 209 |
nir_metadata_control_flow | |
| 210 |
nir_metadata_loop_analysis, |
| 211 |
(void *)&state); |
| 212 |
|
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
bool |
| 217 |
pan_nir_lower_noperspective_fs(nir_shader *shader) |
| 218 |
{ |
| 219 |
assert(shader->info.stage == MESA_SHADER_FRAGMENT); |
| 220 |
|
| 221 |
nir_function_impl *impl = nir_shader_get_entrypoint(shader); |
| 222 |
|
| 223 |
if (!has_noperspective_load(impl)) |
| 224 |
return false; |
| 225 |
|
| 226 |
nir_shader_intrinsics_pass(shader, lower_noperspective_fs, |
| 227 |
nir_metadata_control_flow, NULL); |
| 228 |
|
| 229 |
return true; |
| 230 |
} |