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