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