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 * \param caller Name of calling function for recording errors.
48 * \return A pointer to the buffer object bound to \c target in the
49 * specified context or \c NULL if \c target is invalid or no
50 * buffer object is bound.
52 static INLINE
struct gl_buffer_object
*
53 buffer_object_get_target(GLcontext
*ctx
, GLenum target
, const char *caller
)
55 struct gl_buffer_object
* bufObj
= NULL
;
58 case GL_ARRAY_BUFFER_ARB
:
59 bufObj
= ctx
->Array
.ArrayBufferObj
;
61 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
62 bufObj
= ctx
->Array
.ElementArrayBufferObj
;
64 case GL_PIXEL_PACK_BUFFER_EXT
:
65 bufObj
= ctx
->Pack
.BufferObj
;
67 case GL_PIXEL_UNPACK_BUFFER_EXT
:
68 bufObj
= ctx
->Unpack
.BufferObj
;
71 _mesa_error(ctx
, GL_INVALID_ENUM
, "gl%s(target)", caller
);
75 if (bufObj
->Name
== 0)
83 * Tests the subdata range parameters and sets the GL error code for
84 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
86 * \param ctx GL context.
87 * \param target Buffer object target on which to operate.
88 * \param offset Offset of the first byte of the subdata range.
89 * \param size Size, in bytes, of the subdata range.
90 * \param caller Name of calling function for recording errors.
91 * \return A pointer to the buffer object bound to \c target in the
92 * specified context or \c NULL if any of the parameter or state
93 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
96 * \sa glBufferSubDataARB, glGetBufferSubDataARB
98 static struct gl_buffer_object
*
99 buffer_object_subdata_range_good( GLcontext
* ctx
, GLenum target
,
100 GLintptrARB offset
, GLsizeiptrARB size
,
103 struct gl_buffer_object
*bufObj
;
106 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
111 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
115 bufObj
= buffer_object_get_target(ctx
, target
, caller
);
116 if (!bufObj
|| bufObj
->Name
== 0) {
120 if ((GLuint
) (offset
+ size
) > bufObj
->Size
) {
121 _mesa_error(ctx
, GL_INVALID_VALUE
,
122 "%s(size + offset > buffer size)", caller
);
126 if (bufObj
->Pointer
) {
127 /* Buffer is currently mapped */
128 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
137 * Allocate and initialize a new buffer object.
139 * This function is intended to be called via
140 * \c dd_function_table::NewBufferObject.
142 struct gl_buffer_object
*
143 _mesa_new_buffer_object( GLcontext
*ctx
, GLuint name
, GLenum target
)
145 struct gl_buffer_object
*obj
;
149 obj
= MALLOC_STRUCT(gl_buffer_object
);
150 _mesa_initialize_buffer_object(obj
, name
, target
);
156 * Delete a buffer object.
158 * This function is intended to be called via
159 * \c dd_function_table::DeleteBuffer.
162 _mesa_delete_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*bufObj
)
167 _mesa_free(bufObj
->Data
);
173 * Initialize a buffer object to default values.
176 _mesa_initialize_buffer_object( struct gl_buffer_object
*obj
,
177 GLuint name
, GLenum target
)
181 _mesa_bzero(obj
, sizeof(struct gl_buffer_object
));
184 obj
->Usage
= GL_STATIC_DRAW_ARB
;
185 obj
->Access
= GL_READ_WRITE_ARB
;
190 * Add the given buffer object to the buffer object pool.
193 _mesa_save_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*obj
)
196 /* insert into hash table */
197 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, obj
->Name
, obj
);
203 * Remove the given buffer object from the buffer object pool.
204 * Do not deallocate the buffer object though.
207 _mesa_remove_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*bufObj
)
209 if (bufObj
->Name
> 0) {
210 /* remove from hash table */
211 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, bufObj
->Name
);
217 * Allocate space for and store data in a buffer object. Any data that was
218 * previously stored in the buffer object is lost. If \c data is \c NULL,
219 * memory will be allocated, but no copy will occur.
221 * This function is intended to be called via
222 * \c dd_function_table::BufferData. This function need not set GL error
223 * codes. The input parameters will have been tested before calling.
225 * \param ctx GL context.
226 * \param target Buffer object target on which to operate.
227 * \param size Size, in bytes, of the new data store.
228 * \param data Pointer to the data to store in the buffer object. This
229 * pointer may be \c NULL.
230 * \param usage Hints about how the data will be used.
231 * \param bufObj Object to be used.
233 * \sa glBufferDataARB, dd_function_table::BufferData.
236 _mesa_buffer_data( GLcontext
*ctx
, GLenum target
, GLsizeiptrARB size
,
237 const GLvoid
* data
, GLenum usage
,
238 struct gl_buffer_object
* bufObj
)
242 (void) ctx
; (void) target
;
244 new_data
= _mesa_realloc( bufObj
->Data
, bufObj
->Size
, size
);
246 bufObj
->Data
= (GLubyte
*) new_data
;
248 bufObj
->Usage
= usage
;
251 _mesa_memcpy( bufObj
->Data
, data
, size
);
258 * Replace data in a subrange of buffer object. If the data range
259 * specified by \c size + \c offset extends beyond the end of the buffer or
260 * if \c data is \c NULL, no copy is performed.
262 * This function is intended to be called by
263 * \c dd_function_table::BufferSubData. This function need not set GL error
264 * codes. The input parameters will have been tested before calling.
266 * \param ctx GL context.
267 * \param target Buffer object target on which to operate.
268 * \param offset Offset of the first byte to be modified.
269 * \param size Size, in bytes, of the data range.
270 * \param data Pointer to the data to store in the buffer object.
271 * \param bufObj Object to be used.
273 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
276 _mesa_buffer_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
277 GLsizeiptrARB size
, const GLvoid
* data
,
278 struct gl_buffer_object
* bufObj
)
280 (void) ctx
; (void) target
;
282 if (bufObj
->Data
&& ((GLuint
) (size
+ offset
) <= bufObj
->Size
)) {
283 _mesa_memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
289 * Retrieve data from a subrange of buffer object. If the data range
290 * specified by \c size + \c offset extends beyond the end of the buffer or
291 * if \c data is \c NULL, no copy is performed.
293 * This function is intended to be called by
294 * \c dd_function_table::BufferGetSubData. This function need not set GL error
295 * codes. The input parameters will have been tested before calling.
297 * \param ctx GL context.
298 * \param target Buffer object target on which to operate.
299 * \param offset Offset of the first byte to be modified.
300 * \param size Size, in bytes, of the data range.
301 * \param data Pointer to the data to store in the buffer object.
302 * \param bufObj Object to be used.
304 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
307 _mesa_buffer_get_subdata( GLcontext
*ctx
, GLenum target
, GLintptrARB offset
,
308 GLsizeiptrARB size
, GLvoid
* data
,
309 struct gl_buffer_object
* bufObj
)
311 (void) ctx
; (void) target
;
313 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
314 _mesa_memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
320 * Fallback function called via ctx->Driver.MapBuffer().
321 * Hardware drivers that really implement buffer objects should never use
324 * The input parameters will have been already tested for errors.
326 * \param ctx GL context.
327 * \param target Buffer object target on which to operate.
328 * \param access Information about how the buffer will be accessed.
329 * \param bufObj Object to be mapped.
330 * \return A pointer to the object's internal data store that can be accessed
333 * \sa glMapBufferARB, dd_function_table::MapBuffer
336 _mesa_buffer_map( GLcontext
*ctx
, GLenum target
, GLenum access
,
337 struct gl_buffer_object
*bufObj
)
342 ASSERT(!bufObj
->OnCard
);
343 /* Just return a direct pointer to the data */
344 if (bufObj
->Pointer
) {
345 /* already mapped! */
348 bufObj
->Pointer
= bufObj
->Data
;
349 return bufObj
->Pointer
;
354 * Fallback function called via ctx->Driver.MapBuffer().
355 * Hardware drivers that really implement buffer objects should never use
358 * The input parameters will have been already tested for errors.
360 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
363 _mesa_buffer_unmap( GLcontext
*ctx
, GLenum target
,
364 struct gl_buffer_object
*bufObj
)
368 ASSERT(!bufObj
->OnCard
);
369 /* XXX we might assert here that bufObj->Pointer is non-null */
370 bufObj
->Pointer
= NULL
;
376 * Initialize the state associated with buffer objects
379 _mesa_init_buffer_objects( GLcontext
*ctx
)
383 /* Allocate the default buffer object and set refcount so high that
384 * it never gets deleted.
386 ctx
->Array
.NullBufferObj
= _mesa_new_buffer_object(ctx
, 0, 0);
387 if (ctx
->Array
.NullBufferObj
)
388 ctx
->Array
.NullBufferObj
->RefCount
= 1000;
390 ctx
->Array
.ArrayBufferObj
= ctx
->Array
.NullBufferObj
;
391 ctx
->Array
.ElementArrayBufferObj
= ctx
->Array
.NullBufferObj
;
393 /* Vertex array buffers */
394 ctx
->Array
.Vertex
.BufferObj
= ctx
->Array
.NullBufferObj
;
395 ctx
->Array
.Normal
.BufferObj
= ctx
->Array
.NullBufferObj
;
396 ctx
->Array
.Color
.BufferObj
= ctx
->Array
.NullBufferObj
;
397 ctx
->Array
.SecondaryColor
.BufferObj
= ctx
->Array
.NullBufferObj
;
398 ctx
->Array
.FogCoord
.BufferObj
= ctx
->Array
.NullBufferObj
;
399 ctx
->Array
.Index
.BufferObj
= ctx
->Array
.NullBufferObj
;
400 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
401 ctx
->Array
.TexCoord
[i
].BufferObj
= ctx
->Array
.NullBufferObj
;
403 ctx
->Array
.EdgeFlag
.BufferObj
= ctx
->Array
.NullBufferObj
;
404 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
405 ctx
->Array
.VertexAttrib
[i
].BufferObj
= ctx
->Array
.NullBufferObj
;
411 * When we're about to read pixel data out of a PBO (via glDrawPixels,
412 * glTexImage, etc) or write data into a PBO (via glReadPixels,
413 * glGetTexImage, etc) we call this function to check that we're not
414 * going to read out of bounds.
416 * XXX This would also be a convenient time to check that the PBO isn't
417 * currently mapped. Whoever calls this function should check for that.
418 * Remember, we can't use a PBO when it's mapped!
420 * \param width width of image to read/write
421 * \param height height of image to read/write
422 * \param depth depth of image to read/write
423 * \param format format of image to read/write
424 * \param type datatype of image to read/write
425 * \param ptr the user-provided pointer/offset
426 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
430 _mesa_validate_pbo_access(GLuint dimensions
,
431 const struct gl_pixelstore_attrib
*pack
,
432 GLsizei width
, GLsizei height
, GLsizei depth
,
433 GLenum format
, GLenum type
, const GLvoid
*ptr
)
436 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
438 ASSERT(pack
->BufferObj
->Name
!= 0);
440 if (pack
->BufferObj
->Size
== 0)
444 /* get address of first pixel we'll read */
445 start
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
446 format
, type
, 0, 0, 0);
448 /* get address just past the last pixel we'll read */
449 end
= _mesa_image_address(dimensions
, pack
, ptr
, width
, height
,
450 format
, type
, depth
-1, height
-1, width
);
453 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
455 if ((const GLubyte
*) start
> sizeAddr
) {
456 /* This will catch negative values / wrap-around */
459 if ((const GLubyte
*) end
> sizeAddr
) {
460 /* Image read goes beyond end of buffer */
470 * Return the gl_buffer_object for the given ID.
471 * Always return NULL for ID 0.
473 static INLINE
struct gl_buffer_object
*
474 lookup_bufferobj(GLcontext
*ctx
, GLuint buffer
)
479 return (struct gl_buffer_object
*)
480 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
485 /**********************************************************************/
487 /**********************************************************************/
490 _mesa_BindBufferARB(GLenum target
, GLuint buffer
)
492 GET_CURRENT_CONTEXT(ctx
);
493 struct gl_buffer_object
*oldBufObj
;
494 struct gl_buffer_object
*newBufObj
= NULL
;
495 ASSERT_OUTSIDE_BEGIN_END(ctx
);
497 oldBufObj
= buffer_object_get_target( ctx
, target
, "BindBufferARB" );
498 if (oldBufObj
&& oldBufObj
->Name
== buffer
)
499 return; /* rebinding the same buffer object- no change */
502 * Get pointer to new buffer object (newBufObj)
505 /* The spec says there's not a buffer object named 0, but we use
506 * one internally because it simplifies things.
508 newBufObj
= ctx
->Array
.NullBufferObj
;
511 /* non-default buffer object */
512 newBufObj
= lookup_bufferobj(ctx
, buffer
);
514 /* if this is a new buffer object id, allocate a buffer object now */
515 ASSERT(ctx
->Driver
.NewBufferObject
);
516 newBufObj
= ctx
->Driver
.NewBufferObject(ctx
, buffer
, target
);
518 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindBufferARB");
521 _mesa_save_buffer_object(ctx
, newBufObj
);
523 newBufObj
->RefCount
++;
527 case GL_ARRAY_BUFFER_ARB
:
528 ctx
->Array
.ArrayBufferObj
= newBufObj
;
530 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
531 ctx
->Array
.ElementArrayBufferObj
= newBufObj
;
533 case GL_PIXEL_PACK_BUFFER_EXT
:
534 ctx
->Pack
.BufferObj
= newBufObj
;
536 case GL_PIXEL_UNPACK_BUFFER_EXT
:
537 ctx
->Unpack
.BufferObj
= newBufObj
;
540 _mesa_problem(ctx
, "Bad target in _mesa_BindBufferARB");
544 /* Pass BindBuffer call to device driver */
545 if (ctx
->Driver
.BindBuffer
&& newBufObj
)
546 ctx
->Driver
.BindBuffer( ctx
, target
, newBufObj
);
549 oldBufObj
->RefCount
--;
550 assert(oldBufObj
->RefCount
>= 0);
551 if (oldBufObj
->RefCount
== 0) {
552 assert(oldBufObj
->Name
!= 0);
553 ASSERT(ctx
->Driver
.DeleteBuffer
);
554 ctx
->Driver
.DeleteBuffer( ctx
, oldBufObj
);
561 * Delete a set of buffer objects.
563 * \param n Number of buffer objects to delete.
564 * \param ids Array of \c n buffer object IDs.
567 _mesa_DeleteBuffersARB(GLsizei n
, const GLuint
*ids
)
569 GET_CURRENT_CONTEXT(ctx
);
571 ASSERT_OUTSIDE_BEGIN_END(ctx
);
574 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
578 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
580 for (i
= 0; i
< n
; i
++) {
581 struct gl_buffer_object
*bufObj
= lookup_bufferobj(ctx
, ids
[i
]);
583 /* unbind any vertex pointers bound to this buffer */
586 ASSERT(bufObj
->Name
== ids
[i
]);
588 if (ctx
->Array
.Vertex
.BufferObj
== bufObj
) {
590 ctx
->Array
.Vertex
.BufferObj
= ctx
->Array
.NullBufferObj
;
591 ctx
->Array
.NullBufferObj
->RefCount
++;
593 if (ctx
->Array
.Normal
.BufferObj
== bufObj
) {
595 ctx
->Array
.Normal
.BufferObj
= ctx
->Array
.NullBufferObj
;
596 ctx
->Array
.NullBufferObj
->RefCount
++;
598 if (ctx
->Array
.Color
.BufferObj
== bufObj
) {
600 ctx
->Array
.Color
.BufferObj
= ctx
->Array
.NullBufferObj
;
601 ctx
->Array
.NullBufferObj
->RefCount
++;
603 if (ctx
->Array
.SecondaryColor
.BufferObj
== bufObj
) {
605 ctx
->Array
.SecondaryColor
.BufferObj
= ctx
->Array
.NullBufferObj
;
606 ctx
->Array
.NullBufferObj
->RefCount
++;
608 if (ctx
->Array
.FogCoord
.BufferObj
== bufObj
) {
610 ctx
->Array
.FogCoord
.BufferObj
= ctx
->Array
.NullBufferObj
;
611 ctx
->Array
.NullBufferObj
->RefCount
++;
613 if (ctx
->Array
.Index
.BufferObj
== bufObj
) {
615 ctx
->Array
.Index
.BufferObj
= ctx
->Array
.NullBufferObj
;
616 ctx
->Array
.NullBufferObj
->RefCount
++;
618 if (ctx
->Array
.EdgeFlag
.BufferObj
== bufObj
) {
620 ctx
->Array
.EdgeFlag
.BufferObj
= ctx
->Array
.NullBufferObj
;
621 ctx
->Array
.NullBufferObj
->RefCount
++;
623 for (j
= 0; j
< MAX_TEXTURE_UNITS
; j
++) {
624 if (ctx
->Array
.TexCoord
[j
].BufferObj
== bufObj
) {
626 ctx
->Array
.TexCoord
[j
].BufferObj
= ctx
->Array
.NullBufferObj
;
627 ctx
->Array
.NullBufferObj
->RefCount
++;
630 for (j
= 0; j
< VERT_ATTRIB_MAX
; j
++) {
631 if (ctx
->Array
.VertexAttrib
[j
].BufferObj
== bufObj
) {
633 ctx
->Array
.VertexAttrib
[j
].BufferObj
= ctx
->Array
.NullBufferObj
;
634 ctx
->Array
.NullBufferObj
->RefCount
++;
638 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
639 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB
, 0 );
641 if (ctx
->Array
.ElementArrayBufferObj
== bufObj
) {
642 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
645 if (ctx
->Pack
.BufferObj
== bufObj
) {
646 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
648 if (ctx
->Unpack
.BufferObj
== bufObj
) {
649 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
652 /* The ID is immediately freed for re-use */
653 _mesa_remove_buffer_object(ctx
, bufObj
);
655 if (bufObj
->RefCount
<= 0) {
656 ASSERT(ctx
->Array
.ArrayBufferObj
!= bufObj
);
657 ASSERT(ctx
->Array
.ElementArrayBufferObj
!= bufObj
);
658 ASSERT(ctx
->Array
.Vertex
.BufferObj
!= bufObj
);
659 ASSERT(ctx
->Driver
.DeleteBuffer
);
660 ctx
->Driver
.DeleteBuffer(ctx
, bufObj
);
665 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
670 * Generate a set of unique buffer object IDs and store them in \c buffer.
672 * \param n Number of IDs to generate.
673 * \param buffer Array of \c n locations to store the IDs.
676 _mesa_GenBuffersARB(GLsizei n
, GLuint
*buffer
)
678 GET_CURRENT_CONTEXT(ctx
);
681 ASSERT_OUTSIDE_BEGIN_END(ctx
);
684 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenBuffersARB");
693 * This must be atomic (generation and allocation of buffer object IDs)
695 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
697 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
699 /* Allocate new, empty buffer objects and return identifiers */
700 for (i
= 0; i
< n
; i
++) {
701 struct gl_buffer_object
*bufObj
;
702 GLuint name
= first
+ i
;
704 bufObj
= ctx
->Driver
.NewBufferObject( ctx
, name
, target
);
706 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
707 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenBuffersARB");
710 _mesa_save_buffer_object(ctx
, bufObj
);
711 buffer
[i
] = first
+ i
;
714 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
719 * Determine if ID is the name of a buffer object.
721 * \param id ID of the potential buffer object.
722 * \return \c GL_TRUE if \c id is the name of a buffer object,
723 * \c GL_FALSE otherwise.
726 _mesa_IsBufferARB(GLuint id
)
728 struct gl_buffer_object
*bufObj
;
729 GET_CURRENT_CONTEXT(ctx
);
730 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
732 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
733 bufObj
= lookup_bufferobj(ctx
, id
);
734 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
736 return bufObj
? GL_TRUE
: GL_FALSE
;
741 _mesa_BufferDataARB(GLenum target
, GLsizeiptrARB size
,
742 const GLvoid
* data
, GLenum usage
)
744 GET_CURRENT_CONTEXT(ctx
);
745 struct gl_buffer_object
*bufObj
;
746 ASSERT_OUTSIDE_BEGIN_END(ctx
);
749 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBufferDataARB(size < 0)");
754 case GL_STREAM_DRAW_ARB
:
755 case GL_STREAM_READ_ARB
:
756 case GL_STREAM_COPY_ARB
:
757 case GL_STATIC_DRAW_ARB
:
758 case GL_STATIC_READ_ARB
:
759 case GL_STATIC_COPY_ARB
:
760 case GL_DYNAMIC_DRAW_ARB
:
761 case GL_DYNAMIC_READ_ARB
:
762 case GL_DYNAMIC_COPY_ARB
:
766 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBufferDataARB(usage)");
770 bufObj
= buffer_object_get_target( ctx
, target
, "BufferDataARB" );
771 if (!bufObj
|| bufObj
->Name
==0) {
772 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB" );
776 if (bufObj
->Pointer
) {
777 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBufferDataARB(buffer is mapped)" );
781 ASSERT(ctx
->Driver
.BufferData
);
783 /* Give the buffer object to the driver! <data> may be null! */
784 ctx
->Driver
.BufferData( ctx
, target
, size
, data
, usage
, bufObj
);
789 _mesa_BufferSubDataARB(GLenum target
, GLintptrARB offset
,
790 GLsizeiptrARB size
, const GLvoid
* data
)
792 GET_CURRENT_CONTEXT(ctx
);
793 struct gl_buffer_object
*bufObj
;
794 ASSERT_OUTSIDE_BEGIN_END(ctx
);
796 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
797 "BufferSubDataARB" );
799 /* error already recorded */
803 ASSERT(ctx
->Driver
.BufferSubData
);
804 ctx
->Driver
.BufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
809 _mesa_GetBufferSubDataARB(GLenum target
, GLintptrARB offset
,
810 GLsizeiptrARB size
, void * data
)
812 GET_CURRENT_CONTEXT(ctx
);
813 struct gl_buffer_object
*bufObj
;
814 ASSERT_OUTSIDE_BEGIN_END(ctx
);
816 bufObj
= buffer_object_subdata_range_good( ctx
, target
, offset
, size
,
817 "GetBufferSubDataARB" );
819 /* error already recorded */
823 ASSERT(ctx
->Driver
.GetBufferSubData
);
824 ctx
->Driver
.GetBufferSubData( ctx
, target
, offset
, size
, data
, bufObj
);
829 _mesa_MapBufferARB(GLenum target
, GLenum access
)
831 GET_CURRENT_CONTEXT(ctx
);
832 struct gl_buffer_object
* bufObj
;
833 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
836 case GL_READ_ONLY_ARB
:
837 case GL_WRITE_ONLY_ARB
:
838 case GL_READ_WRITE_ARB
:
842 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBufferARB(access)");
846 bufObj
= buffer_object_get_target( ctx
, target
, "MapBufferARB" );
847 if (!bufObj
|| bufObj
->Name
== 0) {
848 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB" );
852 if (bufObj
->Pointer
) {
853 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMapBufferARB(already mapped)");
857 ASSERT(ctx
->Driver
.MapBuffer
);
858 bufObj
->Pointer
= ctx
->Driver
.MapBuffer( ctx
, target
, access
, bufObj
);
859 if (!bufObj
->Pointer
) {
860 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMapBufferARB(access)");
863 bufObj
->Access
= access
;
865 return bufObj
->Pointer
;
870 _mesa_UnmapBufferARB(GLenum target
)
872 GET_CURRENT_CONTEXT(ctx
);
873 struct gl_buffer_object
*bufObj
;
874 GLboolean status
= GL_TRUE
;
875 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
877 bufObj
= buffer_object_get_target( ctx
, target
, "UnmapBufferARB" );
878 if (!bufObj
|| bufObj
->Name
== 0) {
879 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB" );
883 if (!bufObj
->Pointer
) {
884 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glUnmapBufferARB");
888 if (ctx
->Driver
.UnmapBuffer
) {
889 status
= ctx
->Driver
.UnmapBuffer( ctx
, target
, bufObj
);
892 bufObj
->Access
= GL_READ_WRITE_ARB
; /* initial value, OK? */
893 bufObj
->Pointer
= NULL
;
900 _mesa_GetBufferParameterivARB(GLenum target
, GLenum pname
, GLint
*params
)
902 GET_CURRENT_CONTEXT(ctx
);
903 struct gl_buffer_object
*bufObj
;
904 ASSERT_OUTSIDE_BEGIN_END(ctx
);
906 bufObj
= buffer_object_get_target( ctx
, target
, "GetBufferParameterivARB" );
907 if (!bufObj
|| bufObj
->Name
== 0) {
908 _mesa_error(ctx
, GL_INVALID_OPERATION
, "GetBufferParameterivARB" );
913 case GL_BUFFER_SIZE_ARB
:
914 *params
= (GLint
) bufObj
->Size
;
916 case GL_BUFFER_USAGE_ARB
:
917 *params
= bufObj
->Usage
;
919 case GL_BUFFER_ACCESS_ARB
:
920 *params
= bufObj
->Access
;
922 case GL_BUFFER_MAPPED_ARB
:
923 *params
= (bufObj
->Pointer
!= NULL
);
926 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferParameterivARB(pname)");
933 _mesa_GetBufferPointervARB(GLenum target
, GLenum pname
, GLvoid
**params
)
935 GET_CURRENT_CONTEXT(ctx
);
936 struct gl_buffer_object
* bufObj
;
937 ASSERT_OUTSIDE_BEGIN_END(ctx
);
939 if (pname
!= GL_BUFFER_MAP_POINTER_ARB
) {
940 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointervARB(pname)");
944 bufObj
= buffer_object_get_target( ctx
, target
, "GetBufferPointervARB" );
945 if (!bufObj
|| bufObj
->Name
== 0) {
946 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetBufferPointervARB" );
950 *params
= bufObj
->Pointer
;