23e19f10a7f3cebab46b26988dc9a620350607a4
[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(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.ArrayObj->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
535 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer,
536 ctx->Shared->NullBufferObj);
537 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
538 ctx->Shared->NullBufferObj);
539 }
540
541
542 void
543 _mesa_free_buffer_objects( struct gl_context *ctx )
544 {
545 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
546
547 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL);
548 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, NULL);
549 }
550
551
552 /**
553 * Bind the specified target to buffer for the specified context.
554 * Called by glBindBuffer() and other functions.
555 */
556 static void
557 bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
558 {
559 struct gl_buffer_object *oldBufObj;
560 struct gl_buffer_object *newBufObj = NULL;
561 struct gl_buffer_object **bindTarget = NULL;
562
563 bindTarget = get_buffer_target(ctx, target);
564 if (!bindTarget) {
565 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
566 return;
567 }
568
569 /* Get pointer to old buffer object (to be unbound) */
570 oldBufObj = *bindTarget;
571 if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
572 return; /* rebinding the same buffer object- no change */
573
574 /*
575 * Get pointer to new buffer object (newBufObj)
576 */
577 if (buffer == 0) {
578 /* The spec says there's not a buffer object named 0, but we use
579 * one internally because it simplifies things.
580 */
581 newBufObj = ctx->Shared->NullBufferObj;
582 }
583 else {
584 /* non-default buffer object */
585 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
586 if (!newBufObj || newBufObj == &DummyBufferObject) {
587 /* If this is a new buffer object id, or one which was generated but
588 * never used before, allocate a buffer object now.
589 */
590 ASSERT(ctx->Driver.NewBufferObject);
591 newBufObj = ctx->Driver.NewBufferObject(ctx, buffer, target);
592 if (!newBufObj) {
593 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindBufferARB");
594 return;
595 }
596 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, newBufObj);
597 }
598 }
599
600 /* bind new buffer */
601 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
602
603 /* Pass BindBuffer call to device driver */
604 if (ctx->Driver.BindBuffer)
605 ctx->Driver.BindBuffer( ctx, target, newBufObj );
606 }
607
608
609 /**
610 * Update the default buffer objects in the given context to reference those
611 * specified in the shared state and release those referencing the old
612 * shared state.
613 */
614 void
615 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
616 {
617 /* Bind the NullBufferObj to remove references to those
618 * in the shared context hash table.
619 */
620 bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
621 bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
622 bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
623 bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
624 }
625
626
627
628 /**
629 * Return the gl_buffer_object for the given ID.
630 * Always return NULL for ID 0.
631 */
632 struct gl_buffer_object *
633 _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
634 {
635 if (buffer == 0)
636 return NULL;
637 else
638 return (struct gl_buffer_object *)
639 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
640 }
641
642
643 /**
644 * If *ptr points to obj, set ptr = the Null/default buffer object.
645 * This is a helper for buffer object deletion.
646 * The GL spec says that deleting a buffer object causes it to get
647 * unbound from all arrays in the current context.
648 */
649 static void
650 unbind(struct gl_context *ctx,
651 struct gl_buffer_object **ptr,
652 struct gl_buffer_object *obj)
653 {
654 if (*ptr == obj) {
655 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
656 }
657 }
658
659
660 /**
661 * Plug default/fallback buffer object functions into the device
662 * driver hooks.
663 */
664 void
665 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
666 {
667 /* GL_ARB_vertex/pixel_buffer_object */
668 driver->NewBufferObject = _mesa_new_buffer_object;
669 driver->DeleteBuffer = _mesa_delete_buffer_object;
670 driver->BindBuffer = NULL;
671 driver->BufferData = _mesa_buffer_data;
672 driver->BufferSubData = _mesa_buffer_subdata;
673 driver->GetBufferSubData = _mesa_buffer_get_subdata;
674 driver->UnmapBuffer = _mesa_buffer_unmap;
675
676 /* GL_ARB_map_buffer_range */
677 driver->MapBufferRange = _mesa_buffer_map_range;
678 driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
679
680 /* GL_ARB_copy_buffer */
681 driver->CopyBufferSubData = _mesa_copy_buffer_subdata;
682 }
683
684
685
686 /**********************************************************************/
687 /* API Functions */
688 /**********************************************************************/
689
690 void GLAPIENTRY
691 _mesa_BindBufferARB(GLenum target, GLuint buffer)
692 {
693 GET_CURRENT_CONTEXT(ctx);
694 ASSERT_OUTSIDE_BEGIN_END(ctx);
695
696 if (MESA_VERBOSE & VERBOSE_API)
697 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
698 _mesa_lookup_enum_by_nr(target), buffer);
699
700 bind_buffer_object(ctx, target, buffer);
701 }
702
703
704 /**
705 * Delete a set of buffer objects.
706 *
707 * \param n Number of buffer objects to delete.
708 * \param ids Array of \c n buffer object IDs.
709 */
710 void GLAPIENTRY
711 _mesa_DeleteBuffersARB(GLsizei n, const GLuint *ids)
712 {
713 GET_CURRENT_CONTEXT(ctx);
714 GLsizei i;
715 ASSERT_OUTSIDE_BEGIN_END(ctx);
716 FLUSH_VERTICES(ctx, 0);
717
718 if (n < 0) {
719 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
720 return;
721 }
722
723 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
724
725 for (i = 0; i < n; i++) {
726 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
727 if (bufObj) {
728 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
729 GLuint j;
730
731 ASSERT(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
732
733 if (_mesa_bufferobj_mapped(bufObj)) {
734 /* if mapped, unmap it now */
735 ctx->Driver.UnmapBuffer(ctx, bufObj);
736 bufObj->AccessFlags = DEFAULT_ACCESS;
737 bufObj->Pointer = NULL;
738 }
739
740 /* unbind any vertex pointers bound to this buffer */
741 unbind(ctx, &arrayObj->Vertex.BufferObj, bufObj);
742 unbind(ctx, &arrayObj->Weight.BufferObj, bufObj);
743 unbind(ctx, &arrayObj->Normal.BufferObj, bufObj);
744 unbind(ctx, &arrayObj->Color.BufferObj, bufObj);
745 unbind(ctx, &arrayObj->SecondaryColor.BufferObj, bufObj);
746 unbind(ctx, &arrayObj->FogCoord.BufferObj, bufObj);
747 unbind(ctx, &arrayObj->Index.BufferObj, bufObj);
748 unbind(ctx, &arrayObj->EdgeFlag.BufferObj, bufObj);
749 for (j = 0; j < Elements(arrayObj->TexCoord); j++) {
750 unbind(ctx, &arrayObj->TexCoord[j].BufferObj, bufObj);
751 }
752 for (j = 0; j < Elements(arrayObj->VertexAttrib); j++) {
753 unbind(ctx, &arrayObj->VertexAttrib[j].BufferObj, bufObj);
754 }
755
756 if (ctx->Array.ArrayBufferObj == bufObj) {
757 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
758 }
759 if (arrayObj->ElementArrayBufferObj == bufObj) {
760 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
761 }
762
763 /* unbind any pixel pack/unpack pointers bound to this buffer */
764 if (ctx->Pack.BufferObj == bufObj) {
765 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 );
766 }
767 if (ctx->Unpack.BufferObj == bufObj) {
768 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
769 }
770
771 /* The ID is immediately freed for re-use */
772 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
773 /* Make sure we do not run into the classic ABA problem on bind.
774 * We don't want to allow re-binding a buffer object that's been
775 * "deleted" by glDeleteBuffers().
776 *
777 * The explicit rebinding to the default object in the current context
778 * prevents the above in the current context, but another context
779 * sharing the same objects might suffer from this problem.
780 * The alternative would be to do the hash lookup in any case on bind
781 * which would introduce more runtime overhead than this.
782 */
783 bufObj->DeletePending = GL_TRUE;
784 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
785 }
786 }
787
788 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
789 }
790
791
792 /**
793 * Generate a set of unique buffer object IDs and store them in \c buffer.
794 *
795 * \param n Number of IDs to generate.
796 * \param buffer Array of \c n locations to store the IDs.
797 */
798 void GLAPIENTRY
799 _mesa_GenBuffersARB(GLsizei n, GLuint *buffer)
800 {
801 GET_CURRENT_CONTEXT(ctx);
802 GLuint first;
803 GLint i;
804 ASSERT_OUTSIDE_BEGIN_END(ctx);
805
806 if (MESA_VERBOSE & VERBOSE_API)
807 _mesa_debug(ctx, "glGenBuffers(%d)\n", n);
808
809 if (n < 0) {
810 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
811 return;
812 }
813
814 if (!buffer) {
815 return;
816 }
817
818 /*
819 * This must be atomic (generation and allocation of buffer object IDs)
820 */
821 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
822
823 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
824
825 /* Insert the ID and pointer to dummy buffer object into hash table */
826 for (i = 0; i < n; i++) {
827 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i,
828 &DummyBufferObject);
829 buffer[i] = first + i;
830 }
831
832 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
833 }
834
835
836 /**
837 * Determine if ID is the name of a buffer object.
838 *
839 * \param id ID of the potential buffer object.
840 * \return \c GL_TRUE if \c id is the name of a buffer object,
841 * \c GL_FALSE otherwise.
842 */
843 GLboolean GLAPIENTRY
844 _mesa_IsBufferARB(GLuint id)
845 {
846 struct gl_buffer_object *bufObj;
847 GET_CURRENT_CONTEXT(ctx);
848 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
849
850 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
851 bufObj = _mesa_lookup_bufferobj(ctx, id);
852 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
853
854 return bufObj && bufObj != &DummyBufferObject;
855 }
856
857
858 void GLAPIENTRY
859 _mesa_BufferDataARB(GLenum target, GLsizeiptrARB size,
860 const GLvoid * data, GLenum usage)
861 {
862 GET_CURRENT_CONTEXT(ctx);
863 struct gl_buffer_object *bufObj;
864 ASSERT_OUTSIDE_BEGIN_END(ctx);
865
866 if (MESA_VERBOSE & VERBOSE_API)
867 _mesa_debug(ctx, "glBufferData(%s, %ld, %p, %s)\n",
868 _mesa_lookup_enum_by_nr(target),
869 (long int) size, data,
870 _mesa_lookup_enum_by_nr(usage));
871
872 if (size < 0) {
873 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
874 return;
875 }
876
877 switch (usage) {
878 case GL_STREAM_DRAW_ARB:
879 case GL_STREAM_READ_ARB:
880 case GL_STREAM_COPY_ARB:
881 case GL_STATIC_DRAW_ARB:
882 case GL_STATIC_READ_ARB:
883 case GL_STATIC_COPY_ARB:
884 case GL_DYNAMIC_DRAW_ARB:
885 case GL_DYNAMIC_READ_ARB:
886 case GL_DYNAMIC_COPY_ARB:
887 /* OK */
888 break;
889 default:
890 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(usage)");
891 return;
892 }
893
894 bufObj = get_buffer(ctx, target);
895 if (!bufObj) {
896 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(target)" );
897 return;
898 }
899 if (!_mesa_is_bufferobj(bufObj)) {
900 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferDataARB(buffer 0)" );
901 return;
902 }
903
904 if (_mesa_bufferobj_mapped(bufObj)) {
905 /* Unmap the existing buffer. We'll replace it now. Not an error. */
906 ctx->Driver.UnmapBuffer(ctx, bufObj);
907 bufObj->AccessFlags = DEFAULT_ACCESS;
908 ASSERT(bufObj->Pointer == NULL);
909 }
910
911 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
912
913 bufObj->Written = GL_TRUE;
914
915 #ifdef VBO_DEBUG
916 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
917 bufObj->Name, size, data, usage);
918 #endif
919
920 #ifdef BOUNDS_CHECK
921 size += 100;
922 #endif
923
924 ASSERT(ctx->Driver.BufferData);
925 if (!ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj )) {
926 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB()");
927 }
928 }
929
930
931 void GLAPIENTRY
932 _mesa_BufferSubDataARB(GLenum target, GLintptrARB offset,
933 GLsizeiptrARB size, const GLvoid * data)
934 {
935 GET_CURRENT_CONTEXT(ctx);
936 struct gl_buffer_object *bufObj;
937 ASSERT_OUTSIDE_BEGIN_END(ctx);
938
939 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
940 "glBufferSubDataARB" );
941 if (!bufObj) {
942 /* error already recorded */
943 return;
944 }
945
946 if (size == 0)
947 return;
948
949 bufObj->Written = GL_TRUE;
950
951 ASSERT(ctx->Driver.BufferSubData);
952 ctx->Driver.BufferSubData( ctx, offset, size, data, bufObj );
953 }
954
955
956 void GLAPIENTRY
957 _mesa_GetBufferSubDataARB(GLenum target, GLintptrARB offset,
958 GLsizeiptrARB size, void * data)
959 {
960 GET_CURRENT_CONTEXT(ctx);
961 struct gl_buffer_object *bufObj;
962 ASSERT_OUTSIDE_BEGIN_END(ctx);
963
964 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
965 "glGetBufferSubDataARB" );
966 if (!bufObj) {
967 /* error already recorded */
968 return;
969 }
970
971 ASSERT(ctx->Driver.GetBufferSubData);
972 ctx->Driver.GetBufferSubData( ctx, offset, size, data, bufObj );
973 }
974
975
976 void * GLAPIENTRY
977 _mesa_MapBufferARB(GLenum target, GLenum access)
978 {
979 GET_CURRENT_CONTEXT(ctx);
980 struct gl_buffer_object * bufObj;
981 GLbitfield accessFlags;
982 void *map;
983
984 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
985
986 switch (access) {
987 case GL_READ_ONLY_ARB:
988 accessFlags = GL_MAP_READ_BIT;
989 break;
990 case GL_WRITE_ONLY_ARB:
991 accessFlags = GL_MAP_WRITE_BIT;
992 break;
993 case GL_READ_WRITE_ARB:
994 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
995 break;
996 default:
997 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
998 return NULL;
999 }
1000
1001 bufObj = get_buffer(ctx, target);
1002 if (!bufObj) {
1003 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(target)" );
1004 return NULL;
1005 }
1006 if (!_mesa_is_bufferobj(bufObj)) {
1007 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(buffer 0)" );
1008 return NULL;
1009 }
1010 if (_mesa_bufferobj_mapped(bufObj)) {
1011 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
1012 return NULL;
1013 }
1014
1015 if (!bufObj->Size) {
1016 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1017 "glMapBuffer(buffer size = 0)");
1018 return NULL;
1019 }
1020
1021 ASSERT(ctx->Driver.MapBufferRange);
1022 map = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size, accessFlags, bufObj);
1023 if (!map) {
1024 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1025 return NULL;
1026 }
1027 else {
1028 /* The driver callback should have set these fields.
1029 * This is important because other modules (like VBO) might call
1030 * the driver function directly.
1031 */
1032 ASSERT(bufObj->Pointer == map);
1033 ASSERT(bufObj->Length == bufObj->Size);
1034 ASSERT(bufObj->Offset == 0);
1035 bufObj->AccessFlags = accessFlags;
1036 }
1037
1038 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
1039 bufObj->Written = GL_TRUE;
1040
1041 #ifdef VBO_DEBUG
1042 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1043 bufObj->Name, bufObj->Size, access);
1044 if (access == GL_WRITE_ONLY_ARB) {
1045 GLuint i;
1046 GLubyte *b = (GLubyte *) bufObj->Pointer;
1047 for (i = 0; i < bufObj->Size; i++)
1048 b[i] = i & 0xff;
1049 }
1050 #endif
1051
1052 #ifdef BOUNDS_CHECK
1053 {
1054 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1055 GLuint i;
1056 /* buffer is 100 bytes larger than requested, fill with magic value */
1057 for (i = 0; i < 100; i++) {
1058 buf[bufObj->Size - i - 1] = 123;
1059 }
1060 }
1061 #endif
1062
1063 return bufObj->Pointer;
1064 }
1065
1066
1067 GLboolean GLAPIENTRY
1068 _mesa_UnmapBufferARB(GLenum target)
1069 {
1070 GET_CURRENT_CONTEXT(ctx);
1071 struct gl_buffer_object *bufObj;
1072 GLboolean status = GL_TRUE;
1073 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1074
1075 bufObj = get_buffer(ctx, target);
1076 if (!bufObj) {
1077 _mesa_error(ctx, GL_INVALID_ENUM, "glUnmapBufferARB(target)" );
1078 return GL_FALSE;
1079 }
1080 if (!_mesa_is_bufferobj(bufObj)) {
1081 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB" );
1082 return GL_FALSE;
1083 }
1084 if (!_mesa_bufferobj_mapped(bufObj)) {
1085 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1086 return GL_FALSE;
1087 }
1088
1089 #ifdef BOUNDS_CHECK
1090 if (bufObj->Access != GL_READ_ONLY_ARB) {
1091 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1092 GLuint i;
1093 /* check that last 100 bytes are still = magic value */
1094 for (i = 0; i < 100; i++) {
1095 GLuint pos = bufObj->Size - i - 1;
1096 if (buf[pos] != 123) {
1097 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1098 " at position %d (value = %u)\n",
1099 pos, buf[pos]);
1100 }
1101 }
1102 }
1103 #endif
1104
1105 #ifdef VBO_DEBUG
1106 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1107 GLuint i, unchanged = 0;
1108 GLubyte *b = (GLubyte *) bufObj->Pointer;
1109 GLint pos = -1;
1110 /* check which bytes changed */
1111 for (i = 0; i < bufObj->Size - 1; i++) {
1112 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1113 unchanged++;
1114 if (pos == -1)
1115 pos = i;
1116 }
1117 }
1118 if (unchanged) {
1119 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1120 bufObj->Name, unchanged, bufObj->Size, pos);
1121 }
1122 }
1123 #endif
1124
1125 status = ctx->Driver.UnmapBuffer( ctx, bufObj );
1126 bufObj->AccessFlags = DEFAULT_ACCESS;
1127 ASSERT(bufObj->Pointer == NULL);
1128 ASSERT(bufObj->Offset == 0);
1129 ASSERT(bufObj->Length == 0);
1130
1131 return status;
1132 }
1133
1134
1135 void GLAPIENTRY
1136 _mesa_GetBufferParameterivARB(GLenum target, GLenum pname, GLint *params)
1137 {
1138 GET_CURRENT_CONTEXT(ctx);
1139 struct gl_buffer_object *bufObj;
1140 ASSERT_OUTSIDE_BEGIN_END(ctx);
1141
1142 bufObj = get_buffer(ctx, target);
1143 if (!bufObj) {
1144 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(target)" );
1145 return;
1146 }
1147 if (!_mesa_is_bufferobj(bufObj)) {
1148 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferParameterivARB" );
1149 return;
1150 }
1151
1152 switch (pname) {
1153 case GL_BUFFER_SIZE_ARB:
1154 *params = (GLint) bufObj->Size;
1155 return;
1156 case GL_BUFFER_USAGE_ARB:
1157 *params = bufObj->Usage;
1158 return;
1159 case GL_BUFFER_ACCESS_ARB:
1160 *params = simplified_access_mode(bufObj->AccessFlags);
1161 return;
1162 case GL_BUFFER_MAPPED_ARB:
1163 *params = _mesa_bufferobj_mapped(bufObj);
1164 return;
1165 case GL_BUFFER_ACCESS_FLAGS:
1166 if (ctx->VersionMajor < 3)
1167 goto invalid_pname;
1168 *params = bufObj->AccessFlags;
1169 return;
1170 case GL_BUFFER_MAP_OFFSET:
1171 if (ctx->VersionMajor < 3)
1172 goto invalid_pname;
1173 *params = (GLint) bufObj->Offset;
1174 return;
1175 case GL_BUFFER_MAP_LENGTH:
1176 if (ctx->VersionMajor < 3)
1177 goto invalid_pname;
1178 *params = (GLint) bufObj->Length;
1179 return;
1180 default:
1181 ; /* fall-through */
1182 }
1183
1184 invalid_pname:
1185 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname=%s)",
1186 _mesa_lookup_enum_by_nr(pname));
1187 }
1188
1189
1190 /**
1191 * New in GL 3.2
1192 * This is pretty much a duplicate of GetBufferParameteriv() but the
1193 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1194 */
1195 void GLAPIENTRY
1196 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
1197 {
1198 GET_CURRENT_CONTEXT(ctx);
1199 struct gl_buffer_object *bufObj;
1200 ASSERT_OUTSIDE_BEGIN_END(ctx);
1201
1202 bufObj = get_buffer(ctx, target);
1203 if (!bufObj) {
1204 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(target)" );
1205 return;
1206 }
1207 if (!_mesa_is_bufferobj(bufObj)) {
1208 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferParameteri64v" );
1209 return;
1210 }
1211
1212 switch (pname) {
1213 case GL_BUFFER_SIZE_ARB:
1214 *params = bufObj->Size;
1215 return;
1216 case GL_BUFFER_USAGE_ARB:
1217 *params = bufObj->Usage;
1218 return;
1219 case GL_BUFFER_ACCESS_ARB:
1220 *params = simplified_access_mode(bufObj->AccessFlags);
1221 return;
1222 case GL_BUFFER_ACCESS_FLAGS:
1223 if (ctx->VersionMajor < 3)
1224 goto invalid_pname;
1225 *params = bufObj->AccessFlags;
1226 return;
1227 case GL_BUFFER_MAPPED_ARB:
1228 *params = _mesa_bufferobj_mapped(bufObj);
1229 return;
1230 case GL_BUFFER_MAP_OFFSET:
1231 if (ctx->VersionMajor < 3)
1232 goto invalid_pname;
1233 *params = bufObj->Offset;
1234 return;
1235 case GL_BUFFER_MAP_LENGTH:
1236 if (ctx->VersionMajor < 3)
1237 goto invalid_pname;
1238 *params = bufObj->Length;
1239 return;
1240 default:
1241 ; /* fall-through */
1242 }
1243
1244 invalid_pname:
1245 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
1246 _mesa_lookup_enum_by_nr(pname));
1247 }
1248
1249
1250 void GLAPIENTRY
1251 _mesa_GetBufferPointervARB(GLenum target, GLenum pname, GLvoid **params)
1252 {
1253 GET_CURRENT_CONTEXT(ctx);
1254 struct gl_buffer_object * bufObj;
1255 ASSERT_OUTSIDE_BEGIN_END(ctx);
1256
1257 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1258 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1259 return;
1260 }
1261
1262 bufObj = get_buffer(ctx, target);
1263 if (!bufObj) {
1264 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(target)" );
1265 return;
1266 }
1267 if (!_mesa_is_bufferobj(bufObj)) {
1268 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferPointervARB" );
1269 return;
1270 }
1271
1272 *params = bufObj->Pointer;
1273 }
1274
1275
1276 void GLAPIENTRY
1277 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
1278 GLintptr readOffset, GLintptr writeOffset,
1279 GLsizeiptr size)
1280 {
1281 GET_CURRENT_CONTEXT(ctx);
1282 struct gl_buffer_object *src, *dst;
1283 ASSERT_OUTSIDE_BEGIN_END(ctx);
1284
1285 src = get_buffer(ctx, readTarget);
1286 if (!_mesa_is_bufferobj(src)) {
1287 _mesa_error(ctx, GL_INVALID_ENUM,
1288 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget);
1289 return;
1290 }
1291
1292 dst = get_buffer(ctx, writeTarget);
1293 if (!_mesa_is_bufferobj(dst)) {
1294 _mesa_error(ctx, GL_INVALID_ENUM,
1295 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget);
1296 return;
1297 }
1298
1299 if (_mesa_bufferobj_mapped(src)) {
1300 _mesa_error(ctx, GL_INVALID_OPERATION,
1301 "glCopyBuffserSubData(readBuffer is mapped)");
1302 return;
1303 }
1304
1305 if (_mesa_bufferobj_mapped(dst)) {
1306 _mesa_error(ctx, GL_INVALID_OPERATION,
1307 "glCopyBuffserSubData(writeBuffer is mapped)");
1308 return;
1309 }
1310
1311 if (readOffset < 0) {
1312 _mesa_error(ctx, GL_INVALID_VALUE,
1313 "glCopyBuffserSubData(readOffset = %d)", (int) readOffset);
1314 return;
1315 }
1316
1317 if (writeOffset < 0) {
1318 _mesa_error(ctx, GL_INVALID_VALUE,
1319 "glCopyBuffserSubData(writeOffset = %d)", (int) writeOffset);
1320 return;
1321 }
1322
1323 if (readOffset + size > src->Size) {
1324 _mesa_error(ctx, GL_INVALID_VALUE,
1325 "glCopyBuffserSubData(readOffset + size = %d)",
1326 (int) (readOffset + size));
1327 return;
1328 }
1329
1330 if (writeOffset + size > dst->Size) {
1331 _mesa_error(ctx, GL_INVALID_VALUE,
1332 "glCopyBuffserSubData(writeOffset + size = %d)",
1333 (int) (writeOffset + size));
1334 return;
1335 }
1336
1337 if (src == dst) {
1338 if (readOffset + size <= writeOffset) {
1339 /* OK */
1340 }
1341 else if (writeOffset + size <= readOffset) {
1342 /* OK */
1343 }
1344 else {
1345 /* overlapping src/dst is illegal */
1346 _mesa_error(ctx, GL_INVALID_VALUE,
1347 "glCopyBuffserSubData(overlapping src/dst)");
1348 return;
1349 }
1350 }
1351
1352 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
1353 }
1354
1355
1356 /**
1357 * See GL_ARB_map_buffer_range spec
1358 */
1359 void * GLAPIENTRY
1360 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
1361 GLbitfield access)
1362 {
1363 GET_CURRENT_CONTEXT(ctx);
1364 struct gl_buffer_object *bufObj;
1365 void *map;
1366
1367 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1368
1369 if (!ctx->Extensions.ARB_map_buffer_range) {
1370 _mesa_error(ctx, GL_INVALID_OPERATION,
1371 "glMapBufferRange(extension not supported)");
1372 return NULL;
1373 }
1374
1375 if (offset < 0) {
1376 _mesa_error(ctx, GL_INVALID_VALUE,
1377 "glMapBufferRange(offset = %ld)", (long)offset);
1378 return NULL;
1379 }
1380
1381 if (length < 0) {
1382 _mesa_error(ctx, GL_INVALID_VALUE,
1383 "glMapBufferRange(length = %ld)", (long)length);
1384 return NULL;
1385 }
1386
1387 if (access & ~(GL_MAP_READ_BIT |
1388 GL_MAP_WRITE_BIT |
1389 GL_MAP_INVALIDATE_RANGE_BIT |
1390 GL_MAP_INVALIDATE_BUFFER_BIT |
1391 GL_MAP_FLUSH_EXPLICIT_BIT |
1392 GL_MAP_UNSYNCHRONIZED_BIT)) {
1393 /* generate an error if any undefind bit is set */
1394 _mesa_error(ctx, GL_INVALID_VALUE, "glMapBufferRange(access)");
1395 return NULL;
1396 }
1397
1398 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
1399 _mesa_error(ctx, GL_INVALID_OPERATION,
1400 "glMapBufferRange(access indicates neither read or write)");
1401 return NULL;
1402 }
1403
1404 if ((access & GL_MAP_READ_BIT) &&
1405 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
1406 GL_MAP_INVALIDATE_BUFFER_BIT |
1407 GL_MAP_UNSYNCHRONIZED_BIT))) {
1408 _mesa_error(ctx, GL_INVALID_OPERATION,
1409 "glMapBufferRange(invalid access flags)");
1410 return NULL;
1411 }
1412
1413 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
1414 ((access & GL_MAP_WRITE_BIT) == 0)) {
1415 _mesa_error(ctx, GL_INVALID_OPERATION,
1416 "glMapBufferRange(invalid access flags)");
1417 return NULL;
1418 }
1419
1420 bufObj = get_buffer(ctx, target);
1421 if (!_mesa_is_bufferobj(bufObj)) {
1422 _mesa_error(ctx, GL_INVALID_ENUM,
1423 "glMapBufferRange(target = 0x%x)", target);
1424 return NULL;
1425 }
1426
1427 if (offset + length > bufObj->Size) {
1428 _mesa_error(ctx, GL_INVALID_VALUE,
1429 "glMapBufferRange(offset + length > size)");
1430 return NULL;
1431 }
1432
1433 if (_mesa_bufferobj_mapped(bufObj)) {
1434 _mesa_error(ctx, GL_INVALID_OPERATION,
1435 "glMapBufferRange(buffer already mapped)");
1436 return NULL;
1437 }
1438
1439 if (!bufObj->Size) {
1440 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1441 "glMapBufferRange(buffer size = 0)");
1442 return NULL;
1443 }
1444
1445 /* Mapping zero bytes should return a non-null pointer. */
1446 if (!length) {
1447 static long dummy = 0;
1448 bufObj->Pointer = &dummy;
1449 bufObj->Length = length;
1450 bufObj->Offset = offset;
1451 bufObj->AccessFlags = access;
1452 return bufObj->Pointer;
1453 }
1454
1455 ASSERT(ctx->Driver.MapBufferRange);
1456 map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj);
1457 if (!map) {
1458 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1459 }
1460 else {
1461 /* The driver callback should have set all these fields.
1462 * This is important because other modules (like VBO) might call
1463 * the driver function directly.
1464 */
1465 ASSERT(bufObj->Pointer == map);
1466 ASSERT(bufObj->Length == length);
1467 ASSERT(bufObj->Offset == offset);
1468 ASSERT(bufObj->AccessFlags == access);
1469 }
1470
1471 return map;
1472 }
1473
1474
1475 /**
1476 * See GL_ARB_map_buffer_range spec
1477 */
1478 void GLAPIENTRY
1479 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
1480 {
1481 GET_CURRENT_CONTEXT(ctx);
1482 struct gl_buffer_object *bufObj;
1483 ASSERT_OUTSIDE_BEGIN_END(ctx);
1484
1485 if (!ctx->Extensions.ARB_map_buffer_range) {
1486 _mesa_error(ctx, GL_INVALID_OPERATION,
1487 "glMapBufferRange(extension not supported)");
1488 return;
1489 }
1490
1491 if (offset < 0) {
1492 _mesa_error(ctx, GL_INVALID_VALUE,
1493 "glMapBufferRange(offset = %ld)", (long)offset);
1494 return;
1495 }
1496
1497 if (length < 0) {
1498 _mesa_error(ctx, GL_INVALID_VALUE,
1499 "glMapBufferRange(length = %ld)", (long)length);
1500 return;
1501 }
1502
1503 bufObj = get_buffer(ctx, target);
1504 if (!bufObj) {
1505 _mesa_error(ctx, GL_INVALID_ENUM,
1506 "glMapBufferRange(target = 0x%x)", target);
1507 return;
1508 }
1509
1510 if (!_mesa_is_bufferobj(bufObj)) {
1511 _mesa_error(ctx, GL_INVALID_OPERATION,
1512 "glMapBufferRange(current buffer is 0)");
1513 return;
1514 }
1515
1516 if (!_mesa_bufferobj_mapped(bufObj)) {
1517 /* buffer is not mapped */
1518 _mesa_error(ctx, GL_INVALID_OPERATION,
1519 "glMapBufferRange(buffer is not mapped)");
1520 return;
1521 }
1522
1523 if ((bufObj->AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
1524 _mesa_error(ctx, GL_INVALID_OPERATION,
1525 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1526 return;
1527 }
1528
1529 if (offset + length > bufObj->Length) {
1530 _mesa_error(ctx, GL_INVALID_VALUE,
1531 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1532 (long)offset, (long)length, (long)bufObj->Length);
1533 return;
1534 }
1535
1536 ASSERT(bufObj->AccessFlags & GL_MAP_WRITE_BIT);
1537
1538 if (ctx->Driver.FlushMappedBufferRange)
1539 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj);
1540 }
1541
1542
1543 #if FEATURE_APPLE_object_purgeable
1544 static GLenum
1545 buffer_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
1546 {
1547 struct gl_buffer_object *bufObj;
1548 GLenum retval;
1549
1550 bufObj = _mesa_lookup_bufferobj(ctx, name);
1551 if (!bufObj) {
1552 _mesa_error(ctx, GL_INVALID_VALUE,
1553 "glObjectPurgeable(name = 0x%x)", name);
1554 return 0;
1555 }
1556 if (!_mesa_is_bufferobj(bufObj)) {
1557 _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
1558 return 0;
1559 }
1560
1561 if (bufObj->Purgeable) {
1562 _mesa_error(ctx, GL_INVALID_OPERATION,
1563 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1564 return GL_VOLATILE_APPLE;
1565 }
1566
1567 bufObj->Purgeable = GL_TRUE;
1568
1569 retval = GL_VOLATILE_APPLE;
1570 if (ctx->Driver.BufferObjectPurgeable)
1571 retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
1572
1573 return retval;
1574 }
1575
1576
1577 static GLenum
1578 renderbuffer_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
1579 {
1580 struct gl_renderbuffer *bufObj;
1581 GLenum retval;
1582
1583 bufObj = _mesa_lookup_renderbuffer(ctx, name);
1584 if (!bufObj) {
1585 _mesa_error(ctx, GL_INVALID_VALUE,
1586 "glObjectUnpurgeable(name = 0x%x)", name);
1587 return 0;
1588 }
1589
1590 if (bufObj->Purgeable) {
1591 _mesa_error(ctx, GL_INVALID_OPERATION,
1592 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1593 return GL_VOLATILE_APPLE;
1594 }
1595
1596 bufObj->Purgeable = GL_TRUE;
1597
1598 retval = GL_VOLATILE_APPLE;
1599 if (ctx->Driver.RenderObjectPurgeable)
1600 retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
1601
1602 return retval;
1603 }
1604
1605
1606 static GLenum
1607 texture_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
1608 {
1609 struct gl_texture_object *bufObj;
1610 GLenum retval;
1611
1612 bufObj = _mesa_lookup_texture(ctx, name);
1613 if (!bufObj) {
1614 _mesa_error(ctx, GL_INVALID_VALUE,
1615 "glObjectPurgeable(name = 0x%x)", name);
1616 return 0;
1617 }
1618
1619 if (bufObj->Purgeable) {
1620 _mesa_error(ctx, GL_INVALID_OPERATION,
1621 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1622 return GL_VOLATILE_APPLE;
1623 }
1624
1625 bufObj->Purgeable = GL_TRUE;
1626
1627 retval = GL_VOLATILE_APPLE;
1628 if (ctx->Driver.TextureObjectPurgeable)
1629 retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
1630
1631 return retval;
1632 }
1633
1634
1635 GLenum GLAPIENTRY
1636 _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
1637 {
1638 GLenum retval;
1639
1640 GET_CURRENT_CONTEXT(ctx);
1641 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1642
1643 if (name == 0) {
1644 _mesa_error(ctx, GL_INVALID_VALUE,
1645 "glObjectPurgeable(name = 0x%x)", name);
1646 return 0;
1647 }
1648
1649 switch (option) {
1650 case GL_VOLATILE_APPLE:
1651 case GL_RELEASED_APPLE:
1652 /* legal */
1653 break;
1654 default:
1655 _mesa_error(ctx, GL_INVALID_ENUM,
1656 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1657 name, option);
1658 return 0;
1659 }
1660
1661 switch (objectType) {
1662 case GL_TEXTURE:
1663 retval = texture_object_purgeable(ctx, name, option);
1664 break;
1665 case GL_RENDERBUFFER_EXT:
1666 retval = renderbuffer_purgeable(ctx, name, option);
1667 break;
1668 case GL_BUFFER_OBJECT_APPLE:
1669 retval = buffer_object_purgeable(ctx, name, option);
1670 break;
1671 default:
1672 _mesa_error(ctx, GL_INVALID_ENUM,
1673 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1674 name, objectType);
1675 return 0;
1676 }
1677
1678 /* In strict conformance to the spec, we must only return VOLATILE when
1679 * when passed the VOLATILE option. Madness.
1680 *
1681 * XXX First fix the spec, then fix me.
1682 */
1683 return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
1684 }
1685
1686
1687 static GLenum
1688 buffer_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
1689 {
1690 struct gl_buffer_object *bufObj;
1691 GLenum retval;
1692
1693 bufObj = _mesa_lookup_bufferobj(ctx, name);
1694 if (!bufObj) {
1695 _mesa_error(ctx, GL_INVALID_VALUE,
1696 "glObjectUnpurgeable(name = 0x%x)", name);
1697 return 0;
1698 }
1699
1700 if (! bufObj->Purgeable) {
1701 _mesa_error(ctx, GL_INVALID_OPERATION,
1702 "glObjectUnpurgeable(name = 0x%x) object is "
1703 " already \"unpurged\"", name);
1704 return 0;
1705 }
1706
1707 bufObj->Purgeable = GL_FALSE;
1708
1709 retval = option;
1710 if (ctx->Driver.BufferObjectUnpurgeable)
1711 retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
1712
1713 return retval;
1714 }
1715
1716
1717 static GLenum
1718 renderbuffer_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
1719 {
1720 struct gl_renderbuffer *bufObj;
1721 GLenum retval;
1722
1723 bufObj = _mesa_lookup_renderbuffer(ctx, name);
1724 if (!bufObj) {
1725 _mesa_error(ctx, GL_INVALID_VALUE,
1726 "glObjectUnpurgeable(name = 0x%x)", name);
1727 return 0;
1728 }
1729
1730 if (! bufObj->Purgeable) {
1731 _mesa_error(ctx, GL_INVALID_OPERATION,
1732 "glObjectUnpurgeable(name = 0x%x) object is "
1733 " already \"unpurged\"", name);
1734 return 0;
1735 }
1736
1737 bufObj->Purgeable = GL_FALSE;
1738
1739 retval = option;
1740 if (ctx->Driver.RenderObjectUnpurgeable)
1741 retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
1742
1743 return retval;
1744 }
1745
1746
1747 static GLenum
1748 texture_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
1749 {
1750 struct gl_texture_object *bufObj;
1751 GLenum retval;
1752
1753 bufObj = _mesa_lookup_texture(ctx, name);
1754 if (!bufObj) {
1755 _mesa_error(ctx, GL_INVALID_VALUE,
1756 "glObjectUnpurgeable(name = 0x%x)", name);
1757 return 0;
1758 }
1759
1760 if (! bufObj->Purgeable) {
1761 _mesa_error(ctx, GL_INVALID_OPERATION,
1762 "glObjectUnpurgeable(name = 0x%x) object is"
1763 " already \"unpurged\"", name);
1764 return 0;
1765 }
1766
1767 bufObj->Purgeable = GL_FALSE;
1768
1769 retval = option;
1770 if (ctx->Driver.TextureObjectUnpurgeable)
1771 retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
1772
1773 return retval;
1774 }
1775
1776
1777 GLenum GLAPIENTRY
1778 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
1779 {
1780 GET_CURRENT_CONTEXT(ctx);
1781 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1782
1783 if (name == 0) {
1784 _mesa_error(ctx, GL_INVALID_VALUE,
1785 "glObjectUnpurgeable(name = 0x%x)", name);
1786 return 0;
1787 }
1788
1789 switch (option) {
1790 case GL_RETAINED_APPLE:
1791 case GL_UNDEFINED_APPLE:
1792 /* legal */
1793 break;
1794 default:
1795 _mesa_error(ctx, GL_INVALID_ENUM,
1796 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1797 name, option);
1798 return 0;
1799 }
1800
1801 switch (objectType) {
1802 case GL_BUFFER_OBJECT_APPLE:
1803 return buffer_object_unpurgeable(ctx, name, option);
1804 case GL_TEXTURE:
1805 return texture_object_unpurgeable(ctx, name, option);
1806 case GL_RENDERBUFFER_EXT:
1807 return renderbuffer_unpurgeable(ctx, name, option);
1808 default:
1809 _mesa_error(ctx, GL_INVALID_ENUM,
1810 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1811 name, objectType);
1812 return 0;
1813 }
1814 }
1815
1816
1817 static void
1818 get_buffer_object_parameteriv(struct gl_context *ctx, GLuint name,
1819 GLenum pname, GLint *params)
1820 {
1821 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
1822 if (!bufObj) {
1823 _mesa_error(ctx, GL_INVALID_VALUE,
1824 "glGetObjectParameteriv(name = 0x%x) invalid object", name);
1825 return;
1826 }
1827
1828 switch (pname) {
1829 case GL_PURGEABLE_APPLE:
1830 *params = bufObj->Purgeable;
1831 break;
1832 default:
1833 _mesa_error(ctx, GL_INVALID_ENUM,
1834 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1835 name, pname);
1836 break;
1837 }
1838 }
1839
1840
1841 static void
1842 get_renderbuffer_parameteriv(struct gl_context *ctx, GLuint name,
1843 GLenum pname, GLint *params)
1844 {
1845 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
1846 if (!rb) {
1847 _mesa_error(ctx, GL_INVALID_VALUE,
1848 "glObjectUnpurgeable(name = 0x%x)", name);
1849 return;
1850 }
1851
1852 switch (pname) {
1853 case GL_PURGEABLE_APPLE:
1854 *params = rb->Purgeable;
1855 break;
1856 default:
1857 _mesa_error(ctx, GL_INVALID_ENUM,
1858 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1859 name, pname);
1860 break;
1861 }
1862 }
1863
1864
1865 static void
1866 get_texture_object_parameteriv(struct gl_context *ctx, GLuint name,
1867 GLenum pname, GLint *params)
1868 {
1869 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
1870 if (!texObj) {
1871 _mesa_error(ctx, GL_INVALID_VALUE,
1872 "glObjectUnpurgeable(name = 0x%x)", name);
1873 return;
1874 }
1875
1876 switch (pname) {
1877 case GL_PURGEABLE_APPLE:
1878 *params = texObj->Purgeable;
1879 break;
1880 default:
1881 _mesa_error(ctx, GL_INVALID_ENUM,
1882 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1883 name, pname);
1884 break;
1885 }
1886 }
1887
1888
1889 void GLAPIENTRY
1890 _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
1891 GLint *params)
1892 {
1893 GET_CURRENT_CONTEXT(ctx);
1894
1895 if (name == 0) {
1896 _mesa_error(ctx, GL_INVALID_VALUE,
1897 "glGetObjectParameteriv(name = 0x%x)", name);
1898 return;
1899 }
1900
1901 switch (objectType) {
1902 case GL_TEXTURE:
1903 get_texture_object_parameteriv(ctx, name, pname, params);
1904 break;
1905 case GL_BUFFER_OBJECT_APPLE:
1906 get_buffer_object_parameteriv(ctx, name, pname, params);
1907 break;
1908 case GL_RENDERBUFFER_EXT:
1909 get_renderbuffer_parameteriv(ctx, name, pname, params);
1910 break;
1911 default:
1912 _mesa_error(ctx, GL_INVALID_ENUM,
1913 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
1914 name, objectType);
1915 }
1916 }
1917
1918 #endif /* FEATURE_APPLE_object_purgeable */