blob: ec159fd357c2bfb5762b2df209c443f3f48fb82d [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman89401822014-05-06 15:04:28 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
John Bauman89401822014-05-06 15:04:28 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
John Bauman89401822014-05-06 15:04:28 -04008//
Nicolas Capens0bac2852016-05-07 06:09:58 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
John Bauman89401822014-05-06 15:04:28 -040014
15#include "ShaderCore.hpp"
16
John Bauman19bac1e2014-05-06 15:23:49 -040017#include "Renderer/Renderer.hpp"
18#include "Common/Debug.hpp"
John Bauman89401822014-05-06 15:04:28 -040019
Alexis Hetud5c31da2015-08-28 14:39:13 -040020#include <limits.h>
21
John Bauman89401822014-05-06 15:04:28 -040022namespace sw
23{
John Bauman19bac1e2014-05-06 15:23:49 -040024 extern TranscendentalPrecision logPrecision;
25 extern TranscendentalPrecision expPrecision;
26 extern TranscendentalPrecision rcpPrecision;
27 extern TranscendentalPrecision rsqPrecision;
28
Alexis Hetu96517182015-04-15 10:30:23 -040029 Vector4s::Vector4s()
John Bauman19bac1e2014-05-06 15:23:49 -040030 {
31 }
32
Alexis Hetu96517182015-04-15 10:30:23 -040033 Vector4s::Vector4s(unsigned short x, unsigned short y, unsigned short z, unsigned short w)
John Bauman19bac1e2014-05-06 15:23:49 -040034 {
35 this->x = Short4(x);
36 this->y = Short4(y);
37 this->z = Short4(z);
38 this->w = Short4(w);
39 }
40
Alexis Hetu96517182015-04-15 10:30:23 -040041 Vector4s::Vector4s(const Vector4s &rhs)
John Bauman19bac1e2014-05-06 15:23:49 -040042 {
43 x = rhs.x;
44 y = rhs.y;
45 z = rhs.z;
46 w = rhs.w;
47 }
48
Alexis Hetu96517182015-04-15 10:30:23 -040049 Vector4s &Vector4s::operator=(const Vector4s &rhs)
John Bauman19bac1e2014-05-06 15:23:49 -040050 {
51 x = rhs.x;
52 y = rhs.y;
53 z = rhs.z;
54 w = rhs.w;
55
56 return *this;
57 }
58
Alexis Hetu96517182015-04-15 10:30:23 -040059 Short4 &Vector4s::operator[](int i)
John Bauman19bac1e2014-05-06 15:23:49 -040060 {
61 switch(i)
62 {
63 case 0: return x;
64 case 1: return y;
65 case 2: return z;
66 case 3: return w;
67 }
68
69 return x;
70 }
71
72 Vector4f::Vector4f()
73 {
74 }
75
76 Vector4f::Vector4f(float x, float y, float z, float w)
77 {
78 this->x = Float4(x);
79 this->y = Float4(y);
80 this->z = Float4(z);
81 this->w = Float4(w);
82 }
83
84 Vector4f::Vector4f(const Vector4f &rhs)
85 {
86 x = rhs.x;
87 y = rhs.y;
88 z = rhs.z;
89 w = rhs.w;
90 }
91
92 Vector4f &Vector4f::operator=(const Vector4f &rhs)
93 {
94 x = rhs.x;
95 y = rhs.y;
96 z = rhs.z;
97 w = rhs.w;
98
99 return *this;
100 }
101
102 Float4 &Vector4f::operator[](int i)
103 {
104 switch(i)
105 {
106 case 0: return x;
107 case 1: return y;
108 case 2: return z;
109 case 3: return w;
110 }
111
112 return x;
113 }
114
115 Float4 exponential2(RValue<Float4> x, bool pp)
116 {
117 Float4 x0;
118 Float4 x1;
119 Int4 x2;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400120
John Bauman19bac1e2014-05-06 15:23:49 -0400121 x0 = x;
122
123 x0 = Min(x0, As<Float4>(Int4(0x43010000))); // 129.00000e+0f
124 x0 = Max(x0, As<Float4>(Int4(0xC2FDFFFF))); // -126.99999e+0f
125 x1 = x0;
126 x1 -= Float4(0.5f);
127 x2 = RoundInt(x1);
128 x1 = Float4(x2);
129 x2 += Int4(0x0000007F); // 127
130 x2 = x2 << 23;
131 x0 -= x1;
132 x1 = As<Float4>(Int4(0x3AF61905)); // 1.8775767e-3f
133 x1 *= x0;
134 x1 += As<Float4>(Int4(0x3C134806)); // 8.9893397e-3f
135 x1 *= x0;
136 x1 += As<Float4>(Int4(0x3D64AA23)); // 5.5826318e-2f
137 x1 *= x0;
138 x1 += As<Float4>(Int4(0x3E75EAD4)); // 2.4015361e-1f
139 x1 *= x0;
140 x1 += As<Float4>(Int4(0x3F31727B)); // 6.9315308e-1f
141 x1 *= x0;
142 x1 += As<Float4>(Int4(0x3F7FFFFF)); // 9.9999994e-1f
143 x1 *= As<Float4>(x2);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400144
John Bauman19bac1e2014-05-06 15:23:49 -0400145 return x1;
146 }
147
148 Float4 logarithm2(RValue<Float4> x, bool absolute, bool pp)
149 {
150 Float4 x0;
151 Float4 x1;
152 Float4 x2;
153 Float4 x3;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400154
John Bauman19bac1e2014-05-06 15:23:49 -0400155 x0 = x;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400156
John Bauman19bac1e2014-05-06 15:23:49 -0400157 x1 = As<Float4>(As<Int4>(x0) & Int4(0x7F800000));
158 x1 = As<Float4>(As<UInt4>(x1) >> 8);
159 x1 = As<Float4>(As<Int4>(x1) | As<Int4>(Float4(1.0f)));
160 x1 = (x1 - Float4(1.4960938f)) * Float4(256.0f); // FIXME: (x1 - 1.4960938f) * 256.0f;
161 x0 = As<Float4>((As<Int4>(x0) & Int4(0x007FFFFF)) | As<Int4>(Float4(1.0f)));
162
163 x2 = (Float4(9.5428179e-2f) * x0 + Float4(4.7779095e-1f)) * x0 + Float4(1.9782813e-1f);
164 x3 = ((Float4(1.6618466e-2f) * x0 + Float4(2.0350508e-1f)) * x0 + Float4(2.7382900e-1f)) * x0 + Float4(4.0496687e-2f);
165 x2 /= x3;
166
167 x1 += (x0 - Float4(1.0f)) * x2;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400168
Alexis Hetu0b7003b2017-11-13 16:21:11 -0500169 Int4 pos_inf_x = CmpEQ(As<Int4>(x), Int4(0x7F800000));
170 return As<Float4>((pos_inf_x & As<Int4>(x)) | (~pos_inf_x & As<Int4>(x1)));
John Bauman19bac1e2014-05-06 15:23:49 -0400171 }
172
173 Float4 exponential(RValue<Float4> x, bool pp)
174 {
175 // FIXME: Propagate the constant
Alexis Hetu0b7003b2017-11-13 16:21:11 -0500176 return exponential2(Float4(1.44269504f) * x, pp); // 1/ln(2)
John Bauman19bac1e2014-05-06 15:23:49 -0400177 }
178
179 Float4 logarithm(RValue<Float4> x, bool absolute, bool pp)
180 {
181 // FIXME: Propagate the constant
182 return Float4(6.93147181e-1f) * logarithm2(x, absolute, pp); // ln(2)
183 }
184
185 Float4 power(RValue<Float4> x, RValue<Float4> y, bool pp)
186 {
187 Float4 log = logarithm2(x, true, pp);
188 log *= y;
189 return exponential2(log, pp);
190 }
191
Nicolas Capens05b3d662016-02-25 23:58:33 -0500192 Float4 reciprocal(RValue<Float4> x, bool pp, bool finite, bool exactAtPow2)
John Bauman19bac1e2014-05-06 15:23:49 -0400193 {
194 Float4 rcp;
195
196 if(!pp && rcpPrecision >= WHQL)
197 {
198 rcp = Float4(1.0f) / x;
199 }
200 else
201 {
Nicolas Capens05b3d662016-02-25 23:58:33 -0500202 rcp = Rcp_pp(x, exactAtPow2);
John Bauman19bac1e2014-05-06 15:23:49 -0400203
204 if(!pp)
205 {
206 rcp = (rcp + rcp) - (x * rcp * rcp);
207 }
208 }
209
210 if(finite)
211 {
212 int big = 0x7F7FFFFF;
213 rcp = Min(rcp, Float4((float&)big));
214 }
215
216 return rcp;
217 }
218
219 Float4 reciprocalSquareRoot(RValue<Float4> x, bool absolute, bool pp)
220 {
221 Float4 abs = x;
222
223 if(absolute)
224 {
225 abs = Abs(abs);
226 }
227
228 Float4 rsq;
229
Alexis Hetua0ef97a2017-11-13 17:31:20 -0500230 if(!pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400231 {
232 rsq = Float4(1.0f) / Sqrt(abs);
233 }
234 else
235 {
236 rsq = RcpSqrt_pp(abs);
237
238 if(!pp)
239 {
240 rsq = rsq * (Float4(3.0f) - rsq * rsq * abs) * Float4(0.5f);
241 }
John Bauman19bac1e2014-05-06 15:23:49 -0400242
Alexis Hetua0ef97a2017-11-13 17:31:20 -0500243 rsq = As<Float4>(CmpNEQ(As<Int4>(abs), Int4(0x7F800000)) & As<Int4>(rsq));
244 }
John Bauman19bac1e2014-05-06 15:23:49 -0400245
246 return rsq;
247 }
248
249 Float4 modulo(RValue<Float4> x, RValue<Float4> y)
250 {
251 return x - y * Floor(x / y);
252 }
253
254 Float4 sine_pi(RValue<Float4> x, bool pp)
255 {
256 const Float4 A = Float4(-4.05284734e-1f); // -4/pi^2
257 const Float4 B = Float4(1.27323954e+0f); // 4/pi
258 const Float4 C = Float4(7.75160950e-1f);
259 const Float4 D = Float4(2.24839049e-1f);
260
261 // Parabola approximating sine
262 Float4 sin = x * (Abs(x) * A + B);
263
264 // Improve precision from 0.06 to 0.001
265 if(true)
266 {
267 sin = sin * (Abs(sin) * D + C);
268 }
269
270 return sin;
271 }
272
273 Float4 cosine_pi(RValue<Float4> x, bool pp)
274 {
275 // cos(x) = sin(x + pi/2)
276 Float4 y = x + Float4(1.57079632e+0f);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400277
John Bauman19bac1e2014-05-06 15:23:49 -0400278 // Wrap around
279 y -= As<Float4>(CmpNLT(y, Float4(3.14159265e+0f)) & As<Int4>(Float4(6.28318530e+0f)));
280
281 return sine_pi(y, pp);
282 }
283
284 Float4 sine(RValue<Float4> x, bool pp)
285 {
286 // Reduce to [-0.5, 0.5] range
287 Float4 y = x * Float4(1.59154943e-1f); // 1/2pi
288 y = y - Round(y);
289
Alexis Hetu929c6b02017-11-07 16:04:25 -0500290 if(!pp)
291 {
292 // From the paper: "A Fast, Vectorizable Algorithm for Producing Single-Precision Sine-Cosine Pairs"
293 // This implementation passes OpenGL ES 3.0 precision requirements, at the cost of more operations:
294 // !pp : 17 mul, 7 add, 1 sub, 1 reciprocal
295 // pp : 4 mul, 2 add, 2 abs
296
297 Float4 y2 = y * y;
298 Float4 c1 = y2 * (y2 * (y2 * Float4(-0.0204391631f) + Float4(0.2536086171f)) + Float4(-1.2336977925f)) + Float4(1.0f);
299 Float4 s1 = y * (y2 * (y2 * (y2 * Float4(-0.0046075748f) + Float4(0.0796819754f)) + Float4(-0.645963615f)) + Float4(1.5707963235f));
300 Float4 c2 = (c1 * c1) - (s1 * s1);
301 Float4 s2 = Float4(2.0f) * s1 * c1;
302 return Float4(2.0f) * s2 * c2 * reciprocal(s2 * s2 + c2 * c2, pp, true);
303 }
304
John Bauman19bac1e2014-05-06 15:23:49 -0400305 const Float4 A = Float4(-16.0f);
306 const Float4 B = Float4(8.0f);
307 const Float4 C = Float4(7.75160950e-1f);
308 const Float4 D = Float4(2.24839049e-1f);
309
310 // Parabola approximating sine
311 Float4 sin = y * (Abs(y) * A + B);
312
313 // Improve precision from 0.06 to 0.001
314 if(true)
315 {
316 sin = sin * (Abs(sin) * D + C);
317 }
318
319 return sin;
320 }
321
322 Float4 cosine(RValue<Float4> x, bool pp)
323 {
324 // cos(x) = sin(x + pi/2)
325 Float4 y = x + Float4(1.57079632e+0f);
326 return sine(y, pp);
327 }
328
329 Float4 tangent(RValue<Float4> x, bool pp)
330 {
331 return sine(x, pp) / cosine(x, pp);
332 }
333
334 Float4 arccos(RValue<Float4> x, bool pp)
335 {
336 // pi/2 - arcsin(x)
337 return Float4(1.57079632e+0f) - arcsin(x);
338 }
339
340 Float4 arcsin(RValue<Float4> x, bool pp)
341 {
Alexis Hetu1728dde2017-11-08 13:43:16 -0500342 if(false) // Simpler implementation fails even lowp precision tests
343 {
344 // x*(pi/2-sqrt(1-x*x)*pi/5)
345 return x * (Float4(1.57079632e+0f) - Sqrt(Float4(1.0f) - x*x) * Float4(6.28318531e-1f));
346 }
347 else
348 {
349 // From 4.4.45, page 81 of the Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun
350 const Float4 half_pi(1.57079632f);
351 const Float4 a0(1.5707288f);
352 const Float4 a1(-0.2121144f);
353 const Float4 a2(0.0742610f);
354 const Float4 a3(-0.0187293f);
355 Float4 absx = Abs(x);
356 return As<Float4>(As<Int4>(half_pi - Sqrt(Float4(1.0f) - absx) * (a0 + absx * (a1 + absx * (a2 + absx * a3)))) ^
357 (As<Int4>(x) & Int4(0x80000000)));
358 }
359 }
360
361 // Approximation of atan in [0..1]
362 Float4 arctan_01(Float4 x, bool pp)
363 {
364 if(pp)
365 {
366 return x * (Float4(-0.27f) * x + Float4(1.05539816f));
367 }
368 else
369 {
370 // From 4.4.49, page 81 of the Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun
371 const Float4 a2(-0.3333314528f);
372 const Float4 a4(0.1999355085f);
373 const Float4 a6(-0.1420889944f);
374 const Float4 a8(0.1065626393f);
375 const Float4 a10(-0.0752896400f);
376 const Float4 a12(0.0429096138f);
377 const Float4 a14(-0.0161657367f);
378 const Float4 a16(0.0028662257f);
379 Float4 x2 = x * x;
380 return (x + x * (x2 * (a2 + x2 * (a4 + x2 * (a6 + x2 * (a8 + x2 * (a10 + x2 * (a12 + x2 * (a14 + x2 * a16)))))))));
381 }
John Bauman19bac1e2014-05-06 15:23:49 -0400382 }
383
384 Float4 arctan(RValue<Float4> x, bool pp)
385 {
Alexis Hetu1728dde2017-11-08 13:43:16 -0500386 Float4 absx = Abs(x);
387 Int4 O = CmpNLT(absx, Float4(1.0f));
388 Float4 y = As<Float4>((O & As<Int4>(Float4(1.0f) / absx)) | (~O & As<Int4>(absx))); // FIXME: Vector select
John Bauman19bac1e2014-05-06 15:23:49 -0400389
Alexis Hetu1728dde2017-11-08 13:43:16 -0500390 const Float4 half_pi(1.57079632f);
391 Float4 theta = arctan_01(y, pp);
392 return As<Float4>(((O & As<Int4>(half_pi - theta)) | (~O & As<Int4>(theta))) ^ // FIXME: Vector select
393 (As<Int4>(x) & Int4(0x80000000)));
John Bauman19bac1e2014-05-06 15:23:49 -0400394 }
395
396 Float4 arctan(RValue<Float4> y, RValue<Float4> x, bool pp)
397 {
Alexis Hetu1728dde2017-11-08 13:43:16 -0500398 const Float4 pi(3.14159265f); // pi
399 const Float4 minus_pi(-3.14159265f); // -pi
400 const Float4 half_pi(1.57079632f); // pi/2
401 const Float4 quarter_pi(7.85398163e-1f); // pi/4
402
John Bauman19bac1e2014-05-06 15:23:49 -0400403 // Rotate to upper semicircle when in lower semicircle
404 Int4 S = CmpLT(y, Float4(0.0f));
Alexis Hetu1728dde2017-11-08 13:43:16 -0500405 Float4 theta = As<Float4>(S & As<Int4>(minus_pi));
John Bauman19bac1e2014-05-06 15:23:49 -0400406 Float4 x0 = As<Float4>((As<Int4>(y) & Int4(0x80000000)) ^ As<Int4>(x));
407 Float4 y0 = Abs(y);
408
409 // Rotate to right quadrant when in left quadrant
Alexis Hetu1728dde2017-11-08 13:43:16 -0500410 Int4 non_zero_y = CmpNEQ(y0, Float4(0.0f));
411 Int4 Q = CmpLT(x0, Float4(0.0f)) & non_zero_y;
412 theta += As<Float4>(Q & As<Int4>(half_pi));
413 Float4 x1 = As<Float4>((Q & As<Int4>(y0)) | (~Q & As<Int4>(x0))); // FIXME: Vector select
414 Float4 y1 = As<Float4>((Q & As<Int4>(-x0)) | (~Q & As<Int4>(y0))); // FIXME: Vector select
John Bauman19bac1e2014-05-06 15:23:49 -0400415
Alexis Hetu1728dde2017-11-08 13:43:16 -0500416 // Mirror to first octant when in second octant
417 Int4 O = CmpNLT(y1, x1) & non_zero_y;
418 Float4 x2 = As<Float4>((O & As<Int4>(y1)) | (~O & As<Int4>(x1))); // FIXME: Vector select
419 Float4 y2 = As<Float4>((O & As<Int4>(x1)) | (~O & As<Int4>(y1))); // FIXME: Vector select
John Bauman19bac1e2014-05-06 15:23:49 -0400420
421 // Approximation of atan in [0..1]
Alexis Hetu1728dde2017-11-08 13:43:16 -0500422 Int4 zero_x = CmpEQ(x2, Float4(0.0f));
423 Int4 inf_y = IsInf(y2); // Since x2 >= y2, this means x2 == y2 == inf, so we use 45 degrees or pi/4
424 Float4 atan2_theta = arctan_01(y2 / x2, pp);
425 theta += As<Float4>((~zero_x & ~inf_y & non_zero_y & ((O & As<Int4>(half_pi - atan2_theta)) | (~O & (As<Int4>(atan2_theta))))) | // FIXME: Vector select
426 (inf_y & As<Int4>(quarter_pi)));
John Bauman19bac1e2014-05-06 15:23:49 -0400427
Alexis Hetu1728dde2017-11-08 13:43:16 -0500428 // Recover loss of precision for tiny theta angles
429 Int4 precision_loss = S & Q & O & ~inf_y; // This combination results in (-pi + half_pi + half_pi - atan2_theta) which is equivalent to -atan2_theta
430 return As<Float4>((precision_loss & As<Int4>(-atan2_theta)) | (~precision_loss & As<Int4>(theta))); // FIXME: Vector select
John Bauman19bac1e2014-05-06 15:23:49 -0400431 }
432
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -0400433 Float4 sineh(RValue<Float4> x, bool pp)
434 {
435 return (exponential(x, pp) - exponential(-x, pp)) * Float4(0.5f);
436 }
437
438 Float4 cosineh(RValue<Float4> x, bool pp)
439 {
440 return (exponential(x, pp) + exponential(-x, pp)) * Float4(0.5f);
441 }
442
443 Float4 tangenth(RValue<Float4> x, bool pp)
444 {
445 Float4 e_x = exponential(x, pp);
446 Float4 e_minus_x = exponential(-x, pp);
447 return (e_x - e_minus_x) / (e_x + e_minus_x);
448 }
449
450 Float4 arccosh(RValue<Float4> x, bool pp)
451 {
452 return logarithm(x + Sqrt(x + Float4(1.0f)) * Sqrt(x - Float4(1.0f)), pp);
453 }
454
455 Float4 arcsinh(RValue<Float4> x, bool pp)
456 {
457 return logarithm(x + Sqrt(x * x + Float4(1.0f)), pp);
458 }
459
460 Float4 arctanh(RValue<Float4> x, bool pp)
461 {
462 return logarithm((Float4(1.0f) + x) / (Float4(1.0f) - x), pp) * Float4(0.5f);
463 }
464
Alexis Hetuecad5192015-06-05 13:42:05 -0400465 Float4 dot2(const Vector4f &v0, const Vector4f &v1)
John Bauman19bac1e2014-05-06 15:23:49 -0400466 {
467 return v0.x * v1.x + v0.y * v1.y;
468 }
469
Alexis Hetuecad5192015-06-05 13:42:05 -0400470 Float4 dot3(const Vector4f &v0, const Vector4f &v1)
John Bauman19bac1e2014-05-06 15:23:49 -0400471 {
472 return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z;
473 }
474
Alexis Hetuecad5192015-06-05 13:42:05 -0400475 Float4 dot4(const Vector4f &v0, const Vector4f &v1)
John Bauman19bac1e2014-05-06 15:23:49 -0400476 {
477 return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z + v0.w * v1.w;
478 }
479
480 void transpose4x4(Short4 &row0, Short4 &row1, Short4 &row2, Short4 &row3)
481 {
482 Int2 tmp0 = UnpackHigh(row0, row1);
483 Int2 tmp1 = UnpackHigh(row2, row3);
484 Int2 tmp2 = UnpackLow(row0, row1);
485 Int2 tmp3 = UnpackLow(row2, row3);
486
Nicolas Capens45f187a2016-12-02 15:30:56 -0500487 row0 = UnpackLow(tmp2, tmp3);
488 row1 = UnpackHigh(tmp2, tmp3);
489 row2 = UnpackLow(tmp0, tmp1);
490 row3 = UnpackHigh(tmp0, tmp1);
John Bauman19bac1e2014-05-06 15:23:49 -0400491 }
492
493 void transpose4x4(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
494 {
495 Float4 tmp0 = UnpackLow(row0, row1);
496 Float4 tmp1 = UnpackLow(row2, row3);
497 Float4 tmp2 = UnpackHigh(row0, row1);
498 Float4 tmp3 = UnpackHigh(row2, row3);
499
500 row0 = Float4(tmp0.xy, tmp1.xy);
501 row1 = Float4(tmp0.zw, tmp1.zw);
502 row2 = Float4(tmp2.xy, tmp3.xy);
503 row3 = Float4(tmp2.zw, tmp3.zw);
504 }
505
506 void transpose4x3(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
507 {
508 Float4 tmp0 = UnpackLow(row0, row1);
509 Float4 tmp1 = UnpackLow(row2, row3);
510 Float4 tmp2 = UnpackHigh(row0, row1);
511 Float4 tmp3 = UnpackHigh(row2, row3);
512
513 row0 = Float4(tmp0.xy, tmp1.xy);
514 row1 = Float4(tmp0.zw, tmp1.zw);
515 row2 = Float4(tmp2.xy, tmp3.xy);
516 }
517
518 void transpose4x2(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
519 {
520 Float4 tmp0 = UnpackLow(row0, row1);
521 Float4 tmp1 = UnpackLow(row2, row3);
522
523 row0 = Float4(tmp0.xy, tmp1.xy);
524 row1 = Float4(tmp0.zw, tmp1.zw);
525 }
526
527 void transpose4x1(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
528 {
529 Float4 tmp0 = UnpackLow(row0, row1);
530 Float4 tmp1 = UnpackLow(row2, row3);
531
532 row0 = Float4(tmp0.xy, tmp1.xy);
533 }
534
535 void transpose2x4(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3)
536 {
Nicolas Capens54ac5e82016-12-09 14:07:50 -0500537 Float4 tmp01 = UnpackLow(row0, row1);
538 Float4 tmp23 = UnpackHigh(row0, row1);
John Bauman19bac1e2014-05-06 15:23:49 -0400539
Nicolas Capens54ac5e82016-12-09 14:07:50 -0500540 row0 = tmp01;
541 row1 = Float4(tmp01.zw, row1.zw);
542 row2 = tmp23;
543 row3 = Float4(tmp23.zw, row3.zw);
John Bauman19bac1e2014-05-06 15:23:49 -0400544 }
545
546 void transpose4xN(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3, int N)
547 {
548 switch(N)
549 {
550 case 1: transpose4x1(row0, row1, row2, row3); break;
551 case 2: transpose4x2(row0, row1, row2, row3); break;
552 case 3: transpose4x3(row0, row1, row2, row3); break;
553 case 4: transpose4x4(row0, row1, row2, row3); break;
554 }
555 }
556
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400557 void ShaderCore::mov(Vector4f &dst, const Vector4f &src, bool integerDestination)
John Bauman89401822014-05-06 15:04:28 -0400558 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400559 if(integerDestination)
John Bauman89401822014-05-06 15:04:28 -0400560 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400561 dst.x = As<Float4>(RoundInt(src.x));
562 dst.y = As<Float4>(RoundInt(src.y));
563 dst.z = As<Float4>(RoundInt(src.z));
564 dst.w = As<Float4>(RoundInt(src.w));
John Bauman89401822014-05-06 15:04:28 -0400565 }
566 else
567 {
568 dst = src;
569 }
570 }
571
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400572 void ShaderCore::neg(Vector4f &dst, const Vector4f &src)
573 {
574 dst.x = -src.x;
575 dst.y = -src.y;
576 dst.z = -src.z;
577 dst.w = -src.w;
578 }
579
580 void ShaderCore::ineg(Vector4f &dst, const Vector4f &src)
581 {
582 dst.x = As<Float4>(-As<Int4>(src.x));
583 dst.y = As<Float4>(-As<Int4>(src.y));
584 dst.z = As<Float4>(-As<Int4>(src.z));
585 dst.w = As<Float4>(-As<Int4>(src.w));
586 }
587
Alexis Hetuecad5192015-06-05 13:42:05 -0400588 void ShaderCore::f2b(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -0400589 {
590 dst.x = As<Float4>(CmpNEQ(src.x, Float4(0.0f)));
591 dst.y = As<Float4>(CmpNEQ(src.y, Float4(0.0f)));
592 dst.z = As<Float4>(CmpNEQ(src.z, Float4(0.0f)));
593 dst.w = As<Float4>(CmpNEQ(src.w, Float4(0.0f)));
594 }
595
Alexis Hetuecad5192015-06-05 13:42:05 -0400596 void ShaderCore::b2f(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -0400597 {
598 dst.x = As<Float4>(As<Int4>(src.x) & As<Int4>(Float4(1.0f)));
599 dst.y = As<Float4>(As<Int4>(src.y) & As<Int4>(Float4(1.0f)));
600 dst.z = As<Float4>(As<Int4>(src.z) & As<Int4>(Float4(1.0f)));
601 dst.w = As<Float4>(As<Int4>(src.w) & As<Int4>(Float4(1.0f)));
602 }
603
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400604 void ShaderCore::f2i(Vector4f &dst, const Vector4f &src)
605 {
606 dst.x = As<Float4>(Int4(src.x));
607 dst.y = As<Float4>(Int4(src.y));
608 dst.z = As<Float4>(Int4(src.z));
609 dst.w = As<Float4>(Int4(src.w));
610 }
611
612 void ShaderCore::i2f(Vector4f &dst, const Vector4f &src)
613 {
614 dst.x = Float4(As<Int4>(src.x));
615 dst.y = Float4(As<Int4>(src.y));
616 dst.z = Float4(As<Int4>(src.z));
617 dst.w = Float4(As<Int4>(src.w));
618 }
619
620 void ShaderCore::f2u(Vector4f &dst, const Vector4f &src)
621 {
622 dst.x = As<Float4>(UInt4(src.x));
623 dst.y = As<Float4>(UInt4(src.y));
624 dst.z = As<Float4>(UInt4(src.z));
625 dst.w = As<Float4>(UInt4(src.w));
626 }
627
628 void ShaderCore::u2f(Vector4f &dst, const Vector4f &src)
629 {
630 dst.x = Float4(As<UInt4>(src.x));
631 dst.y = Float4(As<UInt4>(src.y));
632 dst.z = Float4(As<UInt4>(src.z));
633 dst.w = Float4(As<UInt4>(src.w));
634 }
635
636 void ShaderCore::i2b(Vector4f &dst, const Vector4f &src)
637 {
638 dst.x = As<Float4>(CmpNEQ(As<Int4>(src.x), Int4(0)));
639 dst.y = As<Float4>(CmpNEQ(As<Int4>(src.y), Int4(0)));
640 dst.z = As<Float4>(CmpNEQ(As<Int4>(src.z), Int4(0)));
641 dst.w = As<Float4>(CmpNEQ(As<Int4>(src.w), Int4(0)));
642 }
643
644 void ShaderCore::b2i(Vector4f &dst, const Vector4f &src)
645 {
646 dst.x = As<Float4>(As<Int4>(src.x) & Int4(1));
647 dst.y = As<Float4>(As<Int4>(src.y) & Int4(1));
648 dst.z = As<Float4>(As<Int4>(src.z) & Int4(1));
649 dst.w = As<Float4>(As<Int4>(src.w) & Int4(1));
650 }
651
Alexis Hetuecad5192015-06-05 13:42:05 -0400652 void ShaderCore::add(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400653 {
654 dst.x = src0.x + src1.x;
655 dst.y = src0.y + src1.y;
656 dst.z = src0.z + src1.z;
657 dst.w = src0.w + src1.w;
658 }
659
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400660 void ShaderCore::iadd(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
661 {
662 dst.x = As<Float4>(As<Int4>(src0.x) + As<Int4>(src1.x));
663 dst.y = As<Float4>(As<Int4>(src0.y) + As<Int4>(src1.y));
664 dst.z = As<Float4>(As<Int4>(src0.z) + As<Int4>(src1.z));
665 dst.w = As<Float4>(As<Int4>(src0.w) + As<Int4>(src1.w));
666 }
667
Alexis Hetuecad5192015-06-05 13:42:05 -0400668 void ShaderCore::sub(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400669 {
670 dst.x = src0.x - src1.x;
671 dst.y = src0.y - src1.y;
672 dst.z = src0.z - src1.z;
673 dst.w = src0.w - src1.w;
674 }
675
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400676 void ShaderCore::isub(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
677 {
678 dst.x = As<Float4>(As<Int4>(src0.x) - As<Int4>(src1.x));
679 dst.y = As<Float4>(As<Int4>(src0.y) - As<Int4>(src1.y));
680 dst.z = As<Float4>(As<Int4>(src0.z) - As<Int4>(src1.z));
681 dst.w = As<Float4>(As<Int4>(src0.w) - As<Int4>(src1.w));
682 }
683
Alexis Hetuecad5192015-06-05 13:42:05 -0400684 void ShaderCore::mad(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
John Bauman89401822014-05-06 15:04:28 -0400685 {
686 dst.x = src0.x * src1.x + src2.x;
687 dst.y = src0.y * src1.y + src2.y;
688 dst.z = src0.z * src1.z + src2.z;
689 dst.w = src0.w * src1.w + src2.w;
690 }
691
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400692 void ShaderCore::imad(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
693 {
694 dst.x = As<Float4>(As<Int4>(src0.x) * As<Int4>(src1.x) + As<Int4>(src2.x));
695 dst.y = As<Float4>(As<Int4>(src0.y) * As<Int4>(src1.y) + As<Int4>(src2.y));
696 dst.z = As<Float4>(As<Int4>(src0.z) * As<Int4>(src1.z) + As<Int4>(src2.z));
697 dst.w = As<Float4>(As<Int4>(src0.w) * As<Int4>(src1.w) + As<Int4>(src2.w));
698 }
699
Alexis Hetuecad5192015-06-05 13:42:05 -0400700 void ShaderCore::mul(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400701 {
702 dst.x = src0.x * src1.x;
703 dst.y = src0.y * src1.y;
704 dst.z = src0.z * src1.z;
705 dst.w = src0.w * src1.w;
706 }
707
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400708 void ShaderCore::imul(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
709 {
710 dst.x = As<Float4>(As<Int4>(src0.x) * As<Int4>(src1.x));
711 dst.y = As<Float4>(As<Int4>(src0.y) * As<Int4>(src1.y));
712 dst.z = As<Float4>(As<Int4>(src0.z) * As<Int4>(src1.z));
713 dst.w = As<Float4>(As<Int4>(src0.w) * As<Int4>(src1.w));
714 }
715
Alexis Hetuecad5192015-06-05 13:42:05 -0400716 void ShaderCore::rcpx(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman89401822014-05-06 15:04:28 -0400717 {
718 Float4 rcp = reciprocal(src.x, pp, true);
719
720 dst.x = rcp;
721 dst.y = rcp;
722 dst.z = rcp;
723 dst.w = rcp;
724 }
725
Alexis Hetuecad5192015-06-05 13:42:05 -0400726 void ShaderCore::div(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400727 {
728 dst.x = src0.x / src1.x;
729 dst.y = src0.y / src1.y;
730 dst.z = src0.z / src1.z;
731 dst.w = src0.w / src1.w;
732 }
733
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400734 void ShaderCore::idiv(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
735 {
736 Float4 intMax(As<Float4>(Int4(INT_MAX)));
737 cmp0i(dst.x, src1.x, intMax, src1.x);
738 dst.x = As<Float4>(As<Int4>(src0.x) / As<Int4>(dst.x));
739 cmp0i(dst.y, src1.y, intMax, src1.y);
740 dst.y = As<Float4>(As<Int4>(src0.y) / As<Int4>(dst.y));
741 cmp0i(dst.z, src1.z, intMax, src1.z);
742 dst.z = As<Float4>(As<Int4>(src0.z) / As<Int4>(dst.z));
743 cmp0i(dst.w, src1.w, intMax, src1.w);
744 dst.w = As<Float4>(As<Int4>(src0.w) / As<Int4>(dst.w));
745 }
746
747 void ShaderCore::udiv(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
748 {
749 Float4 uintMax(As<Float4>(UInt4(UINT_MAX)));
750 cmp0i(dst.x, src1.x, uintMax, src1.x);
751 dst.x = As<Float4>(As<UInt4>(src0.x) / As<UInt4>(dst.x));
752 cmp0i(dst.y, src1.y, uintMax, src1.y);
753 dst.y = As<Float4>(As<UInt4>(src0.y) / As<UInt4>(dst.y));
754 cmp0i(dst.z, src1.z, uintMax, src1.z);
755 dst.z = As<Float4>(As<UInt4>(src0.z) / As<UInt4>(dst.z));
756 cmp0i(dst.w, src1.w, uintMax, src1.w);
757 dst.w = As<Float4>(As<UInt4>(src0.w) / As<UInt4>(dst.w));
758 }
759
Alexis Hetuecad5192015-06-05 13:42:05 -0400760 void ShaderCore::mod(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400761 {
762 dst.x = modulo(src0.x, src1.x);
763 dst.y = modulo(src0.y, src1.y);
764 dst.z = modulo(src0.z, src1.z);
765 dst.w = modulo(src0.w, src1.w);
766 }
767
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400768 void ShaderCore::imod(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
769 {
Alexis Hetu28958102017-10-02 13:48:19 -0400770 Float4 intMax(As<Float4>(Int4(INT_MAX)));
771 cmp0i(dst.x, src1.x, intMax, src1.x);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400772 dst.x = As<Float4>(As<Int4>(src0.x) % As<Int4>(dst.x));
Alexis Hetu28958102017-10-02 13:48:19 -0400773 cmp0i(dst.y, src1.y, intMax, src1.y);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400774 dst.y = As<Float4>(As<Int4>(src0.y) % As<Int4>(dst.y));
Alexis Hetu28958102017-10-02 13:48:19 -0400775 cmp0i(dst.z, src1.z, intMax, src1.z);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400776 dst.z = As<Float4>(As<Int4>(src0.z) % As<Int4>(dst.z));
Alexis Hetu28958102017-10-02 13:48:19 -0400777 cmp0i(dst.w, src1.w, intMax, src1.w);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400778 dst.w = As<Float4>(As<Int4>(src0.w) % As<Int4>(dst.w));
779 }
Alexis Hetu28958102017-10-02 13:48:19 -0400780
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400781 void ShaderCore::umod(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
782 {
Alexis Hetu28958102017-10-02 13:48:19 -0400783 Float4 uintMax(As<Float4>(UInt4(UINT_MAX)));
784 cmp0i(dst.x, src1.x, uintMax, src1.x);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400785 dst.x = As<Float4>(As<UInt4>(src0.x) % As<UInt4>(dst.x));
Alexis Hetu28958102017-10-02 13:48:19 -0400786 cmp0i(dst.y, src1.y, uintMax, src1.y);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400787 dst.y = As<Float4>(As<UInt4>(src0.y) % As<UInt4>(dst.y));
Alexis Hetu28958102017-10-02 13:48:19 -0400788 cmp0i(dst.z, src1.z, uintMax, src1.z);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400789 dst.z = As<Float4>(As<UInt4>(src0.z) % As<UInt4>(dst.z));
Alexis Hetu28958102017-10-02 13:48:19 -0400790 cmp0i(dst.w, src1.w, uintMax, src1.w);
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400791 dst.w = As<Float4>(As<UInt4>(src0.w) % As<UInt4>(dst.w));
792 }
793
794 void ShaderCore::shl(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
795 {
796 dst.x = As<Float4>(As<Int4>(src0.x) << As<Int4>(src1.x));
797 dst.y = As<Float4>(As<Int4>(src0.y) << As<Int4>(src1.y));
798 dst.z = As<Float4>(As<Int4>(src0.z) << As<Int4>(src1.z));
799 dst.w = As<Float4>(As<Int4>(src0.w) << As<Int4>(src1.w));
800 }
801
802 void ShaderCore::ishr(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
803 {
804 dst.x = As<Float4>(As<Int4>(src0.x) >> As<Int4>(src1.x));
805 dst.y = As<Float4>(As<Int4>(src0.y) >> As<Int4>(src1.y));
806 dst.z = As<Float4>(As<Int4>(src0.z) >> As<Int4>(src1.z));
807 dst.w = As<Float4>(As<Int4>(src0.w) >> As<Int4>(src1.w));
808 }
809
810 void ShaderCore::ushr(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
811 {
812 dst.x = As<Float4>(As<UInt4>(src0.x) >> As<UInt4>(src1.x));
813 dst.y = As<Float4>(As<UInt4>(src0.y) >> As<UInt4>(src1.y));
814 dst.z = As<Float4>(As<UInt4>(src0.z) >> As<UInt4>(src1.z));
815 dst.w = As<Float4>(As<UInt4>(src0.w) >> As<UInt4>(src1.w));
816 }
817
Alexis Hetuecad5192015-06-05 13:42:05 -0400818 void ShaderCore::rsqx(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman89401822014-05-06 15:04:28 -0400819 {
820 Float4 rsq = reciprocalSquareRoot(src.x, true, pp);
821
John Bauman19bac1e2014-05-06 15:23:49 -0400822 dst.x = rsq;
823 dst.y = rsq;
824 dst.z = rsq;
825 dst.w = rsq;
John Bauman89401822014-05-06 15:04:28 -0400826 }
827
Alexis Hetuecad5192015-06-05 13:42:05 -0400828 void ShaderCore::sqrt(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400829 {
830 dst.x = Sqrt(src.x);
831 dst.y = Sqrt(src.y);
832 dst.z = Sqrt(src.z);
833 dst.w = Sqrt(src.w);
834 }
835
Alexis Hetuecad5192015-06-05 13:42:05 -0400836 void ShaderCore::rsq(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400837 {
838 dst.x = reciprocalSquareRoot(src.x, false, pp);
839 dst.y = reciprocalSquareRoot(src.y, false, pp);
840 dst.z = reciprocalSquareRoot(src.z, false, pp);
841 dst.w = reciprocalSquareRoot(src.w, false, pp);
842 }
843
Alexis Hetuecad5192015-06-05 13:42:05 -0400844 void ShaderCore::len2(Float4 &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400845 {
846 dst = Sqrt(dot2(src, src));
847 }
848
Alexis Hetuecad5192015-06-05 13:42:05 -0400849 void ShaderCore::len3(Float4 &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400850 {
851 dst = Sqrt(dot3(src, src));
852 }
853
Alexis Hetuecad5192015-06-05 13:42:05 -0400854 void ShaderCore::len4(Float4 &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400855 {
856 dst = Sqrt(dot4(src, src));
857 }
858
Alexis Hetuecad5192015-06-05 13:42:05 -0400859 void ShaderCore::dist1(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400860 {
861 dst = Abs(src0.x - src1.x);
862 }
863
Alexis Hetuecad5192015-06-05 13:42:05 -0400864 void ShaderCore::dist2(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400865 {
866 Float4 dx = src0.x - src1.x;
867 Float4 dy = src0.y - src1.y;
868 Float4 dot2 = dx * dx + dy * dy;
869 dst = Sqrt(dot2);
870 }
871
Alexis Hetuecad5192015-06-05 13:42:05 -0400872 void ShaderCore::dist3(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400873 {
874 Float4 dx = src0.x - src1.x;
875 Float4 dy = src0.y - src1.y;
876 Float4 dz = src0.z - src1.z;
877 Float4 dot3 = dx * dx + dy * dy + dz * dz;
878 dst = Sqrt(dot3);
879 }
880
Alexis Hetuecad5192015-06-05 13:42:05 -0400881 void ShaderCore::dist4(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -0400882 {
883 Float4 dx = src0.x - src1.x;
884 Float4 dy = src0.y - src1.y;
885 Float4 dz = src0.z - src1.z;
886 Float4 dw = src0.w - src1.w;
887 Float4 dot4 = dx * dx + dy * dy + dz * dz + dw * dw;
888 dst = Sqrt(dot4);
889 }
890
Alexis Hetuecad5192015-06-05 13:42:05 -0400891 void ShaderCore::dp1(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400892 {
893 Float4 t = src0.x * src1.x;
894
895 dst.x = t;
896 dst.y = t;
897 dst.z = t;
898 dst.w = t;
899 }
900
Alexis Hetuecad5192015-06-05 13:42:05 -0400901 void ShaderCore::dp2(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400902 {
903 Float4 t = dot2(src0, src1);
904
905 dst.x = t;
906 dst.y = t;
907 dst.z = t;
908 dst.w = t;
909 }
910
Alexis Hetuecad5192015-06-05 13:42:05 -0400911 void ShaderCore::dp2add(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
John Bauman19bac1e2014-05-06 15:23:49 -0400912 {
913 Float4 t = dot2(src0, src1) + src2.x;
914
915 dst.x = t;
916 dst.y = t;
917 dst.z = t;
918 dst.w = t;
919 }
920
Alexis Hetuecad5192015-06-05 13:42:05 -0400921 void ShaderCore::dp3(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400922 {
923 Float4 dot = dot3(src0, src1);
924
925 dst.x = dot;
926 dst.y = dot;
927 dst.z = dot;
928 dst.w = dot;
929 }
930
Alexis Hetuecad5192015-06-05 13:42:05 -0400931 void ShaderCore::dp4(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400932 {
933 Float4 dot = dot4(src0, src1);
934
935 dst.x = dot;
936 dst.y = dot;
937 dst.z = dot;
938 dst.w = dot;
939 }
940
Alexis Hetuecad5192015-06-05 13:42:05 -0400941 void ShaderCore::min(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400942 {
943 dst.x = Min(src0.x, src1.x);
944 dst.y = Min(src0.y, src1.y);
945 dst.z = Min(src0.z, src1.z);
946 dst.w = Min(src0.w, src1.w);
947 }
948
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400949 void ShaderCore::imin(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
950 {
951 dst.x = As<Float4>(Min(As<Int4>(src0.x), As<Int4>(src1.x)));
952 dst.y = As<Float4>(Min(As<Int4>(src0.y), As<Int4>(src1.y)));
953 dst.z = As<Float4>(Min(As<Int4>(src0.z), As<Int4>(src1.z)));
954 dst.w = As<Float4>(Min(As<Int4>(src0.w), As<Int4>(src1.w)));
955 }
956
957 void ShaderCore::umin(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
958 {
959 dst.x = As<Float4>(Min(As<UInt4>(src0.x), As<UInt4>(src1.x)));
960 dst.y = As<Float4>(Min(As<UInt4>(src0.y), As<UInt4>(src1.y)));
961 dst.z = As<Float4>(Min(As<UInt4>(src0.z), As<UInt4>(src1.z)));
962 dst.w = As<Float4>(Min(As<UInt4>(src0.w), As<UInt4>(src1.w)));
963 }
964
Alexis Hetuecad5192015-06-05 13:42:05 -0400965 void ShaderCore::max(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400966 {
967 dst.x = Max(src0.x, src1.x);
968 dst.y = Max(src0.y, src1.y);
969 dst.z = Max(src0.z, src1.z);
970 dst.w = Max(src0.w, src1.w);
971 }
972
Alexis Hetuc4f2c292015-08-18 15:43:09 -0400973 void ShaderCore::imax(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
974 {
975 dst.x = As<Float4>(Max(As<Int4>(src0.x), As<Int4>(src1.x)));
976 dst.y = As<Float4>(Max(As<Int4>(src0.y), As<Int4>(src1.y)));
977 dst.z = As<Float4>(Max(As<Int4>(src0.z), As<Int4>(src1.z)));
978 dst.w = As<Float4>(Max(As<Int4>(src0.w), As<Int4>(src1.w)));
979 }
980
981 void ShaderCore::umax(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
982 {
983 dst.x = As<Float4>(Max(As<Int4>(src0.x), As<Int4>(src1.x)));
984 dst.y = As<Float4>(Max(As<Int4>(src0.y), As<Int4>(src1.y)));
985 dst.z = As<Float4>(Max(As<Int4>(src0.z), As<Int4>(src1.z)));
986 dst.w = As<Float4>(Max(As<Int4>(src0.w), As<Int4>(src1.w)));
987 }
988
Alexis Hetuecad5192015-06-05 13:42:05 -0400989 void ShaderCore::slt(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -0400990 {
John Bauman19bac1e2014-05-06 15:23:49 -0400991 dst.x = As<Float4>(As<Int4>(CmpLT(src0.x, src1.x)) & As<Int4>(Float4(1.0f)));
992 dst.y = As<Float4>(As<Int4>(CmpLT(src0.y, src1.y)) & As<Int4>(Float4(1.0f)));
993 dst.z = As<Float4>(As<Int4>(CmpLT(src0.z, src1.z)) & As<Int4>(Float4(1.0f)));
994 dst.w = As<Float4>(As<Int4>(CmpLT(src0.w, src1.w)) & As<Int4>(Float4(1.0f)));
John Bauman89401822014-05-06 15:04:28 -0400995 }
996
Alexis Hetuecad5192015-06-05 13:42:05 -0400997 void ShaderCore::step(Vector4f &dst, const Vector4f &edge, const Vector4f &x)
John Bauman89401822014-05-06 15:04:28 -0400998 {
John Bauman19bac1e2014-05-06 15:23:49 -0400999 dst.x = As<Float4>(CmpNLT(x.x, edge.x) & As<Int4>(Float4(1.0f)));
1000 dst.y = As<Float4>(CmpNLT(x.y, edge.y) & As<Int4>(Float4(1.0f)));
1001 dst.z = As<Float4>(CmpNLT(x.z, edge.z) & As<Int4>(Float4(1.0f)));
1002 dst.w = As<Float4>(CmpNLT(x.w, edge.w) & As<Int4>(Float4(1.0f)));
John Bauman89401822014-05-06 15:04:28 -04001003 }
1004
Alexis Hetuecad5192015-06-05 13:42:05 -04001005 void ShaderCore::exp2x(Vector4f &dst, const Vector4f &src, bool pp)
Nicolas Capens0bac2852016-05-07 06:09:58 -04001006 {
John Bauman19bac1e2014-05-06 15:23:49 -04001007 Float4 exp = exponential2(src.x, pp);
John Bauman89401822014-05-06 15:04:28 -04001008
1009 dst.x = exp;
1010 dst.y = exp;
1011 dst.z = exp;
1012 dst.w = exp;
1013 }
1014
Alexis Hetuecad5192015-06-05 13:42:05 -04001015 void ShaderCore::exp2(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman89401822014-05-06 15:04:28 -04001016 {
John Bauman19bac1e2014-05-06 15:23:49 -04001017 dst.x = exponential2(src.x, pp);
1018 dst.y = exponential2(src.y, pp);
1019 dst.z = exponential2(src.z, pp);
1020 dst.w = exponential2(src.w, pp);
1021 }
1022
Alexis Hetuecad5192015-06-05 13:42:05 -04001023 void ShaderCore::exp(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001024 {
1025 dst.x = exponential(src.x, pp);
1026 dst.y = exponential(src.y, pp);
1027 dst.z = exponential(src.z, pp);
1028 dst.w = exponential(src.w, pp);
1029 }
1030
Alexis Hetuecad5192015-06-05 13:42:05 -04001031 void ShaderCore::log2x(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001032 {
1033 Float4 log = logarithm2(src.x, true, pp);
John Bauman89401822014-05-06 15:04:28 -04001034
1035 dst.x = log;
1036 dst.y = log;
1037 dst.z = log;
1038 dst.w = log;
1039 }
1040
Alexis Hetuecad5192015-06-05 13:42:05 -04001041 void ShaderCore::log2(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman89401822014-05-06 15:04:28 -04001042 {
Alexis Hetua781af72017-07-06 17:12:47 -04001043 dst.x = logarithm2(src.x, false, pp);
1044 dst.y = logarithm2(src.y, false, pp);
1045 dst.z = logarithm2(src.z, false, pp);
1046 dst.w = logarithm2(src.w, false, pp);
John Bauman19bac1e2014-05-06 15:23:49 -04001047 }
1048
Alexis Hetuecad5192015-06-05 13:42:05 -04001049 void ShaderCore::log(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001050 {
1051 dst.x = logarithm(src.x, false, pp);
1052 dst.y = logarithm(src.y, false, pp);
1053 dst.z = logarithm(src.z, false, pp);
1054 dst.w = logarithm(src.w, false, pp);
1055 }
1056
Alexis Hetuecad5192015-06-05 13:42:05 -04001057 void ShaderCore::lit(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001058 {
1059 dst.x = Float4(1.0f);
1060 dst.y = Max(src.x, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -04001061
1062 Float4 pow;
1063
1064 pow = src.w;
John Bauman19bac1e2014-05-06 15:23:49 -04001065 pow = Min(pow, Float4(127.9961f));
1066 pow = Max(pow, Float4(-127.9961f));
John Bauman89401822014-05-06 15:04:28 -04001067
1068 dst.z = power(src.y, pow);
John Bauman19bac1e2014-05-06 15:23:49 -04001069 dst.z = As<Float4>(As<Int4>(dst.z) & CmpNLT(src.x, Float4(0.0f)));
1070 dst.z = As<Float4>(As<Int4>(dst.z) & CmpNLT(src.y, Float4(0.0f)));
John Bauman89401822014-05-06 15:04:28 -04001071
John Bauman19bac1e2014-05-06 15:23:49 -04001072 dst.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04001073 }
1074
Alexis Hetuecad5192015-06-05 13:42:05 -04001075 void ShaderCore::att(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -04001076 {
John Bauman19bac1e2014-05-06 15:23:49 -04001077 // Computes attenuation factors (1, d, d^2, 1/d) assuming src0 = d^2, src1 = 1/d
John Bauman89401822014-05-06 15:04:28 -04001078 dst.x = 1;
1079 dst.y = src0.y * src1.y;
1080 dst.z = src0.z;
1081 dst.w = src1.w;
1082 }
1083
Alexis Hetuecad5192015-06-05 13:42:05 -04001084 void ShaderCore::lrp(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
John Bauman89401822014-05-06 15:04:28 -04001085 {
1086 dst.x = src0.x * (src1.x - src2.x) + src2.x;
1087 dst.y = src0.y * (src1.y - src2.y) + src2.y;
1088 dst.z = src0.z * (src1.z - src2.z) + src2.z;
1089 dst.w = src0.w * (src1.w - src2.w) + src2.w;
1090 }
1091
Alexis Hetu8ef6d102017-11-09 15:49:09 -05001092 void ShaderCore::isinf(Vector4f &dst, const Vector4f &src)
1093 {
1094 dst.x = As<Float4>(IsInf(src.x));
1095 dst.y = As<Float4>(IsInf(src.y));
1096 dst.z = As<Float4>(IsInf(src.z));
1097 dst.w = As<Float4>(IsInf(src.w));
1098 }
1099
1100 void ShaderCore::isnan(Vector4f &dst, const Vector4f &src)
1101 {
1102 dst.x = As<Float4>(IsNan(src.x));
1103 dst.y = As<Float4>(IsNan(src.y));
1104 dst.z = As<Float4>(IsNan(src.z));
1105 dst.w = As<Float4>(IsNan(src.w));
1106 }
1107
Alexis Hetuecad5192015-06-05 13:42:05 -04001108 void ShaderCore::smooth(Vector4f &dst, const Vector4f &edge0, const Vector4f &edge1, const Vector4f &x)
John Bauman89401822014-05-06 15:04:28 -04001109 {
John Bauman19bac1e2014-05-06 15:23:49 -04001110 Float4 tx = Min(Max((x.x - edge0.x) / (edge1.x - edge0.x), Float4(0.0f)), Float4(1.0f)); dst.x = tx * tx * (Float4(3.0f) - Float4(2.0f) * tx);
1111 Float4 ty = Min(Max((x.y - edge0.y) / (edge1.y - edge0.y), Float4(0.0f)), Float4(1.0f)); dst.y = ty * ty * (Float4(3.0f) - Float4(2.0f) * ty);
1112 Float4 tz = Min(Max((x.z - edge0.z) / (edge1.z - edge0.z), Float4(0.0f)), Float4(1.0f)); dst.z = tz * tz * (Float4(3.0f) - Float4(2.0f) * tz);
1113 Float4 tw = Min(Max((x.w - edge0.w) / (edge1.w - edge0.w), Float4(0.0f)), Float4(1.0f)); dst.w = tw * tw * (Float4(3.0f) - Float4(2.0f) * tw);
John Bauman89401822014-05-06 15:04:28 -04001114 }
1115
Alexis Hetuffb35eb2016-04-06 18:05:00 -04001116 void ShaderCore::floatToHalfBits(Float4& dst, const Float4& floatBits, bool storeInUpperBits)
1117 {
1118 static const uint32_t mask_sign = 0x80000000u;
1119 static const uint32_t mask_round = ~0xfffu;
1120 static const uint32_t c_f32infty = 255 << 23;
1121 static const uint32_t c_magic = 15 << 23;
1122 static const uint32_t c_nanbit = 0x200;
1123 static const uint32_t c_infty_as_fp16 = 0x7c00;
1124 static const uint32_t c_clamp = (31 << 23) - 0x1000;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001125
Alexis Hetuffb35eb2016-04-06 18:05:00 -04001126 UInt4 justsign = UInt4(mask_sign) & As<UInt4>(floatBits);
1127 UInt4 absf = As<UInt4>(floatBits) ^ justsign;
1128 UInt4 b_isnormal = CmpNLE(UInt4(c_f32infty), absf);
1129
1130 // Note: this version doesn't round to the nearest even in case of a tie as defined by IEEE 754-2008, it rounds to +inf
1131 // instead of nearest even, since that's fine for GLSL ES 3.0's needs (see section 2.1.1 Floating-Point Computation)
1132 UInt4 joined = ((((As<UInt4>(Min(As<Float4>(absf & UInt4(mask_round)) * As<Float4>(UInt4(c_magic)),
1133 As<Float4>(UInt4(c_clamp))))) - UInt4(mask_round)) >> 13) & b_isnormal) |
1134 ((b_isnormal ^ UInt4(0xFFFFFFFF)) & ((CmpNLE(absf, UInt4(c_f32infty)) & UInt4(c_nanbit)) |
1135 UInt4(c_infty_as_fp16)));
1136
1137 dst = As<Float4>(storeInUpperBits ? As<UInt4>(dst) | ((joined << 16) | justsign) : joined | (justsign >> 16));
1138 }
1139
1140 void ShaderCore::halfToFloatBits(Float4& dst, const Float4& halfBits)
1141 {
1142 static const uint32_t mask_nosign = 0x7FFF;
1143 static const uint32_t magic = (254 - 15) << 23;
1144 static const uint32_t was_infnan = 0x7BFF;
1145 static const uint32_t exp_infnan = 255 << 23;
1146
1147 UInt4 expmant = As<UInt4>(halfBits) & UInt4(mask_nosign);
1148 dst = As<Float4>(As<UInt4>(As<Float4>(expmant << 13) * As<Float4>(UInt4(magic))) |
1149 ((As<UInt4>(halfBits) ^ UInt4(expmant)) << 16) |
1150 (CmpNLE(As<UInt4>(expmant), UInt4(was_infnan)) & UInt4(exp_infnan)));
1151 }
1152
1153 void ShaderCore::packHalf2x16(Vector4f &d, const Vector4f &s0)
1154 {
1155 // half2 | half1
1156 floatToHalfBits(d.x, s0.x, false);
1157 floatToHalfBits(d.x, s0.y, true);
1158 }
1159
1160 void ShaderCore::unpackHalf2x16(Vector4f &dst, const Vector4f &s0)
1161 {
1162 // half2 | half1
1163 halfToFloatBits(dst.x, As<Float4>(As<UInt4>(s0.x) & UInt4(0x0000FFFF)));
1164 halfToFloatBits(dst.y, As<Float4>((As<UInt4>(s0.x) & UInt4(0xFFFF0000)) >> 16));
1165 }
1166
Alexis Hetu9cde9742016-04-06 13:03:38 -04001167 void ShaderCore::packSnorm2x16(Vector4f &d, const Vector4f &s0)
1168 {
1169 // round(clamp(c, -1.0, 1.0) * 32767.0)
1170 d.x = As<Float4>((Int4(Round(Min(Max(s0.x, Float4(-1.0f)), Float4(1.0f)) * Float4(32767.0f))) & Int4(0xFFFF)) |
1171 ((Int4(Round(Min(Max(s0.y, Float4(-1.0f)), Float4(1.0f)) * Float4(32767.0f))) & Int4(0xFFFF)) << 16));
1172 }
1173
1174 void ShaderCore::packUnorm2x16(Vector4f &d, const Vector4f &s0)
1175 {
1176 // round(clamp(c, 0.0, 1.0) * 65535.0)
1177 d.x = As<Float4>((Int4(Round(Min(Max(s0.x, Float4(0.0f)), Float4(1.0f)) * Float4(65535.0f))) & Int4(0xFFFF)) |
1178 ((Int4(Round(Min(Max(s0.y, Float4(0.0f)), Float4(1.0f)) * Float4(65535.0f))) & Int4(0xFFFF)) << 16));
1179 }
1180
1181 void ShaderCore::unpackSnorm2x16(Vector4f &dst, const Vector4f &s0)
1182 {
1183 // clamp(f / 32727.0, -1.0, 1.0)
1184 dst.x = Min(Max(Float4(As<Int4>((As<UInt4>(s0.x) & UInt4(0x0000FFFF)) << 16)) * Float4(1.0f / float(0x7FFF0000)), Float4(-1.0f)), Float4(1.0f));
1185 dst.y = Min(Max(Float4(As<Int4>(As<UInt4>(s0.x) & UInt4(0xFFFF0000))) * Float4(1.0f / float(0x7FFF0000)), Float4(-1.0f)), Float4(1.0f));
1186 }
1187
1188 void ShaderCore::unpackUnorm2x16(Vector4f &dst, const Vector4f &s0)
1189 {
1190 // f / 65535.0
1191 dst.x = Float4((As<UInt4>(s0.x) & UInt4(0x0000FFFF)) << 16) * Float4(1.0f / float(0xFFFF0000));
1192 dst.y = Float4(As<UInt4>(s0.x) & UInt4(0xFFFF0000)) * Float4(1.0f / float(0xFFFF0000));
1193 }
1194
Alexis Hetuc3d95f32015-09-23 12:27:32 -04001195 void ShaderCore::det2(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
1196 {
1197 dst.x = src0.x * src1.y - src0.y * src1.x;
1198 dst.y = dst.z = dst.w = dst.x;
1199 }
1200
1201 void ShaderCore::det3(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
1202 {
1203 crs(dst, src1, src2);
1204 dp3(dst, dst, src0);
1205 }
1206
1207 void ShaderCore::det4(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2, const Vector4f &src3)
1208 {
1209 dst.x = src2.z * src3.w - src2.w * src3.z;
1210 dst.y = src1.w * src3.z - src1.z * src3.w;
1211 dst.z = src1.z * src2.w - src1.w * src2.z;
1212 dst.x = src0.x * (src1.y * dst.x + src2.y * dst.y + src3.y * dst.z) -
1213 src0.y * (src1.x * dst.x + src2.x * dst.y + src3.x * dst.z) +
1214 src0.z * (src1.x * (src2.y * src3.w - src2.w * src3.y) +
1215 src2.x * (src1.w * src3.y - src1.y * src3.w) +
1216 src3.x * (src1.y * src2.w - src1.w * src2.y)) +
1217 src0.w * (src1.x * (src2.z * src3.y - src2.y * src3.z) +
1218 src2.x * (src1.y * src3.z - src1.z * src3.y) +
1219 src3.x * (src1.z * src2.y - src1.y * src2.z));
1220 dst.y = dst.z = dst.w = dst.x;
1221 }
1222
Alexis Hetuecad5192015-06-05 13:42:05 -04001223 void ShaderCore::frc(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001224 {
1225 dst.x = Frac(src.x);
1226 dst.y = Frac(src.y);
1227 dst.z = Frac(src.z);
1228 dst.w = Frac(src.w);
1229 }
1230
Alexis Hetuecad5192015-06-05 13:42:05 -04001231 void ShaderCore::trunc(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001232 {
1233 dst.x = Trunc(src.x);
1234 dst.y = Trunc(src.y);
1235 dst.z = Trunc(src.z);
1236 dst.w = Trunc(src.w);
1237 }
1238
Alexis Hetuecad5192015-06-05 13:42:05 -04001239 void ShaderCore::floor(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001240 {
1241 dst.x = Floor(src.x);
1242 dst.y = Floor(src.y);
1243 dst.z = Floor(src.z);
1244 dst.w = Floor(src.w);
1245 }
1246
Alexis Hetuecad5192015-06-05 13:42:05 -04001247 void ShaderCore::round(Vector4f &dst, const Vector4f &src)
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -04001248 {
1249 dst.x = Round(src.x);
1250 dst.y = Round(src.y);
1251 dst.z = Round(src.z);
1252 dst.w = Round(src.w);
1253 }
1254
Alexis Hetuecad5192015-06-05 13:42:05 -04001255 void ShaderCore::roundEven(Vector4f &dst, const Vector4f &src)
Alexis Hetu8e851c12015-06-04 11:30:54 -04001256 {
1257 // dst = round(src) + ((round(src) < src) * 2 - 1) * (fract(src) == 0.5) * isOdd(round(src));
1258 // ex.: 1.5: 2 + (0 * 2 - 1) * 1 * 0 = 2
1259 // 2.5: 3 + (0 * 2 - 1) * 1 * 1 = 2
1260 // -1.5: -2 + (1 * 2 - 1) * 1 * 0 = -2
1261 // -2.5: -3 + (1 * 2 - 1) * 1 * 1 = -2
1262 // Even if the round implementation rounds the other way:
1263 // 1.5: 1 + (1 * 2 - 1) * 1 * 1 = 2
1264 // 2.5: 2 + (1 * 2 - 1) * 1 * 0 = 2
1265 // -1.5: -1 + (0 * 2 - 1) * 1 * 1 = -2
1266 // -2.5: -2 + (0 * 2 - 1) * 1 * 0 = -2
1267 round(dst, src);
1268 dst.x += ((Float4(CmpLT(dst.x, src.x) & Int4(1)) * Float4(2.0f)) - Float4(1.0f)) * Float4(CmpEQ(Frac(src.x), Float4(0.5f)) & Int4(1)) * Float4(Int4(dst.x) & Int4(1));
1269 dst.y += ((Float4(CmpLT(dst.y, src.y) & Int4(1)) * Float4(2.0f)) - Float4(1.0f)) * Float4(CmpEQ(Frac(src.y), Float4(0.5f)) & Int4(1)) * Float4(Int4(dst.y) & Int4(1));
1270 dst.z += ((Float4(CmpLT(dst.z, src.z) & Int4(1)) * Float4(2.0f)) - Float4(1.0f)) * Float4(CmpEQ(Frac(src.z), Float4(0.5f)) & Int4(1)) * Float4(Int4(dst.z) & Int4(1));
1271 dst.w += ((Float4(CmpLT(dst.w, src.w) & Int4(1)) * Float4(2.0f)) - Float4(1.0f)) * Float4(CmpEQ(Frac(src.w), Float4(0.5f)) & Int4(1)) * Float4(Int4(dst.w) & Int4(1));
1272 }
1273
Alexis Hetuecad5192015-06-05 13:42:05 -04001274 void ShaderCore::ceil(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001275 {
1276 dst.x = Ceil(src.x);
1277 dst.y = Ceil(src.y);
1278 dst.z = Ceil(src.z);
1279 dst.w = Ceil(src.w);
1280 }
1281
Alexis Hetuecad5192015-06-05 13:42:05 -04001282 void ShaderCore::powx(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
John Bauman89401822014-05-06 15:04:28 -04001283 {
1284 Float4 pow = power(src0.x, src1.x, pp);
1285
1286 dst.x = pow;
1287 dst.y = pow;
1288 dst.z = pow;
1289 dst.w = pow;
1290 }
1291
Alexis Hetuecad5192015-06-05 13:42:05 -04001292 void ShaderCore::pow(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001293 {
1294 dst.x = power(src0.x, src1.x, pp);
1295 dst.y = power(src0.y, src1.y, pp);
1296 dst.z = power(src0.z, src1.z, pp);
1297 dst.w = power(src0.w, src1.w, pp);
1298 }
1299
Alexis Hetuecad5192015-06-05 13:42:05 -04001300 void ShaderCore::crs(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman89401822014-05-06 15:04:28 -04001301 {
1302 dst.x = src0.y * src1.z - src0.z * src1.y;
1303 dst.y = src0.z * src1.x - src0.x * src1.z;
1304 dst.z = src0.x * src1.y - src0.y * src1.x;
1305 }
1306
Alexis Hetuecad5192015-06-05 13:42:05 -04001307 void ShaderCore::forward1(Vector4f &dst, const Vector4f &N, const Vector4f &I, const Vector4f &Nref)
John Bauman19bac1e2014-05-06 15:23:49 -04001308 {
1309 Int4 flip = CmpNLT(Nref.x * I.x, Float4(0.0f)) & Int4(0x80000000);
1310
1311 dst.x = As<Float4>(flip ^ As<Int4>(N.x));
1312 }
1313
Alexis Hetuecad5192015-06-05 13:42:05 -04001314 void ShaderCore::forward2(Vector4f &dst, const Vector4f &N, const Vector4f &I, const Vector4f &Nref)
John Bauman19bac1e2014-05-06 15:23:49 -04001315 {
1316 Int4 flip = CmpNLT(dot2(Nref, I), Float4(0.0f)) & Int4(0x80000000);
1317
1318 dst.x = As<Float4>(flip ^ As<Int4>(N.x));
1319 dst.y = As<Float4>(flip ^ As<Int4>(N.y));
1320 }
1321
Alexis Hetuecad5192015-06-05 13:42:05 -04001322 void ShaderCore::forward3(Vector4f &dst, const Vector4f &N, const Vector4f &I, const Vector4f &Nref)
John Bauman19bac1e2014-05-06 15:23:49 -04001323 {
1324 Int4 flip = CmpNLT(dot3(Nref, I), Float4(0.0f)) & Int4(0x80000000);
1325
1326 dst.x = As<Float4>(flip ^ As<Int4>(N.x));
1327 dst.y = As<Float4>(flip ^ As<Int4>(N.y));
1328 dst.z = As<Float4>(flip ^ As<Int4>(N.z));
1329 }
1330
Alexis Hetuecad5192015-06-05 13:42:05 -04001331 void ShaderCore::forward4(Vector4f &dst, const Vector4f &N, const Vector4f &I, const Vector4f &Nref)
John Bauman19bac1e2014-05-06 15:23:49 -04001332 {
1333 Int4 flip = CmpNLT(dot4(Nref, I), Float4(0.0f)) & Int4(0x80000000);
1334
1335 dst.x = As<Float4>(flip ^ As<Int4>(N.x));
1336 dst.y = As<Float4>(flip ^ As<Int4>(N.y));
1337 dst.z = As<Float4>(flip ^ As<Int4>(N.z));
1338 dst.w = As<Float4>(flip ^ As<Int4>(N.w));
1339 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001340
Alexis Hetuecad5192015-06-05 13:42:05 -04001341 void ShaderCore::reflect1(Vector4f &dst, const Vector4f &I, const Vector4f &N)
John Bauman19bac1e2014-05-06 15:23:49 -04001342 {
1343 Float4 d = N.x * I.x;
1344
1345 dst.x = I.x - Float4(2.0f) * d * N.x;
1346 }
1347
Alexis Hetuecad5192015-06-05 13:42:05 -04001348 void ShaderCore::reflect2(Vector4f &dst, const Vector4f &I, const Vector4f &N)
John Bauman19bac1e2014-05-06 15:23:49 -04001349 {
1350 Float4 d = dot2(N, I);
1351
1352 dst.x = I.x - Float4(2.0f) * d * N.x;
1353 dst.y = I.y - Float4(2.0f) * d * N.y;
1354 }
1355
Alexis Hetuecad5192015-06-05 13:42:05 -04001356 void ShaderCore::reflect3(Vector4f &dst, const Vector4f &I, const Vector4f &N)
John Bauman19bac1e2014-05-06 15:23:49 -04001357 {
1358 Float4 d = dot3(N, I);
1359
1360 dst.x = I.x - Float4(2.0f) * d * N.x;
1361 dst.y = I.y - Float4(2.0f) * d * N.y;
1362 dst.z = I.z - Float4(2.0f) * d * N.z;
1363 }
1364
Alexis Hetuecad5192015-06-05 13:42:05 -04001365 void ShaderCore::reflect4(Vector4f &dst, const Vector4f &I, const Vector4f &N)
John Bauman19bac1e2014-05-06 15:23:49 -04001366 {
1367 Float4 d = dot4(N, I);
1368
1369 dst.x = I.x - Float4(2.0f) * d * N.x;
1370 dst.y = I.y - Float4(2.0f) * d * N.y;
1371 dst.z = I.z - Float4(2.0f) * d * N.z;
1372 dst.w = I.w - Float4(2.0f) * d * N.w;
1373 }
1374
Alexis Hetuecad5192015-06-05 13:42:05 -04001375 void ShaderCore::refract1(Vector4f &dst, const Vector4f &I, const Vector4f &N, const Float4 &eta)
John Bauman19bac1e2014-05-06 15:23:49 -04001376 {
1377 Float4 d = N.x * I.x;
1378 Float4 k = Float4(1.0f) - eta * eta * (Float4(1.0f) - d * d);
1379 Int4 pos = CmpNLT(k, Float4(0.0f));
1380 Float4 t = (eta * d + Sqrt(k));
1381
1382 dst.x = As<Float4>(pos & As<Int4>(eta * I.x - t * N.x));
1383 }
1384
Alexis Hetuecad5192015-06-05 13:42:05 -04001385 void ShaderCore::refract2(Vector4f &dst, const Vector4f &I, const Vector4f &N, const Float4 &eta)
John Bauman19bac1e2014-05-06 15:23:49 -04001386 {
1387 Float4 d = dot2(N, I);
1388 Float4 k = Float4(1.0f) - eta * eta * (Float4(1.0f) - d * d);
1389 Int4 pos = CmpNLT(k, Float4(0.0f));
1390 Float4 t = (eta * d + Sqrt(k));
1391
1392 dst.x = As<Float4>(pos & As<Int4>(eta * I.x - t * N.x));
1393 dst.y = As<Float4>(pos & As<Int4>(eta * I.y - t * N.y));
1394 }
1395
Alexis Hetuecad5192015-06-05 13:42:05 -04001396 void ShaderCore::refract3(Vector4f &dst, const Vector4f &I, const Vector4f &N, const Float4 &eta)
John Bauman19bac1e2014-05-06 15:23:49 -04001397 {
1398 Float4 d = dot3(N, I);
1399 Float4 k = Float4(1.0f) - eta * eta * (Float4(1.0f) - d * d);
1400 Int4 pos = CmpNLT(k, Float4(0.0f));
1401 Float4 t = (eta * d + Sqrt(k));
1402
1403 dst.x = As<Float4>(pos & As<Int4>(eta * I.x - t * N.x));
1404 dst.y = As<Float4>(pos & As<Int4>(eta * I.y - t * N.y));
1405 dst.z = As<Float4>(pos & As<Int4>(eta * I.z - t * N.z));
1406 }
1407
Alexis Hetuecad5192015-06-05 13:42:05 -04001408 void ShaderCore::refract4(Vector4f &dst, const Vector4f &I, const Vector4f &N, const Float4 &eta)
John Bauman19bac1e2014-05-06 15:23:49 -04001409 {
1410 Float4 d = dot4(N, I);
1411 Float4 k = Float4(1.0f) - eta * eta * (Float4(1.0f) - d * d);
1412 Int4 pos = CmpNLT(k, Float4(0.0f));
1413 Float4 t = (eta * d + Sqrt(k));
1414
1415 dst.x = As<Float4>(pos & As<Int4>(eta * I.x - t * N.x));
1416 dst.y = As<Float4>(pos & As<Int4>(eta * I.y - t * N.y));
1417 dst.z = As<Float4>(pos & As<Int4>(eta * I.z - t * N.z));
1418 dst.w = As<Float4>(pos & As<Int4>(eta * I.w - t * N.w));
1419 }
1420
Alexis Hetuecad5192015-06-05 13:42:05 -04001421 void ShaderCore::sgn(Vector4f &dst, const Vector4f &src)
John Bauman89401822014-05-06 15:04:28 -04001422 {
1423 sgn(dst.x, src.x);
1424 sgn(dst.y, src.y);
1425 sgn(dst.z, src.z);
1426 sgn(dst.w, src.w);
1427 }
1428
Alexis Hetu0f448072016-03-18 10:56:08 -04001429 void ShaderCore::isgn(Vector4f &dst, const Vector4f &src)
1430 {
1431 isgn(dst.x, src.x);
1432 isgn(dst.y, src.y);
1433 isgn(dst.z, src.z);
1434 isgn(dst.w, src.w);
1435 }
1436
Alexis Hetuecad5192015-06-05 13:42:05 -04001437 void ShaderCore::abs(Vector4f &dst, const Vector4f &src)
John Bauman89401822014-05-06 15:04:28 -04001438 {
1439 dst.x = Abs(src.x);
1440 dst.y = Abs(src.y);
1441 dst.z = Abs(src.z);
1442 dst.w = Abs(src.w);
1443 }
Alexis Hetu0f448072016-03-18 10:56:08 -04001444
1445 void ShaderCore::iabs(Vector4f &dst, const Vector4f &src)
1446 {
1447 dst.x = As<Float4>(Abs(As<Int4>(src.x)));
1448 dst.y = As<Float4>(Abs(As<Int4>(src.y)));
1449 dst.z = As<Float4>(Abs(As<Int4>(src.z)));
1450 dst.w = As<Float4>(Abs(As<Int4>(src.w)));
1451 }
1452
Alexis Hetuecad5192015-06-05 13:42:05 -04001453 void ShaderCore::nrm2(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001454 {
1455 Float4 dot = dot2(src, src);
1456 Float4 rsq = reciprocalSquareRoot(dot, false, pp);
John Bauman89401822014-05-06 15:04:28 -04001457
John Bauman19bac1e2014-05-06 15:23:49 -04001458 dst.x = src.x * rsq;
1459 dst.y = src.y * rsq;
1460 dst.z = src.z * rsq;
1461 dst.w = src.w * rsq;
1462 }
1463
Alexis Hetuecad5192015-06-05 13:42:05 -04001464 void ShaderCore::nrm3(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman89401822014-05-06 15:04:28 -04001465 {
1466 Float4 dot = dot3(src, src);
1467 Float4 rsq = reciprocalSquareRoot(dot, false, pp);
1468
1469 dst.x = src.x * rsq;
1470 dst.y = src.y * rsq;
1471 dst.z = src.z * rsq;
1472 dst.w = src.w * rsq;
1473 }
John Bauman19bac1e2014-05-06 15:23:49 -04001474
Alexis Hetuecad5192015-06-05 13:42:05 -04001475 void ShaderCore::nrm4(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman89401822014-05-06 15:04:28 -04001476 {
John Bauman19bac1e2014-05-06 15:23:49 -04001477 Float4 dot = dot4(src, src);
1478 Float4 rsq = reciprocalSquareRoot(dot, false, pp);
John Bauman89401822014-05-06 15:04:28 -04001479
John Bauman19bac1e2014-05-06 15:23:49 -04001480 dst.x = src.x * rsq;
1481 dst.y = src.y * rsq;
1482 dst.z = src.z * rsq;
1483 dst.w = src.w * rsq;
1484 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001485
Alexis Hetuecad5192015-06-05 13:42:05 -04001486 void ShaderCore::sincos(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001487 {
1488 dst.x = cosine_pi(src.x, pp);
1489 dst.y = sine_pi(src.x, pp);
John Bauman89401822014-05-06 15:04:28 -04001490 }
1491
Alexis Hetuecad5192015-06-05 13:42:05 -04001492 void ShaderCore::cos(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001493 {
1494 dst.x = cosine(src.x, pp);
1495 dst.y = cosine(src.y, pp);
1496 dst.z = cosine(src.z, pp);
1497 dst.w = cosine(src.w, pp);
1498 }
1499
Alexis Hetuecad5192015-06-05 13:42:05 -04001500 void ShaderCore::sin(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001501 {
1502 dst.x = sine(src.x, pp);
1503 dst.y = sine(src.y, pp);
1504 dst.z = sine(src.z, pp);
1505 dst.w = sine(src.w, pp);
1506 }
1507
Alexis Hetuecad5192015-06-05 13:42:05 -04001508 void ShaderCore::tan(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001509 {
1510 dst.x = tangent(src.x, pp);
1511 dst.y = tangent(src.y, pp);
1512 dst.z = tangent(src.z, pp);
1513 dst.w = tangent(src.w, pp);
1514 }
1515
Alexis Hetuecad5192015-06-05 13:42:05 -04001516 void ShaderCore::acos(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001517 {
1518 dst.x = arccos(src.x, pp);
1519 dst.y = arccos(src.y, pp);
1520 dst.z = arccos(src.z, pp);
1521 dst.w = arccos(src.w, pp);
1522 }
1523
Alexis Hetuecad5192015-06-05 13:42:05 -04001524 void ShaderCore::asin(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001525 {
1526 dst.x = arcsin(src.x, pp);
1527 dst.y = arcsin(src.y, pp);
1528 dst.z = arcsin(src.z, pp);
1529 dst.w = arcsin(src.w, pp);
1530 }
1531
Alexis Hetuecad5192015-06-05 13:42:05 -04001532 void ShaderCore::atan(Vector4f &dst, const Vector4f &src, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001533 {
1534 dst.x = arctan(src.x, pp);
1535 dst.y = arctan(src.y, pp);
1536 dst.z = arctan(src.z, pp);
1537 dst.w = arctan(src.w, pp);
1538 }
1539
Alexis Hetuecad5192015-06-05 13:42:05 -04001540 void ShaderCore::atan2(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, bool pp)
John Bauman19bac1e2014-05-06 15:23:49 -04001541 {
1542 dst.x = arctan(src0.x, src1.x, pp);
1543 dst.y = arctan(src0.y, src1.y, pp);
1544 dst.z = arctan(src0.z, src1.z, pp);
1545 dst.w = arctan(src0.w, src1.w, pp);
1546 }
1547
Alexis Hetuecad5192015-06-05 13:42:05 -04001548 void ShaderCore::cosh(Vector4f &dst, const Vector4f &src, bool pp)
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -04001549 {
1550 dst.x = cosineh(src.x, pp);
1551 dst.y = cosineh(src.y, pp);
1552 dst.z = cosineh(src.z, pp);
1553 dst.w = cosineh(src.w, pp);
1554 }
1555
Alexis Hetuecad5192015-06-05 13:42:05 -04001556 void ShaderCore::sinh(Vector4f &dst, const Vector4f &src, bool pp)
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -04001557 {
1558 dst.x = sineh(src.x, pp);
1559 dst.y = sineh(src.y, pp);
1560 dst.z = sineh(src.z, pp);
1561 dst.w = sineh(src.w, pp);
1562 }
1563
Alexis Hetuecad5192015-06-05 13:42:05 -04001564 void ShaderCore::tanh(Vector4f &dst, const Vector4f &src, bool pp)
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -04001565 {
1566 dst.x = tangenth(src.x, pp);
1567 dst.y = tangenth(src.y, pp);
1568 dst.z = tangenth(src.z, pp);
1569 dst.w = tangenth(src.w, pp);
1570 }
1571
Alexis Hetuecad5192015-06-05 13:42:05 -04001572 void ShaderCore::acosh(Vector4f &dst, const Vector4f &src, bool pp)
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -04001573 {
1574 dst.x = arccosh(src.x, pp);
1575 dst.y = arccosh(src.y, pp);
1576 dst.z = arccosh(src.z, pp);
1577 dst.w = arccosh(src.w, pp);
1578 }
1579
Alexis Hetuecad5192015-06-05 13:42:05 -04001580 void ShaderCore::asinh(Vector4f &dst, const Vector4f &src, bool pp)
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -04001581 {
1582 dst.x = arcsinh(src.x, pp);
1583 dst.y = arcsinh(src.y, pp);
1584 dst.z = arcsinh(src.z, pp);
1585 dst.w = arcsinh(src.w, pp);
1586 }
1587
Alexis Hetuecad5192015-06-05 13:42:05 -04001588 void ShaderCore::atanh(Vector4f &dst, const Vector4f &src, bool pp)
Alexis Hetu8b5f3ef2015-04-16 11:33:34 -04001589 {
1590 dst.x = arctanh(src.x, pp);
1591 dst.y = arctanh(src.y, pp);
1592 dst.z = arctanh(src.z, pp);
1593 dst.w = arctanh(src.w, pp);
1594 }
1595
Alexis Hetuecad5192015-06-05 13:42:05 -04001596 void ShaderCore::expp(Vector4f &dst, const Vector4f &src, unsigned short version)
John Bauman89401822014-05-06 15:04:28 -04001597 {
1598 if(version < 0x0200)
1599 {
John Bauman19bac1e2014-05-06 15:23:49 -04001600 Float4 frc = Frac(src.x);
John Bauman89401822014-05-06 15:04:28 -04001601 Float4 floor = src.x - frc;
1602
John Bauman19bac1e2014-05-06 15:23:49 -04001603 dst.x = exponential2(floor, true);
John Bauman89401822014-05-06 15:04:28 -04001604 dst.y = frc;
John Bauman19bac1e2014-05-06 15:23:49 -04001605 dst.z = exponential2(src.x, true);
1606 dst.w = Float4(1.0f);
John Bauman89401822014-05-06 15:04:28 -04001607 }
1608 else // Version >= 2.0
1609 {
John Bauman19bac1e2014-05-06 15:23:49 -04001610 exp2x(dst, src, true); // FIXME: 10-bit precision suffices
John Bauman89401822014-05-06 15:04:28 -04001611 }
1612 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001613
Alexis Hetuecad5192015-06-05 13:42:05 -04001614 void ShaderCore::logp(Vector4f &dst, const Vector4f &src, unsigned short version)
John Bauman89401822014-05-06 15:04:28 -04001615 {
1616 if(version < 0x0200)
1617 {
1618 Float4 tmp0;
1619 Float4 tmp1;
1620 Float4 t;
1621 Int4 r;
1622
1623 tmp0 = Abs(src.x);
1624 tmp1 = tmp0;
1625
1626 // X component
John Bauman19bac1e2014-05-06 15:23:49 -04001627 r = As<Int4>(As<UInt4>(tmp0) >> 23) - Int4(127);
John Bauman89401822014-05-06 15:04:28 -04001628 dst.x = Float4(r);
1629
1630 // Y component
1631 dst.y = As<Float4>((As<Int4>(tmp1) & Int4(0x007FFFFF)) | As<Int4>(Float4(1.0f)));
1632
1633 // Z component
John Bauman19bac1e2014-05-06 15:23:49 -04001634 dst.z = logarithm2(src.x, true, true);
John Bauman89401822014-05-06 15:04:28 -04001635
1636 // W component
1637 dst.w = 1.0f;
1638 }
1639 else
1640 {
John Bauman19bac1e2014-05-06 15:23:49 -04001641 log2x(dst, src, true);
John Bauman89401822014-05-06 15:04:28 -04001642 }
1643 }
Nicolas Capens0bac2852016-05-07 06:09:58 -04001644
Alexis Hetuecad5192015-06-05 13:42:05 -04001645 void ShaderCore::cmp0(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
John Bauman89401822014-05-06 15:04:28 -04001646 {
John Bauman19bac1e2014-05-06 15:23:49 -04001647 cmp0(dst.x, src0.x, src1.x, src2.x);
1648 cmp0(dst.y, src0.y, src1.y, src2.y);
1649 cmp0(dst.z, src0.z, src1.z, src2.z);
1650 cmp0(dst.w, src0.w, src1.w, src2.w);
John Bauman89401822014-05-06 15:04:28 -04001651 }
John Bauman89401822014-05-06 15:04:28 -04001652
Alexis Hetuecad5192015-06-05 13:42:05 -04001653 void ShaderCore::select(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2)
John Bauman19bac1e2014-05-06 15:23:49 -04001654 {
1655 select(dst.x, As<Int4>(src0.x), src1.x, src2.x);
1656 select(dst.y, As<Int4>(src0.y), src1.y, src2.y);
1657 select(dst.z, As<Int4>(src0.z), src1.z, src2.z);
1658 select(dst.w, As<Int4>(src0.w), src1.w, src2.w);
1659 }
1660
Alexis Hetuecad5192015-06-05 13:42:05 -04001661 void ShaderCore::extract(Float4 &dst, const Vector4f &src0, const Float4 &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001662 {
Alexis Hetuc4f2c292015-08-18 15:43:09 -04001663 select(dst, CmpEQ(As<Int4>(src1), Int4(1)), src0.y, src0.x);
1664 select(dst, CmpEQ(As<Int4>(src1), Int4(2)), src0.z, dst);
1665 select(dst, CmpEQ(As<Int4>(src1), Int4(3)), src0.w, dst);
John Bauman19bac1e2014-05-06 15:23:49 -04001666 }
1667
Alexis Hetuecad5192015-06-05 13:42:05 -04001668 void ShaderCore::insert(Vector4f &dst, const Vector4f &src, const Float4 &element, const Float4 &index)
John Bauman19bac1e2014-05-06 15:23:49 -04001669 {
Alexis Hetuc4f2c292015-08-18 15:43:09 -04001670 select(dst.x, CmpEQ(As<Int4>(index), Int4(0)), element, src.x);
1671 select(dst.y, CmpEQ(As<Int4>(index), Int4(1)), element, src.y);
1672 select(dst.z, CmpEQ(As<Int4>(index), Int4(2)), element, src.z);
1673 select(dst.w, CmpEQ(As<Int4>(index), Int4(3)), element, src.w);
John Bauman89401822014-05-06 15:04:28 -04001674 }
1675
Alexis Hetuecad5192015-06-05 13:42:05 -04001676 void ShaderCore::sgn(Float4 &dst, const Float4 &src)
John Bauman89401822014-05-06 15:04:28 -04001677 {
John Bauman19bac1e2014-05-06 15:23:49 -04001678 Int4 neg = As<Int4>(CmpLT(src, Float4(-0.0f))) & As<Int4>(Float4(-1.0f));
1679 Int4 pos = As<Int4>(CmpNLE(src, Float4(+0.0f))) & As<Int4>(Float4(1.0f));
John Bauman89401822014-05-06 15:04:28 -04001680 dst = As<Float4>(neg | pos);
1681 }
1682
Alexis Hetu0f448072016-03-18 10:56:08 -04001683 void ShaderCore::isgn(Float4 &dst, const Float4 &src)
1684 {
1685 Int4 neg = CmpLT(As<Int4>(src), Int4(0)) & Int4(-1);
1686 Int4 pos = CmpNLE(As<Int4>(src), Int4(0)) & Int4(1);
1687 dst = As<Float4>(neg | pos);
1688 }
1689
Alexis Hetuecad5192015-06-05 13:42:05 -04001690 void ShaderCore::cmp0(Float4 &dst, const Float4 &src0, const Float4 &src1, const Float4 &src2)
John Bauman89401822014-05-06 15:04:28 -04001691 {
John Bauman19bac1e2014-05-06 15:23:49 -04001692 Int4 pos = CmpLE(Float4(0.0f), src0);
1693 select(dst, pos, src1, src2);
John Bauman89401822014-05-06 15:04:28 -04001694 }
1695
Alexis Hetuc4f2c292015-08-18 15:43:09 -04001696 void ShaderCore::cmp0i(Float4 &dst, const Float4 &src0, const Float4 &src1, const Float4 &src2)
1697 {
1698 Int4 pos = CmpEQ(Int4(0), As<Int4>(src0));
1699 select(dst, pos, src1, src2);
1700 }
1701
Alexis Hetuecad5192015-06-05 13:42:05 -04001702 void ShaderCore::select(Float4 &dst, RValue<Int4> src0, const Float4 &src1, const Float4 &src2)
John Bauman19bac1e2014-05-06 15:23:49 -04001703 {
1704 // FIXME: LLVM vector select
Tom Anderson69bc6e82017-03-20 11:54:29 -07001705 dst = As<Float4>((src0 & As<Int4>(src1)) | (~src0 & As<Int4>(src2)));
John Bauman19bac1e2014-05-06 15:23:49 -04001706 }
1707
Alexis Hetuecad5192015-06-05 13:42:05 -04001708 void ShaderCore::cmp(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001709 {
1710 switch(control)
1711 {
John Bauman19bac1e2014-05-06 15:23:49 -04001712 case Shader::CONTROL_GT:
John Bauman89401822014-05-06 15:04:28 -04001713 dst.x = As<Float4>(CmpNLE(src0.x, src1.x));
1714 dst.y = As<Float4>(CmpNLE(src0.y, src1.y));
1715 dst.z = As<Float4>(CmpNLE(src0.z, src1.z));
1716 dst.w = As<Float4>(CmpNLE(src0.w, src1.w));
1717 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001718 case Shader::CONTROL_EQ:
John Bauman89401822014-05-06 15:04:28 -04001719 dst.x = As<Float4>(CmpEQ(src0.x, src1.x));
1720 dst.y = As<Float4>(CmpEQ(src0.y, src1.y));
1721 dst.z = As<Float4>(CmpEQ(src0.z, src1.z));
1722 dst.w = As<Float4>(CmpEQ(src0.w, src1.w));
1723 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001724 case Shader::CONTROL_GE:
John Bauman89401822014-05-06 15:04:28 -04001725 dst.x = As<Float4>(CmpNLT(src0.x, src1.x));
1726 dst.y = As<Float4>(CmpNLT(src0.y, src1.y));
1727 dst.z = As<Float4>(CmpNLT(src0.z, src1.z));
1728 dst.w = As<Float4>(CmpNLT(src0.w, src1.w));
1729 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001730 case Shader::CONTROL_LT:
John Bauman89401822014-05-06 15:04:28 -04001731 dst.x = As<Float4>(CmpLT(src0.x, src1.x));
1732 dst.y = As<Float4>(CmpLT(src0.y, src1.y));
1733 dst.z = As<Float4>(CmpLT(src0.z, src1.z));
1734 dst.w = As<Float4>(CmpLT(src0.w, src1.w));
1735 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001736 case Shader::CONTROL_NE:
John Bauman89401822014-05-06 15:04:28 -04001737 dst.x = As<Float4>(CmpNEQ(src0.x, src1.x));
1738 dst.y = As<Float4>(CmpNEQ(src0.y, src1.y));
1739 dst.z = As<Float4>(CmpNEQ(src0.z, src1.z));
1740 dst.w = As<Float4>(CmpNEQ(src0.w, src1.w));
1741 break;
John Bauman19bac1e2014-05-06 15:23:49 -04001742 case Shader::CONTROL_LE:
John Bauman89401822014-05-06 15:04:28 -04001743 dst.x = As<Float4>(CmpLE(src0.x, src1.x));
1744 dst.y = As<Float4>(CmpLE(src0.y, src1.y));
1745 dst.z = As<Float4>(CmpLE(src0.z, src1.z));
1746 dst.w = As<Float4>(CmpLE(src0.w, src1.w));
1747 break;
1748 default:
1749 ASSERT(false);
1750 }
1751 }
John Bauman19bac1e2014-05-06 15:23:49 -04001752
Alexis Hetuecad5192015-06-05 13:42:05 -04001753 void ShaderCore::icmp(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, Control control)
John Bauman19bac1e2014-05-06 15:23:49 -04001754 {
1755 switch(control)
1756 {
1757 case Shader::CONTROL_GT:
1758 dst.x = As<Float4>(CmpNLE(As<Int4>(src0.x), As<Int4>(src1.x)));
1759 dst.y = As<Float4>(CmpNLE(As<Int4>(src0.y), As<Int4>(src1.y)));
1760 dst.z = As<Float4>(CmpNLE(As<Int4>(src0.z), As<Int4>(src1.z)));
1761 dst.w = As<Float4>(CmpNLE(As<Int4>(src0.w), As<Int4>(src1.w)));
1762 break;
1763 case Shader::CONTROL_EQ:
1764 dst.x = As<Float4>(CmpEQ(As<Int4>(src0.x), As<Int4>(src1.x)));
1765 dst.y = As<Float4>(CmpEQ(As<Int4>(src0.y), As<Int4>(src1.y)));
1766 dst.z = As<Float4>(CmpEQ(As<Int4>(src0.z), As<Int4>(src1.z)));
1767 dst.w = As<Float4>(CmpEQ(As<Int4>(src0.w), As<Int4>(src1.w)));
1768 break;
1769 case Shader::CONTROL_GE:
1770 dst.x = As<Float4>(CmpNLT(As<Int4>(src0.x), As<Int4>(src1.x)));
1771 dst.y = As<Float4>(CmpNLT(As<Int4>(src0.y), As<Int4>(src1.y)));
1772 dst.z = As<Float4>(CmpNLT(As<Int4>(src0.z), As<Int4>(src1.z)));
1773 dst.w = As<Float4>(CmpNLT(As<Int4>(src0.w), As<Int4>(src1.w)));
1774 break;
1775 case Shader::CONTROL_LT:
1776 dst.x = As<Float4>(CmpLT(As<Int4>(src0.x), As<Int4>(src1.x)));
1777 dst.y = As<Float4>(CmpLT(As<Int4>(src0.y), As<Int4>(src1.y)));
1778 dst.z = As<Float4>(CmpLT(As<Int4>(src0.z), As<Int4>(src1.z)));
1779 dst.w = As<Float4>(CmpLT(As<Int4>(src0.w), As<Int4>(src1.w)));
1780 break;
1781 case Shader::CONTROL_NE:
1782 dst.x = As<Float4>(CmpNEQ(As<Int4>(src0.x), As<Int4>(src1.x)));
1783 dst.y = As<Float4>(CmpNEQ(As<Int4>(src0.y), As<Int4>(src1.y)));
1784 dst.z = As<Float4>(CmpNEQ(As<Int4>(src0.z), As<Int4>(src1.z)));
1785 dst.w = As<Float4>(CmpNEQ(As<Int4>(src0.w), As<Int4>(src1.w)));
1786 break;
1787 case Shader::CONTROL_LE:
1788 dst.x = As<Float4>(CmpLE(As<Int4>(src0.x), As<Int4>(src1.x)));
1789 dst.y = As<Float4>(CmpLE(As<Int4>(src0.y), As<Int4>(src1.y)));
1790 dst.z = As<Float4>(CmpLE(As<Int4>(src0.z), As<Int4>(src1.z)));
1791 dst.w = As<Float4>(CmpLE(As<Int4>(src0.w), As<Int4>(src1.w)));
1792 break;
1793 default:
1794 ASSERT(false);
1795 }
1796 }
1797
Alexis Hetuc4f2c292015-08-18 15:43:09 -04001798 void ShaderCore::ucmp(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, Control control)
1799 {
1800 switch(control)
1801 {
1802 case Shader::CONTROL_GT:
1803 dst.x = As<Float4>(CmpNLE(As<UInt4>(src0.x), As<UInt4>(src1.x)));
1804 dst.y = As<Float4>(CmpNLE(As<UInt4>(src0.y), As<UInt4>(src1.y)));
1805 dst.z = As<Float4>(CmpNLE(As<UInt4>(src0.z), As<UInt4>(src1.z)));
1806 dst.w = As<Float4>(CmpNLE(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1807 break;
1808 case Shader::CONTROL_EQ:
1809 dst.x = As<Float4>(CmpEQ(As<UInt4>(src0.x), As<UInt4>(src1.x)));
1810 dst.y = As<Float4>(CmpEQ(As<UInt4>(src0.y), As<UInt4>(src1.y)));
1811 dst.z = As<Float4>(CmpEQ(As<UInt4>(src0.z), As<UInt4>(src1.z)));
1812 dst.w = As<Float4>(CmpEQ(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1813 break;
1814 case Shader::CONTROL_GE:
1815 dst.x = As<Float4>(CmpNLT(As<UInt4>(src0.x), As<UInt4>(src1.x)));
1816 dst.y = As<Float4>(CmpNLT(As<UInt4>(src0.y), As<UInt4>(src1.y)));
1817 dst.z = As<Float4>(CmpNLT(As<UInt4>(src0.z), As<UInt4>(src1.z)));
1818 dst.w = As<Float4>(CmpNLT(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1819 break;
1820 case Shader::CONTROL_LT:
1821 dst.x = As<Float4>(CmpLT(As<UInt4>(src0.x), As<UInt4>(src1.x)));
1822 dst.y = As<Float4>(CmpLT(As<UInt4>(src0.y), As<UInt4>(src1.y)));
1823 dst.z = As<Float4>(CmpLT(As<UInt4>(src0.z), As<UInt4>(src1.z)));
1824 dst.w = As<Float4>(CmpLT(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1825 break;
1826 case Shader::CONTROL_NE:
1827 dst.x = As<Float4>(CmpNEQ(As<UInt4>(src0.x), As<UInt4>(src1.x)));
1828 dst.y = As<Float4>(CmpNEQ(As<UInt4>(src0.y), As<UInt4>(src1.y)));
1829 dst.z = As<Float4>(CmpNEQ(As<UInt4>(src0.z), As<UInt4>(src1.z)));
1830 dst.w = As<Float4>(CmpNEQ(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1831 break;
1832 case Shader::CONTROL_LE:
1833 dst.x = As<Float4>(CmpLE(As<UInt4>(src0.x), As<UInt4>(src1.x)));
1834 dst.y = As<Float4>(CmpLE(As<UInt4>(src0.y), As<UInt4>(src1.y)));
1835 dst.z = As<Float4>(CmpLE(As<UInt4>(src0.z), As<UInt4>(src1.z)));
1836 dst.w = As<Float4>(CmpLE(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1837 break;
1838 default:
1839 ASSERT(false);
1840 }
1841 }
1842
Alexis Hetuecad5192015-06-05 13:42:05 -04001843 void ShaderCore::all(Float4 &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001844 {
1845 dst = As<Float4>(As<Int4>(src.x) & As<Int4>(src.y) & As<Int4>(src.z) & As<Int4>(src.w));
1846 }
1847
Alexis Hetuecad5192015-06-05 13:42:05 -04001848 void ShaderCore::any(Float4 &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001849 {
1850 dst = As<Float4>(As<Int4>(src.x) | As<Int4>(src.y) | As<Int4>(src.z) | As<Int4>(src.w));
1851 }
1852
Alexis Hetu24f454e2016-08-31 17:22:13 -04001853 void ShaderCore::bitwise_not(Vector4f &dst, const Vector4f &src)
John Bauman19bac1e2014-05-06 15:23:49 -04001854 {
1855 dst.x = As<Float4>(As<Int4>(src.x) ^ Int4(0xFFFFFFFF));
1856 dst.y = As<Float4>(As<Int4>(src.y) ^ Int4(0xFFFFFFFF));
1857 dst.z = As<Float4>(As<Int4>(src.z) ^ Int4(0xFFFFFFFF));
1858 dst.w = As<Float4>(As<Int4>(src.w) ^ Int4(0xFFFFFFFF));
1859 }
1860
Alexis Hetu24f454e2016-08-31 17:22:13 -04001861 void ShaderCore::bitwise_or(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001862 {
Alexis Hetuc4f2c292015-08-18 15:43:09 -04001863 dst.x = As<Float4>(As<Int4>(src0.x) | As<Int4>(src1.x));
1864 dst.y = As<Float4>(As<Int4>(src0.y) | As<Int4>(src1.y));
1865 dst.z = As<Float4>(As<Int4>(src0.z) | As<Int4>(src1.z));
1866 dst.w = As<Float4>(As<Int4>(src0.w) | As<Int4>(src1.w));
John Bauman19bac1e2014-05-06 15:23:49 -04001867 }
1868
Alexis Hetu24f454e2016-08-31 17:22:13 -04001869 void ShaderCore::bitwise_xor(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001870 {
Alexis Hetuc4f2c292015-08-18 15:43:09 -04001871 dst.x = As<Float4>(As<Int4>(src0.x) ^ As<Int4>(src1.x));
1872 dst.y = As<Float4>(As<Int4>(src0.y) ^ As<Int4>(src1.y));
1873 dst.z = As<Float4>(As<Int4>(src0.z) ^ As<Int4>(src1.z));
1874 dst.w = As<Float4>(As<Int4>(src0.w) ^ As<Int4>(src1.w));
John Bauman19bac1e2014-05-06 15:23:49 -04001875 }
1876
Alexis Hetu24f454e2016-08-31 17:22:13 -04001877 void ShaderCore::bitwise_and(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001878 {
Alexis Hetuc4f2c292015-08-18 15:43:09 -04001879 dst.x = As<Float4>(As<Int4>(src0.x) & As<Int4>(src1.x));
1880 dst.y = As<Float4>(As<Int4>(src0.y) & As<Int4>(src1.y));
1881 dst.z = As<Float4>(As<Int4>(src0.z) & As<Int4>(src1.z));
1882 dst.w = As<Float4>(As<Int4>(src0.w) & As<Int4>(src1.w));
1883 }
1884
1885 void ShaderCore::equal(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
1886 {
1887 dst.x = As<Float4>(CmpEQ(As<UInt4>(src0.x), As<UInt4>(src1.x)) &
1888 CmpEQ(As<UInt4>(src0.y), As<UInt4>(src1.y)) &
1889 CmpEQ(As<UInt4>(src0.z), As<UInt4>(src1.z)) &
1890 CmpEQ(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1891 dst.y = dst.x;
1892 dst.z = dst.x;
1893 dst.w = dst.x;
1894 }
1895
1896 void ShaderCore::notEqual(Vector4f &dst, const Vector4f &src0, const Vector4f &src1)
1897 {
1898 dst.x = As<Float4>(CmpNEQ(As<UInt4>(src0.x), As<UInt4>(src1.x)) |
1899 CmpNEQ(As<UInt4>(src0.y), As<UInt4>(src1.y)) |
1900 CmpNEQ(As<UInt4>(src0.z), As<UInt4>(src1.z)) |
1901 CmpNEQ(As<UInt4>(src0.w), As<UInt4>(src1.w)));
1902 dst.y = dst.x;
1903 dst.z = dst.x;
1904 dst.w = dst.x;
John Bauman19bac1e2014-05-06 15:23:49 -04001905 }
John Bauman89401822014-05-06 15:04:28 -04001906}