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
;
94 * Get the buffer object bound to the specified target in a GL context.
95 * \param ctx the GL context
96 * \param target the buffer object target to be retrieved.
97 * \return pointer to the buffer object bound to \c target in the
98 * specified context or \c NULL if \c target is invalid.
100 static INLINE
struct gl_buffer_object
*
101 get_buffer(GLcontext
*ctx
, GLenum target
)
103 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
111 * Convert a GLbitfield describing the mapped buffer access flags
112 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
115 simplified_access_mode(GLbitfield access
)
117 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
118 if ((access
& rwFlags
) == rwFlags
)
119 return GL_READ_WRITE
;
120 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
122 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
123 return GL_WRITE_ONLY
;
124 return GL_READ_WRITE
; /* this should never happen, but no big deal */
129 * Tests the subdata range parameters and sets the GL error code for
130 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
132 * \param ctx GL context.
133 * \param target Buffer object target on which to operate.
134 * \param offset Offset of the first byte of the subdata range.
135 * \param size Size, in bytes, of the subdata range.
136 * \param caller Name of calling function for recording errors.
137 * \return A pointer to the buffer object bound to \c target in the
138 * specified context or \c NULL if any of the parameter or state
139 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
142 * \sa glBufferSubDataARB, glGetBufferSubDataARB
144 static struct gl_buffer_object
*
145 buffer_object_subdata_range_good( GLcontext
* ctx
, GLenum target
,
146 GLintptrARB offset
, GLsizeiptrARB size
,
149 struct gl_buffer_object
*bufObj
;
152 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
157 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
161 bufObj
= get_buffer(ctx
, target
);
163 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", caller
);
166 if (!_mesa_is_bufferobj(bufObj
)) {
167 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
170 if (offset
+ size
> bufObj
->Size
) {
171 _mesa_error(ctx
, GL_INVALID_VALUE
,
172 "%s(size + offset > buffer size)", caller
);
175 if (_mesa_bufferobj_mapped(bufObj
)) {
176 /* Buffer is currently mapped */
177 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
186 * Allocate and initialize a new buffer object.
188 * Default callback for the \c dd_function_table::NewBufferObject() hook.
190 static struct gl_buffer_object
*
191 _mesa_new_buffer_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
193 struct gl_buffer_object
*obj
;
197 obj
= MALLOC_STRUCT(gl_buffer_object
);
198 _mesa_initialize_buffer_object(obj
, name
, target
);
204 * Delete a buffer object.
206 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
209 _mesa_delete_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*bufObj
)
216 /* assign strange values here to help w/ debugging */
217 bufObj
->RefCount
= -1000;
220 _glthread_DESTROY_MUTEX(bufObj
->Mutex
);
227 * Set ptr to bufObj w/ reference counting.
230 _mesa_reference_buffer_object(GLcontext
*ctx
,
231 struct gl_buffer_object
**ptr
,
232 struct gl_buffer_object
*bufObj
)
238 /* Unreference the old buffer */
239 GLboolean deleteFlag
= GL_FALSE
;
240 struct gl_buffer_object
*oldObj
= *ptr
;
242 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
243 ASSERT(oldObj
->RefCount
> 0);
246 printf("BufferObj %p %d DECR to %d\n",
247 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
249 deleteFlag
= (oldObj
->RefCount
== 0);
250 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
254 /* some sanity checking: don't delete a buffer still in use */
256 /* unfortunately, these tests are invalid during context tear-down */
257 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
258 ASSERT(ctx
->Array
.ElementArrayBufferObj
!= bufObj
);
259 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
262 ASSERT(ctx
->Driver
.DeleteBuffer
);
263 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
271 /* reference new buffer */
272 _glthread_LOCK_MUTEX(bufObj
->Mutex
);
273 if (bufObj
->RefCount
== 0) {
274 /* this buffer's being deleted (look just above) */
275 /* Not sure this can every really happen. Warn if it does. */
276 _mesa_problem(NULL
, "referencing deleted buffer object");
282 printf("BufferObj %p %d INCR to %d\n",
283 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
287 _glthread_UNLOCK_MUTEX(bufObj
->Mutex
);
293 * Initialize a buffer object to default values.
296 _mesa_initialize_buffer_object( struct gl_buffer_object
*obj
,
297 GLuint name
, GLenum target
)
301 memset(obj
, 0, sizeof(struct gl_buffer_object
));
302 _glthread_INIT_MUTEX(obj
->Mutex
);
305 obj
->Usage
= GL_STATIC_DRAW_ARB
;
306 obj
->AccessFlags
= DEFAULT_ACCESS
;
311 * Allocate space for and store data in a buffer object. Any data that was
312 * previously stored in the buffer object is lost. If \c data is \c NULL,
313 * memory will be allocated, but no copy will occur.
315 * This is the default callback for \c dd_function_table::BufferData()
316 * Note that all GL error checking will have been done already.
318 * \param ctx GL context.
319 * \param target Buffer object target on which to operate.
320 * \param size Size, in bytes, of the new data store.
321 * \param data Pointer to the data to store in the buffer object. This
322 * pointer may be \c NULL.
323 * \param usage Hints about how the data will be used.
324 * \param bufObj Object to be used.
326 * \return GL_TRUE for success, GL_FALSE for failure
327 * \sa glBufferDataARB, dd_function_table::BufferData.
330 _mesa_buffer_data( GLcontext
*ctx
, GLenum target
, GLsizeiptrARB size
,
331 const GLvoid
* data
, GLenum usage
,
332 struct gl_buffer_object
* bufObj
)
336 (void) ctx
; (void) target
;
338 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
340 bufObj
->Data
= (GLubyte
*) new_data
;
342 bufObj
->Usage
= usage
;
345 memcpy( bufObj
->Data
, data
, size
);
357 * Replace data in a subrange of buffer object. If the data range
358 * specified by \c size + \c offset extends beyond the end of the buffer or
359 * if \c data is \c NULL, no copy is performed.
361 * This is the default callback for \c dd_function_table::BufferSubData()
362 * Note that all GL error checking will have been done already.
364 * \param ctx GL context.
365 * \param target Buffer object target on which to operate.
366 * \param offset Offset of the first byte to be modified.
367 * \param size Size, in bytes, of the data range.
368 * \param data Pointer to the data to store in the buffer object.
369 * \param bufObj Object to be used.
371 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
374 _mesa_buffer_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
375 GLsizeiptrARB size
, const GLvoid
* data
,
376 struct gl_buffer_object
* bufObj
)
378 (void) ctx
; (void) target
;
380 /* this should have been caught in _mesa_BufferSubData() */
381 ASSERT(size
+ offset
<= bufObj
->Size
);
384 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
390 * Retrieve data from a subrange of buffer object. If the data range
391 * specified by \c size + \c offset extends beyond the end of the buffer or
392 * if \c data is \c NULL, no copy is performed.
394 * This is the default callback for \c dd_function_table::GetBufferSubData()
395 * Note that all GL error checking will have been done already.
397 * \param ctx GL context.
398 * \param target Buffer object target on which to operate.
399 * \param offset Offset of the first byte to be fetched.
400 * \param size Size, in bytes, of the data range.
401 * \param data Destination for data
402 * \param bufObj Object to be used.
404 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
407 _mesa_buffer_get_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
408 GLsizeiptrARB size
, GLvoid
* data
,
409 struct gl_buffer_object
* bufObj
)
411 (void) ctx
; (void) target
;
413 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
414 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
420 * Default callback for \c dd_function_tabel::MapBuffer().
422 * The function parameters will have been already tested for errors.
424 * \param ctx GL context.
425 * \param target Buffer object target on which to operate.
426 * \param access Information about how the buffer will be accessed.
427 * \param bufObj Object to be mapped.
428 * \return A pointer to the object's internal data store that can be accessed
431 * \sa glMapBufferARB, dd_function_table::MapBuffer
434 _mesa_buffer_map( GLcontext
*ctx
, GLenum target
, GLenum access
,
435 struct gl_buffer_object
*bufObj
)
440 /* Just return a direct pointer to the data */
441 if (_mesa_bufferobj_mapped(bufObj
)) {
442 /* already mapped! */
445 bufObj
->Pointer
= bufObj
->Data
;
446 bufObj
->Length
= bufObj
->Size
;
448 return bufObj
->Pointer
;
453 * Default fallback for \c dd_function_table::MapBufferRange().
454 * Called via glMapBufferRange().
457 _mesa_buffer_map_range( GLcontext
*ctx
, GLenum target
, GLintptr offset
,
458 GLsizeiptr length
, GLbitfield access
,
459 struct gl_buffer_object
*bufObj
)
463 assert(!_mesa_bufferobj_mapped(bufObj
));
464 /* Just return a direct pointer to the data */
465 bufObj
->Pointer
= bufObj
->Data
+ offset
;
466 bufObj
->Length
= length
;
467 bufObj
->Offset
= offset
;
468 bufObj
->AccessFlags
= access
;
469 return bufObj
->Pointer
;
474 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
475 * Called via glFlushMappedBufferRange().
478 _mesa_buffer_flush_mapped_range( GLcontext
*ctx
, GLenum target
,
479 GLintptr offset
, GLsizeiptr length
,
480 struct gl_buffer_object
*obj
)
492 * Default callback for \c dd_function_table::MapBuffer().
494 * The input parameters will have been already tested for errors.
496 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
499 _mesa_buffer_unmap( GLcontext
*ctx
, GLenum target
,
500 struct gl_buffer_object
*bufObj
)
504 /* XXX we might assert here that bufObj->Pointer is non-null */
505 bufObj
->Pointer
= NULL
;
508 bufObj
->AccessFlags
= 0x0;
514 * Default fallback for \c dd_function_table::CopyBufferSubData().
515 * Called via glCopyBuffserSubData().
518 _mesa_copy_buffer_subdata(GLcontext
*ctx
,
519 struct gl_buffer_object
*src
,
520 struct gl_buffer_object
*dst
,
521 GLintptr readOffset
, GLintptr writeOffset
,
524 GLubyte
*srcPtr
, *dstPtr
;
526 /* buffer should not already be mapped */
527 assert(!_mesa_bufferobj_mapped(src
));
528 assert(!_mesa_bufferobj_mapped(dst
));
530 srcPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_READ_BUFFER
,
532 dstPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_WRITE_BUFFER
,
535 if (srcPtr
&& dstPtr
)
536 memcpy(dstPtr
+ writeOffset
, srcPtr
+ readOffset
, size
);
538 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_READ_BUFFER
, src
);
539 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_WRITE_BUFFER
, dst
);
545 * Initialize the state associated with buffer objects
548 _mesa_init_buffer_objects( GLcontext
*ctx
)
550 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
551 ctx
->Shared
->NullBufferObj
);
552 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
,
553 ctx
->Shared
->NullBufferObj
);
555 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
556 ctx
->Shared
->NullBufferObj
);
557 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
558 ctx
->Shared
->NullBufferObj
);
563 _mesa_free_buffer_objects( GLcontext
*ctx
)
565 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
566 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
, NULL
);
568 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
569 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
574 * Bind the specified target to buffer for the specified context.
575 * Called by glBindBuffer() and other functions.
578 bind_buffer_object(GLcontext
*ctx
, GLenum target
, GLuint buffer
)
580 struct gl_buffer_object
*oldBufObj
;
581 struct gl_buffer_object
*newBufObj
= NULL
;
582 struct gl_buffer_object
**bindTarget
= NULL
;
584 bindTarget
= get_buffer_target(ctx
, target
);
586 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)");
590 /* Get pointer to old buffer object (to be unbound) */
591 oldBufObj
= *bindTarget
;
592 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
593 return; /* rebinding the same buffer object- no change */
596 * Get pointer to new buffer object (newBufObj)
599 /* The spec says there's not a buffer object named 0, but we use
600 * one internally because it simplifies things.
602 newBufObj
= ctx
->Shared
->NullBufferObj
;
605 /* non-default buffer object */
606 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
608 /* if this is a new buffer object id, allocate a buffer object now */
609 ASSERT(ctx
->Driver
.NewBufferObject
);
610 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
612 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
615 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
619 /* bind new buffer */
620 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
622 /* Pass BindBuffer call to device driver */
623 if (ctx
->Driver
.BindBuffer
)
624 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
629 * Update the default buffer objects in the given context to reference those
630 * specified in the shared state and release those referencing the old
634 _mesa_update_default_objects_buffer_objects(GLcontext
*ctx
)
636 /* Bind the NullBufferObj to remove references to those
637 * in the shared context hash table.
639 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
640 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
641 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
642 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
647 * When we're about to read pixel data out of a PBO (via glDrawPixels,
648 * glTexImage, etc) or write data into a PBO (via glReadPixels,
649 * glGetTexImage, etc) we call this function to check that we're not
650 * going to read out of bounds.
652 * XXX This would also be a convenient time to check that the PBO isn't
653 * currently mapped. Whoever calls this function should check for that.
654 * Remember, we can't use a PBO when it's mapped!
656 * If we're not using a PBO, this is a no-op.
658 * \param width width of image to read/write
659 * \param height height of image to read/write
660 * \param depth depth of image to read/write
661 * \param format format of image to read/write
662 * \param type datatype of image to read/write
663 * \param ptr the user-provided pointer/offset
664 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
668 _mesa_validate_pbo_access(GLuint dimensions
,
669 const struct gl_pixelstore_attrib
*pack
,
670 GLsizei width
, GLsizei height
, GLsizei depth
,
671 GLenum format
, GLenum type
, const GLvoid
*ptr
)
674 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
676 if (!_mesa_is_bufferobj(pack
->BufferObj
))
677 return GL_TRUE
; /* no PBO, OK */
679 if (pack
->BufferObj
->Size
== 0)
683 /* get address of first pixel we'll read */
684 start
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
685 format
, type
, 0, 0, 0);
687 /* get address just past the last pixel we'll read */
688 end
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
689 format
, type
, depth
-1, height
-1, width
);
692 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
694 if ((const GLubyte
*) start
> sizeAddr
) {
695 /* This will catch negative values / wrap-around */
698 if ((const GLubyte
*) end
> sizeAddr
) {
699 /* Image read goes beyond end of buffer */
709 * For commands that read from a PBO (glDrawPixels, glTexImage,
710 * glPolygonStipple, etc), if we're reading from a PBO, map it read-only
711 * and return the pointer into the PBO. If we're not reading from a
712 * PBO, return \p src as-is.
713 * If non-null return, must call _mesa_unmap_pbo_source() when done.
715 * \return NULL if error, else pointer to start of data
718 _mesa_map_pbo_source(GLcontext
*ctx
,
719 const struct gl_pixelstore_attrib
*unpack
,
724 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
725 /* unpack from PBO */
726 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
732 buf
= ADD_POINTERS(buf
, src
);
735 /* unpack from normal memory */
744 * Combine PBO-read validation and mapping.
745 * If any GL errors are detected, they'll be recorded and NULL returned.
746 * \sa _mesa_validate_pbo_access
747 * \sa _mesa_map_pbo_source
748 * A call to this function should have a matching call to
749 * _mesa_unmap_pbo_source().
752 _mesa_map_validate_pbo_source(GLcontext
*ctx
,
754 const struct gl_pixelstore_attrib
*unpack
,
755 GLsizei width
, GLsizei height
, GLsizei depth
,
756 GLenum format
, GLenum type
, const GLvoid
*ptr
,
759 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
761 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
762 /* non-PBO access: no validation to be done */
766 if (!_mesa_validate_pbo_access(dimensions
, unpack
,
767 width
, height
, depth
, format
, type
, ptr
)) {
768 _mesa_error(ctx
, GL_INVALID_OPERATION
,
769 "%s(out of bounds PBO access)", where
);
773 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
774 /* buffer is already mapped - that's an error */
775 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
779 ptr
= _mesa_map_pbo_source(ctx
, unpack
, ptr
);
785 * Counterpart to _mesa_map_pbo_source()
788 _mesa_unmap_pbo_source(GLcontext
*ctx
,
789 const struct gl_pixelstore_attrib
*unpack
)
791 ASSERT(unpack
!= &ctx
->Pack
); /* catch pack/unpack mismatch */
792 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
793 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
800 * For commands that write to a PBO (glReadPixels, glGetColorTable, etc),
801 * if we're writing to a PBO, map it write-only and return the pointer
802 * into the PBO. If we're not writing to a PBO, return \p dst as-is.
803 * If non-null return, must call _mesa_unmap_pbo_dest() when done.
805 * \return NULL if error, else pointer to start of data
808 _mesa_map_pbo_dest(GLcontext
*ctx
,
809 const struct gl_pixelstore_attrib
*pack
,
814 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
816 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
822 buf
= ADD_POINTERS(buf
, dest
);
825 /* pack to normal memory */
834 * Combine PBO-write validation and mapping.
835 * If any GL errors are detected, they'll be recorded and NULL returned.
836 * \sa _mesa_validate_pbo_access
837 * \sa _mesa_map_pbo_dest
838 * A call to this function should have a matching call to
839 * _mesa_unmap_pbo_dest().
842 _mesa_map_validate_pbo_dest(GLcontext
*ctx
,
844 const struct gl_pixelstore_attrib
*unpack
,
845 GLsizei width
, GLsizei height
, GLsizei depth
,
846 GLenum format
, GLenum type
, GLvoid
*ptr
,
849 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
851 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
852 /* non-PBO access: no validation to be done */
856 if (!_mesa_validate_pbo_access(dimensions
, unpack
,
857 width
, height
, depth
, format
, type
, ptr
)) {
858 _mesa_error(ctx
, GL_INVALID_OPERATION
,
859 "%s(out of bounds PBO access)", where
);
863 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
864 /* buffer is already mapped - that's an error */
865 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
869 ptr
= _mesa_map_pbo_dest(ctx
, unpack
, ptr
);
875 * Counterpart to _mesa_map_pbo_dest()
878 _mesa_unmap_pbo_dest(GLcontext
*ctx
,
879 const struct gl_pixelstore_attrib
*pack
)
881 ASSERT(pack
!= &ctx
->Unpack
); /* catch pack/unpack mismatch */
882 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
883 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
, pack
->BufferObj
);
890 * Return the gl_buffer_object for the given ID.
891 * Always return NULL for ID 0.
893 struct gl_buffer_object
*
894 _mesa_lookup_bufferobj(GLcontext
*ctx
, GLuint buffer
)
899 return (struct gl_buffer_object
*)
900 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
905 * If *ptr points to obj, set ptr = the Null/default buffer object.
906 * This is a helper for buffer object deletion.
907 * The GL spec says that deleting a buffer object causes it to get
908 * unbound from all arrays in the current context.
911 unbind(GLcontext
*ctx
,
912 struct gl_buffer_object
**ptr
,
913 struct gl_buffer_object
*obj
)
916 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
922 * Plug default/fallback buffer object functions into the device
926 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
928 /* GL_ARB_vertex/pixel_buffer_object */
929 driver
->NewBufferObject
= _mesa_new_buffer_object
;
930 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
931 driver
->BindBuffer
= NULL
;
932 driver
->BufferData
= _mesa_buffer_data
;
933 driver
->BufferSubData
= _mesa_buffer_subdata
;
934 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
935 driver
->MapBuffer
= _mesa_buffer_map
;
936 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
938 /* GL_ARB_map_buffer_range */
939 driver
->MapBufferRange
= _mesa_buffer_map_range
;
940 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
942 /* GL_ARB_copy_buffer */
943 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
948 /**********************************************************************/
950 /**********************************************************************/
953 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
955 GET_CURRENT_CONTEXT(ctx
);
956 ASSERT_OUTSIDE_BEGIN_END(ctx
);
958 bind_buffer_object(ctx
, target
, buffer
);
963 * Delete a set of buffer objects.
965 * \param n Number of buffer objects to delete.
966 * \param ids Array of \c n buffer object IDs.
969 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
971 GET_CURRENT_CONTEXT(ctx
);
973 ASSERT_OUTSIDE_BEGIN_END(ctx
);
976 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
980 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
982 for (i
= 0; i
< n
; i
++) {
983 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
985 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
988 ASSERT(bufObj
->Name
== ids
[i
]);
990 if (_mesa_bufferobj_mapped(bufObj
)) {
991 /* if mapped, unmap it now */
992 ctx
->Driver
.UnmapBuffer(ctx
, 0, bufObj
);
993 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
994 bufObj
->Pointer
= NULL
;
997 /* unbind any vertex pointers bound to this buffer */
998 unbind(ctx
, &arrayObj
->Vertex
.BufferObj
, bufObj
);
999 unbind(ctx
, &arrayObj
->Weight
.BufferObj
, bufObj
);
1000 unbind(ctx
, &arrayObj
->Normal
.BufferObj
, bufObj
);
1001 unbind(ctx
, &arrayObj
->Color
.BufferObj
, bufObj
);
1002 unbind(ctx
, &arrayObj
->SecondaryColor
.BufferObj
, bufObj
);
1003 unbind(ctx
, &arrayObj
->FogCoord
.BufferObj
, bufObj
);
1004 unbind(ctx
, &arrayObj
->Index
.BufferObj
, bufObj
);
1005 unbind(ctx
, &arrayObj
->EdgeFlag
.BufferObj
, bufObj
);
1006 for (j
= 0; j
< Elements(arrayObj
->TexCoord
); j
++) {
1007 unbind(ctx
, &arrayObj
->TexCoord
[j
].BufferObj
, bufObj
);
1009 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
1010 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
1013 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1014 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
1016 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
1017 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1020 /* unbind any pixel pack/unpack pointers bound to this buffer */
1021 if (ctx
->Pack
.BufferObj
== bufObj
) {
1022 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1024 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1025 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1028 /* The ID is immediately freed for re-use */
1029 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, bufObj
->Name
);
1030 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1034 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1039 * Generate a set of unique buffer object IDs and store them in \c buffer.
1041 * \param n Number of IDs to generate.
1042 * \param buffer Array of \c n locations to store the IDs.
1045 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
1047 GET_CURRENT_CONTEXT(ctx
);
1050 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1053 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
1062 * This must be atomic (generation and allocation of buffer object IDs)
1064 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1066 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1068 /* Allocate new, empty buffer objects and return identifiers */
1069 for (i
= 0; i
< n
; i
++) {
1070 struct gl_buffer_object
*bufObj
;
1071 GLuint name
= first
+ i
;
1073 bufObj
= ctx
->Driver
.NewBufferObject( ctx
, name
, target
);
1075 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1076 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenBuffersARB");
1079 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
, bufObj
);
1080 buffer
[i
] = first
+ i
;
1083 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1088 * Determine if ID is the name of a buffer object.
1090 * \param id ID of the potential buffer object.
1091 * \return \c GL_TRUE if \c id is the name of a buffer object,
1092 * \c GL_FALSE otherwise.
1094 GLboolean GLAPIENTRY
1095 _mesa_IsBufferARB(GLuint id
)
1097 struct gl_buffer_object
*bufObj
;
1098 GET_CURRENT_CONTEXT(ctx
);
1099 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1101 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1102 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1103 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1105 return bufObj
? GL_TRUE
: GL_FALSE
;
1110 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
1111 const GLvoid
* data
, GLenum usage
)
1113 GET_CURRENT_CONTEXT(ctx
);
1114 struct gl_buffer_object
*bufObj
;
1115 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1118 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1123 case GL_STREAM_DRAW_ARB
:
1124 case GL_STREAM_READ_ARB
:
1125 case GL_STREAM_COPY_ARB
:
1126 case GL_STATIC_DRAW_ARB
:
1127 case GL_STATIC_READ_ARB
:
1128 case GL_STATIC_COPY_ARB
:
1129 case GL_DYNAMIC_DRAW_ARB
:
1130 case GL_DYNAMIC_READ_ARB
:
1131 case GL_DYNAMIC_COPY_ARB
:
1135 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
1139 bufObj
= get_buffer(ctx
, target
);
1141 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
1144 if (!_mesa_is_bufferobj(bufObj
)) {
1145 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer 0)" );
1149 if (_mesa_bufferobj_mapped(bufObj
)) {
1150 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1151 ctx
->Driver
.UnmapBuffer(ctx
, target
, bufObj
);
1152 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1153 ASSERT(bufObj
->Pointer
== NULL
);
1156 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1158 bufObj
->Written
= GL_TRUE
;
1161 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1162 bufObj
->Name
, size
, data
, usage
);
1169 ASSERT(ctx
->Driver
.BufferData
);
1170 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1171 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1177 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1178 GLsizeiptrARB size
, const GLvoid
* data
)
1180 GET_CURRENT_CONTEXT(ctx
);
1181 struct gl_buffer_object
*bufObj
;
1182 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1184 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1185 "glBufferSubDataARB" );
1187 /* error already recorded */
1191 bufObj
->Written
= GL_TRUE
;
1193 ASSERT(ctx
->Driver
.BufferSubData
);
1194 ctx
->Driver
.BufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1199 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1200 GLsizeiptrARB size
, void * data
)
1202 GET_CURRENT_CONTEXT(ctx
);
1203 struct gl_buffer_object
*bufObj
;
1204 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1206 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1207 "glGetBufferSubDataARB" );
1209 /* error already recorded */
1213 ASSERT(ctx
->Driver
.GetBufferSubData
);
1214 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1219 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1221 GET_CURRENT_CONTEXT(ctx
);
1222 struct gl_buffer_object
* bufObj
;
1223 GLbitfield accessFlags
;
1226 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1229 case GL_READ_ONLY_ARB
:
1230 accessFlags
= GL_MAP_READ_BIT
;
1232 case GL_WRITE_ONLY_ARB
:
1233 accessFlags
= GL_MAP_WRITE_BIT
;
1235 case GL_READ_WRITE_ARB
:
1236 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1239 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1243 bufObj
= get_buffer(ctx
, target
);
1245 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
1248 if (!_mesa_is_bufferobj(bufObj
)) {
1249 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(buffer 0)" );
1252 if (_mesa_bufferobj_mapped(bufObj
)) {
1253 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1257 ASSERT(ctx
->Driver
.MapBuffer
);
1258 map
= ctx
->Driver
.MapBuffer( ctx
, target
, access
, bufObj
);
1260 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1264 /* The driver callback should have set these fields.
1265 * This is important because other modules (like VBO) might call
1266 * the driver function directly.
1268 ASSERT(bufObj
->Pointer
== map
);
1269 ASSERT(bufObj
->Length
== bufObj
->Size
);
1270 ASSERT(bufObj
->Offset
== 0);
1271 bufObj
->AccessFlags
= accessFlags
;
1274 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1275 bufObj
->Written
= GL_TRUE
;
1278 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1279 bufObj
->Name
, bufObj
->Size
, access
);
1280 if (access
== GL_WRITE_ONLY_ARB
) {
1282 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1283 for (i
= 0; i
< bufObj
->Size
; i
++)
1290 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1292 /* buffer is 100 bytes larger than requested, fill with magic value */
1293 for (i
= 0; i
< 100; i
++) {
1294 buf
[bufObj
->Size
- i
- 1] = 123;
1299 return bufObj
->Pointer
;
1303 GLboolean GLAPIENTRY
1304 _mesa_UnmapBufferARB(GLenum target
)
1306 GET_CURRENT_CONTEXT(ctx
);
1307 struct gl_buffer_object
*bufObj
;
1308 GLboolean status
= GL_TRUE
;
1309 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1311 bufObj
= get_buffer(ctx
, target
);
1313 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1316 if (!_mesa_is_bufferobj(bufObj
)) {
1317 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1320 if (!_mesa_bufferobj_mapped(bufObj
)) {
1321 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1326 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1327 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1329 /* check that last 100 bytes are still = magic value */
1330 for (i
= 0; i
< 100; i
++) {
1331 GLuint pos
= bufObj
->Size
- i
- 1;
1332 if (buf
[pos
] != 123) {
1333 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1334 " at position %d (value = %u)\n",
1342 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1343 GLuint i
, unchanged
= 0;
1344 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1346 /* check which bytes changed */
1347 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1348 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1355 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1356 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1361 status
= ctx
->Driver
.UnmapBuffer( ctx
, target
, bufObj
);
1362 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1363 ASSERT(bufObj
->Pointer
== NULL
);
1364 ASSERT(bufObj
->Offset
== 0);
1365 ASSERT(bufObj
->Length
== 0);
1372 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1374 GET_CURRENT_CONTEXT(ctx
);
1375 struct gl_buffer_object
*bufObj
;
1376 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1378 bufObj
= get_buffer(ctx
, target
);
1380 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(target)" );
1383 if (!_mesa_is_bufferobj(bufObj
)) {
1384 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferParameterivARB" );
1389 case GL_BUFFER_SIZE_ARB
:
1390 *params
= (GLint
) bufObj
->Size
;
1392 case GL_BUFFER_USAGE_ARB
:
1393 *params
= bufObj
->Usage
;
1395 case GL_BUFFER_ACCESS_ARB
:
1396 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1398 case GL_BUFFER_MAPPED_ARB
:
1399 *params
= _mesa_bufferobj_mapped(bufObj
);
1401 case GL_BUFFER_ACCESS_FLAGS
:
1402 if (ctx
->VersionMajor
< 3)
1404 *params
= bufObj
->AccessFlags
;
1406 case GL_BUFFER_MAP_OFFSET
:
1407 if (ctx
->VersionMajor
< 3)
1409 *params
= (GLint
) bufObj
->Offset
;
1411 case GL_BUFFER_MAP_LENGTH
:
1412 if (ctx
->VersionMajor
< 3)
1414 *params
= (GLint
) bufObj
->Length
;
1417 ; /* fall-through */
1421 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname=%s)",
1422 _mesa_lookup_enum_by_nr(pname
));
1428 * This is pretty much a duplicate of GetBufferParameteriv() but the
1429 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1432 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1434 GET_CURRENT_CONTEXT(ctx
);
1435 struct gl_buffer_object
*bufObj
;
1436 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1438 bufObj
= get_buffer(ctx
, target
);
1440 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(target)" );
1443 if (!_mesa_is_bufferobj(bufObj
)) {
1444 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferParameteri64v" );
1449 case GL_BUFFER_SIZE_ARB
:
1450 *params
= bufObj
->Size
;
1452 case GL_BUFFER_USAGE_ARB
:
1453 *params
= bufObj
->Usage
;
1455 case GL_BUFFER_ACCESS_ARB
:
1456 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1458 case GL_BUFFER_ACCESS_FLAGS
:
1459 if (ctx
->VersionMajor
< 3)
1461 *params
= bufObj
->AccessFlags
;
1463 case GL_BUFFER_MAPPED_ARB
:
1464 *params
= _mesa_bufferobj_mapped(bufObj
);
1466 case GL_BUFFER_MAP_OFFSET
:
1467 if (ctx
->VersionMajor
< 3)
1469 *params
= bufObj
->Offset
;
1471 case GL_BUFFER_MAP_LENGTH
:
1472 if (ctx
->VersionMajor
< 3)
1474 *params
= bufObj
->Length
;
1477 ; /* fall-through */
1481 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname=%s)",
1482 _mesa_lookup_enum_by_nr(pname
));
1487 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1489 GET_CURRENT_CONTEXT(ctx
);
1490 struct gl_buffer_object
* bufObj
;
1491 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1493 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1494 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1498 bufObj
= get_buffer(ctx
, target
);
1500 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1503 if (!_mesa_is_bufferobj(bufObj
)) {
1504 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1508 *params
= bufObj
->Pointer
;
1513 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1514 GLintptr readOffset
, GLintptr writeOffset
,
1517 GET_CURRENT_CONTEXT(ctx
);
1518 struct gl_buffer_object
*src
, *dst
;
1519 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1521 src
= get_buffer(ctx
, readTarget
);
1522 if (!src
|| !_mesa_is_bufferobj(src
)) {
1523 _mesa_error(ctx
, GL_INVALID_ENUM
,
1524 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget
);
1528 dst
= get_buffer(ctx
, writeTarget
);
1529 if (!dst
|| !_mesa_is_bufferobj(dst
)) {
1530 _mesa_error(ctx
, GL_INVALID_ENUM
,
1531 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget
);
1535 if (_mesa_bufferobj_mapped(src
)) {
1536 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1537 "glCopyBuffserSubData(readBuffer is mapped)");
1541 if (_mesa_bufferobj_mapped(dst
)) {
1542 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1543 "glCopyBuffserSubData(writeBuffer is mapped)");
1547 if (readOffset
< 0) {
1548 _mesa_error(ctx
, GL_INVALID_VALUE
,
1549 "glCopyBuffserSubData(readOffset = %d)", readOffset
);
1553 if (writeOffset
< 0) {
1554 _mesa_error(ctx
, GL_INVALID_VALUE
,
1555 "glCopyBuffserSubData(writeOffset = %d)", writeOffset
);
1559 if (readOffset
+ size
> src
->Size
) {
1560 _mesa_error(ctx
, GL_INVALID_VALUE
,
1561 "glCopyBuffserSubData(readOffset + size = %d)",
1566 if (writeOffset
+ size
> dst
->Size
) {
1567 _mesa_error(ctx
, GL_INVALID_VALUE
,
1568 "glCopyBuffserSubData(writeOffset + size = %d)",
1574 if (readOffset
+ size
<= writeOffset
) {
1577 else if (writeOffset
+ size
<= readOffset
) {
1581 /* overlapping src/dst is illegal */
1582 _mesa_error(ctx
, GL_INVALID_VALUE
,
1583 "glCopyBuffserSubData(overlapping src/dst)");
1588 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1593 * See GL_ARB_map_buffer_range spec
1596 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1599 GET_CURRENT_CONTEXT(ctx
);
1600 struct gl_buffer_object
*bufObj
;
1603 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1605 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1606 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1607 "glMapBufferRange(extension not supported)");
1612 _mesa_error(ctx
, GL_INVALID_VALUE
,
1613 "glMapBufferRange(offset = %ld)", offset
);
1618 _mesa_error(ctx
, GL_INVALID_VALUE
,
1619 "glMapBufferRange(length = %ld)", length
);
1623 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1624 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1625 "glMapBufferRange(access indicates neither read or write)");
1629 if (access
& GL_MAP_READ_BIT
) {
1630 if ((access
& GL_MAP_INVALIDATE_RANGE_BIT
) ||
1631 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) ||
1632 (access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
1633 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1634 "glMapBufferRange(invalid access flags)");
1639 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1640 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1641 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1642 "glMapBufferRange(invalid access flags)");
1646 bufObj
= get_buffer(ctx
, target
);
1647 if (!bufObj
|| !_mesa_is_bufferobj(bufObj
)) {
1648 _mesa_error(ctx
, GL_INVALID_ENUM
,
1649 "glMapBufferRange(target = 0x%x)", target
);
1653 if (offset
+ length
> bufObj
->Size
) {
1654 _mesa_error(ctx
, GL_INVALID_VALUE
,
1655 "glMapBufferRange(offset + length > size)");
1659 if (_mesa_bufferobj_mapped(bufObj
)) {
1660 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1661 "glMapBufferRange(buffer already mapped)");
1665 ASSERT(ctx
->Driver
.MapBufferRange
);
1666 map
= ctx
->Driver
.MapBufferRange(ctx
, target
, offset
, length
,
1669 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1672 /* The driver callback should have set all these fields.
1673 * This is important because other modules (like VBO) might call
1674 * the driver function directly.
1676 ASSERT(bufObj
->Pointer
== map
);
1677 ASSERT(bufObj
->Length
== length
);
1678 ASSERT(bufObj
->Offset
== offset
);
1679 ASSERT(bufObj
->AccessFlags
== access
);
1687 * See GL_ARB_map_buffer_range spec
1690 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1692 GET_CURRENT_CONTEXT(ctx
);
1693 struct gl_buffer_object
*bufObj
;
1694 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1696 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1697 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1698 "glMapBufferRange(extension not supported)");
1703 _mesa_error(ctx
, GL_INVALID_VALUE
,
1704 "glMapBufferRange(offset = %ld)", offset
);
1709 _mesa_error(ctx
, GL_INVALID_VALUE
,
1710 "glMapBufferRange(length = %ld)", length
);
1714 bufObj
= get_buffer(ctx
, target
);
1716 _mesa_error(ctx
, GL_INVALID_ENUM
,
1717 "glMapBufferRange(target = 0x%x)", target
);
1721 if (!_mesa_is_bufferobj(bufObj
)) {
1722 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1723 "glMapBufferRange(current buffer is 0)");
1727 if (!_mesa_bufferobj_mapped(bufObj
)) {
1728 /* buffer is not mapped */
1729 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1730 "glMapBufferRange(buffer is not mapped)");
1734 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1735 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1736 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1740 if (offset
+ length
> bufObj
->Length
) {
1741 _mesa_error(ctx
, GL_INVALID_VALUE
,
1742 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1743 offset
, length
, bufObj
->Length
);
1747 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1749 if (ctx
->Driver
.FlushMappedBufferRange
)
1750 ctx
->Driver
.FlushMappedBufferRange(ctx
, target
, offset
, length
, bufObj
);
1754 #if FEATURE_APPLE_object_purgeable
1756 _mesa_BufferObjectPurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1758 struct gl_buffer_object
*bufObj
;
1761 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1763 _mesa_error(ctx
, GL_INVALID_VALUE
,
1764 "glObjectPurgeable(name = 0x%x)", name
);
1767 if (!_mesa_is_bufferobj(bufObj
)) {
1768 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glObjectPurgeable(buffer 0)" );
1772 if (bufObj
->Purgeable
) {
1773 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1774 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1775 return GL_VOLATILE_APPLE
;
1778 bufObj
->Purgeable
= GL_TRUE
;
1780 retval
= GL_VOLATILE_APPLE
;
1781 if (ctx
->Driver
.BufferObjectPurgeable
)
1782 retval
= ctx
->Driver
.BufferObjectPurgeable(ctx
, bufObj
, option
);
1789 _mesa_RenderObjectPurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1791 struct gl_renderbuffer
*bufObj
;
1794 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1796 _mesa_error(ctx
, GL_INVALID_VALUE
,
1797 "glObjectUnpurgeable(name = 0x%x)", name
);
1801 if (bufObj
->Purgeable
) {
1802 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1803 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1804 return GL_VOLATILE_APPLE
;
1807 bufObj
->Purgeable
= GL_TRUE
;
1809 retval
= GL_VOLATILE_APPLE
;
1810 if (ctx
->Driver
.RenderObjectPurgeable
)
1811 retval
= ctx
->Driver
.RenderObjectPurgeable(ctx
, bufObj
, option
);
1818 _mesa_TextureObjectPurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1820 struct gl_texture_object
*bufObj
;
1823 bufObj
= _mesa_lookup_texture(ctx
, name
);
1825 _mesa_error(ctx
, GL_INVALID_VALUE
,
1826 "glObjectPurgeable(name = 0x%x)", name
);
1830 if (bufObj
->Purgeable
) {
1831 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1832 "glObjectPurgeable(name = 0x%x) is already purgeable", name
);
1833 return GL_VOLATILE_APPLE
;
1836 bufObj
->Purgeable
= GL_TRUE
;
1838 retval
= GL_VOLATILE_APPLE
;
1839 if (ctx
->Driver
.TextureObjectPurgeable
)
1840 retval
= ctx
->Driver
.TextureObjectPurgeable(ctx
, bufObj
, option
);
1847 _mesa_ObjectPurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1851 GET_CURRENT_CONTEXT(ctx
);
1852 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1855 _mesa_error(ctx
, GL_INVALID_VALUE
,
1856 "glObjectPurgeable(name = 0x%x)", name
);
1861 case GL_VOLATILE_APPLE
:
1862 case GL_RELEASED_APPLE
:
1866 _mesa_error(ctx
, GL_INVALID_ENUM
,
1867 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1872 switch (objectType
) {
1874 retval
= _mesa_TextureObjectPurgeable (ctx
, name
, option
);
1876 case GL_RENDERBUFFER_EXT
:
1877 retval
= _mesa_RenderObjectPurgeable (ctx
, name
, option
);
1879 case GL_BUFFER_OBJECT_APPLE
:
1880 retval
= _mesa_BufferObjectPurgeable (ctx
, name
, option
);
1883 _mesa_error(ctx
, GL_INVALID_ENUM
,
1884 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1889 /* In strict conformance to the spec, we must only return VOLATILE when
1890 * when passed the VOLATILE option. Madness.
1892 * XXX First fix the spec, then fix me.
1894 return option
== GL_VOLATILE_APPLE
? GL_VOLATILE_APPLE
: retval
;
1899 _mesa_BufferObjectUnpurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1901 struct gl_buffer_object
*bufObj
;
1904 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
1906 _mesa_error(ctx
, GL_INVALID_VALUE
,
1907 "glObjectUnpurgeable(name = 0x%x)", name
);
1911 if (! bufObj
->Purgeable
) {
1912 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1913 "glObjectUnpurgeable(name = 0x%x) object is "
1914 " already \"unpurged\"", name
);
1918 bufObj
->Purgeable
= GL_FALSE
;
1920 retval
= GL_RETAINED_APPLE
;
1921 if (ctx
->Driver
.BufferObjectUnpurgeable
)
1922 retval
= ctx
->Driver
.BufferObjectUnpurgeable(ctx
, bufObj
, option
);
1929 _mesa_RenderObjectUnpurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1931 struct gl_renderbuffer
*bufObj
;
1934 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
1936 _mesa_error(ctx
, GL_INVALID_VALUE
,
1937 "glObjectUnpurgeable(name = 0x%x)", name
);
1941 if (! bufObj
->Purgeable
) {
1942 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1943 "glObjectUnpurgeable(name = 0x%x) object is "
1944 " already \"unpurged\"", name
);
1948 bufObj
->Purgeable
= GL_FALSE
;
1950 retval
= GL_RETAINED_APPLE
;
1951 if (ctx
->Driver
.RenderObjectUnpurgeable
)
1952 retval
= ctx
->Driver
.RenderObjectUnpurgeable(ctx
, bufObj
, option
);
1959 _mesa_TextureObjectUnpurgeable(GLcontext
*ctx
, GLuint name
, GLenum option
)
1961 struct gl_texture_object
*bufObj
;
1964 bufObj
= _mesa_lookup_texture(ctx
, name
);
1966 _mesa_error(ctx
, GL_INVALID_VALUE
,
1967 "glObjectUnpurgeable(name = 0x%x)", name
);
1971 if (! bufObj
->Purgeable
) {
1972 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1973 "glObjectUnpurgeable(name = 0x%x) object is"
1974 " already \"unpurged\"", name
);
1978 bufObj
->Purgeable
= GL_FALSE
;
1980 retval
= GL_RETAINED_APPLE
;
1981 if (ctx
->Driver
.TextureObjectUnpurgeable
)
1982 retval
= ctx
->Driver
.TextureObjectUnpurgeable(ctx
, bufObj
, option
);
1989 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType
, GLuint name
, GLenum option
)
1991 GET_CURRENT_CONTEXT(ctx
);
1992 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
1995 _mesa_error(ctx
, GL_INVALID_VALUE
,
1996 "glObjectUnpurgeable(name = 0x%x)", name
);
2001 case GL_RETAINED_APPLE
:
2002 case GL_UNDEFINED_APPLE
:
2006 _mesa_error(ctx
, GL_INVALID_ENUM
,
2007 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2012 switch (objectType
) {
2013 case GL_BUFFER_OBJECT_APPLE
:
2014 return _mesa_BufferObjectUnpurgeable(ctx
, name
, option
);
2016 return _mesa_TextureObjectUnpurgeable(ctx
, name
, option
);
2017 case GL_RENDERBUFFER_EXT
:
2018 return _mesa_RenderObjectUnpurgeable(ctx
, name
, option
);
2020 _mesa_error(ctx
, GL_INVALID_ENUM
,
2021 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2029 _mesa_GetBufferObjectParameterivAPPLE(GLcontext
*ctx
, GLuint name
,
2030 GLenum pname
, GLint
* params
)
2032 struct gl_buffer_object
*bufObj
;
2034 bufObj
= _mesa_lookup_bufferobj(ctx
, name
);
2036 _mesa_error(ctx
, GL_INVALID_VALUE
,
2037 "glGetObjectParameteriv(name = 0x%x) invalid object", name
);
2042 case GL_PURGEABLE_APPLE
:
2043 *params
= bufObj
->Purgeable
;
2046 _mesa_error(ctx
, GL_INVALID_ENUM
,
2047 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2055 _mesa_GetRenderObjectParameterivAPPLE(GLcontext
*ctx
, GLuint name
,
2056 GLenum pname
, GLint
* params
)
2058 struct gl_renderbuffer
*bufObj
;
2060 bufObj
= _mesa_lookup_renderbuffer(ctx
, name
);
2062 _mesa_error(ctx
, GL_INVALID_VALUE
,
2063 "glObjectUnpurgeable(name = 0x%x)", name
);
2068 case GL_PURGEABLE_APPLE
:
2069 *params
= bufObj
->Purgeable
;
2072 _mesa_error(ctx
, GL_INVALID_ENUM
,
2073 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2081 _mesa_GetTextureObjectParameterivAPPLE(GLcontext
*ctx
, GLuint name
,
2082 GLenum pname
, GLint
* params
)
2084 struct gl_texture_object
*bufObj
;
2086 bufObj
= _mesa_lookup_texture(ctx
, name
);
2088 _mesa_error(ctx
, GL_INVALID_VALUE
,
2089 "glObjectUnpurgeable(name = 0x%x)", name
);
2094 case GL_PURGEABLE_APPLE
:
2095 *params
= bufObj
->Purgeable
;
2098 _mesa_error(ctx
, GL_INVALID_ENUM
,
2099 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2107 _mesa_GetObjectParameterivAPPLE(GLenum objectType
, GLuint name
, GLenum pname
,
2110 GET_CURRENT_CONTEXT(ctx
);
2113 _mesa_error(ctx
, GL_INVALID_VALUE
,
2114 "glGetObjectParameteriv(name = 0x%x)", name
);
2118 switch (objectType
) {
2120 _mesa_GetTextureObjectParameterivAPPLE (ctx
, name
, pname
, params
);
2122 case GL_BUFFER_OBJECT_APPLE
:
2123 _mesa_GetBufferObjectParameterivAPPLE (ctx
, name
, pname
, params
);
2125 case GL_RENDERBUFFER_EXT
:
2126 _mesa_GetRenderObjectParameterivAPPLE (ctx
, name
, pname
, params
);
2129 _mesa_error(ctx
, GL_INVALID_ENUM
,
2130 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2135 #endif /* FEATURE_APPLE_object_purgeable */