4 // - implement sin, asin, acos, atan, pow, log2, floor, ceil,
5 // - implement texture1D, texture2D, texture3D, textureCube,
6 // - implement shadow1D, shadow2D,
7 // - implement noise1, noise2, noise3, noise4,
11 // From Shader Spec, ver. 1.051
13 // The following built-in constants are provided to vertex and fragment shaders.
17 // Implementation dependent constants. The example values below
18 // are the minimum values allowed for these maximums.
21 const int gl_MaxLights = 8; // GL 1.0
22 const int gl_MaxClipPlanes = 6; // GL 1.0
23 const int gl_MaxTextureUnits = 2; // GL 1.2
24 const int gl_MaxTextureCoordsARB = 2; // ARB_fragment_program
25 const int gl_MaxVertexAttributesGL2 = 16; // GL2_vertex_shader
26 const int gl_MaxVertexUniformFloatsGL2 = 512; // GL2_vertex_shader
27 const int gl_MaxVaryingFloatsGL2 = 32; // GL2_vertex_shader
28 const int gl_MaxVertexTextureUnitsGL2 = 1; // GL2_vertex_shader
29 const int gl_MaxFragmentTextureUnitsGL2 = 2; // GL2_fragment_shader
30 const int gl_MaxFragmentUniformFloatsGL2 = 64; // GL2_fragment_shader
33 // As an aid to accessing OpenGL processing state, the following uniform variables are built into
34 // the OpenGL Shading Language. All page numbers and notations are references to the 1.4
39 // Matrix state. p. 31, 32, 37, 39, 40.
42 uniform mat4 gl_ModelViewMatrix;
43 uniform mat4 gl_ProjectionMatrix;
44 uniform mat4 gl_ModelViewProjectionMatrix;
45 uniform mat3 gl_NormalMatrix; // derived
46 uniform mat4 gl_TextureMatrix[gl_MaxTextureCoordsARB];
49 // Normal scaling p. 39.
52 uniform float gl_NormalScale;
55 // Depth range in window coordinates, p. 33
58 struct gl_DepthRangeParameters {
64 uniform gl_DepthRangeParameters gl_DepthRange;
70 uniform vec4 gl_ClipPlane[gl_MaxClipPlanes];
73 // Point Size, p. 66, 67.
76 struct gl_PointParameters {
80 float fadeThresholdSize;
81 float distanceConstantAttenuation;
82 float distanceLinearAttenuation;
83 float distanceQuadraticAttenuation;
86 uniform gl_PointParameters gl_Point;
89 // Material State p. 50, 55.
92 struct gl_MaterialParameters {
97 float shininess; // Srm
100 uniform gl_MaterialParameters gl_FrontMaterial;
101 uniform gl_MaterialParameters gl_BackMaterial;
104 // Light State p 50, 53, 55.
107 struct gl_LightSourceParameters {
108 vec4 ambient; // Acli
109 vec4 diffuse; // Dcli
110 vec4 specular; // Scli
111 vec4 position; // Ppli
112 vec4 halfVector; // Derived: Hi
113 vec3 spotDirection; // Sdli
114 float spotExponent; // Srli
115 float spotCutoff; // Crli
116 // (range: [0.0,90.0], 180.0)
117 float spotCosCutoff; // Derived: cos(Crli)
118 // (range: [1.0,0.0],-1.0)
119 float constantAttenuation; // K0
120 float linearAttenuation; // K1
121 float quadraticAttenuation; // K2
124 uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];
126 struct gl_LightModelParameters {
130 uniform gl_LightModelParameters gl_LightModel;
133 // Derived state from products of light and material.
136 struct gl_LightModelProducts {
137 vec4 sceneColor; // Derived. Ecm + Acm * Acs
140 uniform gl_LightModelProducts gl_FrontLightModelProduct;
141 uniform gl_LightModelProducts gl_BackLightModelProduct;
143 struct gl_LightProducts {
144 vec4 ambient; // Acm * Acli
145 vec4 diffuse; // Dcm * Dcli
146 vec4 specular; // Scm * Scli
149 uniform gl_LightProducts gl_FrontLightProduct[gl_MaxLights];
150 uniform gl_LightProducts gl_BackLightProduct[gl_MaxLights];
153 // Textureg Environment and Generation, p. 152, p. 40-42.
156 uniform vec4 gl_TextureEnvColor[gl_MaxFragmentTextureUnitsGL2];
157 uniform vec4 gl_EyePlaneS[gl_MaxTextureCoordsARB];
158 uniform vec4 gl_EyePlaneT[gl_MaxTextureCoordsARB];
159 uniform vec4 gl_EyePlaneR[gl_MaxTextureCoordsARB];
160 uniform vec4 gl_EyePlaneQ[gl_MaxTextureCoordsARB];
161 uniform vec4 gl_ObjectPlaneS[gl_MaxTextureCoordsARB];
162 uniform vec4 gl_ObjectPlaneT[gl_MaxTextureCoordsARB];
163 uniform vec4 gl_ObjectPlaneR[gl_MaxTextureCoordsARB];
164 uniform vec4 gl_ObjectPlaneQ[gl_MaxTextureCoordsARB];
170 struct gl_FogParameters {
175 float scale; // 1 / (gl_FogEnd - gl_FogStart)
178 uniform gl_FogParameters gl_Fog;
181 // The OpenGL Shading Language defines an assortment of built-in convenience functions for scalar
182 // and vector operations. Many of these built-in functions can be used in more than one type
183 // of shader, but some are intended to provide a direct mapping to hardware and so are available
184 // only for a specific type of shader.
186 // The built-in functions basically fall into three categories:
188 //
\95 They expose some necessary hardware functionality in a convenient way such as accessing
189 // a texture map. There is no way in the language for these functions to be emulated by a shader.
191 //
\95 They represent a trivial operation (clamp, mix, etc.) that is very simple for the user
192 // to write, but they are very common and may have direct hardware support. It is a very hard
193 // problem for the compiler to map expressions to complex assembler instructions.
195 //
\95 They represent an operation graphics hardware is likely to accelerate at some point. The
196 // trigonometry functions fall into this category.
198 // Many of the functions are similar to the same named ones in common C libraries, but they support
199 // vector input as well as the more traditional scalar input.
201 // Applications should be encouraged to use the built-in functions rather than do the equivalent
202 // computations in their own shader code since the built-in functions are assumed to be optimal
203 // (e.g., perhaps supported directly in hardware).
205 // User code can replace built-in functions with their own if they choose, by simply re-declaring
206 // and defining the same name and argument list.
210 // Angle and Trigonometry Functions
212 // Function parameters specified as angle are assumed to be in units of radians. In no case will
213 // any of these functions result in a divide by zero error. If the divisor of a ratio is 0, then
214 // results will be undefined.
216 // These all operate component-wise.
220 // Converts degrees to radians and returns the result, i.e., result = PI*deg/180.
223 float radians (float deg) {
224 return 3.141593 * deg / 180.0;
226 vec2 radians (vec2 deg) {
227 return vec2 (radians (deg.x), radians (deg.y));
229 vec3 radians (vec3 deg) {
230 return vec3 (radians (deg.x), radians (deg.y), radians (deg.z));
232 vec4 radians (vec4 deg) {
233 return vec4 (radians (deg.x), radians (deg.y), radians (deg.z), radians (deg.w));
237 // Converts radians to degrees and returns the result, i.e., result = 180*rad/PI.
240 float degrees (float rad) {
241 return 180.0 * rad / 3.141593;
243 vec2 degrees (vec2 rad) {
244 return vec2 (degrees (rad.x), degrees (rad.y));
246 vec3 degrees (vec3 rad) {
247 return vec3 (degrees (rad.x), degrees (rad.y), degrees (rad.z));
249 vec4 degrees (vec4 rad) {
250 return vec4 (degrees (rad.x), degrees (rad.y), degrees (rad.z), degrees (rad.w));
254 // The standard trigonometric sine function.
257 float sin (float angle) {
260 vec2 sin (vec2 angle) {
261 return vec2 (sin (angle.x), sin (angle.y));
263 vec3 sin (vec3 angle) {
264 return vec3 (sin (angle.x), sin (angle.y), sin (angle.z));
266 vec4 sin (vec4 angle) {
267 return vec4 (sin (angle.x), sin (angle.y), sin (angle.z), sin (angle.w));
271 // The standard trigonometric cosine function.
274 float cos (float angle) {
275 return sin (angle + 1.5708);
277 vec2 cos (vec2 angle) {
278 return vec2 (cos (angle.x), cos (angle.y));
280 vec3 cos (vec3 angle) {
281 return vec3 (cos (angle.x), cos (angle.y), cos (angle.z));
283 vec4 cos (vec4 angle) {
284 return vec4 (cos (angle.x), cos (angle.y), cos (angle.z), cos (angle.w));
288 // The standard trigonometric tangent.
291 float tan (float angle) {
292 return sin (angle) / cos (angle);
294 vec2 tan (vec2 angle) {
295 return vec2 (tan (angle.x), tan (angle.y));
297 vec3 tan (vec3 angle) {
298 return vec3 (tan (angle.x), tan (angle.y), tan (angle.z));
300 vec4 tan (vec4 angle) {
301 return vec4 (tan (angle.x), tan (angle.y), tan (angle.z), tan (angle.w));
305 // Arc sine. Returns an angle whose sine is x. The range of values returned by this function is
306 // [
\96PI/2, PI/2]. Results are undefined if |x| > 1.
309 float asin (float x) {
313 return vec2 (asin (x.x), asin (x.y));
316 return vec3 (asin (x.x), asin (x.y), asin (x.z));
319 return vec4 (asin (x.x), asin (x.y), asin (x.z), asin (x.w));
323 // Arc cosine. Returns an angle whose cosine is x. The range of values returned by this function is
324 // [0, PI]. Results are undefined if |x| > 1.
327 float acos (float x) {
331 return vec2 (acos (x.x), acos (x.y));
334 return vec3 (acos (x.x), acos (x.y), acos (x.z));
337 return vec4 (acos (x.x), acos (x.y), acos (x.z), acos (x.w));
341 // Arc tangent. Returns an angle whose tangent is y/x. The signs of x and y are used to determine
342 // what quadrant the angle is in. The range of values returned by this function is [
\96PI, PI].
343 // Results are undefined if x and y are both 0.
346 float atan (float x, float y) {
349 vec2 atan (vec2 x, vec2 y) {
350 return vec2 (atan (x.x, y.x), atan (x.y, y.y));
352 vec3 atan (vec3 x, vec3 y) {
353 return vec3 (atan (x.x, y.x), atan (x.y, y.y), atan (x.z, y.z));
355 vec4 atan (vec4 x, vec4 y) {
356 return vec4 (atan (x.x, y.x), atan (x.y, y.y), atan (x.z, y.z), atan (x.w, y.w));
360 // Arc tangent. Returns an angle whose tangent is y_over_x. The range of values returned by this
361 // function is [
\96PI/2, PI/2].
364 float atan (float y_over_x) {
367 vec2 atan (vec2 y_over_x) {
368 return vec2 (atan (y_over_x.x), atan (y_over_x.y));
370 vec3 atan (vec3 y_over_x) {
371 return vec3 (atan (y_over_x.x), atan (y_over_x.y), atan (y_over_x.z));
373 vec4 atan (vec4 y_over_x) {
374 return vec4 (atan (y_over_x.x), atan (y_over_x.y), atan (y_over_x.z), atan (y_over_x.w));
378 // Exponential Functions
380 // These all operate component-wise.
384 // Returns x raised to the y power, i.e., x^y
387 float pow (float x, float y) {
390 vec2 pow (vec2 x, vec2 y) {
391 return vec2 (pow (x.x, y.x), pow (x.y, y.y));
393 vec3 pow (vec3 x, vec3 y) {
394 return vec3 (pow (x.x, y.x), pow (x.y, y.y), pow (x.z, y.z));
396 vec4 pow (vec4 x, vec4 y) {
397 return vec4 (pow (x.x, y.x), pow (x.y, y.y), pow (x.z, y.z), pow (x.w, y.w));
401 // Returns 2 raised to the x power, i.e., 2^x
404 float exp2 (float x) {
408 return vec2 (exp2 (x.x), exp2 (x.y));
411 return vec3 (exp2 (x.x), exp2 (x.y), exp2 (x.z));
414 return vec4 (exp2 (x.x), exp2 (x.y), exp2 (x.z), exp2 (x.w));
418 // Returns the base 2 log of x, i.e., returns the value y which satisfies the equation x = 2^y
421 float log2 (float x) {
425 return vec2 (log2 (x.x), log2 (x.y));
428 return vec3 (log2 (x.x), log2 (x.y), log2 (x.z));
431 return vec4 (log2 (x.x), log2 (x.y), log2 (x.z), log2 (x.w));
435 // Returns the positive square root of x
438 float sqrt (float x) {
442 return vec2 (sqrt (x.x), sqrt (x.y));
445 return vec3 (sqrt (x.x), sqrt (x.y), sqrt (x.z));
448 return vec4 (sqrt (x.x), sqrt (x.y), sqrt (x.z), sqrt (x.w));
452 // Returns the reciprocal of the positive square root of x
455 float inversesqrt (float x) {
456 return 1.0 / sqrt (x);
458 vec2 inversesqrt (vec2 x) {
459 return vec2 (inversesqrt (x.x), inversesqrt (x.y));
461 vec3 inversesqrt (vec3 x) {
462 return vec3 (inversesqrt (x.x), inversesqrt (x.y), inversesqrt (x.z));
464 vec4 inversesqrt (vec4 x) {
465 return vec4 (inversesqrt (x.x), inversesqrt (x.y), inversesqrt (x.z), inversesqrt (x.w));
471 // These all operate component-wise.
475 // Returns x if x >= 0, otherwise it returns
\96x
478 float abs (float x) {
479 return x >= 0.0 ? x : -x;
482 return vec2 (abs (x.x), abs (x.y));
485 return vec3 (abs (x.x), abs (x.y), abs (x.z));
488 return vec4 (abs (x.x), abs (x.y), abs (x.z), abs (x.w));
492 // Returns 1.0 if x > 0, 0.0 if x = 0, or
\961.0 if x < 0
495 float sign (float x) {
496 return x > 0.0 ? 1.0 : x < 0.0 ? -1.0 : 0.0;
499 return vec2 (sign (x.x), sign (x.y));
502 return vec3 (sign (x.x), sign (x.y), sign (x.z));
505 return vec4 (sign (x.x), sign (x.y), sign (x.z), sign (x.w));
509 // Returns a value equal to the nearest integer that is less than or equal to x
512 float floor (float x) {
515 vec2 floor (vec2 x) {
516 return vec2 (floor (x.x), floor (x.y));
518 vec3 floor (vec3 x) {
519 return vec3 (floor (x.x), floor (x.y), floor (x.z));
521 vec4 floor (vec4 x) {
522 return vec4 (floor (x.x), floor (x.y), floor (x.z), floor (x.w));
526 // Returns a value equal to the nearest integer that is greater than or equal to x
529 float ceil (float x) {
533 return vec2 (ceil (x.x), ceil (x.y));
536 return vec3 (ceil (x.x), ceil (x.y), ceil (x.z));
539 return vec4 (ceil (x.x), ceil (x.y), ceil (x.z), ceil (x.w));
543 // Returns x
\96 floor (x)
546 float fract (float x) {
547 return x - floor (x);
549 vec2 fract (vec2 x) {
550 return vec2 (fract (x.x), fract (x.y));
552 vec3 fract (vec3 x) {
553 return vec3 (fract (x.x), fract (x.y), fract (x.z));
555 vec4 fract (vec4 x) {
556 return vec4 (fract (x.x), fract (x.y), fract (x.z), fract (x.w));
560 // Modulus. Returns x
\96 y * floor (x/y)
563 float mod (float x, float y) {
564 return x - y * floor (x / y);
566 vec2 mod (vec2 x, float y) {
567 return vec2 (mod (x.x, y), mod (x.y, y));
569 vec3 mod (vec3 x, float y) {
570 return vec3 (mod (x.x, y), mod (x.y, y), mod (x.z, y));
572 vec4 mod (vec4 x, float y) {
573 return vec4 (mod (x.x, y), mod (x.y, y), mod (x.z, y), mod (x.w, y));
575 vec2 mod (vec2 x, vec2 y) {
576 return vec2 (mod (x.x, y.x), mod (x.y, y.y));
578 vec3 mod (vec3 x, vec3 y) {
579 return vec3 (mod (x.x, y.x), mod (x.y, y.y), mod (x.z, y.z));
581 vec4 mod (vec4 x, vec4 y) {
582 return vec4 (mod (x.x, y.x), mod (x.y, y.y), mod (x.z, y.z), mod (x.w, y.w));
586 // Returns y if y < x, otherwise it returns x
589 float min (float x, float y) {
590 return y < x ? y : x;
592 vec2 min (vec2 x, float y) {
593 return vec2 (min (x.x, y), min (x.y, y));
595 vec3 min (vec3 x, float y) {
596 return vec3 (min (x.x, y), min (x.y, y), min (x.z, y));
598 vec4 min (vec4 x, float y) {
599 return vec4 (min (x.x, y), min (x.y, y), min (x.z, y), min (x.w, y));
601 vec2 min (vec2 x, vec2 y) {
602 return vec2 (min (x.x, y.x), min (x.y, y.y));
604 vec3 min (vec3 x, vec3 y) {
605 return vec3 (min (x.x, y.x), min (x.y, y.y), min (x.z, y.z));
607 vec4 min (vec4 x, vec4 y) {
608 return vec4 (min (x.x, y.x), min (x.y, y.y), min (x.z, y.z), min (x.w, y.w));
612 // Returns y if x < y, otherwise it returns x
615 float max (float x, float y) {
618 vec2 max (vec2 x, float y) {
619 return vec2 (max (x.x, y), max (x.y, y));
621 vec3 max (vec3 x, float y) {
622 return vec3 (max (x.x, y), max (x.y, y), max (x.z, y));
624 vec4 max (vec4 x, float y) {
625 return vec4 (max (x.x, y), max (x.y, y), max (x.z, y), max (x.w, y));
627 vec2 max (vec2 x, vec2 y) {
628 return vec2 (max (x.x, y.x), max (x.y, y.y));
630 vec3 max (vec3 x, vec3 y) {
631 return vec3 (max (x.x, y.x), max (x.y, y.y), max (x.z, y.z));
633 vec4 max (vec4 x, vec4 y) {
634 return vec4 (max (x.x, y.x), max (x.y, y.y), max (x.z, y.z), max (x.w, y.w));
638 // Returns min (max (x, minVal), maxVal)
640 // Note that colors and depths written by fragment shaders will be clamped by the implementation
641 // after the fragment shader runs.
644 float clamp (float x, float minVal, float maxVal) {
645 return min (max (x, minVal), maxVal);
647 vec2 clamp (vec2 x, float minVal, float maxVal) {
648 return vec2 (clamp (x.x, minVal, maxVal), clamp (x.y, minVal, maxVal));
650 vec3 clamp (vec3 x, float minVal, float maxVal) {
651 return vec3 (clamp (x.x, minVal, maxVal), clamp (x.y, minVal, maxVal),
652 clamp (x.z, minVal, maxVal));
654 vec4 clamp (vec4 x, float minVal, float maxVal) {
655 return vec4 (clamp (x.x, minVal, maxVal), clamp (x.y, minVal, maxVal),
656 clamp (x.z, minVal, maxVal), clamp (x.w, minVal, maxVal));
658 vec2 clamp (vec2 x, vec2 minVal, vec2 maxVal) {
659 return vec2 (clamp (x.x, minVal.x, maxVal.x), clamp (x.y, minVal.y, maxVal.y));
661 vec3 clamp (vec3 x, vec3 minVal, vec3 maxVal) {
662 return vec3 (clamp (x.x, minVal.x, maxVal.x), clamp (x.y, minVal.y, maxVal.y),
663 clamp (x.z, minVal.z, maxVal.z));
665 vec4 clamp (vec4 x, vec4 minVal, vec4 maxVal) {
666 return vec4 (clamp (x.x, minVal.x, maxVal.y), clamp (x.y, minVal.y, maxVal.y),
667 clamp (x.z, minVal.z, maxVal.z), clamp (x.w, minVal.w, maxVal.w));
671 // Returns x * (1
\96 a) + y * a, i.e., the linear blend of x and y
674 float mix (float x, float y, float a) {
675 return x * (1.0 - a) + y * a;
677 vec2 mix (vec2 x, vec2 y, float a) {
678 return vec2 (mix (x.x, y.x, a), mix (x.y, y.y, a));
680 vec3 mix (vec3 x, vec3 y, float a) {
681 return vec3 (mix (x.x, y.x, a), mix (x.y, y.y, a), mix (x.z, y.z, a));
683 vec4 mix (vec4 x, vec4 y, float a) {
684 return vec4 (mix (x.x, y.x, a), mix (x.y, y.y, a), mix (x.z, y.z, a), mix (x.w, y.w, a));
686 vec2 mix (vec2 x, vec2 y, vec2 a) {
687 return vec2 (mix (x.x, y.x, a.x), mix (x.y, y.y, a.y));
689 vec3 mix (vec3 x, vec3 y, vec3 a) {
690 return vec3 (mix (x.x, y.x, a.x), mix (x.y, y.y, a.y), mix (x.z, y.z, a.z));
692 vec4 mix (vec4 x, vec4 y, vec4 a) {
693 return vec4 (mix (x.x, y.x, a.x), mix (x.y, y.y, a.y), mix (x.z, y.z, a.z),
694 mix (x.w, y.w, a.w));
698 // Returns 0.0 if x <= edge, otherwise it returns 1.0
701 float step (float edge, float x) {
702 return x <= edge ? 0.0 : 1.0;
704 vec2 step (float edge, vec2 x) {
705 return vec2 (step (edge, x.x), step (edge, x.y));
707 vec3 step (float edge, vec3 x) {
708 return vec3 (step (edge, x.x), step (edge, x.y), step (edge, x.z));
710 vec4 step (float edge, vec4 x) {
711 return vec4 (step (edge, x.x), step (edge, x.y), step (edge, x.z), step (edge, x.w));
713 vec2 step (vec2 edge, vec2 x) {
714 return vec2 (step (edge.x, x.x), step (edge.y, x.y));
716 vec3 step (vec3 edge, vec3 x) {
717 return vec3 (step (edge.x, x.x), step (edge.y, x.y), step (edge.z, x.z));
719 vec4 step (vec4 edge, vec4 x) {
720 return vec4 (step (edge.x, x.x), step (edge.y, x.y), step (edge.z, x.z), step (edge.w, x.w));
724 // Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and performs smooth Hermite interpolation
725 // between 0 and 1 when edge0 < x < edge1. This is useful in cases where you would want a threshold
726 // function with a smooth transition. This is equivalent to:
728 // t = clamp ((x
\96 edge0) / (edge1
\96 edge0), 0, 1);
729 // return t * t * (3
\96 2 * t);
732 float smoothstep (float edge0, float edge1, float x) {
733 const float t = clamp ((x - edge0) / (edge1 - edge0), 0.0, 1.0);
734 return t * t * (3.0 - 2.0 * t);
736 vec2 smoothstep (float edge0, float edge1, vec2 x) {
737 return vec2 (smoothstep (edge0, edge1, x.x), smoothstep (edge0, edge1, x.y));
739 vec3 smoothstep (float edge0, float edge1, vec3 x) {
740 return vec3 (smoothstep (edge0, edge1, x.x), smoothstep (edge0, edge1, x.y),
741 smoothstep (edge0, edge1, x.z));
743 vec4 smoothstep (float edge0, float edge1, vec4 x) {
744 return vec4 (smoothstep (edge0, edge1, x.x), smoothstep (edge0, edge1, x.y),
745 smoothstep (edge0, edge1, x.z), smoothstep (edge0, edge1, x.w));
747 vec2 smoothstep (vec2 edge0, vec2 edge1, vec2 x) {
748 return vec2 (smoothstep (edge0.x, edge1.x, x.x), smoothstep (edge0.y, edge1.y, x.y));
750 vec3 smoothstep (vec3 edge0, vec3 edge1, vec3 x) {
751 return vec3 (smoothstep (edge0.x, edge1.x, x.x), smoothstep (edge0.y, edge1.y, x.y),
752 smoothstep (edge0.z, edge1.z, x.z));
754 vec4 smoothstep (vec4 edge0, vec4 edge1, vec4 x) {
755 return vec4 (smoothstep (edge0.x, edge1.x, x.x), smoothstep (edge0.y, edge1.y, x.y),
756 smoothstep (edge0.z, edge1.z, x.z), smoothstep (edge0.w, edge1.w, x.w));
760 // Geometric Functions
762 // These operate on vectors as vectors, not component-wise.
766 // Returns the dot product of x and y, i.e., result = x[0] * y[0] + x[1] * y[1] + ...
769 float dot (float x, float y) {
772 float dot (vec2 x, vec2 y) {
773 return dot (x.x, y.x) + dot (x.y, y.y);
775 float dot (vec3 x, vec3 y) {
776 return dot (x.x, y.x) + dot (x.y, y.y) + dot (x.z, y.z);
778 float dot (vec4 x, vec4 y) {
779 return dot (x.x, y.x) + dot (x.y, y.y) + dot (x.z, y.z) + dot (x.w, y.w);
783 // Returns the length of vector x, i.e., sqrt (x[0] * x[0] + x[1] * x[1] + ...)
786 float length (float x) {
787 return sqrt (dot (x, x));
789 float length (vec2 x) {
790 return sqrt (dot (x, x));
792 float length (vec3 x) {
793 return sqrt (dot (x, x));
795 float length (vec4 x) {
796 return sqrt (dot (x, x));
800 // Returns the distance between p0 and p1, i.e. length (p0
\96 p1)
803 float distance (float x, float y) {
804 return length (x - y);
806 float distance (vec2 x, vec2 y) {
807 return length (x - y);
809 float distance (vec3 x, vec3 y) {
810 return length (x - y);
812 float distance (vec4 x, vec4 y) {
813 return length (x - y);
817 // Returns the cross product of x and y, i.e.
818 // result.0 = x[1] * y[2] - y[1] * x[2]
819 // result.1 = x[2] * y[0] - y[2] * x[0]
820 // result.2 = x[0] * y[1] - y[0] * x[1]
823 vec3 cross (vec3 x, vec3 y) {
824 return vec3 (x.y * y.z - y.y * x.z, x.z * y.x - y.z * x.x, x.x * y.y - y.x * x.y);
828 // Returns a vector in the same direction as x but with a length of 1.
831 float normalize (float x) {
834 vec2 normalize (vec2 x) {
835 return x / length (x);
837 vec3 normalize (vec3 x) {
838 return x / length (x);
840 vec4 normalize (vec4 x) {
841 return x / length (x);
845 // If dot (Nref, I) < 0 return N otherwise return
\96N
848 float faceforward (float N, float I, float Nref) {
849 return dot (Nref, I) < 0.0 ? N : -N;
851 vec2 faceforward (vec2 N, vec2 I, vec2 Nref) {
852 return dot (Nref, I) < 0.0 ? N : -N;
854 vec3 faceforward (vec3 N, vec3 I, vec3 Nref) {
855 return dot (Nref, I) < 0.0 ? N : -N;
857 vec4 faceforward (vec4 N, vec4 I, vec4 Nref) {
858 return dot (Nref, I) < 0.0 ? N : -N;
862 // For the incident vector I and surface orientation N, returns the reflection direction:
863 // result = I
\96 2 * dot (N, I) * N
864 // N should be normalized in order to achieve the desired result.
866 float reflect (float I, float N) {
867 return I - 2.0 * dot (N, I) * N;
869 vec2 reflect (vec2 I, vec2 N) {
870 return I - 2.0 * dot (N, I) * N;
872 vec3 reflect (vec3 I, vec3 N) {
873 return I - 2.0 * dot (N, I) * N;
875 vec4 reflect (vec4 I, vec4 N) {
876 return I - 2.0 * dot (N, I) * N;
884 // Multiply matrix x by matrix y component-wise, i.e., result[i][j] is the scalar product
885 // of x[i][j] and y[i][j].
886 // Note: to get linear algebraic matrix multiplication, use the multiply operator (*).
889 mat2 matrixCompMult (mat2 x, mat2 y) {
891 x[0].x * y[0].x, x[0].y * y[0].y,
892 x[1].x * y[1].x, x[1].y * y[1].y
895 mat3 matrixCompMult (mat3 x, mat3 y) {
897 x[0].x * y[0].x, x[0].y * y[0].y, x[0].z * y[0].z,
898 x[1].x * y[1].x, x[1].y * y[1].y, x[1].z * y[1].z,
899 x[2].x * y[2].x, x[2].y * y[2].y, x[2].z * y[2].z
902 mat4 matrixCompMult (mat4 x, mat4 y) {
904 x[0].x * y[0].x, x[0].y * y[0].y, x[0].z * y[0].z + x[0].w * y[0].w,
905 x[1].x * y[1].x, x[1].y * y[1].y, x[1].z * y[1].z + x[1].w * y[1].w,
906 x[2].x * y[2].x, x[2].y * y[2].y, x[2].z * y[2].z + x[2].w * y[2].w,
907 x[3].x * y[3].x, x[3].y * y[3].y, x[3].z * y[3].z + x[3].w * y[3].w
912 // Vector Relational Functions
914 // Relational and equality operators (<, <=, >, >=, ==, !=) are defined (or reserved) to produce
915 // scalar Boolean results.
919 // Returns the component-wise compare of x < y.
922 bvec2 lessThan (vec2 x, vec2 y) {
923 return bvec2 (x.x < y.x, x.y < y.y);
925 bvec3 lessThan (vec3 x, vec3 y) {
926 return bvec3 (x.x < y.x, x.y < y.y, x.z < y.z);
928 bvec4 lessThan (vec4 x, vec4 y) {
929 return bvec4 (x.x < y.x, x.y < y.y, x.z < y.z, x.w < y.w);
931 bvec2 lessThan (ivec2 x, ivec2 y) {
932 return bvec2 (x.x < y.x, x.y < y.y);
934 bvec3 lessThan (ivec3 x, ivec3 y) {
935 return bvec3 (x.x < y.x, x.y < y.y, x.z < y.z);
937 bvec4 lessThan (ivec4 x, ivec4 y) {
938 return bvec4 (x.x < y.x, x.y < y.y, x.z < y.z, x.w < y.w);
942 // Returns the component-wise compare of x <= y.
945 bvec2 lessThanEqual (vec2 x, vec2 y) {
946 return bvec2 (x.x <= y.x, x.y <= y.y);
948 bvec3 lessThanEqual (vec3 x, vec3 y) {
949 return bvec3 (x.x <= y.x, x.y <= y.y, x.z <= y.z);
951 bvec4 lessThanEqual (vec4 x, vec4 y) {
952 return bvec4 (x.x <= y.x, x.y <= y.y, x.z <= y.z, x.w <= y.w);
954 bvec2 lessThanEqual (ivec2 x, ivec2 y) {
955 return bvec2 (x.x <= y.x, x.y <= y.y);
957 bvec3 lessThanEqual (ivec3 x, ivec3 y) {
958 return bvec3 (x.x <= y.x, x.y <= y.y, x.z <= y.z);
960 bvec4 lessThanEqual (ivec4 x, ivec4 y) {
961 return bvec4 (x.x <= y.x, x.y <= y.y, x.z <= y.z, x.w <= y.w);
965 // Returns the component-wise compare of x > y.
968 bvec2 greaterThan (vec2 x, vec2 y) {
969 return bvec2 (x.x > y.x, x.y > y.y);
971 bvec3 greaterThan (vec3 x, vec3 y) {
972 return bvec3 (x.x > y.x, x.y > y.y, x.z > y.z);
974 bvec4 greaterThan (vec4 x, vec4 y) {
975 return bvec4 (x.x > y.x, x.y > y.y, x.z > y.z, x.w > y.w);
977 bvec2 greaterThan (ivec2 x, ivec2 y) {
978 return bvec2 (x.x > y.x, x.y > y.y);
980 bvec3 greaterThan (ivec3 x, ivec3 y) {
981 return bvec3 (x.x > y.x, x.y > y.y, x.z > y.z);
983 bvec4 greaterThan (ivec4 x, ivec4 y) {
984 return bvec4 (x.x > y.x, x.y > y.y, x.z > y.z, x.w > y.w);
988 // Returns the component-wise compare of x >= y.
991 bvec2 greaterThanEqual (vec2 x, vec2 y) {
992 return bvec2 (x.x >= y.x, x.y >= y.y);
994 bvec3 greaterThanEqual (vec3 x, vec3 y) {
995 return bvec3 (x.x >= y.x, x.y >= y.y, x.z >= y.z);
997 bvec4 greaterThanEqual (vec4 x, vec4 y) {
998 return bvec4 (x.x >= y.x, x.y >= y.y, x.z >= y.z, x.w >= y.w);
1000 bvec2 greaterThanEqual (ivec2 x, ivec2 y) {
1001 return bvec2 (x.x >= y.x, x.y >= y.y);
1003 bvec3 greaterThanEqual (ivec3 x, ivec3 y) {
1004 return bvec3 (x.x >= y.x, x.y >= y.y, x.z >= y.z);
1006 bvec4 greaterThanEqual (ivec4 x, ivec4 y) {
1007 return bvec4 (x.x >= y.x, x.y >= y.y, x.z >= y.z, x.w >= y.w);
1011 // Returns the component-wise compare of x == y.
1014 bvec2 equal (vec2 x, vec2 y) {
1015 return bvec2 (x.x == y.x, x.y == y.y);
1017 bvec3 equal (vec3 x, vec3 y) {
1018 return bvec3 (x.x == y.x, x.y == y.y, x.z == y.z);
1020 bvec4 equal (vec4 x, vec4 y) {
1021 return bvec4 (x.x == y.x, x.y == y.y, x.z == y.z, x.w == y.w);
1023 bvec2 equal (ivec2 x, ivec2 y) {
1024 return bvec2 (x.x == y.x, x.y == y.y);
1026 bvec3 equal (ivec3 x, ivec3 y) {
1027 return bvec3 (x.x == y.x, x.y == y.y, x.z == y.z);
1029 bvec4 equal (ivec4 x, ivec4 y) {
1030 return bvec4 (x.x == y.x, x.y == y.y, x.z == y.z, x.w == y.w);
1034 // Returns the component-wise compare of x != y.
1037 bvec2 notEqual (vec2 x, vec2 y) {
1038 return bvec2 (x.x != y.x, x.y != y.y);
1040 bvec3 notEqual (vec3 x, vec3 y) {
1041 return bvec3 (x.x != y.x, x.y != y.y, x.z != y.z);
1043 bvec4 notEqual (vec4 x, vec4 y) {
1044 return (bvec4 (x.x != y.x, x.y != y.y, x.z != y.z, x.w != y.w);
1046 bvec2 notEqual (ivec2 x, ivec2 y) {
1047 return (bvec2 (x.x != y.x, x.y != y.y);
1049 bvec3 notEqual (ivec3 x, ivec3 y) {
1050 return (bvec3 (x.x != y.x, x.y != y.y, x.z != y.z);
1052 bvec4 notEqual (ivec4 x, ivec4 y) {
1053 return (bvec4 (x.x != y.x, x.y != y.y, x.z != y.z, x.w != y.w);
1057 // Returns true if any component of x is true.
1060 bool any (bvec2 x) {
1063 bool any (bvec3 x) {
1064 return x.x || x.y || x.z;
1066 bool any (bvec4 x) {
1067 return x.x || x.y || x.z || x.w;
1071 // Returns true only if all components of x are true.
1074 bool all (bvec2 x) {
1077 bool all (bvec3 x) {
1078 return x.x && x.y && x.z;
1080 bool all (bvec4 x) {
1081 return x.x && x.y && x.z && x.w;
1085 // Returns the component-wise logical complement of x.
1088 bvec2 not (bvec2 x) {
1089 return bvec2 (!x.x, !x.y);
1091 bvec3 not (bvec3 x) {
1092 return bvec3 (!x.x, !x.y, !x.z);
1094 bvec4 not (bvec4 x) {
1095 return bvec4 (!x.x, !x.y, !x.z, !x.w);
1099 // Texture Lookup Functions
1101 // Texture lookup functions are available to both vertex and fragment shaders. However, level
1102 // of detail is not computed by fixed functionality for vertex shaders, so there are some
1103 // differences in operation between vertex and fragment texture lookups. The functions in the table
1104 // below provide access to textures through samplers, as set up through the OpenGL API. Texture
1105 // properties such as size, pixel format, number of dimensions, filtering method, number of mip-map
1106 // levels, depth comparison, and so on are also defined by OpenGL API calls. Such properties are
1107 // taken into account as the texture is accessed via the built-in functions defined below.
1109 // If a non-shadow texture call is made to a sampler whose texture has depth comparisons enabled,
1110 // then results are undefined. If a shadow texture call is made to a sampler whose texture does not
1111 // have depth comparisions enabled, the results are also undefined.
1113 // In all functions below, the bias parameter is optional for fragment shaders. The bias parameter
1114 // is not accepted in a vertex shader. For a fragment shader, if bias is present, it is added to
1115 // the calculated level of detail prior to performing the texture access operation. If the bias
1116 // parameter is not provided, then the implementation automatically selects level of detail:
1117 // For a texture that is not mip-mapped, the texture is used directly. If it is mip-mapped and
1118 // running in a fragment shader, the LOD computed by the implementation is used to do the texture
1119 // lookup. If it is mip-mapped and running on the vertex shader, then the base texture is used.
1121 // The built-ins suffixed with
\93Lod
\94 are allowed only in a vertex shader. For the
\93Lod
\94 functions,
1122 // lod is directly used as the level of detail.
1126 // Use the texture coordinate coord to do a texture lookup in the 1D texture currently bound
1127 // to sampler. For the projective (
\93Proj
\94) versions, the texture coordinate coord.s is divided by
1128 // the last component of coord.
1131 vec4 texture1D (sampler1D sampler, float coord) {
1134 vec4 texture1DProj (sampler1D sampler, vec2 coord) {
1135 return texture1D (sampler, coord.s / coord.t);
1137 vec4 texture1DProj (sampler1D sampler, vec4 coord) {
1138 return texture1D (sampler, coord.s / coord.q);
1142 // Use the texture coordinate coord to do a texture lookup in the 2D texture currently bound
1143 // to sampler. For the projective (
\93Proj
\94) versions, the texture coordinate (coord.s, coord.t) is
1144 // divided by the last component of coord. The third component of coord is ignored for the vec4
1148 vec4 texture2D (sampler2D sampler, vec2 coord) {
1151 vec4 texture2DProj (sampler2D sampler, vec3 coord) {
1152 return texture2D (sampler, vec2 (coord.s / coord.p, coord.t / coord.p));
1154 vec4 texture2DProj (sampler2D sampler, vec4 coord) {
1155 return texture2D (sampler, vec2 (coord.s / coord.q, coord.t / coord.q));
1159 // Use the texture coordinate coord to do a texture lookup in the 3D texture currently bound
1160 // to sampler. For the projective (
\93Proj
\94) versions, the texture coordinate is divided by coord.q.
1163 vec4 texture3D (sampler3D sampler, vec3 coord) {
1166 vec4 texture3DProj (sampler3D sampler, vec4 coord) {
1167 return texture3D (sampler, vec3 (coord.s / coord.q, coord.t / coord.q, coord.p / coord.q));
1171 // Use the texture coordinate coord to do a texture lookup in the cube map texture currently bound
1172 // to sampler. The direction of coord is used to select which face to do a 2-dimensional texture
1173 // lookup in, as described in section 3.8.6 in version 1.4 of the OpenGL specification.
1176 vec4 textureCube (samplerCube sampler, vec3 coord) {
1181 // Use texture coordinate coord to do a depth comparison lookup on the depth texture bound
1182 // to sampler, as described in section 3.8.14 of version 1.4 of the OpenGL specification. The 3rd
1183 // component of coord (coord.p) is used as the R value. The texture bound to sampler must be a
1184 // depth texture, or results are undefined. For the projective (
\93Proj
\94) version of each built-in,
1185 // the texture coordinate is divide by coord.q, giving a depth value R of coord.p/coord.q. The
1186 // second component of coord is ignored for the
\931D
\94 variants.
1189 vec4 shadow1D (sampler1DShadow sampler, vec3 coord) {
1193 vec4 shadow2D (sampler2DShadow sampler, vec3 coord) {
1196 vec4 shadow1DProj (sampler1DShadow sampler, vec4 coord) {
1197 return shadow1D (sampler, vec3 (coord.s / coord.q, 0.0, coord.p / coord.q));
1199 vec4 shadow2DProj (sampler2DShadow sampler, vec4 coord) {
1200 return shadow2D (sampler, vec3 (coord.s / coord.q, coord.t / coord.q, coord.p / coord.q));
1206 // Noise functions are available to both fragment and vertex shaders. They are stochastic functions
1207 // that can be used to increase visual complexity. Values returned by the following noise functions
1208 // give the appearance of randomness, but are not truly random. The noise functions below are
1209 // defined to have the following characteristics:
1211 //
\95 The return value(s) are always in the range [-1,1]
1212 //
\95 The return value(s) have an overall average of 0.0
1213 //
\95 They are repeatable, in that a particular input value will always produce the same return value
1214 //
\95 They are statistically invariant under rotation (i.e., no matter how the domain is rotated, it
1215 // has the same statistical character)
1216 //
\95 They have a statistical invariance under translation (i.e., no matter how the domain is
1217 // translated, it has the same statistical character)
1218 //
\95 They typically give different results under translation.
1219 //
\95 They have a narrow bandpass limit in frequency (i.e., it has no visible features larger or
1220 // smaller than a certain narrow size range)
1224 // Returns a 1D noise value based on the input value x.
1227 float noise1 (float x) {
1231 float noise1 (vec2 x) {
1235 float noise1 (vec3 x) {
1239 float noise1 (vec4 x) {
1244 // Returns a 2D noise value based on the input value x.
1247 vec2 noise2 (float x) {
1251 vec2 noise2 (vec2 x) {
1255 vec2 noise2 (vec3 x) {
1259 vec2 noise2 (vec4 x) {
1264 // Returns a 3D noise value based on the input value x.
1267 vec3 noise3 (float x) {
1271 vec3 noise3 (vec2 x) {
1275 vec3 noise3 (vec3 x) {
1279 vec3 noise3 (vec4 x) {
1284 // Returns a 4D noise value based on the input value x.
1287 vec4 noise4 (float x) {
1291 vec4 noise4 (vec2 x) {
1295 vec4 noise4 (vec3 x) {
1299 vec4 noise4 (vec4 x) {