2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions.
30 * \author Brian Paul, Ian Romanick
40 #include "bufferobj.h"
42 #include "mfeatures.h"
45 #include "transformfeedback.h"
50 /*#define BOUNDS_CHECK*/
54 * Used as a placeholder for buffer objects between glGenBuffers() and
55 * glBindBuffer() so that glIsBuffer() can work correctly.
57 static struct gl_buffer_object DummyBufferObject
;
61 * Return pointer to address of a buffer object target.
62 * \param ctx the GL context
63 * \param target the buffer object target to be retrieved.
64 * \return pointer to pointer to the buffer object bound to \c target in the
65 * specified context or \c NULL if \c target is invalid.
67 static inline struct gl_buffer_object
**
68 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
71 case GL_ARRAY_BUFFER_ARB
:
72 return &ctx
->Array
.ArrayBufferObj
;
73 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
74 return &ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
75 case GL_PIXEL_PACK_BUFFER_EXT
:
76 return &ctx
->Pack
.BufferObj
;
77 case GL_PIXEL_UNPACK_BUFFER_EXT
:
78 return &ctx
->Unpack
.BufferObj
;
79 case GL_COPY_READ_BUFFER
:
80 return &ctx
->CopyReadBuffer
;
81 case GL_COPY_WRITE_BUFFER
:
82 return &ctx
->CopyWriteBuffer
;
83 #if FEATURE_EXT_transform_feedback
84 case GL_TRANSFORM_FEEDBACK_BUFFER
:
85 if (ctx
->Extensions
.EXT_transform_feedback
) {
86 return &ctx
->TransformFeedback
.CurrentBuffer
;
90 case GL_TEXTURE_BUFFER
:
91 if (ctx
->Extensions
.ARB_texture_buffer_object
) {
92 return &ctx
->Texture
.BufferObject
;
95 case GL_UNIFORM_BUFFER
:
96 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
97 return &ctx
->UniformBuffer
;
108 * Get the buffer object bound to the specified target in a GL context.
109 * \param ctx the GL context
110 * \param target the buffer object target to be retrieved.
111 * \return pointer to the buffer object bound to \c target in the
112 * specified context or \c NULL if \c target is invalid.
114 static inline struct gl_buffer_object
*
115 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
)
117 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
120 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
124 if (!_mesa_is_bufferobj(*bufObj
)) {
125 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(buffer 0)", func
);
134 default_access_mode(const struct gl_context
*ctx
)
136 /* Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
138 * Name Type Initial Value Legal Values
140 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
143 * However, table 6.8 in the GL_OES_mapbuffer extension says:
145 * Get Value Type Get Command Value Description
146 * --------- ---- ----------- ----- -----------
147 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
149 * The difference is because GL_OES_mapbuffer only supports mapping buffers
152 return (ctx
->API
== API_OPENGLES
)
153 ? GL_MAP_WRITE_BIT
: (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
);
158 * Convert a GLbitfield describing the mapped buffer access flags
159 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
162 simplified_access_mode(GLbitfield access
)
164 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
165 if ((access
& rwFlags
) == rwFlags
)
166 return GL_READ_WRITE
;
167 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
169 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
170 return GL_WRITE_ONLY
;
171 return GL_READ_WRITE
; /* this should never happen, but no big deal */
176 * Tests the subdata range parameters and sets the GL error code for
177 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
179 * \param ctx GL context.
180 * \param target Buffer object target on which to operate.
181 * \param offset Offset of the first byte of the subdata range.
182 * \param size Size, in bytes, of the subdata range.
183 * \param caller Name of calling function for recording errors.
184 * \return A pointer to the buffer object bound to \c target in the
185 * specified context or \c NULL if any of the parameter or state
186 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
189 * \sa glBufferSubDataARB, glGetBufferSubDataARB
191 static struct gl_buffer_object
*
192 buffer_object_subdata_range_good( struct gl_context
* ctx
, GLenum target
,
193 GLintptrARB offset
, GLsizeiptrARB size
,
196 struct gl_buffer_object
*bufObj
;
199 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
204 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
208 bufObj
= get_buffer(ctx
, caller
, target
);
212 if (offset
+ size
> bufObj
->Size
) {
213 _mesa_error(ctx
, GL_INVALID_VALUE
,
214 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
215 (unsigned long) offset
,
216 (unsigned long) size
,
217 (unsigned long) bufObj
->Size
);
220 if (_mesa_bufferobj_mapped(bufObj
)) {
221 /* Buffer is currently mapped */
222 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
231 * Allocate and initialize a new buffer object.
233 * Default callback for the \c dd_function_table::NewBufferObject() hook.
235 static struct gl_buffer_object
*
236 _mesa_new_buffer_object( struct gl_context
*ctx
, GLuint name
, GLenum target
)
238 struct gl_buffer_object
*obj
;
242 obj
= MALLOC_STRUCT(gl_buffer_object
);
243 _mesa_initialize_buffer_object(ctx
, obj
, name
, target
);
249 * Delete a buffer object.
251 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
254 _mesa_delete_buffer_object(struct gl_context
*ctx
,
255 struct gl_buffer_object
*bufObj
)
262 /* assign strange values here to help w/ debugging */
263 bufObj
->RefCount
= -1000;
266 _glthread_DESTROY_MUTEX(bufObj
->Mutex
);
273 * Set ptr to bufObj w/ reference counting.
274 * This is normally only called from the _mesa_reference_buffer_object() macro
275 * when there's a real pointer change.
278 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
279 struct gl_buffer_object
**ptr
,
280 struct gl_buffer_object
*bufObj
)
283 /* Unreference the old buffer */
284 GLboolean deleteFlag
= GL_FALSE
;
285 struct gl_buffer_object
*oldObj
= *ptr
;
287 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
288 ASSERT(oldObj
->RefCount
> 0);
291 printf("BufferObj %p %d DECR to %d\n",
292 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
294 deleteFlag
= (oldObj
->RefCount
== 0);
295 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
299 /* some sanity checking: don't delete a buffer still in use */
301 /* unfortunately, these tests are invalid during context tear-down */
302 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
303 ASSERT(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
!= bufObj
);
304 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
307 ASSERT(ctx
->Driver
.DeleteBuffer
);
308 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
316 /* reference new buffer */
317 _glthread_LOCK_MUTEX(bufObj
->Mutex
);
318 if (bufObj
->RefCount
== 0) {
319 /* this buffer's being deleted (look just above) */
320 /* Not sure this can every really happen. Warn if it does. */
321 _mesa_problem(NULL
, "referencing deleted buffer object");
327 printf("BufferObj %p %d INCR to %d\n",
328 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
332 _glthread_UNLOCK_MUTEX(bufObj
->Mutex
);
338 * Initialize a buffer object to default values.
341 _mesa_initialize_buffer_object( struct gl_context
*ctx
,
342 struct gl_buffer_object
*obj
,
343 GLuint name
, GLenum target
)
347 memset(obj
, 0, sizeof(struct gl_buffer_object
));
348 _glthread_INIT_MUTEX(obj
->Mutex
);
351 obj
->Usage
= GL_STATIC_DRAW_ARB
;
352 obj
->AccessFlags
= default_access_mode(ctx
);
358 * Callback called from _mesa_HashWalk()
361 count_buffer_size(GLuint key
, void *data
, void *userData
)
363 const struct gl_buffer_object
*bufObj
=
364 (const struct gl_buffer_object
*) data
;
365 GLuint
*total
= (GLuint
*) userData
;
367 *total
= *total
+ bufObj
->Size
;
372 * Compute total size (in bytes) of all buffer objects for the given context.
373 * For debugging purposes.
376 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
380 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
387 * Allocate space for and store data in a buffer object. Any data that was
388 * previously stored in the buffer object is lost. If \c data is \c NULL,
389 * memory will be allocated, but no copy will occur.
391 * This is the default callback for \c dd_function_table::BufferData()
392 * Note that all GL error checking will have been done already.
394 * \param ctx GL context.
395 * \param target Buffer object target on which to operate.
396 * \param size Size, in bytes, of the new data store.
397 * \param data Pointer to the data to store in the buffer object. This
398 * pointer may be \c NULL.
399 * \param usage Hints about how the data will be used.
400 * \param bufObj Object to be used.
402 * \return GL_TRUE for success, GL_FALSE for failure
403 * \sa glBufferDataARB, dd_function_table::BufferData.
406 _mesa_buffer_data( struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
407 const GLvoid
* data
, GLenum usage
,
408 struct gl_buffer_object
* bufObj
)
412 (void) ctx
; (void) target
;
414 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
416 bufObj
->Data
= (GLubyte
*) new_data
;
418 bufObj
->Usage
= usage
;
421 memcpy( bufObj
->Data
, data
, size
);
433 * Replace data in a subrange of buffer object. If the data range
434 * specified by \c size + \c offset extends beyond the end of the buffer or
435 * if \c data is \c NULL, no copy is performed.
437 * This is the default callback for \c dd_function_table::BufferSubData()
438 * Note that all GL error checking will have been done already.
440 * \param ctx GL context.
441 * \param target Buffer object target on which to operate.
442 * \param offset Offset of the first byte to be modified.
443 * \param size Size, in bytes, of the data range.
444 * \param data Pointer to the data to store in the buffer object.
445 * \param bufObj Object to be used.
447 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
450 _mesa_buffer_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
451 GLsizeiptrARB size
, const GLvoid
* data
,
452 struct gl_buffer_object
* bufObj
)
456 /* this should have been caught in _mesa_BufferSubData() */
457 ASSERT(size
+ offset
<= bufObj
->Size
);
460 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
466 * Retrieve data from a subrange of buffer object. If the data range
467 * specified by \c size + \c offset extends beyond the end of the buffer or
468 * if \c data is \c NULL, no copy is performed.
470 * This is the default callback for \c dd_function_table::GetBufferSubData()
471 * Note that all GL error checking will have been done already.
473 * \param ctx GL context.
474 * \param target Buffer object target on which to operate.
475 * \param offset Offset of the first byte to be fetched.
476 * \param size Size, in bytes, of the data range.
477 * \param data Destination for data
478 * \param bufObj Object to be used.
480 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
483 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
484 GLsizeiptrARB size
, GLvoid
* data
,
485 struct gl_buffer_object
* bufObj
)
489 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
490 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
496 * Default fallback for \c dd_function_table::MapBufferRange().
497 * Called via glMapBufferRange().
500 _mesa_buffer_map_range( struct gl_context
*ctx
, GLintptr offset
,
501 GLsizeiptr length
, GLbitfield access
,
502 struct gl_buffer_object
*bufObj
)
505 assert(!_mesa_bufferobj_mapped(bufObj
));
506 /* Just return a direct pointer to the data */
507 bufObj
->Pointer
= bufObj
->Data
+ offset
;
508 bufObj
->Length
= length
;
509 bufObj
->Offset
= offset
;
510 bufObj
->AccessFlags
= access
;
511 return bufObj
->Pointer
;
516 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
517 * Called via glFlushMappedBufferRange().
520 _mesa_buffer_flush_mapped_range( struct gl_context
*ctx
,
521 GLintptr offset
, GLsizeiptr length
,
522 struct gl_buffer_object
*obj
)
533 * Default callback for \c dd_function_table::MapBuffer().
535 * The input parameters will have been already tested for errors.
537 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
540 _mesa_buffer_unmap( struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
543 /* XXX we might assert here that bufObj->Pointer is non-null */
544 bufObj
->Pointer
= NULL
;
547 bufObj
->AccessFlags
= 0x0;
553 * Default fallback for \c dd_function_table::CopyBufferSubData().
554 * Called via glCopyBufferSubData().
557 _mesa_copy_buffer_subdata(struct gl_context
*ctx
,
558 struct gl_buffer_object
*src
,
559 struct gl_buffer_object
*dst
,
560 GLintptr readOffset
, GLintptr writeOffset
,
563 GLubyte
*srcPtr
, *dstPtr
;
565 /* the buffers should not be mapped */
566 assert(!_mesa_bufferobj_mapped(src
));
567 assert(!_mesa_bufferobj_mapped(dst
));
570 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
572 GL_MAP_WRITE_BIT
, src
);
577 srcPtr
+= readOffset
;
578 dstPtr
+= writeOffset
;
580 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
581 GL_MAP_READ_BIT
, src
);
582 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
584 GL_MAP_INVALIDATE_RANGE_BIT
), dst
);
587 /* Note: the src and dst regions will never overlap. Trying to do so
588 * would generate GL_INVALID_VALUE earlier.
590 if (srcPtr
&& dstPtr
)
591 memcpy(dstPtr
, srcPtr
, size
);
593 ctx
->Driver
.UnmapBuffer(ctx
, src
);
595 ctx
->Driver
.UnmapBuffer(ctx
, dst
);
601 * Initialize the state associated with buffer objects
604 _mesa_init_buffer_objects( struct gl_context
*ctx
)
608 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
609 _glthread_INIT_MUTEX(DummyBufferObject
.Mutex
);
610 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
612 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
613 ctx
->Shared
->NullBufferObj
);
615 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
616 ctx
->Shared
->NullBufferObj
);
617 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
618 ctx
->Shared
->NullBufferObj
);
620 ctx
->UniformBufferBindings
= calloc(ctx
->Const
.MaxUniformBufferBindings
,
621 sizeof(*ctx
->UniformBufferBindings
));
623 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
624 ctx
->Shared
->NullBufferObj
);
626 for (i
= 0; i
< ctx
->Const
.MaxUniformBufferBindings
; i
++) {
627 _mesa_reference_buffer_object(ctx
,
628 &ctx
->UniformBufferBindings
[i
].BufferObject
,
629 ctx
->Shared
->NullBufferObj
);
630 ctx
->UniformBufferBindings
[i
].Offset
= -1;
631 ctx
->UniformBufferBindings
[i
].Size
= -1;
637 _mesa_free_buffer_objects( struct gl_context
*ctx
)
641 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
643 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
644 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
646 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
648 for (i
= 0; i
< ctx
->Const
.MaxUniformBufferBindings
; i
++) {
649 _mesa_reference_buffer_object(ctx
,
650 &ctx
->UniformBufferBindings
[i
].BufferObject
,
654 free(ctx
->UniformBufferBindings
);
655 ctx
->UniformBufferBindings
= NULL
;
660 * Bind the specified target to buffer for the specified context.
661 * Called by glBindBuffer() and other functions.
664 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
666 struct gl_buffer_object
*oldBufObj
;
667 struct gl_buffer_object
*newBufObj
= NULL
;
668 struct gl_buffer_object
**bindTarget
= NULL
;
670 bindTarget
= get_buffer_target(ctx
, target
);
672 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
676 /* Get pointer to old buffer object (to be unbound) */
677 oldBufObj
= *bindTarget
;
678 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
679 return; /* rebinding the same buffer object- no change */
682 * Get pointer to new buffer object (newBufObj)
685 /* The spec says there's not a buffer object named 0, but we use
686 * one internally because it simplifies things.
688 newBufObj
= ctx
->Shared
->NullBufferObj
;
691 /* non-default buffer object */
692 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
693 if (!newBufObj
|| newBufObj
== &DummyBufferObject
) {
694 /* If this is a new buffer object id, or one which was generated but
695 * never used before, allocate a buffer object now.
697 ASSERT(ctx
->Driver
.NewBufferObject
);
698 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
700 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
703 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
707 /* bind new buffer */
708 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
710 /* Pass BindBuffer call to device driver */
711 if (ctx
->Driver
.BindBuffer
)
712 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
717 * Update the default buffer objects in the given context to reference those
718 * specified in the shared state and release those referencing the old
722 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
724 /* Bind the NullBufferObj to remove references to those
725 * in the shared context hash table.
727 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
728 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
729 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
730 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
736 * Return the gl_buffer_object for the given ID.
737 * Always return NULL for ID 0.
739 struct gl_buffer_object
*
740 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
745 return (struct gl_buffer_object
*)
746 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
751 * If *ptr points to obj, set ptr = the Null/default buffer object.
752 * This is a helper for buffer object deletion.
753 * The GL spec says that deleting a buffer object causes it to get
754 * unbound from all arrays in the current context.
757 unbind(struct gl_context
*ctx
,
758 struct gl_buffer_object
**ptr
,
759 struct gl_buffer_object
*obj
)
762 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
768 * Plug default/fallback buffer object functions into the device
772 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
774 /* GL_ARB_vertex/pixel_buffer_object */
775 driver
->NewBufferObject
= _mesa_new_buffer_object
;
776 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
777 driver
->BindBuffer
= NULL
;
778 driver
->BufferData
= _mesa_buffer_data
;
779 driver
->BufferSubData
= _mesa_buffer_subdata
;
780 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
781 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
783 /* GL_ARB_map_buffer_range */
784 driver
->MapBufferRange
= _mesa_buffer_map_range
;
785 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
787 /* GL_ARB_copy_buffer */
788 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
793 /**********************************************************************/
795 /**********************************************************************/
798 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
800 GET_CURRENT_CONTEXT(ctx
);
801 ASSERT_OUTSIDE_BEGIN_END(ctx
);
803 if (MESA_VERBOSE
& VERBOSE_API
)
804 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
805 _mesa_lookup_enum_by_nr(target
), buffer
);
807 bind_buffer_object(ctx
, target
, buffer
);
812 * Delete a set of buffer objects.
814 * \param n Number of buffer objects to delete.
815 * \param ids Array of \c n buffer object IDs.
818 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
820 GET_CURRENT_CONTEXT(ctx
);
822 ASSERT_OUTSIDE_BEGIN_END(ctx
);
823 FLUSH_VERTICES(ctx
, 0);
826 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
830 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
832 for (i
= 0; i
< n
; i
++) {
833 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
835 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
838 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
840 if (_mesa_bufferobj_mapped(bufObj
)) {
841 /* if mapped, unmap it now */
842 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
843 bufObj
->AccessFlags
= default_access_mode(ctx
);
844 bufObj
->Pointer
= NULL
;
847 /* unbind any vertex pointers bound to this buffer */
848 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
849 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
852 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
853 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
855 if (arrayObj
->ElementArrayBufferObj
== bufObj
) {
856 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
859 /* unbind ARB_copy_buffer binding points */
860 if (ctx
->CopyReadBuffer
== bufObj
) {
861 _mesa_BindBufferARB( GL_COPY_READ_BUFFER
, 0 );
863 if (ctx
->CopyWriteBuffer
== bufObj
) {
864 _mesa_BindBufferARB( GL_COPY_WRITE_BUFFER
, 0 );
867 /* unbind transform feedback binding points */
868 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
869 _mesa_BindBufferARB( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
871 for (j
= 0; j
< MAX_FEEDBACK_ATTRIBS
; j
++) {
872 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
873 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
877 if (ctx
->UniformBuffer
== bufObj
) {
878 _mesa_BindBufferARB( GL_UNIFORM_BUFFER
, 0 );
881 /* unbind any pixel pack/unpack pointers bound to this buffer */
882 if (ctx
->Pack
.BufferObj
== bufObj
) {
883 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
885 if (ctx
->Unpack
.BufferObj
== bufObj
) {
886 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
889 if (ctx
->Texture
.BufferObject
== bufObj
) {
890 _mesa_BindBufferARB( GL_TEXTURE_BUFFER
, 0 );
893 /* The ID is immediately freed for re-use */
894 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
895 /* Make sure we do not run into the classic ABA problem on bind.
896 * We don't want to allow re-binding a buffer object that's been
897 * "deleted" by glDeleteBuffers().
899 * The explicit rebinding to the default object in the current context
900 * prevents the above in the current context, but another context
901 * sharing the same objects might suffer from this problem.
902 * The alternative would be to do the hash lookup in any case on bind
903 * which would introduce more runtime overhead than this.
905 bufObj
->DeletePending
= GL_TRUE
;
906 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
910 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
915 * Generate a set of unique buffer object IDs and store them in \c buffer.
917 * \param n Number of IDs to generate.
918 * \param buffer Array of \c n locations to store the IDs.
921 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
923 GET_CURRENT_CONTEXT(ctx
);
926 ASSERT_OUTSIDE_BEGIN_END(ctx
);
928 if (MESA_VERBOSE
& VERBOSE_API
)
929 _mesa_debug(ctx
, "glGenBuffers(%d)\n", n
);
932 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
941 * This must be atomic (generation and allocation of buffer object IDs)
943 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
945 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
947 /* Insert the ID and pointer to dummy buffer object into hash table */
948 for (i
= 0; i
< n
; i
++) {
949 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
951 buffer
[i
] = first
+ i
;
954 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
959 * Determine if ID is the name of a buffer object.
961 * \param id ID of the potential buffer object.
962 * \return \c GL_TRUE if \c id is the name of a buffer object,
963 * \c GL_FALSE otherwise.
966 _mesa_IsBufferARB(GLuint id
)
968 struct gl_buffer_object
*bufObj
;
969 GET_CURRENT_CONTEXT(ctx
);
970 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
972 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
973 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
974 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
976 return bufObj
&& bufObj
!= &DummyBufferObject
;
981 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
982 const GLvoid
* data
, GLenum usage
)
984 GET_CURRENT_CONTEXT(ctx
);
985 struct gl_buffer_object
*bufObj
;
986 ASSERT_OUTSIDE_BEGIN_END(ctx
);
988 if (MESA_VERBOSE
& VERBOSE_API
)
989 _mesa_debug(ctx
, "glBufferData(%s, %ld, %p, %s)\n",
990 _mesa_lookup_enum_by_nr(target
),
991 (long int) size
, data
,
992 _mesa_lookup_enum_by_nr(usage
));
995 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1000 case GL_STREAM_DRAW_ARB
:
1001 case GL_STREAM_READ_ARB
:
1002 case GL_STREAM_COPY_ARB
:
1003 case GL_STATIC_DRAW_ARB
:
1004 case GL_STATIC_READ_ARB
:
1005 case GL_STATIC_COPY_ARB
:
1006 case GL_DYNAMIC_DRAW_ARB
:
1007 case GL_DYNAMIC_READ_ARB
:
1008 case GL_DYNAMIC_COPY_ARB
:
1012 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
1016 bufObj
= get_buffer(ctx
, "glBufferDataARB", target
);
1020 if (_mesa_bufferobj_mapped(bufObj
)) {
1021 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1022 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
1023 bufObj
->AccessFlags
= default_access_mode(ctx
);
1024 ASSERT(bufObj
->Pointer
== NULL
);
1027 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1029 bufObj
->Written
= GL_TRUE
;
1032 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1033 bufObj
->Name
, size
, data
, usage
);
1040 ASSERT(ctx
->Driver
.BufferData
);
1041 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1042 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1048 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1049 GLsizeiptrARB size
, const GLvoid
* data
)
1051 GET_CURRENT_CONTEXT(ctx
);
1052 struct gl_buffer_object
*bufObj
;
1053 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1055 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1056 "glBufferSubDataARB" );
1058 /* error already recorded */
1065 bufObj
->Written
= GL_TRUE
;
1067 ASSERT(ctx
->Driver
.BufferSubData
);
1068 ctx
->Driver
.BufferSubData( ctx
, offset
, size
, data
, bufObj
);
1073 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1074 GLsizeiptrARB size
, void * data
)
1076 GET_CURRENT_CONTEXT(ctx
);
1077 struct gl_buffer_object
*bufObj
;
1078 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1080 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1081 "glGetBufferSubDataARB" );
1083 /* error already recorded */
1087 ASSERT(ctx
->Driver
.GetBufferSubData
);
1088 ctx
->Driver
.GetBufferSubData( ctx
, offset
, size
, data
, bufObj
);
1093 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1095 GET_CURRENT_CONTEXT(ctx
);
1096 struct gl_buffer_object
* bufObj
;
1097 GLbitfield accessFlags
;
1100 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1103 case GL_READ_ONLY_ARB
:
1104 accessFlags
= GL_MAP_READ_BIT
;
1106 case GL_WRITE_ONLY_ARB
:
1107 accessFlags
= GL_MAP_WRITE_BIT
;
1109 case GL_READ_WRITE_ARB
:
1110 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1113 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1117 bufObj
= get_buffer(ctx
, "glMapBufferARB", target
);
1121 if (_mesa_bufferobj_mapped(bufObj
)) {
1122 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1126 if (!bufObj
->Size
) {
1127 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1128 "glMapBuffer(buffer size = 0)");
1132 ASSERT(ctx
->Driver
.MapBufferRange
);
1133 map
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
, accessFlags
, bufObj
);
1135 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1139 /* The driver callback should have set these fields.
1140 * This is important because other modules (like VBO) might call
1141 * the driver function directly.
1143 ASSERT(bufObj
->Pointer
== map
);
1144 ASSERT(bufObj
->Length
== bufObj
->Size
);
1145 ASSERT(bufObj
->Offset
== 0);
1146 bufObj
->AccessFlags
= accessFlags
;
1149 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1150 bufObj
->Written
= GL_TRUE
;
1153 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1154 bufObj
->Name
, bufObj
->Size
, access
);
1155 if (access
== GL_WRITE_ONLY_ARB
) {
1157 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1158 for (i
= 0; i
< bufObj
->Size
; i
++)
1165 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1167 /* buffer is 100 bytes larger than requested, fill with magic value */
1168 for (i
= 0; i
< 100; i
++) {
1169 buf
[bufObj
->Size
- i
- 1] = 123;
1174 return bufObj
->Pointer
;
1178 GLboolean GLAPIENTRY
1179 _mesa_UnmapBufferARB(GLenum target
)
1181 GET_CURRENT_CONTEXT(ctx
);
1182 struct gl_buffer_object
*bufObj
;
1183 GLboolean status
= GL_TRUE
;
1184 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1186 bufObj
= get_buffer(ctx
, "glUnmapBufferARB", target
);
1190 if (!_mesa_bufferobj_mapped(bufObj
)) {
1191 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1196 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1197 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1199 /* check that last 100 bytes are still = magic value */
1200 for (i
= 0; i
< 100; i
++) {
1201 GLuint pos
= bufObj
->Size
- i
- 1;
1202 if (buf
[pos
] != 123) {
1203 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1204 " at position %d (value = %u)\n",
1212 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1213 GLuint i
, unchanged
= 0;
1214 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1216 /* check which bytes changed */
1217 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1218 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1225 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1226 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1231 status
= ctx
->Driver
.UnmapBuffer( ctx
, bufObj
);
1232 bufObj
->AccessFlags
= default_access_mode(ctx
);
1233 ASSERT(bufObj
->Pointer
== NULL
);
1234 ASSERT(bufObj
->Offset
== 0);
1235 ASSERT(bufObj
->Length
== 0);
1242 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1244 GET_CURRENT_CONTEXT(ctx
);
1245 struct gl_buffer_object
*bufObj
;
1246 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1248 bufObj
= get_buffer(ctx
, "glGetBufferParameterivARB", target
);
1253 case GL_BUFFER_SIZE_ARB
:
1254 *params
= (GLint
) bufObj
->Size
;
1256 case GL_BUFFER_USAGE_ARB
:
1257 *params
= bufObj
->Usage
;
1259 case GL_BUFFER_ACCESS_ARB
:
1260 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1262 case GL_BUFFER_MAPPED_ARB
:
1263 *params
= _mesa_bufferobj_mapped(bufObj
);
1265 case GL_BUFFER_ACCESS_FLAGS
:
1266 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1268 *params
= bufObj
->AccessFlags
;
1270 case GL_BUFFER_MAP_OFFSET
:
1271 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1273 *params
= (GLint
) bufObj
->Offset
;
1275 case GL_BUFFER_MAP_LENGTH
:
1276 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1278 *params
= (GLint
) bufObj
->Length
;
1281 ; /* fall-through */
1285 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1286 _mesa_lookup_enum_by_nr(pname
));
1292 * This is pretty much a duplicate of GetBufferParameteriv() but the
1293 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1296 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1298 GET_CURRENT_CONTEXT(ctx
);
1299 struct gl_buffer_object
*bufObj
;
1300 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1302 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
);
1307 case GL_BUFFER_SIZE_ARB
:
1308 *params
= bufObj
->Size
;
1310 case GL_BUFFER_USAGE_ARB
:
1311 *params
= bufObj
->Usage
;
1313 case GL_BUFFER_ACCESS_ARB
:
1314 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1316 case GL_BUFFER_ACCESS_FLAGS
:
1317 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1319 *params
= bufObj
->AccessFlags
;
1321 case GL_BUFFER_MAPPED_ARB
:
1322 *params
= _mesa_bufferobj_mapped(bufObj
);
1324 case GL_BUFFER_MAP_OFFSET
:
1325 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1327 *params
= bufObj
->Offset
;
1329 case GL_BUFFER_MAP_LENGTH
:
1330 if (!ctx
->Extensions
.ARB_map_buffer_range
)
1332 *params
= bufObj
->Length
;
1335 ; /* fall-through */
1339 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1340 _mesa_lookup_enum_by_nr(pname
));
1345 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1347 GET_CURRENT_CONTEXT(ctx
);
1348 struct gl_buffer_object
* bufObj
;
1349 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1351 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1352 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1356 bufObj
= get_buffer(ctx
, "glGetBufferPointervARB", target
);
1360 *params
= bufObj
->Pointer
;
1365 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1366 GLintptr readOffset
, GLintptr writeOffset
,
1369 GET_CURRENT_CONTEXT(ctx
);
1370 struct gl_buffer_object
*src
, *dst
;
1371 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1373 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
);
1377 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
);
1381 if (_mesa_bufferobj_mapped(src
)) {
1382 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1383 "glCopyBufferSubData(readBuffer is mapped)");
1387 if (_mesa_bufferobj_mapped(dst
)) {
1388 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1389 "glCopyBufferSubData(writeBuffer is mapped)");
1393 if (readOffset
< 0) {
1394 _mesa_error(ctx
, GL_INVALID_VALUE
,
1395 "glCopyBufferSubData(readOffset = %d)", (int) readOffset
);
1399 if (writeOffset
< 0) {
1400 _mesa_error(ctx
, GL_INVALID_VALUE
,
1401 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset
);
1406 _mesa_error(ctx
, GL_INVALID_VALUE
,
1407 "glCopyBufferSubData(writeOffset = %d)", (int) size
);
1411 if (readOffset
+ size
> src
->Size
) {
1412 _mesa_error(ctx
, GL_INVALID_VALUE
,
1413 "glCopyBufferSubData(readOffset + size = %d)",
1414 (int) (readOffset
+ size
));
1418 if (writeOffset
+ size
> dst
->Size
) {
1419 _mesa_error(ctx
, GL_INVALID_VALUE
,
1420 "glCopyBufferSubData(writeOffset + size = %d)",
1421 (int) (writeOffset
+ size
));
1426 if (readOffset
+ size
<= writeOffset
) {
1429 else if (writeOffset
+ size
<= readOffset
) {
1433 /* overlapping src/dst is illegal */
1434 _mesa_error(ctx
, GL_INVALID_VALUE
,
1435 "glCopyBufferSubData(overlapping src/dst)");
1440 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1445 * See GL_ARB_map_buffer_range spec
1448 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1451 GET_CURRENT_CONTEXT(ctx
);
1452 struct gl_buffer_object
*bufObj
;
1455 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1457 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1458 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1459 "glMapBufferRange(extension not supported)");
1464 _mesa_error(ctx
, GL_INVALID_VALUE
,
1465 "glMapBufferRange(offset = %ld)", (long)offset
);
1470 _mesa_error(ctx
, GL_INVALID_VALUE
,
1471 "glMapBufferRange(length = %ld)", (long)length
);
1475 if (access
& ~(GL_MAP_READ_BIT
|
1477 GL_MAP_INVALIDATE_RANGE_BIT
|
1478 GL_MAP_INVALIDATE_BUFFER_BIT
|
1479 GL_MAP_FLUSH_EXPLICIT_BIT
|
1480 GL_MAP_UNSYNCHRONIZED_BIT
)) {
1481 /* generate an error if any undefind bit is set */
1482 _mesa_error(ctx
, GL_INVALID_VALUE
, "glMapBufferRange(access)");
1486 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1487 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1488 "glMapBufferRange(access indicates neither read or write)");
1492 if ((access
& GL_MAP_READ_BIT
) &&
1493 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
1494 GL_MAP_INVALIDATE_BUFFER_BIT
|
1495 GL_MAP_UNSYNCHRONIZED_BIT
))) {
1496 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1497 "glMapBufferRange(invalid access flags)");
1501 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1502 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1503 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1504 "glMapBufferRange(invalid access flags)");
1508 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
);
1512 if (offset
+ length
> bufObj
->Size
) {
1513 _mesa_error(ctx
, GL_INVALID_VALUE
,
1514 "glMapBufferRange(offset + length > size)");
1518 if (_mesa_bufferobj_mapped(bufObj
)) {
1519 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1520 "glMapBufferRange(buffer already mapped)");
1524 if (!bufObj
->Size
) {
1525 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1526 "glMapBufferRange(buffer size = 0)");
1530 /* Mapping zero bytes should return a non-null pointer. */
1532 static long dummy
= 0;
1533 bufObj
->Pointer
= &dummy
;
1534 bufObj
->Length
= length
;
1535 bufObj
->Offset
= offset
;
1536 bufObj
->AccessFlags
= access
;
1537 return bufObj
->Pointer
;
1540 ASSERT(ctx
->Driver
.MapBufferRange
);
1541 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
);
1543 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1546 /* The driver callback should have set all these fields.
1547 * This is important because other modules (like VBO) might call
1548 * the driver function directly.
1550 ASSERT(bufObj
->Pointer
== map
);
1551 ASSERT(bufObj
->Length
== length
);
1552 ASSERT(bufObj
->Offset
== offset
);
1553 ASSERT(bufObj
->AccessFlags
== access
);
1561 * See GL_ARB_map_buffer_range spec
1564 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1566 GET_CURRENT_CONTEXT(ctx
);
1567 struct gl_buffer_object
*bufObj
;
1568 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1570 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1571 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1572 "glFlushMappedBufferRange(extension not supported)");
1577 _mesa_error(ctx
, GL_INVALID_VALUE
,
1578 "glFlushMappedBufferRange(offset = %ld)", (long)offset
);
1583 _mesa_error(ctx
, GL_INVALID_VALUE
,
1584 "glFlushMappedBufferRange(length = %ld)", (long)length
);
1588 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
);
1592 if (!_mesa_bufferobj_mapped(bufObj
)) {
1593 /* buffer is not mapped */
1594 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1595 "glFlushMappedBufferRange(buffer is not mapped)");
1599 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1600 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1601 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1605 if (offset
+ length
> bufObj
->Length
) {
1606 _mesa_error(ctx
, GL_INVALID_VALUE
,
1607 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1608 (long)offset
, (long)length
, (long)bufObj
->Length
);
1612 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1614 if (ctx
->Driver
.FlushMappedBufferRange
)
1615 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
);
1619 #if FEATURE_APPLE_object_purgeable
1621 buffer_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1623 struct gl_buffer_object
*bufObj
;
1626 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1628 _mesa_error(ctx
, GL_INVALID_VALUE
,
1629 "glObjectPurgeable(name = 0x%x)", name
);
1632 if (!_mesa_is_bufferobj(bufObj
)) {
1633 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1637 if (bufObj
->Purgeable
) {
1638 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1639 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1640 return GL_VOLATILE_APPLE
;
1643 bufObj
->Purgeable
= GL_TRUE
;
1645 retval
= GL_VOLATILE_APPLE
;
1646 if (ctx
->Driver
.BufferObjectPurgeable
)
1647 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1654 renderbuffer_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1656 struct gl_renderbuffer
*bufObj
;
1659 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1661 _mesa_error(ctx
, GL_INVALID_VALUE
,
1662 "glObjectUnpurgeable(name = 0x%x)", name
);
1666 if (bufObj
->Purgeable
) {
1667 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1668 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1669 return GL_VOLATILE_APPLE
;
1672 bufObj
->Purgeable
= GL_TRUE
;
1674 retval
= GL_VOLATILE_APPLE
;
1675 if (ctx
->Driver
.RenderObjectPurgeable
)
1676 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1683 texture_object_purgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1685 struct gl_texture_object
*bufObj
;
1688 bufObj
= _mesa_lookup_texture(ctx
, name
);
1690 _mesa_error(ctx
, GL_INVALID_VALUE
,
1691 "glObjectPurgeable(name = 0x%x)", name
);
1695 if (bufObj
->Purgeable
) {
1696 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1697 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1698 return GL_VOLATILE_APPLE
;
1701 bufObj
->Purgeable
= GL_TRUE
;
1703 retval
= GL_VOLATILE_APPLE
;
1704 if (ctx
->Driver
.TextureObjectPurgeable
)
1705 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1712 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1716 GET_CURRENT_CONTEXT(ctx
);
1717 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1720 _mesa_error(ctx
, GL_INVALID_VALUE
,
1721 "glObjectPurgeable(name = 0x%x)", name
);
1726 case GL_VOLATILE_APPLE
:
1727 case GL_RELEASED_APPLE
:
1731 _mesa_error(ctx
, GL_INVALID_ENUM
,
1732 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1737 switch (objectType
) {
1739 retval
= texture_object_purgeable(ctx
, name
, option
);
1741 case GL_RENDERBUFFER_EXT
:
1742 retval
= renderbuffer_purgeable(ctx
, name
, option
);
1744 case GL_BUFFER_OBJECT_APPLE
:
1745 retval
= buffer_object_purgeable(ctx
, name
, option
);
1748 _mesa_error(ctx
, GL_INVALID_ENUM
,
1749 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1754 /* In strict conformance to the spec, we must only return VOLATILE when
1755 * when passed the VOLATILE option. Madness.
1757 * XXX First fix the spec, then fix me.
1759 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1764 buffer_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1766 struct gl_buffer_object
*bufObj
;
1769 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1771 _mesa_error(ctx
, GL_INVALID_VALUE
,
1772 "glObjectUnpurgeable(name = 0x%x)", name
);
1776 if (! bufObj
->Purgeable
) {
1777 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1778 "glObjectUnpurgeable(name = 0x%x) object is "
1779 " already \"unpurged\"", name
);
1783 bufObj
->Purgeable
= GL_FALSE
;
1786 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1787 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1794 renderbuffer_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1796 struct gl_renderbuffer
*bufObj
;
1799 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1801 _mesa_error(ctx
, GL_INVALID_VALUE
,
1802 "glObjectUnpurgeable(name = 0x%x)", name
);
1806 if (! bufObj
->Purgeable
) {
1807 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1808 "glObjectUnpurgeable(name = 0x%x) object is "
1809 " already \"unpurged\"", name
);
1813 bufObj
->Purgeable
= GL_FALSE
;
1816 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1817 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1824 texture_object_unpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1826 struct gl_texture_object
*bufObj
;
1829 bufObj
= _mesa_lookup_texture(ctx
, name
);
1831 _mesa_error(ctx
, GL_INVALID_VALUE
,
1832 "glObjectUnpurgeable(name = 0x%x)", name
);
1836 if (! bufObj
->Purgeable
) {
1837 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1838 "glObjectUnpurgeable(name = 0x%x) object is"
1839 " already \"unpurged\"", name
);
1843 bufObj
->Purgeable
= GL_FALSE
;
1846 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1847 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1854 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1856 GET_CURRENT_CONTEXT(ctx
);
1857 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1860 _mesa_error(ctx
, GL_INVALID_VALUE
,
1861 "glObjectUnpurgeable(name = 0x%x)", name
);
1866 case GL_RETAINED_APPLE
:
1867 case GL_UNDEFINED_APPLE
:
1871 _mesa_error(ctx
, GL_INVALID_ENUM
,
1872 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1877 switch (objectType
) {
1878 case GL_BUFFER_OBJECT_APPLE
:
1879 return buffer_object_unpurgeable(ctx
, name
, option
);
1881 return texture_object_unpurgeable(ctx
, name
, option
);
1882 case GL_RENDERBUFFER_EXT
:
1883 return renderbuffer_unpurgeable(ctx
, name
, option
);
1885 _mesa_error(ctx
, GL_INVALID_ENUM
,
1886 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1894 get_buffer_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1895 GLenum pname
, GLint
*params
)
1897 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1899 _mesa_error(ctx
, GL_INVALID_VALUE
,
1900 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
1905 case GL_PURGEABLE_APPLE
:
1906 *params
= bufObj
->Purgeable
;
1909 _mesa_error(ctx
, GL_INVALID_ENUM
,
1910 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1918 get_renderbuffer_parameteriv(struct gl_context
*ctx
, GLuint name
,
1919 GLenum pname
, GLint
*params
)
1921 struct gl_renderbuffer
*rb
= _mesa_lookup_renderbuffer(ctx
, name
);
1923 _mesa_error(ctx
, GL_INVALID_VALUE
,
1924 "glObjectUnpurgeable(name = 0x%x)", name
);
1929 case GL_PURGEABLE_APPLE
:
1930 *params
= rb
->Purgeable
;
1933 _mesa_error(ctx
, GL_INVALID_ENUM
,
1934 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1942 get_texture_object_parameteriv(struct gl_context
*ctx
, GLuint name
,
1943 GLenum pname
, GLint
*params
)
1945 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, name
);
1947 _mesa_error(ctx
, GL_INVALID_VALUE
,
1948 "glObjectUnpurgeable(name = 0x%x)", name
);
1953 case GL_PURGEABLE_APPLE
:
1954 *params
= texObj
->Purgeable
;
1957 _mesa_error(ctx
, GL_INVALID_ENUM
,
1958 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1966 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
1969 GET_CURRENT_CONTEXT(ctx
);
1972 _mesa_error(ctx
, GL_INVALID_VALUE
,
1973 "glGetObjectParameteriv(name = 0x%x)", name
);
1977 switch (objectType
) {
1979 get_texture_object_parameteriv(ctx
, name
, pname
, params
);
1981 case GL_BUFFER_OBJECT_APPLE
:
1982 get_buffer_object_parameteriv(ctx
, name
, pname
, params
);
1984 case GL_RENDERBUFFER_EXT
:
1985 get_renderbuffer_parameteriv(ctx
, name
, pname
, params
);
1988 _mesa_error(ctx
, GL_INVALID_ENUM
,
1989 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1994 #endif /* FEATURE_APPLE_object_purgeable */