mesa: Unbind ARB_copy_buffer and transform feedback buffers on delete.
[mesa.git] / src / 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
34 #include "glheader.h"
35 #include "enums.h"
36 #include "hash.h"
37 #include "imports.h"
38 #include "image.h"
39 #include "context.h"
40 #include "bufferobj.h"
41 #include "fbobject.h"
42 #include "mfeatures.h"
43 #include "mtypes.h"
44 #include "texobj.h"
45
46
47 /* Debug flags */
48 /*#define VBO_DEBUG*/
49 /*#define BOUNDS_CHECK*/
50
51
52 /**
53 * Used as a placeholder for buffer objects between glGenBuffers() and
54 * glBindBuffer() so that glIsBuffer() can work correctly.
55 */
56 static struct gl_buffer_object DummyBufferObject;
57
58
59 /**
60 * Return pointer to address of a buffer object target.
61 * \param ctx the GL context
62 * \param target the buffer object target to be retrieved.
63 * \return pointer to pointer to the buffer object bound to \c target in the
64 * specified context or \c NULL if \c target is invalid.
65 */
66 static inline struct gl_buffer_object **
67 get_buffer_target(struct gl_context *ctx, GLenum target)
68 {
69 switch (target) {
70 case GL_ARRAY_BUFFER_ARB:
71 return &ctx->Array.ArrayBufferObj;
72 case GL_ELEMENT_ARRAY_BUFFER_ARB:
73 return &ctx->Array.ArrayObj->ElementArrayBufferObj;
74 case GL_PIXEL_PACK_BUFFER_EXT:
75 return &ctx->Pack.BufferObj;
76 case GL_PIXEL_UNPACK_BUFFER_EXT:
77 return &ctx->Unpack.BufferObj;
78 case GL_COPY_READ_BUFFER:
79 return &ctx->CopyReadBuffer;
80 case GL_COPY_WRITE_BUFFER:
81 return &ctx->CopyWriteBuffer;
82 #if FEATURE_EXT_transform_feedback
83 case GL_TRANSFORM_FEEDBACK_BUFFER:
84 if (ctx->Extensions.EXT_transform_feedback) {
85 return &ctx->TransformFeedback.CurrentBuffer;
86 }
87 break;
88 #endif
89 case GL_TEXTURE_BUFFER:
90 if (ctx->Extensions.ARB_texture_buffer_object) {
91 return &ctx->Texture.BufferObject;
92 }
93 break;
94 default:
95 return NULL;
96 }
97 return NULL;
98 }
99
100
101 /**
102 * Get the buffer object bound to the specified target in a GL context.
103 * \param ctx the GL context
104 * \param target the buffer object target to be retrieved.
105 * \return pointer to the buffer object bound to \c target in the
106 * specified context or \c NULL if \c target is invalid.
107 */
108 static inline struct gl_buffer_object *
109 get_buffer(struct gl_context *ctx, const char *func, GLenum target)
110 {
111 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
112
113 if (!bufObj) {
114 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
115 return NULL;
116 }
117
118 if (!_mesa_is_bufferobj(*bufObj)) {
119 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(buffer 0)", func);
120 return NULL;
121 }
122
123 return *bufObj;
124 }
125
126
127 static inline GLenum
128 default_access_mode(const struct gl_context *ctx)
129 {
130 /* Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
131 *
132 * Name Type Initial Value Legal Values
133 * ... ... ... ...
134 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
135 * READ_WRITE
136 *
137 * However, table 6.8 in the GL_OES_mapbuffer extension says:
138 *
139 * Get Value Type Get Command Value Description
140 * --------- ---- ----------- ----- -----------
141 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
142 *
143 * The difference is because GL_OES_mapbuffer only supports mapping buffers
144 * write-only.
145 */
146 return (ctx->API == API_OPENGLES)
147 ? GL_MAP_WRITE_BIT : (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT);
148 }
149
150
151 /**
152 * Convert a GLbitfield describing the mapped buffer access flags
153 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
154 */
155 static GLenum
156 simplified_access_mode(GLbitfield access)
157 {
158 const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
159 if ((access & rwFlags) == rwFlags)
160 return GL_READ_WRITE;
161 if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
162 return GL_READ_ONLY;
163 if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
164 return GL_WRITE_ONLY;
165 return GL_READ_WRITE; /* this should never happen, but no big deal */
166 }
167
168
169 /**
170 * Tests the subdata range parameters and sets the GL error code for
171 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
172 *
173 * \param ctx GL context.
174 * \param target Buffer object target on which to operate.
175 * \param offset Offset of the first byte of the subdata range.
176 * \param size Size, in bytes, of the subdata range.
177 * \param caller Name of calling function for recording errors.
178 * \return A pointer to the buffer object bound to \c target in the
179 * specified context or \c NULL if any of the parameter or state
180 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
181 * are invalid.
182 *
183 * \sa glBufferSubDataARB, glGetBufferSubDataARB
184 */
185 static struct gl_buffer_object *
186 buffer_object_subdata_range_good( struct gl_context * ctx, GLenum target,
187 GLintptrARB offset, GLsizeiptrARB size,
188 const char *caller )
189 {
190 struct gl_buffer_object *bufObj;
191
192 if (size < 0) {
193 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
194 return NULL;
195 }
196
197 if (offset < 0) {
198 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
199 return NULL;
200 }
201
202 bufObj = get_buffer(ctx, caller, target);
203 if (!bufObj)
204 return NULL;
205
206 if (offset + size > bufObj->Size) {
207 _mesa_error(ctx, GL_INVALID_VALUE,
208 "%s(offset %lu + size %lu > buffer size %lu)", caller,
209 (unsigned long) offset,
210 (unsigned long) size,
211 (unsigned long) bufObj->Size);
212 return NULL;
213 }
214 if (_mesa_bufferobj_mapped(bufObj)) {
215 /* Buffer is currently mapped */
216 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
217 return NULL;
218 }
219
220 return bufObj;
221 }
222
223
224 /**
225 * Allocate and initialize a new buffer object.
226 *
227 * Default callback for the \c dd_function_table::NewBufferObject() hook.
228 */
229 static struct gl_buffer_object *
230 _mesa_new_buffer_object( struct gl_context *ctx, GLuint name, GLenum target )
231 {
232 struct gl_buffer_object *obj;
233
234 (void) ctx;
235
236 obj = MALLOC_STRUCT(gl_buffer_object);
237 _mesa_initialize_buffer_object(ctx, obj, name, target);
238 return obj;
239 }
240
241
242 /**
243 * Delete a buffer object.
244 *
245 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
246 */
247 static void
248 _mesa_delete_buffer_object(struct gl_context *ctx,
249 struct gl_buffer_object *bufObj)
250 {
251 (void) ctx;
252
253 if (bufObj->Data)
254 free(bufObj->Data);
255
256 /* assign strange values here to help w/ debugging */
257 bufObj->RefCount = -1000;
258 bufObj->Name = ~0;
259
260 _glthread_DESTROY_MUTEX(bufObj->Mutex);
261 free(bufObj);
262 }
263
264
265
266 /**
267 * Set ptr to bufObj w/ reference counting.
268 * This is normally only called from the _mesa_reference_buffer_object() macro
269 * when there's a real pointer change.
270 */
271 void
272 _mesa_reference_buffer_object_(struct gl_context *ctx,
273 struct gl_buffer_object **ptr,
274 struct gl_buffer_object *bufObj)
275 {
276 if (*ptr) {
277 /* Unreference the old buffer */
278 GLboolean deleteFlag = GL_FALSE;
279 struct gl_buffer_object *oldObj = *ptr;
280
281 _glthread_LOCK_MUTEX(oldObj->Mutex);
282 ASSERT(oldObj->RefCount > 0);
283 oldObj->RefCount--;
284 #if 0
285 printf("BufferObj %p %d DECR to %d\n",
286 (void *) oldObj, oldObj->Name, oldObj->RefCount);
287 #endif
288 deleteFlag = (oldObj->RefCount == 0);
289 _glthread_UNLOCK_MUTEX(oldObj->Mutex);
290
291 if (deleteFlag) {
292
293 /* some sanity checking: don't delete a buffer still in use */
294 #if 0
295 /* unfortunately, these tests are invalid during context tear-down */
296 ASSERT(ctx->Array.ArrayBufferObj != bufObj);
297 ASSERT(ctx->Array.ArrayObj->ElementArrayBufferObj != bufObj);
298 ASSERT(ctx->Array.ArrayObj->Vertex.BufferObj != bufObj);
299 #endif
300
301 ASSERT(ctx->Driver.DeleteBuffer);
302 ctx->Driver.DeleteBuffer(ctx, oldObj);
303 }
304
305 *ptr = NULL;
306 }
307 ASSERT(!*ptr);
308
309 if (bufObj) {
310 /* reference new buffer */
311 _glthread_LOCK_MUTEX(bufObj->Mutex);
312 if (bufObj->RefCount == 0) {
313 /* this buffer's being deleted (look just above) */
314 /* Not sure this can every really happen. Warn if it does. */
315 _mesa_problem(NULL, "referencing deleted buffer object");
316 *ptr = NULL;
317 }
318 else {
319 bufObj->RefCount++;
320 #if 0
321 printf("BufferObj %p %d INCR to %d\n",
322 (void *) bufObj, bufObj->Name, bufObj->RefCount);
323 #endif
324 *ptr = bufObj;
325 }
326 _glthread_UNLOCK_MUTEX(bufObj->Mutex);
327 }
328 }
329
330
331 /**
332 * Initialize a buffer object to default values.
333 */
334 void
335 _mesa_initialize_buffer_object( struct gl_context *ctx,
336 struct gl_buffer_object *obj,
337 GLuint name, GLenum target )
338 {
339 (void) target;
340
341 memset(obj, 0, sizeof(struct gl_buffer_object));
342 _glthread_INIT_MUTEX(obj->Mutex);
343 obj->RefCount = 1;
344 obj->Name = name;
345 obj->Usage = GL_STATIC_DRAW_ARB;
346 obj->AccessFlags = default_access_mode(ctx);
347 }
348
349
350
351 /**
352 * Callback called from _mesa_HashWalk()
353 */
354 static void
355 count_buffer_size(GLuint key, void *data, void *userData)
356 {
357 const struct gl_buffer_object *bufObj =
358 (const struct gl_buffer_object *) data;
359 GLuint *total = (GLuint *) userData;
360
361 *total = *total + bufObj->Size;
362 }
363
364
365 /**
366 * Compute total size (in bytes) of all buffer objects for the given context.
367 * For debugging purposes.
368 */
369 GLuint
370 _mesa_total_buffer_object_memory(struct gl_context *ctx)
371 {
372 GLuint total = 0;
373
374 _mesa_HashWalk(ctx->Shared->BufferObjects, count_buffer_size, &total);
375
376 return total;
377 }
378
379
380 /**
381 * Allocate space for and store data in a buffer object. Any data that was
382 * previously stored in the buffer object is lost. If \c data is \c NULL,
383 * memory will be allocated, but no copy will occur.
384 *
385 * This is the default callback for \c dd_function_table::BufferData()
386 * Note that all GL error checking will have been done already.
387 *
388 * \param ctx GL context.
389 * \param target Buffer object target on which to operate.
390 * \param size Size, in bytes, of the new data store.
391 * \param data Pointer to the data to store in the buffer object. This
392 * pointer may be \c NULL.
393 * \param usage Hints about how the data will be used.
394 * \param bufObj Object to be used.
395 *
396 * \return GL_TRUE for success, GL_FALSE for failure
397 * \sa glBufferDataARB, dd_function_table::BufferData.
398 */
399 static GLboolean
400 _mesa_buffer_data( struct gl_context *ctx, GLenum target, GLsizeiptrARB size,
401 const GLvoid * data, GLenum usage,
402 struct gl_buffer_object * bufObj )
403 {
404 void * new_data;
405
406 (void) ctx; (void) target;
407
408 new_data = _mesa_realloc( bufObj->Data, bufObj->Size, size );
409 if (new_data) {
410 bufObj->Data = (GLubyte *) new_data;
411 bufObj->Size = size;
412 bufObj->Usage = usage;
413
414 if (data) {
415 memcpy( bufObj->Data, data, size );
416 }
417
418 return GL_TRUE;
419 }
420 else {
421 return GL_FALSE;
422 }
423 }
424
425
426 /**
427 * Replace data in a subrange of buffer object. If the data range
428 * specified by \c size + \c offset extends beyond the end of the buffer or
429 * if \c data is \c NULL, no copy is performed.
430 *
431 * This is the default callback for \c dd_function_table::BufferSubData()
432 * Note that all GL error checking will have been done already.
433 *
434 * \param ctx GL context.
435 * \param target Buffer object target on which to operate.
436 * \param offset Offset of the first byte to be modified.
437 * \param size Size, in bytes, of the data range.
438 * \param data Pointer to the data to store in the buffer object.
439 * \param bufObj Object to be used.
440 *
441 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
442 */
443 static void
444 _mesa_buffer_subdata( struct gl_context *ctx, GLintptrARB offset,
445 GLsizeiptrARB size, const GLvoid * data,
446 struct gl_buffer_object * bufObj )
447 {
448 (void) ctx;
449
450 /* this should have been caught in _mesa_BufferSubData() */
451 ASSERT(size + offset <= bufObj->Size);
452
453 if (bufObj->Data) {
454 memcpy( (GLubyte *) bufObj->Data + offset, data, size );
455 }
456 }
457
458
459 /**
460 * Retrieve data from a subrange of buffer object. If the data range
461 * specified by \c size + \c offset extends beyond the end of the buffer or
462 * if \c data is \c NULL, no copy is performed.
463 *
464 * This is the default callback for \c dd_function_table::GetBufferSubData()
465 * Note that all GL error checking will have been done already.
466 *
467 * \param ctx GL context.
468 * \param target Buffer object target on which to operate.
469 * \param offset Offset of the first byte to be fetched.
470 * \param size Size, in bytes, of the data range.
471 * \param data Destination for data
472 * \param bufObj Object to be used.
473 *
474 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
475 */
476 static void
477 _mesa_buffer_get_subdata( struct gl_context *ctx, GLintptrARB offset,
478 GLsizeiptrARB size, GLvoid * data,
479 struct gl_buffer_object * bufObj )
480 {
481 (void) ctx;
482
483 if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
484 memcpy( data, (GLubyte *) bufObj->Data + offset, size );
485 }
486 }
487
488
489 /**
490 * Default fallback for \c dd_function_table::MapBufferRange().
491 * Called via glMapBufferRange().
492 */
493 static void *
494 _mesa_buffer_map_range( struct gl_context *ctx, GLintptr offset,
495 GLsizeiptr length, GLbitfield access,
496 struct gl_buffer_object *bufObj )
497 {
498 (void) ctx;
499 assert(!_mesa_bufferobj_mapped(bufObj));
500 /* Just return a direct pointer to the data */
501 bufObj->Pointer = bufObj->Data + offset;
502 bufObj->Length = length;
503 bufObj->Offset = offset;
504 bufObj->AccessFlags = access;
505 return bufObj->Pointer;
506 }
507
508
509 /**
510 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
511 * Called via glFlushMappedBufferRange().
512 */
513 static void
514 _mesa_buffer_flush_mapped_range( struct gl_context *ctx,
515 GLintptr offset, GLsizeiptr length,
516 struct gl_buffer_object *obj )
517 {
518 (void) ctx;
519 (void) offset;
520 (void) length;
521 (void) obj;
522 /* no-op */
523 }
524
525
526 /**
527 * Default callback for \c dd_function_table::MapBuffer().
528 *
529 * The input parameters will have been already tested for errors.
530 *
531 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
532 */
533 static GLboolean
534 _mesa_buffer_unmap( struct gl_context *ctx, struct gl_buffer_object *bufObj )
535 {
536 (void) ctx;
537 /* XXX we might assert here that bufObj->Pointer is non-null */
538 bufObj->Pointer = NULL;
539 bufObj->Length = 0;
540 bufObj->Offset = 0;
541 bufObj->AccessFlags = 0x0;
542 return GL_TRUE;
543 }
544
545
546 /**
547 * Default fallback for \c dd_function_table::CopyBufferSubData().
548 * Called via glCopyBuffserSubData().
549 */
550 static void
551 _mesa_copy_buffer_subdata(struct gl_context *ctx,
552 struct gl_buffer_object *src,
553 struct gl_buffer_object *dst,
554 GLintptr readOffset, GLintptr writeOffset,
555 GLsizeiptr size)
556 {
557 GLubyte *srcPtr, *dstPtr;
558
559 /* the buffers should not be mapped */
560 assert(!_mesa_bufferobj_mapped(src));
561 assert(!_mesa_bufferobj_mapped(dst));
562
563 if (src == dst) {
564 srcPtr = dstPtr = ctx->Driver.MapBufferRange(ctx, 0, src->Size,
565 GL_MAP_READ_BIT |
566 GL_MAP_WRITE_BIT, src);
567
568 if (!srcPtr)
569 return;
570
571 srcPtr += readOffset;
572 dstPtr += writeOffset;
573 } else {
574 srcPtr = ctx->Driver.MapBufferRange(ctx, readOffset, size,
575 GL_MAP_READ_BIT, src);
576 dstPtr = ctx->Driver.MapBufferRange(ctx, writeOffset, size,
577 (GL_MAP_WRITE_BIT |
578 GL_MAP_INVALIDATE_RANGE_BIT), dst);
579 }
580
581 /* Note: the src and dst regions will never overlap. Trying to do so
582 * would generate GL_INVALID_VALUE earlier.
583 */
584 if (srcPtr && dstPtr)
585 memcpy(dstPtr, srcPtr, size);
586
587 ctx->Driver.UnmapBuffer(ctx, src);
588 if (dst != src)
589 ctx->Driver.UnmapBuffer(ctx, dst);
590 }
591
592
593
594 /**
595 * Initialize the state associated with buffer objects
596 */
597 void
598 _mesa_init_buffer_objects( struct gl_context *ctx )
599 {
600 memset(&DummyBufferObject, 0, sizeof(DummyBufferObject));
601 _glthread_INIT_MUTEX(DummyBufferObject.Mutex);
602 DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
603
604 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj,
605 ctx->Shared->NullBufferObj);
606
607 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer,
608 ctx->Shared->NullBufferObj);
609 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
610 ctx->Shared->NullBufferObj);
611 }
612
613
614 void
615 _mesa_free_buffer_objects( struct gl_context *ctx )
616 {
617 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
618
619 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL);
620 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, NULL);
621 }
622
623
624 /**
625 * Bind the specified target to buffer for the specified context.
626 * Called by glBindBuffer() and other functions.
627 */
628 static void
629 bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
630 {
631 struct gl_buffer_object *oldBufObj;
632 struct gl_buffer_object *newBufObj = NULL;
633 struct gl_buffer_object **bindTarget = NULL;
634
635 bindTarget = get_buffer_target(ctx, target);
636 if (!bindTarget) {
637 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
638 return;
639 }
640
641 /* Get pointer to old buffer object (to be unbound) */
642 oldBufObj = *bindTarget;
643 if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
644 return; /* rebinding the same buffer object- no change */
645
646 /*
647 * Get pointer to new buffer object (newBufObj)
648 */
649 if (buffer == 0) {
650 /* The spec says there's not a buffer object named 0, but we use
651 * one internally because it simplifies things.
652 */
653 newBufObj = ctx->Shared->NullBufferObj;
654 }
655 else {
656 /* non-default buffer object */
657 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
658 if (!newBufObj || newBufObj == &DummyBufferObject) {
659 /* If this is a new buffer object id, or one which was generated but
660 * never used before, allocate a buffer object now.
661 */
662 ASSERT(ctx->Driver.NewBufferObject);
663 newBufObj = ctx->Driver.NewBufferObject(ctx, buffer, target);
664 if (!newBufObj) {
665 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindBufferARB");
666 return;
667 }
668 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, newBufObj);
669 }
670 }
671
672 /* bind new buffer */
673 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
674
675 /* Pass BindBuffer call to device driver */
676 if (ctx->Driver.BindBuffer)
677 ctx->Driver.BindBuffer( ctx, target, newBufObj );
678 }
679
680
681 /**
682 * Update the default buffer objects in the given context to reference those
683 * specified in the shared state and release those referencing the old
684 * shared state.
685 */
686 void
687 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
688 {
689 /* Bind the NullBufferObj to remove references to those
690 * in the shared context hash table.
691 */
692 bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
693 bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
694 bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
695 bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
696 }
697
698
699
700 /**
701 * Return the gl_buffer_object for the given ID.
702 * Always return NULL for ID 0.
703 */
704 struct gl_buffer_object *
705 _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
706 {
707 if (buffer == 0)
708 return NULL;
709 else
710 return (struct gl_buffer_object *)
711 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
712 }
713
714
715 /**
716 * If *ptr points to obj, set ptr = the Null/default buffer object.
717 * This is a helper for buffer object deletion.
718 * The GL spec says that deleting a buffer object causes it to get
719 * unbound from all arrays in the current context.
720 */
721 static void
722 unbind(struct gl_context *ctx,
723 struct gl_buffer_object **ptr,
724 struct gl_buffer_object *obj)
725 {
726 if (*ptr == obj) {
727 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
728 }
729 }
730
731
732 /**
733 * Plug default/fallback buffer object functions into the device
734 * driver hooks.
735 */
736 void
737 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
738 {
739 /* GL_ARB_vertex/pixel_buffer_object */
740 driver->NewBufferObject = _mesa_new_buffer_object;
741 driver->DeleteBuffer = _mesa_delete_buffer_object;
742 driver->BindBuffer = NULL;
743 driver->BufferData = _mesa_buffer_data;
744 driver->BufferSubData = _mesa_buffer_subdata;
745 driver->GetBufferSubData = _mesa_buffer_get_subdata;
746 driver->UnmapBuffer = _mesa_buffer_unmap;
747
748 /* GL_ARB_map_buffer_range */
749 driver->MapBufferRange = _mesa_buffer_map_range;
750 driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
751
752 /* GL_ARB_copy_buffer */
753 driver->CopyBufferSubData = _mesa_copy_buffer_subdata;
754 }
755
756
757
758 /**********************************************************************/
759 /* API Functions */
760 /**********************************************************************/
761
762 void GLAPIENTRY
763 _mesa_BindBufferARB(GLenum target, GLuint buffer)
764 {
765 GET_CURRENT_CONTEXT(ctx);
766 ASSERT_OUTSIDE_BEGIN_END(ctx);
767
768 if (MESA_VERBOSE & VERBOSE_API)
769 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
770 _mesa_lookup_enum_by_nr(target), buffer);
771
772 bind_buffer_object(ctx, target, buffer);
773 }
774
775
776 /**
777 * Delete a set of buffer objects.
778 *
779 * \param n Number of buffer objects to delete.
780 * \param ids Array of \c n buffer object IDs.
781 */
782 void GLAPIENTRY
783 _mesa_DeleteBuffersARB(GLsizei n, const GLuint *ids)
784 {
785 GET_CURRENT_CONTEXT(ctx);
786 GLsizei i;
787 ASSERT_OUTSIDE_BEGIN_END(ctx);
788 FLUSH_VERTICES(ctx, 0);
789
790 if (n < 0) {
791 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
792 return;
793 }
794
795 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
796
797 for (i = 0; i < n; i++) {
798 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
799 if (bufObj) {
800 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
801 GLuint j;
802
803 ASSERT(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
804
805 if (_mesa_bufferobj_mapped(bufObj)) {
806 /* if mapped, unmap it now */
807 ctx->Driver.UnmapBuffer(ctx, bufObj);
808 bufObj->AccessFlags = default_access_mode(ctx);
809 bufObj->Pointer = NULL;
810 }
811
812 /* unbind any vertex pointers bound to this buffer */
813 for (j = 0; j < Elements(arrayObj->VertexAttrib); j++) {
814 unbind(ctx, &arrayObj->VertexAttrib[j].BufferObj, bufObj);
815 }
816
817 if (ctx->Array.ArrayBufferObj == bufObj) {
818 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
819 }
820 if (arrayObj->ElementArrayBufferObj == bufObj) {
821 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
822 }
823
824 /* unbind ARB_copy_buffer binding points */
825 if (ctx->CopyReadBuffer == bufObj) {
826 _mesa_BindBufferARB( GL_COPY_READ_BUFFER, 0 );
827 }
828 if (ctx->CopyWriteBuffer == bufObj) {
829 _mesa_BindBufferARB( GL_COPY_WRITE_BUFFER, 0 );
830 }
831
832 /* unbind transform feedback binding point */
833 if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
834 _mesa_BindBufferARB( GL_TRANSFORM_FEEDBACK_BUFFER, 0 );
835 }
836
837 /* unbind any pixel pack/unpack pointers bound to this buffer */
838 if (ctx->Pack.BufferObj == bufObj) {
839 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 );
840 }
841 if (ctx->Unpack.BufferObj == bufObj) {
842 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
843 }
844
845 /* The ID is immediately freed for re-use */
846 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
847 /* Make sure we do not run into the classic ABA problem on bind.
848 * We don't want to allow re-binding a buffer object that's been
849 * "deleted" by glDeleteBuffers().
850 *
851 * The explicit rebinding to the default object in the current context
852 * prevents the above in the current context, but another context
853 * sharing the same objects might suffer from this problem.
854 * The alternative would be to do the hash lookup in any case on bind
855 * which would introduce more runtime overhead than this.
856 */
857 bufObj->DeletePending = GL_TRUE;
858 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
859 }
860 }
861
862 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
863 }
864
865
866 /**
867 * Generate a set of unique buffer object IDs and store them in \c buffer.
868 *
869 * \param n Number of IDs to generate.
870 * \param buffer Array of \c n locations to store the IDs.
871 */
872 void GLAPIENTRY
873 _mesa_GenBuffersARB(GLsizei n, GLuint *buffer)
874 {
875 GET_CURRENT_CONTEXT(ctx);
876 GLuint first;
877 GLint i;
878 ASSERT_OUTSIDE_BEGIN_END(ctx);
879
880 if (MESA_VERBOSE & VERBOSE_API)
881 _mesa_debug(ctx, "glGenBuffers(%d)\n", n);
882
883 if (n < 0) {
884 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
885 return;
886 }
887
888 if (!buffer) {
889 return;
890 }
891
892 /*
893 * This must be atomic (generation and allocation of buffer object IDs)
894 */
895 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
896
897 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
898
899 /* Insert the ID and pointer to dummy buffer object into hash table */
900 for (i = 0; i < n; i++) {
901 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i,
902 &DummyBufferObject);
903 buffer[i] = first + i;
904 }
905
906 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
907 }
908
909
910 /**
911 * Determine if ID is the name of a buffer object.
912 *
913 * \param id ID of the potential buffer object.
914 * \return \c GL_TRUE if \c id is the name of a buffer object,
915 * \c GL_FALSE otherwise.
916 */
917 GLboolean GLAPIENTRY
918 _mesa_IsBufferARB(GLuint id)
919 {
920 struct gl_buffer_object *bufObj;
921 GET_CURRENT_CONTEXT(ctx);
922 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
923
924 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
925 bufObj = _mesa_lookup_bufferobj(ctx, id);
926 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
927
928 return bufObj && bufObj != &DummyBufferObject;
929 }
930
931
932 void GLAPIENTRY
933 _mesa_BufferDataARB(GLenum target, GLsizeiptrARB size,
934 const GLvoid * data, GLenum usage)
935 {
936 GET_CURRENT_CONTEXT(ctx);
937 struct gl_buffer_object *bufObj;
938 ASSERT_OUTSIDE_BEGIN_END(ctx);
939
940 if (MESA_VERBOSE & VERBOSE_API)
941 _mesa_debug(ctx, "glBufferData(%s, %ld, %p, %s)\n",
942 _mesa_lookup_enum_by_nr(target),
943 (long int) size, data,
944 _mesa_lookup_enum_by_nr(usage));
945
946 if (size < 0) {
947 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
948 return;
949 }
950
951 switch (usage) {
952 case GL_STREAM_DRAW_ARB:
953 case GL_STREAM_READ_ARB:
954 case GL_STREAM_COPY_ARB:
955 case GL_STATIC_DRAW_ARB:
956 case GL_STATIC_READ_ARB:
957 case GL_STATIC_COPY_ARB:
958 case GL_DYNAMIC_DRAW_ARB:
959 case GL_DYNAMIC_READ_ARB:
960 case GL_DYNAMIC_COPY_ARB:
961 /* OK */
962 break;
963 default:
964 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(usage)");
965 return;
966 }
967
968 bufObj = get_buffer(ctx, "glBufferDataARB", target);
969 if (!bufObj)
970 return;
971
972 if (_mesa_bufferobj_mapped(bufObj)) {
973 /* Unmap the existing buffer. We'll replace it now. Not an error. */
974 ctx->Driver.UnmapBuffer(ctx, bufObj);
975 bufObj->AccessFlags = default_access_mode(ctx);
976 ASSERT(bufObj->Pointer == NULL);
977 }
978
979 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
980
981 bufObj->Written = GL_TRUE;
982
983 #ifdef VBO_DEBUG
984 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
985 bufObj->Name, size, data, usage);
986 #endif
987
988 #ifdef BOUNDS_CHECK
989 size += 100;
990 #endif
991
992 ASSERT(ctx->Driver.BufferData);
993 if (!ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj )) {
994 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB()");
995 }
996 }
997
998
999 void GLAPIENTRY
1000 _mesa_BufferSubDataARB(GLenum target, GLintptrARB offset,
1001 GLsizeiptrARB size, const GLvoid * data)
1002 {
1003 GET_CURRENT_CONTEXT(ctx);
1004 struct gl_buffer_object *bufObj;
1005 ASSERT_OUTSIDE_BEGIN_END(ctx);
1006
1007 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1008 "glBufferSubDataARB" );
1009 if (!bufObj) {
1010 /* error already recorded */
1011 return;
1012 }
1013
1014 if (size == 0)
1015 return;
1016
1017 bufObj->Written = GL_TRUE;
1018
1019 ASSERT(ctx->Driver.BufferSubData);
1020 ctx->Driver.BufferSubData( ctx, offset, size, data, bufObj );
1021 }
1022
1023
1024 void GLAPIENTRY
1025 _mesa_GetBufferSubDataARB(GLenum target, GLintptrARB offset,
1026 GLsizeiptrARB size, void * data)
1027 {
1028 GET_CURRENT_CONTEXT(ctx);
1029 struct gl_buffer_object *bufObj;
1030 ASSERT_OUTSIDE_BEGIN_END(ctx);
1031
1032 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1033 "glGetBufferSubDataARB" );
1034 if (!bufObj) {
1035 /* error already recorded */
1036 return;
1037 }
1038
1039 ASSERT(ctx->Driver.GetBufferSubData);
1040 ctx->Driver.GetBufferSubData( ctx, offset, size, data, bufObj );
1041 }
1042
1043
1044 void * GLAPIENTRY
1045 _mesa_MapBufferARB(GLenum target, GLenum access)
1046 {
1047 GET_CURRENT_CONTEXT(ctx);
1048 struct gl_buffer_object * bufObj;
1049 GLbitfield accessFlags;
1050 void *map;
1051
1052 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1053
1054 switch (access) {
1055 case GL_READ_ONLY_ARB:
1056 accessFlags = GL_MAP_READ_BIT;
1057 break;
1058 case GL_WRITE_ONLY_ARB:
1059 accessFlags = GL_MAP_WRITE_BIT;
1060 break;
1061 case GL_READ_WRITE_ARB:
1062 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1063 break;
1064 default:
1065 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
1066 return NULL;
1067 }
1068
1069 bufObj = get_buffer(ctx, "glMapBufferARB", target);
1070 if (!bufObj)
1071 return NULL;
1072
1073 if (_mesa_bufferobj_mapped(bufObj)) {
1074 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
1075 return NULL;
1076 }
1077
1078 if (!bufObj->Size) {
1079 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1080 "glMapBuffer(buffer size = 0)");
1081 return NULL;
1082 }
1083
1084 ASSERT(ctx->Driver.MapBufferRange);
1085 map = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size, accessFlags, bufObj);
1086 if (!map) {
1087 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1088 return NULL;
1089 }
1090 else {
1091 /* The driver callback should have set these fields.
1092 * This is important because other modules (like VBO) might call
1093 * the driver function directly.
1094 */
1095 ASSERT(bufObj->Pointer == map);
1096 ASSERT(bufObj->Length == bufObj->Size);
1097 ASSERT(bufObj->Offset == 0);
1098 bufObj->AccessFlags = accessFlags;
1099 }
1100
1101 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
1102 bufObj->Written = GL_TRUE;
1103
1104 #ifdef VBO_DEBUG
1105 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1106 bufObj->Name, bufObj->Size, access);
1107 if (access == GL_WRITE_ONLY_ARB) {
1108 GLuint i;
1109 GLubyte *b = (GLubyte *) bufObj->Pointer;
1110 for (i = 0; i < bufObj->Size; i++)
1111 b[i] = i & 0xff;
1112 }
1113 #endif
1114
1115 #ifdef BOUNDS_CHECK
1116 {
1117 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1118 GLuint i;
1119 /* buffer is 100 bytes larger than requested, fill with magic value */
1120 for (i = 0; i < 100; i++) {
1121 buf[bufObj->Size - i - 1] = 123;
1122 }
1123 }
1124 #endif
1125
1126 return bufObj->Pointer;
1127 }
1128
1129
1130 GLboolean GLAPIENTRY
1131 _mesa_UnmapBufferARB(GLenum target)
1132 {
1133 GET_CURRENT_CONTEXT(ctx);
1134 struct gl_buffer_object *bufObj;
1135 GLboolean status = GL_TRUE;
1136 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1137
1138 bufObj = get_buffer(ctx, "glUnmapBufferARB", target);
1139 if (!bufObj)
1140 return GL_FALSE;
1141
1142 if (!_mesa_bufferobj_mapped(bufObj)) {
1143 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1144 return GL_FALSE;
1145 }
1146
1147 #ifdef BOUNDS_CHECK
1148 if (bufObj->Access != GL_READ_ONLY_ARB) {
1149 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1150 GLuint i;
1151 /* check that last 100 bytes are still = magic value */
1152 for (i = 0; i < 100; i++) {
1153 GLuint pos = bufObj->Size - i - 1;
1154 if (buf[pos] != 123) {
1155 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1156 " at position %d (value = %u)\n",
1157 pos, buf[pos]);
1158 }
1159 }
1160 }
1161 #endif
1162
1163 #ifdef VBO_DEBUG
1164 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1165 GLuint i, unchanged = 0;
1166 GLubyte *b = (GLubyte *) bufObj->Pointer;
1167 GLint pos = -1;
1168 /* check which bytes changed */
1169 for (i = 0; i < bufObj->Size - 1; i++) {
1170 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1171 unchanged++;
1172 if (pos == -1)
1173 pos = i;
1174 }
1175 }
1176 if (unchanged) {
1177 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1178 bufObj->Name, unchanged, bufObj->Size, pos);
1179 }
1180 }
1181 #endif
1182
1183 status = ctx->Driver.UnmapBuffer( ctx, bufObj );
1184 bufObj->AccessFlags = default_access_mode(ctx);
1185 ASSERT(bufObj->Pointer == NULL);
1186 ASSERT(bufObj->Offset == 0);
1187 ASSERT(bufObj->Length == 0);
1188
1189 return status;
1190 }
1191
1192
1193 void GLAPIENTRY
1194 _mesa_GetBufferParameterivARB(GLenum target, GLenum pname, GLint *params)
1195 {
1196 GET_CURRENT_CONTEXT(ctx);
1197 struct gl_buffer_object *bufObj;
1198 ASSERT_OUTSIDE_BEGIN_END(ctx);
1199
1200 bufObj = get_buffer(ctx, "glGetBufferParameterivARB", target);
1201 if (!bufObj)
1202 return;
1203
1204 switch (pname) {
1205 case GL_BUFFER_SIZE_ARB:
1206 *params = (GLint) bufObj->Size;
1207 return;
1208 case GL_BUFFER_USAGE_ARB:
1209 *params = bufObj->Usage;
1210 return;
1211 case GL_BUFFER_ACCESS_ARB:
1212 *params = simplified_access_mode(bufObj->AccessFlags);
1213 return;
1214 case GL_BUFFER_MAPPED_ARB:
1215 *params = _mesa_bufferobj_mapped(bufObj);
1216 return;
1217 case GL_BUFFER_ACCESS_FLAGS:
1218 if (!ctx->Extensions.ARB_map_buffer_range)
1219 goto invalid_pname;
1220 *params = bufObj->AccessFlags;
1221 return;
1222 case GL_BUFFER_MAP_OFFSET:
1223 if (!ctx->Extensions.ARB_map_buffer_range)
1224 goto invalid_pname;
1225 *params = (GLint) bufObj->Offset;
1226 return;
1227 case GL_BUFFER_MAP_LENGTH:
1228 if (!ctx->Extensions.ARB_map_buffer_range)
1229 goto invalid_pname;
1230 *params = (GLint) bufObj->Length;
1231 return;
1232 default:
1233 ; /* fall-through */
1234 }
1235
1236 invalid_pname:
1237 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname=%s)",
1238 _mesa_lookup_enum_by_nr(pname));
1239 }
1240
1241
1242 /**
1243 * New in GL 3.2
1244 * This is pretty much a duplicate of GetBufferParameteriv() but the
1245 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1246 */
1247 void GLAPIENTRY
1248 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
1249 {
1250 GET_CURRENT_CONTEXT(ctx);
1251 struct gl_buffer_object *bufObj;
1252 ASSERT_OUTSIDE_BEGIN_END(ctx);
1253
1254 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target);
1255 if (!bufObj)
1256 return;
1257
1258 switch (pname) {
1259 case GL_BUFFER_SIZE_ARB:
1260 *params = bufObj->Size;
1261 return;
1262 case GL_BUFFER_USAGE_ARB:
1263 *params = bufObj->Usage;
1264 return;
1265 case GL_BUFFER_ACCESS_ARB:
1266 *params = simplified_access_mode(bufObj->AccessFlags);
1267 return;
1268 case GL_BUFFER_ACCESS_FLAGS:
1269 if (!ctx->Extensions.ARB_map_buffer_range)
1270 goto invalid_pname;
1271 *params = bufObj->AccessFlags;
1272 return;
1273 case GL_BUFFER_MAPPED_ARB:
1274 *params = _mesa_bufferobj_mapped(bufObj);
1275 return;
1276 case GL_BUFFER_MAP_OFFSET:
1277 if (!ctx->Extensions.ARB_map_buffer_range)
1278 goto invalid_pname;
1279 *params = bufObj->Offset;
1280 return;
1281 case GL_BUFFER_MAP_LENGTH:
1282 if (!ctx->Extensions.ARB_map_buffer_range)
1283 goto invalid_pname;
1284 *params = bufObj->Length;
1285 return;
1286 default:
1287 ; /* fall-through */
1288 }
1289
1290 invalid_pname:
1291 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
1292 _mesa_lookup_enum_by_nr(pname));
1293 }
1294
1295
1296 void GLAPIENTRY
1297 _mesa_GetBufferPointervARB(GLenum target, GLenum pname, GLvoid **params)
1298 {
1299 GET_CURRENT_CONTEXT(ctx);
1300 struct gl_buffer_object * bufObj;
1301 ASSERT_OUTSIDE_BEGIN_END(ctx);
1302
1303 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1304 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1305 return;
1306 }
1307
1308 bufObj = get_buffer(ctx, "glGetBufferPointervARB", target);
1309 if (!bufObj)
1310 return;
1311
1312 *params = bufObj->Pointer;
1313 }
1314
1315
1316 void GLAPIENTRY
1317 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
1318 GLintptr readOffset, GLintptr writeOffset,
1319 GLsizeiptr size)
1320 {
1321 GET_CURRENT_CONTEXT(ctx);
1322 struct gl_buffer_object *src, *dst;
1323 ASSERT_OUTSIDE_BEGIN_END(ctx);
1324
1325 src = get_buffer(ctx, "glCopyBuffserSubData", readTarget);
1326 if (!src)
1327 return;
1328
1329 dst = get_buffer(ctx, "glCopyBuffserSubData", writeTarget);
1330 if (!dst)
1331 return;
1332
1333 if (_mesa_bufferobj_mapped(src)) {
1334 _mesa_error(ctx, GL_INVALID_OPERATION,
1335 "glCopyBuffserSubData(readBuffer is mapped)");
1336 return;
1337 }
1338
1339 if (_mesa_bufferobj_mapped(dst)) {
1340 _mesa_error(ctx, GL_INVALID_OPERATION,
1341 "glCopyBuffserSubData(writeBuffer is mapped)");
1342 return;
1343 }
1344
1345 if (readOffset < 0) {
1346 _mesa_error(ctx, GL_INVALID_VALUE,
1347 "glCopyBuffserSubData(readOffset = %d)", (int) readOffset);
1348 return;
1349 }
1350
1351 if (writeOffset < 0) {
1352 _mesa_error(ctx, GL_INVALID_VALUE,
1353 "glCopyBuffserSubData(writeOffset = %d)", (int) writeOffset);
1354 return;
1355 }
1356
1357 if (readOffset + size > src->Size) {
1358 _mesa_error(ctx, GL_INVALID_VALUE,
1359 "glCopyBuffserSubData(readOffset + size = %d)",
1360 (int) (readOffset + size));
1361 return;
1362 }
1363
1364 if (writeOffset + size > dst->Size) {
1365 _mesa_error(ctx, GL_INVALID_VALUE,
1366 "glCopyBuffserSubData(writeOffset + size = %d)",
1367 (int) (writeOffset + size));
1368 return;
1369 }
1370
1371 if (src == dst) {
1372 if (readOffset + size <= writeOffset) {
1373 /* OK */
1374 }
1375 else if (writeOffset + size <= readOffset) {
1376 /* OK */
1377 }
1378 else {
1379 /* overlapping src/dst is illegal */
1380 _mesa_error(ctx, GL_INVALID_VALUE,
1381 "glCopyBuffserSubData(overlapping src/dst)");
1382 return;
1383 }
1384 }
1385
1386 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
1387 }
1388
1389
1390 /**
1391 * See GL_ARB_map_buffer_range spec
1392 */
1393 void * GLAPIENTRY
1394 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
1395 GLbitfield access)
1396 {
1397 GET_CURRENT_CONTEXT(ctx);
1398 struct gl_buffer_object *bufObj;
1399 void *map;
1400
1401 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1402
1403 if (!ctx->Extensions.ARB_map_buffer_range) {
1404 _mesa_error(ctx, GL_INVALID_OPERATION,
1405 "glMapBufferRange(extension not supported)");
1406 return NULL;
1407 }
1408
1409 if (offset < 0) {
1410 _mesa_error(ctx, GL_INVALID_VALUE,
1411 "glMapBufferRange(offset = %ld)", (long)offset);
1412 return NULL;
1413 }
1414
1415 if (length < 0) {
1416 _mesa_error(ctx, GL_INVALID_VALUE,
1417 "glMapBufferRange(length = %ld)", (long)length);
1418 return NULL;
1419 }
1420
1421 if (access & ~(GL_MAP_READ_BIT |
1422 GL_MAP_WRITE_BIT |
1423 GL_MAP_INVALIDATE_RANGE_BIT |
1424 GL_MAP_INVALIDATE_BUFFER_BIT |
1425 GL_MAP_FLUSH_EXPLICIT_BIT |
1426 GL_MAP_UNSYNCHRONIZED_BIT)) {
1427 /* generate an error if any undefind bit is set */
1428 _mesa_error(ctx, GL_INVALID_VALUE, "glMapBufferRange(access)");
1429 return NULL;
1430 }
1431
1432 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
1433 _mesa_error(ctx, GL_INVALID_OPERATION,
1434 "glMapBufferRange(access indicates neither read or write)");
1435 return NULL;
1436 }
1437
1438 if ((access & GL_MAP_READ_BIT) &&
1439 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
1440 GL_MAP_INVALIDATE_BUFFER_BIT |
1441 GL_MAP_UNSYNCHRONIZED_BIT))) {
1442 _mesa_error(ctx, GL_INVALID_OPERATION,
1443 "glMapBufferRange(invalid access flags)");
1444 return NULL;
1445 }
1446
1447 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
1448 ((access & GL_MAP_WRITE_BIT) == 0)) {
1449 _mesa_error(ctx, GL_INVALID_OPERATION,
1450 "glMapBufferRange(invalid access flags)");
1451 return NULL;
1452 }
1453
1454 bufObj = get_buffer(ctx, "glMapBufferRange", target);
1455 if (!bufObj)
1456 return NULL;
1457
1458 if (offset + length > bufObj->Size) {
1459 _mesa_error(ctx, GL_INVALID_VALUE,
1460 "glMapBufferRange(offset + length > size)");
1461 return NULL;
1462 }
1463
1464 if (_mesa_bufferobj_mapped(bufObj)) {
1465 _mesa_error(ctx, GL_INVALID_OPERATION,
1466 "glMapBufferRange(buffer already mapped)");
1467 return NULL;
1468 }
1469
1470 if (!bufObj->Size) {
1471 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1472 "glMapBufferRange(buffer size = 0)");
1473 return NULL;
1474 }
1475
1476 /* Mapping zero bytes should return a non-null pointer. */
1477 if (!length) {
1478 static long dummy = 0;
1479 bufObj->Pointer = &dummy;
1480 bufObj->Length = length;
1481 bufObj->Offset = offset;
1482 bufObj->AccessFlags = access;
1483 return bufObj->Pointer;
1484 }
1485
1486 ASSERT(ctx->Driver.MapBufferRange);
1487 map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj);
1488 if (!map) {
1489 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1490 }
1491 else {
1492 /* The driver callback should have set all these fields.
1493 * This is important because other modules (like VBO) might call
1494 * the driver function directly.
1495 */
1496 ASSERT(bufObj->Pointer == map);
1497 ASSERT(bufObj->Length == length);
1498 ASSERT(bufObj->Offset == offset);
1499 ASSERT(bufObj->AccessFlags == access);
1500 }
1501
1502 return map;
1503 }
1504
1505
1506 /**
1507 * See GL_ARB_map_buffer_range spec
1508 */
1509 void GLAPIENTRY
1510 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
1511 {
1512 GET_CURRENT_CONTEXT(ctx);
1513 struct gl_buffer_object *bufObj;
1514 ASSERT_OUTSIDE_BEGIN_END(ctx);
1515
1516 if (!ctx->Extensions.ARB_map_buffer_range) {
1517 _mesa_error(ctx, GL_INVALID_OPERATION,
1518 "glFlushMappedBufferRange(extension not supported)");
1519 return;
1520 }
1521
1522 if (offset < 0) {
1523 _mesa_error(ctx, GL_INVALID_VALUE,
1524 "glFlushMappedBufferRange(offset = %ld)", (long)offset);
1525 return;
1526 }
1527
1528 if (length < 0) {
1529 _mesa_error(ctx, GL_INVALID_VALUE,
1530 "glFlushMappedBufferRange(length = %ld)", (long)length);
1531 return;
1532 }
1533
1534 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target);
1535 if (!bufObj)
1536 return;
1537
1538 if (!_mesa_bufferobj_mapped(bufObj)) {
1539 /* buffer is not mapped */
1540 _mesa_error(ctx, GL_INVALID_OPERATION,
1541 "glFlushMappedBufferRange(buffer is not mapped)");
1542 return;
1543 }
1544
1545 if ((bufObj->AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
1546 _mesa_error(ctx, GL_INVALID_OPERATION,
1547 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1548 return;
1549 }
1550
1551 if (offset + length > bufObj->Length) {
1552 _mesa_error(ctx, GL_INVALID_VALUE,
1553 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1554 (long)offset, (long)length, (long)bufObj->Length);
1555 return;
1556 }
1557
1558 ASSERT(bufObj->AccessFlags & GL_MAP_WRITE_BIT);
1559
1560 if (ctx->Driver.FlushMappedBufferRange)
1561 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj);
1562 }
1563
1564
1565 #if FEATURE_APPLE_object_purgeable
1566 static GLenum
1567 buffer_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
1568 {
1569 struct gl_buffer_object *bufObj;
1570 GLenum retval;
1571
1572 bufObj = _mesa_lookup_bufferobj(ctx, name);
1573 if (!bufObj) {
1574 _mesa_error(ctx, GL_INVALID_VALUE,
1575 "glObjectPurgeable(name = 0x%x)", name);
1576 return 0;
1577 }
1578 if (!_mesa_is_bufferobj(bufObj)) {
1579 _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
1580 return 0;
1581 }
1582
1583 if (bufObj->Purgeable) {
1584 _mesa_error(ctx, GL_INVALID_OPERATION,
1585 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1586 return GL_VOLATILE_APPLE;
1587 }
1588
1589 bufObj->Purgeable = GL_TRUE;
1590
1591 retval = GL_VOLATILE_APPLE;
1592 if (ctx->Driver.BufferObjectPurgeable)
1593 retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
1594
1595 return retval;
1596 }
1597
1598
1599 static GLenum
1600 renderbuffer_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
1601 {
1602 struct gl_renderbuffer *bufObj;
1603 GLenum retval;
1604
1605 bufObj = _mesa_lookup_renderbuffer(ctx, name);
1606 if (!bufObj) {
1607 _mesa_error(ctx, GL_INVALID_VALUE,
1608 "glObjectUnpurgeable(name = 0x%x)", name);
1609 return 0;
1610 }
1611
1612 if (bufObj->Purgeable) {
1613 _mesa_error(ctx, GL_INVALID_OPERATION,
1614 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1615 return GL_VOLATILE_APPLE;
1616 }
1617
1618 bufObj->Purgeable = GL_TRUE;
1619
1620 retval = GL_VOLATILE_APPLE;
1621 if (ctx->Driver.RenderObjectPurgeable)
1622 retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
1623
1624 return retval;
1625 }
1626
1627
1628 static GLenum
1629 texture_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
1630 {
1631 struct gl_texture_object *bufObj;
1632 GLenum retval;
1633
1634 bufObj = _mesa_lookup_texture(ctx, name);
1635 if (!bufObj) {
1636 _mesa_error(ctx, GL_INVALID_VALUE,
1637 "glObjectPurgeable(name = 0x%x)", name);
1638 return 0;
1639 }
1640
1641 if (bufObj->Purgeable) {
1642 _mesa_error(ctx, GL_INVALID_OPERATION,
1643 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1644 return GL_VOLATILE_APPLE;
1645 }
1646
1647 bufObj->Purgeable = GL_TRUE;
1648
1649 retval = GL_VOLATILE_APPLE;
1650 if (ctx->Driver.TextureObjectPurgeable)
1651 retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
1652
1653 return retval;
1654 }
1655
1656
1657 GLenum GLAPIENTRY
1658 _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
1659 {
1660 GLenum retval;
1661
1662 GET_CURRENT_CONTEXT(ctx);
1663 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1664
1665 if (name == 0) {
1666 _mesa_error(ctx, GL_INVALID_VALUE,
1667 "glObjectPurgeable(name = 0x%x)", name);
1668 return 0;
1669 }
1670
1671 switch (option) {
1672 case GL_VOLATILE_APPLE:
1673 case GL_RELEASED_APPLE:
1674 /* legal */
1675 break;
1676 default:
1677 _mesa_error(ctx, GL_INVALID_ENUM,
1678 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1679 name, option);
1680 return 0;
1681 }
1682
1683 switch (objectType) {
1684 case GL_TEXTURE:
1685 retval = texture_object_purgeable(ctx, name, option);
1686 break;
1687 case GL_RENDERBUFFER_EXT:
1688 retval = renderbuffer_purgeable(ctx, name, option);
1689 break;
1690 case GL_BUFFER_OBJECT_APPLE:
1691 retval = buffer_object_purgeable(ctx, name, option);
1692 break;
1693 default:
1694 _mesa_error(ctx, GL_INVALID_ENUM,
1695 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1696 name, objectType);
1697 return 0;
1698 }
1699
1700 /* In strict conformance to the spec, we must only return VOLATILE when
1701 * when passed the VOLATILE option. Madness.
1702 *
1703 * XXX First fix the spec, then fix me.
1704 */
1705 return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
1706 }
1707
1708
1709 static GLenum
1710 buffer_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
1711 {
1712 struct gl_buffer_object *bufObj;
1713 GLenum retval;
1714
1715 bufObj = _mesa_lookup_bufferobj(ctx, name);
1716 if (!bufObj) {
1717 _mesa_error(ctx, GL_INVALID_VALUE,
1718 "glObjectUnpurgeable(name = 0x%x)", name);
1719 return 0;
1720 }
1721
1722 if (! bufObj->Purgeable) {
1723 _mesa_error(ctx, GL_INVALID_OPERATION,
1724 "glObjectUnpurgeable(name = 0x%x) object is "
1725 " already \"unpurged\"", name);
1726 return 0;
1727 }
1728
1729 bufObj->Purgeable = GL_FALSE;
1730
1731 retval = option;
1732 if (ctx->Driver.BufferObjectUnpurgeable)
1733 retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
1734
1735 return retval;
1736 }
1737
1738
1739 static GLenum
1740 renderbuffer_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
1741 {
1742 struct gl_renderbuffer *bufObj;
1743 GLenum retval;
1744
1745 bufObj = _mesa_lookup_renderbuffer(ctx, name);
1746 if (!bufObj) {
1747 _mesa_error(ctx, GL_INVALID_VALUE,
1748 "glObjectUnpurgeable(name = 0x%x)", name);
1749 return 0;
1750 }
1751
1752 if (! bufObj->Purgeable) {
1753 _mesa_error(ctx, GL_INVALID_OPERATION,
1754 "glObjectUnpurgeable(name = 0x%x) object is "
1755 " already \"unpurged\"", name);
1756 return 0;
1757 }
1758
1759 bufObj->Purgeable = GL_FALSE;
1760
1761 retval = option;
1762 if (ctx->Driver.RenderObjectUnpurgeable)
1763 retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
1764
1765 return retval;
1766 }
1767
1768
1769 static GLenum
1770 texture_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
1771 {
1772 struct gl_texture_object *bufObj;
1773 GLenum retval;
1774
1775 bufObj = _mesa_lookup_texture(ctx, name);
1776 if (!bufObj) {
1777 _mesa_error(ctx, GL_INVALID_VALUE,
1778 "glObjectUnpurgeable(name = 0x%x)", name);
1779 return 0;
1780 }
1781
1782 if (! bufObj->Purgeable) {
1783 _mesa_error(ctx, GL_INVALID_OPERATION,
1784 "glObjectUnpurgeable(name = 0x%x) object is"
1785 " already \"unpurged\"", name);
1786 return 0;
1787 }
1788
1789 bufObj->Purgeable = GL_FALSE;
1790
1791 retval = option;
1792 if (ctx->Driver.TextureObjectUnpurgeable)
1793 retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
1794
1795 return retval;
1796 }
1797
1798
1799 GLenum GLAPIENTRY
1800 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
1801 {
1802 GET_CURRENT_CONTEXT(ctx);
1803 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1804
1805 if (name == 0) {
1806 _mesa_error(ctx, GL_INVALID_VALUE,
1807 "glObjectUnpurgeable(name = 0x%x)", name);
1808 return 0;
1809 }
1810
1811 switch (option) {
1812 case GL_RETAINED_APPLE:
1813 case GL_UNDEFINED_APPLE:
1814 /* legal */
1815 break;
1816 default:
1817 _mesa_error(ctx, GL_INVALID_ENUM,
1818 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1819 name, option);
1820 return 0;
1821 }
1822
1823 switch (objectType) {
1824 case GL_BUFFER_OBJECT_APPLE:
1825 return buffer_object_unpurgeable(ctx, name, option);
1826 case GL_TEXTURE:
1827 return texture_object_unpurgeable(ctx, name, option);
1828 case GL_RENDERBUFFER_EXT:
1829 return renderbuffer_unpurgeable(ctx, name, option);
1830 default:
1831 _mesa_error(ctx, GL_INVALID_ENUM,
1832 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1833 name, objectType);
1834 return 0;
1835 }
1836 }
1837
1838
1839 static void
1840 get_buffer_object_parameteriv(struct gl_context *ctx, GLuint name,
1841 GLenum pname, GLint *params)
1842 {
1843 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
1844 if (!bufObj) {
1845 _mesa_error(ctx, GL_INVALID_VALUE,
1846 "glGetObjectParameteriv(name = 0x%x) invalid object", name);
1847 return;
1848 }
1849
1850 switch (pname) {
1851 case GL_PURGEABLE_APPLE:
1852 *params = bufObj->Purgeable;
1853 break;
1854 default:
1855 _mesa_error(ctx, GL_INVALID_ENUM,
1856 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1857 name, pname);
1858 break;
1859 }
1860 }
1861
1862
1863 static void
1864 get_renderbuffer_parameteriv(struct gl_context *ctx, GLuint name,
1865 GLenum pname, GLint *params)
1866 {
1867 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
1868 if (!rb) {
1869 _mesa_error(ctx, GL_INVALID_VALUE,
1870 "glObjectUnpurgeable(name = 0x%x)", name);
1871 return;
1872 }
1873
1874 switch (pname) {
1875 case GL_PURGEABLE_APPLE:
1876 *params = rb->Purgeable;
1877 break;
1878 default:
1879 _mesa_error(ctx, GL_INVALID_ENUM,
1880 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1881 name, pname);
1882 break;
1883 }
1884 }
1885
1886
1887 static void
1888 get_texture_object_parameteriv(struct gl_context *ctx, GLuint name,
1889 GLenum pname, GLint *params)
1890 {
1891 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
1892 if (!texObj) {
1893 _mesa_error(ctx, GL_INVALID_VALUE,
1894 "glObjectUnpurgeable(name = 0x%x)", name);
1895 return;
1896 }
1897
1898 switch (pname) {
1899 case GL_PURGEABLE_APPLE:
1900 *params = texObj->Purgeable;
1901 break;
1902 default:
1903 _mesa_error(ctx, GL_INVALID_ENUM,
1904 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1905 name, pname);
1906 break;
1907 }
1908 }
1909
1910
1911 void GLAPIENTRY
1912 _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
1913 GLint *params)
1914 {
1915 GET_CURRENT_CONTEXT(ctx);
1916
1917 if (name == 0) {
1918 _mesa_error(ctx, GL_INVALID_VALUE,
1919 "glGetObjectParameteriv(name = 0x%x)", name);
1920 return;
1921 }
1922
1923 switch (objectType) {
1924 case GL_TEXTURE:
1925 get_texture_object_parameteriv(ctx, name, pname, params);
1926 break;
1927 case GL_BUFFER_OBJECT_APPLE:
1928 get_buffer_object_parameteriv(ctx, name, pname, params);
1929 break;
1930 case GL_RENDERBUFFER_EXT:
1931 get_renderbuffer_parameteriv(ctx, name, pname, params);
1932 break;
1933 default:
1934 _mesa_error(ctx, GL_INVALID_ENUM,
1935 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1936 name, objectType);
1937 }
1938 }
1939
1940 #endif /* FEATURE_APPLE_object_purgeable */