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