Fix pow <small> and a very stypid bug with dummy srcs(0 equals to tmp0.x)</small...
[mesa.git] / src / mesa / main / texenvprogram.c
index 10a6b5f21a8464207e94f2ec1080ed6664c2c9fb..04738b019b61c9e216743c51240d8bafd50d3ee6 100644 (file)
@@ -181,7 +181,12 @@ static GLuint translate_tex_src_bit( GLbitfield bit )
    }
 }
 
-static struct state_key *make_state_key( GLcontext *ctx )
+/**
+ * Examine current texture environment state and generate a unique
+ * key to identify it.
+ */
+static struct state_key *
+make_state_key(GLcontext *ctx)
 {
    struct state_key *key = CALLOC_STRUCT(state_key);
    GLuint i, j;
@@ -264,10 +269,6 @@ const static struct ureg undef = {
    0
 };
 
-#define X    0
-#define Y    1
-#define Z    2
-#define W    3
 
 /* State used to build the fragment program:
  */
@@ -276,12 +277,9 @@ struct texenv_fragment_program {
    GLcontext *ctx;
    struct state_key *state;
 
-   GLuint alu_temps;           /* Track texture indirections, see spec. */
-   GLuint temps_output;                /* Track texture indirections, see spec. */
-
-   GLuint temp_in_use;         /* Tracks temporary regs which are in
-                                * use.
-                                */
+   GLbitfield alu_temps;       /* Track texture indirections, see spec. */
+   GLbitfield temps_output;    /* Track texture indirections, see spec. */
+   GLbitfield temp_in_use;     /* Tracks temporary regs which are in use. */
    GLboolean error;
 
    struct ureg src_texture[MAX_TEXTURE_UNITS];   
@@ -470,7 +468,7 @@ emit_op(struct texenv_fragment_program *p,
        enum prog_opcode op,
        struct ureg dest,
        GLuint mask,
-       GLuint saturate,
+       GLboolean saturate,
        struct ureg src0,
        struct ureg src1,
        struct ureg src2 )
@@ -485,7 +483,7 @@ emit_op(struct texenv_fragment_program *p,
    emit_arg( &inst->SrcReg[1], src1 );
    emit_arg( &inst->SrcReg[2], src2 );
    
-   inst->Saturate = saturate;
+   inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
 
    emit_dst( &inst->DstReg, dest, mask );
 
@@ -502,7 +500,7 @@ static struct ureg emit_arith( struct texenv_fragment_program *p,
                               enum prog_opcode op,
                               struct ureg dest,
                               GLuint mask,
-                              GLuint saturate,
+                              GLboolean saturate,
                               struct ureg src0,
                               struct ureg src1,
                               struct ureg src2 )
@@ -537,7 +535,7 @@ static struct ureg emit_texld( struct texenv_fragment_program *p,
 {
    struct prog_instruction *inst = emit_op( p, op, 
                                          dest, destmask, 
-                                         0,            /* don't saturate? */
+                                         GL_FALSE,     /* don't saturate? */
                                          coord,        /* arg 0? */
                                          undef,
                                          undef);
@@ -607,7 +605,6 @@ static struct ureg get_zero( struct texenv_fragment_program *p )
 }
 
 
-
 static void program_error( struct texenv_fragment_program *p, const char *msg )
 {
    _mesa_problem(NULL, msg);
@@ -671,7 +668,7 @@ static struct ureg emit_combine_source( struct texenv_fragment_program *p,
       if (mask == WRITEMASK_W)
         return src;
       else
-        return swizzle1( src, W );
+        return swizzle1( src, SWIZZLE_W );
    case OPR_ONE_MINUS_SRC_ALPHA: 
       /* Get unused tmp,
        * Emit tmp = 1.0 - arg.wwww
@@ -679,7 +676,7 @@ static struct ureg emit_combine_source( struct texenv_fragment_program *p,
       arg = get_temp(p);
       one = get_one(p);
       return emit_arith(p, OPCODE_SUB, arg, mask, 0,
-                       one, swizzle1(src, W), undef);
+                       one, swizzle1(src, SWIZZLE_W), undef);
    case OPR_ZERO:
       return get_zero(p);
    case OPR_ONE:
@@ -692,7 +689,7 @@ static struct ureg emit_combine_source( struct texenv_fragment_program *p,
 
 static GLboolean args_match( struct state_key *key, GLuint unit )
 {
-   int i, nr = key->unit[unit].NumArgsRGB;
+   GLuint i, nr = key->unit[unit].NumArgsRGB;
 
    for (i = 0 ; i < nr ; i++) {
       if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source) 
@@ -728,7 +725,7 @@ static GLboolean args_match( struct state_key *key, GLuint unit )
 static struct ureg emit_combine( struct texenv_fragment_program *p,
                                 struct ureg dest,
                                 GLuint mask,
-                                GLuint saturate,
+                                GLboolean saturate,
                                 GLuint unit,
                                 GLuint nr,
                                 GLuint mode,
@@ -736,7 +733,9 @@ static struct ureg emit_combine( struct texenv_fragment_program *p,
 {
    struct ureg src[3];
    struct ureg tmp, half;
-   int i;
+   GLuint i;
+
+   tmp = undef; /* silence warning (bug 5318) */
 
    for (i = 0; i < nr; i++)
       src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
@@ -816,10 +815,14 @@ static struct ureg emit_combine( struct texenv_fragment_program *p,
 }
 
 
-static struct ureg emit_texenv( struct texenv_fragment_program *p, int unit )
+/**
+ * Generate instructions for one texture unit's env/combiner mode.
+ */
+static struct ureg
+emit_texenv(struct texenv_fragment_program *p, GLuint unit)
 {
    struct state_key *key = p->state;
-   GLuint saturate = (unit < p->last_tex_stage);
+   GLboolean saturate = (unit < p->last_tex_stage);
    GLuint rgb_shift, alpha_shift;
    struct ureg out, shift;
    struct ureg dest;
@@ -909,7 +912,9 @@ static struct ureg emit_texenv( struct texenv_fragment_program *p, int unit )
 }
 
 
-
+/**
+ * Generate instruction for getting a texture source term.
+ */
 static void load_texture( struct texenv_fragment_program *p, GLuint unit )
 {
    if (is_undef(p->src_texture[unit])) {
@@ -956,7 +961,12 @@ static GLboolean load_texenv_source( struct texenv_fragment_program *p,
    return GL_TRUE;
 }
 
-static GLboolean load_texunit_sources( struct texenv_fragment_program *p, int unit )
+
+/**
+ * Generate instructions for loading all texture source terms.
+ */
+static GLboolean
+load_texunit_sources( struct texenv_fragment_program *p, int unit )
 {
    struct state_key *key = p->state;
    int i, nr = key->unit[unit].NumArgsRGB;
@@ -968,8 +978,14 @@ static GLboolean load_texunit_sources( struct texenv_fragment_program *p, int un
    return GL_TRUE;
 }
 
-static void create_new_program(struct state_key *key, GLcontext *ctx,
-                              struct fragment_program *program)
+
+/**
+ * Generate a new fragment program which implements the context's
+ * current texture env/combine mode.
+ */
+static void
+create_new_program(struct state_key *key, GLcontext *ctx,
+                   struct fragment_program *program)
 {
    struct texenv_fragment_program p;
    GLuint unit;
@@ -981,7 +997,7 @@ static void create_new_program(struct state_key *key, GLcontext *ctx,
    p.program = program;
 
    p.program->Base.Instructions =
-      _mesa_malloc(sizeof(struct prog_instruction) * MAX_INSTRUCTIONS);
+      (struct prog_instruction*) _mesa_malloc(sizeof(struct prog_instruction) * MAX_INSTRUCTIONS);
    p.program->Base.NumInstructions = 0;
    p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
    p.program->NumTexIndirections = 1;  /* correct? */
@@ -1099,7 +1115,7 @@ static void cache_item( struct texenvprog_cache **cache,
                        void *key,
                        void *data )
 {
-   struct texenvprog_cache *c = MALLOC(sizeof(*c));
+   struct texenvprog_cache *c = CALLOC_STRUCT(texenvprog_cache);
    c->hash = hash;
    c->key = key;
    c->data = data;