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