only use __x86_64__, not __amd64__ (bug 15503)
[mesa.git] / src / mesa / main / bufferobj.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 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 * \return A pointer to the buffer object bound to \c target in the
48 * specified context or \c NULL if \c target is invalid.
49 */
50 static INLINE struct gl_buffer_object *
51 get_buffer(GLcontext *ctx, GLenum target)
52 {
53 struct gl_buffer_object * bufObj = NULL;
54
55 switch (target) {
56 case GL_ARRAY_BUFFER_ARB:
57 bufObj = ctx->Array.ArrayBufferObj;
58 break;
59 case GL_ELEMENT_ARRAY_BUFFER_ARB:
60 bufObj = ctx->Array.ElementArrayBufferObj;
61 break;
62 case GL_PIXEL_PACK_BUFFER_EXT:
63 bufObj = ctx->Pack.BufferObj;
64 break;
65 case GL_PIXEL_UNPACK_BUFFER_EXT:
66 bufObj = ctx->Unpack.BufferObj;
67 break;
68 default:
69 /* error must be recorded by caller */
70 return NULL;
71 }
72
73 /* bufObj should point to NullBufferObj or a user-created buffer object */
74 ASSERT(bufObj);
75
76 return bufObj;
77 }
78
79
80 /**
81 * Tests the subdata range parameters and sets the GL error code for
82 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
83 *
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
92 * are invalid.
93 *
94 * \sa glBufferSubDataARB, glGetBufferSubDataARB
95 */
96 static struct gl_buffer_object *
97 buffer_object_subdata_range_good( GLcontext * ctx, GLenum target,
98 GLintptrARB offset, GLsizeiptrARB size,
99 const char *caller )
100 {
101 struct gl_buffer_object *bufObj;
102
103 if (size < 0) {
104 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
105 return NULL;
106 }
107
108 if (offset < 0) {
109 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
110 return NULL;
111 }
112
113 bufObj = get_buffer(ctx, target);
114 if (!bufObj) {
115 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", caller);
116 return NULL;
117 }
118 if (bufObj->Name == 0) {
119 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
120 return NULL;
121 }
122 if (offset + size > bufObj->Size) {
123 _mesa_error(ctx, GL_INVALID_VALUE,
124 "%s(size + offset > buffer size)", caller);
125 return NULL;
126 }
127 if (bufObj->Pointer) {
128 /* Buffer is currently mapped */
129 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
130 return NULL;
131 }
132
133 return bufObj;
134 }
135
136
137 /**
138 * Allocate and initialize a new buffer object.
139 *
140 * This function is intended to be called via
141 * \c dd_function_table::NewBufferObject.
142 */
143 struct gl_buffer_object *
144 _mesa_new_buffer_object( GLcontext *ctx, GLuint name, GLenum target )
145 {
146 struct gl_buffer_object *obj;
147
148 (void) ctx;
149
150 obj = MALLOC_STRUCT(gl_buffer_object);
151 _mesa_initialize_buffer_object(obj, name, target);
152 return obj;
153 }
154
155
156 /**
157 * Delete a buffer object.
158 *
159 * This function is intended to be called via
160 * \c dd_function_table::DeleteBuffer.
161 */
162 void
163 _mesa_delete_buffer_object( GLcontext *ctx, struct gl_buffer_object *bufObj )
164 {
165 (void) ctx;
166
167 if (bufObj->Data)
168 _mesa_free(bufObj->Data);
169 _mesa_free(bufObj);
170 }
171
172
173 void
174 _mesa_unbind_buffer_object( GLcontext *ctx, struct gl_buffer_object *bufObj )
175 {
176 if (bufObj != ctx->Array.NullBufferObj) {
177 bufObj->RefCount--;
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);
184 }
185 }
186 }
187
188
189 /**
190 * Initialize a buffer object to default values.
191 */
192 void
193 _mesa_initialize_buffer_object( struct gl_buffer_object *obj,
194 GLuint name, GLenum target )
195 {
196 (void) target;
197
198 _mesa_bzero(obj, sizeof(struct gl_buffer_object));
199 obj->RefCount = 1;
200 obj->Name = name;
201 obj->Usage = GL_STATIC_DRAW_ARB;
202 obj->Access = GL_READ_WRITE_ARB;
203 }
204
205
206 /**
207 * Add the given buffer object to the buffer object pool.
208 */
209 void
210 _mesa_save_buffer_object( GLcontext *ctx, struct gl_buffer_object *obj )
211 {
212 if (obj->Name > 0) {
213 /* insert into hash table */
214 _mesa_HashInsert(ctx->Shared->BufferObjects, obj->Name, obj);
215 }
216 }
217
218
219 /**
220 * Remove the given buffer object from the buffer object pool.
221 * Do not deallocate the buffer object though.
222 */
223 void
224 _mesa_remove_buffer_object( GLcontext *ctx, struct gl_buffer_object *bufObj )
225 {
226 if (bufObj->Name > 0) {
227 /* remove from hash table */
228 _mesa_HashRemove(ctx->Shared->BufferObjects, bufObj->Name);
229 }
230 }
231
232
233 /**
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.
237 *
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.
241 *
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.
249 *
250 * \sa glBufferDataARB, dd_function_table::BufferData.
251 */
252 void
253 _mesa_buffer_data( GLcontext *ctx, GLenum target, GLsizeiptrARB size,
254 const GLvoid * data, GLenum usage,
255 struct gl_buffer_object * bufObj )
256 {
257 void * new_data;
258
259 (void) ctx; (void) target;
260
261 new_data = _mesa_realloc( bufObj->Data, bufObj->Size, size );
262 if (new_data) {
263 bufObj->Data = (GLubyte *) new_data;
264 bufObj->Size = size;
265 bufObj->Usage = usage;
266
267 if (data) {
268 _mesa_memcpy( bufObj->Data, data, size );
269 }
270 }
271 }
272
273
274 /**
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.
278 *
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.
282 *
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.
289 *
290 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
291 */
292 void
293 _mesa_buffer_subdata( GLcontext *ctx, GLenum target, GLintptrARB offset,
294 GLsizeiptrARB size, const GLvoid * data,
295 struct gl_buffer_object * bufObj )
296 {
297 (void) ctx; (void) target;
298
299 /* this should have been caught in _mesa_BufferSubData() */
300 ASSERT(size + offset <= bufObj->Size);
301
302 if (bufObj->Data) {
303 _mesa_memcpy( (GLubyte *) bufObj->Data + offset, data, size );
304 }
305 }
306
307
308 /**
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.
312 *
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.
316 *
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.
323 *
324 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
325 */
326 void
327 _mesa_buffer_get_subdata( GLcontext *ctx, GLenum target, GLintptrARB offset,
328 GLsizeiptrARB size, GLvoid * data,
329 struct gl_buffer_object * bufObj )
330 {
331 (void) ctx; (void) target;
332
333 if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
334 _mesa_memcpy( data, (GLubyte *) bufObj->Data + offset, size );
335 }
336 }
337
338
339 /**
340 * Fallback function called via ctx->Driver.MapBuffer().
341 * Hardware drivers that really implement buffer objects should never use
342 * this function.
343 *
344 * The function parameters will have been already tested for errors.
345 *
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
351 * by the processor
352 *
353 * \sa glMapBufferARB, dd_function_table::MapBuffer
354 */
355 void *
356 _mesa_buffer_map( GLcontext *ctx, GLenum target, GLenum access,
357 struct gl_buffer_object *bufObj )
358 {
359 (void) ctx;
360 (void) target;
361 (void) access;
362 ASSERT(!bufObj->OnCard);
363 /* Just return a direct pointer to the data */
364 if (bufObj->Pointer) {
365 /* already mapped! */
366 return NULL;
367 }
368 bufObj->Pointer = bufObj->Data;
369 return bufObj->Pointer;
370 }
371
372
373 /**
374 * Fallback function called via ctx->Driver.MapBuffer().
375 * Hardware drivers that really implement buffer objects should never use
376 * function.
377 *
378 * The input parameters will have been already tested for errors.
379 *
380 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
381 */
382 GLboolean
383 _mesa_buffer_unmap( GLcontext *ctx, GLenum target,
384 struct gl_buffer_object *bufObj )
385 {
386 (void) ctx;
387 (void) target;
388 ASSERT(!bufObj->OnCard);
389 /* XXX we might assert here that bufObj->Pointer is non-null */
390 bufObj->Pointer = NULL;
391 return GL_TRUE;
392 }
393
394
395 /**
396 * Initialize the state associated with buffer objects
397 */
398 void
399 _mesa_init_buffer_objects( GLcontext *ctx )
400 {
401 /* Allocate the default buffer object and set refcount so high that
402 * it never gets deleted.
403 */
404 ctx->Array.NullBufferObj = _mesa_new_buffer_object(ctx, 0, 0);
405 if (ctx->Array.NullBufferObj)
406 ctx->Array.NullBufferObj->RefCount = 1000;
407
408 ctx->Array.ArrayBufferObj = ctx->Array.NullBufferObj;
409 ctx->Array.ElementArrayBufferObj = ctx->Array.NullBufferObj;
410 }
411
412 /**
413 * Bind the specified target to buffer for the specified context.
414 */
415 static void
416 bind_buffer_object(GLcontext *ctx, GLenum target, GLuint buffer)
417 {
418 struct gl_buffer_object *oldBufObj;
419 struct gl_buffer_object *newBufObj = NULL;
420 struct gl_buffer_object **bindTarget = NULL;
421
422 switch (target) {
423 case GL_ARRAY_BUFFER_ARB:
424 bindTarget = &ctx->Array.ArrayBufferObj;
425 break;
426 case GL_ELEMENT_ARRAY_BUFFER_ARB:
427 bindTarget = &ctx->Array.ElementArrayBufferObj;
428 break;
429 case GL_PIXEL_PACK_BUFFER_EXT:
430 bindTarget = &ctx->Pack.BufferObj;
431 break;
432 case GL_PIXEL_UNPACK_BUFFER_EXT:
433 bindTarget = &ctx->Unpack.BufferObj;
434 break;
435 default:
436 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target)");
437 return;
438 }
439
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 */
444
445 /*
446 * Get pointer to new buffer object (newBufObj)
447 */
448 if (buffer == 0) {
449 /* The spec says there's not a buffer object named 0, but we use
450 * one internally because it simplifies things.
451 */
452 newBufObj = ctx->Array.NullBufferObj;
453 }
454 else {
455 /* non-default buffer object */
456 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
457 if (!newBufObj) {
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);
461 if (!newBufObj) {
462 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindBufferARB");
463 return;
464 }
465 _mesa_save_buffer_object(ctx, newBufObj);
466 }
467 }
468
469 /* Make new binding */
470 *bindTarget = newBufObj;
471 newBufObj->RefCount++;
472
473 /* Pass BindBuffer call to device driver */
474 if (ctx->Driver.BindBuffer && newBufObj)
475 ctx->Driver.BindBuffer( ctx, target, newBufObj );
476
477 /* decr ref count on old buffer obj, delete if needed */
478 if (oldBufObj) {
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 );
485 }
486 }
487 }
488
489
490 /**
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
493 * shared state.
494 */
495 void
496 _mesa_update_default_objects_buffer_objects(GLcontext *ctx)
497 {
498 /* Bind the NullBufferObj to remove references to those
499 * in the shared context hash table.
500 */
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);
505 }
506
507
508 /**
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.
513 *
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!
517 *
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
525 * go out of bounds.
526 */
527 GLboolean
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)
532 {
533 GLvoid *start, *end;
534 const GLubyte *sizeAddr; /* buffer size, cast to a pointer */
535
536 ASSERT(pack->BufferObj->Name != 0);
537
538 if (pack->BufferObj->Size == 0)
539 /* no buffer! */
540 return GL_FALSE;
541
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);
545
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);
549
550
551 sizeAddr = ((const GLubyte *) 0) + pack->BufferObj->Size;
552
553 if ((const GLubyte *) start > sizeAddr) {
554 /* This will catch negative values / wrap-around */
555 return GL_FALSE;
556 }
557 if ((const GLubyte *) end > sizeAddr) {
558 /* Image read goes beyond end of buffer */
559 return GL_FALSE;
560 }
561
562 /* OK! */
563 return GL_TRUE;
564 }
565
566
567 /**
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.
574 */
575 const GLubyte *
576 _mesa_map_bitmap_pbo(GLcontext *ctx,
577 const struct gl_pixelstore_attrib *unpack,
578 const GLubyte *bitmap)
579 {
580 const GLubyte *buf;
581
582 if (unpack->BufferObj->Name) {
583 /* unpack from PBO */
584 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
585 GL_READ_ONLY_ARB,
586 unpack->BufferObj);
587 if (!buf)
588 return NULL;
589
590 buf = ADD_POINTERS(buf, bitmap);
591 }
592 else {
593 /* unpack from normal memory */
594 buf = bitmap;
595 }
596
597 return buf;
598 }
599
600
601 /**
602 * Counterpart to _mesa_map_bitmap_pbo()
603 * This is a helper function for (some) drivers.
604 */
605 void
606 _mesa_unmap_bitmap_pbo(GLcontext *ctx,
607 const struct gl_pixelstore_attrib *unpack)
608 {
609 if (unpack->BufferObj->Name) {
610 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
611 unpack->BufferObj);
612 }
613 }
614
615
616 /**
617 * \sa _mesa_map_bitmap_pbo
618 */
619 const GLvoid *
620 _mesa_map_drawpix_pbo(GLcontext *ctx,
621 const struct gl_pixelstore_attrib *unpack,
622 const GLvoid *pixels)
623 {
624 const GLvoid *buf;
625
626 if (unpack->BufferObj->Name) {
627 /* unpack from PBO */
628 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
629 GL_READ_ONLY_ARB,
630 unpack->BufferObj);
631 if (!buf)
632 return NULL;
633
634 buf = ADD_POINTERS(buf, pixels);
635 }
636 else {
637 /* unpack from normal memory */
638 buf = pixels;
639 }
640
641 return buf;
642 }
643
644
645 /**
646 * \sa _mesa_unmap_bitmap_pbo
647 */
648 void
649 _mesa_unmap_drapix_pbo(GLcontext *ctx,
650 const struct gl_pixelstore_attrib *unpack)
651 {
652 if (unpack->BufferObj->Name) {
653 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
654 unpack->BufferObj);
655 }
656 }
657
658
659 /**
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
663 */
664 void *
665 _mesa_map_readpix_pbo(GLcontext *ctx,
666 const struct gl_pixelstore_attrib *pack,
667 GLvoid *dest)
668 {
669 void *buf;
670
671 if (pack->BufferObj->Name) {
672 /* pack into PBO */
673 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
674 GL_WRITE_ONLY_ARB,
675 pack->BufferObj);
676 if (!buf)
677 return NULL;
678
679 buf = ADD_POINTERS(buf, dest);
680 }
681 else {
682 /* pack to normal memory */
683 buf = dest;
684 }
685
686 return buf;
687 }
688
689
690 /**
691 * Counterpart to _mesa_map_readpix_pbo()
692 */
693 void
694 _mesa_unmap_readpix_pbo(GLcontext *ctx,
695 const struct gl_pixelstore_attrib *pack)
696 {
697 if (pack->BufferObj->Name) {
698 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT, pack->BufferObj);
699 }
700 }
701
702
703
704 /**
705 * Return the gl_buffer_object for the given ID.
706 * Always return NULL for ID 0.
707 */
708 struct gl_buffer_object *
709 _mesa_lookup_bufferobj(GLcontext *ctx, GLuint buffer)
710 {
711 if (buffer == 0)
712 return NULL;
713 else
714 return (struct gl_buffer_object *)
715 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
716 }
717
718
719
720 /**********************************************************************/
721 /* API Functions */
722 /**********************************************************************/
723
724 void GLAPIENTRY
725 _mesa_BindBufferARB(GLenum target, GLuint buffer)
726 {
727 GET_CURRENT_CONTEXT(ctx);
728 ASSERT_OUTSIDE_BEGIN_END(ctx);
729
730 bind_buffer_object(ctx, target, buffer);
731 }
732
733
734 /**
735 * Delete a set of buffer objects.
736 *
737 * \param n Number of buffer objects to delete.
738 * \param ids Array of \c n buffer object IDs.
739 */
740 void GLAPIENTRY
741 _mesa_DeleteBuffersARB(GLsizei n, const GLuint *ids)
742 {
743 GET_CURRENT_CONTEXT(ctx);
744 GLsizei i;
745 ASSERT_OUTSIDE_BEGIN_END(ctx);
746
747 if (n < 0) {
748 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
749 return;
750 }
751
752 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
753
754 for (i = 0; i < n; i++) {
755 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
756 if (bufObj) {
757 /* unbind any vertex pointers bound to this buffer */
758 GLuint j;
759
760 ASSERT(bufObj->Name == ids[i]);
761
762 if (ctx->Array.ArrayObj->Vertex.BufferObj == bufObj) {
763 bufObj->RefCount--;
764 ctx->Array.ArrayObj->Vertex.BufferObj = ctx->Array.NullBufferObj;
765 ctx->Array.NullBufferObj->RefCount++;
766 }
767 if (ctx->Array.ArrayObj->Normal.BufferObj == bufObj) {
768 bufObj->RefCount--;
769 ctx->Array.ArrayObj->Normal.BufferObj = ctx->Array.NullBufferObj;
770 ctx->Array.NullBufferObj->RefCount++;
771 }
772 if (ctx->Array.ArrayObj->Color.BufferObj == bufObj) {
773 bufObj->RefCount--;
774 ctx->Array.ArrayObj->Color.BufferObj = ctx->Array.NullBufferObj;
775 ctx->Array.NullBufferObj->RefCount++;
776 }
777 if (ctx->Array.ArrayObj->SecondaryColor.BufferObj == bufObj) {
778 bufObj->RefCount--;
779 ctx->Array.ArrayObj->SecondaryColor.BufferObj = ctx->Array.NullBufferObj;
780 ctx->Array.NullBufferObj->RefCount++;
781 }
782 if (ctx->Array.ArrayObj->FogCoord.BufferObj == bufObj) {
783 bufObj->RefCount--;
784 ctx->Array.ArrayObj->FogCoord.BufferObj = ctx->Array.NullBufferObj;
785 ctx->Array.NullBufferObj->RefCount++;
786 }
787 if (ctx->Array.ArrayObj->Index.BufferObj == bufObj) {
788 bufObj->RefCount--;
789 ctx->Array.ArrayObj->Index.BufferObj = ctx->Array.NullBufferObj;
790 ctx->Array.NullBufferObj->RefCount++;
791 }
792 if (ctx->Array.ArrayObj->EdgeFlag.BufferObj == bufObj) {
793 bufObj->RefCount--;
794 ctx->Array.ArrayObj->EdgeFlag.BufferObj = ctx->Array.NullBufferObj;
795 ctx->Array.NullBufferObj->RefCount++;
796 }
797 for (j = 0; j < MAX_TEXTURE_UNITS; j++) {
798 if (ctx->Array.ArrayObj->TexCoord[j].BufferObj == bufObj) {
799 bufObj->RefCount--;
800 ctx->Array.ArrayObj->TexCoord[j].BufferObj = ctx->Array.NullBufferObj;
801 ctx->Array.NullBufferObj->RefCount++;
802 }
803 }
804 for (j = 0; j < VERT_ATTRIB_MAX; j++) {
805 if (ctx->Array.ArrayObj->VertexAttrib[j].BufferObj == bufObj) {
806 bufObj->RefCount--;
807 ctx->Array.ArrayObj->VertexAttrib[j].BufferObj = ctx->Array.NullBufferObj;
808 ctx->Array.NullBufferObj->RefCount++;
809 }
810 }
811
812 if (ctx->Array.ArrayBufferObj == bufObj) {
813 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
814 }
815 if (ctx->Array.ElementArrayBufferObj == bufObj) {
816 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
817 }
818
819 if (ctx->Pack.BufferObj == bufObj) {
820 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 );
821 }
822 if (ctx->Unpack.BufferObj == bufObj) {
823 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
824 }
825
826 /* The ID is immediately freed for re-use */
827 _mesa_remove_buffer_object(ctx, bufObj);
828 _mesa_unbind_buffer_object(ctx, bufObj);
829 }
830 }
831
832 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
833 }
834
835
836 /**
837 * Generate a set of unique buffer object IDs and store them in \c buffer.
838 *
839 * \param n Number of IDs to generate.
840 * \param buffer Array of \c n locations to store the IDs.
841 */
842 void GLAPIENTRY
843 _mesa_GenBuffersARB(GLsizei n, GLuint *buffer)
844 {
845 GET_CURRENT_CONTEXT(ctx);
846 GLuint first;
847 GLint i;
848 ASSERT_OUTSIDE_BEGIN_END(ctx);
849
850 if (n < 0) {
851 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
852 return;
853 }
854
855 if (!buffer) {
856 return;
857 }
858
859 /*
860 * This must be atomic (generation and allocation of buffer object IDs)
861 */
862 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
863
864 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
865
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;
870 GLenum target = 0;
871 bufObj = ctx->Driver.NewBufferObject( ctx, name, target );
872 if (!bufObj) {
873 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
874 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenBuffersARB");
875 return;
876 }
877 _mesa_save_buffer_object(ctx, bufObj);
878 buffer[i] = first + i;
879 }
880
881 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
882 }
883
884
885 /**
886 * Determine if ID is the name of a buffer object.
887 *
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.
891 */
892 GLboolean GLAPIENTRY
893 _mesa_IsBufferARB(GLuint id)
894 {
895 struct gl_buffer_object *bufObj;
896 GET_CURRENT_CONTEXT(ctx);
897 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
898
899 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
900 bufObj = _mesa_lookup_bufferobj(ctx, id);
901 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
902
903 return bufObj ? GL_TRUE : GL_FALSE;
904 }
905
906
907 void GLAPIENTRY
908 _mesa_BufferDataARB(GLenum target, GLsizeiptrARB size,
909 const GLvoid * data, GLenum usage)
910 {
911 GET_CURRENT_CONTEXT(ctx);
912 struct gl_buffer_object *bufObj;
913 ASSERT_OUTSIDE_BEGIN_END(ctx);
914
915 if (size < 0) {
916 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
917 return;
918 }
919
920 switch (usage) {
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:
930 /* OK */
931 break;
932 default:
933 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(usage)");
934 return;
935 }
936
937 bufObj = get_buffer(ctx, target);
938 if (!bufObj) {
939 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(target)" );
940 return;
941 }
942 if (bufObj->Name == 0) {
943 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferDataARB" );
944 return;
945 }
946
947 if (bufObj->Pointer) {
948 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferDataARB(buffer is mapped)" );
949 return;
950 }
951
952 ASSERT(ctx->Driver.BufferData);
953
954 /* Give the buffer object to the driver! <data> may be null! */
955 ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj );
956 }
957
958
959 void GLAPIENTRY
960 _mesa_BufferSubDataARB(GLenum target, GLintptrARB offset,
961 GLsizeiptrARB size, const GLvoid * data)
962 {
963 GET_CURRENT_CONTEXT(ctx);
964 struct gl_buffer_object *bufObj;
965 ASSERT_OUTSIDE_BEGIN_END(ctx);
966
967 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
968 "glBufferSubDataARB" );
969 if (!bufObj) {
970 /* error already recorded */
971 return;
972 }
973
974 ASSERT(ctx->Driver.BufferSubData);
975 ctx->Driver.BufferSubData( ctx, target, offset, size, data, bufObj );
976 }
977
978
979 void GLAPIENTRY
980 _mesa_GetBufferSubDataARB(GLenum target, GLintptrARB offset,
981 GLsizeiptrARB size, void * data)
982 {
983 GET_CURRENT_CONTEXT(ctx);
984 struct gl_buffer_object *bufObj;
985 ASSERT_OUTSIDE_BEGIN_END(ctx);
986
987 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
988 "glGetBufferSubDataARB" );
989 if (!bufObj) {
990 /* error already recorded */
991 return;
992 }
993
994 ASSERT(ctx->Driver.GetBufferSubData);
995 ctx->Driver.GetBufferSubData( ctx, target, offset, size, data, bufObj );
996 }
997
998
999 void * GLAPIENTRY
1000 _mesa_MapBufferARB(GLenum target, GLenum access)
1001 {
1002 GET_CURRENT_CONTEXT(ctx);
1003 struct gl_buffer_object * bufObj;
1004 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1005
1006 switch (access) {
1007 case GL_READ_ONLY_ARB:
1008 case GL_WRITE_ONLY_ARB:
1009 case GL_READ_WRITE_ARB:
1010 /* OK */
1011 break;
1012 default:
1013 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
1014 return NULL;
1015 }
1016
1017 bufObj = get_buffer(ctx, target);
1018 if (!bufObj) {
1019 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(target)" );
1020 return NULL;
1021 }
1022 if (bufObj->Name == 0) {
1023 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB" );
1024 return NULL;
1025 }
1026 if (bufObj->Pointer) {
1027 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
1028 return NULL;
1029 }
1030
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)");
1035 }
1036
1037 bufObj->Access = access;
1038
1039 return bufObj->Pointer;
1040 }
1041
1042
1043 GLboolean GLAPIENTRY
1044 _mesa_UnmapBufferARB(GLenum target)
1045 {
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);
1050
1051 bufObj = get_buffer(ctx, target);
1052 if (!bufObj) {
1053 _mesa_error(ctx, GL_INVALID_ENUM, "glUnmapBufferARB(target)" );
1054 return GL_FALSE;
1055 }
1056 if (bufObj->Name == 0) {
1057 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB" );
1058 return GL_FALSE;
1059 }
1060 if (!bufObj->Pointer) {
1061 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1062 return GL_FALSE;
1063 }
1064
1065 if (ctx->Driver.UnmapBuffer) {
1066 status = ctx->Driver.UnmapBuffer( ctx, target, bufObj );
1067 }
1068
1069 bufObj->Access = GL_READ_WRITE_ARB; /* initial value, OK? */
1070 bufObj->Pointer = NULL;
1071
1072 return status;
1073 }
1074
1075
1076 void GLAPIENTRY
1077 _mesa_GetBufferParameterivARB(GLenum target, GLenum pname, GLint *params)
1078 {
1079 GET_CURRENT_CONTEXT(ctx);
1080 struct gl_buffer_object *bufObj;
1081 ASSERT_OUTSIDE_BEGIN_END(ctx);
1082
1083 bufObj = get_buffer(ctx, target);
1084 if (!bufObj) {
1085 _mesa_error(ctx, GL_INVALID_ENUM, "GetBufferParameterivARB(target)" );
1086 return;
1087 }
1088 if (bufObj->Name == 0) {
1089 _mesa_error(ctx, GL_INVALID_OPERATION, "GetBufferParameterivARB" );
1090 return;
1091 }
1092
1093 switch (pname) {
1094 case GL_BUFFER_SIZE_ARB:
1095 *params = (GLint) bufObj->Size;
1096 break;
1097 case GL_BUFFER_USAGE_ARB:
1098 *params = bufObj->Usage;
1099 break;
1100 case GL_BUFFER_ACCESS_ARB:
1101 *params = bufObj->Access;
1102 break;
1103 case GL_BUFFER_MAPPED_ARB:
1104 *params = (bufObj->Pointer != NULL);
1105 break;
1106 default:
1107 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname)");
1108 return;
1109 }
1110 }
1111
1112
1113 void GLAPIENTRY
1114 _mesa_GetBufferPointervARB(GLenum target, GLenum pname, GLvoid **params)
1115 {
1116 GET_CURRENT_CONTEXT(ctx);
1117 struct gl_buffer_object * bufObj;
1118 ASSERT_OUTSIDE_BEGIN_END(ctx);
1119
1120 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1121 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1122 return;
1123 }
1124
1125 bufObj = get_buffer(ctx, target);
1126 if (!bufObj) {
1127 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(target)" );
1128 return;
1129 }
1130 if (bufObj->Name == 0) {
1131 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferPointervARB" );
1132 return;
1133 }
1134
1135 *params = bufObj->Pointer;
1136 }