Reimplement the post-increment/decrement functions.
[mesa.git] / src / mesa / shader / slang / library / slang_core.gc
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 //
26 // This file defines nearly all constructors and operators for built-in data
27 // types, using extended language syntax. In general, compiler treats
28 // constructors and operators as ordinary functions with some exceptions.
29 // For example, the language does not allow functions to be called in
30 // constant expressions - here the exception is made to allow it.
31 //
32 // Each implementation provides its own version of this file. Each
33 // implementation can define the required set of operators and constructors
34 // in its own fashion.
35 //
36 // The extended language syntax is only present when compiling this file.
37 // It is implicitly included at the very beginning of the compiled shader,
38 // so no built-in functions can be used.
39 //
40 // To communicate with the implementation, a special extended "__asm" keyword
41 // is used, followed by an instruction name (any valid identifier), a
42 // destination variable identifier and a list of zero or more source
43 // variable identifiers.
44 //
45 // A variable identifier is a variable name declared earlier in the code
46 // (as a function parameter, local or global variable).
47 //
48 // An instruction name designates an instruction that must be exported
49 // by the implementation. Each instruction receives data from source
50 // variable identifiers and returns data in the destination variable
51 // identifier.
52 //
53 // It is up to the implementation how to define a particular operator
54 // or constructor. If it is expected to being used rarely, it can be
55 // defined in terms of other operators and constructors,
56 // for example:
57 //
58 // ivec2 __operator + (const ivec2 x, const ivec2 y) {
59 // return ivec2 (x[0] + y[0], x[1] + y[1]);
60 // }
61 //
62 // If a particular operator or constructor is expected to be used very
63 // often or is an atomic operation (that is, an operation that cannot be
64 // expressed in terms of other operations or would create a dependency
65 // cycle) it must be defined using one or more __asm constructs.
66 //
67 // Each implementation must define constructors for all scalar types
68 // (bool, float, int). There are 9 scalar-to-scalar constructors
69 // (including identity constructors). However, since the language
70 // introduces special constructors (like matrix constructor with a single
71 // scalar value), implementations must also implement these cases.
72 // The compiler provides the following algorithm when resolving a constructor:
73 // - try to find a constructor with a prototype matching ours,
74 // - if no constructor is found and this is a scalar-to-scalar constructor,
75 // raise an error,
76 // - if a constructor is found, execute it and return,
77 // - count the size of the constructor parameter list - if it is less than
78 // the size of our constructor's type, raise an error,
79 // - for each parameter in the list do a recursive constructor matching for
80 // appropriate scalar fields in the constructed variable,
81 //
82 // Each implementation must also define a set of operators that deal with
83 // built-in data types.
84 // There are four kinds of operators:
85 // 1) Operators that are implemented only by the compiler: "()" (function
86 // call), "," (sequence) and "?:" (selection).
87 // 2) Operators that are implemented by the compiler by expressing it in
88 // terms of other operators:
89 // - "." (field selection) - translated to subscript access,
90 // - "&&" (logical and) - translated to "<left_expr> ? <right_expr> :
91 // false",
92 // - "||" (logical or) - translated to "<left_expr> ? true : <right_expr>",
93 // 3) Operators that can be defined by the implementation and if the required
94 // prototype is not found, standard behaviour is used:
95 // - "==", "!=", "=" (equality, assignment) - compare or assign
96 // matching fields one-by-one;
97 // note that at least operators for scalar data types must be defined
98 // by the implementation to get it work,
99 // 4) All other operators not mentioned above. If no required prototype is
100 // found, an error is raised. An implementation must follow the language
101 // specification to provide all valid operator prototypes.
102 //
103
104 //bp:
105 //vec4 vec4(const float a1, const float b1, const float c1, const float d1)
106 //{
107 // __retVal.x = a1;
108 // __retVal.y = b1;
109 // __retVal.z = c1;
110 // __retVal.w = d1;
111 //}
112
113 ////
114 //// Assorted constructors
115 ////
116
117 int __constructor (const float f) {
118 int i;
119 __asm float_to_int i, f;
120 return i;
121 }
122
123 bool __constructor (const int i) {
124 return i != 0;
125 }
126
127 bool __constructor (const float f) {
128 return f != 0.0;
129 }
130
131 int __constructor (const bool b) {
132 return b ? 1 : 0;
133 }
134
135 float __constructor (const bool b) {
136 return b ? 1.0 : 0.0;
137 }
138
139 float __constructor (const int i) {
140 float f;
141 __asm int_to_float f, i;
142 return f;
143 }
144
145 bool __constructor (const bool b) {
146 return b;
147 }
148
149 int __constructor (const int i) {
150 return i;
151 }
152
153 float __constructor(const float f)
154 {
155 __retVal = f.x;
156 }
157
158 vec2 __constructor(const float f)
159 {
160 __retVal.xy = f.xx;
161 }
162
163 vec2 __constructor (const int i) {
164 float x;
165 __asm int_to_float x, i;
166 return vec2 (x);
167 }
168
169 vec2 __constructor (const bool b) {
170 return vec2 (b ? 1.0 : 0.0);
171 }
172
173 vec3 __constructor(const float f)
174 {
175 __retVal.xyz = f.xxx;
176 }
177
178 vec3 __constructor (const int i) {
179 float x;
180 __asm int_to_float x, i;
181 return vec3 (x);
182 }
183
184 vec3 __constructor (const bool b) {
185 return vec3 (b ? 1.0 : 0.0);
186 }
187
188 vec3 __constructor(const vec4 v)
189 {
190 __retVal.xyz = v.xyz;
191 }
192
193
194 vec4 __constructor(const float f)
195 {
196 __retVal.xyzw = f.xxxx;
197 }
198
199 vec4 __constructor (const int i) {
200 float x;
201 __asm int_to_float x, i;
202 return vec4 (x);
203 }
204
205 vec4 __constructor (const bool b) {
206 return vec4 (b ? 1.0 : 0.0);
207 }
208
209 vec4 __constructor(const vec3 v3, const float f)
210 {
211 __retVal.xyz = v3;
212 __retVal.w = f;
213 }
214
215
216
217 ivec2 __constructor (const int i) {
218 return ivec2 (i, i);
219 }
220
221 ivec2 __constructor (const float f) {
222 return ivec2 (int (f));
223 }
224
225 ivec2 __constructor (const bool b) {
226 return ivec2 (int (b));
227 }
228
229 ivec3 __constructor (const int i) {
230 return ivec3 (i, i, i);
231 }
232
233 ivec3 __constructor (const float f) {
234 return ivec3 (int (f));
235 }
236
237 ivec3 __constructor (const bool b) {
238 return ivec3 (int (b));
239 }
240
241 ivec4 __constructor (const int i) {
242 return ivec4 (i, i, i, i);
243 }
244
245 ivec4 __constructor (const float f) {
246 return ivec4 (int (f));
247 }
248
249 ivec4 __constructor (const bool b) {
250 return ivec4 (int (b));
251 }
252
253 bvec2 __constructor (const bool b) {
254 return bvec2 (b, b);
255 }
256
257 bvec2 __constructor (const float f) {
258 return bvec2 (bool (f));
259 }
260
261 bvec2 __constructor (const int i) {
262 return bvec2 (bool (i));
263 }
264
265 bvec3 __constructor (const bool b) {
266 return bvec3 (b, b, b);
267 }
268
269 bvec3 __constructor (const float f) {
270 return bvec3 (bool (f));
271 }
272
273 bvec3 __constructor (const int i) {
274 return bvec3 (bool (i));
275 }
276
277 bvec4 __constructor (const bool b) {
278 return bvec4 (b, b, b, b);
279 }
280
281 bvec4 __constructor (const float f) {
282 return bvec4 (bool (f));
283 }
284
285 bvec4 __constructor (const int i) {
286 return bvec4 (bool (i));
287 }
288
289
290
291 //// mat2 constructors
292
293 mat2 __constructor (const float f) {
294 return mat2 (f, 0.0, 0.0, f);
295 }
296
297 mat2 __constructor (const int i) {
298 float x;
299 __asm int_to_float x, i;
300 return mat2 (x);
301 }
302
303 mat2 __constructor (const bool b) {
304 return mat2 (b ? 1.0 : 0.0);
305 }
306
307
308 //// mat3 constructors
309
310 mat3 __constructor (const float f) {
311 return mat3 (f, 0.0, 0.0, 0.0, f, 0.0, 0.0, 0.0, f);
312 }
313
314 mat3 __constructor (const int i) {
315 float x;
316 __asm int_to_float x, i;
317 return mat3 (x);
318 }
319
320 mat3 __constructor (const bool b) {
321 return mat3 (b ? 1.0 : 0.0);
322 }
323
324
325 //// mat4 constructors
326
327 mat4 __constructor (const float f) {
328 return mat4 (f, 0.0, 0.0, 0.0, 0.0, f, 0.0, 0.0, 0.0, 0.0, f, 0.0, 0.0, 0.0, 0.0, f);
329 }
330
331 mat4 __constructor (const int i) {
332 float x;
333 __asm int_to_float x, i;
334 return mat4 (x);
335 }
336
337 mat4 __constructor (const bool b) {
338 return mat4 (b ? 1.0 : 0.0);
339 }
340
341 mat4 __constructor (const vec4 r0, const vec4 r1, const vec4 r2, const vec4 r3)
342 {
343 __retVal[0] = r0;
344 __retVal[1] = r1;
345 __retVal[2] = r2;
346 __retVal[3] = r3;
347 }
348
349
350
351 //// Basic int operators
352
353 int __operator + (const int a, const int b)
354 {
355 float x, y;
356 int c;
357 __asm int_to_float x, a;
358 __asm int_to_float y, b;
359 __asm vec4_add x.x, x.x, y.x;
360 __asm float_to_int c, x;
361 return c;
362 }
363
364 int __operator - (const int a, const int b)
365 {
366 float x, y;
367 int c;
368 __asm int_to_float x, a;
369 __asm int_to_float y, b;
370 __asm float_negate y, y;
371 __asm vec4_add x.x, x.x, y.x;
372 __asm float_to_int c, x;
373 return c;
374 }
375
376 int __operator * (const int a, const int b)
377 {
378 float x, y;
379 int c;
380 __asm int_to_float x, a;
381 __asm int_to_float y, b;
382 __asm float_multiply x, x, y;
383 __asm float_to_int c, x;
384 return c;
385 }
386
387 int __operator / (const int a, const int b)
388 {
389 float x, y;
390 int c;
391 __asm int_to_float x, a;
392 __asm int_to_float y, b;
393 __asm float_divide x, x, y;
394 __asm float_to_int c, x;
395 return c;
396 }
397
398
399 //// Basic ivec2 operators
400
401 ivec2 __operator + (const ivec2 v, const ivec2 u)
402 {
403 return ivec2 (v.x + u.x, v.y + u.y);
404 }
405
406 ivec2 __operator - (const ivec2 v, const ivec2 u)
407 {
408 return ivec2 (v.x - u.x, v.y - u.y);
409 }
410
411 ivec2 __operator * (const ivec2 v, const ivec2 u)
412 {
413 return ivec2 (v.x * u.x, v.y * u.y);
414 }
415
416 ivec2 __operator / (const ivec2 v, const ivec2 u)
417 {
418 return ivec2 (v.x / u.x, v.y / u.y);
419 }
420
421
422 //// Basic ivec3 operators
423
424 ivec3 __operator + (const ivec3 v, const ivec3 u)
425 {
426 return ivec3 (v.x + u.x, v.y + u.y, v.z + u.z);
427 }
428
429 ivec3 __operator - (const ivec3 v, const ivec3 u)
430 {
431 return ivec3 (v.x - u.x, v.y - u.y, v.z - u.z);
432 }
433
434 ivec3 __operator * (const ivec3 v, const ivec3 u)
435 {
436 return ivec3 (v.x * u.x, v.y * u.y, v.z * u.z);
437 }
438
439 ivec3 __operator / (const ivec3 v, const ivec3 u)
440 {
441 return ivec3 (v.x / u.x, v.y / u.y, v.z / u.z);
442 }
443
444
445 //// Basic ivec4 operators
446
447 ivec4 __operator + (const ivec4 v, const ivec4 u)
448 {
449 return ivec4 (v.x + u.x, v.y + u.y, v.z + u.z, v.w + u.w);
450 }
451
452 ivec4 __operator - (const ivec4 v, const ivec4 u)
453 {
454 return ivec4 (v.x - u.x, v.y - u.y, v.z - u.z, v.w - u.w);
455 }
456
457 ivec4 __operator * (const ivec4 v, const ivec4 u)
458 {
459 return ivec4 (v.x * u.x, v.y * u.y, v.z * u.z, v.w * u.w);
460 }
461
462 ivec4 __operator / (const ivec4 v, const ivec4 u)
463 {
464 return ivec4 (v.x / u.x, v.y / u.y, v.z / u.z, v.w / u.w);
465 }
466
467
468 //// Basic float operators
469
470 float __operator + (const float a, const float b)
471 {
472 __asm vec4_add __retVal.x, a, b;
473 }
474
475 float __operator - (const float a, const float b)
476 {
477 __asm vec4_subtract __retVal.x, a, b;
478 }
479
480 float __operator * (const float a, const float b)
481 {
482 __asm float_multiply __retVal, a, b;
483 }
484
485 float __operator / (const float a, const float b)
486 {
487 __asm float_divide __retVal, a, b;
488 }
489
490
491 //// Basic vec2 operators
492
493 vec2 __operator + (const vec2 v, const vec2 u)
494 {
495 __asm vec4_add __retVal.xy, v, u;
496 }
497
498 vec2 __operator - (const vec2 v, const vec2 u)
499 {
500 __asm vec4_subtract __retVal.xy, v, u;
501 }
502
503 vec2 __operator * (const vec2 v, const vec2 u)
504 {
505 __asm vec4_multiply __retVal.xy, v, u;
506 }
507
508 vec2 __operator / (const vec2 v, const vec2 u)
509 {
510 vec2 w; // = 1 / u
511 __asm float_rcp w.x, u.x;
512 __asm float_rcp w.y, u.y;
513 __asm vec4_multiply __retVal.xy, v, w;
514 }
515
516
517 //// Basic vec3 operators
518
519 vec3 __operator + (const vec3 v, const vec3 u)
520 {
521 __asm vec4_add __retVal.xyz, v, u;
522 }
523
524 vec3 __operator - (const vec3 v, const vec3 u)
525 {
526 __asm vec4_subtract __retVal.xyz, v, u;
527 }
528
529 vec3 __operator * (const vec3 v, const vec3 u)
530 {
531 __asm vec4_multiply __retVal.xyz, v, u;
532 }
533
534 vec3 __operator / (const vec3 v, const vec3 u)
535 {
536 vec3 w; // = 1 / u
537 __asm float_rcp w.x, u.x;
538 __asm float_rcp w.y, u.y;
539 __asm float_rcp w.z, u.z;
540 __asm vec4_multiply __retVal.xyz, v, w;
541 }
542
543
544 //// Basic vec4 operators
545
546 vec4 __operator + (const vec4 v, const vec4 u)
547 {
548 __asm vec4_add __retVal, v, u;
549 }
550
551 vec4 __operator - (const vec4 v, const vec4 u)
552 {
553 __asm vec4_subtract __retVal, v, u;
554 }
555
556 vec4 __operator * (const vec4 v, const vec4 u)
557 {
558 __asm vec4_multiply __retVal, v, u;
559 }
560
561 vec4 __operator / (const vec4 v, const vec4 u)
562 {
563 vec4 w; // = 1 / u
564 __asm float_rcp w.x, u.x;
565 __asm float_rcp w.y, u.y;
566 __asm float_rcp w.z, u.z;
567 __asm float_rcp w.w, u.w;
568 __asm vec4_multiply __retVal, v, w;
569 }
570
571
572
573
574 //// Basic vec2/float operators
575
576 vec2 __operator + (const float a, const vec2 u)
577 {
578 __asm vec4_add __retVal.xy, a.xx, u.xy;
579 }
580
581 vec2 __operator + (const vec2 v, const float b)
582 {
583 __asm vec4_add __retVal.xy, v.xy, b.xx;
584 }
585
586 vec2 __operator - (const float a, const vec2 u)
587 {
588 __asm vec4_subtract __retVal.xy, a.xx, u.xy;
589 }
590
591 vec2 __operator - (const vec2 v, const float b)
592 {
593 __asm vec4_subtract __retVal.xy, v.xy, b.xx;
594 }
595
596 vec2 __operator * (const float a, const vec2 u)
597 {
598 __asm vec4_multiply __retVal.xy, a.xx, u.xy;
599 }
600
601 vec2 __operator * (const vec2 v, const float b)
602 {
603 __asm vec4_multiply __retVal.xy, v.xy, b.xx;
604 }
605
606 vec2 __operator / (const float a, const vec2 u)
607 {
608 vec2 invU;
609 __asm float_rcp invU.x, u.x;
610 __asm float_rcp invU.y, u.y;
611 __asm vec4_multiply __retVal.xy, a.xx, invU.xy;
612 }
613
614 vec2 __operator / (const vec2 v, const float b)
615 {
616 float invB;
617 __asm float_rcp invB, b;
618 __asm vec4_multiply __retVal.xy, v.xy, invB.xx;
619 }
620
621
622 //// Basic vec3/float operators
623
624 vec3 __operator + (const float a, const vec3 u)
625 {
626 __asm vec4_add __retVal.xyz, a.xxx, u.xyz;
627 }
628
629 vec3 __operator + (const vec3 v, const float b)
630 {
631 __asm vec4_add __retVal.xyz, v.xyz, b.xxx;
632 }
633
634 vec3 __operator - (const float a, const vec3 u)
635 {
636 __asm vec4_subtract __retVal.xyz, a.xxx, u.xyz;
637 }
638
639 vec3 __operator - (const vec3 v, const float b)
640 {
641 __asm vec4_subtract __retVal.xyz, v.xyz, b.xxx;
642 }
643
644 vec3 __operator * (const float a, const vec3 u)
645 {
646 __asm vec4_multiply __retVal.xyz, a.xxx, u.xyz;
647 }
648
649 vec3 __operator * (const vec3 v, const float b)
650 {
651 __asm vec4_multiply __retVal.xyz, v.xyz, b.xxx;
652 }
653
654 vec3 __operator / (const float a, const vec3 u)
655 {
656 vec3 invU;
657 __asm float_rcp invU.x, u.x;
658 __asm float_rcp invU.y, u.y;
659 __asm float_rcp invU.z, u.z;
660 __asm vec4_multiply __retVal.xyz, a.xxx, invU.xyz;
661 }
662
663 vec3 __operator / (const vec3 v, const float b)
664 {
665 float invB;
666 __asm float_rcp invB, b;
667 __asm vec4_multiply __retVal.xyz, v.xyz, invB.xxx;
668 }
669
670
671 //// Basic vec4/float operators
672
673 vec4 __operator + (const float a, const vec4 u)
674 {
675 __asm vec4_add __retVal, a.xxxx, u;
676 }
677
678 vec4 __operator + (const vec4 v, const float b)
679 {
680 __asm vec4_add __retVal, v, b.xxxx;
681 }
682
683 vec4 __operator - (const float a, const vec4 u)
684 {
685 __asm vec4_subtract __retVal, a.xxxx, u;
686 }
687
688 vec4 __operator - (const vec4 v, const float b)
689 {
690 __asm vec4_subtract __retVal, v, b.xxxx;
691 }
692
693 vec4 __operator * (const float a, const vec4 u)
694 {
695 __asm vec4_multiply __retVal, a.xxxx, u;
696 }
697
698 vec4 __operator * (const vec4 v, const float b)
699 {
700 __asm vec4_multiply __retVal, v, b.xxxx;
701 }
702
703 vec4 __operator / (const float a, const vec4 u)
704 {
705 vec4 invU;
706 __asm float_rcp invU.x, u.x;
707 __asm float_rcp invU.y, u.y;
708 __asm float_rcp invU.z, u.z;
709 __asm float_rcp invU.w, u.w;
710 __asm vec4_multiply __retVal, a.xxxx, invU;
711 }
712
713 vec4 __operator / (const vec4 v, const float b)
714 {
715 float invB;
716 __asm float_rcp invB, b;
717 __asm vec4_multiply __retVal, v, invB.xxxx;
718 }
719
720
721
722 //// Basic ivec2/int operators
723
724 ivec2 __operator + (const int a, const ivec2 u) {
725 return ivec2 (a) + u;
726 }
727
728 ivec2 __operator + (const ivec2 v, const int b) {
729 return v + ivec2 (b);
730 }
731
732 ivec2 __operator - (const int a, const ivec2 u) {
733 return ivec2 (a) - u;
734 }
735
736 ivec2 __operator - (const ivec2 v, const int b) {
737 return v - ivec2 (b);
738 }
739
740 ivec2 __operator * (const int a, const ivec2 u) {
741 return ivec2 (a) * u;
742 }
743
744 ivec2 __operator * (const ivec2 v, const int b) {
745 return v * ivec2 (b);
746 }
747
748 ivec2 __operator / (const int a, const ivec2 u) {
749 return ivec2 (a) / u;
750 }
751
752 ivec2 __operator / (const ivec2 v, const int b) {
753 return v / ivec2 (b);
754 }
755
756
757 //// Basic ivec3/int operators
758
759 ivec3 __operator + (const int a, const ivec3 u) {
760 return ivec3 (a) + u;
761 }
762
763 ivec3 __operator + (const ivec3 v, const int b) {
764 return v + ivec3 (b);
765 }
766
767 ivec3 __operator - (const int a, const ivec3 u) {
768 return ivec3 (a) - u;
769 }
770
771 ivec3 __operator - (const ivec3 v, const int b) {
772 return v - ivec3 (b);
773 }
774
775 ivec3 __operator * (const int a, const ivec3 u) {
776 return ivec3 (a) * u;
777 }
778
779 ivec3 __operator * (const ivec3 v, const int b) {
780 return v * ivec3 (b);
781 }
782
783 ivec3 __operator / (const int a, const ivec3 u) {
784 return ivec3 (a) / u;
785 }
786
787 ivec3 __operator / (const ivec3 v, const int b) {
788 return v / ivec3 (b);
789 }
790
791
792 //// Basic ivec4/int operators
793
794 ivec4 __operator + (const int a, const ivec4 u) {
795 return ivec4 (a) + u;
796 }
797
798 ivec4 __operator + (const ivec4 v, const int b) {
799 return v + ivec4 (b);
800 }
801
802 ivec4 __operator - (const int a, const ivec4 u) {
803 return ivec4 (a) - u;
804 }
805
806 ivec4 __operator - (const ivec4 v, const int b) {
807 return v - ivec4 (b);
808 }
809
810 ivec4 __operator * (const int a, const ivec4 u) {
811 return ivec4 (a) * u;
812 }
813
814 ivec4 __operator * (const ivec4 v, const int b) {
815 return v * ivec4 (b);
816 }
817
818 ivec4 __operator / (const int a, const ivec4 u) {
819 return ivec4 (a) / u;
820 }
821
822 ivec4 __operator / (const ivec4 v, const int b) {
823 return v / ivec4 (b);
824 }
825
826
827
828
829 //// Unary negation operator
830
831 int __operator - (const int a)
832 {
833 // XXX redo
834 float x;
835 int b;
836 __asm int_to_float x, a;
837 __asm float_negate x, x;
838 __asm float_to_int b, x;
839 return b;
840 }
841
842 ivec2 __operator - (const ivec2 v)
843 {
844 // XXX redo
845 return ivec2 (-v.x, -v.y);
846 }
847
848 ivec3 __operator - (const ivec3 v)
849 {
850 // XXX redo
851 return ivec3 (-v.x, -v.y, -v.z);
852 }
853
854 ivec4 __operator - (const ivec4 v)
855 {
856 // XXX redo
857 return ivec4 (-v.x, -v.y, -v.z, -v.w);
858 }
859
860 float __operator - (const float a)
861 {
862 __asm vec4_negate __retVal.x, a;
863 }
864
865 vec2 __operator - (const vec2 v)
866 {
867 __asm vec4_negate __retVal.xy, v.xy;
868 }
869
870 vec3 __operator - (const vec3 v)
871 {
872 __asm vec4_negate __retVal.xyz, v.xyz;
873 }
874
875 vec4 __operator - (const vec4 v)
876 {
877 __asm vec4_negate __retVal, v;
878 }
879
880 mat2 __operator - (const mat2 m)
881 {
882 __retVal[0] = -m[0];
883 __retVal[1] = -m[1];
884 }
885
886 mat3 __operator - (const mat3 m)
887 {
888 __retVal[0] = -m[0];
889 __retVal[1] = -m[1];
890 __retVal[2] = -m[2];
891 }
892
893 mat4 __operator - (const mat4 m)
894 {
895 __retVal[0] = -m[0];
896 __retVal[1] = -m[1];
897 __retVal[2] = -m[2];
898 __retVal[3] = -m[3];
899 }
900
901
902
903 //// dot product
904
905 float dot(const float a, const float b)
906 {
907 return a * b;
908 }
909
910 float dot(const vec2 a, const vec2 b)
911 {
912 return a.x * b.x + a.y * b.y;
913 }
914
915 float dot(const vec3 a, const vec3 b)
916 {
917 __asm vec3_dot __retVal, a, b;
918 }
919
920 float dot(const vec4 a, const vec4 b)
921 {
922 __asm vec4_dot __retVal, a, b;
923 }
924
925
926
927 //// int assignment operators
928
929 void __operator += (inout int a, const int b)
930 {
931 a = int (float (a) + float (b));
932 }
933
934 void __operator -= (inout int a, const int b)
935 {
936 a += -b;
937 }
938
939 void __operator *= (inout int a, const int b)
940 {
941 a = int (float (a) * float (b));
942 }
943
944 void __operator /= (inout int a, const int b)
945 {
946 a = int (float (a) / float (b));
947 }
948
949
950 //// ivec2 assignment operators
951
952 void __operator += (inout ivec2 v, const ivec2 u)
953 {
954 v.x += u.x;
955 v.y += u.y;
956 }
957
958 void __operator -= (inout ivec2 v, const ivec2 u)
959 {
960 v.x -= u.x;
961 v.y -= u.y;
962 }
963
964 void __operator *= (inout ivec2 v, const ivec2 u)
965 {
966 v.x *= u.x;
967 v.y *= u.y;
968 }
969
970 void __operator /= (inout ivec2 v, const ivec2 u)
971 {
972 v.x /= u.x;
973 v.y /= u.y;
974 }
975
976
977 //// ivec3 assignment operators
978
979 void __operator += (inout ivec3 v, const ivec3 u)
980 {
981 v.x += u.x;
982 v.y += u.y;
983 v.z += u.z;
984 }
985
986 void __operator -= (inout ivec3 v, const ivec3 u)
987 {
988 v.x -= u.x;
989 v.y -= u.y;
990 v.z -= u.z;
991 }
992
993 void __operator *= (inout ivec3 v, const ivec3 u)
994 {
995 v.x *= u.x;
996 v.y *= u.y;
997 v.z *= u.z;
998 }
999
1000 void __operator /= (inout ivec3 v, const ivec3 u)
1001 {
1002 v.x /= u.x;
1003 v.y /= u.y;
1004 v.z /= u.z;
1005 }
1006
1007
1008 //// ivec4 assignment operators
1009
1010 void __operator += (inout ivec4 v, const ivec4 u) {
1011 v.x += u.x;
1012 v.y += u.y;
1013 v.z += u.z;
1014 v.w += u.w;
1015 }
1016
1017 void __operator -= (inout ivec4 v, const ivec4 u) {
1018 v.x -= u.x;
1019 v.y -= u.y;
1020 v.z -= u.z;
1021 v.w -= u.w;
1022 }
1023
1024 void __operator *= (inout ivec4 v, const ivec4 u) {
1025 v.x *= u.x;
1026 v.y *= u.y;
1027 v.z *= u.z;
1028 v.w *= u.w;
1029 }
1030
1031 void __operator /= (inout ivec4 v, const ivec4 u) {
1032 v.x /= u.x;
1033 v.y /= u.y;
1034 v.z /= u.z;
1035 v.w /= u.w;
1036 }
1037
1038
1039 //// float assignment operators
1040
1041 void __operator += (inout float a, const float b)
1042 {
1043 __asm vec4_add a.x, a.x, b;
1044 }
1045
1046 void __operator -= (inout float a, const float b)
1047 {
1048 __asm vec4_subtract a.x, a, b;
1049 }
1050
1051 void __operator *= (inout float a, const float b)
1052 {
1053 __asm vec4_multiply a.x, a, b;
1054 }
1055
1056 void __operator /= (inout float a, const float b)
1057 {
1058 float w; // = 1 / b
1059 __asm float_rcp w.x, b;
1060 __asm vec4_multiply a.x, a, w;
1061 }
1062
1063
1064 //// vec2 assignment operators
1065
1066 void __operator += (inout vec2 v, const vec2 u)
1067 {
1068 v.x += u.x;
1069 v.y += u.y;
1070 }
1071
1072 void __operator -= (inout vec2 v, const vec2 u)
1073 {
1074 v.x -= u.x;
1075 v.y -= u.y;
1076 }
1077
1078 void __operator *= (inout vec2 v, const vec2 u)
1079 {
1080 v.x *= u.x;
1081 v.y *= u.y;
1082 }
1083
1084 void __operator /= (inout vec2 v, const vec2 u)
1085 {
1086 v.x /= u.x;
1087 v.y /= u.y;
1088 }
1089
1090
1091 //// vec3 assignment operators
1092
1093 void __operator += (inout vec3 v, const vec3 u)
1094 {
1095 __asm vec4_add v.xyz, v, u;
1096 }
1097
1098 void __operator -= (inout vec3 v, const vec3 u)
1099 {
1100 __asm vec4_subtract v.xyz, v, u;
1101 }
1102
1103 void __operator *= (inout vec3 v, const vec3 u)
1104 {
1105 __asm vec4_multiply v.xyz, v, u;
1106 }
1107
1108 void __operator /= (inout vec3 v, const vec3 u)
1109 {
1110 // XXX rcp
1111 v.x /= u.x;
1112 v.y /= u.y;
1113 v.z /= u.z;
1114 }
1115
1116
1117 //// vec4 assignment operators
1118
1119 void __operator += (inout vec4 v, const vec4 u)
1120 {
1121 __asm vec4_add v, v, u;
1122 }
1123
1124 void __operator -= (inout vec4 v, const vec4 u)
1125 {
1126 __asm vec4_subtract v, v, u;
1127 }
1128
1129 void __operator *= (inout vec4 v, const vec4 u)
1130 {
1131 __asm vec4_multiply v, v, u;
1132 }
1133
1134 void __operator /= (inout vec4 v, const vec4 u)
1135 {
1136 // XXX rcp
1137 v.x /= u.x;
1138 v.y /= u.y;
1139 v.z /= u.z;
1140 v.w /= u.w;
1141 }
1142
1143
1144
1145 //// ivec2/int assignment operators
1146
1147 void __operator += (inout ivec2 v, const int a)
1148 {
1149 v.x += a;
1150 v.y += a;
1151 }
1152
1153 void __operator -= (inout ivec2 v, const int a)
1154 {
1155 v.x -= a;
1156 v.y -= a;
1157 }
1158
1159 void __operator *= (inout ivec2 v, const int a)
1160 {
1161 v.x *= a;
1162 v.y *= a;
1163 }
1164
1165 void __operator /= (inout ivec2 v, const int a)
1166 {
1167 // XXX rcp
1168 v.x /= a;
1169 v.y /= a;
1170 }
1171
1172
1173 //// ivec3/int assignment operators
1174
1175 void __operator += (inout ivec3 v, const int a)
1176 {
1177 v.x += a;
1178 v.y += a;
1179 v.z += a;
1180 }
1181
1182 void __operator -= (inout ivec3 v, const int a)
1183 {
1184 v.x -= a;
1185 v.y -= a;
1186 v.z -= a;
1187 }
1188
1189 void __operator *= (inout ivec3 v, const int a)
1190 {
1191 v.x *= a;
1192 v.y *= a;
1193 v.z *= a;
1194 }
1195
1196 void __operator /= (inout ivec3 v, const int a)
1197 {
1198 v.x /= a;
1199 v.y /= a;
1200 v.z /= a;
1201 }
1202
1203
1204 //// ivec4/int assignment operators
1205
1206 void __operator += (inout ivec4 v, const int a)
1207 {
1208 __asm vec4_add v, v, a.xxxx;
1209 }
1210
1211 void __operator -= (inout ivec4 v, const int a)
1212 {
1213 __asm vec4_subtract v, v, a.xxxx;
1214 }
1215
1216 void __operator *= (inout ivec4 v, const int a)
1217 {
1218 __asm vec4_multiply v, v, a.xxxx;
1219 }
1220
1221 void __operator /= (inout ivec4 v, const int a)
1222 {
1223 v.x /= a;
1224 v.y /= a;
1225 v.z /= a;
1226 v.w /= a;
1227 }
1228
1229
1230
1231 //// vec2/float assignment operators
1232
1233 void __operator += (inout vec2 v, const float a)
1234 {
1235 __asm vec4_add v.xy, v, a.xx;
1236 }
1237
1238 void __operator -= (inout vec2 v, const float a)
1239 {
1240 __asm vec4_subtract v.xy, v, a.xx;
1241 }
1242
1243 void __operator *= (inout vec2 v, const float a)
1244 {
1245 __asm vec4_multiply v.xy, v, a.xx;
1246 }
1247
1248 void __operator /= (inout vec2 v, const float a)
1249 {
1250 float invA;
1251 __asm float_rcp invA, a;
1252 __asm vec4_multiply v.xy, v.xy, a.xx;
1253 }
1254
1255
1256 //// vec3/float assignment operators
1257
1258 void __operator += (inout vec3 v, const float a)
1259 {
1260 __asm vec4_add v.xyz, v, a.xxx;
1261 }
1262
1263 void __operator -= (inout vec3 v, const float a)
1264 {
1265 __asm vec4_subtract v.xyz, v, a.xxx;
1266 }
1267
1268 void __operator *= (inout vec3 v, const float a)
1269 {
1270 __asm vec4_multiply v.xyz, v, a.xxx;
1271 }
1272
1273 void __operator /= (inout vec3 v, const float a)
1274 {
1275 float invA;
1276 __asm float_rcp invA, a;
1277 __asm vec4_multiply v.xyz, v.xyz, a.xxx;
1278 }
1279
1280
1281 //// vec4/float assignment operators
1282
1283 void __operator += (inout vec4 v, const float a)
1284 {
1285 __asm vec4_add v, v, a.xxxx;
1286 }
1287
1288 void __operator -= (inout vec4 v, const float a)
1289 {
1290 __asm vec4_subtract v, v, a.xxxx;
1291 }
1292
1293 void __operator *= (inout vec4 v, const float a)
1294 {
1295 __asm vec4_multiply v, v, a.xxxx;
1296 }
1297
1298 void __operator /= (inout vec4 v, const float a)
1299 {
1300 float invA;
1301 __asm float_rcp invA, a;
1302 __asm vec4_multiply v, v, a.xxxx;
1303 }
1304
1305
1306
1307
1308
1309 //// Basic mat2 operations
1310
1311 mat2 __operator + (const mat2 m, const mat2 n)
1312 {
1313 __retVal[0] = m[0] + n[0];
1314 __retVal[1] = m[1] + n[1];
1315 }
1316
1317 mat2 __operator - (const mat2 m, const mat2 n)
1318 {
1319 __retVal[0] = m[0] - n[0];
1320 __retVal[1] = m[1] - n[1];
1321 }
1322
1323 mat2 __operator * (const mat2 m, const mat2 n)
1324 {
1325 vec2 mRow0, mRow1;
1326 mRow0.x = m[0].x;
1327 mRow0.y = m[1].x;
1328 mRow1.x = m[0].y;
1329 mRow1.y = m[1].y;
1330 __retVal[0].x = dot(mRow0, n[0]);
1331 __retVal[1].x = dot(mRow0, n[1]);
1332 __retVal[0].y = dot(mRow1, n[0]);
1333 __retVal[1].y = dot(mRow1, n[1]);
1334 }
1335
1336 mat2 __operator / (const mat2 m, const mat2 n)
1337 {
1338 __retVal[0] = m[0] / n[0];
1339 __retVal[1] = m[1] / n[1];
1340 }
1341
1342
1343 //// Basic mat3 operations
1344
1345 mat3 __operator + (const mat3 m, const mat3 n)
1346 {
1347 __retVal[0] = m[0] + n[0];
1348 __retVal[1] = m[1] + n[1];
1349 __retVal[2] = m[2] + n[2];
1350 }
1351
1352 mat3 __operator - (const mat3 m, const mat3 n)
1353 {
1354 __retVal[0] = m[0] - n[0];
1355 __retVal[1] = m[1] - n[1];
1356 __retVal[2] = m[2] - n[2];
1357 }
1358
1359 mat3 __operator * (const mat3 m, const mat3 n)
1360 {
1361 vec3 mRow0, mRow1, mRow2;
1362 mRow0.x = m[0].x;
1363 mRow0.y = m[1].x;
1364 mRow0.z = m[2].x;
1365 mRow1.x = m[0].y;
1366 mRow1.y = m[1].y;
1367 mRow1.z = m[2].y;
1368 mRow2.x = m[0].z;
1369 mRow2.y = m[1].z;
1370 mRow2.z = m[2].z;
1371 __retVal[0].x = dot(mRow0, n[0]);
1372 __retVal[1].x = dot(mRow0, n[1]);
1373 __retVal[2].x = dot(mRow0, n[2]);
1374 __retVal[0].y = dot(mRow1, n[0]);
1375 __retVal[1].y = dot(mRow1, n[1]);
1376 __retVal[2].y = dot(mRow1, n[2]);
1377 __retVal[0].z = dot(mRow2, n[0]);
1378 __retVal[1].z = dot(mRow2, n[1]);
1379 __retVal[2].z = dot(mRow2, n[2]);
1380 }
1381
1382 mat3 __operator / (const mat3 m, const mat3 n)
1383 {
1384 __retVal[0] = m[0] / n[0];
1385 __retVal[1] = m[1] / n[1];
1386 __retVal[2] = m[2] / n[2];
1387 }
1388
1389
1390 //// Basic mat4 operations
1391
1392 mat4 __operator + (const mat4 m, const mat4 n)
1393 {
1394 __retVal[0] = m[0] + n[0];
1395 __retVal[1] = m[1] + n[1];
1396 __retVal[2] = m[2] + n[2];
1397 __retVal[3] = m[3] + n[3];
1398 }
1399
1400 mat4 __operator - (const mat4 m, const mat4 n)
1401 {
1402 __retVal[0] = m[0] - n[0];
1403 __retVal[1] = m[1] - n[1];
1404 __retVal[2] = m[2] - n[2];
1405 __retVal[3] = m[3] - n[3];
1406 }
1407
1408 mat4 __operator * (const mat4 m, const mat4 n)
1409 {
1410 vec4 mRow0, mRow1, mRow2, mRow3;
1411 mRow0.x = m[0].x;
1412 mRow0.y = m[1].x;
1413 mRow0.z = m[2].x;
1414 mRow0.w = m[3].x;
1415 mRow1.x = m[0].y;
1416 mRow1.y = m[1].y;
1417 mRow1.z = m[2].y;
1418 mRow1.w = m[3].y;
1419 mRow2.x = m[0].z;
1420 mRow2.y = m[1].z;
1421 mRow2.z = m[2].z;
1422 mRow2.w = m[3].z;
1423 mRow3.x = m[0].w;
1424 mRow3.y = m[1].w;
1425 mRow3.z = m[2].w;
1426 mRow3.w = m[3].w;
1427 __retVal[0].x = dot(mRow0, n[0]);
1428 __retVal[1].x = dot(mRow0, n[1]);
1429 __retVal[2].x = dot(mRow0, n[2]);
1430 __retVal[3].x = dot(mRow0, n[3]);
1431 __retVal[0].y = dot(mRow1, n[0]);
1432 __retVal[1].y = dot(mRow1, n[1]);
1433 __retVal[2].y = dot(mRow1, n[2]);
1434 __retVal[3].y = dot(mRow1, n[3]);
1435 __retVal[0].z = dot(mRow2, n[0]);
1436 __retVal[1].z = dot(mRow2, n[1]);
1437 __retVal[2].z = dot(mRow2, n[2]);
1438 __retVal[3].z = dot(mRow2, n[3]);
1439 __retVal[0].w = dot(mRow3, n[0]);
1440 __retVal[1].w = dot(mRow3, n[1]);
1441 __retVal[2].w = dot(mRow3, n[2]);
1442 __retVal[3].w = dot(mRow3, n[3]);
1443 }
1444
1445 mat4 __operator / (const mat4 m, const mat4 n)
1446 {
1447 __retVal[0] = m[0] / n[0];
1448 __retVal[1] = m[1] / n[1];
1449 __retVal[2] = m[2] / n[2];
1450 __retVal[3] = m[3] / n[3];
1451 }
1452
1453
1454 //// mat2/float operations
1455
1456 mat2 __operator + (const float a, const mat2 n)
1457 {
1458 __retVal[0] = a + n[0];
1459 __retVal[1] = a + n[1];
1460 }
1461
1462 mat2 __operator + (const mat2 m, const float b)
1463 {
1464 __retVal[0] = m[0] + b;
1465 __retVal[1] = m[1] + b;
1466 }
1467
1468 mat2 __operator - (const float a, const mat2 n)
1469 {
1470 __retVal[0] = a - n[0];
1471 __retVal[1] = a - n[1];
1472 }
1473
1474 mat2 __operator - (const mat2 m, const float b)
1475 {
1476 __retVal[0] = m[0] - b;
1477 __retVal[1] = m[1] - b;
1478 }
1479
1480 mat2 __operator * (const float a, const mat2 n)
1481 {
1482 __retVal[0] = a * n[0];
1483 __retVal[1] = a * n[1];
1484 }
1485
1486 mat2 __operator * (const mat2 m, const float b)
1487 {
1488 __retVal[0] = m[0] * b;
1489 __retVal[1] = m[1] * b;
1490 }
1491
1492 mat2 __operator / (const float a, const mat2 n)
1493 {
1494 __retVal[0] = a / n[0];
1495 __retVal[1] = a / n[1];
1496 }
1497
1498 mat2 __operator / (const mat2 m, const float b)
1499 {
1500 __retVal[0] = m[0] / b;
1501 __retVal[1] = m[1] / b;
1502 }
1503
1504
1505 //// mat3/float operations
1506
1507 mat3 __operator + (const float a, const mat3 n)
1508 {
1509 __retVal[0] = a + n[0];
1510 __retVal[1] = a + n[1];
1511 __retVal[2] = a + n[2];
1512 }
1513
1514 mat3 __operator + (const mat3 m, const float b)
1515 {
1516 __retVal[0] = m[0] + b;
1517 __retVal[1] = m[1] + b;
1518 __retVal[2] = m[2] + b;
1519 }
1520
1521 mat3 __operator - (const float a, const mat3 n)
1522 {
1523 __retVal[0] = a - n[0];
1524 __retVal[1] = a - n[1];
1525 __retVal[2] = a - n[2];
1526 }
1527
1528 mat3 __operator - (const mat3 m, const float b)
1529 {
1530 __retVal[0] = m[0] - b;
1531 __retVal[1] = m[1] - b;
1532 __retVal[2] = m[2] - b;
1533 }
1534
1535 mat3 __operator * (const float a, const mat3 n)
1536 {
1537 __retVal[0] = a * n[0];
1538 __retVal[1] = a * n[1];
1539 __retVal[2] = a * n[2];
1540 }
1541
1542 mat3 __operator * (const mat3 m, const float b)
1543 {
1544 __retVal[0] = m[0] * b;
1545 __retVal[1] = m[1] * b;
1546 __retVal[2] = m[2] * b;
1547 }
1548
1549 mat3 __operator / (const float a, const mat3 n)
1550 {
1551 __retVal[0] = a / n[0];
1552 __retVal[1] = a / n[1];
1553 __retVal[2] = a / n[2];
1554 }
1555
1556 mat3 __operator / (const mat3 m, const float b)
1557 {
1558 __retVal[0] = m[0] / b;
1559 __retVal[1] = m[1] / b;
1560 __retVal[2] = m[2] / b;
1561 }
1562
1563
1564 //// mat4/float operations
1565
1566 mat4 __operator + (const float a, const mat4 n)
1567 {
1568 __retVal[0] = a + n[0];
1569 __retVal[1] = a + n[1];
1570 __retVal[2] = a + n[2];
1571 __retVal[3] = a + n[3];
1572 }
1573
1574 mat4 __operator + (const mat4 m, const float b)
1575 {
1576 __retVal[0] = m[0] + b;
1577 __retVal[1] = m[1] + b;
1578 __retVal[2] = m[2] + b;
1579 __retVal[3] = m[3] + b;
1580 }
1581
1582 mat4 __operator - (const float a, const mat4 n)
1583 {
1584 __retVal[0] = a - n[0];
1585 __retVal[1] = a - n[1];
1586 __retVal[2] = a - n[2];
1587 __retVal[3] = a - n[3];
1588 }
1589
1590 mat4 __operator - (const mat4 m, const float b)
1591 {
1592 __retVal[0] = m[0] - b;
1593 __retVal[1] = m[1] - b;
1594 __retVal[2] = m[2] - b;
1595 __retVal[3] = m[3] - b;
1596 }
1597
1598 mat4 __operator * (const float a, const mat4 n)
1599 {
1600 __retVal[0] = a * n[0];
1601 __retVal[1] = a * n[1];
1602 __retVal[2] = a * n[2];
1603 __retVal[3] = a * n[3];
1604 }
1605
1606 mat4 __operator * (const mat4 m, const float b)
1607 {
1608 __retVal[0] = m[0] * b;
1609 __retVal[1] = m[1] * b;
1610 __retVal[2] = m[2] * b;
1611 __retVal[3] = m[3] * b;
1612 }
1613
1614 mat4 __operator / (const float a, const mat4 n)
1615 {
1616 __retVal[0] = a / n[0];
1617 __retVal[1] = a / n[1];
1618 __retVal[2] = a / n[2];
1619 __retVal[3] = a / n[3];
1620 }
1621
1622 mat4 __operator / (const mat4 m, const float b)
1623 {
1624 __retVal[0] = m[0] / b;
1625 __retVal[1] = m[1] / b;
1626 __retVal[2] = m[2] / b;
1627 __retVal[3] = m[3] / b;
1628 }
1629
1630
1631
1632 //// matrix / vector products
1633
1634 vec2 __operator * (const mat2 m, const vec2 v)
1635 {
1636 __retVal.x = dot(m[0], v);
1637 __retVal.y = dot(m[1], v);
1638 }
1639
1640 vec2 __operator * (const vec2 v, const mat2 m)
1641 {
1642 vec2 r0, r1;
1643 r0.x = m[0].x;
1644 r0.y = m[1].x;
1645 r1.x = m[0].y;
1646 r1.y = m[1].y;
1647 __retVal.x = dot(v, r0);
1648 __retVal.y = dot(v, r1);
1649 }
1650
1651 vec3 __operator * (const mat3 m, const vec3 v)
1652 {
1653 __retVal.x = dot(m[0], v);
1654 __retVal.y = dot(m[1], v);
1655 __retVal.z = dot(m[2], v);
1656 }
1657
1658 vec3 __operator * (const vec3 v, const mat3 m)
1659 {
1660 vec3 r0, r1, r2;
1661 r0.x = m[0].x;
1662 r0.y = m[1].x;
1663 r0.z = m[2].x;
1664 r1.x = m[0].y;
1665 r1.y = m[1].y;
1666 r1.z = m[2].y;
1667 r2.x = m[0].z;
1668 r2.y = m[1].z;
1669 r2.z = m[2].z;
1670 __asm vec3_dot __retVal.x, v, r0;
1671 __asm vec3_dot __retVal.y, v, r1;
1672 __asm vec3_dot __retVal.z, v, r2;
1673 }
1674
1675 vec4 __operator * (const mat4 m, const vec4 v)
1676 {
1677 __retVal.x = dot(m[0], v);
1678 __retVal.y = dot(m[1], v);
1679 __retVal.z = dot(m[2], v);
1680 __retVal.w = dot(m[3], v);
1681 }
1682
1683 vec4 __operator * (const vec4 v, const mat4 m)
1684 {
1685 vec4 r0, r1, r2, r3;
1686 r0.x = m[0].x;
1687 r0.y = m[1].x;
1688 r0.z = m[2].x;
1689 r0.w = m[3].x;
1690 r1.x = m[0].y;
1691 r1.y = m[1].y;
1692 r1.z = m[2].y;
1693 r1.w = m[3].y;
1694 r2.x = m[0].z;
1695 r2.y = m[1].z;
1696 r2.z = m[2].z;
1697 r2.w = m[3].z;
1698 r3.x = m[0].w;
1699 r3.y = m[1].w;
1700 r3.z = m[2].w;
1701 r3.w = m[3].w;
1702 __asm vec4_dot __retVal.x, v, r0;
1703 __asm vec4_dot __retVal.y, v, r1;
1704 __asm vec4_dot __retVal.z, v, r2;
1705 __asm vec4_dot __retVal.w, v, r3;
1706 }
1707
1708
1709
1710 //// mat2 assignment operators
1711
1712 void __operator += (inout mat2 m, const mat2 n)
1713 {
1714 m[0] += n[0];
1715 m[1] += n[1];
1716 }
1717
1718 void __operator -= (inout mat2 m, const mat2 n)
1719 {
1720 m[0] -= n[0];
1721 m[1] -= n[1];
1722 }
1723
1724 void __operator *= (inout mat2 m, const mat2 n)
1725 {
1726 m = m * n;
1727 }
1728
1729 void __operator /= (inout mat2 m, const mat2 n)
1730 {
1731 m[0] /= n[0];
1732 m[1] /= n[1];
1733 }
1734
1735
1736 //// mat3 assignment operators
1737
1738 void __operator += (inout mat3 m, const mat3 n)
1739 {
1740 m[0] += n[0];
1741 m[1] += n[1];
1742 m[2] += n[2];
1743 }
1744
1745 void __operator -= (inout mat3 m, const mat3 n)
1746 {
1747 m[0] -= n[0];
1748 m[1] -= n[1];
1749 m[2] -= n[2];
1750 }
1751
1752 void __operator *= (inout mat3 m, const mat3 n)
1753 {
1754 m = m * n;
1755 }
1756
1757 void __operator /= (inout mat3 m, const mat3 n)
1758 {
1759 m[0] /= n[0];
1760 m[1] /= n[1];
1761 m[2] /= n[2];
1762 }
1763
1764
1765 // mat4 assignment operators
1766
1767 void __operator += (inout mat4 m, const mat4 n)
1768 {
1769 m[0] += n[0];
1770 m[1] += n[1];
1771 m[2] += n[2];
1772 m[3] += n[3];
1773 }
1774
1775 void __operator -= (inout mat4 m, const mat4 n) {
1776 m[0] -= n[0];
1777 m[1] -= n[1];
1778 m[2] -= n[2];
1779 m[3] -= n[3];
1780 }
1781
1782 void __operator *= (inout mat4 m, const mat4 n)
1783 {
1784 m = m * n;
1785 }
1786
1787 void __operator /= (inout mat4 m, const mat4 n)
1788 {
1789 m[0] /= n[0];
1790 m[1] /= n[1];
1791 m[2] /= n[2];
1792 m[3] /= n[3];
1793 }
1794
1795
1796 //// mat2/float assignment operators
1797
1798 void __operator += (inout mat2 m, const float a) {
1799 m[0] += a;
1800 m[1] += a;
1801 }
1802
1803 void __operator -= (inout mat2 m, const float a) {
1804 m[0] -= a;
1805 m[1] -= a;
1806 }
1807
1808 void __operator *= (inout mat2 m, const float a) {
1809 m[0] *= a;
1810 m[1] *= a;
1811 }
1812
1813 void __operator /= (inout mat2 m, const float a) {
1814 m[0] /= a;
1815 m[1] /= a;
1816 }
1817
1818
1819 //// mat3/float assignment operators
1820
1821 void __operator += (inout mat3 m, const float a) {
1822 m[0] += a;
1823 m[1] += a;
1824 m[2] += a;
1825 }
1826
1827 void __operator -= (inout mat3 m, const float a) {
1828 m[0] -= a;
1829 m[1] -= a;
1830 m[2] -= a;
1831 }
1832
1833 void __operator *= (inout mat3 m, const float a) {
1834 m[0] *= a;
1835 m[1] *= a;
1836 m[2] *= a;
1837 }
1838
1839 void __operator /= (inout mat3 m, const float a) {
1840 m[0] /= a;
1841 m[1] /= a;
1842 m[2] /= a;
1843 }
1844
1845
1846 //// mat4/float assignment operators
1847
1848 void __operator += (inout mat4 m, const float a) {
1849 m[0] += a;
1850 m[1] += a;
1851 m[2] += a;
1852 m[3] += a;
1853 }
1854
1855 void __operator -= (inout mat4 m, const float a) {
1856 m[0] -= a;
1857 m[1] -= a;
1858 m[2] -= a;
1859 m[3] -= a;
1860 }
1861
1862 void __operator *= (inout mat4 m, const float a) {
1863 m[0] *= a;
1864 m[1] *= a;
1865 m[2] *= a;
1866 m[3] *= a;
1867 }
1868
1869 void __operator /= (inout mat4 m, const float a) {
1870 m[0] /= a;
1871 m[1] /= a;
1872 m[2] /= a;
1873 m[3] /= a;
1874 }
1875
1876
1877
1878 //// vec/mat assignment operators
1879
1880 void __operator *= (inout vec2 v, const mat2 m)
1881 {
1882 v = v * m;
1883 }
1884
1885 void __operator *= (inout vec3 v, const mat3 m)
1886 {
1887 v = v * m;
1888 }
1889
1890 void __operator *= (inout vec4 v, const mat4 m)
1891 {
1892 v = v * m;
1893 }
1894
1895
1896
1897 //// pre-decrement operators
1898
1899 int __operator --(inout int a)
1900 {
1901 a = a - 1;
1902 __retVal = a;
1903 }
1904
1905 ivec2 __operator --(inout ivec2 v)
1906 {
1907 v = v - ivec2(1);
1908 __retVal = v;
1909 }
1910
1911 ivec3 __operator --(inout ivec3 v)
1912 {
1913 v = v - ivec3(1);
1914 __retVal = v;
1915 }
1916
1917 ivec4 __operator --(inout ivec4 v)
1918 {
1919 v = v - ivec4(1);
1920 __retVal = v;
1921 }
1922
1923
1924 float __operator --(inout float a)
1925 {
1926 a = a - 1.0;
1927 __retVal = a;
1928 }
1929
1930 vec2 __operator --(inout vec2 v)
1931 {
1932 v = v - vec2(1.0);
1933 __retVal = v;
1934 }
1935
1936 vec3 __operator --(inout vec3 v)
1937 {
1938 v = v - vec3(1.0);
1939 __retVal = v;
1940 }
1941
1942 vec4 __operator --(inout vec4 v)
1943 {
1944 v = v - vec4(1.0);
1945 __retVal = v;
1946 }
1947
1948
1949 mat2 __operator --(inout mat2 m)
1950 {
1951 m[0] = m[0] - vec2(1.0);
1952 m[1] = m[1] - vec2(1.0);
1953 __retVal = m;
1954 }
1955
1956 mat3 __operator --(inout mat3 m)
1957 {
1958 m[0] = m[0] - vec3(1.0);
1959 m[1] = m[1] - vec3(1.0);
1960 m[2] = m[2] - vec3(1.0);
1961 __retVal = m;
1962 }
1963
1964 mat4 __operator --(inout mat4 m)
1965 {
1966 m[0] = m[0] - vec4(1.0);
1967 m[1] = m[1] - vec4(1.0);
1968 m[2] = m[2] - vec4(1.0);
1969 m[3] = m[3] - vec4(1.0);
1970 __retVal = m;
1971 }
1972
1973
1974 //// pre-increment operators
1975
1976 int __operator ++(inout int a)
1977 {
1978 a = a + 1;
1979 __retVal = a;
1980 }
1981
1982 ivec2 __operator ++(inout ivec2 v)
1983 {
1984 v = v + ivec2(1);
1985 __retVal = v;
1986 }
1987
1988 ivec3 __operator ++(inout ivec3 v)
1989 {
1990 v = v + ivec3(1);
1991 __retVal = v;
1992 }
1993
1994 ivec4 __operator ++(inout ivec4 v)
1995 {
1996 v = v + ivec4(1);
1997 __retVal = v;
1998 }
1999
2000
2001 float __operator ++(inout float a)
2002 {
2003 a = a + 1.0;
2004 __retVal = a;
2005 }
2006
2007 vec2 __operator ++(inout vec2 v)
2008 {
2009 v = v + vec2(1.0);
2010 __retVal = v;
2011 }
2012
2013 vec3 __operator ++(inout vec3 v)
2014 {
2015 v = v + vec3(1.0);
2016 __retVal = v;
2017 }
2018
2019 vec4 __operator ++(inout vec4 v)
2020 {
2021 v = v + vec4(1.0);
2022 __retVal = v;
2023 }
2024
2025
2026 mat2 __operator ++(inout mat2 m)
2027 {
2028 m[0] = m[0] + vec2(1.0);
2029 m[1] = m[1] + vec2(1.0);
2030 __retVal = m;
2031 }
2032
2033 mat3 __operator ++(inout mat3 m)
2034 {
2035 m[0] = m[0] + vec3(1.0);
2036 m[1] = m[1] + vec3(1.0);
2037 m[2] = m[2] + vec3(1.0);
2038 __retVal = m;
2039 }
2040
2041 mat4 __operator ++(inout mat4 m)
2042 {
2043 m[0] = m[0] + vec4(1.0);
2044 m[1] = m[1] + vec4(1.0);
2045 m[2] = m[2] + vec4(1.0);
2046 m[3] = m[3] + vec4(1.0);
2047 __retVal = m;
2048 }
2049
2050
2051
2052 //// post-decrement
2053
2054 int __postDecr(inout int a)
2055 {
2056 __retVal = a;
2057 a = a - 1;
2058 }
2059
2060 ivec2 __postDecr(inout ivec2 v)
2061 {
2062 __retVal = v;
2063 v = v - ivec2(1);
2064 }
2065
2066 ivec3 __postDecr(inout ivec3 v)
2067 {
2068 __retVal = v;
2069 v = v - ivec3(1);
2070 }
2071
2072 ivec4 __postDecr(inout ivec4 v)
2073 {
2074 __retVal = v;
2075 v = v - ivec4(1);
2076 }
2077
2078
2079 float __postDecr(inout float a)
2080 {
2081 __retVal = v;
2082 v = v - 1.0;
2083 }
2084
2085 vec2 __postDecr(inout vec2 v)
2086 {
2087 __retVal = v;
2088 v = v - vec2(1.0);
2089 }
2090
2091 vec3 __postDecr(inout vec3 v)
2092 {
2093 __retVal = v;
2094 v = v - vec3(1.0);
2095 }
2096
2097 vec4 __postDecr(inout vec4 v)
2098 {
2099 __retVal = v;
2100 v = v - vec4(1.0);
2101 }
2102
2103
2104 mat2 __postDecr(inout mat2 m)
2105 {
2106 __retVal = m;
2107 m[0] = m[0] - vec2(1.0);
2108 m[1] = m[1] - vec2(1.0);
2109 }
2110
2111 mat3 __postDecr(inout mat3 m)
2112 {
2113 __retVal = m;
2114 m[0] = m[0] - vec3(1.0);
2115 m[1] = m[1] - vec3(1.0);
2116 m[2] = m[2] - vec3(1.0);
2117 }
2118
2119 mat4 __postDecr(inout mat4 m)
2120 {
2121 __retVal = m;
2122 m[0] = m[0] - vec4(1.0);
2123 m[1] = m[1] - vec4(1.0);
2124 m[2] = m[2] - vec4(1.0);
2125 m[3] = m[3] - vec4(1.0);
2126 }
2127
2128
2129 //// post-increment
2130
2131 float __postIncr(inout float a) {
2132 float b = a;
2133 ++a;
2134 return b;
2135 }
2136
2137 vec2 __postIncr(inout vec2 v) {
2138 return vec2 (v.x++, v.y++);
2139 }
2140
2141 vec3 __postIncr(inout vec3 v) {
2142 return vec3 (v.x++, v.y++, v.z++);
2143 }
2144
2145 vec4 __postIncr(inout vec4 v)
2146 {
2147 __retVal = v;
2148 v = v + vec4(1.0);
2149 }
2150
2151
2152 int __postIncr(inout int a) {
2153 int b = a;
2154 ++a;
2155 return b;
2156 }
2157
2158 ivec2 __postIncr(inout ivec2 v) {
2159 return ivec2 (v.x++, v.y++);
2160 }
2161
2162 ivec3 __postIncr(inout ivec3 v) {
2163 return ivec3 (v.x++, v.y++, v.z++);
2164 }
2165
2166 ivec4 __postIncr(inout ivec4 v) {
2167 return ivec4 (v.x++, v.y++, v.z++, v.w++);
2168 }
2169
2170
2171
2172 mat2 __postIncr(inout mat2 m) {
2173 return mat2 (m[0]++, m[1]++);
2174 }
2175
2176 mat3 __postIncr(inout mat3 m) {
2177 return mat3 (m[0]++, m[1]++, m[2]++);
2178 }
2179
2180 mat4 __postIncr(inout mat4 m) {
2181 return mat4 (m[0]++, m[1]++, m[2]++, m[3]++);
2182 }
2183
2184
2185
2186 //// inequality operators
2187
2188
2189 // XXX are the inequality operators for floats/ints really needed????
2190 bool __operator < (const float a, const float b)
2191 {
2192 __asm vec4_sgt __retVal.x, b, a;
2193 }
2194
2195
2196 bool __operator < (const int a, const int b) {
2197 return float (a) < float (b);
2198 }
2199
2200 bool __operator > (const float a, const float b) {
2201 bool c;
2202 __asm float_less c, b, a;
2203 return c;
2204 }
2205
2206 bool __operator > (const int a, const int b) {
2207 return float (a) > float (b);
2208 }
2209
2210 bool __operator >= (const float a, const float b) {
2211 bool g, e;
2212 __asm float_less g, b, a;
2213 __asm float_equal e, a, b;
2214 return g || e;
2215 }
2216
2217 bool __operator >= (const int a, const int b) {
2218 return float (a) >= float (b);
2219 }
2220
2221 bool __operator <= (const float a, const float b) {
2222 bool g, e;
2223 __asm float_less g, a, b;
2224 __asm float_equal e, a, b;
2225 return g || e;
2226 }
2227
2228 bool __operator <= (const int a, const int b) {
2229 return float (a) <= float (b);
2230 }
2231
2232 bool __operator ^^ (const bool a, const bool b) {
2233 return a != b;
2234 }
2235
2236
2237
2238 //
2239 // These operators are handled internally by the compiler:
2240 //
2241 // bool __operator && (bool a, bool b) {
2242 // return a ? b : false;
2243 // }
2244 // bool __operator || (bool a, bool b) {
2245 // return a ? true : b;
2246 // }
2247 //
2248
2249 bool __operator ! (const bool a) {
2250 return a == false;
2251 }
2252
2253 bool __logicalAnd(const bool a, const bool b)
2254 {
2255 if (a)
2256 return b;
2257 return false;
2258 }
2259
2260
2261
2262 //
2263 // MESA-specific extension functions.
2264 //
2265
2266 void printMESA (const float f) {
2267 __asm float_print f;
2268 }
2269
2270 void printMESA (const int i) {
2271 __asm int_print i;
2272 }
2273
2274 void printMESA (const bool b) {
2275 __asm bool_print b;
2276 }
2277
2278 void printMESA (const vec2 v) {
2279 printMESA (v.x);
2280 printMESA (v.y);
2281 }
2282
2283 void printMESA (const vec3 v) {
2284 printMESA (v.x);
2285 printMESA (v.y);
2286 printMESA (v.z);
2287 }
2288
2289 void printMESA (const vec4 v) {
2290 printMESA (v.x);
2291 printMESA (v.y);
2292 printMESA (v.z);
2293 printMESA (v.w);
2294 }
2295
2296 void printMESA (const ivec2 v) {
2297 printMESA (v.x);
2298 printMESA (v.y);
2299 }
2300
2301 void printMESA (const ivec3 v) {
2302 printMESA (v.x);
2303 printMESA (v.y);
2304 printMESA (v.z);
2305 }
2306
2307 void printMESA (const ivec4 v) {
2308 printMESA (v.x);
2309 printMESA (v.y);
2310 printMESA (v.z);
2311 printMESA (v.w);
2312 }
2313
2314 void printMESA (const bvec2 v) {
2315 printMESA (v.x);
2316 printMESA (v.y);
2317 }
2318
2319 void printMESA (const bvec3 v) {
2320 printMESA (v.x);
2321 printMESA (v.y);
2322 printMESA (v.z);
2323 }
2324
2325 void printMESA (const bvec4 v) {
2326 printMESA (v.x);
2327 printMESA (v.y);
2328 printMESA (v.z);
2329 printMESA (v.w);
2330 }
2331
2332 void printMESA (const mat2 m) {
2333 printMESA (m[0]);
2334 printMESA (m[1]);
2335 }
2336
2337 void printMESA (const mat3 m) {
2338 printMESA (m[0]);
2339 printMESA (m[1]);
2340 printMESA (m[2]);
2341 }
2342
2343 void printMESA (const mat4 m) {
2344 printMESA (m[0]);
2345 printMESA (m[1]);
2346 printMESA (m[2]);
2347 printMESA (m[3]);
2348 }
2349
2350 void printMESA (const sampler1D e) {
2351 __asm int_print e;
2352 }
2353
2354 void printMESA (const sampler2D e) {
2355 __asm int_print e;
2356 }
2357
2358 void printMESA (const sampler3D e) {
2359 __asm int_print e;
2360 }
2361
2362 void printMESA (const samplerCube e) {
2363 __asm int_print e;
2364 }
2365
2366 void printMESA (const sampler1DShadow e) {
2367 __asm int_print e;
2368 }
2369
2370 void printMESA (const sampler2DShadow e) {
2371 __asm int_print e;
2372 }
2373