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"
45 #include "transformfeedback.h"
50 /*#define BOUNDS_CHECK*/
54 * Used as a placeholder for buffer objects between glGenBuffers() and
55 * glBindBuffer() so that glIsBuffer() can work correctly.
57 static struct gl_buffer_object DummyBufferObject
;
61 * Return pointer to address of a buffer object target.
62 * \param ctx the GL context
63 * \param target the buffer object target to be retrieved.
64 * \return pointer to pointer to the buffer object bound to \c target in the
65 * specified context or \c NULL if \c target is invalid.
67 static inline struct gl_buffer_object
**
68 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
71 case GL_ARRAY_BUFFER_ARB
:
72 return &ctx
->Array
.ArrayBufferObj
;
73 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
74 return &ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
75 case GL_PIXEL_PACK_BUFFER_EXT
:
76 return &ctx
->Pack
.BufferObj
;
77 case GL_PIXEL_UNPACK_BUFFER_EXT
:
78 return &ctx
->Unpack
.BufferObj
;
79 case GL_COPY_READ_BUFFER
:
80 return &ctx
->CopyReadBuffer
;
81 case GL_COPY_WRITE_BUFFER
:
82 return &ctx
->CopyWriteBuffer
;
83 #if FEATURE_EXT_transform_feedback
84 case GL_TRANSFORM_FEEDBACK_BUFFER
:
85 if (ctx
->Extensions
.EXT_transform_feedback
) {
86 return &ctx
->TransformFeedback
.CurrentBuffer
;
90 case GL_TEXTURE_BUFFER
:
91 if (ctx
->Extensions
.ARB_texture_buffer_object
) {
92 return &ctx
->Texture
.BufferObject
;
103 * Get the buffer object bound to the specified target in a GL context.
104 * \param ctx the GL context
105 * \param target the buffer object target to be retrieved.
106 * \return pointer to the buffer object bound to \c target in the
107 * specified context or \c NULL if \c target is invalid.
109 static inline struct gl_buffer_object
*
110 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
)
112 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
115 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
119 if (!_mesa_is_bufferobj(*bufObj
)) {
120 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(buffer 0)", func
);
129 default_access_mode(const struct gl_context
*ctx
)
131 /* Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
133 * Name Type Initial Value Legal Values
135 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
138 * However, table 6.8 in the GL_OES_mapbuffer extension says:
140 * Get Value Type Get Command Value Description
141 * --------- ---- ----------- ----- -----------
142 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
144 * The difference is because GL_OES_mapbuffer only supports mapping buffers
147 return (ctx
->API
== API_OPENGLES
)
148 ? GL_MAP_WRITE_BIT
: (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
);
153 * Convert a GLbitfield describing the mapped buffer access flags
154 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
157 simplified_access_mode(GLbitfield access
)
159 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
160 if ((access
& rwFlags
) == rwFlags
)
161 return GL_READ_WRITE
;
162 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
164 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
165 return GL_WRITE_ONLY
;
166 return GL_READ_WRITE
; /* this should never happen, but no big deal */
171 * Tests the subdata range parameters and sets the GL error code for
172 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
174 * \param ctx GL context.
175 * \param target Buffer object target on which to operate.
176 * \param offset Offset of the first byte of the subdata range.
177 * \param size Size, in bytes, of the subdata range.
178 * \param caller Name of calling function for recording errors.
179 * \return A pointer to the buffer object bound to \c target in the
180 * specified context or \c NULL if any of the parameter or state
181 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
184 * \sa glBufferSubDataARB, glGetBufferSubDataARB
186 static struct gl_buffer_object
*
187 buffer_object_subdata_range_good( struct gl_context
* ctx
, GLenum target
,
188 GLintptrARB offset
, GLsizeiptrARB size
,
191 struct gl_buffer_object
*bufObj
;
194 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
199 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
203 bufObj
= get_buffer(ctx
, caller
, target
);
207 if (offset
+ size
> bufObj
->Size
) {
208 _mesa_error(ctx
, GL_INVALID_VALUE
,
209 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
210 (unsigned long) offset
,
211 (unsigned long) size
,
212 (unsigned long) bufObj
->Size
);
215 if (_mesa_bufferobj_mapped(bufObj
)) {
216 /* Buffer is currently mapped */
217 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
226 * Allocate and initialize a new buffer object.
228 * Default callback for the \c dd_function_table::NewBufferObject() hook.
230 static struct gl_buffer_object
*
231 _mesa_new_buffer_object( struct gl_context
*ctx
, GLuint name
, GLenum target
)
233 struct gl_buffer_object
*obj
;
237 obj
= MALLOC_STRUCT(gl_buffer_object
);
238 _mesa_initialize_buffer_object(ctx
, obj
, name
, target
);
244 * Delete a buffer object.
246 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
249 _mesa_delete_buffer_object(struct gl_context
*ctx
,
250 struct gl_buffer_object
*bufObj
)
257 /* assign strange values here to help w/ debugging */
258 bufObj
->RefCount
= -1000;
261 _glthread_DESTROY_MUTEX(bufObj
->Mutex
);
268 * Set ptr to bufObj w/ reference counting.
269 * This is normally only called from the _mesa_reference_buffer_object() macro
270 * when there's a real pointer change.
273 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
274 struct gl_buffer_object
**ptr
,
275 struct gl_buffer_object
*bufObj
)
278 /* Unreference the old buffer */
279 GLboolean deleteFlag
= GL_FALSE
;
280 struct gl_buffer_object
*oldObj
= *ptr
;
282 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
283 ASSERT(oldObj
->RefCount
> 0);
286 printf("BufferObj %p %d DECR to %d\n",
287 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
289 deleteFlag
= (oldObj
->RefCount
== 0);
290 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
294 /* some sanity checking: don't delete a buffer still in use */
296 /* unfortunately, these tests are invalid during context tear-down */
297 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
298 ASSERT(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
!= bufObj
);
299 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
302 ASSERT(ctx
->Driver
.DeleteBuffer
);
303 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
311 /* reference new buffer */
312 _glthread_LOCK_MUTEX(bufObj
->Mutex
);
313 if (bufObj
->RefCount
== 0) {
314 /* this buffer's being deleted (look just above) */
315 /* Not sure this can every really happen. Warn if it does. */
316 _mesa_problem(NULL
, "referencing deleted buffer object");
322 printf("BufferObj %p %d INCR to %d\n",
323 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
327 _glthread_UNLOCK_MUTEX(bufObj
->Mutex
);
333 * Initialize a buffer object to default values.
336 _mesa_initialize_buffer_object( struct gl_context
*ctx
,
337 struct gl_buffer_object
*obj
,
338 GLuint name
, GLenum target
)
342 memset(obj
, 0, sizeof(struct gl_buffer_object
));
343 _glthread_INIT_MUTEX(obj
->Mutex
);
346 obj
->Usage
= GL_STATIC_DRAW_ARB
;
347 obj
->AccessFlags
= default_access_mode(ctx
);
353 * Callback called from _mesa_HashWalk()
356 count_buffer_size(GLuint key
, void *data
, void *userData
)
358 const struct gl_buffer_object
*bufObj
=
359 (const struct gl_buffer_object
*) data
;
360 GLuint
*total
= (GLuint
*) userData
;
362 *total
= *total
+ bufObj
->Size
;
367 * Compute total size (in bytes) of all buffer objects for the given context.
368 * For debugging purposes.
371 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
375 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
382 * Allocate space for and store data in a buffer object. Any data that was
383 * previously stored in the buffer object is lost. If \c data is \c NULL,
384 * memory will be allocated, but no copy will occur.
386 * This is the default callback for \c dd_function_table::BufferData()
387 * Note that all GL error checking will have been done already.
389 * \param ctx GL context.
390 * \param target Buffer object target on which to operate.
391 * \param size Size, in bytes, of the new data store.
392 * \param data Pointer to the data to store in the buffer object. This
393 * pointer may be \c NULL.
394 * \param usage Hints about how the data will be used.
395 * \param bufObj Object to be used.
397 * \return GL_TRUE for success, GL_FALSE for failure
398 * \sa glBufferDataARB, dd_function_table::BufferData.
401 _mesa_buffer_data( struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
402 const GLvoid
* data
, GLenum usage
,
403 struct gl_buffer_object
* bufObj
)
407 (void) ctx
; (void) target
;
409 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
411 bufObj
->Data
= (GLubyte
*) new_data
;
413 bufObj
->Usage
= usage
;
416 memcpy( bufObj
->Data
, data
, size
);
428 * Replace data in a subrange of buffer object. If the data range
429 * specified by \c size + \c offset extends beyond the end of the buffer or
430 * if \c data is \c NULL, no copy is performed.
432 * This is the default callback for \c dd_function_table::BufferSubData()
433 * Note that all GL error checking will have been done already.
435 * \param ctx GL context.
436 * \param target Buffer object target on which to operate.
437 * \param offset Offset of the first byte to be modified.
438 * \param size Size, in bytes, of the data range.
439 * \param data Pointer to the data to store in the buffer object.
440 * \param bufObj Object to be used.
442 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
445 _mesa_buffer_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
446 GLsizeiptrARB size
, const GLvoid
* data
,
447 struct gl_buffer_object
* bufObj
)
451 /* this should have been caught in _mesa_BufferSubData() */
452 ASSERT(size
+ offset
<= bufObj
->Size
);
455 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
461 * Retrieve data from a subrange of buffer object. If the data range
462 * specified by \c size + \c offset extends beyond the end of the buffer or
463 * if \c data is \c NULL, no copy is performed.
465 * This is the default callback for \c dd_function_table::GetBufferSubData()
466 * Note that all GL error checking will have been done already.
468 * \param ctx GL context.
469 * \param target Buffer object target on which to operate.
470 * \param offset Offset of the first byte to be fetched.
471 * \param size Size, in bytes, of the data range.
472 * \param data Destination for data
473 * \param bufObj Object to be used.
475 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
478 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
479 GLsizeiptrARB size
, GLvoid
* data
,
480 struct gl_buffer_object
* bufObj
)
484 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
485 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
491 * Default fallback for \c dd_function_table::MapBufferRange().
492 * Called via glMapBufferRange().
495 _mesa_buffer_map_range( struct gl_context
*ctx
, GLintptr offset
,
496 GLsizeiptr length
, GLbitfield access
,
497 struct gl_buffer_object
*bufObj
)
500 assert(!_mesa_bufferobj_mapped(bufObj
));
501 /* Just return a direct pointer to the data */
502 bufObj
->Pointer
= bufObj
->Data
+ offset
;
503 bufObj
->Length
= length
;
504 bufObj
->Offset
= offset
;
505 bufObj
->AccessFlags
= access
;
506 return bufObj
->Pointer
;
511 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
512 * Called via glFlushMappedBufferRange().
515 _mesa_buffer_flush_mapped_range( struct gl_context
*ctx
,
516 GLintptr offset
, GLsizeiptr length
,
517 struct gl_buffer_object
*obj
)
528 * Default callback for \c dd_function_table::MapBuffer().
530 * The input parameters will have been already tested for errors.
532 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
535 _mesa_buffer_unmap( struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
538 /* XXX we might assert here that bufObj->Pointer is non-null */
539 bufObj
->Pointer
= NULL
;
542 bufObj
->AccessFlags
= 0x0;
548 * Default fallback for \c dd_function_table::CopyBufferSubData().
549 * Called via glCopyBufferSubData().
552 _mesa_copy_buffer_subdata(struct gl_context
*ctx
,
553 struct gl_buffer_object
*src
,
554 struct gl_buffer_object
*dst
,
555 GLintptr readOffset
, GLintptr writeOffset
,
558 GLubyte
*srcPtr
, *dstPtr
;
560 /* the buffers should not be mapped */
561 assert(!_mesa_bufferobj_mapped(src
));
562 assert(!_mesa_bufferobj_mapped(dst
));
565 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
567 GL_MAP_WRITE_BIT
, src
);
572 srcPtr
+= readOffset
;
573 dstPtr
+= writeOffset
;
575 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
576 GL_MAP_READ_BIT
, src
);
577 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
579 GL_MAP_INVALIDATE_RANGE_BIT
), dst
);
582 /* Note: the src and dst regions will never overlap. Trying to do so
583 * would generate GL_INVALID_VALUE earlier.
585 if (srcPtr
&& dstPtr
)
586 memcpy(dstPtr
, srcPtr
, size
);
588 ctx
->Driver
.UnmapBuffer(ctx
, src
);
590 ctx
->Driver
.UnmapBuffer(ctx
, dst
);
596 * Initialize the state associated with buffer objects
599 _mesa_init_buffer_objects( struct gl_context
*ctx
)
601 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
602 _glthread_INIT_MUTEX(DummyBufferObject
.Mutex
);
603 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
605 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
606 ctx
->Shared
->NullBufferObj
);
608 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
609 ctx
->Shared
->NullBufferObj
);
610 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
611 ctx
->Shared
->NullBufferObj
);
616 _mesa_free_buffer_objects( struct gl_context
*ctx
)
618 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
620 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
621 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
626 * Bind the specified target to buffer for the specified context.
627 * Called by glBindBuffer() and other functions.
630 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
632 struct gl_buffer_object
*oldBufObj
;
633 struct gl_buffer_object
*newBufObj
= NULL
;
634 struct gl_buffer_object
**bindTarget
= NULL
;
636 bindTarget
= get_buffer_target(ctx
, target
);
638 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
642 /* Get pointer to old buffer object (to be unbound) */
643 oldBufObj
= *bindTarget
;
644 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
645 return; /* rebinding the same buffer object- no change */
648 * Get pointer to new buffer object (newBufObj)
651 /* The spec says there's not a buffer object named 0, but we use
652 * one internally because it simplifies things.
654 newBufObj
= ctx
->Shared
->NullBufferObj
;
657 /* non-default buffer object */
658 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
659 if (!newBufObj
|| newBufObj
== &DummyBufferObject
) {
660 /* If this is a new buffer object id, or one which was generated but
661 * never used before, allocate a buffer object now.
663 ASSERT(ctx
->Driver
.NewBufferObject
);
664 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
666 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
669 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
673 /* bind new buffer */
674 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
676 /* Pass BindBuffer call to device driver */
677 if (ctx
->Driver
.BindBuffer
)
678 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
683 * Update the default buffer objects in the given context to reference those
684 * specified in the shared state and release those referencing the old
688 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
690 /* Bind the NullBufferObj to remove references to those
691 * in the shared context hash table.
693 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
694 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
695 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
696 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
702 * Return the gl_buffer_object for the given ID.
703 * Always return NULL for ID 0.
705 struct gl_buffer_object
*
706 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
711 return (struct gl_buffer_object
*)
712 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
717 * If *ptr points to obj, set ptr = the Null/default buffer object.
718 * This is a helper for buffer object deletion.
719 * The GL spec says that deleting a buffer object causes it to get
720 * unbound from all arrays in the current context.
723 unbind(struct gl_context
*ctx
,
724 struct gl_buffer_object
**ptr
,
725 struct gl_buffer_object
*obj
)
728 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
734 * Plug default/fallback buffer object functions into the device
738 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
740 /* GL_ARB_vertex/pixel_buffer_object */
741 driver
->NewBufferObject
= _mesa_new_buffer_object
;
742 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
743 driver
->BindBuffer
= NULL
;
744 driver
->BufferData
= _mesa_buffer_data
;
745 driver
->BufferSubData
= _mesa_buffer_subdata
;
746 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
747 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
749 /* GL_ARB_map_buffer_range */
750 driver
->MapBufferRange
= _mesa_buffer_map_range
;
751 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
753 /* GL_ARB_copy_buffer */
754 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
759 /**********************************************************************/
761 /**********************************************************************/
764 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
766 GET_CURRENT_CONTEXT(ctx
);
767 ASSERT_OUTSIDE_BEGIN_END(ctx
);
769 if (MESA_VERBOSE
& VERBOSE_API
)
770 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
771 _mesa_lookup_enum_by_nr(target
), buffer
);
773 bind_buffer_object(ctx
, target
, buffer
);
778 * Delete a set of buffer objects.
780 * \param n Number of buffer objects to delete.
781 * \param ids Array of \c n buffer object IDs.
784 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
786 GET_CURRENT_CONTEXT(ctx
);
788 ASSERT_OUTSIDE_BEGIN_END(ctx
);
789 FLUSH_VERTICES(ctx
, 0);
792 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
796 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
798 for (i
= 0; i
< n
; i
++) {
799 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
801 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
804 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
806 if (_mesa_bufferobj_mapped(bufObj
)) {
807 /* if mapped, unmap it now */
808 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
809 bufObj
->AccessFlags
= default_access_mode(ctx
);
810 bufObj
->Pointer
= NULL
;
813 /* unbind any vertex pointers bound to this buffer */
814 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
815 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
818 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
819 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
821 if (arrayObj
->ElementArrayBufferObj
== bufObj
) {
822 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
825 /* unbind ARB_copy_buffer binding points */
826 if (ctx
->CopyReadBuffer
== bufObj
) {
827 _mesa_BindBufferARB( GL_COPY_READ_BUFFER
, 0 );
829 if (ctx
->CopyWriteBuffer
== bufObj
) {
830 _mesa_BindBufferARB( GL_COPY_WRITE_BUFFER
, 0 );
833 /* unbind transform feedback binding points */
834 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
835 _mesa_BindBufferARB( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
837 for (j
= 0; j
< MAX_FEEDBACK_ATTRIBS
; j
++) {
838 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
839 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
843 /* unbind any pixel pack/unpack pointers bound to this buffer */
844 if (ctx
->Pack
.BufferObj
== bufObj
) {
845 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
847 if (ctx
->Unpack
.BufferObj
== bufObj
) {
848 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
851 /* The ID is immediately freed for re-use */
852 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
853 /* Make sure we do not run into the classic ABA problem on bind.
854 * We don't want to allow re-binding a buffer object that's been
855 * "deleted" by glDeleteBuffers().
857 * The explicit rebinding to the default object in the current context
858 * prevents the above in the current context, but another context
859 * sharing the same objects might suffer from this problem.
860 * The alternative would be to do the hash lookup in any case on bind
861 * which would introduce more runtime overhead than this.
863 bufObj
->DeletePending
= GL_TRUE
;
864 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
868 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
873 * Generate a set of unique buffer object IDs and store them in \c buffer.
875 * \param n Number of IDs to generate.
876 * \param buffer Array of \c n locations to store the IDs.
879 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
881 GET_CURRENT_CONTEXT(ctx
);
884 ASSERT_OUTSIDE_BEGIN_END(ctx
);
886 if (MESA_VERBOSE
& VERBOSE_API
)
887 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
890 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
899 * This must be atomic (generation and allocation of buffer object IDs)
901 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
903 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
905 /* Insert the ID and pointer to dummy buffer object into hash table */
906 for (i
= 0; i
< n
; i
++) {
907 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
909 buffer
[i
] = first
+ i
;
912 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
917 * Determine if ID is the name of a buffer object.
919 * \param id ID of the potential buffer object.
920 * \return \c GL_TRUE if \c id is the name of a buffer object,
921 * \c GL_FALSE otherwise.
924 _mesa_IsBufferARB(GLuint id
)
926 struct gl_buffer_object
*bufObj
;
927 GET_CURRENT_CONTEXT(ctx
);
928 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
930 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
931 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
932 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
934 return bufObj
&& bufObj
!= &DummyBufferObject
;
939 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
940 const GLvoid
* data
, GLenum usage
)
942 GET_CURRENT_CONTEXT(ctx
);
943 struct gl_buffer_object
*bufObj
;
944 ASSERT_OUTSIDE_BEGIN_END(ctx
);
946 if (MESA_VERBOSE
& VERBOSE_API
)
947 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
948 _mesa_lookup_enum_by_nr(target
),
949 (long int) size
, data
,
950 _mesa_lookup_enum_by_nr(usage
));
953 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
958 case GL_STREAM_DRAW_ARB
:
959 case GL_STREAM_READ_ARB
:
960 case GL_STREAM_COPY_ARB
:
961 case GL_STATIC_DRAW_ARB
:
962 case GL_STATIC_READ_ARB
:
963 case GL_STATIC_COPY_ARB
:
964 case GL_DYNAMIC_DRAW_ARB
:
965 case GL_DYNAMIC_READ_ARB
:
966 case GL_DYNAMIC_COPY_ARB
:
970 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
974 bufObj
= get_buffer(ctx
, "glBufferDataARB", target
);
978 if (_mesa_bufferobj_mapped(bufObj
)) {
979 /* Unmap the existing buffer. We'll replace it now. Not an error. */
980 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
981 bufObj
->AccessFlags
= default_access_mode(ctx
);
982 ASSERT(bufObj
->Pointer
== NULL
);
985 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
987 bufObj
->Written
= GL_TRUE
;
990 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
991 bufObj
->Name
, size
, data
, usage
);
998 ASSERT(ctx
->Driver
.BufferData
);
999 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1000 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1006 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1007 GLsizeiptrARB size
, const GLvoid
* data
)
1009 GET_CURRENT_CONTEXT(ctx
);
1010 struct gl_buffer_object
*bufObj
;
1011 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1013 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1014 "glBufferSubDataARB" );
1016 /* error already recorded */
1023 bufObj
->Written
= GL_TRUE
;
1025 ASSERT(ctx
->Driver
.BufferSubData
);
1026 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
1031 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1032 GLsizeiptrARB size
, void * data
)
1034 GET_CURRENT_CONTEXT(ctx
);
1035 struct gl_buffer_object
*bufObj
;
1036 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1038 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1039 "glGetBufferSubDataARB" );
1041 /* error already recorded */
1045 ASSERT(ctx
->Driver
.GetBufferSubData
);
1046 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
1051 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1053 GET_CURRENT_CONTEXT(ctx
);
1054 struct gl_buffer_object
* bufObj
;
1055 GLbitfield accessFlags
;
1058 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1061 case GL_READ_ONLY_ARB
:
1062 accessFlags
= GL_MAP_READ_BIT
;
1064 case GL_WRITE_ONLY_ARB
:
1065 accessFlags
= GL_MAP_WRITE_BIT
;
1067 case GL_READ_WRITE_ARB
:
1068 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1071 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1075 bufObj
= get_buffer(ctx
, "glMapBufferARB", target
);
1079 if (_mesa_bufferobj_mapped(bufObj
)) {
1080 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1084 if (!bufObj
->Size
) {
1085 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1086 "glMapBuffer(buffer size = 0)");
1090 ASSERT(ctx
->Driver
.MapBufferRange
);
1091 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
);
1093 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1097 /* The driver callback should have set these fields.
1098 * This is important because other modules (like VBO) might call
1099 * the driver function directly.
1101 ASSERT(bufObj
->Pointer
== map
);
1102 ASSERT(bufObj
->Length
== bufObj
->Size
);
1103 ASSERT(bufObj
->Offset
== 0);
1104 bufObj
->AccessFlags
= accessFlags
;
1107 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1108 bufObj
->Written
= GL_TRUE
;
1111 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1112 bufObj
->Name
, bufObj
->Size
, access
);
1113 if (access
== GL_WRITE_ONLY_ARB
) {
1115 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1116 for (i
= 0; i
< bufObj
->Size
; i
++)
1123 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1125 /* buffer is 100 bytes larger than requested, fill with magic value */
1126 for (i
= 0; i
< 100; i
++) {
1127 buf
[bufObj
->Size
- i
- 1] = 123;
1132 return bufObj
->Pointer
;
1136 GLboolean GLAPIENTRY
1137 _mesa_UnmapBufferARB(GLenum target
)
1139 GET_CURRENT_CONTEXT(ctx
);
1140 struct gl_buffer_object
*bufObj
;
1141 GLboolean status
= GL_TRUE
;
1142 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1144 bufObj
= get_buffer(ctx
, "glUnmapBufferARB", target
);
1148 if (!_mesa_bufferobj_mapped(bufObj
)) {
1149 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1154 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1155 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1157 /* check that last 100 bytes are still = magic value */
1158 for (i
= 0; i
< 100; i
++) {
1159 GLuint pos
= bufObj
->Size
- i
- 1;
1160 if (buf
[pos
] != 123) {
1161 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1162 " at position %d (value = %u)\n",
1170 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1171 GLuint i
, unchanged
= 0;
1172 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1174 /* check which bytes changed */
1175 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1176 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1183 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1184 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1189 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1190 bufObj
->AccessFlags
= default_access_mode(ctx
);
1191 ASSERT(bufObj
->Pointer
== NULL
);
1192 ASSERT(bufObj
->Offset
== 0);
1193 ASSERT(bufObj
->Length
== 0);
1200 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1202 GET_CURRENT_CONTEXT(ctx
);
1203 struct gl_buffer_object
*bufObj
;
1204 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1206 bufObj
= get_buffer(ctx
, "glGetBufferParameterivARB", target
);
1211 case GL_BUFFER_SIZE_ARB
:
1212 *params
= (GLint
) bufObj
->Size
;
1214 case GL_BUFFER_USAGE_ARB
:
1215 *params
= bufObj
->Usage
;
1217 case GL_BUFFER_ACCESS_ARB
:
1218 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1220 case GL_BUFFER_MAPPED_ARB
:
1221 *params
= _mesa_bufferobj_mapped(bufObj
);
1223 case GL_BUFFER_ACCESS_FLAGS
:
1224 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1226 *params
= bufObj
->AccessFlags
;
1228 case GL_BUFFER_MAP_OFFSET
:
1229 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1231 *params
= (GLint
) bufObj
->Offset
;
1233 case GL_BUFFER_MAP_LENGTH
:
1234 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1236 *params
= (GLint
) bufObj
->Length
;
1239 ; /* fall-through */
1243 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1244 _mesa_lookup_enum_by_nr(pname
));
1250 * This is pretty much a duplicate of GetBufferParameteriv() but the
1251 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1254 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1256 GET_CURRENT_CONTEXT(ctx
);
1257 struct gl_buffer_object
*bufObj
;
1258 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1260 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
);
1265 case GL_BUFFER_SIZE_ARB
:
1266 *params
= bufObj
->Size
;
1268 case GL_BUFFER_USAGE_ARB
:
1269 *params
= bufObj
->Usage
;
1271 case GL_BUFFER_ACCESS_ARB
:
1272 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1274 case GL_BUFFER_ACCESS_FLAGS
:
1275 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1277 *params
= bufObj
->AccessFlags
;
1279 case GL_BUFFER_MAPPED_ARB
:
1280 *params
= _mesa_bufferobj_mapped(bufObj
);
1282 case GL_BUFFER_MAP_OFFSET
:
1283 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1285 *params
= bufObj
->Offset
;
1287 case GL_BUFFER_MAP_LENGTH
:
1288 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1290 *params
= bufObj
->Length
;
1293 ; /* fall-through */
1297 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1298 _mesa_lookup_enum_by_nr(pname
));
1303 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1305 GET_CURRENT_CONTEXT(ctx
);
1306 struct gl_buffer_object
* bufObj
;
1307 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1309 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1310 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1314 bufObj
= get_buffer(ctx
, "glGetBufferPointervARB", target
);
1318 *params
= bufObj
->Pointer
;
1323 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1324 GLintptr readOffset
, GLintptr writeOffset
,
1327 GET_CURRENT_CONTEXT(ctx
);
1328 struct gl_buffer_object
*src
, *dst
;
1329 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1331 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
);
1335 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
);
1339 if (_mesa_bufferobj_mapped(src
)) {
1340 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1341 "glCopyBufferSubData(readBuffer is mapped)");
1345 if (_mesa_bufferobj_mapped(dst
)) {
1346 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1347 "glCopyBufferSubData(writeBuffer is mapped)");
1351 if (readOffset
< 0) {
1352 _mesa_error(ctx
, GL_INVALID_VALUE
,
1353 "glCopyBufferSubData(readOffset = %d)", (int) readOffset
);
1357 if (writeOffset
< 0) {
1358 _mesa_error(ctx
, GL_INVALID_VALUE
,
1359 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset
);
1363 if (readOffset
+ size
> src
->Size
) {
1364 _mesa_error(ctx
, GL_INVALID_VALUE
,
1365 "glCopyBufferSubData(readOffset + size = %d)",
1366 (int) (readOffset
+ size
));
1370 if (writeOffset
+ size
> dst
->Size
) {
1371 _mesa_error(ctx
, GL_INVALID_VALUE
,
1372 "glCopyBufferSubData(writeOffset + size = %d)",
1373 (int) (writeOffset
+ size
));
1378 if (readOffset
+ size
<= writeOffset
) {
1381 else if (writeOffset
+ size
<= readOffset
) {
1385 /* overlapping src/dst is illegal */
1386 _mesa_error(ctx
, GL_INVALID_VALUE
,
1387 "glCopyBufferSubData(overlapping src/dst)");
1392 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1397 * See GL_ARB_map_buffer_range spec
1400 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1403 GET_CURRENT_CONTEXT(ctx
);
1404 struct gl_buffer_object
*bufObj
;
1407 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1409 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1410 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1411 "glMapBufferRange(extension not supported)");
1416 _mesa_error(ctx
, GL_INVALID_VALUE
,
1417 "glMapBufferRange(offset = %ld)", (long)offset
);
1422 _mesa_error(ctx
, GL_INVALID_VALUE
,
1423 "glMapBufferRange(length = %ld)", (long)length
);
1427 if (access
& ~(GL_MAP_READ_BIT
|
1429 GL_MAP_INVALIDATE_RANGE_BIT
|
1430 GL_MAP_INVALIDATE_BUFFER_BIT
|
1431 GL_MAP_FLUSH_EXPLICIT_BIT
|
1432 GL_MAP_UNSYNCHRONIZED_BIT
)) {
1433 /* generate an error if any undefind bit is set */
1434 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1438 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1439 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1440 "glMapBufferRange(access indicates neither read or write)");
1444 if ((access
& GL_MAP_READ_BIT
) &&
1445 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1446 GL_MAP_INVALIDATE_BUFFER_BIT
|
1447 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1448 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1449 "glMapBufferRange(invalid access flags)");
1453 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1454 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1455 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1456 "glMapBufferRange(invalid access flags)");
1460 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
);
1464 if (offset
+ length
> bufObj
->Size
) {
1465 _mesa_error(ctx
, GL_INVALID_VALUE
,
1466 "glMapBufferRange(offset + length > size)");
1470 if (_mesa_bufferobj_mapped(bufObj
)) {
1471 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1472 "glMapBufferRange(buffer already mapped)");
1476 if (!bufObj
->Size
) {
1477 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1478 "glMapBufferRange(buffer size = 0)");
1482 /* Mapping zero bytes should return a non-null pointer. */
1484 static long dummy
= 0;
1485 bufObj
->Pointer
= &dummy
;
1486 bufObj
->Length
= length
;
1487 bufObj
->Offset
= offset
;
1488 bufObj
->AccessFlags
= access
;
1489 return bufObj
->Pointer
;
1492 ASSERT(ctx
->Driver
.MapBufferRange
);
1493 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
);
1495 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1498 /* The driver callback should have set all these fields.
1499 * This is important because other modules (like VBO) might call
1500 * the driver function directly.
1502 ASSERT(bufObj
->Pointer
== map
);
1503 ASSERT(bufObj
->Length
== length
);
1504 ASSERT(bufObj
->Offset
== offset
);
1505 ASSERT(bufObj
->AccessFlags
== access
);
1513 * See GL_ARB_map_buffer_range spec
1516 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1518 GET_CURRENT_CONTEXT(ctx
);
1519 struct gl_buffer_object
*bufObj
;
1520 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1522 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1523 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1524 "glFlushMappedBufferRange(extension not supported)");
1529 _mesa_error(ctx
, GL_INVALID_VALUE
,
1530 "glFlushMappedBufferRange(offset = %ld)", (long)offset
);
1535 _mesa_error(ctx
, GL_INVALID_VALUE
,
1536 "glFlushMappedBufferRange(length = %ld)", (long)length
);
1540 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
);
1544 if (!_mesa_bufferobj_mapped(bufObj
)) {
1545 /* buffer is not mapped */
1546 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1547 "glFlushMappedBufferRange(buffer is not mapped)");
1551 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1552 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1553 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1557 if (offset
+ length
> bufObj
->Length
) {
1558 _mesa_error(ctx
, GL_INVALID_VALUE
,
1559 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1560 (long)offset
, (long)length
, (long)bufObj
->Length
);
1564 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1566 if (ctx
->Driver
.FlushMappedBufferRange
)
1567 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
);
1571 #if FEATURE_APPLE_object_purgeable
1573 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1575 struct gl_buffer_object
*bufObj
;
1578 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1580 _mesa_error(ctx
, GL_INVALID_VALUE
,
1581 "glObjectPurgeable(name = 0x%x)", name
);
1584 if (!_mesa_is_bufferobj(bufObj
)) {
1585 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1589 if (bufObj
->Purgeable
) {
1590 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1591 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1592 return GL_VOLATILE_APPLE
;
1595 bufObj
->Purgeable
= GL_TRUE
;
1597 retval
= GL_VOLATILE_APPLE
;
1598 if (ctx
->Driver
.BufferObjectPurgeable
)
1599 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1606 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1608 struct gl_renderbuffer
*bufObj
;
1611 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1613 _mesa_error(ctx
, GL_INVALID_VALUE
,
1614 "glObjectUnpurgeable(name = 0x%x)", name
);
1618 if (bufObj
->Purgeable
) {
1619 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1620 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1621 return GL_VOLATILE_APPLE
;
1624 bufObj
->Purgeable
= GL_TRUE
;
1626 retval
= GL_VOLATILE_APPLE
;
1627 if (ctx
->Driver
.RenderObjectPurgeable
)
1628 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1635 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1637 struct gl_texture_object
*bufObj
;
1640 bufObj
= _mesa_lookup_texture(ctx
, name
);
1642 _mesa_error(ctx
, GL_INVALID_VALUE
,
1643 "glObjectPurgeable(name = 0x%x)", name
);
1647 if (bufObj
->Purgeable
) {
1648 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1649 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1650 return GL_VOLATILE_APPLE
;
1653 bufObj
->Purgeable
= GL_TRUE
;
1655 retval
= GL_VOLATILE_APPLE
;
1656 if (ctx
->Driver
.TextureObjectPurgeable
)
1657 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1664 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1668 GET_CURRENT_CONTEXT(ctx
);
1669 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1672 _mesa_error(ctx
, GL_INVALID_VALUE
,
1673 "glObjectPurgeable(name = 0x%x)", name
);
1678 case GL_VOLATILE_APPLE
:
1679 case GL_RELEASED_APPLE
:
1683 _mesa_error(ctx
, GL_INVALID_ENUM
,
1684 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1689 switch (objectType
) {
1691 retval
= texture_object_purgeable(ctx
, name
, option
);
1693 case GL_RENDERBUFFER_EXT
:
1694 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1696 case GL_BUFFER_OBJECT_APPLE
:
1697 retval
= buffer_object_purgeable(ctx
, name
, option
);
1700 _mesa_error(ctx
, GL_INVALID_ENUM
,
1701 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1706 /* In strict conformance to the spec, we must only return VOLATILE when
1707 * when passed the VOLATILE option. Madness.
1709 * XXX First fix the spec, then fix me.
1711 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1716 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1718 struct gl_buffer_object
*bufObj
;
1721 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1723 _mesa_error(ctx
, GL_INVALID_VALUE
,
1724 "glObjectUnpurgeable(name = 0x%x)", name
);
1728 if (! bufObj
->Purgeable
) {
1729 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1730 "glObjectUnpurgeable(name = 0x%x) object is "
1731 " already \"unpurged\"", name
);
1735 bufObj
->Purgeable
= GL_FALSE
;
1738 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1739 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1746 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1748 struct gl_renderbuffer
*bufObj
;
1751 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1753 _mesa_error(ctx
, GL_INVALID_VALUE
,
1754 "glObjectUnpurgeable(name = 0x%x)", name
);
1758 if (! bufObj
->Purgeable
) {
1759 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1760 "glObjectUnpurgeable(name = 0x%x) object is "
1761 " already \"unpurged\"", name
);
1765 bufObj
->Purgeable
= GL_FALSE
;
1768 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1769 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1776 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1778 struct gl_texture_object
*bufObj
;
1781 bufObj
= _mesa_lookup_texture(ctx
, name
);
1783 _mesa_error(ctx
, GL_INVALID_VALUE
,
1784 "glObjectUnpurgeable(name = 0x%x)", name
);
1788 if (! bufObj
->Purgeable
) {
1789 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1790 "glObjectUnpurgeable(name = 0x%x) object is"
1791 " already \"unpurged\"", name
);
1795 bufObj
->Purgeable
= GL_FALSE
;
1798 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1799 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1806 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1808 GET_CURRENT_CONTEXT(ctx
);
1809 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1812 _mesa_error(ctx
, GL_INVALID_VALUE
,
1813 "glObjectUnpurgeable(name = 0x%x)", name
);
1818 case GL_RETAINED_APPLE
:
1819 case GL_UNDEFINED_APPLE
:
1823 _mesa_error(ctx
, GL_INVALID_ENUM
,
1824 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1829 switch (objectType
) {
1830 case GL_BUFFER_OBJECT_APPLE
:
1831 return buffer_object_unpurgeable(ctx
, name
, option
);
1833 return texture_object_unpurgeable(ctx
, name
, option
);
1834 case GL_RENDERBUFFER_EXT
:
1835 return renderbuffer_unpurgeable(ctx
, name
, option
);
1837 _mesa_error(ctx
, GL_INVALID_ENUM
,
1838 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1846 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1847 GLenum pname
, GLint
*params
)
1849 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1851 _mesa_error(ctx
, GL_INVALID_VALUE
,
1852 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1857 case GL_PURGEABLE_APPLE
:
1858 *params
= bufObj
->Purgeable
;
1861 _mesa_error(ctx
, GL_INVALID_ENUM
,
1862 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1870 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1871 GLenum pname
, GLint
*params
)
1873 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1875 _mesa_error(ctx
, GL_INVALID_VALUE
,
1876 "glObjectUnpurgeable(name = 0x%x)", name
);
1881 case GL_PURGEABLE_APPLE
:
1882 *params
= rb
->Purgeable
;
1885 _mesa_error(ctx
, GL_INVALID_ENUM
,
1886 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1894 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1895 GLenum pname
, GLint
*params
)
1897 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1899 _mesa_error(ctx
, GL_INVALID_VALUE
,
1900 "glObjectUnpurgeable(name = 0x%x)", name
);
1905 case GL_PURGEABLE_APPLE
:
1906 *params
= texObj
->Purgeable
;
1909 _mesa_error(ctx
, GL_INVALID_ENUM
,
1910 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1918 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
1921 GET_CURRENT_CONTEXT(ctx
);
1924 _mesa_error(ctx
, GL_INVALID_VALUE
,
1925 "glGetObjectParameteriv(name = 0x%x)", name
);
1929 switch (objectType
) {
1931 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
1933 case GL_BUFFER_OBJECT_APPLE
:
1934 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
1936 case GL_RENDERBUFFER_EXT
:
1937 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
1940 _mesa_error(ctx
, GL_INVALID_ENUM
,
1941 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1946 #endif /* FEATURE_APPLE_object_purgeable */