1/* 2 * Copyright © 2015 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 * 23 * Authors: 24 * Jason Ekstrand (jason@jlekstrand.net) 25 * 26 */ 27 28#include <math.h> 29 30#include "nir/nir_builtin_builder.h" 31 32#include "vtn_private.h" 33#include "GLSL.std.450.h" 34 35#define M_PIf ((float) M_PI) 36#define M_PI_2f ((float) M_PI_2) 37#define M_PI_4f ((float) M_PI_4) 38 39static nir_ssa_def * 40build_mat2_det(nir_builder *b, nir_ssa_def *col[2]) 41{ 42 unsigned swiz[2] = {1, 0 }; 43 nir_ssa_def *p = nir_fmul(b, col[0], nir_swizzle(b, col[1], swiz, 2)); 44 return nir_fsub(b, nir_channel(b, p, 0), nir_channel(b, p, 1)); 45} 46 47static nir_ssa_def * 48build_mat3_det(nir_builder *b, nir_ssa_def *col[3]) 49{ 50 unsigned yzx[3] = {1, 2, 0 }; 51 unsigned zxy[3] = {2, 0, 1 }; 52 53 nir_ssa_def *prod0 = 54 nir_fmul(b, col[0], 55 nir_fmul(b, nir_swizzle(b, col[1], yzx, 3), 56 nir_swizzle(b, col[2], zxy, 3))); 57 nir_ssa_def *prod1 = 58 nir_fmul(b, col[0], 59 nir_fmul(b, nir_swizzle(b, col[1], zxy, 3), 60 nir_swizzle(b, col[2], yzx, 3))); 61 62 nir_ssa_def *diff = nir_fsub(b, prod0, prod1); 63 64 return nir_fadd(b, nir_channel(b, diff, 0), 65 nir_fadd(b, nir_channel(b, diff, 1), 66 nir_channel(b, diff, 2))); 67} 68 69static nir_ssa_def * 70build_mat4_det(nir_builder *b, nir_ssa_def **col) 71{ 72 nir_ssa_def *subdet[4]; 73 for (unsigned i = 0; i < 4; i++) { 74 unsigned swiz[3]; 75 for (unsigned j = 0; j < 3; j++) 76 swiz[j] = j + (j >= i); 77 78 nir_ssa_def *subcol[3]; 79 subcol[0] = nir_swizzle(b, col[1], swiz, 3); 80 subcol[1] = nir_swizzle(b, col[2], swiz, 3); 81 subcol[2] = nir_swizzle(b, col[3], swiz, 3); 82 83 subdet[i] = build_mat3_det(b, subcol); 84 } 85 86 nir_ssa_def *prod = nir_fmul(b, col[0], nir_vec(b, subdet, 4)); 87 88 return nir_fadd(b, nir_fsub(b, nir_channel(b, prod, 0), 89 nir_channel(b, prod, 1)), 90 nir_fsub(b, nir_channel(b, prod, 2), 91 nir_channel(b, prod, 3))); 92} 93 94static nir_ssa_def * 95build_mat_det(struct vtn_builder *b, struct vtn_ssa_value *src) 96{ 97 unsigned size = glsl_get_vector_elements(src->type); 98 99 nir_ssa_def *cols[4]; 100 for (unsigned i = 0; i < size; i++) 101 cols[i] = src->elems[i]->def; 102 103 switch(size) { 104 case 2: return build_mat2_det(&b->nb, cols); 105 case 3: return build_mat3_det(&b->nb, cols); 106 case 4: return build_mat4_det(&b->nb, cols); 107 default: 108 vtn_fail("Invalid matrix size"); 109 } 110} 111 112/* Computes the determinate of the submatrix given by taking src and 113 * removing the specified row and column. 114 */ 115static nir_ssa_def * 116build_mat_subdet(struct nir_builder *b, struct vtn_ssa_value *src, 117 unsigned size, unsigned row, unsigned col) 118{ 119 assert(row < size && col < size); 120 if (size == 2) { 121 return nir_channel(b, src->elems[1 - col]->def, 1 - row); 122 } else { 123 /* Swizzle to get all but the specified row */ 124 unsigned swiz[NIR_MAX_VEC_COMPONENTS] = {0}; 125 for (unsigned j = 0; j < 3; j++) 126 swiz[j] = j + (j >= row); 127 128 /* Grab all but the specified column */ 129 nir_ssa_def *subcol[3]; 130 for (unsigned j = 0; j < size; j++) { 131 if (j != col) { 132 subcol[j - (j > col)] = nir_swizzle(b, src->elems[j]->def, 133 swiz, size - 1); 134 } 135 } 136 137 if (size == 3) { 138 return build_mat2_det(b, subcol); 139 } else { 140 assert(size == 4); 141 return build_mat3_det(b, subcol); 142 } 143 } 144} 145 146static struct vtn_ssa_value * 147matrix_inverse(struct vtn_builder *b, struct vtn_ssa_value *src) 148{ 149 nir_ssa_def *adj_col[4]; 150 unsigned size = glsl_get_vector_elements(src->type); 151 152 /* Build up an adjugate matrix */ 153 for (unsigned c = 0; c < size; c++) { 154 nir_ssa_def *elem[4]; 155 for (unsigned r = 0; r < size; r++) { 156 elem[r] = build_mat_subdet(&b->nb, src, size, c, r); 157 158 if ((r + c) % 2) 159 elem[r] = nir_fneg(&b->nb, elem[r]); 160 } 161 162 adj_col[c] = nir_vec(&b->nb, elem, size); 163 } 164 165 nir_ssa_def *det_inv = nir_frcp(&b->nb, build_mat_det(b, src)); 166 167 struct vtn_ssa_value *val = vtn_create_ssa_value(b, src->type); 168 for (unsigned i = 0; i < size; i++) 169 val->elems[i]->def = nir_fmul(&b->nb, adj_col[i], det_inv); 170 171 return val; 172} 173 174/** 175 * Approximate asin(x) by the piecewise formula: 176 * for |x| < 0.5, asin~(x) = x * (1 + x²(pS0 + x²(pS1 + x²*pS2)) / (1 + x²*qS1)) 177 * for |x| ≥ 0.5, asin~(x) = sign(x) * (π/2 - sqrt(1 - |x|) * (π/2 + |x|(π/4 - 1 + |x|(p0 + |x|p1)))) 178 * 179 * The latter is correct to first order at x=0 and x=±1 regardless of the p 180 * coefficients but can be made second-order correct at both ends by selecting 181 * the fit coefficients appropriately. Different p coefficients can be used 182 * in the asin and acos implementation to minimize some relative error metric 183 * in each case. 184 */ 185static nir_ssa_def * 186build_asin(nir_builder *b, nir_ssa_def *x, float p0, float p1, bool piecewise) 187{ 188 if (x->bit_size == 16) { 189 /* The polynomial approximation isn't precise enough to meet half-float 190 * precision requirements. Alternatively, we could implement this using 191 * the formula: 192 * 193 * asin(x) = atan2(x, sqrt(1 - x*x)) 194 * 195 * But that is very expensive, so instead we just do the polynomial 196 * approximation in 32-bit math and then we convert the result back to 197 * 16-bit. 198 */ 199 return nir_f2f16(b, build_asin(b, nir_f2f32(b, x), p0, p1, piecewise)); 200 } 201 nir_ssa_def *one = nir_imm_floatN_t(b, 1.0f, x->bit_size); 202 nir_ssa_def *half = nir_imm_floatN_t(b, 0.5f, x->bit_size); 203 nir_ssa_def *abs_x = nir_fabs(b, x); 204 205 nir_ssa_def *p0_plus_xp1 = nir_ffma_imm12(b, abs_x, p1, p0); 206 207 nir_ssa_def *expr_tail = 208 nir_ffma_imm2(b, abs_x, 209 nir_ffma_imm2(b, abs_x, p0_plus_xp1, M_PI_4f - 1.0f), 210 M_PI_2f); 211 212 nir_ssa_def *result0 = nir_fmul(b, nir_fsign(b, x), 213 nir_a_minus_bc(b, nir_imm_floatN_t(b, M_PI_2f, x->bit_size), 214 nir_fsqrt(b, nir_fsub(b, one, abs_x)), 215 expr_tail)); 216 if (piecewise) { 217 /* approximation for |x| < 0.5 */ 218 const float pS0 = 1.6666586697e-01f; 219 const float pS1 = -4.2743422091e-02f; 220 const float pS2 = -8.6563630030e-03f; 221 const float qS1 = -7.0662963390e-01f; 222 223 nir_ssa_def *x2 = nir_fmul(b, x, x); 224 nir_ssa_def *p = nir_fmul(b, 225 x2, 226 nir_ffma_imm2(b, x2, 227 nir_ffma_imm12(b, x2, pS2, pS1), 228 pS0)); 229 230 nir_ssa_def *q = nir_ffma_imm1(b, x2, qS1, one); 231 nir_ssa_def *result1 = nir_ffma(b, x, nir_fdiv(b, p, q), x); 232 return nir_bcsel(b, nir_flt(b, abs_x, half), result1, result0); 233 } else { 234 return result0; 235 } 236} 237 238static nir_op 239vtn_nir_alu_op_for_spirv_glsl_opcode(struct vtn_builder *b, 240 enum GLSLstd450 opcode, 241 unsigned execution_mode, 242 bool *exact) 243{ 244 *exact = false; 245 switch (opcode) { 246 case GLSLstd450Round: return nir_op_fround_even; 247 case GLSLstd450RoundEven: return nir_op_fround_even; 248 case GLSLstd450Trunc: return nir_op_ftrunc; 249 case GLSLstd450FAbs: return nir_op_fabs; 250 case GLSLstd450SAbs: return nir_op_iabs; 251 case GLSLstd450FSign: return nir_op_fsign; 252 case GLSLstd450SSign: return nir_op_isign; 253 case GLSLstd450Floor: return nir_op_ffloor; 254 case GLSLstd450Ceil: return nir_op_fceil; 255 case GLSLstd450Fract: return nir_op_ffract; 256 case GLSLstd450Sin: return nir_op_fsin; 257 case GLSLstd450Cos: return nir_op_fcos; 258 case GLSLstd450Pow: return nir_op_fpow; 259 case GLSLstd450Exp2: return nir_op_fexp2; 260 case GLSLstd450Log2: return nir_op_flog2; 261 case GLSLstd450Sqrt: return nir_op_fsqrt; 262 case GLSLstd450InverseSqrt: return nir_op_frsq; 263 case GLSLstd450NMin: *exact = true; return nir_op_fmin; 264 case GLSLstd450FMin: return nir_op_fmin; 265 case GLSLstd450UMin: return nir_op_umin; 266 case GLSLstd450SMin: return nir_op_imin; 267 case GLSLstd450NMax: *exact = true; return nir_op_fmax; 268 case GLSLstd450FMax: return nir_op_fmax; 269 case GLSLstd450UMax: return nir_op_umax; 270 case GLSLstd450SMax: return nir_op_imax; 271 case GLSLstd450FMix: return nir_op_flrp; 272 case GLSLstd450Fma: return nir_op_ffma; 273 case GLSLstd450Ldexp: return nir_op_ldexp; 274 case GLSLstd450FindILsb: return nir_op_find_lsb; 275 case GLSLstd450FindSMsb: return nir_op_ifind_msb; 276 case GLSLstd450FindUMsb: return nir_op_ufind_msb; 277 278 /* Packing/Unpacking functions */ 279 case GLSLstd450PackSnorm4x8: return nir_op_pack_snorm_4x8; 280 case GLSLstd450PackUnorm4x8: return nir_op_pack_unorm_4x8; 281 case GLSLstd450PackSnorm2x16: return nir_op_pack_snorm_2x16; 282 case GLSLstd450PackUnorm2x16: return nir_op_pack_unorm_2x16; 283 case GLSLstd450PackHalf2x16: return nir_op_pack_half_2x16; 284 case GLSLstd450PackDouble2x32: return nir_op_pack_64_2x32; 285 case GLSLstd450UnpackSnorm4x8: return nir_op_unpack_snorm_4x8; 286 case GLSLstd450UnpackUnorm4x8: return nir_op_unpack_unorm_4x8; 287 case GLSLstd450UnpackSnorm2x16: return nir_op_unpack_snorm_2x16; 288 case GLSLstd450UnpackUnorm2x16: return nir_op_unpack_unorm_2x16; 289 case GLSLstd450UnpackHalf2x16: 290 if (execution_mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16) 291 return nir_op_unpack_half_2x16_flush_to_zero; 292 else 293 return nir_op_unpack_half_2x16; 294 case GLSLstd450UnpackDouble2x32: return nir_op_unpack_64_2x32; 295 296 default: 297 vtn_fail("No NIR equivalent"); 298 } 299} 300 301#define NIR_IMM_FP(n, v) (nir_imm_floatN_t(n, v, src[0]->bit_size)) 302 303static void 304handle_glsl450_alu(struct vtn_builder *b, enum GLSLstd450 entrypoint, 305 const uint32_t *w, unsigned count) 306{ 307 struct nir_builder *nb = &b->nb; 308 const struct glsl_type *dest_type = vtn_get_type(b, w[1])->type; 309 310 /* Collect the various SSA sources */ 311 unsigned num_inputs = count - 5; 312 nir_ssa_def *src[3] = { NULL, }; 313 for (unsigned i = 0; i < num_inputs; i++) { 314 /* These are handled specially below */ 315 if (vtn_untyped_value(b, w[i + 5])->value_type == vtn_value_type_pointer) 316 continue; 317 318 src[i] = vtn_get_nir_ssa(b, w[i + 5]); 319 } 320 321 struct vtn_ssa_value *dest = vtn_create_ssa_value(b, dest_type); 322 vtn_handle_no_contraction(b, vtn_untyped_value(b, w[2])); 323 switch (entrypoint) { 324 case GLSLstd450Radians: 325 dest->def = nir_radians(nb, src[0]); 326 break; 327 case GLSLstd450Degrees: 328 dest->def = nir_degrees(nb, src[0]); 329 break; 330 case GLSLstd450Tan: 331 dest->def = nir_ftan(nb, src[0]); 332 break; 333 334 case GLSLstd450Modf: { 335 nir_ssa_def *inf = nir_imm_floatN_t(&b->nb, INFINITY, src[0]->bit_size); 336 nir_ssa_def *sign_bit = 337 nir_imm_intN_t(&b->nb, (uint64_t)1 << (src[0]->bit_size - 1), 338 src[0]->bit_size); 339 nir_ssa_def *sign = nir_fsign(nb, src[0]); 340 nir_ssa_def *abs = nir_fabs(nb, src[0]); 341 342 /* NaN input should produce a NaN results, and ±Inf input should provide 343 * ±0 result. The fmul(sign(x), ffract(x)) calculation will already 344 * produce the expected NaN. To get ±0, directly compare for equality 345 * with Inf instead of using fisfinite (which is false for NaN). 346 */ 347 dest->def = nir_bcsel(nb, 348 nir_ieq(nb, abs, inf), 349 nir_iand(nb, src[0], sign_bit), 350 nir_fmul(nb, sign, nir_ffract(nb, abs))); 351 352 struct vtn_pointer *i_ptr = vtn_value(b, w[6], vtn_value_type_pointer)->pointer; 353 struct vtn_ssa_value *whole = vtn_create_ssa_value(b, i_ptr->type->type); 354 whole->def = nir_fmul(nb, sign, nir_ffloor(nb, abs)); 355 vtn_variable_store(b, whole, i_ptr, 0); 356 break; 357 } 358 359 case GLSLstd450ModfStruct: { 360 nir_ssa_def *inf = nir_imm_floatN_t(&b->nb, INFINITY, src[0]->bit_size); 361 nir_ssa_def *sign_bit = 362 nir_imm_intN_t(&b->nb, (uint64_t)1 << (src[0]->bit_size - 1), 363 src[0]->bit_size); 364 nir_ssa_def *sign = nir_fsign(nb, src[0]); 365 nir_ssa_def *abs = nir_fabs(nb, src[0]); 366 vtn_assert(glsl_type_is_struct_or_ifc(dest_type)); 367 368 /* See GLSLstd450Modf for explanation of the Inf and NaN handling. */ 369 dest->elems[0]->def = nir_bcsel(nb, 370 nir_ieq(nb, abs, inf), 371 nir_iand(nb, src[0], sign_bit), 372 nir_fmul(nb, sign, nir_ffract(nb, abs))); 373 dest->elems[1]->def = nir_fmul(nb, sign, nir_ffloor(nb, abs)); 374 break; 375 } 376 377 case GLSLstd450Step: { 378 /* The SPIR-V Extended Instructions for GLSL spec says: 379 * 380 * Result is 0.0 if x < edge; otherwise result is 1.0. 381 * 382 * Here src[1] is x, and src[0] is edge. The direct implementation is 383 * 384 * bcsel(src[1] < src[0], 0.0, 1.0) 385 * 386 * This is effectively b2f(!(src1 < src0)). Previously this was 387 * implemented using sge(src1, src0), but that produces incorrect 388 * results for NaN. Instead, we use the identity b2f(!x) = 1 - b2f(x). 389 */ 390 const bool exact = nb->exact; 391 nb->exact = true; 392 393 nir_ssa_def *cmp = nir_slt(nb, src[1], src[0]); 394 395 nb->exact = exact; 396 dest->def = nir_fsub(nb, nir_imm_floatN_t(nb, 1.0f, cmp->bit_size), cmp); 397 break; 398 } 399 400 case GLSLstd450Length: 401 dest->def = nir_fast_length(nb, src[0]); 402 break; 403 case GLSLstd450Distance: 404 dest->def = nir_fast_distance(nb, src[0], src[1]); 405 break; 406 case GLSLstd450Normalize: 407 dest->def = nir_fast_normalize(nb, src[0]); 408 break; 409 410 case GLSLstd450Exp: 411 dest->def = nir_fexp(nb, src[0]); 412 break; 413 414 case GLSLstd450Log: 415 dest->def = nir_flog(nb, src[0]); 416 break; 417 418 case GLSLstd450FClamp: 419 dest->def = nir_fclamp(nb, src[0], src[1], src[2]); 420 break; 421 case GLSLstd450NClamp: 422 nb->exact = true; 423 dest->def = nir_fclamp(nb, src[0], src[1], src[2]); 424 nb->exact = false; 425 break; 426 case GLSLstd450UClamp: 427 dest->def = nir_uclamp(nb, src[0], src[1], src[2]); 428 break; 429 case GLSLstd450SClamp: 430 dest->def = nir_iclamp(nb, src[0], src[1], src[2]); 431 break; 432 433 case GLSLstd450Cross: { 434 dest->def = nir_cross3(nb, src[0], src[1]); 435 break; 436 } 437 438 case GLSLstd450SmoothStep: { 439 dest->def = nir_smoothstep(nb, src[0], src[1], src[2]); 440 break; 441 } 442 443 case GLSLstd450FaceForward: 444 dest->def = 445 nir_bcsel(nb, nir_flt(nb, nir_fdot(nb, src[2], src[1]), 446 NIR_IMM_FP(nb, 0.0)), 447 src[0], nir_fneg(nb, src[0])); 448 break; 449 450 case GLSLstd450Reflect: 451 /* I - 2 * dot(N, I) * N */ 452 dest->def = 453 nir_a_minus_bc(nb, src[0], 454 src[1], 455 nir_fmul(nb, nir_fdot(nb, src[0], src[1]), 456 NIR_IMM_FP(nb, 2.0))); 457 break; 458 459 case GLSLstd450Refract: { 460 nir_ssa_def *I = src[0]; 461 nir_ssa_def *N = src[1]; 462 nir_ssa_def *eta = src[2]; 463 nir_ssa_def *n_dot_i = nir_fdot(nb, N, I); 464 nir_ssa_def *one = NIR_IMM_FP(nb, 1.0); 465 nir_ssa_def *zero = NIR_IMM_FP(nb, 0.0); 466 /* According to the SPIR-V and GLSL specs, eta is always a float 467 * regardless of the type of the other operands. However in practice it 468 * seems that if you try to pass it a float then glslang will just 469 * promote it to a double and generate invalid SPIR-V. In order to 470 * support a hypothetical fixed version of glslang we’ll promote eta to 471 * double if the other operands are double also. 472 */ 473 if (I->bit_size != eta->bit_size) { 474 nir_op conversion_op = 475 nir_type_conversion_op(nir_type_float | eta->bit_size, 476 nir_type_float | I->bit_size, 477 nir_rounding_mode_undef); 478 eta = nir_build_alu(nb, conversion_op, eta, NULL, NULL, NULL); 479 } 480 /* k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I)) */ 481 nir_ssa_def *k = 482 nir_a_minus_bc(nb, one, eta, 483 nir_fmul(nb, eta, nir_a_minus_bc(nb, one, n_dot_i, n_dot_i))); 484 nir_ssa_def *result = 485 nir_a_minus_bc(nb, nir_fmul(nb, eta, I), 486 nir_ffma(nb, eta, n_dot_i, nir_fsqrt(nb, k)), 487 N); 488 /* XXX: bcsel, or if statement? */ 489 dest->def = nir_bcsel(nb, nir_flt(nb, k, zero), zero, result); 490 break; 491 } 492 493 case GLSLstd450Sinh: 494 /* 0.5 * (e^x - e^(-x)) */ 495 dest->def = 496 nir_fmul_imm(nb, nir_fsub(nb, nir_fexp(nb, src[0]), 497 nir_fexp(nb, nir_fneg(nb, src[0]))), 498 0.5f); 499 break; 500 501 case GLSLstd450Cosh: 502 /* 0.5 * (e^x + e^(-x)) */ 503 dest->def = 504 nir_fmul_imm(nb, nir_fadd(nb, nir_fexp(nb, src[0]), 505 nir_fexp(nb, nir_fneg(nb, src[0]))), 506 0.5f); 507 break; 508 509 case GLSLstd450Tanh: { 510 /* tanh(x) := (e^x - e^(-x)) / (e^x + e^(-x)) 511 * 512 * We clamp x to [-10, +10] to avoid precision problems. When x > 10, 513 * e^x dominates the sum, e^(-x) is lost and tanh(x) is 1.0 for 32 bit 514 * floating point. 515 * 516 * For 16-bit precision this we clamp x to [-4.2, +4.2]. 517 */ 518 const uint32_t bit_size = src[0]->bit_size; 519 const double clamped_x = bit_size > 16 ? 10.0 : 4.2; 520 nir_ssa_def *x = nir_fclamp(nb, src[0], 521 nir_imm_floatN_t(nb, -clamped_x, bit_size), 522 nir_imm_floatN_t(nb, clamped_x, bit_size)); 523 524 /* The clamping will filter out NaN values causing an incorrect result. 525 * The comparison is carefully structured to get NaN result for NaN and 526 * get -0 for -0. 527 * 528 * result = abs(s) > 0.0 ? ... : s; 529 */ 530 const bool exact = nb->exact; 531 532 nb->exact = true; 533 nir_ssa_def *is_regular = nir_flt(nb, 534 nir_imm_floatN_t(nb, 0, bit_size), 535 nir_fabs(nb, src[0])); 536 537 /* The extra 1.0*s ensures that subnormal inputs are flushed to zero 538 * when that is selected by the shader. 539 */ 540 nir_ssa_def *flushed = nir_fmul(nb, 541 src[0], 542 nir_imm_floatN_t(nb, 1.0, bit_size)); 543 nb->exact = exact; 544 545 dest->def = nir_bcsel(nb, 546 is_regular, 547 nir_fdiv(nb, nir_fsub(nb, nir_fexp(nb, x), 548 nir_fexp(nb, nir_fneg(nb, x))), 549 nir_fadd(nb, nir_fexp(nb, x), 550 nir_fexp(nb, nir_fneg(nb, x)))), 551 flushed); 552 break; 553 } 554 555 case GLSLstd450Asinh: 556 dest->def = nir_fmul(nb, nir_fsign(nb, src[0]), 557 nir_flog(nb, nir_fadd(nb, nir_fabs(nb, src[0]), 558 nir_fsqrt(nb, nir_ffma_imm2(nb, src[0], src[0], 1.0f))))); 559 break; 560 case GLSLstd450Acosh: 561 dest->def = nir_flog(nb, nir_fadd(nb, src[0], 562 nir_fsqrt(nb, nir_ffma_imm2(nb, src[0], src[0], -1.0f)))); 563 break; 564 case GLSLstd450Atanh: { 565 nir_ssa_def *one = nir_imm_floatN_t(nb, 1.0, src[0]->bit_size); 566 dest->def = 567 nir_fmul_imm(nb, nir_flog(nb, nir_fdiv(nb, nir_fadd(nb, src[0], one), 568 nir_fsub(nb, one, src[0]))), 569 0.5f); 570 break; 571 } 572 573 case GLSLstd450Asin: 574 dest->def = build_asin(nb, src[0], 0.086566724, -0.03102955, true); 575 break; 576 577 case GLSLstd450Acos: 578 dest->def = 579 nir_fsub(nb, nir_imm_floatN_t(nb, M_PI_2f, src[0]->bit_size), 580 build_asin(nb, src[0], 0.08132463, -0.02363318, false)); 581 break; 582 583 case GLSLstd450Atan: 584 dest->def = nir_atan(nb, src[0]); 585 break; 586 587 case GLSLstd450Atan2: 588 dest->def = nir_atan2(nb, src[0], src[1]); 589 break; 590 591 case GLSLstd450Frexp: { 592 dest->def = nir_frexp_sig(nb, src[0]); 593 594 struct vtn_pointer *i_ptr = vtn_value(b, w[6], vtn_value_type_pointer)->pointer; 595 struct vtn_ssa_value *exp = vtn_create_ssa_value(b, i_ptr->type->type); 596 exp->def = nir_frexp_exp(nb, src[0]); 597 vtn_variable_store(b, exp, i_ptr, 0); 598 break; 599 } 600 601 case GLSLstd450FrexpStruct: { 602 vtn_assert(glsl_type_is_struct_or_ifc(dest_type)); 603 dest->elems[0]->def = nir_frexp_sig(nb, src[0]); 604 dest->elems[1]->def = nir_frexp_exp(nb, src[0]); 605 break; 606 } 607 608 default: { 609 unsigned execution_mode = 610 b->shader->info.float_controls_execution_mode; 611 bool exact; 612 nir_op op = vtn_nir_alu_op_for_spirv_glsl_opcode(b, entrypoint, execution_mode, &exact); 613 /* don't override explicit decoration */ 614 b->nb.exact |= exact; 615 dest->def = nir_build_alu(&b->nb, op, src[0], src[1], src[2], NULL); 616 break; 617 } 618 } 619 b->nb.exact = false; 620 621 vtn_push_ssa_value(b, w[2], dest); 622} 623 624static void 625handle_glsl450_interpolation(struct vtn_builder *b, enum GLSLstd450 opcode, 626 const uint32_t *w, unsigned count) 627{ 628 nir_intrinsic_op op; 629 switch (opcode) { 630 case GLSLstd450InterpolateAtCentroid: 631 op = nir_intrinsic_interp_deref_at_centroid; 632 break; 633 case GLSLstd450InterpolateAtSample: 634 op = nir_intrinsic_interp_deref_at_sample; 635 break; 636 case GLSLstd450InterpolateAtOffset: 637 op = nir_intrinsic_interp_deref_at_offset; 638 break; 639 default: 640 vtn_fail("Invalid opcode"); 641 } 642 643 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->nb.shader, op); 644 645 struct vtn_pointer *ptr = 646 vtn_value(b, w[5], vtn_value_type_pointer)->pointer; 647 nir_deref_instr *deref = vtn_pointer_to_deref(b, ptr); 648 649 /* If the value we are interpolating has an index into a vector then 650 * interpolate the vector and index the result of that instead. This is 651 * necessary because the index will get generated as a series of nir_bcsel 652 * instructions so it would no longer be an input variable. 653 */ 654 const bool vec_array_deref = deref->deref_type == nir_deref_type_array && 655 glsl_type_is_vector(nir_deref_instr_parent(deref)->type); 656 657 nir_deref_instr *vec_deref = NULL; 658 if (vec_array_deref) { 659 vec_deref = deref; 660 deref = nir_deref_instr_parent(deref); 661 } 662 intrin->src[0] = nir_src_for_ssa(&deref->dest.ssa); 663 664 switch (opcode) { 665 case GLSLstd450InterpolateAtCentroid: 666 break; 667 case GLSLstd450InterpolateAtSample: 668 case GLSLstd450InterpolateAtOffset: 669 intrin->src[1] = nir_src_for_ssa(vtn_get_nir_ssa(b, w[6])); 670 break; 671 default: 672 vtn_fail("Invalid opcode"); 673 } 674 675 intrin->num_components = glsl_get_vector_elements(deref->type); 676 nir_ssa_dest_init(&intrin->instr, &intrin->dest, 677 glsl_get_vector_elements(deref->type), 678 glsl_get_bit_size(deref->type), NULL); 679 680 nir_builder_instr_insert(&b->nb, &intrin->instr); 681 682 nir_ssa_def *def = &intrin->dest.ssa; 683 if (vec_array_deref) 684 def = nir_vector_extract(&b->nb, def, vec_deref->arr.index.ssa); 685 686 vtn_push_nir_ssa(b, w[2], def); 687} 688 689bool 690vtn_handle_glsl450_instruction(struct vtn_builder *b, SpvOp ext_opcode, 691 const uint32_t *w, unsigned count) 692{ 693 switch ((enum GLSLstd450)ext_opcode) { 694 case GLSLstd450Determinant: { 695 vtn_push_nir_ssa(b, w[2], build_mat_det(b, vtn_ssa_value(b, w[5]))); 696 break; 697 } 698 699 case GLSLstd450MatrixInverse: { 700 vtn_push_ssa_value(b, w[2], matrix_inverse(b, vtn_ssa_value(b, w[5]))); 701 break; 702 } 703 704 case GLSLstd450InterpolateAtCentroid: 705 case GLSLstd450InterpolateAtSample: 706 case GLSLstd450InterpolateAtOffset: 707 handle_glsl450_interpolation(b, (enum GLSLstd450)ext_opcode, w, count); 708 break; 709 710 default: 711 handle_glsl450_alu(b, (enum GLSLstd450)ext_opcode, w, count); 712 } 713 714 return true; 715} 716