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