2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions.
30 * \author Brian Paul, Ian Romanick
39 #include "bufferobj.h"
44 /*#define BOUNDS_CHECK*/
47 #ifdef FEATURE_OES_mapbuffer
48 #define DEFAULT_ACCESS GL_MAP_WRITE_BIT
50 #define DEFAULT_ACCESS (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)
55 * Get the buffer object bound to the specified target in a GL context.
57 * \param ctx GL context
58 * \param target Buffer object target to be retrieved. Currently this must
59 * be either \c GL_ARRAY_BUFFER or \c GL_ELEMENT_ARRAY_BUFFER.
60 * \return A pointer to the buffer object bound to \c target in the
61 * specified context or \c NULL if \c target is invalid.
63 static INLINE
struct gl_buffer_object
*
64 get_buffer(GLcontext
*ctx
, GLenum target
)
66 struct gl_buffer_object
* bufObj
= NULL
;
69 case GL_ARRAY_BUFFER_ARB
:
70 bufObj
= ctx
->Array
.ArrayBufferObj
;
72 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
73 bufObj
= ctx
->Array
.ElementArrayBufferObj
;
75 case GL_PIXEL_PACK_BUFFER_EXT
:
76 bufObj
= ctx
->Pack
.BufferObj
;
78 case GL_PIXEL_UNPACK_BUFFER_EXT
:
79 bufObj
= ctx
->Unpack
.BufferObj
;
81 case GL_COPY_READ_BUFFER
:
82 if (ctx
->Extensions
.ARB_copy_buffer
) {
83 bufObj
= ctx
->CopyReadBuffer
;
86 case GL_COPY_WRITE_BUFFER
:
87 if (ctx
->Extensions
.ARB_copy_buffer
) {
88 bufObj
= ctx
->CopyWriteBuffer
;
92 /* error must be recorded by caller */
96 /* bufObj should point to NullBufferObj or a user-created buffer object */
104 * Convert a GLbitfield describing the mapped buffer access flags
105 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
108 simplified_access_mode(GLbitfield access
)
110 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
111 if ((access
& rwFlags
) == rwFlags
)
112 return GL_READ_WRITE
;
113 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
115 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
116 return GL_WRITE_ONLY
;
117 return GL_READ_WRITE
; /* this should never happen, but no big deal */
122 * Tests the subdata range parameters and sets the GL error code for
123 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
125 * \param ctx GL context.
126 * \param target Buffer object target on which to operate.
127 * \param offset Offset of the first byte of the subdata range.
128 * \param size Size, in bytes, of the subdata range.
129 * \param caller Name of calling function for recording errors.
130 * \return A pointer to the buffer object bound to \c target in the
131 * specified context or \c NULL if any of the parameter or state
132 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
135 * \sa glBufferSubDataARB, glGetBufferSubDataARB
137 static struct gl_buffer_object
*
138 buffer_object_subdata_range_good( GLcontext
* ctx
, GLenum target
,
139 GLintptrARB offset
, GLsizeiptrARB size
,
142 struct gl_buffer_object
*bufObj
;
145 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
150 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
154 bufObj
= get_buffer(ctx
, target
);
156 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", caller
);
159 if (bufObj
->Name
== 0) {
160 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
163 if (offset
+ size
> bufObj
->Size
) {
164 _mesa_error(ctx
, GL_INVALID_VALUE
,
165 "%s(size + offset > buffer size)", caller
);
168 if (bufObj
->Pointer
) {
169 /* Buffer is currently mapped */
170 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
179 * Allocate and initialize a new buffer object.
181 * Default callback for the \c dd_function_table::NewBufferObject() hook.
183 static struct gl_buffer_object
*
184 _mesa_new_buffer_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
186 struct gl_buffer_object
*obj
;
190 obj
= MALLOC_STRUCT(gl_buffer_object
);
191 _mesa_initialize_buffer_object(obj
, name
, target
);
197 * Delete a buffer object.
199 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
202 _mesa_delete_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*bufObj
)
207 _mesa_free(bufObj
->Data
);
209 /* assign strange values here to help w/ debugging */
210 bufObj
->RefCount
= -1000;
219 * Set ptr to bufObj w/ reference counting.
222 _mesa_reference_buffer_object(GLcontext
*ctx
,
223 struct gl_buffer_object
**ptr
,
224 struct gl_buffer_object
*bufObj
)
230 /* Unreference the old buffer */
231 GLboolean deleteFlag
= GL_FALSE
;
232 struct gl_buffer_object
*oldObj
= *ptr
;
234 /*_glthread_LOCK_MUTEX(oldObj->Mutex);*/
235 ASSERT(oldObj
->RefCount
> 0);
238 printf("BufferObj %p %d DECR to %d\n",
239 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
241 deleteFlag
= (oldObj
->RefCount
== 0);
242 /*_glthread_UNLOCK_MUTEX(oldObj->Mutex);*/
246 /* some sanity checking: don't delete a buffer still in use */
248 /* unfortunately, these tests are invalid during context tear-down */
249 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
250 ASSERT(ctx
->Array
.ElementArrayBufferObj
!= bufObj
);
251 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
254 ASSERT(ctx
->Driver
.DeleteBuffer
);
255 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
263 /* reference new buffer */
264 /*_glthread_LOCK_MUTEX(tex->Mutex);*/
265 if (bufObj
->RefCount
== 0) {
266 /* this buffer's being deleted (look just above) */
267 /* Not sure this can every really happen. Warn if it does. */
268 _mesa_problem(NULL
, "referencing deleted buffer object");
274 printf("BufferObj %p %d INCR to %d\n",
275 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
279 /*_glthread_UNLOCK_MUTEX(tex->Mutex);*/
285 * Initialize a buffer object to default values.
288 _mesa_initialize_buffer_object( struct gl_buffer_object
*obj
,
289 GLuint name
, GLenum target
)
293 _mesa_bzero(obj
, sizeof(struct gl_buffer_object
));
296 obj
->Usage
= GL_STATIC_DRAW_ARB
;
297 obj
->AccessFlags
= DEFAULT_ACCESS
;
302 * Allocate space for and store data in a buffer object. Any data that was
303 * previously stored in the buffer object is lost. If \c data is \c NULL,
304 * memory will be allocated, but no copy will occur.
306 * This is the default callback for \c dd_function_table::BufferData()
307 * Note that all GL error checking will have been done already.
309 * \param ctx GL context.
310 * \param target Buffer object target on which to operate.
311 * \param size Size, in bytes, of the new data store.
312 * \param data Pointer to the data to store in the buffer object. This
313 * pointer may be \c NULL.
314 * \param usage Hints about how the data will be used.
315 * \param bufObj Object to be used.
317 * \sa glBufferDataARB, dd_function_table::BufferData.
320 _mesa_buffer_data( GLcontext
*ctx
, GLenum target
, GLsizeiptrARB size
,
321 const GLvoid
* data
, GLenum usage
,
322 struct gl_buffer_object
* bufObj
)
326 (void) ctx
; (void) target
;
328 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
330 bufObj
->Data
= (GLubyte
*) new_data
;
332 bufObj
->Usage
= usage
;
335 _mesa_memcpy( bufObj
->Data
, data
, size
);
342 * Replace data in a subrange of buffer object. If the data range
343 * specified by \c size + \c offset extends beyond the end of the buffer or
344 * if \c data is \c NULL, no copy is performed.
346 * This is the default callback for \c dd_function_table::BufferSubData()
347 * Note that all GL error checking will have been done already.
349 * \param ctx GL context.
350 * \param target Buffer object target on which to operate.
351 * \param offset Offset of the first byte to be modified.
352 * \param size Size, in bytes, of the data range.
353 * \param data Pointer to the data to store in the buffer object.
354 * \param bufObj Object to be used.
356 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
359 _mesa_buffer_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
360 GLsizeiptrARB size
, const GLvoid
* data
,
361 struct gl_buffer_object
* bufObj
)
363 (void) ctx
; (void) target
;
365 /* this should have been caught in _mesa_BufferSubData() */
366 ASSERT(size
+ offset
<= bufObj
->Size
);
369 _mesa_memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
375 * Retrieve data from a subrange of buffer object. If the data range
376 * specified by \c size + \c offset extends beyond the end of the buffer or
377 * if \c data is \c NULL, no copy is performed.
379 * This is the default callback for \c dd_function_table::GetBufferSubData()
380 * Note that all GL error checking will have been done already.
382 * \param ctx GL context.
383 * \param target Buffer object target on which to operate.
384 * \param offset Offset of the first byte to be fetched.
385 * \param size Size, in bytes, of the data range.
386 * \param data Destination for data
387 * \param bufObj Object to be used.
389 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
392 _mesa_buffer_get_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
393 GLsizeiptrARB size
, GLvoid
* data
,
394 struct gl_buffer_object
* bufObj
)
396 (void) ctx
; (void) target
;
398 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
399 _mesa_memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
405 * Default callback for \c dd_function_tabel::MapBuffer().
407 * The function parameters will have been already tested for errors.
409 * \param ctx GL context.
410 * \param target Buffer object target on which to operate.
411 * \param access Information about how the buffer will be accessed.
412 * \param bufObj Object to be mapped.
413 * \return A pointer to the object's internal data store that can be accessed
416 * \sa glMapBufferARB, dd_function_table::MapBuffer
419 _mesa_buffer_map( GLcontext
*ctx
, GLenum target
, GLenum access
,
420 struct gl_buffer_object
*bufObj
)
425 /* Just return a direct pointer to the data */
426 if (bufObj
->Pointer
) {
427 /* already mapped! */
430 bufObj
->Pointer
= bufObj
->Data
;
431 return bufObj
->Pointer
;
436 * Default fallback for \c dd_function_table::MapBufferRange().
437 * Called via glMapBufferRange().
440 _mesa_buffer_map_range( GLcontext
*ctx
, GLenum target
, GLintptr offset
,
441 GLsizeiptr length
, GLbitfield access
,
442 struct gl_buffer_object
*bufObj
)
448 assert(!bufObj
->Pointer
);
449 /* Just return a direct pointer to the data */
450 return bufObj
->Data
+ offset
;
455 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
456 * Called via glFlushMappedBufferRange().
459 _mesa_buffer_flush_mapped_range( GLcontext
*ctx
, GLenum target
,
460 GLintptr offset
, GLsizeiptr length
,
461 struct gl_buffer_object
*obj
)
473 * Default callback for \c dd_function_table::MapBuffer().
475 * The input parameters will have been already tested for errors.
477 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
480 _mesa_buffer_unmap( GLcontext
*ctx
, GLenum target
,
481 struct gl_buffer_object
*bufObj
)
485 /* XXX we might assert here that bufObj->Pointer is non-null */
486 bufObj
->Pointer
= NULL
;
492 * Default fallback for \c dd_function_table::CopyBufferSubData().
493 * Called via glCopyBuffserSubData().
496 _mesa_copy_buffer_subdata(GLcontext
*ctx
,
497 struct gl_buffer_object
*src
,
498 struct gl_buffer_object
*dst
,
499 GLintptr readOffset
, GLintptr writeOffset
,
502 GLubyte
*srcPtr
, *dstPtr
;
504 /* buffer should not already be mapped */
505 assert(!src
->Pointer
);
506 assert(!dst
->Pointer
);
508 srcPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_READ_BUFFER
,
510 dstPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_WRITE_BUFFER
,
513 if (srcPtr
&& dstPtr
)
514 _mesa_memcpy(dstPtr
+ writeOffset
, srcPtr
+ readOffset
, size
);
516 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_READ_BUFFER
, src
);
517 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_WRITE_BUFFER
, dst
);
523 * Initialize the state associated with buffer objects
526 _mesa_init_buffer_objects( GLcontext
*ctx
)
528 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
529 ctx
->Shared
->NullBufferObj
);
530 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
,
531 ctx
->Shared
->NullBufferObj
);
533 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
534 ctx
->Shared
->NullBufferObj
);
535 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
536 ctx
->Shared
->NullBufferObj
);
541 * Bind the specified target to buffer for the specified context.
544 bind_buffer_object(GLcontext
*ctx
, GLenum target
, GLuint buffer
)
546 struct gl_buffer_object
*oldBufObj
;
547 struct gl_buffer_object
*newBufObj
= NULL
;
548 struct gl_buffer_object
**bindTarget
= NULL
;
551 case GL_ARRAY_BUFFER_ARB
:
552 bindTarget
= &ctx
->Array
.ArrayBufferObj
;
554 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
555 bindTarget
= &ctx
->Array
.ElementArrayBufferObj
;
557 case GL_PIXEL_PACK_BUFFER_EXT
:
558 bindTarget
= &ctx
->Pack
.BufferObj
;
560 case GL_PIXEL_UNPACK_BUFFER_EXT
:
561 bindTarget
= &ctx
->Unpack
.BufferObj
;
563 case GL_COPY_READ_BUFFER
:
564 if (ctx
->Extensions
.ARB_copy_buffer
) {
565 bindTarget
= &ctx
->CopyReadBuffer
;
568 case GL_COPY_WRITE_BUFFER
:
569 if (ctx
->Extensions
.ARB_copy_buffer
) {
570 bindTarget
= &ctx
->CopyWriteBuffer
;
574 ; /* no-op / we'll hit the follow error test next */
578 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)");
582 /* Get pointer to old buffer object (to be unbound) */
583 oldBufObj
= get_buffer(ctx
, target
);
584 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
585 return; /* rebinding the same buffer object- no change */
588 * Get pointer to new buffer object (newBufObj)
591 /* The spec says there's not a buffer object named 0, but we use
592 * one internally because it simplifies things.
594 newBufObj
= ctx
->Shared
->NullBufferObj
;
597 /* non-default buffer object */
598 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
600 /* if this is a new buffer object id, allocate a buffer object now */
601 ASSERT(ctx
->Driver
.NewBufferObject
);
602 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
604 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
607 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
611 /* bind new buffer */
612 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
614 /* Pass BindBuffer call to device driver */
615 if (ctx
->Driver
.BindBuffer
)
616 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
621 * Update the default buffer objects in the given context to reference those
622 * specified in the shared state and release those referencing the old
626 _mesa_update_default_objects_buffer_objects(GLcontext
*ctx
)
628 /* Bind the NullBufferObj to remove references to those
629 * in the shared context hash table.
631 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
632 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
633 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
634 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
639 * When we're about to read pixel data out of a PBO (via glDrawPixels,
640 * glTexImage, etc) or write data into a PBO (via glReadPixels,
641 * glGetTexImage, etc) we call this function to check that we're not
642 * going to read out of bounds.
644 * XXX This would also be a convenient time to check that the PBO isn't
645 * currently mapped. Whoever calls this function should check for that.
646 * Remember, we can't use a PBO when it's mapped!
648 * \param width width of image to read/write
649 * \param height height of image to read/write
650 * \param depth depth of image to read/write
651 * \param format format of image to read/write
652 * \param type datatype of image to read/write
653 * \param ptr the user-provided pointer/offset
654 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
658 _mesa_validate_pbo_access(GLuint dimensions
,
659 const struct gl_pixelstore_attrib
*pack
,
660 GLsizei width
, GLsizei height
, GLsizei depth
,
661 GLenum format
, GLenum type
, const GLvoid
*ptr
)
664 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
666 ASSERT(pack
->BufferObj
->Name
!= 0);
668 if (pack
->BufferObj
->Size
== 0)
672 /* get address of first pixel we'll read */
673 start
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
674 format
, type
, 0, 0, 0);
676 /* get address just past the last pixel we'll read */
677 end
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
678 format
, type
, depth
-1, height
-1, width
);
681 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
683 if ((const GLubyte
*) start
> sizeAddr
) {
684 /* This will catch negative values / wrap-around */
687 if ((const GLubyte
*) end
> sizeAddr
) {
688 /* Image read goes beyond end of buffer */
698 * If the source of glBitmap data is a PBO, check that we won't read out
699 * of buffer bounds, then map the buffer.
700 * If not sourcing from a PBO, just return the bitmap pointer.
701 * This is a helper function for (some) drivers.
702 * Return NULL if error.
703 * If non-null return, must call _mesa_unmap_bitmap_pbo() when done.
706 _mesa_map_bitmap_pbo(GLcontext
*ctx
,
707 const struct gl_pixelstore_attrib
*unpack
,
708 const GLubyte
*bitmap
)
712 if (unpack
->BufferObj
->Name
) {
713 /* unpack from PBO */
714 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
720 buf
= ADD_POINTERS(buf
, bitmap
);
723 /* unpack from normal memory */
732 * Counterpart to _mesa_map_bitmap_pbo()
733 * This is a helper function for (some) drivers.
736 _mesa_unmap_bitmap_pbo(GLcontext
*ctx
,
737 const struct gl_pixelstore_attrib
*unpack
)
739 if (unpack
->BufferObj
->Name
) {
740 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
747 * \sa _mesa_map_bitmap_pbo
750 _mesa_map_drawpix_pbo(GLcontext
*ctx
,
751 const struct gl_pixelstore_attrib
*unpack
,
752 const GLvoid
*pixels
)
756 if (unpack
->BufferObj
->Name
) {
757 /* unpack from PBO */
758 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
764 buf
= ADD_POINTERS(buf
, pixels
);
767 /* unpack from normal memory */
776 * \sa _mesa_unmap_bitmap_pbo
779 _mesa_unmap_drawpix_pbo(GLcontext
*ctx
,
780 const struct gl_pixelstore_attrib
*unpack
)
782 if (unpack
->BufferObj
->Name
) {
783 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
790 * If PBO is bound, map the buffer, return dest pointer in mapped buffer.
791 * Call _mesa_unmap_readpix_pbo() when finished
792 * \return NULL if error
795 _mesa_map_readpix_pbo(GLcontext
*ctx
,
796 const struct gl_pixelstore_attrib
*pack
,
801 if (pack
->BufferObj
->Name
) {
803 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
809 buf
= ADD_POINTERS(buf
, dest
);
812 /* pack to normal memory */
821 * Counterpart to _mesa_map_readpix_pbo()
824 _mesa_unmap_readpix_pbo(GLcontext
*ctx
,
825 const struct gl_pixelstore_attrib
*pack
)
827 if (pack
->BufferObj
->Name
) {
828 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
, pack
->BufferObj
);
835 * Return the gl_buffer_object for the given ID.
836 * Always return NULL for ID 0.
838 struct gl_buffer_object
*
839 _mesa_lookup_bufferobj(GLcontext
*ctx
, GLuint buffer
)
844 return (struct gl_buffer_object
*)
845 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
850 * If *ptr points to obj, set ptr = the Null/default buffer object.
851 * This is a helper for buffer object deletion.
852 * The GL spec says that deleting a buffer object causes it to get
853 * unbound from all arrays in the current context.
856 unbind(GLcontext
*ctx
,
857 struct gl_buffer_object
**ptr
,
858 struct gl_buffer_object
*obj
)
861 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
867 * Plug default/fallback buffer object functions into the device
871 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
873 /* GL_ARB_vertex/pixel_buffer_object */
874 driver
->NewBufferObject
= _mesa_new_buffer_object
;
875 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
876 driver
->BindBuffer
= NULL
;
877 driver
->BufferData
= _mesa_buffer_data
;
878 driver
->BufferSubData
= _mesa_buffer_subdata
;
879 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
880 driver
->MapBuffer
= _mesa_buffer_map
;
881 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
883 /* GL_ARB_map_buffer_range */
884 driver
->MapBufferRange
= _mesa_buffer_map_range
;
885 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
887 /* GL_ARB_copy_buffer */
888 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
893 /**********************************************************************/
895 /**********************************************************************/
898 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
900 GET_CURRENT_CONTEXT(ctx
);
901 ASSERT_OUTSIDE_BEGIN_END(ctx
);
903 bind_buffer_object(ctx
, target
, buffer
);
908 * Delete a set of buffer objects.
910 * \param n Number of buffer objects to delete.
911 * \param ids Array of \c n buffer object IDs.
914 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
916 GET_CURRENT_CONTEXT(ctx
);
918 ASSERT_OUTSIDE_BEGIN_END(ctx
);
921 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
925 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
927 for (i
= 0; i
< n
; i
++) {
928 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
930 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
933 ASSERT(bufObj
->Name
== ids
[i
]);
935 if (bufObj
->Pointer
) {
936 /* if mapped, unmap it now */
937 ctx
->Driver
.UnmapBuffer(ctx
, 0, bufObj
);
938 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
939 bufObj
->Pointer
= NULL
;
942 /* unbind any vertex pointers bound to this buffer */
943 unbind(ctx
, &arrayObj
->Vertex
.BufferObj
, bufObj
);
944 unbind(ctx
, &arrayObj
->Weight
.BufferObj
, bufObj
);
945 unbind(ctx
, &arrayObj
->Normal
.BufferObj
, bufObj
);
946 unbind(ctx
, &arrayObj
->Color
.BufferObj
, bufObj
);
947 unbind(ctx
, &arrayObj
->SecondaryColor
.BufferObj
, bufObj
);
948 unbind(ctx
, &arrayObj
->FogCoord
.BufferObj
, bufObj
);
949 unbind(ctx
, &arrayObj
->Index
.BufferObj
, bufObj
);
950 unbind(ctx
, &arrayObj
->EdgeFlag
.BufferObj
, bufObj
);
951 for (j
= 0; j
< Elements(arrayObj
->TexCoord
); j
++) {
952 unbind(ctx
, &arrayObj
->TexCoord
[j
].BufferObj
, bufObj
);
954 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
955 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
958 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
959 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
961 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
962 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
965 /* unbind any pixel pack/unpack pointers bound to this buffer */
966 if (ctx
->Pack
.BufferObj
== bufObj
) {
967 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
969 if (ctx
->Unpack
.BufferObj
== bufObj
) {
970 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
973 /* The ID is immediately freed for re-use */
974 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, bufObj
->Name
);
975 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
979 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
984 * Generate a set of unique buffer object IDs and store them in \c buffer.
986 * \param n Number of IDs to generate.
987 * \param buffer Array of \c n locations to store the IDs.
990 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
992 GET_CURRENT_CONTEXT(ctx
);
995 ASSERT_OUTSIDE_BEGIN_END(ctx
);
998 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
1007 * This must be atomic (generation and allocation of buffer object IDs)
1009 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1011 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1013 /* Allocate new, empty buffer objects and return identifiers */
1014 for (i
= 0; i
< n
; i
++) {
1015 struct gl_buffer_object
*bufObj
;
1016 GLuint name
= first
+ i
;
1018 bufObj
= ctx
->Driver
.NewBufferObject( ctx
, name
, target
);
1020 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1021 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenBuffersARB");
1024 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
, bufObj
);
1025 buffer
[i
] = first
+ i
;
1028 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1033 * Determine if ID is the name of a buffer object.
1035 * \param id ID of the potential buffer object.
1036 * \return \c GL_TRUE if \c id is the name of a buffer object,
1037 * \c GL_FALSE otherwise.
1039 GLboolean GLAPIENTRY
1040 _mesa_IsBufferARB(GLuint id
)
1042 struct gl_buffer_object
*bufObj
;
1043 GET_CURRENT_CONTEXT(ctx
);
1044 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1046 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1047 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1048 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1050 return bufObj
? GL_TRUE
: GL_FALSE
;
1055 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
1056 const GLvoid
* data
, GLenum usage
)
1058 GET_CURRENT_CONTEXT(ctx
);
1059 struct gl_buffer_object
*bufObj
;
1060 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1063 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1068 case GL_STREAM_DRAW_ARB
:
1069 case GL_STREAM_READ_ARB
:
1070 case GL_STREAM_COPY_ARB
:
1071 case GL_STATIC_DRAW_ARB
:
1072 case GL_STATIC_READ_ARB
:
1073 case GL_STATIC_COPY_ARB
:
1074 case GL_DYNAMIC_DRAW_ARB
:
1075 case GL_DYNAMIC_READ_ARB
:
1076 case GL_DYNAMIC_COPY_ARB
:
1080 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
1084 bufObj
= get_buffer(ctx
, target
);
1086 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
1089 if (bufObj
->Name
== 0) {
1090 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer 0)" );
1094 if (bufObj
->Pointer
) {
1095 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1096 ctx
->Driver
.UnmapBuffer(ctx
, target
, bufObj
);
1097 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1098 bufObj
->Pointer
= NULL
;
1101 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1103 ASSERT(ctx
->Driver
.BufferData
);
1105 bufObj
->Written
= GL_TRUE
;
1108 _mesa_printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1109 bufObj
->Name
, size
, data
, usage
);
1115 /* Give the buffer object to the driver! <data> may be null! */
1116 ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
);
1121 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1122 GLsizeiptrARB size
, const GLvoid
* data
)
1124 GET_CURRENT_CONTEXT(ctx
);
1125 struct gl_buffer_object
*bufObj
;
1126 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1128 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1129 "glBufferSubDataARB" );
1131 /* error already recorded */
1135 bufObj
->Written
= GL_TRUE
;
1137 ASSERT(ctx
->Driver
.BufferSubData
);
1138 ctx
->Driver
.BufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1143 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1144 GLsizeiptrARB size
, void * data
)
1146 GET_CURRENT_CONTEXT(ctx
);
1147 struct gl_buffer_object
*bufObj
;
1148 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1150 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1151 "glGetBufferSubDataARB" );
1153 /* error already recorded */
1157 ASSERT(ctx
->Driver
.GetBufferSubData
);
1158 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1163 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1165 GET_CURRENT_CONTEXT(ctx
);
1166 struct gl_buffer_object
* bufObj
;
1167 GLbitfield accessFlags
;
1168 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1171 case GL_READ_ONLY_ARB
:
1172 accessFlags
= GL_MAP_READ_BIT
;
1174 case GL_WRITE_ONLY_ARB
:
1175 accessFlags
= GL_MAP_WRITE_BIT
;
1177 case GL_READ_WRITE_ARB
:
1178 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1181 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1185 bufObj
= get_buffer(ctx
, target
);
1187 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
1190 if (bufObj
->Name
== 0) {
1191 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(buffer 0)" );
1194 if (bufObj
->Pointer
) {
1195 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1199 ASSERT(ctx
->Driver
.MapBuffer
);
1200 bufObj
->Pointer
= ctx
->Driver
.MapBuffer( ctx
, target
, access
, bufObj
);
1201 if (!bufObj
->Pointer
) {
1202 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(access)");
1205 bufObj
->AccessFlags
= accessFlags
;
1207 bufObj
->Length
= bufObj
->Size
;
1209 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1210 bufObj
->Written
= GL_TRUE
;
1213 _mesa_printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1214 bufObj
->Name
, bufObj
->Size
, access
);
1215 if (access
== GL_WRITE_ONLY_ARB
) {
1217 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1218 for (i
= 0; i
< bufObj
->Size
; i
++)
1225 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1227 /* buffer is 100 bytes larger than requested, fill with magic value */
1228 for (i
= 0; i
< 100; i
++) {
1229 buf
[bufObj
->Size
- i
- 1] = 123;
1234 return bufObj
->Pointer
;
1238 GLboolean GLAPIENTRY
1239 _mesa_UnmapBufferARB(GLenum target
)
1241 GET_CURRENT_CONTEXT(ctx
);
1242 struct gl_buffer_object
*bufObj
;
1243 GLboolean status
= GL_TRUE
;
1244 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1246 bufObj
= get_buffer(ctx
, target
);
1248 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1251 if (bufObj
->Name
== 0) {
1252 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1255 if (!bufObj
->Pointer
) {
1256 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1261 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1262 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1264 /* check that last 100 bytes are still = magic value */
1265 for (i
= 0; i
< 100; i
++) {
1266 GLuint pos
= bufObj
->Size
- i
- 1;
1267 if (buf
[pos
] != 123) {
1268 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1269 " at position %d (value = %u)\n",
1277 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1278 GLuint i
, unchanged
= 0;
1279 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1281 /* check which bytes changed */
1282 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1283 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1290 _mesa_printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1291 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1296 status
= ctx
->Driver
.UnmapBuffer( ctx
, target
, bufObj
);
1297 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1298 bufObj
->Pointer
= NULL
;
1307 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1309 GET_CURRENT_CONTEXT(ctx
);
1310 struct gl_buffer_object
*bufObj
;
1311 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1313 bufObj
= get_buffer(ctx
, target
);
1315 _mesa_error(ctx
, GL_INVALID_ENUM
, "GetBufferParameterivARB(target)" );
1318 if (bufObj
->Name
== 0) {
1319 _mesa_error(ctx
, GL_INVALID_OPERATION
, "GetBufferParameterivARB" );
1324 case GL_BUFFER_SIZE_ARB
:
1325 *params
= (GLint
) bufObj
->Size
;
1327 case GL_BUFFER_USAGE_ARB
:
1328 *params
= bufObj
->Usage
;
1330 case GL_BUFFER_ACCESS_ARB
:
1331 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1333 case GL_BUFFER_MAPPED_ARB
:
1334 *params
= (bufObj
->Pointer
!= NULL
);
1337 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname)");
1344 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1346 GET_CURRENT_CONTEXT(ctx
);
1347 struct gl_buffer_object
* bufObj
;
1348 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1350 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1351 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1355 bufObj
= get_buffer(ctx
, target
);
1357 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1360 if (bufObj
->Name
== 0) {
1361 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1365 *params
= bufObj
->Pointer
;
1370 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1371 GLintptr readOffset
, GLintptr writeOffset
,
1374 GET_CURRENT_CONTEXT(ctx
);
1375 struct gl_buffer_object
*src
, *dst
;
1376 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1378 src
= get_buffer(ctx
, readTarget
);
1379 if (!src
|| src
->Name
== 0) {
1380 _mesa_error(ctx
, GL_INVALID_ENUM
,
1381 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget
);
1385 dst
= get_buffer(ctx
, writeTarget
);
1386 if (!dst
|| dst
->Name
== 0) {
1387 _mesa_error(ctx
, GL_INVALID_ENUM
,
1388 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget
);
1393 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1394 "glCopyBuffserSubData(readBuffer is mapped)");
1399 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1400 "glCopyBuffserSubData(writeBuffer is mapped)");
1404 if (readOffset
< 0) {
1405 _mesa_error(ctx
, GL_INVALID_VALUE
,
1406 "glCopyBuffserSubData(readOffset = %d)", readOffset
);
1410 if (writeOffset
< 0) {
1411 _mesa_error(ctx
, GL_INVALID_VALUE
,
1412 "glCopyBuffserSubData(writeOffset = %d)", writeOffset
);
1416 if (readOffset
+ size
> src
->Size
) {
1417 _mesa_error(ctx
, GL_INVALID_VALUE
,
1418 "glCopyBuffserSubData(readOffset + size = %d)",
1423 if (writeOffset
+ size
> dst
->Size
) {
1424 _mesa_error(ctx
, GL_INVALID_VALUE
,
1425 "glCopyBuffserSubData(writeOffset + size = %d)",
1431 if (readOffset
+ size
<= writeOffset
) {
1434 else if (writeOffset
+ size
<= readOffset
) {
1438 /* overlapping src/dst is illegal */
1439 _mesa_error(ctx
, GL_INVALID_VALUE
,
1440 "glCopyBuffserSubData(overlapping src/dst)");
1445 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1450 * See GL_ARB_map_buffer_range spec
1453 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1456 GET_CURRENT_CONTEXT(ctx
);
1457 struct gl_buffer_object
*bufObj
;
1458 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1460 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1461 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1462 "glMapBufferRange(extension not supported)");
1467 _mesa_error(ctx
, GL_INVALID_VALUE
,
1468 "glMapBufferRange(offset = %ld)", offset
);
1473 _mesa_error(ctx
, GL_INVALID_VALUE
,
1474 "glMapBufferRange(length = %ld)", length
);
1478 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1479 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1480 "glMapBufferRange(access indicates neither read or write)");
1484 if (access
& GL_MAP_READ_BIT
) {
1485 if ((access
& GL_MAP_INVALIDATE_RANGE_BIT
) ||
1486 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) ||
1487 (access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
1488 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1489 "glMapBufferRange(invalid access flags)");
1494 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1495 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1496 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1497 "glMapBufferRange(invalid access flags)");
1501 bufObj
= get_buffer(ctx
, target
);
1502 if (!bufObj
|| bufObj
->Name
== 0) {
1503 _mesa_error(ctx
, GL_INVALID_ENUM
,
1504 "glMapBufferRange(target = 0x%x)", target
);
1508 if (offset
+ length
> bufObj
->Size
) {
1509 _mesa_error(ctx
, GL_INVALID_VALUE
,
1510 "glMapBufferRange(offset + length > size)");
1514 if (bufObj
->Pointer
) {
1515 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1516 "glMapBufferRange(buffer already mapped)");
1520 ASSERT(ctx
->Driver
.MapBufferRange
);
1521 bufObj
->Pointer
= ctx
->Driver
.MapBufferRange(ctx
, target
, offset
, length
,
1524 bufObj
->Offset
= offset
;
1525 bufObj
->Length
= length
;
1526 bufObj
->AccessFlags
= access
;
1528 return bufObj
->Pointer
;
1533 * See GL_ARB_map_buffer_range spec
1536 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1538 GET_CURRENT_CONTEXT(ctx
);
1539 struct gl_buffer_object
*bufObj
;
1540 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1542 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1543 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1544 "glMapBufferRange(extension not supported)");
1549 _mesa_error(ctx
, GL_INVALID_VALUE
,
1550 "glMapBufferRange(offset = %ld)", offset
);
1555 _mesa_error(ctx
, GL_INVALID_VALUE
,
1556 "glMapBufferRange(length = %ld)", length
);
1560 bufObj
= get_buffer(ctx
, target
);
1562 _mesa_error(ctx
, GL_INVALID_ENUM
,
1563 "glMapBufferRange(target = 0x%x)", target
);
1567 if (bufObj
->Name
== 0) {
1568 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1569 "glMapBufferRange(current buffer is 0)");
1573 if (!bufObj
->Pointer
) {
1574 /* buffer is not mapped */
1575 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1576 "glMapBufferRange(buffer is not mapped)");
1580 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1581 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1582 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1586 if (offset
+ length
> bufObj
->Length
) {
1587 _mesa_error(ctx
, GL_INVALID_VALUE
,
1588 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1589 offset
, length
, bufObj
->Length
);
1593 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1595 if (ctx
->Driver
.FlushMappedBufferRange
)
1596 ctx
->Driver
.FlushMappedBufferRange(ctx
, target
, offset
, length
, bufObj
);