Merge remote branch 'origin/gallium-st-api-dri'
[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 #ifdef 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 return;
1479 }
1480
1481 invalid_pname:
1482 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
1483 _mesa_lookup_enum_by_nr(pname));
1484 }
1485
1486
1487 void GLAPIENTRY
1488 _mesa_GetBufferPointervARB(GLenum target, GLenum pname, GLvoid **params)
1489 {
1490 GET_CURRENT_CONTEXT(ctx);
1491 struct gl_buffer_object * bufObj;
1492 ASSERT_OUTSIDE_BEGIN_END(ctx);
1493
1494 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1495 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1496 return;
1497 }
1498
1499 bufObj = get_buffer(ctx, target);
1500 if (!bufObj) {
1501 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(target)" );
1502 return;
1503 }
1504 if (!_mesa_is_bufferobj(bufObj)) {
1505 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferPointervARB" );
1506 return;
1507 }
1508
1509 *params = bufObj->Pointer;
1510 }
1511
1512
1513 void GLAPIENTRY
1514 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
1515 GLintptr readOffset, GLintptr writeOffset,
1516 GLsizeiptr size)
1517 {
1518 GET_CURRENT_CONTEXT(ctx);
1519 struct gl_buffer_object *src, *dst;
1520 ASSERT_OUTSIDE_BEGIN_END(ctx);
1521
1522 src = get_buffer(ctx, readTarget);
1523 if (!src || !_mesa_is_bufferobj(src)) {
1524 _mesa_error(ctx, GL_INVALID_ENUM,
1525 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget);
1526 return;
1527 }
1528
1529 dst = get_buffer(ctx, writeTarget);
1530 if (!dst || !_mesa_is_bufferobj(dst)) {
1531 _mesa_error(ctx, GL_INVALID_ENUM,
1532 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget);
1533 return;
1534 }
1535
1536 if (_mesa_bufferobj_mapped(src)) {
1537 _mesa_error(ctx, GL_INVALID_OPERATION,
1538 "glCopyBuffserSubData(readBuffer is mapped)");
1539 return;
1540 }
1541
1542 if (_mesa_bufferobj_mapped(dst)) {
1543 _mesa_error(ctx, GL_INVALID_OPERATION,
1544 "glCopyBuffserSubData(writeBuffer is mapped)");
1545 return;
1546 }
1547
1548 if (readOffset < 0) {
1549 _mesa_error(ctx, GL_INVALID_VALUE,
1550 "glCopyBuffserSubData(readOffset = %d)", readOffset);
1551 return;
1552 }
1553
1554 if (writeOffset < 0) {
1555 _mesa_error(ctx, GL_INVALID_VALUE,
1556 "glCopyBuffserSubData(writeOffset = %d)", writeOffset);
1557 return;
1558 }
1559
1560 if (readOffset + size > src->Size) {
1561 _mesa_error(ctx, GL_INVALID_VALUE,
1562 "glCopyBuffserSubData(readOffset + size = %d)",
1563 readOffset, size);
1564 return;
1565 }
1566
1567 if (writeOffset + size > dst->Size) {
1568 _mesa_error(ctx, GL_INVALID_VALUE,
1569 "glCopyBuffserSubData(writeOffset + size = %d)",
1570 writeOffset, size);
1571 return;
1572 }
1573
1574 if (src == dst) {
1575 if (readOffset + size <= writeOffset) {
1576 /* OK */
1577 }
1578 else if (writeOffset + size <= readOffset) {
1579 /* OK */
1580 }
1581 else {
1582 /* overlapping src/dst is illegal */
1583 _mesa_error(ctx, GL_INVALID_VALUE,
1584 "glCopyBuffserSubData(overlapping src/dst)");
1585 return;
1586 }
1587 }
1588
1589 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
1590 }
1591
1592
1593 /**
1594 * See GL_ARB_map_buffer_range spec
1595 */
1596 void * GLAPIENTRY
1597 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
1598 GLbitfield access)
1599 {
1600 GET_CURRENT_CONTEXT(ctx);
1601 struct gl_buffer_object *bufObj;
1602 void *map;
1603
1604 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1605
1606 if (!ctx->Extensions.ARB_map_buffer_range) {
1607 _mesa_error(ctx, GL_INVALID_OPERATION,
1608 "glMapBufferRange(extension not supported)");
1609 return NULL;
1610 }
1611
1612 if (offset < 0) {
1613 _mesa_error(ctx, GL_INVALID_VALUE,
1614 "glMapBufferRange(offset = %ld)", offset);
1615 return NULL;
1616 }
1617
1618 if (length < 0) {
1619 _mesa_error(ctx, GL_INVALID_VALUE,
1620 "glMapBufferRange(length = %ld)", length);
1621 return NULL;
1622 }
1623
1624 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
1625 _mesa_error(ctx, GL_INVALID_OPERATION,
1626 "glMapBufferRange(access indicates neither read or write)");
1627 return NULL;
1628 }
1629
1630 if (access & GL_MAP_READ_BIT) {
1631 if ((access & GL_MAP_INVALIDATE_RANGE_BIT) ||
1632 (access & GL_MAP_INVALIDATE_BUFFER_BIT) ||
1633 (access & GL_MAP_UNSYNCHRONIZED_BIT)) {
1634 _mesa_error(ctx, GL_INVALID_OPERATION,
1635 "glMapBufferRange(invalid access flags)");
1636 return NULL;
1637 }
1638 }
1639
1640 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
1641 ((access & GL_MAP_WRITE_BIT) == 0)) {
1642 _mesa_error(ctx, GL_INVALID_OPERATION,
1643 "glMapBufferRange(invalid access flags)");
1644 return NULL;
1645 }
1646
1647 bufObj = get_buffer(ctx, target);
1648 if (!bufObj || !_mesa_is_bufferobj(bufObj)) {
1649 _mesa_error(ctx, GL_INVALID_ENUM,
1650 "glMapBufferRange(target = 0x%x)", target);
1651 return NULL;
1652 }
1653
1654 if (offset + length > bufObj->Size) {
1655 _mesa_error(ctx, GL_INVALID_VALUE,
1656 "glMapBufferRange(offset + length > size)");
1657 return NULL;
1658 }
1659
1660 if (_mesa_bufferobj_mapped(bufObj)) {
1661 _mesa_error(ctx, GL_INVALID_OPERATION,
1662 "glMapBufferRange(buffer already mapped)");
1663 return NULL;
1664 }
1665
1666 ASSERT(ctx->Driver.MapBufferRange);
1667 map = ctx->Driver.MapBufferRange(ctx, target, offset, length,
1668 access, bufObj);
1669 if (!map) {
1670 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1671 }
1672 else {
1673 /* The driver callback should have set all these fields.
1674 * This is important because other modules (like VBO) might call
1675 * the driver function directly.
1676 */
1677 ASSERT(bufObj->Pointer == map);
1678 ASSERT(bufObj->Length == length);
1679 ASSERT(bufObj->Offset == offset);
1680 ASSERT(bufObj->AccessFlags == access);
1681 }
1682
1683 return map;
1684 }
1685
1686
1687 /**
1688 * See GL_ARB_map_buffer_range spec
1689 */
1690 void GLAPIENTRY
1691 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
1692 {
1693 GET_CURRENT_CONTEXT(ctx);
1694 struct gl_buffer_object *bufObj;
1695 ASSERT_OUTSIDE_BEGIN_END(ctx);
1696
1697 if (!ctx->Extensions.ARB_map_buffer_range) {
1698 _mesa_error(ctx, GL_INVALID_OPERATION,
1699 "glMapBufferRange(extension not supported)");
1700 return;
1701 }
1702
1703 if (offset < 0) {
1704 _mesa_error(ctx, GL_INVALID_VALUE,
1705 "glMapBufferRange(offset = %ld)", offset);
1706 return;
1707 }
1708
1709 if (length < 0) {
1710 _mesa_error(ctx, GL_INVALID_VALUE,
1711 "glMapBufferRange(length = %ld)", length);
1712 return;
1713 }
1714
1715 bufObj = get_buffer(ctx, target);
1716 if (!bufObj) {
1717 _mesa_error(ctx, GL_INVALID_ENUM,
1718 "glMapBufferRange(target = 0x%x)", target);
1719 return;
1720 }
1721
1722 if (!_mesa_is_bufferobj(bufObj)) {
1723 _mesa_error(ctx, GL_INVALID_OPERATION,
1724 "glMapBufferRange(current buffer is 0)");
1725 return;
1726 }
1727
1728 if (!_mesa_bufferobj_mapped(bufObj)) {
1729 /* buffer is not mapped */
1730 _mesa_error(ctx, GL_INVALID_OPERATION,
1731 "glMapBufferRange(buffer is not mapped)");
1732 return;
1733 }
1734
1735 if ((bufObj->AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
1736 _mesa_error(ctx, GL_INVALID_OPERATION,
1737 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1738 return;
1739 }
1740
1741 if (offset + length > bufObj->Length) {
1742 _mesa_error(ctx, GL_INVALID_VALUE,
1743 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1744 offset, length, bufObj->Length);
1745 return;
1746 }
1747
1748 ASSERT(bufObj->AccessFlags & GL_MAP_WRITE_BIT);
1749
1750 if (ctx->Driver.FlushMappedBufferRange)
1751 ctx->Driver.FlushMappedBufferRange(ctx, target, offset, length, bufObj);
1752 }
1753
1754
1755 #if FEATURE_APPLE_object_purgeable
1756 static GLenum
1757 _mesa_BufferObjectPurgeable(GLcontext *ctx, GLuint name, GLenum option)
1758 {
1759 struct gl_buffer_object *bufObj;
1760 GLenum retval;
1761
1762 bufObj = _mesa_lookup_bufferobj(ctx, name);
1763 if (!bufObj) {
1764 _mesa_error(ctx, GL_INVALID_VALUE,
1765 "glObjectPurgeable(name = 0x%x)", name);
1766 return 0;
1767 }
1768 if (!_mesa_is_bufferobj(bufObj)) {
1769 _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
1770 return 0;
1771 }
1772
1773 if (bufObj->Purgeable) {
1774 _mesa_error(ctx, GL_INVALID_OPERATION,
1775 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1776 return GL_VOLATILE_APPLE;
1777 }
1778
1779 bufObj->Purgeable = GL_TRUE;
1780
1781 retval = GL_VOLATILE_APPLE;
1782 if (ctx->Driver.BufferObjectPurgeable)
1783 retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
1784
1785 return retval;
1786 }
1787
1788
1789 static GLenum
1790 _mesa_RenderObjectPurgeable(GLcontext *ctx, GLuint name, GLenum option)
1791 {
1792 struct gl_renderbuffer *bufObj;
1793 GLenum retval;
1794
1795 bufObj = _mesa_lookup_renderbuffer(ctx, name);
1796 if (!bufObj) {
1797 _mesa_error(ctx, GL_INVALID_VALUE,
1798 "glObjectUnpurgeable(name = 0x%x)", name);
1799 return 0;
1800 }
1801
1802 if (bufObj->Purgeable) {
1803 _mesa_error(ctx, GL_INVALID_OPERATION,
1804 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1805 return GL_VOLATILE_APPLE;
1806 }
1807
1808 bufObj->Purgeable = GL_TRUE;
1809
1810 retval = GL_VOLATILE_APPLE;
1811 if (ctx->Driver.RenderObjectPurgeable)
1812 retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
1813
1814 return retval;
1815 }
1816
1817
1818 static GLenum
1819 _mesa_TextureObjectPurgeable(GLcontext *ctx, GLuint name, GLenum option)
1820 {
1821 struct gl_texture_object *bufObj;
1822 GLenum retval;
1823
1824 bufObj = _mesa_lookup_texture(ctx, name);
1825 if (!bufObj) {
1826 _mesa_error(ctx, GL_INVALID_VALUE,
1827 "glObjectPurgeable(name = 0x%x)", name);
1828 return 0;
1829 }
1830
1831 if (bufObj->Purgeable) {
1832 _mesa_error(ctx, GL_INVALID_OPERATION,
1833 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1834 return GL_VOLATILE_APPLE;
1835 }
1836
1837 bufObj->Purgeable = GL_TRUE;
1838
1839 retval = GL_VOLATILE_APPLE;
1840 if (ctx->Driver.TextureObjectPurgeable)
1841 retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
1842
1843 return retval;
1844 }
1845
1846
1847 GLenum GLAPIENTRY
1848 _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
1849 {
1850 GLenum retval;
1851
1852 GET_CURRENT_CONTEXT(ctx);
1853 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1854
1855 if (name == 0) {
1856 _mesa_error(ctx, GL_INVALID_VALUE,
1857 "glObjectPurgeable(name = 0x%x)", name);
1858 return 0;
1859 }
1860
1861 switch (option) {
1862 case GL_VOLATILE_APPLE:
1863 case GL_RELEASED_APPLE:
1864 /* legal */
1865 break;
1866 default:
1867 _mesa_error(ctx, GL_INVALID_ENUM,
1868 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1869 name, option);
1870 return 0;
1871 }
1872
1873 switch (objectType) {
1874 case GL_TEXTURE:
1875 retval = _mesa_TextureObjectPurgeable (ctx, name, option);
1876 break;
1877 case GL_RENDERBUFFER_EXT:
1878 retval = _mesa_RenderObjectPurgeable (ctx, name, option);
1879 break;
1880 case GL_BUFFER_OBJECT_APPLE:
1881 retval = _mesa_BufferObjectPurgeable (ctx, name, option);
1882 break;
1883 default:
1884 _mesa_error(ctx, GL_INVALID_ENUM,
1885 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1886 name, objectType);
1887 return 0;
1888 }
1889
1890 /* In strict conformance to the spec, we must only return VOLATILE when
1891 * when passed the VOLATILE option. Madness.
1892 *
1893 * XXX First fix the spec, then fix me.
1894 */
1895 return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
1896 }
1897
1898
1899 static GLenum
1900 _mesa_BufferObjectUnpurgeable(GLcontext *ctx, GLuint name, GLenum option)
1901 {
1902 struct gl_buffer_object *bufObj;
1903 GLenum retval;
1904
1905 bufObj = _mesa_lookup_bufferobj(ctx, name);
1906 if (!bufObj) {
1907 _mesa_error(ctx, GL_INVALID_VALUE,
1908 "glObjectUnpurgeable(name = 0x%x)", name);
1909 return 0;
1910 }
1911
1912 if (! bufObj->Purgeable) {
1913 _mesa_error(ctx, GL_INVALID_OPERATION,
1914 "glObjectUnpurgeable(name = 0x%x) object is "
1915 " already \"unpurged\"", name);
1916 return 0;
1917 }
1918
1919 bufObj->Purgeable = GL_FALSE;
1920
1921 retval = GL_RETAINED_APPLE;
1922 if (ctx->Driver.BufferObjectUnpurgeable)
1923 retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
1924
1925 return retval;
1926 }
1927
1928
1929 static GLenum
1930 _mesa_RenderObjectUnpurgeable(GLcontext *ctx, GLuint name, GLenum option)
1931 {
1932 struct gl_renderbuffer *bufObj;
1933 GLenum retval;
1934
1935 bufObj = _mesa_lookup_renderbuffer(ctx, name);
1936 if (!bufObj) {
1937 _mesa_error(ctx, GL_INVALID_VALUE,
1938 "glObjectUnpurgeable(name = 0x%x)", name);
1939 return 0;
1940 }
1941
1942 if (! bufObj->Purgeable) {
1943 _mesa_error(ctx, GL_INVALID_OPERATION,
1944 "glObjectUnpurgeable(name = 0x%x) object is "
1945 " already \"unpurged\"", name);
1946 return 0;
1947 }
1948
1949 bufObj->Purgeable = GL_FALSE;
1950
1951 retval = GL_RETAINED_APPLE;
1952 if (ctx->Driver.RenderObjectUnpurgeable)
1953 retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
1954
1955 return option;
1956 }
1957
1958
1959 static GLenum
1960 _mesa_TextureObjectUnpurgeable(GLcontext *ctx, GLuint name, GLenum option)
1961 {
1962 struct gl_texture_object *bufObj;
1963 GLenum retval;
1964
1965 bufObj = _mesa_lookup_texture(ctx, name);
1966 if (!bufObj) {
1967 _mesa_error(ctx, GL_INVALID_VALUE,
1968 "glObjectUnpurgeable(name = 0x%x)", name);
1969 return 0;
1970 }
1971
1972 if (! bufObj->Purgeable) {
1973 _mesa_error(ctx, GL_INVALID_OPERATION,
1974 "glObjectUnpurgeable(name = 0x%x) object is"
1975 " already \"unpurged\"", name);
1976 return 0;
1977 }
1978
1979 bufObj->Purgeable = GL_FALSE;
1980
1981 retval = GL_RETAINED_APPLE;
1982 if (ctx->Driver.TextureObjectUnpurgeable)
1983 retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
1984
1985 return retval;
1986 }
1987
1988
1989 GLenum GLAPIENTRY
1990 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
1991 {
1992 GET_CURRENT_CONTEXT(ctx);
1993 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1994
1995 if (name == 0) {
1996 _mesa_error(ctx, GL_INVALID_VALUE,
1997 "glObjectUnpurgeable(name = 0x%x)", name);
1998 return 0;
1999 }
2000
2001 switch (option) {
2002 case GL_RETAINED_APPLE:
2003 case GL_UNDEFINED_APPLE:
2004 /* legal */
2005 break;
2006 default:
2007 _mesa_error(ctx, GL_INVALID_ENUM,
2008 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2009 name, option);
2010 return 0;
2011 }
2012
2013 switch (objectType) {
2014 case GL_BUFFER_OBJECT_APPLE:
2015 return _mesa_BufferObjectUnpurgeable(ctx, name, option);
2016 case GL_TEXTURE:
2017 return _mesa_TextureObjectUnpurgeable(ctx, name, option);
2018 case GL_RENDERBUFFER_EXT:
2019 return _mesa_RenderObjectUnpurgeable(ctx, name, option);
2020 default:
2021 _mesa_error(ctx, GL_INVALID_ENUM,
2022 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2023 name, objectType);
2024 return 0;
2025 }
2026 }
2027
2028
2029 static void
2030 _mesa_GetBufferObjectParameterivAPPLE(GLcontext *ctx, GLuint name,
2031 GLenum pname, GLint* params)
2032 {
2033 struct gl_buffer_object *bufObj;
2034
2035 bufObj = _mesa_lookup_bufferobj(ctx, name);
2036 if (!bufObj) {
2037 _mesa_error(ctx, GL_INVALID_VALUE,
2038 "glGetObjectParameteriv(name = 0x%x) invalid object", name);
2039 return;
2040 }
2041
2042 switch (pname) {
2043 case GL_PURGEABLE_APPLE:
2044 *params = bufObj->Purgeable;
2045 break;
2046 default:
2047 _mesa_error(ctx, GL_INVALID_ENUM,
2048 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2049 name, pname);
2050 break;
2051 }
2052 }
2053
2054
2055 static void
2056 _mesa_GetRenderObjectParameterivAPPLE(GLcontext *ctx, GLuint name,
2057 GLenum pname, GLint* params)
2058 {
2059 struct gl_renderbuffer *bufObj;
2060
2061 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2062 if (!bufObj) {
2063 _mesa_error(ctx, GL_INVALID_VALUE,
2064 "glObjectUnpurgeable(name = 0x%x)", name);
2065 return;
2066 }
2067
2068 switch (pname) {
2069 case GL_PURGEABLE_APPLE:
2070 *params = bufObj->Purgeable;
2071 break;
2072 default:
2073 _mesa_error(ctx, GL_INVALID_ENUM,
2074 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2075 name, pname);
2076 break;
2077 }
2078 }
2079
2080
2081 static void
2082 _mesa_GetTextureObjectParameterivAPPLE(GLcontext *ctx, GLuint name,
2083 GLenum pname, GLint* params)
2084 {
2085 struct gl_texture_object *bufObj;
2086
2087 bufObj = _mesa_lookup_texture(ctx, name);
2088 if (!bufObj) {
2089 _mesa_error(ctx, GL_INVALID_VALUE,
2090 "glObjectUnpurgeable(name = 0x%x)", name);
2091 return;
2092 }
2093
2094 switch (pname) {
2095 case GL_PURGEABLE_APPLE:
2096 *params = bufObj->Purgeable;
2097 break;
2098 default:
2099 _mesa_error(ctx, GL_INVALID_ENUM,
2100 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2101 name, pname);
2102 break;
2103 }
2104 }
2105
2106
2107 void GLAPIENTRY
2108 _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
2109 GLint* params)
2110 {
2111 GET_CURRENT_CONTEXT(ctx);
2112
2113 if (name == 0) {
2114 _mesa_error(ctx, GL_INVALID_VALUE,
2115 "glGetObjectParameteriv(name = 0x%x)", name);
2116 return;
2117 }
2118
2119 switch (objectType) {
2120 case GL_TEXTURE:
2121 _mesa_GetTextureObjectParameterivAPPLE (ctx, name, pname, params);
2122 break;
2123 case GL_BUFFER_OBJECT_APPLE:
2124 _mesa_GetBufferObjectParameterivAPPLE (ctx, name, pname, params);
2125 break;
2126 case GL_RENDERBUFFER_EXT:
2127 _mesa_GetRenderObjectParameterivAPPLE (ctx, name, pname, params);
2128 break;
2129 default:
2130 _mesa_error(ctx, GL_INVALID_ENUM,
2131 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2132 name, objectType);
2133 }
2134 }
2135
2136 #endif /* FEATURE_APPLE_object_purgeable */