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 * Return pointer to address of a buffer object target.
59 * \param ctx the GL context
60 * \param target the buffer object target to be retrieved.
61 * \return pointer to pointer to the buffer object bound to \c target in the
62 * specified context or \c NULL if \c target is invalid.
64 static INLINE
struct gl_buffer_object
**
65 get_buffer_target(GLcontext
*ctx
, GLenum target
)
68 case GL_ARRAY_BUFFER_ARB
:
69 return &ctx
->Array
.ArrayBufferObj
;
70 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
71 return &ctx
->Array
.ElementArrayBufferObj
;
72 case GL_PIXEL_PACK_BUFFER_EXT
:
73 return &ctx
->Pack
.BufferObj
;
74 case GL_PIXEL_UNPACK_BUFFER_EXT
:
75 return &ctx
->Unpack
.BufferObj
;
76 case GL_COPY_READ_BUFFER
:
77 return &ctx
->CopyReadBuffer
;
78 case GL_COPY_WRITE_BUFFER
:
79 return &ctx
->CopyWriteBuffer
;
80 #if FEATURE_EXT_transform_feedback
81 case GL_TRANSFORM_FEEDBACK_BUFFER
:
82 if (ctx
->Extensions
.EXT_transform_feedback
) {
83 return &ctx
->TransformFeedback
.CurrentBuffer
;
95 * Get the buffer object bound to the specified target in a GL context.
96 * \param ctx the GL context
97 * \param target the buffer object target to be retrieved.
98 * \return pointer to the buffer object bound to \c target in the
99 * specified context or \c NULL if \c target is invalid.
101 static INLINE
struct gl_buffer_object
*
102 get_buffer(GLcontext
*ctx
, GLenum target
)
104 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
112 * Convert a GLbitfield describing the mapped buffer access flags
113 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
116 simplified_access_mode(GLbitfield access
)
118 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
119 if ((access
& rwFlags
) == rwFlags
)
120 return GL_READ_WRITE
;
121 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
123 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
124 return GL_WRITE_ONLY
;
125 return GL_READ_WRITE
; /* this should never happen, but no big deal */
130 * Tests the subdata range parameters and sets the GL error code for
131 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
133 * \param ctx GL context.
134 * \param target Buffer object target on which to operate.
135 * \param offset Offset of the first byte of the subdata range.
136 * \param size Size, in bytes, of the subdata range.
137 * \param caller Name of calling function for recording errors.
138 * \return A pointer to the buffer object bound to \c target in the
139 * specified context or \c NULL if any of the parameter or state
140 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
143 * \sa glBufferSubDataARB, glGetBufferSubDataARB
145 static struct gl_buffer_object
*
146 buffer_object_subdata_range_good( GLcontext
* ctx
, GLenum target
,
147 GLintptrARB offset
, GLsizeiptrARB size
,
150 struct gl_buffer_object
*bufObj
;
153 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
158 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
162 bufObj
= get_buffer(ctx
, target
);
164 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", caller
);
167 if (!_mesa_is_bufferobj(bufObj
)) {
168 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
171 if (offset
+ size
> bufObj
->Size
) {
172 _mesa_error(ctx
, GL_INVALID_VALUE
,
173 "%s(size + offset > buffer size)", caller
);
176 if (_mesa_bufferobj_mapped(bufObj
)) {
177 /* Buffer is currently mapped */
178 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
187 * Allocate and initialize a new buffer object.
189 * Default callback for the \c dd_function_table::NewBufferObject() hook.
191 static struct gl_buffer_object
*
192 _mesa_new_buffer_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
194 struct gl_buffer_object
*obj
;
198 obj
= MALLOC_STRUCT(gl_buffer_object
);
199 _mesa_initialize_buffer_object(obj
, name
, target
);
205 * Delete a buffer object.
207 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
210 _mesa_delete_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*bufObj
)
217 /* assign strange values here to help w/ debugging */
218 bufObj
->RefCount
= -1000;
221 _glthread_DESTROY_MUTEX(bufObj
->Mutex
);
228 * Set ptr to bufObj w/ reference counting.
231 _mesa_reference_buffer_object(GLcontext
*ctx
,
232 struct gl_buffer_object
**ptr
,
233 struct gl_buffer_object
*bufObj
)
239 /* Unreference the old buffer */
240 GLboolean deleteFlag
= GL_FALSE
;
241 struct gl_buffer_object
*oldObj
= *ptr
;
243 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
244 ASSERT(oldObj
->RefCount
> 0);
247 printf("BufferObj %p %d DECR to %d\n",
248 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
250 deleteFlag
= (oldObj
->RefCount
== 0);
251 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
255 /* some sanity checking: don't delete a buffer still in use */
257 /* unfortunately, these tests are invalid during context tear-down */
258 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
259 ASSERT(ctx
->Array
.ElementArrayBufferObj
!= bufObj
);
260 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
263 ASSERT(ctx
->Driver
.DeleteBuffer
);
264 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
272 /* reference new buffer */
273 _glthread_LOCK_MUTEX(bufObj
->Mutex
);
274 if (bufObj
->RefCount
== 0) {
275 /* this buffer's being deleted (look just above) */
276 /* Not sure this can every really happen. Warn if it does. */
277 _mesa_problem(NULL
, "referencing deleted buffer object");
283 printf("BufferObj %p %d INCR to %d\n",
284 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
288 _glthread_UNLOCK_MUTEX(bufObj
->Mutex
);
294 * Initialize a buffer object to default values.
297 _mesa_initialize_buffer_object( struct gl_buffer_object
*obj
,
298 GLuint name
, GLenum target
)
302 memset(obj
, 0, sizeof(struct gl_buffer_object
));
303 _glthread_INIT_MUTEX(obj
->Mutex
);
306 obj
->Usage
= GL_STATIC_DRAW_ARB
;
307 obj
->AccessFlags
= DEFAULT_ACCESS
;
312 * Allocate space for and store data in a buffer object. Any data that was
313 * previously stored in the buffer object is lost. If \c data is \c NULL,
314 * memory will be allocated, but no copy will occur.
316 * This is the default callback for \c dd_function_table::BufferData()
317 * Note that all GL error checking will have been done already.
319 * \param ctx GL context.
320 * \param target Buffer object target on which to operate.
321 * \param size Size, in bytes, of the new data store.
322 * \param data Pointer to the data to store in the buffer object. This
323 * pointer may be \c NULL.
324 * \param usage Hints about how the data will be used.
325 * \param bufObj Object to be used.
327 * \return GL_TRUE for success, GL_FALSE for failure
328 * \sa glBufferDataARB, dd_function_table::BufferData.
331 _mesa_buffer_data( GLcontext
*ctx
, GLenum target
, GLsizeiptrARB size
,
332 const GLvoid
* data
, GLenum usage
,
333 struct gl_buffer_object
* bufObj
)
337 (void) ctx
; (void) target
;
339 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
341 bufObj
->Data
= (GLubyte
*) new_data
;
343 bufObj
->Usage
= usage
;
346 memcpy( bufObj
->Data
, data
, size
);
358 * Replace data in a subrange of buffer object. If the data range
359 * specified by \c size + \c offset extends beyond the end of the buffer or
360 * if \c data is \c NULL, no copy is performed.
362 * This is the default callback for \c dd_function_table::BufferSubData()
363 * Note that all GL error checking will have been done already.
365 * \param ctx GL context.
366 * \param target Buffer object target on which to operate.
367 * \param offset Offset of the first byte to be modified.
368 * \param size Size, in bytes, of the data range.
369 * \param data Pointer to the data to store in the buffer object.
370 * \param bufObj Object to be used.
372 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
375 _mesa_buffer_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
376 GLsizeiptrARB size
, const GLvoid
* data
,
377 struct gl_buffer_object
* bufObj
)
379 (void) ctx
; (void) target
;
381 /* this should have been caught in _mesa_BufferSubData() */
382 ASSERT(size
+ offset
<= bufObj
->Size
);
385 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
391 * Retrieve data from a subrange of buffer object. If the data range
392 * specified by \c size + \c offset extends beyond the end of the buffer or
393 * if \c data is \c NULL, no copy is performed.
395 * This is the default callback for \c dd_function_table::GetBufferSubData()
396 * Note that all GL error checking will have been done already.
398 * \param ctx GL context.
399 * \param target Buffer object target on which to operate.
400 * \param offset Offset of the first byte to be fetched.
401 * \param size Size, in bytes, of the data range.
402 * \param data Destination for data
403 * \param bufObj Object to be used.
405 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
408 _mesa_buffer_get_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
409 GLsizeiptrARB size
, GLvoid
* data
,
410 struct gl_buffer_object
* bufObj
)
412 (void) ctx
; (void) target
;
414 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
415 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
421 * Default callback for \c dd_function_tabel::MapBuffer().
423 * The function parameters will have been already tested for errors.
425 * \param ctx GL context.
426 * \param target Buffer object target on which to operate.
427 * \param access Information about how the buffer will be accessed.
428 * \param bufObj Object to be mapped.
429 * \return A pointer to the object's internal data store that can be accessed
432 * \sa glMapBufferARB, dd_function_table::MapBuffer
435 _mesa_buffer_map( GLcontext
*ctx
, GLenum target
, GLenum access
,
436 struct gl_buffer_object
*bufObj
)
441 /* Just return a direct pointer to the data */
442 if (_mesa_bufferobj_mapped(bufObj
)) {
443 /* already mapped! */
446 bufObj
->Pointer
= bufObj
->Data
;
447 bufObj
->Length
= bufObj
->Size
;
449 return bufObj
->Pointer
;
454 * Default fallback for \c dd_function_table::MapBufferRange().
455 * Called via glMapBufferRange().
458 _mesa_buffer_map_range( GLcontext
*ctx
, GLenum target
, GLintptr offset
,
459 GLsizeiptr length
, GLbitfield access
,
460 struct gl_buffer_object
*bufObj
)
464 assert(!_mesa_bufferobj_mapped(bufObj
));
465 /* Just return a direct pointer to the data */
466 bufObj
->Pointer
= bufObj
->Data
+ offset
;
467 bufObj
->Length
= length
;
468 bufObj
->Offset
= offset
;
469 bufObj
->AccessFlags
= access
;
470 return bufObj
->Pointer
;
475 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
476 * Called via glFlushMappedBufferRange().
479 _mesa_buffer_flush_mapped_range( GLcontext
*ctx
, GLenum target
,
480 GLintptr offset
, GLsizeiptr length
,
481 struct gl_buffer_object
*obj
)
493 * Default callback for \c dd_function_table::MapBuffer().
495 * The input parameters will have been already tested for errors.
497 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
500 _mesa_buffer_unmap( GLcontext
*ctx
, GLenum target
,
501 struct gl_buffer_object
*bufObj
)
505 /* XXX we might assert here that bufObj->Pointer is non-null */
506 bufObj
->Pointer
= NULL
;
509 bufObj
->AccessFlags
= 0x0;
515 * Default fallback for \c dd_function_table::CopyBufferSubData().
516 * Called via glCopyBuffserSubData().
519 _mesa_copy_buffer_subdata(GLcontext
*ctx
,
520 struct gl_buffer_object
*src
,
521 struct gl_buffer_object
*dst
,
522 GLintptr readOffset
, GLintptr writeOffset
,
525 GLubyte
*srcPtr
, *dstPtr
;
527 /* buffer should not already be mapped */
528 assert(!_mesa_bufferobj_mapped(src
));
529 assert(!_mesa_bufferobj_mapped(dst
));
531 srcPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_READ_BUFFER
,
533 dstPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_WRITE_BUFFER
,
536 if (srcPtr
&& dstPtr
)
537 memcpy(dstPtr
+ writeOffset
, srcPtr
+ readOffset
, size
);
539 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_READ_BUFFER
, src
);
540 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_WRITE_BUFFER
, dst
);
546 * Initialize the state associated with buffer objects
549 _mesa_init_buffer_objects( GLcontext
*ctx
)
551 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
552 ctx
->Shared
->NullBufferObj
);
553 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
,
554 ctx
->Shared
->NullBufferObj
);
556 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
557 ctx
->Shared
->NullBufferObj
);
558 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
559 ctx
->Shared
->NullBufferObj
);
564 _mesa_free_buffer_objects( GLcontext
*ctx
)
566 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
567 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
, NULL
);
569 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
570 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
575 * Bind the specified target to buffer for the specified context.
576 * Called by glBindBuffer() and other functions.
579 bind_buffer_object(GLcontext
*ctx
, GLenum target
, GLuint buffer
)
581 struct gl_buffer_object
*oldBufObj
;
582 struct gl_buffer_object
*newBufObj
= NULL
;
583 struct gl_buffer_object
**bindTarget
= NULL
;
585 bindTarget
= get_buffer_target(ctx
, target
);
587 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
591 /* Get pointer to old buffer object (to be unbound) */
592 oldBufObj
= *bindTarget
;
593 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
594 return; /* rebinding the same buffer object- no change */
597 * Get pointer to new buffer object (newBufObj)
600 /* The spec says there's not a buffer object named 0, but we use
601 * one internally because it simplifies things.
603 newBufObj
= ctx
->Shared
->NullBufferObj
;
606 /* non-default buffer object */
607 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
609 /* if this is a new buffer object id, allocate a buffer object now */
610 ASSERT(ctx
->Driver
.NewBufferObject
);
611 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
613 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
616 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
620 /* bind new buffer */
621 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
623 /* Pass BindBuffer call to device driver */
624 if (ctx
->Driver
.BindBuffer
)
625 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
630 * Update the default buffer objects in the given context to reference those
631 * specified in the shared state and release those referencing the old
635 _mesa_update_default_objects_buffer_objects(GLcontext
*ctx
)
637 /* Bind the NullBufferObj to remove references to those
638 * in the shared context hash table.
640 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
641 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
642 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
643 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
648 * When we're about to read pixel data out of a PBO (via glDrawPixels,
649 * glTexImage, etc) or write data into a PBO (via glReadPixels,
650 * glGetTexImage, etc) we call this function to check that we're not
651 * going to read out of bounds.
653 * XXX This would also be a convenient time to check that the PBO isn't
654 * currently mapped. Whoever calls this function should check for that.
655 * Remember, we can't use a PBO when it's mapped!
657 * If we're not using a PBO, this is a no-op.
659 * \param width width of image to read/write
660 * \param height height of image to read/write
661 * \param depth depth of image to read/write
662 * \param format format of image to read/write
663 * \param type datatype of image to read/write
664 * \param ptr the user-provided pointer/offset
665 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
669 _mesa_validate_pbo_access(GLuint dimensions
,
670 const struct gl_pixelstore_attrib
*pack
,
671 GLsizei width
, GLsizei height
, GLsizei depth
,
672 GLenum format
, GLenum type
, const GLvoid
*ptr
)
675 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
677 if (!_mesa_is_bufferobj(pack
->BufferObj
))
678 return GL_TRUE
; /* no PBO, OK */
680 if (pack
->BufferObj
->Size
== 0)
684 /* get address of first pixel we'll read */
685 start
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
686 format
, type
, 0, 0, 0);
688 /* get address just past the last pixel we'll read */
689 end
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
690 format
, type
, depth
-1, height
-1, width
);
693 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
695 if ((const GLubyte
*) start
> sizeAddr
) {
696 /* This will catch negative values / wrap-around */
699 if ((const GLubyte
*) end
> sizeAddr
) {
700 /* Image read goes beyond end of buffer */
710 * For commands that read from a PBO (glDrawPixels, glTexImage,
711 * glPolygonStipple, etc), if we're reading from a PBO, map it read-only
712 * and return the pointer into the PBO. If we're not reading from a
713 * PBO, return \p src as-is.
714 * If non-null return, must call _mesa_unmap_pbo_source() when done.
716 * \return NULL if error, else pointer to start of data
719 _mesa_map_pbo_source(GLcontext
*ctx
,
720 const struct gl_pixelstore_attrib
*unpack
,
725 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
726 /* unpack from PBO */
727 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
733 buf
= ADD_POINTERS(buf
, src
);
736 /* unpack from normal memory */
745 * Combine PBO-read validation and mapping.
746 * If any GL errors are detected, they'll be recorded and NULL returned.
747 * \sa _mesa_validate_pbo_access
748 * \sa _mesa_map_pbo_source
749 * A call to this function should have a matching call to
750 * _mesa_unmap_pbo_source().
753 _mesa_map_validate_pbo_source(GLcontext
*ctx
,
755 const struct gl_pixelstore_attrib
*unpack
,
756 GLsizei width
, GLsizei height
, GLsizei depth
,
757 GLenum format
, GLenum type
, const GLvoid
*ptr
,
760 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
762 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
763 /* non-PBO access: no validation to be done */
767 if (!_mesa_validate_pbo_access(dimensions
, unpack
,
768 width
, height
, depth
, format
, type
, ptr
)) {
769 _mesa_error(ctx
, GL_INVALID_OPERATION
,
770 "%s(out of bounds PBO access)", where
);
774 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
775 /* buffer is already mapped - that's an error */
776 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
780 ptr
= _mesa_map_pbo_source(ctx
, unpack
, ptr
);
786 * Counterpart to _mesa_map_pbo_source()
789 _mesa_unmap_pbo_source(GLcontext
*ctx
,
790 const struct gl_pixelstore_attrib
*unpack
)
792 ASSERT(unpack
!= &ctx
->Pack
); /* catch pack/unpack mismatch */
793 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
794 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
801 * For commands that write to a PBO (glReadPixels, glGetColorTable, etc),
802 * if we're writing to a PBO, map it write-only and return the pointer
803 * into the PBO. If we're not writing to a PBO, return \p dst as-is.
804 * If non-null return, must call _mesa_unmap_pbo_dest() when done.
806 * \return NULL if error, else pointer to start of data
809 _mesa_map_pbo_dest(GLcontext
*ctx
,
810 const struct gl_pixelstore_attrib
*pack
,
815 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
817 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
823 buf
= ADD_POINTERS(buf
, dest
);
826 /* pack to normal memory */
835 * Combine PBO-write validation and mapping.
836 * If any GL errors are detected, they'll be recorded and NULL returned.
837 * \sa _mesa_validate_pbo_access
838 * \sa _mesa_map_pbo_dest
839 * A call to this function should have a matching call to
840 * _mesa_unmap_pbo_dest().
843 _mesa_map_validate_pbo_dest(GLcontext
*ctx
,
845 const struct gl_pixelstore_attrib
*unpack
,
846 GLsizei width
, GLsizei height
, GLsizei depth
,
847 GLenum format
, GLenum type
, GLvoid
*ptr
,
850 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
852 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
853 /* non-PBO access: no validation to be done */
857 if (!_mesa_validate_pbo_access(dimensions
, unpack
,
858 width
, height
, depth
, format
, type
, ptr
)) {
859 _mesa_error(ctx
, GL_INVALID_OPERATION
,
860 "%s(out of bounds PBO access)", where
);
864 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
865 /* buffer is already mapped - that's an error */
866 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
870 ptr
= _mesa_map_pbo_dest(ctx
, unpack
, ptr
);
876 * Counterpart to _mesa_map_pbo_dest()
879 _mesa_unmap_pbo_dest(GLcontext
*ctx
,
880 const struct gl_pixelstore_attrib
*pack
)
882 ASSERT(pack
!= &ctx
->Unpack
); /* catch pack/unpack mismatch */
883 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
884 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
, pack
->BufferObj
);
891 * Return the gl_buffer_object for the given ID.
892 * Always return NULL for ID 0.
894 struct gl_buffer_object
*
895 _mesa_lookup_bufferobj(GLcontext
*ctx
, GLuint buffer
)
900 return (struct gl_buffer_object
*)
901 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
906 * If *ptr points to obj, set ptr = the Null/default buffer object.
907 * This is a helper for buffer object deletion.
908 * The GL spec says that deleting a buffer object causes it to get
909 * unbound from all arrays in the current context.
912 unbind(GLcontext
*ctx
,
913 struct gl_buffer_object
**ptr
,
914 struct gl_buffer_object
*obj
)
917 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
923 * Plug default/fallback buffer object functions into the device
927 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
929 /* GL_ARB_vertex/pixel_buffer_object */
930 driver
->NewBufferObject
= _mesa_new_buffer_object
;
931 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
932 driver
->BindBuffer
= NULL
;
933 driver
->BufferData
= _mesa_buffer_data
;
934 driver
->BufferSubData
= _mesa_buffer_subdata
;
935 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
936 driver
->MapBuffer
= _mesa_buffer_map
;
937 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
939 /* GL_ARB_map_buffer_range */
940 driver
->MapBufferRange
= _mesa_buffer_map_range
;
941 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
943 /* GL_ARB_copy_buffer */
944 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
949 /**********************************************************************/
951 /**********************************************************************/
954 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
956 GET_CURRENT_CONTEXT(ctx
);
957 ASSERT_OUTSIDE_BEGIN_END(ctx
);
959 bind_buffer_object(ctx
, target
, buffer
);
964 * Delete a set of buffer objects.
966 * \param n Number of buffer objects to delete.
967 * \param ids Array of \c n buffer object IDs.
970 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
972 GET_CURRENT_CONTEXT(ctx
);
974 ASSERT_OUTSIDE_BEGIN_END(ctx
);
977 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
981 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
983 for (i
= 0; i
< n
; i
++) {
984 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
986 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
989 ASSERT(bufObj
->Name
== ids
[i
]);
991 if (_mesa_bufferobj_mapped(bufObj
)) {
992 /* if mapped, unmap it now */
993 ctx
->Driver
.UnmapBuffer(ctx
, 0, bufObj
);
994 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
995 bufObj
->Pointer
= NULL
;
998 /* unbind any vertex pointers bound to this buffer */
999 unbind(ctx
, &arrayObj
->Vertex
.BufferObj
, bufObj
);
1000 unbind(ctx
, &arrayObj
->Weight
.BufferObj
, bufObj
);
1001 unbind(ctx
, &arrayObj
->Normal
.BufferObj
, bufObj
);
1002 unbind(ctx
, &arrayObj
->Color
.BufferObj
, bufObj
);
1003 unbind(ctx
, &arrayObj
->SecondaryColor
.BufferObj
, bufObj
);
1004 unbind(ctx
, &arrayObj
->FogCoord
.BufferObj
, bufObj
);
1005 unbind(ctx
, &arrayObj
->Index
.BufferObj
, bufObj
);
1006 unbind(ctx
, &arrayObj
->EdgeFlag
.BufferObj
, bufObj
);
1007 for (j
= 0; j
< Elements(arrayObj
->TexCoord
); j
++) {
1008 unbind(ctx
, &arrayObj
->TexCoord
[j
].BufferObj
, bufObj
);
1010 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
1011 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
1014 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1015 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
1017 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
1018 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1021 /* unbind any pixel pack/unpack pointers bound to this buffer */
1022 if (ctx
->Pack
.BufferObj
== bufObj
) {
1023 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1025 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1026 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1029 /* The ID is immediately freed for re-use */
1030 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, bufObj
->Name
);
1031 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1035 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1040 * Generate a set of unique buffer object IDs and store them in \c buffer.
1042 * \param n Number of IDs to generate.
1043 * \param buffer Array of \c n locations to store the IDs.
1046 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
1048 GET_CURRENT_CONTEXT(ctx
);
1051 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1054 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
1063 * This must be atomic (generation and allocation of buffer object IDs)
1065 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1067 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1069 /* Allocate new, empty buffer objects and return identifiers */
1070 for (i
= 0; i
< n
; i
++) {
1071 struct gl_buffer_object
*bufObj
;
1072 GLuint name
= first
+ i
;
1074 bufObj
= ctx
->Driver
.NewBufferObject( ctx
, name
, target
);
1076 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1077 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenBuffersARB");
1080 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
, bufObj
);
1081 buffer
[i
] = first
+ i
;
1084 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1089 * Determine if ID is the name of a buffer object.
1091 * \param id ID of the potential buffer object.
1092 * \return \c GL_TRUE if \c id is the name of a buffer object,
1093 * \c GL_FALSE otherwise.
1095 GLboolean GLAPIENTRY
1096 _mesa_IsBufferARB(GLuint id
)
1098 struct gl_buffer_object
*bufObj
;
1099 GET_CURRENT_CONTEXT(ctx
);
1100 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1102 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1103 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1104 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1106 return bufObj
? GL_TRUE
: GL_FALSE
;
1111 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
1112 const GLvoid
* data
, GLenum usage
)
1114 GET_CURRENT_CONTEXT(ctx
);
1115 struct gl_buffer_object
*bufObj
;
1116 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1119 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1124 case GL_STREAM_DRAW_ARB
:
1125 case GL_STREAM_READ_ARB
:
1126 case GL_STREAM_COPY_ARB
:
1127 case GL_STATIC_DRAW_ARB
:
1128 case GL_STATIC_READ_ARB
:
1129 case GL_STATIC_COPY_ARB
:
1130 case GL_DYNAMIC_DRAW_ARB
:
1131 case GL_DYNAMIC_READ_ARB
:
1132 case GL_DYNAMIC_COPY_ARB
:
1136 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
1140 bufObj
= get_buffer(ctx
, target
);
1142 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
1145 if (!_mesa_is_bufferobj(bufObj
)) {
1146 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer 0)" );
1150 if (_mesa_bufferobj_mapped(bufObj
)) {
1151 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1152 ctx
->Driver
.UnmapBuffer(ctx
, target
, bufObj
);
1153 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1154 ASSERT(bufObj
->Pointer
== NULL
);
1157 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1159 bufObj
->Written
= GL_TRUE
;
1162 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1163 bufObj
->Name
, size
, data
, usage
);
1170 ASSERT(ctx
->Driver
.BufferData
);
1171 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1172 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1178 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1179 GLsizeiptrARB size
, const GLvoid
* data
)
1181 GET_CURRENT_CONTEXT(ctx
);
1182 struct gl_buffer_object
*bufObj
;
1183 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1185 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1186 "glBufferSubDataARB" );
1188 /* error already recorded */
1192 bufObj
->Written
= GL_TRUE
;
1194 ASSERT(ctx
->Driver
.BufferSubData
);
1195 ctx
->Driver
.BufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1200 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1201 GLsizeiptrARB size
, void * data
)
1203 GET_CURRENT_CONTEXT(ctx
);
1204 struct gl_buffer_object
*bufObj
;
1205 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1207 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1208 "glGetBufferSubDataARB" );
1210 /* error already recorded */
1214 ASSERT(ctx
->Driver
.GetBufferSubData
);
1215 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1220 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1222 GET_CURRENT_CONTEXT(ctx
);
1223 struct gl_buffer_object
* bufObj
;
1224 GLbitfield accessFlags
;
1227 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1230 case GL_READ_ONLY_ARB
:
1231 accessFlags
= GL_MAP_READ_BIT
;
1233 case GL_WRITE_ONLY_ARB
:
1234 accessFlags
= GL_MAP_WRITE_BIT
;
1236 case GL_READ_WRITE_ARB
:
1237 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1240 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1244 bufObj
= get_buffer(ctx
, target
);
1246 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
1249 if (!_mesa_is_bufferobj(bufObj
)) {
1250 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(buffer 0)" );
1253 if (_mesa_bufferobj_mapped(bufObj
)) {
1254 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1258 ASSERT(ctx
->Driver
.MapBuffer
);
1259 map
= ctx
->Driver
.MapBuffer( ctx
, target
, access
, bufObj
);
1261 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1265 /* The driver callback should have set these fields.
1266 * This is important because other modules (like VBO) might call
1267 * the driver function directly.
1269 ASSERT(bufObj
->Pointer
== map
);
1270 ASSERT(bufObj
->Length
== bufObj
->Size
);
1271 ASSERT(bufObj
->Offset
== 0);
1272 bufObj
->AccessFlags
= accessFlags
;
1275 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1276 bufObj
->Written
= GL_TRUE
;
1279 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1280 bufObj
->Name
, bufObj
->Size
, access
);
1281 if (access
== GL_WRITE_ONLY_ARB
) {
1283 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1284 for (i
= 0; i
< bufObj
->Size
; i
++)
1291 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1293 /* buffer is 100 bytes larger than requested, fill with magic value */
1294 for (i
= 0; i
< 100; i
++) {
1295 buf
[bufObj
->Size
- i
- 1] = 123;
1300 return bufObj
->Pointer
;
1304 GLboolean GLAPIENTRY
1305 _mesa_UnmapBufferARB(GLenum target
)
1307 GET_CURRENT_CONTEXT(ctx
);
1308 struct gl_buffer_object
*bufObj
;
1309 GLboolean status
= GL_TRUE
;
1310 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1312 bufObj
= get_buffer(ctx
, target
);
1314 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1317 if (!_mesa_is_bufferobj(bufObj
)) {
1318 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1321 if (!_mesa_bufferobj_mapped(bufObj
)) {
1322 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1327 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1328 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1330 /* check that last 100 bytes are still = magic value */
1331 for (i
= 0; i
< 100; i
++) {
1332 GLuint pos
= bufObj
->Size
- i
- 1;
1333 if (buf
[pos
] != 123) {
1334 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1335 " at position %d (value = %u)\n",
1343 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1344 GLuint i
, unchanged
= 0;
1345 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1347 /* check which bytes changed */
1348 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1349 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1356 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1357 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1362 status
= ctx
->Driver
.UnmapBuffer( ctx
, target
, bufObj
);
1363 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1364 ASSERT(bufObj
->Pointer
== NULL
);
1365 ASSERT(bufObj
->Offset
== 0);
1366 ASSERT(bufObj
->Length
== 0);
1373 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1375 GET_CURRENT_CONTEXT(ctx
);
1376 struct gl_buffer_object
*bufObj
;
1377 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1379 bufObj
= get_buffer(ctx
, target
);
1381 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(target)" );
1384 if (!_mesa_is_bufferobj(bufObj
)) {
1385 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferParameterivARB" );
1390 case GL_BUFFER_SIZE_ARB
:
1391 *params
= (GLint
) bufObj
->Size
;
1393 case GL_BUFFER_USAGE_ARB
:
1394 *params
= bufObj
->Usage
;
1396 case GL_BUFFER_ACCESS_ARB
:
1397 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1399 case GL_BUFFER_MAPPED_ARB
:
1400 *params
= _mesa_bufferobj_mapped(bufObj
);
1402 case GL_BUFFER_ACCESS_FLAGS
:
1403 if (ctx
->VersionMajor
< 3)
1405 *params
= bufObj
->AccessFlags
;
1407 case GL_BUFFER_MAP_OFFSET
:
1408 if (ctx
->VersionMajor
< 3)
1410 *params
= (GLint
) bufObj
->Offset
;
1412 case GL_BUFFER_MAP_LENGTH
:
1413 if (ctx
->VersionMajor
< 3)
1415 *params
= (GLint
) bufObj
->Length
;
1418 ; /* fall-through */
1422 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1423 _mesa_lookup_enum_by_nr(pname
));
1429 * This is pretty much a duplicate of GetBufferParameteriv() but the
1430 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1433 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1435 GET_CURRENT_CONTEXT(ctx
);
1436 struct gl_buffer_object
*bufObj
;
1437 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1439 bufObj
= get_buffer(ctx
, target
);
1441 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(target)" );
1444 if (!_mesa_is_bufferobj(bufObj
)) {
1445 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferParameteri64v" );
1450 case GL_BUFFER_SIZE_ARB
:
1451 *params
= bufObj
->Size
;
1453 case GL_BUFFER_USAGE_ARB
:
1454 *params
= bufObj
->Usage
;
1456 case GL_BUFFER_ACCESS_ARB
:
1457 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1459 case GL_BUFFER_ACCESS_FLAGS
:
1460 if (ctx
->VersionMajor
< 3)
1462 *params
= bufObj
->AccessFlags
;
1464 case GL_BUFFER_MAPPED_ARB
:
1465 *params
= _mesa_bufferobj_mapped(bufObj
);
1467 case GL_BUFFER_MAP_OFFSET
:
1468 if (ctx
->VersionMajor
< 3)
1470 *params
= bufObj
->Offset
;
1472 case GL_BUFFER_MAP_LENGTH
:
1473 if (ctx
->VersionMajor
< 3)
1475 *params
= bufObj
->Length
;
1478 ; /* fall-through */
1482 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1483 _mesa_lookup_enum_by_nr(pname
));
1488 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1490 GET_CURRENT_CONTEXT(ctx
);
1491 struct gl_buffer_object
* bufObj
;
1492 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1494 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1495 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1499 bufObj
= get_buffer(ctx
, target
);
1501 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1504 if (!_mesa_is_bufferobj(bufObj
)) {
1505 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1509 *params
= bufObj
->Pointer
;
1514 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1515 GLintptr readOffset
, GLintptr writeOffset
,
1518 GET_CURRENT_CONTEXT(ctx
);
1519 struct gl_buffer_object
*src
, *dst
;
1520 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1522 src
= get_buffer(ctx
, readTarget
);
1523 if (!src
|| !_mesa_is_bufferobj(src
)) {
1524 _mesa_error(ctx
, GL_INVALID_ENUM
,
1525 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget
);
1529 dst
= get_buffer(ctx
, writeTarget
);
1530 if (!dst
|| !_mesa_is_bufferobj(dst
)) {
1531 _mesa_error(ctx
, GL_INVALID_ENUM
,
1532 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget
);
1536 if (_mesa_bufferobj_mapped(src
)) {
1537 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1538 "glCopyBuffserSubData(readBuffer is mapped)");
1542 if (_mesa_bufferobj_mapped(dst
)) {
1543 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1544 "glCopyBuffserSubData(writeBuffer is mapped)");
1548 if (readOffset
< 0) {
1549 _mesa_error(ctx
, GL_INVALID_VALUE
,
1550 "glCopyBuffserSubData(readOffset = %d)", (int) readOffset
);
1554 if (writeOffset
< 0) {
1555 _mesa_error(ctx
, GL_INVALID_VALUE
,
1556 "glCopyBuffserSubData(writeOffset = %d)", (int) writeOffset
);
1560 if (readOffset
+ size
> src
->Size
) {
1561 _mesa_error(ctx
, GL_INVALID_VALUE
,
1562 "glCopyBuffserSubData(readOffset + size = %d)",
1563 (int) (readOffset
+ size
));
1567 if (writeOffset
+ size
> dst
->Size
) {
1568 _mesa_error(ctx
, GL_INVALID_VALUE
,
1569 "glCopyBuffserSubData(writeOffset + size = %d)",
1570 (int) (writeOffset
+ size
));
1575 if (readOffset
+ size
<= writeOffset
) {
1578 else if (writeOffset
+ size
<= readOffset
) {
1582 /* overlapping src/dst is illegal */
1583 _mesa_error(ctx
, GL_INVALID_VALUE
,
1584 "glCopyBuffserSubData(overlapping src/dst)");
1589 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1594 * See GL_ARB_map_buffer_range spec
1597 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1600 GET_CURRENT_CONTEXT(ctx
);
1601 struct gl_buffer_object
*bufObj
;
1604 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1606 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1607 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1608 "glMapBufferRange(extension not supported)");
1613 _mesa_error(ctx
, GL_INVALID_VALUE
,
1614 "glMapBufferRange(offset = %ld)", (long)offset
);
1619 _mesa_error(ctx
, GL_INVALID_VALUE
,
1620 "glMapBufferRange(length = %ld)", (long)length
);
1624 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1625 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1626 "glMapBufferRange(access indicates neither read or write)");
1630 if (access
& GL_MAP_READ_BIT
) {
1631 if ((access
& GL_MAP_INVALIDATE_RANGE_BIT
) ||
1632 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) ||
1633 (access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
1634 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1635 "glMapBufferRange(invalid access flags)");
1640 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1641 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1642 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1643 "glMapBufferRange(invalid access flags)");
1647 bufObj
= get_buffer(ctx
, target
);
1648 if (!bufObj
|| !_mesa_is_bufferobj(bufObj
)) {
1649 _mesa_error(ctx
, GL_INVALID_ENUM
,
1650 "glMapBufferRange(target = 0x%x)", target
);
1654 if (offset
+ length
> bufObj
->Size
) {
1655 _mesa_error(ctx
, GL_INVALID_VALUE
,
1656 "glMapBufferRange(offset + length > size)");
1660 if (_mesa_bufferobj_mapped(bufObj
)) {
1661 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1662 "glMapBufferRange(buffer already mapped)");
1666 ASSERT(ctx
->Driver
.MapBufferRange
);
1667 map
= ctx
->Driver
.MapBufferRange(ctx
, target
, offset
, length
,
1670 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1673 /* The driver callback should have set all these fields.
1674 * This is important because other modules (like VBO) might call
1675 * the driver function directly.
1677 ASSERT(bufObj
->Pointer
== map
);
1678 ASSERT(bufObj
->Length
== length
);
1679 ASSERT(bufObj
->Offset
== offset
);
1680 ASSERT(bufObj
->AccessFlags
== access
);
1688 * See GL_ARB_map_buffer_range spec
1691 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1693 GET_CURRENT_CONTEXT(ctx
);
1694 struct gl_buffer_object
*bufObj
;
1695 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1697 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1698 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1699 "glMapBufferRange(extension not supported)");
1704 _mesa_error(ctx
, GL_INVALID_VALUE
,
1705 "glMapBufferRange(offset = %ld)", (long)offset
);
1710 _mesa_error(ctx
, GL_INVALID_VALUE
,
1711 "glMapBufferRange(length = %ld)", (long)length
);
1715 bufObj
= get_buffer(ctx
, target
);
1717 _mesa_error(ctx
, GL_INVALID_ENUM
,
1718 "glMapBufferRange(target = 0x%x)", target
);
1722 if (!_mesa_is_bufferobj(bufObj
)) {
1723 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1724 "glMapBufferRange(current buffer is 0)");
1728 if (!_mesa_bufferobj_mapped(bufObj
)) {
1729 /* buffer is not mapped */
1730 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1731 "glMapBufferRange(buffer is not mapped)");
1735 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1736 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1737 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1741 if (offset
+ length
> bufObj
->Length
) {
1742 _mesa_error(ctx
, GL_INVALID_VALUE
,
1743 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1744 (long)offset
, (long)length
, (long)bufObj
->Length
);
1748 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1750 if (ctx
->Driver
.FlushMappedBufferRange
)
1751 ctx
->Driver
.FlushMappedBufferRange(ctx
, target
, offset
, length
, bufObj
);
1755 #if FEATURE_APPLE_object_purgeable
1757 _mesa_BufferObjectPurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1759 struct gl_buffer_object
*bufObj
;
1762 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1764 _mesa_error(ctx
, GL_INVALID_VALUE
,
1765 "glObjectPurgeable(name = 0x%x)", name
);
1768 if (!_mesa_is_bufferobj(bufObj
)) {
1769 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1773 if (bufObj
->Purgeable
) {
1774 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1775 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1776 return GL_VOLATILE_APPLE
;
1779 bufObj
->Purgeable
= GL_TRUE
;
1781 retval
= GL_VOLATILE_APPLE
;
1782 if (ctx
->Driver
.BufferObjectPurgeable
)
1783 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1790 _mesa_RenderObjectPurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1792 struct gl_renderbuffer
*bufObj
;
1795 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1797 _mesa_error(ctx
, GL_INVALID_VALUE
,
1798 "glObjectUnpurgeable(name = 0x%x)", name
);
1802 if (bufObj
->Purgeable
) {
1803 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1804 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1805 return GL_VOLATILE_APPLE
;
1808 bufObj
->Purgeable
= GL_TRUE
;
1810 retval
= GL_VOLATILE_APPLE
;
1811 if (ctx
->Driver
.RenderObjectPurgeable
)
1812 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1819 _mesa_TextureObjectPurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1821 struct gl_texture_object
*bufObj
;
1824 bufObj
= _mesa_lookup_texture(ctx
, name
);
1826 _mesa_error(ctx
, GL_INVALID_VALUE
,
1827 "glObjectPurgeable(name = 0x%x)", name
);
1831 if (bufObj
->Purgeable
) {
1832 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1833 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1834 return GL_VOLATILE_APPLE
;
1837 bufObj
->Purgeable
= GL_TRUE
;
1839 retval
= GL_VOLATILE_APPLE
;
1840 if (ctx
->Driver
.TextureObjectPurgeable
)
1841 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1848 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1852 GET_CURRENT_CONTEXT(ctx
);
1853 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1856 _mesa_error(ctx
, GL_INVALID_VALUE
,
1857 "glObjectPurgeable(name = 0x%x)", name
);
1862 case GL_VOLATILE_APPLE
:
1863 case GL_RELEASED_APPLE
:
1867 _mesa_error(ctx
, GL_INVALID_ENUM
,
1868 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1873 switch (objectType
) {
1875 retval
= _mesa_TextureObjectPurgeable (ctx
, name
, option
);
1877 case GL_RENDERBUFFER_EXT
:
1878 retval
= _mesa_RenderObjectPurgeable (ctx
, name
, option
);
1880 case GL_BUFFER_OBJECT_APPLE
:
1881 retval
= _mesa_BufferObjectPurgeable (ctx
, name
, option
);
1884 _mesa_error(ctx
, GL_INVALID_ENUM
,
1885 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1890 /* In strict conformance to the spec, we must only return VOLATILE when
1891 * when passed the VOLATILE option. Madness.
1893 * XXX First fix the spec, then fix me.
1895 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1900 _mesa_BufferObjectUnpurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1902 struct gl_buffer_object
*bufObj
;
1905 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1907 _mesa_error(ctx
, GL_INVALID_VALUE
,
1908 "glObjectUnpurgeable(name = 0x%x)", name
);
1912 if (! bufObj
->Purgeable
) {
1913 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1914 "glObjectUnpurgeable(name = 0x%x) object is "
1915 " already \"unpurged\"", name
);
1919 bufObj
->Purgeable
= GL_FALSE
;
1922 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1923 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1930 _mesa_RenderObjectUnpurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1932 struct gl_renderbuffer
*bufObj
;
1935 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1937 _mesa_error(ctx
, GL_INVALID_VALUE
,
1938 "glObjectUnpurgeable(name = 0x%x)", name
);
1942 if (! bufObj
->Purgeable
) {
1943 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1944 "glObjectUnpurgeable(name = 0x%x) object is "
1945 " already \"unpurged\"", name
);
1949 bufObj
->Purgeable
= GL_FALSE
;
1952 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1953 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1960 _mesa_TextureObjectUnpurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1962 struct gl_texture_object
*bufObj
;
1965 bufObj
= _mesa_lookup_texture(ctx
, name
);
1967 _mesa_error(ctx
, GL_INVALID_VALUE
,
1968 "glObjectUnpurgeable(name = 0x%x)", name
);
1972 if (! bufObj
->Purgeable
) {
1973 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1974 "glObjectUnpurgeable(name = 0x%x) object is"
1975 " already \"unpurged\"", name
);
1979 bufObj
->Purgeable
= GL_FALSE
;
1982 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1983 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1990 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1992 GET_CURRENT_CONTEXT(ctx
);
1993 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1996 _mesa_error(ctx
, GL_INVALID_VALUE
,
1997 "glObjectUnpurgeable(name = 0x%x)", name
);
2002 case GL_RETAINED_APPLE
:
2003 case GL_UNDEFINED_APPLE
:
2007 _mesa_error(ctx
, GL_INVALID_ENUM
,
2008 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2013 switch (objectType
) {
2014 case GL_BUFFER_OBJECT_APPLE
:
2015 return _mesa_BufferObjectUnpurgeable(ctx
, name
, option
);
2017 return _mesa_TextureObjectUnpurgeable(ctx
, name
, option
);
2018 case GL_RENDERBUFFER_EXT
:
2019 return _mesa_RenderObjectUnpurgeable(ctx
, name
, option
);
2021 _mesa_error(ctx
, GL_INVALID_ENUM
,
2022 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2030 _mesa_GetBufferObjectParameterivAPPLE(GLcontext
*ctx
, GLuint name
,
2031 GLenum pname
, GLint
* params
)
2033 struct gl_buffer_object
*bufObj
;
2035 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2037 _mesa_error(ctx
, GL_INVALID_VALUE
,
2038 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
2043 case GL_PURGEABLE_APPLE
:
2044 *params
= bufObj
->Purgeable
;
2047 _mesa_error(ctx
, GL_INVALID_ENUM
,
2048 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2056 _mesa_GetRenderObjectParameterivAPPLE(GLcontext
*ctx
, GLuint name
,
2057 GLenum pname
, GLint
* params
)
2059 struct gl_renderbuffer
*bufObj
;
2061 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
2063 _mesa_error(ctx
, GL_INVALID_VALUE
,
2064 "glObjectUnpurgeable(name = 0x%x)", name
);
2069 case GL_PURGEABLE_APPLE
:
2070 *params
= bufObj
->Purgeable
;
2073 _mesa_error(ctx
, GL_INVALID_ENUM
,
2074 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2082 _mesa_GetTextureObjectParameterivAPPLE(GLcontext
*ctx
, GLuint name
,
2083 GLenum pname
, GLint
* params
)
2085 struct gl_texture_object
*bufObj
;
2087 bufObj
= _mesa_lookup_texture(ctx
, name
);
2089 _mesa_error(ctx
, GL_INVALID_VALUE
,
2090 "glObjectUnpurgeable(name = 0x%x)", name
);
2095 case GL_PURGEABLE_APPLE
:
2096 *params
= bufObj
->Purgeable
;
2099 _mesa_error(ctx
, GL_INVALID_ENUM
,
2100 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2108 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
2111 GET_CURRENT_CONTEXT(ctx
);
2114 _mesa_error(ctx
, GL_INVALID_VALUE
,
2115 "glGetObjectParameteriv(name = 0x%x)", name
);
2119 switch (objectType
) {
2121 _mesa_GetTextureObjectParameterivAPPLE (ctx
, name
, pname
, params
);
2123 case GL_BUFFER_OBJECT_APPLE
:
2124 _mesa_GetBufferObjectParameterivAPPLE (ctx
, name
, pname
, params
);
2126 case GL_RENDERBUFFER_EXT
:
2127 _mesa_GetRenderObjectParameterivAPPLE (ctx
, name
, pname
, params
);
2130 _mesa_error(ctx
, GL_INVALID_ENUM
,
2131 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2136 #endif /* FEATURE_APPLE_object_purgeable */