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
);
1212 bind_buffer_object(ctx
, target
, buffer
);
1217 * Delete a set of buffer objects.
1219 * \param n Number of buffer objects to delete.
1220 * \param ids Array of \c n buffer object IDs.
1223 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1225 GET_CURRENT_CONTEXT(ctx
);
1227 FLUSH_VERTICES(ctx
, 0);
1230 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1234 mtx_lock(&ctx
->Shared
->Mutex
);
1236 for (i
= 0; i
< n
; i
++) {
1237 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
1239 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1242 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1244 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1246 /* unbind any vertex pointers bound to this buffer */
1247 for (j
= 0; j
< ARRAY_SIZE(vao
->VertexBinding
); j
++) {
1248 unbind(ctx
, &vao
->VertexBinding
[j
].BufferObj
, bufObj
);
1251 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1252 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
1254 if (vao
->IndexBufferObj
== bufObj
) {
1255 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1258 /* unbind ARB_draw_indirect binding point */
1259 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1260 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER
, 0 );
1263 /* unbind ARB_compute_shader binding point */
1264 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1265 _mesa_BindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, 0);
1268 /* unbind ARB_copy_buffer binding points */
1269 if (ctx
->CopyReadBuffer
== bufObj
) {
1270 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
1272 if (ctx
->CopyWriteBuffer
== bufObj
) {
1273 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
1276 /* unbind transform feedback binding points */
1277 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1278 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
1280 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1281 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1282 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1286 /* unbind UBO binding points */
1287 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1288 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1289 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1293 if (ctx
->UniformBuffer
== bufObj
) {
1294 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
1297 /* unbind SSBO binding points */
1298 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1299 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1300 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER
, j
, 0);
1304 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1305 _mesa_BindBuffer(GL_SHADER_STORAGE_BUFFER
, 0);
1308 /* unbind Atomci Buffer binding points */
1309 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1310 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1311 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1315 if (ctx
->AtomicBuffer
== bufObj
) {
1316 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER
, 0 );
1319 /* unbind any pixel pack/unpack pointers bound to this buffer */
1320 if (ctx
->Pack
.BufferObj
== bufObj
) {
1321 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1323 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1324 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1327 if (ctx
->Texture
.BufferObject
== bufObj
) {
1328 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
1331 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1332 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
, 0);
1335 /* The ID is immediately freed for re-use */
1336 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
1337 /* Make sure we do not run into the classic ABA problem on bind.
1338 * We don't want to allow re-binding a buffer object that's been
1339 * "deleted" by glDeleteBuffers().
1341 * The explicit rebinding to the default object in the current context
1342 * prevents the above in the current context, but another context
1343 * sharing the same objects might suffer from this problem.
1344 * The alternative would be to do the hash lookup in any case on bind
1345 * which would introduce more runtime overhead than this.
1347 bufObj
->DeletePending
= GL_TRUE
;
1348 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1352 mtx_unlock(&ctx
->Shared
->Mutex
);
1357 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1358 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1362 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1364 GET_CURRENT_CONTEXT(ctx
);
1367 struct gl_buffer_object
*buf
;
1369 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1371 if (MESA_VERBOSE
& VERBOSE_API
)
1372 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1375 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1384 * This must be atomic (generation and allocation of buffer object IDs)
1386 mtx_lock(&ctx
->Shared
->Mutex
);
1388 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1390 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1391 * DummyBufferObject. Otherwise, create a new buffer object and insert
1394 for (i
= 0; i
< n
; i
++) {
1395 buffers
[i
] = first
+ i
;
1397 assert(ctx
->Driver
.NewBufferObject
);
1398 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1400 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1401 mtx_unlock(&ctx
->Shared
->Mutex
);
1406 buf
= &DummyBufferObject
;
1408 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1411 mtx_unlock(&ctx
->Shared
->Mutex
);
1415 * Generate a set of unique buffer object IDs and store them in \c buffers.
1417 * \param n Number of IDs to generate.
1418 * \param buffers Array of \c n locations to store the IDs.
1421 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1423 create_buffers(n
, buffers
, false);
1427 * Create a set of buffer objects and store their unique IDs in \c buffers.
1429 * \param n Number of IDs to generate.
1430 * \param buffers Array of \c n locations to store the IDs.
1433 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1435 create_buffers(n
, buffers
, true);
1440 * Determine if ID is the name of a buffer object.
1442 * \param id ID of the potential buffer object.
1443 * \return \c GL_TRUE if \c id is the name of a buffer object,
1444 * \c GL_FALSE otherwise.
1446 GLboolean GLAPIENTRY
1447 _mesa_IsBuffer(GLuint id
)
1449 struct gl_buffer_object
*bufObj
;
1450 GET_CURRENT_CONTEXT(ctx
);
1451 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1453 mtx_lock(&ctx
->Shared
->Mutex
);
1454 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1455 mtx_unlock(&ctx
->Shared
->Mutex
);
1457 return bufObj
&& bufObj
!= &DummyBufferObject
;
1462 _mesa_buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1463 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1464 GLbitfield flags
, const char *func
)
1467 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1471 if (flags
& ~(GL_MAP_READ_BIT
|
1473 GL_MAP_PERSISTENT_BIT
|
1474 GL_MAP_COHERENT_BIT
|
1475 GL_DYNAMIC_STORAGE_BIT
|
1476 GL_CLIENT_STORAGE_BIT
)) {
1477 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1481 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1482 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1483 _mesa_error(ctx
, GL_INVALID_VALUE
,
1484 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1488 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1489 _mesa_error(ctx
, GL_INVALID_VALUE
,
1490 "%s(COHERENT and flags!=PERSISTENT)", func
);
1494 if (bufObj
->Immutable
) {
1495 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1499 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1500 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1502 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1504 bufObj
->Written
= GL_TRUE
;
1505 bufObj
->Immutable
= GL_TRUE
;
1507 assert(ctx
->Driver
.BufferData
);
1508 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1510 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1511 /* Even though the interaction between AMD_pinned_memory and
1512 * glBufferStorage is not described in the spec, Graham Sellers
1513 * said that it should behave the same as glBufferData.
1515 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1518 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1524 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1527 GET_CURRENT_CONTEXT(ctx
);
1528 struct gl_buffer_object
*bufObj
;
1530 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1534 _mesa_buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
,
1539 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1542 GET_CURRENT_CONTEXT(ctx
);
1543 struct gl_buffer_object
*bufObj
;
1545 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferStorage");
1550 * In direct state access, buffer objects have an unspecified target since
1551 * they are not required to be bound.
1553 _mesa_buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
,
1554 "glNamedBufferStorage");
1559 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1560 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1561 GLenum usage
, const char *func
)
1565 if (MESA_VERBOSE
& VERBOSE_API
)
1566 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1568 _mesa_enum_to_string(target
),
1569 (long int) size
, data
,
1570 _mesa_enum_to_string(usage
));
1573 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1578 case GL_STREAM_DRAW_ARB
:
1579 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1582 case GL_STATIC_DRAW_ARB
:
1583 case GL_DYNAMIC_DRAW_ARB
:
1587 case GL_STREAM_READ_ARB
:
1588 case GL_STREAM_COPY_ARB
:
1589 case GL_STATIC_READ_ARB
:
1590 case GL_STATIC_COPY_ARB
:
1591 case GL_DYNAMIC_READ_ARB
:
1592 case GL_DYNAMIC_COPY_ARB
:
1593 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1597 valid_usage
= false;
1602 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1603 _mesa_enum_to_string(usage
));
1607 if (bufObj
->Immutable
) {
1608 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1612 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1613 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1615 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1617 bufObj
->Written
= GL_TRUE
;
1620 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1621 bufObj
->Name
, size
, data
, usage
);
1628 assert(ctx
->Driver
.BufferData
);
1629 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1632 GL_DYNAMIC_STORAGE_BIT
,
1634 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1635 /* From GL_AMD_pinned_memory:
1637 * INVALID_OPERATION is generated by BufferData if <target> is
1638 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1639 * mapped to the GPU address space.
1641 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1644 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1650 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1651 const GLvoid
*data
, GLenum usage
)
1653 GET_CURRENT_CONTEXT(ctx
);
1654 struct gl_buffer_object
*bufObj
;
1656 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1660 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1665 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1668 GET_CURRENT_CONTEXT(ctx
);
1669 struct gl_buffer_object
*bufObj
;
1671 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1675 /* In direct state access, buffer objects have an unspecified target since
1676 * they are not required to be bound.
1678 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1679 "glNamedBufferData");
1684 * Implementation for glBufferSubData and glNamedBufferSubData.
1686 * \param ctx GL context.
1687 * \param bufObj The buffer object.
1688 * \param offset Offset of the first byte of the subdata range.
1689 * \param size Size, in bytes, of the subdata range.
1690 * \param data The data store.
1691 * \param func Name of calling function for recording errors.
1695 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1696 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1699 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1701 /* error already recorded */
1705 if (bufObj
->Immutable
&&
1706 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1707 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1714 bufObj
->NumSubDataCalls
++;
1716 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
1717 bufObj
->Usage
== GL_STATIC_COPY
) &&
1718 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
) {
1719 /* If the application declared the buffer as static draw/copy or stream
1720 * draw, it should not be frequently modified with glBufferSubData.
1722 BUFFER_USAGE_WARNING(ctx
,
1723 "using %s(buffer %u, offset %u, size %u) to "
1724 "update a %s buffer",
1725 func
, bufObj
->Name
, offset
, size
,
1726 _mesa_enum_to_string(bufObj
->Usage
));
1729 bufObj
->Written
= GL_TRUE
;
1731 assert(ctx
->Driver
.BufferSubData
);
1732 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1736 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1737 GLsizeiptr size
, const GLvoid
*data
)
1739 GET_CURRENT_CONTEXT(ctx
);
1740 struct gl_buffer_object
*bufObj
;
1742 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1746 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1750 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1751 GLsizeiptr size
, const GLvoid
*data
)
1753 GET_CURRENT_CONTEXT(ctx
);
1754 struct gl_buffer_object
*bufObj
;
1756 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1760 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1761 "glNamedBufferSubData");
1766 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1767 GLsizeiptr size
, GLvoid
*data
)
1769 GET_CURRENT_CONTEXT(ctx
);
1770 struct gl_buffer_object
*bufObj
;
1772 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1773 GL_INVALID_OPERATION
);
1777 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1778 "glGetBufferSubData")) {
1782 assert(ctx
->Driver
.GetBufferSubData
);
1783 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1787 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
1788 GLsizeiptr size
, GLvoid
*data
)
1790 GET_CURRENT_CONTEXT(ctx
);
1791 struct gl_buffer_object
*bufObj
;
1793 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1794 "glGetNamedBufferSubData");
1798 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1799 "glGetNamedBufferSubData")) {
1803 assert(ctx
->Driver
.GetBufferSubData
);
1804 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1809 * \param subdata true if caller is *SubData, false if *Data
1812 _mesa_clear_buffer_sub_data(struct gl_context
*ctx
,
1813 struct gl_buffer_object
*bufObj
,
1814 GLenum internalformat
,
1815 GLintptr offset
, GLsizeiptr size
,
1816 GLenum format
, GLenum type
,
1818 const char *func
, bool subdata
)
1820 mesa_format mesaFormat
;
1821 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1822 GLsizeiptr clearValueSize
;
1824 /* This checks for disallowed mappings. */
1825 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1830 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1831 format
, type
, func
);
1833 if (mesaFormat
== MESA_FORMAT_NONE
) {
1837 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1838 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1839 _mesa_error(ctx
, GL_INVALID_VALUE
,
1840 "%s(offset or size is not a multiple of "
1841 "internalformat size)", func
);
1846 /* clear to zeros, per the spec */
1848 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1849 NULL
, clearValueSize
, bufObj
);
1854 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1855 format
, type
, data
, func
)) {
1860 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1861 clearValue
, clearValueSize
, bufObj
);
1866 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1867 GLenum type
, const GLvoid
*data
)
1869 GET_CURRENT_CONTEXT(ctx
);
1870 struct gl_buffer_object
*bufObj
;
1872 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1876 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1878 "glClearBufferData", false);
1882 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1883 GLenum format
, GLenum type
, const GLvoid
*data
)
1885 GET_CURRENT_CONTEXT(ctx
);
1886 struct gl_buffer_object
*bufObj
;
1888 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1892 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1894 "glClearNamedBufferData", false);
1899 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1900 GLintptr offset
, GLsizeiptr size
,
1901 GLenum format
, GLenum type
,
1904 GET_CURRENT_CONTEXT(ctx
);
1905 struct gl_buffer_object
*bufObj
;
1907 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1911 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1913 "glClearBufferSubData", true);
1917 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1918 GLintptr offset
, GLsizeiptr size
,
1919 GLenum format
, GLenum type
,
1922 GET_CURRENT_CONTEXT(ctx
);
1923 struct gl_buffer_object
*bufObj
;
1925 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1926 "glClearNamedBufferSubData");
1930 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1932 "glClearNamedBufferSubData", true);
1937 _mesa_unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1940 GLboolean status
= GL_TRUE
;
1941 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1943 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
1944 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1945 "%s(buffer is not mapped)", func
);
1950 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1951 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1953 /* check that last 100 bytes are still = magic value */
1954 for (i
= 0; i
< 100; i
++) {
1955 GLuint pos
= bufObj
->Size
- i
- 1;
1956 if (buf
[pos
] != 123) {
1957 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1958 " at position %d (value = %u)\n",
1966 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1967 GLuint i
, unchanged
= 0;
1968 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1970 /* check which bytes changed */
1971 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1972 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1979 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1980 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1985 status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
1986 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
1987 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
1988 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
1989 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
1994 GLboolean GLAPIENTRY
1995 _mesa_UnmapBuffer(GLenum target
)
1997 GET_CURRENT_CONTEXT(ctx
);
1998 struct gl_buffer_object
*bufObj
;
2000 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2004 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2007 GLboolean GLAPIENTRY
2008 _mesa_UnmapNamedBuffer(GLuint buffer
)
2010 GET_CURRENT_CONTEXT(ctx
);
2011 struct gl_buffer_object
*bufObj
;
2013 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2017 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2022 get_buffer_parameter(struct gl_context
*ctx
,
2023 struct gl_buffer_object
*bufObj
, GLenum pname
,
2024 GLint64
*params
, const char *func
)
2027 case GL_BUFFER_SIZE_ARB
:
2028 *params
= bufObj
->Size
;
2030 case GL_BUFFER_USAGE_ARB
:
2031 *params
= bufObj
->Usage
;
2033 case GL_BUFFER_ACCESS_ARB
:
2034 *params
= simplified_access_mode(ctx
,
2035 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2037 case GL_BUFFER_MAPPED_ARB
:
2038 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2040 case GL_BUFFER_ACCESS_FLAGS
:
2041 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2043 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2045 case GL_BUFFER_MAP_OFFSET
:
2046 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2048 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2050 case GL_BUFFER_MAP_LENGTH
:
2051 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2053 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2055 case GL_BUFFER_IMMUTABLE_STORAGE
:
2056 if (!ctx
->Extensions
.ARB_buffer_storage
)
2058 *params
= bufObj
->Immutable
;
2060 case GL_BUFFER_STORAGE_FLAGS
:
2061 if (!ctx
->Extensions
.ARB_buffer_storage
)
2063 *params
= bufObj
->StorageFlags
;
2072 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2073 _mesa_enum_to_string(pname
));
2078 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2080 GET_CURRENT_CONTEXT(ctx
);
2081 struct gl_buffer_object
*bufObj
;
2084 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2085 GL_INVALID_OPERATION
);
2089 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2090 "glGetBufferParameteriv"))
2091 return; /* Error already recorded. */
2093 *params
= (GLint
) parameter
;
2097 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2099 GET_CURRENT_CONTEXT(ctx
);
2100 struct gl_buffer_object
*bufObj
;
2103 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2104 GL_INVALID_OPERATION
);
2108 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2109 "glGetBufferParameteri64v"))
2110 return; /* Error already recorded. */
2112 *params
= parameter
;
2116 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2118 GET_CURRENT_CONTEXT(ctx
);
2119 struct gl_buffer_object
*bufObj
;
2122 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2123 "glGetNamedBufferParameteriv");
2127 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2128 "glGetNamedBufferParameteriv"))
2129 return; /* Error already recorded. */
2131 *params
= (GLint
) parameter
;
2135 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2138 GET_CURRENT_CONTEXT(ctx
);
2139 struct gl_buffer_object
*bufObj
;
2142 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2143 "glGetNamedBufferParameteri64v");
2147 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2148 "glGetNamedBufferParameteri64v"))
2149 return; /* Error already recorded. */
2151 *params
= parameter
;
2156 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2158 GET_CURRENT_CONTEXT(ctx
);
2159 struct gl_buffer_object
*bufObj
;
2161 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2162 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2163 "GL_BUFFER_MAP_POINTER)");
2167 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2168 GL_INVALID_OPERATION
);
2172 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2176 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2178 GET_CURRENT_CONTEXT(ctx
);
2179 struct gl_buffer_object
*bufObj
;
2181 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2182 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2183 "GL_BUFFER_MAP_POINTER)");
2187 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2188 "glGetNamedBufferPointerv");
2192 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2197 _mesa_copy_buffer_sub_data(struct gl_context
*ctx
,
2198 struct gl_buffer_object
*src
,
2199 struct gl_buffer_object
*dst
,
2200 GLintptr readOffset
, GLintptr writeOffset
,
2201 GLsizeiptr size
, const char *func
)
2203 if (_mesa_check_disallowed_mapping(src
)) {
2204 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2205 "%s(readBuffer is mapped)", func
);
2209 if (_mesa_check_disallowed_mapping(dst
)) {
2210 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2211 "%s(writeBuffer is mapped)", func
);
2215 if (readOffset
< 0) {
2216 _mesa_error(ctx
, GL_INVALID_VALUE
,
2217 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2221 if (writeOffset
< 0) {
2222 _mesa_error(ctx
, GL_INVALID_VALUE
,
2223 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2228 _mesa_error(ctx
, GL_INVALID_VALUE
,
2229 "%s(size %d < 0)", func
, (int) size
);
2233 if (readOffset
+ size
> src
->Size
) {
2234 _mesa_error(ctx
, GL_INVALID_VALUE
,
2235 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2236 (int) readOffset
, (int) size
, (int) src
->Size
);
2240 if (writeOffset
+ size
> dst
->Size
) {
2241 _mesa_error(ctx
, GL_INVALID_VALUE
,
2242 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2243 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2248 if (readOffset
+ size
<= writeOffset
) {
2251 else if (writeOffset
+ size
<= readOffset
) {
2255 /* overlapping src/dst is illegal */
2256 _mesa_error(ctx
, GL_INVALID_VALUE
,
2257 "%s(overlapping src/dst)", func
);
2262 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2266 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2267 GLintptr readOffset
, GLintptr writeOffset
,
2270 GET_CURRENT_CONTEXT(ctx
);
2271 struct gl_buffer_object
*src
, *dst
;
2273 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2274 GL_INVALID_OPERATION
);
2278 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2279 GL_INVALID_OPERATION
);
2283 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2284 "glCopyBufferSubData");
2288 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2289 GLintptr readOffset
, GLintptr writeOffset
,
2292 GET_CURRENT_CONTEXT(ctx
);
2293 struct gl_buffer_object
*src
, *dst
;
2295 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2296 "glCopyNamedBufferSubData");
2300 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2301 "glCopyNamedBufferSubData");
2305 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2306 "glCopyNamedBufferSubData");
2311 _mesa_map_buffer_range(struct gl_context
*ctx
,
2312 struct gl_buffer_object
*bufObj
,
2313 GLintptr offset
, GLsizeiptr length
,
2314 GLbitfield access
, const char *func
)
2317 GLbitfield allowed_access
;
2319 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
2322 _mesa_error(ctx
, GL_INVALID_VALUE
,
2323 "%s(offset %ld < 0)", func
, (long) offset
);
2328 _mesa_error(ctx
, GL_INVALID_VALUE
,
2329 "%s(length %ld < 0)", func
, (long) length
);
2333 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2335 * "An INVALID_OPERATION error is generated for any of the following
2338 * * <length> is zero."
2340 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2341 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2345 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2349 allowed_access
= GL_MAP_READ_BIT
|
2351 GL_MAP_INVALIDATE_RANGE_BIT
|
2352 GL_MAP_INVALIDATE_BUFFER_BIT
|
2353 GL_MAP_FLUSH_EXPLICIT_BIT
|
2354 GL_MAP_UNSYNCHRONIZED_BIT
;
2356 if (ctx
->Extensions
.ARB_buffer_storage
) {
2357 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2358 GL_MAP_COHERENT_BIT
;
2361 if (access
& ~allowed_access
) {
2362 /* generate an error if any bits other than those allowed are set */
2363 _mesa_error(ctx
, GL_INVALID_VALUE
,
2364 "%s(access has undefined bits set)", func
);
2368 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2369 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2370 "%s(access indicates neither read or write)", func
);
2374 if ((access
& GL_MAP_READ_BIT
) &&
2375 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2376 GL_MAP_INVALIDATE_BUFFER_BIT
|
2377 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2378 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2379 "%s(read access with disallowed bits)", func
);
2383 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2384 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2385 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2386 "%s(access has flush explicit without write)", func
);
2390 if (access
& GL_MAP_READ_BIT
&&
2391 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2392 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2393 "%s(buffer does not allow read access)", func
);
2397 if (access
& GL_MAP_WRITE_BIT
&&
2398 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2399 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2400 "%s(buffer does not allow write access)", func
);
2404 if (access
& GL_MAP_COHERENT_BIT
&&
2405 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2406 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2407 "%s(buffer does not allow coherent access)", func
);
2411 if (access
& GL_MAP_PERSISTENT_BIT
&&
2412 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2413 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2414 "%s(buffer does not allow persistent access)", func
);
2418 if (offset
+ length
> bufObj
->Size
) {
2419 _mesa_error(ctx
, GL_INVALID_VALUE
,
2420 "%s(offset %td + length %td > buffer_size %td)", func
,
2421 offset
, length
, bufObj
->Size
);
2425 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2426 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2427 "%s(buffer already mapped)", func
);
2431 if (!bufObj
->Size
) {
2432 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2436 if (access
& GL_MAP_WRITE_BIT
) {
2437 bufObj
->NumMapBufferWriteCalls
++;
2438 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2439 bufObj
->Usage
== GL_STATIC_COPY
) &&
2440 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
2441 BUFFER_USAGE_WARNING(ctx
,
2442 "using %s(buffer %u, offset %u, length %u) to "
2443 "update a %s buffer",
2444 func
, bufObj
->Name
, offset
, length
,
2445 _mesa_enum_to_string(bufObj
->Usage
));
2449 assert(ctx
->Driver
.MapBufferRange
);
2450 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2453 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2456 /* The driver callback should have set all these fields.
2457 * This is important because other modules (like VBO) might call
2458 * the driver function directly.
2460 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2461 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2462 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2463 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2466 if (access
& GL_MAP_WRITE_BIT
)
2467 bufObj
->Written
= GL_TRUE
;
2470 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2471 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2472 bufObj
->Name
, bufObj
->Size
, access
);
2473 /* Access must be write only */
2474 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2476 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2477 for (i
= 0; i
< bufObj
->Size
; i
++)
2484 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2485 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2487 /* buffer is 100 bytes larger than requested, fill with magic value */
2488 for (i
= 0; i
< 100; i
++) {
2489 buf
[bufObj
->Size
- i
- 1] = 123;
2498 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2501 GET_CURRENT_CONTEXT(ctx
);
2502 struct gl_buffer_object
*bufObj
;
2504 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2505 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2506 "glMapBufferRange(ARB_map_buffer_range not supported)");
2510 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2514 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2515 "glMapBufferRange");
2519 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2522 GET_CURRENT_CONTEXT(ctx
);
2523 struct gl_buffer_object
*bufObj
;
2525 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2526 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2527 "glMapNamedBufferRange("
2528 "ARB_map_buffer_range not supported)");
2532 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2536 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2537 "glMapNamedBufferRange");
2541 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2542 * flags for input to _mesa_map_buffer_range.
2544 * \return true if the type of requested access is permissible.
2547 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2551 case GL_READ_ONLY_ARB
:
2552 *flags
= GL_MAP_READ_BIT
;
2553 return _mesa_is_desktop_gl(ctx
);
2554 case GL_WRITE_ONLY_ARB
:
2555 *flags
= GL_MAP_WRITE_BIT
;
2557 case GL_READ_WRITE_ARB
:
2558 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2559 return _mesa_is_desktop_gl(ctx
);
2566 _mesa_MapBuffer(GLenum target
, GLenum access
)
2568 GET_CURRENT_CONTEXT(ctx
);
2569 struct gl_buffer_object
*bufObj
;
2570 GLbitfield accessFlags
;
2572 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2573 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2577 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2581 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2586 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2588 GET_CURRENT_CONTEXT(ctx
);
2589 struct gl_buffer_object
*bufObj
;
2590 GLbitfield accessFlags
;
2592 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2593 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2597 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2601 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2602 "glMapNamedBuffer");
2607 _mesa_flush_mapped_buffer_range(struct gl_context
*ctx
,
2608 struct gl_buffer_object
*bufObj
,
2609 GLintptr offset
, GLsizeiptr length
,
2612 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2613 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2614 "%s(ARB_map_buffer_range not supported)", func
);
2619 _mesa_error(ctx
, GL_INVALID_VALUE
,
2620 "%s(offset %ld < 0)", func
, (long) offset
);
2625 _mesa_error(ctx
, GL_INVALID_VALUE
,
2626 "%s(length %ld < 0)", func
, (long) length
);
2630 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2631 /* buffer is not mapped */
2632 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2633 "%s(buffer is not mapped)", func
);
2637 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2638 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2639 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2640 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2644 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2645 _mesa_error(ctx
, GL_INVALID_VALUE
,
2646 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2647 (long) offset
, (long) length
,
2648 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2652 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2654 if (ctx
->Driver
.FlushMappedBufferRange
)
2655 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2660 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2663 GET_CURRENT_CONTEXT(ctx
);
2664 struct gl_buffer_object
*bufObj
;
2666 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2667 GL_INVALID_OPERATION
);
2671 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2672 "glFlushMappedBufferRange");
2676 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2679 GET_CURRENT_CONTEXT(ctx
);
2680 struct gl_buffer_object
*bufObj
;
2682 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2683 "glFlushMappedNamedBufferRange");
2687 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2688 "glFlushMappedNamedBufferRange");
2693 * Binds a buffer object to a uniform buffer binding point.
2695 * The caller is responsible for flushing vertices and updating
2699 set_ubo_binding(struct gl_context
*ctx
,
2700 struct gl_uniform_buffer_binding
*binding
,
2701 struct gl_buffer_object
*bufObj
,
2706 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2708 binding
->Offset
= offset
;
2709 binding
->Size
= size
;
2710 binding
->AutomaticSize
= autoSize
;
2712 /* If this is a real buffer object, mark it has having been used
2713 * at some point as a UBO.
2716 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
2720 * Binds a buffer object to a shader storage buffer binding point.
2722 * The caller is responsible for flushing vertices and updating
2726 set_ssbo_binding(struct gl_context
*ctx
,
2727 struct gl_shader_storage_buffer_binding
*binding
,
2728 struct gl_buffer_object
*bufObj
,
2733 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2735 binding
->Offset
= offset
;
2736 binding
->Size
= size
;
2737 binding
->AutomaticSize
= autoSize
;
2739 /* If this is a real buffer object, mark it has having been used
2740 * at some point as a SSBO.
2743 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
2747 * Binds a buffer object to a uniform buffer binding point.
2749 * Unlike set_ubo_binding(), this function also flushes vertices
2750 * and updates NewDriverState. It also checks if the binding
2751 * has actually changed before updating it.
2754 bind_uniform_buffer(struct gl_context
*ctx
,
2756 struct gl_buffer_object
*bufObj
,
2761 struct gl_uniform_buffer_binding
*binding
=
2762 &ctx
->UniformBufferBindings
[index
];
2764 if (binding
->BufferObject
== bufObj
&&
2765 binding
->Offset
== offset
&&
2766 binding
->Size
== size
&&
2767 binding
->AutomaticSize
== autoSize
) {
2771 FLUSH_VERTICES(ctx
, 0);
2772 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
2774 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2778 * Binds a buffer object to a shader storage buffer binding point.
2780 * Unlike set_ssbo_binding(), this function also flushes vertices
2781 * and updates NewDriverState. It also checks if the binding
2782 * has actually changed before updating it.
2785 bind_shader_storage_buffer(struct gl_context
*ctx
,
2787 struct gl_buffer_object
*bufObj
,
2792 struct gl_shader_storage_buffer_binding
*binding
=
2793 &ctx
->ShaderStorageBufferBindings
[index
];
2795 if (binding
->BufferObject
== bufObj
&&
2796 binding
->Offset
== offset
&&
2797 binding
->Size
== size
&&
2798 binding
->AutomaticSize
== autoSize
) {
2802 FLUSH_VERTICES(ctx
, 0);
2803 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
2805 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2809 * Bind a region of a buffer object to a uniform block binding point.
2810 * \param index the uniform buffer binding point index
2811 * \param bufObj the buffer object
2812 * \param offset offset to the start of buffer object region
2813 * \param size size of the buffer object region
2816 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2818 struct gl_buffer_object
*bufObj
,
2822 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2823 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2827 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2828 _mesa_error(ctx
, GL_INVALID_VALUE
,
2829 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2830 ctx
->Const
.UniformBufferOffsetAlignment
);
2834 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2839 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2840 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2844 * Bind a region of a buffer object to a shader storage block binding point.
2845 * \param index the shader storage buffer binding point index
2846 * \param bufObj the buffer object
2847 * \param offset offset to the start of buffer object region
2848 * \param size size of the buffer object region
2851 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
2853 struct gl_buffer_object
*bufObj
,
2857 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2858 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2862 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
2863 _mesa_error(ctx
, GL_INVALID_VALUE
,
2864 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2865 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
2869 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2874 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2875 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2879 * Bind a buffer object to a uniform block binding point.
2880 * As above, but offset = 0.
2883 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2885 struct gl_buffer_object
*bufObj
)
2887 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2888 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2892 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2894 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2895 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2897 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2901 * Bind a buffer object to a shader storage block binding point.
2902 * As above, but offset = 0.
2905 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
2907 struct gl_buffer_object
*bufObj
)
2909 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2910 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2914 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2916 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2917 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2919 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2923 * Binds a buffer object to an atomic buffer binding point.
2925 * The caller is responsible for validating the offset,
2926 * flushing the vertices and updating NewDriverState.
2929 set_atomic_buffer_binding(struct gl_context
*ctx
,
2930 struct gl_atomic_buffer_binding
*binding
,
2931 struct gl_buffer_object
*bufObj
,
2935 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2937 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2938 binding
->Offset
= -1;
2941 binding
->Offset
= offset
;
2942 binding
->Size
= size
;
2943 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
2948 * Binds a buffer object to an atomic buffer binding point.
2950 * Unlike set_atomic_buffer_binding(), this function also validates the
2951 * index and offset, flushes vertices, and updates NewDriverState.
2952 * It also checks if the binding has actually changing before
2956 bind_atomic_buffer(struct gl_context
*ctx
,
2958 struct gl_buffer_object
*bufObj
,
2963 struct gl_atomic_buffer_binding
*binding
;
2965 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
2966 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
2970 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
2971 _mesa_error(ctx
, GL_INVALID_VALUE
,
2972 "%s(offset misaligned %d/%d)", name
, (int) offset
,
2973 ATOMIC_COUNTER_SIZE
);
2977 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
2979 binding
= &ctx
->AtomicBufferBindings
[index
];
2980 if (binding
->BufferObject
== bufObj
&&
2981 binding
->Offset
== offset
&&
2982 binding
->Size
== size
) {
2986 FLUSH_VERTICES(ctx
, 0);
2987 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
2989 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
2993 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
2995 const GLintptr
*offsets
,
2996 const GLsizeiptr
*sizes
)
2998 if (offsets
[index
] < 0) {
2999 /* The ARB_multi_bind spec says:
3001 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3002 * value in <offsets> is less than zero (per binding)."
3004 _mesa_error(ctx
, GL_INVALID_VALUE
,
3005 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3006 index
, (int64_t) offsets
[index
]);
3010 if (sizes
[index
] <= 0) {
3011 /* The ARB_multi_bind spec says:
3013 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3014 * value in <sizes> is less than or equal to zero (per binding)."
3016 _mesa_error(ctx
, GL_INVALID_VALUE
,
3017 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3018 index
, (int64_t) sizes
[index
]);
3026 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3027 GLuint first
, GLsizei count
,
3030 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3031 _mesa_error(ctx
, GL_INVALID_ENUM
,
3032 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3036 /* The ARB_multi_bind_spec says:
3038 * "An INVALID_OPERATION error is generated if <first> + <count> is
3039 * greater than the number of target-specific indexed binding points,
3040 * as described in section 6.7.1."
3042 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3043 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3044 "%s(first=%u + count=%d > the value of "
3045 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3046 caller
, first
, count
,
3047 ctx
->Const
.MaxUniformBufferBindings
);
3055 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3056 GLuint first
, GLsizei count
,
3059 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3060 _mesa_error(ctx
, GL_INVALID_ENUM
,
3061 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3065 /* The ARB_multi_bind_spec says:
3067 * "An INVALID_OPERATION error is generated if <first> + <count> is
3068 * greater than the number of target-specific indexed binding points,
3069 * as described in section 6.7.1."
3071 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3072 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3073 "%s(first=%u + count=%d > the value of "
3074 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3075 caller
, first
, count
,
3076 ctx
->Const
.MaxShaderStorageBufferBindings
);
3084 * Unbind all uniform buffers in the range
3085 * <first> through <first>+<count>-1
3088 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3090 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3093 for (i
= 0; i
< count
; i
++)
3094 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3095 bufObj
, -1, -1, GL_TRUE
);
3099 * Unbind all shader storage buffers in the range
3100 * <first> through <first>+<count>-1
3103 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3106 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3109 for (i
= 0; i
< count
; i
++)
3110 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3111 bufObj
, -1, -1, GL_TRUE
);
3115 bind_uniform_buffers_base(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3116 const GLuint
*buffers
)
3120 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, "glBindBuffersBase"))
3123 /* Assume that at least one binding will be changed */
3124 FLUSH_VERTICES(ctx
, 0);
3125 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3128 /* The ARB_multi_bind spec says:
3130 * "If <buffers> is NULL, all bindings from <first> through
3131 * <first>+<count>-1 are reset to their unbound (zero) state."
3133 unbind_uniform_buffers(ctx
, first
, count
);
3137 /* Note that the error semantics for multi-bind commands differ from
3138 * those of other GL commands.
3140 * The Issues section in the ARB_multi_bind spec says:
3142 * "(11) Typically, OpenGL specifies that if an error is generated by a
3143 * command, that command has no effect. This is somewhat
3144 * unfortunate for multi-bind commands, because it would require a
3145 * first pass to scan the entire list of bound objects for errors
3146 * and then a second pass to actually perform the bindings.
3147 * Should we have different error semantics?
3149 * RESOLVED: Yes. In this specification, when the parameters for
3150 * one of the <count> binding points are invalid, that binding point
3151 * is not updated and an error will be generated. However, other
3152 * binding points in the same command will be updated if their
3153 * parameters are valid and no other error occurs."
3156 _mesa_begin_bufferobj_lookups(ctx
);
3158 for (i
= 0; i
< count
; i
++) {
3159 struct gl_uniform_buffer_binding
*binding
=
3160 &ctx
->UniformBufferBindings
[first
+ i
];
3161 struct gl_buffer_object
*bufObj
;
3163 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3164 bufObj
= binding
->BufferObject
;
3166 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3167 "glBindBuffersBase");
3170 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3171 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, GL_TRUE
);
3173 set_ubo_binding(ctx
, binding
, bufObj
, 0, 0, GL_TRUE
);
3177 _mesa_end_bufferobj_lookups(ctx
);
3181 bind_shader_storage_buffers_base(struct gl_context
*ctx
, GLuint first
,
3182 GLsizei count
, const GLuint
*buffers
)
3186 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
,
3187 "glBindBuffersBase"))
3190 /* Assume that at least one binding will be changed */
3191 FLUSH_VERTICES(ctx
, 0);
3192 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3195 /* The ARB_multi_bind spec says:
3197 * "If <buffers> is NULL, all bindings from <first> through
3198 * <first>+<count>-1 are reset to their unbound (zero) state."
3200 unbind_shader_storage_buffers(ctx
, first
, count
);
3204 /* Note that the error semantics for multi-bind commands differ from
3205 * those of other GL commands.
3207 * The Issues section in the ARB_multi_bind spec says:
3209 * "(11) Typically, OpenGL specifies that if an error is generated by a
3210 * command, that command has no effect. This is somewhat
3211 * unfortunate for multi-bind commands, because it would require a
3212 * first pass to scan the entire list of bound objects for errors
3213 * and then a second pass to actually perform the bindings.
3214 * Should we have different error semantics?
3216 * RESOLVED: Yes. In this specification, when the parameters for
3217 * one of the <count> binding points are invalid, that binding point
3218 * is not updated and an error will be generated. However, other
3219 * binding points in the same command will be updated if their
3220 * parameters are valid and no other error occurs."
3223 _mesa_begin_bufferobj_lookups(ctx
);
3225 for (i
= 0; i
< count
; i
++) {
3226 struct gl_shader_storage_buffer_binding
*binding
=
3227 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3228 struct gl_buffer_object
*bufObj
;
3230 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3231 bufObj
= binding
->BufferObject
;
3233 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3234 "glBindBuffersBase");
3237 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3238 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, GL_TRUE
);
3240 set_ssbo_binding(ctx
, binding
, bufObj
, 0, 0, GL_TRUE
);
3244 _mesa_end_bufferobj_lookups(ctx
);
3248 bind_uniform_buffers_range(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3249 const GLuint
*buffers
,
3250 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3254 if (!error_check_bind_uniform_buffers(ctx
, first
, count
,
3255 "glBindBuffersRange"))
3258 /* Assume that at least one binding will be changed */
3259 FLUSH_VERTICES(ctx
, 0);
3260 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3263 /* The ARB_multi_bind spec says:
3265 * "If <buffers> is NULL, all bindings from <first> through
3266 * <first>+<count>-1 are reset to their unbound (zero) state.
3267 * In this case, the offsets and sizes associated with the
3268 * binding points are set to default values, ignoring
3269 * <offsets> and <sizes>."
3271 unbind_uniform_buffers(ctx
, first
, count
);
3275 /* Note that the error semantics for multi-bind commands differ from
3276 * those of other GL commands.
3278 * The Issues section in the ARB_multi_bind spec says:
3280 * "(11) Typically, OpenGL specifies that if an error is generated by a
3281 * command, that command has no effect. This is somewhat
3282 * unfortunate for multi-bind commands, because it would require a
3283 * first pass to scan the entire list of bound objects for errors
3284 * and then a second pass to actually perform the bindings.
3285 * Should we have different error semantics?
3287 * RESOLVED: Yes. In this specification, when the parameters for
3288 * one of the <count> binding points are invalid, that binding point
3289 * is not updated and an error will be generated. However, other
3290 * binding points in the same command will be updated if their
3291 * parameters are valid and no other error occurs."
3294 _mesa_begin_bufferobj_lookups(ctx
);
3296 for (i
= 0; i
< count
; i
++) {
3297 struct gl_uniform_buffer_binding
*binding
=
3298 &ctx
->UniformBufferBindings
[first
+ i
];
3299 struct gl_buffer_object
*bufObj
;
3301 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3304 /* The ARB_multi_bind spec says:
3306 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3307 * pair of values in <offsets> and <sizes> does not respectively
3308 * satisfy the constraints described for those parameters for the
3309 * specified target, as described in section 6.7.1 (per binding)."
3311 * Section 6.7.1 refers to table 6.5, which says:
3313 * "┌───────────────────────────────────────────────────────────────┐
3314 * │ Uniform buffer array bindings (see sec. 7.6) │
3315 * ├─────────────────────┬─────────────────────────────────────────┤
3317 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3318 * │ │ OFFSET_ALIGNMENT │
3320 * │ size restriction │ none │
3321 * └─────────────────────┴─────────────────────────────────────────┘"
3323 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3324 _mesa_error(ctx
, GL_INVALID_VALUE
,
3325 "glBindBuffersRange(offsets[%u]=%" PRId64
3326 " is misaligned; it must be a multiple of the value of "
3327 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3328 "target=GL_UNIFORM_BUFFER)",
3329 i
, (int64_t) offsets
[i
],
3330 ctx
->Const
.UniformBufferOffsetAlignment
);
3334 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3335 bufObj
= binding
->BufferObject
;
3337 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3338 "glBindBuffersRange");
3341 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3342 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, GL_FALSE
);
3344 set_ubo_binding(ctx
, binding
, bufObj
,
3345 offsets
[i
], sizes
[i
], GL_FALSE
);
3349 _mesa_end_bufferobj_lookups(ctx
);
3353 bind_shader_storage_buffers_range(struct gl_context
*ctx
, GLuint first
,
3354 GLsizei count
, const GLuint
*buffers
,
3355 const GLintptr
*offsets
,
3356 const GLsizeiptr
*sizes
)
3360 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
,
3361 "glBindBuffersRange"))
3364 /* Assume that at least one binding will be changed */
3365 FLUSH_VERTICES(ctx
, 0);
3366 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3369 /* The ARB_multi_bind spec says:
3371 * "If <buffers> is NULL, all bindings from <first> through
3372 * <first>+<count>-1 are reset to their unbound (zero) state.
3373 * In this case, the offsets and sizes associated with the
3374 * binding points are set to default values, ignoring
3375 * <offsets> and <sizes>."
3377 unbind_shader_storage_buffers(ctx
, first
, count
);
3381 /* Note that the error semantics for multi-bind commands differ from
3382 * those of other GL commands.
3384 * The Issues section in the ARB_multi_bind spec says:
3386 * "(11) Typically, OpenGL specifies that if an error is generated by a
3387 * command, that command has no effect. This is somewhat
3388 * unfortunate for multi-bind commands, because it would require a
3389 * first pass to scan the entire list of bound objects for errors
3390 * and then a second pass to actually perform the bindings.
3391 * Should we have different error semantics?
3393 * RESOLVED: Yes. In this specification, when the parameters for
3394 * one of the <count> binding points are invalid, that binding point
3395 * is not updated and an error will be generated. However, other
3396 * binding points in the same command will be updated if their
3397 * parameters are valid and no other error occurs."
3400 _mesa_begin_bufferobj_lookups(ctx
);
3402 for (i
= 0; i
< count
; i
++) {
3403 struct gl_shader_storage_buffer_binding
*binding
=
3404 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3405 struct gl_buffer_object
*bufObj
;
3407 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3410 /* The ARB_multi_bind spec says:
3412 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3413 * pair of values in <offsets> and <sizes> does not respectively
3414 * satisfy the constraints described for those parameters for the
3415 * specified target, as described in section 6.7.1 (per binding)."
3417 * Section 6.7.1 refers to table 6.5, which says:
3419 * "┌───────────────────────────────────────────────────────────────┐
3420 * │ Shader storage buffer array bindings (see sec. 7.8) │
3421 * ├─────────────────────┬─────────────────────────────────────────┤
3423 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3424 * │ │ BUFFER_OFFSET_ALIGNMENT │
3426 * │ size restriction │ none │
3427 * └─────────────────────┴─────────────────────────────────────────┘"
3429 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3430 _mesa_error(ctx
, GL_INVALID_VALUE
,
3431 "glBindBuffersRange(offsets[%u]=%" PRId64
3432 " is misaligned; it must be a multiple of the value of "
3433 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3434 "target=GL_SHADER_STORAGE_BUFFER)",
3435 i
, (int64_t) offsets
[i
],
3436 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3440 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3441 bufObj
= binding
->BufferObject
;
3443 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3444 "glBindBuffersRange");
3447 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3448 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, GL_FALSE
);
3450 set_ssbo_binding(ctx
, binding
, bufObj
,
3451 offsets
[i
], sizes
[i
], GL_FALSE
);
3455 _mesa_end_bufferobj_lookups(ctx
);
3459 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3460 struct gl_transform_feedback_object
*tfObj
,
3461 GLuint first
, GLsizei count
, const char *caller
)
3463 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3464 _mesa_error(ctx
, GL_INVALID_ENUM
,
3465 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3469 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3471 * "An INVALID_OPERATION error is generated :
3474 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3475 * FEEDBACK_BUFFER and transform feedback is currently active."
3477 * We assume that this is also meant to apply to BindBuffersRange
3478 * and BindBuffersBase.
3480 if (tfObj
->Active
) {
3481 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3482 "%s(Changing transform feedback buffers while "
3483 "transform feedback is active)", caller
);
3487 /* The ARB_multi_bind_spec says:
3489 * "An INVALID_OPERATION error is generated if <first> + <count> is
3490 * greater than the number of target-specific indexed binding points,
3491 * as described in section 6.7.1."
3493 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3494 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3495 "%s(first=%u + count=%d > the value of "
3496 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3497 caller
, first
, count
,
3498 ctx
->Const
.MaxTransformFeedbackBuffers
);
3506 * Unbind all transform feedback buffers in the range
3507 * <first> through <first>+<count>-1
3510 unbind_xfb_buffers(struct gl_context
*ctx
,
3511 struct gl_transform_feedback_object
*tfObj
,
3512 GLuint first
, GLsizei count
)
3514 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3517 for (i
= 0; i
< count
; i
++)
3518 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3523 bind_xfb_buffers_base(struct gl_context
*ctx
,
3524 GLuint first
, GLsizei count
,
3525 const GLuint
*buffers
)
3527 struct gl_transform_feedback_object
*tfObj
=
3528 ctx
->TransformFeedback
.CurrentObject
;
3531 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
,
3532 "glBindBuffersBase"))
3535 /* Assume that at least one binding will be changed */
3536 FLUSH_VERTICES(ctx
, 0);
3537 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3540 /* The ARB_multi_bind spec says:
3542 * "If <buffers> is NULL, all bindings from <first> through
3543 * <first>+<count>-1 are reset to their unbound (zero) state."
3545 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3549 /* Note that the error semantics for multi-bind commands differ from
3550 * those of other GL commands.
3552 * The Issues section in the ARB_multi_bind spec says:
3554 * "(11) Typically, OpenGL specifies that if an error is generated by a
3555 * command, that command has no effect. This is somewhat
3556 * unfortunate for multi-bind commands, because it would require a
3557 * first pass to scan the entire list of bound objects for errors
3558 * and then a second pass to actually perform the bindings.
3559 * Should we have different error semantics?
3561 * RESOLVED: Yes. In this specification, when the parameters for
3562 * one of the <count> binding points are invalid, that binding point
3563 * is not updated and an error will be generated. However, other
3564 * binding points in the same command will be updated if their
3565 * parameters are valid and no other error occurs."
3568 _mesa_begin_bufferobj_lookups(ctx
);
3570 for (i
= 0; i
< count
; i
++) {
3571 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[first
+ i
];
3572 struct gl_buffer_object
*bufObj
;
3574 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3575 bufObj
= boundBufObj
;
3577 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3578 "glBindBuffersBase");
3581 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3585 _mesa_end_bufferobj_lookups(ctx
);
3589 bind_xfb_buffers_range(struct gl_context
*ctx
,
3590 GLuint first
, GLsizei count
,
3591 const GLuint
*buffers
,
3592 const GLintptr
*offsets
,
3593 const GLsizeiptr
*sizes
)
3595 struct gl_transform_feedback_object
*tfObj
=
3596 ctx
->TransformFeedback
.CurrentObject
;
3599 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
,
3600 "glBindBuffersRange"))
3603 /* Assume that at least one binding will be changed */
3604 FLUSH_VERTICES(ctx
, 0);
3605 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3608 /* The ARB_multi_bind spec says:
3610 * "If <buffers> is NULL, all bindings from <first> through
3611 * <first>+<count>-1 are reset to their unbound (zero) state.
3612 * In this case, the offsets and sizes associated with the
3613 * binding points are set to default values, ignoring
3614 * <offsets> and <sizes>."
3616 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3620 /* Note that the error semantics for multi-bind commands differ from
3621 * those of other GL commands.
3623 * The Issues section in the ARB_multi_bind spec says:
3625 * "(11) Typically, OpenGL specifies that if an error is generated by a
3626 * command, that command has no effect. This is somewhat
3627 * unfortunate for multi-bind commands, because it would require a
3628 * first pass to scan the entire list of bound objects for errors
3629 * and then a second pass to actually perform the bindings.
3630 * Should we have different error semantics?
3632 * RESOLVED: Yes. In this specification, when the parameters for
3633 * one of the <count> binding points are invalid, that binding point
3634 * is not updated and an error will be generated. However, other
3635 * binding points in the same command will be updated if their
3636 * parameters are valid and no other error occurs."
3639 _mesa_begin_bufferobj_lookups(ctx
);
3641 for (i
= 0; i
< count
; i
++) {
3642 const GLuint index
= first
+ i
;
3643 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3644 struct gl_buffer_object
*bufObj
;
3646 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3649 /* The ARB_multi_bind spec says:
3651 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3652 * pair of values in <offsets> and <sizes> does not respectively
3653 * satisfy the constraints described for those parameters for the
3654 * specified target, as described in section 6.7.1 (per binding)."
3656 * Section 6.7.1 refers to table 6.5, which says:
3658 * "┌───────────────────────────────────────────────────────────────┐
3659 * │ Transform feedback array bindings (see sec. 13.2.2) │
3660 * ├───────────────────────┬───────────────────────────────────────┤
3662 * │ offset restriction │ multiple of 4 │
3664 * │ size restriction │ multiple of 4 │
3665 * └───────────────────────┴───────────────────────────────────────┘"
3667 if (offsets
[i
] & 0x3) {
3668 _mesa_error(ctx
, GL_INVALID_VALUE
,
3669 "glBindBuffersRange(offsets[%u]=%" PRId64
3670 " is misaligned; it must be a multiple of 4 when "
3671 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3672 i
, (int64_t) offsets
[i
]);
3676 if (sizes
[i
] & 0x3) {
3677 _mesa_error(ctx
, GL_INVALID_VALUE
,
3678 "glBindBuffersRange(sizes[%u]=%" PRId64
3679 " is misaligned; it must be a multiple of 4 when "
3680 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3681 i
, (int64_t) sizes
[i
]);
3685 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3686 bufObj
= boundBufObj
;
3688 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3689 "glBindBuffersRange");
3692 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
3693 offsets
[i
], sizes
[i
]);
3696 _mesa_end_bufferobj_lookups(ctx
);
3700 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
3701 GLuint first
, GLsizei count
,
3704 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
3705 _mesa_error(ctx
, GL_INVALID_ENUM
,
3706 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
3710 /* The ARB_multi_bind_spec says:
3712 * "An INVALID_OPERATION error is generated if <first> + <count> is
3713 * greater than the number of target-specific indexed binding points,
3714 * as described in section 6.7.1."
3716 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
3717 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3718 "%s(first=%u + count=%d > the value of "
3719 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3720 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
3728 * Unbind all atomic counter buffers in the range
3729 * <first> through <first>+<count>-1
3732 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3734 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3737 for (i
= 0; i
< count
; i
++)
3738 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
3743 bind_atomic_buffers_base(struct gl_context
*ctx
,
3746 const GLuint
*buffers
)
3750 if (!error_check_bind_atomic_buffers(ctx
, first
, count
,
3751 "glBindBuffersBase"))
3754 /* Assume that at least one binding will be changed */
3755 FLUSH_VERTICES(ctx
, 0);
3756 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3759 /* The ARB_multi_bind spec says:
3761 * "If <buffers> is NULL, all bindings from <first> through
3762 * <first>+<count>-1 are reset to their unbound (zero) state."
3764 unbind_atomic_buffers(ctx
, first
, count
);
3768 /* Note that the error semantics for multi-bind commands differ from
3769 * those of other GL commands.
3771 * The Issues section in the ARB_multi_bind spec says:
3773 * "(11) Typically, OpenGL specifies that if an error is generated by a
3774 * command, that command has no effect. This is somewhat
3775 * unfortunate for multi-bind commands, because it would require a
3776 * first pass to scan the entire list of bound objects for errors
3777 * and then a second pass to actually perform the bindings.
3778 * Should we have different error semantics?
3780 * RESOLVED: Yes. In this specification, when the parameters for
3781 * one of the <count> binding points are invalid, that binding point
3782 * is not updated and an error will be generated. However, other
3783 * binding points in the same command will be updated if their
3784 * parameters are valid and no other error occurs."
3787 _mesa_begin_bufferobj_lookups(ctx
);
3789 for (i
= 0; i
< count
; i
++) {
3790 struct gl_atomic_buffer_binding
*binding
=
3791 &ctx
->AtomicBufferBindings
[first
+ i
];
3792 struct gl_buffer_object
*bufObj
;
3794 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3795 bufObj
= binding
->BufferObject
;
3797 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3798 "glBindBuffersBase");
3801 set_atomic_buffer_binding(ctx
, binding
, bufObj
, 0, 0);
3804 _mesa_end_bufferobj_lookups(ctx
);
3808 bind_atomic_buffers_range(struct gl_context
*ctx
,
3811 const GLuint
*buffers
,
3812 const GLintptr
*offsets
,
3813 const GLsizeiptr
*sizes
)
3817 if (!error_check_bind_atomic_buffers(ctx
, first
, count
,
3818 "glBindBuffersRange"))
3821 /* Assume that at least one binding will be changed */
3822 FLUSH_VERTICES(ctx
, 0);
3823 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3826 /* The ARB_multi_bind spec says:
3828 * "If <buffers> is NULL, all bindings from <first> through
3829 * <first>+<count>-1 are reset to their unbound (zero) state.
3830 * In this case, the offsets and sizes associated with the
3831 * binding points are set to default values, ignoring
3832 * <offsets> and <sizes>."
3834 unbind_atomic_buffers(ctx
, first
, count
);
3838 /* Note that the error semantics for multi-bind commands differ from
3839 * those of other GL commands.
3841 * The Issues section in the ARB_multi_bind spec says:
3843 * "(11) Typically, OpenGL specifies that if an error is generated by a
3844 * command, that command has no effect. This is somewhat
3845 * unfortunate for multi-bind commands, because it would require a
3846 * first pass to scan the entire list of bound objects for errors
3847 * and then a second pass to actually perform the bindings.
3848 * Should we have different error semantics?
3850 * RESOLVED: Yes. In this specification, when the parameters for
3851 * one of the <count> binding points are invalid, that binding point
3852 * is not updated and an error will be generated. However, other
3853 * binding points in the same command will be updated if their
3854 * parameters are valid and no other error occurs."
3857 _mesa_begin_bufferobj_lookups(ctx
);
3859 for (i
= 0; i
< count
; i
++) {
3860 struct gl_atomic_buffer_binding
*binding
=
3861 &ctx
->AtomicBufferBindings
[first
+ i
];
3862 struct gl_buffer_object
*bufObj
;
3864 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3867 /* The ARB_multi_bind spec says:
3869 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3870 * pair of values in <offsets> and <sizes> does not respectively
3871 * satisfy the constraints described for those parameters for the
3872 * specified target, as described in section 6.7.1 (per binding)."
3874 * Section 6.7.1 refers to table 6.5, which says:
3876 * "┌───────────────────────────────────────────────────────────────┐
3877 * │ Atomic counter array bindings (see sec. 7.7.2) │
3878 * ├───────────────────────┬───────────────────────────────────────┤
3880 * │ offset restriction │ multiple of 4 │
3882 * │ size restriction │ none │
3883 * └───────────────────────┴───────────────────────────────────────┘"
3885 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
3886 _mesa_error(ctx
, GL_INVALID_VALUE
,
3887 "glBindBuffersRange(offsets[%u]=%" PRId64
3888 " is misaligned; it must be a multiple of %d when "
3889 "target=GL_ATOMIC_COUNTER_BUFFER)",
3890 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
3894 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3895 bufObj
= binding
->BufferObject
;
3897 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3898 "glBindBuffersRange");
3901 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offsets
[i
], sizes
[i
]);
3904 _mesa_end_bufferobj_lookups(ctx
);
3908 _mesa_BindBufferRange(GLenum target
, GLuint index
,
3909 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
3911 GET_CURRENT_CONTEXT(ctx
);
3912 struct gl_buffer_object
*bufObj
;
3915 bufObj
= ctx
->Shared
->NullBufferObj
;
3917 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3919 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3920 &bufObj
, "glBindBufferRange"))
3924 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3925 "glBindBufferRange(invalid buffer=%u)", buffer
);
3931 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
3938 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3939 _mesa_bind_buffer_range_transform_feedback(ctx
,
3940 ctx
->TransformFeedback
.CurrentObject
,
3941 index
, bufObj
, offset
, size
,
3944 case GL_UNIFORM_BUFFER
:
3945 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3947 case GL_SHADER_STORAGE_BUFFER
:
3948 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3950 case GL_ATOMIC_COUNTER_BUFFER
:
3951 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
3952 "glBindBufferRange");
3955 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
3961 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
3963 GET_CURRENT_CONTEXT(ctx
);
3964 struct gl_buffer_object
*bufObj
;
3967 bufObj
= ctx
->Shared
->NullBufferObj
;
3969 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3971 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3972 &bufObj
, "glBindBufferBase"))
3976 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3977 "glBindBufferBase(invalid buffer=%u)", buffer
);
3981 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3982 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3984 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3985 * zero and size equal to the size of buffer."
3987 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3989 * "If the parameter (starting offset or size) was not specified when the
3990 * buffer object was bound, zero is returned."
3992 * What happens if the size of the buffer changes? Does the size of the
3993 * buffer at the moment glBindBufferBase was called still play a role, like
3994 * the first quote would imply, or is the size meaningless in the
3995 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3996 * core spec page 45 says:
3998 * "It is equivalent to calling BindBufferRange with offset zero, while
3999 * size is determined by the size of the bound buffer at the time the
4002 * My interpretation is that the GL 4.1 spec was a clarification of the
4003 * behavior, not a change. In particular, this choice will only make
4004 * rendering work in cases where it would have had undefined results.
4008 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4009 _mesa_bind_buffer_base_transform_feedback(ctx
,
4010 ctx
->TransformFeedback
.CurrentObject
,
4011 index
, bufObj
, false);
4013 case GL_UNIFORM_BUFFER
:
4014 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4016 case GL_SHADER_STORAGE_BUFFER
:
4017 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4019 case GL_ATOMIC_COUNTER_BUFFER
:
4020 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
4021 "glBindBufferBase");
4024 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4030 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4031 const GLuint
*buffers
,
4032 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4034 GET_CURRENT_CONTEXT(ctx
);
4037 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4038 bind_xfb_buffers_range(ctx
, first
, count
, buffers
, offsets
, sizes
);
4040 case GL_UNIFORM_BUFFER
:
4041 bind_uniform_buffers_range(ctx
, first
, count
, buffers
, offsets
, sizes
);
4043 case GL_SHADER_STORAGE_BUFFER
:
4044 bind_shader_storage_buffers_range(ctx
, first
, count
, buffers
, offsets
,
4047 case GL_ATOMIC_COUNTER_BUFFER
:
4048 bind_atomic_buffers_range(ctx
, first
, count
, buffers
,
4052 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4053 _mesa_enum_to_string(target
));
4059 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4060 const GLuint
*buffers
)
4062 GET_CURRENT_CONTEXT(ctx
);
4065 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4066 bind_xfb_buffers_base(ctx
, first
, count
, buffers
);
4068 case GL_UNIFORM_BUFFER
:
4069 bind_uniform_buffers_base(ctx
, first
, count
, buffers
);
4071 case GL_SHADER_STORAGE_BUFFER
:
4072 bind_shader_storage_buffers_base(ctx
, first
, count
, buffers
);
4074 case GL_ATOMIC_COUNTER_BUFFER
:
4075 bind_atomic_buffers_base(ctx
, first
, count
, buffers
);
4078 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4079 _mesa_enum_to_string(target
));
4085 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4088 GET_CURRENT_CONTEXT(ctx
);
4089 struct gl_buffer_object
*bufObj
;
4090 const GLintptr end
= offset
+ length
;
4092 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4094 _mesa_error(ctx
, GL_INVALID_VALUE
,
4095 "glInvalidateBufferSubData(name = 0x%x) invalid object",
4100 /* The GL_ARB_invalidate_subdata spec says:
4102 * "An INVALID_VALUE error is generated if <offset> or <length> is
4103 * negative, or if <offset> + <length> is greater than the value of
4106 if (end
< 0 || end
> bufObj
->Size
) {
4107 _mesa_error(ctx
, GL_INVALID_VALUE
,
4108 "glInvalidateBufferSubData(invalid offset or length)");
4112 /* The OpenGL 4.4 (Core Profile) spec says:
4114 * "An INVALID_OPERATION error is generated if buffer is currently
4115 * mapped by MapBuffer or if the invalidate range intersects the range
4116 * currently mapped by MapBufferRange, unless it was mapped
4117 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4119 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4120 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4121 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4122 "glInvalidateBufferSubData(intersection with mapped "
4127 /* We don't actually do anything for this yet. Just return after
4128 * validating the parameters and generating the required errors.
4134 _mesa_InvalidateBufferData(GLuint buffer
)
4136 GET_CURRENT_CONTEXT(ctx
);
4137 struct gl_buffer_object
*bufObj
;
4139 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4141 _mesa_error(ctx
, GL_INVALID_VALUE
,
4142 "glInvalidateBufferData(name = 0x%x) invalid object",
4147 /* The OpenGL 4.4 (Core Profile) spec says:
4149 * "An INVALID_OPERATION error is generated if buffer is currently
4150 * mapped by MapBuffer or if the invalidate range intersects the range
4151 * currently mapped by MapBufferRange, unless it was mapped
4152 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4154 if (_mesa_check_disallowed_mapping(bufObj
)) {
4155 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4156 "glInvalidateBufferData(intersection with mapped "
4161 /* We don't actually do anything for this yet. Just return after
4162 * validating the parameters and generating the required errors.