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