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 * THE AUTHORS 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"
44 #include "transformfeedback.h"
50 /*#define BOUNDS_CHECK*/
54 * Used as a placeholder for buffer objects between glGenBuffers() and
55 * glBindBuffer() so that glIsBuffer() can work correctly.
57 static struct gl_buffer_object DummyBufferObject
;
61 * Return pointer to address of a buffer object target.
62 * \param ctx the GL context
63 * \param target the buffer object target to be retrieved.
64 * \return pointer to pointer to the buffer object bound to \c target in the
65 * specified context or \c NULL if \c target is invalid.
67 static inline struct gl_buffer_object
**
68 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
70 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
72 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
73 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
77 case GL_ARRAY_BUFFER_ARB
:
78 return &ctx
->Array
.ArrayBufferObj
;
79 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
80 return &ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
81 case GL_PIXEL_PACK_BUFFER_EXT
:
82 return &ctx
->Pack
.BufferObj
;
83 case GL_PIXEL_UNPACK_BUFFER_EXT
:
84 return &ctx
->Unpack
.BufferObj
;
85 case GL_COPY_READ_BUFFER
:
86 return &ctx
->CopyReadBuffer
;
87 case GL_COPY_WRITE_BUFFER
:
88 return &ctx
->CopyWriteBuffer
;
89 case GL_TRANSFORM_FEEDBACK_BUFFER
:
90 if (ctx
->Extensions
.EXT_transform_feedback
) {
91 return &ctx
->TransformFeedback
.CurrentBuffer
;
94 case GL_TEXTURE_BUFFER
:
95 if (ctx
->API
== API_OPENGL_CORE
&&
96 ctx
->Extensions
.ARB_texture_buffer_object
) {
97 return &ctx
->Texture
.BufferObject
;
100 case GL_UNIFORM_BUFFER
:
101 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
102 return &ctx
->UniformBuffer
;
113 * Get the buffer object bound to the specified target in a GL context.
114 * \param ctx the GL context
115 * \param target the buffer object target to be retrieved.
116 * \return pointer to the buffer object bound to \c target in the
117 * specified context or \c NULL if \c target is invalid.
119 static inline struct gl_buffer_object
*
120 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
)
122 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
125 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
129 if (!_mesa_is_bufferobj(*bufObj
)) {
130 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(buffer 0)", func
);
139 * Convert a GLbitfield describing the mapped buffer access flags
140 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
143 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
145 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
146 if ((access
& rwFlags
) == rwFlags
)
147 return GL_READ_WRITE
;
148 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
150 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
151 return GL_WRITE_ONLY
;
153 /* Otherwise, AccessFlags is zero (the default state).
155 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
157 * Name Type Initial Value Legal Values
159 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
162 * However, table 6.8 in the GL_OES_mapbuffer extension says:
164 * Get Value Type Get Command Value Description
165 * --------- ---- ----------- ----- -----------
166 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
168 * The difference is because GL_OES_mapbuffer only supports mapping buffers
173 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
178 * Tests the subdata range parameters and sets the GL error code for
179 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
181 * \param ctx GL context.
182 * \param target Buffer object target on which to operate.
183 * \param offset Offset of the first byte of the subdata range.
184 * \param size Size, in bytes, of the subdata range.
185 * \param caller Name of calling function for recording errors.
186 * \return A pointer to the buffer object bound to \c target in the
187 * specified context or \c NULL if any of the parameter or state
188 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
191 * \sa glBufferSubDataARB, glGetBufferSubDataARB
193 static struct gl_buffer_object
*
194 buffer_object_subdata_range_good( struct gl_context
* ctx
, GLenum target
,
195 GLintptrARB offset
, GLsizeiptrARB size
,
198 struct gl_buffer_object
*bufObj
;
201 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
206 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
210 bufObj
= get_buffer(ctx
, caller
, target
);
214 if (offset
+ size
> bufObj
->Size
) {
215 _mesa_error(ctx
, GL_INVALID_VALUE
,
216 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
217 (unsigned long) offset
,
218 (unsigned long) size
,
219 (unsigned long) bufObj
->Size
);
222 if (_mesa_bufferobj_mapped(bufObj
)) {
223 /* Buffer is currently mapped */
224 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
233 * Allocate and initialize a new buffer object.
235 * Default callback for the \c dd_function_table::NewBufferObject() hook.
237 static struct gl_buffer_object
*
238 _mesa_new_buffer_object( struct gl_context
*ctx
, GLuint name
, GLenum target
)
240 struct gl_buffer_object
*obj
;
244 obj
= MALLOC_STRUCT(gl_buffer_object
);
245 _mesa_initialize_buffer_object(ctx
, obj
, name
, target
);
251 * Delete a buffer object.
253 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
256 _mesa_delete_buffer_object(struct gl_context
*ctx
,
257 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
= 0;
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
&& ctx
->API
== API_OPENGL_CORE
) {
668 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindBuffer(non-gen name)");
672 if (!buf
|| buf
== &DummyBufferObject
) {
673 /* If this is a new buffer object id, or one which was generated but
674 * never used before, allocate a buffer object now.
676 ASSERT(ctx
->Driver
.NewBufferObject
);
677 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
679 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
682 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
690 * Bind the specified target to buffer for the specified context.
691 * Called by glBindBuffer() and other functions.
694 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
696 struct gl_buffer_object
*oldBufObj
;
697 struct gl_buffer_object
*newBufObj
= NULL
;
698 struct gl_buffer_object
**bindTarget
= NULL
;
700 bindTarget
= get_buffer_target(ctx
, target
);
702 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
706 /* Get pointer to old buffer object (to be unbound) */
707 oldBufObj
= *bindTarget
;
708 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
709 return; /* rebinding the same buffer object- no change */
712 * Get pointer to new buffer object (newBufObj)
715 /* The spec says there's not a buffer object named 0, but we use
716 * one internally because it simplifies things.
718 newBufObj
= ctx
->Shared
->NullBufferObj
;
721 /* non-default buffer object */
722 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
723 if (!handle_bind_buffer_gen(ctx
, target
, buffer
, &newBufObj
))
727 /* bind new buffer */
728 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
730 /* Pass BindBuffer call to device driver */
731 if (ctx
->Driver
.BindBuffer
)
732 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
737 * Update the default buffer objects in the given context to reference those
738 * specified in the shared state and release those referencing the old
742 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
744 /* Bind the NullBufferObj to remove references to those
745 * in the shared context hash table.
747 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
748 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
749 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
750 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
756 * Return the gl_buffer_object for the given ID.
757 * Always return NULL for ID 0.
759 struct gl_buffer_object
*
760 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
765 return (struct gl_buffer_object
*)
766 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
771 * If *ptr points to obj, set ptr = the Null/default buffer object.
772 * This is a helper for buffer object deletion.
773 * The GL spec says that deleting a buffer object causes it to get
774 * unbound from all arrays in the current context.
777 unbind(struct gl_context
*ctx
,
778 struct gl_buffer_object
**ptr
,
779 struct gl_buffer_object
*obj
)
782 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
788 * Plug default/fallback buffer object functions into the device
792 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
794 /* GL_ARB_vertex/pixel_buffer_object */
795 driver
->NewBufferObject
= _mesa_new_buffer_object
;
796 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
797 driver
->BindBuffer
= NULL
;
798 driver
->BufferData
= _mesa_buffer_data
;
799 driver
->BufferSubData
= _mesa_buffer_subdata
;
800 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
801 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
803 /* GL_ARB_map_buffer_range */
804 driver
->MapBufferRange
= _mesa_buffer_map_range
;
805 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
807 /* GL_ARB_copy_buffer */
808 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
813 /**********************************************************************/
815 /**********************************************************************/
818 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
820 GET_CURRENT_CONTEXT(ctx
);
822 if (MESA_VERBOSE
& VERBOSE_API
)
823 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
824 _mesa_lookup_enum_by_nr(target
), buffer
);
826 bind_buffer_object(ctx
, target
, buffer
);
831 * Delete a set of buffer objects.
833 * \param n Number of buffer objects to delete.
834 * \param ids Array of \c n buffer object IDs.
837 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
839 GET_CURRENT_CONTEXT(ctx
);
841 FLUSH_VERTICES(ctx
, 0);
844 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
848 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
850 for (i
= 0; i
< n
; i
++) {
851 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
853 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
856 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
858 if (_mesa_bufferobj_mapped(bufObj
)) {
859 /* if mapped, unmap it now */
860 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
861 bufObj
->AccessFlags
= 0;
862 bufObj
->Pointer
= NULL
;
865 /* unbind any vertex pointers bound to this buffer */
866 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
867 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
870 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
871 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
873 if (arrayObj
->ElementArrayBufferObj
== bufObj
) {
874 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
877 /* unbind ARB_copy_buffer binding points */
878 if (ctx
->CopyReadBuffer
== bufObj
) {
879 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
881 if (ctx
->CopyWriteBuffer
== bufObj
) {
882 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
885 /* unbind transform feedback binding points */
886 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
887 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
889 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
890 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
891 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
895 /* unbind UBO binding points */
896 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
897 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
898 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
902 if (ctx
->UniformBuffer
== bufObj
) {
903 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
906 /* unbind any pixel pack/unpack pointers bound to this buffer */
907 if (ctx
->Pack
.BufferObj
== bufObj
) {
908 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
910 if (ctx
->Unpack
.BufferObj
== bufObj
) {
911 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
914 if (ctx
->Texture
.BufferObject
== bufObj
) {
915 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
918 /* The ID is immediately freed for re-use */
919 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
920 /* Make sure we do not run into the classic ABA problem on bind.
921 * We don't want to allow re-binding a buffer object that's been
922 * "deleted" by glDeleteBuffers().
924 * The explicit rebinding to the default object in the current context
925 * prevents the above in the current context, but another context
926 * sharing the same objects might suffer from this problem.
927 * The alternative would be to do the hash lookup in any case on bind
928 * which would introduce more runtime overhead than this.
930 bufObj
->DeletePending
= GL_TRUE
;
931 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
935 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
940 * Generate a set of unique buffer object IDs and store them in \c buffer.
942 * \param n Number of IDs to generate.
943 * \param buffer Array of \c n locations to store the IDs.
946 _mesa_GenBuffers(GLsizei n
, GLuint
*buffer
)
948 GET_CURRENT_CONTEXT(ctx
);
952 if (MESA_VERBOSE
& VERBOSE_API
)
953 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
956 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
965 * This must be atomic (generation and allocation of buffer object IDs)
967 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
969 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
971 /* Insert the ID and pointer to dummy buffer object into hash table */
972 for (i
= 0; i
< n
; i
++) {
973 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
975 buffer
[i
] = first
+ i
;
978 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
983 * Determine if ID is the name of a buffer object.
985 * \param id ID of the potential buffer object.
986 * \return \c GL_TRUE if \c id is the name of a buffer object,
987 * \c GL_FALSE otherwise.
990 _mesa_IsBuffer(GLuint id
)
992 struct gl_buffer_object
*bufObj
;
993 GET_CURRENT_CONTEXT(ctx
);
994 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
996 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
997 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
998 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1000 return bufObj
&& bufObj
!= &DummyBufferObject
;
1005 _mesa_BufferData(GLenum target
, GLsizeiptrARB size
,
1006 const GLvoid
* data
, GLenum usage
)
1008 GET_CURRENT_CONTEXT(ctx
);
1009 struct gl_buffer_object
*bufObj
;
1012 if (MESA_VERBOSE
& VERBOSE_API
)
1013 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
1014 _mesa_lookup_enum_by_nr(target
),
1015 (long int) size
, data
,
1016 _mesa_lookup_enum_by_nr(usage
));
1019 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1024 case GL_STREAM_DRAW_ARB
:
1025 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1028 case GL_STATIC_DRAW_ARB
:
1029 case GL_DYNAMIC_DRAW_ARB
:
1033 case GL_STREAM_READ_ARB
:
1034 case GL_STREAM_COPY_ARB
:
1035 case GL_STATIC_READ_ARB
:
1036 case GL_STATIC_COPY_ARB
:
1037 case GL_DYNAMIC_READ_ARB
:
1038 case GL_DYNAMIC_COPY_ARB
:
1039 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1043 valid_usage
= false;
1048 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferData(usage)");
1052 bufObj
= get_buffer(ctx
, "glBufferDataARB", target
);
1056 if (_mesa_bufferobj_mapped(bufObj
)) {
1057 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1058 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
1059 bufObj
->AccessFlags
= 0;
1060 ASSERT(bufObj
->Pointer
== NULL
);
1063 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1065 bufObj
->Written
= GL_TRUE
;
1068 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1069 bufObj
->Name
, size
, data
, usage
);
1076 ASSERT(ctx
->Driver
.BufferData
);
1077 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1078 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1084 _mesa_BufferSubData(GLenum target
, GLintptrARB offset
,
1085 GLsizeiptrARB size
, const GLvoid
* data
)
1087 GET_CURRENT_CONTEXT(ctx
);
1088 struct gl_buffer_object
*bufObj
;
1090 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1091 "glBufferSubDataARB" );
1093 /* error already recorded */
1100 bufObj
->Written
= GL_TRUE
;
1102 ASSERT(ctx
->Driver
.BufferSubData
);
1103 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
1108 _mesa_GetBufferSubData(GLenum target
, GLintptrARB offset
,
1109 GLsizeiptrARB size
, void * data
)
1111 GET_CURRENT_CONTEXT(ctx
);
1112 struct gl_buffer_object
*bufObj
;
1114 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1115 "glGetBufferSubDataARB" );
1117 /* error already recorded */
1121 ASSERT(ctx
->Driver
.GetBufferSubData
);
1122 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
1127 _mesa_MapBuffer(GLenum target
, GLenum access
)
1129 GET_CURRENT_CONTEXT(ctx
);
1130 struct gl_buffer_object
* bufObj
;
1131 GLbitfield accessFlags
;
1135 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1138 case GL_READ_ONLY_ARB
:
1139 accessFlags
= GL_MAP_READ_BIT
;
1140 valid_access
= _mesa_is_desktop_gl(ctx
);
1142 case GL_WRITE_ONLY_ARB
:
1143 accessFlags
= GL_MAP_WRITE_BIT
;
1144 valid_access
= true;
1146 case GL_READ_WRITE_ARB
:
1147 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1148 valid_access
= _mesa_is_desktop_gl(ctx
);
1151 valid_access
= false;
1155 if (!valid_access
) {
1156 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1160 bufObj
= get_buffer(ctx
, "glMapBufferARB", target
);
1164 if (_mesa_bufferobj_mapped(bufObj
)) {
1165 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1169 if (!bufObj
->Size
) {
1170 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1171 "glMapBuffer(buffer size = 0)");
1175 ASSERT(ctx
->Driver
.MapBufferRange
);
1176 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
);
1178 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1182 /* The driver callback should have set these fields.
1183 * This is important because other modules (like VBO) might call
1184 * the driver function directly.
1186 ASSERT(bufObj
->Pointer
== map
);
1187 ASSERT(bufObj
->Length
== bufObj
->Size
);
1188 ASSERT(bufObj
->Offset
== 0);
1189 bufObj
->AccessFlags
= accessFlags
;
1192 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1193 bufObj
->Written
= GL_TRUE
;
1196 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1197 bufObj
->Name
, bufObj
->Size
, access
);
1198 if (access
== GL_WRITE_ONLY_ARB
) {
1200 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1201 for (i
= 0; i
< bufObj
->Size
; i
++)
1208 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1210 /* buffer is 100 bytes larger than requested, fill with magic value */
1211 for (i
= 0; i
< 100; i
++) {
1212 buf
[bufObj
->Size
- i
- 1] = 123;
1217 return bufObj
->Pointer
;
1221 GLboolean GLAPIENTRY
1222 _mesa_UnmapBuffer(GLenum target
)
1224 GET_CURRENT_CONTEXT(ctx
);
1225 struct gl_buffer_object
*bufObj
;
1226 GLboolean status
= GL_TRUE
;
1227 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1229 bufObj
= get_buffer(ctx
, "glUnmapBufferARB", target
);
1233 if (!_mesa_bufferobj_mapped(bufObj
)) {
1234 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1239 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1240 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1242 /* check that last 100 bytes are still = magic value */
1243 for (i
= 0; i
< 100; i
++) {
1244 GLuint pos
= bufObj
->Size
- i
- 1;
1245 if (buf
[pos
] != 123) {
1246 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1247 " at position %d (value = %u)\n",
1255 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1256 GLuint i
, unchanged
= 0;
1257 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1259 /* check which bytes changed */
1260 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1261 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1268 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1269 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1274 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1275 bufObj
->AccessFlags
= 0;
1276 ASSERT(bufObj
->Pointer
== NULL
);
1277 ASSERT(bufObj
->Offset
== 0);
1278 ASSERT(bufObj
->Length
== 0);
1285 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1287 GET_CURRENT_CONTEXT(ctx
);
1288 struct gl_buffer_object
*bufObj
;
1290 bufObj
= get_buffer(ctx
, "glGetBufferParameterivARB", target
);
1295 case GL_BUFFER_SIZE_ARB
:
1296 *params
= (GLint
) bufObj
->Size
;
1298 case GL_BUFFER_USAGE_ARB
:
1299 *params
= bufObj
->Usage
;
1301 case GL_BUFFER_ACCESS_ARB
:
1302 *params
= simplified_access_mode(ctx
, bufObj
->AccessFlags
);
1304 case GL_BUFFER_MAPPED_ARB
:
1305 *params
= _mesa_bufferobj_mapped(bufObj
);
1307 case GL_BUFFER_ACCESS_FLAGS
:
1308 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1310 *params
= bufObj
->AccessFlags
;
1312 case GL_BUFFER_MAP_OFFSET
:
1313 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1315 *params
= (GLint
) bufObj
->Offset
;
1317 case GL_BUFFER_MAP_LENGTH
:
1318 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1320 *params
= (GLint
) bufObj
->Length
;
1323 ; /* fall-through */
1327 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1328 _mesa_lookup_enum_by_nr(pname
));
1334 * This is pretty much a duplicate of GetBufferParameteriv() but the
1335 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1338 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1340 GET_CURRENT_CONTEXT(ctx
);
1341 struct gl_buffer_object
*bufObj
;
1343 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
);
1348 case GL_BUFFER_SIZE_ARB
:
1349 *params
= bufObj
->Size
;
1351 case GL_BUFFER_USAGE_ARB
:
1352 *params
= bufObj
->Usage
;
1354 case GL_BUFFER_ACCESS_ARB
:
1355 *params
= simplified_access_mode(ctx
, bufObj
->AccessFlags
);
1357 case GL_BUFFER_ACCESS_FLAGS
:
1358 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1360 *params
= bufObj
->AccessFlags
;
1362 case GL_BUFFER_MAPPED_ARB
:
1363 *params
= _mesa_bufferobj_mapped(bufObj
);
1365 case GL_BUFFER_MAP_OFFSET
:
1366 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1368 *params
= bufObj
->Offset
;
1370 case GL_BUFFER_MAP_LENGTH
:
1371 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1373 *params
= bufObj
->Length
;
1376 ; /* fall-through */
1380 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1381 _mesa_lookup_enum_by_nr(pname
));
1386 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
1388 GET_CURRENT_CONTEXT(ctx
);
1389 struct gl_buffer_object
* bufObj
;
1391 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1392 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1396 bufObj
= get_buffer(ctx
, "glGetBufferPointervARB", target
);
1400 *params
= bufObj
->Pointer
;
1405 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1406 GLintptr readOffset
, GLintptr writeOffset
,
1409 GET_CURRENT_CONTEXT(ctx
);
1410 struct gl_buffer_object
*src
, *dst
;
1412 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
);
1416 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
);
1420 if (_mesa_bufferobj_mapped(src
)) {
1421 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1422 "glCopyBufferSubData(readBuffer is mapped)");
1426 if (_mesa_bufferobj_mapped(dst
)) {
1427 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1428 "glCopyBufferSubData(writeBuffer is mapped)");
1432 if (readOffset
< 0) {
1433 _mesa_error(ctx
, GL_INVALID_VALUE
,
1434 "glCopyBufferSubData(readOffset = %d)", (int) readOffset
);
1438 if (writeOffset
< 0) {
1439 _mesa_error(ctx
, GL_INVALID_VALUE
,
1440 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset
);
1445 _mesa_error(ctx
, GL_INVALID_VALUE
,
1446 "glCopyBufferSubData(writeOffset = %d)", (int) size
);
1450 if (readOffset
+ size
> src
->Size
) {
1451 _mesa_error(ctx
, GL_INVALID_VALUE
,
1452 "glCopyBufferSubData(readOffset + size = %d)",
1453 (int) (readOffset
+ size
));
1457 if (writeOffset
+ size
> dst
->Size
) {
1458 _mesa_error(ctx
, GL_INVALID_VALUE
,
1459 "glCopyBufferSubData(writeOffset + size = %d)",
1460 (int) (writeOffset
+ size
));
1465 if (readOffset
+ size
<= writeOffset
) {
1468 else if (writeOffset
+ size
<= readOffset
) {
1472 /* overlapping src/dst is illegal */
1473 _mesa_error(ctx
, GL_INVALID_VALUE
,
1474 "glCopyBufferSubData(overlapping src/dst)");
1479 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1484 * See GL_ARB_map_buffer_range spec
1487 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1490 GET_CURRENT_CONTEXT(ctx
);
1491 struct gl_buffer_object
*bufObj
;
1494 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1496 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1497 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1498 "glMapBufferRange(extension not supported)");
1503 _mesa_error(ctx
, GL_INVALID_VALUE
,
1504 "glMapBufferRange(offset = %ld)", (long)offset
);
1509 _mesa_error(ctx
, GL_INVALID_VALUE
,
1510 "glMapBufferRange(length = %ld)", (long)length
);
1514 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
1516 * "An INVALID_OPERATION error is generated for any of the following
1519 * * <length> is zero."
1521 if (_mesa_is_gles(ctx
) && length
== 0) {
1522 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1523 "glMapBufferRange(length = 0)");
1527 if (access
& ~(GL_MAP_READ_BIT
|
1529 GL_MAP_INVALIDATE_RANGE_BIT
|
1530 GL_MAP_INVALIDATE_BUFFER_BIT
|
1531 GL_MAP_FLUSH_EXPLICIT_BIT
|
1532 GL_MAP_UNSYNCHRONIZED_BIT
)) {
1533 /* generate an error if any undefind bit is set */
1534 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1538 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1539 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1540 "glMapBufferRange(access indicates neither read or write)");
1544 if ((access
& GL_MAP_READ_BIT
) &&
1545 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1546 GL_MAP_INVALIDATE_BUFFER_BIT
|
1547 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1548 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1549 "glMapBufferRange(invalid access flags)");
1553 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1554 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1555 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1556 "glMapBufferRange(invalid access flags)");
1560 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
);
1564 if (offset
+ length
> bufObj
->Size
) {
1565 _mesa_error(ctx
, GL_INVALID_VALUE
,
1566 "glMapBufferRange(offset + length > size)");
1570 if (_mesa_bufferobj_mapped(bufObj
)) {
1571 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1572 "glMapBufferRange(buffer already mapped)");
1576 if (!bufObj
->Size
) {
1577 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1578 "glMapBufferRange(buffer size = 0)");
1582 /* Mapping zero bytes should return a non-null pointer. */
1584 static long dummy
= 0;
1585 bufObj
->Pointer
= &dummy
;
1586 bufObj
->Length
= length
;
1587 bufObj
->Offset
= offset
;
1588 bufObj
->AccessFlags
= access
;
1589 return bufObj
->Pointer
;
1592 ASSERT(ctx
->Driver
.MapBufferRange
);
1593 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
);
1595 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1598 /* The driver callback should have set all these fields.
1599 * This is important because other modules (like VBO) might call
1600 * the driver function directly.
1602 ASSERT(bufObj
->Pointer
== map
);
1603 ASSERT(bufObj
->Length
== length
);
1604 ASSERT(bufObj
->Offset
== offset
);
1605 ASSERT(bufObj
->AccessFlags
== access
);
1613 * See GL_ARB_map_buffer_range spec
1616 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1618 GET_CURRENT_CONTEXT(ctx
);
1619 struct gl_buffer_object
*bufObj
;
1621 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1622 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1623 "glFlushMappedBufferRange(extension not supported)");
1628 _mesa_error(ctx
, GL_INVALID_VALUE
,
1629 "glFlushMappedBufferRange(offset = %ld)", (long)offset
);
1634 _mesa_error(ctx
, GL_INVALID_VALUE
,
1635 "glFlushMappedBufferRange(length = %ld)", (long)length
);
1639 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
);
1643 if (!_mesa_bufferobj_mapped(bufObj
)) {
1644 /* buffer is not mapped */
1645 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1646 "glFlushMappedBufferRange(buffer is not mapped)");
1650 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1651 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1652 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1656 if (offset
+ length
> bufObj
->Length
) {
1657 _mesa_error(ctx
, GL_INVALID_VALUE
,
1658 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1659 (long)offset
, (long)length
, (long)bufObj
->Length
);
1663 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1665 if (ctx
->Driver
.FlushMappedBufferRange
)
1666 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
);
1671 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1673 struct gl_buffer_object
*bufObj
;
1676 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1678 _mesa_error(ctx
, GL_INVALID_VALUE
,
1679 "glObjectPurgeable(name = 0x%x)", name
);
1682 if (!_mesa_is_bufferobj(bufObj
)) {
1683 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1687 if (bufObj
->Purgeable
) {
1688 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1689 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1690 return GL_VOLATILE_APPLE
;
1693 bufObj
->Purgeable
= GL_TRUE
;
1695 retval
= GL_VOLATILE_APPLE
;
1696 if (ctx
->Driver
.BufferObjectPurgeable
)
1697 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1704 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1706 struct gl_renderbuffer
*bufObj
;
1709 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1711 _mesa_error(ctx
, GL_INVALID_VALUE
,
1712 "glObjectUnpurgeable(name = 0x%x)", name
);
1716 if (bufObj
->Purgeable
) {
1717 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1718 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1719 return GL_VOLATILE_APPLE
;
1722 bufObj
->Purgeable
= GL_TRUE
;
1724 retval
= GL_VOLATILE_APPLE
;
1725 if (ctx
->Driver
.RenderObjectPurgeable
)
1726 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1733 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1735 struct gl_texture_object
*bufObj
;
1738 bufObj
= _mesa_lookup_texture(ctx
, name
);
1740 _mesa_error(ctx
, GL_INVALID_VALUE
,
1741 "glObjectPurgeable(name = 0x%x)", name
);
1745 if (bufObj
->Purgeable
) {
1746 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1747 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1748 return GL_VOLATILE_APPLE
;
1751 bufObj
->Purgeable
= GL_TRUE
;
1753 retval
= GL_VOLATILE_APPLE
;
1754 if (ctx
->Driver
.TextureObjectPurgeable
)
1755 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1762 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1766 GET_CURRENT_CONTEXT(ctx
);
1767 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1770 _mesa_error(ctx
, GL_INVALID_VALUE
,
1771 "glObjectPurgeable(name = 0x%x)", name
);
1776 case GL_VOLATILE_APPLE
:
1777 case GL_RELEASED_APPLE
:
1781 _mesa_error(ctx
, GL_INVALID_ENUM
,
1782 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1787 switch (objectType
) {
1789 retval
= texture_object_purgeable(ctx
, name
, option
);
1791 case GL_RENDERBUFFER_EXT
:
1792 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1794 case GL_BUFFER_OBJECT_APPLE
:
1795 retval
= buffer_object_purgeable(ctx
, name
, option
);
1798 _mesa_error(ctx
, GL_INVALID_ENUM
,
1799 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1804 /* In strict conformance to the spec, we must only return VOLATILE when
1805 * when passed the VOLATILE option. Madness.
1807 * XXX First fix the spec, then fix me.
1809 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1814 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1816 struct gl_buffer_object
*bufObj
;
1819 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1821 _mesa_error(ctx
, GL_INVALID_VALUE
,
1822 "glObjectUnpurgeable(name = 0x%x)", name
);
1826 if (! bufObj
->Purgeable
) {
1827 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1828 "glObjectUnpurgeable(name = 0x%x) object is "
1829 " already \"unpurged\"", name
);
1833 bufObj
->Purgeable
= GL_FALSE
;
1836 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1837 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1844 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1846 struct gl_renderbuffer
*bufObj
;
1849 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1851 _mesa_error(ctx
, GL_INVALID_VALUE
,
1852 "glObjectUnpurgeable(name = 0x%x)", name
);
1856 if (! bufObj
->Purgeable
) {
1857 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1858 "glObjectUnpurgeable(name = 0x%x) object is "
1859 " already \"unpurged\"", name
);
1863 bufObj
->Purgeable
= GL_FALSE
;
1866 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1867 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1874 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1876 struct gl_texture_object
*bufObj
;
1879 bufObj
= _mesa_lookup_texture(ctx
, name
);
1881 _mesa_error(ctx
, GL_INVALID_VALUE
,
1882 "glObjectUnpurgeable(name = 0x%x)", name
);
1886 if (! bufObj
->Purgeable
) {
1887 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1888 "glObjectUnpurgeable(name = 0x%x) object is"
1889 " already \"unpurged\"", name
);
1893 bufObj
->Purgeable
= GL_FALSE
;
1896 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1897 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1904 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1906 GET_CURRENT_CONTEXT(ctx
);
1907 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1910 _mesa_error(ctx
, GL_INVALID_VALUE
,
1911 "glObjectUnpurgeable(name = 0x%x)", name
);
1916 case GL_RETAINED_APPLE
:
1917 case GL_UNDEFINED_APPLE
:
1921 _mesa_error(ctx
, GL_INVALID_ENUM
,
1922 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1927 switch (objectType
) {
1928 case GL_BUFFER_OBJECT_APPLE
:
1929 return buffer_object_unpurgeable(ctx
, name
, option
);
1931 return texture_object_unpurgeable(ctx
, name
, option
);
1932 case GL_RENDERBUFFER_EXT
:
1933 return renderbuffer_unpurgeable(ctx
, name
, option
);
1935 _mesa_error(ctx
, GL_INVALID_ENUM
,
1936 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1944 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1945 GLenum pname
, GLint
*params
)
1947 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1949 _mesa_error(ctx
, GL_INVALID_VALUE
,
1950 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1955 case GL_PURGEABLE_APPLE
:
1956 *params
= bufObj
->Purgeable
;
1959 _mesa_error(ctx
, GL_INVALID_ENUM
,
1960 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1968 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1969 GLenum pname
, GLint
*params
)
1971 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1973 _mesa_error(ctx
, GL_INVALID_VALUE
,
1974 "glObjectUnpurgeable(name = 0x%x)", name
);
1979 case GL_PURGEABLE_APPLE
:
1980 *params
= rb
->Purgeable
;
1983 _mesa_error(ctx
, GL_INVALID_ENUM
,
1984 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1992 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1993 GLenum pname
, GLint
*params
)
1995 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1997 _mesa_error(ctx
, GL_INVALID_VALUE
,
1998 "glObjectUnpurgeable(name = 0x%x)", name
);
2003 case GL_PURGEABLE_APPLE
:
2004 *params
= texObj
->Purgeable
;
2007 _mesa_error(ctx
, GL_INVALID_ENUM
,
2008 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2016 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
2019 GET_CURRENT_CONTEXT(ctx
);
2022 _mesa_error(ctx
, GL_INVALID_VALUE
,
2023 "glGetObjectParameteriv(name = 0x%x)", name
);
2027 switch (objectType
) {
2029 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
2031 case GL_BUFFER_OBJECT_APPLE
:
2032 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
2034 case GL_RENDERBUFFER_EXT
:
2035 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
2038 _mesa_error(ctx
, GL_INVALID_ENUM
,
2039 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2045 set_ubo_binding(struct gl_context
*ctx
,
2047 struct gl_buffer_object
*bufObj
,
2052 struct gl_uniform_buffer_binding
*binding
;
2054 binding
= &ctx
->UniformBufferBindings
[index
];
2055 if (binding
->BufferObject
== bufObj
&&
2056 binding
->Offset
== offset
&&
2057 binding
->Size
== size
&&
2058 binding
->AutomaticSize
== autoSize
) {
2062 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
2064 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2065 binding
->Offset
= offset
;
2066 binding
->Size
= size
;
2067 binding
->AutomaticSize
= autoSize
;
2071 * Bind a region of a buffer object to a uniform block binding point.
2072 * \param index the uniform buffer binding point index
2073 * \param bufObj the buffer object
2074 * \param offset offset to the start of buffer object region
2075 * \param size size of the buffer object region
2078 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2080 struct gl_buffer_object
*bufObj
,
2084 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2085 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2089 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2090 _mesa_error(ctx
, GL_INVALID_VALUE
,
2091 "glBindBufferRange(offset misalgned %d/%d)", (int) offset
,
2092 ctx
->Const
.UniformBufferOffsetAlignment
);
2096 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2101 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2102 set_ubo_binding(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2107 * Bind a buffer object to a uniform block binding point.
2108 * As above, but offset = 0.
2111 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2113 struct gl_buffer_object
*bufObj
)
2115 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2116 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2120 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2121 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2122 set_ubo_binding(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2124 set_ubo_binding(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2128 _mesa_BindBufferRange(GLenum target
, GLuint index
,
2129 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
2131 GET_CURRENT_CONTEXT(ctx
);
2132 struct gl_buffer_object
*bufObj
;
2135 bufObj
= ctx
->Shared
->NullBufferObj
;
2137 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2139 if (!handle_bind_buffer_gen(ctx
, target
, buffer
, &bufObj
))
2143 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2144 "glBindBufferRange(invalid buffer=%u)", buffer
);
2150 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
2157 case GL_TRANSFORM_FEEDBACK_BUFFER
:
2158 _mesa_bind_buffer_range_transform_feedback(ctx
, index
, bufObj
,
2161 case GL_UNIFORM_BUFFER
:
2162 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
2165 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
2171 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
2173 GET_CURRENT_CONTEXT(ctx
);
2174 struct gl_buffer_object
*bufObj
;
2177 bufObj
= ctx
->Shared
->NullBufferObj
;
2179 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2181 if (!handle_bind_buffer_gen(ctx
, target
, buffer
, &bufObj
))
2185 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2186 "glBindBufferBase(invalid buffer=%u)", buffer
);
2190 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
2191 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
2193 * "BindBufferBase is equivalent to calling BindBufferRange with offset
2194 * zero and size equal to the size of buffer."
2196 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
2198 * "If the parameter (starting offset or size) was not specified when the
2199 * buffer object was bound, zero is returned."
2201 * What happens if the size of the buffer changes? Does the size of the
2202 * buffer at the moment glBindBufferBase was called still play a role, like
2203 * the first quote would imply, or is the size meaningless in the
2204 * glBindBufferBase case like the second quote would suggest? The GL 4.1
2205 * core spec page 45 says:
2207 * "It is equivalent to calling BindBufferRange with offset zero, while
2208 * size is determined by the size of the bound buffer at the time the
2211 * My interpretation is that the GL 4.1 spec was a clarification of the
2212 * behavior, not a change. In particular, this choice will only make
2213 * rendering work in cases where it would have had undefined results.
2217 case GL_TRANSFORM_FEEDBACK_BUFFER
:
2218 _mesa_bind_buffer_base_transform_feedback(ctx
, index
, bufObj
);
2220 case GL_UNIFORM_BUFFER
:
2221 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
2224 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
2230 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
2233 GET_CURRENT_CONTEXT(ctx
);
2234 struct gl_buffer_object
*bufObj
;
2235 const GLintptr end
= offset
+ length
;
2237 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2239 _mesa_error(ctx
, GL_INVALID_VALUE
,
2240 "glInvalidateBufferSubData(name = 0x%x) invalid object",
2245 /* The GL_ARB_invalidate_subdata spec says:
2247 * "An INVALID_VALUE error is generated if <offset> or <length> is
2248 * negative, or if <offset> + <length> is greater than the value of
2251 if (end
< 0 || end
> bufObj
->Size
) {
2252 _mesa_error(ctx
, GL_INVALID_VALUE
,
2253 "glInvalidateBufferSubData(invalid offset or length)");
2257 /* The GL_ARB_invalidate_subdata spec says:
2259 * "An INVALID_OPERATION error is generated if the buffer is currently
2260 * mapped by MapBuffer, or if the invalidate range intersects the range
2261 * currently mapped by MapBufferRange."
2263 if (_mesa_bufferobj_mapped(bufObj
)) {
2264 const GLintptr mapEnd
= bufObj
->Offset
+ bufObj
->Length
;
2266 /* The regions do not overlap if and only if the end of the discard
2267 * region is before the mapped region or the start of the discard region
2268 * is after the mapped region.
2270 * Note that 'end' and 'mapEnd' are the first byte *after* the discard
2271 * region and the mapped region, repsectively. It is okay for that byte
2272 * to be mapped (for 'end') or discarded (for 'mapEnd').
2274 if (!(end
<= bufObj
->Offset
|| offset
>= mapEnd
)) {
2275 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2276 "glInvalidateBufferSubData(intersection with mapped "
2282 /* We don't actually do anything for this yet. Just return after
2283 * validating the parameters and generating the required errors.
2289 _mesa_InvalidateBufferData(GLuint buffer
)
2291 GET_CURRENT_CONTEXT(ctx
);
2292 struct gl_buffer_object
*bufObj
;
2294 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2296 _mesa_error(ctx
, GL_INVALID_VALUE
,
2297 "glInvalidateBufferData(name = 0x%x) invalid object",
2302 /* The GL_ARB_invalidate_subdata spec says:
2304 * "An INVALID_OPERATION error is generated if the buffer is currently
2305 * mapped by MapBuffer, or if the invalidate range intersects the range
2306 * currently mapped by MapBufferRange."
2308 if (_mesa_bufferobj_mapped(bufObj
)) {
2309 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2310 "glInvalidateBufferData(intersection with mapped "
2315 /* We don't actually do anything for this yet. Just return after
2316 * validating the parameters and generating the required errors.