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