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