gallium: consolidate quad drawing code
authorBrian Paul <brian.paul@tungstengraphics.com>
Fri, 25 Apr 2008 20:14:09 +0000 (14:14 -0600)
committerBrian Paul <brian.paul@tungstengraphics.com>
Fri, 25 Apr 2008 20:18:09 +0000 (14:18 -0600)
src/mesa/state_tracker/st_cb_drawpixels.c

index 8d5769f..7c87402 100644 (file)
@@ -402,152 +402,105 @@ make_texture(struct st_context *st,
 
 
 /**
- * Draw textured quad.
+ * Draw quad with texcoords and optional color.
  * Coords are window coords with y=0=bottom.
+ * \param color  may be null
+ * \param invertTex  if true, flip texcoords vertically
  */
 static void
 draw_quad(GLcontext *ctx, GLfloat x0, GLfloat y0, GLfloat z,
-          GLfloat x1, GLfloat y1, GLboolean invertTex)
+          GLfloat x1, GLfloat y1, const GLfloat *color,
+          GLboolean invertTex)
 {
    struct st_context *st = ctx->st;
    struct pipe_context *pipe = ctx->st->pipe;
-   const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
-   const GLfloat fb_width = fb->Width;
-   const GLfloat fb_height = fb->Height;
-   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;
-   const GLfloat clip_y1 = y1 / fb_height * 2.0 - 1.0;
-   const GLfloat sLeft = 0.0, sRight = 1.0;
-   const GLfloat tTop = invertTex, tBot = 1.0 - tTop;
-   GLfloat verts[4][2][4]; /* four verts, two attribs, XYZW */
-   struct pipe_buffer *buf;
-   ubyte *map;
-   GLuint i;
-
-   /* upper-left */
-   verts[0][0][0] = clip_x0;  /* attr[0].x */
-   verts[0][0][1] = clip_y0;  /* attr[0].y */
-   verts[0][1][0] = sLeft;    /* attr[1].s */
-   verts[0][1][1] = tTop;     /* attr[1].t */
-
-   /* upper-right */
-   verts[1][0][0] = clip_x1;
-   verts[1][0][1] = clip_y0;
-   verts[1][1][0] = sRight;
-   verts[1][1][1] = tTop;
-
-   /* lower-right */
-   verts[2][0][0] = clip_x1;
-   verts[2][0][1] = clip_y1;
-   verts[2][1][0] = sRight;
-   verts[2][1][1] = tBot;
-
-   /* lower-left */
-   verts[3][0][0] = clip_x0;
-   verts[3][0][1] = clip_y1;
-   verts[3][1][0] = sLeft;
-   verts[3][1][1] = tBot;
-
-   /* same for all verts: */
-   for (i = 0; i < 4; i++) {
-      verts[i][0][2] = z;   /*Z*/
-      verts[i][0][3] = 1.0; /*W*/
-      verts[i][1][2] = 0.0; /*R*/
-      verts[i][1][3] = 1.0; /*Q*/
-   }
-
-   buf  = pipe->winsys->buffer_create(pipe->winsys, 32,
-                                      PIPE_BUFFER_USAGE_VERTEX,
-                                      sizeof(verts));
-   /* put vertex data into buffer */
-   map = pipe->winsys->buffer_map(pipe->winsys, buf,
-                                  PIPE_BUFFER_USAGE_CPU_WRITE);
-   memcpy(map, verts, sizeof(verts));
-   pipe->winsys->buffer_unmap(pipe->winsys, buf);
-
-   util_draw_vertex_buffer(pipe, buf,
-                           PIPE_PRIM_QUADS,
-                           4,  /* verts */
-                           2); /* attribs/vert */
-
-   pipe->winsys->buffer_destroy(pipe->winsys, buf);
-}
-
-
-static void
-draw_quad_colored(GLcontext *ctx, GLfloat x0, GLfloat y0, GLfloat z,
-                  GLfloat x1, GLfloat y1, const GLfloat *color,
-                  GLboolean invertTex)
-{
-   struct st_context *st = ctx->st;
-   struct pipe_context *pipe = ctx->st->pipe;
-   const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
-   const GLfloat fb_width = fb->Width;
-   const GLfloat fb_height = fb->Height;
-   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;
-   const GLfloat clip_y1 = y1 / fb_height * 2.0 - 1.0;
-   GLfloat bias = ctx->st->bitmap_texcoord_bias;
    GLfloat verts[4][3][4]; /* four verts, three attribs, XYZW */
-   GLuint i;
-   GLfloat xBias = bias / (x1-x0);
-   GLfloat yBias = bias / (y1-y0);
-   GLfloat sLeft = 0.0 + xBias, sRight = 1.0 + xBias;
-   GLfloat tTop = invertTex - yBias, tBot = 1.0 - tTop - yBias;
-   struct pipe_buffer *buf;
-   ubyte *map;
-
-   /* upper-left */
-   verts[0][0][0] = clip_x0;    /* attr[0].x */
-   verts[0][0][1] = clip_y0;    /* attr[0].y */
-   verts[0][2][0] = sLeft; /* attr[2].s */
-   verts[0][2][1] = tTop;  /* attr[2].t */
-
-   /* upper-right */
-   verts[1][0][0] = clip_x1;
-   verts[1][0][1] = clip_y0;
-   verts[1][2][0] = sRight;
-   verts[1][2][1] = tTop;
-
-   /* lower-right */
-   verts[2][0][0] = clip_x1;
-   verts[2][0][1] = clip_y1;
-   verts[2][2][0] = sRight;
-   verts[2][2][1] = tBot;
-
-   /* lower-left */
-   verts[3][0][0] = clip_x0;
-   verts[3][0][1] = clip_y1;
-   verts[3][2][0] = sLeft;
-   verts[3][2][1] = tBot;
-
-   /* same for all verts: */
-   for (i = 0; i < 4; i++) {
-      verts[i][0][2] = z;   /*Z*/
-      verts[i][0][3] = 1.0; /*W*/
-      verts[i][1][0] = color[0];
-      verts[i][1][1] = color[1];
-      verts[i][1][2] = color[2];
-      verts[i][1][3] = color[3];
-      verts[i][2][2] = 0.0; /*R*/
-      verts[i][2][3] = 1.0; /*Q*/
+
+   /* setup vertex data */
+   {
+      const struct gl_framebuffer *fb = st->ctx->DrawBuffer;
+      const GLfloat fb_width = fb->Width;
+      const GLfloat fb_height = fb->Height;
+      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;
+      const GLfloat clip_y1 = y1 / fb_height * 2.0 - 1.0;
+      GLfloat bias = ctx->st->bitmap_texcoord_bias;
+      GLfloat xBias = 0*bias / (x1-x0);
+      GLfloat yBias = 0*bias / (y1-y0);
+      GLfloat sLeft = 0.0 + xBias, sRight = 1.0 + xBias;
+      GLfloat tTop = invertTex - yBias, tBot = 1.0 - tTop - yBias;
+      GLuint tex, i;
+
+      /* upper-left */
+      verts[0][0][0] = clip_x0;    /* v[0].attr[0].x */
+      verts[0][0][1] = clip_y0;    /* v[0].attr[0].y */
+
+      /* upper-right */
+      verts[1][0][0] = clip_x1;
+      verts[1][0][1] = clip_y0;
+
+      /* lower-right */
+      verts[2][0][0] = clip_x1;
+      verts[2][0][1] = clip_y1;
+
+      /* lower-left */
+      verts[3][0][0] = clip_x0;
+      verts[3][0][1] = clip_y1;
+
+      tex = color ? 2 : 1;
+      verts[0][tex][0] = sLeft; /* v[0].attr[tex].s */
+      verts[0][tex][1] = tTop;  /* v[0].attr[tex].t */
+      verts[1][tex][0] = sRight;
+      verts[1][tex][1] = tTop;
+      verts[2][tex][0] = sRight;
+      verts[2][tex][1] = tBot;
+      verts[3][tex][0] = sLeft;
+      verts[3][tex][1] = tBot;
+
+      /* same for all verts: */
+      if (color) {
+         for (i = 0; i < 4; i++) {
+            verts[i][0][2] = z;   /*Z*/
+            verts[i][0][3] = 1.0; /*W*/
+            verts[i][1][0] = color[0];
+            verts[i][1][1] = color[1];
+            verts[i][1][2] = color[2];
+            verts[i][1][3] = color[3];
+            verts[i][2][2] = 0.0; /*R*/
+            verts[i][2][3] = 1.0; /*Q*/
+         }
+      }
+      else {
+         for (i = 0; i < 4; i++) {
+            verts[i][0][2] = z;   /*Z*/
+            verts[i][0][3] = 1.0; /*W*/
+            verts[i][1][2] = 0.0; /*R*/
+            verts[i][1][3] = 1.0; /*Q*/
+         }
+      }
    }
 
-   buf  = pipe->winsys->buffer_create(pipe->winsys, 32,
-                                      PIPE_BUFFER_USAGE_VERTEX,
-                                      sizeof(verts));
-   /* put vertex data into buffer */
-   map = pipe->winsys->buffer_map(pipe->winsys, buf,
-                                  PIPE_BUFFER_USAGE_CPU_WRITE);
-   memcpy(map, verts, sizeof(verts));
-   pipe->winsys->buffer_unmap(pipe->winsys, buf);
-
-   util_draw_vertex_buffer(pipe, buf,
-                           PIPE_PRIM_QUADS,
-                           4,  /* verts */
-                           3); /* attribs/vert */
+   {
+      struct pipe_buffer *buf;
+      ubyte *map;
+
+      /* allocate/load buffer object with vertex data */
+      buf = pipe->winsys->buffer_create(pipe->winsys, 32,
+                                        PIPE_BUFFER_USAGE_VERTEX,
+                                         sizeof(verts));
+      map = pipe->winsys->buffer_map(pipe->winsys, buf,
+                                     PIPE_BUFFER_USAGE_CPU_WRITE);
+      memcpy(map, verts, sizeof(verts));
+      pipe->winsys->buffer_unmap(pipe->winsys, buf);
+
+      util_draw_vertex_buffer(pipe, buf,
+                              PIPE_PRIM_QUADS,
+                              4,  /* verts */
+                              3); /* attribs/vert */
+
+      pipe->winsys->buffer_destroy(pipe->winsys, buf);
+   }
 }
 
 
@@ -653,12 +606,7 @@ draw_textured_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
    x1 = x + width * ctx->Pixel.ZoomX;
    y0 = y;
    y1 = y + height * ctx->Pixel.ZoomY;
-
-   /* draw textured quad */
-   if (color)
-      draw_quad_colored(ctx, x0, y0, z, x1, y1, color, invertTex);
-   else
-      draw_quad(ctx, x0, y0, z, x1, y1, invertTex);
+   draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex);
 
    /* restore state */
    cso_restore_rasterizer(cso);