mesa: move nvprogram.[ch] to main/
[mesa.git] / src / mesa / shader / slang / library / slang_common_builtin.gc
index 9f2a4bdd0754c86ebf8006a46a665e4604409ad6..d75354deffe71909da2bc2482dfd535d5fff2043 100644 (file)
@@ -1,8 +1,9 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.5
+ * Version:  7.3
  *
  * Copyright (C) 2006  Brian Paul   All Rights Reserved.
+ * Copyright (C) 2008  VMware, Inc.  All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -26,8 +27,9 @@
 // From Shader Spec, ver. 1.10, rev. 59
 //
 
-//bp: XXX these will probably go away since the value needs to be
-//determined at runtime and may vary from one GLcontext to another...
+// Note: the values assigned to these constants here aren't actually used.
+// They're set by the compiler according to the GL context limits.
+// See slang_simplify.c
 const int gl_MaxLights = 8;
 const int gl_MaxClipPlanes = 6;
 const int gl_MaxTextureUnits = 8;
@@ -47,7 +49,6 @@ uniform mat4 gl_ModelViewProjectionMatrix;
 uniform mat4 gl_TextureMatrix[gl_MaxTextureCoords];
 
 uniform mat3 gl_NormalMatrix;
-uniform mat3 __NormalMatrixTranspose;  // Mesa only
 
 uniform mat4 gl_ModelViewMatrixInverse;
 uniform mat4 gl_ProjectionMatrixInverse;
@@ -99,6 +100,9 @@ struct gl_MaterialParameters {
 uniform gl_MaterialParameters gl_FrontMaterial;
 uniform gl_MaterialParameters gl_BackMaterial;
 
+/* NOTE: the order of these fields is significant!
+ * See the definition of the lighting state vars such as STATE_SPOT_DIRECTION.
+ */
 struct gl_LightSourceParameters {
     vec4 ambient;
     vec4 diffuse;
@@ -106,12 +110,14 @@ struct gl_LightSourceParameters {
     vec4 position;
     vec4 halfVector;
     vec3 spotDirection;
-    float spotExponent;
-    float spotCutoff;
     float spotCosCutoff;
+
     float constantAttenuation;
     float linearAttenuation;
     float quadraticAttenuation;
+    float spotExponent;
+
+    float spotCutoff;
 };
 
 uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];
@@ -473,32 +479,33 @@ vec4 pow(const vec4 a, const vec4 b)
 
 float exp(const float a)
 {
-   const float e = 2.71828;
-   __asm float_power __retVal, e, a;
+   // NOTE: log2(e) = 1.44269502
+   float t = a * 1.44269502;
+   __asm float_exp2 __retVal, t;
 }
 
 vec2 exp(const vec2 a)
 {
-   const float e = 2.71828;
-   __asm float_power __retVal.x, e, a.x;
-   __asm float_power __retVal.y, e, a.y;
+   vec2 t = a * 1.44269502;
+   __asm float_exp2 __retVal.x, t.x;
+   __asm float_exp2 __retVal.y, t.y;
 }
 
 vec3 exp(const vec3 a)
 {
-   const float e = 2.71828;
-   __asm float_power __retVal.x, e, a.x;
-   __asm float_power __retVal.y, e, a.y;
-   __asm float_power __retVal.z, e, a.z;
+   vec3 t = a * 1.44269502;
+   __asm float_exp2 __retVal.x, t.x;
+   __asm float_exp2 __retVal.y, t.y;
+   __asm float_exp2 __retVal.z, t.z;
 }
 
 vec4 exp(const vec4 a)
 {
-   const float e = 2.71828;
-   __asm float_power __retVal.x, e, a.x;
-   __asm float_power __retVal.y, e, a.y;
-   __asm float_power __retVal.z, e, a.z;
-   __asm float_power __retVal.w, e, a.w;
+   vec4 t = a * 1.44269502;
+   __asm float_exp2 __retVal.x, t.x;
+   __asm float_exp2 __retVal.y, t.y;
+   __asm float_exp2 __retVal.z, t.z;
+   __asm float_exp2 __retVal.w, t.w;
 }
 
 
@@ -595,42 +602,44 @@ vec4 exp2(const vec4 a)
 
 float sqrt(const float x)
 {
+   const float nx = -x;
    float r;
    __asm float_rsq r, x;
-   __asm float_rcp __retVal, r;
+   r = r * x;
+   __asm vec4_cmp __retVal, nx, r, 0.0;
 }
 
-vec2 sqrt(const vec2 v)
+vec2 sqrt(const vec2 x)
 {
-   float r;
-   __asm float_rsq r, v.x;
-   __asm float_rcp __retVal.x, r;
-   __asm float_rsq r, v.y;
-   __asm float_rcp __retVal.y, r;
+   const vec2 nx = -x, zero = vec2(0.0);
+   vec2 r;
+   __asm float_rsq r.x, x.x;
+   __asm float_rsq r.y, x.y;
+   r = r * x;
+   __asm vec4_cmp __retVal, nx, r, zero;
 }
 
-vec3 sqrt(const vec3 v)
+vec3 sqrt(const vec3 x)
 {
-   float r;
-   __asm float_rsq r, v.x;
-   __asm float_rcp __retVal.x, r;
-   __asm float_rsq r, v.y;
-   __asm float_rcp __retVal.y, r;
-   __asm float_rsq r, v.z;
-   __asm float_rcp __retVal.z, r;
+   const vec3 nx = -x, zero = vec3(0.0);
+   vec3 r;
+   __asm float_rsq r.x, x.x;
+   __asm float_rsq r.y, x.y;
+   __asm float_rsq r.z, x.z;
+   r = r * x;
+   __asm vec4_cmp __retVal, nx, r, zero;
 }
 
-vec4 sqrt(const vec4 v)
+vec4 sqrt(const vec4 x)
 {
-   float r;
-   __asm float_rsq r, v.x;
-   __asm float_rcp __retVal.x, r;
-   __asm float_rsq r, v.y;
-   __asm float_rcp __retVal.y, r;
-   __asm float_rsq r, v.z;
-   __asm float_rcp __retVal.z, r;
-   __asm float_rsq r, v.w;
-   __asm float_rcp __retVal.w, r;
+   const vec4 nx = -x, zero = vec4(0.0);
+   vec4 r;
+   __asm float_rsq r.x, x.x;
+   __asm float_rsq r.y, x.y;
+   __asm float_rsq r.z, x.z;
+   __asm float_rsq r.w, x.w;
+   r = r * x;
+   __asm vec4_cmp __retVal, nx, r, zero;
 }
 
 
@@ -680,7 +689,7 @@ vec3 normalize(const vec3 v)
 {
 //   const float s = inversesqrt(dot(v, v));
 //   __retVal = v * s;
-// XXX note, we _could_ use __retVal.w instead of tmp and and save a
+// XXX note, we _could_ use __retVal.w instead of tmp and save a
 // register, but that's actually a compilation error because v is a vec3
 // and the .w suffix is illegal.  Oh well.
    float tmp;
@@ -872,35 +881,29 @@ vec4 mod(const vec4 a, const float b)
 
 vec2 mod(const vec2 a, const vec2 b)
 {
-    float oneOverBx, oneOverBy;
-    __asm float_rcp oneOverBx, b.x;
-    __asm float_rcp oneOverBy, b.y;
-    __retVal.x = a.x - b.x * floor(a.x * oneOverBx);
-    __retVal.y = a.y - b.y * floor(a.y * oneOverBy);
+    vec2 oneOverB;
+    __asm float_rcp oneOverB.x, b.x;
+    __asm float_rcp oneOverB.y, b.y;
+    __retVal = a - b * floor(a * oneOverB);
 }
 
 vec3 mod(const vec3 a, const vec3 b)
 {
-    float oneOverBx, oneOverBy, oneOverBz;
-    __asm float_rcp oneOverBx, b.x;
-    __asm float_rcp oneOverBy, b.y;
-    __asm float_rcp oneOverBz, b.z;
-    __retVal.x = a.x - b.x * floor(a.x * oneOverBx);
-    __retVal.y = a.y - b.y * floor(a.y * oneOverBy);
-    __retVal.z = a.z - b.z * floor(a.z * oneOverBz);
+    vec3 oneOverB;
+    __asm float_rcp oneOverB.x, b.x;
+    __asm float_rcp oneOverB.y, b.y;
+    __asm float_rcp oneOverB.z, b.z;
+    __retVal = a - b * floor(a * oneOverB);
 }
 
 vec4 mod(const vec4 a, const vec4 b)
 {
-    float oneOverBx, oneOverBy, oneOverBz, oneOverBw;
-    __asm float_rcp oneOverBx, b.x;
-    __asm float_rcp oneOverBy, b.y;
-    __asm float_rcp oneOverBz, b.z;
-    __asm float_rcp oneOverBw, b.w;
-    __retVal.x = a.x - b.x * floor(a.x * oneOverBx);
-    __retVal.y = a.y - b.y * floor(a.y * oneOverBy);
-    __retVal.z = a.z - b.z * floor(a.z * oneOverBz);
-    __retVal.w = a.w - b.w * floor(a.w * oneOverBw);
+    vec4 oneOverB;
+    __asm float_rcp oneOverB.x, b.x;
+    __asm float_rcp oneOverB.y, b.y;
+    __asm float_rcp oneOverB.z, b.z;
+    __asm float_rcp oneOverB.w, b.w;
+    __retVal = a - b * floor(a * oneOverB);
 }
 
 
@@ -1056,45 +1059,45 @@ vec4 mix(const vec4 x, const vec4 y, const vec4 a)
 }
 
 
-//// step (untested)
+//// step
 
 float step(const float edge, const float x)
 {
-   __asm vec4_sgt __retVal.x, x, edge;
+   __asm vec4_sge __retVal, x, edge;
 }
 
 vec2 step(const vec2 edge, const vec2 x)
 {
-   __asm vec4_sgt __retVal.xy, x, edge;
+   __asm vec4_sge __retVal.xy, x, edge;
 }
 
 vec3 step(const vec3 edge, const vec3 x)
 {
-   __asm vec4_sgt __retVal.xyz, x, edge;
+   __asm vec4_sge __retVal.xyz, x, edge;
 }
 
 vec4 step(const vec4 edge, const vec4 x)
 {
-   __asm vec4_sgt __retVal, x, edge;
+   __asm vec4_sge __retVal, x, edge;
 }
 
 vec2 step(const float edge, const vec2 v)
 {
-   __asm vec4_sgt __retVal.xy, v, edge.xx;
+   __asm vec4_sge __retVal.xy, v, edge;
 }
 
 vec3 step(const float edge, const vec3 v)
 {
-   __asm vec4_sgt __retVal.xyz, v, edge.xxx;
+   __asm vec4_sge __retVal.xyz, v, edge;
 }
 
 vec4 step(const float edge, const vec4 v)
 {
-   __asm vec4_sgt __retVal, v, edge.xxxx;
+   __asm vec4_sge __retVal, v, edge;
 }
 
 
-//// smoothstep (untested)
+//// smoothstep
 
 float smoothstep(const float edge0, const float edge1, const float x)
 {
@@ -1157,7 +1160,7 @@ float length(const vec2 v)
    float r;
    const float p = dot(v, v);      // p = v.x * v.x + v.y * v.y
    __asm float_rsq r, p;           // r = 1 / sqrt(p)
-   __asm float_rcp __retVal.x, r;  // retVal = 1 / r
+   __retVal = p * r;               // p * r = sqrt(p);
 }
 
 float length(const vec3 v)
@@ -1165,7 +1168,7 @@ float length(const vec3 v)
    float r;
    const float p = dot(v, v);      // p = v.x * v.x + v.y * v.y + v.z * v.z
    __asm float_rsq r, p;           // r = 1 / sqrt(p)
-   __asm float_rcp __retVal, r;    // retVal = 1 / r
+   __retVal = p * r;               // p * r = sqrt(p);
 }
 
 float length(const vec4 v)
@@ -1173,7 +1176,7 @@ float length(const vec4 v)
    float r;
    const float p = dot(v, v);      // p = v.x * v.x + v.y * v.y + ...
    __asm float_rsq r, p;           // r = 1 / sqrt(p)
-   __asm float_rcp __retVal, r;    // retVal = 1 / r
+   __retVal = p * r;               // p * r = sqrt(p);
 }
 
 
@@ -1277,34 +1280,50 @@ vec4 reflect(const vec4 I, const vec4 N)
 
 float refract(const float I, const float N, const float eta)
 {
-   float k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I));
+   float n_dot_i = dot(N, I);
+   float k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
+   float retval;
    if (k < 0.0)
-       return 0.0;
-   return eta * I - (eta * dot(N, I) + sqrt(k)) * N;
+      retval = 0.0;
+   else
+      retval = eta * I - (eta * n_dot_i + sqrt(k)) * N;
+   return retval;
 }
 
 vec2 refract(const vec2 I, const vec2 N, const float eta)
 {
-   float k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I));
+   float n_dot_i = dot(N, I);
+   float k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
+   vec2 retval;
    if (k < 0.0)
-      return 0.0;
-   return eta * I - (eta * dot(N, I) + sqrt(k)) * N;
+      retval = vec2(0.0);
+   else
+      retval = eta * I - (eta * n_dot_i + sqrt(k)) * N;
+   return retval;
 }
 
 vec3 refract(const vec3 I, const vec3 N, const float eta)
 {
-   float k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I));
+   float n_dot_i = dot(N, I);
+   float k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
+   vec3 retval;
    if (k < 0.0)
-      return 0.0;
-   return eta * I - (eta * dot(N, I) + sqrt(k)) * N;
+      retval = vec3(0.0);
+   else
+      retval = eta * I - (eta * n_dot_i + sqrt(k)) * N;
+   return retval;
 }
 
 vec4 refract(const vec4 I, const vec4 N, const float eta)
 {
-   float k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I));
+   float n_dot_i = dot(N, I);
+   float k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
+   vec4 retval;
    if (k < 0.0)
-      return 0.0;
-   return eta * I - (eta * dot(N, I) + sqrt(k)) * N;
+      retval = vec4(0.0);
+   else
+      retval = eta * I - (eta * n_dot_i + sqrt(k)) * N;
+   return retval;
 }
 
 
@@ -1642,76 +1661,76 @@ bvec4 not (const bvec4 v)
 
 vec4 texture1D(const sampler1D sampler, const float coord)
 {
-   __asm vec4_tex1d __retVal, sampler, coord;
+   __asm vec4_tex_1d __retVal, sampler, coord;
 }
 
 vec4 texture1DProj(const sampler1D sampler, const vec2 coord)
 {
    // need to swizzle .y into .w
-   __asm vec4_texp1d __retVal, sampler, coord.xyyy;
+   __asm vec4_tex_1d_proj __retVal, sampler, coord.xyyy;
 }
 
 vec4 texture1DProj(const sampler1D sampler, const vec4 coord)
 {
-   __asm vec4_texp1d __retVal, sampler, coord;
+   __asm vec4_tex_1d_proj __retVal, sampler, coord;
 }
 
 
 vec4 texture2D(const sampler2D sampler, const vec2 coord)
 {
-   __asm vec4_tex2d __retVal, sampler, coord;
+   __asm vec4_tex_2d __retVal, sampler, coord;
 }
 
 vec4 texture2DProj(const sampler2D sampler, const vec3 coord)
 {
    // need to swizzle 'z' into 'w'.
-   __asm vec4_texp2d __retVal, sampler, coord.xyzz;
+   __asm vec4_tex_2d_proj __retVal, sampler, coord.xyzz;
 }
 
 vec4 texture2DProj(const sampler2D sampler, const vec4 coord)
 {
-   __asm vec4_texp2d __retVal, sampler, coord;
+   __asm vec4_tex_2d_proj __retVal, sampler, coord;
 }
 
 
 vec4 texture3D(const sampler3D sampler, const vec3 coord)
 {
-   __asm vec4_tex3d __retVal, sampler, coord;
+   __asm vec4_tex_3d __retVal, sampler, coord;
 }
 
 vec4 texture3DProj(const sampler3D sampler, const vec4 coord)
 {
-   __asm vec4_texp3d __retVal, sampler, coord;
+   __asm vec4_tex_3d_proj __retVal, sampler, coord;
 }
 
 
 vec4 textureCube(const samplerCube sampler, const vec3 coord)
 {
-   __asm vec4_texcube __retVal, sampler, coord;
+   __asm vec4_tex_cube __retVal, sampler, coord;
 }
 
 
 
 vec4 shadow1D(const sampler1DShadow sampler, const vec3 coord)
 {
-   __asm vec4_tex1d __retVal, sampler, coord;
+   __asm vec4_tex_1d_shadow __retVal, sampler, coord;
 }
 
 vec4 shadow1DProj(const sampler1DShadow sampler, const vec4 coord)
 {
    // .s and .p will be divided by .q
-   __asm vec4_texp1d __retVal, sampler, coord;
+   __asm vec4_tex_1d_proj_shadow __retVal, sampler, coord;
 }
 
 vec4 shadow2D(const sampler2DShadow sampler, const vec3 coord)
 {
-   __asm vec4_tex2d __retVal, sampler, coord;
+   __asm vec4_tex_2d_shadow __retVal, sampler, coord;
 }
 
 vec4 shadow2DProj(const sampler2DShadow sampler, const vec4 coord)
 {
    // .s, .t and .p will be divided by .q
-   __asm vec4_texp2d __retVal, sampler, coord;
+   __asm vec4_tex_2d_proj_shadow __retVal, sampler, coord;
 }
 
 
@@ -1724,24 +1743,36 @@ vec4 texture2DRect(const sampler2DRect sampler, const vec2 coord)
 vec4 texture2DRectProj(const sampler2DRect sampler, const vec3 coord)
 {
    // need to swizzle .y into .w
-   __asm vec4_texp_rect __retVal, sampler, coord.xyzz;
+   __asm vec4_tex_rect_proj __retVal, sampler, coord.xyzz;
 }
 
 vec4 texture2DRectProj(const sampler2DRect sampler, const vec4 coord)
 {
-   __asm vec4_texp_rect __retVal, sampler, ccoord;
+   __asm vec4_tex_rect_proj __retVal, sampler, ccoord;
 }
 
 vec4 shadow2DRect(const sampler2DRectShadow sampler, const vec3 coord)
 {
-   __asm vec4_tex_rect __retVal, sampler, coord;
+   __asm vec4_tex_rect_shadow __retVal, sampler, coord;
 }
 
 vec4 shadow2DRectProj(const sampler2DRectShadow sampler, const vec4 coord)
 {
-   __asm vec4_texp_rect __retVal, sampler, coord;
+   __asm vec4_tex_rect_proj_shadow __retVal, sampler, coord;
+}
+
+
+
+//// GL_EXT_texture_array
+vec4 texture1DArray(const sampler1DArray sampler, const vec2 coord)
+{
+   __asm vec4_tex_1d_array __retVal, sampler, coord;
 }
 
+vec4 texture2DArray(const sampler2DArray sampler, const vec3 coord)
+{
+   __asm vec4_tex_2d_array __retVal, sampler, coord;
+}
 
 
 //