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"
53 /*#define BOUNDS_CHECK*/
57 * We count the number of buffer modification calls to check for
58 * inefficient buffer use. This is the number of such calls before we
61 #define BUFFER_WARNING_CALL_COUNT 4
65 * Helper to warn of possible performance issues, such as frequently
66 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
70 buffer_usage_warning(struct gl_context
*ctx
, GLuint
*id
, const char *fmt
, ...)
75 _mesa_gl_vdebug(ctx
, id
,
76 MESA_DEBUG_SOURCE_API
,
77 MESA_DEBUG_TYPE_PERFORMANCE
,
78 MESA_DEBUG_SEVERITY_MEDIUM
,
83 #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
85 static GLuint id = 0; \
86 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
91 * Used as a placeholder for buffer objects between glGenBuffers() and
92 * glBindBuffer() so that glIsBuffer() can work correctly.
94 static struct gl_buffer_object DummyBufferObject
;
98 * Return pointer to address of a buffer object target.
99 * \param ctx the GL context
100 * \param target the buffer object target to be retrieved.
101 * \return pointer to pointer to the buffer object bound to \c target in the
102 * specified context or \c NULL if \c target is invalid.
104 static inline struct gl_buffer_object
**
105 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
107 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
109 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
110 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
114 case GL_ARRAY_BUFFER_ARB
:
115 return &ctx
->Array
.ArrayBufferObj
;
116 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
117 return &ctx
->Array
.VAO
->IndexBufferObj
;
118 case GL_PIXEL_PACK_BUFFER_EXT
:
119 return &ctx
->Pack
.BufferObj
;
120 case GL_PIXEL_UNPACK_BUFFER_EXT
:
121 return &ctx
->Unpack
.BufferObj
;
122 case GL_COPY_READ_BUFFER
:
123 return &ctx
->CopyReadBuffer
;
124 case GL_COPY_WRITE_BUFFER
:
125 return &ctx
->CopyWriteBuffer
;
126 case GL_QUERY_BUFFER
:
127 if (_mesa_has_ARB_query_buffer_object(ctx
))
128 return &ctx
->QueryBuffer
;
130 case GL_DRAW_INDIRECT_BUFFER
:
131 if ((ctx
->API
== API_OPENGL_CORE
&&
132 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 mtx_destroy(&bufObj
->Mutex
);
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 GLboolean deleteFlag
= GL_FALSE
;
494 struct gl_buffer_object
*oldObj
= *ptr
;
496 mtx_lock(&oldObj
->Mutex
);
497 assert(oldObj
->RefCount
> 0);
499 deleteFlag
= (oldObj
->RefCount
== 0);
500 mtx_unlock(&oldObj
->Mutex
);
503 assert(ctx
->Driver
.DeleteBuffer
);
504 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
512 /* reference new buffer */
513 mtx_lock(&bufObj
->Mutex
);
514 assert(bufObj
->RefCount
> 0);
518 mtx_unlock(&bufObj
->Mutex
);
524 * Get the value of MESA_NO_MINMAX_CACHE.
527 get_no_minmax_cache()
529 static bool read
= false;
530 static bool disable
= false;
533 disable
= env_var_as_boolean("MESA_NO_MINMAX_CACHE", false);
542 * Initialize a buffer object to default values.
545 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
546 struct gl_buffer_object
*obj
,
549 memset(obj
, 0, sizeof(struct gl_buffer_object
));
550 mtx_init(&obj
->Mutex
, mtx_plain
);
553 obj
->Usage
= GL_STATIC_DRAW_ARB
;
555 if (get_no_minmax_cache())
556 obj
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
562 * Callback called from _mesa_HashWalk()
565 count_buffer_size(GLuint key
, void *data
, void *userData
)
567 const struct gl_buffer_object
*bufObj
=
568 (const struct gl_buffer_object
*) data
;
569 GLuint
*total
= (GLuint
*) userData
;
572 *total
= *total
+ bufObj
->Size
;
577 * Compute total size (in bytes) of all buffer objects for the given context.
578 * For debugging purposes.
581 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
585 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
592 * Allocate space for and store data in a buffer object. Any data that was
593 * previously stored in the buffer object is lost. If \c data is \c NULL,
594 * memory will be allocated, but no copy will occur.
596 * This is the default callback for \c dd_function_table::BufferData()
597 * Note that all GL error checking will have been done already.
599 * \param ctx GL context.
600 * \param target Buffer object target on which to operate.
601 * \param size Size, in bytes, of the new data store.
602 * \param data Pointer to the data to store in the buffer object. This
603 * pointer may be \c NULL.
604 * \param usage Hints about how the data will be used.
605 * \param bufObj Object to be used.
607 * \return GL_TRUE for success, GL_FALSE for failure
608 * \sa glBufferDataARB, dd_function_table::BufferData.
611 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptr size
,
612 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
613 struct gl_buffer_object
*bufObj
)
619 _mesa_align_free( bufObj
->Data
);
621 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
623 bufObj
->Data
= (GLubyte
*) new_data
;
625 bufObj
->Usage
= usage
;
626 bufObj
->StorageFlags
= storageFlags
;
629 memcpy( bufObj
->Data
, data
, size
);
641 * Replace data in a subrange of buffer object. If the data range
642 * specified by \c size + \c offset extends beyond the end of the buffer or
643 * if \c data is \c NULL, no copy is performed.
645 * This is the default callback for \c dd_function_table::BufferSubData()
646 * Note that all GL error checking will have been done already.
648 * \param ctx GL context.
649 * \param offset Offset of the first byte to be modified.
650 * \param size Size, in bytes, of the data range.
651 * \param data Pointer to the data to store in the buffer object.
652 * \param bufObj Object to be used.
654 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
657 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptr offset
,
658 GLsizeiptr size
, const GLvoid
*data
,
659 struct gl_buffer_object
*bufObj
)
663 /* this should have been caught in _mesa_BufferSubData() */
664 assert(size
+ offset
<= bufObj
->Size
);
667 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
673 * Retrieve data from a subrange of buffer object. If the data range
674 * specified by \c size + \c offset extends beyond the end of the buffer or
675 * if \c data is \c NULL, no copy is performed.
677 * This is the default callback for \c dd_function_table::GetBufferSubData()
678 * Note that all GL error checking will have been done already.
680 * \param ctx GL context.
681 * \param target Buffer object target on which to operate.
682 * \param offset Offset of the first byte to be fetched.
683 * \param size Size, in bytes, of the data range.
684 * \param data Destination for data
685 * \param bufObj Object to be used.
687 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
690 buffer_get_subdata(struct gl_context
*ctx
, GLintptrARB offset
,
691 GLsizeiptrARB size
, GLvoid
*data
,
692 struct gl_buffer_object
*bufObj
)
696 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
697 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
703 * Clear a subrange of the buffer object with copies of the supplied data.
704 * If data is NULL the buffer is filled with zeros.
706 * This is the default callback for \c dd_function_table::ClearBufferSubData()
707 * Note that all GL error checking will have been done already.
709 * \param ctx GL context.
710 * \param offset Offset of the first byte to be cleared.
711 * \param size Size, in bytes, of the to be cleared range.
712 * \param clearValue Source of the data.
713 * \param clearValueSize Size, in bytes, of the supplied data.
714 * \param bufObj Object to be cleared.
716 * \sa glClearBufferSubData, glClearBufferData and
717 * dd_function_table::ClearBufferSubData.
720 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
721 GLintptr offset
, GLsizeiptr size
,
722 const GLvoid
*clearValue
,
723 GLsizeiptr clearValueSize
,
724 struct gl_buffer_object
*bufObj
)
729 assert(ctx
->Driver
.MapBufferRange
);
730 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
732 GL_MAP_INVALIDATE_RANGE_BIT
,
733 bufObj
, MAP_INTERNAL
);
736 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
740 if (clearValue
== NULL
) {
741 /* Clear with zeros, per the spec */
742 memset(dest
, 0, size
);
743 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
747 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
748 memcpy(dest
, clearValue
, clearValueSize
);
749 dest
+= clearValueSize
;
752 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
757 * Default fallback for \c dd_function_table::MapBufferRange().
758 * Called via glMapBufferRange().
761 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
762 GLsizeiptr length
, GLbitfield access
,
763 struct gl_buffer_object
*bufObj
,
764 gl_map_buffer_index index
)
767 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
768 /* Just return a direct pointer to the data */
769 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
770 bufObj
->Mappings
[index
].Length
= length
;
771 bufObj
->Mappings
[index
].Offset
= offset
;
772 bufObj
->Mappings
[index
].AccessFlags
= access
;
773 return bufObj
->Mappings
[index
].Pointer
;
778 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
779 * Called via glFlushMappedBufferRange().
782 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
783 GLintptr offset
, GLsizeiptr length
,
784 struct gl_buffer_object
*obj
,
785 gl_map_buffer_index index
)
797 * Default callback for \c dd_function_table::UnmapBuffer().
799 * The input parameters will have been already tested for errors.
801 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
804 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
805 gl_map_buffer_index index
)
808 /* XXX we might assert here that bufObj->Pointer is non-null */
809 bufObj
->Mappings
[index
].Pointer
= NULL
;
810 bufObj
->Mappings
[index
].Length
= 0;
811 bufObj
->Mappings
[index
].Offset
= 0;
812 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
818 * Default fallback for \c dd_function_table::CopyBufferSubData().
819 * Called via glCopyBufferSubData().
822 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
823 struct gl_buffer_object
*src
,
824 struct gl_buffer_object
*dst
,
825 GLintptr readOffset
, GLintptr writeOffset
,
828 GLubyte
*srcPtr
, *dstPtr
;
831 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
833 GL_MAP_WRITE_BIT
, src
,
839 srcPtr
+= readOffset
;
840 dstPtr
+= writeOffset
;
842 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
843 GL_MAP_READ_BIT
, src
,
845 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
847 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
851 /* Note: the src and dst regions will never overlap. Trying to do so
852 * would generate GL_INVALID_VALUE earlier.
854 if (srcPtr
&& dstPtr
)
855 memcpy(dstPtr
, srcPtr
, size
);
857 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
859 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
865 * Initialize the state associated with buffer objects
868 _mesa_init_buffer_objects( struct gl_context
*ctx
)
872 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
873 mtx_init(&DummyBufferObject
.Mutex
, mtx_plain
);
874 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
876 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
877 ctx
->Shared
->NullBufferObj
);
879 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
880 ctx
->Shared
->NullBufferObj
);
881 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
882 ctx
->Shared
->NullBufferObj
);
884 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
885 ctx
->Shared
->NullBufferObj
);
887 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
,
888 ctx
->Shared
->NullBufferObj
);
890 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
891 ctx
->Shared
->NullBufferObj
);
893 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
894 ctx
->Shared
->NullBufferObj
);
896 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
,
897 ctx
->Shared
->NullBufferObj
);
899 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
,
900 ctx
->Shared
->NullBufferObj
);
902 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
,
903 ctx
->Shared
->NullBufferObj
);
905 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
906 _mesa_reference_buffer_object(ctx
,
907 &ctx
->UniformBufferBindings
[i
].BufferObject
,
908 ctx
->Shared
->NullBufferObj
);
909 ctx
->UniformBufferBindings
[i
].Offset
= -1;
910 ctx
->UniformBufferBindings
[i
].Size
= -1;
913 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
914 _mesa_reference_buffer_object(ctx
,
915 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
916 ctx
->Shared
->NullBufferObj
);
917 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
918 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
921 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
922 _mesa_reference_buffer_object(ctx
,
923 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
924 ctx
->Shared
->NullBufferObj
);
925 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
926 ctx
->AtomicBufferBindings
[i
].Size
= 0;
932 _mesa_free_buffer_objects( struct gl_context
*ctx
)
936 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
938 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
939 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
941 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
943 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
945 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
947 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
949 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
, NULL
);
951 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, NULL
);
953 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
, NULL
);
955 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
956 _mesa_reference_buffer_object(ctx
,
957 &ctx
->UniformBufferBindings
[i
].BufferObject
,
961 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
962 _mesa_reference_buffer_object(ctx
,
963 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
967 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
968 _mesa_reference_buffer_object(ctx
,
969 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
976 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
978 struct gl_buffer_object
**buf_handle
,
981 struct gl_buffer_object
*buf
= *buf_handle
;
983 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
)) {
984 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
988 if (!buf
|| buf
== &DummyBufferObject
) {
989 /* If this is a new buffer object id, or one which was generated but
990 * never used before, allocate a buffer object now.
992 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
994 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
997 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
1005 * Bind the specified target to buffer for the specified context.
1006 * Called by glBindBuffer() and other functions.
1009 bind_buffer_object(struct gl_context
*ctx
,
1010 struct gl_buffer_object
**bindTarget
, GLuint buffer
)
1012 struct gl_buffer_object
*oldBufObj
;
1013 struct gl_buffer_object
*newBufObj
= NULL
;
1017 /* Get pointer to old buffer object (to be unbound) */
1018 oldBufObj
= *bindTarget
;
1019 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
1020 return; /* rebinding the same buffer object- no change */
1023 * Get pointer to new buffer object (newBufObj)
1026 /* The spec says there's not a buffer object named 0, but we use
1027 * one internally because it simplifies things.
1029 newBufObj
= ctx
->Shared
->NullBufferObj
;
1032 /* non-default buffer object */
1033 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1034 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1035 &newBufObj
, "glBindBuffer"))
1039 /* record usage history */
1040 if (bindTarget
== &ctx
->Pack
.BufferObj
) {
1041 newBufObj
->UsageHistory
|= USAGE_PIXEL_PACK_BUFFER
;
1044 /* bind new buffer */
1045 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1050 * Update the default buffer objects in the given context to reference those
1051 * specified in the shared state and release those referencing the old
1055 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1057 /* Bind the NullBufferObj to remove references to those
1058 * in the shared context hash table.
1060 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1061 bind_buffer_object(ctx
, &ctx
->Array
.VAO
->IndexBufferObj
, 0);
1062 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1063 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1069 * Return the gl_buffer_object for the given ID.
1070 * Always return NULL for ID 0.
1072 struct gl_buffer_object
*
1073 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1078 return (struct gl_buffer_object
*)
1079 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1083 struct gl_buffer_object
*
1084 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1089 return (struct gl_buffer_object
*)
1090 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1094 * A convenience function for direct state access functions that throws
1095 * GL_INVALID_OPERATION if buffer is not the name of an existing
1098 struct gl_buffer_object
*
1099 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1102 struct gl_buffer_object
*bufObj
;
1104 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1105 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1106 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1107 "%s(non-existent buffer object %u)", caller
, buffer
);
1116 * Look up a buffer object for a multi-bind function.
1118 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1119 * of generating an error if the buffer ID is not zero or the name
1120 * of an existing buffer object.
1122 * If the buffer ID refers to an existing buffer object, a pointer
1123 * to the buffer object is returned. If the ID is zero, a pointer
1124 * to the shared NullBufferObj is returned. If the ID is not zero
1125 * and does not refer to a valid buffer object, this function
1128 * This function assumes that the caller has already locked the
1129 * hash table mutex by calling
1130 * _mesa_HashLockMutex(ctx->Shared->BufferObjects).
1132 struct gl_buffer_object
*
1133 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1134 const GLuint
*buffers
,
1135 GLuint index
, const char *caller
)
1137 struct gl_buffer_object
*bufObj
;
1139 if (buffers
[index
] != 0) {
1140 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1142 /* The multi-bind functions don't create the buffer objects
1143 when they don't exist. */
1144 if (bufObj
== &DummyBufferObject
)
1147 bufObj
= ctx
->Shared
->NullBufferObj
;
1150 /* The ARB_multi_bind spec says:
1152 * "An INVALID_OPERATION error is generated if any value
1153 * in <buffers> is not zero or the name of an existing
1154 * buffer object (per binding)."
1156 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1157 "%s(buffers[%u]=%u is not zero or the name "
1158 "of an existing buffer object)",
1159 caller
, index
, buffers
[index
]);
1167 * If *ptr points to obj, set ptr = the Null/default buffer object.
1168 * This is a helper for buffer object deletion.
1169 * The GL spec says that deleting a buffer object causes it to get
1170 * unbound from all arrays in the current context.
1173 unbind(struct gl_context
*ctx
,
1174 struct gl_vertex_array_object
*vao
, unsigned index
,
1175 struct gl_buffer_object
*obj
)
1177 if (vao
->BufferBinding
[index
].BufferObj
== obj
) {
1178 _mesa_bind_vertex_buffer(ctx
, vao
, index
, ctx
->Shared
->NullBufferObj
,
1179 vao
->BufferBinding
[index
].Offset
,
1180 vao
->BufferBinding
[index
].Stride
);
1186 * Plug default/fallback buffer object functions into the device
1190 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1192 /* GL_ARB_vertex/pixel_buffer_object */
1193 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1194 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1195 driver
->BufferData
= buffer_data_fallback
;
1196 driver
->BufferSubData
= buffer_sub_data_fallback
;
1197 driver
->GetBufferSubData
= buffer_get_subdata
;
1198 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1200 /* GL_ARB_clear_buffer_object */
1201 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1203 /* GL_ARB_map_buffer_range */
1204 driver
->MapBufferRange
= map_buffer_range_fallback
;
1205 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1207 /* GL_ARB_copy_buffer */
1208 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1213 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1214 struct gl_buffer_object
*bufObj
)
1216 for (int i
= 0; i
< MAP_COUNT
; i
++) {
1217 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1218 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1219 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1220 bufObj
->Mappings
[i
].AccessFlags
= 0;
1226 /**********************************************************************/
1228 /**********************************************************************/
1231 _mesa_BindBuffer_no_error(GLenum target
, GLuint buffer
)
1233 GET_CURRENT_CONTEXT(ctx
);
1235 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1236 bind_buffer_object(ctx
, bindTarget
, buffer
);
1241 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1243 GET_CURRENT_CONTEXT(ctx
);
1245 if (MESA_VERBOSE
& VERBOSE_API
) {
1246 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1247 _mesa_enum_to_string(target
), buffer
);
1250 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1252 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target %s)",
1253 _mesa_enum_to_string(target
));
1257 bind_buffer_object(ctx
, bindTarget
, buffer
);
1261 * Binds a buffer object to an atomic buffer binding point.
1263 * The caller is responsible for validating the offset,
1264 * flushing the vertices and updating NewDriverState.
1267 set_atomic_buffer_binding(struct gl_context
*ctx
,
1268 struct gl_buffer_binding
*binding
,
1269 struct gl_buffer_object
*bufObj
,
1274 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1276 binding
->Offset
= offset
;
1277 binding
->Size
= size
;
1278 binding
->AutomaticSize
= autoSize
;
1280 /* If this is a real buffer object, mark it has having been used
1281 * at some point as an atomic counter buffer.
1284 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
1288 * Binds a buffer object to a uniform buffer binding point.
1290 * The caller is responsible for flushing vertices and updating
1294 set_ubo_binding(struct gl_context
*ctx
,
1295 struct gl_buffer_binding
*binding
,
1296 struct gl_buffer_object
*bufObj
,
1301 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1303 binding
->Offset
= offset
;
1304 binding
->Size
= size
;
1305 binding
->AutomaticSize
= autoSize
;
1307 /* If this is a real buffer object, mark it has having been used
1308 * at some point as a UBO.
1311 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
1315 * Binds a buffer object to a shader storage buffer binding point.
1317 * The caller is responsible for flushing vertices and updating
1321 set_ssbo_binding(struct gl_context
*ctx
,
1322 struct gl_buffer_binding
*binding
,
1323 struct gl_buffer_object
*bufObj
,
1328 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1330 binding
->Offset
= offset
;
1331 binding
->Size
= size
;
1332 binding
->AutomaticSize
= autoSize
;
1334 /* If this is a real buffer object, mark it has having been used
1335 * at some point as a SSBO.
1338 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
1342 * Binds a buffer object to a uniform buffer binding point.
1344 * Unlike set_ubo_binding(), this function also flushes vertices
1345 * and updates NewDriverState. It also checks if the binding
1346 * has actually changed before updating it.
1349 bind_uniform_buffer(struct gl_context
*ctx
,
1351 struct gl_buffer_object
*bufObj
,
1356 struct gl_buffer_binding
*binding
=
1357 &ctx
->UniformBufferBindings
[index
];
1359 if (binding
->BufferObject
== bufObj
&&
1360 binding
->Offset
== offset
&&
1361 binding
->Size
== size
&&
1362 binding
->AutomaticSize
== autoSize
) {
1366 FLUSH_VERTICES(ctx
, 0);
1367 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
1369 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
1373 * Binds a buffer object to a shader storage buffer binding point.
1375 * Unlike set_ssbo_binding(), this function also flushes vertices
1376 * and updates NewDriverState. It also checks if the binding
1377 * has actually changed before updating it.
1380 bind_shader_storage_buffer(struct gl_context
*ctx
,
1382 struct gl_buffer_object
*bufObj
,
1387 struct gl_buffer_binding
*binding
=
1388 &ctx
->ShaderStorageBufferBindings
[index
];
1390 if (binding
->BufferObject
== bufObj
&&
1391 binding
->Offset
== offset
&&
1392 binding
->Size
== size
&&
1393 binding
->AutomaticSize
== autoSize
) {
1397 FLUSH_VERTICES(ctx
, 0);
1398 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
1400 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
1404 * Binds a buffer object to an atomic buffer binding point.
1406 * Unlike set_atomic_binding(), this function also flushes vertices
1407 * and updates NewDriverState. It also checks if the binding
1408 * has actually changed before updating it.
1411 bind_atomic_buffer(struct gl_context
*ctx
, unsigned index
,
1412 struct gl_buffer_object
*bufObj
, GLintptr offset
,
1413 GLsizeiptr size
, GLboolean autoSize
)
1415 struct gl_buffer_binding
*binding
=
1416 &ctx
->AtomicBufferBindings
[index
];
1418 if (binding
->BufferObject
== bufObj
&&
1419 binding
->Offset
== offset
&&
1420 binding
->Size
== size
&&
1421 binding
->AutomaticSize
== autoSize
) {
1425 FLUSH_VERTICES(ctx
, 0);
1426 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
1428 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
1432 * Bind a buffer object to a uniform block binding point.
1433 * As above, but offset = 0.
1436 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
1438 struct gl_buffer_object
*bufObj
)
1440 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
1441 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1445 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
1447 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1448 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1450 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1454 * Bind a buffer object to a shader storage block binding point.
1455 * As above, but offset = 0.
1458 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
1460 struct gl_buffer_object
*bufObj
)
1462 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
1463 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1467 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
1469 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1470 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1472 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1476 * Bind a buffer object to a shader storage block binding point.
1477 * As above, but offset = 0.
1480 bind_buffer_base_atomic_buffer(struct gl_context
*ctx
,
1482 struct gl_buffer_object
*bufObj
)
1484 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
1485 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1489 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
1491 if (bufObj
== ctx
->Shared
->NullBufferObj
)
1492 bind_atomic_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1494 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1498 * Delete a set of buffer objects.
1500 * \param n Number of buffer objects to delete.
1501 * \param ids Array of \c n buffer object IDs.
1504 delete_buffers(struct gl_context
*ctx
, GLsizei n
, const GLuint
*ids
)
1506 FLUSH_VERTICES(ctx
, 0);
1508 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1510 for (GLsizei i
= 0; i
< n
; i
++) {
1511 struct gl_buffer_object
*bufObj
=
1512 _mesa_lookup_bufferobj_locked(ctx
, ids
[i
]);
1514 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1517 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1519 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1521 /* unbind any vertex pointers bound to this buffer */
1522 for (j
= 0; j
< ARRAY_SIZE(vao
->BufferBinding
); j
++) {
1523 unbind(ctx
, vao
, j
, bufObj
);
1526 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1527 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1529 if (vao
->IndexBufferObj
== bufObj
) {
1530 bind_buffer_object(ctx
, &vao
->IndexBufferObj
, 0);
1533 /* unbind ARB_draw_indirect binding point */
1534 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1535 bind_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, 0);
1538 /* unbind ARB_indirect_parameters binding point */
1539 if (ctx
->ParameterBuffer
== bufObj
) {
1540 bind_buffer_object(ctx
, &ctx
->ParameterBuffer
, 0);
1543 /* unbind ARB_compute_shader binding point */
1544 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1545 bind_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, 0);
1548 /* unbind ARB_copy_buffer binding points */
1549 if (ctx
->CopyReadBuffer
== bufObj
) {
1550 bind_buffer_object(ctx
, &ctx
->CopyReadBuffer
, 0);
1552 if (ctx
->CopyWriteBuffer
== bufObj
) {
1553 bind_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, 0);
1556 /* unbind transform feedback binding points */
1557 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1558 bind_buffer_object(ctx
, &ctx
->TransformFeedback
.CurrentBuffer
, 0);
1560 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1561 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1562 _mesa_bind_buffer_base_transform_feedback(ctx
,
1563 ctx
->TransformFeedback
.CurrentObject
,
1564 j
, ctx
->Shared
->NullBufferObj
,
1569 /* unbind UBO binding points */
1570 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1571 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1572 bind_buffer_base_uniform_buffer(ctx
, j
,
1573 ctx
->Shared
->NullBufferObj
);
1577 if (ctx
->UniformBuffer
== bufObj
) {
1578 bind_buffer_object(ctx
, &ctx
->UniformBuffer
, 0);
1581 /* unbind SSBO binding points */
1582 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1583 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1584 bind_buffer_base_shader_storage_buffer(ctx
, j
,
1585 ctx
->Shared
->NullBufferObj
);
1589 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1590 bind_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, 0);
1593 /* unbind Atomci Buffer binding points */
1594 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1595 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1596 bind_buffer_base_atomic_buffer(ctx
, j
,
1597 ctx
->Shared
->NullBufferObj
);
1601 if (ctx
->AtomicBuffer
== bufObj
) {
1602 bind_buffer_object(ctx
, &ctx
->AtomicBuffer
, 0);
1605 /* unbind any pixel pack/unpack pointers bound to this buffer */
1606 if (ctx
->Pack
.BufferObj
== bufObj
) {
1607 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1609 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1610 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1613 if (ctx
->Texture
.BufferObject
== bufObj
) {
1614 bind_buffer_object(ctx
, &ctx
->Texture
.BufferObject
, 0);
1617 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1618 bind_buffer_object(ctx
, &ctx
->ExternalVirtualMemoryBuffer
, 0);
1621 /* unbind query buffer binding point */
1622 if (ctx
->QueryBuffer
== bufObj
) {
1623 bind_buffer_object(ctx
, &ctx
->QueryBuffer
, 0);
1626 /* The ID is immediately freed for re-use */
1627 _mesa_HashRemoveLocked(ctx
->Shared
->BufferObjects
, ids
[i
]);
1628 /* Make sure we do not run into the classic ABA problem on bind.
1629 * We don't want to allow re-binding a buffer object that's been
1630 * "deleted" by glDeleteBuffers().
1632 * The explicit rebinding to the default object in the current context
1633 * prevents the above in the current context, but another context
1634 * sharing the same objects might suffer from this problem.
1635 * The alternative would be to do the hash lookup in any case on bind
1636 * which would introduce more runtime overhead than this.
1638 bufObj
->DeletePending
= GL_TRUE
;
1639 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1643 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1648 _mesa_DeleteBuffers_no_error(GLsizei n
, const GLuint
*ids
)
1650 GET_CURRENT_CONTEXT(ctx
);
1651 delete_buffers(ctx
, n
, ids
);
1656 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1658 GET_CURRENT_CONTEXT(ctx
);
1661 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1665 delete_buffers(ctx
, n
, ids
);
1670 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1671 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1675 create_buffers(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1678 struct gl_buffer_object
*buf
;
1684 * This must be atomic (generation and allocation of buffer object IDs)
1686 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1688 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1690 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1691 * DummyBufferObject. Otherwise, create a new buffer object and insert
1694 for (int i
= 0; i
< n
; i
++) {
1695 buffers
[i
] = first
+ i
;
1697 assert(ctx
->Driver
.NewBufferObject
);
1698 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1700 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCreateBuffers");
1701 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1706 buf
= &DummyBufferObject
;
1708 _mesa_HashInsertLocked(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1711 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1716 create_buffers_err(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1718 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1720 if (MESA_VERBOSE
& VERBOSE_API
)
1721 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1724 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1728 create_buffers(ctx
, n
, buffers
, dsa
);
1732 * Generate a set of unique buffer object IDs and store them in \c buffers.
1734 * \param n Number of IDs to generate.
1735 * \param buffers Array of \c n locations to store the IDs.
1738 _mesa_GenBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1740 GET_CURRENT_CONTEXT(ctx
);
1741 create_buffers(ctx
, n
, buffers
, false);
1746 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1748 GET_CURRENT_CONTEXT(ctx
);
1749 create_buffers_err(ctx
, n
, buffers
, false);
1753 * Create a set of buffer objects and store their unique IDs in \c buffers.
1755 * \param n Number of IDs to generate.
1756 * \param buffers Array of \c n locations to store the IDs.
1759 _mesa_CreateBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1761 GET_CURRENT_CONTEXT(ctx
);
1762 create_buffers(ctx
, n
, buffers
, true);
1767 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1769 GET_CURRENT_CONTEXT(ctx
);
1770 create_buffers_err(ctx
, n
, buffers
, true);
1775 * Determine if ID is the name of a buffer object.
1777 * \param id ID of the potential buffer object.
1778 * \return \c GL_TRUE if \c id is the name of a buffer object,
1779 * \c GL_FALSE otherwise.
1781 GLboolean GLAPIENTRY
1782 _mesa_IsBuffer(GLuint id
)
1784 struct gl_buffer_object
*bufObj
;
1785 GET_CURRENT_CONTEXT(ctx
);
1786 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1788 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1790 return bufObj
&& bufObj
!= &DummyBufferObject
;
1795 validate_buffer_storage(struct gl_context
*ctx
,
1796 struct gl_buffer_object
*bufObj
, GLsizeiptr size
,
1797 GLbitfield flags
, const char *func
)
1800 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1804 GLbitfield valid_flags
= GL_MAP_READ_BIT
|
1806 GL_MAP_PERSISTENT_BIT
|
1807 GL_MAP_COHERENT_BIT
|
1808 GL_DYNAMIC_STORAGE_BIT
|
1809 GL_CLIENT_STORAGE_BIT
;
1811 if (ctx
->Extensions
.ARB_sparse_buffer
)
1812 valid_flags
|= GL_SPARSE_STORAGE_BIT_ARB
;
1814 if (flags
& ~valid_flags
) {
1815 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1819 /* The Errors section of the GL_ARB_sparse_buffer spec says:
1821 * "INVALID_VALUE is generated by BufferStorage if <flags> contains
1822 * SPARSE_STORAGE_BIT_ARB and <flags> also contains any combination of
1823 * MAP_READ_BIT or MAP_WRITE_BIT."
1825 if (flags
& GL_SPARSE_STORAGE_BIT_ARB
&&
1826 flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) {
1827 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(SPARSE_STORAGE and READ/WRITE)", func
);
1831 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1832 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1833 _mesa_error(ctx
, GL_INVALID_VALUE
,
1834 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1838 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1839 _mesa_error(ctx
, GL_INVALID_VALUE
,
1840 "%s(COHERENT and flags!=PERSISTENT)", func
);
1844 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
1845 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1854 buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1855 struct gl_memory_object
*memObj
, GLenum target
,
1856 GLsizeiptr size
, const GLvoid
*data
, GLbitfield flags
,
1857 GLuint64 offset
, const char *func
)
1861 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1862 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1864 FLUSH_VERTICES(ctx
, 0);
1866 bufObj
->Written
= GL_TRUE
;
1867 bufObj
->Immutable
= GL_TRUE
;
1868 bufObj
->MinMaxCacheDirty
= true;
1871 assert(ctx
->Driver
.BufferDataMem
);
1872 res
= ctx
->Driver
.BufferDataMem(ctx
, target
, size
, memObj
, offset
,
1873 GL_DYNAMIC_DRAW
, bufObj
);
1876 assert(ctx
->Driver
.BufferData
);
1877 res
= ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1882 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1883 /* Even though the interaction between AMD_pinned_memory and
1884 * glBufferStorage is not described in the spec, Graham Sellers
1885 * said that it should behave the same as glBufferData.
1887 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1890 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1896 static ALWAYS_INLINE
void
1897 inlined_buffer_storage(GLenum target
, GLuint buffer
, GLsizeiptr size
,
1898 const GLvoid
*data
, GLbitfield flags
,
1899 GLuint memory
, GLuint64 offset
,
1900 bool dsa
, bool mem
, bool no_error
, const char *func
)
1902 GET_CURRENT_CONTEXT(ctx
);
1903 struct gl_buffer_object
*bufObj
;
1904 struct gl_memory_object
*memObj
= NULL
;
1908 if (!ctx
->Extensions
.EXT_memory_object
) {
1909 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
1913 /* From the EXT_external_objects spec:
1915 * "An INVALID_VALUE error is generated by BufferStorageMemEXT and
1916 * NamedBufferStorageMemEXT if <memory> is 0, or ..."
1919 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(memory == 0)", func
);
1923 memObj
= _mesa_lookup_memory_object(ctx
, memory
);
1927 /* From the EXT_external_objects spec:
1929 * "An INVALID_OPERATION error is generated if <memory> names a
1930 * valid memory object which has no associated memory."
1932 if (!no_error
&& !memObj
->Immutable
) {
1933 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no associated memory)",
1941 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1943 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
1949 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
1950 bufObj
= *bufObjPtr
;
1952 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
1958 if (no_error
|| validate_buffer_storage(ctx
, bufObj
, size
, flags
, func
))
1959 buffer_storage(ctx
, bufObj
, memObj
, target
, size
, data
, flags
, offset
, func
);
1964 _mesa_BufferStorage_no_error(GLenum target
, GLsizeiptr size
,
1965 const GLvoid
*data
, GLbitfield flags
)
1967 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1968 false, false, true, "glBufferStorage");
1973 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1976 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1977 false, false, false, "glBufferStorage");
1982 _mesa_BufferStorageMemEXT(GLenum target
, GLsizeiptr size
,
1983 GLuint memory
, GLuint64 offset
)
1985 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1986 false, true, false, "glBufferStorageMemEXT");
1991 _mesa_BufferStorageMemEXT_no_error(GLenum target
, GLsizeiptr size
,
1992 GLuint memory
, GLuint64 offset
)
1994 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1995 false, true, true, "glBufferStorageMemEXT");
2000 _mesa_NamedBufferStorage_no_error(GLuint buffer
, GLsizeiptr size
,
2001 const GLvoid
*data
, GLbitfield flags
)
2003 /* In direct state access, buffer objects have an unspecified target
2004 * since they are not required to be bound.
2006 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
2007 true, false, true, "glNamedBufferStorage");
2012 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2015 /* In direct state access, buffer objects have an unspecified target
2016 * since they are not required to be bound.
2018 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
2019 true, false, false, "glNamedBufferStorage");
2023 _mesa_NamedBufferStorageMemEXT(GLuint buffer
, GLsizeiptr size
,
2024 GLuint memory
, GLuint64 offset
)
2026 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
2027 true, true, false, "glNamedBufferStorageMemEXT");
2032 _mesa_NamedBufferStorageMemEXT_no_error(GLuint buffer
, GLsizeiptr size
,
2033 GLuint memory
, GLuint64 offset
)
2035 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
2036 true, true, true, "glNamedBufferStorageMemEXT");
2040 static ALWAYS_INLINE
void
2041 buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2042 GLenum target
, GLsizeiptr size
, const GLvoid
*data
, GLenum usage
,
2043 const char *func
, bool no_error
)
2047 if (MESA_VERBOSE
& VERBOSE_API
) {
2048 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
2050 _mesa_enum_to_string(target
),
2051 (long int) size
, data
,
2052 _mesa_enum_to_string(usage
));
2057 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
2062 case GL_STREAM_DRAW_ARB
:
2063 valid_usage
= (ctx
->API
!= API_OPENGLES
);
2065 case GL_STATIC_DRAW_ARB
:
2066 case GL_DYNAMIC_DRAW_ARB
:
2069 case GL_STREAM_READ_ARB
:
2070 case GL_STREAM_COPY_ARB
:
2071 case GL_STATIC_READ_ARB
:
2072 case GL_STATIC_COPY_ARB
:
2073 case GL_DYNAMIC_READ_ARB
:
2074 case GL_DYNAMIC_COPY_ARB
:
2075 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
2078 valid_usage
= false;
2083 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
2084 _mesa_enum_to_string(usage
));
2088 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
2089 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
2094 /* Unmap the existing buffer. We'll replace it now. Not an error. */
2095 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
2097 FLUSH_VERTICES(ctx
, 0);
2099 bufObj
->Written
= GL_TRUE
;
2100 bufObj
->MinMaxCacheDirty
= true;
2103 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
2104 bufObj
->Name
, size
, data
, usage
);
2111 assert(ctx
->Driver
.BufferData
);
2112 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
2115 GL_DYNAMIC_STORAGE_BIT
,
2117 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
2119 /* From GL_AMD_pinned_memory:
2121 * INVALID_OPERATION is generated by BufferData if <target> is
2122 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
2123 * mapped to the GPU address space.
2125 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2128 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
2134 buffer_data_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2135 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2136 GLenum usage
, const char *func
)
2138 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, false);
2142 buffer_data_no_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2143 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2144 GLenum usage
, const char *func
)
2146 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, true);
2150 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2151 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2152 GLenum usage
, const char *func
)
2154 buffer_data_error(ctx
, bufObj
, target
, size
, data
, usage
, func
);
2158 _mesa_BufferData_no_error(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2161 GET_CURRENT_CONTEXT(ctx
);
2163 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2164 buffer_data_no_error(ctx
, *bufObj
, target
, size
, data
, usage
,
2169 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
2170 const GLvoid
*data
, GLenum usage
)
2172 GET_CURRENT_CONTEXT(ctx
);
2173 struct gl_buffer_object
*bufObj
;
2175 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
2179 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
2184 _mesa_NamedBufferData_no_error(GLuint buffer
, GLsizeiptr size
,
2185 const GLvoid
*data
, GLenum usage
)
2187 GET_CURRENT_CONTEXT(ctx
);
2189 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2190 buffer_data_no_error(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2191 "glNamedBufferData");
2195 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2198 GET_CURRENT_CONTEXT(ctx
);
2199 struct gl_buffer_object
*bufObj
;
2201 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
2205 /* In direct state access, buffer objects have an unspecified target since
2206 * they are not required to be bound.
2208 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2209 "glNamedBufferData");
2214 validate_buffer_sub_data(struct gl_context
*ctx
,
2215 struct gl_buffer_object
*bufObj
,
2216 GLintptr offset
, GLsizeiptr size
,
2219 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2221 /* error already recorded */
2225 if (bufObj
->Immutable
&&
2226 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
2227 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2231 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2232 bufObj
->Usage
== GL_STATIC_COPY
) &&
2233 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
- 1) {
2234 /* If the application declared the buffer as static draw/copy or stream
2235 * draw, it should not be frequently modified with glBufferSubData.
2237 BUFFER_USAGE_WARNING(ctx
,
2238 "using %s(buffer %u, offset %u, size %u) to "
2239 "update a %s buffer",
2240 func
, bufObj
->Name
, offset
, size
,
2241 _mesa_enum_to_string(bufObj
->Usage
));
2249 * Implementation for glBufferSubData and glNamedBufferSubData.
2251 * \param ctx GL context.
2252 * \param bufObj The buffer object.
2253 * \param offset Offset of the first byte of the subdata range.
2254 * \param size Size, in bytes, of the subdata range.
2255 * \param data The data store.
2256 * \param func Name of calling function for recording errors.
2260 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2261 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
)
2266 bufObj
->NumSubDataCalls
++;
2267 bufObj
->Written
= GL_TRUE
;
2268 bufObj
->MinMaxCacheDirty
= true;
2270 assert(ctx
->Driver
.BufferSubData
);
2271 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
2275 static ALWAYS_INLINE
void
2276 buffer_sub_data(GLenum target
, GLuint buffer
, GLintptr offset
,
2277 GLsizeiptr size
, const GLvoid
*data
,
2278 bool dsa
, bool no_error
, const char *func
)
2280 GET_CURRENT_CONTEXT(ctx
);
2281 struct gl_buffer_object
*bufObj
;
2285 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2287 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
2293 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2294 bufObj
= *bufObjPtr
;
2296 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
2302 if (no_error
|| validate_buffer_sub_data(ctx
, bufObj
, offset
, size
, func
))
2303 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
);
2308 _mesa_BufferSubData_no_error(GLenum target
, GLintptr offset
,
2309 GLsizeiptr size
, const GLvoid
*data
)
2311 buffer_sub_data(target
, 0, offset
, size
, data
, false, true,
2317 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
2318 GLsizeiptr size
, const GLvoid
*data
)
2320 buffer_sub_data(target
, 0, offset
, size
, data
, false, false,
2325 _mesa_NamedBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
2326 GLsizeiptr size
, const GLvoid
*data
)
2328 buffer_sub_data(0, buffer
, offset
, size
, data
, true, true,
2329 "glNamedBufferSubData");
2333 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
2334 GLsizeiptr size
, const GLvoid
*data
)
2336 buffer_sub_data(0, buffer
, offset
, size
, data
, true, false,
2337 "glNamedBufferSubData");
2342 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
2343 GLsizeiptr size
, GLvoid
*data
)
2345 GET_CURRENT_CONTEXT(ctx
);
2346 struct gl_buffer_object
*bufObj
;
2348 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
2349 GL_INVALID_OPERATION
);
2353 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2354 "glGetBufferSubData")) {
2358 assert(ctx
->Driver
.GetBufferSubData
);
2359 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2363 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
2364 GLsizeiptr size
, GLvoid
*data
)
2366 GET_CURRENT_CONTEXT(ctx
);
2367 struct gl_buffer_object
*bufObj
;
2369 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2370 "glGetNamedBufferSubData");
2374 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2375 "glGetNamedBufferSubData")) {
2379 assert(ctx
->Driver
.GetBufferSubData
);
2380 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2385 * \param subdata true if caller is *SubData, false if *Data
2387 static ALWAYS_INLINE
void
2388 clear_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2389 GLenum internalformat
, GLintptr offset
, GLsizeiptr size
,
2390 GLenum format
, GLenum type
, const GLvoid
*data
,
2391 const char *func
, bool subdata
, bool no_error
)
2393 mesa_format mesaFormat
;
2394 GLubyte clearValue
[MAX_PIXEL_BYTES
];
2395 GLsizeiptr clearValueSize
;
2397 /* This checks for disallowed mappings. */
2398 if (!no_error
&& !buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2404 mesaFormat
= _mesa_get_texbuffer_format(ctx
, internalformat
);
2406 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
2407 format
, type
, func
);
2410 if (mesaFormat
== MESA_FORMAT_NONE
)
2413 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
2415 (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0)) {
2416 _mesa_error(ctx
, GL_INVALID_VALUE
,
2417 "%s(offset or size is not a multiple of "
2418 "internalformat size)", func
);
2422 /* Bail early. Negative size has already been checked. */
2426 bufObj
->MinMaxCacheDirty
= true;
2429 /* clear to zeros, per the spec */
2430 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2431 NULL
, clearValueSize
, bufObj
);
2435 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
2436 format
, type
, data
, func
)) {
2440 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2441 clearValue
, clearValueSize
, bufObj
);
2445 clear_buffer_sub_data_error(struct gl_context
*ctx
,
2446 struct gl_buffer_object
*bufObj
,
2447 GLenum internalformat
, GLintptr offset
,
2448 GLsizeiptr size
, GLenum format
, GLenum type
,
2449 const GLvoid
*data
, const char *func
, bool subdata
)
2451 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2452 type
, data
, func
, subdata
, false);
2457 clear_buffer_sub_data_no_error(struct gl_context
*ctx
,
2458 struct gl_buffer_object
*bufObj
,
2459 GLenum internalformat
, GLintptr offset
,
2460 GLsizeiptr size
, GLenum format
, GLenum type
,
2461 const GLvoid
*data
, const char *func
,
2464 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2465 type
, data
, func
, subdata
, true);
2470 _mesa_ClearBufferData_no_error(GLenum target
, GLenum internalformat
,
2471 GLenum format
, GLenum type
, const GLvoid
*data
)
2473 GET_CURRENT_CONTEXT(ctx
);
2475 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2476 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, 0,
2477 (*bufObj
)->Size
, format
, type
, data
,
2478 "glClearBufferData", false);
2483 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
2484 GLenum type
, const GLvoid
*data
)
2486 GET_CURRENT_CONTEXT(ctx
);
2487 struct gl_buffer_object
*bufObj
;
2489 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
2493 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2494 format
, type
, data
, "glClearBufferData", false);
2499 _mesa_ClearNamedBufferData_no_error(GLuint buffer
, GLenum internalformat
,
2500 GLenum format
, GLenum type
,
2503 GET_CURRENT_CONTEXT(ctx
);
2505 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2506 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2507 format
, type
, data
, "glClearNamedBufferData",
2513 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
2514 GLenum format
, GLenum type
, const GLvoid
*data
)
2516 GET_CURRENT_CONTEXT(ctx
);
2517 struct gl_buffer_object
*bufObj
;
2519 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
2523 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2524 format
, type
, data
, "glClearNamedBufferData",
2530 _mesa_ClearBufferSubData_no_error(GLenum target
, GLenum internalformat
,
2531 GLintptr offset
, GLsizeiptr size
,
2532 GLenum format
, GLenum type
,
2535 GET_CURRENT_CONTEXT(ctx
);
2537 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2538 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, offset
, size
,
2539 format
, type
, data
, "glClearBufferSubData",
2545 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
2546 GLintptr offset
, GLsizeiptr size
,
2547 GLenum format
, GLenum type
,
2550 GET_CURRENT_CONTEXT(ctx
);
2551 struct gl_buffer_object
*bufObj
;
2553 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
2557 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2558 format
, type
, data
, "glClearBufferSubData",
2564 _mesa_ClearNamedBufferSubData_no_error(GLuint buffer
, GLenum internalformat
,
2565 GLintptr offset
, GLsizeiptr size
,
2566 GLenum format
, GLenum type
,
2569 GET_CURRENT_CONTEXT(ctx
);
2571 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2572 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, offset
, size
,
2574 "glClearNamedBufferSubData", true);
2579 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
2580 GLintptr offset
, GLsizeiptr size
,
2581 GLenum format
, GLenum type
,
2584 GET_CURRENT_CONTEXT(ctx
);
2585 struct gl_buffer_object
*bufObj
;
2587 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2588 "glClearNamedBufferSubData");
2592 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2593 format
, type
, data
, "glClearNamedBufferSubData",
2598 unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
2600 GLboolean status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2601 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2602 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2603 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2604 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2610 validate_and_unmap_buffer(struct gl_context
*ctx
,
2611 struct gl_buffer_object
*bufObj
,
2614 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2616 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2617 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2618 "%s(buffer is not mapped)", func
);
2623 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2624 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2626 /* check that last 100 bytes are still = magic value */
2627 for (i
= 0; i
< 100; i
++) {
2628 GLuint pos
= bufObj
->Size
- i
- 1;
2629 if (buf
[pos
] != 123) {
2630 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2631 " at position %d (value = %u)\n",
2639 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2640 GLuint i
, unchanged
= 0;
2641 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2643 /* check which bytes changed */
2644 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2645 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2652 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2653 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2658 return unmap_buffer(ctx
, bufObj
);
2661 GLboolean GLAPIENTRY
2662 _mesa_UnmapBuffer_no_error(GLenum target
)
2664 GET_CURRENT_CONTEXT(ctx
);
2665 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2666 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2668 return unmap_buffer(ctx
, bufObj
);
2671 GLboolean GLAPIENTRY
2672 _mesa_UnmapBuffer(GLenum target
)
2674 GET_CURRENT_CONTEXT(ctx
);
2675 struct gl_buffer_object
*bufObj
;
2677 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2681 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2684 GLboolean GLAPIENTRY
2685 _mesa_UnmapNamedBuffer_no_error(GLuint buffer
)
2687 GET_CURRENT_CONTEXT(ctx
);
2688 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2690 return unmap_buffer(ctx
, bufObj
);
2693 GLboolean GLAPIENTRY
2694 _mesa_UnmapNamedBuffer(GLuint buffer
)
2696 GET_CURRENT_CONTEXT(ctx
);
2697 struct gl_buffer_object
*bufObj
;
2699 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2703 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2708 get_buffer_parameter(struct gl_context
*ctx
,
2709 struct gl_buffer_object
*bufObj
, GLenum pname
,
2710 GLint64
*params
, const char *func
)
2713 case GL_BUFFER_SIZE_ARB
:
2714 *params
= bufObj
->Size
;
2716 case GL_BUFFER_USAGE_ARB
:
2717 *params
= bufObj
->Usage
;
2719 case GL_BUFFER_ACCESS_ARB
:
2720 *params
= simplified_access_mode(ctx
,
2721 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2723 case GL_BUFFER_MAPPED_ARB
:
2724 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2726 case GL_BUFFER_ACCESS_FLAGS
:
2727 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2729 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2731 case GL_BUFFER_MAP_OFFSET
:
2732 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2734 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2736 case GL_BUFFER_MAP_LENGTH
:
2737 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2739 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2741 case GL_BUFFER_IMMUTABLE_STORAGE
:
2742 if (!ctx
->Extensions
.ARB_buffer_storage
)
2744 *params
= bufObj
->Immutable
;
2746 case GL_BUFFER_STORAGE_FLAGS
:
2747 if (!ctx
->Extensions
.ARB_buffer_storage
)
2749 *params
= bufObj
->StorageFlags
;
2758 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2759 _mesa_enum_to_string(pname
));
2764 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2766 GET_CURRENT_CONTEXT(ctx
);
2767 struct gl_buffer_object
*bufObj
;
2770 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2771 GL_INVALID_OPERATION
);
2775 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2776 "glGetBufferParameteriv"))
2777 return; /* Error already recorded. */
2779 *params
= (GLint
) parameter
;
2783 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2785 GET_CURRENT_CONTEXT(ctx
);
2786 struct gl_buffer_object
*bufObj
;
2789 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2790 GL_INVALID_OPERATION
);
2794 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2795 "glGetBufferParameteri64v"))
2796 return; /* Error already recorded. */
2798 *params
= parameter
;
2802 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2804 GET_CURRENT_CONTEXT(ctx
);
2805 struct gl_buffer_object
*bufObj
;
2808 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2809 "glGetNamedBufferParameteriv");
2813 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2814 "glGetNamedBufferParameteriv"))
2815 return; /* Error already recorded. */
2817 *params
= (GLint
) parameter
;
2821 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2824 GET_CURRENT_CONTEXT(ctx
);
2825 struct gl_buffer_object
*bufObj
;
2828 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2829 "glGetNamedBufferParameteri64v");
2833 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2834 "glGetNamedBufferParameteri64v"))
2835 return; /* Error already recorded. */
2837 *params
= parameter
;
2842 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2844 GET_CURRENT_CONTEXT(ctx
);
2845 struct gl_buffer_object
*bufObj
;
2847 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2848 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2849 "GL_BUFFER_MAP_POINTER)");
2853 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2854 GL_INVALID_OPERATION
);
2858 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2862 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2864 GET_CURRENT_CONTEXT(ctx
);
2865 struct gl_buffer_object
*bufObj
;
2867 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2868 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2869 "GL_BUFFER_MAP_POINTER)");
2873 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2874 "glGetNamedBufferPointerv");
2878 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2883 copy_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*src
,
2884 struct gl_buffer_object
*dst
, GLintptr readOffset
,
2885 GLintptr writeOffset
, GLsizeiptr size
, const char *func
)
2887 if (_mesa_check_disallowed_mapping(src
)) {
2888 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2889 "%s(readBuffer is mapped)", func
);
2893 if (_mesa_check_disallowed_mapping(dst
)) {
2894 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2895 "%s(writeBuffer is mapped)", func
);
2899 if (readOffset
< 0) {
2900 _mesa_error(ctx
, GL_INVALID_VALUE
,
2901 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2905 if (writeOffset
< 0) {
2906 _mesa_error(ctx
, GL_INVALID_VALUE
,
2907 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2912 _mesa_error(ctx
, GL_INVALID_VALUE
,
2913 "%s(size %d < 0)", func
, (int) size
);
2917 if (readOffset
+ size
> src
->Size
) {
2918 _mesa_error(ctx
, GL_INVALID_VALUE
,
2919 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2920 (int) readOffset
, (int) size
, (int) src
->Size
);
2924 if (writeOffset
+ size
> dst
->Size
) {
2925 _mesa_error(ctx
, GL_INVALID_VALUE
,
2926 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2927 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2932 if (readOffset
+ size
<= writeOffset
) {
2935 else if (writeOffset
+ size
<= readOffset
) {
2939 /* overlapping src/dst is illegal */
2940 _mesa_error(ctx
, GL_INVALID_VALUE
,
2941 "%s(overlapping src/dst)", func
);
2946 dst
->MinMaxCacheDirty
= true;
2948 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2952 _mesa_CopyBufferSubData_no_error(GLenum readTarget
, GLenum writeTarget
,
2953 GLintptr readOffset
, GLintptr writeOffset
,
2956 GET_CURRENT_CONTEXT(ctx
);
2958 struct gl_buffer_object
**src_ptr
= get_buffer_target(ctx
, readTarget
);
2959 struct gl_buffer_object
*src
= *src_ptr
;
2961 struct gl_buffer_object
**dst_ptr
= get_buffer_target(ctx
, writeTarget
);
2962 struct gl_buffer_object
*dst
= *dst_ptr
;
2964 dst
->MinMaxCacheDirty
= true;
2965 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
2970 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2971 GLintptr readOffset
, GLintptr writeOffset
,
2974 GET_CURRENT_CONTEXT(ctx
);
2975 struct gl_buffer_object
*src
, *dst
;
2977 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2978 GL_INVALID_OPERATION
);
2982 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2983 GL_INVALID_OPERATION
);
2987 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2988 "glCopyBufferSubData");
2992 _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer
, GLuint writeBuffer
,
2993 GLintptr readOffset
,
2994 GLintptr writeOffset
, GLsizeiptr size
)
2996 GET_CURRENT_CONTEXT(ctx
);
2998 struct gl_buffer_object
*src
= _mesa_lookup_bufferobj(ctx
, readBuffer
);
2999 struct gl_buffer_object
*dst
= _mesa_lookup_bufferobj(ctx
, writeBuffer
);
3001 dst
->MinMaxCacheDirty
= true;
3002 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
3007 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
3008 GLintptr readOffset
, GLintptr writeOffset
,
3011 GET_CURRENT_CONTEXT(ctx
);
3012 struct gl_buffer_object
*src
, *dst
;
3014 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
3015 "glCopyNamedBufferSubData");
3019 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
3020 "glCopyNamedBufferSubData");
3024 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
3025 "glCopyNamedBufferSubData");
3029 validate_map_buffer_range(struct gl_context
*ctx
,
3030 struct gl_buffer_object
*bufObj
, GLintptr offset
,
3031 GLsizeiptr length
, GLbitfield access
,
3034 GLbitfield allowed_access
;
3036 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, false);
3039 _mesa_error(ctx
, GL_INVALID_VALUE
,
3040 "%s(offset %ld < 0)", func
, (long) offset
);
3045 _mesa_error(ctx
, GL_INVALID_VALUE
,
3046 "%s(length %ld < 0)", func
, (long) length
);
3050 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
3052 * "An INVALID_OPERATION error is generated for any of the following
3055 * * <length> is zero."
3057 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
3058 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
3062 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
3066 allowed_access
= GL_MAP_READ_BIT
|
3068 GL_MAP_INVALIDATE_RANGE_BIT
|
3069 GL_MAP_INVALIDATE_BUFFER_BIT
|
3070 GL_MAP_FLUSH_EXPLICIT_BIT
|
3071 GL_MAP_UNSYNCHRONIZED_BIT
;
3073 if (ctx
->Extensions
.ARB_buffer_storage
) {
3074 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
3075 GL_MAP_COHERENT_BIT
;
3078 if (access
& ~allowed_access
) {
3079 /* generate an error if any bits other than those allowed are set */
3080 _mesa_error(ctx
, GL_INVALID_VALUE
,
3081 "%s(access has undefined bits set)", func
);
3085 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
3086 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3087 "%s(access indicates neither read or write)", func
);
3091 if ((access
& GL_MAP_READ_BIT
) &&
3092 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
3093 GL_MAP_INVALIDATE_BUFFER_BIT
|
3094 GL_MAP_UNSYNCHRONIZED_BIT
))) {
3095 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3096 "%s(read access with disallowed bits)", func
);
3100 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
3101 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
3102 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3103 "%s(access has flush explicit without write)", func
);
3107 if (access
& GL_MAP_READ_BIT
&&
3108 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
3109 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3110 "%s(buffer does not allow read access)", func
);
3114 if (access
& GL_MAP_WRITE_BIT
&&
3115 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
3116 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3117 "%s(buffer does not allow write access)", func
);
3121 if (access
& GL_MAP_COHERENT_BIT
&&
3122 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
3123 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3124 "%s(buffer does not allow coherent access)", func
);
3128 if (access
& GL_MAP_PERSISTENT_BIT
&&
3129 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
3130 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3131 "%s(buffer does not allow persistent access)", func
);
3135 if (offset
+ length
> bufObj
->Size
) {
3136 _mesa_error(ctx
, GL_INVALID_VALUE
,
3137 "%s(offset %lu + length %lu > buffer_size %lu)", func
,
3138 (unsigned long) offset
, (unsigned long) length
,
3139 (unsigned long) bufObj
->Size
);
3143 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3144 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3145 "%s(buffer already mapped)", func
);
3149 if (access
& GL_MAP_WRITE_BIT
) {
3150 bufObj
->NumMapBufferWriteCalls
++;
3151 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
3152 bufObj
->Usage
== GL_STATIC_COPY
) &&
3153 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
3154 BUFFER_USAGE_WARNING(ctx
,
3155 "using %s(buffer %u, offset %u, length %u) to "
3156 "update a %s buffer",
3157 func
, bufObj
->Name
, offset
, length
,
3158 _mesa_enum_to_string(bufObj
->Usage
));
3166 map_buffer_range(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
3167 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
3170 if (!bufObj
->Size
) {
3171 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
3175 assert(ctx
->Driver
.MapBufferRange
);
3176 void *map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
3179 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
3182 /* The driver callback should have set all these fields.
3183 * This is important because other modules (like VBO) might call
3184 * the driver function directly.
3186 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
3187 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
3188 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
3189 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
3192 if (access
& GL_MAP_WRITE_BIT
) {
3193 bufObj
->Written
= GL_TRUE
;
3194 bufObj
->MinMaxCacheDirty
= true;
3198 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3199 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
3200 bufObj
->Name
, bufObj
->Size
, access
);
3201 /* Access must be write only */
3202 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
3204 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
3205 for (i
= 0; i
< bufObj
->Size
; i
++)
3212 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3213 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
3215 /* buffer is 100 bytes larger than requested, fill with magic value */
3216 for (i
= 0; i
< 100; i
++) {
3217 buf
[bufObj
->Size
- i
- 1] = 123;
3226 _mesa_MapBufferRange_no_error(GLenum target
, GLintptr offset
,
3227 GLsizeiptr length
, GLbitfield access
)
3229 GET_CURRENT_CONTEXT(ctx
);
3231 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3232 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3234 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3235 "glMapBufferRange");
3239 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
3242 GET_CURRENT_CONTEXT(ctx
);
3243 struct gl_buffer_object
*bufObj
;
3245 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3246 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3247 "glMapBufferRange(ARB_map_buffer_range not supported)");
3251 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
3255 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3256 "glMapBufferRange"))
3259 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3260 "glMapBufferRange");
3264 _mesa_MapNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3265 GLsizeiptr length
, GLbitfield access
)
3267 GET_CURRENT_CONTEXT(ctx
);
3268 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3270 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3271 "glMapNamedBufferRange");
3275 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3278 GET_CURRENT_CONTEXT(ctx
);
3279 struct gl_buffer_object
*bufObj
;
3281 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3282 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3283 "glMapNamedBufferRange("
3284 "ARB_map_buffer_range not supported)");
3288 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
3292 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3293 "glMapNamedBufferRange"))
3296 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3297 "glMapNamedBufferRange");
3301 * Converts GLenum access from MapBuffer and MapNamedBuffer into
3302 * flags for input to map_buffer_range.
3304 * \return true if the type of requested access is permissible.
3307 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
3311 case GL_READ_ONLY_ARB
:
3312 *flags
= GL_MAP_READ_BIT
;
3313 return _mesa_is_desktop_gl(ctx
);
3314 case GL_WRITE_ONLY_ARB
:
3315 *flags
= GL_MAP_WRITE_BIT
;
3317 case GL_READ_WRITE_ARB
:
3318 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
3319 return _mesa_is_desktop_gl(ctx
);
3327 _mesa_MapBuffer_no_error(GLenum target
, GLenum access
)
3329 GET_CURRENT_CONTEXT(ctx
);
3331 GLbitfield accessFlags
;
3332 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3334 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3335 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3337 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3342 _mesa_MapBuffer(GLenum target
, GLenum access
)
3344 GET_CURRENT_CONTEXT(ctx
);
3345 struct gl_buffer_object
*bufObj
;
3346 GLbitfield accessFlags
;
3348 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3349 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
3353 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
3357 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3361 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3366 _mesa_MapNamedBuffer_no_error(GLuint buffer
, GLenum access
)
3368 GET_CURRENT_CONTEXT(ctx
);
3370 GLbitfield accessFlags
;
3371 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3373 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3375 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3376 "glMapNamedBuffer");
3380 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
3382 GET_CURRENT_CONTEXT(ctx
);
3383 struct gl_buffer_object
*bufObj
;
3384 GLbitfield accessFlags
;
3386 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3387 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
3391 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
3395 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3396 "glMapNamedBuffer"))
3399 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3400 "glMapNamedBuffer");
3405 flush_mapped_buffer_range(struct gl_context
*ctx
,
3406 struct gl_buffer_object
*bufObj
,
3407 GLintptr offset
, GLsizeiptr length
,
3410 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3411 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3412 "%s(ARB_map_buffer_range not supported)", func
);
3417 _mesa_error(ctx
, GL_INVALID_VALUE
,
3418 "%s(offset %ld < 0)", func
, (long) offset
);
3423 _mesa_error(ctx
, GL_INVALID_VALUE
,
3424 "%s(length %ld < 0)", func
, (long) length
);
3428 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3429 /* buffer is not mapped */
3430 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3431 "%s(buffer is not mapped)", func
);
3435 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
3436 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
3437 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3438 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
3442 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
3443 _mesa_error(ctx
, GL_INVALID_VALUE
,
3444 "%s(offset %ld + length %ld > mapped length %ld)", func
,
3445 (long) offset
, (long) length
,
3446 (long) bufObj
->Mappings
[MAP_USER
].Length
);
3450 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
3452 if (ctx
->Driver
.FlushMappedBufferRange
)
3453 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3458 _mesa_FlushMappedBufferRange_no_error(GLenum target
, GLintptr offset
,
3461 GET_CURRENT_CONTEXT(ctx
);
3462 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3463 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3465 if (ctx
->Driver
.FlushMappedBufferRange
)
3466 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3471 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
3474 GET_CURRENT_CONTEXT(ctx
);
3475 struct gl_buffer_object
*bufObj
;
3477 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
3478 GL_INVALID_OPERATION
);
3482 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3483 "glFlushMappedBufferRange");
3487 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3490 GET_CURRENT_CONTEXT(ctx
);
3491 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3493 if (ctx
->Driver
.FlushMappedBufferRange
)
3494 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3499 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
3502 GET_CURRENT_CONTEXT(ctx
);
3503 struct gl_buffer_object
*bufObj
;
3505 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
3506 "glFlushMappedNamedBufferRange");
3510 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3511 "glFlushMappedNamedBufferRange");
3515 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
, GLuint index
,
3516 struct gl_buffer_object
*bufObj
,
3517 GLintptr offset
, GLsizeiptr size
)
3519 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3524 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3525 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3529 * Bind a region of a buffer object to a uniform block binding point.
3530 * \param index the uniform buffer binding point index
3531 * \param bufObj the buffer object
3532 * \param offset offset to the start of buffer object region
3533 * \param size size of the buffer object region
3536 bind_buffer_range_uniform_buffer_err(struct gl_context
*ctx
, GLuint index
,
3537 struct gl_buffer_object
*bufObj
,
3538 GLintptr offset
, GLsizeiptr size
)
3540 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3541 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3545 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3546 _mesa_error(ctx
, GL_INVALID_VALUE
,
3547 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3548 ctx
->Const
.UniformBufferOffsetAlignment
);
3552 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3556 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
3558 struct gl_buffer_object
*bufObj
,
3562 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3567 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3568 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3572 * Bind a region of a buffer object to a shader storage block binding point.
3573 * \param index the shader storage buffer binding point index
3574 * \param bufObj the buffer object
3575 * \param offset offset to the start of buffer object region
3576 * \param size size of the buffer object region
3579 bind_buffer_range_shader_storage_buffer_err(struct gl_context
*ctx
,
3581 struct gl_buffer_object
*bufObj
,
3582 GLintptr offset
, GLsizeiptr size
)
3584 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3585 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3589 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3590 _mesa_error(ctx
, GL_INVALID_VALUE
,
3591 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3592 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3596 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3600 bind_buffer_range_atomic_buffer(struct gl_context
*ctx
, GLuint index
,
3601 struct gl_buffer_object
*bufObj
,
3602 GLintptr offset
, GLsizeiptr size
)
3604 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3609 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3610 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3614 * Bind a region of a buffer object to an atomic storage block binding point.
3615 * \param index the shader storage buffer binding point index
3616 * \param bufObj the buffer object
3617 * \param offset offset to the start of buffer object region
3618 * \param size size of the buffer object region
3621 bind_buffer_range_atomic_buffer_err(struct gl_context
*ctx
,
3623 struct gl_buffer_object
*bufObj
,
3624 GLintptr offset
, GLsizeiptr size
)
3626 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3627 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3631 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3632 _mesa_error(ctx
, GL_INVALID_VALUE
,
3633 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3634 ATOMIC_COUNTER_SIZE
);
3638 bind_buffer_range_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
3642 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3644 const GLintptr
*offsets
,
3645 const GLsizeiptr
*sizes
)
3647 if (offsets
[index
] < 0) {
3648 /* The ARB_multi_bind spec says:
3650 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3651 * value in <offsets> is less than zero (per binding)."
3653 _mesa_error(ctx
, GL_INVALID_VALUE
,
3654 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3655 index
, (int64_t) offsets
[index
]);
3659 if (sizes
[index
] <= 0) {
3660 /* The ARB_multi_bind spec says:
3662 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3663 * value in <sizes> is less than or equal to zero (per binding)."
3665 _mesa_error(ctx
, GL_INVALID_VALUE
,
3666 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3667 index
, (int64_t) sizes
[index
]);
3675 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3676 GLuint first
, GLsizei count
,
3679 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3680 _mesa_error(ctx
, GL_INVALID_ENUM
,
3681 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3685 /* The ARB_multi_bind_spec says:
3687 * "An INVALID_OPERATION error is generated if <first> + <count> is
3688 * greater than the number of target-specific indexed binding points,
3689 * as described in section 6.7.1."
3691 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3692 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3693 "%s(first=%u + count=%d > the value of "
3694 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3695 caller
, first
, count
,
3696 ctx
->Const
.MaxUniformBufferBindings
);
3704 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3705 GLuint first
, GLsizei count
,
3708 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3709 _mesa_error(ctx
, GL_INVALID_ENUM
,
3710 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3714 /* The ARB_multi_bind_spec says:
3716 * "An INVALID_OPERATION error is generated if <first> + <count> is
3717 * greater than the number of target-specific indexed binding points,
3718 * as described in section 6.7.1."
3720 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3721 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3722 "%s(first=%u + count=%d > the value of "
3723 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3724 caller
, first
, count
,
3725 ctx
->Const
.MaxShaderStorageBufferBindings
);
3733 * Unbind all uniform buffers in the range
3734 * <first> through <first>+<count>-1
3737 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3739 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3741 for (int i
= 0; i
< count
; i
++)
3742 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3743 bufObj
, -1, -1, GL_TRUE
);
3747 * Unbind all shader storage buffers in the range
3748 * <first> through <first>+<count>-1
3751 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3754 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3756 for (int i
= 0; i
< count
; i
++)
3757 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3758 bufObj
, -1, -1, GL_TRUE
);
3762 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3763 const GLuint
*buffers
,
3765 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3768 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3771 /* Assume that at least one binding will be changed */
3772 FLUSH_VERTICES(ctx
, 0);
3773 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3776 /* The ARB_multi_bind spec says:
3778 * "If <buffers> is NULL, all bindings from <first> through
3779 * <first>+<count>-1 are reset to their unbound (zero) state.
3780 * In this case, the offsets and sizes associated with the
3781 * binding points are set to default values, ignoring
3782 * <offsets> and <sizes>."
3784 unbind_uniform_buffers(ctx
, first
, count
);
3788 /* Note that the error semantics for multi-bind commands differ from
3789 * those of other GL commands.
3791 * The Issues section in the ARB_multi_bind spec says:
3793 * "(11) Typically, OpenGL specifies that if an error is generated by a
3794 * command, that command has no effect. This is somewhat
3795 * unfortunate for multi-bind commands, because it would require a
3796 * first pass to scan the entire list of bound objects for errors
3797 * and then a second pass to actually perform the bindings.
3798 * Should we have different error semantics?
3800 * RESOLVED: Yes. In this specification, when the parameters for
3801 * one of the <count> binding points are invalid, that binding point
3802 * is not updated and an error will be generated. However, other
3803 * binding points in the same command will be updated if their
3804 * parameters are valid and no other error occurs."
3807 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3809 for (int i
= 0; i
< count
; i
++) {
3810 struct gl_buffer_binding
*binding
=
3811 &ctx
->UniformBufferBindings
[first
+ i
];
3812 struct gl_buffer_object
*bufObj
;
3813 GLintptr offset
= 0;
3814 GLsizeiptr size
= 0;
3817 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3820 /* The ARB_multi_bind spec says:
3822 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3823 * pair of values in <offsets> and <sizes> does not respectively
3824 * satisfy the constraints described for those parameters for the
3825 * specified target, as described in section 6.7.1 (per binding)."
3827 * Section 6.7.1 refers to table 6.5, which says:
3829 * "┌───────────────────────────────────────────────────────────────┐
3830 * │ Uniform buffer array bindings (see sec. 7.6) │
3831 * ├─────────────────────┬─────────────────────────────────────────┤
3833 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3834 * │ │ OFFSET_ALIGNMENT │
3836 * │ size restriction │ none │
3837 * └─────────────────────┴─────────────────────────────────────────┘"
3839 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3840 _mesa_error(ctx
, GL_INVALID_VALUE
,
3841 "glBindBuffersRange(offsets[%u]=%" PRId64
3842 " is misaligned; it must be a multiple of the value of "
3843 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3844 "target=GL_UNIFORM_BUFFER)",
3845 i
, (int64_t) offsets
[i
],
3846 ctx
->Const
.UniformBufferOffsetAlignment
);
3850 offset
= offsets
[i
];
3854 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3855 bufObj
= binding
->BufferObject
;
3857 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3860 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3861 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3863 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3867 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3871 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3872 GLsizei count
, const GLuint
*buffers
,
3874 const GLintptr
*offsets
,
3875 const GLsizeiptr
*sizes
,
3878 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3881 /* Assume that at least one binding will be changed */
3882 FLUSH_VERTICES(ctx
, 0);
3883 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3886 /* The ARB_multi_bind spec says:
3888 * "If <buffers> is NULL, all bindings from <first> through
3889 * <first>+<count>-1 are reset to their unbound (zero) state.
3890 * In this case, the offsets and sizes associated with the
3891 * binding points are set to default values, ignoring
3892 * <offsets> and <sizes>."
3894 unbind_shader_storage_buffers(ctx
, first
, count
);
3898 /* Note that the error semantics for multi-bind commands differ from
3899 * those of other GL commands.
3901 * The Issues section in the ARB_multi_bind spec says:
3903 * "(11) Typically, OpenGL specifies that if an error is generated by a
3904 * command, that command has no effect. This is somewhat
3905 * unfortunate for multi-bind commands, because it would require a
3906 * first pass to scan the entire list of bound objects for errors
3907 * and then a second pass to actually perform the bindings.
3908 * Should we have different error semantics?
3910 * RESOLVED: Yes. In this specification, when the parameters for
3911 * one of the <count> binding points are invalid, that binding point
3912 * is not updated and an error will be generated. However, other
3913 * binding points in the same command will be updated if their
3914 * parameters are valid and no other error occurs."
3917 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
3919 for (int i
= 0; i
< count
; i
++) {
3920 struct gl_buffer_binding
*binding
=
3921 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3922 struct gl_buffer_object
*bufObj
;
3923 GLintptr offset
= 0;
3924 GLsizeiptr size
= 0;
3927 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3930 /* The ARB_multi_bind spec says:
3932 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3933 * pair of values in <offsets> and <sizes> does not respectively
3934 * satisfy the constraints described for those parameters for the
3935 * specified target, as described in section 6.7.1 (per binding)."
3937 * Section 6.7.1 refers to table 6.5, which says:
3939 * "┌───────────────────────────────────────────────────────────────┐
3940 * │ Shader storage buffer array bindings (see sec. 7.8) │
3941 * ├─────────────────────┬─────────────────────────────────────────┤
3943 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3944 * │ │ BUFFER_OFFSET_ALIGNMENT │
3946 * │ size restriction │ none │
3947 * └─────────────────────┴─────────────────────────────────────────┘"
3949 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3950 _mesa_error(ctx
, GL_INVALID_VALUE
,
3951 "glBindBuffersRange(offsets[%u]=%" PRId64
3952 " is misaligned; it must be a multiple of the value of "
3953 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3954 "target=GL_SHADER_STORAGE_BUFFER)",
3955 i
, (int64_t) offsets
[i
],
3956 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3960 offset
= offsets
[i
];
3964 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3965 bufObj
= binding
->BufferObject
;
3967 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3970 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3971 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3973 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3977 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
3981 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3982 struct gl_transform_feedback_object
*tfObj
,
3983 GLuint first
, GLsizei count
, const char *caller
)
3985 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3986 _mesa_error(ctx
, GL_INVALID_ENUM
,
3987 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3991 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3993 * "An INVALID_OPERATION error is generated :
3996 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3997 * FEEDBACK_BUFFER and transform feedback is currently active."
3999 * We assume that this is also meant to apply to BindBuffersRange
4000 * and BindBuffersBase.
4002 if (tfObj
->Active
) {
4003 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4004 "%s(Changing transform feedback buffers while "
4005 "transform feedback is active)", caller
);
4009 /* The ARB_multi_bind_spec says:
4011 * "An INVALID_OPERATION error is generated if <first> + <count> is
4012 * greater than the number of target-specific indexed binding points,
4013 * as described in section 6.7.1."
4015 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
4016 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4017 "%s(first=%u + count=%d > the value of "
4018 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
4019 caller
, first
, count
,
4020 ctx
->Const
.MaxTransformFeedbackBuffers
);
4028 * Unbind all transform feedback buffers in the range
4029 * <first> through <first>+<count>-1
4032 unbind_xfb_buffers(struct gl_context
*ctx
,
4033 struct gl_transform_feedback_object
*tfObj
,
4034 GLuint first
, GLsizei count
)
4036 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
4038 for (int i
= 0; i
< count
; i
++)
4039 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
4044 bind_xfb_buffers(struct gl_context
*ctx
,
4045 GLuint first
, GLsizei count
,
4046 const GLuint
*buffers
,
4048 const GLintptr
*offsets
,
4049 const GLsizeiptr
*sizes
,
4052 struct gl_transform_feedback_object
*tfObj
=
4053 ctx
->TransformFeedback
.CurrentObject
;
4055 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
4058 /* Assume that at least one binding will be changed */
4059 FLUSH_VERTICES(ctx
, 0);
4060 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
4063 /* The ARB_multi_bind spec says:
4065 * "If <buffers> is NULL, all bindings from <first> through
4066 * <first>+<count>-1 are reset to their unbound (zero) state.
4067 * In this case, the offsets and sizes associated with the
4068 * binding points are set to default values, ignoring
4069 * <offsets> and <sizes>."
4071 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
4075 /* Note that the error semantics for multi-bind commands differ from
4076 * those of other GL commands.
4078 * The Issues section in the ARB_multi_bind spec says:
4080 * "(11) Typically, OpenGL specifies that if an error is generated by a
4081 * command, that command has no effect. This is somewhat
4082 * unfortunate for multi-bind commands, because it would require a
4083 * first pass to scan the entire list of bound objects for errors
4084 * and then a second pass to actually perform the bindings.
4085 * Should we have different error semantics?
4087 * RESOLVED: Yes. In this specification, when the parameters for
4088 * one of the <count> binding points are invalid, that binding point
4089 * is not updated and an error will be generated. However, other
4090 * binding points in the same command will be updated if their
4091 * parameters are valid and no other error occurs."
4094 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4096 for (int i
= 0; i
< count
; i
++) {
4097 const GLuint index
= first
+ i
;
4098 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
4099 struct gl_buffer_object
*bufObj
;
4100 GLintptr offset
= 0;
4101 GLsizeiptr size
= 0;
4104 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4107 /* The ARB_multi_bind spec says:
4109 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4110 * pair of values in <offsets> and <sizes> does not respectively
4111 * satisfy the constraints described for those parameters for the
4112 * specified target, as described in section 6.7.1 (per binding)."
4114 * Section 6.7.1 refers to table 6.5, which says:
4116 * "┌───────────────────────────────────────────────────────────────┐
4117 * │ Transform feedback array bindings (see sec. 13.2.2) │
4118 * ├───────────────────────┬───────────────────────────────────────┤
4120 * │ offset restriction │ multiple of 4 │
4122 * │ size restriction │ multiple of 4 │
4123 * └───────────────────────┴───────────────────────────────────────┘"
4125 if (offsets
[i
] & 0x3) {
4126 _mesa_error(ctx
, GL_INVALID_VALUE
,
4127 "glBindBuffersRange(offsets[%u]=%" PRId64
4128 " is misaligned; it must be a multiple of 4 when "
4129 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4130 i
, (int64_t) offsets
[i
]);
4134 if (sizes
[i
] & 0x3) {
4135 _mesa_error(ctx
, GL_INVALID_VALUE
,
4136 "glBindBuffersRange(sizes[%u]=%" PRId64
4137 " is misaligned; it must be a multiple of 4 when "
4138 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4139 i
, (int64_t) sizes
[i
]);
4143 offset
= offsets
[i
];
4147 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
4148 bufObj
= boundBufObj
;
4150 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
4153 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
4157 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4161 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
4162 GLuint first
, GLsizei count
,
4165 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
4166 _mesa_error(ctx
, GL_INVALID_ENUM
,
4167 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
4171 /* The ARB_multi_bind_spec says:
4173 * "An INVALID_OPERATION error is generated if <first> + <count> is
4174 * greater than the number of target-specific indexed binding points,
4175 * as described in section 6.7.1."
4177 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
4178 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4179 "%s(first=%u + count=%d > the value of "
4180 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
4181 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
4189 * Unbind all atomic counter buffers in the range
4190 * <first> through <first>+<count>-1
4193 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
4195 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
4197 for (int i
= 0; i
< count
; i
++)
4198 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
4199 bufObj
, -1, -1, GL_TRUE
);
4203 bind_atomic_buffers(struct gl_context
*ctx
,
4206 const GLuint
*buffers
,
4208 const GLintptr
*offsets
,
4209 const GLsizeiptr
*sizes
,
4212 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
4215 /* Assume that at least one binding will be changed */
4216 FLUSH_VERTICES(ctx
, 0);
4217 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
4220 /* The ARB_multi_bind spec says:
4222 * "If <buffers> is NULL, all bindings from <first> through
4223 * <first>+<count>-1 are reset to their unbound (zero) state.
4224 * In this case, the offsets and sizes associated with the
4225 * binding points are set to default values, ignoring
4226 * <offsets> and <sizes>."
4228 unbind_atomic_buffers(ctx
, first
, count
);
4232 /* Note that the error semantics for multi-bind commands differ from
4233 * those of other GL commands.
4235 * The Issues section in the ARB_multi_bind spec says:
4237 * "(11) Typically, OpenGL specifies that if an error is generated by a
4238 * command, that command has no effect. This is somewhat
4239 * unfortunate for multi-bind commands, because it would require a
4240 * first pass to scan the entire list of bound objects for errors
4241 * and then a second pass to actually perform the bindings.
4242 * Should we have different error semantics?
4244 * RESOLVED: Yes. In this specification, when the parameters for
4245 * one of the <count> binding points are invalid, that binding point
4246 * is not updated and an error will be generated. However, other
4247 * binding points in the same command will be updated if their
4248 * parameters are valid and no other error occurs."
4251 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4253 for (int i
= 0; i
< count
; i
++) {
4254 struct gl_buffer_binding
*binding
=
4255 &ctx
->AtomicBufferBindings
[first
+ i
];
4256 struct gl_buffer_object
*bufObj
;
4257 GLintptr offset
= 0;
4258 GLsizeiptr size
= 0;
4261 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4264 /* The ARB_multi_bind spec says:
4266 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4267 * pair of values in <offsets> and <sizes> does not respectively
4268 * satisfy the constraints described for those parameters for the
4269 * specified target, as described in section 6.7.1 (per binding)."
4271 * Section 6.7.1 refers to table 6.5, which says:
4273 * "┌───────────────────────────────────────────────────────────────┐
4274 * │ Atomic counter array bindings (see sec. 7.7.2) │
4275 * ├───────────────────────┬───────────────────────────────────────┤
4277 * │ offset restriction │ multiple of 4 │
4279 * │ size restriction │ none │
4280 * └───────────────────────┴───────────────────────────────────────┘"
4282 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
4283 _mesa_error(ctx
, GL_INVALID_VALUE
,
4284 "glBindBuffersRange(offsets[%u]=%" PRId64
4285 " is misaligned; it must be a multiple of %d when "
4286 "target=GL_ATOMIC_COUNTER_BUFFER)",
4287 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
4291 offset
= offsets
[i
];
4295 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
4296 bufObj
= binding
->BufferObject
;
4298 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
4301 if (bufObj
== ctx
->Shared
->NullBufferObj
)
4302 set_atomic_buffer_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
4304 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
4307 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4310 static ALWAYS_INLINE
void
4311 bind_buffer_range(GLenum target
, GLuint index
, GLuint buffer
, GLintptr offset
,
4312 GLsizeiptr size
, bool no_error
)
4314 GET_CURRENT_CONTEXT(ctx
);
4315 struct gl_buffer_object
*bufObj
;
4317 if (MESA_VERBOSE
& VERBOSE_API
) {
4318 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
4319 _mesa_enum_to_string(target
), index
, buffer
,
4320 (unsigned long) offset
, (unsigned long) size
);
4324 bufObj
= ctx
->Shared
->NullBufferObj
;
4326 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4327 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4328 &bufObj
, "glBindBufferRange"))
4331 if (!no_error
&& !bufObj
) {
4332 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4333 "glBindBufferRange(invalid buffer=%u)", buffer
);
4340 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4341 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4342 index
, bufObj
, offset
, size
);
4344 case GL_UNIFORM_BUFFER
:
4345 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
4347 case GL_SHADER_STORAGE_BUFFER
:
4348 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
,
4351 case GL_ATOMIC_COUNTER_BUFFER
:
4352 bind_buffer_range_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
4355 unreachable("invalid BindBufferRange target with KHR_no_error");
4360 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
4367 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4368 if (!_mesa_validate_buffer_range_xfb(ctx
,
4369 ctx
->TransformFeedback
.CurrentObject
,
4370 index
, bufObj
, offset
, size
,
4374 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4375 index
, bufObj
, offset
, size
);
4377 case GL_UNIFORM_BUFFER
:
4378 bind_buffer_range_uniform_buffer_err(ctx
, index
, bufObj
, offset
,
4381 case GL_SHADER_STORAGE_BUFFER
:
4382 bind_buffer_range_shader_storage_buffer_err(ctx
, index
, bufObj
,
4385 case GL_ATOMIC_COUNTER_BUFFER
:
4386 bind_buffer_range_atomic_buffer_err(ctx
, index
, bufObj
,
4390 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
4397 _mesa_BindBufferRange_no_error(GLenum target
, GLuint index
, GLuint buffer
,
4398 GLintptr offset
, GLsizeiptr size
)
4400 bind_buffer_range(target
, index
, buffer
, offset
, size
, true);
4404 _mesa_BindBufferRange(GLenum target
, GLuint index
,
4405 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
4407 bind_buffer_range(target
, index
, buffer
, offset
, size
, false);
4411 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
4413 GET_CURRENT_CONTEXT(ctx
);
4414 struct gl_buffer_object
*bufObj
;
4416 if (MESA_VERBOSE
& VERBOSE_API
) {
4417 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
4418 _mesa_enum_to_string(target
), index
, buffer
);
4422 bufObj
= ctx
->Shared
->NullBufferObj
;
4424 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4425 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4426 &bufObj
, "glBindBufferBase"))
4430 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4431 "glBindBufferBase(invalid buffer=%u)", buffer
);
4436 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4437 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4439 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4440 * zero and size equal to the size of buffer."
4442 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4444 * "If the parameter (starting offset or size) was not specified when the
4445 * buffer object was bound, zero is returned."
4447 * What happens if the size of the buffer changes? Does the size of the
4448 * buffer at the moment glBindBufferBase was called still play a role, like
4449 * the first quote would imply, or is the size meaningless in the
4450 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4451 * core spec page 45 says:
4453 * "It is equivalent to calling BindBufferRange with offset zero, while
4454 * size is determined by the size of the bound buffer at the time the
4457 * My interpretation is that the GL 4.1 spec was a clarification of the
4458 * behavior, not a change. In particular, this choice will only make
4459 * rendering work in cases where it would have had undefined results.
4463 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4464 _mesa_bind_buffer_base_transform_feedback(ctx
,
4465 ctx
->TransformFeedback
.CurrentObject
,
4466 index
, bufObj
, false);
4468 case GL_UNIFORM_BUFFER
:
4469 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4471 case GL_SHADER_STORAGE_BUFFER
:
4472 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4474 case GL_ATOMIC_COUNTER_BUFFER
:
4475 bind_buffer_base_atomic_buffer(ctx
, index
, bufObj
);
4478 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4484 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4485 const GLuint
*buffers
,
4486 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4488 GET_CURRENT_CONTEXT(ctx
);
4490 if (MESA_VERBOSE
& VERBOSE_API
) {
4491 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4492 _mesa_enum_to_string(target
), first
, count
,
4493 buffers
, offsets
, sizes
);
4497 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4498 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4499 "glBindBuffersRange");
4501 case GL_UNIFORM_BUFFER
:
4502 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4503 "glBindBuffersRange");
4505 case GL_SHADER_STORAGE_BUFFER
:
4506 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4507 "glBindBuffersRange");
4509 case GL_ATOMIC_COUNTER_BUFFER
:
4510 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4511 "glBindBuffersRange");
4514 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4515 _mesa_enum_to_string(target
));
4521 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4522 const GLuint
*buffers
)
4524 GET_CURRENT_CONTEXT(ctx
);
4526 if (MESA_VERBOSE
& VERBOSE_API
) {
4527 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
4528 _mesa_enum_to_string(target
), first
, count
, buffers
);
4532 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4533 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4534 "glBindBuffersBase");
4536 case GL_UNIFORM_BUFFER
:
4537 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4538 "glBindBuffersBase");
4540 case GL_SHADER_STORAGE_BUFFER
:
4541 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4542 "glBindBuffersBase");
4544 case GL_ATOMIC_COUNTER_BUFFER
:
4545 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4546 "glBindBuffersBase");
4549 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4550 _mesa_enum_to_string(target
));
4555 static ALWAYS_INLINE
void
4556 invalidate_buffer_subdata(struct gl_context
*ctx
,
4557 struct gl_buffer_object
*bufObj
, GLintptr offset
,
4560 if (ctx
->Driver
.InvalidateBufferSubData
)
4561 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4565 _mesa_InvalidateBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
4568 GET_CURRENT_CONTEXT(ctx
);
4570 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4571 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4575 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4578 GET_CURRENT_CONTEXT(ctx
);
4579 struct gl_buffer_object
*bufObj
;
4580 const GLintptr end
= offset
+ length
;
4582 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4583 * Profile) spec says:
4585 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4586 * name of an existing buffer object."
4588 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4589 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4590 _mesa_error(ctx
, GL_INVALID_VALUE
,
4591 "glInvalidateBufferSubData(name = %u) invalid object",
4596 /* The GL_ARB_invalidate_subdata spec says:
4598 * "An INVALID_VALUE error is generated if <offset> or <length> is
4599 * negative, or if <offset> + <length> is greater than the value of
4602 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
4603 _mesa_error(ctx
, GL_INVALID_VALUE
,
4604 "glInvalidateBufferSubData(invalid offset or length)");
4608 /* The OpenGL 4.4 (Core Profile) spec says:
4610 * "An INVALID_OPERATION error is generated if buffer is currently
4611 * mapped by MapBuffer or if the invalidate range intersects the range
4612 * currently mapped by MapBufferRange, unless it was mapped
4613 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4615 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4616 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4617 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4618 "glInvalidateBufferSubData(intersection with mapped "
4623 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4627 _mesa_InvalidateBufferData_no_error(GLuint buffer
)
4629 GET_CURRENT_CONTEXT(ctx
);
4631 struct gl_buffer_object
*bufObj
=_mesa_lookup_bufferobj(ctx
, buffer
);
4632 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4636 _mesa_InvalidateBufferData(GLuint buffer
)
4638 GET_CURRENT_CONTEXT(ctx
);
4639 struct gl_buffer_object
*bufObj
;
4641 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4642 * Profile) spec says:
4644 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4645 * name of an existing buffer object."
4647 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4648 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4649 _mesa_error(ctx
, GL_INVALID_VALUE
,
4650 "glInvalidateBufferData(name = %u) invalid object",
4655 /* The OpenGL 4.4 (Core Profile) spec says:
4657 * "An INVALID_OPERATION error is generated if buffer is currently
4658 * mapped by MapBuffer or if the invalidate range intersects the range
4659 * currently mapped by MapBufferRange, unless it was mapped
4660 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4662 if (_mesa_check_disallowed_mapping(bufObj
)) {
4663 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4664 "glInvalidateBufferData(intersection with mapped "
4669 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4673 buffer_page_commitment(struct gl_context
*ctx
,
4674 struct gl_buffer_object
*bufferObj
,
4675 GLintptr offset
, GLsizeiptr size
,
4676 GLboolean commit
, const char *func
)
4678 if (!(bufferObj
->StorageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)) {
4679 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(not a sparse buffer object)",
4684 if (size
< 0 || size
> bufferObj
->Size
||
4685 offset
< 0 || offset
> bufferObj
->Size
- size
) {
4686 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(out of bounds)",
4691 /* The GL_ARB_sparse_buffer extension specification says:
4693 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4694 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4695 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4696 * not extend to the end of the buffer's data store."
4698 if (offset
% ctx
->Const
.SparseBufferPageSize
!= 0) {
4699 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset not aligned to page size)",
4704 if (size
% ctx
->Const
.SparseBufferPageSize
!= 0 &&
4705 offset
+ size
!= bufferObj
->Size
) {
4706 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size not aligned to page size)",
4711 ctx
->Driver
.BufferPageCommitment(ctx
, bufferObj
, offset
, size
, commit
);
4715 _mesa_BufferPageCommitmentARB(GLenum target
, GLintptr offset
, GLsizeiptr size
,
4718 GET_CURRENT_CONTEXT(ctx
);
4719 struct gl_buffer_object
*bufferObj
;
4721 bufferObj
= get_buffer(ctx
, "glBufferPageCommitmentARB", target
,
4726 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4727 "glBufferPageCommitmentARB");
4731 _mesa_NamedBufferPageCommitmentARB(GLuint buffer
, GLintptr offset
,
4732 GLsizeiptr size
, GLboolean commit
)
4734 GET_CURRENT_CONTEXT(ctx
);
4735 struct gl_buffer_object
*bufferObj
;
4737 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4738 if (!bufferObj
|| bufferObj
== &DummyBufferObject
) {
4739 /* Note: the extension spec is not clear about the excpected error value. */
4740 _mesa_error(ctx
, GL_INVALID_VALUE
,
4741 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4746 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4747 "glNamedBufferPageCommitmentARB");