mesa: add extension checks in _mesa_max_texture_levels()
[mesa.git] / src / mesa / state_tracker / st_atom_pixeltransfer.c
index b190c39343ad816e250c0860d32cfc1f353a41b9..eff3666ca88a691cbed1cd107d2f1ae6a3c5d27c 100644 (file)
 #include "shader/prog_print.h"
 
 #include "st_context.h"
+#include "st_format.h"
+#include "st_program.h"
+#include "st_texture.h"
+#include "st_inlines.h"
 
+#include "pipe/p_screen.h"
+#include "pipe/p_context.h"
+#include "util/u_pack_color.h"
 
 
 struct state_key
 {
    GLuint scaleAndBias:1;
    GLuint colorMatrix:1;
+   GLuint colorMatrixPostScaleBias:1;
+   GLuint pixelMaps:1;
 
 #if 0
    GLfloat Maps[3][256][4];
@@ -68,7 +77,7 @@ is_identity(const GLfloat m[16])
    GLuint i;
    for (i = 0; i < 16; i++) {
       const int row = i % 4, col = i / 4;
-      const float val = (row == col);
+      const float val = (GLfloat)(row == col);
       if (m[i] != val)
          return GL_FALSE;
    }
@@ -79,8 +88,9 @@ is_identity(const GLfloat m[16])
 static void
 make_state_key(GLcontext *ctx,  struct state_key *key)
 {
-   /*GLuint i, j;*/
-       
+   static const GLfloat zero[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
+   static const GLfloat one[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
+
    memset(key, 0, sizeof(*key));
 
    if (ctx->Pixel.RedBias != 0.0 || ctx->Pixel.RedScale != 1.0 ||
@@ -93,9 +103,77 @@ make_state_key(GLcontext *ctx,  struct state_key *key)
    if (!is_identity(ctx->ColorMatrixStack.Top->m)) {
       key->colorMatrix = 1;
    }
+
+   if (!TEST_EQ_4V(ctx->Pixel.PostColorMatrixScale, one) ||
+       !TEST_EQ_4V(ctx->Pixel.PostColorMatrixBias, zero)) {
+      key->colorMatrixPostScaleBias = 1;
+   }
+
+   key->pixelMaps = ctx->Pixel.MapColorFlag;
 }
 
 
+static struct pipe_texture *
+create_color_map_texture(GLcontext *ctx)
+{
+   struct pipe_context *pipe = ctx->st->pipe;
+   struct pipe_texture *pt;
+   enum pipe_format format;
+   const uint texSize = 256; /* simple, and usually perfect */
+
+   /* find an RGBA texture format */
+   format = st_choose_format(pipe, GL_RGBA, PIPE_TEXTURE_2D, PIPE_TEXTURE_USAGE_SAMPLER);
+
+   /* create texture for color map/table */
+   pt = st_texture_create(ctx->st, PIPE_TEXTURE_2D, format, 0,
+                          texSize, texSize, 1, PIPE_TEXTURE_USAGE_SAMPLER);
+   return pt;
+}
+
+
+/**
+ * Update the pixelmap texture with the contents of the R/G/B/A pixel maps.
+ */
+static void
+load_color_map_texture(GLcontext *ctx, struct pipe_texture *pt)
+{
+   struct pipe_context *pipe = ctx->st->pipe;
+   struct pipe_screen *screen = pipe->screen;
+   struct pipe_transfer *transfer;
+   const GLuint rSize = ctx->PixelMaps.RtoR.Size;
+   const GLuint gSize = ctx->PixelMaps.GtoG.Size;
+   const GLuint bSize = ctx->PixelMaps.BtoB.Size;
+   const GLuint aSize = ctx->PixelMaps.AtoA.Size;
+   const uint texSize = pt->width[0];
+   uint *dest;
+   uint i, j;
+
+   transfer = st_cond_flush_get_tex_transfer(st_context(ctx),
+                                            pt, 0, 0, 0, PIPE_TRANSFER_WRITE,
+                                            0, 0, texSize, texSize);
+   dest = (uint *) screen->transfer_map(screen, transfer);
+
+   /* Pack four 1D maps into a 2D texture:
+    * R map is placed horizontally, indexed by S, in channel 0
+    * G map is placed vertically, indexed by T, in channel 1
+    * B map is placed horizontally, indexed by S, in channel 2
+    * A map is placed vertically, indexed by T, in channel 3
+    */
+   for (i = 0; i < texSize; i++) {
+      for (j = 0; j < texSize; j++) {
+         int k = (i * texSize + j);
+         ubyte r = ctx->PixelMaps.RtoR.Map8[j * rSize / texSize];
+         ubyte g = ctx->PixelMaps.GtoG.Map8[i * gSize / texSize];
+         ubyte b = ctx->PixelMaps.BtoB.Map8[j * bSize / texSize];
+         ubyte a = ctx->PixelMaps.AtoA.Map8[i * aSize / texSize];
+         util_pack_color_ub(r, g, b, a, pt->format, dest + k);
+      }
+   }
+
+   screen->transfer_unmap(screen, transfer);
+   screen->tex_transfer_destroy(transfer);
+}
+
 
 
 #define MAX_INST 100
@@ -106,10 +184,12 @@ make_state_key(GLcontext *ctx,  struct state_key *key)
 static struct gl_fragment_program *
 get_pixel_transfer_program(GLcontext *ctx, const struct state_key *key)
 {
+   struct st_context *st = ctx->st;
    struct prog_instruction inst[MAX_INST];
    struct gl_program_parameter_list *params;
    struct gl_fragment_program *fp;
    GLuint ic = 0;
+   const GLuint colorTemp = 0;
 
    fp = (struct gl_fragment_program *)
       ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
@@ -118,20 +198,23 @@ get_pixel_transfer_program(GLcontext *ctx, const struct state_key *key)
 
    params = _mesa_new_parameter_list();
 
-   /* TEX result.color, fragment.texcoord[0], texture[0], 2D; */
+   /*
+    * Get initial pixel color from the texture.
+    * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
+    */
    _mesa_init_instructions(inst + ic, 1);
    inst[ic].Opcode = OPCODE_TEX;
-   inst[ic].DstReg.File = PROGRAM_OUTPUT;
-   inst[ic].DstReg.Index = FRAG_RESULT_COLR;
+   inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+   inst[ic].DstReg.Index = colorTemp;
    inst[ic].SrcReg[0].File = PROGRAM_INPUT;
    inst[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
    inst[ic].TexSrcUnit = 0;
    inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
    ic++;
    fp->Base.InputsRead = (1 << FRAG_ATTRIB_TEX0);
-   fp->Base.OutputsWritten = (1 << FRAG_RESULT_COLR);
+   fp->Base.OutputsWritten = (1 << FRAG_RESULT_COLOR);
+   fp->Base.SamplersUsed = 0x1;  /* sampler 0 (bit 0) is used */
 
-   /* MAD result.color, result.color, scale, bias; */
    if (key->scaleAndBias) {
       static const gl_state_index scale_state[STATE_LENGTH] =
          { STATE_INTERNAL, STATE_PT_SCALE, 0, 0, 0 };
@@ -152,12 +235,13 @@ get_pixel_transfer_program(GLcontext *ctx, const struct state_key *key)
       scale_p = _mesa_add_state_reference(params, scale_state);
       bias_p = _mesa_add_state_reference(params, bias_state);
 
+      /* MAD colorTemp, colorTemp, scale, bias; */
       _mesa_init_instructions(inst + ic, 1);
       inst[ic].Opcode = OPCODE_MAD;
-      inst[ic].DstReg.File = PROGRAM_OUTPUT;
-      inst[ic].DstReg.Index = FRAG_RESULT_COLR;
-      inst[ic].SrcReg[0].File = PROGRAM_OUTPUT;
-      inst[ic].SrcReg[0].Index = FRAG_RESULT_COLR;
+      inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+      inst[ic].DstReg.Index = colorTemp;
+      inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
+      inst[ic].SrcReg[0].Index = colorTemp;
       inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
       inst[ic].SrcReg[1].Index = scale_p;
       inst[ic].SrcReg[2].File = PROGRAM_STATE_VAR;
@@ -165,6 +249,56 @@ get_pixel_transfer_program(GLcontext *ctx, const struct state_key *key)
       ic++;
    }
 
+   if (key->pixelMaps) {
+      const GLuint temp = 1;
+
+      /* create the colormap/texture now if not already done */
+      if (!st->pixel_xfer.pixelmap_texture) {
+         st->pixel_xfer.pixelmap_texture = create_color_map_texture(ctx);
+      }
+
+      /* with a little effort, we can do four pixel map look-ups with
+       * two TEX instructions:
+       */
+
+      /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
+      _mesa_init_instructions(inst + ic, 1);
+      inst[ic].Opcode = OPCODE_TEX;
+      inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+      inst[ic].DstReg.Index = temp;
+      inst[ic].DstReg.WriteMask = WRITEMASK_XY; /* write R,G */
+      inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
+      inst[ic].SrcReg[0].Index = colorTemp;
+      inst[ic].TexSrcUnit = 1;
+      inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
+      ic++;
+
+      /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
+      _mesa_init_instructions(inst + ic, 1);
+      inst[ic].Opcode = OPCODE_TEX;
+      inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+      inst[ic].DstReg.Index = temp;
+      inst[ic].DstReg.WriteMask = WRITEMASK_ZW; /* write B,A */
+      inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
+      inst[ic].SrcReg[0].Index = colorTemp;
+      inst[ic].SrcReg[0].Swizzle = MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W,
+                                                 SWIZZLE_Z, SWIZZLE_W);
+      inst[ic].TexSrcUnit = 1;
+      inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
+      ic++;
+
+      /* MOV colorTemp, temp; */
+      _mesa_init_instructions(inst + ic, 1);
+      inst[ic].Opcode = OPCODE_MOV;
+      inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+      inst[ic].DstReg.Index = colorTemp;
+      inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
+      inst[ic].SrcReg[0].Index = temp;
+      ic++;
+
+      fp->Base.SamplersUsed |= (1 << 1);  /* sampler 1 is used */
+   }
+
    if (key->colorMatrix) {
       static const gl_state_index row0_state[STATE_LENGTH] =
          { STATE_COLOR_MATRIX, 0, 0, 0, 0 };
@@ -179,63 +313,94 @@ get_pixel_transfer_program(GLcontext *ctx, const struct state_key *key)
       GLint row1_p = _mesa_add_state_reference(params, row1_state);
       GLint row2_p = _mesa_add_state_reference(params, row2_state);
       GLint row3_p = _mesa_add_state_reference(params, row3_state);
+      const GLuint temp = 1;
 
-      /* MOV temp0, result.color; */
-      _mesa_init_instructions(inst + ic, 1);
-      inst[ic].Opcode = OPCODE_MOV;
-      inst[ic].DstReg.File = PROGRAM_TEMPORARY;
-      inst[ic].DstReg.Index = 0;
-      inst[ic].SrcReg[0].File = PROGRAM_OUTPUT;
-      inst[ic].SrcReg[0].Index = FRAG_RESULT_COLR;
-      ic++;
-
-      /* DP4 result.color.x, tmp0, matrow0; */
+      /* DP4 temp.x, colorTemp, matrow0; */
       _mesa_init_instructions(inst + ic, 1);
       inst[ic].Opcode = OPCODE_DP4;
-      inst[ic].DstReg.File = PROGRAM_OUTPUT;
-      inst[ic].DstReg.Index = FRAG_RESULT_COLR;
+      inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+      inst[ic].DstReg.Index = temp;
       inst[ic].DstReg.WriteMask = WRITEMASK_X;
       inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
-      inst[ic].SrcReg[0].Index = 0;
+      inst[ic].SrcReg[0].Index = colorTemp;
       inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
       inst[ic].SrcReg[1].Index = row0_p;
       ic++;
 
-      /* DP4 result.color.y, tmp0, matrow1; */
+      /* DP4 temp.y, colorTemp, matrow1; */
       _mesa_init_instructions(inst + ic, 1);
       inst[ic].Opcode = OPCODE_DP4;
-      inst[ic].DstReg.File = PROGRAM_OUTPUT;
-      inst[ic].DstReg.Index = FRAG_RESULT_COLR;
+      inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+      inst[ic].DstReg.Index = temp;
       inst[ic].DstReg.WriteMask = WRITEMASK_Y;
       inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
-      inst[ic].SrcReg[0].Index = 0;
+      inst[ic].SrcReg[0].Index = colorTemp;
       inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
       inst[ic].SrcReg[1].Index = row1_p;
       ic++;
 
-      /* DP4 result.color.z, tmp0, matrow2; */
+      /* DP4 temp.z, colorTemp, matrow2; */
       _mesa_init_instructions(inst + ic, 1);
       inst[ic].Opcode = OPCODE_DP4;
-      inst[ic].DstReg.File = PROGRAM_OUTPUT;
-      inst[ic].DstReg.Index = FRAG_RESULT_COLR;
+      inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+      inst[ic].DstReg.Index = temp;
       inst[ic].DstReg.WriteMask = WRITEMASK_Z;
       inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
-      inst[ic].SrcReg[0].Index = 0;
+      inst[ic].SrcReg[0].Index = colorTemp;
       inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
       inst[ic].SrcReg[1].Index = row2_p;
       ic++;
 
-      /* DP4 result.color.w, tmp0, matrow3; */
+      /* DP4 temp.w, colorTemp, matrow3; */
       _mesa_init_instructions(inst + ic, 1);
       inst[ic].Opcode = OPCODE_DP4;
-      inst[ic].DstReg.File = PROGRAM_OUTPUT;
-      inst[ic].DstReg.Index = FRAG_RESULT_COLR;
+      inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+      inst[ic].DstReg.Index = temp;
       inst[ic].DstReg.WriteMask = WRITEMASK_W;
       inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
-      inst[ic].SrcReg[0].Index = 0;
+      inst[ic].SrcReg[0].Index = colorTemp;
       inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
       inst[ic].SrcReg[1].Index = row3_p;
       ic++;
+
+      /* MOV colorTemp, temp; */
+      _mesa_init_instructions(inst + ic, 1);
+      inst[ic].Opcode = OPCODE_MOV;
+      inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+      inst[ic].DstReg.Index = colorTemp;
+      inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
+      inst[ic].SrcReg[0].Index = temp;
+      ic++;
+   }
+
+   if (key->colorMatrixPostScaleBias) {
+      static const gl_state_index scale_state[STATE_LENGTH] =
+         { STATE_INTERNAL, STATE_PT_SCALE, 0, 0, 0 };
+      static const gl_state_index bias_state[STATE_LENGTH] =
+         { STATE_INTERNAL, STATE_PT_BIAS, 0, 0, 0 };
+      GLint scale_param, bias_param;
+
+      scale_param = _mesa_add_state_reference(params, scale_state);
+      bias_param = _mesa_add_state_reference(params, bias_state);
+
+      _mesa_init_instructions(inst + ic, 1);
+      inst[ic].Opcode = OPCODE_MAD;
+      inst[ic].DstReg.File = PROGRAM_TEMPORARY;
+      inst[ic].DstReg.Index = colorTemp;
+      inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
+      inst[ic].SrcReg[0].Index = colorTemp;
+      inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
+      inst[ic].SrcReg[1].Index = scale_param;
+      inst[ic].SrcReg[2].File = PROGRAM_STATE_VAR;
+      inst[ic].SrcReg[2].Index = bias_param;
+      ic++;
+   }
+
+   /* Modify last instruction's dst reg to write to result.color */
+   {
+      struct prog_instruction *last = &inst[ic - 1];
+      last->DstReg.File = PROGRAM_OUTPUT;
+      last->DstReg.Index = FRAG_RESULT_COLOR;
    }
 
    /* END; */
@@ -268,32 +433,41 @@ get_pixel_transfer_program(GLcontext *ctx, const struct state_key *key)
 
 
 
+/**
+ * Update st->pixel_xfer.program in response to new pixel-transfer state.
+ */
 static void
 update_pixel_transfer(struct st_context *st)
 {
+   GLcontext *ctx = st->ctx;
    struct state_key key;
    struct gl_fragment_program *fp;
 
    make_state_key(st->ctx, &key);
 
    fp = (struct gl_fragment_program *)
-      _mesa_search_program_cache(st->pixel_transfer_cache, &key, sizeof(key));
+      _mesa_search_program_cache(st->pixel_xfer.cache, &key, sizeof(key));
    if (!fp) {
       fp = get_pixel_transfer_program(st->ctx, &key);
-      _mesa_program_cache_insert(st->ctx, st->pixel_transfer_cache,
+      _mesa_program_cache_insert(st->ctx, st->pixel_xfer.cache,
                                  &key, sizeof(key), &fp->Base);
    }
 
-   st->pixel_transfer_program = fp;
+   if (ctx->Pixel.MapColorFlag) {
+      load_color_map_texture(ctx, st->pixel_xfer.pixelmap_texture);
+   }
+   st->pixel_xfer.pixelmap_enabled = ctx->Pixel.MapColorFlag;
+
+   st->pixel_xfer.program = (struct st_fragment_program *) fp;
 }
 
 
 
 const struct st_tracked_state st_update_pixel_transfer = {
-   .name = "st_update_pixel_transfer",
-   .dirty = {
-      .mesa = _NEW_PIXEL | _NEW_COLOR_MATRIX,
-      .st  = 0,
+   "st_update_pixel_transfer",                         /* name */
+   {                                                   /* dirty */
+      _NEW_PIXEL | _NEW_COLOR_MATRIX,                  /* mesa */
+      0,                                               /* st */
    },
-   .update = update_pixel_transfer
+   update_pixel_transfer                               /* update */
 };