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