Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / mesa / main / ffvertex_prog.c
index adf15b03c2e7935950d94b2db9dc87091b63a49c..ec0a5e3896d91e1645347c395d698c81fffb7a52 100644 (file)
 
 
 struct state_key {
+   unsigned light_color_material_mask:12;
+   unsigned light_material_mask:12;
    unsigned light_global_enabled:1;
    unsigned light_local_viewer:1;
    unsigned light_twoside:1;
    unsigned light_color_material:1;
-   unsigned light_color_material_mask:12;
-   unsigned light_material_mask:12;
-
+   unsigned material_shininess_is_zero:1;
    unsigned need_eye_coords:1;
    unsigned normalize:1;
    unsigned rescale_normals:1;
+
    unsigned fog_source_is_depth:1;
    unsigned tnl_do_vertex_fog:1;
    unsigned separate_specular:1;
    unsigned fog_mode:2;
    unsigned point_attenuated:1;
+   unsigned point_array:1;
    unsigned texture_enabled_global:1;
    unsigned fragprog_inputs_read:12;
 
+   unsigned varying_vp_inputs;
+
    struct {
       unsigned light_enabled:1;
       unsigned light_eyepos3_is_zero:1;
@@ -155,13 +159,33 @@ tnl_get_per_vertex_fog(GLcontext *ctx)
 #endif
 }
 
+static GLboolean check_active_shininess( GLcontext *ctx,
+                                         const struct state_key *key,
+                                         GLuint side )
+{
+   GLuint bit = 1 << (MAT_ATTRIB_FRONT_SHININESS + side);
+
+   if (key->light_color_material_mask & bit)
+      return GL_TRUE;
+
+   if (key->light_material_mask & bit)
+      return GL_TRUE;
+
+   if (ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS + side][0] != 0.0F)
+      return GL_TRUE;
+
+   return GL_FALSE;
+}
+     
+
 
-static struct state_key *make_state_key( GLcontext *ctx )
+
+static void make_state_key( GLcontext *ctx, struct state_key *key )
 {
    const struct gl_fragment_program *fp;
-   struct state_key *key = CALLOC_STRUCT(state_key);
    GLuint i;
 
+   memset(key, 0, sizeof(struct state_key));
    fp = ctx->FragmentProgram._Current;
 
    /* This now relies on texenvprogram.c being active:
@@ -171,6 +195,7 @@ static struct state_key *make_state_key( GLcontext *ctx )
    key->need_eye_coords = ctx->_NeedEyeCoords;
 
    key->fragprog_inputs_read = fp->Base.InputsRead;
+   key->varying_vp_inputs = ctx->varying_vp_inputs;
 
    if (ctx->RenderMode == GL_FEEDBACK) {
       /* make sure the vertprog emits color and tex0 */
@@ -214,6 +239,17 @@ static struct state_key *make_state_key( GLcontext *ctx )
               key->unit[i].light_attenuated = 1;
         }
       }
+
+      if (check_active_shininess(ctx, key, 0)) {
+         key->material_shininess_is_zero = 0;
+      }
+      else if (key->light_twoside &&
+               check_active_shininess(ctx, key, 1)) {
+         key->material_shininess_is_zero = 0;
+      }
+      else {
+         key->material_shininess_is_zero = 1;
+      }
    }
 
    if (ctx->Transform.Normalize)
@@ -232,6 +268,11 @@ static struct state_key *make_state_key( GLcontext *ctx )
    if (ctx->Point._Attenuated)
       key->point_attenuated = 1;
 
+#if FEATURE_point_size_array
+   if (ctx->Array.ArrayObj->PointSize.Enabled)
+      key->point_array = 1;
+#endif
+
    if (ctx->Texture._TexGenEnabled ||
        ctx->Texture._TexMatEnabled ||
        ctx->Texture._EnabledUnits)
@@ -263,8 +304,6 @@ static struct state_key *make_state_key( GLcontext *ctx )
                              texUnit->GenModeQ );
       }
    }
-   
-   return key;
 }
 
 
@@ -273,7 +312,7 @@ static struct state_key *make_state_key( GLcontext *ctx )
  * generated program with line/function references for each
  * instruction back into this file:
  */
-#define DISASSEM (MESA_VERBOSE&VERBOSE_DISASSEM)
+#define DISASSEM 0
 
 /* Should be tunable by the driver - do we want to do matrix
  * multiplications with DP4's or with MUL/MAD's?  SSE works better
@@ -281,7 +320,6 @@ static struct state_key *make_state_key( GLcontext *ctx )
  */
 #define PREFER_DP4 0
 
-#define MAX_INSN 256
 
 /* Use uregs to represent registers internally, translate to Mesa's
  * expected formats on emit.  
@@ -297,21 +335,24 @@ static struct state_key *make_state_key( GLcontext *ctx )
  */
 struct ureg {
    GLuint file:4;
-   GLint idx:8;      /* relative addressing may be negative */
+   GLint idx:9;      /* relative addressing may be negative */
+                     /* sizeof(idx) should == sizeof(prog_src_reg::Index) */
    GLuint negate:1;
    GLuint swz:12;
-   GLuint pad:7;
+   GLuint pad:6;
 };
 
 
 struct tnl_program {
    const struct state_key *state;
    struct gl_vertex_program *program;
+   GLint max_inst;  /** number of instructions allocated for program */
    
    GLuint temp_in_use;
    GLuint temp_reserved;
    
    struct ureg eye_position;
+   struct ureg eye_position_z;
    struct ureg eye_position_normalized;
    struct ureg transformed_normal;
    struct ureg identity;
@@ -323,7 +364,7 @@ struct tnl_program {
 
 static const struct ureg undef = { 
    PROGRAM_UNDEFINED,
-   ~0,
+   0,
    0,
    0,
    0
@@ -410,13 +451,51 @@ static void release_temps( struct tnl_program *p )
 }
 
 
+static struct ureg register_param5(struct tnl_program *p, 
+                                  GLint s0,
+                                  GLint s1,
+                                  GLint s2,
+                                  GLint s3,
+                                   GLint s4)
+{
+   gl_state_index tokens[STATE_LENGTH];
+   GLint idx;
+   tokens[0] = s0;
+   tokens[1] = s1;
+   tokens[2] = s2;
+   tokens[3] = s3;
+   tokens[4] = s4;
+   idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
+   return make_ureg(PROGRAM_STATE_VAR, idx);
+}
+
+
+#define register_param1(p,s0)          register_param5(p,s0,0,0,0,0)
+#define register_param2(p,s0,s1)       register_param5(p,s0,s1,0,0,0)
+#define register_param3(p,s0,s1,s2)    register_param5(p,s0,s1,s2,0,0)
+#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
+
 
+
+/**
+ * \param input  one of VERT_ATTRIB_x tokens.
+ */
 static struct ureg register_input( struct tnl_program *p, GLuint input )
 {
-   p->program->Base.InputsRead |= (1<<input);
-   return make_ureg(PROGRAM_INPUT, input);
+   /* Material attribs are passed here as inputs >= 32
+    */
+   if (input >= 32 || (p->state->varying_vp_inputs & (1<<input))) {
+      p->program->Base.InputsRead |= (1<<input);
+      return make_ureg(PROGRAM_INPUT, input);
+   }
+   else {
+      return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, input );
+   }
 }
 
+/**
+ * \param input  one of VERT_RESULT_x tokens.
+ */
 static struct ureg register_output( struct tnl_program *p, GLuint output )
 {
    p->program->Base.OutputsWritten |= (1<<output);
@@ -460,31 +539,6 @@ static struct ureg get_identity_param( struct tnl_program *p )
    return p->identity;
 }
 
-static struct ureg register_param5(struct tnl_program *p, 
-                                  GLint s0,
-                                  GLint s1,
-                                  GLint s2,
-                                  GLint s3,
-                                   GLint s4)
-{
-   gl_state_index tokens[STATE_LENGTH];
-   GLint idx;
-   tokens[0] = s0;
-   tokens[1] = s1;
-   tokens[2] = s2;
-   tokens[3] = s3;
-   tokens[4] = s4;
-   idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
-   return make_ureg(PROGRAM_STATE_VAR, idx);
-}
-
-
-#define register_param1(p,s0)          register_param5(p,s0,0,0,0,0)
-#define register_param2(p,s0,s1)       register_param5(p,s0,s1,0,0,0)
-#define register_param3(p,s0,s1,s2)    register_param5(p,s0,s1,s2,0,0)
-#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
-
-
 static void register_matrix_param5( struct tnl_program *p,
                                    GLint s0, /* modelview, projection, etc */
                                    GLint s1, /* texture matrix number */
@@ -513,6 +567,8 @@ static void emit_arg( struct prog_src_register *src,
    src->Abs = 0;
    src->NegateAbs = 0;
    src->RelAddr = 0;
+   /* Check that bitfield sizes aren't exceeded */
+   ASSERT(src->Index == reg.idx);
 }
 
 static void emit_dst( struct prog_dst_register *dst,
@@ -526,6 +582,8 @@ static void emit_dst( struct prog_dst_register *dst,
    dst->CondSwizzle = SWIZZLE_NOOP;
    dst->CondSrc = 0;
    dst->pad = 0;
+   /* Check that bitfield sizes aren't exceeded */
+   ASSERT(dst->Index == reg.idx);
 }
 
 static void debug_insn( struct prog_instruction *inst, const char *fn,
@@ -555,14 +613,37 @@ static void emit_op3fn(struct tnl_program *p,
                       const char *fn,
                       GLuint line)
 {
-   GLuint nr = p->program->Base.NumInstructions++;
-   struct prog_instruction *inst = &p->program->Base.Instructions[nr];
+   GLuint nr;
+   struct prog_instruction *inst;
       
-   if (p->program->Base.NumInstructions > MAX_INSN) {
-      _mesa_problem(0, "Out of instructions in emit_op3fn\n");
-      return;
+   assert((GLint) p->program->Base.NumInstructions <= p->max_inst);
+
+   if (p->program->Base.NumInstructions == p->max_inst) {
+      /* need to extend the program's instruction array */
+      struct prog_instruction *newInst;
+
+      /* double the size */
+      p->max_inst *= 2;
+
+      newInst = _mesa_alloc_instructions(p->max_inst);
+      if (!newInst) {
+         _mesa_error(NULL, GL_OUT_OF_MEMORY, "vertex program build");
+         return;
+      }
+
+      _mesa_copy_instructions(newInst,
+                              p->program->Base.Instructions,
+                              p->program->Base.NumInstructions);
+
+      _mesa_free_instructions(p->program->Base.Instructions,
+                              p->program->Base.NumInstructions);
+
+      p->program->Base.Instructions = newInst;
    }
       
+   nr = p->program->Base.NumInstructions++;
+
+   inst = &p->program->Base.Instructions[nr];
    inst->Opcode = (enum prog_opcode) op; 
    inst->StringPos = 0;
    inst->Data = 0;
@@ -655,11 +736,16 @@ static void emit_normalize_vec3( struct tnl_program *p,
                                 struct ureg dest,
                                 struct ureg src )
 {
+#if 0
+   /* XXX use this when drivers are ready for NRM3 */
+   emit_op1(p, OPCODE_NRM3, dest, WRITEMASK_XYZ, src);
+#else
    struct ureg tmp = get_temp(p);
    emit_op2(p, OPCODE_DP3, tmp, WRITEMASK_X, src, src);
    emit_op1(p, OPCODE_RSQ, tmp, WRITEMASK_X, tmp);
    emit_op2(p, OPCODE_MUL, dest, 0, src, swizzle1(tmp, X));
    release_temp(p, tmp);
+#endif
 }
 
 static void emit_passthrough( struct tnl_program *p, 
@@ -696,6 +782,28 @@ static struct ureg get_eye_position( struct tnl_program *p )
 }
 
 
+static struct ureg get_eye_position_z( struct tnl_program *p )
+{
+   if (!is_undef(p->eye_position)) 
+      return swizzle1(p->eye_position, Z);
+
+   if (is_undef(p->eye_position_z)) {
+      struct ureg pos = register_input( p, VERT_ATTRIB_POS ); 
+      struct ureg modelview[4];
+
+      p->eye_position_z = reserve_temp(p);
+
+      register_matrix_param5( p, STATE_MODELVIEW_MATRIX, 0, 0, 3,
+                              0, modelview );
+
+      emit_op2(p, OPCODE_DP4, p->eye_position_z, 0, pos, modelview[2]);
+   }
+   
+   return p->eye_position_z;
+}
+   
+
+
 static struct ureg get_eye_position_normalized( struct tnl_program *p )
 {
    if (is_undef(p->eye_position_normalized)) {
@@ -745,8 +853,7 @@ static struct ureg get_transformed_normal( struct tnl_program *p )
         struct ureg rescale = register_param2(p, STATE_INTERNAL,
                                                STATE_NORMAL_SCALE);
 
-        emit_op2( p, OPCODE_MUL, transformed_normal, 0, normal,
-                  swizzle1(rescale, X));
+        emit_op2( p, OPCODE_MUL, transformed_normal, 0, normal, rescale );
          normal = transformed_normal;
       }
       
@@ -915,7 +1022,30 @@ static struct ureg calculate_light_attenuation( struct tnl_program *p,
 }
                                                
 
+/**
+ * Compute:
+ *   lit.y = MAX(0, dots.x)
+ *   lit.z = SLT(0, dots.x)
+ */
+static void emit_degenerate_lit( struct tnl_program *p,
+                                 struct ureg lit,
+                                 struct ureg dots )
+{
+   struct ureg id = get_identity_param(p);  /* id = {0,0,0,1} */
+
+   /* Note that lit.x & lit.w will not be examined.  Note also that
+    * dots.xyzw == dots.xxxx.
+    */
+
+   /* MAX lit, id, dots;
+    */
+   emit_op2(p, OPCODE_MAX, lit, WRITEMASK_XYZW, id, dots); 
 
+   /* result[2] = (in > 0 ? 1 : 0)
+    * SLT lit.z, id.z, dots;   # lit.z = (0 < dots.z) ? 1 : 0
+    */
+   emit_op2(p, OPCODE_SLT, lit, WRITEMASK_Z, swizzle1(id,Z), dots);
+}
 
 
 /* Need to add some addtional parameters to allow lighting in object
@@ -934,6 +1064,14 @@ static void build_lighting( struct tnl_program *p )
    struct ureg _bfc0 = undef, _bfc1 = undef;
    GLuint i;
 
+   /*
+    * NOTE:
+    * dot.x = dot(normal, VPpli)
+    * dot.y = dot(normal, halfAngle)
+    * dot.z = back.shininess
+    * dot.w = front.shininess
+    */
+
    for (i = 0; i < MAX_LIGHTS; i++) 
       if (p->state->unit[i].light_enabled)
         nr_lights++;
@@ -941,9 +1079,11 @@ static void build_lighting( struct tnl_program *p )
    set_material_flags(p);
 
    {
-      struct ureg shininess = get_material(p, 0, STATE_SHININESS);
-      emit_op1(p, OPCODE_MOV, dots,  WRITEMASK_W, swizzle1(shininess,X));
-      release_temp(p, shininess);
+      if (!p->state->material_shininess_is_zero) {
+         struct ureg shininess = get_material(p, 0, STATE_SHININESS);
+         emit_op1(p, OPCODE_MOV, dots,  WRITEMASK_W, swizzle1(shininess,X));
+         release_temp(p, shininess);
+      }
 
       _col0 = make_temp(p, get_scenecolor(p, 0));
       if (separate)
@@ -954,10 +1094,12 @@ static void build_lighting( struct tnl_program *p )
    }
 
    if (twoside) {
-      struct ureg shininess = get_material(p, 1, STATE_SHININESS);
-      emit_op1(p, OPCODE_MOV, dots, WRITEMASK_Z, 
-              negate(swizzle1(shininess,X)));
-      release_temp(p, shininess);
+      if (!p->state->material_shininess_is_zero) {
+         struct ureg shininess = get_material(p, 1, STATE_SHININESS);
+         emit_op1(p, OPCODE_MOV, dots, WRITEMASK_Z, 
+                  negate(swizzle1(shininess,X)));
+         release_temp(p, shininess);
+      }
 
       _bfc0 = make_temp(p, get_scenecolor(p, 1));
       if (separate)
@@ -1006,14 +1148,17 @@ static void build_lighting( struct tnl_program *p )
             */
            VPpli = register_param3(p, STATE_INTERNAL, 
                                    STATE_LIGHT_POSITION_NORMALIZED, i); 
-            if (p->state->light_local_viewer) {
-                struct ureg eye_hat = get_eye_position_normalized(p);
-                half = get_temp(p);
-                emit_op2(p, OPCODE_SUB, half, 0, VPpli, eye_hat);
-                emit_normalize_vec3(p, half, half);
-            } else {
-                half = register_param3(p, STATE_INTERNAL, 
-                                       STATE_LIGHT_HALF_VECTOR, i);
+            
+            if (!p->state->material_shininess_is_zero) {
+               if (p->state->light_local_viewer) {
+                  struct ureg eye_hat = get_eye_position_normalized(p);
+                  half = get_temp(p);
+                  emit_op2(p, OPCODE_SUB, half, 0, VPpli, eye_hat);
+                  emit_normalize_vec3(p, half, half);
+               } else {
+                  half = register_param3(p, STATE_INTERNAL, 
+                                         STATE_LIGHT_HALF_VECTOR, i);
+               }
             }
         } 
         else {
@@ -1023,7 +1168,6 @@ static void build_lighting( struct tnl_program *p )
            struct ureg dist = get_temp(p);
 
            VPpli = get_temp(p); 
-           half = get_temp(p);
  
            /* Calculate VPpli vector
             */
@@ -1045,24 +1189,33 @@ static void build_lighting( struct tnl_program *p )
 
            /* Calculate viewer direction, or use infinite viewer:
             */
-           if (p->state->light_local_viewer) {
-              struct ureg eye_hat = get_eye_position_normalized(p);
-              emit_op2(p, OPCODE_SUB, half, 0, VPpli, eye_hat);
-           }
-           else {
-              struct ureg z_dir = swizzle(get_identity_param(p),X,Y,W,Z); 
-              emit_op2(p, OPCODE_ADD, half, 0, VPpli, z_dir);
-           }
-
-           emit_normalize_vec3(p, half, half);
+            if (!p->state->material_shininess_is_zero) {
+               half = get_temp(p);
+
+               if (p->state->light_local_viewer) {
+                  struct ureg eye_hat = get_eye_position_normalized(p);
+                  emit_op2(p, OPCODE_SUB, half, 0, VPpli, eye_hat);
+               }
+               else {
+                  struct ureg z_dir = swizzle(get_identity_param(p),X,Y,W,Z); 
+                  emit_op2(p, OPCODE_ADD, half, 0, VPpli, z_dir);
+               }
+
+               emit_normalize_vec3(p, half, half);
+            }
 
            release_temp(p, dist);
         }
 
         /* Calculate dot products:
          */
-        emit_op2(p, OPCODE_DP3, dots, WRITEMASK_X, normal, VPpli);
-        emit_op2(p, OPCODE_DP3, dots, WRITEMASK_Y, normal, half);
+         if (p->state->material_shininess_is_zero) {
+            emit_op2(p, OPCODE_DP3, dots, 0, normal, VPpli);
+         }
+         else {
+            emit_op2(p, OPCODE_DP3, dots, WRITEMASK_X, normal, VPpli);
+            emit_op2(p, OPCODE_DP3, dots, WRITEMASK_Y, normal, half);
+         }
 
         /* Front face lighting:
          */
@@ -1073,11 +1226,6 @@ static void build_lighting( struct tnl_program *p )
            struct ureg res0, res1;
            GLuint mask0, mask1;
 
-           emit_op1(p, OPCODE_LIT, lit, 0, dots);
-   
-           if (!is_undef(att)) 
-              emit_op2(p, OPCODE_MUL, lit, 0, lit, att);
-   
            
            if (count == nr_lights) {
               if (separate) {
@@ -1099,7 +1247,24 @@ static void build_lighting( struct tnl_program *p )
               res1 = _col1;
            }
 
-           emit_op3(p, OPCODE_MAD, _col0, 0, swizzle1(lit,X), ambient, _col0);
+
+           if (!is_undef(att)) {
+               /* light is attenuated by distance */
+               emit_op1(p, OPCODE_LIT, lit, 0, dots);
+               emit_op2(p, OPCODE_MUL, lit, 0, lit, att);
+               emit_op3(p, OPCODE_MAD, _col0, 0, swizzle1(lit,X), ambient, _col0);
+            } 
+            else if (!p->state->material_shininess_is_zero) {
+               /* there's a non-zero specular term */
+               emit_op1(p, OPCODE_LIT, lit, 0, dots);
+               emit_op2(p, OPCODE_ADD, _col0, 0, ambient, _col0);
+            } 
+            else {
+               /* no attenutation, no specular */
+               emit_degenerate_lit(p, lit, dots);
+               emit_op2(p, OPCODE_ADD, _col0, 0, ambient, _col0);
+            }
+
            emit_op3(p, OPCODE_MAD, res0, mask0, swizzle1(lit,Y), diffuse, _col0);
            emit_op3(p, OPCODE_MAD, res1, mask1, swizzle1(lit,Z), specular, _col1);
       
@@ -1117,11 +1282,6 @@ static void build_lighting( struct tnl_program *p )
            struct ureg res0, res1;
            GLuint mask0, mask1;
               
-           emit_op1(p, OPCODE_LIT, lit, 0, negate(swizzle(dots,X,Y,W,Z)));
-
-           if (!is_undef(att)) 
-              emit_op2(p, OPCODE_MUL, lit, 0, lit, att);
-
            if (count == nr_lights) {
               if (separate) {
                  mask0 = WRITEMASK_XYZ;
@@ -1142,10 +1302,28 @@ static void build_lighting( struct tnl_program *p )
               mask1 = 0;
            }
 
-           emit_op3(p, OPCODE_MAD, _bfc0, 0, swizzle1(lit,X), ambient, _bfc0);
+            dots = negate(swizzle(dots,X,Y,W,Z));
+
+           if (!is_undef(att)) {
+               emit_op1(p, OPCODE_LIT, lit, 0, dots);
+              emit_op2(p, OPCODE_MUL, lit, 0, lit, att);
+               emit_op3(p, OPCODE_MAD, _bfc0, 0, swizzle1(lit,X), ambient, _bfc0);
+            }
+            else if (!p->state->material_shininess_is_zero) {
+               emit_op1(p, OPCODE_LIT, lit, 0, dots);
+               emit_op2(p, OPCODE_ADD, _bfc0, 0, ambient, _bfc0);
+            } 
+            else {
+               emit_degenerate_lit(p, lit, dots);
+               emit_op2(p, OPCODE_ADD, _bfc0, 0, ambient, _bfc0);
+            }
+
            emit_op3(p, OPCODE_MAD, res0, mask0, swizzle1(lit,Y), diffuse, _bfc0);
            emit_op3(p, OPCODE_MAD, res1, mask1, swizzle1(lit,Z), specular, _bfc1);
 
+            /* restore negate flag for next lighting */
+            dots = negate(dots);
+
            release_temp(p, ambient);
            release_temp(p, diffuse);
            release_temp(p, specular);
@@ -1167,7 +1345,7 @@ static void build_fog( struct tnl_program *p )
    struct ureg input;
 
    if (p->state->fog_source_is_depth) {
-      input = swizzle1(get_eye_position(p), Z);
+      input = get_eye_position_z(p);
    }
    else {
       input = swizzle1(register_input(p, VERT_ATTRIB_FOG), X);
@@ -1395,9 +1573,12 @@ static void build_texture_transform( struct tnl_program *p )
 }
 
 
-static void build_pointsize( struct tnl_program *p )
+/**
+ * Point size attenuation computation.
+ */
+static void build_atten_pointsize( struct tnl_program *p )
 {
-   struct ureg eye = get_eye_position(p);
+   struct ureg eye = get_eye_position_z(p);
    struct ureg state_size = register_param1(p, STATE_POINT_SIZE);
    struct ureg state_attenuation = register_param1(p, STATE_POINT_ATTENUATION);
    struct ureg out = register_output(p, VERT_RESULT_PSIZ);
@@ -1432,13 +1613,24 @@ static void build_pointsize( struct tnl_program *p )
 /**
  * Emit constant point size.
  */
-static void constant_pointsize( struct tnl_program *p )
+static void build_constant_pointsize( struct tnl_program *p )
 {
    struct ureg state_size = register_param1(p, STATE_POINT_SIZE);
    struct ureg out = register_output(p, VERT_RESULT_PSIZ);
    emit_op1(p, OPCODE_MOV, out, WRITEMASK_X, state_size);
 }
 
+/**
+ * Pass-though per-vertex point size, from user's point size array.
+ */
+static void build_array_pointsize( struct tnl_program *p )
+{
+   struct ureg in = register_input(p, VERT_ATTRIB_POINT_SIZE);
+   struct ureg out = register_output(p, VERT_RESULT_PSIZ);
+   emit_op1(p, OPCODE_MOV, out, WRITEMASK_X, in);
+}
+
+
 static void build_tnl_program( struct tnl_program *p )
 {   /* Emit the program, starting with modelviewproject:
     */
@@ -1466,10 +1658,14 @@ static void build_tnl_program( struct tnl_program *p )
       build_texture_transform(p);
 
    if (p->state->point_attenuated)
-      build_pointsize(p);
+      build_atten_pointsize(p);
+   else if (p->state->point_array)
+      build_array_pointsize(p);
 #if 0
    else
-      constant_pointsize(p);
+      build_constant_pointsize(p);
+#else
+   (void) build_constant_pointsize;
 #endif
 
    /* Finish up:
@@ -1495,6 +1691,7 @@ create_new_program( const struct state_key *key,
    p.state = key;
    p.program = program;
    p.eye_position = undef;
+   p.eye_position_z = undef;
    p.eye_position_normalized = undef;
    p.transformed_normal = undef;
    p.identity = undef;
@@ -1505,7 +1702,11 @@ create_new_program( const struct state_key *key,
    else
       p.temp_reserved = ~((1<<max_temps)-1);
 
-   p.program->Base.Instructions = _mesa_alloc_instructions(MAX_INSN);
+   /* Start by allocating 32 instructions.
+    * If we need more, we'll grow the instruction array as needed.
+    */
+   p.max_inst = 32;
+   p.program->Base.Instructions = _mesa_alloc_instructions(p.max_inst);
    p.program->Base.String = NULL;
    p.program->Base.NumInstructions =
    p.program->Base.NumTemporaries =
@@ -1528,16 +1729,16 @@ struct gl_vertex_program *
 _mesa_get_fixed_func_vertex_program(GLcontext *ctx)
 {
    struct gl_vertex_program *prog;
-   struct state_key *key;
+   struct state_key key;
 
    /* Grab all the relevent state and put it in a single structure:
     */
-   key = make_state_key(ctx);
+   make_state_key(ctx, &key);
 
    /* Look for an already-prepared program for this state:
     */
    prog = (struct gl_vertex_program *)
-      _mesa_search_program_cache(ctx->VertexProgram.Cache, key, sizeof(*key));
+      _mesa_search_program_cache(ctx->VertexProgram.Cache, &key, sizeof(key));
    
    if (!prog) {
       /* OK, we'll have to build a new one */
@@ -1549,7 +1750,7 @@ _mesa_get_fixed_func_vertex_program(GLcontext *ctx)
       if (!prog)
          return NULL;
 
-      create_new_program( key, prog,
+      create_new_program( &key, prog,
                           ctx->Const.VertexProgram.MaxTemps );
 
 #if 0
@@ -1558,10 +1759,8 @@ _mesa_get_fixed_func_vertex_program(GLcontext *ctx)
                                           &prog->Base );
 #endif
       _mesa_program_cache_insert(ctx, ctx->VertexProgram.Cache,
-                                 key, sizeof(*key), &prog->Base);
+                                 &key, sizeof(key), &prog->Base);
    }
 
-   _mesa_free(key);
-
    return prog;
 }