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