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