Merge branch '7.8'
[mesa.git] / src / mesa / state_tracker / st_cb_bufferobjects.c
index 7021d732089507f7c37fab3cec4ad743359aaddf..b55a085cc7c7cb936facea486ee55e9584dd8c88 100644 (file)
@@ -42,7 +42,7 @@
 
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
-#include "pipe/p_inlines.h"
+#include "util/u_inlines.h"
 
 
 /**
@@ -75,10 +75,12 @@ st_bufferobj_free(GLcontext *ctx, struct gl_buffer_object *obj)
 {
    struct st_buffer_object *st_obj = st_buffer_object(obj);
 
+   assert(obj->RefCount == 0);
+
    if (st_obj->buffer) 
       pipe_buffer_reference(&st_obj->buffer, NULL);
 
-   _mesa_free(st_obj);
+   free(st_obj);
 }
 
 
@@ -98,7 +100,20 @@ st_bufferobj_subdata(GLcontext *ctx,
 {
    struct st_buffer_object *st_obj = st_buffer_object(obj);
 
-   if (offset >= st_obj->size || size > (st_obj->size - offset))
+   /* we may be called from VBO code, so double-check params here */
+   ASSERT(offset >= 0);
+   ASSERT(size >= 0);
+   ASSERT(offset + size <= obj->Size);
+
+   if (!size)
+      return;
+
+   /*
+    * According to ARB_vertex_buffer_object specification, if data is null,
+    * then the contents of the buffer object's data store is undefined. We just
+    * ignore, and leave it unchanged.
+    */
+   if (!data)
       return;
 
    st_cond_flush_pipe_buffer_write(st_context(ctx), st_obj->buffer,
@@ -118,7 +133,12 @@ st_bufferobj_get_subdata(GLcontext *ctx,
 {
    struct st_buffer_object *st_obj = st_buffer_object(obj);
 
-   if (offset >= st_obj->size || size > (st_obj->size - offset))
+   /* we may be called from VBO code, so double-check params here */
+   ASSERT(offset >= 0);
+   ASSERT(size >= 0);
+   ASSERT(offset + size <= obj->Size);
+
+   if (!size)
       return;
 
    st_cond_flush_pipe_buffer_read(st_context(ctx), st_obj->buffer,
@@ -130,9 +150,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,18 +186,20 @@ st_bufferobj_data(GLcontext *ctx,
 
    pipe_buffer_reference( &st_obj->buffer, NULL );
 
-   st_obj->buffer = pipe_buffer_create( pipe->screen, 32, buffer_usage, size );
+   if (size != 0) {
+      st_obj->buffer = pipe_buffer_create(pipe->screen, 32, buffer_usage, size);
 
-   if (!st_obj->buffer) {
-      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB");
-      return;
-   }
+      if (!st_obj->buffer) {
+         return GL_FALSE;
+      }
 
-   st_obj->size = size;
+      if (data)
+         st_no_flush_pipe_buffer_write(st_context(ctx), st_obj->buffer, 0,
+                                      size, data);
+      return GL_TRUE;
+   }
 
-   if (data)
-      st_no_flush_pipe_buffer_write(st_context(ctx), st_obj->buffer, 0,
-                                   size, data);
+   return GL_TRUE;
 }
 
 
@@ -215,6 +238,13 @@ st_bufferobj_map(GLcontext *ctx, GLenum target, GLenum access,
 }
 
 
+/**
+ * Dummy data whose's pointer is used for zero length ranges.
+ */
+static long
+st_bufferobj_zero_length_range = 0;
+
+
 /**
  * Called via glMapBufferRange().
  */
@@ -226,7 +256,6 @@ 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);
    uint flags = 0x0;
-   char *map;
 
    if (access & GL_MAP_WRITE_BIT)
       flags |= PIPE_BUFFER_USAGE_CPU_WRITE;
@@ -234,6 +263,12 @@ 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;
+   
+   if (access & GL_MAP_UNSYNCHRONIZED_BIT)
+      flags |= PIPE_BUFFER_USAGE_UNSYNCHRONIZED;
+
    /* ... other flags ...
     */
 
@@ -245,14 +280,27 @@ st_bufferobj_map_range(GLcontext *ctx, GLenum target,
    assert(offset < obj->Size);
    assert(offset + length <= obj->Size);
 
-   map = obj->Pointer = pipe_buffer_map_range(pipe->screen, st_obj->buffer, offset, length, flags);
-   if(obj->Pointer) {
+   /*
+    * We go out of way here to hide the degenerate yet valid case of zero
+    * length range from the pipe driver.
+    */
+   if (!length) {
+      obj->Pointer = &st_bufferobj_zero_length_range;
+   }
+   else {
+      obj->Pointer = pipe_buffer_map_range(pipe->screen, st_obj->buffer, offset, length, flags);
+      if (obj->Pointer) {
+         obj->Pointer = (ubyte *) obj->Pointer + offset;
+      }
+   }
+   
+   if (obj->Pointer) {
       obj->Offset = offset;
       obj->Length = length;
-      map += offset;
+      obj->AccessFlags = access;
    }
-   
-   return map;
+
+   return obj->Pointer;
 }
 
 
@@ -269,6 +317,9 @@ st_bufferobj_flush_mapped_range(GLcontext *ctx, GLenum target,
    assert(length >= 0);
    assert(offset + length <= obj->Length);
    
+   if (!length)
+      return;
+
    pipe_buffer_flush_mapped_range(pipe->screen, st_obj->buffer, 
                                   obj->Offset + offset, length);
 }
@@ -283,7 +334,9 @@ 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->screen, st_obj->buffer);
+   if(obj->Length)
+      pipe_buffer_unmap(pipe->screen, st_obj->buffer);
+
    obj->Pointer = NULL;
    obj->Offset = 0;
    obj->Length = 0;
@@ -306,6 +359,9 @@ st_copy_buffer_subdata(GLcontext *ctx,
    struct st_buffer_object *dstObj = st_buffer_object(dst);
    ubyte *srcPtr, *dstPtr;
 
+   if(!size)
+      return;
+
    /* buffer should not already be mapped */
    assert(!src->Pointer);
    assert(!dst->Pointer);
@@ -321,7 +377,7 @@ st_copy_buffer_subdata(GLcontext *ctx,
                                             PIPE_BUFFER_USAGE_CPU_WRITE);
 
    if (srcPtr && dstPtr)
-      _mesa_memcpy(dstPtr + writeOffset, srcPtr + readOffset, size);
+      memcpy(dstPtr + writeOffset, srcPtr + readOffset, size);
 
    pipe_buffer_unmap(pipe->screen, srcObj->buffer);
    pipe_buffer_unmap(pipe->screen, dstObj->buffer);