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