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 */
40 #include "bufferobj.h"
43 #include "glformats.h"
45 #include "transformfeedback.h"
50 /*#define BOUNDS_CHECK*/
54 * We count the number of buffer modification calls to check for
55 * inefficient buffer use. This is the number of such calls before we
58 #define BUFFER_WARNING_CALL_COUNT 4
62 * Helper to warn of possible performance issues, such as frequently
63 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
67 buffer_usage_warning(struct gl_context
*ctx
, GLuint
*id
, const char *fmt
, ...)
72 _mesa_gl_vdebug(ctx
, id
,
73 MESA_DEBUG_SOURCE_API
,
74 MESA_DEBUG_TYPE_PERFORMANCE
,
75 MESA_DEBUG_SEVERITY_MEDIUM
,
80 #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
82 static GLuint id = 0; \
83 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
88 * Used as a placeholder for buffer objects between glGenBuffers() and
89 * glBindBuffer() so that glIsBuffer() can work correctly.
91 static struct gl_buffer_object DummyBufferObject
;
95 * Return pointer to address of a buffer object target.
96 * \param ctx the GL context
97 * \param target the buffer object target to be retrieved.
98 * \return pointer to pointer to the buffer object bound to \c target in the
99 * specified context or \c NULL if \c target is invalid.
101 static inline struct gl_buffer_object
**
102 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
104 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
106 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
107 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
111 case GL_ARRAY_BUFFER_ARB
:
112 return &ctx
->Array
.ArrayBufferObj
;
113 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
114 return &ctx
->Array
.VAO
->IndexBufferObj
;
115 case GL_PIXEL_PACK_BUFFER_EXT
:
116 return &ctx
->Pack
.BufferObj
;
117 case GL_PIXEL_UNPACK_BUFFER_EXT
:
118 return &ctx
->Unpack
.BufferObj
;
119 case GL_COPY_READ_BUFFER
:
120 return &ctx
->CopyReadBuffer
;
121 case GL_COPY_WRITE_BUFFER
:
122 return &ctx
->CopyWriteBuffer
;
123 case GL_DRAW_INDIRECT_BUFFER
:
124 if ((ctx
->API
== API_OPENGL_CORE
&&
125 ctx
->Extensions
.ARB_draw_indirect
) ||
126 _mesa_is_gles31(ctx
)) {
127 return &ctx
->DrawIndirectBuffer
;
130 case GL_PARAMETER_BUFFER_ARB
:
131 if (_mesa_has_ARB_indirect_parameters(ctx
)) {
132 return &ctx
->ParameterBuffer
;
135 case GL_DISPATCH_INDIRECT_BUFFER
:
136 if (_mesa_has_compute_shaders(ctx
)) {
137 return &ctx
->DispatchIndirectBuffer
;
140 case GL_TRANSFORM_FEEDBACK_BUFFER
:
141 if (ctx
->Extensions
.EXT_transform_feedback
) {
142 return &ctx
->TransformFeedback
.CurrentBuffer
;
145 case GL_TEXTURE_BUFFER
:
146 if (ctx
->API
== API_OPENGL_CORE
&&
147 ctx
->Extensions
.ARB_texture_buffer_object
) {
148 return &ctx
->Texture
.BufferObject
;
151 case GL_UNIFORM_BUFFER
:
152 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
153 return &ctx
->UniformBuffer
;
156 case GL_SHADER_STORAGE_BUFFER
:
157 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
158 return &ctx
->ShaderStorageBuffer
;
161 case GL_ATOMIC_COUNTER_BUFFER
:
162 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
163 return &ctx
->AtomicBuffer
;
166 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
167 if (ctx
->Extensions
.AMD_pinned_memory
) {
168 return &ctx
->ExternalVirtualMemoryBuffer
;
179 * Get the buffer object bound to the specified target in a GL context.
180 * \param ctx the GL context
181 * \param target the buffer object target to be retrieved.
182 * \param error the GL error to record if target is illegal.
183 * \return pointer to the buffer object bound to \c target in the
184 * specified context or \c NULL if \c target is invalid.
186 static inline struct gl_buffer_object
*
187 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
190 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
193 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
197 if (!_mesa_is_bufferobj(*bufObj
)) {
198 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
207 * Convert a GLbitfield describing the mapped buffer access flags
208 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
211 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
213 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
214 if ((access
& rwFlags
) == rwFlags
)
215 return GL_READ_WRITE
;
216 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
218 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
219 return GL_WRITE_ONLY
;
221 /* Otherwise, AccessFlags is zero (the default state).
223 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
225 * Name Type Initial Value Legal Values
227 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
230 * However, table 6.8 in the GL_OES_mapbuffer extension says:
232 * Get Value Type Get Command Value Description
233 * --------- ---- ----------- ----- -----------
234 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
236 * The difference is because GL_OES_mapbuffer only supports mapping buffers
241 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
246 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
248 * The regions do not overlap if and only if the end of the given
249 * region is before the mapped region or the start of the given region
250 * is after the mapped region.
252 * \param obj Buffer object target on which to operate.
253 * \param offset Offset of the first byte of the subdata range.
254 * \param size Size, in bytes, of the subdata range.
255 * \return true if ranges overlap, false otherwise
259 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
260 GLintptr offset
, GLsizeiptr size
)
262 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
263 const GLintptr end
= offset
+ size
;
264 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
265 obj
->Mappings
[MAP_USER
].Length
;
267 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
276 * Tests the subdata range parameters and sets the GL error code for
277 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
278 * \c glClearBufferSubData.
280 * \param ctx GL context.
281 * \param bufObj The buffer object.
282 * \param offset Offset of the first byte of the subdata range.
283 * \param size Size, in bytes, of the subdata range.
284 * \param mappedRange If true, checks if an overlapping range is mapped.
285 * If false, checks if buffer is mapped.
286 * \param caller Name of calling function for recording errors.
287 * \return false if error, true otherwise
289 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
292 buffer_object_subdata_range_good(struct gl_context
*ctx
,
293 const struct gl_buffer_object
*bufObj
,
294 GLintptr offset
, GLsizeiptr size
,
295 bool mappedRange
, const char *caller
)
298 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
303 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
307 if (offset
+ size
> bufObj
->Size
) {
308 _mesa_error(ctx
, GL_INVALID_VALUE
,
309 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
310 (unsigned long) offset
,
311 (unsigned long) size
,
312 (unsigned long) bufObj
->Size
);
316 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
320 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
321 _mesa_error(ctx
, GL_INVALID_OPERATION
,
322 "%s(range is mapped without persistent bit)",
328 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
329 _mesa_error(ctx
, GL_INVALID_OPERATION
,
330 "%s(buffer is mapped without persistent bit)",
341 * Test the format and type parameters and set the GL error code for
342 * \c glClearBufferData and \c glClearBufferSubData.
344 * \param ctx GL context.
345 * \param internalformat Format to which the data is to be converted.
346 * \param format Format of the supplied data.
347 * \param type Type of the supplied data.
348 * \param caller Name of calling function for recording errors.
349 * \return If internalformat, format and type are legal the mesa_format
350 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
352 * \sa glClearBufferData and glClearBufferSubData
355 validate_clear_buffer_format(struct gl_context
*ctx
,
356 GLenum internalformat
,
357 GLenum format
, GLenum type
,
360 mesa_format mesaFormat
;
361 GLenum errorFormatType
;
363 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
364 if (mesaFormat
== MESA_FORMAT_NONE
) {
365 _mesa_error(ctx
, GL_INVALID_ENUM
,
366 "%s(invalid internalformat)", caller
);
367 return MESA_FORMAT_NONE
;
370 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
371 * EXT_texture_integer there is no conversion between integer and
372 * non-integer formats
374 if (_mesa_is_enum_format_signed_int(format
) !=
375 _mesa_is_format_integer_color(mesaFormat
)) {
376 _mesa_error(ctx
, GL_INVALID_OPERATION
,
377 "%s(integer vs non-integer)", caller
);
378 return MESA_FORMAT_NONE
;
381 if (!_mesa_is_color_format(format
)) {
382 _mesa_error(ctx
, GL_INVALID_ENUM
,
383 "%s(format is not a color format)", caller
);
384 return MESA_FORMAT_NONE
;
387 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
388 if (errorFormatType
!= GL_NO_ERROR
) {
389 _mesa_error(ctx
, GL_INVALID_ENUM
,
390 "%s(invalid format or type)", caller
);
391 return MESA_FORMAT_NONE
;
399 * Convert user-specified clear value to the specified internal format.
401 * \param ctx GL context.
402 * \param internalformat Format to which the data is converted.
403 * \param clearValue Points to the converted clear value.
404 * \param format Format of the supplied data.
405 * \param type Type of the supplied data.
406 * \param data Data which is to be converted to internalformat.
407 * \param caller Name of calling function for recording errors.
408 * \return true if data could be converted, false otherwise.
410 * \sa glClearBufferData, glClearBufferSubData
413 convert_clear_buffer_data(struct gl_context
*ctx
,
414 mesa_format internalformat
,
415 GLubyte
*clearValue
, GLenum format
, GLenum type
,
416 const GLvoid
*data
, const char *caller
)
418 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
420 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
421 0, &clearValue
, 1, 1, 1,
422 format
, type
, data
, &ctx
->Unpack
)) {
426 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
433 * Allocate and initialize a new buffer object.
435 * Default callback for the \c dd_function_table::NewBufferObject() hook.
437 static struct gl_buffer_object
*
438 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
440 struct gl_buffer_object
*obj
;
444 obj
= MALLOC_STRUCT(gl_buffer_object
);
445 _mesa_initialize_buffer_object(ctx
, obj
, name
);
451 * Delete a buffer object.
453 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
456 _mesa_delete_buffer_object(struct gl_context
*ctx
,
457 struct gl_buffer_object
*bufObj
)
461 vbo_delete_minmax_cache(bufObj
);
462 _mesa_align_free(bufObj
->Data
);
464 /* assign strange values here to help w/ debugging */
465 bufObj
->RefCount
= -1000;
468 mtx_destroy(&bufObj
->Mutex
);
476 * Set ptr to bufObj w/ reference counting.
477 * This is normally only called from the _mesa_reference_buffer_object() macro
478 * when there's a real pointer change.
481 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
482 struct gl_buffer_object
**ptr
,
483 struct gl_buffer_object
*bufObj
)
486 /* Unreference the old buffer */
487 GLboolean deleteFlag
= GL_FALSE
;
488 struct gl_buffer_object
*oldObj
= *ptr
;
490 mtx_lock(&oldObj
->Mutex
);
491 assert(oldObj
->RefCount
> 0);
493 deleteFlag
= (oldObj
->RefCount
== 0);
494 mtx_unlock(&oldObj
->Mutex
);
497 assert(ctx
->Driver
.DeleteBuffer
);
498 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
506 /* reference new buffer */
507 mtx_lock(&bufObj
->Mutex
);
508 if (bufObj
->RefCount
== 0) {
509 /* this buffer's being deleted (look just above) */
510 /* Not sure this can every really happen. Warn if it does. */
511 _mesa_problem(NULL
, "referencing deleted buffer object");
518 mtx_unlock(&bufObj
->Mutex
);
524 * Initialize a buffer object to default values.
527 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
528 struct gl_buffer_object
*obj
,
531 memset(obj
, 0, sizeof(struct gl_buffer_object
));
532 mtx_init(&obj
->Mutex
, mtx_plain
);
535 obj
->Usage
= GL_STATIC_DRAW_ARB
;
541 * Callback called from _mesa_HashWalk()
544 count_buffer_size(GLuint key
, void *data
, void *userData
)
546 const struct gl_buffer_object
*bufObj
=
547 (const struct gl_buffer_object
*) data
;
548 GLuint
*total
= (GLuint
*) userData
;
551 *total
= *total
+ bufObj
->Size
;
556 * Compute total size (in bytes) of all buffer objects for the given context.
557 * For debugging purposes.
560 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
564 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
571 * Allocate space for and store data in a buffer object. Any data that was
572 * previously stored in the buffer object is lost. If \c data is \c NULL,
573 * memory will be allocated, but no copy will occur.
575 * This is the default callback for \c dd_function_table::BufferData()
576 * Note that all GL error checking will have been done already.
578 * \param ctx GL context.
579 * \param target Buffer object target on which to operate.
580 * \param size Size, in bytes, of the new data store.
581 * \param data Pointer to the data to store in the buffer object. This
582 * pointer may be \c NULL.
583 * \param usage Hints about how the data will be used.
584 * \param bufObj Object to be used.
586 * \return GL_TRUE for success, GL_FALSE for failure
587 * \sa glBufferDataARB, dd_function_table::BufferData.
590 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptr size
,
591 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
592 struct gl_buffer_object
*bufObj
)
598 _mesa_align_free( bufObj
->Data
);
600 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
602 bufObj
->Data
= (GLubyte
*) new_data
;
604 bufObj
->Usage
= usage
;
605 bufObj
->StorageFlags
= storageFlags
;
608 memcpy( bufObj
->Data
, data
, size
);
620 * Replace data in a subrange of buffer object. If the data range
621 * specified by \c size + \c offset extends beyond the end of the buffer or
622 * if \c data is \c NULL, no copy is performed.
624 * This is the default callback for \c dd_function_table::BufferSubData()
625 * Note that all GL error checking will have been done already.
627 * \param ctx GL context.
628 * \param offset Offset of the first byte to be modified.
629 * \param size Size, in bytes, of the data range.
630 * \param data Pointer to the data to store in the buffer object.
631 * \param bufObj Object to be used.
633 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
636 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptr offset
,
637 GLsizeiptr size
, const GLvoid
*data
,
638 struct gl_buffer_object
*bufObj
)
642 /* this should have been caught in _mesa_BufferSubData() */
643 assert(size
+ offset
<= bufObj
->Size
);
646 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
652 * Retrieve data from a subrange of buffer object. If the data range
653 * specified by \c size + \c offset extends beyond the end of the buffer or
654 * if \c data is \c NULL, no copy is performed.
656 * This is the default callback for \c dd_function_table::GetBufferSubData()
657 * Note that all GL error checking will have been done already.
659 * \param ctx GL context.
660 * \param target Buffer object target on which to operate.
661 * \param offset Offset of the first byte to be fetched.
662 * \param size Size, in bytes, of the data range.
663 * \param data Destination for data
664 * \param bufObj Object to be used.
666 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
669 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
670 GLsizeiptrARB size
, GLvoid
* data
,
671 struct gl_buffer_object
* bufObj
)
675 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
676 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
682 * Clear a subrange of the buffer object with copies of the supplied data.
683 * If data is NULL the buffer is filled with zeros.
685 * This is the default callback for \c dd_function_table::ClearBufferSubData()
686 * Note that all GL error checking will have been done already.
688 * \param ctx GL context.
689 * \param offset Offset of the first byte to be cleared.
690 * \param size Size, in bytes, of the to be cleared range.
691 * \param clearValue Source of the data.
692 * \param clearValueSize Size, in bytes, of the supplied data.
693 * \param bufObj Object to be cleared.
695 * \sa glClearBufferSubData, glClearBufferData and
696 * dd_function_table::ClearBufferSubData.
699 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
700 GLintptr offset
, GLsizeiptr size
,
701 const GLvoid
*clearValue
,
702 GLsizeiptr clearValueSize
,
703 struct gl_buffer_object
*bufObj
)
708 assert(ctx
->Driver
.MapBufferRange
);
709 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
711 GL_MAP_INVALIDATE_RANGE_BIT
,
712 bufObj
, MAP_INTERNAL
);
715 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
719 if (clearValue
== NULL
) {
720 /* Clear with zeros, per the spec */
721 memset(dest
, 0, size
);
722 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
726 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
727 memcpy(dest
, clearValue
, clearValueSize
);
728 dest
+= clearValueSize
;
731 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
736 * Default fallback for \c dd_function_table::MapBufferRange().
737 * Called via glMapBufferRange().
740 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
741 GLsizeiptr length
, GLbitfield access
,
742 struct gl_buffer_object
*bufObj
,
743 gl_map_buffer_index index
)
746 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
747 /* Just return a direct pointer to the data */
748 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
749 bufObj
->Mappings
[index
].Length
= length
;
750 bufObj
->Mappings
[index
].Offset
= offset
;
751 bufObj
->Mappings
[index
].AccessFlags
= access
;
752 return bufObj
->Mappings
[index
].Pointer
;
757 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
758 * Called via glFlushMappedBufferRange().
761 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
762 GLintptr offset
, GLsizeiptr length
,
763 struct gl_buffer_object
*obj
,
764 gl_map_buffer_index index
)
776 * Default callback for \c dd_function_table::UnmapBuffer().
778 * The input parameters will have been already tested for errors.
780 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
783 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
784 gl_map_buffer_index index
)
787 /* XXX we might assert here that bufObj->Pointer is non-null */
788 bufObj
->Mappings
[index
].Pointer
= NULL
;
789 bufObj
->Mappings
[index
].Length
= 0;
790 bufObj
->Mappings
[index
].Offset
= 0;
791 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
797 * Default fallback for \c dd_function_table::CopyBufferSubData().
798 * Called via glCopyBufferSubData().
801 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
802 struct gl_buffer_object
*src
,
803 struct gl_buffer_object
*dst
,
804 GLintptr readOffset
, GLintptr writeOffset
,
807 GLubyte
*srcPtr
, *dstPtr
;
810 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
812 GL_MAP_WRITE_BIT
, src
,
818 srcPtr
+= readOffset
;
819 dstPtr
+= writeOffset
;
821 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
822 GL_MAP_READ_BIT
, src
,
824 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
826 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
830 /* Note: the src and dst regions will never overlap. Trying to do so
831 * would generate GL_INVALID_VALUE earlier.
833 if (srcPtr
&& dstPtr
)
834 memcpy(dstPtr
, srcPtr
, size
);
836 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
838 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
844 * Initialize the state associated with buffer objects
847 _mesa_init_buffer_objects( struct gl_context
*ctx
)
851 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
852 mtx_init(&DummyBufferObject
.Mutex
, mtx_plain
);
853 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
855 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
856 ctx
->Shared
->NullBufferObj
);
858 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
859 ctx
->Shared
->NullBufferObj
);
860 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
861 ctx
->Shared
->NullBufferObj
);
863 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
864 ctx
->Shared
->NullBufferObj
);
866 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
,
867 ctx
->Shared
->NullBufferObj
);
869 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
870 ctx
->Shared
->NullBufferObj
);
872 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
873 ctx
->Shared
->NullBufferObj
);
875 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
,
876 ctx
->Shared
->NullBufferObj
);
878 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
,
879 ctx
->Shared
->NullBufferObj
);
881 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
882 _mesa_reference_buffer_object(ctx
,
883 &ctx
->UniformBufferBindings
[i
].BufferObject
,
884 ctx
->Shared
->NullBufferObj
);
885 ctx
->UniformBufferBindings
[i
].Offset
= -1;
886 ctx
->UniformBufferBindings
[i
].Size
= -1;
889 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
890 _mesa_reference_buffer_object(ctx
,
891 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
892 ctx
->Shared
->NullBufferObj
);
893 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
894 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
897 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
898 _mesa_reference_buffer_object(ctx
,
899 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
900 ctx
->Shared
->NullBufferObj
);
901 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
902 ctx
->AtomicBufferBindings
[i
].Size
= 0;
908 _mesa_free_buffer_objects( struct gl_context
*ctx
)
912 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
914 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
915 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
917 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
919 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
921 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
923 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
925 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
, NULL
);
927 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, NULL
);
929 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
930 _mesa_reference_buffer_object(ctx
,
931 &ctx
->UniformBufferBindings
[i
].BufferObject
,
935 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
936 _mesa_reference_buffer_object(ctx
,
937 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
941 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
942 _mesa_reference_buffer_object(ctx
,
943 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
950 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
952 struct gl_buffer_object
**buf_handle
,
955 struct gl_buffer_object
*buf
= *buf_handle
;
957 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
)) {
958 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
962 if (!buf
|| buf
== &DummyBufferObject
) {
963 /* If this is a new buffer object id, or one which was generated but
964 * never used before, allocate a buffer object now.
966 assert(ctx
->Driver
.NewBufferObject
);
967 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
969 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
972 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
980 * Bind the specified target to buffer for the specified context.
981 * Called by glBindBuffer() and other functions.
984 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
986 struct gl_buffer_object
*oldBufObj
;
987 struct gl_buffer_object
*newBufObj
= NULL
;
988 struct gl_buffer_object
**bindTarget
= NULL
;
990 bindTarget
= get_buffer_target(ctx
, target
);
992 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
996 /* Get pointer to old buffer object (to be unbound) */
997 oldBufObj
= *bindTarget
;
998 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
999 return; /* rebinding the same buffer object- no change */
1002 * Get pointer to new buffer object (newBufObj)
1005 /* The spec says there's not a buffer object named 0, but we use
1006 * one internally because it simplifies things.
1008 newBufObj
= ctx
->Shared
->NullBufferObj
;
1011 /* non-default buffer object */
1012 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1013 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1014 &newBufObj
, "glBindBuffer"))
1018 /* record usage history */
1020 case GL_PIXEL_PACK_BUFFER
:
1021 newBufObj
->UsageHistory
|= USAGE_PIXEL_PACK_BUFFER
;
1027 /* bind new buffer */
1028 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1033 * Update the default buffer objects in the given context to reference those
1034 * specified in the shared state and release those referencing the old
1038 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1040 /* Bind the NullBufferObj to remove references to those
1041 * in the shared context hash table.
1043 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
1044 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
1045 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
1046 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1052 * Return the gl_buffer_object for the given ID.
1053 * Always return NULL for ID 0.
1055 struct gl_buffer_object
*
1056 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1061 return (struct gl_buffer_object
*)
1062 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1066 struct gl_buffer_object
*
1067 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1069 return (struct gl_buffer_object
*)
1070 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1074 * A convenience function for direct state access functions that throws
1075 * GL_INVALID_OPERATION if buffer is not the name of an existing
1078 struct gl_buffer_object
*
1079 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1082 struct gl_buffer_object
*bufObj
;
1084 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1085 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1086 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1087 "%s(non-existent buffer object %u)", caller
, buffer
);
1096 _mesa_begin_bufferobj_lookups(struct gl_context
*ctx
)
1098 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1103 _mesa_end_bufferobj_lookups(struct gl_context
*ctx
)
1105 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1110 * Look up a buffer object for a multi-bind function.
1112 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1113 * of generating an error if the buffer ID is not zero or the name
1114 * of an existing buffer object.
1116 * If the buffer ID refers to an existing buffer object, a pointer
1117 * to the buffer object is returned. If the ID is zero, a pointer
1118 * to the shared NullBufferObj is returned. If the ID is not zero
1119 * and does not refer to a valid buffer object, this function
1122 * This function assumes that the caller has already locked the
1123 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1125 struct gl_buffer_object
*
1126 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1127 const GLuint
*buffers
,
1128 GLuint index
, const char *caller
)
1130 struct gl_buffer_object
*bufObj
;
1132 if (buffers
[index
] != 0) {
1133 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1135 /* The multi-bind functions don't create the buffer objects
1136 when they don't exist. */
1137 if (bufObj
== &DummyBufferObject
)
1140 bufObj
= ctx
->Shared
->NullBufferObj
;
1143 /* The ARB_multi_bind spec says:
1145 * "An INVALID_OPERATION error is generated if any value
1146 * in <buffers> is not zero or the name of an existing
1147 * buffer object (per binding)."
1149 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1150 "%s(buffers[%u]=%u is not zero or the name "
1151 "of an existing buffer object)",
1152 caller
, index
, buffers
[index
]);
1160 * If *ptr points to obj, set ptr = the Null/default buffer object.
1161 * This is a helper for buffer object deletion.
1162 * The GL spec says that deleting a buffer object causes it to get
1163 * unbound from all arrays in the current context.
1166 unbind(struct gl_context
*ctx
,
1167 struct gl_buffer_object
**ptr
,
1168 struct gl_buffer_object
*obj
)
1171 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
1177 * Plug default/fallback buffer object functions into the device
1181 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1183 /* GL_ARB_vertex/pixel_buffer_object */
1184 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1185 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1186 driver
->BufferData
= buffer_data_fallback
;
1187 driver
->BufferSubData
= buffer_sub_data_fallback
;
1188 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
1189 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1191 /* GL_ARB_clear_buffer_object */
1192 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1194 /* GL_ARB_map_buffer_range */
1195 driver
->MapBufferRange
= map_buffer_range_fallback
;
1196 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1198 /* GL_ARB_copy_buffer */
1199 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1204 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1205 struct gl_buffer_object
*bufObj
)
1209 for (i
= 0; i
< MAP_COUNT
; i
++) {
1210 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1211 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1212 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1213 bufObj
->Mappings
[i
].AccessFlags
= 0;
1219 /**********************************************************************/
1221 /**********************************************************************/
1224 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1226 GET_CURRENT_CONTEXT(ctx
);
1228 if (MESA_VERBOSE
& VERBOSE_API
) {
1229 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1230 _mesa_enum_to_string(target
), buffer
);
1233 bind_buffer_object(ctx
, target
, buffer
);
1238 * Delete a set of buffer objects.
1240 * \param n Number of buffer objects to delete.
1241 * \param ids Array of \c n buffer object IDs.
1244 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1246 GET_CURRENT_CONTEXT(ctx
);
1248 FLUSH_VERTICES(ctx
, 0);
1251 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1255 mtx_lock(&ctx
->Shared
->Mutex
);
1257 for (i
= 0; i
< n
; i
++) {
1258 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
1260 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1263 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1265 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1267 /* unbind any vertex pointers bound to this buffer */
1268 for (j
= 0; j
< ARRAY_SIZE(vao
->VertexBinding
); j
++) {
1269 unbind(ctx
, &vao
->VertexBinding
[j
].BufferObj
, bufObj
);
1272 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1273 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
1275 if (vao
->IndexBufferObj
== bufObj
) {
1276 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1279 /* unbind ARB_draw_indirect binding point */
1280 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1281 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER
, 0 );
1284 /* unbind ARB_indirect_parameters binding point */
1285 if (ctx
->ParameterBuffer
== bufObj
) {
1286 _mesa_BindBuffer(GL_PARAMETER_BUFFER_ARB
, 0);
1289 /* unbind ARB_compute_shader binding point */
1290 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1291 _mesa_BindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, 0);
1294 /* unbind ARB_copy_buffer binding points */
1295 if (ctx
->CopyReadBuffer
== bufObj
) {
1296 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
1298 if (ctx
->CopyWriteBuffer
== bufObj
) {
1299 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
1302 /* unbind transform feedback binding points */
1303 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1304 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
1306 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1307 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1308 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1312 /* unbind UBO binding points */
1313 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1314 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1315 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1319 if (ctx
->UniformBuffer
== bufObj
) {
1320 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
1323 /* unbind SSBO binding points */
1324 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1325 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1326 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER
, j
, 0);
1330 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1331 _mesa_BindBuffer(GL_SHADER_STORAGE_BUFFER
, 0);
1334 /* unbind Atomci Buffer binding points */
1335 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1336 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1337 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1341 if (ctx
->AtomicBuffer
== bufObj
) {
1342 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER
, 0 );
1345 /* unbind any pixel pack/unpack pointers bound to this buffer */
1346 if (ctx
->Pack
.BufferObj
== bufObj
) {
1347 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1349 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1350 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1353 if (ctx
->Texture
.BufferObject
== bufObj
) {
1354 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
1357 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1358 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
, 0);
1361 /* The ID is immediately freed for re-use */
1362 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
1363 /* Make sure we do not run into the classic ABA problem on bind.
1364 * We don't want to allow re-binding a buffer object that's been
1365 * "deleted" by glDeleteBuffers().
1367 * The explicit rebinding to the default object in the current context
1368 * prevents the above in the current context, but another context
1369 * sharing the same objects might suffer from this problem.
1370 * The alternative would be to do the hash lookup in any case on bind
1371 * which would introduce more runtime overhead than this.
1373 bufObj
->DeletePending
= GL_TRUE
;
1374 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1378 mtx_unlock(&ctx
->Shared
->Mutex
);
1383 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1384 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1388 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1390 GET_CURRENT_CONTEXT(ctx
);
1393 struct gl_buffer_object
*buf
;
1395 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1397 if (MESA_VERBOSE
& VERBOSE_API
)
1398 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1401 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1410 * This must be atomic (generation and allocation of buffer object IDs)
1412 mtx_lock(&ctx
->Shared
->Mutex
);
1414 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1416 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1417 * DummyBufferObject. Otherwise, create a new buffer object and insert
1420 for (i
= 0; i
< n
; i
++) {
1421 buffers
[i
] = first
+ i
;
1423 assert(ctx
->Driver
.NewBufferObject
);
1424 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1426 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1427 mtx_unlock(&ctx
->Shared
->Mutex
);
1432 buf
= &DummyBufferObject
;
1434 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1437 mtx_unlock(&ctx
->Shared
->Mutex
);
1441 * Generate a set of unique buffer object IDs and store them in \c buffers.
1443 * \param n Number of IDs to generate.
1444 * \param buffers Array of \c n locations to store the IDs.
1447 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1449 create_buffers(n
, buffers
, false);
1453 * Create a set of buffer objects and store their unique IDs in \c buffers.
1455 * \param n Number of IDs to generate.
1456 * \param buffers Array of \c n locations to store the IDs.
1459 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1461 create_buffers(n
, buffers
, true);
1466 * Determine if ID is the name of a buffer object.
1468 * \param id ID of the potential buffer object.
1469 * \return \c GL_TRUE if \c id is the name of a buffer object,
1470 * \c GL_FALSE otherwise.
1472 GLboolean GLAPIENTRY
1473 _mesa_IsBuffer(GLuint id
)
1475 struct gl_buffer_object
*bufObj
;
1476 GET_CURRENT_CONTEXT(ctx
);
1477 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1479 mtx_lock(&ctx
->Shared
->Mutex
);
1480 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1481 mtx_unlock(&ctx
->Shared
->Mutex
);
1483 return bufObj
&& bufObj
!= &DummyBufferObject
;
1488 _mesa_buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1489 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1490 GLbitfield flags
, const char *func
)
1493 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1497 if (flags
& ~(GL_MAP_READ_BIT
|
1499 GL_MAP_PERSISTENT_BIT
|
1500 GL_MAP_COHERENT_BIT
|
1501 GL_DYNAMIC_STORAGE_BIT
|
1502 GL_CLIENT_STORAGE_BIT
)) {
1503 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1507 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1508 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1509 _mesa_error(ctx
, GL_INVALID_VALUE
,
1510 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1514 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1515 _mesa_error(ctx
, GL_INVALID_VALUE
,
1516 "%s(COHERENT and flags!=PERSISTENT)", func
);
1520 if (bufObj
->Immutable
) {
1521 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1525 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1526 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1528 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1530 bufObj
->Written
= GL_TRUE
;
1531 bufObj
->Immutable
= GL_TRUE
;
1532 bufObj
->MinMaxCacheDirty
= true;
1534 assert(ctx
->Driver
.BufferData
);
1535 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1537 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1538 /* Even though the interaction between AMD_pinned_memory and
1539 * glBufferStorage is not described in the spec, Graham Sellers
1540 * said that it should behave the same as glBufferData.
1542 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1545 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1551 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1554 GET_CURRENT_CONTEXT(ctx
);
1555 struct gl_buffer_object
*bufObj
;
1557 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1561 _mesa_buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
,
1566 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1569 GET_CURRENT_CONTEXT(ctx
);
1570 struct gl_buffer_object
*bufObj
;
1572 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferStorage");
1577 * In direct state access, buffer objects have an unspecified target since
1578 * they are not required to be bound.
1580 _mesa_buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
,
1581 "glNamedBufferStorage");
1586 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1587 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1588 GLenum usage
, const char *func
)
1592 if (MESA_VERBOSE
& VERBOSE_API
) {
1593 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1595 _mesa_enum_to_string(target
),
1596 (long int) size
, data
,
1597 _mesa_enum_to_string(usage
));
1601 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1606 case GL_STREAM_DRAW_ARB
:
1607 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1610 case GL_STATIC_DRAW_ARB
:
1611 case GL_DYNAMIC_DRAW_ARB
:
1615 case GL_STREAM_READ_ARB
:
1616 case GL_STREAM_COPY_ARB
:
1617 case GL_STATIC_READ_ARB
:
1618 case GL_STATIC_COPY_ARB
:
1619 case GL_DYNAMIC_READ_ARB
:
1620 case GL_DYNAMIC_COPY_ARB
:
1621 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1625 valid_usage
= false;
1630 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1631 _mesa_enum_to_string(usage
));
1635 if (bufObj
->Immutable
) {
1636 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1640 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1641 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1643 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1645 bufObj
->Written
= GL_TRUE
;
1646 bufObj
->MinMaxCacheDirty
= true;
1649 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1650 bufObj
->Name
, size
, data
, usage
);
1657 assert(ctx
->Driver
.BufferData
);
1658 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1661 GL_DYNAMIC_STORAGE_BIT
,
1663 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1664 /* From GL_AMD_pinned_memory:
1666 * INVALID_OPERATION is generated by BufferData if <target> is
1667 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1668 * mapped to the GPU address space.
1670 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1673 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1679 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1680 const GLvoid
*data
, GLenum usage
)
1682 GET_CURRENT_CONTEXT(ctx
);
1683 struct gl_buffer_object
*bufObj
;
1685 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1689 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1694 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1697 GET_CURRENT_CONTEXT(ctx
);
1698 struct gl_buffer_object
*bufObj
;
1700 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1704 /* In direct state access, buffer objects have an unspecified target since
1705 * they are not required to be bound.
1707 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1708 "glNamedBufferData");
1713 * Implementation for glBufferSubData and glNamedBufferSubData.
1715 * \param ctx GL context.
1716 * \param bufObj The buffer object.
1717 * \param offset Offset of the first byte of the subdata range.
1718 * \param size Size, in bytes, of the subdata range.
1719 * \param data The data store.
1720 * \param func Name of calling function for recording errors.
1724 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1725 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1728 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1730 /* error already recorded */
1734 if (bufObj
->Immutable
&&
1735 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1736 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1743 bufObj
->NumSubDataCalls
++;
1745 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
1746 bufObj
->Usage
== GL_STATIC_COPY
) &&
1747 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
) {
1748 /* If the application declared the buffer as static draw/copy or stream
1749 * draw, it should not be frequently modified with glBufferSubData.
1751 BUFFER_USAGE_WARNING(ctx
,
1752 "using %s(buffer %u, offset %u, size %u) to "
1753 "update a %s buffer",
1754 func
, bufObj
->Name
, offset
, size
,
1755 _mesa_enum_to_string(bufObj
->Usage
));
1758 bufObj
->Written
= GL_TRUE
;
1759 bufObj
->MinMaxCacheDirty
= true;
1761 assert(ctx
->Driver
.BufferSubData
);
1762 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1766 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1767 GLsizeiptr size
, const GLvoid
*data
)
1769 GET_CURRENT_CONTEXT(ctx
);
1770 struct gl_buffer_object
*bufObj
;
1772 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1776 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1780 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1781 GLsizeiptr size
, const GLvoid
*data
)
1783 GET_CURRENT_CONTEXT(ctx
);
1784 struct gl_buffer_object
*bufObj
;
1786 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1790 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1791 "glNamedBufferSubData");
1796 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1797 GLsizeiptr size
, GLvoid
*data
)
1799 GET_CURRENT_CONTEXT(ctx
);
1800 struct gl_buffer_object
*bufObj
;
1802 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1803 GL_INVALID_OPERATION
);
1807 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1808 "glGetBufferSubData")) {
1812 assert(ctx
->Driver
.GetBufferSubData
);
1813 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1817 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
1818 GLsizeiptr size
, GLvoid
*data
)
1820 GET_CURRENT_CONTEXT(ctx
);
1821 struct gl_buffer_object
*bufObj
;
1823 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1824 "glGetNamedBufferSubData");
1828 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1829 "glGetNamedBufferSubData")) {
1833 assert(ctx
->Driver
.GetBufferSubData
);
1834 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1839 * \param subdata true if caller is *SubData, false if *Data
1842 _mesa_clear_buffer_sub_data(struct gl_context
*ctx
,
1843 struct gl_buffer_object
*bufObj
,
1844 GLenum internalformat
,
1845 GLintptr offset
, GLsizeiptr size
,
1846 GLenum format
, GLenum type
,
1848 const char *func
, bool subdata
)
1850 mesa_format mesaFormat
;
1851 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1852 GLsizeiptr clearValueSize
;
1854 /* This checks for disallowed mappings. */
1855 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1860 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1861 format
, type
, func
);
1863 if (mesaFormat
== MESA_FORMAT_NONE
) {
1867 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1868 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1869 _mesa_error(ctx
, GL_INVALID_VALUE
,
1870 "%s(offset or size is not a multiple of "
1871 "internalformat size)", func
);
1875 /* Bail early. Negative size has already been checked. */
1879 bufObj
->MinMaxCacheDirty
= true;
1882 /* clear to zeros, per the spec */
1883 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1884 NULL
, clearValueSize
, bufObj
);
1888 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1889 format
, type
, data
, func
)) {
1893 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1894 clearValue
, clearValueSize
, bufObj
);
1898 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1899 GLenum type
, const GLvoid
*data
)
1901 GET_CURRENT_CONTEXT(ctx
);
1902 struct gl_buffer_object
*bufObj
;
1904 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1908 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1910 "glClearBufferData", false);
1914 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1915 GLenum format
, GLenum type
, const GLvoid
*data
)
1917 GET_CURRENT_CONTEXT(ctx
);
1918 struct gl_buffer_object
*bufObj
;
1920 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1924 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1926 "glClearNamedBufferData", false);
1931 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1932 GLintptr offset
, GLsizeiptr size
,
1933 GLenum format
, GLenum type
,
1936 GET_CURRENT_CONTEXT(ctx
);
1937 struct gl_buffer_object
*bufObj
;
1939 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1943 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1945 "glClearBufferSubData", true);
1949 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1950 GLintptr offset
, GLsizeiptr size
,
1951 GLenum format
, GLenum type
,
1954 GET_CURRENT_CONTEXT(ctx
);
1955 struct gl_buffer_object
*bufObj
;
1957 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1958 "glClearNamedBufferSubData");
1962 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1964 "glClearNamedBufferSubData", true);
1969 _mesa_unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1972 GLboolean status
= GL_TRUE
;
1973 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1975 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
1976 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1977 "%s(buffer is not mapped)", func
);
1982 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1983 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1985 /* check that last 100 bytes are still = magic value */
1986 for (i
= 0; i
< 100; i
++) {
1987 GLuint pos
= bufObj
->Size
- i
- 1;
1988 if (buf
[pos
] != 123) {
1989 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1990 " at position %d (value = %u)\n",
1998 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1999 GLuint i
, unchanged
= 0;
2000 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2002 /* check which bytes changed */
2003 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2004 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2011 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2012 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2017 status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2018 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2019 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2020 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2021 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2026 GLboolean GLAPIENTRY
2027 _mesa_UnmapBuffer(GLenum target
)
2029 GET_CURRENT_CONTEXT(ctx
);
2030 struct gl_buffer_object
*bufObj
;
2032 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2036 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2039 GLboolean GLAPIENTRY
2040 _mesa_UnmapNamedBuffer(GLuint buffer
)
2042 GET_CURRENT_CONTEXT(ctx
);
2043 struct gl_buffer_object
*bufObj
;
2045 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2049 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2054 get_buffer_parameter(struct gl_context
*ctx
,
2055 struct gl_buffer_object
*bufObj
, GLenum pname
,
2056 GLint64
*params
, const char *func
)
2059 case GL_BUFFER_SIZE_ARB
:
2060 *params
= bufObj
->Size
;
2062 case GL_BUFFER_USAGE_ARB
:
2063 *params
= bufObj
->Usage
;
2065 case GL_BUFFER_ACCESS_ARB
:
2066 *params
= simplified_access_mode(ctx
,
2067 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2069 case GL_BUFFER_MAPPED_ARB
:
2070 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2072 case GL_BUFFER_ACCESS_FLAGS
:
2073 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2075 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2077 case GL_BUFFER_MAP_OFFSET
:
2078 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2080 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2082 case GL_BUFFER_MAP_LENGTH
:
2083 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2085 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2087 case GL_BUFFER_IMMUTABLE_STORAGE
:
2088 if (!ctx
->Extensions
.ARB_buffer_storage
)
2090 *params
= bufObj
->Immutable
;
2092 case GL_BUFFER_STORAGE_FLAGS
:
2093 if (!ctx
->Extensions
.ARB_buffer_storage
)
2095 *params
= bufObj
->StorageFlags
;
2104 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2105 _mesa_enum_to_string(pname
));
2110 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2112 GET_CURRENT_CONTEXT(ctx
);
2113 struct gl_buffer_object
*bufObj
;
2116 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2117 GL_INVALID_OPERATION
);
2121 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2122 "glGetBufferParameteriv"))
2123 return; /* Error already recorded. */
2125 *params
= (GLint
) parameter
;
2129 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2131 GET_CURRENT_CONTEXT(ctx
);
2132 struct gl_buffer_object
*bufObj
;
2135 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2136 GL_INVALID_OPERATION
);
2140 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2141 "glGetBufferParameteri64v"))
2142 return; /* Error already recorded. */
2144 *params
= parameter
;
2148 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2150 GET_CURRENT_CONTEXT(ctx
);
2151 struct gl_buffer_object
*bufObj
;
2154 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2155 "glGetNamedBufferParameteriv");
2159 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2160 "glGetNamedBufferParameteriv"))
2161 return; /* Error already recorded. */
2163 *params
= (GLint
) parameter
;
2167 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2170 GET_CURRENT_CONTEXT(ctx
);
2171 struct gl_buffer_object
*bufObj
;
2174 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2175 "glGetNamedBufferParameteri64v");
2179 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2180 "glGetNamedBufferParameteri64v"))
2181 return; /* Error already recorded. */
2183 *params
= parameter
;
2188 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2190 GET_CURRENT_CONTEXT(ctx
);
2191 struct gl_buffer_object
*bufObj
;
2193 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2194 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2195 "GL_BUFFER_MAP_POINTER)");
2199 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2200 GL_INVALID_OPERATION
);
2204 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2208 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2210 GET_CURRENT_CONTEXT(ctx
);
2211 struct gl_buffer_object
*bufObj
;
2213 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2214 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2215 "GL_BUFFER_MAP_POINTER)");
2219 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2220 "glGetNamedBufferPointerv");
2224 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2229 _mesa_copy_buffer_sub_data(struct gl_context
*ctx
,
2230 struct gl_buffer_object
*src
,
2231 struct gl_buffer_object
*dst
,
2232 GLintptr readOffset
, GLintptr writeOffset
,
2233 GLsizeiptr size
, const char *func
)
2235 if (_mesa_check_disallowed_mapping(src
)) {
2236 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2237 "%s(readBuffer is mapped)", func
);
2241 if (_mesa_check_disallowed_mapping(dst
)) {
2242 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2243 "%s(writeBuffer is mapped)", func
);
2247 if (readOffset
< 0) {
2248 _mesa_error(ctx
, GL_INVALID_VALUE
,
2249 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2253 if (writeOffset
< 0) {
2254 _mesa_error(ctx
, GL_INVALID_VALUE
,
2255 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2260 _mesa_error(ctx
, GL_INVALID_VALUE
,
2261 "%s(size %d < 0)", func
, (int) size
);
2265 if (readOffset
+ size
> src
->Size
) {
2266 _mesa_error(ctx
, GL_INVALID_VALUE
,
2267 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2268 (int) readOffset
, (int) size
, (int) src
->Size
);
2272 if (writeOffset
+ size
> dst
->Size
) {
2273 _mesa_error(ctx
, GL_INVALID_VALUE
,
2274 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2275 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2280 if (readOffset
+ size
<= writeOffset
) {
2283 else if (writeOffset
+ size
<= readOffset
) {
2287 /* overlapping src/dst is illegal */
2288 _mesa_error(ctx
, GL_INVALID_VALUE
,
2289 "%s(overlapping src/dst)", func
);
2294 dst
->MinMaxCacheDirty
= true;
2296 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2300 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2301 GLintptr readOffset
, GLintptr writeOffset
,
2304 GET_CURRENT_CONTEXT(ctx
);
2305 struct gl_buffer_object
*src
, *dst
;
2307 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2308 GL_INVALID_OPERATION
);
2312 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2313 GL_INVALID_OPERATION
);
2317 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2318 "glCopyBufferSubData");
2322 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2323 GLintptr readOffset
, GLintptr writeOffset
,
2326 GET_CURRENT_CONTEXT(ctx
);
2327 struct gl_buffer_object
*src
, *dst
;
2329 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2330 "glCopyNamedBufferSubData");
2334 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2335 "glCopyNamedBufferSubData");
2339 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2340 "glCopyNamedBufferSubData");
2345 _mesa_map_buffer_range(struct gl_context
*ctx
,
2346 struct gl_buffer_object
*bufObj
,
2347 GLintptr offset
, GLsizeiptr length
,
2348 GLbitfield access
, const char *func
)
2351 GLbitfield allowed_access
;
2353 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
2356 _mesa_error(ctx
, GL_INVALID_VALUE
,
2357 "%s(offset %ld < 0)", func
, (long) offset
);
2362 _mesa_error(ctx
, GL_INVALID_VALUE
,
2363 "%s(length %ld < 0)", func
, (long) length
);
2367 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2369 * "An INVALID_OPERATION error is generated for any of the following
2372 * * <length> is zero."
2374 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2375 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2379 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2383 allowed_access
= GL_MAP_READ_BIT
|
2385 GL_MAP_INVALIDATE_RANGE_BIT
|
2386 GL_MAP_INVALIDATE_BUFFER_BIT
|
2387 GL_MAP_FLUSH_EXPLICIT_BIT
|
2388 GL_MAP_UNSYNCHRONIZED_BIT
;
2390 if (ctx
->Extensions
.ARB_buffer_storage
) {
2391 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2392 GL_MAP_COHERENT_BIT
;
2395 if (access
& ~allowed_access
) {
2396 /* generate an error if any bits other than those allowed are set */
2397 _mesa_error(ctx
, GL_INVALID_VALUE
,
2398 "%s(access has undefined bits set)", func
);
2402 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2403 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2404 "%s(access indicates neither read or write)", func
);
2408 if ((access
& GL_MAP_READ_BIT
) &&
2409 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2410 GL_MAP_INVALIDATE_BUFFER_BIT
|
2411 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2412 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2413 "%s(read access with disallowed bits)", func
);
2417 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2418 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2419 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2420 "%s(access has flush explicit without write)", func
);
2424 if (access
& GL_MAP_READ_BIT
&&
2425 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2426 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2427 "%s(buffer does not allow read access)", func
);
2431 if (access
& GL_MAP_WRITE_BIT
&&
2432 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2433 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2434 "%s(buffer does not allow write access)", func
);
2438 if (access
& GL_MAP_COHERENT_BIT
&&
2439 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2440 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2441 "%s(buffer does not allow coherent access)", func
);
2445 if (access
& GL_MAP_PERSISTENT_BIT
&&
2446 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2447 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2448 "%s(buffer does not allow persistent access)", func
);
2452 if (offset
+ length
> bufObj
->Size
) {
2453 _mesa_error(ctx
, GL_INVALID_VALUE
,
2454 "%s(offset %td + length %td > buffer_size %td)", func
,
2455 offset
, length
, bufObj
->Size
);
2459 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2460 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2461 "%s(buffer already mapped)", func
);
2465 if (!bufObj
->Size
) {
2466 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2470 if (access
& GL_MAP_WRITE_BIT
) {
2471 bufObj
->NumMapBufferWriteCalls
++;
2472 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2473 bufObj
->Usage
== GL_STATIC_COPY
) &&
2474 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
2475 BUFFER_USAGE_WARNING(ctx
,
2476 "using %s(buffer %u, offset %u, length %u) to "
2477 "update a %s buffer",
2478 func
, bufObj
->Name
, offset
, length
,
2479 _mesa_enum_to_string(bufObj
->Usage
));
2483 assert(ctx
->Driver
.MapBufferRange
);
2484 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2487 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2490 /* The driver callback should have set all these fields.
2491 * This is important because other modules (like VBO) might call
2492 * the driver function directly.
2494 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2495 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2496 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2497 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2500 if (access
& GL_MAP_WRITE_BIT
) {
2501 bufObj
->Written
= GL_TRUE
;
2502 bufObj
->MinMaxCacheDirty
= true;
2506 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2507 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2508 bufObj
->Name
, bufObj
->Size
, access
);
2509 /* Access must be write only */
2510 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2512 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2513 for (i
= 0; i
< bufObj
->Size
; i
++)
2520 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2521 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2523 /* buffer is 100 bytes larger than requested, fill with magic value */
2524 for (i
= 0; i
< 100; i
++) {
2525 buf
[bufObj
->Size
- i
- 1] = 123;
2534 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2537 GET_CURRENT_CONTEXT(ctx
);
2538 struct gl_buffer_object
*bufObj
;
2540 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2541 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2542 "glMapBufferRange(ARB_map_buffer_range not supported)");
2546 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2550 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2551 "glMapBufferRange");
2555 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2558 GET_CURRENT_CONTEXT(ctx
);
2559 struct gl_buffer_object
*bufObj
;
2561 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2562 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2563 "glMapNamedBufferRange("
2564 "ARB_map_buffer_range not supported)");
2568 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2572 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2573 "glMapNamedBufferRange");
2577 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2578 * flags for input to _mesa_map_buffer_range.
2580 * \return true if the type of requested access is permissible.
2583 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2587 case GL_READ_ONLY_ARB
:
2588 *flags
= GL_MAP_READ_BIT
;
2589 return _mesa_is_desktop_gl(ctx
);
2590 case GL_WRITE_ONLY_ARB
:
2591 *flags
= GL_MAP_WRITE_BIT
;
2593 case GL_READ_WRITE_ARB
:
2594 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2595 return _mesa_is_desktop_gl(ctx
);
2602 _mesa_MapBuffer(GLenum target
, GLenum access
)
2604 GET_CURRENT_CONTEXT(ctx
);
2605 struct gl_buffer_object
*bufObj
;
2606 GLbitfield accessFlags
;
2608 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2609 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2613 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2617 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2622 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2624 GET_CURRENT_CONTEXT(ctx
);
2625 struct gl_buffer_object
*bufObj
;
2626 GLbitfield accessFlags
;
2628 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2629 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2633 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2637 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2638 "glMapNamedBuffer");
2643 _mesa_flush_mapped_buffer_range(struct gl_context
*ctx
,
2644 struct gl_buffer_object
*bufObj
,
2645 GLintptr offset
, GLsizeiptr length
,
2648 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2649 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2650 "%s(ARB_map_buffer_range not supported)", func
);
2655 _mesa_error(ctx
, GL_INVALID_VALUE
,
2656 "%s(offset %ld < 0)", func
, (long) offset
);
2661 _mesa_error(ctx
, GL_INVALID_VALUE
,
2662 "%s(length %ld < 0)", func
, (long) length
);
2666 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2667 /* buffer is not mapped */
2668 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2669 "%s(buffer is not mapped)", func
);
2673 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2674 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2675 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2676 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2680 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2681 _mesa_error(ctx
, GL_INVALID_VALUE
,
2682 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2683 (long) offset
, (long) length
,
2684 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2688 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2690 if (ctx
->Driver
.FlushMappedBufferRange
)
2691 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2696 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2699 GET_CURRENT_CONTEXT(ctx
);
2700 struct gl_buffer_object
*bufObj
;
2702 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2703 GL_INVALID_OPERATION
);
2707 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2708 "glFlushMappedBufferRange");
2712 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2715 GET_CURRENT_CONTEXT(ctx
);
2716 struct gl_buffer_object
*bufObj
;
2718 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2719 "glFlushMappedNamedBufferRange");
2723 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2724 "glFlushMappedNamedBufferRange");
2729 * Binds a buffer object to a uniform buffer binding point.
2731 * The caller is responsible for flushing vertices and updating
2735 set_ubo_binding(struct gl_context
*ctx
,
2736 struct gl_uniform_buffer_binding
*binding
,
2737 struct gl_buffer_object
*bufObj
,
2742 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2744 binding
->Offset
= offset
;
2745 binding
->Size
= size
;
2746 binding
->AutomaticSize
= autoSize
;
2748 /* If this is a real buffer object, mark it has having been used
2749 * at some point as a UBO.
2752 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
2756 * Binds a buffer object to a shader storage buffer binding point.
2758 * The caller is responsible for flushing vertices and updating
2762 set_ssbo_binding(struct gl_context
*ctx
,
2763 struct gl_shader_storage_buffer_binding
*binding
,
2764 struct gl_buffer_object
*bufObj
,
2769 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2771 binding
->Offset
= offset
;
2772 binding
->Size
= size
;
2773 binding
->AutomaticSize
= autoSize
;
2775 /* If this is a real buffer object, mark it has having been used
2776 * at some point as a SSBO.
2779 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
2783 * Binds a buffer object to a uniform buffer binding point.
2785 * Unlike set_ubo_binding(), this function also flushes vertices
2786 * and updates NewDriverState. It also checks if the binding
2787 * has actually changed before updating it.
2790 bind_uniform_buffer(struct gl_context
*ctx
,
2792 struct gl_buffer_object
*bufObj
,
2797 struct gl_uniform_buffer_binding
*binding
=
2798 &ctx
->UniformBufferBindings
[index
];
2800 if (binding
->BufferObject
== bufObj
&&
2801 binding
->Offset
== offset
&&
2802 binding
->Size
== size
&&
2803 binding
->AutomaticSize
== autoSize
) {
2807 FLUSH_VERTICES(ctx
, 0);
2808 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
2810 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2814 * Binds a buffer object to a shader storage buffer binding point.
2816 * Unlike set_ssbo_binding(), this function also flushes vertices
2817 * and updates NewDriverState. It also checks if the binding
2818 * has actually changed before updating it.
2821 bind_shader_storage_buffer(struct gl_context
*ctx
,
2823 struct gl_buffer_object
*bufObj
,
2828 struct gl_shader_storage_buffer_binding
*binding
=
2829 &ctx
->ShaderStorageBufferBindings
[index
];
2831 if (binding
->BufferObject
== bufObj
&&
2832 binding
->Offset
== offset
&&
2833 binding
->Size
== size
&&
2834 binding
->AutomaticSize
== autoSize
) {
2838 FLUSH_VERTICES(ctx
, 0);
2839 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
2841 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2845 * Bind a region of a buffer object to a uniform block binding point.
2846 * \param index the uniform buffer binding point index
2847 * \param bufObj the buffer object
2848 * \param offset offset to the start of buffer object region
2849 * \param size size of the buffer object region
2852 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2854 struct gl_buffer_object
*bufObj
,
2858 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2859 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2863 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2864 _mesa_error(ctx
, GL_INVALID_VALUE
,
2865 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2866 ctx
->Const
.UniformBufferOffsetAlignment
);
2870 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2875 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2876 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2880 * Bind a region of a buffer object to a shader storage block binding point.
2881 * \param index the shader storage buffer binding point index
2882 * \param bufObj the buffer object
2883 * \param offset offset to the start of buffer object region
2884 * \param size size of the buffer object region
2887 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
2889 struct gl_buffer_object
*bufObj
,
2893 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2894 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2898 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
2899 _mesa_error(ctx
, GL_INVALID_VALUE
,
2900 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2901 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
2905 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2910 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2911 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2915 * Bind a buffer object to a uniform block binding point.
2916 * As above, but offset = 0.
2919 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2921 struct gl_buffer_object
*bufObj
)
2923 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2924 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2928 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2930 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2931 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2933 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2937 * Bind a buffer object to a shader storage block binding point.
2938 * As above, but offset = 0.
2941 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
2943 struct gl_buffer_object
*bufObj
)
2945 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2946 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2950 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2952 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2953 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2955 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2959 * Binds a buffer object to an atomic buffer binding point.
2961 * The caller is responsible for validating the offset,
2962 * flushing the vertices and updating NewDriverState.
2965 set_atomic_buffer_binding(struct gl_context
*ctx
,
2966 struct gl_atomic_buffer_binding
*binding
,
2967 struct gl_buffer_object
*bufObj
,
2971 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2973 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2974 binding
->Offset
= -1;
2977 binding
->Offset
= offset
;
2978 binding
->Size
= size
;
2979 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
2984 * Binds a buffer object to an atomic buffer binding point.
2986 * Unlike set_atomic_buffer_binding(), this function also validates the
2987 * index and offset, flushes vertices, and updates NewDriverState.
2988 * It also checks if the binding has actually changing before
2992 bind_atomic_buffer(struct gl_context
*ctx
,
2994 struct gl_buffer_object
*bufObj
,
2999 struct gl_atomic_buffer_binding
*binding
;
3001 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3002 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
3006 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3007 _mesa_error(ctx
, GL_INVALID_VALUE
,
3008 "%s(offset misaligned %d/%d)", name
, (int) offset
,
3009 ATOMIC_COUNTER_SIZE
);
3013 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3015 binding
= &ctx
->AtomicBufferBindings
[index
];
3016 if (binding
->BufferObject
== bufObj
&&
3017 binding
->Offset
== offset
&&
3018 binding
->Size
== size
) {
3022 FLUSH_VERTICES(ctx
, 0);
3023 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3025 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3029 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3031 const GLintptr
*offsets
,
3032 const GLsizeiptr
*sizes
)
3034 if (offsets
[index
] < 0) {
3035 /* The ARB_multi_bind spec says:
3037 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3038 * value in <offsets> is less than zero (per binding)."
3040 _mesa_error(ctx
, GL_INVALID_VALUE
,
3041 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3042 index
, (int64_t) offsets
[index
]);
3046 if (sizes
[index
] <= 0) {
3047 /* The ARB_multi_bind spec says:
3049 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3050 * value in <sizes> is less than or equal to zero (per binding)."
3052 _mesa_error(ctx
, GL_INVALID_VALUE
,
3053 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3054 index
, (int64_t) sizes
[index
]);
3062 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3063 GLuint first
, GLsizei count
,
3066 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3067 _mesa_error(ctx
, GL_INVALID_ENUM
,
3068 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3072 /* The ARB_multi_bind_spec says:
3074 * "An INVALID_OPERATION error is generated if <first> + <count> is
3075 * greater than the number of target-specific indexed binding points,
3076 * as described in section 6.7.1."
3078 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3079 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3080 "%s(first=%u + count=%d > the value of "
3081 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3082 caller
, first
, count
,
3083 ctx
->Const
.MaxUniformBufferBindings
);
3091 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3092 GLuint first
, GLsizei count
,
3095 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3096 _mesa_error(ctx
, GL_INVALID_ENUM
,
3097 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3101 /* The ARB_multi_bind_spec says:
3103 * "An INVALID_OPERATION error is generated if <first> + <count> is
3104 * greater than the number of target-specific indexed binding points,
3105 * as described in section 6.7.1."
3107 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3108 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3109 "%s(first=%u + count=%d > the value of "
3110 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3111 caller
, first
, count
,
3112 ctx
->Const
.MaxShaderStorageBufferBindings
);
3120 * Unbind all uniform buffers in the range
3121 * <first> through <first>+<count>-1
3124 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3126 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3129 for (i
= 0; i
< count
; i
++)
3130 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3131 bufObj
, -1, -1, GL_TRUE
);
3135 * Unbind all shader storage buffers in the range
3136 * <first> through <first>+<count>-1
3139 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3142 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3145 for (i
= 0; i
< count
; i
++)
3146 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3147 bufObj
, -1, -1, GL_TRUE
);
3151 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3152 const GLuint
*buffers
,
3154 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3159 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3162 /* Assume that at least one binding will be changed */
3163 FLUSH_VERTICES(ctx
, 0);
3164 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3167 /* The ARB_multi_bind spec says:
3169 * "If <buffers> is NULL, all bindings from <first> through
3170 * <first>+<count>-1 are reset to their unbound (zero) state.
3171 * In this case, the offsets and sizes associated with the
3172 * binding points are set to default values, ignoring
3173 * <offsets> and <sizes>."
3175 unbind_uniform_buffers(ctx
, first
, count
);
3179 /* Note that the error semantics for multi-bind commands differ from
3180 * those of other GL commands.
3182 * The Issues section in the ARB_multi_bind spec says:
3184 * "(11) Typically, OpenGL specifies that if an error is generated by a
3185 * command, that command has no effect. This is somewhat
3186 * unfortunate for multi-bind commands, because it would require a
3187 * first pass to scan the entire list of bound objects for errors
3188 * and then a second pass to actually perform the bindings.
3189 * Should we have different error semantics?
3191 * RESOLVED: Yes. In this specification, when the parameters for
3192 * one of the <count> binding points are invalid, that binding point
3193 * is not updated and an error will be generated. However, other
3194 * binding points in the same command will be updated if their
3195 * parameters are valid and no other error occurs."
3198 _mesa_begin_bufferobj_lookups(ctx
);
3200 for (i
= 0; i
< count
; i
++) {
3201 struct gl_uniform_buffer_binding
*binding
=
3202 &ctx
->UniformBufferBindings
[first
+ i
];
3203 struct gl_buffer_object
*bufObj
;
3204 GLintptr offset
= 0;
3205 GLsizeiptr size
= 0;
3208 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3211 /* The ARB_multi_bind spec says:
3213 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3214 * pair of values in <offsets> and <sizes> does not respectively
3215 * satisfy the constraints described for those parameters for the
3216 * specified target, as described in section 6.7.1 (per binding)."
3218 * Section 6.7.1 refers to table 6.5, which says:
3220 * "┌───────────────────────────────────────────────────────────────┐
3221 * │ Uniform buffer array bindings (see sec. 7.6) │
3222 * ├─────────────────────┬─────────────────────────────────────────┤
3224 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3225 * │ │ OFFSET_ALIGNMENT │
3227 * │ size restriction │ none │
3228 * └─────────────────────┴─────────────────────────────────────────┘"
3230 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3231 _mesa_error(ctx
, GL_INVALID_VALUE
,
3232 "glBindBuffersRange(offsets[%u]=%" PRId64
3233 " is misaligned; it must be a multiple of the value of "
3234 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3235 "target=GL_UNIFORM_BUFFER)",
3236 i
, (int64_t) offsets
[i
],
3237 ctx
->Const
.UniformBufferOffsetAlignment
);
3241 offset
= offsets
[i
];
3245 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3246 bufObj
= binding
->BufferObject
;
3248 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3251 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3252 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3254 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3258 _mesa_end_bufferobj_lookups(ctx
);
3262 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3263 GLsizei count
, const GLuint
*buffers
,
3265 const GLintptr
*offsets
,
3266 const GLsizeiptr
*sizes
,
3271 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
3274 /* Assume that at least one binding will be changed */
3275 FLUSH_VERTICES(ctx
, 0);
3276 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3279 /* The ARB_multi_bind spec says:
3281 * "If <buffers> is NULL, all bindings from <first> through
3282 * <first>+<count>-1 are reset to their unbound (zero) state.
3283 * In this case, the offsets and sizes associated with the
3284 * binding points are set to default values, ignoring
3285 * <offsets> and <sizes>."
3287 unbind_shader_storage_buffers(ctx
, first
, count
);
3291 /* Note that the error semantics for multi-bind commands differ from
3292 * those of other GL commands.
3294 * The Issues section in the ARB_multi_bind spec says:
3296 * "(11) Typically, OpenGL specifies that if an error is generated by a
3297 * command, that command has no effect. This is somewhat
3298 * unfortunate for multi-bind commands, because it would require a
3299 * first pass to scan the entire list of bound objects for errors
3300 * and then a second pass to actually perform the bindings.
3301 * Should we have different error semantics?
3303 * RESOLVED: Yes. In this specification, when the parameters for
3304 * one of the <count> binding points are invalid, that binding point
3305 * is not updated and an error will be generated. However, other
3306 * binding points in the same command will be updated if their
3307 * parameters are valid and no other error occurs."
3310 _mesa_begin_bufferobj_lookups(ctx
);
3312 for (i
= 0; i
< count
; i
++) {
3313 struct gl_shader_storage_buffer_binding
*binding
=
3314 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3315 struct gl_buffer_object
*bufObj
;
3316 GLintptr offset
= 0;
3317 GLsizeiptr size
= 0;
3320 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3323 /* The ARB_multi_bind spec says:
3325 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3326 * pair of values in <offsets> and <sizes> does not respectively
3327 * satisfy the constraints described for those parameters for the
3328 * specified target, as described in section 6.7.1 (per binding)."
3330 * Section 6.7.1 refers to table 6.5, which says:
3332 * "┌───────────────────────────────────────────────────────────────┐
3333 * │ Shader storage buffer array bindings (see sec. 7.8) │
3334 * ├─────────────────────┬─────────────────────────────────────────┤
3336 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3337 * │ │ BUFFER_OFFSET_ALIGNMENT │
3339 * │ size restriction │ none │
3340 * └─────────────────────┴─────────────────────────────────────────┘"
3342 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3343 _mesa_error(ctx
, GL_INVALID_VALUE
,
3344 "glBindBuffersRange(offsets[%u]=%" PRId64
3345 " is misaligned; it must be a multiple of the value of "
3346 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3347 "target=GL_SHADER_STORAGE_BUFFER)",
3348 i
, (int64_t) offsets
[i
],
3349 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3353 offset
= offsets
[i
];
3357 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3358 bufObj
= binding
->BufferObject
;
3360 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3363 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3364 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, !range
);
3366 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, !range
);
3370 _mesa_end_bufferobj_lookups(ctx
);
3374 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3375 struct gl_transform_feedback_object
*tfObj
,
3376 GLuint first
, GLsizei count
, const char *caller
)
3378 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3379 _mesa_error(ctx
, GL_INVALID_ENUM
,
3380 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3384 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3386 * "An INVALID_OPERATION error is generated :
3389 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3390 * FEEDBACK_BUFFER and transform feedback is currently active."
3392 * We assume that this is also meant to apply to BindBuffersRange
3393 * and BindBuffersBase.
3395 if (tfObj
->Active
) {
3396 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3397 "%s(Changing transform feedback buffers while "
3398 "transform feedback is active)", caller
);
3402 /* The ARB_multi_bind_spec says:
3404 * "An INVALID_OPERATION error is generated if <first> + <count> is
3405 * greater than the number of target-specific indexed binding points,
3406 * as described in section 6.7.1."
3408 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3409 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3410 "%s(first=%u + count=%d > the value of "
3411 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3412 caller
, first
, count
,
3413 ctx
->Const
.MaxTransformFeedbackBuffers
);
3421 * Unbind all transform feedback buffers in the range
3422 * <first> through <first>+<count>-1
3425 unbind_xfb_buffers(struct gl_context
*ctx
,
3426 struct gl_transform_feedback_object
*tfObj
,
3427 GLuint first
, GLsizei count
)
3429 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3432 for (i
= 0; i
< count
; i
++)
3433 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3438 bind_xfb_buffers(struct gl_context
*ctx
,
3439 GLuint first
, GLsizei count
,
3440 const GLuint
*buffers
,
3442 const GLintptr
*offsets
,
3443 const GLsizeiptr
*sizes
,
3446 struct gl_transform_feedback_object
*tfObj
=
3447 ctx
->TransformFeedback
.CurrentObject
;
3450 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
3453 /* Assume that at least one binding will be changed */
3454 FLUSH_VERTICES(ctx
, 0);
3455 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3458 /* The ARB_multi_bind spec says:
3460 * "If <buffers> is NULL, all bindings from <first> through
3461 * <first>+<count>-1 are reset to their unbound (zero) state.
3462 * In this case, the offsets and sizes associated with the
3463 * binding points are set to default values, ignoring
3464 * <offsets> and <sizes>."
3466 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3470 /* Note that the error semantics for multi-bind commands differ from
3471 * those of other GL commands.
3473 * The Issues section in the ARB_multi_bind spec says:
3475 * "(11) Typically, OpenGL specifies that if an error is generated by a
3476 * command, that command has no effect. This is somewhat
3477 * unfortunate for multi-bind commands, because it would require a
3478 * first pass to scan the entire list of bound objects for errors
3479 * and then a second pass to actually perform the bindings.
3480 * Should we have different error semantics?
3482 * RESOLVED: Yes. In this specification, when the parameters for
3483 * one of the <count> binding points are invalid, that binding point
3484 * is not updated and an error will be generated. However, other
3485 * binding points in the same command will be updated if their
3486 * parameters are valid and no other error occurs."
3489 _mesa_begin_bufferobj_lookups(ctx
);
3491 for (i
= 0; i
< count
; i
++) {
3492 const GLuint index
= first
+ i
;
3493 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3494 struct gl_buffer_object
*bufObj
;
3495 GLintptr offset
= 0;
3496 GLsizeiptr size
= 0;
3499 offset
= offsets
[i
];
3502 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3505 /* The ARB_multi_bind spec says:
3507 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3508 * pair of values in <offsets> and <sizes> does not respectively
3509 * satisfy the constraints described for those parameters for the
3510 * specified target, as described in section 6.7.1 (per binding)."
3512 * Section 6.7.1 refers to table 6.5, which says:
3514 * "┌───────────────────────────────────────────────────────────────┐
3515 * │ Transform feedback array bindings (see sec. 13.2.2) │
3516 * ├───────────────────────┬───────────────────────────────────────┤
3518 * │ offset restriction │ multiple of 4 │
3520 * │ size restriction │ multiple of 4 │
3521 * └───────────────────────┴───────────────────────────────────────┘"
3523 if (offsets
[i
] & 0x3) {
3524 _mesa_error(ctx
, GL_INVALID_VALUE
,
3525 "glBindBuffersRange(offsets[%u]=%" PRId64
3526 " is misaligned; it must be a multiple of 4 when "
3527 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3528 i
, (int64_t) offsets
[i
]);
3532 if (sizes
[i
] & 0x3) {
3533 _mesa_error(ctx
, GL_INVALID_VALUE
,
3534 "glBindBuffersRange(sizes[%u]=%" PRId64
3535 " is misaligned; it must be a multiple of 4 when "
3536 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3537 i
, (int64_t) sizes
[i
]);
3541 offset
= offsets
[i
];
3545 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3546 bufObj
= boundBufObj
;
3548 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3551 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
3555 _mesa_end_bufferobj_lookups(ctx
);
3559 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
3560 GLuint first
, GLsizei count
,
3563 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
3564 _mesa_error(ctx
, GL_INVALID_ENUM
,
3565 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
3569 /* The ARB_multi_bind_spec says:
3571 * "An INVALID_OPERATION error is generated if <first> + <count> is
3572 * greater than the number of target-specific indexed binding points,
3573 * as described in section 6.7.1."
3575 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
3576 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3577 "%s(first=%u + count=%d > the value of "
3578 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3579 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
3587 * Unbind all atomic counter buffers in the range
3588 * <first> through <first>+<count>-1
3591 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3593 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3596 for (i
= 0; i
< count
; i
++)
3597 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
3602 bind_atomic_buffers(struct gl_context
*ctx
,
3605 const GLuint
*buffers
,
3607 const GLintptr
*offsets
,
3608 const GLsizeiptr
*sizes
,
3613 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
3616 /* Assume that at least one binding will be changed */
3617 FLUSH_VERTICES(ctx
, 0);
3618 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3621 /* The ARB_multi_bind spec says:
3623 * "If <buffers> is NULL, all bindings from <first> through
3624 * <first>+<count>-1 are reset to their unbound (zero) state.
3625 * In this case, the offsets and sizes associated with the
3626 * binding points are set to default values, ignoring
3627 * <offsets> and <sizes>."
3629 unbind_atomic_buffers(ctx
, first
, count
);
3633 /* Note that the error semantics for multi-bind commands differ from
3634 * those of other GL commands.
3636 * The Issues section in the ARB_multi_bind spec says:
3638 * "(11) Typically, OpenGL specifies that if an error is generated by a
3639 * command, that command has no effect. This is somewhat
3640 * unfortunate for multi-bind commands, because it would require a
3641 * first pass to scan the entire list of bound objects for errors
3642 * and then a second pass to actually perform the bindings.
3643 * Should we have different error semantics?
3645 * RESOLVED: Yes. In this specification, when the parameters for
3646 * one of the <count> binding points are invalid, that binding point
3647 * is not updated and an error will be generated. However, other
3648 * binding points in the same command will be updated if their
3649 * parameters are valid and no other error occurs."
3652 _mesa_begin_bufferobj_lookups(ctx
);
3654 for (i
= 0; i
< count
; i
++) {
3655 struct gl_atomic_buffer_binding
*binding
=
3656 &ctx
->AtomicBufferBindings
[first
+ i
];
3657 struct gl_buffer_object
*bufObj
;
3658 GLintptr offset
= 0;
3659 GLsizeiptr size
= 0;
3662 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3665 /* The ARB_multi_bind spec says:
3667 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3668 * pair of values in <offsets> and <sizes> does not respectively
3669 * satisfy the constraints described for those parameters for the
3670 * specified target, as described in section 6.7.1 (per binding)."
3672 * Section 6.7.1 refers to table 6.5, which says:
3674 * "┌───────────────────────────────────────────────────────────────┐
3675 * │ Atomic counter array bindings (see sec. 7.7.2) │
3676 * ├───────────────────────┬───────────────────────────────────────┤
3678 * │ offset restriction │ multiple of 4 │
3680 * │ size restriction │ none │
3681 * └───────────────────────┴───────────────────────────────────────┘"
3683 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
3684 _mesa_error(ctx
, GL_INVALID_VALUE
,
3685 "glBindBuffersRange(offsets[%u]=%" PRId64
3686 " is misaligned; it must be a multiple of %d when "
3687 "target=GL_ATOMIC_COUNTER_BUFFER)",
3688 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
3692 offset
= offsets
[i
];
3696 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3697 bufObj
= binding
->BufferObject
;
3699 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
);
3702 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
3705 _mesa_end_bufferobj_lookups(ctx
);
3709 _mesa_BindBufferRange(GLenum target
, GLuint index
,
3710 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
3712 GET_CURRENT_CONTEXT(ctx
);
3713 struct gl_buffer_object
*bufObj
;
3715 if (MESA_VERBOSE
& VERBOSE_API
) {
3716 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %ld, %ld)\n",
3717 _mesa_enum_to_string(target
), index
, buffer
, offset
, size
);
3721 bufObj
= ctx
->Shared
->NullBufferObj
;
3723 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3725 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3726 &bufObj
, "glBindBufferRange"))
3730 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3731 "glBindBufferRange(invalid buffer=%u)", buffer
);
3737 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
3744 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3745 _mesa_bind_buffer_range_transform_feedback(ctx
,
3746 ctx
->TransformFeedback
.CurrentObject
,
3747 index
, bufObj
, offset
, size
,
3750 case GL_UNIFORM_BUFFER
:
3751 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3753 case GL_SHADER_STORAGE_BUFFER
:
3754 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3756 case GL_ATOMIC_COUNTER_BUFFER
:
3757 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
3758 "glBindBufferRange");
3761 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
3767 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
3769 GET_CURRENT_CONTEXT(ctx
);
3770 struct gl_buffer_object
*bufObj
;
3772 if (MESA_VERBOSE
& VERBOSE_API
) {
3773 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
3774 _mesa_enum_to_string(target
), index
, buffer
);
3778 bufObj
= ctx
->Shared
->NullBufferObj
;
3780 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3782 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3783 &bufObj
, "glBindBufferBase"))
3787 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3788 "glBindBufferBase(invalid buffer=%u)", buffer
);
3792 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3793 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3795 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3796 * zero and size equal to the size of buffer."
3798 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3800 * "If the parameter (starting offset or size) was not specified when the
3801 * buffer object was bound, zero is returned."
3803 * What happens if the size of the buffer changes? Does the size of the
3804 * buffer at the moment glBindBufferBase was called still play a role, like
3805 * the first quote would imply, or is the size meaningless in the
3806 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3807 * core spec page 45 says:
3809 * "It is equivalent to calling BindBufferRange with offset zero, while
3810 * size is determined by the size of the bound buffer at the time the
3813 * My interpretation is that the GL 4.1 spec was a clarification of the
3814 * behavior, not a change. In particular, this choice will only make
3815 * rendering work in cases where it would have had undefined results.
3819 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3820 _mesa_bind_buffer_base_transform_feedback(ctx
,
3821 ctx
->TransformFeedback
.CurrentObject
,
3822 index
, bufObj
, false);
3824 case GL_UNIFORM_BUFFER
:
3825 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
3827 case GL_SHADER_STORAGE_BUFFER
:
3828 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
3830 case GL_ATOMIC_COUNTER_BUFFER
:
3831 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
3832 "glBindBufferBase");
3835 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
3841 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
3842 const GLuint
*buffers
,
3843 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3845 GET_CURRENT_CONTEXT(ctx
);
3847 if (MESA_VERBOSE
& VERBOSE_API
) {
3848 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
3849 _mesa_enum_to_string(target
), first
, count
,
3850 buffers
, offsets
, sizes
);
3854 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3855 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3856 "glBindBuffersRange");
3858 case GL_UNIFORM_BUFFER
:
3859 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3860 "glBindBuffersRange");
3862 case GL_SHADER_STORAGE_BUFFER
:
3863 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3864 "glBindBuffersRange");
3866 case GL_ATOMIC_COUNTER_BUFFER
:
3867 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
3868 "glBindBuffersRange");
3871 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
3872 _mesa_enum_to_string(target
));
3878 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
3879 const GLuint
*buffers
)
3881 GET_CURRENT_CONTEXT(ctx
);
3883 if (MESA_VERBOSE
& VERBOSE_API
) {
3884 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
3885 _mesa_enum_to_string(target
), first
, count
, buffers
);
3889 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3890 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3891 "glBindBuffersBase");
3893 case GL_UNIFORM_BUFFER
:
3894 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3895 "glBindBuffersBase");
3897 case GL_SHADER_STORAGE_BUFFER
:
3898 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3899 "glBindBuffersBase");
3901 case GL_ATOMIC_COUNTER_BUFFER
:
3902 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
3903 "glBindBuffersBase");
3906 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
3907 _mesa_enum_to_string(target
));
3913 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
3916 GET_CURRENT_CONTEXT(ctx
);
3917 struct gl_buffer_object
*bufObj
;
3918 const GLintptr end
= offset
+ length
;
3920 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
3921 * Profile) spec says:
3923 * "An INVALID_VALUE error is generated if buffer is zero or is not the
3924 * name of an existing buffer object."
3926 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3927 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
3928 _mesa_error(ctx
, GL_INVALID_VALUE
,
3929 "glInvalidateBufferSubData(name = 0x%x) invalid object",
3934 /* The GL_ARB_invalidate_subdata spec says:
3936 * "An INVALID_VALUE error is generated if <offset> or <length> is
3937 * negative, or if <offset> + <length> is greater than the value of
3940 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
3941 _mesa_error(ctx
, GL_INVALID_VALUE
,
3942 "glInvalidateBufferSubData(invalid offset or length)");
3946 /* The OpenGL 4.4 (Core Profile) spec says:
3948 * "An INVALID_OPERATION error is generated if buffer is currently
3949 * mapped by MapBuffer or if the invalidate range intersects the range
3950 * currently mapped by MapBufferRange, unless it was mapped
3951 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
3953 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
3954 bufferobj_range_mapped(bufObj
, offset
, length
)) {
3955 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3956 "glInvalidateBufferSubData(intersection with mapped "
3961 if (ctx
->Driver
.InvalidateBufferSubData
)
3962 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
3966 _mesa_InvalidateBufferData(GLuint buffer
)
3968 GET_CURRENT_CONTEXT(ctx
);
3969 struct gl_buffer_object
*bufObj
;
3971 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
3972 * Profile) spec says:
3974 * "An INVALID_VALUE error is generated if buffer is zero or is not the
3975 * name of an existing buffer object."
3977 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3978 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
3979 _mesa_error(ctx
, GL_INVALID_VALUE
,
3980 "glInvalidateBufferData(name = 0x%x) invalid object",
3985 /* The OpenGL 4.4 (Core Profile) spec says:
3987 * "An INVALID_OPERATION error is generated if buffer is currently
3988 * mapped by MapBuffer or if the invalidate range intersects the range
3989 * currently mapped by MapBufferRange, unless it was mapped
3990 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
3992 if (_mesa_check_disallowed_mapping(bufObj
)) {
3993 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3994 "glInvalidateBufferData(intersection with mapped "
3999 if (ctx
->Driver
.InvalidateBufferSubData
)
4000 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, 0, bufObj
->Size
);