gallium: simplify texture format selection
[mesa.git] / src / mesa / state_tracker / st_cb_bitmap.c
index 0cc910a67fa5b6047a99b08218d9bfea2fdecdbf..45842fe231b3198e86782424bc18830af1e08049 100644 (file)
 
 
 
+/**
+ * glBitmaps are drawn as textured quads.  The user's bitmap pattern
+ * is stored in a texture image.  An alpha8 texture format is used.
+ * The fragment shader samples a bit (texel) from the texture, then
+ * discards the fragment if the bit is off.
+ *
+ * Note that we actually store the inverse image of the bitmap to
+ * simplify the fragment program.  An "on" bit gets stored as texel=0x0
+ * and an "off" bit is stored as texel=0xff.  Then we kill the
+ * fragment if the negated texel value is less than zero.
+ */
+
+
 /**
  * The bitmap cache attempts to accumulate multiple glBitmap calls in a
  * buffer which is then rendered en mass upon a flush, state change, etc.
@@ -92,7 +105,7 @@ struct bitmap_cache
  * This program will be combined with the user's fragment program.
  */
 static struct st_fragment_program *
-make_bitmap_fragment_program(GLcontext *ctx)
+make_bitmap_fragment_program(GLcontext *ctx, GLuint samplerIndex)
 {
    struct st_fragment_program *stfp;
    struct gl_program *p;
@@ -102,7 +115,7 @@ make_bitmap_fragment_program(GLcontext *ctx)
    if (!p)
       return NULL;
 
-   p->NumInstructions = 5;
+   p->NumInstructions = 3;
 
    p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
    if (!p->Instructions) {
@@ -117,37 +130,15 @@ make_bitmap_fragment_program(GLcontext *ctx)
    p->Instructions[ic].DstReg.Index = 0;
    p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
    p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
-   p->Instructions[ic].TexSrcUnit = 0;
+   p->Instructions[ic].TexSrcUnit = samplerIndex;
    p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
    ic++;
 
-   /* SWZ tmp0.x, tmp0.x, 1111; # tmp0.x = 1.0 */
-   p->Instructions[ic].Opcode = OPCODE_SWZ;
-   p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY;
-   p->Instructions[ic].DstReg.Index = 0;
-   p->Instructions[ic].DstReg.WriteMask = WRITEMASK_X;
-   p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
-   p->Instructions[ic].SrcReg[0].Index = 0;
-   p->Instructions[ic].SrcReg[0].Swizzle
-      = MAKE_SWIZZLE4(SWIZZLE_ONE, SWIZZLE_ONE, SWIZZLE_ONE, SWIZZLE_ONE );
-   ic++;
-
-   /* SUB tmp0, tmp0.wwww, tmp0.xxxx;  #  tmp0.w -= 1 */
-   p->Instructions[ic].Opcode = OPCODE_SUB;
-   p->Instructions[ic].DstReg.File = PROGRAM_TEMPORARY;
-   p->Instructions[ic].DstReg.Index = 0;
-   p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
-   p->Instructions[ic].SrcReg[0].Index = 0;
-   p->Instructions[ic].SrcReg[0].Swizzle = SWIZZLE_WWWW;
-   p->Instructions[ic].SrcReg[1].File = PROGRAM_TEMPORARY;
-   p->Instructions[ic].SrcReg[1].Index = 0;
-   p->Instructions[ic].SrcReg[1].Swizzle = SWIZZLE_XXXX; /* 1.0 */
-   ic++;
-
-   /* KIL if tmp0 < 0 */
+   /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
    p->Instructions[ic].Opcode = OPCODE_KIL;
    p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
    p->Instructions[ic].SrcReg[0].Index = 0;
+   p->Instructions[ic].SrcReg[0].NegateBase = NEGATE_XYZW;
    ic++;
 
    /* END; */
@@ -157,6 +148,7 @@ make_bitmap_fragment_program(GLcontext *ctx)
 
    p->InputsRead = FRAG_BIT_TEX0;
    p->OutputsWritten = 0x0;
+   p->SamplersUsed = (1 << samplerIndex);
 
    stfp = (struct st_fragment_program *) p;
    stfp->Base.UsesKill = GL_TRUE;
@@ -166,6 +158,19 @@ make_bitmap_fragment_program(GLcontext *ctx)
 }
 
 
+static int
+find_free_bit(uint bitfield)
+{
+   int i;
+   for (i = 0; i < 32; i++) {
+      if ((bitfield & (1 << i)) == 0) {
+         return i;
+      }
+   }
+   return -1;
+}
+
+
 /**
  * Combine basic bitmap fragment program with the user-defined program.
  */
@@ -173,28 +178,26 @@ static struct st_fragment_program *
 combined_bitmap_fragment_program(GLcontext *ctx)
 {
    struct st_context *st = ctx->st;
-   struct st_fragment_program *stfp;
+   struct st_fragment_program *stfp = st->fp;
 
-   if (!st->bitmap.program) {
-      /* create the basic bitmap fragment program */
-      st->bitmap.program = make_bitmap_fragment_program(ctx);
-   }
-
-   if (st->bitmap.user_prog_sn == st->fp->serialNo) {
-      /* re-use */
-      stfp = st->bitmap.combined_prog;
-   }
-   else {
-      /* Concatenate the bitmap program with the current user-defined program.
+   if (!stfp->bitmap_program) {
+      /*
+       * Generate new program which is the user-defined program prefixed
+       * with the bitmap sampler/kill instructions.
        */
-      stfp = (struct st_fragment_program *)
-         _mesa_combine_programs(ctx,
-                                &st->bitmap.program->Base.Base,
-                                &st->fp->Base.Base);
+      struct st_fragment_program *bitmap_prog;
+      uint sampler;
+
+      sampler = find_free_bit(st->fp->Base.Base.SamplersUsed);
+      bitmap_prog = make_bitmap_fragment_program(ctx, sampler);
 
+      stfp->bitmap_program = (struct st_fragment_program *)
+         _mesa_combine_programs(ctx,
+                                &bitmap_prog->Base.Base, &stfp->Base.Base);
+      stfp->bitmap_program->bitmap_sampler = sampler;
 #if 0
       {
-         struct gl_program *p = &stfp->Base.Base;
+         struct gl_program *p = &stfp->bitmap_program->Base.Base;
          printf("Combined bitmap program:\n");
          _mesa_print_program(p);
          printf("InputsRead: 0x%x\n", p->InputsRead);
@@ -204,11 +207,7 @@ combined_bitmap_fragment_program(GLcontext *ctx)
 #endif
 
       /* translate to TGSI tokens */
-      st_translate_fragment_program(st, stfp, NULL);
-
-      /* save new program, update serial numbers */
-      st->bitmap.user_prog_sn = st->fp->serialNo;
-      st->bitmap.combined_prog = stfp;
+      st_translate_fragment_program(st, stfp->bitmap_program, NULL);
    }
 
    /* Ideally we'd have updated the pipe constants during the normal
@@ -216,7 +215,7 @@ combined_bitmap_fragment_program(GLcontext *ctx)
     */
    st_upload_constants(st, stfp->Base.Base.Parameters, PIPE_SHADER_FRAGMENT);
 
-   return stfp;
+   return stfp->bitmap_program;
 }
 
 
@@ -231,27 +230,10 @@ make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
    struct pipe_context *pipe = ctx->st->pipe;
    struct pipe_screen *screen = pipe->screen;
    struct pipe_surface *surface;
-   uint format = 0, cpp, comp;
    ubyte *dest;
    struct pipe_texture *pt;
    int row, col;
 
-   /* find a texture format we know */
-   if (screen->is_format_supported( screen, PIPE_FORMAT_U_I8, PIPE_TEXTURE )) {
-      format = PIPE_FORMAT_U_I8;
-      cpp = 1;
-      comp = 0;
-   }
-   else if (screen->is_format_supported( screen, PIPE_FORMAT_A8R8G8B8_UNORM, PIPE_TEXTURE )) {
-      format = PIPE_FORMAT_A8R8G8B8_UNORM;
-      cpp = 4;
-      comp = 3; /* alpha channel */ /*XXX little-endian dependency */
-   }
-   else {
-      /* XXX support more formats */
-      assert( 0 );
-   }
-
    /* PBO source... */
    bitmap = _mesa_map_bitmap_pbo(ctx, unpack, bitmap);
    if (!bitmap) {
@@ -261,8 +243,8 @@ make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
    /**
     * Create texture to hold bitmap pattern.
     */
-   pt = st_texture_create(ctx->st, PIPE_TEXTURE_2D, format, 0, width, height,
-                         1, 0);
+   pt = st_texture_create(ctx->st, PIPE_TEXTURE_2D, ctx->st->bitmap.tex_format,
+                          0, width, height, 1, 0);
    if (!pt) {
       _mesa_unmap_bitmap_pbo(ctx, unpack);
       return NULL;
@@ -281,7 +263,7 @@ make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
    for (row = 0; row < height; row++) {
       const GLubyte *src = (const GLubyte *) _mesa_image_address2d(unpack,
                  bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
-      ubyte *destRow = dest + row * surface->pitch * cpp;
+      ubyte *destRow = dest + row * surface->pitch;
 
       if (unpack->LsbFirst) {
          /* Lsb first */
@@ -289,8 +271,7 @@ make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
          for (col = 0; col < width; col++) {
 
             /* set texel to 255 if bit is set */
-            destRow[comp] = (*src & mask) ? 255 : 0;
-            destRow += cpp;
+            destRow[col] = (*src & mask) ? 0x0 : 0xff;
 
             if (mask == 128U) {
                src++;
@@ -311,8 +292,7 @@ make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
          for (col = 0; col < width; col++) {
 
             /* set texel to 255 if bit is set */
-            destRow[comp] =(*src & mask) ? 255 : 0;
-            destRow += cpp;
+            destRow[col] =(*src & mask) ? 0x0 : 0xff;
 
             if (mask == 1U) {
                src++;
@@ -337,8 +317,6 @@ make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
    pipe_surface_reference(&surface, NULL);
    pipe->texture_update(pipe, pt, 0, 0x1);
 
-   pt->format = format;
-
    return pt;
 }
 
@@ -356,11 +334,8 @@ setup_bitmap_vertex_data(struct st_context *st,
    const GLfloat x1 = x + width;
    const GLfloat y0 = y;
    const GLfloat y1 = y + height;
-   const GLfloat bias = st->bitmap_texcoord_bias;
-   const GLfloat xBias = bias / (x1-x0);
-   const GLfloat yBias = bias / (y1-y0);
-   const GLfloat sLeft = 0.0 + xBias, sRight = 1.0 + xBias;
-   const GLfloat tTop = yBias, tBot = 1.0 - tTop - yBias;
+   const GLfloat sLeft = 0.0F, sRight = 1.0F;
+   const GLfloat tTop = 0.0, tBot = 1.0 - tTop;
    const GLfloat clip_x0 = x0 / fb_width * 2.0 - 1.0;
    const GLfloat clip_y0 = y0 / fb_height * 2.0 - 1.0;
    const GLfloat clip_x1 = x1 / fb_width * 2.0 - 1.0;
@@ -444,22 +419,40 @@ draw_bitmap_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
 
    cso_save_rasterizer(cso);
    cso_save_samplers(cso);
+   cso_save_sampler_textures(cso);
    cso_save_viewport(cso);
+   cso_save_fragment_shader(cso);
+   cso_save_vertex_shader(cso);
 
    /* rasterizer state: just scissor */
    st->bitmap.rasterizer.scissor = ctx->Scissor.Enabled;
    cso_set_rasterizer(cso, &st->bitmap.rasterizer);
 
    /* fragment shader state: TEX lookup program */
-   pipe->bind_fs_state(pipe, stfp->driver_shader);
+   cso_set_fragment_shader_handle(cso, stfp->driver_shader);
 
    /* vertex shader state: position + texcoord pass-through */
-   pipe->bind_vs_state(pipe, st->bitmap.vs);
+   cso_set_vertex_shader_handle(cso, st->bitmap.vs);
 
-   /* sampler / texture state */
-   cso_single_sampler(cso, 0, &st->bitmap.sampler);
-   cso_single_sampler_done(cso);
-   pipe->set_sampler_textures(pipe, 1, &pt);
+   /* user samplers, plus our bitmap sampler */
+   {
+      struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
+      uint num = MAX2(stfp->bitmap_sampler + 1, st->state.num_samplers);
+      uint i;
+      for (i = 0; i < st->state.num_samplers; i++) {
+         samplers[i] = &st->state.samplers[i];
+      }
+      samplers[stfp->bitmap_sampler] = &st->bitmap.sampler;
+      cso_set_samplers(cso, num, (const struct pipe_sampler_state **) samplers);   }
+
+   /* user textures, plus the bitmap texture */
+   {
+      struct pipe_texture *textures[PIPE_MAX_SAMPLERS];
+      uint num = MAX2(stfp->bitmap_sampler + 1, st->state.num_textures);
+      memcpy(textures, st->state.sampler_texture, sizeof(textures));
+      textures[stfp->bitmap_sampler] = pt;
+      cso_set_sampler_textures(cso, num, textures);
+   }
 
    /* viewport state: viewport matching window dims */
    {
@@ -493,19 +486,17 @@ draw_bitmap_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
    /* restore state */
    cso_restore_rasterizer(cso);
    cso_restore_samplers(cso);
+   cso_restore_sampler_textures(cso);
    cso_restore_viewport(cso);
-   /* shaders don't go through cso yet */
-   pipe->bind_fs_state(pipe, st->fp->driver_shader);
-   pipe->bind_vs_state(pipe, st->vp->driver_shader);
-   pipe->set_sampler_textures(pipe, ctx->st->state.num_textures,
-                              ctx->st->state.sampler_texture);
+   cso_restore_fragment_shader(cso);
+   cso_restore_vertex_shader(cso);
 }
 
 
 static void
 reset_cache(struct st_context *st)
 {
-   memset(st->bitmap.cache->buffer, 0, sizeof(st->bitmap.cache->buffer));
+   memset(st->bitmap.cache->buffer, 0xff, sizeof(st->bitmap.cache->buffer));
    st->bitmap.cache->empty = GL_TRUE;
 
    st->bitmap.cache->xmin = 1000000;
@@ -518,25 +509,12 @@ reset_cache(struct st_context *st)
 static void
 init_bitmap_cache(struct st_context *st)
 {
-   struct pipe_context *pipe = st->pipe;
-   struct pipe_screen *screen = pipe->screen;
-   enum pipe_format format;
-
    st->bitmap.cache = CALLOC_STRUCT(bitmap_cache);
    if (!st->bitmap.cache)
       return;
 
-   /* find a usable texture format */
-   if (screen->is_format_supported(screen, PIPE_FORMAT_U_I8, PIPE_TEXTURE)) {
-      format = PIPE_FORMAT_U_I8;
-   }
-   else {
-      /* XXX support more formats */
-      assert(0);
-   }
-
    st->bitmap.cache->texture
-      = st_texture_create(st, PIPE_TEXTURE_2D, format, 0,
+      = st_texture_create(st, PIPE_TEXTURE_2D, st->bitmap.tex_format, 0,
                           BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT, 1, 0);
    if (!st->bitmap.cache->texture) {
       FREE(st->bitmap.cache);
@@ -555,36 +533,42 @@ void
 st_flush_bitmap_cache(struct st_context *st)
 {
    if (!st->bitmap.cache->empty) {
-      struct bitmap_cache *cache = st->bitmap.cache;
-      struct pipe_context *pipe = st->pipe;
-      struct pipe_screen *screen = pipe->screen;
-      struct pipe_surface *surf;
-      void *dest;
+      if (st->ctx->DrawBuffer) {
+         struct bitmap_cache *cache = st->bitmap.cache;
+         struct pipe_context *pipe = st->pipe;
+         struct pipe_screen *screen = pipe->screen;
+         struct pipe_surface *surf;
+         void *dest;
+
+         assert(cache->xmin <= cache->xmax);
+         /*
+         printf("flush size %d x %d  at %d, %d\n",
+                cache->xmax - cache->xmin,
+                cache->ymax - cache->ymin,
+                cache->xpos, cache->ypos);
+         */
+
+         /* update the texture map image */
+         surf = screen->get_tex_surface(screen, cache->texture, 0, 0, 0);
+         dest = pipe_surface_map(surf);
+         memcpy(dest, cache->buffer, sizeof(cache->buffer));
+         pipe_surface_unmap(surf);
+         pipe_surface_reference(&surf, NULL);
+
+         /* flush in case the previous texture contents haven't been
+          * used yet. XXX this is not ideal!  Revisit.
+          */
+         st->pipe->flush( st->pipe, 0x0, NULL );
 
-      assert(cache->xmin <= cache->xmax);
-      /*
-      printf("flush size %d x %d  at %d, %d\n",
-             cache->xmax - cache->xmin,
-             cache->ymax - cache->ymin,
-             cache->xpos, cache->ypos);
-      */
-
-      /* update the texture map image */
-      surf = screen->get_tex_surface(screen, cache->texture, 0, 0, 0);
-      dest = pipe_surface_map(surf);
-      memcpy(dest, cache->buffer, sizeof(cache->buffer));
-      pipe_surface_unmap(surf);
-      pipe_surface_reference(&surf, NULL);
-
-      pipe->texture_update(pipe, cache->texture, 0, 0x1);
-
-      draw_bitmap_quad(st->ctx,
-                       cache->xpos,
-                       cache->ypos,
-                       st->ctx->Current.RasterPos[2],
-                       BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
-                       cache->texture);
+         pipe->texture_update(pipe, cache->texture, 0, 0x1);
 
+         draw_bitmap_quad(st->ctx,
+                          cache->xpos,
+                          cache->ypos,
+                          st->ctx->Current.RasterPos[2],
+                          BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
+                          cache->texture);
+      }
       reset_cache(st);
    }
 }
@@ -642,7 +626,7 @@ accum_bitmap(struct st_context *st,
 
    /* XXX try to combine this code with code in make_bitmap_texture() */
 #define SET_PIXEL(COL, ROW) \
-   cache->buffer[py + (ROW)][px + (COL)] = 0xff;
+   cache->buffer[py + (ROW)][px + (COL)] = 0x0;
 
    for (row = 0; row < height; row++) {
       const GLubyte *src = (const GLubyte *) _mesa_image_address2d(unpack,
@@ -750,6 +734,8 @@ void
 st_init_bitmap(struct st_context *st)
 {
    struct pipe_sampler_state *sampler = &st->bitmap.sampler;
+   struct pipe_context *pipe = st->pipe;
+   struct pipe_screen *screen = pipe->screen;
 
    /* init sampler state once */
    memset(sampler, 0, sizeof(*sampler));
@@ -761,8 +747,19 @@ st_init_bitmap(struct st_context *st)
    sampler->mag_img_filter = PIPE_TEX_FILTER_NEAREST;
    sampler->normalized_coords = 1;
 
-   /* init scissor state once */
+   /* init baseline rasterizer state once */
    memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
+   st->bitmap.rasterizer.gl_rasterization_rules = 1;
+   st->bitmap.rasterizer.bypass_vs = 1;
+
+   /* find a usable texture format */
+   if (screen->is_format_supported(screen, PIPE_FORMAT_U_I8, PIPE_TEXTURE)) {
+      st->bitmap.tex_format = PIPE_FORMAT_U_I8;
+   }
+   else {
+      /* XXX support more formats */
+      assert(0);
+   }
 
    init_bitmap_cache(st);
 }
@@ -774,6 +771,7 @@ st_destroy_bitmap(struct st_context *st)
 {
    struct pipe_context *pipe = st->pipe;
 
+#if 0
    if (st->bitmap.combined_prog) {
       st_delete_program(st->ctx, &st->bitmap.combined_prog->Base.Base);
    }
@@ -781,9 +779,9 @@ st_destroy_bitmap(struct st_context *st)
    if (st->bitmap.program) {
       st_delete_program(st->ctx, &st->bitmap.program->Base.Base);
    }
-
+#endif
    if (st->bitmap.vs) {
-      pipe->delete_vs_state(pipe, st->bitmap.vs);
+      cso_delete_vertex_shader(st->cso_context, st->bitmap.vs);
       st->bitmap.vs = NULL;
    }