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