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 _mesa_is_gles(ctx
)
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
;
660 handle_bind_buffer_gen(struct gl_context
*ctx
,
663 struct gl_buffer_object
**buf_handle
)
665 struct gl_buffer_object
*buf
= *buf_handle
;
667 if (!buf
|| buf
== &DummyBufferObject
) {
668 /* If this is a new buffer object id, or one which was generated but
669 * never used before, allocate a buffer object now.
671 ASSERT(ctx
->Driver
.NewBufferObject
);
672 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
674 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
677 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
683 * Bind the specified target to buffer for the specified context.
684 * Called by glBindBuffer() and other functions.
687 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
689 struct gl_buffer_object
*oldBufObj
;
690 struct gl_buffer_object
*newBufObj
= NULL
;
691 struct gl_buffer_object
**bindTarget
= NULL
;
693 bindTarget
= get_buffer_target(ctx
, target
);
695 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
699 /* Get pointer to old buffer object (to be unbound) */
700 oldBufObj
= *bindTarget
;
701 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
702 return; /* rebinding the same buffer object- no change */
705 * Get pointer to new buffer object (newBufObj)
708 /* The spec says there's not a buffer object named 0, but we use
709 * one internally because it simplifies things.
711 newBufObj
= ctx
->Shared
->NullBufferObj
;
714 /* non-default buffer object */
715 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
716 handle_bind_buffer_gen(ctx
, target
, buffer
, &newBufObj
);
719 /* bind new buffer */
720 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
722 /* Pass BindBuffer call to device driver */
723 if (ctx
->Driver
.BindBuffer
)
724 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
729 * Update the default buffer objects in the given context to reference those
730 * specified in the shared state and release those referencing the old
734 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
736 /* Bind the NullBufferObj to remove references to those
737 * in the shared context hash table.
739 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
740 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
741 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
742 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
748 * Return the gl_buffer_object for the given ID.
749 * Always return NULL for ID 0.
751 struct gl_buffer_object
*
752 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
757 return (struct gl_buffer_object
*)
758 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
763 * If *ptr points to obj, set ptr = the Null/default buffer object.
764 * This is a helper for buffer object deletion.
765 * The GL spec says that deleting a buffer object causes it to get
766 * unbound from all arrays in the current context.
769 unbind(struct gl_context
*ctx
,
770 struct gl_buffer_object
**ptr
,
771 struct gl_buffer_object
*obj
)
774 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
780 * Plug default/fallback buffer object functions into the device
784 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
786 /* GL_ARB_vertex/pixel_buffer_object */
787 driver
->NewBufferObject
= _mesa_new_buffer_object
;
788 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
789 driver
->BindBuffer
= NULL
;
790 driver
->BufferData
= _mesa_buffer_data
;
791 driver
->BufferSubData
= _mesa_buffer_subdata
;
792 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
793 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
795 /* GL_ARB_map_buffer_range */
796 driver
->MapBufferRange
= _mesa_buffer_map_range
;
797 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
799 /* GL_ARB_copy_buffer */
800 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
805 /**********************************************************************/
807 /**********************************************************************/
810 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
812 GET_CURRENT_CONTEXT(ctx
);
813 ASSERT_OUTSIDE_BEGIN_END(ctx
);
815 if (MESA_VERBOSE
& VERBOSE_API
)
816 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
817 _mesa_lookup_enum_by_nr(target
), buffer
);
819 bind_buffer_object(ctx
, target
, buffer
);
824 * Delete a set of buffer objects.
826 * \param n Number of buffer objects to delete.
827 * \param ids Array of \c n buffer object IDs.
830 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
832 GET_CURRENT_CONTEXT(ctx
);
834 ASSERT_OUTSIDE_BEGIN_END(ctx
);
835 FLUSH_VERTICES(ctx
, 0);
838 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
842 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
844 for (i
= 0; i
< n
; i
++) {
845 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
847 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
850 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
852 if (_mesa_bufferobj_mapped(bufObj
)) {
853 /* if mapped, unmap it now */
854 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
855 bufObj
->AccessFlags
= default_access_mode(ctx
);
856 bufObj
->Pointer
= NULL
;
859 /* unbind any vertex pointers bound to this buffer */
860 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
861 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
864 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
865 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
867 if (arrayObj
->ElementArrayBufferObj
== bufObj
) {
868 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
871 /* unbind ARB_copy_buffer binding points */
872 if (ctx
->CopyReadBuffer
== bufObj
) {
873 _mesa_BindBufferARB( GL_COPY_READ_BUFFER
, 0 );
875 if (ctx
->CopyWriteBuffer
== bufObj
) {
876 _mesa_BindBufferARB( GL_COPY_WRITE_BUFFER
, 0 );
879 /* unbind transform feedback binding points */
880 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
881 _mesa_BindBufferARB( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
883 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
884 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
885 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
889 /* unbind UBO binding points */
890 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
891 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
892 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
896 if (ctx
->UniformBuffer
== bufObj
) {
897 _mesa_BindBufferARB( GL_UNIFORM_BUFFER
, 0 );
900 /* unbind any pixel pack/unpack pointers bound to this buffer */
901 if (ctx
->Pack
.BufferObj
== bufObj
) {
902 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
904 if (ctx
->Unpack
.BufferObj
== bufObj
) {
905 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
908 if (ctx
->Texture
.BufferObject
== bufObj
) {
909 _mesa_BindBufferARB( GL_TEXTURE_BUFFER
, 0 );
912 /* The ID is immediately freed for re-use */
913 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
914 /* Make sure we do not run into the classic ABA problem on bind.
915 * We don't want to allow re-binding a buffer object that's been
916 * "deleted" by glDeleteBuffers().
918 * The explicit rebinding to the default object in the current context
919 * prevents the above in the current context, but another context
920 * sharing the same objects might suffer from this problem.
921 * The alternative would be to do the hash lookup in any case on bind
922 * which would introduce more runtime overhead than this.
924 bufObj
->DeletePending
= GL_TRUE
;
925 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
929 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
934 * Generate a set of unique buffer object IDs and store them in \c buffer.
936 * \param n Number of IDs to generate.
937 * \param buffer Array of \c n locations to store the IDs.
940 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
942 GET_CURRENT_CONTEXT(ctx
);
945 ASSERT_OUTSIDE_BEGIN_END(ctx
);
947 if (MESA_VERBOSE
& VERBOSE_API
)
948 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
951 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
960 * This must be atomic (generation and allocation of buffer object IDs)
962 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
964 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
966 /* Insert the ID and pointer to dummy buffer object into hash table */
967 for (i
= 0; i
< n
; i
++) {
968 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
970 buffer
[i
] = first
+ i
;
973 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
978 * Determine if ID is the name of a buffer object.
980 * \param id ID of the potential buffer object.
981 * \return \c GL_TRUE if \c id is the name of a buffer object,
982 * \c GL_FALSE otherwise.
985 _mesa_IsBufferARB(GLuint id
)
987 struct gl_buffer_object
*bufObj
;
988 GET_CURRENT_CONTEXT(ctx
);
989 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
991 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
992 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
993 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
995 return bufObj
&& bufObj
!= &DummyBufferObject
;
1000 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
1001 const GLvoid
* data
, GLenum usage
)
1003 GET_CURRENT_CONTEXT(ctx
);
1004 struct gl_buffer_object
*bufObj
;
1005 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1007 if (MESA_VERBOSE
& VERBOSE_API
)
1008 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
1009 _mesa_lookup_enum_by_nr(target
),
1010 (long int) size
, data
,
1011 _mesa_lookup_enum_by_nr(usage
));
1014 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1019 case GL_STREAM_DRAW_ARB
:
1020 case GL_STREAM_READ_ARB
:
1021 case GL_STREAM_COPY_ARB
:
1022 case GL_STATIC_DRAW_ARB
:
1023 case GL_STATIC_READ_ARB
:
1024 case GL_STATIC_COPY_ARB
:
1025 case GL_DYNAMIC_DRAW_ARB
:
1026 case GL_DYNAMIC_READ_ARB
:
1027 case GL_DYNAMIC_COPY_ARB
:
1031 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
1035 bufObj
= get_buffer(ctx
, "glBufferDataARB", target
);
1039 if (_mesa_bufferobj_mapped(bufObj
)) {
1040 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1041 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
1042 bufObj
->AccessFlags
= default_access_mode(ctx
);
1043 ASSERT(bufObj
->Pointer
== NULL
);
1046 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1048 bufObj
->Written
= GL_TRUE
;
1051 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1052 bufObj
->Name
, size
, data
, usage
);
1059 ASSERT(ctx
->Driver
.BufferData
);
1060 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1061 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1067 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1068 GLsizeiptrARB size
, const GLvoid
* data
)
1070 GET_CURRENT_CONTEXT(ctx
);
1071 struct gl_buffer_object
*bufObj
;
1072 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1074 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1075 "glBufferSubDataARB" );
1077 /* error already recorded */
1084 bufObj
->Written
= GL_TRUE
;
1086 ASSERT(ctx
->Driver
.BufferSubData
);
1087 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
1092 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1093 GLsizeiptrARB size
, void * data
)
1095 GET_CURRENT_CONTEXT(ctx
);
1096 struct gl_buffer_object
*bufObj
;
1097 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1099 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1100 "glGetBufferSubDataARB" );
1102 /* error already recorded */
1106 ASSERT(ctx
->Driver
.GetBufferSubData
);
1107 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
1112 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1114 GET_CURRENT_CONTEXT(ctx
);
1115 struct gl_buffer_object
* bufObj
;
1116 GLbitfield accessFlags
;
1119 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1122 case GL_READ_ONLY_ARB
:
1123 accessFlags
= GL_MAP_READ_BIT
;
1125 case GL_WRITE_ONLY_ARB
:
1126 accessFlags
= GL_MAP_WRITE_BIT
;
1128 case GL_READ_WRITE_ARB
:
1129 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1132 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1136 bufObj
= get_buffer(ctx
, "glMapBufferARB", target
);
1140 if (_mesa_bufferobj_mapped(bufObj
)) {
1141 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1145 if (!bufObj
->Size
) {
1146 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1147 "glMapBuffer(buffer size = 0)");
1151 ASSERT(ctx
->Driver
.MapBufferRange
);
1152 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
);
1154 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1158 /* The driver callback should have set these fields.
1159 * This is important because other modules (like VBO) might call
1160 * the driver function directly.
1162 ASSERT(bufObj
->Pointer
== map
);
1163 ASSERT(bufObj
->Length
== bufObj
->Size
);
1164 ASSERT(bufObj
->Offset
== 0);
1165 bufObj
->AccessFlags
= accessFlags
;
1168 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1169 bufObj
->Written
= GL_TRUE
;
1172 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1173 bufObj
->Name
, bufObj
->Size
, access
);
1174 if (access
== GL_WRITE_ONLY_ARB
) {
1176 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1177 for (i
= 0; i
< bufObj
->Size
; i
++)
1184 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1186 /* buffer is 100 bytes larger than requested, fill with magic value */
1187 for (i
= 0; i
< 100; i
++) {
1188 buf
[bufObj
->Size
- i
- 1] = 123;
1193 return bufObj
->Pointer
;
1197 GLboolean GLAPIENTRY
1198 _mesa_UnmapBufferARB(GLenum target
)
1200 GET_CURRENT_CONTEXT(ctx
);
1201 struct gl_buffer_object
*bufObj
;
1202 GLboolean status
= GL_TRUE
;
1203 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1205 bufObj
= get_buffer(ctx
, "glUnmapBufferARB", target
);
1209 if (!_mesa_bufferobj_mapped(bufObj
)) {
1210 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1215 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1216 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1218 /* check that last 100 bytes are still = magic value */
1219 for (i
= 0; i
< 100; i
++) {
1220 GLuint pos
= bufObj
->Size
- i
- 1;
1221 if (buf
[pos
] != 123) {
1222 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1223 " at position %d (value = %u)\n",
1231 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1232 GLuint i
, unchanged
= 0;
1233 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1235 /* check which bytes changed */
1236 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1237 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1244 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1245 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1250 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1251 bufObj
->AccessFlags
= default_access_mode(ctx
);
1252 ASSERT(bufObj
->Pointer
== NULL
);
1253 ASSERT(bufObj
->Offset
== 0);
1254 ASSERT(bufObj
->Length
== 0);
1261 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1263 GET_CURRENT_CONTEXT(ctx
);
1264 struct gl_buffer_object
*bufObj
;
1265 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1267 bufObj
= get_buffer(ctx
, "glGetBufferParameterivARB", target
);
1272 case GL_BUFFER_SIZE_ARB
:
1273 *params
= (GLint
) bufObj
->Size
;
1275 case GL_BUFFER_USAGE_ARB
:
1276 *params
= bufObj
->Usage
;
1278 case GL_BUFFER_ACCESS_ARB
:
1279 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1281 case GL_BUFFER_MAPPED_ARB
:
1282 *params
= _mesa_bufferobj_mapped(bufObj
);
1284 case GL_BUFFER_ACCESS_FLAGS
:
1285 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1287 *params
= bufObj
->AccessFlags
;
1289 case GL_BUFFER_MAP_OFFSET
:
1290 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1292 *params
= (GLint
) bufObj
->Offset
;
1294 case GL_BUFFER_MAP_LENGTH
:
1295 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1297 *params
= (GLint
) bufObj
->Length
;
1300 ; /* fall-through */
1304 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1305 _mesa_lookup_enum_by_nr(pname
));
1311 * This is pretty much a duplicate of GetBufferParameteriv() but the
1312 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1315 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1317 GET_CURRENT_CONTEXT(ctx
);
1318 struct gl_buffer_object
*bufObj
;
1319 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1321 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
);
1326 case GL_BUFFER_SIZE_ARB
:
1327 *params
= bufObj
->Size
;
1329 case GL_BUFFER_USAGE_ARB
:
1330 *params
= bufObj
->Usage
;
1332 case GL_BUFFER_ACCESS_ARB
:
1333 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1335 case GL_BUFFER_ACCESS_FLAGS
:
1336 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1338 *params
= bufObj
->AccessFlags
;
1340 case GL_BUFFER_MAPPED_ARB
:
1341 *params
= _mesa_bufferobj_mapped(bufObj
);
1343 case GL_BUFFER_MAP_OFFSET
:
1344 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1346 *params
= bufObj
->Offset
;
1348 case GL_BUFFER_MAP_LENGTH
:
1349 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1351 *params
= bufObj
->Length
;
1354 ; /* fall-through */
1358 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1359 _mesa_lookup_enum_by_nr(pname
));
1364 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1366 GET_CURRENT_CONTEXT(ctx
);
1367 struct gl_buffer_object
* bufObj
;
1368 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1370 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1371 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1375 bufObj
= get_buffer(ctx
, "glGetBufferPointervARB", target
);
1379 *params
= bufObj
->Pointer
;
1384 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1385 GLintptr readOffset
, GLintptr writeOffset
,
1388 GET_CURRENT_CONTEXT(ctx
);
1389 struct gl_buffer_object
*src
, *dst
;
1390 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1392 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
);
1396 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
);
1400 if (_mesa_bufferobj_mapped(src
)) {
1401 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1402 "glCopyBufferSubData(readBuffer is mapped)");
1406 if (_mesa_bufferobj_mapped(dst
)) {
1407 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1408 "glCopyBufferSubData(writeBuffer is mapped)");
1412 if (readOffset
< 0) {
1413 _mesa_error(ctx
, GL_INVALID_VALUE
,
1414 "glCopyBufferSubData(readOffset = %d)", (int) readOffset
);
1418 if (writeOffset
< 0) {
1419 _mesa_error(ctx
, GL_INVALID_VALUE
,
1420 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset
);
1425 _mesa_error(ctx
, GL_INVALID_VALUE
,
1426 "glCopyBufferSubData(writeOffset = %d)", (int) size
);
1430 if (readOffset
+ size
> src
->Size
) {
1431 _mesa_error(ctx
, GL_INVALID_VALUE
,
1432 "glCopyBufferSubData(readOffset + size = %d)",
1433 (int) (readOffset
+ size
));
1437 if (writeOffset
+ size
> dst
->Size
) {
1438 _mesa_error(ctx
, GL_INVALID_VALUE
,
1439 "glCopyBufferSubData(writeOffset + size = %d)",
1440 (int) (writeOffset
+ size
));
1445 if (readOffset
+ size
<= writeOffset
) {
1448 else if (writeOffset
+ size
<= readOffset
) {
1452 /* overlapping src/dst is illegal */
1453 _mesa_error(ctx
, GL_INVALID_VALUE
,
1454 "glCopyBufferSubData(overlapping src/dst)");
1459 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1464 * See GL_ARB_map_buffer_range spec
1467 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1470 GET_CURRENT_CONTEXT(ctx
);
1471 struct gl_buffer_object
*bufObj
;
1474 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1476 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1477 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1478 "glMapBufferRange(extension not supported)");
1483 _mesa_error(ctx
, GL_INVALID_VALUE
,
1484 "glMapBufferRange(offset = %ld)", (long)offset
);
1489 _mesa_error(ctx
, GL_INVALID_VALUE
,
1490 "glMapBufferRange(length = %ld)", (long)length
);
1494 if (access
& ~(GL_MAP_READ_BIT
|
1496 GL_MAP_INVALIDATE_RANGE_BIT
|
1497 GL_MAP_INVALIDATE_BUFFER_BIT
|
1498 GL_MAP_FLUSH_EXPLICIT_BIT
|
1499 GL_MAP_UNSYNCHRONIZED_BIT
)) {
1500 /* generate an error if any undefind bit is set */
1501 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1505 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1506 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1507 "glMapBufferRange(access indicates neither read or write)");
1511 if ((access
& GL_MAP_READ_BIT
) &&
1512 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1513 GL_MAP_INVALIDATE_BUFFER_BIT
|
1514 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1515 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1516 "glMapBufferRange(invalid access flags)");
1520 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1521 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1522 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1523 "glMapBufferRange(invalid access flags)");
1527 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
);
1531 if (offset
+ length
> bufObj
->Size
) {
1532 _mesa_error(ctx
, GL_INVALID_VALUE
,
1533 "glMapBufferRange(offset + length > size)");
1537 if (_mesa_bufferobj_mapped(bufObj
)) {
1538 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1539 "glMapBufferRange(buffer already mapped)");
1543 if (!bufObj
->Size
) {
1544 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1545 "glMapBufferRange(buffer size = 0)");
1549 /* Mapping zero bytes should return a non-null pointer. */
1551 static long dummy
= 0;
1552 bufObj
->Pointer
= &dummy
;
1553 bufObj
->Length
= length
;
1554 bufObj
->Offset
= offset
;
1555 bufObj
->AccessFlags
= access
;
1556 return bufObj
->Pointer
;
1559 ASSERT(ctx
->Driver
.MapBufferRange
);
1560 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
);
1562 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1565 /* The driver callback should have set all these fields.
1566 * This is important because other modules (like VBO) might call
1567 * the driver function directly.
1569 ASSERT(bufObj
->Pointer
== map
);
1570 ASSERT(bufObj
->Length
== length
);
1571 ASSERT(bufObj
->Offset
== offset
);
1572 ASSERT(bufObj
->AccessFlags
== access
);
1580 * See GL_ARB_map_buffer_range spec
1583 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1585 GET_CURRENT_CONTEXT(ctx
);
1586 struct gl_buffer_object
*bufObj
;
1587 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1589 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1590 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1591 "glFlushMappedBufferRange(extension not supported)");
1596 _mesa_error(ctx
, GL_INVALID_VALUE
,
1597 "glFlushMappedBufferRange(offset = %ld)", (long)offset
);
1602 _mesa_error(ctx
, GL_INVALID_VALUE
,
1603 "glFlushMappedBufferRange(length = %ld)", (long)length
);
1607 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
);
1611 if (!_mesa_bufferobj_mapped(bufObj
)) {
1612 /* buffer is not mapped */
1613 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1614 "glFlushMappedBufferRange(buffer is not mapped)");
1618 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1619 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1620 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1624 if (offset
+ length
> bufObj
->Length
) {
1625 _mesa_error(ctx
, GL_INVALID_VALUE
,
1626 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1627 (long)offset
, (long)length
, (long)bufObj
->Length
);
1631 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1633 if (ctx
->Driver
.FlushMappedBufferRange
)
1634 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
);
1638 #if FEATURE_APPLE_object_purgeable
1640 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1642 struct gl_buffer_object
*bufObj
;
1645 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1647 _mesa_error(ctx
, GL_INVALID_VALUE
,
1648 "glObjectPurgeable(name = 0x%x)", name
);
1651 if (!_mesa_is_bufferobj(bufObj
)) {
1652 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1656 if (bufObj
->Purgeable
) {
1657 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1658 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1659 return GL_VOLATILE_APPLE
;
1662 bufObj
->Purgeable
= GL_TRUE
;
1664 retval
= GL_VOLATILE_APPLE
;
1665 if (ctx
->Driver
.BufferObjectPurgeable
)
1666 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1673 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1675 struct gl_renderbuffer
*bufObj
;
1678 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1680 _mesa_error(ctx
, GL_INVALID_VALUE
,
1681 "glObjectUnpurgeable(name = 0x%x)", name
);
1685 if (bufObj
->Purgeable
) {
1686 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1687 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1688 return GL_VOLATILE_APPLE
;
1691 bufObj
->Purgeable
= GL_TRUE
;
1693 retval
= GL_VOLATILE_APPLE
;
1694 if (ctx
->Driver
.RenderObjectPurgeable
)
1695 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1702 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1704 struct gl_texture_object
*bufObj
;
1707 bufObj
= _mesa_lookup_texture(ctx
, name
);
1709 _mesa_error(ctx
, GL_INVALID_VALUE
,
1710 "glObjectPurgeable(name = 0x%x)", name
);
1714 if (bufObj
->Purgeable
) {
1715 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1716 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1717 return GL_VOLATILE_APPLE
;
1720 bufObj
->Purgeable
= GL_TRUE
;
1722 retval
= GL_VOLATILE_APPLE
;
1723 if (ctx
->Driver
.TextureObjectPurgeable
)
1724 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1731 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1735 GET_CURRENT_CONTEXT(ctx
);
1736 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1739 _mesa_error(ctx
, GL_INVALID_VALUE
,
1740 "glObjectPurgeable(name = 0x%x)", name
);
1745 case GL_VOLATILE_APPLE
:
1746 case GL_RELEASED_APPLE
:
1750 _mesa_error(ctx
, GL_INVALID_ENUM
,
1751 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1756 switch (objectType
) {
1758 retval
= texture_object_purgeable(ctx
, name
, option
);
1760 case GL_RENDERBUFFER_EXT
:
1761 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1763 case GL_BUFFER_OBJECT_APPLE
:
1764 retval
= buffer_object_purgeable(ctx
, name
, option
);
1767 _mesa_error(ctx
, GL_INVALID_ENUM
,
1768 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1773 /* In strict conformance to the spec, we must only return VOLATILE when
1774 * when passed the VOLATILE option. Madness.
1776 * XXX First fix the spec, then fix me.
1778 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1783 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1785 struct gl_buffer_object
*bufObj
;
1788 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1790 _mesa_error(ctx
, GL_INVALID_VALUE
,
1791 "glObjectUnpurgeable(name = 0x%x)", name
);
1795 if (! bufObj
->Purgeable
) {
1796 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1797 "glObjectUnpurgeable(name = 0x%x) object is "
1798 " already \"unpurged\"", name
);
1802 bufObj
->Purgeable
= GL_FALSE
;
1805 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1806 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1813 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1815 struct gl_renderbuffer
*bufObj
;
1818 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1820 _mesa_error(ctx
, GL_INVALID_VALUE
,
1821 "glObjectUnpurgeable(name = 0x%x)", name
);
1825 if (! bufObj
->Purgeable
) {
1826 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1827 "glObjectUnpurgeable(name = 0x%x) object is "
1828 " already \"unpurged\"", name
);
1832 bufObj
->Purgeable
= GL_FALSE
;
1835 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1836 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1843 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1845 struct gl_texture_object
*bufObj
;
1848 bufObj
= _mesa_lookup_texture(ctx
, name
);
1850 _mesa_error(ctx
, GL_INVALID_VALUE
,
1851 "glObjectUnpurgeable(name = 0x%x)", name
);
1855 if (! bufObj
->Purgeable
) {
1856 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1857 "glObjectUnpurgeable(name = 0x%x) object is"
1858 " already \"unpurged\"", name
);
1862 bufObj
->Purgeable
= GL_FALSE
;
1865 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1866 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1873 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1875 GET_CURRENT_CONTEXT(ctx
);
1876 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1879 _mesa_error(ctx
, GL_INVALID_VALUE
,
1880 "glObjectUnpurgeable(name = 0x%x)", name
);
1885 case GL_RETAINED_APPLE
:
1886 case GL_UNDEFINED_APPLE
:
1890 _mesa_error(ctx
, GL_INVALID_ENUM
,
1891 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1896 switch (objectType
) {
1897 case GL_BUFFER_OBJECT_APPLE
:
1898 return buffer_object_unpurgeable(ctx
, name
, option
);
1900 return texture_object_unpurgeable(ctx
, name
, option
);
1901 case GL_RENDERBUFFER_EXT
:
1902 return renderbuffer_unpurgeable(ctx
, name
, option
);
1904 _mesa_error(ctx
, GL_INVALID_ENUM
,
1905 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1913 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1914 GLenum pname
, GLint
*params
)
1916 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1918 _mesa_error(ctx
, GL_INVALID_VALUE
,
1919 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1924 case GL_PURGEABLE_APPLE
:
1925 *params
= bufObj
->Purgeable
;
1928 _mesa_error(ctx
, GL_INVALID_ENUM
,
1929 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1937 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1938 GLenum pname
, GLint
*params
)
1940 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1942 _mesa_error(ctx
, GL_INVALID_VALUE
,
1943 "glObjectUnpurgeable(name = 0x%x)", name
);
1948 case GL_PURGEABLE_APPLE
:
1949 *params
= rb
->Purgeable
;
1952 _mesa_error(ctx
, GL_INVALID_ENUM
,
1953 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1961 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1962 GLenum pname
, GLint
*params
)
1964 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1966 _mesa_error(ctx
, GL_INVALID_VALUE
,
1967 "glObjectUnpurgeable(name = 0x%x)", name
);
1972 case GL_PURGEABLE_APPLE
:
1973 *params
= texObj
->Purgeable
;
1976 _mesa_error(ctx
, GL_INVALID_ENUM
,
1977 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1985 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
1988 GET_CURRENT_CONTEXT(ctx
);
1991 _mesa_error(ctx
, GL_INVALID_VALUE
,
1992 "glGetObjectParameteriv(name = 0x%x)", name
);
1996 switch (objectType
) {
1998 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
2000 case GL_BUFFER_OBJECT_APPLE
:
2001 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
2003 case GL_RENDERBUFFER_EXT
:
2004 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
2007 _mesa_error(ctx
, GL_INVALID_ENUM
,
2008 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2013 #endif /* FEATURE_APPLE_object_purgeable */
2016 set_ubo_binding(struct gl_context
*ctx
,
2018 struct gl_buffer_object
*bufObj
,
2023 struct gl_uniform_buffer_binding
*binding
;
2025 binding
= &ctx
->UniformBufferBindings
[index
];
2026 if (binding
->BufferObject
== bufObj
&&
2027 binding
->Offset
== offset
&&
2028 binding
->Size
== size
&&
2029 binding
->AutomaticSize
== autoSize
) {
2033 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
2035 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2036 binding
->Offset
= offset
;
2037 binding
->Size
= size
;
2038 binding
->AutomaticSize
= autoSize
;
2042 * Bind a region of a buffer object to a uniform block binding point.
2043 * \param index the uniform buffer binding point index
2044 * \param bufObj the buffer object
2045 * \param offset offset to the start of buffer object region
2046 * \param size size of the buffer object region
2049 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2051 struct gl_buffer_object
*bufObj
,
2055 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2056 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2060 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2061 _mesa_error(ctx
, GL_INVALID_VALUE
,
2062 "glBindBufferRange(offset misalgned %d/%d)", (int) offset
,
2063 ctx
->Const
.UniformBufferOffsetAlignment
);
2067 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2072 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2073 set_ubo_binding(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2078 * Bind a buffer object to a uniform block binding point.
2079 * As above, but offset = 0.
2082 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2084 struct gl_buffer_object
*bufObj
)
2086 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2087 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2091 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2092 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2093 set_ubo_binding(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2095 set_ubo_binding(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2099 _mesa_BindBufferRange(GLenum target
, GLuint index
,
2100 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
2102 GET_CURRENT_CONTEXT(ctx
);
2103 struct gl_buffer_object
*bufObj
;
2106 bufObj
= ctx
->Shared
->NullBufferObj
;
2108 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2110 handle_bind_buffer_gen(ctx
, target
, buffer
, &bufObj
);
2113 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2114 "glBindBufferRange(invalid buffer=%u)", buffer
);
2119 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
2124 if (offset
+ size
> bufObj
->Size
) {
2125 _mesa_error(ctx
, GL_INVALID_VALUE
,
2126 "glBindBufferRange(offset + size %d > buffer size %d)",
2127 (int) (offset
+ size
), (int) (bufObj
->Size
));
2132 case GL_TRANSFORM_FEEDBACK_BUFFER
:
2133 _mesa_bind_buffer_range_transform_feedback(ctx
, index
, bufObj
,
2136 case GL_UNIFORM_BUFFER
:
2137 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
2140 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
2146 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
2148 GET_CURRENT_CONTEXT(ctx
);
2149 struct gl_buffer_object
*bufObj
;
2152 bufObj
= ctx
->Shared
->NullBufferObj
;
2154 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2156 handle_bind_buffer_gen(ctx
, target
, buffer
, &bufObj
);
2159 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2160 "glBindBufferBase(invalid buffer=%u)", buffer
);
2164 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
2165 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
2167 * "BindBufferBase is equivalent to calling BindBufferRange with offset
2168 * zero and size equal to the size of buffer."
2170 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
2172 * "If the parameter (starting offset or size) was not specified when the
2173 * buffer object was bound, zero is returned."
2175 * What happens if the size of the buffer changes? Does the size of the
2176 * buffer at the moment glBindBufferBase was called still play a role, like
2177 * the first quote would imply, or is the size meaningless in the
2178 * glBindBufferBase case like the second quote would suggest? The GL 4.1
2179 * core spec page 45 says:
2181 * "It is equivalent to calling BindBufferRange with offset zero, while
2182 * size is determined by the size of the bound buffer at the time the
2185 * My interpretation is that the GL 4.1 spec was a clarification of the
2186 * behavior, not a change. In particular, this choice will only make
2187 * rendering work in cases where it would have had undefined results.
2191 case GL_TRANSFORM_FEEDBACK_BUFFER
:
2192 _mesa_bind_buffer_base_transform_feedback(ctx
, index
, bufObj
);
2194 case GL_UNIFORM_BUFFER
:
2195 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
2198 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
2204 _mesa_init_bufferobj_dispatch(struct _glapi_table
*disp
)
2206 SET_BindBufferARB(disp
, _mesa_BindBufferARB
);
2207 SET_BufferDataARB(disp
, _mesa_BufferDataARB
);
2208 SET_BufferSubDataARB(disp
, _mesa_BufferSubDataARB
);
2209 SET_DeleteBuffersARB(disp
, _mesa_DeleteBuffersARB
);
2210 SET_GenBuffersARB(disp
, _mesa_GenBuffersARB
);
2211 SET_GetBufferParameterivARB(disp
, _mesa_GetBufferParameterivARB
);
2212 SET_GetBufferPointervARB(disp
, _mesa_GetBufferPointervARB
);
2213 SET_GetBufferSubDataARB(disp
, _mesa_GetBufferSubDataARB
);
2214 SET_IsBufferARB(disp
, _mesa_IsBufferARB
);
2215 SET_MapBufferARB(disp
, _mesa_MapBufferARB
);
2216 SET_UnmapBufferARB(disp
, _mesa_UnmapBufferARB
);
2217 SET_BindBufferRangeEXT(disp
, _mesa_BindBufferRange
);
2218 SET_BindBufferBaseEXT(disp
, _mesa_BindBufferBase
);