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"
42 * Get the buffer object bound to the specified target in a GL context.
44 * \param ctx GL context
45 * \param target Buffer object target to be retrieved. Currently this must
46 * be either \c GL_ARRAY_BUFFER or \c GL_ELEMENT_ARRAY_BUFFER.
47 * \return A pointer to the buffer object bound to \c target in the
48 * specified context or \c NULL if \c target is invalid.
50 static INLINE
struct gl_buffer_object
*
51 get_buffer(GLcontext
*ctx
, GLenum target
)
53 struct gl_buffer_object
* bufObj
= NULL
;
56 case GL_ARRAY_BUFFER_ARB
:
57 bufObj
= ctx
->Array
.ArrayBufferObj
;
59 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
60 bufObj
= ctx
->Array
.ElementArrayBufferObj
;
62 case GL_PIXEL_PACK_BUFFER_EXT
:
63 bufObj
= ctx
->Pack
.BufferObj
;
65 case GL_PIXEL_UNPACK_BUFFER_EXT
:
66 bufObj
= ctx
->Unpack
.BufferObj
;
69 /* error must be recorded by caller */
73 /* bufObj should point to NullBufferObj or a user-created buffer object */
81 * Tests the subdata range parameters and sets the GL error code for
82 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
84 * \param ctx GL context.
85 * \param target Buffer object target on which to operate.
86 * \param offset Offset of the first byte of the subdata range.
87 * \param size Size, in bytes, of the subdata range.
88 * \param caller Name of calling function for recording errors.
89 * \return A pointer to the buffer object bound to \c target in the
90 * specified context or \c NULL if any of the parameter or state
91 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
94 * \sa glBufferSubDataARB, glGetBufferSubDataARB
96 static struct gl_buffer_object
*
97 buffer_object_subdata_range_good( GLcontext
* ctx
, GLenum target
,
98 GLintptrARB offset
, GLsizeiptrARB size
,
101 struct gl_buffer_object
*bufObj
;
104 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
109 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
113 bufObj
= get_buffer(ctx
, target
);
115 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", caller
);
118 if (bufObj
->Name
== 0) {
119 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
122 if (offset
+ size
> bufObj
->Size
) {
123 _mesa_error(ctx
, GL_INVALID_VALUE
,
124 "%s(size + offset > buffer size)", caller
);
127 if (bufObj
->Pointer
) {
128 /* Buffer is currently mapped */
129 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
138 * Allocate and initialize a new buffer object.
140 * This function is intended to be called via
141 * \c dd_function_table::NewBufferObject.
143 struct gl_buffer_object
*
144 _mesa_new_buffer_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
146 struct gl_buffer_object
*obj
;
150 obj
= MALLOC_STRUCT(gl_buffer_object
);
151 _mesa_initialize_buffer_object(obj
, name
, target
);
157 * Delete a buffer object.
159 * This function is intended to be called via
160 * \c dd_function_table::DeleteBuffer.
163 _mesa_delete_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*bufObj
)
168 _mesa_free(bufObj
->Data
);
174 _mesa_unbind_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*bufObj
)
176 if (bufObj
!= ctx
->Array
.NullBufferObj
) {
178 if (bufObj
->RefCount
<= 0) {
179 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
180 ASSERT(ctx
->Array
.ElementArrayBufferObj
!= bufObj
);
181 ASSERT(ctx
->Array
.ArrayObj
->Vertex
.BufferObj
!= bufObj
);
182 ASSERT(ctx
->Driver
.DeleteBuffer
);
183 ctx
->Driver
.DeleteBuffer(ctx
, bufObj
);
190 * Initialize a buffer object to default values.
193 _mesa_initialize_buffer_object( struct gl_buffer_object
*obj
,
194 GLuint name
, GLenum target
)
198 _mesa_bzero(obj
, sizeof(struct gl_buffer_object
));
201 obj
->Usage
= GL_STATIC_DRAW_ARB
;
202 obj
->Access
= GL_READ_WRITE_ARB
;
207 * Add the given buffer object to the buffer object pool.
210 _mesa_save_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*obj
)
213 /* insert into hash table */
214 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, obj
->Name
, obj
);
220 * Remove the given buffer object from the buffer object pool.
221 * Do not deallocate the buffer object though.
224 _mesa_remove_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*bufObj
)
226 if (bufObj
->Name
> 0) {
227 /* remove from hash table */
228 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, bufObj
->Name
);
234 * Allocate space for and store data in a buffer object. Any data that was
235 * previously stored in the buffer object is lost. If \c data is \c NULL,
236 * memory will be allocated, but no copy will occur.
238 * This function is intended to be called via
239 * \c dd_function_table::BufferData. This function need not set GL error
240 * codes. The input parameters will have been tested before calling.
242 * \param ctx GL context.
243 * \param target Buffer object target on which to operate.
244 * \param size Size, in bytes, of the new data store.
245 * \param data Pointer to the data to store in the buffer object. This
246 * pointer may be \c NULL.
247 * \param usage Hints about how the data will be used.
248 * \param bufObj Object to be used.
250 * \sa glBufferDataARB, dd_function_table::BufferData.
253 _mesa_buffer_data( GLcontext
*ctx
, GLenum target
, GLsizeiptrARB size
,
254 const GLvoid
* data
, GLenum usage
,
255 struct gl_buffer_object
* bufObj
)
259 (void) ctx
; (void) target
;
261 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
263 bufObj
->Data
= (GLubyte
*) new_data
;
265 bufObj
->Usage
= usage
;
268 _mesa_memcpy( bufObj
->Data
, data
, size
);
275 * Replace data in a subrange of buffer object. If the data range
276 * specified by \c size + \c offset extends beyond the end of the buffer or
277 * if \c data is \c NULL, no copy is performed.
279 * This function is intended to be called by
280 * \c dd_function_table::BufferSubData. This function need not set GL error
281 * codes. The input parameters will have been tested before calling.
283 * \param ctx GL context.
284 * \param target Buffer object target on which to operate.
285 * \param offset Offset of the first byte to be modified.
286 * \param size Size, in bytes, of the data range.
287 * \param data Pointer to the data to store in the buffer object.
288 * \param bufObj Object to be used.
290 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
293 _mesa_buffer_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
294 GLsizeiptrARB size
, const GLvoid
* data
,
295 struct gl_buffer_object
* bufObj
)
297 (void) ctx
; (void) target
;
299 /* this should have been caught in _mesa_BufferSubData() */
300 ASSERT(size
+ offset
<= bufObj
->Size
);
303 _mesa_memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
309 * Retrieve data from 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 function is intended to be called by
314 * \c dd_function_table::BufferGetSubData. This function need not set GL error
315 * codes. The input parameters will have been tested before calling.
317 * \param ctx GL context.
318 * \param target Buffer object target on which to operate.
319 * \param offset Offset of the first byte to be modified.
320 * \param size Size, in bytes, of the data range.
321 * \param data Pointer to the data to store in the buffer object.
322 * \param bufObj Object to be used.
324 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
327 _mesa_buffer_get_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
328 GLsizeiptrARB size
, GLvoid
* data
,
329 struct gl_buffer_object
* bufObj
)
331 (void) ctx
; (void) target
;
333 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
334 _mesa_memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
340 * Fallback function called via ctx->Driver.MapBuffer().
341 * Hardware drivers that really implement buffer objects should never use
344 * The function parameters will have been already tested for errors.
346 * \param ctx GL context.
347 * \param target Buffer object target on which to operate.
348 * \param access Information about how the buffer will be accessed.
349 * \param bufObj Object to be mapped.
350 * \return A pointer to the object's internal data store that can be accessed
353 * \sa glMapBufferARB, dd_function_table::MapBuffer
356 _mesa_buffer_map( GLcontext
*ctx
, GLenum target
, GLenum access
,
357 struct gl_buffer_object
*bufObj
)
362 ASSERT(!bufObj
->OnCard
);
363 /* Just return a direct pointer to the data */
364 if (bufObj
->Pointer
) {
365 /* already mapped! */
368 bufObj
->Pointer
= bufObj
->Data
;
369 return bufObj
->Pointer
;
374 * Fallback function called via ctx->Driver.MapBuffer().
375 * Hardware drivers that really implement buffer objects should never use
378 * The input parameters will have been already tested for errors.
380 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
383 _mesa_buffer_unmap( GLcontext
*ctx
, GLenum target
,
384 struct gl_buffer_object
*bufObj
)
388 ASSERT(!bufObj
->OnCard
);
389 /* XXX we might assert here that bufObj->Pointer is non-null */
390 bufObj
->Pointer
= NULL
;
396 * Initialize the state associated with buffer objects
399 _mesa_init_buffer_objects( GLcontext
*ctx
)
401 /* Allocate the default buffer object and set refcount so high that
402 * it never gets deleted.
404 ctx
->Array
.NullBufferObj
= _mesa_new_buffer_object(ctx
, 0, 0);
405 if (ctx
->Array
.NullBufferObj
)
406 ctx
->Array
.NullBufferObj
->RefCount
= 1000;
408 ctx
->Array
.ArrayBufferObj
= ctx
->Array
.NullBufferObj
;
409 ctx
->Array
.ElementArrayBufferObj
= ctx
->Array
.NullBufferObj
;
413 * Bind the specified target to buffer for the specified context.
416 bind_buffer_object(GLcontext
*ctx
, GLenum target
, GLuint buffer
)
418 struct gl_buffer_object
*oldBufObj
;
419 struct gl_buffer_object
*newBufObj
= NULL
;
420 struct gl_buffer_object
**bindTarget
= NULL
;
423 case GL_ARRAY_BUFFER_ARB
:
424 bindTarget
= &ctx
->Array
.ArrayBufferObj
;
426 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
427 bindTarget
= &ctx
->Array
.ElementArrayBufferObj
;
429 case GL_PIXEL_PACK_BUFFER_EXT
:
430 bindTarget
= &ctx
->Pack
.BufferObj
;
432 case GL_PIXEL_UNPACK_BUFFER_EXT
:
433 bindTarget
= &ctx
->Unpack
.BufferObj
;
436 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target)");
440 /* Get pointer to old buffer object (to be unbound) */
441 oldBufObj
= get_buffer(ctx
, target
);
442 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
443 return; /* rebinding the same buffer object- no change */
446 * Get pointer to new buffer object (newBufObj)
449 /* The spec says there's not a buffer object named 0, but we use
450 * one internally because it simplifies things.
452 newBufObj
= ctx
->Array
.NullBufferObj
;
455 /* non-default buffer object */
456 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
458 /* if this is a new buffer object id, allocate a buffer object now */
459 ASSERT(ctx
->Driver
.NewBufferObject
);
460 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
462 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
465 _mesa_save_buffer_object(ctx
, newBufObj
);
469 /* Make new binding */
470 *bindTarget
= newBufObj
;
471 newBufObj
->RefCount
++;
473 /* Pass BindBuffer call to device driver */
474 if (ctx
->Driver
.BindBuffer
&& newBufObj
)
475 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
477 /* decr ref count on old buffer obj, delete if needed */
479 oldBufObj
->RefCount
--;
480 assert(oldBufObj
->RefCount
>= 0);
481 if (oldBufObj
->RefCount
== 0) {
482 assert(oldBufObj
->Name
!= 0);
483 ASSERT(ctx
->Driver
.DeleteBuffer
);
484 ctx
->Driver
.DeleteBuffer( ctx
, oldBufObj
);
491 * Update the default buffer objects in the given context to reference those
492 * specified in the shared state and release those referencing the old
496 _mesa_update_default_objects_buffer_objects(GLcontext
*ctx
)
498 /* Bind the NullBufferObj to remove references to those
499 * in the shared context hash table.
501 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
502 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
503 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
504 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
509 * When we're about to read pixel data out of a PBO (via glDrawPixels,
510 * glTexImage, etc) or write data into a PBO (via glReadPixels,
511 * glGetTexImage, etc) we call this function to check that we're not
512 * going to read out of bounds.
514 * XXX This would also be a convenient time to check that the PBO isn't
515 * currently mapped. Whoever calls this function should check for that.
516 * Remember, we can't use a PBO when it's mapped!
518 * \param width width of image to read/write
519 * \param height height of image to read/write
520 * \param depth depth of image to read/write
521 * \param format format of image to read/write
522 * \param type datatype of image to read/write
523 * \param ptr the user-provided pointer/offset
524 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
528 _mesa_validate_pbo_access(GLuint dimensions
,
529 const struct gl_pixelstore_attrib
*pack
,
530 GLsizei width
, GLsizei height
, GLsizei depth
,
531 GLenum format
, GLenum type
, const GLvoid
*ptr
)
534 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
536 ASSERT(pack
->BufferObj
->Name
!= 0);
538 if (pack
->BufferObj
->Size
== 0)
542 /* get address of first pixel we'll read */
543 start
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
544 format
, type
, 0, 0, 0);
546 /* get address just past the last pixel we'll read */
547 end
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
548 format
, type
, depth
-1, height
-1, width
);
551 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
553 if ((const GLubyte
*) start
> sizeAddr
) {
554 /* This will catch negative values / wrap-around */
557 if ((const GLubyte
*) end
> sizeAddr
) {
558 /* Image read goes beyond end of buffer */
568 * If the source of glBitmap data is a PBO, check that we won't read out
569 * of buffer bounds, then map the buffer.
570 * If not sourcing from a PBO, just return the bitmap pointer.
571 * This is a helper function for (some) drivers.
572 * Return NULL if error.
573 * If non-null return, must call _mesa_unmap_bitmap_pbo() when done.
576 _mesa_map_bitmap_pbo(GLcontext
*ctx
,
577 const struct gl_pixelstore_attrib
*unpack
,
578 const GLubyte
*bitmap
)
582 if (unpack
->BufferObj
->Name
) {
583 /* unpack from PBO */
584 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
590 buf
= ADD_POINTERS(buf
, bitmap
);
593 /* unpack from normal memory */
602 * Counterpart to _mesa_map_bitmap_pbo()
603 * This is a helper function for (some) drivers.
606 _mesa_unmap_bitmap_pbo(GLcontext
*ctx
,
607 const struct gl_pixelstore_attrib
*unpack
)
609 if (unpack
->BufferObj
->Name
) {
610 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
617 * \sa _mesa_map_bitmap_pbo
620 _mesa_map_drawpix_pbo(GLcontext
*ctx
,
621 const struct gl_pixelstore_attrib
*unpack
,
622 const GLvoid
*pixels
)
626 if (unpack
->BufferObj
->Name
) {
627 /* unpack from PBO */
628 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
634 buf
= ADD_POINTERS(buf
, pixels
);
637 /* unpack from normal memory */
646 * \sa _mesa_unmap_bitmap_pbo
649 _mesa_unmap_drapix_pbo(GLcontext
*ctx
,
650 const struct gl_pixelstore_attrib
*unpack
)
652 if (unpack
->BufferObj
->Name
) {
653 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
660 * If PBO is bound, map the buffer, return dest pointer in mapped buffer.
661 * Call _mesa_unmap_readpix_pbo() when finished
662 * \return NULL if error
665 _mesa_map_readpix_pbo(GLcontext
*ctx
,
666 const struct gl_pixelstore_attrib
*pack
,
671 if (pack
->BufferObj
->Name
) {
673 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
679 buf
= ADD_POINTERS(buf
, dest
);
682 /* pack to normal memory */
691 * Counterpart to _mesa_map_readpix_pbo()
694 _mesa_unmap_readpix_pbo(GLcontext
*ctx
,
695 const struct gl_pixelstore_attrib
*pack
)
697 if (pack
->BufferObj
->Name
) {
698 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
, pack
->BufferObj
);
705 * Return the gl_buffer_object for the given ID.
706 * Always return NULL for ID 0.
708 struct gl_buffer_object
*
709 _mesa_lookup_bufferobj(GLcontext
*ctx
, GLuint buffer
)
714 return (struct gl_buffer_object
*)
715 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
720 /**********************************************************************/
722 /**********************************************************************/
725 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
727 GET_CURRENT_CONTEXT(ctx
);
728 ASSERT_OUTSIDE_BEGIN_END(ctx
);
730 bind_buffer_object(ctx
, target
, buffer
);
735 * Delete a set of buffer objects.
737 * \param n Number of buffer objects to delete.
738 * \param ids Array of \c n buffer object IDs.
741 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
743 GET_CURRENT_CONTEXT(ctx
);
745 ASSERT_OUTSIDE_BEGIN_END(ctx
);
748 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
752 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
754 for (i
= 0; i
< n
; i
++) {
755 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
757 /* unbind any vertex pointers bound to this buffer */
760 ASSERT(bufObj
->Name
== ids
[i
]);
762 if (ctx
->Array
.ArrayObj
->Vertex
.BufferObj
== bufObj
) {
764 ctx
->Array
.ArrayObj
->Vertex
.BufferObj
= ctx
->Array
.NullBufferObj
;
765 ctx
->Array
.NullBufferObj
->RefCount
++;
767 if (ctx
->Array
.ArrayObj
->Normal
.BufferObj
== bufObj
) {
769 ctx
->Array
.ArrayObj
->Normal
.BufferObj
= ctx
->Array
.NullBufferObj
;
770 ctx
->Array
.NullBufferObj
->RefCount
++;
772 if (ctx
->Array
.ArrayObj
->Color
.BufferObj
== bufObj
) {
774 ctx
->Array
.ArrayObj
->Color
.BufferObj
= ctx
->Array
.NullBufferObj
;
775 ctx
->Array
.NullBufferObj
->RefCount
++;
777 if (ctx
->Array
.ArrayObj
->SecondaryColor
.BufferObj
== bufObj
) {
779 ctx
->Array
.ArrayObj
->SecondaryColor
.BufferObj
= ctx
->Array
.NullBufferObj
;
780 ctx
->Array
.NullBufferObj
->RefCount
++;
782 if (ctx
->Array
.ArrayObj
->FogCoord
.BufferObj
== bufObj
) {
784 ctx
->Array
.ArrayObj
->FogCoord
.BufferObj
= ctx
->Array
.NullBufferObj
;
785 ctx
->Array
.NullBufferObj
->RefCount
++;
787 if (ctx
->Array
.ArrayObj
->Index
.BufferObj
== bufObj
) {
789 ctx
->Array
.ArrayObj
->Index
.BufferObj
= ctx
->Array
.NullBufferObj
;
790 ctx
->Array
.NullBufferObj
->RefCount
++;
792 if (ctx
->Array
.ArrayObj
->EdgeFlag
.BufferObj
== bufObj
) {
794 ctx
->Array
.ArrayObj
->EdgeFlag
.BufferObj
= ctx
->Array
.NullBufferObj
;
795 ctx
->Array
.NullBufferObj
->RefCount
++;
797 for (j
= 0; j
< MAX_TEXTURE_UNITS
; j
++) {
798 if (ctx
->Array
.ArrayObj
->TexCoord
[j
].BufferObj
== bufObj
) {
800 ctx
->Array
.ArrayObj
->TexCoord
[j
].BufferObj
= ctx
->Array
.NullBufferObj
;
801 ctx
->Array
.NullBufferObj
->RefCount
++;
804 for (j
= 0; j
< VERT_ATTRIB_MAX
; j
++) {
805 if (ctx
->Array
.ArrayObj
->VertexAttrib
[j
].BufferObj
== bufObj
) {
807 ctx
->Array
.ArrayObj
->VertexAttrib
[j
].BufferObj
= ctx
->Array
.NullBufferObj
;
808 ctx
->Array
.NullBufferObj
->RefCount
++;
812 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
813 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
815 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
816 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
819 if (ctx
->Pack
.BufferObj
== bufObj
) {
820 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
822 if (ctx
->Unpack
.BufferObj
== bufObj
) {
823 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
826 /* The ID is immediately freed for re-use */
827 _mesa_remove_buffer_object(ctx
, bufObj
);
828 _mesa_unbind_buffer_object(ctx
, bufObj
);
832 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
837 * Generate a set of unique buffer object IDs and store them in \c buffer.
839 * \param n Number of IDs to generate.
840 * \param buffer Array of \c n locations to store the IDs.
843 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
845 GET_CURRENT_CONTEXT(ctx
);
848 ASSERT_OUTSIDE_BEGIN_END(ctx
);
851 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
860 * This must be atomic (generation and allocation of buffer object IDs)
862 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
864 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
866 /* Allocate new, empty buffer objects and return identifiers */
867 for (i
= 0; i
< n
; i
++) {
868 struct gl_buffer_object
*bufObj
;
869 GLuint name
= first
+ i
;
871 bufObj
= ctx
->Driver
.NewBufferObject( ctx
, name
, target
);
873 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
874 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenBuffersARB");
877 _mesa_save_buffer_object(ctx
, bufObj
);
878 buffer
[i
] = first
+ i
;
881 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
886 * Determine if ID is the name of a buffer object.
888 * \param id ID of the potential buffer object.
889 * \return \c GL_TRUE if \c id is the name of a buffer object,
890 * \c GL_FALSE otherwise.
893 _mesa_IsBufferARB(GLuint id
)
895 struct gl_buffer_object
*bufObj
;
896 GET_CURRENT_CONTEXT(ctx
);
897 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
899 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
900 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
901 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
903 return bufObj
? GL_TRUE
: GL_FALSE
;
908 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
909 const GLvoid
* data
, GLenum usage
)
911 GET_CURRENT_CONTEXT(ctx
);
912 struct gl_buffer_object
*bufObj
;
913 ASSERT_OUTSIDE_BEGIN_END(ctx
);
916 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
921 case GL_STREAM_DRAW_ARB
:
922 case GL_STREAM_READ_ARB
:
923 case GL_STREAM_COPY_ARB
:
924 case GL_STATIC_DRAW_ARB
:
925 case GL_STATIC_READ_ARB
:
926 case GL_STATIC_COPY_ARB
:
927 case GL_DYNAMIC_DRAW_ARB
:
928 case GL_DYNAMIC_READ_ARB
:
929 case GL_DYNAMIC_COPY_ARB
:
933 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
937 bufObj
= get_buffer(ctx
, target
);
939 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
942 if (bufObj
->Name
== 0) {
943 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB" );
947 if (bufObj
->Pointer
) {
948 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer is mapped)" );
952 ASSERT(ctx
->Driver
.BufferData
);
954 /* Give the buffer object to the driver! <data> may be null! */
955 ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
);
960 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
961 GLsizeiptrARB size
, const GLvoid
* data
)
963 GET_CURRENT_CONTEXT(ctx
);
964 struct gl_buffer_object
*bufObj
;
965 ASSERT_OUTSIDE_BEGIN_END(ctx
);
967 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
968 "glBufferSubDataARB" );
970 /* error already recorded */
974 ASSERT(ctx
->Driver
.BufferSubData
);
975 ctx
->Driver
.BufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
980 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
981 GLsizeiptrARB size
, void * data
)
983 GET_CURRENT_CONTEXT(ctx
);
984 struct gl_buffer_object
*bufObj
;
985 ASSERT_OUTSIDE_BEGIN_END(ctx
);
987 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
988 "glGetBufferSubDataARB" );
990 /* error already recorded */
994 ASSERT(ctx
->Driver
.GetBufferSubData
);
995 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
1000 _mesa_MapBufferARB(GLenum target
, GLenum access
)
1002 GET_CURRENT_CONTEXT(ctx
);
1003 struct gl_buffer_object
* bufObj
;
1004 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
1007 case GL_READ_ONLY_ARB
:
1008 case GL_WRITE_ONLY_ARB
:
1009 case GL_READ_WRITE_ARB
:
1013 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
1017 bufObj
= get_buffer(ctx
, target
);
1019 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
1022 if (bufObj
->Name
== 0) {
1023 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB" );
1026 if (bufObj
->Pointer
) {
1027 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
1031 ASSERT(ctx
->Driver
.MapBuffer
);
1032 bufObj
->Pointer
= ctx
->Driver
.MapBuffer( ctx
, target
, access
, bufObj
);
1033 if (!bufObj
->Pointer
) {
1034 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(access)");
1037 bufObj
->Access
= access
;
1039 return bufObj
->Pointer
;
1043 GLboolean GLAPIENTRY
1044 _mesa_UnmapBufferARB(GLenum target
)
1046 GET_CURRENT_CONTEXT(ctx
);
1047 struct gl_buffer_object
*bufObj
;
1048 GLboolean status
= GL_TRUE
;
1049 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1051 bufObj
= get_buffer(ctx
, target
);
1053 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
1056 if (bufObj
->Name
== 0) {
1057 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
1060 if (!bufObj
->Pointer
) {
1061 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
1065 if (ctx
->Driver
.UnmapBuffer
) {
1066 status
= ctx
->Driver
.UnmapBuffer( ctx
, target
, bufObj
);
1069 bufObj
->Access
= GL_READ_WRITE_ARB
; /* initial value, OK? */
1070 bufObj
->Pointer
= NULL
;
1077 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
1079 GET_CURRENT_CONTEXT(ctx
);
1080 struct gl_buffer_object
*bufObj
;
1081 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1083 bufObj
= get_buffer(ctx
, target
);
1085 _mesa_error(ctx
, GL_INVALID_ENUM
, "GetBufferParameterivARB(target)" );
1088 if (bufObj
->Name
== 0) {
1089 _mesa_error(ctx
, GL_INVALID_OPERATION
, "GetBufferParameterivARB" );
1094 case GL_BUFFER_SIZE_ARB
:
1095 *params
= (GLint
) bufObj
->Size
;
1097 case GL_BUFFER_USAGE_ARB
:
1098 *params
= bufObj
->Usage
;
1100 case GL_BUFFER_ACCESS_ARB
:
1101 *params
= bufObj
->Access
;
1103 case GL_BUFFER_MAPPED_ARB
:
1104 *params
= (bufObj
->Pointer
!= NULL
);
1107 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname)");
1114 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
1116 GET_CURRENT_CONTEXT(ctx
);
1117 struct gl_buffer_object
* bufObj
;
1118 ASSERT_OUTSIDE_BEGIN_END(ctx
);
1120 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
1121 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
1125 bufObj
= get_buffer(ctx
, target
);
1127 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
1130 if (bufObj
->Name
== 0) {
1131 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
1135 *params
= bufObj
->Pointer
;