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