mesa: Ignore size and offset parameters for BindBufferRange when buffer is 0
[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 bool
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 && ctx->API == API_OPENGL_CORE) {
672 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindBuffer(non-gen name)");
673 return false;
674 }
675
676 if (!buf || buf == &DummyBufferObject) {
677 /* If this is a new buffer object id, or one which was generated but
678 * never used before, allocate a buffer object now.
679 */
680 ASSERT(ctx->Driver.NewBufferObject);
681 buf = ctx->Driver.NewBufferObject(ctx, buffer, target);
682 if (!buf) {
683 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindBufferARB");
684 return false;
685 }
686 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, buf);
687 *buf_handle = buf;
688 }
689
690 return true;
691 }
692
693 /**
694 * Bind the specified target to buffer for the specified context.
695 * Called by glBindBuffer() and other functions.
696 */
697 static void
698 bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
699 {
700 struct gl_buffer_object *oldBufObj;
701 struct gl_buffer_object *newBufObj = NULL;
702 struct gl_buffer_object **bindTarget = NULL;
703
704 bindTarget = get_buffer_target(ctx, target);
705 if (!bindTarget) {
706 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
707 return;
708 }
709
710 /* Get pointer to old buffer object (to be unbound) */
711 oldBufObj = *bindTarget;
712 if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
713 return; /* rebinding the same buffer object- no change */
714
715 /*
716 * Get pointer to new buffer object (newBufObj)
717 */
718 if (buffer == 0) {
719 /* The spec says there's not a buffer object named 0, but we use
720 * one internally because it simplifies things.
721 */
722 newBufObj = ctx->Shared->NullBufferObj;
723 }
724 else {
725 /* non-default buffer object */
726 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
727 if (!handle_bind_buffer_gen(ctx, target, buffer, &newBufObj))
728 return;
729 }
730
731 /* bind new buffer */
732 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
733
734 /* Pass BindBuffer call to device driver */
735 if (ctx->Driver.BindBuffer)
736 ctx->Driver.BindBuffer( ctx, target, newBufObj );
737 }
738
739
740 /**
741 * Update the default buffer objects in the given context to reference those
742 * specified in the shared state and release those referencing the old
743 * shared state.
744 */
745 void
746 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
747 {
748 /* Bind the NullBufferObj to remove references to those
749 * in the shared context hash table.
750 */
751 bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
752 bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
753 bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
754 bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
755 }
756
757
758
759 /**
760 * Return the gl_buffer_object for the given ID.
761 * Always return NULL for ID 0.
762 */
763 struct gl_buffer_object *
764 _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
765 {
766 if (buffer == 0)
767 return NULL;
768 else
769 return (struct gl_buffer_object *)
770 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
771 }
772
773
774 /**
775 * If *ptr points to obj, set ptr = the Null/default buffer object.
776 * This is a helper for buffer object deletion.
777 * The GL spec says that deleting a buffer object causes it to get
778 * unbound from all arrays in the current context.
779 */
780 static void
781 unbind(struct gl_context *ctx,
782 struct gl_buffer_object **ptr,
783 struct gl_buffer_object *obj)
784 {
785 if (*ptr == obj) {
786 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
787 }
788 }
789
790
791 /**
792 * Plug default/fallback buffer object functions into the device
793 * driver hooks.
794 */
795 void
796 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
797 {
798 /* GL_ARB_vertex/pixel_buffer_object */
799 driver->NewBufferObject = _mesa_new_buffer_object;
800 driver->DeleteBuffer = _mesa_delete_buffer_object;
801 driver->BindBuffer = NULL;
802 driver->BufferData = _mesa_buffer_data;
803 driver->BufferSubData = _mesa_buffer_subdata;
804 driver->GetBufferSubData = _mesa_buffer_get_subdata;
805 driver->UnmapBuffer = _mesa_buffer_unmap;
806
807 /* GL_ARB_map_buffer_range */
808 driver->MapBufferRange = _mesa_buffer_map_range;
809 driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
810
811 /* GL_ARB_copy_buffer */
812 driver->CopyBufferSubData = _mesa_copy_buffer_subdata;
813 }
814
815
816
817 /**********************************************************************/
818 /* API Functions */
819 /**********************************************************************/
820
821 void GLAPIENTRY
822 _mesa_BindBuffer(GLenum target, GLuint buffer)
823 {
824 GET_CURRENT_CONTEXT(ctx);
825 ASSERT_OUTSIDE_BEGIN_END(ctx);
826
827 if (MESA_VERBOSE & VERBOSE_API)
828 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
829 _mesa_lookup_enum_by_nr(target), buffer);
830
831 bind_buffer_object(ctx, target, buffer);
832 }
833
834
835 /**
836 * Delete a set of buffer objects.
837 *
838 * \param n Number of buffer objects to delete.
839 * \param ids Array of \c n buffer object IDs.
840 */
841 void GLAPIENTRY
842 _mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
843 {
844 GET_CURRENT_CONTEXT(ctx);
845 GLsizei i;
846 ASSERT_OUTSIDE_BEGIN_END(ctx);
847 FLUSH_VERTICES(ctx, 0);
848
849 if (n < 0) {
850 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
851 return;
852 }
853
854 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
855
856 for (i = 0; i < n; i++) {
857 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
858 if (bufObj) {
859 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
860 GLuint j;
861
862 ASSERT(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
863
864 if (_mesa_bufferobj_mapped(bufObj)) {
865 /* if mapped, unmap it now */
866 ctx->Driver.UnmapBuffer(ctx, bufObj);
867 bufObj->AccessFlags = default_access_mode(ctx);
868 bufObj->Pointer = NULL;
869 }
870
871 /* unbind any vertex pointers bound to this buffer */
872 for (j = 0; j < Elements(arrayObj->VertexAttrib); j++) {
873 unbind(ctx, &arrayObj->VertexAttrib[j].BufferObj, bufObj);
874 }
875
876 if (ctx->Array.ArrayBufferObj == bufObj) {
877 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB, 0 );
878 }
879 if (arrayObj->ElementArrayBufferObj == bufObj) {
880 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
881 }
882
883 /* unbind ARB_copy_buffer binding points */
884 if (ctx->CopyReadBuffer == bufObj) {
885 _mesa_BindBuffer( GL_COPY_READ_BUFFER, 0 );
886 }
887 if (ctx->CopyWriteBuffer == bufObj) {
888 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER, 0 );
889 }
890
891 /* unbind transform feedback binding points */
892 if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
893 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER, 0 );
894 }
895 for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
896 if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
897 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 );
898 }
899 }
900
901 /* unbind UBO binding points */
902 for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
903 if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
904 _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 );
905 }
906 }
907
908 if (ctx->UniformBuffer == bufObj) {
909 _mesa_BindBuffer( GL_UNIFORM_BUFFER, 0 );
910 }
911
912 /* unbind any pixel pack/unpack pointers bound to this buffer */
913 if (ctx->Pack.BufferObj == bufObj) {
914 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT, 0 );
915 }
916 if (ctx->Unpack.BufferObj == bufObj) {
917 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
918 }
919
920 if (ctx->Texture.BufferObject == bufObj) {
921 _mesa_BindBuffer( GL_TEXTURE_BUFFER, 0 );
922 }
923
924 /* The ID is immediately freed for re-use */
925 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
926 /* Make sure we do not run into the classic ABA problem on bind.
927 * We don't want to allow re-binding a buffer object that's been
928 * "deleted" by glDeleteBuffers().
929 *
930 * The explicit rebinding to the default object in the current context
931 * prevents the above in the current context, but another context
932 * sharing the same objects might suffer from this problem.
933 * The alternative would be to do the hash lookup in any case on bind
934 * which would introduce more runtime overhead than this.
935 */
936 bufObj->DeletePending = GL_TRUE;
937 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
938 }
939 }
940
941 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
942 }
943
944
945 /**
946 * Generate a set of unique buffer object IDs and store them in \c buffer.
947 *
948 * \param n Number of IDs to generate.
949 * \param buffer Array of \c n locations to store the IDs.
950 */
951 void GLAPIENTRY
952 _mesa_GenBuffers(GLsizei n, GLuint *buffer)
953 {
954 GET_CURRENT_CONTEXT(ctx);
955 GLuint first;
956 GLint i;
957 ASSERT_OUTSIDE_BEGIN_END(ctx);
958
959 if (MESA_VERBOSE & VERBOSE_API)
960 _mesa_debug(ctx, "glGenBuffers(%d)\n", n);
961
962 if (n < 0) {
963 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
964 return;
965 }
966
967 if (!buffer) {
968 return;
969 }
970
971 /*
972 * This must be atomic (generation and allocation of buffer object IDs)
973 */
974 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
975
976 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
977
978 /* Insert the ID and pointer to dummy buffer object into hash table */
979 for (i = 0; i < n; i++) {
980 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i,
981 &DummyBufferObject);
982 buffer[i] = first + i;
983 }
984
985 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
986 }
987
988
989 /**
990 * Determine if ID is the name of a buffer object.
991 *
992 * \param id ID of the potential buffer object.
993 * \return \c GL_TRUE if \c id is the name of a buffer object,
994 * \c GL_FALSE otherwise.
995 */
996 GLboolean GLAPIENTRY
997 _mesa_IsBuffer(GLuint id)
998 {
999 struct gl_buffer_object *bufObj;
1000 GET_CURRENT_CONTEXT(ctx);
1001 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1002
1003 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1004 bufObj = _mesa_lookup_bufferobj(ctx, id);
1005 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1006
1007 return bufObj && bufObj != &DummyBufferObject;
1008 }
1009
1010
1011 void GLAPIENTRY
1012 _mesa_BufferData(GLenum target, GLsizeiptrARB size,
1013 const GLvoid * data, GLenum usage)
1014 {
1015 GET_CURRENT_CONTEXT(ctx);
1016 struct gl_buffer_object *bufObj;
1017 bool valid_usage;
1018 ASSERT_OUTSIDE_BEGIN_END(ctx);
1019
1020 if (MESA_VERBOSE & VERBOSE_API)
1021 _mesa_debug(ctx, "glBufferData(%s, %ld, %p, %s)\n",
1022 _mesa_lookup_enum_by_nr(target),
1023 (long int) size, data,
1024 _mesa_lookup_enum_by_nr(usage));
1025
1026 if (size < 0) {
1027 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
1028 return;
1029 }
1030
1031 switch (usage) {
1032 case GL_STREAM_DRAW_ARB:
1033 valid_usage = (ctx->API != API_OPENGLES);
1034 break;
1035
1036 case GL_STATIC_DRAW_ARB:
1037 case GL_DYNAMIC_DRAW_ARB:
1038 valid_usage = true;
1039 break;
1040
1041 case GL_STREAM_READ_ARB:
1042 case GL_STREAM_COPY_ARB:
1043 case GL_STATIC_READ_ARB:
1044 case GL_STATIC_COPY_ARB:
1045 case GL_DYNAMIC_READ_ARB:
1046 case GL_DYNAMIC_COPY_ARB:
1047 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
1048 break;
1049
1050 default:
1051 valid_usage = false;
1052 break;
1053 }
1054
1055 if (!valid_usage) {
1056 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferData(usage)");
1057 return;
1058 }
1059
1060 bufObj = get_buffer(ctx, "glBufferDataARB", target);
1061 if (!bufObj)
1062 return;
1063
1064 if (_mesa_bufferobj_mapped(bufObj)) {
1065 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1066 ctx->Driver.UnmapBuffer(ctx, bufObj);
1067 bufObj->AccessFlags = default_access_mode(ctx);
1068 ASSERT(bufObj->Pointer == NULL);
1069 }
1070
1071 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1072
1073 bufObj->Written = GL_TRUE;
1074
1075 #ifdef VBO_DEBUG
1076 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1077 bufObj->Name, size, data, usage);
1078 #endif
1079
1080 #ifdef BOUNDS_CHECK
1081 size += 100;
1082 #endif
1083
1084 ASSERT(ctx->Driver.BufferData);
1085 if (!ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj )) {
1086 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB()");
1087 }
1088 }
1089
1090
1091 void GLAPIENTRY
1092 _mesa_BufferSubData(GLenum target, GLintptrARB offset,
1093 GLsizeiptrARB size, const GLvoid * data)
1094 {
1095 GET_CURRENT_CONTEXT(ctx);
1096 struct gl_buffer_object *bufObj;
1097 ASSERT_OUTSIDE_BEGIN_END(ctx);
1098
1099 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1100 "glBufferSubDataARB" );
1101 if (!bufObj) {
1102 /* error already recorded */
1103 return;
1104 }
1105
1106 if (size == 0)
1107 return;
1108
1109 bufObj->Written = GL_TRUE;
1110
1111 ASSERT(ctx->Driver.BufferSubData);
1112 ctx->Driver.BufferSubData( ctx, offset, size, data, bufObj );
1113 }
1114
1115
1116 void GLAPIENTRY
1117 _mesa_GetBufferSubData(GLenum target, GLintptrARB offset,
1118 GLsizeiptrARB size, void * data)
1119 {
1120 GET_CURRENT_CONTEXT(ctx);
1121 struct gl_buffer_object *bufObj;
1122 ASSERT_OUTSIDE_BEGIN_END(ctx);
1123
1124 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1125 "glGetBufferSubDataARB" );
1126 if (!bufObj) {
1127 /* error already recorded */
1128 return;
1129 }
1130
1131 ASSERT(ctx->Driver.GetBufferSubData);
1132 ctx->Driver.GetBufferSubData( ctx, offset, size, data, bufObj );
1133 }
1134
1135
1136 void * GLAPIENTRY
1137 _mesa_MapBuffer(GLenum target, GLenum access)
1138 {
1139 GET_CURRENT_CONTEXT(ctx);
1140 struct gl_buffer_object * bufObj;
1141 GLbitfield accessFlags;
1142 void *map;
1143 bool valid_access;
1144
1145 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1146
1147 switch (access) {
1148 case GL_READ_ONLY_ARB:
1149 accessFlags = GL_MAP_READ_BIT;
1150 valid_access = _mesa_is_desktop_gl(ctx);
1151 break;
1152 case GL_WRITE_ONLY_ARB:
1153 accessFlags = GL_MAP_WRITE_BIT;
1154 valid_access = true;
1155 break;
1156 case GL_READ_WRITE_ARB:
1157 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1158 valid_access = _mesa_is_desktop_gl(ctx);
1159 break;
1160 default:
1161 valid_access = false;
1162 break;
1163 }
1164
1165 if (!valid_access) {
1166 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
1167 return NULL;
1168 }
1169
1170 bufObj = get_buffer(ctx, "glMapBufferARB", target);
1171 if (!bufObj)
1172 return NULL;
1173
1174 if (_mesa_bufferobj_mapped(bufObj)) {
1175 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
1176 return NULL;
1177 }
1178
1179 if (!bufObj->Size) {
1180 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1181 "glMapBuffer(buffer size = 0)");
1182 return NULL;
1183 }
1184
1185 ASSERT(ctx->Driver.MapBufferRange);
1186 map = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size, accessFlags, bufObj);
1187 if (!map) {
1188 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1189 return NULL;
1190 }
1191 else {
1192 /* The driver callback should have set these fields.
1193 * This is important because other modules (like VBO) might call
1194 * the driver function directly.
1195 */
1196 ASSERT(bufObj->Pointer == map);
1197 ASSERT(bufObj->Length == bufObj->Size);
1198 ASSERT(bufObj->Offset == 0);
1199 bufObj->AccessFlags = accessFlags;
1200 }
1201
1202 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
1203 bufObj->Written = GL_TRUE;
1204
1205 #ifdef VBO_DEBUG
1206 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1207 bufObj->Name, bufObj->Size, access);
1208 if (access == GL_WRITE_ONLY_ARB) {
1209 GLuint i;
1210 GLubyte *b = (GLubyte *) bufObj->Pointer;
1211 for (i = 0; i < bufObj->Size; i++)
1212 b[i] = i & 0xff;
1213 }
1214 #endif
1215
1216 #ifdef BOUNDS_CHECK
1217 {
1218 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1219 GLuint i;
1220 /* buffer is 100 bytes larger than requested, fill with magic value */
1221 for (i = 0; i < 100; i++) {
1222 buf[bufObj->Size - i - 1] = 123;
1223 }
1224 }
1225 #endif
1226
1227 return bufObj->Pointer;
1228 }
1229
1230
1231 GLboolean GLAPIENTRY
1232 _mesa_UnmapBuffer(GLenum target)
1233 {
1234 GET_CURRENT_CONTEXT(ctx);
1235 struct gl_buffer_object *bufObj;
1236 GLboolean status = GL_TRUE;
1237 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1238
1239 bufObj = get_buffer(ctx, "glUnmapBufferARB", target);
1240 if (!bufObj)
1241 return GL_FALSE;
1242
1243 if (!_mesa_bufferobj_mapped(bufObj)) {
1244 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1245 return GL_FALSE;
1246 }
1247
1248 #ifdef BOUNDS_CHECK
1249 if (bufObj->Access != GL_READ_ONLY_ARB) {
1250 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1251 GLuint i;
1252 /* check that last 100 bytes are still = magic value */
1253 for (i = 0; i < 100; i++) {
1254 GLuint pos = bufObj->Size - i - 1;
1255 if (buf[pos] != 123) {
1256 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1257 " at position %d (value = %u)\n",
1258 pos, buf[pos]);
1259 }
1260 }
1261 }
1262 #endif
1263
1264 #ifdef VBO_DEBUG
1265 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1266 GLuint i, unchanged = 0;
1267 GLubyte *b = (GLubyte *) bufObj->Pointer;
1268 GLint pos = -1;
1269 /* check which bytes changed */
1270 for (i = 0; i < bufObj->Size - 1; i++) {
1271 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1272 unchanged++;
1273 if (pos == -1)
1274 pos = i;
1275 }
1276 }
1277 if (unchanged) {
1278 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1279 bufObj->Name, unchanged, bufObj->Size, pos);
1280 }
1281 }
1282 #endif
1283
1284 status = ctx->Driver.UnmapBuffer( ctx, bufObj );
1285 bufObj->AccessFlags = default_access_mode(ctx);
1286 ASSERT(bufObj->Pointer == NULL);
1287 ASSERT(bufObj->Offset == 0);
1288 ASSERT(bufObj->Length == 0);
1289
1290 return status;
1291 }
1292
1293
1294 void GLAPIENTRY
1295 _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
1296 {
1297 GET_CURRENT_CONTEXT(ctx);
1298 struct gl_buffer_object *bufObj;
1299 ASSERT_OUTSIDE_BEGIN_END(ctx);
1300
1301 bufObj = get_buffer(ctx, "glGetBufferParameterivARB", target);
1302 if (!bufObj)
1303 return;
1304
1305 switch (pname) {
1306 case GL_BUFFER_SIZE_ARB:
1307 *params = (GLint) bufObj->Size;
1308 return;
1309 case GL_BUFFER_USAGE_ARB:
1310 *params = bufObj->Usage;
1311 return;
1312 case GL_BUFFER_ACCESS_ARB:
1313 *params = simplified_access_mode(bufObj->AccessFlags);
1314 return;
1315 case GL_BUFFER_MAPPED_ARB:
1316 *params = _mesa_bufferobj_mapped(bufObj);
1317 return;
1318 case GL_BUFFER_ACCESS_FLAGS:
1319 if (!ctx->Extensions.ARB_map_buffer_range)
1320 goto invalid_pname;
1321 *params = bufObj->AccessFlags;
1322 return;
1323 case GL_BUFFER_MAP_OFFSET:
1324 if (!ctx->Extensions.ARB_map_buffer_range)
1325 goto invalid_pname;
1326 *params = (GLint) bufObj->Offset;
1327 return;
1328 case GL_BUFFER_MAP_LENGTH:
1329 if (!ctx->Extensions.ARB_map_buffer_range)
1330 goto invalid_pname;
1331 *params = (GLint) bufObj->Length;
1332 return;
1333 default:
1334 ; /* fall-through */
1335 }
1336
1337 invalid_pname:
1338 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname=%s)",
1339 _mesa_lookup_enum_by_nr(pname));
1340 }
1341
1342
1343 /**
1344 * New in GL 3.2
1345 * This is pretty much a duplicate of GetBufferParameteriv() but the
1346 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1347 */
1348 void GLAPIENTRY
1349 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
1350 {
1351 GET_CURRENT_CONTEXT(ctx);
1352 struct gl_buffer_object *bufObj;
1353 ASSERT_OUTSIDE_BEGIN_END(ctx);
1354
1355 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target);
1356 if (!bufObj)
1357 return;
1358
1359 switch (pname) {
1360 case GL_BUFFER_SIZE_ARB:
1361 *params = bufObj->Size;
1362 return;
1363 case GL_BUFFER_USAGE_ARB:
1364 *params = bufObj->Usage;
1365 return;
1366 case GL_BUFFER_ACCESS_ARB:
1367 *params = simplified_access_mode(bufObj->AccessFlags);
1368 return;
1369 case GL_BUFFER_ACCESS_FLAGS:
1370 if (!ctx->Extensions.ARB_map_buffer_range)
1371 goto invalid_pname;
1372 *params = bufObj->AccessFlags;
1373 return;
1374 case GL_BUFFER_MAPPED_ARB:
1375 *params = _mesa_bufferobj_mapped(bufObj);
1376 return;
1377 case GL_BUFFER_MAP_OFFSET:
1378 if (!ctx->Extensions.ARB_map_buffer_range)
1379 goto invalid_pname;
1380 *params = bufObj->Offset;
1381 return;
1382 case GL_BUFFER_MAP_LENGTH:
1383 if (!ctx->Extensions.ARB_map_buffer_range)
1384 goto invalid_pname;
1385 *params = bufObj->Length;
1386 return;
1387 default:
1388 ; /* fall-through */
1389 }
1390
1391 invalid_pname:
1392 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
1393 _mesa_lookup_enum_by_nr(pname));
1394 }
1395
1396
1397 void GLAPIENTRY
1398 _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
1399 {
1400 GET_CURRENT_CONTEXT(ctx);
1401 struct gl_buffer_object * bufObj;
1402 ASSERT_OUTSIDE_BEGIN_END(ctx);
1403
1404 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1405 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1406 return;
1407 }
1408
1409 bufObj = get_buffer(ctx, "glGetBufferPointervARB", target);
1410 if (!bufObj)
1411 return;
1412
1413 *params = bufObj->Pointer;
1414 }
1415
1416
1417 void GLAPIENTRY
1418 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
1419 GLintptr readOffset, GLintptr writeOffset,
1420 GLsizeiptr size)
1421 {
1422 GET_CURRENT_CONTEXT(ctx);
1423 struct gl_buffer_object *src, *dst;
1424 ASSERT_OUTSIDE_BEGIN_END(ctx);
1425
1426 src = get_buffer(ctx, "glCopyBufferSubData", readTarget);
1427 if (!src)
1428 return;
1429
1430 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget);
1431 if (!dst)
1432 return;
1433
1434 if (_mesa_bufferobj_mapped(src)) {
1435 _mesa_error(ctx, GL_INVALID_OPERATION,
1436 "glCopyBufferSubData(readBuffer is mapped)");
1437 return;
1438 }
1439
1440 if (_mesa_bufferobj_mapped(dst)) {
1441 _mesa_error(ctx, GL_INVALID_OPERATION,
1442 "glCopyBufferSubData(writeBuffer is mapped)");
1443 return;
1444 }
1445
1446 if (readOffset < 0) {
1447 _mesa_error(ctx, GL_INVALID_VALUE,
1448 "glCopyBufferSubData(readOffset = %d)", (int) readOffset);
1449 return;
1450 }
1451
1452 if (writeOffset < 0) {
1453 _mesa_error(ctx, GL_INVALID_VALUE,
1454 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset);
1455 return;
1456 }
1457
1458 if (size < 0) {
1459 _mesa_error(ctx, GL_INVALID_VALUE,
1460 "glCopyBufferSubData(writeOffset = %d)", (int) size);
1461 return;
1462 }
1463
1464 if (readOffset + size > src->Size) {
1465 _mesa_error(ctx, GL_INVALID_VALUE,
1466 "glCopyBufferSubData(readOffset + size = %d)",
1467 (int) (readOffset + size));
1468 return;
1469 }
1470
1471 if (writeOffset + size > dst->Size) {
1472 _mesa_error(ctx, GL_INVALID_VALUE,
1473 "glCopyBufferSubData(writeOffset + size = %d)",
1474 (int) (writeOffset + size));
1475 return;
1476 }
1477
1478 if (src == dst) {
1479 if (readOffset + size <= writeOffset) {
1480 /* OK */
1481 }
1482 else if (writeOffset + size <= readOffset) {
1483 /* OK */
1484 }
1485 else {
1486 /* overlapping src/dst is illegal */
1487 _mesa_error(ctx, GL_INVALID_VALUE,
1488 "glCopyBufferSubData(overlapping src/dst)");
1489 return;
1490 }
1491 }
1492
1493 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
1494 }
1495
1496
1497 /**
1498 * See GL_ARB_map_buffer_range spec
1499 */
1500 void * GLAPIENTRY
1501 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
1502 GLbitfield access)
1503 {
1504 GET_CURRENT_CONTEXT(ctx);
1505 struct gl_buffer_object *bufObj;
1506 void *map;
1507
1508 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1509
1510 if (!ctx->Extensions.ARB_map_buffer_range) {
1511 _mesa_error(ctx, GL_INVALID_OPERATION,
1512 "glMapBufferRange(extension not supported)");
1513 return NULL;
1514 }
1515
1516 if (offset < 0) {
1517 _mesa_error(ctx, GL_INVALID_VALUE,
1518 "glMapBufferRange(offset = %ld)", (long)offset);
1519 return NULL;
1520 }
1521
1522 if (length < 0) {
1523 _mesa_error(ctx, GL_INVALID_VALUE,
1524 "glMapBufferRange(length = %ld)", (long)length);
1525 return NULL;
1526 }
1527
1528 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
1529 *
1530 * "An INVALID_OPERATION error is generated for any of the following
1531 * conditions:
1532 *
1533 * * <length> is zero."
1534 */
1535 if (_mesa_is_gles(ctx) && length == 0) {
1536 _mesa_error(ctx, GL_INVALID_OPERATION,
1537 "glMapBufferRange(length = 0)");
1538 return NULL;
1539 }
1540
1541 if (access & ~(GL_MAP_READ_BIT |
1542 GL_MAP_WRITE_BIT |
1543 GL_MAP_INVALIDATE_RANGE_BIT |
1544 GL_MAP_INVALIDATE_BUFFER_BIT |
1545 GL_MAP_FLUSH_EXPLICIT_BIT |
1546 GL_MAP_UNSYNCHRONIZED_BIT)) {
1547 /* generate an error if any undefind bit is set */
1548 _mesa_error(ctx, GL_INVALID_VALUE, "glMapBufferRange(access)");
1549 return NULL;
1550 }
1551
1552 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
1553 _mesa_error(ctx, GL_INVALID_OPERATION,
1554 "glMapBufferRange(access indicates neither read or write)");
1555 return NULL;
1556 }
1557
1558 if ((access & GL_MAP_READ_BIT) &&
1559 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
1560 GL_MAP_INVALIDATE_BUFFER_BIT |
1561 GL_MAP_UNSYNCHRONIZED_BIT))) {
1562 _mesa_error(ctx, GL_INVALID_OPERATION,
1563 "glMapBufferRange(invalid access flags)");
1564 return NULL;
1565 }
1566
1567 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
1568 ((access & GL_MAP_WRITE_BIT) == 0)) {
1569 _mesa_error(ctx, GL_INVALID_OPERATION,
1570 "glMapBufferRange(invalid access flags)");
1571 return NULL;
1572 }
1573
1574 bufObj = get_buffer(ctx, "glMapBufferRange", target);
1575 if (!bufObj)
1576 return NULL;
1577
1578 if (offset + length > bufObj->Size) {
1579 _mesa_error(ctx, GL_INVALID_VALUE,
1580 "glMapBufferRange(offset + length > size)");
1581 return NULL;
1582 }
1583
1584 if (_mesa_bufferobj_mapped(bufObj)) {
1585 _mesa_error(ctx, GL_INVALID_OPERATION,
1586 "glMapBufferRange(buffer already mapped)");
1587 return NULL;
1588 }
1589
1590 if (!bufObj->Size) {
1591 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1592 "glMapBufferRange(buffer size = 0)");
1593 return NULL;
1594 }
1595
1596 /* Mapping zero bytes should return a non-null pointer. */
1597 if (!length) {
1598 static long dummy = 0;
1599 bufObj->Pointer = &dummy;
1600 bufObj->Length = length;
1601 bufObj->Offset = offset;
1602 bufObj->AccessFlags = access;
1603 return bufObj->Pointer;
1604 }
1605
1606 ASSERT(ctx->Driver.MapBufferRange);
1607 map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj);
1608 if (!map) {
1609 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1610 }
1611 else {
1612 /* The driver callback should have set all these fields.
1613 * This is important because other modules (like VBO) might call
1614 * the driver function directly.
1615 */
1616 ASSERT(bufObj->Pointer == map);
1617 ASSERT(bufObj->Length == length);
1618 ASSERT(bufObj->Offset == offset);
1619 ASSERT(bufObj->AccessFlags == access);
1620 }
1621
1622 return map;
1623 }
1624
1625
1626 /**
1627 * See GL_ARB_map_buffer_range spec
1628 */
1629 void GLAPIENTRY
1630 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
1631 {
1632 GET_CURRENT_CONTEXT(ctx);
1633 struct gl_buffer_object *bufObj;
1634 ASSERT_OUTSIDE_BEGIN_END(ctx);
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, _NEW_BUFFER_OBJECT);
2078
2079 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2080 binding->Offset = offset;
2081 binding->Size = size;
2082 binding->AutomaticSize = autoSize;
2083 }
2084
2085 /**
2086 * Bind a region of a buffer object to a uniform block binding point.
2087 * \param index the uniform buffer binding point index
2088 * \param bufObj the buffer object
2089 * \param offset offset to the start of buffer object region
2090 * \param size size of the buffer object region
2091 */
2092 static void
2093 bind_buffer_range_uniform_buffer(struct gl_context *ctx,
2094 GLuint index,
2095 struct gl_buffer_object *bufObj,
2096 GLintptr offset,
2097 GLsizeiptr size)
2098 {
2099 if (index >= ctx->Const.MaxUniformBufferBindings) {
2100 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
2101 return;
2102 }
2103
2104 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
2105 _mesa_error(ctx, GL_INVALID_VALUE,
2106 "glBindBufferRange(offset misalgned %d/%d)", (int) offset,
2107 ctx->Const.UniformBufferOffsetAlignment);
2108 return;
2109 }
2110
2111 if (bufObj == ctx->Shared->NullBufferObj) {
2112 offset = -1;
2113 size = -1;
2114 }
2115
2116 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2117 set_ubo_binding(ctx, index, bufObj, offset, size, GL_FALSE);
2118 }
2119
2120
2121 /**
2122 * Bind a buffer object to a uniform block binding point.
2123 * As above, but offset = 0.
2124 */
2125 static void
2126 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
2127 GLuint index,
2128 struct gl_buffer_object *bufObj)
2129 {
2130 if (index >= ctx->Const.MaxUniformBufferBindings) {
2131 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
2132 return;
2133 }
2134
2135 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2136 if (bufObj == ctx->Shared->NullBufferObj)
2137 set_ubo_binding(ctx, index, bufObj, -1, -1, GL_TRUE);
2138 else
2139 set_ubo_binding(ctx, index, bufObj, 0, 0, GL_TRUE);
2140 }
2141
2142 void GLAPIENTRY
2143 _mesa_BindBufferRange(GLenum target, GLuint index,
2144 GLuint buffer, GLintptr offset, GLsizeiptr size)
2145 {
2146 GET_CURRENT_CONTEXT(ctx);
2147 struct gl_buffer_object *bufObj;
2148
2149 if (buffer == 0) {
2150 bufObj = ctx->Shared->NullBufferObj;
2151 } else {
2152 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2153 }
2154 if (!handle_bind_buffer_gen(ctx, target, buffer, &bufObj))
2155 return;
2156
2157 if (!bufObj) {
2158 _mesa_error(ctx, GL_INVALID_OPERATION,
2159 "glBindBufferRange(invalid buffer=%u)", buffer);
2160 return;
2161 }
2162
2163 if (buffer != 0) {
2164 if (size <= 0) {
2165 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
2166 (int) size);
2167 return;
2168 }
2169
2170 if (offset + size > bufObj->Size) {
2171 _mesa_error(ctx, GL_INVALID_VALUE,
2172 "glBindBufferRange(offset + size %d > buffer size %d)",
2173 (int) (offset + size), (int) (bufObj->Size));
2174 return;
2175 }
2176 }
2177
2178 switch (target) {
2179 case GL_TRANSFORM_FEEDBACK_BUFFER:
2180 _mesa_bind_buffer_range_transform_feedback(ctx, index, bufObj,
2181 offset, size);
2182 return;
2183 case GL_UNIFORM_BUFFER:
2184 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
2185 return;
2186 default:
2187 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
2188 return;
2189 }
2190 }
2191
2192 void GLAPIENTRY
2193 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
2194 {
2195 GET_CURRENT_CONTEXT(ctx);
2196 struct gl_buffer_object *bufObj;
2197
2198 if (buffer == 0) {
2199 bufObj = ctx->Shared->NullBufferObj;
2200 } else {
2201 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2202 }
2203 if (!handle_bind_buffer_gen(ctx, target, buffer, &bufObj))
2204 return;
2205
2206 if (!bufObj) {
2207 _mesa_error(ctx, GL_INVALID_OPERATION,
2208 "glBindBufferBase(invalid buffer=%u)", buffer);
2209 return;
2210 }
2211
2212 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
2213 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
2214 *
2215 * "BindBufferBase is equivalent to calling BindBufferRange with offset
2216 * zero and size equal to the size of buffer."
2217 *
2218 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
2219 *
2220 * "If the parameter (starting offset or size) was not specified when the
2221 * buffer object was bound, zero is returned."
2222 *
2223 * What happens if the size of the buffer changes? Does the size of the
2224 * buffer at the moment glBindBufferBase was called still play a role, like
2225 * the first quote would imply, or is the size meaningless in the
2226 * glBindBufferBase case like the second quote would suggest? The GL 4.1
2227 * core spec page 45 says:
2228 *
2229 * "It is equivalent to calling BindBufferRange with offset zero, while
2230 * size is determined by the size of the bound buffer at the time the
2231 * binding is used."
2232 *
2233 * My interpretation is that the GL 4.1 spec was a clarification of the
2234 * behavior, not a change. In particular, this choice will only make
2235 * rendering work in cases where it would have had undefined results.
2236 */
2237
2238 switch (target) {
2239 case GL_TRANSFORM_FEEDBACK_BUFFER:
2240 _mesa_bind_buffer_base_transform_feedback(ctx, index, bufObj);
2241 return;
2242 case GL_UNIFORM_BUFFER:
2243 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
2244 return;
2245 default:
2246 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
2247 return;
2248 }
2249 }
2250
2251 void GLAPIENTRY
2252 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
2253 GLsizeiptr length)
2254 {
2255 GET_CURRENT_CONTEXT(ctx);
2256 struct gl_buffer_object *bufObj;
2257 const GLintptr end = offset + length;
2258
2259 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2260 if (!bufObj) {
2261 _mesa_error(ctx, GL_INVALID_VALUE,
2262 "glInvalidateBufferSubData(name = 0x%x) invalid object",
2263 buffer);
2264 return;
2265 }
2266
2267 /* The GL_ARB_invalidate_subdata spec says:
2268 *
2269 * "An INVALID_VALUE error is generated if <offset> or <length> is
2270 * negative, or if <offset> + <length> is greater than the value of
2271 * BUFFER_SIZE."
2272 */
2273 if (end < 0 || end > bufObj->Size) {
2274 _mesa_error(ctx, GL_INVALID_VALUE,
2275 "glInvalidateBufferSubData(invalid offset or length)");
2276 return;
2277 }
2278
2279 /* The GL_ARB_invalidate_subdata spec says:
2280 *
2281 * "An INVALID_OPERATION error is generated if the buffer is currently
2282 * mapped by MapBuffer, or if the invalidate range intersects the range
2283 * currently mapped by MapBufferRange."
2284 */
2285 if (_mesa_bufferobj_mapped(bufObj)) {
2286 const GLintptr mapEnd = bufObj->Offset + bufObj->Length;
2287
2288 /* The regions do not overlap if and only if the end of the discard
2289 * region is before the mapped region or the start of the discard region
2290 * is after the mapped region.
2291 *
2292 * Note that 'end' and 'mapEnd' are the first byte *after* the discard
2293 * region and the mapped region, repsectively. It is okay for that byte
2294 * to be mapped (for 'end') or discarded (for 'mapEnd').
2295 */
2296 if (!(end <= bufObj->Offset || offset >= mapEnd)) {
2297 _mesa_error(ctx, GL_INVALID_OPERATION,
2298 "glInvalidateBufferSubData(intersection with mapped "
2299 "range)");
2300 return;
2301 }
2302 }
2303
2304 /* We don't actually do anything for this yet. Just return after
2305 * validating the parameters and generating the required errors.
2306 */
2307 return;
2308 }
2309
2310 void GLAPIENTRY
2311 _mesa_InvalidateBufferData(GLuint buffer)
2312 {
2313 GET_CURRENT_CONTEXT(ctx);
2314 struct gl_buffer_object *bufObj;
2315
2316 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2317 if (!bufObj) {
2318 _mesa_error(ctx, GL_INVALID_VALUE,
2319 "glInvalidateBufferData(name = 0x%x) invalid object",
2320 buffer);
2321 return;
2322 }
2323
2324 /* The GL_ARB_invalidate_subdata spec says:
2325 *
2326 * "An INVALID_OPERATION error is generated if the buffer is currently
2327 * mapped by MapBuffer, or if the invalidate range intersects the range
2328 * currently mapped by MapBufferRange."
2329 */
2330 if (_mesa_bufferobj_mapped(bufObj)) {
2331 _mesa_error(ctx, GL_INVALID_OPERATION,
2332 "glInvalidateBufferData(intersection with mapped "
2333 "range)");
2334 return;
2335 }
2336
2337 /* We don't actually do anything for this yet. Just return after
2338 * validating the parameters and generating the required errors.
2339 */
2340 return;
2341 }