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.
66 buffer_usage_warning(struct gl_context
*ctx
, const char *fmt
, ...)
72 _mesa_gl_vdebug(ctx
, &msg_id
,
73 MESA_DEBUG_SOURCE_API
,
74 MESA_DEBUG_TYPE_PERFORMANCE
,
75 MESA_DEBUG_SEVERITY_MEDIUM
,
82 * Used as a placeholder for buffer objects between glGenBuffers() and
83 * glBindBuffer() so that glIsBuffer() can work correctly.
85 static struct gl_buffer_object DummyBufferObject
;
89 * Return pointer to address of a buffer object target.
90 * \param ctx the GL context
91 * \param target the buffer object target to be retrieved.
92 * \return pointer to pointer to the buffer object bound to \c target in the
93 * specified context or \c NULL if \c target is invalid.
95 static inline struct gl_buffer_object
**
96 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
98 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
100 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)
101 && target
!= GL_ARRAY_BUFFER
&& target
!= GL_ELEMENT_ARRAY_BUFFER
)
105 case GL_ARRAY_BUFFER_ARB
:
106 return &ctx
->Array
.ArrayBufferObj
;
107 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
108 return &ctx
->Array
.VAO
->IndexBufferObj
;
109 case GL_PIXEL_PACK_BUFFER_EXT
:
110 return &ctx
->Pack
.BufferObj
;
111 case GL_PIXEL_UNPACK_BUFFER_EXT
:
112 return &ctx
->Unpack
.BufferObj
;
113 case GL_COPY_READ_BUFFER
:
114 return &ctx
->CopyReadBuffer
;
115 case GL_COPY_WRITE_BUFFER
:
116 return &ctx
->CopyWriteBuffer
;
117 case GL_DRAW_INDIRECT_BUFFER
:
118 if ((ctx
->API
== API_OPENGL_CORE
&&
119 ctx
->Extensions
.ARB_draw_indirect
) ||
120 _mesa_is_gles31(ctx
)) {
121 return &ctx
->DrawIndirectBuffer
;
124 case GL_DISPATCH_INDIRECT_BUFFER
:
125 if (_mesa_has_compute_shaders(ctx
)) {
126 return &ctx
->DispatchIndirectBuffer
;
129 case GL_TRANSFORM_FEEDBACK_BUFFER
:
130 if (ctx
->Extensions
.EXT_transform_feedback
) {
131 return &ctx
->TransformFeedback
.CurrentBuffer
;
134 case GL_TEXTURE_BUFFER
:
135 if (ctx
->API
== API_OPENGL_CORE
&&
136 ctx
->Extensions
.ARB_texture_buffer_object
) {
137 return &ctx
->Texture
.BufferObject
;
140 case GL_UNIFORM_BUFFER
:
141 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
142 return &ctx
->UniformBuffer
;
145 case GL_SHADER_STORAGE_BUFFER
:
146 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
147 return &ctx
->ShaderStorageBuffer
;
150 case GL_ATOMIC_COUNTER_BUFFER
:
151 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
152 return &ctx
->AtomicBuffer
;
155 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
156 if (ctx
->Extensions
.AMD_pinned_memory
) {
157 return &ctx
->ExternalVirtualMemoryBuffer
;
168 * Get the buffer object bound to the specified target in a GL context.
169 * \param ctx the GL context
170 * \param target the buffer object target to be retrieved.
171 * \param error the GL error to record if target is illegal.
172 * \return pointer to the buffer object bound to \c target in the
173 * specified context or \c NULL if \c target is invalid.
175 static inline struct gl_buffer_object
*
176 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
179 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
182 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
186 if (!_mesa_is_bufferobj(*bufObj
)) {
187 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
196 * Convert a GLbitfield describing the mapped buffer access flags
197 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
200 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
202 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
203 if ((access
& rwFlags
) == rwFlags
)
204 return GL_READ_WRITE
;
205 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
207 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
208 return GL_WRITE_ONLY
;
210 /* Otherwise, AccessFlags is zero (the default state).
212 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
214 * Name Type Initial Value Legal Values
216 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
219 * However, table 6.8 in the GL_OES_mapbuffer extension says:
221 * Get Value Type Get Command Value Description
222 * --------- ---- ----------- ----- -----------
223 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
225 * The difference is because GL_OES_mapbuffer only supports mapping buffers
230 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
235 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
237 * The regions do not overlap if and only if the end of the given
238 * region is before the mapped region or the start of the given region
239 * is after the mapped region.
241 * \param obj Buffer object target on which to operate.
242 * \param offset Offset of the first byte of the subdata range.
243 * \param size Size, in bytes, of the subdata range.
244 * \return true if ranges overlap, false otherwise
248 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
249 GLintptr offset
, GLsizeiptr size
)
251 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
252 const GLintptr end
= offset
+ size
;
253 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
254 obj
->Mappings
[MAP_USER
].Length
;
256 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
265 * Tests the subdata range parameters and sets the GL error code for
266 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
267 * \c glClearBufferSubData.
269 * \param ctx GL context.
270 * \param bufObj The buffer object.
271 * \param offset Offset of the first byte of the subdata range.
272 * \param size Size, in bytes, of the subdata range.
273 * \param mappedRange If true, checks if an overlapping range is mapped.
274 * If false, checks if buffer is mapped.
275 * \param caller Name of calling function for recording errors.
276 * \return false if error, true otherwise
278 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
281 buffer_object_subdata_range_good(struct gl_context
*ctx
,
282 const struct gl_buffer_object
*bufObj
,
283 GLintptr offset
, GLsizeiptr size
,
284 bool mappedRange
, const char *caller
)
287 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
292 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
296 if (offset
+ size
> bufObj
->Size
) {
297 _mesa_error(ctx
, GL_INVALID_VALUE
,
298 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
299 (unsigned long) offset
,
300 (unsigned long) size
,
301 (unsigned long) bufObj
->Size
);
305 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
309 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
310 _mesa_error(ctx
, GL_INVALID_OPERATION
,
311 "%s(range is mapped without persistent bit)",
317 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
318 _mesa_error(ctx
, GL_INVALID_OPERATION
,
319 "%s(buffer is mapped without persistent bit)",
330 * Test the format and type parameters and set the GL error code for
331 * \c glClearBufferData and \c glClearBufferSubData.
333 * \param ctx GL context.
334 * \param internalformat Format to which the data is to be converted.
335 * \param format Format of the supplied data.
336 * \param type Type of the supplied data.
337 * \param caller Name of calling function for recording errors.
338 * \return If internalformat, format and type are legal the mesa_format
339 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
341 * \sa glClearBufferData and glClearBufferSubData
344 validate_clear_buffer_format(struct gl_context
*ctx
,
345 GLenum internalformat
,
346 GLenum format
, GLenum type
,
349 mesa_format mesaFormat
;
350 GLenum errorFormatType
;
352 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
353 if (mesaFormat
== MESA_FORMAT_NONE
) {
354 _mesa_error(ctx
, GL_INVALID_ENUM
,
355 "%s(invalid internalformat)", caller
);
356 return MESA_FORMAT_NONE
;
359 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
360 * EXT_texture_integer there is no conversion between integer and
361 * non-integer formats
363 if (_mesa_is_enum_format_signed_int(format
) !=
364 _mesa_is_format_integer_color(mesaFormat
)) {
365 _mesa_error(ctx
, GL_INVALID_OPERATION
,
366 "%s(integer vs non-integer)", caller
);
367 return MESA_FORMAT_NONE
;
370 if (!_mesa_is_color_format(format
)) {
371 _mesa_error(ctx
, GL_INVALID_ENUM
,
372 "%s(format is not a color format)", caller
);
373 return MESA_FORMAT_NONE
;
376 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
377 if (errorFormatType
!= GL_NO_ERROR
) {
378 _mesa_error(ctx
, GL_INVALID_ENUM
,
379 "%s(invalid format or type)", caller
);
380 return MESA_FORMAT_NONE
;
388 * Convert user-specified clear value to the specified internal format.
390 * \param ctx GL context.
391 * \param internalformat Format to which the data is converted.
392 * \param clearValue Points to the converted clear value.
393 * \param format Format of the supplied data.
394 * \param type Type of the supplied data.
395 * \param data Data which is to be converted to internalformat.
396 * \param caller Name of calling function for recording errors.
397 * \return true if data could be converted, false otherwise.
399 * \sa glClearBufferData, glClearBufferSubData
402 convert_clear_buffer_data(struct gl_context
*ctx
,
403 mesa_format internalformat
,
404 GLubyte
*clearValue
, GLenum format
, GLenum type
,
405 const GLvoid
*data
, const char *caller
)
407 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
409 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
410 0, &clearValue
, 1, 1, 1,
411 format
, type
, data
, &ctx
->Unpack
)) {
415 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
422 * Allocate and initialize a new buffer object.
424 * Default callback for the \c dd_function_table::NewBufferObject() hook.
426 static struct gl_buffer_object
*
427 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
429 struct gl_buffer_object
*obj
;
433 obj
= MALLOC_STRUCT(gl_buffer_object
);
434 _mesa_initialize_buffer_object(ctx
, obj
, name
);
440 * Delete a buffer object.
442 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
445 _mesa_delete_buffer_object(struct gl_context
*ctx
,
446 struct gl_buffer_object
*bufObj
)
450 _mesa_align_free(bufObj
->Data
);
452 /* assign strange values here to help w/ debugging */
453 bufObj
->RefCount
= -1000;
456 mtx_destroy(&bufObj
->Mutex
);
464 * Set ptr to bufObj w/ reference counting.
465 * This is normally only called from the _mesa_reference_buffer_object() macro
466 * when there's a real pointer change.
469 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
470 struct gl_buffer_object
**ptr
,
471 struct gl_buffer_object
*bufObj
)
474 /* Unreference the old buffer */
475 GLboolean deleteFlag
= GL_FALSE
;
476 struct gl_buffer_object
*oldObj
= *ptr
;
478 mtx_lock(&oldObj
->Mutex
);
479 assert(oldObj
->RefCount
> 0);
481 deleteFlag
= (oldObj
->RefCount
== 0);
482 mtx_unlock(&oldObj
->Mutex
);
485 assert(ctx
->Driver
.DeleteBuffer
);
486 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
494 /* reference new buffer */
495 mtx_lock(&bufObj
->Mutex
);
496 if (bufObj
->RefCount
== 0) {
497 /* this buffer's being deleted (look just above) */
498 /* Not sure this can every really happen. Warn if it does. */
499 _mesa_problem(NULL
, "referencing deleted buffer object");
506 mtx_unlock(&bufObj
->Mutex
);
512 * Initialize a buffer object to default values.
515 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
516 struct gl_buffer_object
*obj
,
519 memset(obj
, 0, sizeof(struct gl_buffer_object
));
520 mtx_init(&obj
->Mutex
, mtx_plain
);
523 obj
->Usage
= GL_STATIC_DRAW_ARB
;
529 * Callback called from _mesa_HashWalk()
532 count_buffer_size(GLuint key
, void *data
, void *userData
)
534 const struct gl_buffer_object
*bufObj
=
535 (const struct gl_buffer_object
*) data
;
536 GLuint
*total
= (GLuint
*) userData
;
539 *total
= *total
+ bufObj
->Size
;
544 * Compute total size (in bytes) of all buffer objects for the given context.
545 * For debugging purposes.
548 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
552 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
559 * Allocate space for and store data in a buffer object. Any data that was
560 * previously stored in the buffer object is lost. If \c data is \c NULL,
561 * memory will be allocated, but no copy will occur.
563 * This is the default callback for \c dd_function_table::BufferData()
564 * Note that all GL error checking will have been done already.
566 * \param ctx GL context.
567 * \param target Buffer object target on which to operate.
568 * \param size Size, in bytes, of the new data store.
569 * \param data Pointer to the data to store in the buffer object. This
570 * pointer may be \c NULL.
571 * \param usage Hints about how the data will be used.
572 * \param bufObj Object to be used.
574 * \return GL_TRUE for success, GL_FALSE for failure
575 * \sa glBufferDataARB, dd_function_table::BufferData.
578 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptr size
,
579 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
580 struct gl_buffer_object
*bufObj
)
586 _mesa_align_free( bufObj
->Data
);
588 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
590 bufObj
->Data
= (GLubyte
*) new_data
;
592 bufObj
->Usage
= usage
;
593 bufObj
->StorageFlags
= storageFlags
;
596 memcpy( bufObj
->Data
, data
, size
);
608 * Replace data in a subrange of buffer object. If the data range
609 * specified by \c size + \c offset extends beyond the end of the buffer or
610 * if \c data is \c NULL, no copy is performed.
612 * This is the default callback for \c dd_function_table::BufferSubData()
613 * Note that all GL error checking will have been done already.
615 * \param ctx GL context.
616 * \param offset Offset of the first byte to be modified.
617 * \param size Size, in bytes, of the data range.
618 * \param data Pointer to the data to store in the buffer object.
619 * \param bufObj Object to be used.
621 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
624 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptr offset
,
625 GLsizeiptr size
, const GLvoid
*data
,
626 struct gl_buffer_object
*bufObj
)
630 /* this should have been caught in _mesa_BufferSubData() */
631 assert(size
+ offset
<= bufObj
->Size
);
634 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
640 * Retrieve data from a subrange of buffer object. If the data range
641 * specified by \c size + \c offset extends beyond the end of the buffer or
642 * if \c data is \c NULL, no copy is performed.
644 * This is the default callback for \c dd_function_table::GetBufferSubData()
645 * Note that all GL error checking will have been done already.
647 * \param ctx GL context.
648 * \param target Buffer object target on which to operate.
649 * \param offset Offset of the first byte to be fetched.
650 * \param size Size, in bytes, of the data range.
651 * \param data Destination for data
652 * \param bufObj Object to be used.
654 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
657 _mesa_buffer_get_subdata( struct gl_context
*ctx
, GLintptrARB offset
,
658 GLsizeiptrARB size
, GLvoid
* data
,
659 struct gl_buffer_object
* bufObj
)
663 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
664 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
670 * Clear a subrange of the buffer object with copies of the supplied data.
671 * If data is NULL the buffer is filled with zeros.
673 * This is the default callback for \c dd_function_table::ClearBufferSubData()
674 * Note that all GL error checking will have been done already.
676 * \param ctx GL context.
677 * \param offset Offset of the first byte to be cleared.
678 * \param size Size, in bytes, of the to be cleared range.
679 * \param clearValue Source of the data.
680 * \param clearValueSize Size, in bytes, of the supplied data.
681 * \param bufObj Object to be cleared.
683 * \sa glClearBufferSubData, glClearBufferData and
684 * dd_function_table::ClearBufferSubData.
687 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
688 GLintptr offset
, GLsizeiptr size
,
689 const GLvoid
*clearValue
,
690 GLsizeiptr clearValueSize
,
691 struct gl_buffer_object
*bufObj
)
696 assert(ctx
->Driver
.MapBufferRange
);
697 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
699 GL_MAP_INVALIDATE_RANGE_BIT
,
700 bufObj
, MAP_INTERNAL
);
703 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
707 if (clearValue
== NULL
) {
708 /* Clear with zeros, per the spec */
709 memset(dest
, 0, size
);
710 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
714 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
715 memcpy(dest
, clearValue
, clearValueSize
);
716 dest
+= clearValueSize
;
719 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
724 * Default fallback for \c dd_function_table::MapBufferRange().
725 * Called via glMapBufferRange().
728 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
729 GLsizeiptr length
, GLbitfield access
,
730 struct gl_buffer_object
*bufObj
,
731 gl_map_buffer_index index
)
734 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
735 /* Just return a direct pointer to the data */
736 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
737 bufObj
->Mappings
[index
].Length
= length
;
738 bufObj
->Mappings
[index
].Offset
= offset
;
739 bufObj
->Mappings
[index
].AccessFlags
= access
;
740 return bufObj
->Mappings
[index
].Pointer
;
745 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
746 * Called via glFlushMappedBufferRange().
749 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
750 GLintptr offset
, GLsizeiptr length
,
751 struct gl_buffer_object
*obj
,
752 gl_map_buffer_index index
)
764 * Default callback for \c dd_function_table::UnmapBuffer().
766 * The input parameters will have been already tested for errors.
768 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
771 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
772 gl_map_buffer_index index
)
775 /* XXX we might assert here that bufObj->Pointer is non-null */
776 bufObj
->Mappings
[index
].Pointer
= NULL
;
777 bufObj
->Mappings
[index
].Length
= 0;
778 bufObj
->Mappings
[index
].Offset
= 0;
779 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
785 * Default fallback for \c dd_function_table::CopyBufferSubData().
786 * Called via glCopyBufferSubData().
789 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
790 struct gl_buffer_object
*src
,
791 struct gl_buffer_object
*dst
,
792 GLintptr readOffset
, GLintptr writeOffset
,
795 GLubyte
*srcPtr
, *dstPtr
;
798 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
800 GL_MAP_WRITE_BIT
, src
,
806 srcPtr
+= readOffset
;
807 dstPtr
+= writeOffset
;
809 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
810 GL_MAP_READ_BIT
, src
,
812 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
814 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
818 /* Note: the src and dst regions will never overlap. Trying to do so
819 * would generate GL_INVALID_VALUE earlier.
821 if (srcPtr
&& dstPtr
)
822 memcpy(dstPtr
, srcPtr
, size
);
824 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
826 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
832 * Initialize the state associated with buffer objects
835 _mesa_init_buffer_objects( struct gl_context
*ctx
)
839 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
840 mtx_init(&DummyBufferObject
.Mutex
, mtx_plain
);
841 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
843 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
,
844 ctx
->Shared
->NullBufferObj
);
846 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
,
847 ctx
->Shared
->NullBufferObj
);
848 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
,
849 ctx
->Shared
->NullBufferObj
);
851 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
,
852 ctx
->Shared
->NullBufferObj
);
854 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
,
855 ctx
->Shared
->NullBufferObj
);
857 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
,
858 ctx
->Shared
->NullBufferObj
);
860 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
,
861 ctx
->Shared
->NullBufferObj
);
863 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
,
864 ctx
->Shared
->NullBufferObj
);
866 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
867 _mesa_reference_buffer_object(ctx
,
868 &ctx
->UniformBufferBindings
[i
].BufferObject
,
869 ctx
->Shared
->NullBufferObj
);
870 ctx
->UniformBufferBindings
[i
].Offset
= -1;
871 ctx
->UniformBufferBindings
[i
].Size
= -1;
874 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
875 _mesa_reference_buffer_object(ctx
,
876 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
877 ctx
->Shared
->NullBufferObj
);
878 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
879 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
882 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
883 _mesa_reference_buffer_object(ctx
,
884 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
885 ctx
->Shared
->NullBufferObj
);
886 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
887 ctx
->AtomicBufferBindings
[i
].Size
= 0;
893 _mesa_free_buffer_objects( struct gl_context
*ctx
)
897 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
899 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
900 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
902 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
904 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
906 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
908 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
910 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, NULL
);
912 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
913 _mesa_reference_buffer_object(ctx
,
914 &ctx
->UniformBufferBindings
[i
].BufferObject
,
918 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
919 _mesa_reference_buffer_object(ctx
,
920 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
924 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
925 _mesa_reference_buffer_object(ctx
,
926 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
933 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
935 struct gl_buffer_object
**buf_handle
,
938 struct gl_buffer_object
*buf
= *buf_handle
;
940 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
|| _mesa_is_gles31(ctx
))) {
941 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
945 if (!buf
|| buf
== &DummyBufferObject
) {
946 /* If this is a new buffer object id, or one which was generated but
947 * never used before, allocate a buffer object now.
949 assert(ctx
->Driver
.NewBufferObject
);
950 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
952 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
955 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
963 * Bind the specified target to buffer for the specified context.
964 * Called by glBindBuffer() and other functions.
967 bind_buffer_object(struct gl_context
*ctx
, GLenum target
, GLuint buffer
)
969 struct gl_buffer_object
*oldBufObj
;
970 struct gl_buffer_object
*newBufObj
= NULL
;
971 struct gl_buffer_object
**bindTarget
= NULL
;
973 bindTarget
= get_buffer_target(ctx
, target
);
975 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target 0x%x)", target
);
979 /* Get pointer to old buffer object (to be unbound) */
980 oldBufObj
= *bindTarget
;
981 if (oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
)
982 return; /* rebinding the same buffer object- no change */
985 * Get pointer to new buffer object (newBufObj)
988 /* The spec says there's not a buffer object named 0, but we use
989 * one internally because it simplifies things.
991 newBufObj
= ctx
->Shared
->NullBufferObj
;
994 /* non-default buffer object */
995 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
996 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
997 &newBufObj
, "glBindBuffer"))
1001 /* bind new buffer */
1002 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1007 * Update the default buffer objects in the given context to reference those
1008 * specified in the shared state and release those referencing the old
1012 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1014 /* Bind the NullBufferObj to remove references to those
1015 * in the shared context hash table.
1017 bind_buffer_object( ctx
, GL_ARRAY_BUFFER_ARB
, 0);
1018 bind_buffer_object( ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, 0);
1019 bind_buffer_object( ctx
, GL_PIXEL_PACK_BUFFER_ARB
, 0);
1020 bind_buffer_object( ctx
, GL_PIXEL_UNPACK_BUFFER_ARB
, 0);
1026 * Return the gl_buffer_object for the given ID.
1027 * Always return NULL for ID 0.
1029 struct gl_buffer_object
*
1030 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1035 return (struct gl_buffer_object
*)
1036 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1040 struct gl_buffer_object
*
1041 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1043 return (struct gl_buffer_object
*)
1044 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1048 * A convenience function for direct state access functions that throws
1049 * GL_INVALID_OPERATION if buffer is not the name of an existing
1052 struct gl_buffer_object
*
1053 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1056 struct gl_buffer_object
*bufObj
;
1058 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1059 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1060 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1061 "%s(non-existent buffer object %u)", caller
, buffer
);
1070 _mesa_begin_bufferobj_lookups(struct gl_context
*ctx
)
1072 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1077 _mesa_end_bufferobj_lookups(struct gl_context
*ctx
)
1079 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1084 * Look up a buffer object for a multi-bind function.
1086 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1087 * of generating an error if the buffer ID is not zero or the name
1088 * of an existing buffer object.
1090 * If the buffer ID refers to an existing buffer object, a pointer
1091 * to the buffer object is returned. If the ID is zero, a pointer
1092 * to the shared NullBufferObj is returned. If the ID is not zero
1093 * and does not refer to a valid buffer object, this function
1096 * This function assumes that the caller has already locked the
1097 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1099 struct gl_buffer_object
*
1100 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1101 const GLuint
*buffers
,
1102 GLuint index
, const char *caller
)
1104 struct gl_buffer_object
*bufObj
;
1106 if (buffers
[index
] != 0) {
1107 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1109 /* The multi-bind functions don't create the buffer objects
1110 when they don't exist. */
1111 if (bufObj
== &DummyBufferObject
)
1114 bufObj
= ctx
->Shared
->NullBufferObj
;
1117 /* The ARB_multi_bind spec says:
1119 * "An INVALID_OPERATION error is generated if any value
1120 * in <buffers> is not zero or the name of an existing
1121 * buffer object (per binding)."
1123 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1124 "%s(buffers[%u]=%u is not zero or the name "
1125 "of an existing buffer object)",
1126 caller
, index
, buffers
[index
]);
1134 * If *ptr points to obj, set ptr = the Null/default buffer object.
1135 * This is a helper for buffer object deletion.
1136 * The GL spec says that deleting a buffer object causes it to get
1137 * unbound from all arrays in the current context.
1140 unbind(struct gl_context
*ctx
,
1141 struct gl_buffer_object
**ptr
,
1142 struct gl_buffer_object
*obj
)
1145 _mesa_reference_buffer_object(ctx
, ptr
, ctx
->Shared
->NullBufferObj
);
1151 * Plug default/fallback buffer object functions into the device
1155 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1157 /* GL_ARB_vertex/pixel_buffer_object */
1158 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1159 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1160 driver
->BufferData
= buffer_data_fallback
;
1161 driver
->BufferSubData
= buffer_sub_data_fallback
;
1162 driver
->GetBufferSubData
= _mesa_buffer_get_subdata
;
1163 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1165 /* GL_ARB_clear_buffer_object */
1166 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1168 /* GL_ARB_map_buffer_range */
1169 driver
->MapBufferRange
= map_buffer_range_fallback
;
1170 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1172 /* GL_ARB_copy_buffer */
1173 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1178 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1179 struct gl_buffer_object
*bufObj
)
1183 for (i
= 0; i
< MAP_COUNT
; i
++) {
1184 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1185 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1186 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1187 bufObj
->Mappings
[i
].AccessFlags
= 0;
1193 /**********************************************************************/
1195 /**********************************************************************/
1198 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1200 GET_CURRENT_CONTEXT(ctx
);
1202 if (MESA_VERBOSE
& VERBOSE_API
)
1203 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1204 _mesa_enum_to_string(target
), buffer
);
1206 bind_buffer_object(ctx
, target
, buffer
);
1211 * Delete a set of buffer objects.
1213 * \param n Number of buffer objects to delete.
1214 * \param ids Array of \c n buffer object IDs.
1217 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1219 GET_CURRENT_CONTEXT(ctx
);
1221 FLUSH_VERTICES(ctx
, 0);
1224 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1228 mtx_lock(&ctx
->Shared
->Mutex
);
1230 for (i
= 0; i
< n
; i
++) {
1231 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, ids
[i
]);
1233 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1236 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1238 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1240 /* unbind any vertex pointers bound to this buffer */
1241 for (j
= 0; j
< ARRAY_SIZE(vao
->VertexBinding
); j
++) {
1242 unbind(ctx
, &vao
->VertexBinding
[j
].BufferObj
, bufObj
);
1245 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1246 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB
, 0 );
1248 if (vao
->IndexBufferObj
== bufObj
) {
1249 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB
, 0 );
1252 /* unbind ARB_draw_indirect binding point */
1253 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1254 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER
, 0 );
1257 /* unbind ARB_compute_shader binding point */
1258 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1259 _mesa_BindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, 0);
1262 /* unbind ARB_copy_buffer binding points */
1263 if (ctx
->CopyReadBuffer
== bufObj
) {
1264 _mesa_BindBuffer( GL_COPY_READ_BUFFER
, 0 );
1266 if (ctx
->CopyWriteBuffer
== bufObj
) {
1267 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER
, 0 );
1270 /* unbind transform feedback binding points */
1271 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1272 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER
, 0 );
1274 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1275 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1276 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER
, j
, 0 );
1280 /* unbind UBO binding points */
1281 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1282 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1283 _mesa_BindBufferBase( GL_UNIFORM_BUFFER
, j
, 0 );
1287 if (ctx
->UniformBuffer
== bufObj
) {
1288 _mesa_BindBuffer( GL_UNIFORM_BUFFER
, 0 );
1291 /* unbind SSBO binding points */
1292 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1293 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1294 _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER
, j
, 0);
1298 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1299 _mesa_BindBuffer(GL_SHADER_STORAGE_BUFFER
, 0);
1302 /* unbind Atomci Buffer binding points */
1303 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1304 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1305 _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER
, j
, 0 );
1309 if (ctx
->AtomicBuffer
== bufObj
) {
1310 _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER
, 0 );
1313 /* unbind any pixel pack/unpack pointers bound to this buffer */
1314 if (ctx
->Pack
.BufferObj
== bufObj
) {
1315 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT
, 0 );
1317 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1318 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT
, 0 );
1321 if (ctx
->Texture
.BufferObject
== bufObj
) {
1322 _mesa_BindBuffer( GL_TEXTURE_BUFFER
, 0 );
1325 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1326 _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
, 0);
1329 /* The ID is immediately freed for re-use */
1330 _mesa_HashRemove(ctx
->Shared
->BufferObjects
, ids
[i
]);
1331 /* Make sure we do not run into the classic ABA problem on bind.
1332 * We don't want to allow re-binding a buffer object that's been
1333 * "deleted" by glDeleteBuffers().
1335 * The explicit rebinding to the default object in the current context
1336 * prevents the above in the current context, but another context
1337 * sharing the same objects might suffer from this problem.
1338 * The alternative would be to do the hash lookup in any case on bind
1339 * which would introduce more runtime overhead than this.
1341 bufObj
->DeletePending
= GL_TRUE
;
1342 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1346 mtx_unlock(&ctx
->Shared
->Mutex
);
1351 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1352 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1356 create_buffers(GLsizei n
, GLuint
*buffers
, bool dsa
)
1358 GET_CURRENT_CONTEXT(ctx
);
1361 struct gl_buffer_object
*buf
;
1363 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1365 if (MESA_VERBOSE
& VERBOSE_API
)
1366 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1369 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1378 * This must be atomic (generation and allocation of buffer object IDs)
1380 mtx_lock(&ctx
->Shared
->Mutex
);
1382 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1384 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1385 * DummyBufferObject. Otherwise, create a new buffer object and insert
1388 for (i
= 0; i
< n
; i
++) {
1389 buffers
[i
] = first
+ i
;
1391 assert(ctx
->Driver
.NewBufferObject
);
1392 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1394 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1395 mtx_unlock(&ctx
->Shared
->Mutex
);
1400 buf
= &DummyBufferObject
;
1402 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1405 mtx_unlock(&ctx
->Shared
->Mutex
);
1409 * Generate a set of unique buffer object IDs and store them in \c buffers.
1411 * \param n Number of IDs to generate.
1412 * \param buffers Array of \c n locations to store the IDs.
1415 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1417 create_buffers(n
, buffers
, false);
1421 * Create a set of buffer objects and store their unique IDs in \c buffers.
1423 * \param n Number of IDs to generate.
1424 * \param buffers Array of \c n locations to store the IDs.
1427 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1429 create_buffers(n
, buffers
, true);
1434 * Determine if ID is the name of a buffer object.
1436 * \param id ID of the potential buffer object.
1437 * \return \c GL_TRUE if \c id is the name of a buffer object,
1438 * \c GL_FALSE otherwise.
1440 GLboolean GLAPIENTRY
1441 _mesa_IsBuffer(GLuint id
)
1443 struct gl_buffer_object
*bufObj
;
1444 GET_CURRENT_CONTEXT(ctx
);
1445 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1447 mtx_lock(&ctx
->Shared
->Mutex
);
1448 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1449 mtx_unlock(&ctx
->Shared
->Mutex
);
1451 return bufObj
&& bufObj
!= &DummyBufferObject
;
1456 _mesa_buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1457 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1458 GLbitfield flags
, const char *func
)
1461 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1465 if (flags
& ~(GL_MAP_READ_BIT
|
1467 GL_MAP_PERSISTENT_BIT
|
1468 GL_MAP_COHERENT_BIT
|
1469 GL_DYNAMIC_STORAGE_BIT
|
1470 GL_CLIENT_STORAGE_BIT
)) {
1471 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1475 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1476 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1477 _mesa_error(ctx
, GL_INVALID_VALUE
,
1478 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1482 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1483 _mesa_error(ctx
, GL_INVALID_VALUE
,
1484 "%s(COHERENT and flags!=PERSISTENT)", func
);
1488 if (bufObj
->Immutable
) {
1489 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1493 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1494 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1496 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1498 bufObj
->Written
= GL_TRUE
;
1499 bufObj
->Immutable
= GL_TRUE
;
1501 assert(ctx
->Driver
.BufferData
);
1502 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1504 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1505 /* Even though the interaction between AMD_pinned_memory and
1506 * glBufferStorage is not described in the spec, Graham Sellers
1507 * said that it should behave the same as glBufferData.
1509 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1512 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1518 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1521 GET_CURRENT_CONTEXT(ctx
);
1522 struct gl_buffer_object
*bufObj
;
1524 bufObj
= get_buffer(ctx
, "glBufferStorage", target
, GL_INVALID_OPERATION
);
1528 _mesa_buffer_storage(ctx
, bufObj
, target
, size
, data
, flags
,
1533 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1536 GET_CURRENT_CONTEXT(ctx
);
1537 struct gl_buffer_object
*bufObj
;
1539 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferStorage");
1544 * In direct state access, buffer objects have an unspecified target since
1545 * they are not required to be bound.
1547 _mesa_buffer_storage(ctx
, bufObj
, GL_NONE
, size
, data
, flags
,
1548 "glNamedBufferStorage");
1553 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1554 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1555 GLenum usage
, const char *func
)
1559 if (MESA_VERBOSE
& VERBOSE_API
)
1560 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
1562 _mesa_enum_to_string(target
),
1563 (long int) size
, data
,
1564 _mesa_enum_to_string(usage
));
1567 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
1572 case GL_STREAM_DRAW_ARB
:
1573 valid_usage
= (ctx
->API
!= API_OPENGLES
);
1576 case GL_STATIC_DRAW_ARB
:
1577 case GL_DYNAMIC_DRAW_ARB
:
1581 case GL_STREAM_READ_ARB
:
1582 case GL_STREAM_COPY_ARB
:
1583 case GL_STATIC_READ_ARB
:
1584 case GL_STATIC_COPY_ARB
:
1585 case GL_DYNAMIC_READ_ARB
:
1586 case GL_DYNAMIC_COPY_ARB
:
1587 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
1591 valid_usage
= false;
1596 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
1597 _mesa_enum_to_string(usage
));
1601 if (bufObj
->Immutable
) {
1602 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1606 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1607 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1609 FLUSH_VERTICES(ctx
, _NEW_BUFFER_OBJECT
);
1611 bufObj
->Written
= GL_TRUE
;
1614 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1615 bufObj
->Name
, size
, data
, usage
);
1622 assert(ctx
->Driver
.BufferData
);
1623 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
1626 GL_DYNAMIC_STORAGE_BIT
,
1628 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1629 /* From GL_AMD_pinned_memory:
1631 * INVALID_OPERATION is generated by BufferData if <target> is
1632 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
1633 * mapped to the GPU address space.
1635 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1638 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1644 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
1645 const GLvoid
*data
, GLenum usage
)
1647 GET_CURRENT_CONTEXT(ctx
);
1648 struct gl_buffer_object
*bufObj
;
1650 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
1654 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
1659 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1662 GET_CURRENT_CONTEXT(ctx
);
1663 struct gl_buffer_object
*bufObj
;
1665 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
1669 /* In direct state access, buffer objects have an unspecified target since
1670 * they are not required to be bound.
1672 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
1673 "glNamedBufferData");
1678 * Implementation for glBufferSubData and glNamedBufferSubData.
1680 * \param ctx GL context.
1681 * \param bufObj The buffer object.
1682 * \param offset Offset of the first byte of the subdata range.
1683 * \param size Size, in bytes, of the subdata range.
1684 * \param data The data store.
1685 * \param func Name of calling function for recording errors.
1689 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1690 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
,
1693 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1695 /* error already recorded */
1699 if (bufObj
->Immutable
&&
1700 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
1701 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1708 bufObj
->NumSubDataCalls
++;
1710 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
1711 bufObj
->Usage
== GL_STATIC_COPY
) &&
1712 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
) {
1713 /* If the application declared the buffer as static draw/copy or stream
1714 * draw, it should not be frequently modified with glBufferSubData.
1716 buffer_usage_warning(ctx
,
1717 "using %s(buffer %u, offset %u, size %u) to "
1718 "update a %s buffer",
1719 func
, bufObj
->Name
, offset
, size
,
1720 _mesa_enum_to_string(bufObj
->Usage
));
1723 bufObj
->Written
= GL_TRUE
;
1725 assert(ctx
->Driver
.BufferSubData
);
1726 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
1730 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
1731 GLsizeiptr size
, const GLvoid
*data
)
1733 GET_CURRENT_CONTEXT(ctx
);
1734 struct gl_buffer_object
*bufObj
;
1736 bufObj
= get_buffer(ctx
, "glBufferSubData", target
, GL_INVALID_OPERATION
);
1740 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
, "glBufferSubData");
1744 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
1745 GLsizeiptr size
, const GLvoid
*data
)
1747 GET_CURRENT_CONTEXT(ctx
);
1748 struct gl_buffer_object
*bufObj
;
1750 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferSubData");
1754 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
,
1755 "glNamedBufferSubData");
1760 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
1761 GLsizeiptr size
, GLvoid
*data
)
1763 GET_CURRENT_CONTEXT(ctx
);
1764 struct gl_buffer_object
*bufObj
;
1766 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
1767 GL_INVALID_OPERATION
);
1771 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1772 "glGetBufferSubData")) {
1776 assert(ctx
->Driver
.GetBufferSubData
);
1777 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1781 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
1782 GLsizeiptr size
, GLvoid
*data
)
1784 GET_CURRENT_CONTEXT(ctx
);
1785 struct gl_buffer_object
*bufObj
;
1787 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1788 "glGetNamedBufferSubData");
1792 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
1793 "glGetNamedBufferSubData")) {
1797 assert(ctx
->Driver
.GetBufferSubData
);
1798 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
1803 * \param subdata true if caller is *SubData, false if *Data
1806 _mesa_clear_buffer_sub_data(struct gl_context
*ctx
,
1807 struct gl_buffer_object
*bufObj
,
1808 GLenum internalformat
,
1809 GLintptr offset
, GLsizeiptr size
,
1810 GLenum format
, GLenum type
,
1812 const char *func
, bool subdata
)
1814 mesa_format mesaFormat
;
1815 GLubyte clearValue
[MAX_PIXEL_BYTES
];
1816 GLsizeiptr clearValueSize
;
1818 /* This checks for disallowed mappings. */
1819 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
1824 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
1825 format
, type
, func
);
1827 if (mesaFormat
== MESA_FORMAT_NONE
) {
1831 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
1832 if (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0) {
1833 _mesa_error(ctx
, GL_INVALID_VALUE
,
1834 "%s(offset or size is not a multiple of "
1835 "internalformat size)", func
);
1840 /* clear to zeros, per the spec */
1842 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1843 NULL
, clearValueSize
, bufObj
);
1848 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
1849 format
, type
, data
, func
)) {
1854 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
1855 clearValue
, clearValueSize
, bufObj
);
1860 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
1861 GLenum type
, const GLvoid
*data
)
1863 GET_CURRENT_CONTEXT(ctx
);
1864 struct gl_buffer_object
*bufObj
;
1866 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
1870 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1872 "glClearBufferData", false);
1876 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
1877 GLenum format
, GLenum type
, const GLvoid
*data
)
1879 GET_CURRENT_CONTEXT(ctx
);
1880 struct gl_buffer_object
*bufObj
;
1882 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
1886 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
1888 "glClearNamedBufferData", false);
1893 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
1894 GLintptr offset
, GLsizeiptr size
,
1895 GLenum format
, GLenum type
,
1898 GET_CURRENT_CONTEXT(ctx
);
1899 struct gl_buffer_object
*bufObj
;
1901 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
1905 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1907 "glClearBufferSubData", true);
1911 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
1912 GLintptr offset
, GLsizeiptr size
,
1913 GLenum format
, GLenum type
,
1916 GET_CURRENT_CONTEXT(ctx
);
1917 struct gl_buffer_object
*bufObj
;
1919 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
1920 "glClearNamedBufferSubData");
1924 _mesa_clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
,
1926 "glClearNamedBufferSubData", true);
1931 _mesa_unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1934 GLboolean status
= GL_TRUE
;
1935 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1937 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
1938 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1939 "%s(buffer is not mapped)", func
);
1944 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
1945 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
1947 /* check that last 100 bytes are still = magic value */
1948 for (i
= 0; i
< 100; i
++) {
1949 GLuint pos
= bufObj
->Size
- i
- 1;
1950 if (buf
[pos
] != 123) {
1951 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
1952 " at position %d (value = %u)\n",
1960 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
1961 GLuint i
, unchanged
= 0;
1962 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
1964 /* check which bytes changed */
1965 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
1966 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
1973 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1974 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
1979 status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
1980 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
1981 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
1982 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
1983 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
1988 GLboolean GLAPIENTRY
1989 _mesa_UnmapBuffer(GLenum target
)
1991 GET_CURRENT_CONTEXT(ctx
);
1992 struct gl_buffer_object
*bufObj
;
1994 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
1998 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2001 GLboolean GLAPIENTRY
2002 _mesa_UnmapNamedBuffer(GLuint buffer
)
2004 GET_CURRENT_CONTEXT(ctx
);
2005 struct gl_buffer_object
*bufObj
;
2007 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2011 return _mesa_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2016 get_buffer_parameter(struct gl_context
*ctx
,
2017 struct gl_buffer_object
*bufObj
, GLenum pname
,
2018 GLint64
*params
, const char *func
)
2021 case GL_BUFFER_SIZE_ARB
:
2022 *params
= bufObj
->Size
;
2024 case GL_BUFFER_USAGE_ARB
:
2025 *params
= bufObj
->Usage
;
2027 case GL_BUFFER_ACCESS_ARB
:
2028 *params
= simplified_access_mode(ctx
,
2029 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2031 case GL_BUFFER_MAPPED_ARB
:
2032 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2034 case GL_BUFFER_ACCESS_FLAGS
:
2035 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2037 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2039 case GL_BUFFER_MAP_OFFSET
:
2040 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2042 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2044 case GL_BUFFER_MAP_LENGTH
:
2045 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2047 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2049 case GL_BUFFER_IMMUTABLE_STORAGE
:
2050 if (!ctx
->Extensions
.ARB_buffer_storage
)
2052 *params
= bufObj
->Immutable
;
2054 case GL_BUFFER_STORAGE_FLAGS
:
2055 if (!ctx
->Extensions
.ARB_buffer_storage
)
2057 *params
= bufObj
->StorageFlags
;
2066 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2067 _mesa_enum_to_string(pname
));
2072 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2074 GET_CURRENT_CONTEXT(ctx
);
2075 struct gl_buffer_object
*bufObj
;
2078 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2079 GL_INVALID_OPERATION
);
2083 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2084 "glGetBufferParameteriv"))
2085 return; /* Error already recorded. */
2087 *params
= (GLint
) parameter
;
2091 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2093 GET_CURRENT_CONTEXT(ctx
);
2094 struct gl_buffer_object
*bufObj
;
2097 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2098 GL_INVALID_OPERATION
);
2102 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2103 "glGetBufferParameteri64v"))
2104 return; /* Error already recorded. */
2106 *params
= parameter
;
2110 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2112 GET_CURRENT_CONTEXT(ctx
);
2113 struct gl_buffer_object
*bufObj
;
2116 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2117 "glGetNamedBufferParameteriv");
2121 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2122 "glGetNamedBufferParameteriv"))
2123 return; /* Error already recorded. */
2125 *params
= (GLint
) parameter
;
2129 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2132 GET_CURRENT_CONTEXT(ctx
);
2133 struct gl_buffer_object
*bufObj
;
2136 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2137 "glGetNamedBufferParameteri64v");
2141 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2142 "glGetNamedBufferParameteri64v"))
2143 return; /* Error already recorded. */
2145 *params
= parameter
;
2150 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2152 GET_CURRENT_CONTEXT(ctx
);
2153 struct gl_buffer_object
*bufObj
;
2155 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2156 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2157 "GL_BUFFER_MAP_POINTER)");
2161 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2162 GL_INVALID_OPERATION
);
2166 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2170 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2172 GET_CURRENT_CONTEXT(ctx
);
2173 struct gl_buffer_object
*bufObj
;
2175 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2176 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2177 "GL_BUFFER_MAP_POINTER)");
2181 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2182 "glGetNamedBufferPointerv");
2186 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2191 _mesa_copy_buffer_sub_data(struct gl_context
*ctx
,
2192 struct gl_buffer_object
*src
,
2193 struct gl_buffer_object
*dst
,
2194 GLintptr readOffset
, GLintptr writeOffset
,
2195 GLsizeiptr size
, const char *func
)
2197 if (_mesa_check_disallowed_mapping(src
)) {
2198 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2199 "%s(readBuffer is mapped)", func
);
2203 if (_mesa_check_disallowed_mapping(dst
)) {
2204 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2205 "%s(writeBuffer is mapped)", func
);
2209 if (readOffset
< 0) {
2210 _mesa_error(ctx
, GL_INVALID_VALUE
,
2211 "%s(readOffset %d < 0)", func
, (int) readOffset
);
2215 if (writeOffset
< 0) {
2216 _mesa_error(ctx
, GL_INVALID_VALUE
,
2217 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
2222 _mesa_error(ctx
, GL_INVALID_VALUE
,
2223 "%s(size %d < 0)", func
, (int) size
);
2227 if (readOffset
+ size
> src
->Size
) {
2228 _mesa_error(ctx
, GL_INVALID_VALUE
,
2229 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
2230 (int) readOffset
, (int) size
, (int) src
->Size
);
2234 if (writeOffset
+ size
> dst
->Size
) {
2235 _mesa_error(ctx
, GL_INVALID_VALUE
,
2236 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
2237 (int) writeOffset
, (int) size
, (int) dst
->Size
);
2242 if (readOffset
+ size
<= writeOffset
) {
2245 else if (writeOffset
+ size
<= readOffset
) {
2249 /* overlapping src/dst is illegal */
2250 _mesa_error(ctx
, GL_INVALID_VALUE
,
2251 "%s(overlapping src/dst)", func
);
2256 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
2260 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
2261 GLintptr readOffset
, GLintptr writeOffset
,
2264 GET_CURRENT_CONTEXT(ctx
);
2265 struct gl_buffer_object
*src
, *dst
;
2267 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
2268 GL_INVALID_OPERATION
);
2272 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
2273 GL_INVALID_OPERATION
);
2277 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2278 "glCopyBufferSubData");
2282 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
2283 GLintptr readOffset
, GLintptr writeOffset
,
2286 GET_CURRENT_CONTEXT(ctx
);
2287 struct gl_buffer_object
*src
, *dst
;
2289 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
2290 "glCopyNamedBufferSubData");
2294 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
2295 "glCopyNamedBufferSubData");
2299 _mesa_copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
2300 "glCopyNamedBufferSubData");
2305 _mesa_map_buffer_range(struct gl_context
*ctx
,
2306 struct gl_buffer_object
*bufObj
,
2307 GLintptr offset
, GLsizeiptr length
,
2308 GLbitfield access
, const char *func
)
2311 GLbitfield allowed_access
;
2313 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, NULL
);
2316 _mesa_error(ctx
, GL_INVALID_VALUE
,
2317 "%s(offset %ld < 0)", func
, (long) offset
);
2322 _mesa_error(ctx
, GL_INVALID_VALUE
,
2323 "%s(length %ld < 0)", func
, (long) length
);
2327 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2329 * "An INVALID_OPERATION error is generated for any of the following
2332 * * <length> is zero."
2334 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
2335 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
2339 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
2343 allowed_access
= GL_MAP_READ_BIT
|
2345 GL_MAP_INVALIDATE_RANGE_BIT
|
2346 GL_MAP_INVALIDATE_BUFFER_BIT
|
2347 GL_MAP_FLUSH_EXPLICIT_BIT
|
2348 GL_MAP_UNSYNCHRONIZED_BIT
;
2350 if (ctx
->Extensions
.ARB_buffer_storage
) {
2351 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
2352 GL_MAP_COHERENT_BIT
;
2355 if (access
& ~allowed_access
) {
2356 /* generate an error if any bits other than those allowed are set */
2357 _mesa_error(ctx
, GL_INVALID_VALUE
,
2358 "%s(access has undefined bits set)", func
);
2362 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
2363 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2364 "%s(access indicates neither read or write)", func
);
2368 if ((access
& GL_MAP_READ_BIT
) &&
2369 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
2370 GL_MAP_INVALIDATE_BUFFER_BIT
|
2371 GL_MAP_UNSYNCHRONIZED_BIT
))) {
2372 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2373 "%s(read access with disallowed bits)", func
);
2377 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
2378 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
2379 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2380 "%s(access has flush explicit without write)", func
);
2384 if (access
& GL_MAP_READ_BIT
&&
2385 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
2386 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2387 "%s(buffer does not allow read access)", func
);
2391 if (access
& GL_MAP_WRITE_BIT
&&
2392 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
2393 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2394 "%s(buffer does not allow write access)", func
);
2398 if (access
& GL_MAP_COHERENT_BIT
&&
2399 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
2400 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2401 "%s(buffer does not allow coherent access)", func
);
2405 if (access
& GL_MAP_PERSISTENT_BIT
&&
2406 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
2407 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2408 "%s(buffer does not allow persistent access)", func
);
2412 if (offset
+ length
> bufObj
->Size
) {
2413 _mesa_error(ctx
, GL_INVALID_VALUE
,
2414 "%s(offset %td + length %td > buffer_size %td)", func
,
2415 offset
, length
, bufObj
->Size
);
2419 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2420 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2421 "%s(buffer already mapped)", func
);
2425 if (!bufObj
->Size
) {
2426 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
2430 if (access
& GL_MAP_WRITE_BIT
) {
2431 bufObj
->NumMapBufferWriteCalls
++;
2432 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2433 bufObj
->Usage
== GL_STATIC_COPY
) &&
2434 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
2435 buffer_usage_warning(ctx
,
2436 "using %s(buffer %u, offset %u, length %u) to "
2437 "update a %s buffer",
2438 func
, bufObj
->Name
, offset
, length
,
2439 _mesa_enum_to_string(bufObj
->Usage
));
2443 assert(ctx
->Driver
.MapBufferRange
);
2444 map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
2447 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
2450 /* The driver callback should have set all these fields.
2451 * This is important because other modules (like VBO) might call
2452 * the driver function directly.
2454 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
2455 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
2456 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
2457 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
2460 if (access
& GL_MAP_WRITE_BIT
)
2461 bufObj
->Written
= GL_TRUE
;
2464 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2465 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
2466 bufObj
->Name
, bufObj
->Size
, access
);
2467 /* Access must be write only */
2468 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
2470 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2471 for (i
= 0; i
< bufObj
->Size
; i
++)
2478 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
2479 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2481 /* buffer is 100 bytes larger than requested, fill with magic value */
2482 for (i
= 0; i
< 100; i
++) {
2483 buf
[bufObj
->Size
- i
- 1] = 123;
2492 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
2495 GET_CURRENT_CONTEXT(ctx
);
2496 struct gl_buffer_object
*bufObj
;
2498 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2499 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2500 "glMapBufferRange(ARB_map_buffer_range not supported)");
2504 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
2508 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2509 "glMapBufferRange");
2513 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
2516 GET_CURRENT_CONTEXT(ctx
);
2517 struct gl_buffer_object
*bufObj
;
2519 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2520 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2521 "glMapNamedBufferRange("
2522 "ARB_map_buffer_range not supported)");
2526 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBufferRange");
2530 return _mesa_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
2531 "glMapNamedBufferRange");
2535 * Converts GLenum access from MapBuffer and MapNamedBuffer into
2536 * flags for input to _mesa_map_buffer_range.
2538 * \return true if the type of requested access is permissible.
2541 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
2545 case GL_READ_ONLY_ARB
:
2546 *flags
= GL_MAP_READ_BIT
;
2547 return _mesa_is_desktop_gl(ctx
);
2548 case GL_WRITE_ONLY_ARB
:
2549 *flags
= GL_MAP_WRITE_BIT
;
2551 case GL_READ_WRITE_ARB
:
2552 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
2553 return _mesa_is_desktop_gl(ctx
);
2560 _mesa_MapBuffer(GLenum target
, GLenum access
)
2562 GET_CURRENT_CONTEXT(ctx
);
2563 struct gl_buffer_object
*bufObj
;
2564 GLbitfield accessFlags
;
2566 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2567 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
2571 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
2575 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2580 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
2582 GET_CURRENT_CONTEXT(ctx
);
2583 struct gl_buffer_object
*bufObj
;
2584 GLbitfield accessFlags
;
2586 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
2587 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
2591 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
2595 return _mesa_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
2596 "glMapNamedBuffer");
2601 _mesa_flush_mapped_buffer_range(struct gl_context
*ctx
,
2602 struct gl_buffer_object
*bufObj
,
2603 GLintptr offset
, GLsizeiptr length
,
2606 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
2607 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2608 "%s(ARB_map_buffer_range not supported)", func
);
2613 _mesa_error(ctx
, GL_INVALID_VALUE
,
2614 "%s(offset %ld < 0)", func
, (long) offset
);
2619 _mesa_error(ctx
, GL_INVALID_VALUE
,
2620 "%s(length %ld < 0)", func
, (long) length
);
2624 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2625 /* buffer is not mapped */
2626 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2627 "%s(buffer is not mapped)", func
);
2631 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
2632 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
2633 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2634 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
2638 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
2639 _mesa_error(ctx
, GL_INVALID_VALUE
,
2640 "%s(offset %ld + length %ld > mapped length %ld)", func
,
2641 (long) offset
, (long) length
,
2642 (long) bufObj
->Mappings
[MAP_USER
].Length
);
2646 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
2648 if (ctx
->Driver
.FlushMappedBufferRange
)
2649 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
2654 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
2657 GET_CURRENT_CONTEXT(ctx
);
2658 struct gl_buffer_object
*bufObj
;
2660 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
2661 GL_INVALID_OPERATION
);
2665 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2666 "glFlushMappedBufferRange");
2670 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
2673 GET_CURRENT_CONTEXT(ctx
);
2674 struct gl_buffer_object
*bufObj
;
2676 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2677 "glFlushMappedNamedBufferRange");
2681 _mesa_flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
2682 "glFlushMappedNamedBufferRange");
2687 * Binds a buffer object to a uniform buffer binding point.
2689 * The caller is responsible for flushing vertices and updating
2693 set_ubo_binding(struct gl_context
*ctx
,
2694 struct gl_uniform_buffer_binding
*binding
,
2695 struct gl_buffer_object
*bufObj
,
2700 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2702 binding
->Offset
= offset
;
2703 binding
->Size
= size
;
2704 binding
->AutomaticSize
= autoSize
;
2706 /* If this is a real buffer object, mark it has having been used
2707 * at some point as a UBO.
2710 bufObj
->UsageHistory
|= USAGE_UNIFORM_BUFFER
;
2714 * Binds a buffer object to a shader storage buffer binding point.
2716 * The caller is responsible for flushing vertices and updating
2720 set_ssbo_binding(struct gl_context
*ctx
,
2721 struct gl_shader_storage_buffer_binding
*binding
,
2722 struct gl_buffer_object
*bufObj
,
2727 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2729 binding
->Offset
= offset
;
2730 binding
->Size
= size
;
2731 binding
->AutomaticSize
= autoSize
;
2733 /* If this is a real buffer object, mark it has having been used
2734 * at some point as a SSBO.
2737 bufObj
->UsageHistory
|= USAGE_SHADER_STORAGE_BUFFER
;
2741 * Binds a buffer object to a uniform buffer binding point.
2743 * Unlike set_ubo_binding(), this function also flushes vertices
2744 * and updates NewDriverState. It also checks if the binding
2745 * has actually changed before updating it.
2748 bind_uniform_buffer(struct gl_context
*ctx
,
2750 struct gl_buffer_object
*bufObj
,
2755 struct gl_uniform_buffer_binding
*binding
=
2756 &ctx
->UniformBufferBindings
[index
];
2758 if (binding
->BufferObject
== bufObj
&&
2759 binding
->Offset
== offset
&&
2760 binding
->Size
== size
&&
2761 binding
->AutomaticSize
== autoSize
) {
2765 FLUSH_VERTICES(ctx
, 0);
2766 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
2768 set_ubo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2772 * Binds a buffer object to a shader storage buffer binding point.
2774 * Unlike set_ssbo_binding(), this function also flushes vertices
2775 * and updates NewDriverState. It also checks if the binding
2776 * has actually changed before updating it.
2779 bind_shader_storage_buffer(struct gl_context
*ctx
,
2781 struct gl_buffer_object
*bufObj
,
2786 struct gl_shader_storage_buffer_binding
*binding
=
2787 &ctx
->ShaderStorageBufferBindings
[index
];
2789 if (binding
->BufferObject
== bufObj
&&
2790 binding
->Offset
== offset
&&
2791 binding
->Size
== size
&&
2792 binding
->AutomaticSize
== autoSize
) {
2796 FLUSH_VERTICES(ctx
, 0);
2797 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
2799 set_ssbo_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
);
2803 * Bind a region of a buffer object to a uniform block binding point.
2804 * \param index the uniform buffer binding point index
2805 * \param bufObj the buffer object
2806 * \param offset offset to the start of buffer object region
2807 * \param size size of the buffer object region
2810 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
,
2812 struct gl_buffer_object
*bufObj
,
2816 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2817 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2821 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
2822 _mesa_error(ctx
, GL_INVALID_VALUE
,
2823 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2824 ctx
->Const
.UniformBufferOffsetAlignment
);
2828 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2833 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2834 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2838 * Bind a region of a buffer object to a shader storage block binding point.
2839 * \param index the shader storage buffer binding point index
2840 * \param bufObj the buffer object
2841 * \param offset offset to the start of buffer object region
2842 * \param size size of the buffer object region
2845 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
2847 struct gl_buffer_object
*bufObj
,
2851 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2852 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
2856 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
2857 _mesa_error(ctx
, GL_INVALID_VALUE
,
2858 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
2859 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
2863 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2868 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2869 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
2873 * Bind a buffer object to a uniform block binding point.
2874 * As above, but offset = 0.
2877 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
2879 struct gl_buffer_object
*bufObj
)
2881 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
2882 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2886 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
2888 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2889 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2891 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2895 * Bind a buffer object to a shader storage block binding point.
2896 * As above, but offset = 0.
2899 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
2901 struct gl_buffer_object
*bufObj
)
2903 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
2904 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
2908 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
2910 if (bufObj
== ctx
->Shared
->NullBufferObj
)
2911 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
2913 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
2917 * Binds a buffer object to an atomic buffer binding point.
2919 * The caller is responsible for validating the offset,
2920 * flushing the vertices and updating NewDriverState.
2923 set_atomic_buffer_binding(struct gl_context
*ctx
,
2924 struct gl_atomic_buffer_binding
*binding
,
2925 struct gl_buffer_object
*bufObj
,
2929 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
2931 if (bufObj
== ctx
->Shared
->NullBufferObj
) {
2932 binding
->Offset
= -1;
2935 binding
->Offset
= offset
;
2936 binding
->Size
= size
;
2937 bufObj
->UsageHistory
|= USAGE_ATOMIC_COUNTER_BUFFER
;
2942 * Binds a buffer object to an atomic buffer binding point.
2944 * Unlike set_atomic_buffer_binding(), this function also validates the
2945 * index and offset, flushes vertices, and updates NewDriverState.
2946 * It also checks if the binding has actually changing before
2950 bind_atomic_buffer(struct gl_context
*ctx
,
2952 struct gl_buffer_object
*bufObj
,
2957 struct gl_atomic_buffer_binding
*binding
;
2959 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
2960 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(index=%d)", name
, index
);
2964 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
2965 _mesa_error(ctx
, GL_INVALID_VALUE
,
2966 "%s(offset misaligned %d/%d)", name
, (int) offset
,
2967 ATOMIC_COUNTER_SIZE
);
2971 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
2973 binding
= &ctx
->AtomicBufferBindings
[index
];
2974 if (binding
->BufferObject
== bufObj
&&
2975 binding
->Offset
== offset
&&
2976 binding
->Size
== size
) {
2980 FLUSH_VERTICES(ctx
, 0);
2981 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
2983 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offset
, size
);
2987 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
2989 const GLintptr
*offsets
,
2990 const GLsizeiptr
*sizes
)
2992 if (offsets
[index
] < 0) {
2993 /* The ARB_multi_bind spec says:
2995 * "An INVALID_VALUE error is generated by BindBuffersRange if any
2996 * value in <offsets> is less than zero (per binding)."
2998 _mesa_error(ctx
, GL_INVALID_VALUE
,
2999 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3000 index
, (int64_t) offsets
[index
]);
3004 if (sizes
[index
] <= 0) {
3005 /* The ARB_multi_bind spec says:
3007 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3008 * value in <sizes> is less than or equal to zero (per binding)."
3010 _mesa_error(ctx
, GL_INVALID_VALUE
,
3011 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3012 index
, (int64_t) sizes
[index
]);
3020 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3021 GLuint first
, GLsizei count
,
3024 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3025 _mesa_error(ctx
, GL_INVALID_ENUM
,
3026 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3030 /* The ARB_multi_bind_spec says:
3032 * "An INVALID_OPERATION error is generated if <first> + <count> is
3033 * greater than the number of target-specific indexed binding points,
3034 * as described in section 6.7.1."
3036 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3037 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3038 "%s(first=%u + count=%d > the value of "
3039 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3040 caller
, first
, count
,
3041 ctx
->Const
.MaxUniformBufferBindings
);
3049 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3050 GLuint first
, GLsizei count
,
3053 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3054 _mesa_error(ctx
, GL_INVALID_ENUM
,
3055 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3059 /* The ARB_multi_bind_spec says:
3061 * "An INVALID_OPERATION error is generated if <first> + <count> is
3062 * greater than the number of target-specific indexed binding points,
3063 * as described in section 6.7.1."
3065 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3066 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3067 "%s(first=%u + count=%d > the value of "
3068 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3069 caller
, first
, count
,
3070 ctx
->Const
.MaxShaderStorageBufferBindings
);
3078 * Unbind all uniform buffers in the range
3079 * <first> through <first>+<count>-1
3082 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3084 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3087 for (i
= 0; i
< count
; i
++)
3088 set_ubo_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3089 bufObj
, -1, -1, GL_TRUE
);
3093 * Unbind all shader storage buffers in the range
3094 * <first> through <first>+<count>-1
3097 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3100 struct gl_buffer_object
*bufObj
= ctx
->Shared
->NullBufferObj
;
3103 for (i
= 0; i
< count
; i
++)
3104 set_ssbo_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3105 bufObj
, -1, -1, GL_TRUE
);
3109 bind_uniform_buffers_base(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3110 const GLuint
*buffers
)
3114 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, "glBindBuffersBase"))
3117 /* Assume that at least one binding will be changed */
3118 FLUSH_VERTICES(ctx
, 0);
3119 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3122 /* The ARB_multi_bind spec says:
3124 * "If <buffers> is NULL, all bindings from <first> through
3125 * <first>+<count>-1 are reset to their unbound (zero) state."
3127 unbind_uniform_buffers(ctx
, first
, count
);
3131 /* Note that the error semantics for multi-bind commands differ from
3132 * those of other GL commands.
3134 * The Issues section in the ARB_multi_bind spec says:
3136 * "(11) Typically, OpenGL specifies that if an error is generated by a
3137 * command, that command has no effect. This is somewhat
3138 * unfortunate for multi-bind commands, because it would require a
3139 * first pass to scan the entire list of bound objects for errors
3140 * and then a second pass to actually perform the bindings.
3141 * Should we have different error semantics?
3143 * RESOLVED: Yes. In this specification, when the parameters for
3144 * one of the <count> binding points are invalid, that binding point
3145 * is not updated and an error will be generated. However, other
3146 * binding points in the same command will be updated if their
3147 * parameters are valid and no other error occurs."
3150 _mesa_begin_bufferobj_lookups(ctx
);
3152 for (i
= 0; i
< count
; i
++) {
3153 struct gl_uniform_buffer_binding
*binding
=
3154 &ctx
->UniformBufferBindings
[first
+ i
];
3155 struct gl_buffer_object
*bufObj
;
3157 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3158 bufObj
= binding
->BufferObject
;
3160 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3161 "glBindBuffersBase");
3164 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3165 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, GL_TRUE
);
3167 set_ubo_binding(ctx
, binding
, bufObj
, 0, 0, GL_TRUE
);
3171 _mesa_end_bufferobj_lookups(ctx
);
3175 bind_shader_storage_buffers_base(struct gl_context
*ctx
, GLuint first
,
3176 GLsizei count
, const GLuint
*buffers
)
3180 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
,
3181 "glBindBuffersBase"))
3184 /* Assume that at least one binding will be changed */
3185 FLUSH_VERTICES(ctx
, 0);
3186 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3189 /* The ARB_multi_bind spec says:
3191 * "If <buffers> is NULL, all bindings from <first> through
3192 * <first>+<count>-1 are reset to their unbound (zero) state."
3194 unbind_shader_storage_buffers(ctx
, first
, count
);
3198 /* Note that the error semantics for multi-bind commands differ from
3199 * those of other GL commands.
3201 * The Issues section in the ARB_multi_bind spec says:
3203 * "(11) Typically, OpenGL specifies that if an error is generated by a
3204 * command, that command has no effect. This is somewhat
3205 * unfortunate for multi-bind commands, because it would require a
3206 * first pass to scan the entire list of bound objects for errors
3207 * and then a second pass to actually perform the bindings.
3208 * Should we have different error semantics?
3210 * RESOLVED: Yes. In this specification, when the parameters for
3211 * one of the <count> binding points are invalid, that binding point
3212 * is not updated and an error will be generated. However, other
3213 * binding points in the same command will be updated if their
3214 * parameters are valid and no other error occurs."
3217 _mesa_begin_bufferobj_lookups(ctx
);
3219 for (i
= 0; i
< count
; i
++) {
3220 struct gl_shader_storage_buffer_binding
*binding
=
3221 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3222 struct gl_buffer_object
*bufObj
;
3224 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3225 bufObj
= binding
->BufferObject
;
3227 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3228 "glBindBuffersBase");
3231 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3232 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, GL_TRUE
);
3234 set_ssbo_binding(ctx
, binding
, bufObj
, 0, 0, GL_TRUE
);
3238 _mesa_end_bufferobj_lookups(ctx
);
3242 bind_uniform_buffers_range(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3243 const GLuint
*buffers
,
3244 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
3248 if (!error_check_bind_uniform_buffers(ctx
, first
, count
,
3249 "glBindBuffersRange"))
3252 /* Assume that at least one binding will be changed */
3253 FLUSH_VERTICES(ctx
, 0);
3254 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3257 /* The ARB_multi_bind spec says:
3259 * "If <buffers> is NULL, all bindings from <first> through
3260 * <first>+<count>-1 are reset to their unbound (zero) state.
3261 * In this case, the offsets and sizes associated with the
3262 * binding points are set to default values, ignoring
3263 * <offsets> and <sizes>."
3265 unbind_uniform_buffers(ctx
, first
, count
);
3269 /* Note that the error semantics for multi-bind commands differ from
3270 * those of other GL commands.
3272 * The Issues section in the ARB_multi_bind spec says:
3274 * "(11) Typically, OpenGL specifies that if an error is generated by a
3275 * command, that command has no effect. This is somewhat
3276 * unfortunate for multi-bind commands, because it would require a
3277 * first pass to scan the entire list of bound objects for errors
3278 * and then a second pass to actually perform the bindings.
3279 * Should we have different error semantics?
3281 * RESOLVED: Yes. In this specification, when the parameters for
3282 * one of the <count> binding points are invalid, that binding point
3283 * is not updated and an error will be generated. However, other
3284 * binding points in the same command will be updated if their
3285 * parameters are valid and no other error occurs."
3288 _mesa_begin_bufferobj_lookups(ctx
);
3290 for (i
= 0; i
< count
; i
++) {
3291 struct gl_uniform_buffer_binding
*binding
=
3292 &ctx
->UniformBufferBindings
[first
+ i
];
3293 struct gl_buffer_object
*bufObj
;
3295 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3298 /* The ARB_multi_bind spec says:
3300 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3301 * pair of values in <offsets> and <sizes> does not respectively
3302 * satisfy the constraints described for those parameters for the
3303 * specified target, as described in section 6.7.1 (per binding)."
3305 * Section 6.7.1 refers to table 6.5, which says:
3307 * "┌───────────────────────────────────────────────────────────────┐
3308 * │ Uniform buffer array bindings (see sec. 7.6) │
3309 * ├─────────────────────┬─────────────────────────────────────────┤
3311 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
3312 * │ │ OFFSET_ALIGNMENT │
3314 * │ size restriction │ none │
3315 * └─────────────────────┴─────────────────────────────────────────┘"
3317 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3318 _mesa_error(ctx
, GL_INVALID_VALUE
,
3319 "glBindBuffersRange(offsets[%u]=%" PRId64
3320 " is misaligned; it must be a multiple of the value of "
3321 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
3322 "target=GL_UNIFORM_BUFFER)",
3323 i
, (int64_t) offsets
[i
],
3324 ctx
->Const
.UniformBufferOffsetAlignment
);
3328 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3329 bufObj
= binding
->BufferObject
;
3331 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3332 "glBindBuffersRange");
3335 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3336 set_ubo_binding(ctx
, binding
, bufObj
, -1, -1, GL_FALSE
);
3338 set_ubo_binding(ctx
, binding
, bufObj
,
3339 offsets
[i
], sizes
[i
], GL_FALSE
);
3343 _mesa_end_bufferobj_lookups(ctx
);
3347 bind_shader_storage_buffers_range(struct gl_context
*ctx
, GLuint first
,
3348 GLsizei count
, const GLuint
*buffers
,
3349 const GLintptr
*offsets
,
3350 const GLsizeiptr
*sizes
)
3354 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
,
3355 "glBindBuffersRange"))
3358 /* Assume that at least one binding will be changed */
3359 FLUSH_VERTICES(ctx
, 0);
3360 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
3363 /* The ARB_multi_bind spec says:
3365 * "If <buffers> is NULL, all bindings from <first> through
3366 * <first>+<count>-1 are reset to their unbound (zero) state.
3367 * In this case, the offsets and sizes associated with the
3368 * binding points are set to default values, ignoring
3369 * <offsets> and <sizes>."
3371 unbind_shader_storage_buffers(ctx
, first
, count
);
3375 /* Note that the error semantics for multi-bind commands differ from
3376 * those of other GL commands.
3378 * The Issues section in the ARB_multi_bind spec says:
3380 * "(11) Typically, OpenGL specifies that if an error is generated by a
3381 * command, that command has no effect. This is somewhat
3382 * unfortunate for multi-bind commands, because it would require a
3383 * first pass to scan the entire list of bound objects for errors
3384 * and then a second pass to actually perform the bindings.
3385 * Should we have different error semantics?
3387 * RESOLVED: Yes. In this specification, when the parameters for
3388 * one of the <count> binding points are invalid, that binding point
3389 * is not updated and an error will be generated. However, other
3390 * binding points in the same command will be updated if their
3391 * parameters are valid and no other error occurs."
3394 _mesa_begin_bufferobj_lookups(ctx
);
3396 for (i
= 0; i
< count
; i
++) {
3397 struct gl_shader_storage_buffer_binding
*binding
=
3398 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
3399 struct gl_buffer_object
*bufObj
;
3401 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3404 /* The ARB_multi_bind spec says:
3406 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3407 * pair of values in <offsets> and <sizes> does not respectively
3408 * satisfy the constraints described for those parameters for the
3409 * specified target, as described in section 6.7.1 (per binding)."
3411 * Section 6.7.1 refers to table 6.5, which says:
3413 * "┌───────────────────────────────────────────────────────────────┐
3414 * │ Shader storage buffer array bindings (see sec. 7.8) │
3415 * ├─────────────────────┬─────────────────────────────────────────┤
3417 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
3418 * │ │ BUFFER_OFFSET_ALIGNMENT │
3420 * │ size restriction │ none │
3421 * └─────────────────────┴─────────────────────────────────────────┘"
3423 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3424 _mesa_error(ctx
, GL_INVALID_VALUE
,
3425 "glBindBuffersRange(offsets[%u]=%" PRId64
3426 " is misaligned; it must be a multiple of the value of "
3427 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
3428 "target=GL_SHADER_STORAGE_BUFFER)",
3429 i
, (int64_t) offsets
[i
],
3430 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3434 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3435 bufObj
= binding
->BufferObject
;
3437 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3438 "glBindBuffersRange");
3441 if (bufObj
== ctx
->Shared
->NullBufferObj
)
3442 set_ssbo_binding(ctx
, binding
, bufObj
, -1, -1, GL_FALSE
);
3444 set_ssbo_binding(ctx
, binding
, bufObj
,
3445 offsets
[i
], sizes
[i
], GL_FALSE
);
3449 _mesa_end_bufferobj_lookups(ctx
);
3453 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
3454 struct gl_transform_feedback_object
*tfObj
,
3455 GLuint first
, GLsizei count
, const char *caller
)
3457 if (!ctx
->Extensions
.EXT_transform_feedback
) {
3458 _mesa_error(ctx
, GL_INVALID_ENUM
,
3459 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
3463 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
3465 * "An INVALID_OPERATION error is generated :
3468 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
3469 * FEEDBACK_BUFFER and transform feedback is currently active."
3471 * We assume that this is also meant to apply to BindBuffersRange
3472 * and BindBuffersBase.
3474 if (tfObj
->Active
) {
3475 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3476 "%s(Changing transform feedback buffers while "
3477 "transform feedback is active)", caller
);
3481 /* The ARB_multi_bind_spec says:
3483 * "An INVALID_OPERATION error is generated if <first> + <count> is
3484 * greater than the number of target-specific indexed binding points,
3485 * as described in section 6.7.1."
3487 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
3488 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3489 "%s(first=%u + count=%d > the value of "
3490 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
3491 caller
, first
, count
,
3492 ctx
->Const
.MaxTransformFeedbackBuffers
);
3500 * Unbind all transform feedback buffers in the range
3501 * <first> through <first>+<count>-1
3504 unbind_xfb_buffers(struct gl_context
*ctx
,
3505 struct gl_transform_feedback_object
*tfObj
,
3506 GLuint first
, GLsizei count
)
3508 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3511 for (i
= 0; i
< count
; i
++)
3512 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3517 bind_xfb_buffers_base(struct gl_context
*ctx
,
3518 GLuint first
, GLsizei count
,
3519 const GLuint
*buffers
)
3521 struct gl_transform_feedback_object
*tfObj
=
3522 ctx
->TransformFeedback
.CurrentObject
;
3525 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
,
3526 "glBindBuffersBase"))
3529 /* Assume that at least one binding will be changed */
3530 FLUSH_VERTICES(ctx
, 0);
3531 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3534 /* The ARB_multi_bind spec says:
3536 * "If <buffers> is NULL, all bindings from <first> through
3537 * <first>+<count>-1 are reset to their unbound (zero) state."
3539 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3543 /* Note that the error semantics for multi-bind commands differ from
3544 * those of other GL commands.
3546 * The Issues section in the ARB_multi_bind spec says:
3548 * "(11) Typically, OpenGL specifies that if an error is generated by a
3549 * command, that command has no effect. This is somewhat
3550 * unfortunate for multi-bind commands, because it would require a
3551 * first pass to scan the entire list of bound objects for errors
3552 * and then a second pass to actually perform the bindings.
3553 * Should we have different error semantics?
3555 * RESOLVED: Yes. In this specification, when the parameters for
3556 * one of the <count> binding points are invalid, that binding point
3557 * is not updated and an error will be generated. However, other
3558 * binding points in the same command will be updated if their
3559 * parameters are valid and no other error occurs."
3562 _mesa_begin_bufferobj_lookups(ctx
);
3564 for (i
= 0; i
< count
; i
++) {
3565 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[first
+ i
];
3566 struct gl_buffer_object
*bufObj
;
3568 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3569 bufObj
= boundBufObj
;
3571 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3572 "glBindBuffersBase");
3575 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
3579 _mesa_end_bufferobj_lookups(ctx
);
3583 bind_xfb_buffers_range(struct gl_context
*ctx
,
3584 GLuint first
, GLsizei count
,
3585 const GLuint
*buffers
,
3586 const GLintptr
*offsets
,
3587 const GLsizeiptr
*sizes
)
3589 struct gl_transform_feedback_object
*tfObj
=
3590 ctx
->TransformFeedback
.CurrentObject
;
3593 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
,
3594 "glBindBuffersRange"))
3597 /* Assume that at least one binding will be changed */
3598 FLUSH_VERTICES(ctx
, 0);
3599 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
3602 /* The ARB_multi_bind spec says:
3604 * "If <buffers> is NULL, all bindings from <first> through
3605 * <first>+<count>-1 are reset to their unbound (zero) state.
3606 * In this case, the offsets and sizes associated with the
3607 * binding points are set to default values, ignoring
3608 * <offsets> and <sizes>."
3610 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
3614 /* Note that the error semantics for multi-bind commands differ from
3615 * those of other GL commands.
3617 * The Issues section in the ARB_multi_bind spec says:
3619 * "(11) Typically, OpenGL specifies that if an error is generated by a
3620 * command, that command has no effect. This is somewhat
3621 * unfortunate for multi-bind commands, because it would require a
3622 * first pass to scan the entire list of bound objects for errors
3623 * and then a second pass to actually perform the bindings.
3624 * Should we have different error semantics?
3626 * RESOLVED: Yes. In this specification, when the parameters for
3627 * one of the <count> binding points are invalid, that binding point
3628 * is not updated and an error will be generated. However, other
3629 * binding points in the same command will be updated if their
3630 * parameters are valid and no other error occurs."
3633 _mesa_begin_bufferobj_lookups(ctx
);
3635 for (i
= 0; i
< count
; i
++) {
3636 const GLuint index
= first
+ i
;
3637 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
3638 struct gl_buffer_object
*bufObj
;
3640 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3643 /* The ARB_multi_bind spec says:
3645 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3646 * pair of values in <offsets> and <sizes> does not respectively
3647 * satisfy the constraints described for those parameters for the
3648 * specified target, as described in section 6.7.1 (per binding)."
3650 * Section 6.7.1 refers to table 6.5, which says:
3652 * "┌───────────────────────────────────────────────────────────────┐
3653 * │ Transform feedback array bindings (see sec. 13.2.2) │
3654 * ├───────────────────────┬───────────────────────────────────────┤
3656 * │ offset restriction │ multiple of 4 │
3658 * │ size restriction │ multiple of 4 │
3659 * └───────────────────────┴───────────────────────────────────────┘"
3661 if (offsets
[i
] & 0x3) {
3662 _mesa_error(ctx
, GL_INVALID_VALUE
,
3663 "glBindBuffersRange(offsets[%u]=%" PRId64
3664 " is misaligned; it must be a multiple of 4 when "
3665 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3666 i
, (int64_t) offsets
[i
]);
3670 if (sizes
[i
] & 0x3) {
3671 _mesa_error(ctx
, GL_INVALID_VALUE
,
3672 "glBindBuffersRange(sizes[%u]=%" PRId64
3673 " is misaligned; it must be a multiple of 4 when "
3674 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
3675 i
, (int64_t) sizes
[i
]);
3679 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
3680 bufObj
= boundBufObj
;
3682 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3683 "glBindBuffersRange");
3686 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
3687 offsets
[i
], sizes
[i
]);
3690 _mesa_end_bufferobj_lookups(ctx
);
3694 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
3695 GLuint first
, GLsizei count
,
3698 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
3699 _mesa_error(ctx
, GL_INVALID_ENUM
,
3700 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
3704 /* The ARB_multi_bind_spec says:
3706 * "An INVALID_OPERATION error is generated if <first> + <count> is
3707 * greater than the number of target-specific indexed binding points,
3708 * as described in section 6.7.1."
3710 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
3711 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3712 "%s(first=%u + count=%d > the value of "
3713 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
3714 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
3722 * Unbind all atomic counter buffers in the range
3723 * <first> through <first>+<count>-1
3726 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3728 struct gl_buffer_object
* const bufObj
= ctx
->Shared
->NullBufferObj
;
3731 for (i
= 0; i
< count
; i
++)
3732 set_atomic_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
3737 bind_atomic_buffers_base(struct gl_context
*ctx
,
3740 const GLuint
*buffers
)
3744 if (!error_check_bind_atomic_buffers(ctx
, first
, count
,
3745 "glBindBuffersBase"))
3748 /* Assume that at least one binding will be changed */
3749 FLUSH_VERTICES(ctx
, 0);
3750 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3753 /* The ARB_multi_bind spec says:
3755 * "If <buffers> is NULL, all bindings from <first> through
3756 * <first>+<count>-1 are reset to their unbound (zero) state."
3758 unbind_atomic_buffers(ctx
, first
, count
);
3762 /* Note that the error semantics for multi-bind commands differ from
3763 * those of other GL commands.
3765 * The Issues section in the ARB_multi_bind spec says:
3767 * "(11) Typically, OpenGL specifies that if an error is generated by a
3768 * command, that command has no effect. This is somewhat
3769 * unfortunate for multi-bind commands, because it would require a
3770 * first pass to scan the entire list of bound objects for errors
3771 * and then a second pass to actually perform the bindings.
3772 * Should we have different error semantics?
3774 * RESOLVED: Yes. In this specification, when the parameters for
3775 * one of the <count> binding points are invalid, that binding point
3776 * is not updated and an error will be generated. However, other
3777 * binding points in the same command will be updated if their
3778 * parameters are valid and no other error occurs."
3781 _mesa_begin_bufferobj_lookups(ctx
);
3783 for (i
= 0; i
< count
; i
++) {
3784 struct gl_atomic_buffer_binding
*binding
=
3785 &ctx
->AtomicBufferBindings
[first
+ i
];
3786 struct gl_buffer_object
*bufObj
;
3788 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3789 bufObj
= binding
->BufferObject
;
3791 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3792 "glBindBuffersBase");
3795 set_atomic_buffer_binding(ctx
, binding
, bufObj
, 0, 0);
3798 _mesa_end_bufferobj_lookups(ctx
);
3802 bind_atomic_buffers_range(struct gl_context
*ctx
,
3805 const GLuint
*buffers
,
3806 const GLintptr
*offsets
,
3807 const GLsizeiptr
*sizes
)
3811 if (!error_check_bind_atomic_buffers(ctx
, first
, count
,
3812 "glBindBuffersRange"))
3815 /* Assume that at least one binding will be changed */
3816 FLUSH_VERTICES(ctx
, 0);
3817 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
3820 /* The ARB_multi_bind spec says:
3822 * "If <buffers> is NULL, all bindings from <first> through
3823 * <first>+<count>-1 are reset to their unbound (zero) state.
3824 * In this case, the offsets and sizes associated with the
3825 * binding points are set to default values, ignoring
3826 * <offsets> and <sizes>."
3828 unbind_atomic_buffers(ctx
, first
, count
);
3832 /* Note that the error semantics for multi-bind commands differ from
3833 * those of other GL commands.
3835 * The Issues section in the ARB_multi_bind spec says:
3837 * "(11) Typically, OpenGL specifies that if an error is generated by a
3838 * command, that command has no effect. This is somewhat
3839 * unfortunate for multi-bind commands, because it would require a
3840 * first pass to scan the entire list of bound objects for errors
3841 * and then a second pass to actually perform the bindings.
3842 * Should we have different error semantics?
3844 * RESOLVED: Yes. In this specification, when the parameters for
3845 * one of the <count> binding points are invalid, that binding point
3846 * is not updated and an error will be generated. However, other
3847 * binding points in the same command will be updated if their
3848 * parameters are valid and no other error occurs."
3851 _mesa_begin_bufferobj_lookups(ctx
);
3853 for (i
= 0; i
< count
; i
++) {
3854 struct gl_atomic_buffer_binding
*binding
=
3855 &ctx
->AtomicBufferBindings
[first
+ i
];
3856 struct gl_buffer_object
*bufObj
;
3858 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
3861 /* The ARB_multi_bind spec says:
3863 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3864 * pair of values in <offsets> and <sizes> does not respectively
3865 * satisfy the constraints described for those parameters for the
3866 * specified target, as described in section 6.7.1 (per binding)."
3868 * Section 6.7.1 refers to table 6.5, which says:
3870 * "┌───────────────────────────────────────────────────────────────┐
3871 * │ Atomic counter array bindings (see sec. 7.7.2) │
3872 * ├───────────────────────┬───────────────────────────────────────┤
3874 * │ offset restriction │ multiple of 4 │
3876 * │ size restriction │ none │
3877 * └───────────────────────┴───────────────────────────────────────┘"
3879 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
3880 _mesa_error(ctx
, GL_INVALID_VALUE
,
3881 "glBindBuffersRange(offsets[%u]=%" PRId64
3882 " is misaligned; it must be a multiple of %d when "
3883 "target=GL_ATOMIC_COUNTER_BUFFER)",
3884 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
3888 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[i
])
3889 bufObj
= binding
->BufferObject
;
3891 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
,
3892 "glBindBuffersRange");
3895 set_atomic_buffer_binding(ctx
, binding
, bufObj
, offsets
[i
], sizes
[i
]);
3898 _mesa_end_bufferobj_lookups(ctx
);
3902 _mesa_BindBufferRange(GLenum target
, GLuint index
,
3903 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
3905 GET_CURRENT_CONTEXT(ctx
);
3906 struct gl_buffer_object
*bufObj
;
3909 bufObj
= ctx
->Shared
->NullBufferObj
;
3911 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3913 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3914 &bufObj
, "glBindBufferRange"))
3918 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3919 "glBindBufferRange(invalid buffer=%u)", buffer
);
3925 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
3932 case GL_TRANSFORM_FEEDBACK_BUFFER
:
3933 _mesa_bind_buffer_range_transform_feedback(ctx
,
3934 ctx
->TransformFeedback
.CurrentObject
,
3935 index
, bufObj
, offset
, size
,
3938 case GL_UNIFORM_BUFFER
:
3939 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3941 case GL_SHADER_STORAGE_BUFFER
:
3942 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3944 case GL_ATOMIC_COUNTER_BUFFER
:
3945 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
,
3946 "glBindBufferRange");
3949 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
3955 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
3957 GET_CURRENT_CONTEXT(ctx
);
3958 struct gl_buffer_object
*bufObj
;
3961 bufObj
= ctx
->Shared
->NullBufferObj
;
3963 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3965 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3966 &bufObj
, "glBindBufferBase"))
3970 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3971 "glBindBufferBase(invalid buffer=%u)", buffer
);
3975 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
3976 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
3978 * "BindBufferBase is equivalent to calling BindBufferRange with offset
3979 * zero and size equal to the size of buffer."
3981 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
3983 * "If the parameter (starting offset or size) was not specified when the
3984 * buffer object was bound, zero is returned."
3986 * What happens if the size of the buffer changes? Does the size of the
3987 * buffer at the moment glBindBufferBase was called still play a role, like
3988 * the first quote would imply, or is the size meaningless in the
3989 * glBindBufferBase case like the second quote would suggest? The GL 4.1
3990 * core spec page 45 says:
3992 * "It is equivalent to calling BindBufferRange with offset zero, while
3993 * size is determined by the size of the bound buffer at the time the
3996 * My interpretation is that the GL 4.1 spec was a clarification of the
3997 * behavior, not a change. In particular, this choice will only make
3998 * rendering work in cases where it would have had undefined results.
4002 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4003 _mesa_bind_buffer_base_transform_feedback(ctx
,
4004 ctx
->TransformFeedback
.CurrentObject
,
4005 index
, bufObj
, false);
4007 case GL_UNIFORM_BUFFER
:
4008 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4010 case GL_SHADER_STORAGE_BUFFER
:
4011 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4013 case GL_ATOMIC_COUNTER_BUFFER
:
4014 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0,
4015 "glBindBufferBase");
4018 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4024 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4025 const GLuint
*buffers
,
4026 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4028 GET_CURRENT_CONTEXT(ctx
);
4031 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4032 bind_xfb_buffers_range(ctx
, first
, count
, buffers
, offsets
, sizes
);
4034 case GL_UNIFORM_BUFFER
:
4035 bind_uniform_buffers_range(ctx
, first
, count
, buffers
, offsets
, sizes
);
4037 case GL_SHADER_STORAGE_BUFFER
:
4038 bind_shader_storage_buffers_range(ctx
, first
, count
, buffers
, offsets
,
4041 case GL_ATOMIC_COUNTER_BUFFER
:
4042 bind_atomic_buffers_range(ctx
, first
, count
, buffers
,
4046 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4047 _mesa_enum_to_string(target
));
4053 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4054 const GLuint
*buffers
)
4056 GET_CURRENT_CONTEXT(ctx
);
4059 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4060 bind_xfb_buffers_base(ctx
, first
, count
, buffers
);
4062 case GL_UNIFORM_BUFFER
:
4063 bind_uniform_buffers_base(ctx
, first
, count
, buffers
);
4065 case GL_SHADER_STORAGE_BUFFER
:
4066 bind_shader_storage_buffers_base(ctx
, first
, count
, buffers
);
4068 case GL_ATOMIC_COUNTER_BUFFER
:
4069 bind_atomic_buffers_base(ctx
, first
, count
, buffers
);
4072 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4073 _mesa_enum_to_string(target
));
4079 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4082 GET_CURRENT_CONTEXT(ctx
);
4083 struct gl_buffer_object
*bufObj
;
4084 const GLintptr end
= offset
+ length
;
4086 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4088 _mesa_error(ctx
, GL_INVALID_VALUE
,
4089 "glInvalidateBufferSubData(name = 0x%x) invalid object",
4094 /* The GL_ARB_invalidate_subdata spec says:
4096 * "An INVALID_VALUE error is generated if <offset> or <length> is
4097 * negative, or if <offset> + <length> is greater than the value of
4100 if (end
< 0 || end
> bufObj
->Size
) {
4101 _mesa_error(ctx
, GL_INVALID_VALUE
,
4102 "glInvalidateBufferSubData(invalid offset or length)");
4106 /* The OpenGL 4.4 (Core Profile) spec says:
4108 * "An INVALID_OPERATION error is generated if buffer is currently
4109 * mapped by MapBuffer or if the invalidate range intersects the range
4110 * currently mapped by MapBufferRange, unless it was mapped
4111 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4113 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4114 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4115 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4116 "glInvalidateBufferSubData(intersection with mapped "
4121 /* We don't actually do anything for this yet. Just return after
4122 * validating the parameters and generating the required errors.
4128 _mesa_InvalidateBufferData(GLuint buffer
)
4130 GET_CURRENT_CONTEXT(ctx
);
4131 struct gl_buffer_object
*bufObj
;
4133 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4135 _mesa_error(ctx
, GL_INVALID_VALUE
,
4136 "glInvalidateBufferData(name = 0x%x) invalid object",
4141 /* The OpenGL 4.4 (Core Profile) spec says:
4143 * "An INVALID_OPERATION error is generated if buffer is currently
4144 * mapped by MapBuffer or if the invalidate range intersects the range
4145 * currently mapped by MapBufferRange, unless it was mapped
4146 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4148 if (_mesa_check_disallowed_mapping(bufObj
)) {
4149 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4150 "glInvalidateBufferData(intersection with mapped "
4155 /* We don't actually do anything for this yet. Just return after
4156 * validating the parameters and generating the required errors.