[965] Replace the state cache suballocator with direct dri_bufmgr use.
[mesa.git] / src / mesa / main / bufferobj.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.1
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 * \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 * Return the gl_buffer_object for the given ID.
569 * Always return NULL for ID 0.
570 */
571 struct gl_buffer_object *
572 _mesa_lookup_bufferobj(GLcontext *ctx, GLuint buffer)
573 {
574 if (buffer == 0)
575 return NULL;
576 else
577 return (struct gl_buffer_object *)
578 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
579 }
580
581
582
583 /**********************************************************************/
584 /* API Functions */
585 /**********************************************************************/
586
587 void GLAPIENTRY
588 _mesa_BindBufferARB(GLenum target, GLuint buffer)
589 {
590 GET_CURRENT_CONTEXT(ctx);
591 ASSERT_OUTSIDE_BEGIN_END(ctx);
592
593 bind_buffer_object(ctx, target, buffer);
594 }
595
596
597 /**
598 * Delete a set of buffer objects.
599 *
600 * \param n Number of buffer objects to delete.
601 * \param ids Array of \c n buffer object IDs.
602 */
603 void GLAPIENTRY
604 _mesa_DeleteBuffersARB(GLsizei n, const GLuint *ids)
605 {
606 GET_CURRENT_CONTEXT(ctx);
607 GLsizei i;
608 ASSERT_OUTSIDE_BEGIN_END(ctx);
609
610 if (n < 0) {
611 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
612 return;
613 }
614
615 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
616
617 for (i = 0; i < n; i++) {
618 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
619 if (bufObj) {
620 /* unbind any vertex pointers bound to this buffer */
621 GLuint j;
622
623 ASSERT(bufObj->Name == ids[i]);
624
625 if (ctx->Array.ArrayObj->Vertex.BufferObj == bufObj) {
626 bufObj->RefCount--;
627 ctx->Array.ArrayObj->Vertex.BufferObj = ctx->Array.NullBufferObj;
628 ctx->Array.NullBufferObj->RefCount++;
629 }
630 if (ctx->Array.ArrayObj->Normal.BufferObj == bufObj) {
631 bufObj->RefCount--;
632 ctx->Array.ArrayObj->Normal.BufferObj = ctx->Array.NullBufferObj;
633 ctx->Array.NullBufferObj->RefCount++;
634 }
635 if (ctx->Array.ArrayObj->Color.BufferObj == bufObj) {
636 bufObj->RefCount--;
637 ctx->Array.ArrayObj->Color.BufferObj = ctx->Array.NullBufferObj;
638 ctx->Array.NullBufferObj->RefCount++;
639 }
640 if (ctx->Array.ArrayObj->SecondaryColor.BufferObj == bufObj) {
641 bufObj->RefCount--;
642 ctx->Array.ArrayObj->SecondaryColor.BufferObj = ctx->Array.NullBufferObj;
643 ctx->Array.NullBufferObj->RefCount++;
644 }
645 if (ctx->Array.ArrayObj->FogCoord.BufferObj == bufObj) {
646 bufObj->RefCount--;
647 ctx->Array.ArrayObj->FogCoord.BufferObj = ctx->Array.NullBufferObj;
648 ctx->Array.NullBufferObj->RefCount++;
649 }
650 if (ctx->Array.ArrayObj->Index.BufferObj == bufObj) {
651 bufObj->RefCount--;
652 ctx->Array.ArrayObj->Index.BufferObj = ctx->Array.NullBufferObj;
653 ctx->Array.NullBufferObj->RefCount++;
654 }
655 if (ctx->Array.ArrayObj->EdgeFlag.BufferObj == bufObj) {
656 bufObj->RefCount--;
657 ctx->Array.ArrayObj->EdgeFlag.BufferObj = ctx->Array.NullBufferObj;
658 ctx->Array.NullBufferObj->RefCount++;
659 }
660 for (j = 0; j < MAX_TEXTURE_UNITS; j++) {
661 if (ctx->Array.ArrayObj->TexCoord[j].BufferObj == bufObj) {
662 bufObj->RefCount--;
663 ctx->Array.ArrayObj->TexCoord[j].BufferObj = ctx->Array.NullBufferObj;
664 ctx->Array.NullBufferObj->RefCount++;
665 }
666 }
667 for (j = 0; j < VERT_ATTRIB_MAX; j++) {
668 if (ctx->Array.ArrayObj->VertexAttrib[j].BufferObj == bufObj) {
669 bufObj->RefCount--;
670 ctx->Array.ArrayObj->VertexAttrib[j].BufferObj = ctx->Array.NullBufferObj;
671 ctx->Array.NullBufferObj->RefCount++;
672 }
673 }
674
675 if (ctx->Array.ArrayBufferObj == bufObj) {
676 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
677 }
678 if (ctx->Array.ElementArrayBufferObj == bufObj) {
679 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
680 }
681
682 if (ctx->Pack.BufferObj == bufObj) {
683 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 );
684 }
685 if (ctx->Unpack.BufferObj == bufObj) {
686 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
687 }
688
689 /* The ID is immediately freed for re-use */
690 _mesa_remove_buffer_object(ctx, bufObj);
691 _mesa_unbind_buffer_object(ctx, bufObj);
692 }
693 }
694
695 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
696 }
697
698
699 /**
700 * Generate a set of unique buffer object IDs and store them in \c buffer.
701 *
702 * \param n Number of IDs to generate.
703 * \param buffer Array of \c n locations to store the IDs.
704 */
705 void GLAPIENTRY
706 _mesa_GenBuffersARB(GLsizei n, GLuint *buffer)
707 {
708 GET_CURRENT_CONTEXT(ctx);
709 GLuint first;
710 GLint i;
711 ASSERT_OUTSIDE_BEGIN_END(ctx);
712
713 if (n < 0) {
714 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
715 return;
716 }
717
718 if (!buffer) {
719 return;
720 }
721
722 /*
723 * This must be atomic (generation and allocation of buffer object IDs)
724 */
725 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
726
727 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
728
729 /* Allocate new, empty buffer objects and return identifiers */
730 for (i = 0; i < n; i++) {
731 struct gl_buffer_object *bufObj;
732 GLuint name = first + i;
733 GLenum target = 0;
734 bufObj = ctx->Driver.NewBufferObject( ctx, name, target );
735 if (!bufObj) {
736 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
737 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenBuffersARB");
738 return;
739 }
740 _mesa_save_buffer_object(ctx, bufObj);
741 buffer[i] = first + i;
742 }
743
744 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
745 }
746
747
748 /**
749 * Determine if ID is the name of a buffer object.
750 *
751 * \param id ID of the potential buffer object.
752 * \return \c GL_TRUE if \c id is the name of a buffer object,
753 * \c GL_FALSE otherwise.
754 */
755 GLboolean GLAPIENTRY
756 _mesa_IsBufferARB(GLuint id)
757 {
758 struct gl_buffer_object *bufObj;
759 GET_CURRENT_CONTEXT(ctx);
760 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
761
762 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
763 bufObj = _mesa_lookup_bufferobj(ctx, id);
764 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
765
766 return bufObj ? GL_TRUE : GL_FALSE;
767 }
768
769
770 void GLAPIENTRY
771 _mesa_BufferDataARB(GLenum target, GLsizeiptrARB size,
772 const GLvoid * data, GLenum usage)
773 {
774 GET_CURRENT_CONTEXT(ctx);
775 struct gl_buffer_object *bufObj;
776 ASSERT_OUTSIDE_BEGIN_END(ctx);
777
778 if (size < 0) {
779 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
780 return;
781 }
782
783 switch (usage) {
784 case GL_STREAM_DRAW_ARB:
785 case GL_STREAM_READ_ARB:
786 case GL_STREAM_COPY_ARB:
787 case GL_STATIC_DRAW_ARB:
788 case GL_STATIC_READ_ARB:
789 case GL_STATIC_COPY_ARB:
790 case GL_DYNAMIC_DRAW_ARB:
791 case GL_DYNAMIC_READ_ARB:
792 case GL_DYNAMIC_COPY_ARB:
793 /* OK */
794 break;
795 default:
796 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(usage)");
797 return;
798 }
799
800 bufObj = get_buffer(ctx, target);
801 if (!bufObj) {
802 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(target)" );
803 return;
804 }
805 if (bufObj->Name == 0) {
806 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferDataARB" );
807 return;
808 }
809
810 if (bufObj->Pointer) {
811 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferDataARB(buffer is mapped)" );
812 return;
813 }
814
815 ASSERT(ctx->Driver.BufferData);
816
817 /* Give the buffer object to the driver! <data> may be null! */
818 ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj );
819 }
820
821
822 void GLAPIENTRY
823 _mesa_BufferSubDataARB(GLenum target, GLintptrARB offset,
824 GLsizeiptrARB size, const GLvoid * data)
825 {
826 GET_CURRENT_CONTEXT(ctx);
827 struct gl_buffer_object *bufObj;
828 ASSERT_OUTSIDE_BEGIN_END(ctx);
829
830 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
831 "glBufferSubDataARB" );
832 if (!bufObj) {
833 /* error already recorded */
834 return;
835 }
836
837 ASSERT(ctx->Driver.BufferSubData);
838 ctx->Driver.BufferSubData( ctx, target, offset, size, data, bufObj );
839 }
840
841
842 void GLAPIENTRY
843 _mesa_GetBufferSubDataARB(GLenum target, GLintptrARB offset,
844 GLsizeiptrARB size, void * data)
845 {
846 GET_CURRENT_CONTEXT(ctx);
847 struct gl_buffer_object *bufObj;
848 ASSERT_OUTSIDE_BEGIN_END(ctx);
849
850 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
851 "glGetBufferSubDataARB" );
852 if (!bufObj) {
853 /* error already recorded */
854 return;
855 }
856
857 ASSERT(ctx->Driver.GetBufferSubData);
858 ctx->Driver.GetBufferSubData( ctx, target, offset, size, data, bufObj );
859 }
860
861
862 void * GLAPIENTRY
863 _mesa_MapBufferARB(GLenum target, GLenum access)
864 {
865 GET_CURRENT_CONTEXT(ctx);
866 struct gl_buffer_object * bufObj;
867 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
868
869 switch (access) {
870 case GL_READ_ONLY_ARB:
871 case GL_WRITE_ONLY_ARB:
872 case GL_READ_WRITE_ARB:
873 /* OK */
874 break;
875 default:
876 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
877 return NULL;
878 }
879
880 bufObj = get_buffer(ctx, target);
881 if (!bufObj) {
882 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(target)" );
883 return NULL;
884 }
885 if (bufObj->Name == 0) {
886 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB" );
887 return NULL;
888 }
889 if (bufObj->Pointer) {
890 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
891 return NULL;
892 }
893
894 ASSERT(ctx->Driver.MapBuffer);
895 bufObj->Pointer = ctx->Driver.MapBuffer( ctx, target, access, bufObj );
896 if (!bufObj->Pointer) {
897 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(access)");
898 }
899
900 bufObj->Access = access;
901
902 return bufObj->Pointer;
903 }
904
905
906 GLboolean GLAPIENTRY
907 _mesa_UnmapBufferARB(GLenum target)
908 {
909 GET_CURRENT_CONTEXT(ctx);
910 struct gl_buffer_object *bufObj;
911 GLboolean status = GL_TRUE;
912 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
913
914 bufObj = get_buffer(ctx, target);
915 if (!bufObj) {
916 _mesa_error(ctx, GL_INVALID_ENUM, "glUnmapBufferARB(target)" );
917 return GL_FALSE;
918 }
919 if (bufObj->Name == 0) {
920 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB" );
921 return GL_FALSE;
922 }
923 if (!bufObj->Pointer) {
924 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
925 return GL_FALSE;
926 }
927
928 if (ctx->Driver.UnmapBuffer) {
929 status = ctx->Driver.UnmapBuffer( ctx, target, bufObj );
930 }
931
932 bufObj->Access = GL_READ_WRITE_ARB; /* initial value, OK? */
933 bufObj->Pointer = NULL;
934
935 return status;
936 }
937
938
939 void GLAPIENTRY
940 _mesa_GetBufferParameterivARB(GLenum target, GLenum pname, GLint *params)
941 {
942 GET_CURRENT_CONTEXT(ctx);
943 struct gl_buffer_object *bufObj;
944 ASSERT_OUTSIDE_BEGIN_END(ctx);
945
946 bufObj = get_buffer(ctx, target);
947 if (!bufObj) {
948 _mesa_error(ctx, GL_INVALID_ENUM, "GetBufferParameterivARB(target)" );
949 return;
950 }
951 if (bufObj->Name == 0) {
952 _mesa_error(ctx, GL_INVALID_OPERATION, "GetBufferParameterivARB" );
953 return;
954 }
955
956 switch (pname) {
957 case GL_BUFFER_SIZE_ARB:
958 *params = (GLint) bufObj->Size;
959 break;
960 case GL_BUFFER_USAGE_ARB:
961 *params = bufObj->Usage;
962 break;
963 case GL_BUFFER_ACCESS_ARB:
964 *params = bufObj->Access;
965 break;
966 case GL_BUFFER_MAPPED_ARB:
967 *params = (bufObj->Pointer != NULL);
968 break;
969 default:
970 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname)");
971 return;
972 }
973 }
974
975
976 void GLAPIENTRY
977 _mesa_GetBufferPointervARB(GLenum target, GLenum pname, GLvoid **params)
978 {
979 GET_CURRENT_CONTEXT(ctx);
980 struct gl_buffer_object * bufObj;
981 ASSERT_OUTSIDE_BEGIN_END(ctx);
982
983 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
984 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
985 return;
986 }
987
988 bufObj = get_buffer(ctx, target);
989 if (!bufObj) {
990 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(target)" );
991 return;
992 }
993 if (bufObj->Name == 0) {
994 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferPointervARB" );
995 return;
996 }
997
998 *params = bufObj->Pointer;
999 }