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
39 #include "bufferobj.h"
44 /*#define BOUNDS_CHECK*/
47 #ifdef FEATURE_OES_mapbuffer
48 #define DEFAULT_ACCESS GL_MAP_WRITE_BIT
50 #define DEFAULT_ACCESS (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)
55 * Return pointer to address of a buffer object target.
56 * \param ctx the GL context
57 * \param target the buffer object target to be retrieved.
58 * \return pointer to pointer to the buffer object bound to \c target in the
59 * specified context or \c NULL if \c target is invalid.
61 static INLINE
struct gl_buffer_object
**
62 get_buffer_target(GLcontext
*ctx
, GLenum target
)
65 case GL_ARRAY_BUFFER_ARB
:
66 return &ctx
->Array
.ArrayBufferObj
;
67 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
68 return &ctx
->Array
.ElementArrayBufferObj
;
69 case GL_PIXEL_PACK_BUFFER_EXT
:
70 return &ctx
->Pack
.BufferObj
;
71 case GL_PIXEL_UNPACK_BUFFER_EXT
:
72 return &ctx
->Unpack
.BufferObj
;
73 case GL_COPY_READ_BUFFER
:
74 if (ctx
->Extensions
.ARB_copy_buffer
) {
75 return &ctx
->CopyReadBuffer
;
78 case GL_COPY_WRITE_BUFFER
:
79 if (ctx
->Extensions
.ARB_copy_buffer
) {
80 return &ctx
->CopyWriteBuffer
;
91 * Get the buffer object bound to the specified target in a GL context.
92 * \param ctx the GL context
93 * \param target the buffer object target to be retrieved.
94 * \return pointer to the buffer object bound to \c target in the
95 * specified context or \c NULL if \c target is invalid.
97 static INLINE
struct gl_buffer_object
*
98 get_buffer(GLcontext
*ctx
, GLenum target
)
100 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
108 * Convert a GLbitfield describing the mapped buffer access flags
109 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
112 simplified_access_mode(GLbitfield access
)
114 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
115 if ((access
& rwFlags
) == rwFlags
)
116 return GL_READ_WRITE
;
117 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
119 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
120 return GL_WRITE_ONLY
;
121 return GL_READ_WRITE
; /* this should never happen, but no big deal */
126 * Tests the subdata range parameters and sets the GL error code for
127 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
129 * \param ctx GL context.
130 * \param target Buffer object target on which to operate.
131 * \param offset Offset of the first byte of the subdata range.
132 * \param size Size, in bytes, of the subdata range.
133 * \param caller Name of calling function for recording errors.
134 * \return A pointer to the buffer object bound to \c target in the
135 * specified context or \c NULL if any of the parameter or state
136 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
139 * \sa glBufferSubDataARB, glGetBufferSubDataARB
141 static struct gl_buffer_object
*
142 buffer_object_subdata_range_good( GLcontext
* ctx
, GLenum target
,
143 GLintptrARB offset
, GLsizeiptrARB size
,
146 struct gl_buffer_object
*bufObj
;
149 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
154 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
158 bufObj
= get_buffer(ctx
, target
);
160 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", caller
);
163 if (!_mesa_is_bufferobj(bufObj
)) {
164 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
167 if (offset
+ size
> bufObj
->Size
) {
168 _mesa_error(ctx
, GL_INVALID_VALUE
,
169 "%s(size + offset > buffer size)", caller
);
172 if (_mesa_bufferobj_mapped(bufObj
)) {
173 /* Buffer is currently mapped */
174 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
183 * Allocate and initialize a new buffer object.
185 * Default callback for the \c dd_function_table::NewBufferObject() hook.
187 static struct gl_buffer_object
*
188 _mesa_new_buffer_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
190 struct gl_buffer_object
*obj
;
194 obj
= MALLOC_STRUCT(gl_buffer_object
);
195 _mesa_initialize_buffer_object(obj
, name
, target
);
201 * Delete a buffer object.
203 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
206 _mesa_delete_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*bufObj
)
211 _mesa_free(bufObj
->Data
);
213 /* assign strange values here to help w/ debugging */
214 bufObj
->RefCount
= -1000;
217 _glthread_DESTROY_MUTEX(bufObj
->Mutex
);
224 * Set ptr to bufObj w/ reference counting.
227 _mesa_reference_buffer_object(GLcontext
*ctx
,
228 struct gl_buffer_object
**ptr
,
229 struct gl_buffer_object
*bufObj
)
235 /* Unreference the old buffer */
236 GLboolean deleteFlag
= GL_FALSE
;
237 struct gl_buffer_object
*oldObj
= *ptr
;
239 _glthread_LOCK_MUTEX(oldObj
->Mutex
);
240 ASSERT(oldObj
->RefCount
> 0);
243 printf("BufferObj %p %d DECR to %d\n",
244 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
246 deleteFlag
= (oldObj
->RefCount
== 0);
247 _glthread_UNLOCK_MUTEX(oldObj
->Mutex
);
251 /* some sanity checking: don't delete a buffer still in use */
253 /* unfortunately, these tests are invalid during context tear-down */
254 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
255 ASSERT(ctx
->Array
.ElementArrayBufferObj
!= bufObj
);
256 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
259 ASSERT(ctx
->Driver
.DeleteBuffer
);
260 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
268 /* reference new buffer */
269 _glthread_LOCK_MUTEX(bufObj
->Mutex
);
270 if (bufObj
->RefCount
== 0) {
271 /* this buffer's being deleted (look just above) */
272 /* Not sure this can every really happen. Warn if it does. */
273 _mesa_problem(NULL
, "referencing deleted buffer object");
279 printf("BufferObj %p %d INCR to %d\n",
280 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
284 _glthread_UNLOCK_MUTEX(bufObj
->Mutex
);
290 * Initialize a buffer object to default values.
293 _mesa_initialize_buffer_object( struct gl_buffer_object
*obj
,
294 GLuint name
, GLenum target
)
298 _mesa_bzero(obj
, sizeof(struct gl_buffer_object
));
299 _glthread_INIT_MUTEX(obj
->Mutex
);
302 obj
->Usage
= GL_STATIC_DRAW_ARB
;
303 obj
->AccessFlags
= DEFAULT_ACCESS
;
308 * Allocate space for and store data in a buffer object. Any data that was
309 * previously stored in the buffer object is lost. If \c data is \c NULL,
310 * memory will be allocated, but no copy will occur.
312 * This is the default callback for \c dd_function_table::BufferData()
313 * Note that all GL error checking will have been done already.
315 * \param ctx GL context.
316 * \param target Buffer object target on which to operate.
317 * \param size Size, in bytes, of the new data store.
318 * \param data Pointer to the data to store in the buffer object. This
319 * pointer may be \c NULL.
320 * \param usage Hints about how the data will be used.
321 * \param bufObj Object to be used.
323 * \return GL_TRUE for success, GL_FALSE for failure
324 * \sa glBufferDataARB, dd_function_table::BufferData.
327 _mesa_buffer_data( GLcontext
*ctx
, GLenum target
, GLsizeiptrARB size
,
328 const GLvoid
* data
, GLenum usage
,
329 struct gl_buffer_object
* bufObj
)
333 (void) ctx
; (void) target
;
335 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
337 bufObj
->Data
= (GLubyte
*) new_data
;
339 bufObj
->Usage
= usage
;
342 _mesa_memcpy( bufObj
->Data
, data
, size
);
354 * Replace data in a subrange of buffer object. If the data range
355 * specified by \c size + \c offset extends beyond the end of the buffer or
356 * if \c data is \c NULL, no copy is performed.
358 * This is the default callback for \c dd_function_table::BufferSubData()
359 * Note that all GL error checking will have been done already.
361 * \param ctx GL context.
362 * \param target Buffer object target on which to operate.
363 * \param offset Offset of the first byte to be modified.
364 * \param size Size, in bytes, of the data range.
365 * \param data Pointer to the data to store in the buffer object.
366 * \param bufObj Object to be used.
368 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
371 _mesa_buffer_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
372 GLsizeiptrARB size
, const GLvoid
* data
,
373 struct gl_buffer_object
* bufObj
)
375 (void) ctx
; (void) target
;
377 /* this should have been caught in _mesa_BufferSubData() */
378 ASSERT(size
+ offset
<= bufObj
->Size
);
381 _mesa_memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
387 * Retrieve data from a subrange of buffer object. If the data range
388 * specified by \c size + \c offset extends beyond the end of the buffer or
389 * if \c data is \c NULL, no copy is performed.
391 * This is the default callback for \c dd_function_table::GetBufferSubData()
392 * Note that all GL error checking will have been done already.
394 * \param ctx GL context.
395 * \param target Buffer object target on which to operate.
396 * \param offset Offset of the first byte to be fetched.
397 * \param size Size, in bytes, of the data range.
398 * \param data Destination for data
399 * \param bufObj Object to be used.
401 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
404 _mesa_buffer_get_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
405 GLsizeiptrARB size
, GLvoid
* data
,
406 struct gl_buffer_object
* bufObj
)
408 (void) ctx
; (void) target
;
410 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
411 _mesa_memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
417 * Default callback for \c dd_function_tabel::MapBuffer().
419 * The function parameters will have been already tested for errors.
421 * \param ctx GL context.
422 * \param target Buffer object target on which to operate.
423 * \param access Information about how the buffer will be accessed.
424 * \param bufObj Object to be mapped.
425 * \return A pointer to the object's internal data store that can be accessed
428 * \sa glMapBufferARB, dd_function_table::MapBuffer
431 _mesa_buffer_map( GLcontext
*ctx
, GLenum target
, GLenum access
,
432 struct gl_buffer_object
*bufObj
)
437 /* Just return a direct pointer to the data */
438 if (_mesa_bufferobj_mapped(bufObj
)) {
439 /* already mapped! */
442 bufObj
->Pointer
= bufObj
->Data
;
443 bufObj
->Length
= bufObj
->Size
;
445 return bufObj
->Pointer
;
450 * Default fallback for \c dd_function_table::MapBufferRange().
451 * Called via glMapBufferRange().
454 _mesa_buffer_map_range( GLcontext
*ctx
, GLenum target
, GLintptr offset
,
455 GLsizeiptr length
, GLbitfield access
,
456 struct gl_buffer_object
*bufObj
)
460 assert(!_mesa_bufferobj_mapped(bufObj
));
461 /* Just return a direct pointer to the data */
462 bufObj
->Pointer
= bufObj
->Data
+ offset
;
463 bufObj
->Length
= length
;
464 bufObj
->Offset
= offset
;
465 bufObj
->AccessFlags
= access
;
466 return bufObj
->Pointer
;
471 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
472 * Called via glFlushMappedBufferRange().
475 _mesa_buffer_flush_mapped_range( GLcontext
*ctx
, GLenum target
,
476 GLintptr offset
, GLsizeiptr length
,
477 struct gl_buffer_object
*obj
)
489 * Default callback for \c dd_function_table::MapBuffer().
491 * The input parameters will have been already tested for errors.
493 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
496 _mesa_buffer_unmap( GLcontext
*ctx
, GLenum target
,
497 struct gl_buffer_object
*bufObj
)
501 /* XXX we might assert here that bufObj->Pointer is non-null */
502 bufObj
->Pointer
= NULL
;
505 bufObj
->AccessFlags
= 0x0;
511 * Default fallback for \c dd_function_table::CopyBufferSubData().
512 * Called via glCopyBuffserSubData().
515 _mesa_copy_buffer_subdata(GLcontext
*ctx
,
516 struct gl_buffer_object
*src
,
517 struct gl_buffer_object
*dst
,
518 GLintptr readOffset
, GLintptr writeOffset
,
521 GLubyte
*srcPtr
, *dstPtr
;
523 /* buffer should not already be mapped */
524 assert(!_mesa_bufferobj_mapped(src
));
525 assert(!_mesa_bufferobj_mapped(dst
));
527 srcPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_READ_BUFFER
,
529 dstPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_WRITE_BUFFER
,
532 if (srcPtr
&& dstPtr
)
533 _mesa_memcpy(dstPtr
+ writeOffset
, srcPtr
+ readOffset
, size
);
535 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_READ_BUFFER
, src
);
536 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_WRITE_BUFFER
, dst
);
542 * Initialize the state associated with buffer objects
545 _mesa_init_buffer_objects( GLcontext
*ctx
)
547 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
548 ctx
->Shared
->NullBufferObj
);
549 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
,
550 ctx
->Shared
->NullBufferObj
);
552 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
553 ctx
->Shared
->NullBufferObj
);
554 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
555 ctx
->Shared
->NullBufferObj
);
560 * Bind the specified target to buffer for the specified context.
561 * Called by glBindBuffer() and other functions.
564 bind_buffer_object(GLcontext
*ctx
, GLenum target
, GLuint buffer
)
566 struct gl_buffer_object
*oldBufObj
;
567 struct gl_buffer_object
*newBufObj
= NULL
;
568 struct gl_buffer_object
**bindTarget
= NULL
;
570 bindTarget
= get_buffer_target(ctx
, target
);
572 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)");
576 /* Get pointer to old buffer object (to be unbound) */
577 oldBufObj
= *bindTarget
;
578 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
579 return; /* rebinding the same buffer object- no change */
582 * Get pointer to new buffer object (newBufObj)
585 /* The spec says there's not a buffer object named 0, but we use
586 * one internally because it simplifies things.
588 newBufObj
= ctx
->Shared
->NullBufferObj
;
591 /* non-default buffer object */
592 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
594 /* if this is a new buffer object id, allocate a buffer object now */
595 ASSERT(ctx
->Driver
.NewBufferObject
);
596 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
598 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
601 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
605 /* bind new buffer */
606 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
608 /* Pass BindBuffer call to device driver */
609 if (ctx
->Driver
.BindBuffer
)
610 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
615 * Update the default buffer objects in the given context to reference those
616 * specified in the shared state and release those referencing the old
620 _mesa_update_default_objects_buffer_objects(GLcontext
*ctx
)
622 /* Bind the NullBufferObj to remove references to those
623 * in the shared context hash table.
625 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
626 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
627 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
628 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
633 * When we're about to read pixel data out of a PBO (via glDrawPixels,
634 * glTexImage, etc) or write data into a PBO (via glReadPixels,
635 * glGetTexImage, etc) we call this function to check that we're not
636 * going to read out of bounds.
638 * XXX This would also be a convenient time to check that the PBO isn't
639 * currently mapped. Whoever calls this function should check for that.
640 * Remember, we can't use a PBO when it's mapped!
642 * If we're not using a PBO, this is a no-op.
644 * \param width width of image to read/write
645 * \param height height of image to read/write
646 * \param depth depth of image to read/write
647 * \param format format of image to read/write
648 * \param type datatype of image to read/write
649 * \param ptr the user-provided pointer/offset
650 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
654 _mesa_validate_pbo_access(GLuint dimensions
,
655 const struct gl_pixelstore_attrib
*pack
,
656 GLsizei width
, GLsizei height
, GLsizei depth
,
657 GLenum format
, GLenum type
, const GLvoid
*ptr
)
660 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
662 if (!_mesa_is_bufferobj(pack
->BufferObj
))
663 return GL_TRUE
; /* no PBO, OK */
665 if (pack
->BufferObj
->Size
== 0)
669 /* get address of first pixel we'll read */
670 start
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
671 format
, type
, 0, 0, 0);
673 /* get address just past the last pixel we'll read */
674 end
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
675 format
, type
, depth
-1, height
-1, width
);
678 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
680 if ((const GLubyte
*) start
> sizeAddr
) {
681 /* This will catch negative values / wrap-around */
684 if ((const GLubyte
*) end
> sizeAddr
) {
685 /* Image read goes beyond end of buffer */
695 * For commands that read from a PBO (glDrawPixels, glTexImage,
696 * glPolygonStipple, etc), if we're reading from a PBO, map it read-only
697 * and return the pointer into the PBO. If we're not reading from a
698 * PBO, return \p src as-is.
699 * If non-null return, must call _mesa_unmap_pbo_source() when done.
701 * \return NULL if error, else pointer to start of data
704 _mesa_map_pbo_source(GLcontext
*ctx
,
705 const struct gl_pixelstore_attrib
*unpack
,
710 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
711 /* unpack from PBO */
712 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
718 buf
= ADD_POINTERS(buf
, src
);
721 /* unpack from normal memory */
730 * Combine PBO-read validation and mapping.
731 * If any GL errors are detected, they'll be recorded and NULL returned.
732 * \sa _mesa_validate_pbo_access
733 * \sa _mesa_map_pbo_source
734 * A call to this function should have a matching call to
735 * _mesa_unmap_pbo_source().
738 _mesa_map_validate_pbo_source(GLcontext
*ctx
,
740 const struct gl_pixelstore_attrib
*unpack
,
741 GLsizei width
, GLsizei height
, GLsizei depth
,
742 GLenum format
, GLenum type
, const GLvoid
*ptr
,
745 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
747 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
748 /* non-PBO access: no validation to be done */
752 if (!_mesa_validate_pbo_access(dimensions
, unpack
,
753 width
, height
, depth
, format
, type
, ptr
)) {
754 _mesa_error(ctx
, GL_INVALID_OPERATION
,
755 "%s(out of bounds PBO access)", where
);
759 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
760 /* buffer is already mapped - that's an error */
761 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
765 ptr
= _mesa_map_pbo_source(ctx
, unpack
, ptr
);
771 * Counterpart to _mesa_map_pbo_source()
774 _mesa_unmap_pbo_source(GLcontext
*ctx
,
775 const struct gl_pixelstore_attrib
*unpack
)
777 ASSERT(unpack
!= &ctx
->Pack
); /* catch pack/unpack mismatch */
778 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
779 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
786 * For commands that write to a PBO (glReadPixels, glGetColorTable, etc),
787 * if we're writing to a PBO, map it write-only and return the pointer
788 * into the PBO. If we're not writing to a PBO, return \p dst as-is.
789 * If non-null return, must call _mesa_unmap_pbo_dest() when done.
791 * \return NULL if error, else pointer to start of data
794 _mesa_map_pbo_dest(GLcontext
*ctx
,
795 const struct gl_pixelstore_attrib
*pack
,
800 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
802 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
808 buf
= ADD_POINTERS(buf
, dest
);
811 /* pack to normal memory */
820 * Combine PBO-write validation and mapping.
821 * If any GL errors are detected, they'll be recorded and NULL returned.
822 * \sa _mesa_validate_pbo_access
823 * \sa _mesa_map_pbo_dest
824 * A call to this function should have a matching call to
825 * _mesa_unmap_pbo_dest().
828 _mesa_map_validate_pbo_dest(GLcontext
*ctx
,
830 const struct gl_pixelstore_attrib
*unpack
,
831 GLsizei width
, GLsizei height
, GLsizei depth
,
832 GLenum format
, GLenum type
, GLvoid
*ptr
,
835 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
837 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
838 /* non-PBO access: no validation to be done */
842 if (!_mesa_validate_pbo_access(dimensions
, unpack
,
843 width
, height
, depth
, format
, type
, ptr
)) {
844 _mesa_error(ctx
, GL_INVALID_OPERATION
,
845 "%s(out of bounds PBO access)", where
);
849 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
850 /* buffer is already mapped - that's an error */
851 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
855 ptr
= _mesa_map_pbo_dest(ctx
, unpack
, ptr
);
861 * Counterpart to _mesa_map_pbo_dest()
864 _mesa_unmap_pbo_dest(GLcontext
*ctx
,
865 const struct gl_pixelstore_attrib
*pack
)
867 ASSERT(pack
!= &ctx
->Unpack
); /* catch pack/unpack mismatch */
868 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
869 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
, pack
->BufferObj
);
876 * Return the gl_buffer_object for the given ID.
877 * Always return NULL for ID 0.
879 struct gl_buffer_object
*
880 _mesa_lookup_bufferobj(GLcontext
*ctx
, GLuint buffer
)
885 return (struct gl_buffer_object
*)
886 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
891 * If *ptr points to obj, set ptr = the Null/default buffer object.
892 * This is a helper for buffer object deletion.
893 * The GL spec says that deleting a buffer object causes it to get
894 * unbound from all arrays in the current context.
897 unbind(GLcontext
*ctx
,
898 struct gl_buffer_object
**ptr
,
899 struct gl_buffer_object
*obj
)
902 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
908 * Plug default/fallback buffer object functions into the device
912 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
914 /* GL_ARB_vertex/pixel_buffer_object */
915 driver
->NewBufferObject
= _mesa_new_buffer_object
;
916 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
917 driver
->BindBuffer
= NULL
;
918 driver
->BufferData
= _mesa_buffer_data
;
919 driver
->BufferSubData
= _mesa_buffer_subdata
;
920 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
921 driver
->MapBuffer
= _mesa_buffer_map
;
922 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
924 /* GL_ARB_map_buffer_range */
925 driver
->MapBufferRange
= _mesa_buffer_map_range
;
926 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
928 /* GL_ARB_copy_buffer */
929 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
934 /**********************************************************************/
936 /**********************************************************************/
939 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
941 GET_CURRENT_CONTEXT(ctx
);
942 ASSERT_OUTSIDE_BEGIN_END(ctx
);
944 bind_buffer_object(ctx
, target
, buffer
);
949 * Delete a set of buffer objects.
951 * \param n Number of buffer objects to delete.
952 * \param ids Array of \c n buffer object IDs.
955 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
957 GET_CURRENT_CONTEXT(ctx
);
959 ASSERT_OUTSIDE_BEGIN_END(ctx
);
962 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
966 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
968 for (i
= 0; i
< n
; i
++) {
969 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
971 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
974 ASSERT(bufObj
->Name
== ids
[i
]);
976 if (_mesa_bufferobj_mapped(bufObj
)) {
977 /* if mapped, unmap it now */
978 ctx
->Driver
.UnmapBuffer(ctx
, 0, bufObj
);
979 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
980 bufObj
->Pointer
= NULL
;
983 /* unbind any vertex pointers bound to this buffer */
984 unbind(ctx
, &arrayObj
->Vertex
.BufferObj
, bufObj
);
985 unbind(ctx
, &arrayObj
->Weight
.BufferObj
, bufObj
);
986 unbind(ctx
, &arrayObj
->Normal
.BufferObj
, bufObj
);
987 unbind(ctx
, &arrayObj
->Color
.BufferObj
, bufObj
);
988 unbind(ctx
, &arrayObj
->SecondaryColor
.BufferObj
, bufObj
);
989 unbind(ctx
, &arrayObj
->FogCoord
.BufferObj
, bufObj
);
990 unbind(ctx
, &arrayObj
->Index
.BufferObj
, bufObj
);
991 unbind(ctx
, &arrayObj
->EdgeFlag
.BufferObj
, bufObj
);
992 for (j
= 0; j
< Elements(arrayObj
->TexCoord
); j
++) {
993 unbind(ctx
, &arrayObj
->TexCoord
[j
].BufferObj
, bufObj
);
995 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
996 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
999 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1000 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
1002 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
1003 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1006 /* unbind any pixel pack/unpack pointers bound to this buffer */
1007 if (ctx
->Pack
.BufferObj
== bufObj
) {
1008 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1010 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1011 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1014 /* The ID is immediately freed for re-use */
1015 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, bufObj
->Name
);
1016 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1020 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1025 * Generate a set of unique buffer object IDs and store them in \c buffer.
1027 * \param n Number of IDs to generate.
1028 * \param buffer Array of \c n locations to store the IDs.
1031 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
1033 GET_CURRENT_CONTEXT(ctx
);
1036 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1039 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
1048 * This must be atomic (generation and allocation of buffer object IDs)
1050 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1052 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1054 /* Allocate new, empty buffer objects and return identifiers */
1055 for (i
= 0; i
< n
; i
++) {
1056 struct gl_buffer_object
*bufObj
;
1057 GLuint name
= first
+ i
;
1059 bufObj
= ctx
->Driver
.NewBufferObject( ctx
, name
, target
);
1061 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1062 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenBuffersARB");
1065 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
, bufObj
);
1066 buffer
[i
] = first
+ i
;
1069 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1074 * Determine if ID is the name of a buffer object.
1076 * \param id ID of the potential buffer object.
1077 * \return \c GL_TRUE if \c id is the name of a buffer object,
1078 * \c GL_FALSE otherwise.
1080 GLboolean GLAPIENTRY
1081 _mesa_IsBufferARB(GLuint id
)
1083 struct gl_buffer_object
*bufObj
;
1084 GET_CURRENT_CONTEXT(ctx
);
1085 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1087 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1088 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1089 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1091 return bufObj
? GL_TRUE
: GL_FALSE
;
1096 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
1097 const GLvoid
* data
, GLenum usage
)
1099 GET_CURRENT_CONTEXT(ctx
);
1100 struct gl_buffer_object
*bufObj
;
1101 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1104 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1109 case GL_STREAM_DRAW_ARB
:
1110 case GL_STREAM_READ_ARB
:
1111 case GL_STREAM_COPY_ARB
:
1112 case GL_STATIC_DRAW_ARB
:
1113 case GL_STATIC_READ_ARB
:
1114 case GL_STATIC_COPY_ARB
:
1115 case GL_DYNAMIC_DRAW_ARB
:
1116 case GL_DYNAMIC_READ_ARB
:
1117 case GL_DYNAMIC_COPY_ARB
:
1121 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
1125 bufObj
= get_buffer(ctx
, target
);
1127 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
1130 if (!_mesa_is_bufferobj(bufObj
)) {
1131 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer 0)" );
1135 if (_mesa_bufferobj_mapped(bufObj
)) {
1136 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1137 ctx
->Driver
.UnmapBuffer(ctx
, target
, bufObj
);
1138 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1139 ASSERT(bufObj
->Pointer
== NULL
);
1142 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1144 bufObj
->Written
= GL_TRUE
;
1147 _mesa_printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1148 bufObj
->Name
, size
, data
, usage
);
1155 ASSERT(ctx
->Driver
.BufferData
);
1156 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1157 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1163 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1164 GLsizeiptrARB size
, const GLvoid
* data
)
1166 GET_CURRENT_CONTEXT(ctx
);
1167 struct gl_buffer_object
*bufObj
;
1168 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1170 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1171 "glBufferSubDataARB" );
1173 /* error already recorded */
1177 bufObj
->Written
= GL_TRUE
;
1179 ASSERT(ctx
->Driver
.BufferSubData
);
1180 ctx
->Driver
.BufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1185 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1186 GLsizeiptrARB size
, void * data
)
1188 GET_CURRENT_CONTEXT(ctx
);
1189 struct gl_buffer_object
*bufObj
;
1190 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1192 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1193 "glGetBufferSubDataARB" );
1195 /* error already recorded */
1199 ASSERT(ctx
->Driver
.GetBufferSubData
);
1200 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1205 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1207 GET_CURRENT_CONTEXT(ctx
);
1208 struct gl_buffer_object
* bufObj
;
1209 GLbitfield accessFlags
;
1212 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1215 case GL_READ_ONLY_ARB
:
1216 accessFlags
= GL_MAP_READ_BIT
;
1218 case GL_WRITE_ONLY_ARB
:
1219 accessFlags
= GL_MAP_WRITE_BIT
;
1221 case GL_READ_WRITE_ARB
:
1222 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1225 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1229 bufObj
= get_buffer(ctx
, target
);
1231 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
1234 if (!_mesa_is_bufferobj(bufObj
)) {
1235 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(buffer 0)" );
1238 if (_mesa_bufferobj_mapped(bufObj
)) {
1239 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1243 ASSERT(ctx
->Driver
.MapBuffer
);
1244 map
= ctx
->Driver
.MapBuffer( ctx
, target
, access
, bufObj
);
1246 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1250 /* The driver callback should have set these fields.
1251 * This is important because other modules (like VBO) might call
1252 * the driver function directly.
1254 ASSERT(bufObj
->Pointer
== map
);
1255 ASSERT(bufObj
->Length
== bufObj
->Size
);
1256 ASSERT(bufObj
->Offset
== 0);
1257 bufObj
->AccessFlags
= accessFlags
;
1260 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1261 bufObj
->Written
= GL_TRUE
;
1264 _mesa_printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1265 bufObj
->Name
, bufObj
->Size
, access
);
1266 if (access
== GL_WRITE_ONLY_ARB
) {
1268 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1269 for (i
= 0; i
< bufObj
->Size
; i
++)
1276 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1278 /* buffer is 100 bytes larger than requested, fill with magic value */
1279 for (i
= 0; i
< 100; i
++) {
1280 buf
[bufObj
->Size
- i
- 1] = 123;
1285 return bufObj
->Pointer
;
1289 GLboolean GLAPIENTRY
1290 _mesa_UnmapBufferARB(GLenum target
)
1292 GET_CURRENT_CONTEXT(ctx
);
1293 struct gl_buffer_object
*bufObj
;
1294 GLboolean status
= GL_TRUE
;
1295 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1297 bufObj
= get_buffer(ctx
, target
);
1299 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1302 if (!_mesa_is_bufferobj(bufObj
)) {
1303 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1306 if (!_mesa_bufferobj_mapped(bufObj
)) {
1307 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1312 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1313 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1315 /* check that last 100 bytes are still = magic value */
1316 for (i
= 0; i
< 100; i
++) {
1317 GLuint pos
= bufObj
->Size
- i
- 1;
1318 if (buf
[pos
] != 123) {
1319 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1320 " at position %d (value = %u)\n",
1328 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1329 GLuint i
, unchanged
= 0;
1330 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1332 /* check which bytes changed */
1333 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1334 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1341 _mesa_printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1342 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1347 status
= ctx
->Driver
.UnmapBuffer( ctx
, target
, bufObj
);
1348 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1349 ASSERT(bufObj
->Pointer
== NULL
);
1350 ASSERT(bufObj
->Offset
== 0);
1351 ASSERT(bufObj
->Length
== 0);
1358 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1360 GET_CURRENT_CONTEXT(ctx
);
1361 struct gl_buffer_object
*bufObj
;
1362 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1364 bufObj
= get_buffer(ctx
, target
);
1366 _mesa_error(ctx
, GL_INVALID_ENUM
, "GetBufferParameterivARB(target)" );
1369 if (!_mesa_is_bufferobj(bufObj
)) {
1370 _mesa_error(ctx
, GL_INVALID_OPERATION
, "GetBufferParameterivARB" );
1375 case GL_BUFFER_SIZE_ARB
:
1376 *params
= (GLint
) bufObj
->Size
;
1378 case GL_BUFFER_USAGE_ARB
:
1379 *params
= bufObj
->Usage
;
1381 case GL_BUFFER_ACCESS_ARB
:
1382 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1384 case GL_BUFFER_MAPPED_ARB
:
1385 *params
= _mesa_bufferobj_mapped(bufObj
);
1388 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname)");
1396 * This is pretty much a duplicate of GetBufferParameteriv() but the
1397 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1400 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1402 GET_CURRENT_CONTEXT(ctx
);
1403 struct gl_buffer_object
*bufObj
;
1404 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1406 bufObj
= get_buffer(ctx
, target
);
1408 _mesa_error(ctx
, GL_INVALID_ENUM
, "GetBufferParameteri64v(target)" );
1411 if (!_mesa_is_bufferobj(bufObj
)) {
1412 _mesa_error(ctx
, GL_INVALID_OPERATION
, "GetBufferParameteri64v" );
1417 case GL_BUFFER_SIZE_ARB
:
1418 *params
= bufObj
->Size
;
1420 case GL_BUFFER_USAGE_ARB
:
1421 *params
= bufObj
->Usage
;
1423 case GL_BUFFER_ACCESS_ARB
:
1424 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1426 case GL_BUFFER_MAPPED_ARB
:
1427 *params
= _mesa_bufferobj_mapped(bufObj
);
1430 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname)");
1437 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1439 GET_CURRENT_CONTEXT(ctx
);
1440 struct gl_buffer_object
* bufObj
;
1441 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1443 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1444 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1448 bufObj
= get_buffer(ctx
, target
);
1450 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1453 if (!_mesa_is_bufferobj(bufObj
)) {
1454 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1458 *params
= bufObj
->Pointer
;
1463 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1464 GLintptr readOffset
, GLintptr writeOffset
,
1467 GET_CURRENT_CONTEXT(ctx
);
1468 struct gl_buffer_object
*src
, *dst
;
1469 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1471 src
= get_buffer(ctx
, readTarget
);
1472 if (!src
|| !_mesa_is_bufferobj(src
)) {
1473 _mesa_error(ctx
, GL_INVALID_ENUM
,
1474 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget
);
1478 dst
= get_buffer(ctx
, writeTarget
);
1479 if (!dst
|| !_mesa_is_bufferobj(dst
)) {
1480 _mesa_error(ctx
, GL_INVALID_ENUM
,
1481 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget
);
1485 if (_mesa_bufferobj_mapped(src
)) {
1486 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1487 "glCopyBuffserSubData(readBuffer is mapped)");
1491 if (_mesa_bufferobj_mapped(dst
)) {
1492 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1493 "glCopyBuffserSubData(writeBuffer is mapped)");
1497 if (readOffset
< 0) {
1498 _mesa_error(ctx
, GL_INVALID_VALUE
,
1499 "glCopyBuffserSubData(readOffset = %d)", readOffset
);
1503 if (writeOffset
< 0) {
1504 _mesa_error(ctx
, GL_INVALID_VALUE
,
1505 "glCopyBuffserSubData(writeOffset = %d)", writeOffset
);
1509 if (readOffset
+ size
> src
->Size
) {
1510 _mesa_error(ctx
, GL_INVALID_VALUE
,
1511 "glCopyBuffserSubData(readOffset + size = %d)",
1516 if (writeOffset
+ size
> dst
->Size
) {
1517 _mesa_error(ctx
, GL_INVALID_VALUE
,
1518 "glCopyBuffserSubData(writeOffset + size = %d)",
1524 if (readOffset
+ size
<= writeOffset
) {
1527 else if (writeOffset
+ size
<= readOffset
) {
1531 /* overlapping src/dst is illegal */
1532 _mesa_error(ctx
, GL_INVALID_VALUE
,
1533 "glCopyBuffserSubData(overlapping src/dst)");
1538 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1543 * See GL_ARB_map_buffer_range spec
1546 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1549 GET_CURRENT_CONTEXT(ctx
);
1550 struct gl_buffer_object
*bufObj
;
1553 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1555 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1556 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1557 "glMapBufferRange(extension not supported)");
1562 _mesa_error(ctx
, GL_INVALID_VALUE
,
1563 "glMapBufferRange(offset = %ld)", offset
);
1568 _mesa_error(ctx
, GL_INVALID_VALUE
,
1569 "glMapBufferRange(length = %ld)", length
);
1573 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1574 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1575 "glMapBufferRange(access indicates neither read or write)");
1579 if (access
& GL_MAP_READ_BIT
) {
1580 if ((access
& GL_MAP_INVALIDATE_RANGE_BIT
) ||
1581 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) ||
1582 (access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
1583 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1584 "glMapBufferRange(invalid access flags)");
1589 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1590 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1591 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1592 "glMapBufferRange(invalid access flags)");
1596 bufObj
= get_buffer(ctx
, target
);
1597 if (!bufObj
|| !_mesa_is_bufferobj(bufObj
)) {
1598 _mesa_error(ctx
, GL_INVALID_ENUM
,
1599 "glMapBufferRange(target = 0x%x)", target
);
1603 if (offset
+ length
> bufObj
->Size
) {
1604 _mesa_error(ctx
, GL_INVALID_VALUE
,
1605 "glMapBufferRange(offset + length > size)");
1609 if (_mesa_bufferobj_mapped(bufObj
)) {
1610 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1611 "glMapBufferRange(buffer already mapped)");
1615 ASSERT(ctx
->Driver
.MapBufferRange
);
1616 map
= ctx
->Driver
.MapBufferRange(ctx
, target
, offset
, length
,
1619 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1622 /* The driver callback should have set all these fields.
1623 * This is important because other modules (like VBO) might call
1624 * the driver function directly.
1626 ASSERT(bufObj
->Pointer
== map
);
1627 ASSERT(bufObj
->Length
== length
);
1628 ASSERT(bufObj
->Offset
== offset
);
1629 ASSERT(bufObj
->AccessFlags
== access
);
1637 * See GL_ARB_map_buffer_range spec
1640 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1642 GET_CURRENT_CONTEXT(ctx
);
1643 struct gl_buffer_object
*bufObj
;
1644 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1646 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1647 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1648 "glMapBufferRange(extension not supported)");
1653 _mesa_error(ctx
, GL_INVALID_VALUE
,
1654 "glMapBufferRange(offset = %ld)", offset
);
1659 _mesa_error(ctx
, GL_INVALID_VALUE
,
1660 "glMapBufferRange(length = %ld)", length
);
1664 bufObj
= get_buffer(ctx
, target
);
1666 _mesa_error(ctx
, GL_INVALID_ENUM
,
1667 "glMapBufferRange(target = 0x%x)", target
);
1671 if (!_mesa_is_bufferobj(bufObj
)) {
1672 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1673 "glMapBufferRange(current buffer is 0)");
1677 if (!_mesa_bufferobj_mapped(bufObj
)) {
1678 /* buffer is not mapped */
1679 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1680 "glMapBufferRange(buffer is not mapped)");
1684 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1685 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1686 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1690 if (offset
+ length
> bufObj
->Length
) {
1691 _mesa_error(ctx
, GL_INVALID_VALUE
,
1692 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1693 offset
, length
, bufObj
->Length
);
1697 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1699 if (ctx
->Driver
.FlushMappedBufferRange
)
1700 ctx
->Driver
.FlushMappedBufferRange(ctx
, target
, offset
, length
, bufObj
);