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 case GL_TEXTURE_BUFFER
:
83 if (ctx
->Extensions
.ARB_texture_buffer_object
) {
84 return &ctx
->Texture
.BufferObject
;
95 * Get the buffer object bound to the specified target in a GL context.
96 * \param ctx the GL context
97 * \param target the buffer object target to be retrieved.
98 * \return pointer to the buffer object bound to \c target in the
99 * specified context or \c NULL if \c target is invalid.
101 static inline struct gl_buffer_object
*
102 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
)
104 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
107 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
111 if (!_mesa_is_bufferobj(*bufObj
)) {
112 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(buffer 0)", func
);
121 default_access_mode(const struct gl_context
*ctx
)
124 return GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
129 * Convert a GLbitfield describing the mapped buffer access flags
130 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
133 simplified_access_mode(GLbitfield access
)
135 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
136 if ((access
& rwFlags
) == rwFlags
)
137 return GL_READ_WRITE
;
138 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
140 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
141 return GL_WRITE_ONLY
;
142 return GL_READ_WRITE
; /* this should never happen, but no big deal */
147 * Tests the subdata range parameters and sets the GL error code for
148 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
150 * \param ctx GL context.
151 * \param target Buffer object target on which to operate.
152 * \param offset Offset of the first byte of the subdata range.
153 * \param size Size, in bytes, of the subdata range.
154 * \param caller Name of calling function for recording errors.
155 * \return A pointer to the buffer object bound to \c target in the
156 * specified context or \c NULL if any of the parameter or state
157 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
160 * \sa glBufferSubDataARB, glGetBufferSubDataARB
162 static struct gl_buffer_object
*
163 buffer_object_subdata_range_good( struct gl_context
* ctx
, GLenum target
,
164 GLintptrARB offset
, GLsizeiptrARB size
,
167 struct gl_buffer_object
*bufObj
;
170 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
175 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
179 bufObj
= get_buffer(ctx
, caller
, target
);
183 if (offset
+ size
> bufObj
->Size
) {
184 _mesa_error(ctx
, GL_INVALID_VALUE
,
185 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
186 (unsigned long) offset
,
187 (unsigned long) size
,
188 (unsigned long) bufObj
->Size
);
191 if (_mesa_bufferobj_mapped(bufObj
)) {
192 /* Buffer is currently mapped */
193 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
202 * Allocate and initialize a new buffer object.
204 * Default callback for the \c dd_function_table::NewBufferObject() hook.
206 static struct gl_buffer_object
*
207 _mesa_new_buffer_object( struct gl_context
*ctx
, GLuint name
, GLenum target
)
209 struct gl_buffer_object
*obj
;
213 obj
= MALLOC_STRUCT(gl_buffer_object
);
214 _mesa_initialize_buffer_object(ctx
, obj
, name
, target
);
220 * Delete a buffer object.
222 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
225 _mesa_delete_buffer_object(struct gl_context
*ctx
,
226 struct gl_buffer_object
*bufObj
)
233 /* assign strange values here to help w/ debugging */
234 bufObj
->RefCount
= -1000;
237 _glthread_DESTROY_MUTEX(bufObj
->Mutex
);
244 * Set ptr to bufObj w/ reference counting.
245 * This is normally only called from the _mesa_reference_buffer_object() macro
246 * when there's a real pointer change.
249 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
250 struct gl_buffer_object
**ptr
,
251 struct gl_buffer_object
*bufObj
)
254 /* Unreference the old buffer */
255 GLboolean deleteFlag
= GL_FALSE
;
256 struct gl_buffer_object
*oldObj
= *ptr
;
258 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
259 ASSERT(oldObj
->RefCount
> 0);
262 printf("BufferObj %p %d DECR to %d\n",
263 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
265 deleteFlag
= (oldObj
->RefCount
== 0);
266 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
270 /* some sanity checking: don't delete a buffer still in use */
272 /* unfortunately, these tests are invalid during context tear-down */
273 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
274 ASSERT(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
!= bufObj
);
275 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
278 ASSERT(ctx
->Driver
.DeleteBuffer
);
279 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
287 /* reference new buffer */
288 _glthread_LOCK_MUTEX(bufObj
->Mutex
);
289 if (bufObj
->RefCount
== 0) {
290 /* this buffer's being deleted (look just above) */
291 /* Not sure this can every really happen. Warn if it does. */
292 _mesa_problem(NULL
, "referencing deleted buffer object");
298 printf("BufferObj %p %d INCR to %d\n",
299 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
303 _glthread_UNLOCK_MUTEX(bufObj
->Mutex
);
309 * Initialize a buffer object to default values.
312 _mesa_initialize_buffer_object( struct gl_context
*ctx
,
313 struct gl_buffer_object
*obj
,
314 GLuint name
, GLenum target
)
318 memset(obj
, 0, sizeof(struct gl_buffer_object
));
319 _glthread_INIT_MUTEX(obj
->Mutex
);
322 obj
->Usage
= GL_STATIC_DRAW_ARB
;
323 obj
->AccessFlags
= default_access_mode(ctx
);
328 * Allocate space for and store data in a buffer object. Any data that was
329 * previously stored in the buffer object is lost. If \c data is \c NULL,
330 * memory will be allocated, but no copy will occur.
332 * This is the default callback for \c dd_function_table::BufferData()
333 * Note that all GL error checking will have been done already.
335 * \param ctx GL context.
336 * \param target Buffer object target on which to operate.
337 * \param size Size, in bytes, of the new data store.
338 * \param data Pointer to the data to store in the buffer object. This
339 * pointer may be \c NULL.
340 * \param usage Hints about how the data will be used.
341 * \param bufObj Object to be used.
343 * \return GL_TRUE for success, GL_FALSE for failure
344 * \sa glBufferDataARB, dd_function_table::BufferData.
347 _mesa_buffer_data( struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
348 const GLvoid
* data
, GLenum usage
,
349 struct gl_buffer_object
* bufObj
)
353 (void) ctx
; (void) target
;
355 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
357 bufObj
->Data
= (GLubyte
*) new_data
;
359 bufObj
->Usage
= usage
;
362 memcpy( bufObj
->Data
, data
, size
);
374 * Replace data in a subrange of buffer object. If the data range
375 * specified by \c size + \c offset extends beyond the end of the buffer or
376 * if \c data is \c NULL, no copy is performed.
378 * This is the default callback for \c dd_function_table::BufferSubData()
379 * Note that all GL error checking will have been done already.
381 * \param ctx GL context.
382 * \param target Buffer object target on which to operate.
383 * \param offset Offset of the first byte to be modified.
384 * \param size Size, in bytes, of the data range.
385 * \param data Pointer to the data to store in the buffer object.
386 * \param bufObj Object to be used.
388 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
391 _mesa_buffer_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
392 GLsizeiptrARB size
, const GLvoid
* data
,
393 struct gl_buffer_object
* bufObj
)
397 /* this should have been caught in _mesa_BufferSubData() */
398 ASSERT(size
+ offset
<= bufObj
->Size
);
401 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
407 * Retrieve data from a subrange of buffer object. If the data range
408 * specified by \c size + \c offset extends beyond the end of the buffer or
409 * if \c data is \c NULL, no copy is performed.
411 * This is the default callback for \c dd_function_table::GetBufferSubData()
412 * Note that all GL error checking will have been done already.
414 * \param ctx GL context.
415 * \param target Buffer object target on which to operate.
416 * \param offset Offset of the first byte to be fetched.
417 * \param size Size, in bytes, of the data range.
418 * \param data Destination for data
419 * \param bufObj Object to be used.
421 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
424 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
425 GLsizeiptrARB size
, GLvoid
* data
,
426 struct gl_buffer_object
* bufObj
)
430 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
431 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
437 * Default fallback for \c dd_function_table::MapBufferRange().
438 * Called via glMapBufferRange().
441 _mesa_buffer_map_range( struct gl_context
*ctx
, GLintptr offset
,
442 GLsizeiptr length
, GLbitfield access
,
443 struct gl_buffer_object
*bufObj
)
446 assert(!_mesa_bufferobj_mapped(bufObj
));
447 /* Just return a direct pointer to the data */
448 bufObj
->Pointer
= bufObj
->Data
+ offset
;
449 bufObj
->Length
= length
;
450 bufObj
->Offset
= offset
;
451 bufObj
->AccessFlags
= access
;
452 return bufObj
->Pointer
;
457 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
458 * Called via glFlushMappedBufferRange().
461 _mesa_buffer_flush_mapped_range( struct gl_context
*ctx
,
462 GLintptr offset
, GLsizeiptr length
,
463 struct gl_buffer_object
*obj
)
474 * Default callback for \c dd_function_table::MapBuffer().
476 * The input parameters will have been already tested for errors.
478 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
481 _mesa_buffer_unmap( struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
484 /* XXX we might assert here that bufObj->Pointer is non-null */
485 bufObj
->Pointer
= NULL
;
488 bufObj
->AccessFlags
= 0x0;
494 * Default fallback for \c dd_function_table::CopyBufferSubData().
495 * Called via glCopyBuffserSubData().
498 _mesa_copy_buffer_subdata(struct gl_context
*ctx
,
499 struct gl_buffer_object
*src
,
500 struct gl_buffer_object
*dst
,
501 GLintptr readOffset
, GLintptr writeOffset
,
504 GLubyte
*srcPtr
, *dstPtr
;
506 /* the buffers should not be mapped */
507 assert(!_mesa_bufferobj_mapped(src
));
508 assert(!_mesa_bufferobj_mapped(dst
));
511 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
513 GL_MAP_WRITE_BIT
, src
);
518 srcPtr
+= readOffset
;
519 dstPtr
+= writeOffset
;
521 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
522 GL_MAP_READ_BIT
, src
);
523 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
525 GL_MAP_INVALIDATE_RANGE_BIT
), dst
);
528 /* Note: the src and dst regions will never overlap. Trying to do so
529 * would generate GL_INVALID_VALUE earlier.
531 if (srcPtr
&& dstPtr
)
532 memcpy(dstPtr
, srcPtr
, size
);
534 ctx
->Driver
.UnmapBuffer(ctx
, src
);
536 ctx
->Driver
.UnmapBuffer(ctx
, dst
);
542 * Initialize the state associated with buffer objects
545 _mesa_init_buffer_objects( struct gl_context
*ctx
)
547 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
548 _glthread_INIT_MUTEX(DummyBufferObject
.Mutex
);
549 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
551 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
552 ctx
->Shared
->NullBufferObj
);
554 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
555 ctx
->Shared
->NullBufferObj
);
556 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
557 ctx
->Shared
->NullBufferObj
);
562 _mesa_free_buffer_objects( struct gl_context
*ctx
)
564 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
566 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
567 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
572 * Bind the specified target to buffer for the specified context.
573 * Called by glBindBuffer() and other functions.
576 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
578 struct gl_buffer_object
*oldBufObj
;
579 struct gl_buffer_object
*newBufObj
= NULL
;
580 struct gl_buffer_object
**bindTarget
= NULL
;
582 bindTarget
= get_buffer_target(ctx
, target
);
584 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
588 /* Get pointer to old buffer object (to be unbound) */
589 oldBufObj
= *bindTarget
;
590 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
591 return; /* rebinding the same buffer object- no change */
594 * Get pointer to new buffer object (newBufObj)
597 /* The spec says there's not a buffer object named 0, but we use
598 * one internally because it simplifies things.
600 newBufObj
= ctx
->Shared
->NullBufferObj
;
603 /* non-default buffer object */
604 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
605 if (!newBufObj
|| newBufObj
== &DummyBufferObject
) {
606 /* If this is a new buffer object id, or one which was generated but
607 * never used before, allocate a buffer object now.
609 ASSERT(ctx
->Driver
.NewBufferObject
);
610 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
612 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
615 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
619 /* bind new buffer */
620 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
622 /* Pass BindBuffer call to device driver */
623 if (ctx
->Driver
.BindBuffer
)
624 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
629 * Update the default buffer objects in the given context to reference those
630 * specified in the shared state and release those referencing the old
634 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
636 /* Bind the NullBufferObj to remove references to those
637 * in the shared context hash table.
639 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
640 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
641 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
642 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
648 * Return the gl_buffer_object for the given ID.
649 * Always return NULL for ID 0.
651 struct gl_buffer_object
*
652 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
657 return (struct gl_buffer_object
*)
658 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
663 * If *ptr points to obj, set ptr = the Null/default buffer object.
664 * This is a helper for buffer object deletion.
665 * The GL spec says that deleting a buffer object causes it to get
666 * unbound from all arrays in the current context.
669 unbind(struct gl_context
*ctx
,
670 struct gl_buffer_object
**ptr
,
671 struct gl_buffer_object
*obj
)
674 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
680 * Plug default/fallback buffer object functions into the device
684 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
686 /* GL_ARB_vertex/pixel_buffer_object */
687 driver
->NewBufferObject
= _mesa_new_buffer_object
;
688 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
689 driver
->BindBuffer
= NULL
;
690 driver
->BufferData
= _mesa_buffer_data
;
691 driver
->BufferSubData
= _mesa_buffer_subdata
;
692 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
693 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
695 /* GL_ARB_map_buffer_range */
696 driver
->MapBufferRange
= _mesa_buffer_map_range
;
697 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
699 /* GL_ARB_copy_buffer */
700 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
705 /**********************************************************************/
707 /**********************************************************************/
710 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
712 GET_CURRENT_CONTEXT(ctx
);
713 ASSERT_OUTSIDE_BEGIN_END(ctx
);
715 if (MESA_VERBOSE
& VERBOSE_API
)
716 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
717 _mesa_lookup_enum_by_nr(target
), buffer
);
719 bind_buffer_object(ctx
, target
, buffer
);
724 * Delete a set of buffer objects.
726 * \param n Number of buffer objects to delete.
727 * \param ids Array of \c n buffer object IDs.
730 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
732 GET_CURRENT_CONTEXT(ctx
);
734 ASSERT_OUTSIDE_BEGIN_END(ctx
);
735 FLUSH_VERTICES(ctx
, 0);
738 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
742 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
744 for (i
= 0; i
< n
; i
++) {
745 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
747 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
750 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
752 if (_mesa_bufferobj_mapped(bufObj
)) {
753 /* if mapped, unmap it now */
754 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
755 bufObj
->AccessFlags
= default_access_mode(ctx
);
756 bufObj
->Pointer
= NULL
;
759 /* unbind any vertex pointers bound to this buffer */
760 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
761 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
764 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
765 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
767 if (arrayObj
->ElementArrayBufferObj
== bufObj
) {
768 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
771 /* unbind ARB_copy_buffer binding points */
772 if (ctx
->CopyReadBuffer
== bufObj
) {
773 _mesa_BindBufferARB( GL_COPY_READ_BUFFER
, 0 );
775 if (ctx
->CopyWriteBuffer
== bufObj
) {
776 _mesa_BindBufferARB( GL_COPY_WRITE_BUFFER
, 0 );
779 /* unbind any pixel pack/unpack pointers bound to this buffer */
780 if (ctx
->Pack
.BufferObj
== bufObj
) {
781 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
783 if (ctx
->Unpack
.BufferObj
== bufObj
) {
784 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
787 /* The ID is immediately freed for re-use */
788 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
789 /* Make sure we do not run into the classic ABA problem on bind.
790 * We don't want to allow re-binding a buffer object that's been
791 * "deleted" by glDeleteBuffers().
793 * The explicit rebinding to the default object in the current context
794 * prevents the above in the current context, but another context
795 * sharing the same objects might suffer from this problem.
796 * The alternative would be to do the hash lookup in any case on bind
797 * which would introduce more runtime overhead than this.
799 bufObj
->DeletePending
= GL_TRUE
;
800 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
804 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
809 * Generate a set of unique buffer object IDs and store them in \c buffer.
811 * \param n Number of IDs to generate.
812 * \param buffer Array of \c n locations to store the IDs.
815 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
817 GET_CURRENT_CONTEXT(ctx
);
820 ASSERT_OUTSIDE_BEGIN_END(ctx
);
822 if (MESA_VERBOSE
& VERBOSE_API
)
823 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
826 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
835 * This must be atomic (generation and allocation of buffer object IDs)
837 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
839 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
841 /* Insert the ID and pointer to dummy buffer object into hash table */
842 for (i
= 0; i
< n
; i
++) {
843 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
845 buffer
[i
] = first
+ i
;
848 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
853 * Determine if ID is the name of a buffer object.
855 * \param id ID of the potential buffer object.
856 * \return \c GL_TRUE if \c id is the name of a buffer object,
857 * \c GL_FALSE otherwise.
860 _mesa_IsBufferARB(GLuint id
)
862 struct gl_buffer_object
*bufObj
;
863 GET_CURRENT_CONTEXT(ctx
);
864 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
866 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
867 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
868 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
870 return bufObj
&& bufObj
!= &DummyBufferObject
;
875 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
876 const GLvoid
* data
, GLenum usage
)
878 GET_CURRENT_CONTEXT(ctx
);
879 struct gl_buffer_object
*bufObj
;
880 ASSERT_OUTSIDE_BEGIN_END(ctx
);
882 if (MESA_VERBOSE
& VERBOSE_API
)
883 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
884 _mesa_lookup_enum_by_nr(target
),
885 (long int) size
, data
,
886 _mesa_lookup_enum_by_nr(usage
));
889 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
894 case GL_STREAM_DRAW_ARB
:
895 case GL_STREAM_READ_ARB
:
896 case GL_STREAM_COPY_ARB
:
897 case GL_STATIC_DRAW_ARB
:
898 case GL_STATIC_READ_ARB
:
899 case GL_STATIC_COPY_ARB
:
900 case GL_DYNAMIC_DRAW_ARB
:
901 case GL_DYNAMIC_READ_ARB
:
902 case GL_DYNAMIC_COPY_ARB
:
906 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
910 bufObj
= get_buffer(ctx
, "glBufferDataARB", target
);
914 if (_mesa_bufferobj_mapped(bufObj
)) {
915 /* Unmap the existing buffer. We'll replace it now. Not an error. */
916 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
917 bufObj
->AccessFlags
= default_access_mode(ctx
);
918 ASSERT(bufObj
->Pointer
== NULL
);
921 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
923 bufObj
->Written
= GL_TRUE
;
926 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
927 bufObj
->Name
, size
, data
, usage
);
934 ASSERT(ctx
->Driver
.BufferData
);
935 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
936 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
942 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
943 GLsizeiptrARB size
, const GLvoid
* data
)
945 GET_CURRENT_CONTEXT(ctx
);
946 struct gl_buffer_object
*bufObj
;
947 ASSERT_OUTSIDE_BEGIN_END(ctx
);
949 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
950 "glBufferSubDataARB" );
952 /* error already recorded */
959 bufObj
->Written
= GL_TRUE
;
961 ASSERT(ctx
->Driver
.BufferSubData
);
962 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
967 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
968 GLsizeiptrARB size
, void * data
)
970 GET_CURRENT_CONTEXT(ctx
);
971 struct gl_buffer_object
*bufObj
;
972 ASSERT_OUTSIDE_BEGIN_END(ctx
);
974 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
975 "glGetBufferSubDataARB" );
977 /* error already recorded */
981 ASSERT(ctx
->Driver
.GetBufferSubData
);
982 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
987 _mesa_MapBufferARB(GLenum target
, GLenum access
)
989 GET_CURRENT_CONTEXT(ctx
);
990 struct gl_buffer_object
* bufObj
;
991 GLbitfield accessFlags
;
994 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
997 case GL_READ_ONLY_ARB
:
998 accessFlags
= GL_MAP_READ_BIT
;
1000 case GL_WRITE_ONLY_ARB
:
1001 accessFlags
= GL_MAP_WRITE_BIT
;
1003 case GL_READ_WRITE_ARB
:
1004 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1007 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1011 bufObj
= get_buffer(ctx
, "glMapBufferARB", target
);
1015 if (_mesa_bufferobj_mapped(bufObj
)) {
1016 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1020 if (!bufObj
->Size
) {
1021 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1022 "glMapBuffer(buffer size = 0)");
1026 ASSERT(ctx
->Driver
.MapBufferRange
);
1027 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
);
1029 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1033 /* The driver callback should have set these fields.
1034 * This is important because other modules (like VBO) might call
1035 * the driver function directly.
1037 ASSERT(bufObj
->Pointer
== map
);
1038 ASSERT(bufObj
->Length
== bufObj
->Size
);
1039 ASSERT(bufObj
->Offset
== 0);
1040 bufObj
->AccessFlags
= accessFlags
;
1043 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1044 bufObj
->Written
= GL_TRUE
;
1047 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1048 bufObj
->Name
, bufObj
->Size
, access
);
1049 if (access
== GL_WRITE_ONLY_ARB
) {
1051 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1052 for (i
= 0; i
< bufObj
->Size
; i
++)
1059 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1061 /* buffer is 100 bytes larger than requested, fill with magic value */
1062 for (i
= 0; i
< 100; i
++) {
1063 buf
[bufObj
->Size
- i
- 1] = 123;
1068 return bufObj
->Pointer
;
1072 GLboolean GLAPIENTRY
1073 _mesa_UnmapBufferARB(GLenum target
)
1075 GET_CURRENT_CONTEXT(ctx
);
1076 struct gl_buffer_object
*bufObj
;
1077 GLboolean status
= GL_TRUE
;
1078 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1080 bufObj
= get_buffer(ctx
, "glUnmapBufferARB", target
);
1084 if (!_mesa_bufferobj_mapped(bufObj
)) {
1085 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1090 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1091 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1093 /* check that last 100 bytes are still = magic value */
1094 for (i
= 0; i
< 100; i
++) {
1095 GLuint pos
= bufObj
->Size
- i
- 1;
1096 if (buf
[pos
] != 123) {
1097 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1098 " at position %d (value = %u)\n",
1106 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1107 GLuint i
, unchanged
= 0;
1108 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1110 /* check which bytes changed */
1111 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1112 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1119 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1120 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1125 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1126 bufObj
->AccessFlags
= default_access_mode(ctx
);
1127 ASSERT(bufObj
->Pointer
== NULL
);
1128 ASSERT(bufObj
->Offset
== 0);
1129 ASSERT(bufObj
->Length
== 0);
1136 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1138 GET_CURRENT_CONTEXT(ctx
);
1139 struct gl_buffer_object
*bufObj
;
1140 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1142 bufObj
= get_buffer(ctx
, "glGetBufferParameterivARB", target
);
1147 case GL_BUFFER_SIZE_ARB
:
1148 *params
= (GLint
) bufObj
->Size
;
1150 case GL_BUFFER_USAGE_ARB
:
1151 *params
= bufObj
->Usage
;
1153 case GL_BUFFER_ACCESS_ARB
:
1154 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1156 case GL_BUFFER_MAPPED_ARB
:
1157 *params
= _mesa_bufferobj_mapped(bufObj
);
1159 case GL_BUFFER_ACCESS_FLAGS
:
1160 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1162 *params
= bufObj
->AccessFlags
;
1164 case GL_BUFFER_MAP_OFFSET
:
1165 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1167 *params
= (GLint
) bufObj
->Offset
;
1169 case GL_BUFFER_MAP_LENGTH
:
1170 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1172 *params
= (GLint
) bufObj
->Length
;
1175 ; /* fall-through */
1179 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1180 _mesa_lookup_enum_by_nr(pname
));
1186 * This is pretty much a duplicate of GetBufferParameteriv() but the
1187 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1190 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1192 GET_CURRENT_CONTEXT(ctx
);
1193 struct gl_buffer_object
*bufObj
;
1194 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1196 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
);
1201 case GL_BUFFER_SIZE_ARB
:
1202 *params
= bufObj
->Size
;
1204 case GL_BUFFER_USAGE_ARB
:
1205 *params
= bufObj
->Usage
;
1207 case GL_BUFFER_ACCESS_ARB
:
1208 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1210 case GL_BUFFER_ACCESS_FLAGS
:
1211 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1213 *params
= bufObj
->AccessFlags
;
1215 case GL_BUFFER_MAPPED_ARB
:
1216 *params
= _mesa_bufferobj_mapped(bufObj
);
1218 case GL_BUFFER_MAP_OFFSET
:
1219 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1221 *params
= bufObj
->Offset
;
1223 case GL_BUFFER_MAP_LENGTH
:
1224 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1226 *params
= bufObj
->Length
;
1229 ; /* fall-through */
1233 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1234 _mesa_lookup_enum_by_nr(pname
));
1239 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1241 GET_CURRENT_CONTEXT(ctx
);
1242 struct gl_buffer_object
* bufObj
;
1243 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1245 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1246 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1250 bufObj
= get_buffer(ctx
, "glGetBufferPointervARB", target
);
1254 *params
= bufObj
->Pointer
;
1259 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1260 GLintptr readOffset
, GLintptr writeOffset
,
1263 GET_CURRENT_CONTEXT(ctx
);
1264 struct gl_buffer_object
*src
, *dst
;
1265 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1267 src
= get_buffer(ctx
, "glCopyBuffserSubData", readTarget
);
1271 dst
= get_buffer(ctx
, "glCopyBuffserSubData", writeTarget
);
1275 if (_mesa_bufferobj_mapped(src
)) {
1276 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1277 "glCopyBuffserSubData(readBuffer is mapped)");
1281 if (_mesa_bufferobj_mapped(dst
)) {
1282 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1283 "glCopyBuffserSubData(writeBuffer is mapped)");
1287 if (readOffset
< 0) {
1288 _mesa_error(ctx
, GL_INVALID_VALUE
,
1289 "glCopyBuffserSubData(readOffset = %d)", (int) readOffset
);
1293 if (writeOffset
< 0) {
1294 _mesa_error(ctx
, GL_INVALID_VALUE
,
1295 "glCopyBuffserSubData(writeOffset = %d)", (int) writeOffset
);
1300 _mesa_error(ctx
, GL_INVALID_VALUE
,
1301 "glCopyBufferSubData(writeOffset = %d)", (int) size
);
1305 if (readOffset
+ size
> src
->Size
) {
1306 _mesa_error(ctx
, GL_INVALID_VALUE
,
1307 "glCopyBuffserSubData(readOffset + size = %d)",
1308 (int) (readOffset
+ size
));
1312 if (writeOffset
+ size
> dst
->Size
) {
1313 _mesa_error(ctx
, GL_INVALID_VALUE
,
1314 "glCopyBuffserSubData(writeOffset + size = %d)",
1315 (int) (writeOffset
+ size
));
1320 if (readOffset
+ size
<= writeOffset
) {
1323 else if (writeOffset
+ size
<= readOffset
) {
1327 /* overlapping src/dst is illegal */
1328 _mesa_error(ctx
, GL_INVALID_VALUE
,
1329 "glCopyBuffserSubData(overlapping src/dst)");
1334 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1339 * See GL_ARB_map_buffer_range spec
1342 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1345 GET_CURRENT_CONTEXT(ctx
);
1346 struct gl_buffer_object
*bufObj
;
1349 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1351 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1352 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1353 "glMapBufferRange(extension not supported)");
1358 _mesa_error(ctx
, GL_INVALID_VALUE
,
1359 "glMapBufferRange(offset = %ld)", (long)offset
);
1364 _mesa_error(ctx
, GL_INVALID_VALUE
,
1365 "glMapBufferRange(length = %ld)", (long)length
);
1369 if (access
& ~(GL_MAP_READ_BIT
|
1371 GL_MAP_INVALIDATE_RANGE_BIT
|
1372 GL_MAP_INVALIDATE_BUFFER_BIT
|
1373 GL_MAP_FLUSH_EXPLICIT_BIT
|
1374 GL_MAP_UNSYNCHRONIZED_BIT
)) {
1375 /* generate an error if any undefind bit is set */
1376 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1380 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1381 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1382 "glMapBufferRange(access indicates neither read or write)");
1386 if ((access
& GL_MAP_READ_BIT
) &&
1387 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1388 GL_MAP_INVALIDATE_BUFFER_BIT
|
1389 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1390 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1391 "glMapBufferRange(invalid access flags)");
1395 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1396 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1397 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1398 "glMapBufferRange(invalid access flags)");
1402 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
);
1406 if (offset
+ length
> bufObj
->Size
) {
1407 _mesa_error(ctx
, GL_INVALID_VALUE
,
1408 "glMapBufferRange(offset + length > size)");
1412 if (_mesa_bufferobj_mapped(bufObj
)) {
1413 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1414 "glMapBufferRange(buffer already mapped)");
1418 if (!bufObj
->Size
) {
1419 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1420 "glMapBufferRange(buffer size = 0)");
1424 /* Mapping zero bytes should return a non-null pointer. */
1426 static long dummy
= 0;
1427 bufObj
->Pointer
= &dummy
;
1428 bufObj
->Length
= length
;
1429 bufObj
->Offset
= offset
;
1430 bufObj
->AccessFlags
= access
;
1431 return bufObj
->Pointer
;
1434 ASSERT(ctx
->Driver
.MapBufferRange
);
1435 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
);
1437 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1440 /* The driver callback should have set all these fields.
1441 * This is important because other modules (like VBO) might call
1442 * the driver function directly.
1444 ASSERT(bufObj
->Pointer
== map
);
1445 ASSERT(bufObj
->Length
== length
);
1446 ASSERT(bufObj
->Offset
== offset
);
1447 ASSERT(bufObj
->AccessFlags
== access
);
1455 * See GL_ARB_map_buffer_range spec
1458 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1460 GET_CURRENT_CONTEXT(ctx
);
1461 struct gl_buffer_object
*bufObj
;
1462 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1464 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1465 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1466 "glFlushMappedBufferRange(extension not supported)");
1471 _mesa_error(ctx
, GL_INVALID_VALUE
,
1472 "glFlushMappedBufferRange(offset = %ld)", (long)offset
);
1477 _mesa_error(ctx
, GL_INVALID_VALUE
,
1478 "glFlushMappedBufferRange(length = %ld)", (long)length
);
1482 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
);
1486 if (!_mesa_bufferobj_mapped(bufObj
)) {
1487 /* buffer is not mapped */
1488 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1489 "glFlushMappedBufferRange(buffer is not mapped)");
1493 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1494 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1495 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1499 if (offset
+ length
> bufObj
->Length
) {
1500 _mesa_error(ctx
, GL_INVALID_VALUE
,
1501 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1502 (long)offset
, (long)length
, (long)bufObj
->Length
);
1506 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1508 if (ctx
->Driver
.FlushMappedBufferRange
)
1509 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
);
1513 #if FEATURE_APPLE_object_purgeable
1515 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1517 struct gl_buffer_object
*bufObj
;
1520 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1522 _mesa_error(ctx
, GL_INVALID_VALUE
,
1523 "glObjectPurgeable(name = 0x%x)", name
);
1526 if (!_mesa_is_bufferobj(bufObj
)) {
1527 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1531 if (bufObj
->Purgeable
) {
1532 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1533 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1534 return GL_VOLATILE_APPLE
;
1537 bufObj
->Purgeable
= GL_TRUE
;
1539 retval
= GL_VOLATILE_APPLE
;
1540 if (ctx
->Driver
.BufferObjectPurgeable
)
1541 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1548 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1550 struct gl_renderbuffer
*bufObj
;
1553 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1555 _mesa_error(ctx
, GL_INVALID_VALUE
,
1556 "glObjectUnpurgeable(name = 0x%x)", name
);
1560 if (bufObj
->Purgeable
) {
1561 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1562 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1563 return GL_VOLATILE_APPLE
;
1566 bufObj
->Purgeable
= GL_TRUE
;
1568 retval
= GL_VOLATILE_APPLE
;
1569 if (ctx
->Driver
.RenderObjectPurgeable
)
1570 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1577 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1579 struct gl_texture_object
*bufObj
;
1582 bufObj
= _mesa_lookup_texture(ctx
, name
);
1584 _mesa_error(ctx
, GL_INVALID_VALUE
,
1585 "glObjectPurgeable(name = 0x%x)", name
);
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
.TextureObjectPurgeable
)
1599 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1606 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1610 GET_CURRENT_CONTEXT(ctx
);
1611 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1614 _mesa_error(ctx
, GL_INVALID_VALUE
,
1615 "glObjectPurgeable(name = 0x%x)", name
);
1620 case GL_VOLATILE_APPLE
:
1621 case GL_RELEASED_APPLE
:
1625 _mesa_error(ctx
, GL_INVALID_ENUM
,
1626 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1631 switch (objectType
) {
1633 retval
= texture_object_purgeable(ctx
, name
, option
);
1635 case GL_RENDERBUFFER_EXT
:
1636 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1638 case GL_BUFFER_OBJECT_APPLE
:
1639 retval
= buffer_object_purgeable(ctx
, name
, option
);
1642 _mesa_error(ctx
, GL_INVALID_ENUM
,
1643 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1648 /* In strict conformance to the spec, we must only return VOLATILE when
1649 * when passed the VOLATILE option. Madness.
1651 * XXX First fix the spec, then fix me.
1653 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1658 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1660 struct gl_buffer_object
*bufObj
;
1663 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1665 _mesa_error(ctx
, GL_INVALID_VALUE
,
1666 "glObjectUnpurgeable(name = 0x%x)", name
);
1670 if (! bufObj
->Purgeable
) {
1671 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1672 "glObjectUnpurgeable(name = 0x%x) object is "
1673 " already \"unpurged\"", name
);
1677 bufObj
->Purgeable
= GL_FALSE
;
1680 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1681 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1688 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1690 struct gl_renderbuffer
*bufObj
;
1693 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1695 _mesa_error(ctx
, GL_INVALID_VALUE
,
1696 "glObjectUnpurgeable(name = 0x%x)", name
);
1700 if (! bufObj
->Purgeable
) {
1701 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1702 "glObjectUnpurgeable(name = 0x%x) object is "
1703 " already \"unpurged\"", name
);
1707 bufObj
->Purgeable
= GL_FALSE
;
1710 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1711 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1718 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1720 struct gl_texture_object
*bufObj
;
1723 bufObj
= _mesa_lookup_texture(ctx
, name
);
1725 _mesa_error(ctx
, GL_INVALID_VALUE
,
1726 "glObjectUnpurgeable(name = 0x%x)", name
);
1730 if (! bufObj
->Purgeable
) {
1731 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1732 "glObjectUnpurgeable(name = 0x%x) object is"
1733 " already \"unpurged\"", name
);
1737 bufObj
->Purgeable
= GL_FALSE
;
1740 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1741 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1748 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1750 GET_CURRENT_CONTEXT(ctx
);
1751 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1754 _mesa_error(ctx
, GL_INVALID_VALUE
,
1755 "glObjectUnpurgeable(name = 0x%x)", name
);
1760 case GL_RETAINED_APPLE
:
1761 case GL_UNDEFINED_APPLE
:
1765 _mesa_error(ctx
, GL_INVALID_ENUM
,
1766 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1771 switch (objectType
) {
1772 case GL_BUFFER_OBJECT_APPLE
:
1773 return buffer_object_unpurgeable(ctx
, name
, option
);
1775 return texture_object_unpurgeable(ctx
, name
, option
);
1776 case GL_RENDERBUFFER_EXT
:
1777 return renderbuffer_unpurgeable(ctx
, name
, option
);
1779 _mesa_error(ctx
, GL_INVALID_ENUM
,
1780 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1788 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1789 GLenum pname
, GLint
*params
)
1791 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1793 _mesa_error(ctx
, GL_INVALID_VALUE
,
1794 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1799 case GL_PURGEABLE_APPLE
:
1800 *params
= bufObj
->Purgeable
;
1803 _mesa_error(ctx
, GL_INVALID_ENUM
,
1804 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1812 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1813 GLenum pname
, GLint
*params
)
1815 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1817 _mesa_error(ctx
, GL_INVALID_VALUE
,
1818 "glObjectUnpurgeable(name = 0x%x)", name
);
1823 case GL_PURGEABLE_APPLE
:
1824 *params
= rb
->Purgeable
;
1827 _mesa_error(ctx
, GL_INVALID_ENUM
,
1828 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1836 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1837 GLenum pname
, GLint
*params
)
1839 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1841 _mesa_error(ctx
, GL_INVALID_VALUE
,
1842 "glObjectUnpurgeable(name = 0x%x)", name
);
1847 case GL_PURGEABLE_APPLE
:
1848 *params
= texObj
->Purgeable
;
1851 _mesa_error(ctx
, GL_INVALID_ENUM
,
1852 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1860 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
1863 GET_CURRENT_CONTEXT(ctx
);
1866 _mesa_error(ctx
, GL_INVALID_VALUE
,
1867 "glGetObjectParameteriv(name = 0x%x)", name
);
1871 switch (objectType
) {
1873 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
1875 case GL_BUFFER_OBJECT_APPLE
:
1876 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
1878 case GL_RENDERBUFFER_EXT
:
1879 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
1882 _mesa_error(ctx
, GL_INVALID_ENUM
,
1883 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1888 #endif /* FEATURE_APPLE_object_purgeable */