Merge branch 'mesa_7_6_branch'
[mesa.git] / src / mesa / state_tracker / st_cb_bufferobjects.c
index 526fc9587344f3fb5d5f5e1e1000d85146f0ae4b..8e09d0b9324152ebb196665c949e314d10d48adc 100644 (file)
  **************************************************************************/
 
 
+/**
+ * Functions for pixel buffer objects and vertex/element buffer objects.
+ */
+
+
 #include "main/imports.h"
 #include "main/mtypes.h"
+#include "main/arrayobj.h"
 #include "main/bufferobj.h"
 
+#include "st_inlines.h"
 #include "st_context.h"
 #include "st_cb_bufferobjects.h"
 
 #include "pipe/p_inlines.h"
 
 
-
-/* Pixel buffers and Vertex/index buffers are handled through these
- * mesa callbacks.  Framebuffer/Renderbuffer objects are
- * created/managed elsewhere.
- */
-
-
-
 /**
  * There is some duplication between mesa's bufferobjects and our
  * bufmgr buffers.  Both have an integer handle and a hashtable to
@@ -74,11 +73,10 @@ st_bufferobj_alloc(GLcontext *ctx, GLuint name, GLenum target)
 static void
 st_bufferobj_free(GLcontext *ctx, struct gl_buffer_object *obj)
 {
-   struct pipe_context *pipe = st_context(ctx)->pipe;
    struct st_buffer_object *st_obj = st_buffer_object(obj);
 
    if (st_obj->buffer) 
-      pipe_buffer_reference(pipe->screen, &st_obj->buffer, NULL);
+      pipe_buffer_reference(&st_obj->buffer, NULL);
 
    _mesa_free(st_obj);
 }
@@ -98,13 +96,15 @@ st_bufferobj_subdata(GLcontext *ctx,
                     GLsizeiptrARB size,
                     const 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);
 
-   if (offset >= st_obj->size || size > (st_obj->size - offset))
-      return;
+   /* we may be called from VBO code, so double-check params here */
+   ASSERT(offset >= 0);
+   ASSERT(size >= 0);
+   ASSERT(offset + size <= obj->Size);
 
-   pipe_buffer_write(pipe->screen, st_obj->buffer, offset, size, data);
+   st_cond_flush_pipe_buffer_write(st_context(ctx), st_obj->buffer,
+                                  offset, size, data);
 }
 
 
@@ -118,13 +118,15 @@ st_bufferobj_get_subdata(GLcontext *ctx,
                          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);
 
-   if (offset >= st_obj->size || size > (st_obj->size - offset))
-      return;
+   /* we may be called from VBO code, so double-check params here */
+   ASSERT(offset >= 0);
+   ASSERT(size >= 0);
+   ASSERT(offset + size <= obj->Size);
 
-   pipe_buffer_read(pipe->screen, st_obj->buffer, offset, size, data);
+   st_cond_flush_pipe_buffer_read(st_context(ctx), st_obj->buffer,
+                                 offset, size, data);
 }
 
 
@@ -132,9 +134,10 @@ st_bufferobj_get_subdata(GLcontext *ctx,
  * 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().
+ * Called via ctx->Driver.BufferData().
+ * \return GL_TRUE for success, GL_FALSE if out of memory
  */
-static void
+static GLboolean
 st_bufferobj_data(GLcontext *ctx,
                  GLenum target,
                  GLsizeiptrARB size,
@@ -165,14 +168,18 @@ st_bufferobj_data(GLcontext *ctx,
       buffer_usage = 0;
    }
 
-   pipe_buffer_reference( pipe->screen, &st_obj->buffer, NULL );
+   pipe_buffer_reference( &st_obj->buffer, NULL );
 
    st_obj->buffer = pipe_buffer_create( pipe->screen, 32, buffer_usage, size );
 
-   st_obj->size = size;
+   if (!st_obj->buffer) {
+      return GL_FALSE;
+   }
 
    if (data)
-      st_bufferobj_subdata(ctx, target, 0, size, data, obj);
+      st_no_flush_pipe_buffer_write(st_context(ctx), st_obj->buffer, 0,
+                                   size, data);
+   return GL_TRUE;
 }
 
 
@@ -183,9 +190,8 @@ static void *
 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);
-   GLuint flags;
+   uint flags;
 
    switch (access) {
    case GL_WRITE_ONLY:
@@ -201,12 +207,17 @@ st_bufferobj_map(GLcontext *ctx, GLenum target, GLenum access,
       break;      
    }
 
-   obj->Pointer = pipe_buffer_map(pipe->screen, st_obj->buffer, flags);
+   obj->Pointer = st_cond_flush_pipe_buffer_map(st_context(ctx),
+                                               st_obj->buffer,
+                                               flags);
+   if (obj->Pointer) {
+      obj->Offset = 0;
+      obj->Length = obj->Size;
+   }
    return obj->Pointer;
 }
 
 
-
 /**
  * Called via glMapBufferRange().
  */
@@ -217,8 +228,7 @@ st_bufferobj_map_range(GLcontext *ctx, GLenum target,
 {
    struct pipe_context *pipe = st_context(ctx)->pipe;
    struct st_buffer_object *st_obj = st_buffer_object(obj);
-   GLuint flags = 0;
-   char *map;
+   uint flags = 0x0;
 
    if (access & GL_MAP_WRITE_BIT)
       flags |= PIPE_BUFFER_USAGE_CPU_WRITE;
@@ -226,21 +236,48 @@ st_bufferobj_map_range(GLcontext *ctx, GLenum target,
    if (access & GL_MAP_READ_BIT)
       flags |= PIPE_BUFFER_USAGE_CPU_READ;
 
+   if (access & GL_MAP_FLUSH_EXPLICIT_BIT)
+      flags |= PIPE_BUFFER_USAGE_FLUSH_EXPLICIT;
+   
    /* ... other flags ...
     */
 
    if (access & MESA_MAP_NOWAIT_BIT)
       flags |= PIPE_BUFFER_USAGE_DONTBLOCK;
 
-   map = pipe_buffer_map_range(pipe->screen, st_obj->buffer, offset, length, flags);
-   /* FIXME: some code expects this to point to the buffer start, which means that
-    * the range might not be respected in all circumstances
-    */
-   obj->Pointer = map ? map - offset : NULL;
-   return map;
+   assert(offset >= 0);
+   assert(length >= 0);
+   assert(offset < obj->Size);
+   assert(offset + length <= obj->Size);
+
+   obj->Pointer = pipe_buffer_map_range(pipe->screen, st_obj->buffer, offset, length, flags);
+   if (obj->Pointer) {
+      obj->Pointer = (ubyte *) obj->Pointer + offset;
+      obj->Offset = offset;
+      obj->Length = length;
+      obj->AccessFlags = access;
+   }
+   
+   return obj->Pointer;
 }
 
 
+static void
+st_bufferobj_flush_mapped_range(GLcontext *ctx, GLenum target, 
+                                GLintptr offset, GLsizeiptr length,
+                                struct gl_buffer_object *obj)
+{
+   struct pipe_context *pipe = st_context(ctx)->pipe;
+   struct st_buffer_object *st_obj = st_buffer_object(obj);
+
+   /* Subrange is relative to mapped range */
+   assert(offset >= 0);
+   assert(length >= 0);
+   assert(offset + length <= obj->Length);
+   
+   pipe_buffer_flush_mapped_range(pipe->screen, st_obj->buffer, 
+                                  obj->Offset + offset, length);
+}
 
 
 /**
@@ -254,10 +291,49 @@ st_bufferobj_unmap(GLcontext *ctx, GLenum target, struct gl_buffer_object *obj)
 
    pipe_buffer_unmap(pipe->screen, st_obj->buffer);
    obj->Pointer = NULL;
+   obj->Offset = 0;
+   obj->Length = 0;
    return GL_TRUE;
 }
 
 
+/**
+ * Called via glCopyBufferSubData().
+ */
+static void
+st_copy_buffer_subdata(GLcontext *ctx,
+                       struct gl_buffer_object *src,
+                       struct gl_buffer_object *dst,
+                       GLintptr readOffset, GLintptr writeOffset,
+                       GLsizeiptr size)
+{
+   struct pipe_context *pipe = st_context(ctx)->pipe;
+   struct st_buffer_object *srcObj = st_buffer_object(src);
+   struct st_buffer_object *dstObj = st_buffer_object(dst);
+   ubyte *srcPtr, *dstPtr;
+
+   /* buffer should not already be mapped */
+   assert(!src->Pointer);
+   assert(!dst->Pointer);
+
+   srcPtr = (ubyte *) pipe_buffer_map_range(pipe->screen,
+                                            srcObj->buffer,
+                                            readOffset, size,
+                                            PIPE_BUFFER_USAGE_CPU_READ);
+
+   dstPtr = (ubyte *) pipe_buffer_map_range(pipe->screen,
+                                            dstObj->buffer,
+                                            writeOffset, size,
+                                            PIPE_BUFFER_USAGE_CPU_WRITE);
+
+   if (srcPtr && dstPtr)
+      _mesa_memcpy(dstPtr + writeOffset, srcPtr + readOffset, size);
+
+   pipe_buffer_unmap(pipe->screen, srcObj->buffer);
+   pipe_buffer_unmap(pipe->screen, dstObj->buffer);
+}
+
+
 void
 st_init_bufferobject_functions(struct dd_function_table *functions)
 {
@@ -268,5 +344,11 @@ st_init_bufferobject_functions(struct dd_function_table *functions)
    functions->GetBufferSubData = st_bufferobj_get_subdata;
    functions->MapBuffer = st_bufferobj_map;
    functions->MapBufferRange = st_bufferobj_map_range;
+   functions->FlushMappedBufferRange = st_bufferobj_flush_mapped_range;
    functions->UnmapBuffer = st_bufferobj_unmap;
+   functions->CopyBufferSubData = st_copy_buffer_subdata;
+
+   /* For GL_APPLE_vertex_array_object */
+   functions->NewArrayObject = _mesa_new_array_object;
+   functions->DeleteArrayObject = _mesa_delete_array_object;
 }