2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
29 * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions.
30 * \author Brian Paul, Ian Romanick
34 #include <inttypes.h> /* for PRId64 macro */
35 #include "util/debug.h"
39 #include "util/imports.h"
41 #include "bufferobj.h"
42 #include "externalobjects.h"
45 #include "glformats.h"
47 #include "transformfeedback.h"
49 #include "util/u_atomic.h"
54 /*#define BOUNDS_CHECK*/
58 * We count the number of buffer modification calls to check for
59 * inefficient buffer use. This is the number of such calls before we
62 #define BUFFER_WARNING_CALL_COUNT 4
66 * Helper to warn of possible performance issues, such as frequently
67 * updating a buffer created with GL_STATIC_DRAW. Called via the macro
71 buffer_usage_warning(struct gl_context
*ctx
, GLuint
*id
, const char *fmt
, ...)
76 _mesa_gl_vdebugf(ctx
, id
,
77 MESA_DEBUG_SOURCE_API
,
78 MESA_DEBUG_TYPE_PERFORMANCE
,
79 MESA_DEBUG_SEVERITY_MEDIUM
,
84 #define BUFFER_USAGE_WARNING(CTX, FMT, ...) \
86 static GLuint id = 0; \
87 buffer_usage_warning(CTX, &id, FMT, ##__VA_ARGS__); \
92 * Used as a placeholder for buffer objects between glGenBuffers() and
93 * glBindBuffer() so that glIsBuffer() can work correctly.
95 static struct gl_buffer_object DummyBufferObject
;
99 * Return pointer to address of a buffer object target.
100 * \param ctx the GL context
101 * \param target the buffer object target to be retrieved.
102 * \return pointer to pointer to the buffer object bound to \c target in the
103 * specified context or \c NULL if \c target is invalid.
105 static inline struct gl_buffer_object
**
106 get_buffer_target(struct gl_context
*ctx
, GLenum target
)
108 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0. */
109 if (!_mesa_is_desktop_gl(ctx
) && !_mesa_is_gles3(ctx
)) {
111 case GL_ARRAY_BUFFER
:
112 case GL_ELEMENT_ARRAY_BUFFER
:
114 case GL_PIXEL_PACK_BUFFER
:
115 case GL_PIXEL_UNPACK_BUFFER
:
116 if (!ctx
->Extensions
.EXT_pixel_buffer_object
)
125 case GL_ARRAY_BUFFER_ARB
:
126 if (ctx
->Array
.ArrayBufferObj
)
127 ctx
->Array
.ArrayBufferObj
->UsageHistory
|= USAGE_ARRAY_BUFFER
;
128 return &ctx
->Array
.ArrayBufferObj
;
129 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
130 if (ctx
->Array
.VAO
->IndexBufferObj
)
131 ctx
->Array
.VAO
->IndexBufferObj
->UsageHistory
132 |= USAGE_ELEMENT_ARRAY_BUFFER
;
133 return &ctx
->Array
.VAO
->IndexBufferObj
;
134 case GL_PIXEL_PACK_BUFFER_EXT
:
135 return &ctx
->Pack
.BufferObj
;
136 case GL_PIXEL_UNPACK_BUFFER_EXT
:
137 return &ctx
->Unpack
.BufferObj
;
138 case GL_COPY_READ_BUFFER
:
139 return &ctx
->CopyReadBuffer
;
140 case GL_COPY_WRITE_BUFFER
:
141 return &ctx
->CopyWriteBuffer
;
142 case GL_QUERY_BUFFER
:
143 if (_mesa_has_ARB_query_buffer_object(ctx
))
144 return &ctx
->QueryBuffer
;
146 case GL_DRAW_INDIRECT_BUFFER
:
147 if ((_mesa_is_desktop_gl(ctx
) && ctx
->Extensions
.ARB_draw_indirect
) ||
148 _mesa_is_gles31(ctx
)) {
149 return &ctx
->DrawIndirectBuffer
;
152 case GL_PARAMETER_BUFFER_ARB
:
153 if (_mesa_has_ARB_indirect_parameters(ctx
)) {
154 return &ctx
->ParameterBuffer
;
157 case GL_DISPATCH_INDIRECT_BUFFER
:
158 if (_mesa_has_compute_shaders(ctx
)) {
159 return &ctx
->DispatchIndirectBuffer
;
162 case GL_TRANSFORM_FEEDBACK_BUFFER
:
163 if (ctx
->Extensions
.EXT_transform_feedback
) {
164 return &ctx
->TransformFeedback
.CurrentBuffer
;
167 case GL_TEXTURE_BUFFER
:
168 if (_mesa_has_ARB_texture_buffer_object(ctx
) ||
169 _mesa_has_OES_texture_buffer(ctx
)) {
170 return &ctx
->Texture
.BufferObject
;
173 case GL_UNIFORM_BUFFER
:
174 if (ctx
->Extensions
.ARB_uniform_buffer_object
) {
175 return &ctx
->UniformBuffer
;
178 case GL_SHADER_STORAGE_BUFFER
:
179 if (ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
180 return &ctx
->ShaderStorageBuffer
;
183 case GL_ATOMIC_COUNTER_BUFFER
:
184 if (ctx
->Extensions
.ARB_shader_atomic_counters
) {
185 return &ctx
->AtomicBuffer
;
188 case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
:
189 if (ctx
->Extensions
.AMD_pinned_memory
) {
190 return &ctx
->ExternalVirtualMemoryBuffer
;
201 * Get the buffer object bound to the specified target in a GL context.
202 * \param ctx the GL context
203 * \param target the buffer object target to be retrieved.
204 * \param error the GL error to record if target is illegal.
205 * \return pointer to the buffer object bound to \c target in the
206 * specified context or \c NULL if \c target is invalid.
208 static inline struct gl_buffer_object
*
209 get_buffer(struct gl_context
*ctx
, const char *func
, GLenum target
,
212 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
215 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(target)", func
);
220 _mesa_error(ctx
, error
, "%s(no buffer bound)", func
);
229 * Convert a GLbitfield describing the mapped buffer access flags
230 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
233 simplified_access_mode(struct gl_context
*ctx
, GLbitfield access
)
235 const GLbitfield rwFlags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
236 if ((access
& rwFlags
) == rwFlags
)
237 return GL_READ_WRITE
;
238 if ((access
& GL_MAP_READ_BIT
) == GL_MAP_READ_BIT
)
240 if ((access
& GL_MAP_WRITE_BIT
) == GL_MAP_WRITE_BIT
)
241 return GL_WRITE_ONLY
;
243 /* Otherwise, AccessFlags is zero (the default state).
245 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
247 * Name Type Initial Value Legal Values
249 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
252 * However, table 6.8 in the GL_OES_mapbuffer extension says:
254 * Get Value Type Get Command Value Description
255 * --------- ---- ----------- ----- -----------
256 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
258 * The difference is because GL_OES_mapbuffer only supports mapping buffers
263 return _mesa_is_gles(ctx
) ? GL_WRITE_ONLY
: GL_READ_WRITE
;
268 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
270 * The regions do not overlap if and only if the end of the given
271 * region is before the mapped region or the start of the given region
272 * is after the mapped region.
274 * \param obj Buffer object target on which to operate.
275 * \param offset Offset of the first byte of the subdata range.
276 * \param size Size, in bytes, of the subdata range.
277 * \return true if ranges overlap, false otherwise
281 bufferobj_range_mapped(const struct gl_buffer_object
*obj
,
282 GLintptr offset
, GLsizeiptr size
)
284 if (_mesa_bufferobj_mapped(obj
, MAP_USER
)) {
285 const GLintptr end
= offset
+ size
;
286 const GLintptr mapEnd
= obj
->Mappings
[MAP_USER
].Offset
+
287 obj
->Mappings
[MAP_USER
].Length
;
289 if (!(end
<= obj
->Mappings
[MAP_USER
].Offset
|| offset
>= mapEnd
)) {
298 * Tests the subdata range parameters and sets the GL error code for
299 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
300 * \c glClearBufferSubData.
302 * \param ctx GL context.
303 * \param bufObj The buffer object.
304 * \param offset Offset of the first byte of the subdata range.
305 * \param size Size, in bytes, of the subdata range.
306 * \param mappedRange If true, checks if an overlapping range is mapped.
307 * If false, checks if buffer is mapped.
308 * \param caller Name of calling function for recording errors.
309 * \return false if error, true otherwise
311 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
314 buffer_object_subdata_range_good(struct gl_context
*ctx
,
315 const struct gl_buffer_object
*bufObj
,
316 GLintptr offset
, GLsizeiptr size
,
317 bool mappedRange
, const char *caller
)
320 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", caller
);
325 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset < 0)", caller
);
329 if (offset
+ size
> bufObj
->Size
) {
330 _mesa_error(ctx
, GL_INVALID_VALUE
,
331 "%s(offset %lu + size %lu > buffer size %lu)", caller
,
332 (unsigned long) offset
,
333 (unsigned long) size
,
334 (unsigned long) bufObj
->Size
);
338 if (bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
)
342 if (bufferobj_range_mapped(bufObj
, offset
, size
)) {
343 _mesa_error(ctx
, GL_INVALID_OPERATION
,
344 "%s(range is mapped without persistent bit)",
350 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
351 _mesa_error(ctx
, GL_INVALID_OPERATION
,
352 "%s(buffer is mapped without persistent bit)",
363 * Test the format and type parameters and set the GL error code for
364 * \c glClearBufferData, \c glClearNamedBufferData, \c glClearBufferSubData
365 * and \c glClearNamedBufferSubData.
367 * \param ctx GL context.
368 * \param internalformat Format to which the data is to be converted.
369 * \param format Format of the supplied data.
370 * \param type Type of the supplied data.
371 * \param caller Name of calling function for recording errors.
372 * \return If internalformat, format and type are legal the mesa_format
373 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
375 * \sa glClearBufferData, glClearNamedBufferData, glClearBufferSubData and
376 * glClearNamedBufferSubData.
379 validate_clear_buffer_format(struct gl_context
*ctx
,
380 GLenum internalformat
,
381 GLenum format
, GLenum type
,
384 mesa_format mesaFormat
;
385 GLenum errorFormatType
;
387 mesaFormat
= _mesa_validate_texbuffer_format(ctx
, internalformat
);
388 if (mesaFormat
== MESA_FORMAT_NONE
) {
389 _mesa_error(ctx
, GL_INVALID_ENUM
,
390 "%s(invalid internalformat)", caller
);
391 return MESA_FORMAT_NONE
;
394 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
395 * EXT_texture_integer there is no conversion between integer and
396 * non-integer formats
398 if (_mesa_is_enum_format_signed_int(format
) !=
399 _mesa_is_format_integer_color(mesaFormat
)) {
400 _mesa_error(ctx
, GL_INVALID_OPERATION
,
401 "%s(integer vs non-integer)", caller
);
402 return MESA_FORMAT_NONE
;
405 if (!_mesa_is_color_format(format
)) {
406 _mesa_error(ctx
, GL_INVALID_VALUE
,
407 "%s(format is not a color format)", caller
);
408 return MESA_FORMAT_NONE
;
411 errorFormatType
= _mesa_error_check_format_and_type(ctx
, format
, type
);
412 if (errorFormatType
!= GL_NO_ERROR
) {
413 _mesa_error(ctx
, GL_INVALID_VALUE
,
414 "%s(invalid format or type)", caller
);
415 return MESA_FORMAT_NONE
;
423 * Convert user-specified clear value to the specified internal format.
425 * \param ctx GL context.
426 * \param internalformat Format to which the data is converted.
427 * \param clearValue Points to the converted clear value.
428 * \param format Format of the supplied data.
429 * \param type Type of the supplied data.
430 * \param data Data which is to be converted to internalformat.
431 * \param caller Name of calling function for recording errors.
432 * \return true if data could be converted, false otherwise.
434 * \sa glClearBufferData, glClearBufferSubData
437 convert_clear_buffer_data(struct gl_context
*ctx
,
438 mesa_format internalformat
,
439 GLubyte
*clearValue
, GLenum format
, GLenum type
,
440 const GLvoid
*data
, const char *caller
)
442 GLenum internalformatBase
= _mesa_get_format_base_format(internalformat
);
444 if (_mesa_texstore(ctx
, 1, internalformatBase
, internalformat
,
445 0, &clearValue
, 1, 1, 1,
446 format
, type
, data
, &ctx
->Unpack
)) {
450 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
457 * Allocate and initialize a new buffer object.
459 * Default callback for the \c dd_function_table::NewBufferObject() hook.
461 static struct gl_buffer_object
*
462 _mesa_new_buffer_object(struct gl_context
*ctx
, GLuint name
)
464 struct gl_buffer_object
*obj
= MALLOC_STRUCT(gl_buffer_object
);
468 _mesa_initialize_buffer_object(ctx
, obj
, name
);
474 * Delete a buffer object.
476 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
479 _mesa_delete_buffer_object(struct gl_context
*ctx
,
480 struct gl_buffer_object
*bufObj
)
484 vbo_delete_minmax_cache(bufObj
);
485 _mesa_align_free(bufObj
->Data
);
487 /* assign strange values here to help w/ debugging */
488 bufObj
->RefCount
= -1000;
491 simple_mtx_destroy(&bufObj
->MinMaxCacheMutex
);
499 * Set ptr to bufObj w/ reference counting.
500 * This is normally only called from the _mesa_reference_buffer_object() macro
501 * when there's a real pointer change.
504 _mesa_reference_buffer_object_(struct gl_context
*ctx
,
505 struct gl_buffer_object
**ptr
,
506 struct gl_buffer_object
*bufObj
)
509 /* Unreference the old buffer */
510 struct gl_buffer_object
*oldObj
= *ptr
;
512 if (p_atomic_dec_zero(&oldObj
->RefCount
)) {
513 assert(ctx
->Driver
.DeleteBuffer
);
514 ctx
->Driver
.DeleteBuffer(ctx
, oldObj
);
522 /* reference new buffer */
523 p_atomic_inc(&bufObj
->RefCount
);
530 * Get the value of MESA_NO_MINMAX_CACHE.
533 get_no_minmax_cache()
535 static bool read
= false;
536 static bool disable
= false;
539 disable
= env_var_as_boolean("MESA_NO_MINMAX_CACHE", false);
548 * Initialize a buffer object to default values.
551 _mesa_initialize_buffer_object(struct gl_context
*ctx
,
552 struct gl_buffer_object
*obj
,
555 memset(obj
, 0, sizeof(struct gl_buffer_object
));
558 obj
->Usage
= GL_STATIC_DRAW_ARB
;
560 simple_mtx_init(&obj
->MinMaxCacheMutex
, mtx_plain
);
561 if (get_no_minmax_cache())
562 obj
->UsageHistory
|= USAGE_DISABLE_MINMAX_CACHE
;
568 * Callback called from _mesa_HashWalk()
571 count_buffer_size(GLuint key
, void *data
, void *userData
)
573 const struct gl_buffer_object
*bufObj
=
574 (const struct gl_buffer_object
*) data
;
575 GLuint
*total
= (GLuint
*) userData
;
578 *total
= *total
+ bufObj
->Size
;
583 * Compute total size (in bytes) of all buffer objects for the given context.
584 * For debugging purposes.
587 _mesa_total_buffer_object_memory(struct gl_context
*ctx
)
591 _mesa_HashWalk(ctx
->Shared
->BufferObjects
, count_buffer_size
, &total
);
598 * Allocate space for and store data in a buffer object. Any data that was
599 * previously stored in the buffer object is lost. If \c data is \c NULL,
600 * memory will be allocated, but no copy will occur.
602 * This is the default callback for \c dd_function_table::BufferData()
603 * Note that all GL error checking will have been done already.
605 * \param ctx GL context.
606 * \param target Buffer object target on which to operate.
607 * \param size Size, in bytes, of the new data store.
608 * \param data Pointer to the data to store in the buffer object. This
609 * pointer may be \c NULL.
610 * \param usage Hints about how the data will be used.
611 * \param bufObj Object to be used.
613 * \return GL_TRUE for success, GL_FALSE for failure
614 * \sa glBufferDataARB, dd_function_table::BufferData.
617 buffer_data_fallback(struct gl_context
*ctx
, GLenum target
, GLsizeiptrARB size
,
618 const GLvoid
*data
, GLenum usage
, GLenum storageFlags
,
619 struct gl_buffer_object
*bufObj
)
625 _mesa_align_free( bufObj
->Data
);
627 new_data
= _mesa_align_malloc( size
, ctx
->Const
.MinMapBufferAlignment
);
629 bufObj
->Data
= (GLubyte
*) new_data
;
631 bufObj
->Usage
= usage
;
632 bufObj
->StorageFlags
= storageFlags
;
635 memcpy( bufObj
->Data
, data
, size
);
647 * Replace data in a subrange of buffer object. If the data range
648 * specified by \c size + \c offset extends beyond the end of the buffer or
649 * if \c data is \c NULL, no copy is performed.
651 * This is the default callback for \c dd_function_table::BufferSubData()
652 * Note that all GL error checking will have been done already.
654 * \param ctx GL context.
655 * \param offset Offset of the first byte to be modified.
656 * \param size Size, in bytes, of the data range.
657 * \param data Pointer to the data to store in the buffer object.
658 * \param bufObj Object to be used.
660 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
663 buffer_sub_data_fallback(struct gl_context
*ctx
, GLintptrARB offset
,
664 GLsizeiptrARB size
, const GLvoid
*data
,
665 struct gl_buffer_object
*bufObj
)
669 /* this should have been caught in _mesa_BufferSubData() */
670 assert(size
+ offset
<= bufObj
->Size
);
673 memcpy( (GLubyte
*) bufObj
->Data
+ offset
, data
, size
);
679 * Retrieve data from a subrange of buffer object. If the data range
680 * specified by \c size + \c offset extends beyond the end of the buffer or
681 * if \c data is \c NULL, no copy is performed.
683 * This is the default callback for \c dd_function_table::GetBufferSubData()
684 * Note that all GL error checking will have been done already.
686 * \param ctx GL context.
687 * \param target Buffer object target on which to operate.
688 * \param offset Offset of the first byte to be fetched.
689 * \param size Size, in bytes, of the data range.
690 * \param data Destination for data
691 * \param bufObj Object to be used.
693 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
696 buffer_get_subdata(struct gl_context
*ctx
, GLintptrARB offset
,
697 GLsizeiptrARB size
, GLvoid
*data
,
698 struct gl_buffer_object
*bufObj
)
702 if (bufObj
->Data
&& ((GLsizeiptrARB
) (size
+ offset
) <= bufObj
->Size
)) {
703 memcpy( data
, (GLubyte
*) bufObj
->Data
+ offset
, size
);
709 * Clear a subrange of the buffer object with copies of the supplied data.
710 * If data is NULL the buffer is filled with zeros.
712 * This is the default callback for \c dd_function_table::ClearBufferSubData()
713 * Note that all GL error checking will have been done already.
715 * \param ctx GL context.
716 * \param offset Offset of the first byte to be cleared.
717 * \param size Size, in bytes, of the to be cleared range.
718 * \param clearValue Source of the data.
719 * \param clearValueSize Size, in bytes, of the supplied data.
720 * \param bufObj Object to be cleared.
722 * \sa glClearBufferSubData, glClearBufferData and
723 * dd_function_table::ClearBufferSubData.
726 _mesa_ClearBufferSubData_sw(struct gl_context
*ctx
,
727 GLintptr offset
, GLsizeiptr size
,
728 const GLvoid
*clearValue
,
729 GLsizeiptr clearValueSize
,
730 struct gl_buffer_object
*bufObj
)
735 assert(ctx
->Driver
.MapBufferRange
);
736 dest
= ctx
->Driver
.MapBufferRange(ctx
, offset
, size
,
738 GL_MAP_INVALIDATE_RANGE_BIT
,
739 bufObj
, MAP_INTERNAL
);
742 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearBuffer[Sub]Data");
746 if (clearValue
== NULL
) {
747 /* Clear with zeros, per the spec */
748 memset(dest
, 0, size
);
749 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
753 for (i
= 0; i
< size
/clearValueSize
; ++i
) {
754 memcpy(dest
, clearValue
, clearValueSize
);
755 dest
+= clearValueSize
;
758 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
763 * Default fallback for \c dd_function_table::MapBufferRange().
764 * Called via glMapBufferRange().
767 map_buffer_range_fallback(struct gl_context
*ctx
, GLintptr offset
,
768 GLsizeiptr length
, GLbitfield access
,
769 struct gl_buffer_object
*bufObj
,
770 gl_map_buffer_index index
)
773 assert(!_mesa_bufferobj_mapped(bufObj
, index
));
774 /* Just return a direct pointer to the data */
775 bufObj
->Mappings
[index
].Pointer
= bufObj
->Data
+ offset
;
776 bufObj
->Mappings
[index
].Length
= length
;
777 bufObj
->Mappings
[index
].Offset
= offset
;
778 bufObj
->Mappings
[index
].AccessFlags
= access
;
779 return bufObj
->Mappings
[index
].Pointer
;
784 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
785 * Called via glFlushMappedBufferRange().
788 flush_mapped_buffer_range_fallback(struct gl_context
*ctx
,
789 GLintptr offset
, GLsizeiptr length
,
790 struct gl_buffer_object
*obj
,
791 gl_map_buffer_index index
)
803 * Default callback for \c dd_function_table::UnmapBuffer().
805 * The input parameters will have been already tested for errors.
807 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
810 unmap_buffer_fallback(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
811 gl_map_buffer_index index
)
814 /* XXX we might assert here that bufObj->Pointer is non-null */
815 bufObj
->Mappings
[index
].Pointer
= NULL
;
816 bufObj
->Mappings
[index
].Length
= 0;
817 bufObj
->Mappings
[index
].Offset
= 0;
818 bufObj
->Mappings
[index
].AccessFlags
= 0x0;
824 * Default fallback for \c dd_function_table::CopyBufferSubData().
825 * Called via glCopyBufferSubData().
828 copy_buffer_sub_data_fallback(struct gl_context
*ctx
,
829 struct gl_buffer_object
*src
,
830 struct gl_buffer_object
*dst
,
831 GLintptr readOffset
, GLintptr writeOffset
,
834 GLubyte
*srcPtr
, *dstPtr
;
837 srcPtr
= dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, 0, src
->Size
,
839 GL_MAP_WRITE_BIT
, src
,
845 srcPtr
+= readOffset
;
846 dstPtr
+= writeOffset
;
848 srcPtr
= ctx
->Driver
.MapBufferRange(ctx
, readOffset
, size
,
849 GL_MAP_READ_BIT
, src
,
851 dstPtr
= ctx
->Driver
.MapBufferRange(ctx
, writeOffset
, size
,
853 GL_MAP_INVALIDATE_RANGE_BIT
), dst
,
857 /* Note: the src and dst regions will never overlap. Trying to do so
858 * would generate GL_INVALID_VALUE earlier.
860 if (srcPtr
&& dstPtr
)
861 memcpy(dstPtr
, srcPtr
, size
);
863 ctx
->Driver
.UnmapBuffer(ctx
, src
, MAP_INTERNAL
);
865 ctx
->Driver
.UnmapBuffer(ctx
, dst
, MAP_INTERNAL
);
871 * Initialize the state associated with buffer objects
874 _mesa_init_buffer_objects( struct gl_context
*ctx
)
878 memset(&DummyBufferObject
, 0, sizeof(DummyBufferObject
));
879 simple_mtx_init(&DummyBufferObject
.MinMaxCacheMutex
, mtx_plain
);
880 DummyBufferObject
.RefCount
= 1000*1000*1000; /* never delete */
882 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
883 _mesa_reference_buffer_object(ctx
,
884 &ctx
->UniformBufferBindings
[i
].BufferObject
,
886 ctx
->UniformBufferBindings
[i
].Offset
= -1;
887 ctx
->UniformBufferBindings
[i
].Size
= -1;
890 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
891 _mesa_reference_buffer_object(ctx
,
892 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
894 ctx
->ShaderStorageBufferBindings
[i
].Offset
= -1;
895 ctx
->ShaderStorageBufferBindings
[i
].Size
= -1;
898 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
899 _mesa_reference_buffer_object(ctx
,
900 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
902 ctx
->AtomicBufferBindings
[i
].Offset
= 0;
903 ctx
->AtomicBufferBindings
[i
].Size
= 0;
909 _mesa_free_buffer_objects( struct gl_context
*ctx
)
913 _mesa_reference_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, NULL
);
915 _mesa_reference_buffer_object(ctx
, &ctx
->CopyReadBuffer
, NULL
);
916 _mesa_reference_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, NULL
);
918 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, NULL
);
920 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, NULL
);
922 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, NULL
);
924 _mesa_reference_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, NULL
);
926 _mesa_reference_buffer_object(ctx
, &ctx
->ParameterBuffer
, NULL
);
928 _mesa_reference_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, NULL
);
930 _mesa_reference_buffer_object(ctx
, &ctx
->QueryBuffer
, NULL
);
932 for (i
= 0; i
< MAX_COMBINED_UNIFORM_BUFFERS
; i
++) {
933 _mesa_reference_buffer_object(ctx
,
934 &ctx
->UniformBufferBindings
[i
].BufferObject
,
938 for (i
= 0; i
< MAX_COMBINED_SHADER_STORAGE_BUFFERS
; i
++) {
939 _mesa_reference_buffer_object(ctx
,
940 &ctx
->ShaderStorageBufferBindings
[i
].BufferObject
,
944 for (i
= 0; i
< MAX_COMBINED_ATOMIC_BUFFERS
; i
++) {
945 _mesa_reference_buffer_object(ctx
,
946 &ctx
->AtomicBufferBindings
[i
].BufferObject
,
953 _mesa_handle_bind_buffer_gen(struct gl_context
*ctx
,
955 struct gl_buffer_object
**buf_handle
,
958 struct gl_buffer_object
*buf
= *buf_handle
;
960 if (!buf
&& (ctx
->API
== API_OPENGL_CORE
)) {
961 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(non-gen name)", caller
);
965 if (!buf
|| buf
== &DummyBufferObject
) {
966 /* If this is a new buffer object id, or one which was generated but
967 * never used before, allocate a buffer object now.
969 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffer
);
971 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
974 _mesa_HashInsert(ctx
->Shared
->BufferObjects
, buffer
, buf
);
982 * Bind the specified target to buffer for the specified context.
983 * Called by glBindBuffer() and other functions.
986 bind_buffer_object(struct gl_context
*ctx
,
987 struct gl_buffer_object
**bindTarget
, GLuint buffer
)
989 struct gl_buffer_object
*oldBufObj
;
990 struct gl_buffer_object
*newBufObj
= NULL
;
994 /* Get pointer to old buffer object (to be unbound) */
995 oldBufObj
= *bindTarget
;
996 if ((oldBufObj
&& oldBufObj
->Name
== buffer
&& !oldBufObj
->DeletePending
) ||
997 (!oldBufObj
&& buffer
== 0))
998 return; /* rebinding the same buffer object- no change */
1001 * Get pointer to new buffer object (newBufObj)
1004 /* non-default buffer object */
1005 newBufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1006 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1007 &newBufObj
, "glBindBuffer"))
1010 /* record usage history */
1011 if (bindTarget
== &ctx
->Pack
.BufferObj
)
1012 newBufObj
->UsageHistory
|= USAGE_PIXEL_PACK_BUFFER
;
1015 /* bind new buffer */
1016 _mesa_reference_buffer_object(ctx
, bindTarget
, newBufObj
);
1021 * Update the default buffer objects in the given context to reference those
1022 * specified in the shared state and release those referencing the old
1026 _mesa_update_default_objects_buffer_objects(struct gl_context
*ctx
)
1028 /* Bind 0 to remove references to those in the shared context hash table. */
1029 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1030 bind_buffer_object(ctx
, &ctx
->Array
.VAO
->IndexBufferObj
, 0);
1031 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1032 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1038 * Return the gl_buffer_object for the given ID.
1039 * Always return NULL for ID 0.
1041 struct gl_buffer_object
*
1042 _mesa_lookup_bufferobj(struct gl_context
*ctx
, GLuint buffer
)
1047 return (struct gl_buffer_object
*)
1048 _mesa_HashLookup(ctx
->Shared
->BufferObjects
, buffer
);
1052 struct gl_buffer_object
*
1053 _mesa_lookup_bufferobj_locked(struct gl_context
*ctx
, GLuint buffer
)
1058 return (struct gl_buffer_object
*)
1059 _mesa_HashLookupLocked(ctx
->Shared
->BufferObjects
, buffer
);
1063 * A convenience function for direct state access functions that throws
1064 * GL_INVALID_OPERATION if buffer is not the name of an existing
1067 struct gl_buffer_object
*
1068 _mesa_lookup_bufferobj_err(struct gl_context
*ctx
, GLuint buffer
,
1071 struct gl_buffer_object
*bufObj
;
1073 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1074 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
1075 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1076 "%s(non-existent buffer object %u)", caller
, buffer
);
1085 * Look up a buffer object for a multi-bind function.
1087 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1088 * of generating an error if the buffer ID is not zero or the name
1089 * of an existing buffer object.
1091 * If the buffer ID refers to an existing buffer object, a pointer
1092 * to the buffer object is returned. If the ID is zero, NULL is returned.
1093 * If the ID is not zero and does not refer to a valid buffer object, this
1094 * function returns NULL.
1096 * This function assumes that the caller has already locked the
1097 * hash table mutex by calling
1098 * _mesa_HashLockMutex(ctx->Shared->BufferObjects).
1100 struct gl_buffer_object
*
1101 _mesa_multi_bind_lookup_bufferobj(struct gl_context
*ctx
,
1102 const GLuint
*buffers
,
1103 GLuint index
, const char *caller
,
1106 struct gl_buffer_object
*bufObj
= NULL
;
1110 if (buffers
[index
] != 0) {
1111 bufObj
= _mesa_lookup_bufferobj_locked(ctx
, buffers
[index
]);
1113 /* The multi-bind functions don't create the buffer objects
1114 when they don't exist. */
1115 if (bufObj
== &DummyBufferObject
)
1119 /* The ARB_multi_bind spec says:
1121 * "An INVALID_OPERATION error is generated if any value
1122 * in <buffers> is not zero or the name of an existing
1123 * buffer object (per binding)."
1125 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1126 "%s(buffers[%u]=%u is not zero or the name "
1127 "of an existing buffer object)",
1128 caller
, index
, buffers
[index
]);
1138 * If *ptr points to obj, set ptr = the Null/default buffer object.
1139 * This is a helper for buffer object deletion.
1140 * The GL spec says that deleting a buffer object causes it to get
1141 * unbound from all arrays in the current context.
1144 unbind(struct gl_context
*ctx
,
1145 struct gl_vertex_array_object
*vao
, unsigned index
,
1146 struct gl_buffer_object
*obj
)
1148 if (vao
->BufferBinding
[index
].BufferObj
== obj
) {
1149 _mesa_bind_vertex_buffer(ctx
, vao
, index
, NULL
,
1150 vao
->BufferBinding
[index
].Offset
,
1151 vao
->BufferBinding
[index
].Stride
);
1157 * Plug default/fallback buffer object functions into the device
1161 _mesa_init_buffer_object_functions(struct dd_function_table
*driver
)
1163 /* GL_ARB_vertex/pixel_buffer_object */
1164 driver
->NewBufferObject
= _mesa_new_buffer_object
;
1165 driver
->DeleteBuffer
= _mesa_delete_buffer_object
;
1166 driver
->BufferData
= buffer_data_fallback
;
1167 driver
->BufferSubData
= buffer_sub_data_fallback
;
1168 driver
->GetBufferSubData
= buffer_get_subdata
;
1169 driver
->UnmapBuffer
= unmap_buffer_fallback
;
1171 /* GL_ARB_clear_buffer_object */
1172 driver
->ClearBufferSubData
= _mesa_ClearBufferSubData_sw
;
1174 /* GL_ARB_map_buffer_range */
1175 driver
->MapBufferRange
= map_buffer_range_fallback
;
1176 driver
->FlushMappedBufferRange
= flush_mapped_buffer_range_fallback
;
1178 /* GL_ARB_copy_buffer */
1179 driver
->CopyBufferSubData
= copy_buffer_sub_data_fallback
;
1184 _mesa_buffer_unmap_all_mappings(struct gl_context
*ctx
,
1185 struct gl_buffer_object
*bufObj
)
1187 for (int i
= 0; i
< MAP_COUNT
; i
++) {
1188 if (_mesa_bufferobj_mapped(bufObj
, i
)) {
1189 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, i
);
1190 assert(bufObj
->Mappings
[i
].Pointer
== NULL
);
1191 bufObj
->Mappings
[i
].AccessFlags
= 0;
1197 /**********************************************************************/
1199 /**********************************************************************/
1202 _mesa_BindBuffer_no_error(GLenum target
, GLuint buffer
)
1204 GET_CURRENT_CONTEXT(ctx
);
1206 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1207 bind_buffer_object(ctx
, bindTarget
, buffer
);
1212 _mesa_BindBuffer(GLenum target
, GLuint buffer
)
1214 GET_CURRENT_CONTEXT(ctx
);
1216 if (MESA_VERBOSE
& VERBOSE_API
) {
1217 _mesa_debug(ctx
, "glBindBuffer(%s, %u)\n",
1218 _mesa_enum_to_string(target
), buffer
);
1221 struct gl_buffer_object
**bindTarget
= get_buffer_target(ctx
, target
);
1223 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferARB(target %s)",
1224 _mesa_enum_to_string(target
));
1228 bind_buffer_object(ctx
, bindTarget
, buffer
);
1232 * Binds a buffer object to a binding point.
1234 * The caller is responsible for validating the offset,
1235 * flushing the vertices and updating NewDriverState.
1238 set_buffer_binding(struct gl_context
*ctx
,
1239 struct gl_buffer_binding
*binding
,
1240 struct gl_buffer_object
*bufObj
,
1243 bool autoSize
, gl_buffer_usage usage
)
1245 _mesa_reference_buffer_object(ctx
, &binding
->BufferObject
, bufObj
);
1247 binding
->Offset
= offset
;
1248 binding
->Size
= size
;
1249 binding
->AutomaticSize
= autoSize
;
1251 /* If this is a real buffer object, mark it has having been used
1252 * at some point as an atomic counter buffer.
1255 bufObj
->UsageHistory
|= usage
;
1259 set_buffer_multi_binding(struct gl_context
*ctx
,
1260 const GLuint
*buffers
,
1263 struct gl_buffer_binding
*binding
,
1267 gl_buffer_usage usage
)
1269 struct gl_buffer_object
*bufObj
;
1271 if (binding
->BufferObject
&& binding
->BufferObject
->Name
== buffers
[idx
])
1272 bufObj
= binding
->BufferObject
;
1275 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, idx
, caller
,
1282 set_buffer_binding(ctx
, binding
, bufObj
, -1, -1, !range
, usage
);
1284 set_buffer_binding(ctx
, binding
, bufObj
, offset
, size
, !range
, usage
);
1288 bind_buffer(struct gl_context
*ctx
,
1289 struct gl_buffer_binding
*binding
,
1290 struct gl_buffer_object
*bufObj
,
1294 uint64_t driver_state
,
1295 gl_buffer_usage usage
)
1297 if (binding
->BufferObject
== bufObj
&&
1298 binding
->Offset
== offset
&&
1299 binding
->Size
== size
&&
1300 binding
->AutomaticSize
== autoSize
) {
1304 FLUSH_VERTICES(ctx
, 0);
1305 ctx
->NewDriverState
|= driver_state
;
1307 set_buffer_binding(ctx
, binding
, bufObj
, offset
, size
, autoSize
, usage
);
1311 * Binds a buffer object to a uniform buffer binding point.
1313 * Unlike set_buffer_binding(), this function also flushes vertices
1314 * and updates NewDriverState. It also checks if the binding
1315 * has actually changed before updating it.
1318 bind_uniform_buffer(struct gl_context
*ctx
,
1320 struct gl_buffer_object
*bufObj
,
1325 bind_buffer(ctx
, &ctx
->UniformBufferBindings
[index
],
1326 bufObj
, offset
, size
, autoSize
,
1327 ctx
->DriverFlags
.NewUniformBuffer
,
1328 USAGE_UNIFORM_BUFFER
);
1332 * Binds a buffer object to a shader storage buffer binding point.
1334 * Unlike set_ssbo_binding(), this function also flushes vertices
1335 * and updates NewDriverState. It also checks if the binding
1336 * has actually changed before updating it.
1339 bind_shader_storage_buffer(struct gl_context
*ctx
,
1341 struct gl_buffer_object
*bufObj
,
1346 bind_buffer(ctx
, &ctx
->ShaderStorageBufferBindings
[index
],
1347 bufObj
, offset
, size
, autoSize
,
1348 ctx
->DriverFlags
.NewShaderStorageBuffer
,
1349 USAGE_SHADER_STORAGE_BUFFER
);
1353 * Binds a buffer object to an atomic buffer binding point.
1355 * Unlike set_atomic_binding(), this function also flushes vertices
1356 * and updates NewDriverState. It also checks if the binding
1357 * has actually changed before updating it.
1360 bind_atomic_buffer(struct gl_context
*ctx
, unsigned index
,
1361 struct gl_buffer_object
*bufObj
, GLintptr offset
,
1362 GLsizeiptr size
, GLboolean autoSize
)
1364 bind_buffer(ctx
, &ctx
->AtomicBufferBindings
[index
],
1365 bufObj
, offset
, size
, autoSize
,
1366 ctx
->DriverFlags
.NewAtomicBuffer
,
1367 USAGE_ATOMIC_COUNTER_BUFFER
);
1371 * Bind a buffer object to a uniform block binding point.
1372 * As above, but offset = 0.
1375 bind_buffer_base_uniform_buffer(struct gl_context
*ctx
,
1377 struct gl_buffer_object
*bufObj
)
1379 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
1380 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1384 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
1387 bind_uniform_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1389 bind_uniform_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1393 * Bind a buffer object to a shader storage block binding point.
1394 * As above, but offset = 0.
1397 bind_buffer_base_shader_storage_buffer(struct gl_context
*ctx
,
1399 struct gl_buffer_object
*bufObj
)
1401 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
1402 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1406 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
1409 bind_shader_storage_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1411 bind_shader_storage_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1415 * Bind a buffer object to a shader storage block binding point.
1416 * As above, but offset = 0.
1419 bind_buffer_base_atomic_buffer(struct gl_context
*ctx
,
1421 struct gl_buffer_object
*bufObj
)
1423 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
1424 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferBase(index=%d)", index
);
1428 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
1431 bind_atomic_buffer(ctx
, index
, bufObj
, -1, -1, GL_TRUE
);
1433 bind_atomic_buffer(ctx
, index
, bufObj
, 0, 0, GL_TRUE
);
1437 * Delete a set of buffer objects.
1439 * \param n Number of buffer objects to delete.
1440 * \param ids Array of \c n buffer object IDs.
1443 delete_buffers(struct gl_context
*ctx
, GLsizei n
, const GLuint
*ids
)
1445 FLUSH_VERTICES(ctx
, 0);
1447 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1449 for (GLsizei i
= 0; i
< n
; i
++) {
1450 struct gl_buffer_object
*bufObj
=
1451 _mesa_lookup_bufferobj_locked(ctx
, ids
[i
]);
1453 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
1456 assert(bufObj
->Name
== ids
[i
] || bufObj
== &DummyBufferObject
);
1458 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1460 /* unbind any vertex pointers bound to this buffer */
1461 for (j
= 0; j
< ARRAY_SIZE(vao
->BufferBinding
); j
++) {
1462 unbind(ctx
, vao
, j
, bufObj
);
1465 if (ctx
->Array
.ArrayBufferObj
== bufObj
) {
1466 bind_buffer_object(ctx
, &ctx
->Array
.ArrayBufferObj
, 0);
1468 if (vao
->IndexBufferObj
== bufObj
) {
1469 bind_buffer_object(ctx
, &vao
->IndexBufferObj
, 0);
1472 /* unbind ARB_draw_indirect binding point */
1473 if (ctx
->DrawIndirectBuffer
== bufObj
) {
1474 bind_buffer_object(ctx
, &ctx
->DrawIndirectBuffer
, 0);
1477 /* unbind ARB_indirect_parameters binding point */
1478 if (ctx
->ParameterBuffer
== bufObj
) {
1479 bind_buffer_object(ctx
, &ctx
->ParameterBuffer
, 0);
1482 /* unbind ARB_compute_shader binding point */
1483 if (ctx
->DispatchIndirectBuffer
== bufObj
) {
1484 bind_buffer_object(ctx
, &ctx
->DispatchIndirectBuffer
, 0);
1487 /* unbind ARB_copy_buffer binding points */
1488 if (ctx
->CopyReadBuffer
== bufObj
) {
1489 bind_buffer_object(ctx
, &ctx
->CopyReadBuffer
, 0);
1491 if (ctx
->CopyWriteBuffer
== bufObj
) {
1492 bind_buffer_object(ctx
, &ctx
->CopyWriteBuffer
, 0);
1495 /* unbind transform feedback binding points */
1496 if (ctx
->TransformFeedback
.CurrentBuffer
== bufObj
) {
1497 bind_buffer_object(ctx
, &ctx
->TransformFeedback
.CurrentBuffer
, 0);
1499 for (j
= 0; j
< MAX_FEEDBACK_BUFFERS
; j
++) {
1500 if (ctx
->TransformFeedback
.CurrentObject
->Buffers
[j
] == bufObj
) {
1501 _mesa_bind_buffer_base_transform_feedback(ctx
,
1502 ctx
->TransformFeedback
.CurrentObject
,
1507 /* unbind UBO binding points */
1508 for (j
= 0; j
< ctx
->Const
.MaxUniformBufferBindings
; j
++) {
1509 if (ctx
->UniformBufferBindings
[j
].BufferObject
== bufObj
) {
1510 bind_buffer_base_uniform_buffer(ctx
, j
, NULL
);
1514 if (ctx
->UniformBuffer
== bufObj
) {
1515 bind_buffer_object(ctx
, &ctx
->UniformBuffer
, 0);
1518 /* unbind SSBO binding points */
1519 for (j
= 0; j
< ctx
->Const
.MaxShaderStorageBufferBindings
; j
++) {
1520 if (ctx
->ShaderStorageBufferBindings
[j
].BufferObject
== bufObj
) {
1521 bind_buffer_base_shader_storage_buffer(ctx
, j
, NULL
);
1525 if (ctx
->ShaderStorageBuffer
== bufObj
) {
1526 bind_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, 0);
1529 /* unbind Atomci Buffer binding points */
1530 for (j
= 0; j
< ctx
->Const
.MaxAtomicBufferBindings
; j
++) {
1531 if (ctx
->AtomicBufferBindings
[j
].BufferObject
== bufObj
) {
1532 bind_buffer_base_atomic_buffer(ctx
, j
, NULL
);
1536 if (ctx
->AtomicBuffer
== bufObj
) {
1537 bind_buffer_object(ctx
, &ctx
->AtomicBuffer
, 0);
1540 /* unbind any pixel pack/unpack pointers bound to this buffer */
1541 if (ctx
->Pack
.BufferObj
== bufObj
) {
1542 bind_buffer_object(ctx
, &ctx
->Pack
.BufferObj
, 0);
1544 if (ctx
->Unpack
.BufferObj
== bufObj
) {
1545 bind_buffer_object(ctx
, &ctx
->Unpack
.BufferObj
, 0);
1548 if (ctx
->Texture
.BufferObject
== bufObj
) {
1549 bind_buffer_object(ctx
, &ctx
->Texture
.BufferObject
, 0);
1552 if (ctx
->ExternalVirtualMemoryBuffer
== bufObj
) {
1553 bind_buffer_object(ctx
, &ctx
->ExternalVirtualMemoryBuffer
, 0);
1556 /* unbind query buffer binding point */
1557 if (ctx
->QueryBuffer
== bufObj
) {
1558 bind_buffer_object(ctx
, &ctx
->QueryBuffer
, 0);
1561 /* The ID is immediately freed for re-use */
1562 _mesa_HashRemoveLocked(ctx
->Shared
->BufferObjects
, ids
[i
]);
1563 /* Make sure we do not run into the classic ABA problem on bind.
1564 * We don't want to allow re-binding a buffer object that's been
1565 * "deleted" by glDeleteBuffers().
1567 * The explicit rebinding to the default object in the current context
1568 * prevents the above in the current context, but another context
1569 * sharing the same objects might suffer from this problem.
1570 * The alternative would be to do the hash lookup in any case on bind
1571 * which would introduce more runtime overhead than this.
1573 bufObj
->DeletePending
= GL_TRUE
;
1574 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
1578 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1583 _mesa_DeleteBuffers_no_error(GLsizei n
, const GLuint
*ids
)
1585 GET_CURRENT_CONTEXT(ctx
);
1586 delete_buffers(ctx
, n
, ids
);
1591 _mesa_DeleteBuffers(GLsizei n
, const GLuint
*ids
)
1593 GET_CURRENT_CONTEXT(ctx
);
1596 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteBuffersARB(n)");
1600 delete_buffers(ctx
, n
, ids
);
1605 * This is the implementation for glGenBuffers and glCreateBuffers. It is not
1606 * exposed to the rest of Mesa to encourage the use of nameless buffers in
1610 create_buffers(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1613 struct gl_buffer_object
*buf
;
1619 * This must be atomic (generation and allocation of buffer object IDs)
1621 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
1623 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->BufferObjects
, n
);
1625 /* Insert the ID and pointer into the hash table. If non-DSA, insert a
1626 * DummyBufferObject. Otherwise, create a new buffer object and insert
1629 for (int i
= 0; i
< n
; i
++) {
1630 buffers
[i
] = first
+ i
;
1632 assert(ctx
->Driver
.NewBufferObject
);
1633 buf
= ctx
->Driver
.NewBufferObject(ctx
, buffers
[i
]);
1635 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCreateBuffers");
1636 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1641 buf
= &DummyBufferObject
;
1643 _mesa_HashInsertLocked(ctx
->Shared
->BufferObjects
, buffers
[i
], buf
);
1646 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
1651 create_buffers_err(struct gl_context
*ctx
, GLsizei n
, GLuint
*buffers
, bool dsa
)
1653 const char *func
= dsa
? "glCreateBuffers" : "glGenBuffers";
1655 if (MESA_VERBOSE
& VERBOSE_API
)
1656 _mesa_debug(ctx
, "%s(%d)\n", func
, n
);
1659 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(n %d < 0)", func
, n
);
1663 create_buffers(ctx
, n
, buffers
, dsa
);
1667 * Generate a set of unique buffer object IDs and store them in \c buffers.
1669 * \param n Number of IDs to generate.
1670 * \param buffers Array of \c n locations to store the IDs.
1673 _mesa_GenBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1675 GET_CURRENT_CONTEXT(ctx
);
1676 create_buffers(ctx
, n
, buffers
, false);
1681 _mesa_GenBuffers(GLsizei n
, GLuint
*buffers
)
1683 GET_CURRENT_CONTEXT(ctx
);
1684 create_buffers_err(ctx
, n
, buffers
, false);
1688 * Create a set of buffer objects and store their unique IDs in \c buffers.
1690 * \param n Number of IDs to generate.
1691 * \param buffers Array of \c n locations to store the IDs.
1694 _mesa_CreateBuffers_no_error(GLsizei n
, GLuint
*buffers
)
1696 GET_CURRENT_CONTEXT(ctx
);
1697 create_buffers(ctx
, n
, buffers
, true);
1702 _mesa_CreateBuffers(GLsizei n
, GLuint
*buffers
)
1704 GET_CURRENT_CONTEXT(ctx
);
1705 create_buffers_err(ctx
, n
, buffers
, true);
1710 * Determine if ID is the name of a buffer object.
1712 * \param id ID of the potential buffer object.
1713 * \return \c GL_TRUE if \c id is the name of a buffer object,
1714 * \c GL_FALSE otherwise.
1716 GLboolean GLAPIENTRY
1717 _mesa_IsBuffer(GLuint id
)
1719 struct gl_buffer_object
*bufObj
;
1720 GET_CURRENT_CONTEXT(ctx
);
1721 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
1723 bufObj
= _mesa_lookup_bufferobj(ctx
, id
);
1725 return bufObj
&& bufObj
!= &DummyBufferObject
;
1730 validate_buffer_storage(struct gl_context
*ctx
,
1731 struct gl_buffer_object
*bufObj
, GLsizeiptr size
,
1732 GLbitfield flags
, const char *func
)
1735 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size <= 0)", func
);
1739 GLbitfield valid_flags
= GL_MAP_READ_BIT
|
1741 GL_MAP_PERSISTENT_BIT
|
1742 GL_MAP_COHERENT_BIT
|
1743 GL_DYNAMIC_STORAGE_BIT
|
1744 GL_CLIENT_STORAGE_BIT
;
1746 if (ctx
->Extensions
.ARB_sparse_buffer
)
1747 valid_flags
|= GL_SPARSE_STORAGE_BIT_ARB
;
1749 if (flags
& ~valid_flags
) {
1750 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(invalid flag bits set)", func
);
1754 /* The Errors section of the GL_ARB_sparse_buffer spec says:
1756 * "INVALID_VALUE is generated by BufferStorage if <flags> contains
1757 * SPARSE_STORAGE_BIT_ARB and <flags> also contains any combination of
1758 * MAP_READ_BIT or MAP_WRITE_BIT."
1760 if (flags
& GL_SPARSE_STORAGE_BIT_ARB
&&
1761 flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) {
1762 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(SPARSE_STORAGE and READ/WRITE)", func
);
1766 if (flags
& GL_MAP_PERSISTENT_BIT
&&
1767 !(flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))) {
1768 _mesa_error(ctx
, GL_INVALID_VALUE
,
1769 "%s(PERSISTENT and flags!=READ/WRITE)", func
);
1773 if (flags
& GL_MAP_COHERENT_BIT
&& !(flags
& GL_MAP_PERSISTENT_BIT
)) {
1774 _mesa_error(ctx
, GL_INVALID_VALUE
,
1775 "%s(COHERENT and flags!=PERSISTENT)", func
);
1779 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
1780 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
1789 buffer_storage(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1790 struct gl_memory_object
*memObj
, GLenum target
,
1791 GLsizeiptr size
, const GLvoid
*data
, GLbitfield flags
,
1792 GLuint64 offset
, const char *func
)
1796 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1797 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
1799 FLUSH_VERTICES(ctx
, 0);
1801 bufObj
->Written
= GL_TRUE
;
1802 bufObj
->Immutable
= GL_TRUE
;
1803 bufObj
->MinMaxCacheDirty
= true;
1806 assert(ctx
->Driver
.BufferDataMem
);
1807 res
= ctx
->Driver
.BufferDataMem(ctx
, target
, size
, memObj
, offset
,
1808 GL_DYNAMIC_DRAW
, bufObj
);
1811 assert(ctx
->Driver
.BufferData
);
1812 res
= ctx
->Driver
.BufferData(ctx
, target
, size
, data
, GL_DYNAMIC_DRAW
,
1817 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
1818 /* Even though the interaction between AMD_pinned_memory and
1819 * glBufferStorage is not described in the spec, Graham Sellers
1820 * said that it should behave the same as glBufferData.
1822 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
1825 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
1831 static ALWAYS_INLINE
void
1832 inlined_buffer_storage(GLenum target
, GLuint buffer
, GLsizeiptr size
,
1833 const GLvoid
*data
, GLbitfield flags
,
1834 GLuint memory
, GLuint64 offset
,
1835 bool dsa
, bool mem
, bool no_error
, const char *func
)
1837 GET_CURRENT_CONTEXT(ctx
);
1838 struct gl_buffer_object
*bufObj
;
1839 struct gl_memory_object
*memObj
= NULL
;
1843 if (!ctx
->Extensions
.EXT_memory_object
) {
1844 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(unsupported)", func
);
1848 /* From the EXT_external_objects spec:
1850 * "An INVALID_VALUE error is generated by BufferStorageMemEXT and
1851 * NamedBufferStorageMemEXT if <memory> is 0, or ..."
1854 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(memory == 0)", func
);
1858 memObj
= _mesa_lookup_memory_object(ctx
, memory
);
1862 /* From the EXT_external_objects spec:
1864 * "An INVALID_OPERATION error is generated if <memory> names a
1865 * valid memory object which has no associated memory."
1867 if (!no_error
&& !memObj
->Immutable
) {
1868 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(no associated memory)",
1876 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1878 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
1884 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
1885 bufObj
= *bufObjPtr
;
1887 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
1893 if (no_error
|| validate_buffer_storage(ctx
, bufObj
, size
, flags
, func
))
1894 buffer_storage(ctx
, bufObj
, memObj
, target
, size
, data
, flags
, offset
, func
);
1899 _mesa_BufferStorage_no_error(GLenum target
, GLsizeiptr size
,
1900 const GLvoid
*data
, GLbitfield flags
)
1902 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1903 false, false, true, "glBufferStorage");
1908 _mesa_BufferStorage(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
1911 inlined_buffer_storage(target
, 0, size
, data
, flags
, GL_NONE
, 0,
1912 false, false, false, "glBufferStorage");
1916 _mesa_NamedBufferStorageEXT(GLuint buffer
, GLsizeiptr size
,
1917 const GLvoid
*data
, GLbitfield flags
)
1919 GET_CURRENT_CONTEXT(ctx
);
1921 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
1922 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
1923 &bufObj
, "glNamedBufferStorageEXT"))
1926 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1927 true, false, false, "glNamedBufferStorageEXT");
1932 _mesa_BufferStorageMemEXT(GLenum target
, GLsizeiptr size
,
1933 GLuint memory
, GLuint64 offset
)
1935 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1936 false, true, false, "glBufferStorageMemEXT");
1941 _mesa_BufferStorageMemEXT_no_error(GLenum target
, GLsizeiptr size
,
1942 GLuint memory
, GLuint64 offset
)
1944 inlined_buffer_storage(target
, 0, size
, NULL
, 0, memory
, offset
,
1945 false, true, true, "glBufferStorageMemEXT");
1950 _mesa_NamedBufferStorage_no_error(GLuint buffer
, GLsizeiptr size
,
1951 const GLvoid
*data
, GLbitfield flags
)
1953 /* In direct state access, buffer objects have an unspecified target
1954 * since they are not required to be bound.
1956 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1957 true, false, true, "glNamedBufferStorage");
1962 _mesa_NamedBufferStorage(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
1965 /* In direct state access, buffer objects have an unspecified target
1966 * since they are not required to be bound.
1968 inlined_buffer_storage(GL_NONE
, buffer
, size
, data
, flags
, GL_NONE
, 0,
1969 true, false, false, "glNamedBufferStorage");
1973 _mesa_NamedBufferStorageMemEXT(GLuint buffer
, GLsizeiptr size
,
1974 GLuint memory
, GLuint64 offset
)
1976 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
1977 true, true, false, "glNamedBufferStorageMemEXT");
1982 _mesa_NamedBufferStorageMemEXT_no_error(GLuint buffer
, GLsizeiptr size
,
1983 GLuint memory
, GLuint64 offset
)
1985 inlined_buffer_storage(GL_NONE
, buffer
, size
, GL_NONE
, 0, memory
, offset
,
1986 true, true, true, "glNamedBufferStorageMemEXT");
1990 static ALWAYS_INLINE
void
1991 buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
1992 GLenum target
, GLsizeiptr size
, const GLvoid
*data
, GLenum usage
,
1993 const char *func
, bool no_error
)
1997 if (MESA_VERBOSE
& VERBOSE_API
) {
1998 _mesa_debug(ctx
, "%s(%s, %ld, %p, %s)\n",
2000 _mesa_enum_to_string(target
),
2001 (long int) size
, data
,
2002 _mesa_enum_to_string(usage
));
2007 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size < 0)", func
);
2012 case GL_STREAM_DRAW_ARB
:
2013 valid_usage
= (ctx
->API
!= API_OPENGLES
);
2015 case GL_STATIC_DRAW_ARB
:
2016 case GL_DYNAMIC_DRAW_ARB
:
2019 case GL_STREAM_READ_ARB
:
2020 case GL_STREAM_COPY_ARB
:
2021 case GL_STATIC_READ_ARB
:
2022 case GL_STATIC_COPY_ARB
:
2023 case GL_DYNAMIC_READ_ARB
:
2024 case GL_DYNAMIC_COPY_ARB
:
2025 valid_usage
= _mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
);
2028 valid_usage
= false;
2033 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid usage: %s)", func
,
2034 _mesa_enum_to_string(usage
));
2038 if (bufObj
->Immutable
|| bufObj
->HandleAllocated
) {
2039 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(immutable)", func
);
2044 /* Unmap the existing buffer. We'll replace it now. Not an error. */
2045 _mesa_buffer_unmap_all_mappings(ctx
, bufObj
);
2047 FLUSH_VERTICES(ctx
, 0);
2049 bufObj
->Written
= GL_TRUE
;
2050 bufObj
->MinMaxCacheDirty
= true;
2053 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
2054 bufObj
->Name
, size
, data
, usage
);
2061 assert(ctx
->Driver
.BufferData
);
2062 if (!ctx
->Driver
.BufferData(ctx
, target
, size
, data
, usage
,
2065 GL_DYNAMIC_STORAGE_BIT
,
2067 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
2069 /* From GL_AMD_pinned_memory:
2071 * INVALID_OPERATION is generated by BufferData if <target> is
2072 * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
2073 * mapped to the GPU address space.
2075 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2078 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
2084 buffer_data_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2085 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2086 GLenum usage
, const char *func
)
2088 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, false);
2092 buffer_data_no_error(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2093 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2094 GLenum usage
, const char *func
)
2096 buffer_data(ctx
, bufObj
, target
, size
, data
, usage
, func
, true);
2100 _mesa_buffer_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2101 GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2102 GLenum usage
, const char *func
)
2104 buffer_data_error(ctx
, bufObj
, target
, size
, data
, usage
, func
);
2108 _mesa_BufferData_no_error(GLenum target
, GLsizeiptr size
, const GLvoid
*data
,
2111 GET_CURRENT_CONTEXT(ctx
);
2113 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2114 buffer_data_no_error(ctx
, *bufObj
, target
, size
, data
, usage
,
2119 _mesa_BufferData(GLenum target
, GLsizeiptr size
,
2120 const GLvoid
*data
, GLenum usage
)
2122 GET_CURRENT_CONTEXT(ctx
);
2123 struct gl_buffer_object
*bufObj
;
2125 bufObj
= get_buffer(ctx
, "glBufferData", target
, GL_INVALID_OPERATION
);
2129 _mesa_buffer_data(ctx
, bufObj
, target
, size
, data
, usage
,
2134 _mesa_NamedBufferData_no_error(GLuint buffer
, GLsizeiptr size
,
2135 const GLvoid
*data
, GLenum usage
)
2137 GET_CURRENT_CONTEXT(ctx
);
2139 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2140 buffer_data_no_error(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2141 "glNamedBufferData");
2145 _mesa_NamedBufferData(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2148 GET_CURRENT_CONTEXT(ctx
);
2149 struct gl_buffer_object
*bufObj
;
2151 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glNamedBufferData");
2155 /* In direct state access, buffer objects have an unspecified target since
2156 * they are not required to be bound.
2158 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2159 "glNamedBufferData");
2163 _mesa_NamedBufferDataEXT(GLuint buffer
, GLsizeiptr size
, const GLvoid
*data
,
2166 GET_CURRENT_CONTEXT(ctx
);
2167 struct gl_buffer_object
*bufObj
;
2170 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2171 "glNamedBufferDataEXT(buffer=0)");
2175 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2176 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2177 &bufObj
, "glNamedBufferDataEXT"))
2180 _mesa_buffer_data(ctx
, bufObj
, GL_NONE
, size
, data
, usage
,
2181 "glNamedBufferDataEXT");
2185 validate_buffer_sub_data(struct gl_context
*ctx
,
2186 struct gl_buffer_object
*bufObj
,
2187 GLintptr offset
, GLsizeiptr size
,
2190 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2192 /* error already recorded */
2196 if (bufObj
->Immutable
&&
2197 !(bufObj
->StorageFlags
& GL_DYNAMIC_STORAGE_BIT
)) {
2198 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", func
);
2202 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
2203 bufObj
->Usage
== GL_STATIC_COPY
) &&
2204 bufObj
->NumSubDataCalls
>= BUFFER_WARNING_CALL_COUNT
- 1) {
2205 /* If the application declared the buffer as static draw/copy or stream
2206 * draw, it should not be frequently modified with glBufferSubData.
2208 BUFFER_USAGE_WARNING(ctx
,
2209 "using %s(buffer %u, offset %u, size %u) to "
2210 "update a %s buffer",
2211 func
, bufObj
->Name
, offset
, size
,
2212 _mesa_enum_to_string(bufObj
->Usage
));
2220 * Implementation for glBufferSubData and glNamedBufferSubData.
2222 * \param ctx GL context.
2223 * \param bufObj The buffer object.
2224 * \param offset Offset of the first byte of the subdata range.
2225 * \param size Size, in bytes, of the subdata range.
2226 * \param data The data store.
2227 * \param func Name of calling function for recording errors.
2231 _mesa_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2232 GLintptr offset
, GLsizeiptr size
, const GLvoid
*data
)
2237 bufObj
->NumSubDataCalls
++;
2238 bufObj
->Written
= GL_TRUE
;
2239 bufObj
->MinMaxCacheDirty
= true;
2241 assert(ctx
->Driver
.BufferSubData
);
2242 ctx
->Driver
.BufferSubData(ctx
, offset
, size
, data
, bufObj
);
2246 static ALWAYS_INLINE
void
2247 buffer_sub_data(GLenum target
, GLuint buffer
, GLintptr offset
,
2248 GLsizeiptr size
, const GLvoid
*data
,
2249 bool dsa
, bool no_error
, const char *func
)
2251 GET_CURRENT_CONTEXT(ctx
);
2252 struct gl_buffer_object
*bufObj
;
2256 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2258 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
2264 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2265 bufObj
= *bufObjPtr
;
2267 bufObj
= get_buffer(ctx
, func
, target
, GL_INVALID_OPERATION
);
2273 if (no_error
|| validate_buffer_sub_data(ctx
, bufObj
, offset
, size
, func
))
2274 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
);
2279 _mesa_BufferSubData_no_error(GLenum target
, GLintptr offset
,
2280 GLsizeiptr size
, const GLvoid
*data
)
2282 buffer_sub_data(target
, 0, offset
, size
, data
, false, true,
2288 _mesa_BufferSubData(GLenum target
, GLintptr offset
,
2289 GLsizeiptr size
, const GLvoid
*data
)
2291 buffer_sub_data(target
, 0, offset
, size
, data
, false, false,
2296 _mesa_NamedBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
2297 GLsizeiptr size
, const GLvoid
*data
)
2299 buffer_sub_data(0, buffer
, offset
, size
, data
, true, true,
2300 "glNamedBufferSubData");
2304 _mesa_NamedBufferSubData(GLuint buffer
, GLintptr offset
,
2305 GLsizeiptr size
, const GLvoid
*data
)
2307 buffer_sub_data(0, buffer
, offset
, size
, data
, true, false,
2308 "glNamedBufferSubData");
2312 _mesa_NamedBufferSubDataEXT(GLuint buffer
, GLintptr offset
,
2313 GLsizeiptr size
, const GLvoid
*data
)
2315 GET_CURRENT_CONTEXT(ctx
);
2316 struct gl_buffer_object
*bufObj
;
2319 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2320 "glNamedBufferSubDataEXT(buffer=0)");
2324 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2325 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2326 &bufObj
, "glNamedBufferSubDataEXT"))
2329 if (validate_buffer_sub_data(ctx
, bufObj
, offset
, size
,
2330 "glNamedBufferSubDataEXT")) {
2331 _mesa_buffer_sub_data(ctx
, bufObj
, offset
, size
, data
);
2337 _mesa_GetBufferSubData(GLenum target
, GLintptr offset
,
2338 GLsizeiptr size
, GLvoid
*data
)
2340 GET_CURRENT_CONTEXT(ctx
);
2341 struct gl_buffer_object
*bufObj
;
2343 bufObj
= get_buffer(ctx
, "glGetBufferSubData", target
,
2344 GL_INVALID_OPERATION
);
2348 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2349 "glGetBufferSubData")) {
2353 assert(ctx
->Driver
.GetBufferSubData
);
2354 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2358 _mesa_GetNamedBufferSubData(GLuint buffer
, GLintptr offset
,
2359 GLsizeiptr size
, GLvoid
*data
)
2361 GET_CURRENT_CONTEXT(ctx
);
2362 struct gl_buffer_object
*bufObj
;
2364 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2365 "glGetNamedBufferSubData");
2369 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2370 "glGetNamedBufferSubData")) {
2374 assert(ctx
->Driver
.GetBufferSubData
);
2375 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2380 _mesa_GetNamedBufferSubDataEXT(GLuint buffer
, GLintptr offset
,
2381 GLsizeiptr size
, GLvoid
*data
)
2383 GET_CURRENT_CONTEXT(ctx
);
2384 struct gl_buffer_object
*bufObj
;
2387 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2388 "glGetNamedBufferSubDataEXT(buffer=0)");
2392 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2393 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2394 &bufObj
, "glGetNamedBufferSubDataEXT"))
2397 if (!buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
, false,
2398 "glGetNamedBufferSubDataEXT")) {
2402 assert(ctx
->Driver
.GetBufferSubData
);
2403 ctx
->Driver
.GetBufferSubData(ctx
, offset
, size
, data
, bufObj
);
2407 * \param subdata true if caller is *SubData, false if *Data
2409 static ALWAYS_INLINE
void
2410 clear_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
2411 GLenum internalformat
, GLintptr offset
, GLsizeiptr size
,
2412 GLenum format
, GLenum type
, const GLvoid
*data
,
2413 const char *func
, bool subdata
, bool no_error
)
2415 mesa_format mesaFormat
;
2416 GLubyte clearValue
[MAX_PIXEL_BYTES
];
2417 GLsizeiptr clearValueSize
;
2419 /* This checks for disallowed mappings. */
2420 if (!no_error
&& !buffer_object_subdata_range_good(ctx
, bufObj
, offset
, size
,
2426 mesaFormat
= _mesa_get_texbuffer_format(ctx
, internalformat
);
2428 mesaFormat
= validate_clear_buffer_format(ctx
, internalformat
,
2429 format
, type
, func
);
2432 if (mesaFormat
== MESA_FORMAT_NONE
)
2435 clearValueSize
= _mesa_get_format_bytes(mesaFormat
);
2437 (offset
% clearValueSize
!= 0 || size
% clearValueSize
!= 0)) {
2438 _mesa_error(ctx
, GL_INVALID_VALUE
,
2439 "%s(offset or size is not a multiple of "
2440 "internalformat size)", func
);
2444 /* Bail early. Negative size has already been checked. */
2448 bufObj
->MinMaxCacheDirty
= true;
2451 /* clear to zeros, per the spec */
2452 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2453 NULL
, clearValueSize
, bufObj
);
2457 if (!convert_clear_buffer_data(ctx
, mesaFormat
, clearValue
,
2458 format
, type
, data
, func
)) {
2462 ctx
->Driver
.ClearBufferSubData(ctx
, offset
, size
,
2463 clearValue
, clearValueSize
, bufObj
);
2467 clear_buffer_sub_data_error(struct gl_context
*ctx
,
2468 struct gl_buffer_object
*bufObj
,
2469 GLenum internalformat
, GLintptr offset
,
2470 GLsizeiptr size
, GLenum format
, GLenum type
,
2471 const GLvoid
*data
, const char *func
, bool subdata
)
2473 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2474 type
, data
, func
, subdata
, false);
2479 clear_buffer_sub_data_no_error(struct gl_context
*ctx
,
2480 struct gl_buffer_object
*bufObj
,
2481 GLenum internalformat
, GLintptr offset
,
2482 GLsizeiptr size
, GLenum format
, GLenum type
,
2483 const GLvoid
*data
, const char *func
,
2486 clear_buffer_sub_data(ctx
, bufObj
, internalformat
, offset
, size
, format
,
2487 type
, data
, func
, subdata
, true);
2492 _mesa_ClearBufferData_no_error(GLenum target
, GLenum internalformat
,
2493 GLenum format
, GLenum type
, const GLvoid
*data
)
2495 GET_CURRENT_CONTEXT(ctx
);
2497 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2498 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, 0,
2499 (*bufObj
)->Size
, format
, type
, data
,
2500 "glClearBufferData", false);
2505 _mesa_ClearBufferData(GLenum target
, GLenum internalformat
, GLenum format
,
2506 GLenum type
, const GLvoid
*data
)
2508 GET_CURRENT_CONTEXT(ctx
);
2509 struct gl_buffer_object
*bufObj
;
2511 bufObj
= get_buffer(ctx
, "glClearBufferData", target
, GL_INVALID_VALUE
);
2515 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2516 format
, type
, data
, "glClearBufferData", false);
2521 _mesa_ClearNamedBufferData_no_error(GLuint buffer
, GLenum internalformat
,
2522 GLenum format
, GLenum type
,
2525 GET_CURRENT_CONTEXT(ctx
);
2527 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2528 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2529 format
, type
, data
, "glClearNamedBufferData",
2535 _mesa_ClearNamedBufferData(GLuint buffer
, GLenum internalformat
,
2536 GLenum format
, GLenum type
, const GLvoid
*data
)
2538 GET_CURRENT_CONTEXT(ctx
);
2539 struct gl_buffer_object
*bufObj
;
2541 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glClearNamedBufferData");
2545 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2546 format
, type
, data
, "glClearNamedBufferData",
2552 _mesa_ClearNamedBufferDataEXT(GLuint buffer
, GLenum internalformat
,
2553 GLenum format
, GLenum type
, const GLvoid
*data
)
2555 GET_CURRENT_CONTEXT(ctx
);
2556 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2557 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2558 &bufObj
, "glClearNamedBufferDataEXT"))
2561 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, 0, bufObj
->Size
,
2562 format
, type
, data
, "glClearNamedBufferDataEXT",
2568 _mesa_ClearBufferSubData_no_error(GLenum target
, GLenum internalformat
,
2569 GLintptr offset
, GLsizeiptr size
,
2570 GLenum format
, GLenum type
,
2573 GET_CURRENT_CONTEXT(ctx
);
2575 struct gl_buffer_object
**bufObj
= get_buffer_target(ctx
, target
);
2576 clear_buffer_sub_data_no_error(ctx
, *bufObj
, internalformat
, offset
, size
,
2577 format
, type
, data
, "glClearBufferSubData",
2583 _mesa_ClearBufferSubData(GLenum target
, GLenum internalformat
,
2584 GLintptr offset
, GLsizeiptr size
,
2585 GLenum format
, GLenum type
,
2588 GET_CURRENT_CONTEXT(ctx
);
2589 struct gl_buffer_object
*bufObj
;
2591 bufObj
= get_buffer(ctx
, "glClearBufferSubData", target
, GL_INVALID_VALUE
);
2595 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2596 format
, type
, data
, "glClearBufferSubData",
2602 _mesa_ClearNamedBufferSubData_no_error(GLuint buffer
, GLenum internalformat
,
2603 GLintptr offset
, GLsizeiptr size
,
2604 GLenum format
, GLenum type
,
2607 GET_CURRENT_CONTEXT(ctx
);
2609 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2610 clear_buffer_sub_data_no_error(ctx
, bufObj
, internalformat
, offset
, size
,
2612 "glClearNamedBufferSubData", true);
2617 _mesa_ClearNamedBufferSubData(GLuint buffer
, GLenum internalformat
,
2618 GLintptr offset
, GLsizeiptr size
,
2619 GLenum format
, GLenum type
,
2622 GET_CURRENT_CONTEXT(ctx
);
2623 struct gl_buffer_object
*bufObj
;
2625 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2626 "glClearNamedBufferSubData");
2630 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2631 format
, type
, data
, "glClearNamedBufferSubData",
2636 _mesa_ClearNamedBufferSubDataEXT(GLuint buffer
, GLenum internalformat
,
2637 GLintptr offset
, GLsizeiptr size
,
2638 GLenum format
, GLenum type
,
2641 GET_CURRENT_CONTEXT(ctx
);
2642 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2643 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2644 &bufObj
, "glClearNamedBufferSubDataEXT"))
2647 clear_buffer_sub_data_error(ctx
, bufObj
, internalformat
, offset
, size
,
2648 format
, type
, data
, "glClearNamedBufferSubDataEXT",
2653 unmap_buffer(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
)
2655 GLboolean status
= ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_USER
);
2656 bufObj
->Mappings
[MAP_USER
].AccessFlags
= 0;
2657 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== NULL
);
2658 assert(bufObj
->Mappings
[MAP_USER
].Offset
== 0);
2659 assert(bufObj
->Mappings
[MAP_USER
].Length
== 0);
2665 validate_and_unmap_buffer(struct gl_context
*ctx
,
2666 struct gl_buffer_object
*bufObj
,
2669 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
2671 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
2672 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2673 "%s(buffer is not mapped)", func
);
2678 if (bufObj
->Access
!= GL_READ_ONLY_ARB
) {
2679 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
2681 /* check that last 100 bytes are still = magic value */
2682 for (i
= 0; i
< 100; i
++) {
2683 GLuint pos
= bufObj
->Size
- i
- 1;
2684 if (buf
[pos
] != 123) {
2685 _mesa_warning(ctx
, "Out of bounds buffer object write detected"
2686 " at position %d (value = %u)\n",
2694 if (bufObj
->AccessFlags
& GL_MAP_WRITE_BIT
) {
2695 GLuint i
, unchanged
= 0;
2696 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
2698 /* check which bytes changed */
2699 for (i
= 0; i
< bufObj
->Size
- 1; i
++) {
2700 if (b
[i
] == (i
& 0xff) && b
[i
+1] == ((i
+1) & 0xff)) {
2707 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
2708 bufObj
->Name
, unchanged
, bufObj
->Size
, pos
);
2713 return unmap_buffer(ctx
, bufObj
);
2716 GLboolean GLAPIENTRY
2717 _mesa_UnmapBuffer_no_error(GLenum target
)
2719 GET_CURRENT_CONTEXT(ctx
);
2720 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
2721 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
2723 return unmap_buffer(ctx
, bufObj
);
2726 GLboolean GLAPIENTRY
2727 _mesa_UnmapBuffer(GLenum target
)
2729 GET_CURRENT_CONTEXT(ctx
);
2730 struct gl_buffer_object
*bufObj
;
2732 bufObj
= get_buffer(ctx
, "glUnmapBuffer", target
, GL_INVALID_OPERATION
);
2736 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapBuffer");
2739 GLboolean GLAPIENTRY
2740 _mesa_UnmapNamedBufferEXT_no_error(GLuint buffer
)
2742 GET_CURRENT_CONTEXT(ctx
);
2743 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2745 return unmap_buffer(ctx
, bufObj
);
2748 GLboolean GLAPIENTRY
2749 _mesa_UnmapNamedBufferEXT(GLuint buffer
)
2751 GET_CURRENT_CONTEXT(ctx
);
2752 struct gl_buffer_object
*bufObj
;
2755 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2756 "glUnmapNamedBufferEXT(buffer=0)");
2760 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glUnmapNamedBuffer");
2764 return validate_and_unmap_buffer(ctx
, bufObj
, "glUnmapNamedBuffer");
2769 get_buffer_parameter(struct gl_context
*ctx
,
2770 struct gl_buffer_object
*bufObj
, GLenum pname
,
2771 GLint64
*params
, const char *func
)
2774 case GL_BUFFER_SIZE_ARB
:
2775 *params
= bufObj
->Size
;
2777 case GL_BUFFER_USAGE_ARB
:
2778 *params
= bufObj
->Usage
;
2780 case GL_BUFFER_ACCESS_ARB
:
2781 *params
= simplified_access_mode(ctx
,
2782 bufObj
->Mappings
[MAP_USER
].AccessFlags
);
2784 case GL_BUFFER_MAPPED_ARB
:
2785 *params
= _mesa_bufferobj_mapped(bufObj
, MAP_USER
);
2787 case GL_BUFFER_ACCESS_FLAGS
:
2788 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2790 *params
= bufObj
->Mappings
[MAP_USER
].AccessFlags
;
2792 case GL_BUFFER_MAP_OFFSET
:
2793 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2795 *params
= bufObj
->Mappings
[MAP_USER
].Offset
;
2797 case GL_BUFFER_MAP_LENGTH
:
2798 if (!ctx
->Extensions
.ARB_map_buffer_range
)
2800 *params
= bufObj
->Mappings
[MAP_USER
].Length
;
2802 case GL_BUFFER_IMMUTABLE_STORAGE
:
2803 if (!ctx
->Extensions
.ARB_buffer_storage
)
2805 *params
= bufObj
->Immutable
;
2807 case GL_BUFFER_STORAGE_FLAGS
:
2808 if (!ctx
->Extensions
.ARB_buffer_storage
)
2810 *params
= bufObj
->StorageFlags
;
2819 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s(invalid pname: %s)", func
,
2820 _mesa_enum_to_string(pname
));
2825 _mesa_GetBufferParameteriv(GLenum target
, GLenum pname
, GLint
*params
)
2827 GET_CURRENT_CONTEXT(ctx
);
2828 struct gl_buffer_object
*bufObj
;
2831 bufObj
= get_buffer(ctx
, "glGetBufferParameteriv", target
,
2832 GL_INVALID_OPERATION
);
2836 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2837 "glGetBufferParameteriv"))
2838 return; /* Error already recorded. */
2840 *params
= (GLint
) parameter
;
2844 _mesa_GetBufferParameteri64v(GLenum target
, GLenum pname
, GLint64
*params
)
2846 GET_CURRENT_CONTEXT(ctx
);
2847 struct gl_buffer_object
*bufObj
;
2850 bufObj
= get_buffer(ctx
, "glGetBufferParameteri64v", target
,
2851 GL_INVALID_OPERATION
);
2855 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2856 "glGetBufferParameteri64v"))
2857 return; /* Error already recorded. */
2859 *params
= parameter
;
2863 _mesa_GetNamedBufferParameteriv(GLuint buffer
, GLenum pname
, GLint
*params
)
2865 GET_CURRENT_CONTEXT(ctx
);
2866 struct gl_buffer_object
*bufObj
;
2869 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2870 "glGetNamedBufferParameteriv");
2874 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2875 "glGetNamedBufferParameteriv"))
2876 return; /* Error already recorded. */
2878 *params
= (GLint
) parameter
;
2882 _mesa_GetNamedBufferParameterivEXT(GLuint buffer
, GLenum pname
, GLint
*params
)
2884 GET_CURRENT_CONTEXT(ctx
);
2885 struct gl_buffer_object
*bufObj
;
2889 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2890 "glGetNamedBufferParameterivEXT: buffer=0");
2894 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2895 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2896 &bufObj
, "glGetNamedBufferParameterivEXT"))
2899 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2900 "glGetNamedBufferParameterivEXT"))
2901 return; /* Error already recorded. */
2903 *params
= (GLint
) parameter
;
2907 _mesa_GetNamedBufferParameteri64v(GLuint buffer
, GLenum pname
,
2910 GET_CURRENT_CONTEXT(ctx
);
2911 struct gl_buffer_object
*bufObj
;
2914 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2915 "glGetNamedBufferParameteri64v");
2919 if (!get_buffer_parameter(ctx
, bufObj
, pname
, ¶meter
,
2920 "glGetNamedBufferParameteri64v"))
2921 return; /* Error already recorded. */
2923 *params
= parameter
;
2928 _mesa_GetBufferPointerv(GLenum target
, GLenum pname
, GLvoid
**params
)
2930 GET_CURRENT_CONTEXT(ctx
);
2931 struct gl_buffer_object
*bufObj
;
2933 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2934 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetBufferPointerv(pname != "
2935 "GL_BUFFER_MAP_POINTER)");
2939 bufObj
= get_buffer(ctx
, "glGetBufferPointerv", target
,
2940 GL_INVALID_OPERATION
);
2944 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2948 _mesa_GetNamedBufferPointerv(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2950 GET_CURRENT_CONTEXT(ctx
);
2951 struct gl_buffer_object
*bufObj
;
2953 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2954 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointerv(pname != "
2955 "GL_BUFFER_MAP_POINTER)");
2959 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
2960 "glGetNamedBufferPointerv");
2964 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2968 _mesa_GetNamedBufferPointervEXT(GLuint buffer
, GLenum pname
, GLvoid
**params
)
2970 GET_CURRENT_CONTEXT(ctx
);
2971 struct gl_buffer_object
*bufObj
;
2974 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2975 "glGetNamedBufferPointervEXT(buffer=0)");
2978 if (pname
!= GL_BUFFER_MAP_POINTER
) {
2979 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetNamedBufferPointervEXT(pname != "
2980 "GL_BUFFER_MAP_POINTER)");
2984 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
2985 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
2986 &bufObj
, "glGetNamedBufferPointervEXT"))
2989 *params
= bufObj
->Mappings
[MAP_USER
].Pointer
;
2993 copy_buffer_sub_data(struct gl_context
*ctx
, struct gl_buffer_object
*src
,
2994 struct gl_buffer_object
*dst
, GLintptr readOffset
,
2995 GLintptr writeOffset
, GLsizeiptr size
, const char *func
)
2997 if (_mesa_check_disallowed_mapping(src
)) {
2998 _mesa_error(ctx
, GL_INVALID_OPERATION
,
2999 "%s(readBuffer is mapped)", func
);
3003 if (_mesa_check_disallowed_mapping(dst
)) {
3004 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3005 "%s(writeBuffer is mapped)", func
);
3009 if (readOffset
< 0) {
3010 _mesa_error(ctx
, GL_INVALID_VALUE
,
3011 "%s(readOffset %d < 0)", func
, (int) readOffset
);
3015 if (writeOffset
< 0) {
3016 _mesa_error(ctx
, GL_INVALID_VALUE
,
3017 "%s(writeOffset %d < 0)", func
, (int) writeOffset
);
3022 _mesa_error(ctx
, GL_INVALID_VALUE
,
3023 "%s(size %d < 0)", func
, (int) size
);
3027 if (readOffset
+ size
> src
->Size
) {
3028 _mesa_error(ctx
, GL_INVALID_VALUE
,
3029 "%s(readOffset %d + size %d > src_buffer_size %d)", func
,
3030 (int) readOffset
, (int) size
, (int) src
->Size
);
3034 if (writeOffset
+ size
> dst
->Size
) {
3035 _mesa_error(ctx
, GL_INVALID_VALUE
,
3036 "%s(writeOffset %d + size %d > dst_buffer_size %d)", func
,
3037 (int) writeOffset
, (int) size
, (int) dst
->Size
);
3042 if (readOffset
+ size
<= writeOffset
) {
3045 else if (writeOffset
+ size
<= readOffset
) {
3049 /* overlapping src/dst is illegal */
3050 _mesa_error(ctx
, GL_INVALID_VALUE
,
3051 "%s(overlapping src/dst)", func
);
3056 dst
->MinMaxCacheDirty
= true;
3058 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
, size
);
3062 _mesa_CopyBufferSubData_no_error(GLenum readTarget
, GLenum writeTarget
,
3063 GLintptr readOffset
, GLintptr writeOffset
,
3066 GET_CURRENT_CONTEXT(ctx
);
3068 struct gl_buffer_object
**src_ptr
= get_buffer_target(ctx
, readTarget
);
3069 struct gl_buffer_object
*src
= *src_ptr
;
3071 struct gl_buffer_object
**dst_ptr
= get_buffer_target(ctx
, writeTarget
);
3072 struct gl_buffer_object
*dst
= *dst_ptr
;
3074 dst
->MinMaxCacheDirty
= true;
3075 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
3080 _mesa_CopyBufferSubData(GLenum readTarget
, GLenum writeTarget
,
3081 GLintptr readOffset
, GLintptr writeOffset
,
3084 GET_CURRENT_CONTEXT(ctx
);
3085 struct gl_buffer_object
*src
, *dst
;
3087 src
= get_buffer(ctx
, "glCopyBufferSubData", readTarget
,
3088 GL_INVALID_OPERATION
);
3092 dst
= get_buffer(ctx
, "glCopyBufferSubData", writeTarget
,
3093 GL_INVALID_OPERATION
);
3097 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
3098 "glCopyBufferSubData");
3102 _mesa_NamedCopyBufferSubDataEXT(GLuint readBuffer
, GLuint writeBuffer
,
3103 GLintptr readOffset
, GLintptr writeOffset
,
3106 GET_CURRENT_CONTEXT(ctx
);
3107 struct gl_buffer_object
*src
, *dst
;
3109 src
= _mesa_lookup_bufferobj(ctx
, readBuffer
);
3110 if (!_mesa_handle_bind_buffer_gen(ctx
, readBuffer
,
3112 "glNamedCopyBufferSubDataEXT"))
3115 dst
= _mesa_lookup_bufferobj(ctx
, writeBuffer
);
3116 if (!_mesa_handle_bind_buffer_gen(ctx
, writeBuffer
,
3118 "glNamedCopyBufferSubDataEXT"))
3121 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
3122 "glNamedCopyBufferSubDataEXT");
3126 _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer
, GLuint writeBuffer
,
3127 GLintptr readOffset
,
3128 GLintptr writeOffset
, GLsizeiptr size
)
3130 GET_CURRENT_CONTEXT(ctx
);
3132 struct gl_buffer_object
*src
= _mesa_lookup_bufferobj(ctx
, readBuffer
);
3133 struct gl_buffer_object
*dst
= _mesa_lookup_bufferobj(ctx
, writeBuffer
);
3135 dst
->MinMaxCacheDirty
= true;
3136 ctx
->Driver
.CopyBufferSubData(ctx
, src
, dst
, readOffset
, writeOffset
,
3141 _mesa_CopyNamedBufferSubData(GLuint readBuffer
, GLuint writeBuffer
,
3142 GLintptr readOffset
, GLintptr writeOffset
,
3145 GET_CURRENT_CONTEXT(ctx
);
3146 struct gl_buffer_object
*src
, *dst
;
3148 src
= _mesa_lookup_bufferobj_err(ctx
, readBuffer
,
3149 "glCopyNamedBufferSubData");
3153 dst
= _mesa_lookup_bufferobj_err(ctx
, writeBuffer
,
3154 "glCopyNamedBufferSubData");
3158 copy_buffer_sub_data(ctx
, src
, dst
, readOffset
, writeOffset
, size
,
3159 "glCopyNamedBufferSubData");
3163 validate_map_buffer_range(struct gl_context
*ctx
,
3164 struct gl_buffer_object
*bufObj
, GLintptr offset
,
3165 GLsizeiptr length
, GLbitfield access
,
3168 GLbitfield allowed_access
;
3170 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, false);
3173 _mesa_error(ctx
, GL_INVALID_VALUE
,
3174 "%s(offset %ld < 0)", func
, (long) offset
);
3179 _mesa_error(ctx
, GL_INVALID_VALUE
,
3180 "%s(length %ld < 0)", func
, (long) length
);
3184 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
3186 * "An INVALID_OPERATION error is generated for any of the following
3189 * * <length> is zero."
3191 * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
3192 * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
3196 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(length = 0)", func
);
3200 allowed_access
= GL_MAP_READ_BIT
|
3202 GL_MAP_INVALIDATE_RANGE_BIT
|
3203 GL_MAP_INVALIDATE_BUFFER_BIT
|
3204 GL_MAP_FLUSH_EXPLICIT_BIT
|
3205 GL_MAP_UNSYNCHRONIZED_BIT
;
3207 if (ctx
->Extensions
.ARB_buffer_storage
) {
3208 allowed_access
|= GL_MAP_PERSISTENT_BIT
|
3209 GL_MAP_COHERENT_BIT
;
3212 if (access
& ~allowed_access
) {
3213 /* generate an error if any bits other than those allowed are set */
3214 _mesa_error(ctx
, GL_INVALID_VALUE
,
3215 "%s(access has undefined bits set)", func
);
3219 if ((access
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
)) == 0) {
3220 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3221 "%s(access indicates neither read or write)", func
);
3225 if ((access
& GL_MAP_READ_BIT
) &&
3226 (access
& (GL_MAP_INVALIDATE_RANGE_BIT
|
3227 GL_MAP_INVALIDATE_BUFFER_BIT
|
3228 GL_MAP_UNSYNCHRONIZED_BIT
))) {
3229 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3230 "%s(read access with disallowed bits)", func
);
3234 if ((access
& GL_MAP_FLUSH_EXPLICIT_BIT
) &&
3235 ((access
& GL_MAP_WRITE_BIT
) == 0)) {
3236 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3237 "%s(access has flush explicit without write)", func
);
3241 if (access
& GL_MAP_READ_BIT
&&
3242 !(bufObj
->StorageFlags
& GL_MAP_READ_BIT
)) {
3243 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3244 "%s(buffer does not allow read access)", func
);
3248 if (access
& GL_MAP_WRITE_BIT
&&
3249 !(bufObj
->StorageFlags
& GL_MAP_WRITE_BIT
)) {
3250 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3251 "%s(buffer does not allow write access)", func
);
3255 if (access
& GL_MAP_COHERENT_BIT
&&
3256 !(bufObj
->StorageFlags
& GL_MAP_COHERENT_BIT
)) {
3257 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3258 "%s(buffer does not allow coherent access)", func
);
3262 if (access
& GL_MAP_PERSISTENT_BIT
&&
3263 !(bufObj
->StorageFlags
& GL_MAP_PERSISTENT_BIT
)) {
3264 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3265 "%s(buffer does not allow persistent access)", func
);
3269 if (offset
+ length
> bufObj
->Size
) {
3270 _mesa_error(ctx
, GL_INVALID_VALUE
,
3271 "%s(offset %lu + length %lu > buffer_size %lu)", func
,
3272 (unsigned long) offset
, (unsigned long) length
,
3273 (unsigned long) bufObj
->Size
);
3277 if (_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3278 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3279 "%s(buffer already mapped)", func
);
3283 if (access
& GL_MAP_WRITE_BIT
) {
3284 bufObj
->NumMapBufferWriteCalls
++;
3285 if ((bufObj
->Usage
== GL_STATIC_DRAW
||
3286 bufObj
->Usage
== GL_STATIC_COPY
) &&
3287 bufObj
->NumMapBufferWriteCalls
>= BUFFER_WARNING_CALL_COUNT
) {
3288 BUFFER_USAGE_WARNING(ctx
,
3289 "using %s(buffer %u, offset %u, length %u) to "
3290 "update a %s buffer",
3291 func
, bufObj
->Name
, offset
, length
,
3292 _mesa_enum_to_string(bufObj
->Usage
));
3300 map_buffer_range(struct gl_context
*ctx
, struct gl_buffer_object
*bufObj
,
3301 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
3304 if (!bufObj
->Size
) {
3305 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(buffer size = 0)", func
);
3309 assert(ctx
->Driver
.MapBufferRange
);
3310 void *map
= ctx
->Driver
.MapBufferRange(ctx
, offset
, length
, access
, bufObj
,
3313 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s(map failed)", func
);
3316 /* The driver callback should have set all these fields.
3317 * This is important because other modules (like VBO) might call
3318 * the driver function directly.
3320 assert(bufObj
->Mappings
[MAP_USER
].Pointer
== map
);
3321 assert(bufObj
->Mappings
[MAP_USER
].Length
== length
);
3322 assert(bufObj
->Mappings
[MAP_USER
].Offset
== offset
);
3323 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
== access
);
3326 if (access
& GL_MAP_WRITE_BIT
) {
3327 bufObj
->Written
= GL_TRUE
;
3328 bufObj
->MinMaxCacheDirty
= true;
3332 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3333 printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
3334 bufObj
->Name
, bufObj
->Size
, access
);
3335 /* Access must be write only */
3336 if ((access
& GL_MAP_WRITE_BIT
) && (!(access
& ~GL_MAP_WRITE_BIT
))) {
3338 GLubyte
*b
= (GLubyte
*) bufObj
->Pointer
;
3339 for (i
= 0; i
< bufObj
->Size
; i
++)
3346 if (strstr(func
, "Range") == NULL
) { /* If not MapRange */
3347 GLubyte
*buf
= (GLubyte
*) bufObj
->Pointer
;
3349 /* buffer is 100 bytes larger than requested, fill with magic value */
3350 for (i
= 0; i
< 100; i
++) {
3351 buf
[bufObj
->Size
- i
- 1] = 123;
3360 _mesa_MapBufferRange_no_error(GLenum target
, GLintptr offset
,
3361 GLsizeiptr length
, GLbitfield access
)
3363 GET_CURRENT_CONTEXT(ctx
);
3365 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3366 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3368 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3369 "glMapBufferRange");
3373 _mesa_MapBufferRange(GLenum target
, GLintptr offset
, GLsizeiptr length
,
3376 GET_CURRENT_CONTEXT(ctx
);
3377 struct gl_buffer_object
*bufObj
;
3379 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3380 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3381 "glMapBufferRange(ARB_map_buffer_range not supported)");
3385 bufObj
= get_buffer(ctx
, "glMapBufferRange", target
, GL_INVALID_OPERATION
);
3389 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3390 "glMapBufferRange"))
3393 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3394 "glMapBufferRange");
3398 _mesa_MapNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3399 GLsizeiptr length
, GLbitfield access
)
3401 GET_CURRENT_CONTEXT(ctx
);
3402 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3404 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
,
3405 "glMapNamedBufferRange");
3409 map_named_buffer_range(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3410 GLbitfield access
, bool dsa_ext
, const char *func
)
3412 GET_CURRENT_CONTEXT(ctx
);
3413 struct gl_buffer_object
*bufObj
= NULL
;
3415 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3416 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3417 "%s(ARB_map_buffer_range not supported)", func
);
3422 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3423 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
, &bufObj
, func
))
3426 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, func
);
3431 if (!validate_map_buffer_range(ctx
, bufObj
, offset
, length
, access
, func
))
3434 return map_buffer_range(ctx
, bufObj
, offset
, length
, access
, func
);
3438 _mesa_MapNamedBufferRangeEXT(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3441 GET_CURRENT_CONTEXT(ctx
);
3443 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3444 "glMapNamedBufferRangeEXT(buffer=0)");
3447 return map_named_buffer_range(buffer
, offset
, length
, access
, true,
3448 "glMapNamedBufferRangeEXT");
3452 _mesa_MapNamedBufferRange(GLuint buffer
, GLintptr offset
, GLsizeiptr length
,
3455 return map_named_buffer_range(buffer
, offset
, length
, access
, false,
3456 "glMapNamedBufferRange");
3460 * Converts GLenum access from MapBuffer and MapNamedBuffer into
3461 * flags for input to map_buffer_range.
3463 * \return true if the type of requested access is permissible.
3466 get_map_buffer_access_flags(struct gl_context
*ctx
, GLenum access
,
3470 case GL_READ_ONLY_ARB
:
3471 *flags
= GL_MAP_READ_BIT
;
3472 return _mesa_is_desktop_gl(ctx
);
3473 case GL_WRITE_ONLY_ARB
:
3474 *flags
= GL_MAP_WRITE_BIT
;
3476 case GL_READ_WRITE_ARB
:
3477 *flags
= GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
3478 return _mesa_is_desktop_gl(ctx
);
3486 _mesa_MapBuffer_no_error(GLenum target
, GLenum access
)
3488 GET_CURRENT_CONTEXT(ctx
);
3490 GLbitfield accessFlags
;
3491 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3493 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3494 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3496 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3501 _mesa_MapBuffer(GLenum target
, GLenum access
)
3503 GET_CURRENT_CONTEXT(ctx
);
3504 struct gl_buffer_object
*bufObj
;
3505 GLbitfield accessFlags
;
3507 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3508 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapBuffer(invalid access)");
3512 bufObj
= get_buffer(ctx
, "glMapBuffer", target
, GL_INVALID_OPERATION
);
3516 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3520 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3525 _mesa_MapNamedBuffer_no_error(GLuint buffer
, GLenum access
)
3527 GET_CURRENT_CONTEXT(ctx
);
3529 GLbitfield accessFlags
;
3530 get_map_buffer_access_flags(ctx
, access
, &accessFlags
);
3532 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3534 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3535 "glMapNamedBuffer");
3539 _mesa_MapNamedBuffer(GLuint buffer
, GLenum access
)
3541 GET_CURRENT_CONTEXT(ctx
);
3542 struct gl_buffer_object
*bufObj
;
3543 GLbitfield accessFlags
;
3545 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3546 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBuffer(invalid access)");
3550 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
, "glMapNamedBuffer");
3554 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3555 "glMapNamedBuffer"))
3558 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3559 "glMapNamedBuffer");
3563 _mesa_MapNamedBufferEXT(GLuint buffer
, GLenum access
)
3565 GET_CURRENT_CONTEXT(ctx
);
3567 GLbitfield accessFlags
;
3569 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3570 "glMapNamedBufferEXT(buffer=0)");
3573 if (!get_map_buffer_access_flags(ctx
, access
, &accessFlags
)) {
3574 _mesa_error(ctx
, GL_INVALID_ENUM
, "glMapNamedBufferEXT(invalid access)");
3578 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3579 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3580 &bufObj
, "glMapNamedBufferEXT"))
3583 if (!validate_map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3584 "glMapNamedBufferEXT"))
3587 return map_buffer_range(ctx
, bufObj
, 0, bufObj
->Size
, accessFlags
,
3588 "glMapNamedBufferEXT");
3592 flush_mapped_buffer_range(struct gl_context
*ctx
,
3593 struct gl_buffer_object
*bufObj
,
3594 GLintptr offset
, GLsizeiptr length
,
3597 if (!ctx
->Extensions
.ARB_map_buffer_range
) {
3598 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3599 "%s(ARB_map_buffer_range not supported)", func
);
3604 _mesa_error(ctx
, GL_INVALID_VALUE
,
3605 "%s(offset %ld < 0)", func
, (long) offset
);
3610 _mesa_error(ctx
, GL_INVALID_VALUE
,
3611 "%s(length %ld < 0)", func
, (long) length
);
3615 if (!_mesa_bufferobj_mapped(bufObj
, MAP_USER
)) {
3616 /* buffer is not mapped */
3617 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3618 "%s(buffer is not mapped)", func
);
3622 if ((bufObj
->Mappings
[MAP_USER
].AccessFlags
&
3623 GL_MAP_FLUSH_EXPLICIT_BIT
) == 0) {
3624 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3625 "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func
);
3629 if (offset
+ length
> bufObj
->Mappings
[MAP_USER
].Length
) {
3630 _mesa_error(ctx
, GL_INVALID_VALUE
,
3631 "%s(offset %ld + length %ld > mapped length %ld)", func
,
3632 (long) offset
, (long) length
,
3633 (long) bufObj
->Mappings
[MAP_USER
].Length
);
3637 assert(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_WRITE_BIT
);
3639 if (ctx
->Driver
.FlushMappedBufferRange
)
3640 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3645 _mesa_FlushMappedBufferRange_no_error(GLenum target
, GLintptr offset
,
3648 GET_CURRENT_CONTEXT(ctx
);
3649 struct gl_buffer_object
**bufObjPtr
= get_buffer_target(ctx
, target
);
3650 struct gl_buffer_object
*bufObj
= *bufObjPtr
;
3652 if (ctx
->Driver
.FlushMappedBufferRange
)
3653 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3658 _mesa_FlushMappedBufferRange(GLenum target
, GLintptr offset
,
3661 GET_CURRENT_CONTEXT(ctx
);
3662 struct gl_buffer_object
*bufObj
;
3664 bufObj
= get_buffer(ctx
, "glFlushMappedBufferRange", target
,
3665 GL_INVALID_OPERATION
);
3669 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3670 "glFlushMappedBufferRange");
3674 _mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer
, GLintptr offset
,
3677 GET_CURRENT_CONTEXT(ctx
);
3678 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3680 if (ctx
->Driver
.FlushMappedBufferRange
)
3681 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
, bufObj
,
3686 _mesa_FlushMappedNamedBufferRange(GLuint buffer
, GLintptr offset
,
3689 GET_CURRENT_CONTEXT(ctx
);
3690 struct gl_buffer_object
*bufObj
;
3692 bufObj
= _mesa_lookup_bufferobj_err(ctx
, buffer
,
3693 "glFlushMappedNamedBufferRange");
3697 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3698 "glFlushMappedNamedBufferRange");
3702 _mesa_FlushMappedNamedBufferRangeEXT(GLuint buffer
, GLintptr offset
,
3705 GET_CURRENT_CONTEXT(ctx
);
3706 struct gl_buffer_object
*bufObj
;
3709 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3710 "glFlushMappedNamedBufferRangeEXT(buffer=0)");
3714 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
3715 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
3716 &bufObj
, "glFlushMappedNamedBufferRangeEXT"))
3719 flush_mapped_buffer_range(ctx
, bufObj
, offset
, length
,
3720 "glFlushMappedNamedBufferRangeEXT");
3724 bind_buffer_range_uniform_buffer(struct gl_context
*ctx
, GLuint index
,
3725 struct gl_buffer_object
*bufObj
,
3726 GLintptr offset
, GLsizeiptr size
)
3733 _mesa_reference_buffer_object(ctx
, &ctx
->UniformBuffer
, bufObj
);
3734 bind_uniform_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3738 * Bind a region of a buffer object to a uniform block binding point.
3739 * \param index the uniform buffer binding point index
3740 * \param bufObj the buffer object
3741 * \param offset offset to the start of buffer object region
3742 * \param size size of the buffer object region
3745 bind_buffer_range_uniform_buffer_err(struct gl_context
*ctx
, GLuint index
,
3746 struct gl_buffer_object
*bufObj
,
3747 GLintptr offset
, GLsizeiptr size
)
3749 if (index
>= ctx
->Const
.MaxUniformBufferBindings
) {
3750 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3754 if (offset
& (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
3755 _mesa_error(ctx
, GL_INVALID_VALUE
,
3756 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3757 ctx
->Const
.UniformBufferOffsetAlignment
);
3761 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
3765 bind_buffer_range_shader_storage_buffer(struct gl_context
*ctx
,
3767 struct gl_buffer_object
*bufObj
,
3776 _mesa_reference_buffer_object(ctx
, &ctx
->ShaderStorageBuffer
, bufObj
);
3777 bind_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3781 * Bind a region of a buffer object to a shader storage block binding point.
3782 * \param index the shader storage buffer binding point index
3783 * \param bufObj the buffer object
3784 * \param offset offset to the start of buffer object region
3785 * \param size size of the buffer object region
3788 bind_buffer_range_shader_storage_buffer_err(struct gl_context
*ctx
,
3790 struct gl_buffer_object
*bufObj
,
3791 GLintptr offset
, GLsizeiptr size
)
3793 if (index
>= ctx
->Const
.MaxShaderStorageBufferBindings
) {
3794 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3798 if (offset
& (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
3799 _mesa_error(ctx
, GL_INVALID_VALUE
,
3800 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3801 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
3805 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
, size
);
3809 bind_buffer_range_atomic_buffer(struct gl_context
*ctx
, GLuint index
,
3810 struct gl_buffer_object
*bufObj
,
3811 GLintptr offset
, GLsizeiptr size
)
3818 _mesa_reference_buffer_object(ctx
, &ctx
->AtomicBuffer
, bufObj
);
3819 bind_atomic_buffer(ctx
, index
, bufObj
, offset
, size
, GL_FALSE
);
3823 * Bind a region of a buffer object to an atomic storage block binding point.
3824 * \param index the shader storage buffer binding point index
3825 * \param bufObj the buffer object
3826 * \param offset offset to the start of buffer object region
3827 * \param size size of the buffer object region
3830 bind_buffer_range_atomic_buffer_err(struct gl_context
*ctx
,
3832 struct gl_buffer_object
*bufObj
,
3833 GLintptr offset
, GLsizeiptr size
)
3835 if (index
>= ctx
->Const
.MaxAtomicBufferBindings
) {
3836 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(index=%d)", index
);
3840 if (offset
& (ATOMIC_COUNTER_SIZE
- 1)) {
3841 _mesa_error(ctx
, GL_INVALID_VALUE
,
3842 "glBindBufferRange(offset misaligned %d/%d)", (int) offset
,
3843 ATOMIC_COUNTER_SIZE
);
3847 bind_buffer_range_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
3851 bind_buffers_check_offset_and_size(struct gl_context
*ctx
,
3853 const GLintptr
*offsets
,
3854 const GLsizeiptr
*sizes
)
3856 if (offsets
[index
] < 0) {
3857 /* The ARB_multi_bind spec says:
3859 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3860 * value in <offsets> is less than zero (per binding)."
3862 _mesa_error(ctx
, GL_INVALID_VALUE
,
3863 "glBindBuffersRange(offsets[%u]=%" PRId64
" < 0)",
3864 index
, (int64_t) offsets
[index
]);
3868 if (sizes
[index
] <= 0) {
3869 /* The ARB_multi_bind spec says:
3871 * "An INVALID_VALUE error is generated by BindBuffersRange if any
3872 * value in <sizes> is less than or equal to zero (per binding)."
3874 _mesa_error(ctx
, GL_INVALID_VALUE
,
3875 "glBindBuffersRange(sizes[%u]=%" PRId64
" <= 0)",
3876 index
, (int64_t) sizes
[index
]);
3884 error_check_bind_uniform_buffers(struct gl_context
*ctx
,
3885 GLuint first
, GLsizei count
,
3888 if (!ctx
->Extensions
.ARB_uniform_buffer_object
) {
3889 _mesa_error(ctx
, GL_INVALID_ENUM
,
3890 "%s(target=GL_UNIFORM_BUFFER)", caller
);
3894 /* The ARB_multi_bind_spec says:
3896 * "An INVALID_OPERATION error is generated if <first> + <count> is
3897 * greater than the number of target-specific indexed binding points,
3898 * as described in section 6.7.1."
3900 if (first
+ count
> ctx
->Const
.MaxUniformBufferBindings
) {
3901 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3902 "%s(first=%u + count=%d > the value of "
3903 "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
3904 caller
, first
, count
,
3905 ctx
->Const
.MaxUniformBufferBindings
);
3913 error_check_bind_shader_storage_buffers(struct gl_context
*ctx
,
3914 GLuint first
, GLsizei count
,
3917 if (!ctx
->Extensions
.ARB_shader_storage_buffer_object
) {
3918 _mesa_error(ctx
, GL_INVALID_ENUM
,
3919 "%s(target=GL_SHADER_STORAGE_BUFFER)", caller
);
3923 /* The ARB_multi_bind_spec says:
3925 * "An INVALID_OPERATION error is generated if <first> + <count> is
3926 * greater than the number of target-specific indexed binding points,
3927 * as described in section 6.7.1."
3929 if (first
+ count
> ctx
->Const
.MaxShaderStorageBufferBindings
) {
3930 _mesa_error(ctx
, GL_INVALID_OPERATION
,
3931 "%s(first=%u + count=%d > the value of "
3932 "GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS=%u)",
3933 caller
, first
, count
,
3934 ctx
->Const
.MaxShaderStorageBufferBindings
);
3942 * Unbind all uniform buffers in the range
3943 * <first> through <first>+<count>-1
3946 unbind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
3948 for (int i
= 0; i
< count
; i
++)
3949 set_buffer_binding(ctx
, &ctx
->UniformBufferBindings
[first
+ i
],
3950 NULL
, -1, -1, GL_TRUE
, 0);
3954 * Unbind all shader storage buffers in the range
3955 * <first> through <first>+<count>-1
3958 unbind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
3961 for (int i
= 0; i
< count
; i
++)
3962 set_buffer_binding(ctx
, &ctx
->ShaderStorageBufferBindings
[first
+ i
],
3963 NULL
, -1, -1, GL_TRUE
, 0);
3967 bind_uniform_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
3968 const GLuint
*buffers
,
3970 const GLintptr
*offsets
, const GLsizeiptr
*sizes
,
3973 if (!error_check_bind_uniform_buffers(ctx
, first
, count
, caller
))
3976 /* Assume that at least one binding will be changed */
3977 FLUSH_VERTICES(ctx
, 0);
3978 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewUniformBuffer
;
3981 /* The ARB_multi_bind spec says:
3983 * "If <buffers> is NULL, all bindings from <first> through
3984 * <first>+<count>-1 are reset to their unbound (zero) state.
3985 * In this case, the offsets and sizes associated with the
3986 * binding points are set to default values, ignoring
3987 * <offsets> and <sizes>."
3989 unbind_uniform_buffers(ctx
, first
, count
);
3993 /* Note that the error semantics for multi-bind commands differ from
3994 * those of other GL commands.
3996 * The Issues section in the ARB_multi_bind spec says:
3998 * "(11) Typically, OpenGL specifies that if an error is generated by a
3999 * command, that command has no effect. This is somewhat
4000 * unfortunate for multi-bind commands, because it would require a
4001 * first pass to scan the entire list of bound objects for errors
4002 * and then a second pass to actually perform the bindings.
4003 * Should we have different error semantics?
4005 * RESOLVED: Yes. In this specification, when the parameters for
4006 * one of the <count> binding points are invalid, that binding point
4007 * is not updated and an error will be generated. However, other
4008 * binding points in the same command will be updated if their
4009 * parameters are valid and no other error occurs."
4012 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4014 for (int i
= 0; i
< count
; i
++) {
4015 struct gl_buffer_binding
*binding
=
4016 &ctx
->UniformBufferBindings
[first
+ i
];
4017 GLintptr offset
= 0;
4018 GLsizeiptr size
= 0;
4021 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4024 /* The ARB_multi_bind spec says:
4026 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4027 * pair of values in <offsets> and <sizes> does not respectively
4028 * satisfy the constraints described for those parameters for the
4029 * specified target, as described in section 6.7.1 (per binding)."
4031 * Section 6.7.1 refers to table 6.5, which says:
4033 * "┌───────────────────────────────────────────────────────────────┐
4034 * │ Uniform buffer array bindings (see sec. 7.6) │
4035 * ├─────────────────────┬─────────────────────────────────────────┤
4037 * │ offset restriction │ multiple of value of UNIFORM_BUFFER_- │
4038 * │ │ OFFSET_ALIGNMENT │
4040 * │ size restriction │ none │
4041 * └─────────────────────┴─────────────────────────────────────────┘"
4043 if (offsets
[i
] & (ctx
->Const
.UniformBufferOffsetAlignment
- 1)) {
4044 _mesa_error(ctx
, GL_INVALID_VALUE
,
4045 "glBindBuffersRange(offsets[%u]=%" PRId64
4046 " is misaligned; it must be a multiple of the value of "
4047 "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
4048 "target=GL_UNIFORM_BUFFER)",
4049 i
, (int64_t) offsets
[i
],
4050 ctx
->Const
.UniformBufferOffsetAlignment
);
4054 offset
= offsets
[i
];
4058 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
4059 binding
, offset
, size
, range
,
4060 USAGE_UNIFORM_BUFFER
);
4063 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4067 bind_shader_storage_buffers(struct gl_context
*ctx
, GLuint first
,
4068 GLsizei count
, const GLuint
*buffers
,
4070 const GLintptr
*offsets
,
4071 const GLsizeiptr
*sizes
,
4074 if (!error_check_bind_shader_storage_buffers(ctx
, first
, count
, caller
))
4077 /* Assume that at least one binding will be changed */
4078 FLUSH_VERTICES(ctx
, 0);
4079 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderStorageBuffer
;
4082 /* The ARB_multi_bind spec says:
4084 * "If <buffers> is NULL, all bindings from <first> through
4085 * <first>+<count>-1 are reset to their unbound (zero) state.
4086 * In this case, the offsets and sizes associated with the
4087 * binding points are set to default values, ignoring
4088 * <offsets> and <sizes>."
4090 unbind_shader_storage_buffers(ctx
, first
, count
);
4094 /* Note that the error semantics for multi-bind commands differ from
4095 * those of other GL commands.
4097 * The Issues section in the ARB_multi_bind spec says:
4099 * "(11) Typically, OpenGL specifies that if an error is generated by a
4100 * command, that command has no effect. This is somewhat
4101 * unfortunate for multi-bind commands, because it would require a
4102 * first pass to scan the entire list of bound objects for errors
4103 * and then a second pass to actually perform the bindings.
4104 * Should we have different error semantics?
4106 * RESOLVED: Yes. In this specification, when the parameters for
4107 * one of the <count> binding points are invalid, that binding point
4108 * is not updated and an error will be generated. However, other
4109 * binding points in the same command will be updated if their
4110 * parameters are valid and no other error occurs."
4113 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4115 for (int i
= 0; i
< count
; i
++) {
4116 struct gl_buffer_binding
*binding
=
4117 &ctx
->ShaderStorageBufferBindings
[first
+ i
];
4118 GLintptr offset
= 0;
4119 GLsizeiptr size
= 0;
4122 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4125 /* The ARB_multi_bind spec says:
4127 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4128 * pair of values in <offsets> and <sizes> does not respectively
4129 * satisfy the constraints described for those parameters for the
4130 * specified target, as described in section 6.7.1 (per binding)."
4132 * Section 6.7.1 refers to table 6.5, which says:
4134 * "┌───────────────────────────────────────────────────────────────┐
4135 * │ Shader storage buffer array bindings (see sec. 7.8) │
4136 * ├─────────────────────┬─────────────────────────────────────────┤
4138 * │ offset restriction │ multiple of value of SHADER_STORAGE_- │
4139 * │ │ BUFFER_OFFSET_ALIGNMENT │
4141 * │ size restriction │ none │
4142 * └─────────────────────┴─────────────────────────────────────────┘"
4144 if (offsets
[i
] & (ctx
->Const
.ShaderStorageBufferOffsetAlignment
- 1)) {
4145 _mesa_error(ctx
, GL_INVALID_VALUE
,
4146 "glBindBuffersRange(offsets[%u]=%" PRId64
4147 " is misaligned; it must be a multiple of the value of "
4148 "GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT=%u when "
4149 "target=GL_SHADER_STORAGE_BUFFER)",
4150 i
, (int64_t) offsets
[i
],
4151 ctx
->Const
.ShaderStorageBufferOffsetAlignment
);
4155 offset
= offsets
[i
];
4159 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
4160 binding
, offset
, size
, range
,
4161 USAGE_SHADER_STORAGE_BUFFER
);
4164 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4168 error_check_bind_xfb_buffers(struct gl_context
*ctx
,
4169 struct gl_transform_feedback_object
*tfObj
,
4170 GLuint first
, GLsizei count
, const char *caller
)
4172 if (!ctx
->Extensions
.EXT_transform_feedback
) {
4173 _mesa_error(ctx
, GL_INVALID_ENUM
,
4174 "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller
);
4178 /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
4180 * "An INVALID_OPERATION error is generated :
4183 * • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
4184 * FEEDBACK_BUFFER and transform feedback is currently active."
4186 * We assume that this is also meant to apply to BindBuffersRange
4187 * and BindBuffersBase.
4189 if (tfObj
->Active
) {
4190 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4191 "%s(Changing transform feedback buffers while "
4192 "transform feedback is active)", caller
);
4196 /* The ARB_multi_bind_spec says:
4198 * "An INVALID_OPERATION error is generated if <first> + <count> is
4199 * greater than the number of target-specific indexed binding points,
4200 * as described in section 6.7.1."
4202 if (first
+ count
> ctx
->Const
.MaxTransformFeedbackBuffers
) {
4203 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4204 "%s(first=%u + count=%d > the value of "
4205 "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
4206 caller
, first
, count
,
4207 ctx
->Const
.MaxTransformFeedbackBuffers
);
4215 * Unbind all transform feedback buffers in the range
4216 * <first> through <first>+<count>-1
4219 unbind_xfb_buffers(struct gl_context
*ctx
,
4220 struct gl_transform_feedback_object
*tfObj
,
4221 GLuint first
, GLsizei count
)
4223 for (int i
= 0; i
< count
; i
++)
4224 _mesa_set_transform_feedback_binding(ctx
, tfObj
, first
+ i
,
4229 bind_xfb_buffers(struct gl_context
*ctx
,
4230 GLuint first
, GLsizei count
,
4231 const GLuint
*buffers
,
4233 const GLintptr
*offsets
,
4234 const GLsizeiptr
*sizes
,
4237 struct gl_transform_feedback_object
*tfObj
=
4238 ctx
->TransformFeedback
.CurrentObject
;
4240 if (!error_check_bind_xfb_buffers(ctx
, tfObj
, first
, count
, caller
))
4243 /* Assume that at least one binding will be changed */
4244 FLUSH_VERTICES(ctx
, 0);
4245 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewTransformFeedback
;
4248 /* The ARB_multi_bind spec says:
4250 * "If <buffers> is NULL, all bindings from <first> through
4251 * <first>+<count>-1 are reset to their unbound (zero) state.
4252 * In this case, the offsets and sizes associated with the
4253 * binding points are set to default values, ignoring
4254 * <offsets> and <sizes>."
4256 unbind_xfb_buffers(ctx
, tfObj
, first
, count
);
4260 /* Note that the error semantics for multi-bind commands differ from
4261 * those of other GL commands.
4263 * The Issues section in the ARB_multi_bind spec says:
4265 * "(11) Typically, OpenGL specifies that if an error is generated by a
4266 * command, that command has no effect. This is somewhat
4267 * unfortunate for multi-bind commands, because it would require a
4268 * first pass to scan the entire list of bound objects for errors
4269 * and then a second pass to actually perform the bindings.
4270 * Should we have different error semantics?
4272 * RESOLVED: Yes. In this specification, when the parameters for
4273 * one of the <count> binding points are invalid, that binding point
4274 * is not updated and an error will be generated. However, other
4275 * binding points in the same command will be updated if their
4276 * parameters are valid and no other error occurs."
4279 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4281 for (int i
= 0; i
< count
; i
++) {
4282 const GLuint index
= first
+ i
;
4283 struct gl_buffer_object
* const boundBufObj
= tfObj
->Buffers
[index
];
4284 struct gl_buffer_object
*bufObj
;
4285 GLintptr offset
= 0;
4286 GLsizeiptr size
= 0;
4289 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4292 /* The ARB_multi_bind spec says:
4294 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4295 * pair of values in <offsets> and <sizes> does not respectively
4296 * satisfy the constraints described for those parameters for the
4297 * specified target, as described in section 6.7.1 (per binding)."
4299 * Section 6.7.1 refers to table 6.5, which says:
4301 * "┌───────────────────────────────────────────────────────────────┐
4302 * │ Transform feedback array bindings (see sec. 13.2.2) │
4303 * ├───────────────────────┬───────────────────────────────────────┤
4305 * │ offset restriction │ multiple of 4 │
4307 * │ size restriction │ multiple of 4 │
4308 * └───────────────────────┴───────────────────────────────────────┘"
4310 if (offsets
[i
] & 0x3) {
4311 _mesa_error(ctx
, GL_INVALID_VALUE
,
4312 "glBindBuffersRange(offsets[%u]=%" PRId64
4313 " is misaligned; it must be a multiple of 4 when "
4314 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4315 i
, (int64_t) offsets
[i
]);
4319 if (sizes
[i
] & 0x3) {
4320 _mesa_error(ctx
, GL_INVALID_VALUE
,
4321 "glBindBuffersRange(sizes[%u]=%" PRId64
4322 " is misaligned; it must be a multiple of 4 when "
4323 "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
4324 i
, (int64_t) sizes
[i
]);
4328 offset
= offsets
[i
];
4332 if (boundBufObj
&& boundBufObj
->Name
== buffers
[i
])
4333 bufObj
= boundBufObj
;
4336 bufObj
= _mesa_multi_bind_lookup_bufferobj(ctx
, buffers
, i
, caller
,
4342 _mesa_set_transform_feedback_binding(ctx
, tfObj
, index
, bufObj
,
4346 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4350 error_check_bind_atomic_buffers(struct gl_context
*ctx
,
4351 GLuint first
, GLsizei count
,
4354 if (!ctx
->Extensions
.ARB_shader_atomic_counters
) {
4355 _mesa_error(ctx
, GL_INVALID_ENUM
,
4356 "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller
);
4360 /* The ARB_multi_bind_spec says:
4362 * "An INVALID_OPERATION error is generated if <first> + <count> is
4363 * greater than the number of target-specific indexed binding points,
4364 * as described in section 6.7.1."
4366 if (first
+ count
> ctx
->Const
.MaxAtomicBufferBindings
) {
4367 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4368 "%s(first=%u + count=%d > the value of "
4369 "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
4370 caller
, first
, count
, ctx
->Const
.MaxAtomicBufferBindings
);
4378 * Unbind all atomic counter buffers in the range
4379 * <first> through <first>+<count>-1
4382 unbind_atomic_buffers(struct gl_context
*ctx
, GLuint first
, GLsizei count
)
4384 for (int i
= 0; i
< count
; i
++)
4385 set_buffer_binding(ctx
, &ctx
->AtomicBufferBindings
[first
+ i
],
4386 NULL
, -1, -1, GL_TRUE
, 0);
4390 bind_atomic_buffers(struct gl_context
*ctx
,
4393 const GLuint
*buffers
,
4395 const GLintptr
*offsets
,
4396 const GLsizeiptr
*sizes
,
4399 if (!error_check_bind_atomic_buffers(ctx
, first
, count
, caller
))
4402 /* Assume that at least one binding will be changed */
4403 FLUSH_VERTICES(ctx
, 0);
4404 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
4407 /* The ARB_multi_bind spec says:
4409 * "If <buffers> is NULL, all bindings from <first> through
4410 * <first>+<count>-1 are reset to their unbound (zero) state.
4411 * In this case, the offsets and sizes associated with the
4412 * binding points are set to default values, ignoring
4413 * <offsets> and <sizes>."
4415 unbind_atomic_buffers(ctx
, first
, count
);
4419 /* Note that the error semantics for multi-bind commands differ from
4420 * those of other GL commands.
4422 * The Issues section in the ARB_multi_bind spec says:
4424 * "(11) Typically, OpenGL specifies that if an error is generated by a
4425 * command, that command has no effect. This is somewhat
4426 * unfortunate for multi-bind commands, because it would require a
4427 * first pass to scan the entire list of bound objects for errors
4428 * and then a second pass to actually perform the bindings.
4429 * Should we have different error semantics?
4431 * RESOLVED: Yes. In this specification, when the parameters for
4432 * one of the <count> binding points are invalid, that binding point
4433 * is not updated and an error will be generated. However, other
4434 * binding points in the same command will be updated if their
4435 * parameters are valid and no other error occurs."
4438 _mesa_HashLockMutex(ctx
->Shared
->BufferObjects
);
4440 for (int i
= 0; i
< count
; i
++) {
4441 struct gl_buffer_binding
*binding
=
4442 &ctx
->AtomicBufferBindings
[first
+ i
];
4443 GLintptr offset
= 0;
4444 GLsizeiptr size
= 0;
4447 if (!bind_buffers_check_offset_and_size(ctx
, i
, offsets
, sizes
))
4450 /* The ARB_multi_bind spec says:
4452 * "An INVALID_VALUE error is generated by BindBuffersRange if any
4453 * pair of values in <offsets> and <sizes> does not respectively
4454 * satisfy the constraints described for those parameters for the
4455 * specified target, as described in section 6.7.1 (per binding)."
4457 * Section 6.7.1 refers to table 6.5, which says:
4459 * "┌───────────────────────────────────────────────────────────────┐
4460 * │ Atomic counter array bindings (see sec. 7.7.2) │
4461 * ├───────────────────────┬───────────────────────────────────────┤
4463 * │ offset restriction │ multiple of 4 │
4465 * │ size restriction │ none │
4466 * └───────────────────────┴───────────────────────────────────────┘"
4468 if (offsets
[i
] & (ATOMIC_COUNTER_SIZE
- 1)) {
4469 _mesa_error(ctx
, GL_INVALID_VALUE
,
4470 "glBindBuffersRange(offsets[%u]=%" PRId64
4471 " is misaligned; it must be a multiple of %d when "
4472 "target=GL_ATOMIC_COUNTER_BUFFER)",
4473 i
, (int64_t) offsets
[i
], ATOMIC_COUNTER_SIZE
);
4477 offset
= offsets
[i
];
4481 set_buffer_multi_binding(ctx
, buffers
, i
, caller
,
4482 binding
, offset
, size
, range
,
4483 USAGE_ATOMIC_COUNTER_BUFFER
);
4486 _mesa_HashUnlockMutex(ctx
->Shared
->BufferObjects
);
4489 static ALWAYS_INLINE
void
4490 bind_buffer_range(GLenum target
, GLuint index
, GLuint buffer
, GLintptr offset
,
4491 GLsizeiptr size
, bool no_error
)
4493 GET_CURRENT_CONTEXT(ctx
);
4494 struct gl_buffer_object
*bufObj
;
4496 if (MESA_VERBOSE
& VERBOSE_API
) {
4497 _mesa_debug(ctx
, "glBindBufferRange(%s, %u, %u, %lu, %lu)\n",
4498 _mesa_enum_to_string(target
), index
, buffer
,
4499 (unsigned long) offset
, (unsigned long) size
);
4505 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4506 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4507 &bufObj
, "glBindBufferRange"))
4510 if (!no_error
&& !bufObj
) {
4511 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4512 "glBindBufferRange(invalid buffer=%u)", buffer
);
4519 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4520 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4521 index
, bufObj
, offset
, size
);
4523 case GL_UNIFORM_BUFFER
:
4524 bind_buffer_range_uniform_buffer(ctx
, index
, bufObj
, offset
, size
);
4526 case GL_SHADER_STORAGE_BUFFER
:
4527 bind_buffer_range_shader_storage_buffer(ctx
, index
, bufObj
, offset
,
4530 case GL_ATOMIC_COUNTER_BUFFER
:
4531 bind_buffer_range_atomic_buffer(ctx
, index
, bufObj
, offset
, size
);
4534 unreachable("invalid BindBufferRange target with KHR_no_error");
4539 _mesa_error(ctx
, GL_INVALID_VALUE
, "glBindBufferRange(size=%d)",
4546 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4547 if (!_mesa_validate_buffer_range_xfb(ctx
,
4548 ctx
->TransformFeedback
.CurrentObject
,
4549 index
, bufObj
, offset
, size
,
4553 _mesa_bind_buffer_range_xfb(ctx
, ctx
->TransformFeedback
.CurrentObject
,
4554 index
, bufObj
, offset
, size
);
4556 case GL_UNIFORM_BUFFER
:
4557 bind_buffer_range_uniform_buffer_err(ctx
, index
, bufObj
, offset
,
4560 case GL_SHADER_STORAGE_BUFFER
:
4561 bind_buffer_range_shader_storage_buffer_err(ctx
, index
, bufObj
,
4564 case GL_ATOMIC_COUNTER_BUFFER
:
4565 bind_buffer_range_atomic_buffer_err(ctx
, index
, bufObj
,
4569 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferRange(target)");
4576 _mesa_BindBufferRange_no_error(GLenum target
, GLuint index
, GLuint buffer
,
4577 GLintptr offset
, GLsizeiptr size
)
4579 bind_buffer_range(target
, index
, buffer
, offset
, size
, true);
4583 _mesa_BindBufferRange(GLenum target
, GLuint index
,
4584 GLuint buffer
, GLintptr offset
, GLsizeiptr size
)
4586 bind_buffer_range(target
, index
, buffer
, offset
, size
, false);
4590 _mesa_BindBufferBase(GLenum target
, GLuint index
, GLuint buffer
)
4592 GET_CURRENT_CONTEXT(ctx
);
4593 struct gl_buffer_object
*bufObj
;
4595 if (MESA_VERBOSE
& VERBOSE_API
) {
4596 _mesa_debug(ctx
, "glBindBufferBase(%s, %u, %u)\n",
4597 _mesa_enum_to_string(target
), index
, buffer
);
4603 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4604 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
,
4605 &bufObj
, "glBindBufferBase"))
4609 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4610 "glBindBufferBase(invalid buffer=%u)", buffer
);
4615 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
4616 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
4618 * "BindBufferBase is equivalent to calling BindBufferRange with offset
4619 * zero and size equal to the size of buffer."
4621 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
4623 * "If the parameter (starting offset or size) was not specified when the
4624 * buffer object was bound, zero is returned."
4626 * What happens if the size of the buffer changes? Does the size of the
4627 * buffer at the moment glBindBufferBase was called still play a role, like
4628 * the first quote would imply, or is the size meaningless in the
4629 * glBindBufferBase case like the second quote would suggest? The GL 4.1
4630 * core spec page 45 says:
4632 * "It is equivalent to calling BindBufferRange with offset zero, while
4633 * size is determined by the size of the bound buffer at the time the
4636 * My interpretation is that the GL 4.1 spec was a clarification of the
4637 * behavior, not a change. In particular, this choice will only make
4638 * rendering work in cases where it would have had undefined results.
4642 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4643 _mesa_bind_buffer_base_transform_feedback(ctx
,
4644 ctx
->TransformFeedback
.CurrentObject
,
4645 index
, bufObj
, false);
4647 case GL_UNIFORM_BUFFER
:
4648 bind_buffer_base_uniform_buffer(ctx
, index
, bufObj
);
4650 case GL_SHADER_STORAGE_BUFFER
:
4651 bind_buffer_base_shader_storage_buffer(ctx
, index
, bufObj
);
4653 case GL_ATOMIC_COUNTER_BUFFER
:
4654 bind_buffer_base_atomic_buffer(ctx
, index
, bufObj
);
4657 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBufferBase(target)");
4663 _mesa_BindBuffersRange(GLenum target
, GLuint first
, GLsizei count
,
4664 const GLuint
*buffers
,
4665 const GLintptr
*offsets
, const GLsizeiptr
*sizes
)
4667 GET_CURRENT_CONTEXT(ctx
);
4669 if (MESA_VERBOSE
& VERBOSE_API
) {
4670 _mesa_debug(ctx
, "glBindBuffersRange(%s, %u, %d, %p, %p, %p)\n",
4671 _mesa_enum_to_string(target
), first
, count
,
4672 buffers
, offsets
, sizes
);
4676 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4677 bind_xfb_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4678 "glBindBuffersRange");
4680 case GL_UNIFORM_BUFFER
:
4681 bind_uniform_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4682 "glBindBuffersRange");
4684 case GL_SHADER_STORAGE_BUFFER
:
4685 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4686 "glBindBuffersRange");
4688 case GL_ATOMIC_COUNTER_BUFFER
:
4689 bind_atomic_buffers(ctx
, first
, count
, buffers
, true, offsets
, sizes
,
4690 "glBindBuffersRange");
4693 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersRange(target=%s)",
4694 _mesa_enum_to_string(target
));
4700 _mesa_BindBuffersBase(GLenum target
, GLuint first
, GLsizei count
,
4701 const GLuint
*buffers
)
4703 GET_CURRENT_CONTEXT(ctx
);
4705 if (MESA_VERBOSE
& VERBOSE_API
) {
4706 _mesa_debug(ctx
, "glBindBuffersBase(%s, %u, %d, %p)\n",
4707 _mesa_enum_to_string(target
), first
, count
, buffers
);
4711 case GL_TRANSFORM_FEEDBACK_BUFFER
:
4712 bind_xfb_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4713 "glBindBuffersBase");
4715 case GL_UNIFORM_BUFFER
:
4716 bind_uniform_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4717 "glBindBuffersBase");
4719 case GL_SHADER_STORAGE_BUFFER
:
4720 bind_shader_storage_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4721 "glBindBuffersBase");
4723 case GL_ATOMIC_COUNTER_BUFFER
:
4724 bind_atomic_buffers(ctx
, first
, count
, buffers
, false, NULL
, NULL
,
4725 "glBindBuffersBase");
4728 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBindBuffersBase(target=%s)",
4729 _mesa_enum_to_string(target
));
4734 static ALWAYS_INLINE
void
4735 invalidate_buffer_subdata(struct gl_context
*ctx
,
4736 struct gl_buffer_object
*bufObj
, GLintptr offset
,
4739 if (ctx
->Driver
.InvalidateBufferSubData
)
4740 ctx
->Driver
.InvalidateBufferSubData(ctx
, bufObj
, offset
, length
);
4744 _mesa_InvalidateBufferSubData_no_error(GLuint buffer
, GLintptr offset
,
4747 GET_CURRENT_CONTEXT(ctx
);
4749 struct gl_buffer_object
*bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4750 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4754 _mesa_InvalidateBufferSubData(GLuint buffer
, GLintptr offset
,
4757 GET_CURRENT_CONTEXT(ctx
);
4758 struct gl_buffer_object
*bufObj
;
4759 const GLintptr end
= offset
+ length
;
4761 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4762 * Profile) spec says:
4764 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4765 * name of an existing buffer object."
4767 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4768 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4769 _mesa_error(ctx
, GL_INVALID_VALUE
,
4770 "glInvalidateBufferSubData(name = %u) invalid object",
4775 /* The GL_ARB_invalidate_subdata spec says:
4777 * "An INVALID_VALUE error is generated if <offset> or <length> is
4778 * negative, or if <offset> + <length> is greater than the value of
4781 if (offset
< 0 || length
< 0 || end
> bufObj
->Size
) {
4782 _mesa_error(ctx
, GL_INVALID_VALUE
,
4783 "glInvalidateBufferSubData(invalid offset or length)");
4787 /* The OpenGL 4.4 (Core Profile) spec says:
4789 * "An INVALID_OPERATION error is generated if buffer is currently
4790 * mapped by MapBuffer or if the invalidate range intersects the range
4791 * currently mapped by MapBufferRange, unless it was mapped
4792 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4794 if (!(bufObj
->Mappings
[MAP_USER
].AccessFlags
& GL_MAP_PERSISTENT_BIT
) &&
4795 bufferobj_range_mapped(bufObj
, offset
, length
)) {
4796 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4797 "glInvalidateBufferSubData(intersection with mapped "
4802 invalidate_buffer_subdata(ctx
, bufObj
, offset
, length
);
4806 _mesa_InvalidateBufferData_no_error(GLuint buffer
)
4808 GET_CURRENT_CONTEXT(ctx
);
4810 struct gl_buffer_object
*bufObj
=_mesa_lookup_bufferobj(ctx
, buffer
);
4811 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4815 _mesa_InvalidateBufferData(GLuint buffer
)
4817 GET_CURRENT_CONTEXT(ctx
);
4818 struct gl_buffer_object
*bufObj
;
4820 /* Section 6.5 (Invalidating Buffer Data) of the OpenGL 4.5 (Compatibility
4821 * Profile) spec says:
4823 * "An INVALID_VALUE error is generated if buffer is zero or is not the
4824 * name of an existing buffer object."
4826 bufObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4827 if (!bufObj
|| bufObj
== &DummyBufferObject
) {
4828 _mesa_error(ctx
, GL_INVALID_VALUE
,
4829 "glInvalidateBufferData(name = %u) invalid object",
4834 /* The OpenGL 4.4 (Core Profile) spec says:
4836 * "An INVALID_OPERATION error is generated if buffer is currently
4837 * mapped by MapBuffer or if the invalidate range intersects the range
4838 * currently mapped by MapBufferRange, unless it was mapped
4839 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
4841 if (_mesa_check_disallowed_mapping(bufObj
)) {
4842 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4843 "glInvalidateBufferData(intersection with mapped "
4848 invalidate_buffer_subdata(ctx
, bufObj
, 0, bufObj
->Size
);
4852 buffer_page_commitment(struct gl_context
*ctx
,
4853 struct gl_buffer_object
*bufferObj
,
4854 GLintptr offset
, GLsizeiptr size
,
4855 GLboolean commit
, const char *func
)
4857 if (!(bufferObj
->StorageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)) {
4858 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(not a sparse buffer object)",
4863 if (size
< 0 || size
> bufferObj
->Size
||
4864 offset
< 0 || offset
> bufferObj
->Size
- size
) {
4865 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(out of bounds)",
4870 /* The GL_ARB_sparse_buffer extension specification says:
4872 * "INVALID_VALUE is generated by BufferPageCommitmentARB if <offset> is
4873 * not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB, or if <size>
4874 * is not an integer multiple of SPARSE_BUFFER_PAGE_SIZE_ARB and does
4875 * not extend to the end of the buffer's data store."
4877 if (offset
% ctx
->Const
.SparseBufferPageSize
!= 0) {
4878 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(offset not aligned to page size)",
4883 if (size
% ctx
->Const
.SparseBufferPageSize
!= 0 &&
4884 offset
+ size
!= bufferObj
->Size
) {
4885 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(size not aligned to page size)",
4890 ctx
->Driver
.BufferPageCommitment(ctx
, bufferObj
, offset
, size
, commit
);
4894 _mesa_BufferPageCommitmentARB(GLenum target
, GLintptr offset
, GLsizeiptr size
,
4897 GET_CURRENT_CONTEXT(ctx
);
4898 struct gl_buffer_object
*bufferObj
;
4900 bufferObj
= get_buffer(ctx
, "glBufferPageCommitmentARB", target
,
4905 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4906 "glBufferPageCommitmentARB");
4910 _mesa_NamedBufferPageCommitmentARB(GLuint buffer
, GLintptr offset
,
4911 GLsizeiptr size
, GLboolean commit
)
4913 GET_CURRENT_CONTEXT(ctx
);
4914 struct gl_buffer_object
*bufferObj
;
4916 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4917 if (!bufferObj
|| bufferObj
== &DummyBufferObject
) {
4918 /* Note: the extension spec is not clear about the excpected error value. */
4919 _mesa_error(ctx
, GL_INVALID_VALUE
,
4920 "glNamedBufferPageCommitmentARB(name = %u) invalid object",
4925 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4926 "glNamedBufferPageCommitmentARB");
4930 _mesa_NamedBufferPageCommitmentEXT(GLuint buffer
, GLintptr offset
,
4931 GLsizeiptr size
, GLboolean commit
)
4933 GET_CURRENT_CONTEXT(ctx
);
4934 struct gl_buffer_object
*bufferObj
;
4936 /* Use NamedBuffer* functions logic from EXT_direct_state_access */
4938 bufferObj
= _mesa_lookup_bufferobj(ctx
, buffer
);
4939 if (!_mesa_handle_bind_buffer_gen(ctx
, buffer
, &bufferObj
,
4940 "glNamedBufferPageCommitmentEXT"))
4943 /* GL_EXT_direct_state_access says about NamedBuffer* functions:
4945 * There is no buffer corresponding to the name zero, these commands
4946 * generate the INVALID_OPERATION error if the buffer parameter is
4949 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4950 "glNamedBufferPageCommitmentEXT(buffer = 0)");
4953 buffer_page_commitment(ctx
, bufferObj
, offset
, size
, commit
,
4954 "glNamedBufferPageCommitmentEXT");