2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
29 * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions.
30 * \author Brian Paul, Ian Romanick
34 #include <inttypes.h> /* for PRId64 macro */
40 #include "bufferobj.h"
43 #include "glformats.h"
45 #include "transformfeedback.h"
50 /*#define BOUNDS_CHECK*/
54 * We count the number of buffer modification calls to check for
55 * inefficient buffer use. This is the number of such calls before we
58 #define BUFFER_WARNING_CALL_COUNT 4
62 * Helper to warn of possible performance issues, such as frequently
63 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
67 buffer_usage_warning(struct gl_context *ctx, GLuint *id, const char *fmt, ...)
72 _mesa_gl_vdebug(ctx, id,
73 MESA_DEBUG_SOURCE_API,
74 MESA_DEBUG_TYPE_PERFORMANCE,
75 MESA_DEBUG_SEVERITY_MEDIUM,
80 #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
82 static GLuint id = 0; \
83 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
88 * Used as a placeholder for buffer objects between glGenBuffers() and
89 * glBindBuffer() so that glIsBuffer() can work correctly.
91 static struct gl_buffer_object DummyBufferObject;
95 * Return pointer to address of a buffer object target.
96 * \param ctx the GL context
97 * \param target the buffer object target to be retrieved.
98 * \return pointer to pointer to the buffer object bound to \c target in the
99 * specified context or \c NULL if \c target is invalid.
101 static inline struct gl_buffer_object **
102 get_buffer_target(struct gl_context *ctx, GLenum target)
104 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
106 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx)
107 && target != GL_ARRAY_BUFFER && target != GL_ELEMENT_ARRAY_BUFFER)
111 case GL_ARRAY_BUFFER_ARB:
112 return &ctx->Array.ArrayBufferObj;
113 case GL_ELEMENT_ARRAY_BUFFER_ARB:
114 return &ctx->Array.VAO->IndexBufferObj;
115 case GL_PIXEL_PACK_BUFFER_EXT:
116 return &ctx->Pack.BufferObj;
117 case GL_PIXEL_UNPACK_BUFFER_EXT:
118 return &ctx->Unpack.BufferObj;
119 case GL_COPY_READ_BUFFER:
120 return &ctx->CopyReadBuffer;
121 case GL_COPY_WRITE_BUFFER:
122 return &ctx->CopyWriteBuffer;
123 case GL_DRAW_INDIRECT_BUFFER:
124 if ((ctx->API == API_OPENGL_CORE &&
125 ctx->Extensions.ARB_draw_indirect) ||
126 _mesa_is_gles31(ctx)) {
127 return &ctx->DrawIndirectBuffer;
130 case GL_DISPATCH_INDIRECT_BUFFER:
131 if (_mesa_has_compute_shaders(ctx)) {
132 return &ctx->DispatchIndirectBuffer;
135 case GL_TRANSFORM_FEEDBACK_BUFFER:
136 if (ctx->Extensions.EXT_transform_feedback) {
137 return &ctx->TransformFeedback.CurrentBuffer;
140 case GL_TEXTURE_BUFFER:
141 if (ctx->API == API_OPENGL_CORE &&
142 ctx->Extensions.ARB_texture_buffer_object) {
143 return &ctx->Texture.BufferObject;
146 case GL_UNIFORM_BUFFER:
147 if (ctx->Extensions.ARB_uniform_buffer_object) {
148 return &ctx->UniformBuffer;
151 case GL_SHADER_STORAGE_BUFFER:
152 if (ctx->Extensions.ARB_shader_storage_buffer_object) {
153 return &ctx->ShaderStorageBuffer;
156 case GL_ATOMIC_COUNTER_BUFFER:
157 if (ctx->Extensions.ARB_shader_atomic_counters) {
158 return &ctx->AtomicBuffer;
161 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD:
162 if (ctx->Extensions.AMD_pinned_memory) {
163 return &ctx->ExternalVirtualMemoryBuffer;
174 * Get the buffer object bound to the specified target in a GL context.
175 * \param ctx the GL context
176 * \param target the buffer object target to be retrieved.
177 * \param error the GL error to record if target is illegal.
178 * \return pointer to the buffer object bound to \c target in the
179 * specified context or \c NULL if \c target is invalid.
181 static inline struct gl_buffer_object *
182 get_buffer(struct gl_context *ctx, const char *func, GLenum target,
185 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
188 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
192 if (!_mesa_is_bufferobj(*bufObj)) {
193 _mesa_error(ctx, error, "%s(no buffer bound)", func);
202 * Convert a GLbitfield describing the mapped buffer access flags
203 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
206 simplified_access_mode(struct gl_context *ctx, GLbitfield access)
208 const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
209 if ((access & rwFlags) == rwFlags)
210 return GL_READ_WRITE;
211 if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
213 if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
214 return GL_WRITE_ONLY;
216 /* Otherwise, AccessFlags is zero (the default state).
218 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
220 * Name Type Initial Value Legal Values
222 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
225 * However, table 6.8 in the GL_OES_mapbuffer extension says:
227 * Get Value Type Get Command Value Description
228 * --------- ---- ----------- ----- -----------
229 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
231 * The difference is because GL_OES_mapbuffer only supports mapping buffers
236 return _mesa_is_gles(ctx) ? GL_WRITE_ONLY : GL_READ_WRITE;
241 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
243 * The regions do not overlap if and only if the end of the given
244 * region is before the mapped region or the start of the given region
245 * is after the mapped region.
247 * \param obj Buffer object target on which to operate.
248 * \param offset Offset of the first byte of the subdata range.
249 * \param size Size, in bytes, of the subdata range.
250 * \return true if ranges overlap, false otherwise
254 bufferobj_range_mapped(const struct gl_buffer_object *obj,
255 GLintptr offset, GLsizeiptr size)
257 if (_mesa_bufferobj_mapped(obj, MAP_USER)) {
258 const GLintptr end = offset + size;
259 const GLintptr mapEnd = obj->Mappings[MAP_USER].Offset +
260 obj->Mappings[MAP_USER].Length;
262 if (!(end <= obj->Mappings[MAP_USER].Offset || offset >= mapEnd)) {
271 * Tests the subdata range parameters and sets the GL error code for
272 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
273 * \c glClearBufferSubData.
275 * \param ctx GL context.
276 * \param bufObj The buffer object.
277 * \param offset Offset of the first byte of the subdata range.
278 * \param size Size, in bytes, of the subdata range.
279 * \param mappedRange If true, checks if an overlapping range is mapped.
280 * If false, checks if buffer is mapped.
281 * \param caller Name of calling function for recording errors.
282 * \return false if error, true otherwise
284 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
287 buffer_object_subdata_range_good(struct gl_context *ctx,
288 const struct gl_buffer_object *bufObj,
289 GLintptr offset, GLsizeiptr size,
290 bool mappedRange, const char *caller)
293 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
298 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
302 if (offset + size > bufObj->Size) {
303 _mesa_error(ctx, GL_INVALID_VALUE,
304 "%s(offset %lu + size %lu > buffer size %lu)", caller,
305 (unsigned long) offset,
306 (unsigned long) size,
307 (unsigned long) bufObj->Size);
311 if (bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT)
315 if (bufferobj_range_mapped(bufObj, offset, size)) {
316 _mesa_error(ctx, GL_INVALID_OPERATION,
317 "%s(range is mapped without persistent bit)",
323 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
324 _mesa_error(ctx, GL_INVALID_OPERATION,
325 "%s(buffer is mapped without persistent bit)",
336 * Test the format and type parameters and set the GL error code for
337 * \c glClearBufferData and \c glClearBufferSubData.
339 * \param ctx GL context.
340 * \param internalformat Format to which the data is to be converted.
341 * \param format Format of the supplied data.
342 * \param type Type of the supplied data.
343 * \param caller Name of calling function for recording errors.
344 * \return If internalformat, format and type are legal the mesa_format
345 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
347 * \sa glClearBufferData and glClearBufferSubData
350 validate_clear_buffer_format(struct gl_context *ctx,
351 GLenum internalformat,
352 GLenum format, GLenum type,
355 mesa_format mesaFormat;
356 GLenum errorFormatType;
358 mesaFormat = _mesa_validate_texbuffer_format(ctx, internalformat);
359 if (mesaFormat == MESA_FORMAT_NONE) {
360 _mesa_error(ctx, GL_INVALID_ENUM,
361 "%s(invalid internalformat)", caller);
362 return MESA_FORMAT_NONE;
365 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
366 * EXT_texture_integer there is no conversion between integer and
367 * non-integer formats
369 if (_mesa_is_enum_format_signed_int(format) !=
370 _mesa_is_format_integer_color(mesaFormat)) {
371 _mesa_error(ctx, GL_INVALID_OPERATION,
372 "%s(integer vs non-integer)", caller);
373 return MESA_FORMAT_NONE;
376 if (!_mesa_is_color_format(format)) {
377 _mesa_error(ctx, GL_INVALID_ENUM,
378 "%s(format is not a color format)", caller);
379 return MESA_FORMAT_NONE;
382 errorFormatType = _mesa_error_check_format_and_type(ctx, format, type);
383 if (errorFormatType != GL_NO_ERROR) {
384 _mesa_error(ctx, GL_INVALID_ENUM,
385 "%s(invalid format or type)", caller);
386 return MESA_FORMAT_NONE;
394 * Convert user-specified clear value to the specified internal format.
396 * \param ctx GL context.
397 * \param internalformat Format to which the data is converted.
398 * \param clearValue Points to the converted clear value.
399 * \param format Format of the supplied data.
400 * \param type Type of the supplied data.
401 * \param data Data which is to be converted to internalformat.
402 * \param caller Name of calling function for recording errors.
403 * \return true if data could be converted, false otherwise.
405 * \sa glClearBufferData, glClearBufferSubData
408 convert_clear_buffer_data(struct gl_context *ctx,
409 mesa_format internalformat,
410 GLubyte *clearValue, GLenum format, GLenum type,
411 const GLvoid *data, const char *caller)
413 GLenum internalformatBase = _mesa_get_format_base_format(internalformat);
415 if (_mesa_texstore(ctx, 1, internalformatBase, internalformat,
416 0, &clearValue, 1, 1, 1,
417 format, type, data, &ctx->Unpack)) {
421 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
428 * Allocate and initialize a new buffer object.
430 * Default callback for the \c dd_function_table::NewBufferObject() hook.
432 static struct gl_buffer_object *
433 _mesa_new_buffer_object(struct gl_context *ctx, GLuint name)
435 struct gl_buffer_object *obj;
439 obj = MALLOC_STRUCT(gl_buffer_object);
440 _mesa_initialize_buffer_object(ctx, obj, name);
446 * Delete a buffer object.
448 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
451 _mesa_delete_buffer_object(struct gl_context *ctx,
452 struct gl_buffer_object *bufObj)
456 _mesa_align_free(bufObj->Data);
458 /* assign strange values here to help w/ debugging */
459 bufObj->RefCount = -1000;
462 mtx_destroy(&bufObj->Mutex);
470 * Set ptr to bufObj w/ reference counting.
471 * This is normally only called from the _mesa_reference_buffer_object() macro
472 * when there's a real pointer change.
475 _mesa_reference_buffer_object_(struct gl_context *ctx,
476 struct gl_buffer_object **ptr,
477 struct gl_buffer_object *bufObj)
480 /* Unreference the old buffer */
481 GLboolean deleteFlag = GL_FALSE;
482 struct gl_buffer_object *oldObj = *ptr;
484 mtx_lock(&oldObj->Mutex);
485 assert(oldObj->RefCount > 0);
487 deleteFlag = (oldObj->RefCount == 0);
488 mtx_unlock(&oldObj->Mutex);
491 assert(ctx->Driver.DeleteBuffer);
492 ctx->Driver.DeleteBuffer(ctx, oldObj);
500 /* reference new buffer */
501 mtx_lock(&bufObj->Mutex);
502 if (bufObj->RefCount == 0) {
503 /* this buffer's being deleted (look just above) */
504 /* Not sure this can every really happen. Warn if it does. */
505 _mesa_problem(NULL, "referencing deleted buffer object");
512 mtx_unlock(&bufObj->Mutex);
518 * Initialize a buffer object to default values.
521 _mesa_initialize_buffer_object(struct gl_context *ctx,
522 struct gl_buffer_object *obj,
525 memset(obj, 0, sizeof(struct gl_buffer_object));
526 mtx_init(&obj->Mutex, mtx_plain);
529 obj->Usage = GL_STATIC_DRAW_ARB;
535 * Callback called from _mesa_HashWalk()
538 count_buffer_size(GLuint key, void *data, void *userData)
540 const struct gl_buffer_object *bufObj =
541 (const struct gl_buffer_object *) data;
542 GLuint *total = (GLuint *) userData;
545 *total = *total + bufObj->Size;
550 * Compute total size (in bytes) of all buffer objects for the given context.
551 * For debugging purposes.
554 _mesa_total_buffer_object_memory(struct gl_context *ctx)
558 _mesa_HashWalk(ctx->Shared->BufferObjects, count_buffer_size, &total);
565 * Allocate space for and store data in a buffer object. Any data that was
566 * previously stored in the buffer object is lost. If \c data is \c NULL,
567 * memory will be allocated, but no copy will occur.
569 * This is the default callback for \c dd_function_table::BufferData()
570 * Note that all GL error checking will have been done already.
572 * \param ctx GL context.
573 * \param target Buffer object target on which to operate.
574 * \param size Size, in bytes, of the new data store.
575 * \param data Pointer to the data to store in the buffer object. This
576 * pointer may be \c NULL.
577 * \param usage Hints about how the data will be used.
578 * \param bufObj Object to be used.
580 * \return GL_TRUE for success, GL_FALSE for failure
581 * \sa glBufferDataARB, dd_function_table::BufferData.
584 buffer_data_fallback(struct gl_context *ctx, GLenum target, GLsizeiptr size,
585 const GLvoid *data, GLenum usage, GLenum storageFlags,
586 struct gl_buffer_object *bufObj)
592 _mesa_align_free( bufObj->Data );
594 new_data = _mesa_align_malloc( size, ctx->Const.MinMapBufferAlignment );
596 bufObj->Data = (GLubyte *) new_data;
598 bufObj->Usage = usage;
599 bufObj->StorageFlags = storageFlags;
602 memcpy( bufObj->Data, data, size );
614 * Replace data in a subrange of buffer object. If the data range
615 * specified by \c size + \c offset extends beyond the end of the buffer or
616 * if \c data is \c NULL, no copy is performed.
618 * This is the default callback for \c dd_function_table::BufferSubData()
619 * Note that all GL error checking will have been done already.
621 * \param ctx GL context.
622 * \param offset Offset of the first byte to be modified.
623 * \param size Size, in bytes, of the data range.
624 * \param data Pointer to the data to store in the buffer object.
625 * \param bufObj Object to be used.
627 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
630 buffer_sub_data_fallback(struct gl_context *ctx, GLintptr offset,
631 GLsizeiptr size, const GLvoid *data,
632 struct gl_buffer_object *bufObj)
636 /* this should have been caught in _mesa_BufferSubData() */
637 assert(size + offset <= bufObj->Size);
640 memcpy( (GLubyte *) bufObj->Data + offset, data, size );
646 * Retrieve data from a subrange of buffer object. If the data range
647 * specified by \c size + \c offset extends beyond the end of the buffer or
648 * if \c data is \c NULL, no copy is performed.
650 * This is the default callback for \c dd_function_table::GetBufferSubData()
651 * Note that all GL error checking will have been done already.
653 * \param ctx GL context.
654 * \param target Buffer object target on which to operate.
655 * \param offset Offset of the first byte to be fetched.
656 * \param size Size, in bytes, of the data range.
657 * \param data Destination for data
658 * \param bufObj Object to be used.
660 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
663 _mesa_buffer_get_subdata( struct gl_context *ctx, GLintptrARB offset,
664 GLsizeiptrARB size, GLvoid * data,
665 struct gl_buffer_object * bufObj )
669 if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
670 memcpy( data, (GLubyte *) bufObj->Data + offset, size );
676 * Clear a subrange of the buffer object with copies of the supplied data.
677 * If data is NULL the buffer is filled with zeros.
679 * This is the default callback for \c dd_function_table::ClearBufferSubData()
680 * Note that all GL error checking will have been done already.
682 * \param ctx GL context.
683 * \param offset Offset of the first byte to be cleared.
684 * \param size Size, in bytes, of the to be cleared range.
685 * \param clearValue Source of the data.
686 * \param clearValueSize Size, in bytes, of the supplied data.
687 * \param bufObj Object to be cleared.
689 * \sa glClearBufferSubData, glClearBufferData and
690 * dd_function_table::ClearBufferSubData.
693 _mesa_ClearBufferSubData_sw(struct gl_context *ctx,
694 GLintptr offset, GLsizeiptr size,
695 const GLvoid *clearValue,
696 GLsizeiptr clearValueSize,
697 struct gl_buffer_object *bufObj)
702 assert(ctx->Driver.MapBufferRange);
703 dest = ctx->Driver.MapBufferRange(ctx, offset, size,
705 GL_MAP_INVALIDATE_RANGE_BIT,
706 bufObj, MAP_INTERNAL);
709 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearBuffer[Sub]Data");
713 if (clearValue == NULL) {
714 /* Clear with zeros, per the spec */
715 memset(dest, 0, size);
716 ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL);
720 for (i = 0; i < size/clearValueSize; ++i) {
721 memcpy(dest, clearValue, clearValueSize);
722 dest += clearValueSize;
725 ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL);
730 * Default fallback for \c dd_function_table::MapBufferRange().
731 * Called via glMapBufferRange().
734 map_buffer_range_fallback(struct gl_context *ctx, GLintptr offset,
735 GLsizeiptr length, GLbitfield access,
736 struct gl_buffer_object *bufObj,
737 gl_map_buffer_index index)
740 assert(!_mesa_bufferobj_mapped(bufObj, index));
741 /* Just return a direct pointer to the data */
742 bufObj->Mappings[index].Pointer = bufObj->Data + offset;
743 bufObj->Mappings[index].Length = length;
744 bufObj->Mappings[index].Offset = offset;
745 bufObj->Mappings[index].AccessFlags = access;
746 return bufObj->Mappings[index].Pointer;
751 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
752 * Called via glFlushMappedBufferRange().
755 flush_mapped_buffer_range_fallback(struct gl_context *ctx,
756 GLintptr offset, GLsizeiptr length,
757 struct gl_buffer_object *obj,
758 gl_map_buffer_index index)
770 * Default callback for \c dd_function_table::UnmapBuffer().
772 * The input parameters will have been already tested for errors.
774 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
777 unmap_buffer_fallback(struct gl_context *ctx, struct gl_buffer_object *bufObj,
778 gl_map_buffer_index index)
781 /* XXX we might assert here that bufObj->Pointer is non-null */
782 bufObj->Mappings[index].Pointer = NULL;
783 bufObj->Mappings[index].Length = 0;
784 bufObj->Mappings[index].Offset = 0;
785 bufObj->Mappings[index].AccessFlags = 0x0;
791 * Default fallback for \c dd_function_table::CopyBufferSubData().
792 * Called via glCopyBufferSubData().
795 copy_buffer_sub_data_fallback(struct gl_context *ctx,
796 struct gl_buffer_object *src,
797 struct gl_buffer_object *dst,
798 GLintptr readOffset, GLintptr writeOffset,
801 GLubyte *srcPtr, *dstPtr;
804 srcPtr = dstPtr = ctx->Driver.MapBufferRange(ctx, 0, src->Size,
806 GL_MAP_WRITE_BIT, src,
812 srcPtr += readOffset;
813 dstPtr += writeOffset;
815 srcPtr = ctx->Driver.MapBufferRange(ctx, readOffset, size,
816 GL_MAP_READ_BIT, src,
818 dstPtr = ctx->Driver.MapBufferRange(ctx, writeOffset, size,
820 GL_MAP_INVALIDATE_RANGE_BIT), dst,
824 /* Note: the src and dst regions will never overlap. Trying to do so
825 * would generate GL_INVALID_VALUE earlier.
827 if (srcPtr && dstPtr)
828 memcpy(dstPtr, srcPtr, size);
830 ctx->Driver.UnmapBuffer(ctx, src, MAP_INTERNAL);
832 ctx->Driver.UnmapBuffer(ctx, dst, MAP_INTERNAL);
838 * Initialize the state associated with buffer objects
841 _mesa_init_buffer_objects( struct gl_context *ctx )
845 memset(&DummyBufferObject, 0, sizeof(DummyBufferObject));
846 mtx_init(&DummyBufferObject.Mutex, mtx_plain);
847 DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
849 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj,
850 ctx->Shared->NullBufferObj);
852 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer,
853 ctx->Shared->NullBufferObj);
854 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
855 ctx->Shared->NullBufferObj);
857 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer,
858 ctx->Shared->NullBufferObj);
860 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer,
861 ctx->Shared->NullBufferObj);
863 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer,
864 ctx->Shared->NullBufferObj);
866 _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer,
867 ctx->Shared->NullBufferObj);
869 _mesa_reference_buffer_object(ctx, &ctx->DispatchIndirectBuffer,
870 ctx->Shared->NullBufferObj);
872 for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
873 _mesa_reference_buffer_object(ctx,
874 &ctx->UniformBufferBindings[i].BufferObject,
875 ctx->Shared->NullBufferObj);
876 ctx->UniformBufferBindings[i].Offset = -1;
877 ctx->UniformBufferBindings[i].Size = -1;
880 for (i = 0; i < MAX_COMBINED_SHADER_STORAGE_BUFFERS; i++) {
881 _mesa_reference_buffer_object(ctx,
882 &ctx->ShaderStorageBufferBindings[i].BufferObject,
883 ctx->Shared->NullBufferObj);
884 ctx->ShaderStorageBufferBindings[i].Offset = -1;
885 ctx->ShaderStorageBufferBindings[i].Size = -1;
888 for (i = 0; i < MAX_COMBINED_ATOMIC_BUFFERS; i++) {
889 _mesa_reference_buffer_object(ctx,
890 &ctx->AtomicBufferBindings[i].BufferObject,
891 ctx->Shared->NullBufferObj);
892 ctx->AtomicBufferBindings[i].Offset = 0;
893 ctx->AtomicBufferBindings[i].Size = 0;
899 _mesa_free_buffer_objects( struct gl_context *ctx )
903 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
905 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL);
906 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, NULL);
908 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, NULL);
910 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, NULL);
912 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, NULL);
914 _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer, NULL);
916 _mesa_reference_buffer_object(ctx, &ctx->DispatchIndirectBuffer, NULL);
918 for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
919 _mesa_reference_buffer_object(ctx,
920 &ctx->UniformBufferBindings[i].BufferObject,
924 for (i = 0; i < MAX_COMBINED_SHADER_STORAGE_BUFFERS; i++) {
925 _mesa_reference_buffer_object(ctx,
926 &ctx->ShaderStorageBufferBindings[i].BufferObject,
930 for (i = 0; i < MAX_COMBINED_ATOMIC_BUFFERS; i++) {
931 _mesa_reference_buffer_object(ctx,
932 &ctx->AtomicBufferBindings[i].BufferObject,
939 _mesa_handle_bind_buffer_gen(struct gl_context *ctx,
941 struct gl_buffer_object **buf_handle,
944 struct gl_buffer_object *buf = *buf_handle;
946 if (!buf && (ctx->API == API_OPENGL_CORE || _mesa_is_gles31(ctx))) {
947 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", caller);
951 if (!buf || buf == &DummyBufferObject) {
952 /* If this is a new buffer object id, or one which was generated but
953 * never used before, allocate a buffer object now.
955 assert(ctx->Driver.NewBufferObject);
956 buf = ctx->Driver.NewBufferObject(ctx, buffer);
958 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
961 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, buf);
969 * Bind the specified target to buffer for the specified context.
970 * Called by glBindBuffer() and other functions.
973 bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
975 struct gl_buffer_object *oldBufObj;
976 struct gl_buffer_object *newBufObj = NULL;
977 struct gl_buffer_object **bindTarget = NULL;
979 bindTarget = get_buffer_target(ctx, target);
981 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
985 /* Get pointer to old buffer object (to be unbound) */
986 oldBufObj = *bindTarget;
987 if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
988 return; /* rebinding the same buffer object- no change */
991 * Get pointer to new buffer object (newBufObj)
994 /* The spec says there's not a buffer object named 0, but we use
995 * one internally because it simplifies things.
997 newBufObj = ctx->Shared->NullBufferObj;
1000 /* non-default buffer object */
1001 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
1002 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
1003 &newBufObj, "glBindBuffer"))
1007 /* bind new buffer */
1008 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
1013 * Update the default buffer objects in the given context to reference those
1014 * specified in the shared state and release those referencing the old
1018 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
1020 /* Bind the NullBufferObj to remove references to those
1021 * in the shared context hash table.
1023 bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
1024 bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
1025 bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
1026 bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
1032 * Return the gl_buffer_object for the given ID.
1033 * Always return NULL for ID 0.
1035 struct gl_buffer_object *
1036 _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
1041 return (struct gl_buffer_object *)
1042 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
1046 struct gl_buffer_object *
1047 _mesa_lookup_bufferobj_locked(struct gl_context *ctx, GLuint buffer)
1049 return (struct gl_buffer_object *)
1050 _mesa_HashLookupLocked(ctx->Shared->BufferObjects, buffer);
1054 * A convenience function for direct state access functions that throws
1055 * GL_INVALID_OPERATION if buffer is not the name of an existing
1058 struct gl_buffer_object *
1059 _mesa_lookup_bufferobj_err(struct gl_context *ctx, GLuint buffer,
1062 struct gl_buffer_object *bufObj;
1064 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
1065 if (!bufObj || bufObj == &DummyBufferObject) {
1066 _mesa_error(ctx, GL_INVALID_OPERATION,
1067 "%s(non-existent buffer object %u)", caller, buffer);
1076 _mesa_begin_bufferobj_lookups(struct gl_context *ctx)
1078 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
1083 _mesa_end_bufferobj_lookups(struct gl_context *ctx)
1085 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
1090 * Look up a buffer object for a multi-bind function.
1092 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1093 * of generating an error if the buffer ID is not zero or the name
1094 * of an existing buffer object.
1096 * If the buffer ID refers to an existing buffer object, a pointer
1097 * to the buffer object is returned. If the ID is zero, a pointer
1098 * to the shared NullBufferObj is returned. If the ID is not zero
1099 * and does not refer to a valid buffer object, this function
1102 * This function assumes that the caller has already locked the
1103 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1105 struct gl_buffer_object *
1106 _mesa_multi_bind_lookup_bufferobj(struct gl_context *ctx,
1107 const GLuint *buffers,
1108 GLuint index, const char *caller)
1110 struct gl_buffer_object *bufObj;
1112 if (buffers[index] != 0) {
1113 bufObj = _mesa_lookup_bufferobj_locked(ctx, buffers[index]);
1115 /* The multi-bind functions don't create the buffer objects
1116 when they don't exist. */
1117 if (bufObj == &DummyBufferObject)
1120 bufObj = ctx->Shared->NullBufferObj;
1123 /* The ARB_multi_bind spec says:
1125 * "An INVALID_OPERATION error is generated if any value
1126 * in <buffers> is not zero or the name of an existing
1127 * buffer object (per binding)."
1129 _mesa_error(ctx, GL_INVALID_OPERATION,
1130 "%s(buffers[%u]=%u is not zero or the name "
1131 "of an existing buffer object)",
1132 caller, index, buffers[index]);
1140 * If *ptr points to obj, set ptr = the Null/default buffer object.
1141 * This is a helper for buffer object deletion.
1142 * The GL spec says that deleting a buffer object causes it to get
1143 * unbound from all arrays in the current context.
1146 unbind(struct gl_context *ctx,
1147 struct gl_buffer_object **ptr,
1148 struct gl_buffer_object *obj)
1151 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
1157 * Plug default/fallback buffer object functions into the device
1161 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
1163 /* GL_ARB_vertex/pixel_buffer_object */
1164 driver->NewBufferObject = _mesa_new_buffer_object;
1165 driver->DeleteBuffer = _mesa_delete_buffer_object;
1166 driver->BufferData = buffer_data_fallback;
1167 driver->BufferSubData = buffer_sub_data_fallback;
1168 driver->GetBufferSubData = _mesa_buffer_get_subdata;
1169 driver->UnmapBuffer = unmap_buffer_fallback;
1171 /* GL_ARB_clear_buffer_object */
1172 driver->ClearBufferSubData = _mesa_ClearBufferSubData_sw;
1174 /* GL_ARB_map_buffer_range */
1175 driver->MapBufferRange = map_buffer_range_fallback;
1176 driver->FlushMappedBufferRange = flush_mapped_buffer_range_fallback;
1178 /* GL_ARB_copy_buffer */
1179 driver->CopyBufferSubData = copy_buffer_sub_data_fallback;
1184 _mesa_buffer_unmap_all_mappings(struct gl_context *ctx,
1185 struct gl_buffer_object *bufObj)
1189 for (i = 0; i < MAP_COUNT; i++) {
1190 if (_mesa_bufferobj_mapped(bufObj, i)) {
1191 ctx->Driver.UnmapBuffer(ctx, bufObj, i);
1192 assert(bufObj->Mappings[i].Pointer == NULL);
1193 bufObj->Mappings[i].AccessFlags = 0;
1199 /**********************************************************************/
1201 /**********************************************************************/
1204 _mesa_BindBuffer(GLenum target, GLuint buffer)
1206 GET_CURRENT_CONTEXT(ctx);
1208 if (MESA_VERBOSE & VERBOSE_API) {
1209 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
1210 _mesa_enum_to_string(target), buffer);
1213 bind_buffer_object(ctx, target, buffer);
1218 * Delete a set of buffer objects.
1220 * \param n Number of buffer objects to delete.
1221 * \param ids Array of \c n buffer object IDs.
1224 _mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
1226 GET_CURRENT_CONTEXT(ctx);
1228 FLUSH_VERTICES(ctx, 0);
1231 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
1235 mtx_lock(&ctx->Shared->Mutex);
1237 for (i = 0; i < n; i++) {
1238 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
1240 struct gl_vertex_array_object *vao = ctx->Array.VAO;
1243 assert(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
1245 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1247 /* unbind any vertex pointers bound to this buffer */
1248 for (j = 0; j < ARRAY_SIZE(vao->VertexBinding); j++) {
1249 unbind(ctx, &vao->VertexBinding[j].BufferObj, bufObj);
1252 if (ctx->Array.ArrayBufferObj == bufObj) {
1253 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB, 0 );
1255 if (vao->IndexBufferObj == bufObj) {
1256 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
1259 /* unbind ARB_draw_indirect binding point */
1260 if (ctx->DrawIndirectBuffer == bufObj) {
1261 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER, 0 );
1264 /* unbind ARB_compute_shader binding point */
1265 if (ctx->DispatchIndirectBuffer == bufObj) {
1266 _mesa_BindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0);
1269 /* unbind ARB_copy_buffer binding points */
1270 if (ctx->CopyReadBuffer == bufObj) {
1271 _mesa_BindBuffer( GL_COPY_READ_BUFFER, 0 );
1273 if (ctx->CopyWriteBuffer == bufObj) {
1274 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER, 0 );
1277 /* unbind transform feedback binding points */
1278 if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
1279 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER, 0 );
1281 for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
1282 if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
1283 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 );
1287 /* unbind UBO binding points */
1288 for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
1289 if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
1290 _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 );
1294 if (ctx->UniformBuffer == bufObj) {
1295 _mesa_BindBuffer( GL_UNIFORM_BUFFER, 0 );
1298 /* unbind SSBO binding points */
1299 for (j = 0; j < ctx->Const.MaxShaderStorageBufferBindings; j++) {
1300 if (ctx->ShaderStorageBufferBindings[j].BufferObject == bufObj) {
1301 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER, j, 0);
1305 if (ctx->ShaderStorageBuffer == bufObj) {
1306 _mesa_BindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
1309 /* unbind Atomci Buffer binding points */
1310 for (j = 0; j < ctx->Const.MaxAtomicBufferBindings; j++) {
1311 if (ctx->AtomicBufferBindings[j].BufferObject == bufObj) {
1312 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER, j, 0 );
1316 if (ctx->AtomicBuffer == bufObj) {
1317 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER, 0 );
1320 /* unbind any pixel pack/unpack pointers bound to this buffer */
1321 if (ctx->Pack.BufferObj == bufObj) {
1322 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT, 0 );
1324 if (ctx->Unpack.BufferObj == bufObj) {
1325 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
1328 if (ctx->Texture.BufferObject == bufObj) {
1329 _mesa_BindBuffer( GL_TEXTURE_BUFFER, 0 );
1332 if (ctx->ExternalVirtualMemoryBuffer == bufObj) {
1333 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, 0);
1336 /* The ID is immediately freed for re-use */
1337 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
1338 /* Make sure we do not run into the classic ABA problem on bind.
1339 * We don't want to allow re-binding a buffer object that's been
1340 * "deleted" by glDeleteBuffers().
1342 * The explicit rebinding to the default object in the current context
1343 * prevents the above in the current context, but another context
1344 * sharing the same objects might suffer from this problem.
1345 * The alternative would be to do the hash lookup in any case on bind
1346 * which would introduce more runtime overhead than this.
1348 bufObj->DeletePending = GL_TRUE;
1349 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
1353 mtx_unlock(&ctx->Shared->Mutex);
1358 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1359 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1363 create_buffers(GLsizei n, GLuint *buffers, bool dsa)
1365 GET_CURRENT_CONTEXT(ctx);
1368 struct gl_buffer_object *buf;
1370 const char *func = dsa ? "glCreateBuffers" : "glGenBuffers";
1372 if (MESA_VERBOSE & VERBOSE_API)
1373 _mesa_debug(ctx, "%s(%d)\n", func, n);
1376 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n %d < 0)", func, n);
1385 * This must be atomic (generation and allocation of buffer object IDs)
1387 mtx_lock(&ctx->Shared->Mutex);
1389 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
1391 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1392 * DummyBufferObject. Otherwise, create a new buffer object and insert
1395 for (i = 0; i < n; i++) {
1396 buffers[i] = first + i;
1398 assert(ctx->Driver.NewBufferObject);
1399 buf = ctx->Driver.NewBufferObject(ctx, buffers[i]);
1401 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1402 mtx_unlock(&ctx->Shared->Mutex);
1407 buf = &DummyBufferObject;
1409 _mesa_HashInsert(ctx->Shared->BufferObjects, buffers[i], buf);
1412 mtx_unlock(&ctx->Shared->Mutex);
1416 * Generate a set of unique buffer object IDs and store them in \c buffers.
1418 * \param n Number of IDs to generate.
1419 * \param buffers Array of \c n locations to store the IDs.
1422 _mesa_GenBuffers(GLsizei n, GLuint *buffers)
1424 create_buffers(n, buffers, false);
1428 * Create a set of buffer objects and store their unique IDs in \c buffers.
1430 * \param n Number of IDs to generate.
1431 * \param buffers Array of \c n locations to store the IDs.
1434 _mesa_CreateBuffers(GLsizei n, GLuint *buffers)
1436 create_buffers(n, buffers, true);
1441 * Determine if ID is the name of a buffer object.
1443 * \param id ID of the potential buffer object.
1444 * \return \c GL_TRUE if \c id is the name of a buffer object,
1445 * \c GL_FALSE otherwise.
1447 GLboolean GLAPIENTRY
1448 _mesa_IsBuffer(GLuint id)
1450 struct gl_buffer_object *bufObj;
1451 GET_CURRENT_CONTEXT(ctx);
1452 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1454 mtx_lock(&ctx->Shared->Mutex);
1455 bufObj = _mesa_lookup_bufferobj(ctx, id);
1456 mtx_unlock(&ctx->Shared->Mutex);
1458 return bufObj && bufObj != &DummyBufferObject;
1463 _mesa_buffer_storage(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1464 GLenum target, GLsizeiptr size, const GLvoid *data,
1465 GLbitfield flags, const char *func)
1468 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size <= 0)", func);
1472 if (flags & ~(GL_MAP_READ_BIT |
1474 GL_MAP_PERSISTENT_BIT |
1475 GL_MAP_COHERENT_BIT |
1476 GL_DYNAMIC_STORAGE_BIT |
1477 GL_CLIENT_STORAGE_BIT)) {
1478 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid flag bits set)", func);
1482 if (flags & GL_MAP_PERSISTENT_BIT &&
1483 !(flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT))) {
1484 _mesa_error(ctx, GL_INVALID_VALUE,
1485 "%s(PERSISTENT and flags!=READ/WRITE)", func);
1489 if (flags & GL_MAP_COHERENT_BIT && !(flags & GL_MAP_PERSISTENT_BIT)) {
1490 _mesa_error(ctx, GL_INVALID_VALUE,
1491 "%s(COHERENT and flags!=PERSISTENT)", func);
1495 if (bufObj->Immutable) {
1496 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
1500 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1501 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1503 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1505 bufObj->Written = GL_TRUE;
1506 bufObj->Immutable = GL_TRUE;
1508 assert(ctx->Driver.BufferData);
1509 if (!ctx->Driver.BufferData(ctx, target, size, data, GL_DYNAMIC_DRAW,
1511 if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
1512 /* Even though the interaction between AMD_pinned_memory and
1513 * glBufferStorage is not described in the spec, Graham Sellers
1514 * said that it should behave the same as glBufferData.
1516 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1519 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1525 _mesa_BufferStorage(GLenum target, GLsizeiptr size, const GLvoid *data,
1528 GET_CURRENT_CONTEXT(ctx);
1529 struct gl_buffer_object *bufObj;
1531 bufObj = get_buffer(ctx, "glBufferStorage", target, GL_INVALID_OPERATION);
1535 _mesa_buffer_storage(ctx, bufObj, target, size, data, flags,
1540 _mesa_NamedBufferStorage(GLuint buffer, GLsizeiptr size, const GLvoid *data,
1543 GET_CURRENT_CONTEXT(ctx);
1544 struct gl_buffer_object *bufObj;
1546 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferStorage");
1551 * In direct state access, buffer objects have an unspecified target since
1552 * they are not required to be bound.
1554 _mesa_buffer_storage(ctx, bufObj, GL_NONE, size, data, flags,
1555 "glNamedBufferStorage");
1560 _mesa_buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1561 GLenum target, GLsizeiptr size, const GLvoid *data,
1562 GLenum usage, const char *func)
1566 if (MESA_VERBOSE & VERBOSE_API) {
1567 _mesa_debug(ctx, "%s(%s, %ld, %p, %s)\n",
1569 _mesa_enum_to_string(target),
1570 (long int) size, data,
1571 _mesa_enum_to_string(usage));
1575 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", func);
1580 case GL_STREAM_DRAW_ARB:
1581 valid_usage = (ctx->API != API_OPENGLES);
1584 case GL_STATIC_DRAW_ARB:
1585 case GL_DYNAMIC_DRAW_ARB:
1589 case GL_STREAM_READ_ARB:
1590 case GL_STREAM_COPY_ARB:
1591 case GL_STATIC_READ_ARB:
1592 case GL_STATIC_COPY_ARB:
1593 case GL_DYNAMIC_READ_ARB:
1594 case GL_DYNAMIC_COPY_ARB:
1595 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
1599 valid_usage = false;
1604 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid usage: %s)", func,
1605 _mesa_enum_to_string(usage));
1609 if (bufObj->Immutable) {
1610 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
1614 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1615 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1617 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1619 bufObj->Written = GL_TRUE;
1622 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1623 bufObj->Name, size, data, usage);
1630 assert(ctx->Driver.BufferData);
1631 if (!ctx->Driver.BufferData(ctx, target, size, data, usage,
1634 GL_DYNAMIC_STORAGE_BIT,
1636 if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
1637 /* From GL_AMD_pinned_memory:
1639 * INVALID_OPERATION is generated by BufferData if <target> is
1640 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1641 * mapped to the GPU address space.
1643 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1646 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1652 _mesa_BufferData(GLenum target, GLsizeiptr size,
1653 const GLvoid *data, GLenum usage)
1655 GET_CURRENT_CONTEXT(ctx);
1656 struct gl_buffer_object *bufObj;
1658 bufObj = get_buffer(ctx, "glBufferData", target, GL_INVALID_OPERATION);
1662 _mesa_buffer_data(ctx, bufObj, target, size, data, usage,
1667 _mesa_NamedBufferData(GLuint buffer, GLsizeiptr size, const GLvoid *data,
1670 GET_CURRENT_CONTEXT(ctx);
1671 struct gl_buffer_object *bufObj;
1673 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferData");
1677 /* In direct state access, buffer objects have an unspecified target since
1678 * they are not required to be bound.
1680 _mesa_buffer_data(ctx, bufObj, GL_NONE, size, data, usage,
1681 "glNamedBufferData");
1686 * Implementation for glBufferSubData and glNamedBufferSubData.
1688 * \param ctx GL context.
1689 * \param bufObj The buffer object.
1690 * \param offset Offset of the first byte of the subdata range.
1691 * \param size Size, in bytes, of the subdata range.
1692 * \param data The data store.
1693 * \param func Name of calling function for recording errors.
1697 _mesa_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1698 GLintptr offset, GLsizeiptr size, const GLvoid *data,
1701 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size,
1703 /* error already recorded */
1707 if (bufObj->Immutable &&
1708 !(bufObj->StorageFlags & GL_DYNAMIC_STORAGE_BIT)) {
1709 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1716 bufObj->NumSubDataCalls++;
1718 if ((bufObj->Usage == GL_STATIC_DRAW ||
1719 bufObj->Usage == GL_STATIC_COPY) &&
1720 bufObj->NumSubDataCalls >= BUFFER_WARNING_CALL_COUNT) {
1721 /* If the application declared the buffer as static draw/copy or stream
1722 * draw, it should not be frequently modified with glBufferSubData.
1724 BUFFER_USAGE_WARNING(ctx,
1725 "using %s(buffer %u, offset %u, size %u) to "
1726 "update a %s buffer",
1727 func, bufObj->Name, offset, size,
1728 _mesa_enum_to_string(bufObj->Usage));
1731 bufObj->Written = GL_TRUE;
1733 assert(ctx->Driver.BufferSubData);
1734 ctx->Driver.BufferSubData(ctx, offset, size, data, bufObj);
1738 _mesa_BufferSubData(GLenum target, GLintptr offset,
1739 GLsizeiptr size, const GLvoid *data)
1741 GET_CURRENT_CONTEXT(ctx);
1742 struct gl_buffer_object *bufObj;
1744 bufObj = get_buffer(ctx, "glBufferSubData", target, GL_INVALID_OPERATION);
1748 _mesa_buffer_sub_data(ctx, bufObj, offset, size, data, "glBufferSubData");
1752 _mesa_NamedBufferSubData(GLuint buffer, GLintptr offset,
1753 GLsizeiptr size, const GLvoid *data)
1755 GET_CURRENT_CONTEXT(ctx);
1756 struct gl_buffer_object *bufObj;
1758 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferSubData");
1762 _mesa_buffer_sub_data(ctx, bufObj, offset, size, data,
1763 "glNamedBufferSubData");
1768 _mesa_GetBufferSubData(GLenum target, GLintptr offset,
1769 GLsizeiptr size, GLvoid *data)
1771 GET_CURRENT_CONTEXT(ctx);
1772 struct gl_buffer_object *bufObj;
1774 bufObj = get_buffer(ctx, "glGetBufferSubData", target,
1775 GL_INVALID_OPERATION);
1779 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
1780 "glGetBufferSubData")) {
1784 assert(ctx->Driver.GetBufferSubData);
1785 ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
1789 _mesa_GetNamedBufferSubData(GLuint buffer, GLintptr offset,
1790 GLsizeiptr size, GLvoid *data)
1792 GET_CURRENT_CONTEXT(ctx);
1793 struct gl_buffer_object *bufObj;
1795 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
1796 "glGetNamedBufferSubData");
1800 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
1801 "glGetNamedBufferSubData")) {
1805 assert(ctx->Driver.GetBufferSubData);
1806 ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
1811 * \param subdata true if caller is *SubData, false if *Data
1814 _mesa_clear_buffer_sub_data(struct gl_context *ctx,
1815 struct gl_buffer_object *bufObj,
1816 GLenum internalformat,
1817 GLintptr offset, GLsizeiptr size,
1818 GLenum format, GLenum type,
1820 const char *func, bool subdata)
1822 mesa_format mesaFormat;
1823 GLubyte clearValue[MAX_PIXEL_BYTES];
1824 GLsizeiptr clearValueSize;
1826 /* This checks for disallowed mappings. */
1827 if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size,
1832 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1833 format, type, func);
1835 if (mesaFormat == MESA_FORMAT_NONE) {
1839 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1840 if (offset % clearValueSize != 0 || size % clearValueSize != 0) {
1841 _mesa_error(ctx, GL_INVALID_VALUE,
1842 "%s(offset or size is not a multiple of "
1843 "internalformat size)", func);
1848 /* clear to zeros, per the spec */
1850 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1851 NULL, clearValueSize, bufObj);
1856 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
1857 format, type, data, func)) {
1862 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1863 clearValue, clearValueSize, bufObj);
1868 _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
1869 GLenum type, const GLvoid *data)
1871 GET_CURRENT_CONTEXT(ctx);
1872 struct gl_buffer_object *bufObj;
1874 bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE);
1878 _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, 0, bufObj->Size,
1880 "glClearBufferData", false);
1884 _mesa_ClearNamedBufferData(GLuint buffer, GLenum internalformat,
1885 GLenum format, GLenum type, const GLvoid *data)
1887 GET_CURRENT_CONTEXT(ctx);
1888 struct gl_buffer_object *bufObj;
1890 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glClearNamedBufferData");
1894 _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, 0, bufObj->Size,
1896 "glClearNamedBufferData", false);
1901 _mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
1902 GLintptr offset, GLsizeiptr size,
1903 GLenum format, GLenum type,
1906 GET_CURRENT_CONTEXT(ctx);
1907 struct gl_buffer_object *bufObj;
1909 bufObj = get_buffer(ctx, "glClearBufferSubData", target, GL_INVALID_VALUE);
1913 _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size,
1915 "glClearBufferSubData", true);
1919 _mesa_ClearNamedBufferSubData(GLuint buffer, GLenum internalformat,
1920 GLintptr offset, GLsizeiptr size,
1921 GLenum format, GLenum type,
1924 GET_CURRENT_CONTEXT(ctx);
1925 struct gl_buffer_object *bufObj;
1927 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
1928 "glClearNamedBufferSubData");
1932 _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size,
1934 "glClearNamedBufferSubData", true);
1939 _mesa_unmap_buffer(struct gl_context *ctx, struct gl_buffer_object *bufObj,
1942 GLboolean status = GL_TRUE;
1943 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1945 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
1946 _mesa_error(ctx, GL_INVALID_OPERATION,
1947 "%s(buffer is not mapped)", func);
1952 if (bufObj->Access != GL_READ_ONLY_ARB) {
1953 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1955 /* check that last 100 bytes are still = magic value */
1956 for (i = 0; i < 100; i++) {
1957 GLuint pos = bufObj->Size - i - 1;
1958 if (buf[pos] != 123) {
1959 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1960 " at position %d (value = %u)\n",
1968 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1969 GLuint i, unchanged = 0;
1970 GLubyte *b = (GLubyte *) bufObj->Pointer;
1972 /* check which bytes changed */
1973 for (i = 0; i < bufObj->Size - 1; i++) {
1974 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1981 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1982 bufObj->Name, unchanged, bufObj->Size, pos);
1987 status = ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_USER);
1988 bufObj->Mappings[MAP_USER].AccessFlags = 0;
1989 assert(bufObj->Mappings[MAP_USER].Pointer == NULL);
1990 assert(bufObj->Mappings[MAP_USER].Offset == 0);
1991 assert(bufObj->Mappings[MAP_USER].Length == 0);
1996 GLboolean GLAPIENTRY
1997 _mesa_UnmapBuffer(GLenum target)
1999 GET_CURRENT_CONTEXT(ctx);
2000 struct gl_buffer_object *bufObj;
2002 bufObj = get_buffer(ctx, "glUnmapBuffer", target, GL_INVALID_OPERATION);
2006 return _mesa_unmap_buffer(ctx, bufObj, "glUnmapBuffer");
2009 GLboolean GLAPIENTRY
2010 _mesa_UnmapNamedBuffer(GLuint buffer)
2012 GET_CURRENT_CONTEXT(ctx);
2013 struct gl_buffer_object *bufObj;
2015 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glUnmapNamedBuffer");
2019 return _mesa_unmap_buffer(ctx, bufObj, "glUnmapNamedBuffer");
2024 get_buffer_parameter(struct gl_context *ctx,
2025 struct gl_buffer_object *bufObj, GLenum pname,
2026 GLint64 *params, const char *func)
2029 case GL_BUFFER_SIZE_ARB:
2030 *params = bufObj->Size;
2032 case GL_BUFFER_USAGE_ARB:
2033 *params = bufObj->Usage;
2035 case GL_BUFFER_ACCESS_ARB:
2036 *params = simplified_access_mode(ctx,
2037 bufObj->Mappings[MAP_USER].AccessFlags);
2039 case GL_BUFFER_MAPPED_ARB:
2040 *params = _mesa_bufferobj_mapped(bufObj, MAP_USER);
2042 case GL_BUFFER_ACCESS_FLAGS:
2043 if (!ctx->Extensions.ARB_map_buffer_range)
2045 *params = bufObj->Mappings[MAP_USER].AccessFlags;
2047 case GL_BUFFER_MAP_OFFSET:
2048 if (!ctx->Extensions.ARB_map_buffer_range)
2050 *params = bufObj->Mappings[MAP_USER].Offset;
2052 case GL_BUFFER_MAP_LENGTH:
2053 if (!ctx->Extensions.ARB_map_buffer_range)
2055 *params = bufObj->Mappings[MAP_USER].Length;
2057 case GL_BUFFER_IMMUTABLE_STORAGE:
2058 if (!ctx->Extensions.ARB_buffer_storage)
2060 *params = bufObj->Immutable;
2062 case GL_BUFFER_STORAGE_FLAGS:
2063 if (!ctx->Extensions.ARB_buffer_storage)
2065 *params = bufObj->StorageFlags;
2074 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname: %s)", func,
2075 _mesa_enum_to_string(pname));
2080 _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
2082 GET_CURRENT_CONTEXT(ctx);
2083 struct gl_buffer_object *bufObj;
2086 bufObj = get_buffer(ctx, "glGetBufferParameteriv", target,
2087 GL_INVALID_OPERATION);
2091 if (!get_buffer_parameter(ctx, bufObj, pname, ¶meter,
2092 "glGetBufferParameteriv"))
2093 return; /* Error already recorded. */
2095 *params = (GLint) parameter;
2099 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
2101 GET_CURRENT_CONTEXT(ctx);
2102 struct gl_buffer_object *bufObj;
2105 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target,
2106 GL_INVALID_OPERATION);
2110 if (!get_buffer_parameter(ctx, bufObj, pname, ¶meter,
2111 "glGetBufferParameteri64v"))
2112 return; /* Error already recorded. */
2114 *params = parameter;
2118 _mesa_GetNamedBufferParameteriv(GLuint buffer, GLenum pname, GLint *params)
2120 GET_CURRENT_CONTEXT(ctx);
2121 struct gl_buffer_object *bufObj;
2124 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2125 "glGetNamedBufferParameteriv");
2129 if (!get_buffer_parameter(ctx, bufObj, pname, ¶meter,
2130 "glGetNamedBufferParameteriv"))
2131 return; /* Error already recorded. */
2133 *params = (GLint) parameter;
2137 _mesa_GetNamedBufferParameteri64v(GLuint buffer, GLenum pname,
2140 GET_CURRENT_CONTEXT(ctx);
2141 struct gl_buffer_object *bufObj;
2144 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2145 "glGetNamedBufferParameteri64v");
2149 if (!get_buffer_parameter(ctx, bufObj, pname, ¶meter,
2150 "glGetNamedBufferParameteri64v"))
2151 return; /* Error already recorded. */
2153 *params = parameter;
2158 _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
2160 GET_CURRENT_CONTEXT(ctx);
2161 struct gl_buffer_object *bufObj;
2163 if (pname != GL_BUFFER_MAP_POINTER) {
2164 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointerv(pname != "
2165 "GL_BUFFER_MAP_POINTER)");
2169 bufObj = get_buffer(ctx, "glGetBufferPointerv", target,
2170 GL_INVALID_OPERATION);
2174 *params = bufObj->Mappings[MAP_USER].Pointer;
2178 _mesa_GetNamedBufferPointerv(GLuint buffer, GLenum pname, GLvoid **params)
2180 GET_CURRENT_CONTEXT(ctx);
2181 struct gl_buffer_object *bufObj;
2183 if (pname != GL_BUFFER_MAP_POINTER) {
2184 _mesa_error(ctx, GL_INVALID_ENUM, "glGetNamedBufferPointerv(pname != "
2185 "GL_BUFFER_MAP_POINTER)");
2189 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2190 "glGetNamedBufferPointerv");
2194 *params = bufObj->Mappings[MAP_USER].Pointer;
2199 _mesa_copy_buffer_sub_data(struct gl_context *ctx,
2200 struct gl_buffer_object *src,
2201 struct gl_buffer_object *dst,
2202 GLintptr readOffset, GLintptr writeOffset,
2203 GLsizeiptr size, const char *func)
2205 if (_mesa_check_disallowed_mapping(src)) {
2206 _mesa_error(ctx, GL_INVALID_OPERATION,
2207 "%s(readBuffer is mapped)", func);
2211 if (_mesa_check_disallowed_mapping(dst)) {
2212 _mesa_error(ctx, GL_INVALID_OPERATION,
2213 "%s(writeBuffer is mapped)", func);
2217 if (readOffset < 0) {
2218 _mesa_error(ctx, GL_INVALID_VALUE,
2219 "%s(readOffset %d < 0)", func, (int) readOffset);
2223 if (writeOffset < 0) {
2224 _mesa_error(ctx, GL_INVALID_VALUE,
2225 "%s(writeOffset %d < 0)", func, (int) writeOffset);
2230 _mesa_error(ctx, GL_INVALID_VALUE,
2231 "%s(size %d < 0)", func, (int) size);
2235 if (readOffset + size > src->Size) {
2236 _mesa_error(ctx, GL_INVALID_VALUE,
2237 "%s(readOffset %d + size %d > src_buffer_size %d)", func,
2238 (int) readOffset, (int) size, (int) src->Size);
2242 if (writeOffset + size > dst->Size) {
2243 _mesa_error(ctx, GL_INVALID_VALUE,
2244 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func,
2245 (int) writeOffset, (int) size, (int) dst->Size);
2250 if (readOffset + size <= writeOffset) {
2253 else if (writeOffset + size <= readOffset) {
2257 /* overlapping src/dst is illegal */
2258 _mesa_error(ctx, GL_INVALID_VALUE,
2259 "%s(overlapping src/dst)", func);
2264 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
2268 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
2269 GLintptr readOffset, GLintptr writeOffset,
2272 GET_CURRENT_CONTEXT(ctx);
2273 struct gl_buffer_object *src, *dst;
2275 src = get_buffer(ctx, "glCopyBufferSubData", readTarget,
2276 GL_INVALID_OPERATION);
2280 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget,
2281 GL_INVALID_OPERATION);
2285 _mesa_copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
2286 "glCopyBufferSubData");
2290 _mesa_CopyNamedBufferSubData(GLuint readBuffer, GLuint writeBuffer,
2291 GLintptr readOffset, GLintptr writeOffset,
2294 GET_CURRENT_CONTEXT(ctx);
2295 struct gl_buffer_object *src, *dst;
2297 src = _mesa_lookup_bufferobj_err(ctx, readBuffer,
2298 "glCopyNamedBufferSubData");
2302 dst = _mesa_lookup_bufferobj_err(ctx, writeBuffer,
2303 "glCopyNamedBufferSubData");
2307 _mesa_copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
2308 "glCopyNamedBufferSubData");
2313 _mesa_map_buffer_range(struct gl_context *ctx,
2314 struct gl_buffer_object *bufObj,
2315 GLintptr offset, GLsizeiptr length,
2316 GLbitfield access, const char *func)
2319 GLbitfield allowed_access;
2321 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
2324 _mesa_error(ctx, GL_INVALID_VALUE,
2325 "%s(offset %ld < 0)", func, (long) offset);
2330 _mesa_error(ctx, GL_INVALID_VALUE,
2331 "%s(length %ld < 0)", func, (long) length);
2335 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2337 * "An INVALID_OPERATION error is generated for any of the following
2340 * * <length> is zero."
2342 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2343 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2347 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(length = 0)", func);
2351 allowed_access = GL_MAP_READ_BIT |
2353 GL_MAP_INVALIDATE_RANGE_BIT |
2354 GL_MAP_INVALIDATE_BUFFER_BIT |
2355 GL_MAP_FLUSH_EXPLICIT_BIT |
2356 GL_MAP_UNSYNCHRONIZED_BIT;
2358 if (ctx->Extensions.ARB_buffer_storage) {
2359 allowed_access |= GL_MAP_PERSISTENT_BIT |
2360 GL_MAP_COHERENT_BIT;
2363 if (access & ~allowed_access) {
2364 /* generate an error if any bits other than those allowed are set */
2365 _mesa_error(ctx, GL_INVALID_VALUE,
2366 "%s(access has undefined bits set)", func);
2370 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
2371 _mesa_error(ctx, GL_INVALID_OPERATION,
2372 "%s(access indicates neither read or write)", func);
2376 if ((access & GL_MAP_READ_BIT) &&
2377 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
2378 GL_MAP_INVALIDATE_BUFFER_BIT |
2379 GL_MAP_UNSYNCHRONIZED_BIT))) {
2380 _mesa_error(ctx, GL_INVALID_OPERATION,
2381 "%s(read access with disallowed bits)", func);
2385 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
2386 ((access & GL_MAP_WRITE_BIT) == 0)) {
2387 _mesa_error(ctx, GL_INVALID_OPERATION,
2388 "%s(access has flush explicit without write)", func);
2392 if (access & GL_MAP_READ_BIT &&
2393 !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
2394 _mesa_error(ctx, GL_INVALID_OPERATION,
2395 "%s(buffer does not allow read access)", func);
2399 if (access & GL_MAP_WRITE_BIT &&
2400 !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
2401 _mesa_error(ctx, GL_INVALID_OPERATION,
2402 "%s(buffer does not allow write access)", func);
2406 if (access & GL_MAP_COHERENT_BIT &&
2407 !(bufObj->StorageFlags & GL_MAP_COHERENT_BIT)) {
2408 _mesa_error(ctx, GL_INVALID_OPERATION,
2409 "%s(buffer does not allow coherent access)", func);
2413 if (access & GL_MAP_PERSISTENT_BIT &&
2414 !(bufObj->StorageFlags & GL_MAP_PERSISTENT_BIT)) {
2415 _mesa_error(ctx, GL_INVALID_OPERATION,
2416 "%s(buffer does not allow persistent access)", func);
2420 if (offset + length > bufObj->Size) {
2421 _mesa_error(ctx, GL_INVALID_VALUE,
2422 "%s(offset %td + length %td > buffer_size %td)", func,
2423 offset, length, bufObj->Size);
2427 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2428 _mesa_error(ctx, GL_INVALID_OPERATION,
2429 "%s(buffer already mapped)", func);
2433 if (!bufObj->Size) {
2434 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(buffer size = 0)", func);
2438 if (access & GL_MAP_WRITE_BIT) {
2439 bufObj->NumMapBufferWriteCalls++;
2440 if ((bufObj->Usage == GL_STATIC_DRAW ||
2441 bufObj->Usage == GL_STATIC_COPY) &&
2442 bufObj->NumMapBufferWriteCalls >= BUFFER_WARNING_CALL_COUNT) {
2443 BUFFER_USAGE_WARNING(ctx,
2444 "using %s(buffer %u, offset %u, length %u) to "
2445 "update a %s buffer",
2446 func, bufObj->Name, offset, length,
2447 _mesa_enum_to_string(bufObj->Usage));
2451 assert(ctx->Driver.MapBufferRange);
2452 map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj,
2455 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(map failed)", func);
2458 /* The driver callback should have set all these fields.
2459 * This is important because other modules (like VBO) might call
2460 * the driver function directly.
2462 assert(bufObj->Mappings[MAP_USER].Pointer == map);
2463 assert(bufObj->Mappings[MAP_USER].Length == length);
2464 assert(bufObj->Mappings[MAP_USER].Offset == offset);
2465 assert(bufObj->Mappings[MAP_USER].AccessFlags == access);
2468 if (access & GL_MAP_WRITE_BIT)
2469 bufObj->Written = GL_TRUE;
2472 if (strstr(func, "Range") == NULL) { /* If not MapRange */
2473 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2474 bufObj->Name, bufObj->Size, access);
2475 /* Access must be write only */
2476 if ((access & GL_MAP_WRITE_BIT) && (!(access & ~GL_MAP_WRITE_BIT))) {
2478 GLubyte *b = (GLubyte *) bufObj->Pointer;
2479 for (i = 0; i < bufObj->Size; i++)
2486 if (strstr(func, "Range") == NULL) { /* If not MapRange */
2487 GLubyte *buf = (GLubyte *) bufObj->Pointer;
2489 /* buffer is 100 bytes larger than requested, fill with magic value */
2490 for (i = 0; i < 100; i++) {
2491 buf[bufObj->Size - i - 1] = 123;
2500 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
2503 GET_CURRENT_CONTEXT(ctx);
2504 struct gl_buffer_object *bufObj;
2506 if (!ctx->Extensions.ARB_map_buffer_range) {
2507 _mesa_error(ctx, GL_INVALID_OPERATION,
2508 "glMapBufferRange(ARB_map_buffer_range not supported)");
2512 bufObj = get_buffer(ctx, "glMapBufferRange", target, GL_INVALID_OPERATION);
2516 return _mesa_map_buffer_range(ctx, bufObj, offset, length, access,
2517 "glMapBufferRange");
2521 _mesa_MapNamedBufferRange(GLuint buffer, GLintptr offset, GLsizeiptr length,
2524 GET_CURRENT_CONTEXT(ctx);
2525 struct gl_buffer_object *bufObj;
2527 if (!ctx->Extensions.ARB_map_buffer_range) {
2528 _mesa_error(ctx, GL_INVALID_OPERATION,
2529 "glMapNamedBufferRange("
2530 "ARB_map_buffer_range not supported)");
2534 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBufferRange");
2538 return _mesa_map_buffer_range(ctx, bufObj, offset, length, access,
2539 "glMapNamedBufferRange");
2543 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2544 * flags for input to _mesa_map_buffer_range.
2546 * \return true if the type of requested access is permissible.
2549 get_map_buffer_access_flags(struct gl_context *ctx, GLenum access,
2553 case GL_READ_ONLY_ARB:
2554 *flags = GL_MAP_READ_BIT;
2555 return _mesa_is_desktop_gl(ctx);
2556 case GL_WRITE_ONLY_ARB:
2557 *flags = GL_MAP_WRITE_BIT;
2559 case GL_READ_WRITE_ARB:
2560 *flags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
2561 return _mesa_is_desktop_gl(ctx);
2568 _mesa_MapBuffer(GLenum target, GLenum access)
2570 GET_CURRENT_CONTEXT(ctx);
2571 struct gl_buffer_object *bufObj;
2572 GLbitfield accessFlags;
2574 if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
2575 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBuffer(invalid access)");
2579 bufObj = get_buffer(ctx, "glMapBuffer", target, GL_INVALID_OPERATION);
2583 return _mesa_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2588 _mesa_MapNamedBuffer(GLuint buffer, GLenum access)
2590 GET_CURRENT_CONTEXT(ctx);
2591 struct gl_buffer_object *bufObj;
2592 GLbitfield accessFlags;
2594 if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
2595 _mesa_error(ctx, GL_INVALID_ENUM, "glMapNamedBuffer(invalid access)");
2599 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBuffer");
2603 return _mesa_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
2604 "glMapNamedBuffer");
2609 _mesa_flush_mapped_buffer_range(struct gl_context *ctx,
2610 struct gl_buffer_object *bufObj,
2611 GLintptr offset, GLsizeiptr length,
2614 if (!ctx->Extensions.ARB_map_buffer_range) {
2615 _mesa_error(ctx, GL_INVALID_OPERATION,
2616 "%s(ARB_map_buffer_range not supported)", func);
2621 _mesa_error(ctx, GL_INVALID_VALUE,
2622 "%s(offset %ld < 0)", func, (long) offset);
2627 _mesa_error(ctx, GL_INVALID_VALUE,
2628 "%s(length %ld < 0)", func, (long) length);
2632 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2633 /* buffer is not mapped */
2634 _mesa_error(ctx, GL_INVALID_OPERATION,
2635 "%s(buffer is not mapped)", func);
2639 if ((bufObj->Mappings[MAP_USER].AccessFlags &
2640 GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
2641 _mesa_error(ctx, GL_INVALID_OPERATION,
2642 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func);
2646 if (offset + length > bufObj->Mappings[MAP_USER].Length) {
2647 _mesa_error(ctx, GL_INVALID_VALUE,
2648 "%s(offset %ld + length %ld > mapped length %ld)", func,
2649 (long) offset, (long) length,
2650 (long) bufObj->Mappings[MAP_USER].Length);
2654 assert(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_WRITE_BIT);
2656 if (ctx->Driver.FlushMappedBufferRange)
2657 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
2662 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset,
2665 GET_CURRENT_CONTEXT(ctx);
2666 struct gl_buffer_object *bufObj;
2668 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target,
2669 GL_INVALID_OPERATION);
2673 _mesa_flush_mapped_buffer_range(ctx, bufObj, offset, length,
2674 "glFlushMappedBufferRange");
2678 _mesa_FlushMappedNamedBufferRange(GLuint buffer, GLintptr offset,
2681 GET_CURRENT_CONTEXT(ctx);
2682 struct gl_buffer_object *bufObj;
2684 bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
2685 "glFlushMappedNamedBufferRange");
2689 _mesa_flush_mapped_buffer_range(ctx, bufObj, offset, length,
2690 "glFlushMappedNamedBufferRange");
2695 * Binds a buffer object to a uniform buffer binding point.
2697 * The caller is responsible for flushing vertices and updating
2701 set_ubo_binding(struct gl_context *ctx,
2702 struct gl_uniform_buffer_binding *binding,
2703 struct gl_buffer_object *bufObj,
2708 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2710 binding->Offset = offset;
2711 binding->Size = size;
2712 binding->AutomaticSize = autoSize;
2714 /* If this is a real buffer object, mark it has having been used
2715 * at some point as a UBO.
2718 bufObj->UsageHistory |= USAGE_UNIFORM_BUFFER;
2722 * Binds a buffer object to a shader storage buffer binding point.
2724 * The caller is responsible for flushing vertices and updating
2728 set_ssbo_binding(struct gl_context *ctx,
2729 struct gl_shader_storage_buffer_binding *binding,
2730 struct gl_buffer_object *bufObj,
2735 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2737 binding->Offset = offset;
2738 binding->Size = size;
2739 binding->AutomaticSize = autoSize;
2741 /* If this is a real buffer object, mark it has having been used
2742 * at some point as a SSBO.
2745 bufObj->UsageHistory |= USAGE_SHADER_STORAGE_BUFFER;
2749 * Binds a buffer object to a uniform buffer binding point.
2751 * Unlike set_ubo_binding(), this function also flushes vertices
2752 * and updates NewDriverState. It also checks if the binding
2753 * has actually changed before updating it.
2756 bind_uniform_buffer(struct gl_context *ctx,
2758 struct gl_buffer_object *bufObj,
2763 struct gl_uniform_buffer_binding *binding =
2764 &ctx->UniformBufferBindings[index];
2766 if (binding->BufferObject == bufObj &&
2767 binding->Offset == offset &&
2768 binding->Size == size &&
2769 binding->AutomaticSize == autoSize) {
2773 FLUSH_VERTICES(ctx, 0);
2774 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
2776 set_ubo_binding(ctx, binding, bufObj, offset, size, autoSize);
2780 * Binds a buffer object to a shader storage buffer binding point.
2782 * Unlike set_ssbo_binding(), this function also flushes vertices
2783 * and updates NewDriverState. It also checks if the binding
2784 * has actually changed before updating it.
2787 bind_shader_storage_buffer(struct gl_context *ctx,
2789 struct gl_buffer_object *bufObj,
2794 struct gl_shader_storage_buffer_binding *binding =
2795 &ctx->ShaderStorageBufferBindings[index];
2797 if (binding->BufferObject == bufObj &&
2798 binding->Offset == offset &&
2799 binding->Size == size &&
2800 binding->AutomaticSize == autoSize) {
2804 FLUSH_VERTICES(ctx, 0);
2805 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
2807 set_ssbo_binding(ctx, binding, bufObj, offset, size, autoSize);
2811 * Bind a region of a buffer object to a uniform block binding point.
2812 * \param index the uniform buffer binding point index
2813 * \param bufObj the buffer object
2814 * \param offset offset to the start of buffer object region
2815 * \param size size of the buffer object region
2818 bind_buffer_range_uniform_buffer(struct gl_context *ctx,
2820 struct gl_buffer_object *bufObj,
2824 if (index >= ctx->Const.MaxUniformBufferBindings) {
2825 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
2829 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
2830 _mesa_error(ctx, GL_INVALID_VALUE,
2831 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
2832 ctx->Const.UniformBufferOffsetAlignment);
2836 if (bufObj == ctx->Shared->NullBufferObj) {
2841 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2842 bind_uniform_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
2846 * Bind a region of a buffer object to a shader storage block binding point.
2847 * \param index the shader storage buffer binding point index
2848 * \param bufObj the buffer object
2849 * \param offset offset to the start of buffer object region
2850 * \param size size of the buffer object region
2853 bind_buffer_range_shader_storage_buffer(struct gl_context *ctx,
2855 struct gl_buffer_object *bufObj,
2859 if (index >= ctx->Const.MaxShaderStorageBufferBindings) {
2860 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
2864 if (offset & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) {
2865 _mesa_error(ctx, GL_INVALID_VALUE,
2866 "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
2867 ctx->Const.ShaderStorageBufferOffsetAlignment);
2871 if (bufObj == ctx->Shared->NullBufferObj) {
2876 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj);
2877 bind_shader_storage_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
2881 * Bind a buffer object to a uniform block binding point.
2882 * As above, but offset = 0.
2885 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
2887 struct gl_buffer_object *bufObj)
2889 if (index >= ctx->Const.MaxUniformBufferBindings) {
2890 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
2894 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2896 if (bufObj == ctx->Shared->NullBufferObj)
2897 bind_uniform_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
2899 bind_uniform_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
2903 * Bind a buffer object to a shader storage block binding point.
2904 * As above, but offset = 0.
2907 bind_buffer_base_shader_storage_buffer(struct gl_context *ctx,
2909 struct gl_buffer_object *bufObj)
2911 if (index >= ctx->Const.MaxShaderStorageBufferBindings) {
2912 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
2916 _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj);
2918 if (bufObj == ctx->Shared->NullBufferObj)
2919 bind_shader_storage_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
2921 bind_shader_storage_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
2925 * Binds a buffer object to an atomic buffer binding point.
2927 * The caller is responsible for validating the offset,
2928 * flushing the vertices and updating NewDriverState.
2931 set_atomic_buffer_binding(struct gl_context *ctx,
2932 struct gl_atomic_buffer_binding *binding,
2933 struct gl_buffer_object *bufObj,
2937 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2939 if (bufObj == ctx->Shared->NullBufferObj) {
2940 binding->Offset = -1;
2943 binding->Offset = offset;
2944 binding->Size = size;
2945 bufObj->UsageHistory |= USAGE_ATOMIC_COUNTER_BUFFER;
2950 * Binds a buffer object to an atomic buffer binding point.
2952 * Unlike set_atomic_buffer_binding(), this function also validates the
2953 * index and offset, flushes vertices, and updates NewDriverState.
2954 * It also checks if the binding has actually changing before
2958 bind_atomic_buffer(struct gl_context *ctx,
2960 struct gl_buffer_object *bufObj,
2965 struct gl_atomic_buffer_binding *binding;
2967 if (index >= ctx->Const.MaxAtomicBufferBindings) {
2968 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index);
2972 if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
2973 _mesa_error(ctx, GL_INVALID_VALUE,
2974 "%s(offset misaligned %d/%d)", name, (int) offset,
2975 ATOMIC_COUNTER_SIZE);
2979 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
2981 binding = &ctx->AtomicBufferBindings[index];
2982 if (binding->BufferObject == bufObj &&
2983 binding->Offset == offset &&
2984 binding->Size == size) {
2988 FLUSH_VERTICES(ctx, 0);
2989 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
2991 set_atomic_buffer_binding(ctx, binding, bufObj, offset, size);
2995 bind_buffers_check_offset_and_size(struct gl_context *ctx,
2997 const GLintptr *offsets,
2998 const GLsizeiptr *sizes)
3000 if (offsets[index] < 0) {
3001 /* The ARB_multi_bind spec says:
3003 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3004 * value in <offsets> is less than zero (per binding)."
3006 _mesa_error(ctx, GL_INVALID_VALUE,
3007 "glBindBuffersRange(offsets[%u]=%" PRId64 " < 0)",
3008 index, (int64_t) offsets[index]);
3012 if (sizes[index] <= 0) {
3013 /* The ARB_multi_bind spec says:
3015 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3016 * value in <sizes> is less than or equal to zero (per binding)."
3018 _mesa_error(ctx, GL_INVALID_VALUE,
3019 "glBindBuffersRange(sizes[%u]=%" PRId64 " <= 0)",
3020 index, (int64_t) sizes[index]);
3028 error_check_bind_uniform_buffers(struct gl_context *ctx,
3029 GLuint first, GLsizei count,
3032 if (!ctx->Extensions.ARB_uniform_buffer_object) {
3033 _mesa_error(ctx, GL_INVALID_ENUM,
3034 "%s(target=GL_UNIFORM_BUFFER)", caller);
3038 /* The ARB_multi_bind_spec says:
3040 * "An INVALID_OPERATION error is generated if <first> + <count> is
3041 * greater than the number of target-specific indexed binding points,
3042 * as described in section 6.7.1."
3044 if (first + count > ctx->Const.MaxUniformBufferBindings) {
3045 _mesa_error(ctx, GL_INVALID_OPERATION,
3046 "%s(first=%u + count=%d > the value of "
3047 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3048 caller, first, count,
3049 ctx->Const.MaxUniformBufferBindings);
3057 error_check_bind_shader_storage_buffers(struct gl_context *ctx,
3058 GLuint first, GLsizei count,
3061 if (!ctx->Extensions.ARB_shader_storage_buffer_object) {
3062 _mesa_error(ctx, GL_INVALID_ENUM,
3063 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller);
3067 /* The ARB_multi_bind_spec says:
3069 * "An INVALID_OPERATION error is generated if <first> + <count> is
3070 * greater than the number of target-specific indexed binding points,
3071 * as described in section 6.7.1."
3073 if (first + count > ctx->Const.MaxShaderStorageBufferBindings) {
3074 _mesa_error(ctx, GL_INVALID_OPERATION,
3075 "%s(first=%u + count=%d > the value of "
3076 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3077 caller, first, count,
3078 ctx->Const.MaxShaderStorageBufferBindings);
3086 * Unbind all uniform buffers in the range
3087 * <first> through <first>+<count>-1
3090 unbind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
3092 struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
3095 for (i = 0; i < count; i++)
3096 set_ubo_binding(ctx, &ctx->UniformBufferBindings[first + i],
3097 bufObj, -1, -1, GL_TRUE);
3101 * Unbind all shader storage buffers in the range
3102 * <first> through <first>+<count>-1
3105 unbind_shader_storage_buffers(struct gl_context *ctx, GLuint first,
3108 struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
3111 for (i = 0; i < count; i++)
3112 set_ssbo_binding(ctx, &ctx->ShaderStorageBufferBindings[first + i],
3113 bufObj, -1, -1, GL_TRUE);
3117 bind_uniform_buffers_base(struct gl_context *ctx, GLuint first, GLsizei count,
3118 const GLuint *buffers)
3122 if (!error_check_bind_uniform_buffers(ctx, first, count, "glBindBuffersBase"))
3125 /* Assume that at least one binding will be changed */
3126 FLUSH_VERTICES(ctx, 0);
3127 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
3130 /* The ARB_multi_bind spec says:
3132 * "If <buffers> is NULL, all bindings from <first> through
3133 * <first>+<count>-1 are reset to their unbound (zero) state."
3135 unbind_uniform_buffers(ctx, first, count);
3139 /* Note that the error semantics for multi-bind commands differ from
3140 * those of other GL commands.
3142 * The Issues section in the ARB_multi_bind spec says:
3144 * "(11) Typically, OpenGL specifies that if an error is generated by a
3145 * command, that command has no effect. This is somewhat
3146 * unfortunate for multi-bind commands, because it would require a
3147 * first pass to scan the entire list of bound objects for errors
3148 * and then a second pass to actually perform the bindings.
3149 * Should we have different error semantics?
3151 * RESOLVED: Yes. In this specification, when the parameters for
3152 * one of the <count> binding points are invalid, that binding point
3153 * is not updated and an error will be generated. However, other
3154 * binding points in the same command will be updated if their
3155 * parameters are valid and no other error occurs."
3158 _mesa_begin_bufferobj_lookups(ctx);
3160 for (i = 0; i < count; i++) {
3161 struct gl_uniform_buffer_binding *binding =
3162 &ctx->UniformBufferBindings[first + i];
3163 struct gl_buffer_object *bufObj;
3165 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3166 bufObj = binding->BufferObject;
3168 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3169 "glBindBuffersBase");
3172 if (bufObj == ctx->Shared->NullBufferObj)
3173 set_ubo_binding(ctx, binding, bufObj, -1, -1, GL_TRUE);
3175 set_ubo_binding(ctx, binding, bufObj, 0, 0, GL_TRUE);
3179 _mesa_end_bufferobj_lookups(ctx);
3183 bind_shader_storage_buffers_base(struct gl_context *ctx, GLuint first,
3184 GLsizei count, const GLuint *buffers)
3188 if (!error_check_bind_shader_storage_buffers(ctx, first, count,
3189 "glBindBuffersBase"))
3192 /* Assume that at least one binding will be changed */
3193 FLUSH_VERTICES(ctx, 0);
3194 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
3197 /* The ARB_multi_bind spec says:
3199 * "If <buffers> is NULL, all bindings from <first> through
3200 * <first>+<count>-1 are reset to their unbound (zero) state."
3202 unbind_shader_storage_buffers(ctx, first, count);
3206 /* Note that the error semantics for multi-bind commands differ from
3207 * those of other GL commands.
3209 * The Issues section in the ARB_multi_bind spec says:
3211 * "(11) Typically, OpenGL specifies that if an error is generated by a
3212 * command, that command has no effect. This is somewhat
3213 * unfortunate for multi-bind commands, because it would require a
3214 * first pass to scan the entire list of bound objects for errors
3215 * and then a second pass to actually perform the bindings.
3216 * Should we have different error semantics?
3218 * RESOLVED: Yes. In this specification, when the parameters for
3219 * one of the <count> binding points are invalid, that binding point
3220 * is not updated and an error will be generated. However, other
3221 * binding points in the same command will be updated if their
3222 * parameters are valid and no other error occurs."
3225 _mesa_begin_bufferobj_lookups(ctx);
3227 for (i = 0; i < count; i++) {
3228 struct gl_shader_storage_buffer_binding *binding =
3229 &ctx->ShaderStorageBufferBindings[first + i];
3230 struct gl_buffer_object *bufObj;
3232 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3233 bufObj = binding->BufferObject;
3235 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3236 "glBindBuffersBase");
3239 if (bufObj == ctx->Shared->NullBufferObj)
3240 set_ssbo_binding(ctx, binding, bufObj, -1, -1, GL_TRUE);
3242 set_ssbo_binding(ctx, binding, bufObj, 0, 0, GL_TRUE);
3246 _mesa_end_bufferobj_lookups(ctx);
3250 bind_uniform_buffers_range(struct gl_context *ctx, GLuint first, GLsizei count,
3251 const GLuint *buffers,
3252 const GLintptr *offsets, const GLsizeiptr *sizes)
3256 if (!error_check_bind_uniform_buffers(ctx, first, count,
3257 "glBindBuffersRange"))
3260 /* Assume that at least one binding will be changed */
3261 FLUSH_VERTICES(ctx, 0);
3262 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
3265 /* The ARB_multi_bind spec says:
3267 * "If <buffers> is NULL, all bindings from <first> through
3268 * <first>+<count>-1 are reset to their unbound (zero) state.
3269 * In this case, the offsets and sizes associated with the
3270 * binding points are set to default values, ignoring
3271 * <offsets> and <sizes>."
3273 unbind_uniform_buffers(ctx, first, count);
3277 /* Note that the error semantics for multi-bind commands differ from
3278 * those of other GL commands.
3280 * The Issues section in the ARB_multi_bind spec says:
3282 * "(11) Typically, OpenGL specifies that if an error is generated by a
3283 * command, that command has no effect. This is somewhat
3284 * unfortunate for multi-bind commands, because it would require a
3285 * first pass to scan the entire list of bound objects for errors
3286 * and then a second pass to actually perform the bindings.
3287 * Should we have different error semantics?
3289 * RESOLVED: Yes. In this specification, when the parameters for
3290 * one of the <count> binding points are invalid, that binding point
3291 * is not updated and an error will be generated. However, other
3292 * binding points in the same command will be updated if their
3293 * parameters are valid and no other error occurs."
3296 _mesa_begin_bufferobj_lookups(ctx);
3298 for (i = 0; i < count; i++) {
3299 struct gl_uniform_buffer_binding *binding =
3300 &ctx->UniformBufferBindings[first + i];
3301 struct gl_buffer_object *bufObj;
3303 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3306 /* The ARB_multi_bind spec says:
3308 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3309 * pair of values in <offsets> and <sizes> does not respectively
3310 * satisfy the constraints described for those parameters for the
3311 * specified target, as described in section 6.7.1 (per binding)."
3313 * Section 6.7.1 refers to table 6.5, which says:
3315 * "┌───────────────────────────────────────────────────────────────┐
3316 * │ Uniform buffer array bindings (see sec. 7.6) │
3317 * ├─────────────────────┬─────────────────────────────────────────┤
3319 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3320 * │ │ OFFSET_ALIGNMENT │
3322 * │ size restriction │ none │
3323 * └─────────────────────┴─────────────────────────────────────────┘"
3325 if (offsets[i] & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
3326 _mesa_error(ctx, GL_INVALID_VALUE,
3327 "glBindBuffersRange(offsets[%u]=%" PRId64
3328 " is misaligned; it must be a multiple of the value of "
3329 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3330 "target=GL_UNIFORM_BUFFER)",
3331 i, (int64_t) offsets[i],
3332 ctx->Const.UniformBufferOffsetAlignment);
3336 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3337 bufObj = binding->BufferObject;
3339 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3340 "glBindBuffersRange");
3343 if (bufObj == ctx->Shared->NullBufferObj)
3344 set_ubo_binding(ctx, binding, bufObj, -1, -1, GL_FALSE);
3346 set_ubo_binding(ctx, binding, bufObj,
3347 offsets[i], sizes[i], GL_FALSE);
3351 _mesa_end_bufferobj_lookups(ctx);
3355 bind_shader_storage_buffers_range(struct gl_context *ctx, GLuint first,
3356 GLsizei count, const GLuint *buffers,
3357 const GLintptr *offsets,
3358 const GLsizeiptr *sizes)
3362 if (!error_check_bind_shader_storage_buffers(ctx, first, count,
3363 "glBindBuffersRange"))
3366 /* Assume that at least one binding will be changed */
3367 FLUSH_VERTICES(ctx, 0);
3368 ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer;
3371 /* The ARB_multi_bind spec says:
3373 * "If <buffers> is NULL, all bindings from <first> through
3374 * <first>+<count>-1 are reset to their unbound (zero) state.
3375 * In this case, the offsets and sizes associated with the
3376 * binding points are set to default values, ignoring
3377 * <offsets> and <sizes>."
3379 unbind_shader_storage_buffers(ctx, first, count);
3383 /* Note that the error semantics for multi-bind commands differ from
3384 * those of other GL commands.
3386 * The Issues section in the ARB_multi_bind spec says:
3388 * "(11) Typically, OpenGL specifies that if an error is generated by a
3389 * command, that command has no effect. This is somewhat
3390 * unfortunate for multi-bind commands, because it would require a
3391 * first pass to scan the entire list of bound objects for errors
3392 * and then a second pass to actually perform the bindings.
3393 * Should we have different error semantics?
3395 * RESOLVED: Yes. In this specification, when the parameters for
3396 * one of the <count> binding points are invalid, that binding point
3397 * is not updated and an error will be generated. However, other
3398 * binding points in the same command will be updated if their
3399 * parameters are valid and no other error occurs."
3402 _mesa_begin_bufferobj_lookups(ctx);
3404 for (i = 0; i < count; i++) {
3405 struct gl_shader_storage_buffer_binding *binding =
3406 &ctx->ShaderStorageBufferBindings[first + i];
3407 struct gl_buffer_object *bufObj;
3409 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3412 /* The ARB_multi_bind spec says:
3414 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3415 * pair of values in <offsets> and <sizes> does not respectively
3416 * satisfy the constraints described for those parameters for the
3417 * specified target, as described in section 6.7.1 (per binding)."
3419 * Section 6.7.1 refers to table 6.5, which says:
3421 * "┌───────────────────────────────────────────────────────────────┐
3422 * │ Shader storage buffer array bindings (see sec. 7.8) │
3423 * ├─────────────────────┬─────────────────────────────────────────┤
3425 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3426 * │ │ BUFFER_OFFSET_ALIGNMENT │
3428 * │ size restriction │ none │
3429 * └─────────────────────┴─────────────────────────────────────────┘"
3431 if (offsets[i] & (ctx->Const.ShaderStorageBufferOffsetAlignment - 1)) {
3432 _mesa_error(ctx, GL_INVALID_VALUE,
3433 "glBindBuffersRange(offsets[%u]=%" PRId64
3434 " is misaligned; it must be a multiple of the value of "
3435 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3436 "target=GL_SHADER_STORAGE_BUFFER)",
3437 i, (int64_t) offsets[i],
3438 ctx->Const.ShaderStorageBufferOffsetAlignment);
3442 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3443 bufObj = binding->BufferObject;
3445 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3446 "glBindBuffersRange");
3449 if (bufObj == ctx->Shared->NullBufferObj)
3450 set_ssbo_binding(ctx, binding, bufObj, -1, -1, GL_FALSE);
3452 set_ssbo_binding(ctx, binding, bufObj,
3453 offsets[i], sizes[i], GL_FALSE);
3457 _mesa_end_bufferobj_lookups(ctx);
3461 error_check_bind_xfb_buffers(struct gl_context *ctx,
3462 struct gl_transform_feedback_object *tfObj,
3463 GLuint first, GLsizei count, const char *caller)
3465 if (!ctx->Extensions.EXT_transform_feedback) {
3466 _mesa_error(ctx, GL_INVALID_ENUM,
3467 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller);
3471 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3473 * "An INVALID_OPERATION error is generated :
3476 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3477 * FEEDBACK_BUFFER and transform feedback is currently active."
3479 * We assume that this is also meant to apply to BindBuffersRange
3480 * and BindBuffersBase.
3482 if (tfObj->Active) {
3483 _mesa_error(ctx, GL_INVALID_OPERATION,
3484 "%s(Changing transform feedback buffers while "
3485 "transform feedback is active)", caller);
3489 /* The ARB_multi_bind_spec says:
3491 * "An INVALID_OPERATION error is generated if <first> + <count> is
3492 * greater than the number of target-specific indexed binding points,
3493 * as described in section 6.7.1."
3495 if (first + count > ctx->Const.MaxTransformFeedbackBuffers) {
3496 _mesa_error(ctx, GL_INVALID_OPERATION,
3497 "%s(first=%u + count=%d > the value of "
3498 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3499 caller, first, count,
3500 ctx->Const.MaxTransformFeedbackBuffers);
3508 * Unbind all transform feedback buffers in the range
3509 * <first> through <first>+<count>-1
3512 unbind_xfb_buffers(struct gl_context *ctx,
3513 struct gl_transform_feedback_object *tfObj,
3514 GLuint first, GLsizei count)
3516 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
3519 for (i = 0; i < count; i++)
3520 _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
3525 bind_xfb_buffers_base(struct gl_context *ctx,
3526 GLuint first, GLsizei count,
3527 const GLuint *buffers)
3529 struct gl_transform_feedback_object *tfObj =
3530 ctx->TransformFeedback.CurrentObject;
3533 if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count,
3534 "glBindBuffersBase"))
3537 /* Assume that at least one binding will be changed */
3538 FLUSH_VERTICES(ctx, 0);
3539 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
3542 /* The ARB_multi_bind spec says:
3544 * "If <buffers> is NULL, all bindings from <first> through
3545 * <first>+<count>-1 are reset to their unbound (zero) state."
3547 unbind_xfb_buffers(ctx, tfObj, first, count);
3551 /* Note that the error semantics for multi-bind commands differ from
3552 * those of other GL commands.
3554 * The Issues section in the ARB_multi_bind spec says:
3556 * "(11) Typically, OpenGL specifies that if an error is generated by a
3557 * command, that command has no effect. This is somewhat
3558 * unfortunate for multi-bind commands, because it would require a
3559 * first pass to scan the entire list of bound objects for errors
3560 * and then a second pass to actually perform the bindings.
3561 * Should we have different error semantics?
3563 * RESOLVED: Yes. In this specification, when the parameters for
3564 * one of the <count> binding points are invalid, that binding point
3565 * is not updated and an error will be generated. However, other
3566 * binding points in the same command will be updated if their
3567 * parameters are valid and no other error occurs."
3570 _mesa_begin_bufferobj_lookups(ctx);
3572 for (i = 0; i < count; i++) {
3573 struct gl_buffer_object * const boundBufObj = tfObj->Buffers[first + i];
3574 struct gl_buffer_object *bufObj;
3576 if (boundBufObj && boundBufObj->Name == buffers[i])
3577 bufObj = boundBufObj;
3579 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3580 "glBindBuffersBase");
3583 _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
3587 _mesa_end_bufferobj_lookups(ctx);
3591 bind_xfb_buffers_range(struct gl_context *ctx,
3592 GLuint first, GLsizei count,
3593 const GLuint *buffers,
3594 const GLintptr *offsets,
3595 const GLsizeiptr *sizes)
3597 struct gl_transform_feedback_object *tfObj =
3598 ctx->TransformFeedback.CurrentObject;
3601 if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count,
3602 "glBindBuffersRange"))
3605 /* Assume that at least one binding will be changed */
3606 FLUSH_VERTICES(ctx, 0);
3607 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
3610 /* The ARB_multi_bind spec says:
3612 * "If <buffers> is NULL, all bindings from <first> through
3613 * <first>+<count>-1 are reset to their unbound (zero) state.
3614 * In this case, the offsets and sizes associated with the
3615 * binding points are set to default values, ignoring
3616 * <offsets> and <sizes>."
3618 unbind_xfb_buffers(ctx, tfObj, first, count);
3622 /* Note that the error semantics for multi-bind commands differ from
3623 * those of other GL commands.
3625 * The Issues section in the ARB_multi_bind spec says:
3627 * "(11) Typically, OpenGL specifies that if an error is generated by a
3628 * command, that command has no effect. This is somewhat
3629 * unfortunate for multi-bind commands, because it would require a
3630 * first pass to scan the entire list of bound objects for errors
3631 * and then a second pass to actually perform the bindings.
3632 * Should we have different error semantics?
3634 * RESOLVED: Yes. In this specification, when the parameters for
3635 * one of the <count> binding points are invalid, that binding point
3636 * is not updated and an error will be generated. However, other
3637 * binding points in the same command will be updated if their
3638 * parameters are valid and no other error occurs."
3641 _mesa_begin_bufferobj_lookups(ctx);
3643 for (i = 0; i < count; i++) {
3644 const GLuint index = first + i;
3645 struct gl_buffer_object * const boundBufObj = tfObj->Buffers[index];
3646 struct gl_buffer_object *bufObj;
3648 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3651 /* The ARB_multi_bind spec says:
3653 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3654 * pair of values in <offsets> and <sizes> does not respectively
3655 * satisfy the constraints described for those parameters for the
3656 * specified target, as described in section 6.7.1 (per binding)."
3658 * Section 6.7.1 refers to table 6.5, which says:
3660 * "┌───────────────────────────────────────────────────────────────┐
3661 * │ Transform feedback array bindings (see sec. 13.2.2) │
3662 * ├───────────────────────┬───────────────────────────────────────┤
3664 * │ offset restriction │ multiple of 4 │
3666 * │ size restriction │ multiple of 4 │
3667 * └───────────────────────┴───────────────────────────────────────┘"
3669 if (offsets[i] & 0x3) {
3670 _mesa_error(ctx, GL_INVALID_VALUE,
3671 "glBindBuffersRange(offsets[%u]=%" PRId64
3672 " is misaligned; it must be a multiple of 4 when "
3673 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3674 i, (int64_t) offsets[i]);
3678 if (sizes[i] & 0x3) {
3679 _mesa_error(ctx, GL_INVALID_VALUE,
3680 "glBindBuffersRange(sizes[%u]=%" PRId64
3681 " is misaligned; it must be a multiple of 4 when "
3682 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3683 i, (int64_t) sizes[i]);
3687 if (boundBufObj && boundBufObj->Name == buffers[i])
3688 bufObj = boundBufObj;
3690 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3691 "glBindBuffersRange");
3694 _mesa_set_transform_feedback_binding(ctx, tfObj, index, bufObj,
3695 offsets[i], sizes[i]);
3698 _mesa_end_bufferobj_lookups(ctx);
3702 error_check_bind_atomic_buffers(struct gl_context *ctx,
3703 GLuint first, GLsizei count,
3706 if (!ctx->Extensions.ARB_shader_atomic_counters) {
3707 _mesa_error(ctx, GL_INVALID_ENUM,
3708 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller);
3712 /* The ARB_multi_bind_spec says:
3714 * "An INVALID_OPERATION error is generated if <first> + <count> is
3715 * greater than the number of target-specific indexed binding points,
3716 * as described in section 6.7.1."
3718 if (first + count > ctx->Const.MaxAtomicBufferBindings) {
3719 _mesa_error(ctx, GL_INVALID_OPERATION,
3720 "%s(first=%u + count=%d > the value of "
3721 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3722 caller, first, count, ctx->Const.MaxAtomicBufferBindings);
3730 * Unbind all atomic counter buffers in the range
3731 * <first> through <first>+<count>-1
3734 unbind_atomic_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
3736 struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
3739 for (i = 0; i < count; i++)
3740 set_atomic_buffer_binding(ctx, &ctx->AtomicBufferBindings[first + i],
3745 bind_atomic_buffers_base(struct gl_context *ctx,
3748 const GLuint *buffers)
3752 if (!error_check_bind_atomic_buffers(ctx, first, count,
3753 "glBindBuffersBase"))
3756 /* Assume that at least one binding will be changed */
3757 FLUSH_VERTICES(ctx, 0);
3758 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
3761 /* The ARB_multi_bind spec says:
3763 * "If <buffers> is NULL, all bindings from <first> through
3764 * <first>+<count>-1 are reset to their unbound (zero) state."
3766 unbind_atomic_buffers(ctx, first, count);
3770 /* Note that the error semantics for multi-bind commands differ from
3771 * those of other GL commands.
3773 * The Issues section in the ARB_multi_bind spec says:
3775 * "(11) Typically, OpenGL specifies that if an error is generated by a
3776 * command, that command has no effect. This is somewhat
3777 * unfortunate for multi-bind commands, because it would require a
3778 * first pass to scan the entire list of bound objects for errors
3779 * and then a second pass to actually perform the bindings.
3780 * Should we have different error semantics?
3782 * RESOLVED: Yes. In this specification, when the parameters for
3783 * one of the <count> binding points are invalid, that binding point
3784 * is not updated and an error will be generated. However, other
3785 * binding points in the same command will be updated if their
3786 * parameters are valid and no other error occurs."
3789 _mesa_begin_bufferobj_lookups(ctx);
3791 for (i = 0; i < count; i++) {
3792 struct gl_atomic_buffer_binding *binding =
3793 &ctx->AtomicBufferBindings[first + i];
3794 struct gl_buffer_object *bufObj;
3796 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3797 bufObj = binding->BufferObject;
3799 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3800 "glBindBuffersBase");
3803 set_atomic_buffer_binding(ctx, binding, bufObj, 0, 0);
3806 _mesa_end_bufferobj_lookups(ctx);
3810 bind_atomic_buffers_range(struct gl_context *ctx,
3813 const GLuint *buffers,
3814 const GLintptr *offsets,
3815 const GLsizeiptr *sizes)
3819 if (!error_check_bind_atomic_buffers(ctx, first, count,
3820 "glBindBuffersRange"))
3823 /* Assume that at least one binding will be changed */
3824 FLUSH_VERTICES(ctx, 0);
3825 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
3828 /* The ARB_multi_bind spec says:
3830 * "If <buffers> is NULL, all bindings from <first> through
3831 * <first>+<count>-1 are reset to their unbound (zero) state.
3832 * In this case, the offsets and sizes associated with the
3833 * binding points are set to default values, ignoring
3834 * <offsets> and <sizes>."
3836 unbind_atomic_buffers(ctx, first, count);
3840 /* Note that the error semantics for multi-bind commands differ from
3841 * those of other GL commands.
3843 * The Issues section in the ARB_multi_bind spec says:
3845 * "(11) Typically, OpenGL specifies that if an error is generated by a
3846 * command, that command has no effect. This is somewhat
3847 * unfortunate for multi-bind commands, because it would require a
3848 * first pass to scan the entire list of bound objects for errors
3849 * and then a second pass to actually perform the bindings.
3850 * Should we have different error semantics?
3852 * RESOLVED: Yes. In this specification, when the parameters for
3853 * one of the <count> binding points are invalid, that binding point
3854 * is not updated and an error will be generated. However, other
3855 * binding points in the same command will be updated if their
3856 * parameters are valid and no other error occurs."
3859 _mesa_begin_bufferobj_lookups(ctx);
3861 for (i = 0; i < count; i++) {
3862 struct gl_atomic_buffer_binding *binding =
3863 &ctx->AtomicBufferBindings[first + i];
3864 struct gl_buffer_object *bufObj;
3866 if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
3869 /* The ARB_multi_bind spec says:
3871 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3872 * pair of values in <offsets> and <sizes> does not respectively
3873 * satisfy the constraints described for those parameters for the
3874 * specified target, as described in section 6.7.1 (per binding)."
3876 * Section 6.7.1 refers to table 6.5, which says:
3878 * "┌───────────────────────────────────────────────────────────────┐
3879 * │ Atomic counter array bindings (see sec. 7.7.2) │
3880 * ├───────────────────────┬───────────────────────────────────────┤
3882 * │ offset restriction │ multiple of 4 │
3884 * │ size restriction │ none │
3885 * └───────────────────────┴───────────────────────────────────────┘"
3887 if (offsets[i] & (ATOMIC_COUNTER_SIZE - 1)) {
3888 _mesa_error(ctx, GL_INVALID_VALUE,
3889 "glBindBuffersRange(offsets[%u]=%" PRId64
3890 " is misaligned; it must be a multiple of %d when "
3891 "target=GL_ATOMIC_COUNTER_BUFFER)",
3892 i, (int64_t) offsets[i], ATOMIC_COUNTER_SIZE);
3896 if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
3897 bufObj = binding->BufferObject;
3899 bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
3900 "glBindBuffersRange");
3903 set_atomic_buffer_binding(ctx, binding, bufObj, offsets[i], sizes[i]);
3906 _mesa_end_bufferobj_lookups(ctx);
3910 _mesa_BindBufferRange(GLenum target, GLuint index,
3911 GLuint buffer, GLintptr offset, GLsizeiptr size)
3913 GET_CURRENT_CONTEXT(ctx);
3914 struct gl_buffer_object *bufObj;
3916 if (MESA_VERBOSE & VERBOSE_API) {
3917 _mesa_debug(ctx, "glBindBufferRange(%s, %u, %u, %ld, %ld)\n",
3918 _mesa_enum_to_string(target), index, buffer, offset, size);
3922 bufObj = ctx->Shared->NullBufferObj;
3924 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3926 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
3927 &bufObj, "glBindBufferRange"))
3931 _mesa_error(ctx, GL_INVALID_OPERATION,
3932 "glBindBufferRange(invalid buffer=%u)", buffer);
3938 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
3945 case GL_TRANSFORM_FEEDBACK_BUFFER:
3946 _mesa_bind_buffer_range_transform_feedback(ctx,
3947 ctx->TransformFeedback.CurrentObject,
3948 index, bufObj, offset, size,
3951 case GL_UNIFORM_BUFFER:
3952 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
3954 case GL_SHADER_STORAGE_BUFFER:
3955 bind_buffer_range_shader_storage_buffer(ctx, index, bufObj, offset, size);
3957 case GL_ATOMIC_COUNTER_BUFFER:
3958 bind_atomic_buffer(ctx, index, bufObj, offset, size,
3959 "glBindBufferRange");
3962 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
3968 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
3970 GET_CURRENT_CONTEXT(ctx);
3971 struct gl_buffer_object *bufObj;
3973 if (MESA_VERBOSE & VERBOSE_API) {
3974 _mesa_debug(ctx, "glBindBufferBase(%s, %u, %u)\n",
3975 _mesa_enum_to_string(target), index, buffer);
3979 bufObj = ctx->Shared->NullBufferObj;
3981 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
3983 if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
3984 &bufObj, "glBindBufferBase"))
3988 _mesa_error(ctx, GL_INVALID_OPERATION,
3989 "glBindBufferBase(invalid buffer=%u)", buffer);
3993 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3994 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3996 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3997 * zero and size equal to the size of buffer."
3999 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4001 * "If the parameter (starting offset or size) was not specified when the
4002 * buffer object was bound, zero is returned."
4004 * What happens if the size of the buffer changes? Does the size of the
4005 * buffer at the moment glBindBufferBase was called still play a role, like
4006 * the first quote would imply, or is the size meaningless in the
4007 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4008 * core spec page 45 says:
4010 * "It is equivalent to calling BindBufferRange with offset zero, while
4011 * size is determined by the size of the bound buffer at the time the
4014 * My interpretation is that the GL 4.1 spec was a clarification of the
4015 * behavior, not a change. In particular, this choice will only make
4016 * rendering work in cases where it would have had undefined results.
4020 case GL_TRANSFORM_FEEDBACK_BUFFER:
4021 _mesa_bind_buffer_base_transform_feedback(ctx,
4022 ctx->TransformFeedback.CurrentObject,
4023 index, bufObj, false);
4025 case GL_UNIFORM_BUFFER:
4026 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
4028 case GL_SHADER_STORAGE_BUFFER:
4029 bind_buffer_base_shader_storage_buffer(ctx, index, bufObj);
4031 case GL_ATOMIC_COUNTER_BUFFER:
4032 bind_atomic_buffer(ctx, index, bufObj, 0, 0,
4033 "glBindBufferBase");
4036 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
4042 _mesa_BindBuffersRange(GLenum target, GLuint first, GLsizei count,
4043 const GLuint *buffers,
4044 const GLintptr *offsets, const GLsizeiptr *sizes)
4046 GET_CURRENT_CONTEXT(ctx);
4048 if (MESA_VERBOSE & VERBOSE_API) {
4049 _mesa_debug(ctx, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4050 _mesa_enum_to_string(target), first, count,
4051 buffers, offsets, sizes);
4055 case GL_TRANSFORM_FEEDBACK_BUFFER:
4056 bind_xfb_buffers_range(ctx, first, count, buffers, offsets, sizes);
4058 case GL_UNIFORM_BUFFER:
4059 bind_uniform_buffers_range(ctx, first, count, buffers, offsets, sizes);
4061 case GL_SHADER_STORAGE_BUFFER:
4062 bind_shader_storage_buffers_range(ctx, first, count, buffers, offsets,
4065 case GL_ATOMIC_COUNTER_BUFFER:
4066 bind_atomic_buffers_range(ctx, first, count, buffers,
4070 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersRange(target=%s)",
4071 _mesa_enum_to_string(target));
4077 _mesa_BindBuffersBase(GLenum target, GLuint first, GLsizei count,
4078 const GLuint *buffers)
4080 GET_CURRENT_CONTEXT(ctx);
4082 if (MESA_VERBOSE & VERBOSE_API) {
4083 _mesa_debug(ctx, "glBindBuffersBase(%s, %u, %d, %p)\n",
4084 _mesa_enum_to_string(target), first, count, buffers);
4088 case GL_TRANSFORM_FEEDBACK_BUFFER:
4089 bind_xfb_buffers_base(ctx, first, count, buffers);
4091 case GL_UNIFORM_BUFFER:
4092 bind_uniform_buffers_base(ctx, first, count, buffers);
4094 case GL_SHADER_STORAGE_BUFFER:
4095 bind_shader_storage_buffers_base(ctx, first, count, buffers);
4097 case GL_ATOMIC_COUNTER_BUFFER:
4098 bind_atomic_buffers_base(ctx, first, count, buffers);
4101 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersBase(target=%s)",
4102 _mesa_enum_to_string(target));
4108 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
4111 GET_CURRENT_CONTEXT(ctx);
4112 struct gl_buffer_object *bufObj;
4113 const GLintptr end = offset + length;
4115 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4117 _mesa_error(ctx, GL_INVALID_VALUE,
4118 "glInvalidateBufferSubData(name = 0x%x) invalid object",
4123 /* The GL_ARB_invalidate_subdata spec says:
4125 * "An INVALID_VALUE error is generated if <offset> or <length> is
4126 * negative, or if <offset> + <length> is greater than the value of
4129 if (end < 0 || end > bufObj->Size) {
4130 _mesa_error(ctx, GL_INVALID_VALUE,
4131 "glInvalidateBufferSubData(invalid offset or length)");
4135 /* The OpenGL 4.4 (Core Profile) spec says:
4137 * "An INVALID_OPERATION error is generated if buffer is currently
4138 * mapped by MapBuffer or if the invalidate range intersects the range
4139 * currently mapped by MapBufferRange, unless it was mapped
4140 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4142 if (!(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT) &&
4143 bufferobj_range_mapped(bufObj, offset, length)) {
4144 _mesa_error(ctx, GL_INVALID_OPERATION,
4145 "glInvalidateBufferSubData(intersection with mapped "
4150 /* We don't actually do anything for this yet. Just return after
4151 * validating the parameters and generating the required errors.
4157 _mesa_InvalidateBufferData(GLuint buffer)
4159 GET_CURRENT_CONTEXT(ctx);
4160 struct gl_buffer_object *bufObj;
4162 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4164 _mesa_error(ctx, GL_INVALID_VALUE,
4165 "glInvalidateBufferData(name = 0x%x) invalid object",
4170 /* The OpenGL 4.4 (Core Profile) spec says:
4172 * "An INVALID_OPERATION error is generated if buffer is currently
4173 * mapped by MapBuffer or if the invalidate range intersects the range
4174 * currently mapped by MapBufferRange, unless it was mapped
4175 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4177 if (_mesa_check_disallowed_mapping(bufObj)) {
4178 _mesa_error(ctx, GL_INVALID_OPERATION,
4179 "glInvalidateBufferData(intersection with mapped "
4184 /* We don't actually do anything for this yet. Just return after
4185 * validating the parameters and generating the required errors.