2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions.
30 * \author Brian Paul, Ian Romanick
40 #include "bufferobj.h"
42 #include "mfeatures.h"
45 #include "transformfeedback.h"
50 /*#define BOUNDS_CHECK*/
54 * Used as a placeholder for buffer objects between glGenBuffers() and
55 * glBindBuffer() so that glIsBuffer() can work correctly.
57 static struct gl_buffer_object DummyBufferObject
;
61 * Return pointer to address of a buffer object target.
62 * \param ctx the GL context
63 * \param target the buffer object target to be retrieved.
64 * \return pointer to pointer to the buffer object bound to \c target in the
65 * specified context or \c NULL if \c target is invalid.
67 static inline struct gl_buffer_object
**
68 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
71 case GL_ARRAY_BUFFER_ARB
:
72 return &ctx
->Array
.ArrayBufferObj
;
73 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
74 return &ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
75 case GL_PIXEL_PACK_BUFFER_EXT
:
76 return &ctx
->Pack
.BufferObj
;
77 case GL_PIXEL_UNPACK_BUFFER_EXT
:
78 return &ctx
->Unpack
.BufferObj
;
79 case GL_COPY_READ_BUFFER
:
80 return &ctx
->CopyReadBuffer
;
81 case GL_COPY_WRITE_BUFFER
:
82 return &ctx
->CopyWriteBuffer
;
83 #if FEATURE_EXT_transform_feedback
84 case GL_TRANSFORM_FEEDBACK_BUFFER
:
85 if (ctx
->Extensions
.EXT_transform_feedback
) {
86 return &ctx
->TransformFeedback
.CurrentBuffer
;
90 case GL_TEXTURE_BUFFER
:
91 if (ctx
->Extensions
.ARB_texture_buffer_object
) {
92 return &ctx
->Texture
.BufferObject
;
103 * Get the buffer object bound to the specified target in a GL context.
104 * \param ctx the GL context
105 * \param target the buffer object target to be retrieved.
106 * \return pointer to the buffer object bound to \c target in the
107 * specified context or \c NULL if \c target is invalid.
109 static inline struct gl_buffer_object
*
110 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
)
112 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
115 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
119 if (!_mesa_is_bufferobj(*bufObj
)) {
120 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(buffer 0)", func
);
129 default_access_mode(const struct gl_context
*ctx
)
131 /* Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
133 * Name Type Initial Value Legal Values
135 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
138 * However, table 6.8 in the GL_OES_mapbuffer extension says:
140 * Get Value Type Get Command Value Description
141 * --------- ---- ----------- ----- -----------
142 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
144 * The difference is because GL_OES_mapbuffer only supports mapping buffers
147 return (ctx
->API
== API_OPENGLES
)
148 ? GL_MAP_WRITE_BIT
: (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
);
153 * Convert a GLbitfield describing the mapped buffer access flags
154 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
157 simplified_access_mode(GLbitfield access
)
159 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
160 if ((access
& rwFlags
) == rwFlags
)
161 return GL_READ_WRITE
;
162 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
164 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
165 return GL_WRITE_ONLY
;
166 return GL_READ_WRITE
; /* this should never happen, but no big deal */
171 * Tests the subdata range parameters and sets the GL error code for
172 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
174 * \param ctx GL context.
175 * \param target Buffer object target on which to operate.
176 * \param offset Offset of the first byte of the subdata range.
177 * \param size Size, in bytes, of the subdata range.
178 * \param caller Name of calling function for recording errors.
179 * \return A pointer to the buffer object bound to \c target in the
180 * specified context or \c NULL if any of the parameter or state
181 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
184 * \sa glBufferSubDataARB, glGetBufferSubDataARB
186 static struct gl_buffer_object
*
187 buffer_object_subdata_range_good( struct gl_context
* ctx
, GLenum target
,
188 GLintptrARB offset
, GLsizeiptrARB size
,
191 struct gl_buffer_object
*bufObj
;
194 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
199 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
203 bufObj
= get_buffer(ctx
, caller
, target
);
207 if (offset
+ size
> bufObj
->Size
) {
208 _mesa_error(ctx
, GL_INVALID_VALUE
,
209 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
210 (unsigned long) offset
,
211 (unsigned long) size
,
212 (unsigned long) bufObj
->Size
);
215 if (_mesa_bufferobj_mapped(bufObj
)) {
216 /* Buffer is currently mapped */
217 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
226 * Allocate and initialize a new buffer object.
228 * Default callback for the \c dd_function_table::NewBufferObject() hook.
230 static struct gl_buffer_object
*
231 _mesa_new_buffer_object( struct gl_context
*ctx
, GLuint name
, GLenum target
)
233 struct gl_buffer_object
*obj
;
237 obj
= MALLOC_STRUCT(gl_buffer_object
);
238 _mesa_initialize_buffer_object(ctx
, obj
, name
, target
);
244 * Delete a buffer object.
246 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
249 _mesa_delete_buffer_object(struct gl_context
*ctx
,
250 struct gl_buffer_object
*bufObj
)
257 /* assign strange values here to help w/ debugging */
258 bufObj
->RefCount
= -1000;
261 _glthread_DESTROY_MUTEX(bufObj
->Mutex
);
268 * Set ptr to bufObj w/ reference counting.
269 * This is normally only called from the _mesa_reference_buffer_object() macro
270 * when there's a real pointer change.
273 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
274 struct gl_buffer_object
**ptr
,
275 struct gl_buffer_object
*bufObj
)
278 /* Unreference the old buffer */
279 GLboolean deleteFlag
= GL_FALSE
;
280 struct gl_buffer_object
*oldObj
= *ptr
;
282 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
283 ASSERT(oldObj
->RefCount
> 0);
286 printf("BufferObj %p %d DECR to %d\n",
287 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
289 deleteFlag
= (oldObj
->RefCount
== 0);
290 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
294 /* some sanity checking: don't delete a buffer still in use */
296 /* unfortunately, these tests are invalid during context tear-down */
297 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
298 ASSERT(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
!= bufObj
);
299 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
302 ASSERT(ctx
->Driver
.DeleteBuffer
);
303 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
311 /* reference new buffer */
312 _glthread_LOCK_MUTEX(bufObj
->Mutex
);
313 if (bufObj
->RefCount
== 0) {
314 /* this buffer's being deleted (look just above) */
315 /* Not sure this can every really happen. Warn if it does. */
316 _mesa_problem(NULL
, "referencing deleted buffer object");
322 printf("BufferObj %p %d INCR to %d\n",
323 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
327 _glthread_UNLOCK_MUTEX(bufObj
->Mutex
);
333 * Initialize a buffer object to default values.
336 _mesa_initialize_buffer_object( struct gl_context
*ctx
,
337 struct gl_buffer_object
*obj
,
338 GLuint name
, GLenum target
)
342 memset(obj
, 0, sizeof(struct gl_buffer_object
));
343 _glthread_INIT_MUTEX(obj
->Mutex
);
346 obj
->Usage
= GL_STATIC_DRAW_ARB
;
347 obj
->AccessFlags
= default_access_mode(ctx
);
352 * Allocate space for and store data in a buffer object. Any data that was
353 * previously stored in the buffer object is lost. If \c data is \c NULL,
354 * memory will be allocated, but no copy will occur.
356 * This is the default callback for \c dd_function_table::BufferData()
357 * Note that all GL error checking will have been done already.
359 * \param ctx GL context.
360 * \param target Buffer object target on which to operate.
361 * \param size Size, in bytes, of the new data store.
362 * \param data Pointer to the data to store in the buffer object. This
363 * pointer may be \c NULL.
364 * \param usage Hints about how the data will be used.
365 * \param bufObj Object to be used.
367 * \return GL_TRUE for success, GL_FALSE for failure
368 * \sa glBufferDataARB, dd_function_table::BufferData.
371 _mesa_buffer_data( struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
372 const GLvoid
* data
, GLenum usage
,
373 struct gl_buffer_object
* bufObj
)
377 (void) ctx
; (void) target
;
379 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
381 bufObj
->Data
= (GLubyte
*) new_data
;
383 bufObj
->Usage
= usage
;
386 memcpy( bufObj
->Data
, data
, size
);
398 * Replace data in a subrange of buffer object. If the data range
399 * specified by \c size + \c offset extends beyond the end of the buffer or
400 * if \c data is \c NULL, no copy is performed.
402 * This is the default callback for \c dd_function_table::BufferSubData()
403 * Note that all GL error checking will have been done already.
405 * \param ctx GL context.
406 * \param target Buffer object target on which to operate.
407 * \param offset Offset of the first byte to be modified.
408 * \param size Size, in bytes, of the data range.
409 * \param data Pointer to the data to store in the buffer object.
410 * \param bufObj Object to be used.
412 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
415 _mesa_buffer_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
416 GLsizeiptrARB size
, const GLvoid
* data
,
417 struct gl_buffer_object
* bufObj
)
421 /* this should have been caught in _mesa_BufferSubData() */
422 ASSERT(size
+ offset
<= bufObj
->Size
);
425 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
431 * Retrieve data from a subrange of buffer object. If the data range
432 * specified by \c size + \c offset extends beyond the end of the buffer or
433 * if \c data is \c NULL, no copy is performed.
435 * This is the default callback for \c dd_function_table::GetBufferSubData()
436 * Note that all GL error checking will have been done already.
438 * \param ctx GL context.
439 * \param target Buffer object target on which to operate.
440 * \param offset Offset of the first byte to be fetched.
441 * \param size Size, in bytes, of the data range.
442 * \param data Destination for data
443 * \param bufObj Object to be used.
445 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
448 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
449 GLsizeiptrARB size
, GLvoid
* data
,
450 struct gl_buffer_object
* bufObj
)
454 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
455 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
461 * Default fallback for \c dd_function_table::MapBufferRange().
462 * Called via glMapBufferRange().
465 _mesa_buffer_map_range( struct gl_context
*ctx
, GLintptr offset
,
466 GLsizeiptr length
, GLbitfield access
,
467 struct gl_buffer_object
*bufObj
)
470 assert(!_mesa_bufferobj_mapped(bufObj
));
471 /* Just return a direct pointer to the data */
472 bufObj
->Pointer
= bufObj
->Data
+ offset
;
473 bufObj
->Length
= length
;
474 bufObj
->Offset
= offset
;
475 bufObj
->AccessFlags
= access
;
476 return bufObj
->Pointer
;
481 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
482 * Called via glFlushMappedBufferRange().
485 _mesa_buffer_flush_mapped_range( struct gl_context
*ctx
,
486 GLintptr offset
, GLsizeiptr length
,
487 struct gl_buffer_object
*obj
)
498 * Default callback for \c dd_function_table::MapBuffer().
500 * The input parameters will have been already tested for errors.
502 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
505 _mesa_buffer_unmap( struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
508 /* XXX we might assert here that bufObj->Pointer is non-null */
509 bufObj
->Pointer
= NULL
;
512 bufObj
->AccessFlags
= 0x0;
518 * Default fallback for \c dd_function_table::CopyBufferSubData().
519 * Called via glCopyBuffserSubData().
522 _mesa_copy_buffer_subdata(struct gl_context
*ctx
,
523 struct gl_buffer_object
*src
,
524 struct gl_buffer_object
*dst
,
525 GLintptr readOffset
, GLintptr writeOffset
,
528 GLubyte
*srcPtr
, *dstPtr
;
530 /* the buffers should not be mapped */
531 assert(!_mesa_bufferobj_mapped(src
));
532 assert(!_mesa_bufferobj_mapped(dst
));
535 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
537 GL_MAP_WRITE_BIT
, src
);
542 srcPtr
+= readOffset
;
543 dstPtr
+= writeOffset
;
545 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
546 GL_MAP_READ_BIT
, src
);
547 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
549 GL_MAP_INVALIDATE_RANGE_BIT
), dst
);
552 /* Note: the src and dst regions will never overlap. Trying to do so
553 * would generate GL_INVALID_VALUE earlier.
555 if (srcPtr
&& dstPtr
)
556 memcpy(dstPtr
, srcPtr
, size
);
558 ctx
->Driver
.UnmapBuffer(ctx
, src
);
560 ctx
->Driver
.UnmapBuffer(ctx
, dst
);
566 * Initialize the state associated with buffer objects
569 _mesa_init_buffer_objects( struct gl_context
*ctx
)
571 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
572 _glthread_INIT_MUTEX(DummyBufferObject
.Mutex
);
573 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
575 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
576 ctx
->Shared
->NullBufferObj
);
578 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
579 ctx
->Shared
->NullBufferObj
);
580 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
581 ctx
->Shared
->NullBufferObj
);
586 _mesa_free_buffer_objects( struct gl_context
*ctx
)
588 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
590 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
591 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
596 * Bind the specified target to buffer for the specified context.
597 * Called by glBindBuffer() and other functions.
600 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
602 struct gl_buffer_object
*oldBufObj
;
603 struct gl_buffer_object
*newBufObj
= NULL
;
604 struct gl_buffer_object
**bindTarget
= NULL
;
606 bindTarget
= get_buffer_target(ctx
, target
);
608 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
612 /* Get pointer to old buffer object (to be unbound) */
613 oldBufObj
= *bindTarget
;
614 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
615 return; /* rebinding the same buffer object- no change */
618 * Get pointer to new buffer object (newBufObj)
621 /* The spec says there's not a buffer object named 0, but we use
622 * one internally because it simplifies things.
624 newBufObj
= ctx
->Shared
->NullBufferObj
;
627 /* non-default buffer object */
628 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
629 if (!newBufObj
|| newBufObj
== &DummyBufferObject
) {
630 /* If this is a new buffer object id, or one which was generated but
631 * never used before, allocate a buffer object now.
633 ASSERT(ctx
->Driver
.NewBufferObject
);
634 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
636 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
639 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
643 /* bind new buffer */
644 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
646 /* Pass BindBuffer call to device driver */
647 if (ctx
->Driver
.BindBuffer
)
648 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
653 * Update the default buffer objects in the given context to reference those
654 * specified in the shared state and release those referencing the old
658 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
660 /* Bind the NullBufferObj to remove references to those
661 * in the shared context hash table.
663 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
664 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
665 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
666 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
672 * Return the gl_buffer_object for the given ID.
673 * Always return NULL for ID 0.
675 struct gl_buffer_object
*
676 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
681 return (struct gl_buffer_object
*)
682 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
687 * If *ptr points to obj, set ptr = the Null/default buffer object.
688 * This is a helper for buffer object deletion.
689 * The GL spec says that deleting a buffer object causes it to get
690 * unbound from all arrays in the current context.
693 unbind(struct gl_context
*ctx
,
694 struct gl_buffer_object
**ptr
,
695 struct gl_buffer_object
*obj
)
698 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
704 * Plug default/fallback buffer object functions into the device
708 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
710 /* GL_ARB_vertex/pixel_buffer_object */
711 driver
->NewBufferObject
= _mesa_new_buffer_object
;
712 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
713 driver
->BindBuffer
= NULL
;
714 driver
->BufferData
= _mesa_buffer_data
;
715 driver
->BufferSubData
= _mesa_buffer_subdata
;
716 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
717 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
719 /* GL_ARB_map_buffer_range */
720 driver
->MapBufferRange
= _mesa_buffer_map_range
;
721 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
723 /* GL_ARB_copy_buffer */
724 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
729 /**********************************************************************/
731 /**********************************************************************/
734 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
736 GET_CURRENT_CONTEXT(ctx
);
737 ASSERT_OUTSIDE_BEGIN_END(ctx
);
739 if (MESA_VERBOSE
& VERBOSE_API
)
740 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
741 _mesa_lookup_enum_by_nr(target
), buffer
);
743 bind_buffer_object(ctx
, target
, buffer
);
748 * Delete a set of buffer objects.
750 * \param n Number of buffer objects to delete.
751 * \param ids Array of \c n buffer object IDs.
754 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
756 GET_CURRENT_CONTEXT(ctx
);
758 ASSERT_OUTSIDE_BEGIN_END(ctx
);
759 FLUSH_VERTICES(ctx
, 0);
762 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
766 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
768 for (i
= 0; i
< n
; i
++) {
769 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
771 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
774 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
776 if (_mesa_bufferobj_mapped(bufObj
)) {
777 /* if mapped, unmap it now */
778 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
779 bufObj
->AccessFlags
= default_access_mode(ctx
);
780 bufObj
->Pointer
= NULL
;
783 /* unbind any vertex pointers bound to this buffer */
784 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
785 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
788 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
789 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
791 if (arrayObj
->ElementArrayBufferObj
== bufObj
) {
792 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
795 /* unbind ARB_copy_buffer binding points */
796 if (ctx
->CopyReadBuffer
== bufObj
) {
797 _mesa_BindBufferARB( GL_COPY_READ_BUFFER
, 0 );
799 if (ctx
->CopyWriteBuffer
== bufObj
) {
800 _mesa_BindBufferARB( GL_COPY_WRITE_BUFFER
, 0 );
803 /* unbind transform feedback binding points */
804 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
805 _mesa_BindBufferARB( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
807 for (j
= 0; j
< MAX_FEEDBACK_ATTRIBS
; j
++) {
808 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
809 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
813 /* unbind any pixel pack/unpack pointers bound to this buffer */
814 if (ctx
->Pack
.BufferObj
== bufObj
) {
815 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
817 if (ctx
->Unpack
.BufferObj
== bufObj
) {
818 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
821 /* The ID is immediately freed for re-use */
822 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
823 /* Make sure we do not run into the classic ABA problem on bind.
824 * We don't want to allow re-binding a buffer object that's been
825 * "deleted" by glDeleteBuffers().
827 * The explicit rebinding to the default object in the current context
828 * prevents the above in the current context, but another context
829 * sharing the same objects might suffer from this problem.
830 * The alternative would be to do the hash lookup in any case on bind
831 * which would introduce more runtime overhead than this.
833 bufObj
->DeletePending
= GL_TRUE
;
834 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
838 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
843 * Generate a set of unique buffer object IDs and store them in \c buffer.
845 * \param n Number of IDs to generate.
846 * \param buffer Array of \c n locations to store the IDs.
849 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
851 GET_CURRENT_CONTEXT(ctx
);
854 ASSERT_OUTSIDE_BEGIN_END(ctx
);
856 if (MESA_VERBOSE
& VERBOSE_API
)
857 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
860 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
869 * This must be atomic (generation and allocation of buffer object IDs)
871 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
873 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
875 /* Insert the ID and pointer to dummy buffer object into hash table */
876 for (i
= 0; i
< n
; i
++) {
877 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
879 buffer
[i
] = first
+ i
;
882 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
887 * Determine if ID is the name of a buffer object.
889 * \param id ID of the potential buffer object.
890 * \return \c GL_TRUE if \c id is the name of a buffer object,
891 * \c GL_FALSE otherwise.
894 _mesa_IsBufferARB(GLuint id
)
896 struct gl_buffer_object
*bufObj
;
897 GET_CURRENT_CONTEXT(ctx
);
898 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
900 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
901 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
902 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
904 return bufObj
&& bufObj
!= &DummyBufferObject
;
909 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
910 const GLvoid
* data
, GLenum usage
)
912 GET_CURRENT_CONTEXT(ctx
);
913 struct gl_buffer_object
*bufObj
;
914 ASSERT_OUTSIDE_BEGIN_END(ctx
);
916 if (MESA_VERBOSE
& VERBOSE_API
)
917 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
918 _mesa_lookup_enum_by_nr(target
),
919 (long int) size
, data
,
920 _mesa_lookup_enum_by_nr(usage
));
923 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
928 case GL_STREAM_DRAW_ARB
:
929 case GL_STREAM_READ_ARB
:
930 case GL_STREAM_COPY_ARB
:
931 case GL_STATIC_DRAW_ARB
:
932 case GL_STATIC_READ_ARB
:
933 case GL_STATIC_COPY_ARB
:
934 case GL_DYNAMIC_DRAW_ARB
:
935 case GL_DYNAMIC_READ_ARB
:
936 case GL_DYNAMIC_COPY_ARB
:
940 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
944 bufObj
= get_buffer(ctx
, "glBufferDataARB", target
);
948 if (_mesa_bufferobj_mapped(bufObj
)) {
949 /* Unmap the existing buffer. We'll replace it now. Not an error. */
950 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
951 bufObj
->AccessFlags
= default_access_mode(ctx
);
952 ASSERT(bufObj
->Pointer
== NULL
);
955 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
957 bufObj
->Written
= GL_TRUE
;
960 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
961 bufObj
->Name
, size
, data
, usage
);
968 ASSERT(ctx
->Driver
.BufferData
);
969 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
970 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
976 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
977 GLsizeiptrARB size
, const GLvoid
* data
)
979 GET_CURRENT_CONTEXT(ctx
);
980 struct gl_buffer_object
*bufObj
;
981 ASSERT_OUTSIDE_BEGIN_END(ctx
);
983 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
984 "glBufferSubDataARB" );
986 /* error already recorded */
993 bufObj
->Written
= GL_TRUE
;
995 ASSERT(ctx
->Driver
.BufferSubData
);
996 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
1001 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1002 GLsizeiptrARB size
, void * data
)
1004 GET_CURRENT_CONTEXT(ctx
);
1005 struct gl_buffer_object
*bufObj
;
1006 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1008 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1009 "glGetBufferSubDataARB" );
1011 /* error already recorded */
1015 ASSERT(ctx
->Driver
.GetBufferSubData
);
1016 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
1021 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1023 GET_CURRENT_CONTEXT(ctx
);
1024 struct gl_buffer_object
* bufObj
;
1025 GLbitfield accessFlags
;
1028 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1031 case GL_READ_ONLY_ARB
:
1032 accessFlags
= GL_MAP_READ_BIT
;
1034 case GL_WRITE_ONLY_ARB
:
1035 accessFlags
= GL_MAP_WRITE_BIT
;
1037 case GL_READ_WRITE_ARB
:
1038 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1041 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1045 bufObj
= get_buffer(ctx
, "glMapBufferARB", target
);
1049 if (_mesa_bufferobj_mapped(bufObj
)) {
1050 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1054 if (!bufObj
->Size
) {
1055 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1056 "glMapBuffer(buffer size = 0)");
1060 ASSERT(ctx
->Driver
.MapBufferRange
);
1061 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
);
1063 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1067 /* The driver callback should have set these fields.
1068 * This is important because other modules (like VBO) might call
1069 * the driver function directly.
1071 ASSERT(bufObj
->Pointer
== map
);
1072 ASSERT(bufObj
->Length
== bufObj
->Size
);
1073 ASSERT(bufObj
->Offset
== 0);
1074 bufObj
->AccessFlags
= accessFlags
;
1077 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1078 bufObj
->Written
= GL_TRUE
;
1081 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1082 bufObj
->Name
, bufObj
->Size
, access
);
1083 if (access
== GL_WRITE_ONLY_ARB
) {
1085 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1086 for (i
= 0; i
< bufObj
->Size
; i
++)
1093 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1095 /* buffer is 100 bytes larger than requested, fill with magic value */
1096 for (i
= 0; i
< 100; i
++) {
1097 buf
[bufObj
->Size
- i
- 1] = 123;
1102 return bufObj
->Pointer
;
1106 GLboolean GLAPIENTRY
1107 _mesa_UnmapBufferARB(GLenum target
)
1109 GET_CURRENT_CONTEXT(ctx
);
1110 struct gl_buffer_object
*bufObj
;
1111 GLboolean status
= GL_TRUE
;
1112 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1114 bufObj
= get_buffer(ctx
, "glUnmapBufferARB", target
);
1118 if (!_mesa_bufferobj_mapped(bufObj
)) {
1119 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1124 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1125 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1127 /* check that last 100 bytes are still = magic value */
1128 for (i
= 0; i
< 100; i
++) {
1129 GLuint pos
= bufObj
->Size
- i
- 1;
1130 if (buf
[pos
] != 123) {
1131 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1132 " at position %d (value = %u)\n",
1140 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1141 GLuint i
, unchanged
= 0;
1142 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1144 /* check which bytes changed */
1145 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1146 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1153 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1154 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1159 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1160 bufObj
->AccessFlags
= default_access_mode(ctx
);
1161 ASSERT(bufObj
->Pointer
== NULL
);
1162 ASSERT(bufObj
->Offset
== 0);
1163 ASSERT(bufObj
->Length
== 0);
1170 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1172 GET_CURRENT_CONTEXT(ctx
);
1173 struct gl_buffer_object
*bufObj
;
1174 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1176 bufObj
= get_buffer(ctx
, "glGetBufferParameterivARB", target
);
1181 case GL_BUFFER_SIZE_ARB
:
1182 *params
= (GLint
) bufObj
->Size
;
1184 case GL_BUFFER_USAGE_ARB
:
1185 *params
= bufObj
->Usage
;
1187 case GL_BUFFER_ACCESS_ARB
:
1188 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1190 case GL_BUFFER_MAPPED_ARB
:
1191 *params
= _mesa_bufferobj_mapped(bufObj
);
1193 case GL_BUFFER_ACCESS_FLAGS
:
1194 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1196 *params
= bufObj
->AccessFlags
;
1198 case GL_BUFFER_MAP_OFFSET
:
1199 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1201 *params
= (GLint
) bufObj
->Offset
;
1203 case GL_BUFFER_MAP_LENGTH
:
1204 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1206 *params
= (GLint
) bufObj
->Length
;
1209 ; /* fall-through */
1213 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1214 _mesa_lookup_enum_by_nr(pname
));
1220 * This is pretty much a duplicate of GetBufferParameteriv() but the
1221 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1224 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1226 GET_CURRENT_CONTEXT(ctx
);
1227 struct gl_buffer_object
*bufObj
;
1228 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1230 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
);
1235 case GL_BUFFER_SIZE_ARB
:
1236 *params
= bufObj
->Size
;
1238 case GL_BUFFER_USAGE_ARB
:
1239 *params
= bufObj
->Usage
;
1241 case GL_BUFFER_ACCESS_ARB
:
1242 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1244 case GL_BUFFER_ACCESS_FLAGS
:
1245 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1247 *params
= bufObj
->AccessFlags
;
1249 case GL_BUFFER_MAPPED_ARB
:
1250 *params
= _mesa_bufferobj_mapped(bufObj
);
1252 case GL_BUFFER_MAP_OFFSET
:
1253 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1255 *params
= bufObj
->Offset
;
1257 case GL_BUFFER_MAP_LENGTH
:
1258 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1260 *params
= bufObj
->Length
;
1263 ; /* fall-through */
1267 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1268 _mesa_lookup_enum_by_nr(pname
));
1273 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1275 GET_CURRENT_CONTEXT(ctx
);
1276 struct gl_buffer_object
* bufObj
;
1277 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1279 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1280 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1284 bufObj
= get_buffer(ctx
, "glGetBufferPointervARB", target
);
1288 *params
= bufObj
->Pointer
;
1293 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1294 GLintptr readOffset
, GLintptr writeOffset
,
1297 GET_CURRENT_CONTEXT(ctx
);
1298 struct gl_buffer_object
*src
, *dst
;
1299 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1301 src
= get_buffer(ctx
, "glCopyBuffserSubData", readTarget
);
1305 dst
= get_buffer(ctx
, "glCopyBuffserSubData", writeTarget
);
1309 if (_mesa_bufferobj_mapped(src
)) {
1310 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1311 "glCopyBuffserSubData(readBuffer is mapped)");
1315 if (_mesa_bufferobj_mapped(dst
)) {
1316 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1317 "glCopyBuffserSubData(writeBuffer is mapped)");
1321 if (readOffset
< 0) {
1322 _mesa_error(ctx
, GL_INVALID_VALUE
,
1323 "glCopyBuffserSubData(readOffset = %d)", (int) readOffset
);
1327 if (writeOffset
< 0) {
1328 _mesa_error(ctx
, GL_INVALID_VALUE
,
1329 "glCopyBuffserSubData(writeOffset = %d)", (int) writeOffset
);
1334 _mesa_error(ctx
, GL_INVALID_VALUE
,
1335 "glCopyBufferSubData(writeOffset = %d)", (int) size
);
1339 if (readOffset
+ size
> src
->Size
) {
1340 _mesa_error(ctx
, GL_INVALID_VALUE
,
1341 "glCopyBuffserSubData(readOffset + size = %d)",
1342 (int) (readOffset
+ size
));
1346 if (writeOffset
+ size
> dst
->Size
) {
1347 _mesa_error(ctx
, GL_INVALID_VALUE
,
1348 "glCopyBuffserSubData(writeOffset + size = %d)",
1349 (int) (writeOffset
+ size
));
1354 if (readOffset
+ size
<= writeOffset
) {
1357 else if (writeOffset
+ size
<= readOffset
) {
1361 /* overlapping src/dst is illegal */
1362 _mesa_error(ctx
, GL_INVALID_VALUE
,
1363 "glCopyBuffserSubData(overlapping src/dst)");
1368 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1373 * See GL_ARB_map_buffer_range spec
1376 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1379 GET_CURRENT_CONTEXT(ctx
);
1380 struct gl_buffer_object
*bufObj
;
1383 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1385 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1386 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1387 "glMapBufferRange(extension not supported)");
1392 _mesa_error(ctx
, GL_INVALID_VALUE
,
1393 "glMapBufferRange(offset = %ld)", (long)offset
);
1398 _mesa_error(ctx
, GL_INVALID_VALUE
,
1399 "glMapBufferRange(length = %ld)", (long)length
);
1403 if (access
& ~(GL_MAP_READ_BIT
|
1405 GL_MAP_INVALIDATE_RANGE_BIT
|
1406 GL_MAP_INVALIDATE_BUFFER_BIT
|
1407 GL_MAP_FLUSH_EXPLICIT_BIT
|
1408 GL_MAP_UNSYNCHRONIZED_BIT
)) {
1409 /* generate an error if any undefind bit is set */
1410 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1414 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1415 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1416 "glMapBufferRange(access indicates neither read or write)");
1420 if ((access
& GL_MAP_READ_BIT
) &&
1421 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1422 GL_MAP_INVALIDATE_BUFFER_BIT
|
1423 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1424 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1425 "glMapBufferRange(invalid access flags)");
1429 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1430 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1431 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1432 "glMapBufferRange(invalid access flags)");
1436 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
);
1440 if (offset
+ length
> bufObj
->Size
) {
1441 _mesa_error(ctx
, GL_INVALID_VALUE
,
1442 "glMapBufferRange(offset + length > size)");
1446 if (_mesa_bufferobj_mapped(bufObj
)) {
1447 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1448 "glMapBufferRange(buffer already mapped)");
1452 if (!bufObj
->Size
) {
1453 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1454 "glMapBufferRange(buffer size = 0)");
1458 /* Mapping zero bytes should return a non-null pointer. */
1460 static long dummy
= 0;
1461 bufObj
->Pointer
= &dummy
;
1462 bufObj
->Length
= length
;
1463 bufObj
->Offset
= offset
;
1464 bufObj
->AccessFlags
= access
;
1465 return bufObj
->Pointer
;
1468 ASSERT(ctx
->Driver
.MapBufferRange
);
1469 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
);
1471 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1474 /* The driver callback should have set all these fields.
1475 * This is important because other modules (like VBO) might call
1476 * the driver function directly.
1478 ASSERT(bufObj
->Pointer
== map
);
1479 ASSERT(bufObj
->Length
== length
);
1480 ASSERT(bufObj
->Offset
== offset
);
1481 ASSERT(bufObj
->AccessFlags
== access
);
1489 * See GL_ARB_map_buffer_range spec
1492 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1494 GET_CURRENT_CONTEXT(ctx
);
1495 struct gl_buffer_object
*bufObj
;
1496 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1498 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1499 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1500 "glFlushMappedBufferRange(extension not supported)");
1505 _mesa_error(ctx
, GL_INVALID_VALUE
,
1506 "glFlushMappedBufferRange(offset = %ld)", (long)offset
);
1511 _mesa_error(ctx
, GL_INVALID_VALUE
,
1512 "glFlushMappedBufferRange(length = %ld)", (long)length
);
1516 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
);
1520 if (!_mesa_bufferobj_mapped(bufObj
)) {
1521 /* buffer is not mapped */
1522 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1523 "glFlushMappedBufferRange(buffer is not mapped)");
1527 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1528 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1529 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1533 if (offset
+ length
> bufObj
->Length
) {
1534 _mesa_error(ctx
, GL_INVALID_VALUE
,
1535 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1536 (long)offset
, (long)length
, (long)bufObj
->Length
);
1540 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1542 if (ctx
->Driver
.FlushMappedBufferRange
)
1543 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
);
1547 #if FEATURE_APPLE_object_purgeable
1549 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1551 struct gl_buffer_object
*bufObj
;
1554 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1556 _mesa_error(ctx
, GL_INVALID_VALUE
,
1557 "glObjectPurgeable(name = 0x%x)", name
);
1560 if (!_mesa_is_bufferobj(bufObj
)) {
1561 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1565 if (bufObj
->Purgeable
) {
1566 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1567 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1568 return GL_VOLATILE_APPLE
;
1571 bufObj
->Purgeable
= GL_TRUE
;
1573 retval
= GL_VOLATILE_APPLE
;
1574 if (ctx
->Driver
.BufferObjectPurgeable
)
1575 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1582 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1584 struct gl_renderbuffer
*bufObj
;
1587 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1589 _mesa_error(ctx
, GL_INVALID_VALUE
,
1590 "glObjectUnpurgeable(name = 0x%x)", name
);
1594 if (bufObj
->Purgeable
) {
1595 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1596 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1597 return GL_VOLATILE_APPLE
;
1600 bufObj
->Purgeable
= GL_TRUE
;
1602 retval
= GL_VOLATILE_APPLE
;
1603 if (ctx
->Driver
.RenderObjectPurgeable
)
1604 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1611 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1613 struct gl_texture_object
*bufObj
;
1616 bufObj
= _mesa_lookup_texture(ctx
, name
);
1618 _mesa_error(ctx
, GL_INVALID_VALUE
,
1619 "glObjectPurgeable(name = 0x%x)", name
);
1623 if (bufObj
->Purgeable
) {
1624 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1625 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1626 return GL_VOLATILE_APPLE
;
1629 bufObj
->Purgeable
= GL_TRUE
;
1631 retval
= GL_VOLATILE_APPLE
;
1632 if (ctx
->Driver
.TextureObjectPurgeable
)
1633 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1640 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1644 GET_CURRENT_CONTEXT(ctx
);
1645 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1648 _mesa_error(ctx
, GL_INVALID_VALUE
,
1649 "glObjectPurgeable(name = 0x%x)", name
);
1654 case GL_VOLATILE_APPLE
:
1655 case GL_RELEASED_APPLE
:
1659 _mesa_error(ctx
, GL_INVALID_ENUM
,
1660 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1665 switch (objectType
) {
1667 retval
= texture_object_purgeable(ctx
, name
, option
);
1669 case GL_RENDERBUFFER_EXT
:
1670 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1672 case GL_BUFFER_OBJECT_APPLE
:
1673 retval
= buffer_object_purgeable(ctx
, name
, option
);
1676 _mesa_error(ctx
, GL_INVALID_ENUM
,
1677 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1682 /* In strict conformance to the spec, we must only return VOLATILE when
1683 * when passed the VOLATILE option. Madness.
1685 * XXX First fix the spec, then fix me.
1687 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1692 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1694 struct gl_buffer_object
*bufObj
;
1697 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1699 _mesa_error(ctx
, GL_INVALID_VALUE
,
1700 "glObjectUnpurgeable(name = 0x%x)", name
);
1704 if (! bufObj
->Purgeable
) {
1705 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1706 "glObjectUnpurgeable(name = 0x%x) object is "
1707 " already \"unpurged\"", name
);
1711 bufObj
->Purgeable
= GL_FALSE
;
1714 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1715 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1722 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1724 struct gl_renderbuffer
*bufObj
;
1727 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1729 _mesa_error(ctx
, GL_INVALID_VALUE
,
1730 "glObjectUnpurgeable(name = 0x%x)", name
);
1734 if (! bufObj
->Purgeable
) {
1735 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1736 "glObjectUnpurgeable(name = 0x%x) object is "
1737 " already \"unpurged\"", name
);
1741 bufObj
->Purgeable
= GL_FALSE
;
1744 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1745 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1752 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1754 struct gl_texture_object
*bufObj
;
1757 bufObj
= _mesa_lookup_texture(ctx
, name
);
1759 _mesa_error(ctx
, GL_INVALID_VALUE
,
1760 "glObjectUnpurgeable(name = 0x%x)", name
);
1764 if (! bufObj
->Purgeable
) {
1765 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1766 "glObjectUnpurgeable(name = 0x%x) object is"
1767 " already \"unpurged\"", name
);
1771 bufObj
->Purgeable
= GL_FALSE
;
1774 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1775 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1782 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1784 GET_CURRENT_CONTEXT(ctx
);
1785 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1788 _mesa_error(ctx
, GL_INVALID_VALUE
,
1789 "glObjectUnpurgeable(name = 0x%x)", name
);
1794 case GL_RETAINED_APPLE
:
1795 case GL_UNDEFINED_APPLE
:
1799 _mesa_error(ctx
, GL_INVALID_ENUM
,
1800 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1805 switch (objectType
) {
1806 case GL_BUFFER_OBJECT_APPLE
:
1807 return buffer_object_unpurgeable(ctx
, name
, option
);
1809 return texture_object_unpurgeable(ctx
, name
, option
);
1810 case GL_RENDERBUFFER_EXT
:
1811 return renderbuffer_unpurgeable(ctx
, name
, option
);
1813 _mesa_error(ctx
, GL_INVALID_ENUM
,
1814 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1822 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1823 GLenum pname
, GLint
*params
)
1825 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1827 _mesa_error(ctx
, GL_INVALID_VALUE
,
1828 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1833 case GL_PURGEABLE_APPLE
:
1834 *params
= bufObj
->Purgeable
;
1837 _mesa_error(ctx
, GL_INVALID_ENUM
,
1838 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1846 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1847 GLenum pname
, GLint
*params
)
1849 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1851 _mesa_error(ctx
, GL_INVALID_VALUE
,
1852 "glObjectUnpurgeable(name = 0x%x)", name
);
1857 case GL_PURGEABLE_APPLE
:
1858 *params
= rb
->Purgeable
;
1861 _mesa_error(ctx
, GL_INVALID_ENUM
,
1862 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1870 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1871 GLenum pname
, GLint
*params
)
1873 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1875 _mesa_error(ctx
, GL_INVALID_VALUE
,
1876 "glObjectUnpurgeable(name = 0x%x)", name
);
1881 case GL_PURGEABLE_APPLE
:
1882 *params
= texObj
->Purgeable
;
1885 _mesa_error(ctx
, GL_INVALID_ENUM
,
1886 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1894 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
1897 GET_CURRENT_CONTEXT(ctx
);
1900 _mesa_error(ctx
, GL_INVALID_VALUE
,
1901 "glGetObjectParameteriv(name = 0x%x)", name
);
1905 switch (objectType
) {
1907 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
1909 case GL_BUFFER_OBJECT_APPLE
:
1910 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
1912 case GL_RENDERBUFFER_EXT
:
1913 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
1916 _mesa_error(ctx
, GL_INVALID_ENUM
,
1917 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1922 #endif /* FEATURE_APPLE_object_purgeable */