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 0x%x)", target
);
1027 /* Get pointer to old buffer object (to be unbound) */
1028 oldBufObj
= *bindTarget
;
1029 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
1030 return; /* rebinding the same buffer object- no change */
1033 * Get pointer to new buffer object (newBufObj)
1036 /* The spec says there's not a buffer object named 0, but we use
1037 * one internally because it simplifies things.
1039 newBufObj
= ctx
->Shared
->NullBufferObj
;
1042 /* non-default buffer object */
1043 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1044 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1045 &newBufObj
, "glBindBuffer"))
1049 /* record usage history */
1051 case GL_PIXEL_PACK_BUFFER
:
1052 newBufObj
->UsageHistory
|= USAGE_PIXEL_PACK_BUFFER
;
1058 /* bind new buffer */
1059 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1064 * Update the default buffer objects in the given context to reference those
1065 * specified in the shared state and release those referencing the old
1069 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1071 /* Bind the NullBufferObj to remove references to those
1072 * in the shared context hash table.
1074 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
1075 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
1076 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
1077 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1083 * Return the gl_buffer_object for the given ID.
1084 * Always return NULL for ID 0.
1086 struct gl_buffer_object
*
1087 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1092 return (struct gl_buffer_object
*)
1093 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1097 struct gl_buffer_object
*
1098 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1100 return (struct gl_buffer_object
*)
1101 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1105 * A convenience function for direct state access functions that throws
1106 * GL_INVALID_OPERATION if buffer is not the name of an existing
1109 struct gl_buffer_object
*
1110 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1113 struct gl_buffer_object
*bufObj
;
1115 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1116 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1117 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1118 "%s(non-existent buffer object %u)", caller
, buffer
);
1127 _mesa_begin_bufferobj_lookups(struct gl_context
*ctx
)
1129 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1134 _mesa_end_bufferobj_lookups(struct gl_context
*ctx
)
1136 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1141 * Look up a buffer object for a multi-bind function.
1143 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1144 * of generating an error if the buffer ID is not zero or the name
1145 * of an existing buffer object.
1147 * If the buffer ID refers to an existing buffer object, a pointer
1148 * to the buffer object is returned. If the ID is zero, a pointer
1149 * to the shared NullBufferObj is returned. If the ID is not zero
1150 * and does not refer to a valid buffer object, this function
1153 * This function assumes that the caller has already locked the
1154 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1156 struct gl_buffer_object
*
1157 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1158 const GLuint
*buffers
,
1159 GLuint index
, const char *caller
)
1161 struct gl_buffer_object
*bufObj
;
1163 if (buffers
[index
] != 0) {
1164 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1166 /* The multi-bind functions don't create the buffer objects
1167 when they don't exist. */
1168 if (bufObj
== &DummyBufferObject
)
1171 bufObj
= ctx
->Shared
->NullBufferObj
;
1174 /* The ARB_multi_bind spec says:
1176 * "An INVALID_OPERATION error is generated if any value
1177 * in <buffers> is not zero or the name of an existing
1178 * buffer object (per binding)."
1180 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1181 "%s(buffers[%u]=%u is not zero or the name "
1182 "of an existing buffer object)",
1183 caller
, index
, buffers
[index
]);
1191 * If *ptr points to obj, set ptr = the Null/default buffer object.
1192 * This is a helper for buffer object deletion.
1193 * The GL spec says that deleting a buffer object causes it to get
1194 * unbound from all arrays in the current context.
1197 unbind(struct gl_context
*ctx
,
1198 struct gl_buffer_object
**ptr
,
1199 struct gl_buffer_object
*obj
)
1202 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
1208 * Plug default/fallback buffer object functions into the device
1212 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1214 /* GL_ARB_vertex/pixel_buffer_object */
1215 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1216 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1217 driver
->BufferData
= buffer_data_fallback
;
1218 driver
->BufferSubData
= buffer_sub_data_fallback
;
1219 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
1220 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1222 /* GL_ARB_clear_buffer_object */
1223 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1225 /* GL_ARB_map_buffer_range */
1226 driver
->MapBufferRange
= map_buffer_range_fallback
;
1227 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1229 /* GL_ARB_copy_buffer */
1230 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1235 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1236 struct gl_buffer_object
*bufObj
)
1240 for (i
= 0; i
< MAP_COUNT
; i
++) {
1241 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1242 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1243 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1244 bufObj
->Mappings
[i
].AccessFlags
= 0;
1250 /**********************************************************************/
1252 /**********************************************************************/
1255 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1257 GET_CURRENT_CONTEXT(ctx
);
1259 if (MESA_VERBOSE
& VERBOSE_API
) {
1260 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1261 _mesa_enum_to_string(target
), buffer
);
1264 bind_buffer_object(ctx
, target
, buffer
);
1269 * Delete a set of buffer objects.
1271 * \param n Number of buffer objects to delete.
1272 * \param ids Array of \c n buffer object IDs.
1275 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1277 GET_CURRENT_CONTEXT(ctx
);
1279 FLUSH_VERTICES(ctx
, 0);
1282 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1286 mtx_lock(&ctx
->Shared
->Mutex
);
1288 for (i
= 0; i
< n
; i
++) {
1289 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
1291 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1294 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1296 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1298 /* unbind any vertex pointers bound to this buffer */
1299 for (j
= 0; j
< ARRAY_SIZE(vao
->VertexBinding
); j
++) {
1300 unbind(ctx
, &vao
->VertexBinding
[j
].BufferObj
, bufObj
);
1303 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1304 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
1306 if (vao
->IndexBufferObj
== bufObj
) {
1307 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1310 /* unbind ARB_draw_indirect binding point */
1311 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1312 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER
, 0 );
1315 /* unbind ARB_indirect_parameters binding point */
1316 if (ctx
->ParameterBuffer
== bufObj
) {
1317 _mesa_BindBuffer(GL_PARAMETER_BUFFER_ARB
, 0);
1320 /* unbind ARB_compute_shader binding point */
1321 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1322 _mesa_BindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, 0);
1325 /* unbind ARB_copy_buffer binding points */
1326 if (ctx
->CopyReadBuffer
== bufObj
) {
1327 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
1329 if (ctx
->CopyWriteBuffer
== bufObj
) {
1330 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
1333 /* unbind transform feedback binding points */
1334 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1335 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
1337 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1338 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1339 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1343 /* unbind UBO binding points */
1344 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1345 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1346 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1350 if (ctx
->UniformBuffer
== bufObj
) {
1351 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
1354 /* unbind SSBO binding points */
1355 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1356 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1357 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER
, j
, 0);
1361 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1362 _mesa_BindBuffer(GL_SHADER_STORAGE_BUFFER
, 0);
1365 /* unbind Atomci Buffer binding points */
1366 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1367 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1368 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1372 if (ctx
->AtomicBuffer
== bufObj
) {
1373 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER
, 0 );
1376 /* unbind any pixel pack/unpack pointers bound to this buffer */
1377 if (ctx
->Pack
.BufferObj
== bufObj
) {
1378 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1380 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1381 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1384 if (ctx
->Texture
.BufferObject
== bufObj
) {
1385 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
1388 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1389 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
, 0);
1392 /* unbind query buffer binding point */
1393 if (ctx
->QueryBuffer
== bufObj
) {
1394 _mesa_BindBuffer(GL_QUERY_BUFFER
, 0);
1397 /* The ID is immediately freed for re-use */
1398 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
1399 /* Make sure we do not run into the classic ABA problem on bind.
1400 * We don't want to allow re-binding a buffer object that's been
1401 * "deleted" by glDeleteBuffers().
1403 * The explicit rebinding to the default object in the current context
1404 * prevents the above in the current context, but another context
1405 * sharing the same objects might suffer from this problem.
1406 * The alternative would be to do the hash lookup in any case on bind
1407 * which would introduce more runtime overhead than this.
1409 bufObj
->DeletePending
= GL_TRUE
;
1410 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1414 mtx_unlock(&ctx
->Shared
->Mutex
);
1419 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1420 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1424 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1426 GET_CURRENT_CONTEXT(ctx
);
1429 struct gl_buffer_object
*buf
;
1431 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1433 if (MESA_VERBOSE
& VERBOSE_API
)
1434 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1437 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1446 * This must be atomic (generation and allocation of buffer object IDs)
1448 mtx_lock(&ctx
->Shared
->Mutex
);
1450 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1452 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1453 * DummyBufferObject. Otherwise, create a new buffer object and insert
1456 for (i
= 0; i
< n
; i
++) {
1457 buffers
[i
] = first
+ i
;
1459 assert(ctx
->Driver
.NewBufferObject
);
1460 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1462 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1463 mtx_unlock(&ctx
->Shared
->Mutex
);
1468 buf
= &DummyBufferObject
;
1470 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1473 mtx_unlock(&ctx
->Shared
->Mutex
);
1477 * Generate a set of unique buffer object IDs and store them in \c buffers.
1479 * \param n Number of IDs to generate.
1480 * \param buffers Array of \c n locations to store the IDs.
1483 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1485 create_buffers(n
, buffers
, false);
1489 * Create a set of buffer objects and store their unique IDs in \c buffers.
1491 * \param n Number of IDs to generate.
1492 * \param buffers Array of \c n locations to store the IDs.
1495 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1497 create_buffers(n
, buffers
, true);
1502 * Determine if ID is the name of a buffer object.
1504 * \param id ID of the potential buffer object.
1505 * \return \c GL_TRUE if \c id is the name of a buffer object,
1506 * \c GL_FALSE otherwise.
1508 GLboolean GLAPIENTRY
1509 _mesa_IsBuffer(GLuint id
)
1511 struct gl_buffer_object
*bufObj
;
1512 GET_CURRENT_CONTEXT(ctx
);
1513 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1515 mtx_lock(&ctx
->Shared
->Mutex
);
1516 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1517 mtx_unlock(&ctx
->Shared
->Mutex
);
1519 return bufObj
&& bufObj
!= &DummyBufferObject
;
1524 _mesa_buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1525 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1526 GLbitfield flags
, const char *func
)
1529 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1533 if (flags
& ~(GL_MAP_READ_BIT
|
1535 GL_MAP_PERSISTENT_BIT
|
1536 GL_MAP_COHERENT_BIT
|
1537 GL_DYNAMIC_STORAGE_BIT
|
1538 GL_CLIENT_STORAGE_BIT
)) {
1539 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1543 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1544 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1545 _mesa_error(ctx
, GL_INVALID_VALUE
,
1546 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1550 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1551 _mesa_error(ctx
, GL_INVALID_VALUE
,
1552 "%s(COHERENT and flags!=PERSISTENT)", func
);
1556 if (bufObj
->Immutable
) {
1557 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1561 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1562 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1564 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1566 bufObj
->Written
= GL_TRUE
;
1567 bufObj
->Immutable
= GL_TRUE
;
1568 bufObj
->MinMaxCacheDirty
= true;
1570 assert(ctx
->Driver
.BufferData
);
1571 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1573 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1574 /* Even though the interaction between AMD_pinned_memory and
1575 * glBufferStorage is not described in the spec, Graham Sellers
1576 * said that it should behave the same as glBufferData.
1578 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1581 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1587 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1590 GET_CURRENT_CONTEXT(ctx
);
1591 struct gl_buffer_object
*bufObj
;
1593 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1597 _mesa_buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
,
1602 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1605 GET_CURRENT_CONTEXT(ctx
);
1606 struct gl_buffer_object
*bufObj
;
1608 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferStorage");
1613 * In direct state access, buffer objects have an unspecified target since
1614 * they are not required to be bound.
1616 _mesa_buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
,
1617 "glNamedBufferStorage");
1622 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1623 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1624 GLenum usage
, const char *func
)
1628 if (MESA_VERBOSE
& VERBOSE_API
) {
1629 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1631 _mesa_enum_to_string(target
),
1632 (long int) size
, data
,
1633 _mesa_enum_to_string(usage
));
1637 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1642 case GL_STREAM_DRAW_ARB
:
1643 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1646 case GL_STATIC_DRAW_ARB
:
1647 case GL_DYNAMIC_DRAW_ARB
:
1651 case GL_STREAM_READ_ARB
:
1652 case GL_STREAM_COPY_ARB
:
1653 case GL_STATIC_READ_ARB
:
1654 case GL_STATIC_COPY_ARB
:
1655 case GL_DYNAMIC_READ_ARB
:
1656 case GL_DYNAMIC_COPY_ARB
:
1657 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1661 valid_usage
= false;
1666 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1667 _mesa_enum_to_string(usage
));
1671 if (bufObj
->Immutable
) {
1672 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1676 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1677 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1679 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1681 bufObj
->Written
= GL_TRUE
;
1682 bufObj
->MinMaxCacheDirty
= true;
1685 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1686 bufObj
->Name
, size
, data
, usage
);
1693 assert(ctx
->Driver
.BufferData
);
1694 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1697 GL_DYNAMIC_STORAGE_BIT
,
1699 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1700 /* From GL_AMD_pinned_memory:
1702 * INVALID_OPERATION is generated by BufferData if <target> is
1703 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1704 * mapped to the GPU address space.
1706 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1709 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1715 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1716 const GLvoid
*data
, GLenum usage
)
1718 GET_CURRENT_CONTEXT(ctx
);
1719 struct gl_buffer_object
*bufObj
;
1721 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1725 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1730 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1733 GET_CURRENT_CONTEXT(ctx
);
1734 struct gl_buffer_object
*bufObj
;
1736 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1740 /* In direct state access, buffer objects have an unspecified target since
1741 * they are not required to be bound.
1743 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1744 "glNamedBufferData");
1749 * Implementation for glBufferSubData and glNamedBufferSubData.
1751 * \param ctx GL context.
1752 * \param bufObj The buffer object.
1753 * \param offset Offset of the first byte of the subdata range.
1754 * \param size Size, in bytes, of the subdata range.
1755 * \param data The data store.
1756 * \param func Name of calling function for recording errors.
1760 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1761 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1764 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1766 /* error already recorded */
1770 if (bufObj
->Immutable
&&
1771 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1772 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1779 bufObj
->NumSubDataCalls
++;
1781 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
1782 bufObj
->Usage
== GL_STATIC_COPY
) &&
1783 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
) {
1784 /* If the application declared the buffer as static draw/copy or stream
1785 * draw, it should not be frequently modified with glBufferSubData.
1787 BUFFER_USAGE_WARNING(ctx
,
1788 "using %s(buffer %u, offset %u, size %u) to "
1789 "update a %s buffer",
1790 func
, bufObj
->Name
, offset
, size
,
1791 _mesa_enum_to_string(bufObj
->Usage
));
1794 bufObj
->Written
= GL_TRUE
;
1795 bufObj
->MinMaxCacheDirty
= true;
1797 assert(ctx
->Driver
.BufferSubData
);
1798 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1802 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1803 GLsizeiptr size
, const GLvoid
*data
)
1805 GET_CURRENT_CONTEXT(ctx
);
1806 struct gl_buffer_object
*bufObj
;
1808 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1812 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1816 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1817 GLsizeiptr size
, const GLvoid
*data
)
1819 GET_CURRENT_CONTEXT(ctx
);
1820 struct gl_buffer_object
*bufObj
;
1822 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1826 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1827 "glNamedBufferSubData");
1832 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1833 GLsizeiptr size
, GLvoid
*data
)
1835 GET_CURRENT_CONTEXT(ctx
);
1836 struct gl_buffer_object
*bufObj
;
1838 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1839 GL_INVALID_OPERATION
);
1843 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1844 "glGetBufferSubData")) {
1848 assert(ctx
->Driver
.GetBufferSubData
);
1849 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1853 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
1854 GLsizeiptr size
, GLvoid
*data
)
1856 GET_CURRENT_CONTEXT(ctx
);
1857 struct gl_buffer_object
*bufObj
;
1859 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1860 "glGetNamedBufferSubData");
1864 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1865 "glGetNamedBufferSubData")) {
1869 assert(ctx
->Driver
.GetBufferSubData
);
1870 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1875 * \param subdata true if caller is *SubData, false if *Data
1878 _mesa_clear_buffer_sub_data(struct gl_context
*ctx
,
1879 struct gl_buffer_object
*bufObj
,
1880 GLenum internalformat
,
1881 GLintptr offset
, GLsizeiptr size
,
1882 GLenum format
, GLenum type
,
1884 const char *func
, bool subdata
)
1886 mesa_format mesaFormat
;
1887 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1888 GLsizeiptr clearValueSize
;
1890 /* This checks for disallowed mappings. */
1891 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1896 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1897 format
, type
, func
);
1899 if (mesaFormat
== MESA_FORMAT_NONE
) {
1903 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1904 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1905 _mesa_error(ctx
, GL_INVALID_VALUE
,
1906 "%s(offset or size is not a multiple of "
1907 "internalformat size)", func
);
1911 /* Bail early. Negative size has already been checked. */
1915 bufObj
->MinMaxCacheDirty
= true;
1918 /* clear to zeros, per the spec */
1919 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1920 NULL
, clearValueSize
, bufObj
);
1924 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1925 format
, type
, data
, func
)) {
1929 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1930 clearValue
, clearValueSize
, bufObj
);
1934 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1935 GLenum type
, const GLvoid
*data
)
1937 GET_CURRENT_CONTEXT(ctx
);
1938 struct gl_buffer_object
*bufObj
;
1940 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1944 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1946 "glClearBufferData", false);
1950 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1951 GLenum format
, GLenum type
, const GLvoid
*data
)
1953 GET_CURRENT_CONTEXT(ctx
);
1954 struct gl_buffer_object
*bufObj
;
1956 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1960 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1962 "glClearNamedBufferData", false);
1967 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1968 GLintptr offset
, GLsizeiptr size
,
1969 GLenum format
, GLenum type
,
1972 GET_CURRENT_CONTEXT(ctx
);
1973 struct gl_buffer_object
*bufObj
;
1975 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1979 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1981 "glClearBufferSubData", true);
1985 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1986 GLintptr offset
, GLsizeiptr size
,
1987 GLenum format
, GLenum type
,
1990 GET_CURRENT_CONTEXT(ctx
);
1991 struct gl_buffer_object
*bufObj
;
1993 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1994 "glClearNamedBufferSubData");
1998 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
2000 "glClearNamedBufferSubData", true);
2005 _mesa_unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2008 GLboolean status
= GL_TRUE
;
2009 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2011 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2012 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2013 "%s(buffer is not mapped)", func
);
2018 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2019 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2021 /* check that last 100 bytes are still = magic value */
2022 for (i
= 0; i
< 100; i
++) {
2023 GLuint pos
= bufObj
->Size
- i
- 1;
2024 if (buf
[pos
] != 123) {
2025 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2026 " at position %d (value = %u)\n",
2034 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2035 GLuint i
, unchanged
= 0;
2036 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2038 /* check which bytes changed */
2039 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2040 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2047 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2048 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2053 status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2054 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2055 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2056 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2057 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2062 GLboolean GLAPIENTRY
2063 _mesa_UnmapBuffer(GLenum target
)
2065 GET_CURRENT_CONTEXT(ctx
);
2066 struct gl_buffer_object
*bufObj
;
2068 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2072 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2075 GLboolean GLAPIENTRY
2076 _mesa_UnmapNamedBuffer(GLuint buffer
)
2078 GET_CURRENT_CONTEXT(ctx
);
2079 struct gl_buffer_object
*bufObj
;
2081 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2085 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2090 get_buffer_parameter(struct gl_context
*ctx
,
2091 struct gl_buffer_object
*bufObj
, GLenum pname
,
2092 GLint64
*params
, const char *func
)
2095 case GL_BUFFER_SIZE_ARB
:
2096 *params
= bufObj
->Size
;
2098 case GL_BUFFER_USAGE_ARB
:
2099 *params
= bufObj
->Usage
;
2101 case GL_BUFFER_ACCESS_ARB
:
2102 *params
= simplified_access_mode(ctx
,
2103 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2105 case GL_BUFFER_MAPPED_ARB
:
2106 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2108 case GL_BUFFER_ACCESS_FLAGS
:
2109 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2111 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2113 case GL_BUFFER_MAP_OFFSET
:
2114 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2116 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2118 case GL_BUFFER_MAP_LENGTH
:
2119 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2121 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2123 case GL_BUFFER_IMMUTABLE_STORAGE
:
2124 if (!ctx
->Extensions
.ARB_buffer_storage
)
2126 *params
= bufObj
->Immutable
;
2128 case GL_BUFFER_STORAGE_FLAGS
:
2129 if (!ctx
->Extensions
.ARB_buffer_storage
)
2131 *params
= bufObj
->StorageFlags
;
2140 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2141 _mesa_enum_to_string(pname
));
2146 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2148 GET_CURRENT_CONTEXT(ctx
);
2149 struct gl_buffer_object
*bufObj
;
2152 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2153 GL_INVALID_OPERATION
);
2157 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2158 "glGetBufferParameteriv"))
2159 return; /* Error already recorded. */
2161 *params
= (GLint
) parameter
;
2165 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2167 GET_CURRENT_CONTEXT(ctx
);
2168 struct gl_buffer_object
*bufObj
;
2171 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2172 GL_INVALID_OPERATION
);
2176 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2177 "glGetBufferParameteri64v"))
2178 return; /* Error already recorded. */
2180 *params
= parameter
;
2184 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2186 GET_CURRENT_CONTEXT(ctx
);
2187 struct gl_buffer_object
*bufObj
;
2190 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2191 "glGetNamedBufferParameteriv");
2195 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2196 "glGetNamedBufferParameteriv"))
2197 return; /* Error already recorded. */
2199 *params
= (GLint
) parameter
;
2203 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2206 GET_CURRENT_CONTEXT(ctx
);
2207 struct gl_buffer_object
*bufObj
;
2210 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2211 "glGetNamedBufferParameteri64v");
2215 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2216 "glGetNamedBufferParameteri64v"))
2217 return; /* Error already recorded. */
2219 *params
= parameter
;
2224 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2226 GET_CURRENT_CONTEXT(ctx
);
2227 struct gl_buffer_object
*bufObj
;
2229 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2230 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2231 "GL_BUFFER_MAP_POINTER)");
2235 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2236 GL_INVALID_OPERATION
);
2240 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2244 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2246 GET_CURRENT_CONTEXT(ctx
);
2247 struct gl_buffer_object
*bufObj
;
2249 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2250 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2251 "GL_BUFFER_MAP_POINTER)");
2255 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2256 "glGetNamedBufferPointerv");
2260 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2265 _mesa_copy_buffer_sub_data(struct gl_context
*ctx
,
2266 struct gl_buffer_object
*src
,
2267 struct gl_buffer_object
*dst
,
2268 GLintptr readOffset
, GLintptr writeOffset
,
2269 GLsizeiptr size
, const char *func
)
2271 if (_mesa_check_disallowed_mapping(src
)) {
2272 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2273 "%s(readBuffer is mapped)", func
);
2277 if (_mesa_check_disallowed_mapping(dst
)) {
2278 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2279 "%s(writeBuffer is mapped)", func
);
2283 if (readOffset
< 0) {
2284 _mesa_error(ctx
, GL_INVALID_VALUE
,
2285 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2289 if (writeOffset
< 0) {
2290 _mesa_error(ctx
, GL_INVALID_VALUE
,
2291 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2296 _mesa_error(ctx
, GL_INVALID_VALUE
,
2297 "%s(size %d < 0)", func
, (int) size
);
2301 if (readOffset
+ size
> src
->Size
) {
2302 _mesa_error(ctx
, GL_INVALID_VALUE
,
2303 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2304 (int) readOffset
, (int) size
, (int) src
->Size
);
2308 if (writeOffset
+ size
> dst
->Size
) {
2309 _mesa_error(ctx
, GL_INVALID_VALUE
,
2310 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2311 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2316 if (readOffset
+ size
<= writeOffset
) {
2319 else if (writeOffset
+ size
<= readOffset
) {
2323 /* overlapping src/dst is illegal */
2324 _mesa_error(ctx
, GL_INVALID_VALUE
,
2325 "%s(overlapping src/dst)", func
);
2330 dst
->MinMaxCacheDirty
= true;
2332 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2336 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2337 GLintptr readOffset
, GLintptr writeOffset
,
2340 GET_CURRENT_CONTEXT(ctx
);
2341 struct gl_buffer_object
*src
, *dst
;
2343 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2344 GL_INVALID_OPERATION
);
2348 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2349 GL_INVALID_OPERATION
);
2353 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2354 "glCopyBufferSubData");
2358 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2359 GLintptr readOffset
, GLintptr writeOffset
,
2362 GET_CURRENT_CONTEXT(ctx
);
2363 struct gl_buffer_object
*src
, *dst
;
2365 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2366 "glCopyNamedBufferSubData");
2370 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2371 "glCopyNamedBufferSubData");
2375 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2376 "glCopyNamedBufferSubData");
2381 _mesa_map_buffer_range(struct gl_context
*ctx
,
2382 struct gl_buffer_object
*bufObj
,
2383 GLintptr offset
, GLsizeiptr length
,
2384 GLbitfield access
, const char *func
)
2387 GLbitfield allowed_access
;
2389 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
2392 _mesa_error(ctx
, GL_INVALID_VALUE
,
2393 "%s(offset %ld < 0)", func
, (long) offset
);
2398 _mesa_error(ctx
, GL_INVALID_VALUE
,
2399 "%s(length %ld < 0)", func
, (long) length
);
2403 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2405 * "An INVALID_OPERATION error is generated for any of the following
2408 * * <length> is zero."
2410 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2411 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2415 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2419 allowed_access
= GL_MAP_READ_BIT
|
2421 GL_MAP_INVALIDATE_RANGE_BIT
|
2422 GL_MAP_INVALIDATE_BUFFER_BIT
|
2423 GL_MAP_FLUSH_EXPLICIT_BIT
|
2424 GL_MAP_UNSYNCHRONIZED_BIT
;
2426 if (ctx
->Extensions
.ARB_buffer_storage
) {
2427 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2428 GL_MAP_COHERENT_BIT
;
2431 if (access
& ~allowed_access
) {
2432 /* generate an error if any bits other than those allowed are set */
2433 _mesa_error(ctx
, GL_INVALID_VALUE
,
2434 "%s(access has undefined bits set)", func
);
2438 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2439 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2440 "%s(access indicates neither read or write)", func
);
2444 if ((access
& GL_MAP_READ_BIT
) &&
2445 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2446 GL_MAP_INVALIDATE_BUFFER_BIT
|
2447 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2448 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2449 "%s(read access with disallowed bits)", func
);
2453 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2454 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2455 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2456 "%s(access has flush explicit without write)", func
);
2460 if (access
& GL_MAP_READ_BIT
&&
2461 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2462 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2463 "%s(buffer does not allow read access)", func
);
2467 if (access
& GL_MAP_WRITE_BIT
&&
2468 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2469 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2470 "%s(buffer does not allow write access)", func
);
2474 if (access
& GL_MAP_COHERENT_BIT
&&
2475 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2476 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2477 "%s(buffer does not allow coherent access)", func
);
2481 if (access
& GL_MAP_PERSISTENT_BIT
&&
2482 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2483 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2484 "%s(buffer does not allow persistent access)", func
);
2488 if (offset
+ length
> bufObj
->Size
) {
2489 _mesa_error(ctx
, GL_INVALID_VALUE
,
2490 "%s(offset %td + length %td > buffer_size %td)", func
,
2491 offset
, length
, bufObj
->Size
);
2495 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2496 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2497 "%s(buffer already mapped)", func
);
2501 if (!bufObj
->Size
) {
2502 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2506 if (access
& GL_MAP_WRITE_BIT
) {
2507 bufObj
->NumMapBufferWriteCalls
++;
2508 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2509 bufObj
->Usage
== GL_STATIC_COPY
) &&
2510 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
2511 BUFFER_USAGE_WARNING(ctx
,
2512 "using %s(buffer %u, offset %u, length %u) to "
2513 "update a %s buffer",
2514 func
, bufObj
->Name
, offset
, length
,
2515 _mesa_enum_to_string(bufObj
->Usage
));
2519 assert(ctx
->Driver
.MapBufferRange
);
2520 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2523 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2526 /* The driver callback should have set all these fields.
2527 * This is important because other modules (like VBO) might call
2528 * the driver function directly.
2530 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2531 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2532 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2533 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2536 if (access
& GL_MAP_WRITE_BIT
) {
2537 bufObj
->Written
= GL_TRUE
;
2538 bufObj
->MinMaxCacheDirty
= true;
2542 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2543 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2544 bufObj
->Name
, bufObj
->Size
, access
);
2545 /* Access must be write only */
2546 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2548 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2549 for (i
= 0; i
< bufObj
->Size
; i
++)
2556 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2557 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2559 /* buffer is 100 bytes larger than requested, fill with magic value */
2560 for (i
= 0; i
< 100; i
++) {
2561 buf
[bufObj
->Size
- i
- 1] = 123;
2570 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2573 GET_CURRENT_CONTEXT(ctx
);
2574 struct gl_buffer_object
*bufObj
;
2576 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2577 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2578 "glMapBufferRange(ARB_map_buffer_range not supported)");
2582 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2586 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2587 "glMapBufferRange");
2591 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2594 GET_CURRENT_CONTEXT(ctx
);
2595 struct gl_buffer_object
*bufObj
;
2597 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2598 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2599 "glMapNamedBufferRange("
2600 "ARB_map_buffer_range not supported)");
2604 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2608 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2609 "glMapNamedBufferRange");
2613 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2614 * flags for input to _mesa_map_buffer_range.
2616 * \return true if the type of requested access is permissible.
2619 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2623 case GL_READ_ONLY_ARB
:
2624 *flags
= GL_MAP_READ_BIT
;
2625 return _mesa_is_desktop_gl(ctx
);
2626 case GL_WRITE_ONLY_ARB
:
2627 *flags
= GL_MAP_WRITE_BIT
;
2629 case GL_READ_WRITE_ARB
:
2630 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2631 return _mesa_is_desktop_gl(ctx
);
2638 _mesa_MapBuffer(GLenum target
, GLenum access
)
2640 GET_CURRENT_CONTEXT(ctx
);
2641 struct gl_buffer_object
*bufObj
;
2642 GLbitfield accessFlags
;
2644 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2645 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2649 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2653 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2658 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2660 GET_CURRENT_CONTEXT(ctx
);
2661 struct gl_buffer_object
*bufObj
;
2662 GLbitfield accessFlags
;
2664 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2665 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2669 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2673 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2674 "glMapNamedBuffer");
2679 _mesa_flush_mapped_buffer_range(struct gl_context
*ctx
,
2680 struct gl_buffer_object
*bufObj
,
2681 GLintptr offset
, GLsizeiptr length
,
2684 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2685 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2686 "%s(ARB_map_buffer_range not supported)", func
);
2691 _mesa_error(ctx
, GL_INVALID_VALUE
,
2692 "%s(offset %ld < 0)", func
, (long) offset
);
2697 _mesa_error(ctx
, GL_INVALID_VALUE
,
2698 "%s(length %ld < 0)", func
, (long) length
);
2702 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2703 /* buffer is not mapped */
2704 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2705 "%s(buffer is not mapped)", func
);
2709 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2710 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2711 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2712 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2716 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2717 _mesa_error(ctx
, GL_INVALID_VALUE
,
2718 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2719 (long) offset
, (long) length
,
2720 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2724 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2726 if (ctx
->Driver
.FlushMappedBufferRange
)
2727 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2732 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2735 GET_CURRENT_CONTEXT(ctx
);
2736 struct gl_buffer_object
*bufObj
;
2738 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2739 GL_INVALID_OPERATION
);
2743 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2744 "glFlushMappedBufferRange");
2748 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2751 GET_CURRENT_CONTEXT(ctx
);
2752 struct gl_buffer_object
*bufObj
;
2754 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2755 "glFlushMappedNamedBufferRange");
2759 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2760 "glFlushMappedNamedBufferRange");
2765 * Binds a buffer object to a uniform buffer binding point.
2767 * The caller is responsible for flushing vertices and updating
2771 set_ubo_binding(struct gl_context
*ctx
,
2772 struct gl_uniform_buffer_binding
*binding
,
2773 struct gl_buffer_object
*bufObj
,
2778 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2780 binding
->Offset
= offset
;
2781 binding
->Size
= size
;
2782 binding
->AutomaticSize
= autoSize
;
2784 /* If this is a real buffer object, mark it has having been used
2785 * at some point as a UBO.
2788 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
2792 * Binds a buffer object to a shader storage buffer binding point.
2794 * The caller is responsible for flushing vertices and updating
2798 set_ssbo_binding(struct gl_context
*ctx
,
2799 struct gl_shader_storage_buffer_binding
*binding
,
2800 struct gl_buffer_object
*bufObj
,
2805 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2807 binding
->Offset
= offset
;
2808 binding
->Size
= size
;
2809 binding
->AutomaticSize
= autoSize
;
2811 /* If this is a real buffer object, mark it has having been used
2812 * at some point as a SSBO.
2815 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
2819 * Binds a buffer object to a uniform buffer binding point.
2821 * Unlike set_ubo_binding(), this function also flushes vertices
2822 * and updates NewDriverState. It also checks if the binding
2823 * has actually changed before updating it.
2826 bind_uniform_buffer(struct gl_context
*ctx
,
2828 struct gl_buffer_object
*bufObj
,
2833 struct gl_uniform_buffer_binding
*binding
=
2834 &ctx
->UniformBufferBindings
[index
];
2836 if (binding
->BufferObject
== bufObj
&&
2837 binding
->Offset
== offset
&&
2838 binding
->Size
== size
&&
2839 binding
->AutomaticSize
== autoSize
) {
2843 FLUSH_VERTICES(ctx
, 0);
2844 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
2846 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2850 * Binds a buffer object to a shader storage buffer binding point.
2852 * Unlike set_ssbo_binding(), this function also flushes vertices
2853 * and updates NewDriverState. It also checks if the binding
2854 * has actually changed before updating it.
2857 bind_shader_storage_buffer(struct gl_context
*ctx
,
2859 struct gl_buffer_object
*bufObj
,
2864 struct gl_shader_storage_buffer_binding
*binding
=
2865 &ctx
->ShaderStorageBufferBindings
[index
];
2867 if (binding
->BufferObject
== bufObj
&&
2868 binding
->Offset
== offset
&&
2869 binding
->Size
== size
&&
2870 binding
->AutomaticSize
== autoSize
) {
2874 FLUSH_VERTICES(ctx
, 0);
2875 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
2877 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2881 * Bind a region of a buffer object to a uniform block binding point.
2882 * \param index the uniform buffer binding point index
2883 * \param bufObj the buffer object
2884 * \param offset offset to the start of buffer object region
2885 * \param size size of the buffer object region
2888 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2890 struct gl_buffer_object
*bufObj
,
2894 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2895 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2899 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2900 _mesa_error(ctx
, GL_INVALID_VALUE
,
2901 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2902 ctx
->Const
.UniformBufferOffsetAlignment
);
2906 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2911 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2912 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2916 * Bind a region of a buffer object to a shader storage block binding point.
2917 * \param index the shader storage buffer binding point index
2918 * \param bufObj the buffer object
2919 * \param offset offset to the start of buffer object region
2920 * \param size size of the buffer object region
2923 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
2925 struct gl_buffer_object
*bufObj
,
2929 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2930 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2934 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
2935 _mesa_error(ctx
, GL_INVALID_VALUE
,
2936 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2937 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
2941 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2946 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2947 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2951 * Bind a buffer object to a uniform block binding point.
2952 * As above, but offset = 0.
2955 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2957 struct gl_buffer_object
*bufObj
)
2959 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2960 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2964 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2966 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2967 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2969 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2973 * Bind a buffer object to a shader storage block binding point.
2974 * As above, but offset = 0.
2977 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
2979 struct gl_buffer_object
*bufObj
)
2981 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2982 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2986 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2988 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2989 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2991 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2995 * Binds a buffer object to an atomic buffer binding point.
2997 * The caller is responsible for validating the offset,
2998 * flushing the vertices and updating NewDriverState.
3001 set_atomic_buffer_binding(struct gl_context
*ctx
,
3002 struct gl_atomic_buffer_binding
*binding
,
3003 struct gl_buffer_object
*bufObj
,
3007 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
3009 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3010 binding
->Offset
= 0;
3013 binding
->Offset
= offset
;
3014 binding
->Size
= size
;
3015 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
3020 * Binds a buffer object to an atomic buffer binding point.
3022 * Unlike set_atomic_buffer_binding(), this function also validates the
3023 * index and offset, flushes vertices, and updates NewDriverState.
3024 * It also checks if the binding has actually changing before
3028 bind_atomic_buffer(struct gl_context
*ctx
,
3030 struct gl_buffer_object
*bufObj
,
3035 struct gl_atomic_buffer_binding
*binding
;
3037 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3038 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
3042 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3043 _mesa_error(ctx
, GL_INVALID_VALUE
,
3044 "%s(offset misaligned %d/%d)", name
, (int) offset
,
3045 ATOMIC_COUNTER_SIZE
);
3049 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3051 binding
= &ctx
->AtomicBufferBindings
[index
];
3052 if (binding
->BufferObject
== bufObj
&&
3053 binding
->Offset
== offset
&&
3054 binding
->Size
== size
) {
3058 FLUSH_VERTICES(ctx
, 0);
3059 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3061 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3065 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3067 const GLintptr
*offsets
,
3068 const GLsizeiptr
*sizes
)
3070 if (offsets
[index
] < 0) {
3071 /* The ARB_multi_bind spec says:
3073 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3074 * value in <offsets> is less than zero (per binding)."
3076 _mesa_error(ctx
, GL_INVALID_VALUE
,
3077 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3078 index
, (int64_t) offsets
[index
]);
3082 if (sizes
[index
] <= 0) {
3083 /* The ARB_multi_bind spec says:
3085 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3086 * value in <sizes> is less than or equal to zero (per binding)."
3088 _mesa_error(ctx
, GL_INVALID_VALUE
,
3089 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3090 index
, (int64_t) sizes
[index
]);
3098 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3099 GLuint first
, GLsizei count
,
3102 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3103 _mesa_error(ctx
, GL_INVALID_ENUM
,
3104 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3108 /* The ARB_multi_bind_spec says:
3110 * "An INVALID_OPERATION error is generated if <first> + <count> is
3111 * greater than the number of target-specific indexed binding points,
3112 * as described in section 6.7.1."
3114 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3115 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3116 "%s(first=%u + count=%d > the value of "
3117 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3118 caller
, first
, count
,
3119 ctx
->Const
.MaxUniformBufferBindings
);
3127 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3128 GLuint first
, GLsizei count
,
3131 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3132 _mesa_error(ctx
, GL_INVALID_ENUM
,
3133 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3137 /* The ARB_multi_bind_spec says:
3139 * "An INVALID_OPERATION error is generated if <first> + <count> is
3140 * greater than the number of target-specific indexed binding points,
3141 * as described in section 6.7.1."
3143 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3144 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3145 "%s(first=%u + count=%d > the value of "
3146 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3147 caller
, first
, count
,
3148 ctx
->Const
.MaxShaderStorageBufferBindings
);
3156 * Unbind all uniform buffers in the range
3157 * <first> through <first>+<count>-1
3160 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3162 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3165 for (i
= 0; i
< count
; i
++)
3166 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3167 bufObj
, -1, -1, GL_TRUE
);
3171 * Unbind all shader storage buffers in the range
3172 * <first> through <first>+<count>-1
3175 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3178 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3181 for (i
= 0; i
< count
; i
++)
3182 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3183 bufObj
, -1, -1, GL_TRUE
);
3187 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3188 const GLuint
*buffers
,
3190 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3195 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3198 /* Assume that at least one binding will be changed */
3199 FLUSH_VERTICES(ctx
, 0);
3200 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3203 /* The ARB_multi_bind spec says:
3205 * "If <buffers> is NULL, all bindings from <first> through
3206 * <first>+<count>-1 are reset to their unbound (zero) state.
3207 * In this case, the offsets and sizes associated with the
3208 * binding points are set to default values, ignoring
3209 * <offsets> and <sizes>."
3211 unbind_uniform_buffers(ctx
, first
, count
);
3215 /* Note that the error semantics for multi-bind commands differ from
3216 * those of other GL commands.
3218 * The Issues section in the ARB_multi_bind spec says:
3220 * "(11) Typically, OpenGL specifies that if an error is generated by a
3221 * command, that command has no effect. This is somewhat
3222 * unfortunate for multi-bind commands, because it would require a
3223 * first pass to scan the entire list of bound objects for errors
3224 * and then a second pass to actually perform the bindings.
3225 * Should we have different error semantics?
3227 * RESOLVED: Yes. In this specification, when the parameters for
3228 * one of the <count> binding points are invalid, that binding point
3229 * is not updated and an error will be generated. However, other
3230 * binding points in the same command will be updated if their
3231 * parameters are valid and no other error occurs."
3234 _mesa_begin_bufferobj_lookups(ctx
);
3236 for (i
= 0; i
< count
; i
++) {
3237 struct gl_uniform_buffer_binding
*binding
=
3238 &ctx
->UniformBufferBindings
[first
+ i
];
3239 struct gl_buffer_object
*bufObj
;
3240 GLintptr offset
= 0;
3241 GLsizeiptr size
= 0;
3244 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3247 /* The ARB_multi_bind spec says:
3249 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3250 * pair of values in <offsets> and <sizes> does not respectively
3251 * satisfy the constraints described for those parameters for the
3252 * specified target, as described in section 6.7.1 (per binding)."
3254 * Section 6.7.1 refers to table 6.5, which says:
3256 * "┌───────────────────────────────────────────────────────────────┐
3257 * │ Uniform buffer array bindings (see sec. 7.6) │
3258 * ├─────────────────────┬─────────────────────────────────────────┤
3260 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3261 * │ │ OFFSET_ALIGNMENT │
3263 * │ size restriction │ none │
3264 * └─────────────────────┴─────────────────────────────────────────┘"
3266 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3267 _mesa_error(ctx
, GL_INVALID_VALUE
,
3268 "glBindBuffersRange(offsets[%u]=%" PRId64
3269 " is misaligned; it must be a multiple of the value of "
3270 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3271 "target=GL_UNIFORM_BUFFER)",
3272 i
, (int64_t) offsets
[i
],
3273 ctx
->Const
.UniformBufferOffsetAlignment
);
3277 offset
= offsets
[i
];
3281 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3282 bufObj
= binding
->BufferObject
;
3284 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3287 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3288 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3290 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3294 _mesa_end_bufferobj_lookups(ctx
);
3298 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3299 GLsizei count
, const GLuint
*buffers
,
3301 const GLintptr
*offsets
,
3302 const GLsizeiptr
*sizes
,
3307 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3310 /* Assume that at least one binding will be changed */
3311 FLUSH_VERTICES(ctx
, 0);
3312 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3315 /* The ARB_multi_bind spec says:
3317 * "If <buffers> is NULL, all bindings from <first> through
3318 * <first>+<count>-1 are reset to their unbound (zero) state.
3319 * In this case, the offsets and sizes associated with the
3320 * binding points are set to default values, ignoring
3321 * <offsets> and <sizes>."
3323 unbind_shader_storage_buffers(ctx
, first
, count
);
3327 /* Note that the error semantics for multi-bind commands differ from
3328 * those of other GL commands.
3330 * The Issues section in the ARB_multi_bind spec says:
3332 * "(11) Typically, OpenGL specifies that if an error is generated by a
3333 * command, that command has no effect. This is somewhat
3334 * unfortunate for multi-bind commands, because it would require a
3335 * first pass to scan the entire list of bound objects for errors
3336 * and then a second pass to actually perform the bindings.
3337 * Should we have different error semantics?
3339 * RESOLVED: Yes. In this specification, when the parameters for
3340 * one of the <count> binding points are invalid, that binding point
3341 * is not updated and an error will be generated. However, other
3342 * binding points in the same command will be updated if their
3343 * parameters are valid and no other error occurs."
3346 _mesa_begin_bufferobj_lookups(ctx
);
3348 for (i
= 0; i
< count
; i
++) {
3349 struct gl_shader_storage_buffer_binding
*binding
=
3350 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3351 struct gl_buffer_object
*bufObj
;
3352 GLintptr offset
= 0;
3353 GLsizeiptr size
= 0;
3356 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3359 /* The ARB_multi_bind spec says:
3361 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3362 * pair of values in <offsets> and <sizes> does not respectively
3363 * satisfy the constraints described for those parameters for the
3364 * specified target, as described in section 6.7.1 (per binding)."
3366 * Section 6.7.1 refers to table 6.5, which says:
3368 * "┌───────────────────────────────────────────────────────────────┐
3369 * │ Shader storage buffer array bindings (see sec. 7.8) │
3370 * ├─────────────────────┬─────────────────────────────────────────┤
3372 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3373 * │ │ BUFFER_OFFSET_ALIGNMENT │
3375 * │ size restriction │ none │
3376 * └─────────────────────┴─────────────────────────────────────────┘"
3378 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3379 _mesa_error(ctx
, GL_INVALID_VALUE
,
3380 "glBindBuffersRange(offsets[%u]=%" PRId64
3381 " is misaligned; it must be a multiple of the value of "
3382 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3383 "target=GL_SHADER_STORAGE_BUFFER)",
3384 i
, (int64_t) offsets
[i
],
3385 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3389 offset
= offsets
[i
];
3393 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3394 bufObj
= binding
->BufferObject
;
3396 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3399 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3400 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3402 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3406 _mesa_end_bufferobj_lookups(ctx
);
3410 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3411 struct gl_transform_feedback_object
*tfObj
,
3412 GLuint first
, GLsizei count
, const char *caller
)
3414 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3415 _mesa_error(ctx
, GL_INVALID_ENUM
,
3416 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3420 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3422 * "An INVALID_OPERATION error is generated :
3425 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3426 * FEEDBACK_BUFFER and transform feedback is currently active."
3428 * We assume that this is also meant to apply to BindBuffersRange
3429 * and BindBuffersBase.
3431 if (tfObj
->Active
) {
3432 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3433 "%s(Changing transform feedback buffers while "
3434 "transform feedback is active)", caller
);
3438 /* The ARB_multi_bind_spec says:
3440 * "An INVALID_OPERATION error is generated if <first> + <count> is
3441 * greater than the number of target-specific indexed binding points,
3442 * as described in section 6.7.1."
3444 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3445 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3446 "%s(first=%u + count=%d > the value of "
3447 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3448 caller
, first
, count
,
3449 ctx
->Const
.MaxTransformFeedbackBuffers
);
3457 * Unbind all transform feedback buffers in the range
3458 * <first> through <first>+<count>-1
3461 unbind_xfb_buffers(struct gl_context
*ctx
,
3462 struct gl_transform_feedback_object
*tfObj
,
3463 GLuint first
, GLsizei count
)
3465 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3468 for (i
= 0; i
< count
; i
++)
3469 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3474 bind_xfb_buffers(struct gl_context
*ctx
,
3475 GLuint first
, GLsizei count
,
3476 const GLuint
*buffers
,
3478 const GLintptr
*offsets
,
3479 const GLsizeiptr
*sizes
,
3482 struct gl_transform_feedback_object
*tfObj
=
3483 ctx
->TransformFeedback
.CurrentObject
;
3486 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
3489 /* Assume that at least one binding will be changed */
3490 FLUSH_VERTICES(ctx
, 0);
3491 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3494 /* The ARB_multi_bind spec says:
3496 * "If <buffers> is NULL, all bindings from <first> through
3497 * <first>+<count>-1 are reset to their unbound (zero) state.
3498 * In this case, the offsets and sizes associated with the
3499 * binding points are set to default values, ignoring
3500 * <offsets> and <sizes>."
3502 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3506 /* Note that the error semantics for multi-bind commands differ from
3507 * those of other GL commands.
3509 * The Issues section in the ARB_multi_bind spec says:
3511 * "(11) Typically, OpenGL specifies that if an error is generated by a
3512 * command, that command has no effect. This is somewhat
3513 * unfortunate for multi-bind commands, because it would require a
3514 * first pass to scan the entire list of bound objects for errors
3515 * and then a second pass to actually perform the bindings.
3516 * Should we have different error semantics?
3518 * RESOLVED: Yes. In this specification, when the parameters for
3519 * one of the <count> binding points are invalid, that binding point
3520 * is not updated and an error will be generated. However, other
3521 * binding points in the same command will be updated if their
3522 * parameters are valid and no other error occurs."
3525 _mesa_begin_bufferobj_lookups(ctx
);
3527 for (i
= 0; i
< count
; i
++) {
3528 const GLuint index
= first
+ i
;
3529 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3530 struct gl_buffer_object
*bufObj
;
3531 GLintptr offset
= 0;
3532 GLsizeiptr size
= 0;
3535 offset
= offsets
[i
];
3538 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3541 /* The ARB_multi_bind spec says:
3543 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3544 * pair of values in <offsets> and <sizes> does not respectively
3545 * satisfy the constraints described for those parameters for the
3546 * specified target, as described in section 6.7.1 (per binding)."
3548 * Section 6.7.1 refers to table 6.5, which says:
3550 * "┌───────────────────────────────────────────────────────────────┐
3551 * │ Transform feedback array bindings (see sec. 13.2.2) │
3552 * ├───────────────────────┬───────────────────────────────────────┤
3554 * │ offset restriction │ multiple of 4 │
3556 * │ size restriction │ multiple of 4 │
3557 * └───────────────────────┴───────────────────────────────────────┘"
3559 if (offsets
[i
] & 0x3) {
3560 _mesa_error(ctx
, GL_INVALID_VALUE
,
3561 "glBindBuffersRange(offsets[%u]=%" PRId64
3562 " is misaligned; it must be a multiple of 4 when "
3563 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3564 i
, (int64_t) offsets
[i
]);
3568 if (sizes
[i
] & 0x3) {
3569 _mesa_error(ctx
, GL_INVALID_VALUE
,
3570 "glBindBuffersRange(sizes[%u]=%" PRId64
3571 " is misaligned; it must be a multiple of 4 when "
3572 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3573 i
, (int64_t) sizes
[i
]);
3577 offset
= offsets
[i
];
3581 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3582 bufObj
= boundBufObj
;
3584 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3587 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
3591 _mesa_end_bufferobj_lookups(ctx
);
3595 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
3596 GLuint first
, GLsizei count
,
3599 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
3600 _mesa_error(ctx
, GL_INVALID_ENUM
,
3601 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
3605 /* The ARB_multi_bind_spec says:
3607 * "An INVALID_OPERATION error is generated if <first> + <count> is
3608 * greater than the number of target-specific indexed binding points,
3609 * as described in section 6.7.1."
3611 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
3612 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3613 "%s(first=%u + count=%d > the value of "
3614 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3615 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
3623 * Unbind all atomic counter buffers in the range
3624 * <first> through <first>+<count>-1
3627 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3629 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3632 for (i
= 0; i
< count
; i
++)
3633 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
3638 bind_atomic_buffers(struct gl_context
*ctx
,
3641 const GLuint
*buffers
,
3643 const GLintptr
*offsets
,
3644 const GLsizeiptr
*sizes
,
3649 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
3652 /* Assume that at least one binding will be changed */
3653 FLUSH_VERTICES(ctx
, 0);
3654 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3657 /* The ARB_multi_bind spec says:
3659 * "If <buffers> is NULL, all bindings from <first> through
3660 * <first>+<count>-1 are reset to their unbound (zero) state.
3661 * In this case, the offsets and sizes associated with the
3662 * binding points are set to default values, ignoring
3663 * <offsets> and <sizes>."
3665 unbind_atomic_buffers(ctx
, first
, count
);
3669 /* Note that the error semantics for multi-bind commands differ from
3670 * those of other GL commands.
3672 * The Issues section in the ARB_multi_bind spec says:
3674 * "(11) Typically, OpenGL specifies that if an error is generated by a
3675 * command, that command has no effect. This is somewhat
3676 * unfortunate for multi-bind commands, because it would require a
3677 * first pass to scan the entire list of bound objects for errors
3678 * and then a second pass to actually perform the bindings.
3679 * Should we have different error semantics?
3681 * RESOLVED: Yes. In this specification, when the parameters for
3682 * one of the <count> binding points are invalid, that binding point
3683 * is not updated and an error will be generated. However, other
3684 * binding points in the same command will be updated if their
3685 * parameters are valid and no other error occurs."
3688 _mesa_begin_bufferobj_lookups(ctx
);
3690 for (i
= 0; i
< count
; i
++) {
3691 struct gl_atomic_buffer_binding
*binding
=
3692 &ctx
->AtomicBufferBindings
[first
+ i
];
3693 struct gl_buffer_object
*bufObj
;
3694 GLintptr offset
= 0;
3695 GLsizeiptr size
= 0;
3698 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3701 /* The ARB_multi_bind spec says:
3703 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3704 * pair of values in <offsets> and <sizes> does not respectively
3705 * satisfy the constraints described for those parameters for the
3706 * specified target, as described in section 6.7.1 (per binding)."
3708 * Section 6.7.1 refers to table 6.5, which says:
3710 * "┌───────────────────────────────────────────────────────────────┐
3711 * │ Atomic counter array bindings (see sec. 7.7.2) │
3712 * ├───────────────────────┬───────────────────────────────────────┤
3714 * │ offset restriction │ multiple of 4 │
3716 * │ size restriction │ none │
3717 * └───────────────────────┴───────────────────────────────────────┘"
3719 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
3720 _mesa_error(ctx
, GL_INVALID_VALUE
,
3721 "glBindBuffersRange(offsets[%u]=%" PRId64
3722 " is misaligned; it must be a multiple of %d when "
3723 "target=GL_ATOMIC_COUNTER_BUFFER)",
3724 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
3728 offset
= offsets
[i
];
3732 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3733 bufObj
= binding
->BufferObject
;
3735 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3738 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3741 _mesa_end_bufferobj_lookups(ctx
);
3745 _mesa_BindBufferRange(GLenum target
, GLuint index
,
3746 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
3748 GET_CURRENT_CONTEXT(ctx
);
3749 struct gl_buffer_object
*bufObj
;
3751 if (MESA_VERBOSE
& VERBOSE_API
) {
3752 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %ld, %ld)\n",
3753 _mesa_enum_to_string(target
), index
, buffer
, offset
, size
);
3757 bufObj
= ctx
->Shared
->NullBufferObj
;
3759 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3761 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3762 &bufObj
, "glBindBufferRange"))
3766 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3767 "glBindBufferRange(invalid buffer=%u)", buffer
);
3773 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
3780 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3781 _mesa_bind_buffer_range_transform_feedback(ctx
,
3782 ctx
->TransformFeedback
.CurrentObject
,
3783 index
, bufObj
, offset
, size
,
3786 case GL_UNIFORM_BUFFER
:
3787 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3789 case GL_SHADER_STORAGE_BUFFER
:
3790 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3792 case GL_ATOMIC_COUNTER_BUFFER
:
3793 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
3794 "glBindBufferRange");
3797 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
3803 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
3805 GET_CURRENT_CONTEXT(ctx
);
3806 struct gl_buffer_object
*bufObj
;
3808 if (MESA_VERBOSE
& VERBOSE_API
) {
3809 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
3810 _mesa_enum_to_string(target
), index
, buffer
);
3814 bufObj
= ctx
->Shared
->NullBufferObj
;
3816 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3818 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3819 &bufObj
, "glBindBufferBase"))
3823 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3824 "glBindBufferBase(invalid buffer=%u)", buffer
);
3828 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3829 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3831 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3832 * zero and size equal to the size of buffer."
3834 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3836 * "If the parameter (starting offset or size) was not specified when the
3837 * buffer object was bound, zero is returned."
3839 * What happens if the size of the buffer changes? Does the size of the
3840 * buffer at the moment glBindBufferBase was called still play a role, like
3841 * the first quote would imply, or is the size meaningless in the
3842 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3843 * core spec page 45 says:
3845 * "It is equivalent to calling BindBufferRange with offset zero, while
3846 * size is determined by the size of the bound buffer at the time the
3849 * My interpretation is that the GL 4.1 spec was a clarification of the
3850 * behavior, not a change. In particular, this choice will only make
3851 * rendering work in cases where it would have had undefined results.
3855 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3856 _mesa_bind_buffer_base_transform_feedback(ctx
,
3857 ctx
->TransformFeedback
.CurrentObject
,
3858 index
, bufObj
, false);
3860 case GL_UNIFORM_BUFFER
:
3861 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
3863 case GL_SHADER_STORAGE_BUFFER
:
3864 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
3866 case GL_ATOMIC_COUNTER_BUFFER
:
3867 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
3868 "glBindBufferBase");
3871 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
3877 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
3878 const GLuint
*buffers
,
3879 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3881 GET_CURRENT_CONTEXT(ctx
);
3883 if (MESA_VERBOSE
& VERBOSE_API
) {
3884 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
3885 _mesa_enum_to_string(target
), first
, count
,
3886 buffers
, offsets
, sizes
);
3890 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3891 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3892 "glBindBuffersRange");
3894 case GL_UNIFORM_BUFFER
:
3895 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3896 "glBindBuffersRange");
3898 case GL_SHADER_STORAGE_BUFFER
:
3899 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3900 "glBindBuffersRange");
3902 case GL_ATOMIC_COUNTER_BUFFER
:
3903 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3904 "glBindBuffersRange");
3907 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
3908 _mesa_enum_to_string(target
));
3914 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
3915 const GLuint
*buffers
)
3917 GET_CURRENT_CONTEXT(ctx
);
3919 if (MESA_VERBOSE
& VERBOSE_API
) {
3920 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
3921 _mesa_enum_to_string(target
), first
, count
, buffers
);
3925 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3926 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3927 "glBindBuffersBase");
3929 case GL_UNIFORM_BUFFER
:
3930 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3931 "glBindBuffersBase");
3933 case GL_SHADER_STORAGE_BUFFER
:
3934 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3935 "glBindBuffersBase");
3937 case GL_ATOMIC_COUNTER_BUFFER
:
3938 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3939 "glBindBuffersBase");
3942 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
3943 _mesa_enum_to_string(target
));
3949 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
3952 GET_CURRENT_CONTEXT(ctx
);
3953 struct gl_buffer_object
*bufObj
;
3954 const GLintptr end
= offset
+ length
;
3956 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
3957 * Profile) spec says:
3959 * "An INVALID_VALUE error is generated if buffer is zero or is not the
3960 * name of an existing buffer object."
3962 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3963 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
3964 _mesa_error(ctx
, GL_INVALID_VALUE
,
3965 "glInvalidateBufferSubData(name = 0x%x) invalid object",
3970 /* The GL_ARB_invalidate_subdata spec says:
3972 * "An INVALID_VALUE error is generated if <offset> or <length> is
3973 * negative, or if <offset> + <length> is greater than the value of
3976 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
3977 _mesa_error(ctx
, GL_INVALID_VALUE
,
3978 "glInvalidateBufferSubData(invalid offset or length)");
3982 /* The OpenGL 4.4 (Core Profile) spec says:
3984 * "An INVALID_OPERATION error is generated if buffer is currently
3985 * mapped by MapBuffer or if the invalidate range intersects the range
3986 * currently mapped by MapBufferRange, unless it was mapped
3987 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
3989 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
3990 bufferobj_range_mapped(bufObj
, offset
, length
)) {
3991 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3992 "glInvalidateBufferSubData(intersection with mapped "
3997 if (ctx
->Driver
.InvalidateBufferSubData
)
3998 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4002 _mesa_InvalidateBufferData(GLuint buffer
)
4004 GET_CURRENT_CONTEXT(ctx
);
4005 struct gl_buffer_object
*bufObj
;
4007 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4008 * Profile) spec says:
4010 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4011 * name of an existing buffer object."
4013 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4014 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4015 _mesa_error(ctx
, GL_INVALID_VALUE
,
4016 "glInvalidateBufferData(name = 0x%x) invalid object",
4021 /* The OpenGL 4.4 (Core Profile) spec says:
4023 * "An INVALID_OPERATION error is generated if buffer is currently
4024 * mapped by MapBuffer or if the invalidate range intersects the range
4025 * currently mapped by MapBufferRange, unless it was mapped
4026 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4028 if (_mesa_check_disallowed_mapping(bufObj
)) {
4029 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4030 "glInvalidateBufferData(intersection with mapped "
4035 if (ctx
->Driver
.InvalidateBufferSubData
)
4036 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, 0, bufObj
->Size
);