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
;
95 case GL_UNIFORM_BUFFER
:
96 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
97 return &ctx
->UniformBuffer
;
108 * Get the buffer object bound to the specified target in a GL context.
109 * \param ctx the GL context
110 * \param target the buffer object target to be retrieved.
111 * \return pointer to the buffer object bound to \c target in the
112 * specified context or \c NULL if \c target is invalid.
114 static inline struct gl_buffer_object
*
115 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
)
117 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
120 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
124 if (!_mesa_is_bufferobj(*bufObj
)) {
125 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(buffer 0)", func
);
134 default_access_mode(const struct gl_context
*ctx
)
136 /* Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
138 * Name Type Initial Value Legal Values
140 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
143 * However, table 6.8 in the GL_OES_mapbuffer extension says:
145 * Get Value Type Get Command Value Description
146 * --------- ---- ----------- ----- -----------
147 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
149 * The difference is because GL_OES_mapbuffer only supports mapping buffers
152 return (ctx
->API
== API_OPENGLES
)
153 ? GL_MAP_WRITE_BIT
: (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
);
158 * Convert a GLbitfield describing the mapped buffer access flags
159 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
162 simplified_access_mode(GLbitfield access
)
164 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
165 if ((access
& rwFlags
) == rwFlags
)
166 return GL_READ_WRITE
;
167 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
169 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
170 return GL_WRITE_ONLY
;
171 return GL_READ_WRITE
; /* this should never happen, but no big deal */
176 * Tests the subdata range parameters and sets the GL error code for
177 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
179 * \param ctx GL context.
180 * \param target Buffer object target on which to operate.
181 * \param offset Offset of the first byte of the subdata range.
182 * \param size Size, in bytes, of the subdata range.
183 * \param caller Name of calling function for recording errors.
184 * \return A pointer to the buffer object bound to \c target in the
185 * specified context or \c NULL if any of the parameter or state
186 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
189 * \sa glBufferSubDataARB, glGetBufferSubDataARB
191 static struct gl_buffer_object
*
192 buffer_object_subdata_range_good( struct gl_context
* ctx
, GLenum target
,
193 GLintptrARB offset
, GLsizeiptrARB size
,
196 struct gl_buffer_object
*bufObj
;
199 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
204 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
208 bufObj
= get_buffer(ctx
, caller
, target
);
212 if (offset
+ size
> bufObj
->Size
) {
213 _mesa_error(ctx
, GL_INVALID_VALUE
,
214 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
215 (unsigned long) offset
,
216 (unsigned long) size
,
217 (unsigned long) bufObj
->Size
);
220 if (_mesa_bufferobj_mapped(bufObj
)) {
221 /* Buffer is currently mapped */
222 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
231 * Allocate and initialize a new buffer object.
233 * Default callback for the \c dd_function_table::NewBufferObject() hook.
235 static struct gl_buffer_object
*
236 _mesa_new_buffer_object( struct gl_context
*ctx
, GLuint name
, GLenum target
)
238 struct gl_buffer_object
*obj
;
242 obj
= MALLOC_STRUCT(gl_buffer_object
);
243 _mesa_initialize_buffer_object(ctx
, obj
, name
, target
);
249 * Delete a buffer object.
251 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
254 _mesa_delete_buffer_object(struct gl_context
*ctx
,
255 struct gl_buffer_object
*bufObj
)
262 /* assign strange values here to help w/ debugging */
263 bufObj
->RefCount
= -1000;
266 _glthread_DESTROY_MUTEX(bufObj
->Mutex
);
273 * Set ptr to bufObj w/ reference counting.
274 * This is normally only called from the _mesa_reference_buffer_object() macro
275 * when there's a real pointer change.
278 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
279 struct gl_buffer_object
**ptr
,
280 struct gl_buffer_object
*bufObj
)
283 /* Unreference the old buffer */
284 GLboolean deleteFlag
= GL_FALSE
;
285 struct gl_buffer_object
*oldObj
= *ptr
;
287 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
288 ASSERT(oldObj
->RefCount
> 0);
291 printf("BufferObj %p %d DECR to %d\n",
292 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
294 deleteFlag
= (oldObj
->RefCount
== 0);
295 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
299 /* some sanity checking: don't delete a buffer still in use */
301 /* unfortunately, these tests are invalid during context tear-down */
302 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
303 ASSERT(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
!= bufObj
);
304 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
307 ASSERT(ctx
->Driver
.DeleteBuffer
);
308 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
316 /* reference new buffer */
317 _glthread_LOCK_MUTEX(bufObj
->Mutex
);
318 if (bufObj
->RefCount
== 0) {
319 /* this buffer's being deleted (look just above) */
320 /* Not sure this can every really happen. Warn if it does. */
321 _mesa_problem(NULL
, "referencing deleted buffer object");
327 printf("BufferObj %p %d INCR to %d\n",
328 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
332 _glthread_UNLOCK_MUTEX(bufObj
->Mutex
);
338 * Initialize a buffer object to default values.
341 _mesa_initialize_buffer_object( struct gl_context
*ctx
,
342 struct gl_buffer_object
*obj
,
343 GLuint name
, GLenum target
)
347 memset(obj
, 0, sizeof(struct gl_buffer_object
));
348 _glthread_INIT_MUTEX(obj
->Mutex
);
351 obj
->Usage
= GL_STATIC_DRAW_ARB
;
352 obj
->AccessFlags
= default_access_mode(ctx
);
358 * Callback called from _mesa_HashWalk()
361 count_buffer_size(GLuint key
, void *data
, void *userData
)
363 const struct gl_buffer_object
*bufObj
=
364 (const struct gl_buffer_object
*) data
;
365 GLuint
*total
= (GLuint
*) userData
;
367 *total
= *total
+ bufObj
->Size
;
372 * Compute total size (in bytes) of all buffer objects for the given context.
373 * For debugging purposes.
376 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
380 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
387 * Allocate space for and store data in a buffer object. Any data that was
388 * previously stored in the buffer object is lost. If \c data is \c NULL,
389 * memory will be allocated, but no copy will occur.
391 * This is the default callback for \c dd_function_table::BufferData()
392 * Note that all GL error checking will have been done already.
394 * \param ctx GL context.
395 * \param target Buffer object target on which to operate.
396 * \param size Size, in bytes, of the new data store.
397 * \param data Pointer to the data to store in the buffer object. This
398 * pointer may be \c NULL.
399 * \param usage Hints about how the data will be used.
400 * \param bufObj Object to be used.
402 * \return GL_TRUE for success, GL_FALSE for failure
403 * \sa glBufferDataARB, dd_function_table::BufferData.
406 _mesa_buffer_data( struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
407 const GLvoid
* data
, GLenum usage
,
408 struct gl_buffer_object
* bufObj
)
412 (void) ctx
; (void) target
;
414 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
416 bufObj
->Data
= (GLubyte
*) new_data
;
418 bufObj
->Usage
= usage
;
421 memcpy( bufObj
->Data
, data
, size
);
433 * Replace data in a subrange of buffer object. If the data range
434 * specified by \c size + \c offset extends beyond the end of the buffer or
435 * if \c data is \c NULL, no copy is performed.
437 * This is the default callback for \c dd_function_table::BufferSubData()
438 * Note that all GL error checking will have been done already.
440 * \param ctx GL context.
441 * \param target Buffer object target on which to operate.
442 * \param offset Offset of the first byte to be modified.
443 * \param size Size, in bytes, of the data range.
444 * \param data Pointer to the data to store in the buffer object.
445 * \param bufObj Object to be used.
447 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
450 _mesa_buffer_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
451 GLsizeiptrARB size
, const GLvoid
* data
,
452 struct gl_buffer_object
* bufObj
)
456 /* this should have been caught in _mesa_BufferSubData() */
457 ASSERT(size
+ offset
<= bufObj
->Size
);
460 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
466 * Retrieve data from a subrange of buffer object. If the data range
467 * specified by \c size + \c offset extends beyond the end of the buffer or
468 * if \c data is \c NULL, no copy is performed.
470 * This is the default callback for \c dd_function_table::GetBufferSubData()
471 * Note that all GL error checking will have been done already.
473 * \param ctx GL context.
474 * \param target Buffer object target on which to operate.
475 * \param offset Offset of the first byte to be fetched.
476 * \param size Size, in bytes, of the data range.
477 * \param data Destination for data
478 * \param bufObj Object to be used.
480 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
483 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
484 GLsizeiptrARB size
, GLvoid
* data
,
485 struct gl_buffer_object
* bufObj
)
489 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
490 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
496 * Default fallback for \c dd_function_table::MapBufferRange().
497 * Called via glMapBufferRange().
500 _mesa_buffer_map_range( struct gl_context
*ctx
, GLintptr offset
,
501 GLsizeiptr length
, GLbitfield access
,
502 struct gl_buffer_object
*bufObj
)
505 assert(!_mesa_bufferobj_mapped(bufObj
));
506 /* Just return a direct pointer to the data */
507 bufObj
->Pointer
= bufObj
->Data
+ offset
;
508 bufObj
->Length
= length
;
509 bufObj
->Offset
= offset
;
510 bufObj
->AccessFlags
= access
;
511 return bufObj
->Pointer
;
516 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
517 * Called via glFlushMappedBufferRange().
520 _mesa_buffer_flush_mapped_range( struct gl_context
*ctx
,
521 GLintptr offset
, GLsizeiptr length
,
522 struct gl_buffer_object
*obj
)
533 * Default callback for \c dd_function_table::MapBuffer().
535 * The input parameters will have been already tested for errors.
537 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
540 _mesa_buffer_unmap( struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
543 /* XXX we might assert here that bufObj->Pointer is non-null */
544 bufObj
->Pointer
= NULL
;
547 bufObj
->AccessFlags
= 0x0;
553 * Default fallback for \c dd_function_table::CopyBufferSubData().
554 * Called via glCopyBufferSubData().
557 _mesa_copy_buffer_subdata(struct gl_context
*ctx
,
558 struct gl_buffer_object
*src
,
559 struct gl_buffer_object
*dst
,
560 GLintptr readOffset
, GLintptr writeOffset
,
563 GLubyte
*srcPtr
, *dstPtr
;
565 /* the buffers should not be mapped */
566 assert(!_mesa_bufferobj_mapped(src
));
567 assert(!_mesa_bufferobj_mapped(dst
));
570 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
572 GL_MAP_WRITE_BIT
, src
);
577 srcPtr
+= readOffset
;
578 dstPtr
+= writeOffset
;
580 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
581 GL_MAP_READ_BIT
, src
);
582 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
584 GL_MAP_INVALIDATE_RANGE_BIT
), dst
);
587 /* Note: the src and dst regions will never overlap. Trying to do so
588 * would generate GL_INVALID_VALUE earlier.
590 if (srcPtr
&& dstPtr
)
591 memcpy(dstPtr
, srcPtr
, size
);
593 ctx
->Driver
.UnmapBuffer(ctx
, src
);
595 ctx
->Driver
.UnmapBuffer(ctx
, dst
);
601 * Initialize the state associated with buffer objects
604 _mesa_init_buffer_objects( struct gl_context
*ctx
)
606 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
607 _glthread_INIT_MUTEX(DummyBufferObject
.Mutex
);
608 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
610 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
611 ctx
->Shared
->NullBufferObj
);
613 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
614 ctx
->Shared
->NullBufferObj
);
615 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
616 ctx
->Shared
->NullBufferObj
);
621 _mesa_free_buffer_objects( struct gl_context
*ctx
)
623 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
625 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
626 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
631 * Bind the specified target to buffer for the specified context.
632 * Called by glBindBuffer() and other functions.
635 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
637 struct gl_buffer_object
*oldBufObj
;
638 struct gl_buffer_object
*newBufObj
= NULL
;
639 struct gl_buffer_object
**bindTarget
= NULL
;
641 bindTarget
= get_buffer_target(ctx
, target
);
643 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
647 /* Get pointer to old buffer object (to be unbound) */
648 oldBufObj
= *bindTarget
;
649 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
650 return; /* rebinding the same buffer object- no change */
653 * Get pointer to new buffer object (newBufObj)
656 /* The spec says there's not a buffer object named 0, but we use
657 * one internally because it simplifies things.
659 newBufObj
= ctx
->Shared
->NullBufferObj
;
662 /* non-default buffer object */
663 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
664 if (!newBufObj
|| newBufObj
== &DummyBufferObject
) {
665 /* If this is a new buffer object id, or one which was generated but
666 * never used before, allocate a buffer object now.
668 ASSERT(ctx
->Driver
.NewBufferObject
);
669 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
671 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
674 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
678 /* bind new buffer */
679 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
681 /* Pass BindBuffer call to device driver */
682 if (ctx
->Driver
.BindBuffer
)
683 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
688 * Update the default buffer objects in the given context to reference those
689 * specified in the shared state and release those referencing the old
693 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
695 /* Bind the NullBufferObj to remove references to those
696 * in the shared context hash table.
698 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
699 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
700 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
701 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
707 * Return the gl_buffer_object for the given ID.
708 * Always return NULL for ID 0.
710 struct gl_buffer_object
*
711 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
716 return (struct gl_buffer_object
*)
717 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
722 * If *ptr points to obj, set ptr = the Null/default buffer object.
723 * This is a helper for buffer object deletion.
724 * The GL spec says that deleting a buffer object causes it to get
725 * unbound from all arrays in the current context.
728 unbind(struct gl_context
*ctx
,
729 struct gl_buffer_object
**ptr
,
730 struct gl_buffer_object
*obj
)
733 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
739 * Plug default/fallback buffer object functions into the device
743 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
745 /* GL_ARB_vertex/pixel_buffer_object */
746 driver
->NewBufferObject
= _mesa_new_buffer_object
;
747 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
748 driver
->BindBuffer
= NULL
;
749 driver
->BufferData
= _mesa_buffer_data
;
750 driver
->BufferSubData
= _mesa_buffer_subdata
;
751 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
752 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
754 /* GL_ARB_map_buffer_range */
755 driver
->MapBufferRange
= _mesa_buffer_map_range
;
756 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
758 /* GL_ARB_copy_buffer */
759 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
764 /**********************************************************************/
766 /**********************************************************************/
769 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
771 GET_CURRENT_CONTEXT(ctx
);
772 ASSERT_OUTSIDE_BEGIN_END(ctx
);
774 if (MESA_VERBOSE
& VERBOSE_API
)
775 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
776 _mesa_lookup_enum_by_nr(target
), buffer
);
778 bind_buffer_object(ctx
, target
, buffer
);
783 * Delete a set of buffer objects.
785 * \param n Number of buffer objects to delete.
786 * \param ids Array of \c n buffer object IDs.
789 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
791 GET_CURRENT_CONTEXT(ctx
);
793 ASSERT_OUTSIDE_BEGIN_END(ctx
);
794 FLUSH_VERTICES(ctx
, 0);
797 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
801 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
803 for (i
= 0; i
< n
; i
++) {
804 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
806 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
809 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
811 if (_mesa_bufferobj_mapped(bufObj
)) {
812 /* if mapped, unmap it now */
813 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
814 bufObj
->AccessFlags
= default_access_mode(ctx
);
815 bufObj
->Pointer
= NULL
;
818 /* unbind any vertex pointers bound to this buffer */
819 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
820 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
823 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
824 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
826 if (arrayObj
->ElementArrayBufferObj
== bufObj
) {
827 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
830 /* unbind ARB_copy_buffer binding points */
831 if (ctx
->CopyReadBuffer
== bufObj
) {
832 _mesa_BindBufferARB( GL_COPY_READ_BUFFER
, 0 );
834 if (ctx
->CopyWriteBuffer
== bufObj
) {
835 _mesa_BindBufferARB( GL_COPY_WRITE_BUFFER
, 0 );
838 /* unbind transform feedback binding points */
839 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
840 _mesa_BindBufferARB( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
842 for (j
= 0; j
< MAX_FEEDBACK_ATTRIBS
; j
++) {
843 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
844 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
848 if (ctx
->UniformBuffer
== bufObj
) {
849 _mesa_BindBufferARB( GL_UNIFORM_BUFFER
, 0 );
852 /* unbind any pixel pack/unpack pointers bound to this buffer */
853 if (ctx
->Pack
.BufferObj
== bufObj
) {
854 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
856 if (ctx
->Unpack
.BufferObj
== bufObj
) {
857 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
860 if (ctx
->Texture
.BufferObject
== bufObj
) {
861 _mesa_BindBufferARB( GL_TEXTURE_BUFFER
, 0 );
864 /* The ID is immediately freed for re-use */
865 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
866 /* Make sure we do not run into the classic ABA problem on bind.
867 * We don't want to allow re-binding a buffer object that's been
868 * "deleted" by glDeleteBuffers().
870 * The explicit rebinding to the default object in the current context
871 * prevents the above in the current context, but another context
872 * sharing the same objects might suffer from this problem.
873 * The alternative would be to do the hash lookup in any case on bind
874 * which would introduce more runtime overhead than this.
876 bufObj
->DeletePending
= GL_TRUE
;
877 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
881 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
886 * Generate a set of unique buffer object IDs and store them in \c buffer.
888 * \param n Number of IDs to generate.
889 * \param buffer Array of \c n locations to store the IDs.
892 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
894 GET_CURRENT_CONTEXT(ctx
);
897 ASSERT_OUTSIDE_BEGIN_END(ctx
);
899 if (MESA_VERBOSE
& VERBOSE_API
)
900 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
903 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
912 * This must be atomic (generation and allocation of buffer object IDs)
914 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
916 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
918 /* Insert the ID and pointer to dummy buffer object into hash table */
919 for (i
= 0; i
< n
; i
++) {
920 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
922 buffer
[i
] = first
+ i
;
925 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
930 * Determine if ID is the name of a buffer object.
932 * \param id ID of the potential buffer object.
933 * \return \c GL_TRUE if \c id is the name of a buffer object,
934 * \c GL_FALSE otherwise.
937 _mesa_IsBufferARB(GLuint id
)
939 struct gl_buffer_object
*bufObj
;
940 GET_CURRENT_CONTEXT(ctx
);
941 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
943 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
944 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
945 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
947 return bufObj
&& bufObj
!= &DummyBufferObject
;
952 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
953 const GLvoid
* data
, GLenum usage
)
955 GET_CURRENT_CONTEXT(ctx
);
956 struct gl_buffer_object
*bufObj
;
957 ASSERT_OUTSIDE_BEGIN_END(ctx
);
959 if (MESA_VERBOSE
& VERBOSE_API
)
960 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
961 _mesa_lookup_enum_by_nr(target
),
962 (long int) size
, data
,
963 _mesa_lookup_enum_by_nr(usage
));
966 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
971 case GL_STREAM_DRAW_ARB
:
972 case GL_STREAM_READ_ARB
:
973 case GL_STREAM_COPY_ARB
:
974 case GL_STATIC_DRAW_ARB
:
975 case GL_STATIC_READ_ARB
:
976 case GL_STATIC_COPY_ARB
:
977 case GL_DYNAMIC_DRAW_ARB
:
978 case GL_DYNAMIC_READ_ARB
:
979 case GL_DYNAMIC_COPY_ARB
:
983 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
987 bufObj
= get_buffer(ctx
, "glBufferDataARB", target
);
991 if (_mesa_bufferobj_mapped(bufObj
)) {
992 /* Unmap the existing buffer. We'll replace it now. Not an error. */
993 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
994 bufObj
->AccessFlags
= default_access_mode(ctx
);
995 ASSERT(bufObj
->Pointer
== NULL
);
998 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1000 bufObj
->Written
= GL_TRUE
;
1003 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1004 bufObj
->Name
, size
, data
, usage
);
1011 ASSERT(ctx
->Driver
.BufferData
);
1012 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1013 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1019 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1020 GLsizeiptrARB size
, const GLvoid
* data
)
1022 GET_CURRENT_CONTEXT(ctx
);
1023 struct gl_buffer_object
*bufObj
;
1024 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1026 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1027 "glBufferSubDataARB" );
1029 /* error already recorded */
1036 bufObj
->Written
= GL_TRUE
;
1038 ASSERT(ctx
->Driver
.BufferSubData
);
1039 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
1044 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1045 GLsizeiptrARB size
, void * data
)
1047 GET_CURRENT_CONTEXT(ctx
);
1048 struct gl_buffer_object
*bufObj
;
1049 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1051 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1052 "glGetBufferSubDataARB" );
1054 /* error already recorded */
1058 ASSERT(ctx
->Driver
.GetBufferSubData
);
1059 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
1064 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1066 GET_CURRENT_CONTEXT(ctx
);
1067 struct gl_buffer_object
* bufObj
;
1068 GLbitfield accessFlags
;
1071 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1074 case GL_READ_ONLY_ARB
:
1075 accessFlags
= GL_MAP_READ_BIT
;
1077 case GL_WRITE_ONLY_ARB
:
1078 accessFlags
= GL_MAP_WRITE_BIT
;
1080 case GL_READ_WRITE_ARB
:
1081 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1084 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1088 bufObj
= get_buffer(ctx
, "glMapBufferARB", target
);
1092 if (_mesa_bufferobj_mapped(bufObj
)) {
1093 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1097 if (!bufObj
->Size
) {
1098 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1099 "glMapBuffer(buffer size = 0)");
1103 ASSERT(ctx
->Driver
.MapBufferRange
);
1104 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
);
1106 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1110 /* The driver callback should have set these fields.
1111 * This is important because other modules (like VBO) might call
1112 * the driver function directly.
1114 ASSERT(bufObj
->Pointer
== map
);
1115 ASSERT(bufObj
->Length
== bufObj
->Size
);
1116 ASSERT(bufObj
->Offset
== 0);
1117 bufObj
->AccessFlags
= accessFlags
;
1120 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1121 bufObj
->Written
= GL_TRUE
;
1124 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1125 bufObj
->Name
, bufObj
->Size
, access
);
1126 if (access
== GL_WRITE_ONLY_ARB
) {
1128 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1129 for (i
= 0; i
< bufObj
->Size
; i
++)
1136 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1138 /* buffer is 100 bytes larger than requested, fill with magic value */
1139 for (i
= 0; i
< 100; i
++) {
1140 buf
[bufObj
->Size
- i
- 1] = 123;
1145 return bufObj
->Pointer
;
1149 GLboolean GLAPIENTRY
1150 _mesa_UnmapBufferARB(GLenum target
)
1152 GET_CURRENT_CONTEXT(ctx
);
1153 struct gl_buffer_object
*bufObj
;
1154 GLboolean status
= GL_TRUE
;
1155 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1157 bufObj
= get_buffer(ctx
, "glUnmapBufferARB", target
);
1161 if (!_mesa_bufferobj_mapped(bufObj
)) {
1162 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1167 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1168 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1170 /* check that last 100 bytes are still = magic value */
1171 for (i
= 0; i
< 100; i
++) {
1172 GLuint pos
= bufObj
->Size
- i
- 1;
1173 if (buf
[pos
] != 123) {
1174 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1175 " at position %d (value = %u)\n",
1183 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1184 GLuint i
, unchanged
= 0;
1185 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1187 /* check which bytes changed */
1188 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1189 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1196 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1197 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1202 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1203 bufObj
->AccessFlags
= default_access_mode(ctx
);
1204 ASSERT(bufObj
->Pointer
== NULL
);
1205 ASSERT(bufObj
->Offset
== 0);
1206 ASSERT(bufObj
->Length
== 0);
1213 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1215 GET_CURRENT_CONTEXT(ctx
);
1216 struct gl_buffer_object
*bufObj
;
1217 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1219 bufObj
= get_buffer(ctx
, "glGetBufferParameterivARB", target
);
1224 case GL_BUFFER_SIZE_ARB
:
1225 *params
= (GLint
) bufObj
->Size
;
1227 case GL_BUFFER_USAGE_ARB
:
1228 *params
= bufObj
->Usage
;
1230 case GL_BUFFER_ACCESS_ARB
:
1231 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1233 case GL_BUFFER_MAPPED_ARB
:
1234 *params
= _mesa_bufferobj_mapped(bufObj
);
1236 case GL_BUFFER_ACCESS_FLAGS
:
1237 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1239 *params
= bufObj
->AccessFlags
;
1241 case GL_BUFFER_MAP_OFFSET
:
1242 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1244 *params
= (GLint
) bufObj
->Offset
;
1246 case GL_BUFFER_MAP_LENGTH
:
1247 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1249 *params
= (GLint
) bufObj
->Length
;
1252 ; /* fall-through */
1256 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1257 _mesa_lookup_enum_by_nr(pname
));
1263 * This is pretty much a duplicate of GetBufferParameteriv() but the
1264 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1267 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1269 GET_CURRENT_CONTEXT(ctx
);
1270 struct gl_buffer_object
*bufObj
;
1271 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1273 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
);
1278 case GL_BUFFER_SIZE_ARB
:
1279 *params
= bufObj
->Size
;
1281 case GL_BUFFER_USAGE_ARB
:
1282 *params
= bufObj
->Usage
;
1284 case GL_BUFFER_ACCESS_ARB
:
1285 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1287 case GL_BUFFER_ACCESS_FLAGS
:
1288 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1290 *params
= bufObj
->AccessFlags
;
1292 case GL_BUFFER_MAPPED_ARB
:
1293 *params
= _mesa_bufferobj_mapped(bufObj
);
1295 case GL_BUFFER_MAP_OFFSET
:
1296 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1298 *params
= bufObj
->Offset
;
1300 case GL_BUFFER_MAP_LENGTH
:
1301 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1303 *params
= bufObj
->Length
;
1306 ; /* fall-through */
1310 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1311 _mesa_lookup_enum_by_nr(pname
));
1316 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1318 GET_CURRENT_CONTEXT(ctx
);
1319 struct gl_buffer_object
* bufObj
;
1320 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1322 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1323 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1327 bufObj
= get_buffer(ctx
, "glGetBufferPointervARB", target
);
1331 *params
= bufObj
->Pointer
;
1336 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1337 GLintptr readOffset
, GLintptr writeOffset
,
1340 GET_CURRENT_CONTEXT(ctx
);
1341 struct gl_buffer_object
*src
, *dst
;
1342 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1344 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
);
1348 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
);
1352 if (_mesa_bufferobj_mapped(src
)) {
1353 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1354 "glCopyBufferSubData(readBuffer is mapped)");
1358 if (_mesa_bufferobj_mapped(dst
)) {
1359 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1360 "glCopyBufferSubData(writeBuffer is mapped)");
1364 if (readOffset
< 0) {
1365 _mesa_error(ctx
, GL_INVALID_VALUE
,
1366 "glCopyBufferSubData(readOffset = %d)", (int) readOffset
);
1370 if (writeOffset
< 0) {
1371 _mesa_error(ctx
, GL_INVALID_VALUE
,
1372 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset
);
1377 _mesa_error(ctx
, GL_INVALID_VALUE
,
1378 "glCopyBufferSubData(writeOffset = %d)", (int) size
);
1382 if (readOffset
+ size
> src
->Size
) {
1383 _mesa_error(ctx
, GL_INVALID_VALUE
,
1384 "glCopyBufferSubData(readOffset + size = %d)",
1385 (int) (readOffset
+ size
));
1389 if (writeOffset
+ size
> dst
->Size
) {
1390 _mesa_error(ctx
, GL_INVALID_VALUE
,
1391 "glCopyBufferSubData(writeOffset + size = %d)",
1392 (int) (writeOffset
+ size
));
1397 if (readOffset
+ size
<= writeOffset
) {
1400 else if (writeOffset
+ size
<= readOffset
) {
1404 /* overlapping src/dst is illegal */
1405 _mesa_error(ctx
, GL_INVALID_VALUE
,
1406 "glCopyBufferSubData(overlapping src/dst)");
1411 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1416 * See GL_ARB_map_buffer_range spec
1419 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1422 GET_CURRENT_CONTEXT(ctx
);
1423 struct gl_buffer_object
*bufObj
;
1426 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1428 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1429 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1430 "glMapBufferRange(extension not supported)");
1435 _mesa_error(ctx
, GL_INVALID_VALUE
,
1436 "glMapBufferRange(offset = %ld)", (long)offset
);
1441 _mesa_error(ctx
, GL_INVALID_VALUE
,
1442 "glMapBufferRange(length = %ld)", (long)length
);
1446 if (access
& ~(GL_MAP_READ_BIT
|
1448 GL_MAP_INVALIDATE_RANGE_BIT
|
1449 GL_MAP_INVALIDATE_BUFFER_BIT
|
1450 GL_MAP_FLUSH_EXPLICIT_BIT
|
1451 GL_MAP_UNSYNCHRONIZED_BIT
)) {
1452 /* generate an error if any undefind bit is set */
1453 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1457 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1458 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1459 "glMapBufferRange(access indicates neither read or write)");
1463 if ((access
& GL_MAP_READ_BIT
) &&
1464 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1465 GL_MAP_INVALIDATE_BUFFER_BIT
|
1466 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1467 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1468 "glMapBufferRange(invalid access flags)");
1472 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1473 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1474 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1475 "glMapBufferRange(invalid access flags)");
1479 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
);
1483 if (offset
+ length
> bufObj
->Size
) {
1484 _mesa_error(ctx
, GL_INVALID_VALUE
,
1485 "glMapBufferRange(offset + length > size)");
1489 if (_mesa_bufferobj_mapped(bufObj
)) {
1490 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1491 "glMapBufferRange(buffer already mapped)");
1495 if (!bufObj
->Size
) {
1496 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1497 "glMapBufferRange(buffer size = 0)");
1501 /* Mapping zero bytes should return a non-null pointer. */
1503 static long dummy
= 0;
1504 bufObj
->Pointer
= &dummy
;
1505 bufObj
->Length
= length
;
1506 bufObj
->Offset
= offset
;
1507 bufObj
->AccessFlags
= access
;
1508 return bufObj
->Pointer
;
1511 ASSERT(ctx
->Driver
.MapBufferRange
);
1512 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
);
1514 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1517 /* The driver callback should have set all these fields.
1518 * This is important because other modules (like VBO) might call
1519 * the driver function directly.
1521 ASSERT(bufObj
->Pointer
== map
);
1522 ASSERT(bufObj
->Length
== length
);
1523 ASSERT(bufObj
->Offset
== offset
);
1524 ASSERT(bufObj
->AccessFlags
== access
);
1532 * See GL_ARB_map_buffer_range spec
1535 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1537 GET_CURRENT_CONTEXT(ctx
);
1538 struct gl_buffer_object
*bufObj
;
1539 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1541 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1542 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1543 "glFlushMappedBufferRange(extension not supported)");
1548 _mesa_error(ctx
, GL_INVALID_VALUE
,
1549 "glFlushMappedBufferRange(offset = %ld)", (long)offset
);
1554 _mesa_error(ctx
, GL_INVALID_VALUE
,
1555 "glFlushMappedBufferRange(length = %ld)", (long)length
);
1559 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
);
1563 if (!_mesa_bufferobj_mapped(bufObj
)) {
1564 /* buffer is not mapped */
1565 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1566 "glFlushMappedBufferRange(buffer is not mapped)");
1570 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1571 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1572 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1576 if (offset
+ length
> bufObj
->Length
) {
1577 _mesa_error(ctx
, GL_INVALID_VALUE
,
1578 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1579 (long)offset
, (long)length
, (long)bufObj
->Length
);
1583 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1585 if (ctx
->Driver
.FlushMappedBufferRange
)
1586 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
);
1590 #if FEATURE_APPLE_object_purgeable
1592 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1594 struct gl_buffer_object
*bufObj
;
1597 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1599 _mesa_error(ctx
, GL_INVALID_VALUE
,
1600 "glObjectPurgeable(name = 0x%x)", name
);
1603 if (!_mesa_is_bufferobj(bufObj
)) {
1604 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1608 if (bufObj
->Purgeable
) {
1609 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1610 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1611 return GL_VOLATILE_APPLE
;
1614 bufObj
->Purgeable
= GL_TRUE
;
1616 retval
= GL_VOLATILE_APPLE
;
1617 if (ctx
->Driver
.BufferObjectPurgeable
)
1618 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1625 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1627 struct gl_renderbuffer
*bufObj
;
1630 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1632 _mesa_error(ctx
, GL_INVALID_VALUE
,
1633 "glObjectUnpurgeable(name = 0x%x)", name
);
1637 if (bufObj
->Purgeable
) {
1638 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1639 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1640 return GL_VOLATILE_APPLE
;
1643 bufObj
->Purgeable
= GL_TRUE
;
1645 retval
= GL_VOLATILE_APPLE
;
1646 if (ctx
->Driver
.RenderObjectPurgeable
)
1647 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1654 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1656 struct gl_texture_object
*bufObj
;
1659 bufObj
= _mesa_lookup_texture(ctx
, name
);
1661 _mesa_error(ctx
, GL_INVALID_VALUE
,
1662 "glObjectPurgeable(name = 0x%x)", name
);
1666 if (bufObj
->Purgeable
) {
1667 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1668 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1669 return GL_VOLATILE_APPLE
;
1672 bufObj
->Purgeable
= GL_TRUE
;
1674 retval
= GL_VOLATILE_APPLE
;
1675 if (ctx
->Driver
.TextureObjectPurgeable
)
1676 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1683 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1687 GET_CURRENT_CONTEXT(ctx
);
1688 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1691 _mesa_error(ctx
, GL_INVALID_VALUE
,
1692 "glObjectPurgeable(name = 0x%x)", name
);
1697 case GL_VOLATILE_APPLE
:
1698 case GL_RELEASED_APPLE
:
1702 _mesa_error(ctx
, GL_INVALID_ENUM
,
1703 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1708 switch (objectType
) {
1710 retval
= texture_object_purgeable(ctx
, name
, option
);
1712 case GL_RENDERBUFFER_EXT
:
1713 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1715 case GL_BUFFER_OBJECT_APPLE
:
1716 retval
= buffer_object_purgeable(ctx
, name
, option
);
1719 _mesa_error(ctx
, GL_INVALID_ENUM
,
1720 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1725 /* In strict conformance to the spec, we must only return VOLATILE when
1726 * when passed the VOLATILE option. Madness.
1728 * XXX First fix the spec, then fix me.
1730 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1735 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1737 struct gl_buffer_object
*bufObj
;
1740 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1742 _mesa_error(ctx
, GL_INVALID_VALUE
,
1743 "glObjectUnpurgeable(name = 0x%x)", name
);
1747 if (! bufObj
->Purgeable
) {
1748 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1749 "glObjectUnpurgeable(name = 0x%x) object is "
1750 " already \"unpurged\"", name
);
1754 bufObj
->Purgeable
= GL_FALSE
;
1757 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1758 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1765 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1767 struct gl_renderbuffer
*bufObj
;
1770 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1772 _mesa_error(ctx
, GL_INVALID_VALUE
,
1773 "glObjectUnpurgeable(name = 0x%x)", name
);
1777 if (! bufObj
->Purgeable
) {
1778 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1779 "glObjectUnpurgeable(name = 0x%x) object is "
1780 " already \"unpurged\"", name
);
1784 bufObj
->Purgeable
= GL_FALSE
;
1787 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1788 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1795 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1797 struct gl_texture_object
*bufObj
;
1800 bufObj
= _mesa_lookup_texture(ctx
, name
);
1802 _mesa_error(ctx
, GL_INVALID_VALUE
,
1803 "glObjectUnpurgeable(name = 0x%x)", name
);
1807 if (! bufObj
->Purgeable
) {
1808 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1809 "glObjectUnpurgeable(name = 0x%x) object is"
1810 " already \"unpurged\"", name
);
1814 bufObj
->Purgeable
= GL_FALSE
;
1817 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1818 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1825 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1827 GET_CURRENT_CONTEXT(ctx
);
1828 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1831 _mesa_error(ctx
, GL_INVALID_VALUE
,
1832 "glObjectUnpurgeable(name = 0x%x)", name
);
1837 case GL_RETAINED_APPLE
:
1838 case GL_UNDEFINED_APPLE
:
1842 _mesa_error(ctx
, GL_INVALID_ENUM
,
1843 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1848 switch (objectType
) {
1849 case GL_BUFFER_OBJECT_APPLE
:
1850 return buffer_object_unpurgeable(ctx
, name
, option
);
1852 return texture_object_unpurgeable(ctx
, name
, option
);
1853 case GL_RENDERBUFFER_EXT
:
1854 return renderbuffer_unpurgeable(ctx
, name
, option
);
1856 _mesa_error(ctx
, GL_INVALID_ENUM
,
1857 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1865 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1866 GLenum pname
, GLint
*params
)
1868 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1870 _mesa_error(ctx
, GL_INVALID_VALUE
,
1871 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1876 case GL_PURGEABLE_APPLE
:
1877 *params
= bufObj
->Purgeable
;
1880 _mesa_error(ctx
, GL_INVALID_ENUM
,
1881 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1889 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1890 GLenum pname
, GLint
*params
)
1892 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1894 _mesa_error(ctx
, GL_INVALID_VALUE
,
1895 "glObjectUnpurgeable(name = 0x%x)", name
);
1900 case GL_PURGEABLE_APPLE
:
1901 *params
= rb
->Purgeable
;
1904 _mesa_error(ctx
, GL_INVALID_ENUM
,
1905 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1913 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1914 GLenum pname
, GLint
*params
)
1916 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1918 _mesa_error(ctx
, GL_INVALID_VALUE
,
1919 "glObjectUnpurgeable(name = 0x%x)", name
);
1924 case GL_PURGEABLE_APPLE
:
1925 *params
= texObj
->Purgeable
;
1928 _mesa_error(ctx
, GL_INVALID_ENUM
,
1929 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1937 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
1940 GET_CURRENT_CONTEXT(ctx
);
1943 _mesa_error(ctx
, GL_INVALID_VALUE
,
1944 "glGetObjectParameteriv(name = 0x%x)", name
);
1948 switch (objectType
) {
1950 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
1952 case GL_BUFFER_OBJECT_APPLE
:
1953 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
1955 case GL_RENDERBUFFER_EXT
:
1956 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
1959 _mesa_error(ctx
, GL_INVALID_ENUM
,
1960 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1965 #endif /* FEATURE_APPLE_object_purgeable */