st/mesa: Associate a sampler view with an st texture object.
[mesa.git] / src / mesa / state_tracker / st_cb_bitmap.c
index 902fb38d1a7ed96e101114b34456b10c723c9794..25d33b933e3f1b76c77a65edae93ee04069b0277 100644 (file)
@@ -34,9 +34,7 @@
 #include "main/image.h"
 #include "main/bufferobj.h"
 #include "main/macros.h"
-#include "main/texformat.h"
 #include "shader/program.h"
-#include "shader/prog_parameter.h"
 #include "shader/prog_print.h"
 
 #include "st_context.h"
 #include "st_atom_constbuf.h"
 #include "st_program.h"
 #include "st_cb_bitmap.h"
-#include "st_cb_program.h"
-#include "st_mesa_to_tgsi.h"
 #include "st_texture.h"
 #include "st_inlines.h"
 
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
-#include "pipe/p_inlines.h"
-#include "util/u_tile.h"
+#include "util/u_inlines.h"
 #include "util/u_draw_quad.h"
 #include "util/u_simple_shaders.h"
 #include "shader/prog_instruction.h"
@@ -169,11 +164,6 @@ make_bitmap_fragment_program(GLcontext *ctx, GLuint samplerIndex)
    stfp = (struct st_fragment_program *) p;
    stfp->Base.UsesKill = GL_TRUE;
 
-   /* No need to send this incomplete program down to hardware:
-    *
-    * st_translate_fragment_program(ctx->st, stfp, NULL);
-    */
-
    return stfp;
 }
 
@@ -231,7 +221,7 @@ combined_bitmap_fragment_program(GLcontext *ctx)
 #endif
 
       /* translate to TGSI tokens */
-      st_translate_fragment_program(st, stfp->bitmap_program, NULL);
+      st_translate_fragment_program(st, stfp->bitmap_program);
    }
 
    return stfp->bitmap_program;
@@ -330,7 +320,18 @@ setup_bitmap_vertex_data(struct st_context *st,
    const GLfloat clip_y0 = (GLfloat)(y0 / fb_height * 2.0 - 1.0);
    const GLfloat clip_x1 = (GLfloat)(x1 / fb_width * 2.0 - 1.0);
    const GLfloat clip_y1 = (GLfloat)(y1 / fb_height * 2.0 - 1.0);
-   const GLuint max_slots = 4096 / sizeof(st->bitmap.vertices);
+
+   /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
+    * no_flush) updates to buffers where we know there is no conflict
+    * with previous data.  Currently using max_slots > 1 will cause
+    * synchronous rendering if the driver flushes its command buffers
+    * between one bitmap and the next.  Our flush hook below isn't
+    * sufficient to catch this as the driver doesn't tell us when it
+    * flushes its own command buffers.  Until this gets fixed, pay the
+    * price of allocating a new buffer for each bitmap cache-flush to
+    * avoid synchronous rendering.
+    */
+   const GLuint max_slots = 1; /* 4096 / sizeof(st->bitmap.vertices); */
    GLuint i;
 
    if (st->bitmap.vbuf_slot >= max_slots) {
@@ -380,11 +381,11 @@ setup_bitmap_vertex_data(struct st_context *st,
    }
 
    /* put vertex data into vbuf */
-   st_no_flush_pipe_buffer_write(st,
-                                st->bitmap.vbuf,
-                                st->bitmap.vbuf_slot * sizeof st->bitmap.vertices,
-                                sizeof st->bitmap.vertices,
-                                st->bitmap.vertices);
+   st_no_flush_pipe_buffer_write_nooverlap(st,
+                                           st->bitmap.vbuf,
+                                           st->bitmap.vbuf_slot * sizeof st->bitmap.vertices,
+                                           sizeof st->bitmap.vertices,
+                                           st->bitmap.vertices);
 
    return st->bitmap.vbuf_slot++ * sizeof st->bitmap.vertices;
 }
@@ -397,7 +398,7 @@ setup_bitmap_vertex_data(struct st_context *st,
 static void
 draw_bitmap_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
                  GLsizei width, GLsizei height,
-                 struct pipe_texture *pt,
+                 struct pipe_sampler_view *sv,
                  const GLfloat *color)
 {
    struct st_context *st = ctx->st;
@@ -435,10 +436,11 @@ 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_fragment_sampler_views(cso);
    cso_save_viewport(cso);
    cso_save_fragment_shader(cso);
    cso_save_vertex_shader(cso);
+   cso_save_vertex_elements(cso);
 
    /* rasterizer state: just scissor */
    st->bitmap.rasterizer.scissor = ctx->Scissor.Enabled;
@@ -464,11 +466,11 @@ draw_bitmap_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
 
    /* user textures, plus the bitmap texture */
    {
-      struct pipe_texture *textures[PIPE_MAX_SAMPLERS];
+      struct pipe_sampler_view *sampler_views[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);
+      memcpy(sampler_views, st->state.sampler_views, sizeof(sampler_views));
+      sampler_views[stfp->bitmap_sampler] = sv;
+      cso_set_fragment_sampler_views(cso, num, sampler_views);
    }
 
    /* viewport state: viewport matching window dims */
@@ -480,15 +482,20 @@ draw_bitmap_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
       struct pipe_viewport_state vp;
       vp.scale[0] =  0.5f * width;
       vp.scale[1] = height * (invert ? -0.5f : 0.5f);
-      vp.scale[2] = 1.0f;
+      vp.scale[2] = 0.5f;
       vp.scale[3] = 1.0f;
       vp.translate[0] = 0.5f * width;
       vp.translate[1] = 0.5f * height;
-      vp.translate[2] = 0.0f;
+      vp.translate[2] = 0.5f;
       vp.translate[3] = 0.0f;
       cso_set_viewport(cso, &vp);
    }
 
+   cso_set_vertex_elements(cso, 3, st->velems_util_draw);
+
+   /* convert Z from [0,1] to [-1,-1] to match viewport Z scale/bias */
+   z = z * 2.0 - 1.0;
+
    /* draw textured quad */
    offset = setup_bitmap_vertex_data(st, x, y, width, height, z, color);
 
@@ -501,10 +508,11 @@ 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_fragment_sampler_views(cso);
    cso_restore_viewport(cso);
    cso_restore_fragment_shader(cso);
    cso_restore_vertex_shader(cso);
+   cso_restore_vertex_elements(cso);
 }
 
 
@@ -515,7 +523,7 @@ reset_cache(struct st_context *st)
    struct pipe_screen *screen = pipe->screen;
    struct bitmap_cache *cache = st->bitmap.cache;
 
-   //memset(cache->buffer, 0xff, sizeof(cache->buffer));
+   /*memset(cache->buffer, 0xff, sizeof(cache->buffer));*/
    cache->empty = GL_TRUE;
 
    cache->xmin = 1000000;
@@ -535,9 +543,29 @@ reset_cache(struct st_context *st)
                                       st->bitmap.tex_format, 0,
                                       BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
                                       1, PIPE_TEXTURE_USAGE_SAMPLER);
+}
+
 
+/** Print bitmap image to stdout (debug) */
+static void
+print_cache(const struct bitmap_cache *cache)
+{
+   int i, j, k;
+
+   for (i = 0; i < BITMAP_CACHE_HEIGHT; i++) {
+      k = BITMAP_CACHE_WIDTH * (BITMAP_CACHE_HEIGHT - i - 1);
+      for (j = 0; j < BITMAP_CACHE_WIDTH; j++) {
+         if (cache->buffer[k])
+            printf("X");
+         else
+            printf(" ");
+         k++;
+      }
+      printf("\n");
+   }
 }
 
+
 static void
 create_cache_trans(struct st_context *st)
 {
@@ -574,6 +602,7 @@ st_flush_bitmap_cache(struct st_context *st)
       if (st->ctx->DrawBuffer) {
          struct pipe_context *pipe = st->pipe;
          struct pipe_screen *screen = pipe->screen;
+         struct pipe_sampler_view *sv;
 
          assert(cache->xmin <= cache->xmax);
  
@@ -587,6 +616,8 @@ st_flush_bitmap_cache(struct st_context *st)
           * So unmap and release the texture transfer before drawing.
           */
          if (cache->trans) {
+            if (0)
+               print_cache(cache);
             screen->transfer_unmap(screen, cache->trans);
             cache->buffer = NULL;
 
@@ -594,13 +625,18 @@ st_flush_bitmap_cache(struct st_context *st)
             cache->trans = NULL;
          }
 
-         draw_bitmap_quad(st->ctx,
-                          cache->xpos,
-                          cache->ypos,
-                          cache->zpos,
-                          BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
-                          cache->texture,
-                          cache->color);
+         sv = st_sampler_view_from_texture(st->pipe, cache->texture);
+         if (sv) {
+            draw_bitmap_quad(st->ctx,
+                             cache->xpos,
+                             cache->ypos,
+                             cache->zpos,
+                             BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
+                             sv,
+                             cache->color);
+
+            pipe_sampler_view_reference(&sv, NULL);
+         }
       }
 
       /* release/free the texture */
@@ -636,7 +672,7 @@ accum_bitmap(struct st_context *st,
              const GLubyte *bitmap )
 {
    struct bitmap_cache *cache = st->bitmap.cache;
-   int px = -999, py;
+   int px = -999, py = -999;
    const GLfloat z = st->ctx->Current.RasterPos[2];
 
    if (width > BITMAP_CACHE_WIDTH ||
@@ -670,6 +706,7 @@ accum_bitmap(struct st_context *st,
    }
 
    assert(px != -999);
+   assert(py != -999);
 
    if (x < cache->xmin)
       cache->xmin = x;
@@ -722,10 +759,18 @@ st_Bitmap(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
 
    pt = make_bitmap_texture(ctx, width, height, unpack, bitmap);
    if (pt) {
+      struct pipe_sampler_view *sv = st_sampler_view_from_texture(st->pipe, pt);
+
       assert(pt->target == PIPE_TEXTURE_2D);
-      draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
-                       width, height, pt,
-                       st->ctx->Current.RasterColor);
+
+      if (sv) {
+         draw_bitmap_quad(ctx, x, y, ctx->Current.RasterPos[2],
+                          width, height, sv,
+                          st->ctx->Current.RasterColor);
+
+         pipe_sampler_view_reference(&sv, NULL);
+      }
+
       /* release/free the texture */
       pipe_texture_reference(&pt, NULL);
    }
@@ -813,7 +858,7 @@ st_destroy_bitmap(struct st_context *st)
          screen->tex_transfer_destroy(cache->trans);
       }
       pipe_texture_reference(&st->bitmap.cache->texture, NULL);
-      _mesa_free(st->bitmap.cache);
+      free(st->bitmap.cache);
       st->bitmap.cache = NULL;
    }
 }