2 * Copyright © 2015 Intel Corporation
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:
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
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
24 * Jason Ekstrand (jason@jlekstrand.net)
30 #include "nir/nir_builtin_builder.h"
32 #include "vtn_private.h"
33 #include "GLSL.std.450.h"
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)
40 build_mat2_det(nir_builder
*b
, nir_ssa_def
*col
[2])
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));
48 build_mat3_det(nir_builder
*b
, nir_ssa_def
*col
[3])
50 unsigned yzx
[3] = {1, 2, 0 };
51 unsigned zxy
[3] = {2, 0, 1 };
55 nir_fmul(b
, nir_swizzle(b
, col
[1], yzx
, 3),
56 nir_swizzle(b
, col
[2], zxy
, 3)));
59 nir_fmul(b
, nir_swizzle(b
, col
[1], zxy
, 3),
60 nir_swizzle(b
, col
[2], yzx
, 3)));
62 nir_ssa_def
*diff
= nir_fsub(b
, prod0
, prod1
);
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)));
70 build_mat4_det(nir_builder
*b
, nir_ssa_def
**col
)
72 nir_ssa_def
*subdet
[4];
73 for (unsigned i
= 0; i
< 4; i
++) {
75 for (unsigned j
= 0; j
< 3; j
++)
76 swiz
[j
] = j
+ (j
>= i
);
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);
83 subdet
[i
] = build_mat3_det(b
, subcol
);
86 nir_ssa_def
*prod
= nir_fmul(b
, col
[0], nir_vec(b
, subdet
, 4));
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)));
95 build_mat_det(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
97 unsigned size
= glsl_get_vector_elements(src
->type
);
100 for (unsigned i
= 0; i
< size
; i
++)
101 cols
[i
] = src
->elems
[i
]->def
;
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
);
108 vtn_fail("Invalid matrix size");
112 /* Computes the determinate of the submatrix given by taking src and
113 * removing the specified row and column.
116 build_mat_subdet(struct nir_builder
*b
, struct vtn_ssa_value
*src
,
117 unsigned size
, unsigned row
, unsigned col
)
119 assert(row
< size
&& col
< size
);
121 return nir_channel(b
, src
->elems
[1 - col
]->def
, 1 - row
);
123 /* Swizzle to get all but the specified row */
125 for (unsigned j
= 0; j
< 3; j
++)
126 swiz
[j
] = j
+ (j
>= row
);
128 /* Grab all but the specified column */
129 nir_ssa_def
*subcol
[3];
130 for (unsigned j
= 0; j
< size
; j
++) {
132 subcol
[j
- (j
> col
)] = nir_swizzle(b
, src
->elems
[j
]->def
,
138 return build_mat2_det(b
, subcol
);
141 return build_mat3_det(b
, subcol
);
146 static struct vtn_ssa_value
*
147 matrix_inverse(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
149 nir_ssa_def
*adj_col
[4];
150 unsigned size
= glsl_get_vector_elements(src
->type
);
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
);
159 elem
[r
] = nir_fneg(&b
->nb
, elem
[r
]);
162 adj_col
[c
] = nir_vec(&b
->nb
, elem
, size
);
165 nir_ssa_def
*det_inv
= nir_frcp(&b
->nb
, build_mat_det(b
, src
));
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
);
178 build_exp(nir_builder
*b
, nir_ssa_def
*x
)
180 return nir_fexp2(b
, nir_fmul_imm(b
, x
, M_LOG2E
));
184 * Return ln(x) - the natural logarithm of x.
187 build_log(nir_builder
*b
, nir_ssa_def
*x
)
189 return nir_fmul_imm(b
, nir_flog2(b
, x
), 1.0 / M_LOG2E
);
193 * Approximate asin(x) by the formula:
194 * asin~(x) = sign(x) * (pi/2 - sqrt(1 - |x|) * (pi/2 + |x|(pi/4 - 1 + |x|(p0 + |x|p1))))
196 * which is correct to first order at x=0 and x=±1 regardless of the p
197 * coefficients but can be made second-order correct at both ends by selecting
198 * the fit coefficients appropriately. Different p coefficients can be used
199 * in the asin and acos implementation to minimize some relative error metric
203 build_asin(nir_builder
*b
, nir_ssa_def
*x
, float p0
, float p1
)
205 if (x
->bit_size
== 16) {
206 /* The polynomial approximation isn't precise enough to meet half-float
207 * precision requirements. Alternatively, we could implement this using
210 * asin(x) = atan2(x, sqrt(1 - x*x))
212 * But that is very expensive, so instead we just do the polynomial
213 * approximation in 32-bit math and then we convert the result back to
216 return nir_f2f16(b
, build_asin(b
, nir_f2f32(b
, x
), p0
, p1
));
219 nir_ssa_def
*one
= nir_imm_floatN_t(b
, 1.0f
, x
->bit_size
);
220 nir_ssa_def
*abs_x
= nir_fabs(b
, x
);
222 nir_ssa_def
*p0_plus_xp1
= nir_fadd_imm(b
, nir_fmul_imm(b
, abs_x
, p1
), p0
);
224 nir_ssa_def
*expr_tail
=
225 nir_fadd_imm(b
, nir_fmul(b
, abs_x
,
226 nir_fadd_imm(b
, nir_fmul(b
, abs_x
,
231 return nir_fmul(b
, nir_fsign(b
, x
),
232 nir_fsub(b
, nir_imm_floatN_t(b
, M_PI_2f
, x
->bit_size
),
233 nir_fmul(b
, nir_fsqrt(b
, nir_fsub(b
, one
, abs_x
)),
238 vtn_nir_alu_op_for_spirv_glsl_opcode(struct vtn_builder
*b
,
239 enum GLSLstd450 opcode
,
240 unsigned execution_mode
)
243 case GLSLstd450Round
: return nir_op_fround_even
;
244 case GLSLstd450RoundEven
: return nir_op_fround_even
;
245 case GLSLstd450Trunc
: return nir_op_ftrunc
;
246 case GLSLstd450FAbs
: return nir_op_fabs
;
247 case GLSLstd450SAbs
: return nir_op_iabs
;
248 case GLSLstd450FSign
: return nir_op_fsign
;
249 case GLSLstd450SSign
: return nir_op_isign
;
250 case GLSLstd450Floor
: return nir_op_ffloor
;
251 case GLSLstd450Ceil
: return nir_op_fceil
;
252 case GLSLstd450Fract
: return nir_op_ffract
;
253 case GLSLstd450Sin
: return nir_op_fsin
;
254 case GLSLstd450Cos
: return nir_op_fcos
;
255 case GLSLstd450Pow
: return nir_op_fpow
;
256 case GLSLstd450Exp2
: return nir_op_fexp2
;
257 case GLSLstd450Log2
: return nir_op_flog2
;
258 case GLSLstd450Sqrt
: return nir_op_fsqrt
;
259 case GLSLstd450InverseSqrt
: return nir_op_frsq
;
260 case GLSLstd450NMin
: return nir_op_fmin
;
261 case GLSLstd450FMin
: return nir_op_fmin
;
262 case GLSLstd450UMin
: return nir_op_umin
;
263 case GLSLstd450SMin
: return nir_op_imin
;
264 case GLSLstd450NMax
: return nir_op_fmax
;
265 case GLSLstd450FMax
: return nir_op_fmax
;
266 case GLSLstd450UMax
: return nir_op_umax
;
267 case GLSLstd450SMax
: return nir_op_imax
;
268 case GLSLstd450FMix
: return nir_op_flrp
;
269 case GLSLstd450Fma
: return nir_op_ffma
;
270 case GLSLstd450Ldexp
: return nir_op_ldexp
;
271 case GLSLstd450FindILsb
: return nir_op_find_lsb
;
272 case GLSLstd450FindSMsb
: return nir_op_ifind_msb
;
273 case GLSLstd450FindUMsb
: return nir_op_ufind_msb
;
275 /* Packing/Unpacking functions */
276 case GLSLstd450PackSnorm4x8
: return nir_op_pack_snorm_4x8
;
277 case GLSLstd450PackUnorm4x8
: return nir_op_pack_unorm_4x8
;
278 case GLSLstd450PackSnorm2x16
: return nir_op_pack_snorm_2x16
;
279 case GLSLstd450PackUnorm2x16
: return nir_op_pack_unorm_2x16
;
280 case GLSLstd450PackHalf2x16
: return nir_op_pack_half_2x16
;
281 case GLSLstd450PackDouble2x32
: return nir_op_pack_64_2x32
;
282 case GLSLstd450UnpackSnorm4x8
: return nir_op_unpack_snorm_4x8
;
283 case GLSLstd450UnpackUnorm4x8
: return nir_op_unpack_unorm_4x8
;
284 case GLSLstd450UnpackSnorm2x16
: return nir_op_unpack_snorm_2x16
;
285 case GLSLstd450UnpackUnorm2x16
: return nir_op_unpack_unorm_2x16
;
286 case GLSLstd450UnpackHalf2x16
:
287 if (execution_mode
& FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
)
288 return nir_op_unpack_half_2x16_flush_to_zero
;
290 return nir_op_unpack_half_2x16
;
291 case GLSLstd450UnpackDouble2x32
: return nir_op_unpack_64_2x32
;
294 vtn_fail("No NIR equivalent");
298 #define NIR_IMM_FP(n, v) (nir_imm_floatN_t(n, v, src[0]->bit_size))
301 handle_glsl450_alu(struct vtn_builder
*b
, enum GLSLstd450 entrypoint
,
302 const uint32_t *w
, unsigned count
)
304 struct nir_builder
*nb
= &b
->nb
;
305 const struct glsl_type
*dest_type
=
306 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
308 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
309 val
->ssa
= vtn_create_ssa_value(b
, dest_type
);
311 /* Collect the various SSA sources */
312 unsigned num_inputs
= count
- 5;
313 nir_ssa_def
*src
[3] = { NULL
, };
314 for (unsigned i
= 0; i
< num_inputs
; i
++) {
315 /* These are handled specially below */
316 if (vtn_untyped_value(b
, w
[i
+ 5])->value_type
== vtn_value_type_pointer
)
319 src
[i
] = vtn_ssa_value(b
, w
[i
+ 5])->def
;
322 switch (entrypoint
) {
323 case GLSLstd450Radians
:
324 val
->ssa
->def
= nir_radians(nb
, src
[0]);
326 case GLSLstd450Degrees
:
327 val
->ssa
->def
= nir_degrees(nb
, src
[0]);
330 val
->ssa
->def
= nir_fdiv(nb
, nir_fsin(nb
, src
[0]),
331 nir_fcos(nb
, src
[0]));
334 case GLSLstd450Modf
: {
335 nir_ssa_def
*sign
= nir_fsign(nb
, src
[0]);
336 nir_ssa_def
*abs
= nir_fabs(nb
, src
[0]);
337 val
->ssa
->def
= nir_fmul(nb
, sign
, nir_ffract(nb
, abs
));
338 nir_store_deref(nb
, vtn_nir_deref(b
, w
[6]),
339 nir_fmul(nb
, sign
, nir_ffloor(nb
, abs
)), 0xf);
343 case GLSLstd450ModfStruct
: {
344 nir_ssa_def
*sign
= nir_fsign(nb
, src
[0]);
345 nir_ssa_def
*abs
= nir_fabs(nb
, src
[0]);
346 vtn_assert(glsl_type_is_struct_or_ifc(val
->ssa
->type
));
347 val
->ssa
->elems
[0]->def
= nir_fmul(nb
, sign
, nir_ffract(nb
, abs
));
348 val
->ssa
->elems
[1]->def
= nir_fmul(nb
, sign
, nir_ffloor(nb
, abs
));
353 val
->ssa
->def
= nir_sge(nb
, src
[1], src
[0]);
356 case GLSLstd450Length
:
357 val
->ssa
->def
= nir_fast_length(nb
, src
[0]);
359 case GLSLstd450Distance
:
360 val
->ssa
->def
= nir_fast_distance(nb
, src
[0], src
[1]);
362 case GLSLstd450Normalize
:
363 val
->ssa
->def
= nir_fast_normalize(nb
, src
[0]);
367 val
->ssa
->def
= build_exp(nb
, src
[0]);
371 val
->ssa
->def
= build_log(nb
, src
[0]);
374 case GLSLstd450FClamp
:
375 case GLSLstd450NClamp
:
376 val
->ssa
->def
= nir_fclamp(nb
, src
[0], src
[1], src
[2]);
378 case GLSLstd450UClamp
:
379 val
->ssa
->def
= nir_uclamp(nb
, src
[0], src
[1], src
[2]);
381 case GLSLstd450SClamp
:
382 val
->ssa
->def
= nir_iclamp(nb
, src
[0], src
[1], src
[2]);
385 case GLSLstd450Cross
: {
386 val
->ssa
->def
= nir_cross3(nb
, src
[0], src
[1]);
390 case GLSLstd450SmoothStep
: {
391 val
->ssa
->def
= nir_smoothstep(nb
, src
[0], src
[1], src
[2]);
395 case GLSLstd450FaceForward
:
397 nir_bcsel(nb
, nir_flt(nb
, nir_fdot(nb
, src
[2], src
[1]),
398 NIR_IMM_FP(nb
, 0.0)),
399 src
[0], nir_fneg(nb
, src
[0]));
402 case GLSLstd450Reflect
:
403 /* I - 2 * dot(N, I) * N */
405 nir_fsub(nb
, src
[0], nir_fmul(nb
, NIR_IMM_FP(nb
, 2.0),
406 nir_fmul(nb
, nir_fdot(nb
, src
[0], src
[1]),
410 case GLSLstd450Refract
: {
411 nir_ssa_def
*I
= src
[0];
412 nir_ssa_def
*N
= src
[1];
413 nir_ssa_def
*eta
= src
[2];
414 nir_ssa_def
*n_dot_i
= nir_fdot(nb
, N
, I
);
415 nir_ssa_def
*one
= NIR_IMM_FP(nb
, 1.0);
416 nir_ssa_def
*zero
= NIR_IMM_FP(nb
, 0.0);
417 /* According to the SPIR-V and GLSL specs, eta is always a float
418 * regardless of the type of the other operands. However in practice it
419 * seems that if you try to pass it a float then glslang will just
420 * promote it to a double and generate invalid SPIR-V. In order to
421 * support a hypothetical fixed version of glslang we’ll promote eta to
422 * double if the other operands are double also.
424 if (I
->bit_size
!= eta
->bit_size
) {
425 nir_op conversion_op
=
426 nir_type_conversion_op(nir_type_float
| eta
->bit_size
,
427 nir_type_float
| I
->bit_size
,
428 nir_rounding_mode_undef
);
429 eta
= nir_build_alu(nb
, conversion_op
, eta
, NULL
, NULL
, NULL
);
431 /* k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I)) */
433 nir_fsub(nb
, one
, nir_fmul(nb
, eta
, nir_fmul(nb
, eta
,
434 nir_fsub(nb
, one
, nir_fmul(nb
, n_dot_i
, n_dot_i
)))));
435 nir_ssa_def
*result
=
436 nir_fsub(nb
, nir_fmul(nb
, eta
, I
),
437 nir_fmul(nb
, nir_fadd(nb
, nir_fmul(nb
, eta
, n_dot_i
),
438 nir_fsqrt(nb
, k
)), N
));
439 /* XXX: bcsel, or if statement? */
440 val
->ssa
->def
= nir_bcsel(nb
, nir_flt(nb
, k
, zero
), zero
, result
);
445 /* 0.5 * (e^x - e^(-x)) */
447 nir_fmul_imm(nb
, nir_fsub(nb
, build_exp(nb
, src
[0]),
448 build_exp(nb
, nir_fneg(nb
, src
[0]))),
453 /* 0.5 * (e^x + e^(-x)) */
455 nir_fmul_imm(nb
, nir_fadd(nb
, build_exp(nb
, src
[0]),
456 build_exp(nb
, nir_fneg(nb
, src
[0]))),
460 case GLSLstd450Tanh
: {
461 /* tanh(x) := (0.5 * (e^x - e^(-x))) / (0.5 * (e^x + e^(-x)))
463 * With a little algebra this reduces to (e^2x - 1) / (e^2x + 1)
465 * We clamp x to (-inf, +10] to avoid precision problems. When x > 10,
466 * e^2x is so much larger than 1.0 that 1.0 gets flushed to zero in the
467 * computation e^2x +/- 1 so it can be ignored.
469 * For 16-bit precision we clamp x to (-inf, +4.2] since the maximum
470 * representable number is only 65,504 and e^(2*6) exceeds that. Also,
471 * if x > 4.2, tanh(x) will return 1.0 in fp16.
473 const uint32_t bit_size
= src
[0]->bit_size
;
474 const double clamped_x
= bit_size
> 16 ? 10.0 : 4.2;
475 nir_ssa_def
*x
= nir_fmin(nb
, src
[0],
476 nir_imm_floatN_t(nb
, clamped_x
, bit_size
));
477 nir_ssa_def
*exp2x
= build_exp(nb
, nir_fmul_imm(nb
, x
, 2.0));
478 val
->ssa
->def
= nir_fdiv(nb
, nir_fadd_imm(nb
, exp2x
, -1.0),
479 nir_fadd_imm(nb
, exp2x
, 1.0));
483 case GLSLstd450Asinh
:
484 val
->ssa
->def
= nir_fmul(nb
, nir_fsign(nb
, src
[0]),
485 build_log(nb
, nir_fadd(nb
, nir_fabs(nb
, src
[0]),
486 nir_fsqrt(nb
, nir_fadd_imm(nb
, nir_fmul(nb
, src
[0], src
[0]),
489 case GLSLstd450Acosh
:
490 val
->ssa
->def
= build_log(nb
, nir_fadd(nb
, src
[0],
491 nir_fsqrt(nb
, nir_fadd_imm(nb
, nir_fmul(nb
, src
[0], src
[0]),
494 case GLSLstd450Atanh
: {
495 nir_ssa_def
*one
= nir_imm_floatN_t(nb
, 1.0, src
[0]->bit_size
);
497 nir_fmul_imm(nb
, build_log(nb
, nir_fdiv(nb
, nir_fadd(nb
, src
[0], one
),
498 nir_fsub(nb
, one
, src
[0]))),
504 val
->ssa
->def
= build_asin(nb
, src
[0], 0.086566724, -0.03102955);
509 nir_fsub(nb
, nir_imm_floatN_t(nb
, M_PI_2f
, src
[0]->bit_size
),
510 build_asin(nb
, src
[0], 0.08132463, -0.02363318));
514 val
->ssa
->def
= nir_atan(nb
, src
[0]);
517 case GLSLstd450Atan2
:
518 val
->ssa
->def
= nir_atan2(nb
, src
[0], src
[1]);
521 case GLSLstd450Frexp
: {
522 nir_ssa_def
*exponent
= nir_frexp_exp(nb
, src
[0]);
523 val
->ssa
->def
= nir_frexp_sig(nb
, src
[0]);
524 nir_store_deref(nb
, vtn_nir_deref(b
, w
[6]), exponent
, 0xf);
528 case GLSLstd450FrexpStruct
: {
529 vtn_assert(glsl_type_is_struct_or_ifc(val
->ssa
->type
));
530 val
->ssa
->elems
[0]->def
= nir_frexp_sig(nb
, src
[0]);
531 val
->ssa
->elems
[1]->def
= nir_frexp_exp(nb
, src
[0]);
536 unsigned execution_mode
=
537 b
->shader
->info
.float_controls_execution_mode
;
539 nir_build_alu(&b
->nb
,
540 vtn_nir_alu_op_for_spirv_glsl_opcode(b
, entrypoint
, execution_mode
),
541 src
[0], src
[1], src
[2], NULL
);
548 handle_glsl450_interpolation(struct vtn_builder
*b
, enum GLSLstd450 opcode
,
549 const uint32_t *w
, unsigned count
)
551 const struct glsl_type
*dest_type
=
552 vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
554 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
555 val
->ssa
= vtn_create_ssa_value(b
, dest_type
);
559 case GLSLstd450InterpolateAtCentroid
:
560 op
= nir_intrinsic_interp_deref_at_centroid
;
562 case GLSLstd450InterpolateAtSample
:
563 op
= nir_intrinsic_interp_deref_at_sample
;
565 case GLSLstd450InterpolateAtOffset
:
566 op
= nir_intrinsic_interp_deref_at_offset
;
569 vtn_fail("Invalid opcode");
572 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
574 struct vtn_pointer
*ptr
=
575 vtn_value(b
, w
[5], vtn_value_type_pointer
)->pointer
;
576 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
578 /* If the value we are interpolating has an index into a vector then
579 * interpolate the vector and index the result of that instead. This is
580 * necessary because the index will get generated as a series of nir_bcsel
581 * instructions so it would no longer be an input variable.
583 const bool vec_array_deref
= deref
->deref_type
== nir_deref_type_array
&&
584 glsl_type_is_vector(nir_deref_instr_parent(deref
)->type
);
586 nir_deref_instr
*vec_deref
= NULL
;
587 if (vec_array_deref
) {
589 deref
= nir_deref_instr_parent(deref
);
591 intrin
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
594 case GLSLstd450InterpolateAtCentroid
:
596 case GLSLstd450InterpolateAtSample
:
597 case GLSLstd450InterpolateAtOffset
:
598 intrin
->src
[1] = nir_src_for_ssa(vtn_ssa_value(b
, w
[6])->def
);
601 vtn_fail("Invalid opcode");
604 intrin
->num_components
= glsl_get_vector_elements(deref
->type
);
605 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
606 glsl_get_vector_elements(deref
->type
),
607 glsl_get_bit_size(deref
->type
), NULL
);
609 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
611 if (vec_array_deref
) {
613 if (nir_src_is_const(vec_deref
->arr
.index
)) {
614 val
->ssa
->def
= vtn_vector_extract(b
, &intrin
->dest
.ssa
,
615 nir_src_as_uint(vec_deref
->arr
.index
));
617 val
->ssa
->def
= vtn_vector_extract_dynamic(b
, &intrin
->dest
.ssa
,
618 vec_deref
->arr
.index
.ssa
);
621 val
->ssa
->def
= &intrin
->dest
.ssa
;
626 vtn_handle_glsl450_instruction(struct vtn_builder
*b
, SpvOp ext_opcode
,
627 const uint32_t *w
, unsigned count
)
629 switch ((enum GLSLstd450
)ext_opcode
) {
630 case GLSLstd450Determinant
: {
631 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
632 val
->ssa
= rzalloc(b
, struct vtn_ssa_value
);
633 val
->ssa
->type
= vtn_value(b
, w
[1], vtn_value_type_type
)->type
->type
;
634 val
->ssa
->def
= build_mat_det(b
, vtn_ssa_value(b
, w
[5]));
638 case GLSLstd450MatrixInverse
: {
639 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_ssa
);
640 val
->ssa
= matrix_inverse(b
, vtn_ssa_value(b
, w
[5]));
644 case GLSLstd450InterpolateAtCentroid
:
645 case GLSLstd450InterpolateAtSample
:
646 case GLSLstd450InterpolateAtOffset
:
647 handle_glsl450_interpolation(b
, (enum GLSLstd450
)ext_opcode
, w
, count
);
651 handle_glsl450_alu(b
, (enum GLSLstd450
)ext_opcode
, w
, count
);