| 1 | /** |
| 2 | * MojoShader; generate shader programs from bytecode of compiled |
| 3 | * Direct3D shaders. |
| 4 | * |
| 5 | * Please see the file LICENSE.txt in the source's root directory. |
| 6 | * |
| 7 | * This file written by Ryan C. Gordon. |
| 8 | */ |
| 9 | |
| 10 | #define __MOJOSHADER_INTERNAL__ 1 |
| 11 | #include "mojoshader_profile.h" |
| 12 | |
| 13 | #pragma GCC visibility push(hidden) |
| 14 | |
| 15 | // !!! FIXME: A lot of this is cut-and-paste from the GLSL version. |
| 16 | #if SUPPORT_PROFILE_METAL |
| 17 | |
| 18 | #define EMIT_METAL_OPCODE_UNIMPLEMENTED_FUNC(op) \ |
| 19 | void emit_METAL_##op(Context *ctx) { \ |
| 20 | fail(ctx, #op " unimplemented in Metal profile"); \ |
| 21 | } |
| 22 | |
| 23 | static inline const char *get_METAL_register_string(Context *ctx, |
| 24 | const RegisterType regtype, const int regnum, |
| 25 | char *regnum_str, const size_t regnum_size) |
| 26 | { |
| 27 | // turns out these are identical at the moment. |
| 28 | return get_D3D_register_string(ctx,regtype,regnum,regnum_str,regnum_size); |
| 29 | } // get_METAL_register_string |
| 30 | |
| 31 | const char *get_METAL_uniform_type(Context *ctx, const RegisterType rtype) |
| 32 | { |
| 33 | switch (rtype) |
| 34 | { |
| 35 | case REG_TYPE_CONST: return "float4"; |
| 36 | case REG_TYPE_CONSTINT: return "int4"; |
| 37 | case REG_TYPE_CONSTBOOL: return "bool"; |
| 38 | default: fail(ctx, "BUG: used a uniform we don't know how to define."); |
| 39 | } // switch |
| 40 | |
| 41 | return NULL; |
| 42 | } // get_METAL_uniform_type |
| 43 | |
| 44 | const char *get_METAL_varname_in_buf(Context *ctx, RegisterType rt, |
| 45 | int regnum, char *buf, |
| 46 | const size_t len) |
| 47 | { |
| 48 | char regnum_str[16]; |
| 49 | const char *regtype_str = get_METAL_register_string(ctx, rt, regnum, |
| 50 | regnum_str, sizeof (regnum_str)); |
| 51 | |
| 52 | // We don't separate vars with vs_ or ps_ here, because, for the most part, |
| 53 | // there are only local vars in Metal shaders. |
| 54 | snprintf(buf, len, "%s%s", regtype_str, regnum_str); |
| 55 | return buf; |
| 56 | } // get_METAL_varname_in_buf |
| 57 | |
| 58 | |
| 59 | const char *get_METAL_varname(Context *ctx, RegisterType rt, int regnum) |
| 60 | { |
| 61 | char buf[64]; |
| 62 | get_METAL_varname_in_buf(ctx, rt, regnum, buf, sizeof (buf)); |
| 63 | return StrDup(ctx, buf); |
| 64 | } // get_METAL_varname |
| 65 | |
| 66 | |
| 67 | static inline const char *get_METAL_const_array_varname_in_buf(Context *ctx, |
| 68 | const int base, const int size, |
| 69 | char *buf, const size_t buflen) |
| 70 | { |
| 71 | snprintf(buf, buflen, "const_array_%d_%d", base, size); |
| 72 | return buf; |
| 73 | } // get_METAL_const_array_varname_in_buf |
| 74 | |
| 75 | const char *get_METAL_const_array_varname(Context *ctx, int base, int size) |
| 76 | { |
| 77 | char buf[64]; |
| 78 | get_METAL_const_array_varname_in_buf(ctx, base, size, buf, sizeof (buf)); |
| 79 | return StrDup(ctx, buf); |
| 80 | } // get_METAL_const_array_varname |
| 81 | |
| 82 | |
| 83 | static inline const char *get_METAL_input_array_varname(Context *ctx, |
| 84 | char *buf, const size_t buflen) |
| 85 | { |
| 86 | snprintf(buf, buflen, "%s", "vertex_input_array"); |
| 87 | return buf; |
| 88 | } // get_METAL_input_array_varname |
| 89 | |
| 90 | |
| 91 | const char *get_METAL_uniform_array_varname(Context *ctx, |
| 92 | const RegisterType regtype, |
| 93 | char *buf, const size_t len) |
| 94 | { |
| 95 | const char *shadertype = ctx->shader_type_str; |
| 96 | const char *type = get_METAL_uniform_type(ctx, regtype); |
| 97 | snprintf(buf, len, "uniforms.uniforms_%s", type); |
| 98 | return buf; |
| 99 | } // get_METAL_uniform_array_varname |
| 100 | |
| 101 | const char *get_METAL_destarg_varname(Context *ctx, char *buf, size_t len) |
| 102 | { |
| 103 | const DestArgInfo *arg = &ctx->dest_arg; |
| 104 | return get_METAL_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, len); |
| 105 | } // get_METAL_destarg_varname |
| 106 | |
| 107 | const char *get_METAL_srcarg_varname(Context *ctx, const size_t idx, |
| 108 | char *buf, size_t len) |
| 109 | { |
| 110 | if (idx >= STATICARRAYLEN(ctx->source_args)) |
| 111 | { |
| 112 | fail(ctx, "Too many source args"); |
| 113 | *buf = '\0'; |
| 114 | return buf; |
| 115 | } // if |
| 116 | |
| 117 | const SourceArgInfo *arg = &ctx->source_args[idx]; |
| 118 | return get_METAL_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, len); |
| 119 | } // get_METAL_srcarg_varname |
| 120 | |
| 121 | |
| 122 | const char *make_METAL_destarg_assign(Context *, char *, const size_t, |
| 123 | const char *, ...) ISPRINTF(4,5); |
| 124 | |
| 125 | const char *make_METAL_destarg_assign(Context *ctx, char *buf, |
| 126 | const size_t buflen, |
| 127 | const char *fmt, ...) |
| 128 | { |
| 129 | int need_parens = 0; |
| 130 | const DestArgInfo *arg = &ctx->dest_arg; |
| 131 | |
| 132 | if (arg->writemask == 0) |
| 133 | { |
| 134 | *buf = '\0'; |
| 135 | return buf; // no writemask? It's a no-op. |
| 136 | } // if |
| 137 | |
| 138 | char clampbuf[32] = { '\0' }; |
| 139 | const char *clampleft = ""; |
| 140 | const char *clampright = ""; |
| 141 | if (arg->result_mod & MOD_SATURATE) |
| 142 | { |
| 143 | ctx->metal_need_header_common = 1; |
| 144 | const int vecsize = vecsize_from_writemask(arg->writemask); |
| 145 | clampleft = "clamp("; |
| 146 | if (vecsize == 1) |
| 147 | clampright = ", 0.0, 1.0)"; |
| 148 | else |
| 149 | { |
| 150 | snprintf(clampbuf, sizeof (clampbuf), |
| 151 | ", float%d(0.0), float%d(1.0))", vecsize, vecsize); |
| 152 | clampright = clampbuf; |
| 153 | } // else |
| 154 | } // if |
| 155 | |
| 156 | // MSDN says MOD_PP is a hint and many implementations ignore it. So do we. |
| 157 | |
| 158 | // CENTROID only allowed in DCL opcodes, which shouldn't come through here. |
| 159 | assert((arg->result_mod & MOD_CENTROID) == 0); |
| 160 | |
| 161 | if (ctx->predicated) |
| 162 | { |
| 163 | fail(ctx, "predicated destinations unsupported"); // !!! FIXME |
| 164 | *buf = '\0'; |
| 165 | return buf; |
| 166 | } // if |
| 167 | |
| 168 | char operation[256]; |
| 169 | va_list ap; |
| 170 | va_start(ap, fmt); |
| 171 | const int len = vsnprintf(operation, sizeof (operation), fmt, ap); |
| 172 | va_end(ap); |
| 173 | if (len >= sizeof (operation)) |
| 174 | { |
| 175 | fail(ctx, "operation string too large"); // I'm lazy. :P |
| 176 | *buf = '\0'; |
| 177 | return buf; |
| 178 | } // if |
| 179 | |
| 180 | const char *result_shift_str = ""; |
| 181 | switch (arg->result_shift) |
| 182 | { |
| 183 | case 0x1: result_shift_str = " * 2.0"; break; |
| 184 | case 0x2: result_shift_str = " * 4.0"; break; |
| 185 | case 0x3: result_shift_str = " * 8.0"; break; |
| 186 | case 0xD: result_shift_str = " / 8.0"; break; |
| 187 | case 0xE: result_shift_str = " / 4.0"; break; |
| 188 | case 0xF: result_shift_str = " / 2.0"; break; |
| 189 | } // switch |
| 190 | need_parens |= (result_shift_str[0] != '\0'); |
| 191 | |
| 192 | char regnum_str[16]; |
| 193 | const char *regtype_str = get_METAL_register_string(ctx, arg->regtype, |
| 194 | arg->regnum, regnum_str, |
| 195 | sizeof (regnum_str)); |
| 196 | char writemask_str[6]; |
| 197 | size_t i = 0; |
| 198 | const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum); |
| 199 | if (!scalar && !writemask_xyzw(arg->writemask)) |
| 200 | { |
| 201 | writemask_str[i++] = '.'; |
| 202 | if (arg->writemask0) writemask_str[i++] = 'x'; |
| 203 | if (arg->writemask1) writemask_str[i++] = 'y'; |
| 204 | if (arg->writemask2) writemask_str[i++] = 'z'; |
| 205 | if (arg->writemask3) writemask_str[i++] = 'w'; |
| 206 | } // if |
| 207 | writemask_str[i] = '\0'; |
| 208 | assert(i < sizeof (writemask_str)); |
| 209 | |
| 210 | const char *leftparen = (need_parens) ? "(" : ""; |
| 211 | const char *rightparen = (need_parens) ? ")" : ""; |
| 212 | |
| 213 | snprintf(buf, buflen, "%s%s%s = %s%s%s%s%s%s;", |
| 214 | regtype_str, regnum_str, writemask_str, |
| 215 | clampleft, leftparen, operation, rightparen, result_shift_str, |
| 216 | clampright); |
| 217 | // !!! FIXME: make sure the scratch buffer was large enough. |
| 218 | return buf; |
| 219 | } // make_METAL_destarg_assign |
| 220 | |
| 221 | |
| 222 | char *make_METAL_swizzle_string(char *swiz_str, const size_t strsize, |
| 223 | const int swizzle, const int writemask) |
| 224 | { |
| 225 | size_t i = 0; |
| 226 | if ( (!no_swizzle(swizzle)) || (!writemask_xyzw(writemask)) ) |
| 227 | { |
| 228 | const int writemask0 = (writemask >> 0) & 0x1; |
| 229 | const int writemask1 = (writemask >> 1) & 0x1; |
| 230 | const int writemask2 = (writemask >> 2) & 0x1; |
| 231 | const int writemask3 = (writemask >> 3) & 0x1; |
| 232 | |
| 233 | const int swizzle_x = (swizzle >> 0) & 0x3; |
| 234 | const int swizzle_y = (swizzle >> 2) & 0x3; |
| 235 | const int swizzle_z = (swizzle >> 4) & 0x3; |
| 236 | const int swizzle_w = (swizzle >> 6) & 0x3; |
| 237 | |
| 238 | swiz_str[i++] = '.'; |
| 239 | if (writemask0) swiz_str[i++] = swizzle_channels[swizzle_x]; |
| 240 | if (writemask1) swiz_str[i++] = swizzle_channels[swizzle_y]; |
| 241 | if (writemask2) swiz_str[i++] = swizzle_channels[swizzle_z]; |
| 242 | if (writemask3) swiz_str[i++] = swizzle_channels[swizzle_w]; |
| 243 | } // if |
| 244 | assert(i < strsize); |
| 245 | swiz_str[i] = '\0'; |
| 246 | return swiz_str; |
| 247 | } // make_METAL_swizzle_string |
| 248 | |
| 249 | |
| 250 | const char *make_METAL_srcarg_string(Context *ctx, const size_t idx, |
| 251 | const int writemask, char *buf, |
| 252 | const size_t buflen) |
| 253 | { |
| 254 | *buf = '\0'; |
| 255 | |
| 256 | if (idx >= STATICARRAYLEN(ctx->source_args)) |
| 257 | { |
| 258 | fail(ctx, "Too many source args"); |
| 259 | return buf; |
| 260 | } // if |
| 261 | |
| 262 | const SourceArgInfo *arg = &ctx->source_args[idx]; |
| 263 | |
| 264 | const char *premod_str = ""; |
| 265 | const char *postmod_str = ""; |
| 266 | switch (arg->src_mod) |
| 267 | { |
| 268 | case SRCMOD_NEGATE: |
| 269 | premod_str = "-"; |
| 270 | break; |
| 271 | |
| 272 | case SRCMOD_BIASNEGATE: |
| 273 | premod_str = "-("; |
| 274 | postmod_str = " - 0.5)"; |
| 275 | break; |
| 276 | |
| 277 | case SRCMOD_BIAS: |
| 278 | premod_str = "("; |
| 279 | postmod_str = " - 0.5)"; |
| 280 | break; |
| 281 | |
| 282 | case SRCMOD_SIGNNEGATE: |
| 283 | premod_str = "-(("; |
| 284 | postmod_str = " - 0.5) * 2.0)"; |
| 285 | break; |
| 286 | |
| 287 | case SRCMOD_SIGN: |
| 288 | premod_str = "(("; |
| 289 | postmod_str = " - 0.5) * 2.0)"; |
| 290 | break; |
| 291 | |
| 292 | case SRCMOD_COMPLEMENT: |
| 293 | premod_str = "(1.0 - "; |
| 294 | postmod_str = ")"; |
| 295 | break; |
| 296 | |
| 297 | case SRCMOD_X2NEGATE: |
| 298 | premod_str = "-("; |
| 299 | postmod_str = " * 2.0)"; |
| 300 | break; |
| 301 | |
| 302 | case SRCMOD_X2: |
| 303 | premod_str = "("; |
| 304 | postmod_str = " * 2.0)"; |
| 305 | break; |
| 306 | |
| 307 | case SRCMOD_DZ: |
| 308 | fail(ctx, "SRCMOD_DZ unsupported"); return buf; // !!! FIXME |
| 309 | postmod_str = "_dz"; |
| 310 | break; |
| 311 | |
| 312 | case SRCMOD_DW: |
| 313 | fail(ctx, "SRCMOD_DW unsupported"); return buf; // !!! FIXME |
| 314 | postmod_str = "_dw"; |
| 315 | break; |
| 316 | |
| 317 | case SRCMOD_ABSNEGATE: |
| 318 | ctx->metal_need_header_math = 1; |
| 319 | premod_str = "-abs("; |
| 320 | postmod_str = ")"; |
| 321 | break; |
| 322 | |
| 323 | case SRCMOD_ABS: |
| 324 | ctx->metal_need_header_math = 1; |
| 325 | premod_str = "abs("; |
| 326 | postmod_str = ")"; |
| 327 | break; |
| 328 | |
| 329 | case SRCMOD_NOT: |
| 330 | premod_str = "!"; |
| 331 | break; |
| 332 | |
| 333 | case SRCMOD_NONE: |
| 334 | case SRCMOD_TOTAL: |
| 335 | break; // stop compiler whining. |
| 336 | } // switch |
| 337 | |
| 338 | const char *regtype_str = NULL; |
| 339 | |
| 340 | if (!arg->relative) |
| 341 | { |
| 342 | regtype_str = get_METAL_varname_in_buf(ctx, arg->regtype, arg->regnum, |
| 343 | (char *) alloca(64), 64); |
| 344 | } // if |
| 345 | |
| 346 | const char *rel_lbracket = ""; |
| 347 | char rel_offset[32] = { '\0' }; |
| 348 | const char *rel_rbracket = ""; |
| 349 | char rel_swizzle[4] = { '\0' }; |
| 350 | const char *rel_regtype_str = ""; |
| 351 | if (arg->relative) |
| 352 | { |
| 353 | if (arg->regtype == REG_TYPE_INPUT) |
| 354 | regtype_str=get_METAL_input_array_varname(ctx,(char*)alloca(64),64); |
| 355 | else |
| 356 | { |
| 357 | assert(arg->regtype == REG_TYPE_CONST); |
| 358 | const int arrayidx = arg->relative_array->index; |
| 359 | const int offset = arg->regnum - arrayidx; |
| 360 | assert(offset >= 0); |
| 361 | if (arg->relative_array->constant) |
| 362 | { |
| 363 | const int arraysize = arg->relative_array->count; |
| 364 | regtype_str = get_METAL_const_array_varname_in_buf(ctx, |
| 365 | arrayidx, arraysize, (char *) alloca(64), 64); |
| 366 | if (offset != 0) |
| 367 | snprintf(rel_offset, sizeof (rel_offset), "%d + ", offset); |
| 368 | } // if |
| 369 | else |
| 370 | { |
| 371 | regtype_str = get_METAL_uniform_array_varname(ctx, arg->regtype, |
| 372 | (char *) alloca(64), 64); |
| 373 | if (offset == 0) |
| 374 | { |
| 375 | snprintf(rel_offset, sizeof (rel_offset), |
| 376 | "ARRAYBASE_%d + ", arrayidx); |
| 377 | } // if |
| 378 | else |
| 379 | { |
| 380 | snprintf(rel_offset, sizeof (rel_offset), |
| 381 | "(ARRAYBASE_%d + %d) + ", arrayidx, offset); |
| 382 | } // else |
| 383 | } // else |
| 384 | } // else |
| 385 | |
| 386 | rel_lbracket = "["; |
| 387 | |
| 388 | rel_regtype_str = get_METAL_varname_in_buf(ctx, arg->relative_regtype, |
| 389 | arg->relative_regnum, |
| 390 | (char *) alloca(64), 64); |
| 391 | rel_swizzle[0] = '.'; |
| 392 | rel_swizzle[1] = swizzle_channels[arg->relative_component]; |
| 393 | rel_swizzle[2] = '\0'; |
| 394 | rel_rbracket = "]"; |
| 395 | } // if |
| 396 | |
| 397 | char swiz_str[6] = { '\0' }; |
| 398 | if (!isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum)) |
| 399 | { |
| 400 | make_METAL_swizzle_string(swiz_str, sizeof (swiz_str), |
| 401 | arg->swizzle, writemask); |
| 402 | } // if |
| 403 | |
| 404 | if (regtype_str == NULL) |
| 405 | { |
| 406 | fail(ctx, "Unknown source register type."); |
| 407 | return buf; |
| 408 | } // if |
| 409 | |
| 410 | snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s", |
| 411 | premod_str, regtype_str, rel_lbracket, rel_offset, |
| 412 | rel_regtype_str, rel_swizzle, rel_rbracket, swiz_str, |
| 413 | postmod_str); |
| 414 | // !!! FIXME: make sure the scratch buffer was large enough. |
| 415 | return buf; |
| 416 | } // make_METAL_srcarg_string |
| 417 | |
| 418 | // generate some convenience functions. |
| 419 | #define MAKE_METAL_SRCARG_STRING_(mask, bitmask) \ |
| 420 | static inline const char *make_METAL_srcarg_string_##mask(Context *ctx, \ |
| 421 | const size_t idx, char *buf, \ |
| 422 | const size_t buflen) { \ |
| 423 | return make_METAL_srcarg_string(ctx, idx, bitmask, buf, buflen); \ |
| 424 | } |
| 425 | MAKE_METAL_SRCARG_STRING_(x, (1 << 0)) |
| 426 | MAKE_METAL_SRCARG_STRING_(y, (1 << 1)) |
| 427 | MAKE_METAL_SRCARG_STRING_(z, (1 << 2)) |
| 428 | MAKE_METAL_SRCARG_STRING_(w, (1 << 3)) |
| 429 | MAKE_METAL_SRCARG_STRING_(scalar, (1 << 0)) |
| 430 | MAKE_METAL_SRCARG_STRING_(full, 0xF) |
| 431 | MAKE_METAL_SRCARG_STRING_(masked, ctx->dest_arg.writemask) |
| 432 | MAKE_METAL_SRCARG_STRING_(vec3, 0x7) |
| 433 | MAKE_METAL_SRCARG_STRING_(vec2, 0x3) |
| 434 | #undef MAKE_METAL_SRCARG_STRING_ |
| 435 | |
| 436 | // special cases for comparison opcodes... |
| 437 | |
| 438 | const char *get_METAL_comparison_string_scalar(Context *ctx) |
| 439 | { |
| 440 | const char *comps[] = { "", ">", "==", ">=", "<", "!=", "<=" }; |
| 441 | if (ctx->instruction_controls >= STATICARRAYLEN(comps)) |
| 442 | { |
| 443 | fail(ctx, "unknown comparison control"); |
| 444 | return ""; |
| 445 | } // if |
| 446 | |
| 447 | return comps[ctx->instruction_controls]; |
| 448 | } // get_METAL_comparison_string_scalar |
| 449 | |
| 450 | const char *get_METAL_comparison_string_vector(Context *ctx) |
| 451 | { |
| 452 | return get_METAL_comparison_string_scalar(ctx); // standard C operators work for vectors in Metal. |
| 453 | } // get_METAL_comparison_string_vector |
| 454 | |
| 455 | |
| 456 | void emit_METAL_start(Context *ctx, const char *profilestr) |
| 457 | { |
| 458 | if (!shader_is_vertex(ctx) && !shader_is_pixel(ctx)) |
| 459 | { |
| 460 | failf(ctx, "Shader type %u unsupported in this profile.", |
| 461 | (uint) ctx->shader_type); |
| 462 | return; |
| 463 | } // if |
| 464 | |
| 465 | if (!ctx->mainfn) |
| 466 | { |
| 467 | if (shader_is_vertex(ctx)) |
| 468 | ctx->mainfn = StrDup(ctx, "VertexShader"); |
| 469 | else if (shader_is_pixel(ctx)) |
| 470 | ctx->mainfn = StrDup(ctx, "FragmentShader"); |
| 471 | } // if |
| 472 | |
| 473 | set_output(ctx, &ctx->mainline); |
| 474 | ctx->indent++; |
| 475 | } // emit_METAL_start |
| 476 | |
| 477 | void emit_METAL_RET(Context *ctx); |
| 478 | void emit_METAL_end(Context *ctx) |
| 479 | { |
| 480 | // !!! FIXME: maybe handle this at a higher level? |
| 481 | // ps_1_* writes color to r0 instead oC0. We move it to the right place. |
| 482 | // We don't have to worry about a RET opcode messing this up, since |
| 483 | // RET isn't available before ps_2_0. |
| 484 | if (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 2, 0)) |
| 485 | { |
| 486 | set_used_register(ctx, REG_TYPE_COLOROUT, 0, 1); |
| 487 | output_line(ctx, "oC0 = r0;"); |
| 488 | } // if |
| 489 | |
| 490 | // !!! FIXME: maybe handle this at a higher level? |
| 491 | // force a RET opcode if we're at the end of the stream without one. |
| 492 | if (ctx->previous_opcode != OPCODE_RET) |
| 493 | emit_METAL_RET(ctx); |
| 494 | } // emit_METAL_end |
| 495 | |
| 496 | void emit_METAL_phase(Context *ctx) |
| 497 | { |
| 498 | // no-op in Metal. |
| 499 | } // emit_METAL_phase |
| 500 | |
| 501 | void emit_METAL_finalize(Context *ctx) |
| 502 | { |
| 503 | // If we had a relative addressing of REG_TYPE_INPUT, we need to build |
| 504 | // an array for it at the start of main(). GLSL doesn't let you specify |
| 505 | // arrays of attributes. |
| 506 | //float4 blah_array[BIGGEST_ARRAY]; |
| 507 | if (ctx->have_relative_input_registers) // !!! FIXME |
| 508 | fail(ctx, "Relative addressing of input registers not supported."); |
| 509 | |
| 510 | // Insert header includes we need... |
| 511 | push_output(ctx, &ctx->preflight); |
| 512 | #define INC_METAL_HEADER(name) \ |
| 513 | if (ctx->metal_need_header_##name) { \ |
| 514 | output_line(ctx, "#include <metal_" #name ">"); \ |
| 515 | } |
| 516 | INC_METAL_HEADER(common); |
| 517 | INC_METAL_HEADER(math); |
| 518 | INC_METAL_HEADER(relational); |
| 519 | INC_METAL_HEADER(geometric); |
| 520 | INC_METAL_HEADER(graphics); |
| 521 | INC_METAL_HEADER(texture); |
| 522 | #undef INC_METAL_HEADER |
| 523 | output_blank_line(ctx); |
| 524 | output_line(ctx, "using namespace metal;"); |
| 525 | output_blank_line(ctx); |
| 526 | pop_output(ctx); |
| 527 | |
| 528 | // Fill in the shader's mainline function signature. |
| 529 | push_output(ctx, &ctx->mainline_intro); |
| 530 | output_line(ctx, "%s %s%s %s (", |
| 531 | shader_is_vertex(ctx) ? "vertex" : "fragment", |
| 532 | ctx->outputs ? ctx->mainfn : "void", |
| 533 | ctx->outputs ? "_Output" : "", ctx->mainfn); |
| 534 | pop_output(ctx); |
| 535 | |
| 536 | push_output(ctx, &ctx->mainline_arguments); |
| 537 | ctx->indent++; |
| 538 | |
| 539 | const int uniform_count = ctx->uniform_float4_count + ctx->uniform_int4_count + ctx->uniform_bool_count; |
| 540 | int commas = 0; |
| 541 | if (uniform_count) commas++; |
| 542 | if (ctx->inputs) commas++; |
| 543 | if (commas) commas--; |
| 544 | |
| 545 | if (uniform_count > 0) |
| 546 | { |
| 547 | push_output(ctx, &ctx->globals); |
| 548 | output_line(ctx, "struct %s_Uniforms", ctx->mainfn); |
| 549 | output_line(ctx, "{"); |
| 550 | ctx->indent++; |
| 551 | if (ctx->uniform_float4_count > 0) |
| 552 | output_line(ctx, "float4 uniforms_float4[%d];", ctx->uniform_float4_count); |
| 553 | if (ctx->uniform_int4_count > 0) |
| 554 | output_line(ctx, "int4 uniforms_int4[%d];", ctx->uniform_int4_count); |
| 555 | if (ctx->uniform_bool_count > 0) |
| 556 | output_line(ctx, "bool uniforms_bool[%d];", ctx->uniform_bool_count); |
| 557 | ctx->indent--; |
| 558 | output_line(ctx, "};"); |
| 559 | pop_output(ctx); |
| 560 | |
| 561 | output_line(ctx, "constant %s_Uniforms &uniforms [[buffer(0)]]%s", ctx->mainfn, commas ? "," : ""); |
| 562 | commas--; |
| 563 | } // if |
| 564 | |
| 565 | if (ctx->inputs) |
| 566 | { |
| 567 | output_line(ctx, "%s_Input input [[stage_in]]%s", ctx->mainfn, commas ? "," : ""); |
| 568 | commas--; |
| 569 | } // if |
| 570 | |
| 571 | ctx->indent--; |
| 572 | output_line(ctx, ") {"); |
| 573 | if (ctx->outputs) |
| 574 | { |
| 575 | ctx->indent++; |
| 576 | output_line(ctx, "%s_Output output;", ctx->mainfn); |
| 577 | |
| 578 | push_output(ctx, &ctx->mainline); |
| 579 | ctx->indent++; |
| 580 | output_line(ctx, "return output;"); |
| 581 | pop_output(ctx); |
| 582 | } // if |
| 583 | pop_output(ctx); |
| 584 | |
| 585 | if (ctx->inputs) |
| 586 | { |
| 587 | push_output(ctx, &ctx->inputs); |
| 588 | output_line(ctx, "};"); |
| 589 | output_blank_line(ctx); |
| 590 | pop_output(ctx); |
| 591 | } // if |
| 592 | |
| 593 | if (ctx->outputs) |
| 594 | { |
| 595 | push_output(ctx, &ctx->outputs); |
| 596 | output_line(ctx, "};"); |
| 597 | output_blank_line(ctx); |
| 598 | pop_output(ctx); |
| 599 | } // if |
| 600 | |
| 601 | // throw some blank lines around to make source more readable. |
| 602 | if (ctx->globals) // don't add a blank line if the section is empty. |
| 603 | { |
| 604 | push_output(ctx, &ctx->globals); |
| 605 | output_blank_line(ctx); |
| 606 | pop_output(ctx); |
| 607 | } // if |
| 608 | } // emit_METAL_finalize |
| 609 | |
| 610 | void emit_METAL_global(Context *ctx, RegisterType regtype, int regnum) |
| 611 | { |
| 612 | char varname[64]; |
| 613 | get_METAL_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname)); |
| 614 | |
| 615 | // These aren't actually global in metal, set them up at top of mainline. |
| 616 | push_output(ctx, &ctx->mainline_top); |
| 617 | ctx->indent++; |
| 618 | |
| 619 | switch (regtype) |
| 620 | { |
| 621 | case REG_TYPE_ADDRESS: |
| 622 | if (shader_is_vertex(ctx)) |
| 623 | output_line(ctx, "int4 %s;", varname); |
| 624 | else if (shader_is_pixel(ctx)) // actually REG_TYPE_TEXTURE. |
| 625 | { |
| 626 | // We have to map texture registers to temps for ps_1_1, since |
| 627 | // they work like temps, initialize with tex coords, and the |
| 628 | // ps_1_1 TEX opcode expects to overwrite it. |
| 629 | if (!shader_version_atleast(ctx, 1, 4)) |
| 630 | output_line(ctx, "float4 %s = input.%s;",varname,varname); |
| 631 | } // else if |
| 632 | break; |
| 633 | case REG_TYPE_PREDICATE: |
| 634 | output_line(ctx, "bool4 %s;", varname); |
| 635 | break; |
| 636 | case REG_TYPE_TEMP: |
| 637 | output_line(ctx, "float4 %s;", varname); |
| 638 | break; |
| 639 | case REG_TYPE_LOOP: |
| 640 | break; // no-op. We declare these in for loops at the moment. |
| 641 | case REG_TYPE_LABEL: |
| 642 | break; // no-op. If we see it here, it means we optimized it out. |
| 643 | default: |
| 644 | fail(ctx, "BUG: we used a register we don't know how to define."); |
| 645 | break; |
| 646 | } // switch |
| 647 | |
| 648 | pop_output(ctx); |
| 649 | } // emit_METAL_global |
| 650 | |
| 651 | void emit_METAL_array(Context *ctx, VariableList *var) |
| 652 | { |
| 653 | // All uniforms (except constant arrays, which are literally constant |
| 654 | // data embedded in Metal shaders) are now packed into a single array, |
| 655 | // so we can batch the uniform transfers. So this doesn't actually |
| 656 | // define an array here; the one, big array is emitted during |
| 657 | // finalization instead. |
| 658 | // However, we need to #define the offset into the one, big array here, |
| 659 | // and let dereferences use that #define. |
| 660 | const int base = var->index; |
| 661 | const int metalbase = ctx->uniform_float4_count; |
| 662 | push_output(ctx, &ctx->mainline_top); |
| 663 | ctx->indent++; |
| 664 | output_line(ctx, "const int ARRAYBASE_%d = %d;", base, metalbase); |
| 665 | pop_output(ctx); |
| 666 | var->emit_position = metalbase; |
| 667 | } // emit_METAL_array |
| 668 | |
| 669 | void emit_METAL_const_array(Context *ctx, const ConstantsList *clist, |
| 670 | int base, int size) |
| 671 | { |
| 672 | char varname[64]; |
| 673 | get_METAL_const_array_varname_in_buf(ctx,base,size,varname,sizeof(varname)); |
| 674 | |
| 675 | const char *cstr = NULL; |
| 676 | push_output(ctx, &ctx->mainline_top); |
| 677 | ctx->indent++; |
| 678 | output_line(ctx, "const float4 %s[%d] = {", varname, size); |
| 679 | ctx->indent++; |
| 680 | |
| 681 | int i; |
| 682 | for (i = 0; i < size; i++) |
| 683 | { |
| 684 | while (clist->constant.type != MOJOSHADER_UNIFORM_FLOAT) |
| 685 | clist = clist->next; |
| 686 | assert(clist->constant.index == (base + i)); |
| 687 | |
| 688 | char val0[32]; |
| 689 | char val1[32]; |
| 690 | char val2[32]; |
| 691 | char val3[32]; |
| 692 | floatstr(ctx, val0, sizeof (val0), clist->constant.value.f[0], 1); |
| 693 | floatstr(ctx, val1, sizeof (val1), clist->constant.value.f[1], 1); |
| 694 | floatstr(ctx, val2, sizeof (val2), clist->constant.value.f[2], 1); |
| 695 | floatstr(ctx, val3, sizeof (val3), clist->constant.value.f[3], 1); |
| 696 | |
| 697 | output_line(ctx, "float4(%s, %s, %s, %s)%s", val0, val1, val2, val3, |
| 698 | (i < (size-1)) ? "," : ""); |
| 699 | |
| 700 | clist = clist->next; |
| 701 | } // for |
| 702 | |
| 703 | ctx->indent--; |
| 704 | output_line(ctx, "};"); |
| 705 | output_line(ctx, "(void) %s[0];", varname); // stop compiler warnings. |
| 706 | pop_output(ctx); |
| 707 | } // emit_METAL_const_array |
| 708 | |
| 709 | void emit_METAL_uniform(Context *ctx, RegisterType regtype, int regnum, |
| 710 | const VariableList *var) |
| 711 | { |
| 712 | // Now that we're pushing all the uniforms as one struct, pack these |
| 713 | // down, so if we only use register c439, it'll actually map to |
| 714 | // uniforms.uniforms_float4[0]. As we push one big struct, this will |
| 715 | // prevent uploading unused data. |
| 716 | |
| 717 | const char *utype = get_METAL_uniform_type(ctx, regtype); |
| 718 | char varname[64]; |
| 719 | char name[64]; |
| 720 | int index = 0; |
| 721 | |
| 722 | get_METAL_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname)); |
| 723 | |
| 724 | push_output(ctx, &ctx->mainline_top); |
| 725 | ctx->indent++; |
| 726 | |
| 727 | if (var == NULL) |
| 728 | { |
| 729 | get_METAL_uniform_array_varname(ctx, regtype, name, sizeof (name)); |
| 730 | |
| 731 | if (regtype == REG_TYPE_CONST) |
| 732 | index = ctx->uniform_float4_count; |
| 733 | else if (regtype == REG_TYPE_CONSTINT) |
| 734 | index = ctx->uniform_int4_count; |
| 735 | else if (regtype == REG_TYPE_CONSTBOOL) |
| 736 | index = ctx->uniform_bool_count; |
| 737 | else // get_METAL_uniform_array_varname() would have called fail(). |
| 738 | assert(!(ctx->isfail)); |
| 739 | |
| 740 | // !!! FIXME: can cause unused var warnings in Clang... |
| 741 | //output_line(ctx, "constant %s &%s = %s[%d];", utype, varname, name, index); |
| 742 | output_line(ctx, "#define %s %s[%d]", varname, name, index); |
| 743 | push_output(ctx, &ctx->mainline); |
| 744 | ctx->indent++; |
| 745 | output_line(ctx, "#undef %s", varname); // !!! FIXME: gross. |
| 746 | pop_output(ctx); |
| 747 | } // if |
| 748 | |
| 749 | else |
| 750 | { |
| 751 | const int arraybase = var->index; |
| 752 | if (var->constant) |
| 753 | { |
| 754 | get_METAL_const_array_varname_in_buf(ctx, arraybase, var->count, |
| 755 | name, sizeof (name)); |
| 756 | index = (regnum - arraybase); |
| 757 | } // if |
| 758 | else |
| 759 | { |
| 760 | assert(var->emit_position != -1); |
| 761 | get_METAL_uniform_array_varname(ctx, regtype, name, sizeof (name)); |
| 762 | index = (regnum - arraybase) + var->emit_position; |
| 763 | } // else |
| 764 | |
| 765 | // !!! FIXME: might trigger unused var warnings in Clang. |
| 766 | //output_line(ctx, "constant %s &%s = %s[%d];", utype, varname, name, index); |
| 767 | output_line(ctx, "#define %s %s[%d];", varname, name, index); |
| 768 | push_output(ctx, &ctx->mainline); |
| 769 | ctx->indent++; |
| 770 | output_line(ctx, "#undef %s", varname); // !!! FIXME: gross. |
| 771 | pop_output(ctx); |
| 772 | } // else |
| 773 | |
| 774 | pop_output(ctx); |
| 775 | } // emit_METAL_uniform |
| 776 | |
| 777 | void emit_METAL_sampler(Context *ctx,int stage,TextureType ttype,int tb) |
| 778 | { |
| 779 | char var[64]; |
| 780 | const char *texsuffix = NULL; |
| 781 | switch (ttype) |
| 782 | { |
| 783 | case TEXTURE_TYPE_2D: texsuffix = "2d"; break; |
| 784 | case TEXTURE_TYPE_CUBE: texsuffix = "cube"; break; |
| 785 | case TEXTURE_TYPE_VOLUME: texsuffix = "3d"; break; |
| 786 | default: assert(!"unexpected texture type"); return; |
| 787 | } // switch |
| 788 | |
| 789 | get_METAL_varname_in_buf(ctx, REG_TYPE_SAMPLER, stage, var, sizeof (var)); |
| 790 | |
| 791 | push_output(ctx, &ctx->mainline_arguments); |
| 792 | ctx->indent++; |
| 793 | output_line(ctx, "texture%s<float> %s_texture [[texture(%d)]],", |
| 794 | texsuffix, var, stage); |
| 795 | output_line(ctx, "sampler %s [[sampler(%d)]],", var, stage); |
| 796 | pop_output(ctx); |
| 797 | |
| 798 | if (tb) // This sampler used a ps_1_1 TEXBEM opcode? |
| 799 | { |
| 800 | push_output(ctx, &ctx->mainline_top); |
| 801 | ctx->indent++; |
| 802 | char name[64]; |
| 803 | const int index = ctx->uniform_float4_count; |
| 804 | ctx->uniform_float4_count += 2; |
| 805 | get_METAL_uniform_array_varname(ctx, REG_TYPE_CONST, name, sizeof (name)); |
| 806 | output_line(ctx, "constant float4 &%s_texbem = %s[%d];", var, name, index); |
| 807 | output_line(ctx, "constant float4 &%s_texbeml = %s[%d];", var, name, index+1); |
| 808 | pop_output(ctx); |
| 809 | } // if |
| 810 | } // emit_METAL_sampler |
| 811 | |
| 812 | void emit_METAL_attribute(Context *ctx, RegisterType regtype, int regnum, |
| 813 | MOJOSHADER_usage usage, int index, int wmask, |
| 814 | int flags) |
| 815 | { |
| 816 | // !!! FIXME: this function doesn't deal with write masks at all yet! |
| 817 | const char *usage_str = NULL; |
| 818 | char index_str[16] = { '\0' }; |
| 819 | char var[64]; |
| 820 | |
| 821 | get_METAL_varname_in_buf(ctx, regtype, regnum, var, sizeof (var)); |
| 822 | |
| 823 | //assert((flags & MOD_PP) == 0); // !!! FIXME: is PP allowed? |
| 824 | |
| 825 | if (index != 0) // !!! FIXME: a lot of these MUST be zero. |
| 826 | snprintf(index_str, sizeof (index_str), "%u", (uint) index); |
| 827 | |
| 828 | if (shader_is_vertex(ctx)) |
| 829 | { |
| 830 | // pre-vs3 output registers. |
| 831 | // these don't ever happen in DCL opcodes, I think. Map to vs_3_* |
| 832 | // output registers. |
| 833 | if (!shader_version_atleast(ctx, 3, 0)) |
| 834 | { |
| 835 | if (regtype == REG_TYPE_RASTOUT) |
| 836 | { |
| 837 | regtype = REG_TYPE_OUTPUT; |
| 838 | index = regnum; |
| 839 | switch ((const RastOutType) regnum) |
| 840 | { |
| 841 | case RASTOUT_TYPE_POSITION: |
| 842 | usage = MOJOSHADER_USAGE_POSITION; |
| 843 | break; |
| 844 | case RASTOUT_TYPE_FOG: |
| 845 | usage = MOJOSHADER_USAGE_FOG; |
| 846 | break; |
| 847 | case RASTOUT_TYPE_POINT_SIZE: |
| 848 | usage = MOJOSHADER_USAGE_POINTSIZE; |
| 849 | break; |
| 850 | } // switch |
| 851 | } // if |
| 852 | |
| 853 | else if (regtype == REG_TYPE_ATTROUT) |
| 854 | { |
| 855 | regtype = REG_TYPE_OUTPUT; |
| 856 | usage = MOJOSHADER_USAGE_COLOR; |
| 857 | index = regnum; |
| 858 | } // else if |
| 859 | |
| 860 | else if (regtype == REG_TYPE_TEXCRDOUT) |
| 861 | { |
| 862 | regtype = REG_TYPE_OUTPUT; |
| 863 | usage = MOJOSHADER_USAGE_TEXCOORD; |
| 864 | index = regnum; |
| 865 | } // else if |
| 866 | } // if |
| 867 | |
| 868 | if (regtype == REG_TYPE_INPUT) |
| 869 | { |
| 870 | push_output(ctx, &ctx->inputs); |
| 871 | if (buffer_size(ctx->inputs) == 0) |
| 872 | { |
| 873 | output_line(ctx, "struct %s_Input", ctx->mainfn); |
| 874 | output_line(ctx, "{"); |
| 875 | } // if |
| 876 | |
| 877 | ctx->indent++; |
| 878 | output_line(ctx, "float4 %s [[attribute(%d)]];", var, regnum); |
| 879 | pop_output(ctx); |
| 880 | |
| 881 | push_output(ctx, &ctx->mainline_top); |
| 882 | ctx->indent++; |
| 883 | // !!! FIXME: might trigger unused var warnings in Clang. |
| 884 | //output_line(ctx, "constant float4 &%s = input.%s;", var, var); |
| 885 | output_line(ctx, "#define %s input.%s", var, var); |
| 886 | pop_output(ctx); |
| 887 | push_output(ctx, &ctx->mainline); |
| 888 | ctx->indent++; |
| 889 | output_line(ctx, "#undef %s", var); // !!! FIXME: gross. |
| 890 | pop_output(ctx); |
| 891 | } // if |
| 892 | |
| 893 | else if (regtype == REG_TYPE_OUTPUT) |
| 894 | { |
| 895 | push_output(ctx, &ctx->outputs); |
| 896 | if (buffer_size(ctx->outputs) == 0) |
| 897 | { |
| 898 | output_line(ctx, "struct %s_Output", ctx->mainfn); |
| 899 | output_line(ctx, "{"); |
| 900 | } // if |
| 901 | |
| 902 | ctx->indent++; |
| 903 | |
| 904 | switch (usage) |
| 905 | { |
| 906 | case MOJOSHADER_USAGE_POSITION: |
| 907 | output_line(ctx, "float4 %s [[position]];", var); |
| 908 | break; |
| 909 | case MOJOSHADER_USAGE_POINTSIZE: |
| 910 | output_line(ctx, "float %s [[point_size]];", var); |
| 911 | break; |
| 912 | case MOJOSHADER_USAGE_COLOR: |
| 913 | output_line(ctx, "float4 %s [[user(color%d)]];", var, index); |
| 914 | break; |
| 915 | case MOJOSHADER_USAGE_FOG: |
| 916 | output_line(ctx, "float4 %s [[user(fog)]];", var); |
| 917 | break; |
| 918 | case MOJOSHADER_USAGE_TEXCOORD: |
| 919 | output_line(ctx, "float4 %s [[user(texcoord%d)]];", var, index); |
| 920 | break; |
| 921 | case MOJOSHADER_USAGE_NORMAL: |
| 922 | output_line(ctx, "float4 %s [[user(normal)]];", var); |
| 923 | default: |
| 924 | // !!! FIXME: we need to deal with some more built-in varyings here. |
| 925 | break; |
| 926 | } // switch |
| 927 | |
| 928 | pop_output(ctx); |
| 929 | |
| 930 | push_output(ctx, &ctx->mainline_top); |
| 931 | ctx->indent++; |
| 932 | // !!! FIXME: this doesn't work. |
| 933 | //output_line(ctx, "float4 &%s = output.%s;", var, var); |
| 934 | output_line(ctx, "#define %s output.%s", var, var); |
| 935 | pop_output(ctx); |
| 936 | push_output(ctx, &ctx->mainline); |
| 937 | ctx->indent++; |
| 938 | output_line(ctx, "#undef %s", var); // !!! FIXME: gross. |
| 939 | pop_output(ctx); |
| 940 | } // else if |
| 941 | |
| 942 | else |
| 943 | { |
| 944 | fail(ctx, "unknown vertex shader attribute register"); |
| 945 | } // else |
| 946 | } // if |
| 947 | |
| 948 | else if (shader_is_pixel(ctx)) |
| 949 | { |
| 950 | // samplers DCLs get handled in emit_METAL_sampler(). |
| 951 | |
| 952 | if (flags & MOD_CENTROID) // !!! FIXME |
| 953 | { |
| 954 | failf(ctx, "centroid unsupported in %s profile", ctx->profile->name); |
| 955 | return; |
| 956 | } // if |
| 957 | |
| 958 | if ((regtype == REG_TYPE_COLOROUT) || (regtype == REG_TYPE_DEPTHOUT)) |
| 959 | { |
| 960 | push_output(ctx, &ctx->outputs); |
| 961 | if (buffer_size(ctx->outputs) == 0) |
| 962 | { |
| 963 | output_line(ctx, "struct %s_Output", ctx->mainfn); |
| 964 | output_line(ctx, "{"); |
| 965 | } // if |
| 966 | ctx->indent++; |
| 967 | |
| 968 | if (regtype == REG_TYPE_COLOROUT) |
| 969 | output_line(ctx, "float4 %s [[color(%d)]];", var, regnum); |
| 970 | else if (regtype == REG_TYPE_DEPTHOUT) |
| 971 | output_line(ctx, "float %s [[depth(any)]];", var); |
| 972 | |
| 973 | pop_output(ctx); |
| 974 | |
| 975 | push_output(ctx, &ctx->mainline_top); |
| 976 | ctx->indent++; |
| 977 | // !!! FIXME: this doesn't work. |
| 978 | //output_line(ctx, "float%s &%s = output.%s;", (regtype == REG_TYPE_DEPTHOUT) ? "" : "4", var, var); |
| 979 | output_line(ctx, "#define %s output.%s", var, var); |
| 980 | pop_output(ctx); |
| 981 | push_output(ctx, &ctx->mainline); |
| 982 | ctx->indent++; |
| 983 | output_line(ctx, "#undef %s", var); // !!! FIXME: gross. |
| 984 | pop_output(ctx); |
| 985 | } // if |
| 986 | |
| 987 | // !!! FIXME: can you actualy have a texture register with COLOR usage? |
| 988 | else if ((regtype == REG_TYPE_TEXTURE) || |
| 989 | (regtype == REG_TYPE_INPUT) || |
| 990 | (regtype == REG_TYPE_MISCTYPE)) |
| 991 | { |
| 992 | int skipreference = 0; |
| 993 | push_output(ctx, &ctx->inputs); |
| 994 | if (buffer_size(ctx->inputs) == 0) |
| 995 | { |
| 996 | output_line(ctx, "struct %s_Input", ctx->mainfn); |
| 997 | output_line(ctx, "{"); |
| 998 | } // if |
| 999 | ctx->indent++; |
| 1000 | |
| 1001 | if (regtype == REG_TYPE_MISCTYPE) |
| 1002 | { |
| 1003 | const MiscTypeType mt = (MiscTypeType) regnum; |
| 1004 | if (mt == MISCTYPE_TYPE_FACE) |
| 1005 | output_line(ctx, "bool %s [[front_facing]];", var); |
| 1006 | else if (mt == MISCTYPE_TYPE_POSITION) |
| 1007 | output_line(ctx, "float4 %s [[position]];", var); |
| 1008 | else |
| 1009 | fail(ctx, "BUG: unhandled misc register"); |
| 1010 | } // else if |
| 1011 | |
| 1012 | else |
| 1013 | { |
| 1014 | if (usage == MOJOSHADER_USAGE_TEXCOORD) |
| 1015 | { |
| 1016 | // ps_1_1 does a different hack for this attribute. |
| 1017 | // Refer to emit_METAL_global()'s REG_TYPE_ADDRESS code. |
| 1018 | if (!shader_version_atleast(ctx, 1, 4)) |
| 1019 | skipreference = 1; |
| 1020 | output_line(ctx, "float4 %s [[user(texcoord%d)]];", var, index); |
| 1021 | } // if |
| 1022 | |
| 1023 | else if (usage == MOJOSHADER_USAGE_COLOR) |
| 1024 | output_line(ctx, "float4 %s [[user(color%d)]];", var, index); |
| 1025 | |
| 1026 | else if (usage == MOJOSHADER_USAGE_FOG) |
| 1027 | output_line(ctx, "float4 %s [[user(fog)]];", var); |
| 1028 | |
| 1029 | else if (usage == MOJOSHADER_USAGE_NORMAL) |
| 1030 | output_line(ctx, "float4 %s [[user(normal)]];", var); |
| 1031 | } // else |
| 1032 | |
| 1033 | pop_output(ctx); |
| 1034 | |
| 1035 | // !!! FIXME: can cause unused var warnings in Clang... |
| 1036 | #if 0 |
| 1037 | push_output(ctx, &ctx->mainline_top); |
| 1038 | ctx->indent++; |
| 1039 | if ((regtype == REG_TYPE_MISCTYPE)&&(regnum == MISCTYPE_TYPE_FACE)) |
| 1040 | output_line(ctx, "constant bool &%s = input.%s;", var, var); |
| 1041 | else if (!skipreference) |
| 1042 | output_line(ctx, "constant float4 &%s = input.%s;", var, var); |
| 1043 | pop_output(ctx); |
| 1044 | #endif |
| 1045 | |
| 1046 | if (!skipreference) |
| 1047 | { |
| 1048 | push_output(ctx, &ctx->mainline_top); |
| 1049 | ctx->indent++; |
| 1050 | output_line(ctx, "#define %s input.%s", var, var); |
| 1051 | pop_output(ctx); |
| 1052 | push_output(ctx, &ctx->mainline); |
| 1053 | ctx->indent++; |
| 1054 | output_line(ctx, "#undef %s", var); // !!! FIXME: gross. |
| 1055 | pop_output(ctx); |
| 1056 | } // if |
| 1057 | } // else if |
| 1058 | |
| 1059 | else |
| 1060 | { |
| 1061 | fail(ctx, "unknown pixel shader attribute register"); |
| 1062 | } // else |
| 1063 | } // else if |
| 1064 | |
| 1065 | else |
| 1066 | { |
| 1067 | fail(ctx, "Unknown shader type"); // state machine should catch this. |
| 1068 | } // else |
| 1069 | } // emit_METAL_attribute |
| 1070 | |
| 1071 | void emit_METAL_NOP(Context *ctx) |
| 1072 | { |
| 1073 | // no-op is a no-op. :) |
| 1074 | } // emit_METAL_NOP |
| 1075 | |
| 1076 | void emit_METAL_MOV(Context *ctx) |
| 1077 | { |
| 1078 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1079 | char code[128]; |
| 1080 | make_METAL_destarg_assign(ctx, code, sizeof (code), "%s", src0); |
| 1081 | output_line(ctx, "%s", code); |
| 1082 | } // emit_METAL_MOV |
| 1083 | |
| 1084 | void emit_METAL_ADD(Context *ctx) |
| 1085 | { |
| 1086 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1087 | char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1)); |
| 1088 | char code[128]; |
| 1089 | make_METAL_destarg_assign(ctx, code, sizeof (code), "%s + %s", src0, src1); |
| 1090 | output_line(ctx, "%s", code); |
| 1091 | } // emit_METAL_ADD |
| 1092 | |
| 1093 | void emit_METAL_SUB(Context *ctx) |
| 1094 | { |
| 1095 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1096 | char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1)); |
| 1097 | char code[128]; |
| 1098 | make_METAL_destarg_assign(ctx, code, sizeof (code), "%s - %s", src0, src1); |
| 1099 | output_line(ctx, "%s", code); |
| 1100 | } // emit_METAL_SUB |
| 1101 | |
| 1102 | void emit_METAL_MAD(Context *ctx) |
| 1103 | { |
| 1104 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1105 | char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1)); |
| 1106 | char src2[64]; make_METAL_srcarg_string_masked(ctx, 2, src2, sizeof (src2)); |
| 1107 | char code[128]; |
| 1108 | make_METAL_destarg_assign(ctx, code, sizeof (code), "(%s * %s) + %s", src0, src1, src2); |
| 1109 | output_line(ctx, "%s", code); |
| 1110 | } // emit_METAL_MAD |
| 1111 | |
| 1112 | void emit_METAL_MUL(Context *ctx) |
| 1113 | { |
| 1114 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1115 | char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1)); |
| 1116 | char code[128]; |
| 1117 | make_METAL_destarg_assign(ctx, code, sizeof (code), "%s * %s", src0, src1); |
| 1118 | output_line(ctx, "%s", code); |
| 1119 | } // emit_METAL_MUL |
| 1120 | |
| 1121 | void emit_METAL_RCP(Context *ctx) |
| 1122 | { |
| 1123 | const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask); |
| 1124 | char cast[16] = { '\0' }; |
| 1125 | if (vecsize != 1) |
| 1126 | snprintf(cast, sizeof (cast), "float%d", vecsize); |
| 1127 | char src0[64]; make_METAL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0)); |
| 1128 | char code[128]; |
| 1129 | ctx->metal_need_header_math = 1; |
| 1130 | make_METAL_destarg_assign(ctx, code, sizeof (code), "%s((%s == 0.0) ? FLT_MAX : 1.0 / %s)", cast, src0, src0); |
| 1131 | output_line(ctx, "%s", code); |
| 1132 | } // emit_METAL_RCP |
| 1133 | |
| 1134 | void emit_METAL_RSQ(Context *ctx) |
| 1135 | { |
| 1136 | const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask); |
| 1137 | char cast[16] = { '\0' }; |
| 1138 | if (vecsize != 1) |
| 1139 | snprintf(cast, sizeof (cast), "float%d", vecsize); |
| 1140 | char src0[64]; make_METAL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0)); |
| 1141 | char code[128]; |
| 1142 | ctx->metal_need_header_math = 1; |
| 1143 | make_METAL_destarg_assign(ctx, code, sizeof (code), "%s((%s == 0.0) ? FLT_MAX : rsqrt(abs(%s)))", cast, src0, src0); |
| 1144 | output_line(ctx, "%s", code); |
| 1145 | } // emit_METAL_RSQ |
| 1146 | |
| 1147 | void emit_METAL_dotprod(Context *ctx, const char *src0, const char *src1, |
| 1148 | const char *extra) |
| 1149 | { |
| 1150 | const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask); |
| 1151 | char castleft[16] = { '\0' }; |
| 1152 | const char *castright = ""; |
| 1153 | if (vecsize != 1) |
| 1154 | { |
| 1155 | snprintf(castleft, sizeof (castleft), "float%d(", vecsize); |
| 1156 | castright = ")"; |
| 1157 | } // if |
| 1158 | |
| 1159 | char code[128]; |
| 1160 | ctx->metal_need_header_geometric = 1; |
| 1161 | make_METAL_destarg_assign(ctx, code, sizeof (code), "%sdot(%s, %s)%s%s", |
| 1162 | castleft, src0, src1, extra, castright); |
| 1163 | output_line(ctx, "%s", code); |
| 1164 | } // emit_METAL_dotprod |
| 1165 | |
| 1166 | void emit_METAL_DP3(Context *ctx) |
| 1167 | { |
| 1168 | char src0[64]; make_METAL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0)); |
| 1169 | char src1[64]; make_METAL_srcarg_string_vec3(ctx, 1, src1, sizeof (src1)); |
| 1170 | emit_METAL_dotprod(ctx, src0, src1, ""); |
| 1171 | } // emit_METAL_DP3 |
| 1172 | |
| 1173 | void emit_METAL_DP4(Context *ctx) |
| 1174 | { |
| 1175 | char src0[64]; make_METAL_srcarg_string_full(ctx, 0, src0, sizeof (src0)); |
| 1176 | char src1[64]; make_METAL_srcarg_string_full(ctx, 1, src1, sizeof (src1)); |
| 1177 | emit_METAL_dotprod(ctx, src0, src1, ""); |
| 1178 | } // emit_METAL_DP4 |
| 1179 | |
| 1180 | void emit_METAL_MIN(Context *ctx) |
| 1181 | { |
| 1182 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1183 | char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1)); |
| 1184 | char code[128]; |
| 1185 | ctx->metal_need_header_math = 1; |
| 1186 | make_METAL_destarg_assign(ctx, code, sizeof (code), "min(%s, %s)", src0, src1); |
| 1187 | output_line(ctx, "%s", code); |
| 1188 | } // emit_METAL_MIN |
| 1189 | |
| 1190 | void emit_METAL_MAX(Context *ctx) |
| 1191 | { |
| 1192 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1193 | char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1)); |
| 1194 | char code[128]; |
| 1195 | ctx->metal_need_header_math = 1; |
| 1196 | make_METAL_destarg_assign(ctx, code, sizeof (code), "max(%s, %s)", src0, src1); |
| 1197 | output_line(ctx, "%s", code); |
| 1198 | } // emit_METAL_MAX |
| 1199 | |
| 1200 | void emit_METAL_SLT(Context *ctx) |
| 1201 | { |
| 1202 | const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask); |
| 1203 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1204 | char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1)); |
| 1205 | char code[128]; |
| 1206 | |
| 1207 | // float(bool) or vec(bvec) results in 0.0 or 1.0, like SLT wants. |
| 1208 | if (vecsize == 1) |
| 1209 | make_METAL_destarg_assign(ctx, code, sizeof (code), "float(%s < %s)", src0, src1); |
| 1210 | else |
| 1211 | { |
| 1212 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1213 | "float%d(%s < %s)", vecsize, src0, src1); |
| 1214 | } // else |
| 1215 | output_line(ctx, "%s", code); |
| 1216 | } // emit_METAL_SLT |
| 1217 | |
| 1218 | void emit_METAL_SGE(Context *ctx) |
| 1219 | { |
| 1220 | const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask); |
| 1221 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1222 | char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1)); |
| 1223 | char code[128]; |
| 1224 | |
| 1225 | // float(bool) or vec(bvec) results in 0.0 or 1.0, like SGE wants. |
| 1226 | if (vecsize == 1) |
| 1227 | { |
| 1228 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1229 | "float(%s >= %s)", src0, src1); |
| 1230 | } // if |
| 1231 | else |
| 1232 | { |
| 1233 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1234 | "float%d(%s >= %s)", vecsize, src0, src1); |
| 1235 | } // else |
| 1236 | output_line(ctx, "%s", code); |
| 1237 | } // emit_METAL_SGE |
| 1238 | |
| 1239 | void emit_METAL_EXP(Context *ctx) |
| 1240 | { |
| 1241 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1242 | char code[128]; |
| 1243 | ctx->metal_need_header_math = 1; |
| 1244 | make_METAL_destarg_assign(ctx, code, sizeof (code), "exp2(%s)", src0); |
| 1245 | output_line(ctx, "%s", code); |
| 1246 | } // emit_METAL_EXP |
| 1247 | |
| 1248 | void emit_METAL_LOG(Context *ctx) |
| 1249 | { |
| 1250 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1251 | char code[128]; |
| 1252 | ctx->metal_need_header_math = 1; |
| 1253 | make_METAL_destarg_assign(ctx, code, sizeof (code), "log2(%s)", src0); |
| 1254 | output_line(ctx, "%s", code); |
| 1255 | } // emit_METAL_LOG |
| 1256 | |
| 1257 | void emit_METAL_LIT_helper(Context *ctx) |
| 1258 | { |
| 1259 | const char *maxp = "127.9961"; // value from the dx9 reference. |
| 1260 | |
| 1261 | if (ctx->glsl_generated_lit_helper) |
| 1262 | return; |
| 1263 | |
| 1264 | ctx->glsl_generated_lit_helper = 1; |
| 1265 | ctx->metal_need_header_common = 1; |
| 1266 | ctx->metal_need_header_math = 1; |
| 1267 | |
| 1268 | push_output(ctx, &ctx->helpers); |
| 1269 | output_line(ctx, "float4 LIT(const float4 src)"); |
| 1270 | output_line(ctx, "{"); ctx->indent++; |
| 1271 | output_line(ctx, "const float power = clamp(src.w, -%s, %s);",maxp,maxp); |
| 1272 | output_line(ctx, "float4 retval = float4(1.0, 0.0, 0.0, 1.0);"); |
| 1273 | output_line(ctx, "if (src.x > 0.0) {"); ctx->indent++; |
| 1274 | output_line(ctx, "retval.y = src.x;"); |
| 1275 | output_line(ctx, "if (src.y > 0.0) {"); ctx->indent++; |
| 1276 | output_line(ctx, "retval.z = pow(src.y, power);"); ctx->indent--; |
| 1277 | output_line(ctx, "}"); ctx->indent--; |
| 1278 | output_line(ctx, "}"); |
| 1279 | output_line(ctx, "return retval;"); ctx->indent--; |
| 1280 | output_line(ctx, "}"); |
| 1281 | output_blank_line(ctx); |
| 1282 | pop_output(ctx); |
| 1283 | } // emit_METAL_LIT_helper |
| 1284 | |
| 1285 | void emit_METAL_LIT(Context *ctx) |
| 1286 | { |
| 1287 | char src0[64]; make_METAL_srcarg_string_full(ctx, 0, src0, sizeof (src0)); |
| 1288 | char code[128]; |
| 1289 | emit_METAL_LIT_helper(ctx); |
| 1290 | make_METAL_destarg_assign(ctx, code, sizeof (code), "LIT(%s)", src0); |
| 1291 | output_line(ctx, "%s", code); |
| 1292 | } // emit_METAL_LIT |
| 1293 | |
| 1294 | void emit_METAL_DST(Context *ctx) |
| 1295 | { |
| 1296 | // !!! FIXME: needs to take ctx->dst_arg.writemask into account. |
| 1297 | char src0_y[64]; make_METAL_srcarg_string_y(ctx, 0, src0_y, sizeof (src0_y)); |
| 1298 | char src1_y[64]; make_METAL_srcarg_string_y(ctx, 1, src1_y, sizeof (src1_y)); |
| 1299 | char src0_z[64]; make_METAL_srcarg_string_z(ctx, 0, src0_z, sizeof (src0_z)); |
| 1300 | char src1_w[64]; make_METAL_srcarg_string_w(ctx, 1, src1_w, sizeof (src1_w)); |
| 1301 | |
| 1302 | char code[128]; |
| 1303 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1304 | "float4(1.0, %s * %s, %s, %s)", |
| 1305 | src0_y, src1_y, src0_z, src1_w); |
| 1306 | output_line(ctx, "%s", code); |
| 1307 | } // emit_METAL_DST |
| 1308 | |
| 1309 | void emit_METAL_LRP(Context *ctx) |
| 1310 | { |
| 1311 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1312 | char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1)); |
| 1313 | char src2[64]; make_METAL_srcarg_string_masked(ctx, 2, src2, sizeof (src2)); |
| 1314 | char code[128]; |
| 1315 | ctx->metal_need_header_common = 1; |
| 1316 | make_METAL_destarg_assign(ctx, code, sizeof (code), "mix(%s, %s, %s)", |
| 1317 | src2, src1, src0); |
| 1318 | output_line(ctx, "%s", code); |
| 1319 | } // emit_METAL_LRP |
| 1320 | |
| 1321 | void emit_METAL_FRC(Context *ctx) |
| 1322 | { |
| 1323 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1324 | char code[128]; |
| 1325 | ctx->metal_need_header_math = 1; |
| 1326 | make_METAL_destarg_assign(ctx, code, sizeof (code), "fract(%s)", src0); |
| 1327 | output_line(ctx, "%s", code); |
| 1328 | } // emit_METAL_FRC |
| 1329 | |
| 1330 | void emit_METAL_M4X4(Context *ctx) |
| 1331 | { |
| 1332 | char src0[64]; make_METAL_srcarg_string_full(ctx, 0, src0, sizeof (src0)); |
| 1333 | char row0[64]; make_METAL_srcarg_string_full(ctx, 1, row0, sizeof (row0)); |
| 1334 | char row1[64]; make_METAL_srcarg_string_full(ctx, 2, row1, sizeof (row1)); |
| 1335 | char row2[64]; make_METAL_srcarg_string_full(ctx, 3, row2, sizeof (row2)); |
| 1336 | char row3[64]; make_METAL_srcarg_string_full(ctx, 4, row3, sizeof (row3)); |
| 1337 | char code[256]; |
| 1338 | ctx->metal_need_header_geometric = 1; |
| 1339 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1340 | "float4(dot(%s, %s), dot(%s, %s), dot(%s, %s), dot(%s, %s))", |
| 1341 | src0, row0, src0, row1, src0, row2, src0, row3); |
| 1342 | output_line(ctx, "%s", code); |
| 1343 | } // emit_METAL_M4X4 |
| 1344 | |
| 1345 | void emit_METAL_M4X3(Context *ctx) |
| 1346 | { |
| 1347 | char src0[64]; make_METAL_srcarg_string_full(ctx, 0, src0, sizeof (src0)); |
| 1348 | char row0[64]; make_METAL_srcarg_string_full(ctx, 1, row0, sizeof (row0)); |
| 1349 | char row1[64]; make_METAL_srcarg_string_full(ctx, 2, row1, sizeof (row1)); |
| 1350 | char row2[64]; make_METAL_srcarg_string_full(ctx, 3, row2, sizeof (row2)); |
| 1351 | char code[256]; |
| 1352 | ctx->metal_need_header_geometric = 1; |
| 1353 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1354 | "float3(dot(%s, %s), dot(%s, %s), dot(%s, %s))", |
| 1355 | src0, row0, src0, row1, src0, row2); |
| 1356 | output_line(ctx, "%s", code); |
| 1357 | } // emit_METAL_M4X3 |
| 1358 | |
| 1359 | void emit_METAL_M3X4(Context *ctx) |
| 1360 | { |
| 1361 | char src0[64]; make_METAL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0)); |
| 1362 | char row0[64]; make_METAL_srcarg_string_vec3(ctx, 1, row0, sizeof (row0)); |
| 1363 | char row1[64]; make_METAL_srcarg_string_vec3(ctx, 2, row1, sizeof (row1)); |
| 1364 | char row2[64]; make_METAL_srcarg_string_vec3(ctx, 3, row2, sizeof (row2)); |
| 1365 | char row3[64]; make_METAL_srcarg_string_vec3(ctx, 4, row3, sizeof (row3)); |
| 1366 | char code[256]; |
| 1367 | ctx->metal_need_header_geometric = 1; |
| 1368 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1369 | "float4(dot(%s, %s), dot(%s, %s), " |
| 1370 | "dot(%s, %s), dot(%s, %s))", |
| 1371 | src0, row0, src0, row1, |
| 1372 | src0, row2, src0, row3); |
| 1373 | output_line(ctx, "%s", code); |
| 1374 | } // emit_METAL_M3X4 |
| 1375 | |
| 1376 | void emit_METAL_M3X3(Context *ctx) |
| 1377 | { |
| 1378 | char src0[64]; make_METAL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0)); |
| 1379 | char row0[64]; make_METAL_srcarg_string_vec3(ctx, 1, row0, sizeof (row0)); |
| 1380 | char row1[64]; make_METAL_srcarg_string_vec3(ctx, 2, row1, sizeof (row1)); |
| 1381 | char row2[64]; make_METAL_srcarg_string_vec3(ctx, 3, row2, sizeof (row2)); |
| 1382 | char code[256]; |
| 1383 | ctx->metal_need_header_geometric = 1; |
| 1384 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1385 | "float3(dot(%s, %s), dot(%s, %s), dot(%s, %s))", |
| 1386 | src0, row0, src0, row1, src0, row2); |
| 1387 | output_line(ctx, "%s", code); |
| 1388 | } // emit_METAL_M3X3 |
| 1389 | |
| 1390 | void emit_METAL_M3X2(Context *ctx) |
| 1391 | { |
| 1392 | char src0[64]; make_METAL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0)); |
| 1393 | char row0[64]; make_METAL_srcarg_string_vec3(ctx, 1, row0, sizeof (row0)); |
| 1394 | char row1[64]; make_METAL_srcarg_string_vec3(ctx, 2, row1, sizeof (row1)); |
| 1395 | char code[256]; |
| 1396 | ctx->metal_need_header_geometric = 1; |
| 1397 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1398 | "float2(dot(%s, %s), dot(%s, %s))", |
| 1399 | src0, row0, src0, row1); |
| 1400 | output_line(ctx, "%s", code); |
| 1401 | } // emit_METAL_M3X2 |
| 1402 | |
| 1403 | void emit_METAL_CALL(Context *ctx) |
| 1404 | { |
| 1405 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1406 | if (ctx->loops > 0) |
| 1407 | output_line(ctx, "%s(aL);", src0); |
| 1408 | else |
| 1409 | output_line(ctx, "%s();", src0); |
| 1410 | } // emit_METAL_CALL |
| 1411 | |
| 1412 | void emit_METAL_CALLNZ(Context *ctx) |
| 1413 | { |
| 1414 | // !!! FIXME: if src1 is a constbool that's true, we can remove the |
| 1415 | // !!! FIXME: if. If it's false, we can make this a no-op. |
| 1416 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1417 | char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1)); |
| 1418 | |
| 1419 | if (ctx->loops > 0) |
| 1420 | output_line(ctx, "if (%s) { %s(aL); }", src1, src0); |
| 1421 | else |
| 1422 | output_line(ctx, "if (%s) { %s(); }", src1, src0); |
| 1423 | } // emit_METAL_CALLNZ |
| 1424 | |
| 1425 | void emit_METAL_LOOP(Context *ctx) |
| 1426 | { |
| 1427 | // !!! FIXME: swizzle? |
| 1428 | char var[64]; get_METAL_srcarg_varname(ctx, 1, var, sizeof (var)); |
| 1429 | assert(ctx->source_args[0].regnum == 0); // in case they add aL1 someday. |
| 1430 | output_line(ctx, "{"); |
| 1431 | ctx->indent++; |
| 1432 | output_line(ctx, "const int aLend = %s.x + %s.y;", var, var); |
| 1433 | output_line(ctx, "for (int aL = %s.y; aL < aLend; aL += %s.z) {", var, var); |
| 1434 | ctx->indent++; |
| 1435 | } // emit_METAL_LOOP |
| 1436 | |
| 1437 | void emit_METAL_RET(Context *ctx) |
| 1438 | { |
| 1439 | // thankfully, the MSDN specs say a RET _has_ to end a function...no |
| 1440 | // early returns. So if you hit one, you know you can safely close |
| 1441 | // a high-level function. |
| 1442 | push_output(ctx, &ctx->postflight); |
| 1443 | output_line(ctx, "}"); |
| 1444 | output_blank_line(ctx); |
| 1445 | set_output(ctx, &ctx->subroutines); // !!! FIXME: is this for LABEL? Maybe set it there so we don't allocate unnecessarily. |
| 1446 | } // emit_METAL_RET |
| 1447 | |
| 1448 | void emit_METAL_ENDLOOP(Context *ctx) |
| 1449 | { |
| 1450 | ctx->indent--; |
| 1451 | output_line(ctx, "}"); |
| 1452 | ctx->indent--; |
| 1453 | output_line(ctx, "}"); |
| 1454 | } // emit_METAL_ENDLOOP |
| 1455 | |
| 1456 | void emit_METAL_LABEL(Context *ctx) |
| 1457 | { |
| 1458 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1459 | const int label = ctx->source_args[0].regnum; |
| 1460 | RegisterList *reg = reglist_find(&ctx->used_registers, REG_TYPE_LABEL, label); |
| 1461 | assert(ctx->output == ctx->subroutines); // not mainline, etc. |
| 1462 | assert(ctx->indent == 0); // we shouldn't be in the middle of a function. |
| 1463 | |
| 1464 | // MSDN specs say CALL* has to come before the LABEL, so we know if we |
| 1465 | // can ditch the entire function here as unused. |
| 1466 | if (reg == NULL) |
| 1467 | set_output(ctx, &ctx->ignore); // Func not used. Parse, but don't output. |
| 1468 | |
| 1469 | // !!! FIXME: it would be nice if we could determine if a function is |
| 1470 | // !!! FIXME: only called once and, if so, forcibly inline it. |
| 1471 | |
| 1472 | // !!! FIXME: this worked in GLSL because all our state is global to the shader, |
| 1473 | // !!! FIXME: but in metal we kept it local to the shader mainline. |
| 1474 | // !!! FIXME: Can we do C++11 lambdas in Metal to have nested functions? :) |
| 1475 | |
| 1476 | const char *uses_loopreg = ((reg) && (reg->misc == 1)) ? "int aL" : ""; |
| 1477 | output_line(ctx, "void %s(%s)", src0, uses_loopreg); |
| 1478 | output_line(ctx, "{"); |
| 1479 | ctx->indent++; |
| 1480 | } // emit_METAL_LABEL |
| 1481 | |
| 1482 | void emit_METAL_DCL(Context *ctx) |
| 1483 | { |
| 1484 | // no-op. We do this in our emit_attribute() and emit_uniform(). |
| 1485 | } // emit_METAL_DCL |
| 1486 | |
| 1487 | void emit_METAL_POW(Context *ctx) |
| 1488 | { |
| 1489 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1490 | char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1)); |
| 1491 | char code[128]; |
| 1492 | ctx->metal_need_header_math = 1; |
| 1493 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1494 | "pow(abs(%s), %s)", src0, src1); |
| 1495 | output_line(ctx, "%s", code); |
| 1496 | } // emit_METAL_POW |
| 1497 | |
| 1498 | void emit_METAL_CRS(Context *ctx) |
| 1499 | { |
| 1500 | // !!! FIXME: needs to take ctx->dst_arg.writemask into account. |
| 1501 | char src0[64]; make_METAL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0)); |
| 1502 | char src1[64]; make_METAL_srcarg_string_vec3(ctx, 1, src1, sizeof (src1)); |
| 1503 | char code[128]; |
| 1504 | ctx->metal_need_header_geometric = 1; |
| 1505 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1506 | "cross(%s, %s)", src0, src1); |
| 1507 | output_line(ctx, "%s", code); |
| 1508 | } // emit_METAL_CRS |
| 1509 | |
| 1510 | void emit_METAL_SGN(Context *ctx) |
| 1511 | { |
| 1512 | // (we don't need the temporary registers specified for the D3D opcode.) |
| 1513 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1514 | char code[128]; |
| 1515 | ctx->metal_need_header_common = 1; |
| 1516 | make_METAL_destarg_assign(ctx, code, sizeof (code), "sign(%s)", src0); |
| 1517 | output_line(ctx, "%s", code); |
| 1518 | } // emit_METAL_SGN |
| 1519 | |
| 1520 | void emit_METAL_ABS(Context *ctx) |
| 1521 | { |
| 1522 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1523 | char code[128]; |
| 1524 | ctx->metal_need_header_math = 1; |
| 1525 | make_METAL_destarg_assign(ctx, code, sizeof (code), "abs(%s)", src0); |
| 1526 | output_line(ctx, "%s", code); |
| 1527 | } // emit_METAL_ABS |
| 1528 | |
| 1529 | void emit_METAL_NRM(Context *ctx) |
| 1530 | { |
| 1531 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1532 | char code[128]; |
| 1533 | ctx->metal_need_header_geometric = 1; |
| 1534 | make_METAL_destarg_assign(ctx, code, sizeof (code), "normalize(%s)", src0); |
| 1535 | output_line(ctx, "%s", code); |
| 1536 | } // emit_METAL_NRM |
| 1537 | |
| 1538 | void emit_METAL_SINCOS(Context *ctx) |
| 1539 | { |
| 1540 | // we don't care about the temp registers that <= sm2 demands; ignore them. |
| 1541 | // sm2 also talks about what components are left untouched vs. undefined, |
| 1542 | // but we just leave those all untouched with Metal write masks (which |
| 1543 | // would fulfill the "undefined" requirement, too). |
| 1544 | const int mask = ctx->dest_arg.writemask; |
| 1545 | char src0[64]; make_METAL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0)); |
| 1546 | char code[128] = { '\0' }; |
| 1547 | |
| 1548 | ctx->metal_need_header_math = 1; |
| 1549 | if (writemask_x(mask)) |
| 1550 | make_METAL_destarg_assign(ctx, code, sizeof (code), "cos(%s)", src0); |
| 1551 | else if (writemask_y(mask)) |
| 1552 | make_METAL_destarg_assign(ctx, code, sizeof (code), "sin(%s)", src0); |
| 1553 | else if (writemask_xy(mask)) |
| 1554 | { |
| 1555 | // !!! FIXME: can use sincos(), but need to assign cos to a temp, since it needs a reference. |
| 1556 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1557 | "float2(cos(%s), sin(%s))", src0, src0); |
| 1558 | } // else if |
| 1559 | |
| 1560 | output_line(ctx, "%s", code); |
| 1561 | } // emit_METAL_SINCOS |
| 1562 | |
| 1563 | void emit_METAL_REP(Context *ctx) |
| 1564 | { |
| 1565 | // !!! FIXME: |
| 1566 | // msdn docs say legal loop values are 0 to 255. We can check DEFI values |
| 1567 | // at parse time, but if they are pulling a value from a uniform, do |
| 1568 | // we clamp here? |
| 1569 | // !!! FIXME: swizzle is legal here, right? |
| 1570 | char src0[64]; make_METAL_srcarg_string_x(ctx, 0, src0, sizeof (src0)); |
| 1571 | const uint rep = (uint) ctx->reps; |
| 1572 | output_line(ctx, "for (int rep%u = 0; rep%u < %s; rep%u++) {", |
| 1573 | rep, rep, src0, rep); |
| 1574 | ctx->indent++; |
| 1575 | } // emit_METAL_REP |
| 1576 | |
| 1577 | void emit_METAL_ENDREP(Context *ctx) |
| 1578 | { |
| 1579 | ctx->indent--; |
| 1580 | output_line(ctx, "}"); |
| 1581 | } // emit_METAL_ENDREP |
| 1582 | |
| 1583 | void emit_METAL_IF(Context *ctx) |
| 1584 | { |
| 1585 | char src0[64]; make_METAL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0)); |
| 1586 | output_line(ctx, "if (%s) {", src0); |
| 1587 | ctx->indent++; |
| 1588 | } // emit_METAL_IF |
| 1589 | |
| 1590 | void emit_METAL_IFC(Context *ctx) |
| 1591 | { |
| 1592 | const char *comp = get_METAL_comparison_string_scalar(ctx); |
| 1593 | char src0[64]; make_METAL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0)); |
| 1594 | char src1[64]; make_METAL_srcarg_string_scalar(ctx, 1, src1, sizeof (src1)); |
| 1595 | output_line(ctx, "if (%s %s %s) {", src0, comp, src1); |
| 1596 | ctx->indent++; |
| 1597 | } // emit_METAL_IFC |
| 1598 | |
| 1599 | void emit_METAL_ELSE(Context *ctx) |
| 1600 | { |
| 1601 | ctx->indent--; |
| 1602 | output_line(ctx, "} else {"); |
| 1603 | ctx->indent++; |
| 1604 | } // emit_METAL_ELSE |
| 1605 | |
| 1606 | void emit_METAL_ENDIF(Context *ctx) |
| 1607 | { |
| 1608 | ctx->indent--; |
| 1609 | output_line(ctx, "}"); |
| 1610 | } // emit_METAL_ENDIF |
| 1611 | |
| 1612 | void emit_METAL_BREAK(Context *ctx) |
| 1613 | { |
| 1614 | output_line(ctx, "break;"); |
| 1615 | } // emit_METAL_BREAK |
| 1616 | |
| 1617 | void emit_METAL_BREAKC(Context *ctx) |
| 1618 | { |
| 1619 | const char *comp = get_METAL_comparison_string_scalar(ctx); |
| 1620 | char src0[64]; make_METAL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0)); |
| 1621 | char src1[64]; make_METAL_srcarg_string_scalar(ctx, 1, src1, sizeof (src1)); |
| 1622 | output_line(ctx, "if (%s %s %s) { break; }", src0, comp, src1); |
| 1623 | } // emit_METAL_BREAKC |
| 1624 | |
| 1625 | void emit_METAL_MOVA(Context *ctx) |
| 1626 | { |
| 1627 | const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask); |
| 1628 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 1629 | char code[128]; |
| 1630 | |
| 1631 | ctx->metal_need_header_math = 1; |
| 1632 | ctx->metal_need_header_common = 1; |
| 1633 | |
| 1634 | if (vecsize == 1) |
| 1635 | { |
| 1636 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1637 | "int(floor(abs(%s) + 0.5) * sign(%s))", |
| 1638 | src0, src0); |
| 1639 | } // if |
| 1640 | |
| 1641 | else |
| 1642 | { |
| 1643 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1644 | "int%d(floor(abs(%s) + float%d(0.5)) * sign(%s))", |
| 1645 | vecsize, src0, vecsize, src0); |
| 1646 | } // else |
| 1647 | |
| 1648 | output_line(ctx, "%s", code); |
| 1649 | } // emit_METAL_MOVA |
| 1650 | |
| 1651 | void emit_METAL_DEFB(Context *ctx) |
| 1652 | { |
| 1653 | char varname[64]; get_METAL_destarg_varname(ctx, varname, sizeof (varname)); |
| 1654 | push_output(ctx, &ctx->mainline_top); |
| 1655 | ctx->indent++; |
| 1656 | output_line(ctx, "const bool %s = %s;", |
| 1657 | varname, ctx->dwords[0] ? "true" : "false"); |
| 1658 | pop_output(ctx); |
| 1659 | } // emit_METAL_DEFB |
| 1660 | |
| 1661 | void emit_METAL_DEFI(Context *ctx) |
| 1662 | { |
| 1663 | char varname[64]; get_METAL_destarg_varname(ctx, varname, sizeof (varname)); |
| 1664 | const int32 *x = (const int32 *) ctx->dwords; |
| 1665 | push_output(ctx, &ctx->mainline_top); |
| 1666 | ctx->indent++; |
| 1667 | output_line(ctx, "const int4 %s = int4(%d, %d, %d, %d);", |
| 1668 | varname, (int) x[0], (int) x[1], (int) x[2], (int) x[3]); |
| 1669 | pop_output(ctx); |
| 1670 | } // emit_METAL_DEFI |
| 1671 | |
| 1672 | EMIT_METAL_OPCODE_UNIMPLEMENTED_FUNC(TEXCRD) |
| 1673 | |
| 1674 | void emit_METAL_TEXKILL(Context *ctx) |
| 1675 | { |
| 1676 | char dst[64]; get_METAL_destarg_varname(ctx, dst, sizeof (dst)); |
| 1677 | ctx->metal_need_header_relational = 1; |
| 1678 | ctx->metal_need_header_graphics = 1; |
| 1679 | output_line(ctx, "if (any(%s.xyz < float3(0.0))) discard_fragment();", dst); |
| 1680 | } // emit_METAL_TEXKILL |
| 1681 | |
| 1682 | static void metal_texld(Context *ctx, const int texldd) |
| 1683 | { |
| 1684 | ctx->metal_need_header_texture = 1; |
| 1685 | if (!shader_version_atleast(ctx, 1, 4)) |
| 1686 | { |
| 1687 | DestArgInfo *info = &ctx->dest_arg; |
| 1688 | char dst[64]; |
| 1689 | char sampler[64]; |
| 1690 | char code[128] = {0}; |
| 1691 | |
| 1692 | assert(!texldd); |
| 1693 | |
| 1694 | RegisterList *sreg; |
| 1695 | sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, info->regnum); |
| 1696 | const TextureType ttype = (TextureType) (sreg ? sreg->index : 0); |
| 1697 | |
| 1698 | char swizzle[4] = { 'x', 'y', 'z', '\0' }; |
| 1699 | if (ttype == TEXTURE_TYPE_2D) |
| 1700 | swizzle[2] = '\0'; // "xy" instead of "xyz". |
| 1701 | |
| 1702 | // !!! FIXME: this code counts on the register not having swizzles, etc. |
| 1703 | get_METAL_destarg_varname(ctx, dst, sizeof (dst)); |
| 1704 | get_METAL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum, |
| 1705 | sampler, sizeof (sampler)); |
| 1706 | |
| 1707 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1708 | "%s_texture.sample(%s, %s.%s)", |
| 1709 | sampler, sampler, dst, swizzle); |
| 1710 | output_line(ctx, "%s", code); |
| 1711 | } // if |
| 1712 | |
| 1713 | else if (!shader_version_atleast(ctx, 2, 0)) |
| 1714 | { |
| 1715 | // ps_1_4 is different, too! |
| 1716 | fail(ctx, "TEXLD == Shader Model 1.4 unimplemented."); // !!! FIXME |
| 1717 | return; |
| 1718 | } // else if |
| 1719 | |
| 1720 | else |
| 1721 | { |
| 1722 | const SourceArgInfo *samp_arg = &ctx->source_args[1]; |
| 1723 | RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, |
| 1724 | samp_arg->regnum); |
| 1725 | const char *funcname = NULL; |
| 1726 | char src0[64] = { '\0' }; |
| 1727 | char src1[64]; get_METAL_srcarg_varname(ctx, 1, src1, sizeof (src1)); // !!! FIXME: SRC_MOD? |
| 1728 | char src2[64] = { '\0' }; |
| 1729 | char src3[64] = { '\0' }; |
| 1730 | |
| 1731 | if (sreg == NULL) |
| 1732 | { |
| 1733 | fail(ctx, "TEXLD using undeclared sampler"); |
| 1734 | return; |
| 1735 | } // if |
| 1736 | |
| 1737 | const char *grad = ""; |
| 1738 | if (texldd) |
| 1739 | { |
| 1740 | switch ((const TextureType) sreg->index) |
| 1741 | { |
| 1742 | case TEXTURE_TYPE_2D: |
| 1743 | grad = "2d"; |
| 1744 | make_METAL_srcarg_string_vec2(ctx, 2, src2, sizeof (src2)); |
| 1745 | make_METAL_srcarg_string_vec2(ctx, 3, src3, sizeof (src3)); |
| 1746 | break; |
| 1747 | case TEXTURE_TYPE_VOLUME: |
| 1748 | grad = "3d"; |
| 1749 | make_METAL_srcarg_string_vec3(ctx, 2, src2, sizeof (src2)); |
| 1750 | make_METAL_srcarg_string_vec3(ctx, 3, src3, sizeof (src3)); |
| 1751 | break; |
| 1752 | case TEXTURE_TYPE_CUBE: |
| 1753 | grad = "cube"; |
| 1754 | make_METAL_srcarg_string_vec3(ctx, 2, src2, sizeof (src2)); |
| 1755 | make_METAL_srcarg_string_vec3(ctx, 3, src3, sizeof (src3)); |
| 1756 | break; |
| 1757 | } // switch |
| 1758 | } // if |
| 1759 | |
| 1760 | // !!! FIXME: can TEXLDD set instruction_controls? |
| 1761 | // !!! FIXME: does the d3d bias value map directly to Metal? |
| 1762 | const char *biasleft = ""; |
| 1763 | const char *biasright = ""; |
| 1764 | char bias[64] = { '\0' }; |
| 1765 | if (ctx->instruction_controls == CONTROL_TEXLDB) |
| 1766 | { |
| 1767 | biasleft = ", bias("; |
| 1768 | make_METAL_srcarg_string_w(ctx, 0, bias, sizeof (bias)); |
| 1769 | biasright = ")"; |
| 1770 | } // if |
| 1771 | |
| 1772 | // Metal doesn't have a texture2DProj() function, but you just divide |
| 1773 | // your texcoords by texcoords.w to achieve it anyhow, so DIY. |
| 1774 | const char *projop = ""; |
| 1775 | char proj[64] = { '\0' }; |
| 1776 | if (ctx->instruction_controls == CONTROL_TEXLDP) |
| 1777 | { |
| 1778 | if (sreg->index == TEXTURE_TYPE_CUBE) |
| 1779 | fail(ctx, "TEXLDP on a cubemap"); // !!! FIXME: is this legal? |
| 1780 | projop = " / "; |
| 1781 | make_METAL_srcarg_string_w(ctx, 0, proj, sizeof (proj)); |
| 1782 | } // if |
| 1783 | |
| 1784 | switch ((const TextureType) sreg->index) |
| 1785 | { |
| 1786 | case TEXTURE_TYPE_2D: |
| 1787 | make_METAL_srcarg_string_vec2(ctx, 0, src0, sizeof (src0)); |
| 1788 | break; |
| 1789 | |
| 1790 | case TEXTURE_TYPE_CUBE: |
| 1791 | case TEXTURE_TYPE_VOLUME: |
| 1792 | make_METAL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0)); |
| 1793 | break; |
| 1794 | |
| 1795 | default: |
| 1796 | fail(ctx, "unknown texture type"); |
| 1797 | return; |
| 1798 | } // switch |
| 1799 | |
| 1800 | assert(!isscalar(ctx, ctx->shader_type, samp_arg->regtype, samp_arg->regnum)); |
| 1801 | char swiz_str[6] = { '\0' }; |
| 1802 | make_METAL_swizzle_string(swiz_str, sizeof (swiz_str), |
| 1803 | samp_arg->swizzle, ctx->dest_arg.writemask); |
| 1804 | |
| 1805 | char code[128]; |
| 1806 | if (texldd) |
| 1807 | { |
| 1808 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1809 | "%s_texture.sample(%s, %s, gradient%s(%s, %s))%s", |
| 1810 | src1, src1, src0, grad, src2, src3, swiz_str); |
| 1811 | } // if |
| 1812 | else |
| 1813 | { |
| 1814 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1815 | "%s_texture.sample(%s, %s%s%s%s%s%s)%s", |
| 1816 | src1, src1, src0, projop, proj, |
| 1817 | biasleft, bias, biasright, swiz_str); |
| 1818 | } // else |
| 1819 | |
| 1820 | output_line(ctx, "%s", code); |
| 1821 | } // else |
| 1822 | } // metal_texld |
| 1823 | |
| 1824 | void emit_METAL_TEXLD(Context *ctx) |
| 1825 | { |
| 1826 | metal_texld(ctx, 0); |
| 1827 | } // emit_METAL_TEXLD |
| 1828 | |
| 1829 | |
| 1830 | void emit_METAL_TEXBEM(Context *ctx) |
| 1831 | { |
| 1832 | DestArgInfo *info = &ctx->dest_arg; |
| 1833 | char dst[64]; get_METAL_destarg_varname(ctx, dst, sizeof (dst)); |
| 1834 | char src[64]; get_METAL_srcarg_varname(ctx, 0, src, sizeof (src)); |
| 1835 | char sampler[64]; |
| 1836 | char code[512]; |
| 1837 | |
| 1838 | ctx->metal_need_header_texture = 1; |
| 1839 | |
| 1840 | // !!! FIXME: this code counts on the register not having swizzles, etc. |
| 1841 | get_METAL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum, |
| 1842 | sampler, sizeof (sampler)); |
| 1843 | |
| 1844 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1845 | "%s_texture.sample(%s, float2(%s.x + (%s_texbem.x * %s.x) + (%s_texbem.z * %s.y)," |
| 1846 | " %s.y + (%s_texbem.y * %s.x) + (%s_texbem.w * %s.y)))", |
| 1847 | sampler, sampler, |
| 1848 | dst, sampler, src, sampler, src, |
| 1849 | dst, sampler, src, sampler, src); |
| 1850 | |
| 1851 | output_line(ctx, "%s", code); |
| 1852 | } // emit_METAL_TEXBEM |
| 1853 | |
| 1854 | |
| 1855 | void emit_METAL_TEXBEML(Context *ctx) |
| 1856 | { |
| 1857 | // !!! FIXME: this code counts on the register not having swizzles, etc. |
| 1858 | DestArgInfo *info = &ctx->dest_arg; |
| 1859 | char dst[64]; get_METAL_destarg_varname(ctx, dst, sizeof (dst)); |
| 1860 | char src[64]; get_METAL_srcarg_varname(ctx, 0, src, sizeof (src)); |
| 1861 | char sampler[64]; |
| 1862 | char code[512]; |
| 1863 | |
| 1864 | ctx->metal_need_header_texture = 1; |
| 1865 | |
| 1866 | get_METAL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum, |
| 1867 | sampler, sizeof (sampler)); |
| 1868 | |
| 1869 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1870 | "(%s_texture.sample(%s, float2(%s.x + (%s_texbem.x * %s.x) + (%s_texbem.z * %s.y)," |
| 1871 | " %s.y + (%s_texbem.y * %s.x) + (%s_texbem.w * %s.y)))) *" |
| 1872 | " ((%s.z * %s_texbeml.x) + %s_texbem.y)", |
| 1873 | sampler, sampler, |
| 1874 | dst, sampler, src, sampler, src, |
| 1875 | dst, sampler, src, sampler, src, |
| 1876 | src, sampler, sampler); |
| 1877 | |
| 1878 | output_line(ctx, "%s", code); |
| 1879 | } // emit_METAL_TEXBEML |
| 1880 | |
| 1881 | EMIT_METAL_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2AR) // !!! FIXME |
| 1882 | EMIT_METAL_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2GB) // !!! FIXME |
| 1883 | |
| 1884 | |
| 1885 | void emit_METAL_TEXM3X2PAD(Context *ctx) |
| 1886 | { |
| 1887 | // no-op ... work happens in emit_METAL_TEXM3X2TEX(). |
| 1888 | } // emit_METAL_TEXM3X2PAD |
| 1889 | |
| 1890 | void emit_METAL_TEXM3X2TEX(Context *ctx) |
| 1891 | { |
| 1892 | if (ctx->texm3x2pad_src0 == -1) |
| 1893 | return; |
| 1894 | |
| 1895 | DestArgInfo *info = &ctx->dest_arg; |
| 1896 | char dst[64]; |
| 1897 | char src0[64]; |
| 1898 | char src1[64]; |
| 1899 | char src2[64]; |
| 1900 | char sampler[64]; |
| 1901 | char code[512]; |
| 1902 | |
| 1903 | ctx->metal_need_header_texture = 1; |
| 1904 | ctx->metal_need_header_geometric = 1; |
| 1905 | |
| 1906 | // !!! FIXME: this code counts on the register not having swizzles, etc. |
| 1907 | get_METAL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum, |
| 1908 | sampler, sizeof (sampler)); |
| 1909 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x2pad_src0, |
| 1910 | src0, sizeof (src0)); |
| 1911 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x2pad_dst0, |
| 1912 | src1, sizeof (src1)); |
| 1913 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum, |
| 1914 | src2, sizeof (src2)); |
| 1915 | get_METAL_destarg_varname(ctx, dst, sizeof (dst)); |
| 1916 | |
| 1917 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1918 | "%s_texture.sample(%s, float2(dot(%s.xyz, %s.xyz), dot(%s.xyz, %s.xyz)))", |
| 1919 | sampler, sampler, src0, src1, src2, dst); |
| 1920 | |
| 1921 | output_line(ctx, "%s", code); |
| 1922 | } // emit_METAL_TEXM3X2TEX |
| 1923 | |
| 1924 | void emit_METAL_TEXM3X3PAD(Context *ctx) |
| 1925 | { |
| 1926 | // no-op ... work happens in emit_METAL_TEXM3X3*(). |
| 1927 | } // emit_METAL_TEXM3X3PAD |
| 1928 | |
| 1929 | void emit_METAL_TEXM3X3TEX(Context *ctx) |
| 1930 | { |
| 1931 | if (ctx->texm3x3pad_src1 == -1) |
| 1932 | return; |
| 1933 | |
| 1934 | DestArgInfo *info = &ctx->dest_arg; |
| 1935 | char dst[64]; |
| 1936 | char src0[64]; |
| 1937 | char src1[64]; |
| 1938 | char src2[64]; |
| 1939 | char src3[64]; |
| 1940 | char src4[64]; |
| 1941 | char sampler[64]; |
| 1942 | char code[512]; |
| 1943 | |
| 1944 | ctx->metal_need_header_texture = 1; |
| 1945 | ctx->metal_need_header_geometric = 1; |
| 1946 | |
| 1947 | // !!! FIXME: this code counts on the register not having swizzles, etc. |
| 1948 | get_METAL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum, |
| 1949 | sampler, sizeof (sampler)); |
| 1950 | |
| 1951 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0, |
| 1952 | src0, sizeof (src0)); |
| 1953 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0, |
| 1954 | src1, sizeof (src1)); |
| 1955 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1, |
| 1956 | src2, sizeof (src2)); |
| 1957 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1, |
| 1958 | src3, sizeof (src3)); |
| 1959 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum, |
| 1960 | src4, sizeof (src4)); |
| 1961 | get_METAL_destarg_varname(ctx, dst, sizeof (dst)); |
| 1962 | |
| 1963 | RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, |
| 1964 | info->regnum); |
| 1965 | const TextureType ttype = (TextureType) (sreg ? sreg->index : 0); |
| 1966 | const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "Cube" : "3D"; |
| 1967 | |
| 1968 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 1969 | "texture%s(%s," |
| 1970 | " float3(dot(%s.xyz, %s.xyz)," |
| 1971 | " dot(%s.xyz, %s.xyz)," |
| 1972 | " dot(%s.xyz, %s.xyz)))", |
| 1973 | ttypestr, sampler, src0, src1, src2, src3, dst, src4); |
| 1974 | |
| 1975 | output_line(ctx, "%s", code); |
| 1976 | } // emit_METAL_TEXM3X3TEX |
| 1977 | |
| 1978 | void emit_METAL_TEXM3X3SPEC_helper(Context *ctx) |
| 1979 | { |
| 1980 | if (ctx->glsl_generated_texm3x3spec_helper) |
| 1981 | return; |
| 1982 | |
| 1983 | ctx->glsl_generated_texm3x3spec_helper = 1; |
| 1984 | |
| 1985 | push_output(ctx, &ctx->helpers); |
| 1986 | output_line(ctx, "float3 TEXM3X3SPEC_reflection(const float3 normal, const float3 eyeray)"); |
| 1987 | output_line(ctx, "{"); ctx->indent++; |
| 1988 | output_line(ctx, "return (2.0 * ((normal * eyeray) / (normal * normal)) * normal) - eyeray;"); ctx->indent--; |
| 1989 | output_line(ctx, "}"); |
| 1990 | output_blank_line(ctx); |
| 1991 | pop_output(ctx); |
| 1992 | } // emit_METAL_TEXM3X3SPEC_helper |
| 1993 | |
| 1994 | void emit_METAL_TEXM3X3SPEC(Context *ctx) |
| 1995 | { |
| 1996 | if (ctx->texm3x3pad_src1 == -1) |
| 1997 | return; |
| 1998 | |
| 1999 | DestArgInfo *info = &ctx->dest_arg; |
| 2000 | char dst[64]; |
| 2001 | char src0[64]; |
| 2002 | char src1[64]; |
| 2003 | char src2[64]; |
| 2004 | char src3[64]; |
| 2005 | char src4[64]; |
| 2006 | char src5[64]; |
| 2007 | char sampler[64]; |
| 2008 | char code[512]; |
| 2009 | |
| 2010 | ctx->metal_need_header_texture = 1; |
| 2011 | ctx->metal_need_header_geometric = 1; |
| 2012 | |
| 2013 | emit_METAL_TEXM3X3SPEC_helper(ctx); |
| 2014 | |
| 2015 | // !!! FIXME: this code counts on the register not having swizzles, etc. |
| 2016 | get_METAL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum, |
| 2017 | sampler, sizeof (sampler)); |
| 2018 | |
| 2019 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0, |
| 2020 | src0, sizeof (src0)); |
| 2021 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0, |
| 2022 | src1, sizeof (src1)); |
| 2023 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1, |
| 2024 | src2, sizeof (src2)); |
| 2025 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1, |
| 2026 | src3, sizeof (src3)); |
| 2027 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum, |
| 2028 | src4, sizeof (src4)); |
| 2029 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[1].regnum, |
| 2030 | src5, sizeof (src5)); |
| 2031 | get_METAL_destarg_varname(ctx, dst, sizeof (dst)); |
| 2032 | |
| 2033 | RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, |
| 2034 | info->regnum); |
| 2035 | const TextureType ttype = (TextureType) (sreg ? sreg->index : 0); |
| 2036 | const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "Cube" : "3D"; |
| 2037 | |
| 2038 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 2039 | "texture%s(%s, " |
| 2040 | "TEXM3X3SPEC_reflection(" |
| 2041 | "float3(" |
| 2042 | "dot(%s.xyz, %s.xyz), " |
| 2043 | "dot(%s.xyz, %s.xyz), " |
| 2044 | "dot(%s.xyz, %s.xyz)" |
| 2045 | ")," |
| 2046 | "%s.xyz," |
| 2047 | ")" |
| 2048 | ")", |
| 2049 | ttypestr, sampler, src0, src1, src2, src3, dst, src4, src5); |
| 2050 | |
| 2051 | output_line(ctx, "%s", code); |
| 2052 | } // emit_METAL_TEXM3X3SPEC |
| 2053 | |
| 2054 | void emit_METAL_TEXM3X3VSPEC(Context *ctx) |
| 2055 | { |
| 2056 | if (ctx->texm3x3pad_src1 == -1) |
| 2057 | return; |
| 2058 | |
| 2059 | DestArgInfo *info = &ctx->dest_arg; |
| 2060 | char dst[64]; |
| 2061 | char src0[64]; |
| 2062 | char src1[64]; |
| 2063 | char src2[64]; |
| 2064 | char src3[64]; |
| 2065 | char src4[64]; |
| 2066 | char sampler[64]; |
| 2067 | char code[512]; |
| 2068 | |
| 2069 | ctx->metal_need_header_texture = 1; |
| 2070 | ctx->metal_need_header_geometric = 1; |
| 2071 | |
| 2072 | emit_METAL_TEXM3X3SPEC_helper(ctx); |
| 2073 | |
| 2074 | // !!! FIXME: this code counts on the register not having swizzles, etc. |
| 2075 | get_METAL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum, |
| 2076 | sampler, sizeof (sampler)); |
| 2077 | |
| 2078 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0, |
| 2079 | src0, sizeof (src0)); |
| 2080 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0, |
| 2081 | src1, sizeof (src1)); |
| 2082 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1, |
| 2083 | src2, sizeof (src2)); |
| 2084 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1, |
| 2085 | src3, sizeof (src3)); |
| 2086 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum, |
| 2087 | src4, sizeof (src4)); |
| 2088 | get_METAL_destarg_varname(ctx, dst, sizeof (dst)); |
| 2089 | |
| 2090 | RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, |
| 2091 | info->regnum); |
| 2092 | const TextureType ttype = (TextureType) (sreg ? sreg->index : 0); |
| 2093 | const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "Cube" : "3D"; |
| 2094 | |
| 2095 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 2096 | "texture%s(%s, " |
| 2097 | "TEXM3X3SPEC_reflection(" |
| 2098 | "float3(" |
| 2099 | "dot(%s.xyz, %s.xyz), " |
| 2100 | "dot(%s.xyz, %s.xyz), " |
| 2101 | "dot(%s.xyz, %s.xyz)" |
| 2102 | "), " |
| 2103 | "float3(%s.w, %s.w, %s.w)" |
| 2104 | ")" |
| 2105 | ")", |
| 2106 | ttypestr, sampler, src0, src1, src2, src3, dst, src4, src0, src2, dst); |
| 2107 | |
| 2108 | output_line(ctx, "%s", code); |
| 2109 | } // emit_METAL_TEXM3X3VSPEC |
| 2110 | |
| 2111 | void emit_METAL_EXPP(Context *ctx) |
| 2112 | { |
| 2113 | // !!! FIXME: msdn's asm docs don't list this opcode, I'll have to check the driver documentation. |
| 2114 | emit_METAL_EXP(ctx); // I guess this is just partial precision EXP? |
| 2115 | } // emit_METAL_EXPP |
| 2116 | |
| 2117 | void emit_METAL_LOGP(Context *ctx) |
| 2118 | { |
| 2119 | // LOGP is just low-precision LOG, but we'll take the higher precision. |
| 2120 | emit_METAL_LOG(ctx); |
| 2121 | } // emit_METAL_LOGP |
| 2122 | |
| 2123 | // common code between CMP and CND. |
| 2124 | void emit_METAL_comparison_operations(Context *ctx, const char *cmp) |
| 2125 | { |
| 2126 | int i, j; |
| 2127 | DestArgInfo *dst = &ctx->dest_arg; |
| 2128 | const SourceArgInfo *srcarg0 = &ctx->source_args[0]; |
| 2129 | const int origmask = dst->writemask; |
| 2130 | int used_swiz[4] = { 0, 0, 0, 0 }; |
| 2131 | const int writemask[4] = { dst->writemask0, dst->writemask1, |
| 2132 | dst->writemask2, dst->writemask3 }; |
| 2133 | const int src0swiz[4] = { srcarg0->swizzle_x, srcarg0->swizzle_y, |
| 2134 | srcarg0->swizzle_z, srcarg0->swizzle_w }; |
| 2135 | |
| 2136 | for (i = 0; i < 4; i++) |
| 2137 | { |
| 2138 | int mask = (1 << i); |
| 2139 | |
| 2140 | if (!writemask[i]) continue; |
| 2141 | if (used_swiz[i]) continue; |
| 2142 | |
| 2143 | // This is a swizzle we haven't checked yet. |
| 2144 | used_swiz[i] = 1; |
| 2145 | |
| 2146 | // see if there are any other elements swizzled to match (.yyyy) |
| 2147 | for (j = i + 1; j < 4; j++) |
| 2148 | { |
| 2149 | if (!writemask[j]) continue; |
| 2150 | if (src0swiz[i] != src0swiz[j]) continue; |
| 2151 | mask |= (1 << j); |
| 2152 | used_swiz[j] = 1; |
| 2153 | } // for |
| 2154 | |
| 2155 | // okay, (mask) should be the writemask of swizzles we like. |
| 2156 | |
| 2157 | //return make_METAL_srcarg_string(ctx, idx, (1 << 0)); |
| 2158 | |
| 2159 | char src0[64]; |
| 2160 | char src1[64]; |
| 2161 | char src2[64]; |
| 2162 | make_METAL_srcarg_string(ctx, 0, (1 << i), src0, sizeof (src0)); |
| 2163 | make_METAL_srcarg_string(ctx, 1, mask, src1, sizeof (src1)); |
| 2164 | make_METAL_srcarg_string(ctx, 2, mask, src2, sizeof (src2)); |
| 2165 | |
| 2166 | set_dstarg_writemask(dst, mask); |
| 2167 | |
| 2168 | char code[128]; |
| 2169 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 2170 | "((%s %s) ? %s : %s)", |
| 2171 | src0, cmp, src1, src2); |
| 2172 | output_line(ctx, "%s", code); |
| 2173 | } // for |
| 2174 | |
| 2175 | set_dstarg_writemask(dst, origmask); |
| 2176 | } // emit_METAL_comparison_operations |
| 2177 | |
| 2178 | void emit_METAL_CND(Context *ctx) |
| 2179 | { |
| 2180 | emit_METAL_comparison_operations(ctx, "> 0.5"); |
| 2181 | } // emit_METAL_CND |
| 2182 | |
| 2183 | void emit_METAL_DEF(Context *ctx) |
| 2184 | { |
| 2185 | const float *val = (const float *) ctx->dwords; // !!! FIXME: could be int? |
| 2186 | char varname[64]; get_METAL_destarg_varname(ctx, varname, sizeof (varname)); |
| 2187 | char val0[32]; floatstr(ctx, val0, sizeof (val0), val[0], 1); |
| 2188 | char val1[32]; floatstr(ctx, val1, sizeof (val1), val[1], 1); |
| 2189 | char val2[32]; floatstr(ctx, val2, sizeof (val2), val[2], 1); |
| 2190 | char val3[32]; floatstr(ctx, val3, sizeof (val3), val[3], 1); |
| 2191 | |
| 2192 | push_output(ctx, &ctx->mainline_top); |
| 2193 | ctx->indent++; |
| 2194 | // The "(void) %s;" is to make the compiler not warn if this isn't used. |
| 2195 | output_line(ctx, "const float4 %s = float4(%s, %s, %s, %s); (void) %s;", |
| 2196 | varname, val0, val1, val2, val3, varname); |
| 2197 | pop_output(ctx); |
| 2198 | } // emit_METAL_DEF |
| 2199 | |
| 2200 | EMIT_METAL_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2RGB) // !!! FIXME |
| 2201 | EMIT_METAL_OPCODE_UNIMPLEMENTED_FUNC(TEXDP3TEX) // !!! FIXME |
| 2202 | EMIT_METAL_OPCODE_UNIMPLEMENTED_FUNC(TEXM3X2DEPTH) // !!! FIXME |
| 2203 | EMIT_METAL_OPCODE_UNIMPLEMENTED_FUNC(TEXDP3) // !!! FIXME |
| 2204 | |
| 2205 | void emit_METAL_TEXM3X3(Context *ctx) |
| 2206 | { |
| 2207 | if (ctx->texm3x3pad_src1 == -1) |
| 2208 | return; |
| 2209 | |
| 2210 | char dst[64]; |
| 2211 | char src0[64]; |
| 2212 | char src1[64]; |
| 2213 | char src2[64]; |
| 2214 | char src3[64]; |
| 2215 | char src4[64]; |
| 2216 | char code[512]; |
| 2217 | |
| 2218 | ctx->metal_need_header_geometric = 1; |
| 2219 | |
| 2220 | // !!! FIXME: this code counts on the register not having swizzles, etc. |
| 2221 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0, |
| 2222 | src0, sizeof (src0)); |
| 2223 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0, |
| 2224 | src1, sizeof (src1)); |
| 2225 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1, |
| 2226 | src2, sizeof (src2)); |
| 2227 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1, |
| 2228 | src3, sizeof (src3)); |
| 2229 | get_METAL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum, |
| 2230 | src4, sizeof (src4)); |
| 2231 | get_METAL_destarg_varname(ctx, dst, sizeof (dst)); |
| 2232 | |
| 2233 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 2234 | "float4(dot(%s.xyz, %s.xyz), dot(%s.xyz, %s.xyz), dot(%s.xyz, %s.xyz), 1.0)", |
| 2235 | src0, src1, src2, src3, dst, src4); |
| 2236 | |
| 2237 | output_line(ctx, "%s", code); |
| 2238 | } // emit_METAL_TEXM3X3 |
| 2239 | |
| 2240 | EMIT_METAL_OPCODE_UNIMPLEMENTED_FUNC(TEXDEPTH) // !!! FIXME |
| 2241 | |
| 2242 | void emit_METAL_CMP(Context *ctx) |
| 2243 | { |
| 2244 | emit_METAL_comparison_operations(ctx, ">= 0.0"); |
| 2245 | } // emit_METAL_CMP |
| 2246 | |
| 2247 | EMIT_METAL_OPCODE_UNIMPLEMENTED_FUNC(BEM) // !!! FIXME |
| 2248 | |
| 2249 | void emit_METAL_DP2ADD(Context *ctx) |
| 2250 | { |
| 2251 | char src0[64]; make_METAL_srcarg_string_vec2(ctx, 0, src0, sizeof (src0)); |
| 2252 | char src1[64]; make_METAL_srcarg_string_vec2(ctx, 1, src1, sizeof (src1)); |
| 2253 | char src2[64]; make_METAL_srcarg_string_scalar(ctx, 2, src2, sizeof (src2)); |
| 2254 | char extra[64]; snprintf(extra, sizeof (extra), " + %s", src2); |
| 2255 | emit_METAL_dotprod(ctx, src0, src1, extra); |
| 2256 | } // emit_METAL_DP2ADD |
| 2257 | |
| 2258 | void emit_METAL_DSX(Context *ctx) |
| 2259 | { |
| 2260 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 2261 | char code[128]; |
| 2262 | ctx->metal_need_header_graphics = 1; |
| 2263 | make_METAL_destarg_assign(ctx, code, sizeof (code), "dfdx(%s)", src0); |
| 2264 | output_line(ctx, "%s", code); |
| 2265 | } // emit_METAL_DSX |
| 2266 | |
| 2267 | void emit_METAL_DSY(Context *ctx) |
| 2268 | { |
| 2269 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 2270 | char code[128]; |
| 2271 | ctx->metal_need_header_graphics = 1; |
| 2272 | make_METAL_destarg_assign(ctx, code, sizeof (code), "dfdy(%s)", src0); |
| 2273 | output_line(ctx, "%s", code); |
| 2274 | } // emit_METAL_DSY |
| 2275 | |
| 2276 | void emit_METAL_TEXLDD(Context *ctx) |
| 2277 | { |
| 2278 | metal_texld(ctx, 1); |
| 2279 | } // emit_METAL_TEXLDD |
| 2280 | |
| 2281 | void emit_METAL_SETP(Context *ctx) |
| 2282 | { |
| 2283 | const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask); |
| 2284 | char src0[64]; make_METAL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); |
| 2285 | char src1[64]; make_METAL_srcarg_string_masked(ctx, 1, src1, sizeof (src1)); |
| 2286 | char code[128]; |
| 2287 | |
| 2288 | // destination is always predicate register (which is type bvec4). |
| 2289 | const char *comp = (vecsize == 1) ? |
| 2290 | get_METAL_comparison_string_scalar(ctx) : |
| 2291 | get_METAL_comparison_string_vector(ctx); |
| 2292 | |
| 2293 | make_METAL_destarg_assign(ctx, code, sizeof (code), |
| 2294 | "(%s %s %s)", src0, comp, src1); |
| 2295 | output_line(ctx, "%s", code); |
| 2296 | } // emit_METAL_SETP |
| 2297 | |
| 2298 | void emit_METAL_TEXLDL(Context *ctx) |
| 2299 | { |
| 2300 | // !!! FIXME: The spec says we can't use GLSL's texture*Lod() built-ins |
| 2301 | // !!! FIXME: from fragment shaders for some inexplicable reason. |
| 2302 | // !!! FIXME: Maybe Metal can do it, but I haven't looked into it yet. |
| 2303 | emit_METAL_TEXLD(ctx); |
| 2304 | } // emit_METAL_TEXLDL |
| 2305 | |
| 2306 | void emit_METAL_BREAKP(Context *ctx) |
| 2307 | { |
| 2308 | char src0[64]; make_METAL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0)); |
| 2309 | output_line(ctx, "if (%s) { break; }", src0); |
| 2310 | } // emit_METAL_BREAKP |
| 2311 | |
| 2312 | void emit_METAL_RESERVED(Context *ctx) |
| 2313 | { |
| 2314 | // do nothing; fails in the state machine. |
| 2315 | } // emit_METAL_RESERVED |
| 2316 | |
| 2317 | #endif // SUPPORT_PROFILE_METAL |
| 2318 | |
| 2319 | #pragma GCC visibility pop |