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_vdebugf(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 if (ctx
->Array
.ArrayBufferObj
)
117 ctx
->Array
.ArrayBufferObj
->UsageHistory
|= USAGE_ARRAY_BUFFER
;
118 return &ctx
->Array
.ArrayBufferObj
;
119 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
120 if (ctx
->Array
.VAO
->IndexBufferObj
)
121 ctx
->Array
.VAO
->IndexBufferObj
->UsageHistory
122 |= USAGE_ELEMENT_ARRAY_BUFFER
;
123 return &ctx
->Array
.VAO
->IndexBufferObj
;
124 case GL_PIXEL_PACK_BUFFER_EXT
:
125 return &ctx
->Pack
.BufferObj
;
126 case GL_PIXEL_UNPACK_BUFFER_EXT
:
127 return &ctx
->Unpack
.BufferObj
;
128 case GL_COPY_READ_BUFFER
:
129 return &ctx
->CopyReadBuffer
;
130 case GL_COPY_WRITE_BUFFER
:
131 return &ctx
->CopyWriteBuffer
;
132 case GL_QUERY_BUFFER
:
133 if (_mesa_has_ARB_query_buffer_object(ctx
))
134 return &ctx
->QueryBuffer
;
136 case GL_DRAW_INDIRECT_BUFFER
:
137 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_draw_indirect
) ||
138 _mesa_is_gles31(ctx
)) {
139 return &ctx
->DrawIndirectBuffer
;
142 case GL_PARAMETER_BUFFER_ARB
:
143 if (_mesa_has_ARB_indirect_parameters(ctx
)) {
144 return &ctx
->ParameterBuffer
;
147 case GL_DISPATCH_INDIRECT_BUFFER
:
148 if (_mesa_has_compute_shaders(ctx
)) {
149 return &ctx
->DispatchIndirectBuffer
;
152 case GL_TRANSFORM_FEEDBACK_BUFFER
:
153 if (ctx
->Extensions
.EXT_transform_feedback
) {
154 return &ctx
->TransformFeedback
.CurrentBuffer
;
157 case GL_TEXTURE_BUFFER
:
158 if (_mesa_has_ARB_texture_buffer_object(ctx
) ||
159 _mesa_has_OES_texture_buffer(ctx
)) {
160 return &ctx
->Texture
.BufferObject
;
163 case GL_UNIFORM_BUFFER
:
164 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
165 return &ctx
->UniformBuffer
;
168 case GL_SHADER_STORAGE_BUFFER
:
169 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
170 return &ctx
->ShaderStorageBuffer
;
173 case GL_ATOMIC_COUNTER_BUFFER
:
174 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
175 return &ctx
->AtomicBuffer
;
178 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
179 if (ctx
->Extensions
.AMD_pinned_memory
) {
180 return &ctx
->ExternalVirtualMemoryBuffer
;
191 * Get the buffer object bound to the specified target in a GL context.
192 * \param ctx the GL context
193 * \param target the buffer object target to be retrieved.
194 * \param error the GL error to record if target is illegal.
195 * \return pointer to the buffer object bound to \c target in the
196 * specified context or \c NULL if \c target is invalid.
198 static inline struct gl_buffer_object
*
199 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
202 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
205 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
209 if (!_mesa_is_bufferobj(*bufObj
)) {
210 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
219 * Convert a GLbitfield describing the mapped buffer access flags
220 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
223 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
225 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
226 if ((access
& rwFlags
) == rwFlags
)
227 return GL_READ_WRITE
;
228 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
230 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
231 return GL_WRITE_ONLY
;
233 /* Otherwise, AccessFlags is zero (the default state).
235 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
237 * Name Type Initial Value Legal Values
239 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
242 * However, table 6.8 in the GL_OES_mapbuffer extension says:
244 * Get Value Type Get Command Value Description
245 * --------- ---- ----------- ----- -----------
246 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
248 * The difference is because GL_OES_mapbuffer only supports mapping buffers
253 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
258 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
260 * The regions do not overlap if and only if the end of the given
261 * region is before the mapped region or the start of the given region
262 * is after the mapped region.
264 * \param obj Buffer object target on which to operate.
265 * \param offset Offset of the first byte of the subdata range.
266 * \param size Size, in bytes, of the subdata range.
267 * \return true if ranges overlap, false otherwise
271 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
272 GLintptr offset
, GLsizeiptr size
)
274 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
275 const GLintptr end
= offset
+ size
;
276 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
277 obj
->Mappings
[MAP_USER
].Length
;
279 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
288 * Tests the subdata range parameters and sets the GL error code for
289 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
290 * \c glClearBufferSubData.
292 * \param ctx GL context.
293 * \param bufObj The buffer object.
294 * \param offset Offset of the first byte of the subdata range.
295 * \param size Size, in bytes, of the subdata range.
296 * \param mappedRange If true, checks if an overlapping range is mapped.
297 * If false, checks if buffer is mapped.
298 * \param caller Name of calling function for recording errors.
299 * \return false if error, true otherwise
301 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
304 buffer_object_subdata_range_good(struct gl_context
*ctx
,
305 const struct gl_buffer_object
*bufObj
,
306 GLintptr offset
, GLsizeiptr size
,
307 bool mappedRange
, const char *caller
)
310 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
315 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
319 if (offset
+ size
> bufObj
->Size
) {
320 _mesa_error(ctx
, GL_INVALID_VALUE
,
321 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
322 (unsigned long) offset
,
323 (unsigned long) size
,
324 (unsigned long) bufObj
->Size
);
328 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
332 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
333 _mesa_error(ctx
, GL_INVALID_OPERATION
,
334 "%s(range is mapped without persistent bit)",
340 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
341 _mesa_error(ctx
, GL_INVALID_OPERATION
,
342 "%s(buffer is mapped without persistent bit)",
353 * Test the format and type parameters and set the GL error code for
354 * \c glClearBufferData, \c glClearNamedBufferData, \c glClearBufferSubData
355 * and \c glClearNamedBufferSubData.
357 * \param ctx GL context.
358 * \param internalformat Format to which the data is to be converted.
359 * \param format Format of the supplied data.
360 * \param type Type of the supplied data.
361 * \param caller Name of calling function for recording errors.
362 * \return If internalformat, format and type are legal the mesa_format
363 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
365 * \sa glClearBufferData, glClearNamedBufferData, glClearBufferSubData and
366 * glClearNamedBufferSubData.
369 validate_clear_buffer_format(struct gl_context
*ctx
,
370 GLenum internalformat
,
371 GLenum format
, GLenum type
,
374 mesa_format mesaFormat
;
375 GLenum errorFormatType
;
377 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
378 if (mesaFormat
== MESA_FORMAT_NONE
) {
379 _mesa_error(ctx
, GL_INVALID_ENUM
,
380 "%s(invalid internalformat)", caller
);
381 return MESA_FORMAT_NONE
;
384 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
385 * EXT_texture_integer there is no conversion between integer and
386 * non-integer formats
388 if (_mesa_is_enum_format_signed_int(format
) !=
389 _mesa_is_format_integer_color(mesaFormat
)) {
390 _mesa_error(ctx
, GL_INVALID_OPERATION
,
391 "%s(integer vs non-integer)", caller
);
392 return MESA_FORMAT_NONE
;
395 if (!_mesa_is_color_format(format
)) {
396 _mesa_error(ctx
, GL_INVALID_VALUE
,
397 "%s(format is not a color format)", caller
);
398 return MESA_FORMAT_NONE
;
401 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
402 if (errorFormatType
!= GL_NO_ERROR
) {
403 _mesa_error(ctx
, GL_INVALID_VALUE
,
404 "%s(invalid format or type)", caller
);
405 return MESA_FORMAT_NONE
;
413 * Convert user-specified clear value to the specified internal format.
415 * \param ctx GL context.
416 * \param internalformat Format to which the data is converted.
417 * \param clearValue Points to the converted clear value.
418 * \param format Format of the supplied data.
419 * \param type Type of the supplied data.
420 * \param data Data which is to be converted to internalformat.
421 * \param caller Name of calling function for recording errors.
422 * \return true if data could be converted, false otherwise.
424 * \sa glClearBufferData, glClearBufferSubData
427 convert_clear_buffer_data(struct gl_context
*ctx
,
428 mesa_format internalformat
,
429 GLubyte
*clearValue
, GLenum format
, GLenum type
,
430 const GLvoid
*data
, const char *caller
)
432 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
434 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
435 0, &clearValue
, 1, 1, 1,
436 format
, type
, data
, &ctx
->Unpack
)) {
440 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
447 * Allocate and initialize a new buffer object.
449 * Default callback for the \c dd_function_table::NewBufferObject() hook.
451 static struct gl_buffer_object
*
452 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
454 struct gl_buffer_object
*obj
= MALLOC_STRUCT(gl_buffer_object
);
458 _mesa_initialize_buffer_object(ctx
, obj
, name
);
464 * Delete a buffer object.
466 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
469 _mesa_delete_buffer_object(struct gl_context
*ctx
,
470 struct gl_buffer_object
*bufObj
)
474 vbo_delete_minmax_cache(bufObj
);
475 _mesa_align_free(bufObj
->Data
);
477 /* assign strange values here to help w/ debugging */
478 bufObj
->RefCount
= -1000;
481 simple_mtx_destroy(&bufObj
->MinMaxCacheMutex
);
489 * Set ptr to bufObj w/ reference counting.
490 * This is normally only called from the _mesa_reference_buffer_object() macro
491 * when there's a real pointer change.
494 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
495 struct gl_buffer_object
**ptr
,
496 struct gl_buffer_object
*bufObj
)
499 /* Unreference the old buffer */
500 struct gl_buffer_object
*oldObj
= *ptr
;
502 if (p_atomic_dec_zero(&oldObj
->RefCount
)) {
503 assert(ctx
->Driver
.DeleteBuffer
);
504 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
512 /* reference new buffer */
513 p_atomic_inc(&bufObj
->RefCount
);
520 * Get the value of MESA_NO_MINMAX_CACHE.
523 get_no_minmax_cache()
525 static bool read
= false;
526 static bool disable
= false;
529 disable
= env_var_as_boolean("MESA_NO_MINMAX_CACHE", false);
538 * Initialize a buffer object to default values.
541 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
542 struct gl_buffer_object
*obj
,
545 memset(obj
, 0, sizeof(struct gl_buffer_object
));
548 obj
->Usage
= GL_STATIC_DRAW_ARB
;
550 simple_mtx_init(&obj
->MinMaxCacheMutex
, mtx_plain
);
551 if (get_no_minmax_cache())
552 obj
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
558 * Callback called from _mesa_HashWalk()
561 count_buffer_size(GLuint key
, void *data
, void *userData
)
563 const struct gl_buffer_object
*bufObj
=
564 (const struct gl_buffer_object
*) data
;
565 GLuint
*total
= (GLuint
*) userData
;
568 *total
= *total
+ bufObj
->Size
;
573 * Compute total size (in bytes) of all buffer objects for the given context.
574 * For debugging purposes.
577 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
581 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
588 * Allocate space for and store data in a buffer object. Any data that was
589 * previously stored in the buffer object is lost. If \c data is \c NULL,
590 * memory will be allocated, but no copy will occur.
592 * This is the default callback for \c dd_function_table::BufferData()
593 * Note that all GL error checking will have been done already.
595 * \param ctx GL context.
596 * \param target Buffer object target on which to operate.
597 * \param size Size, in bytes, of the new data store.
598 * \param data Pointer to the data to store in the buffer object. This
599 * pointer may be \c NULL.
600 * \param usage Hints about how the data will be used.
601 * \param bufObj Object to be used.
603 * \return GL_TRUE for success, GL_FALSE for failure
604 * \sa glBufferDataARB, dd_function_table::BufferData.
607 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
608 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
609 struct gl_buffer_object
*bufObj
)
615 _mesa_align_free( bufObj
->Data
);
617 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
619 bufObj
->Data
= (GLubyte
*) new_data
;
621 bufObj
->Usage
= usage
;
622 bufObj
->StorageFlags
= storageFlags
;
625 memcpy( bufObj
->Data
, data
, size
);
637 * Replace data in a subrange of buffer object. If the data range
638 * specified by \c size + \c offset extends beyond the end of the buffer or
639 * if \c data is \c NULL, no copy is performed.
641 * This is the default callback for \c dd_function_table::BufferSubData()
642 * Note that all GL error checking will have been done already.
644 * \param ctx GL context.
645 * \param offset Offset of the first byte to be modified.
646 * \param size Size, in bytes, of the data range.
647 * \param data Pointer to the data to store in the buffer object.
648 * \param bufObj Object to be used.
650 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
653 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptrARB offset
,
654 GLsizeiptrARB size
, const GLvoid
*data
,
655 struct gl_buffer_object
*bufObj
)
659 /* this should have been caught in _mesa_BufferSubData() */
660 assert(size
+ offset
<= bufObj
->Size
);
663 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
669 * Retrieve data from a subrange of buffer object. If the data range
670 * specified by \c size + \c offset extends beyond the end of the buffer or
671 * if \c data is \c NULL, no copy is performed.
673 * This is the default callback for \c dd_function_table::GetBufferSubData()
674 * Note that all GL error checking will have been done already.
676 * \param ctx GL context.
677 * \param target Buffer object target on which to operate.
678 * \param offset Offset of the first byte to be fetched.
679 * \param size Size, in bytes, of the data range.
680 * \param data Destination for data
681 * \param bufObj Object to be used.
683 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
686 buffer_get_subdata(struct gl_context
*ctx
, GLintptrARB offset
,
687 GLsizeiptrARB size
, GLvoid
*data
,
688 struct gl_buffer_object
*bufObj
)
692 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
693 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
699 * Clear a subrange of the buffer object with copies of the supplied data.
700 * If data is NULL the buffer is filled with zeros.
702 * This is the default callback for \c dd_function_table::ClearBufferSubData()
703 * Note that all GL error checking will have been done already.
705 * \param ctx GL context.
706 * \param offset Offset of the first byte to be cleared.
707 * \param size Size, in bytes, of the to be cleared range.
708 * \param clearValue Source of the data.
709 * \param clearValueSize Size, in bytes, of the supplied data.
710 * \param bufObj Object to be cleared.
712 * \sa glClearBufferSubData, glClearBufferData and
713 * dd_function_table::ClearBufferSubData.
716 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
717 GLintptr offset
, GLsizeiptr size
,
718 const GLvoid
*clearValue
,
719 GLsizeiptr clearValueSize
,
720 struct gl_buffer_object
*bufObj
)
725 assert(ctx
->Driver
.MapBufferRange
);
726 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
728 GL_MAP_INVALIDATE_RANGE_BIT
,
729 bufObj
, MAP_INTERNAL
);
732 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
736 if (clearValue
== NULL
) {
737 /* Clear with zeros, per the spec */
738 memset(dest
, 0, size
);
739 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
743 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
744 memcpy(dest
, clearValue
, clearValueSize
);
745 dest
+= clearValueSize
;
748 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
753 * Default fallback for \c dd_function_table::MapBufferRange().
754 * Called via glMapBufferRange().
757 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
758 GLsizeiptr length
, GLbitfield access
,
759 struct gl_buffer_object
*bufObj
,
760 gl_map_buffer_index index
)
763 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
764 /* Just return a direct pointer to the data */
765 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
766 bufObj
->Mappings
[index
].Length
= length
;
767 bufObj
->Mappings
[index
].Offset
= offset
;
768 bufObj
->Mappings
[index
].AccessFlags
= access
;
769 return bufObj
->Mappings
[index
].Pointer
;
774 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
775 * Called via glFlushMappedBufferRange().
778 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
779 GLintptr offset
, GLsizeiptr length
,
780 struct gl_buffer_object
*obj
,
781 gl_map_buffer_index index
)
793 * Default callback for \c dd_function_table::UnmapBuffer().
795 * The input parameters will have been already tested for errors.
797 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
800 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
801 gl_map_buffer_index index
)
804 /* XXX we might assert here that bufObj->Pointer is non-null */
805 bufObj
->Mappings
[index
].Pointer
= NULL
;
806 bufObj
->Mappings
[index
].Length
= 0;
807 bufObj
->Mappings
[index
].Offset
= 0;
808 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
814 * Default fallback for \c dd_function_table::CopyBufferSubData().
815 * Called via glCopyBufferSubData().
818 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
819 struct gl_buffer_object
*src
,
820 struct gl_buffer_object
*dst
,
821 GLintptr readOffset
, GLintptr writeOffset
,
824 GLubyte
*srcPtr
, *dstPtr
;
827 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
829 GL_MAP_WRITE_BIT
, src
,
835 srcPtr
+= readOffset
;
836 dstPtr
+= writeOffset
;
838 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
839 GL_MAP_READ_BIT
, src
,
841 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
843 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
847 /* Note: the src and dst regions will never overlap. Trying to do so
848 * would generate GL_INVALID_VALUE earlier.
850 if (srcPtr
&& dstPtr
)
851 memcpy(dstPtr
, srcPtr
, size
);
853 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
855 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
861 * Initialize the state associated with buffer objects
864 _mesa_init_buffer_objects( struct gl_context
*ctx
)
868 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
869 simple_mtx_init(&DummyBufferObject
.MinMaxCacheMutex
, mtx_plain
);
870 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
872 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
873 ctx
->Shared
->NullBufferObj
);
875 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
876 ctx
->Shared
->NullBufferObj
);
877 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
878 ctx
->Shared
->NullBufferObj
);
880 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
881 ctx
->Shared
->NullBufferObj
);
883 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
,
884 ctx
->Shared
->NullBufferObj
);
886 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
887 ctx
->Shared
->NullBufferObj
);
889 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
890 ctx
->Shared
->NullBufferObj
);
892 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
,
893 ctx
->Shared
->NullBufferObj
);
895 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
,
896 ctx
->Shared
->NullBufferObj
);
898 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
,
899 ctx
->Shared
->NullBufferObj
);
901 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
902 _mesa_reference_buffer_object(ctx
,
903 &ctx
->UniformBufferBindings
[i
].BufferObject
,
904 ctx
->Shared
->NullBufferObj
);
905 ctx
->UniformBufferBindings
[i
].Offset
= -1;
906 ctx
->UniformBufferBindings
[i
].Size
= -1;
909 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
910 _mesa_reference_buffer_object(ctx
,
911 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
912 ctx
->Shared
->NullBufferObj
);
913 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
914 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
917 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
918 _mesa_reference_buffer_object(ctx
,
919 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
920 ctx
->Shared
->NullBufferObj
);
921 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
922 ctx
->AtomicBufferBindings
[i
].Size
= 0;
928 _mesa_free_buffer_objects( struct gl_context
*ctx
)
932 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
934 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
935 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
937 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
939 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
941 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
943 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
945 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
, NULL
);
947 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, NULL
);
949 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
, NULL
);
951 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
952 _mesa_reference_buffer_object(ctx
,
953 &ctx
->UniformBufferBindings
[i
].BufferObject
,
957 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
958 _mesa_reference_buffer_object(ctx
,
959 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
963 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
964 _mesa_reference_buffer_object(ctx
,
965 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
972 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
974 struct gl_buffer_object
**buf_handle
,
977 struct gl_buffer_object
*buf
= *buf_handle
;
979 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
)) {
980 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
984 if (!buf
|| buf
== &DummyBufferObject
) {
985 /* If this is a new buffer object id, or one which was generated but
986 * never used before, allocate a buffer object now.
988 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
990 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
993 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
1001 * Bind the specified target to buffer for the specified context.
1002 * Called by glBindBuffer() and other functions.
1005 bind_buffer_object(struct gl_context
*ctx
,
1006 struct gl_buffer_object
**bindTarget
, GLuint buffer
)
1008 struct gl_buffer_object
*oldBufObj
;
1009 struct gl_buffer_object
*newBufObj
= NULL
;
1013 /* Get pointer to old buffer object (to be unbound) */
1014 oldBufObj
= *bindTarget
;
1015 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
1016 return; /* rebinding the same buffer object- no change */
1019 * Get pointer to new buffer object (newBufObj)
1022 /* The spec says there's not a buffer object named 0, but we use
1023 * one internally because it simplifies things.
1025 newBufObj
= ctx
->Shared
->NullBufferObj
;
1028 /* non-default buffer object */
1029 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1030 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1031 &newBufObj
, "glBindBuffer"))
1035 /* record usage history */
1036 if (bindTarget
== &ctx
->Pack
.BufferObj
) {
1037 newBufObj
->UsageHistory
|= USAGE_PIXEL_PACK_BUFFER
;
1040 /* bind new buffer */
1041 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1046 * Update the default buffer objects in the given context to reference those
1047 * specified in the shared state and release those referencing the old
1051 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1053 /* Bind the NullBufferObj to remove references to those
1054 * in the shared context hash table.
1056 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1057 bind_buffer_object(ctx
, &ctx
->Array
.VAO
->IndexBufferObj
, 0);
1058 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1059 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1065 * Return the gl_buffer_object for the given ID.
1066 * Always return NULL for ID 0.
1068 struct gl_buffer_object
*
1069 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1074 return (struct gl_buffer_object
*)
1075 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1079 struct gl_buffer_object
*
1080 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1085 return (struct gl_buffer_object
*)
1086 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1090 * A convenience function for direct state access functions that throws
1091 * GL_INVALID_OPERATION if buffer is not the name of an existing
1094 struct gl_buffer_object
*
1095 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1098 struct gl_buffer_object
*bufObj
;
1100 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1101 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1102 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1103 "%s(non-existent buffer object %u)", caller
, buffer
);
1112 * Look up a buffer object for a multi-bind function.
1114 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1115 * of generating an error if the buffer ID is not zero or the name
1116 * of an existing buffer object.
1118 * If the buffer ID refers to an existing buffer object, a pointer
1119 * to the buffer object is returned. If the ID is zero, a pointer
1120 * to the shared NullBufferObj is returned. If the ID is not zero
1121 * and does not refer to a valid buffer object, this function
1124 * This function assumes that the caller has already locked the
1125 * hash table mutex by calling
1126 * _mesa_HashLockMutex(ctx->Shared->BufferObjects).
1128 struct gl_buffer_object
*
1129 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1130 const GLuint
*buffers
,
1131 GLuint index
, const char *caller
)
1133 struct gl_buffer_object
*bufObj
;
1135 if (buffers
[index
] != 0) {
1136 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1138 /* The multi-bind functions don't create the buffer objects
1139 when they don't exist. */
1140 if (bufObj
== &DummyBufferObject
)
1143 bufObj
= ctx
->Shared
->NullBufferObj
;
1146 /* The ARB_multi_bind spec says:
1148 * "An INVALID_OPERATION error is generated if any value
1149 * in <buffers> is not zero or the name of an existing
1150 * buffer object (per binding)."
1152 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1153 "%s(buffers[%u]=%u is not zero or the name "
1154 "of an existing buffer object)",
1155 caller
, index
, buffers
[index
]);
1163 * If *ptr points to obj, set ptr = the Null/default buffer object.
1164 * This is a helper for buffer object deletion.
1165 * The GL spec says that deleting a buffer object causes it to get
1166 * unbound from all arrays in the current context.
1169 unbind(struct gl_context
*ctx
,
1170 struct gl_vertex_array_object
*vao
, unsigned index
,
1171 struct gl_buffer_object
*obj
)
1173 if (vao
->BufferBinding
[index
].BufferObj
== obj
) {
1174 _mesa_bind_vertex_buffer(ctx
, vao
, index
, ctx
->Shared
->NullBufferObj
,
1175 vao
->BufferBinding
[index
].Offset
,
1176 vao
->BufferBinding
[index
].Stride
);
1182 * Plug default/fallback buffer object functions into the device
1186 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1188 /* GL_ARB_vertex/pixel_buffer_object */
1189 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1190 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1191 driver
->BufferData
= buffer_data_fallback
;
1192 driver
->BufferSubData
= buffer_sub_data_fallback
;
1193 driver
->GetBufferSubData
= buffer_get_subdata
;
1194 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1196 /* GL_ARB_clear_buffer_object */
1197 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1199 /* GL_ARB_map_buffer_range */
1200 driver
->MapBufferRange
= map_buffer_range_fallback
;
1201 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1203 /* GL_ARB_copy_buffer */
1204 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1209 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1210 struct gl_buffer_object
*bufObj
)
1212 for (int i
= 0; i
< MAP_COUNT
; i
++) {
1213 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1214 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1215 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1216 bufObj
->Mappings
[i
].AccessFlags
= 0;
1222 /**********************************************************************/
1224 /**********************************************************************/
1227 _mesa_BindBuffer_no_error(GLenum target
, GLuint buffer
)
1229 GET_CURRENT_CONTEXT(ctx
);
1231 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1232 bind_buffer_object(ctx
, bindTarget
, buffer
);
1237 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1239 GET_CURRENT_CONTEXT(ctx
);
1241 if (MESA_VERBOSE
& VERBOSE_API
) {
1242 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1243 _mesa_enum_to_string(target
), buffer
);
1246 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1248 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target %s)",
1249 _mesa_enum_to_string(target
));
1253 bind_buffer_object(ctx
, bindTarget
, buffer
);
1257 * Binds a buffer object to a binding point.
1259 * The caller is responsible for validating the offset,
1260 * flushing the vertices and updating NewDriverState.
1263 set_buffer_binding(struct gl_context
*ctx
,
1264 struct gl_buffer_binding
*binding
,
1265 struct gl_buffer_object
*bufObj
,
1268 bool autoSize
, gl_buffer_usage usage
)
1270 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1272 binding
->Offset
= offset
;
1273 binding
->Size
= size
;
1274 binding
->AutomaticSize
= autoSize
;
1276 /* If this is a real buffer object, mark it has having been used
1277 * at some point as an atomic counter buffer.
1280 bufObj
->UsageHistory
|= usage
;
1284 set_buffer_multi_binding(struct gl_context
*ctx
,
1285 const GLuint
*buffers
,
1288 struct gl_buffer_binding
*binding
,
1292 gl_buffer_usage usage
)
1294 struct gl_buffer_object
*bufObj
;
1295 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[idx
])
1296 bufObj
= binding
->BufferObject
;
1298 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, idx
, caller
);
1301 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1302 set_buffer_binding(ctx
, binding
, bufObj
, -1, -1, !range
, usage
);
1304 set_buffer_binding(ctx
, binding
, bufObj
, offset
, size
, !range
, usage
);
1309 bind_buffer(struct gl_context
*ctx
,
1310 struct gl_buffer_binding
*binding
,
1311 struct gl_buffer_object
*bufObj
,
1315 uint64_t driver_state
,
1316 gl_buffer_usage usage
)
1318 if (binding
->BufferObject
== bufObj
&&
1319 binding
->Offset
== offset
&&
1320 binding
->Size
== size
&&
1321 binding
->AutomaticSize
== autoSize
) {
1325 FLUSH_VERTICES(ctx
, 0);
1326 ctx
->NewDriverState
|= driver_state
;
1328 set_buffer_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
, usage
);
1332 * Binds a buffer object to a uniform buffer binding point.
1334 * Unlike set_buffer_binding(), this function also flushes vertices
1335 * and updates NewDriverState. It also checks if the binding
1336 * has actually changed before updating it.
1339 bind_uniform_buffer(struct gl_context
*ctx
,
1341 struct gl_buffer_object
*bufObj
,
1346 bind_buffer(ctx
, &ctx
->UniformBufferBindings
[index
],
1347 bufObj
, offset
, size
, autoSize
,
1348 ctx
->DriverFlags
.NewUniformBuffer
,
1349 USAGE_UNIFORM_BUFFER
);
1353 * Binds a buffer object to a shader storage buffer binding point.
1355 * Unlike set_ssbo_binding(), this function also flushes vertices
1356 * and updates NewDriverState. It also checks if the binding
1357 * has actually changed before updating it.
1360 bind_shader_storage_buffer(struct gl_context
*ctx
,
1362 struct gl_buffer_object
*bufObj
,
1367 bind_buffer(ctx
, &ctx
->ShaderStorageBufferBindings
[index
],
1368 bufObj
, offset
, size
, autoSize
,
1369 ctx
->DriverFlags
.NewShaderStorageBuffer
,
1370 USAGE_SHADER_STORAGE_BUFFER
);
1374 * Binds a buffer object to an atomic buffer binding point.
1376 * Unlike set_atomic_binding(), this function also flushes vertices
1377 * and updates NewDriverState. It also checks if the binding
1378 * has actually changed before updating it.
1381 bind_atomic_buffer(struct gl_context
*ctx
, unsigned index
,
1382 struct gl_buffer_object
*bufObj
, GLintptr offset
,
1383 GLsizeiptr size
, GLboolean autoSize
)
1385 bind_buffer(ctx
, &ctx
->AtomicBufferBindings
[index
],
1386 bufObj
, offset
, size
, autoSize
,
1387 ctx
->DriverFlags
.NewAtomicBuffer
,
1388 USAGE_ATOMIC_COUNTER_BUFFER
);
1392 * Bind a buffer object to a uniform block binding point.
1393 * As above, but offset = 0.
1396 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
1398 struct gl_buffer_object
*bufObj
)
1400 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
1401 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1405 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
1407 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1408 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1410 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1414 * Bind a buffer object to a shader storage block binding point.
1415 * As above, but offset = 0.
1418 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
1420 struct gl_buffer_object
*bufObj
)
1422 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
1423 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1427 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
1429 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1430 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1432 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1436 * Bind a buffer object to a shader storage block binding point.
1437 * As above, but offset = 0.
1440 bind_buffer_base_atomic_buffer(struct gl_context
*ctx
,
1442 struct gl_buffer_object
*bufObj
)
1444 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
1445 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1449 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
1451 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1452 bind_atomic_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1454 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1458 * Delete a set of buffer objects.
1460 * \param n Number of buffer objects to delete.
1461 * \param ids Array of \c n buffer object IDs.
1464 delete_buffers(struct gl_context
*ctx
, GLsizei n
, const GLuint
*ids
)
1466 FLUSH_VERTICES(ctx
, 0);
1468 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1470 for (GLsizei i
= 0; i
< n
; i
++) {
1471 struct gl_buffer_object
*bufObj
=
1472 _mesa_lookup_bufferobj_locked(ctx
, ids
[i
]);
1474 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1477 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1479 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1481 /* unbind any vertex pointers bound to this buffer */
1482 for (j
= 0; j
< ARRAY_SIZE(vao
->BufferBinding
); j
++) {
1483 unbind(ctx
, vao
, j
, bufObj
);
1486 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1487 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1489 if (vao
->IndexBufferObj
== bufObj
) {
1490 bind_buffer_object(ctx
, &vao
->IndexBufferObj
, 0);
1493 /* unbind ARB_draw_indirect binding point */
1494 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1495 bind_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, 0);
1498 /* unbind ARB_indirect_parameters binding point */
1499 if (ctx
->ParameterBuffer
== bufObj
) {
1500 bind_buffer_object(ctx
, &ctx
->ParameterBuffer
, 0);
1503 /* unbind ARB_compute_shader binding point */
1504 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1505 bind_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, 0);
1508 /* unbind ARB_copy_buffer binding points */
1509 if (ctx
->CopyReadBuffer
== bufObj
) {
1510 bind_buffer_object(ctx
, &ctx
->CopyReadBuffer
, 0);
1512 if (ctx
->CopyWriteBuffer
== bufObj
) {
1513 bind_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, 0);
1516 /* unbind transform feedback binding points */
1517 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1518 bind_buffer_object(ctx
, &ctx
->TransformFeedback
.CurrentBuffer
, 0);
1520 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1521 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1522 _mesa_bind_buffer_base_transform_feedback(ctx
,
1523 ctx
->TransformFeedback
.CurrentObject
,
1524 j
, ctx
->Shared
->NullBufferObj
,
1529 /* unbind UBO binding points */
1530 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1531 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1532 bind_buffer_base_uniform_buffer(ctx
, j
,
1533 ctx
->Shared
->NullBufferObj
);
1537 if (ctx
->UniformBuffer
== bufObj
) {
1538 bind_buffer_object(ctx
, &ctx
->UniformBuffer
, 0);
1541 /* unbind SSBO binding points */
1542 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1543 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1544 bind_buffer_base_shader_storage_buffer(ctx
, j
,
1545 ctx
->Shared
->NullBufferObj
);
1549 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1550 bind_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, 0);
1553 /* unbind Atomci Buffer binding points */
1554 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1555 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1556 bind_buffer_base_atomic_buffer(ctx
, j
,
1557 ctx
->Shared
->NullBufferObj
);
1561 if (ctx
->AtomicBuffer
== bufObj
) {
1562 bind_buffer_object(ctx
, &ctx
->AtomicBuffer
, 0);
1565 /* unbind any pixel pack/unpack pointers bound to this buffer */
1566 if (ctx
->Pack
.BufferObj
== bufObj
) {
1567 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1569 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1570 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1573 if (ctx
->Texture
.BufferObject
== bufObj
) {
1574 bind_buffer_object(ctx
, &ctx
->Texture
.BufferObject
, 0);
1577 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1578 bind_buffer_object(ctx
, &ctx
->ExternalVirtualMemoryBuffer
, 0);
1581 /* unbind query buffer binding point */
1582 if (ctx
->QueryBuffer
== bufObj
) {
1583 bind_buffer_object(ctx
, &ctx
->QueryBuffer
, 0);
1586 /* The ID is immediately freed for re-use */
1587 _mesa_HashRemoveLocked(ctx
->Shared
->BufferObjects
, ids
[i
]);
1588 /* Make sure we do not run into the classic ABA problem on bind.
1589 * We don't want to allow re-binding a buffer object that's been
1590 * "deleted" by glDeleteBuffers().
1592 * The explicit rebinding to the default object in the current context
1593 * prevents the above in the current context, but another context
1594 * sharing the same objects might suffer from this problem.
1595 * The alternative would be to do the hash lookup in any case on bind
1596 * which would introduce more runtime overhead than this.
1598 bufObj
->DeletePending
= GL_TRUE
;
1599 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1603 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1608 _mesa_DeleteBuffers_no_error(GLsizei n
, const GLuint
*ids
)
1610 GET_CURRENT_CONTEXT(ctx
);
1611 delete_buffers(ctx
, n
, ids
);
1616 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1618 GET_CURRENT_CONTEXT(ctx
);
1621 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1625 delete_buffers(ctx
, n
, ids
);
1630 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1631 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1635 create_buffers(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1638 struct gl_buffer_object
*buf
;
1644 * This must be atomic (generation and allocation of buffer object IDs)
1646 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1648 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1650 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1651 * DummyBufferObject. Otherwise, create a new buffer object and insert
1654 for (int i
= 0; i
< n
; i
++) {
1655 buffers
[i
] = first
+ i
;
1657 assert(ctx
->Driver
.NewBufferObject
);
1658 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1660 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCreateBuffers");
1661 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1666 buf
= &DummyBufferObject
;
1668 _mesa_HashInsertLocked(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1671 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1676 create_buffers_err(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1678 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1680 if (MESA_VERBOSE
& VERBOSE_API
)
1681 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1684 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1688 create_buffers(ctx
, n
, buffers
, dsa
);
1692 * Generate a set of unique buffer object IDs and store them in \c buffers.
1694 * \param n Number of IDs to generate.
1695 * \param buffers Array of \c n locations to store the IDs.
1698 _mesa_GenBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1700 GET_CURRENT_CONTEXT(ctx
);
1701 create_buffers(ctx
, n
, buffers
, false);
1706 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1708 GET_CURRENT_CONTEXT(ctx
);
1709 create_buffers_err(ctx
, n
, buffers
, false);
1713 * Create a set of buffer objects and store their unique IDs in \c buffers.
1715 * \param n Number of IDs to generate.
1716 * \param buffers Array of \c n locations to store the IDs.
1719 _mesa_CreateBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1721 GET_CURRENT_CONTEXT(ctx
);
1722 create_buffers(ctx
, n
, buffers
, true);
1727 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1729 GET_CURRENT_CONTEXT(ctx
);
1730 create_buffers_err(ctx
, n
, buffers
, true);
1735 * Determine if ID is the name of a buffer object.
1737 * \param id ID of the potential buffer object.
1738 * \return \c GL_TRUE if \c id is the name of a buffer object,
1739 * \c GL_FALSE otherwise.
1741 GLboolean GLAPIENTRY
1742 _mesa_IsBuffer(GLuint id
)
1744 struct gl_buffer_object
*bufObj
;
1745 GET_CURRENT_CONTEXT(ctx
);
1746 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1748 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1750 return bufObj
&& bufObj
!= &DummyBufferObject
;
1755 validate_buffer_storage(struct gl_context
*ctx
,
1756 struct gl_buffer_object
*bufObj
, GLsizeiptr size
,
1757 GLbitfield flags
, const char *func
)
1760 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1764 GLbitfield valid_flags
= GL_MAP_READ_BIT
|
1766 GL_MAP_PERSISTENT_BIT
|
1767 GL_MAP_COHERENT_BIT
|
1768 GL_DYNAMIC_STORAGE_BIT
|
1769 GL_CLIENT_STORAGE_BIT
;
1771 if (ctx
->Extensions
.ARB_sparse_buffer
)
1772 valid_flags
|= GL_SPARSE_STORAGE_BIT_ARB
;
1774 if (flags
& ~valid_flags
) {
1775 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1779 /* The Errors section of the GL_ARB_sparse_buffer spec says:
1781 * "INVALID_VALUE is generated by BufferStorage if <flags> contains
1782 * SPARSE_STORAGE_BIT_ARB and <flags> also contains any combination of
1783 * MAP_READ_BIT or MAP_WRITE_BIT."
1785 if (flags
& GL_SPARSE_STORAGE_BIT_ARB
&&
1786 flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) {
1787 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(SPARSE_STORAGE and READ/WRITE)", func
);
1791 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1792 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1793 _mesa_error(ctx
, GL_INVALID_VALUE
,
1794 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1798 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1799 _mesa_error(ctx
, GL_INVALID_VALUE
,
1800 "%s(COHERENT and flags!=PERSISTENT)", func
);
1804 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
1805 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1814 buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1815 struct gl_memory_object
*memObj
, GLenum target
,
1816 GLsizeiptr size
, const GLvoid
*data
, GLbitfield flags
,
1817 GLuint64 offset
, const char *func
)
1821 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1822 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1824 FLUSH_VERTICES(ctx
, 0);
1826 bufObj
->Written
= GL_TRUE
;
1827 bufObj
->Immutable
= GL_TRUE
;
1828 bufObj
->MinMaxCacheDirty
= true;
1831 assert(ctx
->Driver
.BufferDataMem
);
1832 res
= ctx
->Driver
.BufferDataMem(ctx
, target
, size
, memObj
, offset
,
1833 GL_DYNAMIC_DRAW
, bufObj
);
1836 assert(ctx
->Driver
.BufferData
);
1837 res
= ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1842 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1843 /* Even though the interaction between AMD_pinned_memory and
1844 * glBufferStorage is not described in the spec, Graham Sellers
1845 * said that it should behave the same as glBufferData.
1847 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1850 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1856 static ALWAYS_INLINE
void
1857 inlined_buffer_storage(GLenum target
, GLuint buffer
, GLsizeiptr size
,
1858 const GLvoid
*data
, GLbitfield flags
,
1859 GLuint memory
, GLuint64 offset
,
1860 bool dsa
, bool mem
, bool no_error
, const char *func
)
1862 GET_CURRENT_CONTEXT(ctx
);
1863 struct gl_buffer_object
*bufObj
;
1864 struct gl_memory_object
*memObj
= NULL
;
1868 if (!ctx
->Extensions
.EXT_memory_object
) {
1869 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
1873 /* From the EXT_external_objects spec:
1875 * "An INVALID_VALUE error is generated by BufferStorageMemEXT and
1876 * NamedBufferStorageMemEXT if <memory> is 0, or ..."
1879 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(memory == 0)", func
);
1883 memObj
= _mesa_lookup_memory_object(ctx
, memory
);
1887 /* From the EXT_external_objects spec:
1889 * "An INVALID_OPERATION error is generated if <memory> names a
1890 * valid memory object which has no associated memory."
1892 if (!no_error
&& !memObj
->Immutable
) {
1893 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no associated memory)",
1901 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1903 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
1909 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
1910 bufObj
= *bufObjPtr
;
1912 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
1918 if (no_error
|| validate_buffer_storage(ctx
, bufObj
, size
, flags
, func
))
1919 buffer_storage(ctx
, bufObj
, memObj
, target
, size
, data
, flags
, offset
, func
);
1924 _mesa_BufferStorage_no_error(GLenum target
, GLsizeiptr size
,
1925 const GLvoid
*data
, GLbitfield flags
)
1927 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1928 false, false, true, "glBufferStorage");
1933 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1936 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1937 false, false, false, "glBufferStorage");
1941 _mesa_NamedBufferStorageEXT(GLuint buffer
, GLsizeiptr size
,
1942 const GLvoid
*data
, GLbitfield flags
)
1944 GET_CURRENT_CONTEXT(ctx
);
1946 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1947 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1948 &bufObj
, "glNamedBufferStorageEXT"))
1951 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1952 true, false, false, "glNamedBufferStorageEXT");
1957 _mesa_BufferStorageMemEXT(GLenum target
, GLsizeiptr size
,
1958 GLuint memory
, GLuint64 offset
)
1960 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1961 false, true, false, "glBufferStorageMemEXT");
1966 _mesa_BufferStorageMemEXT_no_error(GLenum target
, GLsizeiptr size
,
1967 GLuint memory
, GLuint64 offset
)
1969 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1970 false, true, true, "glBufferStorageMemEXT");
1975 _mesa_NamedBufferStorage_no_error(GLuint buffer
, GLsizeiptr size
,
1976 const GLvoid
*data
, GLbitfield flags
)
1978 /* In direct state access, buffer objects have an unspecified target
1979 * since they are not required to be bound.
1981 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1982 true, false, true, "glNamedBufferStorage");
1987 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1990 /* In direct state access, buffer objects have an unspecified target
1991 * since they are not required to be bound.
1993 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1994 true, false, false, "glNamedBufferStorage");
1998 _mesa_NamedBufferStorageMemEXT(GLuint buffer
, GLsizeiptr size
,
1999 GLuint memory
, GLuint64 offset
)
2001 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
2002 true, true, false, "glNamedBufferStorageMemEXT");
2007 _mesa_NamedBufferStorageMemEXT_no_error(GLuint buffer
, GLsizeiptr size
,
2008 GLuint memory
, GLuint64 offset
)
2010 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
2011 true, true, true, "glNamedBufferStorageMemEXT");
2015 static ALWAYS_INLINE
void
2016 buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2017 GLenum target
, GLsizeiptr size
, const GLvoid
*data
, GLenum usage
,
2018 const char *func
, bool no_error
)
2022 if (MESA_VERBOSE
& VERBOSE_API
) {
2023 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
2025 _mesa_enum_to_string(target
),
2026 (long int) size
, data
,
2027 _mesa_enum_to_string(usage
));
2032 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
2037 case GL_STREAM_DRAW_ARB
:
2038 valid_usage
= (ctx
->API
!= API_OPENGLES
);
2040 case GL_STATIC_DRAW_ARB
:
2041 case GL_DYNAMIC_DRAW_ARB
:
2044 case GL_STREAM_READ_ARB
:
2045 case GL_STREAM_COPY_ARB
:
2046 case GL_STATIC_READ_ARB
:
2047 case GL_STATIC_COPY_ARB
:
2048 case GL_DYNAMIC_READ_ARB
:
2049 case GL_DYNAMIC_COPY_ARB
:
2050 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
2053 valid_usage
= false;
2058 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
2059 _mesa_enum_to_string(usage
));
2063 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
2064 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
2069 /* Unmap the existing buffer. We'll replace it now. Not an error. */
2070 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
2072 FLUSH_VERTICES(ctx
, 0);
2074 bufObj
->Written
= GL_TRUE
;
2075 bufObj
->MinMaxCacheDirty
= true;
2078 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
2079 bufObj
->Name
, size
, data
, usage
);
2086 assert(ctx
->Driver
.BufferData
);
2087 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
2090 GL_DYNAMIC_STORAGE_BIT
,
2092 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
2094 /* From GL_AMD_pinned_memory:
2096 * INVALID_OPERATION is generated by BufferData if <target> is
2097 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
2098 * mapped to the GPU address space.
2100 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2103 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
2109 buffer_data_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2110 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2111 GLenum usage
, const char *func
)
2113 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, false);
2117 buffer_data_no_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2118 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2119 GLenum usage
, const char *func
)
2121 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, true);
2125 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2126 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2127 GLenum usage
, const char *func
)
2129 buffer_data_error(ctx
, bufObj
, target
, size
, data
, usage
, func
);
2133 _mesa_BufferData_no_error(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2136 GET_CURRENT_CONTEXT(ctx
);
2138 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2139 buffer_data_no_error(ctx
, *bufObj
, target
, size
, data
, usage
,
2144 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
2145 const GLvoid
*data
, GLenum usage
)
2147 GET_CURRENT_CONTEXT(ctx
);
2148 struct gl_buffer_object
*bufObj
;
2150 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
2154 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
2159 _mesa_NamedBufferData_no_error(GLuint buffer
, GLsizeiptr size
,
2160 const GLvoid
*data
, GLenum usage
)
2162 GET_CURRENT_CONTEXT(ctx
);
2164 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2165 buffer_data_no_error(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2166 "glNamedBufferData");
2170 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2173 GET_CURRENT_CONTEXT(ctx
);
2174 struct gl_buffer_object
*bufObj
;
2176 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
2180 /* In direct state access, buffer objects have an unspecified target since
2181 * they are not required to be bound.
2183 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2184 "glNamedBufferData");
2188 _mesa_NamedBufferDataEXT(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2191 GET_CURRENT_CONTEXT(ctx
);
2192 struct gl_buffer_object
*bufObj
;
2195 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2196 "glNamedBufferDataEXT(invalid buffer 0)");
2200 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2201 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2202 &bufObj
, "glNamedBufferDataEXT"))
2205 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2206 "glNamedBufferDataEXT");
2210 validate_buffer_sub_data(struct gl_context
*ctx
,
2211 struct gl_buffer_object
*bufObj
,
2212 GLintptr offset
, GLsizeiptr size
,
2215 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2217 /* error already recorded */
2221 if (bufObj
->Immutable
&&
2222 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
2223 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2227 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2228 bufObj
->Usage
== GL_STATIC_COPY
) &&
2229 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
- 1) {
2230 /* If the application declared the buffer as static draw/copy or stream
2231 * draw, it should not be frequently modified with glBufferSubData.
2233 BUFFER_USAGE_WARNING(ctx
,
2234 "using %s(buffer %u, offset %u, size %u) to "
2235 "update a %s buffer",
2236 func
, bufObj
->Name
, offset
, size
,
2237 _mesa_enum_to_string(bufObj
->Usage
));
2245 * Implementation for glBufferSubData and glNamedBufferSubData.
2247 * \param ctx GL context.
2248 * \param bufObj The buffer object.
2249 * \param offset Offset of the first byte of the subdata range.
2250 * \param size Size, in bytes, of the subdata range.
2251 * \param data The data store.
2252 * \param func Name of calling function for recording errors.
2256 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2257 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
)
2262 bufObj
->NumSubDataCalls
++;
2263 bufObj
->Written
= GL_TRUE
;
2264 bufObj
->MinMaxCacheDirty
= true;
2266 assert(ctx
->Driver
.BufferSubData
);
2267 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
2271 static ALWAYS_INLINE
void
2272 buffer_sub_data(GLenum target
, GLuint buffer
, GLintptr offset
,
2273 GLsizeiptr size
, const GLvoid
*data
,
2274 bool dsa
, bool no_error
, const char *func
)
2276 GET_CURRENT_CONTEXT(ctx
);
2277 struct gl_buffer_object
*bufObj
;
2281 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2283 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
2289 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2290 bufObj
= *bufObjPtr
;
2292 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
2298 if (no_error
|| validate_buffer_sub_data(ctx
, bufObj
, offset
, size
, func
))
2299 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
);
2304 _mesa_BufferSubData_no_error(GLenum target
, GLintptr offset
,
2305 GLsizeiptr size
, const GLvoid
*data
)
2307 buffer_sub_data(target
, 0, offset
, size
, data
, false, true,
2313 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
2314 GLsizeiptr size
, const GLvoid
*data
)
2316 buffer_sub_data(target
, 0, offset
, size
, data
, false, false,
2321 _mesa_NamedBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
2322 GLsizeiptr size
, const GLvoid
*data
)
2324 buffer_sub_data(0, buffer
, offset
, size
, data
, true, true,
2325 "glNamedBufferSubData");
2329 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
2330 GLsizeiptr size
, const GLvoid
*data
)
2332 buffer_sub_data(0, buffer
, offset
, size
, data
, true, false,
2333 "glNamedBufferSubData");
2337 _mesa_NamedBufferSubDataEXT(GLuint buffer
, GLintptr offset
,
2338 GLsizeiptr size
, const GLvoid
*data
)
2340 GET_CURRENT_CONTEXT(ctx
);
2341 struct gl_buffer_object
*bufObj
;
2344 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2345 "glNamedBufferSubDataEXT(invalid buffer 0)");
2349 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2350 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2351 &bufObj
, "glNamedBufferSubDataEXT"))
2354 if (validate_buffer_sub_data(ctx
, bufObj
, offset
, size
,
2355 "glNamedBufferSubDataEXT")) {
2356 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
);
2362 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
2363 GLsizeiptr size
, GLvoid
*data
)
2365 GET_CURRENT_CONTEXT(ctx
);
2366 struct gl_buffer_object
*bufObj
;
2368 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
2369 GL_INVALID_OPERATION
);
2373 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2374 "glGetBufferSubData")) {
2378 assert(ctx
->Driver
.GetBufferSubData
);
2379 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2383 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
2384 GLsizeiptr size
, GLvoid
*data
)
2386 GET_CURRENT_CONTEXT(ctx
);
2387 struct gl_buffer_object
*bufObj
;
2389 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2390 "glGetNamedBufferSubData");
2394 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2395 "glGetNamedBufferSubData")) {
2399 assert(ctx
->Driver
.GetBufferSubData
);
2400 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2405 * \param subdata true if caller is *SubData, false if *Data
2407 static ALWAYS_INLINE
void
2408 clear_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2409 GLenum internalformat
, GLintptr offset
, GLsizeiptr size
,
2410 GLenum format
, GLenum type
, const GLvoid
*data
,
2411 const char *func
, bool subdata
, bool no_error
)
2413 mesa_format mesaFormat
;
2414 GLubyte clearValue
[MAX_PIXEL_BYTES
];
2415 GLsizeiptr clearValueSize
;
2417 /* This checks for disallowed mappings. */
2418 if (!no_error
&& !buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2424 mesaFormat
= _mesa_get_texbuffer_format(ctx
, internalformat
);
2426 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
2427 format
, type
, func
);
2430 if (mesaFormat
== MESA_FORMAT_NONE
)
2433 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
2435 (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0)) {
2436 _mesa_error(ctx
, GL_INVALID_VALUE
,
2437 "%s(offset or size is not a multiple of "
2438 "internalformat size)", func
);
2442 /* Bail early. Negative size has already been checked. */
2446 bufObj
->MinMaxCacheDirty
= true;
2449 /* clear to zeros, per the spec */
2450 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2451 NULL
, clearValueSize
, bufObj
);
2455 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
2456 format
, type
, data
, func
)) {
2460 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2461 clearValue
, clearValueSize
, bufObj
);
2465 clear_buffer_sub_data_error(struct gl_context
*ctx
,
2466 struct gl_buffer_object
*bufObj
,
2467 GLenum internalformat
, GLintptr offset
,
2468 GLsizeiptr size
, GLenum format
, GLenum type
,
2469 const GLvoid
*data
, const char *func
, bool subdata
)
2471 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2472 type
, data
, func
, subdata
, false);
2477 clear_buffer_sub_data_no_error(struct gl_context
*ctx
,
2478 struct gl_buffer_object
*bufObj
,
2479 GLenum internalformat
, GLintptr offset
,
2480 GLsizeiptr size
, GLenum format
, GLenum type
,
2481 const GLvoid
*data
, const char *func
,
2484 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2485 type
, data
, func
, subdata
, true);
2490 _mesa_ClearBufferData_no_error(GLenum target
, GLenum internalformat
,
2491 GLenum format
, GLenum type
, const GLvoid
*data
)
2493 GET_CURRENT_CONTEXT(ctx
);
2495 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2496 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, 0,
2497 (*bufObj
)->Size
, format
, type
, data
,
2498 "glClearBufferData", false);
2503 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
2504 GLenum type
, const GLvoid
*data
)
2506 GET_CURRENT_CONTEXT(ctx
);
2507 struct gl_buffer_object
*bufObj
;
2509 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
2513 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2514 format
, type
, data
, "glClearBufferData", false);
2519 _mesa_ClearNamedBufferData_no_error(GLuint buffer
, GLenum internalformat
,
2520 GLenum format
, GLenum type
,
2523 GET_CURRENT_CONTEXT(ctx
);
2525 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2526 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2527 format
, type
, data
, "glClearNamedBufferData",
2533 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
2534 GLenum format
, GLenum type
, const GLvoid
*data
)
2536 GET_CURRENT_CONTEXT(ctx
);
2537 struct gl_buffer_object
*bufObj
;
2539 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
2543 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2544 format
, type
, data
, "glClearNamedBufferData",
2550 _mesa_ClearBufferSubData_no_error(GLenum target
, GLenum internalformat
,
2551 GLintptr offset
, GLsizeiptr size
,
2552 GLenum format
, GLenum type
,
2555 GET_CURRENT_CONTEXT(ctx
);
2557 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2558 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, offset
, size
,
2559 format
, type
, data
, "glClearBufferSubData",
2565 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
2566 GLintptr offset
, GLsizeiptr size
,
2567 GLenum format
, GLenum type
,
2570 GET_CURRENT_CONTEXT(ctx
);
2571 struct gl_buffer_object
*bufObj
;
2573 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
2577 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2578 format
, type
, data
, "glClearBufferSubData",
2584 _mesa_ClearNamedBufferSubData_no_error(GLuint buffer
, GLenum internalformat
,
2585 GLintptr offset
, GLsizeiptr size
,
2586 GLenum format
, GLenum type
,
2589 GET_CURRENT_CONTEXT(ctx
);
2591 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2592 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, offset
, size
,
2594 "glClearNamedBufferSubData", true);
2599 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
2600 GLintptr offset
, GLsizeiptr size
,
2601 GLenum format
, GLenum type
,
2604 GET_CURRENT_CONTEXT(ctx
);
2605 struct gl_buffer_object
*bufObj
;
2607 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2608 "glClearNamedBufferSubData");
2612 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2613 format
, type
, data
, "glClearNamedBufferSubData",
2618 unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
2620 GLboolean status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2621 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2622 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2623 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2624 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2630 validate_and_unmap_buffer(struct gl_context
*ctx
,
2631 struct gl_buffer_object
*bufObj
,
2634 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2636 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2637 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2638 "%s(buffer is not mapped)", func
);
2643 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2644 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2646 /* check that last 100 bytes are still = magic value */
2647 for (i
= 0; i
< 100; i
++) {
2648 GLuint pos
= bufObj
->Size
- i
- 1;
2649 if (buf
[pos
] != 123) {
2650 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2651 " at position %d (value = %u)\n",
2659 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2660 GLuint i
, unchanged
= 0;
2661 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2663 /* check which bytes changed */
2664 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2665 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2672 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2673 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2678 return unmap_buffer(ctx
, bufObj
);
2681 GLboolean GLAPIENTRY
2682 _mesa_UnmapBuffer_no_error(GLenum target
)
2684 GET_CURRENT_CONTEXT(ctx
);
2685 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2686 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2688 return unmap_buffer(ctx
, bufObj
);
2691 GLboolean GLAPIENTRY
2692 _mesa_UnmapBuffer(GLenum target
)
2694 GET_CURRENT_CONTEXT(ctx
);
2695 struct gl_buffer_object
*bufObj
;
2697 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2701 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2704 GLboolean GLAPIENTRY
2705 _mesa_UnmapNamedBufferEXT_no_error(GLuint buffer
)
2707 GET_CURRENT_CONTEXT(ctx
);
2708 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2710 return unmap_buffer(ctx
, bufObj
);
2713 GLboolean GLAPIENTRY
2714 _mesa_UnmapNamedBufferEXT(GLuint buffer
)
2716 GET_CURRENT_CONTEXT(ctx
);
2717 struct gl_buffer_object
*bufObj
;
2720 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2721 "glUnmapNamedBufferEXT(invalid buffer 0)");
2725 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2729 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2734 get_buffer_parameter(struct gl_context
*ctx
,
2735 struct gl_buffer_object
*bufObj
, GLenum pname
,
2736 GLint64
*params
, const char *func
)
2739 case GL_BUFFER_SIZE_ARB
:
2740 *params
= bufObj
->Size
;
2742 case GL_BUFFER_USAGE_ARB
:
2743 *params
= bufObj
->Usage
;
2745 case GL_BUFFER_ACCESS_ARB
:
2746 *params
= simplified_access_mode(ctx
,
2747 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2749 case GL_BUFFER_MAPPED_ARB
:
2750 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2752 case GL_BUFFER_ACCESS_FLAGS
:
2753 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2755 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2757 case GL_BUFFER_MAP_OFFSET
:
2758 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2760 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2762 case GL_BUFFER_MAP_LENGTH
:
2763 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2765 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2767 case GL_BUFFER_IMMUTABLE_STORAGE
:
2768 if (!ctx
->Extensions
.ARB_buffer_storage
)
2770 *params
= bufObj
->Immutable
;
2772 case GL_BUFFER_STORAGE_FLAGS
:
2773 if (!ctx
->Extensions
.ARB_buffer_storage
)
2775 *params
= bufObj
->StorageFlags
;
2784 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2785 _mesa_enum_to_string(pname
));
2790 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2792 GET_CURRENT_CONTEXT(ctx
);
2793 struct gl_buffer_object
*bufObj
;
2796 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2797 GL_INVALID_OPERATION
);
2801 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2802 "glGetBufferParameteriv"))
2803 return; /* Error already recorded. */
2805 *params
= (GLint
) parameter
;
2809 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2811 GET_CURRENT_CONTEXT(ctx
);
2812 struct gl_buffer_object
*bufObj
;
2815 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2816 GL_INVALID_OPERATION
);
2820 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2821 "glGetBufferParameteri64v"))
2822 return; /* Error already recorded. */
2824 *params
= parameter
;
2828 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2830 GET_CURRENT_CONTEXT(ctx
);
2831 struct gl_buffer_object
*bufObj
;
2834 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2835 "glGetNamedBufferParameteriv");
2839 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2840 "glGetNamedBufferParameteriv"))
2841 return; /* Error already recorded. */
2843 *params
= (GLint
) parameter
;
2847 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2850 GET_CURRENT_CONTEXT(ctx
);
2851 struct gl_buffer_object
*bufObj
;
2854 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2855 "glGetNamedBufferParameteri64v");
2859 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2860 "glGetNamedBufferParameteri64v"))
2861 return; /* Error already recorded. */
2863 *params
= parameter
;
2868 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2870 GET_CURRENT_CONTEXT(ctx
);
2871 struct gl_buffer_object
*bufObj
;
2873 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2874 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2875 "GL_BUFFER_MAP_POINTER)");
2879 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2880 GL_INVALID_OPERATION
);
2884 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2888 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2890 GET_CURRENT_CONTEXT(ctx
);
2891 struct gl_buffer_object
*bufObj
;
2893 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2894 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2895 "GL_BUFFER_MAP_POINTER)");
2899 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2900 "glGetNamedBufferPointerv");
2904 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2909 copy_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*src
,
2910 struct gl_buffer_object
*dst
, GLintptr readOffset
,
2911 GLintptr writeOffset
, GLsizeiptr size
, const char *func
)
2913 if (_mesa_check_disallowed_mapping(src
)) {
2914 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2915 "%s(readBuffer is mapped)", func
);
2919 if (_mesa_check_disallowed_mapping(dst
)) {
2920 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2921 "%s(writeBuffer is mapped)", func
);
2925 if (readOffset
< 0) {
2926 _mesa_error(ctx
, GL_INVALID_VALUE
,
2927 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2931 if (writeOffset
< 0) {
2932 _mesa_error(ctx
, GL_INVALID_VALUE
,
2933 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2938 _mesa_error(ctx
, GL_INVALID_VALUE
,
2939 "%s(size %d < 0)", func
, (int) size
);
2943 if (readOffset
+ size
> src
->Size
) {
2944 _mesa_error(ctx
, GL_INVALID_VALUE
,
2945 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2946 (int) readOffset
, (int) size
, (int) src
->Size
);
2950 if (writeOffset
+ size
> dst
->Size
) {
2951 _mesa_error(ctx
, GL_INVALID_VALUE
,
2952 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2953 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2958 if (readOffset
+ size
<= writeOffset
) {
2961 else if (writeOffset
+ size
<= readOffset
) {
2965 /* overlapping src/dst is illegal */
2966 _mesa_error(ctx
, GL_INVALID_VALUE
,
2967 "%s(overlapping src/dst)", func
);
2972 dst
->MinMaxCacheDirty
= true;
2974 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2978 _mesa_CopyBufferSubData_no_error(GLenum readTarget
, GLenum writeTarget
,
2979 GLintptr readOffset
, GLintptr writeOffset
,
2982 GET_CURRENT_CONTEXT(ctx
);
2984 struct gl_buffer_object
**src_ptr
= get_buffer_target(ctx
, readTarget
);
2985 struct gl_buffer_object
*src
= *src_ptr
;
2987 struct gl_buffer_object
**dst_ptr
= get_buffer_target(ctx
, writeTarget
);
2988 struct gl_buffer_object
*dst
= *dst_ptr
;
2990 dst
->MinMaxCacheDirty
= true;
2991 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
2996 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2997 GLintptr readOffset
, GLintptr writeOffset
,
3000 GET_CURRENT_CONTEXT(ctx
);
3001 struct gl_buffer_object
*src
, *dst
;
3003 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
3004 GL_INVALID_OPERATION
);
3008 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
3009 GL_INVALID_OPERATION
);
3013 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
3014 "glCopyBufferSubData");
3018 _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer
, GLuint writeBuffer
,
3019 GLintptr readOffset
,
3020 GLintptr writeOffset
, GLsizeiptr size
)
3022 GET_CURRENT_CONTEXT(ctx
);
3024 struct gl_buffer_object
*src
= _mesa_lookup_bufferobj(ctx
, readBuffer
);
3025 struct gl_buffer_object
*dst
= _mesa_lookup_bufferobj(ctx
, writeBuffer
);
3027 dst
->MinMaxCacheDirty
= true;
3028 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
3033 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
3034 GLintptr readOffset
, GLintptr writeOffset
,
3037 GET_CURRENT_CONTEXT(ctx
);
3038 struct gl_buffer_object
*src
, *dst
;
3040 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
3041 "glCopyNamedBufferSubData");
3045 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
3046 "glCopyNamedBufferSubData");
3050 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
3051 "glCopyNamedBufferSubData");
3055 validate_map_buffer_range(struct gl_context
*ctx
,
3056 struct gl_buffer_object
*bufObj
, GLintptr offset
,
3057 GLsizeiptr length
, GLbitfield access
,
3060 GLbitfield allowed_access
;
3062 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, false);
3065 _mesa_error(ctx
, GL_INVALID_VALUE
,
3066 "%s(offset %ld < 0)", func
, (long) offset
);
3071 _mesa_error(ctx
, GL_INVALID_VALUE
,
3072 "%s(length %ld < 0)", func
, (long) length
);
3076 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
3078 * "An INVALID_OPERATION error is generated for any of the following
3081 * * <length> is zero."
3083 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
3084 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
3088 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
3092 allowed_access
= GL_MAP_READ_BIT
|
3094 GL_MAP_INVALIDATE_RANGE_BIT
|
3095 GL_MAP_INVALIDATE_BUFFER_BIT
|
3096 GL_MAP_FLUSH_EXPLICIT_BIT
|
3097 GL_MAP_UNSYNCHRONIZED_BIT
;
3099 if (ctx
->Extensions
.ARB_buffer_storage
) {
3100 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
3101 GL_MAP_COHERENT_BIT
;
3104 if (access
& ~allowed_access
) {
3105 /* generate an error if any bits other than those allowed are set */
3106 _mesa_error(ctx
, GL_INVALID_VALUE
,
3107 "%s(access has undefined bits set)", func
);
3111 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
3112 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3113 "%s(access indicates neither read or write)", func
);
3117 if ((access
& GL_MAP_READ_BIT
) &&
3118 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
3119 GL_MAP_INVALIDATE_BUFFER_BIT
|
3120 GL_MAP_UNSYNCHRONIZED_BIT
))) {
3121 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3122 "%s(read access with disallowed bits)", func
);
3126 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
3127 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
3128 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3129 "%s(access has flush explicit without write)", func
);
3133 if (access
& GL_MAP_READ_BIT
&&
3134 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
3135 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3136 "%s(buffer does not allow read access)", func
);
3140 if (access
& GL_MAP_WRITE_BIT
&&
3141 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
3142 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3143 "%s(buffer does not allow write access)", func
);
3147 if (access
& GL_MAP_COHERENT_BIT
&&
3148 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
3149 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3150 "%s(buffer does not allow coherent access)", func
);
3154 if (access
& GL_MAP_PERSISTENT_BIT
&&
3155 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
3156 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3157 "%s(buffer does not allow persistent access)", func
);
3161 if (offset
+ length
> bufObj
->Size
) {
3162 _mesa_error(ctx
, GL_INVALID_VALUE
,
3163 "%s(offset %lu + length %lu > buffer_size %lu)", func
,
3164 (unsigned long) offset
, (unsigned long) length
,
3165 (unsigned long) bufObj
->Size
);
3169 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3170 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3171 "%s(buffer already mapped)", func
);
3175 if (access
& GL_MAP_WRITE_BIT
) {
3176 bufObj
->NumMapBufferWriteCalls
++;
3177 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
3178 bufObj
->Usage
== GL_STATIC_COPY
) &&
3179 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
3180 BUFFER_USAGE_WARNING(ctx
,
3181 "using %s(buffer %u, offset %u, length %u) to "
3182 "update a %s buffer",
3183 func
, bufObj
->Name
, offset
, length
,
3184 _mesa_enum_to_string(bufObj
->Usage
));
3192 map_buffer_range(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
3193 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
3196 if (!bufObj
->Size
) {
3197 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
3201 assert(ctx
->Driver
.MapBufferRange
);
3202 void *map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
3205 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
3208 /* The driver callback should have set all these fields.
3209 * This is important because other modules (like VBO) might call
3210 * the driver function directly.
3212 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
3213 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
3214 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
3215 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
3218 if (access
& GL_MAP_WRITE_BIT
) {
3219 bufObj
->Written
= GL_TRUE
;
3220 bufObj
->MinMaxCacheDirty
= true;
3224 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3225 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
3226 bufObj
->Name
, bufObj
->Size
, access
);
3227 /* Access must be write only */
3228 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
3230 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
3231 for (i
= 0; i
< bufObj
->Size
; i
++)
3238 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3239 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
3241 /* buffer is 100 bytes larger than requested, fill with magic value */
3242 for (i
= 0; i
< 100; i
++) {
3243 buf
[bufObj
->Size
- i
- 1] = 123;
3252 _mesa_MapBufferRange_no_error(GLenum target
, GLintptr offset
,
3253 GLsizeiptr length
, GLbitfield access
)
3255 GET_CURRENT_CONTEXT(ctx
);
3257 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3258 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3260 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3261 "glMapBufferRange");
3265 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
3268 GET_CURRENT_CONTEXT(ctx
);
3269 struct gl_buffer_object
*bufObj
;
3271 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3272 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3273 "glMapBufferRange(ARB_map_buffer_range not supported)");
3277 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
3281 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3282 "glMapBufferRange"))
3285 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3286 "glMapBufferRange");
3290 _mesa_MapNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3291 GLsizeiptr length
, GLbitfield access
)
3293 GET_CURRENT_CONTEXT(ctx
);
3294 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3296 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3297 "glMapNamedBufferRange");
3301 map_named_buffer_range(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3302 GLbitfield access
, bool dsa_ext
, const char *func
)
3304 GET_CURRENT_CONTEXT(ctx
);
3305 struct gl_buffer_object
*bufObj
= NULL
;
3307 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3308 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3309 "%s(ARB_map_buffer_range not supported)", func
);
3314 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3315 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
, &bufObj
, func
))
3318 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
3323 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
, func
))
3326 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
, func
);
3330 _mesa_MapNamedBufferRangeEXT(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3333 GET_CURRENT_CONTEXT(ctx
);
3335 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3336 "glMapNamedBufferRangeEXT(invalid buffer 0)");
3339 return map_named_buffer_range(buffer
, offset
, length
, access
, true,
3340 "glMapNamedBufferRangeEXT");
3344 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3347 return map_named_buffer_range(buffer
, offset
, length
, access
, false,
3348 "glMapNamedBufferRange");
3352 * Converts GLenum access from MapBuffer and MapNamedBuffer into
3353 * flags for input to map_buffer_range.
3355 * \return true if the type of requested access is permissible.
3358 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
3362 case GL_READ_ONLY_ARB
:
3363 *flags
= GL_MAP_READ_BIT
;
3364 return _mesa_is_desktop_gl(ctx
);
3365 case GL_WRITE_ONLY_ARB
:
3366 *flags
= GL_MAP_WRITE_BIT
;
3368 case GL_READ_WRITE_ARB
:
3369 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
3370 return _mesa_is_desktop_gl(ctx
);
3378 _mesa_MapBuffer_no_error(GLenum target
, GLenum access
)
3380 GET_CURRENT_CONTEXT(ctx
);
3382 GLbitfield accessFlags
;
3383 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3385 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3386 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3388 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3393 _mesa_MapBuffer(GLenum target
, GLenum access
)
3395 GET_CURRENT_CONTEXT(ctx
);
3396 struct gl_buffer_object
*bufObj
;
3397 GLbitfield accessFlags
;
3399 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3400 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
3404 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
3408 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3412 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3417 _mesa_MapNamedBuffer_no_error(GLuint buffer
, GLenum access
)
3419 GET_CURRENT_CONTEXT(ctx
);
3421 GLbitfield accessFlags
;
3422 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3424 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3426 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3427 "glMapNamedBuffer");
3431 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
3433 GET_CURRENT_CONTEXT(ctx
);
3434 struct gl_buffer_object
*bufObj
;
3435 GLbitfield accessFlags
;
3437 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3438 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
3442 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
3446 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3447 "glMapNamedBuffer"))
3450 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3451 "glMapNamedBuffer");
3455 _mesa_MapNamedBufferEXT(GLuint buffer
, GLenum access
)
3457 GET_CURRENT_CONTEXT(ctx
);
3459 GLbitfield accessFlags
;
3461 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3462 "glMapNamedBufferEXT(invalid buffer 0)");
3465 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3466 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBufferEXT(invalid access)");
3470 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3471 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3472 &bufObj
, "glMapNamedBufferEXT"))
3475 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3476 "glMapNamedBufferEXT"))
3479 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3480 "glMapNamedBufferEXT");
3484 flush_mapped_buffer_range(struct gl_context
*ctx
,
3485 struct gl_buffer_object
*bufObj
,
3486 GLintptr offset
, GLsizeiptr length
,
3489 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3490 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3491 "%s(ARB_map_buffer_range not supported)", func
);
3496 _mesa_error(ctx
, GL_INVALID_VALUE
,
3497 "%s(offset %ld < 0)", func
, (long) offset
);
3502 _mesa_error(ctx
, GL_INVALID_VALUE
,
3503 "%s(length %ld < 0)", func
, (long) length
);
3507 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3508 /* buffer is not mapped */
3509 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3510 "%s(buffer is not mapped)", func
);
3514 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
3515 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
3516 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3517 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
3521 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
3522 _mesa_error(ctx
, GL_INVALID_VALUE
,
3523 "%s(offset %ld + length %ld > mapped length %ld)", func
,
3524 (long) offset
, (long) length
,
3525 (long) bufObj
->Mappings
[MAP_USER
].Length
);
3529 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
3531 if (ctx
->Driver
.FlushMappedBufferRange
)
3532 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3537 _mesa_FlushMappedBufferRange_no_error(GLenum target
, GLintptr offset
,
3540 GET_CURRENT_CONTEXT(ctx
);
3541 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3542 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3544 if (ctx
->Driver
.FlushMappedBufferRange
)
3545 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3550 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
3553 GET_CURRENT_CONTEXT(ctx
);
3554 struct gl_buffer_object
*bufObj
;
3556 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
3557 GL_INVALID_OPERATION
);
3561 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3562 "glFlushMappedBufferRange");
3566 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3569 GET_CURRENT_CONTEXT(ctx
);
3570 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3572 if (ctx
->Driver
.FlushMappedBufferRange
)
3573 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3578 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
3581 GET_CURRENT_CONTEXT(ctx
);
3582 struct gl_buffer_object
*bufObj
;
3584 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
3585 "glFlushMappedNamedBufferRange");
3589 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3590 "glFlushMappedNamedBufferRange");
3594 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
, GLuint index
,
3595 struct gl_buffer_object
*bufObj
,
3596 GLintptr offset
, GLsizeiptr size
)
3598 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3603 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3604 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3608 * Bind a region of a buffer object to a uniform block binding point.
3609 * \param index the uniform buffer binding point index
3610 * \param bufObj the buffer object
3611 * \param offset offset to the start of buffer object region
3612 * \param size size of the buffer object region
3615 bind_buffer_range_uniform_buffer_err(struct gl_context
*ctx
, GLuint index
,
3616 struct gl_buffer_object
*bufObj
,
3617 GLintptr offset
, GLsizeiptr size
)
3619 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3620 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3624 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3625 _mesa_error(ctx
, GL_INVALID_VALUE
,
3626 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3627 ctx
->Const
.UniformBufferOffsetAlignment
);
3631 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3635 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
3637 struct gl_buffer_object
*bufObj
,
3641 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3646 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3647 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3651 * Bind a region of a buffer object to a shader storage block binding point.
3652 * \param index the shader storage buffer binding point index
3653 * \param bufObj the buffer object
3654 * \param offset offset to the start of buffer object region
3655 * \param size size of the buffer object region
3658 bind_buffer_range_shader_storage_buffer_err(struct gl_context
*ctx
,
3660 struct gl_buffer_object
*bufObj
,
3661 GLintptr offset
, GLsizeiptr size
)
3663 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3664 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3668 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3669 _mesa_error(ctx
, GL_INVALID_VALUE
,
3670 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3671 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3675 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3679 bind_buffer_range_atomic_buffer(struct gl_context
*ctx
, GLuint index
,
3680 struct gl_buffer_object
*bufObj
,
3681 GLintptr offset
, GLsizeiptr size
)
3683 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3688 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3689 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3693 * Bind a region of a buffer object to an atomic storage block binding point.
3694 * \param index the shader storage buffer binding point index
3695 * \param bufObj the buffer object
3696 * \param offset offset to the start of buffer object region
3697 * \param size size of the buffer object region
3700 bind_buffer_range_atomic_buffer_err(struct gl_context
*ctx
,
3702 struct gl_buffer_object
*bufObj
,
3703 GLintptr offset
, GLsizeiptr size
)
3705 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3706 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3710 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3711 _mesa_error(ctx
, GL_INVALID_VALUE
,
3712 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3713 ATOMIC_COUNTER_SIZE
);
3717 bind_buffer_range_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
3721 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3723 const GLintptr
*offsets
,
3724 const GLsizeiptr
*sizes
)
3726 if (offsets
[index
] < 0) {
3727 /* The ARB_multi_bind spec says:
3729 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3730 * value in <offsets> is less than zero (per binding)."
3732 _mesa_error(ctx
, GL_INVALID_VALUE
,
3733 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3734 index
, (int64_t) offsets
[index
]);
3738 if (sizes
[index
] <= 0) {
3739 /* The ARB_multi_bind spec says:
3741 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3742 * value in <sizes> is less than or equal to zero (per binding)."
3744 _mesa_error(ctx
, GL_INVALID_VALUE
,
3745 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3746 index
, (int64_t) sizes
[index
]);
3754 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3755 GLuint first
, GLsizei count
,
3758 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3759 _mesa_error(ctx
, GL_INVALID_ENUM
,
3760 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3764 /* The ARB_multi_bind_spec says:
3766 * "An INVALID_OPERATION error is generated if <first> + <count> is
3767 * greater than the number of target-specific indexed binding points,
3768 * as described in section 6.7.1."
3770 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3771 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3772 "%s(first=%u + count=%d > the value of "
3773 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3774 caller
, first
, count
,
3775 ctx
->Const
.MaxUniformBufferBindings
);
3783 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3784 GLuint first
, GLsizei count
,
3787 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3788 _mesa_error(ctx
, GL_INVALID_ENUM
,
3789 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3793 /* The ARB_multi_bind_spec says:
3795 * "An INVALID_OPERATION error is generated if <first> + <count> is
3796 * greater than the number of target-specific indexed binding points,
3797 * as described in section 6.7.1."
3799 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3800 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3801 "%s(first=%u + count=%d > the value of "
3802 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3803 caller
, first
, count
,
3804 ctx
->Const
.MaxShaderStorageBufferBindings
);
3812 * Unbind all uniform buffers in the range
3813 * <first> through <first>+<count>-1
3816 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3818 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3820 for (int i
= 0; i
< count
; i
++)
3821 set_buffer_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3822 bufObj
, -1, -1, GL_TRUE
, 0);
3826 * Unbind all shader storage buffers in the range
3827 * <first> through <first>+<count>-1
3830 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3833 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3835 for (int i
= 0; i
< count
; i
++)
3836 set_buffer_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3837 bufObj
, -1, -1, GL_TRUE
, 0);
3841 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3842 const GLuint
*buffers
,
3844 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3847 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3850 /* Assume that at least one binding will be changed */
3851 FLUSH_VERTICES(ctx
, 0);
3852 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3855 /* The ARB_multi_bind spec says:
3857 * "If <buffers> is NULL, all bindings from <first> through
3858 * <first>+<count>-1 are reset to their unbound (zero) state.
3859 * In this case, the offsets and sizes associated with the
3860 * binding points are set to default values, ignoring
3861 * <offsets> and <sizes>."
3863 unbind_uniform_buffers(ctx
, first
, count
);
3867 /* Note that the error semantics for multi-bind commands differ from
3868 * those of other GL commands.
3870 * The Issues section in the ARB_multi_bind spec says:
3872 * "(11) Typically, OpenGL specifies that if an error is generated by a
3873 * command, that command has no effect. This is somewhat
3874 * unfortunate for multi-bind commands, because it would require a
3875 * first pass to scan the entire list of bound objects for errors
3876 * and then a second pass to actually perform the bindings.
3877 * Should we have different error semantics?
3879 * RESOLVED: Yes. In this specification, when the parameters for
3880 * one of the <count> binding points are invalid, that binding point
3881 * is not updated and an error will be generated. However, other
3882 * binding points in the same command will be updated if their
3883 * parameters are valid and no other error occurs."
3886 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3888 for (int i
= 0; i
< count
; i
++) {
3889 struct gl_buffer_binding
*binding
=
3890 &ctx
->UniformBufferBindings
[first
+ i
];
3891 GLintptr offset
= 0;
3892 GLsizeiptr size
= 0;
3895 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3898 /* The ARB_multi_bind spec says:
3900 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3901 * pair of values in <offsets> and <sizes> does not respectively
3902 * satisfy the constraints described for those parameters for the
3903 * specified target, as described in section 6.7.1 (per binding)."
3905 * Section 6.7.1 refers to table 6.5, which says:
3907 * "┌───────────────────────────────────────────────────────────────┐
3908 * │ Uniform buffer array bindings (see sec. 7.6) │
3909 * ├─────────────────────┬─────────────────────────────────────────┤
3911 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3912 * │ │ OFFSET_ALIGNMENT │
3914 * │ size restriction │ none │
3915 * └─────────────────────┴─────────────────────────────────────────┘"
3917 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3918 _mesa_error(ctx
, GL_INVALID_VALUE
,
3919 "glBindBuffersRange(offsets[%u]=%" PRId64
3920 " is misaligned; it must be a multiple of the value of "
3921 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3922 "target=GL_UNIFORM_BUFFER)",
3923 i
, (int64_t) offsets
[i
],
3924 ctx
->Const
.UniformBufferOffsetAlignment
);
3928 offset
= offsets
[i
];
3932 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
3933 binding
, offset
, size
, range
,
3934 USAGE_UNIFORM_BUFFER
);
3937 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3941 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3942 GLsizei count
, const GLuint
*buffers
,
3944 const GLintptr
*offsets
,
3945 const GLsizeiptr
*sizes
,
3948 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3951 /* Assume that at least one binding will be changed */
3952 FLUSH_VERTICES(ctx
, 0);
3953 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3956 /* The ARB_multi_bind spec says:
3958 * "If <buffers> is NULL, all bindings from <first> through
3959 * <first>+<count>-1 are reset to their unbound (zero) state.
3960 * In this case, the offsets and sizes associated with the
3961 * binding points are set to default values, ignoring
3962 * <offsets> and <sizes>."
3964 unbind_shader_storage_buffers(ctx
, first
, count
);
3968 /* Note that the error semantics for multi-bind commands differ from
3969 * those of other GL commands.
3971 * The Issues section in the ARB_multi_bind spec says:
3973 * "(11) Typically, OpenGL specifies that if an error is generated by a
3974 * command, that command has no effect. This is somewhat
3975 * unfortunate for multi-bind commands, because it would require a
3976 * first pass to scan the entire list of bound objects for errors
3977 * and then a second pass to actually perform the bindings.
3978 * Should we have different error semantics?
3980 * RESOLVED: Yes. In this specification, when the parameters for
3981 * one of the <count> binding points are invalid, that binding point
3982 * is not updated and an error will be generated. However, other
3983 * binding points in the same command will be updated if their
3984 * parameters are valid and no other error occurs."
3987 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3989 for (int i
= 0; i
< count
; i
++) {
3990 struct gl_buffer_binding
*binding
=
3991 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3992 GLintptr offset
= 0;
3993 GLsizeiptr size
= 0;
3996 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3999 /* The ARB_multi_bind spec says:
4001 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4002 * pair of values in <offsets> and <sizes> does not respectively
4003 * satisfy the constraints described for those parameters for the
4004 * specified target, as described in section 6.7.1 (per binding)."
4006 * Section 6.7.1 refers to table 6.5, which says:
4008 * "┌───────────────────────────────────────────────────────────────┐
4009 * │ Shader storage buffer array bindings (see sec. 7.8) │
4010 * ├─────────────────────┬─────────────────────────────────────────┤
4012 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
4013 * │ │ BUFFER_OFFSET_ALIGNMENT │
4015 * │ size restriction │ none │
4016 * └─────────────────────┴─────────────────────────────────────────┘"
4018 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
4019 _mesa_error(ctx
, GL_INVALID_VALUE
,
4020 "glBindBuffersRange(offsets[%u]=%" PRId64
4021 " is misaligned; it must be a multiple of the value of "
4022 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
4023 "target=GL_SHADER_STORAGE_BUFFER)",
4024 i
, (int64_t) offsets
[i
],
4025 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
4029 offset
= offsets
[i
];
4033 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
4034 binding
, offset
, size
, range
,
4035 USAGE_SHADER_STORAGE_BUFFER
);
4038 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4042 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
4043 struct gl_transform_feedback_object
*tfObj
,
4044 GLuint first
, GLsizei count
, const char *caller
)
4046 if (!ctx
->Extensions
.EXT_transform_feedback
) {
4047 _mesa_error(ctx
, GL_INVALID_ENUM
,
4048 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
4052 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
4054 * "An INVALID_OPERATION error is generated :
4057 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
4058 * FEEDBACK_BUFFER and transform feedback is currently active."
4060 * We assume that this is also meant to apply to BindBuffersRange
4061 * and BindBuffersBase.
4063 if (tfObj
->Active
) {
4064 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4065 "%s(Changing transform feedback buffers while "
4066 "transform feedback is active)", caller
);
4070 /* The ARB_multi_bind_spec says:
4072 * "An INVALID_OPERATION error is generated if <first> + <count> is
4073 * greater than the number of target-specific indexed binding points,
4074 * as described in section 6.7.1."
4076 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
4077 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4078 "%s(first=%u + count=%d > the value of "
4079 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
4080 caller
, first
, count
,
4081 ctx
->Const
.MaxTransformFeedbackBuffers
);
4089 * Unbind all transform feedback buffers in the range
4090 * <first> through <first>+<count>-1
4093 unbind_xfb_buffers(struct gl_context
*ctx
,
4094 struct gl_transform_feedback_object
*tfObj
,
4095 GLuint first
, GLsizei count
)
4097 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
4099 for (int i
= 0; i
< count
; i
++)
4100 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
4105 bind_xfb_buffers(struct gl_context
*ctx
,
4106 GLuint first
, GLsizei count
,
4107 const GLuint
*buffers
,
4109 const GLintptr
*offsets
,
4110 const GLsizeiptr
*sizes
,
4113 struct gl_transform_feedback_object
*tfObj
=
4114 ctx
->TransformFeedback
.CurrentObject
;
4116 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
4119 /* Assume that at least one binding will be changed */
4120 FLUSH_VERTICES(ctx
, 0);
4121 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
4124 /* The ARB_multi_bind spec says:
4126 * "If <buffers> is NULL, all bindings from <first> through
4127 * <first>+<count>-1 are reset to their unbound (zero) state.
4128 * In this case, the offsets and sizes associated with the
4129 * binding points are set to default values, ignoring
4130 * <offsets> and <sizes>."
4132 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
4136 /* Note that the error semantics for multi-bind commands differ from
4137 * those of other GL commands.
4139 * The Issues section in the ARB_multi_bind spec says:
4141 * "(11) Typically, OpenGL specifies that if an error is generated by a
4142 * command, that command has no effect. This is somewhat
4143 * unfortunate for multi-bind commands, because it would require a
4144 * first pass to scan the entire list of bound objects for errors
4145 * and then a second pass to actually perform the bindings.
4146 * Should we have different error semantics?
4148 * RESOLVED: Yes. In this specification, when the parameters for
4149 * one of the <count> binding points are invalid, that binding point
4150 * is not updated and an error will be generated. However, other
4151 * binding points in the same command will be updated if their
4152 * parameters are valid and no other error occurs."
4155 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4157 for (int i
= 0; i
< count
; i
++) {
4158 const GLuint index
= first
+ i
;
4159 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
4160 struct gl_buffer_object
*bufObj
;
4161 GLintptr offset
= 0;
4162 GLsizeiptr size
= 0;
4165 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4168 /* The ARB_multi_bind spec says:
4170 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4171 * pair of values in <offsets> and <sizes> does not respectively
4172 * satisfy the constraints described for those parameters for the
4173 * specified target, as described in section 6.7.1 (per binding)."
4175 * Section 6.7.1 refers to table 6.5, which says:
4177 * "┌───────────────────────────────────────────────────────────────┐
4178 * │ Transform feedback array bindings (see sec. 13.2.2) │
4179 * ├───────────────────────┬───────────────────────────────────────┤
4181 * │ offset restriction │ multiple of 4 │
4183 * │ size restriction │ multiple of 4 │
4184 * └───────────────────────┴───────────────────────────────────────┘"
4186 if (offsets
[i
] & 0x3) {
4187 _mesa_error(ctx
, GL_INVALID_VALUE
,
4188 "glBindBuffersRange(offsets[%u]=%" PRId64
4189 " is misaligned; it must be a multiple of 4 when "
4190 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4191 i
, (int64_t) offsets
[i
]);
4195 if (sizes
[i
] & 0x3) {
4196 _mesa_error(ctx
, GL_INVALID_VALUE
,
4197 "glBindBuffersRange(sizes[%u]=%" PRId64
4198 " is misaligned; it must be a multiple of 4 when "
4199 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4200 i
, (int64_t) sizes
[i
]);
4204 offset
= offsets
[i
];
4208 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
4209 bufObj
= boundBufObj
;
4211 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
4214 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
4218 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4222 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
4223 GLuint first
, GLsizei count
,
4226 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
4227 _mesa_error(ctx
, GL_INVALID_ENUM
,
4228 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
4232 /* The ARB_multi_bind_spec says:
4234 * "An INVALID_OPERATION error is generated if <first> + <count> is
4235 * greater than the number of target-specific indexed binding points,
4236 * as described in section 6.7.1."
4238 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
4239 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4240 "%s(first=%u + count=%d > the value of "
4241 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
4242 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
4250 * Unbind all atomic counter buffers in the range
4251 * <first> through <first>+<count>-1
4254 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
4256 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
4258 for (int i
= 0; i
< count
; i
++)
4259 set_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
4260 bufObj
, -1, -1, GL_TRUE
, 0);
4264 bind_atomic_buffers(struct gl_context
*ctx
,
4267 const GLuint
*buffers
,
4269 const GLintptr
*offsets
,
4270 const GLsizeiptr
*sizes
,
4273 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
4276 /* Assume that at least one binding will be changed */
4277 FLUSH_VERTICES(ctx
, 0);
4278 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
4281 /* The ARB_multi_bind spec says:
4283 * "If <buffers> is NULL, all bindings from <first> through
4284 * <first>+<count>-1 are reset to their unbound (zero) state.
4285 * In this case, the offsets and sizes associated with the
4286 * binding points are set to default values, ignoring
4287 * <offsets> and <sizes>."
4289 unbind_atomic_buffers(ctx
, first
, count
);
4293 /* Note that the error semantics for multi-bind commands differ from
4294 * those of other GL commands.
4296 * The Issues section in the ARB_multi_bind spec says:
4298 * "(11) Typically, OpenGL specifies that if an error is generated by a
4299 * command, that command has no effect. This is somewhat
4300 * unfortunate for multi-bind commands, because it would require a
4301 * first pass to scan the entire list of bound objects for errors
4302 * and then a second pass to actually perform the bindings.
4303 * Should we have different error semantics?
4305 * RESOLVED: Yes. In this specification, when the parameters for
4306 * one of the <count> binding points are invalid, that binding point
4307 * is not updated and an error will be generated. However, other
4308 * binding points in the same command will be updated if their
4309 * parameters are valid and no other error occurs."
4312 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4314 for (int i
= 0; i
< count
; i
++) {
4315 struct gl_buffer_binding
*binding
=
4316 &ctx
->AtomicBufferBindings
[first
+ i
];
4317 GLintptr offset
= 0;
4318 GLsizeiptr size
= 0;
4321 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4324 /* The ARB_multi_bind spec says:
4326 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4327 * pair of values in <offsets> and <sizes> does not respectively
4328 * satisfy the constraints described for those parameters for the
4329 * specified target, as described in section 6.7.1 (per binding)."
4331 * Section 6.7.1 refers to table 6.5, which says:
4333 * "┌───────────────────────────────────────────────────────────────┐
4334 * │ Atomic counter array bindings (see sec. 7.7.2) │
4335 * ├───────────────────────┬───────────────────────────────────────┤
4337 * │ offset restriction │ multiple of 4 │
4339 * │ size restriction │ none │
4340 * └───────────────────────┴───────────────────────────────────────┘"
4342 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
4343 _mesa_error(ctx
, GL_INVALID_VALUE
,
4344 "glBindBuffersRange(offsets[%u]=%" PRId64
4345 " is misaligned; it must be a multiple of %d when "
4346 "target=GL_ATOMIC_COUNTER_BUFFER)",
4347 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
4351 offset
= offsets
[i
];
4355 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
4356 binding
, offset
, size
, range
,
4357 USAGE_ATOMIC_COUNTER_BUFFER
);
4360 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4363 static ALWAYS_INLINE
void
4364 bind_buffer_range(GLenum target
, GLuint index
, GLuint buffer
, GLintptr offset
,
4365 GLsizeiptr size
, bool no_error
)
4367 GET_CURRENT_CONTEXT(ctx
);
4368 struct gl_buffer_object
*bufObj
;
4370 if (MESA_VERBOSE
& VERBOSE_API
) {
4371 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
4372 _mesa_enum_to_string(target
), index
, buffer
,
4373 (unsigned long) offset
, (unsigned long) size
);
4377 bufObj
= ctx
->Shared
->NullBufferObj
;
4379 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4380 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4381 &bufObj
, "glBindBufferRange"))
4384 if (!no_error
&& !bufObj
) {
4385 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4386 "glBindBufferRange(invalid buffer=%u)", buffer
);
4393 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4394 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4395 index
, bufObj
, offset
, size
);
4397 case GL_UNIFORM_BUFFER
:
4398 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
4400 case GL_SHADER_STORAGE_BUFFER
:
4401 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
,
4404 case GL_ATOMIC_COUNTER_BUFFER
:
4405 bind_buffer_range_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
4408 unreachable("invalid BindBufferRange target with KHR_no_error");
4413 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
4420 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4421 if (!_mesa_validate_buffer_range_xfb(ctx
,
4422 ctx
->TransformFeedback
.CurrentObject
,
4423 index
, bufObj
, offset
, size
,
4427 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4428 index
, bufObj
, offset
, size
);
4430 case GL_UNIFORM_BUFFER
:
4431 bind_buffer_range_uniform_buffer_err(ctx
, index
, bufObj
, offset
,
4434 case GL_SHADER_STORAGE_BUFFER
:
4435 bind_buffer_range_shader_storage_buffer_err(ctx
, index
, bufObj
,
4438 case GL_ATOMIC_COUNTER_BUFFER
:
4439 bind_buffer_range_atomic_buffer_err(ctx
, index
, bufObj
,
4443 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
4450 _mesa_BindBufferRange_no_error(GLenum target
, GLuint index
, GLuint buffer
,
4451 GLintptr offset
, GLsizeiptr size
)
4453 bind_buffer_range(target
, index
, buffer
, offset
, size
, true);
4457 _mesa_BindBufferRange(GLenum target
, GLuint index
,
4458 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
4460 bind_buffer_range(target
, index
, buffer
, offset
, size
, false);
4464 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
4466 GET_CURRENT_CONTEXT(ctx
);
4467 struct gl_buffer_object
*bufObj
;
4469 if (MESA_VERBOSE
& VERBOSE_API
) {
4470 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
4471 _mesa_enum_to_string(target
), index
, buffer
);
4475 bufObj
= ctx
->Shared
->NullBufferObj
;
4477 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4478 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4479 &bufObj
, "glBindBufferBase"))
4483 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4484 "glBindBufferBase(invalid buffer=%u)", buffer
);
4489 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4490 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4492 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4493 * zero and size equal to the size of buffer."
4495 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4497 * "If the parameter (starting offset or size) was not specified when the
4498 * buffer object was bound, zero is returned."
4500 * What happens if the size of the buffer changes? Does the size of the
4501 * buffer at the moment glBindBufferBase was called still play a role, like
4502 * the first quote would imply, or is the size meaningless in the
4503 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4504 * core spec page 45 says:
4506 * "It is equivalent to calling BindBufferRange with offset zero, while
4507 * size is determined by the size of the bound buffer at the time the
4510 * My interpretation is that the GL 4.1 spec was a clarification of the
4511 * behavior, not a change. In particular, this choice will only make
4512 * rendering work in cases where it would have had undefined results.
4516 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4517 _mesa_bind_buffer_base_transform_feedback(ctx
,
4518 ctx
->TransformFeedback
.CurrentObject
,
4519 index
, bufObj
, false);
4521 case GL_UNIFORM_BUFFER
:
4522 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4524 case GL_SHADER_STORAGE_BUFFER
:
4525 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4527 case GL_ATOMIC_COUNTER_BUFFER
:
4528 bind_buffer_base_atomic_buffer(ctx
, index
, bufObj
);
4531 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4537 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4538 const GLuint
*buffers
,
4539 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4541 GET_CURRENT_CONTEXT(ctx
);
4543 if (MESA_VERBOSE
& VERBOSE_API
) {
4544 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4545 _mesa_enum_to_string(target
), first
, count
,
4546 buffers
, offsets
, sizes
);
4550 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4551 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4552 "glBindBuffersRange");
4554 case GL_UNIFORM_BUFFER
:
4555 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4556 "glBindBuffersRange");
4558 case GL_SHADER_STORAGE_BUFFER
:
4559 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4560 "glBindBuffersRange");
4562 case GL_ATOMIC_COUNTER_BUFFER
:
4563 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4564 "glBindBuffersRange");
4567 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4568 _mesa_enum_to_string(target
));
4574 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4575 const GLuint
*buffers
)
4577 GET_CURRENT_CONTEXT(ctx
);
4579 if (MESA_VERBOSE
& VERBOSE_API
) {
4580 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
4581 _mesa_enum_to_string(target
), first
, count
, buffers
);
4585 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4586 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4587 "glBindBuffersBase");
4589 case GL_UNIFORM_BUFFER
:
4590 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4591 "glBindBuffersBase");
4593 case GL_SHADER_STORAGE_BUFFER
:
4594 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4595 "glBindBuffersBase");
4597 case GL_ATOMIC_COUNTER_BUFFER
:
4598 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4599 "glBindBuffersBase");
4602 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4603 _mesa_enum_to_string(target
));
4608 static ALWAYS_INLINE
void
4609 invalidate_buffer_subdata(struct gl_context
*ctx
,
4610 struct gl_buffer_object
*bufObj
, GLintptr offset
,
4613 if (ctx
->Driver
.InvalidateBufferSubData
)
4614 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4618 _mesa_InvalidateBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
4621 GET_CURRENT_CONTEXT(ctx
);
4623 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4624 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4628 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4631 GET_CURRENT_CONTEXT(ctx
);
4632 struct gl_buffer_object
*bufObj
;
4633 const GLintptr end
= offset
+ length
;
4635 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4636 * Profile) spec says:
4638 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4639 * name of an existing buffer object."
4641 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4642 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4643 _mesa_error(ctx
, GL_INVALID_VALUE
,
4644 "glInvalidateBufferSubData(name = %u) invalid object",
4649 /* The GL_ARB_invalidate_subdata spec says:
4651 * "An INVALID_VALUE error is generated if <offset> or <length> is
4652 * negative, or if <offset> + <length> is greater than the value of
4655 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
4656 _mesa_error(ctx
, GL_INVALID_VALUE
,
4657 "glInvalidateBufferSubData(invalid offset or length)");
4661 /* The OpenGL 4.4 (Core Profile) spec says:
4663 * "An INVALID_OPERATION error is generated if buffer is currently
4664 * mapped by MapBuffer or if the invalidate range intersects the range
4665 * currently mapped by MapBufferRange, unless it was mapped
4666 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4668 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4669 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4670 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4671 "glInvalidateBufferSubData(intersection with mapped "
4676 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4680 _mesa_InvalidateBufferData_no_error(GLuint buffer
)
4682 GET_CURRENT_CONTEXT(ctx
);
4684 struct gl_buffer_object
*bufObj
=_mesa_lookup_bufferobj(ctx
, buffer
);
4685 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4689 _mesa_InvalidateBufferData(GLuint buffer
)
4691 GET_CURRENT_CONTEXT(ctx
);
4692 struct gl_buffer_object
*bufObj
;
4694 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4695 * Profile) spec says:
4697 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4698 * name of an existing buffer object."
4700 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4701 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4702 _mesa_error(ctx
, GL_INVALID_VALUE
,
4703 "glInvalidateBufferData(name = %u) invalid object",
4708 /* The OpenGL 4.4 (Core Profile) spec says:
4710 * "An INVALID_OPERATION error is generated if buffer is currently
4711 * mapped by MapBuffer or if the invalidate range intersects the range
4712 * currently mapped by MapBufferRange, unless it was mapped
4713 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4715 if (_mesa_check_disallowed_mapping(bufObj
)) {
4716 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4717 "glInvalidateBufferData(intersection with mapped "
4722 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4726 buffer_page_commitment(struct gl_context
*ctx
,
4727 struct gl_buffer_object
*bufferObj
,
4728 GLintptr offset
, GLsizeiptr size
,
4729 GLboolean commit
, const char *func
)
4731 if (!(bufferObj
->StorageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)) {
4732 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(not a sparse buffer object)",
4737 if (size
< 0 || size
> bufferObj
->Size
||
4738 offset
< 0 || offset
> bufferObj
->Size
- size
) {
4739 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(out of bounds)",
4744 /* The GL_ARB_sparse_buffer extension specification says:
4746 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4747 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4748 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4749 * not extend to the end of the buffer's data store."
4751 if (offset
% ctx
->Const
.SparseBufferPageSize
!= 0) {
4752 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset not aligned to page size)",
4757 if (size
% ctx
->Const
.SparseBufferPageSize
!= 0 &&
4758 offset
+ size
!= bufferObj
->Size
) {
4759 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size not aligned to page size)",
4764 ctx
->Driver
.BufferPageCommitment(ctx
, bufferObj
, offset
, size
, commit
);
4768 _mesa_BufferPageCommitmentARB(GLenum target
, GLintptr offset
, GLsizeiptr size
,
4771 GET_CURRENT_CONTEXT(ctx
);
4772 struct gl_buffer_object
*bufferObj
;
4774 bufferObj
= get_buffer(ctx
, "glBufferPageCommitmentARB", target
,
4779 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4780 "glBufferPageCommitmentARB");
4784 _mesa_NamedBufferPageCommitmentARB(GLuint buffer
, GLintptr offset
,
4785 GLsizeiptr size
, GLboolean commit
)
4787 GET_CURRENT_CONTEXT(ctx
);
4788 struct gl_buffer_object
*bufferObj
;
4790 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4791 if (!bufferObj
|| bufferObj
== &DummyBufferObject
) {
4792 /* Note: the extension spec is not clear about the excpected error value. */
4793 _mesa_error(ctx
, GL_INVALID_VALUE
,
4794 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4799 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4800 "glNamedBufferPageCommitmentARB");