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