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"
47 /*#define BOUNDS_CHECK*/
50 #if FEATURE_OES_mapbuffer
51 #define DEFAULT_ACCESS GL_MAP_WRITE_BIT
53 #define DEFAULT_ACCESS (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)
58 * Used as a placeholder for buffer objects between glGenBuffers() and
59 * glBindBuffer() so that glIsBuffer() can work correctly.
61 static struct gl_buffer_object DummyBufferObject
;
65 * Return pointer to address of a buffer object target.
66 * \param ctx the GL context
67 * \param target the buffer object target to be retrieved.
68 * \return pointer to pointer to the buffer object bound to \c target in the
69 * specified context or \c NULL if \c target is invalid.
71 static INLINE
struct gl_buffer_object
**
72 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
75 case GL_ARRAY_BUFFER_ARB
:
76 return &ctx
->Array
.ArrayBufferObj
;
77 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
78 return &ctx
->Array
.ElementArrayBufferObj
;
79 case GL_PIXEL_PACK_BUFFER_EXT
:
80 return &ctx
->Pack
.BufferObj
;
81 case GL_PIXEL_UNPACK_BUFFER_EXT
:
82 return &ctx
->Unpack
.BufferObj
;
83 case GL_COPY_READ_BUFFER
:
84 return &ctx
->CopyReadBuffer
;
85 case GL_COPY_WRITE_BUFFER
:
86 return &ctx
->CopyWriteBuffer
;
87 #if FEATURE_EXT_transform_feedback
88 case GL_TRANSFORM_FEEDBACK_BUFFER
:
89 if (ctx
->Extensions
.EXT_transform_feedback
) {
90 return &ctx
->TransformFeedback
.CurrentBuffer
;
102 * Get the buffer object bound to the specified target in a GL context.
103 * \param ctx the GL context
104 * \param target the buffer object target to be retrieved.
105 * \return pointer to the buffer object bound to \c target in the
106 * specified context or \c NULL if \c target is invalid.
108 static INLINE
struct gl_buffer_object
*
109 get_buffer(struct gl_context
*ctx
, GLenum target
)
111 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
119 * Convert a GLbitfield describing the mapped buffer access flags
120 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
123 simplified_access_mode(GLbitfield access
)
125 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
126 if ((access
& rwFlags
) == rwFlags
)
127 return GL_READ_WRITE
;
128 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
130 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
131 return GL_WRITE_ONLY
;
132 return GL_READ_WRITE
; /* this should never happen, but no big deal */
137 * Tests the subdata range parameters and sets the GL error code for
138 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
140 * \param ctx GL context.
141 * \param target Buffer object target on which to operate.
142 * \param offset Offset of the first byte of the subdata range.
143 * \param size Size, in bytes, of the subdata range.
144 * \param caller Name of calling function for recording errors.
145 * \return A pointer to the buffer object bound to \c target in the
146 * specified context or \c NULL if any of the parameter or state
147 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
150 * \sa glBufferSubDataARB, glGetBufferSubDataARB
152 static struct gl_buffer_object
*
153 buffer_object_subdata_range_good( struct gl_context
* ctx
, GLenum target
,
154 GLintptrARB offset
, GLsizeiptrARB size
,
157 struct gl_buffer_object
*bufObj
;
160 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
165 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
169 bufObj
= get_buffer(ctx
, target
);
171 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", caller
);
174 if (!_mesa_is_bufferobj(bufObj
)) {
175 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
178 if (offset
+ size
> bufObj
->Size
) {
179 _mesa_error(ctx
, GL_INVALID_VALUE
,
180 "%s(size + offset > buffer size)", caller
);
183 if (_mesa_bufferobj_mapped(bufObj
)) {
184 /* Buffer is currently mapped */
185 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
194 * Allocate and initialize a new buffer object.
196 * Default callback for the \c dd_function_table::NewBufferObject() hook.
198 static struct gl_buffer_object
*
199 _mesa_new_buffer_object( struct gl_context
*ctx
, GLuint name
, GLenum target
)
201 struct gl_buffer_object
*obj
;
205 obj
= MALLOC_STRUCT(gl_buffer_object
);
206 _mesa_initialize_buffer_object(obj
, name
, target
);
212 * Delete a buffer object.
214 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
217 _mesa_delete_buffer_object( struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
224 /* assign strange values here to help w/ debugging */
225 bufObj
->RefCount
= -1000;
228 _glthread_DESTROY_MUTEX(bufObj
->Mutex
);
235 * Set ptr to bufObj w/ reference counting.
238 _mesa_reference_buffer_object(struct gl_context
*ctx
,
239 struct gl_buffer_object
**ptr
,
240 struct gl_buffer_object
*bufObj
)
246 /* Unreference the old buffer */
247 GLboolean deleteFlag
= GL_FALSE
;
248 struct gl_buffer_object
*oldObj
= *ptr
;
250 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
251 ASSERT(oldObj
->RefCount
> 0);
254 printf("BufferObj %p %d DECR to %d\n",
255 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
257 deleteFlag
= (oldObj
->RefCount
== 0);
258 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
262 /* some sanity checking: don't delete a buffer still in use */
264 /* unfortunately, these tests are invalid during context tear-down */
265 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
266 ASSERT(ctx
->Array
.ElementArrayBufferObj
!= bufObj
);
267 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
270 ASSERT(ctx
->Driver
.DeleteBuffer
);
271 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
279 /* reference new buffer */
280 _glthread_LOCK_MUTEX(bufObj
->Mutex
);
281 if (bufObj
->RefCount
== 0) {
282 /* this buffer's being deleted (look just above) */
283 /* Not sure this can every really happen. Warn if it does. */
284 _mesa_problem(NULL
, "referencing deleted buffer object");
290 printf("BufferObj %p %d INCR to %d\n",
291 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
295 _glthread_UNLOCK_MUTEX(bufObj
->Mutex
);
301 * Initialize a buffer object to default values.
304 _mesa_initialize_buffer_object( struct gl_buffer_object
*obj
,
305 GLuint name
, GLenum target
)
309 memset(obj
, 0, sizeof(struct gl_buffer_object
));
310 _glthread_INIT_MUTEX(obj
->Mutex
);
313 obj
->Usage
= GL_STATIC_DRAW_ARB
;
314 obj
->AccessFlags
= DEFAULT_ACCESS
;
319 * Allocate space for and store data in a buffer object. Any data that was
320 * previously stored in the buffer object is lost. If \c data is \c NULL,
321 * memory will be allocated, but no copy will occur.
323 * This is the default callback for \c dd_function_table::BufferData()
324 * Note that all GL error checking will have been done already.
326 * \param ctx GL context.
327 * \param target Buffer object target on which to operate.
328 * \param size Size, in bytes, of the new data store.
329 * \param data Pointer to the data to store in the buffer object. This
330 * pointer may be \c NULL.
331 * \param usage Hints about how the data will be used.
332 * \param bufObj Object to be used.
334 * \return GL_TRUE for success, GL_FALSE for failure
335 * \sa glBufferDataARB, dd_function_table::BufferData.
338 _mesa_buffer_data( struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
339 const GLvoid
* data
, GLenum usage
,
340 struct gl_buffer_object
* bufObj
)
344 (void) ctx
; (void) target
;
346 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
348 bufObj
->Data
= (GLubyte
*) new_data
;
350 bufObj
->Usage
= usage
;
353 memcpy( bufObj
->Data
, data
, size
);
365 * Replace data in a subrange of buffer object. If the data range
366 * specified by \c size + \c offset extends beyond the end of the buffer or
367 * if \c data is \c NULL, no copy is performed.
369 * This is the default callback for \c dd_function_table::BufferSubData()
370 * Note that all GL error checking will have been done already.
372 * \param ctx GL context.
373 * \param target Buffer object target on which to operate.
374 * \param offset Offset of the first byte to be modified.
375 * \param size Size, in bytes, of the data range.
376 * \param data Pointer to the data to store in the buffer object.
377 * \param bufObj Object to be used.
379 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
382 _mesa_buffer_subdata( struct gl_context
*ctx
, GLenum target
, GLintptrARB offset
,
383 GLsizeiptrARB size
, const GLvoid
* data
,
384 struct gl_buffer_object
* bufObj
)
386 (void) ctx
; (void) target
;
388 /* this should have been caught in _mesa_BufferSubData() */
389 ASSERT(size
+ offset
<= bufObj
->Size
);
392 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
398 * Retrieve data from a subrange of buffer object. If the data range
399 * specified by \c size + \c offset extends beyond the end of the buffer or
400 * if \c data is \c NULL, no copy is performed.
402 * This is the default callback for \c dd_function_table::GetBufferSubData()
403 * Note that all GL error checking will have been done already.
405 * \param ctx GL context.
406 * \param target Buffer object target on which to operate.
407 * \param offset Offset of the first byte to be fetched.
408 * \param size Size, in bytes, of the data range.
409 * \param data Destination for data
410 * \param bufObj Object to be used.
412 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
415 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLenum target
, GLintptrARB offset
,
416 GLsizeiptrARB size
, GLvoid
* data
,
417 struct gl_buffer_object
* bufObj
)
419 (void) ctx
; (void) target
;
421 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
422 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
428 * Default callback for \c dd_function_tabel::MapBuffer().
430 * The function parameters will have been already tested for errors.
432 * \param ctx GL context.
433 * \param target Buffer object target on which to operate.
434 * \param access Information about how the buffer will be accessed.
435 * \param bufObj Object to be mapped.
436 * \return A pointer to the object's internal data store that can be accessed
439 * \sa glMapBufferARB, dd_function_table::MapBuffer
442 _mesa_buffer_map( struct gl_context
*ctx
, GLenum target
, GLenum access
,
443 struct gl_buffer_object
*bufObj
)
448 /* Just return a direct pointer to the data */
449 if (_mesa_bufferobj_mapped(bufObj
)) {
450 /* already mapped! */
453 bufObj
->Pointer
= bufObj
->Data
;
454 bufObj
->Length
= bufObj
->Size
;
456 return bufObj
->Pointer
;
461 * Default fallback for \c dd_function_table::MapBufferRange().
462 * Called via glMapBufferRange().
465 _mesa_buffer_map_range( struct gl_context
*ctx
, GLenum target
, GLintptr offset
,
466 GLsizeiptr length
, GLbitfield access
,
467 struct gl_buffer_object
*bufObj
)
471 assert(!_mesa_bufferobj_mapped(bufObj
));
472 /* Just return a direct pointer to the data */
473 bufObj
->Pointer
= bufObj
->Data
+ offset
;
474 bufObj
->Length
= length
;
475 bufObj
->Offset
= offset
;
476 bufObj
->AccessFlags
= access
;
477 return bufObj
->Pointer
;
482 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
483 * Called via glFlushMappedBufferRange().
486 _mesa_buffer_flush_mapped_range( struct gl_context
*ctx
, GLenum target
,
487 GLintptr offset
, GLsizeiptr length
,
488 struct gl_buffer_object
*obj
)
500 * Default callback for \c dd_function_table::MapBuffer().
502 * The input parameters will have been already tested for errors.
504 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
507 _mesa_buffer_unmap( struct gl_context
*ctx
, GLenum target
,
508 struct gl_buffer_object
*bufObj
)
512 /* XXX we might assert here that bufObj->Pointer is non-null */
513 bufObj
->Pointer
= NULL
;
516 bufObj
->AccessFlags
= 0x0;
522 * Default fallback for \c dd_function_table::CopyBufferSubData().
523 * Called via glCopyBuffserSubData().
526 _mesa_copy_buffer_subdata(struct gl_context
*ctx
,
527 struct gl_buffer_object
*src
,
528 struct gl_buffer_object
*dst
,
529 GLintptr readOffset
, GLintptr writeOffset
,
532 GLubyte
*srcPtr
, *dstPtr
;
534 /* buffer should not already be mapped */
535 assert(!_mesa_bufferobj_mapped(src
));
536 assert(!_mesa_bufferobj_mapped(dst
));
538 srcPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_READ_BUFFER
,
540 dstPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_WRITE_BUFFER
,
543 if (srcPtr
&& dstPtr
)
544 memcpy(dstPtr
+ writeOffset
, srcPtr
+ readOffset
, size
);
546 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_READ_BUFFER
, src
);
547 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_WRITE_BUFFER
, dst
);
553 * Initialize the state associated with buffer objects
556 _mesa_init_buffer_objects( struct gl_context
*ctx
)
558 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
559 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
561 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
562 ctx
->Shared
->NullBufferObj
);
563 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
,
564 ctx
->Shared
->NullBufferObj
);
566 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
567 ctx
->Shared
->NullBufferObj
);
568 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
569 ctx
->Shared
->NullBufferObj
);
574 _mesa_free_buffer_objects( struct gl_context
*ctx
)
576 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
577 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
, NULL
);
579 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
580 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
585 * Bind the specified target to buffer for the specified context.
586 * Called by glBindBuffer() and other functions.
589 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
591 struct gl_buffer_object
*oldBufObj
;
592 struct gl_buffer_object
*newBufObj
= NULL
;
593 struct gl_buffer_object
**bindTarget
= NULL
;
595 bindTarget
= get_buffer_target(ctx
, target
);
597 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
601 /* Get pointer to old buffer object (to be unbound) */
602 oldBufObj
= *bindTarget
;
603 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
604 return; /* rebinding the same buffer object- no change */
607 * Get pointer to new buffer object (newBufObj)
610 /* The spec says there's not a buffer object named 0, but we use
611 * one internally because it simplifies things.
613 newBufObj
= ctx
->Shared
->NullBufferObj
;
616 /* non-default buffer object */
617 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
618 if (!newBufObj
|| newBufObj
== &DummyBufferObject
) {
619 /* If this is a new buffer object id, or one which was generated but
620 * never used before, allocate a buffer object now.
622 ASSERT(ctx
->Driver
.NewBufferObject
);
623 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
625 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
628 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
632 /* bind new buffer */
633 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
635 /* Pass BindBuffer call to device driver */
636 if (ctx
->Driver
.BindBuffer
)
637 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
642 * Update the default buffer objects in the given context to reference those
643 * specified in the shared state and release those referencing the old
647 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
649 /* Bind the NullBufferObj to remove references to those
650 * in the shared context hash table.
652 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
653 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
654 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
655 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
660 * When we're about to read pixel data out of a PBO (via glDrawPixels,
661 * glTexImage, etc) or write data into a PBO (via glReadPixels,
662 * glGetTexImage, etc) we call this function to check that we're not
663 * going to read out of bounds.
665 * XXX This would also be a convenient time to check that the PBO isn't
666 * currently mapped. Whoever calls this function should check for that.
667 * Remember, we can't use a PBO when it's mapped!
669 * If we're not using a PBO, this is a no-op.
671 * \param width width of image to read/write
672 * \param height height of image to read/write
673 * \param depth depth of image to read/write
674 * \param format format of image to read/write
675 * \param type datatype of image to read/write
676 * \param ptr the user-provided pointer/offset
677 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
681 _mesa_validate_pbo_access(GLuint dimensions
,
682 const struct gl_pixelstore_attrib
*pack
,
683 GLsizei width
, GLsizei height
, GLsizei depth
,
684 GLenum format
, GLenum type
, const GLvoid
*ptr
)
687 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
689 if (!_mesa_is_bufferobj(pack
->BufferObj
))
690 return GL_TRUE
; /* no PBO, OK */
692 if (pack
->BufferObj
->Size
== 0)
696 /* get address of first pixel we'll read */
697 start
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
698 format
, type
, 0, 0, 0);
700 /* get address just past the last pixel we'll read */
701 end
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
702 format
, type
, depth
-1, height
-1, width
);
705 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
707 if ((const GLubyte
*) start
> sizeAddr
) {
708 /* This will catch negative values / wrap-around */
711 if ((const GLubyte
*) end
> sizeAddr
) {
712 /* Image read goes beyond end of buffer */
722 * For commands that read from a PBO (glDrawPixels, glTexImage,
723 * glPolygonStipple, etc), if we're reading from a PBO, map it read-only
724 * and return the pointer into the PBO. If we're not reading from a
725 * PBO, return \p src as-is.
726 * If non-null return, must call _mesa_unmap_pbo_source() when done.
728 * \return NULL if error, else pointer to start of data
731 _mesa_map_pbo_source(struct gl_context
*ctx
,
732 const struct gl_pixelstore_attrib
*unpack
,
737 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
738 /* unpack from PBO */
739 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
745 buf
= ADD_POINTERS(buf
, src
);
748 /* unpack from normal memory */
757 * Combine PBO-read validation and mapping.
758 * If any GL errors are detected, they'll be recorded and NULL returned.
759 * \sa _mesa_validate_pbo_access
760 * \sa _mesa_map_pbo_source
761 * A call to this function should have a matching call to
762 * _mesa_unmap_pbo_source().
765 _mesa_map_validate_pbo_source(struct gl_context
*ctx
,
767 const struct gl_pixelstore_attrib
*unpack
,
768 GLsizei width
, GLsizei height
, GLsizei depth
,
769 GLenum format
, GLenum type
, const GLvoid
*ptr
,
772 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
774 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
775 /* non-PBO access: no validation to be done */
779 if (!_mesa_validate_pbo_access(dimensions
, unpack
,
780 width
, height
, depth
, format
, type
, ptr
)) {
781 _mesa_error(ctx
, GL_INVALID_OPERATION
,
782 "%s(out of bounds PBO access)", where
);
786 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
787 /* buffer is already mapped - that's an error */
788 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
792 ptr
= _mesa_map_pbo_source(ctx
, unpack
, ptr
);
798 * Counterpart to _mesa_map_pbo_source()
801 _mesa_unmap_pbo_source(struct gl_context
*ctx
,
802 const struct gl_pixelstore_attrib
*unpack
)
804 ASSERT(unpack
!= &ctx
->Pack
); /* catch pack/unpack mismatch */
805 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
806 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
813 * For commands that write to a PBO (glReadPixels, glGetColorTable, etc),
814 * if we're writing to a PBO, map it write-only and return the pointer
815 * into the PBO. If we're not writing to a PBO, return \p dst as-is.
816 * If non-null return, must call _mesa_unmap_pbo_dest() when done.
818 * \return NULL if error, else pointer to start of data
821 _mesa_map_pbo_dest(struct gl_context
*ctx
,
822 const struct gl_pixelstore_attrib
*pack
,
827 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
829 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
835 buf
= ADD_POINTERS(buf
, dest
);
838 /* pack to normal memory */
847 * Combine PBO-write validation and mapping.
848 * If any GL errors are detected, they'll be recorded and NULL returned.
849 * \sa _mesa_validate_pbo_access
850 * \sa _mesa_map_pbo_dest
851 * A call to this function should have a matching call to
852 * _mesa_unmap_pbo_dest().
855 _mesa_map_validate_pbo_dest(struct gl_context
*ctx
,
857 const struct gl_pixelstore_attrib
*unpack
,
858 GLsizei width
, GLsizei height
, GLsizei depth
,
859 GLenum format
, GLenum type
, GLvoid
*ptr
,
862 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
864 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
865 /* non-PBO access: no validation to be done */
869 if (!_mesa_validate_pbo_access(dimensions
, unpack
,
870 width
, height
, depth
, format
, type
, ptr
)) {
871 _mesa_error(ctx
, GL_INVALID_OPERATION
,
872 "%s(out of bounds PBO access)", where
);
876 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
877 /* buffer is already mapped - that's an error */
878 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
882 ptr
= _mesa_map_pbo_dest(ctx
, unpack
, ptr
);
888 * Counterpart to _mesa_map_pbo_dest()
891 _mesa_unmap_pbo_dest(struct gl_context
*ctx
,
892 const struct gl_pixelstore_attrib
*pack
)
894 ASSERT(pack
!= &ctx
->Unpack
); /* catch pack/unpack mismatch */
895 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
896 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
, pack
->BufferObj
);
903 * Return the gl_buffer_object for the given ID.
904 * Always return NULL for ID 0.
906 struct gl_buffer_object
*
907 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
912 return (struct gl_buffer_object
*)
913 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
918 * If *ptr points to obj, set ptr = the Null/default buffer object.
919 * This is a helper for buffer object deletion.
920 * The GL spec says that deleting a buffer object causes it to get
921 * unbound from all arrays in the current context.
924 unbind(struct gl_context
*ctx
,
925 struct gl_buffer_object
**ptr
,
926 struct gl_buffer_object
*obj
)
929 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
935 * Plug default/fallback buffer object functions into the device
939 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
941 /* GL_ARB_vertex/pixel_buffer_object */
942 driver
->NewBufferObject
= _mesa_new_buffer_object
;
943 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
944 driver
->BindBuffer
= NULL
;
945 driver
->BufferData
= _mesa_buffer_data
;
946 driver
->BufferSubData
= _mesa_buffer_subdata
;
947 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
948 driver
->MapBuffer
= _mesa_buffer_map
;
949 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
951 /* GL_ARB_map_buffer_range */
952 driver
->MapBufferRange
= _mesa_buffer_map_range
;
953 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
955 /* GL_ARB_copy_buffer */
956 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
961 /**********************************************************************/
963 /**********************************************************************/
966 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
968 GET_CURRENT_CONTEXT(ctx
);
969 ASSERT_OUTSIDE_BEGIN_END(ctx
);
971 bind_buffer_object(ctx
, target
, buffer
);
976 * Delete a set of buffer objects.
978 * \param n Number of buffer objects to delete.
979 * \param ids Array of \c n buffer object IDs.
982 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
984 GET_CURRENT_CONTEXT(ctx
);
986 ASSERT_OUTSIDE_BEGIN_END(ctx
);
989 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
993 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
995 for (i
= 0; i
< n
; i
++) {
996 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
998 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
1001 ASSERT(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1003 if (_mesa_bufferobj_mapped(bufObj
)) {
1004 /* if mapped, unmap it now */
1005 ctx
->Driver
.UnmapBuffer(ctx
, 0, bufObj
);
1006 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1007 bufObj
->Pointer
= NULL
;
1010 /* unbind any vertex pointers bound to this buffer */
1011 unbind(ctx
, &arrayObj
->Vertex
.BufferObj
, bufObj
);
1012 unbind(ctx
, &arrayObj
->Weight
.BufferObj
, bufObj
);
1013 unbind(ctx
, &arrayObj
->Normal
.BufferObj
, bufObj
);
1014 unbind(ctx
, &arrayObj
->Color
.BufferObj
, bufObj
);
1015 unbind(ctx
, &arrayObj
->SecondaryColor
.BufferObj
, bufObj
);
1016 unbind(ctx
, &arrayObj
->FogCoord
.BufferObj
, bufObj
);
1017 unbind(ctx
, &arrayObj
->Index
.BufferObj
, bufObj
);
1018 unbind(ctx
, &arrayObj
->EdgeFlag
.BufferObj
, bufObj
);
1019 for (j
= 0; j
< Elements(arrayObj
->TexCoord
); j
++) {
1020 unbind(ctx
, &arrayObj
->TexCoord
[j
].BufferObj
, bufObj
);
1022 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
1023 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
1026 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1027 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
1029 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
1030 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1033 /* unbind any pixel pack/unpack pointers bound to this buffer */
1034 if (ctx
->Pack
.BufferObj
== bufObj
) {
1035 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1037 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1038 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1041 /* The ID is immediately freed for re-use */
1042 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
1043 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1047 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1052 * Generate a set of unique buffer object IDs and store them in \c buffer.
1054 * \param n Number of IDs to generate.
1055 * \param buffer Array of \c n locations to store the IDs.
1058 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
1060 GET_CURRENT_CONTEXT(ctx
);
1063 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1066 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
1075 * This must be atomic (generation and allocation of buffer object IDs)
1077 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1079 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1081 /* Insert the ID and pointer to dummy buffer object into hash table */
1082 for (i
= 0; i
< n
; i
++) {
1083 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
,
1084 &DummyBufferObject
);
1085 buffer
[i
] = first
+ i
;
1088 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1093 * Determine if ID is the name of a buffer object.
1095 * \param id ID of the potential buffer object.
1096 * \return \c GL_TRUE if \c id is the name of a buffer object,
1097 * \c GL_FALSE otherwise.
1099 GLboolean GLAPIENTRY
1100 _mesa_IsBufferARB(GLuint id
)
1102 struct gl_buffer_object
*bufObj
;
1103 GET_CURRENT_CONTEXT(ctx
);
1104 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1106 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1107 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1108 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1110 return bufObj
&& bufObj
!= &DummyBufferObject
;
1115 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
1116 const GLvoid
* data
, GLenum usage
)
1118 GET_CURRENT_CONTEXT(ctx
);
1119 struct gl_buffer_object
*bufObj
;
1120 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1123 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1128 case GL_STREAM_DRAW_ARB
:
1129 case GL_STREAM_READ_ARB
:
1130 case GL_STREAM_COPY_ARB
:
1131 case GL_STATIC_DRAW_ARB
:
1132 case GL_STATIC_READ_ARB
:
1133 case GL_STATIC_COPY_ARB
:
1134 case GL_DYNAMIC_DRAW_ARB
:
1135 case GL_DYNAMIC_READ_ARB
:
1136 case GL_DYNAMIC_COPY_ARB
:
1140 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
1144 bufObj
= get_buffer(ctx
, target
);
1146 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
1149 if (!_mesa_is_bufferobj(bufObj
)) {
1150 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer 0)" );
1154 if (_mesa_bufferobj_mapped(bufObj
)) {
1155 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1156 ctx
->Driver
.UnmapBuffer(ctx
, target
, bufObj
);
1157 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1158 ASSERT(bufObj
->Pointer
== NULL
);
1161 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1163 bufObj
->Written
= GL_TRUE
;
1166 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1167 bufObj
->Name
, size
, data
, usage
);
1174 ASSERT(ctx
->Driver
.BufferData
);
1175 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1176 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1182 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1183 GLsizeiptrARB size
, const GLvoid
* data
)
1185 GET_CURRENT_CONTEXT(ctx
);
1186 struct gl_buffer_object
*bufObj
;
1187 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1189 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1190 "glBufferSubDataARB" );
1192 /* error already recorded */
1199 bufObj
->Written
= GL_TRUE
;
1201 ASSERT(ctx
->Driver
.BufferSubData
);
1202 ctx
->Driver
.BufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1207 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1208 GLsizeiptrARB size
, void * data
)
1210 GET_CURRENT_CONTEXT(ctx
);
1211 struct gl_buffer_object
*bufObj
;
1212 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1214 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1215 "glGetBufferSubDataARB" );
1217 /* error already recorded */
1221 ASSERT(ctx
->Driver
.GetBufferSubData
);
1222 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1227 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1229 GET_CURRENT_CONTEXT(ctx
);
1230 struct gl_buffer_object
* bufObj
;
1231 GLbitfield accessFlags
;
1234 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1237 case GL_READ_ONLY_ARB
:
1238 accessFlags
= GL_MAP_READ_BIT
;
1240 case GL_WRITE_ONLY_ARB
:
1241 accessFlags
= GL_MAP_WRITE_BIT
;
1243 case GL_READ_WRITE_ARB
:
1244 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1247 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1251 bufObj
= get_buffer(ctx
, target
);
1253 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
1256 if (!_mesa_is_bufferobj(bufObj
)) {
1257 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(buffer 0)" );
1260 if (_mesa_bufferobj_mapped(bufObj
)) {
1261 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1265 ASSERT(ctx
->Driver
.MapBuffer
);
1266 map
= ctx
->Driver
.MapBuffer( ctx
, target
, access
, bufObj
);
1268 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1272 /* The driver callback should have set these fields.
1273 * This is important because other modules (like VBO) might call
1274 * the driver function directly.
1276 ASSERT(bufObj
->Pointer
== map
);
1277 ASSERT(bufObj
->Length
== bufObj
->Size
);
1278 ASSERT(bufObj
->Offset
== 0);
1279 bufObj
->AccessFlags
= accessFlags
;
1282 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1283 bufObj
->Written
= GL_TRUE
;
1286 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1287 bufObj
->Name
, bufObj
->Size
, access
);
1288 if (access
== GL_WRITE_ONLY_ARB
) {
1290 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1291 for (i
= 0; i
< bufObj
->Size
; i
++)
1298 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1300 /* buffer is 100 bytes larger than requested, fill with magic value */
1301 for (i
= 0; i
< 100; i
++) {
1302 buf
[bufObj
->Size
- i
- 1] = 123;
1307 return bufObj
->Pointer
;
1311 GLboolean GLAPIENTRY
1312 _mesa_UnmapBufferARB(GLenum target
)
1314 GET_CURRENT_CONTEXT(ctx
);
1315 struct gl_buffer_object
*bufObj
;
1316 GLboolean status
= GL_TRUE
;
1317 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1319 bufObj
= get_buffer(ctx
, target
);
1321 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1324 if (!_mesa_is_bufferobj(bufObj
)) {
1325 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1328 if (!_mesa_bufferobj_mapped(bufObj
)) {
1329 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1334 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1335 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1337 /* check that last 100 bytes are still = magic value */
1338 for (i
= 0; i
< 100; i
++) {
1339 GLuint pos
= bufObj
->Size
- i
- 1;
1340 if (buf
[pos
] != 123) {
1341 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1342 " at position %d (value = %u)\n",
1350 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1351 GLuint i
, unchanged
= 0;
1352 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1354 /* check which bytes changed */
1355 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1356 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1363 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1364 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1369 status
= ctx
->Driver
.UnmapBuffer( ctx
, target
, bufObj
);
1370 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1371 ASSERT(bufObj
->Pointer
== NULL
);
1372 ASSERT(bufObj
->Offset
== 0);
1373 ASSERT(bufObj
->Length
== 0);
1380 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1382 GET_CURRENT_CONTEXT(ctx
);
1383 struct gl_buffer_object
*bufObj
;
1384 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1386 bufObj
= get_buffer(ctx
, target
);
1388 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(target)" );
1391 if (!_mesa_is_bufferobj(bufObj
)) {
1392 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferParameterivARB" );
1397 case GL_BUFFER_SIZE_ARB
:
1398 *params
= (GLint
) bufObj
->Size
;
1400 case GL_BUFFER_USAGE_ARB
:
1401 *params
= bufObj
->Usage
;
1403 case GL_BUFFER_ACCESS_ARB
:
1404 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1406 case GL_BUFFER_MAPPED_ARB
:
1407 *params
= _mesa_bufferobj_mapped(bufObj
);
1409 case GL_BUFFER_ACCESS_FLAGS
:
1410 if (ctx
->VersionMajor
< 3)
1412 *params
= bufObj
->AccessFlags
;
1414 case GL_BUFFER_MAP_OFFSET
:
1415 if (ctx
->VersionMajor
< 3)
1417 *params
= (GLint
) bufObj
->Offset
;
1419 case GL_BUFFER_MAP_LENGTH
:
1420 if (ctx
->VersionMajor
< 3)
1422 *params
= (GLint
) bufObj
->Length
;
1425 ; /* fall-through */
1429 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1430 _mesa_lookup_enum_by_nr(pname
));
1436 * This is pretty much a duplicate of GetBufferParameteriv() but the
1437 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1440 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1442 GET_CURRENT_CONTEXT(ctx
);
1443 struct gl_buffer_object
*bufObj
;
1444 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1446 bufObj
= get_buffer(ctx
, target
);
1448 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(target)" );
1451 if (!_mesa_is_bufferobj(bufObj
)) {
1452 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferParameteri64v" );
1457 case GL_BUFFER_SIZE_ARB
:
1458 *params
= bufObj
->Size
;
1460 case GL_BUFFER_USAGE_ARB
:
1461 *params
= bufObj
->Usage
;
1463 case GL_BUFFER_ACCESS_ARB
:
1464 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1466 case GL_BUFFER_ACCESS_FLAGS
:
1467 if (ctx
->VersionMajor
< 3)
1469 *params
= bufObj
->AccessFlags
;
1471 case GL_BUFFER_MAPPED_ARB
:
1472 *params
= _mesa_bufferobj_mapped(bufObj
);
1474 case GL_BUFFER_MAP_OFFSET
:
1475 if (ctx
->VersionMajor
< 3)
1477 *params
= bufObj
->Offset
;
1479 case GL_BUFFER_MAP_LENGTH
:
1480 if (ctx
->VersionMajor
< 3)
1482 *params
= bufObj
->Length
;
1485 ; /* fall-through */
1489 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1490 _mesa_lookup_enum_by_nr(pname
));
1495 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1497 GET_CURRENT_CONTEXT(ctx
);
1498 struct gl_buffer_object
* bufObj
;
1499 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1501 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1502 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1506 bufObj
= get_buffer(ctx
, target
);
1508 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1511 if (!_mesa_is_bufferobj(bufObj
)) {
1512 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1516 *params
= bufObj
->Pointer
;
1521 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1522 GLintptr readOffset
, GLintptr writeOffset
,
1525 GET_CURRENT_CONTEXT(ctx
);
1526 struct gl_buffer_object
*src
, *dst
;
1527 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1529 src
= get_buffer(ctx
, readTarget
);
1530 if (!src
|| !_mesa_is_bufferobj(src
)) {
1531 _mesa_error(ctx
, GL_INVALID_ENUM
,
1532 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget
);
1536 dst
= get_buffer(ctx
, writeTarget
);
1537 if (!dst
|| !_mesa_is_bufferobj(dst
)) {
1538 _mesa_error(ctx
, GL_INVALID_ENUM
,
1539 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget
);
1543 if (_mesa_bufferobj_mapped(src
)) {
1544 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1545 "glCopyBuffserSubData(readBuffer is mapped)");
1549 if (_mesa_bufferobj_mapped(dst
)) {
1550 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1551 "glCopyBuffserSubData(writeBuffer is mapped)");
1555 if (readOffset
< 0) {
1556 _mesa_error(ctx
, GL_INVALID_VALUE
,
1557 "glCopyBuffserSubData(readOffset = %d)", (int) readOffset
);
1561 if (writeOffset
< 0) {
1562 _mesa_error(ctx
, GL_INVALID_VALUE
,
1563 "glCopyBuffserSubData(writeOffset = %d)", (int) writeOffset
);
1567 if (readOffset
+ size
> src
->Size
) {
1568 _mesa_error(ctx
, GL_INVALID_VALUE
,
1569 "glCopyBuffserSubData(readOffset + size = %d)",
1570 (int) (readOffset
+ size
));
1574 if (writeOffset
+ size
> dst
->Size
) {
1575 _mesa_error(ctx
, GL_INVALID_VALUE
,
1576 "glCopyBuffserSubData(writeOffset + size = %d)",
1577 (int) (writeOffset
+ size
));
1582 if (readOffset
+ size
<= writeOffset
) {
1585 else if (writeOffset
+ size
<= readOffset
) {
1589 /* overlapping src/dst is illegal */
1590 _mesa_error(ctx
, GL_INVALID_VALUE
,
1591 "glCopyBuffserSubData(overlapping src/dst)");
1596 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1601 * See GL_ARB_map_buffer_range spec
1604 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1607 GET_CURRENT_CONTEXT(ctx
);
1608 struct gl_buffer_object
*bufObj
;
1611 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1613 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1614 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1615 "glMapBufferRange(extension not supported)");
1620 _mesa_error(ctx
, GL_INVALID_VALUE
,
1621 "glMapBufferRange(offset = %ld)", (long)offset
);
1626 _mesa_error(ctx
, GL_INVALID_VALUE
,
1627 "glMapBufferRange(length = %ld)", (long)length
);
1631 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1632 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1633 "glMapBufferRange(access indicates neither read or write)");
1637 if (access
& GL_MAP_READ_BIT
) {
1638 if ((access
& GL_MAP_INVALIDATE_RANGE_BIT
) ||
1639 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) ||
1640 (access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
1641 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1642 "glMapBufferRange(invalid access flags)");
1647 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1648 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1649 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1650 "glMapBufferRange(invalid access flags)");
1654 bufObj
= get_buffer(ctx
, target
);
1655 if (!bufObj
|| !_mesa_is_bufferobj(bufObj
)) {
1656 _mesa_error(ctx
, GL_INVALID_ENUM
,
1657 "glMapBufferRange(target = 0x%x)", target
);
1661 if (offset
+ length
> bufObj
->Size
) {
1662 _mesa_error(ctx
, GL_INVALID_VALUE
,
1663 "glMapBufferRange(offset + length > size)");
1667 if (_mesa_bufferobj_mapped(bufObj
)) {
1668 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1669 "glMapBufferRange(buffer already mapped)");
1673 ASSERT(ctx
->Driver
.MapBufferRange
);
1674 map
= ctx
->Driver
.MapBufferRange(ctx
, target
, offset
, length
,
1677 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1680 /* The driver callback should have set all these fields.
1681 * This is important because other modules (like VBO) might call
1682 * the driver function directly.
1684 ASSERT(bufObj
->Pointer
== map
);
1685 ASSERT(bufObj
->Length
== length
);
1686 ASSERT(bufObj
->Offset
== offset
);
1687 ASSERT(bufObj
->AccessFlags
== access
);
1695 * See GL_ARB_map_buffer_range spec
1698 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1700 GET_CURRENT_CONTEXT(ctx
);
1701 struct gl_buffer_object
*bufObj
;
1702 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1704 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1705 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1706 "glMapBufferRange(extension not supported)");
1711 _mesa_error(ctx
, GL_INVALID_VALUE
,
1712 "glMapBufferRange(offset = %ld)", (long)offset
);
1717 _mesa_error(ctx
, GL_INVALID_VALUE
,
1718 "glMapBufferRange(length = %ld)", (long)length
);
1722 bufObj
= get_buffer(ctx
, target
);
1724 _mesa_error(ctx
, GL_INVALID_ENUM
,
1725 "glMapBufferRange(target = 0x%x)", target
);
1729 if (!_mesa_is_bufferobj(bufObj
)) {
1730 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1731 "glMapBufferRange(current buffer is 0)");
1735 if (!_mesa_bufferobj_mapped(bufObj
)) {
1736 /* buffer is not mapped */
1737 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1738 "glMapBufferRange(buffer is not mapped)");
1742 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1743 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1744 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1748 if (offset
+ length
> bufObj
->Length
) {
1749 _mesa_error(ctx
, GL_INVALID_VALUE
,
1750 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1751 (long)offset
, (long)length
, (long)bufObj
->Length
);
1755 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1757 if (ctx
->Driver
.FlushMappedBufferRange
)
1758 ctx
->Driver
.FlushMappedBufferRange(ctx
, target
, offset
, length
, bufObj
);
1762 #if FEATURE_APPLE_object_purgeable
1764 _mesa_BufferObjectPurgeable(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 "glObjectPurgeable(name = 0x%x)", name
);
1775 if (!_mesa_is_bufferobj(bufObj
)) {
1776 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1780 if (bufObj
->Purgeable
) {
1781 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1782 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1783 return GL_VOLATILE_APPLE
;
1786 bufObj
->Purgeable
= GL_TRUE
;
1788 retval
= GL_VOLATILE_APPLE
;
1789 if (ctx
->Driver
.BufferObjectPurgeable
)
1790 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1797 _mesa_RenderObjectPurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1799 struct gl_renderbuffer
*bufObj
;
1802 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1804 _mesa_error(ctx
, GL_INVALID_VALUE
,
1805 "glObjectUnpurgeable(name = 0x%x)", name
);
1809 if (bufObj
->Purgeable
) {
1810 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1811 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1812 return GL_VOLATILE_APPLE
;
1815 bufObj
->Purgeable
= GL_TRUE
;
1817 retval
= GL_VOLATILE_APPLE
;
1818 if (ctx
->Driver
.RenderObjectPurgeable
)
1819 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1826 _mesa_TextureObjectPurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1828 struct gl_texture_object
*bufObj
;
1831 bufObj
= _mesa_lookup_texture(ctx
, name
);
1833 _mesa_error(ctx
, GL_INVALID_VALUE
,
1834 "glObjectPurgeable(name = 0x%x)", name
);
1838 if (bufObj
->Purgeable
) {
1839 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1840 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1841 return GL_VOLATILE_APPLE
;
1844 bufObj
->Purgeable
= GL_TRUE
;
1846 retval
= GL_VOLATILE_APPLE
;
1847 if (ctx
->Driver
.TextureObjectPurgeable
)
1848 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1855 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1859 GET_CURRENT_CONTEXT(ctx
);
1860 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1863 _mesa_error(ctx
, GL_INVALID_VALUE
,
1864 "glObjectPurgeable(name = 0x%x)", name
);
1869 case GL_VOLATILE_APPLE
:
1870 case GL_RELEASED_APPLE
:
1874 _mesa_error(ctx
, GL_INVALID_ENUM
,
1875 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1880 switch (objectType
) {
1882 retval
= _mesa_TextureObjectPurgeable (ctx
, name
, option
);
1884 case GL_RENDERBUFFER_EXT
:
1885 retval
= _mesa_RenderObjectPurgeable (ctx
, name
, option
);
1887 case GL_BUFFER_OBJECT_APPLE
:
1888 retval
= _mesa_BufferObjectPurgeable (ctx
, name
, option
);
1891 _mesa_error(ctx
, GL_INVALID_ENUM
,
1892 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1897 /* In strict conformance to the spec, we must only return VOLATILE when
1898 * when passed the VOLATILE option. Madness.
1900 * XXX First fix the spec, then fix me.
1902 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1907 _mesa_BufferObjectUnpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1909 struct gl_buffer_object
*bufObj
;
1912 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1914 _mesa_error(ctx
, GL_INVALID_VALUE
,
1915 "glObjectUnpurgeable(name = 0x%x)", name
);
1919 if (! bufObj
->Purgeable
) {
1920 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1921 "glObjectUnpurgeable(name = 0x%x) object is "
1922 " already \"unpurged\"", name
);
1926 bufObj
->Purgeable
= GL_FALSE
;
1929 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1930 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1937 _mesa_RenderObjectUnpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1939 struct gl_renderbuffer
*bufObj
;
1942 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1944 _mesa_error(ctx
, GL_INVALID_VALUE
,
1945 "glObjectUnpurgeable(name = 0x%x)", name
);
1949 if (! bufObj
->Purgeable
) {
1950 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1951 "glObjectUnpurgeable(name = 0x%x) object is "
1952 " already \"unpurged\"", name
);
1956 bufObj
->Purgeable
= GL_FALSE
;
1959 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1960 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1967 _mesa_TextureObjectUnpurgeable(struct gl_context
*ctx
, GLuint name
, GLenum option
)
1969 struct gl_texture_object
*bufObj
;
1972 bufObj
= _mesa_lookup_texture(ctx
, name
);
1974 _mesa_error(ctx
, GL_INVALID_VALUE
,
1975 "glObjectUnpurgeable(name = 0x%x)", name
);
1979 if (! bufObj
->Purgeable
) {
1980 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1981 "glObjectUnpurgeable(name = 0x%x) object is"
1982 " already \"unpurged\"", name
);
1986 bufObj
->Purgeable
= GL_FALSE
;
1989 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1990 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1997 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1999 GET_CURRENT_CONTEXT(ctx
);
2000 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2003 _mesa_error(ctx
, GL_INVALID_VALUE
,
2004 "glObjectUnpurgeable(name = 0x%x)", name
);
2009 case GL_RETAINED_APPLE
:
2010 case GL_UNDEFINED_APPLE
:
2014 _mesa_error(ctx
, GL_INVALID_ENUM
,
2015 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2020 switch (objectType
) {
2021 case GL_BUFFER_OBJECT_APPLE
:
2022 return _mesa_BufferObjectUnpurgeable(ctx
, name
, option
);
2024 return _mesa_TextureObjectUnpurgeable(ctx
, name
, option
);
2025 case GL_RENDERBUFFER_EXT
:
2026 return _mesa_RenderObjectUnpurgeable(ctx
, name
, option
);
2028 _mesa_error(ctx
, GL_INVALID_ENUM
,
2029 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2037 _mesa_GetBufferObjectParameterivAPPLE(struct gl_context
*ctx
, GLuint name
,
2038 GLenum pname
, GLint
* params
)
2040 struct gl_buffer_object
*bufObj
;
2042 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2044 _mesa_error(ctx
, GL_INVALID_VALUE
,
2045 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
2050 case GL_PURGEABLE_APPLE
:
2051 *params
= bufObj
->Purgeable
;
2054 _mesa_error(ctx
, GL_INVALID_ENUM
,
2055 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2063 _mesa_GetRenderObjectParameterivAPPLE(struct gl_context
*ctx
, GLuint name
,
2064 GLenum pname
, GLint
* params
)
2066 struct gl_renderbuffer
*bufObj
;
2068 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
2070 _mesa_error(ctx
, GL_INVALID_VALUE
,
2071 "glObjectUnpurgeable(name = 0x%x)", name
);
2076 case GL_PURGEABLE_APPLE
:
2077 *params
= bufObj
->Purgeable
;
2080 _mesa_error(ctx
, GL_INVALID_ENUM
,
2081 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2089 _mesa_GetTextureObjectParameterivAPPLE(struct gl_context
*ctx
, GLuint name
,
2090 GLenum pname
, GLint
* params
)
2092 struct gl_texture_object
*bufObj
;
2094 bufObj
= _mesa_lookup_texture(ctx
, name
);
2096 _mesa_error(ctx
, GL_INVALID_VALUE
,
2097 "glObjectUnpurgeable(name = 0x%x)", name
);
2102 case GL_PURGEABLE_APPLE
:
2103 *params
= bufObj
->Purgeable
;
2106 _mesa_error(ctx
, GL_INVALID_ENUM
,
2107 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2115 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
2118 GET_CURRENT_CONTEXT(ctx
);
2121 _mesa_error(ctx
, GL_INVALID_VALUE
,
2122 "glGetObjectParameteriv(name = 0x%x)", name
);
2126 switch (objectType
) {
2128 _mesa_GetTextureObjectParameterivAPPLE (ctx
, name
, pname
, params
);
2130 case GL_BUFFER_OBJECT_APPLE
:
2131 _mesa_GetBufferObjectParameterivAPPLE (ctx
, name
, pname
, params
);
2133 case GL_RENDERBUFFER_EXT
:
2134 _mesa_GetRenderObjectParameterivAPPLE (ctx
, name
, pname
, params
);
2137 _mesa_error(ctx
, GL_INVALID_ENUM
,
2138 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2143 #endif /* FEATURE_APPLE_object_purgeable */