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
);
351 * Allocate space for and store data in a buffer object. Any data that was
352 * previously stored in the buffer object is lost. If \c data is \c NULL,
353 * memory will be allocated, but no copy will occur.
355 * This is the default callback for \c dd_function_table::BufferData()
356 * Note that all GL error checking will have been done already.
358 * \param ctx GL context.
359 * \param target Buffer object target on which to operate.
360 * \param size Size, in bytes, of the new data store.
361 * \param data Pointer to the data to store in the buffer object. This
362 * pointer may be \c NULL.
363 * \param usage Hints about how the data will be used.
364 * \param bufObj Object to be used.
366 * \return GL_TRUE for success, GL_FALSE for failure
367 * \sa glBufferDataARB, dd_function_table::BufferData.
370 _mesa_buffer_data( struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
371 const GLvoid
* data
, GLenum usage
,
372 struct gl_buffer_object
* bufObj
)
376 (void) ctx
; (void) target
;
378 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
380 bufObj
->Data
= (GLubyte
*) new_data
;
382 bufObj
->Usage
= usage
;
385 memcpy( bufObj
->Data
, data
, size
);
397 * Replace data in a subrange of buffer object. If the data range
398 * specified by \c size + \c offset extends beyond the end of the buffer or
399 * if \c data is \c NULL, no copy is performed.
401 * This is the default callback for \c dd_function_table::BufferSubData()
402 * Note that all GL error checking will have been done already.
404 * \param ctx GL context.
405 * \param target Buffer object target on which to operate.
406 * \param offset Offset of the first byte to be modified.
407 * \param size Size, in bytes, of the data range.
408 * \param data Pointer to the data to store in the buffer object.
409 * \param bufObj Object to be used.
411 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
414 _mesa_buffer_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
415 GLsizeiptrARB size
, const GLvoid
* data
,
416 struct gl_buffer_object
* bufObj
)
420 /* this should have been caught in _mesa_BufferSubData() */
421 ASSERT(size
+ offset
<= bufObj
->Size
);
424 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
430 * Retrieve data from a subrange of buffer object. If the data range
431 * specified by \c size + \c offset extends beyond the end of the buffer or
432 * if \c data is \c NULL, no copy is performed.
434 * This is the default callback for \c dd_function_table::GetBufferSubData()
435 * Note that all GL error checking will have been done already.
437 * \param ctx GL context.
438 * \param target Buffer object target on which to operate.
439 * \param offset Offset of the first byte to be fetched.
440 * \param size Size, in bytes, of the data range.
441 * \param data Destination for data
442 * \param bufObj Object to be used.
444 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
447 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
448 GLsizeiptrARB size
, GLvoid
* data
,
449 struct gl_buffer_object
* bufObj
)
453 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
454 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
460 * Default fallback for \c dd_function_table::MapBufferRange().
461 * Called via glMapBufferRange().
464 _mesa_buffer_map_range( struct gl_context
*ctx
, GLintptr offset
,
465 GLsizeiptr length
, GLbitfield access
,
466 struct gl_buffer_object
*bufObj
)
469 assert(!_mesa_bufferobj_mapped(bufObj
));
470 /* Just return a direct pointer to the data */
471 bufObj
->Pointer
= bufObj
->Data
+ offset
;
472 bufObj
->Length
= length
;
473 bufObj
->Offset
= offset
;
474 bufObj
->AccessFlags
= access
;
475 return bufObj
->Pointer
;
480 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
481 * Called via glFlushMappedBufferRange().
484 _mesa_buffer_flush_mapped_range( struct gl_context
*ctx
,
485 GLintptr offset
, GLsizeiptr length
,
486 struct gl_buffer_object
*obj
)
497 * Default callback for \c dd_function_table::MapBuffer().
499 * The input parameters will have been already tested for errors.
501 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
504 _mesa_buffer_unmap( struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
507 /* XXX we might assert here that bufObj->Pointer is non-null */
508 bufObj
->Pointer
= NULL
;
511 bufObj
->AccessFlags
= 0x0;
517 * Default fallback for \c dd_function_table::CopyBufferSubData().
518 * Called via glCopyBuffserSubData().
521 _mesa_copy_buffer_subdata(struct gl_context
*ctx
,
522 struct gl_buffer_object
*src
,
523 struct gl_buffer_object
*dst
,
524 GLintptr readOffset
, GLintptr writeOffset
,
527 GLubyte
*srcPtr
, *dstPtr
;
529 /* the buffers should not be mapped */
530 assert(!_mesa_bufferobj_mapped(src
));
531 assert(!_mesa_bufferobj_mapped(dst
));
534 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
536 GL_MAP_WRITE_BIT
, src
);
541 srcPtr
+= readOffset
;
542 dstPtr
+= writeOffset
;
544 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
545 GL_MAP_READ_BIT
, src
);
546 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
548 GL_MAP_INVALIDATE_RANGE_BIT
), dst
);
551 /* Note: the src and dst regions will never overlap. Trying to do so
552 * would generate GL_INVALID_VALUE earlier.
554 if (srcPtr
&& dstPtr
)
555 memcpy(dstPtr
, srcPtr
, size
);
557 ctx
->Driver
.UnmapBuffer(ctx
, src
);
559 ctx
->Driver
.UnmapBuffer(ctx
, dst
);
565 * Initialize the state associated with buffer objects
568 _mesa_init_buffer_objects( struct gl_context
*ctx
)
570 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
571 _glthread_INIT_MUTEX(DummyBufferObject
.Mutex
);
572 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
574 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
575 ctx
->Shared
->NullBufferObj
);
577 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
578 ctx
->Shared
->NullBufferObj
);
579 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
580 ctx
->Shared
->NullBufferObj
);
585 _mesa_free_buffer_objects( struct gl_context
*ctx
)
587 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
589 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
590 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
595 * Bind the specified target to buffer for the specified context.
596 * Called by glBindBuffer() and other functions.
599 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
601 struct gl_buffer_object
*oldBufObj
;
602 struct gl_buffer_object
*newBufObj
= NULL
;
603 struct gl_buffer_object
**bindTarget
= NULL
;
605 bindTarget
= get_buffer_target(ctx
, target
);
607 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
611 /* Get pointer to old buffer object (to be unbound) */
612 oldBufObj
= *bindTarget
;
613 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
614 return; /* rebinding the same buffer object- no change */
617 * Get pointer to new buffer object (newBufObj)
620 /* The spec says there's not a buffer object named 0, but we use
621 * one internally because it simplifies things.
623 newBufObj
= ctx
->Shared
->NullBufferObj
;
626 /* non-default buffer object */
627 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
628 if (!newBufObj
|| newBufObj
== &DummyBufferObject
) {
629 /* If this is a new buffer object id, or one which was generated but
630 * never used before, allocate a buffer object now.
632 ASSERT(ctx
->Driver
.NewBufferObject
);
633 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
635 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
638 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
642 /* bind new buffer */
643 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
645 /* Pass BindBuffer call to device driver */
646 if (ctx
->Driver
.BindBuffer
)
647 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
652 * Update the default buffer objects in the given context to reference those
653 * specified in the shared state and release those referencing the old
657 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
659 /* Bind the NullBufferObj to remove references to those
660 * in the shared context hash table.
662 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
663 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
664 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
665 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
671 * Return the gl_buffer_object for the given ID.
672 * Always return NULL for ID 0.
674 struct gl_buffer_object
*
675 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
680 return (struct gl_buffer_object
*)
681 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
686 * If *ptr points to obj, set ptr = the Null/default buffer object.
687 * This is a helper for buffer object deletion.
688 * The GL spec says that deleting a buffer object causes it to get
689 * unbound from all arrays in the current context.
692 unbind(struct gl_context
*ctx
,
693 struct gl_buffer_object
**ptr
,
694 struct gl_buffer_object
*obj
)
697 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
703 * Plug default/fallback buffer object functions into the device
707 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
709 /* GL_ARB_vertex/pixel_buffer_object */
710 driver
->NewBufferObject
= _mesa_new_buffer_object
;
711 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
712 driver
->BindBuffer
= NULL
;
713 driver
->BufferData
= _mesa_buffer_data
;
714 driver
->BufferSubData
= _mesa_buffer_subdata
;
715 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
716 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
718 /* GL_ARB_map_buffer_range */
719 driver
->MapBufferRange
= _mesa_buffer_map_range
;
720 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
722 /* GL_ARB_copy_buffer */
723 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
728 /**********************************************************************/
730 /**********************************************************************/
733 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
735 GET_CURRENT_CONTEXT(ctx
);
736 ASSERT_OUTSIDE_BEGIN_END(ctx
);
738 if (MESA_VERBOSE
& VERBOSE_API
)
739 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
740 _mesa_lookup_enum_by_nr(target
), buffer
);
742 bind_buffer_object(ctx
, target
, buffer
);
747 * Delete a set of buffer objects.
749 * \param n Number of buffer objects to delete.
750 * \param ids Array of \c n buffer object IDs.
753 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
755 GET_CURRENT_CONTEXT(ctx
);
757 ASSERT_OUTSIDE_BEGIN_END(ctx
);
758 FLUSH_VERTICES(ctx
, 0);
761 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
765 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
767 for (i
= 0; i
< n
; i
++) {
768 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
770 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
773 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
775 if (_mesa_bufferobj_mapped(bufObj
)) {
776 /* if mapped, unmap it now */
777 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
778 bufObj
->AccessFlags
= default_access_mode(ctx
);
779 bufObj
->Pointer
= NULL
;
782 /* unbind any vertex pointers bound to this buffer */
783 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
784 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
787 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
788 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
790 if (arrayObj
->ElementArrayBufferObj
== bufObj
) {
791 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
794 /* unbind any pixel pack/unpack pointers bound to this buffer */
795 if (ctx
->Pack
.BufferObj
== bufObj
) {
796 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
798 if (ctx
->Unpack
.BufferObj
== bufObj
) {
799 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
802 /* The ID is immediately freed for re-use */
803 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
804 /* Make sure we do not run into the classic ABA problem on bind.
805 * We don't want to allow re-binding a buffer object that's been
806 * "deleted" by glDeleteBuffers().
808 * The explicit rebinding to the default object in the current context
809 * prevents the above in the current context, but another context
810 * sharing the same objects might suffer from this problem.
811 * The alternative would be to do the hash lookup in any case on bind
812 * which would introduce more runtime overhead than this.
814 bufObj
->DeletePending
= GL_TRUE
;
815 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
819 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
824 * Generate a set of unique buffer object IDs and store them in \c buffer.
826 * \param n Number of IDs to generate.
827 * \param buffer Array of \c n locations to store the IDs.
830 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
832 GET_CURRENT_CONTEXT(ctx
);
835 ASSERT_OUTSIDE_BEGIN_END(ctx
);
837 if (MESA_VERBOSE
& VERBOSE_API
)
838 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
841 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
850 * This must be atomic (generation and allocation of buffer object IDs)
852 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
854 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
856 /* Insert the ID and pointer to dummy buffer object into hash table */
857 for (i
= 0; i
< n
; i
++) {
858 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
860 buffer
[i
] = first
+ i
;
863 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
868 * Determine if ID is the name of a buffer object.
870 * \param id ID of the potential buffer object.
871 * \return \c GL_TRUE if \c id is the name of a buffer object,
872 * \c GL_FALSE otherwise.
875 _mesa_IsBufferARB(GLuint id
)
877 struct gl_buffer_object
*bufObj
;
878 GET_CURRENT_CONTEXT(ctx
);
879 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
881 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
882 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
883 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
885 return bufObj
&& bufObj
!= &DummyBufferObject
;
890 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
891 const GLvoid
* data
, GLenum usage
)
893 GET_CURRENT_CONTEXT(ctx
);
894 struct gl_buffer_object
*bufObj
;
895 ASSERT_OUTSIDE_BEGIN_END(ctx
);
897 if (MESA_VERBOSE
& VERBOSE_API
)
898 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
899 _mesa_lookup_enum_by_nr(target
),
900 (long int) size
, data
,
901 _mesa_lookup_enum_by_nr(usage
));
904 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
909 case GL_STREAM_DRAW_ARB
:
910 case GL_STREAM_READ_ARB
:
911 case GL_STREAM_COPY_ARB
:
912 case GL_STATIC_DRAW_ARB
:
913 case GL_STATIC_READ_ARB
:
914 case GL_STATIC_COPY_ARB
:
915 case GL_DYNAMIC_DRAW_ARB
:
916 case GL_DYNAMIC_READ_ARB
:
917 case GL_DYNAMIC_COPY_ARB
:
921 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
925 bufObj
= get_buffer(ctx
, "glBufferDataARB", target
);
929 if (_mesa_bufferobj_mapped(bufObj
)) {
930 /* Unmap the existing buffer. We'll replace it now. Not an error. */
931 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
932 bufObj
->AccessFlags
= default_access_mode(ctx
);
933 ASSERT(bufObj
->Pointer
== NULL
);
936 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
938 bufObj
->Written
= GL_TRUE
;
941 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
942 bufObj
->Name
, size
, data
, usage
);
949 ASSERT(ctx
->Driver
.BufferData
);
950 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
951 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
957 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
958 GLsizeiptrARB size
, const GLvoid
* data
)
960 GET_CURRENT_CONTEXT(ctx
);
961 struct gl_buffer_object
*bufObj
;
962 ASSERT_OUTSIDE_BEGIN_END(ctx
);
964 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
965 "glBufferSubDataARB" );
967 /* error already recorded */
974 bufObj
->Written
= GL_TRUE
;
976 ASSERT(ctx
->Driver
.BufferSubData
);
977 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
982 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
983 GLsizeiptrARB size
, void * data
)
985 GET_CURRENT_CONTEXT(ctx
);
986 struct gl_buffer_object
*bufObj
;
987 ASSERT_OUTSIDE_BEGIN_END(ctx
);
989 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
990 "glGetBufferSubDataARB" );
992 /* error already recorded */
996 ASSERT(ctx
->Driver
.GetBufferSubData
);
997 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
1002 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1004 GET_CURRENT_CONTEXT(ctx
);
1005 struct gl_buffer_object
* bufObj
;
1006 GLbitfield accessFlags
;
1009 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1012 case GL_READ_ONLY_ARB
:
1013 accessFlags
= GL_MAP_READ_BIT
;
1015 case GL_WRITE_ONLY_ARB
:
1016 accessFlags
= GL_MAP_WRITE_BIT
;
1018 case GL_READ_WRITE_ARB
:
1019 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1022 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1026 bufObj
= get_buffer(ctx
, "glMapBufferARB", target
);
1030 if (_mesa_bufferobj_mapped(bufObj
)) {
1031 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1035 if (!bufObj
->Size
) {
1036 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1037 "glMapBuffer(buffer size = 0)");
1041 ASSERT(ctx
->Driver
.MapBufferRange
);
1042 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
);
1044 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1048 /* The driver callback should have set these fields.
1049 * This is important because other modules (like VBO) might call
1050 * the driver function directly.
1052 ASSERT(bufObj
->Pointer
== map
);
1053 ASSERT(bufObj
->Length
== bufObj
->Size
);
1054 ASSERT(bufObj
->Offset
== 0);
1055 bufObj
->AccessFlags
= accessFlags
;
1058 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1059 bufObj
->Written
= GL_TRUE
;
1062 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1063 bufObj
->Name
, bufObj
->Size
, access
);
1064 if (access
== GL_WRITE_ONLY_ARB
) {
1066 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1067 for (i
= 0; i
< bufObj
->Size
; i
++)
1074 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1076 /* buffer is 100 bytes larger than requested, fill with magic value */
1077 for (i
= 0; i
< 100; i
++) {
1078 buf
[bufObj
->Size
- i
- 1] = 123;
1083 return bufObj
->Pointer
;
1087 GLboolean GLAPIENTRY
1088 _mesa_UnmapBufferARB(GLenum target
)
1090 GET_CURRENT_CONTEXT(ctx
);
1091 struct gl_buffer_object
*bufObj
;
1092 GLboolean status
= GL_TRUE
;
1093 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1095 bufObj
= get_buffer(ctx
, "glUnmapBufferARB", target
);
1099 if (!_mesa_bufferobj_mapped(bufObj
)) {
1100 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1105 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1106 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1108 /* check that last 100 bytes are still = magic value */
1109 for (i
= 0; i
< 100; i
++) {
1110 GLuint pos
= bufObj
->Size
- i
- 1;
1111 if (buf
[pos
] != 123) {
1112 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1113 " at position %d (value = %u)\n",
1121 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1122 GLuint i
, unchanged
= 0;
1123 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1125 /* check which bytes changed */
1126 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1127 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1134 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1135 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1140 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1141 bufObj
->AccessFlags
= default_access_mode(ctx
);
1142 ASSERT(bufObj
->Pointer
== NULL
);
1143 ASSERT(bufObj
->Offset
== 0);
1144 ASSERT(bufObj
->Length
== 0);
1151 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1153 GET_CURRENT_CONTEXT(ctx
);
1154 struct gl_buffer_object
*bufObj
;
1155 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1157 bufObj
= get_buffer(ctx
, "glGetBufferParameterivARB", target
);
1162 case GL_BUFFER_SIZE_ARB
:
1163 *params
= (GLint
) bufObj
->Size
;
1165 case GL_BUFFER_USAGE_ARB
:
1166 *params
= bufObj
->Usage
;
1168 case GL_BUFFER_ACCESS_ARB
:
1169 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1171 case GL_BUFFER_MAPPED_ARB
:
1172 *params
= _mesa_bufferobj_mapped(bufObj
);
1174 case GL_BUFFER_ACCESS_FLAGS
:
1175 if (ctx
->VersionMajor
< 3)
1177 *params
= bufObj
->AccessFlags
;
1179 case GL_BUFFER_MAP_OFFSET
:
1180 if (ctx
->VersionMajor
< 3)
1182 *params
= (GLint
) bufObj
->Offset
;
1184 case GL_BUFFER_MAP_LENGTH
:
1185 if (ctx
->VersionMajor
< 3)
1187 *params
= (GLint
) bufObj
->Length
;
1190 ; /* fall-through */
1194 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1195 _mesa_lookup_enum_by_nr(pname
));
1201 * This is pretty much a duplicate of GetBufferParameteriv() but the
1202 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1205 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1207 GET_CURRENT_CONTEXT(ctx
);
1208 struct gl_buffer_object
*bufObj
;
1209 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1211 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
);
1216 case GL_BUFFER_SIZE_ARB
:
1217 *params
= bufObj
->Size
;
1219 case GL_BUFFER_USAGE_ARB
:
1220 *params
= bufObj
->Usage
;
1222 case GL_BUFFER_ACCESS_ARB
:
1223 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1225 case GL_BUFFER_ACCESS_FLAGS
:
1226 if (ctx
->VersionMajor
< 3)
1228 *params
= bufObj
->AccessFlags
;
1230 case GL_BUFFER_MAPPED_ARB
:
1231 *params
= _mesa_bufferobj_mapped(bufObj
);
1233 case GL_BUFFER_MAP_OFFSET
:
1234 if (ctx
->VersionMajor
< 3)
1236 *params
= bufObj
->Offset
;
1238 case GL_BUFFER_MAP_LENGTH
:
1239 if (ctx
->VersionMajor
< 3)
1241 *params
= bufObj
->Length
;
1244 ; /* fall-through */
1248 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1249 _mesa_lookup_enum_by_nr(pname
));
1254 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1256 GET_CURRENT_CONTEXT(ctx
);
1257 struct gl_buffer_object
* bufObj
;
1258 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1260 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1261 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1265 bufObj
= get_buffer(ctx
, "glGetBufferPointervARB", target
);
1269 *params
= bufObj
->Pointer
;
1274 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1275 GLintptr readOffset
, GLintptr writeOffset
,
1278 GET_CURRENT_CONTEXT(ctx
);
1279 struct gl_buffer_object
*src
, *dst
;
1280 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1282 src
= get_buffer(ctx
, "glCopyBuffserSubData", readTarget
);
1286 dst
= get_buffer(ctx
, "glCopyBuffserSubData", writeTarget
);
1290 if (_mesa_bufferobj_mapped(src
)) {
1291 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1292 "glCopyBuffserSubData(readBuffer is mapped)");
1296 if (_mesa_bufferobj_mapped(dst
)) {
1297 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1298 "glCopyBuffserSubData(writeBuffer is mapped)");
1302 if (readOffset
< 0) {
1303 _mesa_error(ctx
, GL_INVALID_VALUE
,
1304 "glCopyBuffserSubData(readOffset = %d)", (int) readOffset
);
1308 if (writeOffset
< 0) {
1309 _mesa_error(ctx
, GL_INVALID_VALUE
,
1310 "glCopyBuffserSubData(writeOffset = %d)", (int) writeOffset
);
1314 if (readOffset
+ size
> src
->Size
) {
1315 _mesa_error(ctx
, GL_INVALID_VALUE
,
1316 "glCopyBuffserSubData(readOffset + size = %d)",
1317 (int) (readOffset
+ size
));
1321 if (writeOffset
+ size
> dst
->Size
) {
1322 _mesa_error(ctx
, GL_INVALID_VALUE
,
1323 "glCopyBuffserSubData(writeOffset + size = %d)",
1324 (int) (writeOffset
+ size
));
1329 if (readOffset
+ size
<= writeOffset
) {
1332 else if (writeOffset
+ size
<= readOffset
) {
1336 /* overlapping src/dst is illegal */
1337 _mesa_error(ctx
, GL_INVALID_VALUE
,
1338 "glCopyBuffserSubData(overlapping src/dst)");
1343 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1348 * See GL_ARB_map_buffer_range spec
1351 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1354 GET_CURRENT_CONTEXT(ctx
);
1355 struct gl_buffer_object
*bufObj
;
1358 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1360 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1361 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1362 "glMapBufferRange(extension not supported)");
1367 _mesa_error(ctx
, GL_INVALID_VALUE
,
1368 "glMapBufferRange(offset = %ld)", (long)offset
);
1373 _mesa_error(ctx
, GL_INVALID_VALUE
,
1374 "glMapBufferRange(length = %ld)", (long)length
);
1378 if (access
& ~(GL_MAP_READ_BIT
|
1380 GL_MAP_INVALIDATE_RANGE_BIT
|
1381 GL_MAP_INVALIDATE_BUFFER_BIT
|
1382 GL_MAP_FLUSH_EXPLICIT_BIT
|
1383 GL_MAP_UNSYNCHRONIZED_BIT
)) {
1384 /* generate an error if any undefind bit is set */
1385 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1389 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1390 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1391 "glMapBufferRange(access indicates neither read or write)");
1395 if ((access
& GL_MAP_READ_BIT
) &&
1396 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1397 GL_MAP_INVALIDATE_BUFFER_BIT
|
1398 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1399 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1400 "glMapBufferRange(invalid access flags)");
1404 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1405 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1406 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1407 "glMapBufferRange(invalid access flags)");
1411 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
);
1415 if (offset
+ length
> bufObj
->Size
) {
1416 _mesa_error(ctx
, GL_INVALID_VALUE
,
1417 "glMapBufferRange(offset + length > size)");
1421 if (_mesa_bufferobj_mapped(bufObj
)) {
1422 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1423 "glMapBufferRange(buffer already mapped)");
1427 if (!bufObj
->Size
) {
1428 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1429 "glMapBufferRange(buffer size = 0)");
1433 /* Mapping zero bytes should return a non-null pointer. */
1435 static long dummy
= 0;
1436 bufObj
->Pointer
= &dummy
;
1437 bufObj
->Length
= length
;
1438 bufObj
->Offset
= offset
;
1439 bufObj
->AccessFlags
= access
;
1440 return bufObj
->Pointer
;
1443 ASSERT(ctx
->Driver
.MapBufferRange
);
1444 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
);
1446 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1449 /* The driver callback should have set all these fields.
1450 * This is important because other modules (like VBO) might call
1451 * the driver function directly.
1453 ASSERT(bufObj
->Pointer
== map
);
1454 ASSERT(bufObj
->Length
== length
);
1455 ASSERT(bufObj
->Offset
== offset
);
1456 ASSERT(bufObj
->AccessFlags
== access
);
1464 * See GL_ARB_map_buffer_range spec
1467 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1469 GET_CURRENT_CONTEXT(ctx
);
1470 struct gl_buffer_object
*bufObj
;
1471 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1473 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1474 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1475 "glFlushMappedBufferRange(extension not supported)");
1480 _mesa_error(ctx
, GL_INVALID_VALUE
,
1481 "glFlushMappedBufferRange(offset = %ld)", (long)offset
);
1486 _mesa_error(ctx
, GL_INVALID_VALUE
,
1487 "glFlushMappedBufferRange(length = %ld)", (long)length
);
1491 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
);
1495 if (!_mesa_bufferobj_mapped(bufObj
)) {
1496 /* buffer is not mapped */
1497 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1498 "glFlushMappedBufferRange(buffer is not mapped)");
1502 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1503 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1504 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1508 if (offset
+ length
> bufObj
->Length
) {
1509 _mesa_error(ctx
, GL_INVALID_VALUE
,
1510 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1511 (long)offset
, (long)length
, (long)bufObj
->Length
);
1515 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1517 if (ctx
->Driver
.FlushMappedBufferRange
)
1518 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
);
1522 #if FEATURE_APPLE_object_purgeable
1524 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1526 struct gl_buffer_object
*bufObj
;
1529 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1531 _mesa_error(ctx
, GL_INVALID_VALUE
,
1532 "glObjectPurgeable(name = 0x%x)", name
);
1535 if (!_mesa_is_bufferobj(bufObj
)) {
1536 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1540 if (bufObj
->Purgeable
) {
1541 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1542 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1543 return GL_VOLATILE_APPLE
;
1546 bufObj
->Purgeable
= GL_TRUE
;
1548 retval
= GL_VOLATILE_APPLE
;
1549 if (ctx
->Driver
.BufferObjectPurgeable
)
1550 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1557 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1559 struct gl_renderbuffer
*bufObj
;
1562 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1564 _mesa_error(ctx
, GL_INVALID_VALUE
,
1565 "glObjectUnpurgeable(name = 0x%x)", name
);
1569 if (bufObj
->Purgeable
) {
1570 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1571 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1572 return GL_VOLATILE_APPLE
;
1575 bufObj
->Purgeable
= GL_TRUE
;
1577 retval
= GL_VOLATILE_APPLE
;
1578 if (ctx
->Driver
.RenderObjectPurgeable
)
1579 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1586 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1588 struct gl_texture_object
*bufObj
;
1591 bufObj
= _mesa_lookup_texture(ctx
, name
);
1593 _mesa_error(ctx
, GL_INVALID_VALUE
,
1594 "glObjectPurgeable(name = 0x%x)", name
);
1598 if (bufObj
->Purgeable
) {
1599 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1600 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1601 return GL_VOLATILE_APPLE
;
1604 bufObj
->Purgeable
= GL_TRUE
;
1606 retval
= GL_VOLATILE_APPLE
;
1607 if (ctx
->Driver
.TextureObjectPurgeable
)
1608 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1615 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1619 GET_CURRENT_CONTEXT(ctx
);
1620 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1623 _mesa_error(ctx
, GL_INVALID_VALUE
,
1624 "glObjectPurgeable(name = 0x%x)", name
);
1629 case GL_VOLATILE_APPLE
:
1630 case GL_RELEASED_APPLE
:
1634 _mesa_error(ctx
, GL_INVALID_ENUM
,
1635 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1640 switch (objectType
) {
1642 retval
= texture_object_purgeable(ctx
, name
, option
);
1644 case GL_RENDERBUFFER_EXT
:
1645 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1647 case GL_BUFFER_OBJECT_APPLE
:
1648 retval
= buffer_object_purgeable(ctx
, name
, option
);
1651 _mesa_error(ctx
, GL_INVALID_ENUM
,
1652 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1657 /* In strict conformance to the spec, we must only return VOLATILE when
1658 * when passed the VOLATILE option. Madness.
1660 * XXX First fix the spec, then fix me.
1662 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1667 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1669 struct gl_buffer_object
*bufObj
;
1672 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1674 _mesa_error(ctx
, GL_INVALID_VALUE
,
1675 "glObjectUnpurgeable(name = 0x%x)", name
);
1679 if (! bufObj
->Purgeable
) {
1680 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1681 "glObjectUnpurgeable(name = 0x%x) object is "
1682 " already \"unpurged\"", name
);
1686 bufObj
->Purgeable
= GL_FALSE
;
1689 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1690 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1697 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1699 struct gl_renderbuffer
*bufObj
;
1702 bufObj
= _mesa_lookup_renderbuffer(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
.RenderObjectUnpurgeable
)
1720 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1727 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1729 struct gl_texture_object
*bufObj
;
1732 bufObj
= _mesa_lookup_texture(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
.TextureObjectUnpurgeable
)
1750 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1757 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1759 GET_CURRENT_CONTEXT(ctx
);
1760 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1763 _mesa_error(ctx
, GL_INVALID_VALUE
,
1764 "glObjectUnpurgeable(name = 0x%x)", name
);
1769 case GL_RETAINED_APPLE
:
1770 case GL_UNDEFINED_APPLE
:
1774 _mesa_error(ctx
, GL_INVALID_ENUM
,
1775 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1780 switch (objectType
) {
1781 case GL_BUFFER_OBJECT_APPLE
:
1782 return buffer_object_unpurgeable(ctx
, name
, option
);
1784 return texture_object_unpurgeable(ctx
, name
, option
);
1785 case GL_RENDERBUFFER_EXT
:
1786 return renderbuffer_unpurgeable(ctx
, name
, option
);
1788 _mesa_error(ctx
, GL_INVALID_ENUM
,
1789 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1797 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1798 GLenum pname
, GLint
*params
)
1800 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1802 _mesa_error(ctx
, GL_INVALID_VALUE
,
1803 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1808 case GL_PURGEABLE_APPLE
:
1809 *params
= bufObj
->Purgeable
;
1812 _mesa_error(ctx
, GL_INVALID_ENUM
,
1813 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1821 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1822 GLenum pname
, GLint
*params
)
1824 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1826 _mesa_error(ctx
, GL_INVALID_VALUE
,
1827 "glObjectUnpurgeable(name = 0x%x)", name
);
1832 case GL_PURGEABLE_APPLE
:
1833 *params
= rb
->Purgeable
;
1836 _mesa_error(ctx
, GL_INVALID_ENUM
,
1837 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1845 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1846 GLenum pname
, GLint
*params
)
1848 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1850 _mesa_error(ctx
, GL_INVALID_VALUE
,
1851 "glObjectUnpurgeable(name = 0x%x)", name
);
1856 case GL_PURGEABLE_APPLE
:
1857 *params
= texObj
->Purgeable
;
1860 _mesa_error(ctx
, GL_INVALID_ENUM
,
1861 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1869 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
1872 GET_CURRENT_CONTEXT(ctx
);
1875 _mesa_error(ctx
, GL_INVALID_VALUE
,
1876 "glGetObjectParameteriv(name = 0x%x)", name
);
1880 switch (objectType
) {
1882 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
1884 case GL_BUFFER_OBJECT_APPLE
:
1885 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
1887 case GL_RENDERBUFFER_EXT
:
1888 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
1891 _mesa_error(ctx
, GL_INVALID_ENUM
,
1892 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1897 #endif /* FEATURE_APPLE_object_purgeable */