mesa: handle zero-size buffers in MapBuffer and ranges in MapBufferRange (v3)
authorMarek Olšák <maraeo@gmail.com>
Tue, 30 Aug 2011 17:10:06 +0000 (19:10 +0200)
committerMarek Olšák <maraeo@gmail.com>
Fri, 2 Sep 2011 20:39:56 +0000 (22:39 +0200)
Reviewed-by: Brian Paul <brianp@vmware.com>
src/mesa/main/bufferobj.c
src/mesa/state_tracker/st_cb_bufferobjects.c

index c453f9c8554dc8198ba13071f24342ad212b2dc6..a303401748c112a729baf387496d14e807b49df0 100644 (file)
@@ -1004,6 +1004,12 @@ _mesa_MapBufferARB(GLenum target, GLenum access)
       return NULL;
    }
 
+   if (!bufObj->Size) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY,
+                  "glMapBuffer(buffer size = 0)");
+      return NULL;
+   }
+
    ASSERT(ctx->Driver.MapBufferRange);
    map = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size, accessFlags, bufObj);
    if (!map) {
@@ -1410,7 +1416,23 @@ _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
                   "glMapBufferRange(buffer already mapped)");
       return NULL;
    }
-      
+
+   if (!bufObj->Size) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY,
+                  "glMapBufferRange(buffer size = 0)");
+      return NULL;
+   }
+
+   /* Mapping zero bytes should return a non-null pointer. */
+   if (!length) {
+      static long dummy = 0;
+      bufObj->Pointer = &dummy;
+      bufObj->Length = length;
+      bufObj->Offset = offset;
+      bufObj->AccessFlags = access;
+      return bufObj->Pointer;
+   }
+
    ASSERT(ctx->Driver.MapBufferRange);
    map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj);
    if (!map) {
index a451b44049e70d0509bc4d3445b4dfec4e557b52..aab7444e2010369c7303ccef509b977f2eadda45 100644 (file)
@@ -228,13 +228,6 @@ st_bufferobj_data(struct gl_context *ctx,
 }
 
 
-/**
- * Dummy data whose's pointer is used for zero size buffers or ranges.
- */
-static long st_bufferobj_zero_length = 0;
-
-
-
 /**
  * Called via glMapBufferRange().
  */
@@ -280,24 +273,15 @@ st_bufferobj_map_range(struct gl_context *ctx,
    assert(offset < obj->Size);
    assert(offset + length <= obj->Size);
 
-   /*
-    * 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;
-   }
-   else {
-      obj->Pointer = pipe_buffer_map_range(pipe, 
-                                           st_obj->buffer,
-                                           offset, length,
-                                           flags,
-                                           &st_obj->transfer);
-      if (obj->Pointer) {
-         obj->Pointer = (ubyte *) obj->Pointer + offset;
-      }
+   obj->Pointer = pipe_buffer_map_range(pipe,
+                                        st_obj->buffer,
+                                        offset, length,
+                                        flags,
+                                        &st_obj->transfer);
+   if (obj->Pointer) {
+      obj->Pointer = (ubyte *) obj->Pointer + offset;
    }
-   
+
    if (obj->Pointer) {
       obj->Offset = offset;
       obj->Length = length;