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 _mesa_free_buffer_objects( GLcontext
*ctx
)
562 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
563 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
, NULL
);
565 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
566 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
571 * Bind the specified target to buffer for the specified context.
572 * Called by glBindBuffer() and other functions.
575 bind_buffer_object(GLcontext
*ctx
, GLenum target
, GLuint buffer
)
577 struct gl_buffer_object
*oldBufObj
;
578 struct gl_buffer_object
*newBufObj
= NULL
;
579 struct gl_buffer_object
**bindTarget
= NULL
;
581 bindTarget
= get_buffer_target(ctx
, target
);
583 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)");
587 /* Get pointer to old buffer object (to be unbound) */
588 oldBufObj
= *bindTarget
;
589 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
590 return; /* rebinding the same buffer object- no change */
593 * Get pointer to new buffer object (newBufObj)
596 /* The spec says there's not a buffer object named 0, but we use
597 * one internally because it simplifies things.
599 newBufObj
= ctx
->Shared
->NullBufferObj
;
602 /* non-default buffer object */
603 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
605 /* if this is a new buffer object id, allocate a buffer object now */
606 ASSERT(ctx
->Driver
.NewBufferObject
);
607 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
609 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
612 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
616 /* bind new buffer */
617 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
619 /* Pass BindBuffer call to device driver */
620 if (ctx
->Driver
.BindBuffer
)
621 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
626 * Update the default buffer objects in the given context to reference those
627 * specified in the shared state and release those referencing the old
631 _mesa_update_default_objects_buffer_objects(GLcontext
*ctx
)
633 /* Bind the NullBufferObj to remove references to those
634 * in the shared context hash table.
636 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
637 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
638 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
639 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
644 * When we're about to read pixel data out of a PBO (via glDrawPixels,
645 * glTexImage, etc) or write data into a PBO (via glReadPixels,
646 * glGetTexImage, etc) we call this function to check that we're not
647 * going to read out of bounds.
649 * XXX This would also be a convenient time to check that the PBO isn't
650 * currently mapped. Whoever calls this function should check for that.
651 * Remember, we can't use a PBO when it's mapped!
653 * If we're not using a PBO, this is a no-op.
655 * \param width width of image to read/write
656 * \param height height of image to read/write
657 * \param depth depth of image to read/write
658 * \param format format of image to read/write
659 * \param type datatype of image to read/write
660 * \param ptr the user-provided pointer/offset
661 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
665 _mesa_validate_pbo_access(GLuint dimensions
,
666 const struct gl_pixelstore_attrib
*pack
,
667 GLsizei width
, GLsizei height
, GLsizei depth
,
668 GLenum format
, GLenum type
, const GLvoid
*ptr
)
671 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
673 if (!_mesa_is_bufferobj(pack
->BufferObj
))
674 return GL_TRUE
; /* no PBO, OK */
676 if (pack
->BufferObj
->Size
== 0)
680 /* get address of first pixel we'll read */
681 start
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
682 format
, type
, 0, 0, 0);
684 /* get address just past the last pixel we'll read */
685 end
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
686 format
, type
, depth
-1, height
-1, width
);
689 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
691 if ((const GLubyte
*) start
> sizeAddr
) {
692 /* This will catch negative values / wrap-around */
695 if ((const GLubyte
*) end
> sizeAddr
) {
696 /* Image read goes beyond end of buffer */
706 * For commands that read from a PBO (glDrawPixels, glTexImage,
707 * glPolygonStipple, etc), if we're reading from a PBO, map it read-only
708 * and return the pointer into the PBO. If we're not reading from a
709 * PBO, return \p src as-is.
710 * If non-null return, must call _mesa_unmap_pbo_source() when done.
712 * \return NULL if error, else pointer to start of data
715 _mesa_map_pbo_source(GLcontext
*ctx
,
716 const struct gl_pixelstore_attrib
*unpack
,
721 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
722 /* unpack from PBO */
723 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
729 buf
= ADD_POINTERS(buf
, src
);
732 /* unpack from normal memory */
741 * Combine PBO-read validation and mapping.
742 * If any GL errors are detected, they'll be recorded and NULL returned.
743 * \sa _mesa_validate_pbo_access
744 * \sa _mesa_map_pbo_source
745 * A call to this function should have a matching call to
746 * _mesa_unmap_pbo_source().
749 _mesa_map_validate_pbo_source(GLcontext
*ctx
,
751 const struct gl_pixelstore_attrib
*unpack
,
752 GLsizei width
, GLsizei height
, GLsizei depth
,
753 GLenum format
, GLenum type
, const GLvoid
*ptr
,
756 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
758 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
759 /* non-PBO access: no validation to be done */
763 if (!_mesa_validate_pbo_access(dimensions
, unpack
,
764 width
, height
, depth
, format
, type
, ptr
)) {
765 _mesa_error(ctx
, GL_INVALID_OPERATION
,
766 "%s(out of bounds PBO access)", where
);
770 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
771 /* buffer is already mapped - that's an error */
772 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
776 ptr
= _mesa_map_pbo_source(ctx
, unpack
, ptr
);
782 * Counterpart to _mesa_map_pbo_source()
785 _mesa_unmap_pbo_source(GLcontext
*ctx
,
786 const struct gl_pixelstore_attrib
*unpack
)
788 ASSERT(unpack
!= &ctx
->Pack
); /* catch pack/unpack mismatch */
789 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
790 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
797 * For commands that write to a PBO (glReadPixels, glGetColorTable, etc),
798 * if we're writing to a PBO, map it write-only and return the pointer
799 * into the PBO. If we're not writing to a PBO, return \p dst as-is.
800 * If non-null return, must call _mesa_unmap_pbo_dest() when done.
802 * \return NULL if error, else pointer to start of data
805 _mesa_map_pbo_dest(GLcontext
*ctx
,
806 const struct gl_pixelstore_attrib
*pack
,
811 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
813 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
819 buf
= ADD_POINTERS(buf
, dest
);
822 /* pack to normal memory */
831 * Combine PBO-write validation and mapping.
832 * If any GL errors are detected, they'll be recorded and NULL returned.
833 * \sa _mesa_validate_pbo_access
834 * \sa _mesa_map_pbo_dest
835 * A call to this function should have a matching call to
836 * _mesa_unmap_pbo_dest().
839 _mesa_map_validate_pbo_dest(GLcontext
*ctx
,
841 const struct gl_pixelstore_attrib
*unpack
,
842 GLsizei width
, GLsizei height
, GLsizei depth
,
843 GLenum format
, GLenum type
, GLvoid
*ptr
,
846 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
848 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
849 /* non-PBO access: no validation to be done */
853 if (!_mesa_validate_pbo_access(dimensions
, unpack
,
854 width
, height
, depth
, format
, type
, ptr
)) {
855 _mesa_error(ctx
, GL_INVALID_OPERATION
,
856 "%s(out of bounds PBO access)", where
);
860 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
861 /* buffer is already mapped - that's an error */
862 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
866 ptr
= _mesa_map_pbo_dest(ctx
, unpack
, ptr
);
872 * Counterpart to _mesa_map_pbo_dest()
875 _mesa_unmap_pbo_dest(GLcontext
*ctx
,
876 const struct gl_pixelstore_attrib
*pack
)
878 ASSERT(pack
!= &ctx
->Unpack
); /* catch pack/unpack mismatch */
879 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
880 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
, pack
->BufferObj
);
887 * Return the gl_buffer_object for the given ID.
888 * Always return NULL for ID 0.
890 struct gl_buffer_object
*
891 _mesa_lookup_bufferobj(GLcontext
*ctx
, GLuint buffer
)
896 return (struct gl_buffer_object
*)
897 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
902 * If *ptr points to obj, set ptr = the Null/default buffer object.
903 * This is a helper for buffer object deletion.
904 * The GL spec says that deleting a buffer object causes it to get
905 * unbound from all arrays in the current context.
908 unbind(GLcontext
*ctx
,
909 struct gl_buffer_object
**ptr
,
910 struct gl_buffer_object
*obj
)
913 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
919 * Plug default/fallback buffer object functions into the device
923 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
925 /* GL_ARB_vertex/pixel_buffer_object */
926 driver
->NewBufferObject
= _mesa_new_buffer_object
;
927 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
928 driver
->BindBuffer
= NULL
;
929 driver
->BufferData
= _mesa_buffer_data
;
930 driver
->BufferSubData
= _mesa_buffer_subdata
;
931 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
932 driver
->MapBuffer
= _mesa_buffer_map
;
933 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
935 /* GL_ARB_map_buffer_range */
936 driver
->MapBufferRange
= _mesa_buffer_map_range
;
937 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
939 /* GL_ARB_copy_buffer */
940 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
945 /**********************************************************************/
947 /**********************************************************************/
950 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
952 GET_CURRENT_CONTEXT(ctx
);
953 ASSERT_OUTSIDE_BEGIN_END(ctx
);
955 bind_buffer_object(ctx
, target
, buffer
);
960 * Delete a set of buffer objects.
962 * \param n Number of buffer objects to delete.
963 * \param ids Array of \c n buffer object IDs.
966 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
968 GET_CURRENT_CONTEXT(ctx
);
970 ASSERT_OUTSIDE_BEGIN_END(ctx
);
973 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
977 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
979 for (i
= 0; i
< n
; i
++) {
980 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
982 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
985 ASSERT(bufObj
->Name
== ids
[i
]);
987 if (_mesa_bufferobj_mapped(bufObj
)) {
988 /* if mapped, unmap it now */
989 ctx
->Driver
.UnmapBuffer(ctx
, 0, bufObj
);
990 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
991 bufObj
->Pointer
= NULL
;
994 /* unbind any vertex pointers bound to this buffer */
995 unbind(ctx
, &arrayObj
->Vertex
.BufferObj
, bufObj
);
996 unbind(ctx
, &arrayObj
->Weight
.BufferObj
, bufObj
);
997 unbind(ctx
, &arrayObj
->Normal
.BufferObj
, bufObj
);
998 unbind(ctx
, &arrayObj
->Color
.BufferObj
, bufObj
);
999 unbind(ctx
, &arrayObj
->SecondaryColor
.BufferObj
, bufObj
);
1000 unbind(ctx
, &arrayObj
->FogCoord
.BufferObj
, bufObj
);
1001 unbind(ctx
, &arrayObj
->Index
.BufferObj
, bufObj
);
1002 unbind(ctx
, &arrayObj
->EdgeFlag
.BufferObj
, bufObj
);
1003 for (j
= 0; j
< Elements(arrayObj
->TexCoord
); j
++) {
1004 unbind(ctx
, &arrayObj
->TexCoord
[j
].BufferObj
, bufObj
);
1006 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
1007 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
1010 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1011 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
1013 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
1014 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1017 /* unbind any pixel pack/unpack pointers bound to this buffer */
1018 if (ctx
->Pack
.BufferObj
== bufObj
) {
1019 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1021 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1022 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1025 /* The ID is immediately freed for re-use */
1026 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, bufObj
->Name
);
1027 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1031 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1036 * Generate a set of unique buffer object IDs and store them in \c buffer.
1038 * \param n Number of IDs to generate.
1039 * \param buffer Array of \c n locations to store the IDs.
1042 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
1044 GET_CURRENT_CONTEXT(ctx
);
1047 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1050 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
1059 * This must be atomic (generation and allocation of buffer object IDs)
1061 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1063 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1065 /* Allocate new, empty buffer objects and return identifiers */
1066 for (i
= 0; i
< n
; i
++) {
1067 struct gl_buffer_object
*bufObj
;
1068 GLuint name
= first
+ i
;
1070 bufObj
= ctx
->Driver
.NewBufferObject( ctx
, name
, target
);
1072 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1073 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenBuffersARB");
1076 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
, bufObj
);
1077 buffer
[i
] = first
+ i
;
1080 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1085 * Determine if ID is the name of a buffer object.
1087 * \param id ID of the potential buffer object.
1088 * \return \c GL_TRUE if \c id is the name of a buffer object,
1089 * \c GL_FALSE otherwise.
1091 GLboolean GLAPIENTRY
1092 _mesa_IsBufferARB(GLuint id
)
1094 struct gl_buffer_object
*bufObj
;
1095 GET_CURRENT_CONTEXT(ctx
);
1096 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1098 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1099 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1100 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1102 return bufObj
? GL_TRUE
: GL_FALSE
;
1107 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
1108 const GLvoid
* data
, GLenum usage
)
1110 GET_CURRENT_CONTEXT(ctx
);
1111 struct gl_buffer_object
*bufObj
;
1112 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1115 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1120 case GL_STREAM_DRAW_ARB
:
1121 case GL_STREAM_READ_ARB
:
1122 case GL_STREAM_COPY_ARB
:
1123 case GL_STATIC_DRAW_ARB
:
1124 case GL_STATIC_READ_ARB
:
1125 case GL_STATIC_COPY_ARB
:
1126 case GL_DYNAMIC_DRAW_ARB
:
1127 case GL_DYNAMIC_READ_ARB
:
1128 case GL_DYNAMIC_COPY_ARB
:
1132 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
1136 bufObj
= get_buffer(ctx
, target
);
1138 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
1141 if (!_mesa_is_bufferobj(bufObj
)) {
1142 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer 0)" );
1146 if (_mesa_bufferobj_mapped(bufObj
)) {
1147 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1148 ctx
->Driver
.UnmapBuffer(ctx
, target
, bufObj
);
1149 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1150 ASSERT(bufObj
->Pointer
== NULL
);
1153 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1155 bufObj
->Written
= GL_TRUE
;
1158 _mesa_printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1159 bufObj
->Name
, size
, data
, usage
);
1166 ASSERT(ctx
->Driver
.BufferData
);
1167 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1168 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1174 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1175 GLsizeiptrARB size
, const GLvoid
* data
)
1177 GET_CURRENT_CONTEXT(ctx
);
1178 struct gl_buffer_object
*bufObj
;
1179 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1181 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1182 "glBufferSubDataARB" );
1184 /* error already recorded */
1188 bufObj
->Written
= GL_TRUE
;
1190 ASSERT(ctx
->Driver
.BufferSubData
);
1191 ctx
->Driver
.BufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1196 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1197 GLsizeiptrARB size
, void * data
)
1199 GET_CURRENT_CONTEXT(ctx
);
1200 struct gl_buffer_object
*bufObj
;
1201 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1203 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1204 "glGetBufferSubDataARB" );
1206 /* error already recorded */
1210 ASSERT(ctx
->Driver
.GetBufferSubData
);
1211 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1216 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1218 GET_CURRENT_CONTEXT(ctx
);
1219 struct gl_buffer_object
* bufObj
;
1220 GLbitfield accessFlags
;
1223 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1226 case GL_READ_ONLY_ARB
:
1227 accessFlags
= GL_MAP_READ_BIT
;
1229 case GL_WRITE_ONLY_ARB
:
1230 accessFlags
= GL_MAP_WRITE_BIT
;
1232 case GL_READ_WRITE_ARB
:
1233 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1236 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1240 bufObj
= get_buffer(ctx
, target
);
1242 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
1245 if (!_mesa_is_bufferobj(bufObj
)) {
1246 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(buffer 0)" );
1249 if (_mesa_bufferobj_mapped(bufObj
)) {
1250 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1254 ASSERT(ctx
->Driver
.MapBuffer
);
1255 map
= ctx
->Driver
.MapBuffer( ctx
, target
, access
, bufObj
);
1257 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1261 /* The driver callback should have set these fields.
1262 * This is important because other modules (like VBO) might call
1263 * the driver function directly.
1265 ASSERT(bufObj
->Pointer
== map
);
1266 ASSERT(bufObj
->Length
== bufObj
->Size
);
1267 ASSERT(bufObj
->Offset
== 0);
1268 bufObj
->AccessFlags
= accessFlags
;
1271 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1272 bufObj
->Written
= GL_TRUE
;
1275 _mesa_printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1276 bufObj
->Name
, bufObj
->Size
, access
);
1277 if (access
== GL_WRITE_ONLY_ARB
) {
1279 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1280 for (i
= 0; i
< bufObj
->Size
; i
++)
1287 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1289 /* buffer is 100 bytes larger than requested, fill with magic value */
1290 for (i
= 0; i
< 100; i
++) {
1291 buf
[bufObj
->Size
- i
- 1] = 123;
1296 return bufObj
->Pointer
;
1300 GLboolean GLAPIENTRY
1301 _mesa_UnmapBufferARB(GLenum target
)
1303 GET_CURRENT_CONTEXT(ctx
);
1304 struct gl_buffer_object
*bufObj
;
1305 GLboolean status
= GL_TRUE
;
1306 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1308 bufObj
= get_buffer(ctx
, target
);
1310 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1313 if (!_mesa_is_bufferobj(bufObj
)) {
1314 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1317 if (!_mesa_bufferobj_mapped(bufObj
)) {
1318 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1323 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1324 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1326 /* check that last 100 bytes are still = magic value */
1327 for (i
= 0; i
< 100; i
++) {
1328 GLuint pos
= bufObj
->Size
- i
- 1;
1329 if (buf
[pos
] != 123) {
1330 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1331 " at position %d (value = %u)\n",
1339 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1340 GLuint i
, unchanged
= 0;
1341 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1343 /* check which bytes changed */
1344 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1345 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1352 _mesa_printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1353 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1358 status
= ctx
->Driver
.UnmapBuffer( ctx
, target
, bufObj
);
1359 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1360 ASSERT(bufObj
->Pointer
== NULL
);
1361 ASSERT(bufObj
->Offset
== 0);
1362 ASSERT(bufObj
->Length
== 0);
1369 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1371 GET_CURRENT_CONTEXT(ctx
);
1372 struct gl_buffer_object
*bufObj
;
1373 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1375 bufObj
= get_buffer(ctx
, target
);
1377 _mesa_error(ctx
, GL_INVALID_ENUM
, "GetBufferParameterivARB(target)" );
1380 if (!_mesa_is_bufferobj(bufObj
)) {
1381 _mesa_error(ctx
, GL_INVALID_OPERATION
, "GetBufferParameterivARB" );
1386 case GL_BUFFER_SIZE_ARB
:
1387 *params
= (GLint
) bufObj
->Size
;
1389 case GL_BUFFER_USAGE_ARB
:
1390 *params
= bufObj
->Usage
;
1392 case GL_BUFFER_ACCESS_ARB
:
1393 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1395 case GL_BUFFER_MAPPED_ARB
:
1396 *params
= _mesa_bufferobj_mapped(bufObj
);
1399 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname)");
1407 * This is pretty much a duplicate of GetBufferParameteriv() but the
1408 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1411 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
1413 GET_CURRENT_CONTEXT(ctx
);
1414 struct gl_buffer_object
*bufObj
;
1415 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1417 bufObj
= get_buffer(ctx
, target
);
1419 _mesa_error(ctx
, GL_INVALID_ENUM
, "GetBufferParameteri64v(target)" );
1422 if (!_mesa_is_bufferobj(bufObj
)) {
1423 _mesa_error(ctx
, GL_INVALID_OPERATION
, "GetBufferParameteri64v" );
1428 case GL_BUFFER_SIZE_ARB
:
1429 *params
= bufObj
->Size
;
1431 case GL_BUFFER_USAGE_ARB
:
1432 *params
= bufObj
->Usage
;
1434 case GL_BUFFER_ACCESS_ARB
:
1435 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1437 case GL_BUFFER_MAPPED_ARB
:
1438 *params
= _mesa_bufferobj_mapped(bufObj
);
1441 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameteri64v(pname)");
1448 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1450 GET_CURRENT_CONTEXT(ctx
);
1451 struct gl_buffer_object
* bufObj
;
1452 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1454 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1455 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1459 bufObj
= get_buffer(ctx
, target
);
1461 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1464 if (!_mesa_is_bufferobj(bufObj
)) {
1465 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1469 *params
= bufObj
->Pointer
;
1474 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1475 GLintptr readOffset
, GLintptr writeOffset
,
1478 GET_CURRENT_CONTEXT(ctx
);
1479 struct gl_buffer_object
*src
, *dst
;
1480 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1482 src
= get_buffer(ctx
, readTarget
);
1483 if (!src
|| !_mesa_is_bufferobj(src
)) {
1484 _mesa_error(ctx
, GL_INVALID_ENUM
,
1485 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget
);
1489 dst
= get_buffer(ctx
, writeTarget
);
1490 if (!dst
|| !_mesa_is_bufferobj(dst
)) {
1491 _mesa_error(ctx
, GL_INVALID_ENUM
,
1492 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget
);
1496 if (_mesa_bufferobj_mapped(src
)) {
1497 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1498 "glCopyBuffserSubData(readBuffer is mapped)");
1502 if (_mesa_bufferobj_mapped(dst
)) {
1503 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1504 "glCopyBuffserSubData(writeBuffer is mapped)");
1508 if (readOffset
< 0) {
1509 _mesa_error(ctx
, GL_INVALID_VALUE
,
1510 "glCopyBuffserSubData(readOffset = %d)", readOffset
);
1514 if (writeOffset
< 0) {
1515 _mesa_error(ctx
, GL_INVALID_VALUE
,
1516 "glCopyBuffserSubData(writeOffset = %d)", writeOffset
);
1520 if (readOffset
+ size
> src
->Size
) {
1521 _mesa_error(ctx
, GL_INVALID_VALUE
,
1522 "glCopyBuffserSubData(readOffset + size = %d)",
1527 if (writeOffset
+ size
> dst
->Size
) {
1528 _mesa_error(ctx
, GL_INVALID_VALUE
,
1529 "glCopyBuffserSubData(writeOffset + size = %d)",
1535 if (readOffset
+ size
<= writeOffset
) {
1538 else if (writeOffset
+ size
<= readOffset
) {
1542 /* overlapping src/dst is illegal */
1543 _mesa_error(ctx
, GL_INVALID_VALUE
,
1544 "glCopyBuffserSubData(overlapping src/dst)");
1549 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1554 * See GL_ARB_map_buffer_range spec
1557 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1560 GET_CURRENT_CONTEXT(ctx
);
1561 struct gl_buffer_object
*bufObj
;
1564 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1566 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1567 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1568 "glMapBufferRange(extension not supported)");
1573 _mesa_error(ctx
, GL_INVALID_VALUE
,
1574 "glMapBufferRange(offset = %ld)", offset
);
1579 _mesa_error(ctx
, GL_INVALID_VALUE
,
1580 "glMapBufferRange(length = %ld)", length
);
1584 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1585 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1586 "glMapBufferRange(access indicates neither read or write)");
1590 if (access
& GL_MAP_READ_BIT
) {
1591 if ((access
& GL_MAP_INVALIDATE_RANGE_BIT
) ||
1592 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) ||
1593 (access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
1594 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1595 "glMapBufferRange(invalid access flags)");
1600 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1601 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1602 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1603 "glMapBufferRange(invalid access flags)");
1607 bufObj
= get_buffer(ctx
, target
);
1608 if (!bufObj
|| !_mesa_is_bufferobj(bufObj
)) {
1609 _mesa_error(ctx
, GL_INVALID_ENUM
,
1610 "glMapBufferRange(target = 0x%x)", target
);
1614 if (offset
+ length
> bufObj
->Size
) {
1615 _mesa_error(ctx
, GL_INVALID_VALUE
,
1616 "glMapBufferRange(offset + length > size)");
1620 if (_mesa_bufferobj_mapped(bufObj
)) {
1621 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1622 "glMapBufferRange(buffer already mapped)");
1626 ASSERT(ctx
->Driver
.MapBufferRange
);
1627 map
= ctx
->Driver
.MapBufferRange(ctx
, target
, offset
, length
,
1630 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1633 /* The driver callback should have set all these fields.
1634 * This is important because other modules (like VBO) might call
1635 * the driver function directly.
1637 ASSERT(bufObj
->Pointer
== map
);
1638 ASSERT(bufObj
->Length
== length
);
1639 ASSERT(bufObj
->Offset
== offset
);
1640 ASSERT(bufObj
->AccessFlags
== access
);
1648 * See GL_ARB_map_buffer_range spec
1651 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1653 GET_CURRENT_CONTEXT(ctx
);
1654 struct gl_buffer_object
*bufObj
;
1655 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1657 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1658 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1659 "glMapBufferRange(extension not supported)");
1664 _mesa_error(ctx
, GL_INVALID_VALUE
,
1665 "glMapBufferRange(offset = %ld)", offset
);
1670 _mesa_error(ctx
, GL_INVALID_VALUE
,
1671 "glMapBufferRange(length = %ld)", length
);
1675 bufObj
= get_buffer(ctx
, target
);
1677 _mesa_error(ctx
, GL_INVALID_ENUM
,
1678 "glMapBufferRange(target = 0x%x)", target
);
1682 if (!_mesa_is_bufferobj(bufObj
)) {
1683 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1684 "glMapBufferRange(current buffer is 0)");
1688 if (!_mesa_bufferobj_mapped(bufObj
)) {
1689 /* buffer is not mapped */
1690 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1691 "glMapBufferRange(buffer is not mapped)");
1695 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1696 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1697 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1701 if (offset
+ length
> bufObj
->Length
) {
1702 _mesa_error(ctx
, GL_INVALID_VALUE
,
1703 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1704 offset
, length
, bufObj
->Length
);
1708 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1710 if (ctx
->Driver
.FlushMappedBufferRange
)
1711 ctx
->Driver
.FlushMappedBufferRange(ctx
, target
, offset
, length
, bufObj
);