[PROPSYS]
[reactos.git] / reactos / dll / opengl / mesa / main / bufferobj.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.6
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file bufferobj.c
29 * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions.
30 * \author Brian Paul, Ian Romanick
31 */
32
33 #include <precomp.h>
34
35 /* Debug flags */
36 /*#define VBO_DEBUG*/
37 /*#define BOUNDS_CHECK*/
38
39
40 /**
41 * Used as a placeholder for buffer objects between glGenBuffers() and
42 * glBindBuffer() so that glIsBuffer() can work correctly.
43 */
44 static struct gl_buffer_object DummyBufferObject;
45
46
47 /**
48 * Return pointer to address of a buffer object target.
49 * \param ctx the GL context
50 * \param target the buffer object target to be retrieved.
51 * \return pointer to pointer to the buffer object bound to \c target in the
52 * specified context or \c NULL if \c target is invalid.
53 */
54 static inline struct gl_buffer_object **
55 get_buffer_target(struct gl_context *ctx, GLenum target)
56 {
57 switch (target) {
58 case GL_ARRAY_BUFFER_ARB:
59 return &ctx->Array.ArrayBufferObj;
60 case GL_ELEMENT_ARRAY_BUFFER_ARB:
61 return &ctx->Array.ArrayObj->ElementArrayBufferObj;
62 default:
63 return NULL;
64 }
65 return NULL;
66 }
67
68
69 /**
70 * Get the buffer object bound to the specified target in a GL context.
71 * \param ctx the GL context
72 * \param target the buffer object target to be retrieved.
73 * \return pointer to the buffer object bound to \c target in the
74 * specified context or \c NULL if \c target is invalid.
75 */
76 static inline struct gl_buffer_object *
77 get_buffer(struct gl_context *ctx, const char *func, GLenum target)
78 {
79 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
80
81 if (!bufObj) {
82 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
83 return NULL;
84 }
85
86 if (!_mesa_is_bufferobj(*bufObj)) {
87 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(buffer 0)", func);
88 return NULL;
89 }
90
91 return *bufObj;
92 }
93
94
95 static inline GLenum
96 default_access_mode(const struct gl_context *ctx)
97 {
98 (void)ctx;
99 return GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
100 }
101
102
103 /**
104 * Convert a GLbitfield describing the mapped buffer access flags
105 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
106 */
107 static GLenum
108 simplified_access_mode(GLbitfield access)
109 {
110 const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
111 if ((access & rwFlags) == rwFlags)
112 return GL_READ_WRITE;
113 if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
114 return GL_READ_ONLY;
115 if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
116 return GL_WRITE_ONLY;
117 return GL_READ_WRITE; /* this should never happen, but no big deal */
118 }
119
120
121 /**
122 * Tests the subdata range parameters and sets the GL error code for
123 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
124 *
125 * \param ctx GL context.
126 * \param target Buffer object target on which to operate.
127 * \param offset Offset of the first byte of the subdata range.
128 * \param size Size, in bytes, of the subdata range.
129 * \param caller Name of calling function for recording errors.
130 * \return A pointer to the buffer object bound to \c target in the
131 * specified context or \c NULL if any of the parameter or state
132 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
133 * are invalid.
134 *
135 * \sa glBufferSubDataARB, glGetBufferSubDataARB
136 */
137 static struct gl_buffer_object *
138 buffer_object_subdata_range_good( struct gl_context * ctx, GLenum target,
139 GLintptrARB offset, GLsizeiptrARB size,
140 const char *caller )
141 {
142 struct gl_buffer_object *bufObj;
143
144 if (size < 0) {
145 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
146 return NULL;
147 }
148
149 if (offset < 0) {
150 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
151 return NULL;
152 }
153
154 bufObj = get_buffer(ctx, caller, target);
155 if (!bufObj)
156 return NULL;
157
158 if (offset + size > bufObj->Size) {
159 _mesa_error(ctx, GL_INVALID_VALUE,
160 "%s(offset %lu + size %lu > buffer size %lu)", caller,
161 (unsigned long) offset,
162 (unsigned long) size,
163 (unsigned long) bufObj->Size);
164 return NULL;
165 }
166 if (_mesa_bufferobj_mapped(bufObj)) {
167 /* Buffer is currently mapped */
168 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
169 return NULL;
170 }
171
172 return bufObj;
173 }
174
175
176 /**
177 * Allocate and initialize a new buffer object.
178 *
179 * Default callback for the \c dd_function_table::NewBufferObject() hook.
180 */
181 static struct gl_buffer_object *
182 _mesa_new_buffer_object( struct gl_context *ctx, GLuint name, GLenum target )
183 {
184 struct gl_buffer_object *obj;
185
186 (void) ctx;
187
188 obj = MALLOC_STRUCT(gl_buffer_object);
189 _mesa_initialize_buffer_object(ctx, obj, name, target);
190 return obj;
191 }
192
193
194 /**
195 * Delete a buffer object.
196 *
197 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
198 */
199 static void
200 _mesa_delete_buffer_object(struct gl_context *ctx,
201 struct gl_buffer_object *bufObj)
202 {
203 (void) ctx;
204
205 if (bufObj->Data)
206 free(bufObj->Data);
207
208 /* assign strange values here to help w/ debugging */
209 bufObj->RefCount = -1000;
210 bufObj->Name = ~0;
211
212 _glthread_DESTROY_MUTEX(bufObj->Mutex);
213 free(bufObj);
214 }
215
216
217
218 /**
219 * Set ptr to bufObj w/ reference counting.
220 * This is normally only called from the _mesa_reference_buffer_object() macro
221 * when there's a real pointer change.
222 */
223 void
224 _mesa_reference_buffer_object_(struct gl_context *ctx,
225 struct gl_buffer_object **ptr,
226 struct gl_buffer_object *bufObj)
227 {
228 if (*ptr) {
229 /* Unreference the old buffer */
230 GLboolean deleteFlag = GL_FALSE;
231 struct gl_buffer_object *oldObj = *ptr;
232
233 _glthread_LOCK_MUTEX(oldObj->Mutex);
234 ASSERT(oldObj->RefCount > 0);
235 oldObj->RefCount--;
236 #if 0
237 printf("BufferObj %p %d DECR to %d\n",
238 (void *) oldObj, oldObj->Name, oldObj->RefCount);
239 #endif
240 deleteFlag = (oldObj->RefCount == 0);
241 _glthread_UNLOCK_MUTEX(oldObj->Mutex);
242
243 if (deleteFlag) {
244
245 /* some sanity checking: don't delete a buffer still in use */
246 #if 0
247 /* unfortunately, these tests are invalid during context tear-down */
248 ASSERT(ctx->Array.ArrayBufferObj != bufObj);
249 ASSERT(ctx->Array.ArrayObj->ElementArrayBufferObj != bufObj);
250 ASSERT(ctx->Array.ArrayObj->Vertex.BufferObj != bufObj);
251 #endif
252
253 ASSERT(ctx->Driver.DeleteBuffer);
254 ctx->Driver.DeleteBuffer(ctx, oldObj);
255 }
256
257 *ptr = NULL;
258 }
259 ASSERT(!*ptr);
260
261 if (bufObj) {
262 /* reference new buffer */
263 _glthread_LOCK_MUTEX(bufObj->Mutex);
264 if (bufObj->RefCount == 0) {
265 /* this buffer's being deleted (look just above) */
266 /* Not sure this can every really happen. Warn if it does. */
267 _mesa_problem(NULL, "referencing deleted buffer object");
268 *ptr = NULL;
269 }
270 else {
271 bufObj->RefCount++;
272 #if 0
273 printf("BufferObj %p %d INCR to %d\n",
274 (void *) bufObj, bufObj->Name, bufObj->RefCount);
275 #endif
276 *ptr = bufObj;
277 }
278 _glthread_UNLOCK_MUTEX(bufObj->Mutex);
279 }
280 }
281
282
283 /**
284 * Initialize a buffer object to default values.
285 */
286 void
287 _mesa_initialize_buffer_object( struct gl_context *ctx,
288 struct gl_buffer_object *obj,
289 GLuint name, GLenum target )
290 {
291 (void) target;
292
293 memset(obj, 0, sizeof(struct gl_buffer_object));
294 _glthread_INIT_MUTEX(obj->Mutex);
295 obj->RefCount = 1;
296 obj->Name = name;
297 obj->Usage = GL_STATIC_DRAW_ARB;
298 obj->AccessFlags = default_access_mode(ctx);
299 }
300
301
302 /**
303 * Allocate space for and store data in a buffer object. Any data that was
304 * previously stored in the buffer object is lost. If \c data is \c NULL,
305 * memory will be allocated, but no copy will occur.
306 *
307 * This is the default callback for \c dd_function_table::BufferData()
308 * Note that all GL error checking will have been done already.
309 *
310 * \param ctx GL context.
311 * \param target Buffer object target on which to operate.
312 * \param size Size, in bytes, of the new data store.
313 * \param data Pointer to the data to store in the buffer object. This
314 * pointer may be \c NULL.
315 * \param usage Hints about how the data will be used.
316 * \param bufObj Object to be used.
317 *
318 * \return GL_TRUE for success, GL_FALSE for failure
319 * \sa glBufferDataARB, dd_function_table::BufferData.
320 */
321 static GLboolean
322 _mesa_buffer_data( struct gl_context *ctx, GLenum target, GLsizeiptrARB size,
323 const GLvoid * data, GLenum usage,
324 struct gl_buffer_object * bufObj )
325 {
326 void * new_data;
327
328 (void) ctx; (void) target;
329
330 new_data = _mesa_realloc( bufObj->Data, bufObj->Size, size );
331 if (new_data) {
332 bufObj->Data = (GLubyte *) new_data;
333 bufObj->Size = size;
334 bufObj->Usage = usage;
335
336 if (data) {
337 memcpy( bufObj->Data, data, size );
338 }
339
340 return GL_TRUE;
341 }
342 else {
343 return GL_FALSE;
344 }
345 }
346
347
348 /**
349 * Replace data in a subrange of buffer object. If the data range
350 * specified by \c size + \c offset extends beyond the end of the buffer or
351 * if \c data is \c NULL, no copy is performed.
352 *
353 * This is the default callback for \c dd_function_table::BufferSubData()
354 * Note that all GL error checking will have been done already.
355 *
356 * \param ctx GL context.
357 * \param target Buffer object target on which to operate.
358 * \param offset Offset of the first byte to be modified.
359 * \param size Size, in bytes, of the data range.
360 * \param data Pointer to the data to store in the buffer object.
361 * \param bufObj Object to be used.
362 *
363 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
364 */
365 static void
366 _mesa_buffer_subdata( struct gl_context *ctx, GLintptrARB offset,
367 GLsizeiptrARB size, const GLvoid * data,
368 struct gl_buffer_object * bufObj )
369 {
370 (void) ctx;
371
372 /* this should have been caught in _mesa_BufferSubData() */
373 ASSERT(size + offset <= bufObj->Size);
374
375 if (bufObj->Data) {
376 memcpy( (GLubyte *) bufObj->Data + offset, data, size );
377 }
378 }
379
380
381 /**
382 * Retrieve data from a subrange of buffer object. If the data range
383 * specified by \c size + \c offset extends beyond the end of the buffer or
384 * if \c data is \c NULL, no copy is performed.
385 *
386 * This is the default callback for \c dd_function_table::GetBufferSubData()
387 * Note that all GL error checking will have been done already.
388 *
389 * \param ctx GL context.
390 * \param target Buffer object target on which to operate.
391 * \param offset Offset of the first byte to be fetched.
392 * \param size Size, in bytes, of the data range.
393 * \param data Destination for data
394 * \param bufObj Object to be used.
395 *
396 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
397 */
398 static void
399 _mesa_buffer_get_subdata( struct gl_context *ctx, GLintptrARB offset,
400 GLsizeiptrARB size, GLvoid * data,
401 struct gl_buffer_object * bufObj )
402 {
403 (void) ctx;
404
405 if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
406 memcpy( data, (GLubyte *) bufObj->Data + offset, size );
407 }
408 }
409
410
411 /**
412 * Default fallback for \c dd_function_table::MapBufferRange().
413 * Called via glMapBufferRange().
414 */
415 static void *
416 _mesa_buffer_map_range( struct gl_context *ctx, GLintptr offset,
417 GLsizeiptr length, GLbitfield access,
418 struct gl_buffer_object *bufObj )
419 {
420 (void) ctx;
421 assert(!_mesa_bufferobj_mapped(bufObj));
422 /* Just return a direct pointer to the data */
423 bufObj->Pointer = bufObj->Data + offset;
424 bufObj->Length = length;
425 bufObj->Offset = offset;
426 bufObj->AccessFlags = access;
427 return bufObj->Pointer;
428 }
429
430
431 /**
432 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
433 * Called via glFlushMappedBufferRange().
434 */
435 static void
436 _mesa_buffer_flush_mapped_range( struct gl_context *ctx,
437 GLintptr offset, GLsizeiptr length,
438 struct gl_buffer_object *obj )
439 {
440 (void) ctx;
441 (void) offset;
442 (void) length;
443 (void) obj;
444 /* no-op */
445 }
446
447
448 /**
449 * Default callback for \c dd_function_table::MapBuffer().
450 *
451 * The input parameters will have been already tested for errors.
452 *
453 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
454 */
455 static GLboolean
456 _mesa_buffer_unmap( struct gl_context *ctx, struct gl_buffer_object *bufObj )
457 {
458 (void) ctx;
459 /* XXX we might assert here that bufObj->Pointer is non-null */
460 bufObj->Pointer = NULL;
461 bufObj->Length = 0;
462 bufObj->Offset = 0;
463 bufObj->AccessFlags = 0x0;
464 return GL_TRUE;
465 }
466
467
468 /**
469 * Initialize the state associated with buffer objects
470 */
471 void
472 _mesa_init_buffer_objects( struct gl_context *ctx )
473 {
474 memset(&DummyBufferObject, 0, sizeof(DummyBufferObject));
475 _glthread_INIT_MUTEX(DummyBufferObject.Mutex);
476 DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
477
478 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj,
479 ctx->Shared->NullBufferObj);
480 }
481
482
483 void
484 _mesa_free_buffer_objects( struct gl_context *ctx )
485 {
486 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
487 }
488
489
490 /**
491 * Bind the specified target to buffer for the specified context.
492 * Called by glBindBuffer() and other functions.
493 */
494 static void
495 bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
496 {
497 struct gl_buffer_object *oldBufObj;
498 struct gl_buffer_object *newBufObj = NULL;
499 struct gl_buffer_object **bindTarget = NULL;
500
501 bindTarget = get_buffer_target(ctx, target);
502 if (!bindTarget) {
503 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
504 return;
505 }
506
507 /* Get pointer to old buffer object (to be unbound) */
508 oldBufObj = *bindTarget;
509 if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
510 return; /* rebinding the same buffer object- no change */
511
512 /*
513 * Get pointer to new buffer object (newBufObj)
514 */
515 if (buffer == 0) {
516 /* The spec says there's not a buffer object named 0, but we use
517 * one internally because it simplifies things.
518 */
519 newBufObj = ctx->Shared->NullBufferObj;
520 }
521 else {
522 /* non-default buffer object */
523 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
524 if (!newBufObj || newBufObj == &DummyBufferObject) {
525 /* If this is a new buffer object id, or one which was generated but
526 * never used before, allocate a buffer object now.
527 */
528 ASSERT(ctx->Driver.NewBufferObject);
529 newBufObj = ctx->Driver.NewBufferObject(ctx, buffer, target);
530 if (!newBufObj) {
531 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindBufferARB");
532 return;
533 }
534 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, newBufObj);
535 }
536 }
537
538 /* bind new buffer */
539 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
540
541 /* Pass BindBuffer call to device driver */
542 if (ctx->Driver.BindBuffer)
543 ctx->Driver.BindBuffer( ctx, target, newBufObj );
544 }
545
546
547 /**
548 * Update the default buffer objects in the given context to reference those
549 * specified in the shared state and release those referencing the old
550 * shared state.
551 */
552 void
553 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
554 {
555 /* Bind the NullBufferObj to remove references to those
556 * in the shared context hash table.
557 */
558 bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
559 bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
560 }
561
562
563
564 /**
565 * Return the gl_buffer_object for the given ID.
566 * Always return NULL for ID 0.
567 */
568 struct gl_buffer_object *
569 _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
570 {
571 if (buffer == 0)
572 return NULL;
573 else
574 return (struct gl_buffer_object *)
575 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
576 }
577
578
579 /**
580 * If *ptr points to obj, set ptr = the Null/default buffer object.
581 * This is a helper for buffer object deletion.
582 * The GL spec says that deleting a buffer object causes it to get
583 * unbound from all arrays in the current context.
584 */
585 static void
586 unbind(struct gl_context *ctx,
587 struct gl_buffer_object **ptr,
588 struct gl_buffer_object *obj)
589 {
590 if (*ptr == obj) {
591 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
592 }
593 }
594
595
596 /**
597 * Plug default/fallback buffer object functions into the device
598 * driver hooks.
599 */
600 void
601 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
602 {
603 /* GL_ARB_vertex/pixel_buffer_object */
604 driver->NewBufferObject = _mesa_new_buffer_object;
605 driver->DeleteBuffer = _mesa_delete_buffer_object;
606 driver->BindBuffer = NULL;
607 driver->BufferData = _mesa_buffer_data;
608 driver->BufferSubData = _mesa_buffer_subdata;
609 driver->GetBufferSubData = _mesa_buffer_get_subdata;
610 driver->UnmapBuffer = _mesa_buffer_unmap;
611
612 /* GL_ARB_map_buffer_range */
613 driver->MapBufferRange = _mesa_buffer_map_range;
614 driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
615 }
616
617
618
619 /**********************************************************************/
620 /* API Functions */
621 /**********************************************************************/
622
623 void GLAPIENTRY
624 _mesa_BindBufferARB(GLenum target, GLuint buffer)
625 {
626 GET_CURRENT_CONTEXT(ctx);
627 ASSERT_OUTSIDE_BEGIN_END(ctx);
628
629 if (MESA_VERBOSE & VERBOSE_API)
630 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
631 _mesa_lookup_enum_by_nr(target), buffer);
632
633 bind_buffer_object(ctx, target, buffer);
634 }
635
636
637 /**
638 * Delete a set of buffer objects.
639 *
640 * \param n Number of buffer objects to delete.
641 * \param ids Array of \c n buffer object IDs.
642 */
643 void GLAPIENTRY
644 _mesa_DeleteBuffersARB(GLsizei n, const GLuint *ids)
645 {
646 GET_CURRENT_CONTEXT(ctx);
647 GLsizei i;
648 ASSERT_OUTSIDE_BEGIN_END(ctx);
649 FLUSH_VERTICES(ctx, 0);
650
651 if (n < 0) {
652 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
653 return;
654 }
655
656 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
657
658 for (i = 0; i < n; i++) {
659 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
660 if (bufObj) {
661 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
662 GLuint j;
663
664 ASSERT(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
665
666 if (_mesa_bufferobj_mapped(bufObj)) {
667 /* if mapped, unmap it now */
668 ctx->Driver.UnmapBuffer(ctx, bufObj);
669 bufObj->AccessFlags = default_access_mode(ctx);
670 bufObj->Pointer = NULL;
671 }
672
673 /* unbind any vertex pointers bound to this buffer */
674 for (j = 0; j < Elements(arrayObj->VertexAttrib); j++) {
675 unbind(ctx, &arrayObj->VertexAttrib[j].BufferObj, bufObj);
676 }
677
678 if (ctx->Array.ArrayBufferObj == bufObj) {
679 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
680 }
681 if (arrayObj->ElementArrayBufferObj == bufObj) {
682 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
683 }
684
685 /* The ID is immediately freed for re-use */
686 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
687 /* Make sure we do not run into the classic ABA problem on bind.
688 * We don't want to allow re-binding a buffer object that's been
689 * "deleted" by glDeleteBuffers().
690 *
691 * The explicit rebinding to the default object in the current context
692 * prevents the above in the current context, but another context
693 * sharing the same objects might suffer from this problem.
694 * The alternative would be to do the hash lookup in any case on bind
695 * which would introduce more runtime overhead than this.
696 */
697 bufObj->DeletePending = GL_TRUE;
698 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
699 }
700 }
701
702 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
703 }
704
705
706 /**
707 * Generate a set of unique buffer object IDs and store them in \c buffer.
708 *
709 * \param n Number of IDs to generate.
710 * \param buffer Array of \c n locations to store the IDs.
711 */
712 void GLAPIENTRY
713 _mesa_GenBuffersARB(GLsizei n, GLuint *buffer)
714 {
715 GET_CURRENT_CONTEXT(ctx);
716 GLuint first;
717 GLint i;
718 ASSERT_OUTSIDE_BEGIN_END(ctx);
719
720 if (MESA_VERBOSE & VERBOSE_API)
721 _mesa_debug(ctx, "glGenBuffers(%d)\n", n);
722
723 if (n < 0) {
724 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
725 return;
726 }
727
728 if (!buffer) {
729 return;
730 }
731
732 /*
733 * This must be atomic (generation and allocation of buffer object IDs)
734 */
735 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
736
737 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
738
739 /* Insert the ID and pointer to dummy buffer object into hash table */
740 for (i = 0; i < n; i++) {
741 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i,
742 &DummyBufferObject);
743 buffer[i] = first + i;
744 }
745
746 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
747 }
748
749
750 /**
751 * Determine if ID is the name of a buffer object.
752 *
753 * \param id ID of the potential buffer object.
754 * \return \c GL_TRUE if \c id is the name of a buffer object,
755 * \c GL_FALSE otherwise.
756 */
757 GLboolean GLAPIENTRY
758 _mesa_IsBufferARB(GLuint id)
759 {
760 struct gl_buffer_object *bufObj;
761 GET_CURRENT_CONTEXT(ctx);
762 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
763
764 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
765 bufObj = _mesa_lookup_bufferobj(ctx, id);
766 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
767
768 return bufObj && bufObj != &DummyBufferObject;
769 }
770
771
772 void GLAPIENTRY
773 _mesa_BufferDataARB(GLenum target, GLsizeiptrARB size,
774 const GLvoid * data, GLenum usage)
775 {
776 GET_CURRENT_CONTEXT(ctx);
777 struct gl_buffer_object *bufObj;
778 ASSERT_OUTSIDE_BEGIN_END(ctx);
779
780 if (MESA_VERBOSE & VERBOSE_API)
781 _mesa_debug(ctx, "glBufferData(%s, %ld, %p, %s)\n",
782 _mesa_lookup_enum_by_nr(target),
783 (long int) size, data,
784 _mesa_lookup_enum_by_nr(usage));
785
786 if (size < 0) {
787 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
788 return;
789 }
790
791 switch (usage) {
792 case GL_STREAM_DRAW_ARB:
793 case GL_STREAM_READ_ARB:
794 case GL_STREAM_COPY_ARB:
795 case GL_STATIC_DRAW_ARB:
796 case GL_STATIC_READ_ARB:
797 case GL_STATIC_COPY_ARB:
798 case GL_DYNAMIC_DRAW_ARB:
799 case GL_DYNAMIC_READ_ARB:
800 case GL_DYNAMIC_COPY_ARB:
801 /* OK */
802 break;
803 default:
804 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(usage)");
805 return;
806 }
807
808 bufObj = get_buffer(ctx, "glBufferDataARB", target);
809 if (!bufObj)
810 return;
811
812 if (_mesa_bufferobj_mapped(bufObj)) {
813 /* Unmap the existing buffer. We'll replace it now. Not an error. */
814 ctx->Driver.UnmapBuffer(ctx, bufObj);
815 bufObj->AccessFlags = default_access_mode(ctx);
816 ASSERT(bufObj->Pointer == NULL);
817 }
818
819 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
820
821 bufObj->Written = GL_TRUE;
822
823 #ifdef VBO_DEBUG
824 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
825 bufObj->Name, size, data, usage);
826 #endif
827
828 #ifdef BOUNDS_CHECK
829 size += 100;
830 #endif
831
832 ASSERT(ctx->Driver.BufferData);
833 if (!ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj )) {
834 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB()");
835 }
836 }
837
838
839 void GLAPIENTRY
840 _mesa_BufferSubDataARB(GLenum target, GLintptrARB offset,
841 GLsizeiptrARB size, const GLvoid * data)
842 {
843 GET_CURRENT_CONTEXT(ctx);
844 struct gl_buffer_object *bufObj;
845 ASSERT_OUTSIDE_BEGIN_END(ctx);
846
847 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
848 "glBufferSubDataARB" );
849 if (!bufObj) {
850 /* error already recorded */
851 return;
852 }
853
854 if (size == 0)
855 return;
856
857 bufObj->Written = GL_TRUE;
858
859 ASSERT(ctx->Driver.BufferSubData);
860 ctx->Driver.BufferSubData( ctx, offset, size, data, bufObj );
861 }
862
863
864 void GLAPIENTRY
865 _mesa_GetBufferSubDataARB(GLenum target, GLintptrARB offset,
866 GLsizeiptrARB size, void * data)
867 {
868 GET_CURRENT_CONTEXT(ctx);
869 struct gl_buffer_object *bufObj;
870 ASSERT_OUTSIDE_BEGIN_END(ctx);
871
872 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
873 "glGetBufferSubDataARB" );
874 if (!bufObj) {
875 /* error already recorded */
876 return;
877 }
878
879 ASSERT(ctx->Driver.GetBufferSubData);
880 ctx->Driver.GetBufferSubData( ctx, offset, size, data, bufObj );
881 }
882
883
884 void * GLAPIENTRY
885 _mesa_MapBufferARB(GLenum target, GLenum access)
886 {
887 GET_CURRENT_CONTEXT(ctx);
888 struct gl_buffer_object * bufObj;
889 GLbitfield accessFlags;
890 void *map;
891
892 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
893
894 switch (access) {
895 case GL_READ_ONLY_ARB:
896 accessFlags = GL_MAP_READ_BIT;
897 break;
898 case GL_WRITE_ONLY_ARB:
899 accessFlags = GL_MAP_WRITE_BIT;
900 break;
901 case GL_READ_WRITE_ARB:
902 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
903 break;
904 default:
905 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
906 return NULL;
907 }
908
909 bufObj = get_buffer(ctx, "glMapBufferARB", target);
910 if (!bufObj)
911 return NULL;
912
913 if (_mesa_bufferobj_mapped(bufObj)) {
914 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
915 return NULL;
916 }
917
918 if (!bufObj->Size) {
919 _mesa_error(ctx, GL_OUT_OF_MEMORY,
920 "glMapBuffer(buffer size = 0)");
921 return NULL;
922 }
923
924 ASSERT(ctx->Driver.MapBufferRange);
925 map = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size, accessFlags, bufObj);
926 if (!map) {
927 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
928 return NULL;
929 }
930 else {
931 /* The driver callback should have set these fields.
932 * This is important because other modules (like VBO) might call
933 * the driver function directly.
934 */
935 ASSERT(bufObj->Pointer == map);
936 ASSERT(bufObj->Length == bufObj->Size);
937 ASSERT(bufObj->Offset == 0);
938 bufObj->AccessFlags = accessFlags;
939 }
940
941 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
942 bufObj->Written = GL_TRUE;
943
944 #ifdef VBO_DEBUG
945 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
946 bufObj->Name, bufObj->Size, access);
947 if (access == GL_WRITE_ONLY_ARB) {
948 GLuint i;
949 GLubyte *b = (GLubyte *) bufObj->Pointer;
950 for (i = 0; i < bufObj->Size; i++)
951 b[i] = i & 0xff;
952 }
953 #endif
954
955 #ifdef BOUNDS_CHECK
956 {
957 GLubyte *buf = (GLubyte *) bufObj->Pointer;
958 GLuint i;
959 /* buffer is 100 bytes larger than requested, fill with magic value */
960 for (i = 0; i < 100; i++) {
961 buf[bufObj->Size - i - 1] = 123;
962 }
963 }
964 #endif
965
966 return bufObj->Pointer;
967 }
968
969
970 GLboolean GLAPIENTRY
971 _mesa_UnmapBufferARB(GLenum target)
972 {
973 GET_CURRENT_CONTEXT(ctx);
974 struct gl_buffer_object *bufObj;
975 GLboolean status = GL_TRUE;
976 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
977
978 bufObj = get_buffer(ctx, "glUnmapBufferARB", target);
979 if (!bufObj)
980 return GL_FALSE;
981
982 if (!_mesa_bufferobj_mapped(bufObj)) {
983 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
984 return GL_FALSE;
985 }
986
987 #ifdef BOUNDS_CHECK
988 if (bufObj->Access != GL_READ_ONLY_ARB) {
989 GLubyte *buf = (GLubyte *) bufObj->Pointer;
990 GLuint i;
991 /* check that last 100 bytes are still = magic value */
992 for (i = 0; i < 100; i++) {
993 GLuint pos = bufObj->Size - i - 1;
994 if (buf[pos] != 123) {
995 _mesa_warning(ctx, "Out of bounds buffer object write detected"
996 " at position %d (value = %u)\n",
997 pos, buf[pos]);
998 }
999 }
1000 }
1001 #endif
1002
1003 #ifdef VBO_DEBUG
1004 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1005 GLuint i, unchanged = 0;
1006 GLubyte *b = (GLubyte *) bufObj->Pointer;
1007 GLint pos = -1;
1008 /* check which bytes changed */
1009 for (i = 0; i < bufObj->Size - 1; i++) {
1010 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1011 unchanged++;
1012 if (pos == -1)
1013 pos = i;
1014 }
1015 }
1016 if (unchanged) {
1017 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1018 bufObj->Name, unchanged, bufObj->Size, pos);
1019 }
1020 }
1021 #endif
1022
1023 status = ctx->Driver.UnmapBuffer( ctx, bufObj );
1024 bufObj->AccessFlags = default_access_mode(ctx);
1025 ASSERT(bufObj->Pointer == NULL);
1026 ASSERT(bufObj->Offset == 0);
1027 ASSERT(bufObj->Length == 0);
1028
1029 return status;
1030 }
1031
1032
1033 void GLAPIENTRY
1034 _mesa_GetBufferParameterivARB(GLenum target, GLenum pname, GLint *params)
1035 {
1036 GET_CURRENT_CONTEXT(ctx);
1037 struct gl_buffer_object *bufObj;
1038 ASSERT_OUTSIDE_BEGIN_END(ctx);
1039
1040 bufObj = get_buffer(ctx, "glGetBufferParameterivARB", target);
1041 if (!bufObj)
1042 return;
1043
1044 switch (pname) {
1045 case GL_BUFFER_SIZE_ARB:
1046 *params = (GLint) bufObj->Size;
1047 return;
1048 case GL_BUFFER_USAGE_ARB:
1049 *params = bufObj->Usage;
1050 return;
1051 case GL_BUFFER_ACCESS_ARB:
1052 *params = simplified_access_mode(bufObj->AccessFlags);
1053 return;
1054 case GL_BUFFER_MAPPED_ARB:
1055 *params = _mesa_bufferobj_mapped(bufObj);
1056 return;
1057 case GL_BUFFER_ACCESS_FLAGS:
1058 if (!ctx->Extensions.ARB_map_buffer_range)
1059 goto invalid_pname;
1060 *params = bufObj->AccessFlags;
1061 return;
1062 case GL_BUFFER_MAP_OFFSET:
1063 if (!ctx->Extensions.ARB_map_buffer_range)
1064 goto invalid_pname;
1065 *params = (GLint) bufObj->Offset;
1066 return;
1067 case GL_BUFFER_MAP_LENGTH:
1068 if (!ctx->Extensions.ARB_map_buffer_range)
1069 goto invalid_pname;
1070 *params = (GLint) bufObj->Length;
1071 return;
1072 default:
1073 ; /* fall-through */
1074 }
1075
1076 invalid_pname:
1077 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname=%s)",
1078 _mesa_lookup_enum_by_nr(pname));
1079 }
1080
1081
1082 /**
1083 * New in GL 3.2
1084 * This is pretty much a duplicate of GetBufferParameteriv() but the
1085 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1086 */
1087 void GLAPIENTRY
1088 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
1089 {
1090 GET_CURRENT_CONTEXT(ctx);
1091 struct gl_buffer_object *bufObj;
1092 ASSERT_OUTSIDE_BEGIN_END(ctx);
1093
1094 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target);
1095 if (!bufObj)
1096 return;
1097
1098 switch (pname) {
1099 case GL_BUFFER_SIZE_ARB:
1100 *params = bufObj->Size;
1101 return;
1102 case GL_BUFFER_USAGE_ARB:
1103 *params = bufObj->Usage;
1104 return;
1105 case GL_BUFFER_ACCESS_ARB:
1106 *params = simplified_access_mode(bufObj->AccessFlags);
1107 return;
1108 case GL_BUFFER_ACCESS_FLAGS:
1109 if (!ctx->Extensions.ARB_map_buffer_range)
1110 goto invalid_pname;
1111 *params = bufObj->AccessFlags;
1112 return;
1113 case GL_BUFFER_MAPPED_ARB:
1114 *params = _mesa_bufferobj_mapped(bufObj);
1115 return;
1116 case GL_BUFFER_MAP_OFFSET:
1117 if (!ctx->Extensions.ARB_map_buffer_range)
1118 goto invalid_pname;
1119 *params = bufObj->Offset;
1120 return;
1121 case GL_BUFFER_MAP_LENGTH:
1122 if (!ctx->Extensions.ARB_map_buffer_range)
1123 goto invalid_pname;
1124 *params = bufObj->Length;
1125 return;
1126 default:
1127 ; /* fall-through */
1128 }
1129
1130 invalid_pname:
1131 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
1132 _mesa_lookup_enum_by_nr(pname));
1133 }
1134
1135
1136 void GLAPIENTRY
1137 _mesa_GetBufferPointervARB(GLenum target, GLenum pname, GLvoid **params)
1138 {
1139 GET_CURRENT_CONTEXT(ctx);
1140 struct gl_buffer_object * bufObj;
1141 ASSERT_OUTSIDE_BEGIN_END(ctx);
1142
1143 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1144 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1145 return;
1146 }
1147
1148 bufObj = get_buffer(ctx, "glGetBufferPointervARB", target);
1149 if (!bufObj)
1150 return;
1151
1152 *params = bufObj->Pointer;
1153 }
1154
1155 /**
1156 * See GL_ARB_map_buffer_range spec
1157 */
1158 void * GLAPIENTRY
1159 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
1160 GLbitfield access)
1161 {
1162 GET_CURRENT_CONTEXT(ctx);
1163 struct gl_buffer_object *bufObj;
1164 void *map;
1165
1166 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1167
1168 if (!ctx->Extensions.ARB_map_buffer_range) {
1169 _mesa_error(ctx, GL_INVALID_OPERATION,
1170 "glMapBufferRange(extension not supported)");
1171 return NULL;
1172 }
1173
1174 if (offset < 0) {
1175 _mesa_error(ctx, GL_INVALID_VALUE,
1176 "glMapBufferRange(offset = %ld)", (long)offset);
1177 return NULL;
1178 }
1179
1180 if (length < 0) {
1181 _mesa_error(ctx, GL_INVALID_VALUE,
1182 "glMapBufferRange(length = %ld)", (long)length);
1183 return NULL;
1184 }
1185
1186 if (access & ~(GL_MAP_READ_BIT |
1187 GL_MAP_WRITE_BIT |
1188 GL_MAP_INVALIDATE_RANGE_BIT |
1189 GL_MAP_INVALIDATE_BUFFER_BIT |
1190 GL_MAP_FLUSH_EXPLICIT_BIT |
1191 GL_MAP_UNSYNCHRONIZED_BIT)) {
1192 /* generate an error if any undefind bit is set */
1193 _mesa_error(ctx, GL_INVALID_VALUE, "glMapBufferRange(access)");
1194 return NULL;
1195 }
1196
1197 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
1198 _mesa_error(ctx, GL_INVALID_OPERATION,
1199 "glMapBufferRange(access indicates neither read or write)");
1200 return NULL;
1201 }
1202
1203 if ((access & GL_MAP_READ_BIT) &&
1204 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
1205 GL_MAP_INVALIDATE_BUFFER_BIT |
1206 GL_MAP_UNSYNCHRONIZED_BIT))) {
1207 _mesa_error(ctx, GL_INVALID_OPERATION,
1208 "glMapBufferRange(invalid access flags)");
1209 return NULL;
1210 }
1211
1212 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
1213 ((access & GL_MAP_WRITE_BIT) == 0)) {
1214 _mesa_error(ctx, GL_INVALID_OPERATION,
1215 "glMapBufferRange(invalid access flags)");
1216 return NULL;
1217 }
1218
1219 bufObj = get_buffer(ctx, "glMapBufferRange", target);
1220 if (!bufObj)
1221 return NULL;
1222
1223 if (offset + length > bufObj->Size) {
1224 _mesa_error(ctx, GL_INVALID_VALUE,
1225 "glMapBufferRange(offset + length > size)");
1226 return NULL;
1227 }
1228
1229 if (_mesa_bufferobj_mapped(bufObj)) {
1230 _mesa_error(ctx, GL_INVALID_OPERATION,
1231 "glMapBufferRange(buffer already mapped)");
1232 return NULL;
1233 }
1234
1235 if (!bufObj->Size) {
1236 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1237 "glMapBufferRange(buffer size = 0)");
1238 return NULL;
1239 }
1240
1241 /* Mapping zero bytes should return a non-null pointer. */
1242 if (!length) {
1243 static long dummy = 0;
1244 bufObj->Pointer = &dummy;
1245 bufObj->Length = length;
1246 bufObj->Offset = offset;
1247 bufObj->AccessFlags = access;
1248 return bufObj->Pointer;
1249 }
1250
1251 ASSERT(ctx->Driver.MapBufferRange);
1252 map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj);
1253 if (!map) {
1254 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1255 }
1256 else {
1257 /* The driver callback should have set all these fields.
1258 * This is important because other modules (like VBO) might call
1259 * the driver function directly.
1260 */
1261 ASSERT(bufObj->Pointer == map);
1262 ASSERT(bufObj->Length == length);
1263 ASSERT(bufObj->Offset == offset);
1264 ASSERT(bufObj->AccessFlags == access);
1265 }
1266
1267 return map;
1268 }
1269
1270
1271 /**
1272 * See GL_ARB_map_buffer_range spec
1273 */
1274 void GLAPIENTRY
1275 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
1276 {
1277 GET_CURRENT_CONTEXT(ctx);
1278 struct gl_buffer_object *bufObj;
1279 ASSERT_OUTSIDE_BEGIN_END(ctx);
1280
1281 if (!ctx->Extensions.ARB_map_buffer_range) {
1282 _mesa_error(ctx, GL_INVALID_OPERATION,
1283 "glFlushMappedBufferRange(extension not supported)");
1284 return;
1285 }
1286
1287 if (offset < 0) {
1288 _mesa_error(ctx, GL_INVALID_VALUE,
1289 "glFlushMappedBufferRange(offset = %ld)", (long)offset);
1290 return;
1291 }
1292
1293 if (length < 0) {
1294 _mesa_error(ctx, GL_INVALID_VALUE,
1295 "glFlushMappedBufferRange(length = %ld)", (long)length);
1296 return;
1297 }
1298
1299 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target);
1300 if (!bufObj)
1301 return;
1302
1303 if (!_mesa_bufferobj_mapped(bufObj)) {
1304 /* buffer is not mapped */
1305 _mesa_error(ctx, GL_INVALID_OPERATION,
1306 "glFlushMappedBufferRange(buffer is not mapped)");
1307 return;
1308 }
1309
1310 if ((bufObj->AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
1311 _mesa_error(ctx, GL_INVALID_OPERATION,
1312 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1313 return;
1314 }
1315
1316 if (offset + length > bufObj->Length) {
1317 _mesa_error(ctx, GL_INVALID_VALUE,
1318 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1319 (long)offset, (long)length, (long)bufObj->Length);
1320 return;
1321 }
1322
1323 ASSERT(bufObj->AccessFlags & GL_MAP_WRITE_BIT);
1324
1325 if (ctx->Driver.FlushMappedBufferRange)
1326 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj);
1327 }
1328
1329
1330 #if FEATURE_APPLE_object_purgeable
1331 static GLenum
1332 buffer_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
1333 {
1334 struct gl_buffer_object *bufObj;
1335 GLenum retval;
1336
1337 bufObj = _mesa_lookup_bufferobj(ctx, name);
1338 if (!bufObj) {
1339 _mesa_error(ctx, GL_INVALID_VALUE,
1340 "glObjectPurgeable(name = 0x%x)", name);
1341 return 0;
1342 }
1343 if (!_mesa_is_bufferobj(bufObj)) {
1344 _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
1345 return 0;
1346 }
1347
1348 if (bufObj->Purgeable) {
1349 _mesa_error(ctx, GL_INVALID_OPERATION,
1350 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1351 return GL_VOLATILE_APPLE;
1352 }
1353
1354 bufObj->Purgeable = GL_TRUE;
1355
1356 retval = GL_VOLATILE_APPLE;
1357 if (ctx->Driver.BufferObjectPurgeable)
1358 retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
1359
1360 return retval;
1361 }
1362
1363 static GLenum
1364 texture_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
1365 {
1366 struct gl_texture_object *bufObj;
1367 GLenum retval;
1368
1369 bufObj = _mesa_lookup_texture(ctx, name);
1370 if (!bufObj) {
1371 _mesa_error(ctx, GL_INVALID_VALUE,
1372 "glObjectPurgeable(name = 0x%x)", name);
1373 return 0;
1374 }
1375
1376 if (bufObj->Purgeable) {
1377 _mesa_error(ctx, GL_INVALID_OPERATION,
1378 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1379 return GL_VOLATILE_APPLE;
1380 }
1381
1382 bufObj->Purgeable = GL_TRUE;
1383
1384 retval = GL_VOLATILE_APPLE;
1385 if (ctx->Driver.TextureObjectPurgeable)
1386 retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
1387
1388 return retval;
1389 }
1390
1391
1392 GLenum GLAPIENTRY
1393 _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
1394 {
1395 GLenum retval;
1396
1397 GET_CURRENT_CONTEXT(ctx);
1398 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1399
1400 if (name == 0) {
1401 _mesa_error(ctx, GL_INVALID_VALUE,
1402 "glObjectPurgeable(name = 0x%x)", name);
1403 return 0;
1404 }
1405
1406 switch (option) {
1407 case GL_VOLATILE_APPLE:
1408 case GL_RELEASED_APPLE:
1409 /* legal */
1410 break;
1411 default:
1412 _mesa_error(ctx, GL_INVALID_ENUM,
1413 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1414 name, option);
1415 return 0;
1416 }
1417
1418 switch (objectType) {
1419 case GL_TEXTURE:
1420 retval = texture_object_purgeable(ctx, name, option);
1421 break;
1422 case GL_BUFFER_OBJECT_APPLE:
1423 retval = buffer_object_purgeable(ctx, name, option);
1424 break;
1425 default:
1426 _mesa_error(ctx, GL_INVALID_ENUM,
1427 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1428 name, objectType);
1429 return 0;
1430 }
1431
1432 /* In strict conformance to the spec, we must only return VOLATILE when
1433 * when passed the VOLATILE option. Madness.
1434 *
1435 * XXX First fix the spec, then fix me.
1436 */
1437 return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
1438 }
1439
1440
1441 static GLenum
1442 buffer_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
1443 {
1444 struct gl_buffer_object *bufObj;
1445 GLenum retval;
1446
1447 bufObj = _mesa_lookup_bufferobj(ctx, name);
1448 if (!bufObj) {
1449 _mesa_error(ctx, GL_INVALID_VALUE,
1450 "glObjectUnpurgeable(name = 0x%x)", name);
1451 return 0;
1452 }
1453
1454 if (! bufObj->Purgeable) {
1455 _mesa_error(ctx, GL_INVALID_OPERATION,
1456 "glObjectUnpurgeable(name = 0x%x) object is "
1457 " already \"unpurged\"", name);
1458 return 0;
1459 }
1460
1461 bufObj->Purgeable = GL_FALSE;
1462
1463 retval = option;
1464 if (ctx->Driver.BufferObjectUnpurgeable)
1465 retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
1466
1467 return retval;
1468 }
1469
1470 static GLenum
1471 texture_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
1472 {
1473 struct gl_texture_object *bufObj;
1474 GLenum retval;
1475
1476 bufObj = _mesa_lookup_texture(ctx, name);
1477 if (!bufObj) {
1478 _mesa_error(ctx, GL_INVALID_VALUE,
1479 "glObjectUnpurgeable(name = 0x%x)", name);
1480 return 0;
1481 }
1482
1483 if (! bufObj->Purgeable) {
1484 _mesa_error(ctx, GL_INVALID_OPERATION,
1485 "glObjectUnpurgeable(name = 0x%x) object is"
1486 " already \"unpurged\"", name);
1487 return 0;
1488 }
1489
1490 bufObj->Purgeable = GL_FALSE;
1491
1492 retval = option;
1493 if (ctx->Driver.TextureObjectUnpurgeable)
1494 retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
1495
1496 return retval;
1497 }
1498
1499
1500 GLenum GLAPIENTRY
1501 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
1502 {
1503 GET_CURRENT_CONTEXT(ctx);
1504 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1505
1506 if (name == 0) {
1507 _mesa_error(ctx, GL_INVALID_VALUE,
1508 "glObjectUnpurgeable(name = 0x%x)", name);
1509 return 0;
1510 }
1511
1512 switch (option) {
1513 case GL_RETAINED_APPLE:
1514 case GL_UNDEFINED_APPLE:
1515 /* legal */
1516 break;
1517 default:
1518 _mesa_error(ctx, GL_INVALID_ENUM,
1519 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1520 name, option);
1521 return 0;
1522 }
1523
1524 switch (objectType) {
1525 case GL_BUFFER_OBJECT_APPLE:
1526 return buffer_object_unpurgeable(ctx, name, option);
1527 case GL_TEXTURE:
1528 return texture_object_unpurgeable(ctx, name, option);
1529 default:
1530 _mesa_error(ctx, GL_INVALID_ENUM,
1531 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1532 name, objectType);
1533 return 0;
1534 }
1535 }
1536
1537
1538 static void
1539 get_buffer_object_parameteriv(struct gl_context *ctx, GLuint name,
1540 GLenum pname, GLint *params)
1541 {
1542 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
1543 if (!bufObj) {
1544 _mesa_error(ctx, GL_INVALID_VALUE,
1545 "glGetObjectParameteriv(name = 0x%x) invalid object", name);
1546 return;
1547 }
1548
1549 switch (pname) {
1550 case GL_PURGEABLE_APPLE:
1551 *params = bufObj->Purgeable;
1552 break;
1553 default:
1554 _mesa_error(ctx, GL_INVALID_ENUM,
1555 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1556 name, pname);
1557 break;
1558 }
1559 }
1560
1561 static void
1562 get_texture_object_parameteriv(struct gl_context *ctx, GLuint name,
1563 GLenum pname, GLint *params)
1564 {
1565 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
1566 if (!texObj) {
1567 _mesa_error(ctx, GL_INVALID_VALUE,
1568 "glObjectUnpurgeable(name = 0x%x)", name);
1569 return;
1570 }
1571
1572 switch (pname) {
1573 case GL_PURGEABLE_APPLE:
1574 *params = texObj->Purgeable;
1575 break;
1576 default:
1577 _mesa_error(ctx, GL_INVALID_ENUM,
1578 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1579 name, pname);
1580 break;
1581 }
1582 }
1583
1584
1585 void GLAPIENTRY
1586 _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
1587 GLint *params)
1588 {
1589 GET_CURRENT_CONTEXT(ctx);
1590
1591 if (name == 0) {
1592 _mesa_error(ctx, GL_INVALID_VALUE,
1593 "glGetObjectParameteriv(name = 0x%x)", name);
1594 return;
1595 }
1596
1597 switch (objectType) {
1598 case GL_TEXTURE:
1599 get_texture_object_parameteriv(ctx, name, pname, params);
1600 break;
1601 case GL_BUFFER_OBJECT_APPLE:
1602 get_buffer_object_parameteriv(ctx, name, pname, params);
1603 break;
1604 default:
1605 _mesa_error(ctx, GL_INVALID_ENUM,
1606 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1607 name, objectType);
1608 }
1609 }
1610
1611 #endif /* FEATURE_APPLE_object_purgeable */