| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "msl_private.h" |
| 7 |
#include "nir_to_msl.h" |
| 8 |
|
| 9 |
#include "nir.h" |
| 10 |
#include "nir_builder.h" |
| 11 |
|
| 12 |
#include "util/format/u_format.h" |
| 13 |
|
| 14 |
bool |
| 15 |
msl_nir_vs_remove_point_size_write(nir_builder *b, nir_intrinsic_instr *intrin, |
| 16 |
void *data) |
| 17 |
{ |
| 18 |
if (intrin->intrinsic != nir_intrinsic_store_output) |
| 19 |
return false; |
| 20 |
|
| 21 |
nir_io_semantics io = nir_intrinsic_io_semantics(intrin); |
| 22 |
if (io.location == VARYING_SLOT_PSIZ) { |
| 23 |
return nir_remove_sysval_output(intrin, MESA_SHADER_FRAGMENT); |
| 24 |
} |
| 25 |
|
| 26 |
return false; |
| 27 |
} |
| 28 |
|
| 29 |
bool |
| 30 |
msl_nir_fs_remove_depth_write(nir_builder *b, nir_intrinsic_instr *intrin, |
| 31 |
void *data) |
| 32 |
{ |
| 33 |
if (intrin->intrinsic != nir_intrinsic_store_output) |
| 34 |
return false; |
| 35 |
|
| 36 |
nir_io_semantics io = nir_intrinsic_io_semantics(intrin); |
| 37 |
if (io.location == FRAG_RESULT_DEPTH) { |
| 38 |
return nir_remove_sysval_output(intrin, MESA_SHADER_FRAGMENT); |
| 39 |
} |
| 40 |
|
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
static bool |
| 45 |
fs_force_output_type(nir_builder *b, nir_intrinsic_instr *intrin, void *data) |
| 46 |
{ |
| 47 |
if (intrin->intrinsic != nir_intrinsic_store_output) |
| 48 |
return false; |
| 49 |
|
| 50 |
nir_io_semantics io = nir_intrinsic_io_semantics(intrin); |
| 51 |
if (io.location < FRAG_RESULT_DATA0 || FRAG_RESULT_DATA7 < io.location) |
| 52 |
return false; |
| 53 |
|
| 54 |
enum pipe_format *render_target_formats = (enum pipe_format *)data; |
| 55 |
enum pipe_format format = |
| 56 |
render_target_formats[io.location - FRAG_RESULT_DATA0]; |
| 57 |
nir_alu_type type = nir_intrinsic_src_type(intrin); |
| 58 |
if (util_format_is_float(format) || util_format_is_unorm(format)) { |
| 59 |
if (type & nir_type_uint) { |
| 60 |
b->cursor = nir_before_instr(&intrin->instr); |
| 61 |
nir_def *value = |
| 62 |
nir_u2fN(b, intrin->src[0].ssa, nir_src_bit_size(intrin->src[0])); |
| 63 |
nir_src_rewrite(&intrin->src[0], value); |
| 64 |
type ^= (nir_type_float | nir_type_uint); |
| 65 |
nir_intrinsic_set_src_type(intrin, type); |
| 66 |
return true; |
| 67 |
} else if (type & nir_type_int) { |
| 68 |
b->cursor = nir_before_instr(&intrin->instr); |
| 69 |
nir_def *value = |
| 70 |
nir_u2fN(b, intrin->src[0].ssa, nir_src_bit_size(intrin->src[0])); |
| 71 |
nir_src_rewrite(&intrin->src[0], value); |
| 72 |
type ^= (nir_type_float | nir_type_int); |
| 73 |
nir_intrinsic_set_src_type(intrin, type); |
| 74 |
return true; |
| 75 |
} |
| 76 |
} else if (util_format_is_pure_sint(format)) { |
| 77 |
if (type & nir_type_uint) { |
| 78 |
type ^= (nir_type_uint | nir_type_int); |
| 79 |
nir_intrinsic_set_src_type(intrin, type); |
| 80 |
return true; |
| 81 |
} else if (type & nir_type_float) { |
| 82 |
b->cursor = nir_before_instr(&intrin->instr); |
| 83 |
nir_def *value = |
| 84 |
nir_f2uN(b, intrin->src[0].ssa, nir_src_bit_size(intrin->src[0])); |
| 85 |
nir_src_rewrite(&intrin->src[0], value); |
| 86 |
type ^= (nir_type_float | nir_type_int); |
| 87 |
nir_intrinsic_set_src_type(intrin, type); |
| 88 |
return true; |
| 89 |
} |
| 90 |
} else if (util_format_is_pure_uint(format)) { |
| 91 |
if (type & nir_type_int) { |
| 92 |
type ^= (nir_type_int | nir_type_uint); |
| 93 |
nir_intrinsic_set_src_type(intrin, type); |
| 94 |
return true; |
| 95 |
} else if (type & nir_type_float) { |
| 96 |
b->cursor = nir_before_instr(&intrin->instr); |
| 97 |
nir_def *value = |
| 98 |
nir_f2uN(b, intrin->src[0].ssa, nir_src_bit_size(intrin->src[0])); |
| 99 |
nir_src_rewrite(&intrin->src[0], value); |
| 100 |
type ^= (nir_type_float | nir_type_uint); |
| 101 |
nir_intrinsic_set_src_type(intrin, type); |
| 102 |
return true; |
| 103 |
} |
| 104 |
} |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
bool |
| 109 |
msl_nir_fs_force_output_signedness( |
| 110 |
nir_shader *nir, enum pipe_format render_target_formats[MAX_DRAW_BUFFERS]) |
| 111 |
{ |
| 112 |
return nir_shader_intrinsics_pass(nir, fs_force_output_type, |
| 113 |
nir_metadata_control_flow, |
| 114 |
render_target_formats); |
| 115 |
} |
| 116 |
|
| 117 |
bool |
| 118 |
msl_lower_textures(nir_shader *nir) |
| 119 |
{ |
| 120 |
bool progress = false; |
| 121 |
nir_lower_tex_options lower_tex_options = { |
| 122 |
.lower_txp = ~0u, |
| 123 |
.lower_sampler_lod_bias = true, |
| 124 |
|
| 125 |
|
| 126 |
.lower_1d = true, |
| 127 |
|
| 128 |
|
| 129 |
.lower_tg4_offsets = true, |
| 130 |
|
| 131 |
|
| 132 |
.lower_txf_offset = true, |
| 133 |
.lower_txd_cube_map = true, |
| 134 |
}; |
| 135 |
|
| 136 |
NIR_PASS(progress, nir, nir_lower_tex, &lower_tex_options); |
| 137 |
return progress; |
| 138 |
} |
| 139 |
|
| 140 |
static bool |
| 141 |
replace_sample_id_for_sample_mask(nir_builder *b, nir_intrinsic_instr *intrin, |
| 142 |
void *data) |
| 143 |
{ |
| 144 |
if (intrin->intrinsic != nir_intrinsic_load_sample_mask_in) |
| 145 |
return false; |
| 146 |
|
| 147 |
nir_def_replace(nir_instr_def(&intrin->instr), (nir_def *)data); |
| 148 |
return true; |
| 149 |
} |
| 150 |
|
| 151 |
static bool |
| 152 |
msl_replace_load_sample_mask_in_for_static_sample_mask( |
| 153 |
nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 154 |
{ |
| 155 |
if (intr->intrinsic != nir_intrinsic_load_sample_mask_in) |
| 156 |
return false; |
| 157 |
|
| 158 |
b->cursor = nir_after_instr(&intr->instr); |
| 159 |
nir_def *static_sample_mask = (nir_def *)data; |
| 160 |
nir_def *sample_mask = nir_iand(b, &intr->def, static_sample_mask); |
| 161 |
nir_def_rewrite_uses_after(&intr->def, sample_mask); |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
bool |
| 166 |
msl_lower_static_sample_mask(nir_shader *nir, uint32_t sample_mask) |
| 167 |
{ |
| 168 |
|
| 169 |
assert(nir->info.stage == MESA_SHADER_FRAGMENT); |
| 170 |
|
| 171 |
|
| 172 |
nir_function_impl *entrypoint = nir_shader_get_entrypoint(nir); |
| 173 |
nir_builder b = nir_builder_at(nir_before_impl(entrypoint)); |
| 174 |
|
| 175 |
struct nir_io_semantics io_semantics = { |
| 176 |
.location = FRAG_RESULT_SAMPLE_MASK, |
| 177 |
.num_slots = 1u, |
| 178 |
}; |
| 179 |
nir_def *static_sample_mask = nir_imm_int(&b, sample_mask); |
| 180 |
nir_store_output(&b, nir_load_sample_mask_in(&b), nir_imm_int(&b, 0u), |
| 181 |
.base = 0u, .range = 1u, .write_mask = 0x1, .component = 0u, |
| 182 |
.src_type = nir_type_uint32, .io_semantics = io_semantics); |
| 183 |
BITSET_SET(nir->info.system_values_read, SYSTEM_VALUE_SAMPLE_MASK_IN); |
| 184 |
|
| 185 |
nir_shader_intrinsics_pass( |
| 186 |
nir, msl_replace_load_sample_mask_in_for_static_sample_mask, |
| 187 |
nir_metadata_control_flow, static_sample_mask); |
| 188 |
|
| 189 |
return true; |
| 190 |
} |
| 191 |
|
| 192 |
bool |
| 193 |
msl_ensure_depth_write(nir_shader *nir) |
| 194 |
{ |
| 195 |
assert(nir->info.stage == MESA_SHADER_FRAGMENT); |
| 196 |
|
| 197 |
bool has_depth_write = |
| 198 |
nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH); |
| 199 |
if (!has_depth_write) { |
| 200 |
nir_variable *depth_var = nir_create_variable_with_location( |
| 201 |
nir, nir_var_shader_out, FRAG_RESULT_DEPTH, glsl_float_type()); |
| 202 |
|
| 203 |
|
| 204 |
nir_function_impl *entrypoint = nir_shader_get_entrypoint(nir); |
| 205 |
nir_builder b = nir_builder_at(nir_before_impl(entrypoint)); |
| 206 |
|
| 207 |
nir_deref_instr *depth_deref = nir_build_deref_var(&b, depth_var); |
| 208 |
nir_def *position = nir_load_frag_coord(&b); |
| 209 |
nir_store_deref(&b, depth_deref, nir_channel(&b, position, 2u), |
| 210 |
0xFFFFFFFF); |
| 211 |
|
| 212 |
nir->info.outputs_written |= BITFIELD64_BIT(FRAG_RESULT_DEPTH); |
| 213 |
nir->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_ANY; |
| 214 |
return nir_progress(true, entrypoint, nir_metadata_control_flow); |
| 215 |
} |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
bool |
| 220 |
msl_ensure_vertex_position_output(nir_shader *nir) |
| 221 |
{ |
| 222 |
assert(nir->info.stage == MESA_SHADER_VERTEX); |
| 223 |
|
| 224 |
bool has_position_write = |
| 225 |
nir->info.outputs_written & BITFIELD64_BIT(VARYING_SLOT_POS); |
| 226 |
if (!has_position_write) { |
| 227 |
nir_variable *position_var = nir_create_variable_with_location( |
| 228 |
nir, nir_var_shader_out, VARYING_SLOT_POS, glsl_vec4_type()); |
| 229 |
|
| 230 |
|
| 231 |
nir_function_impl *entrypoint = nir_shader_get_entrypoint(nir); |
| 232 |
nir_builder b = nir_builder_at(nir_before_impl(entrypoint)); |
| 233 |
|
| 234 |
nir_deref_instr *position_deref = nir_build_deref_var(&b, position_var); |
| 235 |
nir_def *zero = nir_imm_float(&b, 0.0f); |
| 236 |
nir_store_deref(&b, position_deref, nir_vec4(&b, zero, zero, zero, zero), |
| 237 |
0xFFFFFFFF); |
| 238 |
|
| 239 |
nir->info.outputs_written |= BITFIELD64_BIT(VARYING_SLOT_POS); |
| 240 |
return nir_progress(true, entrypoint, nir_metadata_control_flow); |
| 241 |
} |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
static bool |
| 246 |
msl_fs_io_types(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 247 |
{ |
| 248 |
if (intr->intrinsic == nir_intrinsic_load_input) { |
| 249 |
struct nir_io_semantics io = nir_intrinsic_io_semantics(intr); |
| 250 |
if (io.location == VARYING_SLOT_VIEWPORT) { |
| 251 |
nir_intrinsic_set_dest_type(intr, nir_type_uint32); |
| 252 |
return true; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
if (intr->intrinsic == nir_intrinsic_store_output) { |
| 257 |
struct nir_io_semantics io = nir_intrinsic_io_semantics(intr); |
| 258 |
if (io.location == FRAG_RESULT_SAMPLE_MASK) { |
| 259 |
nir_intrinsic_set_src_type(intr, nir_type_uint32); |
| 260 |
return true; |
| 261 |
} else if (io.location == FRAG_RESULT_STENCIL) { |
| 262 |
nir_alu_type type = nir_intrinsic_src_type(intr); |
| 263 |
nir_intrinsic_set_src_type( |
| 264 |
intr, nir_type_uint | nir_alu_type_get_type_size(type)); |
| 265 |
return true; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
if (intr->intrinsic == nir_intrinsic_load_output) { |
| 270 |
struct nir_io_semantics io = nir_intrinsic_io_semantics(intr); |
| 271 |
if (io.location == FRAG_RESULT_STENCIL) { |
| 272 |
nir_alu_type type = nir_intrinsic_dest_type(intr); |
| 273 |
nir_intrinsic_set_dest_type( |
| 274 |
intr, nir_type_uint | nir_alu_type_get_type_size(type)); |
| 275 |
return true; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
return false; |
| 280 |
} |
| 281 |
|
| 282 |
bool |
| 283 |
msl_nir_fs_io_types(nir_shader *nir) |
| 284 |
{ |
| 285 |
assert(nir->info.stage == MESA_SHADER_FRAGMENT); |
| 286 |
return nir_shader_intrinsics_pass(nir, msl_fs_io_types, nir_metadata_all, |
| 287 |
NULL); |
| 288 |
} |
| 289 |
|
| 290 |
static bool |
| 291 |
msl_vs_io_types(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 292 |
{ |
| 293 |
if (intr->intrinsic == nir_intrinsic_store_output) { |
| 294 |
struct nir_io_semantics io = nir_intrinsic_io_semantics(intr); |
| 295 |
if (io.location == VARYING_SLOT_LAYER || |
| 296 |
io.location == VARYING_SLOT_VIEWPORT) { |
| 297 |
nir_intrinsic_set_src_type(intr, nir_type_uint32); |
| 298 |
return true; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
return false; |
| 303 |
} |
| 304 |
|
| 305 |
bool |
| 306 |
msl_nir_vs_io_types(nir_shader *nir) |
| 307 |
{ |
| 308 |
assert(nir->info.stage == MESA_SHADER_VERTEX); |
| 309 |
return nir_shader_intrinsics_pass(nir, msl_vs_io_types, nir_metadata_all, |
| 310 |
NULL); |
| 311 |
} |
| 312 |
|
| 313 |
static bool |
| 314 |
fake_guard_for_discards(nir_builder *b, nir_intrinsic_instr *intrin, void *data) |
| 315 |
{ |
| 316 |
if (intrin->intrinsic != nir_intrinsic_demote) |
| 317 |
return false; |
| 318 |
|
| 319 |
b->cursor = nir_before_instr(&intrin->instr); |
| 320 |
nir_def *helper = nir_is_helper_invocation(b, 1); |
| 321 |
nir_demote_if(b, nir_inot(b, helper)); |
| 322 |
nir_instr_remove(&intrin->instr); |
| 323 |
return true; |
| 324 |
} |
| 325 |
|
| 326 |
bool |
| 327 |
msl_nir_fake_guard_for_discards(struct nir_shader *nir) |
| 328 |
{ |
| 329 |
assert(nir->info.stage == MESA_SHADER_FRAGMENT); |
| 330 |
|
| 331 |
|
| 332 |
if (!nir->info.writes_memory) |
| 333 |
return false; |
| 334 |
|
| 335 |
return nir_shader_intrinsics_pass(nir, fake_guard_for_discards, |
| 336 |
nir_metadata_control_flow, NULL); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
static bool |
| 341 |
gather_fs_input_interpolant_usage(nir_builder *b, nir_intrinsic_instr *intr, |
| 342 |
void *data) |
| 343 |
{ |
| 344 |
if (intr->intrinsic != nir_intrinsic_load_interpolated_input) |
| 345 |
return false; |
| 346 |
|
| 347 |
bool *uses_interpolant = (bool *)data; |
| 348 |
struct nir_io_semantics io = nir_intrinsic_io_semantics(intr); |
| 349 |
nir_intrinsic_instr *interpolation = nir_src_as_intrinsic(intr->src[0]); |
| 350 |
bool at_sample = |
| 351 |
interpolation->intrinsic == nir_intrinsic_load_barycentric_at_sample; |
| 352 |
uses_interpolant[io.location] |= |
| 353 |
at_sample || |
| 354 |
interpolation->intrinsic == nir_intrinsic_load_barycentric_at_offset; |
| 355 |
return at_sample; |
| 356 |
} |
| 357 |
|
| 358 |
static bool |
| 359 |
lower_sample_shading(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 360 |
{ |
| 361 |
if (intr->intrinsic == nir_intrinsic_load_frag_coord) { |
| 362 |
b->cursor = nir_after_instr(&intr->instr); |
| 363 |
nir_def *offset = |
| 364 |
nir_fadd(b, nir_load_sample_pos_from_id(b, 32u, nir_load_sample_id(b)), |
| 365 |
nir_imm_vec2(b, -0.5f, -0.5f)); |
| 366 |
nir_def *sample_position = |
| 367 |
nir_fadd(b, &intr->def, nir_pad_vector_imm_int(b, offset, 0u, 4u)); |
| 368 |
nir_def_rewrite_uses_after(&intr->def, sample_position); |
| 369 |
return true; |
| 370 |
} |
| 371 |
|
| 372 |
if (intr->intrinsic != nir_intrinsic_load_interpolated_input) |
| 373 |
return false; |
| 374 |
|
| 375 |
bool *uses_interpolant = (bool *)data; |
| 376 |
struct nir_io_semantics io = nir_intrinsic_io_semantics(intr); |
| 377 |
nir_intrinsic_instr *interpolation = nir_src_as_intrinsic(intr->src[0]); |
| 378 |
if (!uses_interpolant[io.location] || |
| 379 |
interpolation->intrinsic != nir_intrinsic_load_barycentric_sample) |
| 380 |
return false; |
| 381 |
|
| 382 |
b->cursor = nir_before_instr(&intr->instr); |
| 383 |
nir_def *def = nir_load_barycentric_at_sample( |
| 384 |
b, intr->def.bit_size, nir_load_sample_id(b), |
| 385 |
.interp_mode = nir_intrinsic_interp_mode(interpolation)); |
| 386 |
nir_def_rewrite_uses_after(&interpolation->def, def); |
| 387 |
nir_instr_remove(&interpolation->instr); |
| 388 |
return false; |
| 389 |
} |
| 390 |
|
| 391 |
bool |
| 392 |
msl_nir_lower_sample_shading(nir_shader *nir) |
| 393 |
{ |
| 394 |
assert(nir->info.stage == MESA_SHADER_FRAGMENT); |
| 395 |
|
| 396 |
bool uses_interpolant[NUM_TOTAL_VARYING_SLOTS] = {}; |
| 397 |
|
| 398 |
if (nir_shader_intrinsics_pass(nir, gather_fs_input_interpolant_usage, |
| 399 |
nir_metadata_all, uses_interpolant)) |
| 400 |
BITSET_SET(nir->info.system_values_read, SYSTEM_VALUE_SAMPLE_ID); |
| 401 |
|
| 402 |
return nir_shader_intrinsics_pass( |
| 403 |
nir, lower_sample_shading, nir_metadata_control_flow, uses_interpolant); |
| 404 |
} |
| 405 |
|
| 406 |
static bool |
| 407 |
lower_clip_distance(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 408 |
{ |
| 409 |
if (intr->intrinsic != nir_intrinsic_store_output) |
| 410 |
return false; |
| 411 |
|
| 412 |
nir_io_semantics io = nir_intrinsic_io_semantics(intr); |
| 413 |
unsigned component = nir_intrinsic_component(intr); |
| 414 |
if (io.location != VARYING_SLOT_CLIP_DIST0 && |
| 415 |
io.location != VARYING_SLOT_CLIP_DIST1) |
| 416 |
return false; |
| 417 |
|
| 418 |
unsigned base = (io.location - VARYING_SLOT_CLIP_DIST0) * 4 + component; |
| 419 |
if (intr->intrinsic == nir_intrinsic_store_output) { |
| 420 |
b->cursor = nir_after_instr(&intr->instr); |
| 421 |
nir_store_clip_distance_kk(b, intr->src[0].ssa, .base = base); |
| 422 |
} |
| 423 |
return true; |
| 424 |
} |
| 425 |
|
| 426 |
bool |
| 427 |
msl_nir_lower_clip_distance(nir_shader *nir) |
| 428 |
{ |
| 429 |
return nir_shader_intrinsics_pass(nir, lower_clip_distance, nir_metadata_all, |
| 430 |
NULL); |
| 431 |
} |