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 ARB_copy_buffer binding points */
825 if (ctx
->CopyReadBuffer
== bufObj
) {
826 _mesa_BindBufferARB( GL_COPY_READ_BUFFER
, 0 );
828 if (ctx
->CopyWriteBuffer
== bufObj
) {
829 _mesa_BindBufferARB( GL_COPY_WRITE_BUFFER
, 0 );
832 /* unbind transform feedback binding point */
833 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
834 _mesa_BindBufferARB( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
837 /* unbind any pixel pack/unpack pointers bound to this buffer */
838 if (ctx
->Pack
.BufferObj
== bufObj
) {
839 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
841 if (ctx
->Unpack
.BufferObj
== bufObj
) {
842 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
845 /* The ID is immediately freed for re-use */
846 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
847 /* Make sure we do not run into the classic ABA problem on bind.
848 * We don't want to allow re-binding a buffer object that's been
849 * "deleted" by glDeleteBuffers().
851 * The explicit rebinding to the default object in the current context
852 * prevents the above in the current context, but another context
853 * sharing the same objects might suffer from this problem.
854 * The alternative would be to do the hash lookup in any case on bind
855 * which would introduce more runtime overhead than this.
857 bufObj
->DeletePending
= GL_TRUE
;
858 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
862 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
867 * Generate a set of unique buffer object IDs and store them in \c buffer.
869 * \param n Number of IDs to generate.
870 * \param buffer Array of \c n locations to store the IDs.
873 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
875 GET_CURRENT_CONTEXT(ctx
);
878 ASSERT_OUTSIDE_BEGIN_END(ctx
);
880 if (MESA_VERBOSE
& VERBOSE_API
)
881 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
884 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
893 * This must be atomic (generation and allocation of buffer object IDs)
895 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
897 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
899 /* Insert the ID and pointer to dummy buffer object into hash table */
900 for (i
= 0; i
< n
; i
++) {
901 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
903 buffer
[i
] = first
+ i
;
906 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
911 * Determine if ID is the name of a buffer object.
913 * \param id ID of the potential buffer object.
914 * \return \c GL_TRUE if \c id is the name of a buffer object,
915 * \c GL_FALSE otherwise.
918 _mesa_IsBufferARB(GLuint id
)
920 struct gl_buffer_object
*bufObj
;
921 GET_CURRENT_CONTEXT(ctx
);
922 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
924 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
925 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
926 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
928 return bufObj
&& bufObj
!= &DummyBufferObject
;
933 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
934 const GLvoid
* data
, GLenum usage
)
936 GET_CURRENT_CONTEXT(ctx
);
937 struct gl_buffer_object
*bufObj
;
938 ASSERT_OUTSIDE_BEGIN_END(ctx
);
940 if (MESA_VERBOSE
& VERBOSE_API
)
941 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
942 _mesa_lookup_enum_by_nr(target
),
943 (long int) size
, data
,
944 _mesa_lookup_enum_by_nr(usage
));
947 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
952 case GL_STREAM_DRAW_ARB
:
953 case GL_STREAM_READ_ARB
:
954 case GL_STREAM_COPY_ARB
:
955 case GL_STATIC_DRAW_ARB
:
956 case GL_STATIC_READ_ARB
:
957 case GL_STATIC_COPY_ARB
:
958 case GL_DYNAMIC_DRAW_ARB
:
959 case GL_DYNAMIC_READ_ARB
:
960 case GL_DYNAMIC_COPY_ARB
:
964 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
968 bufObj
= get_buffer(ctx
, "glBufferDataARB", target
);
972 if (_mesa_bufferobj_mapped(bufObj
)) {
973 /* Unmap the existing buffer. We'll replace it now. Not an error. */
974 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
975 bufObj
->AccessFlags
= default_access_mode(ctx
);
976 ASSERT(bufObj
->Pointer
== NULL
);
979 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
981 bufObj
->Written
= GL_TRUE
;
984 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
985 bufObj
->Name
, size
, data
, usage
);
992 ASSERT(ctx
->Driver
.BufferData
);
993 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
994 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1000 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1001 GLsizeiptrARB size
, const GLvoid
* data
)
1003 GET_CURRENT_CONTEXT(ctx
);
1004 struct gl_buffer_object
*bufObj
;
1005 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1007 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1008 "glBufferSubDataARB" );
1010 /* error already recorded */
1017 bufObj
->Written
= GL_TRUE
;
1019 ASSERT(ctx
->Driver
.BufferSubData
);
1020 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
1025 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1026 GLsizeiptrARB size
, void * data
)
1028 GET_CURRENT_CONTEXT(ctx
);
1029 struct gl_buffer_object
*bufObj
;
1030 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1032 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1033 "glGetBufferSubDataARB" );
1035 /* error already recorded */
1039 ASSERT(ctx
->Driver
.GetBufferSubData
);
1040 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
1045 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1047 GET_CURRENT_CONTEXT(ctx
);
1048 struct gl_buffer_object
* bufObj
;
1049 GLbitfield accessFlags
;
1052 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1055 case GL_READ_ONLY_ARB
:
1056 accessFlags
= GL_MAP_READ_BIT
;
1058 case GL_WRITE_ONLY_ARB
:
1059 accessFlags
= GL_MAP_WRITE_BIT
;
1061 case GL_READ_WRITE_ARB
:
1062 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1065 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1069 bufObj
= get_buffer(ctx
, "glMapBufferARB", target
);
1073 if (_mesa_bufferobj_mapped(bufObj
)) {
1074 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1078 if (!bufObj
->Size
) {
1079 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1080 "glMapBuffer(buffer size = 0)");
1084 ASSERT(ctx
->Driver
.MapBufferRange
);
1085 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
);
1087 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1091 /* The driver callback should have set these fields.
1092 * This is important because other modules (like VBO) might call
1093 * the driver function directly.
1095 ASSERT(bufObj
->Pointer
== map
);
1096 ASSERT(bufObj
->Length
== bufObj
->Size
);
1097 ASSERT(bufObj
->Offset
== 0);
1098 bufObj
->AccessFlags
= accessFlags
;
1101 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1102 bufObj
->Written
= GL_TRUE
;
1105 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1106 bufObj
->Name
, bufObj
->Size
, access
);
1107 if (access
== GL_WRITE_ONLY_ARB
) {
1109 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1110 for (i
= 0; i
< bufObj
->Size
; i
++)
1117 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1119 /* buffer is 100 bytes larger than requested, fill with magic value */
1120 for (i
= 0; i
< 100; i
++) {
1121 buf
[bufObj
->Size
- i
- 1] = 123;
1126 return bufObj
->Pointer
;
1130 GLboolean GLAPIENTRY
1131 _mesa_UnmapBufferARB(GLenum target
)
1133 GET_CURRENT_CONTEXT(ctx
);
1134 struct gl_buffer_object
*bufObj
;
1135 GLboolean status
= GL_TRUE
;
1136 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1138 bufObj
= get_buffer(ctx
, "glUnmapBufferARB", target
);
1142 if (!_mesa_bufferobj_mapped(bufObj
)) {
1143 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1148 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1149 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1151 /* check that last 100 bytes are still = magic value */
1152 for (i
= 0; i
< 100; i
++) {
1153 GLuint pos
= bufObj
->Size
- i
- 1;
1154 if (buf
[pos
] != 123) {
1155 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1156 " at position %d (value = %u)\n",
1164 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1165 GLuint i
, unchanged
= 0;
1166 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1168 /* check which bytes changed */
1169 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1170 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1177 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1178 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1183 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1184 bufObj
->AccessFlags
= default_access_mode(ctx
);
1185 ASSERT(bufObj
->Pointer
== NULL
);
1186 ASSERT(bufObj
->Offset
== 0);
1187 ASSERT(bufObj
->Length
== 0);
1194 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1196 GET_CURRENT_CONTEXT(ctx
);
1197 struct gl_buffer_object
*bufObj
;
1198 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1200 bufObj
= get_buffer(ctx
, "glGetBufferParameterivARB", target
);
1205 case GL_BUFFER_SIZE_ARB
:
1206 *params
= (GLint
) bufObj
->Size
;
1208 case GL_BUFFER_USAGE_ARB
:
1209 *params
= bufObj
->Usage
;
1211 case GL_BUFFER_ACCESS_ARB
:
1212 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1214 case GL_BUFFER_MAPPED_ARB
:
1215 *params
= _mesa_bufferobj_mapped(bufObj
);
1217 case GL_BUFFER_ACCESS_FLAGS
:
1218 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1220 *params
= bufObj
->AccessFlags
;
1222 case GL_BUFFER_MAP_OFFSET
:
1223 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1225 *params
= (GLint
) bufObj
->Offset
;
1227 case GL_BUFFER_MAP_LENGTH
:
1228 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1230 *params
= (GLint
) bufObj
->Length
;
1233 ; /* fall-through */
1237 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1238 _mesa_lookup_enum_by_nr(pname
));
1244 * This is pretty much a duplicate of GetBufferParameteriv() but the
1245 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1248 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1250 GET_CURRENT_CONTEXT(ctx
);
1251 struct gl_buffer_object
*bufObj
;
1252 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1254 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
);
1259 case GL_BUFFER_SIZE_ARB
:
1260 *params
= bufObj
->Size
;
1262 case GL_BUFFER_USAGE_ARB
:
1263 *params
= bufObj
->Usage
;
1265 case GL_BUFFER_ACCESS_ARB
:
1266 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1268 case GL_BUFFER_ACCESS_FLAGS
:
1269 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1271 *params
= bufObj
->AccessFlags
;
1273 case GL_BUFFER_MAPPED_ARB
:
1274 *params
= _mesa_bufferobj_mapped(bufObj
);
1276 case GL_BUFFER_MAP_OFFSET
:
1277 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1279 *params
= bufObj
->Offset
;
1281 case GL_BUFFER_MAP_LENGTH
:
1282 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1284 *params
= bufObj
->Length
;
1287 ; /* fall-through */
1291 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1292 _mesa_lookup_enum_by_nr(pname
));
1297 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1299 GET_CURRENT_CONTEXT(ctx
);
1300 struct gl_buffer_object
* bufObj
;
1301 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1303 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1304 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1308 bufObj
= get_buffer(ctx
, "glGetBufferPointervARB", target
);
1312 *params
= bufObj
->Pointer
;
1317 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1318 GLintptr readOffset
, GLintptr writeOffset
,
1321 GET_CURRENT_CONTEXT(ctx
);
1322 struct gl_buffer_object
*src
, *dst
;
1323 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1325 src
= get_buffer(ctx
, "glCopyBuffserSubData", readTarget
);
1329 dst
= get_buffer(ctx
, "glCopyBuffserSubData", writeTarget
);
1333 if (_mesa_bufferobj_mapped(src
)) {
1334 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1335 "glCopyBuffserSubData(readBuffer is mapped)");
1339 if (_mesa_bufferobj_mapped(dst
)) {
1340 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1341 "glCopyBuffserSubData(writeBuffer is mapped)");
1345 if (readOffset
< 0) {
1346 _mesa_error(ctx
, GL_INVALID_VALUE
,
1347 "glCopyBuffserSubData(readOffset = %d)", (int) readOffset
);
1351 if (writeOffset
< 0) {
1352 _mesa_error(ctx
, GL_INVALID_VALUE
,
1353 "glCopyBuffserSubData(writeOffset = %d)", (int) writeOffset
);
1357 if (readOffset
+ size
> src
->Size
) {
1358 _mesa_error(ctx
, GL_INVALID_VALUE
,
1359 "glCopyBuffserSubData(readOffset + size = %d)",
1360 (int) (readOffset
+ size
));
1364 if (writeOffset
+ size
> dst
->Size
) {
1365 _mesa_error(ctx
, GL_INVALID_VALUE
,
1366 "glCopyBuffserSubData(writeOffset + size = %d)",
1367 (int) (writeOffset
+ size
));
1372 if (readOffset
+ size
<= writeOffset
) {
1375 else if (writeOffset
+ size
<= readOffset
) {
1379 /* overlapping src/dst is illegal */
1380 _mesa_error(ctx
, GL_INVALID_VALUE
,
1381 "glCopyBuffserSubData(overlapping src/dst)");
1386 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1391 * See GL_ARB_map_buffer_range spec
1394 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1397 GET_CURRENT_CONTEXT(ctx
);
1398 struct gl_buffer_object
*bufObj
;
1401 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1403 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1404 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1405 "glMapBufferRange(extension not supported)");
1410 _mesa_error(ctx
, GL_INVALID_VALUE
,
1411 "glMapBufferRange(offset = %ld)", (long)offset
);
1416 _mesa_error(ctx
, GL_INVALID_VALUE
,
1417 "glMapBufferRange(length = %ld)", (long)length
);
1421 if (access
& ~(GL_MAP_READ_BIT
|
1423 GL_MAP_INVALIDATE_RANGE_BIT
|
1424 GL_MAP_INVALIDATE_BUFFER_BIT
|
1425 GL_MAP_FLUSH_EXPLICIT_BIT
|
1426 GL_MAP_UNSYNCHRONIZED_BIT
)) {
1427 /* generate an error if any undefind bit is set */
1428 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1432 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1433 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1434 "glMapBufferRange(access indicates neither read or write)");
1438 if ((access
& GL_MAP_READ_BIT
) &&
1439 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1440 GL_MAP_INVALIDATE_BUFFER_BIT
|
1441 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1442 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1443 "glMapBufferRange(invalid access flags)");
1447 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1448 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1449 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1450 "glMapBufferRange(invalid access flags)");
1454 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
);
1458 if (offset
+ length
> bufObj
->Size
) {
1459 _mesa_error(ctx
, GL_INVALID_VALUE
,
1460 "glMapBufferRange(offset + length > size)");
1464 if (_mesa_bufferobj_mapped(bufObj
)) {
1465 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1466 "glMapBufferRange(buffer already mapped)");
1470 if (!bufObj
->Size
) {
1471 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1472 "glMapBufferRange(buffer size = 0)");
1476 /* Mapping zero bytes should return a non-null pointer. */
1478 static long dummy
= 0;
1479 bufObj
->Pointer
= &dummy
;
1480 bufObj
->Length
= length
;
1481 bufObj
->Offset
= offset
;
1482 bufObj
->AccessFlags
= access
;
1483 return bufObj
->Pointer
;
1486 ASSERT(ctx
->Driver
.MapBufferRange
);
1487 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
);
1489 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1492 /* The driver callback should have set all these fields.
1493 * This is important because other modules (like VBO) might call
1494 * the driver function directly.
1496 ASSERT(bufObj
->Pointer
== map
);
1497 ASSERT(bufObj
->Length
== length
);
1498 ASSERT(bufObj
->Offset
== offset
);
1499 ASSERT(bufObj
->AccessFlags
== access
);
1507 * See GL_ARB_map_buffer_range spec
1510 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1512 GET_CURRENT_CONTEXT(ctx
);
1513 struct gl_buffer_object
*bufObj
;
1514 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1516 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1517 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1518 "glFlushMappedBufferRange(extension not supported)");
1523 _mesa_error(ctx
, GL_INVALID_VALUE
,
1524 "glFlushMappedBufferRange(offset = %ld)", (long)offset
);
1529 _mesa_error(ctx
, GL_INVALID_VALUE
,
1530 "glFlushMappedBufferRange(length = %ld)", (long)length
);
1534 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
);
1538 if (!_mesa_bufferobj_mapped(bufObj
)) {
1539 /* buffer is not mapped */
1540 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1541 "glFlushMappedBufferRange(buffer is not mapped)");
1545 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1546 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1547 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1551 if (offset
+ length
> bufObj
->Length
) {
1552 _mesa_error(ctx
, GL_INVALID_VALUE
,
1553 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1554 (long)offset
, (long)length
, (long)bufObj
->Length
);
1558 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1560 if (ctx
->Driver
.FlushMappedBufferRange
)
1561 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
);
1565 #if FEATURE_APPLE_object_purgeable
1567 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1569 struct gl_buffer_object
*bufObj
;
1572 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1574 _mesa_error(ctx
, GL_INVALID_VALUE
,
1575 "glObjectPurgeable(name = 0x%x)", name
);
1578 if (!_mesa_is_bufferobj(bufObj
)) {
1579 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
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
.BufferObjectPurgeable
)
1593 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1600 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1602 struct gl_renderbuffer
*bufObj
;
1605 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1607 _mesa_error(ctx
, GL_INVALID_VALUE
,
1608 "glObjectUnpurgeable(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
.RenderObjectPurgeable
)
1622 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1629 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1631 struct gl_texture_object
*bufObj
;
1634 bufObj
= _mesa_lookup_texture(ctx
, name
);
1636 _mesa_error(ctx
, GL_INVALID_VALUE
,
1637 "glObjectPurgeable(name = 0x%x)", name
);
1641 if (bufObj
->Purgeable
) {
1642 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1643 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1644 return GL_VOLATILE_APPLE
;
1647 bufObj
->Purgeable
= GL_TRUE
;
1649 retval
= GL_VOLATILE_APPLE
;
1650 if (ctx
->Driver
.TextureObjectPurgeable
)
1651 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1658 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1662 GET_CURRENT_CONTEXT(ctx
);
1663 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1666 _mesa_error(ctx
, GL_INVALID_VALUE
,
1667 "glObjectPurgeable(name = 0x%x)", name
);
1672 case GL_VOLATILE_APPLE
:
1673 case GL_RELEASED_APPLE
:
1677 _mesa_error(ctx
, GL_INVALID_ENUM
,
1678 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1683 switch (objectType
) {
1685 retval
= texture_object_purgeable(ctx
, name
, option
);
1687 case GL_RENDERBUFFER_EXT
:
1688 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1690 case GL_BUFFER_OBJECT_APPLE
:
1691 retval
= buffer_object_purgeable(ctx
, name
, option
);
1694 _mesa_error(ctx
, GL_INVALID_ENUM
,
1695 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1700 /* In strict conformance to the spec, we must only return VOLATILE when
1701 * when passed the VOLATILE option. Madness.
1703 * XXX First fix the spec, then fix me.
1705 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1710 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1712 struct gl_buffer_object
*bufObj
;
1715 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1717 _mesa_error(ctx
, GL_INVALID_VALUE
,
1718 "glObjectUnpurgeable(name = 0x%x)", name
);
1722 if (! bufObj
->Purgeable
) {
1723 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1724 "glObjectUnpurgeable(name = 0x%x) object is "
1725 " already \"unpurged\"", name
);
1729 bufObj
->Purgeable
= GL_FALSE
;
1732 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1733 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1740 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1742 struct gl_renderbuffer
*bufObj
;
1745 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1747 _mesa_error(ctx
, GL_INVALID_VALUE
,
1748 "glObjectUnpurgeable(name = 0x%x)", name
);
1752 if (! bufObj
->Purgeable
) {
1753 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1754 "glObjectUnpurgeable(name = 0x%x) object is "
1755 " already \"unpurged\"", name
);
1759 bufObj
->Purgeable
= GL_FALSE
;
1762 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1763 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1770 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1772 struct gl_texture_object
*bufObj
;
1775 bufObj
= _mesa_lookup_texture(ctx
, name
);
1777 _mesa_error(ctx
, GL_INVALID_VALUE
,
1778 "glObjectUnpurgeable(name = 0x%x)", name
);
1782 if (! bufObj
->Purgeable
) {
1783 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1784 "glObjectUnpurgeable(name = 0x%x) object is"
1785 " already \"unpurged\"", name
);
1789 bufObj
->Purgeable
= GL_FALSE
;
1792 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1793 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1800 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1802 GET_CURRENT_CONTEXT(ctx
);
1803 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1806 _mesa_error(ctx
, GL_INVALID_VALUE
,
1807 "glObjectUnpurgeable(name = 0x%x)", name
);
1812 case GL_RETAINED_APPLE
:
1813 case GL_UNDEFINED_APPLE
:
1817 _mesa_error(ctx
, GL_INVALID_ENUM
,
1818 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1823 switch (objectType
) {
1824 case GL_BUFFER_OBJECT_APPLE
:
1825 return buffer_object_unpurgeable(ctx
, name
, option
);
1827 return texture_object_unpurgeable(ctx
, name
, option
);
1828 case GL_RENDERBUFFER_EXT
:
1829 return renderbuffer_unpurgeable(ctx
, name
, option
);
1831 _mesa_error(ctx
, GL_INVALID_ENUM
,
1832 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1840 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1841 GLenum pname
, GLint
*params
)
1843 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1845 _mesa_error(ctx
, GL_INVALID_VALUE
,
1846 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1851 case GL_PURGEABLE_APPLE
:
1852 *params
= bufObj
->Purgeable
;
1855 _mesa_error(ctx
, GL_INVALID_ENUM
,
1856 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1864 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1865 GLenum pname
, GLint
*params
)
1867 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1869 _mesa_error(ctx
, GL_INVALID_VALUE
,
1870 "glObjectUnpurgeable(name = 0x%x)", name
);
1875 case GL_PURGEABLE_APPLE
:
1876 *params
= rb
->Purgeable
;
1879 _mesa_error(ctx
, GL_INVALID_ENUM
,
1880 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1888 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1889 GLenum pname
, GLint
*params
)
1891 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1893 _mesa_error(ctx
, GL_INVALID_VALUE
,
1894 "glObjectUnpurgeable(name = 0x%x)", name
);
1899 case GL_PURGEABLE_APPLE
:
1900 *params
= texObj
->Purgeable
;
1903 _mesa_error(ctx
, GL_INVALID_ENUM
,
1904 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1912 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
1915 GET_CURRENT_CONTEXT(ctx
);
1918 _mesa_error(ctx
, GL_INVALID_VALUE
,
1919 "glGetObjectParameteriv(name = 0x%x)", name
);
1923 switch (objectType
) {
1925 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
1927 case GL_BUFFER_OBJECT_APPLE
:
1928 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
1930 case GL_RENDERBUFFER_EXT
:
1931 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
1934 _mesa_error(ctx
, GL_INVALID_ENUM
,
1935 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1940 #endif /* FEATURE_APPLE_object_purgeable */