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