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 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 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 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 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 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 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 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 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 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 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 /**********************************************************************/
868 /* API Functions */
869 /**********************************************************************/
870
871 void GLAPIENTRY
872 _mesa_BindBufferARB(GLenum target, GLuint buffer)
873 {
874 GET_CURRENT_CONTEXT(ctx);
875 ASSERT_OUTSIDE_BEGIN_END(ctx);
876
877 bind_buffer_object(ctx, target, buffer);
878 }
879
880
881 /**
882 * Delete a set of buffer objects.
883 *
884 * \param n Number of buffer objects to delete.
885 * \param ids Array of \c n buffer object IDs.
886 */
887 void GLAPIENTRY
888 _mesa_DeleteBuffersARB(GLsizei n, const GLuint *ids)
889 {
890 GET_CURRENT_CONTEXT(ctx);
891 GLsizei i;
892 ASSERT_OUTSIDE_BEGIN_END(ctx);
893
894 if (n < 0) {
895 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
896 return;
897 }
898
899 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
900
901 for (i = 0; i < n; i++) {
902 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
903 if (bufObj) {
904 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
905 GLuint j;
906
907 ASSERT(bufObj->Name == ids[i]);
908
909 if (bufObj->Pointer) {
910 /* if mapped, unmap it now */
911 ctx->Driver.UnmapBuffer(ctx, 0, bufObj);
912 bufObj->AccessFlags = DEFAULT_ACCESS;
913 bufObj->Pointer = NULL;
914 }
915
916 /* unbind any vertex pointers bound to this buffer */
917 unbind(ctx, &arrayObj->Vertex.BufferObj, bufObj);
918 unbind(ctx, &arrayObj->Weight.BufferObj, bufObj);
919 unbind(ctx, &arrayObj->Normal.BufferObj, bufObj);
920 unbind(ctx, &arrayObj->Color.BufferObj, bufObj);
921 unbind(ctx, &arrayObj->SecondaryColor.BufferObj, bufObj);
922 unbind(ctx, &arrayObj->FogCoord.BufferObj, bufObj);
923 unbind(ctx, &arrayObj->Index.BufferObj, bufObj);
924 unbind(ctx, &arrayObj->EdgeFlag.BufferObj, bufObj);
925 for (j = 0; j < Elements(arrayObj->TexCoord); j++) {
926 unbind(ctx, &arrayObj->TexCoord[j].BufferObj, bufObj);
927 }
928 for (j = 0; j < Elements(arrayObj->VertexAttrib); j++) {
929 unbind(ctx, &arrayObj->VertexAttrib[j].BufferObj, bufObj);
930 }
931
932 if (ctx->Array.ArrayBufferObj == bufObj) {
933 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
934 }
935 if (ctx->Array.ElementArrayBufferObj == bufObj) {
936 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
937 }
938
939 /* unbind any pixel pack/unpack pointers bound to this buffer */
940 if (ctx->Pack.BufferObj == bufObj) {
941 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 );
942 }
943 if (ctx->Unpack.BufferObj == bufObj) {
944 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
945 }
946
947 /* The ID is immediately freed for re-use */
948 _mesa_HashRemove(ctx->Shared->BufferObjects, bufObj->Name);
949 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
950 }
951 }
952
953 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
954 }
955
956
957 /**
958 * Generate a set of unique buffer object IDs and store them in \c buffer.
959 *
960 * \param n Number of IDs to generate.
961 * \param buffer Array of \c n locations to store the IDs.
962 */
963 void GLAPIENTRY
964 _mesa_GenBuffersARB(GLsizei n, GLuint *buffer)
965 {
966 GET_CURRENT_CONTEXT(ctx);
967 GLuint first;
968 GLint i;
969 ASSERT_OUTSIDE_BEGIN_END(ctx);
970
971 if (n < 0) {
972 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
973 return;
974 }
975
976 if (!buffer) {
977 return;
978 }
979
980 /*
981 * This must be atomic (generation and allocation of buffer object IDs)
982 */
983 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
984
985 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
986
987 /* Allocate new, empty buffer objects and return identifiers */
988 for (i = 0; i < n; i++) {
989 struct gl_buffer_object *bufObj;
990 GLuint name = first + i;
991 GLenum target = 0;
992 bufObj = ctx->Driver.NewBufferObject( ctx, name, target );
993 if (!bufObj) {
994 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
995 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenBuffersARB");
996 return;
997 }
998 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i, bufObj);
999 buffer[i] = first + i;
1000 }
1001
1002 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1003 }
1004
1005
1006 /**
1007 * Determine if ID is the name of a buffer object.
1008 *
1009 * \param id ID of the potential buffer object.
1010 * \return \c GL_TRUE if \c id is the name of a buffer object,
1011 * \c GL_FALSE otherwise.
1012 */
1013 GLboolean GLAPIENTRY
1014 _mesa_IsBufferARB(GLuint id)
1015 {
1016 struct gl_buffer_object *bufObj;
1017 GET_CURRENT_CONTEXT(ctx);
1018 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1019
1020 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1021 bufObj = _mesa_lookup_bufferobj(ctx, id);
1022 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1023
1024 return bufObj ? GL_TRUE : GL_FALSE;
1025 }
1026
1027
1028 void GLAPIENTRY
1029 _mesa_BufferDataARB(GLenum target, GLsizeiptrARB size,
1030 const GLvoid * data, GLenum usage)
1031 {
1032 GET_CURRENT_CONTEXT(ctx);
1033 struct gl_buffer_object *bufObj;
1034 ASSERT_OUTSIDE_BEGIN_END(ctx);
1035
1036 if (size < 0) {
1037 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
1038 return;
1039 }
1040
1041 switch (usage) {
1042 case GL_STREAM_DRAW_ARB:
1043 case GL_STREAM_READ_ARB:
1044 case GL_STREAM_COPY_ARB:
1045 case GL_STATIC_DRAW_ARB:
1046 case GL_STATIC_READ_ARB:
1047 case GL_STATIC_COPY_ARB:
1048 case GL_DYNAMIC_DRAW_ARB:
1049 case GL_DYNAMIC_READ_ARB:
1050 case GL_DYNAMIC_COPY_ARB:
1051 /* OK */
1052 break;
1053 default:
1054 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(usage)");
1055 return;
1056 }
1057
1058 bufObj = get_buffer(ctx, target);
1059 if (!bufObj) {
1060 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(target)" );
1061 return;
1062 }
1063 if (bufObj->Name == 0) {
1064 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferDataARB(buffer 0)" );
1065 return;
1066 }
1067
1068 if (bufObj->Pointer) {
1069 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1070 ctx->Driver.UnmapBuffer(ctx, target, bufObj);
1071 bufObj->AccessFlags = DEFAULT_ACCESS;
1072 bufObj->Pointer = NULL;
1073 }
1074
1075 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1076
1077 ASSERT(ctx->Driver.BufferData);
1078
1079 bufObj->Written = GL_TRUE;
1080
1081 #ifdef VBO_DEBUG
1082 _mesa_printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1083 bufObj->Name, size, data, usage);
1084 #endif
1085
1086 #ifdef BOUNDS_CHECK
1087 size += 100;
1088 #endif
1089 /* Give the buffer object to the driver! <data> may be null! */
1090 ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj );
1091 }
1092
1093
1094 void GLAPIENTRY
1095 _mesa_BufferSubDataARB(GLenum target, GLintptrARB offset,
1096 GLsizeiptrARB size, const GLvoid * data)
1097 {
1098 GET_CURRENT_CONTEXT(ctx);
1099 struct gl_buffer_object *bufObj;
1100 ASSERT_OUTSIDE_BEGIN_END(ctx);
1101
1102 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1103 "glBufferSubDataARB" );
1104 if (!bufObj) {
1105 /* error already recorded */
1106 return;
1107 }
1108
1109 bufObj->Written = GL_TRUE;
1110
1111 ASSERT(ctx->Driver.BufferSubData);
1112 ctx->Driver.BufferSubData( ctx, target, offset, size, data, bufObj );
1113 }
1114
1115
1116 void GLAPIENTRY
1117 _mesa_GetBufferSubDataARB(GLenum target, GLintptrARB offset,
1118 GLsizeiptrARB size, void * data)
1119 {
1120 GET_CURRENT_CONTEXT(ctx);
1121 struct gl_buffer_object *bufObj;
1122 ASSERT_OUTSIDE_BEGIN_END(ctx);
1123
1124 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1125 "glGetBufferSubDataARB" );
1126 if (!bufObj) {
1127 /* error already recorded */
1128 return;
1129 }
1130
1131 ASSERT(ctx->Driver.GetBufferSubData);
1132 ctx->Driver.GetBufferSubData( ctx, target, offset, size, data, bufObj );
1133 }
1134
1135
1136 void * GLAPIENTRY
1137 _mesa_MapBufferARB(GLenum target, GLenum access)
1138 {
1139 GET_CURRENT_CONTEXT(ctx);
1140 struct gl_buffer_object * bufObj;
1141 GLbitfield accessFlags;
1142 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1143
1144 switch (access) {
1145 case GL_READ_ONLY_ARB:
1146 accessFlags = GL_MAP_READ_BIT;
1147 break;
1148 case GL_WRITE_ONLY_ARB:
1149 accessFlags = GL_MAP_WRITE_BIT;
1150 break;
1151 case GL_READ_WRITE_ARB:
1152 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1153 break;
1154 default:
1155 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
1156 return NULL;
1157 }
1158
1159 bufObj = get_buffer(ctx, target);
1160 if (!bufObj) {
1161 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(target)" );
1162 return NULL;
1163 }
1164 if (bufObj->Name == 0) {
1165 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(buffer 0)" );
1166 return NULL;
1167 }
1168 if (bufObj->Pointer) {
1169 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
1170 return NULL;
1171 }
1172
1173 ASSERT(ctx->Driver.MapBuffer);
1174 bufObj->Pointer = ctx->Driver.MapBuffer( ctx, target, access, bufObj );
1175 if (!bufObj->Pointer) {
1176 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(access)");
1177 }
1178
1179 bufObj->AccessFlags = accessFlags;
1180 bufObj->Offset = 0;
1181 bufObj->Length = bufObj->Size;
1182
1183 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
1184 bufObj->Written = GL_TRUE;
1185
1186 #ifdef VBO_DEBUG
1187 _mesa_printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1188 bufObj->Name, bufObj->Size, access);
1189 if (access == GL_WRITE_ONLY_ARB) {
1190 GLuint i;
1191 GLubyte *b = (GLubyte *) bufObj->Pointer;
1192 for (i = 0; i < bufObj->Size; i++)
1193 b[i] = i & 0xff;
1194 }
1195 #endif
1196
1197 #ifdef BOUNDS_CHECK
1198 {
1199 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1200 GLuint i;
1201 /* buffer is 100 bytes larger than requested, fill with magic value */
1202 for (i = 0; i < 100; i++) {
1203 buf[bufObj->Size - i - 1] = 123;
1204 }
1205 }
1206 #endif
1207
1208 return bufObj->Pointer;
1209 }
1210
1211
1212 GLboolean GLAPIENTRY
1213 _mesa_UnmapBufferARB(GLenum target)
1214 {
1215 GET_CURRENT_CONTEXT(ctx);
1216 struct gl_buffer_object *bufObj;
1217 GLboolean status = GL_TRUE;
1218 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1219
1220 bufObj = get_buffer(ctx, target);
1221 if (!bufObj) {
1222 _mesa_error(ctx, GL_INVALID_ENUM, "glUnmapBufferARB(target)" );
1223 return GL_FALSE;
1224 }
1225 if (bufObj->Name == 0) {
1226 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB" );
1227 return GL_FALSE;
1228 }
1229 if (!bufObj->Pointer) {
1230 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1231 return GL_FALSE;
1232 }
1233
1234 #ifdef BOUNDS_CHECK
1235 if (bufObj->Access != GL_READ_ONLY_ARB) {
1236 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1237 GLuint i;
1238 /* check that last 100 bytes are still = magic value */
1239 for (i = 0; i < 100; i++) {
1240 GLuint pos = bufObj->Size - i - 1;
1241 if (buf[pos] != 123) {
1242 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1243 " at position %d (value = %u)\n",
1244 pos, buf[pos]);
1245 }
1246 }
1247 }
1248 #endif
1249
1250 #ifdef VBO_DEBUG
1251 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1252 GLuint i, unchanged = 0;
1253 GLubyte *b = (GLubyte *) bufObj->Pointer;
1254 GLint pos = -1;
1255 /* check which bytes changed */
1256 for (i = 0; i < bufObj->Size - 1; i++) {
1257 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1258 unchanged++;
1259 if (pos == -1)
1260 pos = i;
1261 }
1262 }
1263 if (unchanged) {
1264 _mesa_printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1265 bufObj->Name, unchanged, bufObj->Size, pos);
1266 }
1267 }
1268 #endif
1269
1270 status = ctx->Driver.UnmapBuffer( ctx, target, bufObj );
1271 bufObj->AccessFlags = DEFAULT_ACCESS;
1272 bufObj->Pointer = NULL;
1273 bufObj->Offset = 0;
1274 bufObj->Length = 0;
1275
1276 return status;
1277 }
1278
1279
1280 void GLAPIENTRY
1281 _mesa_GetBufferParameterivARB(GLenum target, GLenum pname, GLint *params)
1282 {
1283 GET_CURRENT_CONTEXT(ctx);
1284 struct gl_buffer_object *bufObj;
1285 ASSERT_OUTSIDE_BEGIN_END(ctx);
1286
1287 bufObj = get_buffer(ctx, target);
1288 if (!bufObj) {
1289 _mesa_error(ctx, GL_INVALID_ENUM, "GetBufferParameterivARB(target)" );
1290 return;
1291 }
1292 if (bufObj->Name == 0) {
1293 _mesa_error(ctx, GL_INVALID_OPERATION, "GetBufferParameterivARB" );
1294 return;
1295 }
1296
1297 switch (pname) {
1298 case GL_BUFFER_SIZE_ARB:
1299 *params = (GLint) bufObj->Size;
1300 break;
1301 case GL_BUFFER_USAGE_ARB:
1302 *params = bufObj->Usage;
1303 break;
1304 case GL_BUFFER_ACCESS_ARB:
1305 *params = simplified_access_mode(bufObj->AccessFlags);
1306 break;
1307 case GL_BUFFER_MAPPED_ARB:
1308 *params = (bufObj->Pointer != NULL);
1309 break;
1310 default:
1311 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname)");
1312 return;
1313 }
1314 }
1315
1316
1317 void GLAPIENTRY
1318 _mesa_GetBufferPointervARB(GLenum target, GLenum pname, GLvoid **params)
1319 {
1320 GET_CURRENT_CONTEXT(ctx);
1321 struct gl_buffer_object * bufObj;
1322 ASSERT_OUTSIDE_BEGIN_END(ctx);
1323
1324 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1325 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1326 return;
1327 }
1328
1329 bufObj = get_buffer(ctx, target);
1330 if (!bufObj) {
1331 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(target)" );
1332 return;
1333 }
1334 if (bufObj->Name == 0) {
1335 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferPointervARB" );
1336 return;
1337 }
1338
1339 *params = bufObj->Pointer;
1340 }
1341
1342
1343 void GLAPIENTRY
1344 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
1345 GLintptr readOffset, GLintptr writeOffset,
1346 GLsizeiptr size)
1347 {
1348 GET_CURRENT_CONTEXT(ctx);
1349 struct gl_buffer_object *src, *dst;
1350 ASSERT_OUTSIDE_BEGIN_END(ctx);
1351
1352 src = get_buffer(ctx, readTarget);
1353 if (!src || src->Name == 0) {
1354 _mesa_error(ctx, GL_INVALID_ENUM,
1355 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget);
1356 return;
1357 }
1358
1359 dst = get_buffer(ctx, writeTarget);
1360 if (!dst || dst->Name == 0) {
1361 _mesa_error(ctx, GL_INVALID_ENUM,
1362 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget);
1363 return;
1364 }
1365
1366 if (src->Pointer) {
1367 _mesa_error(ctx, GL_INVALID_OPERATION,
1368 "glCopyBuffserSubData(readBuffer is mapped)");
1369 return;
1370 }
1371
1372 if (dst->Pointer) {
1373 _mesa_error(ctx, GL_INVALID_OPERATION,
1374 "glCopyBuffserSubData(writeBuffer is mapped)");
1375 return;
1376 }
1377
1378 if (readOffset < 0) {
1379 _mesa_error(ctx, GL_INVALID_VALUE,
1380 "glCopyBuffserSubData(readOffset = %d)", readOffset);
1381 return;
1382 }
1383
1384 if (writeOffset < 0) {
1385 _mesa_error(ctx, GL_INVALID_VALUE,
1386 "glCopyBuffserSubData(writeOffset = %d)", writeOffset);
1387 return;
1388 }
1389
1390 if (readOffset + size > src->Size) {
1391 _mesa_error(ctx, GL_INVALID_VALUE,
1392 "glCopyBuffserSubData(readOffset + size = %d)",
1393 readOffset, size);
1394 return;
1395 }
1396
1397 if (writeOffset + size > dst->Size) {
1398 _mesa_error(ctx, GL_INVALID_VALUE,
1399 "glCopyBuffserSubData(writeOffset + size = %d)",
1400 writeOffset, size);
1401 return;
1402 }
1403
1404 if (src == dst) {
1405 if (readOffset + size <= writeOffset) {
1406 /* OK */
1407 }
1408 else if (writeOffset + size <= readOffset) {
1409 /* OK */
1410 }
1411 else {
1412 /* overlapping src/dst is illegal */
1413 _mesa_error(ctx, GL_INVALID_VALUE,
1414 "glCopyBuffserSubData(overlapping src/dst)");
1415 return;
1416 }
1417 }
1418
1419 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
1420 }
1421
1422
1423 /**
1424 * See GL_ARB_map_buffer_range spec
1425 */
1426 void * GLAPIENTRY
1427 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
1428 GLbitfield access)
1429 {
1430 GET_CURRENT_CONTEXT(ctx);
1431 struct gl_buffer_object *bufObj;
1432 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1433
1434 if (!ctx->Extensions.ARB_map_buffer_range) {
1435 _mesa_error(ctx, GL_INVALID_OPERATION,
1436 "glMapBufferRange(extension not supported)");
1437 return NULL;
1438 }
1439
1440 if (offset < 0) {
1441 _mesa_error(ctx, GL_INVALID_VALUE,
1442 "glMapBufferRange(offset = %ld)", offset);
1443 return NULL;
1444 }
1445
1446 if (length < 0) {
1447 _mesa_error(ctx, GL_INVALID_VALUE,
1448 "glMapBufferRange(length = %ld)", length);
1449 return NULL;
1450 }
1451
1452 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
1453 _mesa_error(ctx, GL_INVALID_OPERATION,
1454 "glMapBufferRange(access indicates neither read or write)");
1455 return NULL;
1456 }
1457
1458 if (access & GL_MAP_READ_BIT) {
1459 if ((access & GL_MAP_INVALIDATE_RANGE_BIT) ||
1460 (access & GL_MAP_INVALIDATE_BUFFER_BIT) ||
1461 (access & GL_MAP_UNSYNCHRONIZED_BIT)) {
1462 _mesa_error(ctx, GL_INVALID_OPERATION,
1463 "glMapBufferRange(invalid access flags)");
1464 return NULL;
1465 }
1466 }
1467
1468 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
1469 ((access & GL_MAP_WRITE_BIT) == 0)) {
1470 _mesa_error(ctx, GL_INVALID_OPERATION,
1471 "glMapBufferRange(invalid access flags)");
1472 return NULL;
1473 }
1474
1475 bufObj = get_buffer(ctx, target);
1476 if (!bufObj || bufObj->Name == 0) {
1477 _mesa_error(ctx, GL_INVALID_ENUM,
1478 "glMapBufferRange(target = 0x%x)", target);
1479 return NULL;
1480 }
1481
1482 if (offset + length > bufObj->Size) {
1483 _mesa_error(ctx, GL_INVALID_VALUE,
1484 "glMapBufferRange(offset + length > size)");
1485 return NULL;
1486 }
1487
1488 if (bufObj->Pointer) {
1489 _mesa_error(ctx, GL_INVALID_OPERATION,
1490 "glMapBufferRange(buffer already mapped)");
1491 return NULL;
1492 }
1493
1494 ASSERT(ctx->Driver.MapBufferRange);
1495 bufObj->Pointer = ctx->Driver.MapBufferRange(ctx, target, offset, length,
1496 access, bufObj);
1497
1498 bufObj->Offset = offset;
1499 bufObj->Length = length;
1500 bufObj->AccessFlags = access;
1501
1502 return bufObj->Pointer;
1503 }
1504
1505
1506 /**
1507 * See GL_ARB_map_buffer_range spec
1508 */
1509 void GLAPIENTRY
1510 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
1511 {
1512 GET_CURRENT_CONTEXT(ctx);
1513 struct gl_buffer_object *bufObj;
1514 ASSERT_OUTSIDE_BEGIN_END(ctx);
1515
1516 if (!ctx->Extensions.ARB_map_buffer_range) {
1517 _mesa_error(ctx, GL_INVALID_OPERATION,
1518 "glMapBufferRange(extension not supported)");
1519 return;
1520 }
1521
1522 if (offset < 0) {
1523 _mesa_error(ctx, GL_INVALID_VALUE,
1524 "glMapBufferRange(offset = %ld)", offset);
1525 return;
1526 }
1527
1528 if (length < 0) {
1529 _mesa_error(ctx, GL_INVALID_VALUE,
1530 "glMapBufferRange(length = %ld)", length);
1531 return;
1532 }
1533
1534 bufObj = get_buffer(ctx, target);
1535 if (!bufObj) {
1536 _mesa_error(ctx, GL_INVALID_ENUM,
1537 "glMapBufferRange(target = 0x%x)", target);
1538 return;
1539 }
1540
1541 if (bufObj->Name == 0) {
1542 _mesa_error(ctx, GL_INVALID_OPERATION,
1543 "glMapBufferRange(current buffer is 0)");
1544 return;
1545 }
1546
1547 if (!bufObj->Pointer) {
1548 /* buffer is not mapped */
1549 _mesa_error(ctx, GL_INVALID_OPERATION,
1550 "glMapBufferRange(buffer is not mapped)");
1551 return;
1552 }
1553
1554 if ((bufObj->AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
1555 _mesa_error(ctx, GL_INVALID_OPERATION,
1556 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1557 return;
1558 }
1559
1560 if (offset + length > bufObj->Length) {
1561 _mesa_error(ctx, GL_INVALID_VALUE,
1562 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1563 offset, length, bufObj->Length);
1564 return;
1565 }
1566
1567 ASSERT(bufObj->AccessFlags & GL_MAP_WRITE_BIT);
1568
1569 if (ctx->Driver.FlushMappedBufferRange)
1570 ctx->Driver.FlushMappedBufferRange(ctx, target, offset, length, bufObj);
1571 }