mesa: Fix default value of BUFFER_ACCESS_FLAGS.
[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 #include <stdbool.h>
34 #include "glheader.h"
35 #include "enums.h"
36 #include "hash.h"
37 #include "imports.h"
38 #include "image.h"
39 #include "context.h"
40 #include "bufferobj.h"
41 #include "fbobject.h"
42 #include "mfeatures.h"
43 #include "mtypes.h"
44 #include "texobj.h"
45 #include "transformfeedback.h"
46 #include "dispatch.h"
47
48
49 /* Debug flags */
50 /*#define VBO_DEBUG*/
51 /*#define BOUNDS_CHECK*/
52
53
54 /**
55 * Used as a placeholder for buffer objects between glGenBuffers() and
56 * glBindBuffer() so that glIsBuffer() can work correctly.
57 */
58 static struct gl_buffer_object DummyBufferObject;
59
60
61 /**
62 * Return pointer to address of a buffer object target.
63 * \param ctx the GL context
64 * \param target the buffer object target to be retrieved.
65 * \return pointer to pointer to the buffer object bound to \c target in the
66 * specified context or \c NULL if \c target is invalid.
67 */
68 static inline struct gl_buffer_object **
69 get_buffer_target(struct gl_context *ctx, GLenum target)
70 {
71 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
72 */
73 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx)
74 && target != GL_ARRAY_BUFFER && target != GL_ELEMENT_ARRAY_BUFFER)
75 return NULL;
76
77 switch (target) {
78 case GL_ARRAY_BUFFER_ARB:
79 return &ctx->Array.ArrayBufferObj;
80 case GL_ELEMENT_ARRAY_BUFFER_ARB:
81 return &ctx->Array.ArrayObj->ElementArrayBufferObj;
82 case GL_PIXEL_PACK_BUFFER_EXT:
83 return &ctx->Pack.BufferObj;
84 case GL_PIXEL_UNPACK_BUFFER_EXT:
85 return &ctx->Unpack.BufferObj;
86 case GL_COPY_READ_BUFFER:
87 return &ctx->CopyReadBuffer;
88 case GL_COPY_WRITE_BUFFER:
89 return &ctx->CopyWriteBuffer;
90 case GL_TRANSFORM_FEEDBACK_BUFFER:
91 if (ctx->Extensions.EXT_transform_feedback) {
92 return &ctx->TransformFeedback.CurrentBuffer;
93 }
94 break;
95 case GL_TEXTURE_BUFFER:
96 if (ctx->API == API_OPENGL_CORE &&
97 ctx->Extensions.ARB_texture_buffer_object) {
98 return &ctx->Texture.BufferObject;
99 }
100 break;
101 case GL_UNIFORM_BUFFER:
102 if (ctx->Extensions.ARB_uniform_buffer_object) {
103 return &ctx->UniformBuffer;
104 }
105 break;
106 default:
107 return NULL;
108 }
109 return NULL;
110 }
111
112
113 /**
114 * Get the buffer object bound to the specified target in a GL context.
115 * \param ctx the GL context
116 * \param target the buffer object target to be retrieved.
117 * \return pointer to the buffer object bound to \c target in the
118 * specified context or \c NULL if \c target is invalid.
119 */
120 static inline struct gl_buffer_object *
121 get_buffer(struct gl_context *ctx, const char *func, GLenum target)
122 {
123 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
124
125 if (!bufObj) {
126 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
127 return NULL;
128 }
129
130 if (!_mesa_is_bufferobj(*bufObj)) {
131 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(buffer 0)", func);
132 return NULL;
133 }
134
135 return *bufObj;
136 }
137
138
139 /**
140 * Convert a GLbitfield describing the mapped buffer access flags
141 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
142 */
143 static GLenum
144 simplified_access_mode(struct gl_context *ctx, GLbitfield access)
145 {
146 const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
147 if ((access & rwFlags) == rwFlags)
148 return GL_READ_WRITE;
149 if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
150 return GL_READ_ONLY;
151 if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
152 return GL_WRITE_ONLY;
153
154 /* Otherwise, AccessFlags is zero (the default state).
155 *
156 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
157 *
158 * Name Type Initial Value Legal Values
159 * ... ... ... ...
160 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
161 * READ_WRITE
162 *
163 * However, table 6.8 in the GL_OES_mapbuffer extension says:
164 *
165 * Get Value Type Get Command Value Description
166 * --------- ---- ----------- ----- -----------
167 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
168 *
169 * The difference is because GL_OES_mapbuffer only supports mapping buffers
170 * write-only.
171 */
172 assert(access == 0);
173
174 return _mesa_is_gles(ctx) ? GL_WRITE_ONLY : GL_READ_WRITE;
175 }
176
177
178 /**
179 * Tests the subdata range parameters and sets the GL error code for
180 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
181 *
182 * \param ctx GL context.
183 * \param target Buffer object target on which to operate.
184 * \param offset Offset of the first byte of the subdata range.
185 * \param size Size, in bytes, of the subdata range.
186 * \param caller Name of calling function for recording errors.
187 * \return A pointer to the buffer object bound to \c target in the
188 * specified context or \c NULL if any of the parameter or state
189 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
190 * are invalid.
191 *
192 * \sa glBufferSubDataARB, glGetBufferSubDataARB
193 */
194 static struct gl_buffer_object *
195 buffer_object_subdata_range_good( struct gl_context * ctx, GLenum target,
196 GLintptrARB offset, GLsizeiptrARB size,
197 const char *caller )
198 {
199 struct gl_buffer_object *bufObj;
200
201 if (size < 0) {
202 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
203 return NULL;
204 }
205
206 if (offset < 0) {
207 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
208 return NULL;
209 }
210
211 bufObj = get_buffer(ctx, caller, target);
212 if (!bufObj)
213 return NULL;
214
215 if (offset + size > bufObj->Size) {
216 _mesa_error(ctx, GL_INVALID_VALUE,
217 "%s(offset %lu + size %lu > buffer size %lu)", caller,
218 (unsigned long) offset,
219 (unsigned long) size,
220 (unsigned long) bufObj->Size);
221 return NULL;
222 }
223 if (_mesa_bufferobj_mapped(bufObj)) {
224 /* Buffer is currently mapped */
225 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
226 return NULL;
227 }
228
229 return bufObj;
230 }
231
232
233 /**
234 * Allocate and initialize a new buffer object.
235 *
236 * Default callback for the \c dd_function_table::NewBufferObject() hook.
237 */
238 static struct gl_buffer_object *
239 _mesa_new_buffer_object( struct gl_context *ctx, GLuint name, GLenum target )
240 {
241 struct gl_buffer_object *obj;
242
243 (void) ctx;
244
245 obj = MALLOC_STRUCT(gl_buffer_object);
246 _mesa_initialize_buffer_object(ctx, obj, name, target);
247 return obj;
248 }
249
250
251 /**
252 * Delete a buffer object.
253 *
254 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
255 */
256 static void
257 _mesa_delete_buffer_object(struct gl_context *ctx,
258 struct gl_buffer_object *bufObj)
259 {
260 (void) ctx;
261
262 free(bufObj->Data);
263
264 /* assign strange values here to help w/ debugging */
265 bufObj->RefCount = -1000;
266 bufObj->Name = ~0;
267
268 _glthread_DESTROY_MUTEX(bufObj->Mutex);
269 free(bufObj);
270 }
271
272
273
274 /**
275 * Set ptr to bufObj w/ reference counting.
276 * This is normally only called from the _mesa_reference_buffer_object() macro
277 * when there's a real pointer change.
278 */
279 void
280 _mesa_reference_buffer_object_(struct gl_context *ctx,
281 struct gl_buffer_object **ptr,
282 struct gl_buffer_object *bufObj)
283 {
284 if (*ptr) {
285 /* Unreference the old buffer */
286 GLboolean deleteFlag = GL_FALSE;
287 struct gl_buffer_object *oldObj = *ptr;
288
289 _glthread_LOCK_MUTEX(oldObj->Mutex);
290 ASSERT(oldObj->RefCount > 0);
291 oldObj->RefCount--;
292 #if 0
293 printf("BufferObj %p %d DECR to %d\n",
294 (void *) oldObj, oldObj->Name, oldObj->RefCount);
295 #endif
296 deleteFlag = (oldObj->RefCount == 0);
297 _glthread_UNLOCK_MUTEX(oldObj->Mutex);
298
299 if (deleteFlag) {
300
301 /* some sanity checking: don't delete a buffer still in use */
302 #if 0
303 /* unfortunately, these tests are invalid during context tear-down */
304 ASSERT(ctx->Array.ArrayBufferObj != bufObj);
305 ASSERT(ctx->Array.ArrayObj->ElementArrayBufferObj != bufObj);
306 ASSERT(ctx->Array.ArrayObj->Vertex.BufferObj != bufObj);
307 #endif
308
309 ASSERT(ctx->Driver.DeleteBuffer);
310 ctx->Driver.DeleteBuffer(ctx, oldObj);
311 }
312
313 *ptr = NULL;
314 }
315 ASSERT(!*ptr);
316
317 if (bufObj) {
318 /* reference new buffer */
319 _glthread_LOCK_MUTEX(bufObj->Mutex);
320 if (bufObj->RefCount == 0) {
321 /* this buffer's being deleted (look just above) */
322 /* Not sure this can every really happen. Warn if it does. */
323 _mesa_problem(NULL, "referencing deleted buffer object");
324 *ptr = NULL;
325 }
326 else {
327 bufObj->RefCount++;
328 #if 0
329 printf("BufferObj %p %d INCR to %d\n",
330 (void *) bufObj, bufObj->Name, bufObj->RefCount);
331 #endif
332 *ptr = bufObj;
333 }
334 _glthread_UNLOCK_MUTEX(bufObj->Mutex);
335 }
336 }
337
338
339 /**
340 * Initialize a buffer object to default values.
341 */
342 void
343 _mesa_initialize_buffer_object( struct gl_context *ctx,
344 struct gl_buffer_object *obj,
345 GLuint name, GLenum target )
346 {
347 (void) target;
348
349 memset(obj, 0, sizeof(struct gl_buffer_object));
350 _glthread_INIT_MUTEX(obj->Mutex);
351 obj->RefCount = 1;
352 obj->Name = name;
353 obj->Usage = GL_STATIC_DRAW_ARB;
354 obj->AccessFlags = 0;
355 }
356
357
358
359 /**
360 * Callback called from _mesa_HashWalk()
361 */
362 static void
363 count_buffer_size(GLuint key, void *data, void *userData)
364 {
365 const struct gl_buffer_object *bufObj =
366 (const struct gl_buffer_object *) data;
367 GLuint *total = (GLuint *) userData;
368
369 *total = *total + bufObj->Size;
370 }
371
372
373 /**
374 * Compute total size (in bytes) of all buffer objects for the given context.
375 * For debugging purposes.
376 */
377 GLuint
378 _mesa_total_buffer_object_memory(struct gl_context *ctx)
379 {
380 GLuint total = 0;
381
382 _mesa_HashWalk(ctx->Shared->BufferObjects, count_buffer_size, &total);
383
384 return total;
385 }
386
387
388 /**
389 * Allocate space for and store data in a buffer object. Any data that was
390 * previously stored in the buffer object is lost. If \c data is \c NULL,
391 * memory will be allocated, but no copy will occur.
392 *
393 * This is the default callback for \c dd_function_table::BufferData()
394 * Note that all GL error checking will have been done already.
395 *
396 * \param ctx GL context.
397 * \param target Buffer object target on which to operate.
398 * \param size Size, in bytes, of the new data store.
399 * \param data Pointer to the data to store in the buffer object. This
400 * pointer may be \c NULL.
401 * \param usage Hints about how the data will be used.
402 * \param bufObj Object to be used.
403 *
404 * \return GL_TRUE for success, GL_FALSE for failure
405 * \sa glBufferDataARB, dd_function_table::BufferData.
406 */
407 static GLboolean
408 _mesa_buffer_data( struct gl_context *ctx, GLenum target, GLsizeiptrARB size,
409 const GLvoid * data, GLenum usage,
410 struct gl_buffer_object * bufObj )
411 {
412 void * new_data;
413
414 (void) ctx; (void) target;
415
416 new_data = _mesa_realloc( bufObj->Data, bufObj->Size, size );
417 if (new_data) {
418 bufObj->Data = (GLubyte *) new_data;
419 bufObj->Size = size;
420 bufObj->Usage = usage;
421
422 if (data) {
423 memcpy( bufObj->Data, data, size );
424 }
425
426 return GL_TRUE;
427 }
428 else {
429 return GL_FALSE;
430 }
431 }
432
433
434 /**
435 * Replace data in a subrange of buffer object. If the data range
436 * specified by \c size + \c offset extends beyond the end of the buffer or
437 * if \c data is \c NULL, no copy is performed.
438 *
439 * This is the default callback for \c dd_function_table::BufferSubData()
440 * Note that all GL error checking will have been done already.
441 *
442 * \param ctx GL context.
443 * \param target Buffer object target on which to operate.
444 * \param offset Offset of the first byte to be modified.
445 * \param size Size, in bytes, of the data range.
446 * \param data Pointer to the data to store in the buffer object.
447 * \param bufObj Object to be used.
448 *
449 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
450 */
451 static void
452 _mesa_buffer_subdata( struct gl_context *ctx, GLintptrARB offset,
453 GLsizeiptrARB size, const GLvoid * data,
454 struct gl_buffer_object * bufObj )
455 {
456 (void) ctx;
457
458 /* this should have been caught in _mesa_BufferSubData() */
459 ASSERT(size + offset <= bufObj->Size);
460
461 if (bufObj->Data) {
462 memcpy( (GLubyte *) bufObj->Data + offset, data, size );
463 }
464 }
465
466
467 /**
468 * Retrieve data from a subrange of buffer object. If the data range
469 * specified by \c size + \c offset extends beyond the end of the buffer or
470 * if \c data is \c NULL, no copy is performed.
471 *
472 * This is the default callback for \c dd_function_table::GetBufferSubData()
473 * Note that all GL error checking will have been done already.
474 *
475 * \param ctx GL context.
476 * \param target Buffer object target on which to operate.
477 * \param offset Offset of the first byte to be fetched.
478 * \param size Size, in bytes, of the data range.
479 * \param data Destination for data
480 * \param bufObj Object to be used.
481 *
482 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
483 */
484 static void
485 _mesa_buffer_get_subdata( struct gl_context *ctx, GLintptrARB offset,
486 GLsizeiptrARB size, GLvoid * data,
487 struct gl_buffer_object * bufObj )
488 {
489 (void) ctx;
490
491 if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
492 memcpy( data, (GLubyte *) bufObj->Data + offset, size );
493 }
494 }
495
496
497 /**
498 * Default fallback for \c dd_function_table::MapBufferRange().
499 * Called via glMapBufferRange().
500 */
501 static void *
502 _mesa_buffer_map_range( struct gl_context *ctx, GLintptr offset,
503 GLsizeiptr length, GLbitfield access,
504 struct gl_buffer_object *bufObj )
505 {
506 (void) ctx;
507 assert(!_mesa_bufferobj_mapped(bufObj));
508 /* Just return a direct pointer to the data */
509 bufObj->Pointer = bufObj->Data + offset;
510 bufObj->Length = length;
511 bufObj->Offset = offset;
512 bufObj->AccessFlags = access;
513 return bufObj->Pointer;
514 }
515
516
517 /**
518 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
519 * Called via glFlushMappedBufferRange().
520 */
521 static void
522 _mesa_buffer_flush_mapped_range( struct gl_context *ctx,
523 GLintptr offset, GLsizeiptr length,
524 struct gl_buffer_object *obj )
525 {
526 (void) ctx;
527 (void) offset;
528 (void) length;
529 (void) obj;
530 /* no-op */
531 }
532
533
534 /**
535 * Default callback for \c dd_function_table::MapBuffer().
536 *
537 * The input parameters will have been already tested for errors.
538 *
539 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
540 */
541 static GLboolean
542 _mesa_buffer_unmap( struct gl_context *ctx, struct gl_buffer_object *bufObj )
543 {
544 (void) ctx;
545 /* XXX we might assert here that bufObj->Pointer is non-null */
546 bufObj->Pointer = NULL;
547 bufObj->Length = 0;
548 bufObj->Offset = 0;
549 bufObj->AccessFlags = 0x0;
550 return GL_TRUE;
551 }
552
553
554 /**
555 * Default fallback for \c dd_function_table::CopyBufferSubData().
556 * Called via glCopyBufferSubData().
557 */
558 static void
559 _mesa_copy_buffer_subdata(struct gl_context *ctx,
560 struct gl_buffer_object *src,
561 struct gl_buffer_object *dst,
562 GLintptr readOffset, GLintptr writeOffset,
563 GLsizeiptr size)
564 {
565 GLubyte *srcPtr, *dstPtr;
566
567 /* the buffers should not be mapped */
568 assert(!_mesa_bufferobj_mapped(src));
569 assert(!_mesa_bufferobj_mapped(dst));
570
571 if (src == dst) {
572 srcPtr = dstPtr = ctx->Driver.MapBufferRange(ctx, 0, src->Size,
573 GL_MAP_READ_BIT |
574 GL_MAP_WRITE_BIT, src);
575
576 if (!srcPtr)
577 return;
578
579 srcPtr += readOffset;
580 dstPtr += writeOffset;
581 } else {
582 srcPtr = ctx->Driver.MapBufferRange(ctx, readOffset, size,
583 GL_MAP_READ_BIT, src);
584 dstPtr = ctx->Driver.MapBufferRange(ctx, writeOffset, size,
585 (GL_MAP_WRITE_BIT |
586 GL_MAP_INVALIDATE_RANGE_BIT), dst);
587 }
588
589 /* Note: the src and dst regions will never overlap. Trying to do so
590 * would generate GL_INVALID_VALUE earlier.
591 */
592 if (srcPtr && dstPtr)
593 memcpy(dstPtr, srcPtr, size);
594
595 ctx->Driver.UnmapBuffer(ctx, src);
596 if (dst != src)
597 ctx->Driver.UnmapBuffer(ctx, dst);
598 }
599
600
601
602 /**
603 * Initialize the state associated with buffer objects
604 */
605 void
606 _mesa_init_buffer_objects( struct gl_context *ctx )
607 {
608 GLuint i;
609
610 memset(&DummyBufferObject, 0, sizeof(DummyBufferObject));
611 _glthread_INIT_MUTEX(DummyBufferObject.Mutex);
612 DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
613
614 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj,
615 ctx->Shared->NullBufferObj);
616
617 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer,
618 ctx->Shared->NullBufferObj);
619 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
620 ctx->Shared->NullBufferObj);
621
622 ctx->UniformBufferBindings = calloc(ctx->Const.MaxUniformBufferBindings,
623 sizeof(*ctx->UniformBufferBindings));
624
625 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer,
626 ctx->Shared->NullBufferObj);
627
628 for (i = 0; i < ctx->Const.MaxUniformBufferBindings; i++) {
629 _mesa_reference_buffer_object(ctx,
630 &ctx->UniformBufferBindings[i].BufferObject,
631 ctx->Shared->NullBufferObj);
632 ctx->UniformBufferBindings[i].Offset = -1;
633 ctx->UniformBufferBindings[i].Size = -1;
634 }
635 }
636
637
638 void
639 _mesa_free_buffer_objects( struct gl_context *ctx )
640 {
641 GLuint i;
642
643 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
644
645 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL);
646 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, NULL);
647
648 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, NULL);
649
650 for (i = 0; i < ctx->Const.MaxUniformBufferBindings; i++) {
651 _mesa_reference_buffer_object(ctx,
652 &ctx->UniformBufferBindings[i].BufferObject,
653 NULL);
654 }
655
656 free(ctx->UniformBufferBindings);
657 ctx->UniformBufferBindings = NULL;
658 }
659
660 static bool
661 handle_bind_buffer_gen(struct gl_context *ctx,
662 GLenum target,
663 GLuint buffer,
664 struct gl_buffer_object **buf_handle)
665 {
666 struct gl_buffer_object *buf = *buf_handle;
667
668 if (!buf && ctx->API == API_OPENGL_CORE) {
669 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindBuffer(non-gen name)");
670 return false;
671 }
672
673 if (!buf || buf == &DummyBufferObject) {
674 /* If this is a new buffer object id, or one which was generated but
675 * never used before, allocate a buffer object now.
676 */
677 ASSERT(ctx->Driver.NewBufferObject);
678 buf = ctx->Driver.NewBufferObject(ctx, buffer, target);
679 if (!buf) {
680 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindBufferARB");
681 return false;
682 }
683 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, buf);
684 *buf_handle = buf;
685 }
686
687 return true;
688 }
689
690 /**
691 * Bind the specified target to buffer for the specified context.
692 * Called by glBindBuffer() and other functions.
693 */
694 static void
695 bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
696 {
697 struct gl_buffer_object *oldBufObj;
698 struct gl_buffer_object *newBufObj = NULL;
699 struct gl_buffer_object **bindTarget = NULL;
700
701 bindTarget = get_buffer_target(ctx, target);
702 if (!bindTarget) {
703 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
704 return;
705 }
706
707 /* Get pointer to old buffer object (to be unbound) */
708 oldBufObj = *bindTarget;
709 if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
710 return; /* rebinding the same buffer object- no change */
711
712 /*
713 * Get pointer to new buffer object (newBufObj)
714 */
715 if (buffer == 0) {
716 /* The spec says there's not a buffer object named 0, but we use
717 * one internally because it simplifies things.
718 */
719 newBufObj = ctx->Shared->NullBufferObj;
720 }
721 else {
722 /* non-default buffer object */
723 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
724 if (!handle_bind_buffer_gen(ctx, target, buffer, &newBufObj))
725 return;
726 }
727
728 /* bind new buffer */
729 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
730
731 /* Pass BindBuffer call to device driver */
732 if (ctx->Driver.BindBuffer)
733 ctx->Driver.BindBuffer( ctx, target, newBufObj );
734 }
735
736
737 /**
738 * Update the default buffer objects in the given context to reference those
739 * specified in the shared state and release those referencing the old
740 * shared state.
741 */
742 void
743 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
744 {
745 /* Bind the NullBufferObj to remove references to those
746 * in the shared context hash table.
747 */
748 bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
749 bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
750 bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
751 bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
752 }
753
754
755
756 /**
757 * Return the gl_buffer_object for the given ID.
758 * Always return NULL for ID 0.
759 */
760 struct gl_buffer_object *
761 _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
762 {
763 if (buffer == 0)
764 return NULL;
765 else
766 return (struct gl_buffer_object *)
767 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
768 }
769
770
771 /**
772 * If *ptr points to obj, set ptr = the Null/default buffer object.
773 * This is a helper for buffer object deletion.
774 * The GL spec says that deleting a buffer object causes it to get
775 * unbound from all arrays in the current context.
776 */
777 static void
778 unbind(struct gl_context *ctx,
779 struct gl_buffer_object **ptr,
780 struct gl_buffer_object *obj)
781 {
782 if (*ptr == obj) {
783 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
784 }
785 }
786
787
788 /**
789 * Plug default/fallback buffer object functions into the device
790 * driver hooks.
791 */
792 void
793 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
794 {
795 /* GL_ARB_vertex/pixel_buffer_object */
796 driver->NewBufferObject = _mesa_new_buffer_object;
797 driver->DeleteBuffer = _mesa_delete_buffer_object;
798 driver->BindBuffer = NULL;
799 driver->BufferData = _mesa_buffer_data;
800 driver->BufferSubData = _mesa_buffer_subdata;
801 driver->GetBufferSubData = _mesa_buffer_get_subdata;
802 driver->UnmapBuffer = _mesa_buffer_unmap;
803
804 /* GL_ARB_map_buffer_range */
805 driver->MapBufferRange = _mesa_buffer_map_range;
806 driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
807
808 /* GL_ARB_copy_buffer */
809 driver->CopyBufferSubData = _mesa_copy_buffer_subdata;
810 }
811
812
813
814 /**********************************************************************/
815 /* API Functions */
816 /**********************************************************************/
817
818 void GLAPIENTRY
819 _mesa_BindBuffer(GLenum target, GLuint buffer)
820 {
821 GET_CURRENT_CONTEXT(ctx);
822 ASSERT_OUTSIDE_BEGIN_END(ctx);
823
824 if (MESA_VERBOSE & VERBOSE_API)
825 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
826 _mesa_lookup_enum_by_nr(target), buffer);
827
828 bind_buffer_object(ctx, target, buffer);
829 }
830
831
832 /**
833 * Delete a set of buffer objects.
834 *
835 * \param n Number of buffer objects to delete.
836 * \param ids Array of \c n buffer object IDs.
837 */
838 void GLAPIENTRY
839 _mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
840 {
841 GET_CURRENT_CONTEXT(ctx);
842 GLsizei i;
843 ASSERT_OUTSIDE_BEGIN_END(ctx);
844 FLUSH_VERTICES(ctx, 0);
845
846 if (n < 0) {
847 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
848 return;
849 }
850
851 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
852
853 for (i = 0; i < n; i++) {
854 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
855 if (bufObj) {
856 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
857 GLuint j;
858
859 ASSERT(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
860
861 if (_mesa_bufferobj_mapped(bufObj)) {
862 /* if mapped, unmap it now */
863 ctx->Driver.UnmapBuffer(ctx, bufObj);
864 bufObj->AccessFlags = 0;
865 bufObj->Pointer = NULL;
866 }
867
868 /* unbind any vertex pointers bound to this buffer */
869 for (j = 0; j < Elements(arrayObj->VertexAttrib); j++) {
870 unbind(ctx, &arrayObj->VertexAttrib[j].BufferObj, bufObj);
871 }
872
873 if (ctx->Array.ArrayBufferObj == bufObj) {
874 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB, 0 );
875 }
876 if (arrayObj->ElementArrayBufferObj == bufObj) {
877 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
878 }
879
880 /* unbind ARB_copy_buffer binding points */
881 if (ctx->CopyReadBuffer == bufObj) {
882 _mesa_BindBuffer( GL_COPY_READ_BUFFER, 0 );
883 }
884 if (ctx->CopyWriteBuffer == bufObj) {
885 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER, 0 );
886 }
887
888 /* unbind transform feedback binding points */
889 if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
890 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER, 0 );
891 }
892 for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
893 if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
894 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 );
895 }
896 }
897
898 /* unbind UBO binding points */
899 for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
900 if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
901 _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 );
902 }
903 }
904
905 if (ctx->UniformBuffer == bufObj) {
906 _mesa_BindBuffer( GL_UNIFORM_BUFFER, 0 );
907 }
908
909 /* unbind any pixel pack/unpack pointers bound to this buffer */
910 if (ctx->Pack.BufferObj == bufObj) {
911 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT, 0 );
912 }
913 if (ctx->Unpack.BufferObj == bufObj) {
914 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
915 }
916
917 if (ctx->Texture.BufferObject == bufObj) {
918 _mesa_BindBuffer( GL_TEXTURE_BUFFER, 0 );
919 }
920
921 /* The ID is immediately freed for re-use */
922 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
923 /* Make sure we do not run into the classic ABA problem on bind.
924 * We don't want to allow re-binding a buffer object that's been
925 * "deleted" by glDeleteBuffers().
926 *
927 * The explicit rebinding to the default object in the current context
928 * prevents the above in the current context, but another context
929 * sharing the same objects might suffer from this problem.
930 * The alternative would be to do the hash lookup in any case on bind
931 * which would introduce more runtime overhead than this.
932 */
933 bufObj->DeletePending = GL_TRUE;
934 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
935 }
936 }
937
938 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
939 }
940
941
942 /**
943 * Generate a set of unique buffer object IDs and store them in \c buffer.
944 *
945 * \param n Number of IDs to generate.
946 * \param buffer Array of \c n locations to store the IDs.
947 */
948 void GLAPIENTRY
949 _mesa_GenBuffers(GLsizei n, GLuint *buffer)
950 {
951 GET_CURRENT_CONTEXT(ctx);
952 GLuint first;
953 GLint i;
954 ASSERT_OUTSIDE_BEGIN_END(ctx);
955
956 if (MESA_VERBOSE & VERBOSE_API)
957 _mesa_debug(ctx, "glGenBuffers(%d)\n", n);
958
959 if (n < 0) {
960 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
961 return;
962 }
963
964 if (!buffer) {
965 return;
966 }
967
968 /*
969 * This must be atomic (generation and allocation of buffer object IDs)
970 */
971 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
972
973 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
974
975 /* Insert the ID and pointer to dummy buffer object into hash table */
976 for (i = 0; i < n; i++) {
977 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i,
978 &DummyBufferObject);
979 buffer[i] = first + i;
980 }
981
982 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
983 }
984
985
986 /**
987 * Determine if ID is the name of a buffer object.
988 *
989 * \param id ID of the potential buffer object.
990 * \return \c GL_TRUE if \c id is the name of a buffer object,
991 * \c GL_FALSE otherwise.
992 */
993 GLboolean GLAPIENTRY
994 _mesa_IsBuffer(GLuint id)
995 {
996 struct gl_buffer_object *bufObj;
997 GET_CURRENT_CONTEXT(ctx);
998 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
999
1000 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1001 bufObj = _mesa_lookup_bufferobj(ctx, id);
1002 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1003
1004 return bufObj && bufObj != &DummyBufferObject;
1005 }
1006
1007
1008 void GLAPIENTRY
1009 _mesa_BufferData(GLenum target, GLsizeiptrARB size,
1010 const GLvoid * data, GLenum usage)
1011 {
1012 GET_CURRENT_CONTEXT(ctx);
1013 struct gl_buffer_object *bufObj;
1014 bool valid_usage;
1015 ASSERT_OUTSIDE_BEGIN_END(ctx);
1016
1017 if (MESA_VERBOSE & VERBOSE_API)
1018 _mesa_debug(ctx, "glBufferData(%s, %ld, %p, %s)\n",
1019 _mesa_lookup_enum_by_nr(target),
1020 (long int) size, data,
1021 _mesa_lookup_enum_by_nr(usage));
1022
1023 if (size < 0) {
1024 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
1025 return;
1026 }
1027
1028 switch (usage) {
1029 case GL_STREAM_DRAW_ARB:
1030 valid_usage = (ctx->API != API_OPENGLES);
1031 break;
1032
1033 case GL_STATIC_DRAW_ARB:
1034 case GL_DYNAMIC_DRAW_ARB:
1035 valid_usage = true;
1036 break;
1037
1038 case GL_STREAM_READ_ARB:
1039 case GL_STREAM_COPY_ARB:
1040 case GL_STATIC_READ_ARB:
1041 case GL_STATIC_COPY_ARB:
1042 case GL_DYNAMIC_READ_ARB:
1043 case GL_DYNAMIC_COPY_ARB:
1044 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
1045 break;
1046
1047 default:
1048 valid_usage = false;
1049 break;
1050 }
1051
1052 if (!valid_usage) {
1053 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferData(usage)");
1054 return;
1055 }
1056
1057 bufObj = get_buffer(ctx, "glBufferDataARB", target);
1058 if (!bufObj)
1059 return;
1060
1061 if (_mesa_bufferobj_mapped(bufObj)) {
1062 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1063 ctx->Driver.UnmapBuffer(ctx, bufObj);
1064 bufObj->AccessFlags = 0;
1065 ASSERT(bufObj->Pointer == NULL);
1066 }
1067
1068 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1069
1070 bufObj->Written = GL_TRUE;
1071
1072 #ifdef VBO_DEBUG
1073 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1074 bufObj->Name, size, data, usage);
1075 #endif
1076
1077 #ifdef BOUNDS_CHECK
1078 size += 100;
1079 #endif
1080
1081 ASSERT(ctx->Driver.BufferData);
1082 if (!ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj )) {
1083 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB()");
1084 }
1085 }
1086
1087
1088 void GLAPIENTRY
1089 _mesa_BufferSubData(GLenum target, GLintptrARB offset,
1090 GLsizeiptrARB size, const GLvoid * data)
1091 {
1092 GET_CURRENT_CONTEXT(ctx);
1093 struct gl_buffer_object *bufObj;
1094 ASSERT_OUTSIDE_BEGIN_END(ctx);
1095
1096 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1097 "glBufferSubDataARB" );
1098 if (!bufObj) {
1099 /* error already recorded */
1100 return;
1101 }
1102
1103 if (size == 0)
1104 return;
1105
1106 bufObj->Written = GL_TRUE;
1107
1108 ASSERT(ctx->Driver.BufferSubData);
1109 ctx->Driver.BufferSubData( ctx, offset, size, data, bufObj );
1110 }
1111
1112
1113 void GLAPIENTRY
1114 _mesa_GetBufferSubData(GLenum target, GLintptrARB offset,
1115 GLsizeiptrARB size, void * data)
1116 {
1117 GET_CURRENT_CONTEXT(ctx);
1118 struct gl_buffer_object *bufObj;
1119 ASSERT_OUTSIDE_BEGIN_END(ctx);
1120
1121 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1122 "glGetBufferSubDataARB" );
1123 if (!bufObj) {
1124 /* error already recorded */
1125 return;
1126 }
1127
1128 ASSERT(ctx->Driver.GetBufferSubData);
1129 ctx->Driver.GetBufferSubData( ctx, offset, size, data, bufObj );
1130 }
1131
1132
1133 void * GLAPIENTRY
1134 _mesa_MapBuffer(GLenum target, GLenum access)
1135 {
1136 GET_CURRENT_CONTEXT(ctx);
1137 struct gl_buffer_object * bufObj;
1138 GLbitfield accessFlags;
1139 void *map;
1140 bool valid_access;
1141
1142 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1143
1144 switch (access) {
1145 case GL_READ_ONLY_ARB:
1146 accessFlags = GL_MAP_READ_BIT;
1147 valid_access = _mesa_is_desktop_gl(ctx);
1148 break;
1149 case GL_WRITE_ONLY_ARB:
1150 accessFlags = GL_MAP_WRITE_BIT;
1151 valid_access = true;
1152 break;
1153 case GL_READ_WRITE_ARB:
1154 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1155 valid_access = _mesa_is_desktop_gl(ctx);
1156 break;
1157 default:
1158 valid_access = false;
1159 break;
1160 }
1161
1162 if (!valid_access) {
1163 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
1164 return NULL;
1165 }
1166
1167 bufObj = get_buffer(ctx, "glMapBufferARB", target);
1168 if (!bufObj)
1169 return NULL;
1170
1171 if (_mesa_bufferobj_mapped(bufObj)) {
1172 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
1173 return NULL;
1174 }
1175
1176 if (!bufObj->Size) {
1177 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1178 "glMapBuffer(buffer size = 0)");
1179 return NULL;
1180 }
1181
1182 ASSERT(ctx->Driver.MapBufferRange);
1183 map = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size, accessFlags, bufObj);
1184 if (!map) {
1185 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1186 return NULL;
1187 }
1188 else {
1189 /* The driver callback should have set these fields.
1190 * This is important because other modules (like VBO) might call
1191 * the driver function directly.
1192 */
1193 ASSERT(bufObj->Pointer == map);
1194 ASSERT(bufObj->Length == bufObj->Size);
1195 ASSERT(bufObj->Offset == 0);
1196 bufObj->AccessFlags = accessFlags;
1197 }
1198
1199 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
1200 bufObj->Written = GL_TRUE;
1201
1202 #ifdef VBO_DEBUG
1203 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1204 bufObj->Name, bufObj->Size, access);
1205 if (access == GL_WRITE_ONLY_ARB) {
1206 GLuint i;
1207 GLubyte *b = (GLubyte *) bufObj->Pointer;
1208 for (i = 0; i < bufObj->Size; i++)
1209 b[i] = i & 0xff;
1210 }
1211 #endif
1212
1213 #ifdef BOUNDS_CHECK
1214 {
1215 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1216 GLuint i;
1217 /* buffer is 100 bytes larger than requested, fill with magic value */
1218 for (i = 0; i < 100; i++) {
1219 buf[bufObj->Size - i - 1] = 123;
1220 }
1221 }
1222 #endif
1223
1224 return bufObj->Pointer;
1225 }
1226
1227
1228 GLboolean GLAPIENTRY
1229 _mesa_UnmapBuffer(GLenum target)
1230 {
1231 GET_CURRENT_CONTEXT(ctx);
1232 struct gl_buffer_object *bufObj;
1233 GLboolean status = GL_TRUE;
1234 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1235
1236 bufObj = get_buffer(ctx, "glUnmapBufferARB", target);
1237 if (!bufObj)
1238 return GL_FALSE;
1239
1240 if (!_mesa_bufferobj_mapped(bufObj)) {
1241 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1242 return GL_FALSE;
1243 }
1244
1245 #ifdef BOUNDS_CHECK
1246 if (bufObj->Access != GL_READ_ONLY_ARB) {
1247 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1248 GLuint i;
1249 /* check that last 100 bytes are still = magic value */
1250 for (i = 0; i < 100; i++) {
1251 GLuint pos = bufObj->Size - i - 1;
1252 if (buf[pos] != 123) {
1253 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1254 " at position %d (value = %u)\n",
1255 pos, buf[pos]);
1256 }
1257 }
1258 }
1259 #endif
1260
1261 #ifdef VBO_DEBUG
1262 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1263 GLuint i, unchanged = 0;
1264 GLubyte *b = (GLubyte *) bufObj->Pointer;
1265 GLint pos = -1;
1266 /* check which bytes changed */
1267 for (i = 0; i < bufObj->Size - 1; i++) {
1268 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1269 unchanged++;
1270 if (pos == -1)
1271 pos = i;
1272 }
1273 }
1274 if (unchanged) {
1275 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1276 bufObj->Name, unchanged, bufObj->Size, pos);
1277 }
1278 }
1279 #endif
1280
1281 status = ctx->Driver.UnmapBuffer( ctx, bufObj );
1282 bufObj->AccessFlags = 0;
1283 ASSERT(bufObj->Pointer == NULL);
1284 ASSERT(bufObj->Offset == 0);
1285 ASSERT(bufObj->Length == 0);
1286
1287 return status;
1288 }
1289
1290
1291 void GLAPIENTRY
1292 _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
1293 {
1294 GET_CURRENT_CONTEXT(ctx);
1295 struct gl_buffer_object *bufObj;
1296 ASSERT_OUTSIDE_BEGIN_END(ctx);
1297
1298 bufObj = get_buffer(ctx, "glGetBufferParameterivARB", target);
1299 if (!bufObj)
1300 return;
1301
1302 switch (pname) {
1303 case GL_BUFFER_SIZE_ARB:
1304 *params = (GLint) bufObj->Size;
1305 return;
1306 case GL_BUFFER_USAGE_ARB:
1307 *params = bufObj->Usage;
1308 return;
1309 case GL_BUFFER_ACCESS_ARB:
1310 *params = simplified_access_mode(ctx, bufObj->AccessFlags);
1311 return;
1312 case GL_BUFFER_MAPPED_ARB:
1313 *params = _mesa_bufferobj_mapped(bufObj);
1314 return;
1315 case GL_BUFFER_ACCESS_FLAGS:
1316 if (!ctx->Extensions.ARB_map_buffer_range)
1317 goto invalid_pname;
1318 *params = bufObj->AccessFlags;
1319 return;
1320 case GL_BUFFER_MAP_OFFSET:
1321 if (!ctx->Extensions.ARB_map_buffer_range)
1322 goto invalid_pname;
1323 *params = (GLint) bufObj->Offset;
1324 return;
1325 case GL_BUFFER_MAP_LENGTH:
1326 if (!ctx->Extensions.ARB_map_buffer_range)
1327 goto invalid_pname;
1328 *params = (GLint) bufObj->Length;
1329 return;
1330 default:
1331 ; /* fall-through */
1332 }
1333
1334 invalid_pname:
1335 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname=%s)",
1336 _mesa_lookup_enum_by_nr(pname));
1337 }
1338
1339
1340 /**
1341 * New in GL 3.2
1342 * This is pretty much a duplicate of GetBufferParameteriv() but the
1343 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1344 */
1345 void GLAPIENTRY
1346 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
1347 {
1348 GET_CURRENT_CONTEXT(ctx);
1349 struct gl_buffer_object *bufObj;
1350 ASSERT_OUTSIDE_BEGIN_END(ctx);
1351
1352 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target);
1353 if (!bufObj)
1354 return;
1355
1356 switch (pname) {
1357 case GL_BUFFER_SIZE_ARB:
1358 *params = bufObj->Size;
1359 return;
1360 case GL_BUFFER_USAGE_ARB:
1361 *params = bufObj->Usage;
1362 return;
1363 case GL_BUFFER_ACCESS_ARB:
1364 *params = simplified_access_mode(ctx, bufObj->AccessFlags);
1365 return;
1366 case GL_BUFFER_ACCESS_FLAGS:
1367 if (!ctx->Extensions.ARB_map_buffer_range)
1368 goto invalid_pname;
1369 *params = bufObj->AccessFlags;
1370 return;
1371 case GL_BUFFER_MAPPED_ARB:
1372 *params = _mesa_bufferobj_mapped(bufObj);
1373 return;
1374 case GL_BUFFER_MAP_OFFSET:
1375 if (!ctx->Extensions.ARB_map_buffer_range)
1376 goto invalid_pname;
1377 *params = bufObj->Offset;
1378 return;
1379 case GL_BUFFER_MAP_LENGTH:
1380 if (!ctx->Extensions.ARB_map_buffer_range)
1381 goto invalid_pname;
1382 *params = bufObj->Length;
1383 return;
1384 default:
1385 ; /* fall-through */
1386 }
1387
1388 invalid_pname:
1389 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
1390 _mesa_lookup_enum_by_nr(pname));
1391 }
1392
1393
1394 void GLAPIENTRY
1395 _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
1396 {
1397 GET_CURRENT_CONTEXT(ctx);
1398 struct gl_buffer_object * bufObj;
1399 ASSERT_OUTSIDE_BEGIN_END(ctx);
1400
1401 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1402 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1403 return;
1404 }
1405
1406 bufObj = get_buffer(ctx, "glGetBufferPointervARB", target);
1407 if (!bufObj)
1408 return;
1409
1410 *params = bufObj->Pointer;
1411 }
1412
1413
1414 void GLAPIENTRY
1415 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
1416 GLintptr readOffset, GLintptr writeOffset,
1417 GLsizeiptr size)
1418 {
1419 GET_CURRENT_CONTEXT(ctx);
1420 struct gl_buffer_object *src, *dst;
1421 ASSERT_OUTSIDE_BEGIN_END(ctx);
1422
1423 src = get_buffer(ctx, "glCopyBufferSubData", readTarget);
1424 if (!src)
1425 return;
1426
1427 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget);
1428 if (!dst)
1429 return;
1430
1431 if (_mesa_bufferobj_mapped(src)) {
1432 _mesa_error(ctx, GL_INVALID_OPERATION,
1433 "glCopyBufferSubData(readBuffer is mapped)");
1434 return;
1435 }
1436
1437 if (_mesa_bufferobj_mapped(dst)) {
1438 _mesa_error(ctx, GL_INVALID_OPERATION,
1439 "glCopyBufferSubData(writeBuffer is mapped)");
1440 return;
1441 }
1442
1443 if (readOffset < 0) {
1444 _mesa_error(ctx, GL_INVALID_VALUE,
1445 "glCopyBufferSubData(readOffset = %d)", (int) readOffset);
1446 return;
1447 }
1448
1449 if (writeOffset < 0) {
1450 _mesa_error(ctx, GL_INVALID_VALUE,
1451 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset);
1452 return;
1453 }
1454
1455 if (size < 0) {
1456 _mesa_error(ctx, GL_INVALID_VALUE,
1457 "glCopyBufferSubData(writeOffset = %d)", (int) size);
1458 return;
1459 }
1460
1461 if (readOffset + size > src->Size) {
1462 _mesa_error(ctx, GL_INVALID_VALUE,
1463 "glCopyBufferSubData(readOffset + size = %d)",
1464 (int) (readOffset + size));
1465 return;
1466 }
1467
1468 if (writeOffset + size > dst->Size) {
1469 _mesa_error(ctx, GL_INVALID_VALUE,
1470 "glCopyBufferSubData(writeOffset + size = %d)",
1471 (int) (writeOffset + size));
1472 return;
1473 }
1474
1475 if (src == dst) {
1476 if (readOffset + size <= writeOffset) {
1477 /* OK */
1478 }
1479 else if (writeOffset + size <= readOffset) {
1480 /* OK */
1481 }
1482 else {
1483 /* overlapping src/dst is illegal */
1484 _mesa_error(ctx, GL_INVALID_VALUE,
1485 "glCopyBufferSubData(overlapping src/dst)");
1486 return;
1487 }
1488 }
1489
1490 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
1491 }
1492
1493
1494 /**
1495 * See GL_ARB_map_buffer_range spec
1496 */
1497 void * GLAPIENTRY
1498 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
1499 GLbitfield access)
1500 {
1501 GET_CURRENT_CONTEXT(ctx);
1502 struct gl_buffer_object *bufObj;
1503 void *map;
1504
1505 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1506
1507 if (!ctx->Extensions.ARB_map_buffer_range) {
1508 _mesa_error(ctx, GL_INVALID_OPERATION,
1509 "glMapBufferRange(extension not supported)");
1510 return NULL;
1511 }
1512
1513 if (offset < 0) {
1514 _mesa_error(ctx, GL_INVALID_VALUE,
1515 "glMapBufferRange(offset = %ld)", (long)offset);
1516 return NULL;
1517 }
1518
1519 if (length < 0) {
1520 _mesa_error(ctx, GL_INVALID_VALUE,
1521 "glMapBufferRange(length = %ld)", (long)length);
1522 return NULL;
1523 }
1524
1525 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
1526 *
1527 * "An INVALID_OPERATION error is generated for any of the following
1528 * conditions:
1529 *
1530 * * <length> is zero."
1531 */
1532 if (_mesa_is_gles(ctx) && length == 0) {
1533 _mesa_error(ctx, GL_INVALID_OPERATION,
1534 "glMapBufferRange(length = 0)");
1535 return NULL;
1536 }
1537
1538 if (access & ~(GL_MAP_READ_BIT |
1539 GL_MAP_WRITE_BIT |
1540 GL_MAP_INVALIDATE_RANGE_BIT |
1541 GL_MAP_INVALIDATE_BUFFER_BIT |
1542 GL_MAP_FLUSH_EXPLICIT_BIT |
1543 GL_MAP_UNSYNCHRONIZED_BIT)) {
1544 /* generate an error if any undefind bit is set */
1545 _mesa_error(ctx, GL_INVALID_VALUE, "glMapBufferRange(access)");
1546 return NULL;
1547 }
1548
1549 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
1550 _mesa_error(ctx, GL_INVALID_OPERATION,
1551 "glMapBufferRange(access indicates neither read or write)");
1552 return NULL;
1553 }
1554
1555 if ((access & GL_MAP_READ_BIT) &&
1556 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
1557 GL_MAP_INVALIDATE_BUFFER_BIT |
1558 GL_MAP_UNSYNCHRONIZED_BIT))) {
1559 _mesa_error(ctx, GL_INVALID_OPERATION,
1560 "glMapBufferRange(invalid access flags)");
1561 return NULL;
1562 }
1563
1564 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
1565 ((access & GL_MAP_WRITE_BIT) == 0)) {
1566 _mesa_error(ctx, GL_INVALID_OPERATION,
1567 "glMapBufferRange(invalid access flags)");
1568 return NULL;
1569 }
1570
1571 bufObj = get_buffer(ctx, "glMapBufferRange", target);
1572 if (!bufObj)
1573 return NULL;
1574
1575 if (offset + length > bufObj->Size) {
1576 _mesa_error(ctx, GL_INVALID_VALUE,
1577 "glMapBufferRange(offset + length > size)");
1578 return NULL;
1579 }
1580
1581 if (_mesa_bufferobj_mapped(bufObj)) {
1582 _mesa_error(ctx, GL_INVALID_OPERATION,
1583 "glMapBufferRange(buffer already mapped)");
1584 return NULL;
1585 }
1586
1587 if (!bufObj->Size) {
1588 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1589 "glMapBufferRange(buffer size = 0)");
1590 return NULL;
1591 }
1592
1593 /* Mapping zero bytes should return a non-null pointer. */
1594 if (!length) {
1595 static long dummy = 0;
1596 bufObj->Pointer = &dummy;
1597 bufObj->Length = length;
1598 bufObj->Offset = offset;
1599 bufObj->AccessFlags = access;
1600 return bufObj->Pointer;
1601 }
1602
1603 ASSERT(ctx->Driver.MapBufferRange);
1604 map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj);
1605 if (!map) {
1606 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1607 }
1608 else {
1609 /* The driver callback should have set all these fields.
1610 * This is important because other modules (like VBO) might call
1611 * the driver function directly.
1612 */
1613 ASSERT(bufObj->Pointer == map);
1614 ASSERT(bufObj->Length == length);
1615 ASSERT(bufObj->Offset == offset);
1616 ASSERT(bufObj->AccessFlags == access);
1617 }
1618
1619 return map;
1620 }
1621
1622
1623 /**
1624 * See GL_ARB_map_buffer_range spec
1625 */
1626 void GLAPIENTRY
1627 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
1628 {
1629 GET_CURRENT_CONTEXT(ctx);
1630 struct gl_buffer_object *bufObj;
1631 ASSERT_OUTSIDE_BEGIN_END(ctx);
1632
1633 if (!ctx->Extensions.ARB_map_buffer_range) {
1634 _mesa_error(ctx, GL_INVALID_OPERATION,
1635 "glFlushMappedBufferRange(extension not supported)");
1636 return;
1637 }
1638
1639 if (offset < 0) {
1640 _mesa_error(ctx, GL_INVALID_VALUE,
1641 "glFlushMappedBufferRange(offset = %ld)", (long)offset);
1642 return;
1643 }
1644
1645 if (length < 0) {
1646 _mesa_error(ctx, GL_INVALID_VALUE,
1647 "glFlushMappedBufferRange(length = %ld)", (long)length);
1648 return;
1649 }
1650
1651 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target);
1652 if (!bufObj)
1653 return;
1654
1655 if (!_mesa_bufferobj_mapped(bufObj)) {
1656 /* buffer is not mapped */
1657 _mesa_error(ctx, GL_INVALID_OPERATION,
1658 "glFlushMappedBufferRange(buffer is not mapped)");
1659 return;
1660 }
1661
1662 if ((bufObj->AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
1663 _mesa_error(ctx, GL_INVALID_OPERATION,
1664 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1665 return;
1666 }
1667
1668 if (offset + length > bufObj->Length) {
1669 _mesa_error(ctx, GL_INVALID_VALUE,
1670 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1671 (long)offset, (long)length, (long)bufObj->Length);
1672 return;
1673 }
1674
1675 ASSERT(bufObj->AccessFlags & GL_MAP_WRITE_BIT);
1676
1677 if (ctx->Driver.FlushMappedBufferRange)
1678 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj);
1679 }
1680
1681
1682 static GLenum
1683 buffer_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
1684 {
1685 struct gl_buffer_object *bufObj;
1686 GLenum retval;
1687
1688 bufObj = _mesa_lookup_bufferobj(ctx, name);
1689 if (!bufObj) {
1690 _mesa_error(ctx, GL_INVALID_VALUE,
1691 "glObjectPurgeable(name = 0x%x)", name);
1692 return 0;
1693 }
1694 if (!_mesa_is_bufferobj(bufObj)) {
1695 _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
1696 return 0;
1697 }
1698
1699 if (bufObj->Purgeable) {
1700 _mesa_error(ctx, GL_INVALID_OPERATION,
1701 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1702 return GL_VOLATILE_APPLE;
1703 }
1704
1705 bufObj->Purgeable = GL_TRUE;
1706
1707 retval = GL_VOLATILE_APPLE;
1708 if (ctx->Driver.BufferObjectPurgeable)
1709 retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
1710
1711 return retval;
1712 }
1713
1714
1715 static GLenum
1716 renderbuffer_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
1717 {
1718 struct gl_renderbuffer *bufObj;
1719 GLenum retval;
1720
1721 bufObj = _mesa_lookup_renderbuffer(ctx, name);
1722 if (!bufObj) {
1723 _mesa_error(ctx, GL_INVALID_VALUE,
1724 "glObjectUnpurgeable(name = 0x%x)", name);
1725 return 0;
1726 }
1727
1728 if (bufObj->Purgeable) {
1729 _mesa_error(ctx, GL_INVALID_OPERATION,
1730 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1731 return GL_VOLATILE_APPLE;
1732 }
1733
1734 bufObj->Purgeable = GL_TRUE;
1735
1736 retval = GL_VOLATILE_APPLE;
1737 if (ctx->Driver.RenderObjectPurgeable)
1738 retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
1739
1740 return retval;
1741 }
1742
1743
1744 static GLenum
1745 texture_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
1746 {
1747 struct gl_texture_object *bufObj;
1748 GLenum retval;
1749
1750 bufObj = _mesa_lookup_texture(ctx, name);
1751 if (!bufObj) {
1752 _mesa_error(ctx, GL_INVALID_VALUE,
1753 "glObjectPurgeable(name = 0x%x)", name);
1754 return 0;
1755 }
1756
1757 if (bufObj->Purgeable) {
1758 _mesa_error(ctx, GL_INVALID_OPERATION,
1759 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1760 return GL_VOLATILE_APPLE;
1761 }
1762
1763 bufObj->Purgeable = GL_TRUE;
1764
1765 retval = GL_VOLATILE_APPLE;
1766 if (ctx->Driver.TextureObjectPurgeable)
1767 retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
1768
1769 return retval;
1770 }
1771
1772
1773 GLenum GLAPIENTRY
1774 _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
1775 {
1776 GLenum retval;
1777
1778 GET_CURRENT_CONTEXT(ctx);
1779 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1780
1781 if (name == 0) {
1782 _mesa_error(ctx, GL_INVALID_VALUE,
1783 "glObjectPurgeable(name = 0x%x)", name);
1784 return 0;
1785 }
1786
1787 switch (option) {
1788 case GL_VOLATILE_APPLE:
1789 case GL_RELEASED_APPLE:
1790 /* legal */
1791 break;
1792 default:
1793 _mesa_error(ctx, GL_INVALID_ENUM,
1794 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1795 name, option);
1796 return 0;
1797 }
1798
1799 switch (objectType) {
1800 case GL_TEXTURE:
1801 retval = texture_object_purgeable(ctx, name, option);
1802 break;
1803 case GL_RENDERBUFFER_EXT:
1804 retval = renderbuffer_purgeable(ctx, name, option);
1805 break;
1806 case GL_BUFFER_OBJECT_APPLE:
1807 retval = buffer_object_purgeable(ctx, name, option);
1808 break;
1809 default:
1810 _mesa_error(ctx, GL_INVALID_ENUM,
1811 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1812 name, objectType);
1813 return 0;
1814 }
1815
1816 /* In strict conformance to the spec, we must only return VOLATILE when
1817 * when passed the VOLATILE option. Madness.
1818 *
1819 * XXX First fix the spec, then fix me.
1820 */
1821 return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
1822 }
1823
1824
1825 static GLenum
1826 buffer_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
1827 {
1828 struct gl_buffer_object *bufObj;
1829 GLenum retval;
1830
1831 bufObj = _mesa_lookup_bufferobj(ctx, name);
1832 if (!bufObj) {
1833 _mesa_error(ctx, GL_INVALID_VALUE,
1834 "glObjectUnpurgeable(name = 0x%x)", name);
1835 return 0;
1836 }
1837
1838 if (! bufObj->Purgeable) {
1839 _mesa_error(ctx, GL_INVALID_OPERATION,
1840 "glObjectUnpurgeable(name = 0x%x) object is "
1841 " already \"unpurged\"", name);
1842 return 0;
1843 }
1844
1845 bufObj->Purgeable = GL_FALSE;
1846
1847 retval = option;
1848 if (ctx->Driver.BufferObjectUnpurgeable)
1849 retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
1850
1851 return retval;
1852 }
1853
1854
1855 static GLenum
1856 renderbuffer_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
1857 {
1858 struct gl_renderbuffer *bufObj;
1859 GLenum retval;
1860
1861 bufObj = _mesa_lookup_renderbuffer(ctx, name);
1862 if (!bufObj) {
1863 _mesa_error(ctx, GL_INVALID_VALUE,
1864 "glObjectUnpurgeable(name = 0x%x)", name);
1865 return 0;
1866 }
1867
1868 if (! bufObj->Purgeable) {
1869 _mesa_error(ctx, GL_INVALID_OPERATION,
1870 "glObjectUnpurgeable(name = 0x%x) object is "
1871 " already \"unpurged\"", name);
1872 return 0;
1873 }
1874
1875 bufObj->Purgeable = GL_FALSE;
1876
1877 retval = option;
1878 if (ctx->Driver.RenderObjectUnpurgeable)
1879 retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
1880
1881 return retval;
1882 }
1883
1884
1885 static GLenum
1886 texture_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
1887 {
1888 struct gl_texture_object *bufObj;
1889 GLenum retval;
1890
1891 bufObj = _mesa_lookup_texture(ctx, name);
1892 if (!bufObj) {
1893 _mesa_error(ctx, GL_INVALID_VALUE,
1894 "glObjectUnpurgeable(name = 0x%x)", name);
1895 return 0;
1896 }
1897
1898 if (! bufObj->Purgeable) {
1899 _mesa_error(ctx, GL_INVALID_OPERATION,
1900 "glObjectUnpurgeable(name = 0x%x) object is"
1901 " already \"unpurged\"", name);
1902 return 0;
1903 }
1904
1905 bufObj->Purgeable = GL_FALSE;
1906
1907 retval = option;
1908 if (ctx->Driver.TextureObjectUnpurgeable)
1909 retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
1910
1911 return retval;
1912 }
1913
1914
1915 GLenum GLAPIENTRY
1916 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
1917 {
1918 GET_CURRENT_CONTEXT(ctx);
1919 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1920
1921 if (name == 0) {
1922 _mesa_error(ctx, GL_INVALID_VALUE,
1923 "glObjectUnpurgeable(name = 0x%x)", name);
1924 return 0;
1925 }
1926
1927 switch (option) {
1928 case GL_RETAINED_APPLE:
1929 case GL_UNDEFINED_APPLE:
1930 /* legal */
1931 break;
1932 default:
1933 _mesa_error(ctx, GL_INVALID_ENUM,
1934 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1935 name, option);
1936 return 0;
1937 }
1938
1939 switch (objectType) {
1940 case GL_BUFFER_OBJECT_APPLE:
1941 return buffer_object_unpurgeable(ctx, name, option);
1942 case GL_TEXTURE:
1943 return texture_object_unpurgeable(ctx, name, option);
1944 case GL_RENDERBUFFER_EXT:
1945 return renderbuffer_unpurgeable(ctx, name, option);
1946 default:
1947 _mesa_error(ctx, GL_INVALID_ENUM,
1948 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1949 name, objectType);
1950 return 0;
1951 }
1952 }
1953
1954
1955 static void
1956 get_buffer_object_parameteriv(struct gl_context *ctx, GLuint name,
1957 GLenum pname, GLint *params)
1958 {
1959 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
1960 if (!bufObj) {
1961 _mesa_error(ctx, GL_INVALID_VALUE,
1962 "glGetObjectParameteriv(name = 0x%x) invalid object", name);
1963 return;
1964 }
1965
1966 switch (pname) {
1967 case GL_PURGEABLE_APPLE:
1968 *params = bufObj->Purgeable;
1969 break;
1970 default:
1971 _mesa_error(ctx, GL_INVALID_ENUM,
1972 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1973 name, pname);
1974 break;
1975 }
1976 }
1977
1978
1979 static void
1980 get_renderbuffer_parameteriv(struct gl_context *ctx, GLuint name,
1981 GLenum pname, GLint *params)
1982 {
1983 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
1984 if (!rb) {
1985 _mesa_error(ctx, GL_INVALID_VALUE,
1986 "glObjectUnpurgeable(name = 0x%x)", name);
1987 return;
1988 }
1989
1990 switch (pname) {
1991 case GL_PURGEABLE_APPLE:
1992 *params = rb->Purgeable;
1993 break;
1994 default:
1995 _mesa_error(ctx, GL_INVALID_ENUM,
1996 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1997 name, pname);
1998 break;
1999 }
2000 }
2001
2002
2003 static void
2004 get_texture_object_parameteriv(struct gl_context *ctx, GLuint name,
2005 GLenum pname, GLint *params)
2006 {
2007 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
2008 if (!texObj) {
2009 _mesa_error(ctx, GL_INVALID_VALUE,
2010 "glObjectUnpurgeable(name = 0x%x)", name);
2011 return;
2012 }
2013
2014 switch (pname) {
2015 case GL_PURGEABLE_APPLE:
2016 *params = texObj->Purgeable;
2017 break;
2018 default:
2019 _mesa_error(ctx, GL_INVALID_ENUM,
2020 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2021 name, pname);
2022 break;
2023 }
2024 }
2025
2026
2027 void GLAPIENTRY
2028 _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
2029 GLint *params)
2030 {
2031 GET_CURRENT_CONTEXT(ctx);
2032
2033 if (name == 0) {
2034 _mesa_error(ctx, GL_INVALID_VALUE,
2035 "glGetObjectParameteriv(name = 0x%x)", name);
2036 return;
2037 }
2038
2039 switch (objectType) {
2040 case GL_TEXTURE:
2041 get_texture_object_parameteriv(ctx, name, pname, params);
2042 break;
2043 case GL_BUFFER_OBJECT_APPLE:
2044 get_buffer_object_parameteriv(ctx, name, pname, params);
2045 break;
2046 case GL_RENDERBUFFER_EXT:
2047 get_renderbuffer_parameteriv(ctx, name, pname, params);
2048 break;
2049 default:
2050 _mesa_error(ctx, GL_INVALID_ENUM,
2051 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2052 name, objectType);
2053 }
2054 }
2055
2056 static void
2057 set_ubo_binding(struct gl_context *ctx,
2058 int index,
2059 struct gl_buffer_object *bufObj,
2060 GLintptr offset,
2061 GLsizeiptr size,
2062 GLboolean autoSize)
2063 {
2064 struct gl_uniform_buffer_binding *binding;
2065
2066 binding = &ctx->UniformBufferBindings[index];
2067 if (binding->BufferObject == bufObj &&
2068 binding->Offset == offset &&
2069 binding->Size == size &&
2070 binding->AutomaticSize == autoSize) {
2071 return;
2072 }
2073
2074 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
2075
2076 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2077 binding->Offset = offset;
2078 binding->Size = size;
2079 binding->AutomaticSize = autoSize;
2080 }
2081
2082 /**
2083 * Bind a region of a buffer object to a uniform block binding point.
2084 * \param index the uniform buffer binding point index
2085 * \param bufObj the buffer object
2086 * \param offset offset to the start of buffer object region
2087 * \param size size of the buffer object region
2088 */
2089 static void
2090 bind_buffer_range_uniform_buffer(struct gl_context *ctx,
2091 GLuint index,
2092 struct gl_buffer_object *bufObj,
2093 GLintptr offset,
2094 GLsizeiptr size)
2095 {
2096 if (index >= ctx->Const.MaxUniformBufferBindings) {
2097 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
2098 return;
2099 }
2100
2101 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
2102 _mesa_error(ctx, GL_INVALID_VALUE,
2103 "glBindBufferRange(offset misalgned %d/%d)", (int) offset,
2104 ctx->Const.UniformBufferOffsetAlignment);
2105 return;
2106 }
2107
2108 if (bufObj == ctx->Shared->NullBufferObj) {
2109 offset = -1;
2110 size = -1;
2111 }
2112
2113 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2114 set_ubo_binding(ctx, index, bufObj, offset, size, GL_FALSE);
2115 }
2116
2117
2118 /**
2119 * Bind a buffer object to a uniform block binding point.
2120 * As above, but offset = 0.
2121 */
2122 static void
2123 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
2124 GLuint index,
2125 struct gl_buffer_object *bufObj)
2126 {
2127 if (index >= ctx->Const.MaxUniformBufferBindings) {
2128 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
2129 return;
2130 }
2131
2132 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2133 if (bufObj == ctx->Shared->NullBufferObj)
2134 set_ubo_binding(ctx, index, bufObj, -1, -1, GL_TRUE);
2135 else
2136 set_ubo_binding(ctx, index, bufObj, 0, 0, GL_TRUE);
2137 }
2138
2139 void GLAPIENTRY
2140 _mesa_BindBufferRange(GLenum target, GLuint index,
2141 GLuint buffer, GLintptr offset, GLsizeiptr size)
2142 {
2143 GET_CURRENT_CONTEXT(ctx);
2144 struct gl_buffer_object *bufObj;
2145
2146 if (buffer == 0) {
2147 bufObj = ctx->Shared->NullBufferObj;
2148 } else {
2149 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2150 }
2151 if (!handle_bind_buffer_gen(ctx, target, buffer, &bufObj))
2152 return;
2153
2154 if (!bufObj) {
2155 _mesa_error(ctx, GL_INVALID_OPERATION,
2156 "glBindBufferRange(invalid buffer=%u)", buffer);
2157 return;
2158 }
2159
2160 if (buffer != 0) {
2161 if (size <= 0) {
2162 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
2163 (int) size);
2164 return;
2165 }
2166
2167 if (offset + size > bufObj->Size) {
2168 _mesa_error(ctx, GL_INVALID_VALUE,
2169 "glBindBufferRange(offset + size %d > buffer size %d)",
2170 (int) (offset + size), (int) (bufObj->Size));
2171 return;
2172 }
2173 }
2174
2175 switch (target) {
2176 case GL_TRANSFORM_FEEDBACK_BUFFER:
2177 _mesa_bind_buffer_range_transform_feedback(ctx, index, bufObj,
2178 offset, size);
2179 return;
2180 case GL_UNIFORM_BUFFER:
2181 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
2182 return;
2183 default:
2184 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
2185 return;
2186 }
2187 }
2188
2189 void GLAPIENTRY
2190 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
2191 {
2192 GET_CURRENT_CONTEXT(ctx);
2193 struct gl_buffer_object *bufObj;
2194
2195 if (buffer == 0) {
2196 bufObj = ctx->Shared->NullBufferObj;
2197 } else {
2198 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2199 }
2200 if (!handle_bind_buffer_gen(ctx, target, buffer, &bufObj))
2201 return;
2202
2203 if (!bufObj) {
2204 _mesa_error(ctx, GL_INVALID_OPERATION,
2205 "glBindBufferBase(invalid buffer=%u)", buffer);
2206 return;
2207 }
2208
2209 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
2210 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
2211 *
2212 * "BindBufferBase is equivalent to calling BindBufferRange with offset
2213 * zero and size equal to the size of buffer."
2214 *
2215 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
2216 *
2217 * "If the parameter (starting offset or size) was not specified when the
2218 * buffer object was bound, zero is returned."
2219 *
2220 * What happens if the size of the buffer changes? Does the size of the
2221 * buffer at the moment glBindBufferBase was called still play a role, like
2222 * the first quote would imply, or is the size meaningless in the
2223 * glBindBufferBase case like the second quote would suggest? The GL 4.1
2224 * core spec page 45 says:
2225 *
2226 * "It is equivalent to calling BindBufferRange with offset zero, while
2227 * size is determined by the size of the bound buffer at the time the
2228 * binding is used."
2229 *
2230 * My interpretation is that the GL 4.1 spec was a clarification of the
2231 * behavior, not a change. In particular, this choice will only make
2232 * rendering work in cases where it would have had undefined results.
2233 */
2234
2235 switch (target) {
2236 case GL_TRANSFORM_FEEDBACK_BUFFER:
2237 _mesa_bind_buffer_base_transform_feedback(ctx, index, bufObj);
2238 return;
2239 case GL_UNIFORM_BUFFER:
2240 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
2241 return;
2242 default:
2243 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
2244 return;
2245 }
2246 }
2247
2248 void GLAPIENTRY
2249 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
2250 GLsizeiptr length)
2251 {
2252 GET_CURRENT_CONTEXT(ctx);
2253 struct gl_buffer_object *bufObj;
2254 const GLintptr end = offset + length;
2255
2256 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2257 if (!bufObj) {
2258 _mesa_error(ctx, GL_INVALID_VALUE,
2259 "glInvalidateBufferSubData(name = 0x%x) invalid object",
2260 buffer);
2261 return;
2262 }
2263
2264 /* The GL_ARB_invalidate_subdata spec says:
2265 *
2266 * "An INVALID_VALUE error is generated if <offset> or <length> is
2267 * negative, or if <offset> + <length> is greater than the value of
2268 * BUFFER_SIZE."
2269 */
2270 if (end < 0 || end > bufObj->Size) {
2271 _mesa_error(ctx, GL_INVALID_VALUE,
2272 "glInvalidateBufferSubData(invalid offset or length)");
2273 return;
2274 }
2275
2276 /* The GL_ARB_invalidate_subdata spec says:
2277 *
2278 * "An INVALID_OPERATION error is generated if the buffer is currently
2279 * mapped by MapBuffer, or if the invalidate range intersects the range
2280 * currently mapped by MapBufferRange."
2281 */
2282 if (_mesa_bufferobj_mapped(bufObj)) {
2283 const GLintptr mapEnd = bufObj->Offset + bufObj->Length;
2284
2285 /* The regions do not overlap if and only if the end of the discard
2286 * region is before the mapped region or the start of the discard region
2287 * is after the mapped region.
2288 *
2289 * Note that 'end' and 'mapEnd' are the first byte *after* the discard
2290 * region and the mapped region, repsectively. It is okay for that byte
2291 * to be mapped (for 'end') or discarded (for 'mapEnd').
2292 */
2293 if (!(end <= bufObj->Offset || offset >= mapEnd)) {
2294 _mesa_error(ctx, GL_INVALID_OPERATION,
2295 "glInvalidateBufferSubData(intersection with mapped "
2296 "range)");
2297 return;
2298 }
2299 }
2300
2301 /* We don't actually do anything for this yet. Just return after
2302 * validating the parameters and generating the required errors.
2303 */
2304 return;
2305 }
2306
2307 void GLAPIENTRY
2308 _mesa_InvalidateBufferData(GLuint buffer)
2309 {
2310 GET_CURRENT_CONTEXT(ctx);
2311 struct gl_buffer_object *bufObj;
2312
2313 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2314 if (!bufObj) {
2315 _mesa_error(ctx, GL_INVALID_VALUE,
2316 "glInvalidateBufferData(name = 0x%x) invalid object",
2317 buffer);
2318 return;
2319 }
2320
2321 /* The GL_ARB_invalidate_subdata spec says:
2322 *
2323 * "An INVALID_OPERATION error is generated if the buffer is currently
2324 * mapped by MapBuffer, or if the invalidate range intersects the range
2325 * currently mapped by MapBufferRange."
2326 */
2327 if (_mesa_bufferobj_mapped(bufObj)) {
2328 _mesa_error(ctx, GL_INVALID_OPERATION,
2329 "glInvalidateBufferData(intersection with mapped "
2330 "range)");
2331 return;
2332 }
2333
2334 /* We don't actually do anything for this yet. Just return after
2335 * validating the parameters and generating the required errors.
2336 */
2337 return;
2338 }