2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 1999-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"
42 #ifdef FEATURE_OES_mapbuffer
43 #define DEFAULT_ACCESS GL_WRITE_ONLY;
45 #define DEFAULT_ACCESS GL_READ_WRITE;
50 * Get the buffer object bound to the specified target in a GL context.
52 * \param ctx GL context
53 * \param target Buffer object target to be retrieved. Currently this must
54 * be either \c GL_ARRAY_BUFFER or \c GL_ELEMENT_ARRAY_BUFFER.
55 * \return A pointer to the buffer object bound to \c target in the
56 * specified context or \c NULL if \c target is invalid.
58 static INLINE
struct gl_buffer_object
*
59 get_buffer(GLcontext
*ctx
, GLenum target
)
61 struct gl_buffer_object
* bufObj
= NULL
;
64 case GL_ARRAY_BUFFER_ARB
:
65 bufObj
= ctx
->Array
.ArrayBufferObj
;
67 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
68 bufObj
= ctx
->Array
.ElementArrayBufferObj
;
70 case GL_PIXEL_PACK_BUFFER_EXT
:
71 bufObj
= ctx
->Pack
.BufferObj
;
73 case GL_PIXEL_UNPACK_BUFFER_EXT
:
74 bufObj
= ctx
->Unpack
.BufferObj
;
77 /* error must be recorded by caller */
81 /* bufObj should point to NullBufferObj or a user-created buffer object */
89 * Tests the subdata range parameters and sets the GL error code for
90 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
92 * \param ctx GL context.
93 * \param target Buffer object target on which to operate.
94 * \param offset Offset of the first byte of the subdata range.
95 * \param size Size, in bytes, of the subdata range.
96 * \param caller Name of calling function for recording errors.
97 * \return A pointer to the buffer object bound to \c target in the
98 * specified context or \c NULL if any of the parameter or state
99 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
102 * \sa glBufferSubDataARB, glGetBufferSubDataARB
104 static struct gl_buffer_object
*
105 buffer_object_subdata_range_good( GLcontext
* ctx
, GLenum target
,
106 GLintptrARB offset
, GLsizeiptrARB size
,
109 struct gl_buffer_object
*bufObj
;
112 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
117 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
121 bufObj
= get_buffer(ctx
, target
);
123 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", caller
);
126 if (bufObj
->Name
== 0) {
127 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
130 if (offset
+ size
> bufObj
->Size
) {
131 _mesa_error(ctx
, GL_INVALID_VALUE
,
132 "%s(size + offset > buffer size)", caller
);
135 if (bufObj
->Pointer
) {
136 /* Buffer is currently mapped */
137 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
146 * Allocate and initialize a new buffer object.
148 * Default callback for the \c dd_function_table::NewBufferObject() hook.
150 struct gl_buffer_object
*
151 _mesa_new_buffer_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
153 struct gl_buffer_object
*obj
;
157 obj
= MALLOC_STRUCT(gl_buffer_object
);
158 _mesa_initialize_buffer_object(obj
, name
, target
);
164 * Delete a buffer object.
166 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
169 _mesa_delete_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*bufObj
)
174 _mesa_free(bufObj
->Data
);
176 /* assign strange values here to help w/ debugging */
177 bufObj
->RefCount
= -1000;
186 * Set ptr to bufObj w/ reference counting.
189 _mesa_reference_buffer_object(GLcontext
*ctx
,
190 struct gl_buffer_object
**ptr
,
191 struct gl_buffer_object
*bufObj
)
197 /* Unreference the old buffer */
198 GLboolean deleteFlag
= GL_FALSE
;
199 struct gl_buffer_object
*oldObj
= *ptr
;
201 /*_glthread_LOCK_MUTEX(oldObj->Mutex);*/
202 ASSERT(oldObj
->RefCount
> 0);
205 printf("BufferObj %p %d DECR to %d\n",
206 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
208 deleteFlag
= (oldObj
->RefCount
== 0);
209 /*_glthread_UNLOCK_MUTEX(oldObj->Mutex);*/
213 /* some sanity checking: don't delete a buffer still in use */
215 /* unfortunately, these tests are invalid during context tear-down */
216 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
217 ASSERT(ctx
->Array
.ElementArrayBufferObj
!= bufObj
);
218 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
221 ASSERT(ctx
->Driver
.DeleteBuffer
);
222 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
230 /* reference new buffer */
231 /*_glthread_LOCK_MUTEX(tex->Mutex);*/
232 if (bufObj
->RefCount
== 0) {
233 /* this buffer's being deleted (look just above) */
234 /* Not sure this can every really happen. Warn if it does. */
235 _mesa_problem(NULL
, "referencing deleted buffer object");
241 printf("BufferObj %p %d INCR to %d\n",
242 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
246 /*_glthread_UNLOCK_MUTEX(tex->Mutex);*/
252 * Initialize a buffer object to default values.
255 _mesa_initialize_buffer_object( struct gl_buffer_object
*obj
,
256 GLuint name
, GLenum target
)
260 _mesa_bzero(obj
, sizeof(struct gl_buffer_object
));
263 obj
->Usage
= GL_STATIC_DRAW_ARB
;
264 obj
->Access
= DEFAULT_ACCESS
;
269 * Allocate space for and store data in a buffer object. Any data that was
270 * previously stored in the buffer object is lost. If \c data is \c NULL,
271 * memory will be allocated, but no copy will occur.
273 * This is the default callback for \c dd_function_table::BufferData()
274 * Note that all GL error checking will have been done already.
276 * \param ctx GL context.
277 * \param target Buffer object target on which to operate.
278 * \param size Size, in bytes, of the new data store.
279 * \param data Pointer to the data to store in the buffer object. This
280 * pointer may be \c NULL.
281 * \param usage Hints about how the data will be used.
282 * \param bufObj Object to be used.
284 * \sa glBufferDataARB, dd_function_table::BufferData.
287 _mesa_buffer_data( GLcontext
*ctx
, GLenum target
, GLsizeiptrARB size
,
288 const GLvoid
* data
, GLenum usage
,
289 struct gl_buffer_object
* bufObj
)
293 (void) ctx
; (void) target
;
295 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
297 bufObj
->Data
= (GLubyte
*) new_data
;
299 bufObj
->Usage
= usage
;
302 _mesa_memcpy( bufObj
->Data
, data
, size
);
309 * Replace data in a subrange of buffer object. If the data range
310 * specified by \c size + \c offset extends beyond the end of the buffer or
311 * if \c data is \c NULL, no copy is performed.
313 * This is the default callback for \c dd_function_table::BufferSubData()
314 * Note that all GL error checking will have been done already.
316 * \param ctx GL context.
317 * \param target Buffer object target on which to operate.
318 * \param offset Offset of the first byte to be modified.
319 * \param size Size, in bytes, of the data range.
320 * \param data Pointer to the data to store in the buffer object.
321 * \param bufObj Object to be used.
323 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
326 _mesa_buffer_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
327 GLsizeiptrARB size
, const GLvoid
* data
,
328 struct gl_buffer_object
* bufObj
)
330 (void) ctx
; (void) target
;
332 /* this should have been caught in _mesa_BufferSubData() */
333 ASSERT(size
+ offset
<= bufObj
->Size
);
336 _mesa_memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
342 * Retrieve data from a subrange of buffer object. If the data range
343 * specified by \c size + \c offset extends beyond the end of the buffer or
344 * if \c data is \c NULL, no copy is performed.
346 * This is the default callback for \c dd_function_table::GetBufferSubData()
347 * Note that all GL error checking will have been done already.
349 * \param ctx GL context.
350 * \param target Buffer object target on which to operate.
351 * \param offset Offset of the first byte to be fetched.
352 * \param size Size, in bytes, of the data range.
353 * \param data Destination for data
354 * \param bufObj Object to be used.
356 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
359 _mesa_buffer_get_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
360 GLsizeiptrARB size
, GLvoid
* data
,
361 struct gl_buffer_object
* bufObj
)
363 (void) ctx
; (void) target
;
365 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
366 _mesa_memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
372 * Default callback for \c dd_function_tabel::MapBuffer().
374 * The function parameters will have been already tested for errors.
376 * \param ctx GL context.
377 * \param target Buffer object target on which to operate.
378 * \param access Information about how the buffer will be accessed.
379 * \param bufObj Object to be mapped.
380 * \return A pointer to the object's internal data store that can be accessed
383 * \sa glMapBufferARB, dd_function_table::MapBuffer
386 _mesa_buffer_map( GLcontext
*ctx
, GLenum target
, GLenum access
,
387 struct gl_buffer_object
*bufObj
)
392 /* Just return a direct pointer to the data */
393 if (bufObj
->Pointer
) {
394 /* already mapped! */
397 bufObj
->Pointer
= bufObj
->Data
;
398 return bufObj
->Pointer
;
403 * Default callback for \c dd_function_table::MapBuffer().
405 * The input parameters will have been already tested for errors.
407 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
410 _mesa_buffer_unmap( GLcontext
*ctx
, GLenum target
,
411 struct gl_buffer_object
*bufObj
)
415 /* XXX we might assert here that bufObj->Pointer is non-null */
416 bufObj
->Pointer
= NULL
;
422 * Initialize the state associated with buffer objects
425 _mesa_init_buffer_objects( GLcontext
*ctx
)
427 /* Allocate the default buffer object and set refcount so high that
428 * it never gets deleted.
429 * XXX with recent/improved refcounting this may not longer be needed.
431 ctx
->Array
.NullBufferObj
= _mesa_new_buffer_object(ctx
, 0, 0);
432 if (ctx
->Array
.NullBufferObj
)
433 ctx
->Array
.NullBufferObj
->RefCount
= 1000;
435 ctx
->Array
.ArrayBufferObj
= ctx
->Array
.NullBufferObj
;
436 ctx
->Array
.ElementArrayBufferObj
= ctx
->Array
.NullBufferObj
;
441 * Bind the specified target to buffer for the specified context.
444 bind_buffer_object(GLcontext
*ctx
, GLenum target
, GLuint buffer
)
446 struct gl_buffer_object
*oldBufObj
;
447 struct gl_buffer_object
*newBufObj
= NULL
;
448 struct gl_buffer_object
**bindTarget
= NULL
;
451 case GL_ARRAY_BUFFER_ARB
:
452 bindTarget
= &ctx
->Array
.ArrayBufferObj
;
454 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
455 bindTarget
= &ctx
->Array
.ElementArrayBufferObj
;
457 case GL_PIXEL_PACK_BUFFER_EXT
:
458 bindTarget
= &ctx
->Pack
.BufferObj
;
460 case GL_PIXEL_UNPACK_BUFFER_EXT
:
461 bindTarget
= &ctx
->Unpack
.BufferObj
;
464 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target)");
468 /* Get pointer to old buffer object (to be unbound) */
469 oldBufObj
= get_buffer(ctx
, target
);
470 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
471 return; /* rebinding the same buffer object- no change */
474 * Get pointer to new buffer object (newBufObj)
477 /* The spec says there's not a buffer object named 0, but we use
478 * one internally because it simplifies things.
480 newBufObj
= ctx
->Array
.NullBufferObj
;
483 /* non-default buffer object */
484 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
486 /* if this is a new buffer object id, allocate a buffer object now */
487 ASSERT(ctx
->Driver
.NewBufferObject
);
488 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
490 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
493 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
497 /* bind new buffer */
498 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
500 /* Pass BindBuffer call to device driver */
501 if (ctx
->Driver
.BindBuffer
&& newBufObj
)
502 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
507 * Update the default buffer objects in the given context to reference those
508 * specified in the shared state and release those referencing the old
512 _mesa_update_default_objects_buffer_objects(GLcontext
*ctx
)
514 /* Bind the NullBufferObj to remove references to those
515 * in the shared context hash table.
517 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
518 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
519 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
520 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
525 * When we're about to read pixel data out of a PBO (via glDrawPixels,
526 * glTexImage, etc) or write data into a PBO (via glReadPixels,
527 * glGetTexImage, etc) we call this function to check that we're not
528 * going to read out of bounds.
530 * XXX This would also be a convenient time to check that the PBO isn't
531 * currently mapped. Whoever calls this function should check for that.
532 * Remember, we can't use a PBO when it's mapped!
534 * \param width width of image to read/write
535 * \param height height of image to read/write
536 * \param depth depth of image to read/write
537 * \param format format of image to read/write
538 * \param type datatype of image to read/write
539 * \param ptr the user-provided pointer/offset
540 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
544 _mesa_validate_pbo_access(GLuint dimensions
,
545 const struct gl_pixelstore_attrib
*pack
,
546 GLsizei width
, GLsizei height
, GLsizei depth
,
547 GLenum format
, GLenum type
, const GLvoid
*ptr
)
550 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
552 ASSERT(pack
->BufferObj
->Name
!= 0);
554 if (pack
->BufferObj
->Size
== 0)
558 /* get address of first pixel we'll read */
559 start
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
560 format
, type
, 0, 0, 0);
562 /* get address just past the last pixel we'll read */
563 end
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
564 format
, type
, depth
-1, height
-1, width
);
567 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
569 if ((const GLubyte
*) start
> sizeAddr
) {
570 /* This will catch negative values / wrap-around */
573 if ((const GLubyte
*) end
> sizeAddr
) {
574 /* Image read goes beyond end of buffer */
584 * If the source of glBitmap data is a PBO, check that we won't read out
585 * of buffer bounds, then map the buffer.
586 * If not sourcing from a PBO, just return the bitmap pointer.
587 * This is a helper function for (some) drivers.
588 * Return NULL if error.
589 * If non-null return, must call _mesa_unmap_bitmap_pbo() when done.
592 _mesa_map_bitmap_pbo(GLcontext
*ctx
,
593 const struct gl_pixelstore_attrib
*unpack
,
594 const GLubyte
*bitmap
)
598 if (unpack
->BufferObj
->Name
) {
599 /* unpack from PBO */
600 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
606 buf
= ADD_POINTERS(buf
, bitmap
);
609 /* unpack from normal memory */
618 * Counterpart to _mesa_map_bitmap_pbo()
619 * This is a helper function for (some) drivers.
622 _mesa_unmap_bitmap_pbo(GLcontext
*ctx
,
623 const struct gl_pixelstore_attrib
*unpack
)
625 if (unpack
->BufferObj
->Name
) {
626 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
633 * \sa _mesa_map_bitmap_pbo
636 _mesa_map_drawpix_pbo(GLcontext
*ctx
,
637 const struct gl_pixelstore_attrib
*unpack
,
638 const GLvoid
*pixels
)
642 if (unpack
->BufferObj
->Name
) {
643 /* unpack from PBO */
644 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
650 buf
= ADD_POINTERS(buf
, pixels
);
653 /* unpack from normal memory */
662 * \sa _mesa_unmap_bitmap_pbo
665 _mesa_unmap_drawpix_pbo(GLcontext
*ctx
,
666 const struct gl_pixelstore_attrib
*unpack
)
668 if (unpack
->BufferObj
->Name
) {
669 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
676 * If PBO is bound, map the buffer, return dest pointer in mapped buffer.
677 * Call _mesa_unmap_readpix_pbo() when finished
678 * \return NULL if error
681 _mesa_map_readpix_pbo(GLcontext
*ctx
,
682 const struct gl_pixelstore_attrib
*pack
,
687 if (pack
->BufferObj
->Name
) {
689 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
695 buf
= ADD_POINTERS(buf
, dest
);
698 /* pack to normal memory */
707 * Counterpart to _mesa_map_readpix_pbo()
710 _mesa_unmap_readpix_pbo(GLcontext
*ctx
,
711 const struct gl_pixelstore_attrib
*pack
)
713 if (pack
->BufferObj
->Name
) {
714 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
, pack
->BufferObj
);
721 * Return the gl_buffer_object for the given ID.
722 * Always return NULL for ID 0.
724 struct gl_buffer_object
*
725 _mesa_lookup_bufferobj(GLcontext
*ctx
, GLuint buffer
)
730 return (struct gl_buffer_object
*)
731 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
736 * If *ptr points to obj, set ptr = the Null/default buffer object.
737 * This is a helper for buffer object deletion.
738 * The GL spec says that deleting a buffer object causes it to get
739 * unbound from all arrays in the current context.
742 unbind(GLcontext
*ctx
,
743 struct gl_buffer_object
**ptr
,
744 struct gl_buffer_object
*obj
)
747 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Array
.NullBufferObj
);
753 /**********************************************************************/
755 /**********************************************************************/
758 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
760 GET_CURRENT_CONTEXT(ctx
);
761 ASSERT_OUTSIDE_BEGIN_END(ctx
);
763 bind_buffer_object(ctx
, target
, buffer
);
768 * Delete a set of buffer objects.
770 * \param n Number of buffer objects to delete.
771 * \param ids Array of \c n buffer object IDs.
774 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
776 GET_CURRENT_CONTEXT(ctx
);
778 ASSERT_OUTSIDE_BEGIN_END(ctx
);
781 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
785 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
787 for (i
= 0; i
< n
; i
++) {
788 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
792 ASSERT(bufObj
->Name
== ids
[i
]);
794 if (bufObj
->Pointer
) {
795 /* if mapped, unmap it now */
796 ctx
->Driver
.UnmapBuffer(ctx
, 0, bufObj
);
797 bufObj
->Access
= DEFAULT_ACCESS
;
798 bufObj
->Pointer
= NULL
;
801 /* unbind any vertex pointers bound to this buffer */
802 unbind(ctx
, &ctx
->Array
.ArrayObj
->Vertex
.BufferObj
, bufObj
);
803 unbind(ctx
, &ctx
->Array
.ArrayObj
->Normal
.BufferObj
, bufObj
);
804 unbind(ctx
, &ctx
->Array
.ArrayObj
->Color
.BufferObj
, bufObj
);
805 unbind(ctx
, &ctx
->Array
.ArrayObj
->SecondaryColor
.BufferObj
, bufObj
);
806 unbind(ctx
, &ctx
->Array
.ArrayObj
->FogCoord
.BufferObj
, bufObj
);
807 unbind(ctx
, &ctx
->Array
.ArrayObj
->Index
.BufferObj
, bufObj
);
808 unbind(ctx
, &ctx
->Array
.ArrayObj
->EdgeFlag
.BufferObj
, bufObj
);
809 for (j
= 0; j
< MAX_TEXTURE_COORD_UNITS
; j
++) {
810 unbind(ctx
, &ctx
->Array
.ArrayObj
->TexCoord
[j
].BufferObj
, bufObj
);
812 for (j
= 0; j
< VERT_ATTRIB_MAX
; j
++) {
813 unbind(ctx
, &ctx
->Array
.ArrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
816 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
817 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
819 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
820 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
823 /* unbind any pixel pack/unpack pointers bound to this buffer */
824 if (ctx
->Pack
.BufferObj
== bufObj
) {
825 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
827 if (ctx
->Unpack
.BufferObj
== bufObj
) {
828 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
831 /* The ID is immediately freed for re-use */
832 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, bufObj
->Name
);
833 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
837 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
842 * Generate a set of unique buffer object IDs and store them in \c buffer.
844 * \param n Number of IDs to generate.
845 * \param buffer Array of \c n locations to store the IDs.
848 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
850 GET_CURRENT_CONTEXT(ctx
);
853 ASSERT_OUTSIDE_BEGIN_END(ctx
);
856 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
865 * This must be atomic (generation and allocation of buffer object IDs)
867 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
869 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
871 /* Allocate new, empty buffer objects and return identifiers */
872 for (i
= 0; i
< n
; i
++) {
873 struct gl_buffer_object
*bufObj
;
874 GLuint name
= first
+ i
;
876 bufObj
= ctx
->Driver
.NewBufferObject( ctx
, name
, target
);
878 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
879 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenBuffersARB");
882 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
, bufObj
);
883 buffer
[i
] = first
+ i
;
886 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
891 * Determine if ID is the name of a buffer object.
893 * \param id ID of the potential buffer object.
894 * \return \c GL_TRUE if \c id is the name of a buffer object,
895 * \c GL_FALSE otherwise.
898 _mesa_IsBufferARB(GLuint id
)
900 struct gl_buffer_object
*bufObj
;
901 GET_CURRENT_CONTEXT(ctx
);
902 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
904 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
905 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
906 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
908 return bufObj
? GL_TRUE
: GL_FALSE
;
913 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
914 const GLvoid
* data
, GLenum usage
)
916 GET_CURRENT_CONTEXT(ctx
);
917 struct gl_buffer_object
*bufObj
;
918 ASSERT_OUTSIDE_BEGIN_END(ctx
);
921 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
926 case GL_STREAM_DRAW_ARB
:
927 case GL_STREAM_READ_ARB
:
928 case GL_STREAM_COPY_ARB
:
929 case GL_STATIC_DRAW_ARB
:
930 case GL_STATIC_READ_ARB
:
931 case GL_STATIC_COPY_ARB
:
932 case GL_DYNAMIC_DRAW_ARB
:
933 case GL_DYNAMIC_READ_ARB
:
934 case GL_DYNAMIC_COPY_ARB
:
938 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
942 bufObj
= get_buffer(ctx
, target
);
944 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
947 if (bufObj
->Name
== 0) {
948 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB" );
952 if (bufObj
->Pointer
) {
953 /* Unmap the existing buffer. We'll replace it now. Not an error. */
954 ctx
->Driver
.UnmapBuffer(ctx
, target
, bufObj
);
955 bufObj
->Access
= DEFAULT_ACCESS
;
956 bufObj
->Pointer
= NULL
;
959 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
961 ASSERT(ctx
->Driver
.BufferData
);
963 bufObj
->Written
= GL_TRUE
;
965 /* Give the buffer object to the driver! <data> may be null! */
966 ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
);
971 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
972 GLsizeiptrARB size
, const GLvoid
* data
)
974 GET_CURRENT_CONTEXT(ctx
);
975 struct gl_buffer_object
*bufObj
;
976 ASSERT_OUTSIDE_BEGIN_END(ctx
);
978 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
979 "glBufferSubDataARB" );
981 /* error already recorded */
985 bufObj
->Written
= GL_TRUE
;
987 ASSERT(ctx
->Driver
.BufferSubData
);
988 ctx
->Driver
.BufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
993 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
994 GLsizeiptrARB size
, void * data
)
996 GET_CURRENT_CONTEXT(ctx
);
997 struct gl_buffer_object
*bufObj
;
998 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1000 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
1001 "glGetBufferSubDataARB" );
1003 /* error already recorded */
1007 ASSERT(ctx
->Driver
.GetBufferSubData
);
1008 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1013 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1015 GET_CURRENT_CONTEXT(ctx
);
1016 struct gl_buffer_object
* bufObj
;
1017 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1020 case GL_READ_ONLY_ARB
:
1021 case GL_WRITE_ONLY_ARB
:
1022 case GL_READ_WRITE_ARB
:
1026 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1030 bufObj
= get_buffer(ctx
, target
);
1032 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
1035 if (bufObj
->Name
== 0) {
1036 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB" );
1039 if (bufObj
->Pointer
) {
1040 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1044 ASSERT(ctx
->Driver
.MapBuffer
);
1045 bufObj
->Pointer
= ctx
->Driver
.MapBuffer( ctx
, target
, access
, bufObj
);
1046 if (!bufObj
->Pointer
) {
1047 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(access)");
1050 bufObj
->Access
= access
;
1051 if (access
== GL_WRITE_ONLY_ARB
|| access
== GL_READ_WRITE_ARB
)
1052 bufObj
->Written
= GL_TRUE
;
1054 return bufObj
->Pointer
;
1058 GLboolean GLAPIENTRY
1059 _mesa_UnmapBufferARB(GLenum target
)
1061 GET_CURRENT_CONTEXT(ctx
);
1062 struct gl_buffer_object
*bufObj
;
1063 GLboolean status
= GL_TRUE
;
1064 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1066 bufObj
= get_buffer(ctx
, target
);
1068 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1071 if (bufObj
->Name
== 0) {
1072 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1075 if (!bufObj
->Pointer
) {
1076 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1080 status
= ctx
->Driver
.UnmapBuffer( ctx
, target
, bufObj
);
1081 bufObj
->Access
= DEFAULT_ACCESS
;
1082 bufObj
->Pointer
= NULL
;
1089 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1091 GET_CURRENT_CONTEXT(ctx
);
1092 struct gl_buffer_object
*bufObj
;
1093 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1095 bufObj
= get_buffer(ctx
, target
);
1097 _mesa_error(ctx
, GL_INVALID_ENUM
, "GetBufferParameterivARB(target)" );
1100 if (bufObj
->Name
== 0) {
1101 _mesa_error(ctx
, GL_INVALID_OPERATION
, "GetBufferParameterivARB" );
1106 case GL_BUFFER_SIZE_ARB
:
1107 *params
= (GLint
) bufObj
->Size
;
1109 case GL_BUFFER_USAGE_ARB
:
1110 *params
= bufObj
->Usage
;
1112 case GL_BUFFER_ACCESS_ARB
:
1113 *params
= bufObj
->Access
;
1115 case GL_BUFFER_MAPPED_ARB
:
1116 *params
= (bufObj
->Pointer
!= NULL
);
1119 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname)");
1126 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1128 GET_CURRENT_CONTEXT(ctx
);
1129 struct gl_buffer_object
* bufObj
;
1130 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1132 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1133 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1137 bufObj
= get_buffer(ctx
, target
);
1139 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1142 if (bufObj
->Name
== 0) {
1143 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1147 *params
= bufObj
->Pointer
;