main: Add utility function _mesa_lookup_bufferobj_err.
[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 <inttypes.h> /* for PRId64 macro */
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 "teximage.h"
46 #include "glformats.h"
47 #include "texstore.h"
48 #include "transformfeedback.h"
49 #include "dispatch.h"
50
51
52 /* Debug flags */
53 /*#define VBO_DEBUG*/
54 /*#define BOUNDS_CHECK*/
55
56
57 /**
58 * Used as a placeholder for buffer objects between glGenBuffers() and
59 * glBindBuffer() so that glIsBuffer() can work correctly.
60 */
61 static struct gl_buffer_object DummyBufferObject;
62
63
64 /**
65 * Return pointer to address of a buffer object target.
66 * \param ctx the GL context
67 * \param target the buffer object target to be retrieved.
68 * \return pointer to pointer to the buffer object bound to \c target in the
69 * specified context or \c NULL if \c target is invalid.
70 */
71 static inline struct gl_buffer_object **
72 get_buffer_target(struct gl_context *ctx, GLenum target)
73 {
74 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
75 */
76 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx)
77 && target != GL_ARRAY_BUFFER && target != GL_ELEMENT_ARRAY_BUFFER)
78 return NULL;
79
80 switch (target) {
81 case GL_ARRAY_BUFFER_ARB:
82 return &ctx->Array.ArrayBufferObj;
83 case GL_ELEMENT_ARRAY_BUFFER_ARB:
84 return &ctx->Array.VAO->IndexBufferObj;
85 case GL_PIXEL_PACK_BUFFER_EXT:
86 return &ctx->Pack.BufferObj;
87 case GL_PIXEL_UNPACK_BUFFER_EXT:
88 return &ctx->Unpack.BufferObj;
89 case GL_COPY_READ_BUFFER:
90 return &ctx->CopyReadBuffer;
91 case GL_COPY_WRITE_BUFFER:
92 return &ctx->CopyWriteBuffer;
93 case GL_DRAW_INDIRECT_BUFFER:
94 if (ctx->API == API_OPENGL_CORE &&
95 ctx->Extensions.ARB_draw_indirect) {
96 return &ctx->DrawIndirectBuffer;
97 }
98 break;
99 case GL_TRANSFORM_FEEDBACK_BUFFER:
100 if (ctx->Extensions.EXT_transform_feedback) {
101 return &ctx->TransformFeedback.CurrentBuffer;
102 }
103 break;
104 case GL_TEXTURE_BUFFER:
105 if (ctx->API == API_OPENGL_CORE &&
106 ctx->Extensions.ARB_texture_buffer_object) {
107 return &ctx->Texture.BufferObject;
108 }
109 break;
110 case GL_UNIFORM_BUFFER:
111 if (ctx->Extensions.ARB_uniform_buffer_object) {
112 return &ctx->UniformBuffer;
113 }
114 break;
115 case GL_ATOMIC_COUNTER_BUFFER:
116 if (ctx->Extensions.ARB_shader_atomic_counters) {
117 return &ctx->AtomicBuffer;
118 }
119 break;
120 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD:
121 if (ctx->Extensions.AMD_pinned_memory) {
122 return &ctx->ExternalVirtualMemoryBuffer;
123 }
124 break;
125 default:
126 return NULL;
127 }
128 return NULL;
129 }
130
131
132 /**
133 * Get the buffer object bound to the specified target in a GL context.
134 * \param ctx the GL context
135 * \param target the buffer object target to be retrieved.
136 * \param error the GL error to record if target is illegal.
137 * \return pointer to the buffer object bound to \c target in the
138 * specified context or \c NULL if \c target is invalid.
139 */
140 static inline struct gl_buffer_object *
141 get_buffer(struct gl_context *ctx, const char *func, GLenum target,
142 GLenum error)
143 {
144 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
145
146 if (!bufObj) {
147 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
148 return NULL;
149 }
150
151 if (!_mesa_is_bufferobj(*bufObj)) {
152 _mesa_error(ctx, error, "%s(no buffer bound)", func);
153 return NULL;
154 }
155
156 return *bufObj;
157 }
158
159
160 /**
161 * Convert a GLbitfield describing the mapped buffer access flags
162 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
163 */
164 static GLenum
165 simplified_access_mode(struct gl_context *ctx, GLbitfield access)
166 {
167 const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
168 if ((access & rwFlags) == rwFlags)
169 return GL_READ_WRITE;
170 if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
171 return GL_READ_ONLY;
172 if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
173 return GL_WRITE_ONLY;
174
175 /* Otherwise, AccessFlags is zero (the default state).
176 *
177 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
178 *
179 * Name Type Initial Value Legal Values
180 * ... ... ... ...
181 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
182 * READ_WRITE
183 *
184 * However, table 6.8 in the GL_OES_mapbuffer extension says:
185 *
186 * Get Value Type Get Command Value Description
187 * --------- ---- ----------- ----- -----------
188 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
189 *
190 * The difference is because GL_OES_mapbuffer only supports mapping buffers
191 * write-only.
192 */
193 assert(access == 0);
194
195 return _mesa_is_gles(ctx) ? GL_WRITE_ONLY : GL_READ_WRITE;
196 }
197
198
199 /**
200 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
201 * given range.
202 * The regions do not overlap if and only if the end of the given
203 * region is before the mapped region or the start of the given region
204 * is after the mapped region.
205 *
206 * \param obj Buffer object target on which to operate.
207 * \param offset Offset of the first byte of the subdata range.
208 * \param size Size, in bytes, of the subdata range.
209 * \return true if ranges overlap, false otherwise
210 *
211 */
212 static bool
213 bufferobj_range_mapped(const struct gl_buffer_object *obj,
214 GLintptr offset, GLsizeiptr size)
215 {
216 if (_mesa_bufferobj_mapped(obj, MAP_USER)) {
217 const GLintptr end = offset + size;
218 const GLintptr mapEnd = obj->Mappings[MAP_USER].Offset +
219 obj->Mappings[MAP_USER].Length;
220
221 if (!(end <= obj->Mappings[MAP_USER].Offset || offset >= mapEnd)) {
222 return true;
223 }
224 }
225 return false;
226 }
227
228
229 /**
230 * Tests the subdata range parameters and sets the GL error code for
231 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
232 * \c glClearBufferSubData.
233 *
234 * \param ctx GL context.
235 * \param target Buffer object target on which to operate.
236 * \param offset Offset of the first byte of the subdata range.
237 * \param size Size, in bytes, of the subdata range.
238 * \param mappedRange If true, checks if an overlapping range is mapped.
239 * If false, checks if buffer is mapped.
240 * \param errorNoBuffer Error code if no buffer is bound to target.
241 * \param caller Name of calling function for recording errors.
242 * \return A pointer to the buffer object bound to \c target in the
243 * specified context or \c NULL if any of the parameter or state
244 * conditions are invalid.
245 *
246 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
247 */
248 static struct gl_buffer_object *
249 buffer_object_subdata_range_good(struct gl_context * ctx, GLenum target,
250 GLintptrARB offset, GLsizeiptrARB size,
251 bool mappedRange, GLenum errorNoBuffer,
252 const char *caller)
253 {
254 struct gl_buffer_object *bufObj;
255
256 if (size < 0) {
257 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
258 return NULL;
259 }
260
261 if (offset < 0) {
262 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
263 return NULL;
264 }
265
266 bufObj = get_buffer(ctx, caller, target, errorNoBuffer);
267 if (!bufObj)
268 return NULL;
269
270 if (offset + size > bufObj->Size) {
271 _mesa_error(ctx, GL_INVALID_VALUE,
272 "%s(offset %lu + size %lu > buffer size %lu)", caller,
273 (unsigned long) offset,
274 (unsigned long) size,
275 (unsigned long) bufObj->Size);
276 return NULL;
277 }
278
279 if (bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT)
280 return bufObj;
281
282 if (mappedRange) {
283 if (bufferobj_range_mapped(bufObj, offset, size)) {
284 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
285 return NULL;
286 }
287 }
288 else {
289 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
290 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
291 return NULL;
292 }
293 }
294
295 return bufObj;
296 }
297
298
299 /**
300 * Test the format and type parameters and set the GL error code for
301 * \c glClearBufferData and \c glClearBufferSubData.
302 *
303 * \param ctx GL context.
304 * \param internalformat Format to which the data is to be converted.
305 * \param format Format of the supplied data.
306 * \param type Type of the supplied data.
307 * \param caller Name of calling function for recording errors.
308 * \return If internalformat, format and type are legal the mesa_format
309 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
310 *
311 * \sa glClearBufferData and glClearBufferSubData
312 */
313 static mesa_format
314 validate_clear_buffer_format(struct gl_context *ctx,
315 GLenum internalformat,
316 GLenum format, GLenum type,
317 const char *caller)
318 {
319 mesa_format mesaFormat;
320 GLenum errorFormatType;
321
322 mesaFormat = _mesa_validate_texbuffer_format(ctx, internalformat);
323 if (mesaFormat == MESA_FORMAT_NONE) {
324 _mesa_error(ctx, GL_INVALID_ENUM,
325 "%s(invalid internalformat)", caller);
326 return MESA_FORMAT_NONE;
327 }
328
329 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
330 * EXT_texture_integer there is no conversion between integer and
331 * non-integer formats
332 */
333 if (_mesa_is_enum_format_signed_int(format) !=
334 _mesa_is_format_integer_color(mesaFormat)) {
335 _mesa_error(ctx, GL_INVALID_OPERATION,
336 "%s(integer vs non-integer)", caller);
337 return MESA_FORMAT_NONE;
338 }
339
340 if (!_mesa_is_color_format(format)) {
341 _mesa_error(ctx, GL_INVALID_ENUM,
342 "%s(format is not a color format)", caller);
343 return MESA_FORMAT_NONE;
344 }
345
346 errorFormatType = _mesa_error_check_format_and_type(ctx, format, type);
347 if (errorFormatType != GL_NO_ERROR) {
348 _mesa_error(ctx, GL_INVALID_ENUM,
349 "%s(invalid format or type)", caller);
350 return MESA_FORMAT_NONE;
351 }
352
353 return mesaFormat;
354 }
355
356
357 /**
358 * Convert user-specified clear value to the specified internal format.
359 *
360 * \param ctx GL context.
361 * \param internalformat Format to which the data is converted.
362 * \param clearValue Points to the converted clear value.
363 * \param format Format of the supplied data.
364 * \param type Type of the supplied data.
365 * \param data Data which is to be converted to internalformat.
366 * \param caller Name of calling function for recording errors.
367 * \return true if data could be converted, false otherwise.
368 *
369 * \sa glClearBufferData, glClearBufferSubData
370 */
371 static bool
372 convert_clear_buffer_data(struct gl_context *ctx,
373 mesa_format internalformat,
374 GLubyte *clearValue, GLenum format, GLenum type,
375 const GLvoid *data, const char *caller)
376 {
377 GLenum internalformatBase = _mesa_get_format_base_format(internalformat);
378
379 if (_mesa_texstore(ctx, 1, internalformatBase, internalformat,
380 0, &clearValue, 1, 1, 1,
381 format, type, data, &ctx->Unpack)) {
382 return true;
383 }
384 else {
385 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
386 return false;
387 }
388 }
389
390
391 /**
392 * Allocate and initialize a new buffer object.
393 *
394 * Default callback for the \c dd_function_table::NewBufferObject() hook.
395 */
396 static struct gl_buffer_object *
397 _mesa_new_buffer_object(struct gl_context *ctx, GLuint name)
398 {
399 struct gl_buffer_object *obj;
400
401 (void) ctx;
402
403 obj = MALLOC_STRUCT(gl_buffer_object);
404 _mesa_initialize_buffer_object(ctx, obj, name);
405 return obj;
406 }
407
408
409 /**
410 * Delete a buffer object.
411 *
412 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
413 */
414 static void
415 _mesa_delete_buffer_object(struct gl_context *ctx,
416 struct gl_buffer_object *bufObj)
417 {
418 (void) ctx;
419
420 _mesa_align_free(bufObj->Data);
421
422 /* assign strange values here to help w/ debugging */
423 bufObj->RefCount = -1000;
424 bufObj->Name = ~0;
425
426 mtx_destroy(&bufObj->Mutex);
427 free(bufObj->Label);
428 free(bufObj);
429 }
430
431
432
433 /**
434 * Set ptr to bufObj w/ reference counting.
435 * This is normally only called from the _mesa_reference_buffer_object() macro
436 * when there's a real pointer change.
437 */
438 void
439 _mesa_reference_buffer_object_(struct gl_context *ctx,
440 struct gl_buffer_object **ptr,
441 struct gl_buffer_object *bufObj)
442 {
443 if (*ptr) {
444 /* Unreference the old buffer */
445 GLboolean deleteFlag = GL_FALSE;
446 struct gl_buffer_object *oldObj = *ptr;
447
448 mtx_lock(&oldObj->Mutex);
449 assert(oldObj->RefCount > 0);
450 oldObj->RefCount--;
451 #if 0
452 printf("BufferObj %p %d DECR to %d\n",
453 (void *) oldObj, oldObj->Name, oldObj->RefCount);
454 #endif
455 deleteFlag = (oldObj->RefCount == 0);
456 mtx_unlock(&oldObj->Mutex);
457
458 if (deleteFlag) {
459
460 /* some sanity checking: don't delete a buffer still in use */
461 #if 0
462 /* unfortunately, these tests are invalid during context tear-down */
463 assert(ctx->Array.ArrayBufferObj != bufObj);
464 assert(ctx->Array.VAO->IndexBufferObj != bufObj);
465 assert(ctx->Array.VAO->Vertex.BufferObj != bufObj);
466 #endif
467
468 assert(ctx->Driver.DeleteBuffer);
469 ctx->Driver.DeleteBuffer(ctx, oldObj);
470 }
471
472 *ptr = NULL;
473 }
474 assert(!*ptr);
475
476 if (bufObj) {
477 /* reference new buffer */
478 mtx_lock(&bufObj->Mutex);
479 if (bufObj->RefCount == 0) {
480 /* this buffer's being deleted (look just above) */
481 /* Not sure this can every really happen. Warn if it does. */
482 _mesa_problem(NULL, "referencing deleted buffer object");
483 *ptr = NULL;
484 }
485 else {
486 bufObj->RefCount++;
487 #if 0
488 printf("BufferObj %p %d INCR to %d\n",
489 (void *) bufObj, bufObj->Name, bufObj->RefCount);
490 #endif
491 *ptr = bufObj;
492 }
493 mtx_unlock(&bufObj->Mutex);
494 }
495 }
496
497
498 /**
499 * Initialize a buffer object to default values.
500 */
501 void
502 _mesa_initialize_buffer_object(struct gl_context *ctx,
503 struct gl_buffer_object *obj,
504 GLuint name)
505 {
506 memset(obj, 0, sizeof(struct gl_buffer_object));
507 mtx_init(&obj->Mutex, mtx_plain);
508 obj->RefCount = 1;
509 obj->Name = name;
510 obj->Usage = GL_STATIC_DRAW_ARB;
511 }
512
513
514
515 /**
516 * Callback called from _mesa_HashWalk()
517 */
518 static void
519 count_buffer_size(GLuint key, void *data, void *userData)
520 {
521 const struct gl_buffer_object *bufObj =
522 (const struct gl_buffer_object *) data;
523 GLuint *total = (GLuint *) userData;
524
525 *total = *total + bufObj->Size;
526 }
527
528
529 /**
530 * Compute total size (in bytes) of all buffer objects for the given context.
531 * For debugging purposes.
532 */
533 GLuint
534 _mesa_total_buffer_object_memory(struct gl_context *ctx)
535 {
536 GLuint total = 0;
537
538 _mesa_HashWalk(ctx->Shared->BufferObjects, count_buffer_size, &total);
539
540 return total;
541 }
542
543
544 /**
545 * Allocate space for and store data in a buffer object. Any data that was
546 * previously stored in the buffer object is lost. If \c data is \c NULL,
547 * memory will be allocated, but no copy will occur.
548 *
549 * This is the default callback for \c dd_function_table::BufferData()
550 * Note that all GL error checking will have been done already.
551 *
552 * \param ctx GL context.
553 * \param target Buffer object target on which to operate.
554 * \param size Size, in bytes, of the new data store.
555 * \param data Pointer to the data to store in the buffer object. This
556 * pointer may be \c NULL.
557 * \param usage Hints about how the data will be used.
558 * \param bufObj Object to be used.
559 *
560 * \return GL_TRUE for success, GL_FALSE for failure
561 * \sa glBufferDataARB, dd_function_table::BufferData.
562 */
563 static GLboolean
564 _mesa_buffer_data( struct gl_context *ctx, GLenum target, GLsizeiptrARB size,
565 const GLvoid * data, GLenum usage, GLenum storageFlags,
566 struct gl_buffer_object * bufObj )
567 {
568 void * new_data;
569
570 (void) target;
571
572 _mesa_align_free( bufObj->Data );
573
574 new_data = _mesa_align_malloc( size, ctx->Const.MinMapBufferAlignment );
575 if (new_data) {
576 bufObj->Data = (GLubyte *) new_data;
577 bufObj->Size = size;
578 bufObj->Usage = usage;
579 bufObj->StorageFlags = storageFlags;
580
581 if (data) {
582 memcpy( bufObj->Data, data, size );
583 }
584
585 return GL_TRUE;
586 }
587 else {
588 return GL_FALSE;
589 }
590 }
591
592
593 /**
594 * Replace data in a subrange of buffer object. If the data range
595 * specified by \c size + \c offset extends beyond the end of the buffer or
596 * if \c data is \c NULL, no copy is performed.
597 *
598 * This is the default callback for \c dd_function_table::BufferSubData()
599 * Note that all GL error checking will have been done already.
600 *
601 * \param ctx GL context.
602 * \param offset Offset of the first byte to be modified.
603 * \param size Size, in bytes, of the data range.
604 * \param data Pointer to the data to store in the buffer object.
605 * \param bufObj Object to be used.
606 *
607 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
608 */
609 static void
610 _mesa_buffer_subdata( struct gl_context *ctx, GLintptrARB offset,
611 GLsizeiptrARB size, const GLvoid * data,
612 struct gl_buffer_object * bufObj )
613 {
614 (void) ctx;
615
616 /* this should have been caught in _mesa_BufferSubData() */
617 assert(size + offset <= bufObj->Size);
618
619 if (bufObj->Data) {
620 memcpy( (GLubyte *) bufObj->Data + offset, data, size );
621 }
622 }
623
624
625 /**
626 * Retrieve data from a subrange of buffer object. If the data range
627 * specified by \c size + \c offset extends beyond the end of the buffer or
628 * if \c data is \c NULL, no copy is performed.
629 *
630 * This is the default callback for \c dd_function_table::GetBufferSubData()
631 * Note that all GL error checking will have been done already.
632 *
633 * \param ctx GL context.
634 * \param target Buffer object target on which to operate.
635 * \param offset Offset of the first byte to be fetched.
636 * \param size Size, in bytes, of the data range.
637 * \param data Destination for data
638 * \param bufObj Object to be used.
639 *
640 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
641 */
642 static void
643 _mesa_buffer_get_subdata( struct gl_context *ctx, GLintptrARB offset,
644 GLsizeiptrARB size, GLvoid * data,
645 struct gl_buffer_object * bufObj )
646 {
647 (void) ctx;
648
649 if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
650 memcpy( data, (GLubyte *) bufObj->Data + offset, size );
651 }
652 }
653
654
655 /**
656 * Clear a subrange of the buffer object with copies of the supplied data.
657 * If data is NULL the buffer is filled with zeros.
658 *
659 * This is the default callback for \c dd_function_table::ClearBufferSubData()
660 * Note that all GL error checking will have been done already.
661 *
662 * \param ctx GL context.
663 * \param offset Offset of the first byte to be cleared.
664 * \param size Size, in bytes, of the to be cleared range.
665 * \param clearValue Source of the data.
666 * \param clearValueSize Size, in bytes, of the supplied data.
667 * \param bufObj Object to be cleared.
668 *
669 * \sa glClearBufferSubData, glClearBufferData and
670 * dd_function_table::ClearBufferSubData.
671 */
672 void
673 _mesa_buffer_clear_subdata(struct gl_context *ctx,
674 GLintptr offset, GLsizeiptr size,
675 const GLvoid *clearValue,
676 GLsizeiptr clearValueSize,
677 struct gl_buffer_object *bufObj)
678 {
679 GLsizeiptr i;
680 GLubyte *dest;
681
682 assert(ctx->Driver.MapBufferRange);
683 dest = ctx->Driver.MapBufferRange(ctx, offset, size,
684 GL_MAP_WRITE_BIT |
685 GL_MAP_INVALIDATE_RANGE_BIT,
686 bufObj, MAP_INTERNAL);
687
688 if (!dest) {
689 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearBuffer[Sub]Data");
690 return;
691 }
692
693 if (clearValue == NULL) {
694 /* Clear with zeros, per the spec */
695 memset(dest, 0, size);
696 ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL);
697 return;
698 }
699
700 for (i = 0; i < size/clearValueSize; ++i) {
701 memcpy(dest, clearValue, clearValueSize);
702 dest += clearValueSize;
703 }
704
705 ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL);
706 }
707
708
709 /**
710 * Default fallback for \c dd_function_table::MapBufferRange().
711 * Called via glMapBufferRange().
712 */
713 static void *
714 _mesa_buffer_map_range( struct gl_context *ctx, GLintptr offset,
715 GLsizeiptr length, GLbitfield access,
716 struct gl_buffer_object *bufObj,
717 gl_map_buffer_index index)
718 {
719 (void) ctx;
720 assert(!_mesa_bufferobj_mapped(bufObj, index));
721 /* Just return a direct pointer to the data */
722 bufObj->Mappings[index].Pointer = bufObj->Data + offset;
723 bufObj->Mappings[index].Length = length;
724 bufObj->Mappings[index].Offset = offset;
725 bufObj->Mappings[index].AccessFlags = access;
726 return bufObj->Mappings[index].Pointer;
727 }
728
729
730 /**
731 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
732 * Called via glFlushMappedBufferRange().
733 */
734 static void
735 _mesa_buffer_flush_mapped_range( struct gl_context *ctx,
736 GLintptr offset, GLsizeiptr length,
737 struct gl_buffer_object *obj,
738 gl_map_buffer_index index)
739 {
740 (void) ctx;
741 (void) offset;
742 (void) length;
743 (void) obj;
744 /* no-op */
745 }
746
747
748 /**
749 * Default callback for \c dd_function_table::MapBuffer().
750 *
751 * The input parameters will have been already tested for errors.
752 *
753 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
754 */
755 static GLboolean
756 _mesa_buffer_unmap(struct gl_context *ctx, struct gl_buffer_object *bufObj,
757 gl_map_buffer_index index)
758 {
759 (void) ctx;
760 /* XXX we might assert here that bufObj->Pointer is non-null */
761 bufObj->Mappings[index].Pointer = NULL;
762 bufObj->Mappings[index].Length = 0;
763 bufObj->Mappings[index].Offset = 0;
764 bufObj->Mappings[index].AccessFlags = 0x0;
765 return GL_TRUE;
766 }
767
768
769 /**
770 * Default fallback for \c dd_function_table::CopyBufferSubData().
771 * Called via glCopyBufferSubData().
772 */
773 static void
774 _mesa_copy_buffer_subdata(struct gl_context *ctx,
775 struct gl_buffer_object *src,
776 struct gl_buffer_object *dst,
777 GLintptr readOffset, GLintptr writeOffset,
778 GLsizeiptr size)
779 {
780 GLubyte *srcPtr, *dstPtr;
781
782 if (src == dst) {
783 srcPtr = dstPtr = ctx->Driver.MapBufferRange(ctx, 0, src->Size,
784 GL_MAP_READ_BIT |
785 GL_MAP_WRITE_BIT, src,
786 MAP_INTERNAL);
787
788 if (!srcPtr)
789 return;
790
791 srcPtr += readOffset;
792 dstPtr += writeOffset;
793 } else {
794 srcPtr = ctx->Driver.MapBufferRange(ctx, readOffset, size,
795 GL_MAP_READ_BIT, src,
796 MAP_INTERNAL);
797 dstPtr = ctx->Driver.MapBufferRange(ctx, writeOffset, size,
798 (GL_MAP_WRITE_BIT |
799 GL_MAP_INVALIDATE_RANGE_BIT), dst,
800 MAP_INTERNAL);
801 }
802
803 /* Note: the src and dst regions will never overlap. Trying to do so
804 * would generate GL_INVALID_VALUE earlier.
805 */
806 if (srcPtr && dstPtr)
807 memcpy(dstPtr, srcPtr, size);
808
809 ctx->Driver.UnmapBuffer(ctx, src, MAP_INTERNAL);
810 if (dst != src)
811 ctx->Driver.UnmapBuffer(ctx, dst, MAP_INTERNAL);
812 }
813
814
815
816 /**
817 * Initialize the state associated with buffer objects
818 */
819 void
820 _mesa_init_buffer_objects( struct gl_context *ctx )
821 {
822 GLuint i;
823
824 memset(&DummyBufferObject, 0, sizeof(DummyBufferObject));
825 mtx_init(&DummyBufferObject.Mutex, mtx_plain);
826 DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
827
828 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj,
829 ctx->Shared->NullBufferObj);
830
831 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer,
832 ctx->Shared->NullBufferObj);
833 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
834 ctx->Shared->NullBufferObj);
835
836 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer,
837 ctx->Shared->NullBufferObj);
838
839 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer,
840 ctx->Shared->NullBufferObj);
841
842 _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer,
843 ctx->Shared->NullBufferObj);
844
845 for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
846 _mesa_reference_buffer_object(ctx,
847 &ctx->UniformBufferBindings[i].BufferObject,
848 ctx->Shared->NullBufferObj);
849 ctx->UniformBufferBindings[i].Offset = -1;
850 ctx->UniformBufferBindings[i].Size = -1;
851 }
852
853 for (i = 0; i < MAX_COMBINED_ATOMIC_BUFFERS; i++) {
854 _mesa_reference_buffer_object(ctx,
855 &ctx->AtomicBufferBindings[i].BufferObject,
856 ctx->Shared->NullBufferObj);
857 ctx->AtomicBufferBindings[i].Offset = -1;
858 ctx->AtomicBufferBindings[i].Size = -1;
859 }
860 }
861
862
863 void
864 _mesa_free_buffer_objects( struct gl_context *ctx )
865 {
866 GLuint i;
867
868 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
869
870 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL);
871 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, NULL);
872
873 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, NULL);
874
875 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, NULL);
876
877 _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer, NULL);
878
879 for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
880 _mesa_reference_buffer_object(ctx,
881 &ctx->UniformBufferBindings[i].BufferObject,
882 NULL);
883 }
884
885 for (i = 0; i < MAX_COMBINED_ATOMIC_BUFFERS; i++) {
886 _mesa_reference_buffer_object(ctx,
887 &ctx->AtomicBufferBindings[i].BufferObject,
888 NULL);
889 }
890
891 }
892
893 bool
894 _mesa_handle_bind_buffer_gen(struct gl_context *ctx,
895 GLenum target,
896 GLuint buffer,
897 struct gl_buffer_object **buf_handle,
898 const char *caller)
899 {
900 struct gl_buffer_object *buf = *buf_handle;
901
902 if (!buf && ctx->API == API_OPENGL_CORE) {
903 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", caller);
904 return false;
905 }
906
907 if (!buf || buf == &DummyBufferObject) {
908 /* If this is a new buffer object id, or one which was generated but
909 * never used before, allocate a buffer object now.
910 */
911 assert(ctx->Driver.NewBufferObject);
912 buf = ctx->Driver.NewBufferObject(ctx, buffer);
913 if (!buf) {
914 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
915 return false;
916 }
917 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, buf);
918 *buf_handle = buf;
919 }
920
921 return true;
922 }
923
924 /**
925 * Bind the specified target to buffer for the specified context.
926 * Called by glBindBuffer() and other functions.
927 */
928 static void
929 bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
930 {
931 struct gl_buffer_object *oldBufObj;
932 struct gl_buffer_object *newBufObj = NULL;
933 struct gl_buffer_object **bindTarget = NULL;
934
935 bindTarget = get_buffer_target(ctx, target);
936 if (!bindTarget) {
937 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
938 return;
939 }
940
941 /* Get pointer to old buffer object (to be unbound) */
942 oldBufObj = *bindTarget;
943 if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
944 return; /* rebinding the same buffer object- no change */
945
946 /*
947 * Get pointer to new buffer object (newBufObj)
948 */
949 if (buffer == 0) {
950 /* The spec says there's not a buffer object named 0, but we use
951 * one internally because it simplifies things.
952 */
953 newBufObj = ctx->Shared->NullBufferObj;
954 }
955 else {
956 /* non-default buffer object */
957 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
958 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
959 &newBufObj, "glBindBuffer"))
960 return;
961 }
962
963 /* bind new buffer */
964 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
965 }
966
967
968 /**
969 * Update the default buffer objects in the given context to reference those
970 * specified in the shared state and release those referencing the old
971 * shared state.
972 */
973 void
974 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
975 {
976 /* Bind the NullBufferObj to remove references to those
977 * in the shared context hash table.
978 */
979 bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
980 bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
981 bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
982 bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
983 }
984
985
986
987 /**
988 * Return the gl_buffer_object for the given ID.
989 * Always return NULL for ID 0.
990 */
991 struct gl_buffer_object *
992 _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
993 {
994 if (buffer == 0)
995 return NULL;
996 else
997 return (struct gl_buffer_object *)
998 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
999 }
1000
1001
1002 struct gl_buffer_object *
1003 _mesa_lookup_bufferobj_locked(struct gl_context *ctx, GLuint buffer)
1004 {
1005 return (struct gl_buffer_object *)
1006 _mesa_HashLookupLocked(ctx->Shared->BufferObjects, buffer);
1007 }
1008
1009 /**
1010 * A convenience function for direct state access functions that throws
1011 * GL_INVALID_OPERATION if buffer is not the name of a buffer object in the
1012 * hash table.
1013 */
1014 struct gl_buffer_object *
1015 _mesa_lookup_bufferobj_err(struct gl_context *ctx, GLuint buffer,
1016 const char *caller)
1017 {
1018 struct gl_buffer_object *bufObj;
1019
1020 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
1021 if (!bufObj)
1022 _mesa_error(ctx, GL_INVALID_OPERATION,
1023 "%s(non-generated buffer name %u)", caller, buffer);
1024
1025 return bufObj;
1026 }
1027
1028
1029 void
1030 _mesa_begin_bufferobj_lookups(struct gl_context *ctx)
1031 {
1032 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
1033 }
1034
1035
1036 void
1037 _mesa_end_bufferobj_lookups(struct gl_context *ctx)
1038 {
1039 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
1040 }
1041
1042
1043 /**
1044 * Look up a buffer object for a multi-bind function.
1045 *
1046 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1047 * of generating an error if the buffer ID is not zero or the name
1048 * of an existing buffer object.
1049 *
1050 * If the buffer ID refers to an existing buffer object, a pointer
1051 * to the buffer object is returned. If the ID is zero, a pointer
1052 * to the shared NullBufferObj is returned. If the ID is not zero
1053 * and does not refer to a valid buffer object, this function
1054 * returns NULL.
1055 *
1056 * This function assumes that the caller has already locked the
1057 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1058 */
1059 struct gl_buffer_object *
1060 _mesa_multi_bind_lookup_bufferobj(struct gl_context *ctx,
1061 const GLuint *buffers,
1062 GLuint index, const char *caller)
1063 {
1064 struct gl_buffer_object *bufObj;
1065
1066 if (buffers[index] != 0) {
1067 bufObj = _mesa_lookup_bufferobj_locked(ctx, buffers[index]);
1068
1069 /* The multi-bind functions don't create the buffer objects
1070 when they don't exist. */
1071 if (bufObj == &DummyBufferObject)
1072 bufObj = NULL;
1073 } else
1074 bufObj = ctx->Shared->NullBufferObj;
1075
1076 if (!bufObj) {
1077 /* The ARB_multi_bind spec says:
1078 *
1079 * "An INVALID_OPERATION error is generated if any value
1080 * in <buffers> is not zero or the name of an existing
1081 * buffer object (per binding)."
1082 */
1083 _mesa_error(ctx, GL_INVALID_OPERATION,
1084 "%s(buffers[%u]=%u is not zero or the name "
1085 "of an existing buffer object)",
1086 caller, index, buffers[index]);
1087 }
1088
1089 return bufObj;
1090 }
1091
1092
1093 /**
1094 * If *ptr points to obj, set ptr = the Null/default buffer object.
1095 * This is a helper for buffer object deletion.
1096 * The GL spec says that deleting a buffer object causes it to get
1097 * unbound from all arrays in the current context.
1098 */
1099 static void
1100 unbind(struct gl_context *ctx,
1101 struct gl_buffer_object **ptr,
1102 struct gl_buffer_object *obj)
1103 {
1104 if (*ptr == obj) {
1105 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
1106 }
1107 }
1108
1109
1110 /**
1111 * Plug default/fallback buffer object functions into the device
1112 * driver hooks.
1113 */
1114 void
1115 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
1116 {
1117 /* GL_ARB_vertex/pixel_buffer_object */
1118 driver->NewBufferObject = _mesa_new_buffer_object;
1119 driver->DeleteBuffer = _mesa_delete_buffer_object;
1120 driver->BufferData = _mesa_buffer_data;
1121 driver->BufferSubData = _mesa_buffer_subdata;
1122 driver->GetBufferSubData = _mesa_buffer_get_subdata;
1123 driver->UnmapBuffer = _mesa_buffer_unmap;
1124
1125 /* GL_ARB_clear_buffer_object */
1126 driver->ClearBufferSubData = _mesa_buffer_clear_subdata;
1127
1128 /* GL_ARB_map_buffer_range */
1129 driver->MapBufferRange = _mesa_buffer_map_range;
1130 driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
1131
1132 /* GL_ARB_copy_buffer */
1133 driver->CopyBufferSubData = _mesa_copy_buffer_subdata;
1134 }
1135
1136
1137 void
1138 _mesa_buffer_unmap_all_mappings(struct gl_context *ctx,
1139 struct gl_buffer_object *bufObj)
1140 {
1141 int i;
1142
1143 for (i = 0; i < MAP_COUNT; i++) {
1144 if (_mesa_bufferobj_mapped(bufObj, i)) {
1145 ctx->Driver.UnmapBuffer(ctx, bufObj, i);
1146 assert(bufObj->Mappings[i].Pointer == NULL);
1147 bufObj->Mappings[i].AccessFlags = 0;
1148 }
1149 }
1150 }
1151
1152
1153 /**********************************************************************/
1154 /* API Functions */
1155 /**********************************************************************/
1156
1157 void GLAPIENTRY
1158 _mesa_BindBuffer(GLenum target, GLuint buffer)
1159 {
1160 GET_CURRENT_CONTEXT(ctx);
1161
1162 if (MESA_VERBOSE & VERBOSE_API)
1163 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
1164 _mesa_lookup_enum_by_nr(target), buffer);
1165
1166 bind_buffer_object(ctx, target, buffer);
1167 }
1168
1169
1170 /**
1171 * Delete a set of buffer objects.
1172 *
1173 * \param n Number of buffer objects to delete.
1174 * \param ids Array of \c n buffer object IDs.
1175 */
1176 void GLAPIENTRY
1177 _mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
1178 {
1179 GET_CURRENT_CONTEXT(ctx);
1180 GLsizei i;
1181 FLUSH_VERTICES(ctx, 0);
1182
1183 if (n < 0) {
1184 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
1185 return;
1186 }
1187
1188 mtx_lock(&ctx->Shared->Mutex);
1189
1190 for (i = 0; i < n; i++) {
1191 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
1192 if (bufObj) {
1193 struct gl_vertex_array_object *vao = ctx->Array.VAO;
1194 GLuint j;
1195
1196 assert(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
1197
1198 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1199
1200 /* unbind any vertex pointers bound to this buffer */
1201 for (j = 0; j < ARRAY_SIZE(vao->VertexBinding); j++) {
1202 unbind(ctx, &vao->VertexBinding[j].BufferObj, bufObj);
1203 }
1204
1205 if (ctx->Array.ArrayBufferObj == bufObj) {
1206 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB, 0 );
1207 }
1208 if (vao->IndexBufferObj == bufObj) {
1209 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
1210 }
1211
1212 /* unbind ARB_draw_indirect binding point */
1213 if (ctx->DrawIndirectBuffer == bufObj) {
1214 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER, 0 );
1215 }
1216
1217 /* unbind ARB_copy_buffer binding points */
1218 if (ctx->CopyReadBuffer == bufObj) {
1219 _mesa_BindBuffer( GL_COPY_READ_BUFFER, 0 );
1220 }
1221 if (ctx->CopyWriteBuffer == bufObj) {
1222 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER, 0 );
1223 }
1224
1225 /* unbind transform feedback binding points */
1226 if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
1227 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER, 0 );
1228 }
1229 for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
1230 if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
1231 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 );
1232 }
1233 }
1234
1235 /* unbind UBO binding points */
1236 for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
1237 if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
1238 _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 );
1239 }
1240 }
1241
1242 if (ctx->UniformBuffer == bufObj) {
1243 _mesa_BindBuffer( GL_UNIFORM_BUFFER, 0 );
1244 }
1245
1246 /* unbind Atomci Buffer binding points */
1247 for (j = 0; j < ctx->Const.MaxAtomicBufferBindings; j++) {
1248 if (ctx->AtomicBufferBindings[j].BufferObject == bufObj) {
1249 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER, j, 0 );
1250 }
1251 }
1252
1253 if (ctx->AtomicBuffer == bufObj) {
1254 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER, 0 );
1255 }
1256
1257 /* unbind any pixel pack/unpack pointers bound to this buffer */
1258 if (ctx->Pack.BufferObj == bufObj) {
1259 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT, 0 );
1260 }
1261 if (ctx->Unpack.BufferObj == bufObj) {
1262 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
1263 }
1264
1265 if (ctx->Texture.BufferObject == bufObj) {
1266 _mesa_BindBuffer( GL_TEXTURE_BUFFER, 0 );
1267 }
1268
1269 if (ctx->ExternalVirtualMemoryBuffer == bufObj) {
1270 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, 0);
1271 }
1272
1273 /* The ID is immediately freed for re-use */
1274 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
1275 /* Make sure we do not run into the classic ABA problem on bind.
1276 * We don't want to allow re-binding a buffer object that's been
1277 * "deleted" by glDeleteBuffers().
1278 *
1279 * The explicit rebinding to the default object in the current context
1280 * prevents the above in the current context, but another context
1281 * sharing the same objects might suffer from this problem.
1282 * The alternative would be to do the hash lookup in any case on bind
1283 * which would introduce more runtime overhead than this.
1284 */
1285 bufObj->DeletePending = GL_TRUE;
1286 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
1287 }
1288 }
1289
1290 mtx_unlock(&ctx->Shared->Mutex);
1291 }
1292
1293
1294 /**
1295 * Generate a set of unique buffer object IDs and store them in \c buffer.
1296 *
1297 * \param n Number of IDs to generate.
1298 * \param buffer Array of \c n locations to store the IDs.
1299 */
1300 void GLAPIENTRY
1301 _mesa_GenBuffers(GLsizei n, GLuint *buffer)
1302 {
1303 GET_CURRENT_CONTEXT(ctx);
1304 GLuint first;
1305 GLint i;
1306
1307 if (MESA_VERBOSE & VERBOSE_API)
1308 _mesa_debug(ctx, "glGenBuffers(%d)\n", n);
1309
1310 if (n < 0) {
1311 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
1312 return;
1313 }
1314
1315 if (!buffer) {
1316 return;
1317 }
1318
1319 /*
1320 * This must be atomic (generation and allocation of buffer object IDs)
1321 */
1322 mtx_lock(&ctx->Shared->Mutex);
1323
1324 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
1325
1326 /* Insert the ID and pointer to dummy buffer object into hash table */
1327 for (i = 0; i < n; i++) {
1328 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i,
1329 &DummyBufferObject);
1330 buffer[i] = first + i;
1331 }
1332
1333 mtx_unlock(&ctx->Shared->Mutex);
1334 }
1335
1336
1337 /**
1338 * Determine if ID is the name of a buffer object.
1339 *
1340 * \param id ID of the potential buffer object.
1341 * \return \c GL_TRUE if \c id is the name of a buffer object,
1342 * \c GL_FALSE otherwise.
1343 */
1344 GLboolean GLAPIENTRY
1345 _mesa_IsBuffer(GLuint id)
1346 {
1347 struct gl_buffer_object *bufObj;
1348 GET_CURRENT_CONTEXT(ctx);
1349 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1350
1351 mtx_lock(&ctx->Shared->Mutex);
1352 bufObj = _mesa_lookup_bufferobj(ctx, id);
1353 mtx_unlock(&ctx->Shared->Mutex);
1354
1355 return bufObj && bufObj != &DummyBufferObject;
1356 }
1357
1358
1359 void GLAPIENTRY
1360 _mesa_BufferStorage(GLenum target, GLsizeiptr size, const GLvoid *data,
1361 GLbitfield flags)
1362 {
1363 GET_CURRENT_CONTEXT(ctx);
1364 struct gl_buffer_object *bufObj;
1365
1366 if (size <= 0) {
1367 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(size <= 0)");
1368 return;
1369 }
1370
1371 if (flags & ~(GL_MAP_READ_BIT |
1372 GL_MAP_WRITE_BIT |
1373 GL_MAP_PERSISTENT_BIT |
1374 GL_MAP_COHERENT_BIT |
1375 GL_DYNAMIC_STORAGE_BIT |
1376 GL_CLIENT_STORAGE_BIT)) {
1377 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(flags)");
1378 return;
1379 }
1380
1381 if (flags & GL_MAP_PERSISTENT_BIT &&
1382 !(flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT))) {
1383 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(flags!=READ/WRITE)");
1384 return;
1385 }
1386
1387 if (flags & GL_MAP_COHERENT_BIT && !(flags & GL_MAP_PERSISTENT_BIT)) {
1388 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(flags!=PERSISTENT)");
1389 return;
1390 }
1391
1392 bufObj = get_buffer(ctx, "glBufferStorage", target, GL_INVALID_OPERATION);
1393 if (!bufObj)
1394 return;
1395
1396 if (bufObj->Immutable) {
1397 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferStorage(immutable)");
1398 return;
1399 }
1400
1401 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1402 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1403
1404 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1405
1406 bufObj->Written = GL_TRUE;
1407 bufObj->Immutable = GL_TRUE;
1408
1409 assert(ctx->Driver.BufferData);
1410 if (!ctx->Driver.BufferData(ctx, target, size, data, GL_DYNAMIC_DRAW,
1411 flags, bufObj)) {
1412 if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
1413 /* Even though the interaction between AMD_pinned_memory and
1414 * glBufferStorage is not described in the spec, Graham Sellers
1415 * said that it should behave the same as glBufferData.
1416 */
1417 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferStorage()");
1418 }
1419 else {
1420 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferStorage()");
1421 }
1422 }
1423 }
1424
1425
1426 void GLAPIENTRY
1427 _mesa_BufferData(GLenum target, GLsizeiptrARB size,
1428 const GLvoid * data, GLenum usage)
1429 {
1430 GET_CURRENT_CONTEXT(ctx);
1431 struct gl_buffer_object *bufObj;
1432 bool valid_usage;
1433
1434 if (MESA_VERBOSE & VERBOSE_API)
1435 _mesa_debug(ctx, "glBufferData(%s, %ld, %p, %s)\n",
1436 _mesa_lookup_enum_by_nr(target),
1437 (long int) size, data,
1438 _mesa_lookup_enum_by_nr(usage));
1439
1440 if (size < 0) {
1441 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
1442 return;
1443 }
1444
1445 switch (usage) {
1446 case GL_STREAM_DRAW_ARB:
1447 valid_usage = (ctx->API != API_OPENGLES);
1448 break;
1449
1450 case GL_STATIC_DRAW_ARB:
1451 case GL_DYNAMIC_DRAW_ARB:
1452 valid_usage = true;
1453 break;
1454
1455 case GL_STREAM_READ_ARB:
1456 case GL_STREAM_COPY_ARB:
1457 case GL_STATIC_READ_ARB:
1458 case GL_STATIC_COPY_ARB:
1459 case GL_DYNAMIC_READ_ARB:
1460 case GL_DYNAMIC_COPY_ARB:
1461 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
1462 break;
1463
1464 default:
1465 valid_usage = false;
1466 break;
1467 }
1468
1469 if (!valid_usage) {
1470 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferData(usage)");
1471 return;
1472 }
1473
1474 bufObj = get_buffer(ctx, "glBufferDataARB", target, GL_INVALID_OPERATION);
1475 if (!bufObj)
1476 return;
1477
1478 if (bufObj->Immutable) {
1479 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferData(immutable)");
1480 return;
1481 }
1482
1483 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1484 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1485
1486 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1487
1488 bufObj->Written = GL_TRUE;
1489
1490 #ifdef VBO_DEBUG
1491 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1492 bufObj->Name, size, data, usage);
1493 #endif
1494
1495 #ifdef BOUNDS_CHECK
1496 size += 100;
1497 #endif
1498
1499 assert(ctx->Driver.BufferData);
1500 if (!ctx->Driver.BufferData(ctx, target, size, data, usage,
1501 GL_MAP_READ_BIT |
1502 GL_MAP_WRITE_BIT |
1503 GL_DYNAMIC_STORAGE_BIT,
1504 bufObj)) {
1505 if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
1506 /* From GL_AMD_pinned_memory:
1507 *
1508 * INVALID_OPERATION is generated by BufferData if <target> is
1509 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1510 * mapped to the GPU address space.
1511 */
1512 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferData()");
1513 }
1514 else {
1515 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferData()");
1516 }
1517 }
1518 }
1519
1520
1521 void GLAPIENTRY
1522 _mesa_BufferSubData(GLenum target, GLintptrARB offset,
1523 GLsizeiptrARB size, const GLvoid * data)
1524 {
1525 GET_CURRENT_CONTEXT(ctx);
1526 struct gl_buffer_object *bufObj;
1527
1528 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1529 false, GL_INVALID_OPERATION,
1530 "glBufferSubDataARB" );
1531 if (!bufObj) {
1532 /* error already recorded */
1533 return;
1534 }
1535
1536 if (bufObj->Immutable &&
1537 !(bufObj->StorageFlags & GL_DYNAMIC_STORAGE_BIT)) {
1538 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferSubData");
1539 return;
1540 }
1541
1542 if (size == 0)
1543 return;
1544
1545 bufObj->Written = GL_TRUE;
1546
1547 assert(ctx->Driver.BufferSubData);
1548 ctx->Driver.BufferSubData( ctx, offset, size, data, bufObj );
1549 }
1550
1551
1552 void GLAPIENTRY
1553 _mesa_GetBufferSubData(GLenum target, GLintptrARB offset,
1554 GLsizeiptrARB size, void * data)
1555 {
1556 GET_CURRENT_CONTEXT(ctx);
1557 struct gl_buffer_object *bufObj;
1558
1559 bufObj = buffer_object_subdata_range_good(ctx, target, offset, size,
1560 false, GL_INVALID_OPERATION,
1561 "glGetBufferSubDataARB");
1562 if (!bufObj) {
1563 /* error already recorded */
1564 return;
1565 }
1566
1567 assert(ctx->Driver.GetBufferSubData);
1568 ctx->Driver.GetBufferSubData( ctx, offset, size, data, bufObj );
1569 }
1570
1571
1572 void GLAPIENTRY
1573 _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
1574 GLenum type, const GLvoid* data)
1575 {
1576 GET_CURRENT_CONTEXT(ctx);
1577 struct gl_buffer_object* bufObj;
1578 mesa_format mesaFormat;
1579 GLubyte clearValue[MAX_PIXEL_BYTES];
1580 GLsizeiptr clearValueSize;
1581
1582 bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE);
1583 if (!bufObj) {
1584 return;
1585 }
1586
1587 if (_mesa_check_disallowed_mapping(bufObj)) {
1588 _mesa_error(ctx, GL_INVALID_OPERATION,
1589 "glClearBufferData(buffer currently mapped)");
1590 return;
1591 }
1592
1593 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1594 format, type,
1595 "glClearBufferData");
1596 if (mesaFormat == MESA_FORMAT_NONE) {
1597 return;
1598 }
1599
1600 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1601 if (bufObj->Size % clearValueSize != 0) {
1602 _mesa_error(ctx, GL_INVALID_VALUE,
1603 "glClearBufferData(size is not a multiple of "
1604 "internalformat size)");
1605 return;
1606 }
1607
1608 if (data == NULL) {
1609 /* clear to zeros, per the spec */
1610 ctx->Driver.ClearBufferSubData(ctx, 0, bufObj->Size,
1611 NULL, clearValueSize, bufObj);
1612 return;
1613 }
1614
1615 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
1616 format, type, data, "glClearBufferData")) {
1617 return;
1618 }
1619
1620 ctx->Driver.ClearBufferSubData(ctx, 0, bufObj->Size,
1621 clearValue, clearValueSize, bufObj);
1622 }
1623
1624
1625 void GLAPIENTRY
1626 _mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
1627 GLintptr offset, GLsizeiptr size,
1628 GLenum format, GLenum type,
1629 const GLvoid* data)
1630 {
1631 GET_CURRENT_CONTEXT(ctx);
1632 struct gl_buffer_object* bufObj;
1633 mesa_format mesaFormat;
1634 GLubyte clearValue[MAX_PIXEL_BYTES];
1635 GLsizeiptr clearValueSize;
1636
1637 bufObj = buffer_object_subdata_range_good(ctx, target, offset, size,
1638 true, GL_INVALID_VALUE,
1639 "glClearBufferSubData");
1640 if (!bufObj) {
1641 return;
1642 }
1643
1644 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1645 format, type,
1646 "glClearBufferSubData");
1647 if (mesaFormat == MESA_FORMAT_NONE) {
1648 return;
1649 }
1650
1651 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1652 if (offset % clearValueSize != 0 || size % clearValueSize != 0) {
1653 _mesa_error(ctx, GL_INVALID_VALUE,
1654 "glClearBufferSubData(offset or size is not a multiple of "
1655 "internalformat size)");
1656 return;
1657 }
1658
1659 if (data == NULL) {
1660 /* clear to zeros, per the spec */
1661 if (size > 0) {
1662 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1663 NULL, clearValueSize, bufObj);
1664 }
1665 return;
1666 }
1667
1668 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
1669 format, type, data,
1670 "glClearBufferSubData")) {
1671 return;
1672 }
1673
1674 if (size > 0) {
1675 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1676 clearValue, clearValueSize, bufObj);
1677 }
1678 }
1679
1680
1681 void * GLAPIENTRY
1682 _mesa_MapBuffer(GLenum target, GLenum access)
1683 {
1684 GET_CURRENT_CONTEXT(ctx);
1685 struct gl_buffer_object * bufObj;
1686 GLbitfield accessFlags;
1687 void *map;
1688 bool valid_access;
1689
1690 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1691
1692 switch (access) {
1693 case GL_READ_ONLY_ARB:
1694 accessFlags = GL_MAP_READ_BIT;
1695 valid_access = _mesa_is_desktop_gl(ctx);
1696 break;
1697 case GL_WRITE_ONLY_ARB:
1698 accessFlags = GL_MAP_WRITE_BIT;
1699 valid_access = true;
1700 break;
1701 case GL_READ_WRITE_ARB:
1702 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1703 valid_access = _mesa_is_desktop_gl(ctx);
1704 break;
1705 default:
1706 valid_access = false;
1707 break;
1708 }
1709
1710 if (!valid_access) {
1711 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
1712 return NULL;
1713 }
1714
1715 bufObj = get_buffer(ctx, "glMapBufferARB", target, GL_INVALID_OPERATION);
1716 if (!bufObj)
1717 return NULL;
1718
1719 if (accessFlags & GL_MAP_READ_BIT &&
1720 !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
1721 _mesa_error(ctx, GL_INVALID_OPERATION,
1722 "glMapBuffer(invalid read flag)");
1723 return NULL;
1724 }
1725
1726 if (accessFlags & GL_MAP_WRITE_BIT &&
1727 !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
1728 _mesa_error(ctx, GL_INVALID_OPERATION,
1729 "glMapBuffer(invalid write flag)");
1730 return NULL;
1731 }
1732
1733 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
1734 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
1735 return NULL;
1736 }
1737
1738 if (!bufObj->Size) {
1739 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1740 "glMapBuffer(buffer size = 0)");
1741 return NULL;
1742 }
1743
1744 assert(ctx->Driver.MapBufferRange);
1745 map = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size, accessFlags, bufObj,
1746 MAP_USER);
1747 if (!map) {
1748 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1749 return NULL;
1750 }
1751 else {
1752 /* The driver callback should have set these fields.
1753 * This is important because other modules (like VBO) might call
1754 * the driver function directly.
1755 */
1756 assert(bufObj->Mappings[MAP_USER].Pointer == map);
1757 assert(bufObj->Mappings[MAP_USER].Length == bufObj->Size);
1758 assert(bufObj->Mappings[MAP_USER].Offset == 0);
1759 bufObj->Mappings[MAP_USER].AccessFlags = accessFlags;
1760 }
1761
1762 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
1763 bufObj->Written = GL_TRUE;
1764
1765 #ifdef VBO_DEBUG
1766 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1767 bufObj->Name, bufObj->Size, access);
1768 if (access == GL_WRITE_ONLY_ARB) {
1769 GLuint i;
1770 GLubyte *b = (GLubyte *) bufObj->Pointer;
1771 for (i = 0; i < bufObj->Size; i++)
1772 b[i] = i & 0xff;
1773 }
1774 #endif
1775
1776 #ifdef BOUNDS_CHECK
1777 {
1778 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1779 GLuint i;
1780 /* buffer is 100 bytes larger than requested, fill with magic value */
1781 for (i = 0; i < 100; i++) {
1782 buf[bufObj->Size - i - 1] = 123;
1783 }
1784 }
1785 #endif
1786
1787 return bufObj->Mappings[MAP_USER].Pointer;
1788 }
1789
1790
1791 GLboolean GLAPIENTRY
1792 _mesa_UnmapBuffer(GLenum target)
1793 {
1794 GET_CURRENT_CONTEXT(ctx);
1795 struct gl_buffer_object *bufObj;
1796 GLboolean status = GL_TRUE;
1797 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1798
1799 bufObj = get_buffer(ctx, "glUnmapBufferARB", target, GL_INVALID_OPERATION);
1800 if (!bufObj)
1801 return GL_FALSE;
1802
1803 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
1804 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1805 return GL_FALSE;
1806 }
1807
1808 #ifdef BOUNDS_CHECK
1809 if (bufObj->Access != GL_READ_ONLY_ARB) {
1810 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1811 GLuint i;
1812 /* check that last 100 bytes are still = magic value */
1813 for (i = 0; i < 100; i++) {
1814 GLuint pos = bufObj->Size - i - 1;
1815 if (buf[pos] != 123) {
1816 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1817 " at position %d (value = %u)\n",
1818 pos, buf[pos]);
1819 }
1820 }
1821 }
1822 #endif
1823
1824 #ifdef VBO_DEBUG
1825 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1826 GLuint i, unchanged = 0;
1827 GLubyte *b = (GLubyte *) bufObj->Pointer;
1828 GLint pos = -1;
1829 /* check which bytes changed */
1830 for (i = 0; i < bufObj->Size - 1; i++) {
1831 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1832 unchanged++;
1833 if (pos == -1)
1834 pos = i;
1835 }
1836 }
1837 if (unchanged) {
1838 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1839 bufObj->Name, unchanged, bufObj->Size, pos);
1840 }
1841 }
1842 #endif
1843
1844 status = ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_USER);
1845 bufObj->Mappings[MAP_USER].AccessFlags = 0;
1846 assert(bufObj->Mappings[MAP_USER].Pointer == NULL);
1847 assert(bufObj->Mappings[MAP_USER].Offset == 0);
1848 assert(bufObj->Mappings[MAP_USER].Length == 0);
1849
1850 return status;
1851 }
1852
1853
1854 void GLAPIENTRY
1855 _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
1856 {
1857 GET_CURRENT_CONTEXT(ctx);
1858 struct gl_buffer_object *bufObj;
1859
1860 bufObj = get_buffer(ctx, "glGetBufferParameterivARB", target,
1861 GL_INVALID_OPERATION);
1862 if (!bufObj)
1863 return;
1864
1865 switch (pname) {
1866 case GL_BUFFER_SIZE_ARB:
1867 *params = (GLint) bufObj->Size;
1868 return;
1869 case GL_BUFFER_USAGE_ARB:
1870 *params = bufObj->Usage;
1871 return;
1872 case GL_BUFFER_ACCESS_ARB:
1873 *params = simplified_access_mode(ctx,
1874 bufObj->Mappings[MAP_USER].AccessFlags);
1875 return;
1876 case GL_BUFFER_MAPPED_ARB:
1877 *params = _mesa_bufferobj_mapped(bufObj, MAP_USER);
1878 return;
1879 case GL_BUFFER_ACCESS_FLAGS:
1880 if (!ctx->Extensions.ARB_map_buffer_range)
1881 goto invalid_pname;
1882 *params = bufObj->Mappings[MAP_USER].AccessFlags;
1883 return;
1884 case GL_BUFFER_MAP_OFFSET:
1885 if (!ctx->Extensions.ARB_map_buffer_range)
1886 goto invalid_pname;
1887 *params = (GLint) bufObj->Mappings[MAP_USER].Offset;
1888 return;
1889 case GL_BUFFER_MAP_LENGTH:
1890 if (!ctx->Extensions.ARB_map_buffer_range)
1891 goto invalid_pname;
1892 *params = (GLint) bufObj->Mappings[MAP_USER].Length;
1893 return;
1894 case GL_BUFFER_IMMUTABLE_STORAGE:
1895 if (!ctx->Extensions.ARB_buffer_storage)
1896 goto invalid_pname;
1897 *params = bufObj->Immutable;
1898 return;
1899 case GL_BUFFER_STORAGE_FLAGS:
1900 if (!ctx->Extensions.ARB_buffer_storage)
1901 goto invalid_pname;
1902 *params = bufObj->StorageFlags;
1903 return;
1904 default:
1905 ; /* fall-through */
1906 }
1907
1908 invalid_pname:
1909 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname=%s)",
1910 _mesa_lookup_enum_by_nr(pname));
1911 }
1912
1913
1914 /**
1915 * New in GL 3.2
1916 * This is pretty much a duplicate of GetBufferParameteriv() but the
1917 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1918 */
1919 void GLAPIENTRY
1920 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
1921 {
1922 GET_CURRENT_CONTEXT(ctx);
1923 struct gl_buffer_object *bufObj;
1924
1925 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target,
1926 GL_INVALID_OPERATION);
1927 if (!bufObj)
1928 return;
1929
1930 switch (pname) {
1931 case GL_BUFFER_SIZE_ARB:
1932 *params = bufObj->Size;
1933 return;
1934 case GL_BUFFER_USAGE_ARB:
1935 *params = bufObj->Usage;
1936 return;
1937 case GL_BUFFER_ACCESS_ARB:
1938 *params = simplified_access_mode(ctx,
1939 bufObj->Mappings[MAP_USER].AccessFlags);
1940 return;
1941 case GL_BUFFER_ACCESS_FLAGS:
1942 if (!ctx->Extensions.ARB_map_buffer_range)
1943 goto invalid_pname;
1944 *params = bufObj->Mappings[MAP_USER].AccessFlags;
1945 return;
1946 case GL_BUFFER_MAPPED_ARB:
1947 *params = _mesa_bufferobj_mapped(bufObj, MAP_USER);
1948 return;
1949 case GL_BUFFER_MAP_OFFSET:
1950 if (!ctx->Extensions.ARB_map_buffer_range)
1951 goto invalid_pname;
1952 *params = bufObj->Mappings[MAP_USER].Offset;
1953 return;
1954 case GL_BUFFER_MAP_LENGTH:
1955 if (!ctx->Extensions.ARB_map_buffer_range)
1956 goto invalid_pname;
1957 *params = bufObj->Mappings[MAP_USER].Length;
1958 return;
1959 case GL_BUFFER_IMMUTABLE_STORAGE:
1960 if (!ctx->Extensions.ARB_buffer_storage)
1961 goto invalid_pname;
1962 *params = bufObj->Immutable;
1963 return;
1964 case GL_BUFFER_STORAGE_FLAGS:
1965 if (!ctx->Extensions.ARB_buffer_storage)
1966 goto invalid_pname;
1967 *params = bufObj->StorageFlags;
1968 return;
1969 default:
1970 ; /* fall-through */
1971 }
1972
1973 invalid_pname:
1974 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
1975 _mesa_lookup_enum_by_nr(pname));
1976 }
1977
1978
1979 void GLAPIENTRY
1980 _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
1981 {
1982 GET_CURRENT_CONTEXT(ctx);
1983 struct gl_buffer_object * bufObj;
1984
1985 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1986 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1987 return;
1988 }
1989
1990 bufObj = get_buffer(ctx, "glGetBufferPointervARB", target,
1991 GL_INVALID_OPERATION);
1992 if (!bufObj)
1993 return;
1994
1995 *params = bufObj->Mappings[MAP_USER].Pointer;
1996 }
1997
1998
1999 void GLAPIENTRY
2000 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
2001 GLintptr readOffset, GLintptr writeOffset,
2002 GLsizeiptr size)
2003 {
2004 GET_CURRENT_CONTEXT(ctx);
2005 struct gl_buffer_object *src, *dst;
2006
2007 src = get_buffer(ctx, "glCopyBufferSubData", readTarget,
2008 GL_INVALID_OPERATION);
2009 if (!src)
2010 return;
2011
2012 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget,
2013 GL_INVALID_OPERATION);
2014 if (!dst)
2015 return;
2016
2017 if (_mesa_check_disallowed_mapping(src)) {
2018 _mesa_error(ctx, GL_INVALID_OPERATION,
2019 "glCopyBufferSubData(readBuffer is mapped)");
2020 return;
2021 }
2022
2023 if (_mesa_check_disallowed_mapping(dst)) {
2024 _mesa_error(ctx, GL_INVALID_OPERATION,
2025 "glCopyBufferSubData(writeBuffer is mapped)");
2026 return;
2027 }
2028
2029 if (readOffset < 0) {
2030 _mesa_error(ctx, GL_INVALID_VALUE,
2031 "glCopyBufferSubData(readOffset = %d)", (int) readOffset);
2032 return;
2033 }
2034
2035 if (writeOffset < 0) {
2036 _mesa_error(ctx, GL_INVALID_VALUE,
2037 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset);
2038 return;
2039 }
2040
2041 if (size < 0) {
2042 _mesa_error(ctx, GL_INVALID_VALUE,
2043 "glCopyBufferSubData(writeOffset = %d)", (int) size);
2044 return;
2045 }
2046
2047 if (readOffset + size > src->Size) {
2048 _mesa_error(ctx, GL_INVALID_VALUE,
2049 "glCopyBufferSubData(readOffset + size = %d)",
2050 (int) (readOffset + size));
2051 return;
2052 }
2053
2054 if (writeOffset + size > dst->Size) {
2055 _mesa_error(ctx, GL_INVALID_VALUE,
2056 "glCopyBufferSubData(writeOffset + size = %d)",
2057 (int) (writeOffset + size));
2058 return;
2059 }
2060
2061 if (src == dst) {
2062 if (readOffset + size <= writeOffset) {
2063 /* OK */
2064 }
2065 else if (writeOffset + size <= readOffset) {
2066 /* OK */
2067 }
2068 else {
2069 /* overlapping src/dst is illegal */
2070 _mesa_error(ctx, GL_INVALID_VALUE,
2071 "glCopyBufferSubData(overlapping src/dst)");
2072 return;
2073 }
2074 }
2075
2076 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
2077 }
2078
2079
2080 /**
2081 * See GL_ARB_map_buffer_range spec
2082 */
2083 void * GLAPIENTRY
2084 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
2085 GLbitfield access)
2086 {
2087 GET_CURRENT_CONTEXT(ctx);
2088 struct gl_buffer_object *bufObj;
2089 void *map;
2090 GLbitfield allowed_access;
2091
2092 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
2093
2094 if (!ctx->Extensions.ARB_map_buffer_range) {
2095 _mesa_error(ctx, GL_INVALID_OPERATION,
2096 "glMapBufferRange(extension not supported)");
2097 return NULL;
2098 }
2099
2100 if (offset < 0) {
2101 _mesa_error(ctx, GL_INVALID_VALUE,
2102 "glMapBufferRange(offset = %ld)", (long)offset);
2103 return NULL;
2104 }
2105
2106 if (length < 0) {
2107 _mesa_error(ctx, GL_INVALID_VALUE,
2108 "glMapBufferRange(length = %ld)", (long)length);
2109 return NULL;
2110 }
2111
2112 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2113 *
2114 * "An INVALID_OPERATION error is generated for any of the following
2115 * conditions:
2116 *
2117 * * <length> is zero."
2118 */
2119 if (_mesa_is_gles(ctx) && length == 0) {
2120 _mesa_error(ctx, GL_INVALID_OPERATION,
2121 "glMapBufferRange(length = 0)");
2122 return NULL;
2123 }
2124
2125 allowed_access = GL_MAP_READ_BIT |
2126 GL_MAP_WRITE_BIT |
2127 GL_MAP_INVALIDATE_RANGE_BIT |
2128 GL_MAP_INVALIDATE_BUFFER_BIT |
2129 GL_MAP_FLUSH_EXPLICIT_BIT |
2130 GL_MAP_UNSYNCHRONIZED_BIT;
2131
2132 if (ctx->Extensions.ARB_buffer_storage) {
2133 allowed_access |= GL_MAP_PERSISTENT_BIT |
2134 GL_MAP_COHERENT_BIT;
2135 }
2136
2137 if (access & ~allowed_access) {
2138 /* generate an error if any other than allowed bit is set */
2139 _mesa_error(ctx, GL_INVALID_VALUE, "glMapBufferRange(access)");
2140 return NULL;
2141 }
2142
2143 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
2144 _mesa_error(ctx, GL_INVALID_OPERATION,
2145 "glMapBufferRange(access indicates neither read or write)");
2146 return NULL;
2147 }
2148
2149 if ((access & GL_MAP_READ_BIT) &&
2150 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
2151 GL_MAP_INVALIDATE_BUFFER_BIT |
2152 GL_MAP_UNSYNCHRONIZED_BIT))) {
2153 _mesa_error(ctx, GL_INVALID_OPERATION,
2154 "glMapBufferRange(invalid access flags)");
2155 return NULL;
2156 }
2157
2158 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
2159 ((access & GL_MAP_WRITE_BIT) == 0)) {
2160 _mesa_error(ctx, GL_INVALID_OPERATION,
2161 "glMapBufferRange(invalid access flags)");
2162 return NULL;
2163 }
2164
2165 bufObj = get_buffer(ctx, "glMapBufferRange", target, GL_INVALID_OPERATION);
2166 if (!bufObj)
2167 return NULL;
2168
2169 if (access & GL_MAP_READ_BIT &&
2170 !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
2171 _mesa_error(ctx, GL_INVALID_OPERATION,
2172 "glMapBufferRange(invalid read flag)");
2173 return NULL;
2174 }
2175
2176 if (access & GL_MAP_WRITE_BIT &&
2177 !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
2178 _mesa_error(ctx, GL_INVALID_OPERATION,
2179 "glMapBufferRange(invalid write flag)");
2180 return NULL;
2181 }
2182
2183 if (access & GL_MAP_COHERENT_BIT &&
2184 !(bufObj->StorageFlags & GL_MAP_COHERENT_BIT)) {
2185 _mesa_error(ctx, GL_INVALID_OPERATION,
2186 "glMapBufferRange(invalid coherent flag)");
2187 return NULL;
2188 }
2189
2190 if (access & GL_MAP_PERSISTENT_BIT &&
2191 !(bufObj->StorageFlags & GL_MAP_PERSISTENT_BIT)) {
2192 _mesa_error(ctx, GL_INVALID_OPERATION,
2193 "glMapBufferRange(invalid persistent flag)");
2194 return NULL;
2195 }
2196
2197 if (offset + length > bufObj->Size) {
2198 _mesa_error(ctx, GL_INVALID_VALUE,
2199 "glMapBufferRange(offset + length > size)");
2200 return NULL;
2201 }
2202
2203 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2204 _mesa_error(ctx, GL_INVALID_OPERATION,
2205 "glMapBufferRange(buffer already mapped)");
2206 return NULL;
2207 }
2208
2209 if (!bufObj->Size) {
2210 _mesa_error(ctx, GL_OUT_OF_MEMORY,
2211 "glMapBufferRange(buffer size = 0)");
2212 return NULL;
2213 }
2214
2215 /* Mapping zero bytes should return a non-null pointer. */
2216 if (!length) {
2217 static long dummy = 0;
2218 bufObj->Mappings[MAP_USER].Pointer = &dummy;
2219 bufObj->Mappings[MAP_USER].Length = length;
2220 bufObj->Mappings[MAP_USER].Offset = offset;
2221 bufObj->Mappings[MAP_USER].AccessFlags = access;
2222 return bufObj->Mappings[MAP_USER].Pointer;
2223 }
2224
2225 assert(ctx->Driver.MapBufferRange);
2226 map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj,
2227 MAP_USER);
2228 if (!map) {
2229 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
2230 }
2231 else {
2232 /* The driver callback should have set all these fields.
2233 * This is important because other modules (like VBO) might call
2234 * the driver function directly.
2235 */
2236 assert(bufObj->Mappings[MAP_USER].Pointer == map);
2237 assert(bufObj->Mappings[MAP_USER].Length == length);
2238 assert(bufObj->Mappings[MAP_USER].Offset == offset);
2239 assert(bufObj->Mappings[MAP_USER].AccessFlags == access);
2240 }
2241
2242 return map;
2243 }
2244
2245
2246 /**
2247 * See GL_ARB_map_buffer_range spec
2248 */
2249 void GLAPIENTRY
2250 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
2251 {
2252 GET_CURRENT_CONTEXT(ctx);
2253 struct gl_buffer_object *bufObj;
2254
2255 if (!ctx->Extensions.ARB_map_buffer_range) {
2256 _mesa_error(ctx, GL_INVALID_OPERATION,
2257 "glFlushMappedBufferRange(extension not supported)");
2258 return;
2259 }
2260
2261 if (offset < 0) {
2262 _mesa_error(ctx, GL_INVALID_VALUE,
2263 "glFlushMappedBufferRange(offset = %ld)", (long)offset);
2264 return;
2265 }
2266
2267 if (length < 0) {
2268 _mesa_error(ctx, GL_INVALID_VALUE,
2269 "glFlushMappedBufferRange(length = %ld)", (long)length);
2270 return;
2271 }
2272
2273 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target,
2274 GL_INVALID_OPERATION);
2275 if (!bufObj)
2276 return;
2277
2278 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2279 /* buffer is not mapped */
2280 _mesa_error(ctx, GL_INVALID_OPERATION,
2281 "glFlushMappedBufferRange(buffer is not mapped)");
2282 return;
2283 }
2284
2285 if ((bufObj->Mappings[MAP_USER].AccessFlags &
2286 GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
2287 _mesa_error(ctx, GL_INVALID_OPERATION,
2288 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
2289 return;
2290 }
2291
2292 if (offset + length > bufObj->Mappings[MAP_USER].Length) {
2293 _mesa_error(ctx, GL_INVALID_VALUE,
2294 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
2295 (long)offset, (long)length,
2296 (long)bufObj->Mappings[MAP_USER].Length);
2297 return;
2298 }
2299
2300 assert(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_WRITE_BIT);
2301
2302 if (ctx->Driver.FlushMappedBufferRange)
2303 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
2304 MAP_USER);
2305 }
2306
2307
2308 static GLenum
2309 buffer_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2310 {
2311 struct gl_buffer_object *bufObj;
2312 GLenum retval;
2313
2314 bufObj = _mesa_lookup_bufferobj(ctx, name);
2315 if (!bufObj) {
2316 _mesa_error(ctx, GL_INVALID_VALUE,
2317 "glObjectPurgeable(name = 0x%x)", name);
2318 return 0;
2319 }
2320 if (!_mesa_is_bufferobj(bufObj)) {
2321 _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
2322 return 0;
2323 }
2324
2325 if (bufObj->Purgeable) {
2326 _mesa_error(ctx, GL_INVALID_OPERATION,
2327 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2328 return GL_VOLATILE_APPLE;
2329 }
2330
2331 bufObj->Purgeable = GL_TRUE;
2332
2333 retval = GL_VOLATILE_APPLE;
2334 if (ctx->Driver.BufferObjectPurgeable)
2335 retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
2336
2337 return retval;
2338 }
2339
2340
2341 static GLenum
2342 renderbuffer_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2343 {
2344 struct gl_renderbuffer *bufObj;
2345 GLenum retval;
2346
2347 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2348 if (!bufObj) {
2349 _mesa_error(ctx, GL_INVALID_VALUE,
2350 "glObjectUnpurgeable(name = 0x%x)", name);
2351 return 0;
2352 }
2353
2354 if (bufObj->Purgeable) {
2355 _mesa_error(ctx, GL_INVALID_OPERATION,
2356 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2357 return GL_VOLATILE_APPLE;
2358 }
2359
2360 bufObj->Purgeable = GL_TRUE;
2361
2362 retval = GL_VOLATILE_APPLE;
2363 if (ctx->Driver.RenderObjectPurgeable)
2364 retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
2365
2366 return retval;
2367 }
2368
2369
2370 static GLenum
2371 texture_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2372 {
2373 struct gl_texture_object *bufObj;
2374 GLenum retval;
2375
2376 bufObj = _mesa_lookup_texture(ctx, name);
2377 if (!bufObj) {
2378 _mesa_error(ctx, GL_INVALID_VALUE,
2379 "glObjectPurgeable(name = 0x%x)", name);
2380 return 0;
2381 }
2382
2383 if (bufObj->Purgeable) {
2384 _mesa_error(ctx, GL_INVALID_OPERATION,
2385 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2386 return GL_VOLATILE_APPLE;
2387 }
2388
2389 bufObj->Purgeable = GL_TRUE;
2390
2391 retval = GL_VOLATILE_APPLE;
2392 if (ctx->Driver.TextureObjectPurgeable)
2393 retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
2394
2395 return retval;
2396 }
2397
2398
2399 GLenum GLAPIENTRY
2400 _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
2401 {
2402 GLenum retval;
2403
2404 GET_CURRENT_CONTEXT(ctx);
2405 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2406
2407 if (name == 0) {
2408 _mesa_error(ctx, GL_INVALID_VALUE,
2409 "glObjectPurgeable(name = 0x%x)", name);
2410 return 0;
2411 }
2412
2413 switch (option) {
2414 case GL_VOLATILE_APPLE:
2415 case GL_RELEASED_APPLE:
2416 /* legal */
2417 break;
2418 default:
2419 _mesa_error(ctx, GL_INVALID_ENUM,
2420 "glObjectPurgeable(name = 0x%x) invalid option: %d",
2421 name, option);
2422 return 0;
2423 }
2424
2425 switch (objectType) {
2426 case GL_TEXTURE:
2427 retval = texture_object_purgeable(ctx, name, option);
2428 break;
2429 case GL_RENDERBUFFER_EXT:
2430 retval = renderbuffer_purgeable(ctx, name, option);
2431 break;
2432 case GL_BUFFER_OBJECT_APPLE:
2433 retval = buffer_object_purgeable(ctx, name, option);
2434 break;
2435 default:
2436 _mesa_error(ctx, GL_INVALID_ENUM,
2437 "glObjectPurgeable(name = 0x%x) invalid type: %d",
2438 name, objectType);
2439 return 0;
2440 }
2441
2442 /* In strict conformance to the spec, we must only return VOLATILE when
2443 * when passed the VOLATILE option. Madness.
2444 *
2445 * XXX First fix the spec, then fix me.
2446 */
2447 return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
2448 }
2449
2450
2451 static GLenum
2452 buffer_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2453 {
2454 struct gl_buffer_object *bufObj;
2455 GLenum retval;
2456
2457 bufObj = _mesa_lookup_bufferobj(ctx, name);
2458 if (!bufObj) {
2459 _mesa_error(ctx, GL_INVALID_VALUE,
2460 "glObjectUnpurgeable(name = 0x%x)", name);
2461 return 0;
2462 }
2463
2464 if (! bufObj->Purgeable) {
2465 _mesa_error(ctx, GL_INVALID_OPERATION,
2466 "glObjectUnpurgeable(name = 0x%x) object is "
2467 " already \"unpurged\"", name);
2468 return 0;
2469 }
2470
2471 bufObj->Purgeable = GL_FALSE;
2472
2473 retval = option;
2474 if (ctx->Driver.BufferObjectUnpurgeable)
2475 retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
2476
2477 return retval;
2478 }
2479
2480
2481 static GLenum
2482 renderbuffer_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2483 {
2484 struct gl_renderbuffer *bufObj;
2485 GLenum retval;
2486
2487 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2488 if (!bufObj) {
2489 _mesa_error(ctx, GL_INVALID_VALUE,
2490 "glObjectUnpurgeable(name = 0x%x)", name);
2491 return 0;
2492 }
2493
2494 if (! bufObj->Purgeable) {
2495 _mesa_error(ctx, GL_INVALID_OPERATION,
2496 "glObjectUnpurgeable(name = 0x%x) object is "
2497 " already \"unpurged\"", name);
2498 return 0;
2499 }
2500
2501 bufObj->Purgeable = GL_FALSE;
2502
2503 retval = option;
2504 if (ctx->Driver.RenderObjectUnpurgeable)
2505 retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
2506
2507 return retval;
2508 }
2509
2510
2511 static GLenum
2512 texture_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2513 {
2514 struct gl_texture_object *bufObj;
2515 GLenum retval;
2516
2517 bufObj = _mesa_lookup_texture(ctx, name);
2518 if (!bufObj) {
2519 _mesa_error(ctx, GL_INVALID_VALUE,
2520 "glObjectUnpurgeable(name = 0x%x)", name);
2521 return 0;
2522 }
2523
2524 if (! bufObj->Purgeable) {
2525 _mesa_error(ctx, GL_INVALID_OPERATION,
2526 "glObjectUnpurgeable(name = 0x%x) object is"
2527 " already \"unpurged\"", name);
2528 return 0;
2529 }
2530
2531 bufObj->Purgeable = GL_FALSE;
2532
2533 retval = option;
2534 if (ctx->Driver.TextureObjectUnpurgeable)
2535 retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
2536
2537 return retval;
2538 }
2539
2540
2541 GLenum GLAPIENTRY
2542 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
2543 {
2544 GET_CURRENT_CONTEXT(ctx);
2545 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2546
2547 if (name == 0) {
2548 _mesa_error(ctx, GL_INVALID_VALUE,
2549 "glObjectUnpurgeable(name = 0x%x)", name);
2550 return 0;
2551 }
2552
2553 switch (option) {
2554 case GL_RETAINED_APPLE:
2555 case GL_UNDEFINED_APPLE:
2556 /* legal */
2557 break;
2558 default:
2559 _mesa_error(ctx, GL_INVALID_ENUM,
2560 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2561 name, option);
2562 return 0;
2563 }
2564
2565 switch (objectType) {
2566 case GL_BUFFER_OBJECT_APPLE:
2567 return buffer_object_unpurgeable(ctx, name, option);
2568 case GL_TEXTURE:
2569 return texture_object_unpurgeable(ctx, name, option);
2570 case GL_RENDERBUFFER_EXT:
2571 return renderbuffer_unpurgeable(ctx, name, option);
2572 default:
2573 _mesa_error(ctx, GL_INVALID_ENUM,
2574 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2575 name, objectType);
2576 return 0;
2577 }
2578 }
2579
2580
2581 static void
2582 get_buffer_object_parameteriv(struct gl_context *ctx, GLuint name,
2583 GLenum pname, GLint *params)
2584 {
2585 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
2586 if (!bufObj) {
2587 _mesa_error(ctx, GL_INVALID_VALUE,
2588 "glGetObjectParameteriv(name = 0x%x) invalid object", name);
2589 return;
2590 }
2591
2592 switch (pname) {
2593 case GL_PURGEABLE_APPLE:
2594 *params = bufObj->Purgeable;
2595 break;
2596 default:
2597 _mesa_error(ctx, GL_INVALID_ENUM,
2598 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2599 name, pname);
2600 break;
2601 }
2602 }
2603
2604
2605 static void
2606 get_renderbuffer_parameteriv(struct gl_context *ctx, GLuint name,
2607 GLenum pname, GLint *params)
2608 {
2609 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
2610 if (!rb) {
2611 _mesa_error(ctx, GL_INVALID_VALUE,
2612 "glObjectUnpurgeable(name = 0x%x)", name);
2613 return;
2614 }
2615
2616 switch (pname) {
2617 case GL_PURGEABLE_APPLE:
2618 *params = rb->Purgeable;
2619 break;
2620 default:
2621 _mesa_error(ctx, GL_INVALID_ENUM,
2622 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2623 name, pname);
2624 break;
2625 }
2626 }
2627
2628
2629 static void
2630 get_texture_object_parameteriv(struct gl_context *ctx, GLuint name,
2631 GLenum pname, GLint *params)
2632 {
2633 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
2634 if (!texObj) {
2635 _mesa_error(ctx, GL_INVALID_VALUE,
2636 "glObjectUnpurgeable(name = 0x%x)", name);
2637 return;
2638 }
2639
2640 switch (pname) {
2641 case GL_PURGEABLE_APPLE:
2642 *params = texObj->Purgeable;
2643 break;
2644 default:
2645 _mesa_error(ctx, GL_INVALID_ENUM,
2646 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2647 name, pname);
2648 break;
2649 }
2650 }
2651
2652
2653 void GLAPIENTRY
2654 _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
2655 GLint *params)
2656 {
2657 GET_CURRENT_CONTEXT(ctx);
2658
2659 if (name == 0) {
2660 _mesa_error(ctx, GL_INVALID_VALUE,
2661 "glGetObjectParameteriv(name = 0x%x)", name);
2662 return;
2663 }
2664
2665 switch (objectType) {
2666 case GL_TEXTURE:
2667 get_texture_object_parameteriv(ctx, name, pname, params);
2668 break;
2669 case GL_BUFFER_OBJECT_APPLE:
2670 get_buffer_object_parameteriv(ctx, name, pname, params);
2671 break;
2672 case GL_RENDERBUFFER_EXT:
2673 get_renderbuffer_parameteriv(ctx, name, pname, params);
2674 break;
2675 default:
2676 _mesa_error(ctx, GL_INVALID_ENUM,
2677 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2678 name, objectType);
2679 }
2680 }
2681
2682 /**
2683 * Binds a buffer object to a uniform buffer binding point.
2684 *
2685 * The caller is responsible for flushing vertices and updating
2686 * NewDriverState.
2687 */
2688 static void
2689 set_ubo_binding(struct gl_context *ctx,
2690 struct gl_uniform_buffer_binding *binding,
2691 struct gl_buffer_object *bufObj,
2692 GLintptr offset,
2693 GLsizeiptr size,
2694 GLboolean autoSize)
2695 {
2696 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2697
2698 binding->Offset = offset;
2699 binding->Size = size;
2700 binding->AutomaticSize = autoSize;
2701
2702 /* If this is a real buffer object, mark it has having been used
2703 * at some point as a UBO.
2704 */
2705 if (size >= 0)
2706 bufObj->UsageHistory |= USAGE_UNIFORM_BUFFER;
2707 }
2708
2709 /**
2710 * Binds a buffer object to a uniform buffer binding point.
2711 *
2712 * Unlike set_ubo_binding(), this function also flushes vertices
2713 * and updates NewDriverState. It also checks if the binding
2714 * has actually changed before updating it.
2715 */
2716 static void
2717 bind_uniform_buffer(struct gl_context *ctx,
2718 GLuint index,
2719 struct gl_buffer_object *bufObj,
2720 GLintptr offset,
2721 GLsizeiptr size,
2722 GLboolean autoSize)
2723 {
2724 struct gl_uniform_buffer_binding *binding =
2725 &ctx->UniformBufferBindings[index];
2726
2727 if (binding->BufferObject == bufObj &&
2728 binding->Offset == offset &&
2729 binding->Size == size &&
2730 binding->AutomaticSize == autoSize) {
2731 return;
2732 }
2733
2734 FLUSH_VERTICES(ctx, 0);
2735 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
2736
2737 set_ubo_binding(ctx, binding, bufObj, offset, size, autoSize);
2738 }
2739
2740 /**
2741 * Bind a region of a buffer object to a uniform block binding point.
2742 * \param index the uniform buffer binding point index
2743 * \param bufObj the buffer object
2744 * \param offset offset to the start of buffer object region
2745 * \param size size of the buffer object region
2746 */
2747 static void
2748 bind_buffer_range_uniform_buffer(struct gl_context *ctx,
2749 GLuint index,
2750 struct gl_buffer_object *bufObj,
2751 GLintptr offset,
2752 GLsizeiptr size)
2753 {
2754 if (index >= ctx->Const.MaxUniformBufferBindings) {
2755 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
2756 return;
2757 }
2758
2759 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
2760 _mesa_error(ctx, GL_INVALID_VALUE,
2761 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
2762 ctx->Const.UniformBufferOffsetAlignment);
2763 return;
2764 }
2765
2766 if (bufObj == ctx->Shared->NullBufferObj) {
2767 offset = -1;
2768 size = -1;
2769 }
2770
2771 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2772 bind_uniform_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
2773 }
2774
2775
2776 /**
2777 * Bind a buffer object to a uniform block binding point.
2778 * As above, but offset = 0.
2779 */
2780 static void
2781 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
2782 GLuint index,
2783 struct gl_buffer_object *bufObj)
2784 {
2785 if (index >= ctx->Const.MaxUniformBufferBindings) {
2786 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
2787 return;
2788 }
2789
2790 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2791
2792 if (bufObj == ctx->Shared->NullBufferObj)
2793 bind_uniform_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
2794 else
2795 bind_uniform_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
2796 }
2797
2798 /**
2799 * Binds a buffer object to an atomic buffer binding point.
2800 *
2801 * The caller is responsible for validating the offset,
2802 * flushing the vertices and updating NewDriverState.
2803 */
2804 static void
2805 set_atomic_buffer_binding(struct gl_context *ctx,
2806 struct gl_atomic_buffer_binding *binding,
2807 struct gl_buffer_object *bufObj,
2808 GLintptr offset,
2809 GLsizeiptr size)
2810 {
2811 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2812
2813 if (bufObj == ctx->Shared->NullBufferObj) {
2814 binding->Offset = -1;
2815 binding->Size = -1;
2816 } else {
2817 binding->Offset = offset;
2818 binding->Size = size;
2819 bufObj->UsageHistory |= USAGE_ATOMIC_COUNTER_BUFFER;
2820 }
2821 }
2822
2823 /**
2824 * Binds a buffer object to an atomic buffer binding point.
2825 *
2826 * Unlike set_atomic_buffer_binding(), this function also validates the
2827 * index and offset, flushes vertices, and updates NewDriverState.
2828 * It also checks if the binding has actually changing before
2829 * updating it.
2830 */
2831 static void
2832 bind_atomic_buffer(struct gl_context *ctx,
2833 unsigned index,
2834 struct gl_buffer_object *bufObj,
2835 GLintptr offset,
2836 GLsizeiptr size,
2837 const char *name)
2838 {
2839 struct gl_atomic_buffer_binding *binding;
2840
2841 if (index >= ctx->Const.MaxAtomicBufferBindings) {
2842 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index);
2843 return;
2844 }
2845
2846 if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
2847 _mesa_error(ctx, GL_INVALID_VALUE,
2848 "%s(offset misaligned %d/%d)", name, (int) offset,
2849 ATOMIC_COUNTER_SIZE);
2850 return;
2851 }
2852
2853 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
2854
2855 binding = &ctx->AtomicBufferBindings[index];
2856 if (binding->BufferObject == bufObj &&
2857 binding->Offset == offset &&
2858 binding->Size == size) {
2859 return;
2860 }
2861
2862 FLUSH_VERTICES(ctx, 0);
2863 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
2864
2865 set_atomic_buffer_binding(ctx, binding, bufObj, offset, size);
2866 }
2867
2868 static inline bool
2869 bind_buffers_check_offset_and_size(struct gl_context *ctx,
2870 GLuint index,
2871 const GLintptr *offsets,
2872 const GLsizeiptr *sizes)
2873 {
2874 if (offsets[index] < 0) {
2875 /* The ARB_multi_bind spec says:
2876 *
2877 * "An INVALID_VALUE error is generated by BindBuffersRange if any
2878 * value in <offsets> is less than zero (per binding)."
2879 */
2880 _mesa_error(ctx, GL_INVALID_VALUE,
2881 "glBindBuffersRange(offsets[%u]=%" PRId64 " < 0)",
2882 index, (int64_t) offsets[index]);
2883 return false;
2884 }
2885
2886 if (sizes[index] <= 0) {
2887 /* The ARB_multi_bind spec says:
2888 *
2889 * "An INVALID_VALUE error is generated by BindBuffersRange if any
2890 * value in <sizes> is less than or equal to zero (per binding)."
2891 */
2892 _mesa_error(ctx, GL_INVALID_VALUE,
2893 "glBindBuffersRange(sizes[%u]=%" PRId64 " <= 0)",
2894 index, (int64_t) sizes[index]);
2895 return false;
2896 }
2897
2898 return true;
2899 }
2900
2901 static bool
2902 error_check_bind_uniform_buffers(struct gl_context *ctx,
2903 GLuint first, GLsizei count,
2904 const char *caller)
2905 {
2906 if (!ctx->Extensions.ARB_uniform_buffer_object) {
2907 _mesa_error(ctx, GL_INVALID_ENUM,
2908 "%s(target=GL_UNIFORM_BUFFER)", caller);
2909 return false;
2910 }
2911
2912 /* The ARB_multi_bind_spec says:
2913 *
2914 * "An INVALID_OPERATION error is generated if <first> + <count> is
2915 * greater than the number of target-specific indexed binding points,
2916 * as described in section 6.7.1."
2917 */
2918 if (first + count > ctx->Const.MaxUniformBufferBindings) {
2919 _mesa_error(ctx, GL_INVALID_OPERATION,
2920 "%s(first=%u + count=%d > the value of "
2921 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
2922 caller, first, count,
2923 ctx->Const.MaxUniformBufferBindings);
2924 return false;
2925 }
2926
2927 return true;
2928 }
2929
2930 /**
2931 * Unbind all uniform buffers in the range
2932 * <first> through <first>+<count>-1
2933 */
2934 static void
2935 unbind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
2936 {
2937 struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
2938 GLint i;
2939
2940 for (i = 0; i < count; i++)
2941 set_ubo_binding(ctx, &ctx->UniformBufferBindings[first + i],
2942 bufObj, -1, -1, GL_TRUE);
2943 }
2944
2945 static void
2946 bind_uniform_buffers_base(struct gl_context *ctx, GLuint first, GLsizei count,
2947 const GLuint *buffers)
2948 {
2949 GLint i;
2950
2951 if (!error_check_bind_uniform_buffers(ctx, first, count, "glBindBuffersBase"))
2952 return;
2953
2954 /* Assume that at least one binding will be changed */
2955 FLUSH_VERTICES(ctx, 0);
2956 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
2957
2958 if (!buffers) {
2959 /* The ARB_multi_bind spec says:
2960 *
2961 * "If <buffers> is NULL, all bindings from <first> through
2962 * <first>+<count>-1 are reset to their unbound (zero) state."
2963 */
2964 unbind_uniform_buffers(ctx, first, count);
2965 return;
2966 }
2967
2968 /* Note that the error semantics for multi-bind commands differ from
2969 * those of other GL commands.
2970 *
2971 * The Issues section in the ARB_multi_bind spec says:
2972 *
2973 * "(11) Typically, OpenGL specifies that if an error is generated by a
2974 * command, that command has no effect. This is somewhat
2975 * unfortunate for multi-bind commands, because it would require a
2976 * first pass to scan the entire list of bound objects for errors
2977 * and then a second pass to actually perform the bindings.
2978 * Should we have different error semantics?
2979 *
2980 * RESOLVED: Yes. In this specification, when the parameters for
2981 * one of the <count> binding points are invalid, that binding point
2982 * is not updated and an error will be generated. However, other
2983 * binding points in the same command will be updated if their
2984 * parameters are valid and no other error occurs."
2985 */
2986
2987 _mesa_begin_bufferobj_lookups(ctx);
2988
2989 for (i = 0; i < count; i++) {
2990 struct gl_uniform_buffer_binding *binding =
2991 &ctx->UniformBufferBindings[first + i];
2992 struct gl_buffer_object *bufObj;
2993
2994 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
2995 bufObj = binding->BufferObject;
2996 else
2997 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
2998 "glBindBuffersBase");
2999
3000 if (bufObj) {
3001 if (bufObj == ctx->Shared->NullBufferObj)
3002 set_ubo_binding(ctx, binding, bufObj, -1, -1, GL_TRUE);
3003 else
3004 set_ubo_binding(ctx, binding, bufObj, 0, 0, GL_TRUE);
3005 }
3006 }
3007
3008 _mesa_end_bufferobj_lookups(ctx);
3009 }
3010
3011 static void
3012 bind_uniform_buffers_range(struct gl_context *ctx, GLuint first, GLsizei count,
3013 const GLuint *buffers,
3014 const GLintptr *offsets, const GLsizeiptr *sizes)
3015 {
3016 GLint i;
3017
3018 if (!error_check_bind_uniform_buffers(ctx, first, count,
3019 "glBindBuffersRange"))
3020 return;
3021
3022 /* Assume that at least one binding will be changed */
3023 FLUSH_VERTICES(ctx, 0);
3024 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
3025
3026 if (!buffers) {
3027 /* The ARB_multi_bind spec says:
3028 *
3029 * "If <buffers> is NULL, all bindings from <first> through
3030 * <first>+<count>-1 are reset to their unbound (zero) state.
3031 * In this case, the offsets and sizes associated with the
3032 * binding points are set to default values, ignoring
3033 * <offsets> and <sizes>."
3034 */
3035 unbind_uniform_buffers(ctx, first, count);
3036 return;
3037 }
3038
3039 /* Note that the error semantics for multi-bind commands differ from
3040 * those of other GL commands.
3041 *
3042 * The Issues section in the ARB_multi_bind spec says:
3043 *
3044 * "(11) Typically, OpenGL specifies that if an error is generated by a
3045 * command, that command has no effect. This is somewhat
3046 * unfortunate for multi-bind commands, because it would require a
3047 * first pass to scan the entire list of bound objects for errors
3048 * and then a second pass to actually perform the bindings.
3049 * Should we have different error semantics?
3050 *
3051 * RESOLVED: Yes. In this specification, when the parameters for
3052 * one of the <count> binding points are invalid, that binding point
3053 * is not updated and an error will be generated. However, other
3054 * binding points in the same command will be updated if their
3055 * parameters are valid and no other error occurs."
3056 */
3057
3058 _mesa_begin_bufferobj_lookups(ctx);
3059
3060 for (i = 0; i < count; i++) {
3061 struct gl_uniform_buffer_binding *binding =
3062 &ctx->UniformBufferBindings[first + i];
3063 struct gl_buffer_object *bufObj;
3064
3065 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3066 continue;
3067
3068 /* The ARB_multi_bind spec says:
3069 *
3070 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3071 * pair of values in <offsets> and <sizes> does not respectively
3072 * satisfy the constraints described for those parameters for the
3073 * specified target, as described in section 6.7.1 (per binding)."
3074 *
3075 * Section 6.7.1 refers to table 6.5, which says:
3076 *
3077 * "┌───────────────────────────────────────────────────────────────┐
3078 * │ Uniform buffer array bindings (see sec. 7.6) │
3079 * ├─────────────────────┬─────────────────────────────────────────┤
3080 * │ ... │ ... │
3081 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3082 * │ │ OFFSET_ALIGNMENT │
3083 * │ ... │ ... │
3084 * │ size restriction │ none │
3085 * └─────────────────────┴─────────────────────────────────────────┘"
3086 */
3087 if (offsets[i] & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
3088 _mesa_error(ctx, GL_INVALID_VALUE,
3089 "glBindBuffersRange(offsets[%u]=%" PRId64
3090 " is misaligned; it must be a multiple of the value of "
3091 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3092 "target=GL_UNIFORM_BUFFER)",
3093 i, (int64_t) offsets[i],
3094 ctx->Const.UniformBufferOffsetAlignment);
3095 continue;
3096 }
3097
3098 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3099 bufObj = binding->BufferObject;
3100 else
3101 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3102 "glBindBuffersRange");
3103
3104 if (bufObj) {
3105 if (bufObj == ctx->Shared->NullBufferObj)
3106 set_ubo_binding(ctx, binding, bufObj, -1, -1, GL_FALSE);
3107 else
3108 set_ubo_binding(ctx, binding, bufObj,
3109 offsets[i], sizes[i], GL_FALSE);
3110 }
3111 }
3112
3113 _mesa_end_bufferobj_lookups(ctx);
3114 }
3115
3116 static bool
3117 error_check_bind_xfb_buffers(struct gl_context *ctx,
3118 struct gl_transform_feedback_object *tfObj,
3119 GLuint first, GLsizei count, const char *caller)
3120 {
3121 if (!ctx->Extensions.EXT_transform_feedback) {
3122 _mesa_error(ctx, GL_INVALID_ENUM,
3123 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller);
3124 return false;
3125 }
3126
3127 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3128 *
3129 * "An INVALID_OPERATION error is generated :
3130 *
3131 * ...
3132 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3133 * FEEDBACK_BUFFER and transform feedback is currently active."
3134 *
3135 * We assume that this is also meant to apply to BindBuffersRange
3136 * and BindBuffersBase.
3137 */
3138 if (tfObj->Active) {
3139 _mesa_error(ctx, GL_INVALID_OPERATION,
3140 "%s(Changing transform feedback buffers while "
3141 "transform feedback is active)", caller);
3142 return false;
3143 }
3144
3145 /* The ARB_multi_bind_spec says:
3146 *
3147 * "An INVALID_OPERATION error is generated if <first> + <count> is
3148 * greater than the number of target-specific indexed binding points,
3149 * as described in section 6.7.1."
3150 */
3151 if (first + count > ctx->Const.MaxTransformFeedbackBuffers) {
3152 _mesa_error(ctx, GL_INVALID_OPERATION,
3153 "%s(first=%u + count=%d > the value of "
3154 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3155 caller, first, count,
3156 ctx->Const.MaxTransformFeedbackBuffers);
3157 return false;
3158 }
3159
3160 return true;
3161 }
3162
3163 /**
3164 * Unbind all transform feedback buffers in the range
3165 * <first> through <first>+<count>-1
3166 */
3167 static void
3168 unbind_xfb_buffers(struct gl_context *ctx,
3169 struct gl_transform_feedback_object *tfObj,
3170 GLuint first, GLsizei count)
3171 {
3172 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
3173 GLint i;
3174
3175 for (i = 0; i < count; i++)
3176 _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
3177 bufObj, 0, 0);
3178 }
3179
3180 static void
3181 bind_xfb_buffers_base(struct gl_context *ctx,
3182 GLuint first, GLsizei count,
3183 const GLuint *buffers)
3184 {
3185 struct gl_transform_feedback_object *tfObj =
3186 ctx->TransformFeedback.CurrentObject;
3187 GLint i;
3188
3189 if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count,
3190 "glBindBuffersBase"))
3191 return;
3192
3193 /* Assume that at least one binding will be changed */
3194 FLUSH_VERTICES(ctx, 0);
3195 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
3196
3197 if (!buffers) {
3198 /* The ARB_multi_bind spec says:
3199 *
3200 * "If <buffers> is NULL, all bindings from <first> through
3201 * <first>+<count>-1 are reset to their unbound (zero) state."
3202 */
3203 unbind_xfb_buffers(ctx, tfObj, first, count);
3204 return;
3205 }
3206
3207 /* Note that the error semantics for multi-bind commands differ from
3208 * those of other GL commands.
3209 *
3210 * The Issues section in the ARB_multi_bind spec says:
3211 *
3212 * "(11) Typically, OpenGL specifies that if an error is generated by a
3213 * command, that command has no effect. This is somewhat
3214 * unfortunate for multi-bind commands, because it would require a
3215 * first pass to scan the entire list of bound objects for errors
3216 * and then a second pass to actually perform the bindings.
3217 * Should we have different error semantics?
3218 *
3219 * RESOLVED: Yes. In this specification, when the parameters for
3220 * one of the <count> binding points are invalid, that binding point
3221 * is not updated and an error will be generated. However, other
3222 * binding points in the same command will be updated if their
3223 * parameters are valid and no other error occurs."
3224 */
3225
3226 _mesa_begin_bufferobj_lookups(ctx);
3227
3228 for (i = 0; i < count; i++) {
3229 struct gl_buffer_object * const boundBufObj = tfObj->Buffers[first + i];
3230 struct gl_buffer_object *bufObj;
3231
3232 if (boundBufObj && boundBufObj->Name == buffers[i])
3233 bufObj = boundBufObj;
3234 else
3235 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3236 "glBindBuffersBase");
3237
3238 if (bufObj)
3239 _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
3240 bufObj, 0, 0);
3241 }
3242
3243 _mesa_end_bufferobj_lookups(ctx);
3244 }
3245
3246 static void
3247 bind_xfb_buffers_range(struct gl_context *ctx,
3248 GLuint first, GLsizei count,
3249 const GLuint *buffers,
3250 const GLintptr *offsets,
3251 const GLsizeiptr *sizes)
3252 {
3253 struct gl_transform_feedback_object *tfObj =
3254 ctx->TransformFeedback.CurrentObject;
3255 GLint i;
3256
3257 if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count,
3258 "glBindBuffersRange"))
3259 return;
3260
3261 /* Assume that at least one binding will be changed */
3262 FLUSH_VERTICES(ctx, 0);
3263 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
3264
3265 if (!buffers) {
3266 /* The ARB_multi_bind spec says:
3267 *
3268 * "If <buffers> is NULL, all bindings from <first> through
3269 * <first>+<count>-1 are reset to their unbound (zero) state.
3270 * In this case, the offsets and sizes associated with the
3271 * binding points are set to default values, ignoring
3272 * <offsets> and <sizes>."
3273 */
3274 unbind_xfb_buffers(ctx, tfObj, first, count);
3275 return;
3276 }
3277
3278 /* Note that the error semantics for multi-bind commands differ from
3279 * those of other GL commands.
3280 *
3281 * The Issues section in the ARB_multi_bind spec says:
3282 *
3283 * "(11) Typically, OpenGL specifies that if an error is generated by a
3284 * command, that command has no effect. This is somewhat
3285 * unfortunate for multi-bind commands, because it would require a
3286 * first pass to scan the entire list of bound objects for errors
3287 * and then a second pass to actually perform the bindings.
3288 * Should we have different error semantics?
3289 *
3290 * RESOLVED: Yes. In this specification, when the parameters for
3291 * one of the <count> binding points are invalid, that binding point
3292 * is not updated and an error will be generated. However, other
3293 * binding points in the same command will be updated if their
3294 * parameters are valid and no other error occurs."
3295 */
3296
3297 _mesa_begin_bufferobj_lookups(ctx);
3298
3299 for (i = 0; i < count; i++) {
3300 const GLuint index = first + i;
3301 struct gl_buffer_object * const boundBufObj = tfObj->Buffers[index];
3302 struct gl_buffer_object *bufObj;
3303
3304 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3305 continue;
3306
3307 /* The ARB_multi_bind spec says:
3308 *
3309 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3310 * pair of values in <offsets> and <sizes> does not respectively
3311 * satisfy the constraints described for those parameters for the
3312 * specified target, as described in section 6.7.1 (per binding)."
3313 *
3314 * Section 6.7.1 refers to table 6.5, which says:
3315 *
3316 * "┌───────────────────────────────────────────────────────────────┐
3317 * │ Transform feedback array bindings (see sec. 13.2.2) │
3318 * ├───────────────────────┬───────────────────────────────────────┤
3319 * │ ... │ ... │
3320 * │ offset restriction │ multiple of 4 │
3321 * │ ... │ ... │
3322 * │ size restriction │ multiple of 4 │
3323 * └───────────────────────┴───────────────────────────────────────┘"
3324 */
3325 if (offsets[i] & 0x3) {
3326 _mesa_error(ctx, GL_INVALID_VALUE,
3327 "glBindBuffersRange(offsets[%u]=%" PRId64
3328 " is misaligned; it must be a multiple of 4 when "
3329 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3330 i, (int64_t) offsets[i]);
3331 continue;
3332 }
3333
3334 if (sizes[i] & 0x3) {
3335 _mesa_error(ctx, GL_INVALID_VALUE,
3336 "glBindBuffersRange(sizes[%u]=%" PRId64
3337 " is misaligned; it must be a multiple of 4 when "
3338 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3339 i, (int64_t) sizes[i]);
3340 continue;
3341 }
3342
3343 if (boundBufObj && boundBufObj->Name == buffers[i])
3344 bufObj = boundBufObj;
3345 else
3346 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3347 "glBindBuffersRange");
3348
3349 if (bufObj)
3350 _mesa_set_transform_feedback_binding(ctx, tfObj, index, bufObj,
3351 offsets[i], sizes[i]);
3352 }
3353
3354 _mesa_end_bufferobj_lookups(ctx);
3355 }
3356
3357 static bool
3358 error_check_bind_atomic_buffers(struct gl_context *ctx,
3359 GLuint first, GLsizei count,
3360 const char *caller)
3361 {
3362 if (!ctx->Extensions.ARB_shader_atomic_counters) {
3363 _mesa_error(ctx, GL_INVALID_ENUM,
3364 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller);
3365 return false;
3366 }
3367
3368 /* The ARB_multi_bind_spec says:
3369 *
3370 * "An INVALID_OPERATION error is generated if <first> + <count> is
3371 * greater than the number of target-specific indexed binding points,
3372 * as described in section 6.7.1."
3373 */
3374 if (first + count > ctx->Const.MaxAtomicBufferBindings) {
3375 _mesa_error(ctx, GL_INVALID_OPERATION,
3376 "%s(first=%u + count=%d > the value of "
3377 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3378 caller, first, count, ctx->Const.MaxAtomicBufferBindings);
3379 return false;
3380 }
3381
3382 return true;
3383 }
3384
3385 /**
3386 * Unbind all atomic counter buffers in the range
3387 * <first> through <first>+<count>-1
3388 */
3389 static void
3390 unbind_atomic_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
3391 {
3392 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
3393 GLint i;
3394
3395 for (i = 0; i < count; i++)
3396 set_atomic_buffer_binding(ctx, &ctx->AtomicBufferBindings[first + i],
3397 bufObj, -1, -1);
3398 }
3399
3400 static void
3401 bind_atomic_buffers_base(struct gl_context *ctx,
3402 GLuint first,
3403 GLsizei count,
3404 const GLuint *buffers)
3405 {
3406 GLint i;
3407
3408 if (!error_check_bind_atomic_buffers(ctx, first, count,
3409 "glBindBuffersBase"))
3410 return;
3411
3412 /* Assume that at least one binding will be changed */
3413 FLUSH_VERTICES(ctx, 0);
3414 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
3415
3416 if (!buffers) {
3417 /* The ARB_multi_bind spec says:
3418 *
3419 * "If <buffers> is NULL, all bindings from <first> through
3420 * <first>+<count>-1 are reset to their unbound (zero) state."
3421 */
3422 unbind_atomic_buffers(ctx, first, count);
3423 return;
3424 }
3425
3426 /* Note that the error semantics for multi-bind commands differ from
3427 * those of other GL commands.
3428 *
3429 * The Issues section in the ARB_multi_bind spec says:
3430 *
3431 * "(11) Typically, OpenGL specifies that if an error is generated by a
3432 * command, that command has no effect. This is somewhat
3433 * unfortunate for multi-bind commands, because it would require a
3434 * first pass to scan the entire list of bound objects for errors
3435 * and then a second pass to actually perform the bindings.
3436 * Should we have different error semantics?
3437 *
3438 * RESOLVED: Yes. In this specification, when the parameters for
3439 * one of the <count> binding points are invalid, that binding point
3440 * is not updated and an error will be generated. However, other
3441 * binding points in the same command will be updated if their
3442 * parameters are valid and no other error occurs."
3443 */
3444
3445 _mesa_begin_bufferobj_lookups(ctx);
3446
3447 for (i = 0; i < count; i++) {
3448 struct gl_atomic_buffer_binding *binding =
3449 &ctx->AtomicBufferBindings[first + i];
3450 struct gl_buffer_object *bufObj;
3451
3452 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3453 bufObj = binding->BufferObject;
3454 else
3455 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3456 "glBindBuffersBase");
3457
3458 if (bufObj)
3459 set_atomic_buffer_binding(ctx, binding, bufObj, 0, 0);
3460 }
3461
3462 _mesa_end_bufferobj_lookups(ctx);
3463 }
3464
3465 static void
3466 bind_atomic_buffers_range(struct gl_context *ctx,
3467 GLuint first,
3468 GLsizei count,
3469 const GLuint *buffers,
3470 const GLintptr *offsets,
3471 const GLsizeiptr *sizes)
3472 {
3473 GLint i;
3474
3475 if (!error_check_bind_atomic_buffers(ctx, first, count,
3476 "glBindBuffersRange"))
3477 return;
3478
3479 /* Assume that at least one binding will be changed */
3480 FLUSH_VERTICES(ctx, 0);
3481 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
3482
3483 if (!buffers) {
3484 /* The ARB_multi_bind spec says:
3485 *
3486 * "If <buffers> is NULL, all bindings from <first> through
3487 * <first>+<count>-1 are reset to their unbound (zero) state.
3488 * In this case, the offsets and sizes associated with the
3489 * binding points are set to default values, ignoring
3490 * <offsets> and <sizes>."
3491 */
3492 unbind_atomic_buffers(ctx, first, count);
3493 return;
3494 }
3495
3496 /* Note that the error semantics for multi-bind commands differ from
3497 * those of other GL commands.
3498 *
3499 * The Issues section in the ARB_multi_bind spec says:
3500 *
3501 * "(11) Typically, OpenGL specifies that if an error is generated by a
3502 * command, that command has no effect. This is somewhat
3503 * unfortunate for multi-bind commands, because it would require a
3504 * first pass to scan the entire list of bound objects for errors
3505 * and then a second pass to actually perform the bindings.
3506 * Should we have different error semantics?
3507 *
3508 * RESOLVED: Yes. In this specification, when the parameters for
3509 * one of the <count> binding points are invalid, that binding point
3510 * is not updated and an error will be generated. However, other
3511 * binding points in the same command will be updated if their
3512 * parameters are valid and no other error occurs."
3513 */
3514
3515 _mesa_begin_bufferobj_lookups(ctx);
3516
3517 for (i = 0; i < count; i++) {
3518 struct gl_atomic_buffer_binding *binding =
3519 &ctx->AtomicBufferBindings[first + i];
3520 struct gl_buffer_object *bufObj;
3521
3522 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3523 continue;
3524
3525 /* The ARB_multi_bind spec says:
3526 *
3527 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3528 * pair of values in <offsets> and <sizes> does not respectively
3529 * satisfy the constraints described for those parameters for the
3530 * specified target, as described in section 6.7.1 (per binding)."
3531 *
3532 * Section 6.7.1 refers to table 6.5, which says:
3533 *
3534 * "┌───────────────────────────────────────────────────────────────┐
3535 * │ Atomic counter array bindings (see sec. 7.7.2) │
3536 * ├───────────────────────┬───────────────────────────────────────┤
3537 * │ ... │ ... │
3538 * │ offset restriction │ multiple of 4 │
3539 * │ ... │ ... │
3540 * │ size restriction │ none │
3541 * └───────────────────────┴───────────────────────────────────────┘"
3542 */
3543 if (offsets[i] & (ATOMIC_COUNTER_SIZE - 1)) {
3544 _mesa_error(ctx, GL_INVALID_VALUE,
3545 "glBindBuffersRange(offsets[%u]=%" PRId64
3546 " is misaligned; it must be a multiple of %d when "
3547 "target=GL_ATOMIC_COUNTER_BUFFER)",
3548 i, (int64_t) offsets[i], ATOMIC_COUNTER_SIZE);
3549 continue;
3550 }
3551
3552 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3553 bufObj = binding->BufferObject;
3554 else
3555 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3556 "glBindBuffersRange");
3557
3558 if (bufObj)
3559 set_atomic_buffer_binding(ctx, binding, bufObj, offsets[i], sizes[i]);
3560 }
3561
3562 _mesa_end_bufferobj_lookups(ctx);
3563 }
3564
3565 void GLAPIENTRY
3566 _mesa_BindBufferRange(GLenum target, GLuint index,
3567 GLuint buffer, GLintptr offset, GLsizeiptr size)
3568 {
3569 GET_CURRENT_CONTEXT(ctx);
3570 struct gl_buffer_object *bufObj;
3571
3572 if (buffer == 0) {
3573 bufObj = ctx->Shared->NullBufferObj;
3574 } else {
3575 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3576 }
3577 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
3578 &bufObj, "glBindBufferRange"))
3579 return;
3580
3581 if (!bufObj) {
3582 _mesa_error(ctx, GL_INVALID_OPERATION,
3583 "glBindBufferRange(invalid buffer=%u)", buffer);
3584 return;
3585 }
3586
3587 if (buffer != 0) {
3588 if (size <= 0) {
3589 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
3590 (int) size);
3591 return;
3592 }
3593 }
3594
3595 switch (target) {
3596 case GL_TRANSFORM_FEEDBACK_BUFFER:
3597 _mesa_bind_buffer_range_transform_feedback(ctx, index, bufObj,
3598 offset, size);
3599 return;
3600 case GL_UNIFORM_BUFFER:
3601 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
3602 return;
3603 case GL_ATOMIC_COUNTER_BUFFER:
3604 bind_atomic_buffer(ctx, index, bufObj, offset, size,
3605 "glBindBufferRange");
3606 return;
3607 default:
3608 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
3609 return;
3610 }
3611 }
3612
3613 void GLAPIENTRY
3614 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
3615 {
3616 GET_CURRENT_CONTEXT(ctx);
3617 struct gl_buffer_object *bufObj;
3618
3619 if (buffer == 0) {
3620 bufObj = ctx->Shared->NullBufferObj;
3621 } else {
3622 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3623 }
3624 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
3625 &bufObj, "glBindBufferBase"))
3626 return;
3627
3628 if (!bufObj) {
3629 _mesa_error(ctx, GL_INVALID_OPERATION,
3630 "glBindBufferBase(invalid buffer=%u)", buffer);
3631 return;
3632 }
3633
3634 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3635 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3636 *
3637 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3638 * zero and size equal to the size of buffer."
3639 *
3640 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3641 *
3642 * "If the parameter (starting offset or size) was not specified when the
3643 * buffer object was bound, zero is returned."
3644 *
3645 * What happens if the size of the buffer changes? Does the size of the
3646 * buffer at the moment glBindBufferBase was called still play a role, like
3647 * the first quote would imply, or is the size meaningless in the
3648 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3649 * core spec page 45 says:
3650 *
3651 * "It is equivalent to calling BindBufferRange with offset zero, while
3652 * size is determined by the size of the bound buffer at the time the
3653 * binding is used."
3654 *
3655 * My interpretation is that the GL 4.1 spec was a clarification of the
3656 * behavior, not a change. In particular, this choice will only make
3657 * rendering work in cases where it would have had undefined results.
3658 */
3659
3660 switch (target) {
3661 case GL_TRANSFORM_FEEDBACK_BUFFER:
3662 _mesa_bind_buffer_base_transform_feedback(ctx, index, bufObj);
3663 return;
3664 case GL_UNIFORM_BUFFER:
3665 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
3666 return;
3667 case GL_ATOMIC_COUNTER_BUFFER:
3668 bind_atomic_buffer(ctx, index, bufObj, 0, 0,
3669 "glBindBufferBase");
3670 return;
3671 default:
3672 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
3673 return;
3674 }
3675 }
3676
3677 void GLAPIENTRY
3678 _mesa_BindBuffersRange(GLenum target, GLuint first, GLsizei count,
3679 const GLuint *buffers,
3680 const GLintptr *offsets, const GLsizeiptr *sizes)
3681 {
3682 GET_CURRENT_CONTEXT(ctx);
3683
3684 switch (target) {
3685 case GL_TRANSFORM_FEEDBACK_BUFFER:
3686 bind_xfb_buffers_range(ctx, first, count, buffers, offsets, sizes);
3687 return;
3688 case GL_UNIFORM_BUFFER:
3689 bind_uniform_buffers_range(ctx, first, count, buffers, offsets, sizes);
3690 return;
3691 case GL_ATOMIC_COUNTER_BUFFER:
3692 bind_atomic_buffers_range(ctx, first, count, buffers,
3693 offsets, sizes);
3694 return;
3695 default:
3696 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersRange(target=%s)",
3697 _mesa_lookup_enum_by_nr(target));
3698 break;
3699 }
3700 }
3701
3702 void GLAPIENTRY
3703 _mesa_BindBuffersBase(GLenum target, GLuint first, GLsizei count,
3704 const GLuint *buffers)
3705 {
3706 GET_CURRENT_CONTEXT(ctx);
3707
3708 switch (target) {
3709 case GL_TRANSFORM_FEEDBACK_BUFFER:
3710 bind_xfb_buffers_base(ctx, first, count, buffers);
3711 return;
3712 case GL_UNIFORM_BUFFER:
3713 bind_uniform_buffers_base(ctx, first, count, buffers);
3714 return;
3715 case GL_ATOMIC_COUNTER_BUFFER:
3716 bind_atomic_buffers_base(ctx, first, count, buffers);
3717 return;
3718 default:
3719 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersBase(target=%s)",
3720 _mesa_lookup_enum_by_nr(target));
3721 break;
3722 }
3723 }
3724
3725 void GLAPIENTRY
3726 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
3727 GLsizeiptr length)
3728 {
3729 GET_CURRENT_CONTEXT(ctx);
3730 struct gl_buffer_object *bufObj;
3731 const GLintptr end = offset + length;
3732
3733 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3734 if (!bufObj) {
3735 _mesa_error(ctx, GL_INVALID_VALUE,
3736 "glInvalidateBufferSubData(name = 0x%x) invalid object",
3737 buffer);
3738 return;
3739 }
3740
3741 /* The GL_ARB_invalidate_subdata spec says:
3742 *
3743 * "An INVALID_VALUE error is generated if <offset> or <length> is
3744 * negative, or if <offset> + <length> is greater than the value of
3745 * BUFFER_SIZE."
3746 */
3747 if (end < 0 || end > bufObj->Size) {
3748 _mesa_error(ctx, GL_INVALID_VALUE,
3749 "glInvalidateBufferSubData(invalid offset or length)");
3750 return;
3751 }
3752
3753 /* The OpenGL 4.4 (Core Profile) spec says:
3754 *
3755 * "An INVALID_OPERATION error is generated if buffer is currently
3756 * mapped by MapBuffer or if the invalidate range intersects the range
3757 * currently mapped by MapBufferRange, unless it was mapped
3758 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
3759 */
3760 if (!(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT) &&
3761 bufferobj_range_mapped(bufObj, offset, length)) {
3762 _mesa_error(ctx, GL_INVALID_OPERATION,
3763 "glInvalidateBufferSubData(intersection with mapped "
3764 "range)");
3765 return;
3766 }
3767
3768 /* We don't actually do anything for this yet. Just return after
3769 * validating the parameters and generating the required errors.
3770 */
3771 return;
3772 }
3773
3774 void GLAPIENTRY
3775 _mesa_InvalidateBufferData(GLuint buffer)
3776 {
3777 GET_CURRENT_CONTEXT(ctx);
3778 struct gl_buffer_object *bufObj;
3779
3780 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3781 if (!bufObj) {
3782 _mesa_error(ctx, GL_INVALID_VALUE,
3783 "glInvalidateBufferData(name = 0x%x) invalid object",
3784 buffer);
3785 return;
3786 }
3787
3788 /* The OpenGL 4.4 (Core Profile) spec says:
3789 *
3790 * "An INVALID_OPERATION error is generated if buffer is currently
3791 * mapped by MapBuffer or if the invalidate range intersects the range
3792 * currently mapped by MapBufferRange, unless it was mapped
3793 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
3794 */
3795 if (_mesa_check_disallowed_mapping(bufObj)) {
3796 _mesa_error(ctx, GL_INVALID_OPERATION,
3797 "glInvalidateBufferData(intersection with mapped "
3798 "range)");
3799 return;
3800 }
3801
3802 /* We don't actually do anything for this yet. Just return after
3803 * validating the parameters and generating the required errors.
3804 */
3805 return;
3806 }