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 if (ctx
->Extensions
.ARB_copy_buffer
) {
78 return &ctx
->CopyReadBuffer
;
81 case GL_COPY_WRITE_BUFFER
:
82 if (ctx
->Extensions
.ARB_copy_buffer
) {
83 return &ctx
->CopyWriteBuffer
;
86 #if FEATURE_EXT_transform_feedback
87 case GL_TRANSFORM_FEEDBACK_BUFFER
:
88 if (ctx
->Extensions
.EXT_transform_feedback
) {
89 return &ctx
->TransformFeedback
.CurrentBuffer
;
101 * Get the buffer object bound to the specified target in a GL context.
102 * \param ctx the GL context
103 * \param target the buffer object target to be retrieved.
104 * \return pointer to the buffer object bound to \c target in the
105 * specified context or \c NULL if \c target is invalid.
107 static INLINE
struct gl_buffer_object
*
108 get_buffer(GLcontext
*ctx
, GLenum target
)
110 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
118 * Convert a GLbitfield describing the mapped buffer access flags
119 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
122 simplified_access_mode(GLbitfield access
)
124 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
125 if ((access
& rwFlags
) == rwFlags
)
126 return GL_READ_WRITE
;
127 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
129 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
130 return GL_WRITE_ONLY
;
131 return GL_READ_WRITE
; /* this should never happen, but no big deal */
136 * Tests the subdata range parameters and sets the GL error code for
137 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
139 * \param ctx GL context.
140 * \param target Buffer object target on which to operate.
141 * \param offset Offset of the first byte of the subdata range.
142 * \param size Size, in bytes, of the subdata range.
143 * \param caller Name of calling function for recording errors.
144 * \return A pointer to the buffer object bound to \c target in the
145 * specified context or \c NULL if any of the parameter or state
146 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
149 * \sa glBufferSubDataARB, glGetBufferSubDataARB
151 static struct gl_buffer_object
*
152 buffer_object_subdata_range_good( GLcontext
* ctx
, GLenum target
,
153 GLintptrARB offset
, GLsizeiptrARB size
,
156 struct gl_buffer_object
*bufObj
;
159 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
164 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
168 bufObj
= get_buffer(ctx
, target
);
170 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", caller
);
173 if (!_mesa_is_bufferobj(bufObj
)) {
174 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
177 if (offset
+ size
> bufObj
->Size
) {
178 _mesa_error(ctx
, GL_INVALID_VALUE
,
179 "%s(size + offset > buffer size)", caller
);
182 if (_mesa_bufferobj_mapped(bufObj
)) {
183 /* Buffer is currently mapped */
184 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
193 * Allocate and initialize a new buffer object.
195 * Default callback for the \c dd_function_table::NewBufferObject() hook.
197 static struct gl_buffer_object
*
198 _mesa_new_buffer_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
200 struct gl_buffer_object
*obj
;
204 obj
= MALLOC_STRUCT(gl_buffer_object
);
205 _mesa_initialize_buffer_object(obj
, name
, target
);
211 * Delete a buffer object.
213 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
216 _mesa_delete_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*bufObj
)
223 /* assign strange values here to help w/ debugging */
224 bufObj
->RefCount
= -1000;
227 _glthread_DESTROY_MUTEX(bufObj
->Mutex
);
234 * Set ptr to bufObj w/ reference counting.
237 _mesa_reference_buffer_object(GLcontext
*ctx
,
238 struct gl_buffer_object
**ptr
,
239 struct gl_buffer_object
*bufObj
)
245 /* Unreference the old buffer */
246 GLboolean deleteFlag
= GL_FALSE
;
247 struct gl_buffer_object
*oldObj
= *ptr
;
249 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
250 ASSERT(oldObj
->RefCount
> 0);
253 printf("BufferObj %p %d DECR to %d\n",
254 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
256 deleteFlag
= (oldObj
->RefCount
== 0);
257 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
261 /* some sanity checking: don't delete a buffer still in use */
263 /* unfortunately, these tests are invalid during context tear-down */
264 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
265 ASSERT(ctx
->Array
.ElementArrayBufferObj
!= bufObj
);
266 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
269 ASSERT(ctx
->Driver
.DeleteBuffer
);
270 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
278 /* reference new buffer */
279 _glthread_LOCK_MUTEX(bufObj
->Mutex
);
280 if (bufObj
->RefCount
== 0) {
281 /* this buffer's being deleted (look just above) */
282 /* Not sure this can every really happen. Warn if it does. */
283 _mesa_problem(NULL
, "referencing deleted buffer object");
289 printf("BufferObj %p %d INCR to %d\n",
290 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
294 _glthread_UNLOCK_MUTEX(bufObj
->Mutex
);
300 * Initialize a buffer object to default values.
303 _mesa_initialize_buffer_object( struct gl_buffer_object
*obj
,
304 GLuint name
, GLenum target
)
308 memset(obj
, 0, sizeof(struct gl_buffer_object
));
309 _glthread_INIT_MUTEX(obj
->Mutex
);
312 obj
->Usage
= GL_STATIC_DRAW_ARB
;
313 obj
->AccessFlags
= DEFAULT_ACCESS
;
318 * Allocate space for and store data in a buffer object. Any data that was
319 * previously stored in the buffer object is lost. If \c data is \c NULL,
320 * memory will be allocated, but no copy will occur.
322 * This is the default callback for \c dd_function_table::BufferData()
323 * Note that all GL error checking will have been done already.
325 * \param ctx GL context.
326 * \param target Buffer object target on which to operate.
327 * \param size Size, in bytes, of the new data store.
328 * \param data Pointer to the data to store in the buffer object. This
329 * pointer may be \c NULL.
330 * \param usage Hints about how the data will be used.
331 * \param bufObj Object to be used.
333 * \return GL_TRUE for success, GL_FALSE for failure
334 * \sa glBufferDataARB, dd_function_table::BufferData.
337 _mesa_buffer_data( GLcontext
*ctx
, GLenum target
, GLsizeiptrARB size
,
338 const GLvoid
* data
, GLenum usage
,
339 struct gl_buffer_object
* bufObj
)
343 (void) ctx
; (void) target
;
345 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
347 bufObj
->Data
= (GLubyte
*) new_data
;
349 bufObj
->Usage
= usage
;
352 memcpy( bufObj
->Data
, data
, size
);
364 * Replace data in a subrange of buffer object. If the data range
365 * specified by \c size + \c offset extends beyond the end of the buffer or
366 * if \c data is \c NULL, no copy is performed.
368 * This is the default callback for \c dd_function_table::BufferSubData()
369 * Note that all GL error checking will have been done already.
371 * \param ctx GL context.
372 * \param target Buffer object target on which to operate.
373 * \param offset Offset of the first byte to be modified.
374 * \param size Size, in bytes, of the data range.
375 * \param data Pointer to the data to store in the buffer object.
376 * \param bufObj Object to be used.
378 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
381 _mesa_buffer_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
382 GLsizeiptrARB size
, const GLvoid
* data
,
383 struct gl_buffer_object
* bufObj
)
385 (void) ctx
; (void) target
;
387 /* this should have been caught in _mesa_BufferSubData() */
388 ASSERT(size
+ offset
<= bufObj
->Size
);
391 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
397 * Retrieve data from a subrange of buffer object. If the data range
398 * specified by \c size + \c offset extends beyond the end of the buffer or
399 * if \c data is \c NULL, no copy is performed.
401 * This is the default callback for \c dd_function_table::GetBufferSubData()
402 * Note that all GL error checking will have been done already.
404 * \param ctx GL context.
405 * \param target Buffer object target on which to operate.
406 * \param offset Offset of the first byte to be fetched.
407 * \param size Size, in bytes, of the data range.
408 * \param data Destination for data
409 * \param bufObj Object to be used.
411 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
414 _mesa_buffer_get_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
415 GLsizeiptrARB size
, GLvoid
* data
,
416 struct gl_buffer_object
* bufObj
)
418 (void) ctx
; (void) target
;
420 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
421 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
427 * Default callback for \c dd_function_tabel::MapBuffer().
429 * The function parameters will have been already tested for errors.
431 * \param ctx GL context.
432 * \param target Buffer object target on which to operate.
433 * \param access Information about how the buffer will be accessed.
434 * \param bufObj Object to be mapped.
435 * \return A pointer to the object's internal data store that can be accessed
438 * \sa glMapBufferARB, dd_function_table::MapBuffer
441 _mesa_buffer_map( GLcontext
*ctx
, GLenum target
, GLenum access
,
442 struct gl_buffer_object
*bufObj
)
447 /* Just return a direct pointer to the data */
448 if (_mesa_bufferobj_mapped(bufObj
)) {
449 /* already mapped! */
452 bufObj
->Pointer
= bufObj
->Data
;
453 bufObj
->Length
= bufObj
->Size
;
455 return bufObj
->Pointer
;
460 * Default fallback for \c dd_function_table::MapBufferRange().
461 * Called via glMapBufferRange().
464 _mesa_buffer_map_range( GLcontext
*ctx
, GLenum target
, GLintptr offset
,
465 GLsizeiptr length
, GLbitfield access
,
466 struct gl_buffer_object
*bufObj
)
470 assert(!_mesa_bufferobj_mapped(bufObj
));
471 /* Just return a direct pointer to the data */
472 bufObj
->Pointer
= bufObj
->Data
+ offset
;
473 bufObj
->Length
= length
;
474 bufObj
->Offset
= offset
;
475 bufObj
->AccessFlags
= access
;
476 return bufObj
->Pointer
;
481 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
482 * Called via glFlushMappedBufferRange().
485 _mesa_buffer_flush_mapped_range( GLcontext
*ctx
, GLenum target
,
486 GLintptr offset
, GLsizeiptr length
,
487 struct gl_buffer_object
*obj
)
499 * Default callback for \c dd_function_table::MapBuffer().
501 * The input parameters will have been already tested for errors.
503 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
506 _mesa_buffer_unmap( GLcontext
*ctx
, GLenum target
,
507 struct gl_buffer_object
*bufObj
)
511 /* XXX we might assert here that bufObj->Pointer is non-null */
512 bufObj
->Pointer
= NULL
;
515 bufObj
->AccessFlags
= 0x0;
521 * Default fallback for \c dd_function_table::CopyBufferSubData().
522 * Called via glCopyBuffserSubData().
525 _mesa_copy_buffer_subdata(GLcontext
*ctx
,
526 struct gl_buffer_object
*src
,
527 struct gl_buffer_object
*dst
,
528 GLintptr readOffset
, GLintptr writeOffset
,
531 GLubyte
*srcPtr
, *dstPtr
;
533 /* buffer should not already be mapped */
534 assert(!_mesa_bufferobj_mapped(src
));
535 assert(!_mesa_bufferobj_mapped(dst
));
537 srcPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_READ_BUFFER
,
539 dstPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_WRITE_BUFFER
,
542 if (srcPtr
&& dstPtr
)
543 memcpy(dstPtr
+ writeOffset
, srcPtr
+ readOffset
, size
);
545 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_READ_BUFFER
, src
);
546 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_WRITE_BUFFER
, dst
);
552 * Initialize the state associated with buffer objects
555 _mesa_init_buffer_objects( GLcontext
*ctx
)
557 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
558 ctx
->Shared
->NullBufferObj
);
559 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
,
560 ctx
->Shared
->NullBufferObj
);
562 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
563 ctx
->Shared
->NullBufferObj
);
564 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
565 ctx
->Shared
->NullBufferObj
);
570 _mesa_free_buffer_objects( GLcontext
*ctx
)
572 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
573 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
, NULL
);
575 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
576 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
581 * Bind the specified target to buffer for the specified context.
582 * Called by glBindBuffer() and other functions.
585 bind_buffer_object(GLcontext
*ctx
, GLenum target
, GLuint buffer
)
587 struct gl_buffer_object
*oldBufObj
;
588 struct gl_buffer_object
*newBufObj
= NULL
;
589 struct gl_buffer_object
**bindTarget
= NULL
;
591 bindTarget
= get_buffer_target(ctx
, target
);
593 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)");
597 /* Get pointer to old buffer object (to be unbound) */
598 oldBufObj
= *bindTarget
;
599 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
600 return; /* rebinding the same buffer object- no change */
603 * Get pointer to new buffer object (newBufObj)
606 /* The spec says there's not a buffer object named 0, but we use
607 * one internally because it simplifies things.
609 newBufObj
= ctx
->Shared
->NullBufferObj
;
612 /* non-default buffer object */
613 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
615 /* if this is a new buffer object id, allocate a buffer object now */
616 ASSERT(ctx
->Driver
.NewBufferObject
);
617 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
619 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
622 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
626 /* bind new buffer */
627 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
629 /* Pass BindBuffer call to device driver */
630 if (ctx
->Driver
.BindBuffer
)
631 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
636 * Update the default buffer objects in the given context to reference those
637 * specified in the shared state and release those referencing the old
641 _mesa_update_default_objects_buffer_objects(GLcontext
*ctx
)
643 /* Bind the NullBufferObj to remove references to those
644 * in the shared context hash table.
646 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
647 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
648 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
649 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
654 * When we're about to read pixel data out of a PBO (via glDrawPixels,
655 * glTexImage, etc) or write data into a PBO (via glReadPixels,
656 * glGetTexImage, etc) we call this function to check that we're not
657 * going to read out of bounds.
659 * XXX This would also be a convenient time to check that the PBO isn't
660 * currently mapped. Whoever calls this function should check for that.
661 * Remember, we can't use a PBO when it's mapped!
663 * If we're not using a PBO, this is a no-op.
665 * \param width width of image to read/write
666 * \param height height of image to read/write
667 * \param depth depth of image to read/write
668 * \param format format of image to read/write
669 * \param type datatype of image to read/write
670 * \param ptr the user-provided pointer/offset
671 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
675 _mesa_validate_pbo_access(GLuint dimensions
,
676 const struct gl_pixelstore_attrib
*pack
,
677 GLsizei width
, GLsizei height
, GLsizei depth
,
678 GLenum format
, GLenum type
, const GLvoid
*ptr
)
681 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
683 if (!_mesa_is_bufferobj(pack
->BufferObj
))
684 return GL_TRUE
; /* no PBO, OK */
686 if (pack
->BufferObj
->Size
== 0)
690 /* get address of first pixel we'll read */
691 start
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
692 format
, type
, 0, 0, 0);
694 /* get address just past the last pixel we'll read */
695 end
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
696 format
, type
, depth
-1, height
-1, width
);
699 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
701 if ((const GLubyte
*) start
> sizeAddr
) {
702 /* This will catch negative values / wrap-around */
705 if ((const GLubyte
*) end
> sizeAddr
) {
706 /* Image read goes beyond end of buffer */
716 * For commands that read from a PBO (glDrawPixels, glTexImage,
717 * glPolygonStipple, etc), if we're reading from a PBO, map it read-only
718 * and return the pointer into the PBO. If we're not reading from a
719 * PBO, return \p src as-is.
720 * If non-null return, must call _mesa_unmap_pbo_source() when done.
722 * \return NULL if error, else pointer to start of data
725 _mesa_map_pbo_source(GLcontext
*ctx
,
726 const struct gl_pixelstore_attrib
*unpack
,
731 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
732 /* unpack from PBO */
733 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
739 buf
= ADD_POINTERS(buf
, src
);
742 /* unpack from normal memory */
751 * Combine PBO-read validation and mapping.
752 * If any GL errors are detected, they'll be recorded and NULL returned.
753 * \sa _mesa_validate_pbo_access
754 * \sa _mesa_map_pbo_source
755 * A call to this function should have a matching call to
756 * _mesa_unmap_pbo_source().
759 _mesa_map_validate_pbo_source(GLcontext
*ctx
,
761 const struct gl_pixelstore_attrib
*unpack
,
762 GLsizei width
, GLsizei height
, GLsizei depth
,
763 GLenum format
, GLenum type
, const GLvoid
*ptr
,
766 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
768 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
769 /* non-PBO access: no validation to be done */
773 if (!_mesa_validate_pbo_access(dimensions
, unpack
,
774 width
, height
, depth
, format
, type
, ptr
)) {
775 _mesa_error(ctx
, GL_INVALID_OPERATION
,
776 "%s(out of bounds PBO access)", where
);
780 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
781 /* buffer is already mapped - that's an error */
782 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
786 ptr
= _mesa_map_pbo_source(ctx
, unpack
, ptr
);
792 * Counterpart to _mesa_map_pbo_source()
795 _mesa_unmap_pbo_source(GLcontext
*ctx
,
796 const struct gl_pixelstore_attrib
*unpack
)
798 ASSERT(unpack
!= &ctx
->Pack
); /* catch pack/unpack mismatch */
799 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
800 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
807 * For commands that write to a PBO (glReadPixels, glGetColorTable, etc),
808 * if we're writing to a PBO, map it write-only and return the pointer
809 * into the PBO. If we're not writing to a PBO, return \p dst as-is.
810 * If non-null return, must call _mesa_unmap_pbo_dest() when done.
812 * \return NULL if error, else pointer to start of data
815 _mesa_map_pbo_dest(GLcontext
*ctx
,
816 const struct gl_pixelstore_attrib
*pack
,
821 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
823 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
829 buf
= ADD_POINTERS(buf
, dest
);
832 /* pack to normal memory */
841 * Combine PBO-write validation and mapping.
842 * If any GL errors are detected, they'll be recorded and NULL returned.
843 * \sa _mesa_validate_pbo_access
844 * \sa _mesa_map_pbo_dest
845 * A call to this function should have a matching call to
846 * _mesa_unmap_pbo_dest().
849 _mesa_map_validate_pbo_dest(GLcontext
*ctx
,
851 const struct gl_pixelstore_attrib
*unpack
,
852 GLsizei width
, GLsizei height
, GLsizei depth
,
853 GLenum format
, GLenum type
, GLvoid
*ptr
,
856 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
858 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
859 /* non-PBO access: no validation to be done */
863 if (!_mesa_validate_pbo_access(dimensions
, unpack
,
864 width
, height
, depth
, format
, type
, ptr
)) {
865 _mesa_error(ctx
, GL_INVALID_OPERATION
,
866 "%s(out of bounds PBO access)", where
);
870 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
871 /* buffer is already mapped - that's an error */
872 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
876 ptr
= _mesa_map_pbo_dest(ctx
, unpack
, ptr
);
882 * Counterpart to _mesa_map_pbo_dest()
885 _mesa_unmap_pbo_dest(GLcontext
*ctx
,
886 const struct gl_pixelstore_attrib
*pack
)
888 ASSERT(pack
!= &ctx
->Unpack
); /* catch pack/unpack mismatch */
889 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
890 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
, pack
->BufferObj
);
897 * Return the gl_buffer_object for the given ID.
898 * Always return NULL for ID 0.
900 struct gl_buffer_object
*
901 _mesa_lookup_bufferobj(GLcontext
*ctx
, GLuint buffer
)
906 return (struct gl_buffer_object
*)
907 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
912 * If *ptr points to obj, set ptr = the Null/default buffer object.
913 * This is a helper for buffer object deletion.
914 * The GL spec says that deleting a buffer object causes it to get
915 * unbound from all arrays in the current context.
918 unbind(GLcontext
*ctx
,
919 struct gl_buffer_object
**ptr
,
920 struct gl_buffer_object
*obj
)
923 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
929 * Plug default/fallback buffer object functions into the device
933 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
935 /* GL_ARB_vertex/pixel_buffer_object */
936 driver
->NewBufferObject
= _mesa_new_buffer_object
;
937 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
938 driver
->BindBuffer
= NULL
;
939 driver
->BufferData
= _mesa_buffer_data
;
940 driver
->BufferSubData
= _mesa_buffer_subdata
;
941 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
942 driver
->MapBuffer
= _mesa_buffer_map
;
943 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
945 /* GL_ARB_map_buffer_range */
946 driver
->MapBufferRange
= _mesa_buffer_map_range
;
947 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
949 /* GL_ARB_copy_buffer */
950 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
955 /**********************************************************************/
957 /**********************************************************************/
960 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
962 GET_CURRENT_CONTEXT(ctx
);
963 ASSERT_OUTSIDE_BEGIN_END(ctx
);
965 bind_buffer_object(ctx
, target
, buffer
);
970 * Delete a set of buffer objects.
972 * \param n Number of buffer objects to delete.
973 * \param ids Array of \c n buffer object IDs.
976 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
978 GET_CURRENT_CONTEXT(ctx
);
980 ASSERT_OUTSIDE_BEGIN_END(ctx
);
983 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
987 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
989 for (i
= 0; i
< n
; i
++) {
990 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
992 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
995 ASSERT(bufObj
->Name
== ids
[i
]);
997 if (_mesa_bufferobj_mapped(bufObj
)) {
998 /* if mapped, unmap it now */
999 ctx
->Driver
.UnmapBuffer(ctx
, 0, bufObj
);
1000 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1001 bufObj
->Pointer
= NULL
;
1004 /* unbind any vertex pointers bound to this buffer */
1005 unbind(ctx
, &arrayObj
->Vertex
.BufferObj
, bufObj
);
1006 unbind(ctx
, &arrayObj
->Weight
.BufferObj
, bufObj
);
1007 unbind(ctx
, &arrayObj
->Normal
.BufferObj
, bufObj
);
1008 unbind(ctx
, &arrayObj
->Color
.BufferObj
, bufObj
);
1009 unbind(ctx
, &arrayObj
->SecondaryColor
.BufferObj
, bufObj
);
1010 unbind(ctx
, &arrayObj
->FogCoord
.BufferObj
, bufObj
);
1011 unbind(ctx
, &arrayObj
->Index
.BufferObj
, bufObj
);
1012 unbind(ctx
, &arrayObj
->EdgeFlag
.BufferObj
, bufObj
);
1013 for (j
= 0; j
< Elements(arrayObj
->TexCoord
); j
++) {
1014 unbind(ctx
, &arrayObj
->TexCoord
[j
].BufferObj
, bufObj
);
1016 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
1017 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
1020 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1021 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
1023 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
1024 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1027 /* unbind any pixel pack/unpack pointers bound to this buffer */
1028 if (ctx
->Pack
.BufferObj
== bufObj
) {
1029 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1031 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1032 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1035 /* The ID is immediately freed for re-use */
1036 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, bufObj
->Name
);
1037 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1041 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1046 * Generate a set of unique buffer object IDs and store them in \c buffer.
1048 * \param n Number of IDs to generate.
1049 * \param buffer Array of \c n locations to store the IDs.
1052 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
1054 GET_CURRENT_CONTEXT(ctx
);
1057 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1060 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
1069 * This must be atomic (generation and allocation of buffer object IDs)
1071 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1073 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1075 /* Allocate new, empty buffer objects and return identifiers */
1076 for (i
= 0; i
< n
; i
++) {
1077 struct gl_buffer_object
*bufObj
;
1078 GLuint name
= first
+ i
;
1080 bufObj
= ctx
->Driver
.NewBufferObject( ctx
, name
, target
);
1082 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1083 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenBuffersARB");
1086 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
, bufObj
);
1087 buffer
[i
] = first
+ i
;
1090 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1095 * Determine if ID is the name of a buffer object.
1097 * \param id ID of the potential buffer object.
1098 * \return \c GL_TRUE if \c id is the name of a buffer object,
1099 * \c GL_FALSE otherwise.
1101 GLboolean GLAPIENTRY
1102 _mesa_IsBufferARB(GLuint id
)
1104 struct gl_buffer_object
*bufObj
;
1105 GET_CURRENT_CONTEXT(ctx
);
1106 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1108 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1109 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1110 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1112 return bufObj
? GL_TRUE
: GL_FALSE
;
1117 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
1118 const GLvoid
* data
, GLenum usage
)
1120 GET_CURRENT_CONTEXT(ctx
);
1121 struct gl_buffer_object
*bufObj
;
1122 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1125 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1130 case GL_STREAM_DRAW_ARB
:
1131 case GL_STREAM_READ_ARB
:
1132 case GL_STREAM_COPY_ARB
:
1133 case GL_STATIC_DRAW_ARB
:
1134 case GL_STATIC_READ_ARB
:
1135 case GL_STATIC_COPY_ARB
:
1136 case GL_DYNAMIC_DRAW_ARB
:
1137 case GL_DYNAMIC_READ_ARB
:
1138 case GL_DYNAMIC_COPY_ARB
:
1142 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
1146 bufObj
= get_buffer(ctx
, target
);
1148 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
1151 if (!_mesa_is_bufferobj(bufObj
)) {
1152 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer 0)" );
1156 if (_mesa_bufferobj_mapped(bufObj
)) {
1157 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1158 ctx
->Driver
.UnmapBuffer(ctx
, target
, bufObj
);
1159 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1160 ASSERT(bufObj
->Pointer
== NULL
);
1163 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1165 bufObj
->Written
= GL_TRUE
;
1168 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1169 bufObj
->Name
, size
, data
, usage
);
1176 ASSERT(ctx
->Driver
.BufferData
);
1177 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1178 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1184 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1185 GLsizeiptrARB size
, const GLvoid
* data
)
1187 GET_CURRENT_CONTEXT(ctx
);
1188 struct gl_buffer_object
*bufObj
;
1189 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1191 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1192 "glBufferSubDataARB" );
1194 /* error already recorded */
1198 bufObj
->Written
= GL_TRUE
;
1200 ASSERT(ctx
->Driver
.BufferSubData
);
1201 ctx
->Driver
.BufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1206 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1207 GLsizeiptrARB size
, void * data
)
1209 GET_CURRENT_CONTEXT(ctx
);
1210 struct gl_buffer_object
*bufObj
;
1211 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1213 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1214 "glGetBufferSubDataARB" );
1216 /* error already recorded */
1220 ASSERT(ctx
->Driver
.GetBufferSubData
);
1221 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1226 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1228 GET_CURRENT_CONTEXT(ctx
);
1229 struct gl_buffer_object
* bufObj
;
1230 GLbitfield accessFlags
;
1233 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1236 case GL_READ_ONLY_ARB
:
1237 accessFlags
= GL_MAP_READ_BIT
;
1239 case GL_WRITE_ONLY_ARB
:
1240 accessFlags
= GL_MAP_WRITE_BIT
;
1242 case GL_READ_WRITE_ARB
:
1243 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1246 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1250 bufObj
= get_buffer(ctx
, target
);
1252 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
1255 if (!_mesa_is_bufferobj(bufObj
)) {
1256 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(buffer 0)" );
1259 if (_mesa_bufferobj_mapped(bufObj
)) {
1260 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1264 ASSERT(ctx
->Driver
.MapBuffer
);
1265 map
= ctx
->Driver
.MapBuffer( ctx
, target
, access
, bufObj
);
1267 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1271 /* The driver callback should have set these fields.
1272 * This is important because other modules (like VBO) might call
1273 * the driver function directly.
1275 ASSERT(bufObj
->Pointer
== map
);
1276 ASSERT(bufObj
->Length
== bufObj
->Size
);
1277 ASSERT(bufObj
->Offset
== 0);
1278 bufObj
->AccessFlags
= accessFlags
;
1281 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1282 bufObj
->Written
= GL_TRUE
;
1285 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1286 bufObj
->Name
, bufObj
->Size
, access
);
1287 if (access
== GL_WRITE_ONLY_ARB
) {
1289 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1290 for (i
= 0; i
< bufObj
->Size
; i
++)
1297 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1299 /* buffer is 100 bytes larger than requested, fill with magic value */
1300 for (i
= 0; i
< 100; i
++) {
1301 buf
[bufObj
->Size
- i
- 1] = 123;
1306 return bufObj
->Pointer
;
1310 GLboolean GLAPIENTRY
1311 _mesa_UnmapBufferARB(GLenum target
)
1313 GET_CURRENT_CONTEXT(ctx
);
1314 struct gl_buffer_object
*bufObj
;
1315 GLboolean status
= GL_TRUE
;
1316 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1318 bufObj
= get_buffer(ctx
, target
);
1320 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1323 if (!_mesa_is_bufferobj(bufObj
)) {
1324 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1327 if (!_mesa_bufferobj_mapped(bufObj
)) {
1328 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1333 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1334 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1336 /* check that last 100 bytes are still = magic value */
1337 for (i
= 0; i
< 100; i
++) {
1338 GLuint pos
= bufObj
->Size
- i
- 1;
1339 if (buf
[pos
] != 123) {
1340 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1341 " at position %d (value = %u)\n",
1349 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1350 GLuint i
, unchanged
= 0;
1351 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1353 /* check which bytes changed */
1354 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1355 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1362 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1363 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1368 status
= ctx
->Driver
.UnmapBuffer( ctx
, target
, bufObj
);
1369 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1370 ASSERT(bufObj
->Pointer
== NULL
);
1371 ASSERT(bufObj
->Offset
== 0);
1372 ASSERT(bufObj
->Length
== 0);
1379 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1381 GET_CURRENT_CONTEXT(ctx
);
1382 struct gl_buffer_object
*bufObj
;
1383 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1385 bufObj
= get_buffer(ctx
, target
);
1387 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(target)" );
1390 if (!_mesa_is_bufferobj(bufObj
)) {
1391 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferParameterivARB" );
1396 case GL_BUFFER_SIZE_ARB
:
1397 *params
= (GLint
) bufObj
->Size
;
1399 case GL_BUFFER_USAGE_ARB
:
1400 *params
= bufObj
->Usage
;
1402 case GL_BUFFER_ACCESS_ARB
:
1403 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1405 case GL_BUFFER_MAPPED_ARB
:
1406 *params
= _mesa_bufferobj_mapped(bufObj
);
1408 case GL_BUFFER_ACCESS_FLAGS
:
1409 if (ctx
->VersionMajor
< 3)
1411 *params
= bufObj
->AccessFlags
;
1413 case GL_BUFFER_MAP_OFFSET
:
1414 if (ctx
->VersionMajor
< 3)
1416 *params
= (GLint
) bufObj
->Offset
;
1418 case GL_BUFFER_MAP_LENGTH
:
1419 if (ctx
->VersionMajor
< 3)
1421 *params
= (GLint
) bufObj
->Length
;
1424 ; /* fall-through */
1428 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1429 _mesa_lookup_enum_by_nr(pname
));
1435 * This is pretty much a duplicate of GetBufferParameteriv() but the
1436 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1439 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1441 GET_CURRENT_CONTEXT(ctx
);
1442 struct gl_buffer_object
*bufObj
;
1443 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1445 bufObj
= get_buffer(ctx
, target
);
1447 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(target)" );
1450 if (!_mesa_is_bufferobj(bufObj
)) {
1451 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferParameteri64v" );
1456 case GL_BUFFER_SIZE_ARB
:
1457 *params
= bufObj
->Size
;
1459 case GL_BUFFER_USAGE_ARB
:
1460 *params
= bufObj
->Usage
;
1462 case GL_BUFFER_ACCESS_ARB
:
1463 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1465 case GL_BUFFER_ACCESS_FLAGS
:
1466 if (ctx
->VersionMajor
< 3)
1468 *params
= bufObj
->AccessFlags
;
1470 case GL_BUFFER_MAPPED_ARB
:
1471 *params
= _mesa_bufferobj_mapped(bufObj
);
1473 case GL_BUFFER_MAP_OFFSET
:
1474 if (ctx
->VersionMajor
< 3)
1476 *params
= bufObj
->Offset
;
1478 case GL_BUFFER_MAP_LENGTH
:
1479 if (ctx
->VersionMajor
< 3)
1481 *params
= bufObj
->Length
;
1484 ; /* fall-through */
1488 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1489 _mesa_lookup_enum_by_nr(pname
));
1494 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1496 GET_CURRENT_CONTEXT(ctx
);
1497 struct gl_buffer_object
* bufObj
;
1498 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1500 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1501 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1505 bufObj
= get_buffer(ctx
, target
);
1507 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1510 if (!_mesa_is_bufferobj(bufObj
)) {
1511 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1515 *params
= bufObj
->Pointer
;
1520 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1521 GLintptr readOffset
, GLintptr writeOffset
,
1524 GET_CURRENT_CONTEXT(ctx
);
1525 struct gl_buffer_object
*src
, *dst
;
1526 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1528 src
= get_buffer(ctx
, readTarget
);
1529 if (!src
|| !_mesa_is_bufferobj(src
)) {
1530 _mesa_error(ctx
, GL_INVALID_ENUM
,
1531 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget
);
1535 dst
= get_buffer(ctx
, writeTarget
);
1536 if (!dst
|| !_mesa_is_bufferobj(dst
)) {
1537 _mesa_error(ctx
, GL_INVALID_ENUM
,
1538 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget
);
1542 if (_mesa_bufferobj_mapped(src
)) {
1543 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1544 "glCopyBuffserSubData(readBuffer is mapped)");
1548 if (_mesa_bufferobj_mapped(dst
)) {
1549 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1550 "glCopyBuffserSubData(writeBuffer is mapped)");
1554 if (readOffset
< 0) {
1555 _mesa_error(ctx
, GL_INVALID_VALUE
,
1556 "glCopyBuffserSubData(readOffset = %d)", readOffset
);
1560 if (writeOffset
< 0) {
1561 _mesa_error(ctx
, GL_INVALID_VALUE
,
1562 "glCopyBuffserSubData(writeOffset = %d)", writeOffset
);
1566 if (readOffset
+ size
> src
->Size
) {
1567 _mesa_error(ctx
, GL_INVALID_VALUE
,
1568 "glCopyBuffserSubData(readOffset + size = %d)",
1573 if (writeOffset
+ size
> dst
->Size
) {
1574 _mesa_error(ctx
, GL_INVALID_VALUE
,
1575 "glCopyBuffserSubData(writeOffset + size = %d)",
1581 if (readOffset
+ size
<= writeOffset
) {
1584 else if (writeOffset
+ size
<= readOffset
) {
1588 /* overlapping src/dst is illegal */
1589 _mesa_error(ctx
, GL_INVALID_VALUE
,
1590 "glCopyBuffserSubData(overlapping src/dst)");
1595 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1600 * See GL_ARB_map_buffer_range spec
1603 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1606 GET_CURRENT_CONTEXT(ctx
);
1607 struct gl_buffer_object
*bufObj
;
1610 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1612 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1613 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1614 "glMapBufferRange(extension not supported)");
1619 _mesa_error(ctx
, GL_INVALID_VALUE
,
1620 "glMapBufferRange(offset = %ld)", offset
);
1625 _mesa_error(ctx
, GL_INVALID_VALUE
,
1626 "glMapBufferRange(length = %ld)", length
);
1630 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1631 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1632 "glMapBufferRange(access indicates neither read or write)");
1636 if (access
& GL_MAP_READ_BIT
) {
1637 if ((access
& GL_MAP_INVALIDATE_RANGE_BIT
) ||
1638 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) ||
1639 (access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
1640 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1641 "glMapBufferRange(invalid access flags)");
1646 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1647 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1648 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1649 "glMapBufferRange(invalid access flags)");
1653 bufObj
= get_buffer(ctx
, target
);
1654 if (!bufObj
|| !_mesa_is_bufferobj(bufObj
)) {
1655 _mesa_error(ctx
, GL_INVALID_ENUM
,
1656 "glMapBufferRange(target = 0x%x)", target
);
1660 if (offset
+ length
> bufObj
->Size
) {
1661 _mesa_error(ctx
, GL_INVALID_VALUE
,
1662 "glMapBufferRange(offset + length > size)");
1666 if (_mesa_bufferobj_mapped(bufObj
)) {
1667 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1668 "glMapBufferRange(buffer already mapped)");
1672 ASSERT(ctx
->Driver
.MapBufferRange
);
1673 map
= ctx
->Driver
.MapBufferRange(ctx
, target
, offset
, length
,
1676 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1679 /* The driver callback should have set all these fields.
1680 * This is important because other modules (like VBO) might call
1681 * the driver function directly.
1683 ASSERT(bufObj
->Pointer
== map
);
1684 ASSERT(bufObj
->Length
== length
);
1685 ASSERT(bufObj
->Offset
== offset
);
1686 ASSERT(bufObj
->AccessFlags
== access
);
1694 * See GL_ARB_map_buffer_range spec
1697 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1699 GET_CURRENT_CONTEXT(ctx
);
1700 struct gl_buffer_object
*bufObj
;
1701 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1703 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1704 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1705 "glMapBufferRange(extension not supported)");
1710 _mesa_error(ctx
, GL_INVALID_VALUE
,
1711 "glMapBufferRange(offset = %ld)", offset
);
1716 _mesa_error(ctx
, GL_INVALID_VALUE
,
1717 "glMapBufferRange(length = %ld)", length
);
1721 bufObj
= get_buffer(ctx
, target
);
1723 _mesa_error(ctx
, GL_INVALID_ENUM
,
1724 "glMapBufferRange(target = 0x%x)", target
);
1728 if (!_mesa_is_bufferobj(bufObj
)) {
1729 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1730 "glMapBufferRange(current buffer is 0)");
1734 if (!_mesa_bufferobj_mapped(bufObj
)) {
1735 /* buffer is not mapped */
1736 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1737 "glMapBufferRange(buffer is not mapped)");
1741 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1742 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1743 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1747 if (offset
+ length
> bufObj
->Length
) {
1748 _mesa_error(ctx
, GL_INVALID_VALUE
,
1749 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1750 offset
, length
, bufObj
->Length
);
1754 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1756 if (ctx
->Driver
.FlushMappedBufferRange
)
1757 ctx
->Driver
.FlushMappedBufferRange(ctx
, target
, offset
, length
, bufObj
);
1761 #if FEATURE_APPLE_object_purgeable
1763 _mesa_BufferObjectPurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1765 struct gl_buffer_object
*bufObj
;
1768 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1770 _mesa_error(ctx
, GL_INVALID_VALUE
,
1771 "glObjectPurgeable(name = 0x%x)", name
);
1774 if (!_mesa_is_bufferobj(bufObj
)) {
1775 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1779 if (bufObj
->Purgeable
) {
1780 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1781 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1782 return GL_VOLATILE_APPLE
;
1785 bufObj
->Purgeable
= GL_TRUE
;
1787 retval
= GL_VOLATILE_APPLE
;
1788 if (ctx
->Driver
.BufferObjectPurgeable
)
1789 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1796 _mesa_RenderObjectPurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1798 struct gl_renderbuffer
*bufObj
;
1801 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1803 _mesa_error(ctx
, GL_INVALID_VALUE
,
1804 "glObjectUnpurgeable(name = 0x%x)", name
);
1808 if (bufObj
->Purgeable
) {
1809 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1810 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1811 return GL_VOLATILE_APPLE
;
1814 bufObj
->Purgeable
= GL_TRUE
;
1816 retval
= GL_VOLATILE_APPLE
;
1817 if (ctx
->Driver
.RenderObjectPurgeable
)
1818 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1825 _mesa_TextureObjectPurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1827 struct gl_texture_object
*bufObj
;
1830 bufObj
= _mesa_lookup_texture(ctx
, name
);
1832 _mesa_error(ctx
, GL_INVALID_VALUE
,
1833 "glObjectPurgeable(name = 0x%x)", name
);
1837 if (bufObj
->Purgeable
) {
1838 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1839 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1840 return GL_VOLATILE_APPLE
;
1843 bufObj
->Purgeable
= GL_TRUE
;
1845 retval
= GL_VOLATILE_APPLE
;
1846 if (ctx
->Driver
.TextureObjectPurgeable
)
1847 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1854 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1858 GET_CURRENT_CONTEXT(ctx
);
1859 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1862 _mesa_error(ctx
, GL_INVALID_VALUE
,
1863 "glObjectPurgeable(name = 0x%x)", name
);
1868 case GL_VOLATILE_APPLE
:
1869 case GL_RELEASED_APPLE
:
1873 _mesa_error(ctx
, GL_INVALID_ENUM
,
1874 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1879 switch (objectType
) {
1881 retval
= _mesa_TextureObjectPurgeable (ctx
, name
, option
);
1883 case GL_RENDERBUFFER_EXT
:
1884 retval
= _mesa_RenderObjectPurgeable (ctx
, name
, option
);
1886 case GL_BUFFER_OBJECT_APPLE
:
1887 retval
= _mesa_BufferObjectPurgeable (ctx
, name
, option
);
1890 _mesa_error(ctx
, GL_INVALID_ENUM
,
1891 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1896 /* In strict conformance to the spec, we must only return VOLATILE when
1897 * when passed the VOLATILE option. Madness.
1899 * XXX First fix the spec, then fix me.
1901 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1906 _mesa_BufferObjectUnpurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1908 struct gl_buffer_object
*bufObj
;
1911 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1913 _mesa_error(ctx
, GL_INVALID_VALUE
,
1914 "glObjectUnpurgeable(name = 0x%x)", name
);
1918 if (! bufObj
->Purgeable
) {
1919 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1920 "glObjectUnpurgeable(name = 0x%x) object is "
1921 " already \"unpurged\"", name
);
1925 bufObj
->Purgeable
= GL_FALSE
;
1928 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1929 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1936 _mesa_RenderObjectUnpurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1938 struct gl_renderbuffer
*bufObj
;
1941 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1943 _mesa_error(ctx
, GL_INVALID_VALUE
,
1944 "glObjectUnpurgeable(name = 0x%x)", name
);
1948 if (! bufObj
->Purgeable
) {
1949 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1950 "glObjectUnpurgeable(name = 0x%x) object is "
1951 " already \"unpurged\"", name
);
1955 bufObj
->Purgeable
= GL_FALSE
;
1958 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1959 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1966 _mesa_TextureObjectUnpurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1968 struct gl_texture_object
*bufObj
;
1971 bufObj
= _mesa_lookup_texture(ctx
, name
);
1973 _mesa_error(ctx
, GL_INVALID_VALUE
,
1974 "glObjectUnpurgeable(name = 0x%x)", name
);
1978 if (! bufObj
->Purgeable
) {
1979 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1980 "glObjectUnpurgeable(name = 0x%x) object is"
1981 " already \"unpurged\"", name
);
1985 bufObj
->Purgeable
= GL_FALSE
;
1988 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1989 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1996 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1998 GET_CURRENT_CONTEXT(ctx
);
1999 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
2002 _mesa_error(ctx
, GL_INVALID_VALUE
,
2003 "glObjectUnpurgeable(name = 0x%x)", name
);
2008 case GL_RETAINED_APPLE
:
2009 case GL_UNDEFINED_APPLE
:
2013 _mesa_error(ctx
, GL_INVALID_ENUM
,
2014 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2019 switch (objectType
) {
2020 case GL_BUFFER_OBJECT_APPLE
:
2021 return _mesa_BufferObjectUnpurgeable(ctx
, name
, option
);
2023 return _mesa_TextureObjectUnpurgeable(ctx
, name
, option
);
2024 case GL_RENDERBUFFER_EXT
:
2025 return _mesa_RenderObjectUnpurgeable(ctx
, name
, option
);
2027 _mesa_error(ctx
, GL_INVALID_ENUM
,
2028 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2036 _mesa_GetBufferObjectParameterivAPPLE(GLcontext
*ctx
, GLuint name
,
2037 GLenum pname
, GLint
* params
)
2039 struct gl_buffer_object
*bufObj
;
2041 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2043 _mesa_error(ctx
, GL_INVALID_VALUE
,
2044 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
2049 case GL_PURGEABLE_APPLE
:
2050 *params
= bufObj
->Purgeable
;
2053 _mesa_error(ctx
, GL_INVALID_ENUM
,
2054 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2062 _mesa_GetRenderObjectParameterivAPPLE(GLcontext
*ctx
, GLuint name
,
2063 GLenum pname
, GLint
* params
)
2065 struct gl_renderbuffer
*bufObj
;
2067 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
2069 _mesa_error(ctx
, GL_INVALID_VALUE
,
2070 "glObjectUnpurgeable(name = 0x%x)", name
);
2075 case GL_PURGEABLE_APPLE
:
2076 *params
= bufObj
->Purgeable
;
2079 _mesa_error(ctx
, GL_INVALID_ENUM
,
2080 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2088 _mesa_GetTextureObjectParameterivAPPLE(GLcontext
*ctx
, GLuint name
,
2089 GLenum pname
, GLint
* params
)
2091 struct gl_texture_object
*bufObj
;
2093 bufObj
= _mesa_lookup_texture(ctx
, name
);
2095 _mesa_error(ctx
, GL_INVALID_VALUE
,
2096 "glObjectUnpurgeable(name = 0x%x)", name
);
2101 case GL_PURGEABLE_APPLE
:
2102 *params
= bufObj
->Purgeable
;
2105 _mesa_error(ctx
, GL_INVALID_ENUM
,
2106 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2114 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
2117 GET_CURRENT_CONTEXT(ctx
);
2120 _mesa_error(ctx
, GL_INVALID_VALUE
,
2121 "glGetObjectParameteriv(name = 0x%x)", name
);
2125 switch (objectType
) {
2127 _mesa_GetTextureObjectParameterivAPPLE (ctx
, name
, pname
, params
);
2129 case GL_BUFFER_OBJECT_APPLE
:
2130 _mesa_GetBufferObjectParameterivAPPLE (ctx
, name
, pname
, params
);
2132 case GL_RENDERBUFFER_EXT
:
2133 _mesa_GetRenderObjectParameterivAPPLE (ctx
, name
, pname
, params
);
2136 _mesa_error(ctx
, GL_INVALID_ENUM
,
2137 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2142 #endif /* FEATURE_APPLE_object_purgeable */