Be more consistant with paths in #includes. Eventually, eliminate a bunch of -I...
[mesa.git] / src / mesa / shader / slang / slang_builtin.c
index 688ef9c88ab4de83504c8cbe04c766d3a658d02a..1081d8ff8db744d06fc58069f4d2e262ad76b7e4 100644 (file)
  * \author Brian Paul
  */
 
-#include "imports.h"
-#include "macros.h"
-#include "slang_builtin.h"
-#include "slang_typeinfo.h"
-#include "slang_codegen.h"
-#include "slang_compile.h"
-#include "slang_ir.h"
-#include "mtypes.h"
-#include "program.h"
-#include "prog_instruction.h"
-#include "prog_parameter.h"
-#include "prog_statevars.h"
-#include "slang_print.h"
+#include "main/imports.h"
+#include "main/mtypes.h"
+#include "shader/program.h"
+#include "shader/prog_instruction.h"
+#include "shader/prog_parameter.h"
+#include "shader/prog_statevars.h"
+#include "shader/slang/slang_ir.h"
+#include "shader/slang/slang_builtin.h"
 
 
 /**
@@ -54,6 +49,42 @@ lookup_statevar(const char *var, GLint index1, GLint index2, const char *field,
                 GLuint *swizzleOut,
                 struct gl_program_parameter_list *paramList)
 {
+   /*
+    * NOTE: The ARB_vertex_program extension specified that matrices get
+    * loaded in registers in row-major order.  With GLSL, we want column-
+    * major order.  So, we need to transpose all matrices here...
+    */
+   static const struct {
+      const char *name;
+      gl_state_index matrix;
+      gl_state_index modifier;
+   } matrices[] = {
+      { "gl_ModelViewMatrix", STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE },
+      { "gl_ModelViewMatrixInverse", STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVTRANS },
+      { "gl_ModelViewMatrixTranspose", STATE_MODELVIEW_MATRIX, 0 },
+      { "gl_ModelViewMatrixInverseTranspose", STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVERSE },
+
+      { "gl_ProjectionMatrix", STATE_PROJECTION_MATRIX, STATE_MATRIX_TRANSPOSE },
+      { "gl_ProjectionMatrixInverse", STATE_PROJECTION_MATRIX, STATE_MATRIX_INVTRANS },
+      { "gl_ProjectionMatrixTranspose", STATE_PROJECTION_MATRIX, 0 },
+      { "gl_ProjectionMatrixInverseTranspose", STATE_PROJECTION_MATRIX, STATE_MATRIX_INVERSE },
+
+      { "gl_ModelViewProjectionMatrix", STATE_MVP_MATRIX, STATE_MATRIX_TRANSPOSE },
+      { "gl_ModelViewProjectionMatrixInverse", STATE_MVP_MATRIX, STATE_MATRIX_INVTRANS },
+      { "gl_ModelViewProjectionMatrixTranspose", STATE_MVP_MATRIX, 0 },
+      { "gl_ModelViewProjectionMatrixInverseTranspose", STATE_MVP_MATRIX, STATE_MATRIX_INVERSE },
+
+      { "gl_TextureMatrix", STATE_TEXTURE_MATRIX, STATE_MATRIX_TRANSPOSE },
+      { "gl_TextureMatrixInverse", STATE_TEXTURE_MATRIX, STATE_MATRIX_INVTRANS },
+      { "gl_TextureMatrixTranspose", STATE_TEXTURE_MATRIX, 0 },
+      { "gl_TextureMatrixInverseTranspose", STATE_TEXTURE_MATRIX, STATE_MATRIX_INVERSE },
+
+      /* XXX verify these!!! */
+      { "gl_NormalMatrix", STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE },
+      { "__NormalMatrixTranspose", STATE_MODELVIEW_MATRIX, 0 },
+
+      { NULL, 0, 0 }
+   };
    gl_state_index tokens[STATE_LENGTH];
    GLuint i;
    GLboolean isMatrix = GL_FALSE;
@@ -63,27 +94,24 @@ lookup_statevar(const char *var, GLint index1, GLint index2, const char *field,
    }
    *swizzleOut = SWIZZLE_NOOP;
 
-   if (strcmp(var, "gl_ModelViewMatrix") == 0) {
-      tokens[0] = STATE_MODELVIEW_MATRIX;
-      isMatrix = GL_TRUE;
-   }
-   else if (strcmp(var, "gl_ModelProjectionMatrix") == 0) {
-      tokens[0] = STATE_PROJECTION_MATRIX;
-      isMatrix = GL_TRUE;
-   }
-   else if (strcmp(var, "gl_ModelViewProjectionMatrix") == 0) {
-      tokens[0] = STATE_MVP_MATRIX;
-      isMatrix = GL_TRUE;
-   }
-   else if (strcmp(var, "gl_NormalMatrix") == 0) {
-      tokens[0] = STATE_MODELVIEW_MATRIX;
-      isMatrix = GL_TRUE;
+   /* first, look if var is a pre-defined matrix */
+   for (i = 0; matrices[i].name; i++) {
+      if (strcmp(var, matrices[i].name) == 0) {
+         tokens[0] = matrices[i].matrix;
+         /* tokens[1], [2] and [3] filled below */
+         tokens[4] = matrices[i].modifier;
+         isMatrix = GL_TRUE;
+         break;
+      }
    }
-   else if (strcmp(var, "gl_TextureMatrix") == 0) {
-      tokens[0] = STATE_TEXTURE_MATRIX;
-      if (index2 >= 0)
-         tokens[1] = index2;
-      isMatrix = GL_TRUE;
+
+   if (isMatrix) {
+      if (tokens[0] == STATE_TEXTURE_MATRIX) {
+         if (index1 >= 0) {
+            tokens[1] = index1;
+            index1 = 0; /* prevent extra addition at end of function */
+         }
+      }
    }
    else if (strcmp(var, "gl_DepthRange") == 0) {
       tokens[0] = STATE_DEPTH_RANGE;
@@ -104,6 +132,39 @@ lookup_statevar(const char *var, GLint index1, GLint index2, const char *field,
       tokens[0] = STATE_CLIPPLANE;
       tokens[1] = index1;
    }
+   else if (strcmp(var, "gl_Point") == 0) {
+      if (strcmp(field, "size") == 0) {
+         tokens[0] = STATE_POINT_SIZE;
+         *swizzleOut = SWIZZLE_XXXX;
+      }
+      else if (strcmp(field, "sizeMin") == 0) {
+         tokens[0] = STATE_POINT_SIZE;
+         *swizzleOut = SWIZZLE_YYYY;
+      }
+      else if (strcmp(field, "sizeMax") == 0) {
+         tokens[0] = STATE_POINT_SIZE;
+         *swizzleOut = SWIZZLE_ZZZZ;
+      }
+      else if (strcmp(field, "fadeThresholdSize") == 0) {
+         tokens[0] = STATE_POINT_SIZE;
+         *swizzleOut = SWIZZLE_WWWW;
+      }
+      else if (strcmp(field, "distanceConstantAttenuation") == 0) {
+         tokens[0] = STATE_POINT_ATTENUATION;
+         *swizzleOut = SWIZZLE_XXXX;
+      }
+      else if (strcmp(field, "distanceLinearAttenuation") == 0) {
+         tokens[0] = STATE_POINT_ATTENUATION;
+         *swizzleOut = SWIZZLE_YYYY;
+      }
+      else if (strcmp(field, "distanceQuadraticAttenuation") == 0) {
+         tokens[0] = STATE_POINT_ATTENUATION;
+         *swizzleOut = SWIZZLE_ZZZZ;
+      }
+      else {
+         return -1;
+      }
+   }
    else if (strcmp(var, "gl_FrontMaterial") == 0 ||
             strcmp(var, "gl_BackMaterial") == 0) {
       tokens[0] = STATE_MATERIAL;
@@ -219,7 +280,7 @@ lookup_statevar(const char *var, GLint index1, GLint index2, const char *field,
       if (strcmp(field, "ambient") == 0) {
          tokens[3] = STATE_AMBIENT;
       }
-      else if (strcmp(field, "diffuset") == 0) {
+      else if (strcmp(field, "diffuse") == 0) {
          tokens[3] = STATE_DIFFUSE;
       }
       else if (strcmp(field, "specular") == 0) {
@@ -274,24 +335,23 @@ lookup_statevar(const char *var, GLint index1, GLint index2, const char *field,
       tokens[2] = STATE_TEXGEN_OBJECT_Q;
    }
    else if (strcmp(var, "gl_Fog") == 0) {
-      tokens[0] = STATE_FOG;
       if (strcmp(field, "color") == 0) {
-         tokens[1] = STATE_FOG_COLOR;
+         tokens[0] = STATE_FOG_COLOR;
       }
       else if (strcmp(field, "density") == 0) {
-         tokens[1] = STATE_FOG_PARAMS;
+         tokens[0] = STATE_FOG_PARAMS;
          *swizzleOut = SWIZZLE_XXXX;
       }
       else if (strcmp(field, "start") == 0) {
-         tokens[1] = STATE_FOG_PARAMS;
+         tokens[0] = STATE_FOG_PARAMS;
          *swizzleOut = SWIZZLE_YYYY;
       }
       else if (strcmp(field, "end") == 0) {
-         tokens[1] = STATE_FOG_PARAMS;
+         tokens[0] = STATE_FOG_PARAMS;
          *swizzleOut = SWIZZLE_ZZZZ;
       }
       else if (strcmp(field, "scale") == 0) {
-         tokens[1] = STATE_FOG_PARAMS;
+         tokens[0] = STATE_FOG_PARAMS;
          *swizzleOut = SWIZZLE_WWWW;
       }
       else {
@@ -308,7 +368,7 @@ lookup_statevar(const char *var, GLint index1, GLint index2, const char *field,
       GLuint j;
       for (j = 0; j < 4; j++) {
          tokens[2] = tokens[3] = j; /* jth row of matrix */
-         pos[j] = _mesa_add_state_reference(paramList, (GLint *) tokens);
+         pos[j] = _mesa_add_state_reference(paramList, tokens);
          assert(pos[j] >= 0);
          ASSERT(pos[j] >= 0);
       }
@@ -316,7 +376,7 @@ lookup_statevar(const char *var, GLint index1, GLint index2, const char *field,
    }
    else {
       /* allocate a single register */
-      GLint pos = _mesa_add_state_reference(paramList, (GLint *) tokens);
+      GLint pos = _mesa_add_state_reference(paramList, tokens);
       ASSERT(pos >= 0);
       return pos;
    }
@@ -342,12 +402,13 @@ GLint
 _slang_alloc_statevar(slang_ir_node *n,
                       struct gl_program_parameter_list *paramList)
 {
+   slang_ir_node *n0 = n;
    const char *field = NULL, *var;
    GLint index1 = -1, index2 = -1, pos;
    GLuint swizzle;
 
    if (n->Opcode == IR_FIELD) {
-      field = n->Target;
+      field = n->Field;
       n = n->Children[0];
    }
 
@@ -371,8 +432,8 @@ _slang_alloc_statevar(slang_ir_node *n,
    pos = lookup_statevar(var, index1, index2, field, &swizzle, paramList);
    assert(pos >= 0);
    if (pos >= 0) {
-      n->Store->Index = pos;
-      n->Store->Swizzle = swizzle;
+      n0->Store->Index = pos;
+      n0->Store->Swizzle = swizzle;
    }
    return pos;
 }