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
)
1103 return (struct gl_buffer_object
*)
1104 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1108 * A convenience function for direct state access functions that throws
1109 * GL_INVALID_OPERATION if buffer is not the name of an existing
1112 struct gl_buffer_object
*
1113 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1116 struct gl_buffer_object
*bufObj
;
1118 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1119 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1120 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1121 "%s(non-existent buffer object %u)", caller
, buffer
);
1130 _mesa_begin_bufferobj_lookups(struct gl_context
*ctx
)
1132 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1137 _mesa_end_bufferobj_lookups(struct gl_context
*ctx
)
1139 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1144 * Look up a buffer object for a multi-bind function.
1146 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1147 * of generating an error if the buffer ID is not zero or the name
1148 * of an existing buffer object.
1150 * If the buffer ID refers to an existing buffer object, a pointer
1151 * to the buffer object is returned. If the ID is zero, a pointer
1152 * to the shared NullBufferObj is returned. If the ID is not zero
1153 * and does not refer to a valid buffer object, this function
1156 * This function assumes that the caller has already locked the
1157 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1159 struct gl_buffer_object
*
1160 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1161 const GLuint
*buffers
,
1162 GLuint index
, const char *caller
)
1164 struct gl_buffer_object
*bufObj
;
1166 if (buffers
[index
] != 0) {
1167 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1169 /* The multi-bind functions don't create the buffer objects
1170 when they don't exist. */
1171 if (bufObj
== &DummyBufferObject
)
1174 bufObj
= ctx
->Shared
->NullBufferObj
;
1177 /* The ARB_multi_bind spec says:
1179 * "An INVALID_OPERATION error is generated if any value
1180 * in <buffers> is not zero or the name of an existing
1181 * buffer object (per binding)."
1183 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1184 "%s(buffers[%u]=%u is not zero or the name "
1185 "of an existing buffer object)",
1186 caller
, index
, buffers
[index
]);
1194 * If *ptr points to obj, set ptr = the Null/default buffer object.
1195 * This is a helper for buffer object deletion.
1196 * The GL spec says that deleting a buffer object causes it to get
1197 * unbound from all arrays in the current context.
1200 unbind(struct gl_context
*ctx
,
1201 struct gl_buffer_object
**ptr
,
1202 struct gl_buffer_object
*obj
)
1205 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
1211 * Plug default/fallback buffer object functions into the device
1215 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1217 /* GL_ARB_vertex/pixel_buffer_object */
1218 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1219 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1220 driver
->BufferData
= buffer_data_fallback
;
1221 driver
->BufferSubData
= buffer_sub_data_fallback
;
1222 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
1223 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1225 /* GL_ARB_clear_buffer_object */
1226 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1228 /* GL_ARB_map_buffer_range */
1229 driver
->MapBufferRange
= map_buffer_range_fallback
;
1230 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1232 /* GL_ARB_copy_buffer */
1233 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1238 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1239 struct gl_buffer_object
*bufObj
)
1243 for (i
= 0; i
< MAP_COUNT
; i
++) {
1244 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1245 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1246 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1247 bufObj
->Mappings
[i
].AccessFlags
= 0;
1253 /**********************************************************************/
1255 /**********************************************************************/
1258 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1260 GET_CURRENT_CONTEXT(ctx
);
1262 if (MESA_VERBOSE
& VERBOSE_API
) {
1263 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1264 _mesa_enum_to_string(target
), buffer
);
1267 bind_buffer_object(ctx
, target
, buffer
);
1272 * Delete a set of buffer objects.
1274 * \param n Number of buffer objects to delete.
1275 * \param ids Array of \c n buffer object IDs.
1278 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1280 GET_CURRENT_CONTEXT(ctx
);
1282 FLUSH_VERTICES(ctx
, 0);
1285 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1289 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1291 for (i
= 0; i
< n
; i
++) {
1292 struct gl_buffer_object
*bufObj
=
1293 _mesa_lookup_bufferobj_locked(ctx
, ids
[i
]);
1295 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1298 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1300 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1302 /* unbind any vertex pointers bound to this buffer */
1303 for (j
= 0; j
< ARRAY_SIZE(vao
->VertexBinding
); j
++) {
1304 unbind(ctx
, &vao
->VertexBinding
[j
].BufferObj
, bufObj
);
1307 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1308 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
1310 if (vao
->IndexBufferObj
== bufObj
) {
1311 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1314 /* unbind ARB_draw_indirect binding point */
1315 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1316 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER
, 0 );
1319 /* unbind ARB_indirect_parameters binding point */
1320 if (ctx
->ParameterBuffer
== bufObj
) {
1321 _mesa_BindBuffer(GL_PARAMETER_BUFFER_ARB
, 0);
1324 /* unbind ARB_compute_shader binding point */
1325 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1326 _mesa_BindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, 0);
1329 /* unbind ARB_copy_buffer binding points */
1330 if (ctx
->CopyReadBuffer
== bufObj
) {
1331 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
1333 if (ctx
->CopyWriteBuffer
== bufObj
) {
1334 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
1337 /* unbind transform feedback binding points */
1338 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1339 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
1341 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1342 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1343 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1347 /* unbind UBO binding points */
1348 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1349 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1350 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1354 if (ctx
->UniformBuffer
== bufObj
) {
1355 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
1358 /* unbind SSBO binding points */
1359 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1360 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1361 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER
, j
, 0);
1365 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1366 _mesa_BindBuffer(GL_SHADER_STORAGE_BUFFER
, 0);
1369 /* unbind Atomci Buffer binding points */
1370 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1371 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1372 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1376 if (ctx
->AtomicBuffer
== bufObj
) {
1377 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER
, 0 );
1380 /* unbind any pixel pack/unpack pointers bound to this buffer */
1381 if (ctx
->Pack
.BufferObj
== bufObj
) {
1382 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1384 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1385 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1388 if (ctx
->Texture
.BufferObject
== bufObj
) {
1389 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
1392 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1393 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
, 0);
1396 /* unbind query buffer binding point */
1397 if (ctx
->QueryBuffer
== bufObj
) {
1398 _mesa_BindBuffer(GL_QUERY_BUFFER
, 0);
1401 /* The ID is immediately freed for re-use */
1402 _mesa_HashRemoveLocked(ctx
->Shared
->BufferObjects
, ids
[i
]);
1403 /* Make sure we do not run into the classic ABA problem on bind.
1404 * We don't want to allow re-binding a buffer object that's been
1405 * "deleted" by glDeleteBuffers().
1407 * The explicit rebinding to the default object in the current context
1408 * prevents the above in the current context, but another context
1409 * sharing the same objects might suffer from this problem.
1410 * The alternative would be to do the hash lookup in any case on bind
1411 * which would introduce more runtime overhead than this.
1413 bufObj
->DeletePending
= GL_TRUE
;
1414 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1418 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1423 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1424 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1428 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1430 GET_CURRENT_CONTEXT(ctx
);
1433 struct gl_buffer_object
*buf
;
1435 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1437 if (MESA_VERBOSE
& VERBOSE_API
)
1438 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1441 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1450 * This must be atomic (generation and allocation of buffer object IDs)
1452 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1454 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1456 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1457 * DummyBufferObject. Otherwise, create a new buffer object and insert
1460 for (i
= 0; i
< n
; i
++) {
1461 buffers
[i
] = first
+ i
;
1463 assert(ctx
->Driver
.NewBufferObject
);
1464 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1466 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1467 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1472 buf
= &DummyBufferObject
;
1474 _mesa_HashInsertLocked(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1477 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1481 * Generate a set of unique buffer object IDs and store them in \c buffers.
1483 * \param n Number of IDs to generate.
1484 * \param buffers Array of \c n locations to store the IDs.
1487 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1489 create_buffers(n
, buffers
, false);
1493 * Create a set of buffer objects and store their unique IDs in \c buffers.
1495 * \param n Number of IDs to generate.
1496 * \param buffers Array of \c n locations to store the IDs.
1499 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1501 create_buffers(n
, buffers
, true);
1506 * Determine if ID is the name of a buffer object.
1508 * \param id ID of the potential buffer object.
1509 * \return \c GL_TRUE if \c id is the name of a buffer object,
1510 * \c GL_FALSE otherwise.
1512 GLboolean GLAPIENTRY
1513 _mesa_IsBuffer(GLuint id
)
1515 struct gl_buffer_object
*bufObj
;
1516 GET_CURRENT_CONTEXT(ctx
);
1517 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1519 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1521 return bufObj
&& bufObj
!= &DummyBufferObject
;
1526 _mesa_buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1527 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1528 GLbitfield flags
, const char *func
)
1531 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1535 if (flags
& ~(GL_MAP_READ_BIT
|
1537 GL_MAP_PERSISTENT_BIT
|
1538 GL_MAP_COHERENT_BIT
|
1539 GL_DYNAMIC_STORAGE_BIT
|
1540 GL_CLIENT_STORAGE_BIT
)) {
1541 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1545 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1546 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1547 _mesa_error(ctx
, GL_INVALID_VALUE
,
1548 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1552 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1553 _mesa_error(ctx
, GL_INVALID_VALUE
,
1554 "%s(COHERENT and flags!=PERSISTENT)", func
);
1558 if (bufObj
->Immutable
) {
1559 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1563 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1564 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1566 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1568 bufObj
->Written
= GL_TRUE
;
1569 bufObj
->Immutable
= GL_TRUE
;
1570 bufObj
->MinMaxCacheDirty
= true;
1572 assert(ctx
->Driver
.BufferData
);
1573 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1575 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1576 /* Even though the interaction between AMD_pinned_memory and
1577 * glBufferStorage is not described in the spec, Graham Sellers
1578 * said that it should behave the same as glBufferData.
1580 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1583 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1589 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1592 GET_CURRENT_CONTEXT(ctx
);
1593 struct gl_buffer_object
*bufObj
;
1595 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1599 _mesa_buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
,
1604 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1607 GET_CURRENT_CONTEXT(ctx
);
1608 struct gl_buffer_object
*bufObj
;
1610 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferStorage");
1615 * In direct state access, buffer objects have an unspecified target since
1616 * they are not required to be bound.
1618 _mesa_buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
,
1619 "glNamedBufferStorage");
1624 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1625 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1626 GLenum usage
, const char *func
)
1630 if (MESA_VERBOSE
& VERBOSE_API
) {
1631 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1633 _mesa_enum_to_string(target
),
1634 (long int) size
, data
,
1635 _mesa_enum_to_string(usage
));
1639 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1644 case GL_STREAM_DRAW_ARB
:
1645 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1648 case GL_STATIC_DRAW_ARB
:
1649 case GL_DYNAMIC_DRAW_ARB
:
1653 case GL_STREAM_READ_ARB
:
1654 case GL_STREAM_COPY_ARB
:
1655 case GL_STATIC_READ_ARB
:
1656 case GL_STATIC_COPY_ARB
:
1657 case GL_DYNAMIC_READ_ARB
:
1658 case GL_DYNAMIC_COPY_ARB
:
1659 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1663 valid_usage
= false;
1668 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1669 _mesa_enum_to_string(usage
));
1673 if (bufObj
->Immutable
) {
1674 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1678 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1679 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1681 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1683 bufObj
->Written
= GL_TRUE
;
1684 bufObj
->MinMaxCacheDirty
= true;
1687 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1688 bufObj
->Name
, size
, data
, usage
);
1695 assert(ctx
->Driver
.BufferData
);
1696 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1699 GL_DYNAMIC_STORAGE_BIT
,
1701 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1702 /* From GL_AMD_pinned_memory:
1704 * INVALID_OPERATION is generated by BufferData if <target> is
1705 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1706 * mapped to the GPU address space.
1708 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1711 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1717 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1718 const GLvoid
*data
, GLenum usage
)
1720 GET_CURRENT_CONTEXT(ctx
);
1721 struct gl_buffer_object
*bufObj
;
1723 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1727 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1732 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1735 GET_CURRENT_CONTEXT(ctx
);
1736 struct gl_buffer_object
*bufObj
;
1738 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1742 /* In direct state access, buffer objects have an unspecified target since
1743 * they are not required to be bound.
1745 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1746 "glNamedBufferData");
1751 * Implementation for glBufferSubData and glNamedBufferSubData.
1753 * \param ctx GL context.
1754 * \param bufObj The buffer object.
1755 * \param offset Offset of the first byte of the subdata range.
1756 * \param size Size, in bytes, of the subdata range.
1757 * \param data The data store.
1758 * \param func Name of calling function for recording errors.
1762 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1763 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1766 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1768 /* error already recorded */
1772 if (bufObj
->Immutable
&&
1773 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1774 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1781 bufObj
->NumSubDataCalls
++;
1783 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
1784 bufObj
->Usage
== GL_STATIC_COPY
) &&
1785 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
) {
1786 /* If the application declared the buffer as static draw/copy or stream
1787 * draw, it should not be frequently modified with glBufferSubData.
1789 BUFFER_USAGE_WARNING(ctx
,
1790 "using %s(buffer %u, offset %u, size %u) to "
1791 "update a %s buffer",
1792 func
, bufObj
->Name
, offset
, size
,
1793 _mesa_enum_to_string(bufObj
->Usage
));
1796 bufObj
->Written
= GL_TRUE
;
1797 bufObj
->MinMaxCacheDirty
= true;
1799 assert(ctx
->Driver
.BufferSubData
);
1800 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1804 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1805 GLsizeiptr size
, const GLvoid
*data
)
1807 GET_CURRENT_CONTEXT(ctx
);
1808 struct gl_buffer_object
*bufObj
;
1810 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1814 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1818 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1819 GLsizeiptr size
, const GLvoid
*data
)
1821 GET_CURRENT_CONTEXT(ctx
);
1822 struct gl_buffer_object
*bufObj
;
1824 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1828 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1829 "glNamedBufferSubData");
1834 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1835 GLsizeiptr size
, GLvoid
*data
)
1837 GET_CURRENT_CONTEXT(ctx
);
1838 struct gl_buffer_object
*bufObj
;
1840 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1841 GL_INVALID_OPERATION
);
1845 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1846 "glGetBufferSubData")) {
1850 assert(ctx
->Driver
.GetBufferSubData
);
1851 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1855 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
1856 GLsizeiptr size
, GLvoid
*data
)
1858 GET_CURRENT_CONTEXT(ctx
);
1859 struct gl_buffer_object
*bufObj
;
1861 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1862 "glGetNamedBufferSubData");
1866 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1867 "glGetNamedBufferSubData")) {
1871 assert(ctx
->Driver
.GetBufferSubData
);
1872 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1877 * \param subdata true if caller is *SubData, false if *Data
1880 _mesa_clear_buffer_sub_data(struct gl_context
*ctx
,
1881 struct gl_buffer_object
*bufObj
,
1882 GLenum internalformat
,
1883 GLintptr offset
, GLsizeiptr size
,
1884 GLenum format
, GLenum type
,
1886 const char *func
, bool subdata
)
1888 mesa_format mesaFormat
;
1889 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1890 GLsizeiptr clearValueSize
;
1892 /* This checks for disallowed mappings. */
1893 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1898 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1899 format
, type
, func
);
1901 if (mesaFormat
== MESA_FORMAT_NONE
) {
1905 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1906 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1907 _mesa_error(ctx
, GL_INVALID_VALUE
,
1908 "%s(offset or size is not a multiple of "
1909 "internalformat size)", func
);
1913 /* Bail early. Negative size has already been checked. */
1917 bufObj
->MinMaxCacheDirty
= true;
1920 /* clear to zeros, per the spec */
1921 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1922 NULL
, clearValueSize
, bufObj
);
1926 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1927 format
, type
, data
, func
)) {
1931 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1932 clearValue
, clearValueSize
, bufObj
);
1936 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1937 GLenum type
, const GLvoid
*data
)
1939 GET_CURRENT_CONTEXT(ctx
);
1940 struct gl_buffer_object
*bufObj
;
1942 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1946 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1948 "glClearBufferData", false);
1952 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1953 GLenum format
, GLenum type
, const GLvoid
*data
)
1955 GET_CURRENT_CONTEXT(ctx
);
1956 struct gl_buffer_object
*bufObj
;
1958 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1962 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1964 "glClearNamedBufferData", false);
1969 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1970 GLintptr offset
, GLsizeiptr size
,
1971 GLenum format
, GLenum type
,
1974 GET_CURRENT_CONTEXT(ctx
);
1975 struct gl_buffer_object
*bufObj
;
1977 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1981 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1983 "glClearBufferSubData", true);
1987 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1988 GLintptr offset
, GLsizeiptr size
,
1989 GLenum format
, GLenum type
,
1992 GET_CURRENT_CONTEXT(ctx
);
1993 struct gl_buffer_object
*bufObj
;
1995 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1996 "glClearNamedBufferSubData");
2000 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
2002 "glClearNamedBufferSubData", true);
2007 _mesa_unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2010 GLboolean status
= GL_TRUE
;
2011 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2013 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2014 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2015 "%s(buffer is not mapped)", func
);
2020 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2021 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2023 /* check that last 100 bytes are still = magic value */
2024 for (i
= 0; i
< 100; i
++) {
2025 GLuint pos
= bufObj
->Size
- i
- 1;
2026 if (buf
[pos
] != 123) {
2027 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2028 " at position %d (value = %u)\n",
2036 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2037 GLuint i
, unchanged
= 0;
2038 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2040 /* check which bytes changed */
2041 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2042 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2049 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2050 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2055 status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2056 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2057 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2058 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2059 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2064 GLboolean GLAPIENTRY
2065 _mesa_UnmapBuffer(GLenum target
)
2067 GET_CURRENT_CONTEXT(ctx
);
2068 struct gl_buffer_object
*bufObj
;
2070 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2074 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2077 GLboolean GLAPIENTRY
2078 _mesa_UnmapNamedBuffer(GLuint buffer
)
2080 GET_CURRENT_CONTEXT(ctx
);
2081 struct gl_buffer_object
*bufObj
;
2083 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2087 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2092 get_buffer_parameter(struct gl_context
*ctx
,
2093 struct gl_buffer_object
*bufObj
, GLenum pname
,
2094 GLint64
*params
, const char *func
)
2097 case GL_BUFFER_SIZE_ARB
:
2098 *params
= bufObj
->Size
;
2100 case GL_BUFFER_USAGE_ARB
:
2101 *params
= bufObj
->Usage
;
2103 case GL_BUFFER_ACCESS_ARB
:
2104 *params
= simplified_access_mode(ctx
,
2105 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2107 case GL_BUFFER_MAPPED_ARB
:
2108 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2110 case GL_BUFFER_ACCESS_FLAGS
:
2111 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2113 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2115 case GL_BUFFER_MAP_OFFSET
:
2116 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2118 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2120 case GL_BUFFER_MAP_LENGTH
:
2121 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2123 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2125 case GL_BUFFER_IMMUTABLE_STORAGE
:
2126 if (!ctx
->Extensions
.ARB_buffer_storage
)
2128 *params
= bufObj
->Immutable
;
2130 case GL_BUFFER_STORAGE_FLAGS
:
2131 if (!ctx
->Extensions
.ARB_buffer_storage
)
2133 *params
= bufObj
->StorageFlags
;
2142 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2143 _mesa_enum_to_string(pname
));
2148 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2150 GET_CURRENT_CONTEXT(ctx
);
2151 struct gl_buffer_object
*bufObj
;
2154 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2155 GL_INVALID_OPERATION
);
2159 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2160 "glGetBufferParameteriv"))
2161 return; /* Error already recorded. */
2163 *params
= (GLint
) parameter
;
2167 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2169 GET_CURRENT_CONTEXT(ctx
);
2170 struct gl_buffer_object
*bufObj
;
2173 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2174 GL_INVALID_OPERATION
);
2178 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2179 "glGetBufferParameteri64v"))
2180 return; /* Error already recorded. */
2182 *params
= parameter
;
2186 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2188 GET_CURRENT_CONTEXT(ctx
);
2189 struct gl_buffer_object
*bufObj
;
2192 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2193 "glGetNamedBufferParameteriv");
2197 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2198 "glGetNamedBufferParameteriv"))
2199 return; /* Error already recorded. */
2201 *params
= (GLint
) parameter
;
2205 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2208 GET_CURRENT_CONTEXT(ctx
);
2209 struct gl_buffer_object
*bufObj
;
2212 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2213 "glGetNamedBufferParameteri64v");
2217 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2218 "glGetNamedBufferParameteri64v"))
2219 return; /* Error already recorded. */
2221 *params
= parameter
;
2226 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2228 GET_CURRENT_CONTEXT(ctx
);
2229 struct gl_buffer_object
*bufObj
;
2231 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2232 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2233 "GL_BUFFER_MAP_POINTER)");
2237 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2238 GL_INVALID_OPERATION
);
2242 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2246 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2248 GET_CURRENT_CONTEXT(ctx
);
2249 struct gl_buffer_object
*bufObj
;
2251 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2252 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2253 "GL_BUFFER_MAP_POINTER)");
2257 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2258 "glGetNamedBufferPointerv");
2262 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2267 _mesa_copy_buffer_sub_data(struct gl_context
*ctx
,
2268 struct gl_buffer_object
*src
,
2269 struct gl_buffer_object
*dst
,
2270 GLintptr readOffset
, GLintptr writeOffset
,
2271 GLsizeiptr size
, const char *func
)
2273 if (_mesa_check_disallowed_mapping(src
)) {
2274 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2275 "%s(readBuffer is mapped)", func
);
2279 if (_mesa_check_disallowed_mapping(dst
)) {
2280 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2281 "%s(writeBuffer is mapped)", func
);
2285 if (readOffset
< 0) {
2286 _mesa_error(ctx
, GL_INVALID_VALUE
,
2287 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2291 if (writeOffset
< 0) {
2292 _mesa_error(ctx
, GL_INVALID_VALUE
,
2293 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2298 _mesa_error(ctx
, GL_INVALID_VALUE
,
2299 "%s(size %d < 0)", func
, (int) size
);
2303 if (readOffset
+ size
> src
->Size
) {
2304 _mesa_error(ctx
, GL_INVALID_VALUE
,
2305 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2306 (int) readOffset
, (int) size
, (int) src
->Size
);
2310 if (writeOffset
+ size
> dst
->Size
) {
2311 _mesa_error(ctx
, GL_INVALID_VALUE
,
2312 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2313 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2318 if (readOffset
+ size
<= writeOffset
) {
2321 else if (writeOffset
+ size
<= readOffset
) {
2325 /* overlapping src/dst is illegal */
2326 _mesa_error(ctx
, GL_INVALID_VALUE
,
2327 "%s(overlapping src/dst)", func
);
2332 dst
->MinMaxCacheDirty
= true;
2334 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2338 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2339 GLintptr readOffset
, GLintptr writeOffset
,
2342 GET_CURRENT_CONTEXT(ctx
);
2343 struct gl_buffer_object
*src
, *dst
;
2345 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2346 GL_INVALID_OPERATION
);
2350 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2351 GL_INVALID_OPERATION
);
2355 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2356 "glCopyBufferSubData");
2360 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2361 GLintptr readOffset
, GLintptr writeOffset
,
2364 GET_CURRENT_CONTEXT(ctx
);
2365 struct gl_buffer_object
*src
, *dst
;
2367 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2368 "glCopyNamedBufferSubData");
2372 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2373 "glCopyNamedBufferSubData");
2377 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2378 "glCopyNamedBufferSubData");
2383 _mesa_map_buffer_range(struct gl_context
*ctx
,
2384 struct gl_buffer_object
*bufObj
,
2385 GLintptr offset
, GLsizeiptr length
,
2386 GLbitfield access
, const char *func
)
2389 GLbitfield allowed_access
;
2391 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
2394 _mesa_error(ctx
, GL_INVALID_VALUE
,
2395 "%s(offset %ld < 0)", func
, (long) offset
);
2400 _mesa_error(ctx
, GL_INVALID_VALUE
,
2401 "%s(length %ld < 0)", func
, (long) length
);
2405 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2407 * "An INVALID_OPERATION error is generated for any of the following
2410 * * <length> is zero."
2412 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2413 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2417 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2421 allowed_access
= GL_MAP_READ_BIT
|
2423 GL_MAP_INVALIDATE_RANGE_BIT
|
2424 GL_MAP_INVALIDATE_BUFFER_BIT
|
2425 GL_MAP_FLUSH_EXPLICIT_BIT
|
2426 GL_MAP_UNSYNCHRONIZED_BIT
;
2428 if (ctx
->Extensions
.ARB_buffer_storage
) {
2429 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2430 GL_MAP_COHERENT_BIT
;
2433 if (access
& ~allowed_access
) {
2434 /* generate an error if any bits other than those allowed are set */
2435 _mesa_error(ctx
, GL_INVALID_VALUE
,
2436 "%s(access has undefined bits set)", func
);
2440 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2441 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2442 "%s(access indicates neither read or write)", func
);
2446 if ((access
& GL_MAP_READ_BIT
) &&
2447 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2448 GL_MAP_INVALIDATE_BUFFER_BIT
|
2449 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2450 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2451 "%s(read access with disallowed bits)", func
);
2455 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2456 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2457 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2458 "%s(access has flush explicit without write)", func
);
2462 if (access
& GL_MAP_READ_BIT
&&
2463 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2464 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2465 "%s(buffer does not allow read access)", func
);
2469 if (access
& GL_MAP_WRITE_BIT
&&
2470 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2471 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2472 "%s(buffer does not allow write access)", func
);
2476 if (access
& GL_MAP_COHERENT_BIT
&&
2477 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2478 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2479 "%s(buffer does not allow coherent access)", func
);
2483 if (access
& GL_MAP_PERSISTENT_BIT
&&
2484 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2485 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2486 "%s(buffer does not allow persistent access)", func
);
2490 if (offset
+ length
> bufObj
->Size
) {
2491 _mesa_error(ctx
, GL_INVALID_VALUE
,
2492 "%s(offset %lu + length %lu > buffer_size %lu)", func
,
2493 (unsigned long) offset
, (unsigned long) length
,
2494 (unsigned long) bufObj
->Size
);
2498 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2499 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2500 "%s(buffer already mapped)", func
);
2504 if (!bufObj
->Size
) {
2505 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2509 if (access
& GL_MAP_WRITE_BIT
) {
2510 bufObj
->NumMapBufferWriteCalls
++;
2511 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2512 bufObj
->Usage
== GL_STATIC_COPY
) &&
2513 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
2514 BUFFER_USAGE_WARNING(ctx
,
2515 "using %s(buffer %u, offset %u, length %u) to "
2516 "update a %s buffer",
2517 func
, bufObj
->Name
, offset
, length
,
2518 _mesa_enum_to_string(bufObj
->Usage
));
2522 assert(ctx
->Driver
.MapBufferRange
);
2523 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2526 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2529 /* The driver callback should have set all these fields.
2530 * This is important because other modules (like VBO) might call
2531 * the driver function directly.
2533 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2534 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2535 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2536 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2539 if (access
& GL_MAP_WRITE_BIT
) {
2540 bufObj
->Written
= GL_TRUE
;
2541 bufObj
->MinMaxCacheDirty
= true;
2545 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2546 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2547 bufObj
->Name
, bufObj
->Size
, access
);
2548 /* Access must be write only */
2549 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2551 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2552 for (i
= 0; i
< bufObj
->Size
; i
++)
2559 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2560 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2562 /* buffer is 100 bytes larger than requested, fill with magic value */
2563 for (i
= 0; i
< 100; i
++) {
2564 buf
[bufObj
->Size
- i
- 1] = 123;
2573 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2576 GET_CURRENT_CONTEXT(ctx
);
2577 struct gl_buffer_object
*bufObj
;
2579 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2580 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2581 "glMapBufferRange(ARB_map_buffer_range not supported)");
2585 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2589 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2590 "glMapBufferRange");
2594 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2597 GET_CURRENT_CONTEXT(ctx
);
2598 struct gl_buffer_object
*bufObj
;
2600 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2601 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2602 "glMapNamedBufferRange("
2603 "ARB_map_buffer_range not supported)");
2607 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2611 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2612 "glMapNamedBufferRange");
2616 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2617 * flags for input to _mesa_map_buffer_range.
2619 * \return true if the type of requested access is permissible.
2622 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2626 case GL_READ_ONLY_ARB
:
2627 *flags
= GL_MAP_READ_BIT
;
2628 return _mesa_is_desktop_gl(ctx
);
2629 case GL_WRITE_ONLY_ARB
:
2630 *flags
= GL_MAP_WRITE_BIT
;
2632 case GL_READ_WRITE_ARB
:
2633 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2634 return _mesa_is_desktop_gl(ctx
);
2641 _mesa_MapBuffer(GLenum target
, GLenum access
)
2643 GET_CURRENT_CONTEXT(ctx
);
2644 struct gl_buffer_object
*bufObj
;
2645 GLbitfield accessFlags
;
2647 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2648 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2652 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2656 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2661 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2663 GET_CURRENT_CONTEXT(ctx
);
2664 struct gl_buffer_object
*bufObj
;
2665 GLbitfield accessFlags
;
2667 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2668 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2672 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2676 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2677 "glMapNamedBuffer");
2682 _mesa_flush_mapped_buffer_range(struct gl_context
*ctx
,
2683 struct gl_buffer_object
*bufObj
,
2684 GLintptr offset
, GLsizeiptr length
,
2687 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2688 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2689 "%s(ARB_map_buffer_range not supported)", func
);
2694 _mesa_error(ctx
, GL_INVALID_VALUE
,
2695 "%s(offset %ld < 0)", func
, (long) offset
);
2700 _mesa_error(ctx
, GL_INVALID_VALUE
,
2701 "%s(length %ld < 0)", func
, (long) length
);
2705 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2706 /* buffer is not mapped */
2707 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2708 "%s(buffer is not mapped)", func
);
2712 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2713 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2714 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2715 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2719 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2720 _mesa_error(ctx
, GL_INVALID_VALUE
,
2721 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2722 (long) offset
, (long) length
,
2723 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2727 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2729 if (ctx
->Driver
.FlushMappedBufferRange
)
2730 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2735 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2738 GET_CURRENT_CONTEXT(ctx
);
2739 struct gl_buffer_object
*bufObj
;
2741 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2742 GL_INVALID_OPERATION
);
2746 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2747 "glFlushMappedBufferRange");
2751 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2754 GET_CURRENT_CONTEXT(ctx
);
2755 struct gl_buffer_object
*bufObj
;
2757 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2758 "glFlushMappedNamedBufferRange");
2762 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2763 "glFlushMappedNamedBufferRange");
2768 * Binds a buffer object to a uniform buffer binding point.
2770 * The caller is responsible for flushing vertices and updating
2774 set_ubo_binding(struct gl_context
*ctx
,
2775 struct gl_uniform_buffer_binding
*binding
,
2776 struct gl_buffer_object
*bufObj
,
2781 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2783 binding
->Offset
= offset
;
2784 binding
->Size
= size
;
2785 binding
->AutomaticSize
= autoSize
;
2787 /* If this is a real buffer object, mark it has having been used
2788 * at some point as a UBO.
2791 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
2795 * Binds a buffer object to a shader storage buffer binding point.
2797 * The caller is responsible for flushing vertices and updating
2801 set_ssbo_binding(struct gl_context
*ctx
,
2802 struct gl_shader_storage_buffer_binding
*binding
,
2803 struct gl_buffer_object
*bufObj
,
2808 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2810 binding
->Offset
= offset
;
2811 binding
->Size
= size
;
2812 binding
->AutomaticSize
= autoSize
;
2814 /* If this is a real buffer object, mark it has having been used
2815 * at some point as a SSBO.
2818 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
2822 * Binds a buffer object to a uniform buffer binding point.
2824 * Unlike set_ubo_binding(), this function also flushes vertices
2825 * and updates NewDriverState. It also checks if the binding
2826 * has actually changed before updating it.
2829 bind_uniform_buffer(struct gl_context
*ctx
,
2831 struct gl_buffer_object
*bufObj
,
2836 struct gl_uniform_buffer_binding
*binding
=
2837 &ctx
->UniformBufferBindings
[index
];
2839 if (binding
->BufferObject
== bufObj
&&
2840 binding
->Offset
== offset
&&
2841 binding
->Size
== size
&&
2842 binding
->AutomaticSize
== autoSize
) {
2846 FLUSH_VERTICES(ctx
, 0);
2847 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
2849 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2853 * Binds a buffer object to a shader storage buffer binding point.
2855 * Unlike set_ssbo_binding(), this function also flushes vertices
2856 * and updates NewDriverState. It also checks if the binding
2857 * has actually changed before updating it.
2860 bind_shader_storage_buffer(struct gl_context
*ctx
,
2862 struct gl_buffer_object
*bufObj
,
2867 struct gl_shader_storage_buffer_binding
*binding
=
2868 &ctx
->ShaderStorageBufferBindings
[index
];
2870 if (binding
->BufferObject
== bufObj
&&
2871 binding
->Offset
== offset
&&
2872 binding
->Size
== size
&&
2873 binding
->AutomaticSize
== autoSize
) {
2877 FLUSH_VERTICES(ctx
, 0);
2878 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
2880 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2884 * Bind a region of a buffer object to a uniform block binding point.
2885 * \param index the uniform buffer binding point index
2886 * \param bufObj the buffer object
2887 * \param offset offset to the start of buffer object region
2888 * \param size size of the buffer object region
2891 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2893 struct gl_buffer_object
*bufObj
,
2897 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2898 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2902 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2903 _mesa_error(ctx
, GL_INVALID_VALUE
,
2904 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2905 ctx
->Const
.UniformBufferOffsetAlignment
);
2909 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2914 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2915 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2919 * Bind a region of a buffer object to a shader storage block binding point.
2920 * \param index the shader storage buffer binding point index
2921 * \param bufObj the buffer object
2922 * \param offset offset to the start of buffer object region
2923 * \param size size of the buffer object region
2926 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
2928 struct gl_buffer_object
*bufObj
,
2932 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2933 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2937 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
2938 _mesa_error(ctx
, GL_INVALID_VALUE
,
2939 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2940 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
2944 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2949 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2950 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2954 * Bind a buffer object to a uniform block binding point.
2955 * As above, but offset = 0.
2958 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2960 struct gl_buffer_object
*bufObj
)
2962 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2963 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2967 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2969 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2970 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2972 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2976 * Bind a buffer object to a shader storage block binding point.
2977 * As above, but offset = 0.
2980 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
2982 struct gl_buffer_object
*bufObj
)
2984 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2985 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2989 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2991 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2992 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2994 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2998 * Binds a buffer object to an atomic buffer binding point.
3000 * The caller is responsible for validating the offset,
3001 * flushing the vertices and updating NewDriverState.
3004 set_atomic_buffer_binding(struct gl_context
*ctx
,
3005 struct gl_atomic_buffer_binding
*binding
,
3006 struct gl_buffer_object
*bufObj
,
3010 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
3012 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
3013 binding
->Offset
= 0;
3016 binding
->Offset
= offset
;
3017 binding
->Size
= size
;
3018 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
3023 * Binds a buffer object to an atomic buffer binding point.
3025 * Unlike set_atomic_buffer_binding(), this function also validates the
3026 * index and offset, flushes vertices, and updates NewDriverState.
3027 * It also checks if the binding has actually changing before
3031 bind_atomic_buffer(struct gl_context
*ctx
,
3033 struct gl_buffer_object
*bufObj
,
3038 struct gl_atomic_buffer_binding
*binding
;
3040 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3041 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
3045 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3046 _mesa_error(ctx
, GL_INVALID_VALUE
,
3047 "%s(offset misaligned %d/%d)", name
, (int) offset
,
3048 ATOMIC_COUNTER_SIZE
);
3052 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3054 binding
= &ctx
->AtomicBufferBindings
[index
];
3055 if (binding
->BufferObject
== bufObj
&&
3056 binding
->Offset
== offset
&&
3057 binding
->Size
== size
) {
3061 FLUSH_VERTICES(ctx
, 0);
3062 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3064 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3068 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3070 const GLintptr
*offsets
,
3071 const GLsizeiptr
*sizes
)
3073 if (offsets
[index
] < 0) {
3074 /* The ARB_multi_bind spec says:
3076 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3077 * value in <offsets> is less than zero (per binding)."
3079 _mesa_error(ctx
, GL_INVALID_VALUE
,
3080 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3081 index
, (int64_t) offsets
[index
]);
3085 if (sizes
[index
] <= 0) {
3086 /* The ARB_multi_bind spec says:
3088 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3089 * value in <sizes> is less than or equal to zero (per binding)."
3091 _mesa_error(ctx
, GL_INVALID_VALUE
,
3092 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3093 index
, (int64_t) sizes
[index
]);
3101 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3102 GLuint first
, GLsizei count
,
3105 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3106 _mesa_error(ctx
, GL_INVALID_ENUM
,
3107 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3111 /* The ARB_multi_bind_spec says:
3113 * "An INVALID_OPERATION error is generated if <first> + <count> is
3114 * greater than the number of target-specific indexed binding points,
3115 * as described in section 6.7.1."
3117 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3118 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3119 "%s(first=%u + count=%d > the value of "
3120 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3121 caller
, first
, count
,
3122 ctx
->Const
.MaxUniformBufferBindings
);
3130 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3131 GLuint first
, GLsizei count
,
3134 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3135 _mesa_error(ctx
, GL_INVALID_ENUM
,
3136 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3140 /* The ARB_multi_bind_spec says:
3142 * "An INVALID_OPERATION error is generated if <first> + <count> is
3143 * greater than the number of target-specific indexed binding points,
3144 * as described in section 6.7.1."
3146 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3147 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3148 "%s(first=%u + count=%d > the value of "
3149 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3150 caller
, first
, count
,
3151 ctx
->Const
.MaxShaderStorageBufferBindings
);
3159 * Unbind all uniform buffers in the range
3160 * <first> through <first>+<count>-1
3163 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3165 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3168 for (i
= 0; i
< count
; i
++)
3169 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3170 bufObj
, -1, -1, GL_TRUE
);
3174 * Unbind all shader storage buffers in the range
3175 * <first> through <first>+<count>-1
3178 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3181 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3184 for (i
= 0; i
< count
; i
++)
3185 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3186 bufObj
, -1, -1, GL_TRUE
);
3190 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3191 const GLuint
*buffers
,
3193 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3198 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3201 /* Assume that at least one binding will be changed */
3202 FLUSH_VERTICES(ctx
, 0);
3203 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3206 /* The ARB_multi_bind spec says:
3208 * "If <buffers> is NULL, all bindings from <first> through
3209 * <first>+<count>-1 are reset to their unbound (zero) state.
3210 * In this case, the offsets and sizes associated with the
3211 * binding points are set to default values, ignoring
3212 * <offsets> and <sizes>."
3214 unbind_uniform_buffers(ctx
, first
, count
);
3218 /* Note that the error semantics for multi-bind commands differ from
3219 * those of other GL commands.
3221 * The Issues section in the ARB_multi_bind spec says:
3223 * "(11) Typically, OpenGL specifies that if an error is generated by a
3224 * command, that command has no effect. This is somewhat
3225 * unfortunate for multi-bind commands, because it would require a
3226 * first pass to scan the entire list of bound objects for errors
3227 * and then a second pass to actually perform the bindings.
3228 * Should we have different error semantics?
3230 * RESOLVED: Yes. In this specification, when the parameters for
3231 * one of the <count> binding points are invalid, that binding point
3232 * is not updated and an error will be generated. However, other
3233 * binding points in the same command will be updated if their
3234 * parameters are valid and no other error occurs."
3237 _mesa_begin_bufferobj_lookups(ctx
);
3239 for (i
= 0; i
< count
; i
++) {
3240 struct gl_uniform_buffer_binding
*binding
=
3241 &ctx
->UniformBufferBindings
[first
+ i
];
3242 struct gl_buffer_object
*bufObj
;
3243 GLintptr offset
= 0;
3244 GLsizeiptr size
= 0;
3247 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3250 /* The ARB_multi_bind spec says:
3252 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3253 * pair of values in <offsets> and <sizes> does not respectively
3254 * satisfy the constraints described for those parameters for the
3255 * specified target, as described in section 6.7.1 (per binding)."
3257 * Section 6.7.1 refers to table 6.5, which says:
3259 * "┌───────────────────────────────────────────────────────────────┐
3260 * │ Uniform buffer array bindings (see sec. 7.6) │
3261 * ├─────────────────────┬─────────────────────────────────────────┤
3263 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3264 * │ │ OFFSET_ALIGNMENT │
3266 * │ size restriction │ none │
3267 * └─────────────────────┴─────────────────────────────────────────┘"
3269 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3270 _mesa_error(ctx
, GL_INVALID_VALUE
,
3271 "glBindBuffersRange(offsets[%u]=%" PRId64
3272 " is misaligned; it must be a multiple of the value of "
3273 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3274 "target=GL_UNIFORM_BUFFER)",
3275 i
, (int64_t) offsets
[i
],
3276 ctx
->Const
.UniformBufferOffsetAlignment
);
3280 offset
= offsets
[i
];
3284 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3285 bufObj
= binding
->BufferObject
;
3287 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3290 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3291 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3293 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3297 _mesa_end_bufferobj_lookups(ctx
);
3301 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3302 GLsizei count
, const GLuint
*buffers
,
3304 const GLintptr
*offsets
,
3305 const GLsizeiptr
*sizes
,
3310 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3313 /* Assume that at least one binding will be changed */
3314 FLUSH_VERTICES(ctx
, 0);
3315 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3318 /* The ARB_multi_bind spec says:
3320 * "If <buffers> is NULL, all bindings from <first> through
3321 * <first>+<count>-1 are reset to their unbound (zero) state.
3322 * In this case, the offsets and sizes associated with the
3323 * binding points are set to default values, ignoring
3324 * <offsets> and <sizes>."
3326 unbind_shader_storage_buffers(ctx
, first
, count
);
3330 /* Note that the error semantics for multi-bind commands differ from
3331 * those of other GL commands.
3333 * The Issues section in the ARB_multi_bind spec says:
3335 * "(11) Typically, OpenGL specifies that if an error is generated by a
3336 * command, that command has no effect. This is somewhat
3337 * unfortunate for multi-bind commands, because it would require a
3338 * first pass to scan the entire list of bound objects for errors
3339 * and then a second pass to actually perform the bindings.
3340 * Should we have different error semantics?
3342 * RESOLVED: Yes. In this specification, when the parameters for
3343 * one of the <count> binding points are invalid, that binding point
3344 * is not updated and an error will be generated. However, other
3345 * binding points in the same command will be updated if their
3346 * parameters are valid and no other error occurs."
3349 _mesa_begin_bufferobj_lookups(ctx
);
3351 for (i
= 0; i
< count
; i
++) {
3352 struct gl_shader_storage_buffer_binding
*binding
=
3353 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3354 struct gl_buffer_object
*bufObj
;
3355 GLintptr offset
= 0;
3356 GLsizeiptr size
= 0;
3359 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3362 /* The ARB_multi_bind spec says:
3364 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3365 * pair of values in <offsets> and <sizes> does not respectively
3366 * satisfy the constraints described for those parameters for the
3367 * specified target, as described in section 6.7.1 (per binding)."
3369 * Section 6.7.1 refers to table 6.5, which says:
3371 * "┌───────────────────────────────────────────────────────────────┐
3372 * │ Shader storage buffer array bindings (see sec. 7.8) │
3373 * ├─────────────────────┬─────────────────────────────────────────┤
3375 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3376 * │ │ BUFFER_OFFSET_ALIGNMENT │
3378 * │ size restriction │ none │
3379 * └─────────────────────┴─────────────────────────────────────────┘"
3381 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3382 _mesa_error(ctx
, GL_INVALID_VALUE
,
3383 "glBindBuffersRange(offsets[%u]=%" PRId64
3384 " is misaligned; it must be a multiple of the value of "
3385 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3386 "target=GL_SHADER_STORAGE_BUFFER)",
3387 i
, (int64_t) offsets
[i
],
3388 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3392 offset
= offsets
[i
];
3396 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3397 bufObj
= binding
->BufferObject
;
3399 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3402 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3403 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3405 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3409 _mesa_end_bufferobj_lookups(ctx
);
3413 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3414 struct gl_transform_feedback_object
*tfObj
,
3415 GLuint first
, GLsizei count
, const char *caller
)
3417 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3418 _mesa_error(ctx
, GL_INVALID_ENUM
,
3419 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3423 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3425 * "An INVALID_OPERATION error is generated :
3428 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3429 * FEEDBACK_BUFFER and transform feedback is currently active."
3431 * We assume that this is also meant to apply to BindBuffersRange
3432 * and BindBuffersBase.
3434 if (tfObj
->Active
) {
3435 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3436 "%s(Changing transform feedback buffers while "
3437 "transform feedback is active)", caller
);
3441 /* The ARB_multi_bind_spec says:
3443 * "An INVALID_OPERATION error is generated if <first> + <count> is
3444 * greater than the number of target-specific indexed binding points,
3445 * as described in section 6.7.1."
3447 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3448 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3449 "%s(first=%u + count=%d > the value of "
3450 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3451 caller
, first
, count
,
3452 ctx
->Const
.MaxTransformFeedbackBuffers
);
3460 * Unbind all transform feedback buffers in the range
3461 * <first> through <first>+<count>-1
3464 unbind_xfb_buffers(struct gl_context
*ctx
,
3465 struct gl_transform_feedback_object
*tfObj
,
3466 GLuint first
, GLsizei count
)
3468 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3471 for (i
= 0; i
< count
; i
++)
3472 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3477 bind_xfb_buffers(struct gl_context
*ctx
,
3478 GLuint first
, GLsizei count
,
3479 const GLuint
*buffers
,
3481 const GLintptr
*offsets
,
3482 const GLsizeiptr
*sizes
,
3485 struct gl_transform_feedback_object
*tfObj
=
3486 ctx
->TransformFeedback
.CurrentObject
;
3489 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
3492 /* Assume that at least one binding will be changed */
3493 FLUSH_VERTICES(ctx
, 0);
3494 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3497 /* The ARB_multi_bind spec says:
3499 * "If <buffers> is NULL, all bindings from <first> through
3500 * <first>+<count>-1 are reset to their unbound (zero) state.
3501 * In this case, the offsets and sizes associated with the
3502 * binding points are set to default values, ignoring
3503 * <offsets> and <sizes>."
3505 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3509 /* Note that the error semantics for multi-bind commands differ from
3510 * those of other GL commands.
3512 * The Issues section in the ARB_multi_bind spec says:
3514 * "(11) Typically, OpenGL specifies that if an error is generated by a
3515 * command, that command has no effect. This is somewhat
3516 * unfortunate for multi-bind commands, because it would require a
3517 * first pass to scan the entire list of bound objects for errors
3518 * and then a second pass to actually perform the bindings.
3519 * Should we have different error semantics?
3521 * RESOLVED: Yes. In this specification, when the parameters for
3522 * one of the <count> binding points are invalid, that binding point
3523 * is not updated and an error will be generated. However, other
3524 * binding points in the same command will be updated if their
3525 * parameters are valid and no other error occurs."
3528 _mesa_begin_bufferobj_lookups(ctx
);
3530 for (i
= 0; i
< count
; i
++) {
3531 const GLuint index
= first
+ i
;
3532 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3533 struct gl_buffer_object
*bufObj
;
3534 GLintptr offset
= 0;
3535 GLsizeiptr size
= 0;
3538 offset
= offsets
[i
];
3541 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3544 /* The ARB_multi_bind spec says:
3546 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3547 * pair of values in <offsets> and <sizes> does not respectively
3548 * satisfy the constraints described for those parameters for the
3549 * specified target, as described in section 6.7.1 (per binding)."
3551 * Section 6.7.1 refers to table 6.5, which says:
3553 * "┌───────────────────────────────────────────────────────────────┐
3554 * │ Transform feedback array bindings (see sec. 13.2.2) │
3555 * ├───────────────────────┬───────────────────────────────────────┤
3557 * │ offset restriction │ multiple of 4 │
3559 * │ size restriction │ multiple of 4 │
3560 * └───────────────────────┴───────────────────────────────────────┘"
3562 if (offsets
[i
] & 0x3) {
3563 _mesa_error(ctx
, GL_INVALID_VALUE
,
3564 "glBindBuffersRange(offsets[%u]=%" PRId64
3565 " is misaligned; it must be a multiple of 4 when "
3566 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3567 i
, (int64_t) offsets
[i
]);
3571 if (sizes
[i
] & 0x3) {
3572 _mesa_error(ctx
, GL_INVALID_VALUE
,
3573 "glBindBuffersRange(sizes[%u]=%" PRId64
3574 " is misaligned; it must be a multiple of 4 when "
3575 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3576 i
, (int64_t) sizes
[i
]);
3580 offset
= offsets
[i
];
3584 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3585 bufObj
= boundBufObj
;
3587 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3590 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
3594 _mesa_end_bufferobj_lookups(ctx
);
3598 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
3599 GLuint first
, GLsizei count
,
3602 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
3603 _mesa_error(ctx
, GL_INVALID_ENUM
,
3604 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
3608 /* The ARB_multi_bind_spec says:
3610 * "An INVALID_OPERATION error is generated if <first> + <count> is
3611 * greater than the number of target-specific indexed binding points,
3612 * as described in section 6.7.1."
3614 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
3615 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3616 "%s(first=%u + count=%d > the value of "
3617 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3618 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
3626 * Unbind all atomic counter buffers in the range
3627 * <first> through <first>+<count>-1
3630 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3632 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3635 for (i
= 0; i
< count
; i
++)
3636 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
3641 bind_atomic_buffers(struct gl_context
*ctx
,
3644 const GLuint
*buffers
,
3646 const GLintptr
*offsets
,
3647 const GLsizeiptr
*sizes
,
3652 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
3655 /* Assume that at least one binding will be changed */
3656 FLUSH_VERTICES(ctx
, 0);
3657 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3660 /* The ARB_multi_bind spec says:
3662 * "If <buffers> is NULL, all bindings from <first> through
3663 * <first>+<count>-1 are reset to their unbound (zero) state.
3664 * In this case, the offsets and sizes associated with the
3665 * binding points are set to default values, ignoring
3666 * <offsets> and <sizes>."
3668 unbind_atomic_buffers(ctx
, first
, count
);
3672 /* Note that the error semantics for multi-bind commands differ from
3673 * those of other GL commands.
3675 * The Issues section in the ARB_multi_bind spec says:
3677 * "(11) Typically, OpenGL specifies that if an error is generated by a
3678 * command, that command has no effect. This is somewhat
3679 * unfortunate for multi-bind commands, because it would require a
3680 * first pass to scan the entire list of bound objects for errors
3681 * and then a second pass to actually perform the bindings.
3682 * Should we have different error semantics?
3684 * RESOLVED: Yes. In this specification, when the parameters for
3685 * one of the <count> binding points are invalid, that binding point
3686 * is not updated and an error will be generated. However, other
3687 * binding points in the same command will be updated if their
3688 * parameters are valid and no other error occurs."
3691 _mesa_begin_bufferobj_lookups(ctx
);
3693 for (i
= 0; i
< count
; i
++) {
3694 struct gl_atomic_buffer_binding
*binding
=
3695 &ctx
->AtomicBufferBindings
[first
+ i
];
3696 struct gl_buffer_object
*bufObj
;
3697 GLintptr offset
= 0;
3698 GLsizeiptr size
= 0;
3701 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3704 /* The ARB_multi_bind spec says:
3706 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3707 * pair of values in <offsets> and <sizes> does not respectively
3708 * satisfy the constraints described for those parameters for the
3709 * specified target, as described in section 6.7.1 (per binding)."
3711 * Section 6.7.1 refers to table 6.5, which says:
3713 * "┌───────────────────────────────────────────────────────────────┐
3714 * │ Atomic counter array bindings (see sec. 7.7.2) │
3715 * ├───────────────────────┬───────────────────────────────────────┤
3717 * │ offset restriction │ multiple of 4 │
3719 * │ size restriction │ none │
3720 * └───────────────────────┴───────────────────────────────────────┘"
3722 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
3723 _mesa_error(ctx
, GL_INVALID_VALUE
,
3724 "glBindBuffersRange(offsets[%u]=%" PRId64
3725 " is misaligned; it must be a multiple of %d when "
3726 "target=GL_ATOMIC_COUNTER_BUFFER)",
3727 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
3731 offset
= offsets
[i
];
3735 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3736 bufObj
= binding
->BufferObject
;
3738 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3741 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3744 _mesa_end_bufferobj_lookups(ctx
);
3748 _mesa_BindBufferRange(GLenum target
, GLuint index
,
3749 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
3751 GET_CURRENT_CONTEXT(ctx
);
3752 struct gl_buffer_object
*bufObj
;
3754 if (MESA_VERBOSE
& VERBOSE_API
) {
3755 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
3756 _mesa_enum_to_string(target
), index
, buffer
,
3757 (unsigned long) offset
, (unsigned long) size
);
3761 bufObj
= ctx
->Shared
->NullBufferObj
;
3763 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3765 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3766 &bufObj
, "glBindBufferRange"))
3770 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3771 "glBindBufferRange(invalid buffer=%u)", buffer
);
3777 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
3784 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3785 _mesa_bind_buffer_range_transform_feedback(ctx
,
3786 ctx
->TransformFeedback
.CurrentObject
,
3787 index
, bufObj
, offset
, size
,
3790 case GL_UNIFORM_BUFFER
:
3791 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3793 case GL_SHADER_STORAGE_BUFFER
:
3794 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3796 case GL_ATOMIC_COUNTER_BUFFER
:
3797 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
3798 "glBindBufferRange");
3801 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
3807 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
3809 GET_CURRENT_CONTEXT(ctx
);
3810 struct gl_buffer_object
*bufObj
;
3812 if (MESA_VERBOSE
& VERBOSE_API
) {
3813 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
3814 _mesa_enum_to_string(target
), index
, buffer
);
3818 bufObj
= ctx
->Shared
->NullBufferObj
;
3820 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3822 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3823 &bufObj
, "glBindBufferBase"))
3827 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3828 "glBindBufferBase(invalid buffer=%u)", buffer
);
3832 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3833 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3835 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3836 * zero and size equal to the size of buffer."
3838 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3840 * "If the parameter (starting offset or size) was not specified when the
3841 * buffer object was bound, zero is returned."
3843 * What happens if the size of the buffer changes? Does the size of the
3844 * buffer at the moment glBindBufferBase was called still play a role, like
3845 * the first quote would imply, or is the size meaningless in the
3846 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3847 * core spec page 45 says:
3849 * "It is equivalent to calling BindBufferRange with offset zero, while
3850 * size is determined by the size of the bound buffer at the time the
3853 * My interpretation is that the GL 4.1 spec was a clarification of the
3854 * behavior, not a change. In particular, this choice will only make
3855 * rendering work in cases where it would have had undefined results.
3859 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3860 _mesa_bind_buffer_base_transform_feedback(ctx
,
3861 ctx
->TransformFeedback
.CurrentObject
,
3862 index
, bufObj
, false);
3864 case GL_UNIFORM_BUFFER
:
3865 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
3867 case GL_SHADER_STORAGE_BUFFER
:
3868 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
3870 case GL_ATOMIC_COUNTER_BUFFER
:
3871 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
3872 "glBindBufferBase");
3875 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
3881 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
3882 const GLuint
*buffers
,
3883 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3885 GET_CURRENT_CONTEXT(ctx
);
3887 if (MESA_VERBOSE
& VERBOSE_API
) {
3888 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
3889 _mesa_enum_to_string(target
), first
, count
,
3890 buffers
, offsets
, sizes
);
3894 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3895 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3896 "glBindBuffersRange");
3898 case GL_UNIFORM_BUFFER
:
3899 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3900 "glBindBuffersRange");
3902 case GL_SHADER_STORAGE_BUFFER
:
3903 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3904 "glBindBuffersRange");
3906 case GL_ATOMIC_COUNTER_BUFFER
:
3907 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3908 "glBindBuffersRange");
3911 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
3912 _mesa_enum_to_string(target
));
3918 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
3919 const GLuint
*buffers
)
3921 GET_CURRENT_CONTEXT(ctx
);
3923 if (MESA_VERBOSE
& VERBOSE_API
) {
3924 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
3925 _mesa_enum_to_string(target
), first
, count
, buffers
);
3929 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3930 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3931 "glBindBuffersBase");
3933 case GL_UNIFORM_BUFFER
:
3934 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3935 "glBindBuffersBase");
3937 case GL_SHADER_STORAGE_BUFFER
:
3938 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3939 "glBindBuffersBase");
3941 case GL_ATOMIC_COUNTER_BUFFER
:
3942 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3943 "glBindBuffersBase");
3946 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
3947 _mesa_enum_to_string(target
));
3953 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
3956 GET_CURRENT_CONTEXT(ctx
);
3957 struct gl_buffer_object
*bufObj
;
3958 const GLintptr end
= offset
+ length
;
3960 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
3961 * Profile) spec says:
3963 * "An INVALID_VALUE error is generated if buffer is zero or is not the
3964 * name of an existing buffer object."
3966 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3967 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
3968 _mesa_error(ctx
, GL_INVALID_VALUE
,
3969 "glInvalidateBufferSubData(name = 0x%x) invalid object",
3974 /* The GL_ARB_invalidate_subdata spec says:
3976 * "An INVALID_VALUE error is generated if <offset> or <length> is
3977 * negative, or if <offset> + <length> is greater than the value of
3980 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
3981 _mesa_error(ctx
, GL_INVALID_VALUE
,
3982 "glInvalidateBufferSubData(invalid offset or length)");
3986 /* The OpenGL 4.4 (Core Profile) spec says:
3988 * "An INVALID_OPERATION error is generated if buffer is currently
3989 * mapped by MapBuffer or if the invalidate range intersects the range
3990 * currently mapped by MapBufferRange, unless it was mapped
3991 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
3993 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
3994 bufferobj_range_mapped(bufObj
, offset
, length
)) {
3995 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3996 "glInvalidateBufferSubData(intersection with mapped "
4001 if (ctx
->Driver
.InvalidateBufferSubData
)
4002 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4006 _mesa_InvalidateBufferData(GLuint buffer
)
4008 GET_CURRENT_CONTEXT(ctx
);
4009 struct gl_buffer_object
*bufObj
;
4011 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4012 * Profile) spec says:
4014 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4015 * name of an existing buffer object."
4017 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4018 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4019 _mesa_error(ctx
, GL_INVALID_VALUE
,
4020 "glInvalidateBufferData(name = 0x%x) invalid object",
4025 /* The OpenGL 4.4 (Core Profile) spec says:
4027 * "An INVALID_OPERATION error is generated if buffer is currently
4028 * mapped by MapBuffer or if the invalidate range intersects the range
4029 * currently mapped by MapBufferRange, unless it was mapped
4030 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4032 if (_mesa_check_disallowed_mapping(bufObj
)) {
4033 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4034 "glInvalidateBufferData(intersection with mapped "
4039 if (ctx
->Driver
.InvalidateBufferSubData
)
4040 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, 0, bufObj
->Size
);