2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
29 * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions.
30 * \author Brian Paul, Ian Romanick
34 #include <inttypes.h> /* for PRId64 macro */
35 #include "util/debug.h"
41 #include "bufferobj.h"
42 #include "externalobjects.h"
45 #include "glformats.h"
47 #include "transformfeedback.h"
49 #include "util/u_atomic.h"
54 /*#define BOUNDS_CHECK*/
58 * We count the number of buffer modification calls to check for
59 * inefficient buffer use. This is the number of such calls before we
62 #define BUFFER_WARNING_CALL_COUNT 4
66 * Helper to warn of possible performance issues, such as frequently
67 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
71 buffer_usage_warning(struct gl_context
*ctx
, GLuint
*id
, const char *fmt
, ...)
76 _mesa_gl_vdebug(ctx
, id
,
77 MESA_DEBUG_SOURCE_API
,
78 MESA_DEBUG_TYPE_PERFORMANCE
,
79 MESA_DEBUG_SEVERITY_MEDIUM
,
84 #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
86 static GLuint id = 0; \
87 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
92 * Used as a placeholder for buffer objects between glGenBuffers() and
93 * glBindBuffer() so that glIsBuffer() can work correctly.
95 static struct gl_buffer_object DummyBufferObject
;
99 * Return pointer to address of a buffer object target.
100 * \param ctx the GL context
101 * \param target the buffer object target to be retrieved.
102 * \return pointer to pointer to the buffer object bound to \c target in the
103 * specified context or \c NULL if \c target is invalid.
105 static inline struct gl_buffer_object
**
106 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
108 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
110 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
111 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
115 case GL_ARRAY_BUFFER_ARB
:
116 return &ctx
->Array
.ArrayBufferObj
;
117 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
118 return &ctx
->Array
.VAO
->IndexBufferObj
;
119 case GL_PIXEL_PACK_BUFFER_EXT
:
120 return &ctx
->Pack
.BufferObj
;
121 case GL_PIXEL_UNPACK_BUFFER_EXT
:
122 return &ctx
->Unpack
.BufferObj
;
123 case GL_COPY_READ_BUFFER
:
124 return &ctx
->CopyReadBuffer
;
125 case GL_COPY_WRITE_BUFFER
:
126 return &ctx
->CopyWriteBuffer
;
127 case GL_QUERY_BUFFER
:
128 if (_mesa_has_ARB_query_buffer_object(ctx
))
129 return &ctx
->QueryBuffer
;
131 case GL_DRAW_INDIRECT_BUFFER
:
132 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_draw_indirect
) ||
133 _mesa_is_gles31(ctx
)) {
134 return &ctx
->DrawIndirectBuffer
;
137 case GL_PARAMETER_BUFFER_ARB
:
138 if (_mesa_has_ARB_indirect_parameters(ctx
)) {
139 return &ctx
->ParameterBuffer
;
142 case GL_DISPATCH_INDIRECT_BUFFER
:
143 if (_mesa_has_compute_shaders(ctx
)) {
144 return &ctx
->DispatchIndirectBuffer
;
147 case GL_TRANSFORM_FEEDBACK_BUFFER
:
148 if (ctx
->Extensions
.EXT_transform_feedback
) {
149 return &ctx
->TransformFeedback
.CurrentBuffer
;
152 case GL_TEXTURE_BUFFER
:
153 if (_mesa_has_ARB_texture_buffer_object(ctx
) ||
154 _mesa_has_OES_texture_buffer(ctx
)) {
155 return &ctx
->Texture
.BufferObject
;
158 case GL_UNIFORM_BUFFER
:
159 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
160 return &ctx
->UniformBuffer
;
163 case GL_SHADER_STORAGE_BUFFER
:
164 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
165 return &ctx
->ShaderStorageBuffer
;
168 case GL_ATOMIC_COUNTER_BUFFER
:
169 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
170 return &ctx
->AtomicBuffer
;
173 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
174 if (ctx
->Extensions
.AMD_pinned_memory
) {
175 return &ctx
->ExternalVirtualMemoryBuffer
;
186 * Get the buffer object bound to the specified target in a GL context.
187 * \param ctx the GL context
188 * \param target the buffer object target to be retrieved.
189 * \param error the GL error to record if target is illegal.
190 * \return pointer to the buffer object bound to \c target in the
191 * specified context or \c NULL if \c target is invalid.
193 static inline struct gl_buffer_object
*
194 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
197 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
200 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
204 if (!_mesa_is_bufferobj(*bufObj
)) {
205 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
214 * Convert a GLbitfield describing the mapped buffer access flags
215 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
218 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
220 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
221 if ((access
& rwFlags
) == rwFlags
)
222 return GL_READ_WRITE
;
223 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
225 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
226 return GL_WRITE_ONLY
;
228 /* Otherwise, AccessFlags is zero (the default state).
230 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
232 * Name Type Initial Value Legal Values
234 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
237 * However, table 6.8 in the GL_OES_mapbuffer extension says:
239 * Get Value Type Get Command Value Description
240 * --------- ---- ----------- ----- -----------
241 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
243 * The difference is because GL_OES_mapbuffer only supports mapping buffers
248 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
253 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
255 * The regions do not overlap if and only if the end of the given
256 * region is before the mapped region or the start of the given region
257 * is after the mapped region.
259 * \param obj Buffer object target on which to operate.
260 * \param offset Offset of the first byte of the subdata range.
261 * \param size Size, in bytes, of the subdata range.
262 * \return true if ranges overlap, false otherwise
266 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
267 GLintptr offset
, GLsizeiptr size
)
269 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
270 const GLintptr end
= offset
+ size
;
271 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
272 obj
->Mappings
[MAP_USER
].Length
;
274 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
283 * Tests the subdata range parameters and sets the GL error code for
284 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
285 * \c glClearBufferSubData.
287 * \param ctx GL context.
288 * \param bufObj The buffer object.
289 * \param offset Offset of the first byte of the subdata range.
290 * \param size Size, in bytes, of the subdata range.
291 * \param mappedRange If true, checks if an overlapping range is mapped.
292 * If false, checks if buffer is mapped.
293 * \param caller Name of calling function for recording errors.
294 * \return false if error, true otherwise
296 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
299 buffer_object_subdata_range_good(struct gl_context
*ctx
,
300 const struct gl_buffer_object
*bufObj
,
301 GLintptr offset
, GLsizeiptr size
,
302 bool mappedRange
, const char *caller
)
305 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
310 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
314 if (offset
+ size
> bufObj
->Size
) {
315 _mesa_error(ctx
, GL_INVALID_VALUE
,
316 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
317 (unsigned long) offset
,
318 (unsigned long) size
,
319 (unsigned long) bufObj
->Size
);
323 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
327 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
328 _mesa_error(ctx
, GL_INVALID_OPERATION
,
329 "%s(range is mapped without persistent bit)",
335 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
336 _mesa_error(ctx
, GL_INVALID_OPERATION
,
337 "%s(buffer is mapped without persistent bit)",
348 * Test the format and type parameters and set the GL error code for
349 * \c glClearBufferData and \c glClearBufferSubData.
351 * \param ctx GL context.
352 * \param internalformat Format to which the data is to be converted.
353 * \param format Format of the supplied data.
354 * \param type Type of the supplied data.
355 * \param caller Name of calling function for recording errors.
356 * \return If internalformat, format and type are legal the mesa_format
357 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
359 * \sa glClearBufferData and glClearBufferSubData
362 validate_clear_buffer_format(struct gl_context
*ctx
,
363 GLenum internalformat
,
364 GLenum format
, GLenum type
,
367 mesa_format mesaFormat
;
368 GLenum errorFormatType
;
370 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
371 if (mesaFormat
== MESA_FORMAT_NONE
) {
372 _mesa_error(ctx
, GL_INVALID_ENUM
,
373 "%s(invalid internalformat)", caller
);
374 return MESA_FORMAT_NONE
;
377 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
378 * EXT_texture_integer there is no conversion between integer and
379 * non-integer formats
381 if (_mesa_is_enum_format_signed_int(format
) !=
382 _mesa_is_format_integer_color(mesaFormat
)) {
383 _mesa_error(ctx
, GL_INVALID_OPERATION
,
384 "%s(integer vs non-integer)", caller
);
385 return MESA_FORMAT_NONE
;
388 if (!_mesa_is_color_format(format
)) {
389 _mesa_error(ctx
, GL_INVALID_ENUM
,
390 "%s(format is not a color format)", caller
);
391 return MESA_FORMAT_NONE
;
394 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
395 if (errorFormatType
!= GL_NO_ERROR
) {
396 _mesa_error(ctx
, GL_INVALID_ENUM
,
397 "%s(invalid format or type)", caller
);
398 return MESA_FORMAT_NONE
;
406 * Convert user-specified clear value to the specified internal format.
408 * \param ctx GL context.
409 * \param internalformat Format to which the data is converted.
410 * \param clearValue Points to the converted clear value.
411 * \param format Format of the supplied data.
412 * \param type Type of the supplied data.
413 * \param data Data which is to be converted to internalformat.
414 * \param caller Name of calling function for recording errors.
415 * \return true if data could be converted, false otherwise.
417 * \sa glClearBufferData, glClearBufferSubData
420 convert_clear_buffer_data(struct gl_context
*ctx
,
421 mesa_format internalformat
,
422 GLubyte
*clearValue
, GLenum format
, GLenum type
,
423 const GLvoid
*data
, const char *caller
)
425 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
427 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
428 0, &clearValue
, 1, 1, 1,
429 format
, type
, data
, &ctx
->Unpack
)) {
433 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
440 * Allocate and initialize a new buffer object.
442 * Default callback for the \c dd_function_table::NewBufferObject() hook.
444 static struct gl_buffer_object
*
445 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
447 struct gl_buffer_object
*obj
= MALLOC_STRUCT(gl_buffer_object
);
451 _mesa_initialize_buffer_object(ctx
, obj
, name
);
457 * Delete a buffer object.
459 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
462 _mesa_delete_buffer_object(struct gl_context
*ctx
,
463 struct gl_buffer_object
*bufObj
)
467 vbo_delete_minmax_cache(bufObj
);
468 _mesa_align_free(bufObj
->Data
);
470 /* assign strange values here to help w/ debugging */
471 bufObj
->RefCount
= -1000;
474 simple_mtx_destroy(&bufObj
->MinMaxCacheMutex
);
482 * Set ptr to bufObj w/ reference counting.
483 * This is normally only called from the _mesa_reference_buffer_object() macro
484 * when there's a real pointer change.
487 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
488 struct gl_buffer_object
**ptr
,
489 struct gl_buffer_object
*bufObj
)
492 /* Unreference the old buffer */
493 struct gl_buffer_object
*oldObj
= *ptr
;
495 if (p_atomic_dec_zero(&oldObj
->RefCount
)) {
496 assert(ctx
->Driver
.DeleteBuffer
);
497 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
505 /* reference new buffer */
506 p_atomic_inc(&bufObj
->RefCount
);
513 * Get the value of MESA_NO_MINMAX_CACHE.
516 get_no_minmax_cache()
518 static bool read
= false;
519 static bool disable
= false;
522 disable
= env_var_as_boolean("MESA_NO_MINMAX_CACHE", false);
531 * Initialize a buffer object to default values.
534 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
535 struct gl_buffer_object
*obj
,
538 memset(obj
, 0, sizeof(struct gl_buffer_object
));
541 obj
->Usage
= GL_STATIC_DRAW_ARB
;
543 simple_mtx_init(&obj
->MinMaxCacheMutex
, mtx_plain
);
544 if (get_no_minmax_cache())
545 obj
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
551 * Callback called from _mesa_HashWalk()
554 count_buffer_size(GLuint key
, void *data
, void *userData
)
556 const struct gl_buffer_object
*bufObj
=
557 (const struct gl_buffer_object
*) data
;
558 GLuint
*total
= (GLuint
*) userData
;
561 *total
= *total
+ bufObj
->Size
;
566 * Compute total size (in bytes) of all buffer objects for the given context.
567 * For debugging purposes.
570 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
574 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
581 * Allocate space for and store data in a buffer object. Any data that was
582 * previously stored in the buffer object is lost. If \c data is \c NULL,
583 * memory will be allocated, but no copy will occur.
585 * This is the default callback for \c dd_function_table::BufferData()
586 * Note that all GL error checking will have been done already.
588 * \param ctx GL context.
589 * \param target Buffer object target on which to operate.
590 * \param size Size, in bytes, of the new data store.
591 * \param data Pointer to the data to store in the buffer object. This
592 * pointer may be \c NULL.
593 * \param usage Hints about how the data will be used.
594 * \param bufObj Object to be used.
596 * \return GL_TRUE for success, GL_FALSE for failure
597 * \sa glBufferDataARB, dd_function_table::BufferData.
600 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
601 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
602 struct gl_buffer_object
*bufObj
)
608 _mesa_align_free( bufObj
->Data
);
610 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
612 bufObj
->Data
= (GLubyte
*) new_data
;
614 bufObj
->Usage
= usage
;
615 bufObj
->StorageFlags
= storageFlags
;
618 memcpy( bufObj
->Data
, data
, size
);
630 * Replace data in a subrange of buffer object. If the data range
631 * specified by \c size + \c offset extends beyond the end of the buffer or
632 * if \c data is \c NULL, no copy is performed.
634 * This is the default callback for \c dd_function_table::BufferSubData()
635 * Note that all GL error checking will have been done already.
637 * \param ctx GL context.
638 * \param offset Offset of the first byte to be modified.
639 * \param size Size, in bytes, of the data range.
640 * \param data Pointer to the data to store in the buffer object.
641 * \param bufObj Object to be used.
643 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
646 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptrARB offset
,
647 GLsizeiptrARB size
, const GLvoid
*data
,
648 struct gl_buffer_object
*bufObj
)
652 /* this should have been caught in _mesa_BufferSubData() */
653 assert(size
+ offset
<= bufObj
->Size
);
656 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
662 * Retrieve data from a subrange of buffer object. If the data range
663 * specified by \c size + \c offset extends beyond the end of the buffer or
664 * if \c data is \c NULL, no copy is performed.
666 * This is the default callback for \c dd_function_table::GetBufferSubData()
667 * Note that all GL error checking will have been done already.
669 * \param ctx GL context.
670 * \param target Buffer object target on which to operate.
671 * \param offset Offset of the first byte to be fetched.
672 * \param size Size, in bytes, of the data range.
673 * \param data Destination for data
674 * \param bufObj Object to be used.
676 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
679 buffer_get_subdata(struct gl_context
*ctx
, GLintptrARB offset
,
680 GLsizeiptrARB size
, GLvoid
*data
,
681 struct gl_buffer_object
*bufObj
)
685 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
686 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
692 * Clear a subrange of the buffer object with copies of the supplied data.
693 * If data is NULL the buffer is filled with zeros.
695 * This is the default callback for \c dd_function_table::ClearBufferSubData()
696 * Note that all GL error checking will have been done already.
698 * \param ctx GL context.
699 * \param offset Offset of the first byte to be cleared.
700 * \param size Size, in bytes, of the to be cleared range.
701 * \param clearValue Source of the data.
702 * \param clearValueSize Size, in bytes, of the supplied data.
703 * \param bufObj Object to be cleared.
705 * \sa glClearBufferSubData, glClearBufferData and
706 * dd_function_table::ClearBufferSubData.
709 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
710 GLintptr offset
, GLsizeiptr size
,
711 const GLvoid
*clearValue
,
712 GLsizeiptr clearValueSize
,
713 struct gl_buffer_object
*bufObj
)
718 assert(ctx
->Driver
.MapBufferRange
);
719 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
721 GL_MAP_INVALIDATE_RANGE_BIT
,
722 bufObj
, MAP_INTERNAL
);
725 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
729 if (clearValue
== NULL
) {
730 /* Clear with zeros, per the spec */
731 memset(dest
, 0, size
);
732 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
736 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
737 memcpy(dest
, clearValue
, clearValueSize
);
738 dest
+= clearValueSize
;
741 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
746 * Default fallback for \c dd_function_table::MapBufferRange().
747 * Called via glMapBufferRange().
750 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
751 GLsizeiptr length
, GLbitfield access
,
752 struct gl_buffer_object
*bufObj
,
753 gl_map_buffer_index index
)
756 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
757 /* Just return a direct pointer to the data */
758 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
759 bufObj
->Mappings
[index
].Length
= length
;
760 bufObj
->Mappings
[index
].Offset
= offset
;
761 bufObj
->Mappings
[index
].AccessFlags
= access
;
762 return bufObj
->Mappings
[index
].Pointer
;
767 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
768 * Called via glFlushMappedBufferRange().
771 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
772 GLintptr offset
, GLsizeiptr length
,
773 struct gl_buffer_object
*obj
,
774 gl_map_buffer_index index
)
786 * Default callback for \c dd_function_table::UnmapBuffer().
788 * The input parameters will have been already tested for errors.
790 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
793 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
794 gl_map_buffer_index index
)
797 /* XXX we might assert here that bufObj->Pointer is non-null */
798 bufObj
->Mappings
[index
].Pointer
= NULL
;
799 bufObj
->Mappings
[index
].Length
= 0;
800 bufObj
->Mappings
[index
].Offset
= 0;
801 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
807 * Default fallback for \c dd_function_table::CopyBufferSubData().
808 * Called via glCopyBufferSubData().
811 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
812 struct gl_buffer_object
*src
,
813 struct gl_buffer_object
*dst
,
814 GLintptr readOffset
, GLintptr writeOffset
,
817 GLubyte
*srcPtr
, *dstPtr
;
820 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
822 GL_MAP_WRITE_BIT
, src
,
828 srcPtr
+= readOffset
;
829 dstPtr
+= writeOffset
;
831 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
832 GL_MAP_READ_BIT
, src
,
834 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
836 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
840 /* Note: the src and dst regions will never overlap. Trying to do so
841 * would generate GL_INVALID_VALUE earlier.
843 if (srcPtr
&& dstPtr
)
844 memcpy(dstPtr
, srcPtr
, size
);
846 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
848 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
854 * Initialize the state associated with buffer objects
857 _mesa_init_buffer_objects( struct gl_context
*ctx
)
861 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
862 simple_mtx_init(&DummyBufferObject
.MinMaxCacheMutex
, mtx_plain
);
863 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
865 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
866 ctx
->Shared
->NullBufferObj
);
868 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
869 ctx
->Shared
->NullBufferObj
);
870 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
871 ctx
->Shared
->NullBufferObj
);
873 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
874 ctx
->Shared
->NullBufferObj
);
876 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
,
877 ctx
->Shared
->NullBufferObj
);
879 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
880 ctx
->Shared
->NullBufferObj
);
882 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
883 ctx
->Shared
->NullBufferObj
);
885 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
,
886 ctx
->Shared
->NullBufferObj
);
888 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
,
889 ctx
->Shared
->NullBufferObj
);
891 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
,
892 ctx
->Shared
->NullBufferObj
);
894 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
895 _mesa_reference_buffer_object(ctx
,
896 &ctx
->UniformBufferBindings
[i
].BufferObject
,
897 ctx
->Shared
->NullBufferObj
);
898 ctx
->UniformBufferBindings
[i
].Offset
= -1;
899 ctx
->UniformBufferBindings
[i
].Size
= -1;
902 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
903 _mesa_reference_buffer_object(ctx
,
904 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
905 ctx
->Shared
->NullBufferObj
);
906 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
907 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
910 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
911 _mesa_reference_buffer_object(ctx
,
912 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
913 ctx
->Shared
->NullBufferObj
);
914 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
915 ctx
->AtomicBufferBindings
[i
].Size
= 0;
921 _mesa_free_buffer_objects( struct gl_context
*ctx
)
925 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
927 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
928 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
930 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
932 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
934 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
936 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
938 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
, NULL
);
940 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, NULL
);
942 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
, NULL
);
944 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
945 _mesa_reference_buffer_object(ctx
,
946 &ctx
->UniformBufferBindings
[i
].BufferObject
,
950 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
951 _mesa_reference_buffer_object(ctx
,
952 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
956 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
957 _mesa_reference_buffer_object(ctx
,
958 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
965 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
967 struct gl_buffer_object
**buf_handle
,
970 struct gl_buffer_object
*buf
= *buf_handle
;
972 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
)) {
973 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
977 if (!buf
|| buf
== &DummyBufferObject
) {
978 /* If this is a new buffer object id, or one which was generated but
979 * never used before, allocate a buffer object now.
981 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
983 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
986 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
994 * Bind the specified target to buffer for the specified context.
995 * Called by glBindBuffer() and other functions.
998 bind_buffer_object(struct gl_context
*ctx
,
999 struct gl_buffer_object
**bindTarget
, GLuint buffer
)
1001 struct gl_buffer_object
*oldBufObj
;
1002 struct gl_buffer_object
*newBufObj
= NULL
;
1006 /* Get pointer to old buffer object (to be unbound) */
1007 oldBufObj
= *bindTarget
;
1008 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
1009 return; /* rebinding the same buffer object- no change */
1012 * Get pointer to new buffer object (newBufObj)
1015 /* The spec says there's not a buffer object named 0, but we use
1016 * one internally because it simplifies things.
1018 newBufObj
= ctx
->Shared
->NullBufferObj
;
1021 /* non-default buffer object */
1022 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1023 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1024 &newBufObj
, "glBindBuffer"))
1028 /* record usage history */
1029 if (bindTarget
== &ctx
->Pack
.BufferObj
) {
1030 newBufObj
->UsageHistory
|= USAGE_PIXEL_PACK_BUFFER
;
1033 /* bind new buffer */
1034 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1039 * Update the default buffer objects in the given context to reference those
1040 * specified in the shared state and release those referencing the old
1044 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1046 /* Bind the NullBufferObj to remove references to those
1047 * in the shared context hash table.
1049 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1050 bind_buffer_object(ctx
, &ctx
->Array
.VAO
->IndexBufferObj
, 0);
1051 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1052 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1058 * Return the gl_buffer_object for the given ID.
1059 * Always return NULL for ID 0.
1061 struct gl_buffer_object
*
1062 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1067 return (struct gl_buffer_object
*)
1068 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1072 struct gl_buffer_object
*
1073 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1078 return (struct gl_buffer_object
*)
1079 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1083 * A convenience function for direct state access functions that throws
1084 * GL_INVALID_OPERATION if buffer is not the name of an existing
1087 struct gl_buffer_object
*
1088 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1091 struct gl_buffer_object
*bufObj
;
1093 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1094 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1095 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1096 "%s(non-existent buffer object %u)", caller
, buffer
);
1105 * Look up a buffer object for a multi-bind function.
1107 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1108 * of generating an error if the buffer ID is not zero or the name
1109 * of an existing buffer object.
1111 * If the buffer ID refers to an existing buffer object, a pointer
1112 * to the buffer object is returned. If the ID is zero, a pointer
1113 * to the shared NullBufferObj is returned. If the ID is not zero
1114 * and does not refer to a valid buffer object, this function
1117 * This function assumes that the caller has already locked the
1118 * hash table mutex by calling
1119 * _mesa_HashLockMutex(ctx->Shared->BufferObjects).
1121 struct gl_buffer_object
*
1122 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1123 const GLuint
*buffers
,
1124 GLuint index
, const char *caller
)
1126 struct gl_buffer_object
*bufObj
;
1128 if (buffers
[index
] != 0) {
1129 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1131 /* The multi-bind functions don't create the buffer objects
1132 when they don't exist. */
1133 if (bufObj
== &DummyBufferObject
)
1136 bufObj
= ctx
->Shared
->NullBufferObj
;
1139 /* The ARB_multi_bind spec says:
1141 * "An INVALID_OPERATION error is generated if any value
1142 * in <buffers> is not zero or the name of an existing
1143 * buffer object (per binding)."
1145 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1146 "%s(buffers[%u]=%u is not zero or the name "
1147 "of an existing buffer object)",
1148 caller
, index
, buffers
[index
]);
1156 * If *ptr points to obj, set ptr = the Null/default buffer object.
1157 * This is a helper for buffer object deletion.
1158 * The GL spec says that deleting a buffer object causes it to get
1159 * unbound from all arrays in the current context.
1162 unbind(struct gl_context
*ctx
,
1163 struct gl_vertex_array_object
*vao
, unsigned index
,
1164 struct gl_buffer_object
*obj
)
1166 if (vao
->BufferBinding
[index
].BufferObj
== obj
) {
1167 _mesa_bind_vertex_buffer(ctx
, vao
, index
, ctx
->Shared
->NullBufferObj
,
1168 vao
->BufferBinding
[index
].Offset
,
1169 vao
->BufferBinding
[index
].Stride
);
1175 * Plug default/fallback buffer object functions into the device
1179 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1181 /* GL_ARB_vertex/pixel_buffer_object */
1182 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1183 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1184 driver
->BufferData
= buffer_data_fallback
;
1185 driver
->BufferSubData
= buffer_sub_data_fallback
;
1186 driver
->GetBufferSubData
= buffer_get_subdata
;
1187 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1189 /* GL_ARB_clear_buffer_object */
1190 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1192 /* GL_ARB_map_buffer_range */
1193 driver
->MapBufferRange
= map_buffer_range_fallback
;
1194 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1196 /* GL_ARB_copy_buffer */
1197 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1202 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1203 struct gl_buffer_object
*bufObj
)
1205 for (int i
= 0; i
< MAP_COUNT
; i
++) {
1206 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1207 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1208 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1209 bufObj
->Mappings
[i
].AccessFlags
= 0;
1215 /**********************************************************************/
1217 /**********************************************************************/
1220 _mesa_BindBuffer_no_error(GLenum target
, GLuint buffer
)
1222 GET_CURRENT_CONTEXT(ctx
);
1224 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1225 bind_buffer_object(ctx
, bindTarget
, buffer
);
1230 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1232 GET_CURRENT_CONTEXT(ctx
);
1234 if (MESA_VERBOSE
& VERBOSE_API
) {
1235 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1236 _mesa_enum_to_string(target
), buffer
);
1239 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1241 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target %s)",
1242 _mesa_enum_to_string(target
));
1246 bind_buffer_object(ctx
, bindTarget
, buffer
);
1250 * Binds a buffer object to a binding point.
1252 * The caller is responsible for validating the offset,
1253 * flushing the vertices and updating NewDriverState.
1256 set_buffer_binding(struct gl_context
*ctx
,
1257 struct gl_buffer_binding
*binding
,
1258 struct gl_buffer_object
*bufObj
,
1261 bool autoSize
, gl_buffer_usage usage
)
1263 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1265 binding
->Offset
= offset
;
1266 binding
->Size
= size
;
1267 binding
->AutomaticSize
= autoSize
;
1269 /* If this is a real buffer object, mark it has having been used
1270 * at some point as an atomic counter buffer.
1273 bufObj
->UsageHistory
|= usage
;
1277 set_buffer_multi_binding(struct gl_context
*ctx
,
1278 const GLuint
*buffers
,
1281 struct gl_buffer_binding
*binding
,
1285 gl_buffer_usage usage
)
1287 struct gl_buffer_object
*bufObj
;
1288 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[idx
])
1289 bufObj
= binding
->BufferObject
;
1291 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, idx
, caller
);
1294 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1295 set_buffer_binding(ctx
, binding
, bufObj
, -1, -1, !range
, usage
);
1297 set_buffer_binding(ctx
, binding
, bufObj
, offset
, size
, !range
, usage
);
1302 bind_buffer(struct gl_context
*ctx
,
1303 struct gl_buffer_binding
*binding
,
1304 struct gl_buffer_object
*bufObj
,
1308 uint64_t driver_state
,
1309 gl_buffer_usage usage
)
1311 if (binding
->BufferObject
== bufObj
&&
1312 binding
->Offset
== offset
&&
1313 binding
->Size
== size
&&
1314 binding
->AutomaticSize
== autoSize
) {
1318 FLUSH_VERTICES(ctx
, 0);
1319 ctx
->NewDriverState
|= driver_state
;
1321 set_buffer_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
, usage
);
1325 * Binds a buffer object to a uniform buffer binding point.
1327 * Unlike set_buffer_binding(), this function also flushes vertices
1328 * and updates NewDriverState. It also checks if the binding
1329 * has actually changed before updating it.
1332 bind_uniform_buffer(struct gl_context
*ctx
,
1334 struct gl_buffer_object
*bufObj
,
1339 bind_buffer(ctx
, &ctx
->UniformBufferBindings
[index
],
1340 bufObj
, offset
, size
, autoSize
,
1341 ctx
->DriverFlags
.NewUniformBuffer
,
1342 USAGE_UNIFORM_BUFFER
);
1346 * Binds a buffer object to a shader storage buffer binding point.
1348 * Unlike set_ssbo_binding(), this function also flushes vertices
1349 * and updates NewDriverState. It also checks if the binding
1350 * has actually changed before updating it.
1353 bind_shader_storage_buffer(struct gl_context
*ctx
,
1355 struct gl_buffer_object
*bufObj
,
1360 bind_buffer(ctx
, &ctx
->ShaderStorageBufferBindings
[index
],
1361 bufObj
, offset
, size
, autoSize
,
1362 ctx
->DriverFlags
.NewShaderStorageBuffer
,
1363 USAGE_SHADER_STORAGE_BUFFER
);
1367 * Binds a buffer object to an atomic buffer binding point.
1369 * Unlike set_atomic_binding(), this function also flushes vertices
1370 * and updates NewDriverState. It also checks if the binding
1371 * has actually changed before updating it.
1374 bind_atomic_buffer(struct gl_context
*ctx
, unsigned index
,
1375 struct gl_buffer_object
*bufObj
, GLintptr offset
,
1376 GLsizeiptr size
, GLboolean autoSize
)
1378 bind_buffer(ctx
, &ctx
->AtomicBufferBindings
[index
],
1379 bufObj
, offset
, size
, autoSize
,
1380 ctx
->DriverFlags
.NewAtomicBuffer
,
1381 USAGE_ATOMIC_COUNTER_BUFFER
);
1385 * Bind a buffer object to a uniform block binding point.
1386 * As above, but offset = 0.
1389 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
1391 struct gl_buffer_object
*bufObj
)
1393 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
1394 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1398 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
1400 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1401 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1403 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1407 * Bind a buffer object to a shader storage block binding point.
1408 * As above, but offset = 0.
1411 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
1413 struct gl_buffer_object
*bufObj
)
1415 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
1416 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1420 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
1422 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1423 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1425 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1429 * Bind a buffer object to a shader storage block binding point.
1430 * As above, but offset = 0.
1433 bind_buffer_base_atomic_buffer(struct gl_context
*ctx
,
1435 struct gl_buffer_object
*bufObj
)
1437 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
1438 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1442 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
1444 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1445 bind_atomic_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1447 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1451 * Delete a set of buffer objects.
1453 * \param n Number of buffer objects to delete.
1454 * \param ids Array of \c n buffer object IDs.
1457 delete_buffers(struct gl_context
*ctx
, GLsizei n
, const GLuint
*ids
)
1459 FLUSH_VERTICES(ctx
, 0);
1461 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1463 for (GLsizei i
= 0; i
< n
; i
++) {
1464 struct gl_buffer_object
*bufObj
=
1465 _mesa_lookup_bufferobj_locked(ctx
, ids
[i
]);
1467 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1470 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1472 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1474 /* unbind any vertex pointers bound to this buffer */
1475 for (j
= 0; j
< ARRAY_SIZE(vao
->BufferBinding
); j
++) {
1476 unbind(ctx
, vao
, j
, bufObj
);
1479 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1480 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1482 if (vao
->IndexBufferObj
== bufObj
) {
1483 bind_buffer_object(ctx
, &vao
->IndexBufferObj
, 0);
1486 /* unbind ARB_draw_indirect binding point */
1487 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1488 bind_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, 0);
1491 /* unbind ARB_indirect_parameters binding point */
1492 if (ctx
->ParameterBuffer
== bufObj
) {
1493 bind_buffer_object(ctx
, &ctx
->ParameterBuffer
, 0);
1496 /* unbind ARB_compute_shader binding point */
1497 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1498 bind_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, 0);
1501 /* unbind ARB_copy_buffer binding points */
1502 if (ctx
->CopyReadBuffer
== bufObj
) {
1503 bind_buffer_object(ctx
, &ctx
->CopyReadBuffer
, 0);
1505 if (ctx
->CopyWriteBuffer
== bufObj
) {
1506 bind_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, 0);
1509 /* unbind transform feedback binding points */
1510 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1511 bind_buffer_object(ctx
, &ctx
->TransformFeedback
.CurrentBuffer
, 0);
1513 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1514 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1515 _mesa_bind_buffer_base_transform_feedback(ctx
,
1516 ctx
->TransformFeedback
.CurrentObject
,
1517 j
, ctx
->Shared
->NullBufferObj
,
1522 /* unbind UBO binding points */
1523 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1524 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1525 bind_buffer_base_uniform_buffer(ctx
, j
,
1526 ctx
->Shared
->NullBufferObj
);
1530 if (ctx
->UniformBuffer
== bufObj
) {
1531 bind_buffer_object(ctx
, &ctx
->UniformBuffer
, 0);
1534 /* unbind SSBO binding points */
1535 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1536 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1537 bind_buffer_base_shader_storage_buffer(ctx
, j
,
1538 ctx
->Shared
->NullBufferObj
);
1542 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1543 bind_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, 0);
1546 /* unbind Atomci Buffer binding points */
1547 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1548 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1549 bind_buffer_base_atomic_buffer(ctx
, j
,
1550 ctx
->Shared
->NullBufferObj
);
1554 if (ctx
->AtomicBuffer
== bufObj
) {
1555 bind_buffer_object(ctx
, &ctx
->AtomicBuffer
, 0);
1558 /* unbind any pixel pack/unpack pointers bound to this buffer */
1559 if (ctx
->Pack
.BufferObj
== bufObj
) {
1560 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1562 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1563 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1566 if (ctx
->Texture
.BufferObject
== bufObj
) {
1567 bind_buffer_object(ctx
, &ctx
->Texture
.BufferObject
, 0);
1570 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1571 bind_buffer_object(ctx
, &ctx
->ExternalVirtualMemoryBuffer
, 0);
1574 /* unbind query buffer binding point */
1575 if (ctx
->QueryBuffer
== bufObj
) {
1576 bind_buffer_object(ctx
, &ctx
->QueryBuffer
, 0);
1579 /* The ID is immediately freed for re-use */
1580 _mesa_HashRemoveLocked(ctx
->Shared
->BufferObjects
, ids
[i
]);
1581 /* Make sure we do not run into the classic ABA problem on bind.
1582 * We don't want to allow re-binding a buffer object that's been
1583 * "deleted" by glDeleteBuffers().
1585 * The explicit rebinding to the default object in the current context
1586 * prevents the above in the current context, but another context
1587 * sharing the same objects might suffer from this problem.
1588 * The alternative would be to do the hash lookup in any case on bind
1589 * which would introduce more runtime overhead than this.
1591 bufObj
->DeletePending
= GL_TRUE
;
1592 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1596 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1601 _mesa_DeleteBuffers_no_error(GLsizei n
, const GLuint
*ids
)
1603 GET_CURRENT_CONTEXT(ctx
);
1604 delete_buffers(ctx
, n
, ids
);
1609 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1611 GET_CURRENT_CONTEXT(ctx
);
1614 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1618 delete_buffers(ctx
, n
, ids
);
1623 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1624 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1628 create_buffers(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1631 struct gl_buffer_object
*buf
;
1637 * This must be atomic (generation and allocation of buffer object IDs)
1639 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1641 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1643 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1644 * DummyBufferObject. Otherwise, create a new buffer object and insert
1647 for (int i
= 0; i
< n
; i
++) {
1648 buffers
[i
] = first
+ i
;
1650 assert(ctx
->Driver
.NewBufferObject
);
1651 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1653 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCreateBuffers");
1654 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1659 buf
= &DummyBufferObject
;
1661 _mesa_HashInsertLocked(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1664 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1669 create_buffers_err(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1671 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1673 if (MESA_VERBOSE
& VERBOSE_API
)
1674 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1677 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1681 create_buffers(ctx
, n
, buffers
, dsa
);
1685 * Generate a set of unique buffer object IDs and store them in \c buffers.
1687 * \param n Number of IDs to generate.
1688 * \param buffers Array of \c n locations to store the IDs.
1691 _mesa_GenBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1693 GET_CURRENT_CONTEXT(ctx
);
1694 create_buffers(ctx
, n
, buffers
, false);
1699 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1701 GET_CURRENT_CONTEXT(ctx
);
1702 create_buffers_err(ctx
, n
, buffers
, false);
1706 * Create a set of buffer objects and store their unique IDs in \c buffers.
1708 * \param n Number of IDs to generate.
1709 * \param buffers Array of \c n locations to store the IDs.
1712 _mesa_CreateBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1714 GET_CURRENT_CONTEXT(ctx
);
1715 create_buffers(ctx
, n
, buffers
, true);
1720 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1722 GET_CURRENT_CONTEXT(ctx
);
1723 create_buffers_err(ctx
, n
, buffers
, true);
1728 * Determine if ID is the name of a buffer object.
1730 * \param id ID of the potential buffer object.
1731 * \return \c GL_TRUE if \c id is the name of a buffer object,
1732 * \c GL_FALSE otherwise.
1734 GLboolean GLAPIENTRY
1735 _mesa_IsBuffer(GLuint id
)
1737 struct gl_buffer_object
*bufObj
;
1738 GET_CURRENT_CONTEXT(ctx
);
1739 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1741 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1743 return bufObj
&& bufObj
!= &DummyBufferObject
;
1748 validate_buffer_storage(struct gl_context
*ctx
,
1749 struct gl_buffer_object
*bufObj
, GLsizeiptr size
,
1750 GLbitfield flags
, const char *func
)
1753 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1757 GLbitfield valid_flags
= GL_MAP_READ_BIT
|
1759 GL_MAP_PERSISTENT_BIT
|
1760 GL_MAP_COHERENT_BIT
|
1761 GL_DYNAMIC_STORAGE_BIT
|
1762 GL_CLIENT_STORAGE_BIT
;
1764 if (ctx
->Extensions
.ARB_sparse_buffer
)
1765 valid_flags
|= GL_SPARSE_STORAGE_BIT_ARB
;
1767 if (flags
& ~valid_flags
) {
1768 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1772 /* The Errors section of the GL_ARB_sparse_buffer spec says:
1774 * "INVALID_VALUE is generated by BufferStorage if <flags> contains
1775 * SPARSE_STORAGE_BIT_ARB and <flags> also contains any combination of
1776 * MAP_READ_BIT or MAP_WRITE_BIT."
1778 if (flags
& GL_SPARSE_STORAGE_BIT_ARB
&&
1779 flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) {
1780 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(SPARSE_STORAGE and READ/WRITE)", func
);
1784 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1785 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1786 _mesa_error(ctx
, GL_INVALID_VALUE
,
1787 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1791 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1792 _mesa_error(ctx
, GL_INVALID_VALUE
,
1793 "%s(COHERENT and flags!=PERSISTENT)", func
);
1797 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
1798 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1807 buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1808 struct gl_memory_object
*memObj
, GLenum target
,
1809 GLsizeiptr size
, const GLvoid
*data
, GLbitfield flags
,
1810 GLuint64 offset
, const char *func
)
1814 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1815 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1817 FLUSH_VERTICES(ctx
, 0);
1819 bufObj
->Written
= GL_TRUE
;
1820 bufObj
->Immutable
= GL_TRUE
;
1821 bufObj
->MinMaxCacheDirty
= true;
1824 assert(ctx
->Driver
.BufferDataMem
);
1825 res
= ctx
->Driver
.BufferDataMem(ctx
, target
, size
, memObj
, offset
,
1826 GL_DYNAMIC_DRAW
, bufObj
);
1829 assert(ctx
->Driver
.BufferData
);
1830 res
= ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1835 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1836 /* Even though the interaction between AMD_pinned_memory and
1837 * glBufferStorage is not described in the spec, Graham Sellers
1838 * said that it should behave the same as glBufferData.
1840 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1843 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1849 static ALWAYS_INLINE
void
1850 inlined_buffer_storage(GLenum target
, GLuint buffer
, GLsizeiptr size
,
1851 const GLvoid
*data
, GLbitfield flags
,
1852 GLuint memory
, GLuint64 offset
,
1853 bool dsa
, bool mem
, bool no_error
, const char *func
)
1855 GET_CURRENT_CONTEXT(ctx
);
1856 struct gl_buffer_object
*bufObj
;
1857 struct gl_memory_object
*memObj
= NULL
;
1861 if (!ctx
->Extensions
.EXT_memory_object
) {
1862 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
1866 /* From the EXT_external_objects spec:
1868 * "An INVALID_VALUE error is generated by BufferStorageMemEXT and
1869 * NamedBufferStorageMemEXT if <memory> is 0, or ..."
1872 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(memory == 0)", func
);
1876 memObj
= _mesa_lookup_memory_object(ctx
, memory
);
1880 /* From the EXT_external_objects spec:
1882 * "An INVALID_OPERATION error is generated if <memory> names a
1883 * valid memory object which has no associated memory."
1885 if (!no_error
&& !memObj
->Immutable
) {
1886 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no associated memory)",
1894 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1896 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
1902 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
1903 bufObj
= *bufObjPtr
;
1905 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
1911 if (no_error
|| validate_buffer_storage(ctx
, bufObj
, size
, flags
, func
))
1912 buffer_storage(ctx
, bufObj
, memObj
, target
, size
, data
, flags
, offset
, func
);
1917 _mesa_BufferStorage_no_error(GLenum target
, GLsizeiptr size
,
1918 const GLvoid
*data
, GLbitfield flags
)
1920 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1921 false, false, true, "glBufferStorage");
1926 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1929 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1930 false, false, false, "glBufferStorage");
1935 _mesa_BufferStorageMemEXT(GLenum target
, GLsizeiptr size
,
1936 GLuint memory
, GLuint64 offset
)
1938 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1939 false, true, false, "glBufferStorageMemEXT");
1944 _mesa_BufferStorageMemEXT_no_error(GLenum target
, GLsizeiptr size
,
1945 GLuint memory
, GLuint64 offset
)
1947 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1948 false, true, true, "glBufferStorageMemEXT");
1953 _mesa_NamedBufferStorage_no_error(GLuint buffer
, GLsizeiptr size
,
1954 const GLvoid
*data
, GLbitfield flags
)
1956 /* In direct state access, buffer objects have an unspecified target
1957 * since they are not required to be bound.
1959 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1960 true, false, true, "glNamedBufferStorage");
1965 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1968 /* In direct state access, buffer objects have an unspecified target
1969 * since they are not required to be bound.
1971 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1972 true, false, false, "glNamedBufferStorage");
1976 _mesa_NamedBufferStorageMemEXT(GLuint buffer
, GLsizeiptr size
,
1977 GLuint memory
, GLuint64 offset
)
1979 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
1980 true, true, false, "glNamedBufferStorageMemEXT");
1985 _mesa_NamedBufferStorageMemEXT_no_error(GLuint buffer
, GLsizeiptr size
,
1986 GLuint memory
, GLuint64 offset
)
1988 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
1989 true, true, true, "glNamedBufferStorageMemEXT");
1993 static ALWAYS_INLINE
void
1994 buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1995 GLenum target
, GLsizeiptr size
, const GLvoid
*data
, GLenum usage
,
1996 const char *func
, bool no_error
)
2000 if (MESA_VERBOSE
& VERBOSE_API
) {
2001 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
2003 _mesa_enum_to_string(target
),
2004 (long int) size
, data
,
2005 _mesa_enum_to_string(usage
));
2010 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
2015 case GL_STREAM_DRAW_ARB
:
2016 valid_usage
= (ctx
->API
!= API_OPENGLES
);
2018 case GL_STATIC_DRAW_ARB
:
2019 case GL_DYNAMIC_DRAW_ARB
:
2022 case GL_STREAM_READ_ARB
:
2023 case GL_STREAM_COPY_ARB
:
2024 case GL_STATIC_READ_ARB
:
2025 case GL_STATIC_COPY_ARB
:
2026 case GL_DYNAMIC_READ_ARB
:
2027 case GL_DYNAMIC_COPY_ARB
:
2028 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
2031 valid_usage
= false;
2036 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
2037 _mesa_enum_to_string(usage
));
2041 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
2042 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
2047 /* Unmap the existing buffer. We'll replace it now. Not an error. */
2048 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
2050 FLUSH_VERTICES(ctx
, 0);
2052 bufObj
->Written
= GL_TRUE
;
2053 bufObj
->MinMaxCacheDirty
= true;
2056 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
2057 bufObj
->Name
, size
, data
, usage
);
2064 assert(ctx
->Driver
.BufferData
);
2065 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
2068 GL_DYNAMIC_STORAGE_BIT
,
2070 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
2072 /* From GL_AMD_pinned_memory:
2074 * INVALID_OPERATION is generated by BufferData if <target> is
2075 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
2076 * mapped to the GPU address space.
2078 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2081 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
2087 buffer_data_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2088 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2089 GLenum usage
, const char *func
)
2091 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, false);
2095 buffer_data_no_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2096 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2097 GLenum usage
, const char *func
)
2099 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, true);
2103 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2104 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2105 GLenum usage
, const char *func
)
2107 buffer_data_error(ctx
, bufObj
, target
, size
, data
, usage
, func
);
2111 _mesa_BufferData_no_error(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2114 GET_CURRENT_CONTEXT(ctx
);
2116 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2117 buffer_data_no_error(ctx
, *bufObj
, target
, size
, data
, usage
,
2122 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
2123 const GLvoid
*data
, GLenum usage
)
2125 GET_CURRENT_CONTEXT(ctx
);
2126 struct gl_buffer_object
*bufObj
;
2128 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
2132 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
2137 _mesa_NamedBufferData_no_error(GLuint buffer
, GLsizeiptr size
,
2138 const GLvoid
*data
, GLenum usage
)
2140 GET_CURRENT_CONTEXT(ctx
);
2142 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2143 buffer_data_no_error(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2144 "glNamedBufferData");
2148 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2151 GET_CURRENT_CONTEXT(ctx
);
2152 struct gl_buffer_object
*bufObj
;
2154 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
2158 /* In direct state access, buffer objects have an unspecified target since
2159 * they are not required to be bound.
2161 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2162 "glNamedBufferData");
2167 validate_buffer_sub_data(struct gl_context
*ctx
,
2168 struct gl_buffer_object
*bufObj
,
2169 GLintptr offset
, GLsizeiptr size
,
2172 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2174 /* error already recorded */
2178 if (bufObj
->Immutable
&&
2179 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
2180 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2184 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2185 bufObj
->Usage
== GL_STATIC_COPY
) &&
2186 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
- 1) {
2187 /* If the application declared the buffer as static draw/copy or stream
2188 * draw, it should not be frequently modified with glBufferSubData.
2190 BUFFER_USAGE_WARNING(ctx
,
2191 "using %s(buffer %u, offset %u, size %u) to "
2192 "update a %s buffer",
2193 func
, bufObj
->Name
, offset
, size
,
2194 _mesa_enum_to_string(bufObj
->Usage
));
2202 * Implementation for glBufferSubData and glNamedBufferSubData.
2204 * \param ctx GL context.
2205 * \param bufObj The buffer object.
2206 * \param offset Offset of the first byte of the subdata range.
2207 * \param size Size, in bytes, of the subdata range.
2208 * \param data The data store.
2209 * \param func Name of calling function for recording errors.
2213 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2214 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
)
2219 bufObj
->NumSubDataCalls
++;
2220 bufObj
->Written
= GL_TRUE
;
2221 bufObj
->MinMaxCacheDirty
= true;
2223 assert(ctx
->Driver
.BufferSubData
);
2224 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
2228 static ALWAYS_INLINE
void
2229 buffer_sub_data(GLenum target
, GLuint buffer
, GLintptr offset
,
2230 GLsizeiptr size
, const GLvoid
*data
,
2231 bool dsa
, bool no_error
, const char *func
)
2233 GET_CURRENT_CONTEXT(ctx
);
2234 struct gl_buffer_object
*bufObj
;
2238 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2240 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
2246 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2247 bufObj
= *bufObjPtr
;
2249 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
2255 if (no_error
|| validate_buffer_sub_data(ctx
, bufObj
, offset
, size
, func
))
2256 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
);
2261 _mesa_BufferSubData_no_error(GLenum target
, GLintptr offset
,
2262 GLsizeiptr size
, const GLvoid
*data
)
2264 buffer_sub_data(target
, 0, offset
, size
, data
, false, true,
2270 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
2271 GLsizeiptr size
, const GLvoid
*data
)
2273 buffer_sub_data(target
, 0, offset
, size
, data
, false, false,
2278 _mesa_NamedBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
2279 GLsizeiptr size
, const GLvoid
*data
)
2281 buffer_sub_data(0, buffer
, offset
, size
, data
, true, true,
2282 "glNamedBufferSubData");
2286 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
2287 GLsizeiptr size
, const GLvoid
*data
)
2289 buffer_sub_data(0, buffer
, offset
, size
, data
, true, false,
2290 "glNamedBufferSubData");
2295 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
2296 GLsizeiptr size
, GLvoid
*data
)
2298 GET_CURRENT_CONTEXT(ctx
);
2299 struct gl_buffer_object
*bufObj
;
2301 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
2302 GL_INVALID_OPERATION
);
2306 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2307 "glGetBufferSubData")) {
2311 assert(ctx
->Driver
.GetBufferSubData
);
2312 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2316 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
2317 GLsizeiptr size
, GLvoid
*data
)
2319 GET_CURRENT_CONTEXT(ctx
);
2320 struct gl_buffer_object
*bufObj
;
2322 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2323 "glGetNamedBufferSubData");
2327 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2328 "glGetNamedBufferSubData")) {
2332 assert(ctx
->Driver
.GetBufferSubData
);
2333 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2338 * \param subdata true if caller is *SubData, false if *Data
2340 static ALWAYS_INLINE
void
2341 clear_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2342 GLenum internalformat
, GLintptr offset
, GLsizeiptr size
,
2343 GLenum format
, GLenum type
, const GLvoid
*data
,
2344 const char *func
, bool subdata
, bool no_error
)
2346 mesa_format mesaFormat
;
2347 GLubyte clearValue
[MAX_PIXEL_BYTES
];
2348 GLsizeiptr clearValueSize
;
2350 /* This checks for disallowed mappings. */
2351 if (!no_error
&& !buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2357 mesaFormat
= _mesa_get_texbuffer_format(ctx
, internalformat
);
2359 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
2360 format
, type
, func
);
2363 if (mesaFormat
== MESA_FORMAT_NONE
)
2366 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
2368 (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0)) {
2369 _mesa_error(ctx
, GL_INVALID_VALUE
,
2370 "%s(offset or size is not a multiple of "
2371 "internalformat size)", func
);
2375 /* Bail early. Negative size has already been checked. */
2379 bufObj
->MinMaxCacheDirty
= true;
2382 /* clear to zeros, per the spec */
2383 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2384 NULL
, clearValueSize
, bufObj
);
2388 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
2389 format
, type
, data
, func
)) {
2393 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2394 clearValue
, clearValueSize
, bufObj
);
2398 clear_buffer_sub_data_error(struct gl_context
*ctx
,
2399 struct gl_buffer_object
*bufObj
,
2400 GLenum internalformat
, GLintptr offset
,
2401 GLsizeiptr size
, GLenum format
, GLenum type
,
2402 const GLvoid
*data
, const char *func
, bool subdata
)
2404 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2405 type
, data
, func
, subdata
, false);
2410 clear_buffer_sub_data_no_error(struct gl_context
*ctx
,
2411 struct gl_buffer_object
*bufObj
,
2412 GLenum internalformat
, GLintptr offset
,
2413 GLsizeiptr size
, GLenum format
, GLenum type
,
2414 const GLvoid
*data
, const char *func
,
2417 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2418 type
, data
, func
, subdata
, true);
2423 _mesa_ClearBufferData_no_error(GLenum target
, GLenum internalformat
,
2424 GLenum format
, GLenum type
, const GLvoid
*data
)
2426 GET_CURRENT_CONTEXT(ctx
);
2428 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2429 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, 0,
2430 (*bufObj
)->Size
, format
, type
, data
,
2431 "glClearBufferData", false);
2436 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
2437 GLenum type
, const GLvoid
*data
)
2439 GET_CURRENT_CONTEXT(ctx
);
2440 struct gl_buffer_object
*bufObj
;
2442 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
2446 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2447 format
, type
, data
, "glClearBufferData", false);
2452 _mesa_ClearNamedBufferData_no_error(GLuint buffer
, GLenum internalformat
,
2453 GLenum format
, GLenum type
,
2456 GET_CURRENT_CONTEXT(ctx
);
2458 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2459 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2460 format
, type
, data
, "glClearNamedBufferData",
2466 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
2467 GLenum format
, GLenum type
, const GLvoid
*data
)
2469 GET_CURRENT_CONTEXT(ctx
);
2470 struct gl_buffer_object
*bufObj
;
2472 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
2476 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2477 format
, type
, data
, "glClearNamedBufferData",
2483 _mesa_ClearBufferSubData_no_error(GLenum target
, GLenum internalformat
,
2484 GLintptr offset
, GLsizeiptr size
,
2485 GLenum format
, GLenum type
,
2488 GET_CURRENT_CONTEXT(ctx
);
2490 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2491 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, offset
, size
,
2492 format
, type
, data
, "glClearBufferSubData",
2498 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
2499 GLintptr offset
, GLsizeiptr size
,
2500 GLenum format
, GLenum type
,
2503 GET_CURRENT_CONTEXT(ctx
);
2504 struct gl_buffer_object
*bufObj
;
2506 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
2510 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2511 format
, type
, data
, "glClearBufferSubData",
2517 _mesa_ClearNamedBufferSubData_no_error(GLuint buffer
, GLenum internalformat
,
2518 GLintptr offset
, GLsizeiptr size
,
2519 GLenum format
, GLenum type
,
2522 GET_CURRENT_CONTEXT(ctx
);
2524 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2525 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, offset
, size
,
2527 "glClearNamedBufferSubData", true);
2532 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
2533 GLintptr offset
, GLsizeiptr size
,
2534 GLenum format
, GLenum type
,
2537 GET_CURRENT_CONTEXT(ctx
);
2538 struct gl_buffer_object
*bufObj
;
2540 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2541 "glClearNamedBufferSubData");
2545 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2546 format
, type
, data
, "glClearNamedBufferSubData",
2551 unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
2553 GLboolean status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2554 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2555 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2556 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2557 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2563 validate_and_unmap_buffer(struct gl_context
*ctx
,
2564 struct gl_buffer_object
*bufObj
,
2567 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2569 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2570 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2571 "%s(buffer is not mapped)", func
);
2576 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2577 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2579 /* check that last 100 bytes are still = magic value */
2580 for (i
= 0; i
< 100; i
++) {
2581 GLuint pos
= bufObj
->Size
- i
- 1;
2582 if (buf
[pos
] != 123) {
2583 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2584 " at position %d (value = %u)\n",
2592 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2593 GLuint i
, unchanged
= 0;
2594 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2596 /* check which bytes changed */
2597 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2598 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2605 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2606 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2611 return unmap_buffer(ctx
, bufObj
);
2614 GLboolean GLAPIENTRY
2615 _mesa_UnmapBuffer_no_error(GLenum target
)
2617 GET_CURRENT_CONTEXT(ctx
);
2618 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2619 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2621 return unmap_buffer(ctx
, bufObj
);
2624 GLboolean GLAPIENTRY
2625 _mesa_UnmapBuffer(GLenum target
)
2627 GET_CURRENT_CONTEXT(ctx
);
2628 struct gl_buffer_object
*bufObj
;
2630 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2634 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2637 GLboolean GLAPIENTRY
2638 _mesa_UnmapNamedBuffer_no_error(GLuint buffer
)
2640 GET_CURRENT_CONTEXT(ctx
);
2641 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2643 return unmap_buffer(ctx
, bufObj
);
2646 GLboolean GLAPIENTRY
2647 _mesa_UnmapNamedBuffer(GLuint buffer
)
2649 GET_CURRENT_CONTEXT(ctx
);
2650 struct gl_buffer_object
*bufObj
;
2652 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2656 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2661 get_buffer_parameter(struct gl_context
*ctx
,
2662 struct gl_buffer_object
*bufObj
, GLenum pname
,
2663 GLint64
*params
, const char *func
)
2666 case GL_BUFFER_SIZE_ARB
:
2667 *params
= bufObj
->Size
;
2669 case GL_BUFFER_USAGE_ARB
:
2670 *params
= bufObj
->Usage
;
2672 case GL_BUFFER_ACCESS_ARB
:
2673 *params
= simplified_access_mode(ctx
,
2674 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2676 case GL_BUFFER_MAPPED_ARB
:
2677 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2679 case GL_BUFFER_ACCESS_FLAGS
:
2680 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2682 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2684 case GL_BUFFER_MAP_OFFSET
:
2685 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2687 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2689 case GL_BUFFER_MAP_LENGTH
:
2690 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2692 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2694 case GL_BUFFER_IMMUTABLE_STORAGE
:
2695 if (!ctx
->Extensions
.ARB_buffer_storage
)
2697 *params
= bufObj
->Immutable
;
2699 case GL_BUFFER_STORAGE_FLAGS
:
2700 if (!ctx
->Extensions
.ARB_buffer_storage
)
2702 *params
= bufObj
->StorageFlags
;
2711 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2712 _mesa_enum_to_string(pname
));
2717 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2719 GET_CURRENT_CONTEXT(ctx
);
2720 struct gl_buffer_object
*bufObj
;
2723 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2724 GL_INVALID_OPERATION
);
2728 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2729 "glGetBufferParameteriv"))
2730 return; /* Error already recorded. */
2732 *params
= (GLint
) parameter
;
2736 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2738 GET_CURRENT_CONTEXT(ctx
);
2739 struct gl_buffer_object
*bufObj
;
2742 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2743 GL_INVALID_OPERATION
);
2747 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2748 "glGetBufferParameteri64v"))
2749 return; /* Error already recorded. */
2751 *params
= parameter
;
2755 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2757 GET_CURRENT_CONTEXT(ctx
);
2758 struct gl_buffer_object
*bufObj
;
2761 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2762 "glGetNamedBufferParameteriv");
2766 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2767 "glGetNamedBufferParameteriv"))
2768 return; /* Error already recorded. */
2770 *params
= (GLint
) parameter
;
2774 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2777 GET_CURRENT_CONTEXT(ctx
);
2778 struct gl_buffer_object
*bufObj
;
2781 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2782 "glGetNamedBufferParameteri64v");
2786 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2787 "glGetNamedBufferParameteri64v"))
2788 return; /* Error already recorded. */
2790 *params
= parameter
;
2795 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2797 GET_CURRENT_CONTEXT(ctx
);
2798 struct gl_buffer_object
*bufObj
;
2800 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2801 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2802 "GL_BUFFER_MAP_POINTER)");
2806 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2807 GL_INVALID_OPERATION
);
2811 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2815 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2817 GET_CURRENT_CONTEXT(ctx
);
2818 struct gl_buffer_object
*bufObj
;
2820 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2821 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2822 "GL_BUFFER_MAP_POINTER)");
2826 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2827 "glGetNamedBufferPointerv");
2831 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2836 copy_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*src
,
2837 struct gl_buffer_object
*dst
, GLintptr readOffset
,
2838 GLintptr writeOffset
, GLsizeiptr size
, const char *func
)
2840 if (_mesa_check_disallowed_mapping(src
)) {
2841 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2842 "%s(readBuffer is mapped)", func
);
2846 if (_mesa_check_disallowed_mapping(dst
)) {
2847 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2848 "%s(writeBuffer is mapped)", func
);
2852 if (readOffset
< 0) {
2853 _mesa_error(ctx
, GL_INVALID_VALUE
,
2854 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2858 if (writeOffset
< 0) {
2859 _mesa_error(ctx
, GL_INVALID_VALUE
,
2860 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2865 _mesa_error(ctx
, GL_INVALID_VALUE
,
2866 "%s(size %d < 0)", func
, (int) size
);
2870 if (readOffset
+ size
> src
->Size
) {
2871 _mesa_error(ctx
, GL_INVALID_VALUE
,
2872 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2873 (int) readOffset
, (int) size
, (int) src
->Size
);
2877 if (writeOffset
+ size
> dst
->Size
) {
2878 _mesa_error(ctx
, GL_INVALID_VALUE
,
2879 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2880 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2885 if (readOffset
+ size
<= writeOffset
) {
2888 else if (writeOffset
+ size
<= readOffset
) {
2892 /* overlapping src/dst is illegal */
2893 _mesa_error(ctx
, GL_INVALID_VALUE
,
2894 "%s(overlapping src/dst)", func
);
2899 dst
->MinMaxCacheDirty
= true;
2901 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2905 _mesa_CopyBufferSubData_no_error(GLenum readTarget
, GLenum writeTarget
,
2906 GLintptr readOffset
, GLintptr writeOffset
,
2909 GET_CURRENT_CONTEXT(ctx
);
2911 struct gl_buffer_object
**src_ptr
= get_buffer_target(ctx
, readTarget
);
2912 struct gl_buffer_object
*src
= *src_ptr
;
2914 struct gl_buffer_object
**dst_ptr
= get_buffer_target(ctx
, writeTarget
);
2915 struct gl_buffer_object
*dst
= *dst_ptr
;
2917 dst
->MinMaxCacheDirty
= true;
2918 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
2923 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2924 GLintptr readOffset
, GLintptr writeOffset
,
2927 GET_CURRENT_CONTEXT(ctx
);
2928 struct gl_buffer_object
*src
, *dst
;
2930 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2931 GL_INVALID_OPERATION
);
2935 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2936 GL_INVALID_OPERATION
);
2940 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2941 "glCopyBufferSubData");
2945 _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer
, GLuint writeBuffer
,
2946 GLintptr readOffset
,
2947 GLintptr writeOffset
, GLsizeiptr size
)
2949 GET_CURRENT_CONTEXT(ctx
);
2951 struct gl_buffer_object
*src
= _mesa_lookup_bufferobj(ctx
, readBuffer
);
2952 struct gl_buffer_object
*dst
= _mesa_lookup_bufferobj(ctx
, writeBuffer
);
2954 dst
->MinMaxCacheDirty
= true;
2955 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
2960 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2961 GLintptr readOffset
, GLintptr writeOffset
,
2964 GET_CURRENT_CONTEXT(ctx
);
2965 struct gl_buffer_object
*src
, *dst
;
2967 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2968 "glCopyNamedBufferSubData");
2972 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2973 "glCopyNamedBufferSubData");
2977 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2978 "glCopyNamedBufferSubData");
2982 validate_map_buffer_range(struct gl_context
*ctx
,
2983 struct gl_buffer_object
*bufObj
, GLintptr offset
,
2984 GLsizeiptr length
, GLbitfield access
,
2987 GLbitfield allowed_access
;
2989 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, false);
2992 _mesa_error(ctx
, GL_INVALID_VALUE
,
2993 "%s(offset %ld < 0)", func
, (long) offset
);
2998 _mesa_error(ctx
, GL_INVALID_VALUE
,
2999 "%s(length %ld < 0)", func
, (long) length
);
3003 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
3005 * "An INVALID_OPERATION error is generated for any of the following
3008 * * <length> is zero."
3010 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
3011 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
3015 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
3019 allowed_access
= GL_MAP_READ_BIT
|
3021 GL_MAP_INVALIDATE_RANGE_BIT
|
3022 GL_MAP_INVALIDATE_BUFFER_BIT
|
3023 GL_MAP_FLUSH_EXPLICIT_BIT
|
3024 GL_MAP_UNSYNCHRONIZED_BIT
;
3026 if (ctx
->Extensions
.ARB_buffer_storage
) {
3027 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
3028 GL_MAP_COHERENT_BIT
;
3031 if (access
& ~allowed_access
) {
3032 /* generate an error if any bits other than those allowed are set */
3033 _mesa_error(ctx
, GL_INVALID_VALUE
,
3034 "%s(access has undefined bits set)", func
);
3038 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
3039 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3040 "%s(access indicates neither read or write)", func
);
3044 if ((access
& GL_MAP_READ_BIT
) &&
3045 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
3046 GL_MAP_INVALIDATE_BUFFER_BIT
|
3047 GL_MAP_UNSYNCHRONIZED_BIT
))) {
3048 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3049 "%s(read access with disallowed bits)", func
);
3053 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
3054 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
3055 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3056 "%s(access has flush explicit without write)", func
);
3060 if (access
& GL_MAP_READ_BIT
&&
3061 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
3062 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3063 "%s(buffer does not allow read access)", func
);
3067 if (access
& GL_MAP_WRITE_BIT
&&
3068 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
3069 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3070 "%s(buffer does not allow write access)", func
);
3074 if (access
& GL_MAP_COHERENT_BIT
&&
3075 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
3076 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3077 "%s(buffer does not allow coherent access)", func
);
3081 if (access
& GL_MAP_PERSISTENT_BIT
&&
3082 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
3083 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3084 "%s(buffer does not allow persistent access)", func
);
3088 if (offset
+ length
> bufObj
->Size
) {
3089 _mesa_error(ctx
, GL_INVALID_VALUE
,
3090 "%s(offset %lu + length %lu > buffer_size %lu)", func
,
3091 (unsigned long) offset
, (unsigned long) length
,
3092 (unsigned long) bufObj
->Size
);
3096 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3097 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3098 "%s(buffer already mapped)", func
);
3102 if (access
& GL_MAP_WRITE_BIT
) {
3103 bufObj
->NumMapBufferWriteCalls
++;
3104 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
3105 bufObj
->Usage
== GL_STATIC_COPY
) &&
3106 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
3107 BUFFER_USAGE_WARNING(ctx
,
3108 "using %s(buffer %u, offset %u, length %u) to "
3109 "update a %s buffer",
3110 func
, bufObj
->Name
, offset
, length
,
3111 _mesa_enum_to_string(bufObj
->Usage
));
3119 map_buffer_range(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
3120 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
3123 if (!bufObj
->Size
) {
3124 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
3128 assert(ctx
->Driver
.MapBufferRange
);
3129 void *map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
3132 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
3135 /* The driver callback should have set all these fields.
3136 * This is important because other modules (like VBO) might call
3137 * the driver function directly.
3139 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
3140 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
3141 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
3142 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
3145 if (access
& GL_MAP_WRITE_BIT
) {
3146 bufObj
->Written
= GL_TRUE
;
3147 bufObj
->MinMaxCacheDirty
= true;
3151 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3152 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
3153 bufObj
->Name
, bufObj
->Size
, access
);
3154 /* Access must be write only */
3155 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
3157 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
3158 for (i
= 0; i
< bufObj
->Size
; i
++)
3165 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3166 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
3168 /* buffer is 100 bytes larger than requested, fill with magic value */
3169 for (i
= 0; i
< 100; i
++) {
3170 buf
[bufObj
->Size
- i
- 1] = 123;
3179 _mesa_MapBufferRange_no_error(GLenum target
, GLintptr offset
,
3180 GLsizeiptr length
, GLbitfield access
)
3182 GET_CURRENT_CONTEXT(ctx
);
3184 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3185 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3187 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3188 "glMapBufferRange");
3192 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
3195 GET_CURRENT_CONTEXT(ctx
);
3196 struct gl_buffer_object
*bufObj
;
3198 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3199 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3200 "glMapBufferRange(ARB_map_buffer_range not supported)");
3204 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
3208 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3209 "glMapBufferRange"))
3212 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3213 "glMapBufferRange");
3217 _mesa_MapNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3218 GLsizeiptr length
, GLbitfield access
)
3220 GET_CURRENT_CONTEXT(ctx
);
3221 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3223 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3224 "glMapNamedBufferRange");
3228 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3231 GET_CURRENT_CONTEXT(ctx
);
3232 struct gl_buffer_object
*bufObj
;
3234 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3235 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3236 "glMapNamedBufferRange("
3237 "ARB_map_buffer_range not supported)");
3241 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
3245 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3246 "glMapNamedBufferRange"))
3249 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3250 "glMapNamedBufferRange");
3254 * Converts GLenum access from MapBuffer and MapNamedBuffer into
3255 * flags for input to map_buffer_range.
3257 * \return true if the type of requested access is permissible.
3260 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
3264 case GL_READ_ONLY_ARB
:
3265 *flags
= GL_MAP_READ_BIT
;
3266 return _mesa_is_desktop_gl(ctx
);
3267 case GL_WRITE_ONLY_ARB
:
3268 *flags
= GL_MAP_WRITE_BIT
;
3270 case GL_READ_WRITE_ARB
:
3271 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
3272 return _mesa_is_desktop_gl(ctx
);
3280 _mesa_MapBuffer_no_error(GLenum target
, GLenum access
)
3282 GET_CURRENT_CONTEXT(ctx
);
3284 GLbitfield accessFlags
;
3285 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3287 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3288 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3290 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3295 _mesa_MapBuffer(GLenum target
, GLenum access
)
3297 GET_CURRENT_CONTEXT(ctx
);
3298 struct gl_buffer_object
*bufObj
;
3299 GLbitfield accessFlags
;
3301 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3302 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
3306 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
3310 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3314 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3319 _mesa_MapNamedBuffer_no_error(GLuint buffer
, GLenum access
)
3321 GET_CURRENT_CONTEXT(ctx
);
3323 GLbitfield accessFlags
;
3324 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3326 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3328 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3329 "glMapNamedBuffer");
3333 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
3335 GET_CURRENT_CONTEXT(ctx
);
3336 struct gl_buffer_object
*bufObj
;
3337 GLbitfield accessFlags
;
3339 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3340 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
3344 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
3348 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3349 "glMapNamedBuffer"))
3352 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3353 "glMapNamedBuffer");
3358 flush_mapped_buffer_range(struct gl_context
*ctx
,
3359 struct gl_buffer_object
*bufObj
,
3360 GLintptr offset
, GLsizeiptr length
,
3363 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3364 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3365 "%s(ARB_map_buffer_range not supported)", func
);
3370 _mesa_error(ctx
, GL_INVALID_VALUE
,
3371 "%s(offset %ld < 0)", func
, (long) offset
);
3376 _mesa_error(ctx
, GL_INVALID_VALUE
,
3377 "%s(length %ld < 0)", func
, (long) length
);
3381 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3382 /* buffer is not mapped */
3383 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3384 "%s(buffer is not mapped)", func
);
3388 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
3389 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
3390 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3391 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
3395 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
3396 _mesa_error(ctx
, GL_INVALID_VALUE
,
3397 "%s(offset %ld + length %ld > mapped length %ld)", func
,
3398 (long) offset
, (long) length
,
3399 (long) bufObj
->Mappings
[MAP_USER
].Length
);
3403 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
3405 if (ctx
->Driver
.FlushMappedBufferRange
)
3406 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3411 _mesa_FlushMappedBufferRange_no_error(GLenum target
, GLintptr offset
,
3414 GET_CURRENT_CONTEXT(ctx
);
3415 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3416 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3418 if (ctx
->Driver
.FlushMappedBufferRange
)
3419 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3424 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
3427 GET_CURRENT_CONTEXT(ctx
);
3428 struct gl_buffer_object
*bufObj
;
3430 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
3431 GL_INVALID_OPERATION
);
3435 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3436 "glFlushMappedBufferRange");
3440 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3443 GET_CURRENT_CONTEXT(ctx
);
3444 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3446 if (ctx
->Driver
.FlushMappedBufferRange
)
3447 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3452 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
3455 GET_CURRENT_CONTEXT(ctx
);
3456 struct gl_buffer_object
*bufObj
;
3458 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
3459 "glFlushMappedNamedBufferRange");
3463 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3464 "glFlushMappedNamedBufferRange");
3468 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
, GLuint index
,
3469 struct gl_buffer_object
*bufObj
,
3470 GLintptr offset
, GLsizeiptr size
)
3472 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3477 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3478 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3482 * Bind a region of a buffer object to a uniform block binding point.
3483 * \param index the uniform buffer binding point index
3484 * \param bufObj the buffer object
3485 * \param offset offset to the start of buffer object region
3486 * \param size size of the buffer object region
3489 bind_buffer_range_uniform_buffer_err(struct gl_context
*ctx
, GLuint index
,
3490 struct gl_buffer_object
*bufObj
,
3491 GLintptr offset
, GLsizeiptr size
)
3493 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3494 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3498 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3499 _mesa_error(ctx
, GL_INVALID_VALUE
,
3500 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3501 ctx
->Const
.UniformBufferOffsetAlignment
);
3505 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3509 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
3511 struct gl_buffer_object
*bufObj
,
3515 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3520 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3521 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3525 * Bind a region of a buffer object to a shader storage block binding point.
3526 * \param index the shader storage buffer binding point index
3527 * \param bufObj the buffer object
3528 * \param offset offset to the start of buffer object region
3529 * \param size size of the buffer object region
3532 bind_buffer_range_shader_storage_buffer_err(struct gl_context
*ctx
,
3534 struct gl_buffer_object
*bufObj
,
3535 GLintptr offset
, GLsizeiptr size
)
3537 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3538 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3542 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3543 _mesa_error(ctx
, GL_INVALID_VALUE
,
3544 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3545 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3549 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3553 bind_buffer_range_atomic_buffer(struct gl_context
*ctx
, GLuint index
,
3554 struct gl_buffer_object
*bufObj
,
3555 GLintptr offset
, GLsizeiptr size
)
3557 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3562 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3563 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3567 * Bind a region of a buffer object to an atomic storage block binding point.
3568 * \param index the shader storage buffer binding point index
3569 * \param bufObj the buffer object
3570 * \param offset offset to the start of buffer object region
3571 * \param size size of the buffer object region
3574 bind_buffer_range_atomic_buffer_err(struct gl_context
*ctx
,
3576 struct gl_buffer_object
*bufObj
,
3577 GLintptr offset
, GLsizeiptr size
)
3579 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3580 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3584 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3585 _mesa_error(ctx
, GL_INVALID_VALUE
,
3586 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3587 ATOMIC_COUNTER_SIZE
);
3591 bind_buffer_range_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
3595 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3597 const GLintptr
*offsets
,
3598 const GLsizeiptr
*sizes
)
3600 if (offsets
[index
] < 0) {
3601 /* The ARB_multi_bind spec says:
3603 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3604 * value in <offsets> is less than zero (per binding)."
3606 _mesa_error(ctx
, GL_INVALID_VALUE
,
3607 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3608 index
, (int64_t) offsets
[index
]);
3612 if (sizes
[index
] <= 0) {
3613 /* The ARB_multi_bind spec says:
3615 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3616 * value in <sizes> is less than or equal to zero (per binding)."
3618 _mesa_error(ctx
, GL_INVALID_VALUE
,
3619 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3620 index
, (int64_t) sizes
[index
]);
3628 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3629 GLuint first
, GLsizei count
,
3632 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3633 _mesa_error(ctx
, GL_INVALID_ENUM
,
3634 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3638 /* The ARB_multi_bind_spec says:
3640 * "An INVALID_OPERATION error is generated if <first> + <count> is
3641 * greater than the number of target-specific indexed binding points,
3642 * as described in section 6.7.1."
3644 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3645 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3646 "%s(first=%u + count=%d > the value of "
3647 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3648 caller
, first
, count
,
3649 ctx
->Const
.MaxUniformBufferBindings
);
3657 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3658 GLuint first
, GLsizei count
,
3661 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3662 _mesa_error(ctx
, GL_INVALID_ENUM
,
3663 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3667 /* The ARB_multi_bind_spec says:
3669 * "An INVALID_OPERATION error is generated if <first> + <count> is
3670 * greater than the number of target-specific indexed binding points,
3671 * as described in section 6.7.1."
3673 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3674 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3675 "%s(first=%u + count=%d > the value of "
3676 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3677 caller
, first
, count
,
3678 ctx
->Const
.MaxShaderStorageBufferBindings
);
3686 * Unbind all uniform buffers in the range
3687 * <first> through <first>+<count>-1
3690 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3692 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3694 for (int i
= 0; i
< count
; i
++)
3695 set_buffer_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3696 bufObj
, -1, -1, GL_TRUE
, 0);
3700 * Unbind all shader storage buffers in the range
3701 * <first> through <first>+<count>-1
3704 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3707 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3709 for (int i
= 0; i
< count
; i
++)
3710 set_buffer_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3711 bufObj
, -1, -1, GL_TRUE
, 0);
3715 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3716 const GLuint
*buffers
,
3718 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3721 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3724 /* Assume that at least one binding will be changed */
3725 FLUSH_VERTICES(ctx
, 0);
3726 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3729 /* The ARB_multi_bind spec says:
3731 * "If <buffers> is NULL, all bindings from <first> through
3732 * <first>+<count>-1 are reset to their unbound (zero) state.
3733 * In this case, the offsets and sizes associated with the
3734 * binding points are set to default values, ignoring
3735 * <offsets> and <sizes>."
3737 unbind_uniform_buffers(ctx
, first
, count
);
3741 /* Note that the error semantics for multi-bind commands differ from
3742 * those of other GL commands.
3744 * The Issues section in the ARB_multi_bind spec says:
3746 * "(11) Typically, OpenGL specifies that if an error is generated by a
3747 * command, that command has no effect. This is somewhat
3748 * unfortunate for multi-bind commands, because it would require a
3749 * first pass to scan the entire list of bound objects for errors
3750 * and then a second pass to actually perform the bindings.
3751 * Should we have different error semantics?
3753 * RESOLVED: Yes. In this specification, when the parameters for
3754 * one of the <count> binding points are invalid, that binding point
3755 * is not updated and an error will be generated. However, other
3756 * binding points in the same command will be updated if their
3757 * parameters are valid and no other error occurs."
3760 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3762 for (int i
= 0; i
< count
; i
++) {
3763 struct gl_buffer_binding
*binding
=
3764 &ctx
->UniformBufferBindings
[first
+ i
];
3765 GLintptr offset
= 0;
3766 GLsizeiptr size
= 0;
3769 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3772 /* The ARB_multi_bind spec says:
3774 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3775 * pair of values in <offsets> and <sizes> does not respectively
3776 * satisfy the constraints described for those parameters for the
3777 * specified target, as described in section 6.7.1 (per binding)."
3779 * Section 6.7.1 refers to table 6.5, which says:
3781 * "┌───────────────────────────────────────────────────────────────┐
3782 * │ Uniform buffer array bindings (see sec. 7.6) │
3783 * ├─────────────────────┬─────────────────────────────────────────┤
3785 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3786 * │ │ OFFSET_ALIGNMENT │
3788 * │ size restriction │ none │
3789 * └─────────────────────┴─────────────────────────────────────────┘"
3791 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3792 _mesa_error(ctx
, GL_INVALID_VALUE
,
3793 "glBindBuffersRange(offsets[%u]=%" PRId64
3794 " is misaligned; it must be a multiple of the value of "
3795 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3796 "target=GL_UNIFORM_BUFFER)",
3797 i
, (int64_t) offsets
[i
],
3798 ctx
->Const
.UniformBufferOffsetAlignment
);
3802 offset
= offsets
[i
];
3806 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
3807 binding
, offset
, size
, range
,
3808 USAGE_UNIFORM_BUFFER
);
3811 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3815 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3816 GLsizei count
, const GLuint
*buffers
,
3818 const GLintptr
*offsets
,
3819 const GLsizeiptr
*sizes
,
3822 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3825 /* Assume that at least one binding will be changed */
3826 FLUSH_VERTICES(ctx
, 0);
3827 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3830 /* The ARB_multi_bind spec says:
3832 * "If <buffers> is NULL, all bindings from <first> through
3833 * <first>+<count>-1 are reset to their unbound (zero) state.
3834 * In this case, the offsets and sizes associated with the
3835 * binding points are set to default values, ignoring
3836 * <offsets> and <sizes>."
3838 unbind_shader_storage_buffers(ctx
, first
, count
);
3842 /* Note that the error semantics for multi-bind commands differ from
3843 * those of other GL commands.
3845 * The Issues section in the ARB_multi_bind spec says:
3847 * "(11) Typically, OpenGL specifies that if an error is generated by a
3848 * command, that command has no effect. This is somewhat
3849 * unfortunate for multi-bind commands, because it would require a
3850 * first pass to scan the entire list of bound objects for errors
3851 * and then a second pass to actually perform the bindings.
3852 * Should we have different error semantics?
3854 * RESOLVED: Yes. In this specification, when the parameters for
3855 * one of the <count> binding points are invalid, that binding point
3856 * is not updated and an error will be generated. However, other
3857 * binding points in the same command will be updated if their
3858 * parameters are valid and no other error occurs."
3861 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3863 for (int i
= 0; i
< count
; i
++) {
3864 struct gl_buffer_binding
*binding
=
3865 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3866 GLintptr offset
= 0;
3867 GLsizeiptr size
= 0;
3870 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3873 /* The ARB_multi_bind spec says:
3875 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3876 * pair of values in <offsets> and <sizes> does not respectively
3877 * satisfy the constraints described for those parameters for the
3878 * specified target, as described in section 6.7.1 (per binding)."
3880 * Section 6.7.1 refers to table 6.5, which says:
3882 * "┌───────────────────────────────────────────────────────────────┐
3883 * │ Shader storage buffer array bindings (see sec. 7.8) │
3884 * ├─────────────────────┬─────────────────────────────────────────┤
3886 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3887 * │ │ BUFFER_OFFSET_ALIGNMENT │
3889 * │ size restriction │ none │
3890 * └─────────────────────┴─────────────────────────────────────────┘"
3892 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3893 _mesa_error(ctx
, GL_INVALID_VALUE
,
3894 "glBindBuffersRange(offsets[%u]=%" PRId64
3895 " is misaligned; it must be a multiple of the value of "
3896 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3897 "target=GL_SHADER_STORAGE_BUFFER)",
3898 i
, (int64_t) offsets
[i
],
3899 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3903 offset
= offsets
[i
];
3907 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
3908 binding
, offset
, size
, range
,
3909 USAGE_SHADER_STORAGE_BUFFER
);
3912 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3916 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3917 struct gl_transform_feedback_object
*tfObj
,
3918 GLuint first
, GLsizei count
, const char *caller
)
3920 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3921 _mesa_error(ctx
, GL_INVALID_ENUM
,
3922 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3926 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3928 * "An INVALID_OPERATION error is generated :
3931 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3932 * FEEDBACK_BUFFER and transform feedback is currently active."
3934 * We assume that this is also meant to apply to BindBuffersRange
3935 * and BindBuffersBase.
3937 if (tfObj
->Active
) {
3938 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3939 "%s(Changing transform feedback buffers while "
3940 "transform feedback is active)", caller
);
3944 /* The ARB_multi_bind_spec says:
3946 * "An INVALID_OPERATION error is generated if <first> + <count> is
3947 * greater than the number of target-specific indexed binding points,
3948 * as described in section 6.7.1."
3950 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3951 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3952 "%s(first=%u + count=%d > the value of "
3953 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3954 caller
, first
, count
,
3955 ctx
->Const
.MaxTransformFeedbackBuffers
);
3963 * Unbind all transform feedback buffers in the range
3964 * <first> through <first>+<count>-1
3967 unbind_xfb_buffers(struct gl_context
*ctx
,
3968 struct gl_transform_feedback_object
*tfObj
,
3969 GLuint first
, GLsizei count
)
3971 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3973 for (int i
= 0; i
< count
; i
++)
3974 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3979 bind_xfb_buffers(struct gl_context
*ctx
,
3980 GLuint first
, GLsizei count
,
3981 const GLuint
*buffers
,
3983 const GLintptr
*offsets
,
3984 const GLsizeiptr
*sizes
,
3987 struct gl_transform_feedback_object
*tfObj
=
3988 ctx
->TransformFeedback
.CurrentObject
;
3990 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
3993 /* Assume that at least one binding will be changed */
3994 FLUSH_VERTICES(ctx
, 0);
3995 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3998 /* The ARB_multi_bind spec says:
4000 * "If <buffers> is NULL, all bindings from <first> through
4001 * <first>+<count>-1 are reset to their unbound (zero) state.
4002 * In this case, the offsets and sizes associated with the
4003 * binding points are set to default values, ignoring
4004 * <offsets> and <sizes>."
4006 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
4010 /* Note that the error semantics for multi-bind commands differ from
4011 * those of other GL commands.
4013 * The Issues section in the ARB_multi_bind spec says:
4015 * "(11) Typically, OpenGL specifies that if an error is generated by a
4016 * command, that command has no effect. This is somewhat
4017 * unfortunate for multi-bind commands, because it would require a
4018 * first pass to scan the entire list of bound objects for errors
4019 * and then a second pass to actually perform the bindings.
4020 * Should we have different error semantics?
4022 * RESOLVED: Yes. In this specification, when the parameters for
4023 * one of the <count> binding points are invalid, that binding point
4024 * is not updated and an error will be generated. However, other
4025 * binding points in the same command will be updated if their
4026 * parameters are valid and no other error occurs."
4029 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4031 for (int i
= 0; i
< count
; i
++) {
4032 const GLuint index
= first
+ i
;
4033 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
4034 struct gl_buffer_object
*bufObj
;
4035 GLintptr offset
= 0;
4036 GLsizeiptr size
= 0;
4039 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4042 /* The ARB_multi_bind spec says:
4044 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4045 * pair of values in <offsets> and <sizes> does not respectively
4046 * satisfy the constraints described for those parameters for the
4047 * specified target, as described in section 6.7.1 (per binding)."
4049 * Section 6.7.1 refers to table 6.5, which says:
4051 * "┌───────────────────────────────────────────────────────────────┐
4052 * │ Transform feedback array bindings (see sec. 13.2.2) │
4053 * ├───────────────────────┬───────────────────────────────────────┤
4055 * │ offset restriction │ multiple of 4 │
4057 * │ size restriction │ multiple of 4 │
4058 * └───────────────────────┴───────────────────────────────────────┘"
4060 if (offsets
[i
] & 0x3) {
4061 _mesa_error(ctx
, GL_INVALID_VALUE
,
4062 "glBindBuffersRange(offsets[%u]=%" PRId64
4063 " is misaligned; it must be a multiple of 4 when "
4064 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4065 i
, (int64_t) offsets
[i
]);
4069 if (sizes
[i
] & 0x3) {
4070 _mesa_error(ctx
, GL_INVALID_VALUE
,
4071 "glBindBuffersRange(sizes[%u]=%" PRId64
4072 " is misaligned; it must be a multiple of 4 when "
4073 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4074 i
, (int64_t) sizes
[i
]);
4078 offset
= offsets
[i
];
4082 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
4083 bufObj
= boundBufObj
;
4085 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
4088 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
4092 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4096 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
4097 GLuint first
, GLsizei count
,
4100 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
4101 _mesa_error(ctx
, GL_INVALID_ENUM
,
4102 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
4106 /* The ARB_multi_bind_spec says:
4108 * "An INVALID_OPERATION error is generated if <first> + <count> is
4109 * greater than the number of target-specific indexed binding points,
4110 * as described in section 6.7.1."
4112 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
4113 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4114 "%s(first=%u + count=%d > the value of "
4115 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
4116 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
4124 * Unbind all atomic counter buffers in the range
4125 * <first> through <first>+<count>-1
4128 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
4130 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
4132 for (int i
= 0; i
< count
; i
++)
4133 set_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
4134 bufObj
, -1, -1, GL_TRUE
, 0);
4138 bind_atomic_buffers(struct gl_context
*ctx
,
4141 const GLuint
*buffers
,
4143 const GLintptr
*offsets
,
4144 const GLsizeiptr
*sizes
,
4147 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
4150 /* Assume that at least one binding will be changed */
4151 FLUSH_VERTICES(ctx
, 0);
4152 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
4155 /* The ARB_multi_bind spec says:
4157 * "If <buffers> is NULL, all bindings from <first> through
4158 * <first>+<count>-1 are reset to their unbound (zero) state.
4159 * In this case, the offsets and sizes associated with the
4160 * binding points are set to default values, ignoring
4161 * <offsets> and <sizes>."
4163 unbind_atomic_buffers(ctx
, first
, count
);
4167 /* Note that the error semantics for multi-bind commands differ from
4168 * those of other GL commands.
4170 * The Issues section in the ARB_multi_bind spec says:
4172 * "(11) Typically, OpenGL specifies that if an error is generated by a
4173 * command, that command has no effect. This is somewhat
4174 * unfortunate for multi-bind commands, because it would require a
4175 * first pass to scan the entire list of bound objects for errors
4176 * and then a second pass to actually perform the bindings.
4177 * Should we have different error semantics?
4179 * RESOLVED: Yes. In this specification, when the parameters for
4180 * one of the <count> binding points are invalid, that binding point
4181 * is not updated and an error will be generated. However, other
4182 * binding points in the same command will be updated if their
4183 * parameters are valid and no other error occurs."
4186 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4188 for (int i
= 0; i
< count
; i
++) {
4189 struct gl_buffer_binding
*binding
=
4190 &ctx
->AtomicBufferBindings
[first
+ i
];
4191 GLintptr offset
= 0;
4192 GLsizeiptr size
= 0;
4195 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4198 /* The ARB_multi_bind spec says:
4200 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4201 * pair of values in <offsets> and <sizes> does not respectively
4202 * satisfy the constraints described for those parameters for the
4203 * specified target, as described in section 6.7.1 (per binding)."
4205 * Section 6.7.1 refers to table 6.5, which says:
4207 * "┌───────────────────────────────────────────────────────────────┐
4208 * │ Atomic counter array bindings (see sec. 7.7.2) │
4209 * ├───────────────────────┬───────────────────────────────────────┤
4211 * │ offset restriction │ multiple of 4 │
4213 * │ size restriction │ none │
4214 * └───────────────────────┴───────────────────────────────────────┘"
4216 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
4217 _mesa_error(ctx
, GL_INVALID_VALUE
,
4218 "glBindBuffersRange(offsets[%u]=%" PRId64
4219 " is misaligned; it must be a multiple of %d when "
4220 "target=GL_ATOMIC_COUNTER_BUFFER)",
4221 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
4225 offset
= offsets
[i
];
4229 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
4230 binding
, offset
, size
, range
,
4231 USAGE_ATOMIC_COUNTER_BUFFER
);
4234 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4237 static ALWAYS_INLINE
void
4238 bind_buffer_range(GLenum target
, GLuint index
, GLuint buffer
, GLintptr offset
,
4239 GLsizeiptr size
, bool no_error
)
4241 GET_CURRENT_CONTEXT(ctx
);
4242 struct gl_buffer_object
*bufObj
;
4244 if (MESA_VERBOSE
& VERBOSE_API
) {
4245 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
4246 _mesa_enum_to_string(target
), index
, buffer
,
4247 (unsigned long) offset
, (unsigned long) size
);
4251 bufObj
= ctx
->Shared
->NullBufferObj
;
4253 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4254 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4255 &bufObj
, "glBindBufferRange"))
4258 if (!no_error
&& !bufObj
) {
4259 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4260 "glBindBufferRange(invalid buffer=%u)", buffer
);
4267 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4268 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4269 index
, bufObj
, offset
, size
);
4271 case GL_UNIFORM_BUFFER
:
4272 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
4274 case GL_SHADER_STORAGE_BUFFER
:
4275 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
,
4278 case GL_ATOMIC_COUNTER_BUFFER
:
4279 bind_buffer_range_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
4282 unreachable("invalid BindBufferRange target with KHR_no_error");
4287 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
4294 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4295 if (!_mesa_validate_buffer_range_xfb(ctx
,
4296 ctx
->TransformFeedback
.CurrentObject
,
4297 index
, bufObj
, offset
, size
,
4301 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4302 index
, bufObj
, offset
, size
);
4304 case GL_UNIFORM_BUFFER
:
4305 bind_buffer_range_uniform_buffer_err(ctx
, index
, bufObj
, offset
,
4308 case GL_SHADER_STORAGE_BUFFER
:
4309 bind_buffer_range_shader_storage_buffer_err(ctx
, index
, bufObj
,
4312 case GL_ATOMIC_COUNTER_BUFFER
:
4313 bind_buffer_range_atomic_buffer_err(ctx
, index
, bufObj
,
4317 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
4324 _mesa_BindBufferRange_no_error(GLenum target
, GLuint index
, GLuint buffer
,
4325 GLintptr offset
, GLsizeiptr size
)
4327 bind_buffer_range(target
, index
, buffer
, offset
, size
, true);
4331 _mesa_BindBufferRange(GLenum target
, GLuint index
,
4332 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
4334 bind_buffer_range(target
, index
, buffer
, offset
, size
, false);
4338 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
4340 GET_CURRENT_CONTEXT(ctx
);
4341 struct gl_buffer_object
*bufObj
;
4343 if (MESA_VERBOSE
& VERBOSE_API
) {
4344 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
4345 _mesa_enum_to_string(target
), index
, buffer
);
4349 bufObj
= ctx
->Shared
->NullBufferObj
;
4351 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4352 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4353 &bufObj
, "glBindBufferBase"))
4357 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4358 "glBindBufferBase(invalid buffer=%u)", buffer
);
4363 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4364 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4366 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4367 * zero and size equal to the size of buffer."
4369 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4371 * "If the parameter (starting offset or size) was not specified when the
4372 * buffer object was bound, zero is returned."
4374 * What happens if the size of the buffer changes? Does the size of the
4375 * buffer at the moment glBindBufferBase was called still play a role, like
4376 * the first quote would imply, or is the size meaningless in the
4377 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4378 * core spec page 45 says:
4380 * "It is equivalent to calling BindBufferRange with offset zero, while
4381 * size is determined by the size of the bound buffer at the time the
4384 * My interpretation is that the GL 4.1 spec was a clarification of the
4385 * behavior, not a change. In particular, this choice will only make
4386 * rendering work in cases where it would have had undefined results.
4390 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4391 _mesa_bind_buffer_base_transform_feedback(ctx
,
4392 ctx
->TransformFeedback
.CurrentObject
,
4393 index
, bufObj
, false);
4395 case GL_UNIFORM_BUFFER
:
4396 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4398 case GL_SHADER_STORAGE_BUFFER
:
4399 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4401 case GL_ATOMIC_COUNTER_BUFFER
:
4402 bind_buffer_base_atomic_buffer(ctx
, index
, bufObj
);
4405 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4411 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4412 const GLuint
*buffers
,
4413 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4415 GET_CURRENT_CONTEXT(ctx
);
4417 if (MESA_VERBOSE
& VERBOSE_API
) {
4418 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4419 _mesa_enum_to_string(target
), first
, count
,
4420 buffers
, offsets
, sizes
);
4424 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4425 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4426 "glBindBuffersRange");
4428 case GL_UNIFORM_BUFFER
:
4429 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4430 "glBindBuffersRange");
4432 case GL_SHADER_STORAGE_BUFFER
:
4433 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4434 "glBindBuffersRange");
4436 case GL_ATOMIC_COUNTER_BUFFER
:
4437 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4438 "glBindBuffersRange");
4441 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4442 _mesa_enum_to_string(target
));
4448 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4449 const GLuint
*buffers
)
4451 GET_CURRENT_CONTEXT(ctx
);
4453 if (MESA_VERBOSE
& VERBOSE_API
) {
4454 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
4455 _mesa_enum_to_string(target
), first
, count
, buffers
);
4459 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4460 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4461 "glBindBuffersBase");
4463 case GL_UNIFORM_BUFFER
:
4464 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4465 "glBindBuffersBase");
4467 case GL_SHADER_STORAGE_BUFFER
:
4468 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4469 "glBindBuffersBase");
4471 case GL_ATOMIC_COUNTER_BUFFER
:
4472 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4473 "glBindBuffersBase");
4476 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4477 _mesa_enum_to_string(target
));
4482 static ALWAYS_INLINE
void
4483 invalidate_buffer_subdata(struct gl_context
*ctx
,
4484 struct gl_buffer_object
*bufObj
, GLintptr offset
,
4487 if (ctx
->Driver
.InvalidateBufferSubData
)
4488 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4492 _mesa_InvalidateBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
4495 GET_CURRENT_CONTEXT(ctx
);
4497 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4498 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4502 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4505 GET_CURRENT_CONTEXT(ctx
);
4506 struct gl_buffer_object
*bufObj
;
4507 const GLintptr end
= offset
+ length
;
4509 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4510 * Profile) spec says:
4512 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4513 * name of an existing buffer object."
4515 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4516 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4517 _mesa_error(ctx
, GL_INVALID_VALUE
,
4518 "glInvalidateBufferSubData(name = %u) invalid object",
4523 /* The GL_ARB_invalidate_subdata spec says:
4525 * "An INVALID_VALUE error is generated if <offset> or <length> is
4526 * negative, or if <offset> + <length> is greater than the value of
4529 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
4530 _mesa_error(ctx
, GL_INVALID_VALUE
,
4531 "glInvalidateBufferSubData(invalid offset or length)");
4535 /* The OpenGL 4.4 (Core Profile) spec says:
4537 * "An INVALID_OPERATION error is generated if buffer is currently
4538 * mapped by MapBuffer or if the invalidate range intersects the range
4539 * currently mapped by MapBufferRange, unless it was mapped
4540 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4542 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4543 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4544 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4545 "glInvalidateBufferSubData(intersection with mapped "
4550 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4554 _mesa_InvalidateBufferData_no_error(GLuint buffer
)
4556 GET_CURRENT_CONTEXT(ctx
);
4558 struct gl_buffer_object
*bufObj
=_mesa_lookup_bufferobj(ctx
, buffer
);
4559 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4563 _mesa_InvalidateBufferData(GLuint buffer
)
4565 GET_CURRENT_CONTEXT(ctx
);
4566 struct gl_buffer_object
*bufObj
;
4568 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4569 * Profile) spec says:
4571 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4572 * name of an existing buffer object."
4574 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4575 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4576 _mesa_error(ctx
, GL_INVALID_VALUE
,
4577 "glInvalidateBufferData(name = %u) invalid object",
4582 /* The OpenGL 4.4 (Core Profile) spec says:
4584 * "An INVALID_OPERATION error is generated if buffer is currently
4585 * mapped by MapBuffer or if the invalidate range intersects the range
4586 * currently mapped by MapBufferRange, unless it was mapped
4587 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4589 if (_mesa_check_disallowed_mapping(bufObj
)) {
4590 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4591 "glInvalidateBufferData(intersection with mapped "
4596 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4600 buffer_page_commitment(struct gl_context
*ctx
,
4601 struct gl_buffer_object
*bufferObj
,
4602 GLintptr offset
, GLsizeiptr size
,
4603 GLboolean commit
, const char *func
)
4605 if (!(bufferObj
->StorageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)) {
4606 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(not a sparse buffer object)",
4611 if (size
< 0 || size
> bufferObj
->Size
||
4612 offset
< 0 || offset
> bufferObj
->Size
- size
) {
4613 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(out of bounds)",
4618 /* The GL_ARB_sparse_buffer extension specification says:
4620 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4621 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4622 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4623 * not extend to the end of the buffer's data store."
4625 if (offset
% ctx
->Const
.SparseBufferPageSize
!= 0) {
4626 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset not aligned to page size)",
4631 if (size
% ctx
->Const
.SparseBufferPageSize
!= 0 &&
4632 offset
+ size
!= bufferObj
->Size
) {
4633 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size not aligned to page size)",
4638 ctx
->Driver
.BufferPageCommitment(ctx
, bufferObj
, offset
, size
, commit
);
4642 _mesa_BufferPageCommitmentARB(GLenum target
, GLintptr offset
, GLsizeiptr size
,
4645 GET_CURRENT_CONTEXT(ctx
);
4646 struct gl_buffer_object
*bufferObj
;
4648 bufferObj
= get_buffer(ctx
, "glBufferPageCommitmentARB", target
,
4653 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4654 "glBufferPageCommitmentARB");
4658 _mesa_NamedBufferPageCommitmentARB(GLuint buffer
, GLintptr offset
,
4659 GLsizeiptr size
, GLboolean commit
)
4661 GET_CURRENT_CONTEXT(ctx
);
4662 struct gl_buffer_object
*bufferObj
;
4664 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4665 if (!bufferObj
|| bufferObj
== &DummyBufferObject
) {
4666 /* Note: the extension spec is not clear about the excpected error value. */
4667 _mesa_error(ctx
, GL_INVALID_VALUE
,
4668 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4673 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4674 "glNamedBufferPageCommitmentARB");