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
,
423 GLenum target
, GLintptrARB offset
,
424 GLsizeiptrARB size
, GLvoid
* data
,
425 struct gl_buffer_object
* bufObj
)
427 (void) ctx
; (void) target
;
429 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
430 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
436 * Default callback for \c dd_function_tabel::MapBuffer().
438 * The function parameters will have been already tested for errors.
440 * \param ctx GL context.
441 * \param target Buffer object target on which to operate.
442 * \param access Information about how the buffer will be accessed.
443 * \param bufObj Object to be mapped.
444 * \return A pointer to the object's internal data store that can be accessed
447 * \sa glMapBufferARB, dd_function_table::MapBuffer
450 _mesa_buffer_map( struct gl_context
*ctx
, GLenum access
,
451 struct gl_buffer_object
*bufObj
)
455 /* Just return a direct pointer to the data */
456 if (_mesa_bufferobj_mapped(bufObj
)) {
457 /* already mapped! */
460 bufObj
->Pointer
= bufObj
->Data
;
461 bufObj
->Length
= bufObj
->Size
;
463 return bufObj
->Pointer
;
468 * Default fallback for \c dd_function_table::MapBufferRange().
469 * Called via glMapBufferRange().
472 _mesa_buffer_map_range( struct gl_context
*ctx
, GLenum target
, GLintptr offset
,
473 GLsizeiptr length
, GLbitfield access
,
474 struct gl_buffer_object
*bufObj
)
478 assert(!_mesa_bufferobj_mapped(bufObj
));
479 /* Just return a direct pointer to the data */
480 bufObj
->Pointer
= bufObj
->Data
+ offset
;
481 bufObj
->Length
= length
;
482 bufObj
->Offset
= offset
;
483 bufObj
->AccessFlags
= access
;
484 return bufObj
->Pointer
;
489 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
490 * Called via glFlushMappedBufferRange().
493 _mesa_buffer_flush_mapped_range( struct gl_context
*ctx
, GLenum target
,
494 GLintptr offset
, GLsizeiptr length
,
495 struct gl_buffer_object
*obj
)
507 * Default callback for \c dd_function_table::MapBuffer().
509 * The input parameters will have been already tested for errors.
511 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
514 _mesa_buffer_unmap( struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
517 /* XXX we might assert here that bufObj->Pointer is non-null */
518 bufObj
->Pointer
= NULL
;
521 bufObj
->AccessFlags
= 0x0;
527 * Default fallback for \c dd_function_table::CopyBufferSubData().
528 * Called via glCopyBuffserSubData().
531 _mesa_copy_buffer_subdata(struct gl_context
*ctx
,
532 struct gl_buffer_object
*src
,
533 struct gl_buffer_object
*dst
,
534 GLintptr readOffset
, GLintptr writeOffset
,
537 GLubyte
*srcPtr
, *dstPtr
;
539 /* buffer should not already be mapped */
540 assert(!_mesa_bufferobj_mapped(src
));
541 assert(!_mesa_bufferobj_mapped(dst
));
543 srcPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_READ_ONLY
, src
);
544 dstPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_WRITE_ONLY
, dst
);
546 if (srcPtr
&& dstPtr
)
547 memcpy(dstPtr
+ writeOffset
, srcPtr
+ readOffset
, size
);
549 ctx
->Driver
.UnmapBuffer(ctx
, src
);
550 ctx
->Driver
.UnmapBuffer(ctx
, dst
);
556 * Initialize the state associated with buffer objects
559 _mesa_init_buffer_objects( struct gl_context
*ctx
)
561 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
562 _glthread_INIT_MUTEX(DummyBufferObject
.Mutex
);
563 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
565 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
566 ctx
->Shared
->NullBufferObj
);
567 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
,
568 ctx
->Shared
->NullBufferObj
);
570 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
571 ctx
->Shared
->NullBufferObj
);
572 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
573 ctx
->Shared
->NullBufferObj
);
578 _mesa_free_buffer_objects( struct gl_context
*ctx
)
580 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
581 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
, NULL
);
583 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
584 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
589 * Bind the specified target to buffer for the specified context.
590 * Called by glBindBuffer() and other functions.
593 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
595 struct gl_buffer_object
*oldBufObj
;
596 struct gl_buffer_object
*newBufObj
= NULL
;
597 struct gl_buffer_object
**bindTarget
= NULL
;
599 bindTarget
= get_buffer_target(ctx
, target
);
601 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
605 /* Get pointer to old buffer object (to be unbound) */
606 oldBufObj
= *bindTarget
;
607 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
608 return; /* rebinding the same buffer object- no change */
611 * Get pointer to new buffer object (newBufObj)
614 /* The spec says there's not a buffer object named 0, but we use
615 * one internally because it simplifies things.
617 newBufObj
= ctx
->Shared
->NullBufferObj
;
620 /* non-default buffer object */
621 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
622 if (!newBufObj
|| newBufObj
== &DummyBufferObject
) {
623 /* If this is a new buffer object id, or one which was generated but
624 * never used before, allocate a buffer object now.
626 ASSERT(ctx
->Driver
.NewBufferObject
);
627 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
629 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
632 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
636 /* bind new buffer */
637 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
639 /* Pass BindBuffer call to device driver */
640 if (ctx
->Driver
.BindBuffer
)
641 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
646 * Update the default buffer objects in the given context to reference those
647 * specified in the shared state and release those referencing the old
651 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
653 /* Bind the NullBufferObj to remove references to those
654 * in the shared context hash table.
656 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
657 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
658 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
659 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
665 * Return the gl_buffer_object for the given ID.
666 * Always return NULL for ID 0.
668 struct gl_buffer_object
*
669 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
674 return (struct gl_buffer_object
*)
675 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
680 * If *ptr points to obj, set ptr = the Null/default buffer object.
681 * This is a helper for buffer object deletion.
682 * The GL spec says that deleting a buffer object causes it to get
683 * unbound from all arrays in the current context.
686 unbind(struct gl_context
*ctx
,
687 struct gl_buffer_object
**ptr
,
688 struct gl_buffer_object
*obj
)
691 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
697 * Plug default/fallback buffer object functions into the device
701 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
703 /* GL_ARB_vertex/pixel_buffer_object */
704 driver
->NewBufferObject
= _mesa_new_buffer_object
;
705 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
706 driver
->BindBuffer
= NULL
;
707 driver
->BufferData
= _mesa_buffer_data
;
708 driver
->BufferSubData
= _mesa_buffer_subdata
;
709 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
710 driver
->MapBuffer
= _mesa_buffer_map
;
711 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
713 /* GL_ARB_map_buffer_range */
714 driver
->MapBufferRange
= _mesa_buffer_map_range
;
715 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
717 /* GL_ARB_copy_buffer */
718 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
723 /**********************************************************************/
725 /**********************************************************************/
728 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
730 GET_CURRENT_CONTEXT(ctx
);
731 ASSERT_OUTSIDE_BEGIN_END(ctx
);
733 if (MESA_VERBOSE
& VERBOSE_API
)
734 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
735 _mesa_lookup_enum_by_nr(target
), buffer
);
737 bind_buffer_object(ctx
, target
, buffer
);
742 * Delete a set of buffer objects.
744 * \param n Number of buffer objects to delete.
745 * \param ids Array of \c n buffer object IDs.
748 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
750 GET_CURRENT_CONTEXT(ctx
);
752 ASSERT_OUTSIDE_BEGIN_END(ctx
);
753 FLUSH_VERTICES(ctx
, 0);
756 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
760 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
762 for (i
= 0; i
< n
; i
++) {
763 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
765 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
768 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
770 if (_mesa_bufferobj_mapped(bufObj
)) {
771 /* if mapped, unmap it now */
772 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
773 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
774 bufObj
->Pointer
= NULL
;
777 /* unbind any vertex pointers bound to this buffer */
778 unbind(ctx
, &arrayObj
->Vertex
.BufferObj
, bufObj
);
779 unbind(ctx
, &arrayObj
->Weight
.BufferObj
, bufObj
);
780 unbind(ctx
, &arrayObj
->Normal
.BufferObj
, bufObj
);
781 unbind(ctx
, &arrayObj
->Color
.BufferObj
, bufObj
);
782 unbind(ctx
, &arrayObj
->SecondaryColor
.BufferObj
, bufObj
);
783 unbind(ctx
, &arrayObj
->FogCoord
.BufferObj
, bufObj
);
784 unbind(ctx
, &arrayObj
->Index
.BufferObj
, bufObj
);
785 unbind(ctx
, &arrayObj
->EdgeFlag
.BufferObj
, bufObj
);
786 for (j
= 0; j
< Elements(arrayObj
->TexCoord
); j
++) {
787 unbind(ctx
, &arrayObj
->TexCoord
[j
].BufferObj
, bufObj
);
789 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
790 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
793 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
794 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
796 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
797 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
800 /* unbind any pixel pack/unpack pointers bound to this buffer */
801 if (ctx
->Pack
.BufferObj
== bufObj
) {
802 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
804 if (ctx
->Unpack
.BufferObj
== bufObj
) {
805 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
808 /* The ID is immediately freed for re-use */
809 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
810 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
814 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
819 * Generate a set of unique buffer object IDs and store them in \c buffer.
821 * \param n Number of IDs to generate.
822 * \param buffer Array of \c n locations to store the IDs.
825 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
827 GET_CURRENT_CONTEXT(ctx
);
830 ASSERT_OUTSIDE_BEGIN_END(ctx
);
832 if (MESA_VERBOSE
& VERBOSE_API
)
833 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
836 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
845 * This must be atomic (generation and allocation of buffer object IDs)
847 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
849 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
851 /* Insert the ID and pointer to dummy buffer object into hash table */
852 for (i
= 0; i
< n
; i
++) {
853 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
855 buffer
[i
] = first
+ i
;
858 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
863 * Determine if ID is the name of a buffer object.
865 * \param id ID of the potential buffer object.
866 * \return \c GL_TRUE if \c id is the name of a buffer object,
867 * \c GL_FALSE otherwise.
870 _mesa_IsBufferARB(GLuint id
)
872 struct gl_buffer_object
*bufObj
;
873 GET_CURRENT_CONTEXT(ctx
);
874 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
876 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
877 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
878 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
880 return bufObj
&& bufObj
!= &DummyBufferObject
;
885 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
886 const GLvoid
* data
, GLenum usage
)
888 GET_CURRENT_CONTEXT(ctx
);
889 struct gl_buffer_object
*bufObj
;
890 ASSERT_OUTSIDE_BEGIN_END(ctx
);
892 if (MESA_VERBOSE
& VERBOSE_API
)
893 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
894 _mesa_lookup_enum_by_nr(target
),
895 (long int) size
, data
,
896 _mesa_lookup_enum_by_nr(usage
));
899 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
904 case GL_STREAM_DRAW_ARB
:
905 case GL_STREAM_READ_ARB
:
906 case GL_STREAM_COPY_ARB
:
907 case GL_STATIC_DRAW_ARB
:
908 case GL_STATIC_READ_ARB
:
909 case GL_STATIC_COPY_ARB
:
910 case GL_DYNAMIC_DRAW_ARB
:
911 case GL_DYNAMIC_READ_ARB
:
912 case GL_DYNAMIC_COPY_ARB
:
916 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
920 bufObj
= get_buffer(ctx
, target
);
922 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
925 if (!_mesa_is_bufferobj(bufObj
)) {
926 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer 0)" );
930 if (_mesa_bufferobj_mapped(bufObj
)) {
931 /* Unmap the existing buffer. We'll replace it now. Not an error. */
932 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
933 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
934 ASSERT(bufObj
->Pointer
== NULL
);
937 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
939 bufObj
->Written
= GL_TRUE
;
942 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
943 bufObj
->Name
, size
, data
, usage
);
950 ASSERT(ctx
->Driver
.BufferData
);
951 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
952 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
958 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
959 GLsizeiptrARB size
, const GLvoid
* data
)
961 GET_CURRENT_CONTEXT(ctx
);
962 struct gl_buffer_object
*bufObj
;
963 ASSERT_OUTSIDE_BEGIN_END(ctx
);
965 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
966 "glBufferSubDataARB" );
968 /* error already recorded */
975 bufObj
->Written
= GL_TRUE
;
977 ASSERT(ctx
->Driver
.BufferSubData
);
978 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
983 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
984 GLsizeiptrARB size
, void * data
)
986 GET_CURRENT_CONTEXT(ctx
);
987 struct gl_buffer_object
*bufObj
;
988 ASSERT_OUTSIDE_BEGIN_END(ctx
);
990 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
991 "glGetBufferSubDataARB" );
993 /* error already recorded */
997 ASSERT(ctx
->Driver
.GetBufferSubData
);
998 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1003 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1005 GET_CURRENT_CONTEXT(ctx
);
1006 struct gl_buffer_object
* bufObj
;
1007 GLbitfield accessFlags
;
1010 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1013 case GL_READ_ONLY_ARB
:
1014 accessFlags
= GL_MAP_READ_BIT
;
1016 case GL_WRITE_ONLY_ARB
:
1017 accessFlags
= GL_MAP_WRITE_BIT
;
1019 case GL_READ_WRITE_ARB
:
1020 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1023 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1027 bufObj
= get_buffer(ctx
, target
);
1029 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
1032 if (!_mesa_is_bufferobj(bufObj
)) {
1033 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(buffer 0)" );
1036 if (_mesa_bufferobj_mapped(bufObj
)) {
1037 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1041 ASSERT(ctx
->Driver
.MapBuffer
);
1042 map
= ctx
->Driver
.MapBuffer( ctx
, access
, 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
, target
);
1097 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1100 if (!_mesa_is_bufferobj(bufObj
)) {
1101 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1104 if (!_mesa_bufferobj_mapped(bufObj
)) {
1105 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1110 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1111 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1113 /* check that last 100 bytes are still = magic value */
1114 for (i
= 0; i
< 100; i
++) {
1115 GLuint pos
= bufObj
->Size
- i
- 1;
1116 if (buf
[pos
] != 123) {
1117 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1118 " at position %d (value = %u)\n",
1126 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1127 GLuint i
, unchanged
= 0;
1128 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1130 /* check which bytes changed */
1131 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1132 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1139 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1140 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1145 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1146 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1147 ASSERT(bufObj
->Pointer
== NULL
);
1148 ASSERT(bufObj
->Offset
== 0);
1149 ASSERT(bufObj
->Length
== 0);
1156 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1158 GET_CURRENT_CONTEXT(ctx
);
1159 struct gl_buffer_object
*bufObj
;
1160 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1162 bufObj
= get_buffer(ctx
, target
);
1164 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(target)" );
1167 if (!_mesa_is_bufferobj(bufObj
)) {
1168 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferParameterivARB" );
1173 case GL_BUFFER_SIZE_ARB
:
1174 *params
= (GLint
) bufObj
->Size
;
1176 case GL_BUFFER_USAGE_ARB
:
1177 *params
= bufObj
->Usage
;
1179 case GL_BUFFER_ACCESS_ARB
:
1180 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1182 case GL_BUFFER_MAPPED_ARB
:
1183 *params
= _mesa_bufferobj_mapped(bufObj
);
1185 case GL_BUFFER_ACCESS_FLAGS
:
1186 if (ctx
->VersionMajor
< 3)
1188 *params
= bufObj
->AccessFlags
;
1190 case GL_BUFFER_MAP_OFFSET
:
1191 if (ctx
->VersionMajor
< 3)
1193 *params
= (GLint
) bufObj
->Offset
;
1195 case GL_BUFFER_MAP_LENGTH
:
1196 if (ctx
->VersionMajor
< 3)
1198 *params
= (GLint
) bufObj
->Length
;
1201 ; /* fall-through */
1205 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1206 _mesa_lookup_enum_by_nr(pname
));
1212 * This is pretty much a duplicate of GetBufferParameteriv() but the
1213 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1216 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1218 GET_CURRENT_CONTEXT(ctx
);
1219 struct gl_buffer_object
*bufObj
;
1220 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1222 bufObj
= get_buffer(ctx
, target
);
1224 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(target)" );
1227 if (!_mesa_is_bufferobj(bufObj
)) {
1228 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferParameteri64v" );
1233 case GL_BUFFER_SIZE_ARB
:
1234 *params
= bufObj
->Size
;
1236 case GL_BUFFER_USAGE_ARB
:
1237 *params
= bufObj
->Usage
;
1239 case GL_BUFFER_ACCESS_ARB
:
1240 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1242 case GL_BUFFER_ACCESS_FLAGS
:
1243 if (ctx
->VersionMajor
< 3)
1245 *params
= bufObj
->AccessFlags
;
1247 case GL_BUFFER_MAPPED_ARB
:
1248 *params
= _mesa_bufferobj_mapped(bufObj
);
1250 case GL_BUFFER_MAP_OFFSET
:
1251 if (ctx
->VersionMajor
< 3)
1253 *params
= bufObj
->Offset
;
1255 case GL_BUFFER_MAP_LENGTH
:
1256 if (ctx
->VersionMajor
< 3)
1258 *params
= bufObj
->Length
;
1261 ; /* fall-through */
1265 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1266 _mesa_lookup_enum_by_nr(pname
));
1271 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1273 GET_CURRENT_CONTEXT(ctx
);
1274 struct gl_buffer_object
* bufObj
;
1275 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1277 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1278 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1282 bufObj
= get_buffer(ctx
, target
);
1284 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1287 if (!_mesa_is_bufferobj(bufObj
)) {
1288 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1292 *params
= bufObj
->Pointer
;
1297 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1298 GLintptr readOffset
, GLintptr writeOffset
,
1301 GET_CURRENT_CONTEXT(ctx
);
1302 struct gl_buffer_object
*src
, *dst
;
1303 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1305 src
= get_buffer(ctx
, readTarget
);
1306 if (!src
|| !_mesa_is_bufferobj(src
)) {
1307 _mesa_error(ctx
, GL_INVALID_ENUM
,
1308 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget
);
1312 dst
= get_buffer(ctx
, writeTarget
);
1313 if (!dst
|| !_mesa_is_bufferobj(dst
)) {
1314 _mesa_error(ctx
, GL_INVALID_ENUM
,
1315 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget
);
1319 if (_mesa_bufferobj_mapped(src
)) {
1320 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1321 "glCopyBuffserSubData(readBuffer is mapped)");
1325 if (_mesa_bufferobj_mapped(dst
)) {
1326 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1327 "glCopyBuffserSubData(writeBuffer is mapped)");
1331 if (readOffset
< 0) {
1332 _mesa_error(ctx
, GL_INVALID_VALUE
,
1333 "glCopyBuffserSubData(readOffset = %d)", (int) readOffset
);
1337 if (writeOffset
< 0) {
1338 _mesa_error(ctx
, GL_INVALID_VALUE
,
1339 "glCopyBuffserSubData(writeOffset = %d)", (int) writeOffset
);
1343 if (readOffset
+ size
> src
->Size
) {
1344 _mesa_error(ctx
, GL_INVALID_VALUE
,
1345 "glCopyBuffserSubData(readOffset + size = %d)",
1346 (int) (readOffset
+ size
));
1350 if (writeOffset
+ size
> dst
->Size
) {
1351 _mesa_error(ctx
, GL_INVALID_VALUE
,
1352 "glCopyBuffserSubData(writeOffset + size = %d)",
1353 (int) (writeOffset
+ size
));
1358 if (readOffset
+ size
<= writeOffset
) {
1361 else if (writeOffset
+ size
<= readOffset
) {
1365 /* overlapping src/dst is illegal */
1366 _mesa_error(ctx
, GL_INVALID_VALUE
,
1367 "glCopyBuffserSubData(overlapping src/dst)");
1372 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1377 * See GL_ARB_map_buffer_range spec
1380 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1383 GET_CURRENT_CONTEXT(ctx
);
1384 struct gl_buffer_object
*bufObj
;
1387 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1389 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1390 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1391 "glMapBufferRange(extension not supported)");
1396 _mesa_error(ctx
, GL_INVALID_VALUE
,
1397 "glMapBufferRange(offset = %ld)", (long)offset
);
1402 _mesa_error(ctx
, GL_INVALID_VALUE
,
1403 "glMapBufferRange(length = %ld)", (long)length
);
1407 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1408 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1409 "glMapBufferRange(access indicates neither read or write)");
1413 if ((access
& GL_MAP_READ_BIT
) &&
1414 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1415 GL_MAP_INVALIDATE_BUFFER_BIT
|
1416 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1417 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1418 "glMapBufferRange(invalid access flags)");
1422 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1423 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1424 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1425 "glMapBufferRange(invalid access flags)");
1429 bufObj
= get_buffer(ctx
, target
);
1430 if (!bufObj
|| !_mesa_is_bufferobj(bufObj
)) {
1431 _mesa_error(ctx
, GL_INVALID_ENUM
,
1432 "glMapBufferRange(target = 0x%x)", target
);
1436 if (offset
+ length
> bufObj
->Size
) {
1437 _mesa_error(ctx
, GL_INVALID_VALUE
,
1438 "glMapBufferRange(offset + length > size)");
1442 if (_mesa_bufferobj_mapped(bufObj
)) {
1443 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1444 "glMapBufferRange(buffer already mapped)");
1448 ASSERT(ctx
->Driver
.MapBufferRange
);
1449 map
= ctx
->Driver
.MapBufferRange(ctx
, target
, offset
, length
,
1452 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1455 /* The driver callback should have set all these fields.
1456 * This is important because other modules (like VBO) might call
1457 * the driver function directly.
1459 ASSERT(bufObj
->Pointer
== map
);
1460 ASSERT(bufObj
->Length
== length
);
1461 ASSERT(bufObj
->Offset
== offset
);
1462 ASSERT(bufObj
->AccessFlags
== access
);
1470 * See GL_ARB_map_buffer_range spec
1473 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1475 GET_CURRENT_CONTEXT(ctx
);
1476 struct gl_buffer_object
*bufObj
;
1477 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1479 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1480 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1481 "glMapBufferRange(extension not supported)");
1486 _mesa_error(ctx
, GL_INVALID_VALUE
,
1487 "glMapBufferRange(offset = %ld)", (long)offset
);
1492 _mesa_error(ctx
, GL_INVALID_VALUE
,
1493 "glMapBufferRange(length = %ld)", (long)length
);
1497 bufObj
= get_buffer(ctx
, target
);
1499 _mesa_error(ctx
, GL_INVALID_ENUM
,
1500 "glMapBufferRange(target = 0x%x)", target
);
1504 if (!_mesa_is_bufferobj(bufObj
)) {
1505 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1506 "glMapBufferRange(current buffer is 0)");
1510 if (!_mesa_bufferobj_mapped(bufObj
)) {
1511 /* buffer is not mapped */
1512 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1513 "glMapBufferRange(buffer is not mapped)");
1517 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1518 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1519 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1523 if (offset
+ length
> bufObj
->Length
) {
1524 _mesa_error(ctx
, GL_INVALID_VALUE
,
1525 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1526 (long)offset
, (long)length
, (long)bufObj
->Length
);
1530 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1532 if (ctx
->Driver
.FlushMappedBufferRange
)
1533 ctx
->Driver
.FlushMappedBufferRange(ctx
, target
, offset
, length
, bufObj
);
1537 #if FEATURE_APPLE_object_purgeable
1539 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1541 struct gl_buffer_object
*bufObj
;
1544 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1546 _mesa_error(ctx
, GL_INVALID_VALUE
,
1547 "glObjectPurgeable(name = 0x%x)", name
);
1550 if (!_mesa_is_bufferobj(bufObj
)) {
1551 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1555 if (bufObj
->Purgeable
) {
1556 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1557 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1558 return GL_VOLATILE_APPLE
;
1561 bufObj
->Purgeable
= GL_TRUE
;
1563 retval
= GL_VOLATILE_APPLE
;
1564 if (ctx
->Driver
.BufferObjectPurgeable
)
1565 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1572 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1574 struct gl_renderbuffer
*bufObj
;
1577 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1579 _mesa_error(ctx
, GL_INVALID_VALUE
,
1580 "glObjectUnpurgeable(name = 0x%x)", name
);
1584 if (bufObj
->Purgeable
) {
1585 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1586 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1587 return GL_VOLATILE_APPLE
;
1590 bufObj
->Purgeable
= GL_TRUE
;
1592 retval
= GL_VOLATILE_APPLE
;
1593 if (ctx
->Driver
.RenderObjectPurgeable
)
1594 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1601 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1603 struct gl_texture_object
*bufObj
;
1606 bufObj
= _mesa_lookup_texture(ctx
, name
);
1608 _mesa_error(ctx
, GL_INVALID_VALUE
,
1609 "glObjectPurgeable(name = 0x%x)", name
);
1613 if (bufObj
->Purgeable
) {
1614 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1615 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1616 return GL_VOLATILE_APPLE
;
1619 bufObj
->Purgeable
= GL_TRUE
;
1621 retval
= GL_VOLATILE_APPLE
;
1622 if (ctx
->Driver
.TextureObjectPurgeable
)
1623 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1630 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1634 GET_CURRENT_CONTEXT(ctx
);
1635 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1638 _mesa_error(ctx
, GL_INVALID_VALUE
,
1639 "glObjectPurgeable(name = 0x%x)", name
);
1644 case GL_VOLATILE_APPLE
:
1645 case GL_RELEASED_APPLE
:
1649 _mesa_error(ctx
, GL_INVALID_ENUM
,
1650 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1655 switch (objectType
) {
1657 retval
= texture_object_purgeable(ctx
, name
, option
);
1659 case GL_RENDERBUFFER_EXT
:
1660 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1662 case GL_BUFFER_OBJECT_APPLE
:
1663 retval
= buffer_object_purgeable(ctx
, name
, option
);
1666 _mesa_error(ctx
, GL_INVALID_ENUM
,
1667 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1672 /* In strict conformance to the spec, we must only return VOLATILE when
1673 * when passed the VOLATILE option. Madness.
1675 * XXX First fix the spec, then fix me.
1677 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1682 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1684 struct gl_buffer_object
*bufObj
;
1687 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1689 _mesa_error(ctx
, GL_INVALID_VALUE
,
1690 "glObjectUnpurgeable(name = 0x%x)", name
);
1694 if (! bufObj
->Purgeable
) {
1695 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1696 "glObjectUnpurgeable(name = 0x%x) object is "
1697 " already \"unpurged\"", name
);
1701 bufObj
->Purgeable
= GL_FALSE
;
1704 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1705 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1712 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1714 struct gl_renderbuffer
*bufObj
;
1717 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1719 _mesa_error(ctx
, GL_INVALID_VALUE
,
1720 "glObjectUnpurgeable(name = 0x%x)", name
);
1724 if (! bufObj
->Purgeable
) {
1725 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1726 "glObjectUnpurgeable(name = 0x%x) object is "
1727 " already \"unpurged\"", name
);
1731 bufObj
->Purgeable
= GL_FALSE
;
1734 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1735 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1742 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1744 struct gl_texture_object
*bufObj
;
1747 bufObj
= _mesa_lookup_texture(ctx
, name
);
1749 _mesa_error(ctx
, GL_INVALID_VALUE
,
1750 "glObjectUnpurgeable(name = 0x%x)", name
);
1754 if (! bufObj
->Purgeable
) {
1755 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1756 "glObjectUnpurgeable(name = 0x%x) object is"
1757 " already \"unpurged\"", name
);
1761 bufObj
->Purgeable
= GL_FALSE
;
1764 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1765 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1772 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1774 GET_CURRENT_CONTEXT(ctx
);
1775 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1778 _mesa_error(ctx
, GL_INVALID_VALUE
,
1779 "glObjectUnpurgeable(name = 0x%x)", name
);
1784 case GL_RETAINED_APPLE
:
1785 case GL_UNDEFINED_APPLE
:
1789 _mesa_error(ctx
, GL_INVALID_ENUM
,
1790 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1795 switch (objectType
) {
1796 case GL_BUFFER_OBJECT_APPLE
:
1797 return buffer_object_unpurgeable(ctx
, name
, option
);
1799 return texture_object_unpurgeable(ctx
, name
, option
);
1800 case GL_RENDERBUFFER_EXT
:
1801 return renderbuffer_unpurgeable(ctx
, name
, option
);
1803 _mesa_error(ctx
, GL_INVALID_ENUM
,
1804 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1812 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1813 GLenum pname
, GLint
*params
)
1815 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1817 _mesa_error(ctx
, GL_INVALID_VALUE
,
1818 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1823 case GL_PURGEABLE_APPLE
:
1824 *params
= bufObj
->Purgeable
;
1827 _mesa_error(ctx
, GL_INVALID_ENUM
,
1828 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1836 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1837 GLenum pname
, GLint
*params
)
1839 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1841 _mesa_error(ctx
, GL_INVALID_VALUE
,
1842 "glObjectUnpurgeable(name = 0x%x)", name
);
1847 case GL_PURGEABLE_APPLE
:
1848 *params
= rb
->Purgeable
;
1851 _mesa_error(ctx
, GL_INVALID_ENUM
,
1852 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1860 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1861 GLenum pname
, GLint
*params
)
1863 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1865 _mesa_error(ctx
, GL_INVALID_VALUE
,
1866 "glObjectUnpurgeable(name = 0x%x)", name
);
1871 case GL_PURGEABLE_APPLE
:
1872 *params
= texObj
->Purgeable
;
1875 _mesa_error(ctx
, GL_INVALID_ENUM
,
1876 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1884 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
1887 GET_CURRENT_CONTEXT(ctx
);
1890 _mesa_error(ctx
, GL_INVALID_VALUE
,
1891 "glGetObjectParameteriv(name = 0x%x)", name
);
1895 switch (objectType
) {
1897 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
1899 case GL_BUFFER_OBJECT_APPLE
:
1900 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
1902 case GL_RENDERBUFFER_EXT
:
1903 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
1906 _mesa_error(ctx
, GL_INVALID_ENUM
,
1907 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1912 #endif /* FEATURE_APPLE_object_purgeable */