54bba1aade799f1ad4a9992ab021e9dea1aa0417
[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 static bool
659 handle_bind_buffer_gen(struct gl_context *ctx,
660 GLenum target,
661 GLuint buffer,
662 struct gl_buffer_object **buf_handle)
663 {
664 struct gl_buffer_object *buf = *buf_handle;
665
666 if (!buf && ctx->API == API_OPENGL_CORE) {
667 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindBuffer(non-gen name)");
668 return false;
669 }
670
671 if (!buf || buf == &DummyBufferObject) {
672 /* If this is a new buffer object id, or one which was generated but
673 * never used before, allocate a buffer object now.
674 */
675 ASSERT(ctx->Driver.NewBufferObject);
676 buf = ctx->Driver.NewBufferObject(ctx, buffer, target);
677 if (!buf) {
678 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindBufferARB");
679 return false;
680 }
681 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, buf);
682 *buf_handle = buf;
683 }
684
685 return true;
686 }
687
688 /**
689 * Bind the specified target to buffer for the specified context.
690 * Called by glBindBuffer() and other functions.
691 */
692 static void
693 bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
694 {
695 struct gl_buffer_object *oldBufObj;
696 struct gl_buffer_object *newBufObj = NULL;
697 struct gl_buffer_object **bindTarget = NULL;
698
699 bindTarget = get_buffer_target(ctx, target);
700 if (!bindTarget) {
701 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
702 return;
703 }
704
705 /* Get pointer to old buffer object (to be unbound) */
706 oldBufObj = *bindTarget;
707 if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
708 return; /* rebinding the same buffer object- no change */
709
710 /*
711 * Get pointer to new buffer object (newBufObj)
712 */
713 if (buffer == 0) {
714 /* The spec says there's not a buffer object named 0, but we use
715 * one internally because it simplifies things.
716 */
717 newBufObj = ctx->Shared->NullBufferObj;
718 }
719 else {
720 /* non-default buffer object */
721 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
722 if (!handle_bind_buffer_gen(ctx, target, buffer, &newBufObj))
723 return;
724 }
725
726 /* bind new buffer */
727 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
728
729 /* Pass BindBuffer call to device driver */
730 if (ctx->Driver.BindBuffer)
731 ctx->Driver.BindBuffer( ctx, target, newBufObj );
732 }
733
734
735 /**
736 * Update the default buffer objects in the given context to reference those
737 * specified in the shared state and release those referencing the old
738 * shared state.
739 */
740 void
741 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
742 {
743 /* Bind the NullBufferObj to remove references to those
744 * in the shared context hash table.
745 */
746 bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
747 bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
748 bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
749 bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
750 }
751
752
753
754 /**
755 * Return the gl_buffer_object for the given ID.
756 * Always return NULL for ID 0.
757 */
758 struct gl_buffer_object *
759 _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
760 {
761 if (buffer == 0)
762 return NULL;
763 else
764 return (struct gl_buffer_object *)
765 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
766 }
767
768
769 /**
770 * If *ptr points to obj, set ptr = the Null/default buffer object.
771 * This is a helper for buffer object deletion.
772 * The GL spec says that deleting a buffer object causes it to get
773 * unbound from all arrays in the current context.
774 */
775 static void
776 unbind(struct gl_context *ctx,
777 struct gl_buffer_object **ptr,
778 struct gl_buffer_object *obj)
779 {
780 if (*ptr == obj) {
781 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
782 }
783 }
784
785
786 /**
787 * Plug default/fallback buffer object functions into the device
788 * driver hooks.
789 */
790 void
791 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
792 {
793 /* GL_ARB_vertex/pixel_buffer_object */
794 driver->NewBufferObject = _mesa_new_buffer_object;
795 driver->DeleteBuffer = _mesa_delete_buffer_object;
796 driver->BindBuffer = NULL;
797 driver->BufferData = _mesa_buffer_data;
798 driver->BufferSubData = _mesa_buffer_subdata;
799 driver->GetBufferSubData = _mesa_buffer_get_subdata;
800 driver->UnmapBuffer = _mesa_buffer_unmap;
801
802 /* GL_ARB_map_buffer_range */
803 driver->MapBufferRange = _mesa_buffer_map_range;
804 driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
805
806 /* GL_ARB_copy_buffer */
807 driver->CopyBufferSubData = _mesa_copy_buffer_subdata;
808 }
809
810
811
812 /**********************************************************************/
813 /* API Functions */
814 /**********************************************************************/
815
816 void GLAPIENTRY
817 _mesa_BindBuffer(GLenum target, GLuint buffer)
818 {
819 GET_CURRENT_CONTEXT(ctx);
820
821 if (MESA_VERBOSE & VERBOSE_API)
822 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
823 _mesa_lookup_enum_by_nr(target), buffer);
824
825 bind_buffer_object(ctx, target, buffer);
826 }
827
828
829 /**
830 * Delete a set of buffer objects.
831 *
832 * \param n Number of buffer objects to delete.
833 * \param ids Array of \c n buffer object IDs.
834 */
835 void GLAPIENTRY
836 _mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
837 {
838 GET_CURRENT_CONTEXT(ctx);
839 GLsizei i;
840 FLUSH_VERTICES(ctx, 0);
841
842 if (n < 0) {
843 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
844 return;
845 }
846
847 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
848
849 for (i = 0; i < n; i++) {
850 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
851 if (bufObj) {
852 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
853 GLuint j;
854
855 ASSERT(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
856
857 if (_mesa_bufferobj_mapped(bufObj)) {
858 /* if mapped, unmap it now */
859 ctx->Driver.UnmapBuffer(ctx, bufObj);
860 bufObj->AccessFlags = 0;
861 bufObj->Pointer = NULL;
862 }
863
864 /* unbind any vertex pointers bound to this buffer */
865 for (j = 0; j < Elements(arrayObj->_VertexAttrib); j++) {
866 unbind(ctx, &arrayObj->_VertexAttrib[j].BufferObj, bufObj);
867 }
868
869 if (ctx->Array.ArrayBufferObj == bufObj) {
870 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB, 0 );
871 }
872 if (arrayObj->ElementArrayBufferObj == bufObj) {
873 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
874 }
875
876 /* unbind ARB_copy_buffer binding points */
877 if (ctx->CopyReadBuffer == bufObj) {
878 _mesa_BindBuffer( GL_COPY_READ_BUFFER, 0 );
879 }
880 if (ctx->CopyWriteBuffer == bufObj) {
881 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER, 0 );
882 }
883
884 /* unbind transform feedback binding points */
885 if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
886 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER, 0 );
887 }
888 for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
889 if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
890 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 );
891 }
892 }
893
894 /* unbind UBO binding points */
895 for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
896 if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
897 _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 );
898 }
899 }
900
901 if (ctx->UniformBuffer == bufObj) {
902 _mesa_BindBuffer( GL_UNIFORM_BUFFER, 0 );
903 }
904
905 /* unbind any pixel pack/unpack pointers bound to this buffer */
906 if (ctx->Pack.BufferObj == bufObj) {
907 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT, 0 );
908 }
909 if (ctx->Unpack.BufferObj == bufObj) {
910 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
911 }
912
913 if (ctx->Texture.BufferObject == bufObj) {
914 _mesa_BindBuffer( GL_TEXTURE_BUFFER, 0 );
915 }
916
917 /* The ID is immediately freed for re-use */
918 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
919 /* Make sure we do not run into the classic ABA problem on bind.
920 * We don't want to allow re-binding a buffer object that's been
921 * "deleted" by glDeleteBuffers().
922 *
923 * The explicit rebinding to the default object in the current context
924 * prevents the above in the current context, but another context
925 * sharing the same objects might suffer from this problem.
926 * The alternative would be to do the hash lookup in any case on bind
927 * which would introduce more runtime overhead than this.
928 */
929 bufObj->DeletePending = GL_TRUE;
930 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
931 }
932 }
933
934 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
935 }
936
937
938 /**
939 * Generate a set of unique buffer object IDs and store them in \c buffer.
940 *
941 * \param n Number of IDs to generate.
942 * \param buffer Array of \c n locations to store the IDs.
943 */
944 void GLAPIENTRY
945 _mesa_GenBuffers(GLsizei n, GLuint *buffer)
946 {
947 GET_CURRENT_CONTEXT(ctx);
948 GLuint first;
949 GLint i;
950
951 if (MESA_VERBOSE & VERBOSE_API)
952 _mesa_debug(ctx, "glGenBuffers(%d)\n", n);
953
954 if (n < 0) {
955 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
956 return;
957 }
958
959 if (!buffer) {
960 return;
961 }
962
963 /*
964 * This must be atomic (generation and allocation of buffer object IDs)
965 */
966 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
967
968 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
969
970 /* Insert the ID and pointer to dummy buffer object into hash table */
971 for (i = 0; i < n; i++) {
972 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i,
973 &DummyBufferObject);
974 buffer[i] = first + i;
975 }
976
977 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
978 }
979
980
981 /**
982 * Determine if ID is the name of a buffer object.
983 *
984 * \param id ID of the potential buffer object.
985 * \return \c GL_TRUE if \c id is the name of a buffer object,
986 * \c GL_FALSE otherwise.
987 */
988 GLboolean GLAPIENTRY
989 _mesa_IsBuffer(GLuint id)
990 {
991 struct gl_buffer_object *bufObj;
992 GET_CURRENT_CONTEXT(ctx);
993 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
994
995 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
996 bufObj = _mesa_lookup_bufferobj(ctx, id);
997 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
998
999 return bufObj && bufObj != &DummyBufferObject;
1000 }
1001
1002
1003 void GLAPIENTRY
1004 _mesa_BufferData(GLenum target, GLsizeiptrARB size,
1005 const GLvoid * data, GLenum usage)
1006 {
1007 GET_CURRENT_CONTEXT(ctx);
1008 struct gl_buffer_object *bufObj;
1009 bool valid_usage;
1010
1011 if (MESA_VERBOSE & VERBOSE_API)
1012 _mesa_debug(ctx, "glBufferData(%s, %ld, %p, %s)\n",
1013 _mesa_lookup_enum_by_nr(target),
1014 (long int) size, data,
1015 _mesa_lookup_enum_by_nr(usage));
1016
1017 if (size < 0) {
1018 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
1019 return;
1020 }
1021
1022 switch (usage) {
1023 case GL_STREAM_DRAW_ARB:
1024 valid_usage = (ctx->API != API_OPENGLES);
1025 break;
1026
1027 case GL_STATIC_DRAW_ARB:
1028 case GL_DYNAMIC_DRAW_ARB:
1029 valid_usage = true;
1030 break;
1031
1032 case GL_STREAM_READ_ARB:
1033 case GL_STREAM_COPY_ARB:
1034 case GL_STATIC_READ_ARB:
1035 case GL_STATIC_COPY_ARB:
1036 case GL_DYNAMIC_READ_ARB:
1037 case GL_DYNAMIC_COPY_ARB:
1038 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
1039 break;
1040
1041 default:
1042 valid_usage = false;
1043 break;
1044 }
1045
1046 if (!valid_usage) {
1047 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferData(usage)");
1048 return;
1049 }
1050
1051 bufObj = get_buffer(ctx, "glBufferDataARB", target);
1052 if (!bufObj)
1053 return;
1054
1055 if (_mesa_bufferobj_mapped(bufObj)) {
1056 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1057 ctx->Driver.UnmapBuffer(ctx, bufObj);
1058 bufObj->AccessFlags = 0;
1059 ASSERT(bufObj->Pointer == NULL);
1060 }
1061
1062 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1063
1064 bufObj->Written = GL_TRUE;
1065
1066 #ifdef VBO_DEBUG
1067 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1068 bufObj->Name, size, data, usage);
1069 #endif
1070
1071 #ifdef BOUNDS_CHECK
1072 size += 100;
1073 #endif
1074
1075 ASSERT(ctx->Driver.BufferData);
1076 if (!ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj )) {
1077 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB()");
1078 }
1079 }
1080
1081
1082 void GLAPIENTRY
1083 _mesa_BufferSubData(GLenum target, GLintptrARB offset,
1084 GLsizeiptrARB size, const GLvoid * data)
1085 {
1086 GET_CURRENT_CONTEXT(ctx);
1087 struct gl_buffer_object *bufObj;
1088
1089 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1090 "glBufferSubDataARB" );
1091 if (!bufObj) {
1092 /* error already recorded */
1093 return;
1094 }
1095
1096 if (size == 0)
1097 return;
1098
1099 bufObj->Written = GL_TRUE;
1100
1101 ASSERT(ctx->Driver.BufferSubData);
1102 ctx->Driver.BufferSubData( ctx, offset, size, data, bufObj );
1103 }
1104
1105
1106 void GLAPIENTRY
1107 _mesa_GetBufferSubData(GLenum target, GLintptrARB offset,
1108 GLsizeiptrARB size, void * data)
1109 {
1110 GET_CURRENT_CONTEXT(ctx);
1111 struct gl_buffer_object *bufObj;
1112
1113 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1114 "glGetBufferSubDataARB" );
1115 if (!bufObj) {
1116 /* error already recorded */
1117 return;
1118 }
1119
1120 ASSERT(ctx->Driver.GetBufferSubData);
1121 ctx->Driver.GetBufferSubData( ctx, offset, size, data, bufObj );
1122 }
1123
1124
1125 void * GLAPIENTRY
1126 _mesa_MapBuffer(GLenum target, GLenum access)
1127 {
1128 GET_CURRENT_CONTEXT(ctx);
1129 struct gl_buffer_object * bufObj;
1130 GLbitfield accessFlags;
1131 void *map;
1132 bool valid_access;
1133
1134 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1135
1136 switch (access) {
1137 case GL_READ_ONLY_ARB:
1138 accessFlags = GL_MAP_READ_BIT;
1139 valid_access = _mesa_is_desktop_gl(ctx);
1140 break;
1141 case GL_WRITE_ONLY_ARB:
1142 accessFlags = GL_MAP_WRITE_BIT;
1143 valid_access = true;
1144 break;
1145 case GL_READ_WRITE_ARB:
1146 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1147 valid_access = _mesa_is_desktop_gl(ctx);
1148 break;
1149 default:
1150 valid_access = false;
1151 break;
1152 }
1153
1154 if (!valid_access) {
1155 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
1156 return NULL;
1157 }
1158
1159 bufObj = get_buffer(ctx, "glMapBufferARB", target);
1160 if (!bufObj)
1161 return NULL;
1162
1163 if (_mesa_bufferobj_mapped(bufObj)) {
1164 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
1165 return NULL;
1166 }
1167
1168 if (!bufObj->Size) {
1169 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1170 "glMapBuffer(buffer size = 0)");
1171 return NULL;
1172 }
1173
1174 ASSERT(ctx->Driver.MapBufferRange);
1175 map = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size, accessFlags, bufObj);
1176 if (!map) {
1177 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1178 return NULL;
1179 }
1180 else {
1181 /* The driver callback should have set these fields.
1182 * This is important because other modules (like VBO) might call
1183 * the driver function directly.
1184 */
1185 ASSERT(bufObj->Pointer == map);
1186 ASSERT(bufObj->Length == bufObj->Size);
1187 ASSERT(bufObj->Offset == 0);
1188 bufObj->AccessFlags = accessFlags;
1189 }
1190
1191 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
1192 bufObj->Written = GL_TRUE;
1193
1194 #ifdef VBO_DEBUG
1195 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1196 bufObj->Name, bufObj->Size, access);
1197 if (access == GL_WRITE_ONLY_ARB) {
1198 GLuint i;
1199 GLubyte *b = (GLubyte *) bufObj->Pointer;
1200 for (i = 0; i < bufObj->Size; i++)
1201 b[i] = i & 0xff;
1202 }
1203 #endif
1204
1205 #ifdef BOUNDS_CHECK
1206 {
1207 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1208 GLuint i;
1209 /* buffer is 100 bytes larger than requested, fill with magic value */
1210 for (i = 0; i < 100; i++) {
1211 buf[bufObj->Size - i - 1] = 123;
1212 }
1213 }
1214 #endif
1215
1216 return bufObj->Pointer;
1217 }
1218
1219
1220 GLboolean GLAPIENTRY
1221 _mesa_UnmapBuffer(GLenum target)
1222 {
1223 GET_CURRENT_CONTEXT(ctx);
1224 struct gl_buffer_object *bufObj;
1225 GLboolean status = GL_TRUE;
1226 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1227
1228 bufObj = get_buffer(ctx, "glUnmapBufferARB", target);
1229 if (!bufObj)
1230 return GL_FALSE;
1231
1232 if (!_mesa_bufferobj_mapped(bufObj)) {
1233 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1234 return GL_FALSE;
1235 }
1236
1237 #ifdef BOUNDS_CHECK
1238 if (bufObj->Access != GL_READ_ONLY_ARB) {
1239 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1240 GLuint i;
1241 /* check that last 100 bytes are still = magic value */
1242 for (i = 0; i < 100; i++) {
1243 GLuint pos = bufObj->Size - i - 1;
1244 if (buf[pos] != 123) {
1245 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1246 " at position %d (value = %u)\n",
1247 pos, buf[pos]);
1248 }
1249 }
1250 }
1251 #endif
1252
1253 #ifdef VBO_DEBUG
1254 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1255 GLuint i, unchanged = 0;
1256 GLubyte *b = (GLubyte *) bufObj->Pointer;
1257 GLint pos = -1;
1258 /* check which bytes changed */
1259 for (i = 0; i < bufObj->Size - 1; i++) {
1260 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1261 unchanged++;
1262 if (pos == -1)
1263 pos = i;
1264 }
1265 }
1266 if (unchanged) {
1267 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1268 bufObj->Name, unchanged, bufObj->Size, pos);
1269 }
1270 }
1271 #endif
1272
1273 status = ctx->Driver.UnmapBuffer( ctx, bufObj );
1274 bufObj->AccessFlags = 0;
1275 ASSERT(bufObj->Pointer == NULL);
1276 ASSERT(bufObj->Offset == 0);
1277 ASSERT(bufObj->Length == 0);
1278
1279 return status;
1280 }
1281
1282
1283 void GLAPIENTRY
1284 _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
1285 {
1286 GET_CURRENT_CONTEXT(ctx);
1287 struct gl_buffer_object *bufObj;
1288
1289 bufObj = get_buffer(ctx, "glGetBufferParameterivARB", target);
1290 if (!bufObj)
1291 return;
1292
1293 switch (pname) {
1294 case GL_BUFFER_SIZE_ARB:
1295 *params = (GLint) bufObj->Size;
1296 return;
1297 case GL_BUFFER_USAGE_ARB:
1298 *params = bufObj->Usage;
1299 return;
1300 case GL_BUFFER_ACCESS_ARB:
1301 *params = simplified_access_mode(ctx, bufObj->AccessFlags);
1302 return;
1303 case GL_BUFFER_MAPPED_ARB:
1304 *params = _mesa_bufferobj_mapped(bufObj);
1305 return;
1306 case GL_BUFFER_ACCESS_FLAGS:
1307 if (!ctx->Extensions.ARB_map_buffer_range)
1308 goto invalid_pname;
1309 *params = bufObj->AccessFlags;
1310 return;
1311 case GL_BUFFER_MAP_OFFSET:
1312 if (!ctx->Extensions.ARB_map_buffer_range)
1313 goto invalid_pname;
1314 *params = (GLint) bufObj->Offset;
1315 return;
1316 case GL_BUFFER_MAP_LENGTH:
1317 if (!ctx->Extensions.ARB_map_buffer_range)
1318 goto invalid_pname;
1319 *params = (GLint) bufObj->Length;
1320 return;
1321 default:
1322 ; /* fall-through */
1323 }
1324
1325 invalid_pname:
1326 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname=%s)",
1327 _mesa_lookup_enum_by_nr(pname));
1328 }
1329
1330
1331 /**
1332 * New in GL 3.2
1333 * This is pretty much a duplicate of GetBufferParameteriv() but the
1334 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1335 */
1336 void GLAPIENTRY
1337 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
1338 {
1339 GET_CURRENT_CONTEXT(ctx);
1340 struct gl_buffer_object *bufObj;
1341
1342 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target);
1343 if (!bufObj)
1344 return;
1345
1346 switch (pname) {
1347 case GL_BUFFER_SIZE_ARB:
1348 *params = bufObj->Size;
1349 return;
1350 case GL_BUFFER_USAGE_ARB:
1351 *params = bufObj->Usage;
1352 return;
1353 case GL_BUFFER_ACCESS_ARB:
1354 *params = simplified_access_mode(ctx, bufObj->AccessFlags);
1355 return;
1356 case GL_BUFFER_ACCESS_FLAGS:
1357 if (!ctx->Extensions.ARB_map_buffer_range)
1358 goto invalid_pname;
1359 *params = bufObj->AccessFlags;
1360 return;
1361 case GL_BUFFER_MAPPED_ARB:
1362 *params = _mesa_bufferobj_mapped(bufObj);
1363 return;
1364 case GL_BUFFER_MAP_OFFSET:
1365 if (!ctx->Extensions.ARB_map_buffer_range)
1366 goto invalid_pname;
1367 *params = bufObj->Offset;
1368 return;
1369 case GL_BUFFER_MAP_LENGTH:
1370 if (!ctx->Extensions.ARB_map_buffer_range)
1371 goto invalid_pname;
1372 *params = bufObj->Length;
1373 return;
1374 default:
1375 ; /* fall-through */
1376 }
1377
1378 invalid_pname:
1379 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
1380 _mesa_lookup_enum_by_nr(pname));
1381 }
1382
1383
1384 void GLAPIENTRY
1385 _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
1386 {
1387 GET_CURRENT_CONTEXT(ctx);
1388 struct gl_buffer_object * bufObj;
1389
1390 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1391 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1392 return;
1393 }
1394
1395 bufObj = get_buffer(ctx, "glGetBufferPointervARB", target);
1396 if (!bufObj)
1397 return;
1398
1399 *params = bufObj->Pointer;
1400 }
1401
1402
1403 void GLAPIENTRY
1404 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
1405 GLintptr readOffset, GLintptr writeOffset,
1406 GLsizeiptr size)
1407 {
1408 GET_CURRENT_CONTEXT(ctx);
1409 struct gl_buffer_object *src, *dst;
1410
1411 src = get_buffer(ctx, "glCopyBufferSubData", readTarget);
1412 if (!src)
1413 return;
1414
1415 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget);
1416 if (!dst)
1417 return;
1418
1419 if (_mesa_bufferobj_mapped(src)) {
1420 _mesa_error(ctx, GL_INVALID_OPERATION,
1421 "glCopyBufferSubData(readBuffer is mapped)");
1422 return;
1423 }
1424
1425 if (_mesa_bufferobj_mapped(dst)) {
1426 _mesa_error(ctx, GL_INVALID_OPERATION,
1427 "glCopyBufferSubData(writeBuffer is mapped)");
1428 return;
1429 }
1430
1431 if (readOffset < 0) {
1432 _mesa_error(ctx, GL_INVALID_VALUE,
1433 "glCopyBufferSubData(readOffset = %d)", (int) readOffset);
1434 return;
1435 }
1436
1437 if (writeOffset < 0) {
1438 _mesa_error(ctx, GL_INVALID_VALUE,
1439 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset);
1440 return;
1441 }
1442
1443 if (size < 0) {
1444 _mesa_error(ctx, GL_INVALID_VALUE,
1445 "glCopyBufferSubData(writeOffset = %d)", (int) size);
1446 return;
1447 }
1448
1449 if (readOffset + size > src->Size) {
1450 _mesa_error(ctx, GL_INVALID_VALUE,
1451 "glCopyBufferSubData(readOffset + size = %d)",
1452 (int) (readOffset + size));
1453 return;
1454 }
1455
1456 if (writeOffset + size > dst->Size) {
1457 _mesa_error(ctx, GL_INVALID_VALUE,
1458 "glCopyBufferSubData(writeOffset + size = %d)",
1459 (int) (writeOffset + size));
1460 return;
1461 }
1462
1463 if (src == dst) {
1464 if (readOffset + size <= writeOffset) {
1465 /* OK */
1466 }
1467 else if (writeOffset + size <= readOffset) {
1468 /* OK */
1469 }
1470 else {
1471 /* overlapping src/dst is illegal */
1472 _mesa_error(ctx, GL_INVALID_VALUE,
1473 "glCopyBufferSubData(overlapping src/dst)");
1474 return;
1475 }
1476 }
1477
1478 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
1479 }
1480
1481
1482 /**
1483 * See GL_ARB_map_buffer_range spec
1484 */
1485 void * GLAPIENTRY
1486 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
1487 GLbitfield access)
1488 {
1489 GET_CURRENT_CONTEXT(ctx);
1490 struct gl_buffer_object *bufObj;
1491 void *map;
1492
1493 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1494
1495 if (!ctx->Extensions.ARB_map_buffer_range) {
1496 _mesa_error(ctx, GL_INVALID_OPERATION,
1497 "glMapBufferRange(extension not supported)");
1498 return NULL;
1499 }
1500
1501 if (offset < 0) {
1502 _mesa_error(ctx, GL_INVALID_VALUE,
1503 "glMapBufferRange(offset = %ld)", (long)offset);
1504 return NULL;
1505 }
1506
1507 if (length < 0) {
1508 _mesa_error(ctx, GL_INVALID_VALUE,
1509 "glMapBufferRange(length = %ld)", (long)length);
1510 return NULL;
1511 }
1512
1513 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
1514 *
1515 * "An INVALID_OPERATION error is generated for any of the following
1516 * conditions:
1517 *
1518 * * <length> is zero."
1519 */
1520 if (_mesa_is_gles(ctx) && length == 0) {
1521 _mesa_error(ctx, GL_INVALID_OPERATION,
1522 "glMapBufferRange(length = 0)");
1523 return NULL;
1524 }
1525
1526 if (access & ~(GL_MAP_READ_BIT |
1527 GL_MAP_WRITE_BIT |
1528 GL_MAP_INVALIDATE_RANGE_BIT |
1529 GL_MAP_INVALIDATE_BUFFER_BIT |
1530 GL_MAP_FLUSH_EXPLICIT_BIT |
1531 GL_MAP_UNSYNCHRONIZED_BIT)) {
1532 /* generate an error if any undefind bit is set */
1533 _mesa_error(ctx, GL_INVALID_VALUE, "glMapBufferRange(access)");
1534 return NULL;
1535 }
1536
1537 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
1538 _mesa_error(ctx, GL_INVALID_OPERATION,
1539 "glMapBufferRange(access indicates neither read or write)");
1540 return NULL;
1541 }
1542
1543 if ((access & GL_MAP_READ_BIT) &&
1544 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
1545 GL_MAP_INVALIDATE_BUFFER_BIT |
1546 GL_MAP_UNSYNCHRONIZED_BIT))) {
1547 _mesa_error(ctx, GL_INVALID_OPERATION,
1548 "glMapBufferRange(invalid access flags)");
1549 return NULL;
1550 }
1551
1552 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
1553 ((access & GL_MAP_WRITE_BIT) == 0)) {
1554 _mesa_error(ctx, GL_INVALID_OPERATION,
1555 "glMapBufferRange(invalid access flags)");
1556 return NULL;
1557 }
1558
1559 bufObj = get_buffer(ctx, "glMapBufferRange", target);
1560 if (!bufObj)
1561 return NULL;
1562
1563 if (offset + length > bufObj->Size) {
1564 _mesa_error(ctx, GL_INVALID_VALUE,
1565 "glMapBufferRange(offset + length > size)");
1566 return NULL;
1567 }
1568
1569 if (_mesa_bufferobj_mapped(bufObj)) {
1570 _mesa_error(ctx, GL_INVALID_OPERATION,
1571 "glMapBufferRange(buffer already mapped)");
1572 return NULL;
1573 }
1574
1575 if (!bufObj->Size) {
1576 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1577 "glMapBufferRange(buffer size = 0)");
1578 return NULL;
1579 }
1580
1581 /* Mapping zero bytes should return a non-null pointer. */
1582 if (!length) {
1583 static long dummy = 0;
1584 bufObj->Pointer = &dummy;
1585 bufObj->Length = length;
1586 bufObj->Offset = offset;
1587 bufObj->AccessFlags = access;
1588 return bufObj->Pointer;
1589 }
1590
1591 ASSERT(ctx->Driver.MapBufferRange);
1592 map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj);
1593 if (!map) {
1594 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1595 }
1596 else {
1597 /* The driver callback should have set all these fields.
1598 * This is important because other modules (like VBO) might call
1599 * the driver function directly.
1600 */
1601 ASSERT(bufObj->Pointer == map);
1602 ASSERT(bufObj->Length == length);
1603 ASSERT(bufObj->Offset == offset);
1604 ASSERT(bufObj->AccessFlags == access);
1605 }
1606
1607 return map;
1608 }
1609
1610
1611 /**
1612 * See GL_ARB_map_buffer_range spec
1613 */
1614 void GLAPIENTRY
1615 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
1616 {
1617 GET_CURRENT_CONTEXT(ctx);
1618 struct gl_buffer_object *bufObj;
1619
1620 if (!ctx->Extensions.ARB_map_buffer_range) {
1621 _mesa_error(ctx, GL_INVALID_OPERATION,
1622 "glFlushMappedBufferRange(extension not supported)");
1623 return;
1624 }
1625
1626 if (offset < 0) {
1627 _mesa_error(ctx, GL_INVALID_VALUE,
1628 "glFlushMappedBufferRange(offset = %ld)", (long)offset);
1629 return;
1630 }
1631
1632 if (length < 0) {
1633 _mesa_error(ctx, GL_INVALID_VALUE,
1634 "glFlushMappedBufferRange(length = %ld)", (long)length);
1635 return;
1636 }
1637
1638 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target);
1639 if (!bufObj)
1640 return;
1641
1642 if (!_mesa_bufferobj_mapped(bufObj)) {
1643 /* buffer is not mapped */
1644 _mesa_error(ctx, GL_INVALID_OPERATION,
1645 "glFlushMappedBufferRange(buffer is not mapped)");
1646 return;
1647 }
1648
1649 if ((bufObj->AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
1650 _mesa_error(ctx, GL_INVALID_OPERATION,
1651 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1652 return;
1653 }
1654
1655 if (offset + length > bufObj->Length) {
1656 _mesa_error(ctx, GL_INVALID_VALUE,
1657 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
1658 (long)offset, (long)length, (long)bufObj->Length);
1659 return;
1660 }
1661
1662 ASSERT(bufObj->AccessFlags & GL_MAP_WRITE_BIT);
1663
1664 if (ctx->Driver.FlushMappedBufferRange)
1665 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj);
1666 }
1667
1668
1669 static GLenum
1670 buffer_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
1671 {
1672 struct gl_buffer_object *bufObj;
1673 GLenum retval;
1674
1675 bufObj = _mesa_lookup_bufferobj(ctx, name);
1676 if (!bufObj) {
1677 _mesa_error(ctx, GL_INVALID_VALUE,
1678 "glObjectPurgeable(name = 0x%x)", name);
1679 return 0;
1680 }
1681 if (!_mesa_is_bufferobj(bufObj)) {
1682 _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
1683 return 0;
1684 }
1685
1686 if (bufObj->Purgeable) {
1687 _mesa_error(ctx, GL_INVALID_OPERATION,
1688 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1689 return GL_VOLATILE_APPLE;
1690 }
1691
1692 bufObj->Purgeable = GL_TRUE;
1693
1694 retval = GL_VOLATILE_APPLE;
1695 if (ctx->Driver.BufferObjectPurgeable)
1696 retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
1697
1698 return retval;
1699 }
1700
1701
1702 static GLenum
1703 renderbuffer_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
1704 {
1705 struct gl_renderbuffer *bufObj;
1706 GLenum retval;
1707
1708 bufObj = _mesa_lookup_renderbuffer(ctx, name);
1709 if (!bufObj) {
1710 _mesa_error(ctx, GL_INVALID_VALUE,
1711 "glObjectUnpurgeable(name = 0x%x)", name);
1712 return 0;
1713 }
1714
1715 if (bufObj->Purgeable) {
1716 _mesa_error(ctx, GL_INVALID_OPERATION,
1717 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1718 return GL_VOLATILE_APPLE;
1719 }
1720
1721 bufObj->Purgeable = GL_TRUE;
1722
1723 retval = GL_VOLATILE_APPLE;
1724 if (ctx->Driver.RenderObjectPurgeable)
1725 retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
1726
1727 return retval;
1728 }
1729
1730
1731 static GLenum
1732 texture_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
1733 {
1734 struct gl_texture_object *bufObj;
1735 GLenum retval;
1736
1737 bufObj = _mesa_lookup_texture(ctx, name);
1738 if (!bufObj) {
1739 _mesa_error(ctx, GL_INVALID_VALUE,
1740 "glObjectPurgeable(name = 0x%x)", name);
1741 return 0;
1742 }
1743
1744 if (bufObj->Purgeable) {
1745 _mesa_error(ctx, GL_INVALID_OPERATION,
1746 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1747 return GL_VOLATILE_APPLE;
1748 }
1749
1750 bufObj->Purgeable = GL_TRUE;
1751
1752 retval = GL_VOLATILE_APPLE;
1753 if (ctx->Driver.TextureObjectPurgeable)
1754 retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
1755
1756 return retval;
1757 }
1758
1759
1760 GLenum GLAPIENTRY
1761 _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
1762 {
1763 GLenum retval;
1764
1765 GET_CURRENT_CONTEXT(ctx);
1766 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1767
1768 if (name == 0) {
1769 _mesa_error(ctx, GL_INVALID_VALUE,
1770 "glObjectPurgeable(name = 0x%x)", name);
1771 return 0;
1772 }
1773
1774 switch (option) {
1775 case GL_VOLATILE_APPLE:
1776 case GL_RELEASED_APPLE:
1777 /* legal */
1778 break;
1779 default:
1780 _mesa_error(ctx, GL_INVALID_ENUM,
1781 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1782 name, option);
1783 return 0;
1784 }
1785
1786 switch (objectType) {
1787 case GL_TEXTURE:
1788 retval = texture_object_purgeable(ctx, name, option);
1789 break;
1790 case GL_RENDERBUFFER_EXT:
1791 retval = renderbuffer_purgeable(ctx, name, option);
1792 break;
1793 case GL_BUFFER_OBJECT_APPLE:
1794 retval = buffer_object_purgeable(ctx, name, option);
1795 break;
1796 default:
1797 _mesa_error(ctx, GL_INVALID_ENUM,
1798 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1799 name, objectType);
1800 return 0;
1801 }
1802
1803 /* In strict conformance to the spec, we must only return VOLATILE when
1804 * when passed the VOLATILE option. Madness.
1805 *
1806 * XXX First fix the spec, then fix me.
1807 */
1808 return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
1809 }
1810
1811
1812 static GLenum
1813 buffer_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
1814 {
1815 struct gl_buffer_object *bufObj;
1816 GLenum retval;
1817
1818 bufObj = _mesa_lookup_bufferobj(ctx, name);
1819 if (!bufObj) {
1820 _mesa_error(ctx, GL_INVALID_VALUE,
1821 "glObjectUnpurgeable(name = 0x%x)", name);
1822 return 0;
1823 }
1824
1825 if (! bufObj->Purgeable) {
1826 _mesa_error(ctx, GL_INVALID_OPERATION,
1827 "glObjectUnpurgeable(name = 0x%x) object is "
1828 " already \"unpurged\"", name);
1829 return 0;
1830 }
1831
1832 bufObj->Purgeable = GL_FALSE;
1833
1834 retval = option;
1835 if (ctx->Driver.BufferObjectUnpurgeable)
1836 retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
1837
1838 return retval;
1839 }
1840
1841
1842 static GLenum
1843 renderbuffer_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
1844 {
1845 struct gl_renderbuffer *bufObj;
1846 GLenum retval;
1847
1848 bufObj = _mesa_lookup_renderbuffer(ctx, name);
1849 if (!bufObj) {
1850 _mesa_error(ctx, GL_INVALID_VALUE,
1851 "glObjectUnpurgeable(name = 0x%x)", name);
1852 return 0;
1853 }
1854
1855 if (! bufObj->Purgeable) {
1856 _mesa_error(ctx, GL_INVALID_OPERATION,
1857 "glObjectUnpurgeable(name = 0x%x) object is "
1858 " already \"unpurged\"", name);
1859 return 0;
1860 }
1861
1862 bufObj->Purgeable = GL_FALSE;
1863
1864 retval = option;
1865 if (ctx->Driver.RenderObjectUnpurgeable)
1866 retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
1867
1868 return retval;
1869 }
1870
1871
1872 static GLenum
1873 texture_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
1874 {
1875 struct gl_texture_object *bufObj;
1876 GLenum retval;
1877
1878 bufObj = _mesa_lookup_texture(ctx, name);
1879 if (!bufObj) {
1880 _mesa_error(ctx, GL_INVALID_VALUE,
1881 "glObjectUnpurgeable(name = 0x%x)", name);
1882 return 0;
1883 }
1884
1885 if (! bufObj->Purgeable) {
1886 _mesa_error(ctx, GL_INVALID_OPERATION,
1887 "glObjectUnpurgeable(name = 0x%x) object is"
1888 " already \"unpurged\"", name);
1889 return 0;
1890 }
1891
1892 bufObj->Purgeable = GL_FALSE;
1893
1894 retval = option;
1895 if (ctx->Driver.TextureObjectUnpurgeable)
1896 retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
1897
1898 return retval;
1899 }
1900
1901
1902 GLenum GLAPIENTRY
1903 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
1904 {
1905 GET_CURRENT_CONTEXT(ctx);
1906 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1907
1908 if (name == 0) {
1909 _mesa_error(ctx, GL_INVALID_VALUE,
1910 "glObjectUnpurgeable(name = 0x%x)", name);
1911 return 0;
1912 }
1913
1914 switch (option) {
1915 case GL_RETAINED_APPLE:
1916 case GL_UNDEFINED_APPLE:
1917 /* legal */
1918 break;
1919 default:
1920 _mesa_error(ctx, GL_INVALID_ENUM,
1921 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
1922 name, option);
1923 return 0;
1924 }
1925
1926 switch (objectType) {
1927 case GL_BUFFER_OBJECT_APPLE:
1928 return buffer_object_unpurgeable(ctx, name, option);
1929 case GL_TEXTURE:
1930 return texture_object_unpurgeable(ctx, name, option);
1931 case GL_RENDERBUFFER_EXT:
1932 return renderbuffer_unpurgeable(ctx, name, option);
1933 default:
1934 _mesa_error(ctx, GL_INVALID_ENUM,
1935 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
1936 name, objectType);
1937 return 0;
1938 }
1939 }
1940
1941
1942 static void
1943 get_buffer_object_parameteriv(struct gl_context *ctx, GLuint name,
1944 GLenum pname, GLint *params)
1945 {
1946 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
1947 if (!bufObj) {
1948 _mesa_error(ctx, GL_INVALID_VALUE,
1949 "glGetObjectParameteriv(name = 0x%x) invalid object", name);
1950 return;
1951 }
1952
1953 switch (pname) {
1954 case GL_PURGEABLE_APPLE:
1955 *params = bufObj->Purgeable;
1956 break;
1957 default:
1958 _mesa_error(ctx, GL_INVALID_ENUM,
1959 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1960 name, pname);
1961 break;
1962 }
1963 }
1964
1965
1966 static void
1967 get_renderbuffer_parameteriv(struct gl_context *ctx, GLuint name,
1968 GLenum pname, GLint *params)
1969 {
1970 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
1971 if (!rb) {
1972 _mesa_error(ctx, GL_INVALID_VALUE,
1973 "glObjectUnpurgeable(name = 0x%x)", name);
1974 return;
1975 }
1976
1977 switch (pname) {
1978 case GL_PURGEABLE_APPLE:
1979 *params = rb->Purgeable;
1980 break;
1981 default:
1982 _mesa_error(ctx, GL_INVALID_ENUM,
1983 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
1984 name, pname);
1985 break;
1986 }
1987 }
1988
1989
1990 static void
1991 get_texture_object_parameteriv(struct gl_context *ctx, GLuint name,
1992 GLenum pname, GLint *params)
1993 {
1994 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
1995 if (!texObj) {
1996 _mesa_error(ctx, GL_INVALID_VALUE,
1997 "glObjectUnpurgeable(name = 0x%x)", name);
1998 return;
1999 }
2000
2001 switch (pname) {
2002 case GL_PURGEABLE_APPLE:
2003 *params = texObj->Purgeable;
2004 break;
2005 default:
2006 _mesa_error(ctx, GL_INVALID_ENUM,
2007 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2008 name, pname);
2009 break;
2010 }
2011 }
2012
2013
2014 void GLAPIENTRY
2015 _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
2016 GLint *params)
2017 {
2018 GET_CURRENT_CONTEXT(ctx);
2019
2020 if (name == 0) {
2021 _mesa_error(ctx, GL_INVALID_VALUE,
2022 "glGetObjectParameteriv(name = 0x%x)", name);
2023 return;
2024 }
2025
2026 switch (objectType) {
2027 case GL_TEXTURE:
2028 get_texture_object_parameteriv(ctx, name, pname, params);
2029 break;
2030 case GL_BUFFER_OBJECT_APPLE:
2031 get_buffer_object_parameteriv(ctx, name, pname, params);
2032 break;
2033 case GL_RENDERBUFFER_EXT:
2034 get_renderbuffer_parameteriv(ctx, name, pname, params);
2035 break;
2036 default:
2037 _mesa_error(ctx, GL_INVALID_ENUM,
2038 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2039 name, objectType);
2040 }
2041 }
2042
2043 static void
2044 set_ubo_binding(struct gl_context *ctx,
2045 int index,
2046 struct gl_buffer_object *bufObj,
2047 GLintptr offset,
2048 GLsizeiptr size,
2049 GLboolean autoSize)
2050 {
2051 struct gl_uniform_buffer_binding *binding;
2052
2053 binding = &ctx->UniformBufferBindings[index];
2054 if (binding->BufferObject == bufObj &&
2055 binding->Offset == offset &&
2056 binding->Size == size &&
2057 binding->AutomaticSize == autoSize) {
2058 return;
2059 }
2060
2061 FLUSH_VERTICES(ctx, 0);
2062 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
2063
2064 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2065 binding->Offset = offset;
2066 binding->Size = size;
2067 binding->AutomaticSize = autoSize;
2068 }
2069
2070 /**
2071 * Bind a region of a buffer object to a uniform block binding point.
2072 * \param index the uniform buffer binding point index
2073 * \param bufObj the buffer object
2074 * \param offset offset to the start of buffer object region
2075 * \param size size of the buffer object region
2076 */
2077 static void
2078 bind_buffer_range_uniform_buffer(struct gl_context *ctx,
2079 GLuint index,
2080 struct gl_buffer_object *bufObj,
2081 GLintptr offset,
2082 GLsizeiptr size)
2083 {
2084 if (index >= ctx->Const.MaxUniformBufferBindings) {
2085 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
2086 return;
2087 }
2088
2089 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
2090 _mesa_error(ctx, GL_INVALID_VALUE,
2091 "glBindBufferRange(offset misalgned %d/%d)", (int) offset,
2092 ctx->Const.UniformBufferOffsetAlignment);
2093 return;
2094 }
2095
2096 if (bufObj == ctx->Shared->NullBufferObj) {
2097 offset = -1;
2098 size = -1;
2099 }
2100
2101 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2102 set_ubo_binding(ctx, index, bufObj, offset, size, GL_FALSE);
2103 }
2104
2105
2106 /**
2107 * Bind a buffer object to a uniform block binding point.
2108 * As above, but offset = 0.
2109 */
2110 static void
2111 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
2112 GLuint index,
2113 struct gl_buffer_object *bufObj)
2114 {
2115 if (index >= ctx->Const.MaxUniformBufferBindings) {
2116 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
2117 return;
2118 }
2119
2120 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2121 if (bufObj == ctx->Shared->NullBufferObj)
2122 set_ubo_binding(ctx, index, bufObj, -1, -1, GL_TRUE);
2123 else
2124 set_ubo_binding(ctx, index, bufObj, 0, 0, GL_TRUE);
2125 }
2126
2127 static void
2128 set_atomic_buffer_binding(struct gl_context *ctx,
2129 unsigned index,
2130 struct gl_buffer_object *bufObj,
2131 GLintptr offset,
2132 GLsizeiptr size,
2133 const char *name)
2134 {
2135 struct gl_atomic_buffer_binding *binding;
2136
2137 if (index >= ctx->Const.MaxAtomicBufferBindings) {
2138 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index);
2139 return;
2140 }
2141
2142 if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
2143 _mesa_error(ctx, GL_INVALID_VALUE,
2144 "%s(offset misalgned %d/%d)", name, (int) offset,
2145 ATOMIC_COUNTER_SIZE);
2146 return;
2147 }
2148
2149 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
2150
2151 binding = &ctx->AtomicBufferBindings[index];
2152 if (binding->BufferObject == bufObj &&
2153 binding->Offset == offset &&
2154 binding->Size == size) {
2155 return;
2156 }
2157
2158 FLUSH_VERTICES(ctx, 0);
2159 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
2160
2161 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2162
2163 if (bufObj == ctx->Shared->NullBufferObj) {
2164 binding->Offset = -1;
2165 binding->Size = -1;
2166 } else {
2167 binding->Offset = offset;
2168 binding->Size = size;
2169 }
2170 }
2171
2172 void GLAPIENTRY
2173 _mesa_BindBufferRange(GLenum target, GLuint index,
2174 GLuint buffer, GLintptr offset, GLsizeiptr size)
2175 {
2176 GET_CURRENT_CONTEXT(ctx);
2177 struct gl_buffer_object *bufObj;
2178
2179 if (buffer == 0) {
2180 bufObj = ctx->Shared->NullBufferObj;
2181 } else {
2182 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2183 }
2184 if (!handle_bind_buffer_gen(ctx, target, buffer, &bufObj))
2185 return;
2186
2187 if (!bufObj) {
2188 _mesa_error(ctx, GL_INVALID_OPERATION,
2189 "glBindBufferRange(invalid buffer=%u)", buffer);
2190 return;
2191 }
2192
2193 if (buffer != 0) {
2194 if (size <= 0) {
2195 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
2196 (int) size);
2197 return;
2198 }
2199 }
2200
2201 switch (target) {
2202 case GL_TRANSFORM_FEEDBACK_BUFFER:
2203 _mesa_bind_buffer_range_transform_feedback(ctx, index, bufObj,
2204 offset, size);
2205 return;
2206 case GL_UNIFORM_BUFFER:
2207 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
2208 return;
2209 case GL_ATOMIC_COUNTER_BUFFER:
2210 set_atomic_buffer_binding(ctx, index, bufObj, offset, size,
2211 "glBindBufferRange");
2212 return;
2213 default:
2214 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
2215 return;
2216 }
2217 }
2218
2219 void GLAPIENTRY
2220 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
2221 {
2222 GET_CURRENT_CONTEXT(ctx);
2223 struct gl_buffer_object *bufObj;
2224
2225 if (buffer == 0) {
2226 bufObj = ctx->Shared->NullBufferObj;
2227 } else {
2228 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2229 }
2230 if (!handle_bind_buffer_gen(ctx, target, buffer, &bufObj))
2231 return;
2232
2233 if (!bufObj) {
2234 _mesa_error(ctx, GL_INVALID_OPERATION,
2235 "glBindBufferBase(invalid buffer=%u)", buffer);
2236 return;
2237 }
2238
2239 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
2240 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
2241 *
2242 * "BindBufferBase is equivalent to calling BindBufferRange with offset
2243 * zero and size equal to the size of buffer."
2244 *
2245 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
2246 *
2247 * "If the parameter (starting offset or size) was not specified when the
2248 * buffer object was bound, zero is returned."
2249 *
2250 * What happens if the size of the buffer changes? Does the size of the
2251 * buffer at the moment glBindBufferBase was called still play a role, like
2252 * the first quote would imply, or is the size meaningless in the
2253 * glBindBufferBase case like the second quote would suggest? The GL 4.1
2254 * core spec page 45 says:
2255 *
2256 * "It is equivalent to calling BindBufferRange with offset zero, while
2257 * size is determined by the size of the bound buffer at the time the
2258 * binding is used."
2259 *
2260 * My interpretation is that the GL 4.1 spec was a clarification of the
2261 * behavior, not a change. In particular, this choice will only make
2262 * rendering work in cases where it would have had undefined results.
2263 */
2264
2265 switch (target) {
2266 case GL_TRANSFORM_FEEDBACK_BUFFER:
2267 _mesa_bind_buffer_base_transform_feedback(ctx, index, bufObj);
2268 return;
2269 case GL_UNIFORM_BUFFER:
2270 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
2271 return;
2272 case GL_ATOMIC_COUNTER_BUFFER:
2273 set_atomic_buffer_binding(ctx, index, bufObj, 0, 0,
2274 "glBindBufferBase");
2275 return;
2276 default:
2277 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
2278 return;
2279 }
2280 }
2281
2282 void GLAPIENTRY
2283 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
2284 GLsizeiptr length)
2285 {
2286 GET_CURRENT_CONTEXT(ctx);
2287 struct gl_buffer_object *bufObj;
2288 const GLintptr end = offset + length;
2289
2290 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2291 if (!bufObj) {
2292 _mesa_error(ctx, GL_INVALID_VALUE,
2293 "glInvalidateBufferSubData(name = 0x%x) invalid object",
2294 buffer);
2295 return;
2296 }
2297
2298 /* The GL_ARB_invalidate_subdata spec says:
2299 *
2300 * "An INVALID_VALUE error is generated if <offset> or <length> is
2301 * negative, or if <offset> + <length> is greater than the value of
2302 * BUFFER_SIZE."
2303 */
2304 if (end < 0 || end > bufObj->Size) {
2305 _mesa_error(ctx, GL_INVALID_VALUE,
2306 "glInvalidateBufferSubData(invalid offset or length)");
2307 return;
2308 }
2309
2310 /* The GL_ARB_invalidate_subdata spec says:
2311 *
2312 * "An INVALID_OPERATION error is generated if the buffer is currently
2313 * mapped by MapBuffer, or if the invalidate range intersects the range
2314 * currently mapped by MapBufferRange."
2315 */
2316 if (_mesa_bufferobj_mapped(bufObj)) {
2317 const GLintptr mapEnd = bufObj->Offset + bufObj->Length;
2318
2319 /* The regions do not overlap if and only if the end of the discard
2320 * region is before the mapped region or the start of the discard region
2321 * is after the mapped region.
2322 *
2323 * Note that 'end' and 'mapEnd' are the first byte *after* the discard
2324 * region and the mapped region, repsectively. It is okay for that byte
2325 * to be mapped (for 'end') or discarded (for 'mapEnd').
2326 */
2327 if (!(end <= bufObj->Offset || offset >= mapEnd)) {
2328 _mesa_error(ctx, GL_INVALID_OPERATION,
2329 "glInvalidateBufferSubData(intersection with mapped "
2330 "range)");
2331 return;
2332 }
2333 }
2334
2335 /* We don't actually do anything for this yet. Just return after
2336 * validating the parameters and generating the required errors.
2337 */
2338 return;
2339 }
2340
2341 void GLAPIENTRY
2342 _mesa_InvalidateBufferData(GLuint buffer)
2343 {
2344 GET_CURRENT_CONTEXT(ctx);
2345 struct gl_buffer_object *bufObj;
2346
2347 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2348 if (!bufObj) {
2349 _mesa_error(ctx, GL_INVALID_VALUE,
2350 "glInvalidateBufferData(name = 0x%x) invalid object",
2351 buffer);
2352 return;
2353 }
2354
2355 /* The GL_ARB_invalidate_subdata spec says:
2356 *
2357 * "An INVALID_OPERATION error is generated if the buffer is currently
2358 * mapped by MapBuffer, or if the invalidate range intersects the range
2359 * currently mapped by MapBufferRange."
2360 */
2361 if (_mesa_bufferobj_mapped(bufObj)) {
2362 _mesa_error(ctx, GL_INVALID_OPERATION,
2363 "glInvalidateBufferData(intersection with mapped "
2364 "range)");
2365 return;
2366 }
2367
2368 /* We don't actually do anything for this yet. Just return after
2369 * validating the parameters and generating the required errors.
2370 */
2371 return;
2372 }