| 1 |
/* |
| 2 |
* Copyright (C) 2025 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "pan_compiler.h" |
| 7 |
#include "pan_nir.h" |
| 8 |
|
| 9 |
#include "bifrost/bi_debug.h" |
| 10 |
#include "bifrost/bifrost_compile.h" |
| 11 |
#include "bifrost/bifrost/disassemble.h" |
| 12 |
#include "bifrost/valhall/disassemble.h" |
| 13 |
#include "midgard/disassemble.h" |
| 14 |
#include "midgard/midgard_compile.h" |
| 15 |
|
| 16 |
#include "panfrost/model/pan_model.h" |
| 17 |
|
| 18 |
bool |
| 19 |
pan_will_dump_shaders(unsigned arch) |
| 20 |
{ |
| 21 |
if (arch >= 6) |
| 22 |
return bifrost_will_dump_shaders(); |
| 23 |
else |
| 24 |
return midgard_will_dump_shaders(); |
| 25 |
} |
| 26 |
|
| 27 |
bool |
| 28 |
pan_want_debug_info(unsigned arch) |
| 29 |
{ |
| 30 |
if (arch >= 6) |
| 31 |
return bifrost_want_debug_info(); |
| 32 |
else |
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
const nir_shader_compiler_options * |
| 37 |
pan_get_nir_shader_compiler_options(unsigned arch, bool merge_wg) |
| 38 |
{ |
| 39 |
switch (arch) { |
| 40 |
case 4: |
| 41 |
case 5: |
| 42 |
assert(!merge_wg); |
| 43 |
return &midgard_nir_options; |
| 44 |
case 6: |
| 45 |
case 7: |
| 46 |
assert(!merge_wg); |
| 47 |
return &bifrost_nir_options_v6; |
| 48 |
case 9: |
| 49 |
case 10: |
| 50 |
return merge_wg ? &bifrost_nir_options_v9_merge_wg : |
| 51 |
&bifrost_nir_options_v9; |
| 52 |
case 11: |
| 53 |
case 12: |
| 54 |
case 13: |
| 55 |
return merge_wg ? &bifrost_nir_options_v11_merge_wg : |
| 56 |
&bifrost_nir_options_v11; |
| 57 |
default: |
| 58 |
assert(!"Unsupported arch"); |
| 59 |
return NULL; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
void |
| 64 |
pan_preprocess_nir(nir_shader *nir, uint64_t gpu_id) |
| 65 |
{ |
| 66 |
if (pan_arch(gpu_id) >= 6) |
| 67 |
bifrost_preprocess_nir(nir, gpu_id); |
| 68 |
else |
| 69 |
midgard_preprocess_nir(nir, gpu_id); |
| 70 |
|
| 71 |
/* Lower textures early */ |
| 72 |
nir_lower_tex_options lower_tex_options = { |
| 73 |
.lower_txs_lod = true, |
| 74 |
.lower_txp = ~0, |
| 75 |
.lower_tg4_offsets = true, |
| 76 |
.lower_tg4_broadcom_swizzle = true, |
| 77 |
.lower_txd = pan_arch(gpu_id) < 6, |
| 78 |
.lower_txd_cube_map = true, |
| 79 |
.lower_invalid_implicit_lod = true, |
| 80 |
.lower_index_to_offset = pan_arch(gpu_id) >= 6, |
| 81 |
}; |
| 82 |
|
| 83 |
NIR_PASS(_, nir, nir_lower_tex, &lower_tex_options); |
| 84 |
} |
| 85 |
|
| 86 |
void |
| 87 |
pan_optimize_nir(nir_shader *nir, uint64_t gpu_id) |
| 88 |
{ |
| 89 |
assert(pan_arch(gpu_id) >= 6); |
| 90 |
bifrost_optimize_nir(nir, gpu_id); |
| 91 |
} |
| 92 |
|
| 93 |
void |
| 94 |
pan_postprocess_nir(nir_shader *nir, uint64_t gpu_id) |
| 95 |
{ |
| 96 |
if (pan_arch(gpu_id) >= 6) |
| 97 |
bifrost_postprocess_nir(nir, gpu_id); |
| 98 |
else |
| 99 |
midgard_postprocess_nir(nir, gpu_id); |
| 100 |
} |
| 101 |
|
| 102 |
/** Converts a per-component mask to a byte mask */ |
| 103 |
uint16_t |
| 104 |
pan_to_bytemask(unsigned bytes, unsigned mask) |
| 105 |
{ |
| 106 |
switch (bytes) { |
| 107 |
case 0: |
| 108 |
assert(mask == 0); |
| 109 |
return 0; |
| 110 |
|
| 111 |
case 8: |
| 112 |
return mask; |
| 113 |
|
| 114 |
case 16: { |
| 115 |
unsigned space = |
| 116 |
(mask & 0x1) | ((mask & 0x2) << (2 - 1)) | ((mask & 0x4) << (4 - 2)) | |
| 117 |
((mask & 0x8) << (6 - 3)) | ((mask & 0x10) << (8 - 4)) | |
| 118 |
((mask & 0x20) << (10 - 5)) | ((mask & 0x40) << (12 - 6)) | |
| 119 |
((mask & 0x80) << (14 - 7)); |
| 120 |
|
| 121 |
return space | (space << 1); |
| 122 |
} |
| 123 |
|
| 124 |
case 32: { |
| 125 |
unsigned space = (mask & 0x1) | ((mask & 0x2) << (4 - 1)) | |
| 126 |
((mask & 0x4) << (8 - 2)) | ((mask & 0x8) << (12 - 3)); |
| 127 |
|
| 128 |
return space | (space << 1) | (space << 2) | (space << 3); |
| 129 |
} |
| 130 |
|
| 131 |
case 64: { |
| 132 |
unsigned A = (mask & 0x1) ? 0xFF : 0x00; |
| 133 |
unsigned B = (mask & 0x2) ? 0xFF : 0x00; |
| 134 |
return A | (B << 8); |
| 135 |
} |
| 136 |
|
| 137 |
default: |
| 138 |
UNREACHABLE("Invalid register mode"); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/* Could optimize with a better data structure if anyone cares, TODO: profile */ |
| 143 |
unsigned |
| 144 |
pan_lookup_pushed_ubo(struct pan_ubo_push *push, unsigned ubo, unsigned offs) |
| 145 |
{ |
| 146 |
struct pan_ubo_word word = {.ubo = ubo, .offset = offs}; |
| 147 |
|
| 148 |
for (unsigned i = 0; i < push->count; ++i) { |
| 149 |
if (memcmp(push->words + i, &word, sizeof(word)) == 0) |
| 150 |
return i; |
| 151 |
} |
| 152 |
|
| 153 |
UNREACHABLE("UBO not pushed"); |
| 154 |
} |
| 155 |
|
| 156 |
void |
| 157 |
pan_shader_update_info(struct pan_shader_info *info, nir_shader *s, |
| 158 |
const struct pan_compile_inputs *inputs) |
| 159 |
{ |
| 160 |
unsigned arch = pan_arch(inputs->gpu_id); |
| 161 |
|
| 162 |
info->stage = s->info.stage; |
| 163 |
info->contains_barrier = |
| 164 |
s->info.uses_memory_barrier || s->info.uses_control_barrier; |
| 165 |
info->separable = s->info.separate_shader; |
| 166 |
|
| 167 |
switch (info->stage) { |
| 168 |
case MESA_SHADER_VERTEX: |
| 169 |
info->attributes_read = s->info.inputs_read; |
| 170 |
info->attributes_read_count = util_bitcount64(info->attributes_read); |
| 171 |
info->attribute_count = info->attributes_read_count; |
| 172 |
|
| 173 |
if (arch <= 5) { |
| 174 |
if (info->midgard.vs.reads_raw_vertex_id) |
| 175 |
info->attribute_count = |
| 176 |
MAX2(info->attribute_count, PAN_VERTEX_ID + 1); |
| 177 |
|
| 178 |
bool instance_id = |
| 179 |
BITSET_TEST(s->info.system_values_read, SYSTEM_VALUE_INSTANCE_ID); |
| 180 |
if (instance_id) |
| 181 |
info->attribute_count = |
| 182 |
MAX2(info->attribute_count, PAN_INSTANCE_ID + 1); |
| 183 |
} |
| 184 |
|
| 185 |
info->vs.writes_point_size = |
| 186 |
s->info.outputs_written & VARYING_BIT_PSIZ; |
| 187 |
break; |
| 188 |
case MESA_SHADER_FRAGMENT: |
| 189 |
if (s->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) |
| 190 |
info->fs.writes_depth = true; |
| 191 |
if (s->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) |
| 192 |
info->fs.writes_stencil = true; |
| 193 |
if (s->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK)) |
| 194 |
info->fs.writes_coverage = true; |
| 195 |
|
| 196 |
info->fs.outputs_read = s->info.outputs_read; |
| 197 |
|
| 198 |
info->fs.sample_shading = s->info.fs.uses_sample_shading; |
| 199 |
info->fs.untyped_color_outputs = s->info.fs.untyped_color_outputs; |
| 200 |
|
| 201 |
info->fs.can_discard = s->info.fs.uses_discard; |
| 202 |
info->fs.early_fragment_tests = s->info.fs.early_fragment_tests; |
| 203 |
|
| 204 |
/* List of reasons we need to execute frag shaders when things |
| 205 |
* are masked off */ |
| 206 |
|
| 207 |
info->fs.sidefx = s->info.writes_memory || s->info.fs.uses_discard; |
| 208 |
|
| 209 |
/* With suitable ZSA/blend, is early-z possible? */ |
| 210 |
info->fs.can_early_z = !info->fs.sidefx && !info->fs.writes_depth && |
| 211 |
!info->fs.writes_stencil && |
| 212 |
!info->fs.writes_coverage; |
| 213 |
|
| 214 |
/* Similiarly with suitable state, is FPK possible? */ |
| 215 |
info->fs.can_fpk = !info->fs.writes_depth && !info->fs.writes_stencil && |
| 216 |
!info->fs.writes_coverage && !info->fs.can_discard && |
| 217 |
!info->fs.outputs_read; |
| 218 |
|
| 219 |
/* Requires the same hardware guarantees, so grouped as one bit |
| 220 |
* in the hardware. |
| 221 |
*/ |
| 222 |
info->contains_barrier |= s->info.fs.needs_coarse_quad_helper_invocations; |
| 223 |
|
| 224 |
info->fs.reads_frag_coord = |
| 225 |
(s->info.inputs_read & VARYING_BIT_POS) || |
| 226 |
BITSET_TEST(s->info.system_values_read, SYSTEM_VALUE_FRAG_COORD); |
| 227 |
info->fs.reads_primitive_id = |
| 228 |
(s->info.inputs_read & VARYING_BIT_PRIMITIVE_ID) || |
| 229 |
BITSET_TEST(s->info.system_values_read, SYSTEM_VALUE_PRIMITIVE_ID); |
| 230 |
info->fs.reads_point_coord = |
| 231 |
s->info.inputs_read & VARYING_BIT_PNTC; |
| 232 |
info->fs.reads_face = |
| 233 |
(s->info.inputs_read & VARYING_BIT_FACE) || |
| 234 |
BITSET_TEST(s->info.system_values_read, SYSTEM_VALUE_FRONT_FACE); |
| 235 |
break; |
| 236 |
default: |
| 237 |
/* Everything else treated as compute */ |
| 238 |
info->wls_size = s->info.shared_size; |
| 239 |
break; |
| 240 |
} |
| 241 |
|
| 242 |
info->outputs_written = s->info.outputs_written; |
| 243 |
info->images_used = |
| 244 |
s->info.images_used[0] | ((uint64_t)s->info.images_used[1]) << 32; |
| 245 |
info->attribute_count += BITSET_LAST_BIT(s->info.images_used); |
| 246 |
if (arch >= 6 && arch < 9) |
| 247 |
info->attribute_count += BITSET_LAST_BIT(s->info.texture_buffers); |
| 248 |
info->writes_global = s->info.writes_memory; |
| 249 |
info->ubo_count = s->info.num_ubos; |
| 250 |
|
| 251 |
info->sampler_count = info->texture_count = |
| 252 |
BITSET_LAST_BIT(s->info.textures_used); |
| 253 |
|
| 254 |
unsigned execution_mode = s->info.float_controls_execution_mode; |
| 255 |
info->ftz_fp16 = nir_is_denorm_flush_to_zero(execution_mode, 16); |
| 256 |
info->ftz_fp32 = nir_is_denorm_flush_to_zero(execution_mode, 32); |
| 257 |
|
| 258 |
if (arch >= 9) { |
| 259 |
/* Valhall hardware doesn't have a "flush FP16, preserve FP32" mode, and |
| 260 |
* we don't advertise independent FP16/FP32 denorm modes in panvk, but |
| 261 |
* it's still possible to have shaders that don't specify any denorm mode |
| 262 |
* for FP32. In that case, default to flush FP32. */ |
| 263 |
if (info->ftz_fp16 && !info->ftz_fp32) { |
| 264 |
assert(!nir_is_denorm_preserve(execution_mode, 32)); |
| 265 |
info->ftz_fp32 = true; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
void |
| 271 |
pan_shader_compile(nir_shader *s, struct pan_compile_inputs *inputs, |
| 272 |
struct util_dynarray *binary, struct pan_shader_info *info) |
| 273 |
{ |
| 274 |
unsigned arch = pan_arch(inputs->gpu_id); |
| 275 |
|
| 276 |
memset(info, 0, sizeof(*info)); |
| 277 |
|
| 278 |
NIR_PASS(_, s, nir_inline_sysval, nir_intrinsic_load_printf_buffer_size, |
| 279 |
PAN_PRINTF_BUFFER_SIZE - 8); |
| 280 |
|
| 281 |
if (arch >= 6) { |
| 282 |
bifrost_compile_shader_nir(s, inputs, binary, info); |
| 283 |
/* pan_shader_update_info done in the compile */ |
| 284 |
} else { |
| 285 |
midgard_compile_shader_nir(s, inputs, binary, info); |
| 286 |
pan_shader_update_info(info, s, inputs); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
void |
| 291 |
pan_disassemble(FILE *fp, const void *code, size_t size, uint64_t gpu_id, |
| 292 |
bool verbose) |
| 293 |
{ |
| 294 |
if (pan_arch(gpu_id) >= 9) |
| 295 |
disassemble_valhall(fp, (const uint64_t *)code, size, verbose); |
| 296 |
else if (pan_arch(gpu_id) >= 6) |
| 297 |
disassemble_bifrost(fp, code, size, verbose); |
| 298 |
else |
| 299 |
disassemble_midgard(fp, code, size, gpu_id, verbose); |
| 300 |
} |