2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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
;
414 * When we're about to read pixel data out of a PBO (via glDrawPixels,
415 * glTexImage, etc) or write data into a PBO (via glReadPixels,
416 * glGetTexImage, etc) we call this function to check that we're not
417 * going to read out of bounds.
419 * XXX This would also be a convenient time to check that the PBO isn't
420 * currently mapped. Whoever calls this function should check for that.
421 * Remember, we can't use a PBO when it's mapped!
423 * \param width width of image to read/write
424 * \param height height of image to read/write
425 * \param depth depth of image to read/write
426 * \param format format of image to read/write
427 * \param type datatype of image to read/write
428 * \param ptr the user-provided pointer/offset
429 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
433 _mesa_validate_pbo_access(GLuint dimensions
,
434 const struct gl_pixelstore_attrib
*pack
,
435 GLsizei width
, GLsizei height
, GLsizei depth
,
436 GLenum format
, GLenum type
, const GLvoid
*ptr
)
439 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
441 ASSERT(pack
->BufferObj
->Name
!= 0);
443 if (pack
->BufferObj
->Size
== 0)
447 /* get address of first pixel we'll read */
448 start
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
449 format
, type
, 0, 0, 0);
451 /* get address just past the last pixel we'll read */
452 end
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
453 format
, type
, depth
-1, height
-1, width
);
456 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
458 if ((const GLubyte
*) start
> sizeAddr
) {
459 /* This will catch negative values / wrap-around */
462 if ((const GLubyte
*) end
> sizeAddr
) {
463 /* Image read goes beyond end of buffer */
473 * Return the gl_buffer_object for the given ID.
474 * Always return NULL for ID 0.
476 struct gl_buffer_object
*
477 _mesa_lookup_bufferobj(GLcontext
*ctx
, GLuint buffer
)
482 return (struct gl_buffer_object
*)
483 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
488 /**********************************************************************/
490 /**********************************************************************/
493 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
495 GET_CURRENT_CONTEXT(ctx
);
496 struct gl_buffer_object
*oldBufObj
;
497 struct gl_buffer_object
*newBufObj
= NULL
;
498 struct gl_buffer_object
**bindTarget
= NULL
;
499 ASSERT_OUTSIDE_BEGIN_END(ctx
);
502 case GL_ARRAY_BUFFER_ARB
:
503 bindTarget
= &ctx
->Array
.ArrayBufferObj
;
505 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
506 bindTarget
= &ctx
->Array
.ElementArrayBufferObj
;
508 case GL_PIXEL_PACK_BUFFER_EXT
:
509 bindTarget
= &ctx
->Pack
.BufferObj
;
511 case GL_PIXEL_UNPACK_BUFFER_EXT
:
512 bindTarget
= &ctx
->Unpack
.BufferObj
;
515 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target)");
519 /* Get pointer to old buffer object (to be unbound) */
520 oldBufObj
= get_buffer(ctx
, target
);
521 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
522 return; /* rebinding the same buffer object- no change */
525 * Get pointer to new buffer object (newBufObj)
528 /* The spec says there's not a buffer object named 0, but we use
529 * one internally because it simplifies things.
531 newBufObj
= ctx
->Array
.NullBufferObj
;
534 /* non-default buffer object */
535 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
537 /* if this is a new buffer object id, allocate a buffer object now */
538 ASSERT(ctx
->Driver
.NewBufferObject
);
539 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
541 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
544 _mesa_save_buffer_object(ctx
, newBufObj
);
548 /* Make new binding */
549 *bindTarget
= newBufObj
;
550 newBufObj
->RefCount
++;
552 /* Pass BindBuffer call to device driver */
553 if (ctx
->Driver
.BindBuffer
&& newBufObj
)
554 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
556 /* decr ref count on old buffer obj, delete if needed */
558 oldBufObj
->RefCount
--;
559 assert(oldBufObj
->RefCount
>= 0);
560 if (oldBufObj
->RefCount
== 0) {
561 assert(oldBufObj
->Name
!= 0);
562 ASSERT(ctx
->Driver
.DeleteBuffer
);
563 ctx
->Driver
.DeleteBuffer( ctx
, oldBufObj
);
570 * Delete a set of buffer objects.
572 * \param n Number of buffer objects to delete.
573 * \param ids Array of \c n buffer object IDs.
576 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
578 GET_CURRENT_CONTEXT(ctx
);
580 ASSERT_OUTSIDE_BEGIN_END(ctx
);
583 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
587 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
589 for (i
= 0; i
< n
; i
++) {
590 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
592 /* unbind any vertex pointers bound to this buffer */
595 ASSERT(bufObj
->Name
== ids
[i
]);
597 if (ctx
->Array
.ArrayObj
->Vertex
.BufferObj
== bufObj
) {
599 ctx
->Array
.ArrayObj
->Vertex
.BufferObj
= ctx
->Array
.NullBufferObj
;
600 ctx
->Array
.NullBufferObj
->RefCount
++;
602 if (ctx
->Array
.ArrayObj
->Normal
.BufferObj
== bufObj
) {
604 ctx
->Array
.ArrayObj
->Normal
.BufferObj
= ctx
->Array
.NullBufferObj
;
605 ctx
->Array
.NullBufferObj
->RefCount
++;
607 if (ctx
->Array
.ArrayObj
->Color
.BufferObj
== bufObj
) {
609 ctx
->Array
.ArrayObj
->Color
.BufferObj
= ctx
->Array
.NullBufferObj
;
610 ctx
->Array
.NullBufferObj
->RefCount
++;
612 if (ctx
->Array
.ArrayObj
->SecondaryColor
.BufferObj
== bufObj
) {
614 ctx
->Array
.ArrayObj
->SecondaryColor
.BufferObj
= ctx
->Array
.NullBufferObj
;
615 ctx
->Array
.NullBufferObj
->RefCount
++;
617 if (ctx
->Array
.ArrayObj
->FogCoord
.BufferObj
== bufObj
) {
619 ctx
->Array
.ArrayObj
->FogCoord
.BufferObj
= ctx
->Array
.NullBufferObj
;
620 ctx
->Array
.NullBufferObj
->RefCount
++;
622 if (ctx
->Array
.ArrayObj
->Index
.BufferObj
== bufObj
) {
624 ctx
->Array
.ArrayObj
->Index
.BufferObj
= ctx
->Array
.NullBufferObj
;
625 ctx
->Array
.NullBufferObj
->RefCount
++;
627 if (ctx
->Array
.ArrayObj
->EdgeFlag
.BufferObj
== bufObj
) {
629 ctx
->Array
.ArrayObj
->EdgeFlag
.BufferObj
= ctx
->Array
.NullBufferObj
;
630 ctx
->Array
.NullBufferObj
->RefCount
++;
632 for (j
= 0; j
< MAX_TEXTURE_UNITS
; j
++) {
633 if (ctx
->Array
.ArrayObj
->TexCoord
[j
].BufferObj
== bufObj
) {
635 ctx
->Array
.ArrayObj
->TexCoord
[j
].BufferObj
= ctx
->Array
.NullBufferObj
;
636 ctx
->Array
.NullBufferObj
->RefCount
++;
639 for (j
= 0; j
< VERT_ATTRIB_MAX
; j
++) {
640 if (ctx
->Array
.ArrayObj
->VertexAttrib
[j
].BufferObj
== bufObj
) {
642 ctx
->Array
.ArrayObj
->VertexAttrib
[j
].BufferObj
= ctx
->Array
.NullBufferObj
;
643 ctx
->Array
.NullBufferObj
->RefCount
++;
647 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
648 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
650 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
651 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
654 if (ctx
->Pack
.BufferObj
== bufObj
) {
655 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
657 if (ctx
->Unpack
.BufferObj
== bufObj
) {
658 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
661 /* The ID is immediately freed for re-use */
662 _mesa_remove_buffer_object(ctx
, bufObj
);
663 _mesa_unbind_buffer_object(ctx
, bufObj
);
667 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
672 * Generate a set of unique buffer object IDs and store them in \c buffer.
674 * \param n Number of IDs to generate.
675 * \param buffer Array of \c n locations to store the IDs.
678 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
680 GET_CURRENT_CONTEXT(ctx
);
683 ASSERT_OUTSIDE_BEGIN_END(ctx
);
686 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
695 * This must be atomic (generation and allocation of buffer object IDs)
697 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
699 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
701 /* Allocate new, empty buffer objects and return identifiers */
702 for (i
= 0; i
< n
; i
++) {
703 struct gl_buffer_object
*bufObj
;
704 GLuint name
= first
+ i
;
706 bufObj
= ctx
->Driver
.NewBufferObject( ctx
, name
, target
);
708 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
709 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenBuffersARB");
712 _mesa_save_buffer_object(ctx
, bufObj
);
713 buffer
[i
] = first
+ i
;
716 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
721 * Determine if ID is the name of a buffer object.
723 * \param id ID of the potential buffer object.
724 * \return \c GL_TRUE if \c id is the name of a buffer object,
725 * \c GL_FALSE otherwise.
728 _mesa_IsBufferARB(GLuint id
)
730 struct gl_buffer_object
*bufObj
;
731 GET_CURRENT_CONTEXT(ctx
);
732 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
734 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
735 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
736 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
738 return bufObj
? GL_TRUE
: GL_FALSE
;
743 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
744 const GLvoid
* data
, GLenum usage
)
746 GET_CURRENT_CONTEXT(ctx
);
747 struct gl_buffer_object
*bufObj
;
748 ASSERT_OUTSIDE_BEGIN_END(ctx
);
751 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
756 case GL_STREAM_DRAW_ARB
:
757 case GL_STREAM_READ_ARB
:
758 case GL_STREAM_COPY_ARB
:
759 case GL_STATIC_DRAW_ARB
:
760 case GL_STATIC_READ_ARB
:
761 case GL_STATIC_COPY_ARB
:
762 case GL_DYNAMIC_DRAW_ARB
:
763 case GL_DYNAMIC_READ_ARB
:
764 case GL_DYNAMIC_COPY_ARB
:
768 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
772 bufObj
= get_buffer(ctx
, target
);
774 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(target)" );
777 if (bufObj
->Name
== 0) {
778 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB" );
782 if (bufObj
->Pointer
) {
783 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer is mapped)" );
787 ASSERT(ctx
->Driver
.BufferData
);
789 /* Give the buffer object to the driver! <data> may be null! */
790 ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
);
795 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
796 GLsizeiptrARB size
, const GLvoid
* data
)
798 GET_CURRENT_CONTEXT(ctx
);
799 struct gl_buffer_object
*bufObj
;
800 ASSERT_OUTSIDE_BEGIN_END(ctx
);
802 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
803 "glBufferSubDataARB" );
805 /* error already recorded */
809 ASSERT(ctx
->Driver
.BufferSubData
);
810 ctx
->Driver
.BufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
815 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
816 GLsizeiptrARB size
, void * data
)
818 GET_CURRENT_CONTEXT(ctx
);
819 struct gl_buffer_object
*bufObj
;
820 ASSERT_OUTSIDE_BEGIN_END(ctx
);
822 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
823 "glGetBufferSubDataARB" );
825 /* error already recorded */
829 ASSERT(ctx
->Driver
.GetBufferSubData
);
830 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
835 _mesa_MapBufferARB(GLenum target
, GLenum access
)
837 GET_CURRENT_CONTEXT(ctx
);
838 struct gl_buffer_object
* bufObj
;
839 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
842 case GL_READ_ONLY_ARB
:
843 case GL_WRITE_ONLY_ARB
:
844 case GL_READ_WRITE_ARB
:
848 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
852 bufObj
= get_buffer(ctx
, target
);
854 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(target)" );
857 if (bufObj
->Name
== 0) {
858 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB" );
861 if (bufObj
->Pointer
) {
862 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
866 ASSERT(ctx
->Driver
.MapBuffer
);
867 bufObj
->Pointer
= ctx
->Driver
.MapBuffer( ctx
, target
, access
, bufObj
);
868 if (!bufObj
->Pointer
) {
869 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(access)");
872 bufObj
->Access
= access
;
874 return bufObj
->Pointer
;
879 _mesa_UnmapBufferARB(GLenum target
)
881 GET_CURRENT_CONTEXT(ctx
);
882 struct gl_buffer_object
*bufObj
;
883 GLboolean status
= GL_TRUE
;
884 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
886 bufObj
= get_buffer(ctx
, target
);
888 _mesa_error(ctx
, GL_INVALID_ENUM
, "glUnmapBufferARB(target)" );
891 if (bufObj
->Name
== 0) {
892 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
895 if (!bufObj
->Pointer
) {
896 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
900 if (ctx
->Driver
.UnmapBuffer
) {
901 status
= ctx
->Driver
.UnmapBuffer( ctx
, target
, bufObj
);
904 bufObj
->Access
= GL_READ_WRITE_ARB
; /* initial value, OK? */
905 bufObj
->Pointer
= NULL
;
912 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
914 GET_CURRENT_CONTEXT(ctx
);
915 struct gl_buffer_object
*bufObj
;
916 ASSERT_OUTSIDE_BEGIN_END(ctx
);
918 bufObj
= get_buffer(ctx
, target
);
920 _mesa_error(ctx
, GL_INVALID_ENUM
, "GetBufferParameterivARB(target)" );
923 if (bufObj
->Name
== 0) {
924 _mesa_error(ctx
, GL_INVALID_OPERATION
, "GetBufferParameterivARB" );
929 case GL_BUFFER_SIZE_ARB
:
930 *params
= (GLint
) bufObj
->Size
;
932 case GL_BUFFER_USAGE_ARB
:
933 *params
= bufObj
->Usage
;
935 case GL_BUFFER_ACCESS_ARB
:
936 *params
= bufObj
->Access
;
938 case GL_BUFFER_MAPPED_ARB
:
939 *params
= (bufObj
->Pointer
!= NULL
);
942 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname)");
949 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
951 GET_CURRENT_CONTEXT(ctx
);
952 struct gl_buffer_object
* bufObj
;
953 ASSERT_OUTSIDE_BEGIN_END(ctx
);
955 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
956 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
960 bufObj
= get_buffer(ctx
, target
);
962 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(target)" );
965 if (bufObj
->Name
== 0) {
966 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
970 *params
= bufObj
->Pointer
;