| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include "nir_to_msl.h" |
| 8 |
#include "msl_private.h" |
| 9 |
#include "nir.h" |
| 10 |
|
| 11 |
static const char * |
| 12 |
get_stage_string(mesa_shader_stage stage) |
| 13 |
{ |
| 14 |
switch (stage) { |
| 15 |
case MESA_SHADER_VERTEX: |
| 16 |
return "vertex"; |
| 17 |
case MESA_SHADER_FRAGMENT: |
| 18 |
return "fragment"; |
| 19 |
case MESA_SHADER_COMPUTE: |
| 20 |
return "kernel"; |
| 21 |
default: |
| 22 |
assert(0); |
| 23 |
return ""; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
static const char * |
| 28 |
get_entrypoint_name(nir_shader *shader) |
| 29 |
{ |
| 30 |
return nir_shader_get_entrypoint(shader)->function->name; |
| 31 |
} |
| 32 |
|
| 33 |
static const char *sysval_table[SYSTEM_VALUE_MAX] = { |
| 34 |
[SYSTEM_VALUE_SUBGROUP_SIZE] = |
| 35 |
"uint gl_SubGroupSize [[threads_per_simdgroup]]", |
| 36 |
[SYSTEM_VALUE_SUBGROUP_INVOCATION] = |
| 37 |
"uint gl_SubGroupInvocation [[thread_index_in_simdgroup]]", |
| 38 |
[SYSTEM_VALUE_NUM_SUBGROUPS] = |
| 39 |
"uint gl_NumSubGroups [[simdgroups_per_threadgroup]]", |
| 40 |
[SYSTEM_VALUE_SUBGROUP_ID] = |
| 41 |
"uint gl_SubGroupID [[simdgroup_index_in_threadgroup]]", |
| 42 |
[SYSTEM_VALUE_WORKGROUP_ID] = |
| 43 |
"uint3 gl_WorkGroupID [[threadgroup_position_in_grid]]", |
| 44 |
[SYSTEM_VALUE_LOCAL_INVOCATION_ID] = |
| 45 |
"uint3 gl_LocalInvocationID [[thread_position_in_threadgroup]]", |
| 46 |
[SYSTEM_VALUE_GLOBAL_INVOCATION_ID] = |
| 47 |
"uint3 gl_GlobalInvocationID [[thread_position_in_grid]]", |
| 48 |
[SYSTEM_VALUE_NUM_WORKGROUPS] = |
| 49 |
"uint3 gl_NumWorkGroups [[threadgroups_per_grid]]", |
| 50 |
[SYSTEM_VALUE_LOCAL_INVOCATION_INDEX] = |
| 51 |
"uint gl_LocalInvocationIndex [[thread_index_in_threadgroup]]", |
| 52 |
[SYSTEM_VALUE_VERTEX_ID] = "uint gl_VertexID [[vertex_id]]", |
| 53 |
[SYSTEM_VALUE_INSTANCE_ID] = "uint gl_InstanceID [[instance_id]]", |
| 54 |
[SYSTEM_VALUE_BASE_INSTANCE] = "uint gl_BaseInstance [[base_instance]]", |
| 55 |
[SYSTEM_VALUE_FRAG_COORD] = "float4 gl_FragCoord [[position]]", |
| 56 |
[SYSTEM_VALUE_POINT_COORD] = "float2 gl_PointCoord [[point_coord]]", |
| 57 |
[SYSTEM_VALUE_FRONT_FACE] = "bool gl_FrontFacing [[front_facing]]", |
| 58 |
[SYSTEM_VALUE_LAYER_ID] = "uint gl_Layer [[render_target_array_index]]", |
| 59 |
[SYSTEM_VALUE_SAMPLE_ID] = "uint gl_SampleID [[sample_id]]", |
| 60 |
[SYSTEM_VALUE_SAMPLE_MASK_IN] = "uint gl_SampleMask [[sample_mask]]", |
| 61 |
[SYSTEM_VALUE_AMPLIFICATION_ID_KK] = |
| 62 |
"uint mtl_AmplificationID [[amplification_id]]", |
| 63 |
[SYSTEM_VALUE_FIRST_VERTEX] = "uint gl_FirstVertex [[base_vertex]]", |
| 64 |
}; |
| 65 |
|
| 66 |
static void |
| 67 |
emit_sysvals(struct nir_to_msl_ctx *ctx, nir_shader *shader) |
| 68 |
{ |
| 69 |
unsigned i; |
| 70 |
BITSET_FOREACH_SET(i, shader->info.system_values_read, SYSTEM_VALUE_MAX) { |
| 71 |
assert(sysval_table[i]); |
| 72 |
P_IND(ctx, "%s,\n", sysval_table[i]); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
emit_inputs(struct nir_to_msl_ctx *ctx, nir_shader *shader) |
| 78 |
{ |
| 79 |
switch (shader->info.stage) { |
| 80 |
case MESA_SHADER_FRAGMENT: |
| 81 |
P_IND(ctx, "FragmentIn in [[stage_in]],\n"); |
| 82 |
break; |
| 83 |
default: |
| 84 |
break; |
| 85 |
} |
| 86 |
P_IND(ctx, "constant Buffer &buf0 [[buffer(0)]],\n"); |
| 87 |
P_IND(ctx, "constant SamplerTable &sampler_table [[buffer(1)]]\n"); |
| 88 |
} |
| 89 |
|
| 90 |
static const char * |
| 91 |
output_type(nir_shader *shader) |
| 92 |
{ |
| 93 |
switch (shader->info.stage) { |
| 94 |
case MESA_SHADER_VERTEX: |
| 95 |
return "VertexOut"; |
| 96 |
case MESA_SHADER_FRAGMENT: |
| 97 |
return "FragmentOut"; |
| 98 |
default: |
| 99 |
return "void"; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
static void |
| 104 |
emit_local_vars(struct nir_to_msl_ctx *ctx, nir_shader *shader) |
| 105 |
{ |
| 106 |
if (shader->info.shared_size) { |
| 107 |
P_IND(ctx, "threadgroup char shared_data[%d];\n", |
| 108 |
shader->info.shared_size); |
| 109 |
} |
| 110 |
if (shader->scratch_size) { |
| 111 |
|
| 112 |
if (ctx->disabled_workarounds & BITFIELD64_BIT(1)) { |
| 113 |
P_IND(ctx, "uchar scratch[%d];\n", shader->scratch_size); |
| 114 |
} else { |
| 115 |
P_IND(ctx, "uchar scratch[%d] = {0};\n", shader->scratch_size); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
static bool |
| 121 |
is_register(nir_def *def) |
| 122 |
{ |
| 123 |
return ((nir_def_is_intrinsic(def)) && |
| 124 |
(nir_def_as_intrinsic(def)->intrinsic == nir_intrinsic_load_reg)); |
| 125 |
} |
| 126 |
|
| 127 |
static void |
| 128 |
writemask_to_msl(struct nir_to_msl_ctx *ctx, unsigned write_mask, |
| 129 |
unsigned num_components) |
| 130 |
{ |
| 131 |
if (num_components != util_bitcount(write_mask)) { |
| 132 |
P(ctx, "."); |
| 133 |
for (unsigned i = 0; i < num_components; i++) |
| 134 |
if ((write_mask >> i) & 1) |
| 135 |
P(ctx, "%c", "xyzw"[i]); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
static void |
| 140 |
src_to_msl(struct nir_to_msl_ctx *ctx, nir_src *src) |
| 141 |
{ |
| 142 |
|
| 143 |
const char *bitcast = msl_bitcast_for_src(ctx->types, src); |
| 144 |
if (nir_src_is_const(*src)) { |
| 145 |
msl_src_as_const(ctx, src); |
| 146 |
return; |
| 147 |
} |
| 148 |
if (nir_src_is_undef(*src)) { |
| 149 |
if (src->ssa->num_components == 1) { |
| 150 |
P(ctx, "00"); |
| 151 |
} else { |
| 152 |
P(ctx, "%s(", msl_type_for_src(ctx->types, src)); |
| 153 |
for (int i = 0; i < src->ssa->num_components; i++) { |
| 154 |
if (i) |
| 155 |
P(ctx, ", "); |
| 156 |
P(ctx, "00"); |
| 157 |
} |
| 158 |
P(ctx, ")"); |
| 159 |
} |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
if (bitcast) |
| 164 |
P(ctx, "as_type<%s>(", bitcast); |
| 165 |
if (is_register(src->ssa)) { |
| 166 |
nir_intrinsic_instr *instr = nir_def_as_intrinsic(src->ssa); |
| 167 |
if (src->ssa->bit_size != 1u) { |
| 168 |
P(ctx, "as_type<%s>(r%d)", msl_type_for_def(ctx->types, src->ssa), |
| 169 |
instr->src[0].ssa->index); |
| 170 |
} else { |
| 171 |
P(ctx, "%s(r%d)", msl_type_for_def(ctx->types, src->ssa), |
| 172 |
instr->src[0].ssa->index); |
| 173 |
} |
| 174 |
} else if (nir_src_is_const(*src)) { |
| 175 |
msl_src_as_const(ctx, src); |
| 176 |
} else { |
| 177 |
P(ctx, "t%d", src->ssa->index); |
| 178 |
} |
| 179 |
if (bitcast) |
| 180 |
P(ctx, ")"); |
| 181 |
} |
| 182 |
|
| 183 |
static void |
| 184 |
alu_src_to_msl(struct nir_to_msl_ctx *ctx, nir_alu_instr *instr, int srcn) |
| 185 |
{ |
| 186 |
nir_alu_src *src = &instr->src[srcn]; |
| 187 |
src_to_msl(ctx, &src->src); |
| 188 |
if (!nir_alu_has_trivial_src(instr, srcn) && |
| 189 |
src->src.ssa->num_components > 1) { |
| 190 |
int num_components = nir_src_num_components(src->src); |
| 191 |
assert(num_components <= 4); |
| 192 |
|
| 193 |
P(ctx, "."); |
| 194 |
for (int i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) { |
| 195 |
if (!nir_alu_instr_channel_used(instr, srcn, i)) |
| 196 |
continue; |
| 197 |
P(ctx, "%c", "xyzw"[src->swizzle[i]]); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
static void |
| 203 |
alu_funclike(struct nir_to_msl_ctx *ctx, nir_alu_instr *instr, const char *name) |
| 204 |
{ |
| 205 |
const nir_op_info *info = &nir_op_infos[instr->op]; |
| 206 |
P(ctx, "%s(", name); |
| 207 |
for (int i = 0; i < info->num_inputs; i++) { |
| 208 |
alu_src_to_msl(ctx, instr, i); |
| 209 |
if (i < info->num_inputs - 1) |
| 210 |
P(ctx, ", "); |
| 211 |
} |
| 212 |
P(ctx, ")"); |
| 213 |
} |
| 214 |
|
| 215 |
static void |
| 216 |
alu_to_msl(struct nir_to_msl_ctx *ctx, nir_alu_instr *instr) |
| 217 |
{ |
| 218 |
|
| 219 |
#define ALU_BINOP(op) \ |
| 220 |
do { \ |
| 221 |
alu_src_to_msl(ctx, instr, 0); \ |
| 222 |
P(ctx, " %s ", op); \ |
| 223 |
alu_src_to_msl(ctx, instr, 1); \ |
| 224 |
} while (0); |
| 225 |
|
| 226 |
switch (instr->op) { |
| 227 |
case nir_op_isign: |
| 228 |
alu_src_to_msl(ctx, instr, 0); |
| 229 |
P(ctx, " == 0 ? 0.0 : (("); |
| 230 |
alu_src_to_msl(ctx, instr, 0); |
| 231 |
P(ctx, " < 0) ? -1 : 1)"); |
| 232 |
break; |
| 233 |
case nir_op_iadd: |
| 234 |
case nir_op_fadd: |
| 235 |
ALU_BINOP("+"); |
| 236 |
break; |
| 237 |
case nir_op_uadd_sat: |
| 238 |
case nir_op_iadd_sat: |
| 239 |
alu_funclike(ctx, instr, "addsat"); |
| 240 |
break; |
| 241 |
case nir_op_isub: |
| 242 |
case nir_op_fsub: |
| 243 |
ALU_BINOP("-"); |
| 244 |
break; |
| 245 |
case nir_op_imul: |
| 246 |
case nir_op_fmul: |
| 247 |
ALU_BINOP("*"); |
| 248 |
break; |
| 249 |
case nir_op_idiv: |
| 250 |
case nir_op_udiv: |
| 251 |
case nir_op_fdiv: |
| 252 |
ALU_BINOP("/"); |
| 253 |
break; |
| 254 |
case nir_op_irem: |
| 255 |
ALU_BINOP("%"); |
| 256 |
break; |
| 257 |
case nir_op_ishl: |
| 258 |
ALU_BINOP("<<"); |
| 259 |
break; |
| 260 |
case nir_op_ishr: |
| 261 |
case nir_op_ushr: |
| 262 |
ALU_BINOP(">>"); |
| 263 |
break; |
| 264 |
case nir_op_ige: |
| 265 |
case nir_op_uge: |
| 266 |
case nir_op_fge: |
| 267 |
ALU_BINOP(">="); |
| 268 |
break; |
| 269 |
case nir_op_ilt: |
| 270 |
case nir_op_ult: |
| 271 |
case nir_op_flt: |
| 272 |
ALU_BINOP("<") |
| 273 |
break; |
| 274 |
case nir_op_iand: |
| 275 |
ALU_BINOP("&"); |
| 276 |
break; |
| 277 |
case nir_op_ior: |
| 278 |
ALU_BINOP("|"); |
| 279 |
break; |
| 280 |
case nir_op_ixor: |
| 281 |
ALU_BINOP("^"); |
| 282 |
break; |
| 283 |
case nir_op_bitfield_insert: |
| 284 |
alu_funclike(ctx, instr, "insert_bits"); |
| 285 |
break; |
| 286 |
case nir_op_ibitfield_extract: |
| 287 |
case nir_op_ubitfield_extract: |
| 288 |
alu_funclike(ctx, instr, "extract_bits"); |
| 289 |
break; |
| 290 |
case nir_op_bitfield_reverse: |
| 291 |
alu_funclike(ctx, instr, "reverse_bits"); |
| 292 |
break; |
| 293 |
case nir_op_bit_count: |
| 294 |
alu_funclike(ctx, instr, "popcount"); |
| 295 |
break; |
| 296 |
case nir_op_uclz: |
| 297 |
alu_funclike(ctx, instr, "clz"); |
| 298 |
break; |
| 299 |
case nir_op_ieq: |
| 300 |
case nir_op_feq: |
| 301 |
ALU_BINOP("=="); |
| 302 |
break; |
| 303 |
case nir_op_ine: |
| 304 |
case nir_op_fneu: |
| 305 |
ALU_BINOP("!="); |
| 306 |
break; |
| 307 |
case nir_op_umax: |
| 308 |
case nir_op_imax: |
| 309 |
alu_funclike(ctx, instr, "max"); |
| 310 |
break; |
| 311 |
case nir_op_umin: |
| 312 |
case nir_op_imin: |
| 313 |
alu_funclike(ctx, instr, "min"); |
| 314 |
break; |
| 315 |
case nir_op_umod: |
| 316 |
case nir_op_imod: |
| 317 |
ALU_BINOP("%"); |
| 318 |
break; |
| 319 |
case nir_op_imul_high: |
| 320 |
case nir_op_umul_high: |
| 321 |
alu_funclike(ctx, instr, "mulhi"); |
| 322 |
break; |
| 323 |
case nir_op_usub_sat: |
| 324 |
alu_funclike(ctx, instr, "subsat"); |
| 325 |
break; |
| 326 |
case nir_op_fsat: |
| 327 |
alu_funclike(ctx, instr, "saturate"); |
| 328 |
break; |
| 329 |
|
| 330 |
case nir_op_fisfinite: |
| 331 |
alu_funclike(ctx, instr, "isfinite"); |
| 332 |
break; |
| 333 |
case nir_op_fisnormal: |
| 334 |
alu_funclike(ctx, instr, "isnormal"); |
| 335 |
break; |
| 336 |
|
| 337 |
case nir_op_iabs: |
| 338 |
case nir_op_fabs: |
| 339 |
alu_funclike(ctx, instr, "abs"); |
| 340 |
break; |
| 341 |
case nir_op_fceil: |
| 342 |
alu_funclike(ctx, instr, "ceil"); |
| 343 |
break; |
| 344 |
case nir_op_fcos: |
| 345 |
alu_funclike(ctx, instr, "cos"); |
| 346 |
break; |
| 347 |
case nir_op_fdot2: |
| 348 |
case nir_op_fdot3: |
| 349 |
case nir_op_fdot4: |
| 350 |
alu_funclike(ctx, instr, "dot"); |
| 351 |
break; |
| 352 |
case nir_op_fexp2: |
| 353 |
alu_funclike(ctx, instr, "exp2"); |
| 354 |
break; |
| 355 |
case nir_op_ffloor: |
| 356 |
alu_funclike(ctx, instr, "floor"); |
| 357 |
break; |
| 358 |
case nir_op_ffma: |
| 359 |
alu_funclike(ctx, instr, "fma"); |
| 360 |
break; |
| 361 |
case nir_op_ffract: |
| 362 |
alu_funclike(ctx, instr, "fract"); |
| 363 |
break; |
| 364 |
case nir_op_flog2: |
| 365 |
alu_funclike(ctx, instr, "log2"); |
| 366 |
break; |
| 367 |
case nir_op_flrp: |
| 368 |
alu_funclike(ctx, instr, "mix"); |
| 369 |
break; |
| 370 |
case nir_op_fmax: |
| 371 |
alu_funclike(ctx, instr, "fmax"); |
| 372 |
break; |
| 373 |
case nir_op_fmin: |
| 374 |
alu_funclike(ctx, instr, "fmin"); |
| 375 |
break; |
| 376 |
case nir_op_frem: |
| 377 |
alu_funclike(ctx, instr, "fmod"); |
| 378 |
break; |
| 379 |
case nir_op_fpow: |
| 380 |
alu_funclike(ctx, instr, "pow"); |
| 381 |
break; |
| 382 |
case nir_op_fround_even: |
| 383 |
alu_funclike(ctx, instr, "rint"); |
| 384 |
break; |
| 385 |
case nir_op_frsq: |
| 386 |
alu_funclike(ctx, instr, "rsqrt"); |
| 387 |
break; |
| 388 |
case nir_op_fsign: |
| 389 |
alu_funclike(ctx, instr, "sign"); |
| 390 |
break; |
| 391 |
case nir_op_fsqrt: |
| 392 |
alu_funclike(ctx, instr, "sqrt"); |
| 393 |
break; |
| 394 |
case nir_op_fsin: |
| 395 |
alu_funclike(ctx, instr, "sin"); |
| 396 |
break; |
| 397 |
case nir_op_ldexp: |
| 398 |
alu_funclike(ctx, instr, "ldexp"); |
| 399 |
break; |
| 400 |
case nir_op_ftrunc: |
| 401 |
alu_funclike(ctx, instr, "trunc"); |
| 402 |
break; |
| 403 |
case nir_op_pack_snorm_4x8: |
| 404 |
alu_funclike(ctx, instr, "pack_float_to_snorm4x8"); |
| 405 |
break; |
| 406 |
case nir_op_pack_unorm_4x8: |
| 407 |
alu_funclike(ctx, instr, "pack_float_to_unorm4x8"); |
| 408 |
break; |
| 409 |
case nir_op_pack_snorm_2x16: |
| 410 |
alu_funclike(ctx, instr, "pack_float_to_snorm2x16"); |
| 411 |
break; |
| 412 |
case nir_op_pack_unorm_2x16: |
| 413 |
alu_funclike(ctx, instr, "pack_float_to_unorm2x16"); |
| 414 |
break; |
| 415 |
case nir_op_unpack_snorm_4x8: |
| 416 |
alu_funclike(ctx, instr, "unpack_snorm4x8_to_float"); |
| 417 |
break; |
| 418 |
case nir_op_unpack_unorm_4x8: |
| 419 |
alu_funclike(ctx, instr, "unpack_unorm4x8_to_float"); |
| 420 |
break; |
| 421 |
case nir_op_unpack_snorm_2x16: |
| 422 |
alu_funclike(ctx, instr, "unpack_snorm2x16_to_float"); |
| 423 |
break; |
| 424 |
case nir_op_unpack_unorm_2x16: |
| 425 |
alu_funclike(ctx, instr, "unpack_unorm2x16_to_float"); |
| 426 |
break; |
| 427 |
case nir_op_vec2: |
| 428 |
case nir_op_vec3: |
| 429 |
case nir_op_vec4: |
| 430 |
case nir_op_b2b1: |
| 431 |
case nir_op_b2b32: |
| 432 |
case nir_op_b2i8: |
| 433 |
case nir_op_b2i16: |
| 434 |
case nir_op_b2i32: |
| 435 |
case nir_op_b2i64: |
| 436 |
case nir_op_b2f16: |
| 437 |
case nir_op_i2f16: |
| 438 |
case nir_op_u2f16: |
| 439 |
case nir_op_i2f32: |
| 440 |
case nir_op_u2f32: |
| 441 |
case nir_op_i2i8: |
| 442 |
case nir_op_i2i16: |
| 443 |
case nir_op_i2i32: |
| 444 |
case nir_op_i2i64: |
| 445 |
case nir_op_f2i8: |
| 446 |
case nir_op_f2i16: |
| 447 |
case nir_op_f2i32: |
| 448 |
case nir_op_f2i64: |
| 449 |
case nir_op_f2u8: |
| 450 |
case nir_op_f2u16: |
| 451 |
case nir_op_f2u32: |
| 452 |
case nir_op_f2u64: |
| 453 |
case nir_op_u2u8: |
| 454 |
case nir_op_u2u16: |
| 455 |
case nir_op_u2u32: |
| 456 |
case nir_op_u2u64: |
| 457 |
case nir_op_f2f16: |
| 458 |
case nir_op_f2f16_rtne: |
| 459 |
case nir_op_f2f32: |
| 460 |
alu_funclike(ctx, instr, msl_type_for_def(ctx->types, &instr->def)); |
| 461 |
break; |
| 462 |
case nir_op_frcp: |
| 463 |
P(ctx, "1/"); |
| 464 |
alu_src_to_msl(ctx, instr, 0); |
| 465 |
break; |
| 466 |
case nir_op_inot: |
| 467 |
if (instr->src[0].src.ssa->bit_size == 1) { |
| 468 |
P(ctx, "!"); |
| 469 |
} else { |
| 470 |
P(ctx, "~"); |
| 471 |
} |
| 472 |
alu_src_to_msl(ctx, instr, 0); |
| 473 |
break; |
| 474 |
case nir_op_ineg: |
| 475 |
case nir_op_fneg: |
| 476 |
P(ctx, "-"); |
| 477 |
alu_src_to_msl(ctx, instr, 0); |
| 478 |
break; |
| 479 |
case nir_op_mov: |
| 480 |
alu_src_to_msl(ctx, instr, 0); |
| 481 |
break; |
| 482 |
case nir_op_b2f32: |
| 483 |
alu_src_to_msl(ctx, instr, 0); |
| 484 |
P(ctx, " ? 1.0 : 0.0"); |
| 485 |
break; |
| 486 |
case nir_op_bcsel: |
| 487 |
alu_src_to_msl(ctx, instr, 0); |
| 488 |
P(ctx, " ? "); |
| 489 |
alu_src_to_msl(ctx, instr, 1); |
| 490 |
P(ctx, " : "); |
| 491 |
alu_src_to_msl(ctx, instr, 2); |
| 492 |
break; |
| 493 |
default: |
| 494 |
P(ctx, "ALU %s", nir_op_infos[instr->op].name); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
static const char * |
| 499 |
texture_dim(enum glsl_sampler_dim dim) |
| 500 |
{ |
| 501 |
switch (dim) { |
| 502 |
case GLSL_SAMPLER_DIM_1D: |
| 503 |
return "1d"; |
| 504 |
case GLSL_SAMPLER_DIM_2D: |
| 505 |
return "2d"; |
| 506 |
case GLSL_SAMPLER_DIM_3D: |
| 507 |
return "3d"; |
| 508 |
case GLSL_SAMPLER_DIM_CUBE: |
| 509 |
return "cube"; |
| 510 |
case GLSL_SAMPLER_DIM_BUF: |
| 511 |
return "_buffer"; |
| 512 |
case GLSL_SAMPLER_DIM_MS: |
| 513 |
return "2d_ms"; |
| 514 |
default: |
| 515 |
fprintf(stderr, "Bad texture dim %d\n", dim); |
| 516 |
assert(!"Bad texture dimension"); |
| 517 |
return "BAD"; |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
static const char * |
| 522 |
tex_type_name(nir_alu_type ty) |
| 523 |
{ |
| 524 |
switch (ty) { |
| 525 |
case nir_type_int16: |
| 526 |
return "short"; |
| 527 |
case nir_type_int32: |
| 528 |
return "int"; |
| 529 |
case nir_type_uint16: |
| 530 |
return "ushort"; |
| 531 |
case nir_type_uint32: |
| 532 |
return "uint"; |
| 533 |
case nir_type_float16: |
| 534 |
return "half"; |
| 535 |
case nir_type_float32: |
| 536 |
return "float"; |
| 537 |
default: |
| 538 |
return "BAD"; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
static bool |
| 543 |
instrinsic_needs_dest_type(nir_intrinsic_instr *instr) |
| 544 |
{ |
| 545 |
const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic]; |
| 546 |
nir_intrinsic_op op = instr->intrinsic; |
| 547 |
if (op == nir_intrinsic_decl_reg || op == nir_intrinsic_load_reg || |
| 548 |
op == nir_intrinsic_load_texture_handle_kk || |
| 549 |
op == nir_intrinsic_load_depth_texture_kk || |
| 550 |
|
| 551 |
op == nir_intrinsic_global_atomic_swap || |
| 552 |
op == nir_intrinsic_shared_atomic_swap || |
| 553 |
op == nir_intrinsic_bindless_image_atomic_swap) |
| 554 |
return false; |
| 555 |
return info->has_dest; |
| 556 |
} |
| 557 |
|
| 558 |
static const char * |
| 559 |
msl_pipe_format_to_msl_type(enum pipe_format format) |
| 560 |
{ |
| 561 |
switch (format) { |
| 562 |
case PIPE_FORMAT_R16_FLOAT: |
| 563 |
return "half"; |
| 564 |
case PIPE_FORMAT_R32_FLOAT: |
| 565 |
return "float"; |
| 566 |
case PIPE_FORMAT_R8_UINT: |
| 567 |
return "uchar"; |
| 568 |
case PIPE_FORMAT_R16_UINT: |
| 569 |
return "ushort"; |
| 570 |
case PIPE_FORMAT_R32_UINT: |
| 571 |
return "uint"; |
| 572 |
case PIPE_FORMAT_R64_UINT: |
| 573 |
return "unsigned long"; |
| 574 |
case PIPE_FORMAT_R8_SINT: |
| 575 |
return "char"; |
| 576 |
case PIPE_FORMAT_R16_SINT: |
| 577 |
return "short"; |
| 578 |
case PIPE_FORMAT_R32_SINT: |
| 579 |
return "int"; |
| 580 |
case PIPE_FORMAT_R64_SINT: |
| 581 |
return "long"; |
| 582 |
default: |
| 583 |
assert(0); |
| 584 |
return ""; |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
static const char * |
| 589 |
component_str(uint8_t num_components) |
| 590 |
{ |
| 591 |
switch (num_components) { |
| 592 |
default: |
| 593 |
case 1: |
| 594 |
return ""; |
| 595 |
case 2: |
| 596 |
return "2"; |
| 597 |
case 3: |
| 598 |
return "3"; |
| 599 |
case 4: |
| 600 |
return "4"; |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
static void |
| 605 |
round_src_component_to_uint(struct nir_to_msl_ctx *ctx, nir_src *src, |
| 606 |
char component) |
| 607 |
{ |
| 608 |
bool is_float = msl_src_is_float(ctx, src); |
| 609 |
if (is_float) { |
| 610 |
P(ctx, "uint(max(int(rint("); |
| 611 |
} |
| 612 |
src_to_msl(ctx, src); |
| 613 |
P(ctx, ".%c", component); |
| 614 |
if (is_float) { |
| 615 |
P(ctx, ")), int(0)))"); |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
static void |
| 620 |
texture_src_coord_swizzle(struct nir_to_msl_ctx *ctx, nir_src *coord, |
| 621 |
uint32_t num_components, bool is_cube, bool is_array) |
| 622 |
{ |
| 623 |
src_to_msl(ctx, coord); |
| 624 |
|
| 625 |
uint32_t coord_components = |
| 626 |
num_components - (uint32_t)is_array - (uint32_t)is_cube; |
| 627 |
if (coord_components < coord->ssa->num_components) { |
| 628 |
const char *swizzle = "xyzw"; |
| 629 |
uint32_t i = 0; |
| 630 |
P(ctx, "."); |
| 631 |
for (i = 0; i < coord_components; i++) |
| 632 |
P(ctx, "%c", swizzle[i]); |
| 633 |
|
| 634 |
if (is_cube) { |
| 635 |
P(ctx, ", "); |
| 636 |
round_src_component_to_uint(ctx, coord, swizzle[i++]); |
| 637 |
} |
| 638 |
if (is_array) { |
| 639 |
P(ctx, ", "); |
| 640 |
round_src_component_to_uint(ctx, coord, swizzle[i++]); |
| 641 |
} |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
static void |
| 646 |
image_coord_swizzle(struct nir_to_msl_ctx *ctx, nir_intrinsic_instr *instr) |
| 647 |
{ |
| 648 |
unsigned comps = 0; |
| 649 |
bool is_array = nir_intrinsic_image_array(instr); |
| 650 |
bool is_cube = false; |
| 651 |
switch (nir_intrinsic_image_dim(instr)) { |
| 652 |
case GLSL_SAMPLER_DIM_BUF: |
| 653 |
case GLSL_SAMPLER_DIM_1D: |
| 654 |
comps = 1; |
| 655 |
break; |
| 656 |
case GLSL_SAMPLER_DIM_2D: |
| 657 |
case GLSL_SAMPLER_DIM_MS: |
| 658 |
comps = 2; |
| 659 |
break; |
| 660 |
case GLSL_SAMPLER_DIM_3D: |
| 661 |
comps = 3; |
| 662 |
break; |
| 663 |
case GLSL_SAMPLER_DIM_CUBE: |
| 664 |
comps = 3; |
| 665 |
is_cube = true; |
| 666 |
break; |
| 667 |
default: |
| 668 |
assert(!"Bad dimension for image"); |
| 669 |
break; |
| 670 |
} |
| 671 |
if (is_array) |
| 672 |
comps += 1; |
| 673 |
|
| 674 |
texture_src_coord_swizzle(ctx, &instr->src[1], comps, is_cube, is_array); |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
static void |
| 681 |
src_to_packed(struct nir_to_msl_ctx *ctx, nir_src *src, const char *type, |
| 682 |
uint32_t component_count) |
| 683 |
{ |
| 684 |
if (component_count == 1) { |
| 685 |
P(ctx, "%s(", type); |
| 686 |
} else { |
| 687 |
P(ctx, "packed_%s(", type); |
| 688 |
} |
| 689 |
src_to_msl(ctx, src); |
| 690 |
P(ctx, ")"); |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
static void |
| 698 |
src_to_packed_load(struct nir_to_msl_ctx *ctx, nir_src *src, |
| 699 |
const char *addressing, const char *type, |
| 700 |
uint32_t component_count) |
| 701 |
{ |
| 702 |
if (component_count == 1) { |
| 703 |
P(ctx, "*(%s %s*)(", addressing, type); |
| 704 |
} else { |
| 705 |
P(ctx, "%s(*(%s packed_%s*)", type, addressing, type); |
| 706 |
} |
| 707 |
src_to_msl(ctx, src); |
| 708 |
P(ctx, ")"); |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
static void |
| 716 |
src_to_packed_load_offset(struct nir_to_msl_ctx *ctx, nir_src *src, |
| 717 |
nir_src *offset, const char *addressing, |
| 718 |
const char *type, uint32_t component_count) |
| 719 |
{ |
| 720 |
if (component_count == 1) { |
| 721 |
P(ctx, "*(%s %s*)((", addressing, type); |
| 722 |
} else { |
| 723 |
P(ctx, "%s(*(%s packed_%s*)(", type, addressing, type); |
| 724 |
} |
| 725 |
src_to_msl(ctx, src); |
| 726 |
P(ctx, " + "); |
| 727 |
src_to_msl(ctx, offset); |
| 728 |
P(ctx, "))"); |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
static void |
| 735 |
src_to_packed_store(struct nir_to_msl_ctx *ctx, nir_src *src, |
| 736 |
const char *addressing, const char *type, |
| 737 |
uint32_t num_components) |
| 738 |
{ |
| 739 |
if (num_components == 1) { |
| 740 |
P_IND(ctx, "*(%s %s*)", addressing, type); |
| 741 |
} else { |
| 742 |
P_IND(ctx, "*(%s packed_%s*)", addressing, type); |
| 743 |
} |
| 744 |
src_to_msl(ctx, src); |
| 745 |
} |
| 746 |
|
| 747 |
static const char * |
| 748 |
atomic_op_to_msl(nir_atomic_op op) |
| 749 |
{ |
| 750 |
switch (op) { |
| 751 |
case nir_atomic_op_iadd: |
| 752 |
case nir_atomic_op_fadd: |
| 753 |
return "atomic_fetch_add"; |
| 754 |
case nir_atomic_op_umin: |
| 755 |
case nir_atomic_op_imin: |
| 756 |
case nir_atomic_op_fmin: |
| 757 |
return "atomic_fetch_min"; |
| 758 |
case nir_atomic_op_umax: |
| 759 |
case nir_atomic_op_imax: |
| 760 |
case nir_atomic_op_fmax: |
| 761 |
return "atomic_fetch_max"; |
| 762 |
case nir_atomic_op_iand: |
| 763 |
return "atomic_fetch_and"; |
| 764 |
case nir_atomic_op_ior: |
| 765 |
return "atomic_fetch_or"; |
| 766 |
case nir_atomic_op_ixor: |
| 767 |
return "atomic_fetch_xor"; |
| 768 |
case nir_atomic_op_xchg: |
| 769 |
return "atomic_exchange"; |
| 770 |
case nir_atomic_op_cmpxchg: |
| 771 |
case nir_atomic_op_fcmpxchg: |
| 772 |
return "atomic_compare_exchange_weak"; |
| 773 |
default: |
| 774 |
UNREACHABLE("Unhandled atomic op"); |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
static void |
| 779 |
atomic_to_msl(struct nir_to_msl_ctx *ctx, nir_intrinsic_instr *instr, |
| 780 |
const char *scope, bool shared) |
| 781 |
{ |
| 782 |
const char *atomic_op = atomic_op_to_msl(nir_intrinsic_atomic_op(instr)); |
| 783 |
const char *mem_order = "memory_order_relaxed"; |
| 784 |
|
| 785 |
P(ctx, "%s_explicit((%s atomic_%s*)", atomic_op, scope, |
| 786 |
msl_type_for_def(ctx->types, &instr->def)); |
| 787 |
if (shared) |
| 788 |
P(ctx, "&shared_data["); |
| 789 |
src_to_msl(ctx, &instr->src[0]); |
| 790 |
if (shared) |
| 791 |
P(ctx, "]"); |
| 792 |
P(ctx, ", "); |
| 793 |
src_to_msl(ctx, &instr->src[1]); |
| 794 |
P(ctx, ", %s", mem_order); |
| 795 |
P(ctx, ");\n"); |
| 796 |
} |
| 797 |
|
| 798 |
static void |
| 799 |
atomic_swap_to_msl(struct nir_to_msl_ctx *ctx, nir_intrinsic_instr *instr, |
| 800 |
const char *scope, bool shared) |
| 801 |
{ |
| 802 |
const char *atomic_op = atomic_op_to_msl(nir_intrinsic_atomic_op(instr)); |
| 803 |
const char *mem_order = "memory_order_relaxed"; |
| 804 |
const char *type = msl_type_for_def(ctx->types, &instr->def); |
| 805 |
|
| 806 |
P_IND(ctx, "%s ta%d = ", type, instr->def.index); |
| 807 |
src_to_msl(ctx, &instr->src[1]); |
| 808 |
P(ctx, "; %s_explicit((%s atomic_%s*)", atomic_op, scope, type); |
| 809 |
if (shared) |
| 810 |
P(ctx, "&shared_data["); |
| 811 |
src_to_msl(ctx, &instr->src[0]); |
| 812 |
if (shared) |
| 813 |
P(ctx, "]"); |
| 814 |
P(ctx, ", "); |
| 815 |
P(ctx, "&ta%d, ", instr->def.index); |
| 816 |
src_to_msl(ctx, &instr->src[2]); |
| 817 |
P(ctx, ", %s, %s);", mem_order, mem_order); |
| 818 |
P(ctx, "%s t%d = ta%d;\n", type, instr->def.index, instr->def.index); |
| 819 |
} |
| 820 |
|
| 821 |
static void |
| 822 |
memory_modes_to_msl(struct nir_to_msl_ctx *ctx, nir_variable_mode modes) |
| 823 |
{ |
| 824 |
bool requires_or = false; |
| 825 |
u_foreach_bit(i, modes) { |
| 826 |
nir_variable_mode single_mode = (1 << i); |
| 827 |
if (requires_or) |
| 828 |
P(ctx, " | "); |
| 829 |
switch (single_mode) { |
| 830 |
case nir_var_image: |
| 831 |
P(ctx, "mem_flags::mem_texture"); |
| 832 |
break; |
| 833 |
case nir_var_mem_ssbo: |
| 834 |
case nir_var_mem_global: |
| 835 |
P(ctx, "mem_flags::mem_device"); |
| 836 |
break; |
| 837 |
case nir_var_function_temp: |
| 838 |
P(ctx, "mem_flags::mem_none"); |
| 839 |
break; |
| 840 |
case nir_var_mem_shared: |
| 841 |
P(ctx, "mem_flags::mem_threadgroup"); |
| 842 |
break; |
| 843 |
default: |
| 844 |
UNREACHABLE("bad_memory_mode"); |
| 845 |
} |
| 846 |
requires_or = true; |
| 847 |
} |
| 848 |
if (!requires_or) |
| 849 |
P(ctx, "mem_flags::mem_none"); |
| 850 |
} |
| 851 |
|
| 852 |
static void |
| 853 |
msl_interpolant_method(struct nir_to_msl_ctx *ctx, nir_src *src) |
| 854 |
{ |
| 855 |
nir_intrinsic_instr *interpolation_intrin = nir_src_as_intrinsic(*src); |
| 856 |
switch (interpolation_intrin->intrinsic) { |
| 857 |
case nir_intrinsic_load_barycentric_at_offset: |
| 858 |
P(ctx, ".interpolate_at_offset(") |
| 859 |
src_to_msl(ctx, &interpolation_intrin->src[0u]); |
| 860 |
|
| 861 |
|
| 862 |
P(ctx, " + 0.5f)"); |
| 863 |
break; |
| 864 |
case nir_intrinsic_load_barycentric_centroid: |
| 865 |
P(ctx, ".interpolate_at_centroid()"); |
| 866 |
break; |
| 867 |
case nir_intrinsic_load_barycentric_at_sample: |
| 868 |
P(ctx, ".interpolate_at_sample("); |
| 869 |
src_to_msl(ctx, &interpolation_intrin->src[0u]); |
| 870 |
P(ctx, ")"); |
| 871 |
break; |
| 872 |
case nir_intrinsic_load_barycentric_pixel: |
| 873 |
P(ctx, ".interpolate_at_center()"); |
| 874 |
break; |
| 875 |
default: |
| 876 |
break; |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
static void |
| 881 |
intrinsic_to_msl(struct nir_to_msl_ctx *ctx, nir_intrinsic_instr *instr) |
| 882 |
{ |
| 883 |
|
| 884 |
|
| 885 |
if (instr->intrinsic == nir_intrinsic_load_barycentric_pixel || |
| 886 |
instr->intrinsic == nir_intrinsic_load_barycentric_centroid || |
| 887 |
instr->intrinsic == nir_intrinsic_load_barycentric_sample || |
| 888 |
instr->intrinsic == nir_intrinsic_load_barycentric_at_sample || |
| 889 |
instr->intrinsic == nir_intrinsic_load_barycentric_at_offset) |
| 890 |
return; |
| 891 |
|
| 892 |
const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic]; |
| 893 |
if (instrinsic_needs_dest_type(instr)) { |
| 894 |
P_IND(ctx, "t%d = ", instr->def.index); |
| 895 |
} |
| 896 |
switch (instr->intrinsic) { |
| 897 |
case nir_intrinsic_decl_reg: { |
| 898 |
const char *reg_type = msl_uint_type(nir_intrinsic_bit_size(instr), |
| 899 |
nir_intrinsic_num_components(instr)); |
| 900 |
P_IND(ctx, "%s r%d = %s(0);\n", reg_type, instr->def.index, reg_type); |
| 901 |
} break; |
| 902 |
case nir_intrinsic_load_reg: |
| 903 |
|
| 904 |
break; |
| 905 |
case nir_intrinsic_store_reg: |
| 906 |
P_IND(ctx, "r%d", instr->src[1].ssa->index); |
| 907 |
writemask_to_msl(ctx, nir_intrinsic_write_mask(instr), |
| 908 |
instr->num_components); |
| 909 |
|
| 910 |
|
| 911 |
if (instr->src[0].ssa->bit_size == 1u) { |
| 912 |
P(ctx, " = bool%s((", component_str(instr->num_components)); |
| 913 |
} else if (nir_src_is_const(instr->src[0])) { |
| 914 |
|
| 915 |
if (instr->src[0].ssa->num_components > 1) { |
| 916 |
P(ctx, " = as_type<%s>((", |
| 917 |
msl_uint_type(instr->src[0].ssa->bit_size, |
| 918 |
instr->src[0].ssa->num_components)); |
| 919 |
} else { |
| 920 |
P(ctx, " = as_type<%s>(%s(", |
| 921 |
msl_uint_type(instr->src[0].ssa->bit_size, |
| 922 |
instr->src[0].ssa->num_components), |
| 923 |
msl_type_for_src(ctx->types, &instr->src[0])); |
| 924 |
} |
| 925 |
} else { |
| 926 |
P(ctx, " = as_type<%s>((", |
| 927 |
msl_uint_type(instr->src[0].ssa->bit_size, |
| 928 |
instr->src[0].ssa->num_components)); |
| 929 |
} |
| 930 |
src_to_msl(ctx, &instr->src[0]); |
| 931 |
P(ctx, "));\n"); |
| 932 |
break; |
| 933 |
case nir_intrinsic_load_subgroup_size: |
| 934 |
P(ctx, "gl_SubGroupSize;\n"); |
| 935 |
break; |
| 936 |
case nir_intrinsic_load_subgroup_invocation: |
| 937 |
P(ctx, "gl_SubGroupInvocation;\n"); |
| 938 |
break; |
| 939 |
case nir_intrinsic_load_num_subgroups: |
| 940 |
P(ctx, "gl_NumSubGroups;\n"); |
| 941 |
break; |
| 942 |
case nir_intrinsic_load_subgroup_id: |
| 943 |
P(ctx, "gl_SubGroupID;\n"); |
| 944 |
break; |
| 945 |
case nir_intrinsic_load_workgroup_id: |
| 946 |
P(ctx, "gl_WorkGroupID;\n"); |
| 947 |
break; |
| 948 |
case nir_intrinsic_load_local_invocation_id: |
| 949 |
P(ctx, "gl_LocalInvocationID;\n"); |
| 950 |
break; |
| 951 |
case nir_intrinsic_load_global_invocation_id: |
| 952 |
P(ctx, "gl_GlobalInvocationID;\n"); |
| 953 |
break; |
| 954 |
case nir_intrinsic_load_num_workgroups: |
| 955 |
P(ctx, "gl_NumWorkGroups;\n"); |
| 956 |
break; |
| 957 |
case nir_intrinsic_load_local_invocation_index: |
| 958 |
P(ctx, "gl_LocalInvocationIndex;\n"); |
| 959 |
break; |
| 960 |
case nir_intrinsic_load_frag_coord: |
| 961 |
P(ctx, "gl_FragCoord;\n"); |
| 962 |
break; |
| 963 |
case nir_intrinsic_load_point_coord: |
| 964 |
P(ctx, "gl_PointCoord;\n"); |
| 965 |
break; |
| 966 |
case nir_intrinsic_load_first_vertex: |
| 967 |
P(ctx, "gl_FirstVertex;\n"); |
| 968 |
break; |
| 969 |
case nir_intrinsic_load_vertex_id: |
| 970 |
P(ctx, "gl_VertexID;\n"); |
| 971 |
break; |
| 972 |
case nir_intrinsic_load_instance_id: |
| 973 |
P(ctx, "gl_InstanceID;\n"); |
| 974 |
break; |
| 975 |
case nir_intrinsic_load_base_instance: |
| 976 |
P(ctx, "gl_BaseInstance;\n"); |
| 977 |
break; |
| 978 |
|
| 979 |
|
| 980 |
case nir_intrinsic_load_helper_invocation: |
| 981 |
case nir_intrinsic_is_helper_invocation: |
| 982 |
P(ctx, "simd_is_helper_thread();\n"); |
| 983 |
break; |
| 984 |
case nir_intrinsic_ddx: |
| 985 |
case nir_intrinsic_ddx_coarse: |
| 986 |
case nir_intrinsic_ddx_fine: |
| 987 |
P(ctx, "dfdx("); |
| 988 |
src_to_msl(ctx, &instr->src[0]); |
| 989 |
P(ctx, ");\n"); |
| 990 |
break; |
| 991 |
case nir_intrinsic_ddy: |
| 992 |
case nir_intrinsic_ddy_coarse: |
| 993 |
case nir_intrinsic_ddy_fine: |
| 994 |
P(ctx, "dfdy("); |
| 995 |
src_to_msl(ctx, &instr->src[0]); |
| 996 |
P(ctx, ");\n"); |
| 997 |
break; |
| 998 |
case nir_intrinsic_load_front_face: |
| 999 |
P(ctx, "gl_FrontFacing;\n"); |
| 1000 |
break; |
| 1001 |
case nir_intrinsic_load_layer_id: |
| 1002 |
P(ctx, "gl_Layer;\n"); |
| 1003 |
break; |
| 1004 |
case nir_intrinsic_load_sample_id: |
| 1005 |
P(ctx, "gl_SampleID;\n"); |
| 1006 |
break; |
| 1007 |
case nir_intrinsic_load_sample_mask_in: |
| 1008 |
P(ctx, "gl_SampleMask & (1u << gl_SampleID);\n"); |
| 1009 |
break; |
| 1010 |
case nir_intrinsic_load_sample_pos: |
| 1011 |
P(ctx, "get_sample_position(gl_SampleID);\n"); |
| 1012 |
break; |
| 1013 |
case nir_intrinsic_load_amplification_id_kk: |
| 1014 |
P(ctx, "mtl_AmplificationID;\n"); |
| 1015 |
break; |
| 1016 |
case nir_intrinsic_load_interpolated_input: { |
| 1017 |
unsigned idx = nir_src_as_uint(instr->src[1u]); |
| 1018 |
nir_io_semantics io = nir_intrinsic_io_semantics(instr); |
| 1019 |
uint32_t component = nir_intrinsic_component(instr); |
| 1020 |
uint32_t location = io.location + idx; |
| 1021 |
|
| 1022 |
msl_input_name(ctx, location, component); |
| 1023 |
if (ctx->inputs_info[location].uses_interpolant) |
| 1024 |
msl_interpolant_method(ctx, &instr->src[0u]); |
| 1025 |
if (instr->num_components < msl_input_num_components(ctx, location)) { |
| 1026 |
P(ctx, "."); |
| 1027 |
for (unsigned i = 0; i < instr->num_components; i++) |
| 1028 |
P(ctx, "%c", "xyzw"[component + i]); |
| 1029 |
} |
| 1030 |
P(ctx, ";\n"); |
| 1031 |
break; |
| 1032 |
} |
| 1033 |
case nir_intrinsic_load_input: { |
| 1034 |
unsigned idx = nir_src_as_uint(instr->src[0u]); |
| 1035 |
nir_io_semantics io = nir_intrinsic_io_semantics(instr); |
| 1036 |
uint32_t component = nir_intrinsic_component(instr); |
| 1037 |
uint32_t location = io.location + idx; |
| 1038 |
|
| 1039 |
msl_input_name(ctx, location, component); |
| 1040 |
if (instr->num_components < msl_input_num_components(ctx, location)) { |
| 1041 |
P(ctx, "."); |
| 1042 |
for (unsigned i = 0; i < instr->num_components; i++) |
| 1043 |
P(ctx, "%c", "xyzw"[component + i]); |
| 1044 |
} |
| 1045 |
P(ctx, ";\n"); |
| 1046 |
break; |
| 1047 |
} |
| 1048 |
case nir_intrinsic_load_sample_pos_from_id: { |
| 1049 |
P(ctx, "get_sample_position("); |
| 1050 |
src_to_msl(ctx, &instr->src[0]); |
| 1051 |
P(ctx, ");\n"); |
| 1052 |
break; |
| 1053 |
} |
| 1054 |
case nir_intrinsic_load_output: { |
| 1055 |
unsigned idx = nir_src_as_uint(instr->src[0]); |
| 1056 |
nir_io_semantics io = nir_intrinsic_io_semantics(instr); |
| 1057 |
nir_alu_type type = nir_intrinsic_dest_type(instr); |
| 1058 |
bool needs_padding = |
| 1059 |
FRAG_RESULT_DATA0 <= io.location && io.location <= FRAG_RESULT_DATA7; |
| 1060 |
if (needs_padding) { |
| 1061 |
P(ctx, "%s4(", tex_type_name(type)); |
| 1062 |
} |
| 1063 |
msl_output_name(ctx, io.location + idx, 0); |
| 1064 |
|
| 1065 |
if (needs_padding) { |
| 1066 |
for (uint32_t i = ctx->outputs_info[io.location].num_components; |
| 1067 |
i < 4u; ++i) |
| 1068 |
P(ctx, ", %c", "0001"[i]); |
| 1069 |
P(ctx, ")"); |
| 1070 |
} |
| 1071 |
P(ctx, ";\n"); |
| 1072 |
break; |
| 1073 |
} |
| 1074 |
case nir_intrinsic_store_output: { |
| 1075 |
uint32_t idx = nir_src_as_uint(instr->src[1]); |
| 1076 |
nir_io_semantics io = nir_intrinsic_io_semantics(instr); |
| 1077 |
uint32_t location = io.location + idx; |
| 1078 |
uint32_t write_mask = nir_intrinsic_write_mask(instr); |
| 1079 |
uint32_t component = nir_intrinsic_component(instr); |
| 1080 |
uint32_t dst_num_components = msl_output_num_components(ctx, location); |
| 1081 |
uint32_t num_components = instr->num_components; |
| 1082 |
|
| 1083 |
P_IND(ctx, "%s", ""); |
| 1084 |
msl_output_name(ctx, location, component); |
| 1085 |
if (dst_num_components > 1u) { |
| 1086 |
P(ctx, "."); |
| 1087 |
for (unsigned i = 0; i < num_components; i++) |
| 1088 |
if ((write_mask >> i) & 1) |
| 1089 |
P(ctx, "%c", "xyzw"[component + i]); |
| 1090 |
} |
| 1091 |
P(ctx, " = "); |
| 1092 |
src_to_msl(ctx, &instr->src[0]); |
| 1093 |
if (num_components > 1u) { |
| 1094 |
P(ctx, "."); |
| 1095 |
for (unsigned i = 0; i < num_components; i++) |
| 1096 |
if ((write_mask >> i) & 1) |
| 1097 |
P(ctx, "%c", "xyzw"[i]); |
| 1098 |
} |
| 1099 |
P(ctx, ";\n"); |
| 1100 |
break; |
| 1101 |
} |
| 1102 |
case nir_intrinsic_load_push_constant: { |
| 1103 |
const char *ty = msl_type_for_def(ctx->types, &instr->def); |
| 1104 |
assert(nir_intrinsic_base(instr) == 0); |
| 1105 |
P(ctx, "*((constant %s*)&buf.push_consts[", ty); |
| 1106 |
src_to_msl(ctx, &instr->src[0]); |
| 1107 |
P(ctx, "]);\n"); |
| 1108 |
break; |
| 1109 |
} |
| 1110 |
case nir_intrinsic_load_buffer_ptr_kk: |
| 1111 |
P(ctx, "(ulong)&buf%d.contents[0];\n", nir_intrinsic_binding(instr)); |
| 1112 |
break; |
| 1113 |
case nir_intrinsic_load_global: { |
| 1114 |
enum gl_access_qualifier access = nir_intrinsic_access(instr); |
| 1115 |
const char *type = msl_type_for_def(ctx->types, &instr->def); |
| 1116 |
const bool apply_workaround = |
| 1117 |
!(ctx->disabled_workarounds & BITFIELD64_BIT(6)); |
| 1118 |
const char *addressing = apply_workaround || (access & ACCESS_COHERENT) |
| 1119 |
? "coherent device" |
| 1120 |
: "device"; |
| 1121 |
if (access & ACCESS_ATOMIC) { |
| 1122 |
assert(instr->num_components == 1u && |
| 1123 |
"We can only do single component with atomics"); |
| 1124 |
P(ctx, "atomic_load_explicit((%s atomic_%s*)", addressing, type); |
| 1125 |
src_to_msl(ctx, &instr->src[0]); |
| 1126 |
P(ctx, ", memory_order_relaxed);\n"); |
| 1127 |
} else { |
| 1128 |
src_to_packed_load(ctx, &instr->src[0], addressing, type, |
| 1129 |
instr->def.num_components); |
| 1130 |
P(ctx, ";\n"); |
| 1131 |
} |
| 1132 |
break; |
| 1133 |
} |
| 1134 |
case nir_intrinsic_load_global_constant: { |
| 1135 |
src_to_packed_load(ctx, &instr->src[0], "constant", |
| 1136 |
msl_type_for_def(ctx->types, &instr->def), |
| 1137 |
instr->def.num_components); |
| 1138 |
P(ctx, ";\n"); |
| 1139 |
break; |
| 1140 |
} |
| 1141 |
case nir_intrinsic_load_global_constant_bounded: { |
| 1142 |
src_to_msl(ctx, &instr->src[1]); |
| 1143 |
P(ctx, " < "); |
| 1144 |
src_to_msl(ctx, &instr->src[2]); |
| 1145 |
P(ctx, " ? "); |
| 1146 |
src_to_packed_load_offset(ctx, &instr->src[0], &instr->src[1], "constant", |
| 1147 |
msl_type_for_def(ctx->types, &instr->def), |
| 1148 |
instr->def.num_components); |
| 1149 |
P(ctx, " : 0;\n"); |
| 1150 |
break; |
| 1151 |
} |
| 1152 |
case nir_intrinsic_load_global_constant_offset: { |
| 1153 |
enum gl_access_qualifier access = nir_intrinsic_access(instr); |
| 1154 |
const char *type = msl_type_for_def(ctx->types, &instr->def); |
| 1155 |
const char *addressing = |
| 1156 |
access & ACCESS_COHERENT ? "coherent device" : "device"; |
| 1157 |
if (access & ACCESS_ATOMIC) { |
| 1158 |
assert(instr->num_components == 1u && |
| 1159 |
"We can only do single component with atomics"); |
| 1160 |
P(ctx, "atomic_load_explicit((%s atomic_%s*)(", addressing, type); |
| 1161 |
src_to_msl(ctx, &instr->src[0]); |
| 1162 |
P(ctx, "+"); |
| 1163 |
src_to_msl(ctx, &instr->src[1]); |
| 1164 |
P(ctx, ", memory_order_relaxed);\n"); |
| 1165 |
} else { |
| 1166 |
src_to_packed_load_offset(ctx, &instr->src[0], &instr->src[1], |
| 1167 |
addressing, |
| 1168 |
msl_type_for_def(ctx->types, &instr->def), |
| 1169 |
instr->def.num_components); |
| 1170 |
} |
| 1171 |
P(ctx, ";\n"); |
| 1172 |
break; |
| 1173 |
} |
| 1174 |
case nir_intrinsic_global_atomic: |
| 1175 |
atomic_to_msl(ctx, instr, "device", false); |
| 1176 |
break; |
| 1177 |
case nir_intrinsic_global_atomic_swap: |
| 1178 |
atomic_swap_to_msl(ctx, instr, "device", false); |
| 1179 |
break; |
| 1180 |
case nir_intrinsic_shared_atomic: |
| 1181 |
atomic_to_msl(ctx, instr, "threadgroup", true); |
| 1182 |
break; |
| 1183 |
case nir_intrinsic_shared_atomic_swap: |
| 1184 |
atomic_swap_to_msl(ctx, instr, "threadgroup", true); |
| 1185 |
break; |
| 1186 |
case nir_intrinsic_store_global: { |
| 1187 |
enum gl_access_qualifier access = nir_intrinsic_access(instr); |
| 1188 |
const char *type = msl_type_for_src(ctx->types, &instr->src[0]); |
| 1189 |
const bool apply_workaround = |
| 1190 |
!(ctx->disabled_workarounds & BITFIELD64_BIT(6)); |
| 1191 |
const char *addressing = apply_workaround || (access & ACCESS_COHERENT) |
| 1192 |
? "coherent device" |
| 1193 |
: "device"; |
| 1194 |
if (access & ACCESS_ATOMIC) { |
| 1195 |
assert(instr->num_components == 1u && |
| 1196 |
"We can only do single component with atomics"); |
| 1197 |
P_IND(ctx, "atomic_store_explicit((%s atomic_%s*)", addressing, type); |
| 1198 |
src_to_msl(ctx, &instr->src[1]); |
| 1199 |
P(ctx, ", ") |
| 1200 |
src_to_packed(ctx, &instr->src[0], type, |
| 1201 |
instr->src[0].ssa->num_components); |
| 1202 |
P(ctx, ", memory_order_relaxed);\n"); |
| 1203 |
} else { |
| 1204 |
src_to_packed_store(ctx, &instr->src[1], addressing, type, |
| 1205 |
instr->src[0].ssa->num_components); |
| 1206 |
writemask_to_msl(ctx, nir_intrinsic_write_mask(instr), |
| 1207 |
instr->num_components); |
| 1208 |
P(ctx, " = ") |
| 1209 |
src_to_packed(ctx, &instr->src[0], type, |
| 1210 |
instr->src[0].ssa->num_components); |
| 1211 |
P(ctx, ";\n"); |
| 1212 |
} |
| 1213 |
break; |
| 1214 |
} |
| 1215 |
case nir_intrinsic_barrier: { |
| 1216 |
mesa_scope execution_scope = nir_intrinsic_execution_scope(instr); |
| 1217 |
nir_variable_mode memory_modes = nir_intrinsic_memory_modes(instr); |
| 1218 |
if (execution_scope == SCOPE_SUBGROUP) { |
| 1219 |
P_IND(ctx, "simdgroup_barrier("); |
| 1220 |
memory_modes_to_msl(ctx, memory_modes); |
| 1221 |
} else if (execution_scope == SCOPE_WORKGROUP) { |
| 1222 |
P_IND(ctx, "threadgroup_barrier("); |
| 1223 |
memory_modes_to_msl(ctx, memory_modes); |
| 1224 |
} else if (execution_scope == SCOPE_NONE) { |
| 1225 |
|
| 1226 |
if (memory_modes == 0u) |
| 1227 |
break; |
| 1228 |
|
| 1229 |
P_IND(ctx, "atomic_thread_fence("); |
| 1230 |
memory_modes_to_msl(ctx, memory_modes); |
| 1231 |
P(ctx, ", memory_order_seq_cst, "); |
| 1232 |
switch (nir_intrinsic_memory_scope(instr)) { |
| 1233 |
case SCOPE_SUBGROUP: |
| 1234 |
P(ctx, "thread_scope::thread_scope_simdgroup"); |
| 1235 |
break; |
| 1236 |
case SCOPE_WORKGROUP: |
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
if (memory_modes & |
| 1243 |
(nir_var_mem_global | nir_var_mem_ssbo | nir_var_image)) { |
| 1244 |
P(ctx, "thread_scope::thread_scope_device"); |
| 1245 |
} else { |
| 1246 |
P(ctx, "thread_scope::thread_scope_threadgroup"); |
| 1247 |
} |
| 1248 |
|
| 1249 |
break; |
| 1250 |
case SCOPE_QUEUE_FAMILY: |
| 1251 |
case SCOPE_DEVICE: |
| 1252 |
P(ctx, "thread_scope::thread_scope_device"); |
| 1253 |
break; |
| 1254 |
default: |
| 1255 |
P(ctx, "bad_scope"); |
| 1256 |
assert(!"bad scope"); |
| 1257 |
break; |
| 1258 |
} |
| 1259 |
} else { |
| 1260 |
UNREACHABLE("bad_execution scope"); |
| 1261 |
} |
| 1262 |
P(ctx, ");\n"); |
| 1263 |
break; |
| 1264 |
} |
| 1265 |
case nir_intrinsic_demote: |
| 1266 |
P_IND(ctx, "discard_fragment();\n"); |
| 1267 |
break; |
| 1268 |
case nir_intrinsic_demote_if: |
| 1269 |
P_IND(ctx, "if (") |
| 1270 |
src_to_msl(ctx, &instr->src[0]); |
| 1271 |
P(ctx, ")\n"); |
| 1272 |
ctx->indentlevel++; |
| 1273 |
P_IND(ctx, "discard_fragment();\n"); |
| 1274 |
ctx->indentlevel--; |
| 1275 |
break; |
| 1276 |
case nir_intrinsic_terminate: |
| 1277 |
P_IND(ctx, "discard_fragment();\n"); |
| 1278 |
P_IND(ctx, "return {};\n"); |
| 1279 |
break; |
| 1280 |
case nir_intrinsic_terminate_if: |
| 1281 |
P_IND(ctx, "if (") |
| 1282 |
src_to_msl(ctx, &instr->src[0]); |
| 1283 |
P(ctx, ") {\n"); |
| 1284 |
ctx->indentlevel++; |
| 1285 |
P_IND(ctx, "discard_fragment();\n"); |
| 1286 |
P_IND(ctx, "return {};\n"); |
| 1287 |
ctx->indentlevel--; |
| 1288 |
P_IND(ctx, "}\n"); |
| 1289 |
break; |
| 1290 |
case nir_intrinsic_load_shared: |
| 1291 |
assert(nir_intrinsic_base(instr) == 0); |
| 1292 |
P(ctx, "*(threadgroup %s*)&shared_data[", |
| 1293 |
msl_type_for_def(ctx->types, &instr->def)); |
| 1294 |
src_to_msl(ctx, &instr->src[0]); |
| 1295 |
P(ctx, "];\n"); |
| 1296 |
break; |
| 1297 |
case nir_intrinsic_store_shared: |
| 1298 |
assert(nir_intrinsic_base(instr) == 0); |
| 1299 |
P_IND(ctx, "(*(threadgroup %s*)&shared_data[", |
| 1300 |
msl_type_for_src(ctx->types, &instr->src[0])); |
| 1301 |
src_to_msl(ctx, &instr->src[1]); |
| 1302 |
P(ctx, "])"); |
| 1303 |
writemask_to_msl(ctx, nir_intrinsic_write_mask(instr), |
| 1304 |
instr->num_components); |
| 1305 |
P(ctx, " = "); |
| 1306 |
src_to_msl(ctx, &instr->src[0]); |
| 1307 |
if (instr->src[0].ssa->num_components > 1) |
| 1308 |
writemask_to_msl(ctx, nir_intrinsic_write_mask(instr), |
| 1309 |
instr->num_components); |
| 1310 |
P(ctx, ";\n"); |
| 1311 |
break; |
| 1312 |
case nir_intrinsic_load_scratch: |
| 1313 |
P(ctx, "*(thread %s*)&scratch[", |
| 1314 |
msl_type_for_def(ctx->types, &instr->def)); |
| 1315 |
src_to_msl(ctx, &instr->src[0]); |
| 1316 |
P(ctx, "];\n"); |
| 1317 |
break; |
| 1318 |
case nir_intrinsic_store_scratch: |
| 1319 |
P_IND(ctx, "(*(thread %s*)&scratch[", |
| 1320 |
msl_type_for_src(ctx->types, &instr->src[0])); |
| 1321 |
src_to_msl(ctx, &instr->src[1]); |
| 1322 |
P(ctx, "])"); |
| 1323 |
writemask_to_msl(ctx, nir_intrinsic_write_mask(instr), |
| 1324 |
instr->num_components); |
| 1325 |
P(ctx, " = "); |
| 1326 |
src_to_msl(ctx, &instr->src[0]); |
| 1327 |
if (instr->src[0].ssa->num_components > 1) |
| 1328 |
writemask_to_msl(ctx, nir_intrinsic_write_mask(instr), |
| 1329 |
instr->num_components); |
| 1330 |
P(ctx, ";\n"); |
| 1331 |
break; |
| 1332 |
case nir_intrinsic_load_texture_handle_kk: { |
| 1333 |
const char *access = ""; |
| 1334 |
const char *coherent = nir_intrinsic_access(instr) & ACCESS_COHERENT |
| 1335 |
? ", memory_coherence_device" |
| 1336 |
: ""; |
| 1337 |
switch (nir_intrinsic_flags(instr)) { |
| 1338 |
case MSL_ACCESS_READ: |
| 1339 |
access = ", access::read"; |
| 1340 |
break; |
| 1341 |
case MSL_ACCESS_WRITE: |
| 1342 |
access = ", access::write"; |
| 1343 |
break; |
| 1344 |
case MSL_ACCESS_READ_WRITE: |
| 1345 |
access = ", access::read_write"; |
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
coherent = ", memory_coherence_device"; |
| 1354 |
break; |
| 1355 |
} |
| 1356 |
P_IND(ctx, "texture%s%s<%s%s%s> t%d = *(constant texture%s%s<%s%s%s>*)", |
| 1357 |
texture_dim(nir_intrinsic_image_dim(instr)), |
| 1358 |
nir_intrinsic_image_array(instr) ? "_array" : "", |
| 1359 |
tex_type_name(nir_intrinsic_dest_type(instr)), access, coherent, |
| 1360 |
instr->def.index, texture_dim(nir_intrinsic_image_dim(instr)), |
| 1361 |
nir_intrinsic_image_array(instr) ? "_array" : "", |
| 1362 |
tex_type_name(nir_intrinsic_dest_type(instr)), access, coherent); |
| 1363 |
src_to_msl(ctx, &instr->src[0]); |
| 1364 |
P(ctx, ";\n"); |
| 1365 |
break; |
| 1366 |
} |
| 1367 |
case nir_intrinsic_load_depth_texture_kk: |
| 1368 |
P_IND(ctx, "depth%s%s<float> t%d = *(constant depth%s%s<float>*)", |
| 1369 |
texture_dim(nir_intrinsic_image_dim(instr)), |
| 1370 |
nir_intrinsic_image_array(instr) ? "_array" : "", instr->def.index, |
| 1371 |
texture_dim(nir_intrinsic_image_dim(instr)), |
| 1372 |
nir_intrinsic_image_array(instr) ? "_array" : ""); |
| 1373 |
src_to_msl(ctx, &instr->src[0]); |
| 1374 |
P(ctx, ";\n"); |
| 1375 |
break; |
| 1376 |
case nir_intrinsic_load_sampler_handle_kk: |
| 1377 |
P(ctx, "sampler_table.handles["); |
| 1378 |
src_to_msl(ctx, &instr->src[0]); |
| 1379 |
P(ctx, "];\n"); |
| 1380 |
break; |
| 1381 |
case nir_intrinsic_load_constant_agx: { |
| 1382 |
const char *type = msl_type_for_def(ctx->types, &instr->def); |
| 1383 |
const char *no_component_type = |
| 1384 |
msl_pipe_format_to_msl_type(nir_intrinsic_format(instr)); |
| 1385 |
if (instr->def.num_components == 1) { |
| 1386 |
P(ctx, "(*(((constant %s*)", type); |
| 1387 |
} else { |
| 1388 |
P(ctx, "%s(*(constant packed_%s*)(((constant %s*)", type, type, |
| 1389 |
no_component_type); |
| 1390 |
} |
| 1391 |
src_to_msl(ctx, &instr->src[0]); |
| 1392 |
P(ctx, ") + "); |
| 1393 |
src_to_msl(ctx, &instr->src[1]); |
| 1394 |
P(ctx, "));\n"); |
| 1395 |
break; |
| 1396 |
} |
| 1397 |
case nir_intrinsic_bindless_image_load: |
| 1398 |
src_to_msl(ctx, &instr->src[0]); |
| 1399 |
P(ctx, ".read("); |
| 1400 |
image_coord_swizzle(ctx, instr); |
| 1401 |
if (nir_intrinsic_image_dim(instr) != GLSL_SAMPLER_DIM_BUF) { |
| 1402 |
P(ctx, ", "); |
| 1403 |
src_to_msl(ctx, &instr->src[3]); |
| 1404 |
} |
| 1405 |
|
| 1406 |
|
| 1407 |
P(ctx, ")."); |
| 1408 |
for (uint32_t i = 0u; i < instr->def.num_components; ++i) { |
| 1409 |
P(ctx, "%c", "xyzw"[i]); |
| 1410 |
} |
| 1411 |
P(ctx, ";\n"); |
| 1412 |
break; |
| 1413 |
case nir_intrinsic_bindless_image_store: |
| 1414 |
P_INDENT(ctx); |
| 1415 |
src_to_msl(ctx, &instr->src[0]); |
| 1416 |
P(ctx, ".write("); |
| 1417 |
src_to_msl(ctx, &instr->src[3]); |
| 1418 |
P(ctx, ", "); |
| 1419 |
image_coord_swizzle(ctx, instr); |
| 1420 |
if (nir_intrinsic_image_dim(instr) != GLSL_SAMPLER_DIM_BUF) { |
| 1421 |
P(ctx, ", "); |
| 1422 |
src_to_msl(ctx, &instr->src[4]); |
| 1423 |
} |
| 1424 |
P(ctx, ");\n"); |
| 1425 |
break; |
| 1426 |
case nir_intrinsic_bindless_image_atomic: |
| 1427 |
src_to_msl(ctx, &instr->src[0]); |
| 1428 |
P(ctx, ".%s(", atomic_op_to_msl(nir_intrinsic_atomic_op(instr))); |
| 1429 |
image_coord_swizzle(ctx, instr); |
| 1430 |
P(ctx, ", "); |
| 1431 |
src_to_msl(ctx, &instr->src[3]); |
| 1432 |
P(ctx, ").x;\n"); |
| 1433 |
break; |
| 1434 |
case nir_intrinsic_bindless_image_atomic_swap: { |
| 1435 |
const char *type = msl_type_for_def(ctx->types, &instr->def); |
| 1436 |
P_IND(ctx, "%s4 ta%d = ", type, instr->def.index); |
| 1437 |
src_to_msl(ctx, &instr->src[3]); |
| 1438 |
P(ctx, "; "); |
| 1439 |
src_to_msl(ctx, &instr->src[0]); |
| 1440 |
P(ctx, ".%s(", atomic_op_to_msl(nir_intrinsic_atomic_op(instr))); |
| 1441 |
image_coord_swizzle(ctx, instr); |
| 1442 |
P(ctx, ", &ta%d, ", instr->def.index); |
| 1443 |
src_to_msl(ctx, &instr->src[4]); |
| 1444 |
P(ctx, "); %s t%d = ta%d.x;\n", type, instr->def.index, instr->def.index); |
| 1445 |
break; |
| 1446 |
} |
| 1447 |
case nir_intrinsic_ballot: |
| 1448 |
P(ctx, "(ulong)simd_ballot("); |
| 1449 |
src_to_msl(ctx, &instr->src[0]); |
| 1450 |
P(ctx, ");\n"); |
| 1451 |
break; |
| 1452 |
case nir_intrinsic_elect: |
| 1453 |
|
| 1454 |
if (ctx->disabled_workarounds & BITFIELD64_BIT(3)) { |
| 1455 |
P(ctx, "simd_is_first();\n"); |
| 1456 |
} else { |
| 1457 |
P(ctx, "simd_is_first() && (ulong)simd_ballot(true);\n"); |
| 1458 |
} |
| 1459 |
break; |
| 1460 |
case nir_intrinsic_read_first_invocation: |
| 1461 |
P(ctx, "simd_broadcast_first("); |
| 1462 |
src_to_msl(ctx, &instr->src[0]); |
| 1463 |
P(ctx, ");\n"); |
| 1464 |
break; |
| 1465 |
case nir_intrinsic_read_invocation: |
| 1466 |
P(ctx, "simd_broadcast("); |
| 1467 |
src_to_msl(ctx, &instr->src[0]); |
| 1468 |
P(ctx, ", "); |
| 1469 |
src_to_msl(ctx, &instr->src[1]); |
| 1470 |
P(ctx, ");"); |
| 1471 |
break; |
| 1472 |
case nir_intrinsic_shuffle: |
| 1473 |
P(ctx, "simd_shuffle("); |
| 1474 |
src_to_msl(ctx, &instr->src[0]); |
| 1475 |
P(ctx, ", "); |
| 1476 |
src_to_msl(ctx, &instr->src[1]); |
| 1477 |
P(ctx, ");\n"); |
| 1478 |
break; |
| 1479 |
case nir_intrinsic_shuffle_xor: |
| 1480 |
P(ctx, "simd_shuffle_xor("); |
| 1481 |
src_to_msl(ctx, &instr->src[0]); |
| 1482 |
P(ctx, ", "); |
| 1483 |
src_to_msl(ctx, &instr->src[1]); |
| 1484 |
P(ctx, ");\n"); |
| 1485 |
break; |
| 1486 |
case nir_intrinsic_shuffle_up: |
| 1487 |
P(ctx, "simd_shuffle_up("); |
| 1488 |
src_to_msl(ctx, &instr->src[0]); |
| 1489 |
P(ctx, ", "); |
| 1490 |
src_to_msl(ctx, &instr->src[1]); |
| 1491 |
P(ctx, ");\n"); |
| 1492 |
break; |
| 1493 |
case nir_intrinsic_shuffle_down: |
| 1494 |
P(ctx, "simd_shuffle_down("); |
| 1495 |
src_to_msl(ctx, &instr->src[0]); |
| 1496 |
P(ctx, ", "); |
| 1497 |
src_to_msl(ctx, &instr->src[1]); |
| 1498 |
P(ctx, ");\n"); |
| 1499 |
break; |
| 1500 |
|
| 1501 |
case nir_intrinsic_vote_all: |
| 1502 |
P(ctx, "simd_all("); |
| 1503 |
src_to_msl(ctx, &instr->src[0]); |
| 1504 |
P(ctx, ");\n"); |
| 1505 |
break; |
| 1506 |
case nir_intrinsic_vote_any: |
| 1507 |
P(ctx, "simd_any("); |
| 1508 |
src_to_msl(ctx, &instr->src[0]); |
| 1509 |
P(ctx, ");\n"); |
| 1510 |
break; |
| 1511 |
case nir_intrinsic_quad_broadcast: |
| 1512 |
P(ctx, "quad_broadcast("); |
| 1513 |
src_to_msl(ctx, &instr->src[0]); |
| 1514 |
P(ctx, ", "); |
| 1515 |
src_to_msl(ctx, &instr->src[1]); |
| 1516 |
P(ctx, ");\n"); |
| 1517 |
break; |
| 1518 |
case nir_intrinsic_quad_swap_horizontal: |
| 1519 |
P(ctx, "quad_shuffle_xor("); |
| 1520 |
src_to_msl(ctx, &instr->src[0]); |
| 1521 |
P(ctx, ", 1);\n"); |
| 1522 |
break; |
| 1523 |
case nir_intrinsic_quad_swap_vertical: |
| 1524 |
P(ctx, "quad_shuffle_xor("); |
| 1525 |
src_to_msl(ctx, &instr->src[0]); |
| 1526 |
P(ctx, ", 2);\n"); |
| 1527 |
break; |
| 1528 |
case nir_intrinsic_quad_swap_diagonal: |
| 1529 |
P(ctx, "quad_shuffle_xor("); |
| 1530 |
src_to_msl(ctx, &instr->src[0]); |
| 1531 |
P(ctx, ", 3);\n"); |
| 1532 |
break; |
| 1533 |
case nir_intrinsic_reduce: |
| 1534 |
switch (nir_intrinsic_reduction_op(instr)) { |
| 1535 |
case nir_op_iadd: |
| 1536 |
case nir_op_fadd: |
| 1537 |
P(ctx, "simd_sum("); |
| 1538 |
break; |
| 1539 |
case nir_op_imul: |
| 1540 |
case nir_op_fmul: |
| 1541 |
P(ctx, "simd_product("); |
| 1542 |
break; |
| 1543 |
case nir_op_imin: |
| 1544 |
case nir_op_umin: |
| 1545 |
case nir_op_fmin: |
| 1546 |
P(ctx, "simd_min("); |
| 1547 |
break; |
| 1548 |
case nir_op_imax: |
| 1549 |
case nir_op_umax: |
| 1550 |
case nir_op_fmax: |
| 1551 |
P(ctx, "simd_max("); |
| 1552 |
break; |
| 1553 |
case nir_op_iand: |
| 1554 |
P(ctx, "simd_and("); |
| 1555 |
break; |
| 1556 |
case nir_op_ior: |
| 1557 |
P(ctx, "simd_or("); |
| 1558 |
break; |
| 1559 |
case nir_op_ixor: |
| 1560 |
P(ctx, "simd_xor("); |
| 1561 |
break; |
| 1562 |
default: |
| 1563 |
UNREACHABLE("Bad reduction op"); |
| 1564 |
} |
| 1565 |
|
| 1566 |
src_to_msl(ctx, &instr->src[0]); |
| 1567 |
P(ctx, ");\n"); |
| 1568 |
break; |
| 1569 |
case nir_intrinsic_store_clip_distance_kk: |
| 1570 |
P_IND(ctx, "out.gl_ClipDistance[%d] = ", nir_intrinsic_base(instr)); |
| 1571 |
src_to_msl(ctx, &instr->src[0]); |
| 1572 |
P(ctx, ";\n"); |
| 1573 |
break; |
| 1574 |
default: |
| 1575 |
P_IND(ctx, "Unknown intrinsic %s\n", info->name); |
| 1576 |
} |
| 1577 |
} |
| 1578 |
|
| 1579 |
static nir_src * |
| 1580 |
nir_tex_get_src(struct nir_tex_instr *tex, nir_tex_src_type type) |
| 1581 |
{ |
| 1582 |
int idx = nir_tex_instr_src_index(tex, type); |
| 1583 |
if (idx == -1) |
| 1584 |
return NULL; |
| 1585 |
return &tex->src[idx].src; |
| 1586 |
} |
| 1587 |
|
| 1588 |
static void |
| 1589 |
tex_coord_swizzle(struct nir_to_msl_ctx *ctx, nir_tex_instr *tex) |
| 1590 |
{ |
| 1591 |
texture_src_coord_swizzle(ctx, nir_tex_get_src(tex, nir_tex_src_coord), |
| 1592 |
tex->coord_components, false, tex->is_array); |
| 1593 |
} |
| 1594 |
|
| 1595 |
static void |
| 1596 |
tex_to_msl(struct nir_to_msl_ctx *ctx, nir_tex_instr *tex) |
| 1597 |
{ |
| 1598 |
nir_src *texhandle = nir_tex_get_src(tex, nir_tex_src_texture_handle); |
| 1599 |
nir_src *sampler = nir_tex_get_src(tex, nir_tex_src_sampler_handle); |
| 1600 |
|
| 1601 |
assert(!nir_tex_get_src(tex, nir_tex_src_projector)); |
| 1602 |
|
| 1603 |
P_IND(ctx, "t%d = ", tex->def.index); |
| 1604 |
|
| 1605 |
switch (tex->op) { |
| 1606 |
case nir_texop_tex: |
| 1607 |
case nir_texop_txb: |
| 1608 |
case nir_texop_txl: |
| 1609 |
case nir_texop_txd: { |
| 1610 |
nir_src *bias = nir_tex_get_src(tex, nir_tex_src_bias); |
| 1611 |
nir_src *lod = nir_tex_get_src(tex, nir_tex_src_lod); |
| 1612 |
nir_src *ddx = nir_tex_get_src(tex, nir_tex_src_ddx); |
| 1613 |
nir_src *ddy = nir_tex_get_src(tex, nir_tex_src_ddy); |
| 1614 |
nir_src *min_lod_clamp = nir_tex_get_src(tex, nir_tex_src_min_lod); |
| 1615 |
nir_src *offset = nir_tex_get_src(tex, nir_tex_src_offset); |
| 1616 |
nir_src *comparator = nir_tex_get_src(tex, nir_tex_src_comparator); |
| 1617 |
src_to_msl(ctx, texhandle); |
| 1618 |
if (comparator) { |
| 1619 |
P(ctx, ".sample_compare("); |
| 1620 |
} else { |
| 1621 |
P(ctx, ".sample("); |
| 1622 |
} |
| 1623 |
src_to_msl(ctx, sampler); |
| 1624 |
P(ctx, ", "); |
| 1625 |
tex_coord_swizzle(ctx, tex); |
| 1626 |
if (comparator) { |
| 1627 |
P(ctx, ", "); |
| 1628 |
src_to_msl(ctx, comparator); |
| 1629 |
} |
| 1630 |
if (bias) { |
| 1631 |
P(ctx, ", bias("); |
| 1632 |
src_to_msl(ctx, bias); |
| 1633 |
P(ctx, ")"); |
| 1634 |
} |
| 1635 |
if (lod) { |
| 1636 |
P(ctx, ", level("); |
| 1637 |
src_to_msl(ctx, lod); |
| 1638 |
P(ctx, ")"); |
| 1639 |
} |
| 1640 |
if (ddx) { |
| 1641 |
P(ctx, ", gradient%s(", texture_dim(tex->sampler_dim)); |
| 1642 |
src_to_msl(ctx, ddx); |
| 1643 |
P(ctx, ", "); |
| 1644 |
src_to_msl(ctx, ddy); |
| 1645 |
P(ctx, ")"); |
| 1646 |
} |
| 1647 |
if (min_lod_clamp) { |
| 1648 |
P(ctx, ", min_lod_clamp("); |
| 1649 |
src_to_msl(ctx, min_lod_clamp); |
| 1650 |
P(ctx, ")"); |
| 1651 |
} |
| 1652 |
if (offset) { |
| 1653 |
P(ctx, ", "); |
| 1654 |
src_to_msl(ctx, offset); |
| 1655 |
} |
| 1656 |
P(ctx, ");\n"); |
| 1657 |
break; |
| 1658 |
} |
| 1659 |
case nir_texop_txf: { |
| 1660 |
src_to_msl(ctx, texhandle); |
| 1661 |
P(ctx, ".read("); |
| 1662 |
tex_coord_swizzle(ctx, tex); |
| 1663 |
nir_src *lod = nir_tex_get_src(tex, nir_tex_src_lod); |
| 1664 |
if (lod) { |
| 1665 |
P(ctx, ", "); |
| 1666 |
src_to_msl(ctx, lod); |
| 1667 |
} |
| 1668 |
P(ctx, ");\n"); |
| 1669 |
break; |
| 1670 |
} |
| 1671 |
case nir_texop_txf_ms: |
| 1672 |
src_to_msl(ctx, texhandle); |
| 1673 |
P(ctx, ".read("); |
| 1674 |
tex_coord_swizzle(ctx, tex); |
| 1675 |
P(ctx, ", "); |
| 1676 |
src_to_msl(ctx, nir_tex_get_src(tex, nir_tex_src_ms_index)); |
| 1677 |
P(ctx, ");\n"); |
| 1678 |
break; |
| 1679 |
case nir_texop_txs: { |
| 1680 |
nir_src *lod = nir_tex_get_src(tex, nir_tex_src_lod); |
| 1681 |
if (tex->def.num_components > 1u) { |
| 1682 |
P(ctx, "%s%d(", tex_type_name(tex->dest_type), |
| 1683 |
tex->def.num_components); |
| 1684 |
} else { |
| 1685 |
P(ctx, "%s(", tex_type_name(tex->dest_type)); |
| 1686 |
} |
| 1687 |
src_to_msl(ctx, texhandle); |
| 1688 |
P(ctx, ".get_width(") |
| 1689 |
if (lod && tex->sampler_dim != GLSL_SAMPLER_DIM_MS && |
| 1690 |
tex->sampler_dim != GLSL_SAMPLER_DIM_BUF) |
| 1691 |
src_to_msl(ctx, lod); |
| 1692 |
P(ctx, ")"); |
| 1693 |
if (tex->sampler_dim != GLSL_SAMPLER_DIM_1D && |
| 1694 |
tex->sampler_dim != GLSL_SAMPLER_DIM_BUF) { |
| 1695 |
P(ctx, ", "); |
| 1696 |
src_to_msl(ctx, texhandle); |
| 1697 |
P(ctx, ".get_height("); |
| 1698 |
if (lod && tex->sampler_dim != GLSL_SAMPLER_DIM_MS && |
| 1699 |
tex->sampler_dim != GLSL_SAMPLER_DIM_BUF) |
| 1700 |
src_to_msl(ctx, lod); |
| 1701 |
P(ctx, ")"); |
| 1702 |
} |
| 1703 |
if (tex->sampler_dim == GLSL_SAMPLER_DIM_3D) { |
| 1704 |
P(ctx, ", "); |
| 1705 |
src_to_msl(ctx, texhandle); |
| 1706 |
P(ctx, ".get_depth("); |
| 1707 |
if (lod) |
| 1708 |
src_to_msl(ctx, lod); |
| 1709 |
P(ctx, ")"); |
| 1710 |
} |
| 1711 |
if (tex->is_array) { |
| 1712 |
P(ctx, ", "); |
| 1713 |
src_to_msl(ctx, texhandle); |
| 1714 |
P(ctx, ".get_array_size()"); |
| 1715 |
} |
| 1716 |
P(ctx, ");\n") |
| 1717 |
break; |
| 1718 |
} |
| 1719 |
case nir_texop_query_levels: |
| 1720 |
src_to_msl(ctx, texhandle); |
| 1721 |
P(ctx, ".get_num_mip_levels();\n"); |
| 1722 |
break; |
| 1723 |
case nir_texop_tg4: { |
| 1724 |
nir_src *offset = nir_tex_get_src(tex, nir_tex_src_offset); |
| 1725 |
nir_src *comparator = nir_tex_get_src(tex, nir_tex_src_comparator); |
| 1726 |
src_to_msl(ctx, texhandle); |
| 1727 |
if (comparator) { |
| 1728 |
P(ctx, ".gather_compare("); |
| 1729 |
} else { |
| 1730 |
P(ctx, ".gather("); |
| 1731 |
} |
| 1732 |
src_to_msl(ctx, sampler); |
| 1733 |
P(ctx, ", "); |
| 1734 |
tex_coord_swizzle(ctx, tex); |
| 1735 |
if (comparator) { |
| 1736 |
P(ctx, ", "); |
| 1737 |
src_to_msl(ctx, comparator); |
| 1738 |
} |
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
if (offset) { |
| 1744 |
P(ctx, ", "); |
| 1745 |
src_to_msl(ctx, offset); |
| 1746 |
} else if (tex->sampler_dim != GLSL_SAMPLER_DIM_CUBE) { |
| 1747 |
P(ctx, ", int2(0)"); |
| 1748 |
} |
| 1749 |
|
| 1750 |
|
| 1751 |
if (!comparator) { |
| 1752 |
P(ctx, ", component::%c", "xyzw"[tex->component]); |
| 1753 |
} |
| 1754 |
|
| 1755 |
P(ctx, ");\n"); |
| 1756 |
break; |
| 1757 |
} |
| 1758 |
|
| 1759 |
case nir_texop_texture_samples: |
| 1760 |
src_to_msl(ctx, texhandle); |
| 1761 |
P(ctx, ".get_num_samples();\n"); |
| 1762 |
break; |
| 1763 |
case nir_texop_lod: { |
| 1764 |
nir_src *coord = nir_tex_get_src(tex, nir_tex_src_coord); |
| 1765 |
nir_src *bias = nir_tex_get_src(tex, nir_tex_src_bias); |
| 1766 |
nir_src *min = nir_tex_get_src(tex, nir_tex_src_min_lod); |
| 1767 |
nir_src *max = nir_tex_get_src(tex, nir_tex_src_max_lod_kk); |
| 1768 |
|
| 1769 |
|
| 1770 |
P(ctx, "float2(clamp(") |
| 1771 |
src_to_msl(ctx, texhandle); |
| 1772 |
P(ctx, ".calculate_clamped_lod("); |
| 1773 |
src_to_msl(ctx, sampler); |
| 1774 |
P(ctx, ", "); |
| 1775 |
src_to_msl(ctx, coord); |
| 1776 |
P(ctx, ") + "); |
| 1777 |
src_to_msl(ctx, bias); |
| 1778 |
P(ctx, ", "); |
| 1779 |
src_to_msl(ctx, min); |
| 1780 |
P(ctx, ", "); |
| 1781 |
src_to_msl(ctx, max); |
| 1782 |
P(ctx, "), "); |
| 1783 |
src_to_msl(ctx, texhandle); |
| 1784 |
P(ctx, ".calculate_unclamped_lod("); |
| 1785 |
src_to_msl(ctx, sampler); |
| 1786 |
P(ctx, ", "); |
| 1787 |
src_to_msl(ctx, coord); |
| 1788 |
P(ctx, ")"); |
| 1789 |
P(ctx, ");\n"); |
| 1790 |
break; |
| 1791 |
} |
| 1792 |
default: |
| 1793 |
assert(!"Unsupported texture op"); |
| 1794 |
} |
| 1795 |
} |
| 1796 |
|
| 1797 |
static void |
| 1798 |
jump_instr_to_msl(struct nir_to_msl_ctx *ctx, nir_jump_instr *jump) |
| 1799 |
{ |
| 1800 |
switch (jump->type) { |
| 1801 |
case nir_jump_halt: |
| 1802 |
P_IND(ctx, "TODO: halt\n"); |
| 1803 |
assert(!"Unimplemented"); |
| 1804 |
break; |
| 1805 |
case nir_jump_break: |
| 1806 |
P_IND(ctx, "break;\n"); |
| 1807 |
break; |
| 1808 |
case nir_jump_continue: |
| 1809 |
P_IND(ctx, "continue;\n"); |
| 1810 |
break; |
| 1811 |
case nir_jump_return: |
| 1812 |
assert(!"functions should have been inlined by now"); |
| 1813 |
break; |
| 1814 |
case nir_jump_goto: |
| 1815 |
case nir_jump_goto_if: |
| 1816 |
assert(!"Unstructured control flow not supported"); |
| 1817 |
break; |
| 1818 |
} |
| 1819 |
} |
| 1820 |
|
| 1821 |
static void |
| 1822 |
instr_to_msl(struct nir_to_msl_ctx *ctx, nir_instr *instr) |
| 1823 |
{ |
| 1824 |
switch (instr->type) { |
| 1825 |
case nir_instr_type_alu: { |
| 1826 |
nir_alu_instr *alu = nir_instr_as_alu(instr); |
| 1827 |
P_IND(ctx, "t%d = ", alu->def.index); |
| 1828 |
alu_to_msl(ctx, alu); |
| 1829 |
P(ctx, ";\n"); |
| 1830 |
break; |
| 1831 |
} |
| 1832 |
case nir_instr_type_deref: |
| 1833 |
assert(!"We should have lowered derefs by now"); |
| 1834 |
break; |
| 1835 |
case nir_instr_type_call: |
| 1836 |
case nir_instr_type_cmat_call: |
| 1837 |
assert(!"We should have inlined all functions by now"); |
| 1838 |
break; |
| 1839 |
case nir_instr_type_tex: |
| 1840 |
tex_to_msl(ctx, nir_instr_as_tex(instr)); |
| 1841 |
break; |
| 1842 |
case nir_instr_type_intrinsic: |
| 1843 |
intrinsic_to_msl(ctx, nir_instr_as_intrinsic(instr)); |
| 1844 |
break; |
| 1845 |
case nir_instr_type_load_const: |
| 1846 |
|
| 1847 |
break; |
| 1848 |
case nir_instr_type_jump: |
| 1849 |
jump_instr_to_msl(ctx, nir_instr_as_jump(instr)); |
| 1850 |
break; |
| 1851 |
case nir_instr_type_undef: |
| 1852 |
|
| 1853 |
break; |
| 1854 |
case nir_instr_type_phi: |
| 1855 |
assert(!"NIR should be taken out of SSA"); |
| 1856 |
break; |
| 1857 |
} |
| 1858 |
} |
| 1859 |
|
| 1860 |
static void |
| 1861 |
cf_node_to_metal(struct nir_to_msl_ctx *ctx, nir_cf_node *node) |
| 1862 |
{ |
| 1863 |
switch (node->type) { |
| 1864 |
case nir_cf_node_block: { |
| 1865 |
nir_block *block = nir_cf_node_as_block(node); |
| 1866 |
nir_foreach_instr(instr, block) { |
| 1867 |
instr_to_msl(ctx, instr); |
| 1868 |
} |
| 1869 |
break; |
| 1870 |
} |
| 1871 |
case nir_cf_node_if: { |
| 1872 |
nir_if *ifnode = nir_cf_node_as_if(node); |
| 1873 |
P_IND(ctx, "if ("); |
| 1874 |
src_to_msl(ctx, &ifnode->condition); |
| 1875 |
P(ctx, ") {\n"); |
| 1876 |
ctx->indentlevel++; |
| 1877 |
foreach_list_typed(nir_cf_node, node, node, &ifnode->then_list) { |
| 1878 |
cf_node_to_metal(ctx, node); |
| 1879 |
} |
| 1880 |
ctx->indentlevel--; |
| 1881 |
if (!nir_cf_list_is_empty_block(&ifnode->else_list)) { |
| 1882 |
P_IND(ctx, "} else {\n"); |
| 1883 |
ctx->indentlevel++; |
| 1884 |
foreach_list_typed(nir_cf_node, node, node, &ifnode->else_list) { |
| 1885 |
cf_node_to_metal(ctx, node); |
| 1886 |
} |
| 1887 |
ctx->indentlevel--; |
| 1888 |
} |
| 1889 |
P_IND(ctx, "}\n"); |
| 1890 |
break; |
| 1891 |
} |
| 1892 |
case nir_cf_node_loop: { |
| 1893 |
nir_loop *loop = nir_cf_node_as_loop(node); |
| 1894 |
assert(!nir_loop_has_continue_construct(loop)); |
| 1895 |
|
| 1896 |
if (ctx->disabled_workarounds & BITFIELD64_BIT(2)) { |
| 1897 |
P_IND(ctx, "while (true) {\n"); |
| 1898 |
} else { |
| 1899 |
P_IND(ctx, |
| 1900 |
"for (uint64_t no_crash = 0u; no_crash < %" PRIu64 |
| 1901 |
"; ++no_crash) {\n", |
| 1902 |
UINT64_MAX); |
| 1903 |
} |
| 1904 |
ctx->indentlevel++; |
| 1905 |
foreach_list_typed(nir_cf_node, node, node, &loop->body) { |
| 1906 |
cf_node_to_metal(ctx, node); |
| 1907 |
} |
| 1908 |
ctx->indentlevel--; |
| 1909 |
P_IND(ctx, "}\n"); |
| 1910 |
break; |
| 1911 |
} |
| 1912 |
case nir_cf_node_function: |
| 1913 |
assert(!"All functions are supposed to be inlined"); |
| 1914 |
} |
| 1915 |
} |
| 1916 |
|
| 1917 |
static void |
| 1918 |
emit_output_return(struct nir_to_msl_ctx *ctx, nir_shader *shader) |
| 1919 |
{ |
| 1920 |
if (shader->info.stage == MESA_SHADER_VERTEX || |
| 1921 |
shader->info.stage == MESA_SHADER_FRAGMENT) |
| 1922 |
P_IND(ctx, "return out;\n"); |
| 1923 |
} |
| 1924 |
|
| 1925 |
static void |
| 1926 |
rename_main_entrypoint(struct nir_shader *nir) |
| 1927 |
{ |
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
const char *entrypoint_name = "main_entrypoint"; |
| 1932 |
nir_function_impl *entrypoint = nir_shader_get_entrypoint(nir); |
| 1933 |
struct nir_function *function = entrypoint->function; |
| 1934 |
ralloc_free((void *)function->name); |
| 1935 |
function->name = ralloc_strdup(function, entrypoint_name); |
| 1936 |
} |
| 1937 |
|
| 1938 |
static bool |
| 1939 |
kk_scalarize_filter(const nir_instr *instr, const void *data) |
| 1940 |
{ |
| 1941 |
if (instr->type != nir_instr_type_alu) |
| 1942 |
return false; |
| 1943 |
return true; |
| 1944 |
} |
| 1945 |
|
| 1946 |
void |
| 1947 |
msl_preprocess_nir(struct nir_shader *nir) |
| 1948 |
{ |
| 1949 |
|
| 1950 |
NIR_PASS(_, nir, nir_lower_variable_initializers, nir_var_function_temp); |
| 1951 |
NIR_PASS(_, nir, nir_lower_returns); |
| 1952 |
NIR_PASS(_, nir, nir_inline_functions); |
| 1953 |
NIR_PASS(_, nir, nir_opt_deref); |
| 1954 |
nir_remove_non_entrypoints(nir); |
| 1955 |
|
| 1956 |
NIR_PASS(_, nir, nir_lower_global_vars_to_local); |
| 1957 |
NIR_PASS(_, nir, nir_split_var_copies); |
| 1958 |
NIR_PASS(_, nir, nir_split_struct_vars, nir_var_function_temp); |
| 1959 |
NIR_PASS(_, nir, nir_split_array_vars, nir_var_function_temp); |
| 1960 |
NIR_PASS(_, nir, nir_split_per_member_structs); |
| 1961 |
NIR_PASS(_, nir, nir_lower_continue_constructs); |
| 1962 |
|
| 1963 |
NIR_PASS(_, nir, nir_lower_frexp); |
| 1964 |
|
| 1965 |
NIR_PASS(_, nir, nir_lower_vars_to_ssa); |
| 1966 |
NIR_PASS(_, nir, nir_remove_dead_variables, nir_var_function_temp, NULL); |
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
NIR_PASS(_, nir, nir_lower_system_values); |
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
NIR_PASS(_, nir, nir_opt_dce); |
| 1976 |
|
| 1977 |
if (nir->info.stage == MESA_SHADER_FRAGMENT) { |
| 1978 |
nir_input_attachment_options input_attachment_options = {}; |
| 1979 |
NIR_PASS(_, nir, nir_lower_input_attachments, &input_attachment_options); |
| 1980 |
} |
| 1981 |
NIR_PASS(_, nir, nir_opt_combine_barriers, NULL, NULL); |
| 1982 |
NIR_PASS(_, nir, nir_lower_var_copies); |
| 1983 |
NIR_PASS(_, nir, nir_split_var_copies); |
| 1984 |
|
| 1985 |
NIR_PASS(_, nir, nir_split_array_vars, |
| 1986 |
nir_var_function_temp | nir_var_shader_in | nir_var_shader_out); |
| 1987 |
NIR_PASS(_, nir, nir_lower_alu_to_scalar, kk_scalarize_filter, NULL); |
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
NIR_PASS(_, nir, nir_lower_vars_to_scratch, 32, |
| 1992 |
glsl_get_natural_size_align_bytes, glsl_get_word_size_align_bytes); |
| 1993 |
NIR_PASS(_, nir, nir_lower_indirect_derefs_to_if_else_trees, |
| 1994 |
nir_var_function_temp | nir_var_shader_in | nir_var_shader_out, |
| 1995 |
UINT32_MAX); |
| 1996 |
|
| 1997 |
msl_nir_lower_subgroups(nir); |
| 1998 |
} |
| 1999 |
|
| 2000 |
bool |
| 2001 |
msl_optimize_nir(struct nir_shader *nir) |
| 2002 |
{ |
| 2003 |
bool progress; |
| 2004 |
NIR_PASS(_, nir, nir_lower_int64); |
| 2005 |
do { |
| 2006 |
progress = false; |
| 2007 |
|
| 2008 |
NIR_PASS(progress, nir, nir_split_var_copies); |
| 2009 |
NIR_PASS(progress, nir, nir_split_struct_vars, nir_var_function_temp); |
| 2010 |
NIR_PASS(progress, nir, nir_lower_var_copies); |
| 2011 |
NIR_PASS(progress, nir, nir_lower_vars_to_ssa); |
| 2012 |
NIR_PASS(progress, nir, nir_opt_undef); |
| 2013 |
NIR_PASS(progress, nir, nir_opt_dce); |
| 2014 |
NIR_PASS(progress, nir, nir_opt_cse); |
| 2015 |
NIR_PASS(progress, nir, nir_opt_dead_cf); |
| 2016 |
NIR_PASS(progress, nir, nir_opt_copy_prop); |
| 2017 |
NIR_PASS(progress, nir, nir_opt_deref); |
| 2018 |
NIR_PASS(progress, nir, nir_opt_constant_folding); |
| 2019 |
NIR_PASS(progress, nir, nir_opt_copy_prop_vars); |
| 2020 |
NIR_PASS(progress, nir, nir_opt_dead_write_vars); |
| 2021 |
NIR_PASS(progress, nir, nir_opt_combine_stores, nir_var_all); |
| 2022 |
NIR_PASS(progress, nir, nir_remove_dead_variables, nir_var_function_temp, |
| 2023 |
NULL); |
| 2024 |
NIR_PASS(progress, nir, nir_opt_algebraic); |
| 2025 |
NIR_PASS(progress, nir, nir_opt_if, 0); |
| 2026 |
NIR_PASS(progress, nir, nir_opt_remove_phis); |
| 2027 |
NIR_PASS(progress, nir, nir_opt_loop); |
| 2028 |
NIR_PASS(progress, nir, nir_lower_pack); |
| 2029 |
NIR_PASS(progress, nir, nir_lower_alu_to_scalar, kk_scalarize_filter, |
| 2030 |
NULL); |
| 2031 |
} while (progress); |
| 2032 |
|
| 2033 |
|
| 2034 |
|
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
NIR_PASS(_, nir, nir_opt_shrink_stores, false); |
| 2039 |
NIR_PASS(_, nir, nir_lower_load_const_to_scalar); |
| 2040 |
NIR_PASS(_, nir, msl_nir_lower_algebraic_late); |
| 2041 |
NIR_PASS(_, nir, nir_convert_from_ssa, true, false); |
| 2042 |
NIR_PASS(_, nir, nir_trivialize_registers); |
| 2043 |
NIR_PASS(_, nir, nir_opt_copy_prop); |
| 2044 |
|
| 2045 |
return progress; |
| 2046 |
} |
| 2047 |
|
| 2048 |
|
| 2049 |
static bool |
| 2050 |
scalarize_clip_distance_filter(const nir_intrinsic_instr *intrin, |
| 2051 |
UNUSED const void *_data) |
| 2052 |
{ |
| 2053 |
if (intrin->intrinsic != nir_intrinsic_store_output) |
| 2054 |
return false; |
| 2055 |
nir_io_semantics semantics = nir_intrinsic_io_semantics(intrin); |
| 2056 |
return semantics.location == VARYING_SLOT_CLIP_DIST0 || |
| 2057 |
semantics.location == VARYING_SLOT_CLIP_DIST1; |
| 2058 |
} |
| 2059 |
|
| 2060 |
void |
| 2061 |
msl_lower_nir_late(nir_shader *nir) |
| 2062 |
{ |
| 2063 |
NIR_PASS(_, nir, nir_lower_io_to_scalar, nir_var_shader_out, |
| 2064 |
scalarize_clip_distance_filter, NULL); |
| 2065 |
NIR_PASS(_, nir, msl_nir_lower_clip_distance); |
| 2066 |
} |
| 2067 |
|
| 2068 |
static void |
| 2069 |
msl_gather_info(struct nir_to_msl_ctx *ctx, struct nir_to_msl_options *options) |
| 2070 |
{ |
| 2071 |
nir_function_impl *impl = nir_shader_get_entrypoint(ctx->shader); |
| 2072 |
ctx->types = msl_infer_types(ctx->shader); |
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
|
| 2077 |
nir_index_blocks(impl); |
| 2078 |
nir_index_ssa_defs(impl); |
| 2079 |
|
| 2080 |
if (ctx->shader->info.stage == MESA_SHADER_VERTEX || |
| 2081 |
ctx->shader->info.stage == MESA_SHADER_FRAGMENT) { |
| 2082 |
msl_gather_io_info(ctx, ctx->inputs_info, ctx->outputs_info); |
| 2083 |
|
| 2084 |
if (ctx->shader->info.stage == MESA_SHADER_FRAGMENT) { |
| 2085 |
|
| 2086 |
|
| 2087 |
|
| 2088 |
if (BITSET_TEST(ctx->shader->info.system_values_read, |
| 2089 |
SYSTEM_VALUE_SAMPLE_POS)) { |
| 2090 |
BITSET_CLEAR(ctx->shader->info.system_values_read, |
| 2091 |
SYSTEM_VALUE_SAMPLE_POS); |
| 2092 |
BITSET_SET(ctx->shader->info.system_values_read, |
| 2093 |
SYSTEM_VALUE_SAMPLE_ID); |
| 2094 |
} |
| 2095 |
|
| 2096 |
|
| 2097 |
BITSET_CLEAR(ctx->shader->info.system_values_read, |
| 2098 |
SYSTEM_VALUE_HELPER_INVOCATION); |
| 2099 |
|
| 2100 |
|
| 2101 |
|
| 2102 |
|
| 2103 |
if (BITSET_TEST(ctx->shader->info.system_values_read, |
| 2104 |
SYSTEM_VALUE_SAMPLE_MASK_IN)) |
| 2105 |
BITSET_SET(ctx->shader->info.system_values_read, |
| 2106 |
SYSTEM_VALUE_SAMPLE_ID); |
| 2107 |
|
| 2108 |
for (uint32_t i = 0u; i < MAX_DRAW_BUFFERS; ++i) { |
| 2109 |
uint32_t location = i + FRAG_RESULT_DATA0; |
| 2110 |
ctx->outputs_info[location].num_components = |
| 2111 |
options->rts_component_count[i]; |
| 2112 |
} |
| 2113 |
} |
| 2114 |
} |
| 2115 |
} |
| 2116 |
|
| 2117 |
static void |
| 2118 |
predeclare_ssa_values(struct nir_to_msl_ctx *ctx, nir_function_impl *impl) |
| 2119 |
{ |
| 2120 |
nir_foreach_block(block, impl) { |
| 2121 |
nir_foreach_instr(instr, block) { |
| 2122 |
nir_def *def; |
| 2123 |
switch (instr->type) { |
| 2124 |
case nir_instr_type_alu: { |
| 2125 |
nir_alu_instr *alu = nir_instr_as_alu(instr); |
| 2126 |
def = &alu->def; |
| 2127 |
break; |
| 2128 |
} |
| 2129 |
case nir_instr_type_intrinsic: { |
| 2130 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 2131 |
if (!instrinsic_needs_dest_type(intr)) |
| 2132 |
continue; |
| 2133 |
def = &intr->def; |
| 2134 |
break; |
| 2135 |
} |
| 2136 |
case nir_instr_type_tex: { |
| 2137 |
nir_tex_instr *tex = nir_instr_as_tex(instr); |
| 2138 |
def = &tex->def; |
| 2139 |
break; |
| 2140 |
} |
| 2141 |
default: |
| 2142 |
continue; |
| 2143 |
} |
| 2144 |
const char *type = msl_type_for_def(ctx->types, def); |
| 2145 |
if (!type) |
| 2146 |
continue; |
| 2147 |
if (msl_def_is_sampler(ctx, def)) { |
| 2148 |
P_IND(ctx, "%s t%u;\n", type, def->index); |
| 2149 |
} else |
| 2150 |
P_IND(ctx, "%s t%u = %s(0);\n", type, def->index, type); |
| 2151 |
} |
| 2152 |
} |
| 2153 |
} |
| 2154 |
|
| 2155 |
char * |
| 2156 |
nir_to_msl(nir_shader *shader, struct nir_to_msl_options *options) |
| 2157 |
{ |
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
rename_main_entrypoint(shader); |
| 2162 |
|
| 2163 |
struct nir_to_msl_ctx ctx = { |
| 2164 |
.shader = shader, |
| 2165 |
.text = _mesa_string_buffer_create(options->mem_ctx, 1024), |
| 2166 |
.disabled_workarounds = options->disabled_workarounds, |
| 2167 |
}; |
| 2168 |
nir_function_impl *impl = nir_shader_get_entrypoint(shader); |
| 2169 |
msl_gather_info(&ctx, options); |
| 2170 |
|
| 2171 |
P(&ctx, "// Generated by Mesa compiler\n"); |
| 2172 |
if (shader->info.stage == MESA_SHADER_COMPUTE) |
| 2173 |
P(&ctx, "#include <metal_compute>\n"); |
| 2174 |
P(&ctx, "#include <metal_stdlib>\n"); |
| 2175 |
P(&ctx, "using namespace metal;\n"); |
| 2176 |
|
| 2177 |
msl_emit_io_blocks(&ctx, shader); |
| 2178 |
if (shader->info.stage == MESA_SHADER_FRAGMENT && |
| 2179 |
shader->info.fs.early_fragment_tests) |
| 2180 |
P(&ctx, "[[early_fragment_tests]]\n"); |
| 2181 |
P(&ctx, "%s %s %s(\n", get_stage_string(shader->info.stage), |
| 2182 |
output_type(shader), get_entrypoint_name(shader)); |
| 2183 |
ctx.indentlevel++; |
| 2184 |
emit_sysvals(&ctx, shader); |
| 2185 |
emit_inputs(&ctx, shader); |
| 2186 |
ctx.indentlevel--; |
| 2187 |
P(&ctx, ")\n"); |
| 2188 |
P(&ctx, "{\n"); |
| 2189 |
ctx.indentlevel++; |
| 2190 |
msl_emit_output_var(&ctx, shader); |
| 2191 |
emit_local_vars(&ctx, shader); |
| 2192 |
predeclare_ssa_values(&ctx, impl); |
| 2193 |
foreach_list_typed(nir_cf_node, node, node, &impl->body) { |
| 2194 |
cf_node_to_metal(&ctx, node); |
| 2195 |
} |
| 2196 |
emit_output_return(&ctx, shader); |
| 2197 |
ctx.indentlevel--; |
| 2198 |
P(&ctx, "}\n"); |
| 2199 |
char *ret = ctx.text->buf; |
| 2200 |
_mesa_hash_table_destroy(ctx.types, NULL); |
| 2201 |
ralloc_steal(options->mem_ctx, ctx.text->buf); |
| 2202 |
ralloc_free(ctx.text); |
| 2203 |
return ret; |
| 2204 |
} |