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*/
53 * Used as a placeholder for buffer objects between glGenBuffers() and
54 * glBindBuffer() so that glIsBuffer() can work correctly.
56 static struct gl_buffer_object DummyBufferObject
;
60 * Return pointer to address of a buffer object target.
61 * \param ctx the GL context
62 * \param target the buffer object target to be retrieved.
63 * \return pointer to pointer to the buffer object bound to \c target in the
64 * specified context or \c NULL if \c target is invalid.
66 static inline struct gl_buffer_object
**
67 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
70 case GL_ARRAY_BUFFER_ARB
:
71 return &ctx
->Array
.ArrayBufferObj
;
72 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
73 return &ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
74 case GL_PIXEL_PACK_BUFFER_EXT
:
75 return &ctx
->Pack
.BufferObj
;
76 case GL_PIXEL_UNPACK_BUFFER_EXT
:
77 return &ctx
->Unpack
.BufferObj
;
78 case GL_COPY_READ_BUFFER
:
79 return &ctx
->CopyReadBuffer
;
80 case GL_COPY_WRITE_BUFFER
:
81 return &ctx
->CopyWriteBuffer
;
82 #if FEATURE_EXT_transform_feedback
83 case GL_TRANSFORM_FEEDBACK_BUFFER
:
84 if (ctx
->Extensions
.EXT_transform_feedback
) {
85 return &ctx
->TransformFeedback
.CurrentBuffer
;
89 case GL_TEXTURE_BUFFER
:
90 if (ctx
->Extensions
.ARB_texture_buffer_object
) {
91 return &ctx
->Texture
.BufferObject
;
102 * Get the buffer object bound to the specified target in a GL context.
103 * \param ctx the GL context
104 * \param target the buffer object target to be retrieved.
105 * \return pointer to the buffer object bound to \c target in the
106 * specified context or \c NULL if \c target is invalid.
108 static inline struct gl_buffer_object
*
109 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
)
111 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
114 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
118 if (!_mesa_is_bufferobj(*bufObj
)) {
119 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(buffer 0)", func
);
128 default_access_mode(const struct gl_context
*ctx
)
130 /* Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
132 * Name Type Initial Value Legal Values
134 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
137 * However, table 6.8 in the GL_OES_mapbuffer extension says:
139 * Get Value Type Get Command Value Description
140 * --------- ---- ----------- ----- -----------
141 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
143 * The difference is because GL_OES_mapbuffer only supports mapping buffers
146 return (ctx
->API
== API_OPENGLES
)
147 ? GL_MAP_WRITE_BIT
: (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
);
152 * Convert a GLbitfield describing the mapped buffer access flags
153 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
156 simplified_access_mode(GLbitfield access
)
158 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
159 if ((access
& rwFlags
) == rwFlags
)
160 return GL_READ_WRITE
;
161 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
163 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
164 return GL_WRITE_ONLY
;
165 return GL_READ_WRITE
; /* this should never happen, but no big deal */
170 * Tests the subdata range parameters and sets the GL error code for
171 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
173 * \param ctx GL context.
174 * \param target Buffer object target on which to operate.
175 * \param offset Offset of the first byte of the subdata range.
176 * \param size Size, in bytes, of the subdata range.
177 * \param caller Name of calling function for recording errors.
178 * \return A pointer to the buffer object bound to \c target in the
179 * specified context or \c NULL if any of the parameter or state
180 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
183 * \sa glBufferSubDataARB, glGetBufferSubDataARB
185 static struct gl_buffer_object
*
186 buffer_object_subdata_range_good( struct gl_context
* ctx
, GLenum target
,
187 GLintptrARB offset
, GLsizeiptrARB size
,
190 struct gl_buffer_object
*bufObj
;
193 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
198 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
202 bufObj
= get_buffer(ctx
, caller
, target
);
206 if (offset
+ size
> bufObj
->Size
) {
207 _mesa_error(ctx
, GL_INVALID_VALUE
,
208 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
209 (unsigned long) offset
,
210 (unsigned long) size
,
211 (unsigned long) bufObj
->Size
);
214 if (_mesa_bufferobj_mapped(bufObj
)) {
215 /* Buffer is currently mapped */
216 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
225 * Allocate and initialize a new buffer object.
227 * Default callback for the \c dd_function_table::NewBufferObject() hook.
229 static struct gl_buffer_object
*
230 _mesa_new_buffer_object( struct gl_context
*ctx
, GLuint name
, GLenum target
)
232 struct gl_buffer_object
*obj
;
236 obj
= MALLOC_STRUCT(gl_buffer_object
);
237 _mesa_initialize_buffer_object(ctx
, obj
, name
, target
);
243 * Delete a buffer object.
245 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
248 _mesa_delete_buffer_object(struct gl_context
*ctx
,
249 struct gl_buffer_object
*bufObj
)
256 /* assign strange values here to help w/ debugging */
257 bufObj
->RefCount
= -1000;
260 _glthread_DESTROY_MUTEX(bufObj
->Mutex
);
267 * Set ptr to bufObj w/ reference counting.
268 * This is normally only called from the _mesa_reference_buffer_object() macro
269 * when there's a real pointer change.
272 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
273 struct gl_buffer_object
**ptr
,
274 struct gl_buffer_object
*bufObj
)
277 /* Unreference the old buffer */
278 GLboolean deleteFlag
= GL_FALSE
;
279 struct gl_buffer_object
*oldObj
= *ptr
;
281 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
282 ASSERT(oldObj
->RefCount
> 0);
285 printf("BufferObj %p %d DECR to %d\n",
286 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
288 deleteFlag
= (oldObj
->RefCount
== 0);
289 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
293 /* some sanity checking: don't delete a buffer still in use */
295 /* unfortunately, these tests are invalid during context tear-down */
296 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
297 ASSERT(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
!= bufObj
);
298 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
301 ASSERT(ctx
->Driver
.DeleteBuffer
);
302 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
310 /* reference new buffer */
311 _glthread_LOCK_MUTEX(bufObj
->Mutex
);
312 if (bufObj
->RefCount
== 0) {
313 /* this buffer's being deleted (look just above) */
314 /* Not sure this can every really happen. Warn if it does. */
315 _mesa_problem(NULL
, "referencing deleted buffer object");
321 printf("BufferObj %p %d INCR to %d\n",
322 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
326 _glthread_UNLOCK_MUTEX(bufObj
->Mutex
);
332 * Initialize a buffer object to default values.
335 _mesa_initialize_buffer_object( struct gl_context
*ctx
,
336 struct gl_buffer_object
*obj
,
337 GLuint name
, GLenum target
)
341 memset(obj
, 0, sizeof(struct gl_buffer_object
));
342 _glthread_INIT_MUTEX(obj
->Mutex
);
345 obj
->Usage
= GL_STATIC_DRAW_ARB
;
346 obj
->AccessFlags
= default_access_mode(ctx
);
352 * Callback called from _mesa_HashWalk()
355 count_buffer_size(GLuint key
, void *data
, void *userData
)
357 const struct gl_buffer_object
*bufObj
=
358 (const struct gl_buffer_object
*) data
;
359 GLuint
*total
= (GLuint
*) userData
;
361 *total
= *total
+ bufObj
->Size
;
366 * Compute total size (in bytes) of all buffer objects for the given context.
367 * For debugging purposes.
370 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
374 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
381 * Allocate space for and store data in a buffer object. Any data that was
382 * previously stored in the buffer object is lost. If \c data is \c NULL,
383 * memory will be allocated, but no copy will occur.
385 * This is the default callback for \c dd_function_table::BufferData()
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 size Size, in bytes, of the new data store.
391 * \param data Pointer to the data to store in the buffer object. This
392 * pointer may be \c NULL.
393 * \param usage Hints about how the data will be used.
394 * \param bufObj Object to be used.
396 * \return GL_TRUE for success, GL_FALSE for failure
397 * \sa glBufferDataARB, dd_function_table::BufferData.
400 _mesa_buffer_data( struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
401 const GLvoid
* data
, GLenum usage
,
402 struct gl_buffer_object
* bufObj
)
406 (void) ctx
; (void) target
;
408 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
410 bufObj
->Data
= (GLubyte
*) new_data
;
412 bufObj
->Usage
= usage
;
415 memcpy( bufObj
->Data
, data
, size
);
427 * Replace data in a subrange of buffer object. If the data range
428 * specified by \c size + \c offset extends beyond the end of the buffer or
429 * if \c data is \c NULL, no copy is performed.
431 * This is the default callback for \c dd_function_table::BufferSubData()
432 * Note that all GL error checking will have been done already.
434 * \param ctx GL context.
435 * \param target Buffer object target on which to operate.
436 * \param offset Offset of the first byte to be modified.
437 * \param size Size, in bytes, of the data range.
438 * \param data Pointer to the data to store in the buffer object.
439 * \param bufObj Object to be used.
441 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
444 _mesa_buffer_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
445 GLsizeiptrARB size
, const GLvoid
* data
,
446 struct gl_buffer_object
* bufObj
)
450 /* this should have been caught in _mesa_BufferSubData() */
451 ASSERT(size
+ offset
<= bufObj
->Size
);
454 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
460 * Retrieve data from a subrange of buffer object. If the data range
461 * specified by \c size + \c offset extends beyond the end of the buffer or
462 * if \c data is \c NULL, no copy is performed.
464 * This is the default callback for \c dd_function_table::GetBufferSubData()
465 * Note that all GL error checking will have been done already.
467 * \param ctx GL context.
468 * \param target Buffer object target on which to operate.
469 * \param offset Offset of the first byte to be fetched.
470 * \param size Size, in bytes, of the data range.
471 * \param data Destination for data
472 * \param bufObj Object to be used.
474 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
477 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
478 GLsizeiptrARB size
, GLvoid
* data
,
479 struct gl_buffer_object
* bufObj
)
483 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
484 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
490 * Default fallback for \c dd_function_table::MapBufferRange().
491 * Called via glMapBufferRange().
494 _mesa_buffer_map_range( struct gl_context
*ctx
, GLintptr offset
,
495 GLsizeiptr length
, GLbitfield access
,
496 struct gl_buffer_object
*bufObj
)
499 assert(!_mesa_bufferobj_mapped(bufObj
));
500 /* Just return a direct pointer to the data */
501 bufObj
->Pointer
= bufObj
->Data
+ offset
;
502 bufObj
->Length
= length
;
503 bufObj
->Offset
= offset
;
504 bufObj
->AccessFlags
= access
;
505 return bufObj
->Pointer
;
510 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
511 * Called via glFlushMappedBufferRange().
514 _mesa_buffer_flush_mapped_range( struct gl_context
*ctx
,
515 GLintptr offset
, GLsizeiptr length
,
516 struct gl_buffer_object
*obj
)
527 * Default callback for \c dd_function_table::MapBuffer().
529 * The input parameters will have been already tested for errors.
531 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
534 _mesa_buffer_unmap( struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
537 /* XXX we might assert here that bufObj->Pointer is non-null */
538 bufObj
->Pointer
= NULL
;
541 bufObj
->AccessFlags
= 0x0;
547 * Default fallback for \c dd_function_table::CopyBufferSubData().
548 * Called via glCopyBuffserSubData().
551 _mesa_copy_buffer_subdata(struct gl_context
*ctx
,
552 struct gl_buffer_object
*src
,
553 struct gl_buffer_object
*dst
,
554 GLintptr readOffset
, GLintptr writeOffset
,
557 GLubyte
*srcPtr
, *dstPtr
;
559 /* the buffers should not be mapped */
560 assert(!_mesa_bufferobj_mapped(src
));
561 assert(!_mesa_bufferobj_mapped(dst
));
564 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
566 GL_MAP_WRITE_BIT
, src
);
571 srcPtr
+= readOffset
;
572 dstPtr
+= writeOffset
;
574 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
575 GL_MAP_READ_BIT
, src
);
576 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
578 GL_MAP_INVALIDATE_RANGE_BIT
), dst
);
581 /* Note: the src and dst regions will never overlap. Trying to do so
582 * would generate GL_INVALID_VALUE earlier.
584 if (srcPtr
&& dstPtr
)
585 memcpy(dstPtr
, srcPtr
, size
);
587 ctx
->Driver
.UnmapBuffer(ctx
, src
);
589 ctx
->Driver
.UnmapBuffer(ctx
, dst
);
595 * Initialize the state associated with buffer objects
598 _mesa_init_buffer_objects( struct gl_context
*ctx
)
600 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
601 _glthread_INIT_MUTEX(DummyBufferObject
.Mutex
);
602 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
604 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
605 ctx
->Shared
->NullBufferObj
);
607 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
608 ctx
->Shared
->NullBufferObj
);
609 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
610 ctx
->Shared
->NullBufferObj
);
615 _mesa_free_buffer_objects( struct gl_context
*ctx
)
617 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
619 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
620 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
625 * Bind the specified target to buffer for the specified context.
626 * Called by glBindBuffer() and other functions.
629 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
631 struct gl_buffer_object
*oldBufObj
;
632 struct gl_buffer_object
*newBufObj
= NULL
;
633 struct gl_buffer_object
**bindTarget
= NULL
;
635 bindTarget
= get_buffer_target(ctx
, target
);
637 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
641 /* Get pointer to old buffer object (to be unbound) */
642 oldBufObj
= *bindTarget
;
643 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
644 return; /* rebinding the same buffer object- no change */
647 * Get pointer to new buffer object (newBufObj)
650 /* The spec says there's not a buffer object named 0, but we use
651 * one internally because it simplifies things.
653 newBufObj
= ctx
->Shared
->NullBufferObj
;
656 /* non-default buffer object */
657 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
658 if (!newBufObj
|| newBufObj
== &DummyBufferObject
) {
659 /* If this is a new buffer object id, or one which was generated but
660 * never used before, allocate a buffer object now.
662 ASSERT(ctx
->Driver
.NewBufferObject
);
663 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
665 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
668 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
672 /* bind new buffer */
673 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
675 /* Pass BindBuffer call to device driver */
676 if (ctx
->Driver
.BindBuffer
)
677 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
682 * Update the default buffer objects in the given context to reference those
683 * specified in the shared state and release those referencing the old
687 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
689 /* Bind the NullBufferObj to remove references to those
690 * in the shared context hash table.
692 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
693 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
694 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
695 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
701 * Return the gl_buffer_object for the given ID.
702 * Always return NULL for ID 0.
704 struct gl_buffer_object
*
705 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
710 return (struct gl_buffer_object
*)
711 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
716 * If *ptr points to obj, set ptr = the Null/default buffer object.
717 * This is a helper for buffer object deletion.
718 * The GL spec says that deleting a buffer object causes it to get
719 * unbound from all arrays in the current context.
722 unbind(struct gl_context
*ctx
,
723 struct gl_buffer_object
**ptr
,
724 struct gl_buffer_object
*obj
)
727 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
733 * Plug default/fallback buffer object functions into the device
737 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
739 /* GL_ARB_vertex/pixel_buffer_object */
740 driver
->NewBufferObject
= _mesa_new_buffer_object
;
741 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
742 driver
->BindBuffer
= NULL
;
743 driver
->BufferData
= _mesa_buffer_data
;
744 driver
->BufferSubData
= _mesa_buffer_subdata
;
745 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
746 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
748 /* GL_ARB_map_buffer_range */
749 driver
->MapBufferRange
= _mesa_buffer_map_range
;
750 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
752 /* GL_ARB_copy_buffer */
753 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
758 /**********************************************************************/
760 /**********************************************************************/
763 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
765 GET_CURRENT_CONTEXT(ctx
);
766 ASSERT_OUTSIDE_BEGIN_END(ctx
);
768 if (MESA_VERBOSE
& VERBOSE_API
)
769 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
770 _mesa_lookup_enum_by_nr(target
), buffer
);
772 bind_buffer_object(ctx
, target
, buffer
);
777 * Delete a set of buffer objects.
779 * \param n Number of buffer objects to delete.
780 * \param ids Array of \c n buffer object IDs.
783 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
785 GET_CURRENT_CONTEXT(ctx
);
787 ASSERT_OUTSIDE_BEGIN_END(ctx
);
788 FLUSH_VERTICES(ctx
, 0);
791 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
795 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
797 for (i
= 0; i
< n
; i
++) {
798 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
800 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
803 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
805 if (_mesa_bufferobj_mapped(bufObj
)) {
806 /* if mapped, unmap it now */
807 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
808 bufObj
->AccessFlags
= default_access_mode(ctx
);
809 bufObj
->Pointer
= NULL
;
812 /* unbind any vertex pointers bound to this buffer */
813 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
814 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
817 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
818 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
820 if (arrayObj
->ElementArrayBufferObj
== bufObj
) {
821 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
824 /* unbind any pixel pack/unpack pointers bound to this buffer */
825 if (ctx
->Pack
.BufferObj
== bufObj
) {
826 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
828 if (ctx
->Unpack
.BufferObj
== bufObj
) {
829 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
832 /* The ID is immediately freed for re-use */
833 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
834 /* Make sure we do not run into the classic ABA problem on bind.
835 * We don't want to allow re-binding a buffer object that's been
836 * "deleted" by glDeleteBuffers().
838 * The explicit rebinding to the default object in the current context
839 * prevents the above in the current context, but another context
840 * sharing the same objects might suffer from this problem.
841 * The alternative would be to do the hash lookup in any case on bind
842 * which would introduce more runtime overhead than this.
844 bufObj
->DeletePending
= GL_TRUE
;
845 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
849 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
854 * Generate a set of unique buffer object IDs and store them in \c buffer.
856 * \param n Number of IDs to generate.
857 * \param buffer Array of \c n locations to store the IDs.
860 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
862 GET_CURRENT_CONTEXT(ctx
);
865 ASSERT_OUTSIDE_BEGIN_END(ctx
);
867 if (MESA_VERBOSE
& VERBOSE_API
)
868 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
871 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
880 * This must be atomic (generation and allocation of buffer object IDs)
882 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
884 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
886 /* Insert the ID and pointer to dummy buffer object into hash table */
887 for (i
= 0; i
< n
; i
++) {
888 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
890 buffer
[i
] = first
+ i
;
893 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
898 * Determine if ID is the name of a buffer object.
900 * \param id ID of the potential buffer object.
901 * \return \c GL_TRUE if \c id is the name of a buffer object,
902 * \c GL_FALSE otherwise.
905 _mesa_IsBufferARB(GLuint id
)
907 struct gl_buffer_object
*bufObj
;
908 GET_CURRENT_CONTEXT(ctx
);
909 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
911 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
912 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
913 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
915 return bufObj
&& bufObj
!= &DummyBufferObject
;
920 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
921 const GLvoid
* data
, GLenum usage
)
923 GET_CURRENT_CONTEXT(ctx
);
924 struct gl_buffer_object
*bufObj
;
925 ASSERT_OUTSIDE_BEGIN_END(ctx
);
927 if (MESA_VERBOSE
& VERBOSE_API
)
928 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
929 _mesa_lookup_enum_by_nr(target
),
930 (long int) size
, data
,
931 _mesa_lookup_enum_by_nr(usage
));
934 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
939 case GL_STREAM_DRAW_ARB
:
940 case GL_STREAM_READ_ARB
:
941 case GL_STREAM_COPY_ARB
:
942 case GL_STATIC_DRAW_ARB
:
943 case GL_STATIC_READ_ARB
:
944 case GL_STATIC_COPY_ARB
:
945 case GL_DYNAMIC_DRAW_ARB
:
946 case GL_DYNAMIC_READ_ARB
:
947 case GL_DYNAMIC_COPY_ARB
:
951 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
955 bufObj
= get_buffer(ctx
, "glBufferDataARB", target
);
959 if (_mesa_bufferobj_mapped(bufObj
)) {
960 /* Unmap the existing buffer. We'll replace it now. Not an error. */
961 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
962 bufObj
->AccessFlags
= default_access_mode(ctx
);
963 ASSERT(bufObj
->Pointer
== NULL
);
966 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
968 bufObj
->Written
= GL_TRUE
;
971 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
972 bufObj
->Name
, size
, data
, usage
);
979 ASSERT(ctx
->Driver
.BufferData
);
980 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
981 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
987 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
988 GLsizeiptrARB size
, const GLvoid
* data
)
990 GET_CURRENT_CONTEXT(ctx
);
991 struct gl_buffer_object
*bufObj
;
992 ASSERT_OUTSIDE_BEGIN_END(ctx
);
994 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
995 "glBufferSubDataARB" );
997 /* error already recorded */
1004 bufObj
->Written
= GL_TRUE
;
1006 ASSERT(ctx
->Driver
.BufferSubData
);
1007 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
1012 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1013 GLsizeiptrARB size
, void * data
)
1015 GET_CURRENT_CONTEXT(ctx
);
1016 struct gl_buffer_object
*bufObj
;
1017 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1019 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1020 "glGetBufferSubDataARB" );
1022 /* error already recorded */
1026 ASSERT(ctx
->Driver
.GetBufferSubData
);
1027 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
1032 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1034 GET_CURRENT_CONTEXT(ctx
);
1035 struct gl_buffer_object
* bufObj
;
1036 GLbitfield accessFlags
;
1039 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1042 case GL_READ_ONLY_ARB
:
1043 accessFlags
= GL_MAP_READ_BIT
;
1045 case GL_WRITE_ONLY_ARB
:
1046 accessFlags
= GL_MAP_WRITE_BIT
;
1048 case GL_READ_WRITE_ARB
:
1049 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1052 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1056 bufObj
= get_buffer(ctx
, "glMapBufferARB", target
);
1060 if (_mesa_bufferobj_mapped(bufObj
)) {
1061 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1065 if (!bufObj
->Size
) {
1066 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1067 "glMapBuffer(buffer size = 0)");
1071 ASSERT(ctx
->Driver
.MapBufferRange
);
1072 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
);
1074 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1078 /* The driver callback should have set these fields.
1079 * This is important because other modules (like VBO) might call
1080 * the driver function directly.
1082 ASSERT(bufObj
->Pointer
== map
);
1083 ASSERT(bufObj
->Length
== bufObj
->Size
);
1084 ASSERT(bufObj
->Offset
== 0);
1085 bufObj
->AccessFlags
= accessFlags
;
1088 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1089 bufObj
->Written
= GL_TRUE
;
1092 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1093 bufObj
->Name
, bufObj
->Size
, access
);
1094 if (access
== GL_WRITE_ONLY_ARB
) {
1096 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1097 for (i
= 0; i
< bufObj
->Size
; i
++)
1104 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1106 /* buffer is 100 bytes larger than requested, fill with magic value */
1107 for (i
= 0; i
< 100; i
++) {
1108 buf
[bufObj
->Size
- i
- 1] = 123;
1113 return bufObj
->Pointer
;
1117 GLboolean GLAPIENTRY
1118 _mesa_UnmapBufferARB(GLenum target
)
1120 GET_CURRENT_CONTEXT(ctx
);
1121 struct gl_buffer_object
*bufObj
;
1122 GLboolean status
= GL_TRUE
;
1123 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1125 bufObj
= get_buffer(ctx
, "glUnmapBufferARB", target
);
1129 if (!_mesa_bufferobj_mapped(bufObj
)) {
1130 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1135 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1136 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1138 /* check that last 100 bytes are still = magic value */
1139 for (i
= 0; i
< 100; i
++) {
1140 GLuint pos
= bufObj
->Size
- i
- 1;
1141 if (buf
[pos
] != 123) {
1142 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1143 " at position %d (value = %u)\n",
1151 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1152 GLuint i
, unchanged
= 0;
1153 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1155 /* check which bytes changed */
1156 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1157 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1164 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1165 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1170 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1171 bufObj
->AccessFlags
= default_access_mode(ctx
);
1172 ASSERT(bufObj
->Pointer
== NULL
);
1173 ASSERT(bufObj
->Offset
== 0);
1174 ASSERT(bufObj
->Length
== 0);
1181 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1183 GET_CURRENT_CONTEXT(ctx
);
1184 struct gl_buffer_object
*bufObj
;
1185 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1187 bufObj
= get_buffer(ctx
, "glGetBufferParameterivARB", target
);
1192 case GL_BUFFER_SIZE_ARB
:
1193 *params
= (GLint
) bufObj
->Size
;
1195 case GL_BUFFER_USAGE_ARB
:
1196 *params
= bufObj
->Usage
;
1198 case GL_BUFFER_ACCESS_ARB
:
1199 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1201 case GL_BUFFER_MAPPED_ARB
:
1202 *params
= _mesa_bufferobj_mapped(bufObj
);
1204 case GL_BUFFER_ACCESS_FLAGS
:
1205 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1207 *params
= bufObj
->AccessFlags
;
1209 case GL_BUFFER_MAP_OFFSET
:
1210 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1212 *params
= (GLint
) bufObj
->Offset
;
1214 case GL_BUFFER_MAP_LENGTH
:
1215 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1217 *params
= (GLint
) bufObj
->Length
;
1220 ; /* fall-through */
1224 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1225 _mesa_lookup_enum_by_nr(pname
));
1231 * This is pretty much a duplicate of GetBufferParameteriv() but the
1232 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1235 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1237 GET_CURRENT_CONTEXT(ctx
);
1238 struct gl_buffer_object
*bufObj
;
1239 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1241 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
);
1246 case GL_BUFFER_SIZE_ARB
:
1247 *params
= bufObj
->Size
;
1249 case GL_BUFFER_USAGE_ARB
:
1250 *params
= bufObj
->Usage
;
1252 case GL_BUFFER_ACCESS_ARB
:
1253 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1255 case GL_BUFFER_ACCESS_FLAGS
:
1256 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1258 *params
= bufObj
->AccessFlags
;
1260 case GL_BUFFER_MAPPED_ARB
:
1261 *params
= _mesa_bufferobj_mapped(bufObj
);
1263 case GL_BUFFER_MAP_OFFSET
:
1264 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1266 *params
= bufObj
->Offset
;
1268 case GL_BUFFER_MAP_LENGTH
:
1269 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1271 *params
= bufObj
->Length
;
1274 ; /* fall-through */
1278 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1279 _mesa_lookup_enum_by_nr(pname
));
1284 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1286 GET_CURRENT_CONTEXT(ctx
);
1287 struct gl_buffer_object
* bufObj
;
1288 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1290 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1291 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1295 bufObj
= get_buffer(ctx
, "glGetBufferPointervARB", target
);
1299 *params
= bufObj
->Pointer
;
1304 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1305 GLintptr readOffset
, GLintptr writeOffset
,
1308 GET_CURRENT_CONTEXT(ctx
);
1309 struct gl_buffer_object
*src
, *dst
;
1310 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1312 src
= get_buffer(ctx
, "glCopyBuffserSubData", readTarget
);
1316 dst
= get_buffer(ctx
, "glCopyBuffserSubData", writeTarget
);
1320 if (_mesa_bufferobj_mapped(src
)) {
1321 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1322 "glCopyBuffserSubData(readBuffer is mapped)");
1326 if (_mesa_bufferobj_mapped(dst
)) {
1327 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1328 "glCopyBuffserSubData(writeBuffer is mapped)");
1332 if (readOffset
< 0) {
1333 _mesa_error(ctx
, GL_INVALID_VALUE
,
1334 "glCopyBuffserSubData(readOffset = %d)", (int) readOffset
);
1338 if (writeOffset
< 0) {
1339 _mesa_error(ctx
, GL_INVALID_VALUE
,
1340 "glCopyBuffserSubData(writeOffset = %d)", (int) writeOffset
);
1344 if (readOffset
+ size
> src
->Size
) {
1345 _mesa_error(ctx
, GL_INVALID_VALUE
,
1346 "glCopyBuffserSubData(readOffset + size = %d)",
1347 (int) (readOffset
+ size
));
1351 if (writeOffset
+ size
> dst
->Size
) {
1352 _mesa_error(ctx
, GL_INVALID_VALUE
,
1353 "glCopyBuffserSubData(writeOffset + size = %d)",
1354 (int) (writeOffset
+ size
));
1359 if (readOffset
+ size
<= writeOffset
) {
1362 else if (writeOffset
+ size
<= readOffset
) {
1366 /* overlapping src/dst is illegal */
1367 _mesa_error(ctx
, GL_INVALID_VALUE
,
1368 "glCopyBuffserSubData(overlapping src/dst)");
1373 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1378 * See GL_ARB_map_buffer_range spec
1381 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1384 GET_CURRENT_CONTEXT(ctx
);
1385 struct gl_buffer_object
*bufObj
;
1388 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1390 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1391 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1392 "glMapBufferRange(extension not supported)");
1397 _mesa_error(ctx
, GL_INVALID_VALUE
,
1398 "glMapBufferRange(offset = %ld)", (long)offset
);
1403 _mesa_error(ctx
, GL_INVALID_VALUE
,
1404 "glMapBufferRange(length = %ld)", (long)length
);
1408 if (access
& ~(GL_MAP_READ_BIT
|
1410 GL_MAP_INVALIDATE_RANGE_BIT
|
1411 GL_MAP_INVALIDATE_BUFFER_BIT
|
1412 GL_MAP_FLUSH_EXPLICIT_BIT
|
1413 GL_MAP_UNSYNCHRONIZED_BIT
)) {
1414 /* generate an error if any undefind bit is set */
1415 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1419 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1420 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1421 "glMapBufferRange(access indicates neither read or write)");
1425 if ((access
& GL_MAP_READ_BIT
) &&
1426 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1427 GL_MAP_INVALIDATE_BUFFER_BIT
|
1428 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1429 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1430 "glMapBufferRange(invalid access flags)");
1434 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1435 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1436 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1437 "glMapBufferRange(invalid access flags)");
1441 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
);
1445 if (offset
+ length
> bufObj
->Size
) {
1446 _mesa_error(ctx
, GL_INVALID_VALUE
,
1447 "glMapBufferRange(offset + length > size)");
1451 if (_mesa_bufferobj_mapped(bufObj
)) {
1452 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1453 "glMapBufferRange(buffer already mapped)");
1457 if (!bufObj
->Size
) {
1458 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1459 "glMapBufferRange(buffer size = 0)");
1463 /* Mapping zero bytes should return a non-null pointer. */
1465 static long dummy
= 0;
1466 bufObj
->Pointer
= &dummy
;
1467 bufObj
->Length
= length
;
1468 bufObj
->Offset
= offset
;
1469 bufObj
->AccessFlags
= access
;
1470 return bufObj
->Pointer
;
1473 ASSERT(ctx
->Driver
.MapBufferRange
);
1474 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
);
1476 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1479 /* The driver callback should have set all these fields.
1480 * This is important because other modules (like VBO) might call
1481 * the driver function directly.
1483 ASSERT(bufObj
->Pointer
== map
);
1484 ASSERT(bufObj
->Length
== length
);
1485 ASSERT(bufObj
->Offset
== offset
);
1486 ASSERT(bufObj
->AccessFlags
== access
);
1494 * See GL_ARB_map_buffer_range spec
1497 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1499 GET_CURRENT_CONTEXT(ctx
);
1500 struct gl_buffer_object
*bufObj
;
1501 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1503 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1504 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1505 "glFlushMappedBufferRange(extension not supported)");
1510 _mesa_error(ctx
, GL_INVALID_VALUE
,
1511 "glFlushMappedBufferRange(offset = %ld)", (long)offset
);
1516 _mesa_error(ctx
, GL_INVALID_VALUE
,
1517 "glFlushMappedBufferRange(length = %ld)", (long)length
);
1521 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
);
1525 if (!_mesa_bufferobj_mapped(bufObj
)) {
1526 /* buffer is not mapped */
1527 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1528 "glFlushMappedBufferRange(buffer is not mapped)");
1532 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1533 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1534 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1538 if (offset
+ length
> bufObj
->Length
) {
1539 _mesa_error(ctx
, GL_INVALID_VALUE
,
1540 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1541 (long)offset
, (long)length
, (long)bufObj
->Length
);
1545 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1547 if (ctx
->Driver
.FlushMappedBufferRange
)
1548 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
);
1552 #if FEATURE_APPLE_object_purgeable
1554 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1556 struct gl_buffer_object
*bufObj
;
1559 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1561 _mesa_error(ctx
, GL_INVALID_VALUE
,
1562 "glObjectPurgeable(name = 0x%x)", name
);
1565 if (!_mesa_is_bufferobj(bufObj
)) {
1566 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1570 if (bufObj
->Purgeable
) {
1571 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1572 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1573 return GL_VOLATILE_APPLE
;
1576 bufObj
->Purgeable
= GL_TRUE
;
1578 retval
= GL_VOLATILE_APPLE
;
1579 if (ctx
->Driver
.BufferObjectPurgeable
)
1580 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1587 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1589 struct gl_renderbuffer
*bufObj
;
1592 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1594 _mesa_error(ctx
, GL_INVALID_VALUE
,
1595 "glObjectUnpurgeable(name = 0x%x)", name
);
1599 if (bufObj
->Purgeable
) {
1600 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1601 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1602 return GL_VOLATILE_APPLE
;
1605 bufObj
->Purgeable
= GL_TRUE
;
1607 retval
= GL_VOLATILE_APPLE
;
1608 if (ctx
->Driver
.RenderObjectPurgeable
)
1609 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1616 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1618 struct gl_texture_object
*bufObj
;
1621 bufObj
= _mesa_lookup_texture(ctx
, name
);
1623 _mesa_error(ctx
, GL_INVALID_VALUE
,
1624 "glObjectPurgeable(name = 0x%x)", name
);
1628 if (bufObj
->Purgeable
) {
1629 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1630 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1631 return GL_VOLATILE_APPLE
;
1634 bufObj
->Purgeable
= GL_TRUE
;
1636 retval
= GL_VOLATILE_APPLE
;
1637 if (ctx
->Driver
.TextureObjectPurgeable
)
1638 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1645 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1649 GET_CURRENT_CONTEXT(ctx
);
1650 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1653 _mesa_error(ctx
, GL_INVALID_VALUE
,
1654 "glObjectPurgeable(name = 0x%x)", name
);
1659 case GL_VOLATILE_APPLE
:
1660 case GL_RELEASED_APPLE
:
1664 _mesa_error(ctx
, GL_INVALID_ENUM
,
1665 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1670 switch (objectType
) {
1672 retval
= texture_object_purgeable(ctx
, name
, option
);
1674 case GL_RENDERBUFFER_EXT
:
1675 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1677 case GL_BUFFER_OBJECT_APPLE
:
1678 retval
= buffer_object_purgeable(ctx
, name
, option
);
1681 _mesa_error(ctx
, GL_INVALID_ENUM
,
1682 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1687 /* In strict conformance to the spec, we must only return VOLATILE when
1688 * when passed the VOLATILE option. Madness.
1690 * XXX First fix the spec, then fix me.
1692 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1697 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1699 struct gl_buffer_object
*bufObj
;
1702 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1704 _mesa_error(ctx
, GL_INVALID_VALUE
,
1705 "glObjectUnpurgeable(name = 0x%x)", name
);
1709 if (! bufObj
->Purgeable
) {
1710 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1711 "glObjectUnpurgeable(name = 0x%x) object is "
1712 " already \"unpurged\"", name
);
1716 bufObj
->Purgeable
= GL_FALSE
;
1719 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1720 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1727 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1729 struct gl_renderbuffer
*bufObj
;
1732 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1734 _mesa_error(ctx
, GL_INVALID_VALUE
,
1735 "glObjectUnpurgeable(name = 0x%x)", name
);
1739 if (! bufObj
->Purgeable
) {
1740 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1741 "glObjectUnpurgeable(name = 0x%x) object is "
1742 " already \"unpurged\"", name
);
1746 bufObj
->Purgeable
= GL_FALSE
;
1749 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1750 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1757 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1759 struct gl_texture_object
*bufObj
;
1762 bufObj
= _mesa_lookup_texture(ctx
, name
);
1764 _mesa_error(ctx
, GL_INVALID_VALUE
,
1765 "glObjectUnpurgeable(name = 0x%x)", name
);
1769 if (! bufObj
->Purgeable
) {
1770 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1771 "glObjectUnpurgeable(name = 0x%x) object is"
1772 " already \"unpurged\"", name
);
1776 bufObj
->Purgeable
= GL_FALSE
;
1779 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1780 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1787 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1789 GET_CURRENT_CONTEXT(ctx
);
1790 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1793 _mesa_error(ctx
, GL_INVALID_VALUE
,
1794 "glObjectUnpurgeable(name = 0x%x)", name
);
1799 case GL_RETAINED_APPLE
:
1800 case GL_UNDEFINED_APPLE
:
1804 _mesa_error(ctx
, GL_INVALID_ENUM
,
1805 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1810 switch (objectType
) {
1811 case GL_BUFFER_OBJECT_APPLE
:
1812 return buffer_object_unpurgeable(ctx
, name
, option
);
1814 return texture_object_unpurgeable(ctx
, name
, option
);
1815 case GL_RENDERBUFFER_EXT
:
1816 return renderbuffer_unpurgeable(ctx
, name
, option
);
1818 _mesa_error(ctx
, GL_INVALID_ENUM
,
1819 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1827 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1828 GLenum pname
, GLint
*params
)
1830 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1832 _mesa_error(ctx
, GL_INVALID_VALUE
,
1833 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1838 case GL_PURGEABLE_APPLE
:
1839 *params
= bufObj
->Purgeable
;
1842 _mesa_error(ctx
, GL_INVALID_ENUM
,
1843 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1851 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1852 GLenum pname
, GLint
*params
)
1854 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1856 _mesa_error(ctx
, GL_INVALID_VALUE
,
1857 "glObjectUnpurgeable(name = 0x%x)", name
);
1862 case GL_PURGEABLE_APPLE
:
1863 *params
= rb
->Purgeable
;
1866 _mesa_error(ctx
, GL_INVALID_ENUM
,
1867 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1875 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1876 GLenum pname
, GLint
*params
)
1878 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1880 _mesa_error(ctx
, GL_INVALID_VALUE
,
1881 "glObjectUnpurgeable(name = 0x%x)", name
);
1886 case GL_PURGEABLE_APPLE
:
1887 *params
= texObj
->Purgeable
;
1890 _mesa_error(ctx
, GL_INVALID_ENUM
,
1891 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1899 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
1902 GET_CURRENT_CONTEXT(ctx
);
1905 _mesa_error(ctx
, GL_INVALID_VALUE
,
1906 "glGetObjectParameteriv(name = 0x%x)", name
);
1910 switch (objectType
) {
1912 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
1914 case GL_BUFFER_OBJECT_APPLE
:
1915 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
1917 case GL_RENDERBUFFER_EXT
:
1918 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
1921 _mesa_error(ctx
, GL_INVALID_ENUM
,
1922 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1927 #endif /* FEATURE_APPLE_object_purgeable */