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;
223 * Set ptr to bufObj w/ reference counting.
226 _mesa_reference_buffer_object(GLcontext
*ctx
,
227 struct gl_buffer_object
**ptr
,
228 struct gl_buffer_object
*bufObj
)
234 /* Unreference the old buffer */
235 GLboolean deleteFlag
= GL_FALSE
;
236 struct gl_buffer_object
*oldObj
= *ptr
;
238 /*_glthread_LOCK_MUTEX(oldObj->Mutex);*/
239 ASSERT(oldObj
->RefCount
> 0);
242 printf("BufferObj %p %d DECR to %d\n",
243 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
245 deleteFlag
= (oldObj
->RefCount
== 0);
246 /*_glthread_UNLOCK_MUTEX(oldObj->Mutex);*/
250 /* some sanity checking: don't delete a buffer still in use */
252 /* unfortunately, these tests are invalid during context tear-down */
253 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
254 ASSERT(ctx
->Array
.ElementArrayBufferObj
!= bufObj
);
255 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
258 ASSERT(ctx
->Driver
.DeleteBuffer
);
259 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
267 /* reference new buffer */
268 /*_glthread_LOCK_MUTEX(tex->Mutex);*/
269 if (bufObj
->RefCount
== 0) {
270 /* this buffer's being deleted (look just above) */
271 /* Not sure this can every really happen. Warn if it does. */
272 _mesa_problem(NULL
, "referencing deleted buffer object");
278 printf("BufferObj %p %d INCR to %d\n",
279 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
283 /*_glthread_UNLOCK_MUTEX(tex->Mutex);*/
289 * Initialize a buffer object to default values.
292 _mesa_initialize_buffer_object( struct gl_buffer_object
*obj
,
293 GLuint name
, GLenum target
)
297 _mesa_bzero(obj
, sizeof(struct gl_buffer_object
));
300 obj
->Usage
= GL_STATIC_DRAW_ARB
;
301 obj
->AccessFlags
= DEFAULT_ACCESS
;
306 * Allocate space for and store data in a buffer object. Any data that was
307 * previously stored in the buffer object is lost. If \c data is \c NULL,
308 * memory will be allocated, but no copy will occur.
310 * This is the default callback for \c dd_function_table::BufferData()
311 * Note that all GL error checking will have been done already.
313 * \param ctx GL context.
314 * \param target Buffer object target on which to operate.
315 * \param size Size, in bytes, of the new data store.
316 * \param data Pointer to the data to store in the buffer object. This
317 * pointer may be \c NULL.
318 * \param usage Hints about how the data will be used.
319 * \param bufObj Object to be used.
321 * \return GL_TRUE for success, GL_FALSE for failure
322 * \sa glBufferDataARB, dd_function_table::BufferData.
325 _mesa_buffer_data( GLcontext
*ctx
, GLenum target
, GLsizeiptrARB size
,
326 const GLvoid
* data
, GLenum usage
,
327 struct gl_buffer_object
* bufObj
)
331 (void) ctx
; (void) target
;
333 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
335 bufObj
->Data
= (GLubyte
*) new_data
;
337 bufObj
->Usage
= usage
;
340 _mesa_memcpy( bufObj
->Data
, data
, size
);
352 * Replace data in a subrange of buffer object. If the data range
353 * specified by \c size + \c offset extends beyond the end of the buffer or
354 * if \c data is \c NULL, no copy is performed.
356 * This is the default callback for \c dd_function_table::BufferSubData()
357 * Note that all GL error checking will have been done already.
359 * \param ctx GL context.
360 * \param target Buffer object target on which to operate.
361 * \param offset Offset of the first byte to be modified.
362 * \param size Size, in bytes, of the data range.
363 * \param data Pointer to the data to store in the buffer object.
364 * \param bufObj Object to be used.
366 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
369 _mesa_buffer_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
370 GLsizeiptrARB size
, const GLvoid
* data
,
371 struct gl_buffer_object
* bufObj
)
373 (void) ctx
; (void) target
;
375 /* this should have been caught in _mesa_BufferSubData() */
376 ASSERT(size
+ offset
<= bufObj
->Size
);
379 _mesa_memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
385 * Retrieve data from a subrange of buffer object. If the data range
386 * specified by \c size + \c offset extends beyond the end of the buffer or
387 * if \c data is \c NULL, no copy is performed.
389 * This is the default callback for \c dd_function_table::GetBufferSubData()
390 * Note that all GL error checking will have been done already.
392 * \param ctx GL context.
393 * \param target Buffer object target on which to operate.
394 * \param offset Offset of the first byte to be fetched.
395 * \param size Size, in bytes, of the data range.
396 * \param data Destination for data
397 * \param bufObj Object to be used.
399 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
402 _mesa_buffer_get_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
403 GLsizeiptrARB size
, GLvoid
* data
,
404 struct gl_buffer_object
* bufObj
)
406 (void) ctx
; (void) target
;
408 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
409 _mesa_memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
415 * Default callback for \c dd_function_tabel::MapBuffer().
417 * The function parameters will have been already tested for errors.
419 * \param ctx GL context.
420 * \param target Buffer object target on which to operate.
421 * \param access Information about how the buffer will be accessed.
422 * \param bufObj Object to be mapped.
423 * \return A pointer to the object's internal data store that can be accessed
426 * \sa glMapBufferARB, dd_function_table::MapBuffer
429 _mesa_buffer_map( GLcontext
*ctx
, GLenum target
, GLenum access
,
430 struct gl_buffer_object
*bufObj
)
435 /* Just return a direct pointer to the data */
436 if (_mesa_bufferobj_mapped(bufObj
)) {
437 /* already mapped! */
440 bufObj
->Pointer
= bufObj
->Data
;
441 bufObj
->Length
= bufObj
->Size
;
443 return bufObj
->Pointer
;
448 * Default fallback for \c dd_function_table::MapBufferRange().
449 * Called via glMapBufferRange().
452 _mesa_buffer_map_range( GLcontext
*ctx
, GLenum target
, GLintptr offset
,
453 GLsizeiptr length
, GLbitfield access
,
454 struct gl_buffer_object
*bufObj
)
458 assert(!_mesa_bufferobj_mapped(bufObj
));
459 /* Just return a direct pointer to the data */
460 bufObj
->Pointer
= bufObj
->Data
+ offset
;
461 bufObj
->Length
= length
;
462 bufObj
->Offset
= offset
;
463 bufObj
->AccessFlags
= access
;
464 return bufObj
->Pointer
;
469 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
470 * Called via glFlushMappedBufferRange().
473 _mesa_buffer_flush_mapped_range( GLcontext
*ctx
, GLenum target
,
474 GLintptr offset
, GLsizeiptr length
,
475 struct gl_buffer_object
*obj
)
487 * Default callback for \c dd_function_table::MapBuffer().
489 * The input parameters will have been already tested for errors.
491 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
494 _mesa_buffer_unmap( GLcontext
*ctx
, GLenum target
,
495 struct gl_buffer_object
*bufObj
)
499 /* XXX we might assert here that bufObj->Pointer is non-null */
500 bufObj
->Pointer
= NULL
;
503 bufObj
->AccessFlags
= 0x0;
509 * Default fallback for \c dd_function_table::CopyBufferSubData().
510 * Called via glCopyBuffserSubData().
513 _mesa_copy_buffer_subdata(GLcontext
*ctx
,
514 struct gl_buffer_object
*src
,
515 struct gl_buffer_object
*dst
,
516 GLintptr readOffset
, GLintptr writeOffset
,
519 GLubyte
*srcPtr
, *dstPtr
;
521 /* buffer should not already be mapped */
522 assert(!_mesa_bufferobj_mapped(src
));
523 assert(!_mesa_bufferobj_mapped(dst
));
525 srcPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_READ_BUFFER
,
527 dstPtr
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_COPY_WRITE_BUFFER
,
530 if (srcPtr
&& dstPtr
)
531 _mesa_memcpy(dstPtr
+ writeOffset
, srcPtr
+ readOffset
, size
);
533 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_READ_BUFFER
, src
);
534 ctx
->Driver
.UnmapBuffer(ctx
, GL_COPY_WRITE_BUFFER
, dst
);
540 * Initialize the state associated with buffer objects
543 _mesa_init_buffer_objects( GLcontext
*ctx
)
545 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
546 ctx
->Shared
->NullBufferObj
);
547 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ElementArrayBufferObj
,
548 ctx
->Shared
->NullBufferObj
);
550 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
551 ctx
->Shared
->NullBufferObj
);
552 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
553 ctx
->Shared
->NullBufferObj
);
558 * Bind the specified target to buffer for the specified context.
559 * Called by glBindBuffer() and other functions.
562 bind_buffer_object(GLcontext
*ctx
, GLenum target
, GLuint buffer
)
564 struct gl_buffer_object
*oldBufObj
;
565 struct gl_buffer_object
*newBufObj
= NULL
;
566 struct gl_buffer_object
**bindTarget
= NULL
;
568 bindTarget
= get_buffer_target(ctx
, target
);
570 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)");
574 /* Get pointer to old buffer object (to be unbound) */
575 oldBufObj
= *bindTarget
;
576 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
577 return; /* rebinding the same buffer object- no change */
580 * Get pointer to new buffer object (newBufObj)
583 /* The spec says there's not a buffer object named 0, but we use
584 * one internally because it simplifies things.
586 newBufObj
= ctx
->Shared
->NullBufferObj
;
589 /* non-default buffer object */
590 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
592 /* if this is a new buffer object id, allocate a buffer object now */
593 ASSERT(ctx
->Driver
.NewBufferObject
);
594 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
596 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
599 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
603 /* bind new buffer */
604 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
606 /* Pass BindBuffer call to device driver */
607 if (ctx
->Driver
.BindBuffer
)
608 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
613 * Update the default buffer objects in the given context to reference those
614 * specified in the shared state and release those referencing the old
618 _mesa_update_default_objects_buffer_objects(GLcontext
*ctx
)
620 /* Bind the NullBufferObj to remove references to those
621 * in the shared context hash table.
623 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
624 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
625 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
626 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
631 * When we're about to read pixel data out of a PBO (via glDrawPixels,
632 * glTexImage, etc) or write data into a PBO (via glReadPixels,
633 * glGetTexImage, etc) we call this function to check that we're not
634 * going to read out of bounds.
636 * XXX This would also be a convenient time to check that the PBO isn't
637 * currently mapped. Whoever calls this function should check for that.
638 * Remember, we can't use a PBO when it's mapped!
640 * If we're not using a PBO, this is a no-op.
642 * \param width width of image to read/write
643 * \param height height of image to read/write
644 * \param depth depth of image to read/write
645 * \param format format of image to read/write
646 * \param type datatype of image to read/write
647 * \param ptr the user-provided pointer/offset
648 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
652 _mesa_validate_pbo_access(GLuint dimensions
,
653 const struct gl_pixelstore_attrib
*pack
,
654 GLsizei width
, GLsizei height
, GLsizei depth
,
655 GLenum format
, GLenum type
, const GLvoid
*ptr
)
658 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
660 if (!_mesa_is_bufferobj(pack
->BufferObj
))
661 return GL_TRUE
; /* no PBO, OK */
663 if (pack
->BufferObj
->Size
== 0)
667 /* get address of first pixel we'll read */
668 start
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
669 format
, type
, 0, 0, 0);
671 /* get address just past the last pixel we'll read */
672 end
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
673 format
, type
, depth
-1, height
-1, width
);
676 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
678 if ((const GLubyte
*) start
> sizeAddr
) {
679 /* This will catch negative values / wrap-around */
682 if ((const GLubyte
*) end
> sizeAddr
) {
683 /* Image read goes beyond end of buffer */
693 * For commands that read from a PBO (glDrawPixels, glTexImage,
694 * glPolygonStipple, etc), if we're reading from a PBO, map it read-only
695 * and return the pointer into the PBO. If we're not reading from a
696 * PBO, return \p src as-is.
697 * If non-null return, must call _mesa_unmap_pbo_source() when done.
699 * \return NULL if error, else pointer to start of data
702 _mesa_map_pbo_source(GLcontext
*ctx
,
703 const struct gl_pixelstore_attrib
*unpack
,
708 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
709 /* unpack from PBO */
710 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
716 buf
= ADD_POINTERS(buf
, src
);
719 /* unpack from normal memory */
728 * Combine PBO-read validation and mapping.
729 * If any GL errors are detected, they'll be recorded and NULL returned.
730 * \sa _mesa_validate_pbo_access
731 * \sa _mesa_map_pbo_source
732 * A call to this function should have a matching call to
733 * _mesa_unmap_pbo_source().
736 _mesa_map_validate_pbo_source(GLcontext
*ctx
,
738 const struct gl_pixelstore_attrib
*unpack
,
739 GLsizei width
, GLsizei height
, GLsizei depth
,
740 GLenum format
, GLenum type
, const GLvoid
*ptr
,
743 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
745 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
746 /* non-PBO access: no validation to be done */
750 if (!_mesa_validate_pbo_access(dimensions
, unpack
,
751 width
, height
, depth
, format
, type
, ptr
)) {
752 _mesa_error(ctx
, GL_INVALID_OPERATION
,
753 "%s(out of bounds PBO access)", where
);
757 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
758 /* buffer is already mapped - that's an error */
759 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
763 ptr
= _mesa_map_pbo_source(ctx
, unpack
, ptr
);
769 * Counterpart to _mesa_map_pbo_source()
772 _mesa_unmap_pbo_source(GLcontext
*ctx
,
773 const struct gl_pixelstore_attrib
*unpack
)
775 ASSERT(unpack
!= &ctx
->Pack
); /* catch pack/unpack mismatch */
776 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
777 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
784 * For commands that write to a PBO (glReadPixels, glGetColorTable, etc),
785 * if we're writing to a PBO, map it write-only and return the pointer
786 * into the PBO. If we're not writing to a PBO, return \p dst as-is.
787 * If non-null return, must call _mesa_unmap_pbo_dest() when done.
789 * \return NULL if error, else pointer to start of data
792 _mesa_map_pbo_dest(GLcontext
*ctx
,
793 const struct gl_pixelstore_attrib
*pack
,
798 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
800 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
806 buf
= ADD_POINTERS(buf
, dest
);
809 /* pack to normal memory */
818 * Combine PBO-write validation and mapping.
819 * If any GL errors are detected, they'll be recorded and NULL returned.
820 * \sa _mesa_validate_pbo_access
821 * \sa _mesa_map_pbo_dest
822 * A call to this function should have a matching call to
823 * _mesa_unmap_pbo_dest().
826 _mesa_map_validate_pbo_dest(GLcontext
*ctx
,
828 const struct gl_pixelstore_attrib
*unpack
,
829 GLsizei width
, GLsizei height
, GLsizei depth
,
830 GLenum format
, GLenum type
, GLvoid
*ptr
,
833 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
835 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
836 /* non-PBO access: no validation to be done */
840 if (!_mesa_validate_pbo_access(dimensions
, unpack
,
841 width
, height
, depth
, format
, type
, ptr
)) {
842 _mesa_error(ctx
, GL_INVALID_OPERATION
,
843 "%s(out of bounds PBO access)", where
);
847 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
848 /* buffer is already mapped - that's an error */
849 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
853 ptr
= _mesa_map_pbo_dest(ctx
, unpack
, ptr
);
859 * Counterpart to _mesa_map_pbo_dest()
862 _mesa_unmap_pbo_dest(GLcontext
*ctx
,
863 const struct gl_pixelstore_attrib
*pack
)
865 ASSERT(pack
!= &ctx
->Unpack
); /* catch pack/unpack mismatch */
866 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
867 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
, pack
->BufferObj
);
874 * Return the gl_buffer_object for the given ID.
875 * Always return NULL for ID 0.
877 struct gl_buffer_object
*
878 _mesa_lookup_bufferobj(GLcontext
*ctx
, GLuint buffer
)
883 return (struct gl_buffer_object
*)
884 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
889 * If *ptr points to obj, set ptr = the Null/default buffer object.
890 * This is a helper for buffer object deletion.
891 * The GL spec says that deleting a buffer object causes it to get
892 * unbound from all arrays in the current context.
895 unbind(GLcontext
*ctx
,
896 struct gl_buffer_object
**ptr
,
897 struct gl_buffer_object
*obj
)
900 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
906 * Plug default/fallback buffer object functions into the device
910 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
912 /* GL_ARB_vertex/pixel_buffer_object */
913 driver
->NewBufferObject
= _mesa_new_buffer_object
;
914 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
915 driver
->BindBuffer
= NULL
;
916 driver
->BufferData
= _mesa_buffer_data
;
917 driver
->BufferSubData
= _mesa_buffer_subdata
;
918 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
919 driver
->MapBuffer
= _mesa_buffer_map
;
920 driver
->UnmapBuffer
= _mesa_buffer_unmap
;
922 /* GL_ARB_map_buffer_range */
923 driver
->MapBufferRange
= _mesa_buffer_map_range
;
924 driver
->FlushMappedBufferRange
= _mesa_buffer_flush_mapped_range
;
926 /* GL_ARB_copy_buffer */
927 driver
->CopyBufferSubData
= _mesa_copy_buffer_subdata
;
932 /**********************************************************************/
934 /**********************************************************************/
937 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
939 GET_CURRENT_CONTEXT(ctx
);
940 ASSERT_OUTSIDE_BEGIN_END(ctx
);
942 bind_buffer_object(ctx
, target
, buffer
);
947 * Delete a set of buffer objects.
949 * \param n Number of buffer objects to delete.
950 * \param ids Array of \c n buffer object IDs.
953 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
955 GET_CURRENT_CONTEXT(ctx
);
957 ASSERT_OUTSIDE_BEGIN_END(ctx
);
960 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
964 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
966 for (i
= 0; i
< n
; i
++) {
967 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
969 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
972 ASSERT(bufObj
->Name
== ids
[i
]);
974 if (_mesa_bufferobj_mapped(bufObj
)) {
975 /* if mapped, unmap it now */
976 ctx
->Driver
.UnmapBuffer(ctx
, 0, bufObj
);
977 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
978 bufObj
->Pointer
= NULL
;
981 /* unbind any vertex pointers bound to this buffer */
982 unbind(ctx
, &arrayObj
->Vertex
.BufferObj
, bufObj
);
983 unbind(ctx
, &arrayObj
->Weight
.BufferObj
, bufObj
);
984 unbind(ctx
, &arrayObj
->Normal
.BufferObj
, bufObj
);
985 unbind(ctx
, &arrayObj
->Color
.BufferObj
, bufObj
);
986 unbind(ctx
, &arrayObj
->SecondaryColor
.BufferObj
, bufObj
);
987 unbind(ctx
, &arrayObj
->FogCoord
.BufferObj
, bufObj
);
988 unbind(ctx
, &arrayObj
->Index
.BufferObj
, bufObj
);
989 unbind(ctx
, &arrayObj
->EdgeFlag
.BufferObj
, bufObj
);
990 for (j
= 0; j
< Elements(arrayObj
->TexCoord
); j
++) {
991 unbind(ctx
, &arrayObj
->TexCoord
[j
].BufferObj
, bufObj
);
993 for (j
= 0; j
< Elements(arrayObj
->VertexAttrib
); j
++) {
994 unbind(ctx
, &arrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
997 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
998 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
1000 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
1001 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1004 /* unbind any pixel pack/unpack pointers bound to this buffer */
1005 if (ctx
->Pack
.BufferObj
== bufObj
) {
1006 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1008 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1009 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1012 /* The ID is immediately freed for re-use */
1013 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, bufObj
->Name
);
1014 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1018 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1023 * Generate a set of unique buffer object IDs and store them in \c buffer.
1025 * \param n Number of IDs to generate.
1026 * \param buffer Array of \c n locations to store the IDs.
1029 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
1031 GET_CURRENT_CONTEXT(ctx
);
1034 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1037 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
1046 * This must be atomic (generation and allocation of buffer object IDs)
1048 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1050 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1052 /* Allocate new, empty buffer objects and return identifiers */
1053 for (i
= 0; i
< n
; i
++) {
1054 struct gl_buffer_object
*bufObj
;
1055 GLuint name
= first
+ i
;
1057 bufObj
= ctx
->Driver
.NewBufferObject( ctx
, name
, target
);
1059 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1060 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenBuffersARB");
1063 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
, bufObj
);
1064 buffer
[i
] = first
+ i
;
1067 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1072 * Determine if ID is the name of a buffer object.
1074 * \param id ID of the potential buffer object.
1075 * \return \c GL_TRUE if \c id is the name of a buffer object,
1076 * \c GL_FALSE otherwise.
1078 GLboolean GLAPIENTRY
1079 _mesa_IsBufferARB(GLuint id
)
1081 struct gl_buffer_object
*bufObj
;
1082 GET_CURRENT_CONTEXT(ctx
);
1083 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1085 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
1086 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1087 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
1089 return bufObj
? GL_TRUE
: GL_FALSE
;
1094 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
1095 const GLvoid
* data
, GLenum usage
)
1097 GET_CURRENT_CONTEXT(ctx
);
1098 struct gl_buffer_object
*bufObj
;
1099 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1102 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
1107 case GL_STREAM_DRAW_ARB
:
1108 case GL_STREAM_READ_ARB
:
1109 case GL_STREAM_COPY_ARB
:
1110 case GL_STATIC_DRAW_ARB
:
1111 case GL_STATIC_READ_ARB
:
1112 case GL_STATIC_COPY_ARB
:
1113 case GL_DYNAMIC_DRAW_ARB
:
1114 case GL_DYNAMIC_READ_ARB
:
1115 case GL_DYNAMIC_COPY_ARB
:
1119 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
1123 bufObj
= get_buffer(ctx
, target
);
1125 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
1128 if (!_mesa_is_bufferobj(bufObj
)) {
1129 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer 0)" );
1133 if (_mesa_bufferobj_mapped(bufObj
)) {
1134 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1135 ctx
->Driver
.UnmapBuffer(ctx
, target
, bufObj
);
1136 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1137 ASSERT(bufObj
->Pointer
== NULL
);
1140 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1142 bufObj
->Written
= GL_TRUE
;
1145 _mesa_printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1146 bufObj
->Name
, size
, data
, usage
);
1153 ASSERT(ctx
->Driver
.BufferData
);
1154 if (!ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
)) {
1155 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB()");
1161 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
1162 GLsizeiptrARB size
, const GLvoid
* data
)
1164 GET_CURRENT_CONTEXT(ctx
);
1165 struct gl_buffer_object
*bufObj
;
1166 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1168 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1169 "glBufferSubDataARB" );
1171 /* error already recorded */
1175 bufObj
->Written
= GL_TRUE
;
1177 ASSERT(ctx
->Driver
.BufferSubData
);
1178 ctx
->Driver
.BufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1183 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
1184 GLsizeiptrARB size
, void * data
)
1186 GET_CURRENT_CONTEXT(ctx
);
1187 struct gl_buffer_object
*bufObj
;
1188 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1190 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1191 "glGetBufferSubDataARB" );
1193 /* error already recorded */
1197 ASSERT(ctx
->Driver
.GetBufferSubData
);
1198 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1203 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1205 GET_CURRENT_CONTEXT(ctx
);
1206 struct gl_buffer_object
* bufObj
;
1207 GLbitfield accessFlags
;
1210 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1213 case GL_READ_ONLY_ARB
:
1214 accessFlags
= GL_MAP_READ_BIT
;
1216 case GL_WRITE_ONLY_ARB
:
1217 accessFlags
= GL_MAP_WRITE_BIT
;
1219 case GL_READ_WRITE_ARB
:
1220 accessFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
1223 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1227 bufObj
= get_buffer(ctx
, target
);
1229 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
1232 if (!_mesa_is_bufferobj(bufObj
)) {
1233 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(buffer 0)" );
1236 if (_mesa_bufferobj_mapped(bufObj
)) {
1237 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1241 ASSERT(ctx
->Driver
.MapBuffer
);
1242 map
= ctx
->Driver
.MapBuffer( ctx
, target
, access
, bufObj
);
1244 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1248 /* The driver callback should have set these fields.
1249 * This is important because other modules (like VBO) might call
1250 * the driver function directly.
1252 ASSERT(bufObj
->Pointer
== map
);
1253 ASSERT(bufObj
->Length
== bufObj
->Size
);
1254 ASSERT(bufObj
->Offset
== 0);
1255 bufObj
->AccessFlags
= accessFlags
;
1258 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1259 bufObj
->Written
= GL_TRUE
;
1262 _mesa_printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1263 bufObj
->Name
, bufObj
->Size
, access
);
1264 if (access
== GL_WRITE_ONLY_ARB
) {
1266 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1267 for (i
= 0; i
< bufObj
->Size
; i
++)
1274 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1276 /* buffer is 100 bytes larger than requested, fill with magic value */
1277 for (i
= 0; i
< 100; i
++) {
1278 buf
[bufObj
->Size
- i
- 1] = 123;
1283 return bufObj
->Pointer
;
1287 GLboolean GLAPIENTRY
1288 _mesa_UnmapBufferARB(GLenum target
)
1290 GET_CURRENT_CONTEXT(ctx
);
1291 struct gl_buffer_object
*bufObj
;
1292 GLboolean status
= GL_TRUE
;
1293 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1295 bufObj
= get_buffer(ctx
, target
);
1297 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1300 if (!_mesa_is_bufferobj(bufObj
)) {
1301 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1304 if (!_mesa_bufferobj_mapped(bufObj
)) {
1305 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1310 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1311 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1313 /* check that last 100 bytes are still = magic value */
1314 for (i
= 0; i
< 100; i
++) {
1315 GLuint pos
= bufObj
->Size
- i
- 1;
1316 if (buf
[pos
] != 123) {
1317 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1318 " at position %d (value = %u)\n",
1326 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1327 GLuint i
, unchanged
= 0;
1328 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1330 /* check which bytes changed */
1331 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1332 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1339 _mesa_printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1340 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1345 status
= ctx
->Driver
.UnmapBuffer( ctx
, target
, bufObj
);
1346 bufObj
->AccessFlags
= DEFAULT_ACCESS
;
1347 ASSERT(bufObj
->Pointer
== NULL
);
1348 ASSERT(bufObj
->Offset
== 0);
1349 ASSERT(bufObj
->Length
== 0);
1356 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1358 GET_CURRENT_CONTEXT(ctx
);
1359 struct gl_buffer_object
*bufObj
;
1360 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1362 bufObj
= get_buffer(ctx
, target
);
1364 _mesa_error(ctx
, GL_INVALID_ENUM
, "GetBufferParameterivARB(target)" );
1367 if (!_mesa_is_bufferobj(bufObj
)) {
1368 _mesa_error(ctx
, GL_INVALID_OPERATION
, "GetBufferParameterivARB" );
1373 case GL_BUFFER_SIZE_ARB
:
1374 *params
= (GLint
) bufObj
->Size
;
1376 case GL_BUFFER_USAGE_ARB
:
1377 *params
= bufObj
->Usage
;
1379 case GL_BUFFER_ACCESS_ARB
:
1380 *params
= simplified_access_mode(bufObj
->AccessFlags
);
1382 case GL_BUFFER_MAPPED_ARB
:
1383 *params
= _mesa_bufferobj_mapped(bufObj
);
1386 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname)");
1393 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1395 GET_CURRENT_CONTEXT(ctx
);
1396 struct gl_buffer_object
* bufObj
;
1397 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1399 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1400 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1404 bufObj
= get_buffer(ctx
, target
);
1406 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1409 if (!_mesa_is_bufferobj(bufObj
)) {
1410 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1414 *params
= bufObj
->Pointer
;
1419 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
1420 GLintptr readOffset
, GLintptr writeOffset
,
1423 GET_CURRENT_CONTEXT(ctx
);
1424 struct gl_buffer_object
*src
, *dst
;
1425 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1427 src
= get_buffer(ctx
, readTarget
);
1428 if (!src
|| !_mesa_is_bufferobj(src
)) {
1429 _mesa_error(ctx
, GL_INVALID_ENUM
,
1430 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget
);
1434 dst
= get_buffer(ctx
, writeTarget
);
1435 if (!dst
|| !_mesa_is_bufferobj(dst
)) {
1436 _mesa_error(ctx
, GL_INVALID_ENUM
,
1437 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget
);
1441 if (_mesa_bufferobj_mapped(src
)) {
1442 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1443 "glCopyBuffserSubData(readBuffer is mapped)");
1447 if (_mesa_bufferobj_mapped(dst
)) {
1448 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1449 "glCopyBuffserSubData(writeBuffer is mapped)");
1453 if (readOffset
< 0) {
1454 _mesa_error(ctx
, GL_INVALID_VALUE
,
1455 "glCopyBuffserSubData(readOffset = %d)", readOffset
);
1459 if (writeOffset
< 0) {
1460 _mesa_error(ctx
, GL_INVALID_VALUE
,
1461 "glCopyBuffserSubData(writeOffset = %d)", writeOffset
);
1465 if (readOffset
+ size
> src
->Size
) {
1466 _mesa_error(ctx
, GL_INVALID_VALUE
,
1467 "glCopyBuffserSubData(readOffset + size = %d)",
1472 if (writeOffset
+ size
> dst
->Size
) {
1473 _mesa_error(ctx
, GL_INVALID_VALUE
,
1474 "glCopyBuffserSubData(writeOffset + size = %d)",
1480 if (readOffset
+ size
<= writeOffset
) {
1483 else if (writeOffset
+ size
<= readOffset
) {
1487 /* overlapping src/dst is illegal */
1488 _mesa_error(ctx
, GL_INVALID_VALUE
,
1489 "glCopyBuffserSubData(overlapping src/dst)");
1494 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
1499 * See GL_ARB_map_buffer_range spec
1502 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
1505 GET_CURRENT_CONTEXT(ctx
);
1506 struct gl_buffer_object
*bufObj
;
1509 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1511 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1512 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1513 "glMapBufferRange(extension not supported)");
1518 _mesa_error(ctx
, GL_INVALID_VALUE
,
1519 "glMapBufferRange(offset = %ld)", offset
);
1524 _mesa_error(ctx
, GL_INVALID_VALUE
,
1525 "glMapBufferRange(length = %ld)", length
);
1529 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
1530 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1531 "glMapBufferRange(access indicates neither read or write)");
1535 if (access
& GL_MAP_READ_BIT
) {
1536 if ((access
& GL_MAP_INVALIDATE_RANGE_BIT
) ||
1537 (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) ||
1538 (access
& GL_MAP_UNSYNCHRONIZED_BIT
)) {
1539 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1540 "glMapBufferRange(invalid access flags)");
1545 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
1546 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
1547 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1548 "glMapBufferRange(invalid access flags)");
1552 bufObj
= get_buffer(ctx
, target
);
1553 if (!bufObj
|| !_mesa_is_bufferobj(bufObj
)) {
1554 _mesa_error(ctx
, GL_INVALID_ENUM
,
1555 "glMapBufferRange(target = 0x%x)", target
);
1559 if (offset
+ length
> bufObj
->Size
) {
1560 _mesa_error(ctx
, GL_INVALID_VALUE
,
1561 "glMapBufferRange(offset + length > size)");
1565 if (_mesa_bufferobj_mapped(bufObj
)) {
1566 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1567 "glMapBufferRange(buffer already mapped)");
1571 ASSERT(ctx
->Driver
.MapBufferRange
);
1572 map
= ctx
->Driver
.MapBufferRange(ctx
, target
, offset
, length
,
1575 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(map failed)");
1578 /* The driver callback should have set all these fields.
1579 * This is important because other modules (like VBO) might call
1580 * the driver function directly.
1582 ASSERT(bufObj
->Pointer
== map
);
1583 ASSERT(bufObj
->Length
== length
);
1584 ASSERT(bufObj
->Offset
== offset
);
1585 ASSERT(bufObj
->AccessFlags
== access
);
1593 * See GL_ARB_map_buffer_range spec
1596 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
)
1598 GET_CURRENT_CONTEXT(ctx
);
1599 struct gl_buffer_object
*bufObj
;
1600 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1602 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
1603 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1604 "glMapBufferRange(extension not supported)");
1609 _mesa_error(ctx
, GL_INVALID_VALUE
,
1610 "glMapBufferRange(offset = %ld)", offset
);
1615 _mesa_error(ctx
, GL_INVALID_VALUE
,
1616 "glMapBufferRange(length = %ld)", length
);
1620 bufObj
= get_buffer(ctx
, target
);
1622 _mesa_error(ctx
, GL_INVALID_ENUM
,
1623 "glMapBufferRange(target = 0x%x)", target
);
1627 if (!_mesa_is_bufferobj(bufObj
)) {
1628 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1629 "glMapBufferRange(current buffer is 0)");
1633 if (!_mesa_bufferobj_mapped(bufObj
)) {
1634 /* buffer is not mapped */
1635 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1636 "glMapBufferRange(buffer is not mapped)");
1640 if ((bufObj
->AccessFlags
& GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
1641 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1642 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1646 if (offset
+ length
> bufObj
->Length
) {
1647 _mesa_error(ctx
, GL_INVALID_VALUE
,
1648 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1649 offset
, length
, bufObj
->Length
);
1653 ASSERT(bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
);
1655 if (ctx
->Driver
.FlushMappedBufferRange
)
1656 ctx
->Driver
.FlushMappedBufferRange(ctx
, target
, offset
, length
, bufObj
);