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