2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
29 * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions.
30 * \author Brian Paul, Ian Romanick
34 #include <inttypes.h> /* for PRId64 macro */
40 #include "bufferobj.h"
43 #include "glformats.h"
45 #include "transformfeedback.h"
50 /*#define BOUNDS_CHECK*/
54 * We count the number of buffer modification calls to check for
55 * inefficient buffer use. This is the number of such calls before we
58 #define BUFFER_WARNING_CALL_COUNT 4
62 * Helper to warn of possible performance issues, such as frequently
63 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
67 buffer_usage_warning(struct gl_context
*ctx
, GLuint
*id
, const char *fmt
, ...)
72 _mesa_gl_vdebug(ctx
, id
,
73 MESA_DEBUG_SOURCE_API
,
74 MESA_DEBUG_TYPE_PERFORMANCE
,
75 MESA_DEBUG_SEVERITY_MEDIUM
,
80 #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
82 static GLuint id = 0; \
83 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
88 * Used as a placeholder for buffer objects between glGenBuffers() and
89 * glBindBuffer() so that glIsBuffer() can work correctly.
91 static struct gl_buffer_object DummyBufferObject
;
95 * Return pointer to address of a buffer object target.
96 * \param ctx the GL context
97 * \param target the buffer object target to be retrieved.
98 * \return pointer to pointer to the buffer object bound to \c target in the
99 * specified context or \c NULL if \c target is invalid.
101 static inline struct gl_buffer_object
**
102 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
104 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
106 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
107 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
111 case GL_ARRAY_BUFFER_ARB
:
112 return &ctx
->Array
.ArrayBufferObj
;
113 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
114 return &ctx
->Array
.VAO
->IndexBufferObj
;
115 case GL_PIXEL_PACK_BUFFER_EXT
:
116 return &ctx
->Pack
.BufferObj
;
117 case GL_PIXEL_UNPACK_BUFFER_EXT
:
118 return &ctx
->Unpack
.BufferObj
;
119 case GL_COPY_READ_BUFFER
:
120 return &ctx
->CopyReadBuffer
;
121 case GL_COPY_WRITE_BUFFER
:
122 return &ctx
->CopyWriteBuffer
;
123 case GL_DRAW_INDIRECT_BUFFER
:
124 if ((ctx
->API
== API_OPENGL_CORE
&&
125 ctx
->Extensions
.ARB_draw_indirect
) ||
126 _mesa_is_gles31(ctx
)) {
127 return &ctx
->DrawIndirectBuffer
;
130 case GL_DISPATCH_INDIRECT_BUFFER
:
131 if (_mesa_has_compute_shaders(ctx
)) {
132 return &ctx
->DispatchIndirectBuffer
;
135 case GL_TRANSFORM_FEEDBACK_BUFFER
:
136 if (ctx
->Extensions
.EXT_transform_feedback
) {
137 return &ctx
->TransformFeedback
.CurrentBuffer
;
140 case GL_TEXTURE_BUFFER
:
141 if (ctx
->API
== API_OPENGL_CORE
&&
142 ctx
->Extensions
.ARB_texture_buffer_object
) {
143 return &ctx
->Texture
.BufferObject
;
146 case GL_UNIFORM_BUFFER
:
147 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
148 return &ctx
->UniformBuffer
;
151 case GL_SHADER_STORAGE_BUFFER
:
152 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
153 return &ctx
->ShaderStorageBuffer
;
156 case GL_ATOMIC_COUNTER_BUFFER
:
157 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
158 return &ctx
->AtomicBuffer
;
161 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
162 if (ctx
->Extensions
.AMD_pinned_memory
) {
163 return &ctx
->ExternalVirtualMemoryBuffer
;
174 * Get the buffer object bound to the specified target in a GL context.
175 * \param ctx the GL context
176 * \param target the buffer object target to be retrieved.
177 * \param error the GL error to record if target is illegal.
178 * \return pointer to the buffer object bound to \c target in the
179 * specified context or \c NULL if \c target is invalid.
181 static inline struct gl_buffer_object
*
182 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
185 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
188 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
192 if (!_mesa_is_bufferobj(*bufObj
)) {
193 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
202 * Convert a GLbitfield describing the mapped buffer access flags
203 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
206 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
208 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
209 if ((access
& rwFlags
) == rwFlags
)
210 return GL_READ_WRITE
;
211 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
213 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
214 return GL_WRITE_ONLY
;
216 /* Otherwise, AccessFlags is zero (the default state).
218 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
220 * Name Type Initial Value Legal Values
222 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
225 * However, table 6.8 in the GL_OES_mapbuffer extension says:
227 * Get Value Type Get Command Value Description
228 * --------- ---- ----------- ----- -----------
229 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
231 * The difference is because GL_OES_mapbuffer only supports mapping buffers
236 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
241 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
243 * The regions do not overlap if and only if the end of the given
244 * region is before the mapped region or the start of the given region
245 * is after the mapped region.
247 * \param obj Buffer object target on which to operate.
248 * \param offset Offset of the first byte of the subdata range.
249 * \param size Size, in bytes, of the subdata range.
250 * \return true if ranges overlap, false otherwise
254 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
255 GLintptr offset
, GLsizeiptr size
)
257 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
258 const GLintptr end
= offset
+ size
;
259 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
260 obj
->Mappings
[MAP_USER
].Length
;
262 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
271 * Tests the subdata range parameters and sets the GL error code for
272 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
273 * \c glClearBufferSubData.
275 * \param ctx GL context.
276 * \param bufObj The buffer object.
277 * \param offset Offset of the first byte of the subdata range.
278 * \param size Size, in bytes, of the subdata range.
279 * \param mappedRange If true, checks if an overlapping range is mapped.
280 * If false, checks if buffer is mapped.
281 * \param caller Name of calling function for recording errors.
282 * \return false if error, true otherwise
284 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
287 buffer_object_subdata_range_good(struct gl_context
*ctx
,
288 const struct gl_buffer_object
*bufObj
,
289 GLintptr offset
, GLsizeiptr size
,
290 bool mappedRange
, const char *caller
)
293 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
298 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
302 if (offset
+ size
> bufObj
->Size
) {
303 _mesa_error(ctx
, GL_INVALID_VALUE
,
304 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
305 (unsigned long) offset
,
306 (unsigned long) size
,
307 (unsigned long) bufObj
->Size
);
311 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
315 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
316 _mesa_error(ctx
, GL_INVALID_OPERATION
,
317 "%s(range is mapped without persistent bit)",
323 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
324 _mesa_error(ctx
, GL_INVALID_OPERATION
,
325 "%s(buffer is mapped without persistent bit)",
336 * Test the format and type parameters and set the GL error code for
337 * \c glClearBufferData and \c glClearBufferSubData.
339 * \param ctx GL context.
340 * \param internalformat Format to which the data is to be converted.
341 * \param format Format of the supplied data.
342 * \param type Type of the supplied data.
343 * \param caller Name of calling function for recording errors.
344 * \return If internalformat, format and type are legal the mesa_format
345 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
347 * \sa glClearBufferData and glClearBufferSubData
350 validate_clear_buffer_format(struct gl_context
*ctx
,
351 GLenum internalformat
,
352 GLenum format
, GLenum type
,
355 mesa_format mesaFormat
;
356 GLenum errorFormatType
;
358 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
359 if (mesaFormat
== MESA_FORMAT_NONE
) {
360 _mesa_error(ctx
, GL_INVALID_ENUM
,
361 "%s(invalid internalformat)", caller
);
362 return MESA_FORMAT_NONE
;
365 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
366 * EXT_texture_integer there is no conversion between integer and
367 * non-integer formats
369 if (_mesa_is_enum_format_signed_int(format
) !=
370 _mesa_is_format_integer_color(mesaFormat
)) {
371 _mesa_error(ctx
, GL_INVALID_OPERATION
,
372 "%s(integer vs non-integer)", caller
);
373 return MESA_FORMAT_NONE
;
376 if (!_mesa_is_color_format(format
)) {
377 _mesa_error(ctx
, GL_INVALID_ENUM
,
378 "%s(format is not a color format)", caller
);
379 return MESA_FORMAT_NONE
;
382 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
383 if (errorFormatType
!= GL_NO_ERROR
) {
384 _mesa_error(ctx
, GL_INVALID_ENUM
,
385 "%s(invalid format or type)", caller
);
386 return MESA_FORMAT_NONE
;
394 * Convert user-specified clear value to the specified internal format.
396 * \param ctx GL context.
397 * \param internalformat Format to which the data is converted.
398 * \param clearValue Points to the converted clear value.
399 * \param format Format of the supplied data.
400 * \param type Type of the supplied data.
401 * \param data Data which is to be converted to internalformat.
402 * \param caller Name of calling function for recording errors.
403 * \return true if data could be converted, false otherwise.
405 * \sa glClearBufferData, glClearBufferSubData
408 convert_clear_buffer_data(struct gl_context
*ctx
,
409 mesa_format internalformat
,
410 GLubyte
*clearValue
, GLenum format
, GLenum type
,
411 const GLvoid
*data
, const char *caller
)
413 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
415 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
416 0, &clearValue
, 1, 1, 1,
417 format
, type
, data
, &ctx
->Unpack
)) {
421 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
428 * Allocate and initialize a new buffer object.
430 * Default callback for the \c dd_function_table::NewBufferObject() hook.
432 static struct gl_buffer_object
*
433 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
435 struct gl_buffer_object
*obj
;
439 obj
= MALLOC_STRUCT(gl_buffer_object
);
440 _mesa_initialize_buffer_object(ctx
, obj
, name
);
446 * Delete a buffer object.
448 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
451 _mesa_delete_buffer_object(struct gl_context
*ctx
,
452 struct gl_buffer_object
*bufObj
)
456 _mesa_align_free(bufObj
->Data
);
458 /* assign strange values here to help w/ debugging */
459 bufObj
->RefCount
= -1000;
462 mtx_destroy(&bufObj
->Mutex
);
470 * Set ptr to bufObj w/ reference counting.
471 * This is normally only called from the _mesa_reference_buffer_object() macro
472 * when there's a real pointer change.
475 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
476 struct gl_buffer_object
**ptr
,
477 struct gl_buffer_object
*bufObj
)
480 /* Unreference the old buffer */
481 GLboolean deleteFlag
= GL_FALSE
;
482 struct gl_buffer_object
*oldObj
= *ptr
;
484 mtx_lock(&oldObj
->Mutex
);
485 assert(oldObj
->RefCount
> 0);
487 deleteFlag
= (oldObj
->RefCount
== 0);
488 mtx_unlock(&oldObj
->Mutex
);
491 assert(ctx
->Driver
.DeleteBuffer
);
492 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
500 /* reference new buffer */
501 mtx_lock(&bufObj
->Mutex
);
502 if (bufObj
->RefCount
== 0) {
503 /* this buffer's being deleted (look just above) */
504 /* Not sure this can every really happen. Warn if it does. */
505 _mesa_problem(NULL
, "referencing deleted buffer object");
512 mtx_unlock(&bufObj
->Mutex
);
518 * Initialize a buffer object to default values.
521 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
522 struct gl_buffer_object
*obj
,
525 memset(obj
, 0, sizeof(struct gl_buffer_object
));
526 mtx_init(&obj
->Mutex
, mtx_plain
);
529 obj
->Usage
= GL_STATIC_DRAW_ARB
;
535 * Callback called from _mesa_HashWalk()
538 count_buffer_size(GLuint key
, void *data
, void *userData
)
540 const struct gl_buffer_object
*bufObj
=
541 (const struct gl_buffer_object
*) data
;
542 GLuint
*total
= (GLuint
*) userData
;
545 *total
= *total
+ bufObj
->Size
;
550 * Compute total size (in bytes) of all buffer objects for the given context.
551 * For debugging purposes.
554 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
558 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
565 * Allocate space for and store data in a buffer object. Any data that was
566 * previously stored in the buffer object is lost. If \c data is \c NULL,
567 * memory will be allocated, but no copy will occur.
569 * This is the default callback for \c dd_function_table::BufferData()
570 * Note that all GL error checking will have been done already.
572 * \param ctx GL context.
573 * \param target Buffer object target on which to operate.
574 * \param size Size, in bytes, of the new data store.
575 * \param data Pointer to the data to store in the buffer object. This
576 * pointer may be \c NULL.
577 * \param usage Hints about how the data will be used.
578 * \param bufObj Object to be used.
580 * \return GL_TRUE for success, GL_FALSE for failure
581 * \sa glBufferDataARB, dd_function_table::BufferData.
584 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptr size
,
585 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
586 struct gl_buffer_object
*bufObj
)
592 _mesa_align_free( bufObj
->Data
);
594 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
596 bufObj
->Data
= (GLubyte
*) new_data
;
598 bufObj
->Usage
= usage
;
599 bufObj
->StorageFlags
= storageFlags
;
602 memcpy( bufObj
->Data
, data
, size
);
614 * Replace data in a subrange of buffer object. If the data range
615 * specified by \c size + \c offset extends beyond the end of the buffer or
616 * if \c data is \c NULL, no copy is performed.
618 * This is the default callback for \c dd_function_table::BufferSubData()
619 * Note that all GL error checking will have been done already.
621 * \param ctx GL context.
622 * \param offset Offset of the first byte to be modified.
623 * \param size Size, in bytes, of the data range.
624 * \param data Pointer to the data to store in the buffer object.
625 * \param bufObj Object to be used.
627 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
630 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptr offset
,
631 GLsizeiptr size
, const GLvoid
*data
,
632 struct gl_buffer_object
*bufObj
)
636 /* this should have been caught in _mesa_BufferSubData() */
637 assert(size
+ offset
<= bufObj
->Size
);
640 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
646 * Retrieve data from a subrange of buffer object. If the data range
647 * specified by \c size + \c offset extends beyond the end of the buffer or
648 * if \c data is \c NULL, no copy is performed.
650 * This is the default callback for \c dd_function_table::GetBufferSubData()
651 * Note that all GL error checking will have been done already.
653 * \param ctx GL context.
654 * \param target Buffer object target on which to operate.
655 * \param offset Offset of the first byte to be fetched.
656 * \param size Size, in bytes, of the data range.
657 * \param data Destination for data
658 * \param bufObj Object to be used.
660 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
663 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
664 GLsizeiptrARB size
, GLvoid
* data
,
665 struct gl_buffer_object
* bufObj
)
669 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
670 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
676 * Clear a subrange of the buffer object with copies of the supplied data.
677 * If data is NULL the buffer is filled with zeros.
679 * This is the default callback for \c dd_function_table::ClearBufferSubData()
680 * Note that all GL error checking will have been done already.
682 * \param ctx GL context.
683 * \param offset Offset of the first byte to be cleared.
684 * \param size Size, in bytes, of the to be cleared range.
685 * \param clearValue Source of the data.
686 * \param clearValueSize Size, in bytes, of the supplied data.
687 * \param bufObj Object to be cleared.
689 * \sa glClearBufferSubData, glClearBufferData and
690 * dd_function_table::ClearBufferSubData.
693 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
694 GLintptr offset
, GLsizeiptr size
,
695 const GLvoid
*clearValue
,
696 GLsizeiptr clearValueSize
,
697 struct gl_buffer_object
*bufObj
)
702 assert(ctx
->Driver
.MapBufferRange
);
703 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
705 GL_MAP_INVALIDATE_RANGE_BIT
,
706 bufObj
, MAP_INTERNAL
);
709 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
713 if (clearValue
== NULL
) {
714 /* Clear with zeros, per the spec */
715 memset(dest
, 0, size
);
716 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
720 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
721 memcpy(dest
, clearValue
, clearValueSize
);
722 dest
+= clearValueSize
;
725 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
730 * Default fallback for \c dd_function_table::MapBufferRange().
731 * Called via glMapBufferRange().
734 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
735 GLsizeiptr length
, GLbitfield access
,
736 struct gl_buffer_object
*bufObj
,
737 gl_map_buffer_index index
)
740 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
741 /* Just return a direct pointer to the data */
742 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
743 bufObj
->Mappings
[index
].Length
= length
;
744 bufObj
->Mappings
[index
].Offset
= offset
;
745 bufObj
->Mappings
[index
].AccessFlags
= access
;
746 return bufObj
->Mappings
[index
].Pointer
;
751 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
752 * Called via glFlushMappedBufferRange().
755 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
756 GLintptr offset
, GLsizeiptr length
,
757 struct gl_buffer_object
*obj
,
758 gl_map_buffer_index index
)
770 * Default callback for \c dd_function_table::UnmapBuffer().
772 * The input parameters will have been already tested for errors.
774 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
777 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
778 gl_map_buffer_index index
)
781 /* XXX we might assert here that bufObj->Pointer is non-null */
782 bufObj
->Mappings
[index
].Pointer
= NULL
;
783 bufObj
->Mappings
[index
].Length
= 0;
784 bufObj
->Mappings
[index
].Offset
= 0;
785 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
791 * Default fallback for \c dd_function_table::CopyBufferSubData().
792 * Called via glCopyBufferSubData().
795 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
796 struct gl_buffer_object
*src
,
797 struct gl_buffer_object
*dst
,
798 GLintptr readOffset
, GLintptr writeOffset
,
801 GLubyte
*srcPtr
, *dstPtr
;
804 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
806 GL_MAP_WRITE_BIT
, src
,
812 srcPtr
+= readOffset
;
813 dstPtr
+= writeOffset
;
815 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
816 GL_MAP_READ_BIT
, src
,
818 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
820 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
824 /* Note: the src and dst regions will never overlap. Trying to do so
825 * would generate GL_INVALID_VALUE earlier.
827 if (srcPtr
&& dstPtr
)
828 memcpy(dstPtr
, srcPtr
, size
);
830 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
832 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
838 * Initialize the state associated with buffer objects
841 _mesa_init_buffer_objects( struct gl_context
*ctx
)
845 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
846 mtx_init(&DummyBufferObject
.Mutex
, mtx_plain
);
847 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
849 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
850 ctx
->Shared
->NullBufferObj
);
852 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
853 ctx
->Shared
->NullBufferObj
);
854 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
855 ctx
->Shared
->NullBufferObj
);
857 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
858 ctx
->Shared
->NullBufferObj
);
860 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
,
861 ctx
->Shared
->NullBufferObj
);
863 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
864 ctx
->Shared
->NullBufferObj
);
866 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
867 ctx
->Shared
->NullBufferObj
);
869 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
,
870 ctx
->Shared
->NullBufferObj
);
872 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
873 _mesa_reference_buffer_object(ctx
,
874 &ctx
->UniformBufferBindings
[i
].BufferObject
,
875 ctx
->Shared
->NullBufferObj
);
876 ctx
->UniformBufferBindings
[i
].Offset
= -1;
877 ctx
->UniformBufferBindings
[i
].Size
= -1;
880 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
881 _mesa_reference_buffer_object(ctx
,
882 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
883 ctx
->Shared
->NullBufferObj
);
884 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
885 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
888 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
889 _mesa_reference_buffer_object(ctx
,
890 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
891 ctx
->Shared
->NullBufferObj
);
892 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
893 ctx
->AtomicBufferBindings
[i
].Size
= 0;
899 _mesa_free_buffer_objects( struct gl_context
*ctx
)
903 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
905 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
906 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
908 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
910 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
912 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
914 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
916 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, NULL
);
918 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
919 _mesa_reference_buffer_object(ctx
,
920 &ctx
->UniformBufferBindings
[i
].BufferObject
,
924 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
925 _mesa_reference_buffer_object(ctx
,
926 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
930 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
931 _mesa_reference_buffer_object(ctx
,
932 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
939 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
941 struct gl_buffer_object
**buf_handle
,
944 struct gl_buffer_object
*buf
= *buf_handle
;
946 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
|| _mesa_is_gles31(ctx
))) {
947 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
951 if (!buf
|| buf
== &DummyBufferObject
) {
952 /* If this is a new buffer object id, or one which was generated but
953 * never used before, allocate a buffer object now.
955 assert(ctx
->Driver
.NewBufferObject
);
956 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
958 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
961 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
969 * Bind the specified target to buffer for the specified context.
970 * Called by glBindBuffer() and other functions.
973 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
975 struct gl_buffer_object
*oldBufObj
;
976 struct gl_buffer_object
*newBufObj
= NULL
;
977 struct gl_buffer_object
**bindTarget
= NULL
;
979 bindTarget
= get_buffer_target(ctx
, target
);
981 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
985 /* Get pointer to old buffer object (to be unbound) */
986 oldBufObj
= *bindTarget
;
987 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
988 return; /* rebinding the same buffer object- no change */
991 * Get pointer to new buffer object (newBufObj)
994 /* The spec says there's not a buffer object named 0, but we use
995 * one internally because it simplifies things.
997 newBufObj
= ctx
->Shared
->NullBufferObj
;
1000 /* non-default buffer object */
1001 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1002 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1003 &newBufObj
, "glBindBuffer"))
1007 /* bind new buffer */
1008 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1013 * Update the default buffer objects in the given context to reference those
1014 * specified in the shared state and release those referencing the old
1018 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1020 /* Bind the NullBufferObj to remove references to those
1021 * in the shared context hash table.
1023 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
1024 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
1025 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
1026 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1032 * Return the gl_buffer_object for the given ID.
1033 * Always return NULL for ID 0.
1035 struct gl_buffer_object
*
1036 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1041 return (struct gl_buffer_object
*)
1042 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1046 struct gl_buffer_object
*
1047 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1049 return (struct gl_buffer_object
*)
1050 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1054 * A convenience function for direct state access functions that throws
1055 * GL_INVALID_OPERATION if buffer is not the name of an existing
1058 struct gl_buffer_object
*
1059 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1062 struct gl_buffer_object
*bufObj
;
1064 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1065 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1066 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1067 "%s(non-existent buffer object %u)", caller
, buffer
);
1076 _mesa_begin_bufferobj_lookups(struct gl_context
*ctx
)
1078 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1083 _mesa_end_bufferobj_lookups(struct gl_context
*ctx
)
1085 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1090 * Look up a buffer object for a multi-bind function.
1092 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1093 * of generating an error if the buffer ID is not zero or the name
1094 * of an existing buffer object.
1096 * If the buffer ID refers to an existing buffer object, a pointer
1097 * to the buffer object is returned. If the ID is zero, a pointer
1098 * to the shared NullBufferObj is returned. If the ID is not zero
1099 * and does not refer to a valid buffer object, this function
1102 * This function assumes that the caller has already locked the
1103 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1105 struct gl_buffer_object
*
1106 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1107 const GLuint
*buffers
,
1108 GLuint index
, const char *caller
)
1110 struct gl_buffer_object
*bufObj
;
1112 if (buffers
[index
] != 0) {
1113 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1115 /* The multi-bind functions don't create the buffer objects
1116 when they don't exist. */
1117 if (bufObj
== &DummyBufferObject
)
1120 bufObj
= ctx
->Shared
->NullBufferObj
;
1123 /* The ARB_multi_bind spec says:
1125 * "An INVALID_OPERATION error is generated if any value
1126 * in <buffers> is not zero or the name of an existing
1127 * buffer object (per binding)."
1129 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1130 "%s(buffers[%u]=%u is not zero or the name "
1131 "of an existing buffer object)",
1132 caller
, index
, buffers
[index
]);
1140 * If *ptr points to obj, set ptr = the Null/default buffer object.
1141 * This is a helper for buffer object deletion.
1142 * The GL spec says that deleting a buffer object causes it to get
1143 * unbound from all arrays in the current context.
1146 unbind(struct gl_context
*ctx
,
1147 struct gl_buffer_object
**ptr
,
1148 struct gl_buffer_object
*obj
)
1151 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
1157 * Plug default/fallback buffer object functions into the device
1161 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1163 /* GL_ARB_vertex/pixel_buffer_object */
1164 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1165 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1166 driver
->BufferData
= buffer_data_fallback
;
1167 driver
->BufferSubData
= buffer_sub_data_fallback
;
1168 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
1169 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1171 /* GL_ARB_clear_buffer_object */
1172 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1174 /* GL_ARB_map_buffer_range */
1175 driver
->MapBufferRange
= map_buffer_range_fallback
;
1176 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1178 /* GL_ARB_copy_buffer */
1179 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1184 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1185 struct gl_buffer_object
*bufObj
)
1189 for (i
= 0; i
< MAP_COUNT
; i
++) {
1190 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1191 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1192 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1193 bufObj
->Mappings
[i
].AccessFlags
= 0;
1199 /**********************************************************************/
1201 /**********************************************************************/
1204 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1206 GET_CURRENT_CONTEXT(ctx
);
1208 if (MESA_VERBOSE
& VERBOSE_API
) {
1209 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1210 _mesa_enum_to_string(target
), buffer
);
1213 bind_buffer_object(ctx
, target
, buffer
);
1218 * Delete a set of buffer objects.
1220 * \param n Number of buffer objects to delete.
1221 * \param ids Array of \c n buffer object IDs.
1224 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1226 GET_CURRENT_CONTEXT(ctx
);
1228 FLUSH_VERTICES(ctx
, 0);
1231 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1235 mtx_lock(&ctx
->Shared
->Mutex
);
1237 for (i
= 0; i
< n
; i
++) {
1238 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
1240 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1243 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1245 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1247 /* unbind any vertex pointers bound to this buffer */
1248 for (j
= 0; j
< ARRAY_SIZE(vao
->VertexBinding
); j
++) {
1249 unbind(ctx
, &vao
->VertexBinding
[j
].BufferObj
, bufObj
);
1252 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1253 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
1255 if (vao
->IndexBufferObj
== bufObj
) {
1256 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1259 /* unbind ARB_draw_indirect binding point */
1260 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1261 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER
, 0 );
1264 /* unbind ARB_compute_shader binding point */
1265 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1266 _mesa_BindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, 0);
1269 /* unbind ARB_copy_buffer binding points */
1270 if (ctx
->CopyReadBuffer
== bufObj
) {
1271 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
1273 if (ctx
->CopyWriteBuffer
== bufObj
) {
1274 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
1277 /* unbind transform feedback binding points */
1278 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1279 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
1281 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1282 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1283 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1287 /* unbind UBO binding points */
1288 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1289 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1290 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1294 if (ctx
->UniformBuffer
== bufObj
) {
1295 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
1298 /* unbind SSBO binding points */
1299 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1300 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1301 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER
, j
, 0);
1305 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1306 _mesa_BindBuffer(GL_SHADER_STORAGE_BUFFER
, 0);
1309 /* unbind Atomci Buffer binding points */
1310 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1311 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1312 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1316 if (ctx
->AtomicBuffer
== bufObj
) {
1317 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER
, 0 );
1320 /* unbind any pixel pack/unpack pointers bound to this buffer */
1321 if (ctx
->Pack
.BufferObj
== bufObj
) {
1322 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1324 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1325 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1328 if (ctx
->Texture
.BufferObject
== bufObj
) {
1329 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
1332 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1333 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
, 0);
1336 /* The ID is immediately freed for re-use */
1337 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
1338 /* Make sure we do not run into the classic ABA problem on bind.
1339 * We don't want to allow re-binding a buffer object that's been
1340 * "deleted" by glDeleteBuffers().
1342 * The explicit rebinding to the default object in the current context
1343 * prevents the above in the current context, but another context
1344 * sharing the same objects might suffer from this problem.
1345 * The alternative would be to do the hash lookup in any case on bind
1346 * which would introduce more runtime overhead than this.
1348 bufObj
->DeletePending
= GL_TRUE
;
1349 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1353 mtx_unlock(&ctx
->Shared
->Mutex
);
1358 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1359 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1363 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1365 GET_CURRENT_CONTEXT(ctx
);
1368 struct gl_buffer_object
*buf
;
1370 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1372 if (MESA_VERBOSE
& VERBOSE_API
)
1373 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1376 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1385 * This must be atomic (generation and allocation of buffer object IDs)
1387 mtx_lock(&ctx
->Shared
->Mutex
);
1389 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1391 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1392 * DummyBufferObject. Otherwise, create a new buffer object and insert
1395 for (i
= 0; i
< n
; i
++) {
1396 buffers
[i
] = first
+ i
;
1398 assert(ctx
->Driver
.NewBufferObject
);
1399 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1401 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1402 mtx_unlock(&ctx
->Shared
->Mutex
);
1407 buf
= &DummyBufferObject
;
1409 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1412 mtx_unlock(&ctx
->Shared
->Mutex
);
1416 * Generate a set of unique buffer object IDs and store them in \c buffers.
1418 * \param n Number of IDs to generate.
1419 * \param buffers Array of \c n locations to store the IDs.
1422 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1424 create_buffers(n
, buffers
, false);
1428 * Create a set of buffer objects and store their unique IDs in \c buffers.
1430 * \param n Number of IDs to generate.
1431 * \param buffers Array of \c n locations to store the IDs.
1434 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1436 create_buffers(n
, buffers
, true);
1441 * Determine if ID is the name of a buffer object.
1443 * \param id ID of the potential buffer object.
1444 * \return \c GL_TRUE if \c id is the name of a buffer object,
1445 * \c GL_FALSE otherwise.
1447 GLboolean GLAPIENTRY
1448 _mesa_IsBuffer(GLuint id
)
1450 struct gl_buffer_object
*bufObj
;
1451 GET_CURRENT_CONTEXT(ctx
);
1452 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1454 mtx_lock(&ctx
->Shared
->Mutex
);
1455 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1456 mtx_unlock(&ctx
->Shared
->Mutex
);
1458 return bufObj
&& bufObj
!= &DummyBufferObject
;
1463 _mesa_buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1464 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1465 GLbitfield flags
, const char *func
)
1468 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1472 if (flags
& ~(GL_MAP_READ_BIT
|
1474 GL_MAP_PERSISTENT_BIT
|
1475 GL_MAP_COHERENT_BIT
|
1476 GL_DYNAMIC_STORAGE_BIT
|
1477 GL_CLIENT_STORAGE_BIT
)) {
1478 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1482 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1483 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1484 _mesa_error(ctx
, GL_INVALID_VALUE
,
1485 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1489 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1490 _mesa_error(ctx
, GL_INVALID_VALUE
,
1491 "%s(COHERENT and flags!=PERSISTENT)", func
);
1495 if (bufObj
->Immutable
) {
1496 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1500 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1501 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1503 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1505 bufObj
->Written
= GL_TRUE
;
1506 bufObj
->Immutable
= GL_TRUE
;
1508 assert(ctx
->Driver
.BufferData
);
1509 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1511 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1512 /* Even though the interaction between AMD_pinned_memory and
1513 * glBufferStorage is not described in the spec, Graham Sellers
1514 * said that it should behave the same as glBufferData.
1516 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1519 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1525 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1528 GET_CURRENT_CONTEXT(ctx
);
1529 struct gl_buffer_object
*bufObj
;
1531 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1535 _mesa_buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
,
1540 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1543 GET_CURRENT_CONTEXT(ctx
);
1544 struct gl_buffer_object
*bufObj
;
1546 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferStorage");
1551 * In direct state access, buffer objects have an unspecified target since
1552 * they are not required to be bound.
1554 _mesa_buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
,
1555 "glNamedBufferStorage");
1560 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1561 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1562 GLenum usage
, const char *func
)
1566 if (MESA_VERBOSE
& VERBOSE_API
) {
1567 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1569 _mesa_enum_to_string(target
),
1570 (long int) size
, data
,
1571 _mesa_enum_to_string(usage
));
1575 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1580 case GL_STREAM_DRAW_ARB
:
1581 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1584 case GL_STATIC_DRAW_ARB
:
1585 case GL_DYNAMIC_DRAW_ARB
:
1589 case GL_STREAM_READ_ARB
:
1590 case GL_STREAM_COPY_ARB
:
1591 case GL_STATIC_READ_ARB
:
1592 case GL_STATIC_COPY_ARB
:
1593 case GL_DYNAMIC_READ_ARB
:
1594 case GL_DYNAMIC_COPY_ARB
:
1595 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1599 valid_usage
= false;
1604 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1605 _mesa_enum_to_string(usage
));
1609 if (bufObj
->Immutable
) {
1610 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1614 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1615 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1617 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1619 bufObj
->Written
= GL_TRUE
;
1622 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1623 bufObj
->Name
, size
, data
, usage
);
1630 assert(ctx
->Driver
.BufferData
);
1631 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1634 GL_DYNAMIC_STORAGE_BIT
,
1636 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1637 /* From GL_AMD_pinned_memory:
1639 * INVALID_OPERATION is generated by BufferData if <target> is
1640 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1641 * mapped to the GPU address space.
1643 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1646 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1652 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1653 const GLvoid
*data
, GLenum usage
)
1655 GET_CURRENT_CONTEXT(ctx
);
1656 struct gl_buffer_object
*bufObj
;
1658 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1662 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1667 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1670 GET_CURRENT_CONTEXT(ctx
);
1671 struct gl_buffer_object
*bufObj
;
1673 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1677 /* In direct state access, buffer objects have an unspecified target since
1678 * they are not required to be bound.
1680 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1681 "glNamedBufferData");
1686 * Implementation for glBufferSubData and glNamedBufferSubData.
1688 * \param ctx GL context.
1689 * \param bufObj The buffer object.
1690 * \param offset Offset of the first byte of the subdata range.
1691 * \param size Size, in bytes, of the subdata range.
1692 * \param data The data store.
1693 * \param func Name of calling function for recording errors.
1697 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1698 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1701 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1703 /* error already recorded */
1707 if (bufObj
->Immutable
&&
1708 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1709 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1716 bufObj
->NumSubDataCalls
++;
1718 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
1719 bufObj
->Usage
== GL_STATIC_COPY
) &&
1720 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
) {
1721 /* If the application declared the buffer as static draw/copy or stream
1722 * draw, it should not be frequently modified with glBufferSubData.
1724 BUFFER_USAGE_WARNING(ctx
,
1725 "using %s(buffer %u, offset %u, size %u) to "
1726 "update a %s buffer",
1727 func
, bufObj
->Name
, offset
, size
,
1728 _mesa_enum_to_string(bufObj
->Usage
));
1731 bufObj
->Written
= GL_TRUE
;
1733 assert(ctx
->Driver
.BufferSubData
);
1734 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1738 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1739 GLsizeiptr size
, const GLvoid
*data
)
1741 GET_CURRENT_CONTEXT(ctx
);
1742 struct gl_buffer_object
*bufObj
;
1744 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1748 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1752 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1753 GLsizeiptr size
, const GLvoid
*data
)
1755 GET_CURRENT_CONTEXT(ctx
);
1756 struct gl_buffer_object
*bufObj
;
1758 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1762 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1763 "glNamedBufferSubData");
1768 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1769 GLsizeiptr size
, GLvoid
*data
)
1771 GET_CURRENT_CONTEXT(ctx
);
1772 struct gl_buffer_object
*bufObj
;
1774 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1775 GL_INVALID_OPERATION
);
1779 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1780 "glGetBufferSubData")) {
1784 assert(ctx
->Driver
.GetBufferSubData
);
1785 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1789 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
1790 GLsizeiptr size
, GLvoid
*data
)
1792 GET_CURRENT_CONTEXT(ctx
);
1793 struct gl_buffer_object
*bufObj
;
1795 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1796 "glGetNamedBufferSubData");
1800 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1801 "glGetNamedBufferSubData")) {
1805 assert(ctx
->Driver
.GetBufferSubData
);
1806 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1811 * \param subdata true if caller is *SubData, false if *Data
1814 _mesa_clear_buffer_sub_data(struct gl_context
*ctx
,
1815 struct gl_buffer_object
*bufObj
,
1816 GLenum internalformat
,
1817 GLintptr offset
, GLsizeiptr size
,
1818 GLenum format
, GLenum type
,
1820 const char *func
, bool subdata
)
1822 mesa_format mesaFormat
;
1823 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1824 GLsizeiptr clearValueSize
;
1826 /* This checks for disallowed mappings. */
1827 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1832 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1833 format
, type
, func
);
1835 if (mesaFormat
== MESA_FORMAT_NONE
) {
1839 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1840 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1841 _mesa_error(ctx
, GL_INVALID_VALUE
,
1842 "%s(offset or size is not a multiple of "
1843 "internalformat size)", func
);
1848 /* clear to zeros, per the spec */
1850 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1851 NULL
, clearValueSize
, bufObj
);
1856 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1857 format
, type
, data
, func
)) {
1862 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1863 clearValue
, clearValueSize
, bufObj
);
1868 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1869 GLenum type
, const GLvoid
*data
)
1871 GET_CURRENT_CONTEXT(ctx
);
1872 struct gl_buffer_object
*bufObj
;
1874 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1878 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1880 "glClearBufferData", false);
1884 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1885 GLenum format
, GLenum type
, const GLvoid
*data
)
1887 GET_CURRENT_CONTEXT(ctx
);
1888 struct gl_buffer_object
*bufObj
;
1890 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1894 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1896 "glClearNamedBufferData", false);
1901 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1902 GLintptr offset
, GLsizeiptr size
,
1903 GLenum format
, GLenum type
,
1906 GET_CURRENT_CONTEXT(ctx
);
1907 struct gl_buffer_object
*bufObj
;
1909 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1913 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1915 "glClearBufferSubData", true);
1919 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1920 GLintptr offset
, GLsizeiptr size
,
1921 GLenum format
, GLenum type
,
1924 GET_CURRENT_CONTEXT(ctx
);
1925 struct gl_buffer_object
*bufObj
;
1927 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1928 "glClearNamedBufferSubData");
1932 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1934 "glClearNamedBufferSubData", true);
1939 _mesa_unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1942 GLboolean status
= GL_TRUE
;
1943 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1945 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
1946 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1947 "%s(buffer is not mapped)", func
);
1952 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1953 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1955 /* check that last 100 bytes are still = magic value */
1956 for (i
= 0; i
< 100; i
++) {
1957 GLuint pos
= bufObj
->Size
- i
- 1;
1958 if (buf
[pos
] != 123) {
1959 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1960 " at position %d (value = %u)\n",
1968 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1969 GLuint i
, unchanged
= 0;
1970 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1972 /* check which bytes changed */
1973 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1974 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1981 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1982 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1987 status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
1988 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
1989 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
1990 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
1991 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
1996 GLboolean GLAPIENTRY
1997 _mesa_UnmapBuffer(GLenum target
)
1999 GET_CURRENT_CONTEXT(ctx
);
2000 struct gl_buffer_object
*bufObj
;
2002 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2006 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2009 GLboolean GLAPIENTRY
2010 _mesa_UnmapNamedBuffer(GLuint buffer
)
2012 GET_CURRENT_CONTEXT(ctx
);
2013 struct gl_buffer_object
*bufObj
;
2015 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2019 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2024 get_buffer_parameter(struct gl_context
*ctx
,
2025 struct gl_buffer_object
*bufObj
, GLenum pname
,
2026 GLint64
*params
, const char *func
)
2029 case GL_BUFFER_SIZE_ARB
:
2030 *params
= bufObj
->Size
;
2032 case GL_BUFFER_USAGE_ARB
:
2033 *params
= bufObj
->Usage
;
2035 case GL_BUFFER_ACCESS_ARB
:
2036 *params
= simplified_access_mode(ctx
,
2037 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2039 case GL_BUFFER_MAPPED_ARB
:
2040 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2042 case GL_BUFFER_ACCESS_FLAGS
:
2043 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2045 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2047 case GL_BUFFER_MAP_OFFSET
:
2048 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2050 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2052 case GL_BUFFER_MAP_LENGTH
:
2053 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2055 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2057 case GL_BUFFER_IMMUTABLE_STORAGE
:
2058 if (!ctx
->Extensions
.ARB_buffer_storage
)
2060 *params
= bufObj
->Immutable
;
2062 case GL_BUFFER_STORAGE_FLAGS
:
2063 if (!ctx
->Extensions
.ARB_buffer_storage
)
2065 *params
= bufObj
->StorageFlags
;
2074 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2075 _mesa_enum_to_string(pname
));
2080 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2082 GET_CURRENT_CONTEXT(ctx
);
2083 struct gl_buffer_object
*bufObj
;
2086 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2087 GL_INVALID_OPERATION
);
2091 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2092 "glGetBufferParameteriv"))
2093 return; /* Error already recorded. */
2095 *params
= (GLint
) parameter
;
2099 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2101 GET_CURRENT_CONTEXT(ctx
);
2102 struct gl_buffer_object
*bufObj
;
2105 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2106 GL_INVALID_OPERATION
);
2110 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2111 "glGetBufferParameteri64v"))
2112 return; /* Error already recorded. */
2114 *params
= parameter
;
2118 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2120 GET_CURRENT_CONTEXT(ctx
);
2121 struct gl_buffer_object
*bufObj
;
2124 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2125 "glGetNamedBufferParameteriv");
2129 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2130 "glGetNamedBufferParameteriv"))
2131 return; /* Error already recorded. */
2133 *params
= (GLint
) parameter
;
2137 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2140 GET_CURRENT_CONTEXT(ctx
);
2141 struct gl_buffer_object
*bufObj
;
2144 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2145 "glGetNamedBufferParameteri64v");
2149 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2150 "glGetNamedBufferParameteri64v"))
2151 return; /* Error already recorded. */
2153 *params
= parameter
;
2158 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2160 GET_CURRENT_CONTEXT(ctx
);
2161 struct gl_buffer_object
*bufObj
;
2163 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2164 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2165 "GL_BUFFER_MAP_POINTER)");
2169 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2170 GL_INVALID_OPERATION
);
2174 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2178 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2180 GET_CURRENT_CONTEXT(ctx
);
2181 struct gl_buffer_object
*bufObj
;
2183 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2184 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2185 "GL_BUFFER_MAP_POINTER)");
2189 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2190 "glGetNamedBufferPointerv");
2194 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2199 _mesa_copy_buffer_sub_data(struct gl_context
*ctx
,
2200 struct gl_buffer_object
*src
,
2201 struct gl_buffer_object
*dst
,
2202 GLintptr readOffset
, GLintptr writeOffset
,
2203 GLsizeiptr size
, const char *func
)
2205 if (_mesa_check_disallowed_mapping(src
)) {
2206 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2207 "%s(readBuffer is mapped)", func
);
2211 if (_mesa_check_disallowed_mapping(dst
)) {
2212 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2213 "%s(writeBuffer is mapped)", func
);
2217 if (readOffset
< 0) {
2218 _mesa_error(ctx
, GL_INVALID_VALUE
,
2219 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2223 if (writeOffset
< 0) {
2224 _mesa_error(ctx
, GL_INVALID_VALUE
,
2225 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2230 _mesa_error(ctx
, GL_INVALID_VALUE
,
2231 "%s(size %d < 0)", func
, (int) size
);
2235 if (readOffset
+ size
> src
->Size
) {
2236 _mesa_error(ctx
, GL_INVALID_VALUE
,
2237 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2238 (int) readOffset
, (int) size
, (int) src
->Size
);
2242 if (writeOffset
+ size
> dst
->Size
) {
2243 _mesa_error(ctx
, GL_INVALID_VALUE
,
2244 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2245 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2250 if (readOffset
+ size
<= writeOffset
) {
2253 else if (writeOffset
+ size
<= readOffset
) {
2257 /* overlapping src/dst is illegal */
2258 _mesa_error(ctx
, GL_INVALID_VALUE
,
2259 "%s(overlapping src/dst)", func
);
2264 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2268 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2269 GLintptr readOffset
, GLintptr writeOffset
,
2272 GET_CURRENT_CONTEXT(ctx
);
2273 struct gl_buffer_object
*src
, *dst
;
2275 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2276 GL_INVALID_OPERATION
);
2280 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2281 GL_INVALID_OPERATION
);
2285 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2286 "glCopyBufferSubData");
2290 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2291 GLintptr readOffset
, GLintptr writeOffset
,
2294 GET_CURRENT_CONTEXT(ctx
);
2295 struct gl_buffer_object
*src
, *dst
;
2297 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2298 "glCopyNamedBufferSubData");
2302 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2303 "glCopyNamedBufferSubData");
2307 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2308 "glCopyNamedBufferSubData");
2313 _mesa_map_buffer_range(struct gl_context
*ctx
,
2314 struct gl_buffer_object
*bufObj
,
2315 GLintptr offset
, GLsizeiptr length
,
2316 GLbitfield access
, const char *func
)
2319 GLbitfield allowed_access
;
2321 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
2324 _mesa_error(ctx
, GL_INVALID_VALUE
,
2325 "%s(offset %ld < 0)", func
, (long) offset
);
2330 _mesa_error(ctx
, GL_INVALID_VALUE
,
2331 "%s(length %ld < 0)", func
, (long) length
);
2335 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2337 * "An INVALID_OPERATION error is generated for any of the following
2340 * * <length> is zero."
2342 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2343 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2347 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2351 allowed_access
= GL_MAP_READ_BIT
|
2353 GL_MAP_INVALIDATE_RANGE_BIT
|
2354 GL_MAP_INVALIDATE_BUFFER_BIT
|
2355 GL_MAP_FLUSH_EXPLICIT_BIT
|
2356 GL_MAP_UNSYNCHRONIZED_BIT
;
2358 if (ctx
->Extensions
.ARB_buffer_storage
) {
2359 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2360 GL_MAP_COHERENT_BIT
;
2363 if (access
& ~allowed_access
) {
2364 /* generate an error if any bits other than those allowed are set */
2365 _mesa_error(ctx
, GL_INVALID_VALUE
,
2366 "%s(access has undefined bits set)", func
);
2370 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2371 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2372 "%s(access indicates neither read or write)", func
);
2376 if ((access
& GL_MAP_READ_BIT
) &&
2377 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2378 GL_MAP_INVALIDATE_BUFFER_BIT
|
2379 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2380 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2381 "%s(read access with disallowed bits)", func
);
2385 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2386 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2387 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2388 "%s(access has flush explicit without write)", func
);
2392 if (access
& GL_MAP_READ_BIT
&&
2393 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2394 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2395 "%s(buffer does not allow read access)", func
);
2399 if (access
& GL_MAP_WRITE_BIT
&&
2400 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2401 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2402 "%s(buffer does not allow write access)", func
);
2406 if (access
& GL_MAP_COHERENT_BIT
&&
2407 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2408 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2409 "%s(buffer does not allow coherent access)", func
);
2413 if (access
& GL_MAP_PERSISTENT_BIT
&&
2414 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2415 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2416 "%s(buffer does not allow persistent access)", func
);
2420 if (offset
+ length
> bufObj
->Size
) {
2421 _mesa_error(ctx
, GL_INVALID_VALUE
,
2422 "%s(offset %td + length %td > buffer_size %td)", func
,
2423 offset
, length
, bufObj
->Size
);
2427 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2428 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2429 "%s(buffer already mapped)", func
);
2433 if (!bufObj
->Size
) {
2434 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2438 if (access
& GL_MAP_WRITE_BIT
) {
2439 bufObj
->NumMapBufferWriteCalls
++;
2440 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2441 bufObj
->Usage
== GL_STATIC_COPY
) &&
2442 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
2443 BUFFER_USAGE_WARNING(ctx
,
2444 "using %s(buffer %u, offset %u, length %u) to "
2445 "update a %s buffer",
2446 func
, bufObj
->Name
, offset
, length
,
2447 _mesa_enum_to_string(bufObj
->Usage
));
2451 assert(ctx
->Driver
.MapBufferRange
);
2452 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2455 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2458 /* The driver callback should have set all these fields.
2459 * This is important because other modules (like VBO) might call
2460 * the driver function directly.
2462 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2463 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2464 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2465 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2468 if (access
& GL_MAP_WRITE_BIT
)
2469 bufObj
->Written
= GL_TRUE
;
2472 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2473 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2474 bufObj
->Name
, bufObj
->Size
, access
);
2475 /* Access must be write only */
2476 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2478 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2479 for (i
= 0; i
< bufObj
->Size
; i
++)
2486 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2487 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2489 /* buffer is 100 bytes larger than requested, fill with magic value */
2490 for (i
= 0; i
< 100; i
++) {
2491 buf
[bufObj
->Size
- i
- 1] = 123;
2500 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2503 GET_CURRENT_CONTEXT(ctx
);
2504 struct gl_buffer_object
*bufObj
;
2506 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2507 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2508 "glMapBufferRange(ARB_map_buffer_range not supported)");
2512 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2516 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2517 "glMapBufferRange");
2521 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2524 GET_CURRENT_CONTEXT(ctx
);
2525 struct gl_buffer_object
*bufObj
;
2527 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2528 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2529 "glMapNamedBufferRange("
2530 "ARB_map_buffer_range not supported)");
2534 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2538 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2539 "glMapNamedBufferRange");
2543 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2544 * flags for input to _mesa_map_buffer_range.
2546 * \return true if the type of requested access is permissible.
2549 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2553 case GL_READ_ONLY_ARB
:
2554 *flags
= GL_MAP_READ_BIT
;
2555 return _mesa_is_desktop_gl(ctx
);
2556 case GL_WRITE_ONLY_ARB
:
2557 *flags
= GL_MAP_WRITE_BIT
;
2559 case GL_READ_WRITE_ARB
:
2560 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2561 return _mesa_is_desktop_gl(ctx
);
2568 _mesa_MapBuffer(GLenum target
, GLenum access
)
2570 GET_CURRENT_CONTEXT(ctx
);
2571 struct gl_buffer_object
*bufObj
;
2572 GLbitfield accessFlags
;
2574 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2575 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2579 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2583 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2588 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2590 GET_CURRENT_CONTEXT(ctx
);
2591 struct gl_buffer_object
*bufObj
;
2592 GLbitfield accessFlags
;
2594 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2595 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2599 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2603 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2604 "glMapNamedBuffer");
2609 _mesa_flush_mapped_buffer_range(struct gl_context
*ctx
,
2610 struct gl_buffer_object
*bufObj
,
2611 GLintptr offset
, GLsizeiptr length
,
2614 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2615 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2616 "%s(ARB_map_buffer_range not supported)", func
);
2621 _mesa_error(ctx
, GL_INVALID_VALUE
,
2622 "%s(offset %ld < 0)", func
, (long) offset
);
2627 _mesa_error(ctx
, GL_INVALID_VALUE
,
2628 "%s(length %ld < 0)", func
, (long) length
);
2632 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2633 /* buffer is not mapped */
2634 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2635 "%s(buffer is not mapped)", func
);
2639 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2640 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2641 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2642 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2646 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2647 _mesa_error(ctx
, GL_INVALID_VALUE
,
2648 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2649 (long) offset
, (long) length
,
2650 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2654 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2656 if (ctx
->Driver
.FlushMappedBufferRange
)
2657 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2662 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2665 GET_CURRENT_CONTEXT(ctx
);
2666 struct gl_buffer_object
*bufObj
;
2668 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2669 GL_INVALID_OPERATION
);
2673 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2674 "glFlushMappedBufferRange");
2678 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2681 GET_CURRENT_CONTEXT(ctx
);
2682 struct gl_buffer_object
*bufObj
;
2684 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2685 "glFlushMappedNamedBufferRange");
2689 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2690 "glFlushMappedNamedBufferRange");
2695 * Binds a buffer object to a uniform buffer binding point.
2697 * The caller is responsible for flushing vertices and updating
2701 set_ubo_binding(struct gl_context
*ctx
,
2702 struct gl_uniform_buffer_binding
*binding
,
2703 struct gl_buffer_object
*bufObj
,
2708 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2710 binding
->Offset
= offset
;
2711 binding
->Size
= size
;
2712 binding
->AutomaticSize
= autoSize
;
2714 /* If this is a real buffer object, mark it has having been used
2715 * at some point as a UBO.
2718 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
2722 * Binds a buffer object to a shader storage buffer binding point.
2724 * The caller is responsible for flushing vertices and updating
2728 set_ssbo_binding(struct gl_context
*ctx
,
2729 struct gl_shader_storage_buffer_binding
*binding
,
2730 struct gl_buffer_object
*bufObj
,
2735 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2737 binding
->Offset
= offset
;
2738 binding
->Size
= size
;
2739 binding
->AutomaticSize
= autoSize
;
2741 /* If this is a real buffer object, mark it has having been used
2742 * at some point as a SSBO.
2745 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
2749 * Binds a buffer object to a uniform buffer binding point.
2751 * Unlike set_ubo_binding(), this function also flushes vertices
2752 * and updates NewDriverState. It also checks if the binding
2753 * has actually changed before updating it.
2756 bind_uniform_buffer(struct gl_context
*ctx
,
2758 struct gl_buffer_object
*bufObj
,
2763 struct gl_uniform_buffer_binding
*binding
=
2764 &ctx
->UniformBufferBindings
[index
];
2766 if (binding
->BufferObject
== bufObj
&&
2767 binding
->Offset
== offset
&&
2768 binding
->Size
== size
&&
2769 binding
->AutomaticSize
== autoSize
) {
2773 FLUSH_VERTICES(ctx
, 0);
2774 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
2776 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2780 * Binds a buffer object to a shader storage buffer binding point.
2782 * Unlike set_ssbo_binding(), this function also flushes vertices
2783 * and updates NewDriverState. It also checks if the binding
2784 * has actually changed before updating it.
2787 bind_shader_storage_buffer(struct gl_context
*ctx
,
2789 struct gl_buffer_object
*bufObj
,
2794 struct gl_shader_storage_buffer_binding
*binding
=
2795 &ctx
->ShaderStorageBufferBindings
[index
];
2797 if (binding
->BufferObject
== bufObj
&&
2798 binding
->Offset
== offset
&&
2799 binding
->Size
== size
&&
2800 binding
->AutomaticSize
== autoSize
) {
2804 FLUSH_VERTICES(ctx
, 0);
2805 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
2807 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2811 * Bind a region of a buffer object to a uniform block binding point.
2812 * \param index the uniform buffer binding point index
2813 * \param bufObj the buffer object
2814 * \param offset offset to the start of buffer object region
2815 * \param size size of the buffer object region
2818 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2820 struct gl_buffer_object
*bufObj
,
2824 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2825 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2829 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2830 _mesa_error(ctx
, GL_INVALID_VALUE
,
2831 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2832 ctx
->Const
.UniformBufferOffsetAlignment
);
2836 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2841 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2842 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2843 bufObj
->BufferRangeSize
= MIN2(size
, bufObj
->Size
);
2847 * Bind a region of a buffer object to a shader storage block binding point.
2848 * \param index the shader storage buffer binding point index
2849 * \param bufObj the buffer object
2850 * \param offset offset to the start of buffer object region
2851 * \param size size of the buffer object region
2854 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
2856 struct gl_buffer_object
*bufObj
,
2860 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2861 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2865 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
2866 _mesa_error(ctx
, GL_INVALID_VALUE
,
2867 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2868 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
2872 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2877 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2878 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2879 bufObj
->BufferRangeSize
= MIN2(size
, bufObj
->Size
);
2883 * Bind a buffer object to a uniform block binding point.
2884 * As above, but offset = 0.
2887 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2889 struct gl_buffer_object
*bufObj
)
2891 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2892 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2896 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2898 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2899 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2901 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2905 * Bind a buffer object to a shader storage block binding point.
2906 * As above, but offset = 0.
2909 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
2911 struct gl_buffer_object
*bufObj
)
2913 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2914 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2918 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2920 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2921 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2923 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2927 * Binds a buffer object to an atomic buffer binding point.
2929 * The caller is responsible for validating the offset,
2930 * flushing the vertices and updating NewDriverState.
2933 set_atomic_buffer_binding(struct gl_context
*ctx
,
2934 struct gl_atomic_buffer_binding
*binding
,
2935 struct gl_buffer_object
*bufObj
,
2939 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2941 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2942 binding
->Offset
= -1;
2945 binding
->Offset
= offset
;
2946 binding
->Size
= size
;
2947 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
2952 * Binds a buffer object to an atomic buffer binding point.
2954 * Unlike set_atomic_buffer_binding(), this function also validates the
2955 * index and offset, flushes vertices, and updates NewDriverState.
2956 * It also checks if the binding has actually changing before
2960 bind_atomic_buffer(struct gl_context
*ctx
,
2962 struct gl_buffer_object
*bufObj
,
2967 struct gl_atomic_buffer_binding
*binding
;
2969 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
2970 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
2974 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
2975 _mesa_error(ctx
, GL_INVALID_VALUE
,
2976 "%s(offset misaligned %d/%d)", name
, (int) offset
,
2977 ATOMIC_COUNTER_SIZE
);
2981 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
2983 binding
= &ctx
->AtomicBufferBindings
[index
];
2984 if (binding
->BufferObject
== bufObj
&&
2985 binding
->Offset
== offset
&&
2986 binding
->Size
== size
) {
2990 FLUSH_VERTICES(ctx
, 0);
2991 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
2993 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
2997 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
2999 const GLintptr
*offsets
,
3000 const GLsizeiptr
*sizes
)
3002 if (offsets
[index
] < 0) {
3003 /* The ARB_multi_bind spec says:
3005 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3006 * value in <offsets> is less than zero (per binding)."
3008 _mesa_error(ctx
, GL_INVALID_VALUE
,
3009 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3010 index
, (int64_t) offsets
[index
]);
3014 if (sizes
[index
] <= 0) {
3015 /* The ARB_multi_bind spec says:
3017 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3018 * value in <sizes> is less than or equal to zero (per binding)."
3020 _mesa_error(ctx
, GL_INVALID_VALUE
,
3021 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3022 index
, (int64_t) sizes
[index
]);
3030 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3031 GLuint first
, GLsizei count
,
3034 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3035 _mesa_error(ctx
, GL_INVALID_ENUM
,
3036 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3040 /* The ARB_multi_bind_spec says:
3042 * "An INVALID_OPERATION error is generated if <first> + <count> is
3043 * greater than the number of target-specific indexed binding points,
3044 * as described in section 6.7.1."
3046 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3047 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3048 "%s(first=%u + count=%d > the value of "
3049 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3050 caller
, first
, count
,
3051 ctx
->Const
.MaxUniformBufferBindings
);
3059 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3060 GLuint first
, GLsizei count
,
3063 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3064 _mesa_error(ctx
, GL_INVALID_ENUM
,
3065 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3069 /* The ARB_multi_bind_spec says:
3071 * "An INVALID_OPERATION error is generated if <first> + <count> is
3072 * greater than the number of target-specific indexed binding points,
3073 * as described in section 6.7.1."
3075 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3076 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3077 "%s(first=%u + count=%d > the value of "
3078 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3079 caller
, first
, count
,
3080 ctx
->Const
.MaxShaderStorageBufferBindings
);
3088 * Unbind all uniform buffers in the range
3089 * <first> through <first>+<count>-1
3092 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3094 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3097 for (i
= 0; i
< count
; i
++)
3098 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3099 bufObj
, -1, -1, GL_TRUE
);
3103 * Unbind all shader storage buffers in the range
3104 * <first> through <first>+<count>-1
3107 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3110 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3113 for (i
= 0; i
< count
; i
++)
3114 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3115 bufObj
, -1, -1, GL_TRUE
);
3119 bind_uniform_buffers_base(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3120 const GLuint
*buffers
)
3124 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, "glBindBuffersBase"))
3127 /* Assume that at least one binding will be changed */
3128 FLUSH_VERTICES(ctx
, 0);
3129 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3132 /* The ARB_multi_bind spec says:
3134 * "If <buffers> is NULL, all bindings from <first> through
3135 * <first>+<count>-1 are reset to their unbound (zero) state."
3137 unbind_uniform_buffers(ctx
, first
, count
);
3141 /* Note that the error semantics for multi-bind commands differ from
3142 * those of other GL commands.
3144 * The Issues section in the ARB_multi_bind spec says:
3146 * "(11) Typically, OpenGL specifies that if an error is generated by a
3147 * command, that command has no effect. This is somewhat
3148 * unfortunate for multi-bind commands, because it would require a
3149 * first pass to scan the entire list of bound objects for errors
3150 * and then a second pass to actually perform the bindings.
3151 * Should we have different error semantics?
3153 * RESOLVED: Yes. In this specification, when the parameters for
3154 * one of the <count> binding points are invalid, that binding point
3155 * is not updated and an error will be generated. However, other
3156 * binding points in the same command will be updated if their
3157 * parameters are valid and no other error occurs."
3160 _mesa_begin_bufferobj_lookups(ctx
);
3162 for (i
= 0; i
< count
; i
++) {
3163 struct gl_uniform_buffer_binding
*binding
=
3164 &ctx
->UniformBufferBindings
[first
+ i
];
3165 struct gl_buffer_object
*bufObj
;
3167 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3168 bufObj
= binding
->BufferObject
;
3170 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3171 "glBindBuffersBase");
3174 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3175 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, GL_TRUE
);
3177 set_ubo_binding(ctx
, binding
, bufObj
, 0, 0, GL_TRUE
);
3181 _mesa_end_bufferobj_lookups(ctx
);
3185 bind_shader_storage_buffers_base(struct gl_context
*ctx
, GLuint first
,
3186 GLsizei count
, const GLuint
*buffers
)
3190 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
,
3191 "glBindBuffersBase"))
3194 /* Assume that at least one binding will be changed */
3195 FLUSH_VERTICES(ctx
, 0);
3196 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3199 /* The ARB_multi_bind spec says:
3201 * "If <buffers> is NULL, all bindings from <first> through
3202 * <first>+<count>-1 are reset to their unbound (zero) state."
3204 unbind_shader_storage_buffers(ctx
, first
, count
);
3208 /* Note that the error semantics for multi-bind commands differ from
3209 * those of other GL commands.
3211 * The Issues section in the ARB_multi_bind spec says:
3213 * "(11) Typically, OpenGL specifies that if an error is generated by a
3214 * command, that command has no effect. This is somewhat
3215 * unfortunate for multi-bind commands, because it would require a
3216 * first pass to scan the entire list of bound objects for errors
3217 * and then a second pass to actually perform the bindings.
3218 * Should we have different error semantics?
3220 * RESOLVED: Yes. In this specification, when the parameters for
3221 * one of the <count> binding points are invalid, that binding point
3222 * is not updated and an error will be generated. However, other
3223 * binding points in the same command will be updated if their
3224 * parameters are valid and no other error occurs."
3227 _mesa_begin_bufferobj_lookups(ctx
);
3229 for (i
= 0; i
< count
; i
++) {
3230 struct gl_shader_storage_buffer_binding
*binding
=
3231 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3232 struct gl_buffer_object
*bufObj
;
3234 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3235 bufObj
= binding
->BufferObject
;
3237 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3238 "glBindBuffersBase");
3241 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3242 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, GL_TRUE
);
3244 set_ssbo_binding(ctx
, binding
, bufObj
, 0, 0, GL_TRUE
);
3248 _mesa_end_bufferobj_lookups(ctx
);
3252 bind_uniform_buffers_range(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3253 const GLuint
*buffers
,
3254 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3258 if (!error_check_bind_uniform_buffers(ctx
, first
, count
,
3259 "glBindBuffersRange"))
3262 /* Assume that at least one binding will be changed */
3263 FLUSH_VERTICES(ctx
, 0);
3264 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3267 /* The ARB_multi_bind spec says:
3269 * "If <buffers> is NULL, all bindings from <first> through
3270 * <first>+<count>-1 are reset to their unbound (zero) state.
3271 * In this case, the offsets and sizes associated with the
3272 * binding points are set to default values, ignoring
3273 * <offsets> and <sizes>."
3275 unbind_uniform_buffers(ctx
, first
, count
);
3279 /* Note that the error semantics for multi-bind commands differ from
3280 * those of other GL commands.
3282 * The Issues section in the ARB_multi_bind spec says:
3284 * "(11) Typically, OpenGL specifies that if an error is generated by a
3285 * command, that command has no effect. This is somewhat
3286 * unfortunate for multi-bind commands, because it would require a
3287 * first pass to scan the entire list of bound objects for errors
3288 * and then a second pass to actually perform the bindings.
3289 * Should we have different error semantics?
3291 * RESOLVED: Yes. In this specification, when the parameters for
3292 * one of the <count> binding points are invalid, that binding point
3293 * is not updated and an error will be generated. However, other
3294 * binding points in the same command will be updated if their
3295 * parameters are valid and no other error occurs."
3298 _mesa_begin_bufferobj_lookups(ctx
);
3300 for (i
= 0; i
< count
; i
++) {
3301 struct gl_uniform_buffer_binding
*binding
=
3302 &ctx
->UniformBufferBindings
[first
+ i
];
3303 struct gl_buffer_object
*bufObj
;
3305 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3308 /* The ARB_multi_bind spec says:
3310 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3311 * pair of values in <offsets> and <sizes> does not respectively
3312 * satisfy the constraints described for those parameters for the
3313 * specified target, as described in section 6.7.1 (per binding)."
3315 * Section 6.7.1 refers to table 6.5, which says:
3317 * "┌───────────────────────────────────────────────────────────────┐
3318 * │ Uniform buffer array bindings (see sec. 7.6) │
3319 * ├─────────────────────┬─────────────────────────────────────────┤
3321 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3322 * │ │ OFFSET_ALIGNMENT │
3324 * │ size restriction │ none │
3325 * └─────────────────────┴─────────────────────────────────────────┘"
3327 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3328 _mesa_error(ctx
, GL_INVALID_VALUE
,
3329 "glBindBuffersRange(offsets[%u]=%" PRId64
3330 " is misaligned; it must be a multiple of the value of "
3331 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3332 "target=GL_UNIFORM_BUFFER)",
3333 i
, (int64_t) offsets
[i
],
3334 ctx
->Const
.UniformBufferOffsetAlignment
);
3338 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3339 bufObj
= binding
->BufferObject
;
3341 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3342 "glBindBuffersRange");
3345 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3346 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, GL_FALSE
);
3348 set_ubo_binding(ctx
, binding
, bufObj
,
3349 offsets
[i
], sizes
[i
], GL_FALSE
);
3353 _mesa_end_bufferobj_lookups(ctx
);
3357 bind_shader_storage_buffers_range(struct gl_context
*ctx
, GLuint first
,
3358 GLsizei count
, const GLuint
*buffers
,
3359 const GLintptr
*offsets
,
3360 const GLsizeiptr
*sizes
)
3364 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
,
3365 "glBindBuffersRange"))
3368 /* Assume that at least one binding will be changed */
3369 FLUSH_VERTICES(ctx
, 0);
3370 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3373 /* The ARB_multi_bind spec says:
3375 * "If <buffers> is NULL, all bindings from <first> through
3376 * <first>+<count>-1 are reset to their unbound (zero) state.
3377 * In this case, the offsets and sizes associated with the
3378 * binding points are set to default values, ignoring
3379 * <offsets> and <sizes>."
3381 unbind_shader_storage_buffers(ctx
, first
, count
);
3385 /* Note that the error semantics for multi-bind commands differ from
3386 * those of other GL commands.
3388 * The Issues section in the ARB_multi_bind spec says:
3390 * "(11) Typically, OpenGL specifies that if an error is generated by a
3391 * command, that command has no effect. This is somewhat
3392 * unfortunate for multi-bind commands, because it would require a
3393 * first pass to scan the entire list of bound objects for errors
3394 * and then a second pass to actually perform the bindings.
3395 * Should we have different error semantics?
3397 * RESOLVED: Yes. In this specification, when the parameters for
3398 * one of the <count> binding points are invalid, that binding point
3399 * is not updated and an error will be generated. However, other
3400 * binding points in the same command will be updated if their
3401 * parameters are valid and no other error occurs."
3404 _mesa_begin_bufferobj_lookups(ctx
);
3406 for (i
= 0; i
< count
; i
++) {
3407 struct gl_shader_storage_buffer_binding
*binding
=
3408 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3409 struct gl_buffer_object
*bufObj
;
3411 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3414 /* The ARB_multi_bind spec says:
3416 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3417 * pair of values in <offsets> and <sizes> does not respectively
3418 * satisfy the constraints described for those parameters for the
3419 * specified target, as described in section 6.7.1 (per binding)."
3421 * Section 6.7.1 refers to table 6.5, which says:
3423 * "┌───────────────────────────────────────────────────────────────┐
3424 * │ Shader storage buffer array bindings (see sec. 7.8) │
3425 * ├─────────────────────┬─────────────────────────────────────────┤
3427 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3428 * │ │ BUFFER_OFFSET_ALIGNMENT │
3430 * │ size restriction │ none │
3431 * └─────────────────────┴─────────────────────────────────────────┘"
3433 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3434 _mesa_error(ctx
, GL_INVALID_VALUE
,
3435 "glBindBuffersRange(offsets[%u]=%" PRId64
3436 " is misaligned; it must be a multiple of the value of "
3437 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3438 "target=GL_SHADER_STORAGE_BUFFER)",
3439 i
, (int64_t) offsets
[i
],
3440 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3444 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3445 bufObj
= binding
->BufferObject
;
3447 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3448 "glBindBuffersRange");
3451 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3452 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, GL_FALSE
);
3454 set_ssbo_binding(ctx
, binding
, bufObj
,
3455 offsets
[i
], sizes
[i
], GL_FALSE
);
3459 _mesa_end_bufferobj_lookups(ctx
);
3463 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3464 struct gl_transform_feedback_object
*tfObj
,
3465 GLuint first
, GLsizei count
, const char *caller
)
3467 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3468 _mesa_error(ctx
, GL_INVALID_ENUM
,
3469 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3473 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3475 * "An INVALID_OPERATION error is generated :
3478 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3479 * FEEDBACK_BUFFER and transform feedback is currently active."
3481 * We assume that this is also meant to apply to BindBuffersRange
3482 * and BindBuffersBase.
3484 if (tfObj
->Active
) {
3485 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3486 "%s(Changing transform feedback buffers while "
3487 "transform feedback is active)", caller
);
3491 /* The ARB_multi_bind_spec says:
3493 * "An INVALID_OPERATION error is generated if <first> + <count> is
3494 * greater than the number of target-specific indexed binding points,
3495 * as described in section 6.7.1."
3497 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3498 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3499 "%s(first=%u + count=%d > the value of "
3500 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3501 caller
, first
, count
,
3502 ctx
->Const
.MaxTransformFeedbackBuffers
);
3510 * Unbind all transform feedback buffers in the range
3511 * <first> through <first>+<count>-1
3514 unbind_xfb_buffers(struct gl_context
*ctx
,
3515 struct gl_transform_feedback_object
*tfObj
,
3516 GLuint first
, GLsizei count
)
3518 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3521 for (i
= 0; i
< count
; i
++)
3522 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3527 bind_xfb_buffers_base(struct gl_context
*ctx
,
3528 GLuint first
, GLsizei count
,
3529 const GLuint
*buffers
)
3531 struct gl_transform_feedback_object
*tfObj
=
3532 ctx
->TransformFeedback
.CurrentObject
;
3535 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
,
3536 "glBindBuffersBase"))
3539 /* Assume that at least one binding will be changed */
3540 FLUSH_VERTICES(ctx
, 0);
3541 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3544 /* The ARB_multi_bind spec says:
3546 * "If <buffers> is NULL, all bindings from <first> through
3547 * <first>+<count>-1 are reset to their unbound (zero) state."
3549 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3553 /* Note that the error semantics for multi-bind commands differ from
3554 * those of other GL commands.
3556 * The Issues section in the ARB_multi_bind spec says:
3558 * "(11) Typically, OpenGL specifies that if an error is generated by a
3559 * command, that command has no effect. This is somewhat
3560 * unfortunate for multi-bind commands, because it would require a
3561 * first pass to scan the entire list of bound objects for errors
3562 * and then a second pass to actually perform the bindings.
3563 * Should we have different error semantics?
3565 * RESOLVED: Yes. In this specification, when the parameters for
3566 * one of the <count> binding points are invalid, that binding point
3567 * is not updated and an error will be generated. However, other
3568 * binding points in the same command will be updated if their
3569 * parameters are valid and no other error occurs."
3572 _mesa_begin_bufferobj_lookups(ctx
);
3574 for (i
= 0; i
< count
; i
++) {
3575 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[first
+ i
];
3576 struct gl_buffer_object
*bufObj
;
3578 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3579 bufObj
= boundBufObj
;
3581 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3582 "glBindBuffersBase");
3585 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3589 _mesa_end_bufferobj_lookups(ctx
);
3593 bind_xfb_buffers_range(struct gl_context
*ctx
,
3594 GLuint first
, GLsizei count
,
3595 const GLuint
*buffers
,
3596 const GLintptr
*offsets
,
3597 const GLsizeiptr
*sizes
)
3599 struct gl_transform_feedback_object
*tfObj
=
3600 ctx
->TransformFeedback
.CurrentObject
;
3603 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
,
3604 "glBindBuffersRange"))
3607 /* Assume that at least one binding will be changed */
3608 FLUSH_VERTICES(ctx
, 0);
3609 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3612 /* The ARB_multi_bind spec says:
3614 * "If <buffers> is NULL, all bindings from <first> through
3615 * <first>+<count>-1 are reset to their unbound (zero) state.
3616 * In this case, the offsets and sizes associated with the
3617 * binding points are set to default values, ignoring
3618 * <offsets> and <sizes>."
3620 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3624 /* Note that the error semantics for multi-bind commands differ from
3625 * those of other GL commands.
3627 * The Issues section in the ARB_multi_bind spec says:
3629 * "(11) Typically, OpenGL specifies that if an error is generated by a
3630 * command, that command has no effect. This is somewhat
3631 * unfortunate for multi-bind commands, because it would require a
3632 * first pass to scan the entire list of bound objects for errors
3633 * and then a second pass to actually perform the bindings.
3634 * Should we have different error semantics?
3636 * RESOLVED: Yes. In this specification, when the parameters for
3637 * one of the <count> binding points are invalid, that binding point
3638 * is not updated and an error will be generated. However, other
3639 * binding points in the same command will be updated if their
3640 * parameters are valid and no other error occurs."
3643 _mesa_begin_bufferobj_lookups(ctx
);
3645 for (i
= 0; i
< count
; i
++) {
3646 const GLuint index
= first
+ i
;
3647 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3648 struct gl_buffer_object
*bufObj
;
3650 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3653 /* The ARB_multi_bind spec says:
3655 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3656 * pair of values in <offsets> and <sizes> does not respectively
3657 * satisfy the constraints described for those parameters for the
3658 * specified target, as described in section 6.7.1 (per binding)."
3660 * Section 6.7.1 refers to table 6.5, which says:
3662 * "┌───────────────────────────────────────────────────────────────┐
3663 * │ Transform feedback array bindings (see sec. 13.2.2) │
3664 * ├───────────────────────┬───────────────────────────────────────┤
3666 * │ offset restriction │ multiple of 4 │
3668 * │ size restriction │ multiple of 4 │
3669 * └───────────────────────┴───────────────────────────────────────┘"
3671 if (offsets
[i
] & 0x3) {
3672 _mesa_error(ctx
, GL_INVALID_VALUE
,
3673 "glBindBuffersRange(offsets[%u]=%" PRId64
3674 " is misaligned; it must be a multiple of 4 when "
3675 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3676 i
, (int64_t) offsets
[i
]);
3680 if (sizes
[i
] & 0x3) {
3681 _mesa_error(ctx
, GL_INVALID_VALUE
,
3682 "glBindBuffersRange(sizes[%u]=%" PRId64
3683 " is misaligned; it must be a multiple of 4 when "
3684 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3685 i
, (int64_t) sizes
[i
]);
3689 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3690 bufObj
= boundBufObj
;
3692 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3693 "glBindBuffersRange");
3696 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
3697 offsets
[i
], sizes
[i
]);
3700 _mesa_end_bufferobj_lookups(ctx
);
3704 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
3705 GLuint first
, GLsizei count
,
3708 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
3709 _mesa_error(ctx
, GL_INVALID_ENUM
,
3710 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
3714 /* The ARB_multi_bind_spec says:
3716 * "An INVALID_OPERATION error is generated if <first> + <count> is
3717 * greater than the number of target-specific indexed binding points,
3718 * as described in section 6.7.1."
3720 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
3721 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3722 "%s(first=%u + count=%d > the value of "
3723 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3724 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
3732 * Unbind all atomic counter buffers in the range
3733 * <first> through <first>+<count>-1
3736 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3738 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3741 for (i
= 0; i
< count
; i
++)
3742 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
3747 bind_atomic_buffers_base(struct gl_context
*ctx
,
3750 const GLuint
*buffers
)
3754 if (!error_check_bind_atomic_buffers(ctx
, first
, count
,
3755 "glBindBuffersBase"))
3758 /* Assume that at least one binding will be changed */
3759 FLUSH_VERTICES(ctx
, 0);
3760 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3763 /* The ARB_multi_bind spec says:
3765 * "If <buffers> is NULL, all bindings from <first> through
3766 * <first>+<count>-1 are reset to their unbound (zero) state."
3768 unbind_atomic_buffers(ctx
, first
, count
);
3772 /* Note that the error semantics for multi-bind commands differ from
3773 * those of other GL commands.
3775 * The Issues section in the ARB_multi_bind spec says:
3777 * "(11) Typically, OpenGL specifies that if an error is generated by a
3778 * command, that command has no effect. This is somewhat
3779 * unfortunate for multi-bind commands, because it would require a
3780 * first pass to scan the entire list of bound objects for errors
3781 * and then a second pass to actually perform the bindings.
3782 * Should we have different error semantics?
3784 * RESOLVED: Yes. In this specification, when the parameters for
3785 * one of the <count> binding points are invalid, that binding point
3786 * is not updated and an error will be generated. However, other
3787 * binding points in the same command will be updated if their
3788 * parameters are valid and no other error occurs."
3791 _mesa_begin_bufferobj_lookups(ctx
);
3793 for (i
= 0; i
< count
; i
++) {
3794 struct gl_atomic_buffer_binding
*binding
=
3795 &ctx
->AtomicBufferBindings
[first
+ i
];
3796 struct gl_buffer_object
*bufObj
;
3798 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3799 bufObj
= binding
->BufferObject
;
3801 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3802 "glBindBuffersBase");
3805 set_atomic_buffer_binding(ctx
, binding
, bufObj
, 0, 0);
3808 _mesa_end_bufferobj_lookups(ctx
);
3812 bind_atomic_buffers_range(struct gl_context
*ctx
,
3815 const GLuint
*buffers
,
3816 const GLintptr
*offsets
,
3817 const GLsizeiptr
*sizes
)
3821 if (!error_check_bind_atomic_buffers(ctx
, first
, count
,
3822 "glBindBuffersRange"))
3825 /* Assume that at least one binding will be changed */
3826 FLUSH_VERTICES(ctx
, 0);
3827 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3830 /* The ARB_multi_bind spec says:
3832 * "If <buffers> is NULL, all bindings from <first> through
3833 * <first>+<count>-1 are reset to their unbound (zero) state.
3834 * In this case, the offsets and sizes associated with the
3835 * binding points are set to default values, ignoring
3836 * <offsets> and <sizes>."
3838 unbind_atomic_buffers(ctx
, first
, count
);
3842 /* Note that the error semantics for multi-bind commands differ from
3843 * those of other GL commands.
3845 * The Issues section in the ARB_multi_bind spec says:
3847 * "(11) Typically, OpenGL specifies that if an error is generated by a
3848 * command, that command has no effect. This is somewhat
3849 * unfortunate for multi-bind commands, because it would require a
3850 * first pass to scan the entire list of bound objects for errors
3851 * and then a second pass to actually perform the bindings.
3852 * Should we have different error semantics?
3854 * RESOLVED: Yes. In this specification, when the parameters for
3855 * one of the <count> binding points are invalid, that binding point
3856 * is not updated and an error will be generated. However, other
3857 * binding points in the same command will be updated if their
3858 * parameters are valid and no other error occurs."
3861 _mesa_begin_bufferobj_lookups(ctx
);
3863 for (i
= 0; i
< count
; i
++) {
3864 struct gl_atomic_buffer_binding
*binding
=
3865 &ctx
->AtomicBufferBindings
[first
+ i
];
3866 struct gl_buffer_object
*bufObj
;
3868 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3871 /* The ARB_multi_bind spec says:
3873 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3874 * pair of values in <offsets> and <sizes> does not respectively
3875 * satisfy the constraints described for those parameters for the
3876 * specified target, as described in section 6.7.1 (per binding)."
3878 * Section 6.7.1 refers to table 6.5, which says:
3880 * "┌───────────────────────────────────────────────────────────────┐
3881 * │ Atomic counter array bindings (see sec. 7.7.2) │
3882 * ├───────────────────────┬───────────────────────────────────────┤
3884 * │ offset restriction │ multiple of 4 │
3886 * │ size restriction │ none │
3887 * └───────────────────────┴───────────────────────────────────────┘"
3889 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
3890 _mesa_error(ctx
, GL_INVALID_VALUE
,
3891 "glBindBuffersRange(offsets[%u]=%" PRId64
3892 " is misaligned; it must be a multiple of %d when "
3893 "target=GL_ATOMIC_COUNTER_BUFFER)",
3894 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
3898 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3899 bufObj
= binding
->BufferObject
;
3901 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3902 "glBindBuffersRange");
3905 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offsets
[i
], sizes
[i
]);
3908 _mesa_end_bufferobj_lookups(ctx
);
3912 _mesa_BindBufferRange(GLenum target
, GLuint index
,
3913 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
3915 GET_CURRENT_CONTEXT(ctx
);
3916 struct gl_buffer_object
*bufObj
;
3918 if (MESA_VERBOSE
& VERBOSE_API
) {
3919 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %ld, %ld)\n",
3920 _mesa_enum_to_string(target
), index
, buffer
, offset
, size
);
3924 bufObj
= ctx
->Shared
->NullBufferObj
;
3926 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3928 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3929 &bufObj
, "glBindBufferRange"))
3933 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3934 "glBindBufferRange(invalid buffer=%u)", buffer
);
3940 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
3947 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3948 _mesa_bind_buffer_range_transform_feedback(ctx
,
3949 ctx
->TransformFeedback
.CurrentObject
,
3950 index
, bufObj
, offset
, size
,
3953 case GL_UNIFORM_BUFFER
:
3954 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3956 case GL_SHADER_STORAGE_BUFFER
:
3957 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3959 case GL_ATOMIC_COUNTER_BUFFER
:
3960 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
3961 "glBindBufferRange");
3964 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
3970 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
3972 GET_CURRENT_CONTEXT(ctx
);
3973 struct gl_buffer_object
*bufObj
;
3975 if (MESA_VERBOSE
& VERBOSE_API
) {
3976 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
3977 _mesa_enum_to_string(target
), index
, buffer
);
3981 bufObj
= ctx
->Shared
->NullBufferObj
;
3983 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3985 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3986 &bufObj
, "glBindBufferBase"))
3990 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3991 "glBindBufferBase(invalid buffer=%u)", buffer
);
3995 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3996 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3998 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3999 * zero and size equal to the size of buffer."
4001 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4003 * "If the parameter (starting offset or size) was not specified when the
4004 * buffer object was bound, zero is returned."
4006 * What happens if the size of the buffer changes? Does the size of the
4007 * buffer at the moment glBindBufferBase was called still play a role, like
4008 * the first quote would imply, or is the size meaningless in the
4009 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4010 * core spec page 45 says:
4012 * "It is equivalent to calling BindBufferRange with offset zero, while
4013 * size is determined by the size of the bound buffer at the time the
4016 * My interpretation is that the GL 4.1 spec was a clarification of the
4017 * behavior, not a change. In particular, this choice will only make
4018 * rendering work in cases where it would have had undefined results.
4022 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4023 _mesa_bind_buffer_base_transform_feedback(ctx
,
4024 ctx
->TransformFeedback
.CurrentObject
,
4025 index
, bufObj
, false);
4027 case GL_UNIFORM_BUFFER
:
4028 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4030 case GL_SHADER_STORAGE_BUFFER
:
4031 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4033 case GL_ATOMIC_COUNTER_BUFFER
:
4034 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
4035 "glBindBufferBase");
4038 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4044 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4045 const GLuint
*buffers
,
4046 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4048 GET_CURRENT_CONTEXT(ctx
);
4050 if (MESA_VERBOSE
& VERBOSE_API
) {
4051 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4052 _mesa_enum_to_string(target
), first
, count
,
4053 buffers
, offsets
, sizes
);
4057 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4058 bind_xfb_buffers_range(ctx
, first
, count
, buffers
, offsets
, sizes
);
4060 case GL_UNIFORM_BUFFER
:
4061 bind_uniform_buffers_range(ctx
, first
, count
, buffers
, offsets
, sizes
);
4063 case GL_SHADER_STORAGE_BUFFER
:
4064 bind_shader_storage_buffers_range(ctx
, first
, count
, buffers
, offsets
,
4067 case GL_ATOMIC_COUNTER_BUFFER
:
4068 bind_atomic_buffers_range(ctx
, first
, count
, buffers
,
4072 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4073 _mesa_enum_to_string(target
));
4079 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4080 const GLuint
*buffers
)
4082 GET_CURRENT_CONTEXT(ctx
);
4084 if (MESA_VERBOSE
& VERBOSE_API
) {
4085 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
4086 _mesa_enum_to_string(target
), first
, count
, buffers
);
4090 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4091 bind_xfb_buffers_base(ctx
, first
, count
, buffers
);
4093 case GL_UNIFORM_BUFFER
:
4094 bind_uniform_buffers_base(ctx
, first
, count
, buffers
);
4096 case GL_SHADER_STORAGE_BUFFER
:
4097 bind_shader_storage_buffers_base(ctx
, first
, count
, buffers
);
4099 case GL_ATOMIC_COUNTER_BUFFER
:
4100 bind_atomic_buffers_base(ctx
, first
, count
, buffers
);
4103 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4104 _mesa_enum_to_string(target
));
4110 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4113 GET_CURRENT_CONTEXT(ctx
);
4114 struct gl_buffer_object
*bufObj
;
4115 const GLintptr end
= offset
+ length
;
4117 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4119 _mesa_error(ctx
, GL_INVALID_VALUE
,
4120 "glInvalidateBufferSubData(name = 0x%x) invalid object",
4125 /* The GL_ARB_invalidate_subdata spec says:
4127 * "An INVALID_VALUE error is generated if <offset> or <length> is
4128 * negative, or if <offset> + <length> is greater than the value of
4131 if (end
< 0 || end
> bufObj
->Size
) {
4132 _mesa_error(ctx
, GL_INVALID_VALUE
,
4133 "glInvalidateBufferSubData(invalid offset or length)");
4137 /* The OpenGL 4.4 (Core Profile) spec says:
4139 * "An INVALID_OPERATION error is generated if buffer is currently
4140 * mapped by MapBuffer or if the invalidate range intersects the range
4141 * currently mapped by MapBufferRange, unless it was mapped
4142 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4144 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4145 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4146 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4147 "glInvalidateBufferSubData(intersection with mapped "
4152 /* We don't actually do anything for this yet. Just return after
4153 * validating the parameters and generating the required errors.
4159 _mesa_InvalidateBufferData(GLuint buffer
)
4161 GET_CURRENT_CONTEXT(ctx
);
4162 struct gl_buffer_object
*bufObj
;
4164 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4166 _mesa_error(ctx
, GL_INVALID_VALUE
,
4167 "glInvalidateBufferData(name = 0x%x) invalid object",
4172 /* The OpenGL 4.4 (Core Profile) spec says:
4174 * "An INVALID_OPERATION error is generated if buffer is currently
4175 * mapped by MapBuffer or if the invalidate range intersects the range
4176 * currently mapped by MapBufferRange, unless it was mapped
4177 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4179 if (_mesa_check_disallowed_mapping(bufObj
)) {
4180 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4181 "glInvalidateBufferData(intersection with mapped "
4186 /* We don't actually do anything for this yet. Just return after
4187 * validating the parameters and generating the required errors.