2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * \brief Functions for the GL_ARB_vertex_buffer_object extension.
29 * \author Brian Paul, Ian Romanick
38 #include "bufferobj.h"
41 #ifdef FEATURE_OES_mapbuffer
42 #define DEFAULT_ACCESS GL_WRITE_ONLY;
44 #define DEFAULT_ACCESS GL_READ_WRITE;
49 * Get the buffer object bound to the specified target in a GL context.
51 * \param ctx GL context
52 * \param target Buffer object target to be retrieved. Currently this must
53 * be either \c GL_ARRAY_BUFFER or \c GL_ELEMENT_ARRAY_BUFFER.
54 * \return A pointer to the buffer object bound to \c target in the
55 * specified context or \c NULL if \c target is invalid.
57 static INLINE
struct gl_buffer_object
*
58 get_buffer(GLcontext
*ctx
, GLenum target
)
60 struct gl_buffer_object
* bufObj
= NULL
;
63 case GL_ARRAY_BUFFER_ARB
:
64 bufObj
= ctx
->Array
.ArrayBufferObj
;
66 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
67 bufObj
= ctx
->Array
.ElementArrayBufferObj
;
69 case GL_PIXEL_PACK_BUFFER_EXT
:
70 bufObj
= ctx
->Pack
.BufferObj
;
72 case GL_PIXEL_UNPACK_BUFFER_EXT
:
73 bufObj
= ctx
->Unpack
.BufferObj
;
76 /* error must be recorded by caller */
80 /* bufObj should point to NullBufferObj or a user-created buffer object */
88 * Tests the subdata range parameters and sets the GL error code for
89 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
91 * \param ctx GL context.
92 * \param target Buffer object target on which to operate.
93 * \param offset Offset of the first byte of the subdata range.
94 * \param size Size, in bytes, of the subdata range.
95 * \param caller Name of calling function for recording errors.
96 * \return A pointer to the buffer object bound to \c target in the
97 * specified context or \c NULL if any of the parameter or state
98 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
101 * \sa glBufferSubDataARB, glGetBufferSubDataARB
103 static struct gl_buffer_object
*
104 buffer_object_subdata_range_good( GLcontext
* ctx
, GLenum target
,
105 GLintptrARB offset
, GLsizeiptrARB size
,
108 struct gl_buffer_object
*bufObj
;
111 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
116 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
120 bufObj
= get_buffer(ctx
, target
);
122 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", caller
);
125 if (bufObj
->Name
== 0) {
126 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
129 if (offset
+ size
> bufObj
->Size
) {
130 _mesa_error(ctx
, GL_INVALID_VALUE
,
131 "%s(size + offset > buffer size)", caller
);
134 if (bufObj
->Pointer
) {
135 /* Buffer is currently mapped */
136 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
145 * Allocate and initialize a new buffer object.
147 * This function is intended to be called via
148 * \c dd_function_table::NewBufferObject.
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 * This function is intended to be called via
167 * \c dd_function_table::DeleteBuffer.
170 _mesa_delete_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*bufObj
)
175 _mesa_free(bufObj
->Data
);
177 /* assign strange values here to help w/ debugging */
178 bufObj
->RefCount
= -1000;
187 * Set ptr to bufObj w/ reference counting.
190 _mesa_reference_buffer_object(GLcontext
*ctx
,
191 struct gl_buffer_object
**ptr
,
192 struct gl_buffer_object
*bufObj
)
198 /* Unreference the old texture */
199 GLboolean deleteFlag
= GL_FALSE
;
200 struct gl_buffer_object
*oldObj
= *ptr
;
202 /*_glthread_LOCK_MUTEX(oldObj->Mutex);*/
203 ASSERT(oldObj
->RefCount
> 0);
206 printf("BufferObj %p %d DECR to %d\n",
207 (void *) oldObj
, oldObj
->Name
, oldObj
->RefCount
);
209 deleteFlag
= (oldObj
->RefCount
== 0);
210 /*_glthread_UNLOCK_MUTEX(oldObj->Mutex);*/
214 /* some sanity checking: don't delete a buffer still in use */
216 /* unfortunately, these tests are invalid during context tear-down */
217 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
218 ASSERT(ctx
->Array
.ElementArrayBufferObj
!= bufObj
);
219 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
222 ASSERT(ctx
->Driver
.DeleteBuffer
);
223 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
231 /* reference new texture */
232 /*_glthread_LOCK_MUTEX(tex->Mutex);*/
233 if (bufObj
->RefCount
== 0) {
234 /* this buffer's being deleted (look just above) */
235 /* Not sure this can every really happen. Warn if it does. */
236 _mesa_problem(NULL
, "referencing deleted buffer object");
242 printf("BufferObj %p %d INCR to %d\n",
243 (void *) bufObj
, bufObj
->Name
, bufObj
->RefCount
);
247 /*_glthread_UNLOCK_MUTEX(tex->Mutex);*/
253 * Initialize a buffer object to default values.
256 _mesa_initialize_buffer_object( struct gl_buffer_object
*obj
,
257 GLuint name
, GLenum target
)
261 _mesa_bzero(obj
, sizeof(struct gl_buffer_object
));
264 obj
->Usage
= GL_STATIC_DRAW_ARB
;
265 obj
->Access
= DEFAULT_ACCESS
;
270 * Allocate space for and store data in a buffer object. Any data that was
271 * previously stored in the buffer object is lost. If \c data is \c NULL,
272 * memory will be allocated, but no copy will occur.
274 * This function is intended to be called via
275 * \c dd_function_table::BufferData. This function need not set GL error
276 * codes. The input parameters will have been tested before calling.
278 * \param ctx GL context.
279 * \param target Buffer object target on which to operate.
280 * \param size Size, in bytes, of the new data store.
281 * \param data Pointer to the data to store in the buffer object. This
282 * pointer may be \c NULL.
283 * \param usage Hints about how the data will be used.
284 * \param bufObj Object to be used.
286 * \sa glBufferDataARB, dd_function_table::BufferData.
289 _mesa_buffer_data( GLcontext
*ctx
, GLenum target
, GLsizeiptrARB size
,
290 const GLvoid
* data
, GLenum usage
,
291 struct gl_buffer_object
* bufObj
)
295 (void) ctx
; (void) target
;
297 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
299 bufObj
->Data
= (GLubyte
*) new_data
;
301 bufObj
->Usage
= usage
;
304 _mesa_memcpy( bufObj
->Data
, data
, size
);
311 * Replace data in a subrange of buffer object. If the data range
312 * specified by \c size + \c offset extends beyond the end of the buffer or
313 * if \c data is \c NULL, no copy is performed.
315 * This function is intended to be called by
316 * \c dd_function_table::BufferSubData. This function need not set GL error
317 * codes. The input parameters will have been tested before calling.
319 * \param ctx GL context.
320 * \param target Buffer object target on which to operate.
321 * \param offset Offset of the first byte to be modified.
322 * \param size Size, in bytes, of the data range.
323 * \param data Pointer to the data to store in the buffer object.
324 * \param bufObj Object to be used.
326 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
329 _mesa_buffer_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
330 GLsizeiptrARB size
, const GLvoid
* data
,
331 struct gl_buffer_object
* bufObj
)
333 (void) ctx
; (void) target
;
335 /* this should have been caught in _mesa_BufferSubData() */
336 ASSERT(size
+ offset
<= bufObj
->Size
);
339 _mesa_memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
345 * Retrieve data from a subrange of buffer object. If the data range
346 * specified by \c size + \c offset extends beyond the end of the buffer or
347 * if \c data is \c NULL, no copy is performed.
349 * This function is intended to be called by
350 * \c dd_function_table::BufferGetSubData. This function need not set GL error
351 * codes. The input parameters will have been tested before calling.
353 * \param ctx GL context.
354 * \param target Buffer object target on which to operate.
355 * \param offset Offset of the first byte to be modified.
356 * \param size Size, in bytes, of the data range.
357 * \param data Pointer to the data to store in the buffer object.
358 * \param bufObj Object to be used.
360 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
363 _mesa_buffer_get_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
364 GLsizeiptrARB size
, GLvoid
* data
,
365 struct gl_buffer_object
* bufObj
)
367 (void) ctx
; (void) target
;
369 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
370 _mesa_memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
376 * Fallback function called via ctx->Driver.MapBuffer().
377 * Hardware drivers that really implement buffer objects should never use
380 * The function parameters will have been already tested for errors.
382 * \param ctx GL context.
383 * \param target Buffer object target on which to operate.
384 * \param access Information about how the buffer will be accessed.
385 * \param bufObj Object to be mapped.
386 * \return A pointer to the object's internal data store that can be accessed
389 * \sa glMapBufferARB, dd_function_table::MapBuffer
392 _mesa_buffer_map( GLcontext
*ctx
, GLenum target
, GLenum access
,
393 struct gl_buffer_object
*bufObj
)
398 ASSERT(!bufObj
->OnCard
);
399 /* Just return a direct pointer to the data */
400 if (bufObj
->Pointer
) {
401 /* already mapped! */
404 bufObj
->Pointer
= bufObj
->Data
;
405 return bufObj
->Pointer
;
410 * Fallback function called via ctx->Driver.MapBuffer().
411 * Hardware drivers that really implement buffer objects should never use
414 * The input parameters will have been already tested for errors.
416 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
419 _mesa_buffer_unmap( GLcontext
*ctx
, GLenum target
,
420 struct gl_buffer_object
*bufObj
)
424 ASSERT(!bufObj
->OnCard
);
425 /* XXX we might assert here that bufObj->Pointer is non-null */
426 bufObj
->Pointer
= NULL
;
432 * Initialize the state associated with buffer objects
435 _mesa_init_buffer_objects( GLcontext
*ctx
)
437 /* Allocate the default buffer object and set refcount so high that
438 * it never gets deleted.
439 * XXX with recent/improved refcounting this may not longer be needed.
441 ctx
->Array
.NullBufferObj
= _mesa_new_buffer_object(ctx
, 0, 0);
442 if (ctx
->Array
.NullBufferObj
)
443 ctx
->Array
.NullBufferObj
->RefCount
= 1000;
445 ctx
->Array
.ArrayBufferObj
= ctx
->Array
.NullBufferObj
;
446 ctx
->Array
.ElementArrayBufferObj
= ctx
->Array
.NullBufferObj
;
450 * Bind the specified target to buffer for the specified context.
453 bind_buffer_object(GLcontext
*ctx
, GLenum target
, GLuint buffer
)
455 struct gl_buffer_object
*oldBufObj
;
456 struct gl_buffer_object
*newBufObj
= NULL
;
457 struct gl_buffer_object
**bindTarget
= NULL
;
460 case GL_ARRAY_BUFFER_ARB
:
461 bindTarget
= &ctx
->Array
.ArrayBufferObj
;
463 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
464 bindTarget
= &ctx
->Array
.ElementArrayBufferObj
;
466 case GL_PIXEL_PACK_BUFFER_EXT
:
467 bindTarget
= &ctx
->Pack
.BufferObj
;
469 case GL_PIXEL_UNPACK_BUFFER_EXT
:
470 bindTarget
= &ctx
->Unpack
.BufferObj
;
473 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target)");
477 /* Get pointer to old buffer object (to be unbound) */
478 oldBufObj
= get_buffer(ctx
, target
);
479 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
480 return; /* rebinding the same buffer object- no change */
483 * Get pointer to new buffer object (newBufObj)
486 /* The spec says there's not a buffer object named 0, but we use
487 * one internally because it simplifies things.
489 newBufObj
= ctx
->Array
.NullBufferObj
;
492 /* non-default buffer object */
493 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
495 /* if this is a new buffer object id, allocate a buffer object now */
496 ASSERT(ctx
->Driver
.NewBufferObject
);
497 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
499 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
502 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, newBufObj
);
506 /* bind new buffer */
507 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
509 /* Pass BindBuffer call to device driver */
510 if (ctx
->Driver
.BindBuffer
&& newBufObj
)
511 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
516 * Update the default buffer objects in the given context to reference those
517 * specified in the shared state and release those referencing the old
521 _mesa_update_default_objects_buffer_objects(GLcontext
*ctx
)
523 /* Bind the NullBufferObj to remove references to those
524 * in the shared context hash table.
526 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
527 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
528 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
529 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
534 * When we're about to read pixel data out of a PBO (via glDrawPixels,
535 * glTexImage, etc) or write data into a PBO (via glReadPixels,
536 * glGetTexImage, etc) we call this function to check that we're not
537 * going to read out of bounds.
539 * XXX This would also be a convenient time to check that the PBO isn't
540 * currently mapped. Whoever calls this function should check for that.
541 * Remember, we can't use a PBO when it's mapped!
543 * \param width width of image to read/write
544 * \param height height of image to read/write
545 * \param depth depth of image to read/write
546 * \param format format of image to read/write
547 * \param type datatype of image to read/write
548 * \param ptr the user-provided pointer/offset
549 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
553 _mesa_validate_pbo_access(GLuint dimensions
,
554 const struct gl_pixelstore_attrib
*pack
,
555 GLsizei width
, GLsizei height
, GLsizei depth
,
556 GLenum format
, GLenum type
, const GLvoid
*ptr
)
559 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
561 ASSERT(pack
->BufferObj
->Name
!= 0);
563 if (pack
->BufferObj
->Size
== 0)
567 /* get address of first pixel we'll read */
568 start
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
569 format
, type
, 0, 0, 0);
571 /* get address just past the last pixel we'll read */
572 end
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
573 format
, type
, depth
-1, height
-1, width
);
576 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
578 if ((const GLubyte
*) start
> sizeAddr
) {
579 /* This will catch negative values / wrap-around */
582 if ((const GLubyte
*) end
> sizeAddr
) {
583 /* Image read goes beyond end of buffer */
593 * If the source of glBitmap data is a PBO, check that we won't read out
594 * of buffer bounds, then map the buffer.
595 * If not sourcing from a PBO, just return the bitmap pointer.
596 * This is a helper function for (some) drivers.
597 * Return NULL if error.
598 * If non-null return, must call _mesa_unmap_bitmap_pbo() when done.
601 _mesa_map_bitmap_pbo(GLcontext
*ctx
,
602 const struct gl_pixelstore_attrib
*unpack
,
603 const GLubyte
*bitmap
)
607 if (unpack
->BufferObj
->Name
) {
608 /* unpack from PBO */
609 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
615 buf
= ADD_POINTERS(buf
, bitmap
);
618 /* unpack from normal memory */
627 * Counterpart to _mesa_map_bitmap_pbo()
628 * This is a helper function for (some) drivers.
631 _mesa_unmap_bitmap_pbo(GLcontext
*ctx
,
632 const struct gl_pixelstore_attrib
*unpack
)
634 if (unpack
->BufferObj
->Name
) {
635 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
642 * \sa _mesa_map_bitmap_pbo
645 _mesa_map_drawpix_pbo(GLcontext
*ctx
,
646 const struct gl_pixelstore_attrib
*unpack
,
647 const GLvoid
*pixels
)
651 if (unpack
->BufferObj
->Name
) {
652 /* unpack from PBO */
653 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
659 buf
= ADD_POINTERS(buf
, pixels
);
662 /* unpack from normal memory */
671 * \sa _mesa_unmap_bitmap_pbo
674 _mesa_unmap_drawpix_pbo(GLcontext
*ctx
,
675 const struct gl_pixelstore_attrib
*unpack
)
677 if (unpack
->BufferObj
->Name
) {
678 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
685 * If PBO is bound, map the buffer, return dest pointer in mapped buffer.
686 * Call _mesa_unmap_readpix_pbo() when finished
687 * \return NULL if error
690 _mesa_map_readpix_pbo(GLcontext
*ctx
,
691 const struct gl_pixelstore_attrib
*pack
,
696 if (pack
->BufferObj
->Name
) {
698 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
704 buf
= ADD_POINTERS(buf
, dest
);
707 /* pack to normal memory */
716 * Counterpart to _mesa_map_readpix_pbo()
719 _mesa_unmap_readpix_pbo(GLcontext
*ctx
,
720 const struct gl_pixelstore_attrib
*pack
)
722 if (pack
->BufferObj
->Name
) {
723 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
, pack
->BufferObj
);
730 * Return the gl_buffer_object for the given ID.
731 * Always return NULL for ID 0.
733 struct gl_buffer_object
*
734 _mesa_lookup_bufferobj(GLcontext
*ctx
, GLuint buffer
)
739 return (struct gl_buffer_object
*)
740 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
745 * If *ptr points to obj, set ptr = the Null/default buffer object.
746 * This is a helper for buffer object deletion.
747 * The GL spec says that deleting a buffer object causes it to get
748 * unbound from all arrays in the current context.
751 unbind(GLcontext
*ctx
,
752 struct gl_buffer_object
**ptr
,
753 struct gl_buffer_object
*obj
)
756 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Array
.NullBufferObj
);
762 /**********************************************************************/
764 /**********************************************************************/
767 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
769 GET_CURRENT_CONTEXT(ctx
);
770 ASSERT_OUTSIDE_BEGIN_END(ctx
);
772 bind_buffer_object(ctx
, target
, buffer
);
777 * Delete a set of buffer objects.
779 * \param n Number of buffer objects to delete.
780 * \param ids Array of \c n buffer object IDs.
783 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
785 GET_CURRENT_CONTEXT(ctx
);
787 ASSERT_OUTSIDE_BEGIN_END(ctx
);
790 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
794 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
796 for (i
= 0; i
< n
; i
++) {
797 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
799 /* unbind any vertex pointers bound to this buffer */
802 ASSERT(bufObj
->Name
== ids
[i
]);
804 unbind(ctx
, &ctx
->Array
.ArrayObj
->Vertex
.BufferObj
, bufObj
);
805 unbind(ctx
, &ctx
->Array
.ArrayObj
->Normal
.BufferObj
, bufObj
);
806 unbind(ctx
, &ctx
->Array
.ArrayObj
->Color
.BufferObj
, bufObj
);
807 unbind(ctx
, &ctx
->Array
.ArrayObj
->SecondaryColor
.BufferObj
, bufObj
);
808 unbind(ctx
, &ctx
->Array
.ArrayObj
->FogCoord
.BufferObj
, bufObj
);
809 unbind(ctx
, &ctx
->Array
.ArrayObj
->Index
.BufferObj
, bufObj
);
810 unbind(ctx
, &ctx
->Array
.ArrayObj
->EdgeFlag
.BufferObj
, bufObj
);
811 for (j
= 0; j
< MAX_TEXTURE_COORD_UNITS
; j
++) {
812 unbind(ctx
, &ctx
->Array
.ArrayObj
->TexCoord
[j
].BufferObj
, bufObj
);
814 for (j
= 0; j
< VERT_ATTRIB_MAX
; j
++) {
815 unbind(ctx
, &ctx
->Array
.ArrayObj
->VertexAttrib
[j
].BufferObj
, bufObj
);
818 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
819 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
821 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
822 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
825 if (ctx
->Pack
.BufferObj
== bufObj
) {
826 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
828 if (ctx
->Unpack
.BufferObj
== bufObj
) {
829 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
832 /* The ID is immediately freed for re-use */
833 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, bufObj
->Name
);
834 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
838 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
843 * Generate a set of unique buffer object IDs and store them in \c buffer.
845 * \param n Number of IDs to generate.
846 * \param buffer Array of \c n locations to store the IDs.
849 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
851 GET_CURRENT_CONTEXT(ctx
);
854 ASSERT_OUTSIDE_BEGIN_END(ctx
);
857 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
866 * This must be atomic (generation and allocation of buffer object IDs)
868 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
870 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
872 /* Allocate new, empty buffer objects and return identifiers */
873 for (i
= 0; i
< n
; i
++) {
874 struct gl_buffer_object
*bufObj
;
875 GLuint name
= first
+ i
;
877 bufObj
= ctx
->Driver
.NewBufferObject( ctx
, name
, target
);
879 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
880 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenBuffersARB");
883 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, first
+ i
, bufObj
);
884 buffer
[i
] = first
+ i
;
887 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
892 * Determine if ID is the name of a buffer object.
894 * \param id ID of the potential buffer object.
895 * \return \c GL_TRUE if \c id is the name of a buffer object,
896 * \c GL_FALSE otherwise.
899 _mesa_IsBufferARB(GLuint id
)
901 struct gl_buffer_object
*bufObj
;
902 GET_CURRENT_CONTEXT(ctx
);
903 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
905 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
906 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
907 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
909 return bufObj
? GL_TRUE
: GL_FALSE
;
914 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
915 const GLvoid
* data
, GLenum usage
)
917 GET_CURRENT_CONTEXT(ctx
);
918 struct gl_buffer_object
*bufObj
;
919 ASSERT_OUTSIDE_BEGIN_END(ctx
);
922 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
927 case GL_STREAM_DRAW_ARB
:
928 case GL_STREAM_READ_ARB
:
929 case GL_STREAM_COPY_ARB
:
930 case GL_STATIC_DRAW_ARB
:
931 case GL_STATIC_READ_ARB
:
932 case GL_STATIC_COPY_ARB
:
933 case GL_DYNAMIC_DRAW_ARB
:
934 case GL_DYNAMIC_READ_ARB
:
935 case GL_DYNAMIC_COPY_ARB
:
939 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
943 bufObj
= get_buffer(ctx
, target
);
945 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
948 if (bufObj
->Name
== 0) {
949 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB" );
953 if (bufObj
->Pointer
) {
954 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer is mapped)" );
958 ASSERT(ctx
->Driver
.BufferData
);
960 /* Give the buffer object to the driver! <data> may be null! */
961 ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
);
966 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
967 GLsizeiptrARB size
, const GLvoid
* data
)
969 GET_CURRENT_CONTEXT(ctx
);
970 struct gl_buffer_object
*bufObj
;
971 ASSERT_OUTSIDE_BEGIN_END(ctx
);
973 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
974 "glBufferSubDataARB" );
976 /* error already recorded */
980 ASSERT(ctx
->Driver
.BufferSubData
);
981 ctx
->Driver
.BufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
986 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
987 GLsizeiptrARB size
, void * data
)
989 GET_CURRENT_CONTEXT(ctx
);
990 struct gl_buffer_object
*bufObj
;
991 ASSERT_OUTSIDE_BEGIN_END(ctx
);
993 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
994 "glGetBufferSubDataARB" );
996 /* error already recorded */
1000 ASSERT(ctx
->Driver
.GetBufferSubData
);
1001 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1006 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1008 GET_CURRENT_CONTEXT(ctx
);
1009 struct gl_buffer_object
* bufObj
;
1010 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1013 case GL_READ_ONLY_ARB
:
1014 case GL_WRITE_ONLY_ARB
:
1015 case GL_READ_WRITE_ARB
:
1019 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1023 bufObj
= get_buffer(ctx
, target
);
1025 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
1028 if (bufObj
->Name
== 0) {
1029 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB" );
1032 if (bufObj
->Pointer
) {
1033 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1037 ASSERT(ctx
->Driver
.MapBuffer
);
1038 bufObj
->Pointer
= ctx
->Driver
.MapBuffer( ctx
, target
, access
, bufObj
);
1039 if (!bufObj
->Pointer
) {
1040 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(access)");
1043 bufObj
->Access
= access
;
1045 return bufObj
->Pointer
;
1049 GLboolean GLAPIENTRY
1050 _mesa_UnmapBufferARB(GLenum target
)
1052 GET_CURRENT_CONTEXT(ctx
);
1053 struct gl_buffer_object
*bufObj
;
1054 GLboolean status
= GL_TRUE
;
1055 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1057 bufObj
= get_buffer(ctx
, target
);
1059 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1062 if (bufObj
->Name
== 0) {
1063 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1066 if (!bufObj
->Pointer
) {
1067 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1071 if (ctx
->Driver
.UnmapBuffer
) {
1072 status
= ctx
->Driver
.UnmapBuffer( ctx
, target
, bufObj
);
1075 bufObj
->Access
= DEFAULT_ACCESS
;
1076 bufObj
->Pointer
= NULL
;
1083 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1085 GET_CURRENT_CONTEXT(ctx
);
1086 struct gl_buffer_object
*bufObj
;
1087 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1089 bufObj
= get_buffer(ctx
, target
);
1091 _mesa_error(ctx
, GL_INVALID_ENUM
, "GetBufferParameterivARB(target)" );
1094 if (bufObj
->Name
== 0) {
1095 _mesa_error(ctx
, GL_INVALID_OPERATION
, "GetBufferParameterivARB" );
1100 case GL_BUFFER_SIZE_ARB
:
1101 *params
= (GLint
) bufObj
->Size
;
1103 case GL_BUFFER_USAGE_ARB
:
1104 *params
= bufObj
->Usage
;
1106 case GL_BUFFER_ACCESS_ARB
:
1107 *params
= bufObj
->Access
;
1109 case GL_BUFFER_MAPPED_ARB
:
1110 *params
= (bufObj
->Pointer
!= NULL
);
1113 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname)");
1120 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1122 GET_CURRENT_CONTEXT(ctx
);
1123 struct gl_buffer_object
* bufObj
;
1124 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1126 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1127 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1131 bufObj
= get_buffer(ctx
, target
);
1133 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1136 if (bufObj
->Name
== 0) {
1137 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1141 *params
= bufObj
->Pointer
;