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
40 #include "bufferobj.h"
42 #include "mfeatures.h"
49 /*#define BOUNDS_CHECK*/
52 #if FEATURE_OES_mapbuffer
53 #define DEFAULT_ACCESS GL_MAP_WRITE_BIT
55 #define DEFAULT_ACCESS (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)
60 * Used as a placeholder for buffer objects between glGenBuffers() and
61 * glBindBuffer() so that glIsBuffer() can work correctly.
63 static struct gl_buffer_object DummyBufferObject
;
67 * Return pointer to address of a buffer object target.
68 * \param ctx the GL context
69 * \param target the buffer object target to be retrieved.
70 * \return pointer to pointer to the buffer object bound to \c target in the
71 * specified context or \c NULL if \c target is invalid.
73 static inline struct gl_buffer_object
**
74 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
77 case GL_ARRAY_BUFFER_ARB
:
78 return &ctx
->Array
.ArrayBufferObj
;
79 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
80 return &ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
81 case GL_PIXEL_PACK_BUFFER_EXT
:
82 return &ctx
->Pack
.BufferObj
;
83 case GL_PIXEL_UNPACK_BUFFER_EXT
:
84 return &ctx
->Unpack
.BufferObj
;
85 case GL_COPY_READ_BUFFER
:
86 return &ctx
->CopyReadBuffer
;
87 case GL_COPY_WRITE_BUFFER
:
88 return &ctx
->CopyWriteBuffer
;
89 #if FEATURE_EXT_transform_feedback
90 case GL_TRANSFORM_FEEDBACK_BUFFER
:
91 if (ctx
->Extensions
.EXT_transform_feedback
) {
92 return &ctx
->TransformFeedback
.CurrentBuffer
;
96 case GL_TEXTURE_BUFFER
:
97 if (ctx
->Extensions
.ARB_texture_buffer_object
) {
98 return &ctx
->Texture
.BufferObject
;
109 * Get the buffer object bound to the specified target in a GL context.
110 * \param ctx the GL context
111 * \param target the buffer object target to be retrieved.
112 * \return pointer to the buffer object bound to \c target in the
113 * specified context or \c NULL if \c target is invalid.
115 static inline struct gl_buffer_object
*
116 get_buffer(struct gl_context
*ctx
, GLenum target
)
118 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
126 * Convert a GLbitfield describing the mapped buffer access flags
127 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
130 simplified_access_mode(GLbitfield access
)
132 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
133 if ((access
& rwFlags
) == rwFlags
)
134 return GL_READ_WRITE
;
135 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
137 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
138 return GL_WRITE_ONLY
;
139 return GL_READ_WRITE
; /* this should never happen, but no big deal */
144 * Tests the subdata range parameters and sets the GL error code for
145 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
147 * \param ctx GL context.
148 * \param target Buffer object target on which to operate.
149 * \param offset Offset of the first byte of the subdata range.
150 * \param size Size, in bytes, of the subdata range.
151 * \param caller Name of calling function for recording errors.
152 * \return A pointer to the buffer object bound to \c target in the
153 * specified context or \c NULL if any of the parameter or state
154 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
157 * \sa glBufferSubDataARB, glGetBufferSubDataARB
159 static struct gl_buffer_object
*
160 buffer_object_subdata_range_good( struct gl_context
* ctx
, GLenum target
,
161 GLintptrARB offset
, GLsizeiptrARB size
,
164 struct gl_buffer_object
*bufObj
;
167 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
172 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
176 bufObj
= get_buffer(ctx
, target
);
178 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", caller
);
181 if (!_mesa_is_bufferobj(bufObj
)) {
182 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
185 if (offset
+ size
> bufObj
->Size
) {
186 _mesa_error(ctx
, GL_INVALID_VALUE
,
187 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
188 (unsigned long) offset
,
189 (unsigned long) size
,
190 (unsigned long) bufObj
->Size
);
193 if (_mesa_bufferobj_mapped(bufObj
)) {
194 /* Buffer is currently mapped */
195 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
204 * Allocate and initialize a new buffer object.
206 * Default callback for the \c dd_function_table::NewBufferObject() hook.
208 static struct gl_buffer_object
*
209 _mesa_new_buffer_object( struct gl_context
*ctx
, GLuint name
, GLenum target
)
211 struct gl_buffer_object
*obj
;
215 obj
= MALLOC_STRUCT(gl_buffer_object
);
216 _mesa_initialize_buffer_object(obj
, name
, target
);
222 * Delete a buffer object.
224 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
227 _mesa_delete_buffer_object(struct gl_context
*ctx
,
228 struct gl_buffer_object
*bufObj
)
235 /* assign strange values here to help w/ debugging */
236 bufObj
->RefCount
= -1000;
239 _glthread_DESTROY_MUTEX(bufObj
->Mutex
);
246 * Set ptr to bufObj w/ reference counting.
247 * This is normally only called from the _mesa_reference_buffer_object() macro
248 * when there's a real pointer change.
251 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
252 struct gl_buffer_object
**ptr
,
253 struct gl_buffer_object
*bufObj
)
256 /* Unreference the old buffer */
257 GLboolean deleteFlag
= GL_FALSE
;
258 struct gl_buffer_object
*oldObj
= *ptr
;
260 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
261 ASSERT(oldObj
->RefCount
> 0);
264 printf("BufferObj %p %d DECR to %d\n",
265 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
267 deleteFlag
= (oldObj
->RefCount
== 0);
268 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
272 /* some sanity checking: don't delete a buffer still in use */
274 /* unfortunately, these tests are invalid during context tear-down */
275 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
276 ASSERT(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
!= bufObj
);
277 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
280 ASSERT(ctx
->Driver
.DeleteBuffer
);
281 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
289 /* reference new buffer */
290 _glthread_LOCK_MUTEX(bufObj
->Mutex
);
291 if (bufObj
->RefCount
== 0) {
292 /* this buffer's being deleted (look just above) */
293 /* Not sure this can every really happen. Warn if it does. */
294 _mesa_problem(NULL
, "referencing deleted buffer object");
300 printf("BufferObj %p %d INCR to %d\n",
301 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
305 _glthread_UNLOCK_MUTEX(bufObj
->Mutex
);
311 * Initialize a buffer object to default values.
314 _mesa_initialize_buffer_object( struct gl_buffer_object
*obj
,
315 GLuint name
, GLenum target
)
319 memset(obj
, 0, sizeof(struct gl_buffer_object
));
320 _glthread_INIT_MUTEX(obj
->Mutex
);
323 obj
->Usage
= GL_STATIC_DRAW_ARB
;
324 obj
->AccessFlags
= DEFAULT_ACCESS
;
329 * Allocate space for and store data in a buffer object. Any data that was
330 * previously stored in the buffer object is lost. If \c data is \c NULL,
331 * memory will be allocated, but no copy will occur.
333 * This is the default callback for \c dd_function_table::BufferData()
334 * Note that all GL error checking will have been done already.
336 * \param ctx GL context.
337 * \param target Buffer object target on which to operate.
338 * \param size Size, in bytes, of the new data store.
339 * \param data Pointer to the data to store in the buffer object. This
340 * pointer may be \c NULL.
341 * \param usage Hints about how the data will be used.
342 * \param bufObj Object to be used.
344 * \return GL_TRUE for success, GL_FALSE for failure
345 * \sa glBufferDataARB, dd_function_table::BufferData.
348 _mesa_buffer_data( struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
349 const GLvoid
* data
, GLenum usage
,
350 struct gl_buffer_object
* bufObj
)
354 (void) ctx
; (void) target
;
356 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
358 bufObj
->Data
= (GLubyte
*) new_data
;
360 bufObj
->Usage
= usage
;
363 memcpy( bufObj
->Data
, data
, size
);
375 * Replace data in 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::BufferSubData()
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 modified.
385 * \param size Size, in bytes, of the data range.
386 * \param data Pointer to the data to store in the buffer object.
387 * \param bufObj Object to be used.
389 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
392 _mesa_buffer_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
393 GLsizeiptrARB size
, const GLvoid
* data
,
394 struct gl_buffer_object
* bufObj
)
398 /* this should have been caught in _mesa_BufferSubData() */
399 ASSERT(size
+ offset
<= bufObj
->Size
);
402 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
408 * Retrieve data from a subrange of buffer object. If the data range
409 * specified by \c size + \c offset extends beyond the end of the buffer or
410 * if \c data is \c NULL, no copy is performed.
412 * This is the default callback for \c dd_function_table::GetBufferSubData()
413 * Note that all GL error checking will have been done already.
415 * \param ctx GL context.
416 * \param target Buffer object target on which to operate.
417 * \param offset Offset of the first byte to be fetched.
418 * \param size Size, in bytes, of the data range.
419 * \param data Destination for data
420 * \param bufObj Object to be used.
422 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
425 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
426 GLsizeiptrARB size
, GLvoid
* data
,
427 struct gl_buffer_object
* bufObj
)
431 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
432 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
438 * Default fallback for \c dd_function_table::MapBufferRange().
439 * Called via glMapBufferRange().
442 _mesa_buffer_map_range( struct gl_context
*ctx
, GLintptr offset
,
443 GLsizeiptr length
, GLbitfield access
,
444 struct gl_buffer_object
*bufObj
)
447 assert(!_mesa_bufferobj_mapped(bufObj
));
448 /* Just return a direct pointer to the data */
449 bufObj
->Pointer
= bufObj
->Data
+ offset
;
450 bufObj
->Length
= length
;
451 bufObj
->Offset
= offset
;
452 bufObj
->AccessFlags
= access
;
453 return bufObj
->Pointer
;
458 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
459 * Called via glFlushMappedBufferRange().
462 _mesa_buffer_flush_mapped_range( struct gl_context
*ctx
,
463 GLintptr offset
, GLsizeiptr length
,
464 struct gl_buffer_object
*obj
)
475 * Default callback for \c dd_function_table::MapBuffer().
477 * The input parameters will have been already tested for errors.
479 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
482 _mesa_buffer_unmap( struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
485 /* XXX we might assert here that bufObj->Pointer is non-null */
486 bufObj
->Pointer
= NULL
;
489 bufObj
->AccessFlags
= 0x0;
495 * Default fallback for \c dd_function_table::CopyBufferSubData().
496 * Called via glCopyBuffserSubData().
499 _mesa_copy_buffer_subdata(struct gl_context
*ctx
,
500 struct gl_buffer_object
*src
,
501 struct gl_buffer_object
*dst
,
502 GLintptr readOffset
, GLintptr writeOffset
,
505 void *srcPtr
, *dstPtr
;
507 /* buffer should not already be mapped */
508 assert(!_mesa_bufferobj_mapped(src
));
509 assert(!_mesa_bufferobj_mapped(dst
));
511 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
512 GL_MAP_READ_BIT
, src
);
513 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
515 GL_MAP_INVALIDATE_RANGE_BIT
), dst
);
517 if (srcPtr
&& dstPtr
)
518 memcpy(dstPtr
, srcPtr
, size
);
520 ctx
->Driver
.UnmapBuffer(ctx
, src
);
521 ctx
->Driver
.UnmapBuffer(ctx
, dst
);
527 * Initialize the state associated with buffer objects
530 _mesa_init_buffer_objects( struct gl_context
*ctx
)
532 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
533 _glthread_INIT_MUTEX(DummyBufferObject
.Mutex
);
534 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
536 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
537 ctx
->Shared
->NullBufferObj
);
539 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
540 ctx
->Shared
->NullBufferObj
);
541 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
542 ctx
->Shared
->NullBufferObj
);
547 _mesa_free_buffer_objects( struct gl_context
*ctx
)
549 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
551 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
552 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
557 * Bind the specified target to buffer for the specified context.
558 * Called by glBindBuffer() and other functions.
561 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
563 struct gl_buffer_object
*oldBufObj
;
564 struct gl_buffer_object
*newBufObj
= NULL
;
565 struct gl_buffer_object
**bindTarget
= NULL
;
567 bindTarget
= get_buffer_target(ctx
, target
);
569 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
573 /* Get pointer to old buffer object (to be unbound) */
574 oldBufObj
= *bindTarget
;
575 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
576 return; /* rebinding the same buffer object- no change */
579 * Get pointer to new buffer object (newBufObj)
582 /* The spec says there's not a buffer object named 0, but we use
583 * one internally because it simplifies things.
585 newBufObj
= ctx
->Shared
->NullBufferObj
;
588 /* non-default buffer object */
589 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
590 if (!newBufObj
|| newBufObj
== &DummyBufferObject
) {
591 /* If this is a new buffer object id, or one which was generated but
592 * never used before, allocate a buffer object now.
594 ASSERT(ctx
->Driver
.NewBufferObject
);
595 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
597 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
600 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
604 /* bind new buffer */
605 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
607 /* Pass BindBuffer call to device driver */
608 if (ctx
->Driver
.BindBuffer
)
609 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
614 * Update the default buffer objects in the given context to reference those
615 * specified in the shared state and release those referencing the old
619 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
621 /* Bind the NullBufferObj to remove references to those
622 * in the shared context hash table.
624 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
625 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
626 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
627 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
633 * Return the gl_buffer_object for the given ID.
634 * Always return NULL for ID 0.
636 struct gl_buffer_object
*
637 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
642 return (struct gl_buffer_object
*)
643 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
648 * If *ptr points to obj, set ptr = the Null/default buffer object.
649 * This is a helper for buffer object deletion.
650 * The GL spec says that deleting a buffer object causes it to get
651 * unbound from all arrays in the current context.
654 unbind(struct gl_context
*ctx
,
655 struct gl_buffer_object
**ptr
,
656 struct gl_buffer_object
*obj
)
659 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
665 * Plug default/fallback buffer object functions into the device
669 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
671 /* GL_ARB_vertex/pixel_buffer_object */
672 driver
->NewBufferObject
= _mesa_new_buffer_object
;
673 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
674 driver
->BindBuffer
= NULL
;
675 driver
->BufferData
= _mesa_buffer_data
;
676 driver
->BufferSubData
= _mesa_buffer_subdata
;
677 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
678 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
680 /* GL_ARB_map_buffer_range */
681 driver
->MapBufferRange
= _mesa_buffer_map_range
;
682 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
684 /* GL_ARB_copy_buffer */
685 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
690 /**********************************************************************/
692 /**********************************************************************/
695 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
697 GET_CURRENT_CONTEXT(ctx
);
698 ASSERT_OUTSIDE_BEGIN_END(ctx
);
700 if (MESA_VERBOSE
& VERBOSE_API
)
701 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
702 _mesa_lookup_enum_by_nr(target
), buffer
);
704 bind_buffer_object(ctx
, target
, buffer
);
709 * Delete a set of buffer objects.
711 * \param n Number of buffer objects to delete.
712 * \param ids Array of \c n buffer object IDs.
715 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
717 GET_CURRENT_CONTEXT(ctx
);
719 ASSERT_OUTSIDE_BEGIN_END(ctx
);
720 FLUSH_VERTICES(ctx
, 0);
723 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
727 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
729 for (i
= 0; i
< n
; i
++) {
730 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
732 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
735 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
737 if (_mesa_bufferobj_mapped(bufObj
)) {
738 /* if mapped, unmap it now */
739 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
740 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
741 bufObj
->Pointer
= NULL
;
744 /* unbind any vertex pointers bound to this buffer */
745 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
746 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
749 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
750 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
752 if (arrayObj
->ElementArrayBufferObj
== bufObj
) {
753 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
756 /* unbind any pixel pack/unpack pointers bound to this buffer */
757 if (ctx
->Pack
.BufferObj
== bufObj
) {
758 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
760 if (ctx
->Unpack
.BufferObj
== bufObj
) {
761 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
764 /* The ID is immediately freed for re-use */
765 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
766 /* Make sure we do not run into the classic ABA problem on bind.
767 * We don't want to allow re-binding a buffer object that's been
768 * "deleted" by glDeleteBuffers().
770 * The explicit rebinding to the default object in the current context
771 * prevents the above in the current context, but another context
772 * sharing the same objects might suffer from this problem.
773 * The alternative would be to do the hash lookup in any case on bind
774 * which would introduce more runtime overhead than this.
776 bufObj
->DeletePending
= GL_TRUE
;
777 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
781 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
786 * Generate a set of unique buffer object IDs and store them in \c buffer.
788 * \param n Number of IDs to generate.
789 * \param buffer Array of \c n locations to store the IDs.
792 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
794 GET_CURRENT_CONTEXT(ctx
);
797 ASSERT_OUTSIDE_BEGIN_END(ctx
);
799 if (MESA_VERBOSE
& VERBOSE_API
)
800 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
803 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
812 * This must be atomic (generation and allocation of buffer object IDs)
814 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
816 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
818 /* Insert the ID and pointer to dummy buffer object into hash table */
819 for (i
= 0; i
< n
; i
++) {
820 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
822 buffer
[i
] = first
+ i
;
825 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
830 * Determine if ID is the name of a buffer object.
832 * \param id ID of the potential buffer object.
833 * \return \c GL_TRUE if \c id is the name of a buffer object,
834 * \c GL_FALSE otherwise.
837 _mesa_IsBufferARB(GLuint id
)
839 struct gl_buffer_object
*bufObj
;
840 GET_CURRENT_CONTEXT(ctx
);
841 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
843 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
844 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
845 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
847 return bufObj
&& bufObj
!= &DummyBufferObject
;
852 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
853 const GLvoid
* data
, GLenum usage
)
855 GET_CURRENT_CONTEXT(ctx
);
856 struct gl_buffer_object
*bufObj
;
857 ASSERT_OUTSIDE_BEGIN_END(ctx
);
859 if (MESA_VERBOSE
& VERBOSE_API
)
860 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
861 _mesa_lookup_enum_by_nr(target
),
862 (long int) size
, data
,
863 _mesa_lookup_enum_by_nr(usage
));
866 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
871 case GL_STREAM_DRAW_ARB
:
872 case GL_STREAM_READ_ARB
:
873 case GL_STREAM_COPY_ARB
:
874 case GL_STATIC_DRAW_ARB
:
875 case GL_STATIC_READ_ARB
:
876 case GL_STATIC_COPY_ARB
:
877 case GL_DYNAMIC_DRAW_ARB
:
878 case GL_DYNAMIC_READ_ARB
:
879 case GL_DYNAMIC_COPY_ARB
:
883 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
887 bufObj
= get_buffer(ctx
, target
);
889 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
892 if (!_mesa_is_bufferobj(bufObj
)) {
893 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer 0)" );
897 if (_mesa_bufferobj_mapped(bufObj
)) {
898 /* Unmap the existing buffer. We'll replace it now. Not an error. */
899 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
900 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
901 ASSERT(bufObj
->Pointer
== NULL
);
904 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
906 bufObj
->Written
= GL_TRUE
;
909 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
910 bufObj
->Name
, size
, data
, usage
);
917 ASSERT(ctx
->Driver
.BufferData
);
918 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
919 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
925 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
926 GLsizeiptrARB size
, const GLvoid
* data
)
928 GET_CURRENT_CONTEXT(ctx
);
929 struct gl_buffer_object
*bufObj
;
930 ASSERT_OUTSIDE_BEGIN_END(ctx
);
932 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
933 "glBufferSubDataARB" );
935 /* error already recorded */
942 bufObj
->Written
= GL_TRUE
;
944 ASSERT(ctx
->Driver
.BufferSubData
);
945 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
950 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
951 GLsizeiptrARB size
, void * data
)
953 GET_CURRENT_CONTEXT(ctx
);
954 struct gl_buffer_object
*bufObj
;
955 ASSERT_OUTSIDE_BEGIN_END(ctx
);
957 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
958 "glGetBufferSubDataARB" );
960 /* error already recorded */
964 ASSERT(ctx
->Driver
.GetBufferSubData
);
965 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
970 _mesa_MapBufferARB(GLenum target
, GLenum access
)
972 GET_CURRENT_CONTEXT(ctx
);
973 struct gl_buffer_object
* bufObj
;
974 GLbitfield accessFlags
;
977 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
980 case GL_READ_ONLY_ARB
:
981 accessFlags
= GL_MAP_READ_BIT
;
983 case GL_WRITE_ONLY_ARB
:
984 accessFlags
= GL_MAP_WRITE_BIT
;
986 case GL_READ_WRITE_ARB
:
987 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
990 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
994 bufObj
= get_buffer(ctx
, target
);
996 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
999 if (!_mesa_is_bufferobj(bufObj
)) {
1000 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(buffer 0)" );
1003 if (_mesa_bufferobj_mapped(bufObj
)) {
1004 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1008 if (!bufObj
->Size
) {
1009 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1010 "glMapBuffer(buffer size = 0)");
1014 ASSERT(ctx
->Driver
.MapBufferRange
);
1015 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
);
1017 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1021 /* The driver callback should have set these fields.
1022 * This is important because other modules (like VBO) might call
1023 * the driver function directly.
1025 ASSERT(bufObj
->Pointer
== map
);
1026 ASSERT(bufObj
->Length
== bufObj
->Size
);
1027 ASSERT(bufObj
->Offset
== 0);
1028 bufObj
->AccessFlags
= accessFlags
;
1031 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1032 bufObj
->Written
= GL_TRUE
;
1035 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1036 bufObj
->Name
, bufObj
->Size
, access
);
1037 if (access
== GL_WRITE_ONLY_ARB
) {
1039 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1040 for (i
= 0; i
< bufObj
->Size
; i
++)
1047 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1049 /* buffer is 100 bytes larger than requested, fill with magic value */
1050 for (i
= 0; i
< 100; i
++) {
1051 buf
[bufObj
->Size
- i
- 1] = 123;
1056 return bufObj
->Pointer
;
1060 GLboolean GLAPIENTRY
1061 _mesa_UnmapBufferARB(GLenum target
)
1063 GET_CURRENT_CONTEXT(ctx
);
1064 struct gl_buffer_object
*bufObj
;
1065 GLboolean status
= GL_TRUE
;
1066 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1068 bufObj
= get_buffer(ctx
, target
);
1070 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1073 if (!_mesa_is_bufferobj(bufObj
)) {
1074 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1077 if (!_mesa_bufferobj_mapped(bufObj
)) {
1078 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1083 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1084 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1086 /* check that last 100 bytes are still = magic value */
1087 for (i
= 0; i
< 100; i
++) {
1088 GLuint pos
= bufObj
->Size
- i
- 1;
1089 if (buf
[pos
] != 123) {
1090 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1091 " at position %d (value = %u)\n",
1099 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1100 GLuint i
, unchanged
= 0;
1101 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1103 /* check which bytes changed */
1104 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1105 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1112 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1113 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1118 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1119 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1120 ASSERT(bufObj
->Pointer
== NULL
);
1121 ASSERT(bufObj
->Offset
== 0);
1122 ASSERT(bufObj
->Length
== 0);
1129 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1131 GET_CURRENT_CONTEXT(ctx
);
1132 struct gl_buffer_object
*bufObj
;
1133 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1135 bufObj
= get_buffer(ctx
, target
);
1137 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(target)" );
1140 if (!_mesa_is_bufferobj(bufObj
)) {
1141 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferParameterivARB" );
1146 case GL_BUFFER_SIZE_ARB
:
1147 *params
= (GLint
) bufObj
->Size
;
1149 case GL_BUFFER_USAGE_ARB
:
1150 *params
= bufObj
->Usage
;
1152 case GL_BUFFER_ACCESS_ARB
:
1153 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1155 case GL_BUFFER_MAPPED_ARB
:
1156 *params
= _mesa_bufferobj_mapped(bufObj
);
1158 case GL_BUFFER_ACCESS_FLAGS
:
1159 if (ctx
->VersionMajor
< 3)
1161 *params
= bufObj
->AccessFlags
;
1163 case GL_BUFFER_MAP_OFFSET
:
1164 if (ctx
->VersionMajor
< 3)
1166 *params
= (GLint
) bufObj
->Offset
;
1168 case GL_BUFFER_MAP_LENGTH
:
1169 if (ctx
->VersionMajor
< 3)
1171 *params
= (GLint
) bufObj
->Length
;
1174 ; /* fall-through */
1178 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1179 _mesa_lookup_enum_by_nr(pname
));
1185 * This is pretty much a duplicate of GetBufferParameteriv() but the
1186 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1189 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1191 GET_CURRENT_CONTEXT(ctx
);
1192 struct gl_buffer_object
*bufObj
;
1193 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1195 bufObj
= get_buffer(ctx
, target
);
1197 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(target)" );
1200 if (!_mesa_is_bufferobj(bufObj
)) {
1201 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferParameteri64v" );
1206 case GL_BUFFER_SIZE_ARB
:
1207 *params
= bufObj
->Size
;
1209 case GL_BUFFER_USAGE_ARB
:
1210 *params
= bufObj
->Usage
;
1212 case GL_BUFFER_ACCESS_ARB
:
1213 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1215 case GL_BUFFER_ACCESS_FLAGS
:
1216 if (ctx
->VersionMajor
< 3)
1218 *params
= bufObj
->AccessFlags
;
1220 case GL_BUFFER_MAPPED_ARB
:
1221 *params
= _mesa_bufferobj_mapped(bufObj
);
1223 case GL_BUFFER_MAP_OFFSET
:
1224 if (ctx
->VersionMajor
< 3)
1226 *params
= bufObj
->Offset
;
1228 case GL_BUFFER_MAP_LENGTH
:
1229 if (ctx
->VersionMajor
< 3)
1231 *params
= bufObj
->Length
;
1234 ; /* fall-through */
1238 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1239 _mesa_lookup_enum_by_nr(pname
));
1244 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1246 GET_CURRENT_CONTEXT(ctx
);
1247 struct gl_buffer_object
* bufObj
;
1248 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1250 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1251 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1255 bufObj
= get_buffer(ctx
, target
);
1257 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1260 if (!_mesa_is_bufferobj(bufObj
)) {
1261 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1265 *params
= bufObj
->Pointer
;
1270 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1271 GLintptr readOffset
, GLintptr writeOffset
,
1274 GET_CURRENT_CONTEXT(ctx
);
1275 struct gl_buffer_object
*src
, *dst
;
1276 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1278 src
= get_buffer(ctx
, readTarget
);
1279 if (!_mesa_is_bufferobj(src
)) {
1280 _mesa_error(ctx
, GL_INVALID_ENUM
,
1281 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget
);
1285 dst
= get_buffer(ctx
, writeTarget
);
1286 if (!_mesa_is_bufferobj(dst
)) {
1287 _mesa_error(ctx
, GL_INVALID_ENUM
,
1288 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget
);
1292 if (_mesa_bufferobj_mapped(src
)) {
1293 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1294 "glCopyBuffserSubData(readBuffer is mapped)");
1298 if (_mesa_bufferobj_mapped(dst
)) {
1299 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1300 "glCopyBuffserSubData(writeBuffer is mapped)");
1304 if (readOffset
< 0) {
1305 _mesa_error(ctx
, GL_INVALID_VALUE
,
1306 "glCopyBuffserSubData(readOffset = %d)", (int) readOffset
);
1310 if (writeOffset
< 0) {
1311 _mesa_error(ctx
, GL_INVALID_VALUE
,
1312 "glCopyBuffserSubData(writeOffset = %d)", (int) writeOffset
);
1316 if (readOffset
+ size
> src
->Size
) {
1317 _mesa_error(ctx
, GL_INVALID_VALUE
,
1318 "glCopyBuffserSubData(readOffset + size = %d)",
1319 (int) (readOffset
+ size
));
1323 if (writeOffset
+ size
> dst
->Size
) {
1324 _mesa_error(ctx
, GL_INVALID_VALUE
,
1325 "glCopyBuffserSubData(writeOffset + size = %d)",
1326 (int) (writeOffset
+ size
));
1331 if (readOffset
+ size
<= writeOffset
) {
1334 else if (writeOffset
+ size
<= readOffset
) {
1338 /* overlapping src/dst is illegal */
1339 _mesa_error(ctx
, GL_INVALID_VALUE
,
1340 "glCopyBuffserSubData(overlapping src/dst)");
1345 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1350 * See GL_ARB_map_buffer_range spec
1353 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1356 GET_CURRENT_CONTEXT(ctx
);
1357 struct gl_buffer_object
*bufObj
;
1360 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1362 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1363 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1364 "glMapBufferRange(extension not supported)");
1369 _mesa_error(ctx
, GL_INVALID_VALUE
,
1370 "glMapBufferRange(offset = %ld)", (long)offset
);
1375 _mesa_error(ctx
, GL_INVALID_VALUE
,
1376 "glMapBufferRange(length = %ld)", (long)length
);
1380 if (access
& ~(GL_MAP_READ_BIT
|
1382 GL_MAP_INVALIDATE_RANGE_BIT
|
1383 GL_MAP_INVALIDATE_BUFFER_BIT
|
1384 GL_MAP_FLUSH_EXPLICIT_BIT
|
1385 GL_MAP_UNSYNCHRONIZED_BIT
)) {
1386 /* generate an error if any undefind bit is set */
1387 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1391 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1392 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1393 "glMapBufferRange(access indicates neither read or write)");
1397 if ((access
& GL_MAP_READ_BIT
) &&
1398 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1399 GL_MAP_INVALIDATE_BUFFER_BIT
|
1400 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1401 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1402 "glMapBufferRange(invalid access flags)");
1406 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1407 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1408 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1409 "glMapBufferRange(invalid access flags)");
1413 bufObj
= get_buffer(ctx
, target
);
1414 if (!_mesa_is_bufferobj(bufObj
)) {
1415 _mesa_error(ctx
, GL_INVALID_ENUM
,
1416 "glMapBufferRange(target = 0x%x)", target
);
1420 if (offset
+ length
> bufObj
->Size
) {
1421 _mesa_error(ctx
, GL_INVALID_VALUE
,
1422 "glMapBufferRange(offset + length > size)");
1426 if (_mesa_bufferobj_mapped(bufObj
)) {
1427 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1428 "glMapBufferRange(buffer already mapped)");
1432 if (!bufObj
->Size
) {
1433 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1434 "glMapBufferRange(buffer size = 0)");
1438 /* Mapping zero bytes should return a non-null pointer. */
1440 static long dummy
= 0;
1441 bufObj
->Pointer
= &dummy
;
1442 bufObj
->Length
= length
;
1443 bufObj
->Offset
= offset
;
1444 bufObj
->AccessFlags
= access
;
1445 return bufObj
->Pointer
;
1448 ASSERT(ctx
->Driver
.MapBufferRange
);
1449 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
);
1451 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1454 /* The driver callback should have set all these fields.
1455 * This is important because other modules (like VBO) might call
1456 * the driver function directly.
1458 ASSERT(bufObj
->Pointer
== map
);
1459 ASSERT(bufObj
->Length
== length
);
1460 ASSERT(bufObj
->Offset
== offset
);
1461 ASSERT(bufObj
->AccessFlags
== access
);
1469 * See GL_ARB_map_buffer_range spec
1472 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1474 GET_CURRENT_CONTEXT(ctx
);
1475 struct gl_buffer_object
*bufObj
;
1476 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1478 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1479 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1480 "glMapBufferRange(extension not supported)");
1485 _mesa_error(ctx
, GL_INVALID_VALUE
,
1486 "glMapBufferRange(offset = %ld)", (long)offset
);
1491 _mesa_error(ctx
, GL_INVALID_VALUE
,
1492 "glMapBufferRange(length = %ld)", (long)length
);
1496 bufObj
= get_buffer(ctx
, target
);
1498 _mesa_error(ctx
, GL_INVALID_ENUM
,
1499 "glMapBufferRange(target = 0x%x)", target
);
1503 if (!_mesa_is_bufferobj(bufObj
)) {
1504 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1505 "glMapBufferRange(current buffer is 0)");
1509 if (!_mesa_bufferobj_mapped(bufObj
)) {
1510 /* buffer is not mapped */
1511 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1512 "glMapBufferRange(buffer is not mapped)");
1516 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1517 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1518 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1522 if (offset
+ length
> bufObj
->Length
) {
1523 _mesa_error(ctx
, GL_INVALID_VALUE
,
1524 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1525 (long)offset
, (long)length
, (long)bufObj
->Length
);
1529 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1531 if (ctx
->Driver
.FlushMappedBufferRange
)
1532 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
);
1536 #if FEATURE_APPLE_object_purgeable
1538 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1540 struct gl_buffer_object
*bufObj
;
1543 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1545 _mesa_error(ctx
, GL_INVALID_VALUE
,
1546 "glObjectPurgeable(name = 0x%x)", name
);
1549 if (!_mesa_is_bufferobj(bufObj
)) {
1550 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1554 if (bufObj
->Purgeable
) {
1555 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1556 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1557 return GL_VOLATILE_APPLE
;
1560 bufObj
->Purgeable
= GL_TRUE
;
1562 retval
= GL_VOLATILE_APPLE
;
1563 if (ctx
->Driver
.BufferObjectPurgeable
)
1564 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1571 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1573 struct gl_renderbuffer
*bufObj
;
1576 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1578 _mesa_error(ctx
, GL_INVALID_VALUE
,
1579 "glObjectUnpurgeable(name = 0x%x)", name
);
1583 if (bufObj
->Purgeable
) {
1584 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1585 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1586 return GL_VOLATILE_APPLE
;
1589 bufObj
->Purgeable
= GL_TRUE
;
1591 retval
= GL_VOLATILE_APPLE
;
1592 if (ctx
->Driver
.RenderObjectPurgeable
)
1593 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1600 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1602 struct gl_texture_object
*bufObj
;
1605 bufObj
= _mesa_lookup_texture(ctx
, name
);
1607 _mesa_error(ctx
, GL_INVALID_VALUE
,
1608 "glObjectPurgeable(name = 0x%x)", name
);
1612 if (bufObj
->Purgeable
) {
1613 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1614 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1615 return GL_VOLATILE_APPLE
;
1618 bufObj
->Purgeable
= GL_TRUE
;
1620 retval
= GL_VOLATILE_APPLE
;
1621 if (ctx
->Driver
.TextureObjectPurgeable
)
1622 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1629 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1633 GET_CURRENT_CONTEXT(ctx
);
1634 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1637 _mesa_error(ctx
, GL_INVALID_VALUE
,
1638 "glObjectPurgeable(name = 0x%x)", name
);
1643 case GL_VOLATILE_APPLE
:
1644 case GL_RELEASED_APPLE
:
1648 _mesa_error(ctx
, GL_INVALID_ENUM
,
1649 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1654 switch (objectType
) {
1656 retval
= texture_object_purgeable(ctx
, name
, option
);
1658 case GL_RENDERBUFFER_EXT
:
1659 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1661 case GL_BUFFER_OBJECT_APPLE
:
1662 retval
= buffer_object_purgeable(ctx
, name
, option
);
1665 _mesa_error(ctx
, GL_INVALID_ENUM
,
1666 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1671 /* In strict conformance to the spec, we must only return VOLATILE when
1672 * when passed the VOLATILE option. Madness.
1674 * XXX First fix the spec, then fix me.
1676 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1681 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1683 struct gl_buffer_object
*bufObj
;
1686 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1688 _mesa_error(ctx
, GL_INVALID_VALUE
,
1689 "glObjectUnpurgeable(name = 0x%x)", name
);
1693 if (! bufObj
->Purgeable
) {
1694 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1695 "glObjectUnpurgeable(name = 0x%x) object is "
1696 " already \"unpurged\"", name
);
1700 bufObj
->Purgeable
= GL_FALSE
;
1703 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1704 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1711 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1713 struct gl_renderbuffer
*bufObj
;
1716 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1718 _mesa_error(ctx
, GL_INVALID_VALUE
,
1719 "glObjectUnpurgeable(name = 0x%x)", name
);
1723 if (! bufObj
->Purgeable
) {
1724 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1725 "glObjectUnpurgeable(name = 0x%x) object is "
1726 " already \"unpurged\"", name
);
1730 bufObj
->Purgeable
= GL_FALSE
;
1733 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1734 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1741 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1743 struct gl_texture_object
*bufObj
;
1746 bufObj
= _mesa_lookup_texture(ctx
, name
);
1748 _mesa_error(ctx
, GL_INVALID_VALUE
,
1749 "glObjectUnpurgeable(name = 0x%x)", name
);
1753 if (! bufObj
->Purgeable
) {
1754 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1755 "glObjectUnpurgeable(name = 0x%x) object is"
1756 " already \"unpurged\"", name
);
1760 bufObj
->Purgeable
= GL_FALSE
;
1763 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1764 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1771 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1773 GET_CURRENT_CONTEXT(ctx
);
1774 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1777 _mesa_error(ctx
, GL_INVALID_VALUE
,
1778 "glObjectUnpurgeable(name = 0x%x)", name
);
1783 case GL_RETAINED_APPLE
:
1784 case GL_UNDEFINED_APPLE
:
1788 _mesa_error(ctx
, GL_INVALID_ENUM
,
1789 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1794 switch (objectType
) {
1795 case GL_BUFFER_OBJECT_APPLE
:
1796 return buffer_object_unpurgeable(ctx
, name
, option
);
1798 return texture_object_unpurgeable(ctx
, name
, option
);
1799 case GL_RENDERBUFFER_EXT
:
1800 return renderbuffer_unpurgeable(ctx
, name
, option
);
1802 _mesa_error(ctx
, GL_INVALID_ENUM
,
1803 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1811 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1812 GLenum pname
, GLint
*params
)
1814 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1816 _mesa_error(ctx
, GL_INVALID_VALUE
,
1817 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1822 case GL_PURGEABLE_APPLE
:
1823 *params
= bufObj
->Purgeable
;
1826 _mesa_error(ctx
, GL_INVALID_ENUM
,
1827 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1835 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1836 GLenum pname
, GLint
*params
)
1838 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1840 _mesa_error(ctx
, GL_INVALID_VALUE
,
1841 "glObjectUnpurgeable(name = 0x%x)", name
);
1846 case GL_PURGEABLE_APPLE
:
1847 *params
= rb
->Purgeable
;
1850 _mesa_error(ctx
, GL_INVALID_ENUM
,
1851 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1859 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1860 GLenum pname
, GLint
*params
)
1862 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1864 _mesa_error(ctx
, GL_INVALID_VALUE
,
1865 "glObjectUnpurgeable(name = 0x%x)", name
);
1870 case GL_PURGEABLE_APPLE
:
1871 *params
= texObj
->Purgeable
;
1874 _mesa_error(ctx
, GL_INVALID_ENUM
,
1875 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1883 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
1886 GET_CURRENT_CONTEXT(ctx
);
1889 _mesa_error(ctx
, GL_INVALID_VALUE
,
1890 "glGetObjectParameteriv(name = 0x%x)", name
);
1894 switch (objectType
) {
1896 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
1898 case GL_BUFFER_OBJECT_APPLE
:
1899 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
1901 case GL_RENDERBUFFER_EXT
:
1902 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
1905 _mesa_error(ctx
, GL_INVALID_ENUM
,
1906 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1911 #endif /* FEATURE_APPLE_object_purgeable */