2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * 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 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
622 ctx
->Shared
->NullBufferObj
);
624 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
625 _mesa_reference_buffer_object(ctx
,
626 &ctx
->UniformBufferBindings
[i
].BufferObject
,
627 ctx
->Shared
->NullBufferObj
);
628 ctx
->UniformBufferBindings
[i
].Offset
= -1;
629 ctx
->UniformBufferBindings
[i
].Size
= -1;
635 _mesa_free_buffer_objects( struct gl_context
*ctx
)
639 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
641 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
642 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
644 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
646 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
647 _mesa_reference_buffer_object(ctx
,
648 &ctx
->UniformBufferBindings
[i
].BufferObject
,
654 handle_bind_buffer_gen(struct gl_context
*ctx
,
657 struct gl_buffer_object
**buf_handle
)
659 struct gl_buffer_object
*buf
= *buf_handle
;
661 if (!buf
&& ctx
->API
== API_OPENGL_CORE
) {
662 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBindBuffer(non-gen name)");
666 if (!buf
|| buf
== &DummyBufferObject
) {
667 /* If this is a new buffer object id, or one which was generated but
668 * never used before, allocate a buffer object now.
670 ASSERT(ctx
->Driver
.NewBufferObject
);
671 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
673 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
676 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
684 * Bind the specified target to buffer for the specified context.
685 * Called by glBindBuffer() and other functions.
688 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
690 struct gl_buffer_object
*oldBufObj
;
691 struct gl_buffer_object
*newBufObj
= NULL
;
692 struct gl_buffer_object
**bindTarget
= NULL
;
694 bindTarget
= get_buffer_target(ctx
, target
);
696 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
700 /* Get pointer to old buffer object (to be unbound) */
701 oldBufObj
= *bindTarget
;
702 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
703 return; /* rebinding the same buffer object- no change */
706 * Get pointer to new buffer object (newBufObj)
709 /* The spec says there's not a buffer object named 0, but we use
710 * one internally because it simplifies things.
712 newBufObj
= ctx
->Shared
->NullBufferObj
;
715 /* non-default buffer object */
716 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
717 if (!handle_bind_buffer_gen(ctx
, target
, buffer
, &newBufObj
))
721 /* bind new buffer */
722 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
724 /* Pass BindBuffer call to device driver */
725 if (ctx
->Driver
.BindBuffer
)
726 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
731 * Update the default buffer objects in the given context to reference those
732 * specified in the shared state and release those referencing the old
736 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
738 /* Bind the NullBufferObj to remove references to those
739 * in the shared context hash table.
741 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
742 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
743 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
744 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
750 * Return the gl_buffer_object for the given ID.
751 * Always return NULL for ID 0.
753 struct gl_buffer_object
*
754 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
759 return (struct gl_buffer_object
*)
760 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
765 * If *ptr points to obj, set ptr = the Null/default buffer object.
766 * This is a helper for buffer object deletion.
767 * The GL spec says that deleting a buffer object causes it to get
768 * unbound from all arrays in the current context.
771 unbind(struct gl_context
*ctx
,
772 struct gl_buffer_object
**ptr
,
773 struct gl_buffer_object
*obj
)
776 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
782 * Plug default/fallback buffer object functions into the device
786 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
788 /* GL_ARB_vertex/pixel_buffer_object */
789 driver
->NewBufferObject
= _mesa_new_buffer_object
;
790 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
791 driver
->BindBuffer
= NULL
;
792 driver
->BufferData
= _mesa_buffer_data
;
793 driver
->BufferSubData
= _mesa_buffer_subdata
;
794 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
795 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
797 /* GL_ARB_map_buffer_range */
798 driver
->MapBufferRange
= _mesa_buffer_map_range
;
799 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
801 /* GL_ARB_copy_buffer */
802 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
807 /**********************************************************************/
809 /**********************************************************************/
812 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
814 GET_CURRENT_CONTEXT(ctx
);
816 if (MESA_VERBOSE
& VERBOSE_API
)
817 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
818 _mesa_lookup_enum_by_nr(target
), buffer
);
820 bind_buffer_object(ctx
, target
, buffer
);
825 * Delete a set of buffer objects.
827 * \param n Number of buffer objects to delete.
828 * \param ids Array of \c n buffer object IDs.
831 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
833 GET_CURRENT_CONTEXT(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
= 0;
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_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
867 if (arrayObj
->ElementArrayBufferObj
== bufObj
) {
868 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
871 /* unbind ARB_copy_buffer binding points */
872 if (ctx
->CopyReadBuffer
== bufObj
) {
873 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
875 if (ctx
->CopyWriteBuffer
== bufObj
) {
876 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
879 /* unbind transform feedback binding points */
880 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
881 _mesa_BindBuffer( 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_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
900 /* unbind any pixel pack/unpack pointers bound to this buffer */
901 if (ctx
->Pack
.BufferObj
== bufObj
) {
902 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
904 if (ctx
->Unpack
.BufferObj
== bufObj
) {
905 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
908 if (ctx
->Texture
.BufferObject
== bufObj
) {
909 _mesa_BindBuffer( 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_GenBuffers(GLsizei n
, GLuint
*buffer
)
942 GET_CURRENT_CONTEXT(ctx
);
946 if (MESA_VERBOSE
& VERBOSE_API
)
947 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
950 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
959 * This must be atomic (generation and allocation of buffer object IDs)
961 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
963 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
965 /* Insert the ID and pointer to dummy buffer object into hash table */
966 for (i
= 0; i
< n
; i
++) {
967 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
969 buffer
[i
] = first
+ i
;
972 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
977 * Determine if ID is the name of a buffer object.
979 * \param id ID of the potential buffer object.
980 * \return \c GL_TRUE if \c id is the name of a buffer object,
981 * \c GL_FALSE otherwise.
984 _mesa_IsBuffer(GLuint id
)
986 struct gl_buffer_object
*bufObj
;
987 GET_CURRENT_CONTEXT(ctx
);
988 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
990 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
991 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
992 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
994 return bufObj
&& bufObj
!= &DummyBufferObject
;
999 _mesa_BufferData(GLenum target
, GLsizeiptrARB size
,
1000 const GLvoid
* data
, GLenum usage
)
1002 GET_CURRENT_CONTEXT(ctx
);
1003 struct gl_buffer_object
*bufObj
;
1006 if (MESA_VERBOSE
& VERBOSE_API
)
1007 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
1008 _mesa_lookup_enum_by_nr(target
),
1009 (long int) size
, data
,
1010 _mesa_lookup_enum_by_nr(usage
));
1013 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1018 case GL_STREAM_DRAW_ARB
:
1019 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1022 case GL_STATIC_DRAW_ARB
:
1023 case GL_DYNAMIC_DRAW_ARB
:
1027 case GL_STREAM_READ_ARB
:
1028 case GL_STREAM_COPY_ARB
:
1029 case GL_STATIC_READ_ARB
:
1030 case GL_STATIC_COPY_ARB
:
1031 case GL_DYNAMIC_READ_ARB
:
1032 case GL_DYNAMIC_COPY_ARB
:
1033 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1037 valid_usage
= false;
1042 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferData(usage)");
1046 bufObj
= get_buffer(ctx
, "glBufferDataARB", target
);
1050 if (_mesa_bufferobj_mapped(bufObj
)) {
1051 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1052 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
1053 bufObj
->AccessFlags
= 0;
1054 ASSERT(bufObj
->Pointer
== NULL
);
1057 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1059 bufObj
->Written
= GL_TRUE
;
1062 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1063 bufObj
->Name
, size
, data
, usage
);
1070 ASSERT(ctx
->Driver
.BufferData
);
1071 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1072 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1078 _mesa_BufferSubData(GLenum target
, GLintptrARB offset
,
1079 GLsizeiptrARB size
, const GLvoid
* data
)
1081 GET_CURRENT_CONTEXT(ctx
);
1082 struct gl_buffer_object
*bufObj
;
1084 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1085 "glBufferSubDataARB" );
1087 /* error already recorded */
1094 bufObj
->Written
= GL_TRUE
;
1096 ASSERT(ctx
->Driver
.BufferSubData
);
1097 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
1102 _mesa_GetBufferSubData(GLenum target
, GLintptrARB offset
,
1103 GLsizeiptrARB size
, void * data
)
1105 GET_CURRENT_CONTEXT(ctx
);
1106 struct gl_buffer_object
*bufObj
;
1108 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1109 "glGetBufferSubDataARB" );
1111 /* error already recorded */
1115 ASSERT(ctx
->Driver
.GetBufferSubData
);
1116 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
1121 _mesa_MapBuffer(GLenum target
, GLenum access
)
1123 GET_CURRENT_CONTEXT(ctx
);
1124 struct gl_buffer_object
* bufObj
;
1125 GLbitfield accessFlags
;
1129 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1132 case GL_READ_ONLY_ARB
:
1133 accessFlags
= GL_MAP_READ_BIT
;
1134 valid_access
= _mesa_is_desktop_gl(ctx
);
1136 case GL_WRITE_ONLY_ARB
:
1137 accessFlags
= GL_MAP_WRITE_BIT
;
1138 valid_access
= true;
1140 case GL_READ_WRITE_ARB
:
1141 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1142 valid_access
= _mesa_is_desktop_gl(ctx
);
1145 valid_access
= false;
1149 if (!valid_access
) {
1150 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1154 bufObj
= get_buffer(ctx
, "glMapBufferARB", target
);
1158 if (_mesa_bufferobj_mapped(bufObj
)) {
1159 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1163 if (!bufObj
->Size
) {
1164 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1165 "glMapBuffer(buffer size = 0)");
1169 ASSERT(ctx
->Driver
.MapBufferRange
);
1170 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
);
1172 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1176 /* The driver callback should have set these fields.
1177 * This is important because other modules (like VBO) might call
1178 * the driver function directly.
1180 ASSERT(bufObj
->Pointer
== map
);
1181 ASSERT(bufObj
->Length
== bufObj
->Size
);
1182 ASSERT(bufObj
->Offset
== 0);
1183 bufObj
->AccessFlags
= accessFlags
;
1186 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1187 bufObj
->Written
= GL_TRUE
;
1190 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1191 bufObj
->Name
, bufObj
->Size
, access
);
1192 if (access
== GL_WRITE_ONLY_ARB
) {
1194 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1195 for (i
= 0; i
< bufObj
->Size
; i
++)
1202 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1204 /* buffer is 100 bytes larger than requested, fill with magic value */
1205 for (i
= 0; i
< 100; i
++) {
1206 buf
[bufObj
->Size
- i
- 1] = 123;
1211 return bufObj
->Pointer
;
1215 GLboolean GLAPIENTRY
1216 _mesa_UnmapBuffer(GLenum target
)
1218 GET_CURRENT_CONTEXT(ctx
);
1219 struct gl_buffer_object
*bufObj
;
1220 GLboolean status
= GL_TRUE
;
1221 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1223 bufObj
= get_buffer(ctx
, "glUnmapBufferARB", target
);
1227 if (!_mesa_bufferobj_mapped(bufObj
)) {
1228 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1233 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1234 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1236 /* check that last 100 bytes are still = magic value */
1237 for (i
= 0; i
< 100; i
++) {
1238 GLuint pos
= bufObj
->Size
- i
- 1;
1239 if (buf
[pos
] != 123) {
1240 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1241 " at position %d (value = %u)\n",
1249 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1250 GLuint i
, unchanged
= 0;
1251 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1253 /* check which bytes changed */
1254 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1255 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1262 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1263 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1268 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1269 bufObj
->AccessFlags
= 0;
1270 ASSERT(bufObj
->Pointer
== NULL
);
1271 ASSERT(bufObj
->Offset
== 0);
1272 ASSERT(bufObj
->Length
== 0);
1279 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
1281 GET_CURRENT_CONTEXT(ctx
);
1282 struct gl_buffer_object
*bufObj
;
1284 bufObj
= get_buffer(ctx
, "glGetBufferParameterivARB", target
);
1289 case GL_BUFFER_SIZE_ARB
:
1290 *params
= (GLint
) bufObj
->Size
;
1292 case GL_BUFFER_USAGE_ARB
:
1293 *params
= bufObj
->Usage
;
1295 case GL_BUFFER_ACCESS_ARB
:
1296 *params
= simplified_access_mode(ctx
, bufObj
->AccessFlags
);
1298 case GL_BUFFER_MAPPED_ARB
:
1299 *params
= _mesa_bufferobj_mapped(bufObj
);
1301 case GL_BUFFER_ACCESS_FLAGS
:
1302 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1304 *params
= bufObj
->AccessFlags
;
1306 case GL_BUFFER_MAP_OFFSET
:
1307 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1309 *params
= (GLint
) bufObj
->Offset
;
1311 case GL_BUFFER_MAP_LENGTH
:
1312 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1314 *params
= (GLint
) bufObj
->Length
;
1317 ; /* fall-through */
1321 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1322 _mesa_lookup_enum_by_nr(pname
));
1328 * This is pretty much a duplicate of GetBufferParameteriv() but the
1329 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1332 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1334 GET_CURRENT_CONTEXT(ctx
);
1335 struct gl_buffer_object
*bufObj
;
1337 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
);
1342 case GL_BUFFER_SIZE_ARB
:
1343 *params
= bufObj
->Size
;
1345 case GL_BUFFER_USAGE_ARB
:
1346 *params
= bufObj
->Usage
;
1348 case GL_BUFFER_ACCESS_ARB
:
1349 *params
= simplified_access_mode(ctx
, bufObj
->AccessFlags
);
1351 case GL_BUFFER_ACCESS_FLAGS
:
1352 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1354 *params
= bufObj
->AccessFlags
;
1356 case GL_BUFFER_MAPPED_ARB
:
1357 *params
= _mesa_bufferobj_mapped(bufObj
);
1359 case GL_BUFFER_MAP_OFFSET
:
1360 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1362 *params
= bufObj
->Offset
;
1364 case GL_BUFFER_MAP_LENGTH
:
1365 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1367 *params
= bufObj
->Length
;
1370 ; /* fall-through */
1374 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1375 _mesa_lookup_enum_by_nr(pname
));
1380 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
1382 GET_CURRENT_CONTEXT(ctx
);
1383 struct gl_buffer_object
* bufObj
;
1385 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1386 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1390 bufObj
= get_buffer(ctx
, "glGetBufferPointervARB", target
);
1394 *params
= bufObj
->Pointer
;
1399 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1400 GLintptr readOffset
, GLintptr writeOffset
,
1403 GET_CURRENT_CONTEXT(ctx
);
1404 struct gl_buffer_object
*src
, *dst
;
1406 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
);
1410 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
);
1414 if (_mesa_bufferobj_mapped(src
)) {
1415 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1416 "glCopyBufferSubData(readBuffer is mapped)");
1420 if (_mesa_bufferobj_mapped(dst
)) {
1421 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1422 "glCopyBufferSubData(writeBuffer is mapped)");
1426 if (readOffset
< 0) {
1427 _mesa_error(ctx
, GL_INVALID_VALUE
,
1428 "glCopyBufferSubData(readOffset = %d)", (int) readOffset
);
1432 if (writeOffset
< 0) {
1433 _mesa_error(ctx
, GL_INVALID_VALUE
,
1434 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset
);
1439 _mesa_error(ctx
, GL_INVALID_VALUE
,
1440 "glCopyBufferSubData(writeOffset = %d)", (int) size
);
1444 if (readOffset
+ size
> src
->Size
) {
1445 _mesa_error(ctx
, GL_INVALID_VALUE
,
1446 "glCopyBufferSubData(readOffset + size = %d)",
1447 (int) (readOffset
+ size
));
1451 if (writeOffset
+ size
> dst
->Size
) {
1452 _mesa_error(ctx
, GL_INVALID_VALUE
,
1453 "glCopyBufferSubData(writeOffset + size = %d)",
1454 (int) (writeOffset
+ size
));
1459 if (readOffset
+ size
<= writeOffset
) {
1462 else if (writeOffset
+ size
<= readOffset
) {
1466 /* overlapping src/dst is illegal */
1467 _mesa_error(ctx
, GL_INVALID_VALUE
,
1468 "glCopyBufferSubData(overlapping src/dst)");
1473 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1478 * See GL_ARB_map_buffer_range spec
1481 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1484 GET_CURRENT_CONTEXT(ctx
);
1485 struct gl_buffer_object
*bufObj
;
1488 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1490 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1491 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1492 "glMapBufferRange(extension not supported)");
1497 _mesa_error(ctx
, GL_INVALID_VALUE
,
1498 "glMapBufferRange(offset = %ld)", (long)offset
);
1503 _mesa_error(ctx
, GL_INVALID_VALUE
,
1504 "glMapBufferRange(length = %ld)", (long)length
);
1508 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
1510 * "An INVALID_OPERATION error is generated for any of the following
1513 * * <length> is zero."
1515 if (_mesa_is_gles(ctx
) && length
== 0) {
1516 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1517 "glMapBufferRange(length = 0)");
1521 if (access
& ~(GL_MAP_READ_BIT
|
1523 GL_MAP_INVALIDATE_RANGE_BIT
|
1524 GL_MAP_INVALIDATE_BUFFER_BIT
|
1525 GL_MAP_FLUSH_EXPLICIT_BIT
|
1526 GL_MAP_UNSYNCHRONIZED_BIT
)) {
1527 /* generate an error if any undefind bit is set */
1528 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1532 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1533 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1534 "glMapBufferRange(access indicates neither read or write)");
1538 if ((access
& GL_MAP_READ_BIT
) &&
1539 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1540 GL_MAP_INVALIDATE_BUFFER_BIT
|
1541 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1542 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1543 "glMapBufferRange(invalid access flags)");
1547 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1548 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1549 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1550 "glMapBufferRange(invalid access flags)");
1554 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
);
1558 if (offset
+ length
> bufObj
->Size
) {
1559 _mesa_error(ctx
, GL_INVALID_VALUE
,
1560 "glMapBufferRange(offset + length > size)");
1564 if (_mesa_bufferobj_mapped(bufObj
)) {
1565 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1566 "glMapBufferRange(buffer already mapped)");
1570 if (!bufObj
->Size
) {
1571 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1572 "glMapBufferRange(buffer size = 0)");
1576 /* Mapping zero bytes should return a non-null pointer. */
1578 static long dummy
= 0;
1579 bufObj
->Pointer
= &dummy
;
1580 bufObj
->Length
= length
;
1581 bufObj
->Offset
= offset
;
1582 bufObj
->AccessFlags
= access
;
1583 return bufObj
->Pointer
;
1586 ASSERT(ctx
->Driver
.MapBufferRange
);
1587 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
);
1589 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1592 /* The driver callback should have set all these fields.
1593 * This is important because other modules (like VBO) might call
1594 * the driver function directly.
1596 ASSERT(bufObj
->Pointer
== map
);
1597 ASSERT(bufObj
->Length
== length
);
1598 ASSERT(bufObj
->Offset
== offset
);
1599 ASSERT(bufObj
->AccessFlags
== access
);
1607 * See GL_ARB_map_buffer_range spec
1610 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1612 GET_CURRENT_CONTEXT(ctx
);
1613 struct gl_buffer_object
*bufObj
;
1615 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1616 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1617 "glFlushMappedBufferRange(extension not supported)");
1622 _mesa_error(ctx
, GL_INVALID_VALUE
,
1623 "glFlushMappedBufferRange(offset = %ld)", (long)offset
);
1628 _mesa_error(ctx
, GL_INVALID_VALUE
,
1629 "glFlushMappedBufferRange(length = %ld)", (long)length
);
1633 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
);
1637 if (!_mesa_bufferobj_mapped(bufObj
)) {
1638 /* buffer is not mapped */
1639 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1640 "glFlushMappedBufferRange(buffer is not mapped)");
1644 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1645 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1646 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1650 if (offset
+ length
> bufObj
->Length
) {
1651 _mesa_error(ctx
, GL_INVALID_VALUE
,
1652 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1653 (long)offset
, (long)length
, (long)bufObj
->Length
);
1657 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1659 if (ctx
->Driver
.FlushMappedBufferRange
)
1660 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
);
1665 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1667 struct gl_buffer_object
*bufObj
;
1670 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1672 _mesa_error(ctx
, GL_INVALID_VALUE
,
1673 "glObjectPurgeable(name = 0x%x)", name
);
1676 if (!_mesa_is_bufferobj(bufObj
)) {
1677 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1681 if (bufObj
->Purgeable
) {
1682 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1683 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1684 return GL_VOLATILE_APPLE
;
1687 bufObj
->Purgeable
= GL_TRUE
;
1689 retval
= GL_VOLATILE_APPLE
;
1690 if (ctx
->Driver
.BufferObjectPurgeable
)
1691 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1698 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1700 struct gl_renderbuffer
*bufObj
;
1703 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1705 _mesa_error(ctx
, GL_INVALID_VALUE
,
1706 "glObjectUnpurgeable(name = 0x%x)", name
);
1710 if (bufObj
->Purgeable
) {
1711 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1712 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1713 return GL_VOLATILE_APPLE
;
1716 bufObj
->Purgeable
= GL_TRUE
;
1718 retval
= GL_VOLATILE_APPLE
;
1719 if (ctx
->Driver
.RenderObjectPurgeable
)
1720 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1727 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1729 struct gl_texture_object
*bufObj
;
1732 bufObj
= _mesa_lookup_texture(ctx
, name
);
1734 _mesa_error(ctx
, GL_INVALID_VALUE
,
1735 "glObjectPurgeable(name = 0x%x)", name
);
1739 if (bufObj
->Purgeable
) {
1740 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1741 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1742 return GL_VOLATILE_APPLE
;
1745 bufObj
->Purgeable
= GL_TRUE
;
1747 retval
= GL_VOLATILE_APPLE
;
1748 if (ctx
->Driver
.TextureObjectPurgeable
)
1749 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1756 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1760 GET_CURRENT_CONTEXT(ctx
);
1761 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1764 _mesa_error(ctx
, GL_INVALID_VALUE
,
1765 "glObjectPurgeable(name = 0x%x)", name
);
1770 case GL_VOLATILE_APPLE
:
1771 case GL_RELEASED_APPLE
:
1775 _mesa_error(ctx
, GL_INVALID_ENUM
,
1776 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1781 switch (objectType
) {
1783 retval
= texture_object_purgeable(ctx
, name
, option
);
1785 case GL_RENDERBUFFER_EXT
:
1786 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1788 case GL_BUFFER_OBJECT_APPLE
:
1789 retval
= buffer_object_purgeable(ctx
, name
, option
);
1792 _mesa_error(ctx
, GL_INVALID_ENUM
,
1793 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1798 /* In strict conformance to the spec, we must only return VOLATILE when
1799 * when passed the VOLATILE option. Madness.
1801 * XXX First fix the spec, then fix me.
1803 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1808 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1810 struct gl_buffer_object
*bufObj
;
1813 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1815 _mesa_error(ctx
, GL_INVALID_VALUE
,
1816 "glObjectUnpurgeable(name = 0x%x)", name
);
1820 if (! bufObj
->Purgeable
) {
1821 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1822 "glObjectUnpurgeable(name = 0x%x) object is "
1823 " already \"unpurged\"", name
);
1827 bufObj
->Purgeable
= GL_FALSE
;
1830 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1831 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1838 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1840 struct gl_renderbuffer
*bufObj
;
1843 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1845 _mesa_error(ctx
, GL_INVALID_VALUE
,
1846 "glObjectUnpurgeable(name = 0x%x)", name
);
1850 if (! bufObj
->Purgeable
) {
1851 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1852 "glObjectUnpurgeable(name = 0x%x) object is "
1853 " already \"unpurged\"", name
);
1857 bufObj
->Purgeable
= GL_FALSE
;
1860 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1861 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1868 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1870 struct gl_texture_object
*bufObj
;
1873 bufObj
= _mesa_lookup_texture(ctx
, name
);
1875 _mesa_error(ctx
, GL_INVALID_VALUE
,
1876 "glObjectUnpurgeable(name = 0x%x)", name
);
1880 if (! bufObj
->Purgeable
) {
1881 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1882 "glObjectUnpurgeable(name = 0x%x) object is"
1883 " already \"unpurged\"", name
);
1887 bufObj
->Purgeable
= GL_FALSE
;
1890 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1891 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1898 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1900 GET_CURRENT_CONTEXT(ctx
);
1901 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1904 _mesa_error(ctx
, GL_INVALID_VALUE
,
1905 "glObjectUnpurgeable(name = 0x%x)", name
);
1910 case GL_RETAINED_APPLE
:
1911 case GL_UNDEFINED_APPLE
:
1915 _mesa_error(ctx
, GL_INVALID_ENUM
,
1916 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1921 switch (objectType
) {
1922 case GL_BUFFER_OBJECT_APPLE
:
1923 return buffer_object_unpurgeable(ctx
, name
, option
);
1925 return texture_object_unpurgeable(ctx
, name
, option
);
1926 case GL_RENDERBUFFER_EXT
:
1927 return renderbuffer_unpurgeable(ctx
, name
, option
);
1929 _mesa_error(ctx
, GL_INVALID_ENUM
,
1930 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1938 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1939 GLenum pname
, GLint
*params
)
1941 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1943 _mesa_error(ctx
, GL_INVALID_VALUE
,
1944 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1949 case GL_PURGEABLE_APPLE
:
1950 *params
= bufObj
->Purgeable
;
1953 _mesa_error(ctx
, GL_INVALID_ENUM
,
1954 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1962 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1963 GLenum pname
, GLint
*params
)
1965 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1967 _mesa_error(ctx
, GL_INVALID_VALUE
,
1968 "glObjectUnpurgeable(name = 0x%x)", name
);
1973 case GL_PURGEABLE_APPLE
:
1974 *params
= rb
->Purgeable
;
1977 _mesa_error(ctx
, GL_INVALID_ENUM
,
1978 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1986 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1987 GLenum pname
, GLint
*params
)
1989 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1991 _mesa_error(ctx
, GL_INVALID_VALUE
,
1992 "glObjectUnpurgeable(name = 0x%x)", name
);
1997 case GL_PURGEABLE_APPLE
:
1998 *params
= texObj
->Purgeable
;
2001 _mesa_error(ctx
, GL_INVALID_ENUM
,
2002 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2010 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
2013 GET_CURRENT_CONTEXT(ctx
);
2016 _mesa_error(ctx
, GL_INVALID_VALUE
,
2017 "glGetObjectParameteriv(name = 0x%x)", name
);
2021 switch (objectType
) {
2023 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
2025 case GL_BUFFER_OBJECT_APPLE
:
2026 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
2028 case GL_RENDERBUFFER_EXT
:
2029 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
2032 _mesa_error(ctx
, GL_INVALID_ENUM
,
2033 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2039 set_ubo_binding(struct gl_context
*ctx
,
2041 struct gl_buffer_object
*bufObj
,
2046 struct gl_uniform_buffer_binding
*binding
;
2048 binding
= &ctx
->UniformBufferBindings
[index
];
2049 if (binding
->BufferObject
== bufObj
&&
2050 binding
->Offset
== offset
&&
2051 binding
->Size
== size
&&
2052 binding
->AutomaticSize
== autoSize
) {
2056 FLUSH_VERTICES(ctx
, 0);
2057 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
2059 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2060 binding
->Offset
= offset
;
2061 binding
->Size
= size
;
2062 binding
->AutomaticSize
= autoSize
;
2066 * Bind a region of a buffer object to a uniform block binding point.
2067 * \param index the uniform buffer binding point index
2068 * \param bufObj the buffer object
2069 * \param offset offset to the start of buffer object region
2070 * \param size size of the buffer object region
2073 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2075 struct gl_buffer_object
*bufObj
,
2079 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2080 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2084 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2085 _mesa_error(ctx
, GL_INVALID_VALUE
,
2086 "glBindBufferRange(offset misalgned %d/%d)", (int) offset
,
2087 ctx
->Const
.UniformBufferOffsetAlignment
);
2091 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2096 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2097 set_ubo_binding(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2102 * Bind a buffer object to a uniform block binding point.
2103 * As above, but offset = 0.
2106 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2108 struct gl_buffer_object
*bufObj
)
2110 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2111 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2115 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2116 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2117 set_ubo_binding(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2119 set_ubo_binding(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2123 _mesa_BindBufferRange(GLenum target
, GLuint index
,
2124 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
2126 GET_CURRENT_CONTEXT(ctx
);
2127 struct gl_buffer_object
*bufObj
;
2130 bufObj
= ctx
->Shared
->NullBufferObj
;
2132 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2134 if (!handle_bind_buffer_gen(ctx
, target
, buffer
, &bufObj
))
2138 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2139 "glBindBufferRange(invalid buffer=%u)", buffer
);
2145 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
2152 case GL_TRANSFORM_FEEDBACK_BUFFER
:
2153 _mesa_bind_buffer_range_transform_feedback(ctx
, index
, bufObj
,
2156 case GL_UNIFORM_BUFFER
:
2157 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
2160 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
2166 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
2168 GET_CURRENT_CONTEXT(ctx
);
2169 struct gl_buffer_object
*bufObj
;
2172 bufObj
= ctx
->Shared
->NullBufferObj
;
2174 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2176 if (!handle_bind_buffer_gen(ctx
, target
, buffer
, &bufObj
))
2180 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2181 "glBindBufferBase(invalid buffer=%u)", buffer
);
2185 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
2186 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
2188 * "BindBufferBase is equivalent to calling BindBufferRange with offset
2189 * zero and size equal to the size of buffer."
2191 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
2193 * "If the parameter (starting offset or size) was not specified when the
2194 * buffer object was bound, zero is returned."
2196 * What happens if the size of the buffer changes? Does the size of the
2197 * buffer at the moment glBindBufferBase was called still play a role, like
2198 * the first quote would imply, or is the size meaningless in the
2199 * glBindBufferBase case like the second quote would suggest? The GL 4.1
2200 * core spec page 45 says:
2202 * "It is equivalent to calling BindBufferRange with offset zero, while
2203 * size is determined by the size of the bound buffer at the time the
2206 * My interpretation is that the GL 4.1 spec was a clarification of the
2207 * behavior, not a change. In particular, this choice will only make
2208 * rendering work in cases where it would have had undefined results.
2212 case GL_TRANSFORM_FEEDBACK_BUFFER
:
2213 _mesa_bind_buffer_base_transform_feedback(ctx
, index
, bufObj
);
2215 case GL_UNIFORM_BUFFER
:
2216 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
2219 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
2225 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
2228 GET_CURRENT_CONTEXT(ctx
);
2229 struct gl_buffer_object
*bufObj
;
2230 const GLintptr end
= offset
+ length
;
2232 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2234 _mesa_error(ctx
, GL_INVALID_VALUE
,
2235 "glInvalidateBufferSubData(name = 0x%x) invalid object",
2240 /* The GL_ARB_invalidate_subdata spec says:
2242 * "An INVALID_VALUE error is generated if <offset> or <length> is
2243 * negative, or if <offset> + <length> is greater than the value of
2246 if (end
< 0 || end
> bufObj
->Size
) {
2247 _mesa_error(ctx
, GL_INVALID_VALUE
,
2248 "glInvalidateBufferSubData(invalid offset or length)");
2252 /* The GL_ARB_invalidate_subdata spec says:
2254 * "An INVALID_OPERATION error is generated if the buffer is currently
2255 * mapped by MapBuffer, or if the invalidate range intersects the range
2256 * currently mapped by MapBufferRange."
2258 if (_mesa_bufferobj_mapped(bufObj
)) {
2259 const GLintptr mapEnd
= bufObj
->Offset
+ bufObj
->Length
;
2261 /* The regions do not overlap if and only if the end of the discard
2262 * region is before the mapped region or the start of the discard region
2263 * is after the mapped region.
2265 * Note that 'end' and 'mapEnd' are the first byte *after* the discard
2266 * region and the mapped region, repsectively. It is okay for that byte
2267 * to be mapped (for 'end') or discarded (for 'mapEnd').
2269 if (!(end
<= bufObj
->Offset
|| offset
>= mapEnd
)) {
2270 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2271 "glInvalidateBufferSubData(intersection with mapped "
2277 /* We don't actually do anything for this yet. Just return after
2278 * validating the parameters and generating the required errors.
2284 _mesa_InvalidateBufferData(GLuint buffer
)
2286 GET_CURRENT_CONTEXT(ctx
);
2287 struct gl_buffer_object
*bufObj
;
2289 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2291 _mesa_error(ctx
, GL_INVALID_VALUE
,
2292 "glInvalidateBufferData(name = 0x%x) invalid object",
2297 /* The GL_ARB_invalidate_subdata spec says:
2299 * "An INVALID_OPERATION error is generated if the buffer is currently
2300 * mapped by MapBuffer, or if the invalidate range intersects the range
2301 * currently mapped by MapBufferRange."
2303 if (_mesa_bufferobj_mapped(bufObj
)) {
2304 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2305 "glInvalidateBufferData(intersection with mapped "
2310 /* We don't actually do anything for this yet. Just return after
2311 * validating the parameters and generating the required errors.