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