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