remove unused var
[mesa.git] / src / mesa / main / bufferobj.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26 /**
27 * \file bufferobj.c
28 * \brief Functions for the GL_ARB_vertex_buffer_object extension.
29 * \author Brian Paul, Ian Romanick
30 */
31
32
33 #include "glheader.h"
34 #include "hash.h"
35 #include "imports.h"
36 #include "image.h"
37 #include "context.h"
38 #include "bufferobj.h"
39
40
41 /**
42 * Get the buffer object bound to the specified target in a GL context.
43 *
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.
51 */
52 static INLINE struct gl_buffer_object *
53 buffer_object_get_target(GLcontext *ctx, GLenum target, const char *caller)
54 {
55 struct gl_buffer_object * bufObj = NULL;
56
57 switch (target) {
58 case GL_ARRAY_BUFFER_ARB:
59 bufObj = ctx->Array.ArrayBufferObj;
60 break;
61 case GL_ELEMENT_ARRAY_BUFFER_ARB:
62 bufObj = ctx->Array.ElementArrayBufferObj;
63 break;
64 case GL_PIXEL_PACK_BUFFER_EXT:
65 bufObj = ctx->Pack.BufferObj;
66 break;
67 case GL_PIXEL_UNPACK_BUFFER_EXT:
68 bufObj = ctx->Unpack.BufferObj;
69 break;
70 default:
71 _mesa_error(ctx, GL_INVALID_ENUM, "gl%s(target)", caller);
72 return NULL;
73 }
74
75 if (bufObj->Name == 0)
76 return NULL;
77
78 return bufObj;
79 }
80
81
82 /**
83 * Tests the subdata range parameters and sets the GL error code for
84 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
85 *
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
94 * are invalid.
95 *
96 * \sa glBufferSubDataARB, glGetBufferSubDataARB
97 */
98 static struct gl_buffer_object *
99 buffer_object_subdata_range_good( GLcontext * ctx, GLenum target,
100 GLintptrARB offset, GLsizeiptrARB size,
101 const char *caller )
102 {
103 struct gl_buffer_object *bufObj;
104
105 if (size < 0) {
106 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
107 return NULL;
108 }
109
110 if (offset < 0) {
111 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
112 return NULL;
113 }
114
115 bufObj = buffer_object_get_target(ctx, target, caller);
116 if (!bufObj || bufObj->Name == 0) {
117 return NULL;
118 }
119
120 if ((GLuint) (offset + size) > bufObj->Size) {
121 _mesa_error(ctx, GL_INVALID_VALUE,
122 "%s(size + offset > buffer size)", caller);
123 return NULL;
124 }
125
126 if (bufObj->Pointer) {
127 /* Buffer is currently mapped */
128 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
129 return NULL;
130 }
131
132 return bufObj;
133 }
134
135
136 /**
137 * Allocate and initialize a new buffer object.
138 *
139 * This function is intended to be called via
140 * \c dd_function_table::NewBufferObject.
141 */
142 struct gl_buffer_object *
143 _mesa_new_buffer_object( GLcontext *ctx, GLuint name, GLenum target )
144 {
145 struct gl_buffer_object *obj;
146
147 (void) ctx;
148
149 obj = MALLOC_STRUCT(gl_buffer_object);
150 _mesa_initialize_buffer_object(obj, name, target);
151 return obj;
152 }
153
154
155 /**
156 * Delete a buffer object.
157 *
158 * This function is intended to be called via
159 * \c dd_function_table::DeleteBuffer.
160 */
161 void
162 _mesa_delete_buffer_object( GLcontext *ctx, struct gl_buffer_object *bufObj )
163 {
164 (void) ctx;
165
166 if (bufObj->Data)
167 _mesa_free(bufObj->Data);
168 _mesa_free(bufObj);
169 }
170
171
172 void
173 _mesa_unbind_buffer_object( GLcontext *ctx, struct gl_buffer_object *bufObj )
174 {
175 if (bufObj != ctx->Array.NullBufferObj) {
176 bufObj->RefCount--;
177 if (bufObj->RefCount <= 0) {
178 ASSERT(ctx->Array.ArrayBufferObj != bufObj);
179 ASSERT(ctx->Array.ElementArrayBufferObj != bufObj);
180 ASSERT(ctx->Array.ArrayObj->Vertex.BufferObj != bufObj);
181 ASSERT(ctx->Driver.DeleteBuffer);
182 ctx->Driver.DeleteBuffer(ctx, bufObj);
183 }
184 }
185 }
186
187
188 /**
189 * Initialize a buffer object to default values.
190 */
191 void
192 _mesa_initialize_buffer_object( struct gl_buffer_object *obj,
193 GLuint name, GLenum target )
194 {
195 (void) target;
196
197 _mesa_bzero(obj, sizeof(struct gl_buffer_object));
198 obj->RefCount = 1;
199 obj->Name = name;
200 obj->Usage = GL_STATIC_DRAW_ARB;
201 obj->Access = GL_READ_WRITE_ARB;
202 }
203
204
205 /**
206 * Add the given buffer object to the buffer object pool.
207 */
208 void
209 _mesa_save_buffer_object( GLcontext *ctx, struct gl_buffer_object *obj )
210 {
211 if (obj->Name > 0) {
212 /* insert into hash table */
213 _mesa_HashInsert(ctx->Shared->BufferObjects, obj->Name, obj);
214 }
215 }
216
217
218 /**
219 * Remove the given buffer object from the buffer object pool.
220 * Do not deallocate the buffer object though.
221 */
222 void
223 _mesa_remove_buffer_object( GLcontext *ctx, struct gl_buffer_object *bufObj )
224 {
225 if (bufObj->Name > 0) {
226 /* remove from hash table */
227 _mesa_HashRemove(ctx->Shared->BufferObjects, bufObj->Name);
228 }
229 }
230
231
232 /**
233 * Allocate space for and store data in a buffer object. Any data that was
234 * previously stored in the buffer object is lost. If \c data is \c NULL,
235 * memory will be allocated, but no copy will occur.
236 *
237 * This function is intended to be called via
238 * \c dd_function_table::BufferData. This function need not set GL error
239 * codes. The input parameters will have been tested before calling.
240 *
241 * \param ctx GL context.
242 * \param target Buffer object target on which to operate.
243 * \param size Size, in bytes, of the new data store.
244 * \param data Pointer to the data to store in the buffer object. This
245 * pointer may be \c NULL.
246 * \param usage Hints about how the data will be used.
247 * \param bufObj Object to be used.
248 *
249 * \sa glBufferDataARB, dd_function_table::BufferData.
250 */
251 void
252 _mesa_buffer_data( GLcontext *ctx, GLenum target, GLsizeiptrARB size,
253 const GLvoid * data, GLenum usage,
254 struct gl_buffer_object * bufObj )
255 {
256 void * new_data;
257
258 (void) ctx; (void) target;
259
260 new_data = _mesa_realloc( bufObj->Data, bufObj->Size, size );
261 if (new_data) {
262 bufObj->Data = (GLubyte *) new_data;
263 bufObj->Size = size;
264 bufObj->Usage = usage;
265
266 if (data) {
267 _mesa_memcpy( bufObj->Data, data, size );
268 }
269 }
270 }
271
272
273 /**
274 * Replace data in a subrange of buffer object. If the data range
275 * specified by \c size + \c offset extends beyond the end of the buffer or
276 * if \c data is \c NULL, no copy is performed.
277 *
278 * This function is intended to be called by
279 * \c dd_function_table::BufferSubData. This function need not set GL error
280 * codes. The input parameters will have been tested before calling.
281 *
282 * \param ctx GL context.
283 * \param target Buffer object target on which to operate.
284 * \param offset Offset of the first byte to be modified.
285 * \param size Size, in bytes, of the data range.
286 * \param data Pointer to the data to store in the buffer object.
287 * \param bufObj Object to be used.
288 *
289 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
290 */
291 void
292 _mesa_buffer_subdata( GLcontext *ctx, GLenum target, GLintptrARB offset,
293 GLsizeiptrARB size, const GLvoid * data,
294 struct gl_buffer_object * bufObj )
295 {
296 (void) ctx; (void) target;
297
298 if (bufObj->Data && ((GLuint) (size + offset) <= bufObj->Size)) {
299 _mesa_memcpy( (GLubyte *) bufObj->Data + offset, data, size );
300 }
301 }
302
303
304 /**
305 * Retrieve data from a subrange of buffer object. If the data range
306 * specified by \c size + \c offset extends beyond the end of the buffer or
307 * if \c data is \c NULL, no copy is performed.
308 *
309 * This function is intended to be called by
310 * \c dd_function_table::BufferGetSubData. This function need not set GL error
311 * codes. The input parameters will have been tested before calling.
312 *
313 * \param ctx GL context.
314 * \param target Buffer object target on which to operate.
315 * \param offset Offset of the first byte to be modified.
316 * \param size Size, in bytes, of the data range.
317 * \param data Pointer to the data to store in the buffer object.
318 * \param bufObj Object to be used.
319 *
320 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
321 */
322 void
323 _mesa_buffer_get_subdata( GLcontext *ctx, GLenum target, GLintptrARB offset,
324 GLsizeiptrARB size, GLvoid * data,
325 struct gl_buffer_object * bufObj )
326 {
327 (void) ctx; (void) target;
328
329 if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
330 _mesa_memcpy( data, (GLubyte *) bufObj->Data + offset, size );
331 }
332 }
333
334
335 /**
336 * Fallback function called via ctx->Driver.MapBuffer().
337 * Hardware drivers that really implement buffer objects should never use
338 * function.
339 *
340 * The input parameters will have been already tested for errors.
341 *
342 * \param ctx GL context.
343 * \param target Buffer object target on which to operate.
344 * \param access Information about how the buffer will be accessed.
345 * \param bufObj Object to be mapped.
346 * \return A pointer to the object's internal data store that can be accessed
347 * by the processor
348 *
349 * \sa glMapBufferARB, dd_function_table::MapBuffer
350 */
351 void *
352 _mesa_buffer_map( GLcontext *ctx, GLenum target, GLenum access,
353 struct gl_buffer_object *bufObj )
354 {
355 (void) ctx;
356 (void) target;
357 (void) access;
358 ASSERT(!bufObj->OnCard);
359 /* Just return a direct pointer to the data */
360 if (bufObj->Pointer) {
361 /* already mapped! */
362 return NULL;
363 }
364 bufObj->Pointer = bufObj->Data;
365 return bufObj->Pointer;
366 }
367
368
369 /**
370 * Fallback function called via ctx->Driver.MapBuffer().
371 * Hardware drivers that really implement buffer objects should never use
372 * function.
373 *
374 * The input parameters will have been already tested for errors.
375 *
376 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
377 */
378 GLboolean
379 _mesa_buffer_unmap( GLcontext *ctx, GLenum target,
380 struct gl_buffer_object *bufObj )
381 {
382 (void) ctx;
383 (void) target;
384 ASSERT(!bufObj->OnCard);
385 /* XXX we might assert here that bufObj->Pointer is non-null */
386 bufObj->Pointer = NULL;
387 return GL_TRUE;
388 }
389
390
391 /**
392 * Initialize the state associated with buffer objects
393 */
394 void
395 _mesa_init_buffer_objects( GLcontext *ctx )
396 {
397 /* Allocate the default buffer object and set refcount so high that
398 * it never gets deleted.
399 */
400 ctx->Array.NullBufferObj = _mesa_new_buffer_object(ctx, 0, 0);
401 if (ctx->Array.NullBufferObj)
402 ctx->Array.NullBufferObj->RefCount = 1000;
403
404 ctx->Array.ArrayBufferObj = ctx->Array.NullBufferObj;
405 ctx->Array.ElementArrayBufferObj = ctx->Array.NullBufferObj;
406 }
407
408
409 /**
410 * When we're about to read pixel data out of a PBO (via glDrawPixels,
411 * glTexImage, etc) or write data into a PBO (via glReadPixels,
412 * glGetTexImage, etc) we call this function to check that we're not
413 * going to read out of bounds.
414 *
415 * XXX This would also be a convenient time to check that the PBO isn't
416 * currently mapped. Whoever calls this function should check for that.
417 * Remember, we can't use a PBO when it's mapped!
418 *
419 * \param width width of image to read/write
420 * \param height height of image to read/write
421 * \param depth depth of image to read/write
422 * \param format format of image to read/write
423 * \param type datatype of image to read/write
424 * \param ptr the user-provided pointer/offset
425 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
426 * go out of bounds.
427 */
428 GLboolean
429 _mesa_validate_pbo_access(GLuint dimensions,
430 const struct gl_pixelstore_attrib *pack,
431 GLsizei width, GLsizei height, GLsizei depth,
432 GLenum format, GLenum type, const GLvoid *ptr)
433 {
434 GLvoid *start, *end;
435 const GLubyte *sizeAddr; /* buffer size, cast to a pointer */
436
437 ASSERT(pack->BufferObj->Name != 0);
438
439 if (pack->BufferObj->Size == 0)
440 /* no buffer! */
441 return GL_FALSE;
442
443 /* get address of first pixel we'll read */
444 start = _mesa_image_address(dimensions, pack, ptr, width, height,
445 format, type, 0, 0, 0);
446
447 /* get address just past the last pixel we'll read */
448 end = _mesa_image_address(dimensions, pack, ptr, width, height,
449 format, type, depth-1, height-1, width);
450
451
452 sizeAddr = ((const GLubyte *) 0) + pack->BufferObj->Size;
453
454 if ((const GLubyte *) start > sizeAddr) {
455 /* This will catch negative values / wrap-around */
456 return GL_FALSE;
457 }
458 if ((const GLubyte *) end > sizeAddr) {
459 /* Image read goes beyond end of buffer */
460 return GL_FALSE;
461 }
462
463 /* OK! */
464 return GL_TRUE;
465 }
466
467
468 /**
469 * Return the gl_buffer_object for the given ID.
470 * Always return NULL for ID 0.
471 */
472 static INLINE struct gl_buffer_object *
473 lookup_bufferobj(GLcontext *ctx, GLuint buffer)
474 {
475 if (buffer == 0)
476 return NULL;
477 else
478 return (struct gl_buffer_object *)
479 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
480 }
481
482
483
484 /**********************************************************************/
485 /* API Functions */
486 /**********************************************************************/
487
488 void GLAPIENTRY
489 _mesa_BindBufferARB(GLenum target, GLuint buffer)
490 {
491 GET_CURRENT_CONTEXT(ctx);
492 struct gl_buffer_object *oldBufObj;
493 struct gl_buffer_object *newBufObj = NULL;
494 ASSERT_OUTSIDE_BEGIN_END(ctx);
495
496 oldBufObj = buffer_object_get_target( ctx, target, "BindBufferARB" );
497 if (oldBufObj && oldBufObj->Name == buffer)
498 return; /* rebinding the same buffer object- no change */
499
500 /*
501 * Get pointer to new buffer object (newBufObj)
502 */
503 if (buffer == 0) {
504 /* The spec says there's not a buffer object named 0, but we use
505 * one internally because it simplifies things.
506 */
507 newBufObj = ctx->Array.NullBufferObj;
508 }
509 else {
510 /* non-default buffer object */
511 newBufObj = lookup_bufferobj(ctx, buffer);
512 if (!newBufObj) {
513 /* if this is a new buffer object id, allocate a buffer object now */
514 ASSERT(ctx->Driver.NewBufferObject);
515 newBufObj = ctx->Driver.NewBufferObject(ctx, buffer, target);
516 if (!newBufObj) {
517 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindBufferARB");
518 return;
519 }
520 _mesa_save_buffer_object(ctx, newBufObj);
521 }
522 newBufObj->RefCount++;
523 }
524
525 switch (target) {
526 case GL_ARRAY_BUFFER_ARB:
527 ctx->Array.ArrayBufferObj = newBufObj;
528 break;
529 case GL_ELEMENT_ARRAY_BUFFER_ARB:
530 ctx->Array.ElementArrayBufferObj = newBufObj;
531 break;
532 case GL_PIXEL_PACK_BUFFER_EXT:
533 ctx->Pack.BufferObj = newBufObj;
534 break;
535 case GL_PIXEL_UNPACK_BUFFER_EXT:
536 ctx->Unpack.BufferObj = newBufObj;
537 break;
538 default:
539 _mesa_problem(ctx, "Bad target in _mesa_BindBufferARB");
540 return;
541 }
542
543 /* Pass BindBuffer call to device driver */
544 if (ctx->Driver.BindBuffer && newBufObj)
545 ctx->Driver.BindBuffer( ctx, target, newBufObj );
546
547 if (oldBufObj) {
548 oldBufObj->RefCount--;
549 assert(oldBufObj->RefCount >= 0);
550 if (oldBufObj->RefCount == 0) {
551 assert(oldBufObj->Name != 0);
552 ASSERT(ctx->Driver.DeleteBuffer);
553 ctx->Driver.DeleteBuffer( ctx, oldBufObj );
554 }
555 }
556 }
557
558
559 /**
560 * Delete a set of buffer objects.
561 *
562 * \param n Number of buffer objects to delete.
563 * \param ids Array of \c n buffer object IDs.
564 */
565 void GLAPIENTRY
566 _mesa_DeleteBuffersARB(GLsizei n, const GLuint *ids)
567 {
568 GET_CURRENT_CONTEXT(ctx);
569 GLsizei i;
570 ASSERT_OUTSIDE_BEGIN_END(ctx);
571
572 if (n < 0) {
573 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
574 return;
575 }
576
577 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
578
579 for (i = 0; i < n; i++) {
580 struct gl_buffer_object *bufObj = lookup_bufferobj(ctx, ids[i]);
581 if (bufObj) {
582 /* unbind any vertex pointers bound to this buffer */
583 GLuint j;
584
585 ASSERT(bufObj->Name == ids[i]);
586
587 if (ctx->Array.ArrayObj->Vertex.BufferObj == bufObj) {
588 bufObj->RefCount--;
589 ctx->Array.ArrayObj->Vertex.BufferObj = ctx->Array.NullBufferObj;
590 ctx->Array.NullBufferObj->RefCount++;
591 }
592 if (ctx->Array.ArrayObj->Normal.BufferObj == bufObj) {
593 bufObj->RefCount--;
594 ctx->Array.ArrayObj->Normal.BufferObj = ctx->Array.NullBufferObj;
595 ctx->Array.NullBufferObj->RefCount++;
596 }
597 if (ctx->Array.ArrayObj->Color.BufferObj == bufObj) {
598 bufObj->RefCount--;
599 ctx->Array.ArrayObj->Color.BufferObj = ctx->Array.NullBufferObj;
600 ctx->Array.NullBufferObj->RefCount++;
601 }
602 if (ctx->Array.ArrayObj->SecondaryColor.BufferObj == bufObj) {
603 bufObj->RefCount--;
604 ctx->Array.ArrayObj->SecondaryColor.BufferObj = ctx->Array.NullBufferObj;
605 ctx->Array.NullBufferObj->RefCount++;
606 }
607 if (ctx->Array.ArrayObj->FogCoord.BufferObj == bufObj) {
608 bufObj->RefCount--;
609 ctx->Array.ArrayObj->FogCoord.BufferObj = ctx->Array.NullBufferObj;
610 ctx->Array.NullBufferObj->RefCount++;
611 }
612 if (ctx->Array.ArrayObj->Index.BufferObj == bufObj) {
613 bufObj->RefCount--;
614 ctx->Array.ArrayObj->Index.BufferObj = ctx->Array.NullBufferObj;
615 ctx->Array.NullBufferObj->RefCount++;
616 }
617 if (ctx->Array.ArrayObj->EdgeFlag.BufferObj == bufObj) {
618 bufObj->RefCount--;
619 ctx->Array.ArrayObj->EdgeFlag.BufferObj = ctx->Array.NullBufferObj;
620 ctx->Array.NullBufferObj->RefCount++;
621 }
622 for (j = 0; j < MAX_TEXTURE_UNITS; j++) {
623 if (ctx->Array.ArrayObj->TexCoord[j].BufferObj == bufObj) {
624 bufObj->RefCount--;
625 ctx->Array.ArrayObj->TexCoord[j].BufferObj = ctx->Array.NullBufferObj;
626 ctx->Array.NullBufferObj->RefCount++;
627 }
628 }
629 for (j = 0; j < VERT_ATTRIB_MAX; j++) {
630 if (ctx->Array.ArrayObj->VertexAttrib[j].BufferObj == bufObj) {
631 bufObj->RefCount--;
632 ctx->Array.ArrayObj->VertexAttrib[j].BufferObj = ctx->Array.NullBufferObj;
633 ctx->Array.NullBufferObj->RefCount++;
634 }
635 }
636
637 if (ctx->Array.ArrayBufferObj == bufObj) {
638 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
639 }
640 if (ctx->Array.ElementArrayBufferObj == bufObj) {
641 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
642 }
643
644 if (ctx->Pack.BufferObj == bufObj) {
645 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 );
646 }
647 if (ctx->Unpack.BufferObj == bufObj) {
648 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
649 }
650
651 /* The ID is immediately freed for re-use */
652 _mesa_remove_buffer_object(ctx, bufObj);
653 _mesa_unbind_buffer_object(ctx, bufObj);
654 }
655 }
656
657 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
658 }
659
660
661 /**
662 * Generate a set of unique buffer object IDs and store them in \c buffer.
663 *
664 * \param n Number of IDs to generate.
665 * \param buffer Array of \c n locations to store the IDs.
666 */
667 void GLAPIENTRY
668 _mesa_GenBuffersARB(GLsizei n, GLuint *buffer)
669 {
670 GET_CURRENT_CONTEXT(ctx);
671 GLuint first;
672 GLint i;
673 ASSERT_OUTSIDE_BEGIN_END(ctx);
674
675 if (n < 0) {
676 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
677 return;
678 }
679
680 if (!buffer) {
681 return;
682 }
683
684 /*
685 * This must be atomic (generation and allocation of buffer object IDs)
686 */
687 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
688
689 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
690
691 /* Allocate new, empty buffer objects and return identifiers */
692 for (i = 0; i < n; i++) {
693 struct gl_buffer_object *bufObj;
694 GLuint name = first + i;
695 GLenum target = 0;
696 bufObj = ctx->Driver.NewBufferObject( ctx, name, target );
697 if (!bufObj) {
698 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
699 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenBuffersARB");
700 return;
701 }
702 _mesa_save_buffer_object(ctx, bufObj);
703 buffer[i] = first + i;
704 }
705
706 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
707 }
708
709
710 /**
711 * Determine if ID is the name of a buffer object.
712 *
713 * \param id ID of the potential buffer object.
714 * \return \c GL_TRUE if \c id is the name of a buffer object,
715 * \c GL_FALSE otherwise.
716 */
717 GLboolean GLAPIENTRY
718 _mesa_IsBufferARB(GLuint id)
719 {
720 struct gl_buffer_object *bufObj;
721 GET_CURRENT_CONTEXT(ctx);
722 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
723
724 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
725 bufObj = lookup_bufferobj(ctx, id);
726 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
727
728 return bufObj ? GL_TRUE : GL_FALSE;
729 }
730
731
732 void GLAPIENTRY
733 _mesa_BufferDataARB(GLenum target, GLsizeiptrARB size,
734 const GLvoid * data, GLenum usage)
735 {
736 GET_CURRENT_CONTEXT(ctx);
737 struct gl_buffer_object *bufObj;
738 ASSERT_OUTSIDE_BEGIN_END(ctx);
739
740 if (size < 0) {
741 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
742 return;
743 }
744
745 switch (usage) {
746 case GL_STREAM_DRAW_ARB:
747 case GL_STREAM_READ_ARB:
748 case GL_STREAM_COPY_ARB:
749 case GL_STATIC_DRAW_ARB:
750 case GL_STATIC_READ_ARB:
751 case GL_STATIC_COPY_ARB:
752 case GL_DYNAMIC_DRAW_ARB:
753 case GL_DYNAMIC_READ_ARB:
754 case GL_DYNAMIC_COPY_ARB:
755 /* OK */
756 break;
757 default:
758 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(usage)");
759 return;
760 }
761
762 bufObj = buffer_object_get_target( ctx, target, "BufferDataARB" );
763 if (!bufObj || bufObj->Name ==0) {
764 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferDataARB" );
765 return;
766 }
767
768 if (bufObj->Pointer) {
769 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferDataARB(buffer is mapped)" );
770 return;
771 }
772
773 ASSERT(ctx->Driver.BufferData);
774
775 /* Give the buffer object to the driver! <data> may be null! */
776 ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj );
777 }
778
779
780 void GLAPIENTRY
781 _mesa_BufferSubDataARB(GLenum target, GLintptrARB offset,
782 GLsizeiptrARB size, const GLvoid * data)
783 {
784 GET_CURRENT_CONTEXT(ctx);
785 struct gl_buffer_object *bufObj;
786 ASSERT_OUTSIDE_BEGIN_END(ctx);
787
788 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
789 "BufferSubDataARB" );
790 if (!bufObj) {
791 /* error already recorded */
792 return;
793 }
794
795 ASSERT(ctx->Driver.BufferSubData);
796 ctx->Driver.BufferSubData( ctx, target, offset, size, data, bufObj );
797 }
798
799
800 void GLAPIENTRY
801 _mesa_GetBufferSubDataARB(GLenum target, GLintptrARB offset,
802 GLsizeiptrARB size, void * data)
803 {
804 GET_CURRENT_CONTEXT(ctx);
805 struct gl_buffer_object *bufObj;
806 ASSERT_OUTSIDE_BEGIN_END(ctx);
807
808 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
809 "GetBufferSubDataARB" );
810 if (!bufObj) {
811 /* error already recorded */
812 return;
813 }
814
815 ASSERT(ctx->Driver.GetBufferSubData);
816 ctx->Driver.GetBufferSubData( ctx, target, offset, size, data, bufObj );
817 }
818
819
820 void * GLAPIENTRY
821 _mesa_MapBufferARB(GLenum target, GLenum access)
822 {
823 GET_CURRENT_CONTEXT(ctx);
824 struct gl_buffer_object * bufObj;
825 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
826
827 switch (access) {
828 case GL_READ_ONLY_ARB:
829 case GL_WRITE_ONLY_ARB:
830 case GL_READ_WRITE_ARB:
831 /* OK */
832 break;
833 default:
834 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
835 return NULL;
836 }
837
838 bufObj = buffer_object_get_target( ctx, target, "MapBufferARB" );
839 if (!bufObj || bufObj->Name == 0) {
840 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB" );
841 return NULL;
842 }
843
844 if (bufObj->Pointer) {
845 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
846 return NULL;
847 }
848
849 ASSERT(ctx->Driver.MapBuffer);
850 bufObj->Pointer = ctx->Driver.MapBuffer( ctx, target, access, bufObj );
851 if (!bufObj->Pointer) {
852 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(access)");
853 }
854
855 bufObj->Access = access;
856
857 return bufObj->Pointer;
858 }
859
860
861 GLboolean GLAPIENTRY
862 _mesa_UnmapBufferARB(GLenum target)
863 {
864 GET_CURRENT_CONTEXT(ctx);
865 struct gl_buffer_object *bufObj;
866 GLboolean status = GL_TRUE;
867 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
868
869 bufObj = buffer_object_get_target( ctx, target, "UnmapBufferARB" );
870 if (!bufObj || bufObj->Name == 0) {
871 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB" );
872 return GL_FALSE;
873 }
874
875 if (!bufObj->Pointer) {
876 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
877 return GL_FALSE;
878 }
879
880 if (ctx->Driver.UnmapBuffer) {
881 status = ctx->Driver.UnmapBuffer( ctx, target, bufObj );
882 }
883
884 bufObj->Access = GL_READ_WRITE_ARB; /* initial value, OK? */
885 bufObj->Pointer = NULL;
886
887 return status;
888 }
889
890
891 void GLAPIENTRY
892 _mesa_GetBufferParameterivARB(GLenum target, GLenum pname, GLint *params)
893 {
894 GET_CURRENT_CONTEXT(ctx);
895 struct gl_buffer_object *bufObj;
896 ASSERT_OUTSIDE_BEGIN_END(ctx);
897
898 bufObj = buffer_object_get_target( ctx, target, "GetBufferParameterivARB" );
899 if (!bufObj || bufObj->Name == 0) {
900 _mesa_error(ctx, GL_INVALID_OPERATION, "GetBufferParameterivARB" );
901 return;
902 }
903
904 switch (pname) {
905 case GL_BUFFER_SIZE_ARB:
906 *params = (GLint) bufObj->Size;
907 break;
908 case GL_BUFFER_USAGE_ARB:
909 *params = bufObj->Usage;
910 break;
911 case GL_BUFFER_ACCESS_ARB:
912 *params = bufObj->Access;
913 break;
914 case GL_BUFFER_MAPPED_ARB:
915 *params = (bufObj->Pointer != NULL);
916 break;
917 default:
918 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname)");
919 return;
920 }
921 }
922
923
924 void GLAPIENTRY
925 _mesa_GetBufferPointervARB(GLenum target, GLenum pname, GLvoid **params)
926 {
927 GET_CURRENT_CONTEXT(ctx);
928 struct gl_buffer_object * bufObj;
929 ASSERT_OUTSIDE_BEGIN_END(ctx);
930
931 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
932 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
933 return;
934 }
935
936 bufObj = buffer_object_get_target( ctx, target, "GetBufferPointervARB" );
937 if (!bufObj || bufObj->Name == 0) {
938 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferPointervARB" );
939 return;
940 }
941
942 *params = bufObj->Pointer;
943 }