Merge branch 'mesa_7_5_branch'
[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 "hash.h"
36 #include "imports.h"
37 #include "image.h"
38 #include "context.h"
39 #include "bufferobj.h"
40
41
42 /* Debug flags */
43 /*#define VBO_DEBUG*/
44 /*#define BOUNDS_CHECK*/
45
46
47 #ifdef FEATURE_OES_mapbuffer
48 #define DEFAULT_ACCESS GL_MAP_WRITE_BIT
49 #else
50 #define DEFAULT_ACCESS (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)
51 #endif
52
53
54 /**
55 * Get the buffer object bound to the specified target in a GL context.
56 *
57 * \param ctx GL context
58 * \param target Buffer object target to be retrieved. Currently this must
59 * be either \c GL_ARRAY_BUFFER or \c GL_ELEMENT_ARRAY_BUFFER.
60 * \return A pointer to the buffer object bound to \c target in the
61 * specified context or \c NULL if \c target is invalid.
62 */
63 static INLINE struct gl_buffer_object *
64 get_buffer(GLcontext *ctx, GLenum target)
65 {
66 struct gl_buffer_object * bufObj = NULL;
67
68 switch (target) {
69 case GL_ARRAY_BUFFER_ARB:
70 bufObj = ctx->Array.ArrayBufferObj;
71 break;
72 case GL_ELEMENT_ARRAY_BUFFER_ARB:
73 bufObj = ctx->Array.ElementArrayBufferObj;
74 break;
75 case GL_PIXEL_PACK_BUFFER_EXT:
76 bufObj = ctx->Pack.BufferObj;
77 break;
78 case GL_PIXEL_UNPACK_BUFFER_EXT:
79 bufObj = ctx->Unpack.BufferObj;
80 break;
81 case GL_COPY_READ_BUFFER:
82 if (ctx->Extensions.ARB_copy_buffer) {
83 bufObj = ctx->CopyReadBuffer;
84 }
85 break;
86 case GL_COPY_WRITE_BUFFER:
87 if (ctx->Extensions.ARB_copy_buffer) {
88 bufObj = ctx->CopyWriteBuffer;
89 }
90 break;
91 default:
92 /* error must be recorded by caller */
93 return NULL;
94 }
95
96 /* bufObj should point to NullBufferObj or a user-created buffer object */
97 ASSERT(bufObj);
98
99 return bufObj;
100 }
101
102
103 /**
104 * Convert a GLbitfield describing the mapped buffer access flags
105 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
106 */
107 static GLenum
108 simplified_access_mode(GLbitfield access)
109 {
110 const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
111 if ((access & rwFlags) == rwFlags)
112 return GL_READ_WRITE;
113 if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
114 return GL_READ_ONLY;
115 if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
116 return GL_WRITE_ONLY;
117 return GL_READ_WRITE; /* this should never happen, but no big deal */
118 }
119
120
121 /**
122 * Tests the subdata range parameters and sets the GL error code for
123 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
124 *
125 * \param ctx GL context.
126 * \param target Buffer object target on which to operate.
127 * \param offset Offset of the first byte of the subdata range.
128 * \param size Size, in bytes, of the subdata range.
129 * \param caller Name of calling function for recording errors.
130 * \return A pointer to the buffer object bound to \c target in the
131 * specified context or \c NULL if any of the parameter or state
132 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
133 * are invalid.
134 *
135 * \sa glBufferSubDataARB, glGetBufferSubDataARB
136 */
137 static struct gl_buffer_object *
138 buffer_object_subdata_range_good( GLcontext * ctx, GLenum target,
139 GLintptrARB offset, GLsizeiptrARB size,
140 const char *caller )
141 {
142 struct gl_buffer_object *bufObj;
143
144 if (size < 0) {
145 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
146 return NULL;
147 }
148
149 if (offset < 0) {
150 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
151 return NULL;
152 }
153
154 bufObj = get_buffer(ctx, target);
155 if (!bufObj) {
156 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", caller);
157 return NULL;
158 }
159 if (bufObj->Name == 0) {
160 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
161 return NULL;
162 }
163 if (offset + size > bufObj->Size) {
164 _mesa_error(ctx, GL_INVALID_VALUE,
165 "%s(size + offset > buffer size)", caller);
166 return NULL;
167 }
168 if (bufObj->Pointer) {
169 /* Buffer is currently mapped */
170 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
171 return NULL;
172 }
173
174 return bufObj;
175 }
176
177
178 /**
179 * Allocate and initialize a new buffer object.
180 *
181 * Default callback for the \c dd_function_table::NewBufferObject() hook.
182 */
183 static struct gl_buffer_object *
184 _mesa_new_buffer_object( GLcontext *ctx, GLuint name, GLenum target )
185 {
186 struct gl_buffer_object *obj;
187
188 (void) ctx;
189
190 obj = MALLOC_STRUCT(gl_buffer_object);
191 _mesa_initialize_buffer_object(obj, name, target);
192 return obj;
193 }
194
195
196 /**
197 * Delete a buffer object.
198 *
199 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
200 */
201 static void
202 _mesa_delete_buffer_object( GLcontext *ctx, struct gl_buffer_object *bufObj )
203 {
204 (void) ctx;
205
206 if (bufObj->Data)
207 _mesa_free(bufObj->Data);
208
209 /* assign strange values here to help w/ debugging */
210 bufObj->RefCount = -1000;
211 bufObj->Name = ~0;
212
213 _mesa_free(bufObj);
214 }
215
216
217
218 /**
219 * Set ptr to bufObj w/ reference counting.
220 */
221 void
222 _mesa_reference_buffer_object(GLcontext *ctx,
223 struct gl_buffer_object **ptr,
224 struct gl_buffer_object *bufObj)
225 {
226 if (*ptr == bufObj)
227 return;
228
229 if (*ptr) {
230 /* Unreference the old buffer */
231 GLboolean deleteFlag = GL_FALSE;
232 struct gl_buffer_object *oldObj = *ptr;
233
234 /*_glthread_LOCK_MUTEX(oldObj->Mutex);*/
235 ASSERT(oldObj->RefCount > 0);
236 oldObj->RefCount--;
237 #if 0
238 printf("BufferObj %p %d DECR to %d\n",
239 (void *) oldObj, oldObj->Name, oldObj->RefCount);
240 #endif
241 deleteFlag = (oldObj->RefCount == 0);
242 /*_glthread_UNLOCK_MUTEX(oldObj->Mutex);*/
243
244 if (deleteFlag) {
245
246 /* some sanity checking: don't delete a buffer still in use */
247 #if 0
248 /* unfortunately, these tests are invalid during context tear-down */
249 ASSERT(ctx->Array.ArrayBufferObj != bufObj);
250 ASSERT(ctx->Array.ElementArrayBufferObj != bufObj);
251 ASSERT(ctx->Array.ArrayObj->Vertex.BufferObj != bufObj);
252 #endif
253
254 ASSERT(ctx->Driver.DeleteBuffer);
255 ctx->Driver.DeleteBuffer(ctx, oldObj);
256 }
257
258 *ptr = NULL;
259 }
260 ASSERT(!*ptr);
261
262 if (bufObj) {
263 /* reference new buffer */
264 /*_glthread_LOCK_MUTEX(tex->Mutex);*/
265 if (bufObj->RefCount == 0) {
266 /* this buffer's being deleted (look just above) */
267 /* Not sure this can every really happen. Warn if it does. */
268 _mesa_problem(NULL, "referencing deleted buffer object");
269 *ptr = NULL;
270 }
271 else {
272 bufObj->RefCount++;
273 #if 0
274 printf("BufferObj %p %d INCR to %d\n",
275 (void *) bufObj, bufObj->Name, bufObj->RefCount);
276 #endif
277 *ptr = bufObj;
278 }
279 /*_glthread_UNLOCK_MUTEX(tex->Mutex);*/
280 }
281 }
282
283
284 /**
285 * Initialize a buffer object to default values.
286 */
287 void
288 _mesa_initialize_buffer_object( struct gl_buffer_object *obj,
289 GLuint name, GLenum target )
290 {
291 (void) target;
292
293 _mesa_bzero(obj, sizeof(struct gl_buffer_object));
294 obj->RefCount = 1;
295 obj->Name = name;
296 obj->Usage = GL_STATIC_DRAW_ARB;
297 obj->AccessFlags = DEFAULT_ACCESS;
298 }
299
300
301 /**
302 * Allocate space for and store data in a buffer object. Any data that was
303 * previously stored in the buffer object is lost. If \c data is \c NULL,
304 * memory will be allocated, but no copy will occur.
305 *
306 * This is the default callback for \c dd_function_table::BufferData()
307 * Note that all GL error checking will have been done already.
308 *
309 * \param ctx GL context.
310 * \param target Buffer object target on which to operate.
311 * \param size Size, in bytes, of the new data store.
312 * \param data Pointer to the data to store in the buffer object. This
313 * pointer may be \c NULL.
314 * \param usage Hints about how the data will be used.
315 * \param bufObj Object to be used.
316 *
317 * \sa glBufferDataARB, dd_function_table::BufferData.
318 */
319 static void
320 _mesa_buffer_data( GLcontext *ctx, GLenum target, GLsizeiptrARB size,
321 const GLvoid * data, GLenum usage,
322 struct gl_buffer_object * bufObj )
323 {
324 void * new_data;
325
326 (void) ctx; (void) target;
327
328 new_data = _mesa_realloc( bufObj->Data, bufObj->Size, size );
329 if (new_data) {
330 bufObj->Data = (GLubyte *) new_data;
331 bufObj->Size = size;
332 bufObj->Usage = usage;
333
334 if (data) {
335 _mesa_memcpy( bufObj->Data, data, size );
336 }
337 }
338 }
339
340
341 /**
342 * Replace data in a subrange of buffer object. If the data range
343 * specified by \c size + \c offset extends beyond the end of the buffer or
344 * if \c data is \c NULL, no copy is performed.
345 *
346 * This is the default callback for \c dd_function_table::BufferSubData()
347 * Note that all GL error checking will have been done already.
348 *
349 * \param ctx GL context.
350 * \param target Buffer object target on which to operate.
351 * \param offset Offset of the first byte to be modified.
352 * \param size Size, in bytes, of the data range.
353 * \param data Pointer to the data to store in the buffer object.
354 * \param bufObj Object to be used.
355 *
356 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
357 */
358 static void
359 _mesa_buffer_subdata( GLcontext *ctx, GLenum target, GLintptrARB offset,
360 GLsizeiptrARB size, const GLvoid * data,
361 struct gl_buffer_object * bufObj )
362 {
363 (void) ctx; (void) target;
364
365 /* this should have been caught in _mesa_BufferSubData() */
366 ASSERT(size + offset <= bufObj->Size);
367
368 if (bufObj->Data) {
369 _mesa_memcpy( (GLubyte *) bufObj->Data + offset, data, size );
370 }
371 }
372
373
374 /**
375 * Retrieve data from a subrange of buffer object. If the data range
376 * specified by \c size + \c offset extends beyond the end of the buffer or
377 * if \c data is \c NULL, no copy is performed.
378 *
379 * This is the default callback for \c dd_function_table::GetBufferSubData()
380 * Note that all GL error checking will have been done already.
381 *
382 * \param ctx GL context.
383 * \param target Buffer object target on which to operate.
384 * \param offset Offset of the first byte to be fetched.
385 * \param size Size, in bytes, of the data range.
386 * \param data Destination for data
387 * \param bufObj Object to be used.
388 *
389 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
390 */
391 static void
392 _mesa_buffer_get_subdata( GLcontext *ctx, GLenum target, GLintptrARB offset,
393 GLsizeiptrARB size, GLvoid * data,
394 struct gl_buffer_object * bufObj )
395 {
396 (void) ctx; (void) target;
397
398 if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
399 _mesa_memcpy( data, (GLubyte *) bufObj->Data + offset, size );
400 }
401 }
402
403
404 /**
405 * Default callback for \c dd_function_tabel::MapBuffer().
406 *
407 * The function parameters will have been already tested for errors.
408 *
409 * \param ctx GL context.
410 * \param target Buffer object target on which to operate.
411 * \param access Information about how the buffer will be accessed.
412 * \param bufObj Object to be mapped.
413 * \return A pointer to the object's internal data store that can be accessed
414 * by the processor
415 *
416 * \sa glMapBufferARB, dd_function_table::MapBuffer
417 */
418 static void *
419 _mesa_buffer_map( GLcontext *ctx, GLenum target, GLenum access,
420 struct gl_buffer_object *bufObj )
421 {
422 (void) ctx;
423 (void) target;
424 (void) access;
425 /* Just return a direct pointer to the data */
426 if (bufObj->Pointer) {
427 /* already mapped! */
428 return NULL;
429 }
430 bufObj->Pointer = bufObj->Data;
431 return bufObj->Pointer;
432 }
433
434
435 /**
436 * Default fallback for \c dd_function_table::MapBufferRange().
437 * Called via glMapBufferRange().
438 */
439 static void *
440 _mesa_buffer_map_range( GLcontext *ctx, GLenum target, GLintptr offset,
441 GLsizeiptr length, GLbitfield access,
442 struct gl_buffer_object *bufObj )
443 {
444 (void) ctx;
445 (void) target;
446 (void) access;
447 (void) length;
448 assert(!bufObj->Pointer);
449 /* Just return a direct pointer to the data */
450 return bufObj->Data + offset;
451 }
452
453
454 /**
455 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
456 * Called via glFlushMappedBufferRange().
457 */
458 static void
459 _mesa_buffer_flush_mapped_range( GLcontext *ctx, GLenum target,
460 GLintptr offset, GLsizeiptr length,
461 struct gl_buffer_object *obj )
462 {
463 (void) ctx;
464 (void) target;
465 (void) offset;
466 (void) length;
467 (void) obj;
468 /* no-op */
469 }
470
471
472 /**
473 * Default callback for \c dd_function_table::MapBuffer().
474 *
475 * The input parameters will have been already tested for errors.
476 *
477 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
478 */
479 static GLboolean
480 _mesa_buffer_unmap( GLcontext *ctx, GLenum target,
481 struct gl_buffer_object *bufObj )
482 {
483 (void) ctx;
484 (void) target;
485 /* XXX we might assert here that bufObj->Pointer is non-null */
486 bufObj->Pointer = NULL;
487 return GL_TRUE;
488 }
489
490
491 /**
492 * Default fallback for \c dd_function_table::CopyBufferSubData().
493 * Called via glCopyBuffserSubData().
494 */
495 static void
496 _mesa_copy_buffer_subdata(GLcontext *ctx,
497 struct gl_buffer_object *src,
498 struct gl_buffer_object *dst,
499 GLintptr readOffset, GLintptr writeOffset,
500 GLsizeiptr size)
501 {
502 GLubyte *srcPtr, *dstPtr;
503
504 /* buffer should not already be mapped */
505 assert(!src->Pointer);
506 assert(!dst->Pointer);
507
508 srcPtr = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_COPY_READ_BUFFER,
509 GL_READ_ONLY, src);
510 dstPtr = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_COPY_WRITE_BUFFER,
511 GL_WRITE_ONLY, dst);
512
513 if (srcPtr && dstPtr)
514 _mesa_memcpy(dstPtr + writeOffset, srcPtr + readOffset, size);
515
516 ctx->Driver.UnmapBuffer(ctx, GL_COPY_READ_BUFFER, src);
517 ctx->Driver.UnmapBuffer(ctx, GL_COPY_WRITE_BUFFER, dst);
518 }
519
520
521
522 /**
523 * Initialize the state associated with buffer objects
524 */
525 void
526 _mesa_init_buffer_objects( GLcontext *ctx )
527 {
528 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj,
529 ctx->Shared->NullBufferObj);
530 _mesa_reference_buffer_object(ctx, &ctx->Array.ElementArrayBufferObj,
531 ctx->Shared->NullBufferObj);
532
533 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer,
534 ctx->Shared->NullBufferObj);
535 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
536 ctx->Shared->NullBufferObj);
537 }
538
539
540 /**
541 * Bind the specified target to buffer for the specified context.
542 */
543 static void
544 bind_buffer_object(GLcontext *ctx, GLenum target, GLuint buffer)
545 {
546 struct gl_buffer_object *oldBufObj;
547 struct gl_buffer_object *newBufObj = NULL;
548 struct gl_buffer_object **bindTarget = NULL;
549
550 switch (target) {
551 case GL_ARRAY_BUFFER_ARB:
552 bindTarget = &ctx->Array.ArrayBufferObj;
553 break;
554 case GL_ELEMENT_ARRAY_BUFFER_ARB:
555 bindTarget = &ctx->Array.ElementArrayBufferObj;
556 break;
557 case GL_PIXEL_PACK_BUFFER_EXT:
558 bindTarget = &ctx->Pack.BufferObj;
559 break;
560 case GL_PIXEL_UNPACK_BUFFER_EXT:
561 bindTarget = &ctx->Unpack.BufferObj;
562 break;
563 case GL_COPY_READ_BUFFER:
564 if (ctx->Extensions.ARB_copy_buffer) {
565 bindTarget = &ctx->CopyReadBuffer;
566 }
567 break;
568 case GL_COPY_WRITE_BUFFER:
569 if (ctx->Extensions.ARB_copy_buffer) {
570 bindTarget = &ctx->CopyWriteBuffer;
571 }
572 break;
573 default:
574 ; /* no-op / we'll hit the follow error test next */
575 }
576
577 if (!bindTarget) {
578 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)");
579 return;
580 }
581
582 /* Get pointer to old buffer object (to be unbound) */
583 oldBufObj = get_buffer(ctx, target);
584 if (oldBufObj && oldBufObj->Name == buffer)
585 return; /* rebinding the same buffer object- no change */
586
587 /*
588 * Get pointer to new buffer object (newBufObj)
589 */
590 if (buffer == 0) {
591 /* The spec says there's not a buffer object named 0, but we use
592 * one internally because it simplifies things.
593 */
594 newBufObj = ctx->Shared->NullBufferObj;
595 }
596 else {
597 /* non-default buffer object */
598 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
599 if (!newBufObj) {
600 /* if this is a new buffer object id, allocate a buffer object now */
601 ASSERT(ctx->Driver.NewBufferObject);
602 newBufObj = ctx->Driver.NewBufferObject(ctx, buffer, target);
603 if (!newBufObj) {
604 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindBufferARB");
605 return;
606 }
607 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, newBufObj);
608 }
609 }
610
611 /* bind new buffer */
612 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
613
614 /* Pass BindBuffer call to device driver */
615 if (ctx->Driver.BindBuffer)
616 ctx->Driver.BindBuffer( ctx, target, newBufObj );
617 }
618
619
620 /**
621 * Update the default buffer objects in the given context to reference those
622 * specified in the shared state and release those referencing the old
623 * shared state.
624 */
625 void
626 _mesa_update_default_objects_buffer_objects(GLcontext *ctx)
627 {
628 /* Bind the NullBufferObj to remove references to those
629 * in the shared context hash table.
630 */
631 bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
632 bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
633 bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
634 bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
635 }
636
637
638 /**
639 * When we're about to read pixel data out of a PBO (via glDrawPixels,
640 * glTexImage, etc) or write data into a PBO (via glReadPixels,
641 * glGetTexImage, etc) we call this function to check that we're not
642 * going to read out of bounds.
643 *
644 * XXX This would also be a convenient time to check that the PBO isn't
645 * currently mapped. Whoever calls this function should check for that.
646 * Remember, we can't use a PBO when it's mapped!
647 *
648 * \param width width of image to read/write
649 * \param height height of image to read/write
650 * \param depth depth of image to read/write
651 * \param format format of image to read/write
652 * \param type datatype of image to read/write
653 * \param ptr the user-provided pointer/offset
654 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
655 * go out of bounds.
656 */
657 GLboolean
658 _mesa_validate_pbo_access(GLuint dimensions,
659 const struct gl_pixelstore_attrib *pack,
660 GLsizei width, GLsizei height, GLsizei depth,
661 GLenum format, GLenum type, const GLvoid *ptr)
662 {
663 GLvoid *start, *end;
664 const GLubyte *sizeAddr; /* buffer size, cast to a pointer */
665
666 ASSERT(pack->BufferObj->Name != 0);
667
668 if (pack->BufferObj->Size == 0)
669 /* no buffer! */
670 return GL_FALSE;
671
672 /* get address of first pixel we'll read */
673 start = _mesa_image_address(dimensions, pack, ptr, width, height,
674 format, type, 0, 0, 0);
675
676 /* get address just past the last pixel we'll read */
677 end = _mesa_image_address(dimensions, pack, ptr, width, height,
678 format, type, depth-1, height-1, width);
679
680
681 sizeAddr = ((const GLubyte *) 0) + pack->BufferObj->Size;
682
683 if ((const GLubyte *) start > sizeAddr) {
684 /* This will catch negative values / wrap-around */
685 return GL_FALSE;
686 }
687 if ((const GLubyte *) end > sizeAddr) {
688 /* Image read goes beyond end of buffer */
689 return GL_FALSE;
690 }
691
692 /* OK! */
693 return GL_TRUE;
694 }
695
696
697 /**
698 * If the source of glBitmap data is a PBO, check that we won't read out
699 * of buffer bounds, then map the buffer.
700 * If not sourcing from a PBO, just return the bitmap pointer.
701 * This is a helper function for (some) drivers.
702 * Return NULL if error.
703 * If non-null return, must call _mesa_unmap_bitmap_pbo() when done.
704 */
705 const GLubyte *
706 _mesa_map_bitmap_pbo(GLcontext *ctx,
707 const struct gl_pixelstore_attrib *unpack,
708 const GLubyte *bitmap)
709 {
710 const GLubyte *buf;
711
712 if (unpack->BufferObj->Name) {
713 /* unpack from PBO */
714 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
715 GL_READ_ONLY_ARB,
716 unpack->BufferObj);
717 if (!buf)
718 return NULL;
719
720 buf = ADD_POINTERS(buf, bitmap);
721 }
722 else {
723 /* unpack from normal memory */
724 buf = bitmap;
725 }
726
727 return buf;
728 }
729
730
731 /**
732 * Counterpart to _mesa_map_bitmap_pbo()
733 * This is a helper function for (some) drivers.
734 */
735 void
736 _mesa_unmap_bitmap_pbo(GLcontext *ctx,
737 const struct gl_pixelstore_attrib *unpack)
738 {
739 if (unpack->BufferObj->Name) {
740 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
741 unpack->BufferObj);
742 }
743 }
744
745
746 /**
747 * \sa _mesa_map_bitmap_pbo
748 */
749 const GLvoid *
750 _mesa_map_drawpix_pbo(GLcontext *ctx,
751 const struct gl_pixelstore_attrib *unpack,
752 const GLvoid *pixels)
753 {
754 const GLvoid *buf;
755
756 if (unpack->BufferObj->Name) {
757 /* unpack from PBO */
758 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
759 GL_READ_ONLY_ARB,
760 unpack->BufferObj);
761 if (!buf)
762 return NULL;
763
764 buf = ADD_POINTERS(buf, pixels);
765 }
766 else {
767 /* unpack from normal memory */
768 buf = pixels;
769 }
770
771 return buf;
772 }
773
774
775 /**
776 * \sa _mesa_unmap_bitmap_pbo
777 */
778 void
779 _mesa_unmap_drawpix_pbo(GLcontext *ctx,
780 const struct gl_pixelstore_attrib *unpack)
781 {
782 if (unpack->BufferObj->Name) {
783 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
784 unpack->BufferObj);
785 }
786 }
787
788
789 /**
790 * If PBO is bound, map the buffer, return dest pointer in mapped buffer.
791 * Call _mesa_unmap_readpix_pbo() when finished
792 * \return NULL if error
793 */
794 void *
795 _mesa_map_readpix_pbo(GLcontext *ctx,
796 const struct gl_pixelstore_attrib *pack,
797 GLvoid *dest)
798 {
799 void *buf;
800
801 if (pack->BufferObj->Name) {
802 /* pack into PBO */
803 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
804 GL_WRITE_ONLY_ARB,
805 pack->BufferObj);
806 if (!buf)
807 return NULL;
808
809 buf = ADD_POINTERS(buf, dest);
810 }
811 else {
812 /* pack to normal memory */
813 buf = dest;
814 }
815
816 return buf;
817 }
818
819
820 /**
821 * Counterpart to _mesa_map_readpix_pbo()
822 */
823 void
824 _mesa_unmap_readpix_pbo(GLcontext *ctx,
825 const struct gl_pixelstore_attrib *pack)
826 {
827 if (pack->BufferObj->Name) {
828 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT, pack->BufferObj);
829 }
830 }
831
832
833
834 /**
835 * Return the gl_buffer_object for the given ID.
836 * Always return NULL for ID 0.
837 */
838 struct gl_buffer_object *
839 _mesa_lookup_bufferobj(GLcontext *ctx, GLuint buffer)
840 {
841 if (buffer == 0)
842 return NULL;
843 else
844 return (struct gl_buffer_object *)
845 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
846 }
847
848
849 /**
850 * If *ptr points to obj, set ptr = the Null/default buffer object.
851 * This is a helper for buffer object deletion.
852 * The GL spec says that deleting a buffer object causes it to get
853 * unbound from all arrays in the current context.
854 */
855 static void
856 unbind(GLcontext *ctx,
857 struct gl_buffer_object **ptr,
858 struct gl_buffer_object *obj)
859 {
860 if (*ptr == obj) {
861 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
862 }
863 }
864
865
866 /**
867 * Plug default/fallback buffer object functions into the device
868 * driver hooks.
869 */
870 void
871 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
872 {
873 /* GL_ARB_vertex/pixel_buffer_object */
874 driver->NewBufferObject = _mesa_new_buffer_object;
875 driver->DeleteBuffer = _mesa_delete_buffer_object;
876 driver->BindBuffer = NULL;
877 driver->BufferData = _mesa_buffer_data;
878 driver->BufferSubData = _mesa_buffer_subdata;
879 driver->GetBufferSubData = _mesa_buffer_get_subdata;
880 driver->MapBuffer = _mesa_buffer_map;
881 driver->UnmapBuffer = _mesa_buffer_unmap;
882
883 /* GL_ARB_map_buffer_range */
884 driver->MapBufferRange = _mesa_buffer_map_range;
885 driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
886
887 /* GL_ARB_copy_buffer */
888 driver->CopyBufferSubData = _mesa_copy_buffer_subdata;
889 }
890
891
892
893 /**********************************************************************/
894 /* API Functions */
895 /**********************************************************************/
896
897 void GLAPIENTRY
898 _mesa_BindBufferARB(GLenum target, GLuint buffer)
899 {
900 GET_CURRENT_CONTEXT(ctx);
901 ASSERT_OUTSIDE_BEGIN_END(ctx);
902
903 bind_buffer_object(ctx, target, buffer);
904 }
905
906
907 /**
908 * Delete a set of buffer objects.
909 *
910 * \param n Number of buffer objects to delete.
911 * \param ids Array of \c n buffer object IDs.
912 */
913 void GLAPIENTRY
914 _mesa_DeleteBuffersARB(GLsizei n, const GLuint *ids)
915 {
916 GET_CURRENT_CONTEXT(ctx);
917 GLsizei i;
918 ASSERT_OUTSIDE_BEGIN_END(ctx);
919
920 if (n < 0) {
921 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
922 return;
923 }
924
925 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
926
927 for (i = 0; i < n; i++) {
928 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
929 if (bufObj) {
930 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
931 GLuint j;
932
933 ASSERT(bufObj->Name == ids[i]);
934
935 if (bufObj->Pointer) {
936 /* if mapped, unmap it now */
937 ctx->Driver.UnmapBuffer(ctx, 0, bufObj);
938 bufObj->AccessFlags = DEFAULT_ACCESS;
939 bufObj->Pointer = NULL;
940 }
941
942 /* unbind any vertex pointers bound to this buffer */
943 unbind(ctx, &arrayObj->Vertex.BufferObj, bufObj);
944 unbind(ctx, &arrayObj->Weight.BufferObj, bufObj);
945 unbind(ctx, &arrayObj->Normal.BufferObj, bufObj);
946 unbind(ctx, &arrayObj->Color.BufferObj, bufObj);
947 unbind(ctx, &arrayObj->SecondaryColor.BufferObj, bufObj);
948 unbind(ctx, &arrayObj->FogCoord.BufferObj, bufObj);
949 unbind(ctx, &arrayObj->Index.BufferObj, bufObj);
950 unbind(ctx, &arrayObj->EdgeFlag.BufferObj, bufObj);
951 for (j = 0; j < Elements(arrayObj->TexCoord); j++) {
952 unbind(ctx, &arrayObj->TexCoord[j].BufferObj, bufObj);
953 }
954 for (j = 0; j < Elements(arrayObj->VertexAttrib); j++) {
955 unbind(ctx, &arrayObj->VertexAttrib[j].BufferObj, bufObj);
956 }
957
958 if (ctx->Array.ArrayBufferObj == bufObj) {
959 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
960 }
961 if (ctx->Array.ElementArrayBufferObj == bufObj) {
962 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
963 }
964
965 /* unbind any pixel pack/unpack pointers bound to this buffer */
966 if (ctx->Pack.BufferObj == bufObj) {
967 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 );
968 }
969 if (ctx->Unpack.BufferObj == bufObj) {
970 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
971 }
972
973 /* The ID is immediately freed for re-use */
974 _mesa_HashRemove(ctx->Shared->BufferObjects, bufObj->Name);
975 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
976 }
977 }
978
979 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
980 }
981
982
983 /**
984 * Generate a set of unique buffer object IDs and store them in \c buffer.
985 *
986 * \param n Number of IDs to generate.
987 * \param buffer Array of \c n locations to store the IDs.
988 */
989 void GLAPIENTRY
990 _mesa_GenBuffersARB(GLsizei n, GLuint *buffer)
991 {
992 GET_CURRENT_CONTEXT(ctx);
993 GLuint first;
994 GLint i;
995 ASSERT_OUTSIDE_BEGIN_END(ctx);
996
997 if (n < 0) {
998 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
999 return;
1000 }
1001
1002 if (!buffer) {
1003 return;
1004 }
1005
1006 /*
1007 * This must be atomic (generation and allocation of buffer object IDs)
1008 */
1009 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1010
1011 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
1012
1013 /* Allocate new, empty buffer objects and return identifiers */
1014 for (i = 0; i < n; i++) {
1015 struct gl_buffer_object *bufObj;
1016 GLuint name = first + i;
1017 GLenum target = 0;
1018 bufObj = ctx->Driver.NewBufferObject( ctx, name, target );
1019 if (!bufObj) {
1020 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1021 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenBuffersARB");
1022 return;
1023 }
1024 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i, bufObj);
1025 buffer[i] = first + i;
1026 }
1027
1028 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1029 }
1030
1031
1032 /**
1033 * Determine if ID is the name of a buffer object.
1034 *
1035 * \param id ID of the potential buffer object.
1036 * \return \c GL_TRUE if \c id is the name of a buffer object,
1037 * \c GL_FALSE otherwise.
1038 */
1039 GLboolean GLAPIENTRY
1040 _mesa_IsBufferARB(GLuint id)
1041 {
1042 struct gl_buffer_object *bufObj;
1043 GET_CURRENT_CONTEXT(ctx);
1044 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1045
1046 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1047 bufObj = _mesa_lookup_bufferobj(ctx, id);
1048 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1049
1050 return bufObj ? GL_TRUE : GL_FALSE;
1051 }
1052
1053
1054 void GLAPIENTRY
1055 _mesa_BufferDataARB(GLenum target, GLsizeiptrARB size,
1056 const GLvoid * data, GLenum usage)
1057 {
1058 GET_CURRENT_CONTEXT(ctx);
1059 struct gl_buffer_object *bufObj;
1060 ASSERT_OUTSIDE_BEGIN_END(ctx);
1061
1062 if (size < 0) {
1063 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
1064 return;
1065 }
1066
1067 switch (usage) {
1068 case GL_STREAM_DRAW_ARB:
1069 case GL_STREAM_READ_ARB:
1070 case GL_STREAM_COPY_ARB:
1071 case GL_STATIC_DRAW_ARB:
1072 case GL_STATIC_READ_ARB:
1073 case GL_STATIC_COPY_ARB:
1074 case GL_DYNAMIC_DRAW_ARB:
1075 case GL_DYNAMIC_READ_ARB:
1076 case GL_DYNAMIC_COPY_ARB:
1077 /* OK */
1078 break;
1079 default:
1080 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(usage)");
1081 return;
1082 }
1083
1084 bufObj = get_buffer(ctx, target);
1085 if (!bufObj) {
1086 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(target)" );
1087 return;
1088 }
1089 if (bufObj->Name == 0) {
1090 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferDataARB(buffer 0)" );
1091 return;
1092 }
1093
1094 if (bufObj->Pointer) {
1095 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1096 ctx->Driver.UnmapBuffer(ctx, target, bufObj);
1097 bufObj->AccessFlags = DEFAULT_ACCESS;
1098 bufObj->Pointer = NULL;
1099 }
1100
1101 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1102
1103 ASSERT(ctx->Driver.BufferData);
1104
1105 bufObj->Written = GL_TRUE;
1106
1107 #ifdef VBO_DEBUG
1108 _mesa_printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1109 bufObj->Name, size, data, usage);
1110 #endif
1111
1112 #ifdef BOUNDS_CHECK
1113 size += 100;
1114 #endif
1115 /* Give the buffer object to the driver! <data> may be null! */
1116 ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj );
1117 }
1118
1119
1120 void GLAPIENTRY
1121 _mesa_BufferSubDataARB(GLenum target, GLintptrARB offset,
1122 GLsizeiptrARB size, const GLvoid * data)
1123 {
1124 GET_CURRENT_CONTEXT(ctx);
1125 struct gl_buffer_object *bufObj;
1126 ASSERT_OUTSIDE_BEGIN_END(ctx);
1127
1128 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1129 "glBufferSubDataARB" );
1130 if (!bufObj) {
1131 /* error already recorded */
1132 return;
1133 }
1134
1135 bufObj->Written = GL_TRUE;
1136
1137 ASSERT(ctx->Driver.BufferSubData);
1138 ctx->Driver.BufferSubData( ctx, target, offset, size, data, bufObj );
1139 }
1140
1141
1142 void GLAPIENTRY
1143 _mesa_GetBufferSubDataARB(GLenum target, GLintptrARB offset,
1144 GLsizeiptrARB size, void * data)
1145 {
1146 GET_CURRENT_CONTEXT(ctx);
1147 struct gl_buffer_object *bufObj;
1148 ASSERT_OUTSIDE_BEGIN_END(ctx);
1149
1150 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1151 "glGetBufferSubDataARB" );
1152 if (!bufObj) {
1153 /* error already recorded */
1154 return;
1155 }
1156
1157 ASSERT(ctx->Driver.GetBufferSubData);
1158 ctx->Driver.GetBufferSubData( ctx, target, offset, size, data, bufObj );
1159 }
1160
1161
1162 void * GLAPIENTRY
1163 _mesa_MapBufferARB(GLenum target, GLenum access)
1164 {
1165 GET_CURRENT_CONTEXT(ctx);
1166 struct gl_buffer_object * bufObj;
1167 GLbitfield accessFlags;
1168 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1169
1170 switch (access) {
1171 case GL_READ_ONLY_ARB:
1172 accessFlags = GL_MAP_READ_BIT;
1173 break;
1174 case GL_WRITE_ONLY_ARB:
1175 accessFlags = GL_MAP_WRITE_BIT;
1176 break;
1177 case GL_READ_WRITE_ARB:
1178 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1179 break;
1180 default:
1181 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
1182 return NULL;
1183 }
1184
1185 bufObj = get_buffer(ctx, target);
1186 if (!bufObj) {
1187 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(target)" );
1188 return NULL;
1189 }
1190 if (bufObj->Name == 0) {
1191 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(buffer 0)" );
1192 return NULL;
1193 }
1194 if (bufObj->Pointer) {
1195 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
1196 return NULL;
1197 }
1198
1199 ASSERT(ctx->Driver.MapBuffer);
1200 bufObj->Pointer = ctx->Driver.MapBuffer( ctx, target, access, bufObj );
1201 if (!bufObj->Pointer) {
1202 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(access)");
1203 }
1204
1205 bufObj->AccessFlags = accessFlags;
1206 bufObj->Offset = 0;
1207 bufObj->Length = bufObj->Size;
1208
1209 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
1210 bufObj->Written = GL_TRUE;
1211
1212 #ifdef VBO_DEBUG
1213 _mesa_printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1214 bufObj->Name, bufObj->Size, access);
1215 if (access == GL_WRITE_ONLY_ARB) {
1216 GLuint i;
1217 GLubyte *b = (GLubyte *) bufObj->Pointer;
1218 for (i = 0; i < bufObj->Size; i++)
1219 b[i] = i & 0xff;
1220 }
1221 #endif
1222
1223 #ifdef BOUNDS_CHECK
1224 {
1225 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1226 GLuint i;
1227 /* buffer is 100 bytes larger than requested, fill with magic value */
1228 for (i = 0; i < 100; i++) {
1229 buf[bufObj->Size - i - 1] = 123;
1230 }
1231 }
1232 #endif
1233
1234 return bufObj->Pointer;
1235 }
1236
1237
1238 GLboolean GLAPIENTRY
1239 _mesa_UnmapBufferARB(GLenum target)
1240 {
1241 GET_CURRENT_CONTEXT(ctx);
1242 struct gl_buffer_object *bufObj;
1243 GLboolean status = GL_TRUE;
1244 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1245
1246 bufObj = get_buffer(ctx, target);
1247 if (!bufObj) {
1248 _mesa_error(ctx, GL_INVALID_ENUM, "glUnmapBufferARB(target)" );
1249 return GL_FALSE;
1250 }
1251 if (bufObj->Name == 0) {
1252 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB" );
1253 return GL_FALSE;
1254 }
1255 if (!bufObj->Pointer) {
1256 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1257 return GL_FALSE;
1258 }
1259
1260 #ifdef BOUNDS_CHECK
1261 if (bufObj->Access != GL_READ_ONLY_ARB) {
1262 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1263 GLuint i;
1264 /* check that last 100 bytes are still = magic value */
1265 for (i = 0; i < 100; i++) {
1266 GLuint pos = bufObj->Size - i - 1;
1267 if (buf[pos] != 123) {
1268 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1269 " at position %d (value = %u)\n",
1270 pos, buf[pos]);
1271 }
1272 }
1273 }
1274 #endif
1275
1276 #ifdef VBO_DEBUG
1277 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1278 GLuint i, unchanged = 0;
1279 GLubyte *b = (GLubyte *) bufObj->Pointer;
1280 GLint pos = -1;
1281 /* check which bytes changed */
1282 for (i = 0; i < bufObj->Size - 1; i++) {
1283 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1284 unchanged++;
1285 if (pos == -1)
1286 pos = i;
1287 }
1288 }
1289 if (unchanged) {
1290 _mesa_printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1291 bufObj->Name, unchanged, bufObj->Size, pos);
1292 }
1293 }
1294 #endif
1295
1296 status = ctx->Driver.UnmapBuffer( ctx, target, bufObj );
1297 bufObj->AccessFlags = DEFAULT_ACCESS;
1298 bufObj->Pointer = NULL;
1299 bufObj->Offset = 0;
1300 bufObj->Length = 0;
1301
1302 return status;
1303 }
1304
1305
1306 void GLAPIENTRY
1307 _mesa_GetBufferParameterivARB(GLenum target, GLenum pname, GLint *params)
1308 {
1309 GET_CURRENT_CONTEXT(ctx);
1310 struct gl_buffer_object *bufObj;
1311 ASSERT_OUTSIDE_BEGIN_END(ctx);
1312
1313 bufObj = get_buffer(ctx, target);
1314 if (!bufObj) {
1315 _mesa_error(ctx, GL_INVALID_ENUM, "GetBufferParameterivARB(target)" );
1316 return;
1317 }
1318 if (bufObj->Name == 0) {
1319 _mesa_error(ctx, GL_INVALID_OPERATION, "GetBufferParameterivARB" );
1320 return;
1321 }
1322
1323 switch (pname) {
1324 case GL_BUFFER_SIZE_ARB:
1325 *params = (GLint) bufObj->Size;
1326 break;
1327 case GL_BUFFER_USAGE_ARB:
1328 *params = bufObj->Usage;
1329 break;
1330 case GL_BUFFER_ACCESS_ARB:
1331 *params = simplified_access_mode(bufObj->AccessFlags);
1332 break;
1333 case GL_BUFFER_MAPPED_ARB:
1334 *params = (bufObj->Pointer != NULL);
1335 break;
1336 default:
1337 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname)");
1338 return;
1339 }
1340 }
1341
1342
1343 void GLAPIENTRY
1344 _mesa_GetBufferPointervARB(GLenum target, GLenum pname, GLvoid **params)
1345 {
1346 GET_CURRENT_CONTEXT(ctx);
1347 struct gl_buffer_object * bufObj;
1348 ASSERT_OUTSIDE_BEGIN_END(ctx);
1349
1350 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1351 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1352 return;
1353 }
1354
1355 bufObj = get_buffer(ctx, target);
1356 if (!bufObj) {
1357 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(target)" );
1358 return;
1359 }
1360 if (bufObj->Name == 0) {
1361 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferPointervARB" );
1362 return;
1363 }
1364
1365 *params = bufObj->Pointer;
1366 }
1367
1368
1369 void GLAPIENTRY
1370 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
1371 GLintptr readOffset, GLintptr writeOffset,
1372 GLsizeiptr size)
1373 {
1374 GET_CURRENT_CONTEXT(ctx);
1375 struct gl_buffer_object *src, *dst;
1376 ASSERT_OUTSIDE_BEGIN_END(ctx);
1377
1378 src = get_buffer(ctx, readTarget);
1379 if (!src || src->Name == 0) {
1380 _mesa_error(ctx, GL_INVALID_ENUM,
1381 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget);
1382 return;
1383 }
1384
1385 dst = get_buffer(ctx, writeTarget);
1386 if (!dst || dst->Name == 0) {
1387 _mesa_error(ctx, GL_INVALID_ENUM,
1388 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget);
1389 return;
1390 }
1391
1392 if (src->Pointer) {
1393 _mesa_error(ctx, GL_INVALID_OPERATION,
1394 "glCopyBuffserSubData(readBuffer is mapped)");
1395 return;
1396 }
1397
1398 if (dst->Pointer) {
1399 _mesa_error(ctx, GL_INVALID_OPERATION,
1400 "glCopyBuffserSubData(writeBuffer is mapped)");
1401 return;
1402 }
1403
1404 if (readOffset < 0) {
1405 _mesa_error(ctx, GL_INVALID_VALUE,
1406 "glCopyBuffserSubData(readOffset = %d)", readOffset);
1407 return;
1408 }
1409
1410 if (writeOffset < 0) {
1411 _mesa_error(ctx, GL_INVALID_VALUE,
1412 "glCopyBuffserSubData(writeOffset = %d)", writeOffset);
1413 return;
1414 }
1415
1416 if (readOffset + size > src->Size) {
1417 _mesa_error(ctx, GL_INVALID_VALUE,
1418 "glCopyBuffserSubData(readOffset + size = %d)",
1419 readOffset, size);
1420 return;
1421 }
1422
1423 if (writeOffset + size > dst->Size) {
1424 _mesa_error(ctx, GL_INVALID_VALUE,
1425 "glCopyBuffserSubData(writeOffset + size = %d)",
1426 writeOffset, size);
1427 return;
1428 }
1429
1430 if (src == dst) {
1431 if (readOffset + size <= writeOffset) {
1432 /* OK */
1433 }
1434 else if (writeOffset + size <= readOffset) {
1435 /* OK */
1436 }
1437 else {
1438 /* overlapping src/dst is illegal */
1439 _mesa_error(ctx, GL_INVALID_VALUE,
1440 "glCopyBuffserSubData(overlapping src/dst)");
1441 return;
1442 }
1443 }
1444
1445 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
1446 }
1447
1448
1449 /**
1450 * See GL_ARB_map_buffer_range spec
1451 */
1452 void * GLAPIENTRY
1453 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
1454 GLbitfield access)
1455 {
1456 GET_CURRENT_CONTEXT(ctx);
1457 struct gl_buffer_object *bufObj;
1458 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1459
1460 if (!ctx->Extensions.ARB_map_buffer_range) {
1461 _mesa_error(ctx, GL_INVALID_OPERATION,
1462 "glMapBufferRange(extension not supported)");
1463 return NULL;
1464 }
1465
1466 if (offset < 0) {
1467 _mesa_error(ctx, GL_INVALID_VALUE,
1468 "glMapBufferRange(offset = %ld)", offset);
1469 return NULL;
1470 }
1471
1472 if (length < 0) {
1473 _mesa_error(ctx, GL_INVALID_VALUE,
1474 "glMapBufferRange(length = %ld)", length);
1475 return NULL;
1476 }
1477
1478 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
1479 _mesa_error(ctx, GL_INVALID_OPERATION,
1480 "glMapBufferRange(access indicates neither read or write)");
1481 return NULL;
1482 }
1483
1484 if (access & GL_MAP_READ_BIT) {
1485 if ((access & GL_MAP_INVALIDATE_RANGE_BIT) ||
1486 (access & GL_MAP_INVALIDATE_BUFFER_BIT) ||
1487 (access & GL_MAP_UNSYNCHRONIZED_BIT)) {
1488 _mesa_error(ctx, GL_INVALID_OPERATION,
1489 "glMapBufferRange(invalid access flags)");
1490 return NULL;
1491 }
1492 }
1493
1494 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
1495 ((access & GL_MAP_WRITE_BIT) == 0)) {
1496 _mesa_error(ctx, GL_INVALID_OPERATION,
1497 "glMapBufferRange(invalid access flags)");
1498 return NULL;
1499 }
1500
1501 bufObj = get_buffer(ctx, target);
1502 if (!bufObj || bufObj->Name == 0) {
1503 _mesa_error(ctx, GL_INVALID_ENUM,
1504 "glMapBufferRange(target = 0x%x)", target);
1505 return NULL;
1506 }
1507
1508 if (offset + length > bufObj->Size) {
1509 _mesa_error(ctx, GL_INVALID_VALUE,
1510 "glMapBufferRange(offset + length > size)");
1511 return NULL;
1512 }
1513
1514 if (bufObj->Pointer) {
1515 _mesa_error(ctx, GL_INVALID_OPERATION,
1516 "glMapBufferRange(buffer already mapped)");
1517 return NULL;
1518 }
1519
1520 ASSERT(ctx->Driver.MapBufferRange);
1521 bufObj->Pointer = ctx->Driver.MapBufferRange(ctx, target, offset, length,
1522 access, bufObj);
1523
1524 bufObj->Offset = offset;
1525 bufObj->Length = length;
1526 bufObj->AccessFlags = access;
1527
1528 return bufObj->Pointer;
1529 }
1530
1531
1532 /**
1533 * See GL_ARB_map_buffer_range spec
1534 */
1535 void GLAPIENTRY
1536 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
1537 {
1538 GET_CURRENT_CONTEXT(ctx);
1539 struct gl_buffer_object *bufObj;
1540 ASSERT_OUTSIDE_BEGIN_END(ctx);
1541
1542 if (!ctx->Extensions.ARB_map_buffer_range) {
1543 _mesa_error(ctx, GL_INVALID_OPERATION,
1544 "glMapBufferRange(extension not supported)");
1545 return;
1546 }
1547
1548 if (offset < 0) {
1549 _mesa_error(ctx, GL_INVALID_VALUE,
1550 "glMapBufferRange(offset = %ld)", offset);
1551 return;
1552 }
1553
1554 if (length < 0) {
1555 _mesa_error(ctx, GL_INVALID_VALUE,
1556 "glMapBufferRange(length = %ld)", length);
1557 return;
1558 }
1559
1560 bufObj = get_buffer(ctx, target);
1561 if (!bufObj) {
1562 _mesa_error(ctx, GL_INVALID_ENUM,
1563 "glMapBufferRange(target = 0x%x)", target);
1564 return;
1565 }
1566
1567 if (bufObj->Name == 0) {
1568 _mesa_error(ctx, GL_INVALID_OPERATION,
1569 "glMapBufferRange(current buffer is 0)");
1570 return;
1571 }
1572
1573 if (!bufObj->Pointer) {
1574 /* buffer is not mapped */
1575 _mesa_error(ctx, GL_INVALID_OPERATION,
1576 "glMapBufferRange(buffer is not mapped)");
1577 return;
1578 }
1579
1580 if ((bufObj->AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
1581 _mesa_error(ctx, GL_INVALID_OPERATION,
1582 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1583 return;
1584 }
1585
1586 if (offset + length > bufObj->Length) {
1587 _mesa_error(ctx, GL_INVALID_VALUE,
1588 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1589 offset, length, bufObj->Length);
1590 return;
1591 }
1592
1593 ASSERT(bufObj->AccessFlags & GL_MAP_WRITE_BIT);
1594
1595 if (ctx->Driver.FlushMappedBufferRange)
1596 ctx->Driver.FlushMappedBufferRange(ctx, target, offset, length, bufObj);
1597 }