mesa: refactor: move glPixelStore function into new pixelstore.c file
[mesa.git] / src / mesa / state_tracker / st_cb_bufferobjects.c
index d020eb2007b2698588188c9c2879666cfa4d207a..af79aefa96870bce5d7f80825d6f874b351c26d2 100644 (file)
@@ -1,6 +1,6 @@
 /**************************************************************************
  * 
- * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
  * All Rights Reserved.
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a
  **************************************************************************/
 
 
-#include "imports.h"
-#include "mtypes.h"
-#include "bufferobj.h"
+#include "main/imports.h"
+#include "main/mtypes.h"
+#include "main/bufferobj.h"
 
 #include "st_context.h"
 #include "st_cb_bufferobjects.h"
 
 #include "pipe/p_context.h"
+#include "pipe/p_defines.h"
+#include "pipe/p_inlines.h"
+
+
 
 /* Pixel buffers and Vertex/index buffers are handled through these
  * mesa callbacks.  Framebuffer/Renderbuffer objects are
 static struct gl_buffer_object *
 st_bufferobj_alloc(GLcontext *ctx, GLuint name, GLenum target)
 {
-   struct st_context *st = st_context(ctx);
    struct st_buffer_object *st_obj = CALLOC_STRUCT(st_buffer_object);
 
-   _mesa_initialize_buffer_object(&st_obj->Base, name, target);
+   if (!st_obj)
+      return NULL;
 
-   st_obj->buffer = st->pipe->create_buffer( st->pipe, 32, 0 );
+   _mesa_initialize_buffer_object(&st_obj->Base, name, target);
 
    return &st_obj->Base;
 }
@@ -74,37 +78,13 @@ st_bufferobj_free(GLcontext *ctx, struct gl_buffer_object *obj)
    struct st_buffer_object *st_obj = st_buffer_object(obj);
 
    if (st_obj->buffer) 
-      pipe->buffer_unreference(pipe, &st_obj->buffer);
+      pipe_reference_buffer(pipe, &st_obj->buffer, NULL);
 
-   FREE(st_obj);
+   free(st_obj);
 }
 
 
 
-/**
- * Allocate space for and store data in a buffer object.  Any data that was
- * previously stored in the buffer object is lost.  If data is NULL,
- * memory will be allocated, but no copy will occur.
- * Called via glBufferDataARB().
- */
-static void
-st_bufferobj_data(GLcontext *ctx,
-                 GLenum target,
-                 GLsizeiptrARB size,
-                 const GLvoid * data,
-                 GLenum usage, 
-                 struct gl_buffer_object *obj)
-{
-   struct pipe_context *pipe = st_context(ctx)->pipe;
-   struct st_buffer_object *st_obj = st_buffer_object(obj);
-
-   st_obj->Base.Size = size;
-   st_obj->Base.Usage = usage;
-
-   pipe->buffer_data( pipe, st_obj->buffer, size, data );
-}
-
-
 /**
  * Replace data in a subrange of buffer object.  If the data range
  * specified by size + offset extends beyond the end of the buffer or
@@ -120,8 +100,14 @@ 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;
 
-   pipe->buffer_subdata(pipe, st_obj->buffer, offset, size, data);
+   if (offset >= st_obj->size || size > (st_obj->size - offset))
+      return;
+
+   map = pipe_buffer_map(pipe, st_obj->buffer, PIPE_BUFFER_USAGE_CPU_WRITE);
+   memcpy(map + offset, data, size);
+   pipe_buffer_unmap(pipe, st_obj->buffer);
 }
 
 
@@ -130,26 +116,78 @@ st_bufferobj_subdata(GLcontext *ctx,
  */
 static void
 st_bufferobj_get_subdata(GLcontext *ctx,
-                            GLenum target,
-                            GLintptrARB offset,
-                            GLsizeiptrARB size,
-                            GLvoid * data, struct gl_buffer_object *obj)
+                         GLenum target,
+                         GLintptrARB offset,
+                         GLsizeiptrARB size,
+                         GLvoid * data, struct gl_buffer_object *obj)
 {
    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;
 
-   pipe->buffer_get_subdata(pipe, st_obj->buffer, offset, size, data);
+   map = pipe_buffer_map(pipe, st_obj->buffer, PIPE_BUFFER_USAGE_CPU_READ);
+   memcpy(data, map + offset, size);
+   pipe_buffer_unmap(pipe, st_obj->buffer);
 }
 
 
+/**
+ * Allocate space for and store data in a buffer object.  Any data that was
+ * previously stored in the buffer object is lost.  If data is NULL,
+ * memory will be allocated, but no copy will occur.
+ * Called via glBufferDataARB().
+ */
+static void
+st_bufferobj_data(GLcontext *ctx,
+                 GLenum target,
+                 GLsizeiptrARB size,
+                 const GLvoid * data,
+                 GLenum usage, 
+                 struct gl_buffer_object *obj)
+{
+   struct st_context *st = st_context(ctx);
+   struct pipe_context *pipe = st->pipe;
+   struct st_buffer_object *st_obj = st_buffer_object(obj);
+   unsigned buffer_usage;
+
+   st_obj->Base.Size = size;
+   st_obj->Base.Usage = usage;
+   
+   switch(target) {
+   case GL_PIXEL_PACK_BUFFER_ARB:
+   case GL_PIXEL_UNPACK_BUFFER_ARB:
+      buffer_usage = PIPE_BUFFER_USAGE_PIXEL;
+      break;
+   case GL_ARRAY_BUFFER_ARB:
+      buffer_usage = PIPE_BUFFER_USAGE_VERTEX;
+      break;
+   case GL_ELEMENT_ARRAY_BUFFER_ARB:
+      buffer_usage = PIPE_BUFFER_USAGE_INDEX;
+      break;
+   default:
+      buffer_usage = 0;
+   }
+
+   pipe_reference_buffer( pipe, &st_obj->buffer, NULL );
+
+   st_obj->buffer = pipe_buffer_create( pipe, 32, buffer_usage, size );
+
+   st_obj->size = size;
+
+   if (data)
+      st_bufferobj_subdata(ctx, target, 0, size, data, obj);
+}
+
 
 /**
  * Called via glMapBufferARB().
  */
 static void *
-st_bufferobj_map(GLcontext *ctx,
-                    GLenum target,
-                    GLenum access, struct gl_buffer_object *obj)
+st_bufferobj_map(GLcontext *ctx, GLenum target, GLenum access,
+                 struct gl_buffer_object *obj)
 {
    struct pipe_context *pipe = st_context(ctx)->pipe;
    struct st_buffer_object *st_obj = st_buffer_object(obj);
@@ -157,20 +195,19 @@ st_bufferobj_map(GLcontext *ctx,
 
    switch (access) {
    case GL_WRITE_ONLY:
-      flags = PIPE_BUFFER_FLAG_WRITE;
+      flags = PIPE_BUFFER_USAGE_CPU_WRITE;
       break;
-
-
-      flags = PIPE_BUFFER_FLAG_READ;
+   case GL_READ_ONLY:
+      flags = PIPE_BUFFER_USAGE_CPU_READ;
       break;
-
    case GL_READ_WRITE:
+      /* fall-through */
    default:
-      flags = PIPE_BUFFER_FLAG_READ | PIPE_BUFFER_FLAG_WRITE;
+      flags = PIPE_BUFFER_USAGE_CPU_READ | PIPE_BUFFER_USAGE_CPU_WRITE;
       break;      
    }
 
-   obj->Pointer = pipe->buffer_map(pipe, st_obj->buffer, flags);
+   obj->Pointer = pipe_buffer_map(pipe, st_obj->buffer, flags);
    return obj->Pointer;
 }
 
@@ -179,13 +216,12 @@ st_bufferobj_map(GLcontext *ctx,
  * Called via glMapBufferARB().
  */
 static GLboolean
-st_bufferobj_unmap(GLcontext *ctx,
-                  GLenum target, struct gl_buffer_object *obj)
+st_bufferobj_unmap(GLcontext *ctx, GLenum target, struct gl_buffer_object *obj)
 {
    struct pipe_context *pipe = st_context(ctx)->pipe;
    struct st_buffer_object *st_obj = st_buffer_object(obj);
 
-   pipe->buffer_unmap(pipe, st_obj->buffer);
+   pipe_buffer_unmap(pipe, st_obj->buffer);
    obj->Pointer = NULL;
    return GL_TRUE;
 }