| 1 |
/* |
| 2 |
* Copyright (C) 2025 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "pan_nir.h" |
| 7 |
#include "nir_builder.h" |
| 8 |
|
| 9 |
#include "panfrost/model/pan_model.h" |
| 10 |
|
| 11 |
|
| 12 |
struct lower_fs_inputs_ctx { |
| 13 |
unsigned arch; |
| 14 |
const struct pan_varying_layout *varying_layout; |
| 15 |
struct pan_shader_info *info; |
| 16 |
}; |
| 17 |
|
| 18 |
static bool |
| 19 |
lower_fs_input_load(struct nir_builder *b, |
| 20 |
nir_intrinsic_instr *load, void *cb_data) |
| 21 |
{ |
| 22 |
const struct lower_fs_inputs_ctx *ctx = cb_data; |
| 23 |
|
| 24 |
if (load->intrinsic != nir_intrinsic_load_input && |
| 25 |
load->intrinsic != nir_intrinsic_load_interpolated_input) |
| 26 |
return false; |
| 27 |
|
| 28 |
const nir_io_semantics sem = nir_intrinsic_io_semantics(load); |
| 29 |
const nir_alu_type dest_type = nir_intrinsic_dest_type(load); |
| 30 |
|
| 31 |
/* Indirect array varyings are not yet supported (num_slots > 1) */ |
| 32 |
assert(sem.num_slots == 1); |
| 33 |
assert(nir_src_as_uint(*nir_get_io_offset_src(load)) == 0); |
| 34 |
|
| 35 |
nir_intrinsic_instr *bary; |
| 36 |
switch (load->intrinsic) { |
| 37 |
case nir_intrinsic_load_input: |
| 38 |
bary = NULL; |
| 39 |
break; |
| 40 |
case nir_intrinsic_load_interpolated_input: |
| 41 |
/* Cannot interpolate ints */ |
| 42 |
assert(nir_alu_type_get_base_type(dest_type) == nir_type_float); |
| 43 |
bary = nir_src_as_intrinsic(load->src[0]); |
| 44 |
break; |
| 45 |
default: |
| 46 |
UNREACHABLE("Already handled"); |
| 47 |
} |
| 48 |
|
| 49 |
b->cursor = nir_before_instr(&load->instr); |
| 50 |
|
| 51 |
const unsigned component = nir_intrinsic_component(load); |
| 52 |
const unsigned load_comps = load->num_components + component; |
| 53 |
|
| 54 |
const struct pan_varying_slot *slot = NULL; |
| 55 |
bool use_ld_var_buf = false; |
| 56 |
if (ctx->varying_layout && ctx->arch >= 9) { |
| 57 |
pan_varying_layout_require_layout(ctx->varying_layout); |
| 58 |
slot = pan_varying_layout_find_slot(ctx->varying_layout, sem.location); |
| 59 |
assert(slot); |
| 60 |
use_ld_var_buf = slot->offset < pan_ld_var_buf_off_size(ctx->arch); |
| 61 |
} |
| 62 |
|
| 63 |
nir_def *res; |
| 64 |
if (use_ld_var_buf) { |
| 65 |
nir_def *offset_B = nir_imm_int(b, slot->offset); |
| 66 |
|
| 67 |
if (load->intrinsic == nir_intrinsic_load_interpolated_input) { |
| 68 |
res = nir_load_var_buf_pan(b, load_comps, load->def.bit_size, |
| 69 |
offset_B, &bary->def, |
| 70 |
.src_type = dest_type, |
| 71 |
.io_semantics = sem); |
| 72 |
} else { |
| 73 |
res = nir_load_var_buf_flat_pan(b, load_comps, load->def.bit_size, |
| 74 |
offset_B, |
| 75 |
.src_type = dest_type, |
| 76 |
.io_semantics = sem); |
| 77 |
} |
| 78 |
} else { |
| 79 |
ctx->info->bifrost.uses_ld_var = true; |
| 80 |
const uint32_t base = nir_intrinsic_base(load); |
| 81 |
nir_def *idx = nir_imm_int(b, base); |
| 82 |
|
| 83 |
if (load->intrinsic == nir_intrinsic_load_interpolated_input) { |
| 84 |
res = nir_load_var_pan(b, load_comps, load->def.bit_size, |
| 85 |
idx, &bary->def, |
| 86 |
.dest_type = dest_type, |
| 87 |
.io_semantics = sem); |
| 88 |
} else { |
| 89 |
res = nir_load_var_flat_pan(b, load_comps, load->def.bit_size, idx, |
| 90 |
.dest_type = dest_type, |
| 91 |
.io_semantics = sem); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if (component > 0) { |
| 96 |
unsigned swiz[NIR_MAX_VEC_COMPONENTS] = {0, }; |
| 97 |
for (unsigned c = 0; c < load->num_components; c++) |
| 98 |
swiz[c] = component + c; |
| 99 |
|
| 100 |
res = nir_swizzle(b, res, swiz, load->num_components); |
| 101 |
} |
| 102 |
|
| 103 |
nir_def_replace(&load->def, res); |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
bool |
| 108 |
pan_nir_lower_fs_inputs(nir_shader *shader, uint64_t gpu_id, |
| 109 |
const struct pan_varying_layout *varying_layout, |
| 110 |
struct pan_shader_info *info) |
| 111 |
{ |
| 112 |
const struct lower_fs_inputs_ctx ctx = { |
| 113 |
.arch = pan_arch(gpu_id), |
| 114 |
.varying_layout = varying_layout, |
| 115 |
.info = info, |
| 116 |
}; |
| 117 |
return nir_shader_intrinsics_pass(shader, lower_fs_input_load, |
| 118 |
nir_metadata_control_flow, |
| 119 |
(void *)&ctx); |
| 120 |
} |