spirv: Call nir_builder directly for vector_extract
[mesa.git] / src / compiler / spirv / vtn_glsl450.c
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
39 static nir_ssa_def *
40 build_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
47 static nir_ssa_def *
48 build_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
69 static nir_ssa_def *
70 build_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
94 static nir_ssa_def *
95 build_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 */
115 static nir_ssa_def *
116 build_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
146 static struct vtn_ssa_value *
147 matrix_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 formula:
176 * asin~(x) = sign(x) * (pi/2 - sqrt(1 - |x|) * (pi/2 + |x|(pi/4 - 1 + |x|(p0 + |x|p1))))
177 *
178 * which is correct to first order at x=0 and x=±1 regardless of the p
179 * coefficients but can be made second-order correct at both ends by selecting
180 * the fit coefficients appropriately. Different p coefficients can be used
181 * in the asin and acos implementation to minimize some relative error metric
182 * in each case.
183 */
184 static nir_ssa_def *
185 build_asin(nir_builder *b, nir_ssa_def *x, float p0, float p1)
186 {
187 if (x->bit_size == 16) {
188 /* The polynomial approximation isn't precise enough to meet half-float
189 * precision requirements. Alternatively, we could implement this using
190 * the formula:
191 *
192 * asin(x) = atan2(x, sqrt(1 - x*x))
193 *
194 * But that is very expensive, so instead we just do the polynomial
195 * approximation in 32-bit math and then we convert the result back to
196 * 16-bit.
197 */
198 return nir_f2f16(b, build_asin(b, nir_f2f32(b, x), p0, p1));
199 }
200
201 nir_ssa_def *one = nir_imm_floatN_t(b, 1.0f, x->bit_size);
202 nir_ssa_def *abs_x = nir_fabs(b, x);
203
204 nir_ssa_def *p0_plus_xp1 = nir_fadd_imm(b, nir_fmul_imm(b, abs_x, p1), p0);
205
206 nir_ssa_def *expr_tail =
207 nir_fadd_imm(b, nir_fmul(b, abs_x,
208 nir_fadd_imm(b, nir_fmul(b, abs_x,
209 p0_plus_xp1),
210 M_PI_4f - 1.0f)),
211 M_PI_2f);
212
213 return nir_fmul(b, nir_fsign(b, x),
214 nir_fsub(b, nir_imm_floatN_t(b, M_PI_2f, x->bit_size),
215 nir_fmul(b, nir_fsqrt(b, nir_fsub(b, one, abs_x)),
216 expr_tail)));
217 }
218
219 static nir_op
220 vtn_nir_alu_op_for_spirv_glsl_opcode(struct vtn_builder *b,
221 enum GLSLstd450 opcode,
222 unsigned execution_mode)
223 {
224 switch (opcode) {
225 case GLSLstd450Round: return nir_op_fround_even;
226 case GLSLstd450RoundEven: return nir_op_fround_even;
227 case GLSLstd450Trunc: return nir_op_ftrunc;
228 case GLSLstd450FAbs: return nir_op_fabs;
229 case GLSLstd450SAbs: return nir_op_iabs;
230 case GLSLstd450FSign: return nir_op_fsign;
231 case GLSLstd450SSign: return nir_op_isign;
232 case GLSLstd450Floor: return nir_op_ffloor;
233 case GLSLstd450Ceil: return nir_op_fceil;
234 case GLSLstd450Fract: return nir_op_ffract;
235 case GLSLstd450Sin: return nir_op_fsin;
236 case GLSLstd450Cos: return nir_op_fcos;
237 case GLSLstd450Pow: return nir_op_fpow;
238 case GLSLstd450Exp2: return nir_op_fexp2;
239 case GLSLstd450Log2: return nir_op_flog2;
240 case GLSLstd450Sqrt: return nir_op_fsqrt;
241 case GLSLstd450InverseSqrt: return nir_op_frsq;
242 case GLSLstd450NMin: return nir_op_fmin;
243 case GLSLstd450FMin: return nir_op_fmin;
244 case GLSLstd450UMin: return nir_op_umin;
245 case GLSLstd450SMin: return nir_op_imin;
246 case GLSLstd450NMax: return nir_op_fmax;
247 case GLSLstd450FMax: return nir_op_fmax;
248 case GLSLstd450UMax: return nir_op_umax;
249 case GLSLstd450SMax: return nir_op_imax;
250 case GLSLstd450FMix: return nir_op_flrp;
251 case GLSLstd450Fma: return nir_op_ffma;
252 case GLSLstd450Ldexp: return nir_op_ldexp;
253 case GLSLstd450FindILsb: return nir_op_find_lsb;
254 case GLSLstd450FindSMsb: return nir_op_ifind_msb;
255 case GLSLstd450FindUMsb: return nir_op_ufind_msb;
256
257 /* Packing/Unpacking functions */
258 case GLSLstd450PackSnorm4x8: return nir_op_pack_snorm_4x8;
259 case GLSLstd450PackUnorm4x8: return nir_op_pack_unorm_4x8;
260 case GLSLstd450PackSnorm2x16: return nir_op_pack_snorm_2x16;
261 case GLSLstd450PackUnorm2x16: return nir_op_pack_unorm_2x16;
262 case GLSLstd450PackHalf2x16: return nir_op_pack_half_2x16;
263 case GLSLstd450PackDouble2x32: return nir_op_pack_64_2x32;
264 case GLSLstd450UnpackSnorm4x8: return nir_op_unpack_snorm_4x8;
265 case GLSLstd450UnpackUnorm4x8: return nir_op_unpack_unorm_4x8;
266 case GLSLstd450UnpackSnorm2x16: return nir_op_unpack_snorm_2x16;
267 case GLSLstd450UnpackUnorm2x16: return nir_op_unpack_unorm_2x16;
268 case GLSLstd450UnpackHalf2x16:
269 if (execution_mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16)
270 return nir_op_unpack_half_2x16_flush_to_zero;
271 else
272 return nir_op_unpack_half_2x16;
273 case GLSLstd450UnpackDouble2x32: return nir_op_unpack_64_2x32;
274
275 default:
276 vtn_fail("No NIR equivalent");
277 }
278 }
279
280 #define NIR_IMM_FP(n, v) (nir_imm_floatN_t(n, v, src[0]->bit_size))
281
282 static void
283 handle_glsl450_alu(struct vtn_builder *b, enum GLSLstd450 entrypoint,
284 const uint32_t *w, unsigned count)
285 {
286 struct nir_builder *nb = &b->nb;
287 const struct glsl_type *dest_type =
288 vtn_value(b, w[1], vtn_value_type_type)->type->type;
289
290 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
291 val->ssa = vtn_create_ssa_value(b, dest_type);
292
293 /* Collect the various SSA sources */
294 unsigned num_inputs = count - 5;
295 nir_ssa_def *src[3] = { NULL, };
296 for (unsigned i = 0; i < num_inputs; i++) {
297 /* These are handled specially below */
298 if (vtn_untyped_value(b, w[i + 5])->value_type == vtn_value_type_pointer)
299 continue;
300
301 src[i] = vtn_ssa_value(b, w[i + 5])->def;
302 }
303
304 switch (entrypoint) {
305 case GLSLstd450Radians:
306 val->ssa->def = nir_radians(nb, src[0]);
307 return;
308 case GLSLstd450Degrees:
309 val->ssa->def = nir_degrees(nb, src[0]);
310 return;
311 case GLSLstd450Tan:
312 val->ssa->def = nir_fdiv(nb, nir_fsin(nb, src[0]),
313 nir_fcos(nb, src[0]));
314 return;
315
316 case GLSLstd450Modf: {
317 nir_ssa_def *sign = nir_fsign(nb, src[0]);
318 nir_ssa_def *abs = nir_fabs(nb, src[0]);
319 val->ssa->def = nir_fmul(nb, sign, nir_ffract(nb, abs));
320 nir_store_deref(nb, vtn_nir_deref(b, w[6]),
321 nir_fmul(nb, sign, nir_ffloor(nb, abs)), 0xf);
322 return;
323 }
324
325 case GLSLstd450ModfStruct: {
326 nir_ssa_def *sign = nir_fsign(nb, src[0]);
327 nir_ssa_def *abs = nir_fabs(nb, src[0]);
328 vtn_assert(glsl_type_is_struct_or_ifc(val->ssa->type));
329 val->ssa->elems[0]->def = nir_fmul(nb, sign, nir_ffract(nb, abs));
330 val->ssa->elems[1]->def = nir_fmul(nb, sign, nir_ffloor(nb, abs));
331 return;
332 }
333
334 case GLSLstd450Step:
335 val->ssa->def = nir_sge(nb, src[1], src[0]);
336 return;
337
338 case GLSLstd450Length:
339 val->ssa->def = nir_fast_length(nb, src[0]);
340 return;
341 case GLSLstd450Distance:
342 val->ssa->def = nir_fast_distance(nb, src[0], src[1]);
343 return;
344 case GLSLstd450Normalize:
345 val->ssa->def = nir_fast_normalize(nb, src[0]);
346 return;
347
348 case GLSLstd450Exp:
349 val->ssa->def = nir_fexp(nb, src[0]);
350 return;
351
352 case GLSLstd450Log:
353 val->ssa->def = nir_flog(nb, src[0]);
354 return;
355
356 case GLSLstd450FClamp:
357 case GLSLstd450NClamp:
358 val->ssa->def = nir_fclamp(nb, src[0], src[1], src[2]);
359 return;
360 case GLSLstd450UClamp:
361 val->ssa->def = nir_uclamp(nb, src[0], src[1], src[2]);
362 return;
363 case GLSLstd450SClamp:
364 val->ssa->def = nir_iclamp(nb, src[0], src[1], src[2]);
365 return;
366
367 case GLSLstd450Cross: {
368 val->ssa->def = nir_cross3(nb, src[0], src[1]);
369 return;
370 }
371
372 case GLSLstd450SmoothStep: {
373 val->ssa->def = nir_smoothstep(nb, src[0], src[1], src[2]);
374 return;
375 }
376
377 case GLSLstd450FaceForward:
378 val->ssa->def =
379 nir_bcsel(nb, nir_flt(nb, nir_fdot(nb, src[2], src[1]),
380 NIR_IMM_FP(nb, 0.0)),
381 src[0], nir_fneg(nb, src[0]));
382 return;
383
384 case GLSLstd450Reflect:
385 /* I - 2 * dot(N, I) * N */
386 val->ssa->def =
387 nir_fsub(nb, src[0], nir_fmul(nb, NIR_IMM_FP(nb, 2.0),
388 nir_fmul(nb, nir_fdot(nb, src[0], src[1]),
389 src[1])));
390 return;
391
392 case GLSLstd450Refract: {
393 nir_ssa_def *I = src[0];
394 nir_ssa_def *N = src[1];
395 nir_ssa_def *eta = src[2];
396 nir_ssa_def *n_dot_i = nir_fdot(nb, N, I);
397 nir_ssa_def *one = NIR_IMM_FP(nb, 1.0);
398 nir_ssa_def *zero = NIR_IMM_FP(nb, 0.0);
399 /* According to the SPIR-V and GLSL specs, eta is always a float
400 * regardless of the type of the other operands. However in practice it
401 * seems that if you try to pass it a float then glslang will just
402 * promote it to a double and generate invalid SPIR-V. In order to
403 * support a hypothetical fixed version of glslang we’ll promote eta to
404 * double if the other operands are double also.
405 */
406 if (I->bit_size != eta->bit_size) {
407 nir_op conversion_op =
408 nir_type_conversion_op(nir_type_float | eta->bit_size,
409 nir_type_float | I->bit_size,
410 nir_rounding_mode_undef);
411 eta = nir_build_alu(nb, conversion_op, eta, NULL, NULL, NULL);
412 }
413 /* k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I)) */
414 nir_ssa_def *k =
415 nir_fsub(nb, one, nir_fmul(nb, eta, nir_fmul(nb, eta,
416 nir_fsub(nb, one, nir_fmul(nb, n_dot_i, n_dot_i)))));
417 nir_ssa_def *result =
418 nir_fsub(nb, nir_fmul(nb, eta, I),
419 nir_fmul(nb, nir_fadd(nb, nir_fmul(nb, eta, n_dot_i),
420 nir_fsqrt(nb, k)), N));
421 /* XXX: bcsel, or if statement? */
422 val->ssa->def = nir_bcsel(nb, nir_flt(nb, k, zero), zero, result);
423 return;
424 }
425
426 case GLSLstd450Sinh:
427 /* 0.5 * (e^x - e^(-x)) */
428 val->ssa->def =
429 nir_fmul_imm(nb, nir_fsub(nb, nir_fexp(nb, src[0]),
430 nir_fexp(nb, nir_fneg(nb, src[0]))),
431 0.5f);
432 return;
433
434 case GLSLstd450Cosh:
435 /* 0.5 * (e^x + e^(-x)) */
436 val->ssa->def =
437 nir_fmul_imm(nb, nir_fadd(nb, nir_fexp(nb, src[0]),
438 nir_fexp(nb, nir_fneg(nb, src[0]))),
439 0.5f);
440 return;
441
442 case GLSLstd450Tanh: {
443 /* tanh(x) := (e^x - e^(-x)) / (e^x + e^(-x))
444 *
445 * We clamp x to [-10, +10] to avoid precision problems. When x > 10,
446 * e^x dominates the sum, e^(-x) is lost and tanh(x) is 1.0 for 32 bit
447 * floating point.
448 *
449 * For 16-bit precision this we clamp x to [-4.2, +4.2].
450 */
451 const uint32_t bit_size = src[0]->bit_size;
452 const double clamped_x = bit_size > 16 ? 10.0 : 4.2;
453 nir_ssa_def *x = nir_fclamp(nb, src[0],
454 nir_imm_floatN_t(nb, -clamped_x, bit_size),
455 nir_imm_floatN_t(nb, clamped_x, bit_size));
456 val->ssa->def =
457 nir_fdiv(nb, nir_fsub(nb, nir_fexp(nb, x),
458 nir_fexp(nb, nir_fneg(nb, x))),
459 nir_fadd(nb, nir_fexp(nb, x),
460 nir_fexp(nb, nir_fneg(nb, x))));
461 return;
462 }
463
464 case GLSLstd450Asinh:
465 val->ssa->def = nir_fmul(nb, nir_fsign(nb, src[0]),
466 nir_flog(nb, nir_fadd(nb, nir_fabs(nb, src[0]),
467 nir_fsqrt(nb, nir_fadd_imm(nb, nir_fmul(nb, src[0], src[0]),
468 1.0f)))));
469 return;
470 case GLSLstd450Acosh:
471 val->ssa->def = nir_flog(nb, nir_fadd(nb, src[0],
472 nir_fsqrt(nb, nir_fadd_imm(nb, nir_fmul(nb, src[0], src[0]),
473 -1.0f))));
474 return;
475 case GLSLstd450Atanh: {
476 nir_ssa_def *one = nir_imm_floatN_t(nb, 1.0, src[0]->bit_size);
477 val->ssa->def =
478 nir_fmul_imm(nb, nir_flog(nb, nir_fdiv(nb, nir_fadd(nb, src[0], one),
479 nir_fsub(nb, one, src[0]))),
480 0.5f);
481 return;
482 }
483
484 case GLSLstd450Asin:
485 val->ssa->def = build_asin(nb, src[0], 0.086566724, -0.03102955);
486 return;
487
488 case GLSLstd450Acos:
489 val->ssa->def =
490 nir_fsub(nb, nir_imm_floatN_t(nb, M_PI_2f, src[0]->bit_size),
491 build_asin(nb, src[0], 0.08132463, -0.02363318));
492 return;
493
494 case GLSLstd450Atan:
495 val->ssa->def = nir_atan(nb, src[0]);
496 return;
497
498 case GLSLstd450Atan2:
499 val->ssa->def = nir_atan2(nb, src[0], src[1]);
500 return;
501
502 case GLSLstd450Frexp: {
503 nir_ssa_def *exponent = nir_frexp_exp(nb, src[0]);
504 val->ssa->def = nir_frexp_sig(nb, src[0]);
505 nir_store_deref(nb, vtn_nir_deref(b, w[6]), exponent, 0xf);
506 return;
507 }
508
509 case GLSLstd450FrexpStruct: {
510 vtn_assert(glsl_type_is_struct_or_ifc(val->ssa->type));
511 val->ssa->elems[0]->def = nir_frexp_sig(nb, src[0]);
512 val->ssa->elems[1]->def = nir_frexp_exp(nb, src[0]);
513 return;
514 }
515
516 default: {
517 unsigned execution_mode =
518 b->shader->info.float_controls_execution_mode;
519 val->ssa->def =
520 nir_build_alu(&b->nb,
521 vtn_nir_alu_op_for_spirv_glsl_opcode(b, entrypoint, execution_mode),
522 src[0], src[1], src[2], NULL);
523 return;
524 }
525 }
526 }
527
528 static void
529 handle_glsl450_interpolation(struct vtn_builder *b, enum GLSLstd450 opcode,
530 const uint32_t *w, unsigned count)
531 {
532 const struct glsl_type *dest_type =
533 vtn_value(b, w[1], vtn_value_type_type)->type->type;
534
535 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
536 val->ssa = vtn_create_ssa_value(b, dest_type);
537
538 nir_intrinsic_op op;
539 switch (opcode) {
540 case GLSLstd450InterpolateAtCentroid:
541 op = nir_intrinsic_interp_deref_at_centroid;
542 break;
543 case GLSLstd450InterpolateAtSample:
544 op = nir_intrinsic_interp_deref_at_sample;
545 break;
546 case GLSLstd450InterpolateAtOffset:
547 op = nir_intrinsic_interp_deref_at_offset;
548 break;
549 default:
550 vtn_fail("Invalid opcode");
551 }
552
553 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->nb.shader, op);
554
555 struct vtn_pointer *ptr =
556 vtn_value(b, w[5], vtn_value_type_pointer)->pointer;
557 nir_deref_instr *deref = vtn_pointer_to_deref(b, ptr);
558
559 /* If the value we are interpolating has an index into a vector then
560 * interpolate the vector and index the result of that instead. This is
561 * necessary because the index will get generated as a series of nir_bcsel
562 * instructions so it would no longer be an input variable.
563 */
564 const bool vec_array_deref = deref->deref_type == nir_deref_type_array &&
565 glsl_type_is_vector(nir_deref_instr_parent(deref)->type);
566
567 nir_deref_instr *vec_deref = NULL;
568 if (vec_array_deref) {
569 vec_deref = deref;
570 deref = nir_deref_instr_parent(deref);
571 }
572 intrin->src[0] = nir_src_for_ssa(&deref->dest.ssa);
573
574 switch (opcode) {
575 case GLSLstd450InterpolateAtCentroid:
576 break;
577 case GLSLstd450InterpolateAtSample:
578 case GLSLstd450InterpolateAtOffset:
579 intrin->src[1] = nir_src_for_ssa(vtn_ssa_value(b, w[6])->def);
580 break;
581 default:
582 vtn_fail("Invalid opcode");
583 }
584
585 intrin->num_components = glsl_get_vector_elements(deref->type);
586 nir_ssa_dest_init(&intrin->instr, &intrin->dest,
587 glsl_get_vector_elements(deref->type),
588 glsl_get_bit_size(deref->type), NULL);
589
590 nir_builder_instr_insert(&b->nb, &intrin->instr);
591
592 if (vec_array_deref) {
593 assert(vec_deref);
594 val->ssa->def = nir_vector_extract(&b->nb, &intrin->dest.ssa,
595 vec_deref->arr.index.ssa);
596 } else {
597 val->ssa->def = &intrin->dest.ssa;
598 }
599 }
600
601 bool
602 vtn_handle_glsl450_instruction(struct vtn_builder *b, SpvOp ext_opcode,
603 const uint32_t *w, unsigned count)
604 {
605 switch ((enum GLSLstd450)ext_opcode) {
606 case GLSLstd450Determinant: {
607 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
608 val->ssa = rzalloc(b, struct vtn_ssa_value);
609 val->ssa->type = vtn_value(b, w[1], vtn_value_type_type)->type->type;
610 val->ssa->def = build_mat_det(b, vtn_ssa_value(b, w[5]));
611 break;
612 }
613
614 case GLSLstd450MatrixInverse: {
615 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
616 val->ssa = matrix_inverse(b, vtn_ssa_value(b, w[5]));
617 break;
618 }
619
620 case GLSLstd450InterpolateAtCentroid:
621 case GLSLstd450InterpolateAtSample:
622 case GLSLstd450InterpolateAtOffset:
623 handle_glsl450_interpolation(b, (enum GLSLstd450)ext_opcode, w, count);
624 break;
625
626 default:
627 handle_glsl450_alu(b, (enum GLSLstd450)ext_opcode, w, count);
628 }
629
630 return true;
631 }