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*/
52 #if FEATURE_OES_mapbuffer
53 #define DEFAULT_ACCESS GL_MAP_WRITE_BIT
55 #define DEFAULT_ACCESS (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)
60 * Used as a placeholder for buffer objects between glGenBuffers() and
61 * glBindBuffer() so that glIsBuffer() can work correctly.
63 static struct gl_buffer_object DummyBufferObject
;
67 * Return pointer to address of a buffer object target.
68 * \param ctx the GL context
69 * \param target the buffer object target to be retrieved.
70 * \return pointer to pointer to the buffer object bound to \c target in the
71 * specified context or \c NULL if \c target is invalid.
73 static inline struct gl_buffer_object
**
74 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
77 case GL_ARRAY_BUFFER_ARB
:
78 return &ctx
->Array
.ArrayBufferObj
;
79 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
80 return &ctx
->Array
.ElementArrayBufferObj
;
81 case GL_PIXEL_PACK_BUFFER_EXT
:
82 return &ctx
->Pack
.BufferObj
;
83 case GL_PIXEL_UNPACK_BUFFER_EXT
:
84 return &ctx
->Unpack
.BufferObj
;
85 case GL_COPY_READ_BUFFER
:
86 return &ctx
->CopyReadBuffer
;
87 case GL_COPY_WRITE_BUFFER
:
88 return &ctx
->CopyWriteBuffer
;
89 #if FEATURE_EXT_transform_feedback
90 case GL_TRANSFORM_FEEDBACK_BUFFER
:
91 if (ctx
->Extensions
.EXT_transform_feedback
) {
92 return &ctx
->TransformFeedback
.CurrentBuffer
;
96 case GL_TEXTURE_BUFFER
:
97 if (ctx
->Extensions
.ARB_texture_buffer_object
) {
98 return &ctx
->Texture
.BufferObject
;
109 * Get the buffer object bound to the specified target in a GL context.
110 * \param ctx the GL context
111 * \param target the buffer object target to be retrieved.
112 * \return pointer to the buffer object bound to \c target in the
113 * specified context or \c NULL if \c target is invalid.
115 static inline struct gl_buffer_object
*
116 get_buffer(struct gl_context
*ctx
, GLenum target
)
118 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
126 * Convert a GLbitfield describing the mapped buffer access flags
127 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
130 simplified_access_mode(GLbitfield access
)
132 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
133 if ((access
& rwFlags
) == rwFlags
)
134 return GL_READ_WRITE
;
135 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
137 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
138 return GL_WRITE_ONLY
;
139 return GL_READ_WRITE
; /* this should never happen, but no big deal */
144 * Tests the subdata range parameters and sets the GL error code for
145 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
147 * \param ctx GL context.
148 * \param target Buffer object target on which to operate.
149 * \param offset Offset of the first byte of the subdata range.
150 * \param size Size, in bytes, of the subdata range.
151 * \param caller Name of calling function for recording errors.
152 * \return A pointer to the buffer object bound to \c target in the
153 * specified context or \c NULL if any of the parameter or state
154 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
157 * \sa glBufferSubDataARB, glGetBufferSubDataARB
159 static struct gl_buffer_object
*
160 buffer_object_subdata_range_good( struct gl_context
* ctx
, GLenum target
,
161 GLintptrARB offset
, GLsizeiptrARB size
,
164 struct gl_buffer_object
*bufObj
;
167 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
172 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
176 bufObj
= get_buffer(ctx
, target
);
178 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", caller
);
181 if (!_mesa_is_bufferobj(bufObj
)) {
182 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
185 if (offset
+ size
> bufObj
->Size
) {
186 _mesa_error(ctx
, GL_INVALID_VALUE
,
187 "%s(size + offset > buffer size)", caller
);
190 if (_mesa_bufferobj_mapped(bufObj
)) {
191 /* Buffer is currently mapped */
192 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
201 * Allocate and initialize a new buffer object.
203 * Default callback for the \c dd_function_table::NewBufferObject() hook.
205 static struct gl_buffer_object
*
206 _mesa_new_buffer_object( struct gl_context
*ctx
, GLuint name
, GLenum target
)
208 struct gl_buffer_object
*obj
;
212 obj
= MALLOC_STRUCT(gl_buffer_object
);
213 _mesa_initialize_buffer_object(obj
, name
, target
);
219 * Delete a buffer object.
221 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
224 _mesa_delete_buffer_object(struct gl_context
*ctx
,
225 struct gl_buffer_object
*bufObj
)
232 /* assign strange values here to help w/ debugging */
233 bufObj
->RefCount
= -1000;
236 _glthread_DESTROY_MUTEX(bufObj
->Mutex
);
243 * Set ptr to bufObj w/ reference counting.
244 * This is normally only called from the _mesa_reference_buffer_object() macro
245 * when there's a real pointer change.
248 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
249 struct gl_buffer_object
**ptr
,
250 struct gl_buffer_object
*bufObj
)
253 /* Unreference the old buffer */
254 GLboolean deleteFlag
= GL_FALSE
;
255 struct gl_buffer_object
*oldObj
= *ptr
;
257 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
258 ASSERT(oldObj
->RefCount
> 0);
261 printf("BufferObj %p %d DECR to %d\n",
262 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
264 deleteFlag
= (oldObj
->RefCount
== 0);
265 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
269 /* some sanity checking: don't delete a buffer still in use */
271 /* unfortunately, these tests are invalid during context tear-down */
272 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
273 ASSERT(ctx
->Array
.ElementArrayBufferObj
!= bufObj
);
274 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
277 ASSERT(ctx
->Driver
.DeleteBuffer
);
278 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
286 /* reference new buffer */
287 _glthread_LOCK_MUTEX(bufObj
->Mutex
);
288 if (bufObj
->RefCount
== 0) {
289 /* this buffer's being deleted (look just above) */
290 /* Not sure this can every really happen. Warn if it does. */
291 _mesa_problem(NULL
, "referencing deleted buffer object");
297 printf("BufferObj %p %d INCR to %d\n",
298 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
302 _glthread_UNLOCK_MUTEX(bufObj
->Mutex
);
308 * Initialize a buffer object to default values.
311 _mesa_initialize_buffer_object( struct gl_buffer_object
*obj
,
312 GLuint name
, GLenum target
)
316 memset(obj
, 0, sizeof(struct gl_buffer_object
));
317 _glthread_INIT_MUTEX(obj
->Mutex
);
320 obj
->Usage
= GL_STATIC_DRAW_ARB
;
321 obj
->AccessFlags
= DEFAULT_ACCESS
;
326 * Allocate space for and store data in a buffer object. Any data that was
327 * previously stored in the buffer object is lost. If \c data is \c NULL,
328 * memory will be allocated, but no copy will occur.
330 * This is the default callback for \c dd_function_table::BufferData()
331 * Note that all GL error checking will have been done already.
333 * \param ctx GL context.
334 * \param target Buffer object target on which to operate.
335 * \param size Size, in bytes, of the new data store.
336 * \param data Pointer to the data to store in the buffer object. This
337 * pointer may be \c NULL.
338 * \param usage Hints about how the data will be used.
339 * \param bufObj Object to be used.
341 * \return GL_TRUE for success, GL_FALSE for failure
342 * \sa glBufferDataARB, dd_function_table::BufferData.
345 _mesa_buffer_data( struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
346 const GLvoid
* data
, GLenum usage
,
347 struct gl_buffer_object
* bufObj
)
351 (void) ctx
; (void) target
;
353 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
355 bufObj
->Data
= (GLubyte
*) new_data
;
357 bufObj
->Usage
= usage
;
360 memcpy( bufObj
->Data
, data
, size
);
372 * Replace data in a subrange of buffer object. If the data range
373 * specified by \c size + \c offset extends beyond the end of the buffer or
374 * if \c data is \c NULL, no copy is performed.
376 * This is the default callback for \c dd_function_table::BufferSubData()
377 * Note that all GL error checking will have been done already.
379 * \param ctx GL context.
380 * \param target Buffer object target on which to operate.
381 * \param offset Offset of the first byte to be modified.
382 * \param size Size, in bytes, of the data range.
383 * \param data Pointer to the data to store in the buffer object.
384 * \param bufObj Object to be used.
386 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
389 _mesa_buffer_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
390 GLsizeiptrARB size
, const GLvoid
* data
,
391 struct gl_buffer_object
* bufObj
)
395 /* this should have been caught in _mesa_BufferSubData() */
396 ASSERT(size
+ offset
<= bufObj
->Size
);
399 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
405 * Retrieve data from a subrange of buffer object. If the data range
406 * specified by \c size + \c offset extends beyond the end of the buffer or
407 * if \c data is \c NULL, no copy is performed.
409 * This is the default callback for \c dd_function_table::GetBufferSubData()
410 * Note that all GL error checking will have been done already.
412 * \param ctx GL context.
413 * \param target Buffer object target on which to operate.
414 * \param offset Offset of the first byte to be fetched.
415 * \param size Size, in bytes, of the data range.
416 * \param data Destination for data
417 * \param bufObj Object to be used.
419 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
422 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
423 GLsizeiptrARB size
, GLvoid
* data
,
424 struct gl_buffer_object
* bufObj
)
428 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
429 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
435 * Default fallback for \c dd_function_table::MapBufferRange().
436 * Called via glMapBufferRange().
439 _mesa_buffer_map_range( struct gl_context
*ctx
, GLintptr offset
,
440 GLsizeiptr length
, GLbitfield access
,
441 struct gl_buffer_object
*bufObj
)
444 assert(!_mesa_bufferobj_mapped(bufObj
));
445 /* Just return a direct pointer to the data */
446 bufObj
->Pointer
= bufObj
->Data
+ offset
;
447 bufObj
->Length
= length
;
448 bufObj
->Offset
= offset
;
449 bufObj
->AccessFlags
= access
;
450 return bufObj
->Pointer
;
455 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
456 * Called via glFlushMappedBufferRange().
459 _mesa_buffer_flush_mapped_range( struct gl_context
*ctx
,
460 GLintptr offset
, GLsizeiptr length
,
461 struct gl_buffer_object
*obj
)
472 * Default callback for \c dd_function_table::MapBuffer().
474 * The input parameters will have been already tested for errors.
476 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
479 _mesa_buffer_unmap( struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
482 /* XXX we might assert here that bufObj->Pointer is non-null */
483 bufObj
->Pointer
= NULL
;
486 bufObj
->AccessFlags
= 0x0;
492 * Default fallback for \c dd_function_table::CopyBufferSubData().
493 * Called via glCopyBuffserSubData().
496 _mesa_copy_buffer_subdata(struct gl_context
*ctx
,
497 struct gl_buffer_object
*src
,
498 struct gl_buffer_object
*dst
,
499 GLintptr readOffset
, GLintptr writeOffset
,
502 GLubyte
*srcPtr
, *dstPtr
;
504 /* buffer should not already be mapped */
505 assert(!_mesa_bufferobj_mapped(src
));
506 assert(!_mesa_bufferobj_mapped(dst
));
508 srcPtr
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
509 GL_MAP_READ_BIT
, src
);
510 dstPtr
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0, dst
->Size
,
511 GL_MAP_WRITE_BIT
, dst
);
513 if (srcPtr
&& dstPtr
)
514 memcpy(dstPtr
+ writeOffset
, srcPtr
+ readOffset
, size
);
516 ctx
->Driver
.UnmapBuffer(ctx
, src
);
517 ctx
->Driver
.UnmapBuffer(ctx
, dst
);
523 * Initialize the state associated with buffer objects
526 _mesa_init_buffer_objects( struct gl_context
*ctx
)
528 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
529 _glthread_INIT_MUTEX(DummyBufferObject
.Mutex
);
530 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
532 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
533 ctx
->Shared
->NullBufferObj
);
534 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
,
535 ctx
->Shared
->NullBufferObj
);
537 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
538 ctx
->Shared
->NullBufferObj
);
539 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
540 ctx
->Shared
->NullBufferObj
);
545 _mesa_free_buffer_objects( struct gl_context
*ctx
)
547 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
548 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
, NULL
);
550 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
551 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
556 * Bind the specified target to buffer for the specified context.
557 * Called by glBindBuffer() and other functions.
560 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
562 struct gl_buffer_object
*oldBufObj
;
563 struct gl_buffer_object
*newBufObj
= NULL
;
564 struct gl_buffer_object
**bindTarget
= NULL
;
566 bindTarget
= get_buffer_target(ctx
, target
);
568 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
572 /* Get pointer to old buffer object (to be unbound) */
573 oldBufObj
= *bindTarget
;
574 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
575 return; /* rebinding the same buffer object- no change */
578 * Get pointer to new buffer object (newBufObj)
581 /* The spec says there's not a buffer object named 0, but we use
582 * one internally because it simplifies things.
584 newBufObj
= ctx
->Shared
->NullBufferObj
;
587 /* non-default buffer object */
588 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
589 if (!newBufObj
|| newBufObj
== &DummyBufferObject
) {
590 /* If this is a new buffer object id, or one which was generated but
591 * never used before, allocate a buffer object now.
593 ASSERT(ctx
->Driver
.NewBufferObject
);
594 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
596 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
599 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
603 /* bind new buffer */
604 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
606 /* Pass BindBuffer call to device driver */
607 if (ctx
->Driver
.BindBuffer
)
608 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
613 * Update the default buffer objects in the given context to reference those
614 * specified in the shared state and release those referencing the old
618 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
620 /* Bind the NullBufferObj to remove references to those
621 * in the shared context hash table.
623 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
624 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
625 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
626 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
632 * Return the gl_buffer_object for the given ID.
633 * Always return NULL for ID 0.
635 struct gl_buffer_object
*
636 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
641 return (struct gl_buffer_object
*)
642 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
647 * If *ptr points to obj, set ptr = the Null/default buffer object.
648 * This is a helper for buffer object deletion.
649 * The GL spec says that deleting a buffer object causes it to get
650 * unbound from all arrays in the current context.
653 unbind(struct gl_context
*ctx
,
654 struct gl_buffer_object
**ptr
,
655 struct gl_buffer_object
*obj
)
658 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
664 * Plug default/fallback buffer object functions into the device
668 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
670 /* GL_ARB_vertex/pixel_buffer_object */
671 driver
->NewBufferObject
= _mesa_new_buffer_object
;
672 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
673 driver
->BindBuffer
= NULL
;
674 driver
->BufferData
= _mesa_buffer_data
;
675 driver
->BufferSubData
= _mesa_buffer_subdata
;
676 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
677 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
679 /* GL_ARB_map_buffer_range */
680 driver
->MapBufferRange
= _mesa_buffer_map_range
;
681 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
683 /* GL_ARB_copy_buffer */
684 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
689 /**********************************************************************/
691 /**********************************************************************/
694 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
696 GET_CURRENT_CONTEXT(ctx
);
697 ASSERT_OUTSIDE_BEGIN_END(ctx
);
699 if (MESA_VERBOSE
& VERBOSE_API
)
700 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
701 _mesa_lookup_enum_by_nr(target
), buffer
);
703 bind_buffer_object(ctx
, target
, buffer
);
708 * Delete a set of buffer objects.
710 * \param n Number of buffer objects to delete.
711 * \param ids Array of \c n buffer object IDs.
714 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
716 GET_CURRENT_CONTEXT(ctx
);
718 ASSERT_OUTSIDE_BEGIN_END(ctx
);
719 FLUSH_VERTICES(ctx
, 0);
722 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
726 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
728 for (i
= 0; i
< n
; i
++) {
729 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
731 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
734 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
736 if (_mesa_bufferobj_mapped(bufObj
)) {
737 /* if mapped, unmap it now */
738 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
739 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
740 bufObj
->Pointer
= NULL
;
743 /* unbind any vertex pointers bound to this buffer */
744 unbind(ctx
, &arrayObj
->Vertex
.BufferObj
, bufObj
);
745 unbind(ctx
, &arrayObj
->Weight
.BufferObj
, bufObj
);
746 unbind(ctx
, &arrayObj
->Normal
.BufferObj
, bufObj
);
747 unbind(ctx
, &arrayObj
->Color
.BufferObj
, bufObj
);
748 unbind(ctx
, &arrayObj
->SecondaryColor
.BufferObj
, bufObj
);
749 unbind(ctx
, &arrayObj
->FogCoord
.BufferObj
, bufObj
);
750 unbind(ctx
, &arrayObj
->Index
.BufferObj
, bufObj
);
751 unbind(ctx
, &arrayObj
->EdgeFlag
.BufferObj
, bufObj
);
752 for (j
= 0; j
< Elements(arrayObj
->TexCoord
); j
++) {
753 unbind(ctx
, &arrayObj
->TexCoord
[j
].BufferObj
, bufObj
);
755 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
756 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
759 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
760 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
762 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
763 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
766 /* unbind any pixel pack/unpack pointers bound to this buffer */
767 if (ctx
->Pack
.BufferObj
== bufObj
) {
768 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
770 if (ctx
->Unpack
.BufferObj
== bufObj
) {
771 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
774 /* The ID is immediately freed for re-use */
775 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
776 /* Make sure we do not run into the classic ABA problem on bind.
777 * We don't want to allow re-binding a buffer object that's been
778 * "deleted" by glDeleteBuffers().
780 * The explicit rebinding to the default object in the current context
781 * prevents the above in the current context, but another context
782 * sharing the same objects might suffer from this problem.
783 * The alternative would be to do the hash lookup in any case on bind
784 * which would introduce more runtime overhead than this.
786 bufObj
->DeletePending
= GL_TRUE
;
787 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
791 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
796 * Generate a set of unique buffer object IDs and store them in \c buffer.
798 * \param n Number of IDs to generate.
799 * \param buffer Array of \c n locations to store the IDs.
802 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
804 GET_CURRENT_CONTEXT(ctx
);
807 ASSERT_OUTSIDE_BEGIN_END(ctx
);
809 if (MESA_VERBOSE
& VERBOSE_API
)
810 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
813 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
822 * This must be atomic (generation and allocation of buffer object IDs)
824 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
826 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
828 /* Insert the ID and pointer to dummy buffer object into hash table */
829 for (i
= 0; i
< n
; i
++) {
830 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
832 buffer
[i
] = first
+ i
;
835 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
840 * Determine if ID is the name of a buffer object.
842 * \param id ID of the potential buffer object.
843 * \return \c GL_TRUE if \c id is the name of a buffer object,
844 * \c GL_FALSE otherwise.
847 _mesa_IsBufferARB(GLuint id
)
849 struct gl_buffer_object
*bufObj
;
850 GET_CURRENT_CONTEXT(ctx
);
851 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
853 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
854 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
855 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
857 return bufObj
&& bufObj
!= &DummyBufferObject
;
862 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
863 const GLvoid
* data
, GLenum usage
)
865 GET_CURRENT_CONTEXT(ctx
);
866 struct gl_buffer_object
*bufObj
;
867 ASSERT_OUTSIDE_BEGIN_END(ctx
);
869 if (MESA_VERBOSE
& VERBOSE_API
)
870 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
871 _mesa_lookup_enum_by_nr(target
),
872 (long int) size
, data
,
873 _mesa_lookup_enum_by_nr(usage
));
876 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
881 case GL_STREAM_DRAW_ARB
:
882 case GL_STREAM_READ_ARB
:
883 case GL_STREAM_COPY_ARB
:
884 case GL_STATIC_DRAW_ARB
:
885 case GL_STATIC_READ_ARB
:
886 case GL_STATIC_COPY_ARB
:
887 case GL_DYNAMIC_DRAW_ARB
:
888 case GL_DYNAMIC_READ_ARB
:
889 case GL_DYNAMIC_COPY_ARB
:
893 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
897 bufObj
= get_buffer(ctx
, target
);
899 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
902 if (!_mesa_is_bufferobj(bufObj
)) {
903 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer 0)" );
907 if (_mesa_bufferobj_mapped(bufObj
)) {
908 /* Unmap the existing buffer. We'll replace it now. Not an error. */
909 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
910 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
911 ASSERT(bufObj
->Pointer
== NULL
);
914 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
916 bufObj
->Written
= GL_TRUE
;
919 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
920 bufObj
->Name
, size
, data
, usage
);
927 ASSERT(ctx
->Driver
.BufferData
);
928 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
929 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
935 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
936 GLsizeiptrARB size
, const GLvoid
* data
)
938 GET_CURRENT_CONTEXT(ctx
);
939 struct gl_buffer_object
*bufObj
;
940 ASSERT_OUTSIDE_BEGIN_END(ctx
);
942 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
943 "glBufferSubDataARB" );
945 /* error already recorded */
952 bufObj
->Written
= GL_TRUE
;
954 ASSERT(ctx
->Driver
.BufferSubData
);
955 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
960 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
961 GLsizeiptrARB size
, void * data
)
963 GET_CURRENT_CONTEXT(ctx
);
964 struct gl_buffer_object
*bufObj
;
965 ASSERT_OUTSIDE_BEGIN_END(ctx
);
967 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
968 "glGetBufferSubDataARB" );
970 /* error already recorded */
974 ASSERT(ctx
->Driver
.GetBufferSubData
);
975 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
980 _mesa_MapBufferARB(GLenum target
, GLenum access
)
982 GET_CURRENT_CONTEXT(ctx
);
983 struct gl_buffer_object
* bufObj
;
984 GLbitfield accessFlags
;
987 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
990 case GL_READ_ONLY_ARB
:
991 accessFlags
= GL_MAP_READ_BIT
;
993 case GL_WRITE_ONLY_ARB
:
994 accessFlags
= GL_MAP_WRITE_BIT
;
996 case GL_READ_WRITE_ARB
:
997 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1000 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1004 bufObj
= get_buffer(ctx
, target
);
1006 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
1009 if (!_mesa_is_bufferobj(bufObj
)) {
1010 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(buffer 0)" );
1013 if (_mesa_bufferobj_mapped(bufObj
)) {
1014 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1018 if (!bufObj
->Size
) {
1019 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1020 "glMapBuffer(buffer size = 0)");
1024 ASSERT(ctx
->Driver
.MapBufferRange
);
1025 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
);
1027 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1031 /* The driver callback should have set these fields.
1032 * This is important because other modules (like VBO) might call
1033 * the driver function directly.
1035 ASSERT(bufObj
->Pointer
== map
);
1036 ASSERT(bufObj
->Length
== bufObj
->Size
);
1037 ASSERT(bufObj
->Offset
== 0);
1038 bufObj
->AccessFlags
= accessFlags
;
1041 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1042 bufObj
->Written
= GL_TRUE
;
1045 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1046 bufObj
->Name
, bufObj
->Size
, access
);
1047 if (access
== GL_WRITE_ONLY_ARB
) {
1049 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1050 for (i
= 0; i
< bufObj
->Size
; i
++)
1057 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1059 /* buffer is 100 bytes larger than requested, fill with magic value */
1060 for (i
= 0; i
< 100; i
++) {
1061 buf
[bufObj
->Size
- i
- 1] = 123;
1066 return bufObj
->Pointer
;
1070 GLboolean GLAPIENTRY
1071 _mesa_UnmapBufferARB(GLenum target
)
1073 GET_CURRENT_CONTEXT(ctx
);
1074 struct gl_buffer_object
*bufObj
;
1075 GLboolean status
= GL_TRUE
;
1076 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1078 bufObj
= get_buffer(ctx
, target
);
1080 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1083 if (!_mesa_is_bufferobj(bufObj
)) {
1084 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1087 if (!_mesa_bufferobj_mapped(bufObj
)) {
1088 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1093 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1094 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1096 /* check that last 100 bytes are still = magic value */
1097 for (i
= 0; i
< 100; i
++) {
1098 GLuint pos
= bufObj
->Size
- i
- 1;
1099 if (buf
[pos
] != 123) {
1100 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1101 " at position %d (value = %u)\n",
1109 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1110 GLuint i
, unchanged
= 0;
1111 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1113 /* check which bytes changed */
1114 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1115 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1122 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1123 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1128 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1129 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1130 ASSERT(bufObj
->Pointer
== NULL
);
1131 ASSERT(bufObj
->Offset
== 0);
1132 ASSERT(bufObj
->Length
== 0);
1139 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1141 GET_CURRENT_CONTEXT(ctx
);
1142 struct gl_buffer_object
*bufObj
;
1143 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1145 bufObj
= get_buffer(ctx
, target
);
1147 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(target)" );
1150 if (!_mesa_is_bufferobj(bufObj
)) {
1151 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferParameterivARB" );
1156 case GL_BUFFER_SIZE_ARB
:
1157 *params
= (GLint
) bufObj
->Size
;
1159 case GL_BUFFER_USAGE_ARB
:
1160 *params
= bufObj
->Usage
;
1162 case GL_BUFFER_ACCESS_ARB
:
1163 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1165 case GL_BUFFER_MAPPED_ARB
:
1166 *params
= _mesa_bufferobj_mapped(bufObj
);
1168 case GL_BUFFER_ACCESS_FLAGS
:
1169 if (ctx
->VersionMajor
< 3)
1171 *params
= bufObj
->AccessFlags
;
1173 case GL_BUFFER_MAP_OFFSET
:
1174 if (ctx
->VersionMajor
< 3)
1176 *params
= (GLint
) bufObj
->Offset
;
1178 case GL_BUFFER_MAP_LENGTH
:
1179 if (ctx
->VersionMajor
< 3)
1181 *params
= (GLint
) bufObj
->Length
;
1184 ; /* fall-through */
1188 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1189 _mesa_lookup_enum_by_nr(pname
));
1195 * This is pretty much a duplicate of GetBufferParameteriv() but the
1196 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1199 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1201 GET_CURRENT_CONTEXT(ctx
);
1202 struct gl_buffer_object
*bufObj
;
1203 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1205 bufObj
= get_buffer(ctx
, target
);
1207 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(target)" );
1210 if (!_mesa_is_bufferobj(bufObj
)) {
1211 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferParameteri64v" );
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
, target
);
1267 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1270 if (!_mesa_is_bufferobj(bufObj
)) {
1271 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1275 *params
= bufObj
->Pointer
;
1280 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1281 GLintptr readOffset
, GLintptr writeOffset
,
1284 GET_CURRENT_CONTEXT(ctx
);
1285 struct gl_buffer_object
*src
, *dst
;
1286 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1288 src
= get_buffer(ctx
, readTarget
);
1289 if (!src
|| !_mesa_is_bufferobj(src
)) {
1290 _mesa_error(ctx
, GL_INVALID_ENUM
,
1291 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget
);
1295 dst
= get_buffer(ctx
, writeTarget
);
1296 if (!dst
|| !_mesa_is_bufferobj(dst
)) {
1297 _mesa_error(ctx
, GL_INVALID_ENUM
,
1298 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget
);
1302 if (_mesa_bufferobj_mapped(src
)) {
1303 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1304 "glCopyBuffserSubData(readBuffer is mapped)");
1308 if (_mesa_bufferobj_mapped(dst
)) {
1309 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1310 "glCopyBuffserSubData(writeBuffer is mapped)");
1314 if (readOffset
< 0) {
1315 _mesa_error(ctx
, GL_INVALID_VALUE
,
1316 "glCopyBuffserSubData(readOffset = %d)", (int) readOffset
);
1320 if (writeOffset
< 0) {
1321 _mesa_error(ctx
, GL_INVALID_VALUE
,
1322 "glCopyBuffserSubData(writeOffset = %d)", (int) writeOffset
);
1326 if (readOffset
+ size
> src
->Size
) {
1327 _mesa_error(ctx
, GL_INVALID_VALUE
,
1328 "glCopyBuffserSubData(readOffset + size = %d)",
1329 (int) (readOffset
+ size
));
1333 if (writeOffset
+ size
> dst
->Size
) {
1334 _mesa_error(ctx
, GL_INVALID_VALUE
,
1335 "glCopyBuffserSubData(writeOffset + size = %d)",
1336 (int) (writeOffset
+ size
));
1341 if (readOffset
+ size
<= writeOffset
) {
1344 else if (writeOffset
+ size
<= readOffset
) {
1348 /* overlapping src/dst is illegal */
1349 _mesa_error(ctx
, GL_INVALID_VALUE
,
1350 "glCopyBuffserSubData(overlapping src/dst)");
1355 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1360 * See GL_ARB_map_buffer_range spec
1363 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1366 GET_CURRENT_CONTEXT(ctx
);
1367 struct gl_buffer_object
*bufObj
;
1370 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1372 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1373 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1374 "glMapBufferRange(extension not supported)");
1379 _mesa_error(ctx
, GL_INVALID_VALUE
,
1380 "glMapBufferRange(offset = %ld)", (long)offset
);
1385 _mesa_error(ctx
, GL_INVALID_VALUE
,
1386 "glMapBufferRange(length = %ld)", (long)length
);
1390 if (access
& ~(GL_MAP_READ_BIT
|
1392 GL_MAP_INVALIDATE_RANGE_BIT
|
1393 GL_MAP_INVALIDATE_BUFFER_BIT
|
1394 GL_MAP_FLUSH_EXPLICIT_BIT
|
1395 GL_MAP_UNSYNCHRONIZED_BIT
)) {
1396 /* generate an error if any undefind bit is set */
1397 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1401 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1402 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1403 "glMapBufferRange(access indicates neither read or write)");
1407 if ((access
& GL_MAP_READ_BIT
) &&
1408 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1409 GL_MAP_INVALIDATE_BUFFER_BIT
|
1410 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1411 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1412 "glMapBufferRange(invalid access flags)");
1416 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1417 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1418 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1419 "glMapBufferRange(invalid access flags)");
1423 bufObj
= get_buffer(ctx
, target
);
1424 if (!bufObj
|| !_mesa_is_bufferobj(bufObj
)) {
1425 _mesa_error(ctx
, GL_INVALID_ENUM
,
1426 "glMapBufferRange(target = 0x%x)", target
);
1430 if (offset
+ length
> bufObj
->Size
) {
1431 _mesa_error(ctx
, GL_INVALID_VALUE
,
1432 "glMapBufferRange(offset + length > size)");
1436 if (_mesa_bufferobj_mapped(bufObj
)) {
1437 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1438 "glMapBufferRange(buffer already mapped)");
1442 if (!bufObj
->Size
) {
1443 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1444 "glMapBufferRange(buffer size = 0)");
1448 /* Mapping zero bytes should return a non-null pointer. */
1450 static long dummy
= 0;
1451 bufObj
->Pointer
= &dummy
;
1452 bufObj
->Length
= length
;
1453 bufObj
->Offset
= offset
;
1454 bufObj
->AccessFlags
= access
;
1455 return bufObj
->Pointer
;
1458 ASSERT(ctx
->Driver
.MapBufferRange
);
1459 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
);
1461 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1464 /* The driver callback should have set all these fields.
1465 * This is important because other modules (like VBO) might call
1466 * the driver function directly.
1468 ASSERT(bufObj
->Pointer
== map
);
1469 ASSERT(bufObj
->Length
== length
);
1470 ASSERT(bufObj
->Offset
== offset
);
1471 ASSERT(bufObj
->AccessFlags
== access
);
1479 * See GL_ARB_map_buffer_range spec
1482 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1484 GET_CURRENT_CONTEXT(ctx
);
1485 struct gl_buffer_object
*bufObj
;
1486 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1488 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1489 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1490 "glMapBufferRange(extension not supported)");
1495 _mesa_error(ctx
, GL_INVALID_VALUE
,
1496 "glMapBufferRange(offset = %ld)", (long)offset
);
1501 _mesa_error(ctx
, GL_INVALID_VALUE
,
1502 "glMapBufferRange(length = %ld)", (long)length
);
1506 bufObj
= get_buffer(ctx
, target
);
1508 _mesa_error(ctx
, GL_INVALID_ENUM
,
1509 "glMapBufferRange(target = 0x%x)", target
);
1513 if (!_mesa_is_bufferobj(bufObj
)) {
1514 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1515 "glMapBufferRange(current buffer is 0)");
1519 if (!_mesa_bufferobj_mapped(bufObj
)) {
1520 /* buffer is not mapped */
1521 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1522 "glMapBufferRange(buffer is not mapped)");
1526 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1527 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1528 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1532 if (offset
+ length
> bufObj
->Length
) {
1533 _mesa_error(ctx
, GL_INVALID_VALUE
,
1534 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1535 (long)offset
, (long)length
, (long)bufObj
->Length
);
1539 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1541 if (ctx
->Driver
.FlushMappedBufferRange
)
1542 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
);
1546 #if FEATURE_APPLE_object_purgeable
1548 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1550 struct gl_buffer_object
*bufObj
;
1553 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1555 _mesa_error(ctx
, GL_INVALID_VALUE
,
1556 "glObjectPurgeable(name = 0x%x)", name
);
1559 if (!_mesa_is_bufferobj(bufObj
)) {
1560 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1564 if (bufObj
->Purgeable
) {
1565 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1566 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1567 return GL_VOLATILE_APPLE
;
1570 bufObj
->Purgeable
= GL_TRUE
;
1572 retval
= GL_VOLATILE_APPLE
;
1573 if (ctx
->Driver
.BufferObjectPurgeable
)
1574 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1581 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1583 struct gl_renderbuffer
*bufObj
;
1586 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1588 _mesa_error(ctx
, GL_INVALID_VALUE
,
1589 "glObjectUnpurgeable(name = 0x%x)", name
);
1593 if (bufObj
->Purgeable
) {
1594 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1595 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1596 return GL_VOLATILE_APPLE
;
1599 bufObj
->Purgeable
= GL_TRUE
;
1601 retval
= GL_VOLATILE_APPLE
;
1602 if (ctx
->Driver
.RenderObjectPurgeable
)
1603 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1610 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1612 struct gl_texture_object
*bufObj
;
1615 bufObj
= _mesa_lookup_texture(ctx
, name
);
1617 _mesa_error(ctx
, GL_INVALID_VALUE
,
1618 "glObjectPurgeable(name = 0x%x)", name
);
1622 if (bufObj
->Purgeable
) {
1623 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1624 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1625 return GL_VOLATILE_APPLE
;
1628 bufObj
->Purgeable
= GL_TRUE
;
1630 retval
= GL_VOLATILE_APPLE
;
1631 if (ctx
->Driver
.TextureObjectPurgeable
)
1632 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1639 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1643 GET_CURRENT_CONTEXT(ctx
);
1644 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1647 _mesa_error(ctx
, GL_INVALID_VALUE
,
1648 "glObjectPurgeable(name = 0x%x)", name
);
1653 case GL_VOLATILE_APPLE
:
1654 case GL_RELEASED_APPLE
:
1658 _mesa_error(ctx
, GL_INVALID_ENUM
,
1659 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1664 switch (objectType
) {
1666 retval
= texture_object_purgeable(ctx
, name
, option
);
1668 case GL_RENDERBUFFER_EXT
:
1669 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1671 case GL_BUFFER_OBJECT_APPLE
:
1672 retval
= buffer_object_purgeable(ctx
, name
, option
);
1675 _mesa_error(ctx
, GL_INVALID_ENUM
,
1676 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1681 /* In strict conformance to the spec, we must only return VOLATILE when
1682 * when passed the VOLATILE option. Madness.
1684 * XXX First fix the spec, then fix me.
1686 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1691 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1693 struct gl_buffer_object
*bufObj
;
1696 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1698 _mesa_error(ctx
, GL_INVALID_VALUE
,
1699 "glObjectUnpurgeable(name = 0x%x)", name
);
1703 if (! bufObj
->Purgeable
) {
1704 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1705 "glObjectUnpurgeable(name = 0x%x) object is "
1706 " already \"unpurged\"", name
);
1710 bufObj
->Purgeable
= GL_FALSE
;
1713 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1714 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1721 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1723 struct gl_renderbuffer
*bufObj
;
1726 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1728 _mesa_error(ctx
, GL_INVALID_VALUE
,
1729 "glObjectUnpurgeable(name = 0x%x)", name
);
1733 if (! bufObj
->Purgeable
) {
1734 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1735 "glObjectUnpurgeable(name = 0x%x) object is "
1736 " already \"unpurged\"", name
);
1740 bufObj
->Purgeable
= GL_FALSE
;
1743 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1744 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1751 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1753 struct gl_texture_object
*bufObj
;
1756 bufObj
= _mesa_lookup_texture(ctx
, name
);
1758 _mesa_error(ctx
, GL_INVALID_VALUE
,
1759 "glObjectUnpurgeable(name = 0x%x)", name
);
1763 if (! bufObj
->Purgeable
) {
1764 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1765 "glObjectUnpurgeable(name = 0x%x) object is"
1766 " already \"unpurged\"", name
);
1770 bufObj
->Purgeable
= GL_FALSE
;
1773 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1774 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1781 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1783 GET_CURRENT_CONTEXT(ctx
);
1784 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1787 _mesa_error(ctx
, GL_INVALID_VALUE
,
1788 "glObjectUnpurgeable(name = 0x%x)", name
);
1793 case GL_RETAINED_APPLE
:
1794 case GL_UNDEFINED_APPLE
:
1798 _mesa_error(ctx
, GL_INVALID_ENUM
,
1799 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1804 switch (objectType
) {
1805 case GL_BUFFER_OBJECT_APPLE
:
1806 return buffer_object_unpurgeable(ctx
, name
, option
);
1808 return texture_object_unpurgeable(ctx
, name
, option
);
1809 case GL_RENDERBUFFER_EXT
:
1810 return renderbuffer_unpurgeable(ctx
, name
, option
);
1812 _mesa_error(ctx
, GL_INVALID_ENUM
,
1813 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1821 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1822 GLenum pname
, GLint
*params
)
1824 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1826 _mesa_error(ctx
, GL_INVALID_VALUE
,
1827 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1832 case GL_PURGEABLE_APPLE
:
1833 *params
= bufObj
->Purgeable
;
1836 _mesa_error(ctx
, GL_INVALID_ENUM
,
1837 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1845 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1846 GLenum pname
, GLint
*params
)
1848 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1850 _mesa_error(ctx
, GL_INVALID_VALUE
,
1851 "glObjectUnpurgeable(name = 0x%x)", name
);
1856 case GL_PURGEABLE_APPLE
:
1857 *params
= rb
->Purgeable
;
1860 _mesa_error(ctx
, GL_INVALID_ENUM
,
1861 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1869 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1870 GLenum pname
, GLint
*params
)
1872 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1874 _mesa_error(ctx
, GL_INVALID_VALUE
,
1875 "glObjectUnpurgeable(name = 0x%x)", name
);
1880 case GL_PURGEABLE_APPLE
:
1881 *params
= texObj
->Purgeable
;
1884 _mesa_error(ctx
, GL_INVALID_ENUM
,
1885 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1893 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
1896 GET_CURRENT_CONTEXT(ctx
);
1899 _mesa_error(ctx
, GL_INVALID_VALUE
,
1900 "glGetObjectParameteriv(name = 0x%x)", name
);
1904 switch (objectType
) {
1906 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
1908 case GL_BUFFER_OBJECT_APPLE
:
1909 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
1911 case GL_RENDERBUFFER_EXT
:
1912 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
1915 _mesa_error(ctx
, GL_INVALID_ENUM
,
1916 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1921 #endif /* FEATURE_APPLE_object_purgeable */