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 (!_mesa_is_bufferobj(bufObj
)) {
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 (_mesa_bufferobj_mapped(bufObj
)) {
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 * \return GL_TRUE for success, GL_FALSE for failure
318 * \sa glBufferDataARB, dd_function_table::BufferData.
321 _mesa_buffer_data( GLcontext
*ctx
, GLenum target
, GLsizeiptrARB size
,
322 const GLvoid
* data
, GLenum usage
,
323 struct gl_buffer_object
* bufObj
)
327 (void) ctx
; (void) target
;
329 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
331 bufObj
->Data
= (GLubyte
*) new_data
;
333 bufObj
->Usage
= usage
;
336 _mesa_memcpy( bufObj
->Data
, data
, size
);
348 * Replace data in a subrange of buffer object. If the data range
349 * specified by \c size + \c offset extends beyond the end of the buffer or
350 * if \c data is \c NULL, no copy is performed.
352 * This is the default callback for \c dd_function_table::BufferSubData()
353 * Note that all GL error checking will have been done already.
355 * \param ctx GL context.
356 * \param target Buffer object target on which to operate.
357 * \param offset Offset of the first byte to be modified.
358 * \param size Size, in bytes, of the data range.
359 * \param data Pointer to the data to store in the buffer object.
360 * \param bufObj Object to be used.
362 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
365 _mesa_buffer_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
366 GLsizeiptrARB size
, const GLvoid
* data
,
367 struct gl_buffer_object
* bufObj
)
369 (void) ctx
; (void) target
;
371 /* this should have been caught in _mesa_BufferSubData() */
372 ASSERT(size
+ offset
<= bufObj
->Size
);
375 _mesa_memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
381 * Retrieve data from a subrange of buffer object. If the data range
382 * specified by \c size + \c offset extends beyond the end of the buffer or
383 * if \c data is \c NULL, no copy is performed.
385 * This is the default callback for \c dd_function_table::GetBufferSubData()
386 * Note that all GL error checking will have been done already.
388 * \param ctx GL context.
389 * \param target Buffer object target on which to operate.
390 * \param offset Offset of the first byte to be fetched.
391 * \param size Size, in bytes, of the data range.
392 * \param data Destination for data
393 * \param bufObj Object to be used.
395 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
398 _mesa_buffer_get_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
399 GLsizeiptrARB size
, GLvoid
* data
,
400 struct gl_buffer_object
* bufObj
)
402 (void) ctx
; (void) target
;
404 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
405 _mesa_memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
411 * Default callback for \c dd_function_tabel::MapBuffer().
413 * The function parameters will have been already tested for errors.
415 * \param ctx GL context.
416 * \param target Buffer object target on which to operate.
417 * \param access Information about how the buffer will be accessed.
418 * \param bufObj Object to be mapped.
419 * \return A pointer to the object's internal data store that can be accessed
422 * \sa glMapBufferARB, dd_function_table::MapBuffer
425 _mesa_buffer_map( GLcontext
*ctx
, GLenum target
, GLenum access
,
426 struct gl_buffer_object
*bufObj
)
431 /* Just return a direct pointer to the data */
432 if (_mesa_bufferobj_mapped(bufObj
)) {
433 /* already mapped! */
436 bufObj
->Pointer
= bufObj
->Data
;
437 bufObj
->Length
= bufObj
->Size
;
439 return bufObj
->Pointer
;
444 * Default fallback for \c dd_function_table::MapBufferRange().
445 * Called via glMapBufferRange().
448 _mesa_buffer_map_range( GLcontext
*ctx
, GLenum target
, GLintptr offset
,
449 GLsizeiptr length
, GLbitfield access
,
450 struct gl_buffer_object
*bufObj
)
454 assert(!_mesa_bufferobj_mapped(bufObj
));
455 /* Just return a direct pointer to the data */
456 bufObj
->Pointer
= bufObj
->Data
+ offset
;
457 bufObj
->Length
= length
;
458 bufObj
->Offset
= offset
;
459 bufObj
->AccessFlags
= access
;
460 return bufObj
->Pointer
;
465 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
466 * Called via glFlushMappedBufferRange().
469 _mesa_buffer_flush_mapped_range( GLcontext
*ctx
, GLenum target
,
470 GLintptr offset
, GLsizeiptr length
,
471 struct gl_buffer_object
*obj
)
483 * Default callback for \c dd_function_table::MapBuffer().
485 * The input parameters will have been already tested for errors.
487 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
490 _mesa_buffer_unmap( GLcontext
*ctx
, GLenum target
,
491 struct gl_buffer_object
*bufObj
)
495 /* XXX we might assert here that bufObj->Pointer is non-null */
496 bufObj
->Pointer
= NULL
;
499 bufObj
->AccessFlags
= 0x0;
505 * Default fallback for \c dd_function_table::CopyBufferSubData().
506 * Called via glCopyBuffserSubData().
509 _mesa_copy_buffer_subdata(GLcontext
*ctx
,
510 struct gl_buffer_object
*src
,
511 struct gl_buffer_object
*dst
,
512 GLintptr readOffset
, GLintptr writeOffset
,
515 GLubyte
*srcPtr
, *dstPtr
;
517 /* buffer should not already be mapped */
518 assert(!_mesa_bufferobj_mapped(src
));
519 assert(!_mesa_bufferobj_mapped(dst
));
521 srcPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_READ_BUFFER
,
523 dstPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_WRITE_BUFFER
,
526 if (srcPtr
&& dstPtr
)
527 _mesa_memcpy(dstPtr
+ writeOffset
, srcPtr
+ readOffset
, size
);
529 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_READ_BUFFER
, src
);
530 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_WRITE_BUFFER
, dst
);
536 * Initialize the state associated with buffer objects
539 _mesa_init_buffer_objects( GLcontext
*ctx
)
541 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
542 ctx
->Shared
->NullBufferObj
);
543 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
,
544 ctx
->Shared
->NullBufferObj
);
546 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
547 ctx
->Shared
->NullBufferObj
);
548 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
549 ctx
->Shared
->NullBufferObj
);
554 * Bind the specified target to buffer for the specified context.
557 bind_buffer_object(GLcontext
*ctx
, GLenum target
, GLuint buffer
)
559 struct gl_buffer_object
*oldBufObj
;
560 struct gl_buffer_object
*newBufObj
= NULL
;
561 struct gl_buffer_object
**bindTarget
= NULL
;
564 case GL_ARRAY_BUFFER_ARB
:
565 bindTarget
= &ctx
->Array
.ArrayBufferObj
;
567 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
568 bindTarget
= &ctx
->Array
.ElementArrayBufferObj
;
570 case GL_PIXEL_PACK_BUFFER_EXT
:
571 bindTarget
= &ctx
->Pack
.BufferObj
;
573 case GL_PIXEL_UNPACK_BUFFER_EXT
:
574 bindTarget
= &ctx
->Unpack
.BufferObj
;
576 case GL_COPY_READ_BUFFER
:
577 if (ctx
->Extensions
.ARB_copy_buffer
) {
578 bindTarget
= &ctx
->CopyReadBuffer
;
581 case GL_COPY_WRITE_BUFFER
:
582 if (ctx
->Extensions
.ARB_copy_buffer
) {
583 bindTarget
= &ctx
->CopyWriteBuffer
;
587 ; /* no-op / we'll hit the follow error test next */
591 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)");
595 /* Get pointer to old buffer object (to be unbound) */
596 oldBufObj
= get_buffer(ctx
, target
);
597 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
598 return; /* rebinding the same buffer object- no change */
601 * Get pointer to new buffer object (newBufObj)
604 /* The spec says there's not a buffer object named 0, but we use
605 * one internally because it simplifies things.
607 newBufObj
= ctx
->Shared
->NullBufferObj
;
610 /* non-default buffer object */
611 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
613 /* if this is a new buffer object id, allocate a buffer object now */
614 ASSERT(ctx
->Driver
.NewBufferObject
);
615 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
617 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
620 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
624 /* bind new buffer */
625 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
627 /* Pass BindBuffer call to device driver */
628 if (ctx
->Driver
.BindBuffer
)
629 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
634 * Update the default buffer objects in the given context to reference those
635 * specified in the shared state and release those referencing the old
639 _mesa_update_default_objects_buffer_objects(GLcontext
*ctx
)
641 /* Bind the NullBufferObj to remove references to those
642 * in the shared context hash table.
644 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
645 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
646 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
647 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
652 * When we're about to read pixel data out of a PBO (via glDrawPixels,
653 * glTexImage, etc) or write data into a PBO (via glReadPixels,
654 * glGetTexImage, etc) we call this function to check that we're not
655 * going to read out of bounds.
657 * XXX This would also be a convenient time to check that the PBO isn't
658 * currently mapped. Whoever calls this function should check for that.
659 * Remember, we can't use a PBO when it's mapped!
661 * \param width width of image to read/write
662 * \param height height of image to read/write
663 * \param depth depth of image to read/write
664 * \param format format of image to read/write
665 * \param type datatype of image to read/write
666 * \param ptr the user-provided pointer/offset
667 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
671 _mesa_validate_pbo_access(GLuint dimensions
,
672 const struct gl_pixelstore_attrib
*pack
,
673 GLsizei width
, GLsizei height
, GLsizei depth
,
674 GLenum format
, GLenum type
, const GLvoid
*ptr
)
677 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
679 ASSERT(_mesa_is_bufferobj(pack
->BufferObj
));
681 if (pack
->BufferObj
->Size
== 0)
685 /* get address of first pixel we'll read */
686 start
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
687 format
, type
, 0, 0, 0);
689 /* get address just past the last pixel we'll read */
690 end
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
691 format
, type
, depth
-1, height
-1, width
);
694 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
696 if ((const GLubyte
*) start
> sizeAddr
) {
697 /* This will catch negative values / wrap-around */
700 if ((const GLubyte
*) end
> sizeAddr
) {
701 /* Image read goes beyond end of buffer */
711 * If the source of glBitmap data is a PBO, check that we won't read out
712 * of buffer bounds, then map the buffer.
713 * If not sourcing from a PBO, just return the bitmap pointer.
714 * This is a helper function for (some) drivers.
715 * Return NULL if error.
716 * If non-null return, must call _mesa_unmap_bitmap_pbo() when done.
719 _mesa_map_bitmap_pbo(GLcontext
*ctx
,
720 const struct gl_pixelstore_attrib
*unpack
,
721 const GLubyte
*bitmap
)
725 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
726 /* unpack from PBO */
727 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
733 buf
= ADD_POINTERS(buf
, bitmap
);
736 /* unpack from normal memory */
745 * Counterpart to _mesa_map_bitmap_pbo()
746 * This is a helper function for (some) drivers.
749 _mesa_unmap_bitmap_pbo(GLcontext
*ctx
,
750 const struct gl_pixelstore_attrib
*unpack
)
752 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
753 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
760 * \sa _mesa_map_bitmap_pbo
763 _mesa_map_drawpix_pbo(GLcontext
*ctx
,
764 const struct gl_pixelstore_attrib
*unpack
,
765 const GLvoid
*pixels
)
769 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
770 /* unpack from PBO */
771 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
777 buf
= ADD_POINTERS(buf
, pixels
);
780 /* unpack from normal memory */
789 * \sa _mesa_unmap_bitmap_pbo
792 _mesa_unmap_drawpix_pbo(GLcontext
*ctx
,
793 const struct gl_pixelstore_attrib
*unpack
)
795 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
796 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
803 * If PBO is bound, map the buffer, return dest pointer in mapped buffer.
804 * Call _mesa_unmap_readpix_pbo() when finished
805 * \return NULL if error
808 _mesa_map_readpix_pbo(GLcontext
*ctx
,
809 const struct gl_pixelstore_attrib
*pack
,
814 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
816 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
822 buf
= ADD_POINTERS(buf
, dest
);
825 /* pack to normal memory */
834 * Counterpart to _mesa_map_readpix_pbo()
837 _mesa_unmap_readpix_pbo(GLcontext
*ctx
,
838 const struct gl_pixelstore_attrib
*pack
)
840 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
841 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
, pack
->BufferObj
);
848 * Return the gl_buffer_object for the given ID.
849 * Always return NULL for ID 0.
851 struct gl_buffer_object
*
852 _mesa_lookup_bufferobj(GLcontext
*ctx
, GLuint buffer
)
857 return (struct gl_buffer_object
*)
858 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
863 * If *ptr points to obj, set ptr = the Null/default buffer object.
864 * This is a helper for buffer object deletion.
865 * The GL spec says that deleting a buffer object causes it to get
866 * unbound from all arrays in the current context.
869 unbind(GLcontext
*ctx
,
870 struct gl_buffer_object
**ptr
,
871 struct gl_buffer_object
*obj
)
874 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
880 * Plug default/fallback buffer object functions into the device
884 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
886 /* GL_ARB_vertex/pixel_buffer_object */
887 driver
->NewBufferObject
= _mesa_new_buffer_object
;
888 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
889 driver
->BindBuffer
= NULL
;
890 driver
->BufferData
= _mesa_buffer_data
;
891 driver
->BufferSubData
= _mesa_buffer_subdata
;
892 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
893 driver
->MapBuffer
= _mesa_buffer_map
;
894 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
896 /* GL_ARB_map_buffer_range */
897 driver
->MapBufferRange
= _mesa_buffer_map_range
;
898 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
900 /* GL_ARB_copy_buffer */
901 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
906 /**********************************************************************/
908 /**********************************************************************/
911 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
913 GET_CURRENT_CONTEXT(ctx
);
914 ASSERT_OUTSIDE_BEGIN_END(ctx
);
916 bind_buffer_object(ctx
, target
, buffer
);
921 * Delete a set of buffer objects.
923 * \param n Number of buffer objects to delete.
924 * \param ids Array of \c n buffer object IDs.
927 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
929 GET_CURRENT_CONTEXT(ctx
);
931 ASSERT_OUTSIDE_BEGIN_END(ctx
);
934 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
938 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
940 for (i
= 0; i
< n
; i
++) {
941 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
943 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
946 ASSERT(bufObj
->Name
== ids
[i
]);
948 if (_mesa_bufferobj_mapped(bufObj
)) {
949 /* if mapped, unmap it now */
950 ctx
->Driver
.UnmapBuffer(ctx
, 0, bufObj
);
951 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
952 bufObj
->Pointer
= NULL
;
955 /* unbind any vertex pointers bound to this buffer */
956 unbind(ctx
, &arrayObj
->Vertex
.BufferObj
, bufObj
);
957 unbind(ctx
, &arrayObj
->Weight
.BufferObj
, bufObj
);
958 unbind(ctx
, &arrayObj
->Normal
.BufferObj
, bufObj
);
959 unbind(ctx
, &arrayObj
->Color
.BufferObj
, bufObj
);
960 unbind(ctx
, &arrayObj
->SecondaryColor
.BufferObj
, bufObj
);
961 unbind(ctx
, &arrayObj
->FogCoord
.BufferObj
, bufObj
);
962 unbind(ctx
, &arrayObj
->Index
.BufferObj
, bufObj
);
963 unbind(ctx
, &arrayObj
->EdgeFlag
.BufferObj
, bufObj
);
964 for (j
= 0; j
< Elements(arrayObj
->TexCoord
); j
++) {
965 unbind(ctx
, &arrayObj
->TexCoord
[j
].BufferObj
, bufObj
);
967 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
968 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
971 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
972 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
974 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
975 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
978 /* unbind any pixel pack/unpack pointers bound to this buffer */
979 if (ctx
->Pack
.BufferObj
== bufObj
) {
980 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
982 if (ctx
->Unpack
.BufferObj
== bufObj
) {
983 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
986 /* The ID is immediately freed for re-use */
987 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, bufObj
->Name
);
988 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
992 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
997 * Generate a set of unique buffer object IDs and store them in \c buffer.
999 * \param n Number of IDs to generate.
1000 * \param buffer Array of \c n locations to store the IDs.
1003 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
1005 GET_CURRENT_CONTEXT(ctx
);
1008 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1011 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
1020 * This must be atomic (generation and allocation of buffer object IDs)
1022 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1024 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1026 /* Allocate new, empty buffer objects and return identifiers */
1027 for (i
= 0; i
< n
; i
++) {
1028 struct gl_buffer_object
*bufObj
;
1029 GLuint name
= first
+ i
;
1031 bufObj
= ctx
->Driver
.NewBufferObject( ctx
, name
, target
);
1033 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1034 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenBuffersARB");
1037 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
, bufObj
);
1038 buffer
[i
] = first
+ i
;
1041 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1046 * Determine if ID is the name of a buffer object.
1048 * \param id ID of the potential buffer object.
1049 * \return \c GL_TRUE if \c id is the name of a buffer object,
1050 * \c GL_FALSE otherwise.
1052 GLboolean GLAPIENTRY
1053 _mesa_IsBufferARB(GLuint id
)
1055 struct gl_buffer_object
*bufObj
;
1056 GET_CURRENT_CONTEXT(ctx
);
1057 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1059 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1060 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1061 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1063 return bufObj
? GL_TRUE
: GL_FALSE
;
1068 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
1069 const GLvoid
* data
, GLenum usage
)
1071 GET_CURRENT_CONTEXT(ctx
);
1072 struct gl_buffer_object
*bufObj
;
1073 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1076 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1081 case GL_STREAM_DRAW_ARB
:
1082 case GL_STREAM_READ_ARB
:
1083 case GL_STREAM_COPY_ARB
:
1084 case GL_STATIC_DRAW_ARB
:
1085 case GL_STATIC_READ_ARB
:
1086 case GL_STATIC_COPY_ARB
:
1087 case GL_DYNAMIC_DRAW_ARB
:
1088 case GL_DYNAMIC_READ_ARB
:
1089 case GL_DYNAMIC_COPY_ARB
:
1093 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
1097 bufObj
= get_buffer(ctx
, target
);
1099 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
1102 if (!_mesa_is_bufferobj(bufObj
)) {
1103 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer 0)" );
1107 if (_mesa_bufferobj_mapped(bufObj
)) {
1108 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1109 ctx
->Driver
.UnmapBuffer(ctx
, target
, bufObj
);
1110 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1111 ASSERT(bufObj
->Pointer
== NULL
);
1114 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1116 bufObj
->Written
= GL_TRUE
;
1119 _mesa_printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1120 bufObj
->Name
, size
, data
, usage
);
1127 ASSERT(ctx
->Driver
.BufferData
);
1128 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1129 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB(access)");
1135 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1136 GLsizeiptrARB size
, const GLvoid
* data
)
1138 GET_CURRENT_CONTEXT(ctx
);
1139 struct gl_buffer_object
*bufObj
;
1140 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1142 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1143 "glBufferSubDataARB" );
1145 /* error already recorded */
1149 bufObj
->Written
= GL_TRUE
;
1151 ASSERT(ctx
->Driver
.BufferSubData
);
1152 ctx
->Driver
.BufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1157 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1158 GLsizeiptrARB size
, void * data
)
1160 GET_CURRENT_CONTEXT(ctx
);
1161 struct gl_buffer_object
*bufObj
;
1162 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1164 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1165 "glGetBufferSubDataARB" );
1167 /* error already recorded */
1171 ASSERT(ctx
->Driver
.GetBufferSubData
);
1172 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1177 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1179 GET_CURRENT_CONTEXT(ctx
);
1180 struct gl_buffer_object
* bufObj
;
1181 GLbitfield accessFlags
;
1184 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1187 case GL_READ_ONLY_ARB
:
1188 accessFlags
= GL_MAP_READ_BIT
;
1190 case GL_WRITE_ONLY_ARB
:
1191 accessFlags
= GL_MAP_WRITE_BIT
;
1193 case GL_READ_WRITE_ARB
:
1194 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1197 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1201 bufObj
= get_buffer(ctx
, target
);
1203 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
1206 if (!_mesa_is_bufferobj(bufObj
)) {
1207 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(buffer 0)" );
1210 if (_mesa_bufferobj_mapped(bufObj
)) {
1211 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1215 ASSERT(ctx
->Driver
.MapBuffer
);
1216 map
= ctx
->Driver
.MapBuffer( ctx
, target
, access
, bufObj
);
1218 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(access)");
1222 /* The driver callback should have set these fields.
1223 * This is important because other modules (like VBO) might call
1224 * the driver function directly.
1226 ASSERT(bufObj
->Pointer
== map
);
1227 ASSERT(bufObj
->Length
== bufObj
->Size
);
1228 ASSERT(bufObj
->Offset
== 0);
1229 bufObj
->AccessFlags
= accessFlags
;
1232 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1233 bufObj
->Written
= GL_TRUE
;
1236 _mesa_printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1237 bufObj
->Name
, bufObj
->Size
, access
);
1238 if (access
== GL_WRITE_ONLY_ARB
) {
1240 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1241 for (i
= 0; i
< bufObj
->Size
; i
++)
1248 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1250 /* buffer is 100 bytes larger than requested, fill with magic value */
1251 for (i
= 0; i
< 100; i
++) {
1252 buf
[bufObj
->Size
- i
- 1] = 123;
1257 return bufObj
->Pointer
;
1261 GLboolean GLAPIENTRY
1262 _mesa_UnmapBufferARB(GLenum target
)
1264 GET_CURRENT_CONTEXT(ctx
);
1265 struct gl_buffer_object
*bufObj
;
1266 GLboolean status
= GL_TRUE
;
1267 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1269 bufObj
= get_buffer(ctx
, target
);
1271 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1274 if (!_mesa_is_bufferobj(bufObj
)) {
1275 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1278 if (!_mesa_bufferobj_mapped(bufObj
)) {
1279 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1284 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1285 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1287 /* check that last 100 bytes are still = magic value */
1288 for (i
= 0; i
< 100; i
++) {
1289 GLuint pos
= bufObj
->Size
- i
- 1;
1290 if (buf
[pos
] != 123) {
1291 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1292 " at position %d (value = %u)\n",
1300 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1301 GLuint i
, unchanged
= 0;
1302 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1304 /* check which bytes changed */
1305 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1306 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1313 _mesa_printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1314 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1319 status
= ctx
->Driver
.UnmapBuffer( ctx
, target
, bufObj
);
1320 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1321 ASSERT(bufObj
->Pointer
== NULL
);
1322 ASSERT(bufObj
->Offset
== 0);
1323 ASSERT(bufObj
->Length
== 0);
1330 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1332 GET_CURRENT_CONTEXT(ctx
);
1333 struct gl_buffer_object
*bufObj
;
1334 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1336 bufObj
= get_buffer(ctx
, target
);
1338 _mesa_error(ctx
, GL_INVALID_ENUM
, "GetBufferParameterivARB(target)" );
1341 if (!_mesa_is_bufferobj(bufObj
)) {
1342 _mesa_error(ctx
, GL_INVALID_OPERATION
, "GetBufferParameterivARB" );
1347 case GL_BUFFER_SIZE_ARB
:
1348 *params
= (GLint
) bufObj
->Size
;
1350 case GL_BUFFER_USAGE_ARB
:
1351 *params
= bufObj
->Usage
;
1353 case GL_BUFFER_ACCESS_ARB
:
1354 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1356 case GL_BUFFER_MAPPED_ARB
:
1357 *params
= _mesa_bufferobj_mapped(bufObj
);
1360 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname)");
1367 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1369 GET_CURRENT_CONTEXT(ctx
);
1370 struct gl_buffer_object
* bufObj
;
1371 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1373 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1374 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1378 bufObj
= get_buffer(ctx
, target
);
1380 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1383 if (!_mesa_is_bufferobj(bufObj
)) {
1384 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1388 *params
= bufObj
->Pointer
;
1393 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1394 GLintptr readOffset
, GLintptr writeOffset
,
1397 GET_CURRENT_CONTEXT(ctx
);
1398 struct gl_buffer_object
*src
, *dst
;
1399 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1401 src
= get_buffer(ctx
, readTarget
);
1402 if (!src
|| !_mesa_is_bufferobj(src
)) {
1403 _mesa_error(ctx
, GL_INVALID_ENUM
,
1404 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget
);
1408 dst
= get_buffer(ctx
, writeTarget
);
1409 if (!dst
|| !_mesa_is_bufferobj(dst
)) {
1410 _mesa_error(ctx
, GL_INVALID_ENUM
,
1411 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget
);
1415 if (_mesa_bufferobj_mapped(src
)) {
1416 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1417 "glCopyBuffserSubData(readBuffer is mapped)");
1421 if (_mesa_bufferobj_mapped(dst
)) {
1422 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1423 "glCopyBuffserSubData(writeBuffer is mapped)");
1427 if (readOffset
< 0) {
1428 _mesa_error(ctx
, GL_INVALID_VALUE
,
1429 "glCopyBuffserSubData(readOffset = %d)", readOffset
);
1433 if (writeOffset
< 0) {
1434 _mesa_error(ctx
, GL_INVALID_VALUE
,
1435 "glCopyBuffserSubData(writeOffset = %d)", writeOffset
);
1439 if (readOffset
+ size
> src
->Size
) {
1440 _mesa_error(ctx
, GL_INVALID_VALUE
,
1441 "glCopyBuffserSubData(readOffset + size = %d)",
1446 if (writeOffset
+ size
> dst
->Size
) {
1447 _mesa_error(ctx
, GL_INVALID_VALUE
,
1448 "glCopyBuffserSubData(writeOffset + size = %d)",
1454 if (readOffset
+ size
<= writeOffset
) {
1457 else if (writeOffset
+ size
<= readOffset
) {
1461 /* overlapping src/dst is illegal */
1462 _mesa_error(ctx
, GL_INVALID_VALUE
,
1463 "glCopyBuffserSubData(overlapping src/dst)");
1468 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1473 * See GL_ARB_map_buffer_range spec
1476 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1479 GET_CURRENT_CONTEXT(ctx
);
1480 struct gl_buffer_object
*bufObj
;
1483 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1485 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1486 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1487 "glMapBufferRange(extension not supported)");
1492 _mesa_error(ctx
, GL_INVALID_VALUE
,
1493 "glMapBufferRange(offset = %ld)", offset
);
1498 _mesa_error(ctx
, GL_INVALID_VALUE
,
1499 "glMapBufferRange(length = %ld)", length
);
1503 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1504 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1505 "glMapBufferRange(access indicates neither read or write)");
1509 if (access
& GL_MAP_READ_BIT
) {
1510 if ((access
& GL_MAP_INVALIDATE_RANGE_BIT
) ||
1511 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) ||
1512 (access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
1513 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1514 "glMapBufferRange(invalid access flags)");
1519 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1520 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1521 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1522 "glMapBufferRange(invalid access flags)");
1526 bufObj
= get_buffer(ctx
, target
);
1527 if (!bufObj
|| !_mesa_is_bufferobj(bufObj
)) {
1528 _mesa_error(ctx
, GL_INVALID_ENUM
,
1529 "glMapBufferRange(target = 0x%x)", target
);
1533 if (offset
+ length
> bufObj
->Size
) {
1534 _mesa_error(ctx
, GL_INVALID_VALUE
,
1535 "glMapBufferRange(offset + length > size)");
1539 if (_mesa_bufferobj_mapped(bufObj
)) {
1540 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1541 "glMapBufferRange(buffer already mapped)");
1545 ASSERT(ctx
->Driver
.MapBufferRange
);
1546 map
= ctx
->Driver
.MapBufferRange(ctx
, target
, offset
, length
,
1549 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(access)");
1552 /* The driver callback should have set all these fields.
1553 * This is important because other modules (like VBO) might call
1554 * the driver function directly.
1556 ASSERT(bufObj
->Pointer
== map
);
1557 ASSERT(bufObj
->Length
== length
);
1558 ASSERT(bufObj
->Offset
== offset
);
1559 ASSERT(bufObj
->AccessFlags
== access
);
1567 * See GL_ARB_map_buffer_range spec
1570 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1572 GET_CURRENT_CONTEXT(ctx
);
1573 struct gl_buffer_object
*bufObj
;
1574 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1576 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1577 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1578 "glMapBufferRange(extension not supported)");
1583 _mesa_error(ctx
, GL_INVALID_VALUE
,
1584 "glMapBufferRange(offset = %ld)", offset
);
1589 _mesa_error(ctx
, GL_INVALID_VALUE
,
1590 "glMapBufferRange(length = %ld)", length
);
1594 bufObj
= get_buffer(ctx
, target
);
1596 _mesa_error(ctx
, GL_INVALID_ENUM
,
1597 "glMapBufferRange(target = 0x%x)", target
);
1601 if (!_mesa_is_bufferobj(bufObj
)) {
1602 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1603 "glMapBufferRange(current buffer is 0)");
1607 if (!_mesa_bufferobj_mapped(bufObj
)) {
1608 /* buffer is not mapped */
1609 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1610 "glMapBufferRange(buffer is not mapped)");
1614 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1615 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1616 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1620 if (offset
+ length
> bufObj
->Length
) {
1621 _mesa_error(ctx
, GL_INVALID_VALUE
,
1622 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1623 offset
, length
, bufObj
->Length
);
1627 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1629 if (ctx
->Driver
.FlushMappedBufferRange
)
1630 ctx
->Driver
.FlushMappedBufferRange(ctx
, target
, offset
, length
, bufObj
);