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 */
35 #include "util/debug.h"
41 #include "bufferobj.h"
42 #include "externalobjects.h"
45 #include "glformats.h"
47 #include "transformfeedback.h"
49 #include "util/u_atomic.h"
54 /*#define BOUNDS_CHECK*/
58 * We count the number of buffer modification calls to check for
59 * inefficient buffer use. This is the number of such calls before we
62 #define BUFFER_WARNING_CALL_COUNT 4
66 * Helper to warn of possible performance issues, such as frequently
67 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
71 buffer_usage_warning(struct gl_context
*ctx
, GLuint
*id
, const char *fmt
, ...)
76 _mesa_gl_vdebug(ctx
, id
,
77 MESA_DEBUG_SOURCE_API
,
78 MESA_DEBUG_TYPE_PERFORMANCE
,
79 MESA_DEBUG_SEVERITY_MEDIUM
,
84 #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
86 static GLuint id = 0; \
87 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
92 * Used as a placeholder for buffer objects between glGenBuffers() and
93 * glBindBuffer() so that glIsBuffer() can work correctly.
95 static struct gl_buffer_object DummyBufferObject
;
99 * Return pointer to address of a buffer object target.
100 * \param ctx the GL context
101 * \param target the buffer object target to be retrieved.
102 * \return pointer to pointer to the buffer object bound to \c target in the
103 * specified context or \c NULL if \c target is invalid.
105 static inline struct gl_buffer_object
**
106 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
108 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
110 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
111 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
115 case GL_ARRAY_BUFFER_ARB
:
116 return &ctx
->Array
.ArrayBufferObj
;
117 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
118 return &ctx
->Array
.VAO
->IndexBufferObj
;
119 case GL_PIXEL_PACK_BUFFER_EXT
:
120 return &ctx
->Pack
.BufferObj
;
121 case GL_PIXEL_UNPACK_BUFFER_EXT
:
122 return &ctx
->Unpack
.BufferObj
;
123 case GL_COPY_READ_BUFFER
:
124 return &ctx
->CopyReadBuffer
;
125 case GL_COPY_WRITE_BUFFER
:
126 return &ctx
->CopyWriteBuffer
;
127 case GL_QUERY_BUFFER
:
128 if (_mesa_has_ARB_query_buffer_object(ctx
))
129 return &ctx
->QueryBuffer
;
131 case GL_DRAW_INDIRECT_BUFFER
:
132 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_draw_indirect
) ||
133 _mesa_is_gles31(ctx
)) {
134 return &ctx
->DrawIndirectBuffer
;
137 case GL_PARAMETER_BUFFER_ARB
:
138 if (_mesa_has_ARB_indirect_parameters(ctx
)) {
139 return &ctx
->ParameterBuffer
;
142 case GL_DISPATCH_INDIRECT_BUFFER
:
143 if (_mesa_has_compute_shaders(ctx
)) {
144 return &ctx
->DispatchIndirectBuffer
;
147 case GL_TRANSFORM_FEEDBACK_BUFFER
:
148 if (ctx
->Extensions
.EXT_transform_feedback
) {
149 return &ctx
->TransformFeedback
.CurrentBuffer
;
152 case GL_TEXTURE_BUFFER
:
153 if (_mesa_has_ARB_texture_buffer_object(ctx
) ||
154 _mesa_has_OES_texture_buffer(ctx
)) {
155 return &ctx
->Texture
.BufferObject
;
158 case GL_UNIFORM_BUFFER
:
159 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
160 return &ctx
->UniformBuffer
;
163 case GL_SHADER_STORAGE_BUFFER
:
164 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
165 return &ctx
->ShaderStorageBuffer
;
168 case GL_ATOMIC_COUNTER_BUFFER
:
169 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
170 return &ctx
->AtomicBuffer
;
173 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
174 if (ctx
->Extensions
.AMD_pinned_memory
) {
175 return &ctx
->ExternalVirtualMemoryBuffer
;
186 * Get the buffer object bound to the specified target in a GL context.
187 * \param ctx the GL context
188 * \param target the buffer object target to be retrieved.
189 * \param error the GL error to record if target is illegal.
190 * \return pointer to the buffer object bound to \c target in the
191 * specified context or \c NULL if \c target is invalid.
193 static inline struct gl_buffer_object
*
194 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
197 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
200 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
204 if (!_mesa_is_bufferobj(*bufObj
)) {
205 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
214 * Convert a GLbitfield describing the mapped buffer access flags
215 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
218 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
220 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
221 if ((access
& rwFlags
) == rwFlags
)
222 return GL_READ_WRITE
;
223 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
225 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
226 return GL_WRITE_ONLY
;
228 /* Otherwise, AccessFlags is zero (the default state).
230 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
232 * Name Type Initial Value Legal Values
234 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
237 * However, table 6.8 in the GL_OES_mapbuffer extension says:
239 * Get Value Type Get Command Value Description
240 * --------- ---- ----------- ----- -----------
241 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
243 * The difference is because GL_OES_mapbuffer only supports mapping buffers
248 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
253 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
255 * The regions do not overlap if and only if the end of the given
256 * region is before the mapped region or the start of the given region
257 * is after the mapped region.
259 * \param obj Buffer object target on which to operate.
260 * \param offset Offset of the first byte of the subdata range.
261 * \param size Size, in bytes, of the subdata range.
262 * \return true if ranges overlap, false otherwise
266 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
267 GLintptr offset
, GLsizeiptr size
)
269 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
270 const GLintptr end
= offset
+ size
;
271 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
272 obj
->Mappings
[MAP_USER
].Length
;
274 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
283 * Tests the subdata range parameters and sets the GL error code for
284 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
285 * \c glClearBufferSubData.
287 * \param ctx GL context.
288 * \param bufObj The buffer object.
289 * \param offset Offset of the first byte of the subdata range.
290 * \param size Size, in bytes, of the subdata range.
291 * \param mappedRange If true, checks if an overlapping range is mapped.
292 * If false, checks if buffer is mapped.
293 * \param caller Name of calling function for recording errors.
294 * \return false if error, true otherwise
296 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
299 buffer_object_subdata_range_good(struct gl_context
*ctx
,
300 const struct gl_buffer_object
*bufObj
,
301 GLintptr offset
, GLsizeiptr size
,
302 bool mappedRange
, const char *caller
)
305 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
310 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
314 if (offset
+ size
> bufObj
->Size
) {
315 _mesa_error(ctx
, GL_INVALID_VALUE
,
316 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
317 (unsigned long) offset
,
318 (unsigned long) size
,
319 (unsigned long) bufObj
->Size
);
323 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
327 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
328 _mesa_error(ctx
, GL_INVALID_OPERATION
,
329 "%s(range is mapped without persistent bit)",
335 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
336 _mesa_error(ctx
, GL_INVALID_OPERATION
,
337 "%s(buffer is mapped without persistent bit)",
348 * Test the format and type parameters and set the GL error code for
349 * \c glClearBufferData, \c glClearNamedBufferData, \c glClearBufferSubData
350 * and \c glClearNamedBufferSubData.
352 * \param ctx GL context.
353 * \param internalformat Format to which the data is to be converted.
354 * \param format Format of the supplied data.
355 * \param type Type of the supplied data.
356 * \param caller Name of calling function for recording errors.
357 * \return If internalformat, format and type are legal the mesa_format
358 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
360 * \sa glClearBufferData, glClearNamedBufferData, glClearBufferSubData and
361 * glClearNamedBufferSubData.
364 validate_clear_buffer_format(struct gl_context
*ctx
,
365 GLenum internalformat
,
366 GLenum format
, GLenum type
,
369 mesa_format mesaFormat
;
370 GLenum errorFormatType
;
372 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
373 if (mesaFormat
== MESA_FORMAT_NONE
) {
374 _mesa_error(ctx
, GL_INVALID_ENUM
,
375 "%s(invalid internalformat)", caller
);
376 return MESA_FORMAT_NONE
;
379 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
380 * EXT_texture_integer there is no conversion between integer and
381 * non-integer formats
383 if (_mesa_is_enum_format_signed_int(format
) !=
384 _mesa_is_format_integer_color(mesaFormat
)) {
385 _mesa_error(ctx
, GL_INVALID_OPERATION
,
386 "%s(integer vs non-integer)", caller
);
387 return MESA_FORMAT_NONE
;
390 if (!_mesa_is_color_format(format
)) {
391 _mesa_error(ctx
, GL_INVALID_VALUE
,
392 "%s(format is not a color format)", caller
);
393 return MESA_FORMAT_NONE
;
396 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
397 if (errorFormatType
!= GL_NO_ERROR
) {
398 _mesa_error(ctx
, GL_INVALID_VALUE
,
399 "%s(invalid format or type)", caller
);
400 return MESA_FORMAT_NONE
;
408 * Convert user-specified clear value to the specified internal format.
410 * \param ctx GL context.
411 * \param internalformat Format to which the data is converted.
412 * \param clearValue Points to the converted clear value.
413 * \param format Format of the supplied data.
414 * \param type Type of the supplied data.
415 * \param data Data which is to be converted to internalformat.
416 * \param caller Name of calling function for recording errors.
417 * \return true if data could be converted, false otherwise.
419 * \sa glClearBufferData, glClearBufferSubData
422 convert_clear_buffer_data(struct gl_context
*ctx
,
423 mesa_format internalformat
,
424 GLubyte
*clearValue
, GLenum format
, GLenum type
,
425 const GLvoid
*data
, const char *caller
)
427 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
429 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
430 0, &clearValue
, 1, 1, 1,
431 format
, type
, data
, &ctx
->Unpack
)) {
435 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
442 * Allocate and initialize a new buffer object.
444 * Default callback for the \c dd_function_table::NewBufferObject() hook.
446 static struct gl_buffer_object
*
447 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
449 struct gl_buffer_object
*obj
= MALLOC_STRUCT(gl_buffer_object
);
453 _mesa_initialize_buffer_object(ctx
, obj
, name
);
459 * Delete a buffer object.
461 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
464 _mesa_delete_buffer_object(struct gl_context
*ctx
,
465 struct gl_buffer_object
*bufObj
)
469 vbo_delete_minmax_cache(bufObj
);
470 _mesa_align_free(bufObj
->Data
);
472 /* assign strange values here to help w/ debugging */
473 bufObj
->RefCount
= -1000;
476 simple_mtx_destroy(&bufObj
->MinMaxCacheMutex
);
484 * Set ptr to bufObj w/ reference counting.
485 * This is normally only called from the _mesa_reference_buffer_object() macro
486 * when there's a real pointer change.
489 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
490 struct gl_buffer_object
**ptr
,
491 struct gl_buffer_object
*bufObj
)
494 /* Unreference the old buffer */
495 struct gl_buffer_object
*oldObj
= *ptr
;
497 if (p_atomic_dec_zero(&oldObj
->RefCount
)) {
498 assert(ctx
->Driver
.DeleteBuffer
);
499 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
507 /* reference new buffer */
508 p_atomic_inc(&bufObj
->RefCount
);
515 * Get the value of MESA_NO_MINMAX_CACHE.
518 get_no_minmax_cache()
520 static bool read
= false;
521 static bool disable
= false;
524 disable
= env_var_as_boolean("MESA_NO_MINMAX_CACHE", false);
533 * Initialize a buffer object to default values.
536 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
537 struct gl_buffer_object
*obj
,
540 memset(obj
, 0, sizeof(struct gl_buffer_object
));
543 obj
->Usage
= GL_STATIC_DRAW_ARB
;
545 simple_mtx_init(&obj
->MinMaxCacheMutex
, mtx_plain
);
546 if (get_no_minmax_cache())
547 obj
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
553 * Callback called from _mesa_HashWalk()
556 count_buffer_size(GLuint key
, void *data
, void *userData
)
558 const struct gl_buffer_object
*bufObj
=
559 (const struct gl_buffer_object
*) data
;
560 GLuint
*total
= (GLuint
*) userData
;
563 *total
= *total
+ bufObj
->Size
;
568 * Compute total size (in bytes) of all buffer objects for the given context.
569 * For debugging purposes.
572 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
576 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
583 * Allocate space for and store data in a buffer object. Any data that was
584 * previously stored in the buffer object is lost. If \c data is \c NULL,
585 * memory will be allocated, but no copy will occur.
587 * This is the default callback for \c dd_function_table::BufferData()
588 * Note that all GL error checking will have been done already.
590 * \param ctx GL context.
591 * \param target Buffer object target on which to operate.
592 * \param size Size, in bytes, of the new data store.
593 * \param data Pointer to the data to store in the buffer object. This
594 * pointer may be \c NULL.
595 * \param usage Hints about how the data will be used.
596 * \param bufObj Object to be used.
598 * \return GL_TRUE for success, GL_FALSE for failure
599 * \sa glBufferDataARB, dd_function_table::BufferData.
602 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
603 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
604 struct gl_buffer_object
*bufObj
)
610 _mesa_align_free( bufObj
->Data
);
612 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
614 bufObj
->Data
= (GLubyte
*) new_data
;
616 bufObj
->Usage
= usage
;
617 bufObj
->StorageFlags
= storageFlags
;
620 memcpy( bufObj
->Data
, data
, size
);
632 * Replace data in a subrange of buffer object. If the data range
633 * specified by \c size + \c offset extends beyond the end of the buffer or
634 * if \c data is \c NULL, no copy is performed.
636 * This is the default callback for \c dd_function_table::BufferSubData()
637 * Note that all GL error checking will have been done already.
639 * \param ctx GL context.
640 * \param offset Offset of the first byte to be modified.
641 * \param size Size, in bytes, of the data range.
642 * \param data Pointer to the data to store in the buffer object.
643 * \param bufObj Object to be used.
645 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
648 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptrARB offset
,
649 GLsizeiptrARB size
, const GLvoid
*data
,
650 struct gl_buffer_object
*bufObj
)
654 /* this should have been caught in _mesa_BufferSubData() */
655 assert(size
+ offset
<= bufObj
->Size
);
658 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
664 * Retrieve data from a subrange of buffer object. If the data range
665 * specified by \c size + \c offset extends beyond the end of the buffer or
666 * if \c data is \c NULL, no copy is performed.
668 * This is the default callback for \c dd_function_table::GetBufferSubData()
669 * Note that all GL error checking will have been done already.
671 * \param ctx GL context.
672 * \param target Buffer object target on which to operate.
673 * \param offset Offset of the first byte to be fetched.
674 * \param size Size, in bytes, of the data range.
675 * \param data Destination for data
676 * \param bufObj Object to be used.
678 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
681 buffer_get_subdata(struct gl_context
*ctx
, GLintptrARB offset
,
682 GLsizeiptrARB size
, GLvoid
*data
,
683 struct gl_buffer_object
*bufObj
)
687 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
688 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
694 * Clear a subrange of the buffer object with copies of the supplied data.
695 * If data is NULL the buffer is filled with zeros.
697 * This is the default callback for \c dd_function_table::ClearBufferSubData()
698 * Note that all GL error checking will have been done already.
700 * \param ctx GL context.
701 * \param offset Offset of the first byte to be cleared.
702 * \param size Size, in bytes, of the to be cleared range.
703 * \param clearValue Source of the data.
704 * \param clearValueSize Size, in bytes, of the supplied data.
705 * \param bufObj Object to be cleared.
707 * \sa glClearBufferSubData, glClearBufferData and
708 * dd_function_table::ClearBufferSubData.
711 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
712 GLintptr offset
, GLsizeiptr size
,
713 const GLvoid
*clearValue
,
714 GLsizeiptr clearValueSize
,
715 struct gl_buffer_object
*bufObj
)
720 assert(ctx
->Driver
.MapBufferRange
);
721 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
723 GL_MAP_INVALIDATE_RANGE_BIT
,
724 bufObj
, MAP_INTERNAL
);
727 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
731 if (clearValue
== NULL
) {
732 /* Clear with zeros, per the spec */
733 memset(dest
, 0, size
);
734 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
738 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
739 memcpy(dest
, clearValue
, clearValueSize
);
740 dest
+= clearValueSize
;
743 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
748 * Default fallback for \c dd_function_table::MapBufferRange().
749 * Called via glMapBufferRange().
752 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
753 GLsizeiptr length
, GLbitfield access
,
754 struct gl_buffer_object
*bufObj
,
755 gl_map_buffer_index index
)
758 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
759 /* Just return a direct pointer to the data */
760 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
761 bufObj
->Mappings
[index
].Length
= length
;
762 bufObj
->Mappings
[index
].Offset
= offset
;
763 bufObj
->Mappings
[index
].AccessFlags
= access
;
764 return bufObj
->Mappings
[index
].Pointer
;
769 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
770 * Called via glFlushMappedBufferRange().
773 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
774 GLintptr offset
, GLsizeiptr length
,
775 struct gl_buffer_object
*obj
,
776 gl_map_buffer_index index
)
788 * Default callback for \c dd_function_table::UnmapBuffer().
790 * The input parameters will have been already tested for errors.
792 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
795 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
796 gl_map_buffer_index index
)
799 /* XXX we might assert here that bufObj->Pointer is non-null */
800 bufObj
->Mappings
[index
].Pointer
= NULL
;
801 bufObj
->Mappings
[index
].Length
= 0;
802 bufObj
->Mappings
[index
].Offset
= 0;
803 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
809 * Default fallback for \c dd_function_table::CopyBufferSubData().
810 * Called via glCopyBufferSubData().
813 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
814 struct gl_buffer_object
*src
,
815 struct gl_buffer_object
*dst
,
816 GLintptr readOffset
, GLintptr writeOffset
,
819 GLubyte
*srcPtr
, *dstPtr
;
822 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
824 GL_MAP_WRITE_BIT
, src
,
830 srcPtr
+= readOffset
;
831 dstPtr
+= writeOffset
;
833 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
834 GL_MAP_READ_BIT
, src
,
836 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
838 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
842 /* Note: the src and dst regions will never overlap. Trying to do so
843 * would generate GL_INVALID_VALUE earlier.
845 if (srcPtr
&& dstPtr
)
846 memcpy(dstPtr
, srcPtr
, size
);
848 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
850 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
856 * Initialize the state associated with buffer objects
859 _mesa_init_buffer_objects( struct gl_context
*ctx
)
863 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
864 simple_mtx_init(&DummyBufferObject
.MinMaxCacheMutex
, mtx_plain
);
865 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
867 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
868 ctx
->Shared
->NullBufferObj
);
870 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
871 ctx
->Shared
->NullBufferObj
);
872 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
873 ctx
->Shared
->NullBufferObj
);
875 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
876 ctx
->Shared
->NullBufferObj
);
878 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
,
879 ctx
->Shared
->NullBufferObj
);
881 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
882 ctx
->Shared
->NullBufferObj
);
884 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
885 ctx
->Shared
->NullBufferObj
);
887 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
,
888 ctx
->Shared
->NullBufferObj
);
890 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
,
891 ctx
->Shared
->NullBufferObj
);
893 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
,
894 ctx
->Shared
->NullBufferObj
);
896 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
897 _mesa_reference_buffer_object(ctx
,
898 &ctx
->UniformBufferBindings
[i
].BufferObject
,
899 ctx
->Shared
->NullBufferObj
);
900 ctx
->UniformBufferBindings
[i
].Offset
= -1;
901 ctx
->UniformBufferBindings
[i
].Size
= -1;
904 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
905 _mesa_reference_buffer_object(ctx
,
906 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
907 ctx
->Shared
->NullBufferObj
);
908 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
909 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
912 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
913 _mesa_reference_buffer_object(ctx
,
914 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
915 ctx
->Shared
->NullBufferObj
);
916 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
917 ctx
->AtomicBufferBindings
[i
].Size
= 0;
923 _mesa_free_buffer_objects( struct gl_context
*ctx
)
927 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
929 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
930 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
932 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
934 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
936 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
938 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
940 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
, NULL
);
942 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, NULL
);
944 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
, NULL
);
946 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
947 _mesa_reference_buffer_object(ctx
,
948 &ctx
->UniformBufferBindings
[i
].BufferObject
,
952 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
953 _mesa_reference_buffer_object(ctx
,
954 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
958 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
959 _mesa_reference_buffer_object(ctx
,
960 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
967 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
969 struct gl_buffer_object
**buf_handle
,
972 struct gl_buffer_object
*buf
= *buf_handle
;
974 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
)) {
975 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
979 if (!buf
|| buf
== &DummyBufferObject
) {
980 /* If this is a new buffer object id, or one which was generated but
981 * never used before, allocate a buffer object now.
983 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
985 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
988 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
996 * Bind the specified target to buffer for the specified context.
997 * Called by glBindBuffer() and other functions.
1000 bind_buffer_object(struct gl_context
*ctx
,
1001 struct gl_buffer_object
**bindTarget
, GLuint buffer
)
1003 struct gl_buffer_object
*oldBufObj
;
1004 struct gl_buffer_object
*newBufObj
= NULL
;
1008 /* Get pointer to old buffer object (to be unbound) */
1009 oldBufObj
= *bindTarget
;
1010 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
1011 return; /* rebinding the same buffer object- no change */
1014 * Get pointer to new buffer object (newBufObj)
1017 /* The spec says there's not a buffer object named 0, but we use
1018 * one internally because it simplifies things.
1020 newBufObj
= ctx
->Shared
->NullBufferObj
;
1023 /* non-default buffer object */
1024 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1025 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1026 &newBufObj
, "glBindBuffer"))
1030 /* record usage history */
1031 if (bindTarget
== &ctx
->Pack
.BufferObj
) {
1032 newBufObj
->UsageHistory
|= USAGE_PIXEL_PACK_BUFFER
;
1035 /* bind new buffer */
1036 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1041 * Update the default buffer objects in the given context to reference those
1042 * specified in the shared state and release those referencing the old
1046 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1048 /* Bind the NullBufferObj to remove references to those
1049 * in the shared context hash table.
1051 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1052 bind_buffer_object(ctx
, &ctx
->Array
.VAO
->IndexBufferObj
, 0);
1053 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1054 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1060 * Return the gl_buffer_object for the given ID.
1061 * Always return NULL for ID 0.
1063 struct gl_buffer_object
*
1064 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1069 return (struct gl_buffer_object
*)
1070 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1074 struct gl_buffer_object
*
1075 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1080 return (struct gl_buffer_object
*)
1081 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1085 * A convenience function for direct state access functions that throws
1086 * GL_INVALID_OPERATION if buffer is not the name of an existing
1089 struct gl_buffer_object
*
1090 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1093 struct gl_buffer_object
*bufObj
;
1095 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1096 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1097 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1098 "%s(non-existent buffer object %u)", caller
, buffer
);
1107 * Look up a buffer object for a multi-bind function.
1109 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1110 * of generating an error if the buffer ID is not zero or the name
1111 * of an existing buffer object.
1113 * If the buffer ID refers to an existing buffer object, a pointer
1114 * to the buffer object is returned. If the ID is zero, a pointer
1115 * to the shared NullBufferObj is returned. If the ID is not zero
1116 * and does not refer to a valid buffer object, this function
1119 * This function assumes that the caller has already locked the
1120 * hash table mutex by calling
1121 * _mesa_HashLockMutex(ctx->Shared->BufferObjects).
1123 struct gl_buffer_object
*
1124 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1125 const GLuint
*buffers
,
1126 GLuint index
, const char *caller
)
1128 struct gl_buffer_object
*bufObj
;
1130 if (buffers
[index
] != 0) {
1131 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1133 /* The multi-bind functions don't create the buffer objects
1134 when they don't exist. */
1135 if (bufObj
== &DummyBufferObject
)
1138 bufObj
= ctx
->Shared
->NullBufferObj
;
1141 /* The ARB_multi_bind spec says:
1143 * "An INVALID_OPERATION error is generated if any value
1144 * in <buffers> is not zero or the name of an existing
1145 * buffer object (per binding)."
1147 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1148 "%s(buffers[%u]=%u is not zero or the name "
1149 "of an existing buffer object)",
1150 caller
, index
, buffers
[index
]);
1158 * If *ptr points to obj, set ptr = the Null/default buffer object.
1159 * This is a helper for buffer object deletion.
1160 * The GL spec says that deleting a buffer object causes it to get
1161 * unbound from all arrays in the current context.
1164 unbind(struct gl_context
*ctx
,
1165 struct gl_vertex_array_object
*vao
, unsigned index
,
1166 struct gl_buffer_object
*obj
)
1168 if (vao
->BufferBinding
[index
].BufferObj
== obj
) {
1169 _mesa_bind_vertex_buffer(ctx
, vao
, index
, ctx
->Shared
->NullBufferObj
,
1170 vao
->BufferBinding
[index
].Offset
,
1171 vao
->BufferBinding
[index
].Stride
);
1177 * Plug default/fallback buffer object functions into the device
1181 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1183 /* GL_ARB_vertex/pixel_buffer_object */
1184 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1185 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1186 driver
->BufferData
= buffer_data_fallback
;
1187 driver
->BufferSubData
= buffer_sub_data_fallback
;
1188 driver
->GetBufferSubData
= buffer_get_subdata
;
1189 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1191 /* GL_ARB_clear_buffer_object */
1192 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1194 /* GL_ARB_map_buffer_range */
1195 driver
->MapBufferRange
= map_buffer_range_fallback
;
1196 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1198 /* GL_ARB_copy_buffer */
1199 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1204 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1205 struct gl_buffer_object
*bufObj
)
1207 for (int i
= 0; i
< MAP_COUNT
; i
++) {
1208 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1209 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1210 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1211 bufObj
->Mappings
[i
].AccessFlags
= 0;
1217 /**********************************************************************/
1219 /**********************************************************************/
1222 _mesa_BindBuffer_no_error(GLenum target
, GLuint buffer
)
1224 GET_CURRENT_CONTEXT(ctx
);
1226 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1227 bind_buffer_object(ctx
, bindTarget
, buffer
);
1232 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1234 GET_CURRENT_CONTEXT(ctx
);
1236 if (MESA_VERBOSE
& VERBOSE_API
) {
1237 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1238 _mesa_enum_to_string(target
), buffer
);
1241 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1243 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target %s)",
1244 _mesa_enum_to_string(target
));
1248 bind_buffer_object(ctx
, bindTarget
, buffer
);
1252 * Binds a buffer object to a binding point.
1254 * The caller is responsible for validating the offset,
1255 * flushing the vertices and updating NewDriverState.
1258 set_buffer_binding(struct gl_context
*ctx
,
1259 struct gl_buffer_binding
*binding
,
1260 struct gl_buffer_object
*bufObj
,
1263 bool autoSize
, gl_buffer_usage usage
)
1265 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1267 binding
->Offset
= offset
;
1268 binding
->Size
= size
;
1269 binding
->AutomaticSize
= autoSize
;
1271 /* If this is a real buffer object, mark it has having been used
1272 * at some point as an atomic counter buffer.
1275 bufObj
->UsageHistory
|= usage
;
1279 set_buffer_multi_binding(struct gl_context
*ctx
,
1280 const GLuint
*buffers
,
1283 struct gl_buffer_binding
*binding
,
1287 gl_buffer_usage usage
)
1289 struct gl_buffer_object
*bufObj
;
1290 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[idx
])
1291 bufObj
= binding
->BufferObject
;
1293 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, idx
, caller
);
1296 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1297 set_buffer_binding(ctx
, binding
, bufObj
, -1, -1, !range
, usage
);
1299 set_buffer_binding(ctx
, binding
, bufObj
, offset
, size
, !range
, usage
);
1304 bind_buffer(struct gl_context
*ctx
,
1305 struct gl_buffer_binding
*binding
,
1306 struct gl_buffer_object
*bufObj
,
1310 uint64_t driver_state
,
1311 gl_buffer_usage usage
)
1313 if (binding
->BufferObject
== bufObj
&&
1314 binding
->Offset
== offset
&&
1315 binding
->Size
== size
&&
1316 binding
->AutomaticSize
== autoSize
) {
1320 FLUSH_VERTICES(ctx
, 0);
1321 ctx
->NewDriverState
|= driver_state
;
1323 set_buffer_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
, usage
);
1327 * Binds a buffer object to a uniform buffer binding point.
1329 * Unlike set_buffer_binding(), this function also flushes vertices
1330 * and updates NewDriverState. It also checks if the binding
1331 * has actually changed before updating it.
1334 bind_uniform_buffer(struct gl_context
*ctx
,
1336 struct gl_buffer_object
*bufObj
,
1341 bind_buffer(ctx
, &ctx
->UniformBufferBindings
[index
],
1342 bufObj
, offset
, size
, autoSize
,
1343 ctx
->DriverFlags
.NewUniformBuffer
,
1344 USAGE_UNIFORM_BUFFER
);
1348 * Binds a buffer object to a shader storage buffer binding point.
1350 * Unlike set_ssbo_binding(), this function also flushes vertices
1351 * and updates NewDriverState. It also checks if the binding
1352 * has actually changed before updating it.
1355 bind_shader_storage_buffer(struct gl_context
*ctx
,
1357 struct gl_buffer_object
*bufObj
,
1362 bind_buffer(ctx
, &ctx
->ShaderStorageBufferBindings
[index
],
1363 bufObj
, offset
, size
, autoSize
,
1364 ctx
->DriverFlags
.NewShaderStorageBuffer
,
1365 USAGE_SHADER_STORAGE_BUFFER
);
1369 * Binds a buffer object to an atomic buffer binding point.
1371 * Unlike set_atomic_binding(), this function also flushes vertices
1372 * and updates NewDriverState. It also checks if the binding
1373 * has actually changed before updating it.
1376 bind_atomic_buffer(struct gl_context
*ctx
, unsigned index
,
1377 struct gl_buffer_object
*bufObj
, GLintptr offset
,
1378 GLsizeiptr size
, GLboolean autoSize
)
1380 bind_buffer(ctx
, &ctx
->AtomicBufferBindings
[index
],
1381 bufObj
, offset
, size
, autoSize
,
1382 ctx
->DriverFlags
.NewAtomicBuffer
,
1383 USAGE_ATOMIC_COUNTER_BUFFER
);
1387 * Bind a buffer object to a uniform block binding point.
1388 * As above, but offset = 0.
1391 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
1393 struct gl_buffer_object
*bufObj
)
1395 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
1396 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1400 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
1402 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1403 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1405 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1409 * Bind a buffer object to a shader storage block binding point.
1410 * As above, but offset = 0.
1413 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
1415 struct gl_buffer_object
*bufObj
)
1417 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
1418 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1422 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
1424 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1425 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1427 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1431 * Bind a buffer object to a shader storage block binding point.
1432 * As above, but offset = 0.
1435 bind_buffer_base_atomic_buffer(struct gl_context
*ctx
,
1437 struct gl_buffer_object
*bufObj
)
1439 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
1440 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1444 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
1446 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1447 bind_atomic_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1449 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1453 * Delete a set of buffer objects.
1455 * \param n Number of buffer objects to delete.
1456 * \param ids Array of \c n buffer object IDs.
1459 delete_buffers(struct gl_context
*ctx
, GLsizei n
, const GLuint
*ids
)
1461 FLUSH_VERTICES(ctx
, 0);
1463 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1465 for (GLsizei i
= 0; i
< n
; i
++) {
1466 struct gl_buffer_object
*bufObj
=
1467 _mesa_lookup_bufferobj_locked(ctx
, ids
[i
]);
1469 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1472 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1474 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1476 /* unbind any vertex pointers bound to this buffer */
1477 for (j
= 0; j
< ARRAY_SIZE(vao
->BufferBinding
); j
++) {
1478 unbind(ctx
, vao
, j
, bufObj
);
1481 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1482 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1484 if (vao
->IndexBufferObj
== bufObj
) {
1485 bind_buffer_object(ctx
, &vao
->IndexBufferObj
, 0);
1488 /* unbind ARB_draw_indirect binding point */
1489 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1490 bind_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, 0);
1493 /* unbind ARB_indirect_parameters binding point */
1494 if (ctx
->ParameterBuffer
== bufObj
) {
1495 bind_buffer_object(ctx
, &ctx
->ParameterBuffer
, 0);
1498 /* unbind ARB_compute_shader binding point */
1499 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1500 bind_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, 0);
1503 /* unbind ARB_copy_buffer binding points */
1504 if (ctx
->CopyReadBuffer
== bufObj
) {
1505 bind_buffer_object(ctx
, &ctx
->CopyReadBuffer
, 0);
1507 if (ctx
->CopyWriteBuffer
== bufObj
) {
1508 bind_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, 0);
1511 /* unbind transform feedback binding points */
1512 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1513 bind_buffer_object(ctx
, &ctx
->TransformFeedback
.CurrentBuffer
, 0);
1515 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1516 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1517 _mesa_bind_buffer_base_transform_feedback(ctx
,
1518 ctx
->TransformFeedback
.CurrentObject
,
1519 j
, ctx
->Shared
->NullBufferObj
,
1524 /* unbind UBO binding points */
1525 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1526 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1527 bind_buffer_base_uniform_buffer(ctx
, j
,
1528 ctx
->Shared
->NullBufferObj
);
1532 if (ctx
->UniformBuffer
== bufObj
) {
1533 bind_buffer_object(ctx
, &ctx
->UniformBuffer
, 0);
1536 /* unbind SSBO binding points */
1537 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1538 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1539 bind_buffer_base_shader_storage_buffer(ctx
, j
,
1540 ctx
->Shared
->NullBufferObj
);
1544 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1545 bind_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, 0);
1548 /* unbind Atomci Buffer binding points */
1549 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1550 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1551 bind_buffer_base_atomic_buffer(ctx
, j
,
1552 ctx
->Shared
->NullBufferObj
);
1556 if (ctx
->AtomicBuffer
== bufObj
) {
1557 bind_buffer_object(ctx
, &ctx
->AtomicBuffer
, 0);
1560 /* unbind any pixel pack/unpack pointers bound to this buffer */
1561 if (ctx
->Pack
.BufferObj
== bufObj
) {
1562 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1564 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1565 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1568 if (ctx
->Texture
.BufferObject
== bufObj
) {
1569 bind_buffer_object(ctx
, &ctx
->Texture
.BufferObject
, 0);
1572 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1573 bind_buffer_object(ctx
, &ctx
->ExternalVirtualMemoryBuffer
, 0);
1576 /* unbind query buffer binding point */
1577 if (ctx
->QueryBuffer
== bufObj
) {
1578 bind_buffer_object(ctx
, &ctx
->QueryBuffer
, 0);
1581 /* The ID is immediately freed for re-use */
1582 _mesa_HashRemoveLocked(ctx
->Shared
->BufferObjects
, ids
[i
]);
1583 /* Make sure we do not run into the classic ABA problem on bind.
1584 * We don't want to allow re-binding a buffer object that's been
1585 * "deleted" by glDeleteBuffers().
1587 * The explicit rebinding to the default object in the current context
1588 * prevents the above in the current context, but another context
1589 * sharing the same objects might suffer from this problem.
1590 * The alternative would be to do the hash lookup in any case on bind
1591 * which would introduce more runtime overhead than this.
1593 bufObj
->DeletePending
= GL_TRUE
;
1594 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1598 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1603 _mesa_DeleteBuffers_no_error(GLsizei n
, const GLuint
*ids
)
1605 GET_CURRENT_CONTEXT(ctx
);
1606 delete_buffers(ctx
, n
, ids
);
1611 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1613 GET_CURRENT_CONTEXT(ctx
);
1616 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1620 delete_buffers(ctx
, n
, ids
);
1625 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1626 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1630 create_buffers(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1633 struct gl_buffer_object
*buf
;
1639 * This must be atomic (generation and allocation of buffer object IDs)
1641 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1643 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1645 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1646 * DummyBufferObject. Otherwise, create a new buffer object and insert
1649 for (int i
= 0; i
< n
; i
++) {
1650 buffers
[i
] = first
+ i
;
1652 assert(ctx
->Driver
.NewBufferObject
);
1653 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1655 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCreateBuffers");
1656 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1661 buf
= &DummyBufferObject
;
1663 _mesa_HashInsertLocked(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1666 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1671 create_buffers_err(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1673 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1675 if (MESA_VERBOSE
& VERBOSE_API
)
1676 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1679 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1683 create_buffers(ctx
, n
, buffers
, dsa
);
1687 * Generate a set of unique buffer object IDs and store them in \c buffers.
1689 * \param n Number of IDs to generate.
1690 * \param buffers Array of \c n locations to store the IDs.
1693 _mesa_GenBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1695 GET_CURRENT_CONTEXT(ctx
);
1696 create_buffers(ctx
, n
, buffers
, false);
1701 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1703 GET_CURRENT_CONTEXT(ctx
);
1704 create_buffers_err(ctx
, n
, buffers
, false);
1708 * Create a set of buffer objects and store their unique IDs in \c buffers.
1710 * \param n Number of IDs to generate.
1711 * \param buffers Array of \c n locations to store the IDs.
1714 _mesa_CreateBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1716 GET_CURRENT_CONTEXT(ctx
);
1717 create_buffers(ctx
, n
, buffers
, true);
1722 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1724 GET_CURRENT_CONTEXT(ctx
);
1725 create_buffers_err(ctx
, n
, buffers
, true);
1730 * Determine if ID is the name of a buffer object.
1732 * \param id ID of the potential buffer object.
1733 * \return \c GL_TRUE if \c id is the name of a buffer object,
1734 * \c GL_FALSE otherwise.
1736 GLboolean GLAPIENTRY
1737 _mesa_IsBuffer(GLuint id
)
1739 struct gl_buffer_object
*bufObj
;
1740 GET_CURRENT_CONTEXT(ctx
);
1741 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1743 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1745 return bufObj
&& bufObj
!= &DummyBufferObject
;
1750 validate_buffer_storage(struct gl_context
*ctx
,
1751 struct gl_buffer_object
*bufObj
, GLsizeiptr size
,
1752 GLbitfield flags
, const char *func
)
1755 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1759 GLbitfield valid_flags
= GL_MAP_READ_BIT
|
1761 GL_MAP_PERSISTENT_BIT
|
1762 GL_MAP_COHERENT_BIT
|
1763 GL_DYNAMIC_STORAGE_BIT
|
1764 GL_CLIENT_STORAGE_BIT
;
1766 if (ctx
->Extensions
.ARB_sparse_buffer
)
1767 valid_flags
|= GL_SPARSE_STORAGE_BIT_ARB
;
1769 if (flags
& ~valid_flags
) {
1770 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1774 /* The Errors section of the GL_ARB_sparse_buffer spec says:
1776 * "INVALID_VALUE is generated by BufferStorage if <flags> contains
1777 * SPARSE_STORAGE_BIT_ARB and <flags> also contains any combination of
1778 * MAP_READ_BIT or MAP_WRITE_BIT."
1780 if (flags
& GL_SPARSE_STORAGE_BIT_ARB
&&
1781 flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) {
1782 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(SPARSE_STORAGE and READ/WRITE)", func
);
1786 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1787 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1788 _mesa_error(ctx
, GL_INVALID_VALUE
,
1789 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1793 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1794 _mesa_error(ctx
, GL_INVALID_VALUE
,
1795 "%s(COHERENT and flags!=PERSISTENT)", func
);
1799 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
1800 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1809 buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1810 struct gl_memory_object
*memObj
, GLenum target
,
1811 GLsizeiptr size
, const GLvoid
*data
, GLbitfield flags
,
1812 GLuint64 offset
, const char *func
)
1816 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1817 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1819 FLUSH_VERTICES(ctx
, 0);
1821 bufObj
->Written
= GL_TRUE
;
1822 bufObj
->Immutable
= GL_TRUE
;
1823 bufObj
->MinMaxCacheDirty
= true;
1826 assert(ctx
->Driver
.BufferDataMem
);
1827 res
= ctx
->Driver
.BufferDataMem(ctx
, target
, size
, memObj
, offset
,
1828 GL_DYNAMIC_DRAW
, bufObj
);
1831 assert(ctx
->Driver
.BufferData
);
1832 res
= ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1837 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1838 /* Even though the interaction between AMD_pinned_memory and
1839 * glBufferStorage is not described in the spec, Graham Sellers
1840 * said that it should behave the same as glBufferData.
1842 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1845 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1851 static ALWAYS_INLINE
void
1852 inlined_buffer_storage(GLenum target
, GLuint buffer
, GLsizeiptr size
,
1853 const GLvoid
*data
, GLbitfield flags
,
1854 GLuint memory
, GLuint64 offset
,
1855 bool dsa
, bool mem
, bool no_error
, const char *func
)
1857 GET_CURRENT_CONTEXT(ctx
);
1858 struct gl_buffer_object
*bufObj
;
1859 struct gl_memory_object
*memObj
= NULL
;
1863 if (!ctx
->Extensions
.EXT_memory_object
) {
1864 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
1868 /* From the EXT_external_objects spec:
1870 * "An INVALID_VALUE error is generated by BufferStorageMemEXT and
1871 * NamedBufferStorageMemEXT if <memory> is 0, or ..."
1874 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(memory == 0)", func
);
1878 memObj
= _mesa_lookup_memory_object(ctx
, memory
);
1882 /* From the EXT_external_objects spec:
1884 * "An INVALID_OPERATION error is generated if <memory> names a
1885 * valid memory object which has no associated memory."
1887 if (!no_error
&& !memObj
->Immutable
) {
1888 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no associated memory)",
1896 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1898 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
1904 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
1905 bufObj
= *bufObjPtr
;
1907 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
1913 if (no_error
|| validate_buffer_storage(ctx
, bufObj
, size
, flags
, func
))
1914 buffer_storage(ctx
, bufObj
, memObj
, target
, size
, data
, flags
, offset
, func
);
1919 _mesa_BufferStorage_no_error(GLenum target
, GLsizeiptr size
,
1920 const GLvoid
*data
, GLbitfield flags
)
1922 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1923 false, false, true, "glBufferStorage");
1928 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1931 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1932 false, false, false, "glBufferStorage");
1937 _mesa_BufferStorageMemEXT(GLenum target
, GLsizeiptr size
,
1938 GLuint memory
, GLuint64 offset
)
1940 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1941 false, true, false, "glBufferStorageMemEXT");
1946 _mesa_BufferStorageMemEXT_no_error(GLenum target
, GLsizeiptr size
,
1947 GLuint memory
, GLuint64 offset
)
1949 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1950 false, true, true, "glBufferStorageMemEXT");
1955 _mesa_NamedBufferStorage_no_error(GLuint buffer
, GLsizeiptr size
,
1956 const GLvoid
*data
, GLbitfield flags
)
1958 /* In direct state access, buffer objects have an unspecified target
1959 * since they are not required to be bound.
1961 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1962 true, false, true, "glNamedBufferStorage");
1967 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1970 /* In direct state access, buffer objects have an unspecified target
1971 * since they are not required to be bound.
1973 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1974 true, false, false, "glNamedBufferStorage");
1978 _mesa_NamedBufferStorageMemEXT(GLuint buffer
, GLsizeiptr size
,
1979 GLuint memory
, GLuint64 offset
)
1981 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
1982 true, true, false, "glNamedBufferStorageMemEXT");
1987 _mesa_NamedBufferStorageMemEXT_no_error(GLuint buffer
, GLsizeiptr size
,
1988 GLuint memory
, GLuint64 offset
)
1990 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
1991 true, true, true, "glNamedBufferStorageMemEXT");
1995 static ALWAYS_INLINE
void
1996 buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1997 GLenum target
, GLsizeiptr size
, const GLvoid
*data
, GLenum usage
,
1998 const char *func
, bool no_error
)
2002 if (MESA_VERBOSE
& VERBOSE_API
) {
2003 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
2005 _mesa_enum_to_string(target
),
2006 (long int) size
, data
,
2007 _mesa_enum_to_string(usage
));
2012 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
2017 case GL_STREAM_DRAW_ARB
:
2018 valid_usage
= (ctx
->API
!= API_OPENGLES
);
2020 case GL_STATIC_DRAW_ARB
:
2021 case GL_DYNAMIC_DRAW_ARB
:
2024 case GL_STREAM_READ_ARB
:
2025 case GL_STREAM_COPY_ARB
:
2026 case GL_STATIC_READ_ARB
:
2027 case GL_STATIC_COPY_ARB
:
2028 case GL_DYNAMIC_READ_ARB
:
2029 case GL_DYNAMIC_COPY_ARB
:
2030 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
2033 valid_usage
= false;
2038 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
2039 _mesa_enum_to_string(usage
));
2043 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
2044 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
2049 /* Unmap the existing buffer. We'll replace it now. Not an error. */
2050 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
2052 FLUSH_VERTICES(ctx
, 0);
2054 bufObj
->Written
= GL_TRUE
;
2055 bufObj
->MinMaxCacheDirty
= true;
2058 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
2059 bufObj
->Name
, size
, data
, usage
);
2066 assert(ctx
->Driver
.BufferData
);
2067 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
2070 GL_DYNAMIC_STORAGE_BIT
,
2072 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
2074 /* From GL_AMD_pinned_memory:
2076 * INVALID_OPERATION is generated by BufferData if <target> is
2077 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
2078 * mapped to the GPU address space.
2080 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2083 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
2089 buffer_data_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2090 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2091 GLenum usage
, const char *func
)
2093 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, false);
2097 buffer_data_no_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2098 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2099 GLenum usage
, const char *func
)
2101 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, true);
2105 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2106 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2107 GLenum usage
, const char *func
)
2109 buffer_data_error(ctx
, bufObj
, target
, size
, data
, usage
, func
);
2113 _mesa_BufferData_no_error(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2116 GET_CURRENT_CONTEXT(ctx
);
2118 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2119 buffer_data_no_error(ctx
, *bufObj
, target
, size
, data
, usage
,
2124 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
2125 const GLvoid
*data
, GLenum usage
)
2127 GET_CURRENT_CONTEXT(ctx
);
2128 struct gl_buffer_object
*bufObj
;
2130 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
2134 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
2139 _mesa_NamedBufferData_no_error(GLuint buffer
, GLsizeiptr size
,
2140 const GLvoid
*data
, GLenum usage
)
2142 GET_CURRENT_CONTEXT(ctx
);
2144 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2145 buffer_data_no_error(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2146 "glNamedBufferData");
2150 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2153 GET_CURRENT_CONTEXT(ctx
);
2154 struct gl_buffer_object
*bufObj
;
2156 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
2160 /* In direct state access, buffer objects have an unspecified target since
2161 * they are not required to be bound.
2163 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2164 "glNamedBufferData");
2169 validate_buffer_sub_data(struct gl_context
*ctx
,
2170 struct gl_buffer_object
*bufObj
,
2171 GLintptr offset
, GLsizeiptr size
,
2174 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2176 /* error already recorded */
2180 if (bufObj
->Immutable
&&
2181 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
2182 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2186 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2187 bufObj
->Usage
== GL_STATIC_COPY
) &&
2188 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
- 1) {
2189 /* If the application declared the buffer as static draw/copy or stream
2190 * draw, it should not be frequently modified with glBufferSubData.
2192 BUFFER_USAGE_WARNING(ctx
,
2193 "using %s(buffer %u, offset %u, size %u) to "
2194 "update a %s buffer",
2195 func
, bufObj
->Name
, offset
, size
,
2196 _mesa_enum_to_string(bufObj
->Usage
));
2204 * Implementation for glBufferSubData and glNamedBufferSubData.
2206 * \param ctx GL context.
2207 * \param bufObj The buffer object.
2208 * \param offset Offset of the first byte of the subdata range.
2209 * \param size Size, in bytes, of the subdata range.
2210 * \param data The data store.
2211 * \param func Name of calling function for recording errors.
2215 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2216 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
)
2221 bufObj
->NumSubDataCalls
++;
2222 bufObj
->Written
= GL_TRUE
;
2223 bufObj
->MinMaxCacheDirty
= true;
2225 assert(ctx
->Driver
.BufferSubData
);
2226 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
2230 static ALWAYS_INLINE
void
2231 buffer_sub_data(GLenum target
, GLuint buffer
, GLintptr offset
,
2232 GLsizeiptr size
, const GLvoid
*data
,
2233 bool dsa
, bool no_error
, const char *func
)
2235 GET_CURRENT_CONTEXT(ctx
);
2236 struct gl_buffer_object
*bufObj
;
2240 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2242 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
2248 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2249 bufObj
= *bufObjPtr
;
2251 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
2257 if (no_error
|| validate_buffer_sub_data(ctx
, bufObj
, offset
, size
, func
))
2258 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
);
2263 _mesa_BufferSubData_no_error(GLenum target
, GLintptr offset
,
2264 GLsizeiptr size
, const GLvoid
*data
)
2266 buffer_sub_data(target
, 0, offset
, size
, data
, false, true,
2272 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
2273 GLsizeiptr size
, const GLvoid
*data
)
2275 buffer_sub_data(target
, 0, offset
, size
, data
, false, false,
2280 _mesa_NamedBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
2281 GLsizeiptr size
, const GLvoid
*data
)
2283 buffer_sub_data(0, buffer
, offset
, size
, data
, true, true,
2284 "glNamedBufferSubData");
2288 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
2289 GLsizeiptr size
, const GLvoid
*data
)
2291 buffer_sub_data(0, buffer
, offset
, size
, data
, true, false,
2292 "glNamedBufferSubData");
2297 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
2298 GLsizeiptr size
, GLvoid
*data
)
2300 GET_CURRENT_CONTEXT(ctx
);
2301 struct gl_buffer_object
*bufObj
;
2303 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
2304 GL_INVALID_OPERATION
);
2308 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2309 "glGetBufferSubData")) {
2313 assert(ctx
->Driver
.GetBufferSubData
);
2314 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2318 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
2319 GLsizeiptr size
, GLvoid
*data
)
2321 GET_CURRENT_CONTEXT(ctx
);
2322 struct gl_buffer_object
*bufObj
;
2324 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2325 "glGetNamedBufferSubData");
2329 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2330 "glGetNamedBufferSubData")) {
2334 assert(ctx
->Driver
.GetBufferSubData
);
2335 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2340 * \param subdata true if caller is *SubData, false if *Data
2342 static ALWAYS_INLINE
void
2343 clear_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2344 GLenum internalformat
, GLintptr offset
, GLsizeiptr size
,
2345 GLenum format
, GLenum type
, const GLvoid
*data
,
2346 const char *func
, bool subdata
, bool no_error
)
2348 mesa_format mesaFormat
;
2349 GLubyte clearValue
[MAX_PIXEL_BYTES
];
2350 GLsizeiptr clearValueSize
;
2352 /* This checks for disallowed mappings. */
2353 if (!no_error
&& !buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2359 mesaFormat
= _mesa_get_texbuffer_format(ctx
, internalformat
);
2361 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
2362 format
, type
, func
);
2365 if (mesaFormat
== MESA_FORMAT_NONE
)
2368 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
2370 (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0)) {
2371 _mesa_error(ctx
, GL_INVALID_VALUE
,
2372 "%s(offset or size is not a multiple of "
2373 "internalformat size)", func
);
2377 /* Bail early. Negative size has already been checked. */
2381 bufObj
->MinMaxCacheDirty
= true;
2384 /* clear to zeros, per the spec */
2385 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2386 NULL
, clearValueSize
, bufObj
);
2390 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
2391 format
, type
, data
, func
)) {
2395 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2396 clearValue
, clearValueSize
, bufObj
);
2400 clear_buffer_sub_data_error(struct gl_context
*ctx
,
2401 struct gl_buffer_object
*bufObj
,
2402 GLenum internalformat
, GLintptr offset
,
2403 GLsizeiptr size
, GLenum format
, GLenum type
,
2404 const GLvoid
*data
, const char *func
, bool subdata
)
2406 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2407 type
, data
, func
, subdata
, false);
2412 clear_buffer_sub_data_no_error(struct gl_context
*ctx
,
2413 struct gl_buffer_object
*bufObj
,
2414 GLenum internalformat
, GLintptr offset
,
2415 GLsizeiptr size
, GLenum format
, GLenum type
,
2416 const GLvoid
*data
, const char *func
,
2419 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2420 type
, data
, func
, subdata
, true);
2425 _mesa_ClearBufferData_no_error(GLenum target
, GLenum internalformat
,
2426 GLenum format
, GLenum type
, const GLvoid
*data
)
2428 GET_CURRENT_CONTEXT(ctx
);
2430 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2431 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, 0,
2432 (*bufObj
)->Size
, format
, type
, data
,
2433 "glClearBufferData", false);
2438 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
2439 GLenum type
, const GLvoid
*data
)
2441 GET_CURRENT_CONTEXT(ctx
);
2442 struct gl_buffer_object
*bufObj
;
2444 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
2448 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2449 format
, type
, data
, "glClearBufferData", false);
2454 _mesa_ClearNamedBufferData_no_error(GLuint buffer
, GLenum internalformat
,
2455 GLenum format
, GLenum type
,
2458 GET_CURRENT_CONTEXT(ctx
);
2460 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2461 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2462 format
, type
, data
, "glClearNamedBufferData",
2468 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
2469 GLenum format
, GLenum type
, const GLvoid
*data
)
2471 GET_CURRENT_CONTEXT(ctx
);
2472 struct gl_buffer_object
*bufObj
;
2474 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
2478 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2479 format
, type
, data
, "glClearNamedBufferData",
2485 _mesa_ClearBufferSubData_no_error(GLenum target
, GLenum internalformat
,
2486 GLintptr offset
, GLsizeiptr size
,
2487 GLenum format
, GLenum type
,
2490 GET_CURRENT_CONTEXT(ctx
);
2492 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2493 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, offset
, size
,
2494 format
, type
, data
, "glClearBufferSubData",
2500 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
2501 GLintptr offset
, GLsizeiptr size
,
2502 GLenum format
, GLenum type
,
2505 GET_CURRENT_CONTEXT(ctx
);
2506 struct gl_buffer_object
*bufObj
;
2508 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
2512 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2513 format
, type
, data
, "glClearBufferSubData",
2519 _mesa_ClearNamedBufferSubData_no_error(GLuint buffer
, GLenum internalformat
,
2520 GLintptr offset
, GLsizeiptr size
,
2521 GLenum format
, GLenum type
,
2524 GET_CURRENT_CONTEXT(ctx
);
2526 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2527 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, offset
, size
,
2529 "glClearNamedBufferSubData", true);
2534 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
2535 GLintptr offset
, GLsizeiptr size
,
2536 GLenum format
, GLenum type
,
2539 GET_CURRENT_CONTEXT(ctx
);
2540 struct gl_buffer_object
*bufObj
;
2542 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2543 "glClearNamedBufferSubData");
2547 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2548 format
, type
, data
, "glClearNamedBufferSubData",
2553 unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
2555 GLboolean status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2556 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2557 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2558 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2559 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2565 validate_and_unmap_buffer(struct gl_context
*ctx
,
2566 struct gl_buffer_object
*bufObj
,
2569 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2571 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2572 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2573 "%s(buffer is not mapped)", func
);
2578 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2579 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2581 /* check that last 100 bytes are still = magic value */
2582 for (i
= 0; i
< 100; i
++) {
2583 GLuint pos
= bufObj
->Size
- i
- 1;
2584 if (buf
[pos
] != 123) {
2585 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2586 " at position %d (value = %u)\n",
2594 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2595 GLuint i
, unchanged
= 0;
2596 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2598 /* check which bytes changed */
2599 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2600 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2607 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2608 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2613 return unmap_buffer(ctx
, bufObj
);
2616 GLboolean GLAPIENTRY
2617 _mesa_UnmapBuffer_no_error(GLenum target
)
2619 GET_CURRENT_CONTEXT(ctx
);
2620 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2621 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2623 return unmap_buffer(ctx
, bufObj
);
2626 GLboolean GLAPIENTRY
2627 _mesa_UnmapBuffer(GLenum target
)
2629 GET_CURRENT_CONTEXT(ctx
);
2630 struct gl_buffer_object
*bufObj
;
2632 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2636 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2639 GLboolean GLAPIENTRY
2640 _mesa_UnmapNamedBuffer_no_error(GLuint buffer
)
2642 GET_CURRENT_CONTEXT(ctx
);
2643 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2645 return unmap_buffer(ctx
, bufObj
);
2648 GLboolean GLAPIENTRY
2649 _mesa_UnmapNamedBuffer(GLuint buffer
)
2651 GET_CURRENT_CONTEXT(ctx
);
2652 struct gl_buffer_object
*bufObj
;
2654 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2658 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2663 get_buffer_parameter(struct gl_context
*ctx
,
2664 struct gl_buffer_object
*bufObj
, GLenum pname
,
2665 GLint64
*params
, const char *func
)
2668 case GL_BUFFER_SIZE_ARB
:
2669 *params
= bufObj
->Size
;
2671 case GL_BUFFER_USAGE_ARB
:
2672 *params
= bufObj
->Usage
;
2674 case GL_BUFFER_ACCESS_ARB
:
2675 *params
= simplified_access_mode(ctx
,
2676 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2678 case GL_BUFFER_MAPPED_ARB
:
2679 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2681 case GL_BUFFER_ACCESS_FLAGS
:
2682 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2684 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2686 case GL_BUFFER_MAP_OFFSET
:
2687 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2689 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2691 case GL_BUFFER_MAP_LENGTH
:
2692 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2694 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2696 case GL_BUFFER_IMMUTABLE_STORAGE
:
2697 if (!ctx
->Extensions
.ARB_buffer_storage
)
2699 *params
= bufObj
->Immutable
;
2701 case GL_BUFFER_STORAGE_FLAGS
:
2702 if (!ctx
->Extensions
.ARB_buffer_storage
)
2704 *params
= bufObj
->StorageFlags
;
2713 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2714 _mesa_enum_to_string(pname
));
2719 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2721 GET_CURRENT_CONTEXT(ctx
);
2722 struct gl_buffer_object
*bufObj
;
2725 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2726 GL_INVALID_OPERATION
);
2730 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2731 "glGetBufferParameteriv"))
2732 return; /* Error already recorded. */
2734 *params
= (GLint
) parameter
;
2738 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2740 GET_CURRENT_CONTEXT(ctx
);
2741 struct gl_buffer_object
*bufObj
;
2744 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2745 GL_INVALID_OPERATION
);
2749 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2750 "glGetBufferParameteri64v"))
2751 return; /* Error already recorded. */
2753 *params
= parameter
;
2757 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2759 GET_CURRENT_CONTEXT(ctx
);
2760 struct gl_buffer_object
*bufObj
;
2763 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2764 "glGetNamedBufferParameteriv");
2768 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2769 "glGetNamedBufferParameteriv"))
2770 return; /* Error already recorded. */
2772 *params
= (GLint
) parameter
;
2776 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2779 GET_CURRENT_CONTEXT(ctx
);
2780 struct gl_buffer_object
*bufObj
;
2783 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2784 "glGetNamedBufferParameteri64v");
2788 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2789 "glGetNamedBufferParameteri64v"))
2790 return; /* Error already recorded. */
2792 *params
= parameter
;
2797 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2799 GET_CURRENT_CONTEXT(ctx
);
2800 struct gl_buffer_object
*bufObj
;
2802 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2803 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2804 "GL_BUFFER_MAP_POINTER)");
2808 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2809 GL_INVALID_OPERATION
);
2813 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2817 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2819 GET_CURRENT_CONTEXT(ctx
);
2820 struct gl_buffer_object
*bufObj
;
2822 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2823 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2824 "GL_BUFFER_MAP_POINTER)");
2828 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2829 "glGetNamedBufferPointerv");
2833 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2838 copy_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*src
,
2839 struct gl_buffer_object
*dst
, GLintptr readOffset
,
2840 GLintptr writeOffset
, GLsizeiptr size
, const char *func
)
2842 if (_mesa_check_disallowed_mapping(src
)) {
2843 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2844 "%s(readBuffer is mapped)", func
);
2848 if (_mesa_check_disallowed_mapping(dst
)) {
2849 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2850 "%s(writeBuffer is mapped)", func
);
2854 if (readOffset
< 0) {
2855 _mesa_error(ctx
, GL_INVALID_VALUE
,
2856 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2860 if (writeOffset
< 0) {
2861 _mesa_error(ctx
, GL_INVALID_VALUE
,
2862 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2867 _mesa_error(ctx
, GL_INVALID_VALUE
,
2868 "%s(size %d < 0)", func
, (int) size
);
2872 if (readOffset
+ size
> src
->Size
) {
2873 _mesa_error(ctx
, GL_INVALID_VALUE
,
2874 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2875 (int) readOffset
, (int) size
, (int) src
->Size
);
2879 if (writeOffset
+ size
> dst
->Size
) {
2880 _mesa_error(ctx
, GL_INVALID_VALUE
,
2881 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2882 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2887 if (readOffset
+ size
<= writeOffset
) {
2890 else if (writeOffset
+ size
<= readOffset
) {
2894 /* overlapping src/dst is illegal */
2895 _mesa_error(ctx
, GL_INVALID_VALUE
,
2896 "%s(overlapping src/dst)", func
);
2901 dst
->MinMaxCacheDirty
= true;
2903 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2907 _mesa_CopyBufferSubData_no_error(GLenum readTarget
, GLenum writeTarget
,
2908 GLintptr readOffset
, GLintptr writeOffset
,
2911 GET_CURRENT_CONTEXT(ctx
);
2913 struct gl_buffer_object
**src_ptr
= get_buffer_target(ctx
, readTarget
);
2914 struct gl_buffer_object
*src
= *src_ptr
;
2916 struct gl_buffer_object
**dst_ptr
= get_buffer_target(ctx
, writeTarget
);
2917 struct gl_buffer_object
*dst
= *dst_ptr
;
2919 dst
->MinMaxCacheDirty
= true;
2920 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
2925 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2926 GLintptr readOffset
, GLintptr writeOffset
,
2929 GET_CURRENT_CONTEXT(ctx
);
2930 struct gl_buffer_object
*src
, *dst
;
2932 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2933 GL_INVALID_OPERATION
);
2937 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2938 GL_INVALID_OPERATION
);
2942 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2943 "glCopyBufferSubData");
2947 _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer
, GLuint writeBuffer
,
2948 GLintptr readOffset
,
2949 GLintptr writeOffset
, GLsizeiptr size
)
2951 GET_CURRENT_CONTEXT(ctx
);
2953 struct gl_buffer_object
*src
= _mesa_lookup_bufferobj(ctx
, readBuffer
);
2954 struct gl_buffer_object
*dst
= _mesa_lookup_bufferobj(ctx
, writeBuffer
);
2956 dst
->MinMaxCacheDirty
= true;
2957 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
2962 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2963 GLintptr readOffset
, GLintptr writeOffset
,
2966 GET_CURRENT_CONTEXT(ctx
);
2967 struct gl_buffer_object
*src
, *dst
;
2969 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2970 "glCopyNamedBufferSubData");
2974 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2975 "glCopyNamedBufferSubData");
2979 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2980 "glCopyNamedBufferSubData");
2984 validate_map_buffer_range(struct gl_context
*ctx
,
2985 struct gl_buffer_object
*bufObj
, GLintptr offset
,
2986 GLsizeiptr length
, GLbitfield access
,
2989 GLbitfield allowed_access
;
2991 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, false);
2994 _mesa_error(ctx
, GL_INVALID_VALUE
,
2995 "%s(offset %ld < 0)", func
, (long) offset
);
3000 _mesa_error(ctx
, GL_INVALID_VALUE
,
3001 "%s(length %ld < 0)", func
, (long) length
);
3005 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
3007 * "An INVALID_OPERATION error is generated for any of the following
3010 * * <length> is zero."
3012 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
3013 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
3017 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
3021 allowed_access
= GL_MAP_READ_BIT
|
3023 GL_MAP_INVALIDATE_RANGE_BIT
|
3024 GL_MAP_INVALIDATE_BUFFER_BIT
|
3025 GL_MAP_FLUSH_EXPLICIT_BIT
|
3026 GL_MAP_UNSYNCHRONIZED_BIT
;
3028 if (ctx
->Extensions
.ARB_buffer_storage
) {
3029 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
3030 GL_MAP_COHERENT_BIT
;
3033 if (access
& ~allowed_access
) {
3034 /* generate an error if any bits other than those allowed are set */
3035 _mesa_error(ctx
, GL_INVALID_VALUE
,
3036 "%s(access has undefined bits set)", func
);
3040 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
3041 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3042 "%s(access indicates neither read or write)", func
);
3046 if ((access
& GL_MAP_READ_BIT
) &&
3047 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
3048 GL_MAP_INVALIDATE_BUFFER_BIT
|
3049 GL_MAP_UNSYNCHRONIZED_BIT
))) {
3050 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3051 "%s(read access with disallowed bits)", func
);
3055 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
3056 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
3057 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3058 "%s(access has flush explicit without write)", func
);
3062 if (access
& GL_MAP_READ_BIT
&&
3063 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
3064 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3065 "%s(buffer does not allow read access)", func
);
3069 if (access
& GL_MAP_WRITE_BIT
&&
3070 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
3071 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3072 "%s(buffer does not allow write access)", func
);
3076 if (access
& GL_MAP_COHERENT_BIT
&&
3077 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
3078 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3079 "%s(buffer does not allow coherent access)", func
);
3083 if (access
& GL_MAP_PERSISTENT_BIT
&&
3084 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
3085 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3086 "%s(buffer does not allow persistent access)", func
);
3090 if (offset
+ length
> bufObj
->Size
) {
3091 _mesa_error(ctx
, GL_INVALID_VALUE
,
3092 "%s(offset %lu + length %lu > buffer_size %lu)", func
,
3093 (unsigned long) offset
, (unsigned long) length
,
3094 (unsigned long) bufObj
->Size
);
3098 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3099 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3100 "%s(buffer already mapped)", func
);
3104 if (access
& GL_MAP_WRITE_BIT
) {
3105 bufObj
->NumMapBufferWriteCalls
++;
3106 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
3107 bufObj
->Usage
== GL_STATIC_COPY
) &&
3108 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
3109 BUFFER_USAGE_WARNING(ctx
,
3110 "using %s(buffer %u, offset %u, length %u) to "
3111 "update a %s buffer",
3112 func
, bufObj
->Name
, offset
, length
,
3113 _mesa_enum_to_string(bufObj
->Usage
));
3121 map_buffer_range(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
3122 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
3125 if (!bufObj
->Size
) {
3126 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
3130 assert(ctx
->Driver
.MapBufferRange
);
3131 void *map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
3134 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
3137 /* The driver callback should have set all these fields.
3138 * This is important because other modules (like VBO) might call
3139 * the driver function directly.
3141 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
3142 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
3143 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
3144 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
3147 if (access
& GL_MAP_WRITE_BIT
) {
3148 bufObj
->Written
= GL_TRUE
;
3149 bufObj
->MinMaxCacheDirty
= true;
3153 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3154 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
3155 bufObj
->Name
, bufObj
->Size
, access
);
3156 /* Access must be write only */
3157 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
3159 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
3160 for (i
= 0; i
< bufObj
->Size
; i
++)
3167 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3168 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
3170 /* buffer is 100 bytes larger than requested, fill with magic value */
3171 for (i
= 0; i
< 100; i
++) {
3172 buf
[bufObj
->Size
- i
- 1] = 123;
3181 _mesa_MapBufferRange_no_error(GLenum target
, GLintptr offset
,
3182 GLsizeiptr length
, GLbitfield access
)
3184 GET_CURRENT_CONTEXT(ctx
);
3186 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3187 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3189 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3190 "glMapBufferRange");
3194 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
3197 GET_CURRENT_CONTEXT(ctx
);
3198 struct gl_buffer_object
*bufObj
;
3200 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3201 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3202 "glMapBufferRange(ARB_map_buffer_range not supported)");
3206 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
3210 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3211 "glMapBufferRange"))
3214 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3215 "glMapBufferRange");
3219 _mesa_MapNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3220 GLsizeiptr length
, GLbitfield access
)
3222 GET_CURRENT_CONTEXT(ctx
);
3223 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3225 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3226 "glMapNamedBufferRange");
3230 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3233 GET_CURRENT_CONTEXT(ctx
);
3234 struct gl_buffer_object
*bufObj
;
3236 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3237 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3238 "glMapNamedBufferRange("
3239 "ARB_map_buffer_range not supported)");
3243 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
3247 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3248 "glMapNamedBufferRange"))
3251 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3252 "glMapNamedBufferRange");
3256 * Converts GLenum access from MapBuffer and MapNamedBuffer into
3257 * flags for input to map_buffer_range.
3259 * \return true if the type of requested access is permissible.
3262 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
3266 case GL_READ_ONLY_ARB
:
3267 *flags
= GL_MAP_READ_BIT
;
3268 return _mesa_is_desktop_gl(ctx
);
3269 case GL_WRITE_ONLY_ARB
:
3270 *flags
= GL_MAP_WRITE_BIT
;
3272 case GL_READ_WRITE_ARB
:
3273 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
3274 return _mesa_is_desktop_gl(ctx
);
3282 _mesa_MapBuffer_no_error(GLenum target
, GLenum access
)
3284 GET_CURRENT_CONTEXT(ctx
);
3286 GLbitfield accessFlags
;
3287 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3289 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3290 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3292 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3297 _mesa_MapBuffer(GLenum target
, GLenum access
)
3299 GET_CURRENT_CONTEXT(ctx
);
3300 struct gl_buffer_object
*bufObj
;
3301 GLbitfield accessFlags
;
3303 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3304 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
3308 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
3312 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3316 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3321 _mesa_MapNamedBuffer_no_error(GLuint buffer
, GLenum access
)
3323 GET_CURRENT_CONTEXT(ctx
);
3325 GLbitfield accessFlags
;
3326 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3328 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3330 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3331 "glMapNamedBuffer");
3335 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
3337 GET_CURRENT_CONTEXT(ctx
);
3338 struct gl_buffer_object
*bufObj
;
3339 GLbitfield accessFlags
;
3341 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3342 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
3346 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
3350 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3351 "glMapNamedBuffer"))
3354 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3355 "glMapNamedBuffer");
3360 flush_mapped_buffer_range(struct gl_context
*ctx
,
3361 struct gl_buffer_object
*bufObj
,
3362 GLintptr offset
, GLsizeiptr length
,
3365 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3366 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3367 "%s(ARB_map_buffer_range not supported)", func
);
3372 _mesa_error(ctx
, GL_INVALID_VALUE
,
3373 "%s(offset %ld < 0)", func
, (long) offset
);
3378 _mesa_error(ctx
, GL_INVALID_VALUE
,
3379 "%s(length %ld < 0)", func
, (long) length
);
3383 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3384 /* buffer is not mapped */
3385 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3386 "%s(buffer is not mapped)", func
);
3390 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
3391 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
3392 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3393 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
3397 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
3398 _mesa_error(ctx
, GL_INVALID_VALUE
,
3399 "%s(offset %ld + length %ld > mapped length %ld)", func
,
3400 (long) offset
, (long) length
,
3401 (long) bufObj
->Mappings
[MAP_USER
].Length
);
3405 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
3407 if (ctx
->Driver
.FlushMappedBufferRange
)
3408 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3413 _mesa_FlushMappedBufferRange_no_error(GLenum target
, GLintptr offset
,
3416 GET_CURRENT_CONTEXT(ctx
);
3417 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3418 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3420 if (ctx
->Driver
.FlushMappedBufferRange
)
3421 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3426 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
3429 GET_CURRENT_CONTEXT(ctx
);
3430 struct gl_buffer_object
*bufObj
;
3432 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
3433 GL_INVALID_OPERATION
);
3437 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3438 "glFlushMappedBufferRange");
3442 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3445 GET_CURRENT_CONTEXT(ctx
);
3446 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3448 if (ctx
->Driver
.FlushMappedBufferRange
)
3449 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3454 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
3457 GET_CURRENT_CONTEXT(ctx
);
3458 struct gl_buffer_object
*bufObj
;
3460 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
3461 "glFlushMappedNamedBufferRange");
3465 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3466 "glFlushMappedNamedBufferRange");
3470 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
, GLuint index
,
3471 struct gl_buffer_object
*bufObj
,
3472 GLintptr offset
, GLsizeiptr size
)
3474 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3479 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3480 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3484 * Bind a region of a buffer object to a uniform block binding point.
3485 * \param index the uniform buffer binding point index
3486 * \param bufObj the buffer object
3487 * \param offset offset to the start of buffer object region
3488 * \param size size of the buffer object region
3491 bind_buffer_range_uniform_buffer_err(struct gl_context
*ctx
, GLuint index
,
3492 struct gl_buffer_object
*bufObj
,
3493 GLintptr offset
, GLsizeiptr size
)
3495 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3496 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3500 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3501 _mesa_error(ctx
, GL_INVALID_VALUE
,
3502 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3503 ctx
->Const
.UniformBufferOffsetAlignment
);
3507 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3511 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
3513 struct gl_buffer_object
*bufObj
,
3517 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3522 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3523 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3527 * Bind a region of a buffer object to a shader storage block binding point.
3528 * \param index the shader storage buffer binding point index
3529 * \param bufObj the buffer object
3530 * \param offset offset to the start of buffer object region
3531 * \param size size of the buffer object region
3534 bind_buffer_range_shader_storage_buffer_err(struct gl_context
*ctx
,
3536 struct gl_buffer_object
*bufObj
,
3537 GLintptr offset
, GLsizeiptr size
)
3539 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3540 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3544 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3545 _mesa_error(ctx
, GL_INVALID_VALUE
,
3546 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3547 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3551 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3555 bind_buffer_range_atomic_buffer(struct gl_context
*ctx
, GLuint index
,
3556 struct gl_buffer_object
*bufObj
,
3557 GLintptr offset
, GLsizeiptr size
)
3559 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3564 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3565 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3569 * Bind a region of a buffer object to an atomic storage block binding point.
3570 * \param index the shader storage buffer binding point index
3571 * \param bufObj the buffer object
3572 * \param offset offset to the start of buffer object region
3573 * \param size size of the buffer object region
3576 bind_buffer_range_atomic_buffer_err(struct gl_context
*ctx
,
3578 struct gl_buffer_object
*bufObj
,
3579 GLintptr offset
, GLsizeiptr size
)
3581 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3582 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3586 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3587 _mesa_error(ctx
, GL_INVALID_VALUE
,
3588 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3589 ATOMIC_COUNTER_SIZE
);
3593 bind_buffer_range_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
3597 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3599 const GLintptr
*offsets
,
3600 const GLsizeiptr
*sizes
)
3602 if (offsets
[index
] < 0) {
3603 /* The ARB_multi_bind spec says:
3605 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3606 * value in <offsets> is less than zero (per binding)."
3608 _mesa_error(ctx
, GL_INVALID_VALUE
,
3609 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3610 index
, (int64_t) offsets
[index
]);
3614 if (sizes
[index
] <= 0) {
3615 /* The ARB_multi_bind spec says:
3617 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3618 * value in <sizes> is less than or equal to zero (per binding)."
3620 _mesa_error(ctx
, GL_INVALID_VALUE
,
3621 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3622 index
, (int64_t) sizes
[index
]);
3630 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3631 GLuint first
, GLsizei count
,
3634 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3635 _mesa_error(ctx
, GL_INVALID_ENUM
,
3636 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3640 /* The ARB_multi_bind_spec says:
3642 * "An INVALID_OPERATION error is generated if <first> + <count> is
3643 * greater than the number of target-specific indexed binding points,
3644 * as described in section 6.7.1."
3646 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3647 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3648 "%s(first=%u + count=%d > the value of "
3649 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3650 caller
, first
, count
,
3651 ctx
->Const
.MaxUniformBufferBindings
);
3659 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3660 GLuint first
, GLsizei count
,
3663 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3664 _mesa_error(ctx
, GL_INVALID_ENUM
,
3665 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3669 /* The ARB_multi_bind_spec says:
3671 * "An INVALID_OPERATION error is generated if <first> + <count> is
3672 * greater than the number of target-specific indexed binding points,
3673 * as described in section 6.7.1."
3675 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3676 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3677 "%s(first=%u + count=%d > the value of "
3678 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3679 caller
, first
, count
,
3680 ctx
->Const
.MaxShaderStorageBufferBindings
);
3688 * Unbind all uniform buffers in the range
3689 * <first> through <first>+<count>-1
3692 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3694 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3696 for (int i
= 0; i
< count
; i
++)
3697 set_buffer_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3698 bufObj
, -1, -1, GL_TRUE
, 0);
3702 * Unbind all shader storage buffers in the range
3703 * <first> through <first>+<count>-1
3706 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3709 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3711 for (int i
= 0; i
< count
; i
++)
3712 set_buffer_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3713 bufObj
, -1, -1, GL_TRUE
, 0);
3717 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3718 const GLuint
*buffers
,
3720 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3723 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3726 /* Assume that at least one binding will be changed */
3727 FLUSH_VERTICES(ctx
, 0);
3728 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3731 /* The ARB_multi_bind spec says:
3733 * "If <buffers> is NULL, all bindings from <first> through
3734 * <first>+<count>-1 are reset to their unbound (zero) state.
3735 * In this case, the offsets and sizes associated with the
3736 * binding points are set to default values, ignoring
3737 * <offsets> and <sizes>."
3739 unbind_uniform_buffers(ctx
, first
, count
);
3743 /* Note that the error semantics for multi-bind commands differ from
3744 * those of other GL commands.
3746 * The Issues section in the ARB_multi_bind spec says:
3748 * "(11) Typically, OpenGL specifies that if an error is generated by a
3749 * command, that command has no effect. This is somewhat
3750 * unfortunate for multi-bind commands, because it would require a
3751 * first pass to scan the entire list of bound objects for errors
3752 * and then a second pass to actually perform the bindings.
3753 * Should we have different error semantics?
3755 * RESOLVED: Yes. In this specification, when the parameters for
3756 * one of the <count> binding points are invalid, that binding point
3757 * is not updated and an error will be generated. However, other
3758 * binding points in the same command will be updated if their
3759 * parameters are valid and no other error occurs."
3762 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3764 for (int i
= 0; i
< count
; i
++) {
3765 struct gl_buffer_binding
*binding
=
3766 &ctx
->UniformBufferBindings
[first
+ i
];
3767 GLintptr offset
= 0;
3768 GLsizeiptr size
= 0;
3771 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3774 /* The ARB_multi_bind spec says:
3776 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3777 * pair of values in <offsets> and <sizes> does not respectively
3778 * satisfy the constraints described for those parameters for the
3779 * specified target, as described in section 6.7.1 (per binding)."
3781 * Section 6.7.1 refers to table 6.5, which says:
3783 * "┌───────────────────────────────────────────────────────────────┐
3784 * │ Uniform buffer array bindings (see sec. 7.6) │
3785 * ├─────────────────────┬─────────────────────────────────────────┤
3787 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3788 * │ │ OFFSET_ALIGNMENT │
3790 * │ size restriction │ none │
3791 * └─────────────────────┴─────────────────────────────────────────┘"
3793 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3794 _mesa_error(ctx
, GL_INVALID_VALUE
,
3795 "glBindBuffersRange(offsets[%u]=%" PRId64
3796 " is misaligned; it must be a multiple of the value of "
3797 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3798 "target=GL_UNIFORM_BUFFER)",
3799 i
, (int64_t) offsets
[i
],
3800 ctx
->Const
.UniformBufferOffsetAlignment
);
3804 offset
= offsets
[i
];
3808 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
3809 binding
, offset
, size
, range
,
3810 USAGE_UNIFORM_BUFFER
);
3813 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3817 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3818 GLsizei count
, const GLuint
*buffers
,
3820 const GLintptr
*offsets
,
3821 const GLsizeiptr
*sizes
,
3824 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3827 /* Assume that at least one binding will be changed */
3828 FLUSH_VERTICES(ctx
, 0);
3829 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3832 /* The ARB_multi_bind spec says:
3834 * "If <buffers> is NULL, all bindings from <first> through
3835 * <first>+<count>-1 are reset to their unbound (zero) state.
3836 * In this case, the offsets and sizes associated with the
3837 * binding points are set to default values, ignoring
3838 * <offsets> and <sizes>."
3840 unbind_shader_storage_buffers(ctx
, first
, count
);
3844 /* Note that the error semantics for multi-bind commands differ from
3845 * those of other GL commands.
3847 * The Issues section in the ARB_multi_bind spec says:
3849 * "(11) Typically, OpenGL specifies that if an error is generated by a
3850 * command, that command has no effect. This is somewhat
3851 * unfortunate for multi-bind commands, because it would require a
3852 * first pass to scan the entire list of bound objects for errors
3853 * and then a second pass to actually perform the bindings.
3854 * Should we have different error semantics?
3856 * RESOLVED: Yes. In this specification, when the parameters for
3857 * one of the <count> binding points are invalid, that binding point
3858 * is not updated and an error will be generated. However, other
3859 * binding points in the same command will be updated if their
3860 * parameters are valid and no other error occurs."
3863 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3865 for (int i
= 0; i
< count
; i
++) {
3866 struct gl_buffer_binding
*binding
=
3867 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3868 GLintptr offset
= 0;
3869 GLsizeiptr size
= 0;
3872 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3875 /* The ARB_multi_bind spec says:
3877 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3878 * pair of values in <offsets> and <sizes> does not respectively
3879 * satisfy the constraints described for those parameters for the
3880 * specified target, as described in section 6.7.1 (per binding)."
3882 * Section 6.7.1 refers to table 6.5, which says:
3884 * "┌───────────────────────────────────────────────────────────────┐
3885 * │ Shader storage buffer array bindings (see sec. 7.8) │
3886 * ├─────────────────────┬─────────────────────────────────────────┤
3888 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3889 * │ │ BUFFER_OFFSET_ALIGNMENT │
3891 * │ size restriction │ none │
3892 * └─────────────────────┴─────────────────────────────────────────┘"
3894 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3895 _mesa_error(ctx
, GL_INVALID_VALUE
,
3896 "glBindBuffersRange(offsets[%u]=%" PRId64
3897 " is misaligned; it must be a multiple of the value of "
3898 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3899 "target=GL_SHADER_STORAGE_BUFFER)",
3900 i
, (int64_t) offsets
[i
],
3901 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3905 offset
= offsets
[i
];
3909 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
3910 binding
, offset
, size
, range
,
3911 USAGE_SHADER_STORAGE_BUFFER
);
3914 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3918 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3919 struct gl_transform_feedback_object
*tfObj
,
3920 GLuint first
, GLsizei count
, const char *caller
)
3922 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3923 _mesa_error(ctx
, GL_INVALID_ENUM
,
3924 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3928 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3930 * "An INVALID_OPERATION error is generated :
3933 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3934 * FEEDBACK_BUFFER and transform feedback is currently active."
3936 * We assume that this is also meant to apply to BindBuffersRange
3937 * and BindBuffersBase.
3939 if (tfObj
->Active
) {
3940 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3941 "%s(Changing transform feedback buffers while "
3942 "transform feedback is active)", caller
);
3946 /* The ARB_multi_bind_spec says:
3948 * "An INVALID_OPERATION error is generated if <first> + <count> is
3949 * greater than the number of target-specific indexed binding points,
3950 * as described in section 6.7.1."
3952 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3953 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3954 "%s(first=%u + count=%d > the value of "
3955 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3956 caller
, first
, count
,
3957 ctx
->Const
.MaxTransformFeedbackBuffers
);
3965 * Unbind all transform feedback buffers in the range
3966 * <first> through <first>+<count>-1
3969 unbind_xfb_buffers(struct gl_context
*ctx
,
3970 struct gl_transform_feedback_object
*tfObj
,
3971 GLuint first
, GLsizei count
)
3973 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3975 for (int i
= 0; i
< count
; i
++)
3976 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3981 bind_xfb_buffers(struct gl_context
*ctx
,
3982 GLuint first
, GLsizei count
,
3983 const GLuint
*buffers
,
3985 const GLintptr
*offsets
,
3986 const GLsizeiptr
*sizes
,
3989 struct gl_transform_feedback_object
*tfObj
=
3990 ctx
->TransformFeedback
.CurrentObject
;
3992 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
3995 /* Assume that at least one binding will be changed */
3996 FLUSH_VERTICES(ctx
, 0);
3997 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
4000 /* The ARB_multi_bind spec says:
4002 * "If <buffers> is NULL, all bindings from <first> through
4003 * <first>+<count>-1 are reset to their unbound (zero) state.
4004 * In this case, the offsets and sizes associated with the
4005 * binding points are set to default values, ignoring
4006 * <offsets> and <sizes>."
4008 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
4012 /* Note that the error semantics for multi-bind commands differ from
4013 * those of other GL commands.
4015 * The Issues section in the ARB_multi_bind spec says:
4017 * "(11) Typically, OpenGL specifies that if an error is generated by a
4018 * command, that command has no effect. This is somewhat
4019 * unfortunate for multi-bind commands, because it would require a
4020 * first pass to scan the entire list of bound objects for errors
4021 * and then a second pass to actually perform the bindings.
4022 * Should we have different error semantics?
4024 * RESOLVED: Yes. In this specification, when the parameters for
4025 * one of the <count> binding points are invalid, that binding point
4026 * is not updated and an error will be generated. However, other
4027 * binding points in the same command will be updated if their
4028 * parameters are valid and no other error occurs."
4031 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4033 for (int i
= 0; i
< count
; i
++) {
4034 const GLuint index
= first
+ i
;
4035 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
4036 struct gl_buffer_object
*bufObj
;
4037 GLintptr offset
= 0;
4038 GLsizeiptr size
= 0;
4041 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4044 /* The ARB_multi_bind spec says:
4046 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4047 * pair of values in <offsets> and <sizes> does not respectively
4048 * satisfy the constraints described for those parameters for the
4049 * specified target, as described in section 6.7.1 (per binding)."
4051 * Section 6.7.1 refers to table 6.5, which says:
4053 * "┌───────────────────────────────────────────────────────────────┐
4054 * │ Transform feedback array bindings (see sec. 13.2.2) │
4055 * ├───────────────────────┬───────────────────────────────────────┤
4057 * │ offset restriction │ multiple of 4 │
4059 * │ size restriction │ multiple of 4 │
4060 * └───────────────────────┴───────────────────────────────────────┘"
4062 if (offsets
[i
] & 0x3) {
4063 _mesa_error(ctx
, GL_INVALID_VALUE
,
4064 "glBindBuffersRange(offsets[%u]=%" PRId64
4065 " is misaligned; it must be a multiple of 4 when "
4066 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4067 i
, (int64_t) offsets
[i
]);
4071 if (sizes
[i
] & 0x3) {
4072 _mesa_error(ctx
, GL_INVALID_VALUE
,
4073 "glBindBuffersRange(sizes[%u]=%" PRId64
4074 " is misaligned; it must be a multiple of 4 when "
4075 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4076 i
, (int64_t) sizes
[i
]);
4080 offset
= offsets
[i
];
4084 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
4085 bufObj
= boundBufObj
;
4087 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
4090 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
4094 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4098 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
4099 GLuint first
, GLsizei count
,
4102 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
4103 _mesa_error(ctx
, GL_INVALID_ENUM
,
4104 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
4108 /* The ARB_multi_bind_spec says:
4110 * "An INVALID_OPERATION error is generated if <first> + <count> is
4111 * greater than the number of target-specific indexed binding points,
4112 * as described in section 6.7.1."
4114 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
4115 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4116 "%s(first=%u + count=%d > the value of "
4117 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
4118 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
4126 * Unbind all atomic counter buffers in the range
4127 * <first> through <first>+<count>-1
4130 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
4132 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
4134 for (int i
= 0; i
< count
; i
++)
4135 set_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
4136 bufObj
, -1, -1, GL_TRUE
, 0);
4140 bind_atomic_buffers(struct gl_context
*ctx
,
4143 const GLuint
*buffers
,
4145 const GLintptr
*offsets
,
4146 const GLsizeiptr
*sizes
,
4149 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
4152 /* Assume that at least one binding will be changed */
4153 FLUSH_VERTICES(ctx
, 0);
4154 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
4157 /* The ARB_multi_bind spec says:
4159 * "If <buffers> is NULL, all bindings from <first> through
4160 * <first>+<count>-1 are reset to their unbound (zero) state.
4161 * In this case, the offsets and sizes associated with the
4162 * binding points are set to default values, ignoring
4163 * <offsets> and <sizes>."
4165 unbind_atomic_buffers(ctx
, first
, count
);
4169 /* Note that the error semantics for multi-bind commands differ from
4170 * those of other GL commands.
4172 * The Issues section in the ARB_multi_bind spec says:
4174 * "(11) Typically, OpenGL specifies that if an error is generated by a
4175 * command, that command has no effect. This is somewhat
4176 * unfortunate for multi-bind commands, because it would require a
4177 * first pass to scan the entire list of bound objects for errors
4178 * and then a second pass to actually perform the bindings.
4179 * Should we have different error semantics?
4181 * RESOLVED: Yes. In this specification, when the parameters for
4182 * one of the <count> binding points are invalid, that binding point
4183 * is not updated and an error will be generated. However, other
4184 * binding points in the same command will be updated if their
4185 * parameters are valid and no other error occurs."
4188 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4190 for (int i
= 0; i
< count
; i
++) {
4191 struct gl_buffer_binding
*binding
=
4192 &ctx
->AtomicBufferBindings
[first
+ i
];
4193 GLintptr offset
= 0;
4194 GLsizeiptr size
= 0;
4197 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4200 /* The ARB_multi_bind spec says:
4202 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4203 * pair of values in <offsets> and <sizes> does not respectively
4204 * satisfy the constraints described for those parameters for the
4205 * specified target, as described in section 6.7.1 (per binding)."
4207 * Section 6.7.1 refers to table 6.5, which says:
4209 * "┌───────────────────────────────────────────────────────────────┐
4210 * │ Atomic counter array bindings (see sec. 7.7.2) │
4211 * ├───────────────────────┬───────────────────────────────────────┤
4213 * │ offset restriction │ multiple of 4 │
4215 * │ size restriction │ none │
4216 * └───────────────────────┴───────────────────────────────────────┘"
4218 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
4219 _mesa_error(ctx
, GL_INVALID_VALUE
,
4220 "glBindBuffersRange(offsets[%u]=%" PRId64
4221 " is misaligned; it must be a multiple of %d when "
4222 "target=GL_ATOMIC_COUNTER_BUFFER)",
4223 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
4227 offset
= offsets
[i
];
4231 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
4232 binding
, offset
, size
, range
,
4233 USAGE_ATOMIC_COUNTER_BUFFER
);
4236 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4239 static ALWAYS_INLINE
void
4240 bind_buffer_range(GLenum target
, GLuint index
, GLuint buffer
, GLintptr offset
,
4241 GLsizeiptr size
, bool no_error
)
4243 GET_CURRENT_CONTEXT(ctx
);
4244 struct gl_buffer_object
*bufObj
;
4246 if (MESA_VERBOSE
& VERBOSE_API
) {
4247 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
4248 _mesa_enum_to_string(target
), index
, buffer
,
4249 (unsigned long) offset
, (unsigned long) size
);
4253 bufObj
= ctx
->Shared
->NullBufferObj
;
4255 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4256 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4257 &bufObj
, "glBindBufferRange"))
4260 if (!no_error
&& !bufObj
) {
4261 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4262 "glBindBufferRange(invalid buffer=%u)", buffer
);
4269 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4270 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4271 index
, bufObj
, offset
, size
);
4273 case GL_UNIFORM_BUFFER
:
4274 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
4276 case GL_SHADER_STORAGE_BUFFER
:
4277 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
,
4280 case GL_ATOMIC_COUNTER_BUFFER
:
4281 bind_buffer_range_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
4284 unreachable("invalid BindBufferRange target with KHR_no_error");
4289 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
4296 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4297 if (!_mesa_validate_buffer_range_xfb(ctx
,
4298 ctx
->TransformFeedback
.CurrentObject
,
4299 index
, bufObj
, offset
, size
,
4303 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4304 index
, bufObj
, offset
, size
);
4306 case GL_UNIFORM_BUFFER
:
4307 bind_buffer_range_uniform_buffer_err(ctx
, index
, bufObj
, offset
,
4310 case GL_SHADER_STORAGE_BUFFER
:
4311 bind_buffer_range_shader_storage_buffer_err(ctx
, index
, bufObj
,
4314 case GL_ATOMIC_COUNTER_BUFFER
:
4315 bind_buffer_range_atomic_buffer_err(ctx
, index
, bufObj
,
4319 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
4326 _mesa_BindBufferRange_no_error(GLenum target
, GLuint index
, GLuint buffer
,
4327 GLintptr offset
, GLsizeiptr size
)
4329 bind_buffer_range(target
, index
, buffer
, offset
, size
, true);
4333 _mesa_BindBufferRange(GLenum target
, GLuint index
,
4334 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
4336 bind_buffer_range(target
, index
, buffer
, offset
, size
, false);
4340 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
4342 GET_CURRENT_CONTEXT(ctx
);
4343 struct gl_buffer_object
*bufObj
;
4345 if (MESA_VERBOSE
& VERBOSE_API
) {
4346 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
4347 _mesa_enum_to_string(target
), index
, buffer
);
4351 bufObj
= ctx
->Shared
->NullBufferObj
;
4353 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4354 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4355 &bufObj
, "glBindBufferBase"))
4359 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4360 "glBindBufferBase(invalid buffer=%u)", buffer
);
4365 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4366 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4368 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4369 * zero and size equal to the size of buffer."
4371 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4373 * "If the parameter (starting offset or size) was not specified when the
4374 * buffer object was bound, zero is returned."
4376 * What happens if the size of the buffer changes? Does the size of the
4377 * buffer at the moment glBindBufferBase was called still play a role, like
4378 * the first quote would imply, or is the size meaningless in the
4379 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4380 * core spec page 45 says:
4382 * "It is equivalent to calling BindBufferRange with offset zero, while
4383 * size is determined by the size of the bound buffer at the time the
4386 * My interpretation is that the GL 4.1 spec was a clarification of the
4387 * behavior, not a change. In particular, this choice will only make
4388 * rendering work in cases where it would have had undefined results.
4392 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4393 _mesa_bind_buffer_base_transform_feedback(ctx
,
4394 ctx
->TransformFeedback
.CurrentObject
,
4395 index
, bufObj
, false);
4397 case GL_UNIFORM_BUFFER
:
4398 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4400 case GL_SHADER_STORAGE_BUFFER
:
4401 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4403 case GL_ATOMIC_COUNTER_BUFFER
:
4404 bind_buffer_base_atomic_buffer(ctx
, index
, bufObj
);
4407 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4413 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4414 const GLuint
*buffers
,
4415 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4417 GET_CURRENT_CONTEXT(ctx
);
4419 if (MESA_VERBOSE
& VERBOSE_API
) {
4420 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4421 _mesa_enum_to_string(target
), first
, count
,
4422 buffers
, offsets
, sizes
);
4426 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4427 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4428 "glBindBuffersRange");
4430 case GL_UNIFORM_BUFFER
:
4431 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4432 "glBindBuffersRange");
4434 case GL_SHADER_STORAGE_BUFFER
:
4435 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4436 "glBindBuffersRange");
4438 case GL_ATOMIC_COUNTER_BUFFER
:
4439 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4440 "glBindBuffersRange");
4443 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4444 _mesa_enum_to_string(target
));
4450 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4451 const GLuint
*buffers
)
4453 GET_CURRENT_CONTEXT(ctx
);
4455 if (MESA_VERBOSE
& VERBOSE_API
) {
4456 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
4457 _mesa_enum_to_string(target
), first
, count
, buffers
);
4461 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4462 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4463 "glBindBuffersBase");
4465 case GL_UNIFORM_BUFFER
:
4466 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4467 "glBindBuffersBase");
4469 case GL_SHADER_STORAGE_BUFFER
:
4470 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4471 "glBindBuffersBase");
4473 case GL_ATOMIC_COUNTER_BUFFER
:
4474 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4475 "glBindBuffersBase");
4478 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4479 _mesa_enum_to_string(target
));
4484 static ALWAYS_INLINE
void
4485 invalidate_buffer_subdata(struct gl_context
*ctx
,
4486 struct gl_buffer_object
*bufObj
, GLintptr offset
,
4489 if (ctx
->Driver
.InvalidateBufferSubData
)
4490 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4494 _mesa_InvalidateBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
4497 GET_CURRENT_CONTEXT(ctx
);
4499 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4500 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4504 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4507 GET_CURRENT_CONTEXT(ctx
);
4508 struct gl_buffer_object
*bufObj
;
4509 const GLintptr end
= offset
+ length
;
4511 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4512 * Profile) spec says:
4514 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4515 * name of an existing buffer object."
4517 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4518 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4519 _mesa_error(ctx
, GL_INVALID_VALUE
,
4520 "glInvalidateBufferSubData(name = %u) invalid object",
4525 /* The GL_ARB_invalidate_subdata spec says:
4527 * "An INVALID_VALUE error is generated if <offset> or <length> is
4528 * negative, or if <offset> + <length> is greater than the value of
4531 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
4532 _mesa_error(ctx
, GL_INVALID_VALUE
,
4533 "glInvalidateBufferSubData(invalid offset or length)");
4537 /* The OpenGL 4.4 (Core Profile) spec says:
4539 * "An INVALID_OPERATION error is generated if buffer is currently
4540 * mapped by MapBuffer or if the invalidate range intersects the range
4541 * currently mapped by MapBufferRange, unless it was mapped
4542 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4544 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4545 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4546 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4547 "glInvalidateBufferSubData(intersection with mapped "
4552 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4556 _mesa_InvalidateBufferData_no_error(GLuint buffer
)
4558 GET_CURRENT_CONTEXT(ctx
);
4560 struct gl_buffer_object
*bufObj
=_mesa_lookup_bufferobj(ctx
, buffer
);
4561 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4565 _mesa_InvalidateBufferData(GLuint buffer
)
4567 GET_CURRENT_CONTEXT(ctx
);
4568 struct gl_buffer_object
*bufObj
;
4570 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4571 * Profile) spec says:
4573 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4574 * name of an existing buffer object."
4576 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4577 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4578 _mesa_error(ctx
, GL_INVALID_VALUE
,
4579 "glInvalidateBufferData(name = %u) invalid object",
4584 /* The OpenGL 4.4 (Core Profile) spec says:
4586 * "An INVALID_OPERATION error is generated if buffer is currently
4587 * mapped by MapBuffer or if the invalidate range intersects the range
4588 * currently mapped by MapBufferRange, unless it was mapped
4589 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4591 if (_mesa_check_disallowed_mapping(bufObj
)) {
4592 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4593 "glInvalidateBufferData(intersection with mapped "
4598 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4602 buffer_page_commitment(struct gl_context
*ctx
,
4603 struct gl_buffer_object
*bufferObj
,
4604 GLintptr offset
, GLsizeiptr size
,
4605 GLboolean commit
, const char *func
)
4607 if (!(bufferObj
->StorageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)) {
4608 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(not a sparse buffer object)",
4613 if (size
< 0 || size
> bufferObj
->Size
||
4614 offset
< 0 || offset
> bufferObj
->Size
- size
) {
4615 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(out of bounds)",
4620 /* The GL_ARB_sparse_buffer extension specification says:
4622 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4623 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4624 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4625 * not extend to the end of the buffer's data store."
4627 if (offset
% ctx
->Const
.SparseBufferPageSize
!= 0) {
4628 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset not aligned to page size)",
4633 if (size
% ctx
->Const
.SparseBufferPageSize
!= 0 &&
4634 offset
+ size
!= bufferObj
->Size
) {
4635 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size not aligned to page size)",
4640 ctx
->Driver
.BufferPageCommitment(ctx
, bufferObj
, offset
, size
, commit
);
4644 _mesa_BufferPageCommitmentARB(GLenum target
, GLintptr offset
, GLsizeiptr size
,
4647 GET_CURRENT_CONTEXT(ctx
);
4648 struct gl_buffer_object
*bufferObj
;
4650 bufferObj
= get_buffer(ctx
, "glBufferPageCommitmentARB", target
,
4655 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4656 "glBufferPageCommitmentARB");
4660 _mesa_NamedBufferPageCommitmentARB(GLuint buffer
, GLintptr offset
,
4661 GLsizeiptr size
, GLboolean commit
)
4663 GET_CURRENT_CONTEXT(ctx
);
4664 struct gl_buffer_object
*bufferObj
;
4666 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4667 if (!bufferObj
|| bufferObj
== &DummyBufferObject
) {
4668 /* Note: the extension spec is not clear about the excpected error value. */
4669 _mesa_error(ctx
, GL_INVALID_VALUE
,
4670 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4675 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4676 "glNamedBufferPageCommitmentARB");