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