GLSL fixes:
[mesa.git] / src / mesa / shader / slang / library / slang_common_builtin.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 // From Shader Spec, ver. 1.10, rev. 59
27 //
28
29 const int gl_MaxLights = 8;
30 const int gl_MaxClipPlanes = 6;
31 const int gl_MaxTextureUnits = 8;
32 const int gl_MaxTextureCoords = 8;
33 const int gl_MaxVertexAttribs = 16;
34 const int gl_MaxVertexUniformComponents = 512;
35 const int gl_MaxVaryingFloats = 32;
36 const int gl_MaxVertexTextureImageUnits = 0;
37 const int gl_MaxCombinedTextureImageUnits = 2;
38 const int gl_MaxTextureImageUnits = 2;
39 const int gl_MaxFragmentUniformComponents = 64;
40 const int gl_MaxDrawBuffers = 1;
41
42 uniform mat4 gl_ModelViewMatrix;
43 uniform mat4 gl_ProjectionMatrix;
44 uniform mat4 gl_ModelViewProjectionMatrix;
45 uniform mat4 gl_TextureMatrix[gl_MaxTextureCoords];
46
47 uniform mat3 gl_NormalMatrix;
48
49 uniform mat4 gl_ModelViewMatrixInverse;
50 uniform mat4 gl_ProjectionMatrixInverse;
51 uniform mat4 gl_ModelViewProjectionMatrixInverse;
52 uniform mat4 gl_TextureMatrixInverse[gl_MaxTextureCoords];
53
54 uniform mat4 gl_ModelViewMatrixTranspose;
55 uniform mat4 gl_ProjectionMatrixTranspose;
56 uniform mat4 gl_ModelViewProjectionMatrixTranspose;
57 uniform mat4 gl_TextureMatrixTranspose[gl_MaxTextureCoords];
58
59 uniform mat4 gl_ModelViewMatrixInverseTranspose;
60 uniform mat4 gl_ProjectionMatrixInverseTranspose;
61 uniform mat4 gl_ModelViewProjectionMatrixInverseTranspose;
62 uniform mat4 gl_TextureMatrixInverseTranspose[gl_MaxTextureCoords];
63
64 uniform float gl_NormalScale;
65
66 struct gl_DepthRangeParameters {
67 float near;
68 float far;
69 float diff;
70 };
71
72 uniform gl_DepthRangeParameters gl_DepthRange;
73
74 uniform vec4 gl_ClipPlane[gl_MaxClipPlanes];
75
76 struct gl_PointParameters {
77 float size;
78 float sizeMin;
79 float sizeMax;
80 float fadeThresholdSize;
81 float distanceConstantAttenuation;
82 float distanceLinearAttenuation;
83 float distanceQuadraticAttenuation;
84 };
85
86 uniform gl_PointParameters gl_Point;
87
88 struct gl_MaterialParameters {
89 vec4 emission;
90 vec4 ambient;
91 vec4 diffuse;
92 vec4 specular;
93 float shininess;
94 };
95
96 uniform gl_MaterialParameters gl_FrontMaterial;
97 uniform gl_MaterialParameters gl_BackMaterial;
98
99 struct gl_LightSourceParameters {
100 vec4 ambient;
101 vec4 diffuse;
102 vec4 specular;
103 vec4 position;
104 vec4 halfVector;
105 vec3 spotDirection;
106 float spotExponent;
107 float spotCutoff;
108 float spotCosCutoff;
109 float constantAttenuation;
110 float linearAttenuation;
111 float quadraticAttenuation;
112 };
113
114 uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];
115
116 struct gl_LightModelParameters {
117 vec4 ambient;
118 };
119
120 uniform gl_LightModelParameters gl_LightModel;
121
122 struct gl_LightModelProducts {
123 vec4 sceneColor;
124 };
125
126 uniform gl_LightModelProducts gl_FrontLightModelProduct;
127 uniform gl_LightModelProducts gl_BackLightModelProduct;
128
129 struct gl_LightProducts {
130 vec4 ambient;
131 vec4 diffuse;
132 vec4 specular;
133 };
134
135 uniform gl_LightProducts gl_FrontLightProduct[gl_MaxLights];
136 uniform gl_LightProducts gl_BackLightProduct[gl_MaxLights];
137
138 uniform vec4 gl_TextureEnvColor[gl_MaxTextureImageUnits];
139 uniform vec4 gl_EyePlaneS[gl_MaxTextureCoords];
140 uniform vec4 gl_EyePlaneT[gl_MaxTextureCoords];
141 uniform vec4 gl_EyePlaneR[gl_MaxTextureCoords];
142 uniform vec4 gl_EyePlaneQ[gl_MaxTextureCoords];
143 uniform vec4 gl_ObjectPlaneS[gl_MaxTextureCoords];
144 uniform vec4 gl_ObjectPlaneT[gl_MaxTextureCoords];
145 uniform vec4 gl_ObjectPlaneR[gl_MaxTextureCoords];
146 uniform vec4 gl_ObjectPlaneQ[gl_MaxTextureCoords];
147
148 struct gl_FogParameters {
149 vec4 color;
150 float density;
151 float start;
152 float end;
153 float scale;
154 };
155
156 uniform gl_FogParameters gl_Fog;
157
158 //
159 // 8.1 Angle and Trigonometry Functions
160 //
161
162 float radians (float deg) {
163 return 3.141593 * deg / 180.0;
164 }
165
166 vec2 radians (vec2 deg) {
167 return vec2 (3.141593) * deg / vec2 (180.0);
168 }
169
170 vec3 radians (vec3 deg) {
171 return vec3 (3.141593) * deg / vec3 (180.0);
172 }
173
174 vec4 radians (vec4 deg) {
175 return vec4 (3.141593) * deg / vec4 (180.0);
176 }
177
178 float degrees (float rad) {
179 return 180.0 * rad / 3.141593;
180 }
181
182 vec2 degrees (vec2 rad) {
183 return vec2 (180.0) * rad / vec2 (3.141593);
184 }
185
186 vec3 degrees (vec3 rad) {
187 return vec3 (180.0) * rad / vec3 (3.141593);
188 }
189
190 vec4 degrees (vec4 rad) {
191 return vec4 (180.0) * rad / vec4 (3.141593);
192 }
193
194 float sin (float angle) {
195 float x;
196 __asm float_sine x, angle;
197 return x;
198 }
199
200 vec2 sin (vec2 angle) {
201 return vec2 (
202 sin (angle.x),
203 sin (angle.y)
204 );
205 }
206
207 vec3 sin (vec3 angle) {
208 return vec3 (
209 sin (angle.x),
210 sin (angle.y),
211 sin (angle.z)
212 );
213 }
214
215 vec4 sin (vec4 angle) {
216 return vec4 (
217 sin (angle.x),
218 sin (angle.y),
219 sin (angle.z),
220 sin (angle.w)
221 );
222 }
223
224 float cos (float angle) {
225 return sin (angle + 1.5708);
226 }
227
228 vec2 cos (vec2 angle) {
229 return vec2 (
230 cos (angle.x),
231 cos (angle.y)
232 );
233 }
234
235 vec3 cos (vec3 angle) {
236 return vec3 (
237 cos (angle.x),
238 cos (angle.y),
239 cos (angle.z)
240 );
241 }
242
243 vec4 cos (vec4 angle) {
244 return vec4 (
245 cos (angle.x),
246 cos (angle.y),
247 cos (angle.z),
248 cos (angle.w)
249 );
250 }
251
252 float tan (float angle) {
253 return sin (angle) / cos (angle);
254 }
255
256 vec2 tan (vec2 angle) {
257 return vec2 (
258 tan (angle.x),
259 tan (angle.y)
260 );
261 }
262
263 vec3 tan (vec3 angle) {
264 return vec3 (
265 tan (angle.x),
266 tan (angle.y),
267 tan (angle.z)
268 );
269 }
270
271 vec4 tan (vec4 angle) {
272 return vec4 (
273 tan (angle.x),
274 tan (angle.y),
275 tan (angle.z),
276 tan (angle.w)
277 );
278 }
279
280 float asin (float x) {
281 float y;
282 __asm float_arcsine y, x;
283 return y;
284 }
285
286 vec2 asin (vec2 v) {
287 return vec2 (
288 asin (v.x),
289 asin (v.y)
290 );
291 }
292
293 vec3 asin (vec3 v) {
294 return vec3 (
295 asin (v.x),
296 asin (v.y),
297 asin (v.z)
298 );
299 }
300
301 vec4 asin (vec4 v) {
302 return vec4 (
303 asin (v.x),
304 asin (v.y),
305 asin (v.z),
306 asin (v.w)
307 );
308 }
309
310 float acos (float x) {
311 return 1.5708 - asin (x);
312 }
313
314 vec2 acos (vec2 v) {
315 return vec2 (
316 acos (v.x),
317 acos (v.y)
318 );
319 }
320
321 vec3 acos (vec3 v) {
322 return vec3 (
323 acos (v.x),
324 acos (v.y),
325 acos (v.z)
326 );
327 }
328
329 vec4 acos (vec4 v) {
330 return vec4 (
331 acos (v.x),
332 acos (v.y),
333 acos (v.z),
334 acos (v.w)
335 );
336 }
337
338 float atan (float y_over_x) {
339 float z;
340 __asm float_arctan z, y_over_x;
341 return z;
342 }
343
344 vec2 atan (vec2 y_over_x) {
345 return vec2 (
346 atan (y_over_x.x),
347 atan (y_over_x.y)
348 );
349 }
350
351 vec3 atan (vec3 y_over_x) {
352 return vec3 (
353 atan (y_over_x.x),
354 atan (y_over_x.y),
355 atan (y_over_x.z)
356 );
357 }
358
359 vec4 atan (vec4 y_over_x) {
360 return vec4 (
361 atan (y_over_x.x),
362 atan (y_over_x.y),
363 atan (y_over_x.z),
364 atan (y_over_x.w)
365 );
366 }
367
368 float atan (float y, float x) {
369 float z = atan (y / x);
370 if (x < 0.0)
371 {
372 if (y < 0.0)
373 return z - 3.141593;
374 return z + 3.141593;
375 }
376 return z;
377 }
378
379 vec2 atan (vec2 u, vec2 v) {
380 return vec2 (
381 atan (u.x, v.x),
382 atan (u.y, v.y)
383 );
384 }
385
386 vec3 atan (vec3 u, vec3 v) {
387 return vec3 (
388 atan (u.x, v.x),
389 atan (u.y, v.y),
390 atan (u.z, v.z)
391 );
392 }
393
394 vec4 atan (vec4 u, vec4 v) {
395 return vec4 (
396 atan (u.x, v.x),
397 atan (u.y, v.y),
398 atan (u.z, v.z),
399 atan (u.w, v.w)
400 );
401 }
402
403 //
404 // 8.2 Exponential Functions
405 //
406
407 float pow (float x, float y) {
408 float p;
409 __asm float_power p, x, y;
410 return p;
411 }
412
413 vec2 pow (vec2 v, vec2 u) {
414 return vec2 (
415 pow (v.x, u.x),
416 pow (v.y, u.y)
417 );
418 }
419
420 vec3 pow (vec3 v, vec3 u) {
421 return vec3 (
422 pow (v.x, u.x),
423 pow (v.y, u.y),
424 pow (v.z, u.z)
425 );
426 }
427
428 vec4 pow (vec4 v, vec4 u) {
429 return vec4 (
430 pow (v.x, u.x),
431 pow (v.y, u.y),
432 pow (v.z, u.z),
433 pow (v.w, u.w)
434 );
435 }
436
437 float exp (float x) {
438 return pow (2.71828183, x);
439 }
440
441 vec2 exp (vec2 v) {
442 return pow (vec2 (2.71828183), v);
443 }
444
445 vec3 exp (vec3 v) {
446 return pow (vec3 (2.71828183), v);
447 }
448
449 vec4 exp (vec4 v) {
450 return pow (vec4 (2.71828183), v);
451 }
452
453 float log2 (float x) {
454 float y;
455 __asm float_log2 y, x;
456 return y;
457 }
458
459 vec2 log2 (vec2 v) {
460 return vec2 (
461 log2 (v.x),
462 log2 (v.y)
463 );
464 }
465
466 vec3 log2 (vec3 v) {
467 return vec3 (
468 log2 (v.x),
469 log2 (v.y),
470 log2 (v.z)
471 );
472 }
473
474 vec4 log2 (vec4 v) {
475 return vec4 (
476 log2 (v.x),
477 log2 (v.y),
478 log2 (v.z),
479 log2 (v.w)
480 );
481 }
482
483 float log (float x) {
484 return log2 (x) / log2 (2.71828183);
485 }
486
487 vec2 log (vec2 v) {
488 return log2 (v) / log2 (vec2 (2.71828183));
489 }
490
491 vec3 log (vec3 v) {
492 return log2 (v) / log2 (vec3 (2.71828183));
493 }
494
495 vec4 log (vec4 v) {
496 return log2 (v) / log2 (vec4 (2.71828183));
497 }
498
499 float exp2 (float x) {
500 return pow (2.0, x);
501 }
502
503 vec2 exp2 (vec2 v) {
504 return pow (vec2 (2.0), v);
505 }
506
507 vec3 exp2 (vec3 v) {
508 return pow (vec3 (2.0), v);
509 }
510
511 vec4 exp2 (vec4 v) {
512 return pow (vec4 (2.0), v);
513 }
514
515 float sqrt (float x) {
516 return pow (x, 0.5);
517 }
518
519 vec2 sqrt (vec2 v) {
520 return pow (v, vec2 (0.5));
521 }
522
523 vec3 sqrt (vec3 v) {
524 return pow (v, vec3 (0.5));
525 }
526
527 vec4 sqrt (vec4 v) {
528 return pow (v, vec4 (0.5));
529 }
530
531 float inversesqrt (float x) {
532 return 1.0 / sqrt (x);
533 }
534
535 vec2 inversesqrt (vec2 v) {
536 return vec2 (1.0) / sqrt (v);
537 }
538
539 vec3 inversesqrt (vec3 v) {
540 return vec3 (1.0) / sqrt (v);
541 }
542
543 vec4 inversesqrt (vec4 v) {
544 return vec4 (1.0) / sqrt (v);
545 }
546
547 //
548 // 8.3 Common Functions
549 //
550
551 float abs (float x) {
552 return x >= 0.0 ? x : -x;
553 }
554
555 vec2 abs (vec2 v) {
556 return vec2 (
557 abs (v.x),
558 abs (v.y)
559 );
560 }
561
562 vec3 abs (vec3 v) {
563 return vec3 (
564 abs (v.x),
565 abs (v.y),
566 abs (v.z)
567 );
568 }
569
570 vec4 abs (vec4 v) {
571 return vec4 (
572 abs (v.x),
573 abs (v.y),
574 abs (v.z),
575 abs (v.w)
576 );
577 }
578
579 float sign (float x) {
580 return x > 0.0 ? 1.0 : x < 0.0 ? -1.0 : 0.0;
581 }
582
583 vec2 sign (vec2 v) {
584 return vec2 (
585 sign (v.x),
586 sign (v.y)
587 );
588 }
589
590 vec3 sign (vec3 v) {
591 return vec3 (
592 sign (v.x),
593 sign (v.y),
594 sign (v.z)
595 );
596 }
597
598 vec4 sign (vec4 v) {
599 return vec4 (
600 sign (v.x),
601 sign (v.y),
602 sign (v.z),
603 sign (v.w)
604 );
605 }
606
607 float floor (float x) {
608 float y;
609 __asm float_floor y, x;
610 return y;
611 }
612
613 vec2 floor (vec2 v) {
614 return vec2 (
615 floor (v.x),
616 floor (v.y)
617 );
618 }
619
620 vec3 floor (vec3 v) {
621 return vec3 (
622 floor (v.x),
623 floor (v.y),
624 floor (v.z)
625 );
626 }
627
628 vec4 floor (vec4 v) {
629 return vec4 (
630 floor (v.x),
631 floor (v.y),
632 floor (v.z),
633 floor (v.w)
634 );
635 }
636
637 float ceil (float x) {
638 float y;
639 __asm float_ceil y, x;
640 return y;
641 }
642
643 vec2 ceil (vec2 v) {
644 return vec2 (
645 ceil (v.x),
646 ceil (v.y)
647 );
648 }
649
650 vec3 ceil (vec3 v) {
651 return vec3 (
652 ceil (v.x),
653 ceil (v.y),
654 ceil (v.z)
655 );
656 }
657
658 vec4 ceil (vec4 v) {
659 return vec4 (
660 ceil (v.x),
661 ceil (v.y),
662 ceil (v.z),
663 ceil (v.w)
664 );
665 }
666
667 float fract (float x) {
668 return x - floor (x);
669 }
670
671 vec2 fract (vec2 v) {
672 return v - floor (v);
673 }
674
675 vec3 fract (vec3 v) {
676 return v - floor (v);
677 }
678
679 vec4 fract (vec4 v) {
680 return v - floor (v);
681 }
682
683 float mod (float x, float y) {
684 return x - y * floor (x / y);
685 }
686
687 vec2 mod (vec2 v, float u) {
688 return v - u * floor (v / u);
689 }
690
691 vec3 mod (vec3 v, float u) {
692 return v - u * floor (v / u);
693 }
694
695 vec4 mod (vec4 v, float u) {
696 return v - u * floor (v / u);
697 }
698
699 vec2 mod (vec2 v, vec2 u) {
700 return v - u * floor (v / u);
701 }
702
703 vec3 mod (vec3 v, vec3 u) {
704 return v - u * floor (v / u);
705 }
706
707 vec4 mod (vec4 v, vec4 u) {
708 return v - u * floor (v / u);
709 }
710
711 float min (float x, float y) {
712 return x < y ? x : y;
713 }
714
715 vec2 min (vec2 v, vec2 u) {
716 return vec2 (
717 min (v.x, u.x),
718 min (v.y, u.y)
719 );
720 }
721
722 vec3 min (vec3 v, vec3 u) {
723 return vec3 (
724 min (v.x, u.x),
725 min (v.y, u.y),
726 min (v.z, u.z)
727 );
728 }
729
730 vec4 min (vec4 v, vec4 u) {
731 return vec4 (
732 min (v.x, u.x),
733 min (v.y, u.y),
734 min (v.z, u.z),
735 min (v.w, u.w)
736 );
737 }
738
739 vec2 min (vec2 v, float y) {
740 return min (v, vec2 (y));
741 }
742
743 vec3 min (vec3 v, float y) {
744 return min (v, vec3 (y));
745 }
746
747 vec4 min (vec4 v, float y) {
748 return min (v, vec4 (y));
749 }
750
751 float max (float x, float y) {
752 return x < y ? y : x;
753 }
754
755 vec2 max (vec2 v, vec2 u) {
756 return vec2 (
757 max (v.x, u.x),
758 max (v.y, u.y)
759 );
760 }
761
762 vec3 max (vec3 v, vec3 u) {
763 return vec3 (
764 max (v.x, u.x),
765 max (v.y, u.y),
766 max (v.z, u.z)
767 );
768 }
769
770 vec4 max (vec4 v, vec4 u) {
771 return vec4 (
772 max (v.x, u.x),
773 max (v.y, u.y),
774 max (v.z, u.z),
775 max (v.w, u.w)
776 );
777 }
778
779 vec2 max (vec2 v, float y) {
780 return max (v, vec2 (y));
781 }
782
783 vec3 max (vec3 v, float y) {
784 return max (v, vec3 (y));
785 }
786
787 vec4 max (vec4 v, float y) {
788 return max (v, vec4 (y));
789 }
790
791 float clamp (float x, float minVal, float maxVal) {
792 return min (max (x, minVal), maxVal);
793 }
794
795 vec2 clamp (vec2 x, float minVal, float maxVal) {
796 return min (max (x, minVal), maxVal);
797 }
798
799 vec3 clamp (vec3 x, float minVal, float maxVal) {
800 return min (max (x, minVal), maxVal);
801 }
802
803 vec4 clamp (vec4 x, float minVal, float maxVal) {
804 return min (max (x, minVal), maxVal);
805 }
806
807 vec2 clamp (vec2 x, vec2 minVal, vec2 maxVal) {
808 return min (max (x, minVal), maxVal);
809 }
810
811 vec3 clamp (vec3 x, vec3 minVal, vec3 maxVal) {
812 return min (max (x, minVal), maxVal);
813 }
814
815 vec4 clamp (vec4 x, vec4 minVal, vec4 maxVal) {
816 return min (max (x, minVal), maxVal);
817 }
818
819 float mix (float x, float y, float a) {
820 return x * (1.0 - a) + y * a;
821 }
822
823 vec2 mix (vec2 x, vec2 y, float a) {
824 return x * (1.0 - a) + y * a;
825 }
826
827 vec3 mix (vec3 x, vec3 y, float a) {
828 return x * (1.0 - a) + y * a;
829 }
830
831 vec4 mix (vec4 x, vec4 y, float a) {
832 return x * (1.0 - a) + y * a;
833 }
834
835 vec2 mix (vec2 x, vec2 y, vec2 a) {
836 return x * (1.0 - a) + y * a;
837 }
838
839 vec3 mix (vec3 x, vec3 y, vec3 a) {
840 return x * (1.0 - a) + y * a;
841 }
842
843 vec4 mix (vec4 x, vec4 y, vec4 a) {
844 return x * (1.0 - a) + y * a;
845 }
846
847 float step (float edge, float x) {
848 return x < edge ? 0.0 : 1.0;
849 }
850
851 vec2 step (vec2 edge, vec2 v) {
852 return vec2 (
853 step (edge.x, v.x),
854 step (edge.y, v.y)
855 );
856 }
857
858 vec3 step (vec3 edge, vec3 v) {
859 return vec3 (
860 step (edge.x, v.x),
861 step (edge.y, v.y),
862 step (edge.z, v.z)
863 );
864 }
865
866 vec4 step (vec4 edge, vec4 v) {
867 return vec4 (
868 step (edge.x, v.x),
869 step (edge.y, v.y),
870 step (edge.z, v.z),
871 step (edge.w, v.w)
872 );
873 }
874
875 vec2 step (float edge, vec2 v) {
876 return step (vec2 (edge), v);
877 }
878
879 vec3 step (float edge, vec3 v) {
880 return step (vec3 (edge), v);
881 }
882
883 vec4 step (float edge, vec4 v) {
884 return step (vec4 (edge), v);
885 }
886
887 float smoothstep (float edge0, float edge1, float x) {
888 float t = clamp ((x - edge0) / (edge1 - edge0), 0.0, 1.0);
889 return t * t * (3.0 - 2.0 * t);
890 }
891
892 vec2 smoothstep (vec2 edge0, vec2 edge1, vec2 v) {
893 return vec2 (
894 smoothstep (edge0.x, edge1.x, v.x),
895 smoothstep (edge0.y, edge1.y, v.y)
896 );
897 }
898
899 vec3 smoothstep (vec3 edge0, vec3 edge1, vec3 v) {
900 return vec3 (
901 smoothstep (edge0.x, edge1.x, v.x),
902 smoothstep (edge0.y, edge1.y, v.y),
903 smoothstep (edge0.z, edge1.z, v.z)
904 );
905 }
906
907 vec4 smoothstep (vec4 edge0, vec4 edge1, vec4 v) {
908 return vec4 (
909 smoothstep (edge0.x, edge1.x, v.x),
910 smoothstep (edge0.y, edge1.y, v.y),
911 smoothstep (edge0.z, edge1.z, v.z),
912 smoothstep (edge0.w, edge1.w, v.w)
913 );
914 }
915
916 vec2 smoothstep (float edge0, float edge1, vec2 v) {
917 return vec2 (
918 smoothstep (edge0, edge1, v.x),
919 smoothstep (edge0, edge1, v.y)
920 );
921 }
922
923 vec3 smoothstep (float edge0, float edge1, vec3 v) {
924 return vec3 (
925 smoothstep (edge0, edge1, v.x),
926 smoothstep (edge0, edge1, v.y),
927 smoothstep (edge0, edge1, v.z)
928 );
929 }
930
931 vec4 smoothstep (float edge0, float edge1, vec4 v) {
932 return vec4 (
933 smoothstep (edge0, edge1, v.x),
934 smoothstep (edge0, edge1, v.y),
935 smoothstep (edge0, edge1, v.z),
936 smoothstep (edge0, edge1, v.w)
937 );
938 }
939
940 //
941 // 8.4 Geometric Functions
942 //
943
944 float dot (float x, float y) {
945 return x * y;
946 }
947
948 float dot (vec2 v, vec2 u) {
949 return v.x * u.x + v.y * u.y;
950 }
951
952 float dot (vec3 v, vec3 u) {
953 return v.x * u.x + v.y * u.y + v.z * u.z;
954 }
955
956 float dot (vec4 v, vec4 u) {
957 return v.x * u.x + v.y * u.y + v.z * u.z + v.w * u.w;
958 }
959
960 float length (float x) {
961 return sqrt (dot (x, x));
962 }
963
964 float length (vec2 v) {
965 return sqrt (dot (v, v));
966 }
967
968 float length (vec3 v) {
969 return sqrt (dot (v, v));
970 }
971
972 float length (vec4 v) {
973 return sqrt (dot (v, v));
974 }
975
976 float distance (float x, float y) {
977 return length (x - y);
978 }
979
980 float distance (vec2 v, vec2 u) {
981 return length (v - u);
982 }
983
984 float distance (vec3 v, vec3 u) {
985 return length (v - u);
986 }
987
988 float distance (vec4 v, vec4 u) {
989 return length (v - u);
990 }
991
992 vec3 cross (vec3 v, vec3 u) {
993 return vec3 (
994 v.y * u.z - u.y * v.z,
995 v.z * u.x - u.z * v.x,
996 v.x * u.y - u.x * v.y
997 );
998 }
999
1000 float normalize (float x) {
1001 return 1.0;
1002 }
1003
1004 vec2 normalize (vec2 v) {
1005 return v / length (v);
1006 }
1007
1008 vec3 normalize (vec3 v) {
1009 return v / length (v);
1010 }
1011
1012 vec4 normalize (vec4 v) {
1013 return v / length (v);
1014 }
1015
1016 float faceforward (float N, float I, float Nref) {
1017 return dot (Nref, I) < 0.0 ? N : -N;
1018 }
1019
1020 vec2 faceforward (vec2 N, vec2 I, vec2 Nref) {
1021 return dot (Nref, I) < 0.0 ? N : -N;
1022 }
1023
1024 vec3 faceforward (vec3 N, vec3 I, vec3 Nref) {
1025 return dot (Nref, I) < 0.0 ? N : -N;
1026 }
1027
1028 vec4 faceforward (vec4 N, vec4 I, vec4 Nref) {
1029 return dot (Nref, I) < 0.0 ? N : -N;
1030 }
1031
1032 float reflect (float I, float N) {
1033 return I - 2.0 * dot (N, I) * N;
1034 }
1035
1036 vec2 reflect (vec2 I, vec2 N) {
1037 return I - 2.0 * dot (N, I) * N;
1038 }
1039
1040 vec3 reflect (vec3 I, vec3 N) {
1041 return I - 2.0 * dot (N, I) * N;
1042 }
1043
1044 vec4 reflect (vec4 I, vec4 N) {
1045 return I - 2.0 * dot (N, I) * N;
1046 }
1047
1048 float refract (float I, float N, float eta) {
1049 float k = 1.0 - eta * eta * (1.0 - dot (N, I) * dot (N, I));
1050 if (k < 0.0)
1051 return 0.0;
1052 return eta * I - (eta * dot (N, I) + sqrt (k)) * N;
1053 }
1054
1055 vec2 refract (vec2 I, vec2 N, float eta) {
1056 float k = 1.0 - eta * eta * (1.0 - dot (N, I) * dot (N, I));
1057 if (k < 0.0)
1058 return 0.0;
1059 return eta * I - (eta * dot (N, I) + sqrt (k)) * N;
1060 }
1061
1062 vec3 refract (vec3 I, vec3 N, float eta) {
1063 float k = 1.0 - eta * eta * (1.0 - dot (N, I) * dot (N, I));
1064 if (k < 0.0)
1065 return 0.0;
1066 return eta * I - (eta * dot (N, I) + sqrt (k)) * N;
1067 }
1068
1069 vec4 refract (vec4 I, vec4 N, float eta) {
1070 float k = 1.0 - eta * eta * (1.0 - dot (N, I) * dot (N, I));
1071 if (k < 0.0)
1072 return 0.0;
1073 return eta * I - (eta * dot (N, I) + sqrt (k)) * N;
1074 }
1075
1076 //
1077 // 8.5 Matrix Functions
1078 //
1079
1080 mat2 matrixCompMult (mat2 m, mat2 n) {
1081 return mat2 (m[0] * n[0], m[1] * n[1]);
1082 }
1083
1084 mat3 matrixCompMult (mat3 m, mat3 n) {
1085 return mat3 (m[0] * n[0], m[1] * n[1], m[2] * n[2]);
1086 }
1087
1088 mat4 matrixCompMult (mat4 m, mat4 n) {
1089 return mat4 (m[0] * n[0], m[1] * n[1], m[2] * n[2], m[3] * n[3]);
1090 }
1091
1092 //
1093 // 8.6 Vector Relational Functions
1094 //
1095
1096 bvec2 lessThan (vec2 v, vec2 u) {
1097 return bvec2 (v.x < u.x, v.y < u.y);
1098 }
1099
1100 bvec3 lessThan (vec3 v, vec3 u) {
1101 return bvec3 (v.x < u.x, v.y < u.y, v.z < u.z);
1102 }
1103
1104 bvec4 lessThan (vec4 v, vec4 u) {
1105 return bvec4 (v.x < u.x, v.y < u.y, v.z < u.z, v.w < u.w);
1106 }
1107
1108 bvec2 lessThan (ivec2 v, ivec2 u) {
1109 return bvec2 (v.x < u.x, v.y < u.y);
1110 }
1111
1112 bvec3 lessThan (ivec3 v, ivec3 u) {
1113 return bvec3 (v.x < u.x, v.y < u.y, v.z < u.z);
1114 }
1115
1116 bvec4 lessThan (ivec4 v, ivec4 u) {
1117 return bvec4 (v.x < u.x, v.y < u.y, v.z < u.z, v.w < u.w);
1118 }
1119
1120 bvec2 lessThanEqual (vec2 v, vec2 u) {
1121 return bvec2 (v.x <= u.x, v.y <= u.y);
1122 }
1123
1124 bvec3 lessThanEqual (vec3 v, vec3 u) {
1125 return bvec3 (v.x <= u.x, v.y <= u.y, v.z <= u.z);
1126 }
1127
1128 bvec4 lessThanEqual (vec4 v, vec4 u) {
1129 return bvec4 (v.x <= u.x, v.y <= u.y, v.z <= u.z, v.w <= u.w);
1130 }
1131
1132 bvec2 lessThanEqual (ivec2 v, ivec2 u) {
1133 return bvec2 (v.x <= u.x, v.y <= u.y);
1134 }
1135
1136 bvec3 lessThanEqual (ivec3 v, ivec3 u) {
1137 return bvec3 (v.x <= u.x, v.y <= u.y, v.z <= u.z);
1138 }
1139
1140 bvec4 lessThanEqual (ivec4 v, ivec4 u) {
1141 return bvec4 (v.x <= u.x, v.y <= u.y, v.z <= u.z, v.w <= u.w);
1142 }
1143
1144 bvec2 greaterThan (vec2 v, vec2 u) {
1145 return bvec2 (v.x > u.x, v.y > u.y);
1146 }
1147
1148 bvec3 greaterThan (vec3 v, vec3 u) {
1149 return bvec3 (v.x > u.x, v.y > u.y, v.z > u.z);
1150 }
1151
1152 bvec4 greaterThan (vec4 v, vec4 u) {
1153 return bvec4 (v.x > u.x, v.y > u.y, v.z > u.z, v.w > u.w);
1154 }
1155
1156 bvec2 greaterThan (ivec2 v, ivec2 u) {
1157 return bvec2 (v.x > u.x, v.y > u.y);
1158 }
1159
1160 bvec3 greaterThan (ivec3 v, ivec3 u) {
1161 return bvec3 (v.x > u.x, v.y > u.y, v.z > u.z);
1162 }
1163
1164 bvec4 greaterThan (ivec4 v, ivec4 u) {
1165 return bvec4 (v.x > u.x, v.y > u.y, v.z > u.z, v.w > u.w);
1166 }
1167
1168 bvec2 greaterThanEqual (vec2 v, vec2 u) {
1169 return bvec2 (v.x >= u.x, v.y >= u.y);
1170 }
1171
1172 bvec3 greaterThanEqual (vec3 v, vec3 u) {
1173 return bvec3 (v.x >= u.x, v.y >= u.y, v.z >= u.z);
1174 }
1175
1176 bvec4 greaterThanEqual (vec4 v, vec4 u) {
1177 return bvec4 (v.x >= u.x, v.y >= u.y, v.z >= u.z, v.w >= u.w);
1178 }
1179
1180 bvec2 greaterThanEqual (ivec2 v, ivec2 u) {
1181 return bvec2 (v.x >= u.x, v.y >= u.y);
1182 }
1183
1184 bvec3 greaterThanEqual (ivec3 v, ivec3 u) {
1185 return bvec3 (v.x >= u.x, v.y >= u.y, v.z >= u.z);
1186 }
1187
1188 bvec4 greaterThanEqual (ivec4 v, ivec4 u) {
1189 return bvec4 (v.x >= u.x, v.y >= u.y, v.z >= u.z, v.w >= u.w);
1190 }
1191
1192 bvec2 equal (vec2 v, vec2 u) {
1193 return bvec2 (v.x == u.x, v.y == u.y);
1194 }
1195
1196 bvec3 equal (vec3 v, vec3 u) {
1197 return bvec3 (v.x == u.x, v.y == u.y, v.z == u.z);
1198 }
1199
1200 bvec4 equal (vec4 v, vec4 u) {
1201 return bvec4 (v.x == u.x, v.y == u.y, v.z == u.z, v.w == u.w);
1202 }
1203
1204 bvec2 equal (ivec2 v, ivec2 u) {
1205 return bvec2 (v.x == u.x, v.y == u.y);
1206 }
1207
1208 bvec3 equal (ivec3 v, ivec3 u) {
1209 return bvec3 (v.x == u.x, v.y == u.y, v.z == u.z);
1210 }
1211
1212 bvec4 equal (ivec4 v, ivec4 u) {
1213 return bvec4 (v.x == u.x, v.y == u.y, v.z == u.z, v.w == u.w);
1214 }
1215
1216 bvec2 notEqual (vec2 v, vec2 u) {
1217 return bvec2 (v.x != u.x, v.y != u.y);
1218 }
1219
1220 bvec3 notEqual (vec3 v, vec3 u) {
1221 return bvec3 (v.x != u.x, v.y != u.y, v.z != u.z);
1222 }
1223
1224 bvec4 notEqual (vec4 v, vec4 u) {
1225 return bvec4 (v.x != u.x, v.y != u.y, v.z != u.z, v.w != u.w);
1226 }
1227
1228 bvec2 notEqual (ivec2 v, ivec2 u) {
1229 return bvec2 (v.x != u.x, v.y != u.y);
1230 }
1231
1232 bvec3 notEqual (ivec3 v, ivec3 u) {
1233 return bvec3 (v.x != u.x, v.y != u.y, v.z != u.z);
1234 }
1235
1236 bvec4 notEqual (ivec4 v, ivec4 u) {
1237 return bvec4 (v.x != u.x, v.y != u.y, v.z != u.z, v.w != u.w);
1238 }
1239
1240 bool any (bvec2 v) {
1241 return v.x || v.y;
1242 }
1243
1244 bool any (bvec3 v) {
1245 return v.x || v.y || v.z;
1246 }
1247
1248 bool any (bvec4 v) {
1249 return v.x || v.y || v.z || v.w;
1250 }
1251
1252 bool all (bvec2 v) {
1253 return v.x && v.y;
1254 }
1255
1256 bool all (bvec3 v) {
1257 return v.x && v.y && v.z;
1258 }
1259
1260 bool all (bvec4 v) {
1261 return v.x && v.y && v.z && v.w;
1262 }
1263
1264 bvec2 not (bvec2 v) {
1265 return bvec2 (!v.x, !v.y);
1266 }
1267
1268 bvec3 not (bvec3 v) {
1269 return bvec3 (!v.x, !v.y, !v.z);
1270 }
1271
1272 bvec4 not (bvec4 v) {
1273 return bvec4 (!v.x, !v.y, !v.z, !v.w);
1274 }
1275
1276 //
1277 // 8.7 Texture Lookup Functions
1278 //
1279
1280 vec4 texture1D (sampler1D sampler, float coord) {
1281 vec4 texel;
1282 __asm vec4_tex1d texel, sampler, coord, 0.0;
1283 return texel;
1284 }
1285
1286 vec4 texture1DProj (sampler1D sampler, vec2 coord) {
1287 return texture1D (sampler, coord.s / coord.t);
1288 }
1289
1290 vec4 texture1DProj (sampler1D sampler, vec4 coord) {
1291 return texture1D (sampler, coord.s / coord.q);
1292 }
1293
1294 vec4 texture2D (sampler2D sampler, vec2 coord) {
1295 vec4 texel;
1296 __asm vec4_tex2d texel, sampler, coord, 0.0;
1297 return texel;
1298 }
1299
1300 vec4 texture2DProj (sampler2D sampler, vec3 coord) {
1301 return texture2D (sampler, vec2 (coord.s / coord.p, coord.t / coord.p));
1302 }
1303
1304 vec4 texture2DProj (sampler2D sampler, vec4 coord) {
1305 return texture2D (sampler, vec2 (coord.s / coord.q, coord.t / coord.q));
1306 }
1307
1308 vec4 texture3D (sampler3D sampler, vec3 coord) {
1309 vec4 texel;
1310 __asm vec4_tex3d texel, sampler, coord, 0.0;
1311 return texel;
1312 }
1313
1314 vec4 texture3DProj (sampler3D sampler, vec4 coord) {
1315 return texture3D (sampler, vec3 (coord.s / coord.q, coord.t / coord.q, coord.p / coord.q));
1316 }
1317
1318 vec4 textureCube (samplerCube sampler, vec3 coord) {
1319 vec4 texel;
1320 __asm vec4_texcube texel, sampler, coord, 0.0;
1321 return texel;
1322 }
1323
1324 vec4 shadow1D (sampler1DShadow sampler, vec3 coord) {
1325 vec4 texel;
1326 __asm vec4_shad1d texel, sampler, coord, 0.0;
1327 return texel;
1328 }
1329
1330 vec4 shadow1DProj (sampler1DShadow sampler, vec4 coord) {
1331 return shadow1D (sampler, vec3 (coord.s / coord.q, 0.0, coord.p / coord.q));
1332 }
1333
1334 vec4 shadow2D (sampler2DShadow sampler, vec3 coord) {
1335 vec4 texel;
1336 __asm vec4_shad2d texel, sampler, coord, 0.0;
1337 return texel;
1338 }
1339
1340 vec4 shadow2DProj (sampler2DShadow sampler, vec4 coord) {
1341 return shadow2D (sampler, vec3 (coord.s / coord.q, coord.t / coord.q, coord.p / coord.q));
1342 }
1343
1344 //
1345 // 8.9 Noise Functions
1346 //
1347 // AUTHOR: Stefan Gustavson (stegu@itn.liu.se), Nov 26, 2005
1348 //
1349
1350 float noise1 (float x) {
1351 float a;
1352 __asm float_noise1 a, x;
1353 return a;
1354 }
1355
1356 float noise1 (vec2 x) {
1357 float a;
1358 __asm float_noise2 a, x;
1359 return a;
1360 }
1361
1362 float noise1 (vec3 x) {
1363 float a;
1364 __asm float_noise3 a, x;
1365 return a;
1366 }
1367
1368 float noise1 (vec4 x) {
1369 float a;
1370 __asm float_noise4 a, x;
1371 return a;
1372 }
1373
1374 vec2 noise2 (float x) {
1375 return vec2 (
1376 noise1 (x),
1377 noise1 (x + 19.34)
1378 );
1379 }
1380
1381 vec2 noise2 (vec2 x) {
1382 return vec2 (
1383 noise1 (x),
1384 noise1 (x + vec2 (19.34, 7.66))
1385 );
1386 }
1387
1388 vec2 noise2 (vec3 x) {
1389 return vec2 (
1390 noise1 (x),
1391 noise1 (x + vec3 (19.34, 7.66, 3.23))
1392 );
1393 }
1394
1395 vec2 noise2 (vec4 x) {
1396 return vec2 (
1397 noise1 (x),
1398 noise1 (x + vec4 (19.34, 7.66, 3.23, 2.77))
1399 );
1400 }
1401
1402 vec3 noise3 (float x) {
1403 return vec3 (
1404 noise1 (x),
1405 noise1 (x + 19.34),
1406 noise1 (x + 5.47)
1407 );
1408 }
1409
1410 vec3 noise3 (vec2 x) {
1411 return vec3 (
1412 noise1 (x),
1413 noise1 (x + vec2 (19.34, 7.66)),
1414 noise1 (x + vec2 (5.47, 17.85))
1415 );
1416 }
1417
1418 vec3 noise3 (vec3 x) {
1419 return vec3 (
1420 noise1 (x),
1421 noise1 (x + vec3 (19.34, 7.66, 3.23)),
1422 noise1 (x + vec3 (5.47, 17.85, 11.04))
1423 );
1424 }
1425
1426 vec3 noise3 (vec4 x) {
1427 return vec3 (
1428 noise1 (x),
1429 noise1 (x + vec4 (19.34, 7.66, 3.23, 2.77)),
1430 noise1 (x + vec4 (5.47, 17.85, 11.04, 13.19))
1431 );
1432 }
1433
1434 vec4 noise4 (float x) {
1435 return vec4 (
1436 noise1 (x),
1437 noise1 (x + 19.34),
1438 noise1 (x + 5.47),
1439 noise1 (x + 23.54)
1440 );
1441 }
1442
1443 vec4 noise4 (vec2 x) {
1444 return vec4 (
1445 noise1 (x),
1446 noise1 (x + vec2 (19.34, 7.66)),
1447 noise1 (x + vec2 (5.47, 17.85)),
1448 noise1 (x + vec2 (23.54, 29.11))
1449 );
1450 }
1451
1452 vec4 noise4 (vec3 x) {
1453 return vec4 (
1454 noise1 (x),
1455 noise1 (x + vec3 (19.34, 7.66, 3.23)),
1456 noise1 (x + vec3 (5.47, 17.85, 11.04)),
1457 noise1 (x + vec3 (23.54, 29.11, 31.91))
1458 );
1459 }
1460
1461 vec4 noise4 (vec4 x) {
1462 return vec4 (
1463 noise1 (x),
1464 noise1 (x + vec4 (19.34, 7.66, 3.23, 2.77)),
1465 noise1 (x + vec4 (5.47, 17.85, 11.04, 13.19)),
1466 noise1 (x + vec4 (23.54, 29.11, 31.91, 37.48))
1467 );
1468 }
1469