gallium: Add pipe_buffer_write/read inlines.
authorJosé Fonseca <jfonseca@vmware.com>
Tue, 24 Feb 2009 11:30:25 +0000 (11:30 +0000)
committerJosé Fonseca <jfonseca@vmware.com>
Tue, 24 Feb 2009 11:30:25 +0000 (11:30 +0000)
Saves code, and will simplify future interface changes.

src/gallium/auxiliary/util/u_blit.c
src/gallium/auxiliary/util/u_gen_mipmap.c
src/gallium/include/pipe/p_inlines.h
src/mesa/state_tracker/st_atom_constbuf.c
src/mesa/state_tracker/st_cb_bitmap.c
src/mesa/state_tracker/st_cb_bufferobjects.c
src/mesa/state_tracker/st_cb_clear.c
src/mesa/state_tracker/st_cb_drawpixels.c

index efc3a874cc2b0e971a50c3b0a0d37e477b1cea53..4cc720269dd072a687256151e1de6a488c0daae4 100644 (file)
@@ -199,7 +199,6 @@ static unsigned
 setup_vertex_data(struct blit_state *ctx,
                   float x0, float y0, float x1, float y1, float z)
 {
-   void *buf;
    unsigned offset;
 
    ctx->vertices[0][0][0] = x0;
@@ -228,12 +227,8 @@ setup_vertex_data(struct blit_state *ctx,
 
    offset = get_next_slot( ctx );
 
-   buf = pipe_buffer_map(ctx->pipe->screen, ctx->vbuf,
-                         PIPE_BUFFER_USAGE_CPU_WRITE);
-
-   memcpy((char *)buf + offset, ctx->vertices, sizeof(ctx->vertices));
-
-   pipe_buffer_unmap(ctx->pipe->screen, ctx->vbuf);
+   pipe_buffer_write(ctx->pipe->screen, ctx->vbuf,
+                     offset, sizeof(ctx->vertices), ctx->vertices);
 
    return offset;
 }
@@ -249,7 +244,6 @@ setup_vertex_data_tex(struct blit_state *ctx,
                       float s0, float t0, float s1, float t1,
                       float z)
 {
-   void *buf;
    unsigned offset;
 
    ctx->vertices[0][0][0] = x0;
@@ -278,12 +272,8 @@ setup_vertex_data_tex(struct blit_state *ctx,
 
    offset = get_next_slot( ctx );
 
-   buf = pipe_buffer_map(ctx->pipe->screen, ctx->vbuf,
-                         PIPE_BUFFER_USAGE_CPU_WRITE);
-
-   memcpy((char *)buf + offset, ctx->vertices, sizeof(ctx->vertices));
-
-   pipe_buffer_unmap(ctx->pipe->screen, ctx->vbuf);
+   pipe_buffer_write(ctx->pipe->screen, ctx->vbuf,
+                     offset, sizeof(ctx->vertices), ctx->vertices);
 
    return offset;
 }
index 90483fcb216a1fb2680b92d3e635361bb221ba68..1857a71dd82b0746f153c6f51de8b2e88bf3d619 100644 (file)
@@ -1368,7 +1368,6 @@ get_next_slot(struct gen_mipmap_state *ctx)
 static unsigned
 set_vertex_data(struct gen_mipmap_state *ctx, float width, float height)
 {
-   void *buf;
    unsigned offset;
 
    ctx->vertices[0][0][0] = 0.0f; /*x*/
@@ -1393,12 +1392,8 @@ set_vertex_data(struct gen_mipmap_state *ctx, float width, float height)
 
    offset = get_next_slot( ctx );
 
-   buf = pipe_buffer_map(ctx->pipe->screen, ctx->vbuf,
-                         PIPE_BUFFER_USAGE_CPU_WRITE);
-
-   memcpy((char *)buf + offset, ctx->vertices, sizeof(ctx->vertices));
-
-   pipe_buffer_unmap(ctx->pipe->screen, ctx->vbuf);
+   pipe_buffer_write(ctx->pipe->screen, ctx->vbuf,
+                     offset, sizeof(ctx->vertices), ctx->vertices);
 
    return offset;
 }
index ffbe2d7612a4037621321c66942d88be14a3c00a..4eb928d882f53079b521e01782b5a00e11015b4f 100644 (file)
@@ -161,6 +161,44 @@ pipe_buffer_unmap(struct pipe_screen *screen,
    screen->buffer_unmap(screen, buf);
 }
 
+static INLINE void
+pipe_buffer_write(struct pipe_screen *screen,
+                  struct pipe_buffer *buf,
+                  unsigned offset, unsigned size,
+                  const void *data)
+{
+   uint8_t *map;
+   
+   assert(offset < buf->size);
+   assert(offset + size <= buf->size);
+   
+   map = pipe_buffer_map(screen, buf, PIPE_BUFFER_USAGE_CPU_WRITE);
+   assert(map);
+   if(map) {
+      memcpy(map + offset, data, size);
+      pipe_buffer_unmap(screen, buf);
+   }
+}
+
+static INLINE void
+pipe_buffer_read(struct pipe_screen *screen,
+                 struct pipe_buffer *buf,
+                 unsigned offset, unsigned size,
+                 void *data)
+{
+   uint8_t *map;
+   
+   assert(offset < buf->size);
+   assert(offset + size <= buf->size);
+   
+   map = pipe_buffer_map(screen, buf, PIPE_BUFFER_USAGE_CPU_READ);
+   assert(map);
+   if(map) {
+      memcpy(data, map + offset, size);
+      pipe_buffer_unmap(screen, buf);
+   }
+}
+
 /* XXX: thread safety issues!
  */
 static INLINE void
index 514b10cd022af4c0e7e84e96296f7b05287dd315..2df6fef210e0801f19f2c22971751891b41c0cec 100644 (file)
@@ -85,12 +85,10 @@ void st_upload_constants( struct st_context *st,
       }
 
       /* load Mesa constants into the constant buffer */
-      if (cbuf->buffer) {
-         void *map = pipe_buffer_map(pipe->screen, cbuf->buffer,
-                                     PIPE_BUFFER_USAGE_CPU_WRITE);
-         memcpy(map, params->ParameterValues, paramBytes);
-         pipe_buffer_unmap(pipe->screen, cbuf->buffer);
-      }
+      if (cbuf->buffer)
+         pipe_buffer_write(pipe->screen, cbuf->buffer, 
+                           0, paramBytes, 
+                           params->ParameterValues);
 
       st->pipe->set_constant_buffer(st->pipe, id, 0, cbuf);
    }
index ef44a08859cf0b086cc1a3cd14cd97ba752a0a80..66d7cbf8e6a48e5130f77d03e4fa8f29120d7e2e 100644 (file)
@@ -417,17 +417,11 @@ setup_bitmap_vertex_data(struct st_context *st,
    }
 
    /* put vertex data into vbuf */
-   {
-      char *buf = pipe_buffer_map(pipe->screen, 
-                                  st->bitmap.vbuf, 
-                                  PIPE_BUFFER_USAGE_CPU_WRITE);
-
-      memcpy(buf + st->bitmap.vbuf_slot * sizeof st->bitmap.vertices, 
-             st->bitmap.vertices, 
-             sizeof st->bitmap.vertices);
-
-      pipe_buffer_unmap(pipe->screen, st->bitmap.vbuf);
-   }
+   pipe_buffer_write(pipe->screen, 
+                     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;
 }
index aba1cda9323534e984d8f10d641657a446ba2e97..562ac6c65c5b3c7094bf9c703be17dd44a18a385 100644 (file)
@@ -100,14 +100,11 @@ st_bufferobj_subdata(GLcontext *ctx,
 {
    struct pipe_context *pipe = st_context(ctx)->pipe;
    struct st_buffer_object *st_obj = st_buffer_object(obj);
-   char *map;
 
    if (offset >= st_obj->size || size > (st_obj->size - offset))
       return;
 
-   map = pipe_buffer_map(pipe->screen, st_obj->buffer, PIPE_BUFFER_USAGE_CPU_WRITE);
-   memcpy(map + offset, data, size);
-   pipe_buffer_unmap(pipe->screen, st_obj->buffer);
+   pipe_buffer_write(pipe->screen, st_obj->buffer, offset, size, data);
 }
 
 
@@ -123,14 +120,11 @@ st_bufferobj_get_subdata(GLcontext *ctx,
 {
    struct pipe_context *pipe = st_context(ctx)->pipe;
    struct st_buffer_object *st_obj = st_buffer_object(obj);
-   char *map;
 
    if (offset >= st_obj->size || size > (st_obj->size - offset))
       return;
 
-   map = pipe_buffer_map(pipe->screen, st_obj->buffer, PIPE_BUFFER_USAGE_CPU_READ);
-   memcpy(data, map + offset, size);
-   pipe_buffer_unmap(pipe->screen, st_obj->buffer);
+   pipe_buffer_read(pipe->screen, st_obj->buffer, offset, size, data);
 }
 
 
index 20eaaa4ae73035ee9bc5ac7e42e3e4bb4e2fddb0..c6fc7cec27c87cea16caf8b098f9e041fd810c06 100644 (file)
@@ -150,7 +150,6 @@ draw_quad(GLcontext *ctx,
    struct pipe_context *pipe = st->pipe;
    const GLuint max_slots = 1024 / sizeof(st->clear.vertices);
    GLuint i;
-   void *buf;
 
    if (st->clear.vbuf_slot >= max_slots) {
       pipe_buffer_reference(pipe->screen, &st->clear.vbuf, NULL);
@@ -186,13 +185,10 @@ draw_quad(GLcontext *ctx,
    }
 
    /* put vertex data into vbuf */
-   buf = pipe_buffer_map(pipe->screen, st->clear.vbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
-
-   memcpy((char *)buf + st->clear.vbuf_slot * sizeof(st->clear.vertices), 
-          st->clear.vertices, 
-          sizeof(st->clear.vertices));
-
-   pipe_buffer_unmap(pipe->screen, st->clear.vbuf);
+   pipe_buffer_write(pipe->screen, st->clear.vbuf, 
+                     st->clear.vbuf_slot * sizeof(st->clear.vertices),
+                     sizeof(st->clear.vertices),
+                     st->clear.vertices);
 
    /* draw */
    util_draw_vertex_buffer(pipe, 
index 13307fc2c12b39958c2688f8708991bbc3d6878c..c73d0080cac7b568e459b88ccb7d638b901b6e5f 100644 (file)
@@ -485,14 +485,11 @@ draw_quad(GLcontext *ctx, GLfloat x0, GLfloat y0, GLfloat z,
 
    {
       struct pipe_buffer *buf;
-      ubyte *map;
 
       /* allocate/load buffer object with vertex data */
       buf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
                                sizeof(verts));
-      map = pipe_buffer_map(pipe->screen, buf, PIPE_BUFFER_USAGE_CPU_WRITE);
-      memcpy(map, verts, sizeof(verts));
-      pipe_buffer_unmap(pipe->screen, buf);
+      pipe_buffer_write(pipe->screen, buf, 0, sizeof(verts), verts);
 
       util_draw_vertex_buffer(pipe, buf, 0,
                               PIPE_PRIM_QUADS,