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"
44 #include "glformats.h"
46 #include "transformfeedback.h"
51 /*#define BOUNDS_CHECK*/
55 * We count the number of buffer modification calls to check for
56 * inefficient buffer use. This is the number of such calls before we
59 #define BUFFER_WARNING_CALL_COUNT 4
63 * Helper to warn of possible performance issues, such as frequently
64 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
68 buffer_usage_warning(struct gl_context
*ctx
, GLuint
*id
, const char *fmt
, ...)
73 _mesa_gl_vdebug(ctx
, id
,
74 MESA_DEBUG_SOURCE_API
,
75 MESA_DEBUG_TYPE_PERFORMANCE
,
76 MESA_DEBUG_SEVERITY_MEDIUM
,
81 #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
83 static GLuint id = 0; \
84 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
89 * Used as a placeholder for buffer objects between glGenBuffers() and
90 * glBindBuffer() so that glIsBuffer() can work correctly.
92 static struct gl_buffer_object DummyBufferObject
;
96 * Return pointer to address of a buffer object target.
97 * \param ctx the GL context
98 * \param target the buffer object target to be retrieved.
99 * \return pointer to pointer to the buffer object bound to \c target in the
100 * specified context or \c NULL if \c target is invalid.
102 static inline struct gl_buffer_object
**
103 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
105 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
107 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
108 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
112 case GL_ARRAY_BUFFER_ARB
:
113 return &ctx
->Array
.ArrayBufferObj
;
114 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
115 return &ctx
->Array
.VAO
->IndexBufferObj
;
116 case GL_PIXEL_PACK_BUFFER_EXT
:
117 return &ctx
->Pack
.BufferObj
;
118 case GL_PIXEL_UNPACK_BUFFER_EXT
:
119 return &ctx
->Unpack
.BufferObj
;
120 case GL_COPY_READ_BUFFER
:
121 return &ctx
->CopyReadBuffer
;
122 case GL_COPY_WRITE_BUFFER
:
123 return &ctx
->CopyWriteBuffer
;
124 case GL_QUERY_BUFFER
:
125 if (_mesa_has_ARB_query_buffer_object(ctx
))
126 return &ctx
->QueryBuffer
;
128 case GL_DRAW_INDIRECT_BUFFER
:
129 if ((ctx
->API
== API_OPENGL_CORE
&&
130 ctx
->Extensions
.ARB_draw_indirect
) ||
131 _mesa_is_gles31(ctx
)) {
132 return &ctx
->DrawIndirectBuffer
;
135 case GL_PARAMETER_BUFFER_ARB
:
136 if (_mesa_has_ARB_indirect_parameters(ctx
)) {
137 return &ctx
->ParameterBuffer
;
140 case GL_DISPATCH_INDIRECT_BUFFER
:
141 if (_mesa_has_compute_shaders(ctx
)) {
142 return &ctx
->DispatchIndirectBuffer
;
145 case GL_TRANSFORM_FEEDBACK_BUFFER
:
146 if (ctx
->Extensions
.EXT_transform_feedback
) {
147 return &ctx
->TransformFeedback
.CurrentBuffer
;
150 case GL_TEXTURE_BUFFER
:
151 if (_mesa_has_ARB_texture_buffer_object(ctx
) ||
152 _mesa_has_OES_texture_buffer(ctx
)) {
153 return &ctx
->Texture
.BufferObject
;
156 case GL_UNIFORM_BUFFER
:
157 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
158 return &ctx
->UniformBuffer
;
161 case GL_SHADER_STORAGE_BUFFER
:
162 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
163 return &ctx
->ShaderStorageBuffer
;
166 case GL_ATOMIC_COUNTER_BUFFER
:
167 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
168 return &ctx
->AtomicBuffer
;
171 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
172 if (ctx
->Extensions
.AMD_pinned_memory
) {
173 return &ctx
->ExternalVirtualMemoryBuffer
;
184 * Get the buffer object bound to the specified target in a GL context.
185 * \param ctx the GL context
186 * \param target the buffer object target to be retrieved.
187 * \param error the GL error to record if target is illegal.
188 * \return pointer to the buffer object bound to \c target in the
189 * specified context or \c NULL if \c target is invalid.
191 static inline struct gl_buffer_object
*
192 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
195 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
198 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
202 if (!_mesa_is_bufferobj(*bufObj
)) {
203 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
212 * Convert a GLbitfield describing the mapped buffer access flags
213 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
216 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
218 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
219 if ((access
& rwFlags
) == rwFlags
)
220 return GL_READ_WRITE
;
221 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
223 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
224 return GL_WRITE_ONLY
;
226 /* Otherwise, AccessFlags is zero (the default state).
228 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
230 * Name Type Initial Value Legal Values
232 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
235 * However, table 6.8 in the GL_OES_mapbuffer extension says:
237 * Get Value Type Get Command Value Description
238 * --------- ---- ----------- ----- -----------
239 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
241 * The difference is because GL_OES_mapbuffer only supports mapping buffers
246 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
251 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
253 * The regions do not overlap if and only if the end of the given
254 * region is before the mapped region or the start of the given region
255 * is after the mapped region.
257 * \param obj Buffer object target on which to operate.
258 * \param offset Offset of the first byte of the subdata range.
259 * \param size Size, in bytes, of the subdata range.
260 * \return true if ranges overlap, false otherwise
264 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
265 GLintptr offset
, GLsizeiptr size
)
267 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
268 const GLintptr end
= offset
+ size
;
269 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
270 obj
->Mappings
[MAP_USER
].Length
;
272 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
281 * Tests the subdata range parameters and sets the GL error code for
282 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
283 * \c glClearBufferSubData.
285 * \param ctx GL context.
286 * \param bufObj The buffer object.
287 * \param offset Offset of the first byte of the subdata range.
288 * \param size Size, in bytes, of the subdata range.
289 * \param mappedRange If true, checks if an overlapping range is mapped.
290 * If false, checks if buffer is mapped.
291 * \param caller Name of calling function for recording errors.
292 * \return false if error, true otherwise
294 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
297 buffer_object_subdata_range_good(struct gl_context
*ctx
,
298 const struct gl_buffer_object
*bufObj
,
299 GLintptr offset
, GLsizeiptr size
,
300 bool mappedRange
, const char *caller
)
303 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
308 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
312 if (offset
+ size
> bufObj
->Size
) {
313 _mesa_error(ctx
, GL_INVALID_VALUE
,
314 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
315 (unsigned long) offset
,
316 (unsigned long) size
,
317 (unsigned long) bufObj
->Size
);
321 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
325 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
326 _mesa_error(ctx
, GL_INVALID_OPERATION
,
327 "%s(range is mapped without persistent bit)",
333 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
334 _mesa_error(ctx
, GL_INVALID_OPERATION
,
335 "%s(buffer is mapped without persistent bit)",
346 * Test the format and type parameters and set the GL error code for
347 * \c glClearBufferData and \c glClearBufferSubData.
349 * \param ctx GL context.
350 * \param internalformat Format to which the data is to be converted.
351 * \param format Format of the supplied data.
352 * \param type Type of the supplied data.
353 * \param caller Name of calling function for recording errors.
354 * \return If internalformat, format and type are legal the mesa_format
355 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
357 * \sa glClearBufferData and glClearBufferSubData
360 validate_clear_buffer_format(struct gl_context
*ctx
,
361 GLenum internalformat
,
362 GLenum format
, GLenum type
,
365 mesa_format mesaFormat
;
366 GLenum errorFormatType
;
368 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
369 if (mesaFormat
== MESA_FORMAT_NONE
) {
370 _mesa_error(ctx
, GL_INVALID_ENUM
,
371 "%s(invalid internalformat)", caller
);
372 return MESA_FORMAT_NONE
;
375 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
376 * EXT_texture_integer there is no conversion between integer and
377 * non-integer formats
379 if (_mesa_is_enum_format_signed_int(format
) !=
380 _mesa_is_format_integer_color(mesaFormat
)) {
381 _mesa_error(ctx
, GL_INVALID_OPERATION
,
382 "%s(integer vs non-integer)", caller
);
383 return MESA_FORMAT_NONE
;
386 if (!_mesa_is_color_format(format
)) {
387 _mesa_error(ctx
, GL_INVALID_ENUM
,
388 "%s(format is not a color format)", caller
);
389 return MESA_FORMAT_NONE
;
392 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
393 if (errorFormatType
!= GL_NO_ERROR
) {
394 _mesa_error(ctx
, GL_INVALID_ENUM
,
395 "%s(invalid format or type)", caller
);
396 return MESA_FORMAT_NONE
;
404 * Convert user-specified clear value to the specified internal format.
406 * \param ctx GL context.
407 * \param internalformat Format to which the data is converted.
408 * \param clearValue Points to the converted clear value.
409 * \param format Format of the supplied data.
410 * \param type Type of the supplied data.
411 * \param data Data which is to be converted to internalformat.
412 * \param caller Name of calling function for recording errors.
413 * \return true if data could be converted, false otherwise.
415 * \sa glClearBufferData, glClearBufferSubData
418 convert_clear_buffer_data(struct gl_context
*ctx
,
419 mesa_format internalformat
,
420 GLubyte
*clearValue
, GLenum format
, GLenum type
,
421 const GLvoid
*data
, const char *caller
)
423 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
425 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
426 0, &clearValue
, 1, 1, 1,
427 format
, type
, data
, &ctx
->Unpack
)) {
431 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
438 * Allocate and initialize a new buffer object.
440 * Default callback for the \c dd_function_table::NewBufferObject() hook.
442 static struct gl_buffer_object
*
443 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
445 struct gl_buffer_object
*obj
;
449 obj
= MALLOC_STRUCT(gl_buffer_object
);
450 _mesa_initialize_buffer_object(ctx
, obj
, name
);
456 * Delete a buffer object.
458 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
461 _mesa_delete_buffer_object(struct gl_context
*ctx
,
462 struct gl_buffer_object
*bufObj
)
466 vbo_delete_minmax_cache(bufObj
);
467 _mesa_align_free(bufObj
->Data
);
469 /* assign strange values here to help w/ debugging */
470 bufObj
->RefCount
= -1000;
473 mtx_destroy(&bufObj
->Mutex
);
481 * Set ptr to bufObj w/ reference counting.
482 * This is normally only called from the _mesa_reference_buffer_object() macro
483 * when there's a real pointer change.
486 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
487 struct gl_buffer_object
**ptr
,
488 struct gl_buffer_object
*bufObj
)
491 /* Unreference the old buffer */
492 GLboolean deleteFlag
= GL_FALSE
;
493 struct gl_buffer_object
*oldObj
= *ptr
;
495 mtx_lock(&oldObj
->Mutex
);
496 assert(oldObj
->RefCount
> 0);
498 deleteFlag
= (oldObj
->RefCount
== 0);
499 mtx_unlock(&oldObj
->Mutex
);
502 assert(ctx
->Driver
.DeleteBuffer
);
503 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
511 /* reference new buffer */
512 mtx_lock(&bufObj
->Mutex
);
513 if (bufObj
->RefCount
== 0) {
514 /* this buffer's being deleted (look just above) */
515 /* Not sure this can every really happen. Warn if it does. */
516 _mesa_problem(NULL
, "referencing deleted buffer object");
523 mtx_unlock(&bufObj
->Mutex
);
529 * Get the value of MESA_NO_MINMAX_CACHE.
532 get_no_minmax_cache()
534 static bool read
= false;
535 static bool disable
= false;
538 disable
= env_var_as_boolean("MESA_NO_MINMAX_CACHE", false);
547 * Initialize a buffer object to default values.
550 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
551 struct gl_buffer_object
*obj
,
554 memset(obj
, 0, sizeof(struct gl_buffer_object
));
555 mtx_init(&obj
->Mutex
, mtx_plain
);
558 obj
->Usage
= GL_STATIC_DRAW_ARB
;
560 if (get_no_minmax_cache())
561 obj
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
567 * Callback called from _mesa_HashWalk()
570 count_buffer_size(GLuint key
, void *data
, void *userData
)
572 const struct gl_buffer_object
*bufObj
=
573 (const struct gl_buffer_object
*) data
;
574 GLuint
*total
= (GLuint
*) userData
;
577 *total
= *total
+ bufObj
->Size
;
582 * Compute total size (in bytes) of all buffer objects for the given context.
583 * For debugging purposes.
586 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
590 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
597 * Allocate space for and store data in a buffer object. Any data that was
598 * previously stored in the buffer object is lost. If \c data is \c NULL,
599 * memory will be allocated, but no copy will occur.
601 * This is the default callback for \c dd_function_table::BufferData()
602 * Note that all GL error checking will have been done already.
604 * \param ctx GL context.
605 * \param target Buffer object target on which to operate.
606 * \param size Size, in bytes, of the new data store.
607 * \param data Pointer to the data to store in the buffer object. This
608 * pointer may be \c NULL.
609 * \param usage Hints about how the data will be used.
610 * \param bufObj Object to be used.
612 * \return GL_TRUE for success, GL_FALSE for failure
613 * \sa glBufferDataARB, dd_function_table::BufferData.
616 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptr size
,
617 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
618 struct gl_buffer_object
*bufObj
)
624 _mesa_align_free( bufObj
->Data
);
626 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
628 bufObj
->Data
= (GLubyte
*) new_data
;
630 bufObj
->Usage
= usage
;
631 bufObj
->StorageFlags
= storageFlags
;
634 memcpy( bufObj
->Data
, data
, size
);
646 * Replace data in a subrange of buffer object. If the data range
647 * specified by \c size + \c offset extends beyond the end of the buffer or
648 * if \c data is \c NULL, no copy is performed.
650 * This is the default callback for \c dd_function_table::BufferSubData()
651 * Note that all GL error checking will have been done already.
653 * \param ctx GL context.
654 * \param offset Offset of the first byte to be modified.
655 * \param size Size, in bytes, of the data range.
656 * \param data Pointer to the data to store in the buffer object.
657 * \param bufObj Object to be used.
659 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
662 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptr offset
,
663 GLsizeiptr size
, const GLvoid
*data
,
664 struct gl_buffer_object
*bufObj
)
668 /* this should have been caught in _mesa_BufferSubData() */
669 assert(size
+ offset
<= bufObj
->Size
);
672 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
678 * Retrieve data from a subrange of buffer object. If the data range
679 * specified by \c size + \c offset extends beyond the end of the buffer or
680 * if \c data is \c NULL, no copy is performed.
682 * This is the default callback for \c dd_function_table::GetBufferSubData()
683 * Note that all GL error checking will have been done already.
685 * \param ctx GL context.
686 * \param target Buffer object target on which to operate.
687 * \param offset Offset of the first byte to be fetched.
688 * \param size Size, in bytes, of the data range.
689 * \param data Destination for data
690 * \param bufObj Object to be used.
692 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
695 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
696 GLsizeiptrARB size
, GLvoid
* data
,
697 struct gl_buffer_object
* bufObj
)
701 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
702 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
708 * Clear a subrange of the buffer object with copies of the supplied data.
709 * If data is NULL the buffer is filled with zeros.
711 * This is the default callback for \c dd_function_table::ClearBufferSubData()
712 * Note that all GL error checking will have been done already.
714 * \param ctx GL context.
715 * \param offset Offset of the first byte to be cleared.
716 * \param size Size, in bytes, of the to be cleared range.
717 * \param clearValue Source of the data.
718 * \param clearValueSize Size, in bytes, of the supplied data.
719 * \param bufObj Object to be cleared.
721 * \sa glClearBufferSubData, glClearBufferData and
722 * dd_function_table::ClearBufferSubData.
725 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
726 GLintptr offset
, GLsizeiptr size
,
727 const GLvoid
*clearValue
,
728 GLsizeiptr clearValueSize
,
729 struct gl_buffer_object
*bufObj
)
734 assert(ctx
->Driver
.MapBufferRange
);
735 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
737 GL_MAP_INVALIDATE_RANGE_BIT
,
738 bufObj
, MAP_INTERNAL
);
741 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
745 if (clearValue
== NULL
) {
746 /* Clear with zeros, per the spec */
747 memset(dest
, 0, size
);
748 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
752 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
753 memcpy(dest
, clearValue
, clearValueSize
);
754 dest
+= clearValueSize
;
757 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
762 * Default fallback for \c dd_function_table::MapBufferRange().
763 * Called via glMapBufferRange().
766 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
767 GLsizeiptr length
, GLbitfield access
,
768 struct gl_buffer_object
*bufObj
,
769 gl_map_buffer_index index
)
772 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
773 /* Just return a direct pointer to the data */
774 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
775 bufObj
->Mappings
[index
].Length
= length
;
776 bufObj
->Mappings
[index
].Offset
= offset
;
777 bufObj
->Mappings
[index
].AccessFlags
= access
;
778 return bufObj
->Mappings
[index
].Pointer
;
783 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
784 * Called via glFlushMappedBufferRange().
787 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
788 GLintptr offset
, GLsizeiptr length
,
789 struct gl_buffer_object
*obj
,
790 gl_map_buffer_index index
)
802 * Default callback for \c dd_function_table::UnmapBuffer().
804 * The input parameters will have been already tested for errors.
806 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
809 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
810 gl_map_buffer_index index
)
813 /* XXX we might assert here that bufObj->Pointer is non-null */
814 bufObj
->Mappings
[index
].Pointer
= NULL
;
815 bufObj
->Mappings
[index
].Length
= 0;
816 bufObj
->Mappings
[index
].Offset
= 0;
817 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
823 * Default fallback for \c dd_function_table::CopyBufferSubData().
824 * Called via glCopyBufferSubData().
827 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
828 struct gl_buffer_object
*src
,
829 struct gl_buffer_object
*dst
,
830 GLintptr readOffset
, GLintptr writeOffset
,
833 GLubyte
*srcPtr
, *dstPtr
;
836 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
838 GL_MAP_WRITE_BIT
, src
,
844 srcPtr
+= readOffset
;
845 dstPtr
+= writeOffset
;
847 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
848 GL_MAP_READ_BIT
, src
,
850 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
852 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
856 /* Note: the src and dst regions will never overlap. Trying to do so
857 * would generate GL_INVALID_VALUE earlier.
859 if (srcPtr
&& dstPtr
)
860 memcpy(dstPtr
, srcPtr
, size
);
862 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
864 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
870 * Initialize the state associated with buffer objects
873 _mesa_init_buffer_objects( struct gl_context
*ctx
)
877 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
878 mtx_init(&DummyBufferObject
.Mutex
, mtx_plain
);
879 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
881 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
882 ctx
->Shared
->NullBufferObj
);
884 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
885 ctx
->Shared
->NullBufferObj
);
886 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
887 ctx
->Shared
->NullBufferObj
);
889 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
890 ctx
->Shared
->NullBufferObj
);
892 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
,
893 ctx
->Shared
->NullBufferObj
);
895 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
896 ctx
->Shared
->NullBufferObj
);
898 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
899 ctx
->Shared
->NullBufferObj
);
901 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
,
902 ctx
->Shared
->NullBufferObj
);
904 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
,
905 ctx
->Shared
->NullBufferObj
);
907 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
,
908 ctx
->Shared
->NullBufferObj
);
910 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
911 _mesa_reference_buffer_object(ctx
,
912 &ctx
->UniformBufferBindings
[i
].BufferObject
,
913 ctx
->Shared
->NullBufferObj
);
914 ctx
->UniformBufferBindings
[i
].Offset
= -1;
915 ctx
->UniformBufferBindings
[i
].Size
= -1;
918 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
919 _mesa_reference_buffer_object(ctx
,
920 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
921 ctx
->Shared
->NullBufferObj
);
922 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
923 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
926 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
927 _mesa_reference_buffer_object(ctx
,
928 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
929 ctx
->Shared
->NullBufferObj
);
930 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
931 ctx
->AtomicBufferBindings
[i
].Size
= 0;
937 _mesa_free_buffer_objects( struct gl_context
*ctx
)
941 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
943 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
944 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
946 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
948 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
950 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
952 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
954 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
, NULL
);
956 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, NULL
);
958 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
, NULL
);
960 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
961 _mesa_reference_buffer_object(ctx
,
962 &ctx
->UniformBufferBindings
[i
].BufferObject
,
966 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
967 _mesa_reference_buffer_object(ctx
,
968 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
972 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
973 _mesa_reference_buffer_object(ctx
,
974 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
981 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
983 struct gl_buffer_object
**buf_handle
,
986 struct gl_buffer_object
*buf
= *buf_handle
;
988 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
)) {
989 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
993 if (!buf
|| buf
== &DummyBufferObject
) {
994 /* If this is a new buffer object id, or one which was generated but
995 * never used before, allocate a buffer object now.
997 assert(ctx
->Driver
.NewBufferObject
);
998 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
1000 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
1003 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
1011 * Bind the specified target to buffer for the specified context.
1012 * Called by glBindBuffer() and other functions.
1015 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
1017 struct gl_buffer_object
*oldBufObj
;
1018 struct gl_buffer_object
*newBufObj
= NULL
;
1019 struct gl_buffer_object
**bindTarget
= NULL
;
1021 bindTarget
= get_buffer_target(ctx
, target
);
1023 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target %s)",
1024 _mesa_enum_to_string(target
));
1028 /* Get pointer to old buffer object (to be unbound) */
1029 oldBufObj
= *bindTarget
;
1030 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
1031 return; /* rebinding the same buffer object- no change */
1034 * Get pointer to new buffer object (newBufObj)
1037 /* The spec says there's not a buffer object named 0, but we use
1038 * one internally because it simplifies things.
1040 newBufObj
= ctx
->Shared
->NullBufferObj
;
1043 /* non-default buffer object */
1044 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1045 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1046 &newBufObj
, "glBindBuffer"))
1050 /* record usage history */
1052 case GL_PIXEL_PACK_BUFFER
:
1053 newBufObj
->UsageHistory
|= USAGE_PIXEL_PACK_BUFFER
;
1059 /* bind new buffer */
1060 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1065 * Update the default buffer objects in the given context to reference those
1066 * specified in the shared state and release those referencing the old
1070 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1072 /* Bind the NullBufferObj to remove references to those
1073 * in the shared context hash table.
1075 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
1076 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
1077 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
1078 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1084 * Return the gl_buffer_object for the given ID.
1085 * Always return NULL for ID 0.
1087 struct gl_buffer_object
*
1088 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1093 return (struct gl_buffer_object
*)
1094 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1098 struct gl_buffer_object
*
1099 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1104 return (struct gl_buffer_object
*)
1105 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1109 * A convenience function for direct state access functions that throws
1110 * GL_INVALID_OPERATION if buffer is not the name of an existing
1113 struct gl_buffer_object
*
1114 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1117 struct gl_buffer_object
*bufObj
;
1119 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1120 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1121 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1122 "%s(non-existent buffer object %u)", caller
, buffer
);
1131 _mesa_begin_bufferobj_lookups(struct gl_context
*ctx
)
1133 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1138 _mesa_end_bufferobj_lookups(struct gl_context
*ctx
)
1140 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1145 * Look up a buffer object for a multi-bind function.
1147 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1148 * of generating an error if the buffer ID is not zero or the name
1149 * of an existing buffer object.
1151 * If the buffer ID refers to an existing buffer object, a pointer
1152 * to the buffer object is returned. If the ID is zero, a pointer
1153 * to the shared NullBufferObj is returned. If the ID is not zero
1154 * and does not refer to a valid buffer object, this function
1157 * This function assumes that the caller has already locked the
1158 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1160 struct gl_buffer_object
*
1161 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1162 const GLuint
*buffers
,
1163 GLuint index
, const char *caller
)
1165 struct gl_buffer_object
*bufObj
;
1167 if (buffers
[index
] != 0) {
1168 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1170 /* The multi-bind functions don't create the buffer objects
1171 when they don't exist. */
1172 if (bufObj
== &DummyBufferObject
)
1175 bufObj
= ctx
->Shared
->NullBufferObj
;
1178 /* The ARB_multi_bind spec says:
1180 * "An INVALID_OPERATION error is generated if any value
1181 * in <buffers> is not zero or the name of an existing
1182 * buffer object (per binding)."
1184 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1185 "%s(buffers[%u]=%u is not zero or the name "
1186 "of an existing buffer object)",
1187 caller
, index
, buffers
[index
]);
1195 * If *ptr points to obj, set ptr = the Null/default buffer object.
1196 * This is a helper for buffer object deletion.
1197 * The GL spec says that deleting a buffer object causes it to get
1198 * unbound from all arrays in the current context.
1201 unbind(struct gl_context
*ctx
,
1202 struct gl_buffer_object
**ptr
,
1203 struct gl_buffer_object
*obj
)
1206 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
1212 * Plug default/fallback buffer object functions into the device
1216 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1218 /* GL_ARB_vertex/pixel_buffer_object */
1219 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1220 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1221 driver
->BufferData
= buffer_data_fallback
;
1222 driver
->BufferSubData
= buffer_sub_data_fallback
;
1223 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
1224 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1226 /* GL_ARB_clear_buffer_object */
1227 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1229 /* GL_ARB_map_buffer_range */
1230 driver
->MapBufferRange
= map_buffer_range_fallback
;
1231 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1233 /* GL_ARB_copy_buffer */
1234 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1239 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1240 struct gl_buffer_object
*bufObj
)
1244 for (i
= 0; i
< MAP_COUNT
; i
++) {
1245 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1246 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1247 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1248 bufObj
->Mappings
[i
].AccessFlags
= 0;
1254 /**********************************************************************/
1256 /**********************************************************************/
1259 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1261 GET_CURRENT_CONTEXT(ctx
);
1263 if (MESA_VERBOSE
& VERBOSE_API
) {
1264 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1265 _mesa_enum_to_string(target
), buffer
);
1268 bind_buffer_object(ctx
, target
, buffer
);
1273 * Delete a set of buffer objects.
1275 * \param n Number of buffer objects to delete.
1276 * \param ids Array of \c n buffer object IDs.
1279 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1281 GET_CURRENT_CONTEXT(ctx
);
1283 FLUSH_VERTICES(ctx
, 0);
1286 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1290 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1292 for (i
= 0; i
< n
; i
++) {
1293 struct gl_buffer_object
*bufObj
=
1294 _mesa_lookup_bufferobj_locked(ctx
, ids
[i
]);
1296 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1299 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1301 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1303 /* unbind any vertex pointers bound to this buffer */
1304 for (j
= 0; j
< ARRAY_SIZE(vao
->VertexBinding
); j
++) {
1305 unbind(ctx
, &vao
->VertexBinding
[j
].BufferObj
, bufObj
);
1308 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1309 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
1311 if (vao
->IndexBufferObj
== bufObj
) {
1312 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1315 /* unbind ARB_draw_indirect binding point */
1316 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1317 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER
, 0 );
1320 /* unbind ARB_indirect_parameters binding point */
1321 if (ctx
->ParameterBuffer
== bufObj
) {
1322 _mesa_BindBuffer(GL_PARAMETER_BUFFER_ARB
, 0);
1325 /* unbind ARB_compute_shader binding point */
1326 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1327 _mesa_BindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, 0);
1330 /* unbind ARB_copy_buffer binding points */
1331 if (ctx
->CopyReadBuffer
== bufObj
) {
1332 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
1334 if (ctx
->CopyWriteBuffer
== bufObj
) {
1335 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
1338 /* unbind transform feedback binding points */
1339 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1340 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
1342 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1343 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1344 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1348 /* unbind UBO binding points */
1349 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1350 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1351 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1355 if (ctx
->UniformBuffer
== bufObj
) {
1356 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
1359 /* unbind SSBO binding points */
1360 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1361 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1362 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER
, j
, 0);
1366 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1367 _mesa_BindBuffer(GL_SHADER_STORAGE_BUFFER
, 0);
1370 /* unbind Atomci Buffer binding points */
1371 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1372 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1373 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1377 if (ctx
->AtomicBuffer
== bufObj
) {
1378 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER
, 0 );
1381 /* unbind any pixel pack/unpack pointers bound to this buffer */
1382 if (ctx
->Pack
.BufferObj
== bufObj
) {
1383 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1385 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1386 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1389 if (ctx
->Texture
.BufferObject
== bufObj
) {
1390 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
1393 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1394 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
, 0);
1397 /* unbind query buffer binding point */
1398 if (ctx
->QueryBuffer
== bufObj
) {
1399 _mesa_BindBuffer(GL_QUERY_BUFFER
, 0);
1402 /* The ID is immediately freed for re-use */
1403 _mesa_HashRemoveLocked(ctx
->Shared
->BufferObjects
, ids
[i
]);
1404 /* Make sure we do not run into the classic ABA problem on bind.
1405 * We don't want to allow re-binding a buffer object that's been
1406 * "deleted" by glDeleteBuffers().
1408 * The explicit rebinding to the default object in the current context
1409 * prevents the above in the current context, but another context
1410 * sharing the same objects might suffer from this problem.
1411 * The alternative would be to do the hash lookup in any case on bind
1412 * which would introduce more runtime overhead than this.
1414 bufObj
->DeletePending
= GL_TRUE
;
1415 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1419 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1424 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1425 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1429 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1431 GET_CURRENT_CONTEXT(ctx
);
1434 struct gl_buffer_object
*buf
;
1436 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1438 if (MESA_VERBOSE
& VERBOSE_API
)
1439 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1442 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1451 * This must be atomic (generation and allocation of buffer object IDs)
1453 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1455 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1457 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1458 * DummyBufferObject. Otherwise, create a new buffer object and insert
1461 for (i
= 0; i
< n
; i
++) {
1462 buffers
[i
] = first
+ i
;
1464 assert(ctx
->Driver
.NewBufferObject
);
1465 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1467 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1468 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1473 buf
= &DummyBufferObject
;
1475 _mesa_HashInsertLocked(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1478 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1482 * Generate a set of unique buffer object IDs and store them in \c buffers.
1484 * \param n Number of IDs to generate.
1485 * \param buffers Array of \c n locations to store the IDs.
1488 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1490 create_buffers(n
, buffers
, false);
1494 * Create a set of buffer objects and store their unique IDs in \c buffers.
1496 * \param n Number of IDs to generate.
1497 * \param buffers Array of \c n locations to store the IDs.
1500 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1502 create_buffers(n
, buffers
, true);
1507 * Determine if ID is the name of a buffer object.
1509 * \param id ID of the potential buffer object.
1510 * \return \c GL_TRUE if \c id is the name of a buffer object,
1511 * \c GL_FALSE otherwise.
1513 GLboolean GLAPIENTRY
1514 _mesa_IsBuffer(GLuint id
)
1516 struct gl_buffer_object
*bufObj
;
1517 GET_CURRENT_CONTEXT(ctx
);
1518 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1520 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1522 return bufObj
&& bufObj
!= &DummyBufferObject
;
1527 _mesa_buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1528 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1529 GLbitfield flags
, const char *func
)
1532 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1536 if (flags
& ~(GL_MAP_READ_BIT
|
1538 GL_MAP_PERSISTENT_BIT
|
1539 GL_MAP_COHERENT_BIT
|
1540 GL_DYNAMIC_STORAGE_BIT
|
1541 GL_CLIENT_STORAGE_BIT
)) {
1542 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1546 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1547 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1548 _mesa_error(ctx
, GL_INVALID_VALUE
,
1549 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1553 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1554 _mesa_error(ctx
, GL_INVALID_VALUE
,
1555 "%s(COHERENT and flags!=PERSISTENT)", func
);
1559 if (bufObj
->Immutable
) {
1560 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1564 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1565 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1567 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1569 bufObj
->Written
= GL_TRUE
;
1570 bufObj
->Immutable
= GL_TRUE
;
1571 bufObj
->MinMaxCacheDirty
= true;
1573 assert(ctx
->Driver
.BufferData
);
1574 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1576 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1577 /* Even though the interaction between AMD_pinned_memory and
1578 * glBufferStorage is not described in the spec, Graham Sellers
1579 * said that it should behave the same as glBufferData.
1581 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1584 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1590 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1593 GET_CURRENT_CONTEXT(ctx
);
1594 struct gl_buffer_object
*bufObj
;
1596 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1600 _mesa_buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
,
1605 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1608 GET_CURRENT_CONTEXT(ctx
);
1609 struct gl_buffer_object
*bufObj
;
1611 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferStorage");
1616 * In direct state access, buffer objects have an unspecified target since
1617 * they are not required to be bound.
1619 _mesa_buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
,
1620 "glNamedBufferStorage");
1625 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1626 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1627 GLenum usage
, const char *func
)
1631 if (MESA_VERBOSE
& VERBOSE_API
) {
1632 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1634 _mesa_enum_to_string(target
),
1635 (long int) size
, data
,
1636 _mesa_enum_to_string(usage
));
1640 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1645 case GL_STREAM_DRAW_ARB
:
1646 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1649 case GL_STATIC_DRAW_ARB
:
1650 case GL_DYNAMIC_DRAW_ARB
:
1654 case GL_STREAM_READ_ARB
:
1655 case GL_STREAM_COPY_ARB
:
1656 case GL_STATIC_READ_ARB
:
1657 case GL_STATIC_COPY_ARB
:
1658 case GL_DYNAMIC_READ_ARB
:
1659 case GL_DYNAMIC_COPY_ARB
:
1660 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1664 valid_usage
= false;
1669 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1670 _mesa_enum_to_string(usage
));
1674 if (bufObj
->Immutable
) {
1675 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1679 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1680 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1682 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1684 bufObj
->Written
= GL_TRUE
;
1685 bufObj
->MinMaxCacheDirty
= true;
1688 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1689 bufObj
->Name
, size
, data
, usage
);
1696 assert(ctx
->Driver
.BufferData
);
1697 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1700 GL_DYNAMIC_STORAGE_BIT
,
1702 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1703 /* From GL_AMD_pinned_memory:
1705 * INVALID_OPERATION is generated by BufferData if <target> is
1706 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1707 * mapped to the GPU address space.
1709 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1712 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1718 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1719 const GLvoid
*data
, GLenum usage
)
1721 GET_CURRENT_CONTEXT(ctx
);
1722 struct gl_buffer_object
*bufObj
;
1724 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1728 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1733 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1736 GET_CURRENT_CONTEXT(ctx
);
1737 struct gl_buffer_object
*bufObj
;
1739 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1743 /* In direct state access, buffer objects have an unspecified target since
1744 * they are not required to be bound.
1746 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1747 "glNamedBufferData");
1752 * Implementation for glBufferSubData and glNamedBufferSubData.
1754 * \param ctx GL context.
1755 * \param bufObj The buffer object.
1756 * \param offset Offset of the first byte of the subdata range.
1757 * \param size Size, in bytes, of the subdata range.
1758 * \param data The data store.
1759 * \param func Name of calling function for recording errors.
1763 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1764 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1767 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1769 /* error already recorded */
1773 if (bufObj
->Immutable
&&
1774 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1775 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1782 bufObj
->NumSubDataCalls
++;
1784 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
1785 bufObj
->Usage
== GL_STATIC_COPY
) &&
1786 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
) {
1787 /* If the application declared the buffer as static draw/copy or stream
1788 * draw, it should not be frequently modified with glBufferSubData.
1790 BUFFER_USAGE_WARNING(ctx
,
1791 "using %s(buffer %u, offset %u, size %u) to "
1792 "update a %s buffer",
1793 func
, bufObj
->Name
, offset
, size
,
1794 _mesa_enum_to_string(bufObj
->Usage
));
1797 bufObj
->Written
= GL_TRUE
;
1798 bufObj
->MinMaxCacheDirty
= true;
1800 assert(ctx
->Driver
.BufferSubData
);
1801 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1805 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1806 GLsizeiptr size
, const GLvoid
*data
)
1808 GET_CURRENT_CONTEXT(ctx
);
1809 struct gl_buffer_object
*bufObj
;
1811 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1815 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1819 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1820 GLsizeiptr size
, const GLvoid
*data
)
1822 GET_CURRENT_CONTEXT(ctx
);
1823 struct gl_buffer_object
*bufObj
;
1825 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1829 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1830 "glNamedBufferSubData");
1835 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1836 GLsizeiptr size
, GLvoid
*data
)
1838 GET_CURRENT_CONTEXT(ctx
);
1839 struct gl_buffer_object
*bufObj
;
1841 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1842 GL_INVALID_OPERATION
);
1846 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1847 "glGetBufferSubData")) {
1851 assert(ctx
->Driver
.GetBufferSubData
);
1852 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1856 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
1857 GLsizeiptr size
, GLvoid
*data
)
1859 GET_CURRENT_CONTEXT(ctx
);
1860 struct gl_buffer_object
*bufObj
;
1862 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1863 "glGetNamedBufferSubData");
1867 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1868 "glGetNamedBufferSubData")) {
1872 assert(ctx
->Driver
.GetBufferSubData
);
1873 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1878 * \param subdata true if caller is *SubData, false if *Data
1881 _mesa_clear_buffer_sub_data(struct gl_context
*ctx
,
1882 struct gl_buffer_object
*bufObj
,
1883 GLenum internalformat
,
1884 GLintptr offset
, GLsizeiptr size
,
1885 GLenum format
, GLenum type
,
1887 const char *func
, bool subdata
)
1889 mesa_format mesaFormat
;
1890 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1891 GLsizeiptr clearValueSize
;
1893 /* This checks for disallowed mappings. */
1894 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1899 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1900 format
, type
, func
);
1902 if (mesaFormat
== MESA_FORMAT_NONE
) {
1906 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1907 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1908 _mesa_error(ctx
, GL_INVALID_VALUE
,
1909 "%s(offset or size is not a multiple of "
1910 "internalformat size)", func
);
1914 /* Bail early. Negative size has already been checked. */
1918 bufObj
->MinMaxCacheDirty
= true;
1921 /* clear to zeros, per the spec */
1922 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1923 NULL
, clearValueSize
, bufObj
);
1927 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1928 format
, type
, data
, func
)) {
1932 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1933 clearValue
, clearValueSize
, bufObj
);
1937 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1938 GLenum type
, const GLvoid
*data
)
1940 GET_CURRENT_CONTEXT(ctx
);
1941 struct gl_buffer_object
*bufObj
;
1943 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1947 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1949 "glClearBufferData", false);
1953 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1954 GLenum format
, GLenum type
, const GLvoid
*data
)
1956 GET_CURRENT_CONTEXT(ctx
);
1957 struct gl_buffer_object
*bufObj
;
1959 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1963 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1965 "glClearNamedBufferData", false);
1970 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1971 GLintptr offset
, GLsizeiptr size
,
1972 GLenum format
, GLenum type
,
1975 GET_CURRENT_CONTEXT(ctx
);
1976 struct gl_buffer_object
*bufObj
;
1978 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1982 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1984 "glClearBufferSubData", true);
1988 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1989 GLintptr offset
, GLsizeiptr size
,
1990 GLenum format
, GLenum type
,
1993 GET_CURRENT_CONTEXT(ctx
);
1994 struct gl_buffer_object
*bufObj
;
1996 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1997 "glClearNamedBufferSubData");
2001 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
2003 "glClearNamedBufferSubData", true);
2008 _mesa_unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2011 GLboolean status
= GL_TRUE
;
2012 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2014 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2015 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2016 "%s(buffer is not mapped)", func
);
2021 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2022 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2024 /* check that last 100 bytes are still = magic value */
2025 for (i
= 0; i
< 100; i
++) {
2026 GLuint pos
= bufObj
->Size
- i
- 1;
2027 if (buf
[pos
] != 123) {
2028 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2029 " at position %d (value = %u)\n",
2037 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2038 GLuint i
, unchanged
= 0;
2039 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2041 /* check which bytes changed */
2042 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2043 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2050 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2051 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2056 status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2057 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2058 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2059 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2060 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2065 GLboolean GLAPIENTRY
2066 _mesa_UnmapBuffer(GLenum target
)
2068 GET_CURRENT_CONTEXT(ctx
);
2069 struct gl_buffer_object
*bufObj
;
2071 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2075 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2078 GLboolean GLAPIENTRY
2079 _mesa_UnmapNamedBuffer(GLuint buffer
)
2081 GET_CURRENT_CONTEXT(ctx
);
2082 struct gl_buffer_object
*bufObj
;
2084 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2088 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2093 get_buffer_parameter(struct gl_context
*ctx
,
2094 struct gl_buffer_object
*bufObj
, GLenum pname
,
2095 GLint64
*params
, const char *func
)
2098 case GL_BUFFER_SIZE_ARB
:
2099 *params
= bufObj
->Size
;
2101 case GL_BUFFER_USAGE_ARB
:
2102 *params
= bufObj
->Usage
;
2104 case GL_BUFFER_ACCESS_ARB
:
2105 *params
= simplified_access_mode(ctx
,
2106 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2108 case GL_BUFFER_MAPPED_ARB
:
2109 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2111 case GL_BUFFER_ACCESS_FLAGS
:
2112 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2114 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2116 case GL_BUFFER_MAP_OFFSET
:
2117 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2119 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2121 case GL_BUFFER_MAP_LENGTH
:
2122 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2124 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2126 case GL_BUFFER_IMMUTABLE_STORAGE
:
2127 if (!ctx
->Extensions
.ARB_buffer_storage
)
2129 *params
= bufObj
->Immutable
;
2131 case GL_BUFFER_STORAGE_FLAGS
:
2132 if (!ctx
->Extensions
.ARB_buffer_storage
)
2134 *params
= bufObj
->StorageFlags
;
2143 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2144 _mesa_enum_to_string(pname
));
2149 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2151 GET_CURRENT_CONTEXT(ctx
);
2152 struct gl_buffer_object
*bufObj
;
2155 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2156 GL_INVALID_OPERATION
);
2160 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2161 "glGetBufferParameteriv"))
2162 return; /* Error already recorded. */
2164 *params
= (GLint
) parameter
;
2168 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2170 GET_CURRENT_CONTEXT(ctx
);
2171 struct gl_buffer_object
*bufObj
;
2174 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2175 GL_INVALID_OPERATION
);
2179 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2180 "glGetBufferParameteri64v"))
2181 return; /* Error already recorded. */
2183 *params
= parameter
;
2187 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2189 GET_CURRENT_CONTEXT(ctx
);
2190 struct gl_buffer_object
*bufObj
;
2193 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2194 "glGetNamedBufferParameteriv");
2198 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2199 "glGetNamedBufferParameteriv"))
2200 return; /* Error already recorded. */
2202 *params
= (GLint
) parameter
;
2206 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2209 GET_CURRENT_CONTEXT(ctx
);
2210 struct gl_buffer_object
*bufObj
;
2213 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2214 "glGetNamedBufferParameteri64v");
2218 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2219 "glGetNamedBufferParameteri64v"))
2220 return; /* Error already recorded. */
2222 *params
= parameter
;
2227 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2229 GET_CURRENT_CONTEXT(ctx
);
2230 struct gl_buffer_object
*bufObj
;
2232 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2233 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2234 "GL_BUFFER_MAP_POINTER)");
2238 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2239 GL_INVALID_OPERATION
);
2243 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2247 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2249 GET_CURRENT_CONTEXT(ctx
);
2250 struct gl_buffer_object
*bufObj
;
2252 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2253 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2254 "GL_BUFFER_MAP_POINTER)");
2258 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2259 "glGetNamedBufferPointerv");
2263 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2268 _mesa_copy_buffer_sub_data(struct gl_context
*ctx
,
2269 struct gl_buffer_object
*src
,
2270 struct gl_buffer_object
*dst
,
2271 GLintptr readOffset
, GLintptr writeOffset
,
2272 GLsizeiptr size
, const char *func
)
2274 if (_mesa_check_disallowed_mapping(src
)) {
2275 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2276 "%s(readBuffer is mapped)", func
);
2280 if (_mesa_check_disallowed_mapping(dst
)) {
2281 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2282 "%s(writeBuffer is mapped)", func
);
2286 if (readOffset
< 0) {
2287 _mesa_error(ctx
, GL_INVALID_VALUE
,
2288 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2292 if (writeOffset
< 0) {
2293 _mesa_error(ctx
, GL_INVALID_VALUE
,
2294 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2299 _mesa_error(ctx
, GL_INVALID_VALUE
,
2300 "%s(size %d < 0)", func
, (int) size
);
2304 if (readOffset
+ size
> src
->Size
) {
2305 _mesa_error(ctx
, GL_INVALID_VALUE
,
2306 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2307 (int) readOffset
, (int) size
, (int) src
->Size
);
2311 if (writeOffset
+ size
> dst
->Size
) {
2312 _mesa_error(ctx
, GL_INVALID_VALUE
,
2313 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2314 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2319 if (readOffset
+ size
<= writeOffset
) {
2322 else if (writeOffset
+ size
<= readOffset
) {
2326 /* overlapping src/dst is illegal */
2327 _mesa_error(ctx
, GL_INVALID_VALUE
,
2328 "%s(overlapping src/dst)", func
);
2333 dst
->MinMaxCacheDirty
= true;
2335 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2339 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2340 GLintptr readOffset
, GLintptr writeOffset
,
2343 GET_CURRENT_CONTEXT(ctx
);
2344 struct gl_buffer_object
*src
, *dst
;
2346 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2347 GL_INVALID_OPERATION
);
2351 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2352 GL_INVALID_OPERATION
);
2356 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2357 "glCopyBufferSubData");
2361 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2362 GLintptr readOffset
, GLintptr writeOffset
,
2365 GET_CURRENT_CONTEXT(ctx
);
2366 struct gl_buffer_object
*src
, *dst
;
2368 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2369 "glCopyNamedBufferSubData");
2373 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2374 "glCopyNamedBufferSubData");
2378 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2379 "glCopyNamedBufferSubData");
2384 _mesa_map_buffer_range(struct gl_context
*ctx
,
2385 struct gl_buffer_object
*bufObj
,
2386 GLintptr offset
, GLsizeiptr length
,
2387 GLbitfield access
, const char *func
)
2390 GLbitfield allowed_access
;
2392 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
2395 _mesa_error(ctx
, GL_INVALID_VALUE
,
2396 "%s(offset %ld < 0)", func
, (long) offset
);
2401 _mesa_error(ctx
, GL_INVALID_VALUE
,
2402 "%s(length %ld < 0)", func
, (long) length
);
2406 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2408 * "An INVALID_OPERATION error is generated for any of the following
2411 * * <length> is zero."
2413 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2414 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2418 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2422 allowed_access
= GL_MAP_READ_BIT
|
2424 GL_MAP_INVALIDATE_RANGE_BIT
|
2425 GL_MAP_INVALIDATE_BUFFER_BIT
|
2426 GL_MAP_FLUSH_EXPLICIT_BIT
|
2427 GL_MAP_UNSYNCHRONIZED_BIT
;
2429 if (ctx
->Extensions
.ARB_buffer_storage
) {
2430 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2431 GL_MAP_COHERENT_BIT
;
2434 if (access
& ~allowed_access
) {
2435 /* generate an error if any bits other than those allowed are set */
2436 _mesa_error(ctx
, GL_INVALID_VALUE
,
2437 "%s(access has undefined bits set)", func
);
2441 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2442 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2443 "%s(access indicates neither read or write)", func
);
2447 if ((access
& GL_MAP_READ_BIT
) &&
2448 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2449 GL_MAP_INVALIDATE_BUFFER_BIT
|
2450 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2451 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2452 "%s(read access with disallowed bits)", func
);
2456 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2457 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2458 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2459 "%s(access has flush explicit without write)", func
);
2463 if (access
& GL_MAP_READ_BIT
&&
2464 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2465 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2466 "%s(buffer does not allow read access)", func
);
2470 if (access
& GL_MAP_WRITE_BIT
&&
2471 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2472 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2473 "%s(buffer does not allow write access)", func
);
2477 if (access
& GL_MAP_COHERENT_BIT
&&
2478 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2479 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2480 "%s(buffer does not allow coherent access)", func
);
2484 if (access
& GL_MAP_PERSISTENT_BIT
&&
2485 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2486 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2487 "%s(buffer does not allow persistent access)", func
);
2491 if (offset
+ length
> bufObj
->Size
) {
2492 _mesa_error(ctx
, GL_INVALID_VALUE
,
2493 "%s(offset %lu + length %lu > buffer_size %lu)", func
,
2494 (unsigned long) offset
, (unsigned long) length
,
2495 (unsigned long) bufObj
->Size
);
2499 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2500 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2501 "%s(buffer already mapped)", func
);
2505 if (!bufObj
->Size
) {
2506 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2510 if (access
& GL_MAP_WRITE_BIT
) {
2511 bufObj
->NumMapBufferWriteCalls
++;
2512 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2513 bufObj
->Usage
== GL_STATIC_COPY
) &&
2514 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
2515 BUFFER_USAGE_WARNING(ctx
,
2516 "using %s(buffer %u, offset %u, length %u) to "
2517 "update a %s buffer",
2518 func
, bufObj
->Name
, offset
, length
,
2519 _mesa_enum_to_string(bufObj
->Usage
));
2523 assert(ctx
->Driver
.MapBufferRange
);
2524 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2527 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2530 /* The driver callback should have set all these fields.
2531 * This is important because other modules (like VBO) might call
2532 * the driver function directly.
2534 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2535 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2536 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2537 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2540 if (access
& GL_MAP_WRITE_BIT
) {
2541 bufObj
->Written
= GL_TRUE
;
2542 bufObj
->MinMaxCacheDirty
= true;
2546 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2547 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2548 bufObj
->Name
, bufObj
->Size
, access
);
2549 /* Access must be write only */
2550 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2552 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2553 for (i
= 0; i
< bufObj
->Size
; i
++)
2560 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2561 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2563 /* buffer is 100 bytes larger than requested, fill with magic value */
2564 for (i
= 0; i
< 100; i
++) {
2565 buf
[bufObj
->Size
- i
- 1] = 123;
2574 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2577 GET_CURRENT_CONTEXT(ctx
);
2578 struct gl_buffer_object
*bufObj
;
2580 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2581 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2582 "glMapBufferRange(ARB_map_buffer_range not supported)");
2586 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2590 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2591 "glMapBufferRange");
2595 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2598 GET_CURRENT_CONTEXT(ctx
);
2599 struct gl_buffer_object
*bufObj
;
2601 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2602 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2603 "glMapNamedBufferRange("
2604 "ARB_map_buffer_range not supported)");
2608 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2612 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2613 "glMapNamedBufferRange");
2617 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2618 * flags for input to _mesa_map_buffer_range.
2620 * \return true if the type of requested access is permissible.
2623 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2627 case GL_READ_ONLY_ARB
:
2628 *flags
= GL_MAP_READ_BIT
;
2629 return _mesa_is_desktop_gl(ctx
);
2630 case GL_WRITE_ONLY_ARB
:
2631 *flags
= GL_MAP_WRITE_BIT
;
2633 case GL_READ_WRITE_ARB
:
2634 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2635 return _mesa_is_desktop_gl(ctx
);
2642 _mesa_MapBuffer(GLenum target
, GLenum access
)
2644 GET_CURRENT_CONTEXT(ctx
);
2645 struct gl_buffer_object
*bufObj
;
2646 GLbitfield accessFlags
;
2648 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2649 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2653 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2657 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2662 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2664 GET_CURRENT_CONTEXT(ctx
);
2665 struct gl_buffer_object
*bufObj
;
2666 GLbitfield accessFlags
;
2668 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2669 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2673 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2677 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2678 "glMapNamedBuffer");
2683 _mesa_flush_mapped_buffer_range(struct gl_context
*ctx
,
2684 struct gl_buffer_object
*bufObj
,
2685 GLintptr offset
, GLsizeiptr length
,
2688 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2689 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2690 "%s(ARB_map_buffer_range not supported)", func
);
2695 _mesa_error(ctx
, GL_INVALID_VALUE
,
2696 "%s(offset %ld < 0)", func
, (long) offset
);
2701 _mesa_error(ctx
, GL_INVALID_VALUE
,
2702 "%s(length %ld < 0)", func
, (long) length
);
2706 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2707 /* buffer is not mapped */
2708 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2709 "%s(buffer is not mapped)", func
);
2713 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2714 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2715 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2716 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2720 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2721 _mesa_error(ctx
, GL_INVALID_VALUE
,
2722 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2723 (long) offset
, (long) length
,
2724 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2728 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2730 if (ctx
->Driver
.FlushMappedBufferRange
)
2731 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2736 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2739 GET_CURRENT_CONTEXT(ctx
);
2740 struct gl_buffer_object
*bufObj
;
2742 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2743 GL_INVALID_OPERATION
);
2747 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2748 "glFlushMappedBufferRange");
2752 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2755 GET_CURRENT_CONTEXT(ctx
);
2756 struct gl_buffer_object
*bufObj
;
2758 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2759 "glFlushMappedNamedBufferRange");
2763 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2764 "glFlushMappedNamedBufferRange");
2769 * Binds a buffer object to a uniform buffer binding point.
2771 * The caller is responsible for flushing vertices and updating
2775 set_ubo_binding(struct gl_context
*ctx
,
2776 struct gl_uniform_buffer_binding
*binding
,
2777 struct gl_buffer_object
*bufObj
,
2782 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2784 binding
->Offset
= offset
;
2785 binding
->Size
= size
;
2786 binding
->AutomaticSize
= autoSize
;
2788 /* If this is a real buffer object, mark it has having been used
2789 * at some point as a UBO.
2792 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
2796 * Binds a buffer object to a shader storage buffer binding point.
2798 * The caller is responsible for flushing vertices and updating
2802 set_ssbo_binding(struct gl_context
*ctx
,
2803 struct gl_shader_storage_buffer_binding
*binding
,
2804 struct gl_buffer_object
*bufObj
,
2809 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2811 binding
->Offset
= offset
;
2812 binding
->Size
= size
;
2813 binding
->AutomaticSize
= autoSize
;
2815 /* If this is a real buffer object, mark it has having been used
2816 * at some point as a SSBO.
2819 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
2823 * Binds a buffer object to a uniform buffer binding point.
2825 * Unlike set_ubo_binding(), this function also flushes vertices
2826 * and updates NewDriverState. It also checks if the binding
2827 * has actually changed before updating it.
2830 bind_uniform_buffer(struct gl_context
*ctx
,
2832 struct gl_buffer_object
*bufObj
,
2837 struct gl_uniform_buffer_binding
*binding
=
2838 &ctx
->UniformBufferBindings
[index
];
2840 if (binding
->BufferObject
== bufObj
&&
2841 binding
->Offset
== offset
&&
2842 binding
->Size
== size
&&
2843 binding
->AutomaticSize
== autoSize
) {
2847 FLUSH_VERTICES(ctx
, 0);
2848 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
2850 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2854 * Binds a buffer object to a shader storage buffer binding point.
2856 * Unlike set_ssbo_binding(), this function also flushes vertices
2857 * and updates NewDriverState. It also checks if the binding
2858 * has actually changed before updating it.
2861 bind_shader_storage_buffer(struct gl_context
*ctx
,
2863 struct gl_buffer_object
*bufObj
,
2868 struct gl_shader_storage_buffer_binding
*binding
=
2869 &ctx
->ShaderStorageBufferBindings
[index
];
2871 if (binding
->BufferObject
== bufObj
&&
2872 binding
->Offset
== offset
&&
2873 binding
->Size
== size
&&
2874 binding
->AutomaticSize
== autoSize
) {
2878 FLUSH_VERTICES(ctx
, 0);
2879 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
2881 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2885 * Bind a region of a buffer object to a uniform block binding point.
2886 * \param index the uniform buffer binding point index
2887 * \param bufObj the buffer object
2888 * \param offset offset to the start of buffer object region
2889 * \param size size of the buffer object region
2892 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2894 struct gl_buffer_object
*bufObj
,
2898 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2899 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2903 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2904 _mesa_error(ctx
, GL_INVALID_VALUE
,
2905 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2906 ctx
->Const
.UniformBufferOffsetAlignment
);
2910 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2915 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2916 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2920 * Bind a region of a buffer object to a shader storage block binding point.
2921 * \param index the shader storage buffer binding point index
2922 * \param bufObj the buffer object
2923 * \param offset offset to the start of buffer object region
2924 * \param size size of the buffer object region
2927 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
2929 struct gl_buffer_object
*bufObj
,
2933 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2934 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2938 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
2939 _mesa_error(ctx
, GL_INVALID_VALUE
,
2940 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2941 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
2945 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2950 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2951 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2955 * Bind a buffer object to a uniform block binding point.
2956 * As above, but offset = 0.
2959 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2961 struct gl_buffer_object
*bufObj
)
2963 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2964 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2968 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2970 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2971 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2973 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2977 * Bind a buffer object to a shader storage block binding point.
2978 * As above, but offset = 0.
2981 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
2983 struct gl_buffer_object
*bufObj
)
2985 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2986 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2990 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2992 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2993 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2995 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2999 * Binds a buffer object to an atomic buffer binding point.
3001 * The caller is responsible for validating the offset,
3002 * flushing the vertices and updating NewDriverState.
3005 set_atomic_buffer_binding(struct gl_context
*ctx
,
3006 struct gl_atomic_buffer_binding
*binding
,
3007 struct gl_buffer_object
*bufObj
,
3011 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
3013 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3014 binding
->Offset
= 0;
3017 binding
->Offset
= offset
;
3018 binding
->Size
= size
;
3019 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
3024 * Binds a buffer object to an atomic buffer binding point.
3026 * Unlike set_atomic_buffer_binding(), this function also validates the
3027 * index and offset, flushes vertices, and updates NewDriverState.
3028 * It also checks if the binding has actually changing before
3032 bind_atomic_buffer(struct gl_context
*ctx
,
3034 struct gl_buffer_object
*bufObj
,
3039 struct gl_atomic_buffer_binding
*binding
;
3041 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3042 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
3046 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3047 _mesa_error(ctx
, GL_INVALID_VALUE
,
3048 "%s(offset misaligned %d/%d)", name
, (int) offset
,
3049 ATOMIC_COUNTER_SIZE
);
3053 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3055 binding
= &ctx
->AtomicBufferBindings
[index
];
3056 if (binding
->BufferObject
== bufObj
&&
3057 binding
->Offset
== offset
&&
3058 binding
->Size
== size
) {
3062 FLUSH_VERTICES(ctx
, 0);
3063 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3065 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3069 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3071 const GLintptr
*offsets
,
3072 const GLsizeiptr
*sizes
)
3074 if (offsets
[index
] < 0) {
3075 /* The ARB_multi_bind spec says:
3077 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3078 * value in <offsets> is less than zero (per binding)."
3080 _mesa_error(ctx
, GL_INVALID_VALUE
,
3081 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3082 index
, (int64_t) offsets
[index
]);
3086 if (sizes
[index
] <= 0) {
3087 /* The ARB_multi_bind spec says:
3089 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3090 * value in <sizes> is less than or equal to zero (per binding)."
3092 _mesa_error(ctx
, GL_INVALID_VALUE
,
3093 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3094 index
, (int64_t) sizes
[index
]);
3102 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3103 GLuint first
, GLsizei count
,
3106 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3107 _mesa_error(ctx
, GL_INVALID_ENUM
,
3108 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3112 /* The ARB_multi_bind_spec says:
3114 * "An INVALID_OPERATION error is generated if <first> + <count> is
3115 * greater than the number of target-specific indexed binding points,
3116 * as described in section 6.7.1."
3118 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3119 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3120 "%s(first=%u + count=%d > the value of "
3121 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3122 caller
, first
, count
,
3123 ctx
->Const
.MaxUniformBufferBindings
);
3131 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3132 GLuint first
, GLsizei count
,
3135 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3136 _mesa_error(ctx
, GL_INVALID_ENUM
,
3137 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3141 /* The ARB_multi_bind_spec says:
3143 * "An INVALID_OPERATION error is generated if <first> + <count> is
3144 * greater than the number of target-specific indexed binding points,
3145 * as described in section 6.7.1."
3147 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3148 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3149 "%s(first=%u + count=%d > the value of "
3150 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3151 caller
, first
, count
,
3152 ctx
->Const
.MaxShaderStorageBufferBindings
);
3160 * Unbind all uniform buffers in the range
3161 * <first> through <first>+<count>-1
3164 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3166 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3169 for (i
= 0; i
< count
; i
++)
3170 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3171 bufObj
, -1, -1, GL_TRUE
);
3175 * Unbind all shader storage buffers in the range
3176 * <first> through <first>+<count>-1
3179 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3182 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3185 for (i
= 0; i
< count
; i
++)
3186 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3187 bufObj
, -1, -1, GL_TRUE
);
3191 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3192 const GLuint
*buffers
,
3194 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3199 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3202 /* Assume that at least one binding will be changed */
3203 FLUSH_VERTICES(ctx
, 0);
3204 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3207 /* The ARB_multi_bind spec says:
3209 * "If <buffers> is NULL, all bindings from <first> through
3210 * <first>+<count>-1 are reset to their unbound (zero) state.
3211 * In this case, the offsets and sizes associated with the
3212 * binding points are set to default values, ignoring
3213 * <offsets> and <sizes>."
3215 unbind_uniform_buffers(ctx
, first
, count
);
3219 /* Note that the error semantics for multi-bind commands differ from
3220 * those of other GL commands.
3222 * The Issues section in the ARB_multi_bind spec says:
3224 * "(11) Typically, OpenGL specifies that if an error is generated by a
3225 * command, that command has no effect. This is somewhat
3226 * unfortunate for multi-bind commands, because it would require a
3227 * first pass to scan the entire list of bound objects for errors
3228 * and then a second pass to actually perform the bindings.
3229 * Should we have different error semantics?
3231 * RESOLVED: Yes. In this specification, when the parameters for
3232 * one of the <count> binding points are invalid, that binding point
3233 * is not updated and an error will be generated. However, other
3234 * binding points in the same command will be updated if their
3235 * parameters are valid and no other error occurs."
3238 _mesa_begin_bufferobj_lookups(ctx
);
3240 for (i
= 0; i
< count
; i
++) {
3241 struct gl_uniform_buffer_binding
*binding
=
3242 &ctx
->UniformBufferBindings
[first
+ i
];
3243 struct gl_buffer_object
*bufObj
;
3244 GLintptr offset
= 0;
3245 GLsizeiptr size
= 0;
3248 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3251 /* The ARB_multi_bind spec says:
3253 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3254 * pair of values in <offsets> and <sizes> does not respectively
3255 * satisfy the constraints described for those parameters for the
3256 * specified target, as described in section 6.7.1 (per binding)."
3258 * Section 6.7.1 refers to table 6.5, which says:
3260 * "┌───────────────────────────────────────────────────────────────┐
3261 * │ Uniform buffer array bindings (see sec. 7.6) │
3262 * ├─────────────────────┬─────────────────────────────────────────┤
3264 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3265 * │ │ OFFSET_ALIGNMENT │
3267 * │ size restriction │ none │
3268 * └─────────────────────┴─────────────────────────────────────────┘"
3270 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3271 _mesa_error(ctx
, GL_INVALID_VALUE
,
3272 "glBindBuffersRange(offsets[%u]=%" PRId64
3273 " is misaligned; it must be a multiple of the value of "
3274 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3275 "target=GL_UNIFORM_BUFFER)",
3276 i
, (int64_t) offsets
[i
],
3277 ctx
->Const
.UniformBufferOffsetAlignment
);
3281 offset
= offsets
[i
];
3285 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3286 bufObj
= binding
->BufferObject
;
3288 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3291 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3292 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3294 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3298 _mesa_end_bufferobj_lookups(ctx
);
3302 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3303 GLsizei count
, const GLuint
*buffers
,
3305 const GLintptr
*offsets
,
3306 const GLsizeiptr
*sizes
,
3311 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3314 /* Assume that at least one binding will be changed */
3315 FLUSH_VERTICES(ctx
, 0);
3316 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3319 /* The ARB_multi_bind spec says:
3321 * "If <buffers> is NULL, all bindings from <first> through
3322 * <first>+<count>-1 are reset to their unbound (zero) state.
3323 * In this case, the offsets and sizes associated with the
3324 * binding points are set to default values, ignoring
3325 * <offsets> and <sizes>."
3327 unbind_shader_storage_buffers(ctx
, first
, count
);
3331 /* Note that the error semantics for multi-bind commands differ from
3332 * those of other GL commands.
3334 * The Issues section in the ARB_multi_bind spec says:
3336 * "(11) Typically, OpenGL specifies that if an error is generated by a
3337 * command, that command has no effect. This is somewhat
3338 * unfortunate for multi-bind commands, because it would require a
3339 * first pass to scan the entire list of bound objects for errors
3340 * and then a second pass to actually perform the bindings.
3341 * Should we have different error semantics?
3343 * RESOLVED: Yes. In this specification, when the parameters for
3344 * one of the <count> binding points are invalid, that binding point
3345 * is not updated and an error will be generated. However, other
3346 * binding points in the same command will be updated if their
3347 * parameters are valid and no other error occurs."
3350 _mesa_begin_bufferobj_lookups(ctx
);
3352 for (i
= 0; i
< count
; i
++) {
3353 struct gl_shader_storage_buffer_binding
*binding
=
3354 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3355 struct gl_buffer_object
*bufObj
;
3356 GLintptr offset
= 0;
3357 GLsizeiptr size
= 0;
3360 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3363 /* The ARB_multi_bind spec says:
3365 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3366 * pair of values in <offsets> and <sizes> does not respectively
3367 * satisfy the constraints described for those parameters for the
3368 * specified target, as described in section 6.7.1 (per binding)."
3370 * Section 6.7.1 refers to table 6.5, which says:
3372 * "┌───────────────────────────────────────────────────────────────┐
3373 * │ Shader storage buffer array bindings (see sec. 7.8) │
3374 * ├─────────────────────┬─────────────────────────────────────────┤
3376 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3377 * │ │ BUFFER_OFFSET_ALIGNMENT │
3379 * │ size restriction │ none │
3380 * └─────────────────────┴─────────────────────────────────────────┘"
3382 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3383 _mesa_error(ctx
, GL_INVALID_VALUE
,
3384 "glBindBuffersRange(offsets[%u]=%" PRId64
3385 " is misaligned; it must be a multiple of the value of "
3386 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3387 "target=GL_SHADER_STORAGE_BUFFER)",
3388 i
, (int64_t) offsets
[i
],
3389 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3393 offset
= offsets
[i
];
3397 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3398 bufObj
= binding
->BufferObject
;
3400 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3403 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3404 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3406 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3410 _mesa_end_bufferobj_lookups(ctx
);
3414 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3415 struct gl_transform_feedback_object
*tfObj
,
3416 GLuint first
, GLsizei count
, const char *caller
)
3418 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3419 _mesa_error(ctx
, GL_INVALID_ENUM
,
3420 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3424 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3426 * "An INVALID_OPERATION error is generated :
3429 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3430 * FEEDBACK_BUFFER and transform feedback is currently active."
3432 * We assume that this is also meant to apply to BindBuffersRange
3433 * and BindBuffersBase.
3435 if (tfObj
->Active
) {
3436 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3437 "%s(Changing transform feedback buffers while "
3438 "transform feedback is active)", caller
);
3442 /* The ARB_multi_bind_spec says:
3444 * "An INVALID_OPERATION error is generated if <first> + <count> is
3445 * greater than the number of target-specific indexed binding points,
3446 * as described in section 6.7.1."
3448 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3449 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3450 "%s(first=%u + count=%d > the value of "
3451 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3452 caller
, first
, count
,
3453 ctx
->Const
.MaxTransformFeedbackBuffers
);
3461 * Unbind all transform feedback buffers in the range
3462 * <first> through <first>+<count>-1
3465 unbind_xfb_buffers(struct gl_context
*ctx
,
3466 struct gl_transform_feedback_object
*tfObj
,
3467 GLuint first
, GLsizei count
)
3469 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3472 for (i
= 0; i
< count
; i
++)
3473 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3478 bind_xfb_buffers(struct gl_context
*ctx
,
3479 GLuint first
, GLsizei count
,
3480 const GLuint
*buffers
,
3482 const GLintptr
*offsets
,
3483 const GLsizeiptr
*sizes
,
3486 struct gl_transform_feedback_object
*tfObj
=
3487 ctx
->TransformFeedback
.CurrentObject
;
3490 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
3493 /* Assume that at least one binding will be changed */
3494 FLUSH_VERTICES(ctx
, 0);
3495 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3498 /* The ARB_multi_bind spec says:
3500 * "If <buffers> is NULL, all bindings from <first> through
3501 * <first>+<count>-1 are reset to their unbound (zero) state.
3502 * In this case, the offsets and sizes associated with the
3503 * binding points are set to default values, ignoring
3504 * <offsets> and <sizes>."
3506 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3510 /* Note that the error semantics for multi-bind commands differ from
3511 * those of other GL commands.
3513 * The Issues section in the ARB_multi_bind spec says:
3515 * "(11) Typically, OpenGL specifies that if an error is generated by a
3516 * command, that command has no effect. This is somewhat
3517 * unfortunate for multi-bind commands, because it would require a
3518 * first pass to scan the entire list of bound objects for errors
3519 * and then a second pass to actually perform the bindings.
3520 * Should we have different error semantics?
3522 * RESOLVED: Yes. In this specification, when the parameters for
3523 * one of the <count> binding points are invalid, that binding point
3524 * is not updated and an error will be generated. However, other
3525 * binding points in the same command will be updated if their
3526 * parameters are valid and no other error occurs."
3529 _mesa_begin_bufferobj_lookups(ctx
);
3531 for (i
= 0; i
< count
; i
++) {
3532 const GLuint index
= first
+ i
;
3533 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3534 struct gl_buffer_object
*bufObj
;
3535 GLintptr offset
= 0;
3536 GLsizeiptr size
= 0;
3539 offset
= offsets
[i
];
3542 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3545 /* The ARB_multi_bind spec says:
3547 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3548 * pair of values in <offsets> and <sizes> does not respectively
3549 * satisfy the constraints described for those parameters for the
3550 * specified target, as described in section 6.7.1 (per binding)."
3552 * Section 6.7.1 refers to table 6.5, which says:
3554 * "┌───────────────────────────────────────────────────────────────┐
3555 * │ Transform feedback array bindings (see sec. 13.2.2) │
3556 * ├───────────────────────┬───────────────────────────────────────┤
3558 * │ offset restriction │ multiple of 4 │
3560 * │ size restriction │ multiple of 4 │
3561 * └───────────────────────┴───────────────────────────────────────┘"
3563 if (offsets
[i
] & 0x3) {
3564 _mesa_error(ctx
, GL_INVALID_VALUE
,
3565 "glBindBuffersRange(offsets[%u]=%" PRId64
3566 " is misaligned; it must be a multiple of 4 when "
3567 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3568 i
, (int64_t) offsets
[i
]);
3572 if (sizes
[i
] & 0x3) {
3573 _mesa_error(ctx
, GL_INVALID_VALUE
,
3574 "glBindBuffersRange(sizes[%u]=%" PRId64
3575 " is misaligned; it must be a multiple of 4 when "
3576 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3577 i
, (int64_t) sizes
[i
]);
3581 offset
= offsets
[i
];
3585 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3586 bufObj
= boundBufObj
;
3588 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3591 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
3595 _mesa_end_bufferobj_lookups(ctx
);
3599 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
3600 GLuint first
, GLsizei count
,
3603 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
3604 _mesa_error(ctx
, GL_INVALID_ENUM
,
3605 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
3609 /* The ARB_multi_bind_spec says:
3611 * "An INVALID_OPERATION error is generated if <first> + <count> is
3612 * greater than the number of target-specific indexed binding points,
3613 * as described in section 6.7.1."
3615 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
3616 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3617 "%s(first=%u + count=%d > the value of "
3618 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3619 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
3627 * Unbind all atomic counter buffers in the range
3628 * <first> through <first>+<count>-1
3631 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3633 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3636 for (i
= 0; i
< count
; i
++)
3637 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
3642 bind_atomic_buffers(struct gl_context
*ctx
,
3645 const GLuint
*buffers
,
3647 const GLintptr
*offsets
,
3648 const GLsizeiptr
*sizes
,
3653 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
3656 /* Assume that at least one binding will be changed */
3657 FLUSH_VERTICES(ctx
, 0);
3658 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3661 /* The ARB_multi_bind spec says:
3663 * "If <buffers> is NULL, all bindings from <first> through
3664 * <first>+<count>-1 are reset to their unbound (zero) state.
3665 * In this case, the offsets and sizes associated with the
3666 * binding points are set to default values, ignoring
3667 * <offsets> and <sizes>."
3669 unbind_atomic_buffers(ctx
, first
, count
);
3673 /* Note that the error semantics for multi-bind commands differ from
3674 * those of other GL commands.
3676 * The Issues section in the ARB_multi_bind spec says:
3678 * "(11) Typically, OpenGL specifies that if an error is generated by a
3679 * command, that command has no effect. This is somewhat
3680 * unfortunate for multi-bind commands, because it would require a
3681 * first pass to scan the entire list of bound objects for errors
3682 * and then a second pass to actually perform the bindings.
3683 * Should we have different error semantics?
3685 * RESOLVED: Yes. In this specification, when the parameters for
3686 * one of the <count> binding points are invalid, that binding point
3687 * is not updated and an error will be generated. However, other
3688 * binding points in the same command will be updated if their
3689 * parameters are valid and no other error occurs."
3692 _mesa_begin_bufferobj_lookups(ctx
);
3694 for (i
= 0; i
< count
; i
++) {
3695 struct gl_atomic_buffer_binding
*binding
=
3696 &ctx
->AtomicBufferBindings
[first
+ i
];
3697 struct gl_buffer_object
*bufObj
;
3698 GLintptr offset
= 0;
3699 GLsizeiptr size
= 0;
3702 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3705 /* The ARB_multi_bind spec says:
3707 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3708 * pair of values in <offsets> and <sizes> does not respectively
3709 * satisfy the constraints described for those parameters for the
3710 * specified target, as described in section 6.7.1 (per binding)."
3712 * Section 6.7.1 refers to table 6.5, which says:
3714 * "┌───────────────────────────────────────────────────────────────┐
3715 * │ Atomic counter array bindings (see sec. 7.7.2) │
3716 * ├───────────────────────┬───────────────────────────────────────┤
3718 * │ offset restriction │ multiple of 4 │
3720 * │ size restriction │ none │
3721 * └───────────────────────┴───────────────────────────────────────┘"
3723 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
3724 _mesa_error(ctx
, GL_INVALID_VALUE
,
3725 "glBindBuffersRange(offsets[%u]=%" PRId64
3726 " is misaligned; it must be a multiple of %d when "
3727 "target=GL_ATOMIC_COUNTER_BUFFER)",
3728 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
3732 offset
= offsets
[i
];
3736 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3737 bufObj
= binding
->BufferObject
;
3739 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3742 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3745 _mesa_end_bufferobj_lookups(ctx
);
3749 _mesa_BindBufferRange(GLenum target
, GLuint index
,
3750 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
3752 GET_CURRENT_CONTEXT(ctx
);
3753 struct gl_buffer_object
*bufObj
;
3755 if (MESA_VERBOSE
& VERBOSE_API
) {
3756 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
3757 _mesa_enum_to_string(target
), index
, buffer
,
3758 (unsigned long) offset
, (unsigned long) size
);
3762 bufObj
= ctx
->Shared
->NullBufferObj
;
3764 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3766 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3767 &bufObj
, "glBindBufferRange"))
3771 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3772 "glBindBufferRange(invalid buffer=%u)", buffer
);
3778 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
3785 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3786 _mesa_bind_buffer_range_transform_feedback(ctx
,
3787 ctx
->TransformFeedback
.CurrentObject
,
3788 index
, bufObj
, offset
, size
,
3791 case GL_UNIFORM_BUFFER
:
3792 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3794 case GL_SHADER_STORAGE_BUFFER
:
3795 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3797 case GL_ATOMIC_COUNTER_BUFFER
:
3798 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
3799 "glBindBufferRange");
3802 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
3808 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
3810 GET_CURRENT_CONTEXT(ctx
);
3811 struct gl_buffer_object
*bufObj
;
3813 if (MESA_VERBOSE
& VERBOSE_API
) {
3814 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
3815 _mesa_enum_to_string(target
), index
, buffer
);
3819 bufObj
= ctx
->Shared
->NullBufferObj
;
3821 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3823 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3824 &bufObj
, "glBindBufferBase"))
3828 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3829 "glBindBufferBase(invalid buffer=%u)", buffer
);
3833 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3834 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3836 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3837 * zero and size equal to the size of buffer."
3839 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3841 * "If the parameter (starting offset or size) was not specified when the
3842 * buffer object was bound, zero is returned."
3844 * What happens if the size of the buffer changes? Does the size of the
3845 * buffer at the moment glBindBufferBase was called still play a role, like
3846 * the first quote would imply, or is the size meaningless in the
3847 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3848 * core spec page 45 says:
3850 * "It is equivalent to calling BindBufferRange with offset zero, while
3851 * size is determined by the size of the bound buffer at the time the
3854 * My interpretation is that the GL 4.1 spec was a clarification of the
3855 * behavior, not a change. In particular, this choice will only make
3856 * rendering work in cases where it would have had undefined results.
3860 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3861 _mesa_bind_buffer_base_transform_feedback(ctx
,
3862 ctx
->TransformFeedback
.CurrentObject
,
3863 index
, bufObj
, false);
3865 case GL_UNIFORM_BUFFER
:
3866 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
3868 case GL_SHADER_STORAGE_BUFFER
:
3869 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
3871 case GL_ATOMIC_COUNTER_BUFFER
:
3872 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
3873 "glBindBufferBase");
3876 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
3882 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
3883 const GLuint
*buffers
,
3884 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3886 GET_CURRENT_CONTEXT(ctx
);
3888 if (MESA_VERBOSE
& VERBOSE_API
) {
3889 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
3890 _mesa_enum_to_string(target
), first
, count
,
3891 buffers
, offsets
, sizes
);
3895 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3896 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3897 "glBindBuffersRange");
3899 case GL_UNIFORM_BUFFER
:
3900 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3901 "glBindBuffersRange");
3903 case GL_SHADER_STORAGE_BUFFER
:
3904 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3905 "glBindBuffersRange");
3907 case GL_ATOMIC_COUNTER_BUFFER
:
3908 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3909 "glBindBuffersRange");
3912 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
3913 _mesa_enum_to_string(target
));
3919 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
3920 const GLuint
*buffers
)
3922 GET_CURRENT_CONTEXT(ctx
);
3924 if (MESA_VERBOSE
& VERBOSE_API
) {
3925 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
3926 _mesa_enum_to_string(target
), first
, count
, buffers
);
3930 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3931 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3932 "glBindBuffersBase");
3934 case GL_UNIFORM_BUFFER
:
3935 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3936 "glBindBuffersBase");
3938 case GL_SHADER_STORAGE_BUFFER
:
3939 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3940 "glBindBuffersBase");
3942 case GL_ATOMIC_COUNTER_BUFFER
:
3943 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3944 "glBindBuffersBase");
3947 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
3948 _mesa_enum_to_string(target
));
3954 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
3957 GET_CURRENT_CONTEXT(ctx
);
3958 struct gl_buffer_object
*bufObj
;
3959 const GLintptr end
= offset
+ length
;
3961 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
3962 * Profile) spec says:
3964 * "An INVALID_VALUE error is generated if buffer is zero or is not the
3965 * name of an existing buffer object."
3967 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3968 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
3969 _mesa_error(ctx
, GL_INVALID_VALUE
,
3970 "glInvalidateBufferSubData(name = %u) invalid object",
3975 /* The GL_ARB_invalidate_subdata spec says:
3977 * "An INVALID_VALUE error is generated if <offset> or <length> is
3978 * negative, or if <offset> + <length> is greater than the value of
3981 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
3982 _mesa_error(ctx
, GL_INVALID_VALUE
,
3983 "glInvalidateBufferSubData(invalid offset or length)");
3987 /* The OpenGL 4.4 (Core Profile) spec says:
3989 * "An INVALID_OPERATION error is generated if buffer is currently
3990 * mapped by MapBuffer or if the invalidate range intersects the range
3991 * currently mapped by MapBufferRange, unless it was mapped
3992 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
3994 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
3995 bufferobj_range_mapped(bufObj
, offset
, length
)) {
3996 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3997 "glInvalidateBufferSubData(intersection with mapped "
4002 if (ctx
->Driver
.InvalidateBufferSubData
)
4003 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4007 _mesa_InvalidateBufferData(GLuint buffer
)
4009 GET_CURRENT_CONTEXT(ctx
);
4010 struct gl_buffer_object
*bufObj
;
4012 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4013 * Profile) spec says:
4015 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4016 * name of an existing buffer object."
4018 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4019 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4020 _mesa_error(ctx
, GL_INVALID_VALUE
,
4021 "glInvalidateBufferData(name = %u) invalid object",
4026 /* The OpenGL 4.4 (Core Profile) spec says:
4028 * "An INVALID_OPERATION error is generated if buffer is currently
4029 * mapped by MapBuffer or if the invalidate range intersects the range
4030 * currently mapped by MapBufferRange, unless it was mapped
4031 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4033 if (_mesa_check_disallowed_mapping(bufObj
)) {
4034 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4035 "glInvalidateBufferData(intersection with mapped "
4040 if (ctx
->Driver
.InvalidateBufferSubData
)
4041 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, 0, bufObj
->Size
);