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