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"
51 /*#define BOUNDS_CHECK*/
55 * Used as a placeholder for buffer objects between glGenBuffers() and
56 * glBindBuffer() so that glIsBuffer() can work correctly.
58 static struct gl_buffer_object DummyBufferObject
;
62 * Return pointer to address of a buffer object target.
63 * \param ctx the GL context
64 * \param target the buffer object target to be retrieved.
65 * \return pointer to pointer to the buffer object bound to \c target in the
66 * specified context or \c NULL if \c target is invalid.
68 static inline struct gl_buffer_object
**
69 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
72 case GL_ARRAY_BUFFER_ARB
:
73 return &ctx
->Array
.ArrayBufferObj
;
74 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
75 return &ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
76 case GL_PIXEL_PACK_BUFFER_EXT
:
77 return &ctx
->Pack
.BufferObj
;
78 case GL_PIXEL_UNPACK_BUFFER_EXT
:
79 return &ctx
->Unpack
.BufferObj
;
80 case GL_COPY_READ_BUFFER
:
81 return &ctx
->CopyReadBuffer
;
82 case GL_COPY_WRITE_BUFFER
:
83 return &ctx
->CopyWriteBuffer
;
84 #if FEATURE_EXT_transform_feedback
85 case GL_TRANSFORM_FEEDBACK_BUFFER
:
86 if (ctx
->Extensions
.EXT_transform_feedback
) {
87 return &ctx
->TransformFeedback
.CurrentBuffer
;
91 case GL_TEXTURE_BUFFER
:
92 if (ctx
->Extensions
.ARB_texture_buffer_object
) {
93 return &ctx
->Texture
.BufferObject
;
96 case GL_UNIFORM_BUFFER
:
97 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
98 return &ctx
->UniformBuffer
;
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
, const char *func
, GLenum target
)
118 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
121 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
125 if (!_mesa_is_bufferobj(*bufObj
)) {
126 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(buffer 0)", func
);
134 static inline GLbitfield
135 default_access_mode(const struct gl_context
*ctx
)
137 /* Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
139 * Name Type Initial Value Legal Values
141 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
144 * However, table 6.8 in the GL_OES_mapbuffer extension says:
146 * Get Value Type Get Command Value Description
147 * --------- ---- ----------- ----- -----------
148 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
150 * The difference is because GL_OES_mapbuffer only supports mapping buffers
153 return (ctx
->API
== API_OPENGLES
)
154 ? GL_MAP_WRITE_BIT
: (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
);
159 * Convert a GLbitfield describing the mapped buffer access flags
160 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
163 simplified_access_mode(GLbitfield access
)
165 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
166 if ((access
& rwFlags
) == rwFlags
)
167 return GL_READ_WRITE
;
168 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
170 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
171 return GL_WRITE_ONLY
;
172 return GL_READ_WRITE
; /* this should never happen, but no big deal */
177 * Tests the subdata range parameters and sets the GL error code for
178 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
180 * \param ctx GL context.
181 * \param target Buffer object target on which to operate.
182 * \param offset Offset of the first byte of the subdata range.
183 * \param size Size, in bytes, of the subdata range.
184 * \param caller Name of calling function for recording errors.
185 * \return A pointer to the buffer object bound to \c target in the
186 * specified context or \c NULL if any of the parameter or state
187 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
190 * \sa glBufferSubDataARB, glGetBufferSubDataARB
192 static struct gl_buffer_object
*
193 buffer_object_subdata_range_good( struct gl_context
* ctx
, GLenum target
,
194 GLintptrARB offset
, GLsizeiptrARB size
,
197 struct gl_buffer_object
*bufObj
;
200 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
205 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
209 bufObj
= get_buffer(ctx
, caller
, target
);
213 if (offset
+ size
> bufObj
->Size
) {
214 _mesa_error(ctx
, GL_INVALID_VALUE
,
215 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
216 (unsigned long) offset
,
217 (unsigned long) size
,
218 (unsigned long) bufObj
->Size
);
221 if (_mesa_bufferobj_mapped(bufObj
)) {
222 /* Buffer is currently mapped */
223 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
232 * Allocate and initialize a new buffer object.
234 * Default callback for the \c dd_function_table::NewBufferObject() hook.
236 static struct gl_buffer_object
*
237 _mesa_new_buffer_object( struct gl_context
*ctx
, GLuint name
, GLenum target
)
239 struct gl_buffer_object
*obj
;
243 obj
= MALLOC_STRUCT(gl_buffer_object
);
244 _mesa_initialize_buffer_object(ctx
, obj
, name
, target
);
250 * Delete a buffer object.
252 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
255 _mesa_delete_buffer_object(struct gl_context
*ctx
,
256 struct gl_buffer_object
*bufObj
)
263 /* assign strange values here to help w/ debugging */
264 bufObj
->RefCount
= -1000;
267 _glthread_DESTROY_MUTEX(bufObj
->Mutex
);
274 * Set ptr to bufObj w/ reference counting.
275 * This is normally only called from the _mesa_reference_buffer_object() macro
276 * when there's a real pointer change.
279 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
280 struct gl_buffer_object
**ptr
,
281 struct gl_buffer_object
*bufObj
)
284 /* Unreference the old buffer */
285 GLboolean deleteFlag
= GL_FALSE
;
286 struct gl_buffer_object
*oldObj
= *ptr
;
288 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
289 ASSERT(oldObj
->RefCount
> 0);
292 printf("BufferObj %p %d DECR to %d\n",
293 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
295 deleteFlag
= (oldObj
->RefCount
== 0);
296 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
300 /* some sanity checking: don't delete a buffer still in use */
302 /* unfortunately, these tests are invalid during context tear-down */
303 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
304 ASSERT(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
!= bufObj
);
305 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
308 ASSERT(ctx
->Driver
.DeleteBuffer
);
309 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
317 /* reference new buffer */
318 _glthread_LOCK_MUTEX(bufObj
->Mutex
);
319 if (bufObj
->RefCount
== 0) {
320 /* this buffer's being deleted (look just above) */
321 /* Not sure this can every really happen. Warn if it does. */
322 _mesa_problem(NULL
, "referencing deleted buffer object");
328 printf("BufferObj %p %d INCR to %d\n",
329 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
333 _glthread_UNLOCK_MUTEX(bufObj
->Mutex
);
339 * Initialize a buffer object to default values.
342 _mesa_initialize_buffer_object( struct gl_context
*ctx
,
343 struct gl_buffer_object
*obj
,
344 GLuint name
, GLenum target
)
348 memset(obj
, 0, sizeof(struct gl_buffer_object
));
349 _glthread_INIT_MUTEX(obj
->Mutex
);
352 obj
->Usage
= GL_STATIC_DRAW_ARB
;
353 obj
->AccessFlags
= default_access_mode(ctx
);
359 * Callback called from _mesa_HashWalk()
362 count_buffer_size(GLuint key
, void *data
, void *userData
)
364 const struct gl_buffer_object
*bufObj
=
365 (const struct gl_buffer_object
*) data
;
366 GLuint
*total
= (GLuint
*) userData
;
368 *total
= *total
+ bufObj
->Size
;
373 * Compute total size (in bytes) of all buffer objects for the given context.
374 * For debugging purposes.
377 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
381 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
388 * Allocate space for and store data in a buffer object. Any data that was
389 * previously stored in the buffer object is lost. If \c data is \c NULL,
390 * memory will be allocated, but no copy will occur.
392 * This is the default callback for \c dd_function_table::BufferData()
393 * Note that all GL error checking will have been done already.
395 * \param ctx GL context.
396 * \param target Buffer object target on which to operate.
397 * \param size Size, in bytes, of the new data store.
398 * \param data Pointer to the data to store in the buffer object. This
399 * pointer may be \c NULL.
400 * \param usage Hints about how the data will be used.
401 * \param bufObj Object to be used.
403 * \return GL_TRUE for success, GL_FALSE for failure
404 * \sa glBufferDataARB, dd_function_table::BufferData.
407 _mesa_buffer_data( struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
408 const GLvoid
* data
, GLenum usage
,
409 struct gl_buffer_object
* bufObj
)
413 (void) ctx
; (void) target
;
415 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
417 bufObj
->Data
= (GLubyte
*) new_data
;
419 bufObj
->Usage
= usage
;
422 memcpy( bufObj
->Data
, data
, size
);
434 * Replace data in a subrange of buffer object. If the data range
435 * specified by \c size + \c offset extends beyond the end of the buffer or
436 * if \c data is \c NULL, no copy is performed.
438 * This is the default callback for \c dd_function_table::BufferSubData()
439 * Note that all GL error checking will have been done already.
441 * \param ctx GL context.
442 * \param target Buffer object target on which to operate.
443 * \param offset Offset of the first byte to be modified.
444 * \param size Size, in bytes, of the data range.
445 * \param data Pointer to the data to store in the buffer object.
446 * \param bufObj Object to be used.
448 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
451 _mesa_buffer_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
452 GLsizeiptrARB size
, const GLvoid
* data
,
453 struct gl_buffer_object
* bufObj
)
457 /* this should have been caught in _mesa_BufferSubData() */
458 ASSERT(size
+ offset
<= bufObj
->Size
);
461 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
467 * Retrieve data from a subrange of buffer object. If the data range
468 * specified by \c size + \c offset extends beyond the end of the buffer or
469 * if \c data is \c NULL, no copy is performed.
471 * This is the default callback for \c dd_function_table::GetBufferSubData()
472 * Note that all GL error checking will have been done already.
474 * \param ctx GL context.
475 * \param target Buffer object target on which to operate.
476 * \param offset Offset of the first byte to be fetched.
477 * \param size Size, in bytes, of the data range.
478 * \param data Destination for data
479 * \param bufObj Object to be used.
481 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
484 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
485 GLsizeiptrARB size
, GLvoid
* data
,
486 struct gl_buffer_object
* bufObj
)
490 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
491 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
497 * Default fallback for \c dd_function_table::MapBufferRange().
498 * Called via glMapBufferRange().
501 _mesa_buffer_map_range( struct gl_context
*ctx
, GLintptr offset
,
502 GLsizeiptr length
, GLbitfield access
,
503 struct gl_buffer_object
*bufObj
)
506 assert(!_mesa_bufferobj_mapped(bufObj
));
507 /* Just return a direct pointer to the data */
508 bufObj
->Pointer
= bufObj
->Data
+ offset
;
509 bufObj
->Length
= length
;
510 bufObj
->Offset
= offset
;
511 bufObj
->AccessFlags
= access
;
512 return bufObj
->Pointer
;
517 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
518 * Called via glFlushMappedBufferRange().
521 _mesa_buffer_flush_mapped_range( struct gl_context
*ctx
,
522 GLintptr offset
, GLsizeiptr length
,
523 struct gl_buffer_object
*obj
)
534 * Default callback for \c dd_function_table::MapBuffer().
536 * The input parameters will have been already tested for errors.
538 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
541 _mesa_buffer_unmap( struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
544 /* XXX we might assert here that bufObj->Pointer is non-null */
545 bufObj
->Pointer
= NULL
;
548 bufObj
->AccessFlags
= 0x0;
554 * Default fallback for \c dd_function_table::CopyBufferSubData().
555 * Called via glCopyBufferSubData().
558 _mesa_copy_buffer_subdata(struct gl_context
*ctx
,
559 struct gl_buffer_object
*src
,
560 struct gl_buffer_object
*dst
,
561 GLintptr readOffset
, GLintptr writeOffset
,
564 GLubyte
*srcPtr
, *dstPtr
;
566 /* the buffers should not be mapped */
567 assert(!_mesa_bufferobj_mapped(src
));
568 assert(!_mesa_bufferobj_mapped(dst
));
571 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
573 GL_MAP_WRITE_BIT
, src
);
578 srcPtr
+= readOffset
;
579 dstPtr
+= writeOffset
;
581 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
582 GL_MAP_READ_BIT
, src
);
583 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
585 GL_MAP_INVALIDATE_RANGE_BIT
), dst
);
588 /* Note: the src and dst regions will never overlap. Trying to do so
589 * would generate GL_INVALID_VALUE earlier.
591 if (srcPtr
&& dstPtr
)
592 memcpy(dstPtr
, srcPtr
, size
);
594 ctx
->Driver
.UnmapBuffer(ctx
, src
);
596 ctx
->Driver
.UnmapBuffer(ctx
, dst
);
602 * Initialize the state associated with buffer objects
605 _mesa_init_buffer_objects( struct gl_context
*ctx
)
609 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
610 _glthread_INIT_MUTEX(DummyBufferObject
.Mutex
);
611 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
613 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
614 ctx
->Shared
->NullBufferObj
);
616 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
617 ctx
->Shared
->NullBufferObj
);
618 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
619 ctx
->Shared
->NullBufferObj
);
621 ctx
->UniformBufferBindings
= calloc(ctx
->Const
.MaxUniformBufferBindings
,
622 sizeof(*ctx
->UniformBufferBindings
));
624 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
625 ctx
->Shared
->NullBufferObj
);
627 for (i
= 0; i
< ctx
->Const
.MaxUniformBufferBindings
; i
++) {
628 _mesa_reference_buffer_object(ctx
,
629 &ctx
->UniformBufferBindings
[i
].BufferObject
,
630 ctx
->Shared
->NullBufferObj
);
631 ctx
->UniformBufferBindings
[i
].Offset
= -1;
632 ctx
->UniformBufferBindings
[i
].Size
= -1;
638 _mesa_free_buffer_objects( struct gl_context
*ctx
)
642 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
644 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
645 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
647 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
649 for (i
= 0; i
< ctx
->Const
.MaxUniformBufferBindings
; i
++) {
650 _mesa_reference_buffer_object(ctx
,
651 &ctx
->UniformBufferBindings
[i
].BufferObject
,
655 free(ctx
->UniformBufferBindings
);
656 ctx
->UniformBufferBindings
= NULL
;
661 * Bind the specified target to buffer for the specified context.
662 * Called by glBindBuffer() and other functions.
665 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
667 struct gl_buffer_object
*oldBufObj
;
668 struct gl_buffer_object
*newBufObj
= NULL
;
669 struct gl_buffer_object
**bindTarget
= NULL
;
671 bindTarget
= get_buffer_target(ctx
, target
);
673 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
677 /* Get pointer to old buffer object (to be unbound) */
678 oldBufObj
= *bindTarget
;
679 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
680 return; /* rebinding the same buffer object- no change */
683 * Get pointer to new buffer object (newBufObj)
686 /* The spec says there's not a buffer object named 0, but we use
687 * one internally because it simplifies things.
689 newBufObj
= ctx
->Shared
->NullBufferObj
;
692 /* non-default buffer object */
693 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
694 if (!newBufObj
|| newBufObj
== &DummyBufferObject
) {
695 /* If this is a new buffer object id, or one which was generated but
696 * never used before, allocate a buffer object now.
698 ASSERT(ctx
->Driver
.NewBufferObject
);
699 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
701 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
704 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
708 /* bind new buffer */
709 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
711 /* Pass BindBuffer call to device driver */
712 if (ctx
->Driver
.BindBuffer
)
713 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
718 * Update the default buffer objects in the given context to reference those
719 * specified in the shared state and release those referencing the old
723 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
725 /* Bind the NullBufferObj to remove references to those
726 * in the shared context hash table.
728 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
729 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
730 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
731 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
737 * Return the gl_buffer_object for the given ID.
738 * Always return NULL for ID 0.
740 struct gl_buffer_object
*
741 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
746 return (struct gl_buffer_object
*)
747 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
752 * If *ptr points to obj, set ptr = the Null/default buffer object.
753 * This is a helper for buffer object deletion.
754 * The GL spec says that deleting a buffer object causes it to get
755 * unbound from all arrays in the current context.
758 unbind(struct gl_context
*ctx
,
759 struct gl_buffer_object
**ptr
,
760 struct gl_buffer_object
*obj
)
763 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
769 * Plug default/fallback buffer object functions into the device
773 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
775 /* GL_ARB_vertex/pixel_buffer_object */
776 driver
->NewBufferObject
= _mesa_new_buffer_object
;
777 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
778 driver
->BindBuffer
= NULL
;
779 driver
->BufferData
= _mesa_buffer_data
;
780 driver
->BufferSubData
= _mesa_buffer_subdata
;
781 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
782 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
784 /* GL_ARB_map_buffer_range */
785 driver
->MapBufferRange
= _mesa_buffer_map_range
;
786 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
788 /* GL_ARB_copy_buffer */
789 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
794 /**********************************************************************/
796 /**********************************************************************/
799 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
801 GET_CURRENT_CONTEXT(ctx
);
802 ASSERT_OUTSIDE_BEGIN_END(ctx
);
804 if (MESA_VERBOSE
& VERBOSE_API
)
805 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
806 _mesa_lookup_enum_by_nr(target
), buffer
);
808 bind_buffer_object(ctx
, target
, buffer
);
813 * Delete a set of buffer objects.
815 * \param n Number of buffer objects to delete.
816 * \param ids Array of \c n buffer object IDs.
819 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
821 GET_CURRENT_CONTEXT(ctx
);
823 ASSERT_OUTSIDE_BEGIN_END(ctx
);
824 FLUSH_VERTICES(ctx
, 0);
827 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
831 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
833 for (i
= 0; i
< n
; i
++) {
834 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
836 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
839 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
841 if (_mesa_bufferobj_mapped(bufObj
)) {
842 /* if mapped, unmap it now */
843 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
844 bufObj
->AccessFlags
= default_access_mode(ctx
);
845 bufObj
->Pointer
= NULL
;
848 /* unbind any vertex pointers bound to this buffer */
849 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
850 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
853 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
854 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
856 if (arrayObj
->ElementArrayBufferObj
== bufObj
) {
857 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
860 /* unbind ARB_copy_buffer binding points */
861 if (ctx
->CopyReadBuffer
== bufObj
) {
862 _mesa_BindBufferARB( GL_COPY_READ_BUFFER
, 0 );
864 if (ctx
->CopyWriteBuffer
== bufObj
) {
865 _mesa_BindBufferARB( GL_COPY_WRITE_BUFFER
, 0 );
868 /* unbind transform feedback binding points */
869 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
870 _mesa_BindBufferARB( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
872 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
873 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
874 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
878 if (ctx
->UniformBuffer
== bufObj
) {
879 _mesa_BindBufferARB( GL_UNIFORM_BUFFER
, 0 );
882 /* unbind any pixel pack/unpack pointers bound to this buffer */
883 if (ctx
->Pack
.BufferObj
== bufObj
) {
884 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
886 if (ctx
->Unpack
.BufferObj
== bufObj
) {
887 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
890 if (ctx
->Texture
.BufferObject
== bufObj
) {
891 _mesa_BindBufferARB( GL_TEXTURE_BUFFER
, 0 );
894 /* The ID is immediately freed for re-use */
895 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
896 /* Make sure we do not run into the classic ABA problem on bind.
897 * We don't want to allow re-binding a buffer object that's been
898 * "deleted" by glDeleteBuffers().
900 * The explicit rebinding to the default object in the current context
901 * prevents the above in the current context, but another context
902 * sharing the same objects might suffer from this problem.
903 * The alternative would be to do the hash lookup in any case on bind
904 * which would introduce more runtime overhead than this.
906 bufObj
->DeletePending
= GL_TRUE
;
907 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
911 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
916 * Generate a set of unique buffer object IDs and store them in \c buffer.
918 * \param n Number of IDs to generate.
919 * \param buffer Array of \c n locations to store the IDs.
922 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
924 GET_CURRENT_CONTEXT(ctx
);
927 ASSERT_OUTSIDE_BEGIN_END(ctx
);
929 if (MESA_VERBOSE
& VERBOSE_API
)
930 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
933 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
942 * This must be atomic (generation and allocation of buffer object IDs)
944 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
946 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
948 /* Insert the ID and pointer to dummy buffer object into hash table */
949 for (i
= 0; i
< n
; i
++) {
950 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
952 buffer
[i
] = first
+ i
;
955 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
960 * Determine if ID is the name of a buffer object.
962 * \param id ID of the potential buffer object.
963 * \return \c GL_TRUE if \c id is the name of a buffer object,
964 * \c GL_FALSE otherwise.
967 _mesa_IsBufferARB(GLuint id
)
969 struct gl_buffer_object
*bufObj
;
970 GET_CURRENT_CONTEXT(ctx
);
971 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
973 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
974 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
975 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
977 return bufObj
&& bufObj
!= &DummyBufferObject
;
982 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
983 const GLvoid
* data
, GLenum usage
)
985 GET_CURRENT_CONTEXT(ctx
);
986 struct gl_buffer_object
*bufObj
;
987 ASSERT_OUTSIDE_BEGIN_END(ctx
);
989 if (MESA_VERBOSE
& VERBOSE_API
)
990 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
991 _mesa_lookup_enum_by_nr(target
),
992 (long int) size
, data
,
993 _mesa_lookup_enum_by_nr(usage
));
996 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1001 case GL_STREAM_DRAW_ARB
:
1002 case GL_STREAM_READ_ARB
:
1003 case GL_STREAM_COPY_ARB
:
1004 case GL_STATIC_DRAW_ARB
:
1005 case GL_STATIC_READ_ARB
:
1006 case GL_STATIC_COPY_ARB
:
1007 case GL_DYNAMIC_DRAW_ARB
:
1008 case GL_DYNAMIC_READ_ARB
:
1009 case GL_DYNAMIC_COPY_ARB
:
1013 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
1017 bufObj
= get_buffer(ctx
, "glBufferDataARB", target
);
1021 if (_mesa_bufferobj_mapped(bufObj
)) {
1022 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1023 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
1024 bufObj
->AccessFlags
= default_access_mode(ctx
);
1025 ASSERT(bufObj
->Pointer
== NULL
);
1028 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1030 bufObj
->Written
= GL_TRUE
;
1033 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1034 bufObj
->Name
, size
, data
, usage
);
1041 ASSERT(ctx
->Driver
.BufferData
);
1042 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1043 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1049 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1050 GLsizeiptrARB size
, const GLvoid
* data
)
1052 GET_CURRENT_CONTEXT(ctx
);
1053 struct gl_buffer_object
*bufObj
;
1054 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1056 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1057 "glBufferSubDataARB" );
1059 /* error already recorded */
1066 bufObj
->Written
= GL_TRUE
;
1068 ASSERT(ctx
->Driver
.BufferSubData
);
1069 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
1074 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1075 GLsizeiptrARB size
, void * data
)
1077 GET_CURRENT_CONTEXT(ctx
);
1078 struct gl_buffer_object
*bufObj
;
1079 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1081 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1082 "glGetBufferSubDataARB" );
1084 /* error already recorded */
1088 ASSERT(ctx
->Driver
.GetBufferSubData
);
1089 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
1094 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1096 GET_CURRENT_CONTEXT(ctx
);
1097 struct gl_buffer_object
* bufObj
;
1098 GLbitfield accessFlags
;
1101 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1104 case GL_READ_ONLY_ARB
:
1105 accessFlags
= GL_MAP_READ_BIT
;
1107 case GL_WRITE_ONLY_ARB
:
1108 accessFlags
= GL_MAP_WRITE_BIT
;
1110 case GL_READ_WRITE_ARB
:
1111 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1114 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1118 bufObj
= get_buffer(ctx
, "glMapBufferARB", target
);
1122 if (_mesa_bufferobj_mapped(bufObj
)) {
1123 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1127 if (!bufObj
->Size
) {
1128 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1129 "glMapBuffer(buffer size = 0)");
1133 ASSERT(ctx
->Driver
.MapBufferRange
);
1134 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
);
1136 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1140 /* The driver callback should have set these fields.
1141 * This is important because other modules (like VBO) might call
1142 * the driver function directly.
1144 ASSERT(bufObj
->Pointer
== map
);
1145 ASSERT(bufObj
->Length
== bufObj
->Size
);
1146 ASSERT(bufObj
->Offset
== 0);
1147 bufObj
->AccessFlags
= accessFlags
;
1150 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1151 bufObj
->Written
= GL_TRUE
;
1154 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1155 bufObj
->Name
, bufObj
->Size
, access
);
1156 if (access
== GL_WRITE_ONLY_ARB
) {
1158 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1159 for (i
= 0; i
< bufObj
->Size
; i
++)
1166 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1168 /* buffer is 100 bytes larger than requested, fill with magic value */
1169 for (i
= 0; i
< 100; i
++) {
1170 buf
[bufObj
->Size
- i
- 1] = 123;
1175 return bufObj
->Pointer
;
1179 GLboolean GLAPIENTRY
1180 _mesa_UnmapBufferARB(GLenum target
)
1182 GET_CURRENT_CONTEXT(ctx
);
1183 struct gl_buffer_object
*bufObj
;
1184 GLboolean status
= GL_TRUE
;
1185 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1187 bufObj
= get_buffer(ctx
, "glUnmapBufferARB", target
);
1191 if (!_mesa_bufferobj_mapped(bufObj
)) {
1192 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1197 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1198 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1200 /* check that last 100 bytes are still = magic value */
1201 for (i
= 0; i
< 100; i
++) {
1202 GLuint pos
= bufObj
->Size
- i
- 1;
1203 if (buf
[pos
] != 123) {
1204 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1205 " at position %d (value = %u)\n",
1213 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1214 GLuint i
, unchanged
= 0;
1215 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1217 /* check which bytes changed */
1218 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1219 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1226 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1227 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1232 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1233 bufObj
->AccessFlags
= default_access_mode(ctx
);
1234 ASSERT(bufObj
->Pointer
== NULL
);
1235 ASSERT(bufObj
->Offset
== 0);
1236 ASSERT(bufObj
->Length
== 0);
1243 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1245 GET_CURRENT_CONTEXT(ctx
);
1246 struct gl_buffer_object
*bufObj
;
1247 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1249 bufObj
= get_buffer(ctx
, "glGetBufferParameterivARB", target
);
1254 case GL_BUFFER_SIZE_ARB
:
1255 *params
= (GLint
) bufObj
->Size
;
1257 case GL_BUFFER_USAGE_ARB
:
1258 *params
= bufObj
->Usage
;
1260 case GL_BUFFER_ACCESS_ARB
:
1261 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1263 case GL_BUFFER_MAPPED_ARB
:
1264 *params
= _mesa_bufferobj_mapped(bufObj
);
1266 case GL_BUFFER_ACCESS_FLAGS
:
1267 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1269 *params
= bufObj
->AccessFlags
;
1271 case GL_BUFFER_MAP_OFFSET
:
1272 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1274 *params
= (GLint
) bufObj
->Offset
;
1276 case GL_BUFFER_MAP_LENGTH
:
1277 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1279 *params
= (GLint
) bufObj
->Length
;
1282 ; /* fall-through */
1286 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1287 _mesa_lookup_enum_by_nr(pname
));
1293 * This is pretty much a duplicate of GetBufferParameteriv() but the
1294 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1297 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1299 GET_CURRENT_CONTEXT(ctx
);
1300 struct gl_buffer_object
*bufObj
;
1301 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1303 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
);
1308 case GL_BUFFER_SIZE_ARB
:
1309 *params
= bufObj
->Size
;
1311 case GL_BUFFER_USAGE_ARB
:
1312 *params
= bufObj
->Usage
;
1314 case GL_BUFFER_ACCESS_ARB
:
1315 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1317 case GL_BUFFER_ACCESS_FLAGS
:
1318 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1320 *params
= bufObj
->AccessFlags
;
1322 case GL_BUFFER_MAPPED_ARB
:
1323 *params
= _mesa_bufferobj_mapped(bufObj
);
1325 case GL_BUFFER_MAP_OFFSET
:
1326 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1328 *params
= bufObj
->Offset
;
1330 case GL_BUFFER_MAP_LENGTH
:
1331 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1333 *params
= bufObj
->Length
;
1336 ; /* fall-through */
1340 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1341 _mesa_lookup_enum_by_nr(pname
));
1346 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1348 GET_CURRENT_CONTEXT(ctx
);
1349 struct gl_buffer_object
* bufObj
;
1350 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1352 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1353 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1357 bufObj
= get_buffer(ctx
, "glGetBufferPointervARB", target
);
1361 *params
= bufObj
->Pointer
;
1366 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1367 GLintptr readOffset
, GLintptr writeOffset
,
1370 GET_CURRENT_CONTEXT(ctx
);
1371 struct gl_buffer_object
*src
, *dst
;
1372 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1374 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
);
1378 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
);
1382 if (_mesa_bufferobj_mapped(src
)) {
1383 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1384 "glCopyBufferSubData(readBuffer is mapped)");
1388 if (_mesa_bufferobj_mapped(dst
)) {
1389 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1390 "glCopyBufferSubData(writeBuffer is mapped)");
1394 if (readOffset
< 0) {
1395 _mesa_error(ctx
, GL_INVALID_VALUE
,
1396 "glCopyBufferSubData(readOffset = %d)", (int) readOffset
);
1400 if (writeOffset
< 0) {
1401 _mesa_error(ctx
, GL_INVALID_VALUE
,
1402 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset
);
1407 _mesa_error(ctx
, GL_INVALID_VALUE
,
1408 "glCopyBufferSubData(writeOffset = %d)", (int) size
);
1412 if (readOffset
+ size
> src
->Size
) {
1413 _mesa_error(ctx
, GL_INVALID_VALUE
,
1414 "glCopyBufferSubData(readOffset + size = %d)",
1415 (int) (readOffset
+ size
));
1419 if (writeOffset
+ size
> dst
->Size
) {
1420 _mesa_error(ctx
, GL_INVALID_VALUE
,
1421 "glCopyBufferSubData(writeOffset + size = %d)",
1422 (int) (writeOffset
+ size
));
1427 if (readOffset
+ size
<= writeOffset
) {
1430 else if (writeOffset
+ size
<= readOffset
) {
1434 /* overlapping src/dst is illegal */
1435 _mesa_error(ctx
, GL_INVALID_VALUE
,
1436 "glCopyBufferSubData(overlapping src/dst)");
1441 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1446 * See GL_ARB_map_buffer_range spec
1449 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1452 GET_CURRENT_CONTEXT(ctx
);
1453 struct gl_buffer_object
*bufObj
;
1456 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1458 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1459 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1460 "glMapBufferRange(extension not supported)");
1465 _mesa_error(ctx
, GL_INVALID_VALUE
,
1466 "glMapBufferRange(offset = %ld)", (long)offset
);
1471 _mesa_error(ctx
, GL_INVALID_VALUE
,
1472 "glMapBufferRange(length = %ld)", (long)length
);
1476 if (access
& ~(GL_MAP_READ_BIT
|
1478 GL_MAP_INVALIDATE_RANGE_BIT
|
1479 GL_MAP_INVALIDATE_BUFFER_BIT
|
1480 GL_MAP_FLUSH_EXPLICIT_BIT
|
1481 GL_MAP_UNSYNCHRONIZED_BIT
)) {
1482 /* generate an error if any undefind bit is set */
1483 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1487 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1488 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1489 "glMapBufferRange(access indicates neither read or write)");
1493 if ((access
& GL_MAP_READ_BIT
) &&
1494 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1495 GL_MAP_INVALIDATE_BUFFER_BIT
|
1496 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1497 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1498 "glMapBufferRange(invalid access flags)");
1502 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1503 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1504 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1505 "glMapBufferRange(invalid access flags)");
1509 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
);
1513 if (offset
+ length
> bufObj
->Size
) {
1514 _mesa_error(ctx
, GL_INVALID_VALUE
,
1515 "glMapBufferRange(offset + length > size)");
1519 if (_mesa_bufferobj_mapped(bufObj
)) {
1520 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1521 "glMapBufferRange(buffer already mapped)");
1525 if (!bufObj
->Size
) {
1526 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1527 "glMapBufferRange(buffer size = 0)");
1531 /* Mapping zero bytes should return a non-null pointer. */
1533 static long dummy
= 0;
1534 bufObj
->Pointer
= &dummy
;
1535 bufObj
->Length
= length
;
1536 bufObj
->Offset
= offset
;
1537 bufObj
->AccessFlags
= access
;
1538 return bufObj
->Pointer
;
1541 ASSERT(ctx
->Driver
.MapBufferRange
);
1542 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
);
1544 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1547 /* The driver callback should have set all these fields.
1548 * This is important because other modules (like VBO) might call
1549 * the driver function directly.
1551 ASSERT(bufObj
->Pointer
== map
);
1552 ASSERT(bufObj
->Length
== length
);
1553 ASSERT(bufObj
->Offset
== offset
);
1554 ASSERT(bufObj
->AccessFlags
== access
);
1562 * See GL_ARB_map_buffer_range spec
1565 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1567 GET_CURRENT_CONTEXT(ctx
);
1568 struct gl_buffer_object
*bufObj
;
1569 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1571 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1572 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1573 "glFlushMappedBufferRange(extension not supported)");
1578 _mesa_error(ctx
, GL_INVALID_VALUE
,
1579 "glFlushMappedBufferRange(offset = %ld)", (long)offset
);
1584 _mesa_error(ctx
, GL_INVALID_VALUE
,
1585 "glFlushMappedBufferRange(length = %ld)", (long)length
);
1589 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
);
1593 if (!_mesa_bufferobj_mapped(bufObj
)) {
1594 /* buffer is not mapped */
1595 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1596 "glFlushMappedBufferRange(buffer is not mapped)");
1600 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1601 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1602 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1606 if (offset
+ length
> bufObj
->Length
) {
1607 _mesa_error(ctx
, GL_INVALID_VALUE
,
1608 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1609 (long)offset
, (long)length
, (long)bufObj
->Length
);
1613 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1615 if (ctx
->Driver
.FlushMappedBufferRange
)
1616 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
);
1620 #if FEATURE_APPLE_object_purgeable
1622 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1624 struct gl_buffer_object
*bufObj
;
1627 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1629 _mesa_error(ctx
, GL_INVALID_VALUE
,
1630 "glObjectPurgeable(name = 0x%x)", name
);
1633 if (!_mesa_is_bufferobj(bufObj
)) {
1634 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1638 if (bufObj
->Purgeable
) {
1639 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1640 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1641 return GL_VOLATILE_APPLE
;
1644 bufObj
->Purgeable
= GL_TRUE
;
1646 retval
= GL_VOLATILE_APPLE
;
1647 if (ctx
->Driver
.BufferObjectPurgeable
)
1648 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1655 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1657 struct gl_renderbuffer
*bufObj
;
1660 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1662 _mesa_error(ctx
, GL_INVALID_VALUE
,
1663 "glObjectUnpurgeable(name = 0x%x)", name
);
1667 if (bufObj
->Purgeable
) {
1668 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1669 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1670 return GL_VOLATILE_APPLE
;
1673 bufObj
->Purgeable
= GL_TRUE
;
1675 retval
= GL_VOLATILE_APPLE
;
1676 if (ctx
->Driver
.RenderObjectPurgeable
)
1677 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1684 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1686 struct gl_texture_object
*bufObj
;
1689 bufObj
= _mesa_lookup_texture(ctx
, name
);
1691 _mesa_error(ctx
, GL_INVALID_VALUE
,
1692 "glObjectPurgeable(name = 0x%x)", name
);
1696 if (bufObj
->Purgeable
) {
1697 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1698 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1699 return GL_VOLATILE_APPLE
;
1702 bufObj
->Purgeable
= GL_TRUE
;
1704 retval
= GL_VOLATILE_APPLE
;
1705 if (ctx
->Driver
.TextureObjectPurgeable
)
1706 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1713 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1717 GET_CURRENT_CONTEXT(ctx
);
1718 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1721 _mesa_error(ctx
, GL_INVALID_VALUE
,
1722 "glObjectPurgeable(name = 0x%x)", name
);
1727 case GL_VOLATILE_APPLE
:
1728 case GL_RELEASED_APPLE
:
1732 _mesa_error(ctx
, GL_INVALID_ENUM
,
1733 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1738 switch (objectType
) {
1740 retval
= texture_object_purgeable(ctx
, name
, option
);
1742 case GL_RENDERBUFFER_EXT
:
1743 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1745 case GL_BUFFER_OBJECT_APPLE
:
1746 retval
= buffer_object_purgeable(ctx
, name
, option
);
1749 _mesa_error(ctx
, GL_INVALID_ENUM
,
1750 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1755 /* In strict conformance to the spec, we must only return VOLATILE when
1756 * when passed the VOLATILE option. Madness.
1758 * XXX First fix the spec, then fix me.
1760 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1765 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1767 struct gl_buffer_object
*bufObj
;
1770 bufObj
= _mesa_lookup_bufferobj(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
.BufferObjectUnpurgeable
)
1788 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1795 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1797 struct gl_renderbuffer
*bufObj
;
1800 bufObj
= _mesa_lookup_renderbuffer(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
.RenderObjectUnpurgeable
)
1818 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1825 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1827 struct gl_texture_object
*bufObj
;
1830 bufObj
= _mesa_lookup_texture(ctx
, name
);
1832 _mesa_error(ctx
, GL_INVALID_VALUE
,
1833 "glObjectUnpurgeable(name = 0x%x)", name
);
1837 if (! bufObj
->Purgeable
) {
1838 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1839 "glObjectUnpurgeable(name = 0x%x) object is"
1840 " already \"unpurged\"", name
);
1844 bufObj
->Purgeable
= GL_FALSE
;
1847 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1848 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1855 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1857 GET_CURRENT_CONTEXT(ctx
);
1858 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1861 _mesa_error(ctx
, GL_INVALID_VALUE
,
1862 "glObjectUnpurgeable(name = 0x%x)", name
);
1867 case GL_RETAINED_APPLE
:
1868 case GL_UNDEFINED_APPLE
:
1872 _mesa_error(ctx
, GL_INVALID_ENUM
,
1873 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1878 switch (objectType
) {
1879 case GL_BUFFER_OBJECT_APPLE
:
1880 return buffer_object_unpurgeable(ctx
, name
, option
);
1882 return texture_object_unpurgeable(ctx
, name
, option
);
1883 case GL_RENDERBUFFER_EXT
:
1884 return renderbuffer_unpurgeable(ctx
, name
, option
);
1886 _mesa_error(ctx
, GL_INVALID_ENUM
,
1887 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1895 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1896 GLenum pname
, GLint
*params
)
1898 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1900 _mesa_error(ctx
, GL_INVALID_VALUE
,
1901 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1906 case GL_PURGEABLE_APPLE
:
1907 *params
= bufObj
->Purgeable
;
1910 _mesa_error(ctx
, GL_INVALID_ENUM
,
1911 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1919 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1920 GLenum pname
, GLint
*params
)
1922 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1924 _mesa_error(ctx
, GL_INVALID_VALUE
,
1925 "glObjectUnpurgeable(name = 0x%x)", name
);
1930 case GL_PURGEABLE_APPLE
:
1931 *params
= rb
->Purgeable
;
1934 _mesa_error(ctx
, GL_INVALID_ENUM
,
1935 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1943 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1944 GLenum pname
, GLint
*params
)
1946 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1948 _mesa_error(ctx
, GL_INVALID_VALUE
,
1949 "glObjectUnpurgeable(name = 0x%x)", name
);
1954 case GL_PURGEABLE_APPLE
:
1955 *params
= texObj
->Purgeable
;
1958 _mesa_error(ctx
, GL_INVALID_ENUM
,
1959 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1967 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
1970 GET_CURRENT_CONTEXT(ctx
);
1973 _mesa_error(ctx
, GL_INVALID_VALUE
,
1974 "glGetObjectParameteriv(name = 0x%x)", name
);
1978 switch (objectType
) {
1980 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
1982 case GL_BUFFER_OBJECT_APPLE
:
1983 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
1985 case GL_RENDERBUFFER_EXT
:
1986 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
1989 _mesa_error(ctx
, GL_INVALID_ENUM
,
1990 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1995 #endif /* FEATURE_APPLE_object_purgeable */
1998 set_ubo_binding(struct gl_context
*ctx
,
2000 struct gl_buffer_object
*bufObj
,
2005 struct gl_uniform_buffer_binding
*binding
;
2007 binding
= &ctx
->UniformBufferBindings
[index
];
2008 if (binding
->BufferObject
== bufObj
&&
2009 binding
->Offset
== offset
&&
2010 binding
->Size
== size
&&
2011 binding
->AutomaticSize
== autoSize
) {
2015 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
2017 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2018 binding
->Offset
= offset
;
2019 binding
->Size
= size
;
2020 binding
->AutomaticSize
= autoSize
;
2024 * Bind a region of a buffer object to a uniform block binding point.
2025 * \param index the uniform buffer binding point index
2026 * \param bufObj the buffer object
2027 * \param offset offset to the start of buffer object region
2028 * \param size size of the buffer object region
2031 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2033 struct gl_buffer_object
*bufObj
,
2037 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2038 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2042 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2043 _mesa_error(ctx
, GL_INVALID_VALUE
,
2044 "glBindBufferRange(offset misalgned %d/%d)", (int) offset
,
2045 ctx
->Const
.UniformBufferOffsetAlignment
);
2049 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2054 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2055 set_ubo_binding(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2060 * Bind a buffer object to a uniform block binding point.
2061 * As above, but offset = 0.
2064 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2066 struct gl_buffer_object
*bufObj
)
2068 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2069 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2073 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2074 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2075 set_ubo_binding(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2077 set_ubo_binding(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2081 _mesa_BindBufferRange(GLenum target
, GLuint index
,
2082 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
2084 GET_CURRENT_CONTEXT(ctx
);
2085 struct gl_buffer_object
*bufObj
;
2088 bufObj
= ctx
->Shared
->NullBufferObj
;
2090 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2094 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2095 "glBindBufferRange(invalid buffer=%u)", buffer
);
2100 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
2105 if (offset
+ size
> bufObj
->Size
) {
2106 _mesa_error(ctx
, GL_INVALID_VALUE
,
2107 "glBindBufferRange(offset + size %d > buffer size %d)",
2108 (int) (offset
+ size
), (int) (bufObj
->Size
));
2113 case GL_TRANSFORM_FEEDBACK_BUFFER
:
2114 _mesa_bind_buffer_range_transform_feedback(ctx
, index
, bufObj
,
2117 case GL_UNIFORM_BUFFER
:
2118 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
2121 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
2127 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
2129 GET_CURRENT_CONTEXT(ctx
);
2130 struct gl_buffer_object
*bufObj
;
2133 bufObj
= ctx
->Shared
->NullBufferObj
;
2135 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2139 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2140 "glBindBufferBase(invalid buffer=%u)", buffer
);
2144 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
2145 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
2147 * "BindBufferBase is equivalent to calling BindBufferRange with offset
2148 * zero and size equal to the size of buffer."
2150 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
2152 * "If the parameter (starting offset or size) was not specified when the
2153 * buffer object was bound, zero is returned."
2155 * What happens if the size of the buffer changes? Does the size of the
2156 * buffer at the moment glBindBufferBase was called still play a role, like
2157 * the first quote would imply, or is the size meaningless in the
2158 * glBindBufferBase case like the second quote would suggest? The GL 4.1
2159 * core spec page 45 says:
2161 * "It is equivalent to calling BindBufferRange with offset zero, while
2162 * size is determined by the size of the bound buffer at the time the
2165 * My interpretation is that the GL 4.1 spec was a clarification of the
2166 * behavior, not a change. In particular, this choice will only make
2167 * rendering work in cases where it would have had undefined results.
2171 case GL_TRANSFORM_FEEDBACK_BUFFER
:
2172 _mesa_bind_buffer_base_transform_feedback(ctx
, index
, bufObj
);
2174 case GL_UNIFORM_BUFFER
:
2175 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
2178 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
2184 _mesa_init_bufferobj_dispatch(struct _glapi_table
*disp
)
2186 SET_BindBufferARB(disp
, _mesa_BindBufferARB
);
2187 SET_BufferDataARB(disp
, _mesa_BufferDataARB
);
2188 SET_BufferSubDataARB(disp
, _mesa_BufferSubDataARB
);
2189 SET_DeleteBuffersARB(disp
, _mesa_DeleteBuffersARB
);
2190 SET_GenBuffersARB(disp
, _mesa_GenBuffersARB
);
2191 SET_GetBufferParameterivARB(disp
, _mesa_GetBufferParameterivARB
);
2192 SET_GetBufferPointervARB(disp
, _mesa_GetBufferPointervARB
);
2193 SET_GetBufferSubDataARB(disp
, _mesa_GetBufferSubDataARB
);
2194 SET_IsBufferARB(disp
, _mesa_IsBufferARB
);
2195 SET_MapBufferARB(disp
, _mesa_MapBufferARB
);
2196 SET_UnmapBufferARB(disp
, _mesa_UnmapBufferARB
);
2197 SET_BindBufferRangeEXT(disp
, _mesa_BindBufferRange
);
2198 SET_BindBufferBaseEXT(disp
, _mesa_BindBufferBase
);